* decl.c (maybe_pad_type): Only generate the XVS parallel type if
[official-gcc.git] / gcc / ada / decl.c
blob525dad807fa7a7fa88c74a4f8430e20470ed381d
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2008, Free Software Foundation, Inc. *
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 ****************************************************************************/
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"
33 #include "convert.h"
34 #include "ggc.h"
35 #include "obstack.h"
36 #include "target.h"
37 #include "expr.h"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "stringt.h"
48 #include "uintp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "hashtab.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 #ifndef MAX_FIXED_MODE_SIZE
57 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 #endif
60 /* Convention_Stdcall should be processed in a specific way on Windows targets
61 only. The macro below is a helper to avoid having to check for a Windows
62 specific attribute throughout this unit. */
64 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #else
67 #define Has_Stdcall_Convention(E) (0)
68 #endif
70 struct incomplete
72 struct incomplete *next;
73 tree old_type;
74 Entity_Id full_type;
77 /* These variables are used to defer recursively expanding incomplete types
78 while we are processing an array, a record or a subprogram type. */
79 static int defer_incomplete_level = 0;
80 static struct incomplete *defer_incomplete_list;
82 /* This variable is used to delay expanding From_With_Type types until the
83 end of the spec. */
84 static struct incomplete *defer_limited_with;
86 /* These variables are used to defer finalizing types. The element of the
87 list is the TYPE_DECL associated with the type. */
88 static int defer_finalize_level = 0;
89 static VEC (tree,heap) *defer_finalize_list;
91 /* A hash table used to cache the result of annotate_value. */
92 static GTY ((if_marked ("tree_int_map_marked_p"),
93 param_is (struct tree_int_map))) htab_t annotate_value_cache;
95 static void copy_alias_set (tree, tree);
96 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
97 static bool allocatable_size_p (tree, bool);
98 static void prepend_one_attribute_to (struct attrib **,
99 enum attr_type, tree, tree, Node_Id);
100 static void prepend_attributes (Entity_Id, struct attrib **);
101 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
102 static bool is_variable_size (tree);
103 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
104 bool, bool);
105 static tree make_packable_type (tree, bool);
106 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
107 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
108 bool *);
109 static bool same_discriminant_p (Entity_Id, Entity_Id);
110 static bool array_type_has_nonaliased_component (Entity_Id, tree);
111 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
112 bool, bool, bool, bool);
113 static Uint annotate_value (tree);
114 static void annotate_rep (Entity_Id, tree);
115 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
116 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
117 static void set_rm_size (Uint, tree, Entity_Id);
118 static tree make_type_from_size (tree, tree, bool);
119 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
120 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
121 static void check_ok_for_atomic (tree, Entity_Id, bool);
122 static int compatible_signatures_p (tree ftype1, tree ftype2);
123 static void rest_of_type_decl_compilation_no_defer (tree);
125 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
126 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
127 refer to an Ada type. */
129 tree
130 gnat_to_gnu_type (Entity_Id gnat_entity)
132 tree gnu_decl;
134 /* The back end never attempts to annotate generic types */
135 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
136 return void_type_node;
138 /* Convert the ada entity type into a GCC TYPE_DECL node. */
139 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
140 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
141 return TREE_TYPE (gnu_decl);
144 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
145 entity, this routine returns the equivalent GCC tree for that entity
146 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
147 defining identifier.
149 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
150 initial value (in GCC tree form). This is optional for variables.
151 For renamed entities, GNU_EXPR gives the object being renamed.
153 DEFINITION is nonzero if this call is intended for a definition. This is
154 used for separate compilation where it necessary to know whether an
155 external declaration or a definition should be created if the GCC equivalent
156 was not created previously. The value of 1 is normally used for a nonzero
157 DEFINITION, but a value of 2 is used in special circumstances, defined in
158 the code. */
160 tree
161 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
163 Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
164 tree gnu_entity_id;
165 tree gnu_type = NULL_TREE;
166 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
167 GNAT tree. This node will be associated with the GNAT node by calling
168 the save_gnu_tree routine at the end of the `switch' statement. */
169 tree gnu_decl = NULL_TREE;
170 /* true if we have already saved gnu_decl as a gnat association. */
171 bool saved = false;
172 /* Nonzero if we incremented defer_incomplete_level. */
173 bool this_deferred = false;
174 /* Nonzero if we incremented force_global. */
175 bool this_global = false;
176 /* Nonzero if we should check to see if elaborated during processing. */
177 bool maybe_present = false;
178 /* Nonzero if we made GNU_DECL and its type here. */
179 bool this_made_decl = false;
180 struct attrib *attr_list = NULL;
181 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
182 || debug_info_level == DINFO_LEVEL_VERBOSE);
183 Entity_Kind kind = Ekind (gnat_entity);
184 Entity_Id gnat_temp;
185 unsigned int esize
186 = ((Known_Esize (gnat_entity)
187 && UI_Is_In_Int_Range (Esize (gnat_entity)))
188 ? MIN (UI_To_Int (Esize (gnat_entity)),
189 IN (kind, Float_Kind)
190 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
191 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
192 : LONG_LONG_TYPE_SIZE)
193 : LONG_LONG_TYPE_SIZE);
194 tree gnu_size = 0;
195 bool imported_p
196 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
197 unsigned int align = 0;
199 /* Since a use of an Itype is a definition, process it as such if it
200 is not in a with'ed unit. */
202 if (!definition && Is_Itype (gnat_entity)
203 && !present_gnu_tree (gnat_entity)
204 && In_Extended_Main_Code_Unit (gnat_entity))
206 /* Ensure that we are in a subprogram mentioned in the Scope
207 chain of this entity, our current scope is global,
208 or that we encountered a task or entry (where we can't currently
209 accurately check scoping). */
210 if (!current_function_decl
211 || DECL_ELABORATION_PROC_P (current_function_decl))
213 process_type (gnat_entity);
214 return get_gnu_tree (gnat_entity);
217 for (gnat_temp = Scope (gnat_entity);
218 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
220 if (Is_Type (gnat_temp))
221 gnat_temp = Underlying_Type (gnat_temp);
223 if (Ekind (gnat_temp) == E_Subprogram_Body)
224 gnat_temp
225 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
227 if (IN (Ekind (gnat_temp), Subprogram_Kind)
228 && Present (Protected_Body_Subprogram (gnat_temp)))
229 gnat_temp = Protected_Body_Subprogram (gnat_temp);
231 if (Ekind (gnat_temp) == E_Entry
232 || Ekind (gnat_temp) == E_Entry_Family
233 || Ekind (gnat_temp) == E_Task_Type
234 || (IN (Ekind (gnat_temp), Subprogram_Kind)
235 && present_gnu_tree (gnat_temp)
236 && (current_function_decl
237 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
239 process_type (gnat_entity);
240 return get_gnu_tree (gnat_entity);
244 /* This abort means the entity "gnat_entity" has an incorrect scope,
245 i.e. that its scope does not correspond to the subprogram in which
246 it is declared */
247 gcc_unreachable ();
250 /* If this is entity 0, something went badly wrong. */
251 gcc_assert (Present (gnat_entity));
253 /* If we've already processed this entity, return what we got last time.
254 If we are defining the node, we should not have already processed it.
255 In that case, we will abort below when we try to save a new GCC tree for
256 this object. We also need to handle the case of getting a dummy type
257 when a Full_View exists. */
259 if (present_gnu_tree (gnat_entity)
260 && (!definition || (Is_Type (gnat_entity) && imported_p)))
262 gnu_decl = get_gnu_tree (gnat_entity);
264 if (TREE_CODE (gnu_decl) == TYPE_DECL
265 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
266 && IN (kind, Incomplete_Or_Private_Kind)
267 && Present (Full_View (gnat_entity)))
269 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
270 NULL_TREE, 0);
272 save_gnu_tree (gnat_entity, NULL_TREE, false);
273 save_gnu_tree (gnat_entity, gnu_decl, false);
276 return gnu_decl;
279 /* If this is a numeric or enumeral type, or an access type, a nonzero
280 Esize must be specified unless it was specified by the programmer. */
281 gcc_assert (!Unknown_Esize (gnat_entity)
282 || Has_Size_Clause (gnat_entity)
283 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
284 && (!IN (kind, Access_Kind)
285 || kind == E_Access_Protected_Subprogram_Type
286 || kind == E_Anonymous_Access_Protected_Subprogram_Type
287 || kind == E_Access_Subtype)));
289 /* Likewise, RM_Size must be specified for all discrete and fixed-point
290 types. */
291 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
292 || !Unknown_RM_Size (gnat_entity));
294 /* Get the name of the entity and set up the line number and filename of
295 the original definition for use in any decl we make. */
296 gnu_entity_id = get_entity_name (gnat_entity);
297 Sloc_to_locus (Sloc (gnat_entity), &input_location);
299 /* If we get here, it means we have not yet done anything with this
300 entity. If we are not defining it here, it must be external,
301 otherwise we should have defined it already. */
302 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
303 || kind == E_Discriminant || kind == E_Component
304 || kind == E_Label
305 || (kind == E_Constant && Present (Full_View (gnat_entity)))
306 || IN (kind, Type_Kind));
308 /* For cases when we are not defining (i.e., we are referencing from
309 another compilation unit) Public entities, show we are at global level
310 for the purpose of computing scopes. Don't do this for components or
311 discriminants since the relevant test is whether or not the record is
312 being defined. But do this for Imported functions or procedures in
313 all cases. */
314 if ((!definition && Is_Public (gnat_entity)
315 && !Is_Statically_Allocated (gnat_entity)
316 && kind != E_Discriminant && kind != E_Component)
317 || (Is_Imported (gnat_entity)
318 && (kind == E_Function || kind == E_Procedure)))
319 force_global++, this_global = true;
321 /* Handle any attributes directly attached to the entity. */
322 if (Has_Gigi_Rep_Item (gnat_entity))
323 prepend_attributes (gnat_entity, &attr_list);
325 /* Machine_Attributes on types are expected to be propagated to subtypes.
326 The corresponding Gigi_Rep_Items are only attached to the first subtype
327 though, so we handle the propagation here. */
328 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
329 && !Is_First_Subtype (gnat_entity)
330 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
331 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
333 switch (kind)
335 case E_Constant:
336 /* If this is a use of a deferred constant, get its full
337 declaration. */
338 if (!definition && Present (Full_View (gnat_entity)))
340 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
341 gnu_expr, 0);
342 saved = true;
343 break;
346 /* If we have an external constant that we are not defining, get the
347 expression that is was defined to represent. We may throw that
348 expression away later if it is not a constant. Do not retrieve the
349 expression if it is an aggregate or allocator, because in complex
350 instantiation contexts it may not be expanded */
351 if (!definition
352 && Present (Expression (Declaration_Node (gnat_entity)))
353 && !No_Initialization (Declaration_Node (gnat_entity))
354 && (Nkind (Expression (Declaration_Node (gnat_entity)))
355 != N_Aggregate)
356 && (Nkind (Expression (Declaration_Node (gnat_entity)))
357 != N_Allocator))
358 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
360 /* Ignore deferred constant definitions; they are processed fully in the
361 front-end. For deferred constant references get the full definition.
362 On the other hand, constants that are renamings are handled like
363 variable renamings. If No_Initialization is set, this is not a
364 deferred constant but a constant whose value is built manually. */
365 if (definition && !gnu_expr
366 && !No_Initialization (Declaration_Node (gnat_entity))
367 && No (Renamed_Object (gnat_entity)))
369 gnu_decl = error_mark_node;
370 saved = true;
371 break;
373 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
374 && Present (Full_View (gnat_entity)))
376 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
377 NULL_TREE, 0);
378 saved = true;
379 break;
382 goto object;
384 case E_Exception:
385 /* We used to special case VMS exceptions here to directly map them to
386 their associated condition code. Since this code had to be masked
387 dynamically to strip off the severity bits, this caused trouble in
388 the GCC/ZCX case because the "type" pointers we store in the tables
389 have to be static. We now don't special case here anymore, and let
390 the regular processing take place, which leaves us with a regular
391 exception data object for VMS exceptions too. The condition code
392 mapping is taken care of by the front end and the bitmasking by the
393 runtime library. */
394 goto object;
396 case E_Discriminant:
397 case E_Component:
399 /* The GNAT record where the component was defined. */
400 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
402 /* If the variable is an inherited record component (in the case of
403 extended record types), just return the inherited entity, which
404 must be a FIELD_DECL. Likewise for discriminants.
405 For discriminants of untagged records which have explicit
406 stored discriminants, return the entity for the corresponding
407 stored discriminant. Also use Original_Record_Component
408 if the record has a private extension. */
410 if (Present (Original_Record_Component (gnat_entity))
411 && Original_Record_Component (gnat_entity) != gnat_entity)
413 gnu_decl
414 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
415 gnu_expr, definition);
416 saved = true;
417 break;
420 /* If the enclosing record has explicit stored discriminants,
421 then it is an untagged record. If the Corresponding_Discriminant
422 is not empty then this must be a renamed discriminant and its
423 Original_Record_Component must point to the corresponding explicit
424 stored discriminant (i.e., we should have taken the previous
425 branch). */
427 else if (Present (Corresponding_Discriminant (gnat_entity))
428 && Is_Tagged_Type (gnat_record))
430 /* A tagged record has no explicit stored discriminants. */
432 gcc_assert (First_Discriminant (gnat_record)
433 == First_Stored_Discriminant (gnat_record));
434 gnu_decl
435 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
436 gnu_expr, definition);
437 saved = true;
438 break;
441 else if (Present (CR_Discriminant (gnat_entity))
442 && type_annotate_only)
444 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
445 gnu_expr, definition);
446 saved = true;
447 break;
450 /* If the enclosing record has explicit stored discriminants,
451 then it is an untagged record. If the Corresponding_Discriminant
452 is not empty then this must be a renamed discriminant and its
453 Original_Record_Component must point to the corresponding explicit
454 stored discriminant (i.e., we should have taken the first
455 branch). */
457 else if (Present (Corresponding_Discriminant (gnat_entity))
458 && (First_Discriminant (gnat_record)
459 != First_Stored_Discriminant (gnat_record)))
460 gcc_unreachable ();
462 /* Otherwise, if we are not defining this and we have no GCC type
463 for the containing record, make one for it. Then we should
464 have made our own equivalent. */
465 else if (!definition && !present_gnu_tree (gnat_record))
467 /* ??? If this is in a record whose scope is a protected
468 type and we have an Original_Record_Component, use it.
469 This is a workaround for major problems in protected type
470 handling. */
471 Entity_Id Scop = Scope (Scope (gnat_entity));
472 if ((Is_Protected_Type (Scop)
473 || (Is_Private_Type (Scop)
474 && Present (Full_View (Scop))
475 && Is_Protected_Type (Full_View (Scop))))
476 && Present (Original_Record_Component (gnat_entity)))
478 gnu_decl
479 = gnat_to_gnu_entity (Original_Record_Component
480 (gnat_entity),
481 gnu_expr, 0);
482 saved = true;
483 break;
486 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
487 gnu_decl = get_gnu_tree (gnat_entity);
488 saved = true;
489 break;
492 else
493 /* Here we have no GCC type and this is a reference rather than a
494 definition. This should never happen. Most likely the cause is a
495 reference before declaration in the gnat tree for gnat_entity. */
496 gcc_unreachable ();
499 case E_Loop_Parameter:
500 case E_Out_Parameter:
501 case E_Variable:
503 /* Simple variables, loop variables, Out parameters, and exceptions. */
504 object:
506 bool used_by_ref = false;
507 bool const_flag
508 = ((kind == E_Constant || kind == E_Variable)
509 && Is_True_Constant (gnat_entity)
510 && (((Nkind (Declaration_Node (gnat_entity))
511 == N_Object_Declaration)
512 && Present (Expression (Declaration_Node (gnat_entity))))
513 || Present (Renamed_Object (gnat_entity))));
514 bool inner_const_flag = const_flag;
515 bool static_p = Is_Statically_Allocated (gnat_entity);
516 bool mutable_p = false;
517 tree gnu_ext_name = NULL_TREE;
518 tree renamed_obj = NULL_TREE;
519 tree gnu_object_size;
521 if (Present (Renamed_Object (gnat_entity)) && !definition)
523 if (kind == E_Exception)
524 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
525 NULL_TREE, 0);
526 else
527 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
530 /* Get the type after elaborating the renamed object. */
531 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
533 /* For a debug renaming declaration, build a pure debug entity. */
534 if (Present (Debug_Renaming_Link (gnat_entity)))
536 rtx addr;
537 gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
538 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
539 if (global_bindings_p ())
540 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
541 else
542 addr = stack_pointer_rtx;
543 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
544 gnat_pushdecl (gnu_decl, gnat_entity);
545 break;
548 /* If this is a loop variable, its type should be the base type.
549 This is because the code for processing a loop determines whether
550 a normal loop end test can be done by comparing the bounds of the
551 loop against those of the base type, which is presumed to be the
552 size used for computation. But this is not correct when the size
553 of the subtype is smaller than the type. */
554 if (kind == E_Loop_Parameter)
555 gnu_type = get_base_type (gnu_type);
557 /* Reject non-renamed objects whose types are unconstrained arrays or
558 any object whose type is a dummy type or VOID_TYPE. */
560 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
561 && No (Renamed_Object (gnat_entity)))
562 || TYPE_IS_DUMMY_P (gnu_type)
563 || TREE_CODE (gnu_type) == VOID_TYPE)
565 gcc_assert (type_annotate_only);
566 if (this_global)
567 force_global--;
568 return error_mark_node;
571 /* If an alignment is specified, use it if valid. Note that
572 exceptions are objects but don't have alignments. We must do this
573 before we validate the size, since the alignment can affect the
574 size. */
575 if (kind != E_Exception && Known_Alignment (gnat_entity))
577 gcc_assert (Present (Alignment (gnat_entity)));
578 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
579 TYPE_ALIGN (gnu_type));
580 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
581 "PAD", false, definition, true);
584 /* If we are defining the object, see if it has a Size value and
585 validate it if so. If we are not defining the object and a Size
586 clause applies, simply retrieve the value. We don't want to ignore
587 the clause and it is expected to have been validated already. Then
588 get the new type, if any. */
589 if (definition)
590 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
591 gnat_entity, VAR_DECL, false,
592 Has_Size_Clause (gnat_entity));
593 else if (Has_Size_Clause (gnat_entity))
594 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
596 if (gnu_size)
598 gnu_type
599 = make_type_from_size (gnu_type, gnu_size,
600 Has_Biased_Representation (gnat_entity));
602 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
603 gnu_size = NULL_TREE;
606 /* If this object has self-referential size, it must be a record with
607 a default value. We are supposed to allocate an object of the
608 maximum size in this case unless it is a constant with an
609 initializing expression, in which case we can get the size from
610 that. Note that the resulting size may still be a variable, so
611 this may end up with an indirect allocation. */
612 if (No (Renamed_Object (gnat_entity))
613 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
615 if (gnu_expr && kind == E_Constant)
617 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
618 if (CONTAINS_PLACEHOLDER_P (size))
620 /* If the initializing expression is itself a constant,
621 despite having a nominal type with self-referential
622 size, we can get the size directly from it. */
623 if (TREE_CODE (gnu_expr) == COMPONENT_REF
624 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
625 == RECORD_TYPE
626 && TYPE_IS_PADDING_P
627 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
628 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
629 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
630 || DECL_READONLY_ONCE_ELAB
631 (TREE_OPERAND (gnu_expr, 0))))
632 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
633 else
634 gnu_size
635 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
637 else
638 gnu_size = size;
640 /* We may have no GNU_EXPR because No_Initialization is
641 set even though there's an Expression. */
642 else if (kind == E_Constant
643 && (Nkind (Declaration_Node (gnat_entity))
644 == N_Object_Declaration)
645 && Present (Expression (Declaration_Node (gnat_entity))))
646 gnu_size
647 = TYPE_SIZE (gnat_to_gnu_type
648 (Etype
649 (Expression (Declaration_Node (gnat_entity)))));
650 else
652 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
653 mutable_p = true;
657 /* If the size is zero bytes, make it one byte since some linkers have
658 trouble with zero-sized objects. If the object will have a
659 template, that will make it nonzero so don't bother. Also avoid
660 doing that for an object renaming or an object with an address
661 clause, as we would lose useful information on the view size
662 (e.g. for null array slices) and we are not allocating the object
663 here anyway. */
664 if (((gnu_size
665 && integer_zerop (gnu_size)
666 && !TREE_OVERFLOW (gnu_size))
667 || (TYPE_SIZE (gnu_type)
668 && integer_zerop (TYPE_SIZE (gnu_type))
669 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
670 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
671 || !Is_Array_Type (Etype (gnat_entity)))
672 && !Present (Renamed_Object (gnat_entity))
673 && !Present (Address_Clause (gnat_entity)))
674 gnu_size = bitsize_unit_node;
676 /* If this is an object with no specified size and alignment, and if
677 either it is atomic or we are not optimizing alignment for space
678 and it is a non-scalar variable, and the size of its type is a
679 constant, set the alignment to the smallest not less than the
680 size, or to the biggest meaningful one, whichever is smaller. */
681 if (!gnu_size && align == 0
682 && (Is_Atomic (gnat_entity)
683 || (!Optimize_Alignment_Space (gnat_entity)
684 && kind == E_Variable
685 && AGGREGATE_TYPE_P (gnu_type)
686 && !const_flag && No (Renamed_Object (gnat_entity))
687 && !imported_p && No (Address_Clause (gnat_entity))))
688 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
690 /* No point in jumping through all the hoops needed in order
691 to support BIGGEST_ALIGNMENT if we don't really have to. */
692 unsigned int align_cap = Is_Atomic (gnat_entity)
693 ? BIGGEST_ALIGNMENT
694 : MAX_FIXED_MODE_SIZE;
696 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
697 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
698 align = align_cap;
699 else
700 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
702 /* But make sure not to under-align the object. */
703 if (align < TYPE_ALIGN (gnu_type))
704 align = TYPE_ALIGN (gnu_type);
706 /* And honor the minimum valid atomic alignment, if any. */
707 #ifdef MINIMUM_ATOMIC_ALIGNMENT
708 if (align < MINIMUM_ATOMIC_ALIGNMENT)
709 align = MINIMUM_ATOMIC_ALIGNMENT;
710 #endif
713 /* If the object is set to have atomic components, find the component
714 type and validate it.
716 ??? Note that we ignore Has_Volatile_Components on objects; it's
717 not at all clear what to do in that case. */
719 if (Has_Atomic_Components (gnat_entity))
721 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
722 ? TREE_TYPE (gnu_type) : gnu_type);
724 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
725 && TYPE_MULTI_ARRAY_P (gnu_inner))
726 gnu_inner = TREE_TYPE (gnu_inner);
728 check_ok_for_atomic (gnu_inner, gnat_entity, true);
731 /* Now check if the type of the object allows atomic access. Note
732 that we must test the type, even if this object has size and
733 alignment to allow such access, because we will be going
734 inside the padded record to assign to the object. We could fix
735 this by always copying via an intermediate value, but it's not
736 clear it's worth the effort. */
737 if (Is_Atomic (gnat_entity))
738 check_ok_for_atomic (gnu_type, gnat_entity, false);
740 /* If this is an aliased object with an unconstrained nominal subtype,
741 make a type that includes the template. */
742 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
743 && Is_Array_Type (Etype (gnat_entity))
744 && !type_annotate_only)
746 tree gnu_fat
747 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
749 gnu_type
750 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
751 concat_id_with_name (gnu_entity_id,
752 "UNC"));
755 #ifdef MINIMUM_ATOMIC_ALIGNMENT
756 /* If the size is a constant and no alignment is specified, force
757 the alignment to be the minimum valid atomic alignment. The
758 restriction on constant size avoids problems with variable-size
759 temporaries; if the size is variable, there's no issue with
760 atomic access. Also don't do this for a constant, since it isn't
761 necessary and can interfere with constant replacement. Finally,
762 do not do it for Out parameters since that creates an
763 size inconsistency with In parameters. */
764 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
765 && !FLOAT_TYPE_P (gnu_type)
766 && !const_flag && No (Renamed_Object (gnat_entity))
767 && !imported_p && No (Address_Clause (gnat_entity))
768 && kind != E_Out_Parameter
769 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
770 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
771 align = MINIMUM_ATOMIC_ALIGNMENT;
772 #endif
774 /* Make a new type with the desired size and alignment, if needed.
775 But do not take into account alignment promotions to compute the
776 size of the object. */
777 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
778 if (gnu_size || align > 0)
779 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
780 "PAD", false, definition,
781 gnu_size ? true : false);
783 /* Make a volatile version of this object's type if we are to make
784 the object volatile. We also interpret 13.3(19) conservatively
785 and disallow any optimizations for an object covered by it. */
786 if ((Treat_As_Volatile (gnat_entity)
787 || (Is_Exported (gnat_entity)
788 /* Exclude exported constants created by the compiler,
789 which should boil down to static dispatch tables and
790 make it possible to put them in read-only memory. */
791 && (Comes_From_Source (gnat_entity) || !const_flag))
792 || Is_Imported (gnat_entity)
793 || Present (Address_Clause (gnat_entity)))
794 && !TYPE_VOLATILE (gnu_type))
795 gnu_type = build_qualified_type (gnu_type,
796 (TYPE_QUALS (gnu_type)
797 | TYPE_QUAL_VOLATILE));
799 /* If this is a renaming, avoid as much as possible to create a new
800 object. However, in several cases, creating it is required.
801 This processing needs to be applied to the raw expression so
802 as to make it more likely to rename the underlying object. */
803 if (Present (Renamed_Object (gnat_entity)))
805 bool create_normal_object = false;
807 /* If the renamed object had padding, strip off the reference
808 to the inner object and reset our type. */
809 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
810 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
811 == RECORD_TYPE
812 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
813 /* Strip useless conversions around the object. */
814 || TREE_CODE (gnu_expr) == NOP_EXPR)
816 gnu_expr = TREE_OPERAND (gnu_expr, 0);
817 gnu_type = TREE_TYPE (gnu_expr);
820 /* Case 1: If this is a constant renaming stemming from a function
821 call, treat it as a normal object whose initial value is what
822 is being renamed. RM 3.3 says that the result of evaluating a
823 function call is a constant object. As a consequence, it can
824 be the inner object of a constant renaming. In this case, the
825 renaming must be fully instantiated, i.e. it cannot be a mere
826 reference to (part of) an existing object. */
827 if (const_flag)
829 tree inner_object = gnu_expr;
830 while (handled_component_p (inner_object))
831 inner_object = TREE_OPERAND (inner_object, 0);
832 if (TREE_CODE (inner_object) == CALL_EXPR)
833 create_normal_object = true;
836 /* Otherwise, see if we can proceed with a stabilized version of
837 the renamed entity or if we need to make a new object. */
838 if (!create_normal_object)
840 tree maybe_stable_expr = NULL_TREE;
841 bool stable = false;
843 /* Case 2: If the renaming entity need not be materialized and
844 the renamed expression is something we can stabilize, use
845 that for the renaming. At the global level, we can only do
846 this if we know no SAVE_EXPRs need be made, because the
847 expression we return might be used in arbitrary conditional
848 branches so we must force the SAVE_EXPRs evaluation
849 immediately and this requires a function context. */
850 if (!Materialize_Entity (gnat_entity)
851 && (!global_bindings_p ()
852 || (staticp (gnu_expr)
853 && !TREE_SIDE_EFFECTS (gnu_expr))))
855 maybe_stable_expr
856 = maybe_stabilize_reference (gnu_expr, true, &stable);
858 if (stable)
860 gnu_decl = maybe_stable_expr;
861 /* ??? No DECL_EXPR is created so we need to mark
862 the expression manually lest it is shared. */
863 if (global_bindings_p ())
864 TREE_VISITED (gnu_decl) = 1;
865 save_gnu_tree (gnat_entity, gnu_decl, true);
866 saved = true;
867 break;
870 /* The stabilization failed. Keep maybe_stable_expr
871 untouched here to let the pointer case below know
872 about that failure. */
875 /* Case 3: If this is a constant renaming and creating a
876 new object is allowed and cheap, treat it as a normal
877 object whose initial value is what is being renamed. */
878 if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
881 /* Case 4: Make this into a constant pointer to the object we
882 are to rename and attach the object to the pointer if it is
883 something we can stabilize.
885 From the proper scope, attached objects will be referenced
886 directly instead of indirectly via the pointer to avoid
887 subtle aliasing problems with non-addressable entities.
888 They have to be stable because we must not evaluate the
889 variables in the expression every time the renaming is used.
890 The pointer is called a "renaming" pointer in this case.
892 In the rare cases where we cannot stabilize the renamed
893 object, we just make a "bare" pointer, and the renamed
894 entity is always accessed indirectly through it. */
895 else
897 gnu_type = build_reference_type (gnu_type);
898 inner_const_flag = TREE_READONLY (gnu_expr);
899 const_flag = true;
901 /* If the previous attempt at stabilizing failed, there
902 is no point in trying again and we reuse the result
903 without attaching it to the pointer. In this case it
904 will only be used as the initializing expression of
905 the pointer and thus needs no special treatment with
906 regard to multiple evaluations. */
907 if (maybe_stable_expr)
910 /* Otherwise, try to stabilize and attach the expression
911 to the pointer if the stabilization succeeds.
913 Note that this might introduce SAVE_EXPRs and we don't
914 check whether we're at the global level or not. This
915 is fine since we are building a pointer initializer and
916 neither the pointer nor the initializing expression can
917 be accessed before the pointer elaboration has taken
918 place in a correct program.
920 These SAVE_EXPRs will be evaluated at the right place
921 by either the evaluation of the initializer for the
922 non-global case or the elaboration code for the global
923 case, and will be attached to the elaboration procedure
924 in the latter case. */
925 else
927 maybe_stable_expr
928 = maybe_stabilize_reference (gnu_expr, true, &stable);
930 if (stable)
931 renamed_obj = maybe_stable_expr;
933 /* Attaching is actually performed downstream, as soon
934 as we have a VAR_DECL for the pointer we make. */
937 gnu_expr
938 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
940 gnu_size = NULL_TREE;
941 used_by_ref = true;
946 /* If this is an aliased object whose nominal subtype is unconstrained,
947 the object is a record that contains both the template and
948 the object. If there is an initializer, it will have already
949 been converted to the right type, but we need to create the
950 template if there is no initializer. */
951 else if (definition
952 && TREE_CODE (gnu_type) == RECORD_TYPE
953 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
954 /* Beware that padding might have been introduced
955 via maybe_pad_type above. */
956 || (TYPE_IS_PADDING_P (gnu_type)
957 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
958 == RECORD_TYPE
959 && TYPE_CONTAINS_TEMPLATE_P
960 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
961 && !gnu_expr)
963 tree template_field
964 = TYPE_IS_PADDING_P (gnu_type)
965 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
966 : TYPE_FIELDS (gnu_type);
968 gnu_expr
969 = gnat_build_constructor
970 (gnu_type,
971 tree_cons
972 (template_field,
973 build_template (TREE_TYPE (template_field),
974 TREE_TYPE (TREE_CHAIN (template_field)),
975 NULL_TREE),
976 NULL_TREE));
979 /* Convert the expression to the type of the object except in the
980 case where the object's type is unconstrained or the object's type
981 is a padded record whose field is of self-referential size. In
982 the former case, converting will generate unnecessary evaluations
983 of the CONSTRUCTOR to compute the size and in the latter case, we
984 want to only copy the actual data. */
985 if (gnu_expr
986 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
987 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
988 && !(TREE_CODE (gnu_type) == RECORD_TYPE
989 && TYPE_IS_PADDING_P (gnu_type)
990 && (CONTAINS_PLACEHOLDER_P
991 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
992 gnu_expr = convert (gnu_type, gnu_expr);
994 /* If this is a pointer and it does not have an initializing
995 expression, initialize it to NULL, unless the object is
996 imported. */
997 if (definition
998 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
999 && !Is_Imported (gnat_entity) && !gnu_expr)
1000 gnu_expr = integer_zero_node;
1002 /* If we are defining the object and it has an Address clause we must
1003 get the address expression from the saved GCC tree for the
1004 object if the object has a Freeze_Node. Otherwise, we elaborate
1005 the address expression here since the front-end has guaranteed
1006 in that case that the elaboration has no effects. Note that
1007 only the latter mechanism is currently in use. */
1008 if (definition && Present (Address_Clause (gnat_entity)))
1010 tree gnu_address
1011 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
1012 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
1014 save_gnu_tree (gnat_entity, NULL_TREE, false);
1016 /* Ignore the size. It's either meaningless or was handled
1017 above. */
1018 gnu_size = NULL_TREE;
1019 /* Convert the type of the object to a reference type that can
1020 alias everything as per 13.3(19). */
1021 gnu_type
1022 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1023 gnu_address = convert (gnu_type, gnu_address);
1024 used_by_ref = true;
1025 const_flag = !Is_Public (gnat_entity);
1027 /* If we don't have an initializing expression for the underlying
1028 variable, the initializing expression for the pointer is the
1029 specified address. Otherwise, we have to make a COMPOUND_EXPR
1030 to assign both the address and the initial value. */
1031 if (!gnu_expr)
1032 gnu_expr = gnu_address;
1033 else
1034 gnu_expr
1035 = build2 (COMPOUND_EXPR, gnu_type,
1036 build_binary_op
1037 (MODIFY_EXPR, NULL_TREE,
1038 build_unary_op (INDIRECT_REF, NULL_TREE,
1039 gnu_address),
1040 gnu_expr),
1041 gnu_address);
1044 /* If it has an address clause and we are not defining it, mark it
1045 as an indirect object. Likewise for Stdcall objects that are
1046 imported. */
1047 if ((!definition && Present (Address_Clause (gnat_entity)))
1048 || (Is_Imported (gnat_entity)
1049 && Has_Stdcall_Convention (gnat_entity)))
1051 /* Convert the type of the object to a reference type that can
1052 alias everything as per 13.3(19). */
1053 gnu_type
1054 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1055 gnu_size = NULL_TREE;
1057 gnu_expr = NULL_TREE;
1058 /* No point in taking the address of an initializing expression
1059 that isn't going to be used. */
1061 used_by_ref = true;
1064 /* If we are at top level and this object is of variable size,
1065 make the actual type a hidden pointer to the real type and
1066 make the initializer be a memory allocation and initialization.
1067 Likewise for objects we aren't defining (presumed to be
1068 external references from other packages), but there we do
1069 not set up an initialization.
1071 If the object's size overflows, make an allocator too, so that
1072 Storage_Error gets raised. Note that we will never free
1073 such memory, so we presume it never will get allocated. */
1075 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1076 global_bindings_p () || !definition
1077 || static_p)
1078 || (gnu_size
1079 && ! allocatable_size_p (gnu_size,
1080 global_bindings_p () || !definition
1081 || static_p)))
1083 gnu_type = build_reference_type (gnu_type);
1084 gnu_size = NULL_TREE;
1085 used_by_ref = true;
1086 const_flag = true;
1088 /* In case this was a aliased object whose nominal subtype is
1089 unconstrained, the pointer above will be a thin pointer and
1090 build_allocator will automatically make the template.
1092 If we have a template initializer only (that we made above),
1093 pretend there is none and rely on what build_allocator creates
1094 again anyway. Otherwise (if we have a full initializer), get
1095 the data part and feed that to build_allocator.
1097 If we are elaborating a mutable object, tell build_allocator to
1098 ignore a possibly simpler size from the initializer, if any, as
1099 we must allocate the maximum possible size in this case. */
1101 if (definition)
1103 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1105 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1106 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1108 gnu_alloc_type
1109 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1111 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1112 && 1 == VEC_length (constructor_elt,
1113 CONSTRUCTOR_ELTS (gnu_expr)))
1114 gnu_expr = 0;
1115 else
1116 gnu_expr
1117 = build_component_ref
1118 (gnu_expr, NULL_TREE,
1119 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1120 false);
1123 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1124 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1125 && !Is_Imported (gnat_entity))
1126 post_error ("?Storage_Error will be raised at run-time!",
1127 gnat_entity);
1129 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1130 0, 0, gnat_entity, mutable_p);
1132 else
1134 gnu_expr = NULL_TREE;
1135 const_flag = false;
1139 /* If this object would go into the stack and has an alignment larger
1140 than the largest stack alignment the back-end can honor, resort to
1141 a variable of "aligning type". */
1142 if (!global_bindings_p () && !static_p && definition
1143 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1145 /* Create the new variable. No need for extra room before the
1146 aligned field as this is in automatic storage. */
1147 tree gnu_new_type
1148 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1149 TYPE_SIZE_UNIT (gnu_type),
1150 BIGGEST_ALIGNMENT, 0);
1151 tree gnu_new_var
1152 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1153 NULL_TREE, gnu_new_type, NULL_TREE, false,
1154 false, false, false, NULL, gnat_entity);
1156 /* Initialize the aligned field if we have an initializer. */
1157 if (gnu_expr)
1158 add_stmt_with_node
1159 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1160 build_component_ref
1161 (gnu_new_var, NULL_TREE,
1162 TYPE_FIELDS (gnu_new_type), false),
1163 gnu_expr),
1164 gnat_entity);
1166 /* And setup this entity as a reference to the aligned field. */
1167 gnu_type = build_reference_type (gnu_type);
1168 gnu_expr
1169 = build_unary_op
1170 (ADDR_EXPR, gnu_type,
1171 build_component_ref (gnu_new_var, NULL_TREE,
1172 TYPE_FIELDS (gnu_new_type), false));
1174 gnu_size = NULL_TREE;
1175 used_by_ref = true;
1176 const_flag = true;
1179 if (const_flag)
1180 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1181 | TYPE_QUAL_CONST));
1183 /* Convert the expression to the type of the object except in the
1184 case where the object's type is unconstrained or the object's type
1185 is a padded record whose field is of self-referential size. In
1186 the former case, converting will generate unnecessary evaluations
1187 of the CONSTRUCTOR to compute the size and in the latter case, we
1188 want to only copy the actual data. */
1189 if (gnu_expr
1190 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1191 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1192 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1193 && TYPE_IS_PADDING_P (gnu_type)
1194 && (CONTAINS_PLACEHOLDER_P
1195 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1196 gnu_expr = convert (gnu_type, gnu_expr);
1198 /* If this name is external or there was a name specified, use it,
1199 unless this is a VMS exception object since this would conflict
1200 with the symbol we need to export in addition. Don't use the
1201 Interface_Name if there is an address clause (see CD30005). */
1202 if (!Is_VMS_Exception (gnat_entity)
1203 && ((Present (Interface_Name (gnat_entity))
1204 && No (Address_Clause (gnat_entity)))
1205 || (Is_Public (gnat_entity)
1206 && (!Is_Imported (gnat_entity)
1207 || Is_Exported (gnat_entity)))))
1208 gnu_ext_name = create_concat_name (gnat_entity, 0);
1210 /* If this is constant initialized to a static constant and the
1211 object has an aggregate type, force it to be statically
1212 allocated. */
1213 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1214 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1215 && (AGGREGATE_TYPE_P (gnu_type)
1216 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1217 && TYPE_IS_PADDING_P (gnu_type))))
1218 static_p = true;
1220 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1221 gnu_expr, const_flag,
1222 Is_Public (gnat_entity),
1223 imported_p || !definition,
1224 static_p, attr_list, gnat_entity);
1225 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1226 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1227 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1229 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1230 if (global_bindings_p ())
1232 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1233 record_global_renaming_pointer (gnu_decl);
1237 if (definition && DECL_SIZE (gnu_decl)
1238 && get_block_jmpbuf_decl ()
1239 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1240 || (flag_stack_check && !STACK_CHECK_BUILTIN
1241 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1242 STACK_CHECK_MAX_VAR_SIZE))))
1243 add_stmt_with_node (build_call_1_expr
1244 (update_setjmp_buf_decl,
1245 build_unary_op (ADDR_EXPR, NULL_TREE,
1246 get_block_jmpbuf_decl ())),
1247 gnat_entity);
1249 /* If this is a public constant or we're not optimizing and we're not
1250 making a VAR_DECL for it, make one just for export or debugger use.
1251 Likewise if the address is taken or if either the object or type is
1252 aliased. Make an external declaration for a reference, unless this
1253 is a Standard entity since there no real symbol at the object level
1254 for these. */
1255 if (TREE_CODE (gnu_decl) == CONST_DECL
1256 && (definition || Sloc (gnat_entity) > Standard_Location)
1257 && (Is_Public (gnat_entity)
1258 || optimize == 0
1259 || Address_Taken (gnat_entity)
1260 || Is_Aliased (gnat_entity)
1261 || Is_Aliased (Etype (gnat_entity))))
1263 tree gnu_corr_var
1264 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1265 gnu_expr, true, Is_Public (gnat_entity),
1266 !definition, static_p, NULL,
1267 gnat_entity);
1269 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1272 /* If this is declared in a block that contains a block with an
1273 exception handler, we must force this variable in memory to
1274 suppress an invalid optimization. */
1275 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1276 && Exception_Mechanism != Back_End_Exceptions)
1277 TREE_ADDRESSABLE (gnu_decl) = 1;
1279 gnu_type = TREE_TYPE (gnu_decl);
1281 /* Back-annotate Alignment and Esize of the object if not already
1282 known, except for when the object is actually a pointer to the
1283 real object, since alignment and size of a pointer don't have
1284 anything to do with those of the designated object. Note that
1285 we pick the values of the type, not those of the object, to
1286 shield ourselves from low-level platform-dependent adjustments
1287 like alignment promotion. This is both consistent with all the
1288 treatment above, where alignment and size are set on the type of
1289 the object and not on the object directly, and makes it possible
1290 to support confirming representation clauses in all cases. */
1292 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1293 Set_Alignment (gnat_entity,
1294 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1296 if (!used_by_ref && Unknown_Esize (gnat_entity))
1298 if (TREE_CODE (gnu_type) == RECORD_TYPE
1299 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1300 gnu_object_size
1301 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1303 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1306 break;
1308 case E_Void:
1309 /* Return a TYPE_DECL for "void" that we previously made. */
1310 gnu_decl = void_type_decl_node;
1311 break;
1313 case E_Enumeration_Type:
1314 /* A special case, for the types Character and Wide_Character in
1315 Standard, we do not list all the literals. So if the literals
1316 are not specified, make this an unsigned type. */
1317 if (No (First_Literal (gnat_entity)))
1319 gnu_type = make_unsigned_type (esize);
1320 TYPE_NAME (gnu_type) = gnu_entity_id;
1322 /* Set the TYPE_STRING_FLAG for Ada Character and
1323 Wide_Character types. This is needed by the dwarf-2 debug writer to
1324 distinguish between unsigned integer types and character types. */
1325 TYPE_STRING_FLAG (gnu_type) = 1;
1326 break;
1329 /* Normal case of non-character type, or non-Standard character type */
1331 /* Here we have a list of enumeral constants in First_Literal.
1332 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1333 the list to be places into TYPE_FIELDS. Each node in the list
1334 is a TREE_LIST node whose TREE_VALUE is the literal name
1335 and whose TREE_PURPOSE is the value of the literal.
1337 Esize contains the number of bits needed to represent the enumeral
1338 type, Type_Low_Bound also points to the first literal and
1339 Type_High_Bound points to the last literal. */
1341 Entity_Id gnat_literal;
1342 tree gnu_literal_list = NULL_TREE;
1344 if (Is_Unsigned_Type (gnat_entity))
1345 gnu_type = make_unsigned_type (esize);
1346 else
1347 gnu_type = make_signed_type (esize);
1349 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1351 for (gnat_literal = First_Literal (gnat_entity);
1352 Present (gnat_literal);
1353 gnat_literal = Next_Literal (gnat_literal))
1355 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1356 gnu_type);
1357 tree gnu_literal
1358 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1359 gnu_type, gnu_value, true, false, false,
1360 false, NULL, gnat_literal);
1362 save_gnu_tree (gnat_literal, gnu_literal, false);
1363 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1364 gnu_value, gnu_literal_list);
1367 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1369 /* Note that the bounds are updated at the end of this function
1370 because to avoid an infinite recursion when we get the bounds of
1371 this type, since those bounds are objects of this type. */
1373 break;
1375 case E_Signed_Integer_Type:
1376 case E_Ordinary_Fixed_Point_Type:
1377 case E_Decimal_Fixed_Point_Type:
1378 /* For integer types, just make a signed type the appropriate number
1379 of bits. */
1380 gnu_type = make_signed_type (esize);
1381 break;
1383 case E_Modular_Integer_Type:
1384 /* For modular types, make the unsigned type of the proper number of
1385 bits and then set up the modulus, if required. */
1387 enum machine_mode mode;
1388 tree gnu_modulus;
1389 tree gnu_high = 0;
1391 if (Is_Packed_Array_Type (gnat_entity))
1392 esize = UI_To_Int (RM_Size (gnat_entity));
1394 /* Find the smallest mode at least ESIZE bits wide and make a class
1395 using that mode. */
1397 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1398 GET_MODE_BITSIZE (mode) < esize;
1399 mode = GET_MODE_WIDER_MODE (mode))
1402 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1403 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1404 = (Is_Packed_Array_Type (gnat_entity)
1405 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1407 /* Get the modulus in this type. If it overflows, assume it is because
1408 it is equal to 2**Esize. Note that there is no overflow checking
1409 done on unsigned type, so we detect the overflow by looking for
1410 a modulus of zero, which is otherwise invalid. */
1411 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1413 if (!integer_zerop (gnu_modulus))
1415 TYPE_MODULAR_P (gnu_type) = 1;
1416 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1417 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1418 convert (gnu_type, integer_one_node));
1421 /* If we have to set TYPE_PRECISION different from its natural value,
1422 make a subtype to do do. Likewise if there is a modulus and
1423 it is not one greater than TYPE_MAX_VALUE. */
1424 if (TYPE_PRECISION (gnu_type) != esize
1425 || (TYPE_MODULAR_P (gnu_type)
1426 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1428 tree gnu_subtype = make_node (INTEGER_TYPE);
1430 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1431 TREE_TYPE (gnu_subtype) = gnu_type;
1432 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1433 TYPE_MAX_VALUE (gnu_subtype)
1434 = TYPE_MODULAR_P (gnu_type)
1435 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1436 TYPE_PRECISION (gnu_subtype) = esize;
1437 TYPE_UNSIGNED (gnu_subtype) = 1;
1438 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1439 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1440 = (Is_Packed_Array_Type (gnat_entity)
1441 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1442 layout_type (gnu_subtype);
1444 gnu_type = gnu_subtype;
1447 break;
1449 case E_Signed_Integer_Subtype:
1450 case E_Enumeration_Subtype:
1451 case E_Modular_Integer_Subtype:
1452 case E_Ordinary_Fixed_Point_Subtype:
1453 case E_Decimal_Fixed_Point_Subtype:
1455 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1456 that we do not want to call build_range_type since we would
1457 like each subtype node to be distinct. This will be important
1458 when memory aliasing is implemented.
1460 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1461 parent type; this fact is used by the arithmetic conversion
1462 functions.
1464 We elaborate the Ancestor_Subtype if it is not in the current
1465 unit and one of our bounds is non-static. We do this to ensure
1466 consistent naming in the case where several subtypes share the same
1467 bounds by always elaborating the first such subtype first, thus
1468 using its name. */
1470 if (!definition
1471 && Present (Ancestor_Subtype (gnat_entity))
1472 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1473 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1474 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1475 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1476 gnu_expr, 0);
1478 gnu_type = make_node (INTEGER_TYPE);
1479 if (Is_Packed_Array_Type (gnat_entity)
1480 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1482 esize = UI_To_Int (RM_Size (gnat_entity));
1483 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1486 TYPE_PRECISION (gnu_type) = esize;
1487 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1489 TYPE_MIN_VALUE (gnu_type)
1490 = convert (TREE_TYPE (gnu_type),
1491 elaborate_expression (Type_Low_Bound (gnat_entity),
1492 gnat_entity,
1493 get_identifier ("L"), definition, 1,
1494 Needs_Debug_Info (gnat_entity)));
1496 TYPE_MAX_VALUE (gnu_type)
1497 = convert (TREE_TYPE (gnu_type),
1498 elaborate_expression (Type_High_Bound (gnat_entity),
1499 gnat_entity,
1500 get_identifier ("U"), definition, 1,
1501 Needs_Debug_Info (gnat_entity)));
1503 /* One of the above calls might have caused us to be elaborated,
1504 so don't blow up if so. */
1505 if (present_gnu_tree (gnat_entity))
1507 maybe_present = true;
1508 break;
1511 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1512 = Has_Biased_Representation (gnat_entity);
1514 /* This should be an unsigned type if the lower bound is constant
1515 and non-negative or if the base type is unsigned; a signed type
1516 otherwise. */
1517 TYPE_UNSIGNED (gnu_type)
1518 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1519 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1520 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1521 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1522 || Is_Unsigned_Type (gnat_entity));
1524 layout_type (gnu_type);
1526 /* Inherit our alias set from what we're a subtype of. Subtypes
1527 are not different types and a pointer can designate any instance
1528 within a subtype hierarchy. */
1529 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1531 /* If the type we are dealing with is to represent a packed array,
1532 we need to have the bits left justified on big-endian targets
1533 and right justified on little-endian targets. We also need to
1534 ensure that when the value is read (e.g. for comparison of two
1535 such values), we only get the good bits, since the unused bits
1536 are uninitialized. Both goals are accomplished by wrapping the
1537 modular value in an enclosing struct. */
1538 if (Is_Packed_Array_Type (gnat_entity)
1539 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1541 tree gnu_field_type = gnu_type;
1542 tree gnu_field;
1544 TYPE_RM_SIZE_NUM (gnu_field_type)
1545 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1546 gnu_type = make_node (RECORD_TYPE);
1547 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1549 /* Propagate the alignment of the modular type to the record.
1550 This means that bitpacked arrays have "ceil" alignment for
1551 their size, which may seem counter-intuitive but makes it
1552 possible to easily overlay them on modular types. */
1553 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1554 TYPE_PACKED (gnu_type) = 1;
1556 /* Create a stripped-down declaration of the original type, mainly
1557 for debugging. */
1558 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1559 NULL, true, debug_info_p, gnat_entity);
1561 /* Don't notify the field as "addressable", since we won't be taking
1562 it's address and it would prevent create_field_decl from making a
1563 bitfield. */
1564 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1565 gnu_field_type, gnu_type, 1, 0, 0, 0);
1567 finish_record_type (gnu_type, gnu_field, 0, false);
1568 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1569 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1571 copy_alias_set (gnu_type, gnu_field_type);
1574 /* If the type we are dealing with has got a smaller alignment than the
1575 natural one, we need to wrap it up in a record type and under-align
1576 the latter. We reuse the padding machinery for this purpose. */
1577 else if (Known_Alignment (gnat_entity)
1578 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1579 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1580 && align < TYPE_ALIGN (gnu_type))
1582 tree gnu_field_type = gnu_type;
1583 tree gnu_field;
1585 gnu_type = make_node (RECORD_TYPE);
1586 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1588 TYPE_ALIGN (gnu_type) = align;
1589 TYPE_PACKED (gnu_type) = 1;
1591 /* Create a stripped-down declaration of the original type, mainly
1592 for debugging. */
1593 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1594 NULL, true, debug_info_p, gnat_entity);
1596 /* Don't notify the field as "addressable", since we won't be taking
1597 it's address and it would prevent create_field_decl from making a
1598 bitfield. */
1599 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1600 gnu_field_type, gnu_type, 1, 0, 0, 0);
1602 finish_record_type (gnu_type, gnu_field, 0, false);
1603 TYPE_IS_PADDING_P (gnu_type) = 1;
1604 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1606 copy_alias_set (gnu_type, gnu_field_type);
1609 /* Otherwise reset the alignment lest we computed it above. */
1610 else
1611 align = 0;
1613 break;
1615 case E_Floating_Point_Type:
1616 /* If this is a VAX floating-point type, use an integer of the proper
1617 size. All the operations will be handled with ASM statements. */
1618 if (Vax_Float (gnat_entity))
1620 gnu_type = make_signed_type (esize);
1621 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1622 SET_TYPE_DIGITS_VALUE (gnu_type,
1623 UI_To_gnu (Digits_Value (gnat_entity),
1624 sizetype));
1625 break;
1628 /* The type of the Low and High bounds can be our type if this is
1629 a type from Standard, so set them at the end of the function. */
1630 gnu_type = make_node (REAL_TYPE);
1631 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1632 layout_type (gnu_type);
1633 break;
1635 case E_Floating_Point_Subtype:
1636 if (Vax_Float (gnat_entity))
1638 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1639 break;
1643 if (!definition
1644 && Present (Ancestor_Subtype (gnat_entity))
1645 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1646 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1647 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1648 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1649 gnu_expr, 0);
1651 gnu_type = make_node (REAL_TYPE);
1652 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1653 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1655 TYPE_MIN_VALUE (gnu_type)
1656 = convert (TREE_TYPE (gnu_type),
1657 elaborate_expression (Type_Low_Bound (gnat_entity),
1658 gnat_entity, get_identifier ("L"),
1659 definition, 1,
1660 Needs_Debug_Info (gnat_entity)));
1662 TYPE_MAX_VALUE (gnu_type)
1663 = convert (TREE_TYPE (gnu_type),
1664 elaborate_expression (Type_High_Bound (gnat_entity),
1665 gnat_entity, get_identifier ("U"),
1666 definition, 1,
1667 Needs_Debug_Info (gnat_entity)));
1669 /* One of the above calls might have caused us to be elaborated,
1670 so don't blow up if so. */
1671 if (present_gnu_tree (gnat_entity))
1673 maybe_present = true;
1674 break;
1677 layout_type (gnu_type);
1679 /* Inherit our alias set from what we're a subtype of, as for
1680 integer subtypes. */
1681 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1683 break;
1685 /* Array and String Types and Subtypes
1687 Unconstrained array types are represented by E_Array_Type and
1688 constrained array types are represented by E_Array_Subtype. There
1689 are no actual objects of an unconstrained array type; all we have
1690 are pointers to that type.
1692 The following fields are defined on array types and subtypes:
1694 Component_Type Component type of the array.
1695 Number_Dimensions Number of dimensions (an int).
1696 First_Index Type of first index. */
1698 case E_String_Type:
1699 case E_Array_Type:
1701 tree gnu_template_fields = NULL_TREE;
1702 tree gnu_template_type = make_node (RECORD_TYPE);
1703 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1704 tree gnu_fat_type = make_node (RECORD_TYPE);
1705 int ndim = Number_Dimensions (gnat_entity);
1706 int firstdim
1707 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1708 int nextdim
1709 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1710 int index;
1711 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1712 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1713 tree gnu_comp_size = 0;
1714 tree gnu_max_size = size_one_node;
1715 tree gnu_max_size_unit;
1716 Entity_Id gnat_ind_subtype;
1717 Entity_Id gnat_ind_base_subtype;
1718 tree gnu_template_reference;
1719 tree tem;
1721 TYPE_NAME (gnu_template_type)
1722 = create_concat_name (gnat_entity, "XUB");
1724 /* Make a node for the array. If we are not defining the array
1725 suppress expanding incomplete types. */
1726 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1728 if (!definition)
1729 defer_incomplete_level++, this_deferred = true;
1731 /* Build the fat pointer type. Use a "void *" object instead of
1732 a pointer to the array type since we don't have the array type
1733 yet (it will reference the fat pointer via the bounds). */
1734 tem = chainon (chainon (NULL_TREE,
1735 create_field_decl (get_identifier ("P_ARRAY"),
1736 ptr_void_type_node,
1737 gnu_fat_type, 0, 0, 0, 0)),
1738 create_field_decl (get_identifier ("P_BOUNDS"),
1739 gnu_ptr_template,
1740 gnu_fat_type, 0, 0, 0, 0));
1742 /* Make sure we can put this into a register. */
1743 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1745 /* Do not finalize this record type since the types of its fields
1746 are still incomplete at this point. */
1747 finish_record_type (gnu_fat_type, tem, 0, true);
1748 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1750 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1751 is the fat pointer. This will be used to access the individual
1752 fields once we build them. */
1753 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1754 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1755 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1756 gnu_template_reference
1757 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1758 TREE_READONLY (gnu_template_reference) = 1;
1760 /* Now create the GCC type for each index and add the fields for
1761 that index to the template. */
1762 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1763 gnat_ind_base_subtype
1764 = First_Index (Implementation_Base_Type (gnat_entity));
1765 index < ndim && index >= 0;
1766 index += nextdim,
1767 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1768 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1770 char field_name[10];
1771 tree gnu_ind_subtype
1772 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1773 tree gnu_base_subtype
1774 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1775 tree gnu_base_min
1776 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1777 tree gnu_base_max
1778 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1779 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1781 /* Make the FIELD_DECLs for the minimum and maximum of this
1782 type and then make extractions of that field from the
1783 template. */
1784 sprintf (field_name, "LB%d", index);
1785 gnu_min_field = create_field_decl (get_identifier (field_name),
1786 gnu_ind_subtype,
1787 gnu_template_type, 0, 0, 0, 0);
1788 field_name[0] = 'U';
1789 gnu_max_field = create_field_decl (get_identifier (field_name),
1790 gnu_ind_subtype,
1791 gnu_template_type, 0, 0, 0, 0);
1793 Sloc_to_locus (Sloc (gnat_entity),
1794 &DECL_SOURCE_LOCATION (gnu_min_field));
1795 Sloc_to_locus (Sloc (gnat_entity),
1796 &DECL_SOURCE_LOCATION (gnu_max_field));
1797 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1799 /* We can't use build_component_ref here since the template
1800 type isn't complete yet. */
1801 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1802 gnu_template_reference, gnu_min_field,
1803 NULL_TREE);
1804 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1805 gnu_template_reference, gnu_max_field,
1806 NULL_TREE);
1807 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1809 /* Make a range type with the new ranges, but using
1810 the Ada subtype. Then we convert to sizetype. */
1811 gnu_index_types[index]
1812 = create_index_type (convert (sizetype, gnu_min),
1813 convert (sizetype, gnu_max),
1814 build_range_type (gnu_ind_subtype,
1815 gnu_min, gnu_max),
1816 gnat_entity);
1817 /* Update the maximum size of the array, in elements. */
1818 gnu_max_size
1819 = size_binop (MULT_EXPR, gnu_max_size,
1820 size_binop (PLUS_EXPR, size_one_node,
1821 size_binop (MINUS_EXPR, gnu_base_max,
1822 gnu_base_min)));
1824 TYPE_NAME (gnu_index_types[index])
1825 = create_concat_name (gnat_entity, field_name);
1828 for (index = 0; index < ndim; index++)
1829 gnu_template_fields
1830 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1832 /* Install all the fields into the template. */
1833 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1834 TYPE_READONLY (gnu_template_type) = 1;
1836 /* Now make the array of arrays and update the pointer to the array
1837 in the fat pointer. Note that it is the first field. */
1838 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1840 /* Try to get a smaller form of the component if needed. */
1841 if ((Is_Packed (gnat_entity)
1842 || Has_Component_Size_Clause (gnat_entity))
1843 && !Is_Bit_Packed_Array (gnat_entity)
1844 && !Has_Aliased_Components (gnat_entity)
1845 && !Strict_Alignment (Component_Type (gnat_entity))
1846 && TREE_CODE (tem) == RECORD_TYPE
1847 && host_integerp (TYPE_SIZE (tem), 1))
1848 tem = make_packable_type (tem, false);
1850 if (Has_Atomic_Components (gnat_entity))
1851 check_ok_for_atomic (tem, gnat_entity, true);
1853 /* Get and validate any specified Component_Size, but if Packed,
1854 ignore it since the front end will have taken care of it. */
1855 gnu_comp_size
1856 = validate_size (Component_Size (gnat_entity), tem,
1857 gnat_entity,
1858 (Is_Bit_Packed_Array (gnat_entity)
1859 ? TYPE_DECL : VAR_DECL),
1860 true, Has_Component_Size_Clause (gnat_entity));
1862 /* If the component type is a RECORD_TYPE that has a self-referential
1863 size, use the maxium size. */
1864 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1865 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1866 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1868 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1870 tree orig_tem;
1871 tem = make_type_from_size (tem, gnu_comp_size, false);
1872 orig_tem = tem;
1873 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1874 "C_PAD", false, definition, true);
1875 /* If a padding record was made, declare it now since it will
1876 never be declared otherwise. This is necessary to ensure
1877 that its subtrees are properly marked. */
1878 if (tem != orig_tem)
1879 create_type_decl (TYPE_NAME (tem), tem, NULL, true, false,
1880 gnat_entity);
1883 if (Has_Volatile_Components (gnat_entity))
1884 tem = build_qualified_type (tem,
1885 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1887 /* If Component_Size is not already specified, annotate it with the
1888 size of the component. */
1889 if (Unknown_Component_Size (gnat_entity))
1890 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1892 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1893 size_binop (MULT_EXPR, gnu_max_size,
1894 TYPE_SIZE_UNIT (tem)));
1895 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1896 size_binop (MULT_EXPR,
1897 convert (bitsizetype,
1898 gnu_max_size),
1899 TYPE_SIZE (tem)));
1901 for (index = ndim - 1; index >= 0; index--)
1903 tem = build_array_type (tem, gnu_index_types[index]);
1904 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1905 if (array_type_has_nonaliased_component (gnat_entity, tem))
1906 TYPE_NONALIASED_COMPONENT (tem) = 1;
1909 /* If an alignment is specified, use it if valid. But ignore it for
1910 types that represent the unpacked base type for packed arrays. If
1911 the alignment was requested with an explicit user alignment clause,
1912 state so. */
1913 if (No (Packed_Array_Type (gnat_entity))
1914 && Known_Alignment (gnat_entity))
1916 gcc_assert (Present (Alignment (gnat_entity)));
1917 TYPE_ALIGN (tem)
1918 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1919 TYPE_ALIGN (tem));
1920 if (Present (Alignment_Clause (gnat_entity)))
1921 TYPE_USER_ALIGN (tem) = 1;
1924 TYPE_CONVENTION_FORTRAN_P (tem)
1925 = (Convention (gnat_entity) == Convention_Fortran);
1926 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1928 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1929 corresponding fat pointer. */
1930 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1931 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1932 TYPE_MODE (gnu_type) = BLKmode;
1933 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1934 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1936 /* If the maximum size doesn't overflow, use it. */
1937 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1938 && !TREE_OVERFLOW (gnu_max_size))
1939 TYPE_SIZE (tem)
1940 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1941 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1942 && !TREE_OVERFLOW (gnu_max_size_unit))
1943 TYPE_SIZE_UNIT (tem)
1944 = size_binop (MIN_EXPR, gnu_max_size_unit,
1945 TYPE_SIZE_UNIT (tem));
1947 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1948 tem, NULL, !Comes_From_Source (gnat_entity),
1949 debug_info_p, gnat_entity);
1951 /* Give the fat pointer type a name. */
1952 create_type_decl (create_concat_name (gnat_entity, "XUP"),
1953 gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
1954 debug_info_p, gnat_entity);
1956 /* Create the type to be used as what a thin pointer designates: an
1957 record type for the object and its template with the field offsets
1958 shifted to have the template at a negative offset. */
1959 tem = build_unc_object_type (gnu_template_type, tem,
1960 create_concat_name (gnat_entity, "XUT"));
1961 shift_unc_components_for_thin_pointers (tem);
1963 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1964 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1966 /* Give the thin pointer type a name. */
1967 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1968 build_pointer_type (tem), NULL,
1969 !Comes_From_Source (gnat_entity), debug_info_p,
1970 gnat_entity);
1972 break;
1974 case E_String_Subtype:
1975 case E_Array_Subtype:
1977 /* This is the actual data type for array variables. Multidimensional
1978 arrays are implemented in the gnu tree as arrays of arrays. Note
1979 that for the moment arrays which have sparse enumeration subtypes as
1980 index components create sparse arrays, which is obviously space
1981 inefficient but so much easier to code for now.
1983 Also note that the subtype never refers to the unconstrained
1984 array type, which is somewhat at variance with Ada semantics.
1986 First check to see if this is simply a renaming of the array
1987 type. If so, the result is the array type. */
1989 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1990 if (!Is_Constrained (gnat_entity))
1991 break;
1992 else
1994 int index;
1995 int array_dim = Number_Dimensions (gnat_entity);
1996 int first_dim
1997 = ((Convention (gnat_entity) == Convention_Fortran)
1998 ? array_dim - 1 : 0);
1999 int next_dim
2000 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2001 Entity_Id gnat_ind_subtype;
2002 Entity_Id gnat_ind_base_subtype;
2003 tree gnu_base_type = gnu_type;
2004 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
2005 tree gnu_comp_size = NULL_TREE;
2006 tree gnu_max_size = size_one_node;
2007 tree gnu_max_size_unit;
2008 bool need_index_type_struct = false;
2009 bool max_overflow = false;
2011 /* First create the gnu types for each index. Create types for
2012 debugging information to point to the index types if the
2013 are not integer types, have variable bounds, or are
2014 wider than sizetype. */
2016 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2017 gnat_ind_base_subtype
2018 = First_Index (Implementation_Base_Type (gnat_entity));
2019 index < array_dim && index >= 0;
2020 index += next_dim,
2021 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2022 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2024 tree gnu_index_subtype
2025 = get_unpadded_type (Etype (gnat_ind_subtype));
2026 tree gnu_min
2027 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2028 tree gnu_max
2029 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2030 tree gnu_base_subtype
2031 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2032 tree gnu_base_min
2033 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2034 tree gnu_base_max
2035 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2036 tree gnu_base_type = get_base_type (gnu_base_subtype);
2037 tree gnu_base_base_min
2038 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2039 tree gnu_base_base_max
2040 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2041 tree gnu_high;
2042 tree gnu_this_max;
2044 /* If the minimum and maximum values both overflow in
2045 SIZETYPE, but the difference in the original type
2046 does not overflow in SIZETYPE, ignore the overflow
2047 indications. */
2048 if ((TYPE_PRECISION (gnu_index_subtype)
2049 > TYPE_PRECISION (sizetype)
2050 || TYPE_UNSIGNED (gnu_index_subtype)
2051 != TYPE_UNSIGNED (sizetype))
2052 && TREE_CODE (gnu_min) == INTEGER_CST
2053 && TREE_CODE (gnu_max) == INTEGER_CST
2054 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2055 && (!TREE_OVERFLOW
2056 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2057 TYPE_MAX_VALUE (gnu_index_subtype),
2058 TYPE_MIN_VALUE (gnu_index_subtype)))))
2060 TREE_OVERFLOW (gnu_min) = 0;
2061 TREE_OVERFLOW (gnu_max) = 0;
2064 /* Similarly, if the range is null, use bounds of 1..0 for
2065 the sizetype bounds. */
2066 else if ((TYPE_PRECISION (gnu_index_subtype)
2067 > TYPE_PRECISION (sizetype)
2068 || TYPE_UNSIGNED (gnu_index_subtype)
2069 != TYPE_UNSIGNED (sizetype))
2070 && TREE_CODE (gnu_min) == INTEGER_CST
2071 && TREE_CODE (gnu_max) == INTEGER_CST
2072 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2073 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2074 TYPE_MIN_VALUE (gnu_index_subtype)))
2075 gnu_min = size_one_node, gnu_max = size_zero_node;
2077 /* Now compute the size of this bound. We need to provide
2078 GCC with an upper bound to use but have to deal with the
2079 "superflat" case. There are three ways to do this. If we
2080 can prove that the array can never be superflat, we can
2081 just use the high bound of the index subtype. If we can
2082 prove that the low bound minus one can't overflow, we
2083 can do this as MAX (hb, lb - 1). Otherwise, we have to use
2084 the expression hb >= lb ? hb : lb - 1. */
2085 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2087 /* See if the base array type is already flat. If it is, we
2088 are probably compiling an ACVC test, but it will cause the
2089 code below to malfunction if we don't handle it specially. */
2090 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2091 && TREE_CODE (gnu_base_max) == INTEGER_CST
2092 && !TREE_OVERFLOW (gnu_base_min)
2093 && !TREE_OVERFLOW (gnu_base_max)
2094 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2095 gnu_high = size_zero_node, gnu_min = size_one_node;
2097 /* If gnu_high is now an integer which overflowed, the array
2098 cannot be superflat. */
2099 else if (TREE_CODE (gnu_high) == INTEGER_CST
2100 && TREE_OVERFLOW (gnu_high))
2101 gnu_high = gnu_max;
2102 else if (TYPE_UNSIGNED (gnu_base_subtype)
2103 || TREE_CODE (gnu_high) == INTEGER_CST)
2104 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2105 else
2106 gnu_high
2107 = build_cond_expr
2108 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2109 gnu_max, gnu_min),
2110 gnu_max, gnu_high);
2112 gnu_index_type[index]
2113 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2114 gnat_entity);
2116 /* Also compute the maximum size of the array. Here we
2117 see if any constraint on the index type of the base type
2118 can be used in the case of self-referential bound on
2119 the index type of the subtype. We look for a non-"infinite"
2120 and non-self-referential bound from any type involved and
2121 handle each bound separately. */
2123 if ((TREE_CODE (gnu_min) == INTEGER_CST
2124 && !TREE_OVERFLOW (gnu_min)
2125 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2126 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2127 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2128 && !TREE_OVERFLOW (gnu_base_min)))
2129 gnu_base_min = gnu_min;
2131 if ((TREE_CODE (gnu_max) == INTEGER_CST
2132 && !TREE_OVERFLOW (gnu_max)
2133 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2134 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2135 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2136 && !TREE_OVERFLOW (gnu_base_max)))
2137 gnu_base_max = gnu_max;
2139 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2140 && TREE_OVERFLOW (gnu_base_min))
2141 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2142 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2143 && TREE_OVERFLOW (gnu_base_max))
2144 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2145 max_overflow = true;
2147 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2148 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2150 gnu_this_max
2151 = size_binop (MAX_EXPR,
2152 size_binop (PLUS_EXPR, size_one_node,
2153 size_binop (MINUS_EXPR, gnu_base_max,
2154 gnu_base_min)),
2155 size_zero_node);
2157 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2158 && TREE_OVERFLOW (gnu_this_max))
2159 max_overflow = true;
2161 gnu_max_size
2162 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2164 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2165 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2166 != INTEGER_CST)
2167 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2168 || (TREE_TYPE (gnu_index_subtype)
2169 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2170 != INTEGER_TYPE))
2171 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2172 || (TYPE_PRECISION (gnu_index_subtype)
2173 > TYPE_PRECISION (sizetype)))
2174 need_index_type_struct = true;
2177 /* Then flatten: create the array of arrays. For an array type
2178 used to implement a packed array, get the component type from
2179 the original array type since the representation clauses that
2180 can affect it are on the latter. */
2181 if (Is_Packed_Array_Type (gnat_entity)
2182 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2184 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2185 for (index = array_dim - 1; index >= 0; index--)
2186 gnu_type = TREE_TYPE (gnu_type);
2188 /* One of the above calls might have caused us to be elaborated,
2189 so don't blow up if so. */
2190 if (present_gnu_tree (gnat_entity))
2192 maybe_present = true;
2193 break;
2196 else
2198 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2200 /* One of the above calls might have caused us to be elaborated,
2201 so don't blow up if so. */
2202 if (present_gnu_tree (gnat_entity))
2204 maybe_present = true;
2205 break;
2208 /* Try to get a smaller form of the component if needed. */
2209 if ((Is_Packed (gnat_entity)
2210 || Has_Component_Size_Clause (gnat_entity))
2211 && !Is_Bit_Packed_Array (gnat_entity)
2212 && !Has_Aliased_Components (gnat_entity)
2213 && !Strict_Alignment (Component_Type (gnat_entity))
2214 && TREE_CODE (gnu_type) == RECORD_TYPE
2215 && host_integerp (TYPE_SIZE (gnu_type), 1))
2216 gnu_type = make_packable_type (gnu_type, false);
2218 /* Get and validate any specified Component_Size, but if Packed,
2219 ignore it since the front end will have taken care of it. */
2220 gnu_comp_size
2221 = validate_size (Component_Size (gnat_entity), gnu_type,
2222 gnat_entity,
2223 (Is_Bit_Packed_Array (gnat_entity)
2224 ? TYPE_DECL : VAR_DECL), true,
2225 Has_Component_Size_Clause (gnat_entity));
2227 /* If the component type is a RECORD_TYPE that has a
2228 self-referential size, use the maxium size. */
2229 if (!gnu_comp_size
2230 && TREE_CODE (gnu_type) == RECORD_TYPE
2231 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2232 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2234 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2236 tree orig_gnu_type;
2237 gnu_type
2238 = make_type_from_size (gnu_type, gnu_comp_size, false);
2239 orig_gnu_type = gnu_type;
2240 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2241 gnat_entity, "C_PAD", false,
2242 definition, true);
2243 /* If a padding record was made, declare it now since it
2244 will never be declared otherwise. This is necessary
2245 to ensure that its subtrees are properly marked. */
2246 if (gnu_type != orig_gnu_type)
2247 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2248 true, false, gnat_entity);
2251 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2252 gnu_type = build_qualified_type (gnu_type,
2253 (TYPE_QUALS (gnu_type)
2254 | TYPE_QUAL_VOLATILE));
2257 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2258 TYPE_SIZE_UNIT (gnu_type));
2259 gnu_max_size = size_binop (MULT_EXPR,
2260 convert (bitsizetype, gnu_max_size),
2261 TYPE_SIZE (gnu_type));
2263 for (index = array_dim - 1; index >= 0; index --)
2265 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2266 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2267 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2268 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2271 /* If we are at file level and this is a multi-dimensional array, we
2272 need to make a variable corresponding to the stride of the
2273 inner dimensions. */
2274 if (global_bindings_p () && array_dim > 1)
2276 tree gnu_str_name = get_identifier ("ST");
2277 tree gnu_arr_type;
2279 for (gnu_arr_type = TREE_TYPE (gnu_type);
2280 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2281 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2282 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2284 tree eltype = TREE_TYPE (gnu_arr_type);
2286 TYPE_SIZE (gnu_arr_type)
2287 = elaborate_expression_1 (gnat_entity, gnat_entity,
2288 TYPE_SIZE (gnu_arr_type),
2289 gnu_str_name, definition, 0);
2291 /* ??? For now, store the size as a multiple of the
2292 alignment of the element type in bytes so that we
2293 can see the alignment from the tree. */
2294 TYPE_SIZE_UNIT (gnu_arr_type)
2295 = build_binary_op
2296 (MULT_EXPR, sizetype,
2297 elaborate_expression_1
2298 (gnat_entity, gnat_entity,
2299 build_binary_op (EXACT_DIV_EXPR, sizetype,
2300 TYPE_SIZE_UNIT (gnu_arr_type),
2301 size_int (TYPE_ALIGN (eltype)
2302 / BITS_PER_UNIT)),
2303 concat_id_with_name (gnu_str_name, "A_U"),
2304 definition, 0),
2305 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2307 /* ??? create_type_decl is not invoked on the inner types so
2308 the MULT_EXPR node built above will never be marked. */
2309 TREE_VISITED (TYPE_SIZE_UNIT (gnu_arr_type)) = 1;
2313 /* If we need to write out a record type giving the names of
2314 the bounds, do it now. */
2315 if (need_index_type_struct && debug_info_p)
2317 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2318 tree gnu_field_list = NULL_TREE;
2319 tree gnu_field;
2321 TYPE_NAME (gnu_bound_rec_type)
2322 = create_concat_name (gnat_entity, "XA");
2324 for (index = array_dim - 1; index >= 0; index--)
2326 tree gnu_type_name
2327 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2329 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2330 gnu_type_name = DECL_NAME (gnu_type_name);
2332 gnu_field = create_field_decl (gnu_type_name,
2333 integer_type_node,
2334 gnu_bound_rec_type,
2335 0, NULL_TREE, NULL_TREE, 0);
2336 TREE_CHAIN (gnu_field) = gnu_field_list;
2337 gnu_field_list = gnu_field;
2340 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2341 0, false);
2344 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2345 = (Convention (gnat_entity) == Convention_Fortran);
2346 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2347 = (Is_Packed_Array_Type (gnat_entity)
2348 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2350 /* If our size depends on a placeholder and the maximum size doesn't
2351 overflow, use it. */
2352 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2353 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2354 && TREE_OVERFLOW (gnu_max_size))
2355 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2356 && TREE_OVERFLOW (gnu_max_size_unit))
2357 && !max_overflow)
2359 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2360 TYPE_SIZE (gnu_type));
2361 TYPE_SIZE_UNIT (gnu_type)
2362 = size_binop (MIN_EXPR, gnu_max_size_unit,
2363 TYPE_SIZE_UNIT (gnu_type));
2366 /* Set our alias set to that of our base type. This gives all
2367 array subtypes the same alias set. */
2368 copy_alias_set (gnu_type, gnu_base_type);
2371 /* If this is a packed type, make this type the same as the packed
2372 array type, but do some adjusting in the type first. */
2374 if (Present (Packed_Array_Type (gnat_entity)))
2376 Entity_Id gnat_index;
2377 tree gnu_inner_type;
2379 /* First finish the type we had been making so that we output
2380 debugging information for it */
2381 gnu_type
2382 = build_qualified_type (gnu_type,
2383 (TYPE_QUALS (gnu_type)
2384 | (TYPE_QUAL_VOLATILE
2385 * Treat_As_Volatile (gnat_entity))));
2386 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2387 !Comes_From_Source (gnat_entity),
2388 debug_info_p, gnat_entity);
2389 if (!Comes_From_Source (gnat_entity))
2390 DECL_ARTIFICIAL (gnu_decl) = 1;
2392 /* Save it as our equivalent in case the call below elaborates
2393 this type again. */
2394 save_gnu_tree (gnat_entity, gnu_decl, false);
2396 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2397 NULL_TREE, 0);
2398 this_made_decl = true;
2399 gnu_type = TREE_TYPE (gnu_decl);
2400 save_gnu_tree (gnat_entity, NULL_TREE, false);
2402 gnu_inner_type = gnu_type;
2403 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2404 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2405 || TYPE_IS_PADDING_P (gnu_inner_type)))
2406 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2408 /* We need to point the type we just made to our index type so
2409 the actual bounds can be put into a template. */
2411 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2412 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2413 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2414 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2416 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2418 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2419 If it is, we need to make another type. */
2420 if (TYPE_MODULAR_P (gnu_inner_type))
2422 tree gnu_subtype;
2424 gnu_subtype = make_node (INTEGER_TYPE);
2426 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2427 TYPE_MIN_VALUE (gnu_subtype)
2428 = TYPE_MIN_VALUE (gnu_inner_type);
2429 TYPE_MAX_VALUE (gnu_subtype)
2430 = TYPE_MAX_VALUE (gnu_inner_type);
2431 TYPE_PRECISION (gnu_subtype)
2432 = TYPE_PRECISION (gnu_inner_type);
2433 TYPE_UNSIGNED (gnu_subtype)
2434 = TYPE_UNSIGNED (gnu_inner_type);
2435 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2436 layout_type (gnu_subtype);
2438 gnu_inner_type = gnu_subtype;
2441 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2444 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2446 for (gnat_index = First_Index (gnat_entity);
2447 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2448 SET_TYPE_ACTUAL_BOUNDS
2449 (gnu_inner_type,
2450 tree_cons (NULL_TREE,
2451 get_unpadded_type (Etype (gnat_index)),
2452 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2454 if (Convention (gnat_entity) != Convention_Fortran)
2455 SET_TYPE_ACTUAL_BOUNDS
2456 (gnu_inner_type,
2457 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2459 if (TREE_CODE (gnu_type) == RECORD_TYPE
2460 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2461 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2465 /* Abort if packed array with no packed array type field set. */
2466 else
2467 gcc_assert (!Is_Packed (gnat_entity));
2469 break;
2471 case E_String_Literal_Subtype:
2472 /* Create the type for a string literal. */
2474 Entity_Id gnat_full_type
2475 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2476 && Present (Full_View (Etype (gnat_entity)))
2477 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2478 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2479 tree gnu_string_array_type
2480 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2481 tree gnu_string_index_type
2482 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2483 (TYPE_DOMAIN (gnu_string_array_type))));
2484 tree gnu_lower_bound
2485 = convert (gnu_string_index_type,
2486 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2487 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2488 tree gnu_length = ssize_int (length - 1);
2489 tree gnu_upper_bound
2490 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2491 gnu_lower_bound,
2492 convert (gnu_string_index_type, gnu_length));
2493 tree gnu_range_type
2494 = build_range_type (gnu_string_index_type,
2495 gnu_lower_bound, gnu_upper_bound);
2496 tree gnu_index_type
2497 = create_index_type (convert (sizetype,
2498 TYPE_MIN_VALUE (gnu_range_type)),
2499 convert (sizetype,
2500 TYPE_MAX_VALUE (gnu_range_type)),
2501 gnu_range_type, gnat_entity);
2503 gnu_type
2504 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2505 gnu_index_type);
2506 copy_alias_set (gnu_type, gnu_string_type);
2508 break;
2510 /* Record Types and Subtypes
2512 The following fields are defined on record types:
2514 Has_Discriminants True if the record has discriminants
2515 First_Discriminant Points to head of list of discriminants
2516 First_Entity Points to head of list of fields
2517 Is_Tagged_Type True if the record is tagged
2519 Implementation of Ada records and discriminated records:
2521 A record type definition is transformed into the equivalent of a C
2522 struct definition. The fields that are the discriminants which are
2523 found in the Full_Type_Declaration node and the elements of the
2524 Component_List found in the Record_Type_Definition node. The
2525 Component_List can be a recursive structure since each Variant of
2526 the Variant_Part of the Component_List has a Component_List.
2528 Processing of a record type definition comprises starting the list of
2529 field declarations here from the discriminants and the calling the
2530 function components_to_record to add the rest of the fields from the
2531 component list and return the gnu type node. The function
2532 components_to_record will call itself recursively as it traverses
2533 the tree. */
2535 case E_Record_Type:
2536 if (Has_Complex_Representation (gnat_entity))
2538 gnu_type
2539 = build_complex_type
2540 (get_unpadded_type
2541 (Etype (Defining_Entity
2542 (First (Component_Items
2543 (Component_List
2544 (Type_Definition
2545 (Declaration_Node (gnat_entity)))))))));
2547 break;
2551 Node_Id full_definition = Declaration_Node (gnat_entity);
2552 Node_Id record_definition = Type_Definition (full_definition);
2553 Entity_Id gnat_field;
2554 tree gnu_field;
2555 tree gnu_field_list = NULL_TREE;
2556 tree gnu_get_parent;
2557 /* Set PACKED in keeping with gnat_to_gnu_field. */
2558 int packed
2559 = Is_Packed (gnat_entity)
2561 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2562 ? -1
2563 : (Known_Alignment (gnat_entity)
2564 || (Strict_Alignment (gnat_entity)
2565 && Known_Static_Esize (gnat_entity)))
2566 ? -2
2567 : 0;
2568 bool has_rep = Has_Specified_Layout (gnat_entity);
2569 bool all_rep = has_rep;
2570 bool is_extension
2571 = (Is_Tagged_Type (gnat_entity)
2572 && Nkind (record_definition) == N_Derived_Type_Definition);
2574 /* See if all fields have a rep clause. Stop when we find one
2575 that doesn't. */
2576 for (gnat_field = First_Entity (gnat_entity);
2577 Present (gnat_field) && all_rep;
2578 gnat_field = Next_Entity (gnat_field))
2579 if ((Ekind (gnat_field) == E_Component
2580 || Ekind (gnat_field) == E_Discriminant)
2581 && No (Component_Clause (gnat_field)))
2582 all_rep = false;
2584 /* If this is a record extension, go a level further to find the
2585 record definition. Also, verify we have a Parent_Subtype. */
2586 if (is_extension)
2588 if (!type_annotate_only
2589 || Present (Record_Extension_Part (record_definition)))
2590 record_definition = Record_Extension_Part (record_definition);
2592 gcc_assert (type_annotate_only
2593 || Present (Parent_Subtype (gnat_entity)));
2596 /* Make a node for the record. If we are not defining the record,
2597 suppress expanding incomplete types. */
2598 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2599 TYPE_NAME (gnu_type) = gnu_entity_id;
2600 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2602 if (!definition)
2603 defer_incomplete_level++, this_deferred = true;
2605 /* If both a size and rep clause was specified, put the size in
2606 the record type now so that it can get the proper mode. */
2607 if (has_rep && Known_Esize (gnat_entity))
2608 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2610 /* Always set the alignment here so that it can be used to
2611 set the mode, if it is making the alignment stricter. If
2612 it is invalid, it will be checked again below. If this is to
2613 be Atomic, choose a default alignment of a word unless we know
2614 the size and it's smaller. */
2615 if (Known_Alignment (gnat_entity))
2616 TYPE_ALIGN (gnu_type)
2617 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2618 else if (Is_Atomic (gnat_entity))
2619 TYPE_ALIGN (gnu_type)
2620 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2621 /* If a type needs strict alignment, the minimum size will be the
2622 type size instead of the RM size (see validate_size). Cap the
2623 alignment, lest it causes this type size to become too large. */
2624 else if (Strict_Alignment (gnat_entity)
2625 && Known_Static_Esize (gnat_entity))
2627 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2628 unsigned int raw_align = raw_size & -raw_size;
2629 if (raw_align < BIGGEST_ALIGNMENT)
2630 TYPE_ALIGN (gnu_type) = raw_align;
2632 else
2633 TYPE_ALIGN (gnu_type) = 0;
2635 /* If we have a Parent_Subtype, make a field for the parent. If
2636 this record has rep clauses, force the position to zero. */
2637 if (Present (Parent_Subtype (gnat_entity)))
2639 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2640 tree gnu_parent;
2642 /* A major complexity here is that the parent subtype will
2643 reference our discriminants in its Discriminant_Constraint
2644 list. But those must reference the parent component of this
2645 record which is of the parent subtype we have not built yet!
2646 To break the circle we first build a dummy COMPONENT_REF which
2647 represents the "get to the parent" operation and initialize
2648 each of those discriminants to a COMPONENT_REF of the above
2649 dummy parent referencing the corresponding discriminant of the
2650 base type of the parent subtype. */
2651 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2652 build0 (PLACEHOLDER_EXPR, gnu_type),
2653 build_decl (FIELD_DECL, NULL_TREE,
2654 void_type_node),
2655 NULL_TREE);
2657 if (Has_Discriminants (gnat_entity))
2658 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2659 Present (gnat_field);
2660 gnat_field = Next_Stored_Discriminant (gnat_field))
2661 if (Present (Corresponding_Discriminant (gnat_field)))
2662 save_gnu_tree
2663 (gnat_field,
2664 build3 (COMPONENT_REF,
2665 get_unpadded_type (Etype (gnat_field)),
2666 gnu_get_parent,
2667 gnat_to_gnu_field_decl (Corresponding_Discriminant
2668 (gnat_field)),
2669 NULL_TREE),
2670 true);
2672 /* Then we build the parent subtype. */
2673 gnu_parent = gnat_to_gnu_type (gnat_parent);
2675 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2676 initially built. The discriminants must reference the fields
2677 of the parent subtype and not those of its base type for the
2678 placeholder machinery to properly work. */
2679 if (Has_Discriminants (gnat_entity))
2680 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2681 Present (gnat_field);
2682 gnat_field = Next_Stored_Discriminant (gnat_field))
2683 if (Present (Corresponding_Discriminant (gnat_field)))
2685 Entity_Id field = Empty;
2686 for (field = First_Stored_Discriminant (gnat_parent);
2687 Present (field);
2688 field = Next_Stored_Discriminant (field))
2689 if (same_discriminant_p (gnat_field, field))
2690 break;
2691 gcc_assert (Present (field));
2692 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2693 = gnat_to_gnu_field_decl (field);
2696 /* The "get to the parent" COMPONENT_REF must be given its
2697 proper type... */
2698 TREE_TYPE (gnu_get_parent) = gnu_parent;
2700 /* ...and reference the _parent field of this record. */
2701 gnu_field_list
2702 = create_field_decl (get_identifier
2703 (Get_Name_String (Name_uParent)),
2704 gnu_parent, gnu_type, 0,
2705 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2706 has_rep ? bitsize_zero_node : 0, 1);
2707 DECL_INTERNAL_P (gnu_field_list) = 1;
2708 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2711 /* Make the fields for the discriminants and put them into the record
2712 unless it's an Unchecked_Union. */
2713 if (Has_Discriminants (gnat_entity))
2714 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2715 Present (gnat_field);
2716 gnat_field = Next_Stored_Discriminant (gnat_field))
2718 /* If this is a record extension and this discriminant
2719 is the renaming of another discriminant, we've already
2720 handled the discriminant above. */
2721 if (Present (Parent_Subtype (gnat_entity))
2722 && Present (Corresponding_Discriminant (gnat_field)))
2723 continue;
2725 gnu_field
2726 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2728 /* Make an expression using a PLACEHOLDER_EXPR from the
2729 FIELD_DECL node just created and link that with the
2730 corresponding GNAT defining identifier. Then add to the
2731 list of fields. */
2732 save_gnu_tree (gnat_field,
2733 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2734 build0 (PLACEHOLDER_EXPR,
2735 DECL_CONTEXT (gnu_field)),
2736 gnu_field, NULL_TREE),
2737 true);
2739 if (!Is_Unchecked_Union (gnat_entity))
2741 TREE_CHAIN (gnu_field) = gnu_field_list;
2742 gnu_field_list = gnu_field;
2746 /* Put the discriminants into the record (backwards), so we can
2747 know the appropriate discriminant to use for the names of the
2748 variants. */
2749 TYPE_FIELDS (gnu_type) = gnu_field_list;
2751 /* Add the listed fields into the record and finish it up. */
2752 components_to_record (gnu_type, Component_List (record_definition),
2753 gnu_field_list, packed, definition, NULL,
2754 false, all_rep, false,
2755 Is_Unchecked_Union (gnat_entity));
2757 /* We used to remove the associations of the discriminants and
2758 _Parent for validity checking, but we may need them if there's
2759 Freeze_Node for a subtype used in this record. */
2760 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2761 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2763 /* If it is a tagged record force the type to BLKmode to insure
2764 that these objects will always be placed in memory. Do the
2765 same thing for limited record types. */
2766 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2767 TYPE_MODE (gnu_type) = BLKmode;
2769 /* If this is a derived type, we must make the alias set of this type
2770 the same as that of the type we are derived from. We assume here
2771 that the other type is already frozen. */
2772 if (Etype (gnat_entity) != gnat_entity
2773 && !(Is_Private_Type (Etype (gnat_entity))
2774 && Full_View (Etype (gnat_entity)) == gnat_entity))
2775 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2777 /* Fill in locations of fields. */
2778 annotate_rep (gnat_entity, gnu_type);
2780 /* If there are any entities in the chain corresponding to
2781 components that we did not elaborate, ensure we elaborate their
2782 types if they are Itypes. */
2783 for (gnat_temp = First_Entity (gnat_entity);
2784 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2785 if ((Ekind (gnat_temp) == E_Component
2786 || Ekind (gnat_temp) == E_Discriminant)
2787 && Is_Itype (Etype (gnat_temp))
2788 && !present_gnu_tree (gnat_temp))
2789 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2791 break;
2793 case E_Class_Wide_Subtype:
2794 /* If an equivalent type is present, that is what we should use.
2795 Otherwise, fall through to handle this like a record subtype
2796 since it may have constraints. */
2797 if (gnat_equiv_type != gnat_entity)
2799 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2800 maybe_present = true;
2801 break;
2804 /* ... fall through ... */
2806 case E_Record_Subtype:
2808 /* If Cloned_Subtype is Present it means this record subtype has
2809 identical layout to that type or subtype and we should use
2810 that GCC type for this one. The front end guarantees that
2811 the component list is shared. */
2812 if (Present (Cloned_Subtype (gnat_entity)))
2814 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2815 NULL_TREE, 0);
2816 maybe_present = true;
2819 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2820 changing the type, make a new type with each field having the
2821 type of the field in the new subtype but having the position
2822 computed by transforming every discriminant reference according
2823 to the constraints. We don't see any difference between
2824 private and nonprivate type here since derivations from types should
2825 have been deferred until the completion of the private type. */
2826 else
2828 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2829 tree gnu_base_type;
2830 tree gnu_orig_type;
2832 if (!definition)
2833 defer_incomplete_level++, this_deferred = true;
2835 /* Get the base type initially for its alignment and sizes. But
2836 if it is a padded type, we do all the other work with the
2837 unpadded type. */
2838 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2840 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2841 && TYPE_IS_PADDING_P (gnu_base_type))
2842 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2843 else
2844 gnu_type = gnu_orig_type = gnu_base_type;
2846 if (present_gnu_tree (gnat_entity))
2848 maybe_present = true;
2849 break;
2852 /* When the type has discriminants, and these discriminants
2853 affect the shape of what it built, factor them in.
2855 If we are making a subtype of an Unchecked_Union (must be an
2856 Itype), just return the type.
2858 We can't just use Is_Constrained because private subtypes without
2859 discriminants of full types with discriminants with default
2860 expressions are Is_Constrained but aren't constrained! */
2862 if (IN (Ekind (gnat_base_type), Record_Kind)
2863 && !Is_For_Access_Subtype (gnat_entity)
2864 && !Is_Unchecked_Union (gnat_base_type)
2865 && Is_Constrained (gnat_entity)
2866 && Stored_Constraint (gnat_entity) != No_Elist
2867 && Present (Discriminant_Constraint (gnat_entity)))
2869 Entity_Id gnat_field;
2870 tree gnu_field_list = 0;
2871 tree gnu_pos_list
2872 = compute_field_positions (gnu_orig_type, NULL_TREE,
2873 size_zero_node, bitsize_zero_node,
2874 BIGGEST_ALIGNMENT);
2875 tree gnu_subst_list
2876 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2877 definition);
2878 tree gnu_temp;
2880 gnu_type = make_node (RECORD_TYPE);
2881 TYPE_NAME (gnu_type) = gnu_entity_id;
2882 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2883 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2885 for (gnat_field = First_Entity (gnat_entity);
2886 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2887 if ((Ekind (gnat_field) == E_Component
2888 || Ekind (gnat_field) == E_Discriminant)
2889 && (Underlying_Type (Scope (Original_Record_Component
2890 (gnat_field)))
2891 == gnat_base_type)
2892 && (No (Corresponding_Discriminant (gnat_field))
2893 || !Is_Tagged_Type (gnat_base_type)))
2895 tree gnu_old_field
2896 = gnat_to_gnu_field_decl (Original_Record_Component
2897 (gnat_field));
2898 tree gnu_offset
2899 = TREE_VALUE (purpose_member (gnu_old_field,
2900 gnu_pos_list));
2901 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2902 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2903 tree gnu_field_type
2904 = gnat_to_gnu_type (Etype (gnat_field));
2905 tree gnu_size = TYPE_SIZE (gnu_field_type);
2906 tree gnu_new_pos = 0;
2907 unsigned int offset_align
2908 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2910 tree gnu_field;
2912 /* If there was a component clause, the field types must be
2913 the same for the type and subtype, so copy the data from
2914 the old field to avoid recomputation here. Also if the
2915 field is justified modular and the optimization in
2916 gnat_to_gnu_field was applied. */
2917 if (Present (Component_Clause
2918 (Original_Record_Component (gnat_field)))
2919 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2920 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2921 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2922 == TREE_TYPE (gnu_old_field)))
2924 gnu_size = DECL_SIZE (gnu_old_field);
2925 gnu_field_type = TREE_TYPE (gnu_old_field);
2928 /* If the old field was packed and of constant size, we
2929 have to get the old size here, as it might differ from
2930 what the Etype conveys and the latter might overlap
2931 onto the following field. Try to arrange the type for
2932 possible better packing along the way. */
2933 else if (DECL_PACKED (gnu_old_field)
2934 && TREE_CODE (DECL_SIZE (gnu_old_field))
2935 == INTEGER_CST)
2937 gnu_size = DECL_SIZE (gnu_old_field);
2938 if (TYPE_MODE (gnu_field_type) == BLKmode
2939 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2940 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2941 gnu_field_type
2942 = make_packable_type (gnu_field_type, true);
2945 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2946 for (gnu_temp = gnu_subst_list;
2947 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2948 gnu_pos = substitute_in_expr (gnu_pos,
2949 TREE_PURPOSE (gnu_temp),
2950 TREE_VALUE (gnu_temp));
2952 /* If the size is now a constant, we can set it as the
2953 size of the field when we make it. Otherwise, we need
2954 to deal with it specially. */
2955 if (TREE_CONSTANT (gnu_pos))
2956 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2958 gnu_field
2959 = create_field_decl
2960 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2961 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
2962 !DECL_NONADDRESSABLE_P (gnu_old_field));
2964 if (!TREE_CONSTANT (gnu_pos))
2966 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2967 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2968 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2969 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2970 DECL_SIZE (gnu_field) = gnu_size;
2971 DECL_SIZE_UNIT (gnu_field)
2972 = convert (sizetype,
2973 size_binop (CEIL_DIV_EXPR, gnu_size,
2974 bitsize_unit_node));
2975 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2978 DECL_INTERNAL_P (gnu_field)
2979 = DECL_INTERNAL_P (gnu_old_field);
2980 SET_DECL_ORIGINAL_FIELD
2981 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2982 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2983 : gnu_old_field));
2984 DECL_DISCRIMINANT_NUMBER (gnu_field)
2985 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2986 TREE_THIS_VOLATILE (gnu_field)
2987 = TREE_THIS_VOLATILE (gnu_old_field);
2988 TREE_CHAIN (gnu_field) = gnu_field_list;
2989 gnu_field_list = gnu_field;
2990 save_gnu_tree (gnat_field, gnu_field, false);
2993 /* Now go through the entities again looking for Itypes that
2994 we have not elaborated but should (e.g., Etypes of fields
2995 that have Original_Components). */
2996 for (gnat_field = First_Entity (gnat_entity);
2997 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2998 if ((Ekind (gnat_field) == E_Discriminant
2999 || Ekind (gnat_field) == E_Component)
3000 && !present_gnu_tree (Etype (gnat_field)))
3001 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3003 /* Do not finalize it since we're going to modify it below. */
3004 finish_record_type (gnu_type, nreverse (gnu_field_list),
3005 2, true);
3007 /* Now set the size, alignment and alias set of the new type to
3008 match that of the old one, doing any substitutions, as
3009 above. */
3010 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3011 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3012 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3013 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3014 copy_alias_set (gnu_type, gnu_base_type);
3016 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3017 for (gnu_temp = gnu_subst_list;
3018 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3019 TYPE_SIZE (gnu_type)
3020 = substitute_in_expr (TYPE_SIZE (gnu_type),
3021 TREE_PURPOSE (gnu_temp),
3022 TREE_VALUE (gnu_temp));
3024 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3025 for (gnu_temp = gnu_subst_list;
3026 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3027 TYPE_SIZE_UNIT (gnu_type)
3028 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3029 TREE_PURPOSE (gnu_temp),
3030 TREE_VALUE (gnu_temp));
3032 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3033 for (gnu_temp = gnu_subst_list;
3034 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3035 SET_TYPE_ADA_SIZE
3036 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3037 TREE_PURPOSE (gnu_temp),
3038 TREE_VALUE (gnu_temp)));
3040 /* Reapply variable_size since we have changed the sizes. */
3041 TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3042 TYPE_SIZE_UNIT (gnu_type)
3043 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3045 /* Recompute the mode of this record type now that we know its
3046 actual size. */
3047 compute_record_mode (gnu_type);
3049 /* Fill in locations of fields. */
3050 annotate_rep (gnat_entity, gnu_type);
3052 /* We've built a new type, make an XVS type to show what this
3053 is a subtype of. Some debuggers require the XVS type to be
3054 output first, so do it in that order. */
3055 if (debug_info_p)
3057 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3058 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3060 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3061 gnu_orig_name = DECL_NAME (gnu_orig_name);
3063 TYPE_NAME (gnu_subtype_marker)
3064 = create_concat_name (gnat_entity, "XVS");
3065 finish_record_type (gnu_subtype_marker,
3066 create_field_decl (gnu_orig_name,
3067 integer_type_node,
3068 gnu_subtype_marker,
3069 0, NULL_TREE,
3070 NULL_TREE, 0),
3071 0, false);
3074 /* Now we can finalize it. */
3075 rest_of_record_type_compilation (gnu_type);
3078 /* Otherwise, go down all the components in the new type and
3079 make them equivalent to those in the base type. */
3080 else
3081 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3082 gnat_temp = Next_Entity (gnat_temp))
3083 if ((Ekind (gnat_temp) == E_Discriminant
3084 && !Is_Unchecked_Union (gnat_base_type))
3085 || Ekind (gnat_temp) == E_Component)
3086 save_gnu_tree (gnat_temp,
3087 gnat_to_gnu_field_decl
3088 (Original_Record_Component (gnat_temp)), false);
3090 break;
3092 case E_Access_Subprogram_Type:
3093 /* Use the special descriptor type for dispatch tables if needed,
3094 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3095 Note that we are only required to do so for static tables in
3096 order to be compatible with the C++ ABI, but Ada 2005 allows
3097 to extend library level tagged types at the local level so
3098 we do it in the non-static case as well. */
3099 if (TARGET_VTABLE_USES_DESCRIPTORS
3100 && Is_Dispatch_Table_Entity (gnat_entity))
3102 gnu_type = fdesc_type_node;
3103 gnu_size = TYPE_SIZE (gnu_type);
3104 break;
3107 /* ... fall through ... */
3109 case E_Anonymous_Access_Subprogram_Type:
3110 /* If we are not defining this entity, and we have incomplete
3111 entities being processed above us, make a dummy type and
3112 fill it in later. */
3113 if (!definition && defer_incomplete_level != 0)
3115 struct incomplete *p
3116 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3118 gnu_type
3119 = build_pointer_type
3120 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3121 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3122 !Comes_From_Source (gnat_entity),
3123 debug_info_p, gnat_entity);
3124 this_made_decl = true;
3125 gnu_type = TREE_TYPE (gnu_decl);
3126 save_gnu_tree (gnat_entity, gnu_decl, false);
3127 saved = true;
3129 p->old_type = TREE_TYPE (gnu_type);
3130 p->full_type = Directly_Designated_Type (gnat_entity);
3131 p->next = defer_incomplete_list;
3132 defer_incomplete_list = p;
3133 break;
3136 /* ... fall through ... */
3138 case E_Allocator_Type:
3139 case E_Access_Type:
3140 case E_Access_Attribute_Type:
3141 case E_Anonymous_Access_Type:
3142 case E_General_Access_Type:
3144 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3145 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3146 bool is_from_limited_with
3147 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3148 && From_With_Type (gnat_desig_equiv));
3150 /* Get the "full view" of this entity. If this is an incomplete
3151 entity from a limited with, treat its non-limited view as the full
3152 view. Otherwise, if this is an incomplete or private type, use the
3153 full view. In the former case, we might point to a private type,
3154 in which case, we need its full view. Also, we want to look at the
3155 actual type used for the representation, so this takes a total of
3156 three steps. */
3157 Entity_Id gnat_desig_full_direct_first
3158 = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3159 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3160 ? Full_View (gnat_desig_equiv) : Empty));
3161 Entity_Id gnat_desig_full_direct
3162 = ((is_from_limited_with
3163 && Present (gnat_desig_full_direct_first)
3164 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3165 ? Full_View (gnat_desig_full_direct_first)
3166 : gnat_desig_full_direct_first);
3167 Entity_Id gnat_desig_full
3168 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3170 /* This the type actually used to represent the designated type,
3171 either gnat_desig_full or gnat_desig_equiv. */
3172 Entity_Id gnat_desig_rep;
3174 /* Nonzero if this is a pointer to an unconstrained array. */
3175 bool is_unconstrained_array;
3177 /* We want to know if we'll be seeing the freeze node for any
3178 incomplete type we may be pointing to. */
3179 bool in_main_unit
3180 = (Present (gnat_desig_full)
3181 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3182 : In_Extended_Main_Code_Unit (gnat_desig_type));
3184 /* Nonzero if we make a dummy type here. */
3185 bool got_fat_p = false;
3186 /* Nonzero if the dummy is a fat pointer. */
3187 bool made_dummy = false;
3188 tree gnu_desig_type = NULL_TREE;
3189 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3191 if (!targetm.valid_pointer_mode (p_mode))
3192 p_mode = ptr_mode;
3194 /* If either the designated type or its full view is an unconstrained
3195 array subtype, replace it with the type it's a subtype of. This
3196 avoids problems with multiple copies of unconstrained array types.
3197 Likewise, if the designated type is a subtype of an incomplete
3198 record type, use the parent type to avoid order of elaboration
3199 issues. This can lose some code efficiency, but there is no
3200 alternative. */
3201 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3202 && ! Is_Constrained (gnat_desig_equiv))
3203 gnat_desig_equiv = Etype (gnat_desig_equiv);
3204 if (Present (gnat_desig_full)
3205 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3206 && ! Is_Constrained (gnat_desig_full))
3207 || (Ekind (gnat_desig_full) == E_Record_Subtype
3208 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3209 gnat_desig_full = Etype (gnat_desig_full);
3211 /* Now set the type that actually marks the representation of
3212 the designated type and also flag whether we have a unconstrained
3213 array. */
3214 gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3215 is_unconstrained_array
3216 = (Is_Array_Type (gnat_desig_rep)
3217 && ! Is_Constrained (gnat_desig_rep));
3219 /* If we are pointing to an incomplete type whose completion is an
3220 unconstrained array, make a fat pointer type. The two types in our
3221 fields will be pointers to dummy nodes and will be replaced in
3222 update_pointer_to. Similarly, if the type itself is a dummy type or
3223 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3224 in case we have any thin pointers to it. */
3225 if (is_unconstrained_array
3226 && (Present (gnat_desig_full)
3227 || (present_gnu_tree (gnat_desig_equiv)
3228 && TYPE_IS_DUMMY_P (TREE_TYPE
3229 (get_gnu_tree (gnat_desig_equiv))))
3230 || (No (gnat_desig_full) && ! in_main_unit
3231 && defer_incomplete_level != 0
3232 && ! present_gnu_tree (gnat_desig_equiv))
3233 || (in_main_unit && is_from_limited_with
3234 && Present (Freeze_Node (gnat_desig_rep)))))
3236 tree gnu_old
3237 = (present_gnu_tree (gnat_desig_rep)
3238 ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
3239 : make_dummy_type (gnat_desig_rep));
3240 tree fields;
3242 /* Show the dummy we get will be a fat pointer. */
3243 got_fat_p = made_dummy = true;
3245 /* If the call above got something that has a pointer, that
3246 pointer is our type. This could have happened either
3247 because the type was elaborated or because somebody
3248 else executed the code below. */
3249 gnu_type = TYPE_POINTER_TO (gnu_old);
3250 if (!gnu_type)
3252 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3253 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3254 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3255 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3257 TYPE_NAME (gnu_template_type)
3258 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3259 "XUB");
3260 TYPE_DUMMY_P (gnu_template_type) = 1;
3262 TYPE_NAME (gnu_array_type)
3263 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3264 "XUA");
3265 TYPE_DUMMY_P (gnu_array_type) = 1;
3267 gnu_type = make_node (RECORD_TYPE);
3268 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3269 TYPE_POINTER_TO (gnu_old) = gnu_type;
3271 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3272 fields
3273 = chainon (chainon (NULL_TREE,
3274 create_field_decl
3275 (get_identifier ("P_ARRAY"),
3276 gnu_ptr_array,
3277 gnu_type, 0, 0, 0, 0)),
3278 create_field_decl (get_identifier ("P_BOUNDS"),
3279 gnu_ptr_template,
3280 gnu_type, 0, 0, 0, 0));
3282 /* Make sure we can place this into a register. */
3283 TYPE_ALIGN (gnu_type)
3284 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3285 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3287 /* Do not finalize this record type since the types of
3288 its fields are incomplete. */
3289 finish_record_type (gnu_type, fields, 0, true);
3291 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3292 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3293 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3294 "XUT");
3295 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3299 /* If we already know what the full type is, use it. */
3300 else if (Present (gnat_desig_full)
3301 && present_gnu_tree (gnat_desig_full))
3302 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3304 /* Get the type of the thing we are to point to and build a pointer
3305 to it. If it is a reference to an incomplete or private type with a
3306 full view that is a record, make a dummy type node and get the
3307 actual type later when we have verified it is safe. */
3308 else if ((! in_main_unit
3309 && ! present_gnu_tree (gnat_desig_equiv)
3310 && Present (gnat_desig_full)
3311 && ! present_gnu_tree (gnat_desig_full)
3312 && Is_Record_Type (gnat_desig_full))
3313 /* Likewise if we are pointing to a record or array and we
3314 are to defer elaborating incomplete types. We do this
3315 since this access type may be the full view of some
3316 private type. Note that the unconstrained array case is
3317 handled above. */
3318 || ((! in_main_unit || imported_p)
3319 && defer_incomplete_level != 0
3320 && ! present_gnu_tree (gnat_desig_equiv)
3321 && ((Is_Record_Type (gnat_desig_rep)
3322 || Is_Array_Type (gnat_desig_rep))))
3323 /* If this is a reference from a limited_with type back to our
3324 main unit and there's a Freeze_Node for it, either we have
3325 already processed the declaration and made the dummy type,
3326 in which case we just reuse the latter, or we have not yet,
3327 in which case we make the dummy type and it will be reused
3328 when the declaration is processed. In both cases, the
3329 pointer eventually created below will be automatically
3330 adjusted when the Freeze_Node is processed. Note that the
3331 unconstrained array case is handled above. */
3332 || (in_main_unit && is_from_limited_with
3333 && Present (Freeze_Node (gnat_desig_rep))))
3335 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3336 made_dummy = true;
3339 /* Otherwise handle the case of a pointer to itself. */
3340 else if (gnat_desig_equiv == gnat_entity)
3342 gnu_type
3343 = build_pointer_type_for_mode (void_type_node, p_mode,
3344 No_Strict_Aliasing (gnat_entity));
3345 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3348 /* If expansion is disabled, the equivalent type of a concurrent
3349 type is absent, so build a dummy pointer type. */
3350 else if (type_annotate_only && No (gnat_desig_equiv))
3351 gnu_type = ptr_void_type_node;
3353 /* Finally, handle the straightforward case where we can just
3354 elaborate our designated type and point to it. */
3355 else
3356 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3358 /* It is possible that a call to gnat_to_gnu_type above resolved our
3359 type. If so, just return it. */
3360 if (present_gnu_tree (gnat_entity))
3362 maybe_present = true;
3363 break;
3366 /* If we have a GCC type for the designated type, possibly modify it
3367 if we are pointing only to constant objects and then make a pointer
3368 to it. Don't do this for unconstrained arrays. */
3369 if (!gnu_type && gnu_desig_type)
3371 if (Is_Access_Constant (gnat_entity)
3372 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3374 gnu_desig_type
3375 = build_qualified_type
3376 (gnu_desig_type,
3377 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3379 /* Some extra processing is required if we are building a
3380 pointer to an incomplete type (in the GCC sense). We might
3381 have such a type if we just made a dummy, or directly out
3382 of the call to gnat_to_gnu_type above if we are processing
3383 an access type for a record component designating the
3384 record type itself. */
3385 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3387 /* We must ensure that the pointer to variant we make will
3388 be processed by update_pointer_to when the initial type
3389 is completed. Pretend we made a dummy and let further
3390 processing act as usual. */
3391 made_dummy = true;
3393 /* We must ensure that update_pointer_to will not retrieve
3394 the dummy variant when building a properly qualified
3395 version of the complete type. We take advantage of the
3396 fact that get_qualified_type is requiring TYPE_NAMEs to
3397 match to influence build_qualified_type and then also
3398 update_pointer_to here. */
3399 TYPE_NAME (gnu_desig_type)
3400 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3404 gnu_type
3405 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3406 No_Strict_Aliasing (gnat_entity));
3409 /* If we are not defining this object and we made a dummy pointer,
3410 save our current definition, evaluate the actual type, and replace
3411 the tentative type we made with the actual one. If we are to defer
3412 actually looking up the actual type, make an entry in the
3413 deferred list. If this is from a limited with, we have to defer
3414 to the end of the current spec in two cases: first if the
3415 designated type is in the current unit and second if the access
3416 type is. */
3417 if ((! in_main_unit || is_from_limited_with) && made_dummy)
3419 tree gnu_old_type
3420 = TYPE_FAT_POINTER_P (gnu_type)
3421 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3423 if (esize == POINTER_SIZE
3424 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3425 gnu_type
3426 = build_pointer_type
3427 (TYPE_OBJECT_RECORD_TYPE
3428 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3430 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3431 !Comes_From_Source (gnat_entity),
3432 debug_info_p, gnat_entity);
3433 this_made_decl = true;
3434 gnu_type = TREE_TYPE (gnu_decl);
3435 save_gnu_tree (gnat_entity, gnu_decl, false);
3436 saved = true;
3438 if (defer_incomplete_level == 0
3439 && ! (is_from_limited_with
3440 && (in_main_unit
3441 || In_Extended_Main_Code_Unit (gnat_entity))))
3442 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3443 gnat_to_gnu_type (gnat_desig_equiv));
3445 /* Note that the call to gnat_to_gnu_type here might have
3446 updated gnu_old_type directly, in which case it is not a
3447 dummy type any more when we get into update_pointer_to.
3449 This may happen for instance when the designated type is a
3450 record type, because their elaboration starts with an
3451 initial node from make_dummy_type, which may yield the same
3452 node as the one we got.
3454 Besides, variants of this non-dummy type might have been
3455 created along the way. update_pointer_to is expected to
3456 properly take care of those situations. */
3457 else
3459 struct incomplete *p
3460 = (struct incomplete *) xmalloc (sizeof
3461 (struct incomplete));
3462 struct incomplete **head
3463 = (is_from_limited_with
3464 && (in_main_unit
3465 || In_Extended_Main_Code_Unit (gnat_entity))
3466 ? &defer_limited_with : &defer_incomplete_list);
3468 p->old_type = gnu_old_type;
3469 p->full_type = gnat_desig_equiv;
3470 p->next = *head;
3471 *head = p;
3475 break;
3477 case E_Access_Protected_Subprogram_Type:
3478 case E_Anonymous_Access_Protected_Subprogram_Type:
3479 if (type_annotate_only && No (gnat_equiv_type))
3480 gnu_type = ptr_void_type_node;
3481 else
3483 /* The runtime representation is the equivalent type. */
3484 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3485 maybe_present = 1;
3488 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3489 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3490 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3491 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3492 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3493 NULL_TREE, 0);
3495 break;
3497 case E_Access_Subtype:
3499 /* We treat this as identical to its base type; any constraint is
3500 meaningful only to the front end.
3502 The designated type must be elaborated as well, if it does
3503 not have its own freeze node. Designated (sub)types created
3504 for constrained components of records with discriminants are
3505 not frozen by the front end and thus not elaborated by gigi,
3506 because their use may appear before the base type is frozen,
3507 and because it is not clear that they are needed anywhere in
3508 Gigi. With the current model, there is no correct place where
3509 they could be elaborated. */
3511 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3512 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3513 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3514 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3515 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3517 /* If we are not defining this entity, and we have incomplete
3518 entities being processed above us, make a dummy type and
3519 elaborate it later. */
3520 if (!definition && defer_incomplete_level != 0)
3522 struct incomplete *p
3523 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3524 tree gnu_ptr_type
3525 = build_pointer_type
3526 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3528 p->old_type = TREE_TYPE (gnu_ptr_type);
3529 p->full_type = Directly_Designated_Type (gnat_entity);
3530 p->next = defer_incomplete_list;
3531 defer_incomplete_list = p;
3533 else if (!IN (Ekind (Base_Type
3534 (Directly_Designated_Type (gnat_entity))),
3535 Incomplete_Or_Private_Kind))
3536 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3537 NULL_TREE, 0);
3540 maybe_present = true;
3541 break;
3543 /* Subprogram Entities
3545 The following access functions are defined for subprograms (functions
3546 or procedures):
3548 First_Formal The first formal parameter.
3549 Is_Imported Indicates that the subprogram has appeared in
3550 an INTERFACE or IMPORT pragma. For now we
3551 assume that the external language is C.
3552 Is_Exported Likewise but for an EXPORT pragma.
3553 Is_Inlined True if the subprogram is to be inlined.
3555 In addition for function subprograms we have:
3557 Etype Return type of the function.
3559 Each parameter is first checked by calling must_pass_by_ref on its
3560 type to determine if it is passed by reference. For parameters which
3561 are copied in, if they are Ada In Out or Out parameters, their return
3562 value becomes part of a record which becomes the return type of the
3563 function (C function - note that this applies only to Ada procedures
3564 so there is no Ada return type). Additional code to store back the
3565 parameters will be generated on the caller side. This transformation
3566 is done here, not in the front-end.
3568 The intended result of the transformation can be seen from the
3569 equivalent source rewritings that follow:
3571 struct temp {int a,b};
3572 procedure P (A,B: In Out ...) is temp P (int A,B)
3573 begin {
3574 .. ..
3575 end P; return {A,B};
3578 temp t;
3579 P(X,Y); t = P(X,Y);
3580 X = t.a , Y = t.b;
3582 For subprogram types we need to perform mainly the same conversions to
3583 GCC form that are needed for procedures and function declarations. The
3584 only difference is that at the end, we make a type declaration instead
3585 of a function declaration. */
3587 case E_Subprogram_Type:
3588 case E_Function:
3589 case E_Procedure:
3591 /* The first GCC parameter declaration (a PARM_DECL node). The
3592 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3593 actually is the head of this parameter list. */
3594 tree gnu_param_list = NULL_TREE;
3595 /* Likewise for the stub associated with an exported procedure. */
3596 tree gnu_stub_param_list = NULL_TREE;
3597 /* The type returned by a function. If the subprogram is a procedure
3598 this type should be void_type_node. */
3599 tree gnu_return_type = void_type_node;
3600 /* List of fields in return type of procedure with copy-in copy-out
3601 parameters. */
3602 tree gnu_field_list = NULL_TREE;
3603 /* Non-null for subprograms containing parameters passed by copy-in
3604 copy-out (Ada In Out or Out parameters not passed by reference),
3605 in which case it is the list of nodes used to specify the values of
3606 the in out/out parameters that are returned as a record upon
3607 procedure return. The TREE_PURPOSE of an element of this list is
3608 a field of the record and the TREE_VALUE is the PARM_DECL
3609 corresponding to that field. This list will be saved in the
3610 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3611 tree gnu_return_list = NULL_TREE;
3612 /* If an import pragma asks to map this subprogram to a GCC builtin,
3613 this is the builtin DECL node. */
3614 tree gnu_builtin_decl = NULL_TREE;
3615 /* For the stub associated with an exported procedure. */
3616 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3617 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3618 Entity_Id gnat_param;
3619 bool inline_flag = Is_Inlined (gnat_entity);
3620 bool public_flag = Is_Public (gnat_entity) || imported_p;
3621 bool extern_flag
3622 = (Is_Public (gnat_entity) && !definition) || imported_p;
3623 bool pure_flag = Is_Pure (gnat_entity);
3624 bool volatile_flag = No_Return (gnat_entity);
3625 bool returns_by_ref = false;
3626 bool returns_unconstrained = false;
3627 bool returns_by_target_ptr = false;
3628 bool has_copy_in_out = false;
3629 bool has_stub = false;
3630 int parmnum;
3632 if (kind == E_Subprogram_Type && !definition)
3633 /* A parameter may refer to this type, so defer completion
3634 of any incomplete types. */
3635 defer_incomplete_level++, this_deferred = true;
3637 /* If the subprogram has an alias, it is probably inherited, so
3638 we can use the original one. If the original "subprogram"
3639 is actually an enumeration literal, it may be the first use
3640 of its type, so we must elaborate that type now. */
3641 if (Present (Alias (gnat_entity)))
3643 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3644 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3646 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3647 gnu_expr, 0);
3649 /* Elaborate any Itypes in the parameters of this entity. */
3650 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3651 Present (gnat_temp);
3652 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3653 if (Is_Itype (Etype (gnat_temp)))
3654 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3656 break;
3659 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3660 corresponding DECL node.
3662 We still want the parameter associations to take place because the
3663 proper generation of calls depends on it (a GNAT parameter without
3664 a corresponding GCC tree has a very specific meaning), so we don't
3665 just break here. */
3666 if (Convention (gnat_entity) == Convention_Intrinsic)
3667 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3669 /* ??? What if we don't find the builtin node above ? warn ? err ?
3670 In the current state we neither warn nor err, and calls will just
3671 be handled as for regular subprograms. */
3673 if (kind == E_Function || kind == E_Subprogram_Type)
3674 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3676 /* If this function returns by reference, make the actual
3677 return type of this function the pointer and mark the decl. */
3678 if (Returns_By_Ref (gnat_entity))
3680 returns_by_ref = true;
3681 gnu_return_type = build_pointer_type (gnu_return_type);
3684 /* If the Mechanism is By_Reference, ensure the return type uses
3685 the machine's by-reference mechanism, which may not the same
3686 as above (e.g., it might be by passing a fake parameter). */
3687 else if (kind == E_Function
3688 && Mechanism (gnat_entity) == By_Reference)
3690 TREE_ADDRESSABLE (gnu_return_type) = 1;
3692 /* We expect this bit to be reset by gigi shortly, so can avoid a
3693 type node copy here. This actually also prevents troubles with
3694 the generation of debug information for the function, because
3695 we might have issued such info for this type already, and would
3696 be attaching a distinct type node to the function if we made a
3697 copy here. */
3700 /* If we are supposed to return an unconstrained array,
3701 actually return a fat pointer and make a note of that. Return
3702 a pointer to an unconstrained record of variable size. */
3703 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3705 gnu_return_type = TREE_TYPE (gnu_return_type);
3706 returns_unconstrained = true;
3709 /* If the type requires a transient scope, the result is allocated
3710 on the secondary stack, so the result type of the function is
3711 just a pointer. */
3712 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3714 gnu_return_type = build_pointer_type (gnu_return_type);
3715 returns_unconstrained = true;
3718 /* If the type is a padded type and the underlying type would not
3719 be passed by reference or this function has a foreign convention,
3720 return the underlying type. */
3721 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3722 && TYPE_IS_PADDING_P (gnu_return_type)
3723 && (!default_pass_by_ref (TREE_TYPE
3724 (TYPE_FIELDS (gnu_return_type)))
3725 || Has_Foreign_Convention (gnat_entity)))
3726 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3728 /* If the return type has a non-constant size, we convert the function
3729 into a procedure and its caller will pass a pointer to an object as
3730 the first parameter when we call the function. This can happen for
3731 an unconstrained type with a maximum size or a constrained type with
3732 a size not known at compile time. */
3733 if (TYPE_SIZE_UNIT (gnu_return_type)
3734 && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
3736 returns_by_target_ptr = true;
3737 gnu_param_list
3738 = create_param_decl (get_identifier ("TARGET"),
3739 build_reference_type (gnu_return_type),
3740 true);
3741 gnu_return_type = void_type_node;
3744 /* If the return type has a size that overflows, we cannot have
3745 a function that returns that type. This usage doesn't make
3746 sense anyway, so give an error here. */
3747 if (TYPE_SIZE_UNIT (gnu_return_type)
3748 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3749 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3751 post_error ("cannot return type whose size overflows",
3752 gnat_entity);
3753 gnu_return_type = copy_node (gnu_return_type);
3754 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3755 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3756 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3757 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3760 /* Look at all our parameters and get the type of
3761 each. While doing this, build a copy-out structure if
3762 we need one. */
3764 /* Loop over the parameters and get their associated GCC tree.
3765 While doing this, build a copy-out structure if we need one. */
3766 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3767 Present (gnat_param);
3768 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3770 tree gnu_param_name = get_entity_name (gnat_param);
3771 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3772 tree gnu_param, gnu_field;
3773 bool copy_in_copy_out = false;
3774 Mechanism_Type mech = Mechanism (gnat_param);
3776 /* Builtins are expanded inline and there is no real call sequence
3777 involved. So the type expected by the underlying expander is
3778 always the type of each argument "as is". */
3779 if (gnu_builtin_decl)
3780 mech = By_Copy;
3781 /* Handle the first parameter of a valued procedure specially. */
3782 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3783 mech = By_Copy_Return;
3784 /* Otherwise, see if a Mechanism was supplied that forced this
3785 parameter to be passed one way or another. */
3786 else if (mech == Default
3787 || mech == By_Copy || mech == By_Reference)
3789 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
3790 mech = By_Descriptor;
3791 else if (mech > 0)
3793 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3794 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3795 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3796 mech))
3797 mech = By_Reference;
3798 else
3799 mech = By_Copy;
3801 else
3803 post_error ("unsupported mechanism for&", gnat_param);
3804 mech = Default;
3807 gnu_param
3808 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
3809 Has_Foreign_Convention (gnat_entity),
3810 &copy_in_copy_out);
3812 /* We are returned either a PARM_DECL or a type if no parameter
3813 needs to be passed; in either case, adjust the type. */
3814 if (DECL_P (gnu_param))
3815 gnu_param_type = TREE_TYPE (gnu_param);
3816 else
3818 gnu_param_type = gnu_param;
3819 gnu_param = NULL_TREE;
3822 if (gnu_param)
3824 /* If it's an exported subprogram, we build a parameter list
3825 in parallel, in case we need to emit a stub for it. */
3826 if (Is_Exported (gnat_entity))
3828 gnu_stub_param_list
3829 = chainon (gnu_param, gnu_stub_param_list);
3830 /* Change By_Descriptor parameter to By_Reference for
3831 the internal version of an exported subprogram. */
3832 if (mech == By_Descriptor)
3834 gnu_param
3835 = gnat_to_gnu_param (gnat_param, By_Reference,
3836 gnat_entity, false,
3837 &copy_in_copy_out);
3838 has_stub = true;
3840 else
3841 gnu_param = copy_node (gnu_param);
3844 gnu_param_list = chainon (gnu_param, gnu_param_list);
3845 Sloc_to_locus (Sloc (gnat_param),
3846 &DECL_SOURCE_LOCATION (gnu_param));
3847 save_gnu_tree (gnat_param, gnu_param, false);
3849 /* If a parameter is a pointer, this function may modify
3850 memory through it and thus shouldn't be considered
3851 a pure function. Also, the memory may be modified
3852 between two calls, so they can't be CSE'ed. The latter
3853 case also handles by-ref parameters. */
3854 if (POINTER_TYPE_P (gnu_param_type)
3855 || TYPE_FAT_POINTER_P (gnu_param_type))
3856 pure_flag = false;
3859 if (copy_in_copy_out)
3861 if (!has_copy_in_out)
3863 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3864 gnu_return_type = make_node (RECORD_TYPE);
3865 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3866 has_copy_in_out = true;
3869 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3870 gnu_return_type, 0, 0, 0, 0);
3871 Sloc_to_locus (Sloc (gnat_param),
3872 &DECL_SOURCE_LOCATION (gnu_field));
3873 TREE_CHAIN (gnu_field) = gnu_field_list;
3874 gnu_field_list = gnu_field;
3875 gnu_return_list = tree_cons (gnu_field, gnu_param,
3876 gnu_return_list);
3880 /* Do not compute record for out parameters if subprogram is
3881 stubbed since structures are incomplete for the back-end. */
3882 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
3883 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3884 0, false);
3886 /* If we have a CICO list but it has only one entry, we convert
3887 this function into a function that simply returns that one
3888 object. */
3889 if (list_length (gnu_return_list) == 1)
3890 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3892 if (Has_Stdcall_Convention (gnat_entity))
3893 prepend_one_attribute_to
3894 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3895 get_identifier ("stdcall"), NULL_TREE,
3896 gnat_entity);
3898 /* The lists have been built in reverse. */
3899 gnu_param_list = nreverse (gnu_param_list);
3900 if (has_stub)
3901 gnu_stub_param_list = nreverse (gnu_stub_param_list);
3902 gnu_return_list = nreverse (gnu_return_list);
3904 if (Ekind (gnat_entity) == E_Function)
3905 Set_Mechanism (gnat_entity,
3906 (returns_by_ref || returns_unconstrained
3907 ? By_Reference : By_Copy));
3908 gnu_type
3909 = create_subprog_type (gnu_return_type, gnu_param_list,
3910 gnu_return_list, returns_unconstrained,
3911 returns_by_ref, returns_by_target_ptr);
3913 if (has_stub)
3914 gnu_stub_type
3915 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
3916 gnu_return_list, returns_unconstrained,
3917 returns_by_ref, returns_by_target_ptr);
3919 /* A subprogram (something that doesn't return anything) shouldn't
3920 be considered Pure since there would be no reason for such a
3921 subprogram. Note that procedures with Out (or In Out) parameters
3922 have already been converted into a function with a return type. */
3923 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3924 pure_flag = false;
3926 /* The semantics of "pure" in Ada essentially matches that of "const"
3927 in the back-end. In particular, both properties are orthogonal to
3928 the "nothrow" property. But this is true only if the EH circuitry
3929 is explicit in the internal representation of the back-end. If we
3930 are to completely hide the EH circuitry from it, we need to declare
3931 that calls to pure Ada subprograms that can throw have side effects
3932 since they can trigger an "abnormal" transfer of control flow; thus
3933 they can be neither "const" nor "pure" in the back-end sense. */
3934 gnu_type
3935 = build_qualified_type (gnu_type,
3936 TYPE_QUALS (gnu_type)
3937 | (Exception_Mechanism == Back_End_Exceptions
3938 ? TYPE_QUAL_CONST * pure_flag : 0)
3939 | (TYPE_QUAL_VOLATILE * volatile_flag));
3941 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3943 if (has_stub)
3944 gnu_stub_type
3945 = build_qualified_type (gnu_stub_type,
3946 TYPE_QUALS (gnu_stub_type)
3947 | (Exception_Mechanism == Back_End_Exceptions
3948 ? TYPE_QUAL_CONST * pure_flag : 0)
3949 | (TYPE_QUAL_VOLATILE * volatile_flag));
3951 /* If we have a builtin decl for that function, check the signatures
3952 compatibilities. If the signatures are compatible, use the builtin
3953 decl. If they are not, we expect the checker predicate to have
3954 posted the appropriate errors, and just continue with what we have
3955 so far. */
3956 if (gnu_builtin_decl)
3958 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3960 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3962 gnu_decl = gnu_builtin_decl;
3963 gnu_type = gnu_builtin_type;
3964 break;
3968 /* If there was no specified Interface_Name and the external and
3969 internal names of the subprogram are the same, only use the
3970 internal name to allow disambiguation of nested subprograms. */
3971 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3972 gnu_ext_name = NULL_TREE;
3974 /* If we are defining the subprogram and it has an Address clause
3975 we must get the address expression from the saved GCC tree for the
3976 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3977 the address expression here since the front-end has guaranteed
3978 in that case that the elaboration has no effects. If there is
3979 an Address clause and we are not defining the object, just
3980 make it a constant. */
3981 if (Present (Address_Clause (gnat_entity)))
3983 tree gnu_address = NULL_TREE;
3985 if (definition)
3986 gnu_address
3987 = (present_gnu_tree (gnat_entity)
3988 ? get_gnu_tree (gnat_entity)
3989 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3991 save_gnu_tree (gnat_entity, NULL_TREE, false);
3993 /* Convert the type of the object to a reference type that can
3994 alias everything as per 13.3(19). */
3995 gnu_type
3996 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
3997 if (gnu_address)
3998 gnu_address = convert (gnu_type, gnu_address);
4000 gnu_decl
4001 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
4002 gnu_address, false, Is_Public (gnat_entity),
4003 extern_flag, false, NULL, gnat_entity);
4004 DECL_BY_REF_P (gnu_decl) = 1;
4007 else if (kind == E_Subprogram_Type)
4008 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4009 !Comes_From_Source (gnat_entity),
4010 debug_info_p, gnat_entity);
4011 else
4013 if (has_stub)
4015 gnu_stub_name = gnu_ext_name;
4016 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4017 public_flag = false;
4020 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
4021 gnu_type, gnu_param_list,
4022 inline_flag, public_flag,
4023 extern_flag, attr_list,
4024 gnat_entity);
4025 if (has_stub)
4027 tree gnu_stub_decl
4028 = create_subprog_decl (gnu_entity_id, gnu_stub_name,
4029 gnu_stub_type, gnu_stub_param_list,
4030 inline_flag, true,
4031 extern_flag, attr_list,
4032 gnat_entity);
4033 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4036 /* This is unrelated to the stub built right above. */
4037 DECL_STUBBED_P (gnu_decl)
4038 = Convention (gnat_entity) == Convention_Stubbed;
4041 break;
4043 case E_Incomplete_Type:
4044 case E_Incomplete_Subtype:
4045 case E_Private_Type:
4046 case E_Private_Subtype:
4047 case E_Limited_Private_Type:
4048 case E_Limited_Private_Subtype:
4049 case E_Record_Type_With_Private:
4050 case E_Record_Subtype_With_Private:
4052 /* Get the "full view" of this entity. If this is an incomplete
4053 entity from a limited with, treat its non-limited view as the
4054 full view. Otherwise, use either the full view or the underlying
4055 full view, whichever is present. This is used in all the tests
4056 below. */
4057 Entity_Id full_view
4058 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4059 && From_With_Type (gnat_entity))
4060 ? Non_Limited_View (gnat_entity)
4061 : Present (Full_View (gnat_entity))
4062 ? Full_View (gnat_entity)
4063 : Underlying_Full_View (gnat_entity);
4065 /* If this is an incomplete type with no full view, it must be a Taft
4066 Amendment type, in which case we return a dummy type. Otherwise,
4067 just get the type from its Etype. */
4068 if (No (full_view))
4070 if (kind == E_Incomplete_Type)
4071 gnu_type = make_dummy_type (gnat_entity);
4072 else
4074 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4075 NULL_TREE, 0);
4076 maybe_present = true;
4078 break;
4081 /* If we already made a type for the full view, reuse it. */
4082 else if (present_gnu_tree (full_view))
4084 gnu_decl = get_gnu_tree (full_view);
4085 break;
4088 /* Otherwise, if we are not defining the type now, get the type
4089 from the full view. But always get the type from the full view
4090 for define on use types, since otherwise we won't see them! */
4091 else if (!definition
4092 || (Is_Itype (full_view)
4093 && No (Freeze_Node (gnat_entity)))
4094 || (Is_Itype (gnat_entity)
4095 && No (Freeze_Node (full_view))))
4097 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4098 maybe_present = true;
4099 break;
4102 /* For incomplete types, make a dummy type entry which will be
4103 replaced later. */
4104 gnu_type = make_dummy_type (gnat_entity);
4106 /* Save this type as the full declaration's type so we can do any
4107 needed updates when we see it. */
4108 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4109 !Comes_From_Source (gnat_entity),
4110 debug_info_p, gnat_entity);
4111 save_gnu_tree (full_view, gnu_decl, 0);
4112 break;
4115 /* Simple class_wide types are always viewed as their root_type
4116 by Gigi unless an Equivalent_Type is specified. */
4117 case E_Class_Wide_Type:
4118 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4119 maybe_present = true;
4120 break;
4122 case E_Task_Type:
4123 case E_Task_Subtype:
4124 case E_Protected_Type:
4125 case E_Protected_Subtype:
4126 if (type_annotate_only && No (gnat_equiv_type))
4127 gnu_type = void_type_node;
4128 else
4129 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4131 maybe_present = true;
4132 break;
4134 case E_Label:
4135 gnu_decl = create_label_decl (gnu_entity_id);
4136 break;
4138 case E_Block:
4139 case E_Loop:
4140 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4141 we've already saved it, so we don't try to. */
4142 gnu_decl = error_mark_node;
4143 saved = true;
4144 break;
4146 default:
4147 gcc_unreachable ();
4150 /* If we had a case where we evaluated another type and it might have
4151 defined this one, handle it here. */
4152 if (maybe_present && present_gnu_tree (gnat_entity))
4154 gnu_decl = get_gnu_tree (gnat_entity);
4155 saved = true;
4158 /* If we are processing a type and there is either no decl for it or
4159 we just made one, do some common processing for the type, such as
4160 handling alignment and possible padding. */
4162 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4164 if (Is_Tagged_Type (gnat_entity)
4165 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4166 TYPE_ALIGN_OK (gnu_type) = 1;
4168 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4169 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4171 /* ??? Don't set the size for a String_Literal since it is either
4172 confirming or we don't handle it properly (if the low bound is
4173 non-constant). */
4174 if (!gnu_size && kind != E_String_Literal_Subtype)
4175 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4176 TYPE_DECL, false,
4177 Has_Size_Clause (gnat_entity));
4179 /* If a size was specified, see if we can make a new type of that size
4180 by rearranging the type, for example from a fat to a thin pointer. */
4181 if (gnu_size)
4183 gnu_type
4184 = make_type_from_size (gnu_type, gnu_size,
4185 Has_Biased_Representation (gnat_entity));
4187 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4188 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4189 gnu_size = 0;
4192 /* If the alignment hasn't already been processed and this is
4193 not an unconstrained array, see if an alignment is specified.
4194 If not, we pick a default alignment for atomic objects. */
4195 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4197 else if (Known_Alignment (gnat_entity))
4199 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4200 TYPE_ALIGN (gnu_type));
4202 /* Warn on suspiciously large alignments. This should catch
4203 errors about the (alignment,byte)/(size,bit) discrepancy. */
4204 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4206 tree size;
4208 /* If a size was specified, take it into account. Otherwise
4209 use the RM size for records as the type size has already
4210 been adjusted to the alignment. */
4211 if (gnu_size)
4212 size = gnu_size;
4213 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4214 || TREE_CODE (gnu_type) == UNION_TYPE
4215 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4216 && !TYPE_IS_FAT_POINTER_P (gnu_type))
4217 size = rm_size (gnu_type);
4218 else
4219 size = TYPE_SIZE (gnu_type);
4221 /* Consider an alignment as suspicious if the alignment/size
4222 ratio is greater or equal to the byte/bit ratio. */
4223 if (host_integerp (size, 1)
4224 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4225 post_error_ne ("?suspiciously large alignment specified for&",
4226 Expression (Alignment_Clause (gnat_entity)),
4227 gnat_entity);
4230 else if (Is_Atomic (gnat_entity) && !gnu_size
4231 && host_integerp (TYPE_SIZE (gnu_type), 1)
4232 && integer_pow2p (TYPE_SIZE (gnu_type)))
4233 align = MIN (BIGGEST_ALIGNMENT,
4234 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4235 else if (Is_Atomic (gnat_entity) && gnu_size
4236 && host_integerp (gnu_size, 1)
4237 && integer_pow2p (gnu_size))
4238 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4240 /* See if we need to pad the type. If we did, and made a record,
4241 the name of the new type may be changed. So get it back for
4242 us when we make the new TYPE_DECL below. */
4243 if (gnu_size || align > 0)
4244 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4245 "PAD", true, definition, false);
4247 if (TREE_CODE (gnu_type) == RECORD_TYPE
4248 && TYPE_IS_PADDING_P (gnu_type))
4250 gnu_entity_id = TYPE_NAME (gnu_type);
4251 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4252 gnu_entity_id = DECL_NAME (gnu_entity_id);
4255 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4257 /* If we are at global level, GCC will have applied variable_size to
4258 the type, but that won't have done anything. So, if it's not
4259 a constant or self-referential, call elaborate_expression_1 to
4260 make a variable for the size rather than calculating it each time.
4261 Handle both the RM size and the actual size. */
4262 if (global_bindings_p ()
4263 && TYPE_SIZE (gnu_type)
4264 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4265 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4267 if (TREE_CODE (gnu_type) == RECORD_TYPE
4268 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4269 TYPE_SIZE (gnu_type), 0))
4271 TYPE_SIZE (gnu_type)
4272 = elaborate_expression_1 (gnat_entity, gnat_entity,
4273 TYPE_SIZE (gnu_type),
4274 get_identifier ("SIZE"),
4275 definition, 0);
4276 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4278 else
4280 TYPE_SIZE (gnu_type)
4281 = elaborate_expression_1 (gnat_entity, gnat_entity,
4282 TYPE_SIZE (gnu_type),
4283 get_identifier ("SIZE"),
4284 definition, 0);
4286 /* ??? For now, store the size as a multiple of the alignment
4287 in bytes so that we can see the alignment from the tree. */
4288 TYPE_SIZE_UNIT (gnu_type)
4289 = build_binary_op
4290 (MULT_EXPR, sizetype,
4291 elaborate_expression_1
4292 (gnat_entity, gnat_entity,
4293 build_binary_op (EXACT_DIV_EXPR, sizetype,
4294 TYPE_SIZE_UNIT (gnu_type),
4295 size_int (TYPE_ALIGN (gnu_type)
4296 / BITS_PER_UNIT)),
4297 get_identifier ("SIZE_A_UNIT"),
4298 definition, 0),
4299 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4301 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4302 SET_TYPE_ADA_SIZE
4303 (gnu_type,
4304 elaborate_expression_1 (gnat_entity,
4305 gnat_entity,
4306 TYPE_ADA_SIZE (gnu_type),
4307 get_identifier ("RM_SIZE"),
4308 definition, 0));
4312 /* If this is a record type or subtype, call elaborate_expression_1 on
4313 any field position. Do this for both global and local types.
4314 Skip any fields that we haven't made trees for to avoid problems with
4315 class wide types. */
4316 if (IN (kind, Record_Kind))
4317 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4318 gnat_temp = Next_Entity (gnat_temp))
4319 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4321 tree gnu_field = get_gnu_tree (gnat_temp);
4323 /* ??? Unfortunately, GCC needs to be able to prove the
4324 alignment of this offset and if it's a variable, it can't.
4325 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4326 right now, we have to put in an explicit multiply and
4327 divide by that value. */
4328 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4330 DECL_FIELD_OFFSET (gnu_field)
4331 = build_binary_op
4332 (MULT_EXPR, sizetype,
4333 elaborate_expression_1
4334 (gnat_temp, gnat_temp,
4335 build_binary_op (EXACT_DIV_EXPR, sizetype,
4336 DECL_FIELD_OFFSET (gnu_field),
4337 size_int (DECL_OFFSET_ALIGN (gnu_field)
4338 / BITS_PER_UNIT)),
4339 get_identifier ("OFFSET"),
4340 definition, 0),
4341 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4343 /* ??? The context of gnu_field is not necessarily gnu_type so
4344 the MULT_EXPR node built above may not be marked by the call
4345 to create_type_decl below. Mark it manually for now. */
4346 if (global_bindings_p ())
4347 TREE_VISITED (DECL_FIELD_OFFSET (gnu_field)) = 1;
4351 gnu_type = build_qualified_type (gnu_type,
4352 (TYPE_QUALS (gnu_type)
4353 | (TYPE_QUAL_VOLATILE
4354 * Treat_As_Volatile (gnat_entity))));
4356 if (Is_Atomic (gnat_entity))
4357 check_ok_for_atomic (gnu_type, gnat_entity, false);
4359 if (Present (Alignment_Clause (gnat_entity)))
4360 TYPE_USER_ALIGN (gnu_type) = 1;
4362 if (Universal_Aliasing (gnat_entity))
4363 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4365 if (!gnu_decl)
4366 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4367 !Comes_From_Source (gnat_entity),
4368 debug_info_p, gnat_entity);
4369 else
4370 TREE_TYPE (gnu_decl) = gnu_type;
4373 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4375 gnu_type = TREE_TYPE (gnu_decl);
4377 /* Back-annotate the Alignment of the type if not already in the
4378 tree. Likewise for sizes. */
4379 if (Unknown_Alignment (gnat_entity))
4380 Set_Alignment (gnat_entity,
4381 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4383 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4385 /* If the size is self-referential, we annotate the maximum
4386 value of that size. */
4387 tree gnu_size = TYPE_SIZE (gnu_type);
4389 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4390 gnu_size = max_size (gnu_size, true);
4392 Set_Esize (gnat_entity, annotate_value (gnu_size));
4394 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4396 /* In this mode the tag and the parent components are not
4397 generated by the front-end, so the sizes must be adjusted
4398 explicitly now. */
4399 int size_offset, new_size;
4401 if (Is_Derived_Type (gnat_entity))
4403 size_offset
4404 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4405 Set_Alignment (gnat_entity,
4406 Alignment (Etype (Base_Type (gnat_entity))));
4408 else
4409 size_offset = POINTER_SIZE;
4411 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4412 Set_Esize (gnat_entity,
4413 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4414 / POINTER_SIZE) * POINTER_SIZE));
4415 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4419 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4420 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4423 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4424 DECL_ARTIFICIAL (gnu_decl) = 1;
4426 if (!debug_info_p && DECL_P (gnu_decl)
4427 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4428 && No (Renamed_Object (gnat_entity)))
4429 DECL_IGNORED_P (gnu_decl) = 1;
4431 /* If we haven't already, associate the ..._DECL node that we just made with
4432 the input GNAT entity node. */
4433 if (!saved)
4434 save_gnu_tree (gnat_entity, gnu_decl, false);
4436 /* If this is an enumeral or floating-point type, we were not able to set
4437 the bounds since they refer to the type. These bounds are always static.
4439 For enumeration types, also write debugging information and declare the
4440 enumeration literal table, if needed. */
4442 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4443 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4445 tree gnu_scalar_type = gnu_type;
4447 /* If this is a padded type, we need to use the underlying type. */
4448 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4449 && TYPE_IS_PADDING_P (gnu_scalar_type))
4450 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4452 /* If this is a floating point type and we haven't set a floating
4453 point type yet, use this in the evaluation of the bounds. */
4454 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4455 longest_float_type_node = gnu_type;
4457 TYPE_MIN_VALUE (gnu_scalar_type)
4458 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4459 TYPE_MAX_VALUE (gnu_scalar_type)
4460 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4462 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4464 /* Since this has both a typedef and a tag, avoid outputting
4465 the name twice. */
4466 DECL_ARTIFICIAL (gnu_decl) = 1;
4467 rest_of_type_decl_compilation (gnu_decl);
4471 /* If we deferred processing of incomplete types, re-enable it. If there
4472 were no other disables and we have some to process, do so. */
4473 if (this_deferred && --defer_incomplete_level == 0)
4475 if (defer_incomplete_list)
4477 struct incomplete *incp, *next;
4479 /* We are back to level 0 for the deferring of incomplete types.
4480 But processing these incomplete types below may itself require
4481 deferring, so preserve what we have and restart from scratch. */
4482 incp = defer_incomplete_list;
4483 defer_incomplete_list = NULL;
4485 /* For finalization, however, all types must be complete so we
4486 cannot do the same because deferred incomplete types may end up
4487 referencing each other. Process them all recursively first. */
4488 defer_finalize_level++;
4490 for (; incp; incp = next)
4492 next = incp->next;
4494 if (incp->old_type)
4495 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4496 gnat_to_gnu_type (incp->full_type));
4497 free (incp);
4500 defer_finalize_level--;
4503 /* All the deferred incomplete types have been processed so we can
4504 now proceed with the finalization of the deferred types. */
4505 if (defer_finalize_level == 0 && defer_finalize_list)
4507 unsigned int i;
4508 tree t;
4510 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4511 rest_of_type_decl_compilation_no_defer (t);
4513 VEC_free (tree, heap, defer_finalize_list);
4517 /* If we are not defining this type, see if it's in the incomplete list.
4518 If so, handle that list entry now. */
4519 else if (!definition)
4521 struct incomplete *incp;
4523 for (incp = defer_incomplete_list; incp; incp = incp->next)
4524 if (incp->old_type && incp->full_type == gnat_entity)
4526 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4527 TREE_TYPE (gnu_decl));
4528 incp->old_type = NULL_TREE;
4532 if (this_global)
4533 force_global--;
4535 if (Is_Packed_Array_Type (gnat_entity)
4536 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4537 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4538 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4539 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4541 return gnu_decl;
4544 /* Similar, but if the returned value is a COMPONENT_REF, return the
4545 FIELD_DECL. */
4547 tree
4548 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4550 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4552 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4553 gnu_field = TREE_OPERAND (gnu_field, 1);
4555 return gnu_field;
4558 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4559 Every TYPE_DECL generated for a type definition must be passed
4560 to this function once everything else has been done for it. */
4562 void
4563 rest_of_type_decl_compilation (tree decl)
4565 /* We need to defer finalizing the type if incomplete types
4566 are being deferred or if they are being processed. */
4567 if (defer_incomplete_level || defer_finalize_level)
4568 VEC_safe_push (tree, heap, defer_finalize_list, decl);
4569 else
4570 rest_of_type_decl_compilation_no_defer (decl);
4573 /* Same as above but without deferring the compilation. This
4574 function should not be invoked directly on a TYPE_DECL. */
4576 static void
4577 rest_of_type_decl_compilation_no_defer (tree decl)
4579 const int toplev = global_bindings_p ();
4580 tree t = TREE_TYPE (decl);
4582 rest_of_decl_compilation (decl, toplev, 0);
4584 /* Now process all the variants. This is needed for STABS. */
4585 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
4587 if (t == TREE_TYPE (decl))
4588 continue;
4590 if (!TYPE_STUB_DECL (t))
4592 TYPE_STUB_DECL (t) = build_decl (TYPE_DECL, DECL_NAME (decl), t);
4593 DECL_ARTIFICIAL (TYPE_STUB_DECL (t)) = 1;
4596 rest_of_type_compilation (t, toplev);
4600 /* Finalize any From_With_Type incomplete types. We do this after processing
4601 our compilation unit and after processing its spec, if this is a body. */
4603 void
4604 finalize_from_with_types (void)
4606 struct incomplete *incp = defer_limited_with;
4607 struct incomplete *next;
4609 defer_limited_with = 0;
4610 for (; incp; incp = next)
4612 next = incp->next;
4614 if (incp->old_type != 0)
4615 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4616 gnat_to_gnu_type (incp->full_type));
4617 free (incp);
4621 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4622 kind of type (such E_Task_Type) that has a different type which Gigi
4623 uses for its representation. If the type does not have a special type
4624 for its representation, return GNAT_ENTITY. If a type is supposed to
4625 exist, but does not, abort unless annotating types, in which case
4626 return Empty. If GNAT_ENTITY is Empty, return Empty. */
4628 Entity_Id
4629 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4631 Entity_Id gnat_equiv = gnat_entity;
4633 if (No (gnat_entity))
4634 return gnat_entity;
4636 switch (Ekind (gnat_entity))
4638 case E_Class_Wide_Subtype:
4639 if (Present (Equivalent_Type (gnat_entity)))
4640 gnat_equiv = Equivalent_Type (gnat_entity);
4641 break;
4643 case E_Access_Protected_Subprogram_Type:
4644 case E_Anonymous_Access_Protected_Subprogram_Type:
4645 gnat_equiv = Equivalent_Type (gnat_entity);
4646 break;
4648 case E_Class_Wide_Type:
4649 gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4650 ? Equivalent_Type (gnat_entity)
4651 : Root_Type (gnat_entity));
4652 break;
4654 case E_Task_Type:
4655 case E_Task_Subtype:
4656 case E_Protected_Type:
4657 case E_Protected_Subtype:
4658 gnat_equiv = Corresponding_Record_Type (gnat_entity);
4659 break;
4661 default:
4662 break;
4665 gcc_assert (Present (gnat_equiv) || type_annotate_only);
4666 return gnat_equiv;
4669 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
4670 using MECH as its passing mechanism, to be placed in the parameter
4671 list built for GNAT_SUBPROG. Assume a foreign convention for the
4672 latter if FOREIGN is true. Also set CICO to true if the parameter
4673 must use the copy-in copy-out implementation mechanism.
4675 The returned tree is a PARM_DECL, except for those cases where no
4676 parameter needs to be actually passed to the subprogram; the type
4677 of this "shadow" parameter is then returned instead. */
4679 static tree
4680 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
4681 Entity_Id gnat_subprog, bool foreign, bool *cico)
4683 tree gnu_param_name = get_entity_name (gnat_param);
4684 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4685 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
4686 /* The parameter can be indirectly modified if its address is taken. */
4687 bool ro_param = in_param && !Address_Taken (gnat_param);
4688 bool by_return = false, by_component_ptr = false, by_ref = false;
4689 tree gnu_param;
4691 /* Copy-return is used only for the first parameter of a valued procedure.
4692 It's a copy mechanism for which a parameter is never allocated. */
4693 if (mech == By_Copy_Return)
4695 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
4696 mech = By_Copy;
4697 by_return = true;
4700 /* If this is either a foreign function or if the underlying type won't
4701 be passed by reference, strip off possible padding type. */
4702 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
4703 && TYPE_IS_PADDING_P (gnu_param_type))
4705 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
4707 if (mech == By_Reference
4708 || foreign
4709 || (!must_pass_by_ref (unpadded_type)
4710 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
4711 gnu_param_type = unpadded_type;
4714 /* If this is a read-only parameter, make a variant of the type that is
4715 read-only. ??? However, if this is an unconstrained array, that type
4716 can be very complex, so skip it for now. Likewise for any other
4717 self-referential type. */
4718 if (ro_param
4719 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
4720 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
4721 gnu_param_type = build_qualified_type (gnu_param_type,
4722 (TYPE_QUALS (gnu_param_type)
4723 | TYPE_QUAL_CONST));
4725 /* For foreign conventions, pass arrays as pointers to the element type.
4726 First check for unconstrained array and get the underlying array. */
4727 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
4728 gnu_param_type
4729 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
4731 /* VMS descriptors are themselves passed by reference. */
4732 if (mech == By_Descriptor)
4733 gnu_param_type
4734 = build_pointer_type (build_vms_descriptor (gnu_param_type,
4735 Mechanism (gnat_param),
4736 gnat_subprog));
4738 /* Arrays are passed as pointers to element type for foreign conventions. */
4739 else if (foreign
4740 && mech != By_Copy
4741 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
4743 /* Strip off any multi-dimensional entries, then strip
4744 off the last array to get the component type. */
4745 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
4746 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
4747 gnu_param_type = TREE_TYPE (gnu_param_type);
4749 by_component_ptr = true;
4750 gnu_param_type = TREE_TYPE (gnu_param_type);
4752 if (ro_param)
4753 gnu_param_type = build_qualified_type (gnu_param_type,
4754 (TYPE_QUALS (gnu_param_type)
4755 | TYPE_QUAL_CONST));
4757 gnu_param_type = build_pointer_type (gnu_param_type);
4760 /* Fat pointers are passed as thin pointers for foreign conventions. */
4761 else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
4762 gnu_param_type
4763 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
4765 /* If we must pass or were requested to pass by reference, do so.
4766 If we were requested to pass by copy, do so.
4767 Otherwise, for foreign conventions, pass In Out or Out parameters
4768 or aggregates by reference. For COBOL and Fortran, pass all
4769 integer and FP types that way too. For Convention Ada, use
4770 the standard Ada default. */
4771 else if (must_pass_by_ref (gnu_param_type)
4772 || mech == By_Reference
4773 || (mech != By_Copy
4774 && ((foreign
4775 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
4776 || (foreign
4777 && (Convention (gnat_subprog) == Convention_Fortran
4778 || Convention (gnat_subprog) == Convention_COBOL)
4779 && (INTEGRAL_TYPE_P (gnu_param_type)
4780 || FLOAT_TYPE_P (gnu_param_type)))
4781 || (!foreign
4782 && default_pass_by_ref (gnu_param_type)))))
4784 gnu_param_type = build_reference_type (gnu_param_type);
4785 by_ref = true;
4788 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
4789 else if (!in_param)
4790 *cico = true;
4792 if (mech == By_Copy && (by_ref || by_component_ptr))
4793 post_error ("?cannot pass & by copy", gnat_param);
4795 /* If this is an Out parameter that isn't passed by reference and isn't
4796 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
4797 it will be a VAR_DECL created when we process the procedure, so just
4798 return its type. For the special parameter of a valued procedure,
4799 never pass it in.
4801 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
4802 Out parameters with discriminants or implicit initial values to be
4803 handled like In Out parameters. These type are normally built as
4804 aggregates, hence passed by reference, except for some packed arrays
4805 which end up encoded in special integer types.
4807 The exception we need to make is then for packed arrays of records
4808 with discriminants or implicit initial values. We have no light/easy
4809 way to check for the latter case, so we merely check for packed arrays
4810 of records. This may lead to useless copy-in operations, but in very
4811 rare cases only, as these would be exceptions in a set of already
4812 exceptional situations. */
4813 if (Ekind (gnat_param) == E_Out_Parameter
4814 && !by_ref
4815 && (by_return
4816 || (mech != By_Descriptor
4817 && !POINTER_TYPE_P (gnu_param_type)
4818 && !AGGREGATE_TYPE_P (gnu_param_type)))
4819 && !(Is_Array_Type (Etype (gnat_param))
4820 && Is_Packed (Etype (gnat_param))
4821 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
4822 return gnu_param_type;
4824 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
4825 ro_param || by_ref || by_component_ptr);
4826 DECL_BY_REF_P (gnu_param) = by_ref;
4827 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
4828 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor);
4829 DECL_POINTS_TO_READONLY_P (gnu_param)
4830 = (ro_param && (by_ref || by_component_ptr));
4832 /* If no Mechanism was specified, indicate what we're using, then
4833 back-annotate it. */
4834 if (mech == Default)
4835 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
4837 Set_Mechanism (gnat_param, mech);
4838 return gnu_param;
4841 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
4843 static bool
4844 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
4846 while (Present (Corresponding_Discriminant (discr1)))
4847 discr1 = Corresponding_Discriminant (discr1);
4849 while (Present (Corresponding_Discriminant (discr2)))
4850 discr2 = Corresponding_Discriminant (discr2);
4852 return
4853 Original_Record_Component (discr1) == Original_Record_Component (discr2);
4856 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
4857 a non-aliased component in the back-end sense. */
4859 static bool
4860 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
4862 /* If the type below this is a multi-array type, then
4863 this does not have aliased components. */
4864 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
4865 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
4866 return true;
4868 if (Has_Aliased_Components (gnat_type))
4869 return false;
4871 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
4874 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4875 be elaborated at the point of its definition, but do nothing else. */
4877 void
4878 elaborate_entity (Entity_Id gnat_entity)
4880 switch (Ekind (gnat_entity))
4882 case E_Signed_Integer_Subtype:
4883 case E_Modular_Integer_Subtype:
4884 case E_Enumeration_Subtype:
4885 case E_Ordinary_Fixed_Point_Subtype:
4886 case E_Decimal_Fixed_Point_Subtype:
4887 case E_Floating_Point_Subtype:
4889 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4890 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4892 /* ??? Tests for avoiding static constraint error expression
4893 is needed until the front stops generating bogus conversions
4894 on bounds of real types. */
4896 if (!Raises_Constraint_Error (gnat_lb))
4897 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4898 1, 0, Needs_Debug_Info (gnat_entity));
4899 if (!Raises_Constraint_Error (gnat_hb))
4900 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4901 1, 0, Needs_Debug_Info (gnat_entity));
4902 break;
4905 case E_Record_Type:
4907 Node_Id full_definition = Declaration_Node (gnat_entity);
4908 Node_Id record_definition = Type_Definition (full_definition);
4910 /* If this is a record extension, go a level further to find the
4911 record definition. */
4912 if (Nkind (record_definition) == N_Derived_Type_Definition)
4913 record_definition = Record_Extension_Part (record_definition);
4915 break;
4917 case E_Record_Subtype:
4918 case E_Private_Subtype:
4919 case E_Limited_Private_Subtype:
4920 case E_Record_Subtype_With_Private:
4921 if (Is_Constrained (gnat_entity)
4922 && Has_Discriminants (Base_Type (gnat_entity))
4923 && Present (Discriminant_Constraint (gnat_entity)))
4925 Node_Id gnat_discriminant_expr;
4926 Entity_Id gnat_field;
4928 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4929 gnat_discriminant_expr
4930 = First_Elmt (Discriminant_Constraint (gnat_entity));
4931 Present (gnat_field);
4932 gnat_field = Next_Discriminant (gnat_field),
4933 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4934 /* ??? For now, ignore access discriminants. */
4935 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4936 elaborate_expression (Node (gnat_discriminant_expr),
4937 gnat_entity,
4938 get_entity_name (gnat_field), 1, 0, 0);
4940 break;
4945 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4946 any entities on its entity chain similarly. */
4948 void
4949 mark_out_of_scope (Entity_Id gnat_entity)
4951 Entity_Id gnat_sub_entity;
4952 unsigned int kind = Ekind (gnat_entity);
4954 /* If this has an entity list, process all in the list. */
4955 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4956 || IN (kind, Private_Kind)
4957 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4958 || kind == E_Function || kind == E_Generic_Function
4959 || kind == E_Generic_Package || kind == E_Generic_Procedure
4960 || kind == E_Loop || kind == E_Operator || kind == E_Package
4961 || kind == E_Package_Body || kind == E_Procedure
4962 || kind == E_Record_Type || kind == E_Record_Subtype
4963 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4964 for (gnat_sub_entity = First_Entity (gnat_entity);
4965 Present (gnat_sub_entity);
4966 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4967 if (Scope (gnat_sub_entity) == gnat_entity
4968 && gnat_sub_entity != gnat_entity)
4969 mark_out_of_scope (gnat_sub_entity);
4971 /* Now clear this if it has been defined, but only do so if it isn't
4972 a subprogram or parameter. We could refine this, but it isn't
4973 worth it. If this is statically allocated, it is supposed to
4974 hang around out of cope. */
4975 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4976 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4978 save_gnu_tree (gnat_entity, NULL_TREE, true);
4979 save_gnu_tree (gnat_entity, error_mark_node, true);
4983 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4984 is a multi-dimensional array type, do this recursively. */
4986 static void
4987 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4989 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4990 of a one-dimensional array, since the padding has the same alias set
4991 as the field type, but if it's a multi-dimensional array, we need to
4992 see the inner types. */
4993 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4994 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4995 || TYPE_IS_PADDING_P (gnu_old_type)))
4996 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4998 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4999 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
5000 so we need to go down to what does. */
5001 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5002 gnu_old_type
5003 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5005 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5006 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5007 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5008 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
5010 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5011 record_component_aliases (gnu_new_type);
5014 /* Return a TREE_LIST describing the substitutions needed to reflect
5015 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
5016 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
5017 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
5018 gives the tree for the discriminant and TREE_VALUES is the replacement
5019 value. They are in the form of operands to substitute_in_expr.
5020 DEFINITION is as in gnat_to_gnu_entity. */
5022 static tree
5023 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
5024 tree gnu_list, bool definition)
5026 Entity_Id gnat_discrim;
5027 Node_Id gnat_value;
5029 if (No (gnat_type))
5030 gnat_type = Implementation_Base_Type (gnat_subtype);
5032 if (Has_Discriminants (gnat_type))
5033 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
5034 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
5035 Present (gnat_discrim);
5036 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
5037 gnat_value = Next_Elmt (gnat_value))
5038 /* Ignore access discriminants. */
5039 if (!Is_Access_Type (Etype (Node (gnat_value))))
5040 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
5041 elaborate_expression
5042 (Node (gnat_value), gnat_subtype,
5043 get_entity_name (gnat_discrim), definition,
5044 1, 0),
5045 gnu_list);
5047 return gnu_list;
5050 /* Return true if the size represented by GNU_SIZE can be handled by an
5051 allocation. If STATIC_P is true, consider only what can be done with a
5052 static allocation. */
5054 static bool
5055 allocatable_size_p (tree gnu_size, bool static_p)
5057 HOST_WIDE_INT our_size;
5059 /* If this is not a static allocation, the only case we want to forbid
5060 is an overflowing size. That will be converted into a raise a
5061 Storage_Error. */
5062 if (!static_p)
5063 return !(TREE_CODE (gnu_size) == INTEGER_CST
5064 && TREE_OVERFLOW (gnu_size));
5066 /* Otherwise, we need to deal with both variable sizes and constant
5067 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5068 since assemblers may not like very large sizes. */
5069 if (!host_integerp (gnu_size, 1))
5070 return false;
5072 our_size = tree_low_cst (gnu_size, 1);
5073 return (int) our_size == our_size;
5076 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5077 NAME, ARGS and ERROR_POINT. */
5079 static void
5080 prepend_one_attribute_to (struct attrib ** attr_list,
5081 enum attr_type attr_type,
5082 tree attr_name,
5083 tree attr_args,
5084 Node_Id attr_error_point)
5086 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5088 attr->type = attr_type;
5089 attr->name = attr_name;
5090 attr->args = attr_args;
5091 attr->error_point = attr_error_point;
5093 attr->next = *attr_list;
5094 *attr_list = attr;
5097 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5099 static void
5100 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5102 Node_Id gnat_temp;
5104 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5105 gnat_temp = Next_Rep_Item (gnat_temp))
5106 if (Nkind (gnat_temp) == N_Pragma)
5108 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5109 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5110 enum attr_type etype;
5112 if (Present (gnat_assoc) && Present (First (gnat_assoc))
5113 && Present (Next (First (gnat_assoc)))
5114 && (Nkind (Expression (Next (First (gnat_assoc))))
5115 == N_String_Literal))
5117 gnu_arg0 = get_identifier (TREE_STRING_POINTER
5118 (gnat_to_gnu
5119 (Expression (Next
5120 (First (gnat_assoc))))));
5121 if (Present (Next (Next (First (gnat_assoc))))
5122 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5123 == N_String_Literal))
5124 gnu_arg1 = get_identifier (TREE_STRING_POINTER
5125 (gnat_to_gnu
5126 (Expression
5127 (Next (Next
5128 (First (gnat_assoc)))))));
5131 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5133 case Pragma_Machine_Attribute:
5134 etype = ATTR_MACHINE_ATTRIBUTE;
5135 break;
5137 case Pragma_Linker_Alias:
5138 etype = ATTR_LINK_ALIAS;
5139 break;
5141 case Pragma_Linker_Section:
5142 etype = ATTR_LINK_SECTION;
5143 break;
5145 case Pragma_Linker_Constructor:
5146 etype = ATTR_LINK_CONSTRUCTOR;
5147 break;
5149 case Pragma_Linker_Destructor:
5150 etype = ATTR_LINK_DESTRUCTOR;
5151 break;
5153 case Pragma_Weak_External:
5154 etype = ATTR_WEAK_EXTERNAL;
5155 break;
5157 default:
5158 continue;
5162 /* Prepend to the list now. Make a list of the argument we might
5163 have, as GCC expects it. */
5164 prepend_one_attribute_to
5165 (attr_list,
5166 etype, gnu_arg0,
5167 (gnu_arg1 != NULL_TREE)
5168 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5169 Present (Next (First (gnat_assoc)))
5170 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5174 /* Get the unpadded version of a GNAT type. */
5176 tree
5177 get_unpadded_type (Entity_Id gnat_entity)
5179 tree type = gnat_to_gnu_type (gnat_entity);
5181 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5182 type = TREE_TYPE (TYPE_FIELDS (type));
5184 return type;
5187 /* Called when we need to protect a variable object using a save_expr. */
5189 tree
5190 maybe_variable (tree gnu_operand)
5192 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5193 || TREE_CODE (gnu_operand) == SAVE_EXPR
5194 || TREE_CODE (gnu_operand) == NULL_EXPR)
5195 return gnu_operand;
5197 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5199 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5200 TREE_TYPE (gnu_operand),
5201 variable_size (TREE_OPERAND (gnu_operand, 0)));
5203 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5204 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5205 return gnu_result;
5207 else
5208 return variable_size (gnu_operand);
5211 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5212 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5213 return the GCC tree to use for that expression. GNU_NAME is the
5214 qualification to use if an external name is appropriate and DEFINITION is
5215 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
5216 we need a result. Otherwise, we are just elaborating this for
5217 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
5218 purposes even if it isn't needed for code generation. */
5220 static tree
5221 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5222 tree gnu_name, bool definition, bool need_value,
5223 bool need_debug)
5225 tree gnu_expr;
5227 /* If we already elaborated this expression (e.g., it was involved
5228 in the definition of a private type), use the old value. */
5229 if (present_gnu_tree (gnat_expr))
5230 return get_gnu_tree (gnat_expr);
5232 /* If we don't need a value and this is static or a discriminant, we
5233 don't need to do anything. */
5234 else if (!need_value
5235 && (Is_OK_Static_Expression (gnat_expr)
5236 || (Nkind (gnat_expr) == N_Identifier
5237 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5238 return 0;
5240 /* Otherwise, convert this tree to its GCC equivalent. */
5241 gnu_expr
5242 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5243 gnu_name, definition, need_debug);
5245 /* Save the expression in case we try to elaborate this entity again. Since
5246 this is not a DECL, don't check it. Don't save if it's a discriminant. */
5247 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5248 save_gnu_tree (gnat_expr, gnu_expr, true);
5250 return need_value ? gnu_expr : error_mark_node;
5253 /* Similar, but take a GNU expression. */
5255 static tree
5256 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5257 tree gnu_expr, tree gnu_name, bool definition,
5258 bool need_debug)
5260 tree gnu_decl = NULL_TREE;
5261 /* Strip any conversions to see if the expression is a readonly variable.
5262 ??? This really should remain readonly, but we have to think about
5263 the typing of the tree here. */
5264 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
5265 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5266 bool expr_variable;
5268 /* In most cases, we won't see a naked FIELD_DECL here because a
5269 discriminant reference will have been replaced with a COMPONENT_REF
5270 when the type is being elaborated. However, there are some cases
5271 involving child types where we will. So convert it to a COMPONENT_REF
5272 here. We have to hope it will be at the highest level of the
5273 expression in these cases. */
5274 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5275 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5276 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5277 gnu_expr, NULL_TREE);
5279 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5280 that is a constant, make a variable that is initialized to contain the
5281 bound when the package containing the definition is elaborated. If
5282 this entity is defined at top level and a bound or discriminant value
5283 isn't a constant or a reference to a discriminant, replace the bound
5284 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5285 rely here on the fact that an expression cannot contain both the
5286 discriminant and some other variable. */
5288 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5289 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5290 && (TREE_READONLY (gnu_inner_expr)
5291 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5292 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5294 /* If this is a static expression or contains a discriminant, we don't
5295 need the variable for debugging (and can't elaborate anyway if a
5296 discriminant). */
5297 if (need_debug
5298 && (Is_OK_Static_Expression (gnat_expr)
5299 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5300 need_debug = false;
5302 /* Now create the variable if we need it. */
5303 if (need_debug || (expr_variable && expr_global))
5304 gnu_decl
5305 = create_var_decl (create_concat_name (gnat_entity,
5306 IDENTIFIER_POINTER (gnu_name)),
5307 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5308 !need_debug, Is_Public (gnat_entity),
5309 !definition, false, NULL, gnat_entity);
5311 /* We only need to use this variable if we are in global context since GCC
5312 can do the right thing in the local case. */
5313 if (expr_global && expr_variable)
5314 return gnu_decl;
5315 else if (!expr_variable)
5316 return gnu_expr;
5317 else
5318 return maybe_variable (gnu_expr);
5321 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5322 starting bit position so that it is aligned to ALIGN bits, and leaving at
5323 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5324 record is guaranteed to get. */
5326 tree
5327 make_aligning_type (tree type, unsigned int align, tree size,
5328 unsigned int base_align, int room)
5330 /* We will be crafting a record type with one field at a position set to be
5331 the next multiple of ALIGN past record'address + room bytes. We use a
5332 record placeholder to express record'address. */
5334 tree record_type = make_node (RECORD_TYPE);
5335 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5337 tree record_addr_st
5338 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5340 /* The diagram below summarizes the shape of what we manipulate:
5342 <--------- pos ---------->
5343 { +------------+-------------+-----------------+
5344 record =>{ |############| ... | field (type) |
5345 { +------------+-------------+-----------------+
5346 |<-- room -->|<- voffset ->|<---- size ----->|
5349 record_addr vblock_addr
5351 Every length is in sizetype bytes there, except "pos" which has to be
5352 set as a bit position in the GCC tree for the record. */
5354 tree room_st = size_int (room);
5355 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5356 tree voffset_st, pos, field;
5358 tree name = TYPE_NAME (type);
5360 if (TREE_CODE (name) == TYPE_DECL)
5361 name = DECL_NAME (name);
5363 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
5365 /* Compute VOFFSET and then POS. The next byte position multiple of some
5366 alignment after some address is obtained by "and"ing the alignment minus
5367 1 with the two's complement of the address. */
5369 voffset_st = size_binop (BIT_AND_EXPR,
5370 size_diffop (size_zero_node, vblock_addr_st),
5371 ssize_int ((align / BITS_PER_UNIT) - 1));
5373 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5375 pos = size_binop (MULT_EXPR,
5376 convert (bitsizetype,
5377 size_binop (PLUS_EXPR, room_st, voffset_st)),
5378 bitsize_unit_node);
5380 /* Craft the GCC record representation. We exceptionally do everything
5381 manually here because 1) our generic circuitry is not quite ready to
5382 handle the complex position/size expressions we are setting up, 2) we
5383 have a strong simplifying factor at hand: we know the maximum possible
5384 value of voffset, and 3) we have to set/reset at least the sizes in
5385 accordance with this maximum value anyway, as we need them to convey
5386 what should be "alloc"ated for this type.
5388 Use -1 as the 'addressable' indication for the field to prevent the
5389 creation of a bitfield. We don't need one, it would have damaging
5390 consequences on the alignment computation, and create_field_decl would
5391 make one without this special argument, for instance because of the
5392 complex position expression. */
5394 field = create_field_decl (get_identifier ("F"), type, record_type,
5395 1, size, pos, -1);
5396 TYPE_FIELDS (record_type) = field;
5398 TYPE_ALIGN (record_type) = base_align;
5399 TYPE_USER_ALIGN (record_type) = 1;
5401 TYPE_SIZE (record_type)
5402 = size_binop (PLUS_EXPR,
5403 size_binop (MULT_EXPR, convert (bitsizetype, size),
5404 bitsize_unit_node),
5405 bitsize_int (align + room * BITS_PER_UNIT));
5406 TYPE_SIZE_UNIT (record_type)
5407 = size_binop (PLUS_EXPR, size,
5408 size_int (room + align / BITS_PER_UNIT));
5410 TYPE_MODE (record_type) = BLKmode;
5412 copy_alias_set (record_type, type);
5413 return record_type;
5416 /* Return the result of rounding T up to ALIGN. */
5418 static inline unsigned HOST_WIDE_INT
5419 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5421 t += align - 1;
5422 t /= align;
5423 t *= align;
5424 return t;
5427 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
5428 as the field type of a packed record if IN_RECORD is true, or as the
5429 component type of a packed array if IN_RECORD is false. See if we can
5430 rewrite it either as a type that has a non-BLKmode, which we can pack
5431 tighter in the packed record case, or as a smaller type with BLKmode.
5432 If so, return the new type. If not, return the original type. */
5434 static tree
5435 make_packable_type (tree type, bool in_record)
5437 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5438 unsigned HOST_WIDE_INT new_size;
5439 tree new_type, old_field, field_list = NULL_TREE;
5441 /* No point in doing anything if the size is zero. */
5442 if (size == 0)
5443 return type;
5445 new_type = make_node (TREE_CODE (type));
5447 /* Copy the name and flags from the old type to that of the new. Note
5448 that we rely on the pointer equality created here for TYPE_NAME at
5449 the end of gnat_to_gnu. */
5450 TYPE_NAME (new_type) = TYPE_NAME (type);
5451 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5452 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5453 if (TREE_CODE (type) == RECORD_TYPE)
5454 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5456 /* If we are in a record and have a small size, set the alignment to
5457 try for an integral mode. Otherwise set it to try for a smaller
5458 type with BLKmode. */
5459 if (in_record && size <= MAX_FIXED_MODE_SIZE)
5461 TYPE_ALIGN (new_type) = ceil_alignment (size);
5462 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5464 else
5466 unsigned HOST_WIDE_INT align;
5468 /* Do not try to shrink the size if the RM size is not constant. */
5469 if (TYPE_CONTAINS_TEMPLATE_P (type)
5470 || !host_integerp (TYPE_ADA_SIZE (type), 1))
5471 return type;
5473 /* Round the RM size up to a unit boundary to get the minimal size
5474 for a BLKmode record. Give up if it's already the size. */
5475 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5476 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5477 if (new_size == size)
5478 return type;
5480 align = new_size & -new_size;
5481 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5484 TYPE_USER_ALIGN (new_type) = 1;
5486 /* Now copy the fields, keeping the position and size as we don't
5487 want to propagate packedness downward. But make an exception
5488 for the last field in order to ditch the padding bits. */
5489 for (old_field = TYPE_FIELDS (type); old_field;
5490 old_field = TREE_CHAIN (old_field))
5492 tree new_field_type = TREE_TYPE (old_field);
5493 tree new_field, new_size;
5495 if (TYPE_MODE (new_field_type) == BLKmode
5496 && (TREE_CODE (new_field_type) == RECORD_TYPE
5497 || TREE_CODE (new_field_type) == UNION_TYPE
5498 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5499 && host_integerp (TYPE_SIZE (new_field_type), 1))
5500 new_field_type = make_packable_type (new_field_type, true);
5502 if (!TREE_CHAIN (old_field) && !TYPE_PACKED (type))
5503 new_size = rm_size (new_field_type);
5504 else
5505 new_size = DECL_SIZE (old_field);
5507 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5508 new_type, TYPE_PACKED (type), new_size,
5509 bit_position (old_field),
5510 !DECL_NONADDRESSABLE_P (old_field));
5512 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5513 SET_DECL_ORIGINAL_FIELD
5514 (new_field, (DECL_ORIGINAL_FIELD (old_field)
5515 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5517 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5518 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5520 TREE_CHAIN (new_field) = field_list;
5521 field_list = new_field;
5524 finish_record_type (new_type, nreverse (field_list), 2, true);
5525 copy_alias_set (new_type, type);
5527 /* If this is a padding record, we never want to make the size smaller
5528 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
5529 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5530 || TREE_CODE (type) == QUAL_UNION_TYPE)
5532 TYPE_SIZE (new_type) = TYPE_SIZE (type);
5533 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5535 else
5537 TYPE_SIZE (new_type) = bitsize_int (new_size);
5538 TYPE_SIZE_UNIT (new_type)
5539 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5542 if (!TYPE_CONTAINS_TEMPLATE_P (type))
5543 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5545 compute_record_mode (new_type);
5547 /* Try harder to get a packable type if necessary, for example
5548 in case the record itself contains a BLKmode field. */
5549 if (in_record && TYPE_MODE (new_type) == BLKmode)
5550 TYPE_MODE (new_type)
5551 = mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1);
5553 /* If neither the mode nor the size has shrunk, return the old type. */
5554 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5555 return type;
5557 return new_type;
5560 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5561 if needed. We have already verified that SIZE and TYPE are large enough.
5563 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5564 to issue a warning.
5566 IS_USER_TYPE is true if we must be sure we complete the original type.
5568 DEFINITION is true if this type is being defined.
5570 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be set
5571 to SIZE too; otherwise, it's set to the RM_Size of the original type. */
5573 tree
5574 maybe_pad_type (tree type, tree size, unsigned int align,
5575 Entity_Id gnat_entity, const char *name_trailer,
5576 bool is_user_type, bool definition, bool same_rm_size)
5578 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
5579 tree orig_size = TYPE_SIZE (type);
5580 unsigned int orig_align = align;
5581 tree record, field;
5583 /* If TYPE is a padded type, see if it agrees with any size and alignment
5584 we were given. If so, return the original type. Otherwise, strip
5585 off the padding, since we will either be returning the inner type
5586 or repadding it. If no size or alignment is specified, use that of
5587 the original padded type. */
5588 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5590 if ((!size
5591 || operand_equal_p (round_up (size,
5592 MAX (align, TYPE_ALIGN (type))),
5593 round_up (TYPE_SIZE (type),
5594 MAX (align, TYPE_ALIGN (type))),
5596 && (align == 0 || align == TYPE_ALIGN (type)))
5597 return type;
5599 if (!size)
5600 size = TYPE_SIZE (type);
5601 if (align == 0)
5602 align = TYPE_ALIGN (type);
5604 type = TREE_TYPE (TYPE_FIELDS (type));
5605 orig_size = TYPE_SIZE (type);
5608 /* If the size is either not being changed or is being made smaller (which
5609 is not done here (and is only valid for bitfields anyway), show the size
5610 isn't changing. Likewise, clear the alignment if it isn't being
5611 changed. Then return if we aren't doing anything. */
5612 if (size
5613 && (operand_equal_p (size, orig_size, 0)
5614 || (TREE_CODE (orig_size) == INTEGER_CST
5615 && tree_int_cst_lt (size, orig_size))))
5616 size = NULL_TREE;
5618 if (align == TYPE_ALIGN (type))
5619 align = 0;
5621 if (align == 0 && !size)
5622 return type;
5624 /* We used to modify the record in place in some cases, but that could
5625 generate incorrect debugging information. So make a new record
5626 type and name. */
5627 record = make_node (RECORD_TYPE);
5629 if (Present (gnat_entity))
5630 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5632 /* If we were making a type, complete the original type and give it a
5633 name. */
5634 if (is_user_type)
5635 create_type_decl (get_entity_name (gnat_entity), type,
5636 NULL, !Comes_From_Source (gnat_entity),
5637 !(TYPE_NAME (type)
5638 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5639 && DECL_IGNORED_P (TYPE_NAME (type))),
5640 gnat_entity);
5642 /* If we are changing the alignment and the input type is a record with
5643 BLKmode and a small constant size, try to make a form that has an
5644 integral mode. That might allow this record to have an integral mode,
5645 which will be much more efficient. There is no point in doing this if a
5646 size is specified unless it is also smaller than the maximum mode size
5647 and it is incorrect to do this if the size of the original type is not a
5648 multiple of the alignment. */
5649 if (align != 0
5650 && TREE_CODE (type) == RECORD_TYPE
5651 && TYPE_MODE (type) == BLKmode
5652 && TREE_CODE (orig_size) == INTEGER_CST
5653 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
5654 && (!size
5655 || (TREE_CODE (size) == INTEGER_CST
5656 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0))
5657 && value_factor_p (orig_size, align))
5658 type = make_packable_type (type, true);
5660 field = create_field_decl (get_identifier ("F"), type, record, 0,
5661 NULL_TREE, bitsize_zero_node, 1);
5663 DECL_INTERNAL_P (field) = 1;
5664 TYPE_SIZE (record) = size ? size : orig_size;
5665 TYPE_SIZE_UNIT (record)
5666 = (size ? convert (sizetype,
5667 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
5668 : TYPE_SIZE_UNIT (type));
5670 TYPE_ALIGN (record) = align;
5671 if (orig_align)
5672 TYPE_USER_ALIGN (record) = align;
5674 TYPE_IS_PADDING_P (record) = 1;
5675 TYPE_VOLATILE (record)
5676 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5677 /* Do not finalize it until after the auxiliary record is built. */
5678 finish_record_type (record, field, 1, true);
5680 /* Set the same size for its RM_size if requested; otherwise reuse
5681 the RM_size of the original type. */
5682 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
5684 /* Unless debugging information isn't being written for the input type,
5685 write a record that shows what we are a subtype of and also make a
5686 variable that indicates our size, if still variable. */
5687 if (TYPE_NAME (record)
5688 && AGGREGATE_TYPE_P (type)
5689 && TREE_CODE (orig_size) != INTEGER_CST
5690 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5691 && DECL_IGNORED_P (TYPE_NAME (type))))
5693 tree marker = make_node (RECORD_TYPE);
5694 tree name = TYPE_NAME (record);
5695 tree orig_name = TYPE_NAME (type);
5697 if (TREE_CODE (name) == TYPE_DECL)
5698 name = DECL_NAME (name);
5700 if (TREE_CODE (orig_name) == TYPE_DECL)
5701 orig_name = DECL_NAME (orig_name);
5703 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5704 finish_record_type (marker,
5705 create_field_decl (orig_name, integer_type_node,
5706 marker, 0, NULL_TREE, NULL_TREE,
5708 0, false);
5710 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5711 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5712 bitsizetype, TYPE_SIZE (record), false, false, false,
5713 false, NULL, gnat_entity);
5716 rest_of_record_type_compilation (record);
5718 /* If the size was widened explicitly, maybe give a warning. Take the
5719 original size as the maximum size of the input if there was an
5720 unconstrained record involved and round it up to the specified alignment,
5721 if one was specified. */
5722 if (CONTAINS_PLACEHOLDER_P (orig_size))
5723 orig_size = max_size (orig_size, true);
5725 if (align)
5726 orig_size = round_up (orig_size, align);
5728 if (size && Present (gnat_entity)
5729 && !operand_equal_p (size, orig_size, 0)
5730 && !(TREE_CODE (size) == INTEGER_CST
5731 && TREE_CODE (orig_size) == INTEGER_CST
5732 && tree_int_cst_lt (size, orig_size)))
5734 Node_Id gnat_error_node = Empty;
5736 if (Is_Packed_Array_Type (gnat_entity))
5737 gnat_entity = Original_Array_Type (gnat_entity);
5739 if ((Ekind (gnat_entity) == E_Component
5740 || Ekind (gnat_entity) == E_Discriminant)
5741 && Present (Component_Clause (gnat_entity)))
5742 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5743 else if (Present (Size_Clause (gnat_entity)))
5744 gnat_error_node = Expression (Size_Clause (gnat_entity));
5746 /* Generate message only for entities that come from source, since
5747 if we have an entity created by expansion, the message will be
5748 generated for some other corresponding source entity. */
5749 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5750 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5751 gnat_entity,
5752 size_diffop (size, orig_size));
5754 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5755 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5756 gnat_entity, gnat_entity,
5757 size_diffop (size, orig_size));
5760 return record;
5763 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5764 the value passed against the list of choices. */
5766 tree
5767 choices_to_gnu (tree operand, Node_Id choices)
5769 Node_Id choice;
5770 Node_Id gnat_temp;
5771 tree result = integer_zero_node;
5772 tree this_test, low = 0, high = 0, single = 0;
5774 for (choice = First (choices); Present (choice); choice = Next (choice))
5776 switch (Nkind (choice))
5778 case N_Range:
5779 low = gnat_to_gnu (Low_Bound (choice));
5780 high = gnat_to_gnu (High_Bound (choice));
5782 /* There's no good type to use here, so we might as well use
5783 integer_type_node. */
5784 this_test
5785 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5786 build_binary_op (GE_EXPR, integer_type_node,
5787 operand, low),
5788 build_binary_op (LE_EXPR, integer_type_node,
5789 operand, high));
5791 break;
5793 case N_Subtype_Indication:
5794 gnat_temp = Range_Expression (Constraint (choice));
5795 low = gnat_to_gnu (Low_Bound (gnat_temp));
5796 high = gnat_to_gnu (High_Bound (gnat_temp));
5798 this_test
5799 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5800 build_binary_op (GE_EXPR, integer_type_node,
5801 operand, low),
5802 build_binary_op (LE_EXPR, integer_type_node,
5803 operand, high));
5804 break;
5806 case N_Identifier:
5807 case N_Expanded_Name:
5808 /* This represents either a subtype range, an enumeration
5809 literal, or a constant Ekind says which. If an enumeration
5810 literal or constant, fall through to the next case. */
5811 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5812 && Ekind (Entity (choice)) != E_Constant)
5814 tree type = gnat_to_gnu_type (Entity (choice));
5816 low = TYPE_MIN_VALUE (type);
5817 high = TYPE_MAX_VALUE (type);
5819 this_test
5820 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5821 build_binary_op (GE_EXPR, integer_type_node,
5822 operand, low),
5823 build_binary_op (LE_EXPR, integer_type_node,
5824 operand, high));
5825 break;
5827 /* ... fall through ... */
5828 case N_Character_Literal:
5829 case N_Integer_Literal:
5830 single = gnat_to_gnu (choice);
5831 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5832 single);
5833 break;
5835 case N_Others_Choice:
5836 this_test = integer_one_node;
5837 break;
5839 default:
5840 gcc_unreachable ();
5843 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5844 result, this_test);
5847 return result;
5850 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
5851 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
5853 static int
5854 adjust_packed (tree field_type, tree record_type, int packed)
5856 /* If the field contains an item of variable size, we cannot pack it
5857 because we cannot create temporaries of non-fixed size in case
5858 we need to take the address of the field. See addressable_p and
5859 the notes on the addressability issues for further details. */
5860 if (is_variable_size (field_type))
5861 return 0;
5863 /* If the alignment of the record is specified and the field type
5864 is over-aligned, request Storage_Unit alignment for the field. */
5865 if (packed == -2)
5867 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
5868 return -1;
5869 else
5870 return 0;
5873 return packed;
5876 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5877 placed in GNU_RECORD_TYPE.
5879 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
5880 record has Component_Alignment of Storage_Unit, -2 if the enclosing
5881 record has a specified alignment.
5883 DEFINITION is true if this field is for a record being defined. */
5885 static tree
5886 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5887 bool definition)
5889 tree gnu_field_id = get_entity_name (gnat_field);
5890 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5891 tree gnu_field, gnu_size, gnu_pos;
5892 bool needs_strict_alignment
5893 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5894 || Treat_As_Volatile (gnat_field));
5896 /* If this field requires strict alignment, we cannot pack it because
5897 it would very likely be under-aligned in the record. */
5898 if (needs_strict_alignment)
5899 packed = 0;
5900 else
5901 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
5903 /* If a size is specified, use it. Otherwise, if the record type is packed,
5904 use the official RM size. See "Handling of Type'Size Values" in Einfo
5905 for further details. */
5906 if (Known_Static_Esize (gnat_field))
5907 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5908 gnat_field, FIELD_DECL, false, true);
5909 else if (packed == 1)
5910 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5911 gnat_field, FIELD_DECL, false, true);
5912 else
5913 gnu_size = NULL_TREE;
5915 /* If we have a specified size that's smaller than that of the field type,
5916 or a position is specified, and the field type is also a record that's
5917 BLKmode, see if we can get either an integral mode form of the type or
5918 a smaller BLKmode form. If we can, show a size was specified for the
5919 field if there wasn't one already, so we know to make this a bitfield
5920 and avoid making things wider.
5922 Doing this is first useful if the record is packed because we may then
5923 place the field at a non-byte-aligned position and so achieve tighter
5924 packing.
5926 This is in addition *required* if the field shares a byte with another
5927 field and the front-end lets the back-end handle the references, because
5928 GCC does not handle BLKmode bitfields properly.
5930 We avoid the transformation if it is not required or potentially useful,
5931 as it might entail an increase of the field's alignment and have ripple
5932 effects on the outer record type. A typical case is a field known to be
5933 byte aligned and not to share a byte with another field.
5935 Besides, we don't even look the possibility of a transformation in cases
5936 known to be in error already, for instance when an invalid size results
5937 from a component clause. */
5939 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5940 && TYPE_MODE (gnu_field_type) == BLKmode
5941 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5942 && (packed == 1
5943 || (gnu_size
5944 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
5945 || Present (Component_Clause (gnat_field))))))
5947 /* See what the alternate type and size would be. */
5948 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
5950 bool has_byte_aligned_clause
5951 = Present (Component_Clause (gnat_field))
5952 && (UI_To_Int (Component_Bit_Offset (gnat_field))
5953 % BITS_PER_UNIT == 0);
5955 /* Compute whether we should avoid the substitution. */
5956 bool reject
5957 /* There is no point substituting if there is no change... */
5958 = (gnu_packable_type == gnu_field_type)
5959 /* ... nor when the field is known to be byte aligned and not to
5960 share a byte with another field. */
5961 || (has_byte_aligned_clause
5962 && value_factor_p (gnu_size, BITS_PER_UNIT))
5963 /* The size of an aliased field must be an exact multiple of the
5964 type's alignment, which the substitution might increase. Reject
5965 substitutions that would so invalidate a component clause when the
5966 specified position is byte aligned, as the change would have no
5967 real benefit from the packing standpoint anyway. */
5968 || (Is_Aliased (gnat_field)
5969 && has_byte_aligned_clause
5970 && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
5972 /* Substitute unless told otherwise. */
5973 if (!reject)
5975 gnu_field_type = gnu_packable_type;
5977 if (!gnu_size)
5978 gnu_size = rm_size (gnu_field_type);
5982 /* If we are packing the record and the field is BLKmode, round the
5983 size up to a byte boundary. */
5984 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5985 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5987 if (Present (Component_Clause (gnat_field)))
5989 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5990 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5991 gnat_field, FIELD_DECL, false, true);
5993 /* Ensure the position does not overlap with the parent subtype,
5994 if there is one. */
5995 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5997 tree gnu_parent
5998 = gnat_to_gnu_type (Parent_Subtype
5999 (Underlying_Type (Scope (gnat_field))));
6001 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6002 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6004 post_error_ne_tree
6005 ("offset of& must be beyond parent{, minimum allowed is ^}",
6006 First_Bit (Component_Clause (gnat_field)), gnat_field,
6007 TYPE_SIZE_UNIT (gnu_parent));
6011 /* If this field needs strict alignment, ensure the record is
6012 sufficiently aligned and that that position and size are
6013 consistent with the alignment. */
6014 if (needs_strict_alignment)
6016 TYPE_ALIGN (gnu_record_type)
6017 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6019 if (gnu_size
6020 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6022 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6023 post_error_ne_tree
6024 ("atomic field& must be natural size of type{ (^)}",
6025 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6026 TYPE_SIZE (gnu_field_type));
6028 else if (Is_Aliased (gnat_field))
6029 post_error_ne_tree
6030 ("size of aliased field& must be ^ bits",
6031 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6032 TYPE_SIZE (gnu_field_type));
6034 else if (Strict_Alignment (Etype (gnat_field)))
6035 post_error_ne_tree
6036 ("size of & with aliased or tagged components not ^ bits",
6037 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6038 TYPE_SIZE (gnu_field_type));
6040 gnu_size = NULL_TREE;
6043 if (!integer_zerop (size_binop
6044 (TRUNC_MOD_EXPR, gnu_pos,
6045 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6047 if (Is_Aliased (gnat_field))
6048 post_error_ne_num
6049 ("position of aliased field& must be multiple of ^ bits",
6050 First_Bit (Component_Clause (gnat_field)), gnat_field,
6051 TYPE_ALIGN (gnu_field_type));
6053 else if (Treat_As_Volatile (gnat_field))
6054 post_error_ne_num
6055 ("position of volatile field& must be multiple of ^ bits",
6056 First_Bit (Component_Clause (gnat_field)), gnat_field,
6057 TYPE_ALIGN (gnu_field_type));
6059 else if (Strict_Alignment (Etype (gnat_field)))
6060 post_error_ne_num
6061 ("position of & with aliased or tagged components not multiple of ^ bits",
6062 First_Bit (Component_Clause (gnat_field)), gnat_field,
6063 TYPE_ALIGN (gnu_field_type));
6065 else
6066 gcc_unreachable ();
6068 gnu_pos = NULL_TREE;
6072 if (Is_Atomic (gnat_field))
6073 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6076 /* If the record has rep clauses and this is the tag field, make a rep
6077 clause for it as well. */
6078 else if (Has_Specified_Layout (Scope (gnat_field))
6079 && Chars (gnat_field) == Name_uTag)
6081 gnu_pos = bitsize_zero_node;
6082 gnu_size = TYPE_SIZE (gnu_field_type);
6085 else
6086 gnu_pos = NULL_TREE;
6088 /* We need to make the size the maximum for the type if it is
6089 self-referential and an unconstrained type. In that case, we can't
6090 pack the field since we can't make a copy to align it. */
6091 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6092 && !gnu_size
6093 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6094 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6096 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6097 packed = 0;
6100 /* If a size is specified, adjust the field's type to it. */
6101 if (gnu_size)
6103 /* If the field's type is justified modular, we would need to remove
6104 the wrapper to (better) meet the layout requirements. However we
6105 can do so only if the field is not aliased to preserve the unique
6106 layout and if the prescribed size is not greater than that of the
6107 packed array to preserve the justification. */
6108 if (!needs_strict_alignment
6109 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6110 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6111 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6112 <= 0)
6113 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6115 gnu_field_type
6116 = make_type_from_size (gnu_field_type, gnu_size,
6117 Has_Biased_Representation (gnat_field));
6118 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6119 "PAD", false, definition, true);
6122 /* Otherwise (or if there was an error), don't specify a position. */
6123 else
6124 gnu_pos = NULL_TREE;
6126 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6127 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6129 /* Now create the decl for the field. */
6130 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6131 packed, gnu_size, gnu_pos,
6132 Is_Aliased (gnat_field));
6133 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6134 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6136 if (Ekind (gnat_field) == E_Discriminant)
6137 DECL_DISCRIMINANT_NUMBER (gnu_field)
6138 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6140 return gnu_field;
6143 /* Return true if TYPE is a type with variable size, a padding type with a
6144 field of variable size or is a record that has a field such a field. */
6146 static bool
6147 is_variable_size (tree type)
6149 tree field;
6151 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6152 return true;
6154 if (TREE_CODE (type) == RECORD_TYPE
6155 && TYPE_IS_PADDING_P (type)
6156 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6157 return true;
6159 if (TREE_CODE (type) != RECORD_TYPE
6160 && TREE_CODE (type) != UNION_TYPE
6161 && TREE_CODE (type) != QUAL_UNION_TYPE)
6162 return false;
6164 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6165 if (is_variable_size (TREE_TYPE (field)))
6166 return true;
6168 return false;
6171 /* qsort comparer for the bit positions of two record components. */
6173 static int
6174 compare_field_bitpos (const PTR rt1, const PTR rt2)
6176 const_tree const field1 = * (const_tree const *) rt1;
6177 const_tree const field2 = * (const_tree const *) rt2;
6178 const int ret
6179 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6181 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6184 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6185 of GCC trees for fields that are in the record and have already been
6186 processed. When called from gnat_to_gnu_entity during the processing of a
6187 record type definition, the GCC nodes for the discriminants will be on
6188 the chain. The other calls to this function are recursive calls from
6189 itself for the Component_List of a variant and the chain is empty.
6191 PACKED is 1 if this is for a packed record, -1 if this is for a record
6192 with Component_Alignment of Storage_Unit, -2 if this is for a record
6193 with a specified alignment.
6195 DEFINITION is true if we are defining this record.
6197 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6198 with a rep clause is to be added. If it is nonzero, that is all that
6199 should be done with such fields.
6201 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6202 laying out the record. This means the alignment only serves to force fields
6203 to be bitfields, but not require the record to be that aligned. This is
6204 used for variants.
6206 ALL_REP, if true, means a rep clause was found for all the fields. This
6207 simplifies the logic since we know we're not in the mixed case.
6209 DO_NOT_FINALIZE, if true, means that the record type is expected to be
6210 modified afterwards so it will not be sent to the back-end for finalization.
6212 UNCHECKED_UNION, if true, means that we are building a type for a record
6213 with a Pragma Unchecked_Union.
6215 The processing of the component list fills in the chain with all of the
6216 fields of the record and then the record type is finished. */
6218 static void
6219 components_to_record (tree gnu_record_type, Node_Id component_list,
6220 tree gnu_field_list, int packed, bool definition,
6221 tree *p_gnu_rep_list, bool cancel_alignment,
6222 bool all_rep, bool do_not_finalize, bool unchecked_union)
6224 Node_Id component_decl;
6225 Entity_Id gnat_field;
6226 Node_Id variant_part;
6227 tree gnu_our_rep_list = NULL_TREE;
6228 tree gnu_field, gnu_last;
6229 bool layout_with_rep = false;
6230 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6232 /* For each variable within each component declaration create a GCC field
6233 and add it to the list, skipping any pragmas in the list. */
6234 if (Present (Component_Items (component_list)))
6235 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6236 Present (component_decl);
6237 component_decl = Next_Non_Pragma (component_decl))
6239 gnat_field = Defining_Entity (component_decl);
6241 if (Chars (gnat_field) == Name_uParent)
6242 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6243 else
6245 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6246 packed, definition);
6248 /* If this is the _Tag field, put it before any discriminants,
6249 instead of after them as is the case for all other fields.
6250 Ignore field of void type if only annotating. */
6251 if (Chars (gnat_field) == Name_uTag)
6252 gnu_field_list = chainon (gnu_field_list, gnu_field);
6253 else
6255 TREE_CHAIN (gnu_field) = gnu_field_list;
6256 gnu_field_list = gnu_field;
6260 save_gnu_tree (gnat_field, gnu_field, false);
6263 /* At the end of the component list there may be a variant part. */
6264 variant_part = Variant_Part (component_list);
6266 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6267 mutually exclusive and should go in the same memory. To do this we need
6268 to treat each variant as a record whose elements are created from the
6269 component list for the variant. So here we create the records from the
6270 lists for the variants and put them all into the QUAL_UNION_TYPE.
6271 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6272 use GNU_RECORD_TYPE if there are no fields so far. */
6273 if (Present (variant_part))
6275 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
6276 Node_Id variant;
6277 tree gnu_name = TYPE_NAME (gnu_record_type);
6278 tree gnu_var_name
6279 = concat_id_with_name (get_identifier (Get_Name_String
6280 (Chars (Name (variant_part)))),
6281 "XVN");
6282 tree gnu_union_type;
6283 tree gnu_union_name;
6284 tree gnu_union_field;
6285 tree gnu_variant_list = NULL_TREE;
6287 if (TREE_CODE (gnu_name) == TYPE_DECL)
6288 gnu_name = DECL_NAME (gnu_name);
6290 gnu_union_name = concat_id_with_name (gnu_name,
6291 IDENTIFIER_POINTER (gnu_var_name));
6293 /* Reuse an enclosing union if all fields are in the variant part
6294 and there is no representation clause on the record, to match
6295 the layout of C unions. There is an associated check below. */
6296 if (!gnu_field_list
6297 && TREE_CODE (gnu_record_type) == UNION_TYPE
6298 && !TYPE_PACKED (gnu_record_type))
6299 gnu_union_type = gnu_record_type;
6300 else
6302 gnu_union_type
6303 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6305 TYPE_NAME (gnu_union_type) = gnu_union_name;
6306 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6309 for (variant = First_Non_Pragma (Variants (variant_part));
6310 Present (variant);
6311 variant = Next_Non_Pragma (variant))
6313 tree gnu_variant_type = make_node (RECORD_TYPE);
6314 tree gnu_inner_name;
6315 tree gnu_qual;
6317 Get_Variant_Encoding (variant);
6318 gnu_inner_name = get_identifier (Name_Buffer);
6319 TYPE_NAME (gnu_variant_type)
6320 = concat_id_with_name (gnu_union_name,
6321 IDENTIFIER_POINTER (gnu_inner_name));
6323 /* Set the alignment of the inner type in case we need to make
6324 inner objects into bitfields, but then clear it out
6325 so the record actually gets only the alignment required. */
6326 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6327 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6329 /* Similarly, if the outer record has a size specified and all fields
6330 have record rep clauses, we can propagate the size into the
6331 variant part. */
6332 if (all_rep_and_size)
6334 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6335 TYPE_SIZE_UNIT (gnu_variant_type)
6336 = TYPE_SIZE_UNIT (gnu_record_type);
6339 /* Create the record type for the variant. Note that we defer
6340 finalizing it until after we are sure to actually use it. */
6341 components_to_record (gnu_variant_type, Component_List (variant),
6342 NULL_TREE, packed, definition,
6343 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6344 true, unchecked_union);
6346 gnu_qual = choices_to_gnu (gnu_discriminant,
6347 Discrete_Choices (variant));
6349 Set_Present_Expr (variant, annotate_value (gnu_qual));
6351 /* If this is an Unchecked_Union and we have exactly one field,
6352 use this field directly to match the layout of C unions. */
6353 if (unchecked_union
6354 && TYPE_FIELDS (gnu_variant_type)
6355 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6356 gnu_field = TYPE_FIELDS (gnu_variant_type);
6357 else
6359 /* Deal with packedness like in gnat_to_gnu_field. */
6360 int field_packed
6361 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6363 /* Finalize the record type now. We used to throw away
6364 empty records but we no longer do that because we need
6365 them to generate complete debug info for the variant;
6366 otherwise, the union type definition will be lacking
6367 the fields associated with these empty variants. */
6368 rest_of_record_type_compilation (gnu_variant_type);
6370 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6371 gnu_union_type, field_packed,
6372 (all_rep_and_size
6373 ? TYPE_SIZE (gnu_variant_type)
6374 : 0),
6375 (all_rep_and_size
6376 ? bitsize_zero_node : 0),
6379 DECL_INTERNAL_P (gnu_field) = 1;
6381 if (!unchecked_union)
6382 DECL_QUALIFIER (gnu_field) = gnu_qual;
6385 TREE_CHAIN (gnu_field) = gnu_variant_list;
6386 gnu_variant_list = gnu_field;
6389 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
6390 if (gnu_variant_list)
6392 if (all_rep_and_size)
6394 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6395 TYPE_SIZE_UNIT (gnu_union_type)
6396 = TYPE_SIZE_UNIT (gnu_record_type);
6399 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6400 all_rep_and_size ? 1 : 0, false);
6402 /* If GNU_UNION_TYPE is our record type, it means we must have an
6403 Unchecked_Union with no fields. Verify that and, if so, just
6404 return. */
6405 if (gnu_union_type == gnu_record_type)
6407 gcc_assert (unchecked_union
6408 && !gnu_field_list
6409 && !gnu_our_rep_list);
6410 return;
6413 gnu_union_field
6414 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6415 packed,
6416 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6417 all_rep ? bitsize_zero_node : 0, 0);
6419 DECL_INTERNAL_P (gnu_union_field) = 1;
6420 TREE_CHAIN (gnu_union_field) = gnu_field_list;
6421 gnu_field_list = gnu_union_field;
6425 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
6426 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
6427 in a separate pass since we want to handle the discriminants but can't
6428 play with them until we've used them in debugging data above.
6430 ??? Note: if we then reorder them, debugging information will be wrong,
6431 but there's nothing that can be done about this at the moment. */
6432 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6434 if (DECL_FIELD_OFFSET (gnu_field))
6436 tree gnu_next = TREE_CHAIN (gnu_field);
6438 if (!gnu_last)
6439 gnu_field_list = gnu_next;
6440 else
6441 TREE_CHAIN (gnu_last) = gnu_next;
6443 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6444 gnu_our_rep_list = gnu_field;
6445 gnu_field = gnu_next;
6447 else
6449 gnu_last = gnu_field;
6450 gnu_field = TREE_CHAIN (gnu_field);
6454 /* If we have any items in our rep'ed field list, it is not the case that all
6455 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6456 set it and ignore the items. */
6457 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6458 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6459 else if (gnu_our_rep_list)
6461 /* Otherwise, sort the fields by bit position and put them into their
6462 own record if we have any fields without rep clauses. */
6463 tree gnu_rep_type
6464 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6465 int len = list_length (gnu_our_rep_list);
6466 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6467 int i;
6469 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6470 gnu_field = TREE_CHAIN (gnu_field), i++)
6471 gnu_arr[i] = gnu_field;
6473 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6475 /* Put the fields in the list in order of increasing position, which
6476 means we start from the end. */
6477 gnu_our_rep_list = NULL_TREE;
6478 for (i = len - 1; i >= 0; i--)
6480 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6481 gnu_our_rep_list = gnu_arr[i];
6482 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6485 if (gnu_field_list)
6487 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6488 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6489 gnu_record_type, 0, 0, 0, 1);
6490 DECL_INTERNAL_P (gnu_field) = 1;
6491 gnu_field_list = chainon (gnu_field_list, gnu_field);
6493 else
6495 layout_with_rep = true;
6496 gnu_field_list = nreverse (gnu_our_rep_list);
6500 if (cancel_alignment)
6501 TYPE_ALIGN (gnu_record_type) = 0;
6503 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6504 layout_with_rep ? 1 : 0, do_not_finalize);
6507 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6508 placed into an Esize, Component_Bit_Offset, or Component_Size value
6509 in the GNAT tree. */
6511 static Uint
6512 annotate_value (tree gnu_size)
6514 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6515 TCode tcode;
6516 Node_Ref_Or_Val ops[3], ret;
6517 int i;
6518 int size;
6519 struct tree_int_map **h = NULL;
6521 /* See if we've already saved the value for this node. */
6522 if (EXPR_P (gnu_size))
6524 struct tree_int_map in;
6525 if (!annotate_value_cache)
6526 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6527 tree_int_map_eq, 0);
6528 in.base.from = gnu_size;
6529 h = (struct tree_int_map **)
6530 htab_find_slot (annotate_value_cache, &in, INSERT);
6532 if (*h)
6533 return (Node_Ref_Or_Val) (*h)->to;
6536 /* If we do not return inside this switch, TCODE will be set to the
6537 code to use for a Create_Node operand and LEN (set above) will be
6538 the number of recursive calls for us to make. */
6540 switch (TREE_CODE (gnu_size))
6542 case INTEGER_CST:
6543 if (TREE_OVERFLOW (gnu_size))
6544 return No_Uint;
6546 /* This may have come from a conversion from some smaller type,
6547 so ensure this is in bitsizetype. */
6548 gnu_size = convert (bitsizetype, gnu_size);
6550 /* For negative values, use NEGATE_EXPR of the supplied value. */
6551 if (tree_int_cst_sgn (gnu_size) < 0)
6553 /* The ridiculous code below is to handle the case of the largest
6554 negative integer. */
6555 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6556 bool adjust = false;
6557 tree temp;
6559 if (TREE_OVERFLOW (negative_size))
6561 negative_size
6562 = size_binop (MINUS_EXPR, bitsize_zero_node,
6563 size_binop (PLUS_EXPR, gnu_size,
6564 bitsize_one_node));
6565 adjust = true;
6568 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
6569 if (adjust)
6570 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
6572 return annotate_value (temp);
6575 if (!host_integerp (gnu_size, 1))
6576 return No_Uint;
6578 size = tree_low_cst (gnu_size, 1);
6580 /* This peculiar test is to make sure that the size fits in an int
6581 on machines where HOST_WIDE_INT is not "int". */
6582 if (tree_low_cst (gnu_size, 1) == size)
6583 return UI_From_Int (size);
6584 else
6585 return No_Uint;
6587 case COMPONENT_REF:
6588 /* The only case we handle here is a simple discriminant reference. */
6589 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
6590 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
6591 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
6592 return Create_Node (Discrim_Val,
6593 annotate_value (DECL_DISCRIMINANT_NUMBER
6594 (TREE_OPERAND (gnu_size, 1))),
6595 No_Uint, No_Uint);
6596 else
6597 return No_Uint;
6599 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
6600 return annotate_value (TREE_OPERAND (gnu_size, 0));
6602 /* Now just list the operations we handle. */
6603 case COND_EXPR: tcode = Cond_Expr; break;
6604 case PLUS_EXPR: tcode = Plus_Expr; break;
6605 case MINUS_EXPR: tcode = Minus_Expr; break;
6606 case MULT_EXPR: tcode = Mult_Expr; break;
6607 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
6608 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
6609 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
6610 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
6611 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
6612 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
6613 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
6614 case NEGATE_EXPR: tcode = Negate_Expr; break;
6615 case MIN_EXPR: tcode = Min_Expr; break;
6616 case MAX_EXPR: tcode = Max_Expr; break;
6617 case ABS_EXPR: tcode = Abs_Expr; break;
6618 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
6619 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
6620 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
6621 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
6622 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
6623 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
6624 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
6625 case LT_EXPR: tcode = Lt_Expr; break;
6626 case LE_EXPR: tcode = Le_Expr; break;
6627 case GT_EXPR: tcode = Gt_Expr; break;
6628 case GE_EXPR: tcode = Ge_Expr; break;
6629 case EQ_EXPR: tcode = Eq_Expr; break;
6630 case NE_EXPR: tcode = Ne_Expr; break;
6632 default:
6633 return No_Uint;
6636 /* Now get each of the operands that's relevant for this code. If any
6637 cannot be expressed as a repinfo node, say we can't. */
6638 for (i = 0; i < 3; i++)
6639 ops[i] = No_Uint;
6641 for (i = 0; i < len; i++)
6643 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6644 if (ops[i] == No_Uint)
6645 return No_Uint;
6648 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6650 /* Save the result in the cache. */
6651 if (h)
6653 *h = ggc_alloc (sizeof (struct tree_int_map));
6654 (*h)->base.from = gnu_size;
6655 (*h)->to = ret;
6658 return ret;
6661 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6662 GCC type, set Component_Bit_Offset and Esize to the position and size
6663 used by Gigi. */
6665 static void
6666 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6668 tree gnu_list;
6669 tree gnu_entry;
6670 Entity_Id gnat_field;
6672 /* We operate by first making a list of all fields and their positions
6673 (we can get the sizes easily at any time) by a recursive call
6674 and then update all the sizes into the tree. */
6675 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6676 size_zero_node, bitsize_zero_node,
6677 BIGGEST_ALIGNMENT);
6679 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6680 gnat_field = Next_Entity (gnat_field))
6681 if ((Ekind (gnat_field) == E_Component
6682 || (Ekind (gnat_field) == E_Discriminant
6683 && !Is_Unchecked_Union (Scope (gnat_field)))))
6685 tree parent_offset = bitsize_zero_node;
6687 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6688 gnu_list);
6690 if (gnu_entry)
6692 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6694 /* In this mode the tag and parent components have not been
6695 generated, so we add the appropriate offset to each
6696 component. For a component appearing in the current
6697 extension, the offset is the size of the parent. */
6698 if (Is_Derived_Type (gnat_entity)
6699 && Original_Record_Component (gnat_field) == gnat_field)
6700 parent_offset
6701 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6702 bitsizetype);
6703 else
6704 parent_offset = bitsize_int (POINTER_SIZE);
6707 Set_Component_Bit_Offset
6708 (gnat_field,
6709 annotate_value
6710 (size_binop (PLUS_EXPR,
6711 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6712 TREE_VALUE (TREE_VALUE
6713 (TREE_VALUE (gnu_entry)))),
6714 parent_offset)));
6716 Set_Esize (gnat_field,
6717 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6719 else if (Is_Tagged_Type (gnat_entity)
6720 && Is_Derived_Type (gnat_entity))
6722 /* If there is no gnu_entry, this is an inherited component whose
6723 position is the same as in the parent type. */
6724 Set_Component_Bit_Offset
6725 (gnat_field,
6726 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6727 Set_Esize (gnat_field,
6728 Esize (Original_Record_Component (gnat_field)));
6733 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6734 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6735 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6736 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6737 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6738 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6739 so far. */
6741 static tree
6742 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6743 tree gnu_bitpos, unsigned int offset_align)
6745 tree gnu_field;
6746 tree gnu_result = gnu_list;
6748 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6749 gnu_field = TREE_CHAIN (gnu_field))
6751 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6752 DECL_FIELD_BIT_OFFSET (gnu_field));
6753 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6754 DECL_FIELD_OFFSET (gnu_field));
6755 unsigned int our_offset_align
6756 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6758 gnu_result
6759 = tree_cons (gnu_field,
6760 tree_cons (gnu_our_offset,
6761 tree_cons (size_int (our_offset_align),
6762 gnu_our_bitpos, NULL_TREE),
6763 NULL_TREE),
6764 gnu_result);
6766 if (DECL_INTERNAL_P (gnu_field))
6767 gnu_result
6768 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6769 gnu_our_offset, gnu_our_bitpos,
6770 our_offset_align);
6773 return gnu_result;
6776 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6777 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6778 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6779 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6780 for the size of a field. COMPONENT_P is true if we are being called
6781 to process the Component_Size of GNAT_OBJECT. This is used for error
6782 message handling and to indicate to use the object size of GNU_TYPE.
6783 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6784 it means that a size of zero should be treated as an unspecified size. */
6786 static tree
6787 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6788 enum tree_code kind, bool component_p, bool zero_ok)
6790 Node_Id gnat_error_node;
6791 tree type_size, size;
6793 if (kind == VAR_DECL
6794 /* If a type needs strict alignment, a component of this type in
6795 a packed record cannot be packed and thus uses the type size. */
6796 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
6797 type_size = TYPE_SIZE (gnu_type);
6798 else
6799 type_size = rm_size (gnu_type);
6801 /* Find the node to use for errors. */
6802 if ((Ekind (gnat_object) == E_Component
6803 || Ekind (gnat_object) == E_Discriminant)
6804 && Present (Component_Clause (gnat_object)))
6805 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6806 else if (Present (Size_Clause (gnat_object)))
6807 gnat_error_node = Expression (Size_Clause (gnat_object));
6808 else
6809 gnat_error_node = gnat_object;
6811 /* Return 0 if no size was specified, either because Esize was not Present or
6812 the specified size was zero. */
6813 if (No (uint_size) || uint_size == No_Uint)
6814 return NULL_TREE;
6816 /* Get the size as a tree. Give an error if a size was specified, but cannot
6817 be represented as in sizetype. */
6818 size = UI_To_gnu (uint_size, bitsizetype);
6819 if (TREE_OVERFLOW (size))
6821 post_error_ne (component_p ? "component size of & is too large"
6822 : "size of & is too large",
6823 gnat_error_node, gnat_object);
6824 return NULL_TREE;
6827 /* Ignore a negative size since that corresponds to our back-annotation.
6828 Also ignore a zero size unless a size clause exists. */
6829 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6830 return NULL_TREE;
6832 /* The size of objects is always a multiple of a byte. */
6833 if (kind == VAR_DECL
6834 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6836 if (component_p)
6837 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6838 gnat_error_node, gnat_object);
6839 else
6840 post_error_ne ("size for& is not a multiple of Storage_Unit",
6841 gnat_error_node, gnat_object);
6842 return NULL_TREE;
6845 /* If this is an integral type or a packed array type, the front-end has
6846 verified the size, so we need not do it here (which would entail
6847 checking against the bounds). However, if this is an aliased object, it
6848 may not be smaller than the type of the object. */
6849 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6850 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6851 return size;
6853 /* If the object is a record that contains a template, add the size of
6854 the template to the specified size. */
6855 if (TREE_CODE (gnu_type) == RECORD_TYPE
6856 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6857 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6859 /* Modify the size of the type to be that of the maximum size if it has a
6860 discriminant. */
6861 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6862 type_size = max_size (type_size, true);
6864 /* If this is an access type or a fat pointer, the minimum size is that given
6865 by the smallest integral mode that's valid for pointers. */
6866 if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
6868 enum machine_mode p_mode;
6870 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6871 !targetm.valid_pointer_mode (p_mode);
6872 p_mode = GET_MODE_WIDER_MODE (p_mode))
6875 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6878 /* If the size of the object is a constant, the new size must not be
6879 smaller. */
6880 if (TREE_CODE (type_size) != INTEGER_CST
6881 || TREE_OVERFLOW (type_size)
6882 || tree_int_cst_lt (size, type_size))
6884 if (component_p)
6885 post_error_ne_tree
6886 ("component size for& too small{, minimum allowed is ^}",
6887 gnat_error_node, gnat_object, type_size);
6888 else
6889 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6890 gnat_error_node, gnat_object, type_size);
6892 if (kind == VAR_DECL && !component_p
6893 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6894 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6895 post_error_ne_tree_2
6896 ("\\size of ^ is not a multiple of alignment (^ bits)",
6897 gnat_error_node, gnat_object, rm_size (gnu_type),
6898 TYPE_ALIGN (gnu_type));
6900 else if (INTEGRAL_TYPE_P (gnu_type))
6901 post_error_ne ("\\size would be legal if & were not aliased!",
6902 gnat_error_node, gnat_object);
6904 return NULL_TREE;
6907 return size;
6910 /* Similarly, but both validate and process a value of RM_Size. This
6911 routine is only called for types. */
6913 static void
6914 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6916 /* Only give an error if a Value_Size clause was explicitly given.
6917 Otherwise, we'd be duplicating an error on the Size clause. */
6918 Node_Id gnat_attr_node
6919 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6920 tree old_size = rm_size (gnu_type);
6921 tree size;
6923 /* Get the size as a tree. Do nothing if none was specified, either
6924 because RM_Size was not Present or if the specified size was zero.
6925 Give an error if a size was specified, but cannot be represented as
6926 in sizetype. */
6927 if (No (uint_size) || uint_size == No_Uint)
6928 return;
6930 size = UI_To_gnu (uint_size, bitsizetype);
6931 if (TREE_OVERFLOW (size))
6933 if (Present (gnat_attr_node))
6934 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6935 gnat_entity);
6937 return;
6940 /* Ignore a negative size since that corresponds to our back-annotation.
6941 Also ignore a zero size unless a size clause exists, a Value_Size
6942 clause exists, or this is an integer type, in which case the
6943 front end will have always set it. */
6944 else if (tree_int_cst_sgn (size) < 0
6945 || (integer_zerop (size) && No (gnat_attr_node)
6946 && !Has_Size_Clause (gnat_entity)
6947 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6948 return;
6950 /* If the old size is self-referential, get the maximum size. */
6951 if (CONTAINS_PLACEHOLDER_P (old_size))
6952 old_size = max_size (old_size, true);
6954 /* If the size of the object is a constant, the new size must not be
6955 smaller (the front end checks this for scalar types). */
6956 if (TREE_CODE (old_size) != INTEGER_CST
6957 || TREE_OVERFLOW (old_size)
6958 || (AGGREGATE_TYPE_P (gnu_type)
6959 && tree_int_cst_lt (size, old_size)))
6961 if (Present (gnat_attr_node))
6962 post_error_ne_tree
6963 ("Value_Size for& too small{, minimum allowed is ^}",
6964 gnat_attr_node, gnat_entity, old_size);
6966 return;
6969 /* Otherwise, set the RM_Size. */
6970 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6971 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6972 TYPE_RM_SIZE_NUM (gnu_type) = size;
6973 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6974 TYPE_RM_SIZE_NUM (gnu_type) = size;
6975 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6976 || TREE_CODE (gnu_type) == UNION_TYPE
6977 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6978 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6979 SET_TYPE_ADA_SIZE (gnu_type, size);
6982 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6983 If TYPE is the best type, return it. Otherwise, make a new type. We
6984 only support new integral and pointer types. BIASED_P is nonzero if
6985 we are making a biased type. */
6987 static tree
6988 make_type_from_size (tree type, tree size_tree, bool biased_p)
6990 tree new_type;
6991 unsigned HOST_WIDE_INT size;
6992 bool unsigned_p;
6994 /* If size indicates an error, just return TYPE to avoid propagating the
6995 error. Likewise if it's too large to represent. */
6996 if (!size_tree || !host_integerp (size_tree, 1))
6997 return type;
6999 size = tree_low_cst (size_tree, 1);
7000 switch (TREE_CODE (type))
7002 case INTEGER_TYPE:
7003 case ENUMERAL_TYPE:
7004 /* Only do something if the type is not already the proper size and is
7005 not a packed array type. */
7006 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7007 || (TYPE_PRECISION (type) == size
7008 && biased_p == (TREE_CODE (type) == INTEGER_CST
7009 && TYPE_BIASED_REPRESENTATION_P (type))))
7010 break;
7012 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
7013 && TYPE_BIASED_REPRESENTATION_P (type));
7014 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
7016 size = MIN (size, LONG_LONG_TYPE_SIZE);
7017 new_type
7018 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
7019 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7020 TYPE_MIN_VALUE (new_type)
7021 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
7022 TYPE_MAX_VALUE (new_type)
7023 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
7024 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7025 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
7026 return new_type;
7028 case RECORD_TYPE:
7029 /* Do something if this is a fat pointer, in which case we
7030 may need to return the thin pointer. */
7031 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7032 return
7033 build_pointer_type
7034 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
7035 break;
7037 case POINTER_TYPE:
7038 /* Only do something if this is a thin pointer, in which case we
7039 may need to return the fat pointer. */
7040 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7041 return
7042 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7044 break;
7046 default:
7047 break;
7050 return type;
7053 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7054 a type or object whose present alignment is ALIGN. If this alignment is
7055 valid, return it. Otherwise, give an error and return ALIGN. */
7057 static unsigned int
7058 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7060 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7061 unsigned int new_align;
7062 Node_Id gnat_error_node;
7064 /* Don't worry about checking alignment if alignment was not specified
7065 by the source program and we already posted an error for this entity. */
7066 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7067 return align;
7069 /* Post the error on the alignment clause if any. */
7070 if (Present (Alignment_Clause (gnat_entity)))
7071 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7072 else
7073 gnat_error_node = gnat_entity;
7075 /* Within GCC, an alignment is an integer, so we must make sure a value is
7076 specified that fits in that range. Also, there is an upper bound to
7077 alignments we can support/allow. */
7078 if (!UI_Is_In_Int_Range (alignment)
7079 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7080 post_error_ne_num ("largest supported alignment for& is ^",
7081 gnat_error_node, gnat_entity, max_allowed_alignment);
7082 else if (!(Present (Alignment_Clause (gnat_entity))
7083 && From_At_Mod (Alignment_Clause (gnat_entity)))
7084 && new_align * BITS_PER_UNIT < align)
7085 post_error_ne_num ("alignment for& must be at least ^",
7086 gnat_error_node, gnat_entity,
7087 align / BITS_PER_UNIT);
7088 else
7090 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7091 if (new_align > align)
7092 align = new_align;
7095 return align;
7098 /* Return the smallest alignment not less than SIZE. */
7100 static unsigned int
7101 ceil_alignment (unsigned HOST_WIDE_INT size)
7103 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7106 /* Verify that OBJECT, a type or decl, is something we can implement
7107 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7108 if we require atomic components. */
7110 static void
7111 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7113 Node_Id gnat_error_point = gnat_entity;
7114 Node_Id gnat_node;
7115 enum machine_mode mode;
7116 unsigned int align;
7117 tree size;
7119 /* There are three case of what OBJECT can be. It can be a type, in which
7120 case we take the size, alignment and mode from the type. It can be a
7121 declaration that was indirect, in which case the relevant values are
7122 that of the type being pointed to, or it can be a normal declaration,
7123 in which case the values are of the decl. The code below assumes that
7124 OBJECT is either a type or a decl. */
7125 if (TYPE_P (object))
7127 mode = TYPE_MODE (object);
7128 align = TYPE_ALIGN (object);
7129 size = TYPE_SIZE (object);
7131 else if (DECL_BY_REF_P (object))
7133 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7134 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7135 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7137 else
7139 mode = DECL_MODE (object);
7140 align = DECL_ALIGN (object);
7141 size = DECL_SIZE (object);
7144 /* Consider all floating-point types atomic and any types that that are
7145 represented by integers no wider than a machine word. */
7146 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7147 || ((GET_MODE_CLASS (mode) == MODE_INT
7148 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7149 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7150 return;
7152 /* For the moment, also allow anything that has an alignment equal
7153 to its size and which is smaller than a word. */
7154 if (size && TREE_CODE (size) == INTEGER_CST
7155 && compare_tree_int (size, align) == 0
7156 && align <= BITS_PER_WORD)
7157 return;
7159 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7160 gnat_node = Next_Rep_Item (gnat_node))
7162 if (!comp_p && Nkind (gnat_node) == N_Pragma
7163 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7164 == Pragma_Atomic))
7165 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7166 else if (comp_p && Nkind (gnat_node) == N_Pragma
7167 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7168 == Pragma_Atomic_Components))
7169 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7172 if (comp_p)
7173 post_error_ne ("atomic access to component of & cannot be guaranteed",
7174 gnat_error_point, gnat_entity);
7175 else
7176 post_error_ne ("atomic access to & cannot be guaranteed",
7177 gnat_error_point, gnat_entity);
7180 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7181 have compatible signatures so that a call using one type may be safely
7182 issued if the actual target function type is the other. Return 1 if it is
7183 the case, 0 otherwise, and post errors on the incompatibilities.
7185 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7186 that calls to the subprogram will have arguments suitable for the later
7187 underlying builtin expansion. */
7189 static int
7190 compatible_signatures_p (tree ftype1, tree ftype2)
7192 /* As of now, we only perform very trivial tests and consider it's the
7193 programmer's responsibility to ensure the type correctness in the Ada
7194 declaration, as in the regular Import cases.
7196 Mismatches typically result in either error messages from the builtin
7197 expander, internal compiler errors, or in a real call sequence. This
7198 should be refined to issue diagnostics helping error detection and
7199 correction. */
7201 /* Almost fake test, ensuring a use of each argument. */
7202 if (ftype1 == ftype2)
7203 return 1;
7205 return 1;
7208 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
7209 type with all size expressions that contain F updated by replacing F
7210 with R. If F is NULL_TREE, always make a new RECORD_TYPE, even if
7211 nothing has changed. */
7213 tree
7214 substitute_in_type (tree t, tree f, tree r)
7216 tree new = t;
7217 tree tem;
7219 switch (TREE_CODE (t))
7221 case INTEGER_TYPE:
7222 case ENUMERAL_TYPE:
7223 case BOOLEAN_TYPE:
7224 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7225 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7227 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7228 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7230 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7231 return t;
7233 new = build_range_type (TREE_TYPE (t), low, high);
7234 if (TYPE_INDEX_TYPE (t))
7235 SET_TYPE_INDEX_TYPE
7236 (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7237 return new;
7240 return t;
7242 case REAL_TYPE:
7243 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7244 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7246 tree low = NULL_TREE, high = NULL_TREE;
7248 if (TYPE_MIN_VALUE (t))
7249 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7250 if (TYPE_MAX_VALUE (t))
7251 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7253 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7254 return t;
7256 t = copy_type (t);
7257 TYPE_MIN_VALUE (t) = low;
7258 TYPE_MAX_VALUE (t) = high;
7260 return t;
7262 case COMPLEX_TYPE:
7263 tem = substitute_in_type (TREE_TYPE (t), f, r);
7264 if (tem == TREE_TYPE (t))
7265 return t;
7267 return build_complex_type (tem);
7269 case OFFSET_TYPE:
7270 case METHOD_TYPE:
7271 case FUNCTION_TYPE:
7272 case LANG_TYPE:
7273 /* Don't know how to do these yet. */
7274 gcc_unreachable ();
7276 case ARRAY_TYPE:
7278 tree component = substitute_in_type (TREE_TYPE (t), f, r);
7279 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7281 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7282 return t;
7284 new = build_array_type (component, domain);
7285 TYPE_SIZE (new) = 0;
7286 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7287 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7288 layout_type (new);
7289 TYPE_ALIGN (new) = TYPE_ALIGN (t);
7290 TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7292 /* If we had bounded the sizes of T by a constant, bound the sizes of
7293 NEW by the same constant. */
7294 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
7295 TYPE_SIZE (new)
7296 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
7297 TYPE_SIZE (new));
7298 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
7299 TYPE_SIZE_UNIT (new)
7300 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
7301 TYPE_SIZE_UNIT (new));
7302 return new;
7305 case RECORD_TYPE:
7306 case UNION_TYPE:
7307 case QUAL_UNION_TYPE:
7309 tree field;
7310 bool changed_field
7311 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
7312 bool field_has_rep = false;
7313 tree last_field = NULL_TREE;
7315 tree new = copy_type (t);
7317 /* Start out with no fields, make new fields, and chain them
7318 in. If we haven't actually changed the type of any field,
7319 discard everything we've done and return the old type. */
7321 TYPE_FIELDS (new) = NULL_TREE;
7322 TYPE_SIZE (new) = NULL_TREE;
7324 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7326 tree new_field = copy_node (field);
7328 TREE_TYPE (new_field)
7329 = substitute_in_type (TREE_TYPE (new_field), f, r);
7331 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
7332 field_has_rep = true;
7333 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
7334 changed_field = true;
7336 /* If this is an internal field and the type of this field is
7337 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
7338 the type just has one element, treat that as the field.
7339 But don't do this if we are processing a QUAL_UNION_TYPE. */
7340 if (TREE_CODE (t) != QUAL_UNION_TYPE
7341 && DECL_INTERNAL_P (new_field)
7342 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
7343 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
7345 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
7346 continue;
7348 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
7350 tree next_new_field
7351 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
7353 /* Make sure omitting the union doesn't change
7354 the layout. */
7355 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
7356 new_field = next_new_field;
7360 DECL_CONTEXT (new_field) = new;
7361 SET_DECL_ORIGINAL_FIELD (new_field,
7362 (DECL_ORIGINAL_FIELD (field)
7363 ? DECL_ORIGINAL_FIELD (field) : field));
7365 /* If the size of the old field was set at a constant,
7366 propagate the size in case the type's size was variable.
7367 (This occurs in the case of a variant or discriminated
7368 record with a default size used as a field of another
7369 record.) */
7370 DECL_SIZE (new_field)
7371 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
7372 ? DECL_SIZE (field) : NULL_TREE;
7373 DECL_SIZE_UNIT (new_field)
7374 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
7375 ? DECL_SIZE_UNIT (field) : NULL_TREE;
7377 if (TREE_CODE (t) == QUAL_UNION_TYPE)
7379 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7381 if (new_q != DECL_QUALIFIER (new_field))
7382 changed_field = true;
7384 /* Do the substitution inside the qualifier and if we find
7385 that this field will not be present, omit it. */
7386 DECL_QUALIFIER (new_field) = new_q;
7388 if (integer_zerop (DECL_QUALIFIER (new_field)))
7389 continue;
7392 if (!last_field)
7393 TYPE_FIELDS (new) = new_field;
7394 else
7395 TREE_CHAIN (last_field) = new_field;
7397 last_field = new_field;
7399 /* If this is a qualified type and this field will always be
7400 present, we are done. */
7401 if (TREE_CODE (t) == QUAL_UNION_TYPE
7402 && integer_onep (DECL_QUALIFIER (new_field)))
7403 break;
7406 /* If this used to be a qualified union type, but we now know what
7407 field will be present, make this a normal union. */
7408 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
7409 && (!TYPE_FIELDS (new)
7410 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
7411 TREE_SET_CODE (new, UNION_TYPE);
7412 else if (!changed_field)
7413 return t;
7415 gcc_assert (!field_has_rep);
7416 layout_type (new);
7418 /* If the size was originally a constant use it. */
7419 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
7420 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
7422 TYPE_SIZE (new) = TYPE_SIZE (t);
7423 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
7424 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
7427 return new;
7430 default:
7431 return t;
7435 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
7436 needed to represent the object. */
7438 tree
7439 rm_size (tree gnu_type)
7441 /* For integer types, this is the precision. For record types, we store
7442 the size explicitly. For other types, this is just the size. */
7444 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7445 return TYPE_RM_SIZE (gnu_type);
7446 else if (TREE_CODE (gnu_type) == RECORD_TYPE
7447 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7448 /* Return the rm_size of the actual data plus the size of the template. */
7449 return
7450 size_binop (PLUS_EXPR,
7451 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7452 DECL_SIZE (TYPE_FIELDS (gnu_type)));
7453 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7454 || TREE_CODE (gnu_type) == UNION_TYPE
7455 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7456 && !TYPE_IS_FAT_POINTER_P (gnu_type)
7457 && TYPE_ADA_SIZE (gnu_type))
7458 return TYPE_ADA_SIZE (gnu_type);
7459 else
7460 return TYPE_SIZE (gnu_type);
7463 /* Return an identifier representing the external name to be used for
7464 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
7465 and the specified suffix. */
7467 tree
7468 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7470 Entity_Kind kind = Ekind (gnat_entity);
7472 const char *str = (!suffix ? "" : suffix);
7473 String_Template temp = {1, strlen (str)};
7474 Fat_Pointer fp = {str, &temp};
7476 Get_External_Name_With_Suffix (gnat_entity, fp);
7478 /* A variable using the Stdcall convention (meaning we are running
7479 on a Windows box) live in a DLL. Here we adjust its name to use
7480 the jump-table, the _imp__NAME contains the address for the NAME
7481 variable. */
7482 if ((kind == E_Variable || kind == E_Constant)
7483 && Has_Stdcall_Convention (gnat_entity))
7485 const char *prefix = "_imp__";
7486 int k, plen = strlen (prefix);
7488 for (k = 0; k <= Name_Len; k++)
7489 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
7490 strncpy (Name_Buffer, prefix, plen);
7493 return get_identifier (Name_Buffer);
7496 /* Return the name to be used for GNAT_ENTITY. If a type, create a
7497 fully-qualified name, possibly with type information encoding.
7498 Otherwise, return the name. */
7500 tree
7501 get_entity_name (Entity_Id gnat_entity)
7503 Get_Encoded_Name (gnat_entity);
7504 return get_identifier (Name_Buffer);
7507 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
7508 string, return a new IDENTIFIER_NODE that is the concatenation of
7509 the name in GNU_ID and SUFFIX. */
7511 tree
7512 concat_id_with_name (tree gnu_id, const char *suffix)
7514 int len = IDENTIFIER_LENGTH (gnu_id);
7516 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
7517 strncpy (Name_Buffer + len, "___", 3);
7518 len += 3;
7519 strcpy (Name_Buffer + len, suffix);
7520 return get_identifier (Name_Buffer);
7523 #include "gt-ada-decl.h"