1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2010, Free Software Foundation, Inc. *
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/>. *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 ****************************************************************************/
28 #include "coretypes.h"
36 #include "tree-inline.h"
54 #ifndef MAX_FIXED_MODE_SIZE
55 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 /* Convention_Stdcall should be processed in a specific way on Windows targets
59 only. The macro below is a helper to avoid having to check for a Windows
60 specific attribute throughout this unit. */
62 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
63 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #define Has_Stdcall_Convention(E) (0)
68 /* Stack realignment for functions with foreign conventions is provided on a
69 per back-end basis now, as it is handled by the prologue expanders and not
70 as part of the function's body any more. It might be requested by way of a
71 dedicated function type attribute on the targets that support it.
73 We need a way to avoid setting the attribute on the targets that don't
74 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
76 It is defined on targets where the circuitry is available, and indicates
77 whether the realignment is needed for 'main'. We use this to decide for
78 foreign subprograms as well.
80 It is not defined on targets where the circuitry is not implemented, and
81 we just never set the attribute in these cases.
83 Whether it is defined on all targets that would need it in theory is
84 not entirely clear. We currently trust the base GCC settings for this
87 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
88 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
93 struct incomplete *next;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_With_Type types until the
105 static struct incomplete *defer_limited_with;
107 /* These variables are used to defer finalizing types. The element of the
108 list is the TYPE_DECL associated with the type. */
109 static int defer_finalize_level = 0;
110 static VEC (tree,heap) *defer_finalize_list;
112 /* A hash table used to cache the result of annotate_value. */
113 static GTY ((if_marked ("tree_int_map_marked_p"),
114 param_is (struct tree_int_map))) htab_t annotate_value_cache;
123 static void relate_alias_sets (tree, tree, enum alias_set_op);
125 static bool allocatable_size_p (tree, bool);
126 static void prepend_one_attribute_to (struct attrib **,
127 enum attr_type, tree, tree, Node_Id);
128 static void prepend_attributes (Entity_Id, struct attrib **);
129 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
130 static bool is_variable_size (tree);
131 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
132 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
134 static tree make_packable_type (tree, bool);
135 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
136 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
138 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
139 static bool same_discriminant_p (Entity_Id, Entity_Id);
140 static bool array_type_has_nonaliased_component (tree, Entity_Id);
141 static bool compile_time_known_address_p (Node_Id);
142 static bool cannot_be_superflat_p (Node_Id);
143 static bool constructor_address_p (tree);
144 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
145 bool, bool, bool, bool, bool);
146 static Uint annotate_value (tree);
147 static void annotate_rep (Entity_Id, tree);
148 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
149 static tree build_subst_list (Entity_Id, Entity_Id, bool);
150 static tree build_variant_list (tree, tree, tree);
151 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
152 static void set_rm_size (Uint, tree, Entity_Id);
153 static tree make_type_from_size (tree, tree, bool);
154 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
155 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
156 static void check_ok_for_atomic (tree, Entity_Id, bool);
157 static int compatible_signatures_p (tree, tree);
158 static tree create_field_decl_from (tree, tree, tree, tree, tree, tree);
159 static tree get_rep_part (tree);
160 static tree get_variant_part (tree);
161 static tree create_variant_part_from (tree, tree, tree, tree, tree);
162 static void copy_and_substitute_in_size (tree, tree, tree);
163 static void rest_of_type_decl_compilation_no_defer (tree);
165 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
166 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
167 and associate the ..._DECL node with the input GNAT defining identifier.
169 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
170 initial value (in GCC tree form). This is optional for a variable. For
171 a renamed entity, GNU_EXPR gives the object being renamed.
173 DEFINITION is nonzero if this call is intended for a definition. This is
174 used for separate compilation where it is necessary to know whether an
175 external declaration or a definition must be created if the GCC equivalent
176 was not created previously. The value of 1 is normally used for a nonzero
177 DEFINITION, but a value of 2 is used in special circumstances, defined in
181 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
183 /* Contains the kind of the input GNAT node. */
184 const Entity_Kind kind = Ekind (gnat_entity);
185 /* True if this is a type. */
186 const bool is_type = IN (kind, Type_Kind);
187 /* True if debug info is requested for this entity. */
188 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
189 /* True if this entity is to be considered as imported. */
190 const bool imported_p
191 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
192 /* For a type, contains the equivalent GNAT node to be used in gigi. */
193 Entity_Id gnat_equiv_type = Empty;
194 /* Temporary used to walk the GNAT tree. */
196 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
197 This node will be associated with the GNAT node by calling at the end
198 of the `switch' statement. */
199 tree gnu_decl = NULL_TREE;
200 /* Contains the GCC type to be used for the GCC node. */
201 tree gnu_type = NULL_TREE;
202 /* Contains the GCC size tree to be used for the GCC node. */
203 tree gnu_size = NULL_TREE;
204 /* Contains the GCC name to be used for the GCC node. */
205 tree gnu_entity_name;
206 /* True if we have already saved gnu_decl as a GNAT association. */
208 /* True if we incremented defer_incomplete_level. */
209 bool this_deferred = false;
210 /* True if we incremented force_global. */
211 bool this_global = false;
212 /* True if we should check to see if elaborated during processing. */
213 bool maybe_present = false;
214 /* True if we made GNU_DECL and its type here. */
215 bool this_made_decl = false;
216 /* Size and alignment of the GCC node, if meaningful. */
217 unsigned int esize = 0, align = 0;
218 /* Contains the list of attributes directly attached to the entity. */
219 struct attrib *attr_list = NULL;
221 /* Since a use of an Itype is a definition, process it as such if it
222 is not in a with'ed unit. */
225 && Is_Itype (gnat_entity)
226 && !present_gnu_tree (gnat_entity)
227 && In_Extended_Main_Code_Unit (gnat_entity))
229 /* Ensure that we are in a subprogram mentioned in the Scope chain of
230 this entity, our current scope is global, or we encountered a task
231 or entry (where we can't currently accurately check scoping). */
232 if (!current_function_decl
233 || DECL_ELABORATION_PROC_P (current_function_decl))
235 process_type (gnat_entity);
236 return get_gnu_tree (gnat_entity);
239 for (gnat_temp = Scope (gnat_entity);
241 gnat_temp = Scope (gnat_temp))
243 if (Is_Type (gnat_temp))
244 gnat_temp = Underlying_Type (gnat_temp);
246 if (Ekind (gnat_temp) == E_Subprogram_Body)
248 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
250 if (IN (Ekind (gnat_temp), Subprogram_Kind)
251 && Present (Protected_Body_Subprogram (gnat_temp)))
252 gnat_temp = Protected_Body_Subprogram (gnat_temp);
254 if (Ekind (gnat_temp) == E_Entry
255 || Ekind (gnat_temp) == E_Entry_Family
256 || Ekind (gnat_temp) == E_Task_Type
257 || (IN (Ekind (gnat_temp), Subprogram_Kind)
258 && present_gnu_tree (gnat_temp)
259 && (current_function_decl
260 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
262 process_type (gnat_entity);
263 return get_gnu_tree (gnat_entity);
267 /* This abort means the Itype has an incorrect scope, i.e. that its
268 scope does not correspond to the subprogram it is declared in. */
272 /* If we've already processed this entity, return what we got last time.
273 If we are defining the node, we should not have already processed it.
274 In that case, we will abort below when we try to save a new GCC tree
275 for this object. We also need to handle the case of getting a dummy
276 type when a Full_View exists. */
277 if ((!definition || (is_type && imported_p))
278 && present_gnu_tree (gnat_entity))
280 gnu_decl = get_gnu_tree (gnat_entity);
282 if (TREE_CODE (gnu_decl) == TYPE_DECL
283 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
284 && IN (kind, Incomplete_Or_Private_Kind)
285 && Present (Full_View (gnat_entity)))
288 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
289 save_gnu_tree (gnat_entity, NULL_TREE, false);
290 save_gnu_tree (gnat_entity, gnu_decl, false);
296 /* If this is a numeric or enumeral type, or an access type, a nonzero
297 Esize must be specified unless it was specified by the programmer. */
298 gcc_assert (!Unknown_Esize (gnat_entity)
299 || Has_Size_Clause (gnat_entity)
300 || (!IN (kind, Numeric_Kind)
301 && !IN (kind, Enumeration_Kind)
302 && (!IN (kind, Access_Kind)
303 || kind == E_Access_Protected_Subprogram_Type
304 || kind == E_Anonymous_Access_Protected_Subprogram_Type
305 || kind == E_Access_Subtype)));
307 /* The RM size must be specified for all discrete and fixed-point types. */
308 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
309 && Unknown_RM_Size (gnat_entity)));
311 /* If we get here, it means we have not yet done anything with this entity.
312 If we are not defining it, it must be a type or an entity that is defined
313 elsewhere or externally, otherwise we should have defined it already. */
314 gcc_assert (definition
315 || type_annotate_only
317 || kind == E_Discriminant
318 || kind == E_Component
320 || (kind == E_Constant && Present (Full_View (gnat_entity)))
321 || Is_Public (gnat_entity));
323 /* Get the name of the entity and set up the line number and filename of
324 the original definition for use in any decl we make. */
325 gnu_entity_name = get_entity_name (gnat_entity);
326 Sloc_to_locus (Sloc (gnat_entity), &input_location);
328 /* For cases when we are not defining (i.e., we are referencing from
329 another compilation unit) public entities, show we are at global level
330 for the purpose of computing scopes. Don't do this for components or
331 discriminants since the relevant test is whether or not the record is
334 && kind != E_Component
335 && kind != E_Discriminant
336 && Is_Public (gnat_entity)
337 && !Is_Statically_Allocated (gnat_entity))
338 force_global++, this_global = true;
340 /* Handle any attributes directly attached to the entity. */
341 if (Has_Gigi_Rep_Item (gnat_entity))
342 prepend_attributes (gnat_entity, &attr_list);
344 /* Do some common processing for types. */
347 /* Compute the equivalent type to be used in gigi. */
348 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
350 /* Machine_Attributes on types are expected to be propagated to
351 subtypes. The corresponding Gigi_Rep_Items are only attached
352 to the first subtype though, so we handle the propagation here. */
353 if (Base_Type (gnat_entity) != gnat_entity
354 && !Is_First_Subtype (gnat_entity)
355 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
356 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
359 /* Compute a default value for the size of the type. */
360 if (Known_Esize (gnat_entity)
361 && UI_Is_In_Int_Range (Esize (gnat_entity)))
363 unsigned int max_esize;
364 esize = UI_To_Int (Esize (gnat_entity));
366 if (IN (kind, Float_Kind))
367 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
368 else if (IN (kind, Access_Kind))
369 max_esize = POINTER_SIZE * 2;
371 max_esize = LONG_LONG_TYPE_SIZE;
373 if (esize > max_esize)
377 esize = LONG_LONG_TYPE_SIZE;
383 /* If this is a use of a deferred constant without address clause,
384 get its full definition. */
386 && No (Address_Clause (gnat_entity))
387 && Present (Full_View (gnat_entity)))
390 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
395 /* If we have an external constant that we are not defining, get the
396 expression that is was defined to represent. We may throw that
397 expression away later if it is not a constant. Do not retrieve the
398 expression if it is an aggregate or allocator, because in complex
399 instantiation contexts it may not be expanded */
401 && Present (Expression (Declaration_Node (gnat_entity)))
402 && !No_Initialization (Declaration_Node (gnat_entity))
403 && (Nkind (Expression (Declaration_Node (gnat_entity)))
405 && (Nkind (Expression (Declaration_Node (gnat_entity)))
407 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
409 /* Ignore deferred constant definitions without address clause since
410 they are processed fully in the front-end. If No_Initialization
411 is set, this is not a deferred constant but a constant whose value
412 is built manually. And constants that are renamings are handled
416 && No (Address_Clause (gnat_entity))
417 && !No_Initialization (Declaration_Node (gnat_entity))
418 && No (Renamed_Object (gnat_entity)))
420 gnu_decl = error_mark_node;
425 /* Ignore constant definitions already marked with the error node. See
426 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
429 && present_gnu_tree (gnat_entity)
430 && get_gnu_tree (gnat_entity) == error_mark_node)
432 maybe_present = true;
439 /* We used to special case VMS exceptions here to directly map them to
440 their associated condition code. Since this code had to be masked
441 dynamically to strip off the severity bits, this caused trouble in
442 the GCC/ZCX case because the "type" pointers we store in the tables
443 have to be static. We now don't special case here anymore, and let
444 the regular processing take place, which leaves us with a regular
445 exception data object for VMS exceptions too. The condition code
446 mapping is taken care of by the front end and the bitmasking by the
453 /* The GNAT record where the component was defined. */
454 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
456 /* If the variable is an inherited record component (in the case of
457 extended record types), just return the inherited entity, which
458 must be a FIELD_DECL. Likewise for discriminants.
459 For discriminants of untagged records which have explicit
460 stored discriminants, return the entity for the corresponding
461 stored discriminant. Also use Original_Record_Component
462 if the record has a private extension. */
463 if (Present (Original_Record_Component (gnat_entity))
464 && Original_Record_Component (gnat_entity) != gnat_entity)
467 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
468 gnu_expr, definition);
473 /* If the enclosing record has explicit stored discriminants,
474 then it is an untagged record. If the Corresponding_Discriminant
475 is not empty then this must be a renamed discriminant and its
476 Original_Record_Component must point to the corresponding explicit
477 stored discriminant (i.e. we should have taken the previous
479 else if (Present (Corresponding_Discriminant (gnat_entity))
480 && Is_Tagged_Type (gnat_record))
482 /* A tagged record has no explicit stored discriminants. */
483 gcc_assert (First_Discriminant (gnat_record)
484 == First_Stored_Discriminant (gnat_record));
486 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
487 gnu_expr, definition);
492 else if (Present (CR_Discriminant (gnat_entity))
493 && type_annotate_only)
495 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
496 gnu_expr, definition);
501 /* If the enclosing record has explicit stored discriminants, then
502 it is an untagged record. If the Corresponding_Discriminant
503 is not empty then this must be a renamed discriminant and its
504 Original_Record_Component must point to the corresponding explicit
505 stored discriminant (i.e. we should have taken the first
507 else if (Present (Corresponding_Discriminant (gnat_entity))
508 && (First_Discriminant (gnat_record)
509 != First_Stored_Discriminant (gnat_record)))
512 /* Otherwise, if we are not defining this and we have no GCC type
513 for the containing record, make one for it. Then we should
514 have made our own equivalent. */
515 else if (!definition && !present_gnu_tree (gnat_record))
517 /* ??? If this is in a record whose scope is a protected
518 type and we have an Original_Record_Component, use it.
519 This is a workaround for major problems in protected type
521 Entity_Id Scop = Scope (Scope (gnat_entity));
522 if ((Is_Protected_Type (Scop)
523 || (Is_Private_Type (Scop)
524 && Present (Full_View (Scop))
525 && Is_Protected_Type (Full_View (Scop))))
526 && Present (Original_Record_Component (gnat_entity)))
529 = gnat_to_gnu_entity (Original_Record_Component
536 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
537 gnu_decl = get_gnu_tree (gnat_entity);
543 /* Here we have no GCC type and this is a reference rather than a
544 definition. This should never happen. Most likely the cause is
545 reference before declaration in the gnat tree for gnat_entity. */
549 case E_Loop_Parameter:
550 case E_Out_Parameter:
553 /* Simple variables, loop variables, Out parameters and exceptions. */
557 = ((kind == E_Constant || kind == E_Variable)
558 && Is_True_Constant (gnat_entity)
559 && !Treat_As_Volatile (gnat_entity)
560 && (((Nkind (Declaration_Node (gnat_entity))
561 == N_Object_Declaration)
562 && Present (Expression (Declaration_Node (gnat_entity))))
563 || Present (Renamed_Object (gnat_entity))
565 bool inner_const_flag = const_flag;
566 bool static_p = Is_Statically_Allocated (gnat_entity);
567 bool mutable_p = false;
568 bool used_by_ref = false;
569 tree gnu_ext_name = NULL_TREE;
570 tree renamed_obj = NULL_TREE;
571 tree gnu_object_size;
573 if (Present (Renamed_Object (gnat_entity)) && !definition)
575 if (kind == E_Exception)
576 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
579 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
582 /* Get the type after elaborating the renamed object. */
583 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
585 /* If this is a standard exception definition, then use the standard
586 exception type. This is necessary to make sure that imported and
587 exported views of exceptions are properly merged in LTO mode. */
588 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
589 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
590 gnu_type = except_type_node;
592 /* For a debug renaming declaration, build a pure debug entity. */
593 if (Present (Debug_Renaming_Link (gnat_entity)))
596 gnu_decl = build_decl (input_location,
597 VAR_DECL, gnu_entity_name, gnu_type);
598 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
599 if (global_bindings_p ())
600 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
602 addr = stack_pointer_rtx;
603 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
604 gnat_pushdecl (gnu_decl, gnat_entity);
608 /* If this is a loop variable, its type should be the base type.
609 This is because the code for processing a loop determines whether
610 a normal loop end test can be done by comparing the bounds of the
611 loop against those of the base type, which is presumed to be the
612 size used for computation. But this is not correct when the size
613 of the subtype is smaller than the type. */
614 if (kind == E_Loop_Parameter)
615 gnu_type = get_base_type (gnu_type);
617 /* Reject non-renamed objects whose type is an unconstrained array or
618 any object whose type is a dummy type or void. */
619 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
620 && No (Renamed_Object (gnat_entity)))
621 || TYPE_IS_DUMMY_P (gnu_type)
622 || TREE_CODE (gnu_type) == VOID_TYPE)
624 gcc_assert (type_annotate_only);
627 return error_mark_node;
630 /* If an alignment is specified, use it if valid. Note that exceptions
631 are objects but don't have an alignment. We must do this before we
632 validate the size, since the alignment can affect the size. */
633 if (kind != E_Exception && Known_Alignment (gnat_entity))
635 gcc_assert (Present (Alignment (gnat_entity)));
636 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
637 TYPE_ALIGN (gnu_type));
639 /* No point in changing the type if there is an address clause
640 as the final type of the object will be a reference type. */
641 if (Present (Address_Clause (gnat_entity)))
645 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
646 false, false, definition, true);
649 /* If we are defining the object, see if it has a Size and validate it
650 if so. If we are not defining the object and a Size clause applies,
651 simply retrieve the value. We don't want to ignore the clause and
652 it is expected to have been validated already. Then get the new
655 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
656 gnat_entity, VAR_DECL, false,
657 Has_Size_Clause (gnat_entity));
658 else if (Has_Size_Clause (gnat_entity))
659 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
664 = make_type_from_size (gnu_type, gnu_size,
665 Has_Biased_Representation (gnat_entity));
667 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
668 gnu_size = NULL_TREE;
671 /* If this object has self-referential size, it must be a record with
672 a default discriminant. We are supposed to allocate an object of
673 the maximum size in this case, unless it is a constant with an
674 initializing expression, in which case we can get the size from
675 that. Note that the resulting size may still be a variable, so
676 this may end up with an indirect allocation. */
677 if (No (Renamed_Object (gnat_entity))
678 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
680 if (gnu_expr && kind == E_Constant)
682 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
683 if (CONTAINS_PLACEHOLDER_P (size))
685 /* If the initializing expression is itself a constant,
686 despite having a nominal type with self-referential
687 size, we can get the size directly from it. */
688 if (TREE_CODE (gnu_expr) == COMPONENT_REF
690 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
691 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
692 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
693 || DECL_READONLY_ONCE_ELAB
694 (TREE_OPERAND (gnu_expr, 0))))
695 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
698 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
703 /* We may have no GNU_EXPR because No_Initialization is
704 set even though there's an Expression. */
705 else if (kind == E_Constant
706 && (Nkind (Declaration_Node (gnat_entity))
707 == N_Object_Declaration)
708 && Present (Expression (Declaration_Node (gnat_entity))))
710 = TYPE_SIZE (gnat_to_gnu_type
712 (Expression (Declaration_Node (gnat_entity)))));
715 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
720 /* If the size is zero byte, make it one byte since some linkers have
721 troubles with zero-sized objects. If the object will have a
722 template, that will make it nonzero so don't bother. Also avoid
723 doing that for an object renaming or an object with an address
724 clause, as we would lose useful information on the view size
725 (e.g. for null array slices) and we are not allocating the object
728 && integer_zerop (gnu_size)
729 && !TREE_OVERFLOW (gnu_size))
730 || (TYPE_SIZE (gnu_type)
731 && integer_zerop (TYPE_SIZE (gnu_type))
732 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
733 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
734 || !Is_Array_Type (Etype (gnat_entity)))
735 && No (Renamed_Object (gnat_entity))
736 && No (Address_Clause (gnat_entity)))
737 gnu_size = bitsize_unit_node;
739 /* If this is an object with no specified size and alignment, and
740 if either it is atomic or we are not optimizing alignment for
741 space and it is composite and not an exception, an Out parameter
742 or a reference to another object, and the size of its type is a
743 constant, set the alignment to the smallest one which is not
744 smaller than the size, with an appropriate cap. */
745 if (!gnu_size && align == 0
746 && (Is_Atomic (gnat_entity)
747 || (!Optimize_Alignment_Space (gnat_entity)
748 && kind != E_Exception
749 && kind != E_Out_Parameter
750 && Is_Composite_Type (Etype (gnat_entity))
751 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
752 && !Is_Exported (gnat_entity)
754 && No (Renamed_Object (gnat_entity))
755 && No (Address_Clause (gnat_entity))))
756 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
758 /* No point in jumping through all the hoops needed in order
759 to support BIGGEST_ALIGNMENT if we don't really have to.
760 So we cap to the smallest alignment that corresponds to
761 a known efficient memory access pattern of the target. */
762 unsigned int align_cap = Is_Atomic (gnat_entity)
764 : get_mode_alignment (ptr_mode);
766 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
767 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
770 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
772 /* But make sure not to under-align the object. */
773 if (align <= TYPE_ALIGN (gnu_type))
776 /* And honor the minimum valid atomic alignment, if any. */
777 #ifdef MINIMUM_ATOMIC_ALIGNMENT
778 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
779 align = MINIMUM_ATOMIC_ALIGNMENT;
783 /* If the object is set to have atomic components, find the component
784 type and validate it.
786 ??? Note that we ignore Has_Volatile_Components on objects; it's
787 not at all clear what to do in that case. */
788 if (Has_Atomic_Components (gnat_entity))
790 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
791 ? TREE_TYPE (gnu_type) : gnu_type);
793 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
794 && TYPE_MULTI_ARRAY_P (gnu_inner))
795 gnu_inner = TREE_TYPE (gnu_inner);
797 check_ok_for_atomic (gnu_inner, gnat_entity, true);
800 /* Now check if the type of the object allows atomic access. Note
801 that we must test the type, even if this object has size and
802 alignment to allow such access, because we will be going inside
803 the padded record to assign to the object. We could fix this by
804 always copying via an intermediate value, but it's not clear it's
806 if (Is_Atomic (gnat_entity))
807 check_ok_for_atomic (gnu_type, gnat_entity, false);
809 /* If this is an aliased object with an unconstrained nominal subtype,
810 make a type that includes the template. */
811 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
812 && Is_Array_Type (Etype (gnat_entity))
813 && !type_annotate_only)
816 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
819 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
820 concat_name (gnu_entity_name,
825 #ifdef MINIMUM_ATOMIC_ALIGNMENT
826 /* If the size is a constant and no alignment is specified, force
827 the alignment to be the minimum valid atomic alignment. The
828 restriction on constant size avoids problems with variable-size
829 temporaries; if the size is variable, there's no issue with
830 atomic access. Also don't do this for a constant, since it isn't
831 necessary and can interfere with constant replacement. Finally,
832 do not do it for Out parameters since that creates an
833 size inconsistency with In parameters. */
834 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
835 && !FLOAT_TYPE_P (gnu_type)
836 && !const_flag && No (Renamed_Object (gnat_entity))
837 && !imported_p && No (Address_Clause (gnat_entity))
838 && kind != E_Out_Parameter
839 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
840 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
841 align = MINIMUM_ATOMIC_ALIGNMENT;
844 /* Make a new type with the desired size and alignment, if needed.
845 But do not take into account alignment promotions to compute the
846 size of the object. */
847 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
848 if (gnu_size || align > 0)
849 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
850 false, false, definition,
851 gnu_size ? true : false);
853 /* If this is a renaming, avoid as much as possible to create a new
854 object. However, in several cases, creating it is required.
855 This processing needs to be applied to the raw expression so
856 as to make it more likely to rename the underlying object. */
857 if (Present (Renamed_Object (gnat_entity)))
859 bool create_normal_object = false;
861 /* If the renamed object had padding, strip off the reference
862 to the inner object and reset our type. */
863 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
864 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
865 /* Strip useless conversions around the object. */
866 || (TREE_CODE (gnu_expr) == NOP_EXPR
867 && gnat_types_compatible_p
868 (TREE_TYPE (gnu_expr),
869 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
871 gnu_expr = TREE_OPERAND (gnu_expr, 0);
872 gnu_type = TREE_TYPE (gnu_expr);
875 /* Case 1: If this is a constant renaming stemming from a function
876 call, treat it as a normal object whose initial value is what
877 is being renamed. RM 3.3 says that the result of evaluating a
878 function call is a constant object. As a consequence, it can
879 be the inner object of a constant renaming. In this case, the
880 renaming must be fully instantiated, i.e. it cannot be a mere
881 reference to (part of) an existing object. */
884 tree inner_object = gnu_expr;
885 while (handled_component_p (inner_object))
886 inner_object = TREE_OPERAND (inner_object, 0);
887 if (TREE_CODE (inner_object) == CALL_EXPR)
888 create_normal_object = true;
891 /* Otherwise, see if we can proceed with a stabilized version of
892 the renamed entity or if we need to make a new object. */
893 if (!create_normal_object)
895 tree maybe_stable_expr = NULL_TREE;
898 /* Case 2: If the renaming entity need not be materialized and
899 the renamed expression is something we can stabilize, use
900 that for the renaming. At the global level, we can only do
901 this if we know no SAVE_EXPRs need be made, because the
902 expression we return might be used in arbitrary conditional
903 branches so we must force the SAVE_EXPRs evaluation
904 immediately and this requires a function context. */
905 if (!Materialize_Entity (gnat_entity)
906 && (!global_bindings_p ()
907 || (staticp (gnu_expr)
908 && !TREE_SIDE_EFFECTS (gnu_expr))))
911 = gnat_stabilize_reference (gnu_expr, true, &stable);
915 /* ??? No DECL_EXPR is created so we need to mark
916 the expression manually lest it is shared. */
917 if (global_bindings_p ())
918 MARK_VISITED (maybe_stable_expr);
919 gnu_decl = maybe_stable_expr;
920 save_gnu_tree (gnat_entity, gnu_decl, true);
922 annotate_object (gnat_entity, gnu_type, NULL_TREE,
927 /* The stabilization failed. Keep maybe_stable_expr
928 untouched here to let the pointer case below know
929 about that failure. */
932 /* Case 3: If this is a constant renaming and creating a
933 new object is allowed and cheap, treat it as a normal
934 object whose initial value is what is being renamed. */
936 && !Is_Composite_Type
937 (Underlying_Type (Etype (gnat_entity))))
940 /* Case 4: Make this into a constant pointer to the object we
941 are to rename and attach the object to the pointer if it is
942 something we can stabilize.
944 From the proper scope, attached objects will be referenced
945 directly instead of indirectly via the pointer to avoid
946 subtle aliasing problems with non-addressable entities.
947 They have to be stable because we must not evaluate the
948 variables in the expression every time the renaming is used.
949 The pointer is called a "renaming" pointer in this case.
951 In the rare cases where we cannot stabilize the renamed
952 object, we just make a "bare" pointer, and the renamed
953 entity is always accessed indirectly through it. */
956 gnu_type = build_reference_type (gnu_type);
957 inner_const_flag = TREE_READONLY (gnu_expr);
960 /* If the previous attempt at stabilizing failed, there
961 is no point in trying again and we reuse the result
962 without attaching it to the pointer. In this case it
963 will only be used as the initializing expression of
964 the pointer and thus needs no special treatment with
965 regard to multiple evaluations. */
966 if (maybe_stable_expr)
969 /* Otherwise, try to stabilize and attach the expression
970 to the pointer if the stabilization succeeds.
972 Note that this might introduce SAVE_EXPRs and we don't
973 check whether we're at the global level or not. This
974 is fine since we are building a pointer initializer and
975 neither the pointer nor the initializing expression can
976 be accessed before the pointer elaboration has taken
977 place in a correct program.
979 These SAVE_EXPRs will be evaluated at the right place
980 by either the evaluation of the initializer for the
981 non-global case or the elaboration code for the global
982 case, and will be attached to the elaboration procedure
983 in the latter case. */
987 = gnat_stabilize_reference (gnu_expr, true, &stable);
990 renamed_obj = maybe_stable_expr;
992 /* Attaching is actually performed downstream, as soon
993 as we have a VAR_DECL for the pointer we make. */
996 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
999 gnu_size = NULL_TREE;
1005 /* Make a volatile version of this object's type if we are to make
1006 the object volatile. We also interpret 13.3(19) conservatively
1007 and disallow any optimizations for such a non-constant object. */
1008 if ((Treat_As_Volatile (gnat_entity)
1010 && gnu_type != except_type_node
1011 && (Is_Exported (gnat_entity)
1013 || Present (Address_Clause (gnat_entity)))))
1014 && !TYPE_VOLATILE (gnu_type))
1015 gnu_type = build_qualified_type (gnu_type,
1016 (TYPE_QUALS (gnu_type)
1017 | TYPE_QUAL_VOLATILE));
1019 /* If we are defining an aliased object whose nominal subtype is
1020 unconstrained, the object is a record that contains both the
1021 template and the object. If there is an initializer, it will
1022 have already been converted to the right type, but we need to
1023 create the template if there is no initializer. */
1026 && TREE_CODE (gnu_type) == RECORD_TYPE
1027 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1028 /* Beware that padding might have been introduced above. */
1029 || (TYPE_PADDING_P (gnu_type)
1030 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1032 && TYPE_CONTAINS_TEMPLATE_P
1033 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1036 = TYPE_PADDING_P (gnu_type)
1037 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1038 : TYPE_FIELDS (gnu_type);
1040 = gnat_build_constructor
1044 build_template (TREE_TYPE (template_field),
1045 TREE_TYPE (TREE_CHAIN (template_field)),
1050 /* Convert the expression to the type of the object except in the
1051 case where the object's type is unconstrained or the object's type
1052 is a padded record whose field is of self-referential size. In
1053 the former case, converting will generate unnecessary evaluations
1054 of the CONSTRUCTOR to compute the size and in the latter case, we
1055 want to only copy the actual data. */
1057 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1058 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1059 && !(TYPE_IS_PADDING_P (gnu_type)
1060 && CONTAINS_PLACEHOLDER_P
1061 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1062 gnu_expr = convert (gnu_type, gnu_expr);
1064 /* If this is a pointer that doesn't have an initializing expression,
1065 initialize it to NULL, unless the object is imported. */
1067 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1069 && !Is_Imported (gnat_entity))
1070 gnu_expr = integer_zero_node;
1072 /* If we are defining the object and it has an Address clause, we must
1073 either get the address expression from the saved GCC tree for the
1074 object if it has a Freeze node, or elaborate the address expression
1075 here since the front-end has guaranteed that the elaboration has no
1076 effects in this case. */
1077 if (definition && Present (Address_Clause (gnat_entity)))
1079 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1081 = present_gnu_tree (gnat_entity)
1082 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1084 save_gnu_tree (gnat_entity, NULL_TREE, false);
1086 /* Ignore the size. It's either meaningless or was handled
1088 gnu_size = NULL_TREE;
1089 /* Convert the type of the object to a reference type that can
1090 alias everything as per 13.3(19). */
1092 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1093 gnu_address = convert (gnu_type, gnu_address);
1096 = !Is_Public (gnat_entity)
1097 || compile_time_known_address_p (gnat_expr);
1099 /* If this is a deferred constant, the initializer is attached to
1101 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1104 (Expression (Declaration_Node (Full_View (gnat_entity))));
1106 /* If we don't have an initializing expression for the underlying
1107 variable, the initializing expression for the pointer is the
1108 specified address. Otherwise, we have to make a COMPOUND_EXPR
1109 to assign both the address and the initial value. */
1111 gnu_expr = gnu_address;
1114 = build2 (COMPOUND_EXPR, gnu_type,
1116 (MODIFY_EXPR, NULL_TREE,
1117 build_unary_op (INDIRECT_REF, NULL_TREE,
1123 /* If it has an address clause and we are not defining it, mark it
1124 as an indirect object. Likewise for Stdcall objects that are
1126 if ((!definition && Present (Address_Clause (gnat_entity)))
1127 || (Is_Imported (gnat_entity)
1128 && Has_Stdcall_Convention (gnat_entity)))
1130 /* Convert the type of the object to a reference type that can
1131 alias everything as per 13.3(19). */
1133 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1134 gnu_size = NULL_TREE;
1136 /* No point in taking the address of an initializing expression
1137 that isn't going to be used. */
1138 gnu_expr = NULL_TREE;
1140 /* If it has an address clause whose value is known at compile
1141 time, make the object a CONST_DECL. This will avoid a
1142 useless dereference. */
1143 if (Present (Address_Clause (gnat_entity)))
1145 Node_Id gnat_address
1146 = Expression (Address_Clause (gnat_entity));
1148 if (compile_time_known_address_p (gnat_address))
1150 gnu_expr = gnat_to_gnu (gnat_address);
1158 /* If we are at top level and this object is of variable size,
1159 make the actual type a hidden pointer to the real type and
1160 make the initializer be a memory allocation and initialization.
1161 Likewise for objects we aren't defining (presumed to be
1162 external references from other packages), but there we do
1163 not set up an initialization.
1165 If the object's size overflows, make an allocator too, so that
1166 Storage_Error gets raised. Note that we will never free
1167 such memory, so we presume it never will get allocated. */
1168 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1169 global_bindings_p ()
1172 || (gnu_size && !allocatable_size_p (gnu_size,
1173 global_bindings_p ()
1177 gnu_type = build_reference_type (gnu_type);
1178 gnu_size = NULL_TREE;
1182 /* In case this was a aliased object whose nominal subtype is
1183 unconstrained, the pointer above will be a thin pointer and
1184 build_allocator will automatically make the template.
1186 If we have a template initializer only (that we made above),
1187 pretend there is none and rely on what build_allocator creates
1188 again anyway. Otherwise (if we have a full initializer), get
1189 the data part and feed that to build_allocator.
1191 If we are elaborating a mutable object, tell build_allocator to
1192 ignore a possibly simpler size from the initializer, if any, as
1193 we must allocate the maximum possible size in this case. */
1196 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1198 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1199 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1202 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1204 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1205 && 1 == VEC_length (constructor_elt,
1206 CONSTRUCTOR_ELTS (gnu_expr)))
1210 = build_component_ref
1211 (gnu_expr, NULL_TREE,
1212 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1216 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1217 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1218 && !Is_Imported (gnat_entity))
1219 post_error ("?Storage_Error will be raised at run-time!",
1223 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1224 Empty, Empty, gnat_entity, mutable_p);
1228 gnu_expr = NULL_TREE;
1233 /* If this object would go into the stack and has an alignment larger
1234 than the largest stack alignment the back-end can honor, resort to
1235 a variable of "aligning type". */
1236 if (!global_bindings_p () && !static_p && definition
1237 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1239 /* Create the new variable. No need for extra room before the
1240 aligned field as this is in automatic storage. */
1242 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1243 TYPE_SIZE_UNIT (gnu_type),
1244 BIGGEST_ALIGNMENT, 0);
1246 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1247 NULL_TREE, gnu_new_type, NULL_TREE, false,
1248 false, false, false, NULL, gnat_entity);
1250 /* Initialize the aligned field if we have an initializer. */
1253 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1255 (gnu_new_var, NULL_TREE,
1256 TYPE_FIELDS (gnu_new_type), false),
1260 /* And setup this entity as a reference to the aligned field. */
1261 gnu_type = build_reference_type (gnu_type);
1264 (ADDR_EXPR, gnu_type,
1265 build_component_ref (gnu_new_var, NULL_TREE,
1266 TYPE_FIELDS (gnu_new_type), false));
1268 gnu_size = NULL_TREE;
1274 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1275 | TYPE_QUAL_CONST));
1277 /* Convert the expression to the type of the object except in the
1278 case where the object's type is unconstrained or the object's type
1279 is a padded record whose field is of self-referential size. In
1280 the former case, converting will generate unnecessary evaluations
1281 of the CONSTRUCTOR to compute the size and in the latter case, we
1282 want to only copy the actual data. */
1284 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1285 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1286 && !(TYPE_IS_PADDING_P (gnu_type)
1287 && CONTAINS_PLACEHOLDER_P
1288 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1289 gnu_expr = convert (gnu_type, gnu_expr);
1291 /* If this name is external or there was a name specified, use it,
1292 unless this is a VMS exception object since this would conflict
1293 with the symbol we need to export in addition. Don't use the
1294 Interface_Name if there is an address clause (see CD30005). */
1295 if (!Is_VMS_Exception (gnat_entity)
1296 && ((Present (Interface_Name (gnat_entity))
1297 && No (Address_Clause (gnat_entity)))
1298 || (Is_Public (gnat_entity)
1299 && (!Is_Imported (gnat_entity)
1300 || Is_Exported (gnat_entity)))))
1301 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1303 /* If this is an aggregate constant initialized to a constant, force it
1304 to be statically allocated. This saves an initialization copy. */
1307 && gnu_expr && TREE_CONSTANT (gnu_expr)
1308 && AGGREGATE_TYPE_P (gnu_type)
1309 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1310 && !(TYPE_IS_PADDING_P (gnu_type)
1311 && !host_integerp (TYPE_SIZE_UNIT
1312 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1315 /* Now create the variable or the constant and set various flags. */
1317 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1318 gnu_expr, const_flag, Is_Public (gnat_entity),
1319 imported_p || !definition, static_p, attr_list,
1321 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1322 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1324 /* If we are defining an Out parameter and optimization isn't enabled,
1325 create a fake PARM_DECL for debugging purposes and make it point to
1326 the VAR_DECL. Suppress debug info for the latter but make sure it
1327 will live on the stack so that it can be accessed from within the
1328 debugger through the PARM_DECL. */
1329 if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1331 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1332 gnat_pushdecl (param, gnat_entity);
1333 SET_DECL_VALUE_EXPR (param, gnu_decl);
1334 DECL_HAS_VALUE_EXPR_P (param) = 1;
1335 DECL_IGNORED_P (gnu_decl) = 1;
1336 TREE_ADDRESSABLE (gnu_decl) = 1;
1339 /* If this is a renaming pointer, attach the renamed object to it and
1340 register it if we are at top level. */
1341 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1343 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1344 if (global_bindings_p ())
1346 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1347 record_global_renaming_pointer (gnu_decl);
1351 /* If this is a constant and we are defining it or it generates a real
1352 symbol at the object level and we are referencing it, we may want
1353 or need to have a true variable to represent it:
1354 - if optimization isn't enabled, for debugging purposes,
1355 - if the constant is public and not overlaid on something else,
1356 - if its address is taken,
1357 - if either itself or its type is aliased. */
1358 if (TREE_CODE (gnu_decl) == CONST_DECL
1359 && (definition || Sloc (gnat_entity) > Standard_Location)
1360 && ((!optimize && debug_info_p)
1361 || (Is_Public (gnat_entity)
1362 && No (Address_Clause (gnat_entity)))
1363 || Address_Taken (gnat_entity)
1364 || Is_Aliased (gnat_entity)
1365 || Is_Aliased (Etype (gnat_entity))))
1368 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1369 gnu_expr, true, Is_Public (gnat_entity),
1370 !definition, static_p, attr_list,
1373 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1375 /* As debugging information will be generated for the variable,
1376 do not generate debugging information for the constant. */
1378 DECL_IGNORED_P (gnu_decl) = 1;
1380 DECL_IGNORED_P (gnu_corr_var) = 1;
1383 /* If this is a constant, even if we don't need a true variable, we
1384 may need to avoid returning the initializer in every case. That
1385 can happen for the address of a (constant) constructor because,
1386 upon dereferencing it, the constructor will be reinjected in the
1387 tree, which may not be valid in every case; see lvalue_required_p
1388 for more details. */
1389 if (TREE_CODE (gnu_decl) == CONST_DECL)
1390 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1392 /* If this object is declared in a block that contains a block with an
1393 exception handler, and we aren't using the GCC exception mechanism,
1394 we must force this variable in memory in order to avoid an invalid
1396 if (Exception_Mechanism != Back_End_Exceptions
1397 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1398 TREE_ADDRESSABLE (gnu_decl) = 1;
1400 /* If we are defining an object with variable size or an object with
1401 fixed size that will be dynamically allocated, and we are using the
1402 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1404 && Exception_Mechanism == Setjmp_Longjmp
1405 && get_block_jmpbuf_decl ()
1406 && DECL_SIZE_UNIT (gnu_decl)
1407 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1408 || (flag_stack_check == GENERIC_STACK_CHECK
1409 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1410 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1411 add_stmt_with_node (build_call_1_expr
1412 (update_setjmp_buf_decl,
1413 build_unary_op (ADDR_EXPR, NULL_TREE,
1414 get_block_jmpbuf_decl ())),
1417 /* Back-annotate Esize and Alignment of the object if not already
1418 known. Note that we pick the values of the type, not those of
1419 the object, to shield ourselves from low-level platform-dependent
1420 adjustments like alignment promotion. This is both consistent with
1421 all the treatment above, where alignment and size are set on the
1422 type of the object and not on the object directly, and makes it
1423 possible to support all confirming representation clauses. */
1424 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1430 /* Return a TYPE_DECL for "void" that we previously made. */
1431 gnu_decl = TYPE_NAME (void_type_node);
1434 case E_Enumeration_Type:
1435 /* A special case: for the types Character and Wide_Character in
1436 Standard, we do not list all the literals. So if the literals
1437 are not specified, make this an unsigned type. */
1438 if (No (First_Literal (gnat_entity)))
1440 gnu_type = make_unsigned_type (esize);
1441 TYPE_NAME (gnu_type) = gnu_entity_name;
1443 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1444 This is needed by the DWARF-2 back-end to distinguish between
1445 unsigned integer types and character types. */
1446 TYPE_STRING_FLAG (gnu_type) = 1;
1451 /* We have a list of enumeral constants in First_Literal. We make a
1452 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1453 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1454 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1455 value of the literal. But when we have a regular boolean type, we
1456 simplify this a little by using a BOOLEAN_TYPE. */
1457 bool is_boolean = Is_Boolean_Type (gnat_entity)
1458 && !Has_Non_Standard_Rep (gnat_entity);
1459 tree gnu_literal_list = NULL_TREE;
1460 Entity_Id gnat_literal;
1462 if (Is_Unsigned_Type (gnat_entity))
1463 gnu_type = make_unsigned_type (esize);
1465 gnu_type = make_signed_type (esize);
1467 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1469 for (gnat_literal = First_Literal (gnat_entity);
1470 Present (gnat_literal);
1471 gnat_literal = Next_Literal (gnat_literal))
1474 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1476 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1477 gnu_type, gnu_value, true, false, false,
1478 false, NULL, gnat_literal);
1480 save_gnu_tree (gnat_literal, gnu_literal, false);
1481 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1482 gnu_value, gnu_literal_list);
1486 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1488 /* Note that the bounds are updated at the end of this function
1489 to avoid an infinite recursion since they refer to the type. */
1493 case E_Signed_Integer_Type:
1494 case E_Ordinary_Fixed_Point_Type:
1495 case E_Decimal_Fixed_Point_Type:
1496 /* For integer types, just make a signed type the appropriate number
1498 gnu_type = make_signed_type (esize);
1501 case E_Modular_Integer_Type:
1503 /* For modular types, make the unsigned type of the proper number
1504 of bits and then set up the modulus, if required. */
1505 tree gnu_modulus, gnu_high = NULL_TREE;
1507 /* Packed array types are supposed to be subtypes only. */
1508 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1510 gnu_type = make_unsigned_type (esize);
1512 /* Get the modulus in this type. If it overflows, assume it is because
1513 it is equal to 2**Esize. Note that there is no overflow checking
1514 done on unsigned type, so we detect the overflow by looking for
1515 a modulus of zero, which is otherwise invalid. */
1516 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1518 if (!integer_zerop (gnu_modulus))
1520 TYPE_MODULAR_P (gnu_type) = 1;
1521 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1522 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1523 convert (gnu_type, integer_one_node));
1526 /* If the upper bound is not maximal, make an extra subtype. */
1528 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1530 tree gnu_subtype = make_unsigned_type (esize);
1531 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1532 TREE_TYPE (gnu_subtype) = gnu_type;
1533 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1534 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1535 gnu_type = gnu_subtype;
1540 case E_Signed_Integer_Subtype:
1541 case E_Enumeration_Subtype:
1542 case E_Modular_Integer_Subtype:
1543 case E_Ordinary_Fixed_Point_Subtype:
1544 case E_Decimal_Fixed_Point_Subtype:
1546 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1547 not want to call create_range_type since we would like each subtype
1548 node to be distinct. ??? Historically this was in preparation for
1549 when memory aliasing is implemented, but that's obsolete now given
1550 the call to relate_alias_sets below.
1552 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1553 this fact is used by the arithmetic conversion functions.
1555 We elaborate the Ancestor_Subtype if it is not in the current unit
1556 and one of our bounds is non-static. We do this to ensure consistent
1557 naming in the case where several subtypes share the same bounds, by
1558 elaborating the first such subtype first, thus using its name. */
1561 && Present (Ancestor_Subtype (gnat_entity))
1562 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1563 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1564 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1565 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1567 /* Set the precision to the Esize except for bit-packed arrays. */
1568 if (Is_Packed_Array_Type (gnat_entity)
1569 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1570 esize = UI_To_Int (RM_Size (gnat_entity));
1572 /* This should be an unsigned type if the base type is unsigned or
1573 if the lower bound is constant and non-negative or if the type
1575 if (Is_Unsigned_Type (Etype (gnat_entity))
1576 || Is_Unsigned_Type (gnat_entity)
1577 || Has_Biased_Representation (gnat_entity))
1578 gnu_type = make_unsigned_type (esize);
1580 gnu_type = make_signed_type (esize);
1581 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1583 SET_TYPE_RM_MIN_VALUE
1585 convert (TREE_TYPE (gnu_type),
1586 elaborate_expression (Type_Low_Bound (gnat_entity),
1587 gnat_entity, get_identifier ("L"),
1589 Needs_Debug_Info (gnat_entity))));
1591 SET_TYPE_RM_MAX_VALUE
1593 convert (TREE_TYPE (gnu_type),
1594 elaborate_expression (Type_High_Bound (gnat_entity),
1595 gnat_entity, get_identifier ("U"),
1597 Needs_Debug_Info (gnat_entity))));
1599 /* One of the above calls might have caused us to be elaborated,
1600 so don't blow up if so. */
1601 if (present_gnu_tree (gnat_entity))
1603 maybe_present = true;
1607 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1608 = Has_Biased_Representation (gnat_entity);
1610 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1611 TYPE_STUB_DECL (gnu_type)
1612 = create_type_stub_decl (gnu_entity_name, gnu_type);
1614 /* Inherit our alias set from what we're a subtype of. Subtypes
1615 are not different types and a pointer can designate any instance
1616 within a subtype hierarchy. */
1617 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1619 /* For a packed array, make the original array type a parallel type. */
1621 && Is_Packed_Array_Type (gnat_entity)
1622 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1623 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1625 (Original_Array_Type (gnat_entity)));
1627 /* We have to handle clauses that under-align the type specially. */
1628 if ((Present (Alignment_Clause (gnat_entity))
1629 || (Is_Packed_Array_Type (gnat_entity)
1631 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1632 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1634 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1635 if (align >= TYPE_ALIGN (gnu_type))
1639 /* If the type we are dealing with represents a bit-packed array,
1640 we need to have the bits left justified on big-endian targets
1641 and right justified on little-endian targets. We also need to
1642 ensure that when the value is read (e.g. for comparison of two
1643 such values), we only get the good bits, since the unused bits
1644 are uninitialized. Both goals are accomplished by wrapping up
1645 the modular type in an enclosing record type. */
1646 if (Is_Packed_Array_Type (gnat_entity)
1647 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1649 tree gnu_field_type, gnu_field;
1651 /* Set the RM size before wrapping up the original type. */
1652 SET_TYPE_RM_SIZE (gnu_type,
1653 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1654 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1656 /* Create a stripped-down declaration, mainly for debugging. */
1657 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1658 debug_info_p, gnat_entity);
1660 /* Now save it and build the enclosing record type. */
1661 gnu_field_type = gnu_type;
1663 gnu_type = make_node (RECORD_TYPE);
1664 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1665 TYPE_PACKED (gnu_type) = 1;
1666 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1667 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1668 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1670 /* Propagate the alignment of the modular type to the record type,
1671 unless there is an alignment clause that under-aligns the type.
1672 This means that bit-packed arrays are given "ceil" alignment for
1673 their size by default, which may seem counter-intuitive but makes
1674 it possible to overlay them on modular types easily. */
1675 TYPE_ALIGN (gnu_type)
1676 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1678 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1680 /* Don't notify the field as "addressable", since we won't be taking
1681 it's address and it would prevent create_field_decl from making a
1683 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1684 gnu_field_type, gnu_type, 1,
1685 NULL_TREE, bitsize_zero_node, 0);
1687 /* Do not emit debug info until after the parallel type is added. */
1688 finish_record_type (gnu_type, gnu_field, 2, false);
1689 compute_record_mode (gnu_type);
1690 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1694 /* Make the original array type a parallel type. */
1695 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1696 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1698 (Original_Array_Type (gnat_entity)));
1700 rest_of_record_type_compilation (gnu_type);
1704 /* If the type we are dealing with has got a smaller alignment than the
1705 natural one, we need to wrap it up in a record type and under-align
1706 the latter. We reuse the padding machinery for this purpose. */
1709 tree gnu_field_type, gnu_field;
1711 /* Set the RM size before wrapping up the type. */
1712 SET_TYPE_RM_SIZE (gnu_type,
1713 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1715 /* Create a stripped-down declaration, mainly for debugging. */
1716 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1717 debug_info_p, gnat_entity);
1719 /* Now save it and build the enclosing record type. */
1720 gnu_field_type = gnu_type;
1722 gnu_type = make_node (RECORD_TYPE);
1723 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1724 TYPE_PACKED (gnu_type) = 1;
1725 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1726 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1727 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1728 TYPE_ALIGN (gnu_type) = align;
1729 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1731 /* Don't notify the field as "addressable", since we won't be taking
1732 it's address and it would prevent create_field_decl from making a
1734 gnu_field = create_field_decl (get_identifier ("F"),
1735 gnu_field_type, gnu_type, 1,
1736 NULL_TREE, bitsize_zero_node, 0);
1738 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1739 compute_record_mode (gnu_type);
1740 TYPE_PADDING_P (gnu_type) = 1;
1745 case E_Floating_Point_Type:
1746 /* If this is a VAX floating-point type, use an integer of the proper
1747 size. All the operations will be handled with ASM statements. */
1748 if (Vax_Float (gnat_entity))
1750 gnu_type = make_signed_type (esize);
1751 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1752 SET_TYPE_DIGITS_VALUE (gnu_type,
1753 UI_To_gnu (Digits_Value (gnat_entity),
1758 /* The type of the Low and High bounds can be our type if this is
1759 a type from Standard, so set them at the end of the function. */
1760 gnu_type = make_node (REAL_TYPE);
1761 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1762 layout_type (gnu_type);
1765 case E_Floating_Point_Subtype:
1766 if (Vax_Float (gnat_entity))
1768 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1774 && Present (Ancestor_Subtype (gnat_entity))
1775 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1776 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1777 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1778 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1781 gnu_type = make_node (REAL_TYPE);
1782 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1783 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1784 TYPE_GCC_MIN_VALUE (gnu_type)
1785 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1786 TYPE_GCC_MAX_VALUE (gnu_type)
1787 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1788 layout_type (gnu_type);
1790 SET_TYPE_RM_MIN_VALUE
1792 convert (TREE_TYPE (gnu_type),
1793 elaborate_expression (Type_Low_Bound (gnat_entity),
1794 gnat_entity, get_identifier ("L"),
1796 Needs_Debug_Info (gnat_entity))));
1798 SET_TYPE_RM_MAX_VALUE
1800 convert (TREE_TYPE (gnu_type),
1801 elaborate_expression (Type_High_Bound (gnat_entity),
1802 gnat_entity, get_identifier ("U"),
1804 Needs_Debug_Info (gnat_entity))));
1806 /* One of the above calls might have caused us to be elaborated,
1807 so don't blow up if so. */
1808 if (present_gnu_tree (gnat_entity))
1810 maybe_present = true;
1814 /* Inherit our alias set from what we're a subtype of, as for
1815 integer subtypes. */
1816 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1820 /* Array and String Types and Subtypes
1822 Unconstrained array types are represented by E_Array_Type and
1823 constrained array types are represented by E_Array_Subtype. There
1824 are no actual objects of an unconstrained array type; all we have
1825 are pointers to that type.
1827 The following fields are defined on array types and subtypes:
1829 Component_Type Component type of the array.
1830 Number_Dimensions Number of dimensions (an int).
1831 First_Index Type of first index. */
1836 Entity_Id gnat_index, gnat_name;
1837 const bool convention_fortran_p
1838 = (Convention (gnat_entity) == Convention_Fortran);
1839 const int ndim = Number_Dimensions (gnat_entity);
1840 tree gnu_template_fields = NULL_TREE;
1841 tree gnu_template_type = make_node (RECORD_TYPE);
1842 tree gnu_template_reference;
1843 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1844 tree gnu_fat_type = make_node (RECORD_TYPE);
1845 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1846 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1847 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
1850 TYPE_NAME (gnu_template_type)
1851 = create_concat_name (gnat_entity, "XUB");
1853 /* Make a node for the array. If we are not defining the array
1854 suppress expanding incomplete types. */
1855 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1859 defer_incomplete_level++;
1860 this_deferred = true;
1863 /* Build the fat pointer type. Use a "void *" object instead of
1864 a pointer to the array type since we don't have the array type
1865 yet (it will reference the fat pointer via the bounds). */
1866 tem = chainon (chainon (NULL_TREE,
1867 create_field_decl (get_identifier ("P_ARRAY"),
1869 gnu_fat_type, NULL_TREE,
1871 create_field_decl (get_identifier ("P_BOUNDS"),
1873 gnu_fat_type, NULL_TREE,
1876 /* Make sure we can put this into a register. */
1877 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1879 /* Do not emit debug info for this record type since the types of its
1880 fields are still incomplete at this point. */
1881 finish_record_type (gnu_fat_type, tem, 0, false);
1882 TYPE_FAT_POINTER_P (gnu_fat_type) = 1;
1884 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1885 is the fat pointer. This will be used to access the individual
1886 fields once we build them. */
1887 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1888 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1889 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1890 gnu_template_reference
1891 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1892 TREE_READONLY (gnu_template_reference) = 1;
1894 /* Now create the GCC type for each index and add the fields for that
1895 index to the template. */
1896 for (index = (convention_fortran_p ? ndim - 1 : 0),
1897 gnat_index = First_Index (gnat_entity);
1898 0 <= index && index < ndim;
1899 index += (convention_fortran_p ? - 1 : 1),
1900 gnat_index = Next_Index (gnat_index))
1902 char field_name[16];
1903 tree gnu_index_base_type
1904 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1905 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
1906 tree gnu_min, gnu_max, gnu_high;
1908 /* Make the FIELD_DECLs for the low and high bounds of this
1909 type and then make extractions of these fields from the
1911 sprintf (field_name, "LB%d", index);
1912 gnu_lb_field = create_field_decl (get_identifier (field_name),
1913 gnu_index_base_type,
1914 gnu_template_type, NULL_TREE,
1916 Sloc_to_locus (Sloc (gnat_entity),
1917 &DECL_SOURCE_LOCATION (gnu_lb_field));
1919 field_name[0] = 'U';
1920 gnu_hb_field = create_field_decl (get_identifier (field_name),
1921 gnu_index_base_type,
1922 gnu_template_type, NULL_TREE,
1924 Sloc_to_locus (Sloc (gnat_entity),
1925 &DECL_SOURCE_LOCATION (gnu_hb_field));
1927 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
1929 /* We can't use build_component_ref here since the template type
1930 isn't complete yet. */
1931 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
1932 gnu_template_reference, gnu_lb_field,
1934 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
1935 gnu_template_reference, gnu_hb_field,
1937 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
1939 gnu_min = convert (sizetype, gnu_orig_min);
1940 gnu_max = convert (sizetype, gnu_orig_max);
1942 /* Compute the size of this dimension. See the E_Array_Subtype
1943 case below for the rationale. */
1945 = build3 (COND_EXPR, sizetype,
1946 build2 (GE_EXPR, boolean_type_node,
1947 gnu_orig_max, gnu_orig_min),
1949 size_binop (MINUS_EXPR, gnu_min, size_one_node));
1951 /* Make a range type with the new range in the Ada base type.
1952 Then make an index type with the size range in sizetype. */
1953 gnu_index_types[index]
1954 = create_index_type (gnu_min, gnu_high,
1955 create_range_type (gnu_index_base_type,
1960 /* Update the maximum size of the array in elements. */
1963 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
1965 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
1967 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
1969 = size_binop (MAX_EXPR,
1970 size_binop (PLUS_EXPR, size_one_node,
1971 size_binop (MINUS_EXPR,
1975 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1976 && TREE_OVERFLOW (gnu_this_max))
1977 gnu_max_size = NULL_TREE;
1980 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1983 TYPE_NAME (gnu_index_types[index])
1984 = create_concat_name (gnat_entity, field_name);
1987 for (index = 0; index < ndim; index++)
1989 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1991 /* Install all the fields into the template. */
1992 finish_record_type (gnu_template_type, gnu_template_fields, 0,
1994 TYPE_READONLY (gnu_template_type) = 1;
1996 /* Now make the array of arrays and update the pointer to the array
1997 in the fat pointer. Note that it is the first field. */
1998 tem = gnat_to_gnu_component_type (gnat_entity, definition,
2001 /* If Component_Size is not already specified, annotate it with the
2002 size of the component. */
2003 if (Unknown_Component_Size (gnat_entity))
2004 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2006 /* Compute the maximum size of the array in units and bits. */
2009 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2010 TYPE_SIZE_UNIT (tem));
2011 gnu_max_size = size_binop (MULT_EXPR,
2012 convert (bitsizetype, gnu_max_size),
2016 gnu_max_size_unit = NULL_TREE;
2018 /* Now build the array type. */
2019 for (index = ndim - 1; index >= 0; index--)
2021 tem = build_array_type (tem, gnu_index_types[index]);
2022 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2023 if (array_type_has_nonaliased_component (tem, gnat_entity))
2024 TYPE_NONALIASED_COMPONENT (tem) = 1;
2027 /* If an alignment is specified, use it if valid. But ignore it
2028 for the original type of packed array types. If the alignment
2029 was requested with an explicit alignment clause, state so. */
2030 if (No (Packed_Array_Type (gnat_entity))
2031 && Known_Alignment (gnat_entity))
2034 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2036 if (Present (Alignment_Clause (gnat_entity)))
2037 TYPE_USER_ALIGN (tem) = 1;
2040 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2041 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2043 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2044 corresponding fat pointer. */
2045 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2046 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2047 SET_TYPE_MODE (gnu_type, BLKmode);
2048 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2049 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2051 /* If the maximum size doesn't overflow, use it. */
2053 && TREE_CODE (gnu_max_size) == INTEGER_CST
2054 && !TREE_OVERFLOW (gnu_max_size)
2055 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2056 && !TREE_OVERFLOW (gnu_max_size_unit))
2058 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2060 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2061 TYPE_SIZE_UNIT (tem));
2064 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2065 tem, NULL, !Comes_From_Source (gnat_entity),
2066 debug_info_p, gnat_entity);
2068 /* Give the fat pointer type a name. If this is a packed type, tell
2069 the debugger how to interpret the underlying bits. */
2070 if (Present (Packed_Array_Type (gnat_entity)))
2071 gnat_name = Packed_Array_Type (gnat_entity);
2073 gnat_name = gnat_entity;
2074 create_type_decl (create_concat_name (gnat_name, "XUP"),
2075 gnu_fat_type, NULL, true,
2076 debug_info_p, gnat_entity);
2078 /* Create the type to be used as what a thin pointer designates:
2079 a record type for the object and its template with the fields
2080 shifted to have the template at a negative offset. */
2081 tem = build_unc_object_type (gnu_template_type, tem,
2082 create_concat_name (gnat_name, "XUT"),
2084 shift_unc_components_for_thin_pointers (tem);
2086 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2087 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2091 case E_String_Subtype:
2092 case E_Array_Subtype:
2094 /* This is the actual data type for array variables. Multidimensional
2095 arrays are implemented as arrays of arrays. Note that arrays which
2096 have sparse enumeration subtypes as index components create sparse
2097 arrays, which is obviously space inefficient but so much easier to
2100 Also note that the subtype never refers to the unconstrained array
2101 type, which is somewhat at variance with Ada semantics.
2103 First check to see if this is simply a renaming of the array type.
2104 If so, the result is the array type. */
2106 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2107 if (!Is_Constrained (gnat_entity))
2111 Entity_Id gnat_index, gnat_base_index;
2112 const bool convention_fortran_p
2113 = (Convention (gnat_entity) == Convention_Fortran);
2114 const int ndim = Number_Dimensions (gnat_entity);
2115 tree gnu_base_type = gnu_type;
2116 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
2117 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2118 bool need_index_type_struct = false;
2121 /* First create the GCC type for each index and find out whether
2122 special types are needed for debugging information. */
2123 for (index = (convention_fortran_p ? ndim - 1 : 0),
2124 gnat_index = First_Index (gnat_entity),
2126 = First_Index (Implementation_Base_Type (gnat_entity));
2127 0 <= index && index < ndim;
2128 index += (convention_fortran_p ? - 1 : 1),
2129 gnat_index = Next_Index (gnat_index),
2130 gnat_base_index = Next_Index (gnat_base_index))
2132 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2133 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2134 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2135 tree gnu_min = convert (sizetype, gnu_orig_min);
2136 tree gnu_max = convert (sizetype, gnu_orig_max);
2137 tree gnu_base_index_type
2138 = get_unpadded_type (Etype (gnat_base_index));
2139 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2140 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2143 /* See if the base array type is already flat. If it is, we
2144 are probably compiling an ACATS test but it will cause the
2145 code below to malfunction if we don't handle it specially. */
2146 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2147 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2148 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2150 gnu_min = size_one_node;
2151 gnu_max = size_zero_node;
2155 /* Similarly, if one of the values overflows in sizetype and the
2156 range is null, use 1..0 for the sizetype bounds. */
2157 else if (TREE_CODE (gnu_min) == INTEGER_CST
2158 && TREE_CODE (gnu_max) == INTEGER_CST
2159 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2160 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2162 gnu_min = size_one_node;
2163 gnu_max = size_zero_node;
2167 /* If the minimum and maximum values both overflow in sizetype,
2168 but the difference in the original type does not overflow in
2169 sizetype, ignore the overflow indication. */
2170 else if (TREE_CODE (gnu_min) == INTEGER_CST
2171 && TREE_CODE (gnu_max) == INTEGER_CST
2172 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2175 fold_build2 (MINUS_EXPR, gnu_index_type,
2179 TREE_OVERFLOW (gnu_min) = 0;
2180 TREE_OVERFLOW (gnu_max) = 0;
2184 /* Compute the size of this dimension in the general case. We
2185 need to provide GCC with an upper bound to use but have to
2186 deal with the "superflat" case. There are three ways to do
2187 this. If we can prove that the array can never be superflat,
2188 we can just use the high bound of the index type. */
2189 else if ((Nkind (gnat_index) == N_Range
2190 && cannot_be_superflat_p (gnat_index))
2191 /* Packed Array Types are never superflat. */
2192 || Is_Packed_Array_Type (gnat_entity))
2195 /* Otherwise, if the high bound is constant but the low bound is
2196 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2197 lower bound. Note that the comparison must be done in the
2198 original type to avoid any overflow during the conversion. */
2199 else if (TREE_CODE (gnu_max) == INTEGER_CST
2200 && TREE_CODE (gnu_min) != INTEGER_CST)
2204 = build_cond_expr (sizetype,
2205 build_binary_op (GE_EXPR,
2210 size_binop (PLUS_EXPR, gnu_max,
2214 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2215 in all the other cases. Note that, here as well as above,
2216 the condition used in the comparison must be equivalent to
2217 the condition (length != 0). This is relied upon in order
2218 to optimize array comparisons in compare_arrays. */
2221 = build_cond_expr (sizetype,
2222 build_binary_op (GE_EXPR,
2227 size_binop (MINUS_EXPR, gnu_min,
2230 /* Reuse the index type for the range type. Then make an index
2231 type with the size range in sizetype. */
2232 gnu_index_types[index]
2233 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2236 /* Update the maximum size of the array in elements. Here we
2237 see if any constraint on the index type of the base type
2238 can be used in the case of self-referential bound on the
2239 index type of the subtype. We look for a non-"infinite"
2240 and non-self-referential bound from any type involved and
2241 handle each bound separately. */
2244 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2245 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2246 tree gnu_base_index_base_type
2247 = get_base_type (gnu_base_index_type);
2248 tree gnu_base_base_min
2249 = convert (sizetype,
2250 TYPE_MIN_VALUE (gnu_base_index_base_type));
2251 tree gnu_base_base_max
2252 = convert (sizetype,
2253 TYPE_MAX_VALUE (gnu_base_index_base_type));
2255 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2256 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2257 && !TREE_OVERFLOW (gnu_base_min)))
2258 gnu_base_min = gnu_min;
2260 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2261 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2262 && !TREE_OVERFLOW (gnu_base_max)))
2263 gnu_base_max = gnu_max;
2265 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2266 && TREE_OVERFLOW (gnu_base_min))
2267 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2268 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2269 && TREE_OVERFLOW (gnu_base_max))
2270 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2271 gnu_max_size = NULL_TREE;
2275 = size_binop (MAX_EXPR,
2276 size_binop (PLUS_EXPR, size_one_node,
2277 size_binop (MINUS_EXPR,
2282 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2283 && TREE_OVERFLOW (gnu_this_max))
2284 gnu_max_size = NULL_TREE;
2287 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2291 /* We need special types for debugging information to point to
2292 the index types if they have variable bounds, are not integer
2293 types, are biased or are wider than sizetype. */
2294 if (!integer_onep (gnu_orig_min)
2295 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2296 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2297 || (TREE_TYPE (gnu_index_type)
2298 && TREE_CODE (TREE_TYPE (gnu_index_type))
2300 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2301 || compare_tree_int (rm_size (gnu_index_type),
2302 TYPE_PRECISION (sizetype)) > 0)
2303 need_index_type_struct = true;
2306 /* Then flatten: create the array of arrays. For an array type
2307 used to implement a packed array, get the component type from
2308 the original array type since the representation clauses that
2309 can affect it are on the latter. */
2310 if (Is_Packed_Array_Type (gnat_entity)
2311 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2313 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2314 for (index = ndim - 1; index >= 0; index--)
2315 gnu_type = TREE_TYPE (gnu_type);
2317 /* One of the above calls might have caused us to be elaborated,
2318 so don't blow up if so. */
2319 if (present_gnu_tree (gnat_entity))
2321 maybe_present = true;
2327 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2330 /* One of the above calls might have caused us to be elaborated,
2331 so don't blow up if so. */
2332 if (present_gnu_tree (gnat_entity))
2334 maybe_present = true;
2339 /* Compute the maximum size of the array in units and bits. */
2342 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2343 TYPE_SIZE_UNIT (gnu_type));
2344 gnu_max_size = size_binop (MULT_EXPR,
2345 convert (bitsizetype, gnu_max_size),
2346 TYPE_SIZE (gnu_type));
2349 gnu_max_size_unit = NULL_TREE;
2351 /* Now build the array type. */
2352 for (index = ndim - 1; index >= 0; index --)
2354 gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
2355 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2356 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2357 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2360 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2361 TYPE_STUB_DECL (gnu_type)
2362 = create_type_stub_decl (gnu_entity_name, gnu_type);
2364 /* If we are at file level and this is a multi-dimensional array,
2365 we need to make a variable corresponding to the stride of the
2366 inner dimensions. */
2367 if (global_bindings_p () && ndim > 1)
2369 tree gnu_st_name = get_identifier ("ST");
2372 for (gnu_arr_type = TREE_TYPE (gnu_type);
2373 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2374 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2375 gnu_st_name = concat_name (gnu_st_name, "ST"))
2377 tree eltype = TREE_TYPE (gnu_arr_type);
2379 TYPE_SIZE (gnu_arr_type)
2380 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2381 gnat_entity, gnu_st_name,
2384 /* ??? For now, store the size as a multiple of the
2385 alignment of the element type in bytes so that we
2386 can see the alignment from the tree. */
2387 TYPE_SIZE_UNIT (gnu_arr_type)
2388 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2390 concat_name (gnu_st_name, "A_U"),
2392 TYPE_ALIGN (eltype));
2394 /* ??? create_type_decl is not invoked on the inner types so
2395 the MULT_EXPR node built above will never be marked. */
2396 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2400 /* If we need to write out a record type giving the names of the
2401 bounds for debugging purposes, do it now and make the record
2402 type a parallel type. This is not needed for a packed array
2403 since the bounds are conveyed by the original array type. */
2404 if (need_index_type_struct
2406 && !Is_Packed_Array_Type (gnat_entity))
2408 tree gnu_bound_rec = make_node (RECORD_TYPE);
2409 tree gnu_field_list = NULL_TREE;
2412 TYPE_NAME (gnu_bound_rec)
2413 = create_concat_name (gnat_entity, "XA");
2415 for (index = ndim - 1; index >= 0; index--)
2417 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2418 tree gnu_index_name = TYPE_NAME (gnu_index);
2420 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2421 gnu_index_name = DECL_NAME (gnu_index_name);
2423 /* Make sure to reference the types themselves, and not just
2424 their names, as the debugger may fall back on them. */
2425 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2426 gnu_bound_rec, NULL_TREE,
2428 TREE_CHAIN (gnu_field) = gnu_field_list;
2429 gnu_field_list = gnu_field;
2432 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2433 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2436 /* Otherwise, for a packed array, make the original array type a
2438 else if (debug_info_p
2439 && Is_Packed_Array_Type (gnat_entity)
2440 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2441 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2443 (Original_Array_Type (gnat_entity)));
2445 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2446 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2447 = (Is_Packed_Array_Type (gnat_entity)
2448 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2450 /* If the size is self-referential and the maximum size doesn't
2451 overflow, use it. */
2452 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2454 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2455 && TREE_OVERFLOW (gnu_max_size))
2456 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2457 && TREE_OVERFLOW (gnu_max_size_unit)))
2459 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2460 TYPE_SIZE (gnu_type));
2461 TYPE_SIZE_UNIT (gnu_type)
2462 = size_binop (MIN_EXPR, gnu_max_size_unit,
2463 TYPE_SIZE_UNIT (gnu_type));
2466 /* Set our alias set to that of our base type. This gives all
2467 array subtypes the same alias set. */
2468 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2470 /* If this is a packed type, make this type the same as the packed
2471 array type, but do some adjusting in the type first. */
2472 if (Present (Packed_Array_Type (gnat_entity)))
2474 Entity_Id gnat_index;
2477 /* First finish the type we had been making so that we output
2478 debugging information for it. */
2479 if (Treat_As_Volatile (gnat_entity))
2481 = build_qualified_type (gnu_type,
2482 TYPE_QUALS (gnu_type)
2483 | TYPE_QUAL_VOLATILE);
2485 /* Make it artificial only if the base type was artificial too.
2486 That's sort of "morally" true and will make it possible for
2487 the debugger to look it up by name in DWARF, which is needed
2488 in order to decode the packed array type. */
2490 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2491 !Comes_From_Source (Etype (gnat_entity))
2492 && !Comes_From_Source (gnat_entity),
2493 debug_info_p, gnat_entity);
2495 /* Save it as our equivalent in case the call below elaborates
2497 save_gnu_tree (gnat_entity, gnu_decl, false);
2499 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2501 this_made_decl = true;
2502 gnu_type = TREE_TYPE (gnu_decl);
2503 save_gnu_tree (gnat_entity, NULL_TREE, false);
2505 gnu_inner = gnu_type;
2506 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2507 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2508 || TYPE_PADDING_P (gnu_inner)))
2509 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2511 /* We need to attach the index type to the type we just made so
2512 that the actual bounds can later be put into a template. */
2513 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2514 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2515 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2516 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2518 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2520 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2521 TYPE_MODULUS for modular types so we make an extra
2522 subtype if necessary. */
2523 if (TYPE_MODULAR_P (gnu_inner))
2526 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2527 TREE_TYPE (gnu_subtype) = gnu_inner;
2528 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2529 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2530 TYPE_MIN_VALUE (gnu_inner));
2531 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2532 TYPE_MAX_VALUE (gnu_inner));
2533 gnu_inner = gnu_subtype;
2536 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2538 #ifdef ENABLE_CHECKING
2539 /* Check for other cases of overloading. */
2540 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2544 for (gnat_index = First_Index (gnat_entity);
2545 Present (gnat_index);
2546 gnat_index = Next_Index (gnat_index))
2547 SET_TYPE_ACTUAL_BOUNDS
2549 tree_cons (NULL_TREE,
2550 get_unpadded_type (Etype (gnat_index)),
2551 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2553 if (Convention (gnat_entity) != Convention_Fortran)
2554 SET_TYPE_ACTUAL_BOUNDS
2555 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2557 if (TREE_CODE (gnu_type) == RECORD_TYPE
2558 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2559 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2564 /* Abort if packed array with no Packed_Array_Type field set. */
2565 gcc_assert (!Is_Packed (gnat_entity));
2569 case E_String_Literal_Subtype:
2570 /* Create the type for a string literal. */
2572 Entity_Id gnat_full_type
2573 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2574 && Present (Full_View (Etype (gnat_entity)))
2575 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2576 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2577 tree gnu_string_array_type
2578 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2579 tree gnu_string_index_type
2580 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2581 (TYPE_DOMAIN (gnu_string_array_type))));
2582 tree gnu_lower_bound
2583 = convert (gnu_string_index_type,
2584 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2585 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2586 tree gnu_length = ssize_int (length - 1);
2587 tree gnu_upper_bound
2588 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2590 convert (gnu_string_index_type, gnu_length));
2592 = create_index_type (convert (sizetype, gnu_lower_bound),
2593 convert (sizetype, gnu_upper_bound),
2594 create_range_type (gnu_string_index_type,
2600 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2602 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2603 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2604 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2608 /* Record Types and Subtypes
2610 The following fields are defined on record types:
2612 Has_Discriminants True if the record has discriminants
2613 First_Discriminant Points to head of list of discriminants
2614 First_Entity Points to head of list of fields
2615 Is_Tagged_Type True if the record is tagged
2617 Implementation of Ada records and discriminated records:
2619 A record type definition is transformed into the equivalent of a C
2620 struct definition. The fields that are the discriminants which are
2621 found in the Full_Type_Declaration node and the elements of the
2622 Component_List found in the Record_Type_Definition node. The
2623 Component_List can be a recursive structure since each Variant of
2624 the Variant_Part of the Component_List has a Component_List.
2626 Processing of a record type definition comprises starting the list of
2627 field declarations here from the discriminants and the calling the
2628 function components_to_record to add the rest of the fields from the
2629 component list and return the gnu type node. The function
2630 components_to_record will call itself recursively as it traverses
2634 if (Has_Complex_Representation (gnat_entity))
2637 = build_complex_type
2639 (Etype (Defining_Entity
2640 (First (Component_Items
2643 (Declaration_Node (gnat_entity)))))))));
2649 Node_Id full_definition = Declaration_Node (gnat_entity);
2650 Node_Id record_definition = Type_Definition (full_definition);
2651 Entity_Id gnat_field;
2652 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2653 /* Set PACKED in keeping with gnat_to_gnu_field. */
2655 = Is_Packed (gnat_entity)
2657 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2659 : (Known_Alignment (gnat_entity)
2660 || (Strict_Alignment (gnat_entity)
2661 && Known_Static_Esize (gnat_entity)))
2664 bool has_discr = Has_Discriminants (gnat_entity);
2665 bool has_rep = Has_Specified_Layout (gnat_entity);
2666 bool all_rep = has_rep;
2668 = (Is_Tagged_Type (gnat_entity)
2669 && Nkind (record_definition) == N_Derived_Type_Definition);
2670 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2672 /* See if all fields have a rep clause. Stop when we find one
2675 for (gnat_field = First_Entity (gnat_entity);
2676 Present (gnat_field);
2677 gnat_field = Next_Entity (gnat_field))
2678 if ((Ekind (gnat_field) == E_Component
2679 || Ekind (gnat_field) == E_Discriminant)
2680 && No (Component_Clause (gnat_field)))
2686 /* If this is a record extension, go a level further to find the
2687 record definition. Also, verify we have a Parent_Subtype. */
2690 if (!type_annotate_only
2691 || Present (Record_Extension_Part (record_definition)))
2692 record_definition = Record_Extension_Part (record_definition);
2694 gcc_assert (type_annotate_only
2695 || Present (Parent_Subtype (gnat_entity)));
2698 /* Make a node for the record. If we are not defining the record,
2699 suppress expanding incomplete types. */
2700 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2701 TYPE_NAME (gnu_type) = gnu_entity_name;
2702 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2706 defer_incomplete_level++;
2707 this_deferred = true;
2710 /* If both a size and rep clause was specified, put the size in
2711 the record type now so that it can get the proper mode. */
2712 if (has_rep && Known_Esize (gnat_entity))
2713 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2715 /* Always set the alignment here so that it can be used to
2716 set the mode, if it is making the alignment stricter. If
2717 it is invalid, it will be checked again below. If this is to
2718 be Atomic, choose a default alignment of a word unless we know
2719 the size and it's smaller. */
2720 if (Known_Alignment (gnat_entity))
2721 TYPE_ALIGN (gnu_type)
2722 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2723 else if (Is_Atomic (gnat_entity))
2724 TYPE_ALIGN (gnu_type)
2725 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2726 /* If a type needs strict alignment, the minimum size will be the
2727 type size instead of the RM size (see validate_size). Cap the
2728 alignment, lest it causes this type size to become too large. */
2729 else if (Strict_Alignment (gnat_entity)
2730 && Known_Static_Esize (gnat_entity))
2732 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2733 unsigned int raw_align = raw_size & -raw_size;
2734 if (raw_align < BIGGEST_ALIGNMENT)
2735 TYPE_ALIGN (gnu_type) = raw_align;
2738 TYPE_ALIGN (gnu_type) = 0;
2740 /* If we have a Parent_Subtype, make a field for the parent. If
2741 this record has rep clauses, force the position to zero. */
2742 if (Present (Parent_Subtype (gnat_entity)))
2744 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2747 /* A major complexity here is that the parent subtype will
2748 reference our discriminants in its Discriminant_Constraint
2749 list. But those must reference the parent component of this
2750 record which is of the parent subtype we have not built yet!
2751 To break the circle we first build a dummy COMPONENT_REF which
2752 represents the "get to the parent" operation and initialize
2753 each of those discriminants to a COMPONENT_REF of the above
2754 dummy parent referencing the corresponding discriminant of the
2755 base type of the parent subtype. */
2756 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2757 build0 (PLACEHOLDER_EXPR, gnu_type),
2758 build_decl (input_location,
2759 FIELD_DECL, NULL_TREE,
2764 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2765 Present (gnat_field);
2766 gnat_field = Next_Stored_Discriminant (gnat_field))
2767 if (Present (Corresponding_Discriminant (gnat_field)))
2770 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2774 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2775 gnu_get_parent, gnu_field, NULL_TREE),
2779 /* Then we build the parent subtype. If it has discriminants but
2780 the type itself has unknown discriminants, this means that it
2781 doesn't contain information about how the discriminants are
2782 derived from those of the ancestor type, so it cannot be used
2783 directly. Instead it is built by cloning the parent subtype
2784 of the underlying record view of the type, for which the above
2785 derivation of discriminants has been made explicit. */
2786 if (Has_Discriminants (gnat_parent)
2787 && Has_Unknown_Discriminants (gnat_entity))
2789 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2791 /* If we are defining the type, the underlying record
2792 view must already have been elaborated at this point.
2793 Otherwise do it now as its parent subtype cannot be
2794 technically elaborated on its own. */
2796 gcc_assert (present_gnu_tree (gnat_uview));
2798 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2800 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2802 /* Substitute the "get to the parent" of the type for that
2803 of its underlying record view in the cloned type. */
2804 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2805 Present (gnat_field);
2806 gnat_field = Next_Stored_Discriminant (gnat_field))
2807 if (Present (Corresponding_Discriminant (gnat_field)))
2809 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2811 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2812 gnu_get_parent, gnu_field, NULL_TREE);
2814 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2818 gnu_parent = gnat_to_gnu_type (gnat_parent);
2820 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2821 initially built. The discriminants must reference the fields
2822 of the parent subtype and not those of its base type for the
2823 placeholder machinery to properly work. */
2826 /* The actual parent subtype is the full view. */
2827 if (IN (Ekind (gnat_parent), Private_Kind))
2829 if (Present (Full_View (gnat_parent)))
2830 gnat_parent = Full_View (gnat_parent);
2832 gnat_parent = Underlying_Full_View (gnat_parent);
2835 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2836 Present (gnat_field);
2837 gnat_field = Next_Stored_Discriminant (gnat_field))
2838 if (Present (Corresponding_Discriminant (gnat_field)))
2840 Entity_Id field = Empty;
2841 for (field = First_Stored_Discriminant (gnat_parent);
2843 field = Next_Stored_Discriminant (field))
2844 if (same_discriminant_p (gnat_field, field))
2846 gcc_assert (Present (field));
2847 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2848 = gnat_to_gnu_field_decl (field);
2852 /* The "get to the parent" COMPONENT_REF must be given its
2854 TREE_TYPE (gnu_get_parent) = gnu_parent;
2856 /* ...and reference the _Parent field of this record. */
2858 = create_field_decl (parent_name_id,
2859 gnu_parent, gnu_type,
2861 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2863 ? bitsize_zero_node : NULL_TREE,
2865 DECL_INTERNAL_P (gnu_field) = 1;
2866 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2867 TYPE_FIELDS (gnu_type) = gnu_field;
2870 /* Make the fields for the discriminants and put them into the record
2871 unless it's an Unchecked_Union. */
2873 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2874 Present (gnat_field);
2875 gnat_field = Next_Stored_Discriminant (gnat_field))
2877 /* If this is a record extension and this discriminant is the
2878 renaming of another discriminant, we've handled it above. */
2879 if (Present (Parent_Subtype (gnat_entity))
2880 && Present (Corresponding_Discriminant (gnat_field)))
2884 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
2887 /* Make an expression using a PLACEHOLDER_EXPR from the
2888 FIELD_DECL node just created and link that with the
2889 corresponding GNAT defining identifier. */
2890 save_gnu_tree (gnat_field,
2891 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2892 build0 (PLACEHOLDER_EXPR, gnu_type),
2893 gnu_field, NULL_TREE),
2896 if (!is_unchecked_union)
2898 TREE_CHAIN (gnu_field) = gnu_field_list;
2899 gnu_field_list = gnu_field;
2903 /* Add the fields into the record type and finish it up. */
2904 components_to_record (gnu_type, Component_List (record_definition),
2905 gnu_field_list, packed, definition, NULL,
2906 false, all_rep, is_unchecked_union,
2907 debug_info_p, false);
2909 /* If it is passed by reference, force BLKmode to ensure that objects
2910 of this type will always be put in memory. */
2911 if (Is_By_Reference_Type (gnat_entity))
2912 SET_TYPE_MODE (gnu_type, BLKmode);
2914 /* We used to remove the associations of the discriminants and _Parent
2915 for validity checking but we may need them if there's a Freeze_Node
2916 for a subtype used in this record. */
2917 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2919 /* Fill in locations of fields. */
2920 annotate_rep (gnat_entity, gnu_type);
2922 /* If there are any entities in the chain corresponding to components
2923 that we did not elaborate, ensure we elaborate their types if they
2925 for (gnat_temp = First_Entity (gnat_entity);
2926 Present (gnat_temp);
2927 gnat_temp = Next_Entity (gnat_temp))
2928 if ((Ekind (gnat_temp) == E_Component
2929 || Ekind (gnat_temp) == E_Discriminant)
2930 && Is_Itype (Etype (gnat_temp))
2931 && !present_gnu_tree (gnat_temp))
2932 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2934 /* If this is a record type associated with an exception definition,
2935 equate its fields to those of the standard exception type. This
2936 will make it possible to convert between them. */
2937 if (gnu_entity_name == exception_data_name_id)
2940 for (gnu_field = TYPE_FIELDS (gnu_type),
2941 gnu_std_field = TYPE_FIELDS (except_type_node);
2943 gnu_field = TREE_CHAIN (gnu_field),
2944 gnu_std_field = TREE_CHAIN (gnu_std_field))
2945 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
2946 gcc_assert (!gnu_std_field);
2951 case E_Class_Wide_Subtype:
2952 /* If an equivalent type is present, that is what we should use.
2953 Otherwise, fall through to handle this like a record subtype
2954 since it may have constraints. */
2955 if (gnat_equiv_type != gnat_entity)
2957 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2958 maybe_present = true;
2962 /* ... fall through ... */
2964 case E_Record_Subtype:
2965 /* If Cloned_Subtype is Present it means this record subtype has
2966 identical layout to that type or subtype and we should use
2967 that GCC type for this one. The front end guarantees that
2968 the component list is shared. */
2969 if (Present (Cloned_Subtype (gnat_entity)))
2971 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2973 maybe_present = true;
2977 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2978 changing the type, make a new type with each field having the type of
2979 the field in the new subtype but the position computed by transforming
2980 every discriminant reference according to the constraints. We don't
2981 see any difference between private and non-private type here since
2982 derivations from types should have been deferred until the completion
2983 of the private type. */
2986 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2991 defer_incomplete_level++;
2992 this_deferred = true;
2995 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2997 if (present_gnu_tree (gnat_entity))
2999 maybe_present = true;
3003 /* If this is a record subtype associated with a dispatch table,
3004 strip the suffix. This is necessary to make sure 2 different
3005 subtypes associated with the imported and exported views of a
3006 dispatch table are properly merged in LTO mode. */
3007 if (Is_Dispatch_Table_Entity (gnat_entity))
3010 Get_Encoded_Name (gnat_entity);
3011 p = strchr (Name_Buffer, '_');
3013 strcpy (p+2, "dtS");
3014 gnu_entity_name = get_identifier (Name_Buffer);
3017 /* When the subtype has discriminants and these discriminants affect
3018 the initial shape it has inherited, factor them in. But for an
3019 Unchecked_Union (it must be an Itype), just return the type.
3020 We can't just test Is_Constrained because private subtypes without
3021 discriminants of types with discriminants with default expressions
3022 are Is_Constrained but aren't constrained! */
3023 if (IN (Ekind (gnat_base_type), Record_Kind)
3024 && !Is_Unchecked_Union (gnat_base_type)
3025 && !Is_For_Access_Subtype (gnat_entity)
3026 && Is_Constrained (gnat_entity)
3027 && Has_Discriminants (gnat_entity)
3028 && Present (Discriminant_Constraint (gnat_entity))
3029 && Stored_Constraint (gnat_entity) != No_Elist)
3032 = build_subst_list (gnat_entity, gnat_base_type, definition);
3033 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3034 tree gnu_variant_list, gnu_pos_list, gnu_field_list = NULL_TREE;
3035 bool selected_variant = false;
3036 Entity_Id gnat_field;
3038 gnu_type = make_node (RECORD_TYPE);
3039 TYPE_NAME (gnu_type) = gnu_entity_name;
3041 /* Set the size, alignment and alias set of the new type to
3042 match that of the old one, doing required substitutions. */
3043 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3046 if (TYPE_IS_PADDING_P (gnu_base_type))
3047 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3049 gnu_unpad_base_type = gnu_base_type;
3051 /* Look for a REP part in the base type. */
3052 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3054 /* Look for a variant part in the base type. */
3055 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3057 /* If there is a variant part, we must compute whether the
3058 constraints statically select a particular variant. If
3059 so, we simply drop the qualified union and flatten the
3060 list of fields. Otherwise we'll build a new qualified
3061 union for the variants that are still relevant. */
3062 if (gnu_variant_part)
3065 = build_variant_list (TREE_TYPE (gnu_variant_part),
3066 gnu_subst_list, NULL_TREE);
3068 /* If all the qualifiers are unconditionally true, the
3069 innermost variant is statically selected. */
3070 selected_variant = true;
3071 for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
3072 if (!integer_onep (TREE_VEC_ELT (TREE_VALUE (t), 1)))
3074 selected_variant = false;
3078 /* Otherwise, create the new variants. */
3079 if (!selected_variant)
3080 for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
3082 tree old_variant = TREE_PURPOSE (t);
3083 tree new_variant = make_node (RECORD_TYPE);
3084 TYPE_NAME (new_variant)
3085 = DECL_NAME (TYPE_NAME (old_variant));
3086 copy_and_substitute_in_size (new_variant, old_variant,
3088 TREE_VEC_ELT (TREE_VALUE (t), 2) = new_variant;
3093 gnu_variant_list = NULL_TREE;
3094 selected_variant = false;
3098 = build_position_list (gnu_unpad_base_type,
3099 gnu_variant_list && !selected_variant,
3100 size_zero_node, bitsize_zero_node,
3101 BIGGEST_ALIGNMENT, NULL_TREE);
3103 for (gnat_field = First_Entity (gnat_entity);
3104 Present (gnat_field);
3105 gnat_field = Next_Entity (gnat_field))
3106 if ((Ekind (gnat_field) == E_Component
3107 || Ekind (gnat_field) == E_Discriminant)
3108 && !(Present (Corresponding_Discriminant (gnat_field))
3109 && Is_Tagged_Type (gnat_base_type))
3110 && Underlying_Type (Scope (Original_Record_Component
3114 Name_Id gnat_name = Chars (gnat_field);
3115 Entity_Id gnat_old_field
3116 = Original_Record_Component (gnat_field);
3118 = gnat_to_gnu_field_decl (gnat_old_field);
3119 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3120 tree gnu_field, gnu_field_type, gnu_size;
3121 tree gnu_cont_type, gnu_last = NULL_TREE;
3123 /* If the type is the same, retrieve the GCC type from the
3124 old field to take into account possible adjustments. */
3125 if (Etype (gnat_field) == Etype (gnat_old_field))
3126 gnu_field_type = TREE_TYPE (gnu_old_field);
3128 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3130 /* If there was a component clause, the field types must be
3131 the same for the type and subtype, so copy the data from
3132 the old field to avoid recomputation here. Also if the
3133 field is justified modular and the optimization in
3134 gnat_to_gnu_field was applied. */
3135 if (Present (Component_Clause (gnat_old_field))
3136 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3137 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3138 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3139 == TREE_TYPE (gnu_old_field)))
3141 gnu_size = DECL_SIZE (gnu_old_field);
3142 gnu_field_type = TREE_TYPE (gnu_old_field);
3145 /* If the old field was packed and of constant size, we
3146 have to get the old size here, as it might differ from
3147 what the Etype conveys and the latter might overlap
3148 onto the following field. Try to arrange the type for
3149 possible better packing along the way. */
3150 else if (DECL_PACKED (gnu_old_field)
3151 && TREE_CODE (DECL_SIZE (gnu_old_field))
3154 gnu_size = DECL_SIZE (gnu_old_field);
3155 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3156 && !TYPE_FAT_POINTER_P (gnu_field_type)
3157 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3159 = make_packable_type (gnu_field_type, true);
3163 gnu_size = TYPE_SIZE (gnu_field_type);
3165 /* If the context of the old field is the base type or its
3166 REP part (if any), put the field directly in the new
3167 type; otherwise look up the context in the variant list
3168 and put the field either in the new type if there is a
3169 selected variant or in one of the new variants. */
3170 if (gnu_context == gnu_unpad_base_type
3172 && gnu_context == TREE_TYPE (gnu_rep_part)))
3173 gnu_cont_type = gnu_type;
3176 t = purpose_member (gnu_context, gnu_variant_list);
3179 if (selected_variant)
3180 gnu_cont_type = gnu_type;
3182 gnu_cont_type = TREE_VEC_ELT (TREE_VALUE (t), 2);
3185 /* The front-end may pass us "ghost" components if
3186 it fails to recognize that a constrained subtype
3187 is statically constrained. Discard them. */
3191 /* Now create the new field modeled on the old one. */
3193 = create_field_decl_from (gnu_old_field, gnu_field_type,
3194 gnu_cont_type, gnu_size,
3195 gnu_pos_list, gnu_subst_list);
3197 /* Put it in one of the new variants directly. */
3198 if (gnu_cont_type != gnu_type)
3200 TREE_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3201 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3204 /* To match the layout crafted in components_to_record,
3205 if this is the _Tag or _Parent field, put it before
3206 any other fields. */
3207 else if (gnat_name == Name_uTag
3208 || gnat_name == Name_uParent)
3209 gnu_field_list = chainon (gnu_field_list, gnu_field);
3211 /* Similarly, if this is the _Controller field, put
3212 it before the other fields except for the _Tag or
3214 else if (gnat_name == Name_uController && gnu_last)
3216 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
3217 TREE_CHAIN (gnu_last) = gnu_field;
3220 /* Otherwise, if this is a regular field, put it after
3221 the other fields. */
3224 TREE_CHAIN (gnu_field) = gnu_field_list;
3225 gnu_field_list = gnu_field;
3227 gnu_last = gnu_field;
3230 save_gnu_tree (gnat_field, gnu_field, false);
3233 /* If there is a variant list and no selected variant, we need
3234 to create the nest of variant parts from the old nest. */
3235 if (gnu_variant_list && !selected_variant)
3237 tree new_variant_part
3238 = create_variant_part_from (gnu_variant_part,
3239 gnu_variant_list, gnu_type,
3240 gnu_pos_list, gnu_subst_list);
3241 TREE_CHAIN (new_variant_part) = gnu_field_list;
3242 gnu_field_list = new_variant_part;
3245 /* Now go through the entities again looking for Itypes that
3246 we have not elaborated but should (e.g., Etypes of fields
3247 that have Original_Components). */
3248 for (gnat_field = First_Entity (gnat_entity);
3249 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3250 if ((Ekind (gnat_field) == E_Discriminant
3251 || Ekind (gnat_field) == E_Component)
3252 && !present_gnu_tree (Etype (gnat_field)))
3253 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3255 /* Do not emit debug info for the type yet since we're going to
3257 gnu_field_list = nreverse (gnu_field_list);
3258 finish_record_type (gnu_type, gnu_field_list, 2, false);
3260 /* See the E_Record_Type case for the rationale. */
3261 if (Is_By_Reference_Type (gnat_entity))
3262 SET_TYPE_MODE (gnu_type, BLKmode);
3264 compute_record_mode (gnu_type);
3266 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3268 /* Fill in locations of fields. */
3269 annotate_rep (gnat_entity, gnu_type);
3271 /* If debugging information is being written for the type, write
3272 a record that shows what we are a subtype of and also make a
3273 variable that indicates our size, if still variable. */
3276 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3277 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3278 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3280 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3281 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3283 TYPE_NAME (gnu_subtype_marker)
3284 = create_concat_name (gnat_entity, "XVS");
3285 finish_record_type (gnu_subtype_marker,
3286 create_field_decl (gnu_unpad_base_name,
3287 build_reference_type
3288 (gnu_unpad_base_type),
3290 NULL_TREE, NULL_TREE,
3294 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3295 gnu_subtype_marker);
3298 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3299 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3300 TYPE_SIZE_UNIT (gnu_subtype_marker)
3301 = create_var_decl (create_concat_name (gnat_entity,
3303 NULL_TREE, sizetype, gnu_size_unit,
3304 false, false, false, false, NULL,
3308 /* Now we can finalize it. */
3309 rest_of_record_type_compilation (gnu_type);
3312 /* Otherwise, go down all the components in the new type and make
3313 them equivalent to those in the base type. */
3316 gnu_type = gnu_base_type;
3318 for (gnat_temp = First_Entity (gnat_entity);
3319 Present (gnat_temp);
3320 gnat_temp = Next_Entity (gnat_temp))
3321 if ((Ekind (gnat_temp) == E_Discriminant
3322 && !Is_Unchecked_Union (gnat_base_type))
3323 || Ekind (gnat_temp) == E_Component)
3324 save_gnu_tree (gnat_temp,
3325 gnat_to_gnu_field_decl
3326 (Original_Record_Component (gnat_temp)),
3332 case E_Access_Subprogram_Type:
3333 /* Use the special descriptor type for dispatch tables if needed,
3334 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3335 Note that we are only required to do so for static tables in
3336 order to be compatible with the C++ ABI, but Ada 2005 allows
3337 to extend library level tagged types at the local level so
3338 we do it in the non-static case as well. */
3339 if (TARGET_VTABLE_USES_DESCRIPTORS
3340 && Is_Dispatch_Table_Entity (gnat_entity))
3342 gnu_type = fdesc_type_node;
3343 gnu_size = TYPE_SIZE (gnu_type);
3347 /* ... fall through ... */
3349 case E_Anonymous_Access_Subprogram_Type:
3350 /* If we are not defining this entity, and we have incomplete
3351 entities being processed above us, make a dummy type and
3352 fill it in later. */
3353 if (!definition && defer_incomplete_level != 0)
3355 struct incomplete *p
3356 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3359 = build_pointer_type
3360 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3361 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3362 !Comes_From_Source (gnat_entity),
3363 debug_info_p, gnat_entity);
3364 this_made_decl = true;
3365 gnu_type = TREE_TYPE (gnu_decl);
3366 save_gnu_tree (gnat_entity, gnu_decl, false);
3369 p->old_type = TREE_TYPE (gnu_type);
3370 p->full_type = Directly_Designated_Type (gnat_entity);
3371 p->next = defer_incomplete_list;
3372 defer_incomplete_list = p;
3376 /* ... fall through ... */
3378 case E_Allocator_Type:
3380 case E_Access_Attribute_Type:
3381 case E_Anonymous_Access_Type:
3382 case E_General_Access_Type:
3384 /* The designated type and its equivalent type for gigi. */
3385 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3386 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3387 /* Whether it comes from a limited with. */
3388 bool is_from_limited_with
3389 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3390 && From_With_Type (gnat_desig_equiv));
3391 /* The "full view" of the designated type. If this is an incomplete
3392 entity from a limited with, treat its non-limited view as the full
3393 view. Otherwise, if this is an incomplete or private type, use the
3394 full view. In the former case, we might point to a private type,
3395 in which case, we need its full view. Also, we want to look at the
3396 actual type used for the representation, so this takes a total of
3398 Entity_Id gnat_desig_full_direct_first
3399 = (is_from_limited_with
3400 ? Non_Limited_View (gnat_desig_equiv)
3401 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3402 ? Full_View (gnat_desig_equiv) : Empty));
3403 Entity_Id gnat_desig_full_direct
3404 = ((is_from_limited_with
3405 && Present (gnat_desig_full_direct_first)
3406 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3407 ? Full_View (gnat_desig_full_direct_first)
3408 : gnat_desig_full_direct_first);
3409 Entity_Id gnat_desig_full
3410 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3411 /* The type actually used to represent the designated type, either
3412 gnat_desig_full or gnat_desig_equiv. */
3413 Entity_Id gnat_desig_rep;
3414 /* True if this is a pointer to an unconstrained array. */
3415 bool is_unconstrained_array;
3416 /* We want to know if we'll be seeing the freeze node for any
3417 incomplete type we may be pointing to. */
3419 = (Present (gnat_desig_full)
3420 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3421 : In_Extended_Main_Code_Unit (gnat_desig_type));
3422 /* True if we make a dummy type here. */
3423 bool made_dummy = false;
3424 /* True if the dummy type is a fat pointer. */
3425 bool got_fat_p = false;
3426 /* The mode to be used for the pointer type. */
3427 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3428 /* The GCC type used for the designated type. */
3429 tree gnu_desig_type = NULL_TREE;
3431 if (!targetm.valid_pointer_mode (p_mode))
3434 /* If either the designated type or its full view is an unconstrained
3435 array subtype, replace it with the type it's a subtype of. This
3436 avoids problems with multiple copies of unconstrained array types.
3437 Likewise, if the designated type is a subtype of an incomplete
3438 record type, use the parent type to avoid order of elaboration
3439 issues. This can lose some code efficiency, but there is no
3441 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3442 && !Is_Constrained (gnat_desig_equiv))
3443 gnat_desig_equiv = Etype (gnat_desig_equiv);
3444 if (Present (gnat_desig_full)
3445 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3446 && !Is_Constrained (gnat_desig_full))
3447 || (Ekind (gnat_desig_full) == E_Record_Subtype
3448 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3449 gnat_desig_full = Etype (gnat_desig_full);
3451 /* Set the type that's actually the representation of the designated
3452 type and also flag whether we have a unconstrained array. */
3454 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3455 is_unconstrained_array
3456 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3458 /* If we are pointing to an incomplete type whose completion is an
3459 unconstrained array, make a fat pointer type. The two types in our
3460 fields will be pointers to dummy nodes and will be replaced in
3461 update_pointer_to. Similarly, if the type itself is a dummy type or
3462 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3463 in case we have any thin pointers to it. */
3464 if (is_unconstrained_array
3465 && (Present (gnat_desig_full)
3466 || (present_gnu_tree (gnat_desig_equiv)
3468 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3470 && defer_incomplete_level
3471 && !present_gnu_tree (gnat_desig_equiv))
3473 && is_from_limited_with
3474 && Present (Freeze_Node (gnat_desig_equiv)))))
3476 if (present_gnu_tree (gnat_desig_rep))
3477 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3480 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3481 /* Show the dummy we get will be a fat pointer. */
3482 got_fat_p = made_dummy = true;
3485 /* If the call above got something that has a pointer, the pointer
3486 is our type. This could have happened either because the type
3487 was elaborated or because somebody else executed the code. */
3488 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3491 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3492 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3493 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3494 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3497 TYPE_NAME (gnu_template_type)
3498 = create_concat_name (gnat_desig_equiv, "XUB");
3499 TYPE_DUMMY_P (gnu_template_type) = 1;
3501 TYPE_NAME (gnu_array_type)
3502 = create_concat_name (gnat_desig_equiv, "XUA");
3503 TYPE_DUMMY_P (gnu_array_type) = 1;
3505 gnu_type = make_node (RECORD_TYPE);
3506 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_desig_type);
3507 TYPE_POINTER_TO (gnu_desig_type) = gnu_type;
3510 = create_field_decl (get_identifier ("P_ARRAY"),
3511 gnu_ptr_array, gnu_type,
3512 NULL_TREE, NULL_TREE, 0, 0);
3514 = create_field_decl (get_identifier ("P_BOUNDS"),
3515 gnu_ptr_template, gnu_type,
3516 NULL_TREE, NULL_TREE, 0, 0);
3518 /* Make sure we can place this into a register. */
3519 TYPE_ALIGN (gnu_type)
3520 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3521 TYPE_FAT_POINTER_P (gnu_type) = 1;
3523 /* Do not emit debug info for this record type since the types
3524 of its fields are incomplete. */
3525 finish_record_type (gnu_type, fields, 0, false);
3527 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)
3528 = make_node (RECORD_TYPE);
3529 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type))
3530 = create_concat_name (gnat_desig_equiv, "XUT");
3531 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)) = 1;
3535 /* If we already know what the full type is, use it. */
3536 else if (Present (gnat_desig_full)
3537 && present_gnu_tree (gnat_desig_full))
3538 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3540 /* Get the type of the thing we are to point to and build a pointer to
3541 it. If it is a reference to an incomplete or private type with a
3542 full view that is a record, make a dummy type node and get the
3543 actual type later when we have verified it is safe. */
3544 else if ((!in_main_unit
3545 && !present_gnu_tree (gnat_desig_equiv)
3546 && Present (gnat_desig_full)
3547 && !present_gnu_tree (gnat_desig_full)
3548 && Is_Record_Type (gnat_desig_full))
3549 /* Likewise if we are pointing to a record or array and we are
3550 to defer elaborating incomplete types. We do this as this
3551 access type may be the full view of a private type. Note
3552 that the unconstrained array case is handled above. */
3553 || ((!in_main_unit || imported_p)
3554 && defer_incomplete_level
3555 && !present_gnu_tree (gnat_desig_equiv)
3556 && (Is_Record_Type (gnat_desig_rep)
3557 || Is_Array_Type (gnat_desig_rep)))
3558 /* If this is a reference from a limited_with type back to our
3559 main unit and there's a freeze node for it, either we have
3560 already processed the declaration and made the dummy type,
3561 in which case we just reuse the latter, or we have not yet,
3562 in which case we make the dummy type and it will be reused
3563 when the declaration is finally processed. In both cases,
3564 the pointer eventually created below will be automatically
3565 adjusted when the freeze node is processed. Note that the
3566 unconstrained array case is handled above. */
3568 && is_from_limited_with
3569 && Present (Freeze_Node (gnat_desig_rep))))
3571 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3575 /* Otherwise handle the case of a pointer to itself. */
3576 else if (gnat_desig_equiv == gnat_entity)
3579 = build_pointer_type_for_mode (void_type_node, p_mode,
3580 No_Strict_Aliasing (gnat_entity));
3581 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3584 /* If expansion is disabled, the equivalent type of a concurrent type
3585 is absent, so build a dummy pointer type. */
3586 else if (type_annotate_only && No (gnat_desig_equiv))
3587 gnu_type = ptr_void_type_node;
3589 /* Finally, handle the default case where we can just elaborate our
3592 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3594 /* It is possible that a call to gnat_to_gnu_type above resolved our
3595 type. If so, just return it. */
3596 if (present_gnu_tree (gnat_entity))
3598 maybe_present = true;
3602 /* If we have not done it yet, build the pointer type the usual way. */
3605 /* Modify the designated type if we are pointing only to constant
3606 objects, but don't do it for unconstrained arrays. */
3607 if (Is_Access_Constant (gnat_entity)
3608 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3611 = build_qualified_type
3613 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3615 /* Some extra processing is required if we are building a
3616 pointer to an incomplete type (in the GCC sense). We might
3617 have such a type if we just made a dummy, or directly out
3618 of the call to gnat_to_gnu_type above if we are processing
3619 an access type for a record component designating the
3620 record type itself. */
3621 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3623 /* We must ensure that the pointer to variant we make will
3624 be processed by update_pointer_to when the initial type
3625 is completed. Pretend we made a dummy and let further
3626 processing act as usual. */
3629 /* We must ensure that update_pointer_to will not retrieve
3630 the dummy variant when building a properly qualified
3631 version of the complete type. We take advantage of the
3632 fact that get_qualified_type is requiring TYPE_NAMEs to
3633 match to influence build_qualified_type and then also
3634 update_pointer_to here. */
3635 TYPE_NAME (gnu_desig_type)
3636 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3641 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3642 No_Strict_Aliasing (gnat_entity));
3645 /* If we are not defining this object and we have made a dummy pointer,
3646 save our current definition, evaluate the actual type, and replace
3647 the tentative type we made with the actual one. If we are to defer
3648 actually looking up the actual type, make an entry in the deferred
3649 list. If this is from a limited with, we have to defer to the end
3650 of the current spec in two cases: first if the designated type is
3651 in the current unit and second if the access type itself is. */
3652 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3654 bool is_from_limited_with_in_main_unit
3655 = (is_from_limited_with
3657 || In_Extended_Main_Code_Unit (gnat_entity)));
3658 tree gnu_old_desig_type
3659 = TYPE_IS_FAT_POINTER_P (gnu_type)
3660 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3662 if (esize == POINTER_SIZE
3663 && (got_fat_p || TYPE_IS_FAT_POINTER_P (gnu_type)))
3665 = build_pointer_type
3666 (TYPE_OBJECT_RECORD_TYPE
3667 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3669 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3670 !Comes_From_Source (gnat_entity),
3671 debug_info_p, gnat_entity);
3672 this_made_decl = true;
3673 gnu_type = TREE_TYPE (gnu_decl);
3674 save_gnu_tree (gnat_entity, gnu_decl, false);
3677 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3678 update gnu_old_desig_type directly, in which case it will not be
3679 a dummy type any more when we get into update_pointer_to.
3681 This can happen e.g. when the designated type is a record type,
3682 because their elaboration starts with an initial node from
3683 make_dummy_type, which may be the same node as the one we got.
3685 Besides, variants of this non-dummy type might have been created
3686 along the way. update_pointer_to is expected to properly take
3687 care of those situations. */
3688 if (!defer_incomplete_level && !is_from_limited_with_in_main_unit)
3689 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3690 gnat_to_gnu_type (gnat_desig_equiv));
3693 struct incomplete *p = XNEW (struct incomplete);
3694 struct incomplete **head
3695 = (is_from_limited_with_in_main_unit
3696 ? &defer_limited_with : &defer_incomplete_list);
3697 p->old_type = gnu_old_desig_type;
3698 p->full_type = gnat_desig_equiv;
3706 case E_Access_Protected_Subprogram_Type:
3707 case E_Anonymous_Access_Protected_Subprogram_Type:
3708 if (type_annotate_only && No (gnat_equiv_type))
3709 gnu_type = ptr_void_type_node;
3712 /* The runtime representation is the equivalent type. */
3713 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3714 maybe_present = true;
3717 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3718 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3719 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3720 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3721 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3726 case E_Access_Subtype:
3728 /* We treat this as identical to its base type; any constraint is
3729 meaningful only to the front end.
3731 The designated type must be elaborated as well, if it does
3732 not have its own freeze node. Designated (sub)types created
3733 for constrained components of records with discriminants are
3734 not frozen by the front end and thus not elaborated by gigi,
3735 because their use may appear before the base type is frozen,
3736 and because it is not clear that they are needed anywhere in
3737 Gigi. With the current model, there is no correct place where
3738 they could be elaborated. */
3740 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3741 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3742 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3743 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3744 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3746 /* If we are not defining this entity, and we have incomplete
3747 entities being processed above us, make a dummy type and
3748 elaborate it later. */
3749 if (!definition && defer_incomplete_level != 0)
3751 struct incomplete *p
3752 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3754 = build_pointer_type
3755 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3757 p->old_type = TREE_TYPE (gnu_ptr_type);
3758 p->full_type = Directly_Designated_Type (gnat_entity);
3759 p->next = defer_incomplete_list;
3760 defer_incomplete_list = p;
3762 else if (!IN (Ekind (Base_Type
3763 (Directly_Designated_Type (gnat_entity))),
3764 Incomplete_Or_Private_Kind))
3765 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3769 maybe_present = true;
3772 /* Subprogram Entities
3774 The following access functions are defined for subprograms (functions
3777 First_Formal The first formal parameter.
3778 Is_Imported Indicates that the subprogram has appeared in
3779 an INTERFACE or IMPORT pragma. For now we
3780 assume that the external language is C.
3781 Is_Exported Likewise but for an EXPORT pragma.
3782 Is_Inlined True if the subprogram is to be inlined.
3784 In addition for function subprograms we have:
3786 Etype Return type of the function.
3788 Each parameter is first checked by calling must_pass_by_ref on its
3789 type to determine if it is passed by reference. For parameters which
3790 are copied in, if they are Ada In Out or Out parameters, their return
3791 value becomes part of a record which becomes the return type of the
3792 function (C function - note that this applies only to Ada procedures
3793 so there is no Ada return type). Additional code to store back the
3794 parameters will be generated on the caller side. This transformation
3795 is done here, not in the front-end.
3797 The intended result of the transformation can be seen from the
3798 equivalent source rewritings that follow:
3800 struct temp {int a,b};
3801 procedure P (A,B: In Out ...) is temp P (int A,B)
3804 end P; return {A,B};
3811 For subprogram types we need to perform mainly the same conversions to
3812 GCC form that are needed for procedures and function declarations. The
3813 only difference is that at the end, we make a type declaration instead
3814 of a function declaration. */
3816 case E_Subprogram_Type:
3820 /* The first GCC parameter declaration (a PARM_DECL node). The
3821 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3822 actually is the head of this parameter list. */
3823 tree gnu_param_list = NULL_TREE;
3824 /* Likewise for the stub associated with an exported procedure. */
3825 tree gnu_stub_param_list = NULL_TREE;
3826 /* The type returned by a function. If the subprogram is a procedure
3827 this type should be void_type_node. */
3828 tree gnu_return_type = void_type_node;
3829 /* List of fields in return type of procedure with copy-in copy-out
3831 tree gnu_field_list = NULL_TREE;
3832 /* Non-null for subprograms containing parameters passed by copy-in
3833 copy-out (Ada In Out or Out parameters not passed by reference),
3834 in which case it is the list of nodes used to specify the values
3835 of the In Out/Out parameters that are returned as a record upon
3836 procedure return. The TREE_PURPOSE of an element of this list is
3837 a field of the record and the TREE_VALUE is the PARM_DECL
3838 corresponding to that field. This list will be saved in the
3839 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3840 tree gnu_cico_list = NULL_TREE;
3841 /* If an import pragma asks to map this subprogram to a GCC builtin,
3842 this is the builtin DECL node. */
3843 tree gnu_builtin_decl = NULL_TREE;
3844 /* For the stub associated with an exported procedure. */
3845 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3846 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3847 Entity_Id gnat_param;
3848 bool inline_flag = Is_Inlined (gnat_entity);
3849 bool public_flag = Is_Public (gnat_entity) || imported_p;
3851 = (Is_Public (gnat_entity) && !definition) || imported_p;
3853 /* The semantics of "pure" in Ada essentially matches that of "const"
3854 in the back-end. In particular, both properties are orthogonal to
3855 the "nothrow" property if the EH circuitry is explicit in the
3856 internal representation of the back-end. If we are to completely
3857 hide the EH circuitry from it, we need to declare that calls to pure
3858 Ada subprograms that can throw have side effects since they can
3859 trigger an "abnormal" transfer of control flow; thus they can be
3860 neither "const" nor "pure" in the back-end sense. */
3862 = (Exception_Mechanism == Back_End_Exceptions
3863 && Is_Pure (gnat_entity));
3865 bool volatile_flag = No_Return (gnat_entity);
3866 bool return_by_direct_ref_p = false;
3867 bool return_by_invisi_ref_p = false;
3868 bool return_unconstrained_p = false;
3869 bool has_copy_in_out = false;
3870 bool has_stub = false;
3873 /* A parameter may refer to this type, so defer completion of any
3874 incomplete types. */
3875 if (kind == E_Subprogram_Type && !definition)
3877 defer_incomplete_level++;
3878 this_deferred = true;
3881 /* If the subprogram has an alias, it is probably inherited, so
3882 we can use the original one. If the original "subprogram"
3883 is actually an enumeration literal, it may be the first use
3884 of its type, so we must elaborate that type now. */
3885 if (Present (Alias (gnat_entity)))
3887 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3888 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3890 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3893 /* Elaborate any Itypes in the parameters of this entity. */
3894 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3895 Present (gnat_temp);
3896 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3897 if (Is_Itype (Etype (gnat_temp)))
3898 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3903 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3904 corresponding DECL node.
3906 We still want the parameter associations to take place because the
3907 proper generation of calls depends on it (a GNAT parameter without
3908 a corresponding GCC tree has a very specific meaning), so we don't
3910 if (Convention (gnat_entity) == Convention_Intrinsic)
3911 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3913 /* ??? What if we don't find the builtin node above ? warn ? err ?
3914 In the current state we neither warn nor err, and calls will just
3915 be handled as for regular subprograms. */
3917 if (kind == E_Function || kind == E_Subprogram_Type)
3918 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3920 /* If this function returns by reference, make the actual return
3921 type of this function the pointer and mark the decl. */
3922 if (Returns_By_Ref (gnat_entity))
3924 gnu_return_type = build_pointer_type (gnu_return_type);
3925 return_by_direct_ref_p = true;
3928 /* If the Mechanism is By_Reference, ensure this function uses the
3929 target's by-invisible-reference mechanism, which may not be the
3930 same as above (e.g. it might be passing an extra parameter).
3932 Prior to GCC 4, this was handled by just setting TREE_ADDRESSABLE
3933 on the result type. Everything required to pass by invisible
3934 reference using the target's mechanism (e.g. an extra parameter)
3935 was handled at RTL expansion time.
3937 This doesn't work with GCC 4 any more for several reasons. First,
3938 the gimplification process might need to create temporaries of this
3939 type and the gimplifier ICEs on such attempts; that's why the flag
3940 is now set on the function type instead. Second, the middle-end
3941 now also relies on a different attribute, DECL_BY_REFERENCE on the
3942 RESULT_DECL, and expects the by-invisible-reference-ness to be made
3943 explicit in the function body. */
3944 else if (kind == E_Function && Mechanism (gnat_entity) == By_Reference)
3945 return_by_invisi_ref_p = true;
3947 /* If we are supposed to return an unconstrained array, actually return
3948 a fat pointer and make a note of that. */
3949 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3951 gnu_return_type = TREE_TYPE (gnu_return_type);
3952 return_unconstrained_p = true;
3955 /* If the type requires a transient scope, the result is allocated
3956 on the secondary stack, so the result type of the function is
3958 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3960 gnu_return_type = build_pointer_type (gnu_return_type);
3961 return_unconstrained_p = true;
3964 /* If the type is a padded type and the underlying type would not
3965 be passed by reference or this function has a foreign convention,
3966 return the underlying type. */
3967 else if (TYPE_IS_PADDING_P (gnu_return_type)
3968 && (!default_pass_by_ref (TREE_TYPE
3969 (TYPE_FIELDS (gnu_return_type)))
3970 || Has_Foreign_Convention (gnat_entity)))
3971 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3973 /* If the return type is unconstrained, that means it must have a
3974 maximum size. Use the padded type as the effective return type.
3975 And ensure the function uses the target's by-invisible-reference
3976 mechanism to avoid copying too much data when it returns. */
3977 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3980 = maybe_pad_type (gnu_return_type,
3981 max_size (TYPE_SIZE (gnu_return_type), true),
3982 0, gnat_entity, false, false, false, true);
3983 return_by_invisi_ref_p = true;
3986 /* If the return type has a size that overflows, we cannot have
3987 a function that returns that type. This usage doesn't make
3988 sense anyway, so give an error here. */
3989 if (TYPE_SIZE_UNIT (gnu_return_type)
3990 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3991 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3993 post_error ("cannot return type whose size overflows",
3995 gnu_return_type = copy_node (gnu_return_type);
3996 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3997 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3998 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3999 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4002 /* Look at all our parameters and get the type of
4003 each. While doing this, build a copy-out structure if
4006 /* Loop over the parameters and get their associated GCC tree.
4007 While doing this, build a copy-out structure if we need one. */
4008 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4009 Present (gnat_param);
4010 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4012 tree gnu_param_name = get_entity_name (gnat_param);
4013 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4014 tree gnu_param, gnu_field;
4015 bool copy_in_copy_out = false;
4016 Mechanism_Type mech = Mechanism (gnat_param);
4018 /* Builtins are expanded inline and there is no real call sequence
4019 involved. So the type expected by the underlying expander is
4020 always the type of each argument "as is". */
4021 if (gnu_builtin_decl)
4023 /* Handle the first parameter of a valued procedure specially. */
4024 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4025 mech = By_Copy_Return;
4026 /* Otherwise, see if a Mechanism was supplied that forced this
4027 parameter to be passed one way or another. */
4028 else if (mech == Default
4029 || mech == By_Copy || mech == By_Reference)
4031 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4032 mech = By_Descriptor;
4034 else if (By_Short_Descriptor_Last <= mech &&
4035 mech <= By_Short_Descriptor)
4036 mech = By_Short_Descriptor;
4040 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4041 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4042 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4044 mech = By_Reference;
4050 post_error ("unsupported mechanism for&", gnat_param);
4055 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4056 Has_Foreign_Convention (gnat_entity),
4059 /* We are returned either a PARM_DECL or a type if no parameter
4060 needs to be passed; in either case, adjust the type. */
4061 if (DECL_P (gnu_param))
4062 gnu_param_type = TREE_TYPE (gnu_param);
4065 gnu_param_type = gnu_param;
4066 gnu_param = NULL_TREE;
4071 /* If it's an exported subprogram, we build a parameter list
4072 in parallel, in case we need to emit a stub for it. */
4073 if (Is_Exported (gnat_entity))
4076 = chainon (gnu_param, gnu_stub_param_list);
4077 /* Change By_Descriptor parameter to By_Reference for
4078 the internal version of an exported subprogram. */
4079 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4082 = gnat_to_gnu_param (gnat_param, By_Reference,
4088 gnu_param = copy_node (gnu_param);
4091 gnu_param_list = chainon (gnu_param, gnu_param_list);
4092 Sloc_to_locus (Sloc (gnat_param),
4093 &DECL_SOURCE_LOCATION (gnu_param));
4094 save_gnu_tree (gnat_param, gnu_param, false);
4096 /* If a parameter is a pointer, this function may modify
4097 memory through it and thus shouldn't be considered
4098 a const function. Also, the memory may be modified
4099 between two calls, so they can't be CSE'ed. The latter
4100 case also handles by-ref parameters. */
4101 if (POINTER_TYPE_P (gnu_param_type)
4102 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4106 if (copy_in_copy_out)
4108 if (!has_copy_in_out)
4110 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4111 gnu_return_type = make_node (RECORD_TYPE);
4112 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4113 /* Set a default alignment to speed up accesses. */
4114 TYPE_ALIGN (gnu_return_type)
4115 = get_mode_alignment (ptr_mode);
4116 has_copy_in_out = true;
4120 = create_field_decl (gnu_param_name, gnu_param_type,
4121 gnu_return_type, NULL_TREE, NULL_TREE,
4123 Sloc_to_locus (Sloc (gnat_param),
4124 &DECL_SOURCE_LOCATION (gnu_field));
4125 TREE_CHAIN (gnu_field) = gnu_field_list;
4126 gnu_field_list = gnu_field;
4128 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4132 /* Do not compute record for out parameters if subprogram is
4133 stubbed since structures are incomplete for the back-end. */
4134 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4135 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4138 /* If we have a CICO list but it has only one entry, we convert
4139 this function into a function that simply returns that one
4141 if (list_length (gnu_cico_list) == 1)
4142 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4144 if (Has_Stdcall_Convention (gnat_entity))
4145 prepend_one_attribute_to
4146 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4147 get_identifier ("stdcall"), NULL_TREE,
4150 /* If we are on a target where stack realignment is needed for 'main'
4151 to honor GCC's implicit expectations (stack alignment greater than
4152 what the base ABI guarantees), ensure we do the same for foreign
4153 convention subprograms as they might be used as callbacks from code
4154 breaking such expectations. Note that this applies to task entry
4155 points in particular. */
4156 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4157 && Has_Foreign_Convention (gnat_entity))
4158 prepend_one_attribute_to
4159 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4160 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4163 /* The lists have been built in reverse. */
4164 gnu_param_list = nreverse (gnu_param_list);
4166 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4167 gnu_cico_list = nreverse (gnu_cico_list);
4169 if (Ekind (gnat_entity) == E_Function)
4170 Set_Mechanism (gnat_entity, return_unconstrained_p
4171 || return_by_direct_ref_p
4172 || return_by_invisi_ref_p
4173 ? By_Reference : By_Copy);
4175 = create_subprog_type (gnu_return_type, gnu_param_list,
4176 gnu_cico_list, return_unconstrained_p,
4177 return_by_direct_ref_p,
4178 return_by_invisi_ref_p);
4182 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4183 gnu_cico_list, return_unconstrained_p,
4184 return_by_direct_ref_p,
4185 return_by_invisi_ref_p);
4187 /* A subprogram (something that doesn't return anything) shouldn't
4188 be considered const since there would be no reason for such a
4189 subprogram. Note that procedures with Out (or In Out) parameters
4190 have already been converted into a function with a return type. */
4191 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4195 = build_qualified_type (gnu_type,
4196 TYPE_QUALS (gnu_type)
4197 | (TYPE_QUAL_CONST * const_flag)
4198 | (TYPE_QUAL_VOLATILE * volatile_flag));
4202 = build_qualified_type (gnu_stub_type,
4203 TYPE_QUALS (gnu_stub_type)
4204 | (TYPE_QUAL_CONST * const_flag)
4205 | (TYPE_QUAL_VOLATILE * volatile_flag));
4207 /* If we have a builtin decl for that function, check the signatures
4208 compatibilities. If the signatures are compatible, use the builtin
4209 decl. If they are not, we expect the checker predicate to have
4210 posted the appropriate errors, and just continue with what we have
4212 if (gnu_builtin_decl)
4214 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
4216 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
4218 gnu_decl = gnu_builtin_decl;
4219 gnu_type = gnu_builtin_type;
4224 /* If there was no specified Interface_Name and the external and
4225 internal names of the subprogram are the same, only use the
4226 internal name to allow disambiguation of nested subprograms. */
4227 if (No (Interface_Name (gnat_entity))
4228 && gnu_ext_name == gnu_entity_name)
4229 gnu_ext_name = NULL_TREE;
4231 /* If we are defining the subprogram and it has an Address clause
4232 we must get the address expression from the saved GCC tree for the
4233 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4234 the address expression here since the front-end has guaranteed
4235 in that case that the elaboration has no effects. If there is
4236 an Address clause and we are not defining the object, just
4237 make it a constant. */
4238 if (Present (Address_Clause (gnat_entity)))
4240 tree gnu_address = NULL_TREE;
4244 = (present_gnu_tree (gnat_entity)
4245 ? get_gnu_tree (gnat_entity)
4246 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4248 save_gnu_tree (gnat_entity, NULL_TREE, false);
4250 /* Convert the type of the object to a reference type that can
4251 alias everything as per 13.3(19). */
4253 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4255 gnu_address = convert (gnu_type, gnu_address);
4258 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4259 gnu_address, false, Is_Public (gnat_entity),
4260 extern_flag, false, NULL, gnat_entity);
4261 DECL_BY_REF_P (gnu_decl) = 1;
4264 else if (kind == E_Subprogram_Type)
4265 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4266 !Comes_From_Source (gnat_entity),
4267 debug_info_p, gnat_entity);
4272 gnu_stub_name = gnu_ext_name;
4273 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4274 public_flag = false;
4277 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4278 gnu_type, gnu_param_list,
4279 inline_flag, public_flag,
4280 extern_flag, attr_list,
4285 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4286 gnu_stub_type, gnu_stub_param_list,
4288 extern_flag, attr_list,
4290 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4293 /* This is unrelated to the stub built right above. */
4294 DECL_STUBBED_P (gnu_decl)
4295 = Convention (gnat_entity) == Convention_Stubbed;
4300 case E_Incomplete_Type:
4301 case E_Incomplete_Subtype:
4302 case E_Private_Type:
4303 case E_Private_Subtype:
4304 case E_Limited_Private_Type:
4305 case E_Limited_Private_Subtype:
4306 case E_Record_Type_With_Private:
4307 case E_Record_Subtype_With_Private:
4309 /* Get the "full view" of this entity. If this is an incomplete
4310 entity from a limited with, treat its non-limited view as the
4311 full view. Otherwise, use either the full view or the underlying
4312 full view, whichever is present. This is used in all the tests
4315 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4316 && From_With_Type (gnat_entity))
4317 ? Non_Limited_View (gnat_entity)
4318 : Present (Full_View (gnat_entity))
4319 ? Full_View (gnat_entity)
4320 : Underlying_Full_View (gnat_entity);
4322 /* If this is an incomplete type with no full view, it must be a Taft
4323 Amendment type, in which case we return a dummy type. Otherwise,
4324 just get the type from its Etype. */
4327 if (kind == E_Incomplete_Type)
4329 gnu_type = make_dummy_type (gnat_entity);
4330 gnu_decl = TYPE_STUB_DECL (gnu_type);
4334 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4336 maybe_present = true;
4341 /* If we already made a type for the full view, reuse it. */
4342 else if (present_gnu_tree (full_view))
4344 gnu_decl = get_gnu_tree (full_view);
4348 /* Otherwise, if we are not defining the type now, get the type
4349 from the full view. But always get the type from the full view
4350 for define on use types, since otherwise we won't see them! */
4351 else if (!definition
4352 || (Is_Itype (full_view)
4353 && No (Freeze_Node (gnat_entity)))
4354 || (Is_Itype (gnat_entity)
4355 && No (Freeze_Node (full_view))))
4357 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4358 maybe_present = true;
4362 /* For incomplete types, make a dummy type entry which will be
4363 replaced later. Save it as the full declaration's type so
4364 we can do any needed updates when we see it. */
4365 gnu_type = make_dummy_type (gnat_entity);
4366 gnu_decl = TYPE_STUB_DECL (gnu_type);
4367 save_gnu_tree (full_view, gnu_decl, 0);
4371 case E_Class_Wide_Type:
4372 /* Class-wide types are always transformed into their root type. */
4373 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4374 maybe_present = true;
4378 case E_Task_Subtype:
4379 case E_Protected_Type:
4380 case E_Protected_Subtype:
4381 if (type_annotate_only && No (gnat_equiv_type))
4382 gnu_type = void_type_node;
4384 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4386 maybe_present = true;
4390 gnu_decl = create_label_decl (gnu_entity_name);
4395 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4396 we've already saved it, so we don't try to. */
4397 gnu_decl = error_mark_node;
4405 /* If we had a case where we evaluated another type and it might have
4406 defined this one, handle it here. */
4407 if (maybe_present && present_gnu_tree (gnat_entity))
4409 gnu_decl = get_gnu_tree (gnat_entity);
4413 /* If we are processing a type and there is either no decl for it or
4414 we just made one, do some common processing for the type, such as
4415 handling alignment and possible padding. */
4416 if (is_type && (!gnu_decl || this_made_decl))
4418 /* Tell the middle-end that objects of tagged types are guaranteed to
4419 be properly aligned. This is necessary because conversions to the
4420 class-wide type are translated into conversions to the root type,
4421 which can be less aligned than some of its derived types. */
4422 if (Is_Tagged_Type (gnat_entity)
4423 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4424 TYPE_ALIGN_OK (gnu_type) = 1;
4426 /* If the type is passed by reference, objects of this type must be
4427 fully addressable and cannot be copied. */
4428 if (Is_By_Reference_Type (gnat_entity))
4429 TREE_ADDRESSABLE (gnu_type) = 1;
4431 /* ??? Don't set the size for a String_Literal since it is either
4432 confirming or we don't handle it properly (if the low bound is
4434 if (!gnu_size && kind != E_String_Literal_Subtype)
4435 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4437 Has_Size_Clause (gnat_entity));
4439 /* If a size was specified, see if we can make a new type of that size
4440 by rearranging the type, for example from a fat to a thin pointer. */
4444 = make_type_from_size (gnu_type, gnu_size,
4445 Has_Biased_Representation (gnat_entity));
4447 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4448 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4452 /* If the alignment hasn't already been processed and this is
4453 not an unconstrained array, see if an alignment is specified.
4454 If not, we pick a default alignment for atomic objects. */
4455 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4457 else if (Known_Alignment (gnat_entity))
4459 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4460 TYPE_ALIGN (gnu_type));
4462 /* Warn on suspiciously large alignments. This should catch
4463 errors about the (alignment,byte)/(size,bit) discrepancy. */
4464 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4468 /* If a size was specified, take it into account. Otherwise
4469 use the RM size for records as the type size has already
4470 been adjusted to the alignment. */
4473 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4474 || TREE_CODE (gnu_type) == UNION_TYPE
4475 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4476 && !TYPE_FAT_POINTER_P (gnu_type))
4477 size = rm_size (gnu_type);
4479 size = TYPE_SIZE (gnu_type);
4481 /* Consider an alignment as suspicious if the alignment/size
4482 ratio is greater or equal to the byte/bit ratio. */
4483 if (host_integerp (size, 1)
4484 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4485 post_error_ne ("?suspiciously large alignment specified for&",
4486 Expression (Alignment_Clause (gnat_entity)),
4490 else if (Is_Atomic (gnat_entity) && !gnu_size
4491 && host_integerp (TYPE_SIZE (gnu_type), 1)
4492 && integer_pow2p (TYPE_SIZE (gnu_type)))
4493 align = MIN (BIGGEST_ALIGNMENT,
4494 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4495 else if (Is_Atomic (gnat_entity) && gnu_size
4496 && host_integerp (gnu_size, 1)
4497 && integer_pow2p (gnu_size))
4498 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4500 /* See if we need to pad the type. If we did, and made a record,
4501 the name of the new type may be changed. So get it back for
4502 us when we make the new TYPE_DECL below. */
4503 if (gnu_size || align > 0)
4504 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4505 false, !gnu_decl, definition, false);
4507 if (TYPE_IS_PADDING_P (gnu_type))
4509 gnu_entity_name = TYPE_NAME (gnu_type);
4510 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4511 gnu_entity_name = DECL_NAME (gnu_entity_name);
4514 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4516 /* If we are at global level, GCC will have applied variable_size to
4517 the type, but that won't have done anything. So, if it's not
4518 a constant or self-referential, call elaborate_expression_1 to
4519 make a variable for the size rather than calculating it each time.
4520 Handle both the RM size and the actual size. */
4521 if (global_bindings_p ()
4522 && TYPE_SIZE (gnu_type)
4523 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4524 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4526 tree size = TYPE_SIZE (gnu_type);
4528 TYPE_SIZE (gnu_type)
4529 = elaborate_expression_1 (size, gnat_entity,
4530 get_identifier ("SIZE"),
4533 /* ??? For now, store the size as a multiple of the alignment in
4534 bytes so that we can see the alignment from the tree. */
4535 TYPE_SIZE_UNIT (gnu_type)
4536 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4537 get_identifier ("SIZE_A_UNIT"),
4539 TYPE_ALIGN (gnu_type));
4541 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4542 may not be marked by the call to create_type_decl below. */
4543 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4545 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4547 tree variant_part = get_variant_part (gnu_type);
4548 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4552 tree union_type = TREE_TYPE (variant_part);
4553 tree offset = DECL_FIELD_OFFSET (variant_part);
4555 /* If the position of the variant part is constant, subtract
4556 it from the size of the type of the parent to get the new
4557 size. This manual CSE reduces the data size. */
4558 if (TREE_CODE (offset) == INTEGER_CST)
4560 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4561 TYPE_SIZE (union_type)
4562 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4563 bit_from_pos (offset, bitpos));
4564 TYPE_SIZE_UNIT (union_type)
4565 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4566 byte_from_pos (offset, bitpos));
4570 TYPE_SIZE (union_type)
4571 = elaborate_expression_1 (TYPE_SIZE (union_type),
4573 get_identifier ("VSIZE"),
4576 /* ??? For now, store the size as a multiple of the
4577 alignment in bytes so that we can see the alignment
4579 TYPE_SIZE_UNIT (union_type)
4580 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4585 TYPE_ALIGN (union_type));
4587 /* ??? For now, store the offset as a multiple of the
4588 alignment in bytes so that we can see the alignment
4590 DECL_FIELD_OFFSET (variant_part)
4591 = elaborate_expression_2 (offset,
4593 get_identifier ("VOFFSET"),
4599 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4600 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4603 if (operand_equal_p (ada_size, size, 0))
4604 ada_size = TYPE_SIZE (gnu_type);
4607 = elaborate_expression_1 (ada_size, gnat_entity,
4608 get_identifier ("RM_SIZE"),
4610 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4614 /* If this is a record type or subtype, call elaborate_expression_1 on
4615 any field position. Do this for both global and local types.
4616 Skip any fields that we haven't made trees for to avoid problems with
4617 class wide types. */
4618 if (IN (kind, Record_Kind))
4619 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4620 gnat_temp = Next_Entity (gnat_temp))
4621 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4623 tree gnu_field = get_gnu_tree (gnat_temp);
4625 /* ??? For now, store the offset as a multiple of the alignment
4626 in bytes so that we can see the alignment from the tree. */
4627 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4629 DECL_FIELD_OFFSET (gnu_field)
4630 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4632 get_identifier ("OFFSET"),
4634 DECL_OFFSET_ALIGN (gnu_field));
4636 /* ??? The context of gnu_field is not necessarily gnu_type
4637 so the MULT_EXPR node built above may not be marked by
4638 the call to create_type_decl below. */
4639 if (global_bindings_p ())
4640 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4644 if (Treat_As_Volatile (gnat_entity))
4646 = build_qualified_type (gnu_type,
4647 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4649 if (Is_Atomic (gnat_entity))
4650 check_ok_for_atomic (gnu_type, gnat_entity, false);
4652 if (Present (Alignment_Clause (gnat_entity)))
4653 TYPE_USER_ALIGN (gnu_type) = 1;
4655 if (Universal_Aliasing (gnat_entity))
4656 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4659 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4660 !Comes_From_Source (gnat_entity),
4661 debug_info_p, gnat_entity);
4664 TREE_TYPE (gnu_decl) = gnu_type;
4665 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4669 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4671 gnu_type = TREE_TYPE (gnu_decl);
4673 /* If this is a derived type, relate its alias set to that of its parent
4674 to avoid troubles when a call to an inherited primitive is inlined in
4675 a context where a derived object is accessed. The inlined code works
4676 on the parent view so the resulting code may access the same object
4677 using both the parent and the derived alias sets, which thus have to
4678 conflict. As the same issue arises with component references, the
4679 parent alias set also has to conflict with composite types enclosing
4680 derived components. For instance, if we have:
4687 we want T to conflict with both D and R, in addition to R being a
4688 superset of D by record/component construction.
4690 One way to achieve this is to perform an alias set copy from the
4691 parent to the derived type. This is not quite appropriate, though,
4692 as we don't want separate derived types to conflict with each other:
4694 type I1 is new Integer;
4695 type I2 is new Integer;
4697 We want I1 and I2 to both conflict with Integer but we do not want
4698 I1 to conflict with I2, and an alias set copy on derivation would
4701 The option chosen is to make the alias set of the derived type a
4702 superset of that of its parent type. It trivially fulfills the
4703 simple requirement for the Integer derivation example above, and
4704 the component case as well by superset transitivity:
4707 R ----------> D ----------> T
4709 However, for composite types, conversions between derived types are
4710 translated into VIEW_CONVERT_EXPRs so a sequence like:
4712 type Comp1 is new Comp;
4713 type Comp2 is new Comp;
4714 procedure Proc (C : Comp1);
4722 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4724 and gimplified into:
4731 i.e. generates code involving type punning. Therefore, Comp1 needs
4732 to conflict with Comp2 and an alias set copy is required.
4734 The language rules ensure the parent type is already frozen here. */
4735 if (Is_Derived_Type (gnat_entity))
4737 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4738 relate_alias_sets (gnu_type, gnu_parent_type,
4739 Is_Composite_Type (gnat_entity)
4740 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4743 /* Back-annotate the Alignment of the type if not already in the
4744 tree. Likewise for sizes. */
4745 if (Unknown_Alignment (gnat_entity))
4747 unsigned int double_align, align;
4748 bool is_capped_double, align_clause;
4750 /* If the default alignment of "double" or larger scalar types is
4751 specifically capped and this is not an array with an alignment
4752 clause on the component type, return the cap. */
4753 if ((double_align = double_float_alignment) > 0)
4755 = is_double_float_or_array (gnat_entity, &align_clause);
4756 else if ((double_align = double_scalar_alignment) > 0)
4758 = is_double_scalar_or_array (gnat_entity, &align_clause);
4760 is_capped_double = align_clause = false;
4762 if (is_capped_double && !align_clause)
4763 align = double_align;
4765 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4767 Set_Alignment (gnat_entity, UI_From_Int (align));
4770 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4772 tree gnu_size = TYPE_SIZE (gnu_type);
4774 /* If the size is self-referential, annotate the maximum value. */
4775 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4776 gnu_size = max_size (gnu_size, true);
4778 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4780 /* In this mode, the tag and the parent components are not
4781 generated by the front-end so the sizes must be adjusted. */
4782 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
4785 if (Is_Derived_Type (gnat_entity))
4787 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
4789 Set_Alignment (gnat_entity,
4790 Alignment (Etype (Base_Type (gnat_entity))));
4793 offset = pointer_size;
4795 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4796 gnu_size = size_binop (MULT_EXPR, pointer_size,
4797 size_binop (CEIL_DIV_EXPR,
4800 uint_size = annotate_value (gnu_size);
4801 Set_Esize (gnat_entity, uint_size);
4802 Set_RM_Size (gnat_entity, uint_size);
4805 Set_Esize (gnat_entity, annotate_value (gnu_size));
4808 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4809 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4812 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4813 DECL_ARTIFICIAL (gnu_decl) = 1;
4815 if (!debug_info_p && DECL_P (gnu_decl)
4816 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4817 && No (Renamed_Object (gnat_entity)))
4818 DECL_IGNORED_P (gnu_decl) = 1;
4820 /* If we haven't already, associate the ..._DECL node that we just made with
4821 the input GNAT entity node. */
4823 save_gnu_tree (gnat_entity, gnu_decl, false);
4825 /* If this is an enumeration or floating-point type, we were not able to set
4826 the bounds since they refer to the type. These are always static. */
4827 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4828 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4830 tree gnu_scalar_type = gnu_type;
4831 tree gnu_low_bound, gnu_high_bound;
4833 /* If this is a padded type, we need to use the underlying type. */
4834 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4835 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4837 /* If this is a floating point type and we haven't set a floating
4838 point type yet, use this in the evaluation of the bounds. */
4839 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4840 longest_float_type_node = gnu_scalar_type;
4842 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4843 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4845 if (kind == E_Enumeration_Type)
4847 /* Enumeration types have specific RM bounds. */
4848 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4849 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4851 /* Write full debugging information. Since this has both a
4852 typedef and a tag, avoid outputting the name twice. */
4853 DECL_ARTIFICIAL (gnu_decl) = 1;
4854 rest_of_type_decl_compilation (gnu_decl);
4859 /* Floating-point types don't have specific RM bounds. */
4860 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4861 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4865 /* If we deferred processing of incomplete types, re-enable it. If there
4866 were no other disables and we have some to process, do so. */
4867 if (this_deferred && --defer_incomplete_level == 0)
4869 if (defer_incomplete_list)
4871 struct incomplete *incp, *next;
4873 /* We are back to level 0 for the deferring of incomplete types.
4874 But processing these incomplete types below may itself require
4875 deferring, so preserve what we have and restart from scratch. */
4876 incp = defer_incomplete_list;
4877 defer_incomplete_list = NULL;
4879 /* For finalization, however, all types must be complete so we
4880 cannot do the same because deferred incomplete types may end up
4881 referencing each other. Process them all recursively first. */
4882 defer_finalize_level++;
4884 for (; incp; incp = next)
4889 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4890 gnat_to_gnu_type (incp->full_type));
4894 defer_finalize_level--;
4897 /* All the deferred incomplete types have been processed so we can
4898 now proceed with the finalization of the deferred types. */
4899 if (defer_finalize_level == 0 && defer_finalize_list)
4904 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4905 rest_of_type_decl_compilation_no_defer (t);
4907 VEC_free (tree, heap, defer_finalize_list);
4911 /* If we are not defining this type, see if it's in the incomplete list.
4912 If so, handle that list entry now. */
4913 else if (!definition)
4915 struct incomplete *incp;
4917 for (incp = defer_incomplete_list; incp; incp = incp->next)
4918 if (incp->old_type && incp->full_type == gnat_entity)
4920 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4921 TREE_TYPE (gnu_decl));
4922 incp->old_type = NULL_TREE;
4929 /* If this is a packed array type whose original array type is itself
4930 an Itype without freeze node, make sure the latter is processed. */
4931 if (Is_Packed_Array_Type (gnat_entity)
4932 && Is_Itype (Original_Array_Type (gnat_entity))
4933 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4934 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4935 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
4940 /* Similar, but if the returned value is a COMPONENT_REF, return the
4944 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4946 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4948 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4949 gnu_field = TREE_OPERAND (gnu_field, 1);
4954 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4955 the GCC type corresponding to that entity. */
4958 gnat_to_gnu_type (Entity_Id gnat_entity)
4962 /* The back end never attempts to annotate generic types. */
4963 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4964 return void_type_node;
4966 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4967 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4969 return TREE_TYPE (gnu_decl);
4972 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4973 the unpadded version of the GCC type corresponding to that entity. */
4976 get_unpadded_type (Entity_Id gnat_entity)
4978 tree type = gnat_to_gnu_type (gnat_entity);
4980 if (TYPE_IS_PADDING_P (type))
4981 type = TREE_TYPE (TYPE_FIELDS (type));
4986 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4987 Every TYPE_DECL generated for a type definition must be passed
4988 to this function once everything else has been done for it. */
4991 rest_of_type_decl_compilation (tree decl)
4993 /* We need to defer finalizing the type if incomplete types
4994 are being deferred or if they are being processed. */
4995 if (defer_incomplete_level || defer_finalize_level)
4996 VEC_safe_push (tree, heap, defer_finalize_list, decl);
4998 rest_of_type_decl_compilation_no_defer (decl);
5001 /* Same as above but without deferring the compilation. This
5002 function should not be invoked directly on a TYPE_DECL. */
5005 rest_of_type_decl_compilation_no_defer (tree decl)
5007 const int toplev = global_bindings_p ();
5008 tree t = TREE_TYPE (decl);
5010 rest_of_decl_compilation (decl, toplev, 0);
5012 /* Now process all the variants. This is needed for STABS. */
5013 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5015 if (t == TREE_TYPE (decl))
5018 if (!TYPE_STUB_DECL (t))
5019 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5021 rest_of_type_compilation (t, toplev);
5025 /* Finalize any From_With_Type incomplete types. We do this after processing
5026 our compilation unit and after processing its spec, if this is a body. */
5029 finalize_from_with_types (void)
5031 struct incomplete *incp = defer_limited_with;
5032 struct incomplete *next;
5034 defer_limited_with = 0;
5035 for (; incp; incp = next)
5039 if (incp->old_type != 0)
5040 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5041 gnat_to_gnu_type (incp->full_type));
5046 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5047 kind of type (such E_Task_Type) that has a different type which Gigi
5048 uses for its representation. If the type does not have a special type
5049 for its representation, return GNAT_ENTITY. If a type is supposed to
5050 exist, but does not, abort unless annotating types, in which case
5051 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5054 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5056 Entity_Id gnat_equiv = gnat_entity;
5058 if (No (gnat_entity))
5061 switch (Ekind (gnat_entity))
5063 case E_Class_Wide_Subtype:
5064 if (Present (Equivalent_Type (gnat_entity)))
5065 gnat_equiv = Equivalent_Type (gnat_entity);
5068 case E_Access_Protected_Subprogram_Type:
5069 case E_Anonymous_Access_Protected_Subprogram_Type:
5070 gnat_equiv = Equivalent_Type (gnat_entity);
5073 case E_Class_Wide_Type:
5074 gnat_equiv = Root_Type (gnat_entity);
5078 case E_Task_Subtype:
5079 case E_Protected_Type:
5080 case E_Protected_Subtype:
5081 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5088 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5092 /* Return a GCC tree for a type corresponding to the component type of the
5093 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5094 is for an array being defined. DEBUG_INFO_P is true if we need to write
5095 debug information for other types that we may create in the process. */
5098 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5101 tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
5104 /* Try to get a smaller form of the component if needed. */
5105 if ((Is_Packed (gnat_array)
5106 || Has_Component_Size_Clause (gnat_array))
5107 && !Is_Bit_Packed_Array (gnat_array)
5108 && !Has_Aliased_Components (gnat_array)
5109 && !Strict_Alignment (Component_Type (gnat_array))
5110 && TREE_CODE (gnu_type) == RECORD_TYPE
5111 && !TYPE_FAT_POINTER_P (gnu_type)
5112 && host_integerp (TYPE_SIZE (gnu_type), 1))
5113 gnu_type = make_packable_type (gnu_type, false);
5115 if (Has_Atomic_Components (gnat_array))
5116 check_ok_for_atomic (gnu_type, gnat_array, true);
5118 /* Get and validate any specified Component_Size. */
5120 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5121 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5122 true, Has_Component_Size_Clause (gnat_array));
5124 /* If the array has aliased components and the component size can be zero,
5125 force at least unit size to ensure that the components have distinct
5128 && Has_Aliased_Components (gnat_array)
5129 && (integer_zerop (TYPE_SIZE (gnu_type))
5130 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5131 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5133 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5135 /* If the component type is a RECORD_TYPE that has a self-referential size,
5136 then use the maximum size for the component size. */
5138 && TREE_CODE (gnu_type) == RECORD_TYPE
5139 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5140 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5142 /* Honor the component size. This is not needed for bit-packed arrays. */
5143 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5145 tree orig_type = gnu_type;
5146 unsigned int max_align;
5148 /* If an alignment is specified, use it as a cap on the component type
5149 so that it can be honored for the whole type. But ignore it for the
5150 original type of packed array types. */
5151 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5152 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5156 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5157 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5158 gnu_type = orig_type;
5160 orig_type = gnu_type;
5162 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5163 true, false, definition, true);
5165 /* If a padding record was made, declare it now since it will never be
5166 declared otherwise. This is necessary to ensure that its subtrees
5167 are properly marked. */
5168 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5169 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5170 debug_info_p, gnat_array);
5173 if (Has_Volatile_Components (Base_Type (gnat_array)))
5175 = build_qualified_type (gnu_type,
5176 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5181 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5182 using MECH as its passing mechanism, to be placed in the parameter
5183 list built for GNAT_SUBPROG. Assume a foreign convention for the
5184 latter if FOREIGN is true. Also set CICO to true if the parameter
5185 must use the copy-in copy-out implementation mechanism.
5187 The returned tree is a PARM_DECL, except for those cases where no
5188 parameter needs to be actually passed to the subprogram; the type
5189 of this "shadow" parameter is then returned instead. */
5192 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5193 Entity_Id gnat_subprog, bool foreign, bool *cico)
5195 tree gnu_param_name = get_entity_name (gnat_param);
5196 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5197 tree gnu_param_type_alt = NULL_TREE;
5198 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5199 /* The parameter can be indirectly modified if its address is taken. */
5200 bool ro_param = in_param && !Address_Taken (gnat_param);
5201 bool by_return = false, by_component_ptr = false, by_ref = false;
5204 /* Copy-return is used only for the first parameter of a valued procedure.
5205 It's a copy mechanism for which a parameter is never allocated. */
5206 if (mech == By_Copy_Return)
5208 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5213 /* If this is either a foreign function or if the underlying type won't
5214 be passed by reference, strip off possible padding type. */
5215 if (TYPE_IS_PADDING_P (gnu_param_type))
5217 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5219 if (mech == By_Reference
5221 || (!must_pass_by_ref (unpadded_type)
5222 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5223 gnu_param_type = unpadded_type;
5226 /* If this is a read-only parameter, make a variant of the type that is
5227 read-only. ??? However, if this is an unconstrained array, that type
5228 can be very complex, so skip it for now. Likewise for any other
5229 self-referential type. */
5231 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5232 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5233 gnu_param_type = build_qualified_type (gnu_param_type,
5234 (TYPE_QUALS (gnu_param_type)
5235 | TYPE_QUAL_CONST));
5237 /* For foreign conventions, pass arrays as pointers to the element type.
5238 First check for unconstrained array and get the underlying array. */
5239 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5241 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5243 /* VMS descriptors are themselves passed by reference. */
5244 if (mech == By_Short_Descriptor ||
5245 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5247 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5248 Mechanism (gnat_param),
5250 else if (mech == By_Descriptor)
5252 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5253 chosen in fill_vms_descriptor. */
5255 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5256 Mechanism (gnat_param),
5259 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5260 Mechanism (gnat_param),
5264 /* Arrays are passed as pointers to element type for foreign conventions. */
5267 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5269 /* Strip off any multi-dimensional entries, then strip
5270 off the last array to get the component type. */
5271 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5272 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5273 gnu_param_type = TREE_TYPE (gnu_param_type);
5275 by_component_ptr = true;
5276 gnu_param_type = TREE_TYPE (gnu_param_type);
5279 gnu_param_type = build_qualified_type (gnu_param_type,
5280 (TYPE_QUALS (gnu_param_type)
5281 | TYPE_QUAL_CONST));
5283 gnu_param_type = build_pointer_type (gnu_param_type);
5286 /* Fat pointers are passed as thin pointers for foreign conventions. */
5287 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5289 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5291 /* If we must pass or were requested to pass by reference, do so.
5292 If we were requested to pass by copy, do so.
5293 Otherwise, for foreign conventions, pass In Out or Out parameters
5294 or aggregates by reference. For COBOL and Fortran, pass all
5295 integer and FP types that way too. For Convention Ada, use
5296 the standard Ada default. */
5297 else if (must_pass_by_ref (gnu_param_type)
5298 || mech == By_Reference
5301 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5303 && (Convention (gnat_subprog) == Convention_Fortran
5304 || Convention (gnat_subprog) == Convention_COBOL)
5305 && (INTEGRAL_TYPE_P (gnu_param_type)
5306 || FLOAT_TYPE_P (gnu_param_type)))
5308 && default_pass_by_ref (gnu_param_type)))))
5310 gnu_param_type = build_reference_type (gnu_param_type);
5314 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5318 if (mech == By_Copy && (by_ref || by_component_ptr))
5319 post_error ("?cannot pass & by copy", gnat_param);
5321 /* If this is an Out parameter that isn't passed by reference and isn't
5322 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5323 it will be a VAR_DECL created when we process the procedure, so just
5324 return its type. For the special parameter of a valued procedure,
5327 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5328 Out parameters with discriminants or implicit initial values to be
5329 handled like In Out parameters. These type are normally built as
5330 aggregates, hence passed by reference, except for some packed arrays
5331 which end up encoded in special integer types.
5333 The exception we need to make is then for packed arrays of records
5334 with discriminants or implicit initial values. We have no light/easy
5335 way to check for the latter case, so we merely check for packed arrays
5336 of records. This may lead to useless copy-in operations, but in very
5337 rare cases only, as these would be exceptions in a set of already
5338 exceptional situations. */
5339 if (Ekind (gnat_param) == E_Out_Parameter
5342 || (mech != By_Descriptor
5343 && mech != By_Short_Descriptor
5344 && !POINTER_TYPE_P (gnu_param_type)
5345 && !AGGREGATE_TYPE_P (gnu_param_type)))
5346 && !(Is_Array_Type (Etype (gnat_param))
5347 && Is_Packed (Etype (gnat_param))
5348 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5349 return gnu_param_type;
5351 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5352 ro_param || by_ref || by_component_ptr);
5353 DECL_BY_REF_P (gnu_param) = by_ref;
5354 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5355 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5356 mech == By_Short_Descriptor);
5357 DECL_POINTS_TO_READONLY_P (gnu_param)
5358 = (ro_param && (by_ref || by_component_ptr));
5360 /* Save the alternate descriptor type, if any. */
5361 if (gnu_param_type_alt)
5362 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5364 /* If no Mechanism was specified, indicate what we're using, then
5365 back-annotate it. */
5366 if (mech == Default)
5367 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5369 Set_Mechanism (gnat_param, mech);
5373 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5376 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5378 while (Present (Corresponding_Discriminant (discr1)))
5379 discr1 = Corresponding_Discriminant (discr1);
5381 while (Present (Corresponding_Discriminant (discr2)))
5382 discr2 = Corresponding_Discriminant (discr2);
5385 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5388 /* Return true if the array type GNU_TYPE, which represents a dimension of
5389 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5392 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5394 /* If the array type is not the innermost dimension of the GNAT type,
5395 then it has a non-aliased component. */
5396 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5397 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5400 /* If the array type has an aliased component in the front-end sense,
5401 then it also has an aliased component in the back-end sense. */
5402 if (Has_Aliased_Components (gnat_type))
5405 /* If this is a derived type, then it has a non-aliased component if
5406 and only if its parent type also has one. */
5407 if (Is_Derived_Type (gnat_type))
5409 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5411 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5413 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5414 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5415 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5416 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5419 /* Otherwise, rely exclusively on properties of the element type. */
5420 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5423 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5426 compile_time_known_address_p (Node_Id gnat_address)
5428 /* Catch System'To_Address. */
5429 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5430 gnat_address = Expression (gnat_address);
5432 return Compile_Time_Known_Value (gnat_address);
5435 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5436 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5439 cannot_be_superflat_p (Node_Id gnat_range)
5441 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5442 Node_Id scalar_range;
5443 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5445 /* If the low bound is not constant, try to find an upper bound. */
5446 while (Nkind (gnat_lb) != N_Integer_Literal
5447 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5448 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5449 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5450 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5451 || Nkind (scalar_range) == N_Range))
5452 gnat_lb = High_Bound (scalar_range);
5454 /* If the high bound is not constant, try to find a lower bound. */
5455 while (Nkind (gnat_hb) != N_Integer_Literal
5456 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5457 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5458 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5459 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5460 || Nkind (scalar_range) == N_Range))
5461 gnat_hb = Low_Bound (scalar_range);
5463 /* If we have failed to find constant bounds, punt. */
5464 if (Nkind (gnat_lb) != N_Integer_Literal
5465 || Nkind (gnat_hb) != N_Integer_Literal)
5468 /* We need at least a signed 64-bit type to catch most cases. */
5469 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5470 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5471 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5474 /* If the low bound is the smallest integer, nothing can be smaller. */
5475 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5476 if (TREE_OVERFLOW (gnu_lb_minus_one))
5479 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5482 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5485 constructor_address_p (tree gnu_expr)
5487 while (TREE_CODE (gnu_expr) == NOP_EXPR
5488 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5489 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5490 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5492 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5493 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5496 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5497 be elaborated at the point of its definition, but do nothing else. */
5500 elaborate_entity (Entity_Id gnat_entity)
5502 switch (Ekind (gnat_entity))
5504 case E_Signed_Integer_Subtype:
5505 case E_Modular_Integer_Subtype:
5506 case E_Enumeration_Subtype:
5507 case E_Ordinary_Fixed_Point_Subtype:
5508 case E_Decimal_Fixed_Point_Subtype:
5509 case E_Floating_Point_Subtype:
5511 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5512 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5514 /* ??? Tests to avoid Constraint_Error in static expressions
5515 are needed until after the front stops generating bogus
5516 conversions on bounds of real types. */
5517 if (!Raises_Constraint_Error (gnat_lb))
5518 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5519 true, false, Needs_Debug_Info (gnat_entity));
5520 if (!Raises_Constraint_Error (gnat_hb))
5521 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5522 true, false, Needs_Debug_Info (gnat_entity));
5528 Node_Id full_definition = Declaration_Node (gnat_entity);
5529 Node_Id record_definition = Type_Definition (full_definition);
5531 /* If this is a record extension, go a level further to find the
5532 record definition. */
5533 if (Nkind (record_definition) == N_Derived_Type_Definition)
5534 record_definition = Record_Extension_Part (record_definition);
5538 case E_Record_Subtype:
5539 case E_Private_Subtype:
5540 case E_Limited_Private_Subtype:
5541 case E_Record_Subtype_With_Private:
5542 if (Is_Constrained (gnat_entity)
5543 && Has_Discriminants (gnat_entity)
5544 && Present (Discriminant_Constraint (gnat_entity)))
5546 Node_Id gnat_discriminant_expr;
5547 Entity_Id gnat_field;
5550 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5551 gnat_discriminant_expr
5552 = First_Elmt (Discriminant_Constraint (gnat_entity));
5553 Present (gnat_field);
5554 gnat_field = Next_Discriminant (gnat_field),
5555 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5556 /* ??? For now, ignore access discriminants. */
5557 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5558 elaborate_expression (Node (gnat_discriminant_expr),
5559 gnat_entity, get_entity_name (gnat_field),
5560 true, false, false);
5567 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5568 any entities on its entity chain similarly. */
5571 mark_out_of_scope (Entity_Id gnat_entity)
5573 Entity_Id gnat_sub_entity;
5574 unsigned int kind = Ekind (gnat_entity);
5576 /* If this has an entity list, process all in the list. */
5577 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5578 || IN (kind, Private_Kind)
5579 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5580 || kind == E_Function || kind == E_Generic_Function
5581 || kind == E_Generic_Package || kind == E_Generic_Procedure
5582 || kind == E_Loop || kind == E_Operator || kind == E_Package
5583 || kind == E_Package_Body || kind == E_Procedure
5584 || kind == E_Record_Type || kind == E_Record_Subtype
5585 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5586 for (gnat_sub_entity = First_Entity (gnat_entity);
5587 Present (gnat_sub_entity);
5588 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5589 if (Scope (gnat_sub_entity) == gnat_entity
5590 && gnat_sub_entity != gnat_entity)
5591 mark_out_of_scope (gnat_sub_entity);
5593 /* Now clear this if it has been defined, but only do so if it isn't
5594 a subprogram or parameter. We could refine this, but it isn't
5595 worth it. If this is statically allocated, it is supposed to
5596 hang around out of cope. */
5597 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5598 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5600 save_gnu_tree (gnat_entity, NULL_TREE, true);
5601 save_gnu_tree (gnat_entity, error_mark_node, true);
5605 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5606 If this is a multi-dimensional array type, do this recursively.
5609 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5610 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5611 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5614 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5616 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5617 of a one-dimensional array, since the padding has the same alias set
5618 as the field type, but if it's a multi-dimensional array, we need to
5619 see the inner types. */
5620 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5621 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5622 || TYPE_PADDING_P (gnu_old_type)))
5623 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5625 /* Unconstrained array types are deemed incomplete and would thus be given
5626 alias set 0. Retrieve the underlying array type. */
5627 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5629 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5630 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5632 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5634 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5635 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5636 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5637 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5641 case ALIAS_SET_COPY:
5642 /* The alias set shouldn't be copied between array types with different
5643 aliasing settings because this can break the aliasing relationship
5644 between the array type and its element type. */
5645 #ifndef ENABLE_CHECKING
5646 if (flag_strict_aliasing)
5648 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5649 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5650 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5651 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5653 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5656 case ALIAS_SET_SUBSET:
5657 case ALIAS_SET_SUPERSET:
5659 alias_set_type old_set = get_alias_set (gnu_old_type);
5660 alias_set_type new_set = get_alias_set (gnu_new_type);
5662 /* Do nothing if the alias sets conflict. This ensures that we
5663 never call record_alias_subset several times for the same pair
5664 or at all for alias set 0. */
5665 if (!alias_sets_conflict_p (old_set, new_set))
5667 if (op == ALIAS_SET_SUBSET)
5668 record_alias_subset (old_set, new_set);
5670 record_alias_subset (new_set, old_set);
5679 record_component_aliases (gnu_new_type);
5682 /* Return true if the size represented by GNU_SIZE can be handled by an
5683 allocation. If STATIC_P is true, consider only what can be done with a
5684 static allocation. */
5687 allocatable_size_p (tree gnu_size, bool static_p)
5689 HOST_WIDE_INT our_size;
5691 /* If this is not a static allocation, the only case we want to forbid
5692 is an overflowing size. That will be converted into a raise a
5695 return !(TREE_CODE (gnu_size) == INTEGER_CST
5696 && TREE_OVERFLOW (gnu_size));
5698 /* Otherwise, we need to deal with both variable sizes and constant
5699 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5700 since assemblers may not like very large sizes. */
5701 if (!host_integerp (gnu_size, 1))
5704 our_size = tree_low_cst (gnu_size, 1);
5705 return (int) our_size == our_size;
5708 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5709 NAME, ARGS and ERROR_POINT. */
5712 prepend_one_attribute_to (struct attrib ** attr_list,
5713 enum attr_type attr_type,
5716 Node_Id attr_error_point)
5718 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5720 attr->type = attr_type;
5721 attr->name = attr_name;
5722 attr->args = attr_args;
5723 attr->error_point = attr_error_point;
5725 attr->next = *attr_list;
5729 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5732 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5736 /* Attributes are stored as Representation Item pragmas. */
5738 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5739 gnat_temp = Next_Rep_Item (gnat_temp))
5740 if (Nkind (gnat_temp) == N_Pragma)
5742 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5743 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5744 enum attr_type etype;
5746 /* Map the kind of pragma at hand. Skip if this is not one
5747 we know how to handle. */
5749 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5751 case Pragma_Machine_Attribute:
5752 etype = ATTR_MACHINE_ATTRIBUTE;
5755 case Pragma_Linker_Alias:
5756 etype = ATTR_LINK_ALIAS;
5759 case Pragma_Linker_Section:
5760 etype = ATTR_LINK_SECTION;
5763 case Pragma_Linker_Constructor:
5764 etype = ATTR_LINK_CONSTRUCTOR;
5767 case Pragma_Linker_Destructor:
5768 etype = ATTR_LINK_DESTRUCTOR;
5771 case Pragma_Weak_External:
5772 etype = ATTR_WEAK_EXTERNAL;
5775 case Pragma_Thread_Local_Storage:
5776 etype = ATTR_THREAD_LOCAL_STORAGE;
5783 /* See what arguments we have and turn them into GCC trees for
5784 attribute handlers. These expect identifier for strings. We
5785 handle at most two arguments, static expressions only. */
5787 if (Present (gnat_assoc) && Present (First (gnat_assoc)))
5789 Node_Id gnat_arg0 = Next (First (gnat_assoc));
5790 Node_Id gnat_arg1 = Empty;
5792 if (Present (gnat_arg0)
5793 && Is_Static_Expression (Expression (gnat_arg0)))
5795 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
5797 if (TREE_CODE (gnu_arg0) == STRING_CST)
5798 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
5800 gnat_arg1 = Next (gnat_arg0);
5803 if (Present (gnat_arg1)
5804 && Is_Static_Expression (Expression (gnat_arg1)))
5806 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
5808 if (TREE_CODE (gnu_arg1) == STRING_CST)
5809 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
5813 /* Prepend to the list now. Make a list of the argument we might
5814 have, as GCC expects it. */
5815 prepend_one_attribute_to
5818 (gnu_arg1 != NULL_TREE)
5819 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5820 Present (Next (First (gnat_assoc)))
5821 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5825 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5826 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5827 return the GCC tree to use for that expression. GNU_NAME is the suffix
5828 to use if a variable needs to be created and DEFINITION is true if this
5829 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
5830 otherwise, we are just elaborating the expression for side-effects. If
5831 NEED_DEBUG is true, we need a variable for debugging purposes even if it
5832 isn't needed for code generation. */
5835 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
5836 bool definition, bool need_value, bool need_debug)
5840 /* If we already elaborated this expression (e.g. it was involved
5841 in the definition of a private type), use the old value. */
5842 if (present_gnu_tree (gnat_expr))
5843 return get_gnu_tree (gnat_expr);
5845 /* If we don't need a value and this is static or a discriminant,
5846 we don't need to do anything. */
5848 && (Is_OK_Static_Expression (gnat_expr)
5849 || (Nkind (gnat_expr) == N_Identifier
5850 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5853 /* If it's a static expression, we don't need a variable for debugging. */
5854 if (need_debug && Is_OK_Static_Expression (gnat_expr))
5857 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
5858 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
5859 gnu_name, definition, need_debug);
5861 /* Save the expression in case we try to elaborate this entity again. Since
5862 it's not a DECL, don't check it. Don't save if it's a discriminant. */
5863 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5864 save_gnu_tree (gnat_expr, gnu_expr, true);
5866 return need_value ? gnu_expr : error_mark_node;
5869 /* Similar, but take a GNU expression and always return a result. */
5872 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
5873 bool definition, bool need_debug)
5875 /* Skip any conversions and simple arithmetics to see if the expression
5876 is a read-only variable.
5877 ??? This really should remain read-only, but we have to think about
5878 the typing of the tree here. */
5880 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5881 tree gnu_decl = NULL_TREE;
5882 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5885 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
5886 reference will have been replaced with a COMPONENT_REF when the type
5887 is being elaborated. However, there are some cases involving child
5888 types where we will. So convert it to a COMPONENT_REF. We hope it
5889 will be at the highest level of the expression in these cases. */
5890 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5891 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5892 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5893 gnu_expr, NULL_TREE);
5895 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5896 that is read-only, make a variable that is initialized to contain the
5897 bound when the package containing the definition is elaborated. If
5898 this entity is defined at top level and a bound or discriminant value
5899 isn't a constant or a reference to a discriminant, replace the bound
5900 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5901 rely here on the fact that an expression cannot contain both the
5902 discriminant and some other variable. */
5903 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5904 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5905 && (TREE_READONLY (gnu_inner_expr)
5906 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5907 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5909 /* If GNU_EXPR contains a discriminant, we can't elaborate a variable. */
5910 if (need_debug && CONTAINS_PLACEHOLDER_P (gnu_expr))
5913 /* Now create the variable if we need it. */
5914 if (need_debug || (expr_variable && expr_global))
5916 = create_var_decl (create_concat_name (gnat_entity,
5917 IDENTIFIER_POINTER (gnu_name)),
5918 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5919 !need_debug, Is_Public (gnat_entity),
5920 !definition, false, NULL, gnat_entity);
5922 /* We only need to use this variable if we are in global context since GCC
5923 can do the right thing in the local case. */
5924 if (expr_global && expr_variable)
5927 return expr_variable ? gnat_save_expr (gnu_expr) : gnu_expr;
5930 /* Similar, but take an alignment factor and make it explicit in the tree. */
5933 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
5934 bool definition, bool need_debug, unsigned int align)
5936 tree unit_align = size_int (align / BITS_PER_UNIT);
5938 size_binop (MULT_EXPR,
5939 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
5942 gnat_entity, gnu_name, definition,
5947 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5948 starting bit position so that it is aligned to ALIGN bits, and leaving at
5949 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5950 record is guaranteed to get. */
5953 make_aligning_type (tree type, unsigned int align, tree size,
5954 unsigned int base_align, int room)
5956 /* We will be crafting a record type with one field at a position set to be
5957 the next multiple of ALIGN past record'address + room bytes. We use a
5958 record placeholder to express record'address. */
5959 tree record_type = make_node (RECORD_TYPE);
5960 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5963 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5965 /* The diagram below summarizes the shape of what we manipulate:
5967 <--------- pos ---------->
5968 { +------------+-------------+-----------------+
5969 record =>{ |############| ... | field (type) |
5970 { +------------+-------------+-----------------+
5971 |<-- room -->|<- voffset ->|<---- size ----->|
5974 record_addr vblock_addr
5976 Every length is in sizetype bytes there, except "pos" which has to be
5977 set as a bit position in the GCC tree for the record. */
5978 tree room_st = size_int (room);
5979 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5980 tree voffset_st, pos, field;
5982 tree name = TYPE_NAME (type);
5984 if (TREE_CODE (name) == TYPE_DECL)
5985 name = DECL_NAME (name);
5986 name = concat_name (name, "ALIGN");
5987 TYPE_NAME (record_type) = name;
5989 /* Compute VOFFSET and then POS. The next byte position multiple of some
5990 alignment after some address is obtained by "and"ing the alignment minus
5991 1 with the two's complement of the address. */
5992 voffset_st = size_binop (BIT_AND_EXPR,
5993 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
5994 size_int ((align / BITS_PER_UNIT) - 1));
5996 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5997 pos = size_binop (MULT_EXPR,
5998 convert (bitsizetype,
5999 size_binop (PLUS_EXPR, room_st, voffset_st)),
6002 /* Craft the GCC record representation. We exceptionally do everything
6003 manually here because 1) our generic circuitry is not quite ready to
6004 handle the complex position/size expressions we are setting up, 2) we
6005 have a strong simplifying factor at hand: we know the maximum possible
6006 value of voffset, and 3) we have to set/reset at least the sizes in
6007 accordance with this maximum value anyway, as we need them to convey
6008 what should be "alloc"ated for this type.
6010 Use -1 as the 'addressable' indication for the field to prevent the
6011 creation of a bitfield. We don't need one, it would have damaging
6012 consequences on the alignment computation, and create_field_decl would
6013 make one without this special argument, for instance because of the
6014 complex position expression. */
6015 field = create_field_decl (get_identifier ("F"), type, record_type, size,
6017 TYPE_FIELDS (record_type) = field;
6019 TYPE_ALIGN (record_type) = base_align;
6020 TYPE_USER_ALIGN (record_type) = 1;
6022 TYPE_SIZE (record_type)
6023 = size_binop (PLUS_EXPR,
6024 size_binop (MULT_EXPR, convert (bitsizetype, size),
6026 bitsize_int (align + room * BITS_PER_UNIT));
6027 TYPE_SIZE_UNIT (record_type)
6028 = size_binop (PLUS_EXPR, size,
6029 size_int (room + align / BITS_PER_UNIT));
6031 SET_TYPE_MODE (record_type, BLKmode);
6032 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6034 /* Declare it now since it will never be declared otherwise. This is
6035 necessary to ensure that its subtrees are properly marked. */
6036 create_type_decl (name, record_type, NULL, true, false, Empty);
6041 /* Return the result of rounding T up to ALIGN. */
6043 static inline unsigned HOST_WIDE_INT
6044 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6052 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6053 as the field type of a packed record if IN_RECORD is true, or as the
6054 component type of a packed array if IN_RECORD is false. See if we can
6055 rewrite it either as a type that has a non-BLKmode, which we can pack
6056 tighter in the packed record case, or as a smaller type. If so, return
6057 the new type. If not, return the original type. */
6060 make_packable_type (tree type, bool in_record)
6062 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6063 unsigned HOST_WIDE_INT new_size;
6064 tree new_type, old_field, field_list = NULL_TREE;
6066 /* No point in doing anything if the size is zero. */
6070 new_type = make_node (TREE_CODE (type));
6072 /* Copy the name and flags from the old type to that of the new.
6073 Note that we rely on the pointer equality created here for
6074 TYPE_NAME to look through conversions in various places. */
6075 TYPE_NAME (new_type) = TYPE_NAME (type);
6076 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6077 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6078 if (TREE_CODE (type) == RECORD_TYPE)
6079 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6081 /* If we are in a record and have a small size, set the alignment to
6082 try for an integral mode. Otherwise set it to try for a smaller
6083 type with BLKmode. */
6084 if (in_record && size <= MAX_FIXED_MODE_SIZE)
6086 TYPE_ALIGN (new_type) = ceil_alignment (size);
6087 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6091 unsigned HOST_WIDE_INT align;
6093 /* Do not try to shrink the size if the RM size is not constant. */
6094 if (TYPE_CONTAINS_TEMPLATE_P (type)
6095 || !host_integerp (TYPE_ADA_SIZE (type), 1))
6098 /* Round the RM size up to a unit boundary to get the minimal size
6099 for a BLKmode record. Give up if it's already the size. */
6100 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6101 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6102 if (new_size == size)
6105 align = new_size & -new_size;
6106 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6109 TYPE_USER_ALIGN (new_type) = 1;
6111 /* Now copy the fields, keeping the position and size as we don't want
6112 to change the layout by propagating the packedness downwards. */
6113 for (old_field = TYPE_FIELDS (type); old_field;
6114 old_field = TREE_CHAIN (old_field))
6116 tree new_field_type = TREE_TYPE (old_field);
6117 tree new_field, new_size;
6119 if ((TREE_CODE (new_field_type) == RECORD_TYPE
6120 || TREE_CODE (new_field_type) == UNION_TYPE
6121 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6122 && !TYPE_FAT_POINTER_P (new_field_type)
6123 && host_integerp (TYPE_SIZE (new_field_type), 1))
6124 new_field_type = make_packable_type (new_field_type, true);
6126 /* However, for the last field in a not already packed record type
6127 that is of an aggregate type, we need to use the RM size in the
6128 packable version of the record type, see finish_record_type. */
6129 if (!TREE_CHAIN (old_field)
6130 && !TYPE_PACKED (type)
6131 && (TREE_CODE (new_field_type) == RECORD_TYPE
6132 || TREE_CODE (new_field_type) == UNION_TYPE
6133 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6134 && !TYPE_FAT_POINTER_P (new_field_type)
6135 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6136 && TYPE_ADA_SIZE (new_field_type))
6137 new_size = TYPE_ADA_SIZE (new_field_type);
6139 new_size = DECL_SIZE (old_field);
6142 = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6143 new_size, bit_position (old_field),
6145 !DECL_NONADDRESSABLE_P (old_field));
6147 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6148 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6149 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6150 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6152 TREE_CHAIN (new_field) = field_list;
6153 field_list = new_field;
6156 finish_record_type (new_type, nreverse (field_list), 2, false);
6157 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6159 /* If this is a padding record, we never want to make the size smaller
6160 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
6161 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6163 TYPE_SIZE (new_type) = TYPE_SIZE (type);
6164 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6169 TYPE_SIZE (new_type) = bitsize_int (new_size);
6170 TYPE_SIZE_UNIT (new_type)
6171 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6174 if (!TYPE_CONTAINS_TEMPLATE_P (type))
6175 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6177 compute_record_mode (new_type);
6179 /* Try harder to get a packable type if necessary, for example
6180 in case the record itself contains a BLKmode field. */
6181 if (in_record && TYPE_MODE (new_type) == BLKmode)
6182 SET_TYPE_MODE (new_type,
6183 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6185 /* If neither the mode nor the size has shrunk, return the old type. */
6186 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6192 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
6193 if needed. We have already verified that SIZE and TYPE are large enough.
6194 GNAT_ENTITY is used to name the resulting record and to issue a warning.
6195 IS_COMPONENT_TYPE is true if this is being done for the component type
6196 of an array. IS_USER_TYPE is true if we must complete the original type.
6197 DEFINITION is true if this type is being defined. SAME_RM_SIZE is true
6198 if the RM size of the resulting type is to be set to SIZE too; otherwise,
6199 it's set to the RM size of the original type. */
6202 maybe_pad_type (tree type, tree size, unsigned int align,
6203 Entity_Id gnat_entity, bool is_component_type,
6204 bool is_user_type, bool definition, bool same_rm_size)
6206 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6207 tree orig_size = TYPE_SIZE (type);
6210 /* If TYPE is a padded type, see if it agrees with any size and alignment
6211 we were given. If so, return the original type. Otherwise, strip
6212 off the padding, since we will either be returning the inner type
6213 or repadding it. If no size or alignment is specified, use that of
6214 the original padded type. */
6215 if (TYPE_IS_PADDING_P (type))
6218 || operand_equal_p (round_up (size,
6219 MAX (align, TYPE_ALIGN (type))),
6220 round_up (TYPE_SIZE (type),
6221 MAX (align, TYPE_ALIGN (type))),
6223 && (align == 0 || align == TYPE_ALIGN (type)))
6227 size = TYPE_SIZE (type);
6229 align = TYPE_ALIGN (type);
6231 type = TREE_TYPE (TYPE_FIELDS (type));
6232 orig_size = TYPE_SIZE (type);
6235 /* If the size is either not being changed or is being made smaller (which
6236 is not done here and is only valid for bitfields anyway), show the size
6237 isn't changing. Likewise, clear the alignment if it isn't being
6238 changed. Then return if we aren't doing anything. */
6240 && (operand_equal_p (size, orig_size, 0)
6241 || (TREE_CODE (orig_size) == INTEGER_CST
6242 && tree_int_cst_lt (size, orig_size))))
6245 if (align == TYPE_ALIGN (type))
6248 if (align == 0 && !size)
6251 /* If requested, complete the original type and give it a name. */
6253 create_type_decl (get_entity_name (gnat_entity), type,
6254 NULL, !Comes_From_Source (gnat_entity),
6256 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6257 && DECL_IGNORED_P (TYPE_NAME (type))),
6260 /* We used to modify the record in place in some cases, but that could
6261 generate incorrect debugging information. So make a new record
6263 record = make_node (RECORD_TYPE);
6264 TYPE_PADDING_P (record) = 1;
6266 if (Present (gnat_entity))
6267 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6269 TYPE_VOLATILE (record)
6270 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6272 TYPE_ALIGN (record) = align;
6273 TYPE_SIZE (record) = size ? size : orig_size;
6274 TYPE_SIZE_UNIT (record)
6275 = convert (sizetype,
6276 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6277 bitsize_unit_node));
6279 /* If we are changing the alignment and the input type is a record with
6280 BLKmode and a small constant size, try to make a form that has an
6281 integral mode. This might allow the padding record to also have an
6282 integral mode, which will be much more efficient. There is no point
6283 in doing so if a size is specified unless it is also a small constant
6284 size and it is incorrect to do so if we cannot guarantee that the mode
6285 will be naturally aligned since the field must always be addressable.
6287 ??? This might not always be a win when done for a stand-alone object:
6288 since the nominal and the effective type of the object will now have
6289 different modes, a VIEW_CONVERT_EXPR will be required for converting
6290 between them and it might be hard to overcome afterwards, including
6291 at the RTL level when the stand-alone object is accessed as a whole. */
6293 && TREE_CODE (type) == RECORD_TYPE
6294 && TYPE_MODE (type) == BLKmode
6295 && TREE_CODE (orig_size) == INTEGER_CST
6296 && !TREE_OVERFLOW (orig_size)
6297 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6299 || (TREE_CODE (size) == INTEGER_CST
6300 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6302 tree packable_type = make_packable_type (type, true);
6303 if (TYPE_MODE (packable_type) != BLKmode
6304 && align >= TYPE_ALIGN (packable_type))
6305 type = packable_type;
6308 /* Now create the field with the original size. */
6309 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
6310 bitsize_zero_node, 0, 1);
6311 DECL_INTERNAL_P (field) = 1;
6313 /* Do not emit debug info until after the auxiliary record is built. */
6314 finish_record_type (record, field, 1, false);
6316 /* Set the same size for its RM size if requested; otherwise reuse
6317 the RM size of the original type. */
6318 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6320 /* Unless debugging information isn't being written for the input type,
6321 write a record that shows what we are a subtype of and also make a
6322 variable that indicates our size, if still variable. */
6323 if (TREE_CODE (orig_size) != INTEGER_CST
6324 && TYPE_NAME (record)
6326 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6327 && DECL_IGNORED_P (TYPE_NAME (type))))
6329 tree marker = make_node (RECORD_TYPE);
6330 tree name = TYPE_NAME (record);
6331 tree orig_name = TYPE_NAME (type);
6333 if (TREE_CODE (name) == TYPE_DECL)
6334 name = DECL_NAME (name);
6336 if (TREE_CODE (orig_name) == TYPE_DECL)
6337 orig_name = DECL_NAME (orig_name);
6339 TYPE_NAME (marker) = concat_name (name, "XVS");
6340 finish_record_type (marker,
6341 create_field_decl (orig_name,
6342 build_reference_type (type),
6343 marker, NULL_TREE, NULL_TREE,
6347 add_parallel_type (TYPE_STUB_DECL (record), marker);
6349 if (definition && size && TREE_CODE (size) != INTEGER_CST)
6350 TYPE_SIZE_UNIT (marker)
6351 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6352 TYPE_SIZE_UNIT (record), false, false, false,
6353 false, NULL, gnat_entity);
6356 rest_of_record_type_compilation (record);
6358 /* If the size was widened explicitly, maybe give a warning. Take the
6359 original size as the maximum size of the input if there was an
6360 unconstrained record involved and round it up to the specified alignment,
6361 if one was specified. */
6362 if (CONTAINS_PLACEHOLDER_P (orig_size))
6363 orig_size = max_size (orig_size, true);
6366 orig_size = round_up (orig_size, align);
6368 if (Present (gnat_entity)
6370 && TREE_CODE (size) != MAX_EXPR
6371 && TREE_CODE (size) != COND_EXPR
6372 && !operand_equal_p (size, orig_size, 0)
6373 && !(TREE_CODE (size) == INTEGER_CST
6374 && TREE_CODE (orig_size) == INTEGER_CST
6375 && (TREE_OVERFLOW (size)
6376 || TREE_OVERFLOW (orig_size)
6377 || tree_int_cst_lt (size, orig_size))))
6379 Node_Id gnat_error_node = Empty;
6381 if (Is_Packed_Array_Type (gnat_entity))
6382 gnat_entity = Original_Array_Type (gnat_entity);
6384 if ((Ekind (gnat_entity) == E_Component
6385 || Ekind (gnat_entity) == E_Discriminant)
6386 && Present (Component_Clause (gnat_entity)))
6387 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6388 else if (Present (Size_Clause (gnat_entity)))
6389 gnat_error_node = Expression (Size_Clause (gnat_entity));
6391 /* Generate message only for entities that come from source, since
6392 if we have an entity created by expansion, the message will be
6393 generated for some other corresponding source entity. */
6394 if (Comes_From_Source (gnat_entity))
6396 if (Present (gnat_error_node))
6397 post_error_ne_tree ("{^ }bits of & unused?",
6398 gnat_error_node, gnat_entity,
6399 size_diffop (size, orig_size));
6400 else if (is_component_type)
6401 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6402 gnat_entity, gnat_entity,
6403 size_diffop (size, orig_size));
6410 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6411 the value passed against the list of choices. */
6414 choices_to_gnu (tree operand, Node_Id choices)
6418 tree result = integer_zero_node;
6419 tree this_test, low = 0, high = 0, single = 0;
6421 for (choice = First (choices); Present (choice); choice = Next (choice))
6423 switch (Nkind (choice))
6426 low = gnat_to_gnu (Low_Bound (choice));
6427 high = gnat_to_gnu (High_Bound (choice));
6430 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6431 build_binary_op (GE_EXPR, boolean_type_node,
6433 build_binary_op (LE_EXPR, boolean_type_node,
6438 case N_Subtype_Indication:
6439 gnat_temp = Range_Expression (Constraint (choice));
6440 low = gnat_to_gnu (Low_Bound (gnat_temp));
6441 high = gnat_to_gnu (High_Bound (gnat_temp));
6444 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6445 build_binary_op (GE_EXPR, boolean_type_node,
6447 build_binary_op (LE_EXPR, boolean_type_node,
6452 case N_Expanded_Name:
6453 /* This represents either a subtype range, an enumeration
6454 literal, or a constant Ekind says which. If an enumeration
6455 literal or constant, fall through to the next case. */
6456 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6457 && Ekind (Entity (choice)) != E_Constant)
6459 tree type = gnat_to_gnu_type (Entity (choice));
6461 low = TYPE_MIN_VALUE (type);
6462 high = TYPE_MAX_VALUE (type);
6465 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6466 build_binary_op (GE_EXPR, boolean_type_node,
6468 build_binary_op (LE_EXPR, boolean_type_node,
6473 /* ... fall through ... */
6475 case N_Character_Literal:
6476 case N_Integer_Literal:
6477 single = gnat_to_gnu (choice);
6478 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6482 case N_Others_Choice:
6483 this_test = integer_one_node;
6490 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6497 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6498 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6501 adjust_packed (tree field_type, tree record_type, int packed)
6503 /* If the field contains an item of variable size, we cannot pack it
6504 because we cannot create temporaries of non-fixed size in case
6505 we need to take the address of the field. See addressable_p and
6506 the notes on the addressability issues for further details. */
6507 if (is_variable_size (field_type))
6510 /* If the alignment of the record is specified and the field type
6511 is over-aligned, request Storage_Unit alignment for the field. */
6514 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6523 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6524 placed in GNU_RECORD_TYPE.
6526 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6527 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6528 record has a specified alignment.
6530 DEFINITION is true if this field is for a record being defined.
6532 DEBUG_INFO_P is true if we need to write debug information for types
6533 that we may create in the process. */
6536 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6537 bool definition, bool debug_info_p)
6539 tree gnu_field_id = get_entity_name (gnat_field);
6540 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6541 tree gnu_field, gnu_size, gnu_pos;
6542 bool needs_strict_alignment
6543 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6544 || Treat_As_Volatile (gnat_field));
6546 /* If this field requires strict alignment, we cannot pack it because
6547 it would very likely be under-aligned in the record. */
6548 if (needs_strict_alignment)
6551 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6553 /* If a size is specified, use it. Otherwise, if the record type is packed,
6554 use the official RM size. See "Handling of Type'Size Values" in Einfo
6555 for further details. */
6556 if (Known_Static_Esize (gnat_field))
6557 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6558 gnat_field, FIELD_DECL, false, true);
6559 else if (packed == 1)
6560 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6561 gnat_field, FIELD_DECL, false, true);
6563 gnu_size = NULL_TREE;
6565 /* If we have a specified size that is smaller than that of the field's type,
6566 or a position is specified, and the field's type is a record that doesn't
6567 require strict alignment, see if we can get either an integral mode form
6568 of the type or a smaller form. If we can, show a size was specified for
6569 the field if there wasn't one already, so we know to make this a bitfield
6570 and avoid making things wider.
6572 Changing to an integral mode form is useful when the record is packed as
6573 we can then place the field at a non-byte-aligned position and so achieve
6574 tighter packing. This is in addition required if the field shares a byte
6575 with another field and the front-end lets the back-end handle the access
6576 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6578 Changing to a smaller form is required if the specified size is smaller
6579 than that of the field's type and the type contains sub-fields that are
6580 padded, in order to avoid generating accesses to these sub-fields that
6581 are wider than the field.
6583 We avoid the transformation if it is not required or potentially useful,
6584 as it might entail an increase of the field's alignment and have ripple
6585 effects on the outer record type. A typical case is a field known to be
6586 byte-aligned and not to share a byte with another field. */
6587 if (!needs_strict_alignment
6588 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6589 && !TYPE_FAT_POINTER_P (gnu_field_type)
6590 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6593 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6594 || (Present (Component_Clause (gnat_field))
6595 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6596 % BITS_PER_UNIT == 0
6597 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6599 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6600 if (gnu_packable_type != gnu_field_type)
6602 gnu_field_type = gnu_packable_type;
6604 gnu_size = rm_size (gnu_field_type);
6608 /* If we are packing the record and the field is BLKmode, round the
6609 size up to a byte boundary. */
6610 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6611 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6613 if (Present (Component_Clause (gnat_field)))
6615 Entity_Id gnat_parent
6616 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6618 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6619 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6620 gnat_field, FIELD_DECL, false, true);
6622 /* Ensure the position does not overlap with the parent subtype, if there
6623 is one. This test is omitted if the parent of the tagged type has a
6624 full rep clause since, in this case, component clauses are allowed to
6625 overlay the space allocated for the parent type and the front-end has
6626 checked that there are no overlapping components. */
6627 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6629 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6631 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6632 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6635 ("offset of& must be beyond parent{, minimum allowed is ^}",
6636 First_Bit (Component_Clause (gnat_field)), gnat_field,
6637 TYPE_SIZE_UNIT (gnu_parent));
6641 /* If this field needs strict alignment, ensure the record is
6642 sufficiently aligned and that that position and size are
6643 consistent with the alignment. */
6644 if (needs_strict_alignment)
6646 TYPE_ALIGN (gnu_record_type)
6647 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6650 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6652 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6654 ("atomic field& must be natural size of type{ (^)}",
6655 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6656 TYPE_SIZE (gnu_field_type));
6658 else if (Is_Aliased (gnat_field))
6660 ("size of aliased field& must be ^ bits",
6661 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6662 TYPE_SIZE (gnu_field_type));
6664 else if (Strict_Alignment (Etype (gnat_field)))
6666 ("size of & with aliased or tagged components not ^ bits",
6667 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6668 TYPE_SIZE (gnu_field_type));
6670 gnu_size = NULL_TREE;
6673 if (!integer_zerop (size_binop
6674 (TRUNC_MOD_EXPR, gnu_pos,
6675 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6677 if (Is_Aliased (gnat_field))
6679 ("position of aliased field& must be multiple of ^ bits",
6680 First_Bit (Component_Clause (gnat_field)), gnat_field,
6681 TYPE_ALIGN (gnu_field_type));
6683 else if (Treat_As_Volatile (gnat_field))
6685 ("position of volatile field& must be multiple of ^ bits",
6686 First_Bit (Component_Clause (gnat_field)), gnat_field,
6687 TYPE_ALIGN (gnu_field_type));
6689 else if (Strict_Alignment (Etype (gnat_field)))
6691 ("position of & with aliased or tagged components not multiple of ^ bits",
6692 First_Bit (Component_Clause (gnat_field)), gnat_field,
6693 TYPE_ALIGN (gnu_field_type));
6698 gnu_pos = NULL_TREE;
6702 if (Is_Atomic (gnat_field))
6703 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6706 /* If the record has rep clauses and this is the tag field, make a rep
6707 clause for it as well. */
6708 else if (Has_Specified_Layout (Scope (gnat_field))
6709 && Chars (gnat_field) == Name_uTag)
6711 gnu_pos = bitsize_zero_node;
6712 gnu_size = TYPE_SIZE (gnu_field_type);
6716 gnu_pos = NULL_TREE;
6718 /* We need to make the size the maximum for the type if it is
6719 self-referential and an unconstrained type. In that case, we can't
6720 pack the field since we can't make a copy to align it. */
6721 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6723 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6724 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6726 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6730 /* If a size is specified, adjust the field's type to it. */
6733 tree orig_field_type;
6735 /* If the field's type is justified modular, we would need to remove
6736 the wrapper to (better) meet the layout requirements. However we
6737 can do so only if the field is not aliased to preserve the unique
6738 layout and if the prescribed size is not greater than that of the
6739 packed array to preserve the justification. */
6740 if (!needs_strict_alignment
6741 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6742 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6743 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6745 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6748 = make_type_from_size (gnu_field_type, gnu_size,
6749 Has_Biased_Representation (gnat_field));
6751 orig_field_type = gnu_field_type;
6752 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6753 false, false, definition, true);
6755 /* If a padding record was made, declare it now since it will never be
6756 declared otherwise. This is necessary to ensure that its subtrees
6757 are properly marked. */
6758 if (gnu_field_type != orig_field_type
6759 && !DECL_P (TYPE_NAME (gnu_field_type)))
6760 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6761 true, debug_info_p, gnat_field);
6764 /* Otherwise (or if there was an error), don't specify a position. */
6766 gnu_pos = NULL_TREE;
6768 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6769 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6771 /* Now create the decl for the field. */
6773 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6774 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6775 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6776 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6778 if (Ekind (gnat_field) == E_Discriminant)
6779 DECL_DISCRIMINANT_NUMBER (gnu_field)
6780 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6785 /* Return true if TYPE is a type with variable size, a padding type with a
6786 field of variable size or is a record that has a field such a field. */
6789 is_variable_size (tree type)
6793 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6796 if (TYPE_IS_PADDING_P (type)
6797 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6800 if (TREE_CODE (type) != RECORD_TYPE
6801 && TREE_CODE (type) != UNION_TYPE
6802 && TREE_CODE (type) != QUAL_UNION_TYPE)
6805 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6806 if (is_variable_size (TREE_TYPE (field)))
6812 /* qsort comparer for the bit positions of two record components. */
6815 compare_field_bitpos (const PTR rt1, const PTR rt2)
6817 const_tree const field1 = * (const_tree const *) rt1;
6818 const_tree const field2 = * (const_tree const *) rt2;
6820 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6822 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6825 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6826 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6827 called from gnat_to_gnu_entity during the processing of a record type
6828 definition, the GCC node for the parent, if any, will be the single field
6829 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6830 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6831 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6833 PACKED is 1 if this is for a packed record, -1 if this is for a record
6834 with Component_Alignment of Storage_Unit, -2 if this is for a record
6835 with a specified alignment.
6837 DEFINITION is true if we are defining this record type.
6839 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6840 with a rep clause is to be added; in this case, that is all that should
6841 be done with such fields.
6843 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6844 out the record. This means the alignment only serves to force fields to
6845 be bitfields, but not to require the record to be that aligned. This is
6848 ALL_REP is true if a rep clause is present for all the fields.
6850 UNCHECKED_UNION is true if we are building this type for a record with a
6851 Pragma Unchecked_Union.
6853 DEBUG_INFO_P is true if we need to write debug information about the type.
6855 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6856 mean that its contents may be unused as well, but only the container. */
6860 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6861 tree gnu_field_list, int packed, bool definition,
6862 tree *p_gnu_rep_list, bool cancel_alignment,
6863 bool all_rep, bool unchecked_union, bool debug_info_p,
6866 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6867 bool layout_with_rep = false;
6868 Node_Id component_decl, variant_part;
6869 tree gnu_our_rep_list = NULL_TREE;
6870 tree gnu_field, gnu_next, gnu_last = tree_last (gnu_field_list);
6872 /* For each component referenced in a component declaration create a GCC
6873 field and add it to the list, skipping pragmas in the GNAT list. */
6874 if (Present (Component_Items (gnat_component_list)))
6876 = First_Non_Pragma (Component_Items (gnat_component_list));
6877 Present (component_decl);
6878 component_decl = Next_Non_Pragma (component_decl))
6880 Entity_Id gnat_field = Defining_Entity (component_decl);
6881 Name_Id gnat_name = Chars (gnat_field);
6883 /* If present, the _Parent field must have been created as the single
6884 field of the record type. Put it before any other fields. */
6885 if (gnat_name == Name_uParent)
6887 gnu_field = TYPE_FIELDS (gnu_record_type);
6888 gnu_field_list = chainon (gnu_field_list, gnu_field);
6892 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6893 definition, debug_info_p);
6895 /* If this is the _Tag field, put it before any other fields. */
6896 if (gnat_name == Name_uTag)
6897 gnu_field_list = chainon (gnu_field_list, gnu_field);
6899 /* If this is the _Controller field, put it before the other
6900 fields except for the _Tag or _Parent field. */
6901 else if (gnat_name == Name_uController && gnu_last)
6903 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
6904 TREE_CHAIN (gnu_last) = gnu_field;
6907 /* If this is a regular field, put it after the other fields. */
6910 TREE_CHAIN (gnu_field) = gnu_field_list;
6911 gnu_field_list = gnu_field;
6913 gnu_last = gnu_field;
6917 save_gnu_tree (gnat_field, gnu_field, false);
6920 /* At the end of the component list there may be a variant part. */
6921 variant_part = Variant_Part (gnat_component_list);
6923 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6924 mutually exclusive and should go in the same memory. To do this we need
6925 to treat each variant as a record whose elements are created from the
6926 component list for the variant. So here we create the records from the
6927 lists for the variants and put them all into the QUAL_UNION_TYPE.
6928 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6929 use GNU_RECORD_TYPE if there are no fields so far. */
6930 if (Present (variant_part))
6932 Node_Id gnat_discr = Name (variant_part), variant;
6933 tree gnu_discr = gnat_to_gnu (gnat_discr);
6934 tree gnu_name = TYPE_NAME (gnu_record_type);
6936 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6938 tree gnu_union_type, gnu_union_name, gnu_union_field;
6939 tree gnu_variant_list = NULL_TREE;
6941 if (TREE_CODE (gnu_name) == TYPE_DECL)
6942 gnu_name = DECL_NAME (gnu_name);
6945 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6947 /* Reuse an enclosing union if all fields are in the variant part
6948 and there is no representation clause on the record, to match
6949 the layout of C unions. There is an associated check below. */
6951 && TREE_CODE (gnu_record_type) == UNION_TYPE
6952 && !TYPE_PACKED (gnu_record_type))
6953 gnu_union_type = gnu_record_type;
6957 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6959 TYPE_NAME (gnu_union_type) = gnu_union_name;
6960 TYPE_ALIGN (gnu_union_type) = 0;
6961 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6964 for (variant = First_Non_Pragma (Variants (variant_part));
6966 variant = Next_Non_Pragma (variant))
6968 tree gnu_variant_type = make_node (RECORD_TYPE);
6969 tree gnu_inner_name;
6972 Get_Variant_Encoding (variant);
6973 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
6974 TYPE_NAME (gnu_variant_type)
6975 = concat_name (gnu_union_name,
6976 IDENTIFIER_POINTER (gnu_inner_name));
6978 /* Set the alignment of the inner type in case we need to make
6979 inner objects into bitfields, but then clear it out so the
6980 record actually gets only the alignment required. */
6981 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6982 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6984 /* Similarly, if the outer record has a size specified and all
6985 fields have record rep clauses, we can propagate the size
6986 into the variant part. */
6987 if (all_rep_and_size)
6989 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6990 TYPE_SIZE_UNIT (gnu_variant_type)
6991 = TYPE_SIZE_UNIT (gnu_record_type);
6994 /* Add the fields into the record type for the variant. Note that
6995 we aren't sure to really use it at this point, see below. */
6996 components_to_record (gnu_variant_type, Component_List (variant),
6997 NULL_TREE, packed, definition,
6998 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6999 unchecked_union, debug_info_p, true);
7001 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7003 Set_Present_Expr (variant, annotate_value (gnu_qual));
7005 /* If this is an Unchecked_Union and we have exactly one field,
7006 use this field directly to match the layout of C unions. */
7008 && TYPE_FIELDS (gnu_variant_type)
7009 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
7010 gnu_field = TYPE_FIELDS (gnu_variant_type);
7013 /* Deal with packedness like in gnat_to_gnu_field. */
7015 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7017 /* Finalize the record type now. We used to throw away
7018 empty records but we no longer do that because we need
7019 them to generate complete debug info for the variant;
7020 otherwise, the union type definition will be lacking
7021 the fields associated with these empty variants. */
7022 rest_of_record_type_compilation (gnu_variant_type);
7023 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7024 NULL, true, debug_info_p, gnat_component_list);
7027 = create_field_decl (gnu_inner_name, gnu_variant_type,
7030 ? TYPE_SIZE (gnu_variant_type) : 0,
7032 ? bitsize_zero_node : 0,
7035 DECL_INTERNAL_P (gnu_field) = 1;
7037 if (!unchecked_union)
7038 DECL_QUALIFIER (gnu_field) = gnu_qual;
7041 TREE_CHAIN (gnu_field) = gnu_variant_list;
7042 gnu_variant_list = gnu_field;
7045 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7046 if (gnu_variant_list)
7048 int union_field_packed;
7050 if (all_rep_and_size)
7052 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7053 TYPE_SIZE_UNIT (gnu_union_type)
7054 = TYPE_SIZE_UNIT (gnu_record_type);
7057 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7058 all_rep_and_size ? 1 : 0, debug_info_p);
7060 /* If GNU_UNION_TYPE is our record type, it means we must have an
7061 Unchecked_Union with no fields. Verify that and, if so, just
7063 if (gnu_union_type == gnu_record_type)
7065 gcc_assert (unchecked_union
7067 && !gnu_our_rep_list);
7071 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7072 NULL, true, debug_info_p, gnat_component_list);
7074 /* Deal with packedness like in gnat_to_gnu_field. */
7076 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7079 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7080 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7081 all_rep ? bitsize_zero_node : 0,
7082 union_field_packed, 0);
7084 DECL_INTERNAL_P (gnu_union_field) = 1;
7085 TREE_CHAIN (gnu_union_field) = gnu_field_list;
7086 gnu_field_list = gnu_union_field;
7090 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
7091 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do
7092 this in a separate pass since we want to handle the discriminants but
7093 can't play with them until we've used them in debugging data above.
7095 ??? If we then reorder them, debugging information will be wrong but
7096 there's nothing that can be done about this at the moment. */
7097 gnu_last = NULL_TREE;
7098 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7100 gnu_next = TREE_CHAIN (gnu_field);
7102 if (DECL_FIELD_OFFSET (gnu_field))
7105 gnu_field_list = gnu_next;
7107 TREE_CHAIN (gnu_last) = gnu_next;
7109 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
7110 gnu_our_rep_list = gnu_field;
7113 gnu_last = gnu_field;
7116 /* If we have any fields in our rep'ed field list and it is not the case that
7117 all the fields in the record have rep clauses and P_REP_LIST is nonzero,
7118 set it and ignore these fields. */
7119 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
7120 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
7122 /* Otherwise, sort the fields by bit position and put them into their own
7123 record, before the others, if we also have fields without rep clauses. */
7124 else if (gnu_our_rep_list)
7127 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7128 int i, len = list_length (gnu_our_rep_list);
7129 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
7131 for (gnu_field = gnu_our_rep_list, i = 0;
7133 gnu_field = TREE_CHAIN (gnu_field), i++)
7134 gnu_arr[i] = gnu_field;
7136 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7138 /* Put the fields in the list in order of increasing position, which
7139 means we start from the end. */
7140 gnu_our_rep_list = NULL_TREE;
7141 for (i = len - 1; i >= 0; i--)
7143 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
7144 gnu_our_rep_list = gnu_arr[i];
7145 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7150 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, debug_info_p);
7152 = create_field_decl (get_identifier ("REP"), gnu_rep_type,
7153 gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
7154 DECL_INTERNAL_P (gnu_field) = 1;
7155 gnu_field_list = chainon (gnu_field_list, gnu_field);
7159 layout_with_rep = true;
7160 gnu_field_list = nreverse (gnu_our_rep_list);
7164 if (cancel_alignment)
7165 TYPE_ALIGN (gnu_record_type) = 0;
7167 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7168 layout_with_rep ? 1 : 0, debug_info_p && !maybe_unused);
7171 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7172 placed into an Esize, Component_Bit_Offset, or Component_Size value
7173 in the GNAT tree. */
7176 annotate_value (tree gnu_size)
7179 Node_Ref_Or_Val ops[3], ret;
7180 struct tree_int_map **h = NULL;
7183 /* See if we've already saved the value for this node. */
7184 if (EXPR_P (gnu_size))
7186 struct tree_int_map in;
7187 if (!annotate_value_cache)
7188 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7189 tree_int_map_eq, 0);
7190 in.base.from = gnu_size;
7191 h = (struct tree_int_map **)
7192 htab_find_slot (annotate_value_cache, &in, INSERT);
7195 return (Node_Ref_Or_Val) (*h)->to;
7198 /* If we do not return inside this switch, TCODE will be set to the
7199 code to use for a Create_Node operand and LEN (set above) will be
7200 the number of recursive calls for us to make. */
7202 switch (TREE_CODE (gnu_size))
7205 if (TREE_OVERFLOW (gnu_size))
7208 /* This may come from a conversion from some smaller type, so ensure
7209 this is in bitsizetype. */
7210 gnu_size = convert (bitsizetype, gnu_size);
7212 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7213 appear in expressions containing aligning patterns. Note that, since
7214 sizetype is sign-extended but nonetheless unsigned, we don't directly
7215 use tree_int_cst_sgn. */
7216 if (TREE_INT_CST_HIGH (gnu_size) < 0)
7218 tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7219 return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7222 return UI_From_gnu (gnu_size);
7225 /* The only case we handle here is a simple discriminant reference. */
7226 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7227 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7228 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7229 return Create_Node (Discrim_Val,
7230 annotate_value (DECL_DISCRIMINANT_NUMBER
7231 (TREE_OPERAND (gnu_size, 1))),
7236 CASE_CONVERT: case NON_LVALUE_EXPR:
7237 return annotate_value (TREE_OPERAND (gnu_size, 0));
7239 /* Now just list the operations we handle. */
7240 case COND_EXPR: tcode = Cond_Expr; break;
7241 case PLUS_EXPR: tcode = Plus_Expr; break;
7242 case MINUS_EXPR: tcode = Minus_Expr; break;
7243 case MULT_EXPR: tcode = Mult_Expr; break;
7244 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7245 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7246 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7247 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7248 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7249 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7250 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7251 case NEGATE_EXPR: tcode = Negate_Expr; break;
7252 case MIN_EXPR: tcode = Min_Expr; break;
7253 case MAX_EXPR: tcode = Max_Expr; break;
7254 case ABS_EXPR: tcode = Abs_Expr; break;
7255 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7256 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7257 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7258 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7259 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7260 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7261 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7262 case LT_EXPR: tcode = Lt_Expr; break;
7263 case LE_EXPR: tcode = Le_Expr; break;
7264 case GT_EXPR: tcode = Gt_Expr; break;
7265 case GE_EXPR: tcode = Ge_Expr; break;
7266 case EQ_EXPR: tcode = Eq_Expr; break;
7267 case NE_EXPR: tcode = Ne_Expr; break;
7271 tree t = maybe_inline_call_in_expr (gnu_size);
7273 return annotate_value (t);
7276 /* Fall through... */
7282 /* Now get each of the operands that's relevant for this code. If any
7283 cannot be expressed as a repinfo node, say we can't. */
7284 for (i = 0; i < 3; i++)
7287 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7289 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7290 if (ops[i] == No_Uint)
7294 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7296 /* Save the result in the cache. */
7299 *h = GGC_NEW (struct tree_int_map);
7300 (*h)->base.from = gnu_size;
7307 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7308 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7309 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7310 BY_REF is true if the object is used by reference. */
7313 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7317 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7318 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7320 gnu_type = TREE_TYPE (gnu_type);
7323 if (Unknown_Esize (gnat_entity))
7325 if (TREE_CODE (gnu_type) == RECORD_TYPE
7326 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7327 size = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
7329 size = TYPE_SIZE (gnu_type);
7332 Set_Esize (gnat_entity, annotate_value (size));
7335 if (Unknown_Alignment (gnat_entity))
7336 Set_Alignment (gnat_entity,
7337 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7340 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7341 Return NULL_TREE if there is no such element in the list. */
7344 purpose_member_field (const_tree elem, tree list)
7348 tree field = TREE_PURPOSE (list);
7349 if (SAME_FIELD_P (field, elem))
7351 list = TREE_CHAIN (list);
7356 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7357 set Component_Bit_Offset and Esize of the components to the position and
7358 size used by Gigi. */
7361 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7363 Entity_Id gnat_field;
7366 /* We operate by first making a list of all fields and their position (we
7367 can get the size easily) and then update all the sizes in the tree. */
7369 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7370 BIGGEST_ALIGNMENT, NULL_TREE);
7372 for (gnat_field = First_Entity (gnat_entity);
7373 Present (gnat_field);
7374 gnat_field = Next_Entity (gnat_field))
7375 if (Ekind (gnat_field) == E_Component
7376 || (Ekind (gnat_field) == E_Discriminant
7377 && !Is_Unchecked_Union (Scope (gnat_field))))
7379 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7385 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7387 /* In this mode the tag and parent components are not
7388 generated, so we add the appropriate offset to each
7389 component. For a component appearing in the current
7390 extension, the offset is the size of the parent. */
7391 if (Is_Derived_Type (gnat_entity)
7392 && Original_Record_Component (gnat_field) == gnat_field)
7394 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7397 parent_offset = bitsize_int (POINTER_SIZE);
7400 parent_offset = bitsize_zero_node;
7402 Set_Component_Bit_Offset
7405 (size_binop (PLUS_EXPR,
7406 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7407 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7410 Set_Esize (gnat_field,
7411 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7413 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7415 /* If there is no entry, this is an inherited component whose
7416 position is the same as in the parent type. */
7417 Set_Component_Bit_Offset
7419 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7421 Set_Esize (gnat_field,
7422 Esize (Original_Record_Component (gnat_field)));
7427 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7428 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7429 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7430 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7431 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7432 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7433 pre-existing list to be chained to the newly created entries. */
7436 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7437 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7441 for (gnu_field = TYPE_FIELDS (gnu_type);
7443 gnu_field = TREE_CHAIN (gnu_field))
7445 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7446 DECL_FIELD_BIT_OFFSET (gnu_field));
7447 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7448 DECL_FIELD_OFFSET (gnu_field));
7449 unsigned int our_offset_align
7450 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7451 tree v = make_tree_vec (3);
7453 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7454 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7455 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7456 gnu_list = tree_cons (gnu_field, v, gnu_list);
7458 /* Recurse on internal fields, flattening the nested fields except for
7459 those in the variant part, if requested. */
7460 if (DECL_INTERNAL_P (gnu_field))
7462 tree gnu_field_type = TREE_TYPE (gnu_field);
7463 if (do_not_flatten_variant
7464 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7466 = build_position_list (gnu_field_type, do_not_flatten_variant,
7467 size_zero_node, bitsize_zero_node,
7468 BIGGEST_ALIGNMENT, gnu_list);
7471 = build_position_list (gnu_field_type, do_not_flatten_variant,
7472 gnu_our_offset, gnu_our_bitpos,
7473 our_offset_align, gnu_list);
7480 /* Return a TREE_LIST describing the substitutions needed to reflect the
7481 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7482 be in any order. TREE_PURPOSE gives the tree for the discriminant and
7483 TREE_VALUE is the replacement value. They are in the form of operands
7484 to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for a definition
7488 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7490 tree gnu_list = NULL_TREE;
7491 Entity_Id gnat_discrim;
7494 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7495 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7496 Present (gnat_discrim);
7497 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7498 gnat_value = Next_Elmt (gnat_value))
7499 /* Ignore access discriminants. */
7500 if (!Is_Access_Type (Etype (Node (gnat_value))))
7502 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7503 gnu_list = tree_cons (gnu_field,
7504 convert (TREE_TYPE (gnu_field),
7505 elaborate_expression
7506 (Node (gnat_value), gnat_subtype,
7507 get_entity_name (gnat_discrim),
7508 definition, true, false)),
7515 /* Scan all fields in QUAL_UNION_TYPE and return a TREE_LIST describing the
7516 variants of QUAL_UNION_TYPE that are still relevant after applying the
7517 substitutions described in SUBST_LIST. TREE_PURPOSE is the type of the
7518 variant and TREE_VALUE is a TREE_VEC containing the field, the new value
7519 of the qualifier and NULL_TREE respectively. GNU_LIST is a pre-existing
7520 list to be chained to the newly created entries. */
7523 build_variant_list (tree qual_union_type, tree subst_list, tree gnu_list)
7527 for (gnu_field = TYPE_FIELDS (qual_union_type);
7529 gnu_field = TREE_CHAIN (gnu_field))
7531 tree t, qual = DECL_QUALIFIER (gnu_field);
7533 for (t = subst_list; t; t = TREE_CHAIN (t))
7534 qual = SUBSTITUTE_IN_EXPR (qual, TREE_PURPOSE (t), TREE_VALUE (t));
7536 /* If the new qualifier is not unconditionally false, its variant may
7537 still be accessed. */
7538 if (!integer_zerop (qual))
7540 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7541 tree v = make_tree_vec (3);
7542 TREE_VEC_ELT (v, 0) = gnu_field;
7543 TREE_VEC_ELT (v, 1) = qual;
7544 TREE_VEC_ELT (v, 2) = NULL_TREE;
7545 gnu_list = tree_cons (variant_type, v, gnu_list);
7547 /* Recurse on the variant subpart of the variant, if any. */
7548 variant_subpart = get_variant_part (variant_type);
7549 if (variant_subpart)
7550 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7551 subst_list, gnu_list);
7553 /* If the new qualifier is unconditionally true, the subsequent
7554 variants cannot be accessed. */
7555 if (integer_onep (qual))
7563 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7564 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
7565 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
7566 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7567 for the size of a field. COMPONENT_P is true if we are being called
7568 to process the Component_Size of GNAT_OBJECT. This is used for error
7569 message handling and to indicate to use the object size of GNU_TYPE.
7570 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7571 it means that a size of zero should be treated as an unspecified size. */
7574 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7575 enum tree_code kind, bool component_p, bool zero_ok)
7577 Node_Id gnat_error_node;
7578 tree type_size, size;
7580 /* Return 0 if no size was specified. */
7581 if (uint_size == No_Uint)
7584 /* Ignore a negative size since that corresponds to our back-annotation. */
7585 if (UI_Lt (uint_size, Uint_0))
7588 /* Find the node to use for errors. */
7589 if ((Ekind (gnat_object) == E_Component
7590 || Ekind (gnat_object) == E_Discriminant)
7591 && Present (Component_Clause (gnat_object)))
7592 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7593 else if (Present (Size_Clause (gnat_object)))
7594 gnat_error_node = Expression (Size_Clause (gnat_object));
7596 gnat_error_node = gnat_object;
7598 /* Get the size as a tree. Issue an error if a size was specified but
7599 cannot be represented in sizetype. */
7600 size = UI_To_gnu (uint_size, bitsizetype);
7601 if (TREE_OVERFLOW (size))
7604 post_error_ne ("component size of & is too large", gnat_error_node,
7607 post_error_ne ("size of & is too large", gnat_error_node,
7612 /* Ignore a zero size if it is not permitted. */
7613 if (!zero_ok && integer_zerop (size))
7616 /* The size of objects is always a multiple of a byte. */
7617 if (kind == VAR_DECL
7618 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7621 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7622 gnat_error_node, gnat_object);
7624 post_error_ne ("size for& is not a multiple of Storage_Unit",
7625 gnat_error_node, gnat_object);
7629 /* If this is an integral type or a packed array type, the front-end has
7630 verified the size, so we need not do it here (which would entail
7631 checking against the bounds). However, if this is an aliased object,
7632 it may not be smaller than the type of the object. */
7633 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7634 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7637 /* If the object is a record that contains a template, add the size of
7638 the template to the specified size. */
7639 if (TREE_CODE (gnu_type) == RECORD_TYPE
7640 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7641 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7643 if (kind == VAR_DECL
7644 /* If a type needs strict alignment, a component of this type in
7645 a packed record cannot be packed and thus uses the type size. */
7646 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7647 type_size = TYPE_SIZE (gnu_type);
7649 type_size = rm_size (gnu_type);
7651 /* Modify the size of the type to be that of the maximum size if it has a
7653 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7654 type_size = max_size (type_size, true);
7656 /* If this is an access type or a fat pointer, the minimum size is that given
7657 by the smallest integral mode that's valid for pointers. */
7658 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7660 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7661 while (!targetm.valid_pointer_mode (p_mode))
7662 p_mode = GET_MODE_WIDER_MODE (p_mode);
7663 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7666 /* If the size of the object is a constant, the new size must not be
7668 if (TREE_CODE (type_size) != INTEGER_CST
7669 || TREE_OVERFLOW (type_size)
7670 || tree_int_cst_lt (size, type_size))
7674 ("component size for& too small{, minimum allowed is ^}",
7675 gnat_error_node, gnat_object, type_size);
7678 ("size for& too small{, minimum allowed is ^}",
7679 gnat_error_node, gnat_object, type_size);
7687 /* Similarly, but both validate and process a value of RM size. This
7688 routine is only called for types. */
7691 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7693 Node_Id gnat_attr_node;
7694 tree old_size, size;
7696 /* Do nothing if no size was specified. */
7697 if (uint_size == No_Uint)
7700 /* Ignore a negative size since that corresponds to our back-annotation. */
7701 if (UI_Lt (uint_size, Uint_0))
7704 /* Only issue an error if a Value_Size clause was explicitly given.
7705 Otherwise, we'd be duplicating an error on the Size clause. */
7707 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7709 /* Get the size as a tree. Issue an error if a size was specified but
7710 cannot be represented in sizetype. */
7711 size = UI_To_gnu (uint_size, bitsizetype);
7712 if (TREE_OVERFLOW (size))
7714 if (Present (gnat_attr_node))
7715 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7720 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7721 exists, or this is an integer type, in which case the front-end will
7722 have always set it. */
7723 if (No (gnat_attr_node)
7724 && integer_zerop (size)
7725 && !Has_Size_Clause (gnat_entity)
7726 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7729 old_size = rm_size (gnu_type);
7731 /* If the old size is self-referential, get the maximum size. */
7732 if (CONTAINS_PLACEHOLDER_P (old_size))
7733 old_size = max_size (old_size, true);
7735 /* If the size of the object is a constant, the new size must not be smaller
7736 (the front-end has verified this for scalar and packed array types). */
7737 if (TREE_CODE (old_size) != INTEGER_CST
7738 || TREE_OVERFLOW (old_size)
7739 || (AGGREGATE_TYPE_P (gnu_type)
7740 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7741 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7742 && !(TYPE_IS_PADDING_P (gnu_type)
7743 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7744 && TYPE_PACKED_ARRAY_TYPE_P
7745 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7746 && tree_int_cst_lt (size, old_size)))
7748 if (Present (gnat_attr_node))
7750 ("Value_Size for& too small{, minimum allowed is ^}",
7751 gnat_attr_node, gnat_entity, old_size);
7755 /* Otherwise, set the RM size proper for integral types... */
7756 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7757 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7758 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7759 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7760 SET_TYPE_RM_SIZE (gnu_type, size);
7762 /* ...or the Ada size for record and union types. */
7763 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7764 || TREE_CODE (gnu_type) == UNION_TYPE
7765 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7766 && !TYPE_FAT_POINTER_P (gnu_type))
7767 SET_TYPE_ADA_SIZE (gnu_type, size);
7770 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7771 If TYPE is the best type, return it. Otherwise, make a new type. We
7772 only support new integral and pointer types. FOR_BIASED is true if
7773 we are making a biased type. */
7776 make_type_from_size (tree type, tree size_tree, bool for_biased)
7778 unsigned HOST_WIDE_INT size;
7782 /* If size indicates an error, just return TYPE to avoid propagating
7783 the error. Likewise if it's too large to represent. */
7784 if (!size_tree || !host_integerp (size_tree, 1))
7787 size = tree_low_cst (size_tree, 1);
7789 switch (TREE_CODE (type))
7794 biased_p = (TREE_CODE (type) == INTEGER_TYPE
7795 && TYPE_BIASED_REPRESENTATION_P (type));
7797 /* Integer types with precision 0 are forbidden. */
7801 /* Only do something if the type is not a packed array type and
7802 doesn't already have the proper size. */
7803 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7804 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7807 biased_p |= for_biased;
7808 if (size > LONG_LONG_TYPE_SIZE)
7809 size = LONG_LONG_TYPE_SIZE;
7811 if (TYPE_UNSIGNED (type) || biased_p)
7812 new_type = make_unsigned_type (size);
7814 new_type = make_signed_type (size);
7815 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7816 SET_TYPE_RM_MIN_VALUE (new_type,
7817 convert (TREE_TYPE (new_type),
7818 TYPE_MIN_VALUE (type)));
7819 SET_TYPE_RM_MAX_VALUE (new_type,
7820 convert (TREE_TYPE (new_type),
7821 TYPE_MAX_VALUE (type)));
7822 /* Copy the name to show that it's essentially the same type and
7823 not a subrange type. */
7824 TYPE_NAME (new_type) = TYPE_NAME (type);
7825 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7826 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
7830 /* Do something if this is a fat pointer, in which case we
7831 may need to return the thin pointer. */
7832 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7834 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7835 if (!targetm.valid_pointer_mode (p_mode))
7838 build_pointer_type_for_mode
7839 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7845 /* Only do something if this is a thin pointer, in which case we
7846 may need to return the fat pointer. */
7847 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7849 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7859 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7860 a type or object whose present alignment is ALIGN. If this alignment is
7861 valid, return it. Otherwise, give an error and return ALIGN. */
7864 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7866 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7867 unsigned int new_align;
7868 Node_Id gnat_error_node;
7870 /* Don't worry about checking alignment if alignment was not specified
7871 by the source program and we already posted an error for this entity. */
7872 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7875 /* Post the error on the alignment clause if any. Note, for the implicit
7876 base type of an array type, the alignment clause is on the first
7878 if (Present (Alignment_Clause (gnat_entity)))
7879 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7881 else if (Is_Itype (gnat_entity)
7882 && Is_Array_Type (gnat_entity)
7883 && Etype (gnat_entity) == gnat_entity
7884 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7886 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
7889 gnat_error_node = gnat_entity;
7891 /* Within GCC, an alignment is an integer, so we must make sure a value is
7892 specified that fits in that range. Also, there is an upper bound to
7893 alignments we can support/allow. */
7894 if (!UI_Is_In_Int_Range (alignment)
7895 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7896 post_error_ne_num ("largest supported alignment for& is ^",
7897 gnat_error_node, gnat_entity, max_allowed_alignment);
7898 else if (!(Present (Alignment_Clause (gnat_entity))
7899 && From_At_Mod (Alignment_Clause (gnat_entity)))
7900 && new_align * BITS_PER_UNIT < align)
7902 unsigned int double_align;
7903 bool is_capped_double, align_clause;
7905 /* If the default alignment of "double" or larger scalar types is
7906 specifically capped and the new alignment is above the cap, do
7907 not post an error and change the alignment only if there is an
7908 alignment clause; this makes it possible to have the associated
7909 GCC type overaligned by default for performance reasons. */
7910 if ((double_align = double_float_alignment) > 0)
7913 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7915 = is_double_float_or_array (gnat_type, &align_clause);
7917 else if ((double_align = double_scalar_alignment) > 0)
7920 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7922 = is_double_scalar_or_array (gnat_type, &align_clause);
7925 is_capped_double = align_clause = false;
7927 if (is_capped_double && new_align >= double_align)
7930 align = new_align * BITS_PER_UNIT;
7934 if (is_capped_double)
7935 align = double_align * BITS_PER_UNIT;
7937 post_error_ne_num ("alignment for& must be at least ^",
7938 gnat_error_node, gnat_entity,
7939 align / BITS_PER_UNIT);
7944 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7945 if (new_align > align)
7952 /* Return the smallest alignment not less than SIZE. */
7955 ceil_alignment (unsigned HOST_WIDE_INT size)
7957 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7960 /* Verify that OBJECT, a type or decl, is something we can implement
7961 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7962 if we require atomic components. */
7965 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7967 Node_Id gnat_error_point = gnat_entity;
7969 enum machine_mode mode;
7973 /* There are three case of what OBJECT can be. It can be a type, in which
7974 case we take the size, alignment and mode from the type. It can be a
7975 declaration that was indirect, in which case the relevant values are
7976 that of the type being pointed to, or it can be a normal declaration,
7977 in which case the values are of the decl. The code below assumes that
7978 OBJECT is either a type or a decl. */
7979 if (TYPE_P (object))
7981 /* If this is an anonymous base type, nothing to check. Error will be
7982 reported on the source type. */
7983 if (!Comes_From_Source (gnat_entity))
7986 mode = TYPE_MODE (object);
7987 align = TYPE_ALIGN (object);
7988 size = TYPE_SIZE (object);
7990 else if (DECL_BY_REF_P (object))
7992 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7993 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7994 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7998 mode = DECL_MODE (object);
7999 align = DECL_ALIGN (object);
8000 size = DECL_SIZE (object);
8003 /* Consider all floating-point types atomic and any types that that are
8004 represented by integers no wider than a machine word. */
8005 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8006 || ((GET_MODE_CLASS (mode) == MODE_INT
8007 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8008 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8011 /* For the moment, also allow anything that has an alignment equal
8012 to its size and which is smaller than a word. */
8013 if (size && TREE_CODE (size) == INTEGER_CST
8014 && compare_tree_int (size, align) == 0
8015 && align <= BITS_PER_WORD)
8018 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8019 gnat_node = Next_Rep_Item (gnat_node))
8021 if (!comp_p && Nkind (gnat_node) == N_Pragma
8022 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8024 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8025 else if (comp_p && Nkind (gnat_node) == N_Pragma
8026 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8027 == Pragma_Atomic_Components))
8028 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8032 post_error_ne ("atomic access to component of & cannot be guaranteed",
8033 gnat_error_point, gnat_entity);
8035 post_error_ne ("atomic access to & cannot be guaranteed",
8036 gnat_error_point, gnat_entity);
8039 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
8040 have compatible signatures so that a call using one type may be safely
8041 issued if the actual target function type is the other. Return 1 if it is
8042 the case, 0 otherwise, and post errors on the incompatibilities.
8044 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
8045 that calls to the subprogram will have arguments suitable for the later
8046 underlying builtin expansion. */
8049 compatible_signatures_p (tree ftype1, tree ftype2)
8051 /* As of now, we only perform very trivial tests and consider it's the
8052 programmer's responsibility to ensure the type correctness in the Ada
8053 declaration, as in the regular Import cases.
8055 Mismatches typically result in either error messages from the builtin
8056 expander, internal compiler errors, or in a real call sequence. This
8057 should be refined to issue diagnostics helping error detection and
8060 /* Almost fake test, ensuring a use of each argument. */
8061 if (ftype1 == ftype2)
8067 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8068 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8069 specified size for this field. POS_LIST is a position list describing
8070 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8074 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8075 tree size, tree pos_list, tree subst_list)
8077 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8078 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8079 unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8080 tree new_pos, new_field;
8082 if (CONTAINS_PLACEHOLDER_P (pos))
8083 for (t = subst_list; t; t = TREE_CHAIN (t))
8084 pos = SUBSTITUTE_IN_EXPR (pos, TREE_PURPOSE (t), TREE_VALUE (t));
8086 /* If the position is now a constant, we can set it as the position of the
8087 field when we make it. Otherwise, we need to deal with it specially. */
8088 if (TREE_CONSTANT (pos))
8089 new_pos = bit_from_pos (pos, bitpos);
8091 new_pos = NULL_TREE;
8094 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8095 size, new_pos, DECL_PACKED (old_field),
8096 !DECL_NONADDRESSABLE_P (old_field));
8100 normalize_offset (&pos, &bitpos, offset_align);
8101 DECL_FIELD_OFFSET (new_field) = pos;
8102 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8103 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8104 DECL_SIZE (new_field) = size;
8105 DECL_SIZE_UNIT (new_field)
8106 = convert (sizetype,
8107 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8108 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8111 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8112 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8113 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8114 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8119 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8122 get_rep_part (tree record_type)
8124 tree field = TYPE_FIELDS (record_type);
8126 /* The REP part is the first field, internal, another record, and its name
8127 doesn't start with an underscore (i.e. is not generated by the FE). */
8128 if (DECL_INTERNAL_P (field)
8129 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8130 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
8136 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8139 get_variant_part (tree record_type)
8143 /* The variant part is the only internal field that is a qualified union. */
8144 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
8145 if (DECL_INTERNAL_P (field)
8146 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8152 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8153 the list of variants to be used and RECORD_TYPE is the type of the parent.
8154 POS_LIST is a position list describing the layout of fields present in
8155 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8159 create_variant_part_from (tree old_variant_part, tree variant_list,
8160 tree record_type, tree pos_list, tree subst_list)
8162 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8163 tree old_union_type = TREE_TYPE (old_variant_part);
8164 tree new_union_type, new_variant_part, t;
8165 tree union_field_list = NULL_TREE;
8167 /* First create the type of the variant part from that of the old one. */
8168 new_union_type = make_node (QUAL_UNION_TYPE);
8169 TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8171 /* If the position of the variant part is constant, subtract it from the
8172 size of the type of the parent to get the new size. This manual CSE
8173 reduces the code size when not optimizing. */
8174 if (TREE_CODE (offset) == INTEGER_CST)
8176 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8177 tree first_bit = bit_from_pos (offset, bitpos);
8178 TYPE_SIZE (new_union_type)
8179 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8180 TYPE_SIZE_UNIT (new_union_type)
8181 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8182 byte_from_pos (offset, bitpos));
8183 SET_TYPE_ADA_SIZE (new_union_type,
8184 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8186 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8187 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8190 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8192 /* Now finish up the new variants and populate the union type. */
8193 for (t = variant_list; t; t = TREE_CHAIN (t))
8195 tree old_field = TREE_VEC_ELT (TREE_VALUE (t), 0), new_field;
8196 tree old_variant, old_variant_subpart, new_variant, field_list;
8198 /* Skip variants that don't belong to this nesting level. */
8199 if (DECL_CONTEXT (old_field) != old_union_type)
8202 /* Retrieve the list of fields already added to the new variant. */
8203 new_variant = TREE_VEC_ELT (TREE_VALUE (t), 2);
8204 field_list = TYPE_FIELDS (new_variant);
8206 /* If the old variant had a variant subpart, we need to create a new
8207 variant subpart and add it to the field list. */
8208 old_variant = TREE_PURPOSE (t);
8209 old_variant_subpart = get_variant_part (old_variant);
8210 if (old_variant_subpart)
8212 tree new_variant_subpart
8213 = create_variant_part_from (old_variant_subpart, variant_list,
8214 new_variant, pos_list, subst_list);
8215 TREE_CHAIN (new_variant_subpart) = field_list;
8216 field_list = new_variant_subpart;
8219 /* Finish up the new variant and create the field. No need for debug
8220 info thanks to the XVS type. */
8221 finish_record_type (new_variant, nreverse (field_list), 2, false);
8222 compute_record_mode (new_variant);
8223 create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8224 true, false, Empty);
8227 = create_field_decl_from (old_field, new_variant, new_union_type,
8228 TYPE_SIZE (new_variant),
8229 pos_list, subst_list);
8230 DECL_QUALIFIER (new_field) = TREE_VEC_ELT (TREE_VALUE (t), 1);
8231 DECL_INTERNAL_P (new_field) = 1;
8232 TREE_CHAIN (new_field) = union_field_list;
8233 union_field_list = new_field;
8236 /* Finish up the union type and create the variant part. No need for debug
8237 info thanks to the XVS type. */
8238 finish_record_type (new_union_type, union_field_list, 2, false);
8239 compute_record_mode (new_union_type);
8240 create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8241 true, false, Empty);
8244 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8245 TYPE_SIZE (new_union_type),
8246 pos_list, subst_list);
8247 DECL_INTERNAL_P (new_variant_part) = 1;
8249 /* With multiple discriminants it is possible for an inner variant to be
8250 statically selected while outer ones are not; in this case, the list
8251 of fields of the inner variant is not flattened and we end up with a
8252 qualified union with a single member. Drop the useless container. */
8253 if (!TREE_CHAIN (union_field_list))
8255 DECL_CONTEXT (union_field_list) = record_type;
8256 DECL_FIELD_OFFSET (union_field_list)
8257 = DECL_FIELD_OFFSET (new_variant_part);
8258 DECL_FIELD_BIT_OFFSET (union_field_list)
8259 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8260 SET_DECL_OFFSET_ALIGN (union_field_list,
8261 DECL_OFFSET_ALIGN (new_variant_part));
8262 new_variant_part = union_field_list;
8265 return new_variant_part;
8268 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8269 which are both RECORD_TYPE, after applying the substitutions described
8273 copy_and_substitute_in_size (tree new_type, tree old_type, tree subst_list)
8277 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8278 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8279 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8280 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8281 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8283 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8284 for (t = subst_list; t; t = TREE_CHAIN (t))
8285 TYPE_SIZE (new_type)
8286 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8290 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8291 for (t = subst_list; t; t = TREE_CHAIN (t))
8292 TYPE_SIZE_UNIT (new_type)
8293 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8297 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8298 for (t = subst_list; t; t = TREE_CHAIN (t))
8300 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8304 /* Finalize the size. */
8305 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8306 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8309 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8310 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8311 updated by replacing F with R.
8313 The function doesn't update the layout of the type, i.e. it assumes
8314 that the substitution is purely formal. That's why the replacement
8315 value R must itself contain a PLACEHOLDER_EXPR. */
8318 substitute_in_type (tree t, tree f, tree r)
8322 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8324 switch (TREE_CODE (t))
8331 /* First the domain types of arrays. */
8332 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8333 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8335 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8336 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8338 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8342 TYPE_GCC_MIN_VALUE (nt) = low;
8343 TYPE_GCC_MAX_VALUE (nt) = high;
8345 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8347 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8352 /* Then the subtypes. */
8353 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8354 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8356 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8357 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8359 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8363 SET_TYPE_RM_MIN_VALUE (nt, low);
8364 SET_TYPE_RM_MAX_VALUE (nt, high);
8372 nt = substitute_in_type (TREE_TYPE (t), f, r);
8373 if (nt == TREE_TYPE (t))
8376 return build_complex_type (nt);
8382 /* These should never show up here. */
8387 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8388 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8390 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8393 nt = build_array_type (component, domain);
8394 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8395 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8396 SET_TYPE_MODE (nt, TYPE_MODE (t));
8397 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8398 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8399 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8400 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8401 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8407 case QUAL_UNION_TYPE:
8409 bool changed_field = false;
8412 /* Start out with no fields, make new fields, and chain them
8413 in. If we haven't actually changed the type of any field,
8414 discard everything we've done and return the old type. */
8416 TYPE_FIELDS (nt) = NULL_TREE;
8418 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
8420 tree new_field = copy_node (field), new_n;
8422 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8423 if (new_n != TREE_TYPE (field))
8425 TREE_TYPE (new_field) = new_n;
8426 changed_field = true;
8429 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8430 if (new_n != DECL_FIELD_OFFSET (field))
8432 DECL_FIELD_OFFSET (new_field) = new_n;
8433 changed_field = true;
8436 /* Do the substitution inside the qualifier, if any. */
8437 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8439 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8440 if (new_n != DECL_QUALIFIER (field))
8442 DECL_QUALIFIER (new_field) = new_n;
8443 changed_field = true;
8447 DECL_CONTEXT (new_field) = nt;
8448 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8450 TREE_CHAIN (new_field) = TYPE_FIELDS (nt);
8451 TYPE_FIELDS (nt) = new_field;
8457 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8458 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8459 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8460 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8469 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8470 needed to represent the object. */
8473 rm_size (tree gnu_type)
8475 /* For integral types, we store the RM size explicitly. */
8476 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8477 return TYPE_RM_SIZE (gnu_type);
8479 /* Return the RM size of the actual data plus the size of the template. */
8480 if (TREE_CODE (gnu_type) == RECORD_TYPE
8481 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8483 size_binop (PLUS_EXPR,
8484 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
8485 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8487 /* For record types, we store the size explicitly. */
8488 if ((TREE_CODE (gnu_type) == RECORD_TYPE
8489 || TREE_CODE (gnu_type) == UNION_TYPE
8490 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8491 && !TYPE_FAT_POINTER_P (gnu_type)
8492 && TYPE_ADA_SIZE (gnu_type))
8493 return TYPE_ADA_SIZE (gnu_type);
8495 /* For other types, this is just the size. */
8496 return TYPE_SIZE (gnu_type);
8499 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8500 fully-qualified name, possibly with type information encoding.
8501 Otherwise, return the name. */
8504 get_entity_name (Entity_Id gnat_entity)
8506 Get_Encoded_Name (gnat_entity);
8507 return get_identifier_with_length (Name_Buffer, Name_Len);
8510 /* Return an identifier representing the external name to be used for
8511 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8512 and the specified suffix. */
8515 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8517 Entity_Kind kind = Ekind (gnat_entity);
8521 String_Template temp = {1, strlen (suffix)};
8522 Fat_Pointer fp = {suffix, &temp};
8523 Get_External_Name_With_Suffix (gnat_entity, fp);
8526 Get_External_Name (gnat_entity, 0);
8528 /* A variable using the Stdcall convention lives in a DLL. We adjust
8529 its name to use the jump table, the _imp__NAME contains the address
8530 for the NAME variable. */
8531 if ((kind == E_Variable || kind == E_Constant)
8532 && Has_Stdcall_Convention (gnat_entity))
8534 const int len = 6 + Name_Len;
8535 char *new_name = (char *) alloca (len + 1);
8536 strcpy (new_name, "_imp__");
8537 strcat (new_name, Name_Buffer);
8538 return get_identifier_with_length (new_name, len);
8541 return get_identifier_with_length (Name_Buffer, Name_Len);
8544 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8545 string, return a new IDENTIFIER_NODE that is the concatenation of
8546 the name followed by "___" and the specified suffix. */
8549 concat_name (tree gnu_name, const char *suffix)
8551 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8552 char *new_name = (char *) alloca (len + 1);
8553 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8554 strcat (new_name, "___");
8555 strcat (new_name, suffix);
8556 return get_identifier_with_length (new_name, len);
8559 #include "gt-ada-decl.h"