1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2011, 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"
35 #include "tree-inline.h"
53 /* Convention_Stdcall should be processed in a specific way on 32 bits
54 Windows targets only. The macro below is a helper to avoid having to
55 check for a Windows specific attribute throughout this unit. */
57 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
59 #define Has_Stdcall_Convention(E) \
60 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #define Has_Stdcall_Convention(E) 0
68 /* Stack realignment is necessary for functions with foreign conventions when
69 the ABI doesn't mandate as much as what the compiler assumes - that is, up
70 to PREFERRED_STACK_BOUNDARY.
72 Such realignment can be requested with a dedicated function type attribute
73 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
74 characterize the situations where the attribute should be set. We rely on
75 compiler configuration settings for 'main' to decide. */
77 #ifdef MAIN_STACK_BOUNDARY
78 #define FOREIGN_FORCE_REALIGN_STACK \
79 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
81 #define FOREIGN_FORCE_REALIGN_STACK 0
86 struct incomplete *next;
91 /* These variables are used to defer recursively expanding incomplete types
92 while we are processing an array, a record or a subprogram type. */
93 static int defer_incomplete_level = 0;
94 static struct incomplete *defer_incomplete_list;
96 /* This variable is used to delay expanding From_With_Type types until the
98 static struct incomplete *defer_limited_with;
100 /* These variables are used to defer finalizing types. The element of the
101 list is the TYPE_DECL associated with the type. */
102 static int defer_finalize_level = 0;
103 static VEC (tree,heap) *defer_finalize_list;
105 typedef struct subst_pair_d {
110 DEF_VEC_O(subst_pair);
111 DEF_VEC_ALLOC_O(subst_pair,heap);
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
117 /* The associated field. */
120 /* The value of the qualifier. */
123 /* The record associated with this variant. */
127 DEF_VEC_O(variant_desc);
128 DEF_VEC_ALLOC_O(variant_desc,heap);
130 /* A hash table used to cache the result of annotate_value. */
131 static GTY ((if_marked ("tree_int_map_marked_p"),
132 param_is (struct tree_int_map))) htab_t annotate_value_cache;
141 static void relate_alias_sets (tree, tree, enum alias_set_op);
143 static bool allocatable_size_p (tree, bool);
144 static void prepend_one_attribute_to (struct attrib **,
145 enum attr_type, tree, tree, Node_Id);
146 static void prepend_attributes (Entity_Id, struct attrib **);
147 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
148 static bool is_variable_size (tree);
149 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
150 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
152 static tree make_packable_type (tree, bool);
153 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
154 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
156 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
157 static bool same_discriminant_p (Entity_Id, Entity_Id);
158 static bool array_type_has_nonaliased_component (tree, Entity_Id);
159 static bool compile_time_known_address_p (Node_Id);
160 static bool cannot_be_superflat_p (Node_Id);
161 static bool constructor_address_p (tree);
162 static void components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
163 bool, bool, bool, bool, tree *);
164 static Uint annotate_value (tree);
165 static void annotate_rep (Entity_Id, tree);
166 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
167 static VEC(subst_pair,heap) *build_subst_list (Entity_Id, Entity_Id, bool);
168 static VEC(variant_desc,heap) *build_variant_list (tree,
169 VEC(subst_pair,heap) *,
170 VEC(variant_desc,heap) *);
171 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
172 static void set_rm_size (Uint, tree, Entity_Id);
173 static tree make_type_from_size (tree, tree, bool);
174 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
175 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
176 static void check_ok_for_atomic (tree, Entity_Id, bool);
177 static tree create_field_decl_from (tree, tree, tree, tree, tree,
178 VEC(subst_pair,heap) *);
179 static tree get_rep_part (tree);
180 static tree get_variant_part (tree);
181 static tree create_variant_part_from (tree, VEC(variant_desc,heap) *, tree,
182 tree, VEC(subst_pair,heap) *);
183 static void copy_and_substitute_in_size (tree, tree, VEC(subst_pair,heap) *);
184 static void rest_of_type_decl_compilation_no_defer (tree);
185 static void finish_fat_pointer_type (tree, tree);
187 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
188 to pass around calls performing profile compatibility checks. */
191 Entity_Id gnat_entity; /* The Ada subprogram entity. */
192 tree ada_fntype; /* The corresponding GCC type node. */
193 tree btin_fntype; /* The GCC builtin function type node. */
196 static bool intrin_profiles_compatible_p (intrin_binding_t *);
198 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
199 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
200 and associate the ..._DECL node with the input GNAT defining identifier.
202 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
203 initial value (in GCC tree form). This is optional for a variable. For
204 a renamed entity, GNU_EXPR gives the object being renamed.
206 DEFINITION is nonzero if this call is intended for a definition. This is
207 used for separate compilation where it is necessary to know whether an
208 external declaration or a definition must be created if the GCC equivalent
209 was not created previously. The value of 1 is normally used for a nonzero
210 DEFINITION, but a value of 2 is used in special circumstances, defined in
214 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
216 /* Contains the kind of the input GNAT node. */
217 const Entity_Kind kind = Ekind (gnat_entity);
218 /* True if this is a type. */
219 const bool is_type = IN (kind, Type_Kind);
220 /* True if debug info is requested for this entity. */
221 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
222 /* True if this entity is to be considered as imported. */
223 const bool imported_p
224 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
225 /* For a type, contains the equivalent GNAT node to be used in gigi. */
226 Entity_Id gnat_equiv_type = Empty;
227 /* Temporary used to walk the GNAT tree. */
229 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
230 This node will be associated with the GNAT node by calling at the end
231 of the `switch' statement. */
232 tree gnu_decl = NULL_TREE;
233 /* Contains the GCC type to be used for the GCC node. */
234 tree gnu_type = NULL_TREE;
235 /* Contains the GCC size tree to be used for the GCC node. */
236 tree gnu_size = NULL_TREE;
237 /* Contains the GCC name to be used for the GCC node. */
238 tree gnu_entity_name;
239 /* True if we have already saved gnu_decl as a GNAT association. */
241 /* True if we incremented defer_incomplete_level. */
242 bool this_deferred = false;
243 /* True if we incremented force_global. */
244 bool this_global = false;
245 /* True if we should check to see if elaborated during processing. */
246 bool maybe_present = false;
247 /* True if we made GNU_DECL and its type here. */
248 bool this_made_decl = false;
249 /* Size and alignment of the GCC node, if meaningful. */
250 unsigned int esize = 0, align = 0;
251 /* Contains the list of attributes directly attached to the entity. */
252 struct attrib *attr_list = NULL;
254 /* Since a use of an Itype is a definition, process it as such if it
255 is not in a with'ed unit. */
258 && Is_Itype (gnat_entity)
259 && !present_gnu_tree (gnat_entity)
260 && In_Extended_Main_Code_Unit (gnat_entity))
262 /* Ensure that we are in a subprogram mentioned in the Scope chain of
263 this entity, our current scope is global, or we encountered a task
264 or entry (where we can't currently accurately check scoping). */
265 if (!current_function_decl
266 || DECL_ELABORATION_PROC_P (current_function_decl))
268 process_type (gnat_entity);
269 return get_gnu_tree (gnat_entity);
272 for (gnat_temp = Scope (gnat_entity);
274 gnat_temp = Scope (gnat_temp))
276 if (Is_Type (gnat_temp))
277 gnat_temp = Underlying_Type (gnat_temp);
279 if (Ekind (gnat_temp) == E_Subprogram_Body)
281 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
283 if (IN (Ekind (gnat_temp), Subprogram_Kind)
284 && Present (Protected_Body_Subprogram (gnat_temp)))
285 gnat_temp = Protected_Body_Subprogram (gnat_temp);
287 if (Ekind (gnat_temp) == E_Entry
288 || Ekind (gnat_temp) == E_Entry_Family
289 || Ekind (gnat_temp) == E_Task_Type
290 || (IN (Ekind (gnat_temp), Subprogram_Kind)
291 && present_gnu_tree (gnat_temp)
292 && (current_function_decl
293 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
295 process_type (gnat_entity);
296 return get_gnu_tree (gnat_entity);
300 /* This abort means the Itype has an incorrect scope, i.e. that its
301 scope does not correspond to the subprogram it is declared in. */
305 /* If we've already processed this entity, return what we got last time.
306 If we are defining the node, we should not have already processed it.
307 In that case, we will abort below when we try to save a new GCC tree
308 for this object. We also need to handle the case of getting a dummy
309 type when a Full_View exists. */
310 if ((!definition || (is_type && imported_p))
311 && present_gnu_tree (gnat_entity))
313 gnu_decl = get_gnu_tree (gnat_entity);
315 if (TREE_CODE (gnu_decl) == TYPE_DECL
316 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
317 && IN (kind, Incomplete_Or_Private_Kind)
318 && Present (Full_View (gnat_entity)))
321 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
322 save_gnu_tree (gnat_entity, NULL_TREE, false);
323 save_gnu_tree (gnat_entity, gnu_decl, false);
329 /* If this is a numeric or enumeral type, or an access type, a nonzero
330 Esize must be specified unless it was specified by the programmer. */
331 gcc_assert (!Unknown_Esize (gnat_entity)
332 || Has_Size_Clause (gnat_entity)
333 || (!IN (kind, Numeric_Kind)
334 && !IN (kind, Enumeration_Kind)
335 && (!IN (kind, Access_Kind)
336 || kind == E_Access_Protected_Subprogram_Type
337 || kind == E_Anonymous_Access_Protected_Subprogram_Type
338 || kind == E_Access_Subtype)));
340 /* The RM size must be specified for all discrete and fixed-point types. */
341 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
342 && Unknown_RM_Size (gnat_entity)));
344 /* If we get here, it means we have not yet done anything with this entity.
345 If we are not defining it, it must be a type or an entity that is defined
346 elsewhere or externally, otherwise we should have defined it already. */
347 gcc_assert (definition
348 || type_annotate_only
350 || kind == E_Discriminant
351 || kind == E_Component
353 || (kind == E_Constant && Present (Full_View (gnat_entity)))
354 || Is_Public (gnat_entity));
356 /* Get the name of the entity and set up the line number and filename of
357 the original definition for use in any decl we make. */
358 gnu_entity_name = get_entity_name (gnat_entity);
359 Sloc_to_locus (Sloc (gnat_entity), &input_location);
361 /* For cases when we are not defining (i.e., we are referencing from
362 another compilation unit) public entities, show we are at global level
363 for the purpose of computing scopes. Don't do this for components or
364 discriminants since the relevant test is whether or not the record is
365 being defined. Don't do this for constants either as we'll look into
366 their defining expression in the local context. */
368 && kind != E_Component
369 && kind != E_Discriminant
370 && kind != E_Constant
371 && Is_Public (gnat_entity)
372 && !Is_Statically_Allocated (gnat_entity))
373 force_global++, this_global = true;
375 /* Handle any attributes directly attached to the entity. */
376 if (Has_Gigi_Rep_Item (gnat_entity))
377 prepend_attributes (gnat_entity, &attr_list);
379 /* Do some common processing for types. */
382 /* Compute the equivalent type to be used in gigi. */
383 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
385 /* Machine_Attributes on types are expected to be propagated to
386 subtypes. The corresponding Gigi_Rep_Items are only attached
387 to the first subtype though, so we handle the propagation here. */
388 if (Base_Type (gnat_entity) != gnat_entity
389 && !Is_First_Subtype (gnat_entity)
390 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
391 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
394 /* Compute a default value for the size of the type. */
395 if (Known_Esize (gnat_entity)
396 && UI_Is_In_Int_Range (Esize (gnat_entity)))
398 unsigned int max_esize;
399 esize = UI_To_Int (Esize (gnat_entity));
401 if (IN (kind, Float_Kind))
402 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
403 else if (IN (kind, Access_Kind))
404 max_esize = POINTER_SIZE * 2;
406 max_esize = LONG_LONG_TYPE_SIZE;
408 if (esize > max_esize)
412 esize = LONG_LONG_TYPE_SIZE;
418 /* If this is a use of a deferred constant without address clause,
419 get its full definition. */
421 && No (Address_Clause (gnat_entity))
422 && Present (Full_View (gnat_entity)))
425 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
430 /* If we have an external constant that we are not defining, get the
431 expression that is was defined to represent. We may throw it away
432 later if it is not a constant. But do not retrieve the expression
433 if it is an allocator because the designated type might be dummy
436 && !No_Initialization (Declaration_Node (gnat_entity))
437 && Present (Expression (Declaration_Node (gnat_entity)))
438 && Nkind (Expression (Declaration_Node (gnat_entity)))
441 bool went_into_elab_proc = false;
443 /* The expression may contain N_Expression_With_Actions nodes and
444 thus object declarations from other units. In this case, even
445 though the expression will eventually be discarded since not a
446 constant, the declarations would be stuck either in the global
447 varpool or in the current scope. Therefore we force the local
448 context and create a fake scope that we'll zap at the end. */
449 if (!current_function_decl)
451 current_function_decl = get_elaboration_procedure ();
452 went_into_elab_proc = true;
456 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
459 if (went_into_elab_proc)
460 current_function_decl = NULL_TREE;
463 /* Ignore deferred constant definitions without address clause since
464 they are processed fully in the front-end. If No_Initialization
465 is set, this is not a deferred constant but a constant whose value
466 is built manually. And constants that are renamings are handled
470 && No (Address_Clause (gnat_entity))
471 && !No_Initialization (Declaration_Node (gnat_entity))
472 && No (Renamed_Object (gnat_entity)))
474 gnu_decl = error_mark_node;
479 /* Ignore constant definitions already marked with the error node. See
480 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
483 && present_gnu_tree (gnat_entity)
484 && get_gnu_tree (gnat_entity) == error_mark_node)
486 maybe_present = true;
493 /* We used to special case VMS exceptions here to directly map them to
494 their associated condition code. Since this code had to be masked
495 dynamically to strip off the severity bits, this caused trouble in
496 the GCC/ZCX case because the "type" pointers we store in the tables
497 have to be static. We now don't special case here anymore, and let
498 the regular processing take place, which leaves us with a regular
499 exception data object for VMS exceptions too. The condition code
500 mapping is taken care of by the front end and the bitmasking by the
507 /* The GNAT record where the component was defined. */
508 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
510 /* If the variable is an inherited record component (in the case of
511 extended record types), just return the inherited entity, which
512 must be a FIELD_DECL. Likewise for discriminants.
513 For discriminants of untagged records which have explicit
514 stored discriminants, return the entity for the corresponding
515 stored discriminant. Also use Original_Record_Component
516 if the record has a private extension. */
517 if (Present (Original_Record_Component (gnat_entity))
518 && Original_Record_Component (gnat_entity) != gnat_entity)
521 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
522 gnu_expr, definition);
527 /* If the enclosing record has explicit stored discriminants,
528 then it is an untagged record. If the Corresponding_Discriminant
529 is not empty then this must be a renamed discriminant and its
530 Original_Record_Component must point to the corresponding explicit
531 stored discriminant (i.e. we should have taken the previous
533 else if (Present (Corresponding_Discriminant (gnat_entity))
534 && Is_Tagged_Type (gnat_record))
536 /* A tagged record has no explicit stored discriminants. */
537 gcc_assert (First_Discriminant (gnat_record)
538 == First_Stored_Discriminant (gnat_record));
540 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
541 gnu_expr, definition);
546 else if (Present (CR_Discriminant (gnat_entity))
547 && type_annotate_only)
549 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
550 gnu_expr, definition);
555 /* If the enclosing record has explicit stored discriminants, then
556 it is an untagged record. If the Corresponding_Discriminant
557 is not empty then this must be a renamed discriminant and its
558 Original_Record_Component must point to the corresponding explicit
559 stored discriminant (i.e. we should have taken the first
561 else if (Present (Corresponding_Discriminant (gnat_entity))
562 && (First_Discriminant (gnat_record)
563 != First_Stored_Discriminant (gnat_record)))
566 /* Otherwise, if we are not defining this and we have no GCC type
567 for the containing record, make one for it. Then we should
568 have made our own equivalent. */
569 else if (!definition && !present_gnu_tree (gnat_record))
571 /* ??? If this is in a record whose scope is a protected
572 type and we have an Original_Record_Component, use it.
573 This is a workaround for major problems in protected type
575 Entity_Id Scop = Scope (Scope (gnat_entity));
576 if ((Is_Protected_Type (Scop)
577 || (Is_Private_Type (Scop)
578 && Present (Full_View (Scop))
579 && Is_Protected_Type (Full_View (Scop))))
580 && Present (Original_Record_Component (gnat_entity)))
583 = gnat_to_gnu_entity (Original_Record_Component
590 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
591 gnu_decl = get_gnu_tree (gnat_entity);
597 /* Here we have no GCC type and this is a reference rather than a
598 definition. This should never happen. Most likely the cause is
599 reference before declaration in the gnat tree for gnat_entity. */
603 case E_Loop_Parameter:
604 case E_Out_Parameter:
607 /* Simple variables, loop variables, Out parameters and exceptions. */
611 = ((kind == E_Constant || kind == E_Variable)
612 && Is_True_Constant (gnat_entity)
613 && !Treat_As_Volatile (gnat_entity)
614 && (((Nkind (Declaration_Node (gnat_entity))
615 == N_Object_Declaration)
616 && Present (Expression (Declaration_Node (gnat_entity))))
617 || Present (Renamed_Object (gnat_entity))
619 bool inner_const_flag = const_flag;
620 bool static_p = Is_Statically_Allocated (gnat_entity);
621 bool mutable_p = false;
622 bool used_by_ref = false;
623 tree gnu_ext_name = NULL_TREE;
624 tree renamed_obj = NULL_TREE;
625 tree gnu_object_size;
627 if (Present (Renamed_Object (gnat_entity)) && !definition)
629 if (kind == E_Exception)
630 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
633 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
636 /* Get the type after elaborating the renamed object. */
637 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
639 /* If this is a standard exception definition, then use the standard
640 exception type. This is necessary to make sure that imported and
641 exported views of exceptions are properly merged in LTO mode. */
642 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
643 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
644 gnu_type = except_type_node;
646 /* For a debug renaming declaration, build a debug-only entity. */
647 if (Present (Debug_Renaming_Link (gnat_entity)))
649 /* Force a non-null value to make sure the symbol is retained. */
650 tree value = build1 (INDIRECT_REF, gnu_type,
652 build_pointer_type (gnu_type),
653 integer_minus_one_node));
654 gnu_decl = build_decl (input_location,
655 VAR_DECL, gnu_entity_name, gnu_type);
656 SET_DECL_VALUE_EXPR (gnu_decl, value);
657 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
658 gnat_pushdecl (gnu_decl, gnat_entity);
662 /* If this is a loop variable, its type should be the base type.
663 This is because the code for processing a loop determines whether
664 a normal loop end test can be done by comparing the bounds of the
665 loop against those of the base type, which is presumed to be the
666 size used for computation. But this is not correct when the size
667 of the subtype is smaller than the type. */
668 if (kind == E_Loop_Parameter)
669 gnu_type = get_base_type (gnu_type);
671 /* Reject non-renamed objects whose type is an unconstrained array or
672 any object whose type is a dummy type or void. */
673 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
674 && No (Renamed_Object (gnat_entity)))
675 || TYPE_IS_DUMMY_P (gnu_type)
676 || TREE_CODE (gnu_type) == VOID_TYPE)
678 gcc_assert (type_annotate_only);
681 return error_mark_node;
684 /* If an alignment is specified, use it if valid. Note that exceptions
685 are objects but don't have an alignment. We must do this before we
686 validate the size, since the alignment can affect the size. */
687 if (kind != E_Exception && Known_Alignment (gnat_entity))
689 gcc_assert (Present (Alignment (gnat_entity)));
691 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
692 TYPE_ALIGN (gnu_type));
694 /* No point in changing the type if there is an address clause
695 as the final type of the object will be a reference type. */
696 if (Present (Address_Clause (gnat_entity)))
700 tree orig_type = gnu_type;
703 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
704 false, false, definition, true);
706 /* If a padding record was made, declare it now since it will
707 never be declared otherwise. This is necessary to ensure
708 that its subtrees are properly marked. */
709 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
710 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
711 debug_info_p, gnat_entity);
715 /* If we are defining the object, see if it has a Size and validate it
716 if so. If we are not defining the object and a Size clause applies,
717 simply retrieve the value. We don't want to ignore the clause and
718 it is expected to have been validated already. Then get the new
721 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
722 gnat_entity, VAR_DECL, false,
723 Has_Size_Clause (gnat_entity));
724 else if (Has_Size_Clause (gnat_entity))
725 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
730 = make_type_from_size (gnu_type, gnu_size,
731 Has_Biased_Representation (gnat_entity));
733 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
734 gnu_size = NULL_TREE;
737 /* If this object has self-referential size, it must be a record with
738 a default discriminant. We are supposed to allocate an object of
739 the maximum size in this case, unless it is a constant with an
740 initializing expression, in which case we can get the size from
741 that. Note that the resulting size may still be a variable, so
742 this may end up with an indirect allocation. */
743 if (No (Renamed_Object (gnat_entity))
744 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
746 if (gnu_expr && kind == E_Constant)
748 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
749 if (CONTAINS_PLACEHOLDER_P (size))
751 /* If the initializing expression is itself a constant,
752 despite having a nominal type with self-referential
753 size, we can get the size directly from it. */
754 if (TREE_CODE (gnu_expr) == COMPONENT_REF
756 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
757 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
758 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
759 || DECL_READONLY_ONCE_ELAB
760 (TREE_OPERAND (gnu_expr, 0))))
761 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
764 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
769 /* We may have no GNU_EXPR because No_Initialization is
770 set even though there's an Expression. */
771 else if (kind == E_Constant
772 && (Nkind (Declaration_Node (gnat_entity))
773 == N_Object_Declaration)
774 && Present (Expression (Declaration_Node (gnat_entity))))
776 = TYPE_SIZE (gnat_to_gnu_type
778 (Expression (Declaration_Node (gnat_entity)))));
781 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
786 /* If the size is zero byte, make it one byte since some linkers have
787 troubles with zero-sized objects. If the object will have a
788 template, that will make it nonzero so don't bother. Also avoid
789 doing that for an object renaming or an object with an address
790 clause, as we would lose useful information on the view size
791 (e.g. for null array slices) and we are not allocating the object
794 && integer_zerop (gnu_size)
795 && !TREE_OVERFLOW (gnu_size))
796 || (TYPE_SIZE (gnu_type)
797 && integer_zerop (TYPE_SIZE (gnu_type))
798 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
799 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
800 || !Is_Array_Type (Etype (gnat_entity)))
801 && No (Renamed_Object (gnat_entity))
802 && No (Address_Clause (gnat_entity)))
803 gnu_size = bitsize_unit_node;
805 /* If this is an object with no specified size and alignment, and
806 if either it is atomic or we are not optimizing alignment for
807 space and it is composite and not an exception, an Out parameter
808 or a reference to another object, and the size of its type is a
809 constant, set the alignment to the smallest one which is not
810 smaller than the size, with an appropriate cap. */
811 if (!gnu_size && align == 0
812 && (Is_Atomic (gnat_entity)
813 || (!Optimize_Alignment_Space (gnat_entity)
814 && kind != E_Exception
815 && kind != E_Out_Parameter
816 && Is_Composite_Type (Etype (gnat_entity))
817 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
818 && !Is_Exported (gnat_entity)
820 && No (Renamed_Object (gnat_entity))
821 && No (Address_Clause (gnat_entity))))
822 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
824 /* No point in jumping through all the hoops needed in order
825 to support BIGGEST_ALIGNMENT if we don't really have to.
826 So we cap to the smallest alignment that corresponds to
827 a known efficient memory access pattern of the target. */
828 unsigned int align_cap = Is_Atomic (gnat_entity)
830 : get_mode_alignment (ptr_mode);
832 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
833 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
836 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
838 /* But make sure not to under-align the object. */
839 if (align <= TYPE_ALIGN (gnu_type))
842 /* And honor the minimum valid atomic alignment, if any. */
843 #ifdef MINIMUM_ATOMIC_ALIGNMENT
844 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
845 align = MINIMUM_ATOMIC_ALIGNMENT;
849 /* If the object is set to have atomic components, find the component
850 type and validate it.
852 ??? Note that we ignore Has_Volatile_Components on objects; it's
853 not at all clear what to do in that case. */
854 if (Has_Atomic_Components (gnat_entity))
856 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
857 ? TREE_TYPE (gnu_type) : gnu_type);
859 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
860 && TYPE_MULTI_ARRAY_P (gnu_inner))
861 gnu_inner = TREE_TYPE (gnu_inner);
863 check_ok_for_atomic (gnu_inner, gnat_entity, true);
866 /* Now check if the type of the object allows atomic access. Note
867 that we must test the type, even if this object has size and
868 alignment to allow such access, because we will be going inside
869 the padded record to assign to the object. We could fix this by
870 always copying via an intermediate value, but it's not clear it's
872 if (Is_Atomic (gnat_entity))
873 check_ok_for_atomic (gnu_type, gnat_entity, false);
875 /* If this is an aliased object with an unconstrained nominal subtype,
876 make a type that includes the template. */
877 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
878 && Is_Array_Type (Etype (gnat_entity))
879 && !type_annotate_only)
882 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
884 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
885 concat_name (gnu_entity_name,
890 #ifdef MINIMUM_ATOMIC_ALIGNMENT
891 /* If the size is a constant and no alignment is specified, force
892 the alignment to be the minimum valid atomic alignment. The
893 restriction on constant size avoids problems with variable-size
894 temporaries; if the size is variable, there's no issue with
895 atomic access. Also don't do this for a constant, since it isn't
896 necessary and can interfere with constant replacement. Finally,
897 do not do it for Out parameters since that creates an
898 size inconsistency with In parameters. */
899 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
900 && !FLOAT_TYPE_P (gnu_type)
901 && !const_flag && No (Renamed_Object (gnat_entity))
902 && !imported_p && No (Address_Clause (gnat_entity))
903 && kind != E_Out_Parameter
904 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
905 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
906 align = MINIMUM_ATOMIC_ALIGNMENT;
909 /* Make a new type with the desired size and alignment, if needed.
910 But do not take into account alignment promotions to compute the
911 size of the object. */
912 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
913 if (gnu_size || align > 0)
914 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
915 false, false, definition,
916 gnu_size ? true : false);
918 /* If this is a renaming, avoid as much as possible to create a new
919 object. However, in several cases, creating it is required.
920 This processing needs to be applied to the raw expression so
921 as to make it more likely to rename the underlying object. */
922 if (Present (Renamed_Object (gnat_entity)))
924 bool create_normal_object = false;
926 /* If the renamed object had padding, strip off the reference
927 to the inner object and reset our type. */
928 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
929 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
930 /* Strip useless conversions around the object. */
931 || (TREE_CODE (gnu_expr) == NOP_EXPR
932 && gnat_types_compatible_p
933 (TREE_TYPE (gnu_expr),
934 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
936 gnu_expr = TREE_OPERAND (gnu_expr, 0);
937 gnu_type = TREE_TYPE (gnu_expr);
940 /* Case 1: If this is a constant renaming stemming from a function
941 call, treat it as a normal object whose initial value is what
942 is being renamed. RM 3.3 says that the result of evaluating a
943 function call is a constant object. As a consequence, it can
944 be the inner object of a constant renaming. In this case, the
945 renaming must be fully instantiated, i.e. it cannot be a mere
946 reference to (part of) an existing object. */
949 tree inner_object = gnu_expr;
950 while (handled_component_p (inner_object))
951 inner_object = TREE_OPERAND (inner_object, 0);
952 if (TREE_CODE (inner_object) == CALL_EXPR)
953 create_normal_object = true;
956 /* Otherwise, see if we can proceed with a stabilized version of
957 the renamed entity or if we need to make a new object. */
958 if (!create_normal_object)
960 tree maybe_stable_expr = NULL_TREE;
963 /* Case 2: If the renaming entity need not be materialized and
964 the renamed expression is something we can stabilize, use
965 that for the renaming. At the global level, we can only do
966 this if we know no SAVE_EXPRs need be made, because the
967 expression we return might be used in arbitrary conditional
968 branches so we must force the evaluation of the SAVE_EXPRs
969 immediately and this requires a proper function context.
970 Note that an external constant is at the global level. */
971 if (!Materialize_Entity (gnat_entity)
972 && (!((!definition && kind == E_Constant)
973 || global_bindings_p ())
974 || (staticp (gnu_expr)
975 && !TREE_SIDE_EFFECTS (gnu_expr))))
978 = gnat_stabilize_reference (gnu_expr, true, &stable);
982 /* ??? No DECL_EXPR is created so we need to mark
983 the expression manually lest it is shared. */
984 if ((!definition && kind == E_Constant)
985 || global_bindings_p ())
986 MARK_VISITED (maybe_stable_expr);
987 gnu_decl = maybe_stable_expr;
988 save_gnu_tree (gnat_entity, gnu_decl, true);
990 annotate_object (gnat_entity, gnu_type, NULL_TREE,
995 /* The stabilization failed. Keep maybe_stable_expr
996 untouched here to let the pointer case below know
997 about that failure. */
1000 /* Case 3: If this is a constant renaming and creating a
1001 new object is allowed and cheap, treat it as a normal
1002 object whose initial value is what is being renamed. */
1004 && !Is_Composite_Type
1005 (Underlying_Type (Etype (gnat_entity))))
1008 /* Case 4: Make this into a constant pointer to the object we
1009 are to rename and attach the object to the pointer if it is
1010 something we can stabilize.
1012 From the proper scope, attached objects will be referenced
1013 directly instead of indirectly via the pointer to avoid
1014 subtle aliasing problems with non-addressable entities.
1015 They have to be stable because we must not evaluate the
1016 variables in the expression every time the renaming is used.
1017 The pointer is called a "renaming" pointer in this case.
1019 In the rare cases where we cannot stabilize the renamed
1020 object, we just make a "bare" pointer, and the renamed
1021 entity is always accessed indirectly through it. */
1024 gnu_type = build_reference_type (gnu_type);
1025 inner_const_flag = TREE_READONLY (gnu_expr);
1028 /* If the previous attempt at stabilizing failed, there
1029 is no point in trying again and we reuse the result
1030 without attaching it to the pointer. In this case it
1031 will only be used as the initializing expression of
1032 the pointer and thus needs no special treatment with
1033 regard to multiple evaluations. */
1034 if (maybe_stable_expr)
1037 /* Otherwise, try to stabilize and attach the expression
1038 to the pointer if the stabilization succeeds.
1040 Note that this might introduce SAVE_EXPRs and we don't
1041 check whether we're at the global level or not. This
1042 is fine since we are building a pointer initializer and
1043 neither the pointer nor the initializing expression can
1044 be accessed before the pointer elaboration has taken
1045 place in a correct program.
1047 These SAVE_EXPRs will be evaluated at the right place
1048 by either the evaluation of the initializer for the
1049 non-global case or the elaboration code for the global
1050 case, and will be attached to the elaboration procedure
1051 in the latter case. */
1055 = gnat_stabilize_reference (gnu_expr, true, &stable);
1058 renamed_obj = maybe_stable_expr;
1060 /* Attaching is actually performed downstream, as soon
1061 as we have a VAR_DECL for the pointer we make. */
1064 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1067 gnu_size = NULL_TREE;
1073 /* Make a volatile version of this object's type if we are to make
1074 the object volatile. We also interpret 13.3(19) conservatively
1075 and disallow any optimizations for such a non-constant object. */
1076 if ((Treat_As_Volatile (gnat_entity)
1078 && gnu_type != except_type_node
1079 && (Is_Exported (gnat_entity)
1081 || Present (Address_Clause (gnat_entity)))))
1082 && !TYPE_VOLATILE (gnu_type))
1083 gnu_type = build_qualified_type (gnu_type,
1084 (TYPE_QUALS (gnu_type)
1085 | TYPE_QUAL_VOLATILE));
1087 /* If we are defining an aliased object whose nominal subtype is
1088 unconstrained, the object is a record that contains both the
1089 template and the object. If there is an initializer, it will
1090 have already been converted to the right type, but we need to
1091 create the template if there is no initializer. */
1094 && TREE_CODE (gnu_type) == RECORD_TYPE
1095 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1096 /* Beware that padding might have been introduced above. */
1097 || (TYPE_PADDING_P (gnu_type)
1098 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1100 && TYPE_CONTAINS_TEMPLATE_P
1101 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1104 = TYPE_PADDING_P (gnu_type)
1105 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1106 : TYPE_FIELDS (gnu_type);
1107 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
1108 tree t = build_template (TREE_TYPE (template_field),
1109 TREE_TYPE (DECL_CHAIN (template_field)),
1111 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1112 gnu_expr = gnat_build_constructor (gnu_type, v);
1115 /* Convert the expression to the type of the object except in the
1116 case where the object's type is unconstrained or the object's type
1117 is a padded record whose field is of self-referential size. In
1118 the former case, converting will generate unnecessary evaluations
1119 of the CONSTRUCTOR to compute the size and in the latter case, we
1120 want to only copy the actual data. */
1122 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1123 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1124 && !(TYPE_IS_PADDING_P (gnu_type)
1125 && CONTAINS_PLACEHOLDER_P
1126 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1127 gnu_expr = convert (gnu_type, gnu_expr);
1129 /* If this is a pointer that doesn't have an initializing expression,
1130 initialize it to NULL, unless the object is imported. */
1132 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1134 && !Is_Imported (gnat_entity))
1135 gnu_expr = integer_zero_node;
1137 /* If we are defining the object and it has an Address clause, we must
1138 either get the address expression from the saved GCC tree for the
1139 object if it has a Freeze node, or elaborate the address expression
1140 here since the front-end has guaranteed that the elaboration has no
1141 effects in this case. */
1142 if (definition && Present (Address_Clause (gnat_entity)))
1144 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1146 = present_gnu_tree (gnat_entity)
1147 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1149 save_gnu_tree (gnat_entity, NULL_TREE, false);
1151 /* Ignore the size. It's either meaningless or was handled
1153 gnu_size = NULL_TREE;
1154 /* Convert the type of the object to a reference type that can
1155 alias everything as per 13.3(19). */
1157 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1158 gnu_address = convert (gnu_type, gnu_address);
1161 = !Is_Public (gnat_entity)
1162 || compile_time_known_address_p (gnat_expr);
1164 /* If this is a deferred constant, the initializer is attached to
1166 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1169 (Expression (Declaration_Node (Full_View (gnat_entity))));
1171 /* If we don't have an initializing expression for the underlying
1172 variable, the initializing expression for the pointer is the
1173 specified address. Otherwise, we have to make a COMPOUND_EXPR
1174 to assign both the address and the initial value. */
1176 gnu_expr = gnu_address;
1179 = build2 (COMPOUND_EXPR, gnu_type,
1181 (MODIFY_EXPR, NULL_TREE,
1182 build_unary_op (INDIRECT_REF, NULL_TREE,
1188 /* If it has an address clause and we are not defining it, mark it
1189 as an indirect object. Likewise for Stdcall objects that are
1191 if ((!definition && Present (Address_Clause (gnat_entity)))
1192 || (Is_Imported (gnat_entity)
1193 && Has_Stdcall_Convention (gnat_entity)))
1195 /* Convert the type of the object to a reference type that can
1196 alias everything as per 13.3(19). */
1198 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1199 gnu_size = NULL_TREE;
1201 /* No point in taking the address of an initializing expression
1202 that isn't going to be used. */
1203 gnu_expr = NULL_TREE;
1205 /* If it has an address clause whose value is known at compile
1206 time, make the object a CONST_DECL. This will avoid a
1207 useless dereference. */
1208 if (Present (Address_Clause (gnat_entity)))
1210 Node_Id gnat_address
1211 = Expression (Address_Clause (gnat_entity));
1213 if (compile_time_known_address_p (gnat_address))
1215 gnu_expr = gnat_to_gnu (gnat_address);
1223 /* If we are at top level and this object is of variable size,
1224 make the actual type a hidden pointer to the real type and
1225 make the initializer be a memory allocation and initialization.
1226 Likewise for objects we aren't defining (presumed to be
1227 external references from other packages), but there we do
1228 not set up an initialization.
1230 If the object's size overflows, make an allocator too, so that
1231 Storage_Error gets raised. Note that we will never free
1232 such memory, so we presume it never will get allocated. */
1233 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1234 global_bindings_p ()
1237 || (gnu_size && !allocatable_size_p (gnu_size,
1238 global_bindings_p ()
1242 gnu_type = build_reference_type (gnu_type);
1243 gnu_size = NULL_TREE;
1246 /* In case this was a aliased object whose nominal subtype is
1247 unconstrained, the pointer above will be a thin pointer and
1248 build_allocator will automatically make the template.
1250 If we have a template initializer only (that we made above),
1251 pretend there is none and rely on what build_allocator creates
1252 again anyway. Otherwise (if we have a full initializer), get
1253 the data part and feed that to build_allocator.
1255 If we are elaborating a mutable object, tell build_allocator to
1256 ignore a possibly simpler size from the initializer, if any, as
1257 we must allocate the maximum possible size in this case. */
1258 if (definition && !imported_p)
1260 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1262 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1263 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1266 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1268 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1269 && 1 == VEC_length (constructor_elt,
1270 CONSTRUCTOR_ELTS (gnu_expr)))
1274 = build_component_ref
1275 (gnu_expr, NULL_TREE,
1276 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1280 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1281 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type)))
1282 post_error ("?`Storage_Error` will be raised at run time!",
1286 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1287 Empty, Empty, gnat_entity, mutable_p);
1292 gnu_expr = NULL_TREE;
1297 /* If this object would go into the stack and has an alignment larger
1298 than the largest stack alignment the back-end can honor, resort to
1299 a variable of "aligning type". */
1300 if (!global_bindings_p () && !static_p && definition
1301 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1303 /* Create the new variable. No need for extra room before the
1304 aligned field as this is in automatic storage. */
1306 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1307 TYPE_SIZE_UNIT (gnu_type),
1308 BIGGEST_ALIGNMENT, 0);
1310 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1311 NULL_TREE, gnu_new_type, NULL_TREE, false,
1312 false, false, false, NULL, gnat_entity);
1314 /* Initialize the aligned field if we have an initializer. */
1317 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1319 (gnu_new_var, NULL_TREE,
1320 TYPE_FIELDS (gnu_new_type), false),
1324 /* And setup this entity as a reference to the aligned field. */
1325 gnu_type = build_reference_type (gnu_type);
1328 (ADDR_EXPR, gnu_type,
1329 build_component_ref (gnu_new_var, NULL_TREE,
1330 TYPE_FIELDS (gnu_new_type), false));
1332 gnu_size = NULL_TREE;
1338 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1339 | TYPE_QUAL_CONST));
1341 /* Convert the expression to the type of the object except in the
1342 case where the object's type is unconstrained or the object's type
1343 is a padded record whose field is of self-referential size. In
1344 the former case, converting will generate unnecessary evaluations
1345 of the CONSTRUCTOR to compute the size and in the latter case, we
1346 want to only copy the actual data. */
1348 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1349 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1350 && !(TYPE_IS_PADDING_P (gnu_type)
1351 && CONTAINS_PLACEHOLDER_P
1352 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1353 gnu_expr = convert (gnu_type, gnu_expr);
1355 /* If this name is external or there was a name specified, use it,
1356 unless this is a VMS exception object since this would conflict
1357 with the symbol we need to export in addition. Don't use the
1358 Interface_Name if there is an address clause (see CD30005). */
1359 if (!Is_VMS_Exception (gnat_entity)
1360 && ((Present (Interface_Name (gnat_entity))
1361 && No (Address_Clause (gnat_entity)))
1362 || (Is_Public (gnat_entity)
1363 && (!Is_Imported (gnat_entity)
1364 || Is_Exported (gnat_entity)))))
1365 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1367 /* If this is an aggregate constant initialized to a constant, force it
1368 to be statically allocated. This saves an initialization copy. */
1371 && gnu_expr && TREE_CONSTANT (gnu_expr)
1372 && AGGREGATE_TYPE_P (gnu_type)
1373 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1374 && !(TYPE_IS_PADDING_P (gnu_type)
1375 && !host_integerp (TYPE_SIZE_UNIT
1376 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1379 /* Now create the variable or the constant and set various flags. */
1381 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1382 gnu_expr, const_flag, Is_Public (gnat_entity),
1383 imported_p || !definition, static_p, attr_list,
1385 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1386 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1388 /* If we are defining an Out parameter and optimization isn't enabled,
1389 create a fake PARM_DECL for debugging purposes and make it point to
1390 the VAR_DECL. Suppress debug info for the latter but make sure it
1391 will live on the stack so that it can be accessed from within the
1392 debugger through the PARM_DECL. */
1393 if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1395 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1396 gnat_pushdecl (param, gnat_entity);
1397 SET_DECL_VALUE_EXPR (param, gnu_decl);
1398 DECL_HAS_VALUE_EXPR_P (param) = 1;
1399 DECL_IGNORED_P (gnu_decl) = 1;
1400 TREE_ADDRESSABLE (gnu_decl) = 1;
1403 /* If this is a renaming pointer, attach the renamed object to it and
1404 register it if we are at the global level. Note that an external
1405 constant is at the global level. */
1406 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1408 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1409 if ((!definition && kind == E_Constant) || global_bindings_p ())
1411 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1412 record_global_renaming_pointer (gnu_decl);
1416 /* If this is a constant and we are defining it or it generates a real
1417 symbol at the object level and we are referencing it, we may want
1418 or need to have a true variable to represent it:
1419 - if optimization isn't enabled, for debugging purposes,
1420 - if the constant is public and not overlaid on something else,
1421 - if its address is taken,
1422 - if either itself or its type is aliased. */
1423 if (TREE_CODE (gnu_decl) == CONST_DECL
1424 && (definition || Sloc (gnat_entity) > Standard_Location)
1425 && ((!optimize && debug_info_p)
1426 || (Is_Public (gnat_entity)
1427 && No (Address_Clause (gnat_entity)))
1428 || Address_Taken (gnat_entity)
1429 || Is_Aliased (gnat_entity)
1430 || Is_Aliased (Etype (gnat_entity))))
1433 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1434 gnu_expr, true, Is_Public (gnat_entity),
1435 !definition, static_p, attr_list,
1438 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1440 /* As debugging information will be generated for the variable,
1441 do not generate debugging information for the constant. */
1443 DECL_IGNORED_P (gnu_decl) = 1;
1445 DECL_IGNORED_P (gnu_corr_var) = 1;
1448 /* If this is a constant, even if we don't need a true variable, we
1449 may need to avoid returning the initializer in every case. That
1450 can happen for the address of a (constant) constructor because,
1451 upon dereferencing it, the constructor will be reinjected in the
1452 tree, which may not be valid in every case; see lvalue_required_p
1453 for more details. */
1454 if (TREE_CODE (gnu_decl) == CONST_DECL)
1455 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1457 /* If this object is declared in a block that contains a block with an
1458 exception handler, and we aren't using the GCC exception mechanism,
1459 we must force this variable in memory in order to avoid an invalid
1461 if (Exception_Mechanism != Back_End_Exceptions
1462 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1463 TREE_ADDRESSABLE (gnu_decl) = 1;
1465 /* If we are defining an object with variable size or an object with
1466 fixed size that will be dynamically allocated, and we are using the
1467 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1469 && Exception_Mechanism == Setjmp_Longjmp
1470 && get_block_jmpbuf_decl ()
1471 && DECL_SIZE_UNIT (gnu_decl)
1472 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1473 || (flag_stack_check == GENERIC_STACK_CHECK
1474 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1475 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1476 add_stmt_with_node (build_call_1_expr
1477 (update_setjmp_buf_decl,
1478 build_unary_op (ADDR_EXPR, NULL_TREE,
1479 get_block_jmpbuf_decl ())),
1482 /* Back-annotate Esize and Alignment of the object if not already
1483 known. Note that we pick the values of the type, not those of
1484 the object, to shield ourselves from low-level platform-dependent
1485 adjustments like alignment promotion. This is both consistent with
1486 all the treatment above, where alignment and size are set on the
1487 type of the object and not on the object directly, and makes it
1488 possible to support all confirming representation clauses. */
1489 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1490 used_by_ref, false);
1495 /* Return a TYPE_DECL for "void" that we previously made. */
1496 gnu_decl = TYPE_NAME (void_type_node);
1499 case E_Enumeration_Type:
1500 /* A special case: for the types Character and Wide_Character in
1501 Standard, we do not list all the literals. So if the literals
1502 are not specified, make this an unsigned type. */
1503 if (No (First_Literal (gnat_entity)))
1505 gnu_type = make_unsigned_type (esize);
1506 TYPE_NAME (gnu_type) = gnu_entity_name;
1508 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1509 This is needed by the DWARF-2 back-end to distinguish between
1510 unsigned integer types and character types. */
1511 TYPE_STRING_FLAG (gnu_type) = 1;
1516 /* We have a list of enumeral constants in First_Literal. We make a
1517 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1518 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1519 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1520 value of the literal. But when we have a regular boolean type, we
1521 simplify this a little by using a BOOLEAN_TYPE. */
1522 bool is_boolean = Is_Boolean_Type (gnat_entity)
1523 && !Has_Non_Standard_Rep (gnat_entity);
1524 tree gnu_literal_list = NULL_TREE;
1525 Entity_Id gnat_literal;
1527 if (Is_Unsigned_Type (gnat_entity))
1528 gnu_type = make_unsigned_type (esize);
1530 gnu_type = make_signed_type (esize);
1532 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1534 for (gnat_literal = First_Literal (gnat_entity);
1535 Present (gnat_literal);
1536 gnat_literal = Next_Literal (gnat_literal))
1539 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1541 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1542 gnu_type, gnu_value, true, false, false,
1543 false, NULL, gnat_literal);
1544 /* Do not generate debug info for individual enumerators. */
1545 DECL_IGNORED_P (gnu_literal) = 1;
1546 save_gnu_tree (gnat_literal, gnu_literal, false);
1547 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1548 gnu_value, gnu_literal_list);
1552 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1554 /* Note that the bounds are updated at the end of this function
1555 to avoid an infinite recursion since they refer to the type. */
1559 case E_Signed_Integer_Type:
1560 case E_Ordinary_Fixed_Point_Type:
1561 case E_Decimal_Fixed_Point_Type:
1562 /* For integer types, just make a signed type the appropriate number
1564 gnu_type = make_signed_type (esize);
1567 case E_Modular_Integer_Type:
1569 /* For modular types, make the unsigned type of the proper number
1570 of bits and then set up the modulus, if required. */
1571 tree gnu_modulus, gnu_high = NULL_TREE;
1573 /* Packed array types are supposed to be subtypes only. */
1574 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1576 gnu_type = make_unsigned_type (esize);
1578 /* Get the modulus in this type. If it overflows, assume it is because
1579 it is equal to 2**Esize. Note that there is no overflow checking
1580 done on unsigned type, so we detect the overflow by looking for
1581 a modulus of zero, which is otherwise invalid. */
1582 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1584 if (!integer_zerop (gnu_modulus))
1586 TYPE_MODULAR_P (gnu_type) = 1;
1587 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1588 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1589 convert (gnu_type, integer_one_node));
1592 /* If the upper bound is not maximal, make an extra subtype. */
1594 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1596 tree gnu_subtype = make_unsigned_type (esize);
1597 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1598 TREE_TYPE (gnu_subtype) = gnu_type;
1599 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1600 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1601 gnu_type = gnu_subtype;
1606 case E_Signed_Integer_Subtype:
1607 case E_Enumeration_Subtype:
1608 case E_Modular_Integer_Subtype:
1609 case E_Ordinary_Fixed_Point_Subtype:
1610 case E_Decimal_Fixed_Point_Subtype:
1612 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1613 not want to call create_range_type since we would like each subtype
1614 node to be distinct. ??? Historically this was in preparation for
1615 when memory aliasing is implemented, but that's obsolete now given
1616 the call to relate_alias_sets below.
1618 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1619 this fact is used by the arithmetic conversion functions.
1621 We elaborate the Ancestor_Subtype if it is not in the current unit
1622 and one of our bounds is non-static. We do this to ensure consistent
1623 naming in the case where several subtypes share the same bounds, by
1624 elaborating the first such subtype first, thus using its name. */
1627 && Present (Ancestor_Subtype (gnat_entity))
1628 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1629 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1630 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1631 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1633 /* Set the precision to the Esize except for bit-packed arrays. */
1634 if (Is_Packed_Array_Type (gnat_entity)
1635 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1636 esize = UI_To_Int (RM_Size (gnat_entity));
1638 /* This should be an unsigned type if the base type is unsigned or
1639 if the lower bound is constant and non-negative or if the type
1641 if (Is_Unsigned_Type (Etype (gnat_entity))
1642 || Is_Unsigned_Type (gnat_entity)
1643 || Has_Biased_Representation (gnat_entity))
1644 gnu_type = make_unsigned_type (esize);
1646 gnu_type = make_signed_type (esize);
1647 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1649 SET_TYPE_RM_MIN_VALUE
1651 convert (TREE_TYPE (gnu_type),
1652 elaborate_expression (Type_Low_Bound (gnat_entity),
1653 gnat_entity, get_identifier ("L"),
1655 Needs_Debug_Info (gnat_entity))));
1657 SET_TYPE_RM_MAX_VALUE
1659 convert (TREE_TYPE (gnu_type),
1660 elaborate_expression (Type_High_Bound (gnat_entity),
1661 gnat_entity, get_identifier ("U"),
1663 Needs_Debug_Info (gnat_entity))));
1665 /* One of the above calls might have caused us to be elaborated,
1666 so don't blow up if so. */
1667 if (present_gnu_tree (gnat_entity))
1669 maybe_present = true;
1673 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1674 = Has_Biased_Representation (gnat_entity);
1676 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1677 TYPE_STUB_DECL (gnu_type)
1678 = create_type_stub_decl (gnu_entity_name, gnu_type);
1680 /* Inherit our alias set from what we're a subtype of. Subtypes
1681 are not different types and a pointer can designate any instance
1682 within a subtype hierarchy. */
1683 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1685 /* For a packed array, make the original array type a parallel type. */
1687 && Is_Packed_Array_Type (gnat_entity)
1688 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1689 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1691 (Original_Array_Type (gnat_entity)));
1695 /* We have to handle clauses that under-align the type specially. */
1696 if ((Present (Alignment_Clause (gnat_entity))
1697 || (Is_Packed_Array_Type (gnat_entity)
1699 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1700 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1702 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1703 if (align >= TYPE_ALIGN (gnu_type))
1707 /* If the type we are dealing with represents a bit-packed array,
1708 we need to have the bits left justified on big-endian targets
1709 and right justified on little-endian targets. We also need to
1710 ensure that when the value is read (e.g. for comparison of two
1711 such values), we only get the good bits, since the unused bits
1712 are uninitialized. Both goals are accomplished by wrapping up
1713 the modular type in an enclosing record type. */
1714 if (Is_Packed_Array_Type (gnat_entity)
1715 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1717 tree gnu_field_type, gnu_field;
1719 /* Set the RM size before wrapping up the original type. */
1720 SET_TYPE_RM_SIZE (gnu_type,
1721 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1722 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1724 /* Create a stripped-down declaration, mainly for debugging. */
1725 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1726 debug_info_p, gnat_entity);
1728 /* Now save it and build the enclosing record type. */
1729 gnu_field_type = gnu_type;
1731 gnu_type = make_node (RECORD_TYPE);
1732 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1733 TYPE_PACKED (gnu_type) = 1;
1734 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1735 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1736 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1738 /* Propagate the alignment of the modular type to the record type,
1739 unless there is an alignment clause that under-aligns the type.
1740 This means that bit-packed arrays are given "ceil" alignment for
1741 their size by default, which may seem counter-intuitive but makes
1742 it possible to overlay them on modular types easily. */
1743 TYPE_ALIGN (gnu_type)
1744 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1746 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1748 /* Don't declare the field as addressable since we won't be taking
1749 its address and this would prevent create_field_decl from making
1752 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1753 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1755 /* Do not emit debug info until after the parallel type is added. */
1756 finish_record_type (gnu_type, gnu_field, 2, false);
1757 compute_record_mode (gnu_type);
1758 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1762 /* Make the original array type a parallel type. */
1763 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1764 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1766 (Original_Array_Type (gnat_entity)));
1768 rest_of_record_type_compilation (gnu_type);
1772 /* If the type we are dealing with has got a smaller alignment than the
1773 natural one, we need to wrap it up in a record type and under-align
1774 the latter. We reuse the padding machinery for this purpose. */
1777 tree gnu_field_type, gnu_field;
1779 /* Set the RM size before wrapping up the type. */
1780 SET_TYPE_RM_SIZE (gnu_type,
1781 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1783 /* Create a stripped-down declaration, mainly for debugging. */
1784 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1785 debug_info_p, gnat_entity);
1787 /* Now save it and build the enclosing record type. */
1788 gnu_field_type = gnu_type;
1790 gnu_type = make_node (RECORD_TYPE);
1791 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1792 TYPE_PACKED (gnu_type) = 1;
1793 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1794 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1795 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1796 TYPE_ALIGN (gnu_type) = align;
1797 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1799 /* Don't declare the field as addressable since we won't be taking
1800 its address and this would prevent create_field_decl from making
1803 = create_field_decl (get_identifier ("F"), gnu_field_type,
1804 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1806 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1807 compute_record_mode (gnu_type);
1808 TYPE_PADDING_P (gnu_type) = 1;
1813 case E_Floating_Point_Type:
1814 /* If this is a VAX floating-point type, use an integer of the proper
1815 size. All the operations will be handled with ASM statements. */
1816 if (Vax_Float (gnat_entity))
1818 gnu_type = make_signed_type (esize);
1819 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1820 SET_TYPE_DIGITS_VALUE (gnu_type,
1821 UI_To_gnu (Digits_Value (gnat_entity),
1826 /* The type of the Low and High bounds can be our type if this is
1827 a type from Standard, so set them at the end of the function. */
1828 gnu_type = make_node (REAL_TYPE);
1829 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1830 layout_type (gnu_type);
1833 case E_Floating_Point_Subtype:
1834 if (Vax_Float (gnat_entity))
1836 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1842 && Present (Ancestor_Subtype (gnat_entity))
1843 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1844 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1845 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1846 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1849 gnu_type = make_node (REAL_TYPE);
1850 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1851 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1852 TYPE_GCC_MIN_VALUE (gnu_type)
1853 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1854 TYPE_GCC_MAX_VALUE (gnu_type)
1855 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1856 layout_type (gnu_type);
1858 SET_TYPE_RM_MIN_VALUE
1860 convert (TREE_TYPE (gnu_type),
1861 elaborate_expression (Type_Low_Bound (gnat_entity),
1862 gnat_entity, get_identifier ("L"),
1864 Needs_Debug_Info (gnat_entity))));
1866 SET_TYPE_RM_MAX_VALUE
1868 convert (TREE_TYPE (gnu_type),
1869 elaborate_expression (Type_High_Bound (gnat_entity),
1870 gnat_entity, get_identifier ("U"),
1872 Needs_Debug_Info (gnat_entity))));
1874 /* One of the above calls might have caused us to be elaborated,
1875 so don't blow up if so. */
1876 if (present_gnu_tree (gnat_entity))
1878 maybe_present = true;
1882 /* Inherit our alias set from what we're a subtype of, as for
1883 integer subtypes. */
1884 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1888 /* Array and String Types and Subtypes
1890 Unconstrained array types are represented by E_Array_Type and
1891 constrained array types are represented by E_Array_Subtype. There
1892 are no actual objects of an unconstrained array type; all we have
1893 are pointers to that type.
1895 The following fields are defined on array types and subtypes:
1897 Component_Type Component type of the array.
1898 Number_Dimensions Number of dimensions (an int).
1899 First_Index Type of first index. */
1904 Entity_Id gnat_index, gnat_name;
1905 const bool convention_fortran_p
1906 = (Convention (gnat_entity) == Convention_Fortran);
1907 const int ndim = Number_Dimensions (gnat_entity);
1908 tree gnu_template_fields = NULL_TREE;
1909 tree gnu_template_type = make_node (RECORD_TYPE);
1910 tree gnu_template_reference;
1911 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1912 tree gnu_fat_type = make_node (RECORD_TYPE);
1913 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1914 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1915 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
1918 TYPE_NAME (gnu_template_type)
1919 = create_concat_name (gnat_entity, "XUB");
1921 /* Make a node for the array. If we are not defining the array
1922 suppress expanding incomplete types. */
1923 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1927 defer_incomplete_level++;
1928 this_deferred = true;
1931 /* Build the fat pointer type. Use a "void *" object instead of
1932 a pointer to the array type since we don't have the array type
1933 yet (it will reference the fat pointer via the bounds). */
1935 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
1936 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
1938 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
1939 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
1940 finish_fat_pointer_type (gnu_fat_type, tem);
1942 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1943 is the fat pointer. This will be used to access the individual
1944 fields once we build them. */
1945 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1946 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1947 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1948 gnu_template_reference
1949 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1950 TREE_READONLY (gnu_template_reference) = 1;
1952 /* Now create the GCC type for each index and add the fields for that
1953 index to the template. */
1954 for (index = (convention_fortran_p ? ndim - 1 : 0),
1955 gnat_index = First_Index (gnat_entity);
1956 0 <= index && index < ndim;
1957 index += (convention_fortran_p ? - 1 : 1),
1958 gnat_index = Next_Index (gnat_index))
1960 char field_name[16];
1961 tree gnu_index_base_type
1962 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1963 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
1964 tree gnu_min, gnu_max, gnu_high;
1966 /* Make the FIELD_DECLs for the low and high bounds of this
1967 type and then make extractions of these fields from the
1969 sprintf (field_name, "LB%d", index);
1970 gnu_lb_field = create_field_decl (get_identifier (field_name),
1971 gnu_index_base_type,
1972 gnu_template_type, NULL_TREE,
1974 Sloc_to_locus (Sloc (gnat_entity),
1975 &DECL_SOURCE_LOCATION (gnu_lb_field));
1977 field_name[0] = 'U';
1978 gnu_hb_field = create_field_decl (get_identifier (field_name),
1979 gnu_index_base_type,
1980 gnu_template_type, NULL_TREE,
1982 Sloc_to_locus (Sloc (gnat_entity),
1983 &DECL_SOURCE_LOCATION (gnu_hb_field));
1985 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
1987 /* We can't use build_component_ref here since the template type
1988 isn't complete yet. */
1989 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
1990 gnu_template_reference, gnu_lb_field,
1992 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
1993 gnu_template_reference, gnu_hb_field,
1995 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
1997 gnu_min = convert (sizetype, gnu_orig_min);
1998 gnu_max = convert (sizetype, gnu_orig_max);
2000 /* Compute the size of this dimension. See the E_Array_Subtype
2001 case below for the rationale. */
2003 = build3 (COND_EXPR, sizetype,
2004 build2 (GE_EXPR, boolean_type_node,
2005 gnu_orig_max, gnu_orig_min),
2007 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2009 /* Make a range type with the new range in the Ada base type.
2010 Then make an index type with the size range in sizetype. */
2011 gnu_index_types[index]
2012 = create_index_type (gnu_min, gnu_high,
2013 create_range_type (gnu_index_base_type,
2018 /* Update the maximum size of the array in elements. */
2021 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2023 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2025 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2027 = size_binop (MAX_EXPR,
2028 size_binop (PLUS_EXPR, size_one_node,
2029 size_binop (MINUS_EXPR,
2033 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2034 && TREE_OVERFLOW (gnu_this_max))
2035 gnu_max_size = NULL_TREE;
2038 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2041 TYPE_NAME (gnu_index_types[index])
2042 = create_concat_name (gnat_entity, field_name);
2045 for (index = 0; index < ndim; index++)
2047 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2049 /* Install all the fields into the template. */
2050 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2052 TYPE_READONLY (gnu_template_type) = 1;
2054 /* Now make the array of arrays and update the pointer to the array
2055 in the fat pointer. Note that it is the first field. */
2056 tem = gnat_to_gnu_component_type (gnat_entity, definition,
2059 /* If Component_Size is not already specified, annotate it with the
2060 size of the component. */
2061 if (Unknown_Component_Size (gnat_entity))
2062 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2064 /* Compute the maximum size of the array in units and bits. */
2067 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2068 TYPE_SIZE_UNIT (tem));
2069 gnu_max_size = size_binop (MULT_EXPR,
2070 convert (bitsizetype, gnu_max_size),
2074 gnu_max_size_unit = NULL_TREE;
2076 /* Now build the array type. */
2077 for (index = ndim - 1; index >= 0; index--)
2079 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2080 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2081 if (array_type_has_nonaliased_component (tem, gnat_entity))
2082 TYPE_NONALIASED_COMPONENT (tem) = 1;
2085 /* If an alignment is specified, use it if valid. But ignore it
2086 for the original type of packed array types. If the alignment
2087 was requested with an explicit alignment clause, state so. */
2088 if (No (Packed_Array_Type (gnat_entity))
2089 && Known_Alignment (gnat_entity))
2092 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2094 if (Present (Alignment_Clause (gnat_entity)))
2095 TYPE_USER_ALIGN (tem) = 1;
2098 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2099 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2101 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2102 corresponding fat pointer. */
2103 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2104 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2105 SET_TYPE_MODE (gnu_type, BLKmode);
2106 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2107 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2109 /* If the maximum size doesn't overflow, use it. */
2111 && TREE_CODE (gnu_max_size) == INTEGER_CST
2112 && !TREE_OVERFLOW (gnu_max_size)
2113 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2114 && !TREE_OVERFLOW (gnu_max_size_unit))
2116 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2118 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2119 TYPE_SIZE_UNIT (tem));
2122 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2123 tem, NULL, !Comes_From_Source (gnat_entity),
2124 debug_info_p, gnat_entity);
2126 /* Give the fat pointer type a name. If this is a packed type, tell
2127 the debugger how to interpret the underlying bits. */
2128 if (Present (Packed_Array_Type (gnat_entity)))
2129 gnat_name = Packed_Array_Type (gnat_entity);
2131 gnat_name = gnat_entity;
2132 create_type_decl (create_concat_name (gnat_name, "XUP"),
2133 gnu_fat_type, NULL, true,
2134 debug_info_p, gnat_entity);
2136 /* Create the type to be used as what a thin pointer designates:
2137 a record type for the object and its template with the fields
2138 shifted to have the template at a negative offset. */
2139 tem = build_unc_object_type (gnu_template_type, tem,
2140 create_concat_name (gnat_name, "XUT"),
2142 shift_unc_components_for_thin_pointers (tem);
2144 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2145 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2149 case E_String_Subtype:
2150 case E_Array_Subtype:
2152 /* This is the actual data type for array variables. Multidimensional
2153 arrays are implemented as arrays of arrays. Note that arrays which
2154 have sparse enumeration subtypes as index components create sparse
2155 arrays, which is obviously space inefficient but so much easier to
2158 Also note that the subtype never refers to the unconstrained array
2159 type, which is somewhat at variance with Ada semantics.
2161 First check to see if this is simply a renaming of the array type.
2162 If so, the result is the array type. */
2164 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2165 if (!Is_Constrained (gnat_entity))
2169 Entity_Id gnat_index, gnat_base_index;
2170 const bool convention_fortran_p
2171 = (Convention (gnat_entity) == Convention_Fortran);
2172 const int ndim = Number_Dimensions (gnat_entity);
2173 tree gnu_base_type = gnu_type;
2174 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2175 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2176 bool need_index_type_struct = false;
2179 /* First create the GCC type for each index and find out whether
2180 special types are needed for debugging information. */
2181 for (index = (convention_fortran_p ? ndim - 1 : 0),
2182 gnat_index = First_Index (gnat_entity),
2184 = First_Index (Implementation_Base_Type (gnat_entity));
2185 0 <= index && index < ndim;
2186 index += (convention_fortran_p ? - 1 : 1),
2187 gnat_index = Next_Index (gnat_index),
2188 gnat_base_index = Next_Index (gnat_base_index))
2190 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2191 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2192 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2193 tree gnu_min = convert (sizetype, gnu_orig_min);
2194 tree gnu_max = convert (sizetype, gnu_orig_max);
2195 tree gnu_base_index_type
2196 = get_unpadded_type (Etype (gnat_base_index));
2197 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2198 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2201 /* See if the base array type is already flat. If it is, we
2202 are probably compiling an ACATS test but it will cause the
2203 code below to malfunction if we don't handle it specially. */
2204 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2205 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2206 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2208 gnu_min = size_one_node;
2209 gnu_max = size_zero_node;
2213 /* Similarly, if one of the values overflows in sizetype and the
2214 range is null, use 1..0 for the sizetype bounds. */
2215 else if (TREE_CODE (gnu_min) == INTEGER_CST
2216 && TREE_CODE (gnu_max) == INTEGER_CST
2217 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2218 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2220 gnu_min = size_one_node;
2221 gnu_max = size_zero_node;
2225 /* If the minimum and maximum values both overflow in sizetype,
2226 but the difference in the original type does not overflow in
2227 sizetype, ignore the overflow indication. */
2228 else if (TREE_CODE (gnu_min) == INTEGER_CST
2229 && TREE_CODE (gnu_max) == INTEGER_CST
2230 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2233 fold_build2 (MINUS_EXPR, gnu_index_type,
2237 TREE_OVERFLOW (gnu_min) = 0;
2238 TREE_OVERFLOW (gnu_max) = 0;
2242 /* Compute the size of this dimension in the general case. We
2243 need to provide GCC with an upper bound to use but have to
2244 deal with the "superflat" case. There are three ways to do
2245 this. If we can prove that the array can never be superflat,
2246 we can just use the high bound of the index type. */
2247 else if ((Nkind (gnat_index) == N_Range
2248 && cannot_be_superflat_p (gnat_index))
2249 /* Packed Array Types are never superflat. */
2250 || Is_Packed_Array_Type (gnat_entity))
2253 /* Otherwise, if the high bound is constant but the low bound is
2254 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2255 lower bound. Note that the comparison must be done in the
2256 original type to avoid any overflow during the conversion. */
2257 else if (TREE_CODE (gnu_max) == INTEGER_CST
2258 && TREE_CODE (gnu_min) != INTEGER_CST)
2262 = build_cond_expr (sizetype,
2263 build_binary_op (GE_EXPR,
2268 size_binop (PLUS_EXPR, gnu_max,
2272 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2273 in all the other cases. Note that, here as well as above,
2274 the condition used in the comparison must be equivalent to
2275 the condition (length != 0). This is relied upon in order
2276 to optimize array comparisons in compare_arrays. */
2279 = build_cond_expr (sizetype,
2280 build_binary_op (GE_EXPR,
2285 size_binop (MINUS_EXPR, gnu_min,
2288 /* Reuse the index type for the range type. Then make an index
2289 type with the size range in sizetype. */
2290 gnu_index_types[index]
2291 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2294 /* Update the maximum size of the array in elements. Here we
2295 see if any constraint on the index type of the base type
2296 can be used in the case of self-referential bound on the
2297 index type of the subtype. We look for a non-"infinite"
2298 and non-self-referential bound from any type involved and
2299 handle each bound separately. */
2302 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2303 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2304 tree gnu_base_index_base_type
2305 = get_base_type (gnu_base_index_type);
2306 tree gnu_base_base_min
2307 = convert (sizetype,
2308 TYPE_MIN_VALUE (gnu_base_index_base_type));
2309 tree gnu_base_base_max
2310 = convert (sizetype,
2311 TYPE_MAX_VALUE (gnu_base_index_base_type));
2313 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2314 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2315 && !TREE_OVERFLOW (gnu_base_min)))
2316 gnu_base_min = gnu_min;
2318 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2319 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2320 && !TREE_OVERFLOW (gnu_base_max)))
2321 gnu_base_max = gnu_max;
2323 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2324 && TREE_OVERFLOW (gnu_base_min))
2325 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2326 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2327 && TREE_OVERFLOW (gnu_base_max))
2328 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2329 gnu_max_size = NULL_TREE;
2333 = size_binop (MAX_EXPR,
2334 size_binop (PLUS_EXPR, size_one_node,
2335 size_binop (MINUS_EXPR,
2340 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2341 && TREE_OVERFLOW (gnu_this_max))
2342 gnu_max_size = NULL_TREE;
2345 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2349 /* We need special types for debugging information to point to
2350 the index types if they have variable bounds, are not integer
2351 types, are biased or are wider than sizetype. */
2352 if (!integer_onep (gnu_orig_min)
2353 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2354 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2355 || (TREE_TYPE (gnu_index_type)
2356 && TREE_CODE (TREE_TYPE (gnu_index_type))
2358 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2359 || compare_tree_int (rm_size (gnu_index_type),
2360 TYPE_PRECISION (sizetype)) > 0)
2361 need_index_type_struct = true;
2364 /* Then flatten: create the array of arrays. For an array type
2365 used to implement a packed array, get the component type from
2366 the original array type since the representation clauses that
2367 can affect it are on the latter. */
2368 if (Is_Packed_Array_Type (gnat_entity)
2369 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2371 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2372 for (index = ndim - 1; index >= 0; index--)
2373 gnu_type = TREE_TYPE (gnu_type);
2375 /* One of the above calls might have caused us to be elaborated,
2376 so don't blow up if so. */
2377 if (present_gnu_tree (gnat_entity))
2379 maybe_present = true;
2385 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2388 /* One of the above calls might have caused us to be elaborated,
2389 so don't blow up if so. */
2390 if (present_gnu_tree (gnat_entity))
2392 maybe_present = true;
2397 /* Compute the maximum size of the array in units and bits. */
2400 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2401 TYPE_SIZE_UNIT (gnu_type));
2402 gnu_max_size = size_binop (MULT_EXPR,
2403 convert (bitsizetype, gnu_max_size),
2404 TYPE_SIZE (gnu_type));
2407 gnu_max_size_unit = NULL_TREE;
2409 /* Now build the array type. */
2410 for (index = ndim - 1; index >= 0; index --)
2412 gnu_type = build_nonshared_array_type (gnu_type,
2413 gnu_index_types[index]);
2414 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2415 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2416 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2419 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2420 TYPE_STUB_DECL (gnu_type)
2421 = create_type_stub_decl (gnu_entity_name, gnu_type);
2423 /* If we are at file level and this is a multi-dimensional array,
2424 we need to make a variable corresponding to the stride of the
2425 inner dimensions. */
2426 if (global_bindings_p () && ndim > 1)
2428 tree gnu_st_name = get_identifier ("ST");
2431 for (gnu_arr_type = TREE_TYPE (gnu_type);
2432 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2433 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2434 gnu_st_name = concat_name (gnu_st_name, "ST"))
2436 tree eltype = TREE_TYPE (gnu_arr_type);
2438 TYPE_SIZE (gnu_arr_type)
2439 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2440 gnat_entity, gnu_st_name,
2443 /* ??? For now, store the size as a multiple of the
2444 alignment of the element type in bytes so that we
2445 can see the alignment from the tree. */
2446 TYPE_SIZE_UNIT (gnu_arr_type)
2447 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2449 concat_name (gnu_st_name, "A_U"),
2451 TYPE_ALIGN (eltype));
2453 /* ??? create_type_decl is not invoked on the inner types so
2454 the MULT_EXPR node built above will never be marked. */
2455 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2459 /* If we need to write out a record type giving the names of the
2460 bounds for debugging purposes, do it now and make the record
2461 type a parallel type. This is not needed for a packed array
2462 since the bounds are conveyed by the original array type. */
2463 if (need_index_type_struct
2465 && !Is_Packed_Array_Type (gnat_entity))
2467 tree gnu_bound_rec = make_node (RECORD_TYPE);
2468 tree gnu_field_list = NULL_TREE;
2471 TYPE_NAME (gnu_bound_rec)
2472 = create_concat_name (gnat_entity, "XA");
2474 for (index = ndim - 1; index >= 0; index--)
2476 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2477 tree gnu_index_name = TYPE_NAME (gnu_index);
2479 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2480 gnu_index_name = DECL_NAME (gnu_index_name);
2482 /* Make sure to reference the types themselves, and not just
2483 their names, as the debugger may fall back on them. */
2484 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2485 gnu_bound_rec, NULL_TREE,
2487 DECL_CHAIN (gnu_field) = gnu_field_list;
2488 gnu_field_list = gnu_field;
2491 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2492 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2495 /* Otherwise, for a packed array, make the original array type a
2497 else if (debug_info_p
2498 && Is_Packed_Array_Type (gnat_entity)
2499 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2500 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2502 (Original_Array_Type (gnat_entity)));
2504 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2505 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2506 = (Is_Packed_Array_Type (gnat_entity)
2507 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2509 /* If the size is self-referential and the maximum size doesn't
2510 overflow, use it. */
2511 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2513 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2514 && TREE_OVERFLOW (gnu_max_size))
2515 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2516 && TREE_OVERFLOW (gnu_max_size_unit)))
2518 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2519 TYPE_SIZE (gnu_type));
2520 TYPE_SIZE_UNIT (gnu_type)
2521 = size_binop (MIN_EXPR, gnu_max_size_unit,
2522 TYPE_SIZE_UNIT (gnu_type));
2525 /* Set our alias set to that of our base type. This gives all
2526 array subtypes the same alias set. */
2527 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2529 /* If this is a packed type, make this type the same as the packed
2530 array type, but do some adjusting in the type first. */
2531 if (Present (Packed_Array_Type (gnat_entity)))
2533 Entity_Id gnat_index;
2536 /* First finish the type we had been making so that we output
2537 debugging information for it. */
2538 if (Treat_As_Volatile (gnat_entity))
2540 = build_qualified_type (gnu_type,
2541 TYPE_QUALS (gnu_type)
2542 | TYPE_QUAL_VOLATILE);
2544 /* Make it artificial only if the base type was artificial too.
2545 That's sort of "morally" true and will make it possible for
2546 the debugger to look it up by name in DWARF, which is needed
2547 in order to decode the packed array type. */
2549 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2550 !Comes_From_Source (Etype (gnat_entity))
2551 && !Comes_From_Source (gnat_entity),
2552 debug_info_p, gnat_entity);
2554 /* Save it as our equivalent in case the call below elaborates
2556 save_gnu_tree (gnat_entity, gnu_decl, false);
2558 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2560 this_made_decl = true;
2561 gnu_type = TREE_TYPE (gnu_decl);
2562 save_gnu_tree (gnat_entity, NULL_TREE, false);
2564 gnu_inner = gnu_type;
2565 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2566 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2567 || TYPE_PADDING_P (gnu_inner)))
2568 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2570 /* We need to attach the index type to the type we just made so
2571 that the actual bounds can later be put into a template. */
2572 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2573 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2574 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2575 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2577 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2579 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2580 TYPE_MODULUS for modular types so we make an extra
2581 subtype if necessary. */
2582 if (TYPE_MODULAR_P (gnu_inner))
2585 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2586 TREE_TYPE (gnu_subtype) = gnu_inner;
2587 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2588 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2589 TYPE_MIN_VALUE (gnu_inner));
2590 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2591 TYPE_MAX_VALUE (gnu_inner));
2592 gnu_inner = gnu_subtype;
2595 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2597 #ifdef ENABLE_CHECKING
2598 /* Check for other cases of overloading. */
2599 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2603 for (gnat_index = First_Index (gnat_entity);
2604 Present (gnat_index);
2605 gnat_index = Next_Index (gnat_index))
2606 SET_TYPE_ACTUAL_BOUNDS
2608 tree_cons (NULL_TREE,
2609 get_unpadded_type (Etype (gnat_index)),
2610 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2612 if (Convention (gnat_entity) != Convention_Fortran)
2613 SET_TYPE_ACTUAL_BOUNDS
2614 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2616 if (TREE_CODE (gnu_type) == RECORD_TYPE
2617 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2618 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2623 /* Abort if packed array with no Packed_Array_Type field set. */
2624 gcc_assert (!Is_Packed (gnat_entity));
2628 case E_String_Literal_Subtype:
2629 /* Create the type for a string literal. */
2631 Entity_Id gnat_full_type
2632 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2633 && Present (Full_View (Etype (gnat_entity)))
2634 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2635 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2636 tree gnu_string_array_type
2637 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2638 tree gnu_string_index_type
2639 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2640 (TYPE_DOMAIN (gnu_string_array_type))));
2641 tree gnu_lower_bound
2642 = convert (gnu_string_index_type,
2643 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2644 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2645 tree gnu_length = ssize_int (length - 1);
2646 tree gnu_upper_bound
2647 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2649 convert (gnu_string_index_type, gnu_length));
2651 = create_index_type (convert (sizetype, gnu_lower_bound),
2652 convert (sizetype, gnu_upper_bound),
2653 create_range_type (gnu_string_index_type,
2659 = build_nonshared_array_type (gnat_to_gnu_type
2660 (Component_Type (gnat_entity)),
2662 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2663 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2664 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2668 /* Record Types and Subtypes
2670 The following fields are defined on record types:
2672 Has_Discriminants True if the record has discriminants
2673 First_Discriminant Points to head of list of discriminants
2674 First_Entity Points to head of list of fields
2675 Is_Tagged_Type True if the record is tagged
2677 Implementation of Ada records and discriminated records:
2679 A record type definition is transformed into the equivalent of a C
2680 struct definition. The fields that are the discriminants which are
2681 found in the Full_Type_Declaration node and the elements of the
2682 Component_List found in the Record_Type_Definition node. The
2683 Component_List can be a recursive structure since each Variant of
2684 the Variant_Part of the Component_List has a Component_List.
2686 Processing of a record type definition comprises starting the list of
2687 field declarations here from the discriminants and the calling the
2688 function components_to_record to add the rest of the fields from the
2689 component list and return the gnu type node. The function
2690 components_to_record will call itself recursively as it traverses
2694 if (Has_Complex_Representation (gnat_entity))
2697 = build_complex_type
2699 (Etype (Defining_Entity
2700 (First (Component_Items
2703 (Declaration_Node (gnat_entity)))))))));
2709 Node_Id full_definition = Declaration_Node (gnat_entity);
2710 Node_Id record_definition = Type_Definition (full_definition);
2711 Entity_Id gnat_field;
2712 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2713 /* Set PACKED in keeping with gnat_to_gnu_field. */
2715 = Is_Packed (gnat_entity)
2717 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2719 : (Known_Alignment (gnat_entity)
2720 || (Strict_Alignment (gnat_entity)
2721 && Known_Static_Esize (gnat_entity)))
2724 bool has_discr = Has_Discriminants (gnat_entity);
2725 bool has_rep = Has_Specified_Layout (gnat_entity);
2726 bool all_rep = has_rep;
2728 = (Is_Tagged_Type (gnat_entity)
2729 && Nkind (record_definition) == N_Derived_Type_Definition);
2730 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2732 /* See if all fields have a rep clause. Stop when we find one
2735 for (gnat_field = First_Entity (gnat_entity);
2736 Present (gnat_field);
2737 gnat_field = Next_Entity (gnat_field))
2738 if ((Ekind (gnat_field) == E_Component
2739 || Ekind (gnat_field) == E_Discriminant)
2740 && No (Component_Clause (gnat_field)))
2746 /* If this is a record extension, go a level further to find the
2747 record definition. Also, verify we have a Parent_Subtype. */
2750 if (!type_annotate_only
2751 || Present (Record_Extension_Part (record_definition)))
2752 record_definition = Record_Extension_Part (record_definition);
2754 gcc_assert (type_annotate_only
2755 || Present (Parent_Subtype (gnat_entity)));
2758 /* Make a node for the record. If we are not defining the record,
2759 suppress expanding incomplete types. */
2760 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2761 TYPE_NAME (gnu_type) = gnu_entity_name;
2762 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2766 defer_incomplete_level++;
2767 this_deferred = true;
2770 /* If both a size and rep clause was specified, put the size in
2771 the record type now so that it can get the proper mode. */
2772 if (has_rep && Known_Esize (gnat_entity))
2773 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2775 /* Always set the alignment here so that it can be used to
2776 set the mode, if it is making the alignment stricter. If
2777 it is invalid, it will be checked again below. If this is to
2778 be Atomic, choose a default alignment of a word unless we know
2779 the size and it's smaller. */
2780 if (Known_Alignment (gnat_entity))
2781 TYPE_ALIGN (gnu_type)
2782 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2783 else if (Is_Atomic (gnat_entity))
2784 TYPE_ALIGN (gnu_type)
2785 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2786 /* If a type needs strict alignment, the minimum size will be the
2787 type size instead of the RM size (see validate_size). Cap the
2788 alignment, lest it causes this type size to become too large. */
2789 else if (Strict_Alignment (gnat_entity)
2790 && Known_Static_Esize (gnat_entity))
2792 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2793 unsigned int raw_align = raw_size & -raw_size;
2794 if (raw_align < BIGGEST_ALIGNMENT)
2795 TYPE_ALIGN (gnu_type) = raw_align;
2798 TYPE_ALIGN (gnu_type) = 0;
2800 /* If we have a Parent_Subtype, make a field for the parent. If
2801 this record has rep clauses, force the position to zero. */
2802 if (Present (Parent_Subtype (gnat_entity)))
2804 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2807 /* A major complexity here is that the parent subtype will
2808 reference our discriminants in its Discriminant_Constraint
2809 list. But those must reference the parent component of this
2810 record which is of the parent subtype we have not built yet!
2811 To break the circle we first build a dummy COMPONENT_REF which
2812 represents the "get to the parent" operation and initialize
2813 each of those discriminants to a COMPONENT_REF of the above
2814 dummy parent referencing the corresponding discriminant of the
2815 base type of the parent subtype. */
2816 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2817 build0 (PLACEHOLDER_EXPR, gnu_type),
2818 build_decl (input_location,
2819 FIELD_DECL, NULL_TREE,
2824 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2825 Present (gnat_field);
2826 gnat_field = Next_Stored_Discriminant (gnat_field))
2827 if (Present (Corresponding_Discriminant (gnat_field)))
2830 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2834 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2835 gnu_get_parent, gnu_field, NULL_TREE),
2839 /* Then we build the parent subtype. If it has discriminants but
2840 the type itself has unknown discriminants, this means that it
2841 doesn't contain information about how the discriminants are
2842 derived from those of the ancestor type, so it cannot be used
2843 directly. Instead it is built by cloning the parent subtype
2844 of the underlying record view of the type, for which the above
2845 derivation of discriminants has been made explicit. */
2846 if (Has_Discriminants (gnat_parent)
2847 && Has_Unknown_Discriminants (gnat_entity))
2849 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2851 /* If we are defining the type, the underlying record
2852 view must already have been elaborated at this point.
2853 Otherwise do it now as its parent subtype cannot be
2854 technically elaborated on its own. */
2856 gcc_assert (present_gnu_tree (gnat_uview));
2858 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2860 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2862 /* Substitute the "get to the parent" of the type for that
2863 of its underlying record view in the cloned type. */
2864 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2865 Present (gnat_field);
2866 gnat_field = Next_Stored_Discriminant (gnat_field))
2867 if (Present (Corresponding_Discriminant (gnat_field)))
2869 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2871 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2872 gnu_get_parent, gnu_field, NULL_TREE);
2874 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2878 gnu_parent = gnat_to_gnu_type (gnat_parent);
2880 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2881 initially built. The discriminants must reference the fields
2882 of the parent subtype and not those of its base type for the
2883 placeholder machinery to properly work. */
2886 /* The actual parent subtype is the full view. */
2887 if (IN (Ekind (gnat_parent), Private_Kind))
2889 if (Present (Full_View (gnat_parent)))
2890 gnat_parent = Full_View (gnat_parent);
2892 gnat_parent = Underlying_Full_View (gnat_parent);
2895 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2896 Present (gnat_field);
2897 gnat_field = Next_Stored_Discriminant (gnat_field))
2898 if (Present (Corresponding_Discriminant (gnat_field)))
2900 Entity_Id field = Empty;
2901 for (field = First_Stored_Discriminant (gnat_parent);
2903 field = Next_Stored_Discriminant (field))
2904 if (same_discriminant_p (gnat_field, field))
2906 gcc_assert (Present (field));
2907 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2908 = gnat_to_gnu_field_decl (field);
2912 /* The "get to the parent" COMPONENT_REF must be given its
2914 TREE_TYPE (gnu_get_parent) = gnu_parent;
2916 /* ...and reference the _Parent field of this record. */
2918 = create_field_decl (parent_name_id,
2919 gnu_parent, gnu_type,
2921 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2923 ? bitsize_zero_node : NULL_TREE,
2925 DECL_INTERNAL_P (gnu_field) = 1;
2926 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2927 TYPE_FIELDS (gnu_type) = gnu_field;
2930 /* Make the fields for the discriminants and put them into the record
2931 unless it's an Unchecked_Union. */
2933 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2934 Present (gnat_field);
2935 gnat_field = Next_Stored_Discriminant (gnat_field))
2937 /* If this is a record extension and this discriminant is the
2938 renaming of another discriminant, we've handled it above. */
2939 if (Present (Parent_Subtype (gnat_entity))
2940 && Present (Corresponding_Discriminant (gnat_field)))
2944 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
2947 /* Make an expression using a PLACEHOLDER_EXPR from the
2948 FIELD_DECL node just created and link that with the
2949 corresponding GNAT defining identifier. */
2950 save_gnu_tree (gnat_field,
2951 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2952 build0 (PLACEHOLDER_EXPR, gnu_type),
2953 gnu_field, NULL_TREE),
2956 if (!is_unchecked_union)
2958 DECL_CHAIN (gnu_field) = gnu_field_list;
2959 gnu_field_list = gnu_field;
2963 /* Add the fields into the record type and finish it up. */
2964 components_to_record (gnu_type, Component_List (record_definition),
2965 gnu_field_list, packed, definition, false,
2966 all_rep, is_unchecked_union, debug_info_p,
2967 false, OK_To_Reorder_Components (gnat_entity),
2970 /* If it is passed by reference, force BLKmode to ensure that objects
2971 of this type will always be put in memory. */
2972 if (Is_By_Reference_Type (gnat_entity))
2973 SET_TYPE_MODE (gnu_type, BLKmode);
2975 /* We used to remove the associations of the discriminants and _Parent
2976 for validity checking but we may need them if there's a Freeze_Node
2977 for a subtype used in this record. */
2978 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2980 /* Fill in locations of fields. */
2981 annotate_rep (gnat_entity, gnu_type);
2983 /* If there are any entities in the chain corresponding to components
2984 that we did not elaborate, ensure we elaborate their types if they
2986 for (gnat_temp = First_Entity (gnat_entity);
2987 Present (gnat_temp);
2988 gnat_temp = Next_Entity (gnat_temp))
2989 if ((Ekind (gnat_temp) == E_Component
2990 || Ekind (gnat_temp) == E_Discriminant)
2991 && Is_Itype (Etype (gnat_temp))
2992 && !present_gnu_tree (gnat_temp))
2993 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2995 /* If this is a record type associated with an exception definition,
2996 equate its fields to those of the standard exception type. This
2997 will make it possible to convert between them. */
2998 if (gnu_entity_name == exception_data_name_id)
3001 for (gnu_field = TYPE_FIELDS (gnu_type),
3002 gnu_std_field = TYPE_FIELDS (except_type_node);
3004 gnu_field = DECL_CHAIN (gnu_field),
3005 gnu_std_field = DECL_CHAIN (gnu_std_field))
3006 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3007 gcc_assert (!gnu_std_field);
3012 case E_Class_Wide_Subtype:
3013 /* If an equivalent type is present, that is what we should use.
3014 Otherwise, fall through to handle this like a record subtype
3015 since it may have constraints. */
3016 if (gnat_equiv_type != gnat_entity)
3018 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3019 maybe_present = true;
3023 /* ... fall through ... */
3025 case E_Record_Subtype:
3026 /* If Cloned_Subtype is Present it means this record subtype has
3027 identical layout to that type or subtype and we should use
3028 that GCC type for this one. The front end guarantees that
3029 the component list is shared. */
3030 if (Present (Cloned_Subtype (gnat_entity)))
3032 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3034 maybe_present = true;
3038 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3039 changing the type, make a new type with each field having the type of
3040 the field in the new subtype but the position computed by transforming
3041 every discriminant reference according to the constraints. We don't
3042 see any difference between private and non-private type here since
3043 derivations from types should have been deferred until the completion
3044 of the private type. */
3047 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3052 defer_incomplete_level++;
3053 this_deferred = true;
3056 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3058 if (present_gnu_tree (gnat_entity))
3060 maybe_present = true;
3064 /* If this is a record subtype associated with a dispatch table,
3065 strip the suffix. This is necessary to make sure 2 different
3066 subtypes associated with the imported and exported views of a
3067 dispatch table are properly merged in LTO mode. */
3068 if (Is_Dispatch_Table_Entity (gnat_entity))
3071 Get_Encoded_Name (gnat_entity);
3072 p = strchr (Name_Buffer, '_');
3074 strcpy (p+2, "dtS");
3075 gnu_entity_name = get_identifier (Name_Buffer);
3078 /* When the subtype has discriminants and these discriminants affect
3079 the initial shape it has inherited, factor them in. But for an
3080 Unchecked_Union (it must be an Itype), just return the type.
3081 We can't just test Is_Constrained because private subtypes without
3082 discriminants of types with discriminants with default expressions
3083 are Is_Constrained but aren't constrained! */
3084 if (IN (Ekind (gnat_base_type), Record_Kind)
3085 && !Is_Unchecked_Union (gnat_base_type)
3086 && !Is_For_Access_Subtype (gnat_entity)
3087 && Is_Constrained (gnat_entity)
3088 && Has_Discriminants (gnat_entity)
3089 && Present (Discriminant_Constraint (gnat_entity))
3090 && Stored_Constraint (gnat_entity) != No_Elist)
3092 VEC(subst_pair,heap) *gnu_subst_list
3093 = build_subst_list (gnat_entity, gnat_base_type, definition);
3094 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3095 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3096 bool selected_variant = false;
3097 Entity_Id gnat_field;
3098 VEC(variant_desc,heap) *gnu_variant_list;
3100 gnu_type = make_node (RECORD_TYPE);
3101 TYPE_NAME (gnu_type) = gnu_entity_name;
3103 /* Set the size, alignment and alias set of the new type to
3104 match that of the old one, doing required substitutions. */
3105 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3108 if (TYPE_IS_PADDING_P (gnu_base_type))
3109 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3111 gnu_unpad_base_type = gnu_base_type;
3113 /* Look for a REP part in the base type. */
3114 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3116 /* Look for a variant part in the base type. */
3117 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3119 /* If there is a variant part, we must compute whether the
3120 constraints statically select a particular variant. If
3121 so, we simply drop the qualified union and flatten the
3122 list of fields. Otherwise we'll build a new qualified
3123 union for the variants that are still relevant. */
3124 if (gnu_variant_part)
3130 = build_variant_list (TREE_TYPE (gnu_variant_part),
3131 gnu_subst_list, NULL);
3133 /* If all the qualifiers are unconditionally true, the
3134 innermost variant is statically selected. */
3135 selected_variant = true;
3136 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3138 if (!integer_onep (v->qual))
3140 selected_variant = false;
3144 /* Otherwise, create the new variants. */
3145 if (!selected_variant)
3146 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3149 tree old_variant = v->type;
3150 tree new_variant = make_node (RECORD_TYPE);
3151 TYPE_NAME (new_variant)
3152 = DECL_NAME (TYPE_NAME (old_variant));
3153 copy_and_substitute_in_size (new_variant, old_variant,
3155 v->record = new_variant;
3160 gnu_variant_list = NULL;
3161 selected_variant = false;
3165 = build_position_list (gnu_unpad_base_type,
3166 gnu_variant_list && !selected_variant,
3167 size_zero_node, bitsize_zero_node,
3168 BIGGEST_ALIGNMENT, NULL_TREE);
3170 for (gnat_field = First_Entity (gnat_entity);
3171 Present (gnat_field);
3172 gnat_field = Next_Entity (gnat_field))
3173 if ((Ekind (gnat_field) == E_Component
3174 || Ekind (gnat_field) == E_Discriminant)
3175 && !(Present (Corresponding_Discriminant (gnat_field))
3176 && Is_Tagged_Type (gnat_base_type))
3177 && Underlying_Type (Scope (Original_Record_Component
3181 Name_Id gnat_name = Chars (gnat_field);
3182 Entity_Id gnat_old_field
3183 = Original_Record_Component (gnat_field);
3185 = gnat_to_gnu_field_decl (gnat_old_field);
3186 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3187 tree gnu_field, gnu_field_type, gnu_size;
3188 tree gnu_cont_type, gnu_last = NULL_TREE;
3190 /* If the type is the same, retrieve the GCC type from the
3191 old field to take into account possible adjustments. */
3192 if (Etype (gnat_field) == Etype (gnat_old_field))
3193 gnu_field_type = TREE_TYPE (gnu_old_field);
3195 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3197 /* If there was a component clause, the field types must be
3198 the same for the type and subtype, so copy the data from
3199 the old field to avoid recomputation here. Also if the
3200 field is justified modular and the optimization in
3201 gnat_to_gnu_field was applied. */
3202 if (Present (Component_Clause (gnat_old_field))
3203 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3204 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3205 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3206 == TREE_TYPE (gnu_old_field)))
3208 gnu_size = DECL_SIZE (gnu_old_field);
3209 gnu_field_type = TREE_TYPE (gnu_old_field);
3212 /* If the old field was packed and of constant size, we
3213 have to get the old size here, as it might differ from
3214 what the Etype conveys and the latter might overlap
3215 onto the following field. Try to arrange the type for
3216 possible better packing along the way. */
3217 else if (DECL_PACKED (gnu_old_field)
3218 && TREE_CODE (DECL_SIZE (gnu_old_field))
3221 gnu_size = DECL_SIZE (gnu_old_field);
3222 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3223 && !TYPE_FAT_POINTER_P (gnu_field_type)
3224 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3226 = make_packable_type (gnu_field_type, true);
3230 gnu_size = TYPE_SIZE (gnu_field_type);
3232 /* If the context of the old field is the base type or its
3233 REP part (if any), put the field directly in the new
3234 type; otherwise look up the context in the variant list
3235 and put the field either in the new type if there is a
3236 selected variant or in one of the new variants. */
3237 if (gnu_context == gnu_unpad_base_type
3239 && gnu_context == TREE_TYPE (gnu_rep_part)))
3240 gnu_cont_type = gnu_type;
3247 FOR_EACH_VEC_ELT_REVERSE (variant_desc,
3248 gnu_variant_list, ix, v)
3249 if (v->type == gnu_context)
3256 if (selected_variant)
3257 gnu_cont_type = gnu_type;
3259 gnu_cont_type = v->record;
3262 /* The front-end may pass us "ghost" components if
3263 it fails to recognize that a constrained subtype
3264 is statically constrained. Discard them. */
3268 /* Now create the new field modeled on the old one. */
3270 = create_field_decl_from (gnu_old_field, gnu_field_type,
3271 gnu_cont_type, gnu_size,
3272 gnu_pos_list, gnu_subst_list);
3274 /* Put it in one of the new variants directly. */
3275 if (gnu_cont_type != gnu_type)
3277 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3278 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3281 /* To match the layout crafted in components_to_record,
3282 if this is the _Tag or _Parent field, put it before
3283 any other fields. */
3284 else if (gnat_name == Name_uTag
3285 || gnat_name == Name_uParent)
3286 gnu_field_list = chainon (gnu_field_list, gnu_field);
3288 /* Similarly, if this is the _Controller field, put
3289 it before the other fields except for the _Tag or
3291 else if (gnat_name == Name_uController && gnu_last)
3293 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
3294 TREE_CHAIN (gnu_last) = gnu_field;
3297 /* Otherwise, if this is a regular field, put it after
3298 the other fields. */
3301 DECL_CHAIN (gnu_field) = gnu_field_list;
3302 gnu_field_list = gnu_field;
3304 gnu_last = gnu_field;
3307 save_gnu_tree (gnat_field, gnu_field, false);
3310 /* If there is a variant list and no selected variant, we need
3311 to create the nest of variant parts from the old nest. */
3312 if (gnu_variant_list && !selected_variant)
3314 tree new_variant_part
3315 = create_variant_part_from (gnu_variant_part,
3316 gnu_variant_list, gnu_type,
3317 gnu_pos_list, gnu_subst_list);
3318 DECL_CHAIN (new_variant_part) = gnu_field_list;
3319 gnu_field_list = new_variant_part;
3322 /* Now go through the entities again looking for Itypes that
3323 we have not elaborated but should (e.g., Etypes of fields
3324 that have Original_Components). */
3325 for (gnat_field = First_Entity (gnat_entity);
3326 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3327 if ((Ekind (gnat_field) == E_Discriminant
3328 || Ekind (gnat_field) == E_Component)
3329 && !present_gnu_tree (Etype (gnat_field)))
3330 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3332 /* Do not emit debug info for the type yet since we're going to
3334 gnu_field_list = nreverse (gnu_field_list);
3335 finish_record_type (gnu_type, gnu_field_list, 2, false);
3337 /* See the E_Record_Type case for the rationale. */
3338 if (Is_By_Reference_Type (gnat_entity))
3339 SET_TYPE_MODE (gnu_type, BLKmode);
3341 compute_record_mode (gnu_type);
3343 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3345 /* Fill in locations of fields. */
3346 annotate_rep (gnat_entity, gnu_type);
3348 /* If debugging information is being written for the type, write
3349 a record that shows what we are a subtype of and also make a
3350 variable that indicates our size, if still variable. */
3353 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3354 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3355 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3357 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3358 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3360 TYPE_NAME (gnu_subtype_marker)
3361 = create_concat_name (gnat_entity, "XVS");
3362 finish_record_type (gnu_subtype_marker,
3363 create_field_decl (gnu_unpad_base_name,
3364 build_reference_type
3365 (gnu_unpad_base_type),
3367 NULL_TREE, NULL_TREE,
3371 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3372 gnu_subtype_marker);
3375 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3376 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3377 TYPE_SIZE_UNIT (gnu_subtype_marker)
3378 = create_var_decl (create_concat_name (gnat_entity,
3380 NULL_TREE, sizetype, gnu_size_unit,
3381 false, false, false, false, NULL,
3385 VEC_free (variant_desc, heap, gnu_variant_list);
3386 VEC_free (subst_pair, heap, gnu_subst_list);
3388 /* Now we can finalize it. */
3389 rest_of_record_type_compilation (gnu_type);
3392 /* Otherwise, go down all the components in the new type and make
3393 them equivalent to those in the base type. */
3396 gnu_type = gnu_base_type;
3398 for (gnat_temp = First_Entity (gnat_entity);
3399 Present (gnat_temp);
3400 gnat_temp = Next_Entity (gnat_temp))
3401 if ((Ekind (gnat_temp) == E_Discriminant
3402 && !Is_Unchecked_Union (gnat_base_type))
3403 || Ekind (gnat_temp) == E_Component)
3404 save_gnu_tree (gnat_temp,
3405 gnat_to_gnu_field_decl
3406 (Original_Record_Component (gnat_temp)),
3412 case E_Access_Subprogram_Type:
3413 /* Use the special descriptor type for dispatch tables if needed,
3414 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3415 Note that we are only required to do so for static tables in
3416 order to be compatible with the C++ ABI, but Ada 2005 allows
3417 to extend library level tagged types at the local level so
3418 we do it in the non-static case as well. */
3419 if (TARGET_VTABLE_USES_DESCRIPTORS
3420 && Is_Dispatch_Table_Entity (gnat_entity))
3422 gnu_type = fdesc_type_node;
3423 gnu_size = TYPE_SIZE (gnu_type);
3427 /* ... fall through ... */
3429 case E_Anonymous_Access_Subprogram_Type:
3430 /* If we are not defining this entity, and we have incomplete
3431 entities being processed above us, make a dummy type and
3432 fill it in later. */
3433 if (!definition && defer_incomplete_level != 0)
3435 struct incomplete *p
3436 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3439 = build_pointer_type
3440 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3441 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3442 !Comes_From_Source (gnat_entity),
3443 debug_info_p, gnat_entity);
3444 this_made_decl = true;
3445 gnu_type = TREE_TYPE (gnu_decl);
3446 save_gnu_tree (gnat_entity, gnu_decl, false);
3449 p->old_type = TREE_TYPE (gnu_type);
3450 p->full_type = Directly_Designated_Type (gnat_entity);
3451 p->next = defer_incomplete_list;
3452 defer_incomplete_list = p;
3456 /* ... fall through ... */
3458 case E_Allocator_Type:
3460 case E_Access_Attribute_Type:
3461 case E_Anonymous_Access_Type:
3462 case E_General_Access_Type:
3464 /* The designated type and its equivalent type for gigi. */
3465 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3466 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3467 /* Whether it comes from a limited with. */
3468 bool is_from_limited_with
3469 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3470 && From_With_Type (gnat_desig_equiv));
3471 /* The "full view" of the designated type. If this is an incomplete
3472 entity from a limited with, treat its non-limited view as the full
3473 view. Otherwise, if this is an incomplete or private type, use the
3474 full view. In the former case, we might point to a private type,
3475 in which case, we need its full view. Also, we want to look at the
3476 actual type used for the representation, so this takes a total of
3478 Entity_Id gnat_desig_full_direct_first
3479 = (is_from_limited_with
3480 ? Non_Limited_View (gnat_desig_equiv)
3481 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3482 ? Full_View (gnat_desig_equiv) : Empty));
3483 Entity_Id gnat_desig_full_direct
3484 = ((is_from_limited_with
3485 && Present (gnat_desig_full_direct_first)
3486 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3487 ? Full_View (gnat_desig_full_direct_first)
3488 : gnat_desig_full_direct_first);
3489 Entity_Id gnat_desig_full
3490 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3491 /* The type actually used to represent the designated type, either
3492 gnat_desig_full or gnat_desig_equiv. */
3493 Entity_Id gnat_desig_rep;
3494 /* True if this is a pointer to an unconstrained array. */
3495 bool is_unconstrained_array;
3496 /* We want to know if we'll be seeing the freeze node for any
3497 incomplete type we may be pointing to. */
3499 = (Present (gnat_desig_full)
3500 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3501 : In_Extended_Main_Code_Unit (gnat_desig_type));
3502 /* True if we make a dummy type here. */
3503 bool made_dummy = false;
3504 /* True if the dummy type is a fat pointer. */
3505 bool got_fat_p = false;
3506 /* The mode to be used for the pointer type. */
3507 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3508 /* The GCC type used for the designated type. */
3509 tree gnu_desig_type = NULL_TREE;
3511 if (!targetm.valid_pointer_mode (p_mode))
3514 /* If either the designated type or its full view is an unconstrained
3515 array subtype, replace it with the type it's a subtype of. This
3516 avoids problems with multiple copies of unconstrained array types.
3517 Likewise, if the designated type is a subtype of an incomplete
3518 record type, use the parent type to avoid order of elaboration
3519 issues. This can lose some code efficiency, but there is no
3521 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3522 && !Is_Constrained (gnat_desig_equiv))
3523 gnat_desig_equiv = Etype (gnat_desig_equiv);
3524 if (Present (gnat_desig_full)
3525 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3526 && !Is_Constrained (gnat_desig_full))
3527 || (Ekind (gnat_desig_full) == E_Record_Subtype
3528 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3529 gnat_desig_full = Etype (gnat_desig_full);
3531 /* Set the type that's actually the representation of the designated
3532 type and also flag whether we have a unconstrained array. */
3534 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3535 is_unconstrained_array
3536 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3538 /* If we are pointing to an incomplete type whose completion is an
3539 unconstrained array, make a fat pointer type. The two types in our
3540 fields will be pointers to dummy nodes and will be replaced in
3541 update_pointer_to. Similarly, if the type itself is a dummy type or
3542 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3543 in case we have any thin pointers to it. */
3544 if (is_unconstrained_array
3545 && (Present (gnat_desig_full)
3546 || (present_gnu_tree (gnat_desig_equiv)
3548 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3550 && defer_incomplete_level != 0
3551 && !present_gnu_tree (gnat_desig_equiv))
3553 && is_from_limited_with
3554 && Present (Freeze_Node (gnat_desig_equiv)))))
3556 if (present_gnu_tree (gnat_desig_rep))
3557 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3560 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3561 /* Show the dummy we get will be a fat pointer. */
3562 got_fat_p = made_dummy = true;
3565 /* If the call above got something that has a pointer, the pointer
3566 is our type. This could have happened either because the type
3567 was elaborated or because somebody else executed the code. */
3568 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3571 tree gnu_template_type = make_node (RECORD_TYPE);
3572 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3573 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3574 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3577 TYPE_NAME (gnu_template_type)
3578 = create_concat_name (gnat_desig_equiv, "XUB");
3579 TYPE_DUMMY_P (gnu_template_type) = 1;
3581 TYPE_NAME (gnu_array_type)
3582 = create_concat_name (gnat_desig_equiv, "XUA");
3583 TYPE_DUMMY_P (gnu_array_type) = 1;
3585 gnu_type = make_node (RECORD_TYPE);
3586 /* Build a stub DECL to trigger the special processing for fat
3587 pointer types in gnat_pushdecl. */
3588 TYPE_NAME (gnu_type)
3589 = create_type_stub_decl
3590 (create_concat_name (gnat_desig_equiv, "XUP"), gnu_type);
3591 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_desig_type);
3592 TYPE_POINTER_TO (gnu_desig_type) = gnu_type;
3595 = create_field_decl (get_identifier ("P_ARRAY"),
3596 gnu_ptr_array, gnu_type,
3597 NULL_TREE, NULL_TREE, 0, 0);
3599 = create_field_decl (get_identifier ("P_BOUNDS"),
3600 gnu_ptr_template, gnu_type,
3601 NULL_TREE, NULL_TREE, 0, 0);
3602 finish_fat_pointer_type (gnu_type, fields);
3604 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)
3605 = make_node (RECORD_TYPE);
3606 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type))
3607 = create_concat_name (gnat_desig_equiv, "XUT");
3608 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)) = 1;
3612 /* If we already know what the full type is, use it. */
3613 else if (Present (gnat_desig_full)
3614 && present_gnu_tree (gnat_desig_full))
3615 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3617 /* Get the type of the thing we are to point to and build a pointer to
3618 it. If it is a reference to an incomplete or private type with a
3619 full view that is a record, make a dummy type node and get the
3620 actual type later when we have verified it is safe. */
3621 else if ((!in_main_unit
3622 && !present_gnu_tree (gnat_desig_equiv)
3623 && Present (gnat_desig_full)
3624 && !present_gnu_tree (gnat_desig_full)
3625 && Is_Record_Type (gnat_desig_full))
3626 /* Likewise if we are pointing to a record or array and we are
3627 to defer elaborating incomplete types. We do this as this
3628 access type may be the full view of a private type. Note
3629 that the unconstrained array case is handled above. */
3630 || ((!in_main_unit || imported_p)
3631 && defer_incomplete_level != 0
3632 && !present_gnu_tree (gnat_desig_equiv)
3633 && (Is_Record_Type (gnat_desig_rep)
3634 || Is_Array_Type (gnat_desig_rep)))
3635 /* If this is a reference from a limited_with type back to our
3636 main unit and there's a freeze node for it, either we have
3637 already processed the declaration and made the dummy type,
3638 in which case we just reuse the latter, or we have not yet,
3639 in which case we make the dummy type and it will be reused
3640 when the declaration is finally processed. In both cases,
3641 the pointer eventually created below will be automatically
3642 adjusted when the freeze node is processed. Note that the
3643 unconstrained array case is handled above. */
3645 && is_from_limited_with
3646 && Present (Freeze_Node (gnat_desig_rep))))
3648 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3652 /* Otherwise handle the case of a pointer to itself. */
3653 else if (gnat_desig_equiv == gnat_entity)
3656 = build_pointer_type_for_mode (void_type_node, p_mode,
3657 No_Strict_Aliasing (gnat_entity));
3658 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3661 /* If expansion is disabled, the equivalent type of a concurrent type
3662 is absent, so build a dummy pointer type. */
3663 else if (type_annotate_only && No (gnat_desig_equiv))
3664 gnu_type = ptr_void_type_node;
3666 /* Finally, handle the default case where we can just elaborate our
3669 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3671 /* It is possible that a call to gnat_to_gnu_type above resolved our
3672 type. If so, just return it. */
3673 if (present_gnu_tree (gnat_entity))
3675 maybe_present = true;
3679 /* If we have not done it yet, build the pointer type the usual way. */
3682 /* Modify the designated type if we are pointing only to constant
3683 objects, but don't do it for unconstrained arrays. */
3684 if (Is_Access_Constant (gnat_entity)
3685 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3688 = build_qualified_type
3690 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3692 /* Some extra processing is required if we are building a
3693 pointer to an incomplete type (in the GCC sense). We might
3694 have such a type if we just made a dummy, or directly out
3695 of the call to gnat_to_gnu_type above if we are processing
3696 an access type for a record component designating the
3697 record type itself. */
3698 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3700 /* We must ensure that the pointer to variant we make will
3701 be processed by update_pointer_to when the initial type
3702 is completed. Pretend we made a dummy and let further
3703 processing act as usual. */
3706 /* We must ensure that update_pointer_to will not retrieve
3707 the dummy variant when building a properly qualified
3708 version of the complete type. We take advantage of the
3709 fact that get_qualified_type is requiring TYPE_NAMEs to
3710 match to influence build_qualified_type and then also
3711 update_pointer_to here. */
3712 TYPE_NAME (gnu_desig_type)
3713 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3718 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3719 No_Strict_Aliasing (gnat_entity));
3722 /* If we are not defining this object and we have made a dummy pointer,
3723 save our current definition, evaluate the actual type, and replace
3724 the tentative type we made with the actual one. If we are to defer
3725 actually looking up the actual type, make an entry in the deferred
3726 list. If this is from a limited with, we have to defer to the end
3727 of the current spec in two cases: first if the designated type is
3728 in the current unit and second if the access type itself is. */
3729 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3731 bool is_from_limited_with_in_main_unit
3732 = (is_from_limited_with
3734 || In_Extended_Main_Code_Unit (gnat_entity)));
3735 tree gnu_old_desig_type
3736 = TYPE_IS_FAT_POINTER_P (gnu_type)
3737 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3739 if (esize == POINTER_SIZE
3740 && (got_fat_p || TYPE_IS_FAT_POINTER_P (gnu_type)))
3742 = build_pointer_type
3743 (TYPE_OBJECT_RECORD_TYPE
3744 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3746 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3747 !Comes_From_Source (gnat_entity),
3748 debug_info_p, gnat_entity);
3749 this_made_decl = true;
3750 gnu_type = TREE_TYPE (gnu_decl);
3751 save_gnu_tree (gnat_entity, gnu_decl, false);
3754 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3755 update gnu_old_desig_type directly, in which case it will not be
3756 a dummy type any more when we get into update_pointer_to.
3758 This can happen e.g. when the designated type is a record type,
3759 because their elaboration starts with an initial node from
3760 make_dummy_type, which may be the same node as the one we got.
3762 Besides, variants of this non-dummy type might have been created
3763 along the way. update_pointer_to is expected to properly take
3764 care of those situations. */
3765 if (defer_incomplete_level == 0
3766 && !is_from_limited_with_in_main_unit)
3767 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3768 gnat_to_gnu_type (gnat_desig_equiv));
3771 struct incomplete *p = XNEW (struct incomplete);
3772 struct incomplete **head
3773 = (is_from_limited_with_in_main_unit
3774 ? &defer_limited_with : &defer_incomplete_list);
3775 p->old_type = gnu_old_desig_type;
3776 p->full_type = gnat_desig_equiv;
3784 case E_Access_Protected_Subprogram_Type:
3785 case E_Anonymous_Access_Protected_Subprogram_Type:
3786 if (type_annotate_only && No (gnat_equiv_type))
3787 gnu_type = ptr_void_type_node;
3790 /* The run-time representation is the equivalent type. */
3791 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3792 maybe_present = true;
3795 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3796 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3797 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3798 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3799 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3804 case E_Access_Subtype:
3806 /* We treat this as identical to its base type; any constraint is
3807 meaningful only to the front end.
3809 The designated type must be elaborated as well, if it does
3810 not have its own freeze node. Designated (sub)types created
3811 for constrained components of records with discriminants are
3812 not frozen by the front end and thus not elaborated by gigi,
3813 because their use may appear before the base type is frozen,
3814 and because it is not clear that they are needed anywhere in
3815 Gigi. With the current model, there is no correct place where
3816 they could be elaborated. */
3818 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3819 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3820 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3821 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3822 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3824 /* If we are not defining this entity, and we have incomplete
3825 entities being processed above us, make a dummy type and
3826 elaborate it later. */
3827 if (!definition && defer_incomplete_level != 0)
3829 struct incomplete *p
3830 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3832 = build_pointer_type
3833 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3835 p->old_type = TREE_TYPE (gnu_ptr_type);
3836 p->full_type = Directly_Designated_Type (gnat_entity);
3837 p->next = defer_incomplete_list;
3838 defer_incomplete_list = p;
3840 else if (!IN (Ekind (Base_Type
3841 (Directly_Designated_Type (gnat_entity))),
3842 Incomplete_Or_Private_Kind))
3843 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3847 maybe_present = true;
3850 /* Subprogram Entities
3852 The following access functions are defined for subprograms:
3854 Etype Return type or Standard_Void_Type.
3855 First_Formal The first formal parameter.
3856 Is_Imported Indicates that the subprogram has appeared in
3857 an INTERFACE or IMPORT pragma. For now we
3858 assume that the external language is C.
3859 Is_Exported Likewise but for an EXPORT pragma.
3860 Is_Inlined True if the subprogram is to be inlined.
3862 Each parameter is first checked by calling must_pass_by_ref on its
3863 type to determine if it is passed by reference. For parameters which
3864 are copied in, if they are Ada In Out or Out parameters, their return
3865 value becomes part of a record which becomes the return type of the
3866 function (C function - note that this applies only to Ada procedures
3867 so there is no Ada return type). Additional code to store back the
3868 parameters will be generated on the caller side. This transformation
3869 is done here, not in the front-end.
3871 The intended result of the transformation can be seen from the
3872 equivalent source rewritings that follow:
3874 struct temp {int a,b};
3875 procedure P (A,B: In Out ...) is temp P (int A,B)
3878 end P; return {A,B};
3885 For subprogram types we need to perform mainly the same conversions to
3886 GCC form that are needed for procedures and function declarations. The
3887 only difference is that at the end, we make a type declaration instead
3888 of a function declaration. */
3890 case E_Subprogram_Type:
3894 /* The type returned by a function or else Standard_Void_Type for a
3896 Entity_Id gnat_return_type = Etype (gnat_entity);
3897 tree gnu_return_type;
3898 /* The first GCC parameter declaration (a PARM_DECL node). The
3899 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3900 actually is the head of this parameter list. */
3901 tree gnu_param_list = NULL_TREE;
3902 /* Likewise for the stub associated with an exported procedure. */
3903 tree gnu_stub_param_list = NULL_TREE;
3904 /* Non-null for subprograms containing parameters passed by copy-in
3905 copy-out (Ada In Out or Out parameters not passed by reference),
3906 in which case it is the list of nodes used to specify the values
3907 of the In Out/Out parameters that are returned as a record upon
3908 procedure return. The TREE_PURPOSE of an element of this list is
3909 a field of the record and the TREE_VALUE is the PARM_DECL
3910 corresponding to that field. This list will be saved in the
3911 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3912 tree gnu_cico_list = NULL_TREE;
3913 /* List of fields in return type of procedure with copy-in copy-out
3915 tree gnu_field_list = NULL_TREE;
3916 /* If an import pragma asks to map this subprogram to a GCC builtin,
3917 this is the builtin DECL node. */
3918 tree gnu_builtin_decl = NULL_TREE;
3919 /* For the stub associated with an exported procedure. */
3920 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3921 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3922 Entity_Id gnat_param;
3923 bool inline_flag = Is_Inlined (gnat_entity);
3924 bool public_flag = Is_Public (gnat_entity) || imported_p;
3926 = (Is_Public (gnat_entity) && !definition) || imported_p;
3927 /* The semantics of "pure" in Ada essentially matches that of "const"
3928 in the back-end. In particular, both properties are orthogonal to
3929 the "nothrow" property if the EH circuitry is explicit in the
3930 internal representation of the back-end. If we are to completely
3931 hide the EH circuitry from it, we need to declare that calls to pure
3932 Ada subprograms that can throw have side effects since they can
3933 trigger an "abnormal" transfer of control flow; thus they can be
3934 neither "const" nor "pure" in the back-end sense. */
3936 = (Exception_Mechanism == Back_End_Exceptions
3937 && Is_Pure (gnat_entity));
3938 bool volatile_flag = No_Return (gnat_entity);
3939 bool return_by_direct_ref_p = false;
3940 bool return_by_invisi_ref_p = false;
3941 bool return_unconstrained_p = false;
3942 bool has_stub = false;
3945 /* A parameter may refer to this type, so defer completion of any
3946 incomplete types. */
3947 if (kind == E_Subprogram_Type && !definition)
3949 defer_incomplete_level++;
3950 this_deferred = true;
3953 /* If the subprogram has an alias, it is probably inherited, so
3954 we can use the original one. If the original "subprogram"
3955 is actually an enumeration literal, it may be the first use
3956 of its type, so we must elaborate that type now. */
3957 if (Present (Alias (gnat_entity)))
3959 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3960 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3962 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
3964 /* Elaborate any Itypes in the parameters of this entity. */
3965 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3966 Present (gnat_temp);
3967 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3968 if (Is_Itype (Etype (gnat_temp)))
3969 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3974 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3975 corresponding DECL node. Proper generation of calls later on need
3976 proper parameter associations so we don't "break;" here. */
3977 if (Convention (gnat_entity) == Convention_Intrinsic
3978 && Present (Interface_Name (gnat_entity)))
3980 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3982 /* Inability to find the builtin decl most often indicates a
3983 genuine mistake, but imports of unregistered intrinsics are
3984 sometimes issued on purpose to allow hooking in alternate
3985 bodies. We post a warning conditioned on Wshadow in this case,
3986 to let developers be notified on demand without risking false
3987 positives with common default sets of options. */
3989 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
3990 post_error ("?gcc intrinsic not found for&!", gnat_entity);
3993 /* ??? What if we don't find the builtin node above ? warn ? err ?
3994 In the current state we neither warn nor err, and calls will just
3995 be handled as for regular subprograms. */
3997 /* Look into the return type and get its associated GCC tree. If it
3998 is not void, compute various flags for the subprogram type. */
3999 if (Ekind (gnat_return_type) == E_Void)
4000 gnu_return_type = void_type_node;
4003 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4005 /* If this function returns by reference, make the actual return
4006 type the pointer type and make a note of that. */
4007 if (Returns_By_Ref (gnat_entity))
4009 gnu_return_type = build_pointer_type (gnu_return_type);
4010 return_by_direct_ref_p = true;
4013 /* If we are supposed to return an unconstrained array type, make
4014 the actual return type the fat pointer type. */
4015 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4017 gnu_return_type = TREE_TYPE (gnu_return_type);
4018 return_unconstrained_p = true;
4021 /* Likewise, if the return type requires a transient scope, the
4022 return value will be allocated on the secondary stack so the
4023 actual return type is the pointer type. */
4024 else if (Requires_Transient_Scope (gnat_return_type))
4026 gnu_return_type = build_pointer_type (gnu_return_type);
4027 return_unconstrained_p = true;
4030 /* If the Mechanism is By_Reference, ensure this function uses the
4031 target's by-invisible-reference mechanism, which may not be the
4032 same as above (e.g. it might be passing an extra parameter). */
4033 else if (kind == E_Function
4034 && Mechanism (gnat_entity) == By_Reference)
4035 return_by_invisi_ref_p = true;
4037 /* Likewise, if the return type is itself By_Reference. */
4038 else if (TREE_ADDRESSABLE (gnu_return_type))
4039 return_by_invisi_ref_p = true;
4041 /* If the type is a padded type and the underlying type would not
4042 be passed by reference or the function has a foreign convention,
4043 return the underlying type. */
4044 else if (TYPE_IS_PADDING_P (gnu_return_type)
4045 && (!default_pass_by_ref
4046 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4047 || Has_Foreign_Convention (gnat_entity)))
4048 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4050 /* If the return type is unconstrained, that means it must have a
4051 maximum size. Use the padded type as the effective return type.
4052 And ensure the function uses the target's by-invisible-reference
4053 mechanism to avoid copying too much data when it returns. */
4054 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4057 = maybe_pad_type (gnu_return_type,
4058 max_size (TYPE_SIZE (gnu_return_type),
4060 0, gnat_entity, false, false, false, true);
4061 return_by_invisi_ref_p = true;
4064 /* If the return type has a size that overflows, we cannot have
4065 a function that returns that type. This usage doesn't make
4066 sense anyway, so give an error here. */
4067 if (TYPE_SIZE_UNIT (gnu_return_type)
4068 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
4069 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
4071 post_error ("cannot return type whose size overflows",
4073 gnu_return_type = copy_node (gnu_return_type);
4074 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4075 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4076 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4077 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4081 /* Loop over the parameters and get their associated GCC tree. While
4082 doing this, build a copy-in copy-out structure if we need one. */
4083 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4084 Present (gnat_param);
4085 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4087 tree gnu_param_name = get_entity_name (gnat_param);
4088 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4089 tree gnu_param, gnu_field;
4090 bool copy_in_copy_out = false;
4091 Mechanism_Type mech = Mechanism (gnat_param);
4093 /* Builtins are expanded inline and there is no real call sequence
4094 involved. So the type expected by the underlying expander is
4095 always the type of each argument "as is". */
4096 if (gnu_builtin_decl)
4098 /* Handle the first parameter of a valued procedure specially. */
4099 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4100 mech = By_Copy_Return;
4101 /* Otherwise, see if a Mechanism was supplied that forced this
4102 parameter to be passed one way or another. */
4103 else if (mech == Default
4104 || mech == By_Copy || mech == By_Reference)
4106 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4107 mech = By_Descriptor;
4109 else if (By_Short_Descriptor_Last <= mech &&
4110 mech <= By_Short_Descriptor)
4111 mech = By_Short_Descriptor;
4115 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4116 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4117 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4119 mech = By_Reference;
4125 post_error ("unsupported mechanism for&", gnat_param);
4130 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4131 Has_Foreign_Convention (gnat_entity),
4134 /* We are returned either a PARM_DECL or a type if no parameter
4135 needs to be passed; in either case, adjust the type. */
4136 if (DECL_P (gnu_param))
4137 gnu_param_type = TREE_TYPE (gnu_param);
4140 gnu_param_type = gnu_param;
4141 gnu_param = NULL_TREE;
4144 /* The failure of this assertion will very likely come from an
4145 order of elaboration issue for the type of the parameter. */
4146 gcc_assert (kind == E_Subprogram_Type
4147 || !TYPE_IS_DUMMY_P (gnu_param_type));
4151 /* If it's an exported subprogram, we build a parameter list
4152 in parallel, in case we need to emit a stub for it. */
4153 if (Is_Exported (gnat_entity))
4156 = chainon (gnu_param, gnu_stub_param_list);
4157 /* Change By_Descriptor parameter to By_Reference for
4158 the internal version of an exported subprogram. */
4159 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4162 = gnat_to_gnu_param (gnat_param, By_Reference,
4168 gnu_param = copy_node (gnu_param);
4171 gnu_param_list = chainon (gnu_param, gnu_param_list);
4172 Sloc_to_locus (Sloc (gnat_param),
4173 &DECL_SOURCE_LOCATION (gnu_param));
4174 save_gnu_tree (gnat_param, gnu_param, false);
4176 /* If a parameter is a pointer, this function may modify
4177 memory through it and thus shouldn't be considered
4178 a const function. Also, the memory may be modified
4179 between two calls, so they can't be CSE'ed. The latter
4180 case also handles by-ref parameters. */
4181 if (POINTER_TYPE_P (gnu_param_type)
4182 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4186 if (copy_in_copy_out)
4190 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4192 /* If this is a function, we also need a field for the
4193 return value to be placed. */
4194 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4197 = create_field_decl (get_identifier ("RETVAL"),
4199 gnu_new_ret_type, NULL_TREE,
4201 Sloc_to_locus (Sloc (gnat_entity),
4202 &DECL_SOURCE_LOCATION (gnu_field));
4203 gnu_field_list = gnu_field;
4205 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4208 gnu_return_type = gnu_new_ret_type;
4209 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4210 /* Set a default alignment to speed up accesses. */
4211 TYPE_ALIGN (gnu_return_type)
4212 = get_mode_alignment (ptr_mode);
4216 = create_field_decl (gnu_param_name, gnu_param_type,
4217 gnu_return_type, NULL_TREE, NULL_TREE,
4219 Sloc_to_locus (Sloc (gnat_param),
4220 &DECL_SOURCE_LOCATION (gnu_field));
4221 DECL_CHAIN (gnu_field) = gnu_field_list;
4222 gnu_field_list = gnu_field;
4224 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4228 /* Do not compute record for out parameters if subprogram is
4229 stubbed since structures are incomplete for the back-end. */
4230 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4231 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4234 /* If we have a CICO list but it has only one entry, we convert
4235 this function into a function that simply returns that one
4237 if (list_length (gnu_cico_list) == 1)
4238 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4240 if (Has_Stdcall_Convention (gnat_entity))
4241 prepend_one_attribute_to
4242 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4243 get_identifier ("stdcall"), NULL_TREE,
4246 /* If we should request stack realignment for a foreign convention
4247 subprogram, do so. Note that this applies to task entry points in
4249 if (FOREIGN_FORCE_REALIGN_STACK
4250 && Has_Foreign_Convention (gnat_entity))
4251 prepend_one_attribute_to
4252 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4253 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4256 /* The lists have been built in reverse. */
4257 gnu_param_list = nreverse (gnu_param_list);
4259 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4260 gnu_cico_list = nreverse (gnu_cico_list);
4262 if (kind == E_Function)
4263 Set_Mechanism (gnat_entity, return_unconstrained_p
4264 || return_by_direct_ref_p
4265 || return_by_invisi_ref_p
4266 ? By_Reference : By_Copy);
4268 = create_subprog_type (gnu_return_type, gnu_param_list,
4269 gnu_cico_list, return_unconstrained_p,
4270 return_by_direct_ref_p,
4271 return_by_invisi_ref_p);
4275 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4276 gnu_cico_list, return_unconstrained_p,
4277 return_by_direct_ref_p,
4278 return_by_invisi_ref_p);
4280 /* A subprogram (something that doesn't return anything) shouldn't
4281 be considered const since there would be no reason for such a
4282 subprogram. Note that procedures with Out (or In Out) parameters
4283 have already been converted into a function with a return type. */
4284 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4288 = build_qualified_type (gnu_type,
4289 TYPE_QUALS (gnu_type)
4290 | (TYPE_QUAL_CONST * const_flag)
4291 | (TYPE_QUAL_VOLATILE * volatile_flag));
4295 = build_qualified_type (gnu_stub_type,
4296 TYPE_QUALS (gnu_stub_type)
4297 | (TYPE_QUAL_CONST * const_flag)
4298 | (TYPE_QUAL_VOLATILE * volatile_flag));
4300 /* If we have a builtin decl for that function, use it. Check if the
4301 profiles are compatible and warn if they are not. The checker is
4302 expected to post extra diagnostics in this case. */
4303 if (gnu_builtin_decl)
4305 intrin_binding_t inb;
4307 inb.gnat_entity = gnat_entity;
4308 inb.ada_fntype = gnu_type;
4309 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4311 if (!intrin_profiles_compatible_p (&inb))
4313 ("?profile of& doesn''t match the builtin it binds!",
4316 gnu_decl = gnu_builtin_decl;
4317 gnu_type = TREE_TYPE (gnu_builtin_decl);
4321 /* If there was no specified Interface_Name and the external and
4322 internal names of the subprogram are the same, only use the
4323 internal name to allow disambiguation of nested subprograms. */
4324 if (No (Interface_Name (gnat_entity))
4325 && gnu_ext_name == gnu_entity_name)
4326 gnu_ext_name = NULL_TREE;
4328 /* If we are defining the subprogram and it has an Address clause
4329 we must get the address expression from the saved GCC tree for the
4330 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4331 the address expression here since the front-end has guaranteed
4332 in that case that the elaboration has no effects. If there is
4333 an Address clause and we are not defining the object, just
4334 make it a constant. */
4335 if (Present (Address_Clause (gnat_entity)))
4337 tree gnu_address = NULL_TREE;
4341 = (present_gnu_tree (gnat_entity)
4342 ? get_gnu_tree (gnat_entity)
4343 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4345 save_gnu_tree (gnat_entity, NULL_TREE, false);
4347 /* Convert the type of the object to a reference type that can
4348 alias everything as per 13.3(19). */
4350 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4352 gnu_address = convert (gnu_type, gnu_address);
4355 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4356 gnu_address, false, Is_Public (gnat_entity),
4357 extern_flag, false, NULL, gnat_entity);
4358 DECL_BY_REF_P (gnu_decl) = 1;
4361 else if (kind == E_Subprogram_Type)
4362 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4363 !Comes_From_Source (gnat_entity),
4364 debug_info_p, gnat_entity);
4369 gnu_stub_name = gnu_ext_name;
4370 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4371 public_flag = false;
4374 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4375 gnu_type, gnu_param_list,
4376 inline_flag, public_flag,
4377 extern_flag, attr_list,
4382 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4383 gnu_stub_type, gnu_stub_param_list,
4385 extern_flag, attr_list,
4387 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4390 /* This is unrelated to the stub built right above. */
4391 DECL_STUBBED_P (gnu_decl)
4392 = Convention (gnat_entity) == Convention_Stubbed;
4397 case E_Incomplete_Type:
4398 case E_Incomplete_Subtype:
4399 case E_Private_Type:
4400 case E_Private_Subtype:
4401 case E_Limited_Private_Type:
4402 case E_Limited_Private_Subtype:
4403 case E_Record_Type_With_Private:
4404 case E_Record_Subtype_With_Private:
4406 /* Get the "full view" of this entity. If this is an incomplete
4407 entity from a limited with, treat its non-limited view as the
4408 full view. Otherwise, use either the full view or the underlying
4409 full view, whichever is present. This is used in all the tests
4412 = (IN (kind, Incomplete_Kind) && From_With_Type (gnat_entity))
4413 ? Non_Limited_View (gnat_entity)
4414 : Present (Full_View (gnat_entity))
4415 ? Full_View (gnat_entity)
4416 : Underlying_Full_View (gnat_entity);
4418 /* If this is an incomplete type with no full view, it must be a Taft
4419 Amendment type, in which case we return a dummy type. Otherwise,
4420 just get the type from its Etype. */
4423 if (kind == E_Incomplete_Type)
4425 gnu_type = make_dummy_type (gnat_entity);
4426 gnu_decl = TYPE_STUB_DECL (gnu_type);
4430 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4432 maybe_present = true;
4437 /* If we already made a type for the full view, reuse it. */
4438 else if (present_gnu_tree (full_view))
4440 gnu_decl = get_gnu_tree (full_view);
4444 /* Otherwise, if we are not defining the type now, get the type
4445 from the full view. But always get the type from the full view
4446 for define on use types, since otherwise we won't see them! */
4447 else if (!definition
4448 || (Is_Itype (full_view)
4449 && No (Freeze_Node (gnat_entity)))
4450 || (Is_Itype (gnat_entity)
4451 && No (Freeze_Node (full_view))))
4453 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4454 maybe_present = true;
4458 /* For incomplete types, make a dummy type entry which will be
4459 replaced later. Save it as the full declaration's type so
4460 we can do any needed updates when we see it. */
4461 gnu_type = make_dummy_type (gnat_entity);
4462 gnu_decl = TYPE_STUB_DECL (gnu_type);
4463 save_gnu_tree (full_view, gnu_decl, 0);
4467 case E_Class_Wide_Type:
4468 /* Class-wide types are always transformed into their root type. */
4469 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4470 maybe_present = true;
4474 case E_Task_Subtype:
4475 case E_Protected_Type:
4476 case E_Protected_Subtype:
4477 /* Concurrent types are always transformed into their record type. */
4478 if (type_annotate_only && No (gnat_equiv_type))
4479 gnu_type = void_type_node;
4481 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4482 maybe_present = true;
4486 gnu_decl = create_label_decl (gnu_entity_name);
4491 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4492 we've already saved it, so we don't try to. */
4493 gnu_decl = error_mark_node;
4501 /* If we had a case where we evaluated another type and it might have
4502 defined this one, handle it here. */
4503 if (maybe_present && present_gnu_tree (gnat_entity))
4505 gnu_decl = get_gnu_tree (gnat_entity);
4509 /* If we are processing a type and there is either no decl for it or
4510 we just made one, do some common processing for the type, such as
4511 handling alignment and possible padding. */
4512 if (is_type && (!gnu_decl || this_made_decl))
4514 /* Tell the middle-end that objects of tagged types are guaranteed to
4515 be properly aligned. This is necessary because conversions to the
4516 class-wide type are translated into conversions to the root type,
4517 which can be less aligned than some of its derived types. */
4518 if (Is_Tagged_Type (gnat_entity)
4519 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4520 TYPE_ALIGN_OK (gnu_type) = 1;
4522 /* If the type is passed by reference, objects of this type must be
4523 fully addressable and cannot be copied. */
4524 if (Is_By_Reference_Type (gnat_entity))
4525 TREE_ADDRESSABLE (gnu_type) = 1;
4527 /* ??? Don't set the size for a String_Literal since it is either
4528 confirming or we don't handle it properly (if the low bound is
4530 if (!gnu_size && kind != E_String_Literal_Subtype)
4531 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4533 Has_Size_Clause (gnat_entity));
4535 /* If a size was specified, see if we can make a new type of that size
4536 by rearranging the type, for example from a fat to a thin pointer. */
4540 = make_type_from_size (gnu_type, gnu_size,
4541 Has_Biased_Representation (gnat_entity));
4543 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4544 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4548 /* If the alignment hasn't already been processed and this is
4549 not an unconstrained array, see if an alignment is specified.
4550 If not, we pick a default alignment for atomic objects. */
4551 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4553 else if (Known_Alignment (gnat_entity))
4555 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4556 TYPE_ALIGN (gnu_type));
4558 /* Warn on suspiciously large alignments. This should catch
4559 errors about the (alignment,byte)/(size,bit) discrepancy. */
4560 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4564 /* If a size was specified, take it into account. Otherwise
4565 use the RM size for records as the type size has already
4566 been adjusted to the alignment. */
4569 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4570 || TREE_CODE (gnu_type) == UNION_TYPE
4571 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4572 && !TYPE_FAT_POINTER_P (gnu_type))
4573 size = rm_size (gnu_type);
4575 size = TYPE_SIZE (gnu_type);
4577 /* Consider an alignment as suspicious if the alignment/size
4578 ratio is greater or equal to the byte/bit ratio. */
4579 if (host_integerp (size, 1)
4580 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4581 post_error_ne ("?suspiciously large alignment specified for&",
4582 Expression (Alignment_Clause (gnat_entity)),
4586 else if (Is_Atomic (gnat_entity) && !gnu_size
4587 && host_integerp (TYPE_SIZE (gnu_type), 1)
4588 && integer_pow2p (TYPE_SIZE (gnu_type)))
4589 align = MIN (BIGGEST_ALIGNMENT,
4590 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4591 else if (Is_Atomic (gnat_entity) && gnu_size
4592 && host_integerp (gnu_size, 1)
4593 && integer_pow2p (gnu_size))
4594 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4596 /* See if we need to pad the type. If we did, and made a record,
4597 the name of the new type may be changed. So get it back for
4598 us when we make the new TYPE_DECL below. */
4599 if (gnu_size || align > 0)
4600 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4601 false, !gnu_decl, definition, false);
4603 if (TYPE_IS_PADDING_P (gnu_type))
4605 gnu_entity_name = TYPE_NAME (gnu_type);
4606 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4607 gnu_entity_name = DECL_NAME (gnu_entity_name);
4610 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4612 /* If we are at global level, GCC will have applied variable_size to
4613 the type, but that won't have done anything. So, if it's not
4614 a constant or self-referential, call elaborate_expression_1 to
4615 make a variable for the size rather than calculating it each time.
4616 Handle both the RM size and the actual size. */
4617 if (global_bindings_p ()
4618 && TYPE_SIZE (gnu_type)
4619 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4620 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4622 tree size = TYPE_SIZE (gnu_type);
4624 TYPE_SIZE (gnu_type)
4625 = elaborate_expression_1 (size, gnat_entity,
4626 get_identifier ("SIZE"),
4629 /* ??? For now, store the size as a multiple of the alignment in
4630 bytes so that we can see the alignment from the tree. */
4631 TYPE_SIZE_UNIT (gnu_type)
4632 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4633 get_identifier ("SIZE_A_UNIT"),
4635 TYPE_ALIGN (gnu_type));
4637 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4638 may not be marked by the call to create_type_decl below. */
4639 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4641 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4643 tree variant_part = get_variant_part (gnu_type);
4644 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4648 tree union_type = TREE_TYPE (variant_part);
4649 tree offset = DECL_FIELD_OFFSET (variant_part);
4651 /* If the position of the variant part is constant, subtract
4652 it from the size of the type of the parent to get the new
4653 size. This manual CSE reduces the data size. */
4654 if (TREE_CODE (offset) == INTEGER_CST)
4656 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4657 TYPE_SIZE (union_type)
4658 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4659 bit_from_pos (offset, bitpos));
4660 TYPE_SIZE_UNIT (union_type)
4661 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4662 byte_from_pos (offset, bitpos));
4666 TYPE_SIZE (union_type)
4667 = elaborate_expression_1 (TYPE_SIZE (union_type),
4669 get_identifier ("VSIZE"),
4672 /* ??? For now, store the size as a multiple of the
4673 alignment in bytes so that we can see the alignment
4675 TYPE_SIZE_UNIT (union_type)
4676 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4681 TYPE_ALIGN (union_type));
4683 /* ??? For now, store the offset as a multiple of the
4684 alignment in bytes so that we can see the alignment
4686 DECL_FIELD_OFFSET (variant_part)
4687 = elaborate_expression_2 (offset,
4689 get_identifier ("VOFFSET"),
4695 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4696 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4699 if (operand_equal_p (ada_size, size, 0))
4700 ada_size = TYPE_SIZE (gnu_type);
4703 = elaborate_expression_1 (ada_size, gnat_entity,
4704 get_identifier ("RM_SIZE"),
4706 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4710 /* If this is a record type or subtype, call elaborate_expression_1 on
4711 any field position. Do this for both global and local types.
4712 Skip any fields that we haven't made trees for to avoid problems with
4713 class wide types. */
4714 if (IN (kind, Record_Kind))
4715 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4716 gnat_temp = Next_Entity (gnat_temp))
4717 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4719 tree gnu_field = get_gnu_tree (gnat_temp);
4721 /* ??? For now, store the offset as a multiple of the alignment
4722 in bytes so that we can see the alignment from the tree. */
4723 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4725 DECL_FIELD_OFFSET (gnu_field)
4726 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4728 get_identifier ("OFFSET"),
4730 DECL_OFFSET_ALIGN (gnu_field));
4732 /* ??? The context of gnu_field is not necessarily gnu_type
4733 so the MULT_EXPR node built above may not be marked by
4734 the call to create_type_decl below. */
4735 if (global_bindings_p ())
4736 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4740 if (Treat_As_Volatile (gnat_entity))
4742 = build_qualified_type (gnu_type,
4743 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4745 if (Is_Atomic (gnat_entity))
4746 check_ok_for_atomic (gnu_type, gnat_entity, false);
4748 if (Present (Alignment_Clause (gnat_entity)))
4749 TYPE_USER_ALIGN (gnu_type) = 1;
4751 if (Universal_Aliasing (gnat_entity))
4752 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4755 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4756 !Comes_From_Source (gnat_entity),
4757 debug_info_p, gnat_entity);
4760 TREE_TYPE (gnu_decl) = gnu_type;
4761 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4765 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4767 gnu_type = TREE_TYPE (gnu_decl);
4769 /* If this is a derived type, relate its alias set to that of its parent
4770 to avoid troubles when a call to an inherited primitive is inlined in
4771 a context where a derived object is accessed. The inlined code works
4772 on the parent view so the resulting code may access the same object
4773 using both the parent and the derived alias sets, which thus have to
4774 conflict. As the same issue arises with component references, the
4775 parent alias set also has to conflict with composite types enclosing
4776 derived components. For instance, if we have:
4783 we want T to conflict with both D and R, in addition to R being a
4784 superset of D by record/component construction.
4786 One way to achieve this is to perform an alias set copy from the
4787 parent to the derived type. This is not quite appropriate, though,
4788 as we don't want separate derived types to conflict with each other:
4790 type I1 is new Integer;
4791 type I2 is new Integer;
4793 We want I1 and I2 to both conflict with Integer but we do not want
4794 I1 to conflict with I2, and an alias set copy on derivation would
4797 The option chosen is to make the alias set of the derived type a
4798 superset of that of its parent type. It trivially fulfills the
4799 simple requirement for the Integer derivation example above, and
4800 the component case as well by superset transitivity:
4803 R ----------> D ----------> T
4805 However, for composite types, conversions between derived types are
4806 translated into VIEW_CONVERT_EXPRs so a sequence like:
4808 type Comp1 is new Comp;
4809 type Comp2 is new Comp;
4810 procedure Proc (C : Comp1);
4818 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4820 and gimplified into:
4827 i.e. generates code involving type punning. Therefore, Comp1 needs
4828 to conflict with Comp2 and an alias set copy is required.
4830 The language rules ensure the parent type is already frozen here. */
4831 if (Is_Derived_Type (gnat_entity))
4833 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4834 relate_alias_sets (gnu_type, gnu_parent_type,
4835 Is_Composite_Type (gnat_entity)
4836 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4839 /* Back-annotate the Alignment of the type if not already in the
4840 tree. Likewise for sizes. */
4841 if (Unknown_Alignment (gnat_entity))
4843 unsigned int double_align, align;
4844 bool is_capped_double, align_clause;
4846 /* If the default alignment of "double" or larger scalar types is
4847 specifically capped and this is not an array with an alignment
4848 clause on the component type, return the cap. */
4849 if ((double_align = double_float_alignment) > 0)
4851 = is_double_float_or_array (gnat_entity, &align_clause);
4852 else if ((double_align = double_scalar_alignment) > 0)
4854 = is_double_scalar_or_array (gnat_entity, &align_clause);
4856 is_capped_double = align_clause = false;
4858 if (is_capped_double && !align_clause)
4859 align = double_align;
4861 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4863 Set_Alignment (gnat_entity, UI_From_Int (align));
4866 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4868 tree gnu_size = TYPE_SIZE (gnu_type);
4870 /* If the size is self-referential, annotate the maximum value. */
4871 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4872 gnu_size = max_size (gnu_size, true);
4874 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4876 /* In this mode, the tag and the parent components are not
4877 generated by the front-end so the sizes must be adjusted. */
4878 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
4881 if (Is_Derived_Type (gnat_entity))
4883 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
4885 Set_Alignment (gnat_entity,
4886 Alignment (Etype (Base_Type (gnat_entity))));
4889 offset = pointer_size;
4891 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4892 gnu_size = size_binop (MULT_EXPR, pointer_size,
4893 size_binop (CEIL_DIV_EXPR,
4896 uint_size = annotate_value (gnu_size);
4897 Set_Esize (gnat_entity, uint_size);
4898 Set_RM_Size (gnat_entity, uint_size);
4901 Set_Esize (gnat_entity, annotate_value (gnu_size));
4904 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4905 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4908 /* If we really have a ..._DECL node, set a couple of flags on it. But we
4909 cannot do that if we are reusing the ..._DECL node made for a renamed
4910 object, since the predicates don't apply to it but to GNAT_ENTITY. */
4911 if (DECL_P (gnu_decl) && !(Present (Renamed_Object (gnat_entity)) && saved))
4913 if (!Comes_From_Source (gnat_entity))
4914 DECL_ARTIFICIAL (gnu_decl) = 1;
4916 if (!debug_info_p && TREE_CODE (gnu_decl) != FUNCTION_DECL)
4917 DECL_IGNORED_P (gnu_decl) = 1;
4920 /* If we haven't already, associate the ..._DECL node that we just made with
4921 the input GNAT entity node. */
4923 save_gnu_tree (gnat_entity, gnu_decl, false);
4925 /* If this is an enumeration or floating-point type, we were not able to set
4926 the bounds since they refer to the type. These are always static. */
4927 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4928 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4930 tree gnu_scalar_type = gnu_type;
4931 tree gnu_low_bound, gnu_high_bound;
4933 /* If this is a padded type, we need to use the underlying type. */
4934 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4935 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4937 /* If this is a floating point type and we haven't set a floating
4938 point type yet, use this in the evaluation of the bounds. */
4939 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4940 longest_float_type_node = gnu_scalar_type;
4942 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4943 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4945 if (kind == E_Enumeration_Type)
4947 /* Enumeration types have specific RM bounds. */
4948 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4949 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4951 /* Write full debugging information. Since this has both a
4952 typedef and a tag, avoid outputting the name twice. */
4953 DECL_ARTIFICIAL (gnu_decl) = 1;
4954 rest_of_type_decl_compilation (gnu_decl);
4959 /* Floating-point types don't have specific RM bounds. */
4960 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4961 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4965 /* If we deferred processing of incomplete types, re-enable it. If there
4966 were no other disables and we have some to process, do so. */
4967 if (this_deferred && --defer_incomplete_level == 0)
4969 if (defer_incomplete_list)
4971 struct incomplete *incp, *next;
4973 /* We are back to level 0 for the deferring of incomplete types.
4974 But processing these incomplete types below may itself require
4975 deferring, so preserve what we have and restart from scratch. */
4976 incp = defer_incomplete_list;
4977 defer_incomplete_list = NULL;
4979 /* For finalization, however, all types must be complete so we
4980 cannot do the same because deferred incomplete types may end up
4981 referencing each other. Process them all recursively first. */
4982 defer_finalize_level++;
4984 for (; incp; incp = next)
4989 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4990 gnat_to_gnu_type (incp->full_type));
4994 defer_finalize_level--;
4997 /* All the deferred incomplete types have been processed so we can
4998 now proceed with the finalization of the deferred types. */
4999 if (defer_finalize_level == 0 && defer_finalize_list)
5004 FOR_EACH_VEC_ELT (tree, defer_finalize_list, i, t)
5005 rest_of_type_decl_compilation_no_defer (t);
5007 VEC_free (tree, heap, defer_finalize_list);
5011 /* If we are not defining this type, see if it's in the incomplete list.
5012 If so, handle that list entry now. */
5013 else if (!definition)
5015 struct incomplete *incp;
5017 for (incp = defer_incomplete_list; incp; incp = incp->next)
5018 if (incp->old_type && incp->full_type == gnat_entity)
5020 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5021 TREE_TYPE (gnu_decl));
5022 incp->old_type = NULL_TREE;
5029 /* If this is a packed array type whose original array type is itself
5030 an Itype without freeze node, make sure the latter is processed. */
5031 if (Is_Packed_Array_Type (gnat_entity)
5032 && Is_Itype (Original_Array_Type (gnat_entity))
5033 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5034 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5035 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5040 /* Similar, but if the returned value is a COMPONENT_REF, return the
5044 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5046 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5048 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5049 gnu_field = TREE_OPERAND (gnu_field, 1);
5054 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5055 the GCC type corresponding to that entity. */
5058 gnat_to_gnu_type (Entity_Id gnat_entity)
5062 /* The back end never attempts to annotate generic types. */
5063 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5064 return void_type_node;
5066 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5067 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5069 return TREE_TYPE (gnu_decl);
5072 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5073 the unpadded version of the GCC type corresponding to that entity. */
5076 get_unpadded_type (Entity_Id gnat_entity)
5078 tree type = gnat_to_gnu_type (gnat_entity);
5080 if (TYPE_IS_PADDING_P (type))
5081 type = TREE_TYPE (TYPE_FIELDS (type));
5086 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
5087 Every TYPE_DECL generated for a type definition must be passed
5088 to this function once everything else has been done for it. */
5091 rest_of_type_decl_compilation (tree decl)
5093 /* We need to defer finalizing the type if incomplete types
5094 are being deferred or if they are being processed. */
5095 if (defer_incomplete_level != 0 || defer_finalize_level != 0)
5096 VEC_safe_push (tree, heap, defer_finalize_list, decl);
5098 rest_of_type_decl_compilation_no_defer (decl);
5101 /* Same as above but without deferring the compilation. This
5102 function should not be invoked directly on a TYPE_DECL. */
5105 rest_of_type_decl_compilation_no_defer (tree decl)
5107 const int toplev = global_bindings_p ();
5108 tree t = TREE_TYPE (decl);
5110 rest_of_decl_compilation (decl, toplev, 0);
5112 /* Now process all the variants. This is needed for STABS. */
5113 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5115 if (t == TREE_TYPE (decl))
5118 if (!TYPE_STUB_DECL (t))
5119 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5121 rest_of_type_compilation (t, toplev);
5125 /* Given a record type RECORD_TYPE and a list of FIELD_DECL nodes FIELD_LIST,
5126 finish constructing the record type as a fat pointer type. */
5129 finish_fat_pointer_type (tree record_type, tree field_list)
5131 /* Make sure we can put it into a register. */
5132 TYPE_ALIGN (record_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
5134 /* Show what it really is. */
5135 TYPE_FAT_POINTER_P (record_type) = 1;
5137 /* Do not emit debug info for it since the types of its fields may still be
5138 incomplete at this point. */
5139 finish_record_type (record_type, field_list, 0, false);
5141 /* Force type_contains_placeholder_p to return true on it. Although the
5142 PLACEHOLDER_EXPRs are referenced only indirectly, this isn't a pointer
5143 type but the representation of the unconstrained array. */
5144 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (record_type) = 2;
5147 /* Finalize any From_With_Type incomplete types. We do this after processing
5148 our compilation unit and after processing its spec, if this is a body. */
5151 finalize_from_with_types (void)
5153 struct incomplete *incp = defer_limited_with;
5154 struct incomplete *next;
5156 defer_limited_with = 0;
5157 for (; incp; incp = next)
5161 if (incp->old_type != 0)
5162 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5163 gnat_to_gnu_type (incp->full_type));
5168 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5169 kind of type (such E_Task_Type) that has a different type which Gigi
5170 uses for its representation. If the type does not have a special type
5171 for its representation, return GNAT_ENTITY. If a type is supposed to
5172 exist, but does not, abort unless annotating types, in which case
5173 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5176 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5178 Entity_Id gnat_equiv = gnat_entity;
5180 if (No (gnat_entity))
5183 switch (Ekind (gnat_entity))
5185 case E_Class_Wide_Subtype:
5186 if (Present (Equivalent_Type (gnat_entity)))
5187 gnat_equiv = Equivalent_Type (gnat_entity);
5190 case E_Access_Protected_Subprogram_Type:
5191 case E_Anonymous_Access_Protected_Subprogram_Type:
5192 gnat_equiv = Equivalent_Type (gnat_entity);
5195 case E_Class_Wide_Type:
5196 gnat_equiv = Root_Type (gnat_entity);
5200 case E_Task_Subtype:
5201 case E_Protected_Type:
5202 case E_Protected_Subtype:
5203 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5210 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5214 /* Return a GCC tree for a type corresponding to the component type of the
5215 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5216 is for an array being defined. DEBUG_INFO_P is true if we need to write
5217 debug information for other types that we may create in the process. */
5220 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5223 tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
5226 /* Try to get a smaller form of the component if needed. */
5227 if ((Is_Packed (gnat_array)
5228 || Has_Component_Size_Clause (gnat_array))
5229 && !Is_Bit_Packed_Array (gnat_array)
5230 && !Has_Aliased_Components (gnat_array)
5231 && !Strict_Alignment (Component_Type (gnat_array))
5232 && TREE_CODE (gnu_type) == RECORD_TYPE
5233 && !TYPE_FAT_POINTER_P (gnu_type)
5234 && host_integerp (TYPE_SIZE (gnu_type), 1))
5235 gnu_type = make_packable_type (gnu_type, false);
5237 if (Has_Atomic_Components (gnat_array))
5238 check_ok_for_atomic (gnu_type, gnat_array, true);
5240 /* Get and validate any specified Component_Size. */
5242 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5243 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5244 true, Has_Component_Size_Clause (gnat_array));
5246 /* If the array has aliased components and the component size can be zero,
5247 force at least unit size to ensure that the components have distinct
5250 && Has_Aliased_Components (gnat_array)
5251 && (integer_zerop (TYPE_SIZE (gnu_type))
5252 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5253 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5255 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5257 /* If the component type is a RECORD_TYPE that has a self-referential size,
5258 then use the maximum size for the component size. */
5260 && TREE_CODE (gnu_type) == RECORD_TYPE
5261 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5262 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5264 /* Honor the component size. This is not needed for bit-packed arrays. */
5265 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5267 tree orig_type = gnu_type;
5268 unsigned int max_align;
5270 /* If an alignment is specified, use it as a cap on the component type
5271 so that it can be honored for the whole type. But ignore it for the
5272 original type of packed array types. */
5273 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5274 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5278 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5279 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5280 gnu_type = orig_type;
5282 orig_type = gnu_type;
5284 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5285 true, false, definition, true);
5287 /* If a padding record was made, declare it now since it will never be
5288 declared otherwise. This is necessary to ensure that its subtrees
5289 are properly marked. */
5290 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5291 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5292 debug_info_p, gnat_array);
5295 if (Has_Volatile_Components (Base_Type (gnat_array)))
5297 = build_qualified_type (gnu_type,
5298 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5303 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5304 using MECH as its passing mechanism, to be placed in the parameter
5305 list built for GNAT_SUBPROG. Assume a foreign convention for the
5306 latter if FOREIGN is true. Also set CICO to true if the parameter
5307 must use the copy-in copy-out implementation mechanism.
5309 The returned tree is a PARM_DECL, except for those cases where no
5310 parameter needs to be actually passed to the subprogram; the type
5311 of this "shadow" parameter is then returned instead. */
5314 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5315 Entity_Id gnat_subprog, bool foreign, bool *cico)
5317 tree gnu_param_name = get_entity_name (gnat_param);
5318 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5319 tree gnu_param_type_alt = NULL_TREE;
5320 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5321 /* The parameter can be indirectly modified if its address is taken. */
5322 bool ro_param = in_param && !Address_Taken (gnat_param);
5323 bool by_return = false, by_component_ptr = false;
5324 bool by_ref = false, by_double_ref = false;
5327 /* Copy-return is used only for the first parameter of a valued procedure.
5328 It's a copy mechanism for which a parameter is never allocated. */
5329 if (mech == By_Copy_Return)
5331 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5336 /* If this is either a foreign function or if the underlying type won't
5337 be passed by reference, strip off possible padding type. */
5338 if (TYPE_IS_PADDING_P (gnu_param_type))
5340 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5342 if (mech == By_Reference
5344 || (!must_pass_by_ref (unpadded_type)
5345 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5346 gnu_param_type = unpadded_type;
5349 /* If this is a read-only parameter, make a variant of the type that is
5350 read-only. ??? However, if this is an unconstrained array, that type
5351 can be very complex, so skip it for now. Likewise for any other
5352 self-referential type. */
5354 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5355 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5356 gnu_param_type = build_qualified_type (gnu_param_type,
5357 (TYPE_QUALS (gnu_param_type)
5358 | TYPE_QUAL_CONST));
5360 /* For foreign conventions, pass arrays as pointers to the element type.
5361 First check for unconstrained array and get the underlying array. */
5362 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5364 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5366 /* For GCC builtins, pass Address integer types as (void *) */
5367 if (Convention (gnat_subprog) == Convention_Intrinsic
5368 && Present (Interface_Name (gnat_subprog))
5369 && Is_Descendent_Of_Address (Etype (gnat_param)))
5370 gnu_param_type = ptr_void_type_node;
5372 /* VMS descriptors are themselves passed by reference. */
5373 if (mech == By_Short_Descriptor ||
5374 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5376 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5377 Mechanism (gnat_param),
5379 else if (mech == By_Descriptor)
5381 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5382 chosen in fill_vms_descriptor. */
5384 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5385 Mechanism (gnat_param),
5388 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5389 Mechanism (gnat_param),
5393 /* Arrays are passed as pointers to element type for foreign conventions. */
5396 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5398 /* Strip off any multi-dimensional entries, then strip
5399 off the last array to get the component type. */
5400 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5401 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5402 gnu_param_type = TREE_TYPE (gnu_param_type);
5404 by_component_ptr = true;
5405 gnu_param_type = TREE_TYPE (gnu_param_type);
5408 gnu_param_type = build_qualified_type (gnu_param_type,
5409 (TYPE_QUALS (gnu_param_type)
5410 | TYPE_QUAL_CONST));
5412 gnu_param_type = build_pointer_type (gnu_param_type);
5415 /* Fat pointers are passed as thin pointers for foreign conventions. */
5416 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5418 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5420 /* If we must pass or were requested to pass by reference, do so.
5421 If we were requested to pass by copy, do so.
5422 Otherwise, for foreign conventions, pass In Out or Out parameters
5423 or aggregates by reference. For COBOL and Fortran, pass all
5424 integer and FP types that way too. For Convention Ada, use
5425 the standard Ada default. */
5426 else if (must_pass_by_ref (gnu_param_type)
5427 || mech == By_Reference
5430 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5432 && (Convention (gnat_subprog) == Convention_Fortran
5433 || Convention (gnat_subprog) == Convention_COBOL)
5434 && (INTEGRAL_TYPE_P (gnu_param_type)
5435 || FLOAT_TYPE_P (gnu_param_type)))
5437 && default_pass_by_ref (gnu_param_type)))))
5439 gnu_param_type = build_reference_type (gnu_param_type);
5442 /* In some ABIs, e.g. SPARC 32-bit, fat pointer types are themselves
5443 passed by reference. Pass them by explicit reference, this will
5444 generate more debuggable code at -O0. */
5445 if (TYPE_IS_FAT_POINTER_P (gnu_param_type)
5446 && targetm.calls.pass_by_reference (NULL,
5447 TYPE_MODE (gnu_param_type),
5451 gnu_param_type = build_reference_type (gnu_param_type);
5452 by_double_ref = true;
5456 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5460 if (mech == By_Copy && (by_ref || by_component_ptr))
5461 post_error ("?cannot pass & by copy", gnat_param);
5463 /* If this is an Out parameter that isn't passed by reference and isn't
5464 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5465 it will be a VAR_DECL created when we process the procedure, so just
5466 return its type. For the special parameter of a valued procedure,
5469 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5470 Out parameters with discriminants or implicit initial values to be
5471 handled like In Out parameters. These type are normally built as
5472 aggregates, hence passed by reference, except for some packed arrays
5473 which end up encoded in special integer types.
5475 The exception we need to make is then for packed arrays of records
5476 with discriminants or implicit initial values. We have no light/easy
5477 way to check for the latter case, so we merely check for packed arrays
5478 of records. This may lead to useless copy-in operations, but in very
5479 rare cases only, as these would be exceptions in a set of already
5480 exceptional situations. */
5481 if (Ekind (gnat_param) == E_Out_Parameter
5484 || (mech != By_Descriptor
5485 && mech != By_Short_Descriptor
5486 && !POINTER_TYPE_P (gnu_param_type)
5487 && !AGGREGATE_TYPE_P (gnu_param_type)))
5488 && !(Is_Array_Type (Etype (gnat_param))
5489 && Is_Packed (Etype (gnat_param))
5490 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5491 return gnu_param_type;
5493 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5494 ro_param || by_ref || by_component_ptr);
5495 DECL_BY_REF_P (gnu_param) = by_ref;
5496 DECL_BY_DOUBLE_REF_P (gnu_param) = by_double_ref;
5497 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5498 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5499 mech == By_Short_Descriptor);
5500 DECL_POINTS_TO_READONLY_P (gnu_param)
5501 = (ro_param && (by_ref || by_component_ptr));
5503 /* Save the alternate descriptor type, if any. */
5504 if (gnu_param_type_alt)
5505 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5507 /* If no Mechanism was specified, indicate what we're using, then
5508 back-annotate it. */
5509 if (mech == Default)
5510 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5512 Set_Mechanism (gnat_param, mech);
5516 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5519 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5521 while (Present (Corresponding_Discriminant (discr1)))
5522 discr1 = Corresponding_Discriminant (discr1);
5524 while (Present (Corresponding_Discriminant (discr2)))
5525 discr2 = Corresponding_Discriminant (discr2);
5528 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5531 /* Return true if the array type GNU_TYPE, which represents a dimension of
5532 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5535 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5537 /* If the array type is not the innermost dimension of the GNAT type,
5538 then it has a non-aliased component. */
5539 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5540 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5543 /* If the array type has an aliased component in the front-end sense,
5544 then it also has an aliased component in the back-end sense. */
5545 if (Has_Aliased_Components (gnat_type))
5548 /* If this is a derived type, then it has a non-aliased component if
5549 and only if its parent type also has one. */
5550 if (Is_Derived_Type (gnat_type))
5552 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5554 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5556 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5557 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5558 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5559 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5562 /* Otherwise, rely exclusively on properties of the element type. */
5563 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5566 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5569 compile_time_known_address_p (Node_Id gnat_address)
5571 /* Catch System'To_Address. */
5572 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5573 gnat_address = Expression (gnat_address);
5575 return Compile_Time_Known_Value (gnat_address);
5578 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5579 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5582 cannot_be_superflat_p (Node_Id gnat_range)
5584 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5585 Node_Id scalar_range;
5586 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5588 /* If the low bound is not constant, try to find an upper bound. */
5589 while (Nkind (gnat_lb) != N_Integer_Literal
5590 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5591 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5592 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5593 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5594 || Nkind (scalar_range) == N_Range))
5595 gnat_lb = High_Bound (scalar_range);
5597 /* If the high bound is not constant, try to find a lower bound. */
5598 while (Nkind (gnat_hb) != N_Integer_Literal
5599 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5600 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5601 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5602 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5603 || Nkind (scalar_range) == N_Range))
5604 gnat_hb = Low_Bound (scalar_range);
5606 /* If we have failed to find constant bounds, punt. */
5607 if (Nkind (gnat_lb) != N_Integer_Literal
5608 || Nkind (gnat_hb) != N_Integer_Literal)
5611 /* We need at least a signed 64-bit type to catch most cases. */
5612 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5613 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5614 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5617 /* If the low bound is the smallest integer, nothing can be smaller. */
5618 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5619 if (TREE_OVERFLOW (gnu_lb_minus_one))
5622 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5625 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5628 constructor_address_p (tree gnu_expr)
5630 while (TREE_CODE (gnu_expr) == NOP_EXPR
5631 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5632 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5633 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5635 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5636 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5639 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5640 be elaborated at the point of its definition, but do nothing else. */
5643 elaborate_entity (Entity_Id gnat_entity)
5645 switch (Ekind (gnat_entity))
5647 case E_Signed_Integer_Subtype:
5648 case E_Modular_Integer_Subtype:
5649 case E_Enumeration_Subtype:
5650 case E_Ordinary_Fixed_Point_Subtype:
5651 case E_Decimal_Fixed_Point_Subtype:
5652 case E_Floating_Point_Subtype:
5654 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5655 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5657 /* ??? Tests to avoid Constraint_Error in static expressions
5658 are needed until after the front stops generating bogus
5659 conversions on bounds of real types. */
5660 if (!Raises_Constraint_Error (gnat_lb))
5661 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5662 true, false, Needs_Debug_Info (gnat_entity));
5663 if (!Raises_Constraint_Error (gnat_hb))
5664 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5665 true, false, Needs_Debug_Info (gnat_entity));
5671 Node_Id full_definition = Declaration_Node (gnat_entity);
5672 Node_Id record_definition = Type_Definition (full_definition);
5674 /* If this is a record extension, go a level further to find the
5675 record definition. */
5676 if (Nkind (record_definition) == N_Derived_Type_Definition)
5677 record_definition = Record_Extension_Part (record_definition);
5681 case E_Record_Subtype:
5682 case E_Private_Subtype:
5683 case E_Limited_Private_Subtype:
5684 case E_Record_Subtype_With_Private:
5685 if (Is_Constrained (gnat_entity)
5686 && Has_Discriminants (gnat_entity)
5687 && Present (Discriminant_Constraint (gnat_entity)))
5689 Node_Id gnat_discriminant_expr;
5690 Entity_Id gnat_field;
5693 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5694 gnat_discriminant_expr
5695 = First_Elmt (Discriminant_Constraint (gnat_entity));
5696 Present (gnat_field);
5697 gnat_field = Next_Discriminant (gnat_field),
5698 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5699 /* ??? For now, ignore access discriminants. */
5700 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5701 elaborate_expression (Node (gnat_discriminant_expr),
5702 gnat_entity, get_entity_name (gnat_field),
5703 true, false, false);
5710 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5711 any entities on its entity chain similarly. */
5714 mark_out_of_scope (Entity_Id gnat_entity)
5716 Entity_Id gnat_sub_entity;
5717 unsigned int kind = Ekind (gnat_entity);
5719 /* If this has an entity list, process all in the list. */
5720 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5721 || IN (kind, Private_Kind)
5722 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5723 || kind == E_Function || kind == E_Generic_Function
5724 || kind == E_Generic_Package || kind == E_Generic_Procedure
5725 || kind == E_Loop || kind == E_Operator || kind == E_Package
5726 || kind == E_Package_Body || kind == E_Procedure
5727 || kind == E_Record_Type || kind == E_Record_Subtype
5728 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5729 for (gnat_sub_entity = First_Entity (gnat_entity);
5730 Present (gnat_sub_entity);
5731 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5732 if (Scope (gnat_sub_entity) == gnat_entity
5733 && gnat_sub_entity != gnat_entity)
5734 mark_out_of_scope (gnat_sub_entity);
5736 /* Now clear this if it has been defined, but only do so if it isn't
5737 a subprogram or parameter. We could refine this, but it isn't
5738 worth it. If this is statically allocated, it is supposed to
5739 hang around out of cope. */
5740 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5741 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5743 save_gnu_tree (gnat_entity, NULL_TREE, true);
5744 save_gnu_tree (gnat_entity, error_mark_node, true);
5748 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5749 If this is a multi-dimensional array type, do this recursively.
5752 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5753 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5754 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5757 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5759 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5760 of a one-dimensional array, since the padding has the same alias set
5761 as the field type, but if it's a multi-dimensional array, we need to
5762 see the inner types. */
5763 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5764 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5765 || TYPE_PADDING_P (gnu_old_type)))
5766 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5768 /* Unconstrained array types are deemed incomplete and would thus be given
5769 alias set 0. Retrieve the underlying array type. */
5770 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5772 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5773 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5775 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5777 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5778 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5779 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5780 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5784 case ALIAS_SET_COPY:
5785 /* The alias set shouldn't be copied between array types with different
5786 aliasing settings because this can break the aliasing relationship
5787 between the array type and its element type. */
5788 #ifndef ENABLE_CHECKING
5789 if (flag_strict_aliasing)
5791 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5792 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5793 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5794 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5796 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5799 case ALIAS_SET_SUBSET:
5800 case ALIAS_SET_SUPERSET:
5802 alias_set_type old_set = get_alias_set (gnu_old_type);
5803 alias_set_type new_set = get_alias_set (gnu_new_type);
5805 /* Do nothing if the alias sets conflict. This ensures that we
5806 never call record_alias_subset several times for the same pair
5807 or at all for alias set 0. */
5808 if (!alias_sets_conflict_p (old_set, new_set))
5810 if (op == ALIAS_SET_SUBSET)
5811 record_alias_subset (old_set, new_set);
5813 record_alias_subset (new_set, old_set);
5822 record_component_aliases (gnu_new_type);
5825 /* Return true if the size represented by GNU_SIZE can be handled by an
5826 allocation. If STATIC_P is true, consider only what can be done with a
5827 static allocation. */
5830 allocatable_size_p (tree gnu_size, bool static_p)
5832 HOST_WIDE_INT our_size;
5834 /* If this is not a static allocation, the only case we want to forbid
5835 is an overflowing size. That will be converted into a raise a
5838 return !(TREE_CODE (gnu_size) == INTEGER_CST
5839 && TREE_OVERFLOW (gnu_size));
5841 /* Otherwise, we need to deal with both variable sizes and constant
5842 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5843 since assemblers may not like very large sizes. */
5844 if (!host_integerp (gnu_size, 1))
5847 our_size = tree_low_cst (gnu_size, 1);
5848 return (int) our_size == our_size;
5851 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5852 NAME, ARGS and ERROR_POINT. */
5855 prepend_one_attribute_to (struct attrib ** attr_list,
5856 enum attr_type attr_type,
5859 Node_Id attr_error_point)
5861 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5863 attr->type = attr_type;
5864 attr->name = attr_name;
5865 attr->args = attr_args;
5866 attr->error_point = attr_error_point;
5868 attr->next = *attr_list;
5872 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5875 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5879 /* Attributes are stored as Representation Item pragmas. */
5881 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5882 gnat_temp = Next_Rep_Item (gnat_temp))
5883 if (Nkind (gnat_temp) == N_Pragma)
5885 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5886 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5887 enum attr_type etype;
5889 /* Map the kind of pragma at hand. Skip if this is not one
5890 we know how to handle. */
5892 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5894 case Pragma_Machine_Attribute:
5895 etype = ATTR_MACHINE_ATTRIBUTE;
5898 case Pragma_Linker_Alias:
5899 etype = ATTR_LINK_ALIAS;
5902 case Pragma_Linker_Section:
5903 etype = ATTR_LINK_SECTION;
5906 case Pragma_Linker_Constructor:
5907 etype = ATTR_LINK_CONSTRUCTOR;
5910 case Pragma_Linker_Destructor:
5911 etype = ATTR_LINK_DESTRUCTOR;
5914 case Pragma_Weak_External:
5915 etype = ATTR_WEAK_EXTERNAL;
5918 case Pragma_Thread_Local_Storage:
5919 etype = ATTR_THREAD_LOCAL_STORAGE;
5926 /* See what arguments we have and turn them into GCC trees for
5927 attribute handlers. These expect identifier for strings. We
5928 handle at most two arguments, static expressions only. */
5930 if (Present (gnat_assoc) && Present (First (gnat_assoc)))
5932 Node_Id gnat_arg0 = Next (First (gnat_assoc));
5933 Node_Id gnat_arg1 = Empty;
5935 if (Present (gnat_arg0)
5936 && Is_Static_Expression (Expression (gnat_arg0)))
5938 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
5940 if (TREE_CODE (gnu_arg0) == STRING_CST)
5941 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
5943 gnat_arg1 = Next (gnat_arg0);
5946 if (Present (gnat_arg1)
5947 && Is_Static_Expression (Expression (gnat_arg1)))
5949 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
5951 if (TREE_CODE (gnu_arg1) == STRING_CST)
5952 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
5956 /* Prepend to the list now. Make a list of the argument we might
5957 have, as GCC expects it. */
5958 prepend_one_attribute_to
5961 (gnu_arg1 != NULL_TREE)
5962 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5963 Present (Next (First (gnat_assoc)))
5964 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5968 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5969 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5970 return the GCC tree to use for that expression. GNU_NAME is the suffix
5971 to use if a variable needs to be created and DEFINITION is true if this
5972 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
5973 otherwise, we are just elaborating the expression for side-effects. If
5974 NEED_DEBUG is true, we need a variable for debugging purposes even if it
5975 isn't needed for code generation. */
5978 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
5979 bool definition, bool need_value, bool need_debug)
5983 /* If we already elaborated this expression (e.g. it was involved
5984 in the definition of a private type), use the old value. */
5985 if (present_gnu_tree (gnat_expr))
5986 return get_gnu_tree (gnat_expr);
5988 /* If we don't need a value and this is static or a discriminant,
5989 we don't need to do anything. */
5991 && (Is_OK_Static_Expression (gnat_expr)
5992 || (Nkind (gnat_expr) == N_Identifier
5993 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5996 /* If it's a static expression, we don't need a variable for debugging. */
5997 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6000 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6001 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6002 gnu_name, definition, need_debug);
6004 /* Save the expression in case we try to elaborate this entity again. Since
6005 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6006 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6007 save_gnu_tree (gnat_expr, gnu_expr, true);
6009 return need_value ? gnu_expr : error_mark_node;
6012 /* Similar, but take a GNU expression and always return a result. */
6015 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6016 bool definition, bool need_debug)
6018 const bool expr_global_p = Is_Public (gnat_entity) || global_bindings_p ();
6019 bool expr_variable_p, use_variable;
6021 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6022 reference will have been replaced with a COMPONENT_REF when the type
6023 is being elaborated. However, there are some cases involving child
6024 types where we will. So convert it to a COMPONENT_REF. We hope it
6025 will be at the highest level of the expression in these cases. */
6026 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6027 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6028 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6029 gnu_expr, NULL_TREE);
6031 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6032 that an expression cannot contain both a discriminant and a variable. */
6033 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6036 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6037 a variable that is initialized to contain the expression when the package
6038 containing the definition is elaborated. If this entity is defined at top
6039 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6040 if this is necessary. */
6041 if (CONSTANT_CLASS_P (gnu_expr))
6042 expr_variable_p = false;
6045 /* Skip any conversions and simple arithmetics to see if the expression
6046 is based on a read-only variable.
6047 ??? This really should remain read-only, but we have to think about
6048 the typing of the tree here. */
6050 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
6052 if (handled_component_p (inner))
6054 HOST_WIDE_INT bitsize, bitpos;
6056 enum machine_mode mode;
6057 int unsignedp, volatilep;
6059 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6060 &mode, &unsignedp, &volatilep, false);
6061 /* If the offset is variable, err on the side of caution. */
6068 && TREE_CODE (inner) == VAR_DECL
6069 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6072 /* We only need to use the variable if we are in a global context since GCC
6073 can do the right thing in the local case. However, when not optimizing,
6074 use it for bounds of loop iteration scheme to avoid code duplication. */
6075 use_variable = expr_variable_p
6078 && Is_Itype (gnat_entity)
6079 && Nkind (Associated_Node_For_Itype (gnat_entity))
6080 == N_Loop_Parameter_Specification));
6082 /* Now create it, possibly only for debugging purposes. */
6083 if (use_variable || need_debug)
6086 = create_var_decl (create_concat_name (gnat_entity,
6087 IDENTIFIER_POINTER (gnu_name)),
6088 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
6089 !need_debug, Is_Public (gnat_entity),
6090 !definition, expr_global_p, NULL, gnat_entity);
6096 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6099 /* Similar, but take an alignment factor and make it explicit in the tree. */
6102 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6103 bool definition, bool need_debug, unsigned int align)
6105 tree unit_align = size_int (align / BITS_PER_UNIT);
6107 size_binop (MULT_EXPR,
6108 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6111 gnat_entity, gnu_name, definition,
6116 /* Create a record type that contains a SIZE bytes long field of TYPE with a
6117 starting bit position so that it is aligned to ALIGN bits, and leaving at
6118 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
6119 record is guaranteed to get. */
6122 make_aligning_type (tree type, unsigned int align, tree size,
6123 unsigned int base_align, int room)
6125 /* We will be crafting a record type with one field at a position set to be
6126 the next multiple of ALIGN past record'address + room bytes. We use a
6127 record placeholder to express record'address. */
6128 tree record_type = make_node (RECORD_TYPE);
6129 tree record = build0 (PLACEHOLDER_EXPR, record_type);
6132 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
6134 /* The diagram below summarizes the shape of what we manipulate:
6136 <--------- pos ---------->
6137 { +------------+-------------+-----------------+
6138 record =>{ |############| ... | field (type) |
6139 { +------------+-------------+-----------------+
6140 |<-- room -->|<- voffset ->|<---- size ----->|
6143 record_addr vblock_addr
6145 Every length is in sizetype bytes there, except "pos" which has to be
6146 set as a bit position in the GCC tree for the record. */
6147 tree room_st = size_int (room);
6148 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
6149 tree voffset_st, pos, field;
6151 tree name = TYPE_NAME (type);
6153 if (TREE_CODE (name) == TYPE_DECL)
6154 name = DECL_NAME (name);
6155 name = concat_name (name, "ALIGN");
6156 TYPE_NAME (record_type) = name;
6158 /* Compute VOFFSET and then POS. The next byte position multiple of some
6159 alignment after some address is obtained by "and"ing the alignment minus
6160 1 with the two's complement of the address. */
6161 voffset_st = size_binop (BIT_AND_EXPR,
6162 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
6163 size_int ((align / BITS_PER_UNIT) - 1));
6165 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
6166 pos = size_binop (MULT_EXPR,
6167 convert (bitsizetype,
6168 size_binop (PLUS_EXPR, room_st, voffset_st)),
6171 /* Craft the GCC record representation. We exceptionally do everything
6172 manually here because 1) our generic circuitry is not quite ready to
6173 handle the complex position/size expressions we are setting up, 2) we
6174 have a strong simplifying factor at hand: we know the maximum possible
6175 value of voffset, and 3) we have to set/reset at least the sizes in
6176 accordance with this maximum value anyway, as we need them to convey
6177 what should be "alloc"ated for this type.
6179 Use -1 as the 'addressable' indication for the field to prevent the
6180 creation of a bitfield. We don't need one, it would have damaging
6181 consequences on the alignment computation, and create_field_decl would
6182 make one without this special argument, for instance because of the
6183 complex position expression. */
6184 field = create_field_decl (get_identifier ("F"), type, record_type, size,
6186 TYPE_FIELDS (record_type) = field;
6188 TYPE_ALIGN (record_type) = base_align;
6189 TYPE_USER_ALIGN (record_type) = 1;
6191 TYPE_SIZE (record_type)
6192 = size_binop (PLUS_EXPR,
6193 size_binop (MULT_EXPR, convert (bitsizetype, size),
6195 bitsize_int (align + room * BITS_PER_UNIT));
6196 TYPE_SIZE_UNIT (record_type)
6197 = size_binop (PLUS_EXPR, size,
6198 size_int (room + align / BITS_PER_UNIT));
6200 SET_TYPE_MODE (record_type, BLKmode);
6201 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6203 /* Declare it now since it will never be declared otherwise. This is
6204 necessary to ensure that its subtrees are properly marked. */
6205 create_type_decl (name, record_type, NULL, true, false, Empty);
6210 /* Return the result of rounding T up to ALIGN. */
6212 static inline unsigned HOST_WIDE_INT
6213 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6221 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6222 as the field type of a packed record if IN_RECORD is true, or as the
6223 component type of a packed array if IN_RECORD is false. See if we can
6224 rewrite it either as a type that has a non-BLKmode, which we can pack
6225 tighter in the packed record case, or as a smaller type. If so, return
6226 the new type. If not, return the original type. */
6229 make_packable_type (tree type, bool in_record)
6231 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6232 unsigned HOST_WIDE_INT new_size;
6233 tree new_type, old_field, field_list = NULL_TREE;
6235 /* No point in doing anything if the size is zero. */
6239 new_type = make_node (TREE_CODE (type));
6241 /* Copy the name and flags from the old type to that of the new.
6242 Note that we rely on the pointer equality created here for
6243 TYPE_NAME to look through conversions in various places. */
6244 TYPE_NAME (new_type) = TYPE_NAME (type);
6245 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6246 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6247 if (TREE_CODE (type) == RECORD_TYPE)
6248 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6250 /* If we are in a record and have a small size, set the alignment to
6251 try for an integral mode. Otherwise set it to try for a smaller
6252 type with BLKmode. */
6253 if (in_record && size <= MAX_FIXED_MODE_SIZE)
6255 TYPE_ALIGN (new_type) = ceil_alignment (size);
6256 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6260 unsigned HOST_WIDE_INT align;
6262 /* Do not try to shrink the size if the RM size is not constant. */
6263 if (TYPE_CONTAINS_TEMPLATE_P (type)
6264 || !host_integerp (TYPE_ADA_SIZE (type), 1))
6267 /* Round the RM size up to a unit boundary to get the minimal size
6268 for a BLKmode record. Give up if it's already the size. */
6269 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6270 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6271 if (new_size == size)
6274 align = new_size & -new_size;
6275 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6278 TYPE_USER_ALIGN (new_type) = 1;
6280 /* Now copy the fields, keeping the position and size as we don't want
6281 to change the layout by propagating the packedness downwards. */
6282 for (old_field = TYPE_FIELDS (type); old_field;
6283 old_field = DECL_CHAIN (old_field))
6285 tree new_field_type = TREE_TYPE (old_field);
6286 tree new_field, new_size;
6288 if ((TREE_CODE (new_field_type) == RECORD_TYPE
6289 || TREE_CODE (new_field_type) == UNION_TYPE
6290 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6291 && !TYPE_FAT_POINTER_P (new_field_type)
6292 && host_integerp (TYPE_SIZE (new_field_type), 1))
6293 new_field_type = make_packable_type (new_field_type, true);
6295 /* However, for the last field in a not already packed record type
6296 that is of an aggregate type, we need to use the RM size in the
6297 packable version of the record type, see finish_record_type. */
6298 if (!DECL_CHAIN (old_field)
6299 && !TYPE_PACKED (type)
6300 && (TREE_CODE (new_field_type) == RECORD_TYPE
6301 || TREE_CODE (new_field_type) == UNION_TYPE
6302 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6303 && !TYPE_FAT_POINTER_P (new_field_type)
6304 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6305 && TYPE_ADA_SIZE (new_field_type))
6306 new_size = TYPE_ADA_SIZE (new_field_type);
6308 new_size = DECL_SIZE (old_field);
6311 = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6312 new_size, bit_position (old_field),
6314 !DECL_NONADDRESSABLE_P (old_field));
6316 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6317 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6318 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6319 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6321 DECL_CHAIN (new_field) = field_list;
6322 field_list = new_field;
6325 finish_record_type (new_type, nreverse (field_list), 2, false);
6326 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6328 /* If this is a padding record, we never want to make the size smaller
6329 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
6330 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6332 TYPE_SIZE (new_type) = TYPE_SIZE (type);
6333 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6338 TYPE_SIZE (new_type) = bitsize_int (new_size);
6339 TYPE_SIZE_UNIT (new_type)
6340 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6343 if (!TYPE_CONTAINS_TEMPLATE_P (type))
6344 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6346 compute_record_mode (new_type);
6348 /* Try harder to get a packable type if necessary, for example
6349 in case the record itself contains a BLKmode field. */
6350 if (in_record && TYPE_MODE (new_type) == BLKmode)
6351 SET_TYPE_MODE (new_type,
6352 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6354 /* If neither the mode nor the size has shrunk, return the old type. */
6355 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6361 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
6362 if needed. We have already verified that SIZE and TYPE are large enough.
6363 GNAT_ENTITY is used to name the resulting record and to issue a warning.
6364 IS_COMPONENT_TYPE is true if this is being done for the component type
6365 of an array. IS_USER_TYPE is true if we must complete the original type.
6366 DEFINITION is true if this type is being defined. SAME_RM_SIZE is true
6367 if the RM size of the resulting type is to be set to SIZE too; otherwise,
6368 it's set to the RM size of the original type. */
6371 maybe_pad_type (tree type, tree size, unsigned int align,
6372 Entity_Id gnat_entity, bool is_component_type,
6373 bool is_user_type, bool definition, bool same_rm_size)
6375 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6376 tree orig_size = TYPE_SIZE (type);
6379 /* If TYPE is a padded type, see if it agrees with any size and alignment
6380 we were given. If so, return the original type. Otherwise, strip
6381 off the padding, since we will either be returning the inner type
6382 or repadding it. If no size or alignment is specified, use that of
6383 the original padded type. */
6384 if (TYPE_IS_PADDING_P (type))
6387 || operand_equal_p (round_up (size,
6388 MAX (align, TYPE_ALIGN (type))),
6389 round_up (TYPE_SIZE (type),
6390 MAX (align, TYPE_ALIGN (type))),
6392 && (align == 0 || align == TYPE_ALIGN (type)))
6396 size = TYPE_SIZE (type);
6398 align = TYPE_ALIGN (type);
6400 type = TREE_TYPE (TYPE_FIELDS (type));
6401 orig_size = TYPE_SIZE (type);
6404 /* If the size is either not being changed or is being made smaller (which
6405 is not done here and is only valid for bitfields anyway), show the size
6406 isn't changing. Likewise, clear the alignment if it isn't being
6407 changed. Then return if we aren't doing anything. */
6409 && (operand_equal_p (size, orig_size, 0)
6410 || (TREE_CODE (orig_size) == INTEGER_CST
6411 && tree_int_cst_lt (size, orig_size))))
6414 if (align == TYPE_ALIGN (type))
6417 if (align == 0 && !size)
6420 /* If requested, complete the original type and give it a name. */
6422 create_type_decl (get_entity_name (gnat_entity), type,
6423 NULL, !Comes_From_Source (gnat_entity),
6425 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6426 && DECL_IGNORED_P (TYPE_NAME (type))),
6429 /* We used to modify the record in place in some cases, but that could
6430 generate incorrect debugging information. So make a new record
6432 record = make_node (RECORD_TYPE);
6433 TYPE_PADDING_P (record) = 1;
6435 if (Present (gnat_entity))
6436 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6438 TYPE_VOLATILE (record)
6439 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6441 TYPE_ALIGN (record) = align;
6442 TYPE_SIZE (record) = size ? size : orig_size;
6443 TYPE_SIZE_UNIT (record)
6444 = convert (sizetype,
6445 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6446 bitsize_unit_node));
6448 /* If we are changing the alignment and the input type is a record with
6449 BLKmode and a small constant size, try to make a form that has an
6450 integral mode. This might allow the padding record to also have an
6451 integral mode, which will be much more efficient. There is no point
6452 in doing so if a size is specified unless it is also a small constant
6453 size and it is incorrect to do so if we cannot guarantee that the mode
6454 will be naturally aligned since the field must always be addressable.
6456 ??? This might not always be a win when done for a stand-alone object:
6457 since the nominal and the effective type of the object will now have
6458 different modes, a VIEW_CONVERT_EXPR will be required for converting
6459 between them and it might be hard to overcome afterwards, including
6460 at the RTL level when the stand-alone object is accessed as a whole. */
6462 && TREE_CODE (type) == RECORD_TYPE
6463 && TYPE_MODE (type) == BLKmode
6464 && TREE_CODE (orig_size) == INTEGER_CST
6465 && !TREE_OVERFLOW (orig_size)
6466 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6468 || (TREE_CODE (size) == INTEGER_CST
6469 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6471 tree packable_type = make_packable_type (type, true);
6472 if (TYPE_MODE (packable_type) != BLKmode
6473 && align >= TYPE_ALIGN (packable_type))
6474 type = packable_type;
6477 /* Now create the field with the original size. */
6478 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
6479 bitsize_zero_node, 0, 1);
6480 DECL_INTERNAL_P (field) = 1;
6482 /* Do not emit debug info until after the auxiliary record is built. */
6483 finish_record_type (record, field, 1, false);
6485 /* Set the same size for its RM size if requested; otherwise reuse
6486 the RM size of the original type. */
6487 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6489 /* Unless debugging information isn't being written for the input type,
6490 write a record that shows what we are a subtype of and also make a
6491 variable that indicates our size, if still variable. */
6492 if (TREE_CODE (orig_size) != INTEGER_CST
6493 && TYPE_NAME (record)
6495 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6496 && DECL_IGNORED_P (TYPE_NAME (type))))
6498 tree marker = make_node (RECORD_TYPE);
6499 tree name = TYPE_NAME (record);
6500 tree orig_name = TYPE_NAME (type);
6502 if (TREE_CODE (name) == TYPE_DECL)
6503 name = DECL_NAME (name);
6505 if (TREE_CODE (orig_name) == TYPE_DECL)
6506 orig_name = DECL_NAME (orig_name);
6508 TYPE_NAME (marker) = concat_name (name, "XVS");
6509 finish_record_type (marker,
6510 create_field_decl (orig_name,
6511 build_reference_type (type),
6512 marker, NULL_TREE, NULL_TREE,
6516 add_parallel_type (TYPE_STUB_DECL (record), marker);
6518 if (definition && size && TREE_CODE (size) != INTEGER_CST)
6519 TYPE_SIZE_UNIT (marker)
6520 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6521 TYPE_SIZE_UNIT (record), false, false, false,
6522 false, NULL, gnat_entity);
6525 rest_of_record_type_compilation (record);
6527 /* If the size was widened explicitly, maybe give a warning. Take the
6528 original size as the maximum size of the input if there was an
6529 unconstrained record involved and round it up to the specified alignment,
6530 if one was specified. */
6531 if (CONTAINS_PLACEHOLDER_P (orig_size))
6532 orig_size = max_size (orig_size, true);
6535 orig_size = round_up (orig_size, align);
6537 if (Present (gnat_entity)
6539 && TREE_CODE (size) != MAX_EXPR
6540 && TREE_CODE (size) != COND_EXPR
6541 && !operand_equal_p (size, orig_size, 0)
6542 && !(TREE_CODE (size) == INTEGER_CST
6543 && TREE_CODE (orig_size) == INTEGER_CST
6544 && (TREE_OVERFLOW (size)
6545 || TREE_OVERFLOW (orig_size)
6546 || tree_int_cst_lt (size, orig_size))))
6548 Node_Id gnat_error_node = Empty;
6550 if (Is_Packed_Array_Type (gnat_entity))
6551 gnat_entity = Original_Array_Type (gnat_entity);
6553 if ((Ekind (gnat_entity) == E_Component
6554 || Ekind (gnat_entity) == E_Discriminant)
6555 && Present (Component_Clause (gnat_entity)))
6556 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6557 else if (Present (Size_Clause (gnat_entity)))
6558 gnat_error_node = Expression (Size_Clause (gnat_entity));
6560 /* Generate message only for entities that come from source, since
6561 if we have an entity created by expansion, the message will be
6562 generated for some other corresponding source entity. */
6563 if (Comes_From_Source (gnat_entity))
6565 if (Present (gnat_error_node))
6566 post_error_ne_tree ("{^ }bits of & unused?",
6567 gnat_error_node, gnat_entity,
6568 size_diffop (size, orig_size));
6569 else if (is_component_type)
6570 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6571 gnat_entity, gnat_entity,
6572 size_diffop (size, orig_size));
6579 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6580 the value passed against the list of choices. */
6583 choices_to_gnu (tree operand, Node_Id choices)
6587 tree result = integer_zero_node;
6588 tree this_test, low = 0, high = 0, single = 0;
6590 for (choice = First (choices); Present (choice); choice = Next (choice))
6592 switch (Nkind (choice))
6595 low = gnat_to_gnu (Low_Bound (choice));
6596 high = gnat_to_gnu (High_Bound (choice));
6599 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6600 build_binary_op (GE_EXPR, boolean_type_node,
6602 build_binary_op (LE_EXPR, boolean_type_node,
6607 case N_Subtype_Indication:
6608 gnat_temp = Range_Expression (Constraint (choice));
6609 low = gnat_to_gnu (Low_Bound (gnat_temp));
6610 high = gnat_to_gnu (High_Bound (gnat_temp));
6613 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6614 build_binary_op (GE_EXPR, boolean_type_node,
6616 build_binary_op (LE_EXPR, boolean_type_node,
6621 case N_Expanded_Name:
6622 /* This represents either a subtype range, an enumeration
6623 literal, or a constant Ekind says which. If an enumeration
6624 literal or constant, fall through to the next case. */
6625 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6626 && Ekind (Entity (choice)) != E_Constant)
6628 tree type = gnat_to_gnu_type (Entity (choice));
6630 low = TYPE_MIN_VALUE (type);
6631 high = TYPE_MAX_VALUE (type);
6634 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6635 build_binary_op (GE_EXPR, boolean_type_node,
6637 build_binary_op (LE_EXPR, boolean_type_node,
6642 /* ... fall through ... */
6644 case N_Character_Literal:
6645 case N_Integer_Literal:
6646 single = gnat_to_gnu (choice);
6647 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6651 case N_Others_Choice:
6652 this_test = integer_one_node;
6659 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6666 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6667 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6670 adjust_packed (tree field_type, tree record_type, int packed)
6672 /* If the field contains an item of variable size, we cannot pack it
6673 because we cannot create temporaries of non-fixed size in case
6674 we need to take the address of the field. See addressable_p and
6675 the notes on the addressability issues for further details. */
6676 if (is_variable_size (field_type))
6679 /* If the alignment of the record is specified and the field type
6680 is over-aligned, request Storage_Unit alignment for the field. */
6683 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6692 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6693 placed in GNU_RECORD_TYPE.
6695 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6696 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6697 record has a specified alignment.
6699 DEFINITION is true if this field is for a record being defined.
6701 DEBUG_INFO_P is true if we need to write debug information for types
6702 that we may create in the process. */
6705 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6706 bool definition, bool debug_info_p)
6708 tree gnu_field_id = get_entity_name (gnat_field);
6709 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6710 tree gnu_field, gnu_size, gnu_pos;
6711 bool needs_strict_alignment
6712 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6713 || Treat_As_Volatile (gnat_field));
6715 /* If this field requires strict alignment, we cannot pack it because
6716 it would very likely be under-aligned in the record. */
6717 if (needs_strict_alignment)
6720 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6722 /* If a size is specified, use it. Otherwise, if the record type is packed,
6723 use the official RM size. See "Handling of Type'Size Values" in Einfo
6724 for further details. */
6725 if (Known_Static_Esize (gnat_field))
6726 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6727 gnat_field, FIELD_DECL, false, true);
6728 else if (packed == 1)
6729 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6730 gnat_field, FIELD_DECL, false, true);
6732 gnu_size = NULL_TREE;
6734 /* If we have a specified size that is smaller than that of the field's type,
6735 or a position is specified, and the field's type is a record that doesn't
6736 require strict alignment, see if we can get either an integral mode form
6737 of the type or a smaller form. If we can, show a size was specified for
6738 the field if there wasn't one already, so we know to make this a bitfield
6739 and avoid making things wider.
6741 Changing to an integral mode form is useful when the record is packed as
6742 we can then place the field at a non-byte-aligned position and so achieve
6743 tighter packing. This is in addition required if the field shares a byte
6744 with another field and the front-end lets the back-end handle the access
6745 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6747 Changing to a smaller form is required if the specified size is smaller
6748 than that of the field's type and the type contains sub-fields that are
6749 padded, in order to avoid generating accesses to these sub-fields that
6750 are wider than the field.
6752 We avoid the transformation if it is not required or potentially useful,
6753 as it might entail an increase of the field's alignment and have ripple
6754 effects on the outer record type. A typical case is a field known to be
6755 byte-aligned and not to share a byte with another field. */
6756 if (!needs_strict_alignment
6757 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6758 && !TYPE_FAT_POINTER_P (gnu_field_type)
6759 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6762 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6763 || (Present (Component_Clause (gnat_field))
6764 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6765 % BITS_PER_UNIT == 0
6766 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6768 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6769 if (gnu_packable_type != gnu_field_type)
6771 gnu_field_type = gnu_packable_type;
6773 gnu_size = rm_size (gnu_field_type);
6777 /* If we are packing the record and the field is BLKmode, round the
6778 size up to a byte boundary. */
6779 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6780 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6782 if (Present (Component_Clause (gnat_field)))
6784 Entity_Id gnat_parent
6785 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6787 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6788 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6789 gnat_field, FIELD_DECL, false, true);
6791 /* Ensure the position does not overlap with the parent subtype, if there
6792 is one. This test is omitted if the parent of the tagged type has a
6793 full rep clause since, in this case, component clauses are allowed to
6794 overlay the space allocated for the parent type and the front-end has
6795 checked that there are no overlapping components. */
6796 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6798 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6800 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6801 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6804 ("offset of& must be beyond parent{, minimum allowed is ^}",
6805 First_Bit (Component_Clause (gnat_field)), gnat_field,
6806 TYPE_SIZE_UNIT (gnu_parent));
6810 /* If this field needs strict alignment, ensure the record is
6811 sufficiently aligned and that that position and size are
6812 consistent with the alignment. */
6813 if (needs_strict_alignment)
6815 TYPE_ALIGN (gnu_record_type)
6816 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6819 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6821 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6823 ("atomic field& must be natural size of type{ (^)}",
6824 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6825 TYPE_SIZE (gnu_field_type));
6827 else if (Is_Aliased (gnat_field))
6829 ("size of aliased field& must be ^ bits",
6830 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6831 TYPE_SIZE (gnu_field_type));
6833 else if (Strict_Alignment (Etype (gnat_field)))
6835 ("size of & with aliased or tagged components not ^ bits",
6836 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6837 TYPE_SIZE (gnu_field_type));
6839 gnu_size = NULL_TREE;
6842 if (!integer_zerop (size_binop
6843 (TRUNC_MOD_EXPR, gnu_pos,
6844 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6846 if (Is_Aliased (gnat_field))
6848 ("position of aliased field& must be multiple of ^ bits",
6849 First_Bit (Component_Clause (gnat_field)), gnat_field,
6850 TYPE_ALIGN (gnu_field_type));
6852 else if (Treat_As_Volatile (gnat_field))
6854 ("position of volatile field& must be multiple of ^ bits",
6855 First_Bit (Component_Clause (gnat_field)), gnat_field,
6856 TYPE_ALIGN (gnu_field_type));
6858 else if (Strict_Alignment (Etype (gnat_field)))
6860 ("position of & with aliased or tagged components not multiple of ^ bits",
6861 First_Bit (Component_Clause (gnat_field)), gnat_field,
6862 TYPE_ALIGN (gnu_field_type));
6867 gnu_pos = NULL_TREE;
6871 if (Is_Atomic (gnat_field))
6872 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6875 /* If the record has rep clauses and this is the tag field, make a rep
6876 clause for it as well. */
6877 else if (Has_Specified_Layout (Scope (gnat_field))
6878 && Chars (gnat_field) == Name_uTag)
6880 gnu_pos = bitsize_zero_node;
6881 gnu_size = TYPE_SIZE (gnu_field_type);
6885 gnu_pos = NULL_TREE;
6887 /* We need to make the size the maximum for the type if it is
6888 self-referential and an unconstrained type. In that case, we can't
6889 pack the field since we can't make a copy to align it. */
6890 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6892 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6893 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6895 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6899 /* If a size is specified, adjust the field's type to it. */
6902 tree orig_field_type;
6904 /* If the field's type is justified modular, we would need to remove
6905 the wrapper to (better) meet the layout requirements. However we
6906 can do so only if the field is not aliased to preserve the unique
6907 layout and if the prescribed size is not greater than that of the
6908 packed array to preserve the justification. */
6909 if (!needs_strict_alignment
6910 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6911 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6912 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6914 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6917 = make_type_from_size (gnu_field_type, gnu_size,
6918 Has_Biased_Representation (gnat_field));
6920 orig_field_type = gnu_field_type;
6921 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6922 false, false, definition, true);
6924 /* If a padding record was made, declare it now since it will never be
6925 declared otherwise. This is necessary to ensure that its subtrees
6926 are properly marked. */
6927 if (gnu_field_type != orig_field_type
6928 && !DECL_P (TYPE_NAME (gnu_field_type)))
6929 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6930 true, debug_info_p, gnat_field);
6933 /* Otherwise (or if there was an error), don't specify a position. */
6935 gnu_pos = NULL_TREE;
6937 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6938 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6940 /* Now create the decl for the field. */
6942 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6943 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6944 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6945 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6947 if (Ekind (gnat_field) == E_Discriminant)
6948 DECL_DISCRIMINANT_NUMBER (gnu_field)
6949 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6954 /* Return true if TYPE is a type with variable size, a padding type with a
6955 field of variable size or is a record that has a field such a field. */
6958 is_variable_size (tree type)
6962 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6965 if (TYPE_IS_PADDING_P (type)
6966 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6969 if (TREE_CODE (type) != RECORD_TYPE
6970 && TREE_CODE (type) != UNION_TYPE
6971 && TREE_CODE (type) != QUAL_UNION_TYPE)
6974 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6975 if (is_variable_size (TREE_TYPE (field)))
6981 /* qsort comparer for the bit positions of two record components. */
6984 compare_field_bitpos (const PTR rt1, const PTR rt2)
6986 const_tree const field1 = * (const_tree const *) rt1;
6987 const_tree const field2 = * (const_tree const *) rt2;
6989 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6991 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6994 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6995 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6996 called from gnat_to_gnu_entity during the processing of a record type
6997 definition, the GCC node for the parent, if any, will be the single field
6998 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6999 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7000 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7002 PACKED is 1 if this is for a packed record, -1 if this is for a record
7003 with Component_Alignment of Storage_Unit, -2 if this is for a record
7004 with a specified alignment.
7006 DEFINITION is true if we are defining this record type.
7008 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7009 out the record. This means the alignment only serves to force fields to
7010 be bitfields, but not to require the record to be that aligned. This is
7013 ALL_REP is true if a rep clause is present for all the fields.
7015 UNCHECKED_UNION is true if we are building this type for a record with a
7016 Pragma Unchecked_Union.
7018 DEBUG_INFO is true if we need to write debug information about the type.
7020 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7021 mean that its contents may be unused as well, only the container itself.
7023 REORDER is true if we are permitted to reorder components of this type.
7025 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7026 with a rep clause is to be added; in this case, that is all that should
7027 be done with such fields. */
7030 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7031 tree gnu_field_list, int packed, bool definition,
7032 bool cancel_alignment, bool all_rep,
7033 bool unchecked_union, bool debug_info,
7034 bool maybe_unused, bool reorder,
7035 tree *p_gnu_rep_list)
7037 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7038 bool layout_with_rep = false;
7039 Node_Id component_decl, variant_part;
7040 tree gnu_field, gnu_next, gnu_last;
7041 tree gnu_variant_part = NULL_TREE;
7042 tree gnu_rep_list = NULL_TREE;
7043 tree gnu_var_list = NULL_TREE;
7044 tree gnu_self_list = NULL_TREE;
7046 /* For each component referenced in a component declaration create a GCC
7047 field and add it to the list, skipping pragmas in the GNAT list. */
7048 gnu_last = tree_last (gnu_field_list);
7049 if (Present (Component_Items (gnat_component_list)))
7051 = First_Non_Pragma (Component_Items (gnat_component_list));
7052 Present (component_decl);
7053 component_decl = Next_Non_Pragma (component_decl))
7055 Entity_Id gnat_field = Defining_Entity (component_decl);
7056 Name_Id gnat_name = Chars (gnat_field);
7058 /* If present, the _Parent field must have been created as the single
7059 field of the record type. Put it before any other fields. */
7060 if (gnat_name == Name_uParent)
7062 gnu_field = TYPE_FIELDS (gnu_record_type);
7063 gnu_field_list = chainon (gnu_field_list, gnu_field);
7067 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7068 definition, debug_info);
7070 /* If this is the _Tag field, put it before any other fields. */
7071 if (gnat_name == Name_uTag)
7072 gnu_field_list = chainon (gnu_field_list, gnu_field);
7074 /* If this is the _Controller field, put it before the other
7075 fields except for the _Tag or _Parent field. */
7076 else if (gnat_name == Name_uController && gnu_last)
7078 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7079 DECL_CHAIN (gnu_last) = gnu_field;
7082 /* If this is a regular field, put it after the other fields. */
7085 DECL_CHAIN (gnu_field) = gnu_field_list;
7086 gnu_field_list = gnu_field;
7088 gnu_last = gnu_field;
7092 save_gnu_tree (gnat_field, gnu_field, false);
7095 /* At the end of the component list there may be a variant part. */
7096 variant_part = Variant_Part (gnat_component_list);
7098 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7099 mutually exclusive and should go in the same memory. To do this we need
7100 to treat each variant as a record whose elements are created from the
7101 component list for the variant. So here we create the records from the
7102 lists for the variants and put them all into the QUAL_UNION_TYPE.
7103 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7104 use GNU_RECORD_TYPE if there are no fields so far. */
7105 if (Present (variant_part))
7107 Node_Id gnat_discr = Name (variant_part), variant;
7108 tree gnu_discr = gnat_to_gnu (gnat_discr);
7109 tree gnu_name = TYPE_NAME (gnu_record_type);
7111 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7113 tree gnu_union_type, gnu_union_name;
7114 tree gnu_variant_list = NULL_TREE;
7116 if (TREE_CODE (gnu_name) == TYPE_DECL)
7117 gnu_name = DECL_NAME (gnu_name);
7120 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7122 /* Reuse an enclosing union if all fields are in the variant part
7123 and there is no representation clause on the record, to match
7124 the layout of C unions. There is an associated check below. */
7126 && TREE_CODE (gnu_record_type) == UNION_TYPE
7127 && !TYPE_PACKED (gnu_record_type))
7128 gnu_union_type = gnu_record_type;
7132 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7134 TYPE_NAME (gnu_union_type) = gnu_union_name;
7135 TYPE_ALIGN (gnu_union_type) = 0;
7136 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7139 for (variant = First_Non_Pragma (Variants (variant_part));
7141 variant = Next_Non_Pragma (variant))
7143 tree gnu_variant_type = make_node (RECORD_TYPE);
7144 tree gnu_inner_name;
7147 Get_Variant_Encoding (variant);
7148 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7149 TYPE_NAME (gnu_variant_type)
7150 = concat_name (gnu_union_name,
7151 IDENTIFIER_POINTER (gnu_inner_name));
7153 /* Set the alignment of the inner type in case we need to make
7154 inner objects into bitfields, but then clear it out so the
7155 record actually gets only the alignment required. */
7156 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7157 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7159 /* Similarly, if the outer record has a size specified and all
7160 fields have record rep clauses, we can propagate the size
7161 into the variant part. */
7162 if (all_rep_and_size)
7164 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7165 TYPE_SIZE_UNIT (gnu_variant_type)
7166 = TYPE_SIZE_UNIT (gnu_record_type);
7169 /* Add the fields into the record type for the variant. Note that
7170 we aren't sure to really use it at this point, see below. */
7171 components_to_record (gnu_variant_type, Component_List (variant),
7172 NULL_TREE, packed, definition,
7173 !all_rep_and_size, all_rep,
7174 unchecked_union, debug_info,
7175 true, reorder, &gnu_rep_list);
7177 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7179 Set_Present_Expr (variant, annotate_value (gnu_qual));
7181 /* If this is an Unchecked_Union and we have exactly one field,
7182 use this field directly to match the layout of C unions. */
7184 && TYPE_FIELDS (gnu_variant_type)
7185 && !DECL_CHAIN (TYPE_FIELDS (gnu_variant_type)))
7186 gnu_field = TYPE_FIELDS (gnu_variant_type);
7189 /* Deal with packedness like in gnat_to_gnu_field. */
7191 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7193 /* Finalize the record type now. We used to throw away
7194 empty records but we no longer do that because we need
7195 them to generate complete debug info for the variant;
7196 otherwise, the union type definition will be lacking
7197 the fields associated with these empty variants. */
7198 rest_of_record_type_compilation (gnu_variant_type);
7199 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7200 NULL, true, debug_info, gnat_component_list);
7203 = create_field_decl (gnu_inner_name, gnu_variant_type,
7206 ? TYPE_SIZE (gnu_variant_type) : 0,
7208 ? bitsize_zero_node : 0,
7211 DECL_INTERNAL_P (gnu_field) = 1;
7213 if (!unchecked_union)
7214 DECL_QUALIFIER (gnu_field) = gnu_qual;
7217 DECL_CHAIN (gnu_field) = gnu_variant_list;
7218 gnu_variant_list = gnu_field;
7221 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7222 if (gnu_variant_list)
7224 int union_field_packed;
7226 if (all_rep_and_size)
7228 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7229 TYPE_SIZE_UNIT (gnu_union_type)
7230 = TYPE_SIZE_UNIT (gnu_record_type);
7233 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7234 all_rep_and_size ? 1 : 0, debug_info);
7236 /* If GNU_UNION_TYPE is our record type, it means we must have an
7237 Unchecked_Union with no fields. Verify that and, if so, just
7239 if (gnu_union_type == gnu_record_type)
7241 gcc_assert (unchecked_union
7247 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7248 NULL, true, debug_info, gnat_component_list);
7250 /* Deal with packedness like in gnat_to_gnu_field. */
7252 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7255 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7256 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7257 all_rep ? bitsize_zero_node : 0,
7258 union_field_packed, 0);
7260 DECL_INTERNAL_P (gnu_variant_part) = 1;
7261 DECL_CHAIN (gnu_variant_part) = gnu_field_list;
7262 gnu_field_list = gnu_variant_part;
7266 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7267 permitted to reorder components, self-referential sizes or variable sizes.
7268 If they do, pull them out and put them onto the appropriate list. We have
7269 to do this in a separate pass since we want to handle the discriminants
7270 but can't play with them until we've used them in debugging data above.
7272 ??? If we reorder them, debugging information will be wrong but there is
7273 nothing that can be done about this at the moment. */
7274 gnu_last = NULL_TREE;
7276 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7279 DECL_CHAIN (gnu_last) = gnu_next; \
7281 gnu_field_list = gnu_next; \
7283 DECL_CHAIN (gnu_field) = (LIST); \
7284 (LIST) = gnu_field; \
7287 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7289 gnu_next = DECL_CHAIN (gnu_field);
7291 if (DECL_FIELD_OFFSET (gnu_field))
7293 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7299 /* Pull out the variant part and put it onto GNU_SELF_LIST. */
7300 if (gnu_field == gnu_variant_part)
7302 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7306 /* Skip internal fields and fields with fixed size. */
7307 if (!DECL_INTERNAL_P (gnu_field)
7308 && !(DECL_SIZE (gnu_field)
7309 && TREE_CODE (DECL_SIZE (gnu_field)) == INTEGER_CST))
7311 tree type_size = TYPE_SIZE (TREE_TYPE (gnu_field));
7313 if (CONTAINS_PLACEHOLDER_P (type_size))
7315 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7319 if (TREE_CODE (type_size) != INTEGER_CST)
7321 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7327 gnu_last = gnu_field;
7330 #undef MOVE_FROM_FIELD_LIST_TO
7332 /* If permitted, we reorder the components as follows:
7334 1) all fixed length fields,
7335 2) all fields whose length doesn't depend on discriminants,
7336 3) all fields whose length depends on discriminants,
7337 4) the variant part,
7339 within the record and within each variant recursively. */
7342 = chainon (nreverse (gnu_self_list),
7343 chainon (nreverse (gnu_var_list), gnu_field_list));
7345 /* If we have any fields in our rep'ed field list and it is not the case that
7346 all the fields in the record have rep clauses and P_REP_LIST is nonzero,
7347 set it and ignore these fields. */
7348 if (gnu_rep_list && p_gnu_rep_list && !all_rep)
7349 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7351 /* Otherwise, sort the fields by bit position and put them into their own
7352 record, before the others, if we also have fields without rep clauses. */
7353 else if (gnu_rep_list)
7356 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7357 int i, len = list_length (gnu_rep_list);
7358 tree *gnu_arr = XALLOCAVEC (tree, len);
7360 for (gnu_field = gnu_rep_list, i = 0;
7362 gnu_field = DECL_CHAIN (gnu_field), i++)
7363 gnu_arr[i] = gnu_field;
7365 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7367 /* Put the fields in the list in order of increasing position, which
7368 means we start from the end. */
7369 gnu_rep_list = NULL_TREE;
7370 for (i = len - 1; i >= 0; i--)
7372 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7373 gnu_rep_list = gnu_arr[i];
7374 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7379 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7381 = create_field_decl (get_identifier ("REP"), gnu_rep_type,
7382 gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
7383 DECL_INTERNAL_P (gnu_field) = 1;
7384 gnu_field_list = chainon (gnu_field_list, gnu_field);
7388 layout_with_rep = true;
7389 gnu_field_list = nreverse (gnu_rep_list);
7393 if (cancel_alignment)
7394 TYPE_ALIGN (gnu_record_type) = 0;
7396 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7397 layout_with_rep ? 1 : 0, debug_info && !maybe_unused);
7400 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7401 placed into an Esize, Component_Bit_Offset, or Component_Size value
7402 in the GNAT tree. */
7405 annotate_value (tree gnu_size)
7408 Node_Ref_Or_Val ops[3], ret;
7409 struct tree_int_map **h = NULL;
7412 /* See if we've already saved the value for this node. */
7413 if (EXPR_P (gnu_size))
7415 struct tree_int_map in;
7416 if (!annotate_value_cache)
7417 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7418 tree_int_map_eq, 0);
7419 in.base.from = gnu_size;
7420 h = (struct tree_int_map **)
7421 htab_find_slot (annotate_value_cache, &in, INSERT);
7424 return (Node_Ref_Or_Val) (*h)->to;
7427 /* If we do not return inside this switch, TCODE will be set to the
7428 code to use for a Create_Node operand and LEN (set above) will be
7429 the number of recursive calls for us to make. */
7431 switch (TREE_CODE (gnu_size))
7434 if (TREE_OVERFLOW (gnu_size))
7437 /* This may come from a conversion from some smaller type, so ensure
7438 this is in bitsizetype. */
7439 gnu_size = convert (bitsizetype, gnu_size);
7441 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7442 appear in expressions containing aligning patterns. Note that, since
7443 sizetype is sign-extended but nonetheless unsigned, we don't directly
7444 use tree_int_cst_sgn. */
7445 if (TREE_INT_CST_HIGH (gnu_size) < 0)
7447 tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7448 return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7451 return UI_From_gnu (gnu_size);
7454 /* The only case we handle here is a simple discriminant reference. */
7455 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7456 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7457 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7458 return Create_Node (Discrim_Val,
7459 annotate_value (DECL_DISCRIMINANT_NUMBER
7460 (TREE_OPERAND (gnu_size, 1))),
7465 CASE_CONVERT: case NON_LVALUE_EXPR:
7466 return annotate_value (TREE_OPERAND (gnu_size, 0));
7468 /* Now just list the operations we handle. */
7469 case COND_EXPR: tcode = Cond_Expr; break;
7470 case PLUS_EXPR: tcode = Plus_Expr; break;
7471 case MINUS_EXPR: tcode = Minus_Expr; break;
7472 case MULT_EXPR: tcode = Mult_Expr; break;
7473 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7474 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7475 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7476 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7477 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7478 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7479 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7480 case NEGATE_EXPR: tcode = Negate_Expr; break;
7481 case MIN_EXPR: tcode = Min_Expr; break;
7482 case MAX_EXPR: tcode = Max_Expr; break;
7483 case ABS_EXPR: tcode = Abs_Expr; break;
7484 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7485 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7486 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7487 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7488 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7489 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7490 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7491 case LT_EXPR: tcode = Lt_Expr; break;
7492 case LE_EXPR: tcode = Le_Expr; break;
7493 case GT_EXPR: tcode = Gt_Expr; break;
7494 case GE_EXPR: tcode = Ge_Expr; break;
7495 case EQ_EXPR: tcode = Eq_Expr; break;
7496 case NE_EXPR: tcode = Ne_Expr; break;
7500 tree t = maybe_inline_call_in_expr (gnu_size);
7502 return annotate_value (t);
7505 /* Fall through... */
7511 /* Now get each of the operands that's relevant for this code. If any
7512 cannot be expressed as a repinfo node, say we can't. */
7513 for (i = 0; i < 3; i++)
7516 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7518 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7519 if (ops[i] == No_Uint)
7523 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7525 /* Save the result in the cache. */
7528 *h = ggc_alloc_tree_int_map ();
7529 (*h)->base.from = gnu_size;
7536 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7537 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7538 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7539 BY_REF is true if the object is used by reference and BY_DOUBLE_REF is
7540 true if the object is used by double reference. */
7543 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref,
7549 gnu_type = TREE_TYPE (gnu_type);
7551 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7552 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7554 gnu_type = TREE_TYPE (gnu_type);
7557 if (Unknown_Esize (gnat_entity))
7559 if (TREE_CODE (gnu_type) == RECORD_TYPE
7560 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7561 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7563 size = TYPE_SIZE (gnu_type);
7566 Set_Esize (gnat_entity, annotate_value (size));
7569 if (Unknown_Alignment (gnat_entity))
7570 Set_Alignment (gnat_entity,
7571 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7574 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7575 Return NULL_TREE if there is no such element in the list. */
7578 purpose_member_field (const_tree elem, tree list)
7582 tree field = TREE_PURPOSE (list);
7583 if (SAME_FIELD_P (field, elem))
7585 list = TREE_CHAIN (list);
7590 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7591 set Component_Bit_Offset and Esize of the components to the position and
7592 size used by Gigi. */
7595 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7597 Entity_Id gnat_field;
7600 /* We operate by first making a list of all fields and their position (we
7601 can get the size easily) and then update all the sizes in the tree. */
7603 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7604 BIGGEST_ALIGNMENT, NULL_TREE);
7606 for (gnat_field = First_Entity (gnat_entity);
7607 Present (gnat_field);
7608 gnat_field = Next_Entity (gnat_field))
7609 if (Ekind (gnat_field) == E_Component
7610 || (Ekind (gnat_field) == E_Discriminant
7611 && !Is_Unchecked_Union (Scope (gnat_field))))
7613 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7619 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7621 /* In this mode the tag and parent components are not
7622 generated, so we add the appropriate offset to each
7623 component. For a component appearing in the current
7624 extension, the offset is the size of the parent. */
7625 if (Is_Derived_Type (gnat_entity)
7626 && Original_Record_Component (gnat_field) == gnat_field)
7628 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7631 parent_offset = bitsize_int (POINTER_SIZE);
7634 parent_offset = bitsize_zero_node;
7636 Set_Component_Bit_Offset
7639 (size_binop (PLUS_EXPR,
7640 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7641 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7644 Set_Esize (gnat_field,
7645 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7647 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7649 /* If there is no entry, this is an inherited component whose
7650 position is the same as in the parent type. */
7651 Set_Component_Bit_Offset
7653 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7655 Set_Esize (gnat_field,
7656 Esize (Original_Record_Component (gnat_field)));
7661 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7662 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7663 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7664 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7665 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7666 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7667 pre-existing list to be chained to the newly created entries. */
7670 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7671 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7675 for (gnu_field = TYPE_FIELDS (gnu_type);
7677 gnu_field = DECL_CHAIN (gnu_field))
7679 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7680 DECL_FIELD_BIT_OFFSET (gnu_field));
7681 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7682 DECL_FIELD_OFFSET (gnu_field));
7683 unsigned int our_offset_align
7684 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7685 tree v = make_tree_vec (3);
7687 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7688 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7689 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7690 gnu_list = tree_cons (gnu_field, v, gnu_list);
7692 /* Recurse on internal fields, flattening the nested fields except for
7693 those in the variant part, if requested. */
7694 if (DECL_INTERNAL_P (gnu_field))
7696 tree gnu_field_type = TREE_TYPE (gnu_field);
7697 if (do_not_flatten_variant
7698 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7700 = build_position_list (gnu_field_type, do_not_flatten_variant,
7701 size_zero_node, bitsize_zero_node,
7702 BIGGEST_ALIGNMENT, gnu_list);
7705 = build_position_list (gnu_field_type, do_not_flatten_variant,
7706 gnu_our_offset, gnu_our_bitpos,
7707 our_offset_align, gnu_list);
7714 /* Return a VEC describing the substitutions needed to reflect the
7715 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7716 be in any order. The values in an element of the VEC are in the form
7717 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7718 a definition of GNAT_SUBTYPE. */
7720 static VEC(subst_pair,heap) *
7721 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7723 VEC(subst_pair,heap) *gnu_vec = NULL;
7724 Entity_Id gnat_discrim;
7727 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7728 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7729 Present (gnat_discrim);
7730 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7731 gnat_value = Next_Elmt (gnat_value))
7732 /* Ignore access discriminants. */
7733 if (!Is_Access_Type (Etype (Node (gnat_value))))
7735 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7736 tree replacement = convert (TREE_TYPE (gnu_field),
7737 elaborate_expression
7738 (Node (gnat_value), gnat_subtype,
7739 get_entity_name (gnat_discrim),
7740 definition, true, false));
7741 subst_pair *s = VEC_safe_push (subst_pair, heap, gnu_vec, NULL);
7742 s->discriminant = gnu_field;
7743 s->replacement = replacement;
7749 /* Scan all fields in QUAL_UNION_TYPE and return a VEC describing the
7750 variants of QUAL_UNION_TYPE that are still relevant after applying
7751 the substitutions described in SUBST_LIST. VARIANT_LIST is a
7752 pre-existing VEC onto which newly created entries should be
7755 static VEC(variant_desc,heap) *
7756 build_variant_list (tree qual_union_type, VEC(subst_pair,heap) *subst_list,
7757 VEC(variant_desc,heap) *variant_list)
7761 for (gnu_field = TYPE_FIELDS (qual_union_type);
7763 gnu_field = DECL_CHAIN (gnu_field))
7765 tree qual = DECL_QUALIFIER (gnu_field);
7769 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
7770 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7772 /* If the new qualifier is not unconditionally false, its variant may
7773 still be accessed. */
7774 if (!integer_zerop (qual))
7777 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7779 v = VEC_safe_push (variant_desc, heap, variant_list, NULL);
7780 v->type = variant_type;
7781 v->field = gnu_field;
7783 v->record = NULL_TREE;
7785 /* Recurse on the variant subpart of the variant, if any. */
7786 variant_subpart = get_variant_part (variant_type);
7787 if (variant_subpart)
7788 variant_list = build_variant_list (TREE_TYPE (variant_subpart),
7789 subst_list, variant_list);
7791 /* If the new qualifier is unconditionally true, the subsequent
7792 variants cannot be accessed. */
7793 if (integer_onep (qual))
7798 return variant_list;
7801 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7802 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
7803 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
7804 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7805 for the size of a field. COMPONENT_P is true if we are being called
7806 to process the Component_Size of GNAT_OBJECT. This is used for error
7807 message handling and to indicate to use the object size of GNU_TYPE.
7808 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7809 it means that a size of zero should be treated as an unspecified size. */
7812 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7813 enum tree_code kind, bool component_p, bool zero_ok)
7815 Node_Id gnat_error_node;
7816 tree type_size, size;
7818 /* Return 0 if no size was specified. */
7819 if (uint_size == No_Uint)
7822 /* Ignore a negative size since that corresponds to our back-annotation. */
7823 if (UI_Lt (uint_size, Uint_0))
7826 /* Find the node to use for errors. */
7827 if ((Ekind (gnat_object) == E_Component
7828 || Ekind (gnat_object) == E_Discriminant)
7829 && Present (Component_Clause (gnat_object)))
7830 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7831 else if (Present (Size_Clause (gnat_object)))
7832 gnat_error_node = Expression (Size_Clause (gnat_object));
7834 gnat_error_node = gnat_object;
7836 /* Get the size as a tree. Issue an error if a size was specified but
7837 cannot be represented in sizetype. */
7838 size = UI_To_gnu (uint_size, bitsizetype);
7839 if (TREE_OVERFLOW (size))
7842 post_error_ne ("component size of & is too large", gnat_error_node,
7845 post_error_ne ("size of & is too large", gnat_error_node,
7850 /* Ignore a zero size if it is not permitted. */
7851 if (!zero_ok && integer_zerop (size))
7854 /* The size of objects is always a multiple of a byte. */
7855 if (kind == VAR_DECL
7856 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7859 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7860 gnat_error_node, gnat_object);
7862 post_error_ne ("size for& is not a multiple of Storage_Unit",
7863 gnat_error_node, gnat_object);
7867 /* If this is an integral type or a packed array type, the front-end has
7868 verified the size, so we need not do it here (which would entail
7869 checking against the bounds). However, if this is an aliased object,
7870 it may not be smaller than the type of the object. */
7871 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7872 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7875 /* If the object is a record that contains a template, add the size of
7876 the template to the specified size. */
7877 if (TREE_CODE (gnu_type) == RECORD_TYPE
7878 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7879 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7881 if (kind == VAR_DECL
7882 /* If a type needs strict alignment, a component of this type in
7883 a packed record cannot be packed and thus uses the type size. */
7884 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7885 type_size = TYPE_SIZE (gnu_type);
7887 type_size = rm_size (gnu_type);
7889 /* Modify the size of the type to be that of the maximum size if it has a
7891 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7892 type_size = max_size (type_size, true);
7894 /* If this is an access type or a fat pointer, the minimum size is that given
7895 by the smallest integral mode that's valid for pointers. */
7896 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7898 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7899 while (!targetm.valid_pointer_mode (p_mode))
7900 p_mode = GET_MODE_WIDER_MODE (p_mode);
7901 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7904 /* If the size of the object is a constant, the new size must not be
7906 if (TREE_CODE (type_size) != INTEGER_CST
7907 || TREE_OVERFLOW (type_size)
7908 || tree_int_cst_lt (size, type_size))
7912 ("component size for& too small{, minimum allowed is ^}",
7913 gnat_error_node, gnat_object, type_size);
7916 ("size for& too small{, minimum allowed is ^}",
7917 gnat_error_node, gnat_object, type_size);
7925 /* Similarly, but both validate and process a value of RM size. This
7926 routine is only called for types. */
7929 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7931 Node_Id gnat_attr_node;
7932 tree old_size, size;
7934 /* Do nothing if no size was specified. */
7935 if (uint_size == No_Uint)
7938 /* Ignore a negative size since that corresponds to our back-annotation. */
7939 if (UI_Lt (uint_size, Uint_0))
7942 /* Only issue an error if a Value_Size clause was explicitly given.
7943 Otherwise, we'd be duplicating an error on the Size clause. */
7945 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7947 /* Get the size as a tree. Issue an error if a size was specified but
7948 cannot be represented in sizetype. */
7949 size = UI_To_gnu (uint_size, bitsizetype);
7950 if (TREE_OVERFLOW (size))
7952 if (Present (gnat_attr_node))
7953 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7958 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7959 exists, or this is an integer type, in which case the front-end will
7960 have always set it. */
7961 if (No (gnat_attr_node)
7962 && integer_zerop (size)
7963 && !Has_Size_Clause (gnat_entity)
7964 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7967 old_size = rm_size (gnu_type);
7969 /* If the old size is self-referential, get the maximum size. */
7970 if (CONTAINS_PLACEHOLDER_P (old_size))
7971 old_size = max_size (old_size, true);
7973 /* If the size of the object is a constant, the new size must not be smaller
7974 (the front-end has verified this for scalar and packed array types). */
7975 if (TREE_CODE (old_size) != INTEGER_CST
7976 || TREE_OVERFLOW (old_size)
7977 || (AGGREGATE_TYPE_P (gnu_type)
7978 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7979 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7980 && !(TYPE_IS_PADDING_P (gnu_type)
7981 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7982 && TYPE_PACKED_ARRAY_TYPE_P
7983 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7984 && tree_int_cst_lt (size, old_size)))
7986 if (Present (gnat_attr_node))
7988 ("Value_Size for& too small{, minimum allowed is ^}",
7989 gnat_attr_node, gnat_entity, old_size);
7993 /* Otherwise, set the RM size proper for integral types... */
7994 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7995 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7996 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7997 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7998 SET_TYPE_RM_SIZE (gnu_type, size);
8000 /* ...or the Ada size for record and union types. */
8001 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
8002 || TREE_CODE (gnu_type) == UNION_TYPE
8003 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8004 && !TYPE_FAT_POINTER_P (gnu_type))
8005 SET_TYPE_ADA_SIZE (gnu_type, size);
8008 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
8009 If TYPE is the best type, return it. Otherwise, make a new type. We
8010 only support new integral and pointer types. FOR_BIASED is true if
8011 we are making a biased type. */
8014 make_type_from_size (tree type, tree size_tree, bool for_biased)
8016 unsigned HOST_WIDE_INT size;
8020 /* If size indicates an error, just return TYPE to avoid propagating
8021 the error. Likewise if it's too large to represent. */
8022 if (!size_tree || !host_integerp (size_tree, 1))
8025 size = tree_low_cst (size_tree, 1);
8027 switch (TREE_CODE (type))
8032 biased_p = (TREE_CODE (type) == INTEGER_TYPE
8033 && TYPE_BIASED_REPRESENTATION_P (type));
8035 /* Integer types with precision 0 are forbidden. */
8039 /* Only do something if the type is not a packed array type and
8040 doesn't already have the proper size. */
8041 if (TYPE_PACKED_ARRAY_TYPE_P (type)
8042 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
8045 biased_p |= for_biased;
8046 if (size > LONG_LONG_TYPE_SIZE)
8047 size = LONG_LONG_TYPE_SIZE;
8049 if (TYPE_UNSIGNED (type) || biased_p)
8050 new_type = make_unsigned_type (size);
8052 new_type = make_signed_type (size);
8053 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
8054 SET_TYPE_RM_MIN_VALUE (new_type,
8055 convert (TREE_TYPE (new_type),
8056 TYPE_MIN_VALUE (type)));
8057 SET_TYPE_RM_MAX_VALUE (new_type,
8058 convert (TREE_TYPE (new_type),
8059 TYPE_MAX_VALUE (type)));
8060 /* Copy the name to show that it's essentially the same type and
8061 not a subrange type. */
8062 TYPE_NAME (new_type) = TYPE_NAME (type);
8063 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
8064 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
8068 /* Do something if this is a fat pointer, in which case we
8069 may need to return the thin pointer. */
8070 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
8072 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
8073 if (!targetm.valid_pointer_mode (p_mode))
8076 build_pointer_type_for_mode
8077 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
8083 /* Only do something if this is a thin pointer, in which case we
8084 may need to return the fat pointer. */
8085 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
8087 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
8097 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8098 a type or object whose present alignment is ALIGN. If this alignment is
8099 valid, return it. Otherwise, give an error and return ALIGN. */
8102 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8104 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8105 unsigned int new_align;
8106 Node_Id gnat_error_node;
8108 /* Don't worry about checking alignment if alignment was not specified
8109 by the source program and we already posted an error for this entity. */
8110 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8113 /* Post the error on the alignment clause if any. Note, for the implicit
8114 base type of an array type, the alignment clause is on the first
8116 if (Present (Alignment_Clause (gnat_entity)))
8117 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8119 else if (Is_Itype (gnat_entity)
8120 && Is_Array_Type (gnat_entity)
8121 && Etype (gnat_entity) == gnat_entity
8122 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8124 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8127 gnat_error_node = gnat_entity;
8129 /* Within GCC, an alignment is an integer, so we must make sure a value is
8130 specified that fits in that range. Also, there is an upper bound to
8131 alignments we can support/allow. */
8132 if (!UI_Is_In_Int_Range (alignment)
8133 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8134 post_error_ne_num ("largest supported alignment for& is ^",
8135 gnat_error_node, gnat_entity, max_allowed_alignment);
8136 else if (!(Present (Alignment_Clause (gnat_entity))
8137 && From_At_Mod (Alignment_Clause (gnat_entity)))
8138 && new_align * BITS_PER_UNIT < align)
8140 unsigned int double_align;
8141 bool is_capped_double, align_clause;
8143 /* If the default alignment of "double" or larger scalar types is
8144 specifically capped and the new alignment is above the cap, do
8145 not post an error and change the alignment only if there is an
8146 alignment clause; this makes it possible to have the associated
8147 GCC type overaligned by default for performance reasons. */
8148 if ((double_align = double_float_alignment) > 0)
8151 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8153 = is_double_float_or_array (gnat_type, &align_clause);
8155 else if ((double_align = double_scalar_alignment) > 0)
8158 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8160 = is_double_scalar_or_array (gnat_type, &align_clause);
8163 is_capped_double = align_clause = false;
8165 if (is_capped_double && new_align >= double_align)
8168 align = new_align * BITS_PER_UNIT;
8172 if (is_capped_double)
8173 align = double_align * BITS_PER_UNIT;
8175 post_error_ne_num ("alignment for& must be at least ^",
8176 gnat_error_node, gnat_entity,
8177 align / BITS_PER_UNIT);
8182 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8183 if (new_align > align)
8190 /* Return the smallest alignment not less than SIZE. */
8193 ceil_alignment (unsigned HOST_WIDE_INT size)
8195 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
8198 /* Verify that OBJECT, a type or decl, is something we can implement
8199 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8200 if we require atomic components. */
8203 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8205 Node_Id gnat_error_point = gnat_entity;
8207 enum machine_mode mode;
8211 /* There are three case of what OBJECT can be. It can be a type, in which
8212 case we take the size, alignment and mode from the type. It can be a
8213 declaration that was indirect, in which case the relevant values are
8214 that of the type being pointed to, or it can be a normal declaration,
8215 in which case the values are of the decl. The code below assumes that
8216 OBJECT is either a type or a decl. */
8217 if (TYPE_P (object))
8219 /* If this is an anonymous base type, nothing to check. Error will be
8220 reported on the source type. */
8221 if (!Comes_From_Source (gnat_entity))
8224 mode = TYPE_MODE (object);
8225 align = TYPE_ALIGN (object);
8226 size = TYPE_SIZE (object);
8228 else if (DECL_BY_REF_P (object))
8230 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8231 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8232 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8236 mode = DECL_MODE (object);
8237 align = DECL_ALIGN (object);
8238 size = DECL_SIZE (object);
8241 /* Consider all floating-point types atomic and any types that that are
8242 represented by integers no wider than a machine word. */
8243 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8244 || ((GET_MODE_CLASS (mode) == MODE_INT
8245 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8246 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8249 /* For the moment, also allow anything that has an alignment equal
8250 to its size and which is smaller than a word. */
8251 if (size && TREE_CODE (size) == INTEGER_CST
8252 && compare_tree_int (size, align) == 0
8253 && align <= BITS_PER_WORD)
8256 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8257 gnat_node = Next_Rep_Item (gnat_node))
8259 if (!comp_p && Nkind (gnat_node) == N_Pragma
8260 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8262 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8263 else if (comp_p && Nkind (gnat_node) == N_Pragma
8264 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8265 == Pragma_Atomic_Components))
8266 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8270 post_error_ne ("atomic access to component of & cannot be guaranteed",
8271 gnat_error_point, gnat_entity);
8273 post_error_ne ("atomic access to & cannot be guaranteed",
8274 gnat_error_point, gnat_entity);
8278 /* Helper for the intrin compatibility checks family. Evaluate whether
8279 two types are definitely incompatible. */
8282 intrin_types_incompatible_p (tree t1, tree t2)
8284 enum tree_code code;
8286 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8289 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8292 if (TREE_CODE (t1) != TREE_CODE (t2))
8295 code = TREE_CODE (t1);
8301 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8304 case REFERENCE_TYPE:
8305 /* Assume designated types are ok. We'd need to account for char * and
8306 void * variants to do better, which could rapidly get messy and isn't
8307 clearly worth the effort. */
8317 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8318 on the Ada/builtin argument lists for the INB binding. */
8321 intrin_arglists_compatible_p (intrin_binding_t * inb)
8323 tree ada_args = TYPE_ARG_TYPES (inb->ada_fntype);
8324 tree btin_args = TYPE_ARG_TYPES (inb->btin_fntype);
8326 /* Sequence position of the last argument we checked. */
8329 while (ada_args != 0 || btin_args != 0)
8331 tree ada_type, btin_type;
8333 /* If one list is shorter than the other, they fail to match. */
8334 if (ada_args == 0 || btin_args == 0)
8337 ada_type = TREE_VALUE (ada_args);
8338 btin_type = TREE_VALUE (btin_args);
8340 /* If we're done with the Ada args and not with the internal builtin
8341 args, or the other way around, complain. */
8342 if (ada_type == void_type_node
8343 && btin_type != void_type_node)
8345 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8349 if (btin_type == void_type_node
8350 && ada_type != void_type_node)
8352 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8353 inb->gnat_entity, inb->gnat_entity, argpos);
8357 /* Otherwise, check that types match for the current argument. */
8359 if (intrin_types_incompatible_p (ada_type, btin_type))
8361 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8362 inb->gnat_entity, inb->gnat_entity, argpos);
8366 ada_args = TREE_CHAIN (ada_args);
8367 btin_args = TREE_CHAIN (btin_args);
8373 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8374 on the Ada/builtin return values for the INB binding. */
8377 intrin_return_compatible_p (intrin_binding_t * inb)
8379 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8380 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8382 /* Accept function imported as procedure, common and convenient. */
8383 if (VOID_TYPE_P (ada_return_type)
8384 && !VOID_TYPE_P (btin_return_type))
8387 /* Check return types compatibility otherwise. Note that this
8388 handles void/void as well. */
8389 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8391 post_error ("?intrinsic binding type mismatch on return value!",
8399 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8400 compatible. Issue relevant warnings when they are not.
8402 This is intended as a light check to diagnose the most obvious cases, not
8403 as a full fledged type compatibility predicate. It is the programmer's
8404 responsibility to ensure correctness of the Ada declarations in Imports,
8405 especially when binding straight to a compiler internal. */
8408 intrin_profiles_compatible_p (intrin_binding_t * inb)
8410 /* Check compatibility on return values and argument lists, each responsible
8411 for posting warnings as appropriate. Ensure use of the proper sloc for
8414 bool arglists_compatible_p, return_compatible_p;
8415 location_t saved_location = input_location;
8417 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8419 return_compatible_p = intrin_return_compatible_p (inb);
8420 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8422 input_location = saved_location;
8424 return return_compatible_p && arglists_compatible_p;
8427 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8428 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8429 specified size for this field. POS_LIST is a position list describing
8430 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8434 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8435 tree size, tree pos_list,
8436 VEC(subst_pair,heap) *subst_list)
8438 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8439 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8440 unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8441 tree new_pos, new_field;
8445 if (CONTAINS_PLACEHOLDER_P (pos))
8446 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8447 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8449 /* If the position is now a constant, we can set it as the position of the
8450 field when we make it. Otherwise, we need to deal with it specially. */
8451 if (TREE_CONSTANT (pos))
8452 new_pos = bit_from_pos (pos, bitpos);
8454 new_pos = NULL_TREE;
8457 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8458 size, new_pos, DECL_PACKED (old_field),
8459 !DECL_NONADDRESSABLE_P (old_field));
8463 normalize_offset (&pos, &bitpos, offset_align);
8464 DECL_FIELD_OFFSET (new_field) = pos;
8465 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8466 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8467 DECL_SIZE (new_field) = size;
8468 DECL_SIZE_UNIT (new_field)
8469 = convert (sizetype,
8470 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8471 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8474 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8475 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8476 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8477 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8482 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8485 get_rep_part (tree record_type)
8487 tree field = TYPE_FIELDS (record_type);
8489 /* The REP part is the first field, internal, another record, and its name
8490 doesn't start with an underscore (i.e. is not generated by the FE). */
8491 if (DECL_INTERNAL_P (field)
8492 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8493 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
8499 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8502 get_variant_part (tree record_type)
8506 /* The variant part is the only internal field that is a qualified union. */
8507 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8508 if (DECL_INTERNAL_P (field)
8509 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8515 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8516 the list of variants to be used and RECORD_TYPE is the type of the parent.
8517 POS_LIST is a position list describing the layout of fields present in
8518 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8522 create_variant_part_from (tree old_variant_part,
8523 VEC(variant_desc,heap) *variant_list,
8524 tree record_type, tree pos_list,
8525 VEC(subst_pair,heap) *subst_list)
8527 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8528 tree old_union_type = TREE_TYPE (old_variant_part);
8529 tree new_union_type, new_variant_part;
8530 tree union_field_list = NULL_TREE;
8534 /* First create the type of the variant part from that of the old one. */
8535 new_union_type = make_node (QUAL_UNION_TYPE);
8536 TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8538 /* If the position of the variant part is constant, subtract it from the
8539 size of the type of the parent to get the new size. This manual CSE
8540 reduces the code size when not optimizing. */
8541 if (TREE_CODE (offset) == INTEGER_CST)
8543 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8544 tree first_bit = bit_from_pos (offset, bitpos);
8545 TYPE_SIZE (new_union_type)
8546 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8547 TYPE_SIZE_UNIT (new_union_type)
8548 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8549 byte_from_pos (offset, bitpos));
8550 SET_TYPE_ADA_SIZE (new_union_type,
8551 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8553 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8554 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8557 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8559 /* Now finish up the new variants and populate the union type. */
8560 FOR_EACH_VEC_ELT_REVERSE (variant_desc, variant_list, ix, v)
8562 tree old_field = v->field, new_field;
8563 tree old_variant, old_variant_subpart, new_variant, field_list;
8565 /* Skip variants that don't belong to this nesting level. */
8566 if (DECL_CONTEXT (old_field) != old_union_type)
8569 /* Retrieve the list of fields already added to the new variant. */
8570 new_variant = v->record;
8571 field_list = TYPE_FIELDS (new_variant);
8573 /* If the old variant had a variant subpart, we need to create a new
8574 variant subpart and add it to the field list. */
8575 old_variant = v->type;
8576 old_variant_subpart = get_variant_part (old_variant);
8577 if (old_variant_subpart)
8579 tree new_variant_subpart
8580 = create_variant_part_from (old_variant_subpart, variant_list,
8581 new_variant, pos_list, subst_list);
8582 DECL_CHAIN (new_variant_subpart) = field_list;
8583 field_list = new_variant_subpart;
8586 /* Finish up the new variant and create the field. No need for debug
8587 info thanks to the XVS type. */
8588 finish_record_type (new_variant, nreverse (field_list), 2, false);
8589 compute_record_mode (new_variant);
8590 create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8591 true, false, Empty);
8594 = create_field_decl_from (old_field, new_variant, new_union_type,
8595 TYPE_SIZE (new_variant),
8596 pos_list, subst_list);
8597 DECL_QUALIFIER (new_field) = v->qual;
8598 DECL_INTERNAL_P (new_field) = 1;
8599 DECL_CHAIN (new_field) = union_field_list;
8600 union_field_list = new_field;
8603 /* Finish up the union type and create the variant part. No need for debug
8604 info thanks to the XVS type. */
8605 finish_record_type (new_union_type, union_field_list, 2, false);
8606 compute_record_mode (new_union_type);
8607 create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8608 true, false, Empty);
8611 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8612 TYPE_SIZE (new_union_type),
8613 pos_list, subst_list);
8614 DECL_INTERNAL_P (new_variant_part) = 1;
8616 /* With multiple discriminants it is possible for an inner variant to be
8617 statically selected while outer ones are not; in this case, the list
8618 of fields of the inner variant is not flattened and we end up with a
8619 qualified union with a single member. Drop the useless container. */
8620 if (!DECL_CHAIN (union_field_list))
8622 DECL_CONTEXT (union_field_list) = record_type;
8623 DECL_FIELD_OFFSET (union_field_list)
8624 = DECL_FIELD_OFFSET (new_variant_part);
8625 DECL_FIELD_BIT_OFFSET (union_field_list)
8626 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8627 SET_DECL_OFFSET_ALIGN (union_field_list,
8628 DECL_OFFSET_ALIGN (new_variant_part));
8629 new_variant_part = union_field_list;
8632 return new_variant_part;
8635 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8636 which are both RECORD_TYPE, after applying the substitutions described
8640 copy_and_substitute_in_size (tree new_type, tree old_type,
8641 VEC(subst_pair,heap) *subst_list)
8646 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8647 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8648 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8649 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8650 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8652 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8653 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8654 TYPE_SIZE (new_type)
8655 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8656 s->discriminant, s->replacement);
8658 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8659 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8660 TYPE_SIZE_UNIT (new_type)
8661 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8662 s->discriminant, s->replacement);
8664 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8665 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8667 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8668 s->discriminant, s->replacement));
8670 /* Finalize the size. */
8671 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8672 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8675 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8676 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8677 updated by replacing F with R.
8679 The function doesn't update the layout of the type, i.e. it assumes
8680 that the substitution is purely formal. That's why the replacement
8681 value R must itself contain a PLACEHOLDER_EXPR. */
8684 substitute_in_type (tree t, tree f, tree r)
8688 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8690 switch (TREE_CODE (t))
8697 /* First the domain types of arrays. */
8698 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8699 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8701 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8702 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8704 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8708 TYPE_GCC_MIN_VALUE (nt) = low;
8709 TYPE_GCC_MAX_VALUE (nt) = high;
8711 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8713 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8718 /* Then the subtypes. */
8719 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8720 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8722 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8723 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8725 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8729 SET_TYPE_RM_MIN_VALUE (nt, low);
8730 SET_TYPE_RM_MAX_VALUE (nt, high);
8738 nt = substitute_in_type (TREE_TYPE (t), f, r);
8739 if (nt == TREE_TYPE (t))
8742 return build_complex_type (nt);
8745 /* These should never show up here. */
8750 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8751 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8753 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8756 nt = build_nonshared_array_type (component, domain);
8757 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8758 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8759 SET_TYPE_MODE (nt, TYPE_MODE (t));
8760 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8761 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8762 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8763 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8764 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8770 case QUAL_UNION_TYPE:
8772 bool changed_field = false;
8775 /* Start out with no fields, make new fields, and chain them
8776 in. If we haven't actually changed the type of any field,
8777 discard everything we've done and return the old type. */
8779 TYPE_FIELDS (nt) = NULL_TREE;
8781 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8783 tree new_field = copy_node (field), new_n;
8785 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8786 if (new_n != TREE_TYPE (field))
8788 TREE_TYPE (new_field) = new_n;
8789 changed_field = true;
8792 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8793 if (new_n != DECL_FIELD_OFFSET (field))
8795 DECL_FIELD_OFFSET (new_field) = new_n;
8796 changed_field = true;
8799 /* Do the substitution inside the qualifier, if any. */
8800 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8802 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8803 if (new_n != DECL_QUALIFIER (field))
8805 DECL_QUALIFIER (new_field) = new_n;
8806 changed_field = true;
8810 DECL_CONTEXT (new_field) = nt;
8811 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8813 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8814 TYPE_FIELDS (nt) = new_field;
8820 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8821 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8822 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8823 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8832 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8833 needed to represent the object. */
8836 rm_size (tree gnu_type)
8838 /* For integral types, we store the RM size explicitly. */
8839 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8840 return TYPE_RM_SIZE (gnu_type);
8842 /* Return the RM size of the actual data plus the size of the template. */
8843 if (TREE_CODE (gnu_type) == RECORD_TYPE
8844 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8846 size_binop (PLUS_EXPR,
8847 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8848 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8850 /* For record types, we store the size explicitly. */
8851 if ((TREE_CODE (gnu_type) == RECORD_TYPE
8852 || TREE_CODE (gnu_type) == UNION_TYPE
8853 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8854 && !TYPE_FAT_POINTER_P (gnu_type)
8855 && TYPE_ADA_SIZE (gnu_type))
8856 return TYPE_ADA_SIZE (gnu_type);
8858 /* For other types, this is just the size. */
8859 return TYPE_SIZE (gnu_type);
8862 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8863 fully-qualified name, possibly with type information encoding.
8864 Otherwise, return the name. */
8867 get_entity_name (Entity_Id gnat_entity)
8869 Get_Encoded_Name (gnat_entity);
8870 return get_identifier_with_length (Name_Buffer, Name_Len);
8873 /* Return an identifier representing the external name to be used for
8874 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8875 and the specified suffix. */
8878 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8880 Entity_Kind kind = Ekind (gnat_entity);
8884 String_Template temp = {1, strlen (suffix)};
8885 Fat_Pointer fp = {suffix, &temp};
8886 Get_External_Name_With_Suffix (gnat_entity, fp);
8889 Get_External_Name (gnat_entity, 0);
8891 /* A variable using the Stdcall convention lives in a DLL. We adjust
8892 its name to use the jump table, the _imp__NAME contains the address
8893 for the NAME variable. */
8894 if ((kind == E_Variable || kind == E_Constant)
8895 && Has_Stdcall_Convention (gnat_entity))
8897 const int len = 6 + Name_Len;
8898 char *new_name = (char *) alloca (len + 1);
8899 strcpy (new_name, "_imp__");
8900 strcat (new_name, Name_Buffer);
8901 return get_identifier_with_length (new_name, len);
8904 return get_identifier_with_length (Name_Buffer, Name_Len);
8907 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8908 string, return a new IDENTIFIER_NODE that is the concatenation of
8909 the name followed by "___" and the specified suffix. */
8912 concat_name (tree gnu_name, const char *suffix)
8914 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8915 char *new_name = (char *) alloca (len + 1);
8916 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8917 strcat (new_name, "___");
8918 strcat (new_name, suffix);
8919 return get_identifier_with_length (new_name, len);
8922 #include "gt-ada-decl.h"