1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2008, 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"
56 #ifndef MAX_FIXED_MODE_SIZE
57 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
60 /* Convention_Stdcall should be processed in a specific way on Windows targets
61 only. The macro below is a helper to avoid having to check for a Windows
62 specific attribute throughout this unit. */
64 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
67 #define Has_Stdcall_Convention(E) (0)
72 struct incomplete *next;
77 /* These variables are used to defer recursively expanding incomplete types
78 while we are processing an array, a record or a subprogram type. */
79 static int defer_incomplete_level = 0;
80 static struct incomplete *defer_incomplete_list;
82 /* This variable is used to delay expanding From_With_Type types until the
84 static struct incomplete *defer_limited_with;
86 /* These variables are used to defer finalizing types. The element of the
87 list is the TYPE_DECL associated with the type. */
88 static int defer_finalize_level = 0;
89 static VEC (tree,heap) *defer_finalize_list;
91 /* A hash table used to cache the result of annotate_value. */
92 static GTY ((if_marked ("tree_int_map_marked_p"),
93 param_is (struct tree_int_map))) htab_t annotate_value_cache;
95 static void copy_alias_set (tree, tree);
96 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
97 static bool allocatable_size_p (tree, bool);
98 static void prepend_one_attribute_to (struct attrib **,
99 enum attr_type, tree, tree, Node_Id);
100 static void prepend_attributes (Entity_Id, struct attrib **);
101 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
102 static bool is_variable_size (tree);
103 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
105 static tree make_packable_type (tree, bool);
106 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
107 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
109 static bool same_discriminant_p (Entity_Id, Entity_Id);
110 static bool array_type_has_nonaliased_component (Entity_Id, tree);
111 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
112 bool, bool, bool, bool);
113 static Uint annotate_value (tree);
114 static void annotate_rep (Entity_Id, tree);
115 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
116 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
117 static void set_rm_size (Uint, tree, Entity_Id);
118 static tree make_type_from_size (tree, tree, bool);
119 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
120 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
121 static void check_ok_for_atomic (tree, Entity_Id, bool);
122 static int compatible_signatures_p (tree ftype1, tree ftype2);
124 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
125 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
126 refer to an Ada type. */
129 gnat_to_gnu_type (Entity_Id gnat_entity)
133 /* The back end never attempts to annotate generic types */
134 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
135 return void_type_node;
137 /* Convert the ada entity type into a GCC TYPE_DECL node. */
138 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
139 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
140 return TREE_TYPE (gnu_decl);
143 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
144 entity, this routine returns the equivalent GCC tree for that entity
145 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
148 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
149 initial value (in GCC tree form). This is optional for variables.
150 For renamed entities, GNU_EXPR gives the object being renamed.
152 DEFINITION is nonzero if this call is intended for a definition. This is
153 used for separate compilation where it necessary to know whether an
154 external declaration or a definition should be created if the GCC equivalent
155 was not created previously. The value of 1 is normally used for a nonzero
156 DEFINITION, but a value of 2 is used in special circumstances, defined in
160 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
162 Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
164 tree gnu_type = NULL_TREE;
165 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
166 GNAT tree. This node will be associated with the GNAT node by calling
167 the save_gnu_tree routine at the end of the `switch' statement. */
168 tree gnu_decl = NULL_TREE;
169 /* true if we have already saved gnu_decl as a gnat association. */
171 /* Nonzero if we incremented defer_incomplete_level. */
172 bool this_deferred = false;
173 /* Nonzero if we incremented force_global. */
174 bool this_global = false;
175 /* Nonzero if we should check to see if elaborated during processing. */
176 bool maybe_present = false;
177 /* Nonzero if we made GNU_DECL and its type here. */
178 bool this_made_decl = false;
179 struct attrib *attr_list = NULL;
180 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
181 || debug_info_level == DINFO_LEVEL_VERBOSE);
182 Entity_Kind kind = Ekind (gnat_entity);
185 = ((Known_Esize (gnat_entity)
186 && UI_Is_In_Int_Range (Esize (gnat_entity)))
187 ? MIN (UI_To_Int (Esize (gnat_entity)),
188 IN (kind, Float_Kind)
189 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
190 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
191 : LONG_LONG_TYPE_SIZE)
192 : LONG_LONG_TYPE_SIZE);
195 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
196 unsigned int align = 0;
198 /* Since a use of an Itype is a definition, process it as such if it
199 is not in a with'ed unit. */
201 if (!definition && Is_Itype (gnat_entity)
202 && !present_gnu_tree (gnat_entity)
203 && In_Extended_Main_Code_Unit (gnat_entity))
205 /* Ensure that we are in a subprogram mentioned in the Scope
206 chain of this entity, our current scope is global,
207 or that we encountered a task or entry (where we can't currently
208 accurately check scoping). */
209 if (!current_function_decl
210 || DECL_ELABORATION_PROC_P (current_function_decl))
212 process_type (gnat_entity);
213 return get_gnu_tree (gnat_entity);
216 for (gnat_temp = Scope (gnat_entity);
217 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
219 if (Is_Type (gnat_temp))
220 gnat_temp = Underlying_Type (gnat_temp);
222 if (Ekind (gnat_temp) == E_Subprogram_Body)
224 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
226 if (IN (Ekind (gnat_temp), Subprogram_Kind)
227 && Present (Protected_Body_Subprogram (gnat_temp)))
228 gnat_temp = Protected_Body_Subprogram (gnat_temp);
230 if (Ekind (gnat_temp) == E_Entry
231 || Ekind (gnat_temp) == E_Entry_Family
232 || Ekind (gnat_temp) == E_Task_Type
233 || (IN (Ekind (gnat_temp), Subprogram_Kind)
234 && present_gnu_tree (gnat_temp)
235 && (current_function_decl
236 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
238 process_type (gnat_entity);
239 return get_gnu_tree (gnat_entity);
243 /* This abort means the entity "gnat_entity" has an incorrect scope,
244 i.e. that its scope does not correspond to the subprogram in which
249 /* If this is entity 0, something went badly wrong. */
250 gcc_assert (Present (gnat_entity));
252 /* If we've already processed this entity, return what we got last time.
253 If we are defining the node, we should not have already processed it.
254 In that case, we will abort below when we try to save a new GCC tree for
255 this object. We also need to handle the case of getting a dummy type
256 when a Full_View exists. */
258 if (present_gnu_tree (gnat_entity)
259 && (!definition || (Is_Type (gnat_entity) && imported_p)))
261 gnu_decl = get_gnu_tree (gnat_entity);
263 if (TREE_CODE (gnu_decl) == TYPE_DECL
264 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
265 && IN (kind, Incomplete_Or_Private_Kind)
266 && Present (Full_View (gnat_entity)))
268 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
271 save_gnu_tree (gnat_entity, NULL_TREE, false);
272 save_gnu_tree (gnat_entity, gnu_decl, false);
278 /* If this is a numeric or enumeral type, or an access type, a nonzero
279 Esize must be specified unless it was specified by the programmer. */
280 gcc_assert (!Unknown_Esize (gnat_entity)
281 || Has_Size_Clause (gnat_entity)
282 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
283 && (!IN (kind, Access_Kind)
284 || kind == E_Access_Protected_Subprogram_Type
285 || kind == E_Anonymous_Access_Protected_Subprogram_Type
286 || kind == E_Access_Subtype)));
288 /* Likewise, RM_Size must be specified for all discrete and fixed-point
290 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
291 || !Unknown_RM_Size (gnat_entity));
293 /* Get the name of the entity and set up the line number and filename of
294 the original definition for use in any decl we make. */
295 gnu_entity_id = get_entity_name (gnat_entity);
296 Sloc_to_locus (Sloc (gnat_entity), &input_location);
298 /* If we get here, it means we have not yet done anything with this
299 entity. If we are not defining it here, it must be external,
300 otherwise we should have defined it already. */
301 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
302 || kind == E_Discriminant || kind == E_Component
304 || (kind == E_Constant && Present (Full_View (gnat_entity)))
305 || IN (kind, Type_Kind));
307 /* For cases when we are not defining (i.e., we are referencing from
308 another compilation unit) Public entities, show we are at global level
309 for the purpose of computing scopes. Don't do this for components or
310 discriminants since the relevant test is whether or not the record is
311 being defined. But do this for Imported functions or procedures in
313 if ((!definition && Is_Public (gnat_entity)
314 && !Is_Statically_Allocated (gnat_entity)
315 && kind != E_Discriminant && kind != E_Component)
316 || (Is_Imported (gnat_entity)
317 && (kind == E_Function || kind == E_Procedure)))
318 force_global++, this_global = true;
320 /* Handle any attributes directly attached to the entity. */
321 if (Has_Gigi_Rep_Item (gnat_entity))
322 prepend_attributes (gnat_entity, &attr_list);
324 /* Machine_Attributes on types are expected to be propagated to subtypes.
325 The corresponding Gigi_Rep_Items are only attached to the first subtype
326 though, so we handle the propagation here. */
327 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
328 && !Is_First_Subtype (gnat_entity)
329 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
330 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
335 /* If this is a use of a deferred constant, get its full
337 if (!definition && Present (Full_View (gnat_entity)))
339 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
345 /* If we have an external constant that we are not defining, get the
346 expression that is was defined to represent. We may throw that
347 expression away later if it is not a constant. Do not retrieve the
348 expression if it is an aggregate or allocator, because in complex
349 instantiation contexts it may not be expanded */
351 && Present (Expression (Declaration_Node (gnat_entity)))
352 && !No_Initialization (Declaration_Node (gnat_entity))
353 && (Nkind (Expression (Declaration_Node (gnat_entity)))
355 && (Nkind (Expression (Declaration_Node (gnat_entity)))
357 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
359 /* Ignore deferred constant definitions; they are processed fully in the
360 front-end. For deferred constant references get the full definition.
361 On the other hand, constants that are renamings are handled like
362 variable renamings. If No_Initialization is set, this is not a
363 deferred constant but a constant whose value is built manually. */
364 if (definition && !gnu_expr
365 && !No_Initialization (Declaration_Node (gnat_entity))
366 && No (Renamed_Object (gnat_entity)))
368 gnu_decl = error_mark_node;
372 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
373 && Present (Full_View (gnat_entity)))
375 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
384 /* We used to special case VMS exceptions here to directly map them to
385 their associated condition code. Since this code had to be masked
386 dynamically to strip off the severity bits, this caused trouble in
387 the GCC/ZCX case because the "type" pointers we store in the tables
388 have to be static. We now don't special case here anymore, and let
389 the regular processing take place, which leaves us with a regular
390 exception data object for VMS exceptions too. The condition code
391 mapping is taken care of by the front end and the bitmasking by the
398 /* The GNAT record where the component was defined. */
399 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
401 /* If the variable is an inherited record component (in the case of
402 extended record types), just return the inherited entity, which
403 must be a FIELD_DECL. Likewise for discriminants.
404 For discriminants of untagged records which have explicit
405 stored discriminants, return the entity for the corresponding
406 stored discriminant. Also use Original_Record_Component
407 if the record has a private extension. */
409 if (Present (Original_Record_Component (gnat_entity))
410 && Original_Record_Component (gnat_entity) != gnat_entity)
413 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
414 gnu_expr, definition);
419 /* If the enclosing record has explicit stored discriminants,
420 then it is an untagged record. If the Corresponding_Discriminant
421 is not empty then this must be a renamed discriminant and its
422 Original_Record_Component must point to the corresponding explicit
423 stored discriminant (i.e., we should have taken the previous
426 else if (Present (Corresponding_Discriminant (gnat_entity))
427 && Is_Tagged_Type (gnat_record))
429 /* A tagged record has no explicit stored discriminants. */
431 gcc_assert (First_Discriminant (gnat_record)
432 == First_Stored_Discriminant (gnat_record));
434 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
435 gnu_expr, definition);
440 else if (Present (CR_Discriminant (gnat_entity))
441 && type_annotate_only)
443 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
444 gnu_expr, definition);
449 /* If the enclosing record has explicit stored discriminants,
450 then it is an untagged record. If the Corresponding_Discriminant
451 is not empty then this must be a renamed discriminant and its
452 Original_Record_Component must point to the corresponding explicit
453 stored discriminant (i.e., we should have taken the first
456 else if (Present (Corresponding_Discriminant (gnat_entity))
457 && (First_Discriminant (gnat_record)
458 != First_Stored_Discriminant (gnat_record)))
461 /* Otherwise, if we are not defining this and we have no GCC type
462 for the containing record, make one for it. Then we should
463 have made our own equivalent. */
464 else if (!definition && !present_gnu_tree (gnat_record))
466 /* ??? If this is in a record whose scope is a protected
467 type and we have an Original_Record_Component, use it.
468 This is a workaround for major problems in protected type
470 Entity_Id Scop = Scope (Scope (gnat_entity));
471 if ((Is_Protected_Type (Scop)
472 || (Is_Private_Type (Scop)
473 && Present (Full_View (Scop))
474 && Is_Protected_Type (Full_View (Scop))))
475 && Present (Original_Record_Component (gnat_entity)))
478 = gnat_to_gnu_entity (Original_Record_Component
485 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
486 gnu_decl = get_gnu_tree (gnat_entity);
492 /* Here we have no GCC type and this is a reference rather than a
493 definition. This should never happen. Most likely the cause is a
494 reference before declaration in the gnat tree for gnat_entity. */
498 case E_Loop_Parameter:
499 case E_Out_Parameter:
502 /* Simple variables, loop variables, Out parameters, and exceptions. */
505 bool used_by_ref = false;
507 = ((kind == E_Constant || kind == E_Variable)
508 && Is_True_Constant (gnat_entity)
509 && (((Nkind (Declaration_Node (gnat_entity))
510 == N_Object_Declaration)
511 && Present (Expression (Declaration_Node (gnat_entity))))
512 || Present (Renamed_Object (gnat_entity))));
513 bool inner_const_flag = const_flag;
514 bool static_p = Is_Statically_Allocated (gnat_entity);
515 bool mutable_p = false;
516 tree gnu_ext_name = NULL_TREE;
517 tree renamed_obj = NULL_TREE;
519 if (Present (Renamed_Object (gnat_entity)) && !definition)
521 if (kind == E_Exception)
522 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
525 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
528 /* Get the type after elaborating the renamed object. */
529 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
531 /* For a debug renaming declaration, build a pure debug entity. */
532 if (Present (Debug_Renaming_Link (gnat_entity)))
535 gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
536 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
537 if (global_bindings_p ())
538 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
540 addr = stack_pointer_rtx;
541 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
542 gnat_pushdecl (gnu_decl, gnat_entity);
546 /* If this is a loop variable, its type should be the base type.
547 This is because the code for processing a loop determines whether
548 a normal loop end test can be done by comparing the bounds of the
549 loop against those of the base type, which is presumed to be the
550 size used for computation. But this is not correct when the size
551 of the subtype is smaller than the type. */
552 if (kind == E_Loop_Parameter)
553 gnu_type = get_base_type (gnu_type);
555 /* Reject non-renamed objects whose types are unconstrained arrays or
556 any object whose type is a dummy type or VOID_TYPE. */
558 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
559 && No (Renamed_Object (gnat_entity)))
560 || TYPE_IS_DUMMY_P (gnu_type)
561 || TREE_CODE (gnu_type) == VOID_TYPE)
563 gcc_assert (type_annotate_only);
566 return error_mark_node;
569 /* If an alignment is specified, use it if valid. Note that
570 exceptions are objects but don't have alignments. We must do this
571 before we validate the size, since the alignment can affect the
573 if (kind != E_Exception && Known_Alignment (gnat_entity))
575 gcc_assert (Present (Alignment (gnat_entity)));
576 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
577 TYPE_ALIGN (gnu_type));
578 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
579 "PAD", false, definition, true);
582 /* If we are defining the object, see if it has a Size value and
583 validate it if so. If we are not defining the object and a Size
584 clause applies, simply retrieve the value. We don't want to ignore
585 the clause and it is expected to have been validated already. Then
586 get the new type, if any. */
588 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
589 gnat_entity, VAR_DECL, false,
590 Has_Size_Clause (gnat_entity));
591 else if (Has_Size_Clause (gnat_entity))
592 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
597 = make_type_from_size (gnu_type, gnu_size,
598 Has_Biased_Representation (gnat_entity));
600 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
601 gnu_size = NULL_TREE;
604 /* If this object has self-referential size, it must be a record with
605 a default value. We are supposed to allocate an object of the
606 maximum size in this case unless it is a constant with an
607 initializing expression, in which case we can get the size from
608 that. Note that the resulting size may still be a variable, so
609 this may end up with an indirect allocation. */
611 if (No (Renamed_Object (gnat_entity))
612 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
614 if (gnu_expr && kind == E_Constant)
616 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
617 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
619 /* We may have no GNU_EXPR because No_Initialization is
620 set even though there's an Expression. */
621 else if (kind == E_Constant
622 && (Nkind (Declaration_Node (gnat_entity))
623 == N_Object_Declaration)
624 && Present (Expression (Declaration_Node (gnat_entity))))
626 = TYPE_SIZE (gnat_to_gnu_type
628 (Expression (Declaration_Node (gnat_entity)))));
631 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
636 /* If the size is zero bytes, make it one byte since some linkers have
637 trouble with zero-sized objects. If the object will have a
638 template, that will make it nonzero so don't bother. Also avoid
639 doing that for an object renaming or an object with an address
640 clause, as we would lose useful information on the view size
641 (e.g. for null array slices) and we are not allocating the object
644 && integer_zerop (gnu_size)
645 && !TREE_OVERFLOW (gnu_size))
646 || (TYPE_SIZE (gnu_type)
647 && integer_zerop (TYPE_SIZE (gnu_type))
648 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
649 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
650 || !Is_Array_Type (Etype (gnat_entity)))
651 && !Present (Renamed_Object (gnat_entity))
652 && !Present (Address_Clause (gnat_entity)))
653 gnu_size = bitsize_unit_node;
655 /* If this is an atomic object with no specified size and alignment,
656 but where the size of the type is a constant, set the alignment to
657 the smallest not less than the size, or to the biggest meaningful
658 alignment, whichever is smaller. */
659 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
660 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
662 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
663 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
665 align = BIGGEST_ALIGNMENT;
667 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
670 /* If the object is set to have atomic components, find the component
671 type and validate it.
673 ??? Note that we ignore Has_Volatile_Components on objects; it's
674 not at all clear what to do in that case. */
676 if (Has_Atomic_Components (gnat_entity))
678 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
679 ? TREE_TYPE (gnu_type) : gnu_type);
681 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
682 && TYPE_MULTI_ARRAY_P (gnu_inner))
683 gnu_inner = TREE_TYPE (gnu_inner);
685 check_ok_for_atomic (gnu_inner, gnat_entity, true);
688 /* Now check if the type of the object allows atomic access. Note
689 that we must test the type, even if this object has size and
690 alignment to allow such access, because we will be going
691 inside the padded record to assign to the object. We could fix
692 this by always copying via an intermediate value, but it's not
693 clear it's worth the effort. */
694 if (Is_Atomic (gnat_entity))
695 check_ok_for_atomic (gnu_type, gnat_entity, false);
697 /* If this is an aliased object with an unconstrained nominal subtype,
698 make a type that includes the template. */
699 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
700 && Is_Array_Type (Etype (gnat_entity))
701 && !type_annotate_only)
704 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
707 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
708 concat_id_with_name (gnu_entity_id,
712 #ifdef MINIMUM_ATOMIC_ALIGNMENT
713 /* If the size is a constant and no alignment is specified, force
714 the alignment to be the minimum valid atomic alignment. The
715 restriction on constant size avoids problems with variable-size
716 temporaries; if the size is variable, there's no issue with
717 atomic access. Also don't do this for a constant, since it isn't
718 necessary and can interfere with constant replacement. Finally,
719 do not do it for Out parameters since that creates an
720 size inconsistency with In parameters. */
721 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
722 && !FLOAT_TYPE_P (gnu_type)
723 && !const_flag && No (Renamed_Object (gnat_entity))
724 && !imported_p && No (Address_Clause (gnat_entity))
725 && kind != E_Out_Parameter
726 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
727 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
728 align = MINIMUM_ATOMIC_ALIGNMENT;
731 /* Make a new type with the desired size and alignment, if needed. */
732 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
733 "PAD", false, definition, true);
735 /* Make a volatile version of this object's type if we are to make
736 the object volatile. We also interpret 13.3(19) conservatively
737 and disallow any optimizations for an object covered by it. */
738 if ((Treat_As_Volatile (gnat_entity)
739 || (Is_Exported (gnat_entity)
740 /* Exclude exported constants created by the compiler,
741 which should boil down to static dispatch tables and
742 make it possible to put them in read-only memory. */
743 && (Comes_From_Source (gnat_entity) || !const_flag))
744 || Is_Imported (gnat_entity)
745 || Present (Address_Clause (gnat_entity)))
746 && !TYPE_VOLATILE (gnu_type))
747 gnu_type = build_qualified_type (gnu_type,
748 (TYPE_QUALS (gnu_type)
749 | TYPE_QUAL_VOLATILE));
751 /* If this is a renaming, avoid as much as possible to create a new
752 object. However, in several cases, creating it is required.
753 This processing needs to be applied to the raw expression so
754 as to make it more likely to rename the underlying object. */
755 if (Present (Renamed_Object (gnat_entity)))
757 bool create_normal_object = false;
759 /* If the renamed object had padding, strip off the reference
760 to the inner object and reset our type. */
761 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
762 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
764 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
765 /* Strip useless conversions around the object. */
766 || TREE_CODE (gnu_expr) == NOP_EXPR)
768 gnu_expr = TREE_OPERAND (gnu_expr, 0);
769 gnu_type = TREE_TYPE (gnu_expr);
772 /* Case 1: If this is a constant renaming stemming from a function
773 call, treat it as a normal object whose initial value is what
774 is being renamed. RM 3.3 says that the result of evaluating a
775 function call is a constant object. As a consequence, it can
776 be the inner object of a constant renaming. In this case, the
777 renaming must be fully instantiated, i.e. it cannot be a mere
778 reference to (part of) an existing object. */
781 tree inner_object = gnu_expr;
782 while (handled_component_p (inner_object))
783 inner_object = TREE_OPERAND (inner_object, 0);
784 if (TREE_CODE (inner_object) == CALL_EXPR)
785 create_normal_object = true;
788 /* Otherwise, see if we can proceed with a stabilized version of
789 the renamed entity or if we need to make a new object. */
790 if (!create_normal_object)
792 tree maybe_stable_expr = NULL_TREE;
795 /* Case 2: If the renaming entity need not be materialized and
796 the renamed expression is something we can stabilize, use
797 that for the renaming. At the global level, we can only do
798 this if we know no SAVE_EXPRs need be made, because the
799 expression we return might be used in arbitrary conditional
800 branches so we must force the SAVE_EXPRs evaluation
801 immediately and this requires a function context. */
802 if (!Materialize_Entity (gnat_entity)
803 && (!global_bindings_p ()
804 || (staticp (gnu_expr)
805 && !TREE_SIDE_EFFECTS (gnu_expr))))
808 = maybe_stabilize_reference (gnu_expr, true, &stable);
812 gnu_decl = maybe_stable_expr;
813 /* ??? No DECL_EXPR is created so we need to mark
814 the expression manually lest it is shared. */
815 if (global_bindings_p ())
816 TREE_VISITED (gnu_decl) = 1;
817 save_gnu_tree (gnat_entity, gnu_decl, true);
822 /* The stabilization failed. Keep maybe_stable_expr
823 untouched here to let the pointer case below know
824 about that failure. */
827 /* Case 3: If this is a constant renaming and creating a
828 new object is allowed and cheap, treat it as a normal
829 object whose initial value is what is being renamed. */
830 if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
833 /* Case 4: Make this into a constant pointer to the object we
834 are to rename and attach the object to the pointer if it is
835 something we can stabilize.
837 From the proper scope, attached objects will be referenced
838 directly instead of indirectly via the pointer to avoid
839 subtle aliasing problems with non-addressable entities.
840 They have to be stable because we must not evaluate the
841 variables in the expression every time the renaming is used.
842 The pointer is called a "renaming" pointer in this case.
844 In the rare cases where we cannot stabilize the renamed
845 object, we just make a "bare" pointer, and the renamed
846 entity is always accessed indirectly through it. */
849 gnu_type = build_reference_type (gnu_type);
850 inner_const_flag = TREE_READONLY (gnu_expr);
853 /* If the previous attempt at stabilizing failed, there
854 is no point in trying again and we reuse the result
855 without attaching it to the pointer. In this case it
856 will only be used as the initializing expression of
857 the pointer and thus needs no special treatment with
858 regard to multiple evaluations. */
859 if (maybe_stable_expr)
862 /* Otherwise, try to stabilize and attach the expression
863 to the pointer if the stabilization succeeds.
865 Note that this might introduce SAVE_EXPRs and we don't
866 check whether we're at the global level or not. This
867 is fine since we are building a pointer initializer and
868 neither the pointer nor the initializing expression can
869 be accessed before the pointer elaboration has taken
870 place in a correct program.
872 These SAVE_EXPRs will be evaluated at the right place
873 by either the evaluation of the initializer for the
874 non-global case or the elaboration code for the global
875 case, and will be attached to the elaboration procedure
876 in the latter case. */
880 = maybe_stabilize_reference (gnu_expr, true, &stable);
883 renamed_obj = maybe_stable_expr;
885 /* Attaching is actually performed downstream, as soon
886 as we have a VAR_DECL for the pointer we make. */
890 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
892 gnu_size = NULL_TREE;
898 /* If this is an aliased object whose nominal subtype is unconstrained,
899 the object is a record that contains both the template and
900 the object. If there is an initializer, it will have already
901 been converted to the right type, but we need to create the
902 template if there is no initializer. */
904 && TREE_CODE (gnu_type) == RECORD_TYPE
905 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
906 /* Beware that padding might have been introduced
907 via maybe_pad_type above. */
908 || (TYPE_IS_PADDING_P (gnu_type)
909 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
911 && TYPE_CONTAINS_TEMPLATE_P
912 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
916 = TYPE_IS_PADDING_P (gnu_type)
917 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
918 : TYPE_FIELDS (gnu_type);
921 = gnat_build_constructor
925 build_template (TREE_TYPE (template_field),
926 TREE_TYPE (TREE_CHAIN (template_field)),
931 /* Convert the expression to the type of the object except in the
932 case where the object's type is unconstrained or the object's type
933 is a padded record whose field is of self-referential size. In
934 the former case, converting will generate unnecessary evaluations
935 of the CONSTRUCTOR to compute the size and in the latter case, we
936 want to only copy the actual data. */
938 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
939 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
940 && !(TREE_CODE (gnu_type) == RECORD_TYPE
941 && TYPE_IS_PADDING_P (gnu_type)
942 && (CONTAINS_PLACEHOLDER_P
943 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
944 gnu_expr = convert (gnu_type, gnu_expr);
946 /* If this is a pointer and it does not have an initializing
947 expression, initialize it to NULL, unless the object is
950 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
951 && !Is_Imported (gnat_entity) && !gnu_expr)
952 gnu_expr = integer_zero_node;
954 /* If we are defining the object and it has an Address clause we must
955 get the address expression from the saved GCC tree for the
956 object if the object has a Freeze_Node. Otherwise, we elaborate
957 the address expression here since the front-end has guaranteed
958 in that case that the elaboration has no effects. Note that
959 only the latter mechanism is currently in use. */
960 if (definition && Present (Address_Clause (gnat_entity)))
963 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
964 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
966 save_gnu_tree (gnat_entity, NULL_TREE, false);
968 /* Ignore the size. It's either meaningless or was handled
970 gnu_size = NULL_TREE;
971 /* Convert the type of the object to a reference type that can
972 alias everything as per 13.3(19). */
974 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
975 gnu_address = convert (gnu_type, gnu_address);
977 const_flag = !Is_Public (gnat_entity);
979 /* If we don't have an initializing expression for the underlying
980 variable, the initializing expression for the pointer is the
981 specified address. Otherwise, we have to make a COMPOUND_EXPR
982 to assign both the address and the initial value. */
984 gnu_expr = gnu_address;
987 = build2 (COMPOUND_EXPR, gnu_type,
989 (MODIFY_EXPR, NULL_TREE,
990 build_unary_op (INDIRECT_REF, NULL_TREE,
996 /* If it has an address clause and we are not defining it, mark it
997 as an indirect object. Likewise for Stdcall objects that are
999 if ((!definition && Present (Address_Clause (gnat_entity)))
1000 || (Is_Imported (gnat_entity)
1001 && Has_Stdcall_Convention (gnat_entity)))
1003 /* Convert the type of the object to a reference type that can
1004 alias everything as per 13.3(19). */
1006 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1007 gnu_size = NULL_TREE;
1009 gnu_expr = NULL_TREE;
1010 /* No point in taking the address of an initializing expression
1011 that isn't going to be used. */
1016 /* If we are at top level and this object is of variable size,
1017 make the actual type a hidden pointer to the real type and
1018 make the initializer be a memory allocation and initialization.
1019 Likewise for objects we aren't defining (presumed to be
1020 external references from other packages), but there we do
1021 not set up an initialization.
1023 If the object's size overflows, make an allocator too, so that
1024 Storage_Error gets raised. Note that we will never free
1025 such memory, so we presume it never will get allocated. */
1027 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1028 global_bindings_p () || !definition
1031 && ! allocatable_size_p (gnu_size,
1032 global_bindings_p () || !definition
1035 gnu_type = build_reference_type (gnu_type);
1036 gnu_size = NULL_TREE;
1040 /* In case this was a aliased object whose nominal subtype is
1041 unconstrained, the pointer above will be a thin pointer and
1042 build_allocator will automatically make the template.
1044 If we have a template initializer only (that we made above),
1045 pretend there is none and rely on what build_allocator creates
1046 again anyway. Otherwise (if we have a full initializer), get
1047 the data part and feed that to build_allocator.
1049 If we are elaborating a mutable object, tell build_allocator to
1050 ignore a possibly simpler size from the initializer, if any, as
1051 we must allocate the maximum possible size in this case. */
1055 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1057 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1058 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1061 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1063 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1064 && 1 == VEC_length (constructor_elt,
1065 CONSTRUCTOR_ELTS (gnu_expr)))
1069 = build_component_ref
1070 (gnu_expr, NULL_TREE,
1071 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1075 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1076 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1077 && !Is_Imported (gnat_entity))
1078 post_error ("?Storage_Error will be raised at run-time!",
1081 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1082 0, 0, gnat_entity, mutable_p);
1086 gnu_expr = NULL_TREE;
1091 /* If this object would go into the stack and has an alignment larger
1092 than the largest stack alignment the back-end can honor, resort to
1093 a variable of "aligning type". */
1094 if (!global_bindings_p () && !static_p && definition
1095 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1097 /* Create the new variable. No need for extra room before the
1098 aligned field as this is in automatic storage. */
1100 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1101 TYPE_SIZE_UNIT (gnu_type),
1102 BIGGEST_ALIGNMENT, 0);
1104 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1105 NULL_TREE, gnu_new_type, NULL_TREE, false,
1106 false, false, false, NULL, gnat_entity);
1108 /* Initialize the aligned field if we have an initializer. */
1111 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1113 (gnu_new_var, NULL_TREE,
1114 TYPE_FIELDS (gnu_new_type), false),
1118 /* And setup this entity as a reference to the aligned field. */
1119 gnu_type = build_reference_type (gnu_type);
1122 (ADDR_EXPR, gnu_type,
1123 build_component_ref (gnu_new_var, NULL_TREE,
1124 TYPE_FIELDS (gnu_new_type), false));
1126 gnu_size = NULL_TREE;
1132 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1133 | TYPE_QUAL_CONST));
1135 /* Convert the expression to the type of the object except in the
1136 case where the object's type is unconstrained or the object's type
1137 is a padded record whose field is of self-referential size. In
1138 the former case, converting will generate unnecessary evaluations
1139 of the CONSTRUCTOR to compute the size and in the latter case, we
1140 want to only copy the actual data. */
1142 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1143 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1144 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1145 && TYPE_IS_PADDING_P (gnu_type)
1146 && (CONTAINS_PLACEHOLDER_P
1147 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1148 gnu_expr = convert (gnu_type, gnu_expr);
1150 /* If this name is external or there was a name specified, use it,
1151 unless this is a VMS exception object since this would conflict
1152 with the symbol we need to export in addition. Don't use the
1153 Interface_Name if there is an address clause (see CD30005). */
1154 if (!Is_VMS_Exception (gnat_entity)
1155 && ((Present (Interface_Name (gnat_entity))
1156 && No (Address_Clause (gnat_entity)))
1157 || (Is_Public (gnat_entity)
1158 && (!Is_Imported (gnat_entity)
1159 || Is_Exported (gnat_entity)))))
1160 gnu_ext_name = create_concat_name (gnat_entity, 0);
1162 /* If this is constant initialized to a static constant and the
1163 object has an aggregate type, force it to be statically
1165 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1166 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1167 && (AGGREGATE_TYPE_P (gnu_type)
1168 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1169 && TYPE_IS_PADDING_P (gnu_type))))
1172 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1173 gnu_expr, const_flag,
1174 Is_Public (gnat_entity),
1175 imported_p || !definition,
1176 static_p, attr_list, gnat_entity);
1177 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1178 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1179 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1181 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1182 if (global_bindings_p ())
1184 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1185 record_global_renaming_pointer (gnu_decl);
1189 if (definition && DECL_SIZE (gnu_decl)
1190 && get_block_jmpbuf_decl ()
1191 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1192 || (flag_stack_check && !STACK_CHECK_BUILTIN
1193 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1194 STACK_CHECK_MAX_VAR_SIZE))))
1195 add_stmt_with_node (build_call_1_expr
1196 (update_setjmp_buf_decl,
1197 build_unary_op (ADDR_EXPR, NULL_TREE,
1198 get_block_jmpbuf_decl ())),
1201 /* If this is a public constant or we're not optimizing and we're not
1202 making a VAR_DECL for it, make one just for export or debugger use.
1203 Likewise if the address is taken or if either the object or type is
1204 aliased. Make an external declaration for a reference, unless this
1205 is a Standard entity since there no real symbol at the object level
1207 if (TREE_CODE (gnu_decl) == CONST_DECL
1208 && (definition || Sloc (gnat_entity) > Standard_Location)
1209 && (Is_Public (gnat_entity)
1211 || Address_Taken (gnat_entity)
1212 || Is_Aliased (gnat_entity)
1213 || Is_Aliased (Etype (gnat_entity))))
1216 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1217 gnu_expr, true, Is_Public (gnat_entity),
1218 !definition, static_p, NULL,
1221 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1224 /* If this is declared in a block that contains a block with an
1225 exception handler, we must force this variable in memory to
1226 suppress an invalid optimization. */
1227 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1228 && Exception_Mechanism != Back_End_Exceptions)
1229 TREE_ADDRESSABLE (gnu_decl) = 1;
1231 gnu_type = TREE_TYPE (gnu_decl);
1233 /* Back-annotate Alignment and Esize of the object if not already
1234 known, except for when the object is actually a pointer to the
1235 real object, since alignment and size of a pointer don't have
1236 anything to do with those of the designated object. Note that
1237 we pick the values of the type, not those of the object, to
1238 shield ourselves from low-level platform-dependent adjustments
1239 like alignment promotion. This is both consistent with all the
1240 treatment above, where alignment and size are set on the type of
1241 the object and not on the object directly, and makes it possible
1242 to support confirming representation clauses in all cases. */
1244 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1245 Set_Alignment (gnat_entity,
1246 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1248 if (!used_by_ref && Unknown_Esize (gnat_entity))
1252 if (TREE_CODE (gnu_type) == RECORD_TYPE
1253 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1255 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1257 gnu_back_size = TYPE_SIZE (gnu_type);
1259 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1265 /* Return a TYPE_DECL for "void" that we previously made. */
1266 gnu_decl = void_type_decl_node;
1269 case E_Enumeration_Type:
1270 /* A special case, for the types Character and Wide_Character in
1271 Standard, we do not list all the literals. So if the literals
1272 are not specified, make this an unsigned type. */
1273 if (No (First_Literal (gnat_entity)))
1275 gnu_type = make_unsigned_type (esize);
1276 TYPE_NAME (gnu_type) = gnu_entity_id;
1278 /* Set the TYPE_STRING_FLAG for Ada Character and
1279 Wide_Character types. This is needed by the dwarf-2 debug writer to
1280 distinguish between unsigned integer types and character types. */
1281 TYPE_STRING_FLAG (gnu_type) = 1;
1285 /* Normal case of non-character type, or non-Standard character type */
1287 /* Here we have a list of enumeral constants in First_Literal.
1288 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1289 the list to be places into TYPE_FIELDS. Each node in the list
1290 is a TREE_LIST node whose TREE_VALUE is the literal name
1291 and whose TREE_PURPOSE is the value of the literal.
1293 Esize contains the number of bits needed to represent the enumeral
1294 type, Type_Low_Bound also points to the first literal and
1295 Type_High_Bound points to the last literal. */
1297 Entity_Id gnat_literal;
1298 tree gnu_literal_list = NULL_TREE;
1300 if (Is_Unsigned_Type (gnat_entity))
1301 gnu_type = make_unsigned_type (esize);
1303 gnu_type = make_signed_type (esize);
1305 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1307 for (gnat_literal = First_Literal (gnat_entity);
1308 Present (gnat_literal);
1309 gnat_literal = Next_Literal (gnat_literal))
1311 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1314 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1315 gnu_type, gnu_value, true, false, false,
1316 false, NULL, gnat_literal);
1318 save_gnu_tree (gnat_literal, gnu_literal, false);
1319 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1320 gnu_value, gnu_literal_list);
1323 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1325 /* Note that the bounds are updated at the end of this function
1326 because to avoid an infinite recursion when we get the bounds of
1327 this type, since those bounds are objects of this type. */
1331 case E_Signed_Integer_Type:
1332 case E_Ordinary_Fixed_Point_Type:
1333 case E_Decimal_Fixed_Point_Type:
1334 /* For integer types, just make a signed type the appropriate number
1336 gnu_type = make_signed_type (esize);
1339 case E_Modular_Integer_Type:
1340 /* For modular types, make the unsigned type of the proper number of
1341 bits and then set up the modulus, if required. */
1343 enum machine_mode mode;
1347 if (Is_Packed_Array_Type (gnat_entity))
1348 esize = UI_To_Int (RM_Size (gnat_entity));
1350 /* Find the smallest mode at least ESIZE bits wide and make a class
1353 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1354 GET_MODE_BITSIZE (mode) < esize;
1355 mode = GET_MODE_WIDER_MODE (mode))
1358 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1359 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1360 = Is_Packed_Array_Type (gnat_entity);
1362 /* Get the modulus in this type. If it overflows, assume it is because
1363 it is equal to 2**Esize. Note that there is no overflow checking
1364 done on unsigned type, so we detect the overflow by looking for
1365 a modulus of zero, which is otherwise invalid. */
1366 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1368 if (!integer_zerop (gnu_modulus))
1370 TYPE_MODULAR_P (gnu_type) = 1;
1371 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1372 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1373 convert (gnu_type, integer_one_node));
1376 /* If we have to set TYPE_PRECISION different from its natural value,
1377 make a subtype to do do. Likewise if there is a modulus and
1378 it is not one greater than TYPE_MAX_VALUE. */
1379 if (TYPE_PRECISION (gnu_type) != esize
1380 || (TYPE_MODULAR_P (gnu_type)
1381 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1383 tree gnu_subtype = make_node (INTEGER_TYPE);
1385 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1386 TREE_TYPE (gnu_subtype) = gnu_type;
1387 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1388 TYPE_MAX_VALUE (gnu_subtype)
1389 = TYPE_MODULAR_P (gnu_type)
1390 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1391 TYPE_PRECISION (gnu_subtype) = esize;
1392 TYPE_UNSIGNED (gnu_subtype) = 1;
1393 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1394 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1395 = Is_Packed_Array_Type (gnat_entity);
1396 layout_type (gnu_subtype);
1398 gnu_type = gnu_subtype;
1403 case E_Signed_Integer_Subtype:
1404 case E_Enumeration_Subtype:
1405 case E_Modular_Integer_Subtype:
1406 case E_Ordinary_Fixed_Point_Subtype:
1407 case E_Decimal_Fixed_Point_Subtype:
1409 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1410 that we do not want to call build_range_type since we would
1411 like each subtype node to be distinct. This will be important
1412 when memory aliasing is implemented.
1414 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1415 parent type; this fact is used by the arithmetic conversion
1418 We elaborate the Ancestor_Subtype if it is not in the current
1419 unit and one of our bounds is non-static. We do this to ensure
1420 consistent naming in the case where several subtypes share the same
1421 bounds by always elaborating the first such subtype first, thus
1425 && Present (Ancestor_Subtype (gnat_entity))
1426 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1427 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1428 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1429 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1432 gnu_type = make_node (INTEGER_TYPE);
1433 if (Is_Packed_Array_Type (gnat_entity))
1435 esize = UI_To_Int (RM_Size (gnat_entity));
1436 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1439 TYPE_PRECISION (gnu_type) = esize;
1440 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1442 TYPE_MIN_VALUE (gnu_type)
1443 = convert (TREE_TYPE (gnu_type),
1444 elaborate_expression (Type_Low_Bound (gnat_entity),
1446 get_identifier ("L"), definition, 1,
1447 Needs_Debug_Info (gnat_entity)));
1449 TYPE_MAX_VALUE (gnu_type)
1450 = convert (TREE_TYPE (gnu_type),
1451 elaborate_expression (Type_High_Bound (gnat_entity),
1453 get_identifier ("U"), definition, 1,
1454 Needs_Debug_Info (gnat_entity)));
1456 /* One of the above calls might have caused us to be elaborated,
1457 so don't blow up if so. */
1458 if (present_gnu_tree (gnat_entity))
1460 maybe_present = true;
1464 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1465 = Has_Biased_Representation (gnat_entity);
1467 /* This should be an unsigned type if the lower bound is constant
1468 and non-negative or if the base type is unsigned; a signed type
1470 TYPE_UNSIGNED (gnu_type)
1471 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1472 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1473 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1474 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1475 || Is_Unsigned_Type (gnat_entity));
1477 layout_type (gnu_type);
1479 /* Inherit our alias set from what we're a subtype of. Subtypes
1480 are not different types and a pointer can designate any instance
1481 within a subtype hierarchy. */
1482 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1484 /* If the type we are dealing with is to represent a packed array,
1485 we need to have the bits left justified on big-endian targets
1486 and right justified on little-endian targets. We also need to
1487 ensure that when the value is read (e.g. for comparison of two
1488 such values), we only get the good bits, since the unused bits
1489 are uninitialized. Both goals are accomplished by wrapping the
1490 modular value in an enclosing struct. */
1491 if (Is_Packed_Array_Type (gnat_entity))
1493 tree gnu_field_type = gnu_type;
1496 TYPE_RM_SIZE_NUM (gnu_field_type)
1497 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1498 gnu_type = make_node (RECORD_TYPE);
1499 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1501 /* Propagate the alignment of the modular type to the record.
1502 This means that bitpacked arrays have "ceil" alignment for
1503 their size, which may seem counter-intuitive but makes it
1504 possible to easily overlay them on modular types. */
1505 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1506 TYPE_PACKED (gnu_type) = 1;
1508 /* Create a stripped-down declaration of the original type, mainly
1510 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1511 NULL, true, debug_info_p, gnat_entity);
1513 /* Don't notify the field as "addressable", since we won't be taking
1514 it's address and it would prevent create_field_decl from making a
1516 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1517 gnu_field_type, gnu_type, 1, 0, 0, 0);
1519 finish_record_type (gnu_type, gnu_field, 0, false);
1520 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1521 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1523 copy_alias_set (gnu_type, gnu_field_type);
1526 /* If the type we are dealing with has got a smaller alignment than the
1527 natural one, we need to wrap it up in a record type and under-align
1528 the latter. We reuse the padding machinery for this purpose. */
1529 else if (Known_Alignment (gnat_entity)
1530 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1531 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1532 && align < TYPE_ALIGN (gnu_type))
1534 tree gnu_field_type = gnu_type;
1537 gnu_type = make_node (RECORD_TYPE);
1538 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1540 TYPE_ALIGN (gnu_type) = align;
1541 TYPE_PACKED (gnu_type) = 1;
1543 /* Create a stripped-down declaration of the original type, mainly
1545 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1546 NULL, true, debug_info_p, gnat_entity);
1548 /* Don't notify the field as "addressable", since we won't be taking
1549 it's address and it would prevent create_field_decl from making a
1551 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1552 gnu_field_type, gnu_type, 1, 0, 0, 0);
1554 finish_record_type (gnu_type, gnu_field, 0, false);
1555 TYPE_IS_PADDING_P (gnu_type) = 1;
1556 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1558 copy_alias_set (gnu_type, gnu_field_type);
1561 /* Otherwise reset the alignment lest we computed it above. */
1567 case E_Floating_Point_Type:
1568 /* If this is a VAX floating-point type, use an integer of the proper
1569 size. All the operations will be handled with ASM statements. */
1570 if (Vax_Float (gnat_entity))
1572 gnu_type = make_signed_type (esize);
1573 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1574 SET_TYPE_DIGITS_VALUE (gnu_type,
1575 UI_To_gnu (Digits_Value (gnat_entity),
1580 /* The type of the Low and High bounds can be our type if this is
1581 a type from Standard, so set them at the end of the function. */
1582 gnu_type = make_node (REAL_TYPE);
1583 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1584 layout_type (gnu_type);
1587 case E_Floating_Point_Subtype:
1588 if (Vax_Float (gnat_entity))
1590 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1596 && Present (Ancestor_Subtype (gnat_entity))
1597 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1598 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1599 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1600 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1603 gnu_type = make_node (REAL_TYPE);
1604 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1605 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1607 TYPE_MIN_VALUE (gnu_type)
1608 = convert (TREE_TYPE (gnu_type),
1609 elaborate_expression (Type_Low_Bound (gnat_entity),
1610 gnat_entity, get_identifier ("L"),
1612 Needs_Debug_Info (gnat_entity)));
1614 TYPE_MAX_VALUE (gnu_type)
1615 = convert (TREE_TYPE (gnu_type),
1616 elaborate_expression (Type_High_Bound (gnat_entity),
1617 gnat_entity, get_identifier ("U"),
1619 Needs_Debug_Info (gnat_entity)));
1621 /* One of the above calls might have caused us to be elaborated,
1622 so don't blow up if so. */
1623 if (present_gnu_tree (gnat_entity))
1625 maybe_present = true;
1629 layout_type (gnu_type);
1631 /* Inherit our alias set from what we're a subtype of, as for
1632 integer subtypes. */
1633 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1637 /* Array and String Types and Subtypes
1639 Unconstrained array types are represented by E_Array_Type and
1640 constrained array types are represented by E_Array_Subtype. There
1641 are no actual objects of an unconstrained array type; all we have
1642 are pointers to that type.
1644 The following fields are defined on array types and subtypes:
1646 Component_Type Component type of the array.
1647 Number_Dimensions Number of dimensions (an int).
1648 First_Index Type of first index. */
1653 tree gnu_template_fields = NULL_TREE;
1654 tree gnu_template_type = make_node (RECORD_TYPE);
1655 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1656 tree gnu_fat_type = make_node (RECORD_TYPE);
1657 int ndim = Number_Dimensions (gnat_entity);
1659 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1661 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1663 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1664 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1665 tree gnu_comp_size = 0;
1666 tree gnu_max_size = size_one_node;
1667 tree gnu_max_size_unit;
1668 Entity_Id gnat_ind_subtype;
1669 Entity_Id gnat_ind_base_subtype;
1670 tree gnu_template_reference;
1673 TYPE_NAME (gnu_template_type)
1674 = create_concat_name (gnat_entity, "XUB");
1676 /* Make a node for the array. If we are not defining the array
1677 suppress expanding incomplete types. */
1678 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1681 defer_incomplete_level++, this_deferred = true;
1683 /* Build the fat pointer type. Use a "void *" object instead of
1684 a pointer to the array type since we don't have the array type
1685 yet (it will reference the fat pointer via the bounds). */
1686 tem = chainon (chainon (NULL_TREE,
1687 create_field_decl (get_identifier ("P_ARRAY"),
1689 gnu_fat_type, 0, 0, 0, 0)),
1690 create_field_decl (get_identifier ("P_BOUNDS"),
1692 gnu_fat_type, 0, 0, 0, 0));
1694 /* Make sure we can put this into a register. */
1695 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1697 /* Do not finalize this record type since the types of its fields
1698 are still incomplete at this point. */
1699 finish_record_type (gnu_fat_type, tem, 0, true);
1700 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1702 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1703 is the fat pointer. This will be used to access the individual
1704 fields once we build them. */
1705 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1706 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1707 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1708 gnu_template_reference
1709 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1710 TREE_READONLY (gnu_template_reference) = 1;
1712 /* Now create the GCC type for each index and add the fields for
1713 that index to the template. */
1714 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1715 gnat_ind_base_subtype
1716 = First_Index (Implementation_Base_Type (gnat_entity));
1717 index < ndim && index >= 0;
1719 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1720 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1722 char field_name[10];
1723 tree gnu_ind_subtype
1724 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1725 tree gnu_base_subtype
1726 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1728 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1730 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1731 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1733 /* Make the FIELD_DECLs for the minimum and maximum of this
1734 type and then make extractions of that field from the
1736 sprintf (field_name, "LB%d", index);
1737 gnu_min_field = create_field_decl (get_identifier (field_name),
1739 gnu_template_type, 0, 0, 0, 0);
1740 field_name[0] = 'U';
1741 gnu_max_field = create_field_decl (get_identifier (field_name),
1743 gnu_template_type, 0, 0, 0, 0);
1745 Sloc_to_locus (Sloc (gnat_entity),
1746 &DECL_SOURCE_LOCATION (gnu_min_field));
1747 Sloc_to_locus (Sloc (gnat_entity),
1748 &DECL_SOURCE_LOCATION (gnu_max_field));
1749 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1751 /* We can't use build_component_ref here since the template
1752 type isn't complete yet. */
1753 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1754 gnu_template_reference, gnu_min_field,
1756 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1757 gnu_template_reference, gnu_max_field,
1759 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1761 /* Make a range type with the new ranges, but using
1762 the Ada subtype. Then we convert to sizetype. */
1763 gnu_index_types[index]
1764 = create_index_type (convert (sizetype, gnu_min),
1765 convert (sizetype, gnu_max),
1766 build_range_type (gnu_ind_subtype,
1769 /* Update the maximum size of the array, in elements. */
1771 = size_binop (MULT_EXPR, gnu_max_size,
1772 size_binop (PLUS_EXPR, size_one_node,
1773 size_binop (MINUS_EXPR, gnu_base_max,
1776 TYPE_NAME (gnu_index_types[index])
1777 = create_concat_name (gnat_entity, field_name);
1780 for (index = 0; index < ndim; index++)
1782 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1784 /* Install all the fields into the template. */
1785 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1786 TYPE_READONLY (gnu_template_type) = 1;
1788 /* Now make the array of arrays and update the pointer to the array
1789 in the fat pointer. Note that it is the first field. */
1790 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1792 /* Try to get a smaller form of the component if needed. */
1793 if ((Is_Packed (gnat_entity)
1794 || Has_Component_Size_Clause (gnat_entity))
1795 && !Is_Bit_Packed_Array (gnat_entity)
1796 && !Has_Aliased_Components (gnat_entity)
1797 && !Strict_Alignment (Component_Type (gnat_entity))
1798 && TREE_CODE (tem) == RECORD_TYPE
1799 && TYPE_MODE (tem) == BLKmode
1800 && host_integerp (TYPE_SIZE (tem), 1))
1801 tem = make_packable_type (tem, false);
1803 if (Has_Atomic_Components (gnat_entity))
1804 check_ok_for_atomic (tem, gnat_entity, true);
1806 /* Get and validate any specified Component_Size, but if Packed,
1807 ignore it since the front end will have taken care of it. */
1809 = validate_size (Component_Size (gnat_entity), tem,
1811 (Is_Bit_Packed_Array (gnat_entity)
1812 ? TYPE_DECL : VAR_DECL),
1813 true, Has_Component_Size_Clause (gnat_entity));
1815 /* If the component type is a RECORD_TYPE that has a self-referential
1816 size, use the maxium size. */
1817 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1818 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1819 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1821 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1824 tem = make_type_from_size (tem, gnu_comp_size, false);
1826 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1827 "C_PAD", false, definition, true);
1828 /* If a padding record was made, declare it now since it will
1829 never be declared otherwise. This is necessary to ensure
1830 that its subtrees are properly marked. */
1831 if (tem != orig_tem)
1832 create_type_decl (TYPE_NAME (tem), tem, NULL, true, false,
1836 if (Has_Volatile_Components (gnat_entity))
1837 tem = build_qualified_type (tem,
1838 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1840 /* If Component_Size is not already specified, annotate it with the
1841 size of the component. */
1842 if (Unknown_Component_Size (gnat_entity))
1843 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1845 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1846 size_binop (MULT_EXPR, gnu_max_size,
1847 TYPE_SIZE_UNIT (tem)));
1848 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1849 size_binop (MULT_EXPR,
1850 convert (bitsizetype,
1854 for (index = ndim - 1; index >= 0; index--)
1856 tem = build_array_type (tem, gnu_index_types[index]);
1857 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1858 if (array_type_has_nonaliased_component (gnat_entity, tem))
1859 TYPE_NONALIASED_COMPONENT (tem) = 1;
1862 /* If an alignment is specified, use it if valid. But ignore it for
1863 types that represent the unpacked base type for packed arrays. If
1864 the alignment was requested with an explicit user alignment clause,
1866 if (No (Packed_Array_Type (gnat_entity))
1867 && Known_Alignment (gnat_entity))
1869 gcc_assert (Present (Alignment (gnat_entity)));
1871 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1873 if (Present (Alignment_Clause (gnat_entity)))
1874 TYPE_USER_ALIGN (tem) = 1;
1877 TYPE_CONVENTION_FORTRAN_P (tem)
1878 = (Convention (gnat_entity) == Convention_Fortran);
1879 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1881 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1882 corresponding fat pointer. */
1883 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1884 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1885 TYPE_MODE (gnu_type) = BLKmode;
1886 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1887 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1889 /* If the maximum size doesn't overflow, use it. */
1890 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1891 && !TREE_OVERFLOW (gnu_max_size))
1893 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1894 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1895 && !TREE_OVERFLOW (gnu_max_size_unit))
1896 TYPE_SIZE_UNIT (tem)
1897 = size_binop (MIN_EXPR, gnu_max_size_unit,
1898 TYPE_SIZE_UNIT (tem));
1900 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1901 tem, NULL, !Comes_From_Source (gnat_entity),
1902 debug_info_p, gnat_entity);
1904 /* Give the fat pointer type a name. */
1905 create_type_decl (create_concat_name (gnat_entity, "XUP"),
1906 gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
1907 debug_info_p, gnat_entity);
1909 /* Create the type to be used as what a thin pointer designates: an
1910 record type for the object and its template with the field offsets
1911 shifted to have the template at a negative offset. */
1912 tem = build_unc_object_type (gnu_template_type, tem,
1913 create_concat_name (gnat_entity, "XUT"));
1914 shift_unc_components_for_thin_pointers (tem);
1916 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1917 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1919 /* Give the thin pointer type a name. */
1920 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1921 build_pointer_type (tem), NULL,
1922 !Comes_From_Source (gnat_entity), debug_info_p,
1927 case E_String_Subtype:
1928 case E_Array_Subtype:
1930 /* This is the actual data type for array variables. Multidimensional
1931 arrays are implemented in the gnu tree as arrays of arrays. Note
1932 that for the moment arrays which have sparse enumeration subtypes as
1933 index components create sparse arrays, which is obviously space
1934 inefficient but so much easier to code for now.
1936 Also note that the subtype never refers to the unconstrained
1937 array type, which is somewhat at variance with Ada semantics.
1939 First check to see if this is simply a renaming of the array
1940 type. If so, the result is the array type. */
1942 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1943 if (!Is_Constrained (gnat_entity))
1948 int array_dim = Number_Dimensions (gnat_entity);
1950 = ((Convention (gnat_entity) == Convention_Fortran)
1951 ? array_dim - 1 : 0);
1953 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1954 Entity_Id gnat_ind_subtype;
1955 Entity_Id gnat_ind_base_subtype;
1956 tree gnu_base_type = gnu_type;
1957 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1958 tree gnu_comp_size = NULL_TREE;
1959 tree gnu_max_size = size_one_node;
1960 tree gnu_max_size_unit;
1961 bool need_index_type_struct = false;
1962 bool max_overflow = false;
1964 /* First create the gnu types for each index. Create types for
1965 debugging information to point to the index types if the
1966 are not integer types, have variable bounds, or are
1967 wider than sizetype. */
1969 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1970 gnat_ind_base_subtype
1971 = First_Index (Implementation_Base_Type (gnat_entity));
1972 index < array_dim && index >= 0;
1974 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1975 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1977 tree gnu_index_subtype
1978 = get_unpadded_type (Etype (gnat_ind_subtype));
1980 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1982 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1983 tree gnu_base_subtype
1984 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1986 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1988 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1989 tree gnu_base_type = get_base_type (gnu_base_subtype);
1990 tree gnu_base_base_min
1991 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1992 tree gnu_base_base_max
1993 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1997 /* If the minimum and maximum values both overflow in
1998 SIZETYPE, but the difference in the original type
1999 does not overflow in SIZETYPE, ignore the overflow
2001 if ((TYPE_PRECISION (gnu_index_subtype)
2002 > TYPE_PRECISION (sizetype)
2003 || TYPE_UNSIGNED (gnu_index_subtype)
2004 != TYPE_UNSIGNED (sizetype))
2005 && TREE_CODE (gnu_min) == INTEGER_CST
2006 && TREE_CODE (gnu_max) == INTEGER_CST
2007 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2009 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2010 TYPE_MAX_VALUE (gnu_index_subtype),
2011 TYPE_MIN_VALUE (gnu_index_subtype)))))
2013 TREE_OVERFLOW (gnu_min) = 0;
2014 TREE_OVERFLOW (gnu_max) = 0;
2017 /* Similarly, if the range is null, use bounds of 1..0 for
2018 the sizetype bounds. */
2019 else if ((TYPE_PRECISION (gnu_index_subtype)
2020 > TYPE_PRECISION (sizetype)
2021 || TYPE_UNSIGNED (gnu_index_subtype)
2022 != TYPE_UNSIGNED (sizetype))
2023 && TREE_CODE (gnu_min) == INTEGER_CST
2024 && TREE_CODE (gnu_max) == INTEGER_CST
2025 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2026 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2027 TYPE_MIN_VALUE (gnu_index_subtype)))
2028 gnu_min = size_one_node, gnu_max = size_zero_node;
2030 /* Now compute the size of this bound. We need to provide
2031 GCC with an upper bound to use but have to deal with the
2032 "superflat" case. There are three ways to do this. If we
2033 can prove that the array can never be superflat, we can
2034 just use the high bound of the index subtype. If we can
2035 prove that the low bound minus one can't overflow, we
2036 can do this as MAX (hb, lb - 1). Otherwise, we have to use
2037 the expression hb >= lb ? hb : lb - 1. */
2038 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2040 /* See if the base array type is already flat. If it is, we
2041 are probably compiling an ACVC test, but it will cause the
2042 code below to malfunction if we don't handle it specially. */
2043 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2044 && TREE_CODE (gnu_base_max) == INTEGER_CST
2045 && !TREE_OVERFLOW (gnu_base_min)
2046 && !TREE_OVERFLOW (gnu_base_max)
2047 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2048 gnu_high = size_zero_node, gnu_min = size_one_node;
2050 /* If gnu_high is now an integer which overflowed, the array
2051 cannot be superflat. */
2052 else if (TREE_CODE (gnu_high) == INTEGER_CST
2053 && TREE_OVERFLOW (gnu_high))
2055 else if (TYPE_UNSIGNED (gnu_base_subtype)
2056 || TREE_CODE (gnu_high) == INTEGER_CST)
2057 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2061 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2065 gnu_index_type[index]
2066 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2069 /* Also compute the maximum size of the array. Here we
2070 see if any constraint on the index type of the base type
2071 can be used in the case of self-referential bound on
2072 the index type of the subtype. We look for a non-"infinite"
2073 and non-self-referential bound from any type involved and
2074 handle each bound separately. */
2076 if ((TREE_CODE (gnu_min) == INTEGER_CST
2077 && !TREE_OVERFLOW (gnu_min)
2078 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2079 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2080 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2081 && !TREE_OVERFLOW (gnu_base_min)))
2082 gnu_base_min = gnu_min;
2084 if ((TREE_CODE (gnu_max) == INTEGER_CST
2085 && !TREE_OVERFLOW (gnu_max)
2086 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2087 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2088 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2089 && !TREE_OVERFLOW (gnu_base_max)))
2090 gnu_base_max = gnu_max;
2092 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2093 && TREE_OVERFLOW (gnu_base_min))
2094 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2095 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2096 && TREE_OVERFLOW (gnu_base_max))
2097 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2098 max_overflow = true;
2100 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2101 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2104 = size_binop (MAX_EXPR,
2105 size_binop (PLUS_EXPR, size_one_node,
2106 size_binop (MINUS_EXPR, gnu_base_max,
2110 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2111 && TREE_OVERFLOW (gnu_this_max))
2112 max_overflow = true;
2115 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2117 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2118 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2120 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2121 || (TREE_TYPE (gnu_index_subtype)
2122 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2124 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2125 || (TYPE_PRECISION (gnu_index_subtype)
2126 > TYPE_PRECISION (sizetype)))
2127 need_index_type_struct = true;
2130 /* Then flatten: create the array of arrays. For an array type
2131 used to implement a packed array, get the component type from
2132 the original array type since the representation clauses that
2133 can affect it are on the latter. */
2134 if (Is_Packed_Array_Type (gnat_entity)
2135 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2137 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2138 for (index = array_dim - 1; index >= 0; index--)
2139 gnu_type = TREE_TYPE (gnu_type);
2141 /* One of the above calls might have caused us to be elaborated,
2142 so don't blow up if so. */
2143 if (present_gnu_tree (gnat_entity))
2145 maybe_present = true;
2151 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2153 /* One of the above calls might have caused us to be elaborated,
2154 so don't blow up if so. */
2155 if (present_gnu_tree (gnat_entity))
2157 maybe_present = true;
2161 /* Try to get a smaller form of the component if needed. */
2162 if ((Is_Packed (gnat_entity)
2163 || Has_Component_Size_Clause (gnat_entity))
2164 && !Is_Bit_Packed_Array (gnat_entity)
2165 && !Has_Aliased_Components (gnat_entity)
2166 && !Strict_Alignment (Component_Type (gnat_entity))
2167 && TREE_CODE (gnu_type) == RECORD_TYPE
2168 && TYPE_MODE (gnu_type) == BLKmode
2169 && host_integerp (TYPE_SIZE (gnu_type), 1))
2170 gnu_type = make_packable_type (gnu_type, false);
2172 /* Get and validate any specified Component_Size, but if Packed,
2173 ignore it since the front end will have taken care of it. */
2175 = validate_size (Component_Size (gnat_entity), gnu_type,
2177 (Is_Bit_Packed_Array (gnat_entity)
2178 ? TYPE_DECL : VAR_DECL), true,
2179 Has_Component_Size_Clause (gnat_entity));
2181 /* If the component type is a RECORD_TYPE that has a
2182 self-referential size, use the maxium size. */
2184 && TREE_CODE (gnu_type) == RECORD_TYPE
2185 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2186 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2188 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2192 = make_type_from_size (gnu_type, gnu_comp_size, false);
2193 orig_gnu_type = gnu_type;
2194 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2195 gnat_entity, "C_PAD", false,
2197 /* If a padding record was made, declare it now since it
2198 will never be declared otherwise. This is necessary
2199 to ensure that its subtrees are properly marked. */
2200 if (gnu_type != orig_gnu_type)
2201 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2202 true, false, gnat_entity);
2205 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2206 gnu_type = build_qualified_type (gnu_type,
2207 (TYPE_QUALS (gnu_type)
2208 | TYPE_QUAL_VOLATILE));
2211 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2212 TYPE_SIZE_UNIT (gnu_type));
2213 gnu_max_size = size_binop (MULT_EXPR,
2214 convert (bitsizetype, gnu_max_size),
2215 TYPE_SIZE (gnu_type));
2217 for (index = array_dim - 1; index >= 0; index --)
2219 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2220 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2221 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2222 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2225 /* If we are at file level and this is a multi-dimensional array, we
2226 need to make a variable corresponding to the stride of the
2227 inner dimensions. */
2228 if (global_bindings_p () && array_dim > 1)
2230 tree gnu_str_name = get_identifier ("ST");
2233 for (gnu_arr_type = TREE_TYPE (gnu_type);
2234 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2235 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2236 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2238 tree eltype = TREE_TYPE (gnu_arr_type);
2240 TYPE_SIZE (gnu_arr_type)
2241 = elaborate_expression_1 (gnat_entity, gnat_entity,
2242 TYPE_SIZE (gnu_arr_type),
2243 gnu_str_name, definition, 0);
2245 /* ??? For now, store the size as a multiple of the
2246 alignment of the element type in bytes so that we
2247 can see the alignment from the tree. */
2248 TYPE_SIZE_UNIT (gnu_arr_type)
2250 (MULT_EXPR, sizetype,
2251 elaborate_expression_1
2252 (gnat_entity, gnat_entity,
2253 build_binary_op (EXACT_DIV_EXPR, sizetype,
2254 TYPE_SIZE_UNIT (gnu_arr_type),
2255 size_int (TYPE_ALIGN (eltype)
2257 concat_id_with_name (gnu_str_name, "A_U"),
2259 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2261 /* ??? create_type_decl is not invoked on the inner types so
2262 the MULT_EXPR node built above will never be marked. */
2263 TREE_VISITED (TYPE_SIZE_UNIT (gnu_arr_type)) = 1;
2267 /* If we need to write out a record type giving the names of
2268 the bounds, do it now. */
2269 if (need_index_type_struct && debug_info_p)
2271 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2272 tree gnu_field_list = NULL_TREE;
2275 TYPE_NAME (gnu_bound_rec_type)
2276 = create_concat_name (gnat_entity, "XA");
2278 for (index = array_dim - 1; index >= 0; index--)
2281 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2283 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2284 gnu_type_name = DECL_NAME (gnu_type_name);
2286 gnu_field = create_field_decl (gnu_type_name,
2289 0, NULL_TREE, NULL_TREE, 0);
2290 TREE_CHAIN (gnu_field) = gnu_field_list;
2291 gnu_field_list = gnu_field;
2294 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2298 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2299 = (Convention (gnat_entity) == Convention_Fortran);
2300 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2301 = Is_Packed_Array_Type (gnat_entity);
2303 /* If our size depends on a placeholder and the maximum size doesn't
2304 overflow, use it. */
2305 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2306 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2307 && TREE_OVERFLOW (gnu_max_size))
2308 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2309 && TREE_OVERFLOW (gnu_max_size_unit))
2312 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2313 TYPE_SIZE (gnu_type));
2314 TYPE_SIZE_UNIT (gnu_type)
2315 = size_binop (MIN_EXPR, gnu_max_size_unit,
2316 TYPE_SIZE_UNIT (gnu_type));
2319 /* Set our alias set to that of our base type. This gives all
2320 array subtypes the same alias set. */
2321 copy_alias_set (gnu_type, gnu_base_type);
2324 /* If this is a packed type, make this type the same as the packed
2325 array type, but do some adjusting in the type first. */
2327 if (Present (Packed_Array_Type (gnat_entity)))
2329 Entity_Id gnat_index;
2330 tree gnu_inner_type;
2332 /* First finish the type we had been making so that we output
2333 debugging information for it */
2335 = build_qualified_type (gnu_type,
2336 (TYPE_QUALS (gnu_type)
2337 | (TYPE_QUAL_VOLATILE
2338 * Treat_As_Volatile (gnat_entity))));
2339 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2340 !Comes_From_Source (gnat_entity),
2341 debug_info_p, gnat_entity);
2342 if (!Comes_From_Source (gnat_entity))
2343 DECL_ARTIFICIAL (gnu_decl) = 1;
2345 /* Save it as our equivalent in case the call below elaborates
2347 save_gnu_tree (gnat_entity, gnu_decl, false);
2349 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2351 this_made_decl = true;
2352 gnu_type = TREE_TYPE (gnu_decl);
2353 save_gnu_tree (gnat_entity, NULL_TREE, false);
2355 gnu_inner_type = gnu_type;
2356 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2357 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2358 || TYPE_IS_PADDING_P (gnu_inner_type)))
2359 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2361 /* We need to point the type we just made to our index type so
2362 the actual bounds can be put into a template. */
2364 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2365 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2366 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2367 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2369 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2371 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2372 If it is, we need to make another type. */
2373 if (TYPE_MODULAR_P (gnu_inner_type))
2377 gnu_subtype = make_node (INTEGER_TYPE);
2379 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2380 TYPE_MIN_VALUE (gnu_subtype)
2381 = TYPE_MIN_VALUE (gnu_inner_type);
2382 TYPE_MAX_VALUE (gnu_subtype)
2383 = TYPE_MAX_VALUE (gnu_inner_type);
2384 TYPE_PRECISION (gnu_subtype)
2385 = TYPE_PRECISION (gnu_inner_type);
2386 TYPE_UNSIGNED (gnu_subtype)
2387 = TYPE_UNSIGNED (gnu_inner_type);
2388 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2389 layout_type (gnu_subtype);
2391 gnu_inner_type = gnu_subtype;
2394 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2397 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2399 for (gnat_index = First_Index (gnat_entity);
2400 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2401 SET_TYPE_ACTUAL_BOUNDS
2403 tree_cons (NULL_TREE,
2404 get_unpadded_type (Etype (gnat_index)),
2405 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2407 if (Convention (gnat_entity) != Convention_Fortran)
2408 SET_TYPE_ACTUAL_BOUNDS
2410 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2412 if (TREE_CODE (gnu_type) == RECORD_TYPE
2413 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2414 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2418 /* Abort if packed array with no packed array type field set. */
2420 gcc_assert (!Is_Packed (gnat_entity));
2424 case E_String_Literal_Subtype:
2425 /* Create the type for a string literal. */
2427 Entity_Id gnat_full_type
2428 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2429 && Present (Full_View (Etype (gnat_entity)))
2430 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2431 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2432 tree gnu_string_array_type
2433 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2434 tree gnu_string_index_type
2435 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2436 (TYPE_DOMAIN (gnu_string_array_type))));
2437 tree gnu_lower_bound
2438 = convert (gnu_string_index_type,
2439 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2440 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2441 tree gnu_length = ssize_int (length - 1);
2442 tree gnu_upper_bound
2443 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2445 convert (gnu_string_index_type, gnu_length));
2447 = build_range_type (gnu_string_index_type,
2448 gnu_lower_bound, gnu_upper_bound);
2450 = create_index_type (convert (sizetype,
2451 TYPE_MIN_VALUE (gnu_range_type)),
2453 TYPE_MAX_VALUE (gnu_range_type)),
2454 gnu_range_type, gnat_entity);
2457 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2459 copy_alias_set (gnu_type, gnu_string_type);
2463 /* Record Types and Subtypes
2465 The following fields are defined on record types:
2467 Has_Discriminants True if the record has discriminants
2468 First_Discriminant Points to head of list of discriminants
2469 First_Entity Points to head of list of fields
2470 Is_Tagged_Type True if the record is tagged
2472 Implementation of Ada records and discriminated records:
2474 A record type definition is transformed into the equivalent of a C
2475 struct definition. The fields that are the discriminants which are
2476 found in the Full_Type_Declaration node and the elements of the
2477 Component_List found in the Record_Type_Definition node. The
2478 Component_List can be a recursive structure since each Variant of
2479 the Variant_Part of the Component_List has a Component_List.
2481 Processing of a record type definition comprises starting the list of
2482 field declarations here from the discriminants and the calling the
2483 function components_to_record to add the rest of the fields from the
2484 component list and return the gnu type node. The function
2485 components_to_record will call itself recursively as it traverses
2489 if (Has_Complex_Representation (gnat_entity))
2492 = build_complex_type
2494 (Etype (Defining_Entity
2495 (First (Component_Items
2498 (Declaration_Node (gnat_entity)))))))));
2504 Node_Id full_definition = Declaration_Node (gnat_entity);
2505 Node_Id record_definition = Type_Definition (full_definition);
2506 Entity_Id gnat_field;
2508 tree gnu_field_list = NULL_TREE;
2509 tree gnu_get_parent;
2510 /* Set PACKED in keeping with gnat_to_gnu_field. */
2512 = Is_Packed (gnat_entity)
2514 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2516 : (Known_Alignment (gnat_entity)
2517 || (Strict_Alignment (gnat_entity)
2518 && Known_Static_Esize (gnat_entity)))
2521 bool has_rep = Has_Specified_Layout (gnat_entity);
2522 bool all_rep = has_rep;
2524 = (Is_Tagged_Type (gnat_entity)
2525 && Nkind (record_definition) == N_Derived_Type_Definition);
2527 /* See if all fields have a rep clause. Stop when we find one
2529 for (gnat_field = First_Entity (gnat_entity);
2530 Present (gnat_field) && all_rep;
2531 gnat_field = Next_Entity (gnat_field))
2532 if ((Ekind (gnat_field) == E_Component
2533 || Ekind (gnat_field) == E_Discriminant)
2534 && No (Component_Clause (gnat_field)))
2537 /* If this is a record extension, go a level further to find the
2538 record definition. Also, verify we have a Parent_Subtype. */
2541 if (!type_annotate_only
2542 || Present (Record_Extension_Part (record_definition)))
2543 record_definition = Record_Extension_Part (record_definition);
2545 gcc_assert (type_annotate_only
2546 || Present (Parent_Subtype (gnat_entity)));
2549 /* Make a node for the record. If we are not defining the record,
2550 suppress expanding incomplete types. */
2551 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2552 TYPE_NAME (gnu_type) = gnu_entity_id;
2553 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2556 defer_incomplete_level++, this_deferred = true;
2558 /* If both a size and rep clause was specified, put the size in
2559 the record type now so that it can get the proper mode. */
2560 if (has_rep && Known_Esize (gnat_entity))
2561 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2563 /* Always set the alignment here so that it can be used to
2564 set the mode, if it is making the alignment stricter. If
2565 it is invalid, it will be checked again below. If this is to
2566 be Atomic, choose a default alignment of a word unless we know
2567 the size and it's smaller. */
2568 if (Known_Alignment (gnat_entity))
2569 TYPE_ALIGN (gnu_type)
2570 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2571 else if (Is_Atomic (gnat_entity))
2572 TYPE_ALIGN (gnu_type)
2573 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2574 /* If a type needs strict alignment, the minimum size will be the
2575 type size instead of the RM size (see validate_size). Cap the
2576 alignment, lest it causes this type size to become too large. */
2577 else if (Strict_Alignment (gnat_entity)
2578 && Known_Static_Esize (gnat_entity))
2580 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2581 unsigned int raw_align = raw_size & -raw_size;
2582 if (raw_align < BIGGEST_ALIGNMENT)
2583 TYPE_ALIGN (gnu_type) = raw_align;
2586 TYPE_ALIGN (gnu_type) = 0;
2588 /* If we have a Parent_Subtype, make a field for the parent. If
2589 this record has rep clauses, force the position to zero. */
2590 if (Present (Parent_Subtype (gnat_entity)))
2592 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2595 /* A major complexity here is that the parent subtype will
2596 reference our discriminants in its Discriminant_Constraint
2597 list. But those must reference the parent component of this
2598 record which is of the parent subtype we have not built yet!
2599 To break the circle we first build a dummy COMPONENT_REF which
2600 represents the "get to the parent" operation and initialize
2601 each of those discriminants to a COMPONENT_REF of the above
2602 dummy parent referencing the corresponding discriminant of the
2603 base type of the parent subtype. */
2604 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2605 build0 (PLACEHOLDER_EXPR, gnu_type),
2606 build_decl (FIELD_DECL, NULL_TREE,
2610 if (Has_Discriminants (gnat_entity))
2611 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2612 Present (gnat_field);
2613 gnat_field = Next_Stored_Discriminant (gnat_field))
2614 if (Present (Corresponding_Discriminant (gnat_field)))
2617 build3 (COMPONENT_REF,
2618 get_unpadded_type (Etype (gnat_field)),
2620 gnat_to_gnu_field_decl (Corresponding_Discriminant
2625 /* Then we build the parent subtype. */
2626 gnu_parent = gnat_to_gnu_type (gnat_parent);
2628 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2629 initially built. The discriminants must reference the fields
2630 of the parent subtype and not those of its base type for the
2631 placeholder machinery to properly work. */
2632 if (Has_Discriminants (gnat_entity))
2633 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2634 Present (gnat_field);
2635 gnat_field = Next_Stored_Discriminant (gnat_field))
2636 if (Present (Corresponding_Discriminant (gnat_field)))
2638 Entity_Id field = Empty;
2639 for (field = First_Stored_Discriminant (gnat_parent);
2641 field = Next_Stored_Discriminant (field))
2642 if (same_discriminant_p (gnat_field, field))
2644 gcc_assert (Present (field));
2645 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2646 = gnat_to_gnu_field_decl (field);
2649 /* The "get to the parent" COMPONENT_REF must be given its
2651 TREE_TYPE (gnu_get_parent) = gnu_parent;
2653 /* ...and reference the _parent field of this record. */
2655 = create_field_decl (get_identifier
2656 (Get_Name_String (Name_uParent)),
2657 gnu_parent, gnu_type, 0,
2658 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2659 has_rep ? bitsize_zero_node : 0, 1);
2660 DECL_INTERNAL_P (gnu_field_list) = 1;
2661 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2664 /* Make the fields for the discriminants and put them into the record
2665 unless it's an Unchecked_Union. */
2666 if (Has_Discriminants (gnat_entity))
2667 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2668 Present (gnat_field);
2669 gnat_field = Next_Stored_Discriminant (gnat_field))
2671 /* If this is a record extension and this discriminant
2672 is the renaming of another discriminant, we've already
2673 handled the discriminant above. */
2674 if (Present (Parent_Subtype (gnat_entity))
2675 && Present (Corresponding_Discriminant (gnat_field)))
2679 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2681 /* Make an expression using a PLACEHOLDER_EXPR from the
2682 FIELD_DECL node just created and link that with the
2683 corresponding GNAT defining identifier. Then add to the
2685 save_gnu_tree (gnat_field,
2686 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2687 build0 (PLACEHOLDER_EXPR,
2688 DECL_CONTEXT (gnu_field)),
2689 gnu_field, NULL_TREE),
2692 if (!Is_Unchecked_Union (gnat_entity))
2694 TREE_CHAIN (gnu_field) = gnu_field_list;
2695 gnu_field_list = gnu_field;
2699 /* Put the discriminants into the record (backwards), so we can
2700 know the appropriate discriminant to use for the names of the
2702 TYPE_FIELDS (gnu_type) = gnu_field_list;
2704 /* Add the listed fields into the record and finish it up. */
2705 components_to_record (gnu_type, Component_List (record_definition),
2706 gnu_field_list, packed, definition, NULL,
2707 false, all_rep, false,
2708 Is_Unchecked_Union (gnat_entity));
2710 /* We used to remove the associations of the discriminants and
2711 _Parent for validity checking, but we may need them if there's
2712 Freeze_Node for a subtype used in this record. */
2713 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2714 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2716 /* If it is a tagged record force the type to BLKmode to insure
2717 that these objects will always be placed in memory. Do the
2718 same thing for limited record types. */
2719 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2720 TYPE_MODE (gnu_type) = BLKmode;
2722 /* If this is a derived type, we must make the alias set of this type
2723 the same as that of the type we are derived from. We assume here
2724 that the other type is already frozen. */
2725 if (Etype (gnat_entity) != gnat_entity
2726 && !(Is_Private_Type (Etype (gnat_entity))
2727 && Full_View (Etype (gnat_entity)) == gnat_entity))
2728 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2730 /* Fill in locations of fields. */
2731 annotate_rep (gnat_entity, gnu_type);
2733 /* If there are any entities in the chain corresponding to
2734 components that we did not elaborate, ensure we elaborate their
2735 types if they are Itypes. */
2736 for (gnat_temp = First_Entity (gnat_entity);
2737 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2738 if ((Ekind (gnat_temp) == E_Component
2739 || Ekind (gnat_temp) == E_Discriminant)
2740 && Is_Itype (Etype (gnat_temp))
2741 && !present_gnu_tree (gnat_temp))
2742 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2746 case E_Class_Wide_Subtype:
2747 /* If an equivalent type is present, that is what we should use.
2748 Otherwise, fall through to handle this like a record subtype
2749 since it may have constraints. */
2750 if (gnat_equiv_type != gnat_entity)
2752 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2753 maybe_present = true;
2757 /* ... fall through ... */
2759 case E_Record_Subtype:
2761 /* If Cloned_Subtype is Present it means this record subtype has
2762 identical layout to that type or subtype and we should use
2763 that GCC type for this one. The front end guarantees that
2764 the component list is shared. */
2765 if (Present (Cloned_Subtype (gnat_entity)))
2767 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2769 maybe_present = true;
2772 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2773 changing the type, make a new type with each field having the
2774 type of the field in the new subtype but having the position
2775 computed by transforming every discriminant reference according
2776 to the constraints. We don't see any difference between
2777 private and nonprivate type here since derivations from types should
2778 have been deferred until the completion of the private type. */
2781 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2786 defer_incomplete_level++, this_deferred = true;
2788 /* Get the base type initially for its alignment and sizes. But
2789 if it is a padded type, we do all the other work with the
2791 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2793 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2794 && TYPE_IS_PADDING_P (gnu_base_type))
2795 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2797 gnu_type = gnu_orig_type = gnu_base_type;
2799 if (present_gnu_tree (gnat_entity))
2801 maybe_present = true;
2805 /* When the type has discriminants, and these discriminants
2806 affect the shape of what it built, factor them in.
2808 If we are making a subtype of an Unchecked_Union (must be an
2809 Itype), just return the type.
2811 We can't just use Is_Constrained because private subtypes without
2812 discriminants of full types with discriminants with default
2813 expressions are Is_Constrained but aren't constrained! */
2815 if (IN (Ekind (gnat_base_type), Record_Kind)
2816 && !Is_For_Access_Subtype (gnat_entity)
2817 && !Is_Unchecked_Union (gnat_base_type)
2818 && Is_Constrained (gnat_entity)
2819 && Stored_Constraint (gnat_entity) != No_Elist
2820 && Present (Discriminant_Constraint (gnat_entity)))
2822 Entity_Id gnat_field;
2823 tree gnu_field_list = 0;
2825 = compute_field_positions (gnu_orig_type, NULL_TREE,
2826 size_zero_node, bitsize_zero_node,
2829 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2833 gnu_type = make_node (RECORD_TYPE);
2834 TYPE_NAME (gnu_type) = gnu_entity_id;
2835 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2836 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2838 for (gnat_field = First_Entity (gnat_entity);
2839 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2840 if ((Ekind (gnat_field) == E_Component
2841 || Ekind (gnat_field) == E_Discriminant)
2842 && (Underlying_Type (Scope (Original_Record_Component
2845 && (No (Corresponding_Discriminant (gnat_field))
2846 || !Is_Tagged_Type (gnat_base_type)))
2849 = gnat_to_gnu_field_decl (Original_Record_Component
2852 = TREE_VALUE (purpose_member (gnu_old_field,
2854 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2855 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2857 = gnat_to_gnu_type (Etype (gnat_field));
2858 tree gnu_size = TYPE_SIZE (gnu_field_type);
2859 tree gnu_new_pos = 0;
2860 unsigned int offset_align
2861 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2865 /* If there was a component clause, the field types must be
2866 the same for the type and subtype, so copy the data from
2867 the old field to avoid recomputation here. Also if the
2868 field is justified modular and the optimization in
2869 gnat_to_gnu_field was applied. */
2870 if (Present (Component_Clause
2871 (Original_Record_Component (gnat_field)))
2872 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2873 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2874 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2875 == TREE_TYPE (gnu_old_field)))
2877 gnu_size = DECL_SIZE (gnu_old_field);
2878 gnu_field_type = TREE_TYPE (gnu_old_field);
2881 /* If the old field was packed and of constant size, we
2882 have to get the old size here, as it might differ from
2883 what the Etype conveys and the latter might overlap
2884 onto the following field. Try to arrange the type for
2885 possible better packing along the way. */
2886 else if (DECL_PACKED (gnu_old_field)
2887 && TREE_CODE (DECL_SIZE (gnu_old_field))
2890 gnu_size = DECL_SIZE (gnu_old_field);
2891 if (TYPE_MODE (gnu_field_type) == BLKmode
2892 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2893 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2895 = make_packable_type (gnu_field_type, true);
2898 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2899 for (gnu_temp = gnu_subst_list;
2900 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2901 gnu_pos = substitute_in_expr (gnu_pos,
2902 TREE_PURPOSE (gnu_temp),
2903 TREE_VALUE (gnu_temp));
2905 /* If the size is now a constant, we can set it as the
2906 size of the field when we make it. Otherwise, we need
2907 to deal with it specially. */
2908 if (TREE_CONSTANT (gnu_pos))
2909 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2913 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2914 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
2915 !DECL_NONADDRESSABLE_P (gnu_old_field));
2917 if (!TREE_CONSTANT (gnu_pos))
2919 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2920 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2921 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2922 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2923 DECL_SIZE (gnu_field) = gnu_size;
2924 DECL_SIZE_UNIT (gnu_field)
2925 = convert (sizetype,
2926 size_binop (CEIL_DIV_EXPR, gnu_size,
2927 bitsize_unit_node));
2928 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2931 DECL_INTERNAL_P (gnu_field)
2932 = DECL_INTERNAL_P (gnu_old_field);
2933 SET_DECL_ORIGINAL_FIELD
2934 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2935 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2937 DECL_DISCRIMINANT_NUMBER (gnu_field)
2938 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2939 TREE_THIS_VOLATILE (gnu_field)
2940 = TREE_THIS_VOLATILE (gnu_old_field);
2941 TREE_CHAIN (gnu_field) = gnu_field_list;
2942 gnu_field_list = gnu_field;
2943 save_gnu_tree (gnat_field, gnu_field, false);
2946 /* Now go through the entities again looking for Itypes that
2947 we have not elaborated but should (e.g., Etypes of fields
2948 that have Original_Components). */
2949 for (gnat_field = First_Entity (gnat_entity);
2950 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2951 if ((Ekind (gnat_field) == E_Discriminant
2952 || Ekind (gnat_field) == E_Component)
2953 && !present_gnu_tree (Etype (gnat_field)))
2954 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2956 /* Do not finalize it since we're going to modify it below. */
2957 finish_record_type (gnu_type, nreverse (gnu_field_list),
2960 /* Now set the size, alignment and alias set of the new type to
2961 match that of the old one, doing any substitutions, as
2963 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2964 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2965 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2966 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2967 copy_alias_set (gnu_type, gnu_base_type);
2969 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2970 for (gnu_temp = gnu_subst_list;
2971 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2972 TYPE_SIZE (gnu_type)
2973 = substitute_in_expr (TYPE_SIZE (gnu_type),
2974 TREE_PURPOSE (gnu_temp),
2975 TREE_VALUE (gnu_temp));
2977 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2978 for (gnu_temp = gnu_subst_list;
2979 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2980 TYPE_SIZE_UNIT (gnu_type)
2981 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2982 TREE_PURPOSE (gnu_temp),
2983 TREE_VALUE (gnu_temp));
2985 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2986 for (gnu_temp = gnu_subst_list;
2987 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2989 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2990 TREE_PURPOSE (gnu_temp),
2991 TREE_VALUE (gnu_temp)));
2993 /* Reapply variable_size since we have changed the sizes. */
2994 TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
2995 TYPE_SIZE_UNIT (gnu_type)
2996 = variable_size (TYPE_SIZE_UNIT (gnu_type));
2998 /* Recompute the mode of this record type now that we know its
3000 compute_record_mode (gnu_type);
3002 /* Fill in locations of fields. */
3003 annotate_rep (gnat_entity, gnu_type);
3005 /* We've built a new type, make an XVS type to show what this
3006 is a subtype of. Some debuggers require the XVS type to be
3007 output first, so do it in that order. */
3010 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3011 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3013 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3014 gnu_orig_name = DECL_NAME (gnu_orig_name);
3016 TYPE_NAME (gnu_subtype_marker)
3017 = create_concat_name (gnat_entity, "XVS");
3018 finish_record_type (gnu_subtype_marker,
3019 create_field_decl (gnu_orig_name,
3027 /* Now we can finalize it. */
3028 rest_of_record_type_compilation (gnu_type);
3031 /* Otherwise, go down all the components in the new type and
3032 make them equivalent to those in the base type. */
3034 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3035 gnat_temp = Next_Entity (gnat_temp))
3036 if ((Ekind (gnat_temp) == E_Discriminant
3037 && !Is_Unchecked_Union (gnat_base_type))
3038 || Ekind (gnat_temp) == E_Component)
3039 save_gnu_tree (gnat_temp,
3040 gnat_to_gnu_field_decl
3041 (Original_Record_Component (gnat_temp)), false);
3045 case E_Access_Subprogram_Type:
3046 case E_Anonymous_Access_Subprogram_Type:
3047 /* If we are not defining this entity, and we have incomplete
3048 entities being processed above us, make a dummy type and
3049 fill it in later. */
3050 if (!definition && defer_incomplete_level != 0)
3052 struct incomplete *p
3053 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3056 = build_pointer_type
3057 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3058 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3059 !Comes_From_Source (gnat_entity),
3060 debug_info_p, gnat_entity);
3061 this_made_decl = true;
3062 gnu_type = TREE_TYPE (gnu_decl);
3063 save_gnu_tree (gnat_entity, gnu_decl, false);
3066 p->old_type = TREE_TYPE (gnu_type);
3067 p->full_type = Directly_Designated_Type (gnat_entity);
3068 p->next = defer_incomplete_list;
3069 defer_incomplete_list = p;
3073 /* ... fall through ... */
3075 case E_Allocator_Type:
3077 case E_Access_Attribute_Type:
3078 case E_Anonymous_Access_Type:
3079 case E_General_Access_Type:
3081 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3082 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3083 bool is_from_limited_with
3084 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3085 && From_With_Type (gnat_desig_equiv));
3087 /* Get the "full view" of this entity. If this is an incomplete
3088 entity from a limited with, treat its non-limited view as the full
3089 view. Otherwise, if this is an incomplete or private type, use the
3090 full view. In the former case, we might point to a private type,
3091 in which case, we need its full view. Also, we want to look at the
3092 actual type used for the representation, so this takes a total of
3094 Entity_Id gnat_desig_full_direct_first
3095 = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3096 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3097 ? Full_View (gnat_desig_equiv) : Empty));
3098 Entity_Id gnat_desig_full_direct
3099 = ((is_from_limited_with
3100 && Present (gnat_desig_full_direct_first)
3101 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3102 ? Full_View (gnat_desig_full_direct_first)
3103 : gnat_desig_full_direct_first);
3104 Entity_Id gnat_desig_full
3105 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3107 /* This the type actually used to represent the designated type,
3108 either gnat_desig_full or gnat_desig_equiv. */
3109 Entity_Id gnat_desig_rep;
3111 /* Nonzero if this is a pointer to an unconstrained array. */
3112 bool is_unconstrained_array;
3114 /* We want to know if we'll be seeing the freeze node for any
3115 incomplete type we may be pointing to. */
3117 = (Present (gnat_desig_full)
3118 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3119 : In_Extended_Main_Code_Unit (gnat_desig_type));
3121 /* Nonzero if we make a dummy type here. */
3122 bool got_fat_p = false;
3123 /* Nonzero if the dummy is a fat pointer. */
3124 bool made_dummy = false;
3125 tree gnu_desig_type = NULL_TREE;
3126 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3128 if (!targetm.valid_pointer_mode (p_mode))
3131 /* If either the designated type or its full view is an unconstrained
3132 array subtype, replace it with the type it's a subtype of. This
3133 avoids problems with multiple copies of unconstrained array types.
3134 Likewise, if the designated type is a subtype of an incomplete
3135 record type, use the parent type to avoid order of elaboration
3136 issues. This can lose some code efficiency, but there is no
3138 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3139 && ! Is_Constrained (gnat_desig_equiv))
3140 gnat_desig_equiv = Etype (gnat_desig_equiv);
3141 if (Present (gnat_desig_full)
3142 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3143 && ! Is_Constrained (gnat_desig_full))
3144 || (Ekind (gnat_desig_full) == E_Record_Subtype
3145 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3146 gnat_desig_full = Etype (gnat_desig_full);
3148 /* Now set the type that actually marks the representation of
3149 the designated type and also flag whether we have a unconstrained
3151 gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3152 is_unconstrained_array
3153 = (Is_Array_Type (gnat_desig_rep)
3154 && ! Is_Constrained (gnat_desig_rep));
3156 /* If we are pointing to an incomplete type whose completion is an
3157 unconstrained array, make a fat pointer type. The two types in our
3158 fields will be pointers to dummy nodes and will be replaced in
3159 update_pointer_to. Similarly, if the type itself is a dummy type or
3160 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3161 in case we have any thin pointers to it. */
3162 if (is_unconstrained_array
3163 && (Present (gnat_desig_full)
3164 || (present_gnu_tree (gnat_desig_equiv)
3165 && TYPE_IS_DUMMY_P (TREE_TYPE
3166 (get_gnu_tree (gnat_desig_equiv))))
3167 || (No (gnat_desig_full) && ! in_main_unit
3168 && defer_incomplete_level != 0
3169 && ! present_gnu_tree (gnat_desig_equiv))
3170 || (in_main_unit && is_from_limited_with
3171 && Present (Freeze_Node (gnat_desig_rep)))))
3174 = (present_gnu_tree (gnat_desig_rep)
3175 ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
3176 : make_dummy_type (gnat_desig_rep));
3179 /* Show the dummy we get will be a fat pointer. */
3180 got_fat_p = made_dummy = true;
3182 /* If the call above got something that has a pointer, that
3183 pointer is our type. This could have happened either
3184 because the type was elaborated or because somebody
3185 else executed the code below. */
3186 gnu_type = TYPE_POINTER_TO (gnu_old);
3189 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3190 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3191 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3192 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3194 TYPE_NAME (gnu_template_type)
3195 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3197 TYPE_DUMMY_P (gnu_template_type) = 1;
3199 TYPE_NAME (gnu_array_type)
3200 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3202 TYPE_DUMMY_P (gnu_array_type) = 1;
3204 gnu_type = make_node (RECORD_TYPE);
3205 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3206 TYPE_POINTER_TO (gnu_old) = gnu_type;
3208 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3210 = chainon (chainon (NULL_TREE,
3212 (get_identifier ("P_ARRAY"),
3214 gnu_type, 0, 0, 0, 0)),
3215 create_field_decl (get_identifier ("P_BOUNDS"),
3217 gnu_type, 0, 0, 0, 0));
3219 /* Make sure we can place this into a register. */
3220 TYPE_ALIGN (gnu_type)
3221 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3222 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3224 /* Do not finalize this record type since the types of
3225 its fields are incomplete. */
3226 finish_record_type (gnu_type, fields, 0, true);
3228 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3229 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3230 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3232 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3236 /* If we already know what the full type is, use it. */
3237 else if (Present (gnat_desig_full)
3238 && present_gnu_tree (gnat_desig_full))
3239 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3241 /* Get the type of the thing we are to point to and build a pointer
3242 to it. If it is a reference to an incomplete or private type with a
3243 full view that is a record, make a dummy type node and get the
3244 actual type later when we have verified it is safe. */
3245 else if ((! in_main_unit
3246 && ! present_gnu_tree (gnat_desig_equiv)
3247 && Present (gnat_desig_full)
3248 && ! present_gnu_tree (gnat_desig_full)
3249 && Is_Record_Type (gnat_desig_full))
3250 /* Likewise if we are pointing to a record or array and we
3251 are to defer elaborating incomplete types. We do this
3252 since this access type may be the full view of some
3253 private type. Note that the unconstrained array case is
3255 || ((! in_main_unit || imported_p)
3256 && defer_incomplete_level != 0
3257 && ! present_gnu_tree (gnat_desig_equiv)
3258 && ((Is_Record_Type (gnat_desig_rep)
3259 || Is_Array_Type (gnat_desig_rep))))
3260 /* If this is a reference from a limited_with type back to our
3261 main unit and there's a Freeze_Node for it, either we have
3262 already processed the declaration and made the dummy type,
3263 in which case we just reuse the latter, or we have not yet,
3264 in which case we make the dummy type and it will be reused
3265 when the declaration is processed. In both cases, the
3266 pointer eventually created below will be automatically
3267 adjusted when the Freeze_Node is processed. Note that the
3268 unconstrained array case is handled above. */
3269 || (in_main_unit && is_from_limited_with
3270 && Present (Freeze_Node (gnat_desig_rep))))
3272 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3276 /* Otherwise handle the case of a pointer to itself. */
3277 else if (gnat_desig_equiv == gnat_entity)
3280 = build_pointer_type_for_mode (void_type_node, p_mode,
3281 No_Strict_Aliasing (gnat_entity));
3282 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3285 /* If expansion is disabled, the equivalent type of a concurrent
3286 type is absent, so build a dummy pointer type. */
3287 else if (type_annotate_only && No (gnat_desig_equiv))
3288 gnu_type = ptr_void_type_node;
3290 /* Finally, handle the straightforward case where we can just
3291 elaborate our designated type and point to it. */
3293 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3295 /* It is possible that a call to gnat_to_gnu_type above resolved our
3296 type. If so, just return it. */
3297 if (present_gnu_tree (gnat_entity))
3299 maybe_present = true;
3303 /* If we have a GCC type for the designated type, possibly modify it
3304 if we are pointing only to constant objects and then make a pointer
3305 to it. Don't do this for unconstrained arrays. */
3306 if (!gnu_type && gnu_desig_type)
3308 if (Is_Access_Constant (gnat_entity)
3309 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3312 = build_qualified_type
3314 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3316 /* Some extra processing is required if we are building a
3317 pointer to an incomplete type (in the GCC sense). We might
3318 have such a type if we just made a dummy, or directly out
3319 of the call to gnat_to_gnu_type above if we are processing
3320 an access type for a record component designating the
3321 record type itself. */
3322 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3324 /* We must ensure that the pointer to variant we make will
3325 be processed by update_pointer_to when the initial type
3326 is completed. Pretend we made a dummy and let further
3327 processing act as usual. */
3330 /* We must ensure that update_pointer_to will not retrieve
3331 the dummy variant when building a properly qualified
3332 version of the complete type. We take advantage of the
3333 fact that get_qualified_type is requiring TYPE_NAMEs to
3334 match to influence build_qualified_type and then also
3335 update_pointer_to here. */
3336 TYPE_NAME (gnu_desig_type)
3337 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3342 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3343 No_Strict_Aliasing (gnat_entity));
3346 /* If we are not defining this object and we made a dummy pointer,
3347 save our current definition, evaluate the actual type, and replace
3348 the tentative type we made with the actual one. If we are to defer
3349 actually looking up the actual type, make an entry in the
3350 deferred list. If this is from a limited with, we have to defer
3351 to the end of the current spec in two cases: first if the
3352 designated type is in the current unit and second if the access
3354 if ((! in_main_unit || is_from_limited_with) && made_dummy)
3357 = TYPE_FAT_POINTER_P (gnu_type)
3358 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3360 if (esize == POINTER_SIZE
3361 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3363 = build_pointer_type
3364 (TYPE_OBJECT_RECORD_TYPE
3365 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3367 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3368 !Comes_From_Source (gnat_entity),
3369 debug_info_p, gnat_entity);
3370 this_made_decl = true;
3371 gnu_type = TREE_TYPE (gnu_decl);
3372 save_gnu_tree (gnat_entity, gnu_decl, false);
3375 if (defer_incomplete_level == 0
3376 && ! (is_from_limited_with
3378 || In_Extended_Main_Code_Unit (gnat_entity))))
3379 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3380 gnat_to_gnu_type (gnat_desig_equiv));
3382 /* Note that the call to gnat_to_gnu_type here might have
3383 updated gnu_old_type directly, in which case it is not a
3384 dummy type any more when we get into update_pointer_to.
3386 This may happen for instance when the designated type is a
3387 record type, because their elaboration starts with an
3388 initial node from make_dummy_type, which may yield the same
3389 node as the one we got.
3391 Besides, variants of this non-dummy type might have been
3392 created along the way. update_pointer_to is expected to
3393 properly take care of those situations. */
3396 struct incomplete *p
3397 = (struct incomplete *) xmalloc (sizeof
3398 (struct incomplete));
3399 struct incomplete **head
3400 = (is_from_limited_with
3402 || In_Extended_Main_Code_Unit (gnat_entity))
3403 ? &defer_limited_with : &defer_incomplete_list);
3405 p->old_type = gnu_old_type;
3406 p->full_type = gnat_desig_equiv;
3414 case E_Access_Protected_Subprogram_Type:
3415 case E_Anonymous_Access_Protected_Subprogram_Type:
3416 if (type_annotate_only && No (gnat_equiv_type))
3417 gnu_type = ptr_void_type_node;
3420 /* The runtime representation is the equivalent type. */
3421 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3425 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3426 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3427 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3428 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3429 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3434 case E_Access_Subtype:
3436 /* We treat this as identical to its base type; any constraint is
3437 meaningful only to the front end.
3439 The designated type must be elaborated as well, if it does
3440 not have its own freeze node. Designated (sub)types created
3441 for constrained components of records with discriminants are
3442 not frozen by the front end and thus not elaborated by gigi,
3443 because their use may appear before the base type is frozen,
3444 and because it is not clear that they are needed anywhere in
3445 Gigi. With the current model, there is no correct place where
3446 they could be elaborated. */
3448 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3449 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3450 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3451 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3452 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3454 /* If we are not defining this entity, and we have incomplete
3455 entities being processed above us, make a dummy type and
3456 elaborate it later. */
3457 if (!definition && defer_incomplete_level != 0)
3459 struct incomplete *p
3460 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3462 = build_pointer_type
3463 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3465 p->old_type = TREE_TYPE (gnu_ptr_type);
3466 p->full_type = Directly_Designated_Type (gnat_entity);
3467 p->next = defer_incomplete_list;
3468 defer_incomplete_list = p;
3470 else if (!IN (Ekind (Base_Type
3471 (Directly_Designated_Type (gnat_entity))),
3472 Incomplete_Or_Private_Kind))
3473 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3477 maybe_present = true;
3480 /* Subprogram Entities
3482 The following access functions are defined for subprograms (functions
3485 First_Formal The first formal parameter.
3486 Is_Imported Indicates that the subprogram has appeared in
3487 an INTERFACE or IMPORT pragma. For now we
3488 assume that the external language is C.
3489 Is_Exported Likewise but for an EXPORT pragma.
3490 Is_Inlined True if the subprogram is to be inlined.
3492 In addition for function subprograms we have:
3494 Etype Return type of the function.
3496 Each parameter is first checked by calling must_pass_by_ref on its
3497 type to determine if it is passed by reference. For parameters which
3498 are copied in, if they are Ada In Out or Out parameters, their return
3499 value becomes part of a record which becomes the return type of the
3500 function (C function - note that this applies only to Ada procedures
3501 so there is no Ada return type). Additional code to store back the
3502 parameters will be generated on the caller side. This transformation
3503 is done here, not in the front-end.
3505 The intended result of the transformation can be seen from the
3506 equivalent source rewritings that follow:
3508 struct temp {int a,b};
3509 procedure P (A,B: In Out ...) is temp P (int A,B)
3512 end P; return {A,B};
3519 For subprogram types we need to perform mainly the same conversions to
3520 GCC form that are needed for procedures and function declarations. The
3521 only difference is that at the end, we make a type declaration instead
3522 of a function declaration. */
3524 case E_Subprogram_Type:
3528 /* The first GCC parameter declaration (a PARM_DECL node). The
3529 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3530 actually is the head of this parameter list. */
3531 tree gnu_param_list = NULL_TREE;
3532 /* Likewise for the stub associated with an exported procedure. */
3533 tree gnu_stub_param_list = NULL_TREE;
3534 /* The type returned by a function. If the subprogram is a procedure
3535 this type should be void_type_node. */
3536 tree gnu_return_type = void_type_node;
3537 /* List of fields in return type of procedure with copy-in copy-out
3539 tree gnu_field_list = NULL_TREE;
3540 /* Non-null for subprograms containing parameters passed by copy-in
3541 copy-out (Ada In Out or Out parameters not passed by reference),
3542 in which case it is the list of nodes used to specify the values of
3543 the in out/out parameters that are returned as a record upon
3544 procedure return. The TREE_PURPOSE of an element of this list is
3545 a field of the record and the TREE_VALUE is the PARM_DECL
3546 corresponding to that field. This list will be saved in the
3547 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3548 tree gnu_return_list = NULL_TREE;
3549 /* If an import pragma asks to map this subprogram to a GCC builtin,
3550 this is the builtin DECL node. */
3551 tree gnu_builtin_decl = NULL_TREE;
3552 /* For the stub associated with an exported procedure. */
3553 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3554 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3555 Entity_Id gnat_param;
3556 bool inline_flag = Is_Inlined (gnat_entity);
3557 bool public_flag = Is_Public (gnat_entity) || imported_p;
3559 = (Is_Public (gnat_entity) && !definition) || imported_p;
3560 bool pure_flag = Is_Pure (gnat_entity);
3561 bool volatile_flag = No_Return (gnat_entity);
3562 bool returns_by_ref = false;
3563 bool returns_unconstrained = false;
3564 bool returns_by_target_ptr = false;
3565 bool has_copy_in_out = false;
3566 bool has_stub = false;
3569 if (kind == E_Subprogram_Type && !definition)
3570 /* A parameter may refer to this type, so defer completion
3571 of any incomplete types. */
3572 defer_incomplete_level++, this_deferred = true;
3574 /* If the subprogram has an alias, it is probably inherited, so
3575 we can use the original one. If the original "subprogram"
3576 is actually an enumeration literal, it may be the first use
3577 of its type, so we must elaborate that type now. */
3578 if (Present (Alias (gnat_entity)))
3580 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3581 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3583 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3586 /* Elaborate any Itypes in the parameters of this entity. */
3587 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3588 Present (gnat_temp);
3589 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3590 if (Is_Itype (Etype (gnat_temp)))
3591 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3596 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3597 corresponding DECL node.
3599 We still want the parameter associations to take place because the
3600 proper generation of calls depends on it (a GNAT parameter without
3601 a corresponding GCC tree has a very specific meaning), so we don't
3603 if (Convention (gnat_entity) == Convention_Intrinsic)
3604 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3606 /* ??? What if we don't find the builtin node above ? warn ? err ?
3607 In the current state we neither warn nor err, and calls will just
3608 be handled as for regular subprograms. */
3610 if (kind == E_Function || kind == E_Subprogram_Type)
3611 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3613 /* If this function returns by reference, make the actual
3614 return type of this function the pointer and mark the decl. */
3615 if (Returns_By_Ref (gnat_entity))
3617 returns_by_ref = true;
3618 gnu_return_type = build_pointer_type (gnu_return_type);
3621 /* If the Mechanism is By_Reference, ensure the return type uses
3622 the machine's by-reference mechanism, which may not the same
3623 as above (e.g., it might be by passing a fake parameter). */
3624 else if (kind == E_Function
3625 && Mechanism (gnat_entity) == By_Reference)
3627 TREE_ADDRESSABLE (gnu_return_type) = 1;
3629 /* We expect this bit to be reset by gigi shortly, so can avoid a
3630 type node copy here. This actually also prevents troubles with
3631 the generation of debug information for the function, because
3632 we might have issued such info for this type already, and would
3633 be attaching a distinct type node to the function if we made a
3637 /* If we are supposed to return an unconstrained array,
3638 actually return a fat pointer and make a note of that. Return
3639 a pointer to an unconstrained record of variable size. */
3640 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3642 gnu_return_type = TREE_TYPE (gnu_return_type);
3643 returns_unconstrained = true;
3646 /* If the type requires a transient scope, the result is allocated
3647 on the secondary stack, so the result type of the function is
3649 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3651 gnu_return_type = build_pointer_type (gnu_return_type);
3652 returns_unconstrained = true;
3655 /* If the type is a padded type and the underlying type would not
3656 be passed by reference or this function has a foreign convention,
3657 return the underlying type. */
3658 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3659 && TYPE_IS_PADDING_P (gnu_return_type)
3660 && (!default_pass_by_ref (TREE_TYPE
3661 (TYPE_FIELDS (gnu_return_type)))
3662 || Has_Foreign_Convention (gnat_entity)))
3663 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3665 /* If the return type is unconstrained, that means it must have a
3666 maximum size. We convert the function into a procedure and its
3667 caller will pass a pointer to an object of that maximum size as the
3668 first parameter when we call the function. */
3669 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3671 returns_by_target_ptr = true;
3673 = create_param_decl (get_identifier ("TARGET"),
3674 build_reference_type (gnu_return_type),
3676 gnu_return_type = void_type_node;
3679 /* If the return type has a size that overflows, we cannot have
3680 a function that returns that type. This usage doesn't make
3681 sense anyway, so give an error here. */
3682 if (TYPE_SIZE_UNIT (gnu_return_type)
3683 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3684 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3686 post_error ("cannot return type whose size overflows",
3688 gnu_return_type = copy_node (gnu_return_type);
3689 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3690 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3691 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3692 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3695 /* Look at all our parameters and get the type of
3696 each. While doing this, build a copy-out structure if
3699 /* Loop over the parameters and get their associated GCC tree.
3700 While doing this, build a copy-out structure if we need one. */
3701 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3702 Present (gnat_param);
3703 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3705 tree gnu_param_name = get_entity_name (gnat_param);
3706 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3707 tree gnu_param, gnu_field;
3708 bool copy_in_copy_out = false;
3709 Mechanism_Type mech = Mechanism (gnat_param);
3711 /* Builtins are expanded inline and there is no real call sequence
3712 involved. So the type expected by the underlying expander is
3713 always the type of each argument "as is". */
3714 if (gnu_builtin_decl)
3716 /* Handle the first parameter of a valued procedure specially. */
3717 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3718 mech = By_Copy_Return;
3719 /* Otherwise, see if a Mechanism was supplied that forced this
3720 parameter to be passed one way or another. */
3721 else if (mech == Default
3722 || mech == By_Copy || mech == By_Reference)
3724 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
3725 mech = By_Descriptor;
3728 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3729 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3730 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3732 mech = By_Reference;
3738 post_error ("unsupported mechanism for&", gnat_param);
3743 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
3744 Has_Foreign_Convention (gnat_entity),
3747 /* We are returned either a PARM_DECL or a type if no parameter
3748 needs to be passed; in either case, adjust the type. */
3749 if (DECL_P (gnu_param))
3750 gnu_param_type = TREE_TYPE (gnu_param);
3753 gnu_param_type = gnu_param;
3754 gnu_param = NULL_TREE;
3759 /* If it's an exported subprogram, we build a parameter list
3760 in parallel, in case we need to emit a stub for it. */
3761 if (Is_Exported (gnat_entity))
3764 = chainon (gnu_param, gnu_stub_param_list);
3765 /* Change By_Descriptor parameter to By_Reference for
3766 the internal version of an exported subprogram. */
3767 if (mech == By_Descriptor)
3770 = gnat_to_gnu_param (gnat_param, By_Reference,
3776 gnu_param = copy_node (gnu_param);
3779 gnu_param_list = chainon (gnu_param, gnu_param_list);
3780 Sloc_to_locus (Sloc (gnat_param),
3781 &DECL_SOURCE_LOCATION (gnu_param));
3782 save_gnu_tree (gnat_param, gnu_param, false);
3784 /* If a parameter is a pointer, this function may modify
3785 memory through it and thus shouldn't be considered
3786 a pure function. Also, the memory may be modified
3787 between two calls, so they can't be CSE'ed. The latter
3788 case also handles by-ref parameters. */
3789 if (POINTER_TYPE_P (gnu_param_type)
3790 || TYPE_FAT_POINTER_P (gnu_param_type))
3794 if (copy_in_copy_out)
3796 if (!has_copy_in_out)
3798 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3799 gnu_return_type = make_node (RECORD_TYPE);
3800 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3801 has_copy_in_out = true;
3804 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3805 gnu_return_type, 0, 0, 0, 0);
3806 Sloc_to_locus (Sloc (gnat_param),
3807 &DECL_SOURCE_LOCATION (gnu_field));
3808 TREE_CHAIN (gnu_field) = gnu_field_list;
3809 gnu_field_list = gnu_field;
3810 gnu_return_list = tree_cons (gnu_field, gnu_param,
3815 /* Do not compute record for out parameters if subprogram is
3816 stubbed since structures are incomplete for the back-end. */
3817 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
3818 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3821 /* If we have a CICO list but it has only one entry, we convert
3822 this function into a function that simply returns that one
3824 if (list_length (gnu_return_list) == 1)
3825 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3827 if (Has_Stdcall_Convention (gnat_entity))
3828 prepend_one_attribute_to
3829 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3830 get_identifier ("stdcall"), NULL_TREE,
3833 /* The lists have been built in reverse. */
3834 gnu_param_list = nreverse (gnu_param_list);
3836 gnu_stub_param_list = nreverse (gnu_stub_param_list);
3837 gnu_return_list = nreverse (gnu_return_list);
3839 if (Ekind (gnat_entity) == E_Function)
3840 Set_Mechanism (gnat_entity,
3841 (returns_by_ref || returns_unconstrained
3842 ? By_Reference : By_Copy));
3844 = create_subprog_type (gnu_return_type, gnu_param_list,
3845 gnu_return_list, returns_unconstrained,
3847 Function_Returns_With_DSP (gnat_entity),
3848 returns_by_target_ptr);
3852 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
3853 gnu_return_list, returns_unconstrained,
3855 Function_Returns_With_DSP (gnat_entity),
3856 returns_by_target_ptr);
3858 /* A subprogram (something that doesn't return anything) shouldn't
3859 be considered Pure since there would be no reason for such a
3860 subprogram. Note that procedures with Out (or In Out) parameters
3861 have already been converted into a function with a return type. */
3862 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3865 /* The semantics of "pure" in Ada essentially matches that of "const"
3866 in the back-end. In particular, both properties are orthogonal to
3867 the "nothrow" property. But this is true only if the EH circuitry
3868 is explicit in the internal representation of the back-end. If we
3869 are to completely hide the EH circuitry from it, we need to declare
3870 that calls to pure Ada subprograms that can throw have side effects
3871 since they can trigger an "abnormal" transfer of control flow; thus
3872 they can be neither "const" nor "pure" in the back-end sense. */
3874 = build_qualified_type (gnu_type,
3875 TYPE_QUALS (gnu_type)
3876 | (Exception_Mechanism == Back_End_Exceptions
3877 ? TYPE_QUAL_CONST * pure_flag : 0)
3878 | (TYPE_QUAL_VOLATILE * volatile_flag));
3880 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3884 = build_qualified_type (gnu_stub_type,
3885 TYPE_QUALS (gnu_stub_type)
3886 | (Exception_Mechanism == Back_End_Exceptions
3887 ? TYPE_QUAL_CONST * pure_flag : 0)
3888 | (TYPE_QUAL_VOLATILE * volatile_flag));
3890 /* If we have a builtin decl for that function, check the signatures
3891 compatibilities. If the signatures are compatible, use the builtin
3892 decl. If they are not, we expect the checker predicate to have
3893 posted the appropriate errors, and just continue with what we have
3895 if (gnu_builtin_decl)
3897 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3899 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3901 gnu_decl = gnu_builtin_decl;
3902 gnu_type = gnu_builtin_type;
3907 /* If there was no specified Interface_Name and the external and
3908 internal names of the subprogram are the same, only use the
3909 internal name to allow disambiguation of nested subprograms. */
3910 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3911 gnu_ext_name = NULL_TREE;
3913 /* If we are defining the subprogram and it has an Address clause
3914 we must get the address expression from the saved GCC tree for the
3915 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3916 the address expression here since the front-end has guaranteed
3917 in that case that the elaboration has no effects. If there is
3918 an Address clause and we are not defining the object, just
3919 make it a constant. */
3920 if (Present (Address_Clause (gnat_entity)))
3922 tree gnu_address = NULL_TREE;
3926 = (present_gnu_tree (gnat_entity)
3927 ? get_gnu_tree (gnat_entity)
3928 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3930 save_gnu_tree (gnat_entity, NULL_TREE, false);
3932 /* Convert the type of the object to a reference type that can
3933 alias everything as per 13.3(19). */
3935 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
3937 gnu_address = convert (gnu_type, gnu_address);
3940 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3941 gnu_address, false, Is_Public (gnat_entity),
3942 extern_flag, false, NULL, gnat_entity);
3943 DECL_BY_REF_P (gnu_decl) = 1;
3946 else if (kind == E_Subprogram_Type)
3947 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3948 !Comes_From_Source (gnat_entity),
3949 debug_info_p, gnat_entity);
3954 gnu_stub_name = gnu_ext_name;
3955 gnu_ext_name = create_concat_name (gnat_entity, "internal");
3956 public_flag = false;
3959 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3960 gnu_type, gnu_param_list,
3961 inline_flag, public_flag,
3962 extern_flag, attr_list,
3967 = create_subprog_decl (gnu_entity_id, gnu_stub_name,
3968 gnu_stub_type, gnu_stub_param_list,
3970 extern_flag, attr_list,
3972 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
3975 /* This is unrelated to the stub built right above. */
3976 DECL_STUBBED_P (gnu_decl)
3977 = Convention (gnat_entity) == Convention_Stubbed;
3982 case E_Incomplete_Type:
3983 case E_Incomplete_Subtype:
3984 case E_Private_Type:
3985 case E_Private_Subtype:
3986 case E_Limited_Private_Type:
3987 case E_Limited_Private_Subtype:
3988 case E_Record_Type_With_Private:
3989 case E_Record_Subtype_With_Private:
3991 /* Get the "full view" of this entity. If this is an incomplete
3992 entity from a limited with, treat its non-limited view as the
3993 full view. Otherwise, use either the full view or the underlying
3994 full view, whichever is present. This is used in all the tests
3997 = (IN (Ekind (gnat_entity), Incomplete_Kind)
3998 && From_With_Type (gnat_entity))
3999 ? Non_Limited_View (gnat_entity)
4000 : Present (Full_View (gnat_entity))
4001 ? Full_View (gnat_entity)
4002 : Underlying_Full_View (gnat_entity);
4004 /* If this is an incomplete type with no full view, it must be a Taft
4005 Amendment type, in which case we return a dummy type. Otherwise,
4006 just get the type from its Etype. */
4009 if (kind == E_Incomplete_Type)
4010 gnu_type = make_dummy_type (gnat_entity);
4013 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4015 maybe_present = true;
4020 /* If we already made a type for the full view, reuse it. */
4021 else if (present_gnu_tree (full_view))
4023 gnu_decl = get_gnu_tree (full_view);
4027 /* Otherwise, if we are not defining the type now, get the type
4028 from the full view. But always get the type from the full view
4029 for define on use types, since otherwise we won't see them! */
4030 else if (!definition
4031 || (Is_Itype (full_view)
4032 && No (Freeze_Node (gnat_entity)))
4033 || (Is_Itype (gnat_entity)
4034 && No (Freeze_Node (full_view))))
4036 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4037 maybe_present = true;
4041 /* For incomplete types, make a dummy type entry which will be
4043 gnu_type = make_dummy_type (gnat_entity);
4045 /* Save this type as the full declaration's type so we can do any
4046 needed updates when we see it. */
4047 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4048 !Comes_From_Source (gnat_entity),
4049 debug_info_p, gnat_entity);
4050 save_gnu_tree (full_view, gnu_decl, 0);
4054 /* Simple class_wide types are always viewed as their root_type
4055 by Gigi unless an Equivalent_Type is specified. */
4056 case E_Class_Wide_Type:
4057 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4058 maybe_present = true;
4062 case E_Task_Subtype:
4063 case E_Protected_Type:
4064 case E_Protected_Subtype:
4065 if (type_annotate_only && No (gnat_equiv_type))
4066 gnu_type = void_type_node;
4068 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4070 maybe_present = true;
4074 gnu_decl = create_label_decl (gnu_entity_id);
4079 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4080 we've already saved it, so we don't try to. */
4081 gnu_decl = error_mark_node;
4089 /* If we had a case where we evaluated another type and it might have
4090 defined this one, handle it here. */
4091 if (maybe_present && present_gnu_tree (gnat_entity))
4093 gnu_decl = get_gnu_tree (gnat_entity);
4097 /* If we are processing a type and there is either no decl for it or
4098 we just made one, do some common processing for the type, such as
4099 handling alignment and possible padding. */
4101 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4103 if (Is_Tagged_Type (gnat_entity)
4104 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4105 TYPE_ALIGN_OK (gnu_type) = 1;
4107 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4108 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4110 /* ??? Don't set the size for a String_Literal since it is either
4111 confirming or we don't handle it properly (if the low bound is
4113 if (!gnu_size && kind != E_String_Literal_Subtype)
4114 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4116 Has_Size_Clause (gnat_entity));
4118 /* If a size was specified, see if we can make a new type of that size
4119 by rearranging the type, for example from a fat to a thin pointer. */
4123 = make_type_from_size (gnu_type, gnu_size,
4124 Has_Biased_Representation (gnat_entity));
4126 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4127 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4131 /* If the alignment hasn't already been processed and this is
4132 not an unconstrained array, see if an alignment is specified.
4133 If not, we pick a default alignment for atomic objects. */
4134 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4136 else if (Known_Alignment (gnat_entity))
4138 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4139 TYPE_ALIGN (gnu_type));
4141 /* Warn on suspiciously large alignments. This should catch
4142 errors about the (alignment,byte)/(size,bit) discrepancy. */
4143 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4147 /* If a size was specified, take it into account. Otherwise
4148 use the RM size for records as the type size has already
4149 been adjusted to the alignment. */
4152 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4153 || TREE_CODE (gnu_type) == UNION_TYPE
4154 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4155 && !TYPE_IS_FAT_POINTER_P (gnu_type))
4156 size = rm_size (gnu_type);
4158 size = TYPE_SIZE (gnu_type);
4160 /* Consider an alignment as suspicious if the alignment/size
4161 ratio is greater or equal to the byte/bit ratio. */
4162 if (host_integerp (size, 1)
4163 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4164 post_error_ne ("?suspiciously large alignment specified for&",
4165 Expression (Alignment_Clause (gnat_entity)),
4169 else if (Is_Atomic (gnat_entity) && !gnu_size
4170 && host_integerp (TYPE_SIZE (gnu_type), 1)
4171 && integer_pow2p (TYPE_SIZE (gnu_type)))
4172 align = MIN (BIGGEST_ALIGNMENT,
4173 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4174 else if (Is_Atomic (gnat_entity) && gnu_size
4175 && host_integerp (gnu_size, 1)
4176 && integer_pow2p (gnu_size))
4177 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4179 /* See if we need to pad the type. If we did, and made a record,
4180 the name of the new type may be changed. So get it back for
4181 us when we make the new TYPE_DECL below. */
4182 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
4183 true, definition, false);
4184 if (TREE_CODE (gnu_type) == RECORD_TYPE
4185 && TYPE_IS_PADDING_P (gnu_type))
4187 gnu_entity_id = TYPE_NAME (gnu_type);
4188 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4189 gnu_entity_id = DECL_NAME (gnu_entity_id);
4192 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4194 /* If we are at global level, GCC will have applied variable_size to
4195 the type, but that won't have done anything. So, if it's not
4196 a constant or self-referential, call elaborate_expression_1 to
4197 make a variable for the size rather than calculating it each time.
4198 Handle both the RM size and the actual size. */
4199 if (global_bindings_p ()
4200 && TYPE_SIZE (gnu_type)
4201 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4202 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4204 if (TREE_CODE (gnu_type) == RECORD_TYPE
4205 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4206 TYPE_SIZE (gnu_type), 0))
4208 TYPE_SIZE (gnu_type)
4209 = elaborate_expression_1 (gnat_entity, gnat_entity,
4210 TYPE_SIZE (gnu_type),
4211 get_identifier ("SIZE"),
4213 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4217 TYPE_SIZE (gnu_type)
4218 = elaborate_expression_1 (gnat_entity, gnat_entity,
4219 TYPE_SIZE (gnu_type),
4220 get_identifier ("SIZE"),
4223 /* ??? For now, store the size as a multiple of the alignment
4224 in bytes so that we can see the alignment from the tree. */
4225 TYPE_SIZE_UNIT (gnu_type)
4227 (MULT_EXPR, sizetype,
4228 elaborate_expression_1
4229 (gnat_entity, gnat_entity,
4230 build_binary_op (EXACT_DIV_EXPR, sizetype,
4231 TYPE_SIZE_UNIT (gnu_type),
4232 size_int (TYPE_ALIGN (gnu_type)
4234 get_identifier ("SIZE_A_UNIT"),
4236 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4238 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4241 elaborate_expression_1 (gnat_entity,
4243 TYPE_ADA_SIZE (gnu_type),
4244 get_identifier ("RM_SIZE"),
4249 /* If this is a record type or subtype, call elaborate_expression_1 on
4250 any field position. Do this for both global and local types.
4251 Skip any fields that we haven't made trees for to avoid problems with
4252 class wide types. */
4253 if (IN (kind, Record_Kind))
4254 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4255 gnat_temp = Next_Entity (gnat_temp))
4256 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4258 tree gnu_field = get_gnu_tree (gnat_temp);
4260 /* ??? Unfortunately, GCC needs to be able to prove the
4261 alignment of this offset and if it's a variable, it can't.
4262 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4263 right now, we have to put in an explicit multiply and
4264 divide by that value. */
4265 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4267 DECL_FIELD_OFFSET (gnu_field)
4269 (MULT_EXPR, sizetype,
4270 elaborate_expression_1
4271 (gnat_temp, gnat_temp,
4272 build_binary_op (EXACT_DIV_EXPR, sizetype,
4273 DECL_FIELD_OFFSET (gnu_field),
4274 size_int (DECL_OFFSET_ALIGN (gnu_field)
4276 get_identifier ("OFFSET"),
4278 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4280 /* ??? The context of gnu_field is not necessarily gnu_type so
4281 the MULT_EXPR node built above may not be marked by the call
4282 to create_type_decl below. Mark it manually for now. */
4283 if (global_bindings_p ())
4284 TREE_VISITED (DECL_FIELD_OFFSET (gnu_field)) = 1;
4288 gnu_type = build_qualified_type (gnu_type,
4289 (TYPE_QUALS (gnu_type)
4290 | (TYPE_QUAL_VOLATILE
4291 * Treat_As_Volatile (gnat_entity))));
4293 if (Is_Atomic (gnat_entity))
4294 check_ok_for_atomic (gnu_type, gnat_entity, false);
4296 if (Present (Alignment_Clause (gnat_entity)))
4297 TYPE_USER_ALIGN (gnu_type) = 1;
4299 if (Universal_Aliasing (gnat_entity))
4300 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4303 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4304 !Comes_From_Source (gnat_entity),
4305 debug_info_p, gnat_entity);
4307 TREE_TYPE (gnu_decl) = gnu_type;
4310 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4312 gnu_type = TREE_TYPE (gnu_decl);
4314 /* Back-annotate the Alignment of the type if not already in the
4315 tree. Likewise for sizes. */
4316 if (Unknown_Alignment (gnat_entity))
4317 Set_Alignment (gnat_entity,
4318 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4320 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4322 /* If the size is self-referential, we annotate the maximum
4323 value of that size. */
4324 tree gnu_size = TYPE_SIZE (gnu_type);
4326 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4327 gnu_size = max_size (gnu_size, true);
4329 Set_Esize (gnat_entity, annotate_value (gnu_size));
4331 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4333 /* In this mode the tag and the parent components are not
4334 generated by the front-end, so the sizes must be adjusted
4336 int size_offset, new_size;
4338 if (Is_Derived_Type (gnat_entity))
4341 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4342 Set_Alignment (gnat_entity,
4343 Alignment (Etype (Base_Type (gnat_entity))));
4346 size_offset = POINTER_SIZE;
4348 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4349 Set_Esize (gnat_entity,
4350 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4351 / POINTER_SIZE) * POINTER_SIZE));
4352 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4356 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4357 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4360 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4361 DECL_ARTIFICIAL (gnu_decl) = 1;
4363 if (!debug_info_p && DECL_P (gnu_decl)
4364 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4365 && No (Renamed_Object (gnat_entity)))
4366 DECL_IGNORED_P (gnu_decl) = 1;
4368 /* If we haven't already, associate the ..._DECL node that we just made with
4369 the input GNAT entity node. */
4371 save_gnu_tree (gnat_entity, gnu_decl, false);
4373 /* If this is an enumeral or floating-point type, we were not able to set
4374 the bounds since they refer to the type. These bounds are always static.
4376 For enumeration types, also write debugging information and declare the
4377 enumeration literal table, if needed. */
4379 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4380 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4382 tree gnu_scalar_type = gnu_type;
4384 /* If this is a padded type, we need to use the underlying type. */
4385 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4386 && TYPE_IS_PADDING_P (gnu_scalar_type))
4387 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4389 /* If this is a floating point type and we haven't set a floating
4390 point type yet, use this in the evaluation of the bounds. */
4391 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4392 longest_float_type_node = gnu_type;
4394 TYPE_MIN_VALUE (gnu_scalar_type)
4395 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4396 TYPE_MAX_VALUE (gnu_scalar_type)
4397 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4399 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4401 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4403 /* Since this has both a typedef and a tag, avoid outputting
4405 DECL_ARTIFICIAL (gnu_decl) = 1;
4406 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4410 /* If we deferred processing of incomplete types, re-enable it. If there
4411 were no other disables and we have some to process, do so. */
4412 if (this_deferred && --defer_incomplete_level == 0)
4414 if (defer_incomplete_list)
4416 struct incomplete *incp, *next;
4418 /* We are back to level 0 for the deferring of incomplete types.
4419 But processing these incomplete types below may itself require
4420 deferring, so preserve what we have and restart from scratch. */
4421 incp = defer_incomplete_list;
4422 defer_incomplete_list = NULL;
4424 /* For finalization, however, all types must be complete so we
4425 cannot do the same because deferred incomplete types may end up
4426 referencing each other. Process them all recursively first. */
4427 defer_finalize_level++;
4429 for (; incp; incp = next)
4434 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4435 gnat_to_gnu_type (incp->full_type));
4439 defer_finalize_level--;
4442 /* All the deferred incomplete types have been processed so we can
4443 now proceed with the finalization of the deferred types. */
4444 if (defer_finalize_level == 0 && defer_finalize_list)
4446 int toplev = global_bindings_p ();
4450 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4451 rest_of_decl_compilation (t, toplev, 0);
4453 VEC_free (tree, heap, defer_finalize_list);
4457 /* If we are not defining this type, see if it's in the incomplete list.
4458 If so, handle that list entry now. */
4459 else if (!definition)
4461 struct incomplete *incp;
4463 for (incp = defer_incomplete_list; incp; incp = incp->next)
4464 if (incp->old_type && incp->full_type == gnat_entity)
4466 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4467 TREE_TYPE (gnu_decl));
4468 incp->old_type = NULL_TREE;
4475 if (Is_Packed_Array_Type (gnat_entity)
4476 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4477 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4478 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4479 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4484 /* Similar, but if the returned value is a COMPONENT_REF, return the
4488 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4490 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4492 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4493 gnu_field = TREE_OPERAND (gnu_field, 1);
4498 /* Wrap up compilation of T, a TYPE_DECL, possibly deferring it. */
4501 rest_of_type_decl_compilation (tree t)
4503 /* We need to defer finalizing the type if incomplete types
4504 are being deferred or if they are being processed. */
4505 if (defer_incomplete_level || defer_finalize_level)
4506 VEC_safe_push (tree, heap, defer_finalize_list, t);
4508 rest_of_decl_compilation (t, global_bindings_p (), 0);
4511 /* Finalize any From_With_Type incomplete types. We do this after processing
4512 our compilation unit and after processing its spec, if this is a body. */
4515 finalize_from_with_types (void)
4517 struct incomplete *incp = defer_limited_with;
4518 struct incomplete *next;
4520 defer_limited_with = 0;
4521 for (; incp; incp = next)
4525 if (incp->old_type != 0)
4526 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4527 gnat_to_gnu_type (incp->full_type));
4532 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4533 kind of type (such E_Task_Type) that has a different type which Gigi
4534 uses for its representation. If the type does not have a special type
4535 for its representation, return GNAT_ENTITY. If a type is supposed to
4536 exist, but does not, abort unless annotating types, in which case
4537 return Empty. If GNAT_ENTITY is Empty, return Empty. */
4540 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4542 Entity_Id gnat_equiv = gnat_entity;
4544 if (No (gnat_entity))
4547 switch (Ekind (gnat_entity))
4549 case E_Class_Wide_Subtype:
4550 if (Present (Equivalent_Type (gnat_entity)))
4551 gnat_equiv = Equivalent_Type (gnat_entity);
4554 case E_Access_Protected_Subprogram_Type:
4555 case E_Anonymous_Access_Protected_Subprogram_Type:
4556 gnat_equiv = Equivalent_Type (gnat_entity);
4559 case E_Class_Wide_Type:
4560 gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4561 ? Equivalent_Type (gnat_entity)
4562 : Root_Type (gnat_entity));
4566 case E_Task_Subtype:
4567 case E_Protected_Type:
4568 case E_Protected_Subtype:
4569 gnat_equiv = Corresponding_Record_Type (gnat_entity);
4576 gcc_assert (Present (gnat_equiv) || type_annotate_only);
4580 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
4581 using MECH as its passing mechanism, to be placed in the parameter
4582 list built for GNAT_SUBPROG. Assume a foreign convention for the
4583 latter if FOREIGN is true. Also set CICO to true if the parameter
4584 must use the copy-in copy-out implementation mechanism.
4586 The returned tree is a PARM_DECL, except for those cases where no
4587 parameter needs to be actually passed to the subprogram; the type
4588 of this "shadow" parameter is then returned instead. */
4591 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
4592 Entity_Id gnat_subprog, bool foreign, bool *cico)
4594 tree gnu_param_name = get_entity_name (gnat_param);
4595 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4596 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
4597 /* The parameter can be indirectly modified if its address is taken. */
4598 bool ro_param = in_param && !Address_Taken (gnat_param);
4599 bool by_return = false, by_component_ptr = false, by_ref = false;
4602 /* Copy-return is used only for the first parameter of a valued procedure.
4603 It's a copy mechanism for which a parameter is never allocated. */
4604 if (mech == By_Copy_Return)
4606 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
4611 /* If this is either a foreign function or if the underlying type won't
4612 be passed by reference, strip off possible padding type. */
4613 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
4614 && TYPE_IS_PADDING_P (gnu_param_type))
4616 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
4618 if (mech == By_Reference
4620 || (!must_pass_by_ref (unpadded_type)
4621 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
4622 gnu_param_type = unpadded_type;
4625 /* If this is a read-only parameter, make a variant of the type that is
4626 read-only. ??? However, if this is an unconstrained array, that type
4627 can be very complex, so skip it for now. Likewise for any other
4628 self-referential type. */
4630 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
4631 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
4632 gnu_param_type = build_qualified_type (gnu_param_type,
4633 (TYPE_QUALS (gnu_param_type)
4634 | TYPE_QUAL_CONST));
4636 /* For foreign conventions, pass arrays as pointers to the element type.
4637 First check for unconstrained array and get the underlying array. */
4638 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
4640 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
4642 /* VMS descriptors are themselves passed by reference. */
4643 if (mech == By_Descriptor)
4645 = build_pointer_type (build_vms_descriptor (gnu_param_type,
4646 Mechanism (gnat_param),
4649 /* Arrays are passed as pointers to element type for foreign conventions. */
4652 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
4654 /* Strip off any multi-dimensional entries, then strip
4655 off the last array to get the component type. */
4656 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
4657 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
4658 gnu_param_type = TREE_TYPE (gnu_param_type);
4660 by_component_ptr = true;
4661 gnu_param_type = TREE_TYPE (gnu_param_type);
4664 gnu_param_type = build_qualified_type (gnu_param_type,
4665 (TYPE_QUALS (gnu_param_type)
4666 | TYPE_QUAL_CONST));
4668 gnu_param_type = build_pointer_type (gnu_param_type);
4671 /* Fat pointers are passed as thin pointers for foreign conventions. */
4672 else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
4674 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
4676 /* If we must pass or were requested to pass by reference, do so.
4677 If we were requested to pass by copy, do so.
4678 Otherwise, for foreign conventions, pass In Out or Out parameters
4679 or aggregates by reference. For COBOL and Fortran, pass all
4680 integer and FP types that way too. For Convention Ada, use
4681 the standard Ada default. */
4682 else if (must_pass_by_ref (gnu_param_type)
4683 || mech == By_Reference
4686 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
4688 && (Convention (gnat_subprog) == Convention_Fortran
4689 || Convention (gnat_subprog) == Convention_COBOL)
4690 && (INTEGRAL_TYPE_P (gnu_param_type)
4691 || FLOAT_TYPE_P (gnu_param_type)))
4693 && default_pass_by_ref (gnu_param_type)))))
4695 gnu_param_type = build_reference_type (gnu_param_type);
4699 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
4703 if (mech == By_Copy && (by_ref || by_component_ptr))
4704 post_error ("?cannot pass & by copy", gnat_param);
4706 /* If this is an Out parameter that isn't passed by reference and isn't
4707 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
4708 it will be a VAR_DECL created when we process the procedure, so just
4709 return its type. For the special parameter of a valued procedure,
4712 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
4713 Out parameters with discriminants or implicit initial values to be
4714 handled like In Out parameters. These type are normally built as
4715 aggregates, hence passed by reference, except for some packed arrays
4716 which end up encoded in special integer types.
4718 The exception we need to make is then for packed arrays of records
4719 with discriminants or implicit initial values. We have no light/easy
4720 way to check for the latter case, so we merely check for packed arrays
4721 of records. This may lead to useless copy-in operations, but in very
4722 rare cases only, as these would be exceptions in a set of already
4723 exceptional situations. */
4724 if (Ekind (gnat_param) == E_Out_Parameter
4727 || (mech != By_Descriptor
4728 && !POINTER_TYPE_P (gnu_param_type)
4729 && !AGGREGATE_TYPE_P (gnu_param_type)))
4730 && !(Is_Array_Type (Etype (gnat_param))
4731 && Is_Packed (Etype (gnat_param))
4732 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
4733 return gnu_param_type;
4735 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
4736 ro_param || by_ref || by_component_ptr);
4737 DECL_BY_REF_P (gnu_param) = by_ref;
4738 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
4739 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor);
4740 DECL_POINTS_TO_READONLY_P (gnu_param)
4741 = (ro_param && (by_ref || by_component_ptr));
4743 /* If no Mechanism was specified, indicate what we're using, then
4744 back-annotate it. */
4745 if (mech == Default)
4746 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
4748 Set_Mechanism (gnat_param, mech);
4752 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
4755 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
4757 while (Present (Corresponding_Discriminant (discr1)))
4758 discr1 = Corresponding_Discriminant (discr1);
4760 while (Present (Corresponding_Discriminant (discr2)))
4761 discr2 = Corresponding_Discriminant (discr2);
4764 Original_Record_Component (discr1) == Original_Record_Component (discr2);
4767 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
4768 a non-aliased component in the back-end sense. */
4771 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
4773 /* If the type below this is a multi-array type, then
4774 this does not have aliased components. */
4775 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
4776 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
4779 if (Has_Aliased_Components (gnat_type))
4782 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
4785 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4786 be elaborated at the point of its definition, but do nothing else. */
4789 elaborate_entity (Entity_Id gnat_entity)
4791 switch (Ekind (gnat_entity))
4793 case E_Signed_Integer_Subtype:
4794 case E_Modular_Integer_Subtype:
4795 case E_Enumeration_Subtype:
4796 case E_Ordinary_Fixed_Point_Subtype:
4797 case E_Decimal_Fixed_Point_Subtype:
4798 case E_Floating_Point_Subtype:
4800 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4801 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4803 /* ??? Tests for avoiding static constraint error expression
4804 is needed until the front stops generating bogus conversions
4805 on bounds of real types. */
4807 if (!Raises_Constraint_Error (gnat_lb))
4808 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4809 1, 0, Needs_Debug_Info (gnat_entity));
4810 if (!Raises_Constraint_Error (gnat_hb))
4811 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4812 1, 0, Needs_Debug_Info (gnat_entity));
4818 Node_Id full_definition = Declaration_Node (gnat_entity);
4819 Node_Id record_definition = Type_Definition (full_definition);
4821 /* If this is a record extension, go a level further to find the
4822 record definition. */
4823 if (Nkind (record_definition) == N_Derived_Type_Definition)
4824 record_definition = Record_Extension_Part (record_definition);
4828 case E_Record_Subtype:
4829 case E_Private_Subtype:
4830 case E_Limited_Private_Subtype:
4831 case E_Record_Subtype_With_Private:
4832 if (Is_Constrained (gnat_entity)
4833 && Has_Discriminants (Base_Type (gnat_entity))
4834 && Present (Discriminant_Constraint (gnat_entity)))
4836 Node_Id gnat_discriminant_expr;
4837 Entity_Id gnat_field;
4839 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4840 gnat_discriminant_expr
4841 = First_Elmt (Discriminant_Constraint (gnat_entity));
4842 Present (gnat_field);
4843 gnat_field = Next_Discriminant (gnat_field),
4844 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4845 /* ??? For now, ignore access discriminants. */
4846 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4847 elaborate_expression (Node (gnat_discriminant_expr),
4849 get_entity_name (gnat_field), 1, 0, 0);
4856 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4857 any entities on its entity chain similarly. */
4860 mark_out_of_scope (Entity_Id gnat_entity)
4862 Entity_Id gnat_sub_entity;
4863 unsigned int kind = Ekind (gnat_entity);
4865 /* If this has an entity list, process all in the list. */
4866 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4867 || IN (kind, Private_Kind)
4868 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4869 || kind == E_Function || kind == E_Generic_Function
4870 || kind == E_Generic_Package || kind == E_Generic_Procedure
4871 || kind == E_Loop || kind == E_Operator || kind == E_Package
4872 || kind == E_Package_Body || kind == E_Procedure
4873 || kind == E_Record_Type || kind == E_Record_Subtype
4874 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4875 for (gnat_sub_entity = First_Entity (gnat_entity);
4876 Present (gnat_sub_entity);
4877 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4878 if (Scope (gnat_sub_entity) == gnat_entity
4879 && gnat_sub_entity != gnat_entity)
4880 mark_out_of_scope (gnat_sub_entity);
4882 /* Now clear this if it has been defined, but only do so if it isn't
4883 a subprogram or parameter. We could refine this, but it isn't
4884 worth it. If this is statically allocated, it is supposed to
4885 hang around out of cope. */
4886 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4887 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4889 save_gnu_tree (gnat_entity, NULL_TREE, true);
4890 save_gnu_tree (gnat_entity, error_mark_node, true);
4894 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4895 is a multi-dimensional array type, do this recursively. */
4898 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4900 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4901 of a one-dimensional array, since the padding has the same alias set
4902 as the field type, but if it's a multi-dimensional array, we need to
4903 see the inner types. */
4904 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4905 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4906 || TYPE_IS_PADDING_P (gnu_old_type)))
4907 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4909 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4910 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4911 so we need to go down to what does. */
4912 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4914 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4916 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4917 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4918 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4919 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4921 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4922 record_component_aliases (gnu_new_type);
4925 /* Return a TREE_LIST describing the substitutions needed to reflect
4926 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4927 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4928 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4929 gives the tree for the discriminant and TREE_VALUES is the replacement
4930 value. They are in the form of operands to substitute_in_expr.
4931 DEFINITION is as in gnat_to_gnu_entity. */
4934 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4935 tree gnu_list, bool definition)
4937 Entity_Id gnat_discrim;
4941 gnat_type = Implementation_Base_Type (gnat_subtype);
4943 if (Has_Discriminants (gnat_type))
4944 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4945 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4946 Present (gnat_discrim);
4947 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4948 gnat_value = Next_Elmt (gnat_value))
4949 /* Ignore access discriminants. */
4950 if (!Is_Access_Type (Etype (Node (gnat_value))))
4951 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4952 elaborate_expression
4953 (Node (gnat_value), gnat_subtype,
4954 get_entity_name (gnat_discrim), definition,
4961 /* Return true if the size represented by GNU_SIZE can be handled by an
4962 allocation. If STATIC_P is true, consider only what can be done with a
4963 static allocation. */
4966 allocatable_size_p (tree gnu_size, bool static_p)
4968 HOST_WIDE_INT our_size;
4970 /* If this is not a static allocation, the only case we want to forbid
4971 is an overflowing size. That will be converted into a raise a
4974 return !(TREE_CODE (gnu_size) == INTEGER_CST
4975 && TREE_OVERFLOW (gnu_size));
4977 /* Otherwise, we need to deal with both variable sizes and constant
4978 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4979 since assemblers may not like very large sizes. */
4980 if (!host_integerp (gnu_size, 1))
4983 our_size = tree_low_cst (gnu_size, 1);
4984 return (int) our_size == our_size;
4987 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
4988 NAME, ARGS and ERROR_POINT. */
4991 prepend_one_attribute_to (struct attrib ** attr_list,
4992 enum attr_type attr_type,
4995 Node_Id attr_error_point)
4997 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4999 attr->type = attr_type;
5000 attr->name = attr_name;
5001 attr->args = attr_args;
5002 attr->error_point = attr_error_point;
5004 attr->next = *attr_list;
5008 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5011 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5015 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5016 gnat_temp = Next_Rep_Item (gnat_temp))
5017 if (Nkind (gnat_temp) == N_Pragma)
5019 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5020 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5021 enum attr_type etype;
5023 if (Present (gnat_assoc) && Present (First (gnat_assoc))
5024 && Present (Next (First (gnat_assoc)))
5025 && (Nkind (Expression (Next (First (gnat_assoc))))
5026 == N_String_Literal))
5028 gnu_arg0 = get_identifier (TREE_STRING_POINTER
5031 (First (gnat_assoc))))));
5032 if (Present (Next (Next (First (gnat_assoc))))
5033 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5034 == N_String_Literal))
5035 gnu_arg1 = get_identifier (TREE_STRING_POINTER
5039 (First (gnat_assoc)))))));
5042 switch (Get_Pragma_Id (Pragma_Identifier (Chars (gnat_temp))))
5044 case Pragma_Machine_Attribute:
5045 etype = ATTR_MACHINE_ATTRIBUTE;
5048 case Pragma_Linker_Alias:
5049 etype = ATTR_LINK_ALIAS;
5052 case Pragma_Linker_Section:
5053 etype = ATTR_LINK_SECTION;
5056 case Pragma_Linker_Constructor:
5057 etype = ATTR_LINK_CONSTRUCTOR;
5060 case Pragma_Linker_Destructor:
5061 etype = ATTR_LINK_DESTRUCTOR;
5064 case Pragma_Weak_External:
5065 etype = ATTR_WEAK_EXTERNAL;
5073 /* Prepend to the list now. Make a list of the argument we might
5074 have, as GCC expects it. */
5075 prepend_one_attribute_to
5078 (gnu_arg1 != NULL_TREE)
5079 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5080 Present (Next (First (gnat_assoc)))
5081 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5085 /* Get the unpadded version of a GNAT type. */
5088 get_unpadded_type (Entity_Id gnat_entity)
5090 tree type = gnat_to_gnu_type (gnat_entity);
5092 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5093 type = TREE_TYPE (TYPE_FIELDS (type));
5098 /* Called when we need to protect a variable object using a save_expr. */
5101 maybe_variable (tree gnu_operand)
5103 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5104 || TREE_CODE (gnu_operand) == SAVE_EXPR
5105 || TREE_CODE (gnu_operand) == NULL_EXPR)
5108 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5110 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5111 TREE_TYPE (gnu_operand),
5112 variable_size (TREE_OPERAND (gnu_operand, 0)));
5114 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5115 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5119 return variable_size (gnu_operand);
5122 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5123 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5124 return the GCC tree to use for that expression. GNU_NAME is the
5125 qualification to use if an external name is appropriate and DEFINITION is
5126 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
5127 we need a result. Otherwise, we are just elaborating this for
5128 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
5129 purposes even if it isn't needed for code generation. */
5132 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5133 tree gnu_name, bool definition, bool need_value,
5138 /* If we already elaborated this expression (e.g., it was involved
5139 in the definition of a private type), use the old value. */
5140 if (present_gnu_tree (gnat_expr))
5141 return get_gnu_tree (gnat_expr);
5143 /* If we don't need a value and this is static or a discriminant, we
5144 don't need to do anything. */
5145 else if (!need_value
5146 && (Is_OK_Static_Expression (gnat_expr)
5147 || (Nkind (gnat_expr) == N_Identifier
5148 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5151 /* Otherwise, convert this tree to its GCC equivalent. */
5153 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5154 gnu_name, definition, need_debug);
5156 /* Save the expression in case we try to elaborate this entity again. Since
5157 this is not a DECL, don't check it. Don't save if it's a discriminant. */
5158 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5159 save_gnu_tree (gnat_expr, gnu_expr, true);
5161 return need_value ? gnu_expr : error_mark_node;
5164 /* Similar, but take a GNU expression. */
5167 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5168 tree gnu_expr, tree gnu_name, bool definition,
5171 tree gnu_decl = NULL_TREE;
5172 /* Strip any conversions to see if the expression is a readonly variable.
5173 ??? This really should remain readonly, but we have to think about
5174 the typing of the tree here. */
5175 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
5176 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5179 /* In most cases, we won't see a naked FIELD_DECL here because a
5180 discriminant reference will have been replaced with a COMPONENT_REF
5181 when the type is being elaborated. However, there are some cases
5182 involving child types where we will. So convert it to a COMPONENT_REF
5183 here. We have to hope it will be at the highest level of the
5184 expression in these cases. */
5185 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5186 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5187 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5188 gnu_expr, NULL_TREE);
5190 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5191 that is a constant, make a variable that is initialized to contain the
5192 bound when the package containing the definition is elaborated. If
5193 this entity is defined at top level and a bound or discriminant value
5194 isn't a constant or a reference to a discriminant, replace the bound
5195 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5196 rely here on the fact that an expression cannot contain both the
5197 discriminant and some other variable. */
5199 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5200 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5201 && (TREE_READONLY (gnu_inner_expr)
5202 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5203 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5205 /* If this is a static expression or contains a discriminant, we don't
5206 need the variable for debugging (and can't elaborate anyway if a
5209 && (Is_OK_Static_Expression (gnat_expr)
5210 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5213 /* Now create the variable if we need it. */
5214 if (need_debug || (expr_variable && expr_global))
5216 = create_var_decl (create_concat_name (gnat_entity,
5217 IDENTIFIER_POINTER (gnu_name)),
5218 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5219 !need_debug, Is_Public (gnat_entity),
5220 !definition, false, NULL, gnat_entity);
5222 /* We only need to use this variable if we are in global context since GCC
5223 can do the right thing in the local case. */
5224 if (expr_global && expr_variable)
5226 else if (!expr_variable)
5229 return maybe_variable (gnu_expr);
5232 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5233 starting bit position so that it is aligned to ALIGN bits, and leaving at
5234 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5235 record is guaranteed to get. */
5238 make_aligning_type (tree type, unsigned int align, tree size,
5239 unsigned int base_align, int room)
5241 /* We will be crafting a record type with one field at a position set to be
5242 the next multiple of ALIGN past record'address + room bytes. We use a
5243 record placeholder to express record'address. */
5245 tree record_type = make_node (RECORD_TYPE);
5246 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5249 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5251 /* The diagram below summarizes the shape of what we manipulate:
5253 <--------- pos ---------->
5254 { +------------+-------------+-----------------+
5255 record =>{ |############| ... | field (type) |
5256 { +------------+-------------+-----------------+
5257 |<-- room -->|<- voffset ->|<---- size ----->|
5260 record_addr vblock_addr
5262 Every length is in sizetype bytes there, except "pos" which has to be
5263 set as a bit position in the GCC tree for the record. */
5265 tree room_st = size_int (room);
5266 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5267 tree voffset_st, pos, field;
5269 tree name = TYPE_NAME (type);
5271 if (TREE_CODE (name) == TYPE_DECL)
5272 name = DECL_NAME (name);
5274 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
5276 /* Compute VOFFSET and then POS. The next byte position multiple of some
5277 alignment after some address is obtained by "and"ing the alignment minus
5278 1 with the two's complement of the address. */
5280 voffset_st = size_binop (BIT_AND_EXPR,
5281 size_diffop (size_zero_node, vblock_addr_st),
5282 ssize_int ((align / BITS_PER_UNIT) - 1));
5284 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5286 pos = size_binop (MULT_EXPR,
5287 convert (bitsizetype,
5288 size_binop (PLUS_EXPR, room_st, voffset_st)),
5291 /* Craft the GCC record representation. We exceptionally do everything
5292 manually here because 1) our generic circuitry is not quite ready to
5293 handle the complex position/size expressions we are setting up, 2) we
5294 have a strong simplifying factor at hand: we know the maximum possible
5295 value of voffset, and 3) we have to set/reset at least the sizes in
5296 accordance with this maximum value anyway, as we need them to convey
5297 what should be "alloc"ated for this type.
5299 Use -1 as the 'addressable' indication for the field to prevent the
5300 creation of a bitfield. We don't need one, it would have damaging
5301 consequences on the alignment computation, and create_field_decl would
5302 make one without this special argument, for instance because of the
5303 complex position expression. */
5305 field = create_field_decl (get_identifier ("F"), type, record_type,
5307 TYPE_FIELDS (record_type) = field;
5309 TYPE_ALIGN (record_type) = base_align;
5310 TYPE_USER_ALIGN (record_type) = 1;
5312 TYPE_SIZE (record_type)
5313 = size_binop (PLUS_EXPR,
5314 size_binop (MULT_EXPR, convert (bitsizetype, size),
5316 bitsize_int (align + room * BITS_PER_UNIT));
5317 TYPE_SIZE_UNIT (record_type)
5318 = size_binop (PLUS_EXPR, size,
5319 size_int (room + align / BITS_PER_UNIT));
5321 TYPE_MODE (record_type) = BLKmode;
5323 copy_alias_set (record_type, type);
5327 /* Return the result of rounding T up to ALIGN. */
5329 static inline unsigned HOST_WIDE_INT
5330 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5338 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that
5339 is being used as the field type of a packed record if IN_RECORD is true,
5340 or as the component type of a packed array if IN_RECORD is false. See
5341 if we can rewrite it either as a type that has a non-BLKmode, which we
5342 can pack tighter, or as a smaller type with BLKmode. If so, return the
5343 new type. If not, return the original type. */
5346 make_packable_type (tree type, bool in_record)
5348 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5349 unsigned HOST_WIDE_INT new_size;
5350 tree new_type, old_field, field_list = NULL_TREE;
5352 /* No point in doing anything if the size is zero. */
5356 new_type = make_node (TREE_CODE (type));
5358 /* Copy the name and flags from the old type to that of the new. Note
5359 that we rely on the pointer equality created here for TYPE_NAME at
5360 the end of gnat_to_gnu. */
5361 TYPE_NAME (new_type) = TYPE_NAME (type);
5362 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5363 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5364 if (TREE_CODE (type) == RECORD_TYPE)
5365 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5367 /* If we are in a record and have a small size, set the alignment to
5368 try for an integral mode. Otherwise set it to try for a smaller
5369 type with BLKmode. */
5370 if (in_record && size <= MAX_FIXED_MODE_SIZE)
5372 TYPE_ALIGN (new_type) = ceil_alignment (size);
5373 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5377 unsigned HOST_WIDE_INT align;
5379 /* Do not try to shrink the size if the RM size is not constant. */
5380 if (TYPE_CONTAINS_TEMPLATE_P (type)
5381 || !host_integerp (TYPE_ADA_SIZE (type), 1))
5384 /* Round the RM size up to a unit boundary to get the minimal size
5385 for a BLKmode record. Give up if it's already the size. */
5386 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5387 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5388 if (new_size == size)
5391 align = new_size & -new_size;
5392 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5395 TYPE_USER_ALIGN (new_type) = 1;
5397 /* Now copy the fields, keeping the position and size as we don't
5398 want to propagate packedness downward. But make an exception
5399 for the last field in order to ditch the padding bits. */
5400 for (old_field = TYPE_FIELDS (type); old_field;
5401 old_field = TREE_CHAIN (old_field))
5403 tree new_field_type = TREE_TYPE (old_field);
5404 tree new_field, new_size;
5406 if (TYPE_MODE (new_field_type) == BLKmode
5407 && (TREE_CODE (new_field_type) == RECORD_TYPE
5408 || TREE_CODE (new_field_type) == UNION_TYPE
5409 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5410 && host_integerp (TYPE_SIZE (new_field_type), 1))
5411 new_field_type = make_packable_type (new_field_type, true);
5413 if (!TREE_CHAIN (old_field) && !TYPE_PACKED (type))
5414 new_size = rm_size (new_field_type);
5416 new_size = DECL_SIZE (old_field);
5418 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5419 new_type, TYPE_PACKED (type), new_size,
5420 bit_position (old_field),
5421 !DECL_NONADDRESSABLE_P (old_field));
5423 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5424 SET_DECL_ORIGINAL_FIELD
5425 (new_field, (DECL_ORIGINAL_FIELD (old_field)
5426 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5428 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5429 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5431 TREE_CHAIN (new_field) = field_list;
5432 field_list = new_field;
5435 finish_record_type (new_type, nreverse (field_list), 2, true);
5436 copy_alias_set (new_type, type);
5438 /* If this is a padding record, we never want to make the size smaller
5439 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
5440 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5441 || TREE_CODE (type) == QUAL_UNION_TYPE)
5443 TYPE_SIZE (new_type) = TYPE_SIZE (type);
5444 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5448 TYPE_SIZE (new_type) = bitsize_int (new_size);
5449 TYPE_SIZE_UNIT (new_type)
5450 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5453 if (!TYPE_CONTAINS_TEMPLATE_P (type))
5454 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5456 compute_record_mode (new_type);
5458 /* Try harder to get a packable type if necessary, for example
5459 in case the record itself contains a BLKmode field. */
5460 if (in_record && TYPE_MODE (new_type) == BLKmode)
5461 TYPE_MODE (new_type)
5462 = mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1);
5464 /* If neither the mode nor the size has shrunk, return the old type. */
5465 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5471 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5472 if needed. We have already verified that SIZE and TYPE are large enough.
5474 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5477 IS_USER_TYPE is true if we must be sure we complete the original type.
5479 DEFINITION is true if this type is being defined.
5481 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
5482 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
5486 maybe_pad_type (tree type, tree size, unsigned int align,
5487 Entity_Id gnat_entity, const char *name_trailer,
5488 bool is_user_type, bool definition, bool same_rm_size)
5490 tree orig_size = TYPE_SIZE (type);
5491 unsigned int orig_align = align;
5495 /* If TYPE is a padded type, see if it agrees with any size and alignment
5496 we were given. If so, return the original type. Otherwise, strip
5497 off the padding, since we will either be returning the inner type
5498 or repadding it. If no size or alignment is specified, use that of
5499 the original padded type. */
5500 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5503 || operand_equal_p (round_up (size,
5504 MAX (align, TYPE_ALIGN (type))),
5505 round_up (TYPE_SIZE (type),
5506 MAX (align, TYPE_ALIGN (type))),
5508 && (align == 0 || align == TYPE_ALIGN (type)))
5512 size = TYPE_SIZE (type);
5514 align = TYPE_ALIGN (type);
5516 type = TREE_TYPE (TYPE_FIELDS (type));
5517 orig_size = TYPE_SIZE (type);
5520 /* If the size is either not being changed or is being made smaller (which
5521 is not done here (and is only valid for bitfields anyway), show the size
5522 isn't changing. Likewise, clear the alignment if it isn't being
5523 changed. Then return if we aren't doing anything. */
5525 && (operand_equal_p (size, orig_size, 0)
5526 || (TREE_CODE (orig_size) == INTEGER_CST
5527 && tree_int_cst_lt (size, orig_size))))
5530 if (align == TYPE_ALIGN (type))
5533 if (align == 0 && !size)
5536 /* We used to modify the record in place in some cases, but that could
5537 generate incorrect debugging information. So make a new record
5539 record = make_node (RECORD_TYPE);
5541 if (Present (gnat_entity))
5542 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5544 /* If we were making a type, complete the original type and give it a
5547 create_type_decl (get_entity_name (gnat_entity), type,
5548 NULL, !Comes_From_Source (gnat_entity),
5550 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5551 && DECL_IGNORED_P (TYPE_NAME (type))),
5554 /* If we are changing the alignment and the input type is a record with
5555 BLKmode and a small constant size, try to make a form that has an
5556 integral mode. That might allow this record to have an integral mode,
5557 which will be much more efficient. There is no point in doing this if a
5558 size is specified unless it is also smaller than the maximum mode size
5559 and it is incorrect to do this if the size of the original type is not a
5560 multiple of the alignment. */
5562 && TREE_CODE (type) == RECORD_TYPE
5563 && TYPE_MODE (type) == BLKmode
5564 && TREE_CODE (orig_size) == INTEGER_CST
5565 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
5567 || (TREE_CODE (size) == INTEGER_CST
5568 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0))
5569 && value_factor_p (orig_size, align))
5570 type = make_packable_type (type, true);
5572 field = create_field_decl (get_identifier ("F"), type, record, 0,
5573 NULL_TREE, bitsize_zero_node, 1);
5575 DECL_INTERNAL_P (field) = 1;
5576 TYPE_SIZE (record) = size ? size : orig_size;
5577 TYPE_SIZE_UNIT (record)
5578 = (size ? convert (sizetype,
5579 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
5580 : TYPE_SIZE_UNIT (type));
5582 TYPE_ALIGN (record) = align;
5584 TYPE_USER_ALIGN (record) = align;
5586 TYPE_IS_PADDING_P (record) = 1;
5587 TYPE_VOLATILE (record)
5588 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5589 /* Do not finalize it until after the auxiliary record is built. */
5590 finish_record_type (record, field, 1, true);
5592 /* Keep the RM_Size of the padded record as that of the old record
5594 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
5596 /* Unless debugging information isn't being written for the input type,
5597 write a record that shows what we are a subtype of and also make a
5598 variable that indicates our size, if variable. */
5599 if (TYPE_NAME (record)
5600 && AGGREGATE_TYPE_P (type)
5601 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
5602 || !DECL_IGNORED_P (TYPE_NAME (type))))
5604 tree marker = make_node (RECORD_TYPE);
5605 tree name = TYPE_NAME (record);
5606 tree orig_name = TYPE_NAME (type);
5608 if (TREE_CODE (name) == TYPE_DECL)
5609 name = DECL_NAME (name);
5611 if (TREE_CODE (orig_name) == TYPE_DECL)
5612 orig_name = DECL_NAME (orig_name);
5614 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5615 finish_record_type (marker,
5616 create_field_decl (orig_name, integer_type_node,
5617 marker, 0, NULL_TREE, NULL_TREE,
5621 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5622 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5623 bitsizetype, TYPE_SIZE (record), false, false, false,
5624 false, NULL, gnat_entity);
5627 rest_of_record_type_compilation (record);
5629 /* If the size was widened explicitly, maybe give a warning. Take the
5630 original size as the maximum size of the input if there was an
5631 unconstrained record involved and round it up to the specified alignment,
5632 if one was specified. */
5633 if (CONTAINS_PLACEHOLDER_P (orig_size))
5634 orig_size = max_size (orig_size, true);
5637 orig_size = round_up (orig_size, align);
5639 if (size && Present (gnat_entity)
5640 && !operand_equal_p (size, orig_size, 0)
5641 && !(TREE_CODE (size) == INTEGER_CST
5642 && TREE_CODE (orig_size) == INTEGER_CST
5643 && tree_int_cst_lt (size, orig_size)))
5645 Node_Id gnat_error_node = Empty;
5647 if (Is_Packed_Array_Type (gnat_entity))
5648 gnat_entity = Original_Array_Type (gnat_entity);
5650 if ((Ekind (gnat_entity) == E_Component
5651 || Ekind (gnat_entity) == E_Discriminant)
5652 && Present (Component_Clause (gnat_entity)))
5653 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5654 else if (Present (Size_Clause (gnat_entity)))
5655 gnat_error_node = Expression (Size_Clause (gnat_entity));
5657 /* Generate message only for entities that come from source, since
5658 if we have an entity created by expansion, the message will be
5659 generated for some other corresponding source entity. */
5660 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5661 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5663 size_diffop (size, orig_size));
5665 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5666 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5667 gnat_entity, gnat_entity,
5668 size_diffop (size, orig_size));
5674 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5675 the value passed against the list of choices. */
5678 choices_to_gnu (tree operand, Node_Id choices)
5682 tree result = integer_zero_node;
5683 tree this_test, low = 0, high = 0, single = 0;
5685 for (choice = First (choices); Present (choice); choice = Next (choice))
5687 switch (Nkind (choice))
5690 low = gnat_to_gnu (Low_Bound (choice));
5691 high = gnat_to_gnu (High_Bound (choice));
5693 /* There's no good type to use here, so we might as well use
5694 integer_type_node. */
5696 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5697 build_binary_op (GE_EXPR, integer_type_node,
5699 build_binary_op (LE_EXPR, integer_type_node,
5704 case N_Subtype_Indication:
5705 gnat_temp = Range_Expression (Constraint (choice));
5706 low = gnat_to_gnu (Low_Bound (gnat_temp));
5707 high = gnat_to_gnu (High_Bound (gnat_temp));
5710 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5711 build_binary_op (GE_EXPR, integer_type_node,
5713 build_binary_op (LE_EXPR, integer_type_node,
5718 case N_Expanded_Name:
5719 /* This represents either a subtype range, an enumeration
5720 literal, or a constant Ekind says which. If an enumeration
5721 literal or constant, fall through to the next case. */
5722 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5723 && Ekind (Entity (choice)) != E_Constant)
5725 tree type = gnat_to_gnu_type (Entity (choice));
5727 low = TYPE_MIN_VALUE (type);
5728 high = TYPE_MAX_VALUE (type);
5731 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5732 build_binary_op (GE_EXPR, integer_type_node,
5734 build_binary_op (LE_EXPR, integer_type_node,
5738 /* ... fall through ... */
5739 case N_Character_Literal:
5740 case N_Integer_Literal:
5741 single = gnat_to_gnu (choice);
5742 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5746 case N_Others_Choice:
5747 this_test = integer_one_node;
5754 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5761 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
5762 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
5765 adjust_packed (tree field_type, tree record_type, int packed)
5767 /* If the field contains an item of variable size, we cannot pack it
5768 because we cannot create temporaries of non-fixed size. */
5769 if (is_variable_size (field_type))
5772 /* If the alignment of the record is specified and the field type
5773 is over-aligned, request Storage_Unit alignment for the field. */
5776 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
5785 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5786 placed in GNU_RECORD_TYPE.
5788 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
5789 record has Component_Alignment of Storage_Unit, -2 if the enclosing
5790 record has a specified alignment.
5792 DEFINITION is true if this field is for a record being defined. */
5795 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5798 tree gnu_field_id = get_entity_name (gnat_field);
5799 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5800 tree gnu_field, gnu_size, gnu_pos;
5801 bool needs_strict_alignment
5802 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5803 || Treat_As_Volatile (gnat_field));
5805 /* If this field requires strict alignment, we cannot pack it because
5806 it would very likely be under-aligned in the record. */
5807 if (needs_strict_alignment)
5810 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
5812 /* If a size is specified, use it. Otherwise, if the record type is packed,
5813 use the official RM size. See "Handling of Type'Size Values" in Einfo
5814 for further details. */
5815 if (Known_Static_Esize (gnat_field))
5816 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5817 gnat_field, FIELD_DECL, false, true);
5818 else if (packed == 1)
5819 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5820 gnat_field, FIELD_DECL, false, true);
5822 gnu_size = NULL_TREE;
5824 /* If we have a specified size that's smaller than that of the field type,
5825 or a position is specified, and the field type is also a record that's
5826 BLKmode, see if we can get either an integral mode form of the type or
5827 a smaller BLKmode form. If we can, show a size was specified for the
5828 field if there wasn't one already, so we know to make this a bitfield
5829 and avoid making things wider.
5831 Doing this is first useful if the record is packed because we may then
5832 place the field at a non-byte-aligned position and so achieve tighter
5835 This is in addition *required* if the field shares a byte with another
5836 field and the front-end lets the back-end handle the references, because
5837 GCC does not handle BLKmode bitfields properly.
5839 We avoid the transformation if it is not required or potentially useful,
5840 as it might entail an increase of the field's alignment and have ripple
5841 effects on the outer record type. A typical case is a field known to be
5842 byte aligned and not to share a byte with another field.
5844 Besides, we don't even look the possibility of a transformation in cases
5845 known to be in error already, for instance when an invalid size results
5846 from a component clause. */
5848 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5849 && TYPE_MODE (gnu_field_type) == BLKmode
5850 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5853 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
5854 || Present (Component_Clause (gnat_field))))))
5856 /* See what the alternate type and size would be. */
5857 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
5859 bool has_byte_aligned_clause
5860 = Present (Component_Clause (gnat_field))
5861 && (UI_To_Int (Component_Bit_Offset (gnat_field))
5862 % BITS_PER_UNIT == 0);
5864 /* Compute whether we should avoid the substitution. */
5866 /* There is no point substituting if there is no change... */
5867 = (gnu_packable_type == gnu_field_type)
5868 /* ... nor when the field is known to be byte aligned and not to
5869 share a byte with another field. */
5870 || (has_byte_aligned_clause
5871 && value_factor_p (gnu_size, BITS_PER_UNIT))
5872 /* The size of an aliased field must be an exact multiple of the
5873 type's alignment, which the substitution might increase. Reject
5874 substitutions that would so invalidate a component clause when the
5875 specified position is byte aligned, as the change would have no
5876 real benefit from the packing standpoint anyway. */
5877 || (Is_Aliased (gnat_field)
5878 && has_byte_aligned_clause
5879 && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
5881 /* Substitute unless told otherwise. */
5884 gnu_field_type = gnu_packable_type;
5887 gnu_size = rm_size (gnu_field_type);
5891 /* If we are packing the record and the field is BLKmode, round the
5892 size up to a byte boundary. */
5893 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5894 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5896 if (Present (Component_Clause (gnat_field)))
5898 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5899 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5900 gnat_field, FIELD_DECL, false, true);
5902 /* Ensure the position does not overlap with the parent subtype,
5904 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5907 = gnat_to_gnu_type (Parent_Subtype
5908 (Underlying_Type (Scope (gnat_field))));
5910 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5911 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5914 ("offset of& must be beyond parent{, minimum allowed is ^}",
5915 First_Bit (Component_Clause (gnat_field)), gnat_field,
5916 TYPE_SIZE_UNIT (gnu_parent));
5920 /* If this field needs strict alignment, ensure the record is
5921 sufficiently aligned and that that position and size are
5922 consistent with the alignment. */
5923 if (needs_strict_alignment)
5925 TYPE_ALIGN (gnu_record_type)
5926 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5929 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5931 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5933 ("atomic field& must be natural size of type{ (^)}",
5934 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5935 TYPE_SIZE (gnu_field_type));
5937 else if (Is_Aliased (gnat_field))
5939 ("size of aliased field& must be ^ bits",
5940 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5941 TYPE_SIZE (gnu_field_type));
5943 else if (Strict_Alignment (Etype (gnat_field)))
5945 ("size of & with aliased or tagged components not ^ bits",
5946 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5947 TYPE_SIZE (gnu_field_type));
5949 gnu_size = NULL_TREE;
5952 if (!integer_zerop (size_binop
5953 (TRUNC_MOD_EXPR, gnu_pos,
5954 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5956 if (Is_Aliased (gnat_field))
5958 ("position of aliased field& must be multiple of ^ bits",
5959 First_Bit (Component_Clause (gnat_field)), gnat_field,
5960 TYPE_ALIGN (gnu_field_type));
5962 else if (Treat_As_Volatile (gnat_field))
5964 ("position of volatile field& must be multiple of ^ bits",
5965 First_Bit (Component_Clause (gnat_field)), gnat_field,
5966 TYPE_ALIGN (gnu_field_type));
5968 else if (Strict_Alignment (Etype (gnat_field)))
5970 ("position of & with aliased or tagged components not multiple of ^ bits",
5971 First_Bit (Component_Clause (gnat_field)), gnat_field,
5972 TYPE_ALIGN (gnu_field_type));
5977 gnu_pos = NULL_TREE;
5981 if (Is_Atomic (gnat_field))
5982 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5985 /* If the record has rep clauses and this is the tag field, make a rep
5986 clause for it as well. */
5987 else if (Has_Specified_Layout (Scope (gnat_field))
5988 && Chars (gnat_field) == Name_uTag)
5990 gnu_pos = bitsize_zero_node;
5991 gnu_size = TYPE_SIZE (gnu_field_type);
5995 gnu_pos = NULL_TREE;
5997 /* We need to make the size the maximum for the type if it is
5998 self-referential and an unconstrained type. In that case, we can't
5999 pack the field since we can't make a copy to align it. */
6000 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6002 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6003 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6005 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6009 /* If a size is specified, adjust the field's type to it. */
6012 /* If the field's type is justified modular, we would need to remove
6013 the wrapper to (better) meet the layout requirements. However we
6014 can do so only if the field is not aliased to preserve the unique
6015 layout and if the prescribed size is not greater than that of the
6016 packed array to preserve the justification. */
6017 if (!needs_strict_alignment
6018 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6019 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6020 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6022 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6025 = make_type_from_size (gnu_field_type, gnu_size,
6026 Has_Biased_Representation (gnat_field));
6027 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6028 "PAD", false, definition, true);
6031 /* Otherwise (or if there was an error), don't specify a position. */
6033 gnu_pos = NULL_TREE;
6035 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6036 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6038 /* Now create the decl for the field. */
6039 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6040 packed, gnu_size, gnu_pos,
6041 Is_Aliased (gnat_field));
6042 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6043 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6045 if (Ekind (gnat_field) == E_Discriminant)
6046 DECL_DISCRIMINANT_NUMBER (gnu_field)
6047 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6052 /* Return true if TYPE is a type with variable size, a padding type with a
6053 field of variable size or is a record that has a field such a field. */
6056 is_variable_size (tree type)
6060 /* We need not be concerned about this at all if we don't have
6061 strict alignment. */
6062 if (!STRICT_ALIGNMENT)
6064 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
6066 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
6067 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6069 else if (TREE_CODE (type) != RECORD_TYPE
6070 && TREE_CODE (type) != UNION_TYPE
6071 && TREE_CODE (type) != QUAL_UNION_TYPE)
6074 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6075 if (is_variable_size (TREE_TYPE (field)))
6081 /* qsort comparer for the bit positions of two record components. */
6084 compare_field_bitpos (const PTR rt1, const PTR rt2)
6086 const_tree const field1 = * (const_tree const *) rt1;
6087 const_tree const field2 = * (const_tree const *) rt2;
6089 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6091 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6094 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6095 of GCC trees for fields that are in the record and have already been
6096 processed. When called from gnat_to_gnu_entity during the processing of a
6097 record type definition, the GCC nodes for the discriminants will be on
6098 the chain. The other calls to this function are recursive calls from
6099 itself for the Component_List of a variant and the chain is empty.
6101 PACKED is 1 if this is for a packed record, -1 if this is for a record
6102 with Component_Alignment of Storage_Unit, -2 if this is for a record
6103 with a specified alignment.
6105 DEFINITION is true if we are defining this record.
6107 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6108 with a rep clause is to be added. If it is nonzero, that is all that
6109 should be done with such fields.
6111 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6112 laying out the record. This means the alignment only serves to force fields
6113 to be bitfields, but not require the record to be that aligned. This is
6116 ALL_REP, if true, means a rep clause was found for all the fields. This
6117 simplifies the logic since we know we're not in the mixed case.
6119 DO_NOT_FINALIZE, if true, means that the record type is expected to be
6120 modified afterwards so it will not be sent to the back-end for finalization.
6122 UNCHECKED_UNION, if true, means that we are building a type for a record
6123 with a Pragma Unchecked_Union.
6125 The processing of the component list fills in the chain with all of the
6126 fields of the record and then the record type is finished. */
6129 components_to_record (tree gnu_record_type, Node_Id component_list,
6130 tree gnu_field_list, int packed, bool definition,
6131 tree *p_gnu_rep_list, bool cancel_alignment,
6132 bool all_rep, bool do_not_finalize, bool unchecked_union)
6134 Node_Id component_decl;
6135 Entity_Id gnat_field;
6136 Node_Id variant_part;
6137 tree gnu_our_rep_list = NULL_TREE;
6138 tree gnu_field, gnu_last;
6139 bool layout_with_rep = false;
6140 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6142 /* For each variable within each component declaration create a GCC field
6143 and add it to the list, skipping any pragmas in the list. */
6144 if (Present (Component_Items (component_list)))
6145 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6146 Present (component_decl);
6147 component_decl = Next_Non_Pragma (component_decl))
6149 gnat_field = Defining_Entity (component_decl);
6151 if (Chars (gnat_field) == Name_uParent)
6152 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6155 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6156 packed, definition);
6158 /* If this is the _Tag field, put it before any discriminants,
6159 instead of after them as is the case for all other fields.
6160 Ignore field of void type if only annotating. */
6161 if (Chars (gnat_field) == Name_uTag)
6162 gnu_field_list = chainon (gnu_field_list, gnu_field);
6165 TREE_CHAIN (gnu_field) = gnu_field_list;
6166 gnu_field_list = gnu_field;
6170 save_gnu_tree (gnat_field, gnu_field, false);
6173 /* At the end of the component list there may be a variant part. */
6174 variant_part = Variant_Part (component_list);
6176 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6177 mutually exclusive and should go in the same memory. To do this we need
6178 to treat each variant as a record whose elements are created from the
6179 component list for the variant. So here we create the records from the
6180 lists for the variants and put them all into the QUAL_UNION_TYPE.
6181 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6182 use GNU_RECORD_TYPE if there are no fields so far. */
6183 if (Present (variant_part))
6185 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
6187 tree gnu_name = TYPE_NAME (gnu_record_type);
6189 = concat_id_with_name (get_identifier (Get_Name_String
6190 (Chars (Name (variant_part)))),
6192 tree gnu_union_type;
6193 tree gnu_union_name;
6194 tree gnu_union_field;
6195 tree gnu_variant_list = NULL_TREE;
6197 if (TREE_CODE (gnu_name) == TYPE_DECL)
6198 gnu_name = DECL_NAME (gnu_name);
6200 gnu_union_name = concat_id_with_name (gnu_name,
6201 IDENTIFIER_POINTER (gnu_var_name));
6203 /* Reuse an enclosing union if all fields are in the variant part
6204 and there is no representation clause on the record, to match
6205 the layout of C unions. There is an associated check below. */
6207 && TREE_CODE (gnu_record_type) == UNION_TYPE
6208 && !TYPE_PACKED (gnu_record_type))
6209 gnu_union_type = gnu_record_type;
6213 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6215 TYPE_NAME (gnu_union_type) = gnu_union_name;
6216 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6219 for (variant = First_Non_Pragma (Variants (variant_part));
6221 variant = Next_Non_Pragma (variant))
6223 tree gnu_variant_type = make_node (RECORD_TYPE);
6224 tree gnu_inner_name;
6227 Get_Variant_Encoding (variant);
6228 gnu_inner_name = get_identifier (Name_Buffer);
6229 TYPE_NAME (gnu_variant_type)
6230 = concat_id_with_name (gnu_union_name,
6231 IDENTIFIER_POINTER (gnu_inner_name));
6233 /* Set the alignment of the inner type in case we need to make
6234 inner objects into bitfields, but then clear it out
6235 so the record actually gets only the alignment required. */
6236 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6237 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6239 /* Similarly, if the outer record has a size specified and all fields
6240 have record rep clauses, we can propagate the size into the
6242 if (all_rep_and_size)
6244 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6245 TYPE_SIZE_UNIT (gnu_variant_type)
6246 = TYPE_SIZE_UNIT (gnu_record_type);
6249 /* Create the record type for the variant. Note that we defer
6250 finalizing it until after we are sure to actually use it. */
6251 components_to_record (gnu_variant_type, Component_List (variant),
6252 NULL_TREE, packed, definition,
6253 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6254 true, unchecked_union);
6256 gnu_qual = choices_to_gnu (gnu_discriminant,
6257 Discrete_Choices (variant));
6259 Set_Present_Expr (variant, annotate_value (gnu_qual));
6261 /* If this is an Unchecked_Union and we have exactly one field,
6262 use this field directly to match the layout of C unions. */
6264 && TYPE_FIELDS (gnu_variant_type)
6265 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6266 gnu_field = TYPE_FIELDS (gnu_variant_type);
6269 /* Deal with packedness like in gnat_to_gnu_field. */
6271 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6273 /* Finalize the record type now. We used to throw away
6274 empty records but we no longer do that because we need
6275 them to generate complete debug info for the variant;
6276 otherwise, the union type definition will be lacking
6277 the fields associated with these empty variants. */
6278 rest_of_record_type_compilation (gnu_variant_type);
6280 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6281 gnu_union_type, field_packed,
6283 ? TYPE_SIZE (gnu_variant_type)
6286 ? bitsize_zero_node : 0),
6289 DECL_INTERNAL_P (gnu_field) = 1;
6291 if (!unchecked_union)
6292 DECL_QUALIFIER (gnu_field) = gnu_qual;
6295 TREE_CHAIN (gnu_field) = gnu_variant_list;
6296 gnu_variant_list = gnu_field;
6299 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
6300 if (gnu_variant_list)
6302 if (all_rep_and_size)
6304 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6305 TYPE_SIZE_UNIT (gnu_union_type)
6306 = TYPE_SIZE_UNIT (gnu_record_type);
6309 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6310 all_rep_and_size ? 1 : 0, false);
6312 /* If GNU_UNION_TYPE is our record type, it means we must have an
6313 Unchecked_Union with no fields. Verify that and, if so, just
6315 if (gnu_union_type == gnu_record_type)
6317 gcc_assert (unchecked_union
6319 && !gnu_our_rep_list);
6324 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6326 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6327 all_rep ? bitsize_zero_node : 0, 0);
6329 DECL_INTERNAL_P (gnu_union_field) = 1;
6330 TREE_CHAIN (gnu_union_field) = gnu_field_list;
6331 gnu_field_list = gnu_union_field;
6335 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
6336 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
6337 in a separate pass since we want to handle the discriminants but can't
6338 play with them until we've used them in debugging data above.
6340 ??? Note: if we then reorder them, debugging information will be wrong,
6341 but there's nothing that can be done about this at the moment. */
6342 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6344 if (DECL_FIELD_OFFSET (gnu_field))
6346 tree gnu_next = TREE_CHAIN (gnu_field);
6349 gnu_field_list = gnu_next;
6351 TREE_CHAIN (gnu_last) = gnu_next;
6353 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6354 gnu_our_rep_list = gnu_field;
6355 gnu_field = gnu_next;
6359 gnu_last = gnu_field;
6360 gnu_field = TREE_CHAIN (gnu_field);
6364 /* If we have any items in our rep'ed field list, it is not the case that all
6365 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6366 set it and ignore the items. */
6367 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6368 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6369 else if (gnu_our_rep_list)
6371 /* Otherwise, sort the fields by bit position and put them into their
6372 own record if we have any fields without rep clauses. */
6374 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6375 int len = list_length (gnu_our_rep_list);
6376 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6379 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6380 gnu_field = TREE_CHAIN (gnu_field), i++)
6381 gnu_arr[i] = gnu_field;
6383 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6385 /* Put the fields in the list in order of increasing position, which
6386 means we start from the end. */
6387 gnu_our_rep_list = NULL_TREE;
6388 for (i = len - 1; i >= 0; i--)
6390 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6391 gnu_our_rep_list = gnu_arr[i];
6392 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6397 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6398 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6399 gnu_record_type, 0, 0, 0, 1);
6400 DECL_INTERNAL_P (gnu_field) = 1;
6401 gnu_field_list = chainon (gnu_field_list, gnu_field);
6405 layout_with_rep = true;
6406 gnu_field_list = nreverse (gnu_our_rep_list);
6410 if (cancel_alignment)
6411 TYPE_ALIGN (gnu_record_type) = 0;
6413 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6414 layout_with_rep ? 1 : 0, do_not_finalize);
6417 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6418 placed into an Esize, Component_Bit_Offset, or Component_Size value
6419 in the GNAT tree. */
6422 annotate_value (tree gnu_size)
6424 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6426 Node_Ref_Or_Val ops[3], ret;
6429 struct tree_int_map **h = NULL;
6431 /* See if we've already saved the value for this node. */
6432 if (EXPR_P (gnu_size))
6434 struct tree_int_map in;
6435 if (!annotate_value_cache)
6436 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6437 tree_int_map_eq, 0);
6438 in.base.from = gnu_size;
6439 h = (struct tree_int_map **)
6440 htab_find_slot (annotate_value_cache, &in, INSERT);
6443 return (Node_Ref_Or_Val) (*h)->to;
6446 /* If we do not return inside this switch, TCODE will be set to the
6447 code to use for a Create_Node operand and LEN (set above) will be
6448 the number of recursive calls for us to make. */
6450 switch (TREE_CODE (gnu_size))
6453 if (TREE_OVERFLOW (gnu_size))
6456 /* This may have come from a conversion from some smaller type,
6457 so ensure this is in bitsizetype. */
6458 gnu_size = convert (bitsizetype, gnu_size);
6460 /* For negative values, use NEGATE_EXPR of the supplied value. */
6461 if (tree_int_cst_sgn (gnu_size) < 0)
6463 /* The ridiculous code below is to handle the case of the largest
6464 negative integer. */
6465 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6466 bool adjust = false;
6469 if (TREE_OVERFLOW (negative_size))
6472 = size_binop (MINUS_EXPR, bitsize_zero_node,
6473 size_binop (PLUS_EXPR, gnu_size,
6478 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
6480 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
6482 return annotate_value (temp);
6485 if (!host_integerp (gnu_size, 1))
6488 size = tree_low_cst (gnu_size, 1);
6490 /* This peculiar test is to make sure that the size fits in an int
6491 on machines where HOST_WIDE_INT is not "int". */
6492 if (tree_low_cst (gnu_size, 1) == size)
6493 return UI_From_Int (size);
6498 /* The only case we handle here is a simple discriminant reference. */
6499 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
6500 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
6501 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
6502 return Create_Node (Discrim_Val,
6503 annotate_value (DECL_DISCRIMINANT_NUMBER
6504 (TREE_OPERAND (gnu_size, 1))),
6509 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
6510 return annotate_value (TREE_OPERAND (gnu_size, 0));
6512 /* Now just list the operations we handle. */
6513 case COND_EXPR: tcode = Cond_Expr; break;
6514 case PLUS_EXPR: tcode = Plus_Expr; break;
6515 case MINUS_EXPR: tcode = Minus_Expr; break;
6516 case MULT_EXPR: tcode = Mult_Expr; break;
6517 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
6518 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
6519 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
6520 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
6521 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
6522 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
6523 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
6524 case NEGATE_EXPR: tcode = Negate_Expr; break;
6525 case MIN_EXPR: tcode = Min_Expr; break;
6526 case MAX_EXPR: tcode = Max_Expr; break;
6527 case ABS_EXPR: tcode = Abs_Expr; break;
6528 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
6529 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
6530 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
6531 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
6532 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
6533 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
6534 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
6535 case LT_EXPR: tcode = Lt_Expr; break;
6536 case LE_EXPR: tcode = Le_Expr; break;
6537 case GT_EXPR: tcode = Gt_Expr; break;
6538 case GE_EXPR: tcode = Ge_Expr; break;
6539 case EQ_EXPR: tcode = Eq_Expr; break;
6540 case NE_EXPR: tcode = Ne_Expr; break;
6546 /* Now get each of the operands that's relevant for this code. If any
6547 cannot be expressed as a repinfo node, say we can't. */
6548 for (i = 0; i < 3; i++)
6551 for (i = 0; i < len; i++)
6553 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6554 if (ops[i] == No_Uint)
6558 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6560 /* Save the result in the cache. */
6563 *h = ggc_alloc (sizeof (struct tree_int_map));
6564 (*h)->base.from = gnu_size;
6571 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6572 GCC type, set Component_Bit_Offset and Esize to the position and size
6576 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6580 Entity_Id gnat_field;
6582 /* We operate by first making a list of all fields and their positions
6583 (we can get the sizes easily at any time) by a recursive call
6584 and then update all the sizes into the tree. */
6585 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6586 size_zero_node, bitsize_zero_node,
6589 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6590 gnat_field = Next_Entity (gnat_field))
6591 if ((Ekind (gnat_field) == E_Component
6592 || (Ekind (gnat_field) == E_Discriminant
6593 && !Is_Unchecked_Union (Scope (gnat_field)))))
6595 tree parent_offset = bitsize_zero_node;
6597 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6602 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6604 /* In this mode the tag and parent components have not been
6605 generated, so we add the appropriate offset to each
6606 component. For a component appearing in the current
6607 extension, the offset is the size of the parent. */
6608 if (Is_Derived_Type (gnat_entity)
6609 && Original_Record_Component (gnat_field) == gnat_field)
6611 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6614 parent_offset = bitsize_int (POINTER_SIZE);
6617 Set_Component_Bit_Offset
6620 (size_binop (PLUS_EXPR,
6621 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6622 TREE_VALUE (TREE_VALUE
6623 (TREE_VALUE (gnu_entry)))),
6626 Set_Esize (gnat_field,
6627 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6629 else if (Is_Tagged_Type (gnat_entity)
6630 && Is_Derived_Type (gnat_entity))
6632 /* If there is no gnu_entry, this is an inherited component whose
6633 position is the same as in the parent type. */
6634 Set_Component_Bit_Offset
6636 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6637 Set_Esize (gnat_field,
6638 Esize (Original_Record_Component (gnat_field)));
6643 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6644 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6645 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6646 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6647 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6648 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6652 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6653 tree gnu_bitpos, unsigned int offset_align)
6656 tree gnu_result = gnu_list;
6658 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6659 gnu_field = TREE_CHAIN (gnu_field))
6661 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6662 DECL_FIELD_BIT_OFFSET (gnu_field));
6663 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6664 DECL_FIELD_OFFSET (gnu_field));
6665 unsigned int our_offset_align
6666 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6669 = tree_cons (gnu_field,
6670 tree_cons (gnu_our_offset,
6671 tree_cons (size_int (our_offset_align),
6672 gnu_our_bitpos, NULL_TREE),
6676 if (DECL_INTERNAL_P (gnu_field))
6678 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6679 gnu_our_offset, gnu_our_bitpos,
6686 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6687 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6688 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6689 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6690 for the size of a field. COMPONENT_P is true if we are being called
6691 to process the Component_Size of GNAT_OBJECT. This is used for error
6692 message handling and to indicate to use the object size of GNU_TYPE.
6693 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6694 it means that a size of zero should be treated as an unspecified size. */
6697 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6698 enum tree_code kind, bool component_p, bool zero_ok)
6700 Node_Id gnat_error_node;
6701 tree type_size, size;
6703 if (kind == VAR_DECL
6704 /* If a type needs strict alignment, a component of this type in
6705 a packed record cannot be packed and thus uses the type size. */
6706 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
6707 type_size = TYPE_SIZE (gnu_type);
6709 type_size = rm_size (gnu_type);
6711 /* Find the node to use for errors. */
6712 if ((Ekind (gnat_object) == E_Component
6713 || Ekind (gnat_object) == E_Discriminant)
6714 && Present (Component_Clause (gnat_object)))
6715 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6716 else if (Present (Size_Clause (gnat_object)))
6717 gnat_error_node = Expression (Size_Clause (gnat_object));
6719 gnat_error_node = gnat_object;
6721 /* Return 0 if no size was specified, either because Esize was not Present or
6722 the specified size was zero. */
6723 if (No (uint_size) || uint_size == No_Uint)
6726 /* Get the size as a tree. Give an error if a size was specified, but cannot
6727 be represented as in sizetype. */
6728 size = UI_To_gnu (uint_size, bitsizetype);
6729 if (TREE_OVERFLOW (size))
6731 post_error_ne (component_p ? "component size of & is too large"
6732 : "size of & is too large",
6733 gnat_error_node, gnat_object);
6737 /* Ignore a negative size since that corresponds to our back-annotation.
6738 Also ignore a zero size unless a size clause exists. */
6739 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6742 /* The size of objects is always a multiple of a byte. */
6743 if (kind == VAR_DECL
6744 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6747 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6748 gnat_error_node, gnat_object);
6750 post_error_ne ("size for& is not a multiple of Storage_Unit",
6751 gnat_error_node, gnat_object);
6755 /* If this is an integral type or a packed array type, the front-end has
6756 verified the size, so we need not do it here (which would entail
6757 checking against the bounds). However, if this is an aliased object, it
6758 may not be smaller than the type of the object. */
6759 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6760 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6763 /* If the object is a record that contains a template, add the size of
6764 the template to the specified size. */
6765 if (TREE_CODE (gnu_type) == RECORD_TYPE
6766 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6767 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6769 /* Modify the size of the type to be that of the maximum size if it has a
6770 discriminant or the size of a thin pointer if this is a fat pointer. */
6771 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6772 type_size = max_size (type_size, true);
6773 else if (TYPE_FAT_POINTER_P (gnu_type))
6774 type_size = bitsize_int (POINTER_SIZE);
6776 /* If this is an access type, the minimum size is that given by the smallest
6777 integral mode that's valid for pointers. */
6778 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6780 enum machine_mode p_mode;
6782 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6783 !targetm.valid_pointer_mode (p_mode);
6784 p_mode = GET_MODE_WIDER_MODE (p_mode))
6787 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6790 /* If the size of the object is a constant, the new size must not be
6792 if (TREE_CODE (type_size) != INTEGER_CST
6793 || TREE_OVERFLOW (type_size)
6794 || tree_int_cst_lt (size, type_size))
6798 ("component size for& too small{, minimum allowed is ^}",
6799 gnat_error_node, gnat_object, type_size);
6801 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6802 gnat_error_node, gnat_object, type_size);
6804 if (kind == VAR_DECL && !component_p
6805 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6806 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6807 post_error_ne_tree_2
6808 ("\\size of ^ is not a multiple of alignment (^ bits)",
6809 gnat_error_node, gnat_object, rm_size (gnu_type),
6810 TYPE_ALIGN (gnu_type));
6812 else if (INTEGRAL_TYPE_P (gnu_type))
6813 post_error_ne ("\\size would be legal if & were not aliased!",
6814 gnat_error_node, gnat_object);
6822 /* Similarly, but both validate and process a value of RM_Size. This
6823 routine is only called for types. */
6826 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6828 /* Only give an error if a Value_Size clause was explicitly given.
6829 Otherwise, we'd be duplicating an error on the Size clause. */
6830 Node_Id gnat_attr_node
6831 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6832 tree old_size = rm_size (gnu_type);
6835 /* Get the size as a tree. Do nothing if none was specified, either
6836 because RM_Size was not Present or if the specified size was zero.
6837 Give an error if a size was specified, but cannot be represented as
6839 if (No (uint_size) || uint_size == No_Uint)
6842 size = UI_To_gnu (uint_size, bitsizetype);
6843 if (TREE_OVERFLOW (size))
6845 if (Present (gnat_attr_node))
6846 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6852 /* Ignore a negative size since that corresponds to our back-annotation.
6853 Also ignore a zero size unless a size clause exists, a Value_Size
6854 clause exists, or this is an integer type, in which case the
6855 front end will have always set it. */
6856 else if (tree_int_cst_sgn (size) < 0
6857 || (integer_zerop (size) && No (gnat_attr_node)
6858 && !Has_Size_Clause (gnat_entity)
6859 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6862 /* If the old size is self-referential, get the maximum size. */
6863 if (CONTAINS_PLACEHOLDER_P (old_size))
6864 old_size = max_size (old_size, true);
6866 /* If the size of the object is a constant, the new size must not be
6867 smaller (the front end checks this for scalar types). */
6868 if (TREE_CODE (old_size) != INTEGER_CST
6869 || TREE_OVERFLOW (old_size)
6870 || (AGGREGATE_TYPE_P (gnu_type)
6871 && tree_int_cst_lt (size, old_size)))
6873 if (Present (gnat_attr_node))
6875 ("Value_Size for& too small{, minimum allowed is ^}",
6876 gnat_attr_node, gnat_entity, old_size);
6881 /* Otherwise, set the RM_Size. */
6882 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6883 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6884 TYPE_RM_SIZE_NUM (gnu_type) = size;
6885 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6886 TYPE_RM_SIZE_NUM (gnu_type) = size;
6887 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6888 || TREE_CODE (gnu_type) == UNION_TYPE
6889 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6890 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6891 SET_TYPE_ADA_SIZE (gnu_type, size);
6894 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6895 If TYPE is the best type, return it. Otherwise, make a new type. We
6896 only support new integral and pointer types. BIASED_P is nonzero if
6897 we are making a biased type. */
6900 make_type_from_size (tree type, tree size_tree, bool biased_p)
6903 unsigned HOST_WIDE_INT size;
6906 /* If size indicates an error, just return TYPE to avoid propagating the
6907 error. Likewise if it's too large to represent. */
6908 if (!size_tree || !host_integerp (size_tree, 1))
6911 size = tree_low_cst (size_tree, 1);
6912 switch (TREE_CODE (type))
6916 /* Only do something if the type is not already the proper size and is
6917 not a packed array type. */
6918 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6919 || (TYPE_PRECISION (type) == size
6920 && biased_p == (TREE_CODE (type) == INTEGER_CST
6921 && TYPE_BIASED_REPRESENTATION_P (type))))
6924 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6925 && TYPE_BIASED_REPRESENTATION_P (type));
6926 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6928 size = MIN (size, LONG_LONG_TYPE_SIZE);
6930 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6931 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6932 TYPE_MIN_VALUE (new_type)
6933 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6934 TYPE_MAX_VALUE (new_type)
6935 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6936 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6937 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6941 /* Do something if this is a fat pointer, in which case we
6942 may need to return the thin pointer. */
6943 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6946 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6950 /* Only do something if this is a thin pointer, in which case we
6951 may need to return the fat pointer. */
6952 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6954 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6965 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6966 a type or object whose present alignment is ALIGN. If this alignment is
6967 valid, return it. Otherwise, give an error and return ALIGN. */
6970 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6972 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
6973 unsigned int new_align;
6974 Node_Id gnat_error_node;
6976 /* Don't worry about checking alignment if alignment was not specified
6977 by the source program and we already posted an error for this entity. */
6978 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6981 /* Post the error on the alignment clause if any. */
6982 if (Present (Alignment_Clause (gnat_entity)))
6983 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6985 gnat_error_node = gnat_entity;
6987 /* Within GCC, an alignment is an integer, so we must make sure a value is
6988 specified that fits in that range. Also, there is an upper bound to
6989 alignments we can support/allow. */
6990 if (!UI_Is_In_Int_Range (alignment)
6991 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
6992 post_error_ne_num ("largest supported alignment for& is ^",
6993 gnat_error_node, gnat_entity, max_allowed_alignment);
6994 else if (!(Present (Alignment_Clause (gnat_entity))
6995 && From_At_Mod (Alignment_Clause (gnat_entity)))
6996 && new_align * BITS_PER_UNIT < align)
6997 post_error_ne_num ("alignment for& must be at least ^",
6998 gnat_error_node, gnat_entity,
6999 align / BITS_PER_UNIT);
7002 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7003 if (new_align > align)
7010 /* Return the smallest alignment not less than SIZE. */
7013 ceil_alignment (unsigned HOST_WIDE_INT size)
7015 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7018 /* Verify that OBJECT, a type or decl, is something we can implement
7019 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7020 if we require atomic components. */
7023 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7025 Node_Id gnat_error_point = gnat_entity;
7027 enum machine_mode mode;
7031 /* There are three case of what OBJECT can be. It can be a type, in which
7032 case we take the size, alignment and mode from the type. It can be a
7033 declaration that was indirect, in which case the relevant values are
7034 that of the type being pointed to, or it can be a normal declaration,
7035 in which case the values are of the decl. The code below assumes that
7036 OBJECT is either a type or a decl. */
7037 if (TYPE_P (object))
7039 mode = TYPE_MODE (object);
7040 align = TYPE_ALIGN (object);
7041 size = TYPE_SIZE (object);
7043 else if (DECL_BY_REF_P (object))
7045 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7046 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7047 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7051 mode = DECL_MODE (object);
7052 align = DECL_ALIGN (object);
7053 size = DECL_SIZE (object);
7056 /* Consider all floating-point types atomic and any types that that are
7057 represented by integers no wider than a machine word. */
7058 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7059 || ((GET_MODE_CLASS (mode) == MODE_INT
7060 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7061 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7064 /* For the moment, also allow anything that has an alignment equal
7065 to its size and which is smaller than a word. */
7066 if (size && TREE_CODE (size) == INTEGER_CST
7067 && compare_tree_int (size, align) == 0
7068 && align <= BITS_PER_WORD)
7071 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7072 gnat_node = Next_Rep_Item (gnat_node))
7074 if (!comp_p && Nkind (gnat_node) == N_Pragma
7075 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7077 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7078 else if (comp_p && Nkind (gnat_node) == N_Pragma
7079 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7080 == Pragma_Atomic_Components))
7081 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7085 post_error_ne ("atomic access to component of & cannot be guaranteed",
7086 gnat_error_point, gnat_entity);
7088 post_error_ne ("atomic access to & cannot be guaranteed",
7089 gnat_error_point, gnat_entity);
7092 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7093 have compatible signatures so that a call using one type may be safely
7094 issued if the actual target function type is the other. Return 1 if it is
7095 the case, 0 otherwise, and post errors on the incompatibilities.
7097 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7098 that calls to the subprogram will have arguments suitable for the later
7099 underlying builtin expansion. */
7102 compatible_signatures_p (tree ftype1, tree ftype2)
7104 /* As of now, we only perform very trivial tests and consider it's the
7105 programmer's responsibility to ensure the type correctness in the Ada
7106 declaration, as in the regular Import cases.
7108 Mismatches typically result in either error messages from the builtin
7109 expander, internal compiler errors, or in a real call sequence. This
7110 should be refined to issue diagnostics helping error detection and
7113 /* Almost fake test, ensuring a use of each argument. */
7114 if (ftype1 == ftype2)
7120 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
7121 type with all size expressions that contain F updated by replacing F
7122 with R. If F is NULL_TREE, always make a new RECORD_TYPE, even if
7123 nothing has changed. */
7126 substitute_in_type (tree t, tree f, tree r)
7131 switch (TREE_CODE (t))
7136 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7137 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7139 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7140 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7142 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7145 new = build_range_type (TREE_TYPE (t), low, high);
7146 if (TYPE_INDEX_TYPE (t))
7148 (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7155 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7156 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7158 tree low = NULL_TREE, high = NULL_TREE;
7160 if (TYPE_MIN_VALUE (t))
7161 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7162 if (TYPE_MAX_VALUE (t))
7163 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7165 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7169 TYPE_MIN_VALUE (t) = low;
7170 TYPE_MAX_VALUE (t) = high;
7175 tem = substitute_in_type (TREE_TYPE (t), f, r);
7176 if (tem == TREE_TYPE (t))
7179 return build_complex_type (tem);
7185 /* Don't know how to do these yet. */
7190 tree component = substitute_in_type (TREE_TYPE (t), f, r);
7191 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7193 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7196 new = build_array_type (component, domain);
7197 TYPE_SIZE (new) = 0;
7198 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7199 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7201 TYPE_ALIGN (new) = TYPE_ALIGN (t);
7202 TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7204 /* If we had bounded the sizes of T by a constant, bound the sizes of
7205 NEW by the same constant. */
7206 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
7208 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
7210 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
7211 TYPE_SIZE_UNIT (new)
7212 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
7213 TYPE_SIZE_UNIT (new));
7219 case QUAL_UNION_TYPE:
7223 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
7224 bool field_has_rep = false;
7225 tree last_field = NULL_TREE;
7227 tree new = copy_type (t);
7229 /* Start out with no fields, make new fields, and chain them
7230 in. If we haven't actually changed the type of any field,
7231 discard everything we've done and return the old type. */
7233 TYPE_FIELDS (new) = NULL_TREE;
7234 TYPE_SIZE (new) = NULL_TREE;
7236 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7238 tree new_field = copy_node (field);
7240 TREE_TYPE (new_field)
7241 = substitute_in_type (TREE_TYPE (new_field), f, r);
7243 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
7244 field_has_rep = true;
7245 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
7246 changed_field = true;
7248 /* If this is an internal field and the type of this field is
7249 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
7250 the type just has one element, treat that as the field.
7251 But don't do this if we are processing a QUAL_UNION_TYPE. */
7252 if (TREE_CODE (t) != QUAL_UNION_TYPE
7253 && DECL_INTERNAL_P (new_field)
7254 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
7255 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
7257 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
7260 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
7263 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
7265 /* Make sure omitting the union doesn't change
7267 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
7268 new_field = next_new_field;
7272 DECL_CONTEXT (new_field) = new;
7273 SET_DECL_ORIGINAL_FIELD (new_field,
7274 (DECL_ORIGINAL_FIELD (field)
7275 ? DECL_ORIGINAL_FIELD (field) : field));
7277 /* If the size of the old field was set at a constant,
7278 propagate the size in case the type's size was variable.
7279 (This occurs in the case of a variant or discriminated
7280 record with a default size used as a field of another
7282 DECL_SIZE (new_field)
7283 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
7284 ? DECL_SIZE (field) : NULL_TREE;
7285 DECL_SIZE_UNIT (new_field)
7286 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
7287 ? DECL_SIZE_UNIT (field) : NULL_TREE;
7289 if (TREE_CODE (t) == QUAL_UNION_TYPE)
7291 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7293 if (new_q != DECL_QUALIFIER (new_field))
7294 changed_field = true;
7296 /* Do the substitution inside the qualifier and if we find
7297 that this field will not be present, omit it. */
7298 DECL_QUALIFIER (new_field) = new_q;
7300 if (integer_zerop (DECL_QUALIFIER (new_field)))
7305 TYPE_FIELDS (new) = new_field;
7307 TREE_CHAIN (last_field) = new_field;
7309 last_field = new_field;
7311 /* If this is a qualified type and this field will always be
7312 present, we are done. */
7313 if (TREE_CODE (t) == QUAL_UNION_TYPE
7314 && integer_onep (DECL_QUALIFIER (new_field)))
7318 /* If this used to be a qualified union type, but we now know what
7319 field will be present, make this a normal union. */
7320 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
7321 && (!TYPE_FIELDS (new)
7322 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
7323 TREE_SET_CODE (new, UNION_TYPE);
7324 else if (!changed_field)
7327 gcc_assert (!field_has_rep);
7330 /* If the size was originally a constant use it. */
7331 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
7332 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
7334 TYPE_SIZE (new) = TYPE_SIZE (t);
7335 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
7336 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
7347 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
7348 needed to represent the object. */
7351 rm_size (tree gnu_type)
7353 /* For integer types, this is the precision. For record types, we store
7354 the size explicitly. For other types, this is just the size. */
7356 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7357 return TYPE_RM_SIZE (gnu_type);
7358 else if (TREE_CODE (gnu_type) == RECORD_TYPE
7359 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7360 /* Return the rm_size of the actual data plus the size of the template. */
7362 size_binop (PLUS_EXPR,
7363 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7364 DECL_SIZE (TYPE_FIELDS (gnu_type)));
7365 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7366 || TREE_CODE (gnu_type) == UNION_TYPE
7367 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7368 && !TYPE_IS_FAT_POINTER_P (gnu_type)
7369 && TYPE_ADA_SIZE (gnu_type))
7370 return TYPE_ADA_SIZE (gnu_type);
7372 return TYPE_SIZE (gnu_type);
7375 /* Return an identifier representing the external name to be used for
7376 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
7377 and the specified suffix. */
7380 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7382 Entity_Kind kind = Ekind (gnat_entity);
7384 const char *str = (!suffix ? "" : suffix);
7385 String_Template temp = {1, strlen (str)};
7386 Fat_Pointer fp = {str, &temp};
7388 Get_External_Name_With_Suffix (gnat_entity, fp);
7390 /* A variable using the Stdcall convention (meaning we are running
7391 on a Windows box) live in a DLL. Here we adjust its name to use
7392 the jump-table, the _imp__NAME contains the address for the NAME
7394 if ((kind == E_Variable || kind == E_Constant)
7395 && Has_Stdcall_Convention (gnat_entity))
7397 const char *prefix = "_imp__";
7398 int k, plen = strlen (prefix);
7400 for (k = 0; k <= Name_Len; k++)
7401 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
7402 strncpy (Name_Buffer, prefix, plen);
7405 return get_identifier (Name_Buffer);
7408 /* Return the name to be used for GNAT_ENTITY. If a type, create a
7409 fully-qualified name, possibly with type information encoding.
7410 Otherwise, return the name. */
7413 get_entity_name (Entity_Id gnat_entity)
7415 Get_Encoded_Name (gnat_entity);
7416 return get_identifier (Name_Buffer);
7419 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
7420 string, return a new IDENTIFIER_NODE that is the concatenation of
7421 the name in GNU_ID and SUFFIX. */
7424 concat_id_with_name (tree gnu_id, const char *suffix)
7426 int len = IDENTIFIER_LENGTH (gnu_id);
7428 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
7429 strncpy (Name_Buffer + len, "___", 3);
7431 strcpy (Name_Buffer + len, suffix);
7432 return get_identifier (Name_Buffer);
7435 #include "gt-ada-decl.h"