1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2009, 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"
53 #ifndef MAX_FIXED_MODE_SIZE
54 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
57 /* Convention_Stdcall should be processed in a specific way on Windows targets
58 only. The macro below is a helper to avoid having to check for a Windows
59 specific attribute throughout this unit. */
61 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
62 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
64 #define Has_Stdcall_Convention(E) (0)
67 /* Stack realignment for functions with foreign conventions is provided on a
68 per back-end basis now, as it is handled by the prologue expanders and not
69 as part of the function's body any more. It might be requested by way of a
70 dedicated function type attribute on the targets that support it.
72 We need a way to avoid setting the attribute on the targets that don't
73 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
75 It is defined on targets where the circuitry is available, and indicates
76 whether the realignment is needed for 'main'. We use this to decide for
77 foreign subprograms as well.
79 It is not defined on targets where the circuitry is not implemented, and
80 we just never set the attribute in these cases.
82 Whether it is defined on all targets that would need it in theory is
83 not entirely clear. We currently trust the base GCC settings for this
86 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
87 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
92 struct incomplete *next;
97 /* These variables are used to defer recursively expanding incomplete types
98 while we are processing an array, a record or a subprogram type. */
99 static int defer_incomplete_level = 0;
100 static struct incomplete *defer_incomplete_list;
102 /* This variable is used to delay expanding From_With_Type types until the
104 static struct incomplete *defer_limited_with;
106 /* These variables are used to defer finalizing types. The element of the
107 list is the TYPE_DECL associated with the type. */
108 static int defer_finalize_level = 0;
109 static VEC (tree,heap) *defer_finalize_list;
111 /* A hash table used to cache the result of annotate_value. */
112 static GTY ((if_marked ("tree_int_map_marked_p"),
113 param_is (struct tree_int_map))) htab_t annotate_value_cache;
122 static void relate_alias_sets (tree, tree, enum alias_set_op);
124 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
125 static bool allocatable_size_p (tree, bool);
126 static void prepend_one_attribute_to (struct attrib **,
127 enum attr_type, tree, tree, Node_Id);
128 static void prepend_attributes (Entity_Id, struct attrib **);
129 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
130 static bool is_variable_size (tree);
131 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
133 static tree make_packable_type (tree, bool);
134 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
135 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
137 static bool same_discriminant_p (Entity_Id, Entity_Id);
138 static bool array_type_has_nonaliased_component (Entity_Id, tree);
139 static bool compile_time_known_address_p (Node_Id);
140 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
141 bool, bool, bool, bool);
142 static Uint annotate_value (tree);
143 static void annotate_rep (Entity_Id, tree);
144 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
145 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
146 static void set_rm_size (Uint, tree, Entity_Id);
147 static tree make_type_from_size (tree, tree, bool);
148 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
149 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
150 static void check_ok_for_atomic (tree, Entity_Id, bool);
151 static int compatible_signatures_p (tree ftype1, tree ftype2);
152 static void rest_of_type_decl_compilation_no_defer (tree);
154 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
155 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
156 and associate the ..._DECL node with the input GNAT defining identifier.
158 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
159 initial value (in GCC tree form). This is optional for a variable. For
160 a renamed entity, GNU_EXPR gives the object being renamed.
162 DEFINITION is nonzero if this call is intended for a definition. This is
163 used for separate compilation where it is necessary to know whether an
164 external declaration or a definition must be created if the GCC equivalent
165 was not created previously. The value of 1 is normally used for a nonzero
166 DEFINITION, but a value of 2 is used in special circumstances, defined in
170 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
172 /* Contains the kind of the input GNAT node. */
173 const Entity_Kind kind = Ekind (gnat_entity);
174 /* True if this is a type. */
175 const bool is_type = IN (kind, Type_Kind);
176 /* For a type, contains the equivalent GNAT node to be used in gigi. */
177 Entity_Id gnat_equiv_type = Empty;
178 /* Temporary used to walk the GNAT tree. */
180 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
181 This node will be associated with the GNAT node by calling at the end
182 of the `switch' statement. */
183 tree gnu_decl = NULL_TREE;
184 /* Contains the GCC type to be used for the GCC node. */
185 tree gnu_type = NULL_TREE;
186 /* Contains the GCC size tree to be used for the GCC node. */
187 tree gnu_size = NULL_TREE;
188 /* Contains the GCC name to be used for the GCC node. */
189 tree gnu_entity_name;
190 /* True if we have already saved gnu_decl as a GNAT association. */
192 /* True if we incremented defer_incomplete_level. */
193 bool this_deferred = false;
194 /* True if we incremented force_global. */
195 bool this_global = false;
196 /* True if we should check to see if elaborated during processing. */
197 bool maybe_present = false;
198 /* True if we made GNU_DECL and its type here. */
199 bool this_made_decl = false;
200 /* True if debug info is requested for this entity. */
201 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
202 || debug_info_level == DINFO_LEVEL_VERBOSE);
203 /* True if this entity is to be considered as imported. */
204 bool imported_p = (Is_Imported (gnat_entity)
205 && No (Address_Clause (gnat_entity)));
206 /* Size and alignment of the GCC node, if meaningful. */
207 unsigned int esize = 0, align = 0;
208 /* Contains the list of attributes directly attached to the entity. */
209 struct attrib *attr_list = NULL;
211 /* Since a use of an Itype is a definition, process it as such if it
212 is not in a with'ed unit. */
215 && Is_Itype (gnat_entity)
216 && !present_gnu_tree (gnat_entity)
217 && In_Extended_Main_Code_Unit (gnat_entity))
219 /* Ensure that we are in a subprogram mentioned in the Scope chain of
220 this entity, our current scope is global, or we encountered a task
221 or entry (where we can't currently accurately check scoping). */
222 if (!current_function_decl
223 || DECL_ELABORATION_PROC_P (current_function_decl))
225 process_type (gnat_entity);
226 return get_gnu_tree (gnat_entity);
229 for (gnat_temp = Scope (gnat_entity);
231 gnat_temp = Scope (gnat_temp))
233 if (Is_Type (gnat_temp))
234 gnat_temp = Underlying_Type (gnat_temp);
236 if (Ekind (gnat_temp) == E_Subprogram_Body)
238 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
240 if (IN (Ekind (gnat_temp), Subprogram_Kind)
241 && Present (Protected_Body_Subprogram (gnat_temp)))
242 gnat_temp = Protected_Body_Subprogram (gnat_temp);
244 if (Ekind (gnat_temp) == E_Entry
245 || Ekind (gnat_temp) == E_Entry_Family
246 || Ekind (gnat_temp) == E_Task_Type
247 || (IN (Ekind (gnat_temp), Subprogram_Kind)
248 && present_gnu_tree (gnat_temp)
249 && (current_function_decl
250 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
252 process_type (gnat_entity);
253 return get_gnu_tree (gnat_entity);
257 /* This abort means the Itype has an incorrect scope, i.e. that its
258 scope does not correspond to the subprogram it is declared in. */
262 /* If we've already processed this entity, return what we got last time.
263 If we are defining the node, we should not have already processed it.
264 In that case, we will abort below when we try to save a new GCC tree
265 for this object. We also need to handle the case of getting a dummy
266 type when a Full_View exists. */
267 if ((!definition || (is_type && imported_p))
268 && present_gnu_tree (gnat_entity))
270 gnu_decl = get_gnu_tree (gnat_entity);
272 if (TREE_CODE (gnu_decl) == TYPE_DECL
273 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
274 && IN (kind, Incomplete_Or_Private_Kind)
275 && Present (Full_View (gnat_entity)))
278 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
279 save_gnu_tree (gnat_entity, NULL_TREE, false);
280 save_gnu_tree (gnat_entity, gnu_decl, false);
286 /* If this is a numeric or enumeral type, or an access type, a nonzero
287 Esize must be specified unless it was specified by the programmer. */
288 gcc_assert (!Unknown_Esize (gnat_entity)
289 || Has_Size_Clause (gnat_entity)
290 || (!IN (kind, Numeric_Kind)
291 && !IN (kind, Enumeration_Kind)
292 && (!IN (kind, Access_Kind)
293 || kind == E_Access_Protected_Subprogram_Type
294 || kind == E_Anonymous_Access_Protected_Subprogram_Type
295 || kind == E_Access_Subtype)));
297 /* The RM size must be specified for all discrete and fixed-point types. */
298 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
299 && Unknown_RM_Size (gnat_entity)));
301 /* If we get here, it means we have not yet done anything with this entity.
302 If we are not defining it, it must be a type or an entity that is defined
303 elsewhere or externally, otherwise we should have defined it already. */
304 gcc_assert (definition
305 || type_annotate_only
307 || kind == E_Discriminant
308 || kind == E_Component
310 || (kind == E_Constant && Present (Full_View (gnat_entity)))
311 || Is_Public (gnat_entity));
313 /* Get the name of the entity and set up the line number and filename of
314 the original definition for use in any decl we make. */
315 gnu_entity_name = get_entity_name (gnat_entity);
316 Sloc_to_locus (Sloc (gnat_entity), &input_location);
318 /* For cases when we are not defining (i.e., we are referencing from
319 another compilation unit) public entities, show we are at global level
320 for the purpose of computing scopes. Don't do this for components or
321 discriminants since the relevant test is whether or not the record is
324 && kind != E_Component
325 && kind != E_Discriminant
326 && Is_Public (gnat_entity)
327 && !Is_Statically_Allocated (gnat_entity))
328 force_global++, this_global = true;
330 /* Handle any attributes directly attached to the entity. */
331 if (Has_Gigi_Rep_Item (gnat_entity))
332 prepend_attributes (gnat_entity, &attr_list);
334 /* Do some common processing for types. */
337 /* Compute the equivalent type to be used in gigi. */
338 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
340 /* Machine_Attributes on types are expected to be propagated to
341 subtypes. The corresponding Gigi_Rep_Items are only attached
342 to the first subtype though, so we handle the propagation here. */
343 if (Base_Type (gnat_entity) != gnat_entity
344 && !Is_First_Subtype (gnat_entity)
345 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
346 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
349 /* Compute a default value for the size of the type. */
350 if (Known_Esize (gnat_entity)
351 && UI_Is_In_Int_Range (Esize (gnat_entity)))
353 unsigned int max_esize;
354 esize = UI_To_Int (Esize (gnat_entity));
356 if (IN (kind, Float_Kind))
357 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
358 else if (IN (kind, Access_Kind))
359 max_esize = POINTER_SIZE * 2;
361 max_esize = LONG_LONG_TYPE_SIZE;
363 if (esize > max_esize)
367 esize = LONG_LONG_TYPE_SIZE;
373 /* If this is a use of a deferred constant without address clause,
374 get its full definition. */
376 && No (Address_Clause (gnat_entity))
377 && Present (Full_View (gnat_entity)))
380 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
385 /* If we have an external constant that we are not defining, get the
386 expression that is was defined to represent. We may throw that
387 expression away later if it is not a constant. Do not retrieve the
388 expression if it is an aggregate or allocator, because in complex
389 instantiation contexts it may not be expanded */
391 && Present (Expression (Declaration_Node (gnat_entity)))
392 && !No_Initialization (Declaration_Node (gnat_entity))
393 && (Nkind (Expression (Declaration_Node (gnat_entity)))
395 && (Nkind (Expression (Declaration_Node (gnat_entity)))
397 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
399 /* Ignore deferred constant definitions without address clause since
400 they are processed fully in the front-end. If No_Initialization
401 is set, this is not a deferred constant but a constant whose value
402 is built manually. And constants that are renamings are handled
406 && No (Address_Clause (gnat_entity))
407 && !No_Initialization (Declaration_Node (gnat_entity))
408 && No (Renamed_Object (gnat_entity)))
410 gnu_decl = error_mark_node;
415 /* Ignore constant definitions already marked with the error node. See
416 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
419 && present_gnu_tree (gnat_entity)
420 && get_gnu_tree (gnat_entity) == error_mark_node)
422 maybe_present = true;
429 /* We used to special case VMS exceptions here to directly map them to
430 their associated condition code. Since this code had to be masked
431 dynamically to strip off the severity bits, this caused trouble in
432 the GCC/ZCX case because the "type" pointers we store in the tables
433 have to be static. We now don't special case here anymore, and let
434 the regular processing take place, which leaves us with a regular
435 exception data object for VMS exceptions too. The condition code
436 mapping is taken care of by the front end and the bitmasking by the
443 /* The GNAT record where the component was defined. */
444 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
446 /* If the variable is an inherited record component (in the case of
447 extended record types), just return the inherited entity, which
448 must be a FIELD_DECL. Likewise for discriminants.
449 For discriminants of untagged records which have explicit
450 stored discriminants, return the entity for the corresponding
451 stored discriminant. Also use Original_Record_Component
452 if the record has a private extension. */
453 if (Present (Original_Record_Component (gnat_entity))
454 && Original_Record_Component (gnat_entity) != gnat_entity)
457 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
458 gnu_expr, definition);
463 /* If the enclosing record has explicit stored discriminants,
464 then it is an untagged record. If the Corresponding_Discriminant
465 is not empty then this must be a renamed discriminant and its
466 Original_Record_Component must point to the corresponding explicit
467 stored discriminant (i.e. we should have taken the previous
469 else if (Present (Corresponding_Discriminant (gnat_entity))
470 && Is_Tagged_Type (gnat_record))
472 /* A tagged record has no explicit stored discriminants. */
473 gcc_assert (First_Discriminant (gnat_record)
474 == First_Stored_Discriminant (gnat_record));
476 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
477 gnu_expr, definition);
482 else if (Present (CR_Discriminant (gnat_entity))
483 && type_annotate_only)
485 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
486 gnu_expr, definition);
491 /* If the enclosing record has explicit stored discriminants, then
492 it is an untagged record. If the Corresponding_Discriminant
493 is not empty then this must be a renamed discriminant and its
494 Original_Record_Component must point to the corresponding explicit
495 stored discriminant (i.e. we should have taken the first
497 else if (Present (Corresponding_Discriminant (gnat_entity))
498 && (First_Discriminant (gnat_record)
499 != First_Stored_Discriminant (gnat_record)))
502 /* Otherwise, if we are not defining this and we have no GCC type
503 for the containing record, make one for it. Then we should
504 have made our own equivalent. */
505 else if (!definition && !present_gnu_tree (gnat_record))
507 /* ??? If this is in a record whose scope is a protected
508 type and we have an Original_Record_Component, use it.
509 This is a workaround for major problems in protected type
511 Entity_Id Scop = Scope (Scope (gnat_entity));
512 if ((Is_Protected_Type (Scop)
513 || (Is_Private_Type (Scop)
514 && Present (Full_View (Scop))
515 && Is_Protected_Type (Full_View (Scop))))
516 && Present (Original_Record_Component (gnat_entity)))
519 = gnat_to_gnu_entity (Original_Record_Component
526 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
527 gnu_decl = get_gnu_tree (gnat_entity);
533 /* Here we have no GCC type and this is a reference rather than a
534 definition. This should never happen. Most likely the cause is
535 reference before declaration in the gnat tree for gnat_entity. */
539 case E_Loop_Parameter:
540 case E_Out_Parameter:
543 /* Simple variables, loop variables, Out parameters, and exceptions. */
546 bool used_by_ref = false;
548 = ((kind == E_Constant || kind == E_Variable)
549 && Is_True_Constant (gnat_entity)
550 && !Treat_As_Volatile (gnat_entity)
551 && (((Nkind (Declaration_Node (gnat_entity))
552 == N_Object_Declaration)
553 && Present (Expression (Declaration_Node (gnat_entity))))
554 || Present (Renamed_Object (gnat_entity))));
555 bool inner_const_flag = const_flag;
556 bool static_p = Is_Statically_Allocated (gnat_entity);
557 bool mutable_p = false;
558 tree gnu_ext_name = NULL_TREE;
559 tree renamed_obj = NULL_TREE;
560 tree gnu_object_size;
562 if (Present (Renamed_Object (gnat_entity)) && !definition)
564 if (kind == E_Exception)
565 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
568 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
571 /* Get the type after elaborating the renamed object. */
572 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
574 /* For a debug renaming declaration, build a pure debug entity. */
575 if (Present (Debug_Renaming_Link (gnat_entity)))
578 gnu_decl = build_decl (VAR_DECL, gnu_entity_name, gnu_type);
579 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
580 if (global_bindings_p ())
581 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
583 addr = stack_pointer_rtx;
584 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
585 gnat_pushdecl (gnu_decl, gnat_entity);
589 /* If this is a loop variable, its type should be the base type.
590 This is because the code for processing a loop determines whether
591 a normal loop end test can be done by comparing the bounds of the
592 loop against those of the base type, which is presumed to be the
593 size used for computation. But this is not correct when the size
594 of the subtype is smaller than the type. */
595 if (kind == E_Loop_Parameter)
596 gnu_type = get_base_type (gnu_type);
598 /* Reject non-renamed objects whose types are unconstrained arrays or
599 any object whose type is a dummy type or VOID_TYPE. */
601 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
602 && No (Renamed_Object (gnat_entity)))
603 || TYPE_IS_DUMMY_P (gnu_type)
604 || TREE_CODE (gnu_type) == VOID_TYPE)
606 gcc_assert (type_annotate_only);
609 return error_mark_node;
612 /* If an alignment is specified, use it if valid. Note that
613 exceptions are objects but don't have alignments. We must do this
614 before we validate the size, since the alignment can affect the
616 if (kind != E_Exception && Known_Alignment (gnat_entity))
618 gcc_assert (Present (Alignment (gnat_entity)));
619 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
620 TYPE_ALIGN (gnu_type));
621 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
622 "PAD", false, definition, true);
625 /* If we are defining the object, see if it has a Size value and
626 validate it if so. If we are not defining the object and a Size
627 clause applies, simply retrieve the value. We don't want to ignore
628 the clause and it is expected to have been validated already. Then
629 get the new type, if any. */
631 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
632 gnat_entity, VAR_DECL, false,
633 Has_Size_Clause (gnat_entity));
634 else if (Has_Size_Clause (gnat_entity))
635 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
640 = make_type_from_size (gnu_type, gnu_size,
641 Has_Biased_Representation (gnat_entity));
643 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
644 gnu_size = NULL_TREE;
647 /* If this object has self-referential size, it must be a record with
648 a default value. We are supposed to allocate an object of the
649 maximum size in this case unless it is a constant with an
650 initializing expression, in which case we can get the size from
651 that. Note that the resulting size may still be a variable, so
652 this may end up with an indirect allocation. */
653 if (No (Renamed_Object (gnat_entity))
654 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
656 if (gnu_expr && kind == E_Constant)
658 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
659 if (CONTAINS_PLACEHOLDER_P (size))
661 /* If the initializing expression is itself a constant,
662 despite having a nominal type with self-referential
663 size, we can get the size directly from it. */
664 if (TREE_CODE (gnu_expr) == COMPONENT_REF
665 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
668 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
669 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
670 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
671 || DECL_READONLY_ONCE_ELAB
672 (TREE_OPERAND (gnu_expr, 0))))
673 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
676 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
681 /* We may have no GNU_EXPR because No_Initialization is
682 set even though there's an Expression. */
683 else if (kind == E_Constant
684 && (Nkind (Declaration_Node (gnat_entity))
685 == N_Object_Declaration)
686 && Present (Expression (Declaration_Node (gnat_entity))))
688 = TYPE_SIZE (gnat_to_gnu_type
690 (Expression (Declaration_Node (gnat_entity)))));
693 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
698 /* If the size is zero bytes, make it one byte since some linkers have
699 trouble with zero-sized objects. If the object will have a
700 template, that will make it nonzero so don't bother. Also avoid
701 doing that for an object renaming or an object with an address
702 clause, as we would lose useful information on the view size
703 (e.g. for null array slices) and we are not allocating the object
706 && integer_zerop (gnu_size)
707 && !TREE_OVERFLOW (gnu_size))
708 || (TYPE_SIZE (gnu_type)
709 && integer_zerop (TYPE_SIZE (gnu_type))
710 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
711 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
712 || !Is_Array_Type (Etype (gnat_entity)))
713 && No (Renamed_Object (gnat_entity))
714 && No (Address_Clause (gnat_entity)))
715 gnu_size = bitsize_unit_node;
717 /* If this is an object with no specified size and alignment, and
718 if either it is atomic or we are not optimizing alignment for
719 space and it is composite and not an exception, an Out parameter
720 or a reference to another object, and the size of its type is a
721 constant, set the alignment to the smallest one which is not
722 smaller than the size, with an appropriate cap. */
723 if (!gnu_size && align == 0
724 && (Is_Atomic (gnat_entity)
725 || (!Optimize_Alignment_Space (gnat_entity)
726 && kind != E_Exception
727 && kind != E_Out_Parameter
728 && Is_Composite_Type (Etype (gnat_entity))
729 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
731 && No (Renamed_Object (gnat_entity))
732 && No (Address_Clause (gnat_entity))))
733 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
735 /* No point in jumping through all the hoops needed in order
736 to support BIGGEST_ALIGNMENT if we don't really have to.
737 So we cap to the smallest alignment that corresponds to
738 a known efficient memory access pattern of the target. */
739 unsigned int align_cap = Is_Atomic (gnat_entity)
741 : get_mode_alignment (ptr_mode);
743 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
744 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
747 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
749 /* But make sure not to under-align the object. */
750 if (align <= TYPE_ALIGN (gnu_type))
753 /* And honor the minimum valid atomic alignment, if any. */
754 #ifdef MINIMUM_ATOMIC_ALIGNMENT
755 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
756 align = MINIMUM_ATOMIC_ALIGNMENT;
760 /* If the object is set to have atomic components, find the component
761 type and validate it.
763 ??? Note that we ignore Has_Volatile_Components on objects; it's
764 not at all clear what to do in that case. */
766 if (Has_Atomic_Components (gnat_entity))
768 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
769 ? TREE_TYPE (gnu_type) : gnu_type);
771 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
772 && TYPE_MULTI_ARRAY_P (gnu_inner))
773 gnu_inner = TREE_TYPE (gnu_inner);
775 check_ok_for_atomic (gnu_inner, gnat_entity, true);
778 /* Now check if the type of the object allows atomic access. Note
779 that we must test the type, even if this object has size and
780 alignment to allow such access, because we will be going
781 inside the padded record to assign to the object. We could fix
782 this by always copying via an intermediate value, but it's not
783 clear it's worth the effort. */
784 if (Is_Atomic (gnat_entity))
785 check_ok_for_atomic (gnu_type, gnat_entity, false);
787 /* If this is an aliased object with an unconstrained nominal subtype,
788 make a type that includes the template. */
789 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
790 && Is_Array_Type (Etype (gnat_entity))
791 && !type_annotate_only)
794 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
797 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
798 concat_name (gnu_entity_name,
802 #ifdef MINIMUM_ATOMIC_ALIGNMENT
803 /* If the size is a constant and no alignment is specified, force
804 the alignment to be the minimum valid atomic alignment. The
805 restriction on constant size avoids problems with variable-size
806 temporaries; if the size is variable, there's no issue with
807 atomic access. Also don't do this for a constant, since it isn't
808 necessary and can interfere with constant replacement. Finally,
809 do not do it for Out parameters since that creates an
810 size inconsistency with In parameters. */
811 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
812 && !FLOAT_TYPE_P (gnu_type)
813 && !const_flag && No (Renamed_Object (gnat_entity))
814 && !imported_p && No (Address_Clause (gnat_entity))
815 && kind != E_Out_Parameter
816 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
817 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
818 align = MINIMUM_ATOMIC_ALIGNMENT;
821 /* Make a new type with the desired size and alignment, if needed.
822 But do not take into account alignment promotions to compute the
823 size of the object. */
824 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
825 if (gnu_size || align > 0)
826 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
827 "PAD", false, definition,
828 gnu_size ? true : false);
830 /* If this is a renaming, avoid as much as possible to create a new
831 object. However, in several cases, creating it is required.
832 This processing needs to be applied to the raw expression so
833 as to make it more likely to rename the underlying object. */
834 if (Present (Renamed_Object (gnat_entity)))
836 bool create_normal_object = false;
838 /* If the renamed object had padding, strip off the reference
839 to the inner object and reset our type. */
840 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
841 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
843 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
844 /* Strip useless conversions around the object. */
845 || (TREE_CODE (gnu_expr) == NOP_EXPR
846 && gnat_types_compatible_p
847 (TREE_TYPE (gnu_expr),
848 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
850 gnu_expr = TREE_OPERAND (gnu_expr, 0);
851 gnu_type = TREE_TYPE (gnu_expr);
854 /* Case 1: If this is a constant renaming stemming from a function
855 call, treat it as a normal object whose initial value is what
856 is being renamed. RM 3.3 says that the result of evaluating a
857 function call is a constant object. As a consequence, it can
858 be the inner object of a constant renaming. In this case, the
859 renaming must be fully instantiated, i.e. it cannot be a mere
860 reference to (part of) an existing object. */
863 tree inner_object = gnu_expr;
864 while (handled_component_p (inner_object))
865 inner_object = TREE_OPERAND (inner_object, 0);
866 if (TREE_CODE (inner_object) == CALL_EXPR)
867 create_normal_object = true;
870 /* Otherwise, see if we can proceed with a stabilized version of
871 the renamed entity or if we need to make a new object. */
872 if (!create_normal_object)
874 tree maybe_stable_expr = NULL_TREE;
877 /* Case 2: If the renaming entity need not be materialized and
878 the renamed expression is something we can stabilize, use
879 that for the renaming. At the global level, we can only do
880 this if we know no SAVE_EXPRs need be made, because the
881 expression we return might be used in arbitrary conditional
882 branches so we must force the SAVE_EXPRs evaluation
883 immediately and this requires a function context. */
884 if (!Materialize_Entity (gnat_entity)
885 && (!global_bindings_p ()
886 || (staticp (gnu_expr)
887 && !TREE_SIDE_EFFECTS (gnu_expr))))
890 = maybe_stabilize_reference (gnu_expr, true, &stable);
894 gnu_decl = maybe_stable_expr;
895 /* ??? No DECL_EXPR is created so we need to mark
896 the expression manually lest it is shared. */
897 if (global_bindings_p ())
898 mark_visited (&gnu_decl);
899 save_gnu_tree (gnat_entity, gnu_decl, true);
904 /* The stabilization failed. Keep maybe_stable_expr
905 untouched here to let the pointer case below know
906 about that failure. */
909 /* Case 3: If this is a constant renaming and creating a
910 new object is allowed and cheap, treat it as a normal
911 object whose initial value is what is being renamed. */
913 && !Is_Composite_Type
914 (Underlying_Type (Etype (gnat_entity))))
917 /* Case 4: Make this into a constant pointer to the object we
918 are to rename and attach the object to the pointer if it is
919 something we can stabilize.
921 From the proper scope, attached objects will be referenced
922 directly instead of indirectly via the pointer to avoid
923 subtle aliasing problems with non-addressable entities.
924 They have to be stable because we must not evaluate the
925 variables in the expression every time the renaming is used.
926 The pointer is called a "renaming" pointer in this case.
928 In the rare cases where we cannot stabilize the renamed
929 object, we just make a "bare" pointer, and the renamed
930 entity is always accessed indirectly through it. */
933 gnu_type = build_reference_type (gnu_type);
934 inner_const_flag = TREE_READONLY (gnu_expr);
937 /* If the previous attempt at stabilizing failed, there
938 is no point in trying again and we reuse the result
939 without attaching it to the pointer. In this case it
940 will only be used as the initializing expression of
941 the pointer and thus needs no special treatment with
942 regard to multiple evaluations. */
943 if (maybe_stable_expr)
946 /* Otherwise, try to stabilize and attach the expression
947 to the pointer if the stabilization succeeds.
949 Note that this might introduce SAVE_EXPRs and we don't
950 check whether we're at the global level or not. This
951 is fine since we are building a pointer initializer and
952 neither the pointer nor the initializing expression can
953 be accessed before the pointer elaboration has taken
954 place in a correct program.
956 These SAVE_EXPRs will be evaluated at the right place
957 by either the evaluation of the initializer for the
958 non-global case or the elaboration code for the global
959 case, and will be attached to the elaboration procedure
960 in the latter case. */
964 = maybe_stabilize_reference (gnu_expr, true, &stable);
967 renamed_obj = maybe_stable_expr;
969 /* Attaching is actually performed downstream, as soon
970 as we have a VAR_DECL for the pointer we make. */
974 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
976 gnu_size = NULL_TREE;
982 /* Make a volatile version of this object's type if we are to make
983 the object volatile. We also interpret 13.3(19) conservatively
984 and disallow any optimizations for such a non-constant object. */
985 if ((Treat_As_Volatile (gnat_entity)
987 && (Is_Exported (gnat_entity)
988 || Is_Imported (gnat_entity)
989 || Present (Address_Clause (gnat_entity)))))
990 && !TYPE_VOLATILE (gnu_type))
991 gnu_type = build_qualified_type (gnu_type,
992 (TYPE_QUALS (gnu_type)
993 | TYPE_QUAL_VOLATILE));
995 /* If we are defining an aliased object whose nominal subtype is
996 unconstrained, the object is a record that contains both the
997 template and the object. If there is an initializer, it will
998 have already been converted to the right type, but we need to
999 create the template if there is no initializer. */
1002 && TREE_CODE (gnu_type) == RECORD_TYPE
1003 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1004 /* Beware that padding might have been introduced
1005 via maybe_pad_type above. */
1006 || (TYPE_IS_PADDING_P (gnu_type)
1007 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1009 && TYPE_CONTAINS_TEMPLATE_P
1010 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1013 = TYPE_IS_PADDING_P (gnu_type)
1014 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1015 : TYPE_FIELDS (gnu_type);
1018 = gnat_build_constructor
1022 build_template (TREE_TYPE (template_field),
1023 TREE_TYPE (TREE_CHAIN (template_field)),
1028 /* Convert the expression to the type of the object except in the
1029 case where the object's type is unconstrained or the object's type
1030 is a padded record whose field is of self-referential size. In
1031 the former case, converting will generate unnecessary evaluations
1032 of the CONSTRUCTOR to compute the size and in the latter case, we
1033 want to only copy the actual data. */
1035 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1036 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1037 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1038 && TYPE_IS_PADDING_P (gnu_type)
1039 && (CONTAINS_PLACEHOLDER_P
1040 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1041 gnu_expr = convert (gnu_type, gnu_expr);
1043 /* If this is a pointer and it does not have an initializing
1044 expression, initialize it to NULL, unless the object is
1047 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1048 && !Is_Imported (gnat_entity) && !gnu_expr)
1049 gnu_expr = integer_zero_node;
1051 /* If we are defining the object and it has an Address clause, we must
1052 either get the address expression from the saved GCC tree for the
1053 object if it has a Freeze node, or elaborate the address expression
1054 here since the front-end has guaranteed that the elaboration has no
1055 effects in this case. */
1056 if (definition && Present (Address_Clause (gnat_entity)))
1059 = present_gnu_tree (gnat_entity)
1060 ? get_gnu_tree (gnat_entity)
1061 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1063 save_gnu_tree (gnat_entity, NULL_TREE, false);
1065 /* Ignore the size. It's either meaningless or was handled
1067 gnu_size = NULL_TREE;
1068 /* Convert the type of the object to a reference type that can
1069 alias everything as per 13.3(19). */
1071 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1072 gnu_address = convert (gnu_type, gnu_address);
1074 const_flag = !Is_Public (gnat_entity)
1075 || compile_time_known_address_p (Expression (Address_Clause
1078 /* If this is a deferred constant, the initializer is attached to
1080 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1083 (Expression (Declaration_Node (Full_View (gnat_entity))));
1085 /* If we don't have an initializing expression for the underlying
1086 variable, the initializing expression for the pointer is the
1087 specified address. Otherwise, we have to make a COMPOUND_EXPR
1088 to assign both the address and the initial value. */
1090 gnu_expr = gnu_address;
1093 = build2 (COMPOUND_EXPR, gnu_type,
1095 (MODIFY_EXPR, NULL_TREE,
1096 build_unary_op (INDIRECT_REF, NULL_TREE,
1102 /* If it has an address clause and we are not defining it, mark it
1103 as an indirect object. Likewise for Stdcall objects that are
1105 if ((!definition && Present (Address_Clause (gnat_entity)))
1106 || (Is_Imported (gnat_entity)
1107 && Has_Stdcall_Convention (gnat_entity)))
1109 /* Convert the type of the object to a reference type that can
1110 alias everything as per 13.3(19). */
1112 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1113 gnu_size = NULL_TREE;
1115 /* No point in taking the address of an initializing expression
1116 that isn't going to be used. */
1117 gnu_expr = NULL_TREE;
1119 /* If it has an address clause whose value is known at compile
1120 time, make the object a CONST_DECL. This will avoid a
1121 useless dereference. */
1122 if (Present (Address_Clause (gnat_entity)))
1124 Node_Id gnat_address
1125 = Expression (Address_Clause (gnat_entity));
1127 if (compile_time_known_address_p (gnat_address))
1129 gnu_expr = gnat_to_gnu (gnat_address);
1137 /* If we are at top level and this object is of variable size,
1138 make the actual type a hidden pointer to the real type and
1139 make the initializer be a memory allocation and initialization.
1140 Likewise for objects we aren't defining (presumed to be
1141 external references from other packages), but there we do
1142 not set up an initialization.
1144 If the object's size overflows, make an allocator too, so that
1145 Storage_Error gets raised. Note that we will never free
1146 such memory, so we presume it never will get allocated. */
1148 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1149 global_bindings_p () || !definition
1152 && ! allocatable_size_p (gnu_size,
1153 global_bindings_p () || !definition
1156 gnu_type = build_reference_type (gnu_type);
1157 gnu_size = NULL_TREE;
1161 /* In case this was a aliased object whose nominal subtype is
1162 unconstrained, the pointer above will be a thin pointer and
1163 build_allocator will automatically make the template.
1165 If we have a template initializer only (that we made above),
1166 pretend there is none and rely on what build_allocator creates
1167 again anyway. Otherwise (if we have a full initializer), get
1168 the data part and feed that to build_allocator.
1170 If we are elaborating a mutable object, tell build_allocator to
1171 ignore a possibly simpler size from the initializer, if any, as
1172 we must allocate the maximum possible size in this case. */
1176 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1178 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1179 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1182 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1184 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1185 && 1 == VEC_length (constructor_elt,
1186 CONSTRUCTOR_ELTS (gnu_expr)))
1190 = build_component_ref
1191 (gnu_expr, NULL_TREE,
1192 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1196 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1197 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1198 && !Is_Imported (gnat_entity))
1199 post_error ("?Storage_Error will be raised at run-time!",
1203 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1204 Empty, Empty, gnat_entity, mutable_p);
1208 gnu_expr = NULL_TREE;
1213 /* If this object would go into the stack and has an alignment larger
1214 than the largest stack alignment the back-end can honor, resort to
1215 a variable of "aligning type". */
1216 if (!global_bindings_p () && !static_p && definition
1217 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1219 /* Create the new variable. No need for extra room before the
1220 aligned field as this is in automatic storage. */
1222 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1223 TYPE_SIZE_UNIT (gnu_type),
1224 BIGGEST_ALIGNMENT, 0);
1226 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1227 NULL_TREE, gnu_new_type, NULL_TREE, false,
1228 false, false, false, NULL, gnat_entity);
1230 /* Initialize the aligned field if we have an initializer. */
1233 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1235 (gnu_new_var, NULL_TREE,
1236 TYPE_FIELDS (gnu_new_type), false),
1240 /* And setup this entity as a reference to the aligned field. */
1241 gnu_type = build_reference_type (gnu_type);
1244 (ADDR_EXPR, gnu_type,
1245 build_component_ref (gnu_new_var, NULL_TREE,
1246 TYPE_FIELDS (gnu_new_type), false));
1248 gnu_size = NULL_TREE;
1254 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1255 | TYPE_QUAL_CONST));
1257 /* Convert the expression to the type of the object except in the
1258 case where the object's type is unconstrained or the object's type
1259 is a padded record whose field is of self-referential size. In
1260 the former case, converting will generate unnecessary evaluations
1261 of the CONSTRUCTOR to compute the size and in the latter case, we
1262 want to only copy the actual data. */
1264 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1265 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1266 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1267 && TYPE_IS_PADDING_P (gnu_type)
1268 && (CONTAINS_PLACEHOLDER_P
1269 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1270 gnu_expr = convert (gnu_type, gnu_expr);
1272 /* If this name is external or there was a name specified, use it,
1273 unless this is a VMS exception object since this would conflict
1274 with the symbol we need to export in addition. Don't use the
1275 Interface_Name if there is an address clause (see CD30005). */
1276 if (!Is_VMS_Exception (gnat_entity)
1277 && ((Present (Interface_Name (gnat_entity))
1278 && No (Address_Clause (gnat_entity)))
1279 || (Is_Public (gnat_entity)
1280 && (!Is_Imported (gnat_entity)
1281 || Is_Exported (gnat_entity)))))
1282 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1284 /* If this is constant initialized to a static constant and the
1285 object has an aggregate type, force it to be statically
1286 allocated. This will avoid an initialization copy. */
1287 if (!static_p && const_flag
1288 && gnu_expr && TREE_CONSTANT (gnu_expr)
1289 && AGGREGATE_TYPE_P (gnu_type)
1290 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1291 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1292 && TYPE_IS_PADDING_P (gnu_type)
1293 && !host_integerp (TYPE_SIZE_UNIT
1294 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1297 gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1298 gnu_expr, const_flag,
1299 Is_Public (gnat_entity),
1300 imported_p || !definition,
1301 static_p, attr_list, gnat_entity);
1302 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1303 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1304 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1306 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1307 if (global_bindings_p ())
1309 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1310 record_global_renaming_pointer (gnu_decl);
1314 if (definition && DECL_SIZE_UNIT (gnu_decl)
1315 && get_block_jmpbuf_decl ()
1316 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1317 || (flag_stack_check == GENERIC_STACK_CHECK
1318 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1319 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1320 add_stmt_with_node (build_call_1_expr
1321 (update_setjmp_buf_decl,
1322 build_unary_op (ADDR_EXPR, NULL_TREE,
1323 get_block_jmpbuf_decl ())),
1326 /* If we are defining an Out parameter and we're not optimizing,
1327 create a fake PARM_DECL for debugging purposes and make it
1328 point to the VAR_DECL. Suppress debug info for the latter
1329 but make sure it will still live on the stack so it can be
1330 accessed from within the debugger through the PARM_DECL. */
1331 if (kind == E_Out_Parameter && definition && !optimize)
1333 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1334 gnat_pushdecl (param, gnat_entity);
1335 SET_DECL_VALUE_EXPR (param, gnu_decl);
1336 DECL_HAS_VALUE_EXPR_P (param) = 1;
1338 debug_info_p = false;
1340 DECL_IGNORED_P (param) = 1;
1341 TREE_ADDRESSABLE (gnu_decl) = 1;
1344 /* If this is a public constant or we're not optimizing and we're not
1345 making a VAR_DECL for it, make one just for export or debugger use.
1346 Likewise if the address is taken or if either the object or type is
1347 aliased. Make an external declaration for a reference, unless this
1348 is a Standard entity since there no real symbol at the object level
1350 if (TREE_CODE (gnu_decl) == CONST_DECL
1351 && (definition || Sloc (gnat_entity) > Standard_Location)
1352 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1354 || Address_Taken (gnat_entity)
1355 || Is_Aliased (gnat_entity)
1356 || Is_Aliased (Etype (gnat_entity))))
1359 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1360 gnu_expr, true, Is_Public (gnat_entity),
1361 !definition, static_p, NULL,
1364 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1366 /* As debugging information will be generated for the variable,
1367 do not generate information for the constant. */
1368 DECL_IGNORED_P (gnu_decl) = 1;
1371 /* If this is declared in a block that contains a block with an
1372 exception handler, we must force this variable in memory to
1373 suppress an invalid optimization. */
1374 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1375 && Exception_Mechanism != Back_End_Exceptions)
1376 TREE_ADDRESSABLE (gnu_decl) = 1;
1378 gnu_type = TREE_TYPE (gnu_decl);
1380 /* Back-annotate Alignment and Esize of the object if not already
1381 known, except for when the object is actually a pointer to the
1382 real object, since alignment and size of a pointer don't have
1383 anything to do with those of the designated object. Note that
1384 we pick the values of the type, not those of the object, to
1385 shield ourselves from low-level platform-dependent adjustments
1386 like alignment promotion. This is both consistent with all the
1387 treatment above, where alignment and size are set on the type of
1388 the object and not on the object directly, and makes it possible
1389 to support confirming representation clauses in all cases. */
1391 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1392 Set_Alignment (gnat_entity,
1393 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1395 if (!used_by_ref && Unknown_Esize (gnat_entity))
1397 if (TREE_CODE (gnu_type) == RECORD_TYPE
1398 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1400 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1402 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1408 /* Return a TYPE_DECL for "void" that we previously made. */
1409 gnu_decl = TYPE_NAME (void_type_node);
1412 case E_Enumeration_Type:
1413 /* A special case: for the types Character and Wide_Character in
1414 Standard, we do not list all the literals. So if the literals
1415 are not specified, make this an unsigned type. */
1416 if (No (First_Literal (gnat_entity)))
1418 gnu_type = make_unsigned_type (esize);
1419 TYPE_NAME (gnu_type) = gnu_entity_name;
1421 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1422 This is needed by the DWARF-2 back-end to distinguish between
1423 unsigned integer types and character types. */
1424 TYPE_STRING_FLAG (gnu_type) = 1;
1428 /* Normal case of non-character type or non-Standard character type. */
1430 /* Here we have a list of enumeral constants in First_Literal.
1431 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1432 the list to be placed into TYPE_FIELDS. Each node in the list
1433 is a TREE_LIST whose TREE_VALUE is the literal name and whose
1434 TREE_PURPOSE is the value of the literal. */
1436 Entity_Id gnat_literal;
1437 tree gnu_literal_list = NULL_TREE;
1439 if (Is_Unsigned_Type (gnat_entity))
1440 gnu_type = make_unsigned_type (esize);
1442 gnu_type = make_signed_type (esize);
1444 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1446 for (gnat_literal = First_Literal (gnat_entity);
1447 Present (gnat_literal);
1448 gnat_literal = Next_Literal (gnat_literal))
1450 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1453 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1454 gnu_type, gnu_value, true, false, false,
1455 false, NULL, gnat_literal);
1457 save_gnu_tree (gnat_literal, gnu_literal, false);
1458 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1459 gnu_value, gnu_literal_list);
1462 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1464 /* Note that the bounds are updated at the end of this function
1465 to avoid an infinite recursion since they refer to the type. */
1469 case E_Signed_Integer_Type:
1470 case E_Ordinary_Fixed_Point_Type:
1471 case E_Decimal_Fixed_Point_Type:
1472 /* For integer types, just make a signed type the appropriate number
1474 gnu_type = make_signed_type (esize);
1477 case E_Modular_Integer_Type:
1479 /* For modular types, make the unsigned type of the proper number
1480 of bits and then set up the modulus, if required. */
1481 tree gnu_modulus, gnu_high = NULL_TREE;
1483 /* Packed array types are supposed to be subtypes only. */
1484 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1486 gnu_type = make_unsigned_type (esize);
1488 /* Get the modulus in this type. If it overflows, assume it is because
1489 it is equal to 2**Esize. Note that there is no overflow checking
1490 done on unsigned type, so we detect the overflow by looking for
1491 a modulus of zero, which is otherwise invalid. */
1492 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1494 if (!integer_zerop (gnu_modulus))
1496 TYPE_MODULAR_P (gnu_type) = 1;
1497 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1498 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1499 convert (gnu_type, integer_one_node));
1502 /* If the upper bound is not maximal, make an extra subtype. */
1504 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1506 tree gnu_subtype = make_unsigned_type (esize);
1507 TYPE_MAX_VALUE (gnu_subtype) = gnu_high;
1508 TREE_TYPE (gnu_subtype) = gnu_type;
1509 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1510 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1511 gnu_type = gnu_subtype;
1516 case E_Signed_Integer_Subtype:
1517 case E_Enumeration_Subtype:
1518 case E_Modular_Integer_Subtype:
1519 case E_Ordinary_Fixed_Point_Subtype:
1520 case E_Decimal_Fixed_Point_Subtype:
1522 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1523 not want to call build_range_type since we would like each subtype
1524 node to be distinct. ??? Historically this was in preparation for
1525 when memory aliasing is implemented, but that's obsolete now given
1526 the call to relate_alias_sets below.
1528 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1529 this fact is used by the arithmetic conversion functions.
1531 We elaborate the Ancestor_Subtype if it is not in the current unit
1532 and one of our bounds is non-static. We do this to ensure consistent
1533 naming in the case where several subtypes share the same bounds, by
1534 elaborating the first such subtype first, thus using its name. */
1537 && Present (Ancestor_Subtype (gnat_entity))
1538 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1539 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1540 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1541 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1543 gnu_type = make_node (INTEGER_TYPE);
1544 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1546 /* This should be an unsigned type if the base type is unsigned or
1547 if the lower bound is constant and non-negative or if the type
1549 TYPE_UNSIGNED (gnu_type) = (Is_Unsigned_Type (Etype (gnat_entity))
1550 || Is_Unsigned_Type (gnat_entity)
1551 || Has_Biased_Representation (gnat_entity));
1553 /* Set the precision to the Esize except for bit-packed arrays and
1554 subtypes of Standard.Boolean. */
1555 if (Is_Packed_Array_Type (gnat_entity)
1556 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1557 esize = UI_To_Int (RM_Size (gnat_entity));
1558 else if (Is_Boolean_Type (gnat_entity))
1561 TYPE_PRECISION (gnu_type) = esize;
1563 TYPE_MIN_VALUE (gnu_type)
1564 = convert (TREE_TYPE (gnu_type),
1565 elaborate_expression (Type_Low_Bound (gnat_entity),
1567 get_identifier ("L"), definition, 1,
1568 Needs_Debug_Info (gnat_entity)));
1570 TYPE_MAX_VALUE (gnu_type)
1571 = convert (TREE_TYPE (gnu_type),
1572 elaborate_expression (Type_High_Bound (gnat_entity),
1574 get_identifier ("U"), definition, 1,
1575 Needs_Debug_Info (gnat_entity)));
1577 /* One of the above calls might have caused us to be elaborated,
1578 so don't blow up if so. */
1579 if (present_gnu_tree (gnat_entity))
1581 maybe_present = true;
1585 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1586 = Has_Biased_Representation (gnat_entity);
1588 layout_type (gnu_type);
1590 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1591 TYPE_STUB_DECL (gnu_type)
1592 = create_type_stub_decl (gnu_entity_name, gnu_type);
1594 /* Inherit our alias set from what we're a subtype of. Subtypes
1595 are not different types and a pointer can designate any instance
1596 within a subtype hierarchy. */
1597 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1599 /* For a packed array, make the original array type a parallel type. */
1601 && Is_Packed_Array_Type (gnat_entity)
1602 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1603 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1605 (Original_Array_Type (gnat_entity)));
1607 /* If the type we are dealing with represents a bit-packed array,
1608 we need to have the bits left justified on big-endian targets
1609 and right justified on little-endian targets. We also need to
1610 ensure that when the value is read (e.g. for comparison of two
1611 such values), we only get the good bits, since the unused bits
1612 are uninitialized. Both goals are accomplished by wrapping up
1613 the modular type in an enclosing record type. */
1614 if (Is_Packed_Array_Type (gnat_entity)
1615 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1617 tree gnu_field_type, gnu_field;
1619 /* Set the RM size before wrapping up the type. */
1620 TYPE_RM_SIZE (gnu_type)
1621 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1622 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1623 gnu_field_type = gnu_type;
1625 gnu_type = make_node (RECORD_TYPE);
1626 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1628 /* Propagate the alignment of the modular type to the record.
1629 This means that bit-packed arrays have "ceil" alignment for
1630 their size, which may seem counter-intuitive but makes it
1631 possible to easily overlay them on modular types. */
1632 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1633 TYPE_PACKED (gnu_type) = 1;
1635 /* Create a stripped-down declaration of the original type, mainly
1637 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1638 debug_info_p, gnat_entity);
1640 /* Don't notify the field as "addressable", since we won't be taking
1641 it's address and it would prevent create_field_decl from making a
1643 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1644 gnu_field_type, gnu_type, 1, 0, 0, 0);
1646 /* Do not finalize it until after the parallel type is added. */
1647 finish_record_type (gnu_type, gnu_field, 0, true);
1648 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1650 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1652 /* Make the original array type a parallel type. */
1654 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1655 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1657 (Original_Array_Type (gnat_entity)));
1659 rest_of_record_type_compilation (gnu_type);
1662 /* If the type we are dealing with has got a smaller alignment than the
1663 natural one, we need to wrap it up in a record type and under-align
1664 the latter. We reuse the padding machinery for this purpose. */
1665 else if (Present (Alignment_Clause (gnat_entity))
1666 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1667 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1668 && align < TYPE_ALIGN (gnu_type))
1670 tree gnu_field_type, gnu_field;
1672 /* Set the RM size before wrapping up the type. */
1673 TYPE_RM_SIZE (gnu_type)
1674 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1675 gnu_field_type = gnu_type;
1677 gnu_type = make_node (RECORD_TYPE);
1678 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1680 TYPE_ALIGN (gnu_type) = align;
1681 TYPE_PACKED (gnu_type) = 1;
1683 /* Create a stripped-down declaration of the original type, mainly
1685 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1686 debug_info_p, gnat_entity);
1688 /* Don't notify the field as "addressable", since we won't be taking
1689 it's address and it would prevent create_field_decl from making a
1691 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1692 gnu_field_type, gnu_type, 1, 0, 0, 0);
1694 finish_record_type (gnu_type, gnu_field, 0, false);
1695 TYPE_IS_PADDING_P (gnu_type) = 1;
1697 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1700 /* Otherwise reset the alignment lest we computed it above. */
1706 case E_Floating_Point_Type:
1707 /* If this is a VAX floating-point type, use an integer of the proper
1708 size. All the operations will be handled with ASM statements. */
1709 if (Vax_Float (gnat_entity))
1711 gnu_type = make_signed_type (esize);
1712 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1713 SET_TYPE_DIGITS_VALUE (gnu_type,
1714 UI_To_gnu (Digits_Value (gnat_entity),
1719 /* The type of the Low and High bounds can be our type if this is
1720 a type from Standard, so set them at the end of the function. */
1721 gnu_type = make_node (REAL_TYPE);
1722 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1723 layout_type (gnu_type);
1726 case E_Floating_Point_Subtype:
1727 if (Vax_Float (gnat_entity))
1729 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1735 && Present (Ancestor_Subtype (gnat_entity))
1736 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1737 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1738 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1739 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1742 gnu_type = make_node (REAL_TYPE);
1743 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1744 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1746 TYPE_MIN_VALUE (gnu_type)
1747 = convert (TREE_TYPE (gnu_type),
1748 elaborate_expression (Type_Low_Bound (gnat_entity),
1749 gnat_entity, get_identifier ("L"),
1751 Needs_Debug_Info (gnat_entity)));
1753 TYPE_MAX_VALUE (gnu_type)
1754 = convert (TREE_TYPE (gnu_type),
1755 elaborate_expression (Type_High_Bound (gnat_entity),
1756 gnat_entity, get_identifier ("U"),
1758 Needs_Debug_Info (gnat_entity)));
1760 /* One of the above calls might have caused us to be elaborated,
1761 so don't blow up if so. */
1762 if (present_gnu_tree (gnat_entity))
1764 maybe_present = true;
1768 layout_type (gnu_type);
1770 /* Inherit our alias set from what we're a subtype of, as for
1771 integer subtypes. */
1772 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1776 /* Array and String Types and Subtypes
1778 Unconstrained array types are represented by E_Array_Type and
1779 constrained array types are represented by E_Array_Subtype. There
1780 are no actual objects of an unconstrained array type; all we have
1781 are pointers to that type.
1783 The following fields are defined on array types and subtypes:
1785 Component_Type Component type of the array.
1786 Number_Dimensions Number of dimensions (an int).
1787 First_Index Type of first index. */
1792 Entity_Id gnat_ind_subtype;
1793 Entity_Id gnat_ind_base_subtype;
1794 int ndim = Number_Dimensions (gnat_entity);
1796 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1798 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1800 tree gnu_template_fields = NULL_TREE;
1801 tree gnu_template_type = make_node (RECORD_TYPE);
1802 tree gnu_template_reference;
1803 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1804 tree gnu_fat_type = make_node (RECORD_TYPE);
1805 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1806 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1807 tree gnu_max_size = size_one_node, gnu_max_size_unit;
1808 tree gnu_comp_size, tem;
1810 TYPE_NAME (gnu_template_type)
1811 = create_concat_name (gnat_entity, "XUB");
1813 /* Make a node for the array. If we are not defining the array
1814 suppress expanding incomplete types. */
1815 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1818 defer_incomplete_level++, this_deferred = true;
1820 /* Build the fat pointer type. Use a "void *" object instead of
1821 a pointer to the array type since we don't have the array type
1822 yet (it will reference the fat pointer via the bounds). */
1823 tem = chainon (chainon (NULL_TREE,
1824 create_field_decl (get_identifier ("P_ARRAY"),
1826 gnu_fat_type, 0, 0, 0, 0)),
1827 create_field_decl (get_identifier ("P_BOUNDS"),
1829 gnu_fat_type, 0, 0, 0, 0));
1831 /* Make sure we can put this into a register. */
1832 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1834 /* Do not finalize this record type since the types of its fields
1835 are still incomplete at this point. */
1836 finish_record_type (gnu_fat_type, tem, 0, true);
1837 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1839 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1840 is the fat pointer. This will be used to access the individual
1841 fields once we build them. */
1842 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1843 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1844 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1845 gnu_template_reference
1846 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1847 TREE_READONLY (gnu_template_reference) = 1;
1849 /* Now create the GCC type for each index and add the fields for
1850 that index to the template. */
1851 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1852 gnat_ind_base_subtype
1853 = First_Index (Implementation_Base_Type (gnat_entity));
1854 index < ndim && index >= 0;
1856 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1857 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1859 char field_name[10];
1860 tree gnu_ind_subtype
1861 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1862 tree gnu_base_subtype
1863 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1865 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1867 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1868 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1870 /* Make the FIELD_DECLs for the minimum and maximum of this
1871 type and then make extractions of that field from the
1873 sprintf (field_name, "LB%d", index);
1874 gnu_min_field = create_field_decl (get_identifier (field_name),
1876 gnu_template_type, 0, 0, 0, 0);
1877 field_name[0] = 'U';
1878 gnu_max_field = create_field_decl (get_identifier (field_name),
1880 gnu_template_type, 0, 0, 0, 0);
1882 Sloc_to_locus (Sloc (gnat_entity),
1883 &DECL_SOURCE_LOCATION (gnu_min_field));
1884 Sloc_to_locus (Sloc (gnat_entity),
1885 &DECL_SOURCE_LOCATION (gnu_max_field));
1886 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1888 /* We can't use build_component_ref here since the template
1889 type isn't complete yet. */
1890 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1891 gnu_template_reference, gnu_min_field,
1893 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1894 gnu_template_reference, gnu_max_field,
1896 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1898 /* Make a range type with the new ranges, but using
1899 the Ada subtype. Then we convert to sizetype. */
1900 gnu_index_types[index]
1901 = create_index_type (convert (sizetype, gnu_min),
1902 convert (sizetype, gnu_max),
1903 build_range_type (gnu_ind_subtype,
1906 /* Update the maximum size of the array, in elements. */
1908 = size_binop (MULT_EXPR, gnu_max_size,
1909 size_binop (PLUS_EXPR, size_one_node,
1910 size_binop (MINUS_EXPR, gnu_base_max,
1913 TYPE_NAME (gnu_index_types[index])
1914 = create_concat_name (gnat_entity, field_name);
1917 for (index = 0; index < ndim; index++)
1919 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1921 /* Install all the fields into the template. */
1922 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1923 TYPE_READONLY (gnu_template_type) = 1;
1925 /* Now make the array of arrays and update the pointer to the array
1926 in the fat pointer. Note that it is the first field. */
1927 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1929 /* Try to get a smaller form of the component if needed. */
1930 if ((Is_Packed (gnat_entity)
1931 || Has_Component_Size_Clause (gnat_entity))
1932 && !Is_Bit_Packed_Array (gnat_entity)
1933 && !Has_Aliased_Components (gnat_entity)
1934 && !Strict_Alignment (Component_Type (gnat_entity))
1935 && TREE_CODE (tem) == RECORD_TYPE
1936 && !TYPE_IS_FAT_POINTER_P (tem)
1937 && host_integerp (TYPE_SIZE (tem), 1))
1938 tem = make_packable_type (tem, false);
1940 if (Has_Atomic_Components (gnat_entity))
1941 check_ok_for_atomic (tem, gnat_entity, true);
1943 /* Get and validate any specified Component_Size, but if Packed,
1944 ignore it since the front end will have taken care of it. */
1946 = validate_size (Component_Size (gnat_entity), tem,
1948 (Is_Bit_Packed_Array (gnat_entity)
1949 ? TYPE_DECL : VAR_DECL),
1950 true, Has_Component_Size_Clause (gnat_entity));
1952 /* If the component type is a RECORD_TYPE that has a self-referential
1953 size, use the maximum size. */
1955 && TREE_CODE (tem) == RECORD_TYPE
1956 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1957 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1959 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1961 tree orig_tem = tem;
1962 unsigned int max_align;
1964 /* If an alignment is specified, use it as a cap on the component
1965 type so that it can be honored for the whole type. But ignore
1966 it for the original type of packed array types. */
1967 if (No (Packed_Array_Type (gnat_entity))
1968 && Known_Alignment (gnat_entity))
1969 max_align = validate_alignment (Alignment (gnat_entity),
1974 tem = make_type_from_size (tem, gnu_comp_size, false);
1975 if (max_align > 0 && TYPE_ALIGN (tem) > max_align)
1980 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1981 "C_PAD", false, definition, true);
1983 /* If a padding record was made, declare it now since it will
1984 never be declared otherwise. This is necessary to ensure
1985 that its subtrees are properly marked. */
1986 if (tem != orig_tem)
1987 create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1988 debug_info_p, gnat_entity);
1991 if (Has_Volatile_Components (gnat_entity))
1992 tem = build_qualified_type (tem,
1993 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1995 /* If Component_Size is not already specified, annotate it with the
1996 size of the component. */
1997 if (Unknown_Component_Size (gnat_entity))
1998 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2000 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
2001 size_binop (MULT_EXPR, gnu_max_size,
2002 TYPE_SIZE_UNIT (tem)));
2003 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
2004 size_binop (MULT_EXPR,
2005 convert (bitsizetype,
2009 for (index = ndim - 1; index >= 0; index--)
2011 tem = build_array_type (tem, gnu_index_types[index]);
2012 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2013 if (array_type_has_nonaliased_component (gnat_entity, tem))
2014 TYPE_NONALIASED_COMPONENT (tem) = 1;
2017 /* If an alignment is specified, use it if valid. But ignore it
2018 for the original type of packed array types. If the alignment
2019 was requested with an explicit alignment clause, state so. */
2020 if (No (Packed_Array_Type (gnat_entity))
2021 && Known_Alignment (gnat_entity))
2024 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2026 if (Present (Alignment_Clause (gnat_entity)))
2027 TYPE_USER_ALIGN (tem) = 1;
2030 TYPE_CONVENTION_FORTRAN_P (tem)
2031 = (Convention (gnat_entity) == Convention_Fortran);
2032 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2034 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2035 corresponding fat pointer. */
2036 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2037 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2038 SET_TYPE_MODE (gnu_type, BLKmode);
2039 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2040 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2042 /* If the maximum size doesn't overflow, use it. */
2043 if (TREE_CODE (gnu_max_size) == INTEGER_CST
2044 && !TREE_OVERFLOW (gnu_max_size))
2046 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
2047 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2048 && !TREE_OVERFLOW (gnu_max_size_unit))
2049 TYPE_SIZE_UNIT (tem)
2050 = size_binop (MIN_EXPR, gnu_max_size_unit,
2051 TYPE_SIZE_UNIT (tem));
2053 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2054 tem, NULL, !Comes_From_Source (gnat_entity),
2055 debug_info_p, gnat_entity);
2057 /* Give the fat pointer type a name. */
2058 create_type_decl (create_concat_name (gnat_entity, "XUP"),
2059 gnu_fat_type, NULL, true,
2060 debug_info_p, gnat_entity);
2062 /* Create the type to be used as what a thin pointer designates: an
2063 record type for the object and its template with the field offsets
2064 shifted to have the template at a negative offset. */
2065 tem = build_unc_object_type (gnu_template_type, tem,
2066 create_concat_name (gnat_entity, "XUT"));
2067 shift_unc_components_for_thin_pointers (tem);
2069 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2070 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2072 /* Give the thin pointer type a name. */
2073 create_type_decl (create_concat_name (gnat_entity, "XUX"),
2074 build_pointer_type (tem), NULL, true,
2075 debug_info_p, gnat_entity);
2079 case E_String_Subtype:
2080 case E_Array_Subtype:
2082 /* This is the actual data type for array variables. Multidimensional
2083 arrays are implemented in the gnu tree as arrays of arrays. Note
2084 that for the moment arrays which have sparse enumeration subtypes as
2085 index components create sparse arrays, which is obviously space
2086 inefficient but so much easier to code for now.
2088 Also note that the subtype never refers to the unconstrained
2089 array type, which is somewhat at variance with Ada semantics.
2091 First check to see if this is simply a renaming of the array
2092 type. If so, the result is the array type. */
2094 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2095 if (!Is_Constrained (gnat_entity))
2099 Entity_Id gnat_ind_subtype;
2100 Entity_Id gnat_ind_base_subtype;
2101 int dim = Number_Dimensions (gnat_entity);
2103 = (Convention (gnat_entity) == Convention_Fortran) ? dim - 1 : 0;
2105 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2107 tree gnu_base_type = gnu_type;
2108 tree *gnu_index_type = (tree *) alloca (dim * sizeof (tree));
2109 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2110 bool need_index_type_struct = false;
2111 bool max_overflow = false;
2113 /* First create the gnu types for each index. Create types for
2114 debugging information to point to the index types if the
2115 are not integer types, have variable bounds, or are
2116 wider than sizetype. */
2118 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2119 gnat_ind_base_subtype
2120 = First_Index (Implementation_Base_Type (gnat_entity));
2121 index < dim && index >= 0;
2123 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2124 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2126 tree gnu_index_subtype
2127 = get_unpadded_type (Etype (gnat_ind_subtype));
2129 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2131 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2132 tree gnu_base_subtype
2133 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2135 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2137 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2138 tree gnu_base_type = get_base_type (gnu_base_subtype);
2139 tree gnu_base_base_min
2140 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2141 tree gnu_base_base_max
2142 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2146 /* If the minimum and maximum values both overflow in
2147 SIZETYPE, but the difference in the original type
2148 does not overflow in SIZETYPE, ignore the overflow
2150 if ((TYPE_PRECISION (gnu_index_subtype)
2151 > TYPE_PRECISION (sizetype)
2152 || TYPE_UNSIGNED (gnu_index_subtype)
2153 != TYPE_UNSIGNED (sizetype))
2154 && TREE_CODE (gnu_min) == INTEGER_CST
2155 && TREE_CODE (gnu_max) == INTEGER_CST
2156 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2158 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2159 TYPE_MAX_VALUE (gnu_index_subtype),
2160 TYPE_MIN_VALUE (gnu_index_subtype))))
2162 TREE_OVERFLOW (gnu_min) = 0;
2163 TREE_OVERFLOW (gnu_max) = 0;
2164 if (tree_int_cst_lt (gnu_max, gnu_min))
2166 gnu_min = size_one_node;
2167 gnu_max = size_zero_node;
2172 /* Similarly, if the range is null, use bounds of 1..0 for
2173 the sizetype bounds. */
2174 else if ((TYPE_PRECISION (gnu_index_subtype)
2175 > TYPE_PRECISION (sizetype)
2176 || TYPE_UNSIGNED (gnu_index_subtype)
2177 != TYPE_UNSIGNED (sizetype))
2178 && TREE_CODE (gnu_min) == INTEGER_CST
2179 && TREE_CODE (gnu_max) == INTEGER_CST
2180 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2181 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2182 TYPE_MIN_VALUE (gnu_index_subtype)))
2184 gnu_min = size_one_node;
2185 gnu_max = size_zero_node;
2189 /* See if the base array type is already flat. If it is, we
2190 are probably compiling an ACATS test, but it will cause the
2191 code below to malfunction if we don't handle it specially. */
2192 else if (TREE_CODE (gnu_base_min) == INTEGER_CST
2193 && TREE_CODE (gnu_base_max) == INTEGER_CST
2194 && !TREE_OVERFLOW (gnu_base_min)
2195 && !TREE_OVERFLOW (gnu_base_max)
2196 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2198 gnu_min = size_one_node;
2199 gnu_max = size_zero_node;
2205 /* Now compute the size of this bound. We need to provide
2206 GCC with an upper bound to use but have to deal with the
2207 "superflat" case. There are three ways to do this. If
2208 we can prove that the array can never be superflat, we
2209 can just use the high bound of the index subtype. If we
2210 can prove that the low bound minus one can't overflow,
2211 we can do this as MAX (hb, lb - 1). Otherwise, we have
2212 to use the expression hb >= lb ? hb : lb - 1. */
2213 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2215 /* If gnu_high is now an integer which overflowed, the array
2216 cannot be superflat. */
2217 if (TREE_CODE (gnu_high) == INTEGER_CST
2218 && TREE_OVERFLOW (gnu_high))
2221 /* gnu_high cannot overflow if the subtype is unsigned since
2222 sizetype is signed, or if it is now a constant that hasn't
2224 else if (TYPE_UNSIGNED (gnu_base_subtype)
2225 || TREE_CODE (gnu_high) == INTEGER_CST)
2226 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2230 = build_cond_expr (sizetype,
2231 build_binary_op (GE_EXPR,
2237 gnu_index_type[index]
2238 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2241 /* Also compute the maximum size of the array. Here we
2242 see if any constraint on the index type of the base type
2243 can be used in the case of self-referential bound on
2244 the index type of the subtype. We look for a non-"infinite"
2245 and non-self-referential bound from any type involved and
2246 handle each bound separately. */
2248 if ((TREE_CODE (gnu_min) == INTEGER_CST
2249 && !TREE_OVERFLOW (gnu_min)
2250 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2251 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2252 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2253 && !TREE_OVERFLOW (gnu_base_min)))
2254 gnu_base_min = gnu_min;
2256 if ((TREE_CODE (gnu_max) == INTEGER_CST
2257 && !TREE_OVERFLOW (gnu_max)
2258 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2259 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2260 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2261 && !TREE_OVERFLOW (gnu_base_max)))
2262 gnu_base_max = gnu_max;
2264 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2265 && TREE_OVERFLOW (gnu_base_min))
2266 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2267 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2268 && TREE_OVERFLOW (gnu_base_max))
2269 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2270 max_overflow = true;
2272 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2273 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2276 = size_binop (MAX_EXPR,
2277 size_binop (PLUS_EXPR, size_one_node,
2278 size_binop (MINUS_EXPR, gnu_base_max,
2282 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2283 && TREE_OVERFLOW (gnu_this_max))
2284 max_overflow = true;
2287 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2289 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2290 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2292 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2293 || (TREE_TYPE (gnu_index_subtype)
2294 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2296 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2297 || (TYPE_PRECISION (gnu_index_subtype)
2298 > TYPE_PRECISION (sizetype)))
2299 need_index_type_struct = true;
2302 /* Then flatten: create the array of arrays. For an array type
2303 used to implement a packed array, get the component type from
2304 the original array type since the representation clauses that
2305 can affect it are on the latter. */
2306 if (Is_Packed_Array_Type (gnat_entity)
2307 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2309 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2310 for (index = dim - 1; index >= 0; index--)
2311 gnu_type = TREE_TYPE (gnu_type);
2313 /* One of the above calls might have caused us to be elaborated,
2314 so don't blow up if so. */
2315 if (present_gnu_tree (gnat_entity))
2317 maybe_present = true;
2325 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2327 /* One of the above calls might have caused us to be elaborated,
2328 so don't blow up if so. */
2329 if (present_gnu_tree (gnat_entity))
2331 maybe_present = true;
2335 /* Try to get a smaller form of the component if needed. */
2336 if ((Is_Packed (gnat_entity)
2337 || Has_Component_Size_Clause (gnat_entity))
2338 && !Is_Bit_Packed_Array (gnat_entity)
2339 && !Has_Aliased_Components (gnat_entity)
2340 && !Strict_Alignment (Component_Type (gnat_entity))
2341 && TREE_CODE (gnu_type) == RECORD_TYPE
2342 && !TYPE_IS_FAT_POINTER_P (gnu_type)
2343 && host_integerp (TYPE_SIZE (gnu_type), 1))
2344 gnu_type = make_packable_type (gnu_type, false);
2346 /* Get and validate any specified Component_Size, but if Packed,
2347 ignore it since the front end will have taken care of it. */
2349 = validate_size (Component_Size (gnat_entity), gnu_type,
2351 (Is_Bit_Packed_Array (gnat_entity)
2352 ? TYPE_DECL : VAR_DECL), true,
2353 Has_Component_Size_Clause (gnat_entity));
2355 /* If the component type is a RECORD_TYPE that has a
2356 self-referential size, use the maximum size. */
2358 && TREE_CODE (gnu_type) == RECORD_TYPE
2359 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2360 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2362 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2364 tree orig_gnu_type = gnu_type;
2365 unsigned int max_align;
2367 /* If an alignment is specified, use it as a cap on the
2368 component type so that it can be honored for the whole
2369 type. But ignore it for the original type of packed
2371 if (No (Packed_Array_Type (gnat_entity))
2372 && Known_Alignment (gnat_entity))
2373 max_align = validate_alignment (Alignment (gnat_entity),
2379 = make_type_from_size (gnu_type, gnu_comp_size, false);
2380 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
2381 gnu_type = orig_gnu_type;
2383 orig_gnu_type = gnu_type;
2385 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2386 gnat_entity, "C_PAD", false,
2389 /* If a padding record was made, declare it now since it
2390 will never be declared otherwise. This is necessary
2391 to ensure that its subtrees are properly marked. */
2392 if (gnu_type != orig_gnu_type)
2393 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2394 true, debug_info_p, gnat_entity);
2397 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2398 gnu_type = build_qualified_type (gnu_type,
2399 (TYPE_QUALS (gnu_type)
2400 | TYPE_QUAL_VOLATILE));
2403 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2404 TYPE_SIZE_UNIT (gnu_type));
2405 gnu_max_size = size_binop (MULT_EXPR,
2406 convert (bitsizetype, gnu_max_size),
2407 TYPE_SIZE (gnu_type));
2409 for (index = dim - 1; index >= 0; index --)
2411 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2412 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2413 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2414 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2417 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2418 TYPE_STUB_DECL (gnu_type)
2419 = create_type_stub_decl (gnu_entity_name, gnu_type);
2421 /* If we are at file level and this is a multi-dimensional array, we
2422 need to make a variable corresponding to the stride of the
2423 inner dimensions. */
2424 if (global_bindings_p () && dim > 1)
2426 tree gnu_str_name = get_identifier ("ST");
2429 for (gnu_arr_type = TREE_TYPE (gnu_type);
2430 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2431 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2432 gnu_str_name = concat_name (gnu_str_name, "ST"))
2434 tree eltype = TREE_TYPE (gnu_arr_type);
2436 TYPE_SIZE (gnu_arr_type)
2437 = elaborate_expression_1 (gnat_entity, gnat_entity,
2438 TYPE_SIZE (gnu_arr_type),
2439 gnu_str_name, definition, 0);
2441 /* ??? For now, store the size as a multiple of the
2442 alignment of the element type in bytes so that we
2443 can see the alignment from the tree. */
2444 TYPE_SIZE_UNIT (gnu_arr_type)
2446 (MULT_EXPR, sizetype,
2447 elaborate_expression_1
2448 (gnat_entity, gnat_entity,
2449 build_binary_op (EXACT_DIV_EXPR, sizetype,
2450 TYPE_SIZE_UNIT (gnu_arr_type),
2451 size_int (TYPE_ALIGN (eltype)
2453 concat_name (gnu_str_name, "A_U"), definition, 0),
2454 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2456 /* ??? create_type_decl is not invoked on the inner types so
2457 the MULT_EXPR node built above will never be marked. */
2458 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2462 /* If we need to write out a record type giving the names of the
2463 bounds for debugging purposes, do it now and make the record
2464 type a parallel type. This is not needed for a packed array
2465 since the bounds are conveyed by the original array type. */
2466 if (need_index_type_struct
2468 && !Is_Packed_Array_Type (gnat_entity))
2470 tree gnu_bound_rec = make_node (RECORD_TYPE);
2471 tree gnu_field_list = NULL_TREE;
2474 TYPE_NAME (gnu_bound_rec)
2475 = create_concat_name (gnat_entity, "XA");
2477 for (index = dim - 1; index >= 0; index--)
2479 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_type[index]);
2480 tree gnu_index_name = TYPE_NAME (gnu_index);
2482 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2483 gnu_index_name = DECL_NAME (gnu_index_name);
2485 /* Make sure to reference the types themselves, and not just
2486 their names, as the debugger may fall back on them. */
2487 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2489 0, NULL_TREE, NULL_TREE, 0);
2490 TREE_CHAIN (gnu_field) = gnu_field_list;
2491 gnu_field_list = gnu_field;
2494 finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
2495 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2498 /* Otherwise, for a packed array, make the original array type a
2500 else if (debug_info_p
2501 && Is_Packed_Array_Type (gnat_entity)
2502 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2503 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2505 (Original_Array_Type (gnat_entity)));
2507 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2508 = (Convention (gnat_entity) == Convention_Fortran);
2509 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2510 = (Is_Packed_Array_Type (gnat_entity)
2511 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2513 /* If our size depends on a placeholder and the maximum size doesn't
2514 overflow, use it. */
2515 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2516 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2517 && TREE_OVERFLOW (gnu_max_size))
2518 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2519 && TREE_OVERFLOW (gnu_max_size_unit))
2522 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2523 TYPE_SIZE (gnu_type));
2524 TYPE_SIZE_UNIT (gnu_type)
2525 = size_binop (MIN_EXPR, gnu_max_size_unit,
2526 TYPE_SIZE_UNIT (gnu_type));
2529 /* Set our alias set to that of our base type. This gives all
2530 array subtypes the same alias set. */
2531 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2534 /* If this is a packed type, make this type the same as the packed
2535 array type, but do some adjusting in the type first. */
2536 if (Present (Packed_Array_Type (gnat_entity)))
2538 Entity_Id gnat_index;
2539 tree gnu_inner_type;
2541 /* First finish the type we had been making so that we output
2542 debugging information for it. */
2544 = build_qualified_type (gnu_type,
2545 (TYPE_QUALS (gnu_type)
2546 | (TYPE_QUAL_VOLATILE
2547 * Treat_As_Volatile (gnat_entity))));
2549 /* Make it artificial only if the base type was artificial as well.
2550 That's sort of "morally" true and will make it possible for the
2551 debugger to look it up by name in DWARF, which is necessary in
2552 order to decode the packed array type. */
2554 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2555 !Comes_From_Source (gnat_entity)
2556 && !Comes_From_Source (Etype (gnat_entity)),
2557 debug_info_p, gnat_entity);
2559 /* Save it as our equivalent in case the call below elaborates
2561 save_gnu_tree (gnat_entity, gnu_decl, false);
2563 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2565 this_made_decl = true;
2566 gnu_type = TREE_TYPE (gnu_decl);
2567 save_gnu_tree (gnat_entity, NULL_TREE, false);
2569 gnu_inner_type = gnu_type;
2570 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2571 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2572 || TYPE_IS_PADDING_P (gnu_inner_type)))
2573 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2575 /* We need to attach the index type to the type we just made so
2576 that the actual bounds can later be put into a template. */
2577 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2578 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2579 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2580 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2582 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2584 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2585 TYPE_MODULUS for modular types so we make an extra
2586 subtype if necessary. */
2587 if (TYPE_MODULAR_P (gnu_inner_type))
2589 tree gnu_subtype = make_node (INTEGER_TYPE);
2590 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2591 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2593 TYPE_UNSIGNED (gnu_subtype) = 1;
2594 TYPE_PRECISION (gnu_subtype)
2595 = TYPE_PRECISION (gnu_inner_type);
2596 TYPE_MIN_VALUE (gnu_subtype)
2597 = TYPE_MIN_VALUE (gnu_inner_type);
2598 TYPE_MAX_VALUE (gnu_subtype)
2599 = TYPE_MAX_VALUE (gnu_inner_type);
2600 layout_type (gnu_subtype);
2602 gnu_inner_type = gnu_subtype;
2605 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2607 #ifdef ENABLE_CHECKING
2608 /* Check for other cases of overloading. */
2609 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2613 /* ??? This is necessary to make sure that the container is
2614 allocated with a null tree upfront; otherwise, it could
2615 be allocated with an uninitialized tree that is accessed
2616 before being set below. See ada-tree.h for details. */
2617 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2619 for (gnat_index = First_Index (gnat_entity);
2620 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2621 SET_TYPE_ACTUAL_BOUNDS
2623 tree_cons (NULL_TREE,
2624 get_unpadded_type (Etype (gnat_index)),
2625 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2627 if (Convention (gnat_entity) != Convention_Fortran)
2628 SET_TYPE_ACTUAL_BOUNDS
2630 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2632 if (TREE_CODE (gnu_type) == RECORD_TYPE
2633 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2634 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2638 /* Abort if packed array with no packed array type field set. */
2640 gcc_assert (!Is_Packed (gnat_entity));
2644 case E_String_Literal_Subtype:
2645 /* Create the type for a string literal. */
2647 Entity_Id gnat_full_type
2648 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2649 && Present (Full_View (Etype (gnat_entity)))
2650 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2651 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2652 tree gnu_string_array_type
2653 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2654 tree gnu_string_index_type
2655 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2656 (TYPE_DOMAIN (gnu_string_array_type))));
2657 tree gnu_lower_bound
2658 = convert (gnu_string_index_type,
2659 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2660 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2661 tree gnu_length = ssize_int (length - 1);
2662 tree gnu_upper_bound
2663 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2665 convert (gnu_string_index_type, gnu_length));
2667 = create_index_type (convert (sizetype, gnu_lower_bound),
2668 convert (sizetype, gnu_upper_bound),
2669 build_range_type (gnu_string_index_type,
2675 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2677 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2678 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2679 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2683 /* Record Types and Subtypes
2685 The following fields are defined on record types:
2687 Has_Discriminants True if the record has discriminants
2688 First_Discriminant Points to head of list of discriminants
2689 First_Entity Points to head of list of fields
2690 Is_Tagged_Type True if the record is tagged
2692 Implementation of Ada records and discriminated records:
2694 A record type definition is transformed into the equivalent of a C
2695 struct definition. The fields that are the discriminants which are
2696 found in the Full_Type_Declaration node and the elements of the
2697 Component_List found in the Record_Type_Definition node. The
2698 Component_List can be a recursive structure since each Variant of
2699 the Variant_Part of the Component_List has a Component_List.
2701 Processing of a record type definition comprises starting the list of
2702 field declarations here from the discriminants and the calling the
2703 function components_to_record to add the rest of the fields from the
2704 component list and return the gnu type node. The function
2705 components_to_record will call itself recursively as it traverses
2709 if (Has_Complex_Representation (gnat_entity))
2712 = build_complex_type
2714 (Etype (Defining_Entity
2715 (First (Component_Items
2718 (Declaration_Node (gnat_entity)))))))));
2724 Node_Id full_definition = Declaration_Node (gnat_entity);
2725 Node_Id record_definition = Type_Definition (full_definition);
2726 Entity_Id gnat_field;
2728 tree gnu_field_list = NULL_TREE;
2729 tree gnu_get_parent;
2730 /* Set PACKED in keeping with gnat_to_gnu_field. */
2732 = Is_Packed (gnat_entity)
2734 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2736 : (Known_Alignment (gnat_entity)
2737 || (Strict_Alignment (gnat_entity)
2738 && Known_Static_Esize (gnat_entity)))
2741 bool has_rep = Has_Specified_Layout (gnat_entity);
2742 bool all_rep = has_rep;
2744 = (Is_Tagged_Type (gnat_entity)
2745 && Nkind (record_definition) == N_Derived_Type_Definition);
2747 /* See if all fields have a rep clause. Stop when we find one
2749 for (gnat_field = First_Entity (gnat_entity);
2750 Present (gnat_field) && all_rep;
2751 gnat_field = Next_Entity (gnat_field))
2752 if ((Ekind (gnat_field) == E_Component
2753 || Ekind (gnat_field) == E_Discriminant)
2754 && No (Component_Clause (gnat_field)))
2757 /* If this is a record extension, go a level further to find the
2758 record definition. Also, verify we have a Parent_Subtype. */
2761 if (!type_annotate_only
2762 || Present (Record_Extension_Part (record_definition)))
2763 record_definition = Record_Extension_Part (record_definition);
2765 gcc_assert (type_annotate_only
2766 || Present (Parent_Subtype (gnat_entity)));
2769 /* Make a node for the record. If we are not defining the record,
2770 suppress expanding incomplete types. */
2771 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2772 TYPE_NAME (gnu_type) = gnu_entity_name;
2773 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2776 defer_incomplete_level++, this_deferred = true;
2778 /* If both a size and rep clause was specified, put the size in
2779 the record type now so that it can get the proper mode. */
2780 if (has_rep && Known_Esize (gnat_entity))
2781 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2783 /* Always set the alignment here so that it can be used to
2784 set the mode, if it is making the alignment stricter. If
2785 it is invalid, it will be checked again below. If this is to
2786 be Atomic, choose a default alignment of a word unless we know
2787 the size and it's smaller. */
2788 if (Known_Alignment (gnat_entity))
2789 TYPE_ALIGN (gnu_type)
2790 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2791 else if (Is_Atomic (gnat_entity))
2792 TYPE_ALIGN (gnu_type)
2793 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2794 /* If a type needs strict alignment, the minimum size will be the
2795 type size instead of the RM size (see validate_size). Cap the
2796 alignment, lest it causes this type size to become too large. */
2797 else if (Strict_Alignment (gnat_entity)
2798 && Known_Static_Esize (gnat_entity))
2800 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2801 unsigned int raw_align = raw_size & -raw_size;
2802 if (raw_align < BIGGEST_ALIGNMENT)
2803 TYPE_ALIGN (gnu_type) = raw_align;
2806 TYPE_ALIGN (gnu_type) = 0;
2808 /* If we have a Parent_Subtype, make a field for the parent. If
2809 this record has rep clauses, force the position to zero. */
2810 if (Present (Parent_Subtype (gnat_entity)))
2812 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2815 /* A major complexity here is that the parent subtype will
2816 reference our discriminants in its Discriminant_Constraint
2817 list. But those must reference the parent component of this
2818 record which is of the parent subtype we have not built yet!
2819 To break the circle we first build a dummy COMPONENT_REF which
2820 represents the "get to the parent" operation and initialize
2821 each of those discriminants to a COMPONENT_REF of the above
2822 dummy parent referencing the corresponding discriminant of the
2823 base type of the parent subtype. */
2824 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2825 build0 (PLACEHOLDER_EXPR, gnu_type),
2826 build_decl (FIELD_DECL, NULL_TREE,
2830 if (Has_Discriminants (gnat_entity))
2831 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2832 Present (gnat_field);
2833 gnat_field = Next_Stored_Discriminant (gnat_field))
2834 if (Present (Corresponding_Discriminant (gnat_field)))
2837 build3 (COMPONENT_REF,
2838 get_unpadded_type (Etype (gnat_field)),
2840 gnat_to_gnu_field_decl (Corresponding_Discriminant
2845 /* Then we build the parent subtype. If it has discriminants but
2846 the type itself has unknown discriminants, this means that it
2847 doesn't contain information about how the discriminants are
2848 derived from those of the ancestor type, so it cannot be used
2849 directly. Instead it is built by cloning the parent subtype
2850 of the underlying record view of the type, for which the above
2851 derivation of discriminants has been made explicit. */
2852 if (Has_Discriminants (gnat_parent)
2853 && Has_Unknown_Discriminants (gnat_entity))
2855 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2857 /* If we are defining the type, the underlying record
2858 view must already have been elaborated at this point.
2859 Otherwise do it now as its parent subtype cannot be
2860 technically elaborated on its own. */
2862 gcc_assert (present_gnu_tree (gnat_uview));
2864 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2866 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2868 /* Substitute the "get to the parent" of the type for that
2869 of its underlying record view in the cloned type. */
2870 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2871 Present (gnat_field);
2872 gnat_field = Next_Stored_Discriminant (gnat_field))
2873 if (Present (Corresponding_Discriminant (gnat_field)))
2875 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2877 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2878 gnu_get_parent, gnu_field, NULL_TREE);
2880 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2884 gnu_parent = gnat_to_gnu_type (gnat_parent);
2886 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2887 initially built. The discriminants must reference the fields
2888 of the parent subtype and not those of its base type for the
2889 placeholder machinery to properly work. */
2890 if (Has_Discriminants (gnat_entity))
2891 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2892 Present (gnat_field);
2893 gnat_field = Next_Stored_Discriminant (gnat_field))
2894 if (Present (Corresponding_Discriminant (gnat_field)))
2896 Entity_Id field = Empty;
2897 for (field = First_Stored_Discriminant (gnat_parent);
2899 field = Next_Stored_Discriminant (field))
2900 if (same_discriminant_p (gnat_field, field))
2902 gcc_assert (Present (field));
2903 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2904 = gnat_to_gnu_field_decl (field);
2907 /* The "get to the parent" COMPONENT_REF must be given its
2909 TREE_TYPE (gnu_get_parent) = gnu_parent;
2911 /* ...and reference the _parent field of this record. */
2913 = create_field_decl (get_identifier
2914 (Get_Name_String (Name_uParent)),
2915 gnu_parent, gnu_type, 0,
2916 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2917 has_rep ? bitsize_zero_node : 0, 1);
2918 DECL_INTERNAL_P (gnu_field_list) = 1;
2919 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2922 /* Make the fields for the discriminants and put them into the record
2923 unless it's an Unchecked_Union. */
2924 if (Has_Discriminants (gnat_entity))
2925 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2926 Present (gnat_field);
2927 gnat_field = Next_Stored_Discriminant (gnat_field))
2929 /* If this is a record extension and this discriminant
2930 is the renaming of another discriminant, we've already
2931 handled the discriminant above. */
2932 if (Present (Parent_Subtype (gnat_entity))
2933 && Present (Corresponding_Discriminant (gnat_field)))
2937 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2939 /* Make an expression using a PLACEHOLDER_EXPR from the
2940 FIELD_DECL node just created and link that with the
2941 corresponding GNAT defining identifier. Then add to the
2943 save_gnu_tree (gnat_field,
2944 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2945 build0 (PLACEHOLDER_EXPR,
2946 DECL_CONTEXT (gnu_field)),
2947 gnu_field, NULL_TREE),
2950 if (!Is_Unchecked_Union (gnat_entity))
2952 TREE_CHAIN (gnu_field) = gnu_field_list;
2953 gnu_field_list = gnu_field;
2957 /* Put the discriminants into the record (backwards), so we can
2958 know the appropriate discriminant to use for the names of the
2960 TYPE_FIELDS (gnu_type) = gnu_field_list;
2962 /* Add the listed fields into the record and finish it up. */
2963 components_to_record (gnu_type, Component_List (record_definition),
2964 gnu_field_list, packed, definition, NULL,
2965 false, all_rep, false,
2966 Is_Unchecked_Union (gnat_entity));
2968 /* We used to remove the associations of the discriminants and
2969 _Parent for validity checking, but we may need them if there's
2970 Freeze_Node for a subtype used in this record. */
2971 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2972 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2974 /* If it is a tagged record force the type to BLKmode to insure
2975 that these objects will always be placed in memory. Do the
2976 same thing for limited record types. */
2977 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2978 SET_TYPE_MODE (gnu_type, BLKmode);
2980 /* Fill in locations of fields. */
2981 annotate_rep (gnat_entity, gnu_type);
2983 /* If there are any entities in the chain corresponding to
2984 components that we did not elaborate, ensure we elaborate their
2985 types if they are Itypes. */
2986 for (gnat_temp = First_Entity (gnat_entity);
2987 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2988 if ((Ekind (gnat_temp) == E_Component
2989 || Ekind (gnat_temp) == E_Discriminant)
2990 && Is_Itype (Etype (gnat_temp))
2991 && !present_gnu_tree (gnat_temp))
2992 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2996 case E_Class_Wide_Subtype:
2997 /* If an equivalent type is present, that is what we should use.
2998 Otherwise, fall through to handle this like a record subtype
2999 since it may have constraints. */
3000 if (gnat_equiv_type != gnat_entity)
3002 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3003 maybe_present = true;
3007 /* ... fall through ... */
3009 case E_Record_Subtype:
3011 /* If Cloned_Subtype is Present it means this record subtype has
3012 identical layout to that type or subtype and we should use
3013 that GCC type for this one. The front end guarantees that
3014 the component list is shared. */
3015 if (Present (Cloned_Subtype (gnat_entity)))
3017 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3019 maybe_present = true;
3022 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3023 changing the type, make a new type with each field having the
3024 type of the field in the new subtype but having the position
3025 computed by transforming every discriminant reference according
3026 to the constraints. We don't see any difference between
3027 private and nonprivate type here since derivations from types should
3028 have been deferred until the completion of the private type. */
3031 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3036 defer_incomplete_level++, this_deferred = true;
3038 /* Get the base type initially for its alignment and sizes. But
3039 if it is a padded type, we do all the other work with the
3041 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3043 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
3044 && TYPE_IS_PADDING_P (gnu_base_type))
3045 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3047 gnu_type = gnu_orig_type = gnu_base_type;
3049 if (present_gnu_tree (gnat_entity))
3051 maybe_present = true;
3055 /* When the type has discriminants, and these discriminants
3056 affect the shape of what it built, factor them in.
3058 If we are making a subtype of an Unchecked_Union (must be an
3059 Itype), just return the type.
3061 We can't just use Is_Constrained because private subtypes without
3062 discriminants of full types with discriminants with default
3063 expressions are Is_Constrained but aren't constrained! */
3065 if (IN (Ekind (gnat_base_type), Record_Kind)
3066 && !Is_For_Access_Subtype (gnat_entity)
3067 && !Is_Unchecked_Union (gnat_base_type)
3068 && Is_Constrained (gnat_entity)
3069 && Stored_Constraint (gnat_entity) != No_Elist
3070 && Present (Discriminant_Constraint (gnat_entity)))
3072 Entity_Id gnat_field;
3073 tree gnu_field_list = 0;
3075 = compute_field_positions (gnu_orig_type, NULL_TREE,
3076 size_zero_node, bitsize_zero_node,
3079 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
3083 gnu_type = make_node (RECORD_TYPE);
3084 TYPE_NAME (gnu_type) = gnu_entity_name;
3085 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3087 /* Set the size, alignment and alias set of the new type to
3088 match that of the old one, doing required substitutions.
3089 We do it this early because we need the size of the new
3090 type below to discard old fields if necessary. */
3091 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3092 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3093 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3094 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3095 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3097 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3098 for (gnu_temp = gnu_subst_list;
3099 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3100 TYPE_SIZE (gnu_type)
3101 = substitute_in_expr (TYPE_SIZE (gnu_type),
3102 TREE_PURPOSE (gnu_temp),
3103 TREE_VALUE (gnu_temp));
3105 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3106 for (gnu_temp = gnu_subst_list;
3107 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3108 TYPE_SIZE_UNIT (gnu_type)
3109 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3110 TREE_PURPOSE (gnu_temp),
3111 TREE_VALUE (gnu_temp));
3113 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3114 for (gnu_temp = gnu_subst_list;
3115 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3117 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3118 TREE_PURPOSE (gnu_temp),
3119 TREE_VALUE (gnu_temp)));
3121 for (gnat_field = First_Entity (gnat_entity);
3122 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3123 if ((Ekind (gnat_field) == E_Component
3124 || Ekind (gnat_field) == E_Discriminant)
3125 && (Underlying_Type (Scope (Original_Record_Component
3128 && (No (Corresponding_Discriminant (gnat_field))
3129 || !Is_Tagged_Type (gnat_base_type)))
3132 = gnat_to_gnu_field_decl (Original_Record_Component
3135 = TREE_VALUE (purpose_member (gnu_old_field,
3137 tree gnu_pos = TREE_PURPOSE (gnu_offset);
3138 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3140 = gnat_to_gnu_type (Etype (gnat_field));
3141 tree gnu_size = TYPE_SIZE (gnu_field_type);
3142 tree gnu_new_pos = NULL_TREE;
3143 unsigned int offset_align
3144 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
3148 /* If there was a component clause, the field types must be
3149 the same for the type and subtype, so copy the data from
3150 the old field to avoid recomputation here. Also if the
3151 field is justified modular and the optimization in
3152 gnat_to_gnu_field was applied. */
3153 if (Present (Component_Clause
3154 (Original_Record_Component (gnat_field)))
3155 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3156 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3157 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3158 == TREE_TYPE (gnu_old_field)))
3160 gnu_size = DECL_SIZE (gnu_old_field);
3161 gnu_field_type = TREE_TYPE (gnu_old_field);
3164 /* If the old field was packed and of constant size, we
3165 have to get the old size here, as it might differ from
3166 what the Etype conveys and the latter might overlap
3167 onto the following field. Try to arrange the type for
3168 possible better packing along the way. */
3169 else if (DECL_PACKED (gnu_old_field)
3170 && TREE_CODE (DECL_SIZE (gnu_old_field))
3173 gnu_size = DECL_SIZE (gnu_old_field);
3174 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3175 && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3176 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3178 = make_packable_type (gnu_field_type, true);
3181 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3182 for (gnu_temp = gnu_subst_list;
3183 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3184 gnu_pos = substitute_in_expr (gnu_pos,
3185 TREE_PURPOSE (gnu_temp),
3186 TREE_VALUE (gnu_temp));
3188 /* If the position is now a constant, we can set it as the
3189 position of the field when we make it. Otherwise, we need
3190 to deal with it specially below. */
3191 if (TREE_CONSTANT (gnu_pos))
3193 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3195 /* Discard old fields that are outside the new type.
3196 This avoids confusing code scanning it to decide
3197 how to pass it to functions on some platforms. */
3198 if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3199 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3200 && !integer_zerop (gnu_size)
3201 && !tree_int_cst_lt (gnu_new_pos,
3202 TYPE_SIZE (gnu_type)))
3208 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3209 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3210 !DECL_NONADDRESSABLE_P (gnu_old_field));
3212 if (!TREE_CONSTANT (gnu_pos))
3214 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3215 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3216 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3217 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3218 DECL_SIZE (gnu_field) = gnu_size;
3219 DECL_SIZE_UNIT (gnu_field)
3220 = convert (sizetype,
3221 size_binop (CEIL_DIV_EXPR, gnu_size,
3222 bitsize_unit_node));
3223 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3226 DECL_INTERNAL_P (gnu_field)
3227 = DECL_INTERNAL_P (gnu_old_field);
3228 SET_DECL_ORIGINAL_FIELD
3229 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3230 ? DECL_ORIGINAL_FIELD (gnu_old_field)
3232 DECL_DISCRIMINANT_NUMBER (gnu_field)
3233 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3234 TREE_THIS_VOLATILE (gnu_field)
3235 = TREE_THIS_VOLATILE (gnu_old_field);
3237 /* To match the layout crafted in components_to_record, if
3238 this is the _Tag field, put it before any discriminants
3239 instead of after them as for all other fields. */
3240 if (Chars (gnat_field) == Name_uTag)
3241 gnu_field_list = chainon (gnu_field_list, gnu_field);
3244 TREE_CHAIN (gnu_field) = gnu_field_list;
3245 gnu_field_list = gnu_field;
3248 save_gnu_tree (gnat_field, gnu_field, false);
3251 /* Now go through the entities again looking for Itypes that
3252 we have not elaborated but should (e.g., Etypes of fields
3253 that have Original_Components). */
3254 for (gnat_field = First_Entity (gnat_entity);
3255 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3256 if ((Ekind (gnat_field) == E_Discriminant
3257 || Ekind (gnat_field) == E_Component)
3258 && !present_gnu_tree (Etype (gnat_field)))
3259 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3261 /* Do not finalize it since we're going to modify it below. */
3262 gnu_field_list = nreverse (gnu_field_list);
3263 finish_record_type (gnu_type, gnu_field_list, 2, true);
3265 /* Finalize size and mode. */
3266 TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3267 TYPE_SIZE_UNIT (gnu_type)
3268 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3270 compute_record_mode (gnu_type);
3272 /* Fill in locations of fields. */
3273 annotate_rep (gnat_entity, gnu_type);
3275 /* We've built a new type, make an XVS type to show what this
3276 is a subtype of. Some debuggers require the XVS type to be
3277 output first, so do it in that order. */
3280 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3281 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3283 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3284 gnu_orig_name = DECL_NAME (gnu_orig_name);
3286 TYPE_NAME (gnu_subtype_marker)
3287 = create_concat_name (gnat_entity, "XVS");
3288 finish_record_type (gnu_subtype_marker,
3289 create_field_decl (gnu_orig_name,
3296 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3297 gnu_subtype_marker);
3300 /* Now we can finalize it. */
3301 rest_of_record_type_compilation (gnu_type);
3304 /* Otherwise, go down all the components in the new type and
3305 make them equivalent to those in the base type. */
3307 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3308 gnat_temp = Next_Entity (gnat_temp))
3309 if ((Ekind (gnat_temp) == E_Discriminant
3310 && !Is_Unchecked_Union (gnat_base_type))
3311 || Ekind (gnat_temp) == E_Component)
3312 save_gnu_tree (gnat_temp,
3313 gnat_to_gnu_field_decl
3314 (Original_Record_Component (gnat_temp)), false);
3318 case E_Access_Subprogram_Type:
3319 /* Use the special descriptor type for dispatch tables if needed,
3320 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3321 Note that we are only required to do so for static tables in
3322 order to be compatible with the C++ ABI, but Ada 2005 allows
3323 to extend library level tagged types at the local level so
3324 we do it in the non-static case as well. */
3325 if (TARGET_VTABLE_USES_DESCRIPTORS
3326 && Is_Dispatch_Table_Entity (gnat_entity))
3328 gnu_type = fdesc_type_node;
3329 gnu_size = TYPE_SIZE (gnu_type);
3333 /* ... fall through ... */
3335 case E_Anonymous_Access_Subprogram_Type:
3336 /* If we are not defining this entity, and we have incomplete
3337 entities being processed above us, make a dummy type and
3338 fill it in later. */
3339 if (!definition && defer_incomplete_level != 0)
3341 struct incomplete *p
3342 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3345 = build_pointer_type
3346 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3347 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3348 !Comes_From_Source (gnat_entity),
3349 debug_info_p, gnat_entity);
3350 this_made_decl = true;
3351 gnu_type = TREE_TYPE (gnu_decl);
3352 save_gnu_tree (gnat_entity, gnu_decl, false);
3355 p->old_type = TREE_TYPE (gnu_type);
3356 p->full_type = Directly_Designated_Type (gnat_entity);
3357 p->next = defer_incomplete_list;
3358 defer_incomplete_list = p;
3362 /* ... fall through ... */
3364 case E_Allocator_Type:
3366 case E_Access_Attribute_Type:
3367 case E_Anonymous_Access_Type:
3368 case E_General_Access_Type:
3370 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3371 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3372 bool is_from_limited_with
3373 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3374 && From_With_Type (gnat_desig_equiv));
3376 /* Get the "full view" of this entity. If this is an incomplete
3377 entity from a limited with, treat its non-limited view as the full
3378 view. Otherwise, if this is an incomplete or private type, use the
3379 full view. In the former case, we might point to a private type,
3380 in which case, we need its full view. Also, we want to look at the
3381 actual type used for the representation, so this takes a total of
3383 Entity_Id gnat_desig_full_direct_first
3384 = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3385 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3386 ? Full_View (gnat_desig_equiv) : Empty));
3387 Entity_Id gnat_desig_full_direct
3388 = ((is_from_limited_with
3389 && Present (gnat_desig_full_direct_first)
3390 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3391 ? Full_View (gnat_desig_full_direct_first)
3392 : gnat_desig_full_direct_first);
3393 Entity_Id gnat_desig_full
3394 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3396 /* This the type actually used to represent the designated type,
3397 either gnat_desig_full or gnat_desig_equiv. */
3398 Entity_Id gnat_desig_rep;
3400 /* True if this is a pointer to an unconstrained array. */
3401 bool is_unconstrained_array;
3403 /* We want to know if we'll be seeing the freeze node for any
3404 incomplete type we may be pointing to. */
3406 = (Present (gnat_desig_full)
3407 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3408 : In_Extended_Main_Code_Unit (gnat_desig_type));
3410 /* True if we make a dummy type here. */
3411 bool got_fat_p = false;
3412 /* True if the dummy is a fat pointer. */
3413 bool made_dummy = false;
3414 tree gnu_desig_type = NULL_TREE;
3415 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3417 if (!targetm.valid_pointer_mode (p_mode))
3420 /* If either the designated type or its full view is an unconstrained
3421 array subtype, replace it with the type it's a subtype of. This
3422 avoids problems with multiple copies of unconstrained array types.
3423 Likewise, if the designated type is a subtype of an incomplete
3424 record type, use the parent type to avoid order of elaboration
3425 issues. This can lose some code efficiency, but there is no
3427 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3428 && ! Is_Constrained (gnat_desig_equiv))
3429 gnat_desig_equiv = Etype (gnat_desig_equiv);
3430 if (Present (gnat_desig_full)
3431 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3432 && ! Is_Constrained (gnat_desig_full))
3433 || (Ekind (gnat_desig_full) == E_Record_Subtype
3434 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3435 gnat_desig_full = Etype (gnat_desig_full);
3437 /* Now set the type that actually marks the representation of
3438 the designated type and also flag whether we have a unconstrained
3440 gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3441 is_unconstrained_array
3442 = (Is_Array_Type (gnat_desig_rep)
3443 && ! Is_Constrained (gnat_desig_rep));
3445 /* If we are pointing to an incomplete type whose completion is an
3446 unconstrained array, make a fat pointer type. The two types in our
3447 fields will be pointers to dummy nodes and will be replaced in
3448 update_pointer_to. Similarly, if the type itself is a dummy type or
3449 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3450 in case we have any thin pointers to it. */
3451 if (is_unconstrained_array
3452 && (Present (gnat_desig_full)
3453 || (present_gnu_tree (gnat_desig_equiv)
3454 && TYPE_IS_DUMMY_P (TREE_TYPE
3455 (get_gnu_tree (gnat_desig_equiv))))
3456 || (No (gnat_desig_full) && ! in_main_unit
3457 && defer_incomplete_level != 0
3458 && ! present_gnu_tree (gnat_desig_equiv))
3459 || (in_main_unit && is_from_limited_with
3460 && Present (Freeze_Node (gnat_desig_rep)))))
3464 if (present_gnu_tree (gnat_desig_rep))
3465 gnu_old = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3468 gnu_old = make_dummy_type (gnat_desig_rep);
3470 /* Show the dummy we get will be a fat pointer. */
3471 got_fat_p = made_dummy = true;
3474 /* If the call above got something that has a pointer, that
3475 pointer is our type. This could have happened either
3476 because the type was elaborated or because somebody
3477 else executed the code below. */
3478 gnu_type = TYPE_POINTER_TO (gnu_old);
3481 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3482 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3483 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3484 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3487 TYPE_NAME (gnu_template_type)
3488 = create_concat_name (gnat_desig_equiv, "XUB");
3489 TYPE_DUMMY_P (gnu_template_type) = 1;
3491 TYPE_NAME (gnu_array_type)
3492 = create_concat_name (gnat_desig_equiv, "XUA");
3493 TYPE_DUMMY_P (gnu_array_type) = 1;
3495 gnu_type = make_node (RECORD_TYPE);
3496 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3497 TYPE_POINTER_TO (gnu_old) = gnu_type;
3499 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3501 = chainon (chainon (NULL_TREE,
3503 (get_identifier ("P_ARRAY"),
3505 gnu_type, 0, 0, 0, 0)),
3506 create_field_decl (get_identifier ("P_BOUNDS"),
3508 gnu_type, 0, 0, 0, 0));
3510 /* Make sure we can place this into a register. */
3511 TYPE_ALIGN (gnu_type)
3512 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3513 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3515 /* Do not finalize this record type since the types of
3516 its fields are incomplete. */
3517 finish_record_type (gnu_type, fields, 0, true);
3519 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3520 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3521 = create_concat_name (gnat_desig_equiv, "XUT");
3522 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3526 /* If we already know what the full type is, use it. */
3527 else if (Present (gnat_desig_full)
3528 && present_gnu_tree (gnat_desig_full))
3529 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3531 /* Get the type of the thing we are to point to and build a pointer
3532 to it. If it is a reference to an incomplete or private type with a
3533 full view that is a record, make a dummy type node and get the
3534 actual type later when we have verified it is safe. */
3535 else if ((! in_main_unit
3536 && ! present_gnu_tree (gnat_desig_equiv)
3537 && Present (gnat_desig_full)
3538 && ! present_gnu_tree (gnat_desig_full)
3539 && Is_Record_Type (gnat_desig_full))
3540 /* Likewise if we are pointing to a record or array and we
3541 are to defer elaborating incomplete types. We do this
3542 since this access type may be the full view of some
3543 private type. Note that the unconstrained array case is
3545 || ((! in_main_unit || imported_p)
3546 && defer_incomplete_level != 0
3547 && ! present_gnu_tree (gnat_desig_equiv)
3548 && ((Is_Record_Type (gnat_desig_rep)
3549 || Is_Array_Type (gnat_desig_rep))))
3550 /* If this is a reference from a limited_with type back to our
3551 main unit and there's a Freeze_Node for it, either we have
3552 already processed the declaration and made the dummy type,
3553 in which case we just reuse the latter, or we have not yet,
3554 in which case we make the dummy type and it will be reused
3555 when the declaration is processed. In both cases, the
3556 pointer eventually created below will be automatically
3557 adjusted when the Freeze_Node is processed. Note that the
3558 unconstrained array case is handled above. */
3559 || (in_main_unit && is_from_limited_with
3560 && Present (Freeze_Node (gnat_desig_rep))))
3562 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3566 /* Otherwise handle the case of a pointer to itself. */
3567 else if (gnat_desig_equiv == gnat_entity)
3570 = build_pointer_type_for_mode (void_type_node, p_mode,
3571 No_Strict_Aliasing (gnat_entity));
3572 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3575 /* If expansion is disabled, the equivalent type of a concurrent
3576 type is absent, so build a dummy pointer type. */
3577 else if (type_annotate_only && No (gnat_desig_equiv))
3578 gnu_type = ptr_void_type_node;
3580 /* Finally, handle the straightforward case where we can just
3581 elaborate our designated type and point to it. */
3583 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3585 /* It is possible that a call to gnat_to_gnu_type above resolved our
3586 type. If so, just return it. */
3587 if (present_gnu_tree (gnat_entity))
3589 maybe_present = true;
3593 /* If we have a GCC type for the designated type, possibly modify it
3594 if we are pointing only to constant objects and then make a pointer
3595 to it. Don't do this for unconstrained arrays. */
3596 if (!gnu_type && gnu_desig_type)
3598 if (Is_Access_Constant (gnat_entity)
3599 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3602 = build_qualified_type
3604 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3606 /* Some extra processing is required if we are building a
3607 pointer to an incomplete type (in the GCC sense). We might
3608 have such a type if we just made a dummy, or directly out
3609 of the call to gnat_to_gnu_type above if we are processing
3610 an access type for a record component designating the
3611 record type itself. */
3612 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3614 /* We must ensure that the pointer to variant we make will
3615 be processed by update_pointer_to when the initial type
3616 is completed. Pretend we made a dummy and let further
3617 processing act as usual. */
3620 /* We must ensure that update_pointer_to will not retrieve
3621 the dummy variant when building a properly qualified
3622 version of the complete type. We take advantage of the
3623 fact that get_qualified_type is requiring TYPE_NAMEs to
3624 match to influence build_qualified_type and then also
3625 update_pointer_to here. */
3626 TYPE_NAME (gnu_desig_type)
3627 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3632 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3633 No_Strict_Aliasing (gnat_entity));
3636 /* If we are not defining this object and we made a dummy pointer,
3637 save our current definition, evaluate the actual type, and replace
3638 the tentative type we made with the actual one. If we are to defer
3639 actually looking up the actual type, make an entry in the
3640 deferred list. If this is from a limited with, we have to defer
3641 to the end of the current spec in two cases: first if the
3642 designated type is in the current unit and second if the access
3644 if ((! in_main_unit || is_from_limited_with) && made_dummy)
3647 = TYPE_FAT_POINTER_P (gnu_type)
3648 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3650 if (esize == POINTER_SIZE
3651 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3653 = build_pointer_type
3654 (TYPE_OBJECT_RECORD_TYPE
3655 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3657 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3658 !Comes_From_Source (gnat_entity),
3659 debug_info_p, gnat_entity);
3660 this_made_decl = true;
3661 gnu_type = TREE_TYPE (gnu_decl);
3662 save_gnu_tree (gnat_entity, gnu_decl, false);
3665 if (defer_incomplete_level == 0
3666 && ! (is_from_limited_with
3668 || In_Extended_Main_Code_Unit (gnat_entity))))
3669 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3670 gnat_to_gnu_type (gnat_desig_equiv));
3672 /* Note that the call to gnat_to_gnu_type here might have
3673 updated gnu_old_type directly, in which case it is not a
3674 dummy type any more when we get into update_pointer_to.
3676 This may happen for instance when the designated type is a
3677 record type, because their elaboration starts with an
3678 initial node from make_dummy_type, which may yield the same
3679 node as the one we got.
3681 Besides, variants of this non-dummy type might have been
3682 created along the way. update_pointer_to is expected to
3683 properly take care of those situations. */
3686 struct incomplete *p
3687 = (struct incomplete *) xmalloc (sizeof
3688 (struct incomplete));
3689 struct incomplete **head
3690 = (is_from_limited_with
3692 || In_Extended_Main_Code_Unit (gnat_entity))
3693 ? &defer_limited_with : &defer_incomplete_list);
3695 p->old_type = gnu_old_type;
3696 p->full_type = gnat_desig_equiv;
3704 case E_Access_Protected_Subprogram_Type:
3705 case E_Anonymous_Access_Protected_Subprogram_Type:
3706 if (type_annotate_only && No (gnat_equiv_type))
3707 gnu_type = ptr_void_type_node;
3710 /* The runtime representation is the equivalent type. */
3711 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3712 maybe_present = true;
3715 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3716 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3717 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3718 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3719 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3724 case E_Access_Subtype:
3726 /* We treat this as identical to its base type; any constraint is
3727 meaningful only to the front end.
3729 The designated type must be elaborated as well, if it does
3730 not have its own freeze node. Designated (sub)types created
3731 for constrained components of records with discriminants are
3732 not frozen by the front end and thus not elaborated by gigi,
3733 because their use may appear before the base type is frozen,
3734 and because it is not clear that they are needed anywhere in
3735 Gigi. With the current model, there is no correct place where
3736 they could be elaborated. */
3738 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3739 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3740 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3741 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3742 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3744 /* If we are not defining this entity, and we have incomplete
3745 entities being processed above us, make a dummy type and
3746 elaborate it later. */
3747 if (!definition && defer_incomplete_level != 0)
3749 struct incomplete *p
3750 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3752 = build_pointer_type
3753 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3755 p->old_type = TREE_TYPE (gnu_ptr_type);
3756 p->full_type = Directly_Designated_Type (gnat_entity);
3757 p->next = defer_incomplete_list;
3758 defer_incomplete_list = p;
3760 else if (!IN (Ekind (Base_Type
3761 (Directly_Designated_Type (gnat_entity))),
3762 Incomplete_Or_Private_Kind))
3763 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3767 maybe_present = true;
3770 /* Subprogram Entities
3772 The following access functions are defined for subprograms (functions
3775 First_Formal The first formal parameter.
3776 Is_Imported Indicates that the subprogram has appeared in
3777 an INTERFACE or IMPORT pragma. For now we
3778 assume that the external language is C.
3779 Is_Exported Likewise but for an EXPORT pragma.
3780 Is_Inlined True if the subprogram is to be inlined.
3782 In addition for function subprograms we have:
3784 Etype Return type of the function.
3786 Each parameter is first checked by calling must_pass_by_ref on its
3787 type to determine if it is passed by reference. For parameters which
3788 are copied in, if they are Ada In Out or Out parameters, their return
3789 value becomes part of a record which becomes the return type of the
3790 function (C function - note that this applies only to Ada procedures
3791 so there is no Ada return type). Additional code to store back the
3792 parameters will be generated on the caller side. This transformation
3793 is done here, not in the front-end.
3795 The intended result of the transformation can be seen from the
3796 equivalent source rewritings that follow:
3798 struct temp {int a,b};
3799 procedure P (A,B: In Out ...) is temp P (int A,B)
3802 end P; return {A,B};
3809 For subprogram types we need to perform mainly the same conversions to
3810 GCC form that are needed for procedures and function declarations. The
3811 only difference is that at the end, we make a type declaration instead
3812 of a function declaration. */
3814 case E_Subprogram_Type:
3818 /* The first GCC parameter declaration (a PARM_DECL node). The
3819 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3820 actually is the head of this parameter list. */
3821 tree gnu_param_list = NULL_TREE;
3822 /* Likewise for the stub associated with an exported procedure. */
3823 tree gnu_stub_param_list = NULL_TREE;
3824 /* The type returned by a function. If the subprogram is a procedure
3825 this type should be void_type_node. */
3826 tree gnu_return_type = void_type_node;
3827 /* List of fields in return type of procedure with copy-in copy-out
3829 tree gnu_field_list = NULL_TREE;
3830 /* Non-null for subprograms containing parameters passed by copy-in
3831 copy-out (Ada In Out or Out parameters not passed by reference),
3832 in which case it is the list of nodes used to specify the values of
3833 the in out/out parameters that are returned as a record upon
3834 procedure return. The TREE_PURPOSE of an element of this list is
3835 a field of the record and the TREE_VALUE is the PARM_DECL
3836 corresponding to that field. This list will be saved in the
3837 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3838 tree gnu_return_list = NULL_TREE;
3839 /* If an import pragma asks to map this subprogram to a GCC builtin,
3840 this is the builtin DECL node. */
3841 tree gnu_builtin_decl = NULL_TREE;
3842 /* For the stub associated with an exported procedure. */
3843 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3844 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3845 Entity_Id gnat_param;
3846 bool inline_flag = Is_Inlined (gnat_entity);
3847 bool public_flag = Is_Public (gnat_entity) || imported_p;
3849 = (Is_Public (gnat_entity) && !definition) || imported_p;
3851 /* The semantics of "pure" in Ada essentially matches that of "const"
3852 in the back-end. In particular, both properties are orthogonal to
3853 the "nothrow" property if the EH circuitry is explicit in the
3854 internal representation of the back-end. If we are to completely
3855 hide the EH circuitry from it, we need to declare that calls to pure
3856 Ada subprograms that can throw have side effects since they can
3857 trigger an "abnormal" transfer of control flow; thus they can be
3858 neither "const" nor "pure" in the back-end sense. */
3860 = (Exception_Mechanism == Back_End_Exceptions
3861 && Is_Pure (gnat_entity));
3863 bool volatile_flag = No_Return (gnat_entity);
3864 bool returns_by_ref = false;
3865 bool returns_unconstrained = false;
3866 bool returns_by_target_ptr = false;
3867 bool has_copy_in_out = false;
3868 bool has_stub = false;
3871 if (kind == E_Subprogram_Type && !definition)
3872 /* A parameter may refer to this type, so defer completion
3873 of any incomplete types. */
3874 defer_incomplete_level++, this_deferred = true;
3876 /* If the subprogram has an alias, it is probably inherited, so
3877 we can use the original one. If the original "subprogram"
3878 is actually an enumeration literal, it may be the first use
3879 of its type, so we must elaborate that type now. */
3880 if (Present (Alias (gnat_entity)))
3882 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3883 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3885 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3888 /* Elaborate any Itypes in the parameters of this entity. */
3889 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3890 Present (gnat_temp);
3891 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3892 if (Is_Itype (Etype (gnat_temp)))
3893 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3898 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3899 corresponding DECL node.
3901 We still want the parameter associations to take place because the
3902 proper generation of calls depends on it (a GNAT parameter without
3903 a corresponding GCC tree has a very specific meaning), so we don't
3905 if (Convention (gnat_entity) == Convention_Intrinsic)
3906 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3908 /* ??? What if we don't find the builtin node above ? warn ? err ?
3909 In the current state we neither warn nor err, and calls will just
3910 be handled as for regular subprograms. */
3912 if (kind == E_Function || kind == E_Subprogram_Type)
3913 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3915 /* If this function returns by reference, make the actual
3916 return type of this function the pointer and mark the decl. */
3917 if (Returns_By_Ref (gnat_entity))
3919 returns_by_ref = true;
3920 gnu_return_type = build_pointer_type (gnu_return_type);
3923 /* If the Mechanism is By_Reference, ensure the return type uses
3924 the machine's by-reference mechanism, which may not the same
3925 as above (e.g., it might be by passing a fake parameter). */
3926 else if (kind == E_Function
3927 && Mechanism (gnat_entity) == By_Reference)
3929 TREE_ADDRESSABLE (gnu_return_type) = 1;
3931 /* We expect this bit to be reset by gigi shortly, so can avoid a
3932 type node copy here. This actually also prevents troubles with
3933 the generation of debug information for the function, because
3934 we might have issued such info for this type already, and would
3935 be attaching a distinct type node to the function if we made a
3939 /* If we are supposed to return an unconstrained array,
3940 actually return a fat pointer and make a note of that. Return
3941 a pointer to an unconstrained record of variable size. */
3942 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3944 gnu_return_type = TREE_TYPE (gnu_return_type);
3945 returns_unconstrained = true;
3948 /* If the type requires a transient scope, the result is allocated
3949 on the secondary stack, so the result type of the function is
3951 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3953 gnu_return_type = build_pointer_type (gnu_return_type);
3954 returns_unconstrained = true;
3957 /* If the type is a padded type and the underlying type would not
3958 be passed by reference or this function has a foreign convention,
3959 return the underlying type. */
3960 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3961 && TYPE_IS_PADDING_P (gnu_return_type)
3962 && (!default_pass_by_ref (TREE_TYPE
3963 (TYPE_FIELDS (gnu_return_type)))
3964 || Has_Foreign_Convention (gnat_entity)))
3965 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3967 /* If the return type has a non-constant size, we convert the function
3968 into a procedure and its caller will pass a pointer to an object as
3969 the first parameter when we call the function. This can happen for
3970 an unconstrained type with a maximum size or a constrained type with
3971 a size not known at compile time. */
3972 if (TYPE_SIZE_UNIT (gnu_return_type)
3973 && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
3975 returns_by_target_ptr = true;
3977 = create_param_decl (get_identifier ("TARGET"),
3978 build_reference_type (gnu_return_type),
3980 gnu_return_type = void_type_node;
3983 /* If the return type has a size that overflows, we cannot have
3984 a function that returns that type. This usage doesn't make
3985 sense anyway, so give an error here. */
3986 if (TYPE_SIZE_UNIT (gnu_return_type)
3987 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3988 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3990 post_error ("cannot return type whose size overflows",
3992 gnu_return_type = copy_node (gnu_return_type);
3993 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3994 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3995 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3996 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3999 /* Look at all our parameters and get the type of
4000 each. While doing this, build a copy-out structure if
4003 /* Loop over the parameters and get their associated GCC tree.
4004 While doing this, build a copy-out structure if we need one. */
4005 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4006 Present (gnat_param);
4007 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4009 tree gnu_param_name = get_entity_name (gnat_param);
4010 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4011 tree gnu_param, gnu_field;
4012 bool copy_in_copy_out = false;
4013 Mechanism_Type mech = Mechanism (gnat_param);
4015 /* Builtins are expanded inline and there is no real call sequence
4016 involved. So the type expected by the underlying expander is
4017 always the type of each argument "as is". */
4018 if (gnu_builtin_decl)
4020 /* Handle the first parameter of a valued procedure specially. */
4021 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4022 mech = By_Copy_Return;
4023 /* Otherwise, see if a Mechanism was supplied that forced this
4024 parameter to be passed one way or another. */
4025 else if (mech == Default
4026 || mech == By_Copy || mech == By_Reference)
4028 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4029 mech = By_Descriptor;
4031 else if (By_Short_Descriptor_Last <= mech &&
4032 mech <= By_Short_Descriptor)
4033 mech = By_Short_Descriptor;
4037 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4038 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4039 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4041 mech = By_Reference;
4047 post_error ("unsupported mechanism for&", gnat_param);
4052 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4053 Has_Foreign_Convention (gnat_entity),
4056 /* We are returned either a PARM_DECL or a type if no parameter
4057 needs to be passed; in either case, adjust the type. */
4058 if (DECL_P (gnu_param))
4059 gnu_param_type = TREE_TYPE (gnu_param);
4062 gnu_param_type = gnu_param;
4063 gnu_param = NULL_TREE;
4068 /* If it's an exported subprogram, we build a parameter list
4069 in parallel, in case we need to emit a stub for it. */
4070 if (Is_Exported (gnat_entity))
4073 = chainon (gnu_param, gnu_stub_param_list);
4074 /* Change By_Descriptor parameter to By_Reference for
4075 the internal version of an exported subprogram. */
4076 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4079 = gnat_to_gnu_param (gnat_param, By_Reference,
4085 gnu_param = copy_node (gnu_param);
4088 gnu_param_list = chainon (gnu_param, gnu_param_list);
4089 Sloc_to_locus (Sloc (gnat_param),
4090 &DECL_SOURCE_LOCATION (gnu_param));
4091 save_gnu_tree (gnat_param, gnu_param, false);
4093 /* If a parameter is a pointer, this function may modify
4094 memory through it and thus shouldn't be considered
4095 a const function. Also, the memory may be modified
4096 between two calls, so they can't be CSE'ed. The latter
4097 case also handles by-ref parameters. */
4098 if (POINTER_TYPE_P (gnu_param_type)
4099 || TYPE_FAT_POINTER_P (gnu_param_type))
4103 if (copy_in_copy_out)
4105 if (!has_copy_in_out)
4107 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4108 gnu_return_type = make_node (RECORD_TYPE);
4109 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4110 has_copy_in_out = true;
4113 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
4114 gnu_return_type, 0, 0, 0, 0);
4115 Sloc_to_locus (Sloc (gnat_param),
4116 &DECL_SOURCE_LOCATION (gnu_field));
4117 TREE_CHAIN (gnu_field) = gnu_field_list;
4118 gnu_field_list = gnu_field;
4119 gnu_return_list = tree_cons (gnu_field, gnu_param,
4124 /* Do not compute record for out parameters if subprogram is
4125 stubbed since structures are incomplete for the back-end. */
4126 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4127 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4130 /* If we have a CICO list but it has only one entry, we convert
4131 this function into a function that simply returns that one
4133 if (list_length (gnu_return_list) == 1)
4134 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
4136 if (Has_Stdcall_Convention (gnat_entity))
4137 prepend_one_attribute_to
4138 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4139 get_identifier ("stdcall"), NULL_TREE,
4142 /* If we are on a target where stack realignment is needed for 'main'
4143 to honor GCC's implicit expectations (stack alignment greater than
4144 what the base ABI guarantees), ensure we do the same for foreign
4145 convention subprograms as they might be used as callbacks from code
4146 breaking such expectations. Note that this applies to task entry
4147 points in particular. */
4148 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4149 && Has_Foreign_Convention (gnat_entity))
4150 prepend_one_attribute_to
4151 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4152 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4155 /* The lists have been built in reverse. */
4156 gnu_param_list = nreverse (gnu_param_list);
4158 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4159 gnu_return_list = nreverse (gnu_return_list);
4161 if (Ekind (gnat_entity) == E_Function)
4162 Set_Mechanism (gnat_entity,
4163 (returns_by_ref || returns_unconstrained
4164 ? By_Reference : By_Copy));
4166 = create_subprog_type (gnu_return_type, gnu_param_list,
4167 gnu_return_list, returns_unconstrained,
4168 returns_by_ref, returns_by_target_ptr);
4172 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4173 gnu_return_list, returns_unconstrained,
4174 returns_by_ref, returns_by_target_ptr);
4176 /* A subprogram (something that doesn't return anything) shouldn't
4177 be considered const since there would be no reason for such a
4178 subprogram. Note that procedures with Out (or In Out) parameters
4179 have already been converted into a function with a return type. */
4180 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4184 = build_qualified_type (gnu_type,
4185 TYPE_QUALS (gnu_type)
4186 | (TYPE_QUAL_CONST * const_flag)
4187 | (TYPE_QUAL_VOLATILE * volatile_flag));
4189 Sloc_to_locus (Sloc (gnat_entity), &input_location);
4193 = build_qualified_type (gnu_stub_type,
4194 TYPE_QUALS (gnu_stub_type)
4195 | (TYPE_QUAL_CONST * const_flag)
4196 | (TYPE_QUAL_VOLATILE * volatile_flag));
4198 /* If we have a builtin decl for that function, check the signatures
4199 compatibilities. If the signatures are compatible, use the builtin
4200 decl. If they are not, we expect the checker predicate to have
4201 posted the appropriate errors, and just continue with what we have
4203 if (gnu_builtin_decl)
4205 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
4207 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
4209 gnu_decl = gnu_builtin_decl;
4210 gnu_type = gnu_builtin_type;
4215 /* If there was no specified Interface_Name and the external and
4216 internal names of the subprogram are the same, only use the
4217 internal name to allow disambiguation of nested subprograms. */
4218 if (No (Interface_Name (gnat_entity))
4219 && gnu_ext_name == gnu_entity_name)
4220 gnu_ext_name = NULL_TREE;
4222 /* If we are defining the subprogram and it has an Address clause
4223 we must get the address expression from the saved GCC tree for the
4224 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4225 the address expression here since the front-end has guaranteed
4226 in that case that the elaboration has no effects. If there is
4227 an Address clause and we are not defining the object, just
4228 make it a constant. */
4229 if (Present (Address_Clause (gnat_entity)))
4231 tree gnu_address = NULL_TREE;
4235 = (present_gnu_tree (gnat_entity)
4236 ? get_gnu_tree (gnat_entity)
4237 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4239 save_gnu_tree (gnat_entity, NULL_TREE, false);
4241 /* Convert the type of the object to a reference type that can
4242 alias everything as per 13.3(19). */
4244 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4246 gnu_address = convert (gnu_type, gnu_address);
4249 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4250 gnu_address, false, Is_Public (gnat_entity),
4251 extern_flag, false, NULL, gnat_entity);
4252 DECL_BY_REF_P (gnu_decl) = 1;
4255 else if (kind == E_Subprogram_Type)
4256 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4257 !Comes_From_Source (gnat_entity),
4258 debug_info_p, gnat_entity);
4263 gnu_stub_name = gnu_ext_name;
4264 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4265 public_flag = false;
4268 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4269 gnu_type, gnu_param_list,
4270 inline_flag, public_flag,
4271 extern_flag, attr_list,
4276 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4277 gnu_stub_type, gnu_stub_param_list,
4279 extern_flag, attr_list,
4281 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4284 /* This is unrelated to the stub built right above. */
4285 DECL_STUBBED_P (gnu_decl)
4286 = Convention (gnat_entity) == Convention_Stubbed;
4291 case E_Incomplete_Type:
4292 case E_Incomplete_Subtype:
4293 case E_Private_Type:
4294 case E_Private_Subtype:
4295 case E_Limited_Private_Type:
4296 case E_Limited_Private_Subtype:
4297 case E_Record_Type_With_Private:
4298 case E_Record_Subtype_With_Private:
4300 /* Get the "full view" of this entity. If this is an incomplete
4301 entity from a limited with, treat its non-limited view as the
4302 full view. Otherwise, use either the full view or the underlying
4303 full view, whichever is present. This is used in all the tests
4306 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4307 && From_With_Type (gnat_entity))
4308 ? Non_Limited_View (gnat_entity)
4309 : Present (Full_View (gnat_entity))
4310 ? Full_View (gnat_entity)
4311 : Underlying_Full_View (gnat_entity);
4313 /* If this is an incomplete type with no full view, it must be a Taft
4314 Amendment type, in which case we return a dummy type. Otherwise,
4315 just get the type from its Etype. */
4318 if (kind == E_Incomplete_Type)
4320 gnu_type = make_dummy_type (gnat_entity);
4321 gnu_decl = TYPE_STUB_DECL (gnu_type);
4325 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4327 maybe_present = true;
4332 /* If we already made a type for the full view, reuse it. */
4333 else if (present_gnu_tree (full_view))
4335 gnu_decl = get_gnu_tree (full_view);
4339 /* Otherwise, if we are not defining the type now, get the type
4340 from the full view. But always get the type from the full view
4341 for define on use types, since otherwise we won't see them! */
4342 else if (!definition
4343 || (Is_Itype (full_view)
4344 && No (Freeze_Node (gnat_entity)))
4345 || (Is_Itype (gnat_entity)
4346 && No (Freeze_Node (full_view))))
4348 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4349 maybe_present = true;
4353 /* For incomplete types, make a dummy type entry which will be
4354 replaced later. Save it as the full declaration's type so
4355 we can do any needed updates when we see it. */
4356 gnu_type = make_dummy_type (gnat_entity);
4357 gnu_decl = TYPE_STUB_DECL (gnu_type);
4358 save_gnu_tree (full_view, gnu_decl, 0);
4362 /* Simple class_wide types are always viewed as their root_type
4363 by Gigi unless an Equivalent_Type is specified. */
4364 case E_Class_Wide_Type:
4365 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4366 maybe_present = true;
4370 case E_Task_Subtype:
4371 case E_Protected_Type:
4372 case E_Protected_Subtype:
4373 if (type_annotate_only && No (gnat_equiv_type))
4374 gnu_type = void_type_node;
4376 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4378 maybe_present = true;
4382 gnu_decl = create_label_decl (gnu_entity_name);
4387 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4388 we've already saved it, so we don't try to. */
4389 gnu_decl = error_mark_node;
4397 /* If we had a case where we evaluated another type and it might have
4398 defined this one, handle it here. */
4399 if (maybe_present && present_gnu_tree (gnat_entity))
4401 gnu_decl = get_gnu_tree (gnat_entity);
4405 /* If we are processing a type and there is either no decl for it or
4406 we just made one, do some common processing for the type, such as
4407 handling alignment and possible padding. */
4408 if (is_type && (!gnu_decl || this_made_decl))
4410 if (Is_Tagged_Type (gnat_entity)
4411 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4412 TYPE_ALIGN_OK (gnu_type) = 1;
4414 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4415 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4417 /* ??? Don't set the size for a String_Literal since it is either
4418 confirming or we don't handle it properly (if the low bound is
4420 if (!gnu_size && kind != E_String_Literal_Subtype)
4421 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4423 Has_Size_Clause (gnat_entity));
4425 /* If a size was specified, see if we can make a new type of that size
4426 by rearranging the type, for example from a fat to a thin pointer. */
4430 = make_type_from_size (gnu_type, gnu_size,
4431 Has_Biased_Representation (gnat_entity));
4433 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4434 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4438 /* If the alignment hasn't already been processed and this is
4439 not an unconstrained array, see if an alignment is specified.
4440 If not, we pick a default alignment for atomic objects. */
4441 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4443 else if (Known_Alignment (gnat_entity))
4445 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4446 TYPE_ALIGN (gnu_type));
4448 /* Warn on suspiciously large alignments. This should catch
4449 errors about the (alignment,byte)/(size,bit) discrepancy. */
4450 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4454 /* If a size was specified, take it into account. Otherwise
4455 use the RM size for records as the type size has already
4456 been adjusted to the alignment. */
4459 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4460 || TREE_CODE (gnu_type) == UNION_TYPE
4461 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4462 && !TYPE_IS_FAT_POINTER_P (gnu_type))
4463 size = rm_size (gnu_type);
4465 size = TYPE_SIZE (gnu_type);
4467 /* Consider an alignment as suspicious if the alignment/size
4468 ratio is greater or equal to the byte/bit ratio. */
4469 if (host_integerp (size, 1)
4470 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4471 post_error_ne ("?suspiciously large alignment specified for&",
4472 Expression (Alignment_Clause (gnat_entity)),
4476 else if (Is_Atomic (gnat_entity) && !gnu_size
4477 && host_integerp (TYPE_SIZE (gnu_type), 1)
4478 && integer_pow2p (TYPE_SIZE (gnu_type)))
4479 align = MIN (BIGGEST_ALIGNMENT,
4480 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4481 else if (Is_Atomic (gnat_entity) && gnu_size
4482 && host_integerp (gnu_size, 1)
4483 && integer_pow2p (gnu_size))
4484 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4486 /* See if we need to pad the type. If we did, and made a record,
4487 the name of the new type may be changed. So get it back for
4488 us when we make the new TYPE_DECL below. */
4489 if (gnu_size || align > 0)
4490 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4491 "PAD", true, definition, false);
4493 if (TREE_CODE (gnu_type) == RECORD_TYPE
4494 && TYPE_IS_PADDING_P (gnu_type))
4496 gnu_entity_name = TYPE_NAME (gnu_type);
4497 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4498 gnu_entity_name = DECL_NAME (gnu_entity_name);
4501 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4503 /* If we are at global level, GCC will have applied variable_size to
4504 the type, but that won't have done anything. So, if it's not
4505 a constant or self-referential, call elaborate_expression_1 to
4506 make a variable for the size rather than calculating it each time.
4507 Handle both the RM size and the actual size. */
4508 if (global_bindings_p ()
4509 && TYPE_SIZE (gnu_type)
4510 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4511 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4513 if (TREE_CODE (gnu_type) == RECORD_TYPE
4514 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4515 TYPE_SIZE (gnu_type), 0))
4517 TYPE_SIZE (gnu_type)
4518 = elaborate_expression_1 (gnat_entity, gnat_entity,
4519 TYPE_SIZE (gnu_type),
4520 get_identifier ("SIZE"),
4522 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4526 TYPE_SIZE (gnu_type)
4527 = elaborate_expression_1 (gnat_entity, gnat_entity,
4528 TYPE_SIZE (gnu_type),
4529 get_identifier ("SIZE"),
4532 /* ??? For now, store the size as a multiple of the alignment
4533 in bytes so that we can see the alignment from the tree. */
4534 TYPE_SIZE_UNIT (gnu_type)
4536 (MULT_EXPR, sizetype,
4537 elaborate_expression_1
4538 (gnat_entity, gnat_entity,
4539 build_binary_op (EXACT_DIV_EXPR, sizetype,
4540 TYPE_SIZE_UNIT (gnu_type),
4541 size_int (TYPE_ALIGN (gnu_type)
4543 get_identifier ("SIZE_A_UNIT"),
4545 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4547 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4550 elaborate_expression_1 (gnat_entity,
4552 TYPE_ADA_SIZE (gnu_type),
4553 get_identifier ("RM_SIZE"),
4558 /* If this is a record type or subtype, call elaborate_expression_1 on
4559 any field position. Do this for both global and local types.
4560 Skip any fields that we haven't made trees for to avoid problems with
4561 class wide types. */
4562 if (IN (kind, Record_Kind))
4563 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4564 gnat_temp = Next_Entity (gnat_temp))
4565 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4567 tree gnu_field = get_gnu_tree (gnat_temp);
4569 /* ??? Unfortunately, GCC needs to be able to prove the
4570 alignment of this offset and if it's a variable, it can't.
4571 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4572 right now, we have to put in an explicit multiply and
4573 divide by that value. */
4574 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4576 DECL_FIELD_OFFSET (gnu_field)
4578 (MULT_EXPR, sizetype,
4579 elaborate_expression_1
4580 (gnat_temp, gnat_temp,
4581 build_binary_op (EXACT_DIV_EXPR, sizetype,
4582 DECL_FIELD_OFFSET (gnu_field),
4583 size_int (DECL_OFFSET_ALIGN (gnu_field)
4585 get_identifier ("OFFSET"),
4587 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4589 /* ??? The context of gnu_field is not necessarily gnu_type so
4590 the MULT_EXPR node built above may not be marked by the call
4591 to create_type_decl below. */
4592 if (global_bindings_p ())
4593 mark_visited (&DECL_FIELD_OFFSET (gnu_field));
4597 gnu_type = build_qualified_type (gnu_type,
4598 (TYPE_QUALS (gnu_type)
4599 | (TYPE_QUAL_VOLATILE
4600 * Treat_As_Volatile (gnat_entity))));
4602 if (Is_Atomic (gnat_entity))
4603 check_ok_for_atomic (gnu_type, gnat_entity, false);
4605 if (Present (Alignment_Clause (gnat_entity)))
4606 TYPE_USER_ALIGN (gnu_type) = 1;
4608 if (Universal_Aliasing (gnat_entity))
4609 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4612 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4613 !Comes_From_Source (gnat_entity),
4614 debug_info_p, gnat_entity);
4616 TREE_TYPE (gnu_decl) = gnu_type;
4619 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4621 gnu_type = TREE_TYPE (gnu_decl);
4623 /* If this is a derived type, relate its alias set to that of its parent
4624 to avoid troubles when a call to an inherited primitive is inlined in
4625 a context where a derived object is accessed. The inlined code works
4626 on the parent view so the resulting code may access the same object
4627 using both the parent and the derived alias sets, which thus have to
4628 conflict. As the same issue arises with component references, the
4629 parent alias set also has to conflict with composite types enclosing
4630 derived components. For instance, if we have:
4637 we want T to conflict with both D and R, in addition to R being a
4638 superset of D by record/component construction.
4640 One way to achieve this is to perform an alias set copy from the
4641 parent to the derived type. This is not quite appropriate, though,
4642 as we don't want separate derived types to conflict with each other:
4644 type I1 is new Integer;
4645 type I2 is new Integer;
4647 We want I1 and I2 to both conflict with Integer but we do not want
4648 I1 to conflict with I2, and an alias set copy on derivation would
4651 The option chosen is to make the alias set of the derived type a
4652 superset of that of its parent type. It trivially fulfills the
4653 simple requirement for the Integer derivation example above, and
4654 the component case as well by superset transitivity:
4657 R ----------> D ----------> T
4659 The language rules ensure the parent type is already frozen here. */
4660 if (Is_Derived_Type (gnat_entity))
4662 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4663 relate_alias_sets (gnu_type, gnu_parent_type, ALIAS_SET_SUPERSET);
4666 /* Back-annotate the Alignment of the type if not already in the
4667 tree. Likewise for sizes. */
4668 if (Unknown_Alignment (gnat_entity))
4670 unsigned int double_align, align;
4671 bool is_capped_double, align_clause;
4673 /* If the default alignment of "double" or larger scalar types is
4674 specifically capped and this is not an array with an alignment
4675 clause on the component type, return the cap. */
4676 if ((double_align = double_float_alignment) > 0)
4678 = is_double_float_or_array (gnat_entity, &align_clause);
4679 else if ((double_align = double_scalar_alignment) > 0)
4681 = is_double_scalar_or_array (gnat_entity, &align_clause);
4683 is_capped_double = align_clause = false;
4685 if (is_capped_double && !align_clause)
4686 align = double_align;
4688 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4690 Set_Alignment (gnat_entity, UI_From_Int (align));
4693 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4695 /* If the size is self-referential, we annotate the maximum
4696 value of that size. */
4697 tree gnu_size = TYPE_SIZE (gnu_type);
4699 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4700 gnu_size = max_size (gnu_size, true);
4702 Set_Esize (gnat_entity, annotate_value (gnu_size));
4704 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4706 /* In this mode the tag and the parent components are not
4707 generated by the front-end, so the sizes must be adjusted
4709 int size_offset, new_size;
4711 if (Is_Derived_Type (gnat_entity))
4714 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4715 Set_Alignment (gnat_entity,
4716 Alignment (Etype (Base_Type (gnat_entity))));
4719 size_offset = POINTER_SIZE;
4721 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4722 Set_Esize (gnat_entity,
4723 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4724 / POINTER_SIZE) * POINTER_SIZE));
4725 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4729 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4730 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4733 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4734 DECL_ARTIFICIAL (gnu_decl) = 1;
4736 if (!debug_info_p && DECL_P (gnu_decl)
4737 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4738 && No (Renamed_Object (gnat_entity)))
4739 DECL_IGNORED_P (gnu_decl) = 1;
4741 /* If we haven't already, associate the ..._DECL node that we just made with
4742 the input GNAT entity node. */
4744 save_gnu_tree (gnat_entity, gnu_decl, false);
4746 /* If this is an enumeration or floating-point type, we were not able to set
4747 the bounds since they refer to the type. These are always static. */
4748 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4749 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4751 tree gnu_scalar_type = gnu_type;
4753 /* If this is a padded type, we need to use the underlying type. */
4754 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4755 && TYPE_IS_PADDING_P (gnu_scalar_type))
4756 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4758 /* If this is a floating point type and we haven't set a floating
4759 point type yet, use this in the evaluation of the bounds. */
4760 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4761 longest_float_type_node = gnu_scalar_type;
4763 TYPE_MIN_VALUE (gnu_scalar_type)
4764 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4765 TYPE_MAX_VALUE (gnu_scalar_type)
4766 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4768 /* For enumeration types, write full debugging information. */
4769 if (kind == E_Enumeration_Type)
4771 /* Since this has both a typedef and a tag, avoid outputting
4773 DECL_ARTIFICIAL (gnu_decl) = 1;
4774 rest_of_type_decl_compilation (gnu_decl);
4778 /* If we deferred processing of incomplete types, re-enable it. If there
4779 were no other disables and we have some to process, do so. */
4780 if (this_deferred && --defer_incomplete_level == 0)
4782 if (defer_incomplete_list)
4784 struct incomplete *incp, *next;
4786 /* We are back to level 0 for the deferring of incomplete types.
4787 But processing these incomplete types below may itself require
4788 deferring, so preserve what we have and restart from scratch. */
4789 incp = defer_incomplete_list;
4790 defer_incomplete_list = NULL;
4792 /* For finalization, however, all types must be complete so we
4793 cannot do the same because deferred incomplete types may end up
4794 referencing each other. Process them all recursively first. */
4795 defer_finalize_level++;
4797 for (; incp; incp = next)
4802 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4803 gnat_to_gnu_type (incp->full_type));
4807 defer_finalize_level--;
4810 /* All the deferred incomplete types have been processed so we can
4811 now proceed with the finalization of the deferred types. */
4812 if (defer_finalize_level == 0 && defer_finalize_list)
4817 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4818 rest_of_type_decl_compilation_no_defer (t);
4820 VEC_free (tree, heap, defer_finalize_list);
4824 /* If we are not defining this type, see if it's in the incomplete list.
4825 If so, handle that list entry now. */
4826 else if (!definition)
4828 struct incomplete *incp;
4830 for (incp = defer_incomplete_list; incp; incp = incp->next)
4831 if (incp->old_type && incp->full_type == gnat_entity)
4833 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4834 TREE_TYPE (gnu_decl));
4835 incp->old_type = NULL_TREE;
4842 /* If this is a packed array type whose original array type is itself
4843 an Itype without freeze node, make sure the latter is processed. */
4844 if (Is_Packed_Array_Type (gnat_entity)
4845 && Is_Itype (Original_Array_Type (gnat_entity))
4846 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4847 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4848 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
4853 /* Similar, but if the returned value is a COMPONENT_REF, return the
4857 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4859 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4861 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4862 gnu_field = TREE_OPERAND (gnu_field, 1);
4867 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4868 the GCC type corresponding to that entity. */
4871 gnat_to_gnu_type (Entity_Id gnat_entity)
4875 /* The back end never attempts to annotate generic types. */
4876 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4877 return void_type_node;
4879 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4880 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4882 return TREE_TYPE (gnu_decl);
4885 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4886 the unpadded version of the GCC type corresponding to that entity. */
4889 get_unpadded_type (Entity_Id gnat_entity)
4891 tree type = gnat_to_gnu_type (gnat_entity);
4893 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4894 type = TREE_TYPE (TYPE_FIELDS (type));
4899 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4900 Every TYPE_DECL generated for a type definition must be passed
4901 to this function once everything else has been done for it. */
4904 rest_of_type_decl_compilation (tree decl)
4906 /* We need to defer finalizing the type if incomplete types
4907 are being deferred or if they are being processed. */
4908 if (defer_incomplete_level || defer_finalize_level)
4909 VEC_safe_push (tree, heap, defer_finalize_list, decl);
4911 rest_of_type_decl_compilation_no_defer (decl);
4914 /* Same as above but without deferring the compilation. This
4915 function should not be invoked directly on a TYPE_DECL. */
4918 rest_of_type_decl_compilation_no_defer (tree decl)
4920 const int toplev = global_bindings_p ();
4921 tree t = TREE_TYPE (decl);
4923 rest_of_decl_compilation (decl, toplev, 0);
4925 /* Now process all the variants. This is needed for STABS. */
4926 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
4928 if (t == TREE_TYPE (decl))
4931 if (!TYPE_STUB_DECL (t))
4932 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
4934 rest_of_type_compilation (t, toplev);
4938 /* Finalize any From_With_Type incomplete types. We do this after processing
4939 our compilation unit and after processing its spec, if this is a body. */
4942 finalize_from_with_types (void)
4944 struct incomplete *incp = defer_limited_with;
4945 struct incomplete *next;
4947 defer_limited_with = 0;
4948 for (; incp; incp = next)
4952 if (incp->old_type != 0)
4953 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4954 gnat_to_gnu_type (incp->full_type));
4959 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4960 kind of type (such E_Task_Type) that has a different type which Gigi
4961 uses for its representation. If the type does not have a special type
4962 for its representation, return GNAT_ENTITY. If a type is supposed to
4963 exist, but does not, abort unless annotating types, in which case
4964 return Empty. If GNAT_ENTITY is Empty, return Empty. */
4967 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4969 Entity_Id gnat_equiv = gnat_entity;
4971 if (No (gnat_entity))
4974 switch (Ekind (gnat_entity))
4976 case E_Class_Wide_Subtype:
4977 if (Present (Equivalent_Type (gnat_entity)))
4978 gnat_equiv = Equivalent_Type (gnat_entity);
4981 case E_Access_Protected_Subprogram_Type:
4982 case E_Anonymous_Access_Protected_Subprogram_Type:
4983 gnat_equiv = Equivalent_Type (gnat_entity);
4986 case E_Class_Wide_Type:
4987 gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4988 ? Equivalent_Type (gnat_entity)
4989 : Root_Type (gnat_entity));
4993 case E_Task_Subtype:
4994 case E_Protected_Type:
4995 case E_Protected_Subtype:
4996 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5003 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5007 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5008 using MECH as its passing mechanism, to be placed in the parameter
5009 list built for GNAT_SUBPROG. Assume a foreign convention for the
5010 latter if FOREIGN is true. Also set CICO to true if the parameter
5011 must use the copy-in copy-out implementation mechanism.
5013 The returned tree is a PARM_DECL, except for those cases where no
5014 parameter needs to be actually passed to the subprogram; the type
5015 of this "shadow" parameter is then returned instead. */
5018 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5019 Entity_Id gnat_subprog, bool foreign, bool *cico)
5021 tree gnu_param_name = get_entity_name (gnat_param);
5022 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5023 tree gnu_param_type_alt = NULL_TREE;
5024 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5025 /* The parameter can be indirectly modified if its address is taken. */
5026 bool ro_param = in_param && !Address_Taken (gnat_param);
5027 bool by_return = false, by_component_ptr = false, by_ref = false;
5030 /* Copy-return is used only for the first parameter of a valued procedure.
5031 It's a copy mechanism for which a parameter is never allocated. */
5032 if (mech == By_Copy_Return)
5034 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5039 /* If this is either a foreign function or if the underlying type won't
5040 be passed by reference, strip off possible padding type. */
5041 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
5042 && TYPE_IS_PADDING_P (gnu_param_type))
5044 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5046 if (mech == By_Reference
5048 || (!must_pass_by_ref (unpadded_type)
5049 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5050 gnu_param_type = unpadded_type;
5053 /* If this is a read-only parameter, make a variant of the type that is
5054 read-only. ??? However, if this is an unconstrained array, that type
5055 can be very complex, so skip it for now. Likewise for any other
5056 self-referential type. */
5058 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5059 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5060 gnu_param_type = build_qualified_type (gnu_param_type,
5061 (TYPE_QUALS (gnu_param_type)
5062 | TYPE_QUAL_CONST));
5064 /* For foreign conventions, pass arrays as pointers to the element type.
5065 First check for unconstrained array and get the underlying array. */
5066 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5068 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5070 /* VMS descriptors are themselves passed by reference. */
5071 if (mech == By_Short_Descriptor ||
5072 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5074 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5075 Mechanism (gnat_param),
5077 else if (mech == By_Descriptor)
5079 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5080 chosen in fill_vms_descriptor. */
5082 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5083 Mechanism (gnat_param),
5086 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5087 Mechanism (gnat_param),
5091 /* Arrays are passed as pointers to element type for foreign conventions. */
5094 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5096 /* Strip off any multi-dimensional entries, then strip
5097 off the last array to get the component type. */
5098 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5099 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5100 gnu_param_type = TREE_TYPE (gnu_param_type);
5102 by_component_ptr = true;
5103 gnu_param_type = TREE_TYPE (gnu_param_type);
5106 gnu_param_type = build_qualified_type (gnu_param_type,
5107 (TYPE_QUALS (gnu_param_type)
5108 | TYPE_QUAL_CONST));
5110 gnu_param_type = build_pointer_type (gnu_param_type);
5113 /* Fat pointers are passed as thin pointers for foreign conventions. */
5114 else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
5116 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5118 /* If we must pass or were requested to pass by reference, do so.
5119 If we were requested to pass by copy, do so.
5120 Otherwise, for foreign conventions, pass In Out or Out parameters
5121 or aggregates by reference. For COBOL and Fortran, pass all
5122 integer and FP types that way too. For Convention Ada, use
5123 the standard Ada default. */
5124 else if (must_pass_by_ref (gnu_param_type)
5125 || mech == By_Reference
5128 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5130 && (Convention (gnat_subprog) == Convention_Fortran
5131 || Convention (gnat_subprog) == Convention_COBOL)
5132 && (INTEGRAL_TYPE_P (gnu_param_type)
5133 || FLOAT_TYPE_P (gnu_param_type)))
5135 && default_pass_by_ref (gnu_param_type)))))
5137 gnu_param_type = build_reference_type (gnu_param_type);
5141 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5145 if (mech == By_Copy && (by_ref || by_component_ptr))
5146 post_error ("?cannot pass & by copy", gnat_param);
5148 /* If this is an Out parameter that isn't passed by reference and isn't
5149 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5150 it will be a VAR_DECL created when we process the procedure, so just
5151 return its type. For the special parameter of a valued procedure,
5154 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5155 Out parameters with discriminants or implicit initial values to be
5156 handled like In Out parameters. These type are normally built as
5157 aggregates, hence passed by reference, except for some packed arrays
5158 which end up encoded in special integer types.
5160 The exception we need to make is then for packed arrays of records
5161 with discriminants or implicit initial values. We have no light/easy
5162 way to check for the latter case, so we merely check for packed arrays
5163 of records. This may lead to useless copy-in operations, but in very
5164 rare cases only, as these would be exceptions in a set of already
5165 exceptional situations. */
5166 if (Ekind (gnat_param) == E_Out_Parameter
5169 || (mech != By_Descriptor
5170 && mech != By_Short_Descriptor
5171 && !POINTER_TYPE_P (gnu_param_type)
5172 && !AGGREGATE_TYPE_P (gnu_param_type)))
5173 && !(Is_Array_Type (Etype (gnat_param))
5174 && Is_Packed (Etype (gnat_param))
5175 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5176 return gnu_param_type;
5178 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5179 ro_param || by_ref || by_component_ptr);
5180 DECL_BY_REF_P (gnu_param) = by_ref;
5181 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5182 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5183 mech == By_Short_Descriptor);
5184 DECL_POINTS_TO_READONLY_P (gnu_param)
5185 = (ro_param && (by_ref || by_component_ptr));
5187 /* Save the alternate descriptor type, if any. */
5188 if (gnu_param_type_alt)
5189 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5191 /* If no Mechanism was specified, indicate what we're using, then
5192 back-annotate it. */
5193 if (mech == Default)
5194 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5196 Set_Mechanism (gnat_param, mech);
5200 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5203 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5205 while (Present (Corresponding_Discriminant (discr1)))
5206 discr1 = Corresponding_Discriminant (discr1);
5208 while (Present (Corresponding_Discriminant (discr2)))
5209 discr2 = Corresponding_Discriminant (discr2);
5212 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5215 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
5216 a non-aliased component in the back-end sense. */
5219 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
5221 /* If the type below this is a multi-array type, then
5222 this does not have aliased components. */
5223 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5224 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5227 if (Has_Aliased_Components (gnat_type))
5230 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5233 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5236 compile_time_known_address_p (Node_Id gnat_address)
5238 /* Catch System'To_Address. */
5239 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5240 gnat_address = Expression (gnat_address);
5242 return Compile_Time_Known_Value (gnat_address);
5245 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5246 be elaborated at the point of its definition, but do nothing else. */
5249 elaborate_entity (Entity_Id gnat_entity)
5251 switch (Ekind (gnat_entity))
5253 case E_Signed_Integer_Subtype:
5254 case E_Modular_Integer_Subtype:
5255 case E_Enumeration_Subtype:
5256 case E_Ordinary_Fixed_Point_Subtype:
5257 case E_Decimal_Fixed_Point_Subtype:
5258 case E_Floating_Point_Subtype:
5260 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5261 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5263 /* ??? Tests to avoid Constraint_Error in static expressions
5264 are needed until after the front stops generating bogus
5265 conversions on bounds of real types. */
5266 if (!Raises_Constraint_Error (gnat_lb))
5267 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5268 1, 0, Needs_Debug_Info (gnat_entity));
5269 if (!Raises_Constraint_Error (gnat_hb))
5270 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5271 1, 0, Needs_Debug_Info (gnat_entity));
5277 Node_Id full_definition = Declaration_Node (gnat_entity);
5278 Node_Id record_definition = Type_Definition (full_definition);
5280 /* If this is a record extension, go a level further to find the
5281 record definition. */
5282 if (Nkind (record_definition) == N_Derived_Type_Definition)
5283 record_definition = Record_Extension_Part (record_definition);
5287 case E_Record_Subtype:
5288 case E_Private_Subtype:
5289 case E_Limited_Private_Subtype:
5290 case E_Record_Subtype_With_Private:
5291 if (Is_Constrained (gnat_entity)
5292 && Has_Discriminants (Base_Type (gnat_entity))
5293 && Present (Discriminant_Constraint (gnat_entity)))
5295 Node_Id gnat_discriminant_expr;
5296 Entity_Id gnat_field;
5298 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
5299 gnat_discriminant_expr
5300 = First_Elmt (Discriminant_Constraint (gnat_entity));
5301 Present (gnat_field);
5302 gnat_field = Next_Discriminant (gnat_field),
5303 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5304 /* ??? For now, ignore access discriminants. */
5305 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5306 elaborate_expression (Node (gnat_discriminant_expr),
5308 get_entity_name (gnat_field), 1, 0, 0);
5315 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5316 any entities on its entity chain similarly. */
5319 mark_out_of_scope (Entity_Id gnat_entity)
5321 Entity_Id gnat_sub_entity;
5322 unsigned int kind = Ekind (gnat_entity);
5324 /* If this has an entity list, process all in the list. */
5325 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5326 || IN (kind, Private_Kind)
5327 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5328 || kind == E_Function || kind == E_Generic_Function
5329 || kind == E_Generic_Package || kind == E_Generic_Procedure
5330 || kind == E_Loop || kind == E_Operator || kind == E_Package
5331 || kind == E_Package_Body || kind == E_Procedure
5332 || kind == E_Record_Type || kind == E_Record_Subtype
5333 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5334 for (gnat_sub_entity = First_Entity (gnat_entity);
5335 Present (gnat_sub_entity);
5336 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5337 if (Scope (gnat_sub_entity) == gnat_entity
5338 && gnat_sub_entity != gnat_entity)
5339 mark_out_of_scope (gnat_sub_entity);
5341 /* Now clear this if it has been defined, but only do so if it isn't
5342 a subprogram or parameter. We could refine this, but it isn't
5343 worth it. If this is statically allocated, it is supposed to
5344 hang around out of cope. */
5345 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5346 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5348 save_gnu_tree (gnat_entity, NULL_TREE, true);
5349 save_gnu_tree (gnat_entity, error_mark_node, true);
5353 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5354 If this is a multi-dimensional array type, do this recursively.
5357 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5358 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5359 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5362 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5364 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5365 of a one-dimensional array, since the padding has the same alias set
5366 as the field type, but if it's a multi-dimensional array, we need to
5367 see the inner types. */
5368 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5369 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5370 || TYPE_IS_PADDING_P (gnu_old_type)))
5371 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5373 /* Unconstrained array types are deemed incomplete and would thus be given
5374 alias set 0. Retrieve the underlying array type. */
5375 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5377 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5378 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5380 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5382 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5383 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5384 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5385 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5389 case ALIAS_SET_COPY:
5390 /* The alias set shouldn't be copied between array types with different
5391 aliasing settings because this can break the aliasing relationship
5392 between the array type and its element type. */
5393 #ifndef ENABLE_CHECKING
5394 if (flag_strict_aliasing)
5396 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5397 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5398 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5399 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5401 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5404 case ALIAS_SET_SUBSET:
5405 case ALIAS_SET_SUPERSET:
5407 alias_set_type old_set = get_alias_set (gnu_old_type);
5408 alias_set_type new_set = get_alias_set (gnu_new_type);
5410 /* Do nothing if the alias sets conflict. This ensures that we
5411 never call record_alias_subset several times for the same pair
5412 or at all for alias set 0. */
5413 if (!alias_sets_conflict_p (old_set, new_set))
5415 if (op == ALIAS_SET_SUBSET)
5416 record_alias_subset (old_set, new_set);
5418 record_alias_subset (new_set, old_set);
5427 record_component_aliases (gnu_new_type);
5430 /* Return a TREE_LIST describing the substitutions needed to reflect
5431 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
5432 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
5433 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
5434 gives the tree for the discriminant and TREE_VALUES is the replacement
5435 value. They are in the form of operands to substitute_in_expr.
5436 DEFINITION is as in gnat_to_gnu_entity. */
5439 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
5440 tree gnu_list, bool definition)
5442 Entity_Id gnat_discrim;
5446 gnat_type = Implementation_Base_Type (gnat_subtype);
5448 if (Has_Discriminants (gnat_type))
5449 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
5450 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
5451 Present (gnat_discrim);
5452 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
5453 gnat_value = Next_Elmt (gnat_value))
5454 /* Ignore access discriminants. */
5455 if (!Is_Access_Type (Etype (Node (gnat_value))))
5456 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
5457 elaborate_expression
5458 (Node (gnat_value), gnat_subtype,
5459 get_entity_name (gnat_discrim), definition,
5466 /* Return true if the size represented by GNU_SIZE can be handled by an
5467 allocation. If STATIC_P is true, consider only what can be done with a
5468 static allocation. */
5471 allocatable_size_p (tree gnu_size, bool static_p)
5473 HOST_WIDE_INT our_size;
5475 /* If this is not a static allocation, the only case we want to forbid
5476 is an overflowing size. That will be converted into a raise a
5479 return !(TREE_CODE (gnu_size) == INTEGER_CST
5480 && TREE_OVERFLOW (gnu_size));
5482 /* Otherwise, we need to deal with both variable sizes and constant
5483 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5484 since assemblers may not like very large sizes. */
5485 if (!host_integerp (gnu_size, 1))
5488 our_size = tree_low_cst (gnu_size, 1);
5489 return (int) our_size == our_size;
5492 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5493 NAME, ARGS and ERROR_POINT. */
5496 prepend_one_attribute_to (struct attrib ** attr_list,
5497 enum attr_type attr_type,
5500 Node_Id attr_error_point)
5502 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5504 attr->type = attr_type;
5505 attr->name = attr_name;
5506 attr->args = attr_args;
5507 attr->error_point = attr_error_point;
5509 attr->next = *attr_list;
5513 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5516 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5520 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5521 gnat_temp = Next_Rep_Item (gnat_temp))
5522 if (Nkind (gnat_temp) == N_Pragma)
5524 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5525 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5526 enum attr_type etype;
5528 if (Present (gnat_assoc) && Present (First (gnat_assoc))
5529 && Present (Next (First (gnat_assoc)))
5530 && (Nkind (Expression (Next (First (gnat_assoc))))
5531 == N_String_Literal))
5533 gnu_arg0 = get_identifier (TREE_STRING_POINTER
5536 (First (gnat_assoc))))));
5537 if (Present (Next (Next (First (gnat_assoc))))
5538 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5539 == N_String_Literal))
5540 gnu_arg1 = get_identifier (TREE_STRING_POINTER
5544 (First (gnat_assoc)))))));
5547 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5549 case Pragma_Machine_Attribute:
5550 etype = ATTR_MACHINE_ATTRIBUTE;
5553 case Pragma_Linker_Alias:
5554 etype = ATTR_LINK_ALIAS;
5557 case Pragma_Linker_Section:
5558 etype = ATTR_LINK_SECTION;
5561 case Pragma_Linker_Constructor:
5562 etype = ATTR_LINK_CONSTRUCTOR;
5565 case Pragma_Linker_Destructor:
5566 etype = ATTR_LINK_DESTRUCTOR;
5569 case Pragma_Weak_External:
5570 etype = ATTR_WEAK_EXTERNAL;
5573 case Pragma_Thread_Local_Storage:
5574 etype = ATTR_THREAD_LOCAL_STORAGE;
5582 /* Prepend to the list now. Make a list of the argument we might
5583 have, as GCC expects it. */
5584 prepend_one_attribute_to
5587 (gnu_arg1 != NULL_TREE)
5588 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5589 Present (Next (First (gnat_assoc)))
5590 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5594 /* Called when we need to protect a variable object using a save_expr. */
5597 maybe_variable (tree gnu_operand)
5599 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5600 || TREE_CODE (gnu_operand) == SAVE_EXPR
5601 || TREE_CODE (gnu_operand) == NULL_EXPR)
5604 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5606 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5607 TREE_TYPE (gnu_operand),
5608 variable_size (TREE_OPERAND (gnu_operand, 0)));
5610 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5611 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5615 return variable_size (gnu_operand);
5618 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5619 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5620 return the GCC tree to use for that expression. GNU_NAME is the
5621 qualification to use if an external name is appropriate and DEFINITION is
5622 true if this is a definition of GNAT_ENTITY. If NEED_VALUE is true, we
5623 need a result. Otherwise, we are just elaborating this for side-effects.
5624 If NEED_DEBUG is true we need the symbol for debugging purposes even if it
5625 isn't needed for code generation. */
5628 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5629 tree gnu_name, bool definition, bool need_value,
5634 /* If we already elaborated this expression (e.g., it was involved
5635 in the definition of a private type), use the old value. */
5636 if (present_gnu_tree (gnat_expr))
5637 return get_gnu_tree (gnat_expr);
5639 /* If we don't need a value and this is static or a discriminant, we
5640 don't need to do anything. */
5641 else if (!need_value
5642 && (Is_OK_Static_Expression (gnat_expr)
5643 || (Nkind (gnat_expr) == N_Identifier
5644 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5647 /* Otherwise, convert this tree to its GCC equivalent. */
5649 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5650 gnu_name, definition, need_debug);
5652 /* Save the expression in case we try to elaborate this entity again. Since
5653 it's not a DECL, don't check it. Don't save if it's a discriminant. */
5654 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5655 save_gnu_tree (gnat_expr, gnu_expr, true);
5657 return need_value ? gnu_expr : error_mark_node;
5660 /* Similar, but take a GNU expression. */
5663 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5664 tree gnu_expr, tree gnu_name, bool definition,
5667 tree gnu_decl = NULL_TREE;
5668 /* Skip any conversions and simple arithmetics to see if the expression
5669 is a read-only variable.
5670 ??? This really should remain read-only, but we have to think about
5671 the typing of the tree here. */
5673 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5674 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5677 /* In most cases, we won't see a naked FIELD_DECL here because a
5678 discriminant reference will have been replaced with a COMPONENT_REF
5679 when the type is being elaborated. However, there are some cases
5680 involving child types where we will. So convert it to a COMPONENT_REF
5681 here. We have to hope it will be at the highest level of the
5682 expression in these cases. */
5683 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5684 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5685 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5686 gnu_expr, NULL_TREE);
5688 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5689 that is read-only, make a variable that is initialized to contain the
5690 bound when the package containing the definition is elaborated. If
5691 this entity is defined at top level and a bound or discriminant value
5692 isn't a constant or a reference to a discriminant, replace the bound
5693 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5694 rely here on the fact that an expression cannot contain both the
5695 discriminant and some other variable. */
5697 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5698 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5699 && (TREE_READONLY (gnu_inner_expr)
5700 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5701 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5703 /* If this is a static expression or contains a discriminant, we don't
5704 need the variable for debugging (and can't elaborate anyway if a
5707 && (Is_OK_Static_Expression (gnat_expr)
5708 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5711 /* Now create the variable if we need it. */
5712 if (need_debug || (expr_variable && expr_global))
5714 = create_var_decl (create_concat_name (gnat_entity,
5715 IDENTIFIER_POINTER (gnu_name)),
5716 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5717 !need_debug, Is_Public (gnat_entity),
5718 !definition, false, NULL, gnat_entity);
5720 /* We only need to use this variable if we are in global context since GCC
5721 can do the right thing in the local case. */
5722 if (expr_global && expr_variable)
5724 else if (!expr_variable)
5727 return maybe_variable (gnu_expr);
5730 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5731 starting bit position so that it is aligned to ALIGN bits, and leaving at
5732 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5733 record is guaranteed to get. */
5736 make_aligning_type (tree type, unsigned int align, tree size,
5737 unsigned int base_align, int room)
5739 /* We will be crafting a record type with one field at a position set to be
5740 the next multiple of ALIGN past record'address + room bytes. We use a
5741 record placeholder to express record'address. */
5743 tree record_type = make_node (RECORD_TYPE);
5744 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5747 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5749 /* The diagram below summarizes the shape of what we manipulate:
5751 <--------- pos ---------->
5752 { +------------+-------------+-----------------+
5753 record =>{ |############| ... | field (type) |
5754 { +------------+-------------+-----------------+
5755 |<-- room -->|<- voffset ->|<---- size ----->|
5758 record_addr vblock_addr
5760 Every length is in sizetype bytes there, except "pos" which has to be
5761 set as a bit position in the GCC tree for the record. */
5763 tree room_st = size_int (room);
5764 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5765 tree voffset_st, pos, field;
5767 tree name = TYPE_NAME (type);
5769 if (TREE_CODE (name) == TYPE_DECL)
5770 name = DECL_NAME (name);
5772 TYPE_NAME (record_type) = concat_name (name, "_ALIGN");
5774 /* Compute VOFFSET and then POS. The next byte position multiple of some
5775 alignment after some address is obtained by "and"ing the alignment minus
5776 1 with the two's complement of the address. */
5778 voffset_st = size_binop (BIT_AND_EXPR,
5779 size_diffop (size_zero_node, vblock_addr_st),
5780 ssize_int ((align / BITS_PER_UNIT) - 1));
5782 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5784 pos = size_binop (MULT_EXPR,
5785 convert (bitsizetype,
5786 size_binop (PLUS_EXPR, room_st, voffset_st)),
5789 /* Craft the GCC record representation. We exceptionally do everything
5790 manually here because 1) our generic circuitry is not quite ready to
5791 handle the complex position/size expressions we are setting up, 2) we
5792 have a strong simplifying factor at hand: we know the maximum possible
5793 value of voffset, and 3) we have to set/reset at least the sizes in
5794 accordance with this maximum value anyway, as we need them to convey
5795 what should be "alloc"ated for this type.
5797 Use -1 as the 'addressable' indication for the field to prevent the
5798 creation of a bitfield. We don't need one, it would have damaging
5799 consequences on the alignment computation, and create_field_decl would
5800 make one without this special argument, for instance because of the
5801 complex position expression. */
5803 field = create_field_decl (get_identifier ("F"), type, record_type,
5805 TYPE_FIELDS (record_type) = field;
5807 TYPE_ALIGN (record_type) = base_align;
5808 TYPE_USER_ALIGN (record_type) = 1;
5810 TYPE_SIZE (record_type)
5811 = size_binop (PLUS_EXPR,
5812 size_binop (MULT_EXPR, convert (bitsizetype, size),
5814 bitsize_int (align + room * BITS_PER_UNIT));
5815 TYPE_SIZE_UNIT (record_type)
5816 = size_binop (PLUS_EXPR, size,
5817 size_int (room + align / BITS_PER_UNIT));
5819 SET_TYPE_MODE (record_type, BLKmode);
5821 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
5825 /* Return the result of rounding T up to ALIGN. */
5827 static inline unsigned HOST_WIDE_INT
5828 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5836 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
5837 as the field type of a packed record if IN_RECORD is true, or as the
5838 component type of a packed array if IN_RECORD is false. See if we can
5839 rewrite it either as a type that has a non-BLKmode, which we can pack
5840 tighter in the packed record case, or as a smaller type. If so, return
5841 the new type. If not, return the original type. */
5844 make_packable_type (tree type, bool in_record)
5846 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5847 unsigned HOST_WIDE_INT new_size;
5848 tree new_type, old_field, field_list = NULL_TREE;
5850 /* No point in doing anything if the size is zero. */
5854 new_type = make_node (TREE_CODE (type));
5856 /* Copy the name and flags from the old type to that of the new.
5857 Note that we rely on the pointer equality created here for
5858 TYPE_NAME to look through conversions in various places. */
5859 TYPE_NAME (new_type) = TYPE_NAME (type);
5860 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5861 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5862 if (TREE_CODE (type) == RECORD_TYPE)
5863 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5865 /* If we are in a record and have a small size, set the alignment to
5866 try for an integral mode. Otherwise set it to try for a smaller
5867 type with BLKmode. */
5868 if (in_record && size <= MAX_FIXED_MODE_SIZE)
5870 TYPE_ALIGN (new_type) = ceil_alignment (size);
5871 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5875 unsigned HOST_WIDE_INT align;
5877 /* Do not try to shrink the size if the RM size is not constant. */
5878 if (TYPE_CONTAINS_TEMPLATE_P (type)
5879 || !host_integerp (TYPE_ADA_SIZE (type), 1))
5882 /* Round the RM size up to a unit boundary to get the minimal size
5883 for a BLKmode record. Give up if it's already the size. */
5884 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5885 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5886 if (new_size == size)
5889 align = new_size & -new_size;
5890 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5893 TYPE_USER_ALIGN (new_type) = 1;
5895 /* Now copy the fields, keeping the position and size as we don't want
5896 to change the layout by propagating the packedness downwards. */
5897 for (old_field = TYPE_FIELDS (type); old_field;
5898 old_field = TREE_CHAIN (old_field))
5900 tree new_field_type = TREE_TYPE (old_field);
5901 tree new_field, new_size;
5903 if ((TREE_CODE (new_field_type) == RECORD_TYPE
5904 || TREE_CODE (new_field_type) == UNION_TYPE
5905 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5906 && !TYPE_IS_FAT_POINTER_P (new_field_type)
5907 && host_integerp (TYPE_SIZE (new_field_type), 1))
5908 new_field_type = make_packable_type (new_field_type, true);
5910 /* However, for the last field in a not already packed record type
5911 that is of an aggregate type, we need to use the RM size in the
5912 packable version of the record type, see finish_record_type. */
5913 if (!TREE_CHAIN (old_field)
5914 && !TYPE_PACKED (type)
5915 && (TREE_CODE (new_field_type) == RECORD_TYPE
5916 || TREE_CODE (new_field_type) == UNION_TYPE
5917 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5918 && !TYPE_IS_FAT_POINTER_P (new_field_type)
5919 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
5920 && TYPE_ADA_SIZE (new_field_type))
5921 new_size = TYPE_ADA_SIZE (new_field_type);
5923 new_size = DECL_SIZE (old_field);
5925 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5926 new_type, TYPE_PACKED (type), new_size,
5927 bit_position (old_field),
5928 !DECL_NONADDRESSABLE_P (old_field));
5930 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5931 SET_DECL_ORIGINAL_FIELD
5932 (new_field, (DECL_ORIGINAL_FIELD (old_field)
5933 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5935 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5936 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5938 TREE_CHAIN (new_field) = field_list;
5939 field_list = new_field;
5942 finish_record_type (new_type, nreverse (field_list), 2, true);
5943 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
5945 /* If this is a padding record, we never want to make the size smaller
5946 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
5947 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5948 || TREE_CODE (type) == QUAL_UNION_TYPE)
5950 TYPE_SIZE (new_type) = TYPE_SIZE (type);
5951 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5955 TYPE_SIZE (new_type) = bitsize_int (new_size);
5956 TYPE_SIZE_UNIT (new_type)
5957 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5960 if (!TYPE_CONTAINS_TEMPLATE_P (type))
5961 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5963 compute_record_mode (new_type);
5965 /* Try harder to get a packable type if necessary, for example
5966 in case the record itself contains a BLKmode field. */
5967 if (in_record && TYPE_MODE (new_type) == BLKmode)
5968 SET_TYPE_MODE (new_type,
5969 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
5971 /* If neither the mode nor the size has shrunk, return the old type. */
5972 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5978 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5979 if needed. We have already verified that SIZE and TYPE are large enough.
5981 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5984 IS_USER_TYPE is true if we must complete the original type.
5986 DEFINITION is true if this type is being defined.
5988 SAME_RM_SIZE is true if the RM size of the resulting type is to be set
5989 to SIZE too; otherwise, it's set to the RM size of the original type. */
5992 maybe_pad_type (tree type, tree size, unsigned int align,
5993 Entity_Id gnat_entity, const char *name_trailer,
5994 bool is_user_type, bool definition, bool same_rm_size)
5996 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
5997 tree orig_size = TYPE_SIZE (type);
5998 unsigned int orig_align = align;
6001 /* If TYPE is a padded type, see if it agrees with any size and alignment
6002 we were given. If so, return the original type. Otherwise, strip
6003 off the padding, since we will either be returning the inner type
6004 or repadding it. If no size or alignment is specified, use that of
6005 the original padded type. */
6006 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
6009 || operand_equal_p (round_up (size,
6010 MAX (align, TYPE_ALIGN (type))),
6011 round_up (TYPE_SIZE (type),
6012 MAX (align, TYPE_ALIGN (type))),
6014 && (align == 0 || align == TYPE_ALIGN (type)))
6018 size = TYPE_SIZE (type);
6020 align = TYPE_ALIGN (type);
6022 type = TREE_TYPE (TYPE_FIELDS (type));
6023 orig_size = TYPE_SIZE (type);
6026 /* If the size is either not being changed or is being made smaller (which
6027 is not done here and is only valid for bitfields anyway), show the size
6028 isn't changing. Likewise, clear the alignment if it isn't being
6029 changed. Then return if we aren't doing anything. */
6031 && (operand_equal_p (size, orig_size, 0)
6032 || (TREE_CODE (orig_size) == INTEGER_CST
6033 && tree_int_cst_lt (size, orig_size))))
6036 if (align == TYPE_ALIGN (type))
6039 if (align == 0 && !size)
6042 /* If requested, complete the original type and give it a name. */
6044 create_type_decl (get_entity_name (gnat_entity), type,
6045 NULL, !Comes_From_Source (gnat_entity),
6047 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6048 && DECL_IGNORED_P (TYPE_NAME (type))),
6051 /* We used to modify the record in place in some cases, but that could
6052 generate incorrect debugging information. So make a new record
6054 record = make_node (RECORD_TYPE);
6055 TYPE_IS_PADDING_P (record) = 1;
6057 if (Present (gnat_entity))
6058 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
6060 TYPE_VOLATILE (record)
6061 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6063 TYPE_ALIGN (record) = align;
6065 TYPE_USER_ALIGN (record) = align;
6067 TYPE_SIZE (record) = size ? size : orig_size;
6068 TYPE_SIZE_UNIT (record)
6069 = convert (sizetype,
6070 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6071 bitsize_unit_node));
6073 /* If we are changing the alignment and the input type is a record with
6074 BLKmode and a small constant size, try to make a form that has an
6075 integral mode. This might allow the padding record to also have an
6076 integral mode, which will be much more efficient. There is no point
6077 in doing so if a size is specified unless it is also a small constant
6078 size and it is incorrect to do so if we cannot guarantee that the mode
6079 will be naturally aligned since the field must always be addressable.
6081 ??? This might not always be a win when done for a stand-alone object:
6082 since the nominal and the effective type of the object will now have
6083 different modes, a VIEW_CONVERT_EXPR will be required for converting
6084 between them and it might be hard to overcome afterwards, including
6085 at the RTL level when the stand-alone object is accessed as a whole. */
6087 && TREE_CODE (type) == RECORD_TYPE
6088 && TYPE_MODE (type) == BLKmode
6089 && TREE_CODE (orig_size) == INTEGER_CST
6090 && !TREE_OVERFLOW (orig_size)
6091 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6093 || (TREE_CODE (size) == INTEGER_CST
6094 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6096 tree packable_type = make_packable_type (type, true);
6097 if (TYPE_MODE (packable_type) != BLKmode
6098 && align >= TYPE_ALIGN (packable_type))
6099 type = packable_type;
6102 /* Now create the field with the original size. */
6103 field = create_field_decl (get_identifier ("F"), type, record, 0,
6104 orig_size, bitsize_zero_node, 1);
6105 DECL_INTERNAL_P (field) = 1;
6107 /* Do not finalize it until after the auxiliary record is built. */
6108 finish_record_type (record, field, 1, true);
6110 /* Set the same size for its RM size if requested; otherwise reuse
6111 the RM size of the original type. */
6112 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6114 /* Unless debugging information isn't being written for the input type,
6115 write a record that shows what we are a subtype of and also make a
6116 variable that indicates our size, if still variable. */
6117 if (TYPE_NAME (record)
6118 && AGGREGATE_TYPE_P (type)
6119 && TREE_CODE (orig_size) != INTEGER_CST
6120 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6121 && DECL_IGNORED_P (TYPE_NAME (type))))
6123 tree marker = make_node (RECORD_TYPE);
6124 tree name = TYPE_NAME (record);
6125 tree orig_name = TYPE_NAME (type);
6127 if (TREE_CODE (name) == TYPE_DECL)
6128 name = DECL_NAME (name);
6130 if (TREE_CODE (orig_name) == TYPE_DECL)
6131 orig_name = DECL_NAME (orig_name);
6133 TYPE_NAME (marker) = concat_name (name, "XVS");
6134 finish_record_type (marker,
6135 create_field_decl (orig_name, integer_type_node,
6136 marker, 0, NULL_TREE, NULL_TREE,
6140 add_parallel_type (TYPE_STUB_DECL (record), marker);
6142 if (size && TREE_CODE (size) != INTEGER_CST && definition)
6143 create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6144 TYPE_SIZE_UNIT (record), false, false, false,
6145 false, NULL, gnat_entity);
6148 rest_of_record_type_compilation (record);
6150 /* If the size was widened explicitly, maybe give a warning. Take the
6151 original size as the maximum size of the input if there was an
6152 unconstrained record involved and round it up to the specified alignment,
6153 if one was specified. */
6154 if (CONTAINS_PLACEHOLDER_P (orig_size))
6155 orig_size = max_size (orig_size, true);
6158 orig_size = round_up (orig_size, align);
6160 if (size && Present (gnat_entity)
6161 && !operand_equal_p (size, orig_size, 0)
6162 && !(TREE_CODE (size) == INTEGER_CST
6163 && TREE_CODE (orig_size) == INTEGER_CST
6164 && tree_int_cst_lt (size, orig_size)))
6166 Node_Id gnat_error_node = Empty;
6168 if (Is_Packed_Array_Type (gnat_entity))
6169 gnat_entity = Original_Array_Type (gnat_entity);
6171 if ((Ekind (gnat_entity) == E_Component
6172 || Ekind (gnat_entity) == E_Discriminant)
6173 && Present (Component_Clause (gnat_entity)))
6174 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6175 else if (Present (Size_Clause (gnat_entity)))
6176 gnat_error_node = Expression (Size_Clause (gnat_entity));
6178 /* Generate message only for entities that come from source, since
6179 if we have an entity created by expansion, the message will be
6180 generated for some other corresponding source entity. */
6181 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
6182 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
6184 size_diffop (size, orig_size));
6186 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
6187 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6188 gnat_entity, gnat_entity,
6189 size_diffop (size, orig_size));
6195 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6196 the value passed against the list of choices. */
6199 choices_to_gnu (tree operand, Node_Id choices)
6203 tree result = integer_zero_node;
6204 tree this_test, low = 0, high = 0, single = 0;
6206 for (choice = First (choices); Present (choice); choice = Next (choice))
6208 switch (Nkind (choice))
6211 low = gnat_to_gnu (Low_Bound (choice));
6212 high = gnat_to_gnu (High_Bound (choice));
6214 /* There's no good type to use here, so we might as well use
6215 integer_type_node. */
6217 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6218 build_binary_op (GE_EXPR, integer_type_node,
6220 build_binary_op (LE_EXPR, integer_type_node,
6225 case N_Subtype_Indication:
6226 gnat_temp = Range_Expression (Constraint (choice));
6227 low = gnat_to_gnu (Low_Bound (gnat_temp));
6228 high = gnat_to_gnu (High_Bound (gnat_temp));
6231 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6232 build_binary_op (GE_EXPR, integer_type_node,
6234 build_binary_op (LE_EXPR, integer_type_node,
6239 case N_Expanded_Name:
6240 /* This represents either a subtype range, an enumeration
6241 literal, or a constant Ekind says which. If an enumeration
6242 literal or constant, fall through to the next case. */
6243 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6244 && Ekind (Entity (choice)) != E_Constant)
6246 tree type = gnat_to_gnu_type (Entity (choice));
6248 low = TYPE_MIN_VALUE (type);
6249 high = TYPE_MAX_VALUE (type);
6252 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6253 build_binary_op (GE_EXPR, integer_type_node,
6255 build_binary_op (LE_EXPR, integer_type_node,
6260 /* ... fall through ... */
6262 case N_Character_Literal:
6263 case N_Integer_Literal:
6264 single = gnat_to_gnu (choice);
6265 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
6269 case N_Others_Choice:
6270 this_test = integer_one_node;
6277 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6284 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6285 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6288 adjust_packed (tree field_type, tree record_type, int packed)
6290 /* If the field contains an item of variable size, we cannot pack it
6291 because we cannot create temporaries of non-fixed size in case
6292 we need to take the address of the field. See addressable_p and
6293 the notes on the addressability issues for further details. */
6294 if (is_variable_size (field_type))
6297 /* If the alignment of the record is specified and the field type
6298 is over-aligned, request Storage_Unit alignment for the field. */
6301 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6310 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6311 placed in GNU_RECORD_TYPE.
6313 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6314 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6315 record has a specified alignment.
6317 DEFINITION is true if this field is for a record being defined. */
6320 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6323 tree gnu_field_id = get_entity_name (gnat_field);
6324 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6325 tree gnu_field, gnu_size, gnu_pos;
6326 bool needs_strict_alignment
6327 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6328 || Treat_As_Volatile (gnat_field));
6330 /* If this field requires strict alignment, we cannot pack it because
6331 it would very likely be under-aligned in the record. */
6332 if (needs_strict_alignment)
6335 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6337 /* If a size is specified, use it. Otherwise, if the record type is packed,
6338 use the official RM size. See "Handling of Type'Size Values" in Einfo
6339 for further details. */
6340 if (Known_Static_Esize (gnat_field))
6341 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6342 gnat_field, FIELD_DECL, false, true);
6343 else if (packed == 1)
6344 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6345 gnat_field, FIELD_DECL, false, true);
6347 gnu_size = NULL_TREE;
6349 /* If we have a specified size that's smaller than that of the field type,
6350 or a position is specified, and the field type is a record, see if we can
6351 get either an integral mode form of the type or a smaller form. If we
6352 can, show a size was specified for the field if there wasn't one already,
6353 so we know to make this a bitfield and avoid making things wider.
6355 Doing this is first useful if the record is packed because we may then
6356 place the field at a non-byte-aligned position and so achieve tighter
6359 This is in addition *required* if the field shares a byte with another
6360 field and the front-end lets the back-end handle the references, because
6361 GCC does not handle BLKmode bitfields properly.
6363 We avoid the transformation if it is not required or potentially useful,
6364 as it might entail an increase of the field's alignment and have ripple
6365 effects on the outer record type. A typical case is a field known to be
6366 byte aligned and not to share a byte with another field.
6368 Besides, we don't even look the possibility of a transformation in cases
6369 known to be in error already, for instance when an invalid size results
6370 from a component clause. */
6372 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6373 && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
6374 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6377 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6378 || Present (Component_Clause (gnat_field))))))
6380 /* See what the alternate type and size would be. */
6381 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6383 bool has_byte_aligned_clause
6384 = Present (Component_Clause (gnat_field))
6385 && (UI_To_Int (Component_Bit_Offset (gnat_field))
6386 % BITS_PER_UNIT == 0);
6388 /* Compute whether we should avoid the substitution. */
6390 /* There is no point substituting if there is no change... */
6391 = (gnu_packable_type == gnu_field_type)
6392 /* ... nor when the field is known to be byte aligned and not to
6393 share a byte with another field. */
6394 || (has_byte_aligned_clause
6395 && value_factor_p (gnu_size, BITS_PER_UNIT))
6396 /* The size of an aliased field must be an exact multiple of the
6397 type's alignment, which the substitution might increase. Reject
6398 substitutions that would so invalidate a component clause when the
6399 specified position is byte aligned, as the change would have no
6400 real benefit from the packing standpoint anyway. */
6401 || (Is_Aliased (gnat_field)
6402 && has_byte_aligned_clause
6403 && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
6405 /* Substitute unless told otherwise. */
6408 gnu_field_type = gnu_packable_type;
6411 gnu_size = rm_size (gnu_field_type);
6415 /* If we are packing the record and the field is BLKmode, round the
6416 size up to a byte boundary. */
6417 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6418 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6420 if (Present (Component_Clause (gnat_field)))
6422 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6423 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6424 gnat_field, FIELD_DECL, false, true);
6426 /* Ensure the position does not overlap with the parent subtype,
6428 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
6431 = gnat_to_gnu_type (Parent_Subtype
6432 (Underlying_Type (Scope (gnat_field))));
6434 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6435 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6438 ("offset of& must be beyond parent{, minimum allowed is ^}",
6439 First_Bit (Component_Clause (gnat_field)), gnat_field,
6440 TYPE_SIZE_UNIT (gnu_parent));
6444 /* If this field needs strict alignment, ensure the record is
6445 sufficiently aligned and that that position and size are
6446 consistent with the alignment. */
6447 if (needs_strict_alignment)
6449 TYPE_ALIGN (gnu_record_type)
6450 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6453 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6455 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6457 ("atomic field& must be natural size of type{ (^)}",
6458 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6459 TYPE_SIZE (gnu_field_type));
6461 else if (Is_Aliased (gnat_field))
6463 ("size of aliased field& must be ^ bits",
6464 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6465 TYPE_SIZE (gnu_field_type));
6467 else if (Strict_Alignment (Etype (gnat_field)))
6469 ("size of & with aliased or tagged components not ^ bits",
6470 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6471 TYPE_SIZE (gnu_field_type));
6473 gnu_size = NULL_TREE;
6476 if (!integer_zerop (size_binop
6477 (TRUNC_MOD_EXPR, gnu_pos,
6478 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6480 if (Is_Aliased (gnat_field))
6482 ("position of aliased field& must be multiple of ^ bits",
6483 First_Bit (Component_Clause (gnat_field)), gnat_field,
6484 TYPE_ALIGN (gnu_field_type));
6486 else if (Treat_As_Volatile (gnat_field))
6488 ("position of volatile field& must be multiple of ^ bits",
6489 First_Bit (Component_Clause (gnat_field)), gnat_field,
6490 TYPE_ALIGN (gnu_field_type));
6492 else if (Strict_Alignment (Etype (gnat_field)))
6494 ("position of & with aliased or tagged components not multiple of ^ bits",
6495 First_Bit (Component_Clause (gnat_field)), gnat_field,
6496 TYPE_ALIGN (gnu_field_type));
6501 gnu_pos = NULL_TREE;
6505 if (Is_Atomic (gnat_field))
6506 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6509 /* If the record has rep clauses and this is the tag field, make a rep
6510 clause for it as well. */
6511 else if (Has_Specified_Layout (Scope (gnat_field))
6512 && Chars (gnat_field) == Name_uTag)
6514 gnu_pos = bitsize_zero_node;
6515 gnu_size = TYPE_SIZE (gnu_field_type);
6519 gnu_pos = NULL_TREE;
6521 /* We need to make the size the maximum for the type if it is
6522 self-referential and an unconstrained type. In that case, we can't
6523 pack the field since we can't make a copy to align it. */
6524 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6526 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6527 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6529 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6533 /* If a size is specified, adjust the field's type to it. */
6536 /* If the field's type is justified modular, we would need to remove
6537 the wrapper to (better) meet the layout requirements. However we
6538 can do so only if the field is not aliased to preserve the unique
6539 layout and if the prescribed size is not greater than that of the
6540 packed array to preserve the justification. */
6541 if (!needs_strict_alignment
6542 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6543 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6544 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6546 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6549 = make_type_from_size (gnu_field_type, gnu_size,
6550 Has_Biased_Representation (gnat_field));
6551 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6552 "PAD", false, definition, true);
6555 /* Otherwise (or if there was an error), don't specify a position. */
6557 gnu_pos = NULL_TREE;
6559 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6560 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6562 /* Now create the decl for the field. */
6563 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6564 packed, gnu_size, gnu_pos,
6565 Is_Aliased (gnat_field));
6566 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6567 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6569 if (Ekind (gnat_field) == E_Discriminant)
6570 DECL_DISCRIMINANT_NUMBER (gnu_field)
6571 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6576 /* Return true if TYPE is a type with variable size, a padding type with a
6577 field of variable size or is a record that has a field such a field. */
6580 is_variable_size (tree type)
6584 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6587 if (TREE_CODE (type) == RECORD_TYPE
6588 && TYPE_IS_PADDING_P (type)
6589 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6592 if (TREE_CODE (type) != RECORD_TYPE
6593 && TREE_CODE (type) != UNION_TYPE
6594 && TREE_CODE (type) != QUAL_UNION_TYPE)
6597 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6598 if (is_variable_size (TREE_TYPE (field)))
6604 /* qsort comparer for the bit positions of two record components. */
6607 compare_field_bitpos (const PTR rt1, const PTR rt2)
6609 const_tree const field1 = * (const_tree const *) rt1;
6610 const_tree const field2 = * (const_tree const *) rt2;
6612 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6614 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6617 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6618 of GCC trees for fields that are in the record and have already been
6619 processed. When called from gnat_to_gnu_entity during the processing of a
6620 record type definition, the GCC nodes for the discriminants will be on
6621 the chain. The other calls to this function are recursive calls from
6622 itself for the Component_List of a variant and the chain is empty.
6624 PACKED is 1 if this is for a packed record, -1 if this is for a record
6625 with Component_Alignment of Storage_Unit, -2 if this is for a record
6626 with a specified alignment.
6628 DEFINITION is true if we are defining this record.
6630 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6631 with a rep clause is to be added. If it is nonzero, that is all that
6632 should be done with such fields.
6634 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6635 laying out the record. This means the alignment only serves to force fields
6636 to be bitfields, but not require the record to be that aligned. This is
6639 ALL_REP, if true, means a rep clause was found for all the fields. This
6640 simplifies the logic since we know we're not in the mixed case.
6642 DO_NOT_FINALIZE, if true, means that the record type is expected to be
6643 modified afterwards so it will not be sent to the back-end for finalization.
6645 UNCHECKED_UNION, if true, means that we are building a type for a record
6646 with a Pragma Unchecked_Union.
6648 The processing of the component list fills in the chain with all of the
6649 fields of the record and then the record type is finished. */
6652 components_to_record (tree gnu_record_type, Node_Id component_list,
6653 tree gnu_field_list, int packed, bool definition,
6654 tree *p_gnu_rep_list, bool cancel_alignment,
6655 bool all_rep, bool do_not_finalize, bool unchecked_union)
6657 Node_Id component_decl;
6658 Entity_Id gnat_field;
6659 Node_Id variant_part;
6660 tree gnu_our_rep_list = NULL_TREE;
6661 tree gnu_field, gnu_last;
6662 bool layout_with_rep = false;
6663 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6665 /* For each variable within each component declaration create a GCC field
6666 and add it to the list, skipping any pragmas in the list. */
6667 if (Present (Component_Items (component_list)))
6668 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6669 Present (component_decl);
6670 component_decl = Next_Non_Pragma (component_decl))
6672 gnat_field = Defining_Entity (component_decl);
6674 if (Chars (gnat_field) == Name_uParent)
6675 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6678 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6679 packed, definition);
6681 /* If this is the _Tag field, put it before any discriminants,
6682 instead of after them as is the case for all other fields. */
6683 if (Chars (gnat_field) == Name_uTag)
6684 gnu_field_list = chainon (gnu_field_list, gnu_field);
6687 TREE_CHAIN (gnu_field) = gnu_field_list;
6688 gnu_field_list = gnu_field;
6692 save_gnu_tree (gnat_field, gnu_field, false);
6695 /* At the end of the component list there may be a variant part. */
6696 variant_part = Variant_Part (component_list);
6698 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6699 mutually exclusive and should go in the same memory. To do this we need
6700 to treat each variant as a record whose elements are created from the
6701 component list for the variant. So here we create the records from the
6702 lists for the variants and put them all into the QUAL_UNION_TYPE.
6703 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6704 use GNU_RECORD_TYPE if there are no fields so far. */
6705 if (Present (variant_part))
6707 Node_Id gnat_discr = Name (variant_part), variant;
6708 tree gnu_discr = gnat_to_gnu (gnat_discr);
6709 tree gnu_name = TYPE_NAME (gnu_record_type);
6711 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6713 tree gnu_union_type, gnu_union_name, gnu_union_field;
6714 tree gnu_variant_list = NULL_TREE;
6716 if (TREE_CODE (gnu_name) == TYPE_DECL)
6717 gnu_name = DECL_NAME (gnu_name);
6720 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6722 /* Reuse an enclosing union if all fields are in the variant part
6723 and there is no representation clause on the record, to match
6724 the layout of C unions. There is an associated check below. */
6726 && TREE_CODE (gnu_record_type) == UNION_TYPE
6727 && !TYPE_PACKED (gnu_record_type))
6728 gnu_union_type = gnu_record_type;
6732 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6734 TYPE_NAME (gnu_union_type) = gnu_union_name;
6735 TYPE_ALIGN (gnu_union_type) = 0;
6736 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6739 for (variant = First_Non_Pragma (Variants (variant_part));
6741 variant = Next_Non_Pragma (variant))
6743 tree gnu_variant_type = make_node (RECORD_TYPE);
6744 tree gnu_inner_name;
6747 Get_Variant_Encoding (variant);
6748 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
6749 TYPE_NAME (gnu_variant_type)
6750 = concat_name (gnu_union_name,
6751 IDENTIFIER_POINTER (gnu_inner_name));
6753 /* Set the alignment of the inner type in case we need to make
6754 inner objects into bitfields, but then clear it out
6755 so the record actually gets only the alignment required. */
6756 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6757 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6759 /* Similarly, if the outer record has a size specified and all fields
6760 have record rep clauses, we can propagate the size into the
6762 if (all_rep_and_size)
6764 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6765 TYPE_SIZE_UNIT (gnu_variant_type)
6766 = TYPE_SIZE_UNIT (gnu_record_type);
6769 /* Create the record type for the variant. Note that we defer
6770 finalizing it until after we are sure to actually use it. */
6771 components_to_record (gnu_variant_type, Component_List (variant),
6772 NULL_TREE, packed, definition,
6773 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6774 true, unchecked_union);
6776 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
6778 Set_Present_Expr (variant, annotate_value (gnu_qual));
6780 /* If this is an Unchecked_Union and we have exactly one field,
6781 use this field directly to match the layout of C unions. */
6783 && TYPE_FIELDS (gnu_variant_type)
6784 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6785 gnu_field = TYPE_FIELDS (gnu_variant_type);
6788 /* Deal with packedness like in gnat_to_gnu_field. */
6790 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6792 /* Finalize the record type now. We used to throw away
6793 empty records but we no longer do that because we need
6794 them to generate complete debug info for the variant;
6795 otherwise, the union type definition will be lacking
6796 the fields associated with these empty variants. */
6797 rest_of_record_type_compilation (gnu_variant_type);
6799 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6800 gnu_union_type, field_packed,
6802 ? TYPE_SIZE (gnu_variant_type)
6805 ? bitsize_zero_node : 0),
6808 DECL_INTERNAL_P (gnu_field) = 1;
6810 if (!unchecked_union)
6811 DECL_QUALIFIER (gnu_field) = gnu_qual;
6814 TREE_CHAIN (gnu_field) = gnu_variant_list;
6815 gnu_variant_list = gnu_field;
6818 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
6819 if (gnu_variant_list)
6821 int union_field_packed;
6823 if (all_rep_and_size)
6825 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6826 TYPE_SIZE_UNIT (gnu_union_type)
6827 = TYPE_SIZE_UNIT (gnu_record_type);
6830 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6831 all_rep_and_size ? 1 : 0, false);
6833 /* If GNU_UNION_TYPE is our record type, it means we must have an
6834 Unchecked_Union with no fields. Verify that and, if so, just
6836 if (gnu_union_type == gnu_record_type)
6838 gcc_assert (unchecked_union
6840 && !gnu_our_rep_list);
6844 /* Deal with packedness like in gnat_to_gnu_field. */
6846 = adjust_packed (gnu_union_type, gnu_record_type, packed);
6849 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6851 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6852 all_rep ? bitsize_zero_node : 0, 0);
6854 DECL_INTERNAL_P (gnu_union_field) = 1;
6855 TREE_CHAIN (gnu_union_field) = gnu_field_list;
6856 gnu_field_list = gnu_union_field;
6860 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
6861 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
6862 in a separate pass since we want to handle the discriminants but can't
6863 play with them until we've used them in debugging data above.
6865 ??? Note: if we then reorder them, debugging information will be wrong,
6866 but there's nothing that can be done about this at the moment. */
6867 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6869 if (DECL_FIELD_OFFSET (gnu_field))
6871 tree gnu_next = TREE_CHAIN (gnu_field);
6874 gnu_field_list = gnu_next;
6876 TREE_CHAIN (gnu_last) = gnu_next;
6878 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6879 gnu_our_rep_list = gnu_field;
6880 gnu_field = gnu_next;
6884 gnu_last = gnu_field;
6885 gnu_field = TREE_CHAIN (gnu_field);
6889 /* If we have any items in our rep'ed field list, it is not the case that all
6890 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6891 set it and ignore the items. */
6892 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6893 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6894 else if (gnu_our_rep_list)
6896 /* Otherwise, sort the fields by bit position and put them into their
6897 own record if we have any fields without rep clauses. */
6899 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6900 int len = list_length (gnu_our_rep_list);
6901 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6904 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6905 gnu_field = TREE_CHAIN (gnu_field), i++)
6906 gnu_arr[i] = gnu_field;
6908 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6910 /* Put the fields in the list in order of increasing position, which
6911 means we start from the end. */
6912 gnu_our_rep_list = NULL_TREE;
6913 for (i = len - 1; i >= 0; i--)
6915 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6916 gnu_our_rep_list = gnu_arr[i];
6917 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6922 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6923 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6924 gnu_record_type, 0, 0, 0, 1);
6925 DECL_INTERNAL_P (gnu_field) = 1;
6926 gnu_field_list = chainon (gnu_field_list, gnu_field);
6930 layout_with_rep = true;
6931 gnu_field_list = nreverse (gnu_our_rep_list);
6935 if (cancel_alignment)
6936 TYPE_ALIGN (gnu_record_type) = 0;
6938 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6939 layout_with_rep ? 1 : 0, do_not_finalize);
6942 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6943 placed into an Esize, Component_Bit_Offset, or Component_Size value
6944 in the GNAT tree. */
6947 annotate_value (tree gnu_size)
6949 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6951 Node_Ref_Or_Val ops[3], ret;
6954 struct tree_int_map **h = NULL;
6956 /* See if we've already saved the value for this node. */
6957 if (EXPR_P (gnu_size))
6959 struct tree_int_map in;
6960 if (!annotate_value_cache)
6961 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6962 tree_int_map_eq, 0);
6963 in.base.from = gnu_size;
6964 h = (struct tree_int_map **)
6965 htab_find_slot (annotate_value_cache, &in, INSERT);
6968 return (Node_Ref_Or_Val) (*h)->to;
6971 /* If we do not return inside this switch, TCODE will be set to the
6972 code to use for a Create_Node operand and LEN (set above) will be
6973 the number of recursive calls for us to make. */
6975 switch (TREE_CODE (gnu_size))
6978 if (TREE_OVERFLOW (gnu_size))
6981 /* This may have come from a conversion from some smaller type,
6982 so ensure this is in bitsizetype. */
6983 gnu_size = convert (bitsizetype, gnu_size);
6985 /* For negative values, use NEGATE_EXPR of the supplied value. */
6986 if (tree_int_cst_sgn (gnu_size) < 0)
6988 /* The ridiculous code below is to handle the case of the largest
6989 negative integer. */
6990 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6991 bool adjust = false;
6994 if (TREE_OVERFLOW (negative_size))
6997 = size_binop (MINUS_EXPR, bitsize_zero_node,
6998 size_binop (PLUS_EXPR, gnu_size,
7003 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
7005 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
7007 return annotate_value (temp);
7010 if (!host_integerp (gnu_size, 1))
7013 size = tree_low_cst (gnu_size, 1);
7015 /* This peculiar test is to make sure that the size fits in an int
7016 on machines where HOST_WIDE_INT is not "int". */
7017 if (tree_low_cst (gnu_size, 1) == size)
7018 return UI_From_Int (size);
7023 /* The only case we handle here is a simple discriminant reference. */
7024 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7025 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7026 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7027 return Create_Node (Discrim_Val,
7028 annotate_value (DECL_DISCRIMINANT_NUMBER
7029 (TREE_OPERAND (gnu_size, 1))),
7034 CASE_CONVERT: case NON_LVALUE_EXPR:
7035 return annotate_value (TREE_OPERAND (gnu_size, 0));
7037 /* Now just list the operations we handle. */
7038 case COND_EXPR: tcode = Cond_Expr; break;
7039 case PLUS_EXPR: tcode = Plus_Expr; break;
7040 case MINUS_EXPR: tcode = Minus_Expr; break;
7041 case MULT_EXPR: tcode = Mult_Expr; break;
7042 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7043 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7044 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7045 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7046 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7047 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7048 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7049 case NEGATE_EXPR: tcode = Negate_Expr; break;
7050 case MIN_EXPR: tcode = Min_Expr; break;
7051 case MAX_EXPR: tcode = Max_Expr; break;
7052 case ABS_EXPR: tcode = Abs_Expr; break;
7053 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7054 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7055 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7056 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7057 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7058 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7059 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7060 case LT_EXPR: tcode = Lt_Expr; break;
7061 case LE_EXPR: tcode = Le_Expr; break;
7062 case GT_EXPR: tcode = Gt_Expr; break;
7063 case GE_EXPR: tcode = Ge_Expr; break;
7064 case EQ_EXPR: tcode = Eq_Expr; break;
7065 case NE_EXPR: tcode = Ne_Expr; break;
7071 /* Now get each of the operands that's relevant for this code. If any
7072 cannot be expressed as a repinfo node, say we can't. */
7073 for (i = 0; i < 3; i++)
7076 for (i = 0; i < len; i++)
7078 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7079 if (ops[i] == No_Uint)
7083 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7085 /* Save the result in the cache. */
7088 *h = GGC_NEW (struct tree_int_map);
7089 (*h)->base.from = gnu_size;
7096 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
7097 GCC type, set Component_Bit_Offset and Esize to the position and size
7101 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7105 Entity_Id gnat_field;
7107 /* We operate by first making a list of all fields and their positions
7108 (we can get the sizes easily at any time) by a recursive call
7109 and then update all the sizes into the tree. */
7110 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
7111 size_zero_node, bitsize_zero_node,
7114 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
7115 gnat_field = Next_Entity (gnat_field))
7116 if ((Ekind (gnat_field) == E_Component
7117 || (Ekind (gnat_field) == E_Discriminant
7118 && !Is_Unchecked_Union (Scope (gnat_field)))))
7120 tree parent_offset = bitsize_zero_node;
7122 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
7127 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7129 /* In this mode the tag and parent components have not been
7130 generated, so we add the appropriate offset to each
7131 component. For a component appearing in the current
7132 extension, the offset is the size of the parent. */
7133 if (Is_Derived_Type (gnat_entity)
7134 && Original_Record_Component (gnat_field) == gnat_field)
7136 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7139 parent_offset = bitsize_int (POINTER_SIZE);
7142 Set_Component_Bit_Offset
7145 (size_binop (PLUS_EXPR,
7146 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
7147 TREE_VALUE (TREE_VALUE
7148 (TREE_VALUE (gnu_entry)))),
7151 Set_Esize (gnat_field,
7152 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
7154 else if (Is_Tagged_Type (gnat_entity)
7155 && Is_Derived_Type (gnat_entity))
7157 /* If there is no gnu_entry, this is an inherited component whose
7158 position is the same as in the parent type. */
7159 Set_Component_Bit_Offset
7161 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7162 Set_Esize (gnat_field,
7163 Esize (Original_Record_Component (gnat_field)));
7168 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
7169 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
7170 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
7171 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
7172 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
7173 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
7177 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
7178 tree gnu_bitpos, unsigned int offset_align)
7181 tree gnu_result = gnu_list;
7183 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
7184 gnu_field = TREE_CHAIN (gnu_field))
7186 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7187 DECL_FIELD_BIT_OFFSET (gnu_field));
7188 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7189 DECL_FIELD_OFFSET (gnu_field));
7190 unsigned int our_offset_align
7191 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7194 = tree_cons (gnu_field,
7195 tree_cons (gnu_our_offset,
7196 tree_cons (size_int (our_offset_align),
7197 gnu_our_bitpos, NULL_TREE),
7201 if (DECL_INTERNAL_P (gnu_field))
7203 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
7204 gnu_our_offset, gnu_our_bitpos,
7211 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7212 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
7213 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
7214 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7215 for the size of a field. COMPONENT_P is true if we are being called
7216 to process the Component_Size of GNAT_OBJECT. This is used for error
7217 message handling and to indicate to use the object size of GNU_TYPE.
7218 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7219 it means that a size of zero should be treated as an unspecified size. */
7222 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7223 enum tree_code kind, bool component_p, bool zero_ok)
7225 Node_Id gnat_error_node;
7226 tree type_size, size;
7228 if (kind == VAR_DECL
7229 /* If a type needs strict alignment, a component of this type in
7230 a packed record cannot be packed and thus uses the type size. */
7231 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7232 type_size = TYPE_SIZE (gnu_type);
7234 type_size = rm_size (gnu_type);
7236 /* Find the node to use for errors. */
7237 if ((Ekind (gnat_object) == E_Component
7238 || Ekind (gnat_object) == E_Discriminant)
7239 && Present (Component_Clause (gnat_object)))
7240 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7241 else if (Present (Size_Clause (gnat_object)))
7242 gnat_error_node = Expression (Size_Clause (gnat_object));
7244 gnat_error_node = gnat_object;
7246 /* Return 0 if no size was specified, either because Esize was not Present
7247 or the specified size was zero. */
7248 if (No (uint_size) || uint_size == No_Uint)
7251 /* Get the size as a tree. Issue an error if a size was specified but
7252 cannot be represented in sizetype. */
7253 size = UI_To_gnu (uint_size, bitsizetype);
7254 if (TREE_OVERFLOW (size))
7256 post_error_ne (component_p ? "component size of & is too large"
7257 : "size of & is too large",
7258 gnat_error_node, gnat_object);
7262 /* Ignore a negative size since that corresponds to our back-annotation.
7263 Also ignore a zero size if it is not permitted. */
7264 if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
7267 /* The size of objects is always a multiple of a byte. */
7268 if (kind == VAR_DECL
7269 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7272 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7273 gnat_error_node, gnat_object);
7275 post_error_ne ("size for& is not a multiple of Storage_Unit",
7276 gnat_error_node, gnat_object);
7280 /* If this is an integral type or a packed array type, the front-end has
7281 verified the size, so we need not do it here (which would entail
7282 checking against the bounds). However, if this is an aliased object,
7283 it may not be smaller than the type of the object. */
7284 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7285 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7288 /* If the object is a record that contains a template, add the size of
7289 the template to the specified size. */
7290 if (TREE_CODE (gnu_type) == RECORD_TYPE
7291 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7292 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7294 /* Modify the size of the type to be that of the maximum size if it has a
7296 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7297 type_size = max_size (type_size, true);
7299 /* If this is an access type or a fat pointer, the minimum size is that given
7300 by the smallest integral mode that's valid for pointers. */
7301 if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
7303 enum machine_mode p_mode;
7305 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7306 !targetm.valid_pointer_mode (p_mode);
7307 p_mode = GET_MODE_WIDER_MODE (p_mode))
7310 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7313 /* If the size of the object is a constant, the new size must not be
7315 if (TREE_CODE (type_size) != INTEGER_CST
7316 || TREE_OVERFLOW (type_size)
7317 || tree_int_cst_lt (size, type_size))
7321 ("component size for& too small{, minimum allowed is ^}",
7322 gnat_error_node, gnat_object, type_size);
7324 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
7325 gnat_error_node, gnat_object, type_size);
7327 if (kind == VAR_DECL && !component_p
7328 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
7329 && !tree_int_cst_lt (size, rm_size (gnu_type)))
7330 post_error_ne_tree_2
7331 ("\\size of ^ is not a multiple of alignment (^ bits)",
7332 gnat_error_node, gnat_object, rm_size (gnu_type),
7333 TYPE_ALIGN (gnu_type));
7335 else if (INTEGRAL_TYPE_P (gnu_type))
7336 post_error_ne ("\\size would be legal if & were not aliased!",
7337 gnat_error_node, gnat_object);
7345 /* Similarly, but both validate and process a value of RM size. This
7346 routine is only called for types. */
7349 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7351 /* Only issue an error if a Value_Size clause was explicitly given.
7352 Otherwise, we'd be duplicating an error on the Size clause. */
7353 Node_Id gnat_attr_node
7354 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7355 tree old_size = rm_size (gnu_type), size;
7357 /* Do nothing if no size was specified, either because RM size was not
7358 Present or if the specified size was zero. */
7359 if (No (uint_size) || uint_size == No_Uint)
7362 /* Get the size as a tree. Issue an error if a size was specified but
7363 cannot be represented in sizetype. */
7364 size = UI_To_gnu (uint_size, bitsizetype);
7365 if (TREE_OVERFLOW (size))
7367 if (Present (gnat_attr_node))
7368 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7373 /* Ignore a negative size since that corresponds to our back-annotation.
7374 Also ignore a zero size unless a Value_Size clause exists, or a size
7375 clause exists, or this is an integer type, in which case the front-end
7376 will have always set it. */
7377 if (tree_int_cst_sgn (size) < 0
7378 || (integer_zerop (size)
7379 && No (gnat_attr_node)
7380 && !Has_Size_Clause (gnat_entity)
7381 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
7384 /* If the old size is self-referential, get the maximum size. */
7385 if (CONTAINS_PLACEHOLDER_P (old_size))
7386 old_size = max_size (old_size, true);
7388 /* If the size of the object is a constant, the new size must not be
7389 smaller (the front-end checks this for scalar types). */
7390 if (TREE_CODE (old_size) != INTEGER_CST
7391 || TREE_OVERFLOW (old_size)
7392 || (AGGREGATE_TYPE_P (gnu_type) && tree_int_cst_lt (size, old_size)))
7394 if (Present (gnat_attr_node))
7396 ("Value_Size for& too small{, minimum allowed is ^}",
7397 gnat_attr_node, gnat_entity, old_size);
7401 /* Otherwise, set the RM size proper for integral types... */
7402 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7403 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7404 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7405 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7406 TYPE_RM_SIZE (gnu_type) = size;
7408 /* ...or the Ada size for record and union types. */
7409 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7410 || TREE_CODE (gnu_type) == UNION_TYPE
7411 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7412 && !TYPE_IS_FAT_POINTER_P (gnu_type))
7413 SET_TYPE_ADA_SIZE (gnu_type, size);
7416 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7417 If TYPE is the best type, return it. Otherwise, make a new type. We
7418 only support new integral and pointer types. FOR_BIASED is true if
7419 we are making a biased type. */
7422 make_type_from_size (tree type, tree size_tree, bool for_biased)
7424 unsigned HOST_WIDE_INT size;
7428 /* If size indicates an error, just return TYPE to avoid propagating
7429 the error. Likewise if it's too large to represent. */
7430 if (!size_tree || !host_integerp (size_tree, 1))
7433 size = tree_low_cst (size_tree, 1);
7435 switch (TREE_CODE (type))
7440 biased_p = (TREE_CODE (type) == INTEGER_TYPE
7441 && TYPE_BIASED_REPRESENTATION_P (type));
7443 /* Only do something if the type is not a packed array type and
7444 doesn't already have the proper size. */
7445 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7446 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7449 biased_p |= for_biased;
7450 if (size > LONG_LONG_TYPE_SIZE)
7451 size = LONG_LONG_TYPE_SIZE;
7453 if (TYPE_UNSIGNED (type) || biased_p)
7454 new_type = make_unsigned_type (size);
7456 new_type = make_signed_type (size);
7457 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7458 TYPE_MIN_VALUE (new_type)
7459 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
7460 TYPE_MAX_VALUE (new_type)
7461 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
7462 /* Propagate the name to avoid creating a fake subrange type. */
7463 if (TYPE_NAME (type))
7465 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
7466 TYPE_NAME (new_type) = DECL_NAME (TYPE_NAME (type));
7468 TYPE_NAME (new_type) = TYPE_NAME (type);
7470 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7471 TYPE_RM_SIZE (new_type) = bitsize_int (size);
7475 /* Do something if this is a fat pointer, in which case we
7476 may need to return the thin pointer. */
7477 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7479 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7480 if (!targetm.valid_pointer_mode (p_mode))
7483 build_pointer_type_for_mode
7484 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7490 /* Only do something if this is a thin pointer, in which case we
7491 may need to return the fat pointer. */
7492 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7494 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7504 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7505 a type or object whose present alignment is ALIGN. If this alignment is
7506 valid, return it. Otherwise, give an error and return ALIGN. */
7509 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7511 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7512 unsigned int new_align;
7513 Node_Id gnat_error_node;
7515 /* Don't worry about checking alignment if alignment was not specified
7516 by the source program and we already posted an error for this entity. */
7517 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7520 /* Post the error on the alignment clause if any. */
7521 if (Present (Alignment_Clause (gnat_entity)))
7522 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7524 gnat_error_node = gnat_entity;
7526 /* Within GCC, an alignment is an integer, so we must make sure a value is
7527 specified that fits in that range. Also, there is an upper bound to
7528 alignments we can support/allow. */
7529 if (!UI_Is_In_Int_Range (alignment)
7530 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7531 post_error_ne_num ("largest supported alignment for& is ^",
7532 gnat_error_node, gnat_entity, max_allowed_alignment);
7533 else if (!(Present (Alignment_Clause (gnat_entity))
7534 && From_At_Mod (Alignment_Clause (gnat_entity)))
7535 && new_align * BITS_PER_UNIT < align)
7537 unsigned int double_align;
7538 bool is_capped_double, align_clause;
7540 /* If the default alignment of "double" or larger scalar types is
7541 specifically capped and the new alignment is above the cap, do
7542 not post an error and change the alignment only if there is an
7543 alignment clause; this makes it possible to have the associated
7544 GCC type overaligned by default for performance reasons. */
7545 if ((double_align = double_float_alignment) > 0)
7548 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7550 = is_double_float_or_array (gnat_type, &align_clause);
7552 else if ((double_align = double_scalar_alignment) > 0)
7555 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7557 = is_double_scalar_or_array (gnat_type, &align_clause);
7560 is_capped_double = align_clause = false;
7562 if (is_capped_double && new_align >= double_align)
7565 align = new_align * BITS_PER_UNIT;
7569 if (is_capped_double)
7570 align = double_align * BITS_PER_UNIT;
7572 post_error_ne_num ("alignment for& must be at least ^",
7573 gnat_error_node, gnat_entity,
7574 align / BITS_PER_UNIT);
7579 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7580 if (new_align > align)
7587 /* Return the smallest alignment not less than SIZE. */
7590 ceil_alignment (unsigned HOST_WIDE_INT size)
7592 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7595 /* Verify that OBJECT, a type or decl, is something we can implement
7596 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7597 if we require atomic components. */
7600 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7602 Node_Id gnat_error_point = gnat_entity;
7604 enum machine_mode mode;
7608 /* There are three case of what OBJECT can be. It can be a type, in which
7609 case we take the size, alignment and mode from the type. It can be a
7610 declaration that was indirect, in which case the relevant values are
7611 that of the type being pointed to, or it can be a normal declaration,
7612 in which case the values are of the decl. The code below assumes that
7613 OBJECT is either a type or a decl. */
7614 if (TYPE_P (object))
7616 mode = TYPE_MODE (object);
7617 align = TYPE_ALIGN (object);
7618 size = TYPE_SIZE (object);
7620 else if (DECL_BY_REF_P (object))
7622 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7623 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7624 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7628 mode = DECL_MODE (object);
7629 align = DECL_ALIGN (object);
7630 size = DECL_SIZE (object);
7633 /* Consider all floating-point types atomic and any types that that are
7634 represented by integers no wider than a machine word. */
7635 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7636 || ((GET_MODE_CLASS (mode) == MODE_INT
7637 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7638 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7641 /* For the moment, also allow anything that has an alignment equal
7642 to its size and which is smaller than a word. */
7643 if (size && TREE_CODE (size) == INTEGER_CST
7644 && compare_tree_int (size, align) == 0
7645 && align <= BITS_PER_WORD)
7648 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7649 gnat_node = Next_Rep_Item (gnat_node))
7651 if (!comp_p && Nkind (gnat_node) == N_Pragma
7652 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7654 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7655 else if (comp_p && Nkind (gnat_node) == N_Pragma
7656 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7657 == Pragma_Atomic_Components))
7658 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7662 post_error_ne ("atomic access to component of & cannot be guaranteed",
7663 gnat_error_point, gnat_entity);
7665 post_error_ne ("atomic access to & cannot be guaranteed",
7666 gnat_error_point, gnat_entity);
7669 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7670 have compatible signatures so that a call using one type may be safely
7671 issued if the actual target function type is the other. Return 1 if it is
7672 the case, 0 otherwise, and post errors on the incompatibilities.
7674 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7675 that calls to the subprogram will have arguments suitable for the later
7676 underlying builtin expansion. */
7679 compatible_signatures_p (tree ftype1, tree ftype2)
7681 /* As of now, we only perform very trivial tests and consider it's the
7682 programmer's responsibility to ensure the type correctness in the Ada
7683 declaration, as in the regular Import cases.
7685 Mismatches typically result in either error messages from the builtin
7686 expander, internal compiler errors, or in a real call sequence. This
7687 should be refined to issue diagnostics helping error detection and
7690 /* Almost fake test, ensuring a use of each argument. */
7691 if (ftype1 == ftype2)
7697 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
7698 type with all size expressions that contain F in a PLACEHOLDER_EXPR
7699 updated by replacing F with R.
7701 The function doesn't update the layout of the type, i.e. it assumes
7702 that the substitution is purely formal. That's why the replacement
7703 value R must itself contain a PLACEHOLDER_EXPR. */
7706 substitute_in_type (tree t, tree f, tree r)
7710 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
7712 switch (TREE_CODE (t))
7717 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7718 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7720 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7721 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7723 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7726 new = copy_type (t);
7727 TYPE_MIN_VALUE (new) = low;
7728 TYPE_MAX_VALUE (new) = high;
7729 if (TYPE_INDEX_TYPE (t))
7731 (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7738 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7739 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7741 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7742 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7744 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7747 new = copy_type (t);
7748 TYPE_MIN_VALUE (new) = low;
7749 TYPE_MAX_VALUE (new) = high;
7756 new = substitute_in_type (TREE_TYPE (t), f, r);
7757 if (new == TREE_TYPE (t))
7760 return build_complex_type (new);
7766 /* These should never show up here. */
7771 tree component = substitute_in_type (TREE_TYPE (t), f, r);
7772 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7774 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7777 new = build_array_type (component, domain);
7778 TYPE_ALIGN (new) = TYPE_ALIGN (t);
7779 TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7780 SET_TYPE_MODE (new, TYPE_MODE (t));
7781 TYPE_SIZE (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
7782 TYPE_SIZE_UNIT (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
7783 TYPE_NONALIASED_COMPONENT (new) = TYPE_NONALIASED_COMPONENT (t);
7784 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7785 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7791 case QUAL_UNION_TYPE:
7793 bool changed_field = false;
7796 /* Start out with no fields, make new fields, and chain them
7797 in. If we haven't actually changed the type of any field,
7798 discard everything we've done and return the old type. */
7799 new = copy_type (t);
7800 TYPE_FIELDS (new) = NULL_TREE;
7802 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7804 tree new_field = copy_node (field), new_n;
7806 new_n = substitute_in_type (TREE_TYPE (field), f, r);
7807 if (new_n != TREE_TYPE (field))
7809 TREE_TYPE (new_field) = new_n;
7810 changed_field = true;
7813 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
7814 if (new_n != DECL_FIELD_OFFSET (field))
7816 DECL_FIELD_OFFSET (new_field) = new_n;
7817 changed_field = true;
7820 /* Do the substitution inside the qualifier, if any. */
7821 if (TREE_CODE (t) == QUAL_UNION_TYPE)
7823 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7824 if (new_n != DECL_QUALIFIER (field))
7826 DECL_QUALIFIER (new_field) = new_n;
7827 changed_field = true;
7831 DECL_CONTEXT (new_field) = new;
7832 SET_DECL_ORIGINAL_FIELD (new_field,
7833 (DECL_ORIGINAL_FIELD (field)
7834 ? DECL_ORIGINAL_FIELD (field) : field));
7836 TREE_CHAIN (new_field) = TYPE_FIELDS (new);
7837 TYPE_FIELDS (new) = new_field;
7843 TYPE_FIELDS (new) = nreverse (TYPE_FIELDS (new));
7844 TYPE_SIZE (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
7845 TYPE_SIZE_UNIT (new) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
7846 SET_TYPE_ADA_SIZE (new, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
7855 /* Return the RM size of GNU_TYPE. This is the actual number of bits
7856 needed to represent the object. */
7859 rm_size (tree gnu_type)
7861 /* For integral types, we store the RM size explicitly. */
7862 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7863 return TYPE_RM_SIZE (gnu_type);
7865 /* Return the RM size of the actual data plus the size of the template. */
7866 if (TREE_CODE (gnu_type) == RECORD_TYPE
7867 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7869 size_binop (PLUS_EXPR,
7870 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7871 DECL_SIZE (TYPE_FIELDS (gnu_type)));
7873 /* For record types, we store the size explicitly. */
7874 if ((TREE_CODE (gnu_type) == RECORD_TYPE
7875 || TREE_CODE (gnu_type) == UNION_TYPE
7876 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7877 && !TYPE_IS_FAT_POINTER_P (gnu_type)
7878 && TYPE_ADA_SIZE (gnu_type))
7879 return TYPE_ADA_SIZE (gnu_type);
7881 /* For other types, this is just the size. */
7882 return TYPE_SIZE (gnu_type);
7885 /* Return the name to be used for GNAT_ENTITY. If a type, create a
7886 fully-qualified name, possibly with type information encoding.
7887 Otherwise, return the name. */
7890 get_entity_name (Entity_Id gnat_entity)
7892 Get_Encoded_Name (gnat_entity);
7893 return get_identifier_with_length (Name_Buffer, Name_Len);
7896 /* Return an identifier representing the external name to be used for
7897 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
7898 and the specified suffix. */
7901 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7903 Entity_Kind kind = Ekind (gnat_entity);
7907 String_Template temp = {1, strlen (suffix)};
7908 Fat_Pointer fp = {suffix, &temp};
7909 Get_External_Name_With_Suffix (gnat_entity, fp);
7912 Get_External_Name (gnat_entity, 0);
7914 /* A variable using the Stdcall convention lives in a DLL. We adjust
7915 its name to use the jump table, the _imp__NAME contains the address
7916 for the NAME variable. */
7917 if ((kind == E_Variable || kind == E_Constant)
7918 && Has_Stdcall_Convention (gnat_entity))
7920 const int len = 6 + Name_Len;
7921 char *new_name = (char *) alloca (len + 1);
7922 strcpy (new_name, "_imp__");
7923 strcat (new_name, Name_Buffer);
7924 return get_identifier_with_length (new_name, len);
7927 return get_identifier_with_length (Name_Buffer, Name_Len);
7930 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
7931 string, return a new IDENTIFIER_NODE that is the concatenation of
7932 the name followed by "___" and the specified suffix. */
7935 concat_name (tree gnu_name, const char *suffix)
7937 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
7938 char *new_name = (char *) alloca (len + 1);
7939 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
7940 strcat (new_name, "___");
7941 strcat (new_name, suffix);
7942 return get_identifier_with_length (new_name, len);
7945 #include "gt-ada-decl.h"