1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2016, 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"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
73 #define STDCALL_PREFIX "_imp__"
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
88 #define FOREIGN_FORCE_REALIGN_STACK 0
93 struct incomplete *next;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_Limited_With types until the
105 static struct incomplete *defer_limited_with;
107 typedef struct subst_pair_d {
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
117 /* The associated field. */
120 /* The value of the qualifier. */
123 /* The type of the variant after transformation. */
128 /* A hash table used to cache the result of annotate_value. */
130 struct value_annotation_hasher : ggc_cache_ptr_hash<tree_int_map>
132 static inline hashval_t
133 hash (tree_int_map *m)
135 return htab_hash_pointer (m->base.from);
139 equal (tree_int_map *a, tree_int_map *b)
141 return a->base.from == b->base.from;
145 keep_cache_entry (tree_int_map *&m)
147 return ggc_marked_p (m->base.from);
151 static GTY ((cache)) hash_table<value_annotation_hasher> *annotate_value_cache;
153 static void prepend_one_attribute (struct attrib **,
154 enum attrib_type, tree, tree, Node_Id);
155 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
156 static void prepend_attributes (struct attrib **, Entity_Id);
157 static tree elaborate_expression (Node_Id, Entity_Id, const char *, bool, bool,
159 static bool type_has_variable_size (tree);
160 static tree elaborate_expression_1 (tree, Entity_Id, const char *, bool, bool);
161 static tree elaborate_expression_2 (tree, Entity_Id, const char *, bool, bool,
163 static tree elaborate_reference (tree, Entity_Id, bool, tree *);
164 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
165 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
167 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
168 static bool is_from_limited_with_of_main (Entity_Id);
169 static tree change_qualified_type (tree, int);
170 static bool same_discriminant_p (Entity_Id, Entity_Id);
171 static bool array_type_has_nonaliased_component (tree, Entity_Id);
172 static bool compile_time_known_address_p (Node_Id);
173 static bool cannot_be_superflat (Node_Id);
174 static bool constructor_address_p (tree);
175 static bool allocatable_size_p (tree, bool);
176 static bool initial_value_needs_conversion (tree, tree);
177 static int compare_field_bitpos (const PTR, const PTR);
178 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
179 bool, bool, bool, bool, bool, tree, tree *);
180 static Uint annotate_value (tree);
181 static void annotate_rep (Entity_Id, tree);
182 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
183 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
184 static vec<variant_desc> build_variant_list (tree,
187 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
188 static void set_rm_size (Uint, tree, Entity_Id);
189 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
190 static void check_ok_for_atomic_type (tree, Entity_Id, bool);
191 static tree create_field_decl_from (tree, tree, tree, tree, tree,
193 static tree create_rep_part (tree, tree, tree);
194 static tree get_rep_part (tree);
195 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
196 tree, vec<subst_pair> );
197 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
198 static void associate_original_type_to_packed_array (tree, Entity_Id);
199 static const char *get_entity_char (Entity_Id);
201 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
202 to pass around calls performing profile compatibility checks. */
205 Entity_Id gnat_entity; /* The Ada subprogram entity. */
206 tree ada_fntype; /* The corresponding GCC type node. */
207 tree btin_fntype; /* The GCC builtin function type node. */
210 static bool intrin_profiles_compatible_p (intrin_binding_t *);
212 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
213 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
214 and associate the ..._DECL node with the input GNAT defining identifier.
216 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
217 initial value (in GCC tree form). This is optional for a variable. For
218 a renamed entity, GNU_EXPR gives the object being renamed.
220 DEFINITION is nonzero if this call is intended for a definition. This is
221 used for separate compilation where it is necessary to know whether an
222 external declaration or a definition must be created if the GCC equivalent
223 was not created previously. The value of 1 is normally used for a nonzero
224 DEFINITION, but a value of 2 is used in special circumstances, defined in
228 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
230 /* Contains the kind of the input GNAT node. */
231 const Entity_Kind kind = Ekind (gnat_entity);
232 /* True if this is a type. */
233 const bool is_type = IN (kind, Type_Kind);
234 /* True if this is an artificial entity. */
235 const bool artificial_p = !Comes_From_Source (gnat_entity);
236 /* True if debug info is requested for this entity. */
237 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
238 /* True if this entity is to be considered as imported. */
239 const bool imported_p
240 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
241 /* For a type, contains the equivalent GNAT node to be used in gigi. */
242 Entity_Id gnat_equiv_type = Empty;
243 /* Temporary used to walk the GNAT tree. */
245 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
246 This node will be associated with the GNAT node by calling at the end
247 of the `switch' statement. */
248 tree gnu_decl = NULL_TREE;
249 /* Contains the GCC type to be used for the GCC node. */
250 tree gnu_type = NULL_TREE;
251 /* Contains the GCC size tree to be used for the GCC node. */
252 tree gnu_size = NULL_TREE;
253 /* Contains the GCC name to be used for the GCC node. */
254 tree gnu_entity_name;
255 /* True if we have already saved gnu_decl as a GNAT association. */
257 /* True if we incremented defer_incomplete_level. */
258 bool this_deferred = false;
259 /* True if we incremented force_global. */
260 bool this_global = false;
261 /* True if we should check to see if elaborated during processing. */
262 bool maybe_present = false;
263 /* True if we made GNU_DECL and its type here. */
264 bool this_made_decl = false;
265 /* Size and alignment of the GCC node, if meaningful. */
266 unsigned int esize = 0, align = 0;
267 /* Contains the list of attributes directly attached to the entity. */
268 struct attrib *attr_list = NULL;
270 /* Since a use of an Itype is a definition, process it as such if it
271 is not in a with'ed unit. */
274 && Is_Itype (gnat_entity)
275 && !present_gnu_tree (gnat_entity)
276 && In_Extended_Main_Code_Unit (gnat_entity))
278 /* Ensure that we are in a subprogram mentioned in the Scope chain of
279 this entity, our current scope is global, or we encountered a task
280 or entry (where we can't currently accurately check scoping). */
281 if (!current_function_decl
282 || DECL_ELABORATION_PROC_P (current_function_decl))
284 process_type (gnat_entity);
285 return get_gnu_tree (gnat_entity);
288 for (gnat_temp = Scope (gnat_entity);
290 gnat_temp = Scope (gnat_temp))
292 if (Is_Type (gnat_temp))
293 gnat_temp = Underlying_Type (gnat_temp);
295 if (Ekind (gnat_temp) == E_Subprogram_Body)
297 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
299 if (IN (Ekind (gnat_temp), Subprogram_Kind)
300 && Present (Protected_Body_Subprogram (gnat_temp)))
301 gnat_temp = Protected_Body_Subprogram (gnat_temp);
303 if (Ekind (gnat_temp) == E_Entry
304 || Ekind (gnat_temp) == E_Entry_Family
305 || Ekind (gnat_temp) == E_Task_Type
306 || (IN (Ekind (gnat_temp), Subprogram_Kind)
307 && present_gnu_tree (gnat_temp)
308 && (current_function_decl
309 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
311 process_type (gnat_entity);
312 return get_gnu_tree (gnat_entity);
316 /* This abort means the Itype has an incorrect scope, i.e. that its
317 scope does not correspond to the subprogram it is declared in. */
321 /* If we've already processed this entity, return what we got last time.
322 If we are defining the node, we should not have already processed it.
323 In that case, we will abort below when we try to save a new GCC tree
324 for this object. We also need to handle the case of getting a dummy
325 type when a Full_View exists but be careful so as not to trigger its
326 premature elaboration. */
327 if ((!definition || (is_type && imported_p))
328 && present_gnu_tree (gnat_entity))
330 gnu_decl = get_gnu_tree (gnat_entity);
332 if (TREE_CODE (gnu_decl) == TYPE_DECL
333 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
334 && IN (kind, Incomplete_Or_Private_Kind)
335 && Present (Full_View (gnat_entity))
336 && (present_gnu_tree (Full_View (gnat_entity))
337 || No (Freeze_Node (Full_View (gnat_entity)))))
340 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
341 save_gnu_tree (gnat_entity, NULL_TREE, false);
342 save_gnu_tree (gnat_entity, gnu_decl, false);
348 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
349 must be specified unless it was specified by the programmer. Exceptions
350 are for access-to-protected-subprogram types and all access subtypes, as
351 another GNAT type is used to lay out the GCC type for them. */
352 gcc_assert (!Unknown_Esize (gnat_entity)
353 || Has_Size_Clause (gnat_entity)
354 || (!IN (kind, Numeric_Kind)
355 && !IN (kind, Enumeration_Kind)
356 && (!IN (kind, Access_Kind)
357 || kind == E_Access_Protected_Subprogram_Type
358 || kind == E_Anonymous_Access_Protected_Subprogram_Type
359 || kind == E_Access_Subtype
360 || type_annotate_only)));
362 /* The RM size must be specified for all discrete and fixed-point types. */
363 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
364 && Unknown_RM_Size (gnat_entity)));
366 /* If we get here, it means we have not yet done anything with this entity.
367 If we are not defining it, it must be a type or an entity that is defined
368 elsewhere or externally, otherwise we should have defined it already. */
369 gcc_assert (definition
370 || type_annotate_only
372 || kind == E_Discriminant
373 || kind == E_Component
375 || (kind == E_Constant && Present (Full_View (gnat_entity)))
376 || Is_Public (gnat_entity));
378 /* Get the name of the entity and set up the line number and filename of
379 the original definition for use in any decl we make. Make sure we do not
380 inherit another source location. */
381 gnu_entity_name = get_entity_name (gnat_entity);
382 if (Sloc (gnat_entity) != No_Location
383 && !renaming_from_generic_instantiation_p (gnat_entity))
384 Sloc_to_locus (Sloc (gnat_entity), &input_location);
386 /* For cases when we are not defining (i.e., we are referencing from
387 another compilation unit) public entities, show we are at global level
388 for the purpose of computing scopes. Don't do this for components or
389 discriminants since the relevant test is whether or not the record is
392 && kind != E_Component
393 && kind != E_Discriminant
394 && Is_Public (gnat_entity)
395 && !Is_Statically_Allocated (gnat_entity))
396 force_global++, this_global = true;
398 /* Handle any attributes directly attached to the entity. */
399 if (Has_Gigi_Rep_Item (gnat_entity))
400 prepend_attributes (&attr_list, gnat_entity);
402 /* Do some common processing for types. */
405 /* Compute the equivalent type to be used in gigi. */
406 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
408 /* Machine_Attributes on types are expected to be propagated to
409 subtypes. The corresponding Gigi_Rep_Items are only attached
410 to the first subtype though, so we handle the propagation here. */
411 if (Base_Type (gnat_entity) != gnat_entity
412 && !Is_First_Subtype (gnat_entity)
413 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
414 prepend_attributes (&attr_list,
415 First_Subtype (Base_Type (gnat_entity)));
417 /* Compute a default value for the size of an elementary type. */
418 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
420 unsigned int max_esize;
422 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
423 esize = UI_To_Int (Esize (gnat_entity));
425 if (IN (kind, Float_Kind))
426 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
427 else if (IN (kind, Access_Kind))
428 max_esize = POINTER_SIZE * 2;
430 max_esize = LONG_LONG_TYPE_SIZE;
432 if (esize > max_esize)
442 /* The GNAT record where the component was defined. */
443 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
445 /* If the entity is a discriminant of an extended tagged type used to
446 rename a discriminant of the parent type, return the latter. */
447 if (Is_Tagged_Type (gnat_record)
448 && Present (Corresponding_Discriminant (gnat_entity)))
451 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
452 gnu_expr, definition);
457 /* If the entity is an inherited component (in the case of extended
458 tagged record types), just return the original entity, which must
459 be a FIELD_DECL. Likewise for discriminants. If the entity is a
460 non-girder discriminant (in the case of derived untagged record
461 types), return the stored discriminant it renames. */
462 else if (Present (Original_Record_Component (gnat_entity))
463 && Original_Record_Component (gnat_entity) != gnat_entity)
466 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
467 gnu_expr, definition);
472 /* Otherwise, if we are not defining this and we have no GCC type
473 for the containing record, make one for it. Then we should
474 have made our own equivalent. */
475 else if (!definition && !present_gnu_tree (gnat_record))
477 /* ??? If this is in a record whose scope is a protected
478 type and we have an Original_Record_Component, use it.
479 This is a workaround for major problems in protected type
481 Entity_Id Scop = Scope (Scope (gnat_entity));
482 if (Is_Protected_Type (Underlying_Type (Scop))
483 && Present (Original_Record_Component (gnat_entity)))
486 = gnat_to_gnu_entity (Original_Record_Component
493 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
494 gnu_decl = get_gnu_tree (gnat_entity);
500 /* Here we have no GCC type and this is a reference rather than a
501 definition. This should never happen. Most likely the cause is
502 reference before declaration in the GNAT tree for gnat_entity. */
507 /* Ignore constant definitions already marked with the error node. See
508 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
510 && present_gnu_tree (gnat_entity)
511 && get_gnu_tree (gnat_entity) == error_mark_node)
513 maybe_present = true;
517 /* Ignore deferred constant definitions without address clause since
518 they are processed fully in the front-end. If No_Initialization
519 is set, this is not a deferred constant but a constant whose value
520 is built manually. And constants that are renamings are handled
524 && No (Address_Clause (gnat_entity))
525 && !No_Initialization (Declaration_Node (gnat_entity))
526 && No (Renamed_Object (gnat_entity)))
528 gnu_decl = error_mark_node;
533 /* If this is a use of a deferred constant without address clause,
534 get its full definition. */
536 && No (Address_Clause (gnat_entity))
537 && Present (Full_View (gnat_entity)))
540 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
545 /* If we have a constant that we are not defining, get the expression it
546 was defined to represent. This is necessary to avoid generating dumb
547 elaboration code in simple cases, but we may throw it away later if it
548 is not a constant. But do not retrieve it if it is an allocator since
549 the designated type might still be dummy at this point. */
551 && !No_Initialization (Declaration_Node (gnat_entity))
552 && Present (Expression (Declaration_Node (gnat_entity)))
553 && Nkind (Expression (Declaration_Node (gnat_entity)))
555 /* The expression may contain N_Expression_With_Actions nodes and
556 thus object declarations from other units. Discard them. */
558 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity)));
560 /* ... fall through ... */
563 case E_Loop_Parameter:
564 case E_Out_Parameter:
567 /* Always create a variable for volatile objects and variables seen
568 constant but with a Linker_Section pragma. */
570 = ((kind == E_Constant || kind == E_Variable)
571 && Is_True_Constant (gnat_entity)
572 && !(kind == E_Variable
573 && Present (Linker_Section_Pragma (gnat_entity)))
574 && !Treat_As_Volatile (gnat_entity)
575 && (((Nkind (Declaration_Node (gnat_entity))
576 == N_Object_Declaration)
577 && Present (Expression (Declaration_Node (gnat_entity))))
578 || Present (Renamed_Object (gnat_entity))
580 bool inner_const_flag = const_flag;
581 bool static_flag = Is_Statically_Allocated (gnat_entity);
582 /* We implement RM 13.3(19) for exported and imported (non-constant)
583 objects by making them volatile. */
585 = (Treat_As_Volatile (gnat_entity)
586 || (!const_flag && (Is_Exported (gnat_entity) || imported_p)));
587 bool mutable_p = false;
588 bool used_by_ref = false;
589 tree gnu_ext_name = NULL_TREE;
590 tree renamed_obj = NULL_TREE;
591 tree gnu_object_size;
593 /* We need to translate the renamed object even though we are only
594 referencing the renaming. But it may contain a call for which
595 we'll generate a temporary to hold the return value and which
596 is part of the definition of the renaming, so discard it. */
597 if (Present (Renamed_Object (gnat_entity)) && !definition)
599 if (kind == E_Exception)
600 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
603 gnu_expr = gnat_to_gnu_external (Renamed_Object (gnat_entity));
606 /* Get the type after elaborating the renamed object. */
607 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
609 /* If this is a standard exception definition, then use the standard
610 exception type. This is necessary to make sure that imported and
611 exported views of exceptions are properly merged in LTO mode. */
612 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
613 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
614 gnu_type = except_type_node;
616 /* For a debug renaming declaration, build a debug-only entity. */
617 if (Present (Debug_Renaming_Link (gnat_entity)))
619 /* Force a non-null value to make sure the symbol is retained. */
620 tree value = build1 (INDIRECT_REF, gnu_type,
622 build_pointer_type (gnu_type),
623 integer_minus_one_node));
624 gnu_decl = build_decl (input_location,
625 VAR_DECL, gnu_entity_name, gnu_type);
626 SET_DECL_VALUE_EXPR (gnu_decl, value);
627 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
628 gnat_pushdecl (gnu_decl, gnat_entity);
632 /* If this is a loop variable, its type should be the base type.
633 This is because the code for processing a loop determines whether
634 a normal loop end test can be done by comparing the bounds of the
635 loop against those of the base type, which is presumed to be the
636 size used for computation. But this is not correct when the size
637 of the subtype is smaller than the type. */
638 if (kind == E_Loop_Parameter)
639 gnu_type = get_base_type (gnu_type);
641 /* Reject non-renamed objects whose type is an unconstrained array or
642 any object whose type is a dummy type or void. */
643 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
644 && No (Renamed_Object (gnat_entity)))
645 || TYPE_IS_DUMMY_P (gnu_type)
646 || TREE_CODE (gnu_type) == VOID_TYPE)
648 gcc_assert (type_annotate_only);
651 return error_mark_node;
654 /* If an alignment is specified, use it if valid. Note that exceptions
655 are objects but don't have an alignment. We must do this before we
656 validate the size, since the alignment can affect the size. */
657 if (kind != E_Exception && Known_Alignment (gnat_entity))
659 gcc_assert (Present (Alignment (gnat_entity)));
661 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
662 TYPE_ALIGN (gnu_type));
664 /* No point in changing the type if there is an address clause
665 as the final type of the object will be a reference type. */
666 if (Present (Address_Clause (gnat_entity)))
670 tree orig_type = gnu_type;
673 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
674 false, false, definition, true);
676 /* If a padding record was made, declare it now since it will
677 never be declared otherwise. This is necessary to ensure
678 that its subtrees are properly marked. */
679 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
680 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
681 debug_info_p, gnat_entity);
685 /* If we are defining the object, see if it has a Size and validate it
686 if so. If we are not defining the object and a Size clause applies,
687 simply retrieve the value. We don't want to ignore the clause and
688 it is expected to have been validated already. Then get the new
691 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
692 gnat_entity, VAR_DECL, false,
693 Has_Size_Clause (gnat_entity));
694 else if (Has_Size_Clause (gnat_entity))
695 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
700 = make_type_from_size (gnu_type, gnu_size,
701 Has_Biased_Representation (gnat_entity));
703 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
704 gnu_size = NULL_TREE;
707 /* If this object has self-referential size, it must be a record with
708 a default discriminant. We are supposed to allocate an object of
709 the maximum size in this case, unless it is a constant with an
710 initializing expression, in which case we can get the size from
711 that. Note that the resulting size may still be a variable, so
712 this may end up with an indirect allocation. */
713 if (No (Renamed_Object (gnat_entity))
714 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
716 if (gnu_expr && kind == E_Constant)
718 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
719 if (CONTAINS_PLACEHOLDER_P (size))
721 /* If the initializing expression is itself a constant,
722 despite having a nominal type with self-referential
723 size, we can get the size directly from it. */
724 if (TREE_CODE (gnu_expr) == COMPONENT_REF
726 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
727 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
728 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
729 || DECL_READONLY_ONCE_ELAB
730 (TREE_OPERAND (gnu_expr, 0))))
731 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
734 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
739 /* We may have no GNU_EXPR because No_Initialization is
740 set even though there's an Expression. */
741 else if (kind == E_Constant
742 && (Nkind (Declaration_Node (gnat_entity))
743 == N_Object_Declaration)
744 && Present (Expression (Declaration_Node (gnat_entity))))
746 = TYPE_SIZE (gnat_to_gnu_type
748 (Expression (Declaration_Node (gnat_entity)))));
751 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
755 /* If we are at global level and the size isn't constant, call
756 elaborate_expression_1 to make a variable for it rather than
757 calculating it each time. */
758 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
759 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
760 "SIZE", definition, false);
763 /* If the size is zero byte, make it one byte since some linkers have
764 troubles with zero-sized objects. If the object will have a
765 template, that will make it nonzero so don't bother. Also avoid
766 doing that for an object renaming or an object with an address
767 clause, as we would lose useful information on the view size
768 (e.g. for null array slices) and we are not allocating the object
771 && integer_zerop (gnu_size)
772 && !TREE_OVERFLOW (gnu_size))
773 || (TYPE_SIZE (gnu_type)
774 && integer_zerop (TYPE_SIZE (gnu_type))
775 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
776 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
777 && No (Renamed_Object (gnat_entity))
778 && No (Address_Clause (gnat_entity)))
779 gnu_size = bitsize_unit_node;
781 /* If this is an object with no specified size and alignment, and
782 if either it is atomic or we are not optimizing alignment for
783 space and it is composite and not an exception, an Out parameter
784 or a reference to another object, and the size of its type is a
785 constant, set the alignment to the smallest one which is not
786 smaller than the size, with an appropriate cap. */
787 if (!gnu_size && align == 0
788 && (Is_Atomic_Or_VFA (gnat_entity)
789 || (!Optimize_Alignment_Space (gnat_entity)
790 && kind != E_Exception
791 && kind != E_Out_Parameter
792 && Is_Composite_Type (Etype (gnat_entity))
793 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
794 && !Is_Exported (gnat_entity)
796 && No (Renamed_Object (gnat_entity))
797 && No (Address_Clause (gnat_entity))))
798 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
800 unsigned int size_cap, align_cap;
802 /* No point in promoting the alignment if this doesn't prevent
803 BLKmode access to the object, in particular block copy, as
804 this will for example disable the NRV optimization for it.
805 No point in jumping through all the hoops needed in order
806 to support BIGGEST_ALIGNMENT if we don't really have to.
807 So we cap to the smallest alignment that corresponds to
808 a known efficient memory access pattern of the target. */
809 if (Is_Atomic_Or_VFA (gnat_entity))
812 align_cap = BIGGEST_ALIGNMENT;
816 size_cap = MAX_FIXED_MODE_SIZE;
817 align_cap = get_mode_alignment (ptr_mode);
820 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
821 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
823 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
826 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
828 /* But make sure not to under-align the object. */
829 if (align <= TYPE_ALIGN (gnu_type))
832 /* And honor the minimum valid atomic alignment, if any. */
833 #ifdef MINIMUM_ATOMIC_ALIGNMENT
834 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
835 align = MINIMUM_ATOMIC_ALIGNMENT;
839 /* If the object is set to have atomic components, find the component
840 type and validate it.
842 ??? Note that we ignore Has_Volatile_Components on objects; it's
843 not at all clear what to do in that case. */
844 if (Has_Atomic_Components (gnat_entity))
846 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
847 ? TREE_TYPE (gnu_type) : gnu_type);
849 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
850 && TYPE_MULTI_ARRAY_P (gnu_inner))
851 gnu_inner = TREE_TYPE (gnu_inner);
853 check_ok_for_atomic_type (gnu_inner, gnat_entity, true);
856 /* If this is an aliased object with an unconstrained array nominal
857 subtype, make a type that includes the template. We will either
858 allocate or create a variable of that type, see below. */
859 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
860 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
861 && !type_annotate_only)
864 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
866 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
868 concat_name (gnu_entity_name,
873 /* ??? If this is an object of CW type initialized to a value, try to
874 ensure that the object is sufficient aligned for this value, but
875 without pessimizing the allocation. This is a kludge necessary
876 because we don't support dynamic alignment. */
878 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
879 && No (Renamed_Object (gnat_entity))
880 && No (Address_Clause (gnat_entity)))
881 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
883 #ifdef MINIMUM_ATOMIC_ALIGNMENT
884 /* If the size is a constant and no alignment is specified, force
885 the alignment to be the minimum valid atomic alignment. The
886 restriction on constant size avoids problems with variable-size
887 temporaries; if the size is variable, there's no issue with
888 atomic access. Also don't do this for a constant, since it isn't
889 necessary and can interfere with constant replacement. Finally,
890 do not do it for Out parameters since that creates an
891 size inconsistency with In parameters. */
893 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
894 && !FLOAT_TYPE_P (gnu_type)
895 && !const_flag && No (Renamed_Object (gnat_entity))
896 && !imported_p && No (Address_Clause (gnat_entity))
897 && kind != E_Out_Parameter
898 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
899 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
900 align = MINIMUM_ATOMIC_ALIGNMENT;
903 /* Make a new type with the desired size and alignment, if needed.
904 But do not take into account alignment promotions to compute the
905 size of the object. */
906 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
907 if (gnu_size || align > 0)
909 tree orig_type = gnu_type;
911 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
912 false, false, definition, true);
914 /* If a padding record was made, declare it now since it will
915 never be declared otherwise. This is necessary to ensure
916 that its subtrees are properly marked. */
917 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
918 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
919 debug_info_p, gnat_entity);
922 /* Now check if the type of the object allows atomic access. */
923 if (Is_Atomic_Or_VFA (gnat_entity))
924 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
926 /* If this is a renaming, avoid as much as possible to create a new
927 object. However, in some cases, creating it is required because
928 renaming can be applied to objects that are not names in Ada.
929 This processing needs to be applied to the raw expression so as
930 to make it more likely to rename the underlying object. */
931 if (Present (Renamed_Object (gnat_entity)))
933 /* If the renamed object had padding, strip off the reference to
934 the inner object and reset our type. */
935 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
936 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
937 /* Strip useless conversions around the object. */
938 || gnat_useless_type_conversion (gnu_expr))
940 gnu_expr = TREE_OPERAND (gnu_expr, 0);
941 gnu_type = TREE_TYPE (gnu_expr);
944 /* Or else, if the renamed object has an unconstrained type with
945 default discriminant, use the padded type. */
946 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr)))
947 gnu_type = TREE_TYPE (gnu_expr);
949 /* Case 1: if this is a constant renaming stemming from a function
950 call, treat it as a normal object whose initial value is what
951 is being renamed. RM 3.3 says that the result of evaluating a
952 function call is a constant object. Therefore, it can be the
953 inner object of a constant renaming and the renaming must be
954 fully instantiated, i.e. it cannot be a reference to (part of)
955 an existing object. And treat other rvalues (addresses, null
956 expressions, constructors and literals) the same way. */
957 tree inner = gnu_expr;
958 while (handled_component_p (inner) || CONVERT_EXPR_P (inner))
959 inner = TREE_OPERAND (inner, 0);
960 /* Expand_Dispatching_Call can prepend a comparison of the tags
961 before the call to "=". */
962 if (TREE_CODE (inner) == TRUTH_ANDIF_EXPR
963 || TREE_CODE (inner) == COMPOUND_EXPR)
964 inner = TREE_OPERAND (inner, 1);
965 if ((TREE_CODE (inner) == CALL_EXPR
966 && !call_is_atomic_load (inner))
967 || TREE_CODE (inner) == ADDR_EXPR
968 || TREE_CODE (inner) == NULL_EXPR
969 || TREE_CODE (inner) == PLUS_EXPR
970 || TREE_CODE (inner) == CONSTRUCTOR
971 || CONSTANT_CLASS_P (inner)
972 /* We need to detect the case where a temporary is created to
973 hold the return value, since we cannot safely rename it at
974 top level as it lives only in the elaboration routine. */
975 || (TREE_CODE (inner) == VAR_DECL
976 && DECL_RETURN_VALUE_P (inner))
977 /* We also need to detect the case where the front-end creates
978 a dangling 'reference to a function call at top level and
979 substitutes it in the renaming, for example:
981 q__b : boolean renames r__f.e (1);
983 can be rewritten into:
985 q__R1s : constant q__A2s := r__f'reference;
987 q__b : boolean renames q__R1s.all.e (1);
989 We cannot safely rename the rewritten expression since the
990 underlying object lives only in the elaboration routine. */
991 || (TREE_CODE (inner) == INDIRECT_REF
993 = remove_conversions (TREE_OPERAND (inner, 0), true))
994 && TREE_CODE (inner) == VAR_DECL
995 && DECL_RETURN_VALUE_P (inner)))
998 /* Case 2: if the renaming entity need not be materialized, use
999 the elaborated renamed expression for the renaming. But this
1000 means that the caller is responsible for evaluating the address
1001 of the renaming in the correct place for the definition case to
1002 instantiate the SAVE_EXPRs. */
1003 else if (!Materialize_Entity (gnat_entity))
1005 tree init = NULL_TREE;
1008 = elaborate_reference (gnu_expr, gnat_entity, definition,
1011 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1012 correct place for this case. */
1015 /* No DECL_EXPR will be created so the expression needs to be
1016 marked manually because it will likely be shared. */
1017 if (global_bindings_p ())
1018 MARK_VISITED (gnu_decl);
1020 /* This assertion will fail if the renamed object isn't aligned
1021 enough as to make it possible to honor the alignment set on
1025 unsigned int ralign = DECL_P (gnu_decl)
1026 ? DECL_ALIGN (gnu_decl)
1027 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1028 gcc_assert (ralign >= align);
1031 save_gnu_tree (gnat_entity, gnu_decl, true);
1033 annotate_object (gnat_entity, gnu_type, NULL_TREE, false);
1037 /* Case 3: otherwise, make a constant pointer to the object we
1038 are renaming and attach the object to the pointer after it is
1039 elaborated. The object will be referenced directly instead
1040 of indirectly via the pointer to avoid aliasing problems with
1041 non-addressable entities. The pointer is called a "renaming"
1042 pointer in this case. Note that we also need to preserve the
1043 volatility of the renamed object through the indirection. */
1046 tree init = NULL_TREE;
1048 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1050 = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1051 gnu_type = build_reference_type (gnu_type);
1054 volatile_flag = false;
1055 inner_const_flag = TREE_READONLY (gnu_expr);
1056 gnu_size = NULL_TREE;
1059 = elaborate_reference (gnu_expr, gnat_entity, definition,
1062 /* The expression needs to be marked manually because it will
1063 likely be shared, even for a definition since the ADDR_EXPR
1064 built below can cause the first few nodes to be folded. */
1065 if (global_bindings_p ())
1066 MARK_VISITED (renamed_obj);
1068 if (type_annotate_only
1069 && TREE_CODE (renamed_obj) == ERROR_MARK)
1070 gnu_expr = NULL_TREE;
1074 = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
1077 = build_compound_expr (TREE_TYPE (gnu_expr), init,
1083 /* If we are defining an aliased object whose nominal subtype is
1084 unconstrained, the object is a record that contains both the
1085 template and the object. If there is an initializer, it will
1086 have already been converted to the right type, but we need to
1087 create the template if there is no initializer. */
1090 && TREE_CODE (gnu_type) == RECORD_TYPE
1091 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1092 /* Beware that padding might have been introduced above. */
1093 || (TYPE_PADDING_P (gnu_type)
1094 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1096 && TYPE_CONTAINS_TEMPLATE_P
1097 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1100 = TYPE_PADDING_P (gnu_type)
1101 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1102 : TYPE_FIELDS (gnu_type);
1103 vec<constructor_elt, va_gc> *v;
1105 tree t = build_template (TREE_TYPE (template_field),
1106 TREE_TYPE (DECL_CHAIN (template_field)),
1108 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1109 gnu_expr = gnat_build_constructor (gnu_type, v);
1112 /* Convert the expression to the type of the object if need be. */
1113 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1114 gnu_expr = convert (gnu_type, gnu_expr);
1116 /* If this is a pointer that doesn't have an initializing expression,
1117 initialize it to NULL, unless the object is declared imported as
1120 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1122 && !Is_Imported (gnat_entity))
1123 gnu_expr = integer_zero_node;
1125 /* If we are defining the object and it has an Address clause, we must
1126 either get the address expression from the saved GCC tree for the
1127 object if it has a Freeze node, or elaborate the address expression
1128 here since the front-end has guaranteed that the elaboration has no
1129 effects in this case. */
1130 if (definition && Present (Address_Clause (gnat_entity)))
1132 const Node_Id gnat_clause = Address_Clause (gnat_entity);
1133 Node_Id gnat_expr = Expression (gnat_clause);
1135 = present_gnu_tree (gnat_entity)
1136 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1138 save_gnu_tree (gnat_entity, NULL_TREE, false);
1140 /* Convert the type of the object to a reference type that can
1141 alias everything as per RM 13.3(19). */
1142 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1143 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1145 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1146 gnu_address = convert (gnu_type, gnu_address);
1149 = (!Is_Public (gnat_entity)
1150 || compile_time_known_address_p (gnat_expr));
1151 volatile_flag = false;
1152 gnu_size = NULL_TREE;
1154 /* If this is an aliased object with an unconstrained array nominal
1155 subtype, then it can overlay only another aliased object with an
1156 unconstrained array nominal subtype and compatible template. */
1157 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1158 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1159 && !type_annotate_only)
1161 tree rec_type = TREE_TYPE (gnu_type);
1162 tree off = byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type)));
1164 /* This is the pattern built for a regular object. */
1165 if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1166 && TREE_OPERAND (gnu_address, 1) == off)
1167 gnu_address = TREE_OPERAND (gnu_address, 0);
1168 /* This is the pattern built for an overaligned object. */
1169 else if (TREE_CODE (gnu_address) == POINTER_PLUS_EXPR
1170 && TREE_CODE (TREE_OPERAND (gnu_address, 1))
1172 && TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 1)
1175 = build2 (POINTER_PLUS_EXPR, gnu_type,
1176 TREE_OPERAND (gnu_address, 0),
1177 TREE_OPERAND (TREE_OPERAND (gnu_address, 1), 0));
1180 post_error_ne ("aliased object& with unconstrained array "
1181 "nominal subtype", gnat_clause,
1183 post_error ("\\can overlay only aliased object with "
1184 "compatible subtype", gnat_clause);
1188 /* If we don't have an initializing expression for the underlying
1189 variable, the initializing expression for the pointer is the
1190 specified address. Otherwise, we have to make a COMPOUND_EXPR
1191 to assign both the address and the initial value. */
1193 gnu_expr = gnu_address;
1196 = build2 (COMPOUND_EXPR, gnu_type,
1197 build_binary_op (INIT_EXPR, NULL_TREE,
1198 build_unary_op (INDIRECT_REF,
1205 /* If it has an address clause and we are not defining it, mark it
1206 as an indirect object. Likewise for Stdcall objects that are
1208 if ((!definition && Present (Address_Clause (gnat_entity)))
1209 || (imported_p && Has_Stdcall_Convention (gnat_entity)))
1211 /* Convert the type of the object to a reference type that can
1212 alias everything as per RM 13.3(19). */
1213 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1214 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1216 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1219 volatile_flag = false;
1220 gnu_size = NULL_TREE;
1222 /* No point in taking the address of an initializing expression
1223 that isn't going to be used. */
1224 gnu_expr = NULL_TREE;
1226 /* If it has an address clause whose value is known at compile
1227 time, make the object a CONST_DECL. This will avoid a
1228 useless dereference. */
1229 if (Present (Address_Clause (gnat_entity)))
1231 Node_Id gnat_address
1232 = Expression (Address_Clause (gnat_entity));
1234 if (compile_time_known_address_p (gnat_address))
1236 gnu_expr = gnat_to_gnu (gnat_address);
1242 /* If we are at top level and this object is of variable size,
1243 make the actual type a hidden pointer to the real type and
1244 make the initializer be a memory allocation and initialization.
1245 Likewise for objects we aren't defining (presumed to be
1246 external references from other packages), but there we do
1247 not set up an initialization.
1249 If the object's size overflows, make an allocator too, so that
1250 Storage_Error gets raised. Note that we will never free
1251 such memory, so we presume it never will get allocated. */
1252 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1253 global_bindings_p ()
1257 && !allocatable_size_p (convert (sizetype,
1259 (CEIL_DIV_EXPR, gnu_size,
1260 bitsize_unit_node)),
1261 global_bindings_p ()
1265 if (volatile_flag && !TYPE_VOLATILE (gnu_type))
1266 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_VOLATILE);
1267 gnu_type = build_reference_type (gnu_type);
1270 volatile_flag = false;
1271 gnu_size = NULL_TREE;
1273 /* In case this was a aliased object whose nominal subtype is
1274 unconstrained, the pointer above will be a thin pointer and
1275 build_allocator will automatically make the template.
1277 If we have a template initializer only (that we made above),
1278 pretend there is none and rely on what build_allocator creates
1279 again anyway. Otherwise (if we have a full initializer), get
1280 the data part and feed that to build_allocator.
1282 If we are elaborating a mutable object, tell build_allocator to
1283 ignore a possibly simpler size from the initializer, if any, as
1284 we must allocate the maximum possible size in this case. */
1285 if (definition && !imported_p)
1287 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1289 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1290 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1293 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1295 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1296 && vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)) == 1)
1297 gnu_expr = NULL_TREE;
1300 = build_component_ref
1302 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1306 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1307 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1308 post_error ("?`Storage_Error` will be raised at run time!",
1312 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1313 Empty, Empty, gnat_entity, mutable_p);
1316 gnu_expr = NULL_TREE;
1319 /* If this object would go into the stack and has an alignment larger
1320 than the largest stack alignment the back-end can honor, resort to
1321 a variable of "aligning type". */
1323 && !global_bindings_p ()
1326 && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1328 /* Create the new variable. No need for extra room before the
1329 aligned field as this is in automatic storage. */
1331 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1332 TYPE_SIZE_UNIT (gnu_type),
1333 BIGGEST_ALIGNMENT, 0, gnat_entity);
1335 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1336 NULL_TREE, gnu_new_type, NULL_TREE,
1337 false, false, false, false, false,
1338 true, debug_info_p, NULL, gnat_entity);
1340 /* Initialize the aligned field if we have an initializer. */
1343 (build_binary_op (INIT_EXPR, NULL_TREE,
1345 (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1350 /* And setup this entity as a reference to the aligned field. */
1351 gnu_type = build_reference_type (gnu_type);
1354 (ADDR_EXPR, NULL_TREE,
1355 build_component_ref (gnu_new_var, TYPE_FIELDS (gnu_new_type),
1357 TREE_CONSTANT (gnu_expr) = 1;
1361 volatile_flag = false;
1362 gnu_size = NULL_TREE;
1365 /* If this is an aliased object with an unconstrained array nominal
1366 subtype, we make its type a thin reference, i.e. the reference
1367 counterpart of a thin pointer, so it points to the array part.
1368 This is aimed to make it easier for the debugger to decode the
1369 object. Note that we have to do it this late because of the
1370 couple of allocation adjustments that might be made above. */
1371 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1372 && Is_Array_Type (Underlying_Type (Etype (gnat_entity)))
1373 && !type_annotate_only)
1375 /* In case the object with the template has already been allocated
1376 just above, we have nothing to do here. */
1377 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1379 /* This variable is a GNAT encoding used by Workbench: let it
1380 go through the debugging information but mark it as
1381 artificial: users are not interested in it. */
1383 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1384 NULL_TREE, gnu_type, gnu_expr,
1385 const_flag, Is_Public (gnat_entity),
1386 imported_p || !definition, static_flag,
1387 volatile_flag, true, debug_info_p,
1389 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1390 TREE_CONSTANT (gnu_expr) = 1;
1394 volatile_flag = false;
1395 inner_const_flag = TREE_READONLY (gnu_unc_var);
1396 gnu_size = NULL_TREE;
1400 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1402 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1406 gnu_type = change_qualified_type (gnu_type, TYPE_QUAL_CONST);
1408 /* Convert the expression to the type of the object if need be. */
1409 if (gnu_expr && initial_value_needs_conversion (gnu_type, gnu_expr))
1410 gnu_expr = convert (gnu_type, gnu_expr);
1412 /* If this name is external or a name was specified, use it, but don't
1413 use the Interface_Name with an address clause (see cd30005). */
1414 if ((Is_Public (gnat_entity) && !Is_Imported (gnat_entity))
1415 || (Present (Interface_Name (gnat_entity))
1416 && No (Address_Clause (gnat_entity))))
1417 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1419 /* If this is an aggregate constant initialized to a constant, force it
1420 to be statically allocated. This saves an initialization copy. */
1423 && gnu_expr && TREE_CONSTANT (gnu_expr)
1424 && AGGREGATE_TYPE_P (gnu_type)
1425 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1426 && !(TYPE_IS_PADDING_P (gnu_type)
1427 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1428 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1431 /* Deal with a pragma Linker_Section on a constant or variable. */
1432 if ((kind == E_Constant || kind == E_Variable)
1433 && Present (Linker_Section_Pragma (gnat_entity)))
1434 prepend_one_attribute_pragma (&attr_list,
1435 Linker_Section_Pragma (gnat_entity));
1437 /* Now create the variable or the constant and set various flags. */
1439 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1440 gnu_expr, const_flag, Is_Public (gnat_entity),
1441 imported_p || !definition, static_flag,
1442 volatile_flag, artificial_p, debug_info_p,
1443 attr_list, gnat_entity, !renamed_obj);
1444 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1445 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1446 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1448 /* If we are defining an Out parameter and optimization isn't enabled,
1449 create a fake PARM_DECL for debugging purposes and make it point to
1450 the VAR_DECL. Suppress debug info for the latter but make sure it
1451 will live in memory so that it can be accessed from within the
1452 debugger through the PARM_DECL. */
1453 if (kind == E_Out_Parameter
1457 && !flag_generate_lto)
1459 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1460 gnat_pushdecl (param, gnat_entity);
1461 SET_DECL_VALUE_EXPR (param, gnu_decl);
1462 DECL_HAS_VALUE_EXPR_P (param) = 1;
1463 DECL_IGNORED_P (gnu_decl) = 1;
1464 TREE_ADDRESSABLE (gnu_decl) = 1;
1467 /* If this is a loop parameter, set the corresponding flag. */
1468 else if (kind == E_Loop_Parameter)
1469 DECL_LOOP_PARM_P (gnu_decl) = 1;
1471 /* If this is a renaming pointer, attach the renamed object to it. */
1473 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1475 /* If this is a constant and we are defining it or it generates a real
1476 symbol at the object level and we are referencing it, we may want
1477 or need to have a true variable to represent it:
1478 - if optimization isn't enabled, for debugging purposes,
1479 - if the constant is public and not overlaid on something else,
1480 - if its address is taken,
1481 - if either itself or its type is aliased. */
1482 if (TREE_CODE (gnu_decl) == CONST_DECL
1483 && (definition || Sloc (gnat_entity) > Standard_Location)
1484 && ((!optimize && debug_info_p)
1485 || (Is_Public (gnat_entity)
1486 && No (Address_Clause (gnat_entity)))
1487 || Address_Taken (gnat_entity)
1488 || Is_Aliased (gnat_entity)
1489 || Is_Aliased (Etype (gnat_entity))))
1492 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1493 gnu_expr, true, Is_Public (gnat_entity),
1494 !definition, static_flag, volatile_flag,
1495 artificial_p, debug_info_p, attr_list,
1496 gnat_entity, false);
1498 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1501 /* If this is a constant, even if we don't need a true variable, we
1502 may need to avoid returning the initializer in every case. That
1503 can happen for the address of a (constant) constructor because,
1504 upon dereferencing it, the constructor will be reinjected in the
1505 tree, which may not be valid in every case; see lvalue_required_p
1506 for more details. */
1507 if (TREE_CODE (gnu_decl) == CONST_DECL)
1508 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1510 /* If this object is declared in a block that contains a block with an
1511 exception handler, and we aren't using the GCC exception mechanism,
1512 we must force this variable in memory in order to avoid an invalid
1514 if (Front_End_Exceptions ()
1515 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1516 TREE_ADDRESSABLE (gnu_decl) = 1;
1518 /* If this is a local variable with non-BLKmode and aggregate type,
1519 and optimization isn't enabled, then force it in memory so that
1520 a register won't be allocated to it with possible subparts left
1521 uninitialized and reaching the register allocator. */
1522 else if (TREE_CODE (gnu_decl) == VAR_DECL
1523 && !DECL_EXTERNAL (gnu_decl)
1524 && !TREE_STATIC (gnu_decl)
1525 && DECL_MODE (gnu_decl) != BLKmode
1526 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1527 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1529 TREE_ADDRESSABLE (gnu_decl) = 1;
1531 /* If we are defining an object with variable size or an object with
1532 fixed size that will be dynamically allocated, and we are using the
1533 front-end setjmp/longjmp exception mechanism, update the setjmp
1536 && Exception_Mechanism == Front_End_SJLJ
1537 && get_block_jmpbuf_decl ()
1538 && DECL_SIZE_UNIT (gnu_decl)
1539 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1540 || (flag_stack_check == GENERIC_STACK_CHECK
1541 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1542 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1543 add_stmt_with_node (build_call_n_expr
1544 (update_setjmp_buf_decl, 1,
1545 build_unary_op (ADDR_EXPR, NULL_TREE,
1546 get_block_jmpbuf_decl ())),
1549 /* Back-annotate Esize and Alignment of the object if not already
1550 known. Note that we pick the values of the type, not those of
1551 the object, to shield ourselves from low-level platform-dependent
1552 adjustments like alignment promotion. This is both consistent with
1553 all the treatment above, where alignment and size are set on the
1554 type of the object and not on the object directly, and makes it
1555 possible to support all confirming representation clauses. */
1556 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1562 /* Return a TYPE_DECL for "void" that we previously made. */
1563 gnu_decl = TYPE_NAME (void_type_node);
1566 case E_Enumeration_Type:
1567 /* A special case: for the types Character and Wide_Character in
1568 Standard, we do not list all the literals. So if the literals
1569 are not specified, make this an integer type. */
1570 if (No (First_Literal (gnat_entity)))
1572 if (esize == CHAR_TYPE_SIZE && flag_signed_char)
1573 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1575 gnu_type = make_unsigned_type (esize);
1576 TYPE_NAME (gnu_type) = gnu_entity_name;
1578 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1579 This is needed by the DWARF-2 back-end to distinguish between
1580 unsigned integer types and character types. */
1581 TYPE_STRING_FLAG (gnu_type) = 1;
1583 /* This flag is needed by the call just below. */
1584 TYPE_ARTIFICIAL (gnu_type) = artificial_p;
1586 finish_character_type (gnu_type);
1590 /* We have a list of enumeral constants in First_Literal. We make a
1591 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1592 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1593 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1594 value of the literal. But when we have a regular boolean type, we
1595 simplify this a little by using a BOOLEAN_TYPE. */
1596 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1597 && !Has_Non_Standard_Rep (gnat_entity);
1598 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1599 tree gnu_list = NULL_TREE;
1600 Entity_Id gnat_literal;
1602 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1603 TYPE_PRECISION (gnu_type) = esize;
1604 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1605 set_min_and_max_values_for_integral_type (gnu_type, esize,
1606 TYPE_SIGN (gnu_type));
1607 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1608 layout_type (gnu_type);
1610 for (gnat_literal = First_Literal (gnat_entity);
1611 Present (gnat_literal);
1612 gnat_literal = Next_Literal (gnat_literal))
1615 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1616 /* Do not generate debug info for individual enumerators. */
1618 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1619 gnu_type, gnu_value, true, false, false,
1620 false, false, artificial_p, false,
1621 NULL, gnat_literal);
1622 save_gnu_tree (gnat_literal, gnu_literal, false);
1624 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1628 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1630 /* Note that the bounds are updated at the end of this function
1631 to avoid an infinite recursion since they refer to the type. */
1636 case E_Signed_Integer_Type:
1637 /* For integer types, just make a signed type the appropriate number
1639 gnu_type = make_signed_type (esize);
1642 case E_Ordinary_Fixed_Point_Type:
1643 case E_Decimal_Fixed_Point_Type:
1645 /* Small_Value is the scale factor. */
1646 const Ureal gnat_small_value = Small_Value (gnat_entity);
1647 tree scale_factor = NULL_TREE;
1649 gnu_type = make_signed_type (esize);
1651 /* Try to decode the scale factor and to save it for the fixed-point
1652 types debug hook. */
1654 /* There are various ways to describe the scale factor, however there
1655 are cases where back-end internals cannot hold it. In such cases,
1656 we output invalid scale factor for such cases (i.e. the 0/0
1657 rational constant) but we expect GNAT to output GNAT encodings,
1658 then. Thus, keep this in sync with
1659 Exp_Dbug.Is_Handled_Scale_Factor. */
1661 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1662 binary or decimal scale: it is easier to read for humans. */
1663 if (UI_Eq (Numerator (gnat_small_value), Uint_1)
1664 && (Rbase (gnat_small_value) == 2
1665 || Rbase (gnat_small_value) == 10))
1667 /* Given RM restrictions on 'Small values, we assume here that
1668 the denominator fits in an int. */
1669 const tree base = build_int_cst (integer_type_node,
1670 Rbase (gnat_small_value));
1672 = build_int_cst (integer_type_node,
1673 UI_To_Int (Denominator (gnat_small_value)));
1675 = build2 (RDIV_EXPR, integer_type_node,
1677 build2 (POWER_EXPR, integer_type_node,
1681 /* Default to arbitrary scale factors descriptions. */
1684 const Uint num = Norm_Num (gnat_small_value);
1685 const Uint den = Norm_Den (gnat_small_value);
1687 if (UI_Is_In_Int_Range (num) && UI_Is_In_Int_Range (den))
1690 = build_int_cst (integer_type_node,
1691 UI_To_Int (Norm_Num (gnat_small_value)));
1693 = build_int_cst (integer_type_node,
1694 UI_To_Int (Norm_Den (gnat_small_value)));
1695 scale_factor = build2 (RDIV_EXPR, integer_type_node,
1699 /* If compiler internals cannot represent arbitrary scale
1700 factors, output an invalid scale factor so that debugger
1701 don't try to handle them but so that we still have a type
1702 in the output. Note that GNAT */
1703 scale_factor = integer_zero_node;
1706 TYPE_FIXED_POINT_P (gnu_type) = 1;
1707 SET_TYPE_SCALE_FACTOR (gnu_type, scale_factor);
1711 case E_Modular_Integer_Type:
1713 /* For modular types, make the unsigned type of the proper number
1714 of bits and then set up the modulus, if required. */
1715 tree gnu_modulus, gnu_high = NULL_TREE;
1717 /* Packed Array Impl. Types are supposed to be subtypes only. */
1718 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity));
1720 gnu_type = make_unsigned_type (esize);
1722 /* Get the modulus in this type. If it overflows, assume it is because
1723 it is equal to 2**Esize. Note that there is no overflow checking
1724 done on unsigned type, so we detect the overflow by looking for
1725 a modulus of zero, which is otherwise invalid. */
1726 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1728 if (!integer_zerop (gnu_modulus))
1730 TYPE_MODULAR_P (gnu_type) = 1;
1731 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1732 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1733 build_int_cst (gnu_type, 1));
1736 /* If the upper bound is not maximal, make an extra subtype. */
1738 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1740 tree gnu_subtype = make_unsigned_type (esize);
1741 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1742 TREE_TYPE (gnu_subtype) = gnu_type;
1743 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1744 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1745 gnu_type = gnu_subtype;
1750 case E_Signed_Integer_Subtype:
1751 case E_Enumeration_Subtype:
1752 case E_Modular_Integer_Subtype:
1753 case E_Ordinary_Fixed_Point_Subtype:
1754 case E_Decimal_Fixed_Point_Subtype:
1756 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1757 not want to call create_range_type since we would like each subtype
1758 node to be distinct. ??? Historically this was in preparation for
1759 when memory aliasing is implemented, but that's obsolete now given
1760 the call to relate_alias_sets below.
1762 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1763 this fact is used by the arithmetic conversion functions.
1765 We elaborate the Ancestor_Subtype if it is not in the current unit
1766 and one of our bounds is non-static. We do this to ensure consistent
1767 naming in the case where several subtypes share the same bounds, by
1768 elaborating the first such subtype first, thus using its name. */
1771 && Present (Ancestor_Subtype (gnat_entity))
1772 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1773 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1774 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1775 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1777 /* Set the precision to the Esize except for bit-packed arrays. */
1778 if (Is_Packed_Array_Impl_Type (gnat_entity)
1779 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1780 esize = UI_To_Int (RM_Size (gnat_entity));
1782 /* First subtypes of Character are treated as Character; otherwise
1783 this should be an unsigned type if the base type is unsigned or
1784 if the lower bound is constant and non-negative or if the type
1786 if (kind == E_Enumeration_Subtype
1787 && No (First_Literal (Etype (gnat_entity)))
1788 && Esize (gnat_entity) == RM_Size (gnat_entity)
1789 && esize == CHAR_TYPE_SIZE
1790 && flag_signed_char)
1791 gnu_type = make_signed_type (CHAR_TYPE_SIZE);
1792 else if (Is_Unsigned_Type (Etype (gnat_entity))
1793 || Is_Unsigned_Type (gnat_entity)
1794 || Has_Biased_Representation (gnat_entity))
1795 gnu_type = make_unsigned_type (esize);
1797 gnu_type = make_signed_type (esize);
1798 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1800 SET_TYPE_RM_MIN_VALUE
1801 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
1802 gnat_entity, "L", definition, true,
1805 SET_TYPE_RM_MAX_VALUE
1806 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
1807 gnat_entity, "U", definition, true,
1810 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1811 = Has_Biased_Representation (gnat_entity);
1813 /* Set TYPE_STRING_FLAG for Character and Wide_Character subtypes. */
1814 TYPE_STRING_FLAG (gnu_type) = TYPE_STRING_FLAG (TREE_TYPE (gnu_type));
1816 /* Inherit our alias set from what we're a subtype of. Subtypes
1817 are not different types and a pointer can designate any instance
1818 within a subtype hierarchy. */
1819 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1821 /* One of the above calls might have caused us to be elaborated,
1822 so don't blow up if so. */
1823 if (present_gnu_tree (gnat_entity))
1825 maybe_present = true;
1829 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1830 TYPE_STUB_DECL (gnu_type)
1831 = create_type_stub_decl (gnu_entity_name, gnu_type);
1833 /* For a packed array, make the original array type a parallel/debug
1835 if (debug_info_p && Is_Packed_Array_Impl_Type (gnat_entity))
1836 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1840 /* We have to handle clauses that under-align the type specially. */
1841 if ((Present (Alignment_Clause (gnat_entity))
1842 || (Is_Packed_Array_Impl_Type (gnat_entity)
1844 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1845 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1847 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1848 if (align >= TYPE_ALIGN (gnu_type))
1852 /* If the type we are dealing with represents a bit-packed array,
1853 we need to have the bits left justified on big-endian targets
1854 and right justified on little-endian targets. We also need to
1855 ensure that when the value is read (e.g. for comparison of two
1856 such values), we only get the good bits, since the unused bits
1857 are uninitialized. Both goals are accomplished by wrapping up
1858 the modular type in an enclosing record type. */
1859 if (Is_Packed_Array_Impl_Type (gnat_entity)
1860 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1862 tree gnu_field_type, gnu_field;
1864 /* Set the RM size before wrapping up the original type. */
1865 SET_TYPE_RM_SIZE (gnu_type,
1866 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1867 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1869 /* Strip the ___XP suffix for standard DWARF. */
1870 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1871 gnu_entity_name = TYPE_NAME (gnu_type);
1873 /* Create a stripped-down declaration, mainly for debugging. */
1874 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1877 /* Now save it and build the enclosing record type. */
1878 gnu_field_type = gnu_type;
1880 gnu_type = make_node (RECORD_TYPE);
1881 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1882 TYPE_PACKED (gnu_type) = 1;
1883 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1884 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1885 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1887 /* Propagate the alignment of the modular type to the record type,
1888 unless there is an alignment clause that under-aligns the type.
1889 This means that bit-packed arrays are given "ceil" alignment for
1890 their size by default, which may seem counter-intuitive but makes
1891 it possible to overlay them on modular types easily. */
1892 TYPE_ALIGN (gnu_type)
1893 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1895 /* Propagate the reverse storage order flag to the record type so
1896 that the required byte swapping is performed when retrieving the
1897 enclosed modular value. */
1898 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
1899 = Reverse_Storage_Order (Original_Array_Type (gnat_entity));
1901 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1903 /* Don't declare the field as addressable since we won't be taking
1904 its address and this would prevent create_field_decl from making
1907 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1908 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1910 /* Do not emit debug info until after the parallel type is added. */
1911 finish_record_type (gnu_type, gnu_field, 2, false);
1912 compute_record_mode (gnu_type);
1913 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1917 /* Make the original array type a parallel/debug type. */
1918 associate_original_type_to_packed_array (gnu_type, gnat_entity);
1920 /* Since GNU_TYPE is a padding type around the packed array
1921 implementation type, the padded type is its debug type. */
1922 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1923 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1925 rest_of_record_type_compilation (gnu_type);
1929 /* If the type we are dealing with has got a smaller alignment than the
1930 natural one, we need to wrap it up in a record type and misalign the
1931 latter; we reuse the padding machinery for this purpose. Note that,
1932 even if the record type is marked as packed because of misalignment,
1933 we don't pack the field so as to give it the size of the type. */
1936 tree gnu_field_type, gnu_field;
1938 /* Set the RM size before wrapping up the type. */
1939 SET_TYPE_RM_SIZE (gnu_type,
1940 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1942 /* Create a stripped-down declaration, mainly for debugging. */
1943 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1946 /* Now save it and build the enclosing record type. */
1947 gnu_field_type = gnu_type;
1949 gnu_type = make_node (RECORD_TYPE);
1950 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1951 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1952 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_field_type);
1953 TYPE_PACKED (gnu_type) = 1;
1954 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1955 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1956 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1957 TYPE_ALIGN (gnu_type) = align;
1958 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1960 /* Don't declare the field as addressable since we won't be taking
1961 its address and this would prevent create_field_decl from making
1964 = create_field_decl (get_identifier ("F"), gnu_field_type,
1965 gnu_type, TYPE_SIZE (gnu_field_type),
1966 bitsize_zero_node, 0, 0);
1968 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1969 compute_record_mode (gnu_type);
1970 TYPE_PADDING_P (gnu_type) = 1;
1975 case E_Floating_Point_Type:
1976 /* The type of the Low and High bounds can be our type if this is
1977 a type from Standard, so set them at the end of the function. */
1978 gnu_type = make_node (REAL_TYPE);
1979 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1980 layout_type (gnu_type);
1983 case E_Floating_Point_Subtype:
1984 /* See the E_Signed_Integer_Subtype case for the rationale. */
1986 && Present (Ancestor_Subtype (gnat_entity))
1987 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1988 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1989 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1990 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1992 gnu_type = make_node (REAL_TYPE);
1993 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1994 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1995 TYPE_GCC_MIN_VALUE (gnu_type)
1996 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1997 TYPE_GCC_MAX_VALUE (gnu_type)
1998 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1999 layout_type (gnu_type);
2001 SET_TYPE_RM_MIN_VALUE
2002 (gnu_type, elaborate_expression (Type_Low_Bound (gnat_entity),
2003 gnat_entity, "L", definition, true,
2006 SET_TYPE_RM_MAX_VALUE
2007 (gnu_type, elaborate_expression (Type_High_Bound (gnat_entity),
2008 gnat_entity, "U", definition, true,
2011 /* Inherit our alias set from what we're a subtype of, as for
2012 integer subtypes. */
2013 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2015 /* One of the above calls might have caused us to be elaborated,
2016 so don't blow up if so. */
2017 maybe_present = true;
2020 /* Array Types and Subtypes
2022 Unconstrained array types are represented by E_Array_Type and
2023 constrained array types are represented by E_Array_Subtype. There
2024 are no actual objects of an unconstrained array type; all we have
2025 are pointers to that type.
2027 The following fields are defined on array types and subtypes:
2029 Component_Type Component type of the array.
2030 Number_Dimensions Number of dimensions (an int).
2031 First_Index Type of first index. */
2035 const bool convention_fortran_p
2036 = (Convention (gnat_entity) == Convention_Fortran);
2037 const int ndim = Number_Dimensions (gnat_entity);
2038 tree gnu_template_type;
2039 tree gnu_ptr_template;
2040 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2041 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2042 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2043 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2044 Entity_Id gnat_index, gnat_name;
2048 /* Create the type for the component now, as it simplifies breaking
2049 type reference loops. */
2051 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2052 if (present_gnu_tree (gnat_entity))
2054 /* As a side effect, the type may have been translated. */
2055 maybe_present = true;
2059 /* We complete an existing dummy fat pointer type in place. This both
2060 avoids further complex adjustments in update_pointer_to and yields
2061 better debugging information in DWARF by leveraging the support for
2062 incomplete declarations of "tagged" types in the DWARF back-end. */
2063 gnu_type = get_dummy_type (gnat_entity);
2064 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2066 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2067 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2068 /* Save the contents of the dummy type for update_pointer_to. */
2069 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2071 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2072 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2076 gnu_fat_type = make_node (RECORD_TYPE);
2077 gnu_template_type = make_node (RECORD_TYPE);
2078 gnu_ptr_template = build_pointer_type (gnu_template_type);
2081 /* Make a node for the array. If we are not defining the array
2082 suppress expanding incomplete types. */
2083 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2087 defer_incomplete_level++;
2088 this_deferred = true;
2091 /* Build the fat pointer type. Use a "void *" object instead of
2092 a pointer to the array type since we don't have the array type
2093 yet (it will reference the fat pointer via the bounds). */
2095 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node,
2096 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2098 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2099 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2101 if (COMPLETE_TYPE_P (gnu_fat_type))
2103 /* We are going to lay it out again so reset the alias set. */
2104 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2105 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2106 finish_fat_pointer_type (gnu_fat_type, tem);
2107 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2108 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2110 TYPE_FIELDS (t) = tem;
2111 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2116 finish_fat_pointer_type (gnu_fat_type, tem);
2117 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2120 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2121 is the fat pointer. This will be used to access the individual
2122 fields once we build them. */
2123 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2124 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2125 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2126 gnu_template_reference
2127 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2128 TREE_READONLY (gnu_template_reference) = 1;
2129 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2131 /* Now create the GCC type for each index and add the fields for that
2132 index to the template. */
2133 for (index = (convention_fortran_p ? ndim - 1 : 0),
2134 gnat_index = First_Index (gnat_entity);
2135 0 <= index && index < ndim;
2136 index += (convention_fortran_p ? - 1 : 1),
2137 gnat_index = Next_Index (gnat_index))
2139 char field_name[16];
2140 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2141 tree gnu_index_base_type
2142 = maybe_character_type (get_base_type (gnu_index_type));
2143 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2144 tree gnu_min, gnu_max, gnu_high;
2146 /* Make the FIELD_DECLs for the low and high bounds of this
2147 type and then make extractions of these fields from the
2149 sprintf (field_name, "LB%d", index);
2150 gnu_lb_field = create_field_decl (get_identifier (field_name),
2151 gnu_index_base_type,
2152 gnu_template_type, NULL_TREE,
2154 Sloc_to_locus (Sloc (gnat_entity),
2155 &DECL_SOURCE_LOCATION (gnu_lb_field));
2157 field_name[0] = 'U';
2158 gnu_hb_field = create_field_decl (get_identifier (field_name),
2159 gnu_index_base_type,
2160 gnu_template_type, NULL_TREE,
2162 Sloc_to_locus (Sloc (gnat_entity),
2163 &DECL_SOURCE_LOCATION (gnu_hb_field));
2165 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2167 /* We can't use build_component_ref here since the template type
2168 isn't complete yet. */
2169 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2170 gnu_template_reference, gnu_lb_field,
2172 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2173 gnu_template_reference, gnu_hb_field,
2175 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2177 gnu_min = convert (sizetype, gnu_orig_min);
2178 gnu_max = convert (sizetype, gnu_orig_max);
2180 /* Compute the size of this dimension. See the E_Array_Subtype
2181 case below for the rationale. */
2183 = build3 (COND_EXPR, sizetype,
2184 build2 (GE_EXPR, boolean_type_node,
2185 gnu_orig_max, gnu_orig_min),
2187 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2189 /* Make a range type with the new range in the Ada base type.
2190 Then make an index type with the size range in sizetype. */
2191 gnu_index_types[index]
2192 = create_index_type (gnu_min, gnu_high,
2193 create_range_type (gnu_index_base_type,
2198 /* Update the maximum size of the array in elements. */
2202 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2204 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2206 = size_binop (PLUS_EXPR, size_one_node,
2207 size_binop (MINUS_EXPR, gnu_max, gnu_min));
2209 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2210 && TREE_OVERFLOW (gnu_this_max))
2211 gnu_max_size = NULL_TREE;
2214 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2217 TYPE_NAME (gnu_index_types[index])
2218 = create_concat_name (gnat_entity, field_name);
2221 /* Install all the fields into the template. */
2222 TYPE_NAME (gnu_template_type)
2223 = create_concat_name (gnat_entity, "XUB");
2224 gnu_template_fields = NULL_TREE;
2225 for (index = 0; index < ndim; index++)
2227 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2228 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2230 TYPE_READONLY (gnu_template_type) = 1;
2232 /* If Component_Size is not already specified, annotate it with the
2233 size of the component. */
2234 if (Unknown_Component_Size (gnat_entity))
2235 Set_Component_Size (gnat_entity,
2236 annotate_value (TYPE_SIZE (comp_type)));
2238 /* Compute the maximum size of the array in units and bits. */
2241 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2242 TYPE_SIZE_UNIT (comp_type));
2243 gnu_max_size = size_binop (MULT_EXPR,
2244 convert (bitsizetype, gnu_max_size),
2245 TYPE_SIZE (comp_type));
2248 gnu_max_size_unit = NULL_TREE;
2250 /* Now build the array type. */
2252 for (index = ndim - 1; index >= 0; index--)
2254 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2255 if (index == ndim - 1)
2256 TYPE_REVERSE_STORAGE_ORDER (tem)
2257 = Reverse_Storage_Order (gnat_entity);
2258 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2259 if (array_type_has_nonaliased_component (tem, gnat_entity))
2260 TYPE_NONALIASED_COMPONENT (tem) = 1;
2263 /* If an alignment is specified, use it if valid. But ignore it
2264 for the original type of packed array types. If the alignment
2265 was requested with an explicit alignment clause, state so. */
2266 if (No (Packed_Array_Impl_Type (gnat_entity))
2267 && Known_Alignment (gnat_entity))
2270 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2272 if (Present (Alignment_Clause (gnat_entity)))
2273 TYPE_USER_ALIGN (tem) = 1;
2276 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2278 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2279 implementation types as such so that the debug information back-end
2280 can output the appropriate description for them. */
2282 = (Is_Packed (gnat_entity)
2283 || Is_Packed_Array_Impl_Type (gnat_entity));
2285 if (Treat_As_Volatile (gnat_entity))
2286 tem = change_qualified_type (tem, TYPE_QUAL_VOLATILE);
2288 /* Adjust the type of the pointer-to-array field of the fat pointer
2289 and record the aliasing relationships if necessary. */
2290 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2291 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2292 record_component_aliases (gnu_fat_type);
2294 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2295 corresponding fat pointer. */
2296 TREE_TYPE (gnu_type) = gnu_fat_type;
2297 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2298 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2299 SET_TYPE_MODE (gnu_type, BLKmode);
2300 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2302 /* If the maximum size doesn't overflow, use it. */
2304 && TREE_CODE (gnu_max_size) == INTEGER_CST
2305 && !TREE_OVERFLOW (gnu_max_size)
2306 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2307 && !TREE_OVERFLOW (gnu_max_size_unit))
2309 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2311 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2312 TYPE_SIZE_UNIT (tem));
2315 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2316 artificial_p, debug_info_p, gnat_entity);
2318 /* If told to generate GNAT encodings for them (GDB rely on them at the
2319 moment): give the fat pointer type a name. If this is a packed
2320 array, tell the debugger how to interpret the underlying bits. */
2321 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2322 gnat_name = Packed_Array_Impl_Type (gnat_entity);
2324 gnat_name = gnat_entity;
2326 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2327 ? get_entity_name (gnat_name)
2328 : create_concat_name (gnat_name, "XUP");
2329 create_type_decl (xup_name, gnu_fat_type, artificial_p, debug_info_p,
2332 /* Create the type to be designated by thin pointers: a record type for
2333 the array and its template. We used to shift the fields to have the
2334 template at a negative offset, but this was somewhat of a kludge; we
2335 now shift thin pointer values explicitly but only those which have a
2336 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2337 Note that GDB can handle standard DWARF information for them, so we
2338 don't have to name them as a GNAT encoding, except if specifically
2341 = (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2342 ? get_entity_name (gnat_name)
2343 : create_concat_name (gnat_name, "XUT");
2344 tem = build_unc_object_type (gnu_template_type, tem, xut_name,
2347 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2348 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2352 case E_Array_Subtype:
2354 /* This is the actual data type for array variables. Multidimensional
2355 arrays are implemented as arrays of arrays. Note that arrays which
2356 have sparse enumeration subtypes as index components create sparse
2357 arrays, which is obviously space inefficient but so much easier to
2360 Also note that the subtype never refers to the unconstrained array
2361 type, which is somewhat at variance with Ada semantics.
2363 First check to see if this is simply a renaming of the array type.
2364 If so, the result is the array type. */
2366 gnu_type = TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity)));
2367 if (!Is_Constrained (gnat_entity))
2371 Entity_Id gnat_index, gnat_base_index;
2372 const bool convention_fortran_p
2373 = (Convention (gnat_entity) == Convention_Fortran);
2374 const int ndim = Number_Dimensions (gnat_entity);
2375 tree gnu_base_type = gnu_type;
2376 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2377 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2378 bool need_index_type_struct = false;
2381 /* First create the GCC type for each index and find out whether
2382 special types are needed for debugging information. */
2383 for (index = (convention_fortran_p ? ndim - 1 : 0),
2384 gnat_index = First_Index (gnat_entity),
2386 = First_Index (Implementation_Base_Type (gnat_entity));
2387 0 <= index && index < ndim;
2388 index += (convention_fortran_p ? - 1 : 1),
2389 gnat_index = Next_Index (gnat_index),
2390 gnat_base_index = Next_Index (gnat_base_index))
2392 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2393 tree gnu_index_base_type
2394 = maybe_character_type (get_base_type (gnu_index_type));
2396 = convert (gnu_index_base_type,
2397 TYPE_MIN_VALUE (gnu_index_type));
2399 = convert (gnu_index_base_type,
2400 TYPE_MAX_VALUE (gnu_index_type));
2401 tree gnu_min = convert (sizetype, gnu_orig_min);
2402 tree gnu_max = convert (sizetype, gnu_orig_max);
2403 tree gnu_base_index_type
2404 = get_unpadded_type (Etype (gnat_base_index));
2405 tree gnu_base_index_base_type
2406 = maybe_character_type (get_base_type (gnu_base_index_type));
2407 tree gnu_base_orig_min
2408 = convert (gnu_base_index_base_type,
2409 TYPE_MIN_VALUE (gnu_base_index_type));
2410 tree gnu_base_orig_max
2411 = convert (gnu_base_index_base_type,
2412 TYPE_MAX_VALUE (gnu_base_index_type));
2415 /* See if the base array type is already flat. If it is, we
2416 are probably compiling an ACATS test but it will cause the
2417 code below to malfunction if we don't handle it specially. */
2418 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2419 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2420 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2422 gnu_min = size_one_node;
2423 gnu_max = size_zero_node;
2427 /* Similarly, if one of the values overflows in sizetype and the
2428 range is null, use 1..0 for the sizetype bounds. */
2429 else if (TREE_CODE (gnu_min) == INTEGER_CST
2430 && TREE_CODE (gnu_max) == INTEGER_CST
2431 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2432 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2434 gnu_min = size_one_node;
2435 gnu_max = size_zero_node;
2439 /* If the minimum and maximum values both overflow in sizetype,
2440 but the difference in the original type does not overflow in
2441 sizetype, ignore the overflow indication. */
2442 else if (TREE_CODE (gnu_min) == INTEGER_CST
2443 && TREE_CODE (gnu_max) == INTEGER_CST
2444 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2447 fold_build2 (MINUS_EXPR, gnu_index_type,
2451 TREE_OVERFLOW (gnu_min) = 0;
2452 TREE_OVERFLOW (gnu_max) = 0;
2456 /* Compute the size of this dimension in the general case. We
2457 need to provide GCC with an upper bound to use but have to
2458 deal with the "superflat" case. There are three ways to do
2459 this. If we can prove that the array can never be superflat,
2460 we can just use the high bound of the index type. */
2461 else if ((Nkind (gnat_index) == N_Range
2462 && cannot_be_superflat (gnat_index))
2463 /* Bit-Packed Array Impl. Types are never superflat. */
2464 || (Is_Packed_Array_Impl_Type (gnat_entity)
2465 && Is_Bit_Packed_Array
2466 (Original_Array_Type (gnat_entity))))
2469 /* Otherwise, if the high bound is constant but the low bound is
2470 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2471 lower bound. Note that the comparison must be done in the
2472 original type to avoid any overflow during the conversion. */
2473 else if (TREE_CODE (gnu_max) == INTEGER_CST
2474 && TREE_CODE (gnu_min) != INTEGER_CST)
2478 = build_cond_expr (sizetype,
2479 build_binary_op (GE_EXPR,
2484 int_const_binop (PLUS_EXPR, gnu_max,
2488 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2489 in all the other cases. Note that, here as well as above,
2490 the condition used in the comparison must be equivalent to
2491 the condition (length != 0). This is relied upon in order
2492 to optimize array comparisons in compare_arrays. Moreover
2493 we use int_const_binop for the shift by 1 if the bound is
2494 constant to avoid any unwanted overflow. */
2497 = build_cond_expr (sizetype,
2498 build_binary_op (GE_EXPR,
2503 TREE_CODE (gnu_min) == INTEGER_CST
2504 ? int_const_binop (MINUS_EXPR, gnu_min,
2506 : size_binop (MINUS_EXPR, gnu_min,
2509 /* Reuse the index type for the range type. Then make an index
2510 type with the size range in sizetype. */
2511 gnu_index_types[index]
2512 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2515 /* Update the maximum size of the array in elements. Here we
2516 see if any constraint on the index type of the base type
2517 can be used in the case of self-referential bound on the
2518 index type of the subtype. We look for a non-"infinite"
2519 and non-self-referential bound from any type involved and
2520 handle each bound separately. */
2523 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2524 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2525 tree gnu_base_base_min
2526 = convert (sizetype,
2527 TYPE_MIN_VALUE (gnu_base_index_base_type));
2528 tree gnu_base_base_max
2529 = convert (sizetype,
2530 TYPE_MAX_VALUE (gnu_base_index_base_type));
2532 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2533 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2534 && !TREE_OVERFLOW (gnu_base_min)))
2535 gnu_base_min = gnu_min;
2537 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2538 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2539 && !TREE_OVERFLOW (gnu_base_max)))
2540 gnu_base_max = gnu_max;
2542 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2543 && TREE_OVERFLOW (gnu_base_min))
2544 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2545 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2546 && TREE_OVERFLOW (gnu_base_max))
2547 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2548 gnu_max_size = NULL_TREE;
2553 /* Use int_const_binop if the bounds are constant to
2554 avoid any unwanted overflow. */
2555 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2556 && TREE_CODE (gnu_base_max) == INTEGER_CST)
2558 = int_const_binop (PLUS_EXPR, size_one_node,
2559 int_const_binop (MINUS_EXPR,
2564 = size_binop (PLUS_EXPR, size_one_node,
2565 size_binop (MINUS_EXPR,
2570 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2574 /* We need special types for debugging information to point to
2575 the index types if they have variable bounds, are not integer
2576 types, are biased or are wider than sizetype. These are GNAT
2577 encodings, so we have to include them only when all encodings
2579 if ((TREE_CODE (gnu_orig_min) != INTEGER_CST
2580 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2581 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2582 || (TREE_TYPE (gnu_index_type)
2583 && TREE_CODE (TREE_TYPE (gnu_index_type))
2585 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type))
2586 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2587 need_index_type_struct = true;
2590 /* Then flatten: create the array of arrays. For an array type
2591 used to implement a packed array, get the component type from
2592 the original array type since the representation clauses that
2593 can affect it are on the latter. */
2594 if (Is_Packed_Array_Impl_Type (gnat_entity)
2595 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2597 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2598 for (index = ndim - 1; index >= 0; index--)
2599 gnu_type = TREE_TYPE (gnu_type);
2601 /* One of the above calls might have caused us to be elaborated,
2602 so don't blow up if so. */
2603 if (present_gnu_tree (gnat_entity))
2605 maybe_present = true;
2611 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2614 /* One of the above calls might have caused us to be elaborated,
2615 so don't blow up if so. */
2616 if (present_gnu_tree (gnat_entity))
2618 maybe_present = true;
2623 /* Compute the maximum size of the array in units and bits. */
2626 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2627 TYPE_SIZE_UNIT (gnu_type));
2628 gnu_max_size = size_binop (MULT_EXPR,
2629 convert (bitsizetype, gnu_max_size),
2630 TYPE_SIZE (gnu_type));
2633 gnu_max_size_unit = NULL_TREE;
2635 /* Now build the array type. */
2636 for (index = ndim - 1; index >= 0; index --)
2638 gnu_type = build_nonshared_array_type (gnu_type,
2639 gnu_index_types[index]);
2640 if (index == ndim - 1)
2641 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
2642 = Reverse_Storage_Order (gnat_entity);
2643 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2644 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2645 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2648 /* Strip the ___XP suffix for standard DWARF. */
2649 if (Is_Packed_Array_Impl_Type (gnat_entity)
2650 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
2652 Entity_Id gnat_original_array_type
2653 = Underlying_Type (Original_Array_Type (gnat_entity));
2656 = get_entity_name (gnat_original_array_type);
2659 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2660 TYPE_STUB_DECL (gnu_type)
2661 = create_type_stub_decl (gnu_entity_name, gnu_type);
2663 /* If we are at file level and this is a multi-dimensional array,
2664 we need to make a variable corresponding to the stride of the
2665 inner dimensions. */
2666 if (global_bindings_p () && ndim > 1)
2670 for (gnu_arr_type = TREE_TYPE (gnu_type), index = 1;
2671 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2672 gnu_arr_type = TREE_TYPE (gnu_arr_type), index++)
2674 tree eltype = TREE_TYPE (gnu_arr_type);
2675 char stride_name[32];
2677 sprintf (stride_name, "ST%d", index);
2678 TYPE_SIZE (gnu_arr_type)
2679 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2680 gnat_entity, stride_name,
2683 /* ??? For now, store the size as a multiple of the
2684 alignment of the element type in bytes so that we
2685 can see the alignment from the tree. */
2686 sprintf (stride_name, "ST%d_A_UNIT", index);
2687 TYPE_SIZE_UNIT (gnu_arr_type)
2688 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2689 gnat_entity, stride_name,
2691 TYPE_ALIGN (eltype));
2693 /* ??? create_type_decl is not invoked on the inner types so
2694 the MULT_EXPR node built above will never be marked. */
2695 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2699 /* If we need to write out a record type giving the names of the
2700 bounds for debugging purposes, do it now and make the record
2701 type a parallel type. This is not needed for a packed array
2702 since the bounds are conveyed by the original array type. */
2703 if (need_index_type_struct
2705 && !Is_Packed_Array_Impl_Type (gnat_entity))
2707 tree gnu_bound_rec = make_node (RECORD_TYPE);
2708 tree gnu_field_list = NULL_TREE;
2711 TYPE_NAME (gnu_bound_rec)
2712 = create_concat_name (gnat_entity, "XA");
2714 for (index = ndim - 1; index >= 0; index--)
2716 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2717 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2719 /* Make sure to reference the types themselves, and not just
2720 their names, as the debugger may fall back on them. */
2721 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2722 gnu_bound_rec, NULL_TREE,
2724 DECL_CHAIN (gnu_field) = gnu_field_list;
2725 gnu_field_list = gnu_field;
2728 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2729 add_parallel_type (gnu_type, gnu_bound_rec);
2732 /* If this is a packed array type, make the original array type a
2733 parallel/debug type. Otherwise, if such GNAT encodings are
2734 required, do it for the base array type if it isn't artificial to
2735 make sure it is kept in the debug info. */
2738 if (Is_Packed_Array_Impl_Type (gnat_entity))
2739 associate_original_type_to_packed_array (gnu_type,
2744 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2745 if (!DECL_ARTIFICIAL (gnu_base_decl)
2746 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
2747 add_parallel_type (gnu_type,
2748 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2752 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2753 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2754 = (Is_Packed_Array_Impl_Type (gnat_entity)
2755 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2757 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2758 implementation types as such so that the debug information back-end
2759 can output the appropriate description for them. */
2760 TYPE_PACKED (gnu_type)
2761 = (Is_Packed (gnat_entity)
2762 || Is_Packed_Array_Impl_Type (gnat_entity));
2764 /* If the size is self-referential and the maximum size doesn't
2765 overflow, use it. */
2766 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2768 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2769 && TREE_OVERFLOW (gnu_max_size))
2770 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2771 && TREE_OVERFLOW (gnu_max_size_unit)))
2773 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2774 TYPE_SIZE (gnu_type));
2775 TYPE_SIZE_UNIT (gnu_type)
2776 = size_binop (MIN_EXPR, gnu_max_size_unit,
2777 TYPE_SIZE_UNIT (gnu_type));
2780 /* Set our alias set to that of our base type. This gives all
2781 array subtypes the same alias set. */
2782 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2784 /* If this is a packed type, make this type the same as the packed
2785 array type, but do some adjusting in the type first. */
2786 if (Present (Packed_Array_Impl_Type (gnat_entity)))
2788 Entity_Id gnat_index;
2791 /* First finish the type we had been making so that we output
2792 debugging information for it. */
2793 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2794 if (Treat_As_Volatile (gnat_entity))
2797 = TYPE_QUAL_VOLATILE
2798 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
2799 gnu_type = change_qualified_type (gnu_type, quals);
2801 /* Make it artificial only if the base type was artificial too.
2802 That's sort of "morally" true and will make it possible for
2803 the debugger to look it up by name in DWARF, which is needed
2804 in order to decode the packed array type. */
2806 = create_type_decl (gnu_entity_name, gnu_type,
2807 !Comes_From_Source (Etype (gnat_entity))
2808 && artificial_p, debug_info_p,
2811 /* Save it as our equivalent in case the call below elaborates
2813 save_gnu_tree (gnat_entity, gnu_decl, false);
2816 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity),
2818 this_made_decl = true;
2819 gnu_type = TREE_TYPE (gnu_decl);
2821 save_gnu_tree (gnat_entity, NULL_TREE, false);
2823 gnu_inner = gnu_type;
2824 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2825 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2826 || TYPE_PADDING_P (gnu_inner)))
2827 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2829 /* We need to attach the index type to the type we just made so
2830 that the actual bounds can later be put into a template. */
2831 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2832 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2833 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2834 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2836 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2838 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2839 TYPE_MODULUS for modular types so we make an extra
2840 subtype if necessary. */
2841 if (TYPE_MODULAR_P (gnu_inner))
2844 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2845 TREE_TYPE (gnu_subtype) = gnu_inner;
2846 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2847 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2848 TYPE_MIN_VALUE (gnu_inner));
2849 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2850 TYPE_MAX_VALUE (gnu_inner));
2851 gnu_inner = gnu_subtype;
2854 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2856 /* Check for other cases of overloading. */
2857 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2860 for (gnat_index = First_Index (gnat_entity);
2861 Present (gnat_index);
2862 gnat_index = Next_Index (gnat_index))
2863 SET_TYPE_ACTUAL_BOUNDS
2865 tree_cons (NULL_TREE,
2866 get_unpadded_type (Etype (gnat_index)),
2867 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2869 if (Convention (gnat_entity) != Convention_Fortran)
2870 SET_TYPE_ACTUAL_BOUNDS
2871 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2873 if (TREE_CODE (gnu_type) == RECORD_TYPE
2874 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2875 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2880 /* Abort if packed array with no Packed_Array_Impl_Type. */
2881 gcc_assert (!Is_Packed (gnat_entity));
2885 case E_String_Literal_Subtype:
2886 /* Create the type for a string literal. */
2888 Entity_Id gnat_full_type
2889 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2890 && Present (Full_View (Etype (gnat_entity)))
2891 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2892 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2893 tree gnu_string_array_type
2894 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2895 tree gnu_string_index_type
2896 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2897 (TYPE_DOMAIN (gnu_string_array_type))));
2898 tree gnu_lower_bound
2899 = convert (gnu_string_index_type,
2900 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2902 = UI_To_gnu (String_Literal_Length (gnat_entity),
2903 gnu_string_index_type);
2904 tree gnu_upper_bound
2905 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2907 int_const_binop (MINUS_EXPR, gnu_length,
2908 convert (gnu_string_index_type,
2909 integer_one_node)));
2911 = create_index_type (convert (sizetype, gnu_lower_bound),
2912 convert (sizetype, gnu_upper_bound),
2913 create_range_type (gnu_string_index_type,
2919 = build_nonshared_array_type (gnat_to_gnu_type
2920 (Component_Type (gnat_entity)),
2922 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2923 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2924 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2928 /* Record Types and Subtypes
2930 The following fields are defined on record types:
2932 Has_Discriminants True if the record has discriminants
2933 First_Discriminant Points to head of list of discriminants
2934 First_Entity Points to head of list of fields
2935 Is_Tagged_Type True if the record is tagged
2937 Implementation of Ada records and discriminated records:
2939 A record type definition is transformed into the equivalent of a C
2940 struct definition. The fields that are the discriminants which are
2941 found in the Full_Type_Declaration node and the elements of the
2942 Component_List found in the Record_Type_Definition node. The
2943 Component_List can be a recursive structure since each Variant of
2944 the Variant_Part of the Component_List has a Component_List.
2946 Processing of a record type definition comprises starting the list of
2947 field declarations here from the discriminants and the calling the
2948 function components_to_record to add the rest of the fields from the
2949 component list and return the gnu type node. The function
2950 components_to_record will call itself recursively as it traverses
2954 if (Has_Complex_Representation (gnat_entity))
2957 = build_complex_type
2959 (Etype (Defining_Entity
2960 (First (Component_Items
2963 (Declaration_Node (gnat_entity)))))))));
2969 Node_Id full_definition = Declaration_Node (gnat_entity);
2970 Node_Id record_definition = Type_Definition (full_definition);
2971 Node_Id gnat_constr;
2972 Entity_Id gnat_field;
2973 tree gnu_field, gnu_field_list = NULL_TREE;
2974 tree gnu_get_parent;
2975 /* Set PACKED in keeping with gnat_to_gnu_field. */
2977 = Is_Packed (gnat_entity)
2979 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2982 const bool has_align = Known_Alignment (gnat_entity);
2983 const bool has_discr = Has_Discriminants (gnat_entity);
2984 const bool has_rep = Has_Specified_Layout (gnat_entity);
2985 const bool is_extension
2986 = (Is_Tagged_Type (gnat_entity)
2987 && Nkind (record_definition) == N_Derived_Type_Definition);
2988 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2989 bool all_rep = has_rep;
2991 /* See if all fields have a rep clause. Stop when we find one
2994 for (gnat_field = First_Entity (gnat_entity);
2995 Present (gnat_field);
2996 gnat_field = Next_Entity (gnat_field))
2997 if ((Ekind (gnat_field) == E_Component
2998 || Ekind (gnat_field) == E_Discriminant)
2999 && No (Component_Clause (gnat_field)))
3005 /* If this is a record extension, go a level further to find the
3006 record definition. Also, verify we have a Parent_Subtype. */
3009 if (!type_annotate_only
3010 || Present (Record_Extension_Part (record_definition)))
3011 record_definition = Record_Extension_Part (record_definition);
3013 gcc_assert (type_annotate_only
3014 || Present (Parent_Subtype (gnat_entity)));
3017 /* Make a node for the record. If we are not defining the record,
3018 suppress expanding incomplete types. */
3019 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
3020 TYPE_NAME (gnu_type) = gnu_entity_name;
3021 TYPE_PACKED (gnu_type) = (packed != 0) || has_align || has_rep;
3022 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3023 = Reverse_Storage_Order (gnat_entity);
3024 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3028 defer_incomplete_level++;
3029 this_deferred = true;
3032 /* If both a size and rep clause were specified, put the size on
3033 the record type now so that it can get the proper layout. */
3034 if (has_rep && Known_RM_Size (gnat_entity))
3035 TYPE_SIZE (gnu_type)
3036 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
3038 /* Always set the alignment on the record type here so that it can
3039 get the proper layout. */
3041 TYPE_ALIGN (gnu_type)
3042 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
3045 TYPE_ALIGN (gnu_type) = 0;
3047 /* If a type needs strict alignment, the minimum size will be the
3048 type size instead of the RM size (see validate_size). Cap the
3049 alignment lest it causes this type size to become too large. */
3050 if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3052 unsigned int max_size = UI_To_Int (RM_Size (gnat_entity));
3053 unsigned int max_align = max_size & -max_size;
3054 if (max_align < BIGGEST_ALIGNMENT)
3055 TYPE_MAX_ALIGN (gnu_type) = max_align;
3059 /* If we have a Parent_Subtype, make a field for the parent. If
3060 this record has rep clauses, force the position to zero. */
3061 if (Present (Parent_Subtype (gnat_entity)))
3063 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3064 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3067 /* A major complexity here is that the parent subtype will
3068 reference our discriminants in its Stored_Constraint list.
3069 But those must reference the parent component of this record
3070 which is precisely of the parent subtype we have not built yet!
3071 To break the circle we first build a dummy COMPONENT_REF which
3072 represents the "get to the parent" operation and initialize
3073 each of those discriminants to a COMPONENT_REF of the above
3074 dummy parent referencing the corresponding discriminant of the
3075 base type of the parent subtype. */
3076 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3077 build0 (PLACEHOLDER_EXPR, gnu_type),
3078 build_decl (input_location,
3079 FIELD_DECL, NULL_TREE,
3080 gnu_dummy_parent_type),
3084 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3085 Present (gnat_field);
3086 gnat_field = Next_Stored_Discriminant (gnat_field))
3087 if (Present (Corresponding_Discriminant (gnat_field)))
3090 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3094 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3095 gnu_get_parent, gnu_field, NULL_TREE),
3099 /* Then we build the parent subtype. If it has discriminants but
3100 the type itself has unknown discriminants, this means that it
3101 doesn't contain information about how the discriminants are
3102 derived from those of the ancestor type, so it cannot be used
3103 directly. Instead it is built by cloning the parent subtype
3104 of the underlying record view of the type, for which the above
3105 derivation of discriminants has been made explicit. */
3106 if (Has_Discriminants (gnat_parent)
3107 && Has_Unknown_Discriminants (gnat_entity))
3109 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3111 /* If we are defining the type, the underlying record
3112 view must already have been elaborated at this point.
3113 Otherwise do it now as its parent subtype cannot be
3114 technically elaborated on its own. */
3116 gcc_assert (present_gnu_tree (gnat_uview));
3118 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3120 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3122 /* Substitute the "get to the parent" of the type for that
3123 of its underlying record view in the cloned type. */
3124 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3125 Present (gnat_field);
3126 gnat_field = Next_Stored_Discriminant (gnat_field))
3127 if (Present (Corresponding_Discriminant (gnat_field)))
3129 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3131 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3132 gnu_get_parent, gnu_field, NULL_TREE);
3134 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3138 gnu_parent = gnat_to_gnu_type (gnat_parent);
3140 /* The parent field needs strict alignment so, if it is to
3141 be created with a component clause below, then we need
3142 to apply the same adjustment as in gnat_to_gnu_field. */
3143 if (has_rep && TYPE_ALIGN (gnu_type) < TYPE_ALIGN (gnu_parent))
3144 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_parent);
3146 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3147 initially built. The discriminants must reference the fields
3148 of the parent subtype and not those of its base type for the
3149 placeholder machinery to properly work. */
3152 /* The actual parent subtype is the full view. */
3153 if (IN (Ekind (gnat_parent), Private_Kind))
3155 if (Present (Full_View (gnat_parent)))
3156 gnat_parent = Full_View (gnat_parent);
3158 gnat_parent = Underlying_Full_View (gnat_parent);
3161 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3162 Present (gnat_field);
3163 gnat_field = Next_Stored_Discriminant (gnat_field))
3164 if (Present (Corresponding_Discriminant (gnat_field)))
3167 for (field = First_Stored_Discriminant (gnat_parent);
3169 field = Next_Stored_Discriminant (field))
3170 if (same_discriminant_p (gnat_field, field))
3172 gcc_assert (Present (field));
3173 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3174 = gnat_to_gnu_field_decl (field);
3178 /* The "get to the parent" COMPONENT_REF must be given its
3180 TREE_TYPE (gnu_get_parent) = gnu_parent;
3182 /* ...and reference the _Parent field of this record. */
3184 = create_field_decl (parent_name_id,
3185 gnu_parent, gnu_type,
3187 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3189 ? bitsize_zero_node : NULL_TREE,
3191 DECL_INTERNAL_P (gnu_field) = 1;
3192 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3193 TYPE_FIELDS (gnu_type) = gnu_field;
3196 /* Make the fields for the discriminants and put them into the record
3197 unless it's an Unchecked_Union. */
3199 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3200 Present (gnat_field);
3201 gnat_field = Next_Stored_Discriminant (gnat_field))
3203 /* If this is a record extension and this discriminant is the
3204 renaming of another discriminant, we've handled it above. */
3205 if (Present (Parent_Subtype (gnat_entity))
3206 && Present (Corresponding_Discriminant (gnat_field)))
3209 /* However, if we are just annotating types, the Parent_Subtype
3210 doesn't exist so we need skip the discriminant altogether. */
3211 if (type_annotate_only
3212 && Is_Tagged_Type (gnat_entity)
3213 && Is_Derived_Type (gnat_entity)
3214 && Present (Corresponding_Discriminant (gnat_field)))
3218 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3221 /* Make an expression using a PLACEHOLDER_EXPR from the
3222 FIELD_DECL node just created and link that with the
3223 corresponding GNAT defining identifier. */
3224 save_gnu_tree (gnat_field,
3225 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3226 build0 (PLACEHOLDER_EXPR, gnu_type),
3227 gnu_field, NULL_TREE),
3230 if (!is_unchecked_union)
3232 DECL_CHAIN (gnu_field) = gnu_field_list;
3233 gnu_field_list = gnu_field;
3237 /* If we have a derived untagged type that renames discriminants in
3238 the root type, the (stored) discriminants are a just copy of the
3239 discriminants of the root type. This means that any constraints
3240 added by the renaming in the derivation are disregarded as far
3241 as the layout of the derived type is concerned. To rescue them,
3242 we change the type of the (stored) discriminants to a subtype
3243 with the bounds of the type of the visible discriminants. */
3246 && Stored_Constraint (gnat_entity) != No_Elist)
3247 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3248 gnat_constr != No_Elmt;
3249 gnat_constr = Next_Elmt (gnat_constr))
3250 if (Nkind (Node (gnat_constr)) == N_Identifier
3251 /* Ignore access discriminants. */
3252 && !Is_Access_Type (Etype (Node (gnat_constr)))
3253 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3255 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3256 tree gnu_discr_type, gnu_ref;
3258 /* If the scope of the discriminant is not the record type,
3259 this means that we're processing the implicit full view
3260 of a type derived from a private discriminated type: in
3261 this case, the Stored_Constraint list is simply copied
3262 from the partial view, see Build_Derived_Private_Type.
3263 So we need to retrieve the corresponding discriminant
3264 of the implicit full view, otherwise we will abort. */
3265 if (Scope (gnat_discr) != gnat_entity)
3268 for (field = First_Entity (gnat_entity);
3270 field = Next_Entity (field))
3271 if (Ekind (field) == E_Discriminant
3272 && same_discriminant_p (gnat_discr, field))
3274 gcc_assert (Present (field));
3278 gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3280 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3283 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3284 just above for one of the stored discriminants. */
3285 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3287 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3289 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3291 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3292 ? make_unsigned_type (prec) : make_signed_type (prec);
3293 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3294 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3295 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3296 TYPE_MIN_VALUE (gnu_discr_type));
3297 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3298 TYPE_MAX_VALUE (gnu_discr_type));
3300 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3304 /* Add the fields into the record type and finish it up. */
3305 components_to_record (gnu_type, Component_List (record_definition),
3306 gnu_field_list, packed, definition, false,
3307 all_rep, is_unchecked_union,
3308 artificial_p, debug_info_p,
3309 false, OK_To_Reorder_Components (gnat_entity),
3310 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3312 /* Fill in locations of fields. */
3313 annotate_rep (gnat_entity, gnu_type);
3315 /* If there are any entities in the chain corresponding to components
3316 that we did not elaborate, ensure we elaborate their types if they
3318 for (gnat_temp = First_Entity (gnat_entity);
3319 Present (gnat_temp);
3320 gnat_temp = Next_Entity (gnat_temp))
3321 if ((Ekind (gnat_temp) == E_Component
3322 || Ekind (gnat_temp) == E_Discriminant)
3323 && Is_Itype (Etype (gnat_temp))
3324 && !present_gnu_tree (gnat_temp))
3325 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3327 /* If this is a record type associated with an exception definition,
3328 equate its fields to those of the standard exception type. This
3329 will make it possible to convert between them. */
3330 if (gnu_entity_name == exception_data_name_id)
3333 for (gnu_field = TYPE_FIELDS (gnu_type),
3334 gnu_std_field = TYPE_FIELDS (except_type_node);
3336 gnu_field = DECL_CHAIN (gnu_field),
3337 gnu_std_field = DECL_CHAIN (gnu_std_field))
3338 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3339 gcc_assert (!gnu_std_field);
3344 case E_Class_Wide_Subtype:
3345 /* If an equivalent type is present, that is what we should use.
3346 Otherwise, fall through to handle this like a record subtype
3347 since it may have constraints. */
3348 if (gnat_equiv_type != gnat_entity)
3350 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3351 maybe_present = true;
3355 /* ... fall through ... */
3357 case E_Record_Subtype:
3358 /* If Cloned_Subtype is Present it means this record subtype has
3359 identical layout to that type or subtype and we should use
3360 that GCC type for this one. The front end guarantees that
3361 the component list is shared. */
3362 if (Present (Cloned_Subtype (gnat_entity)))
3364 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3366 maybe_present = true;
3370 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3371 changing the type, make a new type with each field having the type of
3372 the field in the new subtype but the position computed by transforming
3373 every discriminant reference according to the constraints. We don't
3374 see any difference between private and non-private type here since
3375 derivations from types should have been deferred until the completion
3376 of the private type. */
3379 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3384 defer_incomplete_level++;
3385 this_deferred = true;
3389 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type));
3391 if (present_gnu_tree (gnat_entity))
3393 maybe_present = true;
3397 /* If this is a record subtype associated with a dispatch table,
3398 strip the suffix. This is necessary to make sure 2 different
3399 subtypes associated with the imported and exported views of a
3400 dispatch table are properly merged in LTO mode. */
3401 if (Is_Dispatch_Table_Entity (gnat_entity))
3404 Get_Encoded_Name (gnat_entity);
3405 p = strchr (Name_Buffer, '_');
3407 strcpy (p+2, "dtS");
3408 gnu_entity_name = get_identifier (Name_Buffer);
3411 /* When the subtype has discriminants and these discriminants affect
3412 the initial shape it has inherited, factor them in. But for an
3413 Unchecked_Union (it must be an Itype), just return the type.
3414 We can't just test Is_Constrained because private subtypes without
3415 discriminants of types with discriminants with default expressions
3416 are Is_Constrained but aren't constrained! */
3417 if (IN (Ekind (gnat_base_type), Record_Kind)
3418 && !Is_Unchecked_Union (gnat_base_type)
3419 && !Is_For_Access_Subtype (gnat_entity)
3420 && Has_Discriminants (gnat_entity)
3421 && Is_Constrained (gnat_entity)
3422 && Stored_Constraint (gnat_entity) != No_Elist)
3424 vec<subst_pair> gnu_subst_list
3425 = build_subst_list (gnat_entity, gnat_base_type, definition);
3426 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3427 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3428 bool selected_variant = false, all_constant_pos = true;
3429 Entity_Id gnat_field;
3430 vec<variant_desc> gnu_variant_list;
3432 gnu_type = make_node (RECORD_TYPE);
3433 TYPE_NAME (gnu_type) = gnu_entity_name;
3434 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
3435 SET_TYPE_DEBUG_TYPE (gnu_type, gnu_base_type);
3436 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3437 TYPE_REVERSE_STORAGE_ORDER (gnu_type)
3438 = Reverse_Storage_Order (gnat_entity);
3439 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3441 /* Set the size, alignment and alias set of the new type to
3442 match that of the old one, doing required substitutions. */
3443 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3446 if (TYPE_IS_PADDING_P (gnu_base_type))
3447 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3449 gnu_unpad_base_type = gnu_base_type;
3451 /* Look for REP and variant parts in the base type. */
3452 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3453 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3455 /* If there is a variant part, we must compute whether the
3456 constraints statically select a particular variant. If
3457 so, we simply drop the qualified union and flatten the
3458 list of fields. Otherwise we'll build a new qualified
3459 union for the variants that are still relevant. */
3460 if (gnu_variant_part)
3466 = build_variant_list (TREE_TYPE (gnu_variant_part),
3470 /* If all the qualifiers are unconditionally true, the
3471 innermost variant is statically selected. */
3472 selected_variant = true;
3473 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3474 if (!integer_onep (v->qual))
3476 selected_variant = false;
3480 /* Otherwise, create the new variants. */
3481 if (!selected_variant)
3482 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3484 tree old_variant = v->type;
3485 tree new_variant = make_node (RECORD_TYPE);
3487 = concat_name (DECL_NAME (gnu_variant_part),
3489 (DECL_NAME (v->field)));
3490 TYPE_NAME (new_variant)
3491 = concat_name (TYPE_NAME (gnu_type),
3492 IDENTIFIER_POINTER (suffix));
3493 TYPE_REVERSE_STORAGE_ORDER (new_variant)
3494 = TYPE_REVERSE_STORAGE_ORDER (gnu_type);
3495 copy_and_substitute_in_size (new_variant, old_variant,
3497 v->new_type = new_variant;
3502 gnu_variant_list.create (0);
3503 selected_variant = false;
3506 /* Make a list of fields and their position in the base type. */
3508 = build_position_list (gnu_unpad_base_type,
3509 gnu_variant_list.exists ()
3510 && !selected_variant,
3511 size_zero_node, bitsize_zero_node,
3512 BIGGEST_ALIGNMENT, NULL_TREE);
3514 /* Now go down every component in the subtype and compute its
3515 size and position from those of the component in the base
3516 type and from the constraints of the subtype. */
3517 for (gnat_field = First_Entity (gnat_entity);
3518 Present (gnat_field);
3519 gnat_field = Next_Entity (gnat_field))
3520 if ((Ekind (gnat_field) == E_Component
3521 || Ekind (gnat_field) == E_Discriminant)
3522 && !(Present (Corresponding_Discriminant (gnat_field))
3523 && Is_Tagged_Type (gnat_base_type))
3525 (Scope (Original_Record_Component (gnat_field)))
3528 Name_Id gnat_name = Chars (gnat_field);
3529 Entity_Id gnat_old_field
3530 = Original_Record_Component (gnat_field);
3532 = gnat_to_gnu_field_decl (gnat_old_field);
3533 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3534 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3535 tree gnu_cont_type, gnu_last = NULL_TREE;
3537 /* If the type is the same, retrieve the GCC type from the
3538 old field to take into account possible adjustments. */
3539 if (Etype (gnat_field) == Etype (gnat_old_field))
3540 gnu_field_type = TREE_TYPE (gnu_old_field);
3542 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3544 /* If there was a component clause, the field types must be
3545 the same for the type and subtype, so copy the data from
3546 the old field to avoid recomputation here. Also if the
3547 field is justified modular and the optimization in
3548 gnat_to_gnu_field was applied. */
3549 if (Present (Component_Clause (gnat_old_field))
3550 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3551 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3552 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3553 == TREE_TYPE (gnu_old_field)))
3555 gnu_size = DECL_SIZE (gnu_old_field);
3556 gnu_field_type = TREE_TYPE (gnu_old_field);
3559 /* If the old field was packed and of constant size, we
3560 have to get the old size here, as it might differ from
3561 what the Etype conveys and the latter might overlap
3562 onto the following field. Try to arrange the type for
3563 possible better packing along the way. */
3564 else if (DECL_PACKED (gnu_old_field)
3565 && TREE_CODE (DECL_SIZE (gnu_old_field))
3568 gnu_size = DECL_SIZE (gnu_old_field);
3569 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3570 && !TYPE_FAT_POINTER_P (gnu_field_type)
3571 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3573 = make_packable_type (gnu_field_type, true);
3577 gnu_size = TYPE_SIZE (gnu_field_type);
3579 /* If the context of the old field is the base type or its
3580 REP part (if any), put the field directly in the new
3581 type; otherwise look up the context in the variant list
3582 and put the field either in the new type if there is a
3583 selected variant or in one of the new variants. */
3584 if (gnu_context == gnu_unpad_base_type
3586 && gnu_context == TREE_TYPE (gnu_rep_part)))
3587 gnu_cont_type = gnu_type;
3594 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3595 if (gnu_context == v->type
3596 || ((rep_part = get_rep_part (v->type))
3597 && gnu_context == TREE_TYPE (rep_part)))
3601 if (selected_variant)
3602 gnu_cont_type = gnu_type;
3604 gnu_cont_type = v->new_type;
3607 /* The front-end may pass us "ghost" components if
3608 it fails to recognize that a constrained subtype
3609 is statically constrained. Discard them. */
3613 /* Now create the new field modeled on the old one. */
3615 = create_field_decl_from (gnu_old_field, gnu_field_type,
3616 gnu_cont_type, gnu_size,
3617 gnu_pos_list, gnu_subst_list);
3618 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3620 /* Put it in one of the new variants directly. */
3621 if (gnu_cont_type != gnu_type)
3623 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3624 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3627 /* To match the layout crafted in components_to_record,
3628 if this is the _Tag or _Parent field, put it before
3629 any other fields. */
3630 else if (gnat_name == Name_uTag
3631 || gnat_name == Name_uParent)
3632 gnu_field_list = chainon (gnu_field_list, gnu_field);
3634 /* Similarly, if this is the _Controller field, put
3635 it before the other fields except for the _Tag or
3637 else if (gnat_name == Name_uController && gnu_last)
3639 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3640 DECL_CHAIN (gnu_last) = gnu_field;
3643 /* Otherwise, if this is a regular field, put it after
3644 the other fields. */
3647 DECL_CHAIN (gnu_field) = gnu_field_list;
3648 gnu_field_list = gnu_field;
3650 gnu_last = gnu_field;
3651 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3652 all_constant_pos = false;
3655 save_gnu_tree (gnat_field, gnu_field, false);
3658 /* If there is a variant list, a selected variant and the fields
3659 all have a constant position, put them in order of increasing
3660 position to match that of constant CONSTRUCTORs. Likewise if
3661 there is no variant list but a REP part, since the latter has
3662 been flattened in the process. */
3663 if (((gnu_variant_list.exists () && selected_variant)
3664 || (!gnu_variant_list.exists () && gnu_rep_part))
3665 && all_constant_pos)
3667 const int len = list_length (gnu_field_list);
3668 tree *field_arr = XALLOCAVEC (tree, len), t;
3671 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3674 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3676 gnu_field_list = NULL_TREE;
3677 for (i = 0; i < len; i++)
3679 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3680 gnu_field_list = field_arr[i];
3684 /* If there is a variant list and no selected variant, we need
3685 to create the nest of variant parts from the old nest. */
3686 else if (gnu_variant_list.exists () && !selected_variant)
3688 tree new_variant_part
3689 = create_variant_part_from (gnu_variant_part,
3690 gnu_variant_list, gnu_type,
3691 gnu_pos_list, gnu_subst_list);
3692 DECL_CHAIN (new_variant_part) = gnu_field_list;
3693 gnu_field_list = new_variant_part;
3696 /* Now go through the entities again looking for Itypes that
3697 we have not elaborated but should (e.g., Etypes of fields
3698 that have Original_Components). */
3699 for (gnat_field = First_Entity (gnat_entity);
3700 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3701 if ((Ekind (gnat_field) == E_Discriminant
3702 || Ekind (gnat_field) == E_Component)
3703 && !present_gnu_tree (Etype (gnat_field)))
3704 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3706 /* Do not emit debug info for the type yet since we're going to
3708 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3710 compute_record_mode (gnu_type);
3712 /* Fill in locations of fields. */
3713 annotate_rep (gnat_entity, gnu_type);
3715 /* If debugging information is being written for the type and if
3716 we are asked to output such encodings, write a record that
3717 shows what we are a subtype of and also make a variable that
3718 indicates our size, if still variable. */
3719 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
3721 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3722 tree gnu_unpad_base_name
3723 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3724 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3726 TYPE_NAME (gnu_subtype_marker)
3727 = create_concat_name (gnat_entity, "XVS");
3728 finish_record_type (gnu_subtype_marker,
3729 create_field_decl (gnu_unpad_base_name,
3730 build_reference_type
3731 (gnu_unpad_base_type),
3733 NULL_TREE, NULL_TREE,
3737 add_parallel_type (gnu_type, gnu_subtype_marker);
3740 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3741 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3742 TYPE_SIZE_UNIT (gnu_subtype_marker)
3743 = create_var_decl (create_concat_name (gnat_entity,
3745 NULL_TREE, sizetype, gnu_size_unit,
3746 false, false, false, false, false,
3751 gnu_variant_list.release ();
3752 gnu_subst_list.release ();
3754 /* Now we can finalize it. */
3755 rest_of_record_type_compilation (gnu_type);
3758 /* Otherwise, go down all the components in the new type and make
3759 them equivalent to those in the base type. */
3762 gnu_type = gnu_base_type;
3764 for (gnat_temp = First_Entity (gnat_entity);
3765 Present (gnat_temp);
3766 gnat_temp = Next_Entity (gnat_temp))
3767 if ((Ekind (gnat_temp) == E_Discriminant
3768 && !Is_Unchecked_Union (gnat_base_type))
3769 || Ekind (gnat_temp) == E_Component)
3770 save_gnu_tree (gnat_temp,
3771 gnat_to_gnu_field_decl
3772 (Original_Record_Component (gnat_temp)),
3778 case E_Access_Subprogram_Type:
3779 /* Use the special descriptor type for dispatch tables if needed,
3780 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3781 Note that we are only required to do so for static tables in
3782 order to be compatible with the C++ ABI, but Ada 2005 allows
3783 to extend library level tagged types at the local level so
3784 we do it in the non-static case as well. */
3785 if (TARGET_VTABLE_USES_DESCRIPTORS
3786 && Is_Dispatch_Table_Entity (gnat_entity))
3788 gnu_type = fdesc_type_node;
3789 gnu_size = TYPE_SIZE (gnu_type);
3793 /* ... fall through ... */
3795 case E_Anonymous_Access_Subprogram_Type:
3796 /* If we are not defining this entity, and we have incomplete
3797 entities being processed above us, make a dummy type and
3798 fill it in later. */
3799 if (!definition && defer_incomplete_level != 0)
3801 struct incomplete *p = XNEW (struct incomplete);
3804 = build_pointer_type
3805 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3806 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3807 artificial_p, debug_info_p,
3809 this_made_decl = true;
3810 gnu_type = TREE_TYPE (gnu_decl);
3811 save_gnu_tree (gnat_entity, gnu_decl, false);
3814 p->old_type = TREE_TYPE (gnu_type);
3815 p->full_type = Directly_Designated_Type (gnat_entity);
3816 p->next = defer_incomplete_list;
3817 defer_incomplete_list = p;
3821 /* ... fall through ... */
3823 case E_Allocator_Type:
3825 case E_Access_Attribute_Type:
3826 case E_Anonymous_Access_Type:
3827 case E_General_Access_Type:
3829 /* The designated type and its equivalent type for gigi. */
3830 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3831 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3832 /* Whether it comes from a limited with. */
3833 bool is_from_limited_with
3834 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3835 && From_Limited_With (gnat_desig_equiv));
3836 /* The "full view" of the designated type. If this is an incomplete
3837 entity from a limited with, treat its non-limited view as the full
3838 view. Otherwise, if this is an incomplete or private type, use the
3839 full view. In the former case, we might point to a private type,
3840 in which case, we need its full view. Also, we want to look at the
3841 actual type used for the representation, so this takes a total of
3843 Entity_Id gnat_desig_full_direct_first
3844 = (is_from_limited_with
3845 ? Non_Limited_View (gnat_desig_equiv)
3846 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3847 ? Full_View (gnat_desig_equiv) : Empty));
3848 Entity_Id gnat_desig_full_direct
3849 = ((is_from_limited_with
3850 && Present (gnat_desig_full_direct_first)
3851 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3852 ? Full_View (gnat_desig_full_direct_first)
3853 : gnat_desig_full_direct_first);
3854 Entity_Id gnat_desig_full
3855 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3856 /* The type actually used to represent the designated type, either
3857 gnat_desig_full or gnat_desig_equiv. */
3858 Entity_Id gnat_desig_rep;
3859 /* We want to know if we'll be seeing the freeze node for any
3860 incomplete type we may be pointing to. */
3862 = (Present (gnat_desig_full)
3863 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3864 : In_Extended_Main_Code_Unit (gnat_desig_type));
3865 /* True if we make a dummy type here. */
3866 bool made_dummy = false;
3867 /* The mode to be used for the pointer type. */
3868 machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3869 /* The GCC type used for the designated type. */
3870 tree gnu_desig_type = NULL_TREE;
3872 if (!targetm.valid_pointer_mode (p_mode))
3875 /* If either the designated type or its full view is an unconstrained
3876 array subtype, replace it with the type it's a subtype of. This
3877 avoids problems with multiple copies of unconstrained array types.
3878 Likewise, if the designated type is a subtype of an incomplete
3879 record type, use the parent type to avoid order of elaboration
3880 issues. This can lose some code efficiency, but there is no
3882 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3883 && !Is_Constrained (gnat_desig_equiv))
3884 gnat_desig_equiv = Etype (gnat_desig_equiv);
3885 if (Present (gnat_desig_full)
3886 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3887 && !Is_Constrained (gnat_desig_full))
3888 || (Ekind (gnat_desig_full) == E_Record_Subtype
3889 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3890 gnat_desig_full = Etype (gnat_desig_full);
3892 /* Set the type that's the representation of the designated type. */
3894 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3896 /* If we already know what the full type is, use it. */
3897 if (Present (gnat_desig_full) && present_gnu_tree (gnat_desig_full))
3898 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3900 /* Get the type of the thing we are to point to and build a pointer to
3901 it. If it is a reference to an incomplete or private type with a
3902 full view that is a record or an array, make a dummy type node and
3903 get the actual type later when we have verified it is safe. */
3904 else if ((!in_main_unit
3905 && !present_gnu_tree (gnat_desig_equiv)
3906 && Present (gnat_desig_full)
3907 && (Is_Record_Type (gnat_desig_full)
3908 || Is_Array_Type (gnat_desig_full)))
3909 /* Likewise if we are pointing to a record or array and we are
3910 to defer elaborating incomplete types. We do this as this
3911 access type may be the full view of a private type. */
3912 || ((!in_main_unit || imported_p)
3913 && defer_incomplete_level != 0
3914 && !present_gnu_tree (gnat_desig_equiv)
3915 && (Is_Record_Type (gnat_desig_rep)
3916 || Is_Array_Type (gnat_desig_rep)))
3917 /* If this is a reference from a limited_with type back to our
3918 main unit and there's a freeze node for it, either we have
3919 already processed the declaration and made the dummy type,
3920 in which case we just reuse the latter, or we have not yet,
3921 in which case we make the dummy type and it will be reused
3922 when the declaration is finally processed. In both cases,
3923 the pointer eventually created below will be automatically
3924 adjusted when the freeze node is processed. */
3926 && is_from_limited_with
3927 && Present (Freeze_Node (gnat_desig_rep))))
3929 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3933 /* Otherwise handle the case of a pointer to itself. */
3934 else if (gnat_desig_equiv == gnat_entity)
3937 = build_pointer_type_for_mode (void_type_node, p_mode,
3938 No_Strict_Aliasing (gnat_entity));
3939 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3942 /* If expansion is disabled, the equivalent type of a concurrent type
3943 is absent, so build a dummy pointer type. */
3944 else if (type_annotate_only && No (gnat_desig_equiv))
3945 gnu_type = ptr_type_node;
3947 /* Finally, handle the default case where we can just elaborate our
3950 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3952 /* It is possible that a call to gnat_to_gnu_type above resolved our
3953 type. If so, just return it. */
3954 if (present_gnu_tree (gnat_entity))
3956 maybe_present = true;
3960 /* For an unconstrained array, make dummy fat & thin pointer types. */
3961 if (Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep))
3963 /* If the processing above got something that has a pointer, then
3964 we are done. This could have happened either because the type
3965 was elaborated or because somebody else executed the code. */
3966 if (!TYPE_POINTER_TO (gnu_desig_type))
3967 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3968 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3971 /* If we haven't done it yet, build the pointer type the usual way. */
3974 /* Modify the designated type if we are pointing only to constant
3975 objects, but don't do it for unconstrained arrays. */
3976 if (Is_Access_Constant (gnat_entity)
3977 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3980 = change_qualified_type (gnu_desig_type, TYPE_QUAL_CONST);
3982 /* Some extra processing is required if we are building a
3983 pointer to an incomplete type (in the GCC sense). We might
3984 have such a type if we just made a dummy, or directly out
3985 of the call to gnat_to_gnu_type above if we are processing
3986 an access type for a record component designating the
3987 record type itself. */
3988 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3990 /* We must ensure that the pointer to variant we make will
3991 be processed by update_pointer_to when the initial type
3992 is completed. Pretend we made a dummy and let further
3993 processing act as usual. */
3996 /* We must ensure that update_pointer_to will not retrieve
3997 the dummy variant when building a properly qualified
3998 version of the complete type. We take advantage of the
3999 fact that get_qualified_type is requiring TYPE_NAMEs to
4000 match to influence build_qualified_type and then also
4001 update_pointer_to here. */
4002 TYPE_NAME (gnu_desig_type)
4003 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
4008 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
4009 No_Strict_Aliasing (gnat_entity));
4012 /* If we are not defining this object and we have made a dummy pointer,
4013 save our current definition, evaluate the actual type, and replace
4014 the tentative type we made with the actual one. If we are to defer
4015 actually looking up the actual type, make an entry in the deferred
4016 list. If this is from a limited with, we may have to defer to the
4017 end of the current unit. */
4018 if ((!in_main_unit || is_from_limited_with) && made_dummy)
4020 tree gnu_old_desig_type;
4022 if (TYPE_IS_FAT_POINTER_P (gnu_type))
4024 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
4025 if (esize == POINTER_SIZE)
4026 gnu_type = build_pointer_type
4027 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
4030 gnu_old_desig_type = TREE_TYPE (gnu_type);
4032 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4033 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
4034 artificial_p, debug_info_p,
4036 this_made_decl = true;
4037 gnu_type = TREE_TYPE (gnu_decl);
4038 save_gnu_tree (gnat_entity, gnu_decl, false);
4041 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4042 update gnu_old_desig_type directly, in which case it will not be
4043 a dummy type any more when we get into update_pointer_to.
4045 This can happen e.g. when the designated type is a record type,
4046 because their elaboration starts with an initial node from
4047 make_dummy_type, which may be the same node as the one we got.
4049 Besides, variants of this non-dummy type might have been created
4050 along the way. update_pointer_to is expected to properly take
4051 care of those situations. */
4052 if (defer_incomplete_level == 0 && !is_from_limited_with)
4054 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4055 gnat_to_gnu_type (gnat_desig_equiv));
4059 struct incomplete *p = XNEW (struct incomplete);
4060 struct incomplete **head
4061 = (is_from_limited_with
4062 ? &defer_limited_with : &defer_incomplete_list);
4063 p->old_type = gnu_old_desig_type;
4064 p->full_type = gnat_desig_equiv;
4072 case E_Access_Protected_Subprogram_Type:
4073 case E_Anonymous_Access_Protected_Subprogram_Type:
4074 if (type_annotate_only && No (gnat_equiv_type))
4075 gnu_type = ptr_type_node;
4078 /* The run-time representation is the equivalent type. */
4079 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4080 maybe_present = true;
4083 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4084 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4085 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4086 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4087 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4092 case E_Access_Subtype:
4094 /* We treat this as identical to its base type; any constraint is
4095 meaningful only to the front-end.
4097 The designated type must be elaborated as well, if it does
4098 not have its own freeze node. Designated (sub)types created
4099 for constrained components of records with discriminants are
4100 not frozen by the front-end and thus not elaborated by gigi,
4101 because their use may appear before the base type is frozen,
4102 and because it is not clear that they are needed anywhere in
4103 gigi. With the current model, there is no correct place where
4104 they could be elaborated. */
4106 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4107 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4108 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4109 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4110 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4112 /* If we are not defining this entity, and we have incomplete
4113 entities being processed above us, make a dummy type and
4114 elaborate it later. */
4115 if (!definition && defer_incomplete_level != 0)
4117 struct incomplete *p = XNEW (struct incomplete);
4120 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4121 p->full_type = Directly_Designated_Type (gnat_entity);
4122 p->next = defer_incomplete_list;
4123 defer_incomplete_list = p;
4125 else if (!IN (Ekind (Base_Type
4126 (Directly_Designated_Type (gnat_entity))),
4127 Incomplete_Or_Private_Kind))
4128 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4132 maybe_present = true;
4135 /* Subprogram Entities
4137 The following access functions are defined for subprograms:
4139 Etype Return type or Standard_Void_Type.
4140 First_Formal The first formal parameter.
4141 Is_Imported Indicates that the subprogram has appeared in
4142 an INTERFACE or IMPORT pragma. For now we
4143 assume that the external language is C.
4144 Is_Exported Likewise but for an EXPORT pragma.
4145 Is_Inlined True if the subprogram is to be inlined.
4147 Each parameter is first checked by calling must_pass_by_ref on its
4148 type to determine if it is passed by reference. For parameters which
4149 are copied in, if they are Ada In Out or Out parameters, their return
4150 value becomes part of a record which becomes the return type of the
4151 function (C function - note that this applies only to Ada procedures
4152 so there is no Ada return type). Additional code to store back the
4153 parameters will be generated on the caller side. This transformation
4154 is done here, not in the front-end.
4156 The intended result of the transformation can be seen from the
4157 equivalent source rewritings that follow:
4159 struct temp {int a,b};
4160 procedure P (A,B: In Out ...) is temp P (int A,B)
4163 end P; return {A,B};
4170 For subprogram types we need to perform mainly the same conversions to
4171 GCC form that are needed for procedures and function declarations. The
4172 only difference is that at the end, we make a type declaration instead
4173 of a function declaration. */
4175 case E_Subprogram_Type:
4179 /* The type returned by a function or else Standard_Void_Type for a
4181 Entity_Id gnat_return_type = Etype (gnat_entity);
4182 tree gnu_return_type;
4183 /* The first GCC parameter declaration (a PARM_DECL node). The
4184 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4185 actually is the head of this parameter list. */
4186 tree gnu_param_list = NULL_TREE;
4187 /* Non-null for subprograms containing parameters passed by copy-in
4188 copy-out (Ada In Out or Out parameters not passed by reference),
4189 in which case it is the list of nodes used to specify the values
4190 of the In Out/Out parameters that are returned as a record upon
4191 procedure return. The TREE_PURPOSE of an element of this list is
4192 a field of the record and the TREE_VALUE is the PARM_DECL
4193 corresponding to that field. This list will be saved in the
4194 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4195 tree gnu_cico_list = NULL_TREE;
4196 /* List of fields in return type of procedure with copy-in copy-out
4198 tree gnu_field_list = NULL_TREE;
4199 /* If an import pragma asks to map this subprogram to a GCC builtin,
4200 this is the builtin DECL node. */
4201 tree gnu_builtin_decl = NULL_TREE;
4202 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4203 Entity_Id gnat_param;
4204 enum inline_status_t inline_status
4205 = Has_Pragma_No_Inline (gnat_entity)
4207 : Has_Pragma_Inline_Always (gnat_entity)
4209 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4210 bool public_flag = Is_Public (gnat_entity) || imported_p;
4211 /* Subprograms marked both Intrinsic and Always_Inline need not
4212 have a body of their own. */
4214 = ((Is_Public (gnat_entity) && !definition)
4216 || (Convention (gnat_entity) == Convention_Intrinsic
4217 && Has_Pragma_Inline_Always (gnat_entity)));
4218 /* The semantics of "pure" in Ada essentially matches that of "const"
4219 in the back-end. In particular, both properties are orthogonal to
4220 the "nothrow" property if the EH circuitry is explicit in the
4221 internal representation of the back-end. If we are to completely
4222 hide the EH circuitry from it, we need to declare that calls to pure
4223 Ada subprograms that can throw have side effects since they can
4224 trigger an "abnormal" transfer of control flow; thus they can be
4225 neither "const" nor "pure" in the back-end sense. */
4226 bool const_flag = (Back_End_Exceptions () && Is_Pure (gnat_entity));
4227 bool volatile_flag = No_Return (gnat_entity);
4228 bool return_by_direct_ref_p = false;
4229 bool return_by_invisi_ref_p = false;
4230 bool return_unconstrained_p = false;
4233 /* A parameter may refer to this type, so defer completion of any
4234 incomplete types. */
4235 if (kind == E_Subprogram_Type && !definition)
4237 defer_incomplete_level++;
4238 this_deferred = true;
4241 /* If the subprogram has an alias, it is probably inherited, so
4242 we can use the original one. If the original "subprogram"
4243 is actually an enumeration literal, it may be the first use
4244 of its type, so we must elaborate that type now. */
4245 if (Present (Alias (gnat_entity)))
4247 const Entity_Id gnat_renamed = Renamed_Object (gnat_entity);
4249 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4250 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4252 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4254 /* Elaborate any Itypes in the parameters of this entity. */
4255 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4256 Present (gnat_temp);
4257 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4258 if (Is_Itype (Etype (gnat_temp)))
4259 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4261 /* Materialize renamed subprograms in the debugging information
4262 when the renamed object is compile time known. We can consider
4263 such renamings as imported declarations.
4265 Because the parameters in generics instantiation are generally
4266 materialized as renamings, we ofter end up having both the
4267 renamed subprogram and the renaming in the same context and with
4268 the same name: in this case, renaming is both useless debug-wise
4269 and potentially harmful as name resolution in the debugger could
4270 return twice the same entity! So avoid this case. */
4271 if (debug_info_p && !artificial_p
4272 && !(get_debug_scope (gnat_entity, NULL)
4273 == get_debug_scope (gnat_renamed, NULL)
4274 && Name_Equals (Chars (gnat_entity),
4275 Chars (gnat_renamed)))
4276 && Present (gnat_renamed)
4277 && (Ekind (gnat_renamed) == E_Function
4278 || Ekind (gnat_renamed) == E_Procedure)
4280 && TREE_CODE (gnu_decl) == FUNCTION_DECL)
4282 tree decl = build_decl (input_location, IMPORTED_DECL,
4283 gnu_entity_name, void_type_node);
4284 IMPORTED_DECL_ASSOCIATED_DECL (decl) = gnu_decl;
4285 gnat_pushdecl (decl, gnat_entity);
4291 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4292 corresponding DECL node. Proper generation of calls later on need
4293 proper parameter associations so we don't "break;" here. */
4294 if (Convention (gnat_entity) == Convention_Intrinsic
4295 && Present (Interface_Name (gnat_entity)))
4297 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4299 /* Inability to find the builtin decl most often indicates a
4300 genuine mistake, but imports of unregistered intrinsics are
4301 sometimes issued on purpose to allow hooking in alternate
4302 bodies. We post a warning conditioned on Wshadow in this case,
4303 to let developers be notified on demand without risking false
4304 positives with common default sets of options. */
4306 if (!gnu_builtin_decl && warn_shadow)
4307 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4310 /* ??? What if we don't find the builtin node above ? warn ? err ?
4311 In the current state we neither warn nor err, and calls will just
4312 be handled as for regular subprograms. */
4314 /* Look into the return type and get its associated GCC tree. If it
4315 is not void, compute various flags for the subprogram type. */
4316 if (Ekind (gnat_return_type) == E_Void)
4317 gnu_return_type = void_type_node;
4320 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4321 context may now appear in parameter and result profiles. If
4322 we are only annotating types, break circularities here. */
4323 if (type_annotate_only
4324 && is_from_limited_with_of_main (gnat_return_type))
4325 gnu_return_type = void_type_node;
4327 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4329 /* If this function returns by reference, make the actual return
4330 type the pointer type and make a note of that. */
4331 if (Returns_By_Ref (gnat_entity))
4333 gnu_return_type = build_reference_type (gnu_return_type);
4334 return_by_direct_ref_p = true;
4337 /* If the return type is an unconstrained array type, the return
4338 value will be allocated on the secondary stack so the actual
4339 return type is the fat pointer type. */
4340 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4342 gnu_return_type = TREE_TYPE (gnu_return_type);
4343 return_unconstrained_p = true;
4346 /* Likewise, if the return type requires a transient scope, the
4347 return value will also be allocated on the secondary stack so
4348 the actual return type is the pointer type. */
4349 else if (Requires_Transient_Scope (gnat_return_type))
4351 gnu_return_type = build_reference_type (gnu_return_type);
4352 return_unconstrained_p = true;
4355 /* If the Mechanism is By_Reference, ensure this function uses the
4356 target's by-invisible-reference mechanism, which may not be the
4357 same as above (e.g. it might be passing an extra parameter). */
4358 else if (kind == E_Function
4359 && Mechanism (gnat_entity) == By_Reference)
4360 return_by_invisi_ref_p = true;
4362 /* Likewise, if the return type is itself By_Reference. */
4363 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4364 return_by_invisi_ref_p = true;
4366 /* If the type is a padded type and the underlying type would not
4367 be passed by reference or the function has a foreign convention,
4368 return the underlying type. */
4369 else if (TYPE_IS_PADDING_P (gnu_return_type)
4370 && (!default_pass_by_ref
4371 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4372 || Has_Foreign_Convention (gnat_entity)))
4373 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4375 /* If the return type is unconstrained, that means it must have a
4376 maximum size. Use the padded type as the effective return type.
4377 And ensure the function uses the target's by-invisible-reference
4378 mechanism to avoid copying too much data when it returns. */
4379 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4381 tree orig_type = gnu_return_type;
4382 tree max_return_size
4383 = max_size (TYPE_SIZE (gnu_return_type), true);
4385 /* If the size overflows to 0, set it to an arbitrary positive
4386 value so that assignments in the type are preserved. Their
4387 actual size is independent of this positive value. */
4388 if (TREE_CODE (max_return_size) == INTEGER_CST
4389 && TREE_OVERFLOW (max_return_size)
4390 && integer_zerop (max_return_size))
4392 max_return_size = copy_node (bitsize_unit_node);
4393 TREE_OVERFLOW (max_return_size) = 1;
4397 = maybe_pad_type (gnu_return_type, max_return_size, 0,
4398 gnat_entity, false, false, definition,
4401 /* Declare it now since it will never be declared otherwise.
4402 This is necessary to ensure that its subtrees are properly
4404 if (gnu_return_type != orig_type
4405 && !DECL_P (TYPE_NAME (gnu_return_type)))
4406 create_type_decl (TYPE_NAME (gnu_return_type),
4407 gnu_return_type, true, debug_info_p,
4410 return_by_invisi_ref_p = true;
4413 /* If the return type has a size that overflows, we cannot have
4414 a function that returns that type. This usage doesn't make
4415 sense anyway, so give an error here. */
4416 if (!return_by_invisi_ref_p
4417 && TYPE_SIZE_UNIT (gnu_return_type)
4418 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4419 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4421 post_error ("cannot return type whose size overflows",
4423 gnu_return_type = copy_node (gnu_return_type);
4424 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4425 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4426 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4427 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4431 /* Loop over the parameters and get their associated GCC tree. While
4432 doing this, build a copy-in copy-out structure if we need one. */
4433 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4434 Present (gnat_param);
4435 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4437 Entity_Id gnat_param_type = Etype (gnat_param);
4438 tree gnu_param_name = get_entity_name (gnat_param);
4439 tree gnu_param_type, gnu_param, gnu_field;
4440 Mechanism_Type mech = Mechanism (gnat_param);
4441 bool copy_in_copy_out = false, fake_param_type;
4443 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4444 context may now appear in parameter and result profiles. If
4445 we are only annotating types, break circularities here. */
4446 if (type_annotate_only
4447 && is_from_limited_with_of_main (gnat_param_type))
4449 gnu_param_type = void_type_node;
4450 fake_param_type = true;
4454 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4455 fake_param_type = false;
4458 /* Builtins are expanded inline and there is no real call sequence
4459 involved. So the type expected by the underlying expander is
4460 always the type of each argument "as is". */
4461 if (gnu_builtin_decl)
4463 /* Handle the first parameter of a valued procedure specially. */
4464 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4465 mech = By_Copy_Return;
4466 /* Otherwise, see if a Mechanism was supplied that forced this
4467 parameter to be passed one way or another. */
4468 else if (mech == Default
4470 || mech == By_Reference)
4474 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4475 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4476 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4478 mech = By_Reference;
4484 post_error ("unsupported mechanism for&", gnat_param);
4488 /* Do not call gnat_to_gnu_param for a fake parameter type since
4489 it will try to use the real type again. */
4490 if (fake_param_type)
4492 if (Ekind (gnat_param) == E_Out_Parameter)
4493 gnu_param = NULL_TREE;
4497 = create_param_decl (gnu_param_name, gnu_param_type,
4499 Set_Mechanism (gnat_param,
4500 mech == Default ? By_Copy : mech);
4501 if (Ekind (gnat_param) == E_In_Out_Parameter)
4502 copy_in_copy_out = true;
4507 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4508 Has_Foreign_Convention (gnat_entity),
4511 /* We are returned either a PARM_DECL or a type if no parameter
4512 needs to be passed; in either case, adjust the type. */
4513 if (DECL_P (gnu_param))
4514 gnu_param_type = TREE_TYPE (gnu_param);
4517 gnu_param_type = gnu_param;
4518 gnu_param = NULL_TREE;
4521 /* The failure of this assertion will very likely come from an
4522 order of elaboration issue for the type of the parameter. */
4523 gcc_assert (kind == E_Subprogram_Type
4524 || !TYPE_IS_DUMMY_P (gnu_param_type)
4525 || type_annotate_only);
4529 gnu_param_list = chainon (gnu_param, gnu_param_list);
4530 Sloc_to_locus (Sloc (gnat_param),
4531 &DECL_SOURCE_LOCATION (gnu_param));
4532 save_gnu_tree (gnat_param, gnu_param, false);
4534 /* If a parameter is a pointer, this function may modify
4535 memory through it and thus shouldn't be considered
4536 a const function. Also, the memory may be modified
4537 between two calls, so they can't be CSE'ed. The latter
4538 case also handles by-ref parameters. */
4539 if (POINTER_TYPE_P (gnu_param_type)
4540 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4544 if (copy_in_copy_out)
4548 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4550 /* If this is a function, we also need a field for the
4551 return value to be placed. */
4552 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4555 = create_field_decl (get_identifier ("RETVAL"),
4557 gnu_new_ret_type, NULL_TREE,
4559 Sloc_to_locus (Sloc (gnat_entity),
4560 &DECL_SOURCE_LOCATION (gnu_field));
4561 gnu_field_list = gnu_field;
4563 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4566 gnu_return_type = gnu_new_ret_type;
4567 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4568 /* Set a default alignment to speed up accesses. But we
4569 shouldn't increase the size of the structure too much,
4570 lest it doesn't fit in return registers anymore. */
4571 TYPE_ALIGN (gnu_return_type)
4572 = get_mode_alignment (ptr_mode);
4576 = create_field_decl (gnu_param_name, gnu_param_type,
4577 gnu_return_type, NULL_TREE, NULL_TREE,
4579 Sloc_to_locus (Sloc (gnat_param),
4580 &DECL_SOURCE_LOCATION (gnu_field));
4581 DECL_CHAIN (gnu_field) = gnu_field_list;
4582 gnu_field_list = gnu_field;
4584 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4590 /* If we have a CICO list but it has only one entry, we convert
4591 this function into a function that returns this object. */
4592 if (list_length (gnu_cico_list) == 1)
4593 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4595 /* Do not finalize the return type if the subprogram is stubbed
4596 since structures are incomplete for the back-end. */
4597 else if (Convention (gnat_entity) != Convention_Stubbed)
4599 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4602 /* Try to promote the mode of the return type if it is passed
4603 in registers, again to speed up accesses. */
4604 if (TYPE_MODE (gnu_return_type) == BLKmode
4605 && !targetm.calls.return_in_memory (gnu_return_type,
4609 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4610 unsigned int i = BITS_PER_UNIT;
4615 mode = mode_for_size (i, MODE_INT, 0);
4616 if (mode != BLKmode)
4618 SET_TYPE_MODE (gnu_return_type, mode);
4619 TYPE_ALIGN (gnu_return_type)
4620 = GET_MODE_ALIGNMENT (mode);
4621 TYPE_SIZE (gnu_return_type)
4622 = bitsize_int (GET_MODE_BITSIZE (mode));
4623 TYPE_SIZE_UNIT (gnu_return_type)
4624 = size_int (GET_MODE_SIZE (mode));
4629 rest_of_record_type_compilation (gnu_return_type);
4633 /* Deal with platform-specific calling conventions. */
4634 if (Has_Stdcall_Convention (gnat_entity))
4635 prepend_one_attribute
4636 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4637 get_identifier ("stdcall"), NULL_TREE,
4639 else if (Has_Thiscall_Convention (gnat_entity))
4640 prepend_one_attribute
4641 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4642 get_identifier ("thiscall"), NULL_TREE,
4645 /* If we should request stack realignment for a foreign convention
4646 subprogram, do so. Note that this applies to task entry points
4648 if (FOREIGN_FORCE_REALIGN_STACK
4649 && Has_Foreign_Convention (gnat_entity))
4650 prepend_one_attribute
4651 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4652 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4655 /* Deal with a pragma Linker_Section on a subprogram. */
4656 if ((kind == E_Function || kind == E_Procedure)
4657 && Present (Linker_Section_Pragma (gnat_entity)))
4658 prepend_one_attribute_pragma (&attr_list,
4659 Linker_Section_Pragma (gnat_entity));
4661 /* The lists have been built in reverse. */
4662 gnu_param_list = nreverse (gnu_param_list);
4663 gnu_cico_list = nreverse (gnu_cico_list);
4665 if (kind == E_Function)
4666 Set_Mechanism (gnat_entity, return_unconstrained_p
4667 || return_by_direct_ref_p
4668 || return_by_invisi_ref_p
4669 ? By_Reference : By_Copy);
4671 = create_subprog_type (gnu_return_type, gnu_param_list,
4672 gnu_cico_list, return_unconstrained_p,
4673 return_by_direct_ref_p,
4674 return_by_invisi_ref_p);
4676 /* A procedure (something that doesn't return anything) shouldn't be
4677 considered const since there would be no reason for calling such a
4678 subprogram. Note that procedures with Out (or In Out) parameters
4679 have already been converted into a function with a return type.
4680 Similarly, if the function returns an unconstrained type, then the
4681 function will allocate the return value on the secondary stack and
4682 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
4683 if (TREE_CODE (gnu_return_type) == VOID_TYPE || return_unconstrained_p)
4686 /* If we have a builtin decl for that function, use it. Check if the
4687 profiles are compatible and warn if they are not. The checker is
4688 expected to post extra diagnostics in this case. */
4689 if (gnu_builtin_decl)
4691 intrin_binding_t inb;
4693 inb.gnat_entity = gnat_entity;
4694 inb.ada_fntype = gnu_type;
4695 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4697 if (!intrin_profiles_compatible_p (&inb))
4699 ("?profile of& doesn''t match the builtin it binds!",
4702 gnu_decl = gnu_builtin_decl;
4703 gnu_type = TREE_TYPE (gnu_builtin_decl);
4707 /* If there was no specified Interface_Name and the external and
4708 internal names of the subprogram are the same, only use the
4709 internal name to allow disambiguation of nested subprograms. */
4710 if (No (Interface_Name (gnat_entity))
4711 && gnu_ext_name == gnu_entity_name)
4712 gnu_ext_name = NULL_TREE;
4714 /* If we are defining the subprogram and it has an Address clause
4715 we must get the address expression from the saved GCC tree for the
4716 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4717 the address expression here since the front-end has guaranteed
4718 in that case that the elaboration has no effects. If there is
4719 an Address clause and we are not defining the object, just
4720 make it a constant. */
4721 if (Present (Address_Clause (gnat_entity)))
4723 tree gnu_address = NULL_TREE;
4727 = (present_gnu_tree (gnat_entity)
4728 ? get_gnu_tree (gnat_entity)
4729 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4731 save_gnu_tree (gnat_entity, NULL_TREE, false);
4733 /* Convert the type of the object to a reference type that can
4734 alias everything as per RM 13.3(19). */
4736 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4738 gnu_address = convert (gnu_type, gnu_address);
4741 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4742 gnu_address, false, Is_Public (gnat_entity),
4743 extern_flag, false, false, artificial_p,
4744 debug_info_p, NULL, gnat_entity);
4745 DECL_BY_REF_P (gnu_decl) = 1;
4748 else if (kind == E_Subprogram_Type)
4750 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4752 if (const_flag || volatile_flag)
4755 = (const_flag ? TYPE_QUAL_CONST : 0)
4756 | (volatile_flag ? TYPE_QUAL_VOLATILE : 0);
4757 gnu_type = change_qualified_type (gnu_type, quals);
4761 = create_type_decl (gnu_entity_name, gnu_type, artificial_p,
4762 debug_info_p, gnat_entity);
4767 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4768 gnu_param_list, inline_status, const_flag,
4769 public_flag, extern_flag, volatile_flag,
4770 artificial_p, debug_info_p,
4771 attr_list, gnat_entity);
4772 /* This is unrelated to the stub built right above. */
4773 DECL_STUBBED_P (gnu_decl)
4774 = Convention (gnat_entity) == Convention_Stubbed;
4779 case E_Incomplete_Type:
4780 case E_Incomplete_Subtype:
4781 case E_Private_Type:
4782 case E_Private_Subtype:
4783 case E_Limited_Private_Type:
4784 case E_Limited_Private_Subtype:
4785 case E_Record_Type_With_Private:
4786 case E_Record_Subtype_With_Private:
4788 bool is_from_limited_with
4789 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity));
4790 /* Get the "full view" of this entity. If this is an incomplete
4791 entity from a limited with, treat its non-limited view as the
4792 full view. Otherwise, use either the full view or the underlying
4793 full view, whichever is present. This is used in all the tests
4796 = is_from_limited_with
4797 ? Non_Limited_View (gnat_entity)
4798 : Present (Full_View (gnat_entity))
4799 ? Full_View (gnat_entity)
4800 : IN (kind, Private_Kind)
4801 ? Underlying_Full_View (gnat_entity)
4804 /* If this is an incomplete type with no full view, it must be a Taft
4805 Amendment type, in which case we return a dummy type. Otherwise,
4806 just get the type from its Etype. */
4809 if (kind == E_Incomplete_Type)
4811 gnu_type = make_dummy_type (gnat_entity);
4812 gnu_decl = TYPE_STUB_DECL (gnu_type);
4816 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4818 maybe_present = true;
4823 /* If we already made a type for the full view, reuse it. */
4824 else if (present_gnu_tree (full_view))
4826 gnu_decl = get_gnu_tree (full_view);
4830 /* Otherwise, if we are not defining the type now, get the type
4831 from the full view. But always get the type from the full view
4832 for define on use types, since otherwise we won't see them.
4833 Likewise if this is a non-limited view not declared in the main
4834 unit, which can happen for incomplete formal types instantiated
4835 on a type coming from a limited_with clause. */
4836 else if (!definition
4837 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4838 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view)))
4839 || (is_from_limited_with
4840 && !In_Extended_Main_Code_Unit (full_view)))
4842 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4843 maybe_present = true;
4847 /* For incomplete types, make a dummy type entry which will be
4848 replaced later. Save it as the full declaration's type so
4849 we can do any needed updates when we see it. */
4850 gnu_type = make_dummy_type (gnat_entity);
4851 gnu_decl = TYPE_STUB_DECL (gnu_type);
4852 if (Has_Completion_In_Body (gnat_entity))
4853 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4854 save_gnu_tree (full_view, gnu_decl, 0);
4858 case E_Class_Wide_Type:
4859 /* Class-wide types are always transformed into their root type. */
4860 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4861 maybe_present = true;
4864 case E_Protected_Type:
4865 case E_Protected_Subtype:
4867 case E_Task_Subtype:
4868 /* If we are just annotating types and have no equivalent record type,
4869 just return void_type, except for root types that have discriminants
4870 because the discriminants will very likely be used in the declarative
4871 part of the associated body so they need to be translated. */
4872 if (type_annotate_only && No (gnat_equiv_type))
4874 if (Has_Discriminants (gnat_entity)
4875 && Root_Type (gnat_entity) == gnat_entity)
4877 tree gnu_field_list = NULL_TREE;
4878 Entity_Id gnat_field;
4880 /* This is a minimal version of the E_Record_Type handling. */
4881 gnu_type = make_node (RECORD_TYPE);
4882 TYPE_NAME (gnu_type) = gnu_entity_name;
4884 for (gnat_field = First_Stored_Discriminant (gnat_entity);
4885 Present (gnat_field);
4886 gnat_field = Next_Stored_Discriminant (gnat_field))
4889 = gnat_to_gnu_field (gnat_field, gnu_type, false,
4890 definition, debug_info_p);
4892 save_gnu_tree (gnat_field,
4893 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
4894 build0 (PLACEHOLDER_EXPR, gnu_type),
4895 gnu_field, NULL_TREE),
4898 DECL_CHAIN (gnu_field) = gnu_field_list;
4899 gnu_field_list = gnu_field;
4902 finish_record_type (gnu_type, nreverse (gnu_field_list), 0,
4906 gnu_type = void_type_node;
4909 /* Concurrent types are always transformed into their record type. */
4911 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4912 maybe_present = true;
4916 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4921 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4922 we've already saved it, so we don't try to. */
4923 gnu_decl = error_mark_node;
4927 case E_Abstract_State:
4928 /* This is a SPARK annotation that only reaches here when compiling in
4930 gcc_assert (type_annotate_only);
4931 gnu_decl = error_mark_node;
4939 /* If we had a case where we evaluated another type and it might have
4940 defined this one, handle it here. */
4941 if (maybe_present && present_gnu_tree (gnat_entity))
4943 gnu_decl = get_gnu_tree (gnat_entity);
4947 /* If we are processing a type and there is either no decl for it or
4948 we just made one, do some common processing for the type, such as
4949 handling alignment and possible padding. */
4950 if (is_type && (!gnu_decl || this_made_decl))
4952 /* Process the attributes, if not already done. Note that the type is
4953 already defined so we cannot pass true for IN_PLACE here. */
4954 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4956 /* Tell the middle-end that objects of tagged types are guaranteed to
4957 be properly aligned. This is necessary because conversions to the
4958 class-wide type are translated into conversions to the root type,
4959 which can be less aligned than some of its derived types. */
4960 if (Is_Tagged_Type (gnat_entity)
4961 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4962 TYPE_ALIGN_OK (gnu_type) = 1;
4964 /* Record whether the type is passed by reference. */
4965 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4966 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4968 /* ??? Don't set the size for a String_Literal since it is either
4969 confirming or we don't handle it properly (if the low bound is
4971 if (!gnu_size && kind != E_String_Literal_Subtype)
4973 Uint gnat_size = Known_Esize (gnat_entity)
4974 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4976 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4977 false, Has_Size_Clause (gnat_entity));
4980 /* If a size was specified, see if we can make a new type of that size
4981 by rearranging the type, for example from a fat to a thin pointer. */
4985 = make_type_from_size (gnu_type, gnu_size,
4986 Has_Biased_Representation (gnat_entity));
4988 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4989 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4990 gnu_size = NULL_TREE;
4993 /* If the alignment has not already been processed and this is not
4994 an unconstrained array type, see if an alignment is specified.
4995 If not, we pick a default alignment for atomic objects. */
4996 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4998 else if (Known_Alignment (gnat_entity))
5000 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
5001 TYPE_ALIGN (gnu_type));
5003 /* Warn on suspiciously large alignments. This should catch
5004 errors about the (alignment,byte)/(size,bit) discrepancy. */
5005 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
5009 /* If a size was specified, take it into account. Otherwise
5010 use the RM size for records or unions as the type size has
5011 already been adjusted to the alignment. */
5014 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
5015 && !TYPE_FAT_POINTER_P (gnu_type))
5016 size = rm_size (gnu_type);
5018 size = TYPE_SIZE (gnu_type);
5020 /* Consider an alignment as suspicious if the alignment/size
5021 ratio is greater or equal to the byte/bit ratio. */
5022 if (tree_fits_uhwi_p (size)
5023 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
5024 post_error_ne ("?suspiciously large alignment specified for&",
5025 Expression (Alignment_Clause (gnat_entity)),
5029 else if (Is_Atomic_Or_VFA (gnat_entity) && !gnu_size
5030 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
5031 && integer_pow2p (TYPE_SIZE (gnu_type)))
5032 align = MIN (BIGGEST_ALIGNMENT,
5033 tree_to_uhwi (TYPE_SIZE (gnu_type)));
5034 else if (Is_Atomic_Or_VFA (gnat_entity) && gnu_size
5035 && tree_fits_uhwi_p (gnu_size)
5036 && integer_pow2p (gnu_size))
5037 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
5039 /* See if we need to pad the type. If we did, and made a record,
5040 the name of the new type may be changed. So get it back for
5041 us when we make the new TYPE_DECL below. */
5042 if (gnu_size || align > 0)
5043 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
5044 false, !gnu_decl, definition, false);
5046 if (TYPE_IS_PADDING_P (gnu_type))
5047 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
5049 /* Now set the RM size of the type. We cannot do it before padding
5050 because we need to accept arbitrary RM sizes on integral types. */
5051 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
5053 /* If we are at global level, GCC will have applied variable_size to
5054 the type, but that won't have done anything. So, if it's not
5055 a constant or self-referential, call elaborate_expression_1 to
5056 make a variable for the size rather than calculating it each time.
5057 Handle both the RM size and the actual size. */
5058 if (global_bindings_p ()
5059 && TYPE_SIZE (gnu_type)
5060 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
5061 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5063 tree size = TYPE_SIZE (gnu_type);
5065 TYPE_SIZE (gnu_type)
5066 = elaborate_expression_1 (size, gnat_entity, "SIZE", definition,
5069 /* ??? For now, store the size as a multiple of the alignment in
5070 bytes so that we can see the alignment from the tree. */
5071 TYPE_SIZE_UNIT (gnu_type)
5072 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
5073 "SIZE_A_UNIT", definition, false,
5074 TYPE_ALIGN (gnu_type));
5076 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
5077 may not be marked by the call to create_type_decl below. */
5078 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
5080 if (TREE_CODE (gnu_type) == RECORD_TYPE)
5082 tree variant_part = get_variant_part (gnu_type);
5083 tree ada_size = TYPE_ADA_SIZE (gnu_type);
5087 tree union_type = TREE_TYPE (variant_part);
5088 tree offset = DECL_FIELD_OFFSET (variant_part);
5090 /* If the position of the variant part is constant, subtract
5091 it from the size of the type of the parent to get the new
5092 size. This manual CSE reduces the data size. */
5093 if (TREE_CODE (offset) == INTEGER_CST)
5095 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
5096 TYPE_SIZE (union_type)
5097 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
5098 bit_from_pos (offset, bitpos));
5099 TYPE_SIZE_UNIT (union_type)
5100 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
5101 byte_from_pos (offset, bitpos));
5105 TYPE_SIZE (union_type)
5106 = elaborate_expression_1 (TYPE_SIZE (union_type),
5107 gnat_entity, "VSIZE",
5110 /* ??? For now, store the size as a multiple of the
5111 alignment in bytes so that we can see the alignment
5113 TYPE_SIZE_UNIT (union_type)
5114 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5115 gnat_entity, "VSIZE_A_UNIT",
5117 TYPE_ALIGN (union_type));
5119 /* ??? For now, store the offset as a multiple of the
5120 alignment in bytes so that we can see the alignment
5122 DECL_FIELD_OFFSET (variant_part)
5123 = elaborate_expression_2 (offset, gnat_entity,
5124 "VOFFSET", definition, false,
5129 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5130 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5133 if (operand_equal_p (ada_size, size, 0))
5134 ada_size = TYPE_SIZE (gnu_type);
5137 = elaborate_expression_1 (ada_size, gnat_entity, "RM_SIZE",
5139 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5143 /* If this is a record type or subtype, call elaborate_expression_2 on
5144 any field position. Do this for both global and local types.
5145 Skip any fields that we haven't made trees for to avoid problems with
5146 class wide types. */
5147 if (IN (kind, Record_Kind))
5148 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5149 gnat_temp = Next_Entity (gnat_temp))
5150 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5152 tree gnu_field = get_gnu_tree (gnat_temp);
5154 /* ??? For now, store the offset as a multiple of the alignment
5155 in bytes so that we can see the alignment from the tree. */
5156 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5158 DECL_FIELD_OFFSET (gnu_field)
5159 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5160 gnat_temp, "OFFSET", definition,
5162 DECL_OFFSET_ALIGN (gnu_field));
5164 /* ??? The context of gnu_field is not necessarily gnu_type
5165 so the MULT_EXPR node built above may not be marked by
5166 the call to create_type_decl below. */
5167 if (global_bindings_p ())
5168 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5172 if (Is_Atomic_Or_VFA (gnat_entity))
5173 check_ok_for_atomic_type (gnu_type, gnat_entity, false);
5175 /* If this is not an unconstrained array type, set some flags. */
5176 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5178 if (Present (Alignment_Clause (gnat_entity)))
5179 TYPE_USER_ALIGN (gnu_type) = 1;
5181 if (Universal_Aliasing (gnat_entity))
5182 TYPE_UNIVERSAL_ALIASING_P (gnu_type) = 1;
5184 /* If it is passed by reference, force BLKmode to ensure that
5185 objects of this type will always be put in memory. */
5186 if (TYPE_MODE (gnu_type) != BLKmode
5187 && AGGREGATE_TYPE_P (gnu_type)
5188 && TYPE_BY_REFERENCE_P (gnu_type))
5189 SET_TYPE_MODE (gnu_type, BLKmode);
5191 if (Treat_As_Volatile (gnat_entity))
5194 = TYPE_QUAL_VOLATILE
5195 | (Is_Atomic_Or_VFA (gnat_entity) ? TYPE_QUAL_ATOMIC : 0);
5196 gnu_type = change_qualified_type (gnu_type, quals);
5201 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5202 artificial_p, debug_info_p,
5206 TREE_TYPE (gnu_decl) = gnu_type;
5207 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5211 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5213 gnu_type = TREE_TYPE (gnu_decl);
5215 /* If this is a derived type, relate its alias set to that of its parent
5216 to avoid troubles when a call to an inherited primitive is inlined in
5217 a context where a derived object is accessed. The inlined code works
5218 on the parent view so the resulting code may access the same object
5219 using both the parent and the derived alias sets, which thus have to
5220 conflict. As the same issue arises with component references, the
5221 parent alias set also has to conflict with composite types enclosing
5222 derived components. For instance, if we have:
5229 we want T to conflict with both D and R, in addition to R being a
5230 superset of D by record/component construction.
5232 One way to achieve this is to perform an alias set copy from the
5233 parent to the derived type. This is not quite appropriate, though,
5234 as we don't want separate derived types to conflict with each other:
5236 type I1 is new Integer;
5237 type I2 is new Integer;
5239 We want I1 and I2 to both conflict with Integer but we do not want
5240 I1 to conflict with I2, and an alias set copy on derivation would
5243 The option chosen is to make the alias set of the derived type a
5244 superset of that of its parent type. It trivially fulfills the
5245 simple requirement for the Integer derivation example above, and
5246 the component case as well by superset transitivity:
5249 R ----------> D ----------> T
5251 However, for composite types, conversions between derived types are
5252 translated into VIEW_CONVERT_EXPRs so a sequence like:
5254 type Comp1 is new Comp;
5255 type Comp2 is new Comp;
5256 procedure Proc (C : Comp1);
5264 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5266 and gimplified into:
5273 i.e. generates code involving type punning. Therefore, Comp1 needs
5274 to conflict with Comp2 and an alias set copy is required.
5276 The language rules ensure the parent type is already frozen here. */
5277 if (kind != E_Subprogram_Type
5278 && Is_Derived_Type (gnat_entity)
5279 && !type_annotate_only)
5281 Entity_Id gnat_parent_type = Underlying_Type (Etype (gnat_entity));
5282 /* For constrained packed array subtypes, the implementation type is
5283 used instead of the nominal type. */
5284 if (kind == E_Array_Subtype
5285 && Is_Constrained (gnat_entity)
5286 && Present (Packed_Array_Impl_Type (gnat_parent_type)))
5287 gnat_parent_type = Packed_Array_Impl_Type (gnat_parent_type);
5288 relate_alias_sets (gnu_type, gnat_to_gnu_type (gnat_parent_type),
5289 Is_Composite_Type (gnat_entity)
5290 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5293 /* Back-annotate the Alignment of the type if not already in the
5294 tree. Likewise for sizes. */
5295 if (Unknown_Alignment (gnat_entity))
5297 unsigned int double_align, align;
5298 bool is_capped_double, align_clause;
5300 /* If the default alignment of "double" or larger scalar types is
5301 specifically capped and this is not an array with an alignment
5302 clause on the component type, return the cap. */
5303 if ((double_align = double_float_alignment) > 0)
5305 = is_double_float_or_array (gnat_entity, &align_clause);
5306 else if ((double_align = double_scalar_alignment) > 0)
5308 = is_double_scalar_or_array (gnat_entity, &align_clause);
5310 is_capped_double = align_clause = false;
5312 if (is_capped_double && !align_clause)
5313 align = double_align;
5315 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5317 Set_Alignment (gnat_entity, UI_From_Int (align));
5320 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5322 tree gnu_size = TYPE_SIZE (gnu_type);
5324 /* If the size is self-referential, annotate the maximum value. */
5325 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5326 gnu_size = max_size (gnu_size, true);
5328 /* If we are just annotating types and the type is tagged, the tag
5329 and the parent components are not generated by the front-end so
5330 alignment and sizes must be adjusted if there is no rep clause. */
5331 if (type_annotate_only
5332 && Is_Tagged_Type (gnat_entity)
5333 && Unknown_RM_Size (gnat_entity)
5334 && !VOID_TYPE_P (gnu_type)
5335 && (!TYPE_FIELDS (gnu_type)
5336 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5340 if (Is_Derived_Type (gnat_entity))
5342 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5343 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5344 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5349 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
5350 offset = bitsize_int (POINTER_SIZE);
5351 Set_Alignment (gnat_entity, UI_From_Int (align));
5354 if (TYPE_FIELDS (gnu_type))
5356 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5358 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5359 gnu_size = round_up (gnu_size, POINTER_SIZE);
5360 Uint uint_size = annotate_value (gnu_size);
5361 Set_RM_Size (gnat_entity, uint_size);
5362 Set_Esize (gnat_entity, uint_size);
5365 /* If there is a rep clause, only adjust alignment and Esize. */
5366 else if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5369 = MAX (TYPE_ALIGN (gnu_type), POINTER_SIZE) / BITS_PER_UNIT;
5370 Set_Alignment (gnat_entity, UI_From_Int (align));
5371 gnu_size = round_up (gnu_size, POINTER_SIZE);
5372 Set_Esize (gnat_entity, annotate_value (gnu_size));
5375 /* Otherwise no adjustment is needed. */
5377 Set_Esize (gnat_entity, annotate_value (gnu_size));
5380 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5381 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5384 /* If we haven't already, associate the ..._DECL node that we just made with
5385 the input GNAT entity node. */
5387 save_gnu_tree (gnat_entity, gnu_decl, false);
5389 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
5390 eliminate as many deferred computations as possible. */
5391 process_deferred_decl_context (false);
5393 /* If this is an enumeration or floating-point type, we were not able to set
5394 the bounds since they refer to the type. These are always static. */
5395 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5396 || (kind == E_Floating_Point_Type))
5398 tree gnu_scalar_type = gnu_type;
5399 tree gnu_low_bound, gnu_high_bound;
5401 /* If this is a padded type, we need to use the underlying type. */
5402 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5403 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5405 /* If this is a floating point type and we haven't set a floating
5406 point type yet, use this in the evaluation of the bounds. */
5407 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5408 longest_float_type_node = gnu_scalar_type;
5410 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5411 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5413 if (kind == E_Enumeration_Type)
5415 /* Enumeration types have specific RM bounds. */
5416 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5417 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5421 /* Floating-point types don't have specific RM bounds. */
5422 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5423 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5427 /* If we deferred processing of incomplete types, re-enable it. If there
5428 were no other disables and we have deferred types to process, do so. */
5430 && --defer_incomplete_level == 0
5431 && defer_incomplete_list)
5433 struct incomplete *p, *next;
5435 /* We are back to level 0 for the deferring of incomplete types.
5436 But processing these incomplete types below may itself require
5437 deferring, so preserve what we have and restart from scratch. */
5438 p = defer_incomplete_list;
5439 defer_incomplete_list = NULL;
5446 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5447 gnat_to_gnu_type (p->full_type));
5452 /* If we are not defining this type, see if it's on one of the lists of
5453 incomplete types. If so, handle the list entry now. */
5454 if (is_type && !definition)
5456 struct incomplete *p;
5458 for (p = defer_incomplete_list; p; p = p->next)
5459 if (p->old_type && p->full_type == gnat_entity)
5461 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5462 TREE_TYPE (gnu_decl));
5463 p->old_type = NULL_TREE;
5466 for (p = defer_limited_with; p; p = p->next)
5467 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5469 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5470 TREE_TYPE (gnu_decl));
5471 p->old_type = NULL_TREE;
5478 /* If this is a packed array type whose original array type is itself
5479 an Itype without freeze node, make sure the latter is processed. */
5480 if (Is_Packed_Array_Impl_Type (gnat_entity)
5481 && Is_Itype (Original_Array_Type (gnat_entity))
5482 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5483 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5484 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5489 /* Similar, but if the returned value is a COMPONENT_REF, return the
5493 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5495 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5497 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5498 gnu_field = TREE_OPERAND (gnu_field, 1);
5503 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5504 the GCC type corresponding to that entity. */
5507 gnat_to_gnu_type (Entity_Id gnat_entity)
5511 /* The back end never attempts to annotate generic types. */
5512 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5513 return void_type_node;
5515 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5516 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5518 return TREE_TYPE (gnu_decl);
5521 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5522 the unpadded version of the GCC type corresponding to that entity. */
5525 get_unpadded_type (Entity_Id gnat_entity)
5527 tree type = gnat_to_gnu_type (gnat_entity);
5529 if (TYPE_IS_PADDING_P (type))
5530 type = TREE_TYPE (TYPE_FIELDS (type));
5535 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5536 type has been changed to that of the parameterless procedure, except if an
5537 alias is already present, in which case it is returned instead. */
5540 get_minimal_subprog_decl (Entity_Id gnat_entity)
5542 tree gnu_entity_name, gnu_ext_name;
5543 struct attrib *attr_list = NULL;
5545 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5546 of the handling applied here. */
5548 while (Present (Alias (gnat_entity)))
5550 gnat_entity = Alias (gnat_entity);
5551 if (present_gnu_tree (gnat_entity))
5552 return get_gnu_tree (gnat_entity);
5555 gnu_entity_name = get_entity_name (gnat_entity);
5556 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5558 if (Has_Stdcall_Convention (gnat_entity))
5559 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5560 get_identifier ("stdcall"), NULL_TREE,
5562 else if (Has_Thiscall_Convention (gnat_entity))
5563 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5564 get_identifier ("thiscall"), NULL_TREE,
5567 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5568 gnu_ext_name = NULL_TREE;
5571 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5572 is_disabled, false, true, true, false, true, false,
5573 attr_list, gnat_entity);
5576 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5577 a C++ imported method or equivalent.
5579 We use the predicate on 32-bit x86/Windows to find out whether we need to
5580 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5581 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5584 is_cplusplus_method (Entity_Id gnat_entity)
5586 /* Check that the subprogram has C++ convention. */
5587 if (Convention (gnat_entity) != Convention_CPP)
5590 /* A constructor is a method on the C++ side. We deal with it now because
5591 it is declared without the 'this' parameter in the sources and, although
5592 the front-end will create a version with the 'this' parameter for code
5593 generation purposes, we want to return true for both versions. */
5594 if (Is_Constructor (gnat_entity))
5597 /* And that the type of the first parameter (indirectly) has it too. */
5598 Entity_Id gnat_first = First_Formal (gnat_entity);
5599 if (No (gnat_first))
5602 Entity_Id gnat_type = Etype (gnat_first);
5603 if (Is_Access_Type (gnat_type))
5604 gnat_type = Directly_Designated_Type (gnat_type);
5605 if (Convention (gnat_type) != Convention_CPP)
5608 /* This is the main case: C++ method imported as a primitive operation.
5609 Note that a C++ class with no virtual functions can be imported as a
5610 limited record type so the operation is not necessarily dispatching. */
5611 if (Is_Primitive (gnat_entity))
5614 /* A thunk needs to be handled like its associated primitive operation. */
5615 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5618 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5619 if (Is_Dispatch_Table_Entity (gnat_entity))
5625 /* Finalize the processing of From_Limited_With incomplete types. */
5628 finalize_from_limited_with (void)
5630 struct incomplete *p, *next;
5632 p = defer_limited_with;
5633 defer_limited_with = NULL;
5640 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5641 gnat_to_gnu_type (p->full_type));
5646 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5647 kind of type (such E_Task_Type) that has a different type which Gigi
5648 uses for its representation. If the type does not have a special type
5649 for its representation, return GNAT_ENTITY. If a type is supposed to
5650 exist, but does not, abort unless annotating types, in which case
5651 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5654 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5656 Entity_Id gnat_equiv = gnat_entity;
5658 if (No (gnat_entity))
5661 switch (Ekind (gnat_entity))
5663 case E_Class_Wide_Subtype:
5664 if (Present (Equivalent_Type (gnat_entity)))
5665 gnat_equiv = Equivalent_Type (gnat_entity);
5668 case E_Access_Protected_Subprogram_Type:
5669 case E_Anonymous_Access_Protected_Subprogram_Type:
5670 gnat_equiv = Equivalent_Type (gnat_entity);
5673 case E_Class_Wide_Type:
5674 gnat_equiv = Root_Type (gnat_entity);
5678 case E_Task_Subtype:
5679 case E_Protected_Type:
5680 case E_Protected_Subtype:
5681 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5688 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5693 /* Return a GCC tree for a type corresponding to the component type of the
5694 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5695 is for an array being defined. DEBUG_INFO_P is true if we need to write
5696 debug information for other types that we may create in the process. */
5699 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5702 const Entity_Id gnat_type = Component_Type (gnat_array);
5703 tree gnu_type = gnat_to_gnu_type (gnat_type);
5706 /* Try to get a smaller form of the component if needed. */
5707 if ((Is_Packed (gnat_array)
5708 || Has_Component_Size_Clause (gnat_array))
5709 && !Is_Bit_Packed_Array (gnat_array)
5710 && !Has_Aliased_Components (gnat_array)
5711 && !Strict_Alignment (gnat_type)
5712 && RECORD_OR_UNION_TYPE_P (gnu_type)
5713 && !TYPE_FAT_POINTER_P (gnu_type)
5714 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5715 gnu_type = make_packable_type (gnu_type, false);
5717 if (Has_Atomic_Components (gnat_array))
5718 check_ok_for_atomic_type (gnu_type, gnat_array, true);
5720 /* Get and validate any specified Component_Size. */
5722 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5723 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5724 true, Has_Component_Size_Clause (gnat_array));
5726 /* If the array has aliased components and the component size can be zero,
5727 force at least unit size to ensure that the components have distinct
5730 && Has_Aliased_Components (gnat_array)
5731 && (integer_zerop (TYPE_SIZE (gnu_type))
5732 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5733 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5735 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5737 /* If the component type is a RECORD_TYPE that has a self-referential size,
5738 then use the maximum size for the component size. */
5740 && TREE_CODE (gnu_type) == RECORD_TYPE
5741 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5742 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5744 /* Honor the component size. This is not needed for bit-packed arrays. */
5745 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5747 tree orig_type = gnu_type;
5748 unsigned int max_align;
5750 /* If an alignment is specified, use it as a cap on the component type
5751 so that it can be honored for the whole type. But ignore it for the
5752 original type of packed array types. */
5753 if (No (Packed_Array_Impl_Type (gnat_array))
5754 && Known_Alignment (gnat_array))
5755 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5759 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5760 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5761 gnu_type = orig_type;
5763 orig_type = gnu_type;
5765 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5766 true, false, definition, true);
5768 /* If a padding record was made, declare it now since it will never be
5769 declared otherwise. This is necessary to ensure that its subtrees
5770 are properly marked. */
5771 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5772 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5776 /* If the component type is a padded type made for a non-bit-packed array
5777 of scalars with reverse storage order, we need to propagate the reverse
5778 storage order to the padding type since it is the innermost enclosing
5779 aggregate type around the scalar. */
5780 if (TYPE_IS_PADDING_P (gnu_type)
5781 && Reverse_Storage_Order (gnat_array)
5782 && !Is_Bit_Packed_Array (gnat_array)
5783 && Is_Scalar_Type (gnat_type))
5784 gnu_type = set_reverse_storage_order_on_pad_type (gnu_type);
5786 if (Has_Volatile_Components (gnat_array))
5789 = TYPE_QUAL_VOLATILE
5790 | (Has_Atomic_Components (gnat_array) ? TYPE_QUAL_ATOMIC : 0);
5791 gnu_type = change_qualified_type (gnu_type, quals);
5797 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5798 using MECH as its passing mechanism, to be placed in the parameter
5799 list built for GNAT_SUBPROG. Assume a foreign convention for the
5800 latter if FOREIGN is true. Also set CICO to true if the parameter
5801 must use the copy-in copy-out implementation mechanism.
5803 The returned tree is a PARM_DECL, except for those cases where no
5804 parameter needs to be actually passed to the subprogram; the type
5805 of this "shadow" parameter is then returned instead. */
5808 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5809 Entity_Id gnat_subprog, bool foreign, bool *cico)
5811 tree gnu_param_name = get_entity_name (gnat_param);
5812 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5813 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5814 /* The parameter can be indirectly modified if its address is taken. */
5815 bool ro_param = in_param && !Address_Taken (gnat_param);
5816 bool by_return = false, by_component_ptr = false;
5817 bool by_ref = false;
5818 bool restricted_aliasing_p = false;
5821 /* Copy-return is used only for the first parameter of a valued procedure.
5822 It's a copy mechanism for which a parameter is never allocated. */
5823 if (mech == By_Copy_Return)
5825 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5830 /* If this is either a foreign function or if the underlying type won't
5831 be passed by reference and is as aligned as the original type, strip
5832 off possible padding type. */
5833 if (TYPE_IS_PADDING_P (gnu_param_type))
5835 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5838 || (!must_pass_by_ref (unpadded_type)
5839 && mech != By_Reference
5840 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))
5841 && TYPE_ALIGN (unpadded_type) >= TYPE_ALIGN (gnu_param_type)))
5842 gnu_param_type = unpadded_type;
5845 /* If this is a read-only parameter, make a variant of the type that is
5846 read-only. ??? However, if this is an unconstrained array, that type
5847 can be very complex, so skip it for now. Likewise for any other
5848 self-referential type. */
5850 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5851 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5852 gnu_param_type = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5854 /* For foreign conventions, pass arrays as pointers to the element type.
5855 First check for unconstrained array and get the underlying array. */
5856 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5858 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5860 /* For GCC builtins, pass Address integer types as (void *) */
5861 if (Convention (gnat_subprog) == Convention_Intrinsic
5862 && Present (Interface_Name (gnat_subprog))
5863 && Is_Descendent_Of_Address (Etype (gnat_param)))
5864 gnu_param_type = ptr_type_node;
5866 /* Arrays are passed as pointers to element type for foreign conventions. */
5867 if (foreign && mech != By_Copy && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5869 /* Strip off any multi-dimensional entries, then strip
5870 off the last array to get the component type. */
5871 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5872 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5873 gnu_param_type = TREE_TYPE (gnu_param_type);
5875 by_component_ptr = true;
5876 gnu_param_type = TREE_TYPE (gnu_param_type);
5880 = change_qualified_type (gnu_param_type, TYPE_QUAL_CONST);
5882 gnu_param_type = build_pointer_type (gnu_param_type);
5885 /* Fat pointers are passed as thin pointers for foreign conventions. */
5886 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5888 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5890 /* If we must pass or were requested to pass by reference, do so.
5891 If we were requested to pass by copy, do so.
5892 Otherwise, for foreign conventions, pass In Out or Out parameters
5893 or aggregates by reference. For COBOL and Fortran, pass all
5894 integer and FP types that way too. For Convention Ada, use
5895 the standard Ada default. */
5896 else if (must_pass_by_ref (gnu_param_type)
5897 || mech == By_Reference
5900 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5902 && (Convention (gnat_subprog) == Convention_Fortran
5903 || Convention (gnat_subprog) == Convention_COBOL)
5904 && (INTEGRAL_TYPE_P (gnu_param_type)
5905 || FLOAT_TYPE_P (gnu_param_type)))
5907 && default_pass_by_ref (gnu_param_type)))))
5909 gnu_param_type = build_reference_type (gnu_param_type);
5910 /* We take advantage of 6.2(12) by considering that references built for
5911 parameters whose type isn't by-ref and for which the mechanism hasn't
5912 been forced to by-ref allow only a restricted form of aliasing. */
5913 restricted_aliasing_p
5914 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5918 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5922 if (mech == By_Copy && (by_ref || by_component_ptr))
5923 post_error ("?cannot pass & by copy", gnat_param);
5925 /* If this is an Out parameter that isn't passed by reference and isn't
5926 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5927 it will be a VAR_DECL created when we process the procedure, so just
5928 return its type. For the special parameter of a valued procedure,
5931 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5932 Out parameters with discriminants or implicit initial values to be
5933 handled like In Out parameters. These type are normally built as
5934 aggregates, hence passed by reference, except for some packed arrays
5935 which end up encoded in special integer types. Note that scalars can
5936 be given implicit initial values using the Default_Value aspect.
5938 The exception we need to make is then for packed arrays of records
5939 with discriminants or implicit initial values. We have no light/easy
5940 way to check for the latter case, so we merely check for packed arrays
5941 of records. This may lead to useless copy-in operations, but in very
5942 rare cases only, as these would be exceptions in a set of already
5943 exceptional situations. */
5944 if (Ekind (gnat_param) == E_Out_Parameter
5947 || (!POINTER_TYPE_P (gnu_param_type)
5948 && !AGGREGATE_TYPE_P (gnu_param_type)
5949 && !Has_Default_Aspect (Etype (gnat_param))))
5950 && !(Is_Array_Type (Etype (gnat_param))
5951 && Is_Packed (Etype (gnat_param))
5952 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5953 return gnu_param_type;
5955 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5956 ro_param || by_ref || by_component_ptr);
5957 DECL_BY_REF_P (gnu_param) = by_ref;
5958 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5959 DECL_POINTS_TO_READONLY_P (gnu_param)
5960 = (ro_param && (by_ref || by_component_ptr));
5961 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5962 DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
5964 /* If no Mechanism was specified, indicate what we're using, then
5965 back-annotate it. */
5966 if (mech == Default)
5967 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5969 Set_Mechanism (gnat_param, mech);
5973 /* Return true if GNAT_ENTITY is an incomplete entity coming from a limited
5974 with of the main unit and whose full view has not been elaborated yet. */
5977 is_from_limited_with_of_main (Entity_Id gnat_entity)
5979 /* Class-wide types are always transformed into their root type. */
5980 if (Ekind (gnat_entity) == E_Class_Wide_Type)
5981 gnat_entity = Root_Type (gnat_entity);
5983 if (IN (Ekind (gnat_entity), Incomplete_Kind)
5984 && From_Limited_With (gnat_entity))
5986 Entity_Id gnat_full_view = Non_Limited_View (gnat_entity);
5988 if (present_gnu_tree (gnat_full_view))
5991 return In_Extended_Main_Code_Unit (gnat_full_view);
5997 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
5998 qualifiers on TYPE. */
6001 change_qualified_type (tree type, int type_quals)
6003 return build_qualified_type (type, TYPE_QUALS (type) | type_quals);
6006 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6009 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
6011 while (Present (Corresponding_Discriminant (discr1)))
6012 discr1 = Corresponding_Discriminant (discr1);
6014 while (Present (Corresponding_Discriminant (discr2)))
6015 discr2 = Corresponding_Discriminant (discr2);
6018 Original_Record_Component (discr1) == Original_Record_Component (discr2);
6021 /* Return true if the array type GNU_TYPE, which represents a dimension of
6022 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6025 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
6027 /* If the array type is not the innermost dimension of the GNAT type,
6028 then it has a non-aliased component. */
6029 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6030 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
6033 /* If the array type has an aliased component in the front-end sense,
6034 then it also has an aliased component in the back-end sense. */
6035 if (Has_Aliased_Components (gnat_type))
6038 /* If this is a derived type, then it has a non-aliased component if
6039 and only if its parent type also has one. */
6040 if (Is_Derived_Type (gnat_type))
6042 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
6044 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
6046 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
6047 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
6048 gnu_parent_type = TREE_TYPE (gnu_parent_type);
6049 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
6052 /* Otherwise, rely exclusively on properties of the element type. */
6053 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
6056 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6059 compile_time_known_address_p (Node_Id gnat_address)
6061 /* Catch System'To_Address. */
6062 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
6063 gnat_address = Expression (gnat_address);
6065 return Compile_Time_Known_Value (gnat_address);
6068 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6069 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6072 cannot_be_superflat (Node_Id gnat_range)
6074 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
6075 Node_Id scalar_range;
6076 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
6078 /* If the low bound is not constant, try to find an upper bound. */
6079 while (Nkind (gnat_lb) != N_Integer_Literal
6080 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
6081 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
6082 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
6083 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6084 || Nkind (scalar_range) == N_Range))
6085 gnat_lb = High_Bound (scalar_range);
6087 /* If the high bound is not constant, try to find a lower bound. */
6088 while (Nkind (gnat_hb) != N_Integer_Literal
6089 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
6090 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
6091 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
6092 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
6093 || Nkind (scalar_range) == N_Range))
6094 gnat_hb = Low_Bound (scalar_range);
6096 /* If we have failed to find constant bounds, punt. */
6097 if (Nkind (gnat_lb) != N_Integer_Literal
6098 || Nkind (gnat_hb) != N_Integer_Literal)
6101 /* We need at least a signed 64-bit type to catch most cases. */
6102 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
6103 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
6104 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
6107 /* If the low bound is the smallest integer, nothing can be smaller. */
6108 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
6109 if (TREE_OVERFLOW (gnu_lb_minus_one))
6112 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6115 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6118 constructor_address_p (tree gnu_expr)
6120 while (TREE_CODE (gnu_expr) == NOP_EXPR
6121 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6122 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6123 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6125 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6126 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6129 /* Return true if the size in units represented by GNU_SIZE can be handled by
6130 an allocation. If STATIC_P is true, consider only what can be done with a
6131 static allocation. */
6134 allocatable_size_p (tree gnu_size, bool static_p)
6136 /* We can allocate a fixed size if it is a valid for the middle-end. */
6137 if (TREE_CODE (gnu_size) == INTEGER_CST)
6138 return valid_constant_size_p (gnu_size);
6140 /* We can allocate a variable size if this isn't a static allocation. */
6145 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6146 initial value of an object of GNU_TYPE. */
6149 initial_value_needs_conversion (tree gnu_type, tree gnu_expr)
6151 /* Do not convert if the object's type is unconstrained because this would
6152 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6153 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
6154 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
6157 /* Do not convert if the object's type is a padding record whose field is of
6158 self-referential size because we want to copy only the actual data. */
6159 if (type_is_padding_self_referential (gnu_type))
6162 /* Do not convert a call to a function that returns with variable size since
6163 we want to use the return slot optimization in this case. */
6164 if (TREE_CODE (gnu_expr) == CALL_EXPR
6165 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr)))
6168 /* Do not convert to a record type with a variant part from a record type
6169 without one, to keep the object simpler. */
6170 if (TREE_CODE (gnu_type) == RECORD_TYPE
6171 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
6172 && get_variant_part (gnu_type)
6173 && !get_variant_part (TREE_TYPE (gnu_expr)))
6176 /* In all the other cases, convert the expression to the object's type. */
6180 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6181 be elaborated at the point of its definition, but do nothing else. */
6184 elaborate_entity (Entity_Id gnat_entity)
6186 switch (Ekind (gnat_entity))
6188 case E_Signed_Integer_Subtype:
6189 case E_Modular_Integer_Subtype:
6190 case E_Enumeration_Subtype:
6191 case E_Ordinary_Fixed_Point_Subtype:
6192 case E_Decimal_Fixed_Point_Subtype:
6193 case E_Floating_Point_Subtype:
6195 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6196 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6198 /* ??? Tests to avoid Constraint_Error in static expressions
6199 are needed until after the front stops generating bogus
6200 conversions on bounds of real types. */
6201 if (!Raises_Constraint_Error (gnat_lb))
6202 elaborate_expression (gnat_lb, gnat_entity, "L", true, false,
6203 Needs_Debug_Info (gnat_entity));
6204 if (!Raises_Constraint_Error (gnat_hb))
6205 elaborate_expression (gnat_hb, gnat_entity, "U", true, false,
6206 Needs_Debug_Info (gnat_entity));
6210 case E_Record_Subtype:
6211 case E_Private_Subtype:
6212 case E_Limited_Private_Subtype:
6213 case E_Record_Subtype_With_Private:
6214 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6216 Node_Id gnat_discriminant_expr;
6217 Entity_Id gnat_field;
6220 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6221 gnat_discriminant_expr
6222 = First_Elmt (Discriminant_Constraint (gnat_entity));
6223 Present (gnat_field);
6224 gnat_field = Next_Discriminant (gnat_field),
6225 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6226 /* Ignore access discriminants. */
6227 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6228 elaborate_expression (Node (gnat_discriminant_expr),
6229 gnat_entity, get_entity_char (gnat_field),
6230 true, false, false);
6237 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6238 NAME, ARGS and ERROR_POINT. */
6241 prepend_one_attribute (struct attrib **attr_list,
6242 enum attrib_type attrib_type,
6245 Node_Id attr_error_point)
6247 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6249 attr->type = attrib_type;
6250 attr->name = attr_name;
6251 attr->args = attr_args;
6252 attr->error_point = attr_error_point;
6254 attr->next = *attr_list;
6258 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6261 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6263 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6264 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6265 enum attrib_type etype;
6267 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6268 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6270 case Pragma_Machine_Attribute:
6271 etype = ATTR_MACHINE_ATTRIBUTE;
6274 case Pragma_Linker_Alias:
6275 etype = ATTR_LINK_ALIAS;
6278 case Pragma_Linker_Section:
6279 etype = ATTR_LINK_SECTION;
6282 case Pragma_Linker_Constructor:
6283 etype = ATTR_LINK_CONSTRUCTOR;
6286 case Pragma_Linker_Destructor:
6287 etype = ATTR_LINK_DESTRUCTOR;
6290 case Pragma_Weak_External:
6291 etype = ATTR_WEAK_EXTERNAL;
6294 case Pragma_Thread_Local_Storage:
6295 etype = ATTR_THREAD_LOCAL_STORAGE;
6302 /* See what arguments we have and turn them into GCC trees for attribute
6303 handlers. These expect identifier for strings. We handle at most two
6304 arguments and static expressions only. */
6305 if (Present (gnat_arg) && Present (First (gnat_arg)))
6307 Node_Id gnat_arg0 = Next (First (gnat_arg));
6308 Node_Id gnat_arg1 = Empty;
6310 if (Present (gnat_arg0)
6311 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6313 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6315 if (TREE_CODE (gnu_arg0) == STRING_CST)
6317 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6318 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6322 gnat_arg1 = Next (gnat_arg0);
6325 if (Present (gnat_arg1)
6326 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6328 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6330 if (TREE_CODE (gnu_arg1) == STRING_CST)
6331 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6335 /* Prepend to the list. Make a list of the argument we might have, as GCC
6337 prepend_one_attribute (attr_list, etype, gnu_arg0,
6339 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6340 Present (Next (First (gnat_arg)))
6341 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6344 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6347 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6351 /* Attributes are stored as Representation Item pragmas. */
6352 for (gnat_temp = First_Rep_Item (gnat_entity);
6353 Present (gnat_temp);
6354 gnat_temp = Next_Rep_Item (gnat_temp))
6355 if (Nkind (gnat_temp) == N_Pragma)
6356 prepend_one_attribute_pragma (attr_list, gnat_temp);
6359 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6360 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6361 return the GCC tree to use for that expression. S is the suffix to use
6362 if a variable needs to be created and DEFINITION is true if this is done
6363 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6364 otherwise, we are just elaborating the expression for side-effects. If
6365 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6366 isn't needed for code generation. */
6369 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, const char *s,
6370 bool definition, bool need_value, bool need_debug)
6374 /* If we already elaborated this expression (e.g. it was involved
6375 in the definition of a private type), use the old value. */
6376 if (present_gnu_tree (gnat_expr))
6377 return get_gnu_tree (gnat_expr);
6379 /* If we don't need a value and this is static or a discriminant,
6380 we don't need to do anything. */
6382 && (Is_OK_Static_Expression (gnat_expr)
6383 || (Nkind (gnat_expr) == N_Identifier
6384 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6387 /* If it's a static expression, we don't need a variable for debugging. */
6388 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6391 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6392 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity, s,
6393 definition, need_debug);
6395 /* Save the expression in case we try to elaborate this entity again. Since
6396 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6397 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6398 save_gnu_tree (gnat_expr, gnu_expr, true);
6400 return need_value ? gnu_expr : error_mark_node;
6403 /* Similar, but take a GNU expression and always return a result. */
6406 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6407 bool definition, bool need_debug)
6409 const bool expr_public_p = Is_Public (gnat_entity);
6410 const bool expr_global_p = expr_public_p || global_bindings_p ();
6411 bool expr_variable_p, use_variable;
6413 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6414 that an expression cannot contain both a discriminant and a variable. */
6415 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6418 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6419 a variable that is initialized to contain the expression when the package
6420 containing the definition is elaborated. If this entity is defined at top
6421 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6422 if this is necessary. */
6423 if (TREE_CONSTANT (gnu_expr))
6424 expr_variable_p = false;
6427 /* Skip any conversions and simple constant arithmetics to see if the
6428 expression is based on a read-only variable. */
6429 tree inner = remove_conversions (gnu_expr, true);
6431 inner = skip_simple_constant_arithmetic (inner);
6433 if (handled_component_p (inner))
6434 inner = get_inner_constant_reference (inner);
6438 && TREE_CODE (inner) == VAR_DECL
6439 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6442 /* We only need to use the variable if we are in a global context since GCC
6443 can do the right thing in the local case. However, when not optimizing,
6444 use it for bounds of loop iteration scheme to avoid code duplication. */
6445 use_variable = expr_variable_p
6449 && Is_Itype (gnat_entity)
6450 && Nkind (Associated_Node_For_Itype (gnat_entity))
6451 == N_Loop_Parameter_Specification));
6453 /* Now create it, possibly only for debugging purposes. */
6454 if (use_variable || need_debug)
6456 /* The following variable creation can happen when processing the body
6457 of subprograms that are defined out of the extended main unit and
6458 inlined. In this case, we are not at the global scope, and thus the
6459 new variable must not be tagged "external", as we used to do here as
6460 soon as DEFINITION was false. */
6462 = create_var_decl (create_concat_name (gnat_entity, s), NULL_TREE,
6463 TREE_TYPE (gnu_expr), gnu_expr, true,
6464 expr_public_p, !definition && expr_global_p,
6465 expr_global_p, false, true, need_debug,
6468 /* Using this variable at debug time (if need_debug is true) requires a
6469 proper location. The back-end will compute a location for this
6470 variable only if the variable is used by the generated code.
6471 Returning the variable ensures the caller will use it in generated
6472 code. Note that there is no need for a location if the debug info
6473 contains an integer constant.
6474 TODO: when the encoding-based debug scheme is dropped, move this
6475 condition to the top-level IF block: we will not need to create a
6476 variable anymore in such cases, then. */
6477 if (use_variable || (need_debug && !TREE_CONSTANT (gnu_expr)))
6481 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6484 /* Similar, but take an alignment factor and make it explicit in the tree. */
6487 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, const char *s,
6488 bool definition, bool need_debug, unsigned int align)
6490 tree unit_align = size_int (align / BITS_PER_UNIT);
6492 size_binop (MULT_EXPR,
6493 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6496 gnat_entity, s, definition,
6501 /* Structure to hold internal data for elaborate_reference. */
6510 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6513 elaborate_reference_1 (tree ref, void *data)
6515 struct er_data *er = (struct er_data *)data;
6518 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6519 if (TREE_CONSTANT (ref))
6522 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6523 pointer. This may be more efficient, but will also allow us to more
6524 easily find the match for the PLACEHOLDER_EXPR. */
6525 if (TREE_CODE (ref) == COMPONENT_REF
6526 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
6527 return build3 (COMPONENT_REF, TREE_TYPE (ref),
6528 elaborate_reference_1 (TREE_OPERAND (ref, 0), data),
6529 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
6531 sprintf (suffix, "EXP%d", ++er->n);
6533 elaborate_expression_1 (ref, er->entity, suffix, er->definition, false);
6536 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6537 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6538 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6541 elaborate_reference (tree ref, Entity_Id gnat_entity, bool definition,
6544 struct er_data er = { gnat_entity, definition, 0 };
6545 return gnat_rewrite_reference (ref, elaborate_reference_1, &er, init);
6548 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6549 the value passed against the list of choices. */
6552 choices_to_gnu (tree operand, Node_Id choices)
6556 tree result = boolean_false_node;
6557 tree this_test, low = 0, high = 0, single = 0;
6559 for (choice = First (choices); Present (choice); choice = Next (choice))
6561 switch (Nkind (choice))
6564 low = gnat_to_gnu (Low_Bound (choice));
6565 high = gnat_to_gnu (High_Bound (choice));
6568 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6569 build_binary_op (GE_EXPR, boolean_type_node,
6571 build_binary_op (LE_EXPR, boolean_type_node,
6576 case N_Subtype_Indication:
6577 gnat_temp = Range_Expression (Constraint (choice));
6578 low = gnat_to_gnu (Low_Bound (gnat_temp));
6579 high = gnat_to_gnu (High_Bound (gnat_temp));
6582 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6583 build_binary_op (GE_EXPR, boolean_type_node,
6585 build_binary_op (LE_EXPR, boolean_type_node,
6590 case N_Expanded_Name:
6591 /* This represents either a subtype range, an enumeration
6592 literal, or a constant Ekind says which. If an enumeration
6593 literal or constant, fall through to the next case. */
6594 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6595 && Ekind (Entity (choice)) != E_Constant)
6597 tree type = gnat_to_gnu_type (Entity (choice));
6599 low = TYPE_MIN_VALUE (type);
6600 high = TYPE_MAX_VALUE (type);
6603 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6604 build_binary_op (GE_EXPR, boolean_type_node,
6606 build_binary_op (LE_EXPR, boolean_type_node,
6611 /* ... fall through ... */
6613 case N_Character_Literal:
6614 case N_Integer_Literal:
6615 single = gnat_to_gnu (choice);
6616 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6620 case N_Others_Choice:
6621 this_test = boolean_true_node;
6628 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6635 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6636 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6639 adjust_packed (tree field_type, tree record_type, int packed)
6641 /* If the field contains an item of variable size, we cannot pack it
6642 because we cannot create temporaries of non-fixed size in case
6643 we need to take the address of the field. See addressable_p and
6644 the notes on the addressability issues for further details. */
6645 if (type_has_variable_size (field_type))
6648 /* In the other cases, we can honor the packing. */
6652 /* If the alignment of the record is specified and the field type
6653 is over-aligned, request Storage_Unit alignment for the field. */
6654 if (TYPE_ALIGN (record_type)
6655 && TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6658 /* Likewise if the maximum alignment of the record is specified. */
6659 if (TYPE_MAX_ALIGN (record_type)
6660 && TYPE_ALIGN (field_type) > TYPE_MAX_ALIGN (record_type))
6666 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6667 placed in GNU_RECORD_TYPE.
6669 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6670 record has Component_Alignment of Storage_Unit.
6672 DEFINITION is true if this field is for a record being defined.
6674 DEBUG_INFO_P is true if we need to write debug information for types
6675 that we may create in the process. */
6678 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6679 bool definition, bool debug_info_p)
6681 const Entity_Id gnat_field_type = Etype (gnat_field);
6682 const bool is_aliased
6683 = Is_Aliased (gnat_field);
6684 const bool is_atomic
6685 = (Is_Atomic_Or_VFA (gnat_field) || Is_Atomic_Or_VFA (gnat_field_type));
6686 const bool is_independent
6687 = (Is_Independent (gnat_field) || Is_Independent (gnat_field_type));
6688 const bool is_volatile
6689 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6690 const bool needs_strict_alignment
6694 || Strict_Alignment (gnat_field_type));
6695 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6696 tree gnu_field_id = get_entity_name (gnat_field);
6697 tree gnu_field, gnu_size, gnu_pos;
6699 /* If this field requires strict alignment, we cannot pack it because
6700 it would very likely be under-aligned in the record. */
6701 if (needs_strict_alignment)
6704 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6706 /* If a size is specified, use it. Otherwise, if the record type is packed,
6707 use the official RM size. See "Handling of Type'Size Values" in Einfo
6708 for further details. */
6709 if (Known_Esize (gnat_field))
6710 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6711 gnat_field, FIELD_DECL, false, true);
6712 else if (packed == 1)
6713 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6714 gnat_field, FIELD_DECL, false, true);
6716 gnu_size = NULL_TREE;
6718 /* If we have a specified size that is smaller than that of the field's type,
6719 or a position is specified, and the field's type is a record that doesn't
6720 require strict alignment, see if we can get either an integral mode form
6721 of the type or a smaller form. If we can, show a size was specified for
6722 the field if there wasn't one already, so we know to make this a bitfield
6723 and avoid making things wider.
6725 Changing to an integral mode form is useful when the record is packed as
6726 we can then place the field at a non-byte-aligned position and so achieve
6727 tighter packing. This is in addition required if the field shares a byte
6728 with another field and the front-end lets the back-end handle the access
6729 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6731 Changing to a smaller form is required if the specified size is smaller
6732 than that of the field's type and the type contains sub-fields that are
6733 padded, in order to avoid generating accesses to these sub-fields that
6734 are wider than the field.
6736 We avoid the transformation if it is not required or potentially useful,
6737 as it might entail an increase of the field's alignment and have ripple
6738 effects on the outer record type. A typical case is a field known to be
6739 byte-aligned and not to share a byte with another field. */
6740 if (!needs_strict_alignment
6741 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6742 && !TYPE_FAT_POINTER_P (gnu_field_type)
6743 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6746 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6747 || (Present (Component_Clause (gnat_field))
6748 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6749 % BITS_PER_UNIT == 0
6750 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6752 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6753 if (gnu_packable_type != gnu_field_type)
6755 gnu_field_type = gnu_packable_type;
6757 gnu_size = rm_size (gnu_field_type);
6761 if (Is_Atomic_Or_VFA (gnat_field))
6762 check_ok_for_atomic_type (gnu_field_type, gnat_field, false);
6764 if (Present (Component_Clause (gnat_field)))
6766 Node_Id gnat_clause = Component_Clause (gnat_field);
6767 Entity_Id gnat_parent
6768 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6770 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6771 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6772 gnat_field, FIELD_DECL, false, true);
6774 /* Ensure the position does not overlap with the parent subtype, if there
6775 is one. This test is omitted if the parent of the tagged type has a
6776 full rep clause since, in this case, component clauses are allowed to
6777 overlay the space allocated for the parent type and the front-end has
6778 checked that there are no overlapping components. */
6779 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6781 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6783 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6784 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6786 ("offset of& must be beyond parent{, minimum allowed is ^}",
6787 Position (gnat_clause), gnat_field, TYPE_SIZE_UNIT (gnu_parent));
6790 /* If this field needs strict alignment, make sure that the record is
6791 sufficiently aligned and that the position and size are consistent
6792 with the type. But don't do it if we are just annotating types and
6793 the field's type is tagged, since tagged types aren't fully laid out
6794 in this mode. Also, note that atomic implies volatile so the inner
6795 test sequences ordering is significant here. */
6796 if (needs_strict_alignment
6797 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6799 const unsigned int type_align = TYPE_ALIGN (gnu_field_type);
6801 if (TYPE_ALIGN (gnu_record_type) < type_align)
6802 TYPE_ALIGN (gnu_record_type) = type_align;
6804 /* If the position is not a multiple of the alignment of the type,
6805 then error out and reset the position. */
6806 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
6807 bitsize_int (type_align))))
6812 s = "position of atomic field& must be multiple of ^ bits";
6813 else if (is_aliased)
6814 s = "position of aliased field& must be multiple of ^ bits";
6815 else if (is_independent)
6816 s = "position of independent field& must be multiple of ^ bits";
6817 else if (is_volatile)
6818 s = "position of volatile field& must be multiple of ^ bits";
6819 else if (Strict_Alignment (gnat_field_type))
6820 s = "position of & with aliased or tagged part must be"
6821 " multiple of ^ bits";
6825 post_error_ne_num (s, First_Bit (gnat_clause), gnat_field,
6827 gnu_pos = NULL_TREE;
6832 tree gnu_type_size = TYPE_SIZE (gnu_field_type);
6833 const int cmp = tree_int_cst_compare (gnu_size, gnu_type_size);
6835 /* If the size is lower than that of the type, or greater for
6836 atomic and aliased, then error out and reset the size. */
6837 if (cmp < 0 || (cmp > 0 && (is_atomic || is_aliased)))
6842 s = "size of atomic field& must be ^ bits";
6843 else if (is_aliased)
6844 s = "size of aliased field& must be ^ bits";
6845 else if (is_independent)
6846 s = "size of independent field& must be at least ^ bits";
6847 else if (is_volatile)
6848 s = "size of volatile field& must be at least ^ bits";
6849 else if (Strict_Alignment (gnat_field_type))
6850 s = "size of & with aliased or tagged part must be"
6855 post_error_ne_tree (s, Last_Bit (gnat_clause), gnat_field,
6857 gnu_size = NULL_TREE;
6860 /* Likewise if the size is not a multiple of a byte, */
6861 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
6862 bitsize_unit_node)))
6867 s = "size of independent field& must be multiple of"
6869 else if (is_volatile)
6870 s = "size of volatile field& must be multiple of"
6872 else if (Strict_Alignment (gnat_field_type))
6873 s = "size of & with aliased or tagged part must be"
6874 " multiple of Storage_Unit";
6878 post_error_ne (s, Last_Bit (gnat_clause), gnat_field);
6879 gnu_size = NULL_TREE;
6885 /* If the record has rep clauses and this is the tag field, make a rep
6886 clause for it as well. */
6887 else if (Has_Specified_Layout (Scope (gnat_field))
6888 && Chars (gnat_field) == Name_uTag)
6890 gnu_pos = bitsize_zero_node;
6891 gnu_size = TYPE_SIZE (gnu_field_type);
6896 gnu_pos = NULL_TREE;
6898 /* If we are packing the record and the field is BLKmode, round the
6899 size up to a byte boundary. */
6900 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6901 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6904 /* We need to make the size the maximum for the type if it is
6905 self-referential and an unconstrained type. In that case, we can't
6906 pack the field since we can't make a copy to align it. */
6907 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6909 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6910 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6912 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6916 /* If a size is specified, adjust the field's type to it. */
6919 tree orig_field_type;
6921 /* If the field's type is justified modular, we would need to remove
6922 the wrapper to (better) meet the layout requirements. However we
6923 can do so only if the field is not aliased to preserve the unique
6924 layout and if the prescribed size is not greater than that of the
6925 packed array to preserve the justification. */
6926 if (!needs_strict_alignment
6927 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6928 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6929 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6931 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6933 /* Similarly if the field's type is a misaligned integral type, but
6934 there is no restriction on the size as there is no justification. */
6935 if (!needs_strict_alignment
6936 && TYPE_IS_PADDING_P (gnu_field_type)
6937 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6938 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6941 = make_type_from_size (gnu_field_type, gnu_size,
6942 Has_Biased_Representation (gnat_field));
6944 orig_field_type = gnu_field_type;
6945 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6946 false, false, definition, true);
6948 /* If a padding record was made, declare it now since it will never be
6949 declared otherwise. This is necessary to ensure that its subtrees
6950 are properly marked. */
6951 if (gnu_field_type != orig_field_type
6952 && !DECL_P (TYPE_NAME (gnu_field_type)))
6953 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6954 debug_info_p, gnat_field);
6957 /* Otherwise (or if there was an error), don't specify a position. */
6959 gnu_pos = NULL_TREE;
6961 /* If the field's type is a padded type made for a scalar field of a record
6962 type with reverse storage order, we need to propagate the reverse storage
6963 order to the padding type since it is the innermost enclosing aggregate
6964 type around the scalar. */
6965 if (TYPE_IS_PADDING_P (gnu_field_type)
6966 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type)
6967 && Is_Scalar_Type (gnat_field_type))
6968 gnu_field_type = set_reverse_storage_order_on_pad_type (gnu_field_type);
6970 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6971 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6973 /* Now create the decl for the field. */
6975 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6976 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6977 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6978 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6979 TREE_SIDE_EFFECTS (gnu_field) = TREE_THIS_VOLATILE (gnu_field) = is_volatile;
6981 if (Ekind (gnat_field) == E_Discriminant)
6983 DECL_INVARIANT_P (gnu_field)
6984 = No (Discriminant_Default_Value (gnat_field));
6985 DECL_DISCRIMINANT_NUMBER (gnu_field)
6986 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6992 /* Return true if at least one member of COMPONENT_LIST needs strict
6996 components_need_strict_alignment (Node_Id component_list)
6998 Node_Id component_decl;
7000 for (component_decl = First_Non_Pragma (Component_Items (component_list));
7001 Present (component_decl);
7002 component_decl = Next_Non_Pragma (component_decl))
7004 Entity_Id gnat_field = Defining_Entity (component_decl);
7006 if (Is_Aliased (gnat_field))
7009 if (Strict_Alignment (Etype (gnat_field)))
7016 /* Return true if TYPE is a type with variable size or a padding type with a
7017 field of variable size or a record that has a field with such a type. */
7020 type_has_variable_size (tree type)
7024 if (!TREE_CONSTANT (TYPE_SIZE (type)))
7027 if (TYPE_IS_PADDING_P (type)
7028 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
7031 if (!RECORD_OR_UNION_TYPE_P (type))
7034 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7035 if (type_has_variable_size (TREE_TYPE (field)))
7041 /* Return true if FIELD is an artificial field. */
7044 field_is_artificial (tree field)
7046 /* These fields are generated by the front-end proper. */
7047 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
7050 /* These fields are generated by gigi. */
7051 if (DECL_INTERNAL_P (field))
7057 /* Return true if FIELD is a non-artificial aliased field. */
7060 field_is_aliased (tree field)
7062 if (field_is_artificial (field))
7065 return DECL_ALIASED_P (field);
7068 /* Return true if FIELD is a non-artificial field with self-referential
7072 field_has_self_size (tree field)
7074 if (field_is_artificial (field))
7077 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7080 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
7083 /* Return true if FIELD is a non-artificial field with variable size. */
7086 field_has_variable_size (tree field)
7088 if (field_is_artificial (field))
7091 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
7094 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
7097 /* qsort comparer for the bit positions of two record components. */
7100 compare_field_bitpos (const PTR rt1, const PTR rt2)
7102 const_tree const field1 = * (const_tree const *) rt1;
7103 const_tree const field2 = * (const_tree const *) rt2;
7105 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
7107 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
7110 /* Structure holding information for a given variant. */
7111 typedef struct vinfo
7113 /* The record type of the variant. */
7116 /* The name of the variant. */
7119 /* The qualifier of the variant. */
7122 /* Whether the variant has a rep clause. */
7125 /* Whether the variant is packed. */
7130 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
7131 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
7132 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
7133 When called from gnat_to_gnu_entity during the processing of a record type
7134 definition, the GCC node for the parent, if any, will be the single field
7135 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7136 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7137 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7139 PACKED is 1 if this is for a packed record or -1 if this is for a record
7140 with Component_Alignment of Storage_Unit.
7142 DEFINITION is true if we are defining this record type.
7144 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7145 out the record. This means the alignment only serves to force fields to
7146 be bitfields, but not to require the record to be that aligned. This is
7149 ALL_REP is true if a rep clause is present for all the fields.
7151 UNCHECKED_UNION is true if we are building this type for a record with a
7152 Pragma Unchecked_Union.
7154 ARTIFICIAL is true if this is a type that was generated by the compiler.
7156 DEBUG_INFO is true if we need to write debug information about the type.
7158 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7159 mean that its contents may be unused as well, only the container itself.
7161 REORDER is true if we are permitted to reorder components of this type.
7163 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7164 the outer record type down to this variant level. It is nonzero only if
7165 all the fields down to this level have a rep clause and ALL_REP is false.
7167 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7168 with a rep clause is to be added; in this case, that is all that should
7169 be done with such fields and the return value will be false. */
7172 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7173 tree gnu_field_list, int packed, bool definition,
7174 bool cancel_alignment, bool all_rep,
7175 bool unchecked_union, bool artificial,
7176 bool debug_info, bool maybe_unused, bool reorder,
7177 tree first_free_pos, tree *p_gnu_rep_list)
7179 const bool needs_xv_encodings
7180 = debug_info && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL;
7181 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7182 bool variants_have_rep = all_rep;
7183 bool layout_with_rep = false;
7184 bool has_self_field = false;
7185 bool has_aliased_after_self_field = false;
7186 Node_Id component_decl, variant_part;
7187 tree gnu_field, gnu_next, gnu_last;
7188 tree gnu_variant_part = NULL_TREE;
7189 tree gnu_rep_list = NULL_TREE;
7190 tree gnu_var_list = NULL_TREE;
7191 tree gnu_self_list = NULL_TREE;
7192 tree gnu_zero_list = NULL_TREE;
7194 /* For each component referenced in a component declaration create a GCC
7195 field and add it to the list, skipping pragmas in the GNAT list. */
7196 gnu_last = tree_last (gnu_field_list);
7197 if (Present (Component_Items (gnat_component_list)))
7199 = First_Non_Pragma (Component_Items (gnat_component_list));
7200 Present (component_decl);
7201 component_decl = Next_Non_Pragma (component_decl))
7203 Entity_Id gnat_field = Defining_Entity (component_decl);
7204 Name_Id gnat_name = Chars (gnat_field);
7206 /* If present, the _Parent field must have been created as the single
7207 field of the record type. Put it before any other fields. */
7208 if (gnat_name == Name_uParent)
7210 gnu_field = TYPE_FIELDS (gnu_record_type);
7211 gnu_field_list = chainon (gnu_field_list, gnu_field);
7215 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7216 definition, debug_info);
7218 /* If this is the _Tag field, put it before any other fields. */
7219 if (gnat_name == Name_uTag)
7220 gnu_field_list = chainon (gnu_field_list, gnu_field);
7222 /* If this is the _Controller field, put it before the other
7223 fields except for the _Tag or _Parent field. */
7224 else if (gnat_name == Name_uController && gnu_last)
7226 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7227 DECL_CHAIN (gnu_last) = gnu_field;
7230 /* If this is a regular field, put it after the other fields. */
7233 DECL_CHAIN (gnu_field) = gnu_field_list;
7234 gnu_field_list = gnu_field;
7236 gnu_last = gnu_field;
7238 /* And record information for the final layout. */
7239 if (field_has_self_size (gnu_field))
7240 has_self_field = true;
7241 else if (has_self_field && field_is_aliased (gnu_field))
7242 has_aliased_after_self_field = true;
7246 save_gnu_tree (gnat_field, gnu_field, false);
7249 /* At the end of the component list there may be a variant part. */
7250 variant_part = Variant_Part (gnat_component_list);
7252 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7253 mutually exclusive and should go in the same memory. To do this we need
7254 to treat each variant as a record whose elements are created from the
7255 component list for the variant. So here we create the records from the
7256 lists for the variants and put them all into the QUAL_UNION_TYPE.
7257 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7258 use GNU_RECORD_TYPE if there are no fields so far. */
7259 if (Present (variant_part))
7261 Node_Id gnat_discr = Name (variant_part), variant;
7262 tree gnu_discr = gnat_to_gnu (gnat_discr);
7263 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7265 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7267 tree gnu_union_type, gnu_union_name;
7268 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7269 bool union_field_needs_strict_alignment = false;
7270 auto_vec <vinfo_t, 16> variant_types;
7271 vinfo_t *gnu_variant;
7272 unsigned int variants_align = 0;
7276 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7278 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7279 are all in the variant part, to match the layout of C unions. There
7280 is an associated check below. */
7281 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7282 gnu_union_type = gnu_record_type;
7286 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7288 TYPE_NAME (gnu_union_type) = gnu_union_name;
7289 TYPE_ALIGN (gnu_union_type) = 0;
7290 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7291 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type)
7292 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7295 /* If all the fields down to this level have a rep clause, find out
7296 whether all the fields at this level also have one. If so, then
7297 compute the new first free position to be passed downward. */
7298 this_first_free_pos = first_free_pos;
7299 if (this_first_free_pos)
7301 for (gnu_field = gnu_field_list;
7303 gnu_field = DECL_CHAIN (gnu_field))
7304 if (DECL_FIELD_OFFSET (gnu_field))
7306 tree pos = bit_position (gnu_field);
7307 if (!tree_int_cst_lt (pos, this_first_free_pos))
7309 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7313 this_first_free_pos = NULL_TREE;
7318 /* We build the variants in two passes. The bulk of the work is done in
7319 the first pass, that is to say translating the GNAT nodes, building
7320 the container types and computing the associated properties. However
7321 we cannot finish up the container types during this pass because we
7322 don't know where the variant part will be placed until the end. */
7323 for (variant = First_Non_Pragma (Variants (variant_part));
7325 variant = Next_Non_Pragma (variant))
7327 tree gnu_variant_type = make_node (RECORD_TYPE);
7328 tree gnu_inner_name, gnu_qual;
7333 Get_Variant_Encoding (variant);
7334 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7335 TYPE_NAME (gnu_variant_type)
7336 = concat_name (gnu_union_name,
7337 IDENTIFIER_POINTER (gnu_inner_name));
7339 /* Set the alignment of the inner type in case we need to make
7340 inner objects into bitfields, but then clear it out so the
7341 record actually gets only the alignment required. */
7342 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7343 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7344 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type)
7345 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7347 /* Similarly, if the outer record has a size specified and all
7348 the fields have a rep clause, we can propagate the size. */
7349 if (all_rep_and_size)
7351 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7352 TYPE_SIZE_UNIT (gnu_variant_type)
7353 = TYPE_SIZE_UNIT (gnu_record_type);
7356 /* Add the fields into the record type for the variant. Note that
7357 we aren't sure to really use it at this point, see below. */
7359 = components_to_record (gnu_variant_type, Component_List (variant),
7360 NULL_TREE, packed, definition,
7361 !all_rep_and_size, all_rep,
7363 true, needs_xv_encodings, true, reorder,
7364 this_first_free_pos,
7365 all_rep || this_first_free_pos
7366 ? NULL : &gnu_rep_list);
7368 /* Translate the qualifier and annotate the GNAT node. */
7369 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7370 Set_Present_Expr (variant, annotate_value (gnu_qual));
7372 /* Deal with packedness like in gnat_to_gnu_field. */
7373 if (components_need_strict_alignment (Component_List (variant)))
7376 union_field_needs_strict_alignment = true;
7380 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7382 /* Push this variant onto the stack for the second pass. */
7383 vinfo.type = gnu_variant_type;
7384 vinfo.name = gnu_inner_name;
7385 vinfo.qual = gnu_qual;
7386 vinfo.has_rep = has_rep;
7387 vinfo.packed = field_packed;
7388 variant_types.safe_push (vinfo);
7390 /* Compute the global properties that will determine the placement of
7391 the variant part. */
7392 variants_have_rep |= has_rep;
7393 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7394 variants_align = TYPE_ALIGN (gnu_variant_type);
7397 /* Round up the first free position to the alignment of the variant part
7398 for the variants without rep clause. This will guarantee a consistent
7399 layout independently of the placement of the variant part. */
7400 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7401 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7403 /* In the second pass, the container types are adjusted if necessary and
7404 finished up, then the corresponding fields of the variant part are
7405 built with their qualifier, unless this is an unchecked union. */
7406 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7408 tree gnu_variant_type = gnu_variant->type;
7409 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7411 /* If this is an Unchecked_Union whose fields are all in the variant
7412 part and we have a single field with no representation clause or
7413 placed at offset zero, use the field directly to match the layout
7415 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7417 && !DECL_CHAIN (gnu_field_list)
7418 && (!DECL_FIELD_OFFSET (gnu_field_list)
7419 || integer_zerop (bit_position (gnu_field_list))))
7421 gnu_field = gnu_field_list;
7422 DECL_CONTEXT (gnu_field) = gnu_record_type;
7426 /* Finalize the variant type now. We used to throw away empty
7427 record types but we no longer do that because we need them to
7428 generate complete debug info for the variant; otherwise, the
7429 union type definition will be lacking the fields associated
7430 with these empty variants. */
7431 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7433 /* The variant part will be at offset 0 so we need to ensure
7434 that the fields are laid out starting from the first free
7435 position at this level. */
7436 tree gnu_rep_type = make_node (RECORD_TYPE);
7438 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7439 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type);
7440 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7442 = create_rep_part (gnu_rep_type, gnu_variant_type,
7443 this_first_free_pos);
7444 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7445 gnu_field_list = gnu_rep_part;
7446 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7451 rest_of_record_type_compilation (gnu_variant_type);
7452 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7453 true, needs_xv_encodings, gnat_component_list);
7456 = create_field_decl (gnu_variant->name, gnu_variant_type,
7459 ? TYPE_SIZE (gnu_variant_type) : 0,
7460 variants_have_rep ? bitsize_zero_node : 0,
7461 gnu_variant->packed, 0);
7463 DECL_INTERNAL_P (gnu_field) = 1;
7465 if (!unchecked_union)
7466 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7469 DECL_CHAIN (gnu_field) = gnu_variant_list;
7470 gnu_variant_list = gnu_field;
7473 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7474 if (gnu_variant_list)
7476 int union_field_packed;
7478 if (all_rep_and_size)
7480 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7481 TYPE_SIZE_UNIT (gnu_union_type)
7482 = TYPE_SIZE_UNIT (gnu_record_type);
7485 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7486 all_rep_and_size ? 1 : 0, needs_xv_encodings);
7488 /* If GNU_UNION_TYPE is our record type, it means we must have an
7489 Unchecked_Union with no fields. Verify that and, if so, just
7491 if (gnu_union_type == gnu_record_type)
7493 gcc_assert (unchecked_union
7496 return variants_have_rep;
7499 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7500 needs_xv_encodings, gnat_component_list);
7502 /* Deal with packedness like in gnat_to_gnu_field. */
7503 if (union_field_needs_strict_alignment)
7504 union_field_packed = 0;
7507 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7510 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7512 ? TYPE_SIZE (gnu_union_type) : 0,
7513 variants_have_rep ? bitsize_zero_node : 0,
7514 union_field_packed, 0);
7516 DECL_INTERNAL_P (gnu_variant_part) = 1;
7520 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7521 permitted to reorder components, self-referential sizes or variable sizes.
7522 If they do, pull them out and put them onto the appropriate list. We have
7523 to do this in a separate pass since we want to handle the discriminants
7524 but can't play with them until we've used them in debugging data above.
7526 Similarly, pull out the fields with zero size and no rep clause, as they
7527 would otherwise modify the layout and thus very likely run afoul of the
7528 Ada semantics, which are different from those of C here.
7530 ??? If we reorder them, debugging information will be wrong but there is
7531 nothing that can be done about this at the moment. */
7532 gnu_last = NULL_TREE;
7534 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7537 DECL_CHAIN (gnu_last) = gnu_next; \
7539 gnu_field_list = gnu_next; \
7541 DECL_CHAIN (gnu_field) = (LIST); \
7542 (LIST) = gnu_field; \
7545 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7547 gnu_next = DECL_CHAIN (gnu_field);
7549 if (DECL_FIELD_OFFSET (gnu_field))
7551 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7555 if ((reorder || has_aliased_after_self_field)
7556 && field_has_self_size (gnu_field))
7558 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7562 if (reorder && field_has_variable_size (gnu_field))
7564 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7568 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7570 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7571 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7572 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7573 if (field_is_aliased (gnu_field))
7574 TYPE_ALIGN (gnu_record_type)
7575 = MAX (TYPE_ALIGN (gnu_record_type),
7576 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7577 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7581 gnu_last = gnu_field;
7584 #undef MOVE_FROM_FIELD_LIST_TO
7586 gnu_field_list = nreverse (gnu_field_list);
7588 /* If permitted, we reorder the fields as follows:
7590 1) all fixed length fields,
7591 2) all fields whose length doesn't depend on discriminants,
7592 3) all fields whose length depends on discriminants,
7593 4) the variant part,
7595 within the record and within each variant recursively. */
7598 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7600 /* Otherwise, if there is an aliased field placed after a field whose length
7601 depends on discriminants, we put all the fields of the latter sort, last.
7602 We need to do this in case an object of this record type is mutable. */
7603 else if (has_aliased_after_self_field)
7604 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7606 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7607 in our REP list to the previous level because this level needs them in
7608 order to do a correct layout, i.e. avoid having overlapping fields. */
7609 if (p_gnu_rep_list && gnu_rep_list)
7610 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7612 /* Deal with the annoying case of an extension of a record with variable size
7613 and partial rep clause, for which the _Parent field is forced at offset 0
7614 and has variable size, which we do not support below. Note that we cannot
7615 do it if the field has fixed size because we rely on the presence of the
7616 REP part built below to trigger the reordering of the fields in a derived
7617 record type when all the fields have a fixed position. */
7618 else if (gnu_rep_list
7619 && !DECL_CHAIN (gnu_rep_list)
7620 && TREE_CODE (DECL_SIZE (gnu_rep_list)) != INTEGER_CST
7621 && !variants_have_rep
7623 && integer_zerop (first_free_pos)
7624 && integer_zerop (bit_position (gnu_rep_list)))
7626 DECL_CHAIN (gnu_rep_list) = gnu_field_list;
7627 gnu_field_list = gnu_rep_list;
7628 gnu_rep_list = NULL_TREE;
7631 /* Otherwise, sort the fields by bit position and put them into their own
7632 record, before the others, if we also have fields without rep clause. */
7633 else if (gnu_rep_list)
7635 tree gnu_rep_type, gnu_rep_part;
7636 int i, len = list_length (gnu_rep_list);
7637 tree *gnu_arr = XALLOCAVEC (tree, len);
7639 /* If all the fields have a rep clause, we can do a flat layout. */
7640 layout_with_rep = !gnu_field_list
7641 && (!gnu_variant_part || variants_have_rep);
7643 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7645 for (gnu_field = gnu_rep_list, i = 0;
7647 gnu_field = DECL_CHAIN (gnu_field), i++)
7648 gnu_arr[i] = gnu_field;
7650 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7652 /* Put the fields in the list in order of increasing position, which
7653 means we start from the end. */
7654 gnu_rep_list = NULL_TREE;
7655 for (i = len - 1; i >= 0; i--)
7657 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7658 gnu_rep_list = gnu_arr[i];
7659 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7662 if (layout_with_rep)
7663 gnu_field_list = gnu_rep_list;
7666 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type)
7667 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type);
7668 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7670 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7671 without rep clause are laid out starting from this position.
7672 Therefore, we force it as a minimal size on the REP part. */
7674 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7676 /* Chain the REP part at the beginning of the field list. */
7677 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7678 gnu_field_list = gnu_rep_part;
7682 /* Chain the variant part at the end of the field list. */
7683 if (gnu_variant_part)
7684 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7686 if (cancel_alignment)
7687 TYPE_ALIGN (gnu_record_type) = 0;
7689 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7691 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7692 debug_info && !maybe_unused);
7694 /* Chain the fields with zero size at the beginning of the field list. */
7696 TYPE_FIELDS (gnu_record_type)
7697 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7699 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7702 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7703 placed into an Esize, Component_Bit_Offset, or Component_Size value
7704 in the GNAT tree. */
7707 annotate_value (tree gnu_size)
7710 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7711 struct tree_int_map in;
7714 /* See if we've already saved the value for this node. */
7715 if (EXPR_P (gnu_size))
7717 struct tree_int_map *e;
7719 in.base.from = gnu_size;
7720 e = annotate_value_cache->find (&in);
7723 return (Node_Ref_Or_Val) e->to;
7726 in.base.from = NULL_TREE;
7728 /* If we do not return inside this switch, TCODE will be set to the
7729 code to use for a Create_Node operand and LEN (set above) will be
7730 the number of recursive calls for us to make. */
7732 switch (TREE_CODE (gnu_size))
7735 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7738 /* The only case we handle here is a simple discriminant reference. */
7739 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7741 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7743 /* Climb up the chain of successive extensions, if any. */
7744 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7745 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7747 gnu_size = TREE_OPERAND (gnu_size, 0);
7749 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7751 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7756 CASE_CONVERT: case NON_LVALUE_EXPR:
7757 return annotate_value (TREE_OPERAND (gnu_size, 0));
7759 /* Now just list the operations we handle. */
7760 case COND_EXPR: tcode = Cond_Expr; break;
7761 case PLUS_EXPR: tcode = Plus_Expr; break;
7762 case MINUS_EXPR: tcode = Minus_Expr; break;
7763 case MULT_EXPR: tcode = Mult_Expr; break;
7764 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7765 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7766 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7767 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7768 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7769 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7770 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7771 case NEGATE_EXPR: tcode = Negate_Expr; break;
7772 case MIN_EXPR: tcode = Min_Expr; break;
7773 case MAX_EXPR: tcode = Max_Expr; break;
7774 case ABS_EXPR: tcode = Abs_Expr; break;
7775 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7776 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7777 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7778 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7779 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7780 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7781 case LT_EXPR: tcode = Lt_Expr; break;
7782 case LE_EXPR: tcode = Le_Expr; break;
7783 case GT_EXPR: tcode = Gt_Expr; break;
7784 case GE_EXPR: tcode = Ge_Expr; break;
7785 case EQ_EXPR: tcode = Eq_Expr; break;
7786 case NE_EXPR: tcode = Ne_Expr; break;
7789 tcode = Bit_And_Expr;
7790 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7791 Such values appear in expressions with aligning patterns. Note that,
7792 since sizetype is unsigned, we have to jump through some hoops. */
7793 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7795 tree op1 = TREE_OPERAND (gnu_size, 1);
7796 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7797 if (wi::neg_p (signed_op1))
7799 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7800 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7806 /* In regular mode, inline back only if symbolic annotation is requested
7807 in order to avoid memory explosion on big discriminated record types.
7808 But not in ASIS mode, as symbolic annotation is required for DDA. */
7809 if (List_Representation_Info == 3 || type_annotate_only)
7811 tree t = maybe_inline_call_in_expr (gnu_size);
7813 return annotate_value (t);
7816 return Uint_Minus_1;
7818 /* Fall through... */
7824 /* Now get each of the operands that's relevant for this code. If any
7825 cannot be expressed as a repinfo node, say we can't. */
7826 for (i = 0; i < 3; i++)
7829 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7831 if (i == 1 && pre_op1 != No_Uint)
7834 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7835 if (ops[i] == No_Uint)
7839 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7841 /* Save the result in the cache. */
7844 struct tree_int_map **h;
7845 /* We can't assume the hash table data hasn't moved since the initial
7846 look up, so we have to search again. Allocating and inserting an
7847 entry at that point would be an alternative, but then we'd better
7848 discard the entry if we decided not to cache it. */
7849 h = annotate_value_cache->find_slot (&in, INSERT);
7851 *h = ggc_alloc<tree_int_map> ();
7852 (*h)->base.from = gnu_size;
7859 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7860 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7861 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7862 BY_REF is true if the object is used by reference. */
7865 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7869 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7870 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7872 gnu_type = TREE_TYPE (gnu_type);
7875 if (Unknown_Esize (gnat_entity))
7877 if (TREE_CODE (gnu_type) == RECORD_TYPE
7878 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7879 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7881 size = TYPE_SIZE (gnu_type);
7884 Set_Esize (gnat_entity, annotate_value (size));
7887 if (Unknown_Alignment (gnat_entity))
7888 Set_Alignment (gnat_entity,
7889 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7892 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7893 Return NULL_TREE if there is no such element in the list. */
7896 purpose_member_field (const_tree elem, tree list)
7900 tree field = TREE_PURPOSE (list);
7901 if (SAME_FIELD_P (field, elem))
7903 list = TREE_CHAIN (list);
7908 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7909 set Component_Bit_Offset and Esize of the components to the position and
7910 size used by Gigi. */
7913 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7915 Entity_Id gnat_field;
7918 /* We operate by first making a list of all fields and their position (we
7919 can get the size easily) and then update all the sizes in the tree. */
7921 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7922 BIGGEST_ALIGNMENT, NULL_TREE);
7924 for (gnat_field = First_Entity (gnat_entity);
7925 Present (gnat_field);
7926 gnat_field = Next_Entity (gnat_field))
7927 if (Ekind (gnat_field) == E_Component
7928 || (Ekind (gnat_field) == E_Discriminant
7929 && !Is_Unchecked_Union (Scope (gnat_field))))
7931 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7937 /* If we are just annotating types and the type is tagged, the tag
7938 and the parent components are not generated by the front-end so
7939 we need to add the appropriate offset to each component without
7940 representation clause. */
7941 if (type_annotate_only
7942 && Is_Tagged_Type (gnat_entity)
7943 && No (Component_Clause (gnat_field)))
7945 /* For a component appearing in the current extension, the
7946 offset is the size of the parent. */
7947 if (Is_Derived_Type (gnat_entity)
7948 && Original_Record_Component (gnat_field) == gnat_field)
7950 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7953 parent_offset = bitsize_int (POINTER_SIZE);
7955 if (TYPE_FIELDS (gnu_type))
7957 = round_up (parent_offset,
7958 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7961 parent_offset = bitsize_zero_node;
7963 Set_Component_Bit_Offset
7966 (size_binop (PLUS_EXPR,
7967 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7968 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7971 Set_Esize (gnat_field,
7972 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7974 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7976 /* If there is no entry, this is an inherited component whose
7977 position is the same as in the parent type. */
7978 Entity_Id gnat_orig_field = Original_Record_Component (gnat_field);
7980 /* If we are just annotating types, discriminants renaming those of
7981 the parent have no entry so deal with them specifically. */
7982 if (type_annotate_only
7983 && gnat_orig_field == gnat_field
7984 && Ekind (gnat_field) == E_Discriminant)
7985 gnat_orig_field = Corresponding_Discriminant (gnat_field);
7987 Set_Component_Bit_Offset (gnat_field,
7988 Component_Bit_Offset (gnat_orig_field));
7990 Set_Esize (gnat_field, Esize (gnat_orig_field));
7995 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7996 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7997 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7998 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7999 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8000 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8001 pre-existing list to be chained to the newly created entries. */
8004 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
8005 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
8009 for (gnu_field = TYPE_FIELDS (gnu_type);
8011 gnu_field = DECL_CHAIN (gnu_field))
8013 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
8014 DECL_FIELD_BIT_OFFSET (gnu_field));
8015 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
8016 DECL_FIELD_OFFSET (gnu_field));
8017 unsigned int our_offset_align
8018 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
8019 tree v = make_tree_vec (3);
8021 TREE_VEC_ELT (v, 0) = gnu_our_offset;
8022 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
8023 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
8024 gnu_list = tree_cons (gnu_field, v, gnu_list);
8026 /* Recurse on internal fields, flattening the nested fields except for
8027 those in the variant part, if requested. */
8028 if (DECL_INTERNAL_P (gnu_field))
8030 tree gnu_field_type = TREE_TYPE (gnu_field);
8031 if (do_not_flatten_variant
8032 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
8034 = build_position_list (gnu_field_type, do_not_flatten_variant,
8035 size_zero_node, bitsize_zero_node,
8036 BIGGEST_ALIGNMENT, gnu_list);
8039 = build_position_list (gnu_field_type, do_not_flatten_variant,
8040 gnu_our_offset, gnu_our_bitpos,
8041 our_offset_align, gnu_list);
8048 /* Return a list describing the substitutions needed to reflect the
8049 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8050 be in any order. The values in an element of the list are in the form
8051 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8052 a definition of GNAT_SUBTYPE. */
8054 static vec<subst_pair>
8055 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
8057 vec<subst_pair> gnu_list = vNULL;
8058 Entity_Id gnat_discrim;
8059 Node_Id gnat_constr;
8061 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
8062 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
8063 Present (gnat_discrim);
8064 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
8065 gnat_constr = Next_Elmt (gnat_constr))
8066 /* Ignore access discriminants. */
8067 if (!Is_Access_Type (Etype (Node (gnat_constr))))
8069 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
8070 tree replacement = convert (TREE_TYPE (gnu_field),
8071 elaborate_expression
8072 (Node (gnat_constr), gnat_subtype,
8073 get_entity_char (gnat_discrim),
8074 definition, true, false));
8075 subst_pair s = {gnu_field, replacement};
8076 gnu_list.safe_push (s);
8082 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8083 variants of QUAL_UNION_TYPE that are still relevant after applying
8084 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8085 list to be prepended to the newly created entries. */
8087 static vec<variant_desc>
8088 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
8089 vec<variant_desc> gnu_list)
8093 for (gnu_field = TYPE_FIELDS (qual_union_type);
8095 gnu_field = DECL_CHAIN (gnu_field))
8097 tree qual = DECL_QUALIFIER (gnu_field);
8101 FOR_EACH_VEC_ELT (subst_list, i, s)
8102 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
8104 /* If the new qualifier is not unconditionally false, its variant may
8105 still be accessed. */
8106 if (!integer_zerop (qual))
8108 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
8109 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
8111 gnu_list.safe_push (v);
8113 /* Recurse on the variant subpart of the variant, if any. */
8114 variant_subpart = get_variant_part (variant_type);
8115 if (variant_subpart)
8116 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
8117 subst_list, gnu_list);
8119 /* If the new qualifier is unconditionally true, the subsequent
8120 variants cannot be accessed. */
8121 if (integer_onep (qual))
8129 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8130 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8131 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8132 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8133 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8134 true if we are being called to process the Component_Size of GNAT_OBJECT;
8135 this is used only for error messages. ZERO_OK is true if a size of zero
8136 is permitted; if ZERO_OK is false, it means that a size of zero should be
8137 treated as an unspecified size. */
8140 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
8141 enum tree_code kind, bool component_p, bool zero_ok)
8143 Node_Id gnat_error_node;
8144 tree type_size, size;
8146 /* Return 0 if no size was specified. */
8147 if (uint_size == No_Uint)
8150 /* Ignore a negative size since that corresponds to our back-annotation. */
8151 if (UI_Lt (uint_size, Uint_0))
8154 /* Find the node to use for error messages. */
8155 if ((Ekind (gnat_object) == E_Component
8156 || Ekind (gnat_object) == E_Discriminant)
8157 && Present (Component_Clause (gnat_object)))
8158 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
8159 else if (Present (Size_Clause (gnat_object)))
8160 gnat_error_node = Expression (Size_Clause (gnat_object));
8162 gnat_error_node = gnat_object;
8164 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8165 but cannot be represented in bitsizetype. */
8166 size = UI_To_gnu (uint_size, bitsizetype);
8167 if (TREE_OVERFLOW (size))
8170 post_error_ne ("component size for& is too large", gnat_error_node,
8173 post_error_ne ("size for& is too large", gnat_error_node,
8178 /* Ignore a zero size if it is not permitted. */
8179 if (!zero_ok && integer_zerop (size))
8182 /* The size of objects is always a multiple of a byte. */
8183 if (kind == VAR_DECL
8184 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
8187 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8188 gnat_error_node, gnat_object);
8190 post_error_ne ("size for& is not a multiple of Storage_Unit",
8191 gnat_error_node, gnat_object);
8195 /* If this is an integral type or a packed array type, the front-end has
8196 already verified the size, so we need not do it here (which would mean
8197 checking against the bounds). However, if this is an aliased object,
8198 it may not be smaller than the type of the object. */
8199 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
8200 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
8203 /* If the object is a record that contains a template, add the size of the
8204 template to the specified size. */
8205 if (TREE_CODE (gnu_type) == RECORD_TYPE
8206 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8207 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
8209 if (kind == VAR_DECL
8210 /* If a type needs strict alignment, a component of this type in
8211 a packed record cannot be packed and thus uses the type size. */
8212 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
8213 type_size = TYPE_SIZE (gnu_type);
8215 type_size = rm_size (gnu_type);
8217 /* Modify the size of a discriminated type to be the maximum size. */
8218 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
8219 type_size = max_size (type_size, true);
8221 /* If this is an access type or a fat pointer, the minimum size is that given
8222 by the smallest integral mode that's valid for pointers. */
8223 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
8225 machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
8226 while (!targetm.valid_pointer_mode (p_mode))
8227 p_mode = GET_MODE_WIDER_MODE (p_mode);
8228 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
8231 /* Issue an error either if the default size of the object isn't a constant
8232 or if the new size is smaller than it. */
8233 if (TREE_CODE (type_size) != INTEGER_CST
8234 || TREE_OVERFLOW (type_size)
8235 || tree_int_cst_lt (size, type_size))
8239 ("component size for& too small{, minimum allowed is ^}",
8240 gnat_error_node, gnat_object, type_size);
8243 ("size for& too small{, minimum allowed is ^}",
8244 gnat_error_node, gnat_object, type_size);
8251 /* Similarly, but both validate and process a value of RM size. This routine
8252 is only called for types. */
8255 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8257 Node_Id gnat_attr_node;
8258 tree old_size, size;
8260 /* Do nothing if no size was specified. */
8261 if (uint_size == No_Uint)
8264 /* Ignore a negative size since that corresponds to our back-annotation. */
8265 if (UI_Lt (uint_size, Uint_0))
8268 /* Only issue an error if a Value_Size clause was explicitly given.
8269 Otherwise, we'd be duplicating an error on the Size clause. */
8271 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8273 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8274 but cannot be represented in bitsizetype. */
8275 size = UI_To_gnu (uint_size, bitsizetype);
8276 if (TREE_OVERFLOW (size))
8278 if (Present (gnat_attr_node))
8279 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8284 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8285 exists, or this is an integer type, in which case the front-end will
8286 have always set it. */
8287 if (No (gnat_attr_node)
8288 && integer_zerop (size)
8289 && !Has_Size_Clause (gnat_entity)
8290 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8293 old_size = rm_size (gnu_type);
8295 /* If the old size is self-referential, get the maximum size. */
8296 if (CONTAINS_PLACEHOLDER_P (old_size))
8297 old_size = max_size (old_size, true);
8299 /* Issue an error either if the old size of the object isn't a constant or
8300 if the new size is smaller than it. The front-end has already verified
8301 this for scalar and packed array types. */
8302 if (TREE_CODE (old_size) != INTEGER_CST
8303 || TREE_OVERFLOW (old_size)
8304 || (AGGREGATE_TYPE_P (gnu_type)
8305 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8306 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8307 && !(TYPE_IS_PADDING_P (gnu_type)
8308 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8309 && TYPE_PACKED_ARRAY_TYPE_P
8310 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8311 && tree_int_cst_lt (size, old_size)))
8313 if (Present (gnat_attr_node))
8315 ("Value_Size for& too small{, minimum allowed is ^}",
8316 gnat_attr_node, gnat_entity, old_size);
8320 /* Otherwise, set the RM size proper for integral types... */
8321 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8322 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8323 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8324 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8325 SET_TYPE_RM_SIZE (gnu_type, size);
8327 /* ...or the Ada size for record and union types. */
8328 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8329 && !TYPE_FAT_POINTER_P (gnu_type))
8330 SET_TYPE_ADA_SIZE (gnu_type, size);
8333 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8334 a type or object whose present alignment is ALIGN. If this alignment is
8335 valid, return it. Otherwise, give an error and return ALIGN. */
8338 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8340 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8341 unsigned int new_align;
8342 Node_Id gnat_error_node;
8344 /* Don't worry about checking alignment if alignment was not specified
8345 by the source program and we already posted an error for this entity. */
8346 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8349 /* Post the error on the alignment clause if any. Note, for the implicit
8350 base type of an array type, the alignment clause is on the first
8352 if (Present (Alignment_Clause (gnat_entity)))
8353 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8355 else if (Is_Itype (gnat_entity)
8356 && Is_Array_Type (gnat_entity)
8357 && Etype (gnat_entity) == gnat_entity
8358 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8360 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8363 gnat_error_node = gnat_entity;
8365 /* Within GCC, an alignment is an integer, so we must make sure a value is
8366 specified that fits in that range. Also, there is an upper bound to
8367 alignments we can support/allow. */
8368 if (!UI_Is_In_Int_Range (alignment)
8369 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8370 post_error_ne_num ("largest supported alignment for& is ^",
8371 gnat_error_node, gnat_entity, max_allowed_alignment);
8372 else if (!(Present (Alignment_Clause (gnat_entity))
8373 && From_At_Mod (Alignment_Clause (gnat_entity)))
8374 && new_align * BITS_PER_UNIT < align)
8376 unsigned int double_align;
8377 bool is_capped_double, align_clause;
8379 /* If the default alignment of "double" or larger scalar types is
8380 specifically capped and the new alignment is above the cap, do
8381 not post an error and change the alignment only if there is an
8382 alignment clause; this makes it possible to have the associated
8383 GCC type overaligned by default for performance reasons. */
8384 if ((double_align = double_float_alignment) > 0)
8387 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8389 = is_double_float_or_array (gnat_type, &align_clause);
8391 else if ((double_align = double_scalar_alignment) > 0)
8394 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8396 = is_double_scalar_or_array (gnat_type, &align_clause);
8399 is_capped_double = align_clause = false;
8401 if (is_capped_double && new_align >= double_align)
8404 align = new_align * BITS_PER_UNIT;
8408 if (is_capped_double)
8409 align = double_align * BITS_PER_UNIT;
8411 post_error_ne_num ("alignment for& must be at least ^",
8412 gnat_error_node, gnat_entity,
8413 align / BITS_PER_UNIT);
8418 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8419 if (new_align > align)
8426 /* Verify that TYPE is something we can implement atomically. If not, issue
8427 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8428 process a component type. */
8431 check_ok_for_atomic_type (tree type, Entity_Id gnat_entity, bool component_p)
8433 Node_Id gnat_error_point = gnat_entity;
8436 enum mode_class mclass;
8440 /* If this is an anonymous base type, nothing to check, the error will be
8441 reported on the source type if need be. */
8442 if (!Comes_From_Source (gnat_entity))
8445 mode = TYPE_MODE (type);
8446 mclass = GET_MODE_CLASS (mode);
8447 align = TYPE_ALIGN (type);
8448 size = TYPE_SIZE (type);
8450 /* Consider all aligned floating-point types atomic and any aligned types
8451 that are represented by integers no wider than a machine word. */
8452 if ((mclass == MODE_FLOAT
8453 || ((mclass == MODE_INT || mclass == MODE_PARTIAL_INT)
8454 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8455 && align >= GET_MODE_ALIGNMENT (mode))
8458 /* For the moment, also allow anything that has an alignment equal to its
8459 size and which is smaller than a word. */
8461 && TREE_CODE (size) == INTEGER_CST
8462 && compare_tree_int (size, align) == 0
8463 && align <= BITS_PER_WORD)
8466 for (gnat_node = First_Rep_Item (gnat_entity);
8467 Present (gnat_node);
8468 gnat_node = Next_Rep_Item (gnat_node))
8469 if (Nkind (gnat_node) == N_Pragma)
8471 unsigned char pragma_id
8472 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
8474 if ((pragma_id == Pragma_Atomic && !component_p)
8475 || (pragma_id == Pragma_Atomic_Components && component_p))
8477 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8483 post_error_ne ("atomic access to component of & cannot be guaranteed",
8484 gnat_error_point, gnat_entity);
8485 else if (Is_Volatile_Full_Access (gnat_entity))
8486 post_error_ne ("volatile full access to & cannot be guaranteed",
8487 gnat_error_point, gnat_entity);
8489 post_error_ne ("atomic access to & cannot be guaranteed",
8490 gnat_error_point, gnat_entity);
8494 /* Helper for the intrin compatibility checks family. Evaluate whether
8495 two types are definitely incompatible. */
8498 intrin_types_incompatible_p (tree t1, tree t2)
8500 enum tree_code code;
8502 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8505 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8508 if (TREE_CODE (t1) != TREE_CODE (t2))
8511 code = TREE_CODE (t1);
8517 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8520 case REFERENCE_TYPE:
8521 /* Assume designated types are ok. We'd need to account for char * and
8522 void * variants to do better, which could rapidly get messy and isn't
8523 clearly worth the effort. */
8533 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8534 on the Ada/builtin argument lists for the INB binding. */
8537 intrin_arglists_compatible_p (intrin_binding_t * inb)
8539 function_args_iterator ada_iter, btin_iter;
8541 function_args_iter_init (&ada_iter, inb->ada_fntype);
8542 function_args_iter_init (&btin_iter, inb->btin_fntype);
8544 /* Sequence position of the last argument we checked. */
8549 tree ada_type = function_args_iter_cond (&ada_iter);
8550 tree btin_type = function_args_iter_cond (&btin_iter);
8552 /* If we've exhausted both lists simultaneously, we're done. */
8553 if (!ada_type && !btin_type)
8556 /* If one list is shorter than the other, they fail to match. */
8557 if (!ada_type || !btin_type)
8560 /* If we're done with the Ada args and not with the internal builtin
8561 args, or the other way around, complain. */
8562 if (ada_type == void_type_node
8563 && btin_type != void_type_node)
8565 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8569 if (btin_type == void_type_node
8570 && ada_type != void_type_node)
8572 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8573 inb->gnat_entity, inb->gnat_entity, argpos);
8577 /* Otherwise, check that types match for the current argument. */
8579 if (intrin_types_incompatible_p (ada_type, btin_type))
8581 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8582 inb->gnat_entity, inb->gnat_entity, argpos);
8587 function_args_iter_next (&ada_iter);
8588 function_args_iter_next (&btin_iter);
8594 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8595 on the Ada/builtin return values for the INB binding. */
8598 intrin_return_compatible_p (intrin_binding_t * inb)
8600 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8601 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8603 /* Accept function imported as procedure, common and convenient. */
8604 if (VOID_TYPE_P (ada_return_type)
8605 && !VOID_TYPE_P (btin_return_type))
8608 /* If return type is Address (integer type), map it to void *. */
8609 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8610 ada_return_type = ptr_type_node;
8612 /* Check return types compatibility otherwise. Note that this
8613 handles void/void as well. */
8614 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8616 post_error ("?intrinsic binding type mismatch on return value!",
8624 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8625 compatible. Issue relevant warnings when they are not.
8627 This is intended as a light check to diagnose the most obvious cases, not
8628 as a full fledged type compatibility predicate. It is the programmer's
8629 responsibility to ensure correctness of the Ada declarations in Imports,
8630 especially when binding straight to a compiler internal. */
8633 intrin_profiles_compatible_p (intrin_binding_t * inb)
8635 /* Check compatibility on return values and argument lists, each responsible
8636 for posting warnings as appropriate. Ensure use of the proper sloc for
8639 bool arglists_compatible_p, return_compatible_p;
8640 location_t saved_location = input_location;
8642 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8644 return_compatible_p = intrin_return_compatible_p (inb);
8645 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8647 input_location = saved_location;
8649 return return_compatible_p && arglists_compatible_p;
8652 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8653 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8654 specified size for this field. POS_LIST is a position list describing
8655 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8659 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8660 tree size, tree pos_list,
8661 vec<subst_pair> subst_list)
8663 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8664 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8665 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8666 tree new_pos, new_field;
8670 if (CONTAINS_PLACEHOLDER_P (pos))
8671 FOR_EACH_VEC_ELT (subst_list, i, s)
8672 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8674 /* If the position is now a constant, we can set it as the position of the
8675 field when we make it. Otherwise, we need to deal with it specially. */
8676 if (TREE_CONSTANT (pos))
8677 new_pos = bit_from_pos (pos, bitpos);
8679 new_pos = NULL_TREE;
8682 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8683 size, new_pos, DECL_PACKED (old_field),
8684 !DECL_NONADDRESSABLE_P (old_field));
8688 normalize_offset (&pos, &bitpos, offset_align);
8689 /* Finalize the position. */
8690 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8691 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8692 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8693 DECL_SIZE (new_field) = size;
8694 DECL_SIZE_UNIT (new_field)
8695 = convert (sizetype,
8696 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8697 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8700 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8701 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8702 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8703 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8708 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8709 it is the minimal size the REP_PART must have. */
8712 create_rep_part (tree rep_type, tree record_type, tree min_size)
8716 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8717 min_size = NULL_TREE;
8719 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8720 min_size, NULL_TREE, 0, 1);
8721 DECL_INTERNAL_P (field) = 1;
8726 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8729 get_rep_part (tree record_type)
8731 tree field = TYPE_FIELDS (record_type);
8733 /* The REP part is the first field, internal, another record, and its name
8734 starts with an 'R'. */
8736 && DECL_INTERNAL_P (field)
8737 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8738 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8744 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8747 get_variant_part (tree record_type)
8751 /* The variant part is the only internal field that is a qualified union. */
8752 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8753 if (DECL_INTERNAL_P (field)
8754 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8760 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8761 the list of variants to be used and RECORD_TYPE is the type of the parent.
8762 POS_LIST is a position list describing the layout of fields present in
8763 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8767 create_variant_part_from (tree old_variant_part,
8768 vec<variant_desc> variant_list,
8769 tree record_type, tree pos_list,
8770 vec<subst_pair> subst_list)
8772 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8773 tree old_union_type = TREE_TYPE (old_variant_part);
8774 tree new_union_type, new_variant_part;
8775 tree union_field_list = NULL_TREE;
8779 /* First create the type of the variant part from that of the old one. */
8780 new_union_type = make_node (QUAL_UNION_TYPE);
8781 TYPE_NAME (new_union_type)
8782 = concat_name (TYPE_NAME (record_type),
8783 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8785 /* If the position of the variant part is constant, subtract it from the
8786 size of the type of the parent to get the new size. This manual CSE
8787 reduces the code size when not optimizing. */
8788 if (TREE_CODE (offset) == INTEGER_CST)
8790 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8791 tree first_bit = bit_from_pos (offset, bitpos);
8792 TYPE_SIZE (new_union_type)
8793 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8794 TYPE_SIZE_UNIT (new_union_type)
8795 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8796 byte_from_pos (offset, bitpos));
8797 SET_TYPE_ADA_SIZE (new_union_type,
8798 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8800 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8801 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8804 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8806 /* Now finish up the new variants and populate the union type. */
8807 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8809 tree old_field = v->field, new_field;
8810 tree old_variant, old_variant_subpart, new_variant, field_list;
8812 /* Skip variants that don't belong to this nesting level. */
8813 if (DECL_CONTEXT (old_field) != old_union_type)
8816 /* Retrieve the list of fields already added to the new variant. */
8817 new_variant = v->new_type;
8818 field_list = TYPE_FIELDS (new_variant);
8820 /* If the old variant had a variant subpart, we need to create a new
8821 variant subpart and add it to the field list. */
8822 old_variant = v->type;
8823 old_variant_subpart = get_variant_part (old_variant);
8824 if (old_variant_subpart)
8826 tree new_variant_subpart
8827 = create_variant_part_from (old_variant_subpart, variant_list,
8828 new_variant, pos_list, subst_list);
8829 DECL_CHAIN (new_variant_subpart) = field_list;
8830 field_list = new_variant_subpart;
8833 /* Finish up the new variant and create the field. No need for debug
8834 info thanks to the XVS type. */
8835 finish_record_type (new_variant, nreverse (field_list), 2, false);
8836 compute_record_mode (new_variant);
8837 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8841 = create_field_decl_from (old_field, new_variant, new_union_type,
8842 TYPE_SIZE (new_variant),
8843 pos_list, subst_list);
8844 DECL_QUALIFIER (new_field) = v->qual;
8845 DECL_INTERNAL_P (new_field) = 1;
8846 DECL_CHAIN (new_field) = union_field_list;
8847 union_field_list = new_field;
8850 /* Finish up the union type and create the variant part. No need for debug
8851 info thanks to the XVS type. Note that we don't reverse the field list
8852 because VARIANT_LIST has been traversed in reverse order. */
8853 finish_record_type (new_union_type, union_field_list, 2, false);
8854 compute_record_mode (new_union_type);
8855 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8859 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8860 TYPE_SIZE (new_union_type),
8861 pos_list, subst_list);
8862 DECL_INTERNAL_P (new_variant_part) = 1;
8864 /* With multiple discriminants it is possible for an inner variant to be
8865 statically selected while outer ones are not; in this case, the list
8866 of fields of the inner variant is not flattened and we end up with a
8867 qualified union with a single member. Drop the useless container. */
8868 if (!DECL_CHAIN (union_field_list))
8870 DECL_CONTEXT (union_field_list) = record_type;
8871 DECL_FIELD_OFFSET (union_field_list)
8872 = DECL_FIELD_OFFSET (new_variant_part);
8873 DECL_FIELD_BIT_OFFSET (union_field_list)
8874 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8875 SET_DECL_OFFSET_ALIGN (union_field_list,
8876 DECL_OFFSET_ALIGN (new_variant_part));
8877 new_variant_part = union_field_list;
8880 return new_variant_part;
8883 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8884 which are both RECORD_TYPE, after applying the substitutions described
8888 copy_and_substitute_in_size (tree new_type, tree old_type,
8889 vec<subst_pair> subst_list)
8894 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8895 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8896 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8897 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8898 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8900 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8901 FOR_EACH_VEC_ELT (subst_list, i, s)
8902 TYPE_SIZE (new_type)
8903 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8904 s->discriminant, s->replacement);
8906 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8907 FOR_EACH_VEC_ELT (subst_list, i, s)
8908 TYPE_SIZE_UNIT (new_type)
8909 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8910 s->discriminant, s->replacement);
8912 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8913 FOR_EACH_VEC_ELT (subst_list, i, s)
8915 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8916 s->discriminant, s->replacement));
8918 /* Finalize the size. */
8919 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8920 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8923 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
8924 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
8925 the original array type if it has been translated. This association is a
8926 parallel type for GNAT encodings or a debug type for standard DWARF. Note
8927 that for standard DWARF, we also want to get the original type name. */
8930 associate_original_type_to_packed_array (tree gnu_type, Entity_Id gnat_entity)
8932 Entity_Id gnat_original_array_type
8933 = Underlying_Type (Original_Array_Type (gnat_entity));
8934 tree gnu_original_array_type;
8936 if (!present_gnu_tree (gnat_original_array_type))
8939 gnu_original_array_type = gnat_to_gnu_type (gnat_original_array_type);
8941 if (TYPE_IS_DUMMY_P (gnu_original_array_type))
8944 if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
8946 tree original_name = TYPE_NAME (gnu_original_array_type);
8948 if (TREE_CODE (original_name) == TYPE_DECL)
8949 original_name = DECL_NAME (original_name);
8951 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type, gnu_original_array_type);
8952 TYPE_NAME (gnu_type) = original_name;
8955 add_parallel_type (gnu_type, gnu_original_array_type);
8958 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8959 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8960 updated by replacing F with R.
8962 The function doesn't update the layout of the type, i.e. it assumes
8963 that the substitution is purely formal. That's why the replacement
8964 value R must itself contain a PLACEHOLDER_EXPR. */
8967 substitute_in_type (tree t, tree f, tree r)
8971 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8973 switch (TREE_CODE (t))
8980 /* First the domain types of arrays. */
8981 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8982 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8984 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8985 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8987 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8991 TYPE_GCC_MIN_VALUE (nt) = low;
8992 TYPE_GCC_MAX_VALUE (nt) = high;
8994 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8996 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
9001 /* Then the subtypes. */
9002 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
9003 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
9005 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
9006 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
9008 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
9012 SET_TYPE_RM_MIN_VALUE (nt, low);
9013 SET_TYPE_RM_MAX_VALUE (nt, high);
9021 nt = substitute_in_type (TREE_TYPE (t), f, r);
9022 if (nt == TREE_TYPE (t))
9025 return build_complex_type (nt);
9028 /* These should never show up here. */
9033 tree component = substitute_in_type (TREE_TYPE (t), f, r);
9034 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
9036 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9039 nt = build_nonshared_array_type (component, domain);
9040 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
9041 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
9042 SET_TYPE_MODE (nt, TYPE_MODE (t));
9043 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9044 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9045 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
9046 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
9047 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
9053 case QUAL_UNION_TYPE:
9055 bool changed_field = false;
9058 /* Start out with no fields, make new fields, and chain them
9059 in. If we haven't actually changed the type of any field,
9060 discard everything we've done and return the old type. */
9062 TYPE_FIELDS (nt) = NULL_TREE;
9064 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9066 tree new_field = copy_node (field), new_n;
9068 new_n = substitute_in_type (TREE_TYPE (field), f, r);
9069 if (new_n != TREE_TYPE (field))
9071 TREE_TYPE (new_field) = new_n;
9072 changed_field = true;
9075 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
9076 if (new_n != DECL_FIELD_OFFSET (field))
9078 DECL_FIELD_OFFSET (new_field) = new_n;
9079 changed_field = true;
9082 /* Do the substitution inside the qualifier, if any. */
9083 if (TREE_CODE (t) == QUAL_UNION_TYPE)
9085 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
9086 if (new_n != DECL_QUALIFIER (field))
9088 DECL_QUALIFIER (new_field) = new_n;
9089 changed_field = true;
9093 DECL_CONTEXT (new_field) = nt;
9094 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
9096 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
9097 TYPE_FIELDS (nt) = new_field;
9103 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
9104 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
9105 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
9106 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
9115 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9116 needed to represent the object. */
9119 rm_size (tree gnu_type)
9121 /* For integral types, we store the RM size explicitly. */
9122 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
9123 return TYPE_RM_SIZE (gnu_type);
9125 /* Return the RM size of the actual data plus the size of the template. */
9126 if (TREE_CODE (gnu_type) == RECORD_TYPE
9127 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
9129 size_binop (PLUS_EXPR,
9130 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
9131 DECL_SIZE (TYPE_FIELDS (gnu_type)));
9133 /* For record or union types, we store the size explicitly. */
9134 if (RECORD_OR_UNION_TYPE_P (gnu_type)
9135 && !TYPE_FAT_POINTER_P (gnu_type)
9136 && TYPE_ADA_SIZE (gnu_type))
9137 return TYPE_ADA_SIZE (gnu_type);
9139 /* For other types, this is just the size. */
9140 return TYPE_SIZE (gnu_type);
9143 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9144 fully-qualified name, possibly with type information encoding.
9145 Otherwise, return the name. */
9148 get_entity_char (Entity_Id gnat_entity)
9150 Get_Encoded_Name (gnat_entity);
9151 return ggc_strdup (Name_Buffer);
9155 get_entity_name (Entity_Id gnat_entity)
9157 Get_Encoded_Name (gnat_entity);
9158 return get_identifier_with_length (Name_Buffer, Name_Len);
9161 /* Return an identifier representing the external name to be used for
9162 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9163 and the specified suffix. */
9166 create_concat_name (Entity_Id gnat_entity, const char *suffix)
9168 const Entity_Kind kind = Ekind (gnat_entity);
9169 const bool has_suffix = (suffix != NULL);
9170 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
9171 String_Pointer sp = {suffix, &temp};
9173 Get_External_Name (gnat_entity, has_suffix, sp);
9175 /* A variable using the Stdcall convention lives in a DLL. We adjust
9176 its name to use the jump table, the _imp__NAME contains the address
9177 for the NAME variable. */
9178 if ((kind == E_Variable || kind == E_Constant)
9179 && Has_Stdcall_Convention (gnat_entity))
9181 const int len = strlen (STDCALL_PREFIX) + Name_Len;
9182 char *new_name = (char *) alloca (len + 1);
9183 strcpy (new_name, STDCALL_PREFIX);
9184 strcat (new_name, Name_Buffer);
9185 return get_identifier_with_length (new_name, len);
9188 return get_identifier_with_length (Name_Buffer, Name_Len);
9191 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9192 string, return a new IDENTIFIER_NODE that is the concatenation of
9193 the name followed by "___" and the specified suffix. */
9196 concat_name (tree gnu_name, const char *suffix)
9198 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
9199 char *new_name = (char *) alloca (len + 1);
9200 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
9201 strcat (new_name, "___");
9202 strcat (new_name, suffix);
9203 return get_identifier_with_length (new_name, len);
9206 /* Initialize data structures of the decl.c module. */
9209 init_gnat_decl (void)
9211 /* Initialize the cache of annotated values. */
9212 annotate_value_cache = hash_table<value_annotation_hasher>::create_ggc (512);
9215 /* Destroy data structures of the decl.c module. */
9218 destroy_gnat_decl (void)
9220 /* Destroy the cache of annotated values. */
9221 annotate_value_cache->empty ();
9222 annotate_value_cache = NULL;
9225 #include "gt-ada-decl.h"