1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2010, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 ****************************************************************************/
28 #include "coretypes.h"
36 #include "tree-inline.h"
54 #ifndef MAX_FIXED_MODE_SIZE
55 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 /* Convention_Stdcall should be processed in a specific way on Windows targets
59 only. The macro below is a helper to avoid having to check for a Windows
60 specific attribute throughout this unit. */
62 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
63 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #define Has_Stdcall_Convention(E) (0)
68 /* Stack realignment for functions with foreign conventions is provided on a
69 per back-end basis now, as it is handled by the prologue expanders and not
70 as part of the function's body any more. It might be requested by way of a
71 dedicated function type attribute on the targets that support it.
73 We need a way to avoid setting the attribute on the targets that don't
74 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
76 It is defined on targets where the circuitry is available, and indicates
77 whether the realignment is needed for 'main'. We use this to decide for
78 foreign subprograms as well.
80 It is not defined on targets where the circuitry is not implemented, and
81 we just never set the attribute in these cases.
83 Whether it is defined on all targets that would need it in theory is
84 not entirely clear. We currently trust the base GCC settings for this
87 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
88 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
93 struct incomplete *next;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_With_Type types until the
105 static struct incomplete *defer_limited_with;
107 /* These variables are used to defer finalizing types. The element of the
108 list is the TYPE_DECL associated with the type. */
109 static int defer_finalize_level = 0;
110 static VEC (tree,heap) *defer_finalize_list;
112 /* A hash table used to cache the result of annotate_value. */
113 static GTY ((if_marked ("tree_int_map_marked_p"),
114 param_is (struct tree_int_map))) htab_t annotate_value_cache;
123 static void relate_alias_sets (tree, tree, enum alias_set_op);
125 static bool allocatable_size_p (tree, bool);
126 static void prepend_one_attribute_to (struct attrib **,
127 enum attr_type, tree, tree, Node_Id);
128 static void prepend_attributes (Entity_Id, struct attrib **);
129 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
130 static bool is_variable_size (tree);
131 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
132 static tree make_packable_type (tree, bool);
133 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
134 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
136 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
137 static bool same_discriminant_p (Entity_Id, Entity_Id);
138 static bool array_type_has_nonaliased_component (tree, Entity_Id);
139 static bool compile_time_known_address_p (Node_Id);
140 static bool cannot_be_superflat_p (Node_Id);
141 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
142 bool, bool, bool, bool, bool);
143 static Uint annotate_value (tree);
144 static void annotate_rep (Entity_Id, tree);
145 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
146 static tree build_subst_list (Entity_Id, Entity_Id, bool);
147 static tree build_variant_list (tree, tree, tree);
148 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
149 static void set_rm_size (Uint, tree, Entity_Id);
150 static tree make_type_from_size (tree, tree, bool);
151 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
152 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
153 static void check_ok_for_atomic (tree, Entity_Id, bool);
154 static int compatible_signatures_p (tree, tree);
155 static tree create_field_decl_from (tree, tree, tree, tree, tree, tree);
156 static tree get_rep_part (tree);
157 static tree get_variant_part (tree);
158 static tree create_variant_part_from (tree, tree, tree, tree, tree);
159 static void copy_and_substitute_in_size (tree, tree, tree);
160 static void rest_of_type_decl_compilation_no_defer (tree);
162 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
163 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
164 and associate the ..._DECL node with the input GNAT defining identifier.
166 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
167 initial value (in GCC tree form). This is optional for a variable. For
168 a renamed entity, GNU_EXPR gives the object being renamed.
170 DEFINITION is nonzero if this call is intended for a definition. This is
171 used for separate compilation where it is necessary to know whether an
172 external declaration or a definition must be created if the GCC equivalent
173 was not created previously. The value of 1 is normally used for a nonzero
174 DEFINITION, but a value of 2 is used in special circumstances, defined in
178 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
180 /* Contains the kind of the input GNAT node. */
181 const Entity_Kind kind = Ekind (gnat_entity);
182 /* True if this is a type. */
183 const bool is_type = IN (kind, Type_Kind);
184 /* For a type, contains the equivalent GNAT node to be used in gigi. */
185 Entity_Id gnat_equiv_type = Empty;
186 /* Temporary used to walk the GNAT tree. */
188 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
189 This node will be associated with the GNAT node by calling at the end
190 of the `switch' statement. */
191 tree gnu_decl = NULL_TREE;
192 /* Contains the GCC type to be used for the GCC node. */
193 tree gnu_type = NULL_TREE;
194 /* Contains the GCC size tree to be used for the GCC node. */
195 tree gnu_size = NULL_TREE;
196 /* Contains the GCC name to be used for the GCC node. */
197 tree gnu_entity_name;
198 /* True if we have already saved gnu_decl as a GNAT association. */
200 /* True if we incremented defer_incomplete_level. */
201 bool this_deferred = false;
202 /* True if we incremented force_global. */
203 bool this_global = false;
204 /* True if we should check to see if elaborated during processing. */
205 bool maybe_present = false;
206 /* True if we made GNU_DECL and its type here. */
207 bool this_made_decl = false;
208 /* True if debug info is requested for this entity. */
209 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
210 || debug_info_level == DINFO_LEVEL_VERBOSE);
211 /* True if this entity is to be considered as imported. */
212 bool imported_p = (Is_Imported (gnat_entity)
213 && No (Address_Clause (gnat_entity)));
214 /* Size and alignment of the GCC node, if meaningful. */
215 unsigned int esize = 0, align = 0;
216 /* Contains the list of attributes directly attached to the entity. */
217 struct attrib *attr_list = NULL;
219 /* Since a use of an Itype is a definition, process it as such if it
220 is not in a with'ed unit. */
223 && Is_Itype (gnat_entity)
224 && !present_gnu_tree (gnat_entity)
225 && In_Extended_Main_Code_Unit (gnat_entity))
227 /* Ensure that we are in a subprogram mentioned in the Scope chain of
228 this entity, our current scope is global, or we encountered a task
229 or entry (where we can't currently accurately check scoping). */
230 if (!current_function_decl
231 || DECL_ELABORATION_PROC_P (current_function_decl))
233 process_type (gnat_entity);
234 return get_gnu_tree (gnat_entity);
237 for (gnat_temp = Scope (gnat_entity);
239 gnat_temp = Scope (gnat_temp))
241 if (Is_Type (gnat_temp))
242 gnat_temp = Underlying_Type (gnat_temp);
244 if (Ekind (gnat_temp) == E_Subprogram_Body)
246 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
248 if (IN (Ekind (gnat_temp), Subprogram_Kind)
249 && Present (Protected_Body_Subprogram (gnat_temp)))
250 gnat_temp = Protected_Body_Subprogram (gnat_temp);
252 if (Ekind (gnat_temp) == E_Entry
253 || Ekind (gnat_temp) == E_Entry_Family
254 || Ekind (gnat_temp) == E_Task_Type
255 || (IN (Ekind (gnat_temp), Subprogram_Kind)
256 && present_gnu_tree (gnat_temp)
257 && (current_function_decl
258 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
260 process_type (gnat_entity);
261 return get_gnu_tree (gnat_entity);
265 /* This abort means the Itype has an incorrect scope, i.e. that its
266 scope does not correspond to the subprogram it is declared in. */
270 /* If we've already processed this entity, return what we got last time.
271 If we are defining the node, we should not have already processed it.
272 In that case, we will abort below when we try to save a new GCC tree
273 for this object. We also need to handle the case of getting a dummy
274 type when a Full_View exists. */
275 if ((!definition || (is_type && imported_p))
276 && present_gnu_tree (gnat_entity))
278 gnu_decl = get_gnu_tree (gnat_entity);
280 if (TREE_CODE (gnu_decl) == TYPE_DECL
281 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
282 && IN (kind, Incomplete_Or_Private_Kind)
283 && Present (Full_View (gnat_entity)))
286 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
287 save_gnu_tree (gnat_entity, NULL_TREE, false);
288 save_gnu_tree (gnat_entity, gnu_decl, false);
294 /* If this is a numeric or enumeral type, or an access type, a nonzero
295 Esize must be specified unless it was specified by the programmer. */
296 gcc_assert (!Unknown_Esize (gnat_entity)
297 || Has_Size_Clause (gnat_entity)
298 || (!IN (kind, Numeric_Kind)
299 && !IN (kind, Enumeration_Kind)
300 && (!IN (kind, Access_Kind)
301 || kind == E_Access_Protected_Subprogram_Type
302 || kind == E_Anonymous_Access_Protected_Subprogram_Type
303 || kind == E_Access_Subtype)));
305 /* The RM size must be specified for all discrete and fixed-point types. */
306 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
307 && Unknown_RM_Size (gnat_entity)));
309 /* If we get here, it means we have not yet done anything with this entity.
310 If we are not defining it, it must be a type or an entity that is defined
311 elsewhere or externally, otherwise we should have defined it already. */
312 gcc_assert (definition
313 || type_annotate_only
315 || kind == E_Discriminant
316 || kind == E_Component
318 || (kind == E_Constant && Present (Full_View (gnat_entity)))
319 || Is_Public (gnat_entity));
321 /* Get the name of the entity and set up the line number and filename of
322 the original definition for use in any decl we make. */
323 gnu_entity_name = get_entity_name (gnat_entity);
324 Sloc_to_locus (Sloc (gnat_entity), &input_location);
326 /* For cases when we are not defining (i.e., we are referencing from
327 another compilation unit) public entities, show we are at global level
328 for the purpose of computing scopes. Don't do this for components or
329 discriminants since the relevant test is whether or not the record is
332 && kind != E_Component
333 && kind != E_Discriminant
334 && Is_Public (gnat_entity)
335 && !Is_Statically_Allocated (gnat_entity))
336 force_global++, this_global = true;
338 /* Handle any attributes directly attached to the entity. */
339 if (Has_Gigi_Rep_Item (gnat_entity))
340 prepend_attributes (gnat_entity, &attr_list);
342 /* Do some common processing for types. */
345 /* Compute the equivalent type to be used in gigi. */
346 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
348 /* Machine_Attributes on types are expected to be propagated to
349 subtypes. The corresponding Gigi_Rep_Items are only attached
350 to the first subtype though, so we handle the propagation here. */
351 if (Base_Type (gnat_entity) != gnat_entity
352 && !Is_First_Subtype (gnat_entity)
353 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
354 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
357 /* Compute a default value for the size of the type. */
358 if (Known_Esize (gnat_entity)
359 && UI_Is_In_Int_Range (Esize (gnat_entity)))
361 unsigned int max_esize;
362 esize = UI_To_Int (Esize (gnat_entity));
364 if (IN (kind, Float_Kind))
365 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
366 else if (IN (kind, Access_Kind))
367 max_esize = POINTER_SIZE * 2;
369 max_esize = LONG_LONG_TYPE_SIZE;
371 if (esize > max_esize)
375 esize = LONG_LONG_TYPE_SIZE;
381 /* If this is a use of a deferred constant without address clause,
382 get its full definition. */
384 && No (Address_Clause (gnat_entity))
385 && Present (Full_View (gnat_entity)))
388 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
393 /* If we have an external constant that we are not defining, get the
394 expression that is was defined to represent. We may throw that
395 expression away later if it is not a constant. Do not retrieve the
396 expression if it is an aggregate or allocator, because in complex
397 instantiation contexts it may not be expanded */
399 && Present (Expression (Declaration_Node (gnat_entity)))
400 && !No_Initialization (Declaration_Node (gnat_entity))
401 && (Nkind (Expression (Declaration_Node (gnat_entity)))
403 && (Nkind (Expression (Declaration_Node (gnat_entity)))
405 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
407 /* Ignore deferred constant definitions without address clause since
408 they are processed fully in the front-end. If No_Initialization
409 is set, this is not a deferred constant but a constant whose value
410 is built manually. And constants that are renamings are handled
414 && No (Address_Clause (gnat_entity))
415 && !No_Initialization (Declaration_Node (gnat_entity))
416 && No (Renamed_Object (gnat_entity)))
418 gnu_decl = error_mark_node;
423 /* Ignore constant definitions already marked with the error node. See
424 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
427 && present_gnu_tree (gnat_entity)
428 && get_gnu_tree (gnat_entity) == error_mark_node)
430 maybe_present = true;
437 /* We used to special case VMS exceptions here to directly map them to
438 their associated condition code. Since this code had to be masked
439 dynamically to strip off the severity bits, this caused trouble in
440 the GCC/ZCX case because the "type" pointers we store in the tables
441 have to be static. We now don't special case here anymore, and let
442 the regular processing take place, which leaves us with a regular
443 exception data object for VMS exceptions too. The condition code
444 mapping is taken care of by the front end and the bitmasking by the
451 /* The GNAT record where the component was defined. */
452 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
454 /* If the variable is an inherited record component (in the case of
455 extended record types), just return the inherited entity, which
456 must be a FIELD_DECL. Likewise for discriminants.
457 For discriminants of untagged records which have explicit
458 stored discriminants, return the entity for the corresponding
459 stored discriminant. Also use Original_Record_Component
460 if the record has a private extension. */
461 if (Present (Original_Record_Component (gnat_entity))
462 && Original_Record_Component (gnat_entity) != gnat_entity)
465 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
466 gnu_expr, definition);
471 /* If the enclosing record has explicit stored discriminants,
472 then it is an untagged record. If the Corresponding_Discriminant
473 is not empty then this must be a renamed discriminant and its
474 Original_Record_Component must point to the corresponding explicit
475 stored discriminant (i.e. we should have taken the previous
477 else if (Present (Corresponding_Discriminant (gnat_entity))
478 && Is_Tagged_Type (gnat_record))
480 /* A tagged record has no explicit stored discriminants. */
481 gcc_assert (First_Discriminant (gnat_record)
482 == First_Stored_Discriminant (gnat_record));
484 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
485 gnu_expr, definition);
490 else if (Present (CR_Discriminant (gnat_entity))
491 && type_annotate_only)
493 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
494 gnu_expr, definition);
499 /* If the enclosing record has explicit stored discriminants, then
500 it is an untagged record. If the Corresponding_Discriminant
501 is not empty then this must be a renamed discriminant and its
502 Original_Record_Component must point to the corresponding explicit
503 stored discriminant (i.e. we should have taken the first
505 else if (Present (Corresponding_Discriminant (gnat_entity))
506 && (First_Discriminant (gnat_record)
507 != First_Stored_Discriminant (gnat_record)))
510 /* Otherwise, if we are not defining this and we have no GCC type
511 for the containing record, make one for it. Then we should
512 have made our own equivalent. */
513 else if (!definition && !present_gnu_tree (gnat_record))
515 /* ??? If this is in a record whose scope is a protected
516 type and we have an Original_Record_Component, use it.
517 This is a workaround for major problems in protected type
519 Entity_Id Scop = Scope (Scope (gnat_entity));
520 if ((Is_Protected_Type (Scop)
521 || (Is_Private_Type (Scop)
522 && Present (Full_View (Scop))
523 && Is_Protected_Type (Full_View (Scop))))
524 && Present (Original_Record_Component (gnat_entity)))
527 = gnat_to_gnu_entity (Original_Record_Component
534 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
535 gnu_decl = get_gnu_tree (gnat_entity);
541 /* Here we have no GCC type and this is a reference rather than a
542 definition. This should never happen. Most likely the cause is
543 reference before declaration in the gnat tree for gnat_entity. */
547 case E_Loop_Parameter:
548 case E_Out_Parameter:
551 /* Simple variables, loop variables, Out parameters, and exceptions. */
554 bool used_by_ref = false;
556 = ((kind == E_Constant || kind == E_Variable)
557 && Is_True_Constant (gnat_entity)
558 && !Treat_As_Volatile (gnat_entity)
559 && (((Nkind (Declaration_Node (gnat_entity))
560 == N_Object_Declaration)
561 && Present (Expression (Declaration_Node (gnat_entity))))
562 || Present (Renamed_Object (gnat_entity))));
563 bool inner_const_flag = const_flag;
564 bool static_p = Is_Statically_Allocated (gnat_entity);
565 bool mutable_p = false;
566 tree gnu_ext_name = NULL_TREE;
567 tree renamed_obj = NULL_TREE;
568 tree gnu_object_size;
570 if (Present (Renamed_Object (gnat_entity)) && !definition)
572 if (kind == E_Exception)
573 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
576 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
579 /* Get the type after elaborating the renamed object. */
580 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
582 /* For a debug renaming declaration, build a pure debug entity. */
583 if (Present (Debug_Renaming_Link (gnat_entity)))
586 gnu_decl = build_decl (input_location,
587 VAR_DECL, gnu_entity_name, gnu_type);
588 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
589 if (global_bindings_p ())
590 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
592 addr = stack_pointer_rtx;
593 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
594 gnat_pushdecl (gnu_decl, gnat_entity);
598 /* If this is a loop variable, its type should be the base type.
599 This is because the code for processing a loop determines whether
600 a normal loop end test can be done by comparing the bounds of the
601 loop against those of the base type, which is presumed to be the
602 size used for computation. But this is not correct when the size
603 of the subtype is smaller than the type. */
604 if (kind == E_Loop_Parameter)
605 gnu_type = get_base_type (gnu_type);
607 /* Reject non-renamed objects whose types are unconstrained arrays or
608 any object whose type is a dummy type or VOID_TYPE. */
610 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
611 && No (Renamed_Object (gnat_entity)))
612 || TYPE_IS_DUMMY_P (gnu_type)
613 || TREE_CODE (gnu_type) == VOID_TYPE)
615 gcc_assert (type_annotate_only);
618 return error_mark_node;
621 /* If an alignment is specified, use it if valid. Note that exceptions
622 are objects but don't have an alignment. We must do this before we
623 validate the size, since the alignment can affect the size. */
624 if (kind != E_Exception && Known_Alignment (gnat_entity))
626 gcc_assert (Present (Alignment (gnat_entity)));
627 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
628 TYPE_ALIGN (gnu_type));
629 /* No point in changing the type if there is an address clause
630 as the final type of the object will be a reference type. */
631 if (Present (Address_Clause (gnat_entity)))
635 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
636 false, false, definition, true);
639 /* If we are defining the object, see if it has a Size value and
640 validate it if so. If we are not defining the object and a Size
641 clause applies, simply retrieve the value. We don't want to ignore
642 the clause and it is expected to have been validated already. Then
643 get the new type, if any. */
645 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
646 gnat_entity, VAR_DECL, false,
647 Has_Size_Clause (gnat_entity));
648 else if (Has_Size_Clause (gnat_entity))
649 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
654 = make_type_from_size (gnu_type, gnu_size,
655 Has_Biased_Representation (gnat_entity));
657 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
658 gnu_size = NULL_TREE;
661 /* If this object has self-referential size, it must be a record with
662 a default value. We are supposed to allocate an object of the
663 maximum size in this case unless it is a constant with an
664 initializing expression, in which case we can get the size from
665 that. Note that the resulting size may still be a variable, so
666 this may end up with an indirect allocation. */
667 if (No (Renamed_Object (gnat_entity))
668 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
670 if (gnu_expr && kind == E_Constant)
672 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
673 if (CONTAINS_PLACEHOLDER_P (size))
675 /* If the initializing expression is itself a constant,
676 despite having a nominal type with self-referential
677 size, we can get the size directly from it. */
678 if (TREE_CODE (gnu_expr) == COMPONENT_REF
680 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
681 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
682 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
683 || DECL_READONLY_ONCE_ELAB
684 (TREE_OPERAND (gnu_expr, 0))))
685 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
688 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
693 /* We may have no GNU_EXPR because No_Initialization is
694 set even though there's an Expression. */
695 else if (kind == E_Constant
696 && (Nkind (Declaration_Node (gnat_entity))
697 == N_Object_Declaration)
698 && Present (Expression (Declaration_Node (gnat_entity))))
700 = TYPE_SIZE (gnat_to_gnu_type
702 (Expression (Declaration_Node (gnat_entity)))));
705 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
710 /* If the size is zero bytes, make it one byte since some linkers have
711 trouble with zero-sized objects. If the object will have a
712 template, that will make it nonzero so don't bother. Also avoid
713 doing that for an object renaming or an object with an address
714 clause, as we would lose useful information on the view size
715 (e.g. for null array slices) and we are not allocating the object
718 && integer_zerop (gnu_size)
719 && !TREE_OVERFLOW (gnu_size))
720 || (TYPE_SIZE (gnu_type)
721 && integer_zerop (TYPE_SIZE (gnu_type))
722 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
723 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
724 || !Is_Array_Type (Etype (gnat_entity)))
725 && No (Renamed_Object (gnat_entity))
726 && No (Address_Clause (gnat_entity)))
727 gnu_size = bitsize_unit_node;
729 /* If this is an object with no specified size and alignment, and
730 if either it is atomic or we are not optimizing alignment for
731 space and it is composite and not an exception, an Out parameter
732 or a reference to another object, and the size of its type is a
733 constant, set the alignment to the smallest one which is not
734 smaller than the size, with an appropriate cap. */
735 if (!gnu_size && align == 0
736 && (Is_Atomic (gnat_entity)
737 || (!Optimize_Alignment_Space (gnat_entity)
738 && kind != E_Exception
739 && kind != E_Out_Parameter
740 && Is_Composite_Type (Etype (gnat_entity))
741 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
743 && No (Renamed_Object (gnat_entity))
744 && No (Address_Clause (gnat_entity))))
745 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
747 /* No point in jumping through all the hoops needed in order
748 to support BIGGEST_ALIGNMENT if we don't really have to.
749 So we cap to the smallest alignment that corresponds to
750 a known efficient memory access pattern of the target. */
751 unsigned int align_cap = Is_Atomic (gnat_entity)
753 : get_mode_alignment (ptr_mode);
755 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
756 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
759 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
761 /* But make sure not to under-align the object. */
762 if (align <= TYPE_ALIGN (gnu_type))
765 /* And honor the minimum valid atomic alignment, if any. */
766 #ifdef MINIMUM_ATOMIC_ALIGNMENT
767 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
768 align = MINIMUM_ATOMIC_ALIGNMENT;
772 /* If the object is set to have atomic components, find the component
773 type and validate it.
775 ??? Note that we ignore Has_Volatile_Components on objects; it's
776 not at all clear what to do in that case. */
778 if (Has_Atomic_Components (gnat_entity))
780 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
781 ? TREE_TYPE (gnu_type) : gnu_type);
783 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
784 && TYPE_MULTI_ARRAY_P (gnu_inner))
785 gnu_inner = TREE_TYPE (gnu_inner);
787 check_ok_for_atomic (gnu_inner, gnat_entity, true);
790 /* Now check if the type of the object allows atomic access. Note
791 that we must test the type, even if this object has size and
792 alignment to allow such access, because we will be going
793 inside the padded record to assign to the object. We could fix
794 this by always copying via an intermediate value, but it's not
795 clear it's worth the effort. */
796 if (Is_Atomic (gnat_entity))
797 check_ok_for_atomic (gnu_type, gnat_entity, false);
799 /* If this is an aliased object with an unconstrained nominal subtype,
800 make a type that includes the template. */
801 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
802 && Is_Array_Type (Etype (gnat_entity))
803 && !type_annotate_only)
806 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
809 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
810 concat_name (gnu_entity_name,
814 #ifdef MINIMUM_ATOMIC_ALIGNMENT
815 /* If the size is a constant and no alignment is specified, force
816 the alignment to be the minimum valid atomic alignment. The
817 restriction on constant size avoids problems with variable-size
818 temporaries; if the size is variable, there's no issue with
819 atomic access. Also don't do this for a constant, since it isn't
820 necessary and can interfere with constant replacement. Finally,
821 do not do it for Out parameters since that creates an
822 size inconsistency with In parameters. */
823 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
824 && !FLOAT_TYPE_P (gnu_type)
825 && !const_flag && No (Renamed_Object (gnat_entity))
826 && !imported_p && No (Address_Clause (gnat_entity))
827 && kind != E_Out_Parameter
828 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
829 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
830 align = MINIMUM_ATOMIC_ALIGNMENT;
833 /* Make a new type with the desired size and alignment, if needed.
834 But do not take into account alignment promotions to compute the
835 size of the object. */
836 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
837 if (gnu_size || align > 0)
838 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
839 false, false, definition,
840 gnu_size ? true : false);
842 /* If this is a renaming, avoid as much as possible to create a new
843 object. However, in several cases, creating it is required.
844 This processing needs to be applied to the raw expression so
845 as to make it more likely to rename the underlying object. */
846 if (Present (Renamed_Object (gnat_entity)))
848 bool create_normal_object = false;
850 /* If the renamed object had padding, strip off the reference
851 to the inner object and reset our type. */
852 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
853 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
854 /* Strip useless conversions around the object. */
855 || (TREE_CODE (gnu_expr) == NOP_EXPR
856 && gnat_types_compatible_p
857 (TREE_TYPE (gnu_expr),
858 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
860 gnu_expr = TREE_OPERAND (gnu_expr, 0);
861 gnu_type = TREE_TYPE (gnu_expr);
864 /* Case 1: If this is a constant renaming stemming from a function
865 call, treat it as a normal object whose initial value is what
866 is being renamed. RM 3.3 says that the result of evaluating a
867 function call is a constant object. As a consequence, it can
868 be the inner object of a constant renaming. In this case, the
869 renaming must be fully instantiated, i.e. it cannot be a mere
870 reference to (part of) an existing object. */
873 tree inner_object = gnu_expr;
874 while (handled_component_p (inner_object))
875 inner_object = TREE_OPERAND (inner_object, 0);
876 if (TREE_CODE (inner_object) == CALL_EXPR)
877 create_normal_object = true;
880 /* Otherwise, see if we can proceed with a stabilized version of
881 the renamed entity or if we need to make a new object. */
882 if (!create_normal_object)
884 tree maybe_stable_expr = NULL_TREE;
887 /* Case 2: If the renaming entity need not be materialized and
888 the renamed expression is something we can stabilize, use
889 that for the renaming. At the global level, we can only do
890 this if we know no SAVE_EXPRs need be made, because the
891 expression we return might be used in arbitrary conditional
892 branches so we must force the SAVE_EXPRs evaluation
893 immediately and this requires a function context. */
894 if (!Materialize_Entity (gnat_entity)
895 && (!global_bindings_p ()
896 || (staticp (gnu_expr)
897 && !TREE_SIDE_EFFECTS (gnu_expr))))
900 = maybe_stabilize_reference (gnu_expr, true, &stable);
904 /* ??? No DECL_EXPR is created so we need to mark
905 the expression manually lest it is shared. */
906 if (global_bindings_p ())
907 MARK_VISITED (maybe_stable_expr);
908 gnu_decl = maybe_stable_expr;
909 save_gnu_tree (gnat_entity, gnu_decl, true);
911 annotate_object (gnat_entity, gnu_type, NULL_TREE,
916 /* The stabilization failed. Keep maybe_stable_expr
917 untouched here to let the pointer case below know
918 about that failure. */
921 /* Case 3: If this is a constant renaming and creating a
922 new object is allowed and cheap, treat it as a normal
923 object whose initial value is what is being renamed. */
925 && !Is_Composite_Type
926 (Underlying_Type (Etype (gnat_entity))))
929 /* Case 4: Make this into a constant pointer to the object we
930 are to rename and attach the object to the pointer if it is
931 something we can stabilize.
933 From the proper scope, attached objects will be referenced
934 directly instead of indirectly via the pointer to avoid
935 subtle aliasing problems with non-addressable entities.
936 They have to be stable because we must not evaluate the
937 variables in the expression every time the renaming is used.
938 The pointer is called a "renaming" pointer in this case.
940 In the rare cases where we cannot stabilize the renamed
941 object, we just make a "bare" pointer, and the renamed
942 entity is always accessed indirectly through it. */
945 gnu_type = build_reference_type (gnu_type);
946 inner_const_flag = TREE_READONLY (gnu_expr);
949 /* If the previous attempt at stabilizing failed, there
950 is no point in trying again and we reuse the result
951 without attaching it to the pointer. In this case it
952 will only be used as the initializing expression of
953 the pointer and thus needs no special treatment with
954 regard to multiple evaluations. */
955 if (maybe_stable_expr)
958 /* Otherwise, try to stabilize and attach the expression
959 to the pointer if the stabilization succeeds.
961 Note that this might introduce SAVE_EXPRs and we don't
962 check whether we're at the global level or not. This
963 is fine since we are building a pointer initializer and
964 neither the pointer nor the initializing expression can
965 be accessed before the pointer elaboration has taken
966 place in a correct program.
968 These SAVE_EXPRs will be evaluated at the right place
969 by either the evaluation of the initializer for the
970 non-global case or the elaboration code for the global
971 case, and will be attached to the elaboration procedure
972 in the latter case. */
976 = maybe_stabilize_reference (gnu_expr, true, &stable);
979 renamed_obj = maybe_stable_expr;
981 /* Attaching is actually performed downstream, as soon
982 as we have a VAR_DECL for the pointer we make. */
986 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
988 gnu_size = NULL_TREE;
994 /* Make a volatile version of this object's type if we are to make
995 the object volatile. We also interpret 13.3(19) conservatively
996 and disallow any optimizations for such a non-constant object. */
997 if ((Treat_As_Volatile (gnat_entity)
999 && (Is_Exported (gnat_entity)
1000 || Is_Imported (gnat_entity)
1001 || Present (Address_Clause (gnat_entity)))))
1002 && !TYPE_VOLATILE (gnu_type))
1003 gnu_type = build_qualified_type (gnu_type,
1004 (TYPE_QUALS (gnu_type)
1005 | TYPE_QUAL_VOLATILE));
1007 /* If we are defining an aliased object whose nominal subtype is
1008 unconstrained, the object is a record that contains both the
1009 template and the object. If there is an initializer, it will
1010 have already been converted to the right type, but we need to
1011 create the template if there is no initializer. */
1014 && TREE_CODE (gnu_type) == RECORD_TYPE
1015 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1016 /* Beware that padding might have been introduced above. */
1017 || (TYPE_PADDING_P (gnu_type)
1018 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1020 && TYPE_CONTAINS_TEMPLATE_P
1021 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1024 = TYPE_PADDING_P (gnu_type)
1025 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1026 : TYPE_FIELDS (gnu_type);
1029 = gnat_build_constructor
1033 build_template (TREE_TYPE (template_field),
1034 TREE_TYPE (TREE_CHAIN (template_field)),
1039 /* Convert the expression to the type of the object except in the
1040 case where the object's type is unconstrained or the object's type
1041 is a padded record whose field is of self-referential size. In
1042 the former case, converting will generate unnecessary evaluations
1043 of the CONSTRUCTOR to compute the size and in the latter case, we
1044 want to only copy the actual data. */
1046 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1047 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1048 && !(TYPE_IS_PADDING_P (gnu_type)
1049 && CONTAINS_PLACEHOLDER_P
1050 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1051 gnu_expr = convert (gnu_type, gnu_expr);
1053 /* If this is a pointer and it does not have an initializing
1054 expression, initialize it to NULL, unless the object is
1057 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1058 && !Is_Imported (gnat_entity) && !gnu_expr)
1059 gnu_expr = integer_zero_node;
1061 /* If we are defining the object and it has an Address clause, we must
1062 either get the address expression from the saved GCC tree for the
1063 object if it has a Freeze node, or elaborate the address expression
1064 here since the front-end has guaranteed that the elaboration has no
1065 effects in this case. */
1066 if (definition && Present (Address_Clause (gnat_entity)))
1069 = present_gnu_tree (gnat_entity)
1070 ? get_gnu_tree (gnat_entity)
1071 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1073 save_gnu_tree (gnat_entity, NULL_TREE, false);
1075 /* Ignore the size. It's either meaningless or was handled
1077 gnu_size = NULL_TREE;
1078 /* Convert the type of the object to a reference type that can
1079 alias everything as per 13.3(19). */
1081 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1082 gnu_address = convert (gnu_type, gnu_address);
1084 const_flag = !Is_Public (gnat_entity)
1085 || compile_time_known_address_p (Expression (Address_Clause
1088 /* If this is a deferred constant, the initializer is attached to
1090 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1093 (Expression (Declaration_Node (Full_View (gnat_entity))));
1095 /* If we don't have an initializing expression for the underlying
1096 variable, the initializing expression for the pointer is the
1097 specified address. Otherwise, we have to make a COMPOUND_EXPR
1098 to assign both the address and the initial value. */
1100 gnu_expr = gnu_address;
1103 = build2 (COMPOUND_EXPR, gnu_type,
1105 (MODIFY_EXPR, NULL_TREE,
1106 build_unary_op (INDIRECT_REF, NULL_TREE,
1112 /* If it has an address clause and we are not defining it, mark it
1113 as an indirect object. Likewise for Stdcall objects that are
1115 if ((!definition && Present (Address_Clause (gnat_entity)))
1116 || (Is_Imported (gnat_entity)
1117 && Has_Stdcall_Convention (gnat_entity)))
1119 /* Convert the type of the object to a reference type that can
1120 alias everything as per 13.3(19). */
1122 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1123 gnu_size = NULL_TREE;
1125 /* No point in taking the address of an initializing expression
1126 that isn't going to be used. */
1127 gnu_expr = NULL_TREE;
1129 /* If it has an address clause whose value is known at compile
1130 time, make the object a CONST_DECL. This will avoid a
1131 useless dereference. */
1132 if (Present (Address_Clause (gnat_entity)))
1134 Node_Id gnat_address
1135 = Expression (Address_Clause (gnat_entity));
1137 if (compile_time_known_address_p (gnat_address))
1139 gnu_expr = gnat_to_gnu (gnat_address);
1147 /* If we are at top level and this object is of variable size,
1148 make the actual type a hidden pointer to the real type and
1149 make the initializer be a memory allocation and initialization.
1150 Likewise for objects we aren't defining (presumed to be
1151 external references from other packages), but there we do
1152 not set up an initialization.
1154 If the object's size overflows, make an allocator too, so that
1155 Storage_Error gets raised. Note that we will never free
1156 such memory, so we presume it never will get allocated. */
1158 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1159 global_bindings_p () || !definition
1162 && ! allocatable_size_p (gnu_size,
1163 global_bindings_p () || !definition
1166 gnu_type = build_reference_type (gnu_type);
1167 gnu_size = NULL_TREE;
1171 /* In case this was a aliased object whose nominal subtype is
1172 unconstrained, the pointer above will be a thin pointer and
1173 build_allocator will automatically make the template.
1175 If we have a template initializer only (that we made above),
1176 pretend there is none and rely on what build_allocator creates
1177 again anyway. Otherwise (if we have a full initializer), get
1178 the data part and feed that to build_allocator.
1180 If we are elaborating a mutable object, tell build_allocator to
1181 ignore a possibly simpler size from the initializer, if any, as
1182 we must allocate the maximum possible size in this case. */
1186 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1188 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1189 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1192 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1194 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1195 && 1 == VEC_length (constructor_elt,
1196 CONSTRUCTOR_ELTS (gnu_expr)))
1200 = build_component_ref
1201 (gnu_expr, NULL_TREE,
1202 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1206 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1207 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1208 && !Is_Imported (gnat_entity))
1209 post_error ("?Storage_Error will be raised at run-time!",
1213 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1214 Empty, Empty, gnat_entity, mutable_p);
1218 gnu_expr = NULL_TREE;
1223 /* If this object would go into the stack and has an alignment larger
1224 than the largest stack alignment the back-end can honor, resort to
1225 a variable of "aligning type". */
1226 if (!global_bindings_p () && !static_p && definition
1227 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1229 /* Create the new variable. No need for extra room before the
1230 aligned field as this is in automatic storage. */
1232 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1233 TYPE_SIZE_UNIT (gnu_type),
1234 BIGGEST_ALIGNMENT, 0);
1236 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1237 NULL_TREE, gnu_new_type, NULL_TREE, false,
1238 false, false, false, NULL, gnat_entity);
1240 /* Initialize the aligned field if we have an initializer. */
1243 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1245 (gnu_new_var, NULL_TREE,
1246 TYPE_FIELDS (gnu_new_type), false),
1250 /* And setup this entity as a reference to the aligned field. */
1251 gnu_type = build_reference_type (gnu_type);
1254 (ADDR_EXPR, gnu_type,
1255 build_component_ref (gnu_new_var, NULL_TREE,
1256 TYPE_FIELDS (gnu_new_type), false));
1258 gnu_size = NULL_TREE;
1264 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1265 | TYPE_QUAL_CONST));
1267 /* Convert the expression to the type of the object except in the
1268 case where the object's type is unconstrained or the object's type
1269 is a padded record whose field is of self-referential size. In
1270 the former case, converting will generate unnecessary evaluations
1271 of the CONSTRUCTOR to compute the size and in the latter case, we
1272 want to only copy the actual data. */
1274 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1275 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1276 && !(TYPE_IS_PADDING_P (gnu_type)
1277 && CONTAINS_PLACEHOLDER_P
1278 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1279 gnu_expr = convert (gnu_type, gnu_expr);
1281 /* If this name is external or there was a name specified, use it,
1282 unless this is a VMS exception object since this would conflict
1283 with the symbol we need to export in addition. Don't use the
1284 Interface_Name if there is an address clause (see CD30005). */
1285 if (!Is_VMS_Exception (gnat_entity)
1286 && ((Present (Interface_Name (gnat_entity))
1287 && No (Address_Clause (gnat_entity)))
1288 || (Is_Public (gnat_entity)
1289 && (!Is_Imported (gnat_entity)
1290 || Is_Exported (gnat_entity)))))
1291 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1293 /* If this is constant initialized to a static constant and the
1294 object has an aggregate type, force it to be statically
1295 allocated. This will avoid an initialization copy. */
1296 if (!static_p && const_flag
1297 && gnu_expr && TREE_CONSTANT (gnu_expr)
1298 && AGGREGATE_TYPE_P (gnu_type)
1299 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1300 && !(TYPE_IS_PADDING_P (gnu_type)
1301 && !host_integerp (TYPE_SIZE_UNIT
1302 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1305 gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1306 gnu_expr, const_flag,
1307 Is_Public (gnat_entity),
1308 imported_p || !definition,
1309 static_p, attr_list, gnat_entity);
1310 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1311 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1312 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1314 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1315 if (global_bindings_p ())
1317 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1318 record_global_renaming_pointer (gnu_decl);
1322 if (definition && DECL_SIZE_UNIT (gnu_decl)
1323 && get_block_jmpbuf_decl ()
1324 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1325 || (flag_stack_check == GENERIC_STACK_CHECK
1326 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1327 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1328 add_stmt_with_node (build_call_1_expr
1329 (update_setjmp_buf_decl,
1330 build_unary_op (ADDR_EXPR, NULL_TREE,
1331 get_block_jmpbuf_decl ())),
1334 /* If we are defining an Out parameter and we're not optimizing,
1335 create a fake PARM_DECL for debugging purposes and make it
1336 point to the VAR_DECL. Suppress debug info for the latter
1337 but make sure it will still live on the stack so it can be
1338 accessed from within the debugger through the PARM_DECL. */
1339 if (kind == E_Out_Parameter && definition && !optimize)
1341 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1342 gnat_pushdecl (param, gnat_entity);
1343 SET_DECL_VALUE_EXPR (param, gnu_decl);
1344 DECL_HAS_VALUE_EXPR_P (param) = 1;
1346 debug_info_p = false;
1348 DECL_IGNORED_P (param) = 1;
1349 TREE_ADDRESSABLE (gnu_decl) = 1;
1352 /* If this is a public constant or we're not optimizing and we're not
1353 making a VAR_DECL for it, make one just for export or debugger use.
1354 Likewise if the address is taken or if either the object or type is
1355 aliased. Make an external declaration for a reference, unless this
1356 is a Standard entity since there no real symbol at the object level
1358 if (TREE_CODE (gnu_decl) == CONST_DECL
1359 && (definition || Sloc (gnat_entity) > Standard_Location)
1360 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1362 || Address_Taken (gnat_entity)
1363 || Is_Aliased (gnat_entity)
1364 || Is_Aliased (Etype (gnat_entity))))
1367 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1368 gnu_expr, true, Is_Public (gnat_entity),
1369 !definition, static_p, attr_list,
1372 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1374 /* As debugging information will be generated for the variable,
1375 do not generate information for the constant. */
1376 DECL_IGNORED_P (gnu_decl) = 1;
1379 /* If this is declared in a block that contains a block with an
1380 exception handler, we must force this variable in memory to
1381 suppress an invalid optimization. */
1382 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1383 && Exception_Mechanism != Back_End_Exceptions)
1384 TREE_ADDRESSABLE (gnu_decl) = 1;
1386 /* Back-annotate Esize and Alignment of the object if not already
1387 known. Note that we pick the values of the type, not those of
1388 the object, to shield ourselves from low-level platform-dependent
1389 adjustments like alignment promotion. This is both consistent with
1390 all the treatment above, where alignment and size are set on the
1391 type of the object and not on the object directly, and makes it
1392 possible to support all confirming representation clauses. */
1393 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1399 /* Return a TYPE_DECL for "void" that we previously made. */
1400 gnu_decl = TYPE_NAME (void_type_node);
1403 case E_Enumeration_Type:
1404 /* A special case: for the types Character and Wide_Character in
1405 Standard, we do not list all the literals. So if the literals
1406 are not specified, make this an unsigned type. */
1407 if (No (First_Literal (gnat_entity)))
1409 gnu_type = make_unsigned_type (esize);
1410 TYPE_NAME (gnu_type) = gnu_entity_name;
1412 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1413 This is needed by the DWARF-2 back-end to distinguish between
1414 unsigned integer types and character types. */
1415 TYPE_STRING_FLAG (gnu_type) = 1;
1420 /* We have a list of enumeral constants in First_Literal. We make a
1421 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1422 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1423 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1424 value of the literal. But when we have a regular boolean type, we
1425 simplify this a little by using a BOOLEAN_TYPE. */
1426 bool is_boolean = Is_Boolean_Type (gnat_entity)
1427 && !Has_Non_Standard_Rep (gnat_entity);
1428 tree gnu_literal_list = NULL_TREE;
1429 Entity_Id gnat_literal;
1431 if (Is_Unsigned_Type (gnat_entity))
1432 gnu_type = make_unsigned_type (esize);
1434 gnu_type = make_signed_type (esize);
1436 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1438 for (gnat_literal = First_Literal (gnat_entity);
1439 Present (gnat_literal);
1440 gnat_literal = Next_Literal (gnat_literal))
1443 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1445 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1446 gnu_type, gnu_value, true, false, false,
1447 false, NULL, gnat_literal);
1449 save_gnu_tree (gnat_literal, gnu_literal, false);
1450 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1451 gnu_value, gnu_literal_list);
1455 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1457 /* Note that the bounds are updated at the end of this function
1458 to avoid an infinite recursion since they refer to the type. */
1462 case E_Signed_Integer_Type:
1463 case E_Ordinary_Fixed_Point_Type:
1464 case E_Decimal_Fixed_Point_Type:
1465 /* For integer types, just make a signed type the appropriate number
1467 gnu_type = make_signed_type (esize);
1470 case E_Modular_Integer_Type:
1472 /* For modular types, make the unsigned type of the proper number
1473 of bits and then set up the modulus, if required. */
1474 tree gnu_modulus, gnu_high = NULL_TREE;
1476 /* Packed array types are supposed to be subtypes only. */
1477 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1479 gnu_type = make_unsigned_type (esize);
1481 /* Get the modulus in this type. If it overflows, assume it is because
1482 it is equal to 2**Esize. Note that there is no overflow checking
1483 done on unsigned type, so we detect the overflow by looking for
1484 a modulus of zero, which is otherwise invalid. */
1485 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1487 if (!integer_zerop (gnu_modulus))
1489 TYPE_MODULAR_P (gnu_type) = 1;
1490 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1491 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1492 convert (gnu_type, integer_one_node));
1495 /* If the upper bound is not maximal, make an extra subtype. */
1497 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1499 tree gnu_subtype = make_unsigned_type (esize);
1500 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1501 TREE_TYPE (gnu_subtype) = gnu_type;
1502 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1503 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1504 gnu_type = gnu_subtype;
1509 case E_Signed_Integer_Subtype:
1510 case E_Enumeration_Subtype:
1511 case E_Modular_Integer_Subtype:
1512 case E_Ordinary_Fixed_Point_Subtype:
1513 case E_Decimal_Fixed_Point_Subtype:
1515 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1516 not want to call create_range_type since we would like each subtype
1517 node to be distinct. ??? Historically this was in preparation for
1518 when memory aliasing is implemented, but that's obsolete now given
1519 the call to relate_alias_sets below.
1521 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1522 this fact is used by the arithmetic conversion functions.
1524 We elaborate the Ancestor_Subtype if it is not in the current unit
1525 and one of our bounds is non-static. We do this to ensure consistent
1526 naming in the case where several subtypes share the same bounds, by
1527 elaborating the first such subtype first, thus using its name. */
1530 && Present (Ancestor_Subtype (gnat_entity))
1531 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1532 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1533 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1534 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1536 /* Set the precision to the Esize except for bit-packed arrays. */
1537 if (Is_Packed_Array_Type (gnat_entity)
1538 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1539 esize = UI_To_Int (RM_Size (gnat_entity));
1541 /* This should be an unsigned type if the base type is unsigned or
1542 if the lower bound is constant and non-negative or if the type
1544 if (Is_Unsigned_Type (Etype (gnat_entity))
1545 || Is_Unsigned_Type (gnat_entity)
1546 || Has_Biased_Representation (gnat_entity))
1547 gnu_type = make_unsigned_type (esize);
1549 gnu_type = make_signed_type (esize);
1550 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1552 SET_TYPE_RM_MIN_VALUE
1554 convert (TREE_TYPE (gnu_type),
1555 elaborate_expression (Type_Low_Bound (gnat_entity),
1556 gnat_entity, get_identifier ("L"),
1558 Needs_Debug_Info (gnat_entity))));
1560 SET_TYPE_RM_MAX_VALUE
1562 convert (TREE_TYPE (gnu_type),
1563 elaborate_expression (Type_High_Bound (gnat_entity),
1564 gnat_entity, get_identifier ("U"),
1566 Needs_Debug_Info (gnat_entity))));
1568 /* One of the above calls might have caused us to be elaborated,
1569 so don't blow up if so. */
1570 if (present_gnu_tree (gnat_entity))
1572 maybe_present = true;
1576 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1577 = Has_Biased_Representation (gnat_entity);
1579 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1580 TYPE_STUB_DECL (gnu_type)
1581 = create_type_stub_decl (gnu_entity_name, gnu_type);
1583 /* Inherit our alias set from what we're a subtype of. Subtypes
1584 are not different types and a pointer can designate any instance
1585 within a subtype hierarchy. */
1586 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1588 /* For a packed array, make the original array type a parallel type. */
1590 && Is_Packed_Array_Type (gnat_entity)
1591 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1592 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1594 (Original_Array_Type (gnat_entity)));
1596 /* We have to handle clauses that under-align the type specially. */
1597 if ((Present (Alignment_Clause (gnat_entity))
1598 || (Is_Packed_Array_Type (gnat_entity)
1600 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1601 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1603 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1604 if (align >= TYPE_ALIGN (gnu_type))
1608 /* If the type we are dealing with represents a bit-packed array,
1609 we need to have the bits left justified on big-endian targets
1610 and right justified on little-endian targets. We also need to
1611 ensure that when the value is read (e.g. for comparison of two
1612 such values), we only get the good bits, since the unused bits
1613 are uninitialized. Both goals are accomplished by wrapping up
1614 the modular type in an enclosing record type. */
1615 if (Is_Packed_Array_Type (gnat_entity)
1616 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1618 tree gnu_field_type, gnu_field;
1620 /* Set the RM size before wrapping up the original type. */
1621 SET_TYPE_RM_SIZE (gnu_type,
1622 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1623 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1625 /* Create a stripped-down declaration, mainly for debugging. */
1626 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1627 debug_info_p, gnat_entity);
1629 /* Now save it and build the enclosing record type. */
1630 gnu_field_type = gnu_type;
1632 gnu_type = make_node (RECORD_TYPE);
1633 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1634 TYPE_PACKED (gnu_type) = 1;
1635 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1636 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1637 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1639 /* Propagate the alignment of the modular type to the record type,
1640 unless there is an alignment clause that under-aligns the type.
1641 This means that bit-packed arrays are given "ceil" alignment for
1642 their size by default, which may seem counter-intuitive but makes
1643 it possible to overlay them on modular types easily. */
1644 TYPE_ALIGN (gnu_type)
1645 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1647 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1649 /* Don't notify the field as "addressable", since we won't be taking
1650 it's address and it would prevent create_field_decl from making a
1652 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1653 gnu_field_type, gnu_type, 1,
1654 NULL_TREE, bitsize_zero_node, 0);
1656 /* Do not emit debug info until after the parallel type is added. */
1657 finish_record_type (gnu_type, gnu_field, 2, false);
1658 compute_record_mode (gnu_type);
1659 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1663 /* Make the original array type a parallel type. */
1664 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1665 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1667 (Original_Array_Type (gnat_entity)));
1669 rest_of_record_type_compilation (gnu_type);
1673 /* If the type we are dealing with has got a smaller alignment than the
1674 natural one, we need to wrap it up in a record type and under-align
1675 the latter. We reuse the padding machinery for this purpose. */
1678 tree gnu_field_type, gnu_field;
1680 /* Set the RM size before wrapping up the type. */
1681 SET_TYPE_RM_SIZE (gnu_type,
1682 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1684 /* Create a stripped-down declaration, mainly for debugging. */
1685 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1686 debug_info_p, gnat_entity);
1688 /* Now save it and build the enclosing record type. */
1689 gnu_field_type = gnu_type;
1691 gnu_type = make_node (RECORD_TYPE);
1692 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1693 TYPE_PACKED (gnu_type) = 1;
1694 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1695 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1696 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1697 TYPE_ALIGN (gnu_type) = align;
1698 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1700 /* Don't notify the field as "addressable", since we won't be taking
1701 it's address and it would prevent create_field_decl from making a
1703 gnu_field = create_field_decl (get_identifier ("F"),
1704 gnu_field_type, gnu_type, 1,
1705 NULL_TREE, bitsize_zero_node, 0);
1707 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1708 compute_record_mode (gnu_type);
1709 TYPE_PADDING_P (gnu_type) = 1;
1714 case E_Floating_Point_Type:
1715 /* If this is a VAX floating-point type, use an integer of the proper
1716 size. All the operations will be handled with ASM statements. */
1717 if (Vax_Float (gnat_entity))
1719 gnu_type = make_signed_type (esize);
1720 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1721 SET_TYPE_DIGITS_VALUE (gnu_type,
1722 UI_To_gnu (Digits_Value (gnat_entity),
1727 /* The type of the Low and High bounds can be our type if this is
1728 a type from Standard, so set them at the end of the function. */
1729 gnu_type = make_node (REAL_TYPE);
1730 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1731 layout_type (gnu_type);
1734 case E_Floating_Point_Subtype:
1735 if (Vax_Float (gnat_entity))
1737 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1743 && Present (Ancestor_Subtype (gnat_entity))
1744 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1745 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1746 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1747 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1750 gnu_type = make_node (REAL_TYPE);
1751 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1752 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1753 TYPE_GCC_MIN_VALUE (gnu_type)
1754 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1755 TYPE_GCC_MAX_VALUE (gnu_type)
1756 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1757 layout_type (gnu_type);
1759 SET_TYPE_RM_MIN_VALUE
1761 convert (TREE_TYPE (gnu_type),
1762 elaborate_expression (Type_Low_Bound (gnat_entity),
1763 gnat_entity, get_identifier ("L"),
1765 Needs_Debug_Info (gnat_entity))));
1767 SET_TYPE_RM_MAX_VALUE
1769 convert (TREE_TYPE (gnu_type),
1770 elaborate_expression (Type_High_Bound (gnat_entity),
1771 gnat_entity, get_identifier ("U"),
1773 Needs_Debug_Info (gnat_entity))));
1775 /* One of the above calls might have caused us to be elaborated,
1776 so don't blow up if so. */
1777 if (present_gnu_tree (gnat_entity))
1779 maybe_present = true;
1783 /* Inherit our alias set from what we're a subtype of, as for
1784 integer subtypes. */
1785 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1789 /* Array and String Types and Subtypes
1791 Unconstrained array types are represented by E_Array_Type and
1792 constrained array types are represented by E_Array_Subtype. There
1793 are no actual objects of an unconstrained array type; all we have
1794 are pointers to that type.
1796 The following fields are defined on array types and subtypes:
1798 Component_Type Component type of the array.
1799 Number_Dimensions Number of dimensions (an int).
1800 First_Index Type of first index. */
1805 Entity_Id gnat_index, gnat_name;
1806 const bool convention_fortran_p
1807 = (Convention (gnat_entity) == Convention_Fortran);
1808 const int ndim = Number_Dimensions (gnat_entity);
1809 tree gnu_template_fields = NULL_TREE;
1810 tree gnu_template_type = make_node (RECORD_TYPE);
1811 tree gnu_template_reference;
1812 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1813 tree gnu_fat_type = make_node (RECORD_TYPE);
1814 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1815 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1816 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
1819 TYPE_NAME (gnu_template_type)
1820 = create_concat_name (gnat_entity, "XUB");
1822 /* Make a node for the array. If we are not defining the array
1823 suppress expanding incomplete types. */
1824 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1828 defer_incomplete_level++;
1829 this_deferred = true;
1832 /* Build the fat pointer type. Use a "void *" object instead of
1833 a pointer to the array type since we don't have the array type
1834 yet (it will reference the fat pointer via the bounds). */
1835 tem = chainon (chainon (NULL_TREE,
1836 create_field_decl (get_identifier ("P_ARRAY"),
1839 NULL_TREE, NULL_TREE, 0)),
1840 create_field_decl (get_identifier ("P_BOUNDS"),
1843 NULL_TREE, NULL_TREE, 0));
1845 /* Make sure we can put this into a register. */
1846 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1848 /* Do not emit debug info for this record type since the types of its
1849 fields are still incomplete at this point. */
1850 finish_record_type (gnu_fat_type, tem, 0, false);
1851 TYPE_FAT_POINTER_P (gnu_fat_type) = 1;
1853 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1854 is the fat pointer. This will be used to access the individual
1855 fields once we build them. */
1856 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1857 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1858 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1859 gnu_template_reference
1860 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1861 TREE_READONLY (gnu_template_reference) = 1;
1863 /* Now create the GCC type for each index and add the fields for that
1864 index to the template. */
1865 for (index = (convention_fortran_p ? ndim - 1 : 0),
1866 gnat_index = First_Index (gnat_entity);
1867 0 <= index && index < ndim;
1868 index += (convention_fortran_p ? - 1 : 1),
1869 gnat_index = Next_Index (gnat_index))
1871 char field_name[16];
1872 tree gnu_index_base_type
1873 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1874 tree gnu_low_field, gnu_high_field, gnu_low, gnu_high, gnu_max;
1876 /* Make the FIELD_DECLs for the low and high bounds of this
1877 type and then make extractions of these fields from the
1879 sprintf (field_name, "LB%d", index);
1880 gnu_low_field = create_field_decl (get_identifier (field_name),
1881 gnu_index_base_type,
1882 gnu_template_type, 0,
1883 NULL_TREE, NULL_TREE, 0);
1884 Sloc_to_locus (Sloc (gnat_entity),
1885 &DECL_SOURCE_LOCATION (gnu_low_field));
1887 field_name[0] = 'U';
1888 gnu_high_field = create_field_decl (get_identifier (field_name),
1889 gnu_index_base_type,
1890 gnu_template_type, 0,
1891 NULL_TREE, NULL_TREE, 0);
1892 Sloc_to_locus (Sloc (gnat_entity),
1893 &DECL_SOURCE_LOCATION (gnu_high_field));
1895 gnu_temp_fields[index] = chainon (gnu_low_field, gnu_high_field);
1897 /* We can't use build_component_ref here since the template type
1898 isn't complete yet. */
1899 gnu_low = build3 (COMPONENT_REF, gnu_index_base_type,
1900 gnu_template_reference, gnu_low_field,
1902 gnu_high = build3 (COMPONENT_REF, gnu_index_base_type,
1903 gnu_template_reference, gnu_high_field,
1905 TREE_READONLY (gnu_low) = TREE_READONLY (gnu_high) = 1;
1907 /* Compute the size of this dimension. */
1909 = build3 (COND_EXPR, gnu_index_base_type,
1910 build2 (GE_EXPR, integer_type_node, gnu_high, gnu_low),
1912 build2 (MINUS_EXPR, gnu_index_base_type,
1913 gnu_low, fold_convert (gnu_index_base_type,
1914 integer_one_node)));
1916 /* Make a range type with the new range in the Ada base type.
1917 Then make an index type with the size range in sizetype. */
1918 gnu_index_types[index]
1919 = create_index_type (convert (sizetype, gnu_low),
1920 convert (sizetype, gnu_max),
1921 create_range_type (gnu_index_base_type,
1925 /* Update the maximum size of the array in elements. */
1928 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
1930 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
1932 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
1934 = size_binop (MAX_EXPR,
1935 size_binop (PLUS_EXPR, size_one_node,
1936 size_binop (MINUS_EXPR,
1940 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1941 && TREE_OVERFLOW (gnu_this_max))
1942 gnu_max_size = NULL_TREE;
1945 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1948 TYPE_NAME (gnu_index_types[index])
1949 = create_concat_name (gnat_entity, field_name);
1952 for (index = 0; index < ndim; index++)
1954 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1956 /* Install all the fields into the template. */
1957 finish_record_type (gnu_template_type, gnu_template_fields, 0,
1959 TYPE_READONLY (gnu_template_type) = 1;
1961 /* Now make the array of arrays and update the pointer to the array
1962 in the fat pointer. Note that it is the first field. */
1963 tem = gnat_to_gnu_component_type (gnat_entity, definition,
1966 /* If Component_Size is not already specified, annotate it with the
1967 size of the component. */
1968 if (Unknown_Component_Size (gnat_entity))
1969 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1971 /* Compute the maximum size of the array in units and bits. */
1974 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1975 TYPE_SIZE_UNIT (tem));
1976 gnu_max_size = size_binop (MULT_EXPR,
1977 convert (bitsizetype, gnu_max_size),
1981 gnu_max_size_unit = NULL_TREE;
1983 /* Now build the array type. */
1984 for (index = ndim - 1; index >= 0; index--)
1986 tem = build_array_type (tem, gnu_index_types[index]);
1987 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1988 if (array_type_has_nonaliased_component (tem, gnat_entity))
1989 TYPE_NONALIASED_COMPONENT (tem) = 1;
1992 /* If an alignment is specified, use it if valid. But ignore it
1993 for the original type of packed array types. If the alignment
1994 was requested with an explicit alignment clause, state so. */
1995 if (No (Packed_Array_Type (gnat_entity))
1996 && Known_Alignment (gnat_entity))
1999 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2001 if (Present (Alignment_Clause (gnat_entity)))
2002 TYPE_USER_ALIGN (tem) = 1;
2005 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2006 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2008 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2009 corresponding fat pointer. */
2010 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2011 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2012 SET_TYPE_MODE (gnu_type, BLKmode);
2013 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2014 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2016 /* If the maximum size doesn't overflow, use it. */
2018 && TREE_CODE (gnu_max_size) == INTEGER_CST
2019 && !TREE_OVERFLOW (gnu_max_size)
2020 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2021 && !TREE_OVERFLOW (gnu_max_size_unit))
2023 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2025 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2026 TYPE_SIZE_UNIT (tem));
2029 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2030 tem, NULL, !Comes_From_Source (gnat_entity),
2031 debug_info_p, gnat_entity);
2033 /* Give the fat pointer type a name. If this is a packed type, tell
2034 the debugger how to interpret the underlying bits. */
2035 if (Present (Packed_Array_Type (gnat_entity)))
2036 gnat_name = Packed_Array_Type (gnat_entity);
2038 gnat_name = gnat_entity;
2039 create_type_decl (create_concat_name (gnat_name, "XUP"),
2040 gnu_fat_type, NULL, true,
2041 debug_info_p, gnat_entity);
2043 /* Create the type to be used as what a thin pointer designates: an
2044 record type for the object and its template with the field offsets
2045 shifted to have the template at a negative offset. */
2046 tem = build_unc_object_type (gnu_template_type, tem,
2047 create_concat_name (gnat_name, "XUT"));
2048 shift_unc_components_for_thin_pointers (tem);
2050 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2051 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2055 case E_String_Subtype:
2056 case E_Array_Subtype:
2058 /* This is the actual data type for array variables. Multidimensional
2059 arrays are implemented as arrays of arrays. Note that arrays which
2060 have sparse enumeration subtypes as index components create sparse
2061 arrays, which is obviously space inefficient but so much easier to
2064 Also note that the subtype never refers to the unconstrained array
2065 type, which is somewhat at variance with Ada semantics.
2067 First check to see if this is simply a renaming of the array type.
2068 If so, the result is the array type. */
2070 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2071 if (!Is_Constrained (gnat_entity))
2075 Entity_Id gnat_index, gnat_base_index;
2076 const bool convention_fortran_p
2077 = (Convention (gnat_entity) == Convention_Fortran);
2078 const int ndim = Number_Dimensions (gnat_entity);
2079 tree gnu_base_type = gnu_type;
2080 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
2081 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2082 bool need_index_type_struct = false;
2085 /* First create the GCC type for each index and find out whether
2086 special types are needed for debugging information. */
2087 for (index = (convention_fortran_p ? ndim - 1 : 0),
2088 gnat_index = First_Index (gnat_entity),
2090 = First_Index (Implementation_Base_Type (gnat_entity));
2091 0 <= index && index < ndim;
2092 index += (convention_fortran_p ? - 1 : 1),
2093 gnat_index = Next_Index (gnat_index),
2094 gnat_base_index = Next_Index (gnat_base_index))
2096 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2098 = compare_tree_int (TYPE_RM_SIZE (gnu_index_type),
2099 TYPE_PRECISION (sizetype));
2100 const bool subrange_p = (prec_comp < 0)
2102 && TYPE_UNSIGNED (gnu_index_type)
2103 == TYPE_UNSIGNED (sizetype));
2104 const bool wider_p = (prec_comp > 0);
2105 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2106 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2107 tree gnu_min = convert (sizetype, gnu_orig_min);
2108 tree gnu_max = convert (sizetype, gnu_orig_max);
2109 tree gnu_base_index_type
2110 = get_unpadded_type (Etype (gnat_base_index));
2111 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2112 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2113 tree gnu_high, gnu_low;
2115 /* See if the base array type is already flat. If it is, we
2116 are probably compiling an ACATS test but it will cause the
2117 code below to malfunction if we don't handle it specially. */
2118 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2119 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2120 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2122 gnu_min = size_one_node;
2123 gnu_max = size_zero_node;
2127 /* Similarly, if one of the values overflows in sizetype and the
2128 range is null, use 1..0 for the sizetype bounds. */
2129 else if (!subrange_p
2130 && TREE_CODE (gnu_min) == INTEGER_CST
2131 && TREE_CODE (gnu_max) == INTEGER_CST
2132 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2133 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2135 gnu_min = size_one_node;
2136 gnu_max = size_zero_node;
2140 /* If the minimum and maximum values both overflow in sizetype,
2141 but the difference in the original type does not overflow in
2142 sizetype, ignore the overflow indication. */
2143 else if (!subrange_p
2144 && TREE_CODE (gnu_min) == INTEGER_CST
2145 && TREE_CODE (gnu_max) == INTEGER_CST
2146 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2149 fold_build2 (MINUS_EXPR, gnu_index_type,
2153 TREE_OVERFLOW (gnu_min) = 0;
2154 TREE_OVERFLOW (gnu_max) = 0;
2158 /* Compute the size of this dimension in the general case. We
2159 need to provide GCC with an upper bound to use but have to
2160 deal with the "superflat" case. There are three ways to do
2161 this. If we can prove that the array can never be superflat,
2162 we can just use the high bound of the index type. */
2163 else if (Nkind (gnat_index) == N_Range
2164 && cannot_be_superflat_p (gnat_index))
2167 /* Otherwise, if we can prove that the low bound minus one and
2168 the high bound cannot overflow, we can just use the expression
2169 MAX (hb, lb - 1). Similarly, if we can prove that the high
2170 bound plus one and the low bound cannot overflow, we can use
2171 the high bound as-is and MIN (hb + 1, lb) for the low bound.
2172 Otherwise, we have to fall back to the most general expression
2173 (hb >= lb) ? hb : lb - 1. Note that the comparison must be
2174 done in the original index type, to avoid any overflow during
2178 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2179 gnu_low = size_binop (PLUS_EXPR, gnu_max, size_one_node);
2181 /* If gnu_high is a constant that has overflowed, the low
2182 bound is the smallest integer so cannot be the maximum.
2183 If gnu_low is a constant that has overflowed, the high
2184 bound is the highest integer so cannot be the minimum. */
2185 if ((TREE_CODE (gnu_high) == INTEGER_CST
2186 && TREE_OVERFLOW (gnu_high))
2187 || (TREE_CODE (gnu_low) == INTEGER_CST
2188 && TREE_OVERFLOW (gnu_low)))
2191 /* If the index type is a subrange and gnu_high a constant
2192 that hasn't overflowed, we can use the maximum. */
2193 else if (subrange_p && TREE_CODE (gnu_high) == INTEGER_CST)
2194 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2196 /* If the index type is a subrange and gnu_low a constant
2197 that hasn't overflowed, we can use the minimum. */
2198 else if (subrange_p && TREE_CODE (gnu_low) == INTEGER_CST)
2201 gnu_min = size_binop (MIN_EXPR, gnu_min, gnu_low);
2206 = build_cond_expr (sizetype,
2207 build_binary_op (GE_EXPR,
2214 gnu_index_types[index]
2215 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2218 /* Update the maximum size of the array in elements. Here we
2219 see if any constraint on the index type of the base type
2220 can be used in the case of self-referential bound on the
2221 index type of the subtype. We look for a non-"infinite"
2222 and non-self-referential bound from any type involved and
2223 handle each bound separately. */
2226 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2227 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2228 tree gnu_base_index_base_type
2229 = get_base_type (gnu_base_index_type);
2230 tree gnu_base_base_min
2231 = convert (sizetype,
2232 TYPE_MIN_VALUE (gnu_base_index_base_type));
2233 tree gnu_base_base_max
2234 = convert (sizetype,
2235 TYPE_MAX_VALUE (gnu_base_index_base_type));
2237 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2238 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2239 && !TREE_OVERFLOW (gnu_base_min)))
2240 gnu_base_min = gnu_min;
2242 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2243 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2244 && !TREE_OVERFLOW (gnu_base_max)))
2245 gnu_base_max = gnu_max;
2247 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2248 && TREE_OVERFLOW (gnu_base_min))
2249 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2250 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2251 && TREE_OVERFLOW (gnu_base_max))
2252 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2253 gnu_max_size = NULL_TREE;
2257 = size_binop (MAX_EXPR,
2258 size_binop (PLUS_EXPR, size_one_node,
2259 size_binop (MINUS_EXPR,
2264 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2265 && TREE_OVERFLOW (gnu_this_max))
2266 gnu_max_size = NULL_TREE;
2269 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2273 /* We need special types for debugging information to point to
2274 the index types if they have variable bounds, are not integer
2275 types, are biased or are wider than sizetype. */
2276 if (!integer_onep (gnu_orig_min)
2277 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2278 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2279 || (TREE_TYPE (gnu_index_type)
2280 && TREE_CODE (TREE_TYPE (gnu_index_type))
2282 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2284 need_index_type_struct = true;
2287 /* Then flatten: create the array of arrays. For an array type
2288 used to implement a packed array, get the component type from
2289 the original array type since the representation clauses that
2290 can affect it are on the latter. */
2291 if (Is_Packed_Array_Type (gnat_entity)
2292 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2294 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2295 for (index = ndim - 1; index >= 0; index--)
2296 gnu_type = TREE_TYPE (gnu_type);
2298 /* One of the above calls might have caused us to be elaborated,
2299 so don't blow up if so. */
2300 if (present_gnu_tree (gnat_entity))
2302 maybe_present = true;
2308 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2311 /* One of the above calls might have caused us to be elaborated,
2312 so don't blow up if so. */
2313 if (present_gnu_tree (gnat_entity))
2315 maybe_present = true;
2320 /* Compute the maximum size of the array in units and bits. */
2323 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2324 TYPE_SIZE_UNIT (gnu_type));
2325 gnu_max_size = size_binop (MULT_EXPR,
2326 convert (bitsizetype, gnu_max_size),
2327 TYPE_SIZE (gnu_type));
2330 gnu_max_size_unit = NULL_TREE;
2332 /* Now build the array type. */
2333 for (index = ndim - 1; index >= 0; index --)
2335 gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
2336 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2337 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2338 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2341 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2342 TYPE_STUB_DECL (gnu_type)
2343 = create_type_stub_decl (gnu_entity_name, gnu_type);
2345 /* If we are at file level and this is a multi-dimensional array,
2346 we need to make a variable corresponding to the stride of the
2347 inner dimensions. */
2348 if (global_bindings_p () && ndim > 1)
2350 tree gnu_str_name = get_identifier ("ST");
2353 for (gnu_arr_type = TREE_TYPE (gnu_type);
2354 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2355 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2356 gnu_str_name = concat_name (gnu_str_name, "ST"))
2358 tree eltype = TREE_TYPE (gnu_arr_type);
2360 TYPE_SIZE (gnu_arr_type)
2361 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2362 gnat_entity, gnu_str_name,
2365 /* ??? For now, store the size as a multiple of the
2366 alignment of the element type in bytes so that we
2367 can see the alignment from the tree. */
2368 TYPE_SIZE_UNIT (gnu_arr_type)
2370 (MULT_EXPR, sizetype,
2371 elaborate_expression_1
2372 (build_binary_op (EXACT_DIV_EXPR, sizetype,
2373 TYPE_SIZE_UNIT (gnu_arr_type),
2374 size_int (TYPE_ALIGN (eltype)
2376 gnat_entity, concat_name (gnu_str_name, "A_U"),
2378 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2380 /* ??? create_type_decl is not invoked on the inner types so
2381 the MULT_EXPR node built above will never be marked. */
2382 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2386 /* If we need to write out a record type giving the names of the
2387 bounds for debugging purposes, do it now and make the record
2388 type a parallel type. This is not needed for a packed array
2389 since the bounds are conveyed by the original array type. */
2390 if (need_index_type_struct
2392 && !Is_Packed_Array_Type (gnat_entity))
2394 tree gnu_bound_rec = make_node (RECORD_TYPE);
2395 tree gnu_field_list = NULL_TREE;
2398 TYPE_NAME (gnu_bound_rec)
2399 = create_concat_name (gnat_entity, "XA");
2401 for (index = ndim - 1; index >= 0; index--)
2403 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2404 tree gnu_index_name = TYPE_NAME (gnu_index);
2406 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2407 gnu_index_name = DECL_NAME (gnu_index_name);
2409 /* Make sure to reference the types themselves, and not just
2410 their names, as the debugger may fall back on them. */
2411 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2413 0, NULL_TREE, NULL_TREE, 0);
2414 TREE_CHAIN (gnu_field) = gnu_field_list;
2415 gnu_field_list = gnu_field;
2418 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2419 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2422 /* Otherwise, for a packed array, make the original array type a
2424 else if (debug_info_p
2425 && Is_Packed_Array_Type (gnat_entity)
2426 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2427 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2429 (Original_Array_Type (gnat_entity)));
2431 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2432 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2433 = (Is_Packed_Array_Type (gnat_entity)
2434 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2436 /* If the size is self-referential and the maximum size doesn't
2437 overflow, use it. */
2438 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2440 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2441 && TREE_OVERFLOW (gnu_max_size))
2442 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2443 && TREE_OVERFLOW (gnu_max_size_unit)))
2445 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2446 TYPE_SIZE (gnu_type));
2447 TYPE_SIZE_UNIT (gnu_type)
2448 = size_binop (MIN_EXPR, gnu_max_size_unit,
2449 TYPE_SIZE_UNIT (gnu_type));
2452 /* Set our alias set to that of our base type. This gives all
2453 array subtypes the same alias set. */
2454 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2456 /* If this is a packed type, make this type the same as the packed
2457 array type, but do some adjusting in the type first. */
2458 if (Present (Packed_Array_Type (gnat_entity)))
2460 Entity_Id gnat_index;
2463 /* First finish the type we had been making so that we output
2464 debugging information for it. */
2465 if (Treat_As_Volatile (gnat_entity))
2467 = build_qualified_type (gnu_type,
2468 TYPE_QUALS (gnu_type)
2469 | TYPE_QUAL_VOLATILE);
2471 /* Make it artificial only if the base type was artificial too.
2472 That's sort of "morally" true and will make it possible for
2473 the debugger to look it up by name in DWARF, which is needed
2474 in order to decode the packed array type. */
2476 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2477 !Comes_From_Source (Etype (gnat_entity))
2478 && !Comes_From_Source (gnat_entity),
2479 debug_info_p, gnat_entity);
2481 /* Save it as our equivalent in case the call below elaborates
2483 save_gnu_tree (gnat_entity, gnu_decl, false);
2485 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2487 this_made_decl = true;
2488 gnu_type = TREE_TYPE (gnu_decl);
2489 save_gnu_tree (gnat_entity, NULL_TREE, false);
2491 gnu_inner = gnu_type;
2492 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2493 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2494 || TYPE_PADDING_P (gnu_inner)))
2495 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2497 /* We need to attach the index type to the type we just made so
2498 that the actual bounds can later be put into a template. */
2499 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2500 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2501 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2502 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2504 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2506 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2507 TYPE_MODULUS for modular types so we make an extra
2508 subtype if necessary. */
2509 if (TYPE_MODULAR_P (gnu_inner))
2512 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2513 TREE_TYPE (gnu_subtype) = gnu_inner;
2514 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2515 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2516 TYPE_MIN_VALUE (gnu_inner));
2517 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2518 TYPE_MAX_VALUE (gnu_inner));
2519 gnu_inner = gnu_subtype;
2522 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2524 #ifdef ENABLE_CHECKING
2525 /* Check for other cases of overloading. */
2526 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2530 for (gnat_index = First_Index (gnat_entity);
2531 Present (gnat_index);
2532 gnat_index = Next_Index (gnat_index))
2533 SET_TYPE_ACTUAL_BOUNDS
2535 tree_cons (NULL_TREE,
2536 get_unpadded_type (Etype (gnat_index)),
2537 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2539 if (Convention (gnat_entity) != Convention_Fortran)
2540 SET_TYPE_ACTUAL_BOUNDS
2541 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2543 if (TREE_CODE (gnu_type) == RECORD_TYPE
2544 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2545 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2550 /* Abort if packed array with no Packed_Array_Type field set. */
2551 gcc_assert (!Is_Packed (gnat_entity));
2555 case E_String_Literal_Subtype:
2556 /* Create the type for a string literal. */
2558 Entity_Id gnat_full_type
2559 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2560 && Present (Full_View (Etype (gnat_entity)))
2561 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2562 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2563 tree gnu_string_array_type
2564 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2565 tree gnu_string_index_type
2566 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2567 (TYPE_DOMAIN (gnu_string_array_type))));
2568 tree gnu_lower_bound
2569 = convert (gnu_string_index_type,
2570 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2571 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2572 tree gnu_length = ssize_int (length - 1);
2573 tree gnu_upper_bound
2574 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2576 convert (gnu_string_index_type, gnu_length));
2578 = create_index_type (convert (sizetype, gnu_lower_bound),
2579 convert (sizetype, gnu_upper_bound),
2580 create_range_type (gnu_string_index_type,
2586 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2588 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2589 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2590 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2594 /* Record Types and Subtypes
2596 The following fields are defined on record types:
2598 Has_Discriminants True if the record has discriminants
2599 First_Discriminant Points to head of list of discriminants
2600 First_Entity Points to head of list of fields
2601 Is_Tagged_Type True if the record is tagged
2603 Implementation of Ada records and discriminated records:
2605 A record type definition is transformed into the equivalent of a C
2606 struct definition. The fields that are the discriminants which are
2607 found in the Full_Type_Declaration node and the elements of the
2608 Component_List found in the Record_Type_Definition node. The
2609 Component_List can be a recursive structure since each Variant of
2610 the Variant_Part of the Component_List has a Component_List.
2612 Processing of a record type definition comprises starting the list of
2613 field declarations here from the discriminants and the calling the
2614 function components_to_record to add the rest of the fields from the
2615 component list and return the gnu type node. The function
2616 components_to_record will call itself recursively as it traverses
2620 if (Has_Complex_Representation (gnat_entity))
2623 = build_complex_type
2625 (Etype (Defining_Entity
2626 (First (Component_Items
2629 (Declaration_Node (gnat_entity)))))))));
2635 Node_Id full_definition = Declaration_Node (gnat_entity);
2636 Node_Id record_definition = Type_Definition (full_definition);
2637 Entity_Id gnat_field;
2638 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2639 /* Set PACKED in keeping with gnat_to_gnu_field. */
2641 = Is_Packed (gnat_entity)
2643 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2645 : (Known_Alignment (gnat_entity)
2646 || (Strict_Alignment (gnat_entity)
2647 && Known_Static_Esize (gnat_entity)))
2650 bool has_discr = Has_Discriminants (gnat_entity);
2651 bool has_rep = Has_Specified_Layout (gnat_entity);
2652 bool all_rep = has_rep;
2654 = (Is_Tagged_Type (gnat_entity)
2655 && Nkind (record_definition) == N_Derived_Type_Definition);
2656 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2658 /* See if all fields have a rep clause. Stop when we find one
2661 for (gnat_field = First_Entity (gnat_entity);
2662 Present (gnat_field);
2663 gnat_field = Next_Entity (gnat_field))
2664 if ((Ekind (gnat_field) == E_Component
2665 || Ekind (gnat_field) == E_Discriminant)
2666 && No (Component_Clause (gnat_field)))
2672 /* If this is a record extension, go a level further to find the
2673 record definition. Also, verify we have a Parent_Subtype. */
2676 if (!type_annotate_only
2677 || Present (Record_Extension_Part (record_definition)))
2678 record_definition = Record_Extension_Part (record_definition);
2680 gcc_assert (type_annotate_only
2681 || Present (Parent_Subtype (gnat_entity)));
2684 /* Make a node for the record. If we are not defining the record,
2685 suppress expanding incomplete types. */
2686 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2687 TYPE_NAME (gnu_type) = gnu_entity_name;
2688 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2692 defer_incomplete_level++;
2693 this_deferred = true;
2696 /* If both a size and rep clause was specified, put the size in
2697 the record type now so that it can get the proper mode. */
2698 if (has_rep && Known_Esize (gnat_entity))
2699 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2701 /* Always set the alignment here so that it can be used to
2702 set the mode, if it is making the alignment stricter. If
2703 it is invalid, it will be checked again below. If this is to
2704 be Atomic, choose a default alignment of a word unless we know
2705 the size and it's smaller. */
2706 if (Known_Alignment (gnat_entity))
2707 TYPE_ALIGN (gnu_type)
2708 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2709 else if (Is_Atomic (gnat_entity))
2710 TYPE_ALIGN (gnu_type)
2711 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2712 /* If a type needs strict alignment, the minimum size will be the
2713 type size instead of the RM size (see validate_size). Cap the
2714 alignment, lest it causes this type size to become too large. */
2715 else if (Strict_Alignment (gnat_entity)
2716 && Known_Static_Esize (gnat_entity))
2718 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2719 unsigned int raw_align = raw_size & -raw_size;
2720 if (raw_align < BIGGEST_ALIGNMENT)
2721 TYPE_ALIGN (gnu_type) = raw_align;
2724 TYPE_ALIGN (gnu_type) = 0;
2726 /* If we have a Parent_Subtype, make a field for the parent. If
2727 this record has rep clauses, force the position to zero. */
2728 if (Present (Parent_Subtype (gnat_entity)))
2730 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2733 /* A major complexity here is that the parent subtype will
2734 reference our discriminants in its Discriminant_Constraint
2735 list. But those must reference the parent component of this
2736 record which is of the parent subtype we have not built yet!
2737 To break the circle we first build a dummy COMPONENT_REF which
2738 represents the "get to the parent" operation and initialize
2739 each of those discriminants to a COMPONENT_REF of the above
2740 dummy parent referencing the corresponding discriminant of the
2741 base type of the parent subtype. */
2742 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2743 build0 (PLACEHOLDER_EXPR, gnu_type),
2744 build_decl (input_location,
2745 FIELD_DECL, NULL_TREE,
2750 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2751 Present (gnat_field);
2752 gnat_field = Next_Stored_Discriminant (gnat_field))
2753 if (Present (Corresponding_Discriminant (gnat_field)))
2756 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2760 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2761 gnu_get_parent, gnu_field, NULL_TREE),
2765 /* Then we build the parent subtype. If it has discriminants but
2766 the type itself has unknown discriminants, this means that it
2767 doesn't contain information about how the discriminants are
2768 derived from those of the ancestor type, so it cannot be used
2769 directly. Instead it is built by cloning the parent subtype
2770 of the underlying record view of the type, for which the above
2771 derivation of discriminants has been made explicit. */
2772 if (Has_Discriminants (gnat_parent)
2773 && Has_Unknown_Discriminants (gnat_entity))
2775 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2777 /* If we are defining the type, the underlying record
2778 view must already have been elaborated at this point.
2779 Otherwise do it now as its parent subtype cannot be
2780 technically elaborated on its own. */
2782 gcc_assert (present_gnu_tree (gnat_uview));
2784 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2786 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2788 /* Substitute the "get to the parent" of the type for that
2789 of its underlying record view in the cloned type. */
2790 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2791 Present (gnat_field);
2792 gnat_field = Next_Stored_Discriminant (gnat_field))
2793 if (Present (Corresponding_Discriminant (gnat_field)))
2795 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2797 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2798 gnu_get_parent, gnu_field, NULL_TREE);
2800 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2804 gnu_parent = gnat_to_gnu_type (gnat_parent);
2806 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2807 initially built. The discriminants must reference the fields
2808 of the parent subtype and not those of its base type for the
2809 placeholder machinery to properly work. */
2812 /* The actual parent subtype is the full view. */
2813 if (IN (Ekind (gnat_parent), Private_Kind))
2815 if (Present (Full_View (gnat_parent)))
2816 gnat_parent = Full_View (gnat_parent);
2818 gnat_parent = Underlying_Full_View (gnat_parent);
2821 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2822 Present (gnat_field);
2823 gnat_field = Next_Stored_Discriminant (gnat_field))
2824 if (Present (Corresponding_Discriminant (gnat_field)))
2826 Entity_Id field = Empty;
2827 for (field = First_Stored_Discriminant (gnat_parent);
2829 field = Next_Stored_Discriminant (field))
2830 if (same_discriminant_p (gnat_field, field))
2832 gcc_assert (Present (field));
2833 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2834 = gnat_to_gnu_field_decl (field);
2838 /* The "get to the parent" COMPONENT_REF must be given its
2840 TREE_TYPE (gnu_get_parent) = gnu_parent;
2842 /* ...and reference the _Parent field of this record. */
2844 = create_field_decl (get_identifier
2845 (Get_Name_String (Name_uParent)),
2846 gnu_parent, gnu_type, 0,
2848 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2850 ? bitsize_zero_node : NULL_TREE, 1);
2851 DECL_INTERNAL_P (gnu_field) = 1;
2852 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2853 TYPE_FIELDS (gnu_type) = gnu_field;
2856 /* Make the fields for the discriminants and put them into the record
2857 unless it's an Unchecked_Union. */
2859 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2860 Present (gnat_field);
2861 gnat_field = Next_Stored_Discriminant (gnat_field))
2863 /* If this is a record extension and this discriminant is the
2864 renaming of another discriminant, we've handled it above. */
2865 if (Present (Parent_Subtype (gnat_entity))
2866 && Present (Corresponding_Discriminant (gnat_field)))
2870 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
2873 /* Make an expression using a PLACEHOLDER_EXPR from the
2874 FIELD_DECL node just created and link that with the
2875 corresponding GNAT defining identifier. */
2876 save_gnu_tree (gnat_field,
2877 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2878 build0 (PLACEHOLDER_EXPR, gnu_type),
2879 gnu_field, NULL_TREE),
2882 if (!is_unchecked_union)
2884 TREE_CHAIN (gnu_field) = gnu_field_list;