1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2004, 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 2, 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 distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
25 ****************************************************************************/
29 #include "coretypes.h"
55 /* Provide default values for the macros controlling stack checking.
56 This is copied from GCC's expr.h. */
58 #ifndef STACK_CHECK_BUILTIN
59 #define STACK_CHECK_BUILTIN 0
61 #ifndef STACK_CHECK_PROBE_INTERVAL
62 #define STACK_CHECK_PROBE_INTERVAL 4096
64 #ifndef STACK_CHECK_MAX_FRAME_SIZE
65 #define STACK_CHECK_MAX_FRAME_SIZE \
66 (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
68 #ifndef STACK_CHECK_MAX_VAR_SIZE
69 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
72 /* These two variables are used to defer recursively expanding incomplete
73 types while we are processing a record or subprogram type. */
75 static int defer_incomplete_level = 0;
76 static struct incomplete
78 struct incomplete *next;
81 } *defer_incomplete_list = 0;
83 static void copy_alias_set (tree, tree);
84 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
85 static bool allocatable_size_p (tree, bool);
86 static struct attrib *build_attr_list (Entity_Id);
87 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
88 static bool is_variable_size (tree);
89 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
91 static tree make_packable_type (tree);
92 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
93 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
95 static int compare_field_bitpos (const PTR, const PTR);
96 static Uint annotate_value (tree);
97 static void annotate_rep (Entity_Id, tree);
98 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
99 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
100 static void set_rm_size (Uint, tree, Entity_Id);
101 static tree make_type_from_size (tree, tree, bool);
102 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
103 static void check_ok_for_atomic (tree, Entity_Id, bool);
105 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
106 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
107 refer to an Ada type. */
110 gnat_to_gnu_type (Entity_Id gnat_entity)
114 /* The back end never attempts to annotate generic types */
115 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
116 return void_type_node;
118 /* Convert the ada entity type into a GCC TYPE_DECL node. */
119 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
120 if (TREE_CODE (gnu_decl) != TYPE_DECL)
123 return TREE_TYPE (gnu_decl);
126 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
127 entity, this routine returns the equivalent GCC tree for that entity
128 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
131 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
132 initial value (in GCC tree form). This is optional for variables.
133 For renamed entities, GNU_EXPR gives the object being renamed.
135 DEFINITION is nonzero if this call is intended for a definition. This is
136 used for separate compilation where it necessary to know whether an
137 external declaration or a definition should be created if the GCC equivalent
138 was not created previously. The value of 1 is normally used for a non-zero
139 DEFINITION, but a value of 2 is used in special circumstances, defined in
143 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
146 tree gnu_type = NULL_TREE;
147 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
148 GNAT tree. This node will be associated with the GNAT node by calling
149 the save_gnu_tree routine at the end of the `switch' statement. */
150 tree gnu_decl = NULL_TREE;
151 /* true if we have already saved gnu_decl as a gnat association. */
153 /* Nonzero if we incremented defer_incomplete_level. */
154 bool this_deferred = false;
155 /* Nonzero if we incremented force_global. */
156 bool this_global = false;
157 /* Nonzero if we should check to see if elaborated during processing. */
158 bool maybe_present = false;
159 /* Nonzero if we made GNU_DECL and its type here. */
160 bool this_made_decl = false;
161 struct attrib *attr_list = NULL;
162 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
163 || debug_info_level == DINFO_LEVEL_VERBOSE);
164 Entity_Kind kind = Ekind (gnat_entity);
167 = ((Known_Esize (gnat_entity)
168 && UI_Is_In_Int_Range (Esize (gnat_entity)))
169 ? MIN (UI_To_Int (Esize (gnat_entity)),
170 IN (kind, Float_Kind)
171 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
172 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
173 : LONG_LONG_TYPE_SIZE)
174 : LONG_LONG_TYPE_SIZE);
177 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
178 || From_With_Type (gnat_entity));
179 unsigned int align = 0;
181 /* Since a use of an Itype is a definition, process it as such if it
182 is not in a with'ed unit. */
184 if (!definition && Is_Itype (gnat_entity)
185 && !present_gnu_tree (gnat_entity)
186 && In_Extended_Main_Code_Unit (gnat_entity))
188 /* Ensure that we are in a subprogram mentioned in the Scope
189 chain of this entity, our current scope is global,
190 or that we encountered a task or entry (where we can't currently
191 accurately check scoping). */
192 if (!current_function_decl
193 || DECL_ELABORATION_PROC_P (current_function_decl))
195 process_type (gnat_entity);
196 return get_gnu_tree (gnat_entity);
199 for (gnat_temp = Scope (gnat_entity);
200 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
202 if (Is_Type (gnat_temp))
203 gnat_temp = Underlying_Type (gnat_temp);
205 if (Ekind (gnat_temp) == E_Subprogram_Body)
207 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
209 if (IN (Ekind (gnat_temp), Subprogram_Kind)
210 && Present (Protected_Body_Subprogram (gnat_temp)))
211 gnat_temp = Protected_Body_Subprogram (gnat_temp);
213 if (Ekind (gnat_temp) == E_Entry
214 || Ekind (gnat_temp) == E_Entry_Family
215 || Ekind (gnat_temp) == E_Task_Type
216 || (IN (Ekind (gnat_temp), Subprogram_Kind)
217 && present_gnu_tree (gnat_temp)
218 && (current_function_decl
219 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
221 process_type (gnat_entity);
222 return get_gnu_tree (gnat_entity);
226 /* This abort means the entity "gnat_entity" has an incorrect scope,
227 i.e. that its scope does not correspond to the subprogram in which
232 /* If this is entity 0, something went badly wrong. */
233 if (No (gnat_entity))
236 /* If we've already processed this entity, return what we got last time.
237 If we are defining the node, we should not have already processed it.
238 In that case, we will abort below when we try to save a new GCC tree for
239 this object. We also need to handle the case of getting a dummy type
240 when a Full_View exists. */
242 if (present_gnu_tree (gnat_entity)
244 || (Is_Type (gnat_entity) && imported_p)))
246 gnu_decl = get_gnu_tree (gnat_entity);
248 if (TREE_CODE (gnu_decl) == TYPE_DECL
249 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
250 && IN (kind, Incomplete_Or_Private_Kind)
251 && Present (Full_View (gnat_entity)))
253 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
256 save_gnu_tree (gnat_entity, NULL_TREE, false);
257 save_gnu_tree (gnat_entity, gnu_decl, false);
263 /* If this is a numeric or enumeral type, or an access type, a nonzero
264 Esize must be specified unless it was specified by the programmer. */
265 if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
266 || (IN (kind, Access_Kind)
267 && kind != E_Access_Protected_Subprogram_Type
268 && kind != E_Access_Subtype))
269 && Unknown_Esize (gnat_entity)
270 && !Has_Size_Clause (gnat_entity))
273 /* Likewise, RM_Size must be specified for all discrete and fixed-point
275 if (IN (kind, Discrete_Or_Fixed_Point_Kind)
276 && Unknown_RM_Size (gnat_entity))
279 /* Get the name of the entity and set up the line number and filename of
280 the original definition for use in any decl we make. */
281 gnu_entity_id = get_entity_name (gnat_entity);
282 Sloc_to_locus (Sloc (gnat_entity), &input_location);
284 /* If we get here, it means we have not yet done anything with this
285 entity. If we are not defining it here, it must be external,
286 otherwise we should have defined it already. */
287 if (!definition && ! Is_Public (gnat_entity)
288 && !type_annotate_only
289 && kind != E_Discriminant && kind != E_Component
291 && !(kind == E_Constant && Present (Full_View (gnat_entity)))
293 && !IN (kind, Type_Kind)
298 /* For cases when we are not defining (i.e., we are referencing from
299 another compilation unit) Public entities, show we are at global level
300 for the purpose of computing scopes. Don't do this for components or
301 discriminants since the relevant test is whether or not the record is
302 being defined. But do this for Imported functions or procedures in
304 if ((!definition && Is_Public (gnat_entity)
305 && !Is_Statically_Allocated (gnat_entity)
306 && kind != E_Discriminant && kind != E_Component)
307 || (Is_Imported (gnat_entity)
308 && (kind == E_Function || kind == E_Procedure)))
309 force_global++, this_global = true;
311 /* Handle any attributes. */
312 if (Has_Gigi_Rep_Item (gnat_entity))
313 attr_list = build_attr_list (gnat_entity);
318 /* If this is a use of a deferred constant, get its full
320 if (!definition && Present (Full_View (gnat_entity)))
322 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
323 gnu_expr, definition);
328 /* If we have an external constant that we are not defining,
329 get the expression that is was defined to represent. We
330 may throw that expression away later if it is not a
332 Do not retrieve the expression if it is an aggregate, because
333 in complex instantiation contexts it may not be expanded */
336 && Present (Expression (Declaration_Node (gnat_entity)))
337 && !No_Initialization (Declaration_Node (gnat_entity))
338 && (Nkind (Expression (Declaration_Node (gnat_entity)))
340 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
342 /* Ignore deferred constant definitions; they are processed fully in the
343 front-end. For deferred constant references, get the full
344 definition. On the other hand, constants that are renamings are
345 handled like variable renamings. If No_Initialization is set, this is
346 not a deferred constant but a constant whose value is built
349 if (definition && !gnu_expr
350 && !No_Initialization (Declaration_Node (gnat_entity))
351 && No (Renamed_Object (gnat_entity)))
353 gnu_decl = error_mark_node;
357 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
358 && Present (Full_View (gnat_entity)))
360 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
369 /* We used to special case VMS exceptions here to directly map them to
370 their associated condition code. Since this code had to be masked
371 dynamically to strip off the severity bits, this caused trouble in
372 the GCC/ZCX case because the "type" pointers we store in the tables
373 have to be static. We now don't special case here anymore, and let
374 the regular processing take place, which leaves us with a regular
375 exception data object for VMS exceptions too. The condition code
376 mapping is taken care of by the front end and the bitmasking by the
383 /* The GNAT record where the component was defined. */
384 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
386 /* If the variable is an inherited record component (in the case of
387 extended record types), just return the inherited entity, which
388 must be a FIELD_DECL. Likewise for discriminants.
389 For discriminants of untagged records which have explicit
390 stored discriminants, return the entity for the corresponding
391 stored discriminant. Also use Original_Record_Component
392 if the record has a private extension. */
394 if ((Base_Type (gnat_record) == gnat_record
395 || Ekind (Scope (gnat_entity)) == E_Private_Subtype
396 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
397 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
398 && Present (Original_Record_Component (gnat_entity))
399 && Original_Record_Component (gnat_entity) != gnat_entity)
402 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
403 gnu_expr, definition);
408 /* If the enclosing record has explicit stored discriminants,
409 then it is an untagged record. If the Corresponding_Discriminant
410 is not empty then this must be a renamed discriminant and its
411 Original_Record_Component must point to the corresponding explicit
412 stored discriminant (i.e., we should have taken the previous
415 else if (Present (Corresponding_Discriminant (gnat_entity))
416 && Is_Tagged_Type (gnat_record))
418 /* A tagged record has no explicit stored discriminants. */
420 if (First_Discriminant (gnat_record)
421 != First_Stored_Discriminant (gnat_record))
425 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
426 gnu_expr, definition);
431 /* If the enclosing record has explicit stored discriminants,
432 then it is an untagged record. If the Corresponding_Discriminant
433 is not empty then this must be a renamed discriminant and its
434 Original_Record_Component must point to the corresponding explicit
435 stored discriminant (i.e., we should have taken the first
438 else if (Present (Corresponding_Discriminant (gnat_entity))
439 && (First_Discriminant (gnat_record)
440 != First_Stored_Discriminant (gnat_record)))
443 /* Otherwise, if we are not defining this and we have no GCC type
444 for the containing record, make one for it. Then we should
445 have made our own equivalent. */
446 else if (!definition && !present_gnu_tree (gnat_record))
448 /* ??? If this is in a record whose scope is a protected
449 type and we have an Original_Record_Component, use it.
450 This is a workaround for major problems in protected type
453 Entity_Id Scop = Scope (Scope (gnat_entity));
454 if ((Is_Protected_Type (Scop)
455 || (Is_Private_Type (Scop)
456 && Present (Full_View (Scop))
457 && Is_Protected_Type (Full_View (Scop))))
458 && Present (Original_Record_Component (gnat_entity)))
461 = gnat_to_gnu_entity (Original_Record_Component
463 gnu_expr, definition);
468 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
469 gnu_decl = get_gnu_tree (gnat_entity);
475 /* Here we have no GCC type and this is a reference rather than a
476 definition. This should never happen. Most likely the cause is a
477 reference before declaration in the gnat tree for gnat_entity. */
481 case E_Loop_Parameter:
482 case E_Out_Parameter:
485 /* Simple variables, loop variables, OUT parameters, and exceptions. */
488 bool used_by_ref = false;
490 = ((kind == E_Constant || kind == E_Variable)
491 && !Is_Statically_Allocated (gnat_entity)
492 && Is_True_Constant (gnat_entity)
493 && (((Nkind (Declaration_Node (gnat_entity))
494 == N_Object_Declaration)
495 && Present (Expression (Declaration_Node (gnat_entity))))
496 || Present (Renamed_Object (gnat_entity))));
497 bool inner_const_flag = const_flag;
498 bool static_p = Is_Statically_Allocated (gnat_entity);
499 tree gnu_ext_name = NULL_TREE;
501 if (Present (Renamed_Object (gnat_entity)) && !definition)
503 if (kind == E_Exception)
504 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
507 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
510 /* Get the type after elaborating the renamed object. */
511 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
513 /* If this is a loop variable, its type should be the base type.
514 This is because the code for processing a loop determines whether
515 a normal loop end test can be done by comparing the bounds of the
516 loop against those of the base type, which is presumed to be the
517 size used for computation. But this is not correct when the size
518 of the subtype is smaller than the type. */
519 if (kind == E_Loop_Parameter)
520 gnu_type = get_base_type (gnu_type);
522 /* Reject non-renamed objects whose types are unconstrained arrays or
523 any object whose type is a dummy type or VOID_TYPE. */
525 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
526 && No (Renamed_Object (gnat_entity)))
527 || TYPE_IS_DUMMY_P (gnu_type)
528 || TREE_CODE (gnu_type) == VOID_TYPE)
530 if (type_annotate_only)
531 return error_mark_node;
536 /* If we are defining the object, see if it has a Size value and
537 validate it if so. If we are not defining the object and a Size
538 clause applies, simply retrieve the value. We don't want to ignore
539 the clause and it is expected to have been validated already. Then
540 get the new type, if any. */
542 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
543 gnat_entity, VAR_DECL, false,
544 Has_Size_Clause (gnat_entity));
545 else if (Has_Size_Clause (gnat_entity))
546 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
551 = make_type_from_size (gnu_type, gnu_size,
552 Has_Biased_Representation (gnat_entity));
554 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
555 gnu_size = NULL_TREE;
558 /* If this object has self-referential size, it must be a record with
559 a default value. We are supposed to allocate an object of the
560 maximum size in this case unless it is a constant with an
561 initializing expression, in which case we can get the size from
562 that. Note that the resulting size may still be a variable, so
563 this may end up with an indirect allocation. */
565 if (No (Renamed_Object (gnat_entity))
566 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
568 if (gnu_expr && kind == E_Constant)
570 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
571 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
573 /* We may have no GNU_EXPR because No_Initialization is
574 set even though there's an Expression. */
575 else if (kind == E_Constant
576 && (Nkind (Declaration_Node (gnat_entity))
577 == N_Object_Declaration)
578 && Present (Expression (Declaration_Node (gnat_entity))))
580 = TYPE_SIZE (gnat_to_gnu_type
582 (Expression (Declaration_Node (gnat_entity)))));
584 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
587 /* If the size is zero bytes, make it one byte since some linkers have
588 trouble with zero-sized objects. If the object will have a
589 template, that will make it nonzero so don't bother. Also avoid
590 doing that for an object renaming or an object with an address
591 clause, as we would lose useful information on the view size
592 (e.g. for null array slices) and we are not allocating the object
594 if (((gnu_size && integer_zerop (gnu_size))
595 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
596 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
597 || !Is_Array_Type (Etype (gnat_entity)))
598 && !Present (Renamed_Object (gnat_entity))
599 && !Present (Address_Clause (gnat_entity)))
600 gnu_size = bitsize_unit_node;
602 /* If an alignment is specified, use it if valid. Note that
603 exceptions are objects but don't have alignments. */
604 if (kind != E_Exception && Known_Alignment (gnat_entity))
606 if (No (Alignment (gnat_entity)))
609 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
610 TYPE_ALIGN (gnu_type));
613 /* If this is an atomic object with no specified size and alignment,
614 but where the size of the type is a constant, set the alignment to
615 the lowest power of two greater than the size, or to the
616 biggest meaningful alignment, whichever is smaller. */
618 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
619 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
621 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
622 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
624 align = BIGGEST_ALIGNMENT;
626 align = ((unsigned int) 1
627 << (floor_log2 (tree_low_cst
628 (TYPE_SIZE (gnu_type), 1) - 1)
632 /* If the object is set to have atomic components, find the component
633 type and validate it.
635 ??? Note that we ignore Has_Volatile_Components on objects; it's
636 not at all clear what to do in that case. */
638 if (Has_Atomic_Components (gnat_entity))
640 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
641 ? TREE_TYPE (gnu_type) : gnu_type);
643 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
644 && TYPE_MULTI_ARRAY_P (gnu_inner))
645 gnu_inner = TREE_TYPE (gnu_inner);
647 check_ok_for_atomic (gnu_inner, gnat_entity, true);
650 /* Now check if the type of the object allows atomic access. Note
651 that we must test the type, even if this object has size and
652 alignment to allow such access, because we will be going
653 inside the padded record to assign to the object. We could fix
654 this by always copying via an intermediate value, but it's not
655 clear it's worth the effort. */
656 if (Is_Atomic (gnat_entity))
657 check_ok_for_atomic (gnu_type, gnat_entity, false);
659 /* If this is an aliased object with an unconstrained nominal subtype,
660 make a type that includes the template. */
661 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
662 && Is_Array_Type (Etype (gnat_entity))
663 && !type_annotate_only)
666 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
668 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
671 = build_unc_object_type (gnu_temp_type, gnu_type,
672 concat_id_with_name (gnu_entity_id,
676 #ifdef MINIMUM_ATOMIC_ALIGNMENT
677 /* If the size is a constant and no alignment is specified, force
678 the alignment to be the minimum valid atomic alignment. The
679 restriction on constant size avoids problems with variable-size
680 temporaries; if the size is variable, there's no issue with
681 atomic access. Also don't do this for a constant, since it isn't
682 necessary and can interfere with constant replacement. Finally,
683 do not do it for Out parameters since that creates an
684 size inconsistency with In parameters. */
685 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
686 && !FLOAT_TYPE_P (gnu_type)
687 && !const_flag && No (Renamed_Object (gnat_entity))
688 && !imported_p && No (Address_Clause (gnat_entity))
689 && kind != E_Out_Parameter
690 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
691 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
692 align = MINIMUM_ATOMIC_ALIGNMENT;
695 /* Make a new type with the desired size and alignment, if needed. */
696 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
697 "PAD", false, definition, true);
699 /* Make a volatile version of this object's type if we are to
700 make the object volatile. Note that 13.3(19) says that we
701 should treat other types of objects as volatile as well. */
702 if ((Treat_As_Volatile (gnat_entity)
703 || Is_Exported (gnat_entity)
704 || Is_Imported (gnat_entity)
705 || Present (Address_Clause (gnat_entity)))
706 && !TYPE_VOLATILE (gnu_type))
707 gnu_type = build_qualified_type (gnu_type,
708 (TYPE_QUALS (gnu_type)
709 | TYPE_QUAL_VOLATILE));
711 /* Convert the expression to the type of the object except in the
712 case where the object's type is unconstrained or the object's type
713 is a padded record whose field is of self-referential size. In
714 the former case, converting will generate unnecessary evaluations
715 of the CONSTRUCTOR to compute the size and in the latter case, we
716 want to only copy the actual data. */
718 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
719 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
720 && !(TREE_CODE (gnu_type) == RECORD_TYPE
721 && TYPE_IS_PADDING_P (gnu_type)
722 && (CONTAINS_PLACEHOLDER_P
723 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
724 gnu_expr = convert (gnu_type, gnu_expr);
726 /* See if this is a renaming. If this is a constant renaming, treat
727 it as a normal variable whose initial value is what is being
728 renamed. We cannot do this if the type is unconstrained or
731 Otherwise, if what we are renaming is a reference, we can simply
732 return a stabilized version of that reference, after forcing any
733 SAVE_EXPRs to be evaluated. But, if this is at global level, we
734 can only do this if we know no SAVE_EXPRs will be made.
736 Otherwise, make this into a constant pointer to the object we are
739 if (Present (Renamed_Object (gnat_entity)))
741 /* If the renamed object had padding, strip off the reference
742 to the inner object and reset our type. */
743 if (TREE_CODE (gnu_expr) == COMPONENT_REF
744 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
746 && (TYPE_IS_PADDING_P
747 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
749 gnu_expr = TREE_OPERAND (gnu_expr, 0);
750 gnu_type = TREE_TYPE (gnu_expr);
754 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
755 && TYPE_MODE (gnu_type) != BLKmode
756 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
757 && !Is_Array_Type (Etype (gnat_entity)))
760 /* If this is a declaration or reference that we can stabilize,
761 just use that declaration or reference as this entity unless
762 the latter has to be materialized. */
763 else if ((DECL_P (gnu_expr)
764 || (REFERENCE_CLASS_P (gnu_expr) == tcc_reference))
765 && !Materialize_Entity (gnat_entity)
766 && (!global_bindings_p ()
767 || (staticp (gnu_expr)
768 && !TREE_SIDE_EFFECTS (gnu_expr))))
770 gnu_decl = gnat_stabilize_reference (gnu_expr, true);
771 save_gnu_tree (gnat_entity, gnu_decl, true);
776 /* Otherwise, make this into a constant pointer to the object we
779 Stabilize it if we are not at the global level since in this
780 case the renaming evaluation may directly dereference the
781 initial value we make here instead of the pointer we will
782 assign it to. We don't want variables in the expression to be
783 evaluated every time the renaming is used, since the value of
784 these variables may change in between.
786 If we are at the global level and the value is not constant,
787 create_var_decl generates a mere elaboration assignment and
788 does not attach the initial expression to the declaration.
789 There is no possible direct initial-value dereference then. */
792 inner_const_flag = TREE_READONLY (gnu_expr);
794 gnu_type = build_reference_type (gnu_type);
795 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
797 if (!global_bindings_p ())
799 gnu_expr = gnat_stabilize_reference (gnu_expr, true);
803 gnu_size = NULL_TREE;
808 /* If this is an aliased object whose nominal subtype is unconstrained,
809 the object is a record that contains both the template and
810 the object. If there is an initializer, it will have already
811 been converted to the right type, but we need to create the
812 template if there is no initializer. */
813 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
814 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
815 /* Beware that padding might have been introduced
816 via maybe_pad_type above. */
817 || (TYPE_IS_PADDING_P (gnu_type)
818 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
820 && TYPE_CONTAINS_TEMPLATE_P
821 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
825 = TYPE_IS_PADDING_P (gnu_type)
826 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
827 : TYPE_FIELDS (gnu_type);
830 = gnat_build_constructor
834 build_template (TREE_TYPE (template_field),
835 TREE_TYPE (TREE_CHAIN (template_field)),
840 /* If this is a pointer and it does not have an initializing
841 expression, initialize it to NULL, unless the obect is
844 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
845 && !Is_Imported (gnat_entity) && !gnu_expr)
846 gnu_expr = integer_zero_node;
848 /* If we are defining the object and it has an Address clause we must
849 get the address expression from the saved GCC tree for the
850 object if the object has a Freeze_Node. Otherwise, we elaborate
851 the address expression here since the front-end has guaranteed
852 in that case that the elaboration has no effects. Note that
853 only the latter mechanism is currently in use. */
854 if (definition && Present (Address_Clause (gnat_entity)))
857 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
858 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
860 save_gnu_tree (gnat_entity, NULL_TREE, false);
862 /* Ignore the size. It's either meaningless or was handled
864 gnu_size = NULL_TREE;
865 gnu_type = build_reference_type (gnu_type);
866 gnu_address = convert (gnu_type, gnu_address);
868 const_flag = !Is_Public (gnat_entity);
870 /* If we don't have an initializing expression for the underlying
871 variable, the initializing expression for the pointer is the
872 specified address. Otherwise, we have to make a COMPOUND_EXPR
873 to assign both the address and the initial value. */
875 gnu_expr = gnu_address;
878 = build2 (COMPOUND_EXPR, gnu_type,
880 (MODIFY_EXPR, NULL_TREE,
881 build_unary_op (INDIRECT_REF, NULL_TREE,
887 /* If it has an address clause and we are not defining it, mark it
888 as an indirect object. Likewise for Stdcall objects that are
890 if ((!definition && Present (Address_Clause (gnat_entity)))
891 || (Is_Imported (gnat_entity)
892 && Convention (gnat_entity) == Convention_Stdcall))
894 gnu_type = build_reference_type (gnu_type);
895 gnu_size = NULL_TREE;
899 /* If we are at top level and this object is of variable size,
900 make the actual type a hidden pointer to the real type and
901 make the initializer be a memory allocation and initialization.
902 Likewise for objects we aren't defining (presumed to be
903 external references from other packages), but there we do
904 not set up an initialization.
906 If the object's size overflows, make an allocator too, so that
907 Storage_Error gets raised. Note that we will never free
908 such memory, so we presume it never will get allocated. */
910 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
911 global_bindings_p () || !definition
914 && ! allocatable_size_p (gnu_size,
915 global_bindings_p () || !definition
918 gnu_type = build_reference_type (gnu_type);
919 gnu_size = NULL_TREE;
923 /* In case this was a aliased object whose nominal subtype is
924 unconstrained, the pointer above will be a thin pointer and
925 build_allocator will automatically make the template.
927 If we have a template initializer only (that we made above),
928 pretend there is none and rely on what build_allocator creates
929 again anyway. Otherwise (if we have a full initializer), get
930 the data part and feed that to build_allocator. */
934 tree gnu_alloc_type = TREE_TYPE (gnu_type);
936 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
937 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
940 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
942 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
944 TREE_CHAIN (CONSTRUCTOR_ELTS (gnu_expr)) == NULL_TREE)
948 = build_component_ref
949 (gnu_expr, NULL_TREE,
950 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
954 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
955 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
956 && !Is_Imported (gnat_entity))
957 post_error ("Storage_Error will be raised at run-time?",
960 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
961 gnu_type, 0, 0, gnat_entity);
965 gnu_expr = NULL_TREE;
970 /* If this object would go into the stack and has an alignment
971 larger than the default largest alignment, make a variable
972 to hold the "aligning type" with a modified initial value,
973 if any, then point to it and make that the value of this
974 variable, which is now indirect. */
975 if (!global_bindings_p () && !static_p && definition
976 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
979 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
980 TYPE_SIZE_UNIT (gnu_type));
984 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
985 NULL_TREE, gnu_new_type, gnu_expr, false,
986 false, false, false, NULL, gnat_entity);
990 (build_binary_op (MODIFY_EXPR, NULL_TREE,
992 (gnu_new_var, NULL_TREE,
993 TYPE_FIELDS (gnu_new_type), false),
997 gnu_type = build_reference_type (gnu_type);
1000 (ADDR_EXPR, gnu_type,
1001 build_component_ref (gnu_new_var, NULL_TREE,
1002 TYPE_FIELDS (gnu_new_type), false));
1004 gnu_size = NULL_TREE;
1009 /* Convert the expression to the type of the object except in the
1010 case where the object's type is unconstrained or the object's type
1011 is a padded record whose field is of self-referential size. In
1012 the former case, converting will generate unnecessary evaluations
1013 of the CONSTRUCTOR to compute the size and in the latter case, we
1014 want to only copy the actual data. */
1016 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1017 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1018 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1019 && TYPE_IS_PADDING_P (gnu_type)
1020 && (CONTAINS_PLACEHOLDER_P
1021 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1022 gnu_expr = convert (gnu_type, gnu_expr);
1024 /* If this name is external or there was a name specified, use it,
1025 unless this is a VMS exception object since this would conflict
1026 with the symbol we need to export in addition. Don't use the
1027 Interface_Name if there is an address clause (see CD30005). */
1028 if (!Is_VMS_Exception (gnat_entity)
1029 && ((Present (Interface_Name (gnat_entity))
1030 && No (Address_Clause (gnat_entity)))
1031 || (Is_Public (gnat_entity)
1032 && (!Is_Imported (gnat_entity)
1033 || Is_Exported (gnat_entity)))))
1034 gnu_ext_name = create_concat_name (gnat_entity, 0);
1038 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1039 | TYPE_QUAL_CONST));
1041 gnu_expr = convert (gnu_type, gnu_expr);
1044 /* If this is constant initialized to a static constant and the
1045 object has an aggregrate type, force it to be statically
1047 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1048 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1049 && (AGGREGATE_TYPE_P (gnu_type)
1050 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1051 && TYPE_IS_PADDING_P (gnu_type))))
1054 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1055 gnu_expr, const_flag,
1056 Is_Public (gnat_entity),
1057 imported_p || !definition,
1058 static_p, attr_list, gnat_entity);
1059 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1060 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1062 /* If we have an address clause and we've made this indirect, it's
1063 not enough to merely mark the type as volatile since volatile
1064 references only conflict with other volatile references while this
1065 reference must conflict with all other references. So ensure that
1066 the dereferenced value has alias set 0. */
1067 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1068 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1070 if (definition && DECL_SIZE (gnu_decl)
1071 && get_block_jmpbuf_decl ()
1072 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1073 || (flag_stack_check && !STACK_CHECK_BUILTIN
1074 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1075 STACK_CHECK_MAX_VAR_SIZE))))
1076 add_stmt_with_node (build_call_1_expr
1077 (update_setjmp_buf_decl,
1078 build_unary_op (ADDR_EXPR, NULL_TREE,
1079 get_block_jmpbuf_decl ())),
1082 /* If this is a public constant or we're not optimizing and we're not
1083 making a VAR_DECL for it, make one just for export or debugger
1084 use. Likewise if the address is taken or if the object or type is
1086 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1087 && (Is_Public (gnat_entity)
1089 || Address_Taken (gnat_entity)
1090 || Is_Aliased (gnat_entity)
1091 || Is_Aliased (Etype (gnat_entity))))
1094 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1095 gnu_expr, false, Is_Public (gnat_entity),
1096 false, static_p, NULL, gnat_entity);
1098 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1101 /* If this is declared in a block that contains an block with an
1102 exception handler, we must force this variable in memory to
1103 suppress an invalid optimization. */
1104 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1105 && Exception_Mechanism != GCC_ZCX)
1106 TREE_ADDRESSABLE (gnu_decl) = 1;
1108 /* Back-annotate the Alignment of the object if not already in the
1109 tree. Likewise for Esize if the object is of a constant size.
1110 But if the "object" is actually a pointer to an object, the
1111 alignment and size are the same as teh type, so don't back-annotate
1112 the values for the pointer. */
1113 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1114 Set_Alignment (gnat_entity,
1115 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1117 if (!used_by_ref && Unknown_Esize (gnat_entity)
1118 && DECL_SIZE (gnu_decl))
1120 tree gnu_back_size = DECL_SIZE (gnu_decl);
1122 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1123 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1125 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1126 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1128 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1134 /* Return a TYPE_DECL for "void" that we previously made. */
1135 gnu_decl = void_type_decl_node;
1138 case E_Enumeration_Type:
1139 /* A special case, for the types Character and Wide_Character in
1140 Standard, we do not list all the literals. So if the literals
1141 are not specified, make this an unsigned type. */
1142 if (No (First_Literal (gnat_entity)))
1144 gnu_type = make_unsigned_type (esize);
1148 /* Normal case of non-character type, or non-Standard character type */
1150 /* Here we have a list of enumeral constants in First_Literal.
1151 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1152 the list to be places into TYPE_FIELDS. Each node in the list
1153 is a TREE_LIST node whose TREE_VALUE is the literal name
1154 and whose TREE_PURPOSE is the value of the literal.
1156 Esize contains the number of bits needed to represent the enumeral
1157 type, Type_Low_Bound also points to the first literal and
1158 Type_High_Bound points to the last literal. */
1160 Entity_Id gnat_literal;
1161 tree gnu_literal_list = NULL_TREE;
1163 if (Is_Unsigned_Type (gnat_entity))
1164 gnu_type = make_unsigned_type (esize);
1166 gnu_type = make_signed_type (esize);
1168 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1170 for (gnat_literal = First_Literal (gnat_entity);
1171 Present (gnat_literal);
1172 gnat_literal = Next_Literal (gnat_literal))
1174 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1177 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1178 gnu_type, gnu_value, true, false, false,
1179 false, NULL, gnat_literal);
1181 save_gnu_tree (gnat_literal, gnu_literal, false);
1182 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1183 gnu_value, gnu_literal_list);
1186 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1188 /* Note that the bounds are updated at the end of this function
1189 because to avoid an infinite recursion when we get the bounds of
1190 this type, since those bounds are objects of this type. */
1194 case E_Signed_Integer_Type:
1195 case E_Ordinary_Fixed_Point_Type:
1196 case E_Decimal_Fixed_Point_Type:
1197 /* For integer types, just make a signed type the appropriate number
1199 gnu_type = make_signed_type (esize);
1202 case E_Modular_Integer_Type:
1203 /* For modular types, make the unsigned type of the proper number of
1204 bits and then set up the modulus, if required. */
1206 enum machine_mode mode;
1210 if (Is_Packed_Array_Type (gnat_entity))
1211 esize = UI_To_Int (RM_Size (gnat_entity));
1213 /* Find the smallest mode at least ESIZE bits wide and make a class
1216 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1217 GET_MODE_BITSIZE (mode) < esize;
1218 mode = GET_MODE_WIDER_MODE (mode))
1221 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1222 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1223 = Is_Packed_Array_Type (gnat_entity);
1225 /* Get the modulus in this type. If it overflows, assume it is because
1226 it is equal to 2**Esize. Note that there is no overflow checking
1227 done on unsigned type, so we detect the overflow by looking for
1228 a modulus of zero, which is otherwise invalid. */
1229 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1231 if (!integer_zerop (gnu_modulus))
1233 TYPE_MODULAR_P (gnu_type) = 1;
1234 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1235 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1236 convert (gnu_type, integer_one_node)));
1239 /* If we have to set TYPE_PRECISION different from its natural value,
1240 make a subtype to do do. Likewise if there is a modulus and
1241 it is not one greater than TYPE_MAX_VALUE. */
1242 if (TYPE_PRECISION (gnu_type) != esize
1243 || (TYPE_MODULAR_P (gnu_type)
1244 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1246 tree gnu_subtype = make_node (INTEGER_TYPE);
1248 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1249 TREE_TYPE (gnu_subtype) = gnu_type;
1250 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1251 TYPE_MAX_VALUE (gnu_subtype)
1252 = TYPE_MODULAR_P (gnu_type)
1253 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1254 TYPE_PRECISION (gnu_subtype) = esize;
1255 TYPE_UNSIGNED (gnu_subtype) = 1;
1256 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1257 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1258 = Is_Packed_Array_Type (gnat_entity);
1259 layout_type (gnu_subtype);
1261 gnu_type = gnu_subtype;
1266 case E_Signed_Integer_Subtype:
1267 case E_Enumeration_Subtype:
1268 case E_Modular_Integer_Subtype:
1269 case E_Ordinary_Fixed_Point_Subtype:
1270 case E_Decimal_Fixed_Point_Subtype:
1272 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1273 that we do not want to call build_range_type since we would
1274 like each subtype node to be distinct. This will be important
1275 when memory aliasing is implemented.
1277 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1278 parent type; this fact is used by the arithmetic conversion
1281 We elaborate the Ancestor_Subtype if it is not in the current
1282 unit and one of our bounds is non-static. We do this to ensure
1283 consistent naming in the case where several subtypes share the same
1284 bounds by always elaborating the first such subtype first, thus
1288 && Present (Ancestor_Subtype (gnat_entity))
1289 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1290 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1291 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1292 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1293 gnu_expr, definition);
1295 gnu_type = make_node (INTEGER_TYPE);
1296 if (Is_Packed_Array_Type (gnat_entity))
1298 esize = UI_To_Int (RM_Size (gnat_entity));
1299 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1302 TYPE_PRECISION (gnu_type) = esize;
1303 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1305 TYPE_MIN_VALUE (gnu_type)
1306 = convert (TREE_TYPE (gnu_type),
1307 elaborate_expression (Type_Low_Bound (gnat_entity),
1309 get_identifier ("L"), definition, 1,
1310 Needs_Debug_Info (gnat_entity)));
1312 TYPE_MAX_VALUE (gnu_type)
1313 = convert (TREE_TYPE (gnu_type),
1314 elaborate_expression (Type_High_Bound (gnat_entity),
1316 get_identifier ("U"), definition, 1,
1317 Needs_Debug_Info (gnat_entity)));
1319 /* One of the above calls might have caused us to be elaborated,
1320 so don't blow up if so. */
1321 if (present_gnu_tree (gnat_entity))
1323 maybe_present = true;
1327 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1328 = Has_Biased_Representation (gnat_entity);
1330 /* This should be an unsigned type if the lower bound is constant
1331 and non-negative or if the base type is unsigned; a signed type
1333 TYPE_UNSIGNED (gnu_type)
1334 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1335 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1336 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1337 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1338 || Is_Unsigned_Type (gnat_entity));
1340 layout_type (gnu_type);
1342 /* If the type we are dealing with is to represent a packed array,
1343 we need to have the bits left justified on big-endian targets
1344 and right justified on little-endian targets. We also need to
1345 ensure that when the value is read (e.g. for comparison of two
1346 such values), we only get the good bits, since the unused bits
1347 are uninitialized. Both goals are accomplished by wrapping the
1348 modular value in an enclosing struct. */
1349 if (Is_Packed_Array_Type (gnat_entity))
1351 tree gnu_field_type = gnu_type;
1354 TYPE_RM_SIZE_NUM (gnu_field_type)
1355 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1356 gnu_type = make_node (RECORD_TYPE);
1357 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1358 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1359 TYPE_PACKED (gnu_type) = 1;
1361 /* Don't notify the field as "addressable", since we won't be taking
1362 it's address and it would prevent create_field_decl from making a
1364 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1365 gnu_field_type, gnu_type, 1, 0, 0, 0);
1367 finish_record_type (gnu_type, gnu_field, false, false);
1368 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1369 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1374 case E_Floating_Point_Type:
1375 /* If this is a VAX floating-point type, use an integer of the proper
1376 size. All the operations will be handled with ASM statements. */
1377 if (Vax_Float (gnat_entity))
1379 gnu_type = make_signed_type (esize);
1380 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1381 SET_TYPE_DIGITS_VALUE (gnu_type,
1382 UI_To_gnu (Digits_Value (gnat_entity),
1387 /* The type of the Low and High bounds can be our type if this is
1388 a type from Standard, so set them at the end of the function. */
1389 gnu_type = make_node (REAL_TYPE);
1390 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1391 layout_type (gnu_type);
1394 case E_Floating_Point_Subtype:
1395 if (Vax_Float (gnat_entity))
1397 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1403 && Present (Ancestor_Subtype (gnat_entity))
1404 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1405 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1406 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1407 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1408 gnu_expr, definition);
1410 gnu_type = make_node (REAL_TYPE);
1411 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1412 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1414 TYPE_MIN_VALUE (gnu_type)
1415 = convert (TREE_TYPE (gnu_type),
1416 elaborate_expression (Type_Low_Bound (gnat_entity),
1417 gnat_entity, get_identifier ("L"),
1419 Needs_Debug_Info (gnat_entity)));
1421 TYPE_MAX_VALUE (gnu_type)
1422 = convert (TREE_TYPE (gnu_type),
1423 elaborate_expression (Type_High_Bound (gnat_entity),
1424 gnat_entity, get_identifier ("U"),
1426 Needs_Debug_Info (gnat_entity)));
1428 /* One of the above calls might have caused us to be elaborated,
1429 so don't blow up if so. */
1430 if (present_gnu_tree (gnat_entity))
1432 maybe_present = true;
1436 layout_type (gnu_type);
1440 /* Array and String Types and Subtypes
1442 Unconstrained array types are represented by E_Array_Type and
1443 constrained array types are represented by E_Array_Subtype. There
1444 are no actual objects of an unconstrained array type; all we have
1445 are pointers to that type.
1447 The following fields are defined on array types and subtypes:
1449 Component_Type Component type of the array.
1450 Number_Dimensions Number of dimensions (an int).
1451 First_Index Type of first index. */
1456 tree gnu_template_fields = NULL_TREE;
1457 tree gnu_template_type = make_node (RECORD_TYPE);
1458 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1459 tree gnu_fat_type = make_node (RECORD_TYPE);
1460 int ndim = Number_Dimensions (gnat_entity);
1462 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1464 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1465 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1466 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1467 tree gnu_comp_size = 0;
1468 tree gnu_max_size = size_one_node;
1469 tree gnu_max_size_unit;
1471 Entity_Id gnat_ind_subtype;
1472 Entity_Id gnat_ind_base_subtype;
1473 tree gnu_template_reference;
1476 TYPE_NAME (gnu_template_type)
1477 = create_concat_name (gnat_entity, "XUB");
1478 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1479 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1480 TYPE_READONLY (gnu_template_type) = 1;
1482 /* Make a node for the array. If we are not defining the array
1483 suppress expanding incomplete types and save the node as the type
1485 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1488 defer_incomplete_level++;
1489 this_deferred = this_made_decl = true;
1490 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1491 !Comes_From_Source (gnat_entity),
1492 debug_info_p, gnat_entity);
1493 save_gnu_tree (gnat_entity, gnu_decl, false);
1497 /* Build the fat pointer type. Use a "void *" object instead of
1498 a pointer to the array type since we don't have the array type
1499 yet (it will reference the fat pointer via the bounds). */
1500 tem = chainon (chainon (NULL_TREE,
1501 create_field_decl (get_identifier ("P_ARRAY"),
1503 gnu_fat_type, 0, 0, 0, 0)),
1504 create_field_decl (get_identifier ("P_BOUNDS"),
1506 gnu_fat_type, 0, 0, 0, 0));
1508 /* Make sure we can put this into a register. */
1509 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1510 finish_record_type (gnu_fat_type, tem, false, true);
1512 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1513 is the fat pointer. This will be used to access the individual
1514 fields once we build them. */
1515 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1516 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1517 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1518 gnu_template_reference
1519 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1520 TREE_READONLY (gnu_template_reference) = 1;
1522 /* Now create the GCC type for each index and add the fields for
1523 that index to the template. */
1524 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1525 gnat_ind_base_subtype
1526 = First_Index (Implementation_Base_Type (gnat_entity));
1527 index < ndim && index >= 0;
1529 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1530 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1532 char field_name[10];
1533 tree gnu_ind_subtype
1534 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1535 tree gnu_base_subtype
1536 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1538 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1540 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1541 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1543 /* Make the FIELD_DECLs for the minimum and maximum of this
1544 type and then make extractions of that field from the
1546 sprintf (field_name, "LB%d", index);
1547 gnu_min_field = create_field_decl (get_identifier (field_name),
1549 gnu_template_type, 0, 0, 0, 0);
1550 field_name[0] = 'U';
1551 gnu_max_field = create_field_decl (get_identifier (field_name),
1553 gnu_template_type, 0, 0, 0, 0);
1555 Sloc_to_locus (Sloc (gnat_entity),
1556 &DECL_SOURCE_LOCATION (gnu_min_field));
1557 Sloc_to_locus (Sloc (gnat_entity),
1558 &DECL_SOURCE_LOCATION (gnu_max_field));
1559 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1561 /* We can't use build_component_ref here since the template
1562 type isn't complete yet. */
1563 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1564 gnu_template_reference, gnu_min_field,
1566 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1567 gnu_template_reference, gnu_max_field,
1569 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1571 /* Make a range type with the new ranges, but using
1572 the Ada subtype. Then we convert to sizetype. */
1573 gnu_index_types[index]
1574 = create_index_type (convert (sizetype, gnu_min),
1575 convert (sizetype, gnu_max),
1576 build_range_type (gnu_ind_subtype,
1578 /* Update the maximum size of the array, in elements. */
1580 = size_binop (MULT_EXPR, gnu_max_size,
1581 size_binop (PLUS_EXPR, size_one_node,
1582 size_binop (MINUS_EXPR, gnu_base_max,
1585 TYPE_NAME (gnu_index_types[index])
1586 = create_concat_name (gnat_entity, field_name);
1589 for (index = 0; index < ndim; index++)
1591 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1593 /* Install all the fields into the template. */
1594 finish_record_type (gnu_template_type, gnu_template_fields,
1596 TYPE_READONLY (gnu_template_type) = 1;
1598 /* Now make the array of arrays and update the pointer to the array
1599 in the fat pointer. Note that it is the first field. */
1601 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1603 /* Get and validate any specified Component_Size, but if Packed,
1604 ignore it since the front end will have taken care of it. */
1606 = validate_size (Component_Size (gnat_entity), tem,
1608 (Is_Bit_Packed_Array (gnat_entity)
1609 ? TYPE_DECL : VAR_DECL),
1610 true, Has_Component_Size_Clause (gnat_entity));
1612 if (Has_Atomic_Components (gnat_entity))
1613 check_ok_for_atomic (tem, gnat_entity, true);
1615 /* If the component type is a RECORD_TYPE that has a self-referential
1616 size, use the maxium size. */
1617 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1618 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1619 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1621 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1623 tem = make_type_from_size (tem, gnu_comp_size, false);
1624 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1625 "C_PAD", false, definition, true);
1628 if (Has_Volatile_Components (gnat_entity))
1629 tem = build_qualified_type (tem,
1630 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1632 /* If Component_Size is not already specified, annotate it with the
1633 size of the component. */
1634 if (Unknown_Component_Size (gnat_entity))
1635 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1637 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1638 size_binop (MULT_EXPR, gnu_max_size,
1639 TYPE_SIZE_UNIT (tem)));
1640 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1641 size_binop (MULT_EXPR,
1642 convert (bitsizetype,
1646 for (index = ndim - 1; index >= 0; index--)
1648 tem = build_array_type (tem, gnu_index_types[index]);
1649 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1651 /* If the type below this an multi-array type, then this
1652 does not not have aliased components.
1654 ??? Otherwise, for now, we say that any component of aggregate
1655 type is addressable because the front end may take 'Reference
1656 of it. But we have to make it addressable if it must be passed
1657 by reference or it that is the default. */
1658 TYPE_NONALIASED_COMPONENT (tem)
1659 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1660 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1661 : (!Has_Aliased_Components (gnat_entity)
1662 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1665 /* If an alignment is specified, use it if valid. But ignore it for
1666 types that represent the unpacked base type for packed arrays. */
1667 if (No (Packed_Array_Type (gnat_entity))
1668 && Known_Alignment (gnat_entity))
1670 if (No (Alignment (gnat_entity)))
1674 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1678 TYPE_CONVENTION_FORTRAN_P (tem)
1679 = (Convention (gnat_entity) == Convention_Fortran);
1680 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1682 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1683 corresponding fat pointer. */
1684 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1685 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1686 TYPE_MODE (gnu_type) = BLKmode;
1687 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1688 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1690 /* If the maximum size doesn't overflow, use it. */
1691 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1692 && !TREE_OVERFLOW (gnu_max_size))
1694 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1695 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1696 && !TREE_OVERFLOW (gnu_max_size_unit))
1697 TYPE_SIZE_UNIT (tem)
1698 = size_binop (MIN_EXPR, gnu_max_size_unit,
1699 TYPE_SIZE_UNIT (tem));
1701 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1702 tem, NULL, !Comes_From_Source (gnat_entity),
1703 debug_info_p, gnat_entity);
1705 /* Create a record type for the object and its template and
1706 set the template at a negative offset. */
1707 tem = build_unc_object_type (gnu_template_type, tem,
1708 create_concat_name (gnat_entity, "XUT"));
1709 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1710 = size_binop (MINUS_EXPR, size_zero_node,
1711 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1712 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1713 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1714 = bitsize_zero_node;
1715 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1716 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1718 /* Give the thin pointer type a name. */
1719 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1720 build_pointer_type (tem), NULL,
1721 !Comes_From_Source (gnat_entity), debug_info_p,
1726 case E_String_Subtype:
1727 case E_Array_Subtype:
1729 /* This is the actual data type for array variables. Multidimensional
1730 arrays are implemented in the gnu tree as arrays of arrays. Note
1731 that for the moment arrays which have sparse enumeration subtypes as
1732 index components create sparse arrays, which is obviously space
1733 inefficient but so much easier to code for now.
1735 Also note that the subtype never refers to the unconstrained
1736 array type, which is somewhat at variance with Ada semantics.
1738 First check to see if this is simply a renaming of the array
1739 type. If so, the result is the array type. */
1741 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1742 if (!Is_Constrained (gnat_entity))
1747 int array_dim = Number_Dimensions (gnat_entity);
1749 = ((Convention (gnat_entity) == Convention_Fortran)
1750 ? array_dim - 1 : 0);
1752 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1753 Entity_Id gnat_ind_subtype;
1754 Entity_Id gnat_ind_base_subtype;
1755 tree gnu_base_type = gnu_type;
1756 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1757 tree gnu_comp_size = NULL_TREE;
1758 tree gnu_max_size = size_one_node;
1759 tree gnu_max_size_unit;
1760 bool need_index_type_struct = false;
1761 bool max_overflow = false;
1763 /* First create the gnu types for each index. Create types for
1764 debugging information to point to the index types if the
1765 are not integer types, have variable bounds, or are
1766 wider than sizetype. */
1768 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1769 gnat_ind_base_subtype
1770 = First_Index (Implementation_Base_Type (gnat_entity));
1771 index < array_dim && index >= 0;
1773 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1774 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1776 tree gnu_index_subtype
1777 = get_unpadded_type (Etype (gnat_ind_subtype));
1779 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1781 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1782 tree gnu_base_subtype
1783 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1785 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1787 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1788 tree gnu_base_type = get_base_type (gnu_base_subtype);
1789 tree gnu_base_base_min
1790 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1791 tree gnu_base_base_max
1792 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1796 /* If the minimum and maximum values both overflow in
1797 SIZETYPE, but the difference in the original type
1798 does not overflow in SIZETYPE, ignore the overflow
1800 if ((TYPE_PRECISION (gnu_index_subtype)
1801 > TYPE_PRECISION (sizetype)
1802 || TYPE_UNSIGNED (gnu_index_subtype)
1803 != TYPE_UNSIGNED (sizetype))
1804 && TREE_CODE (gnu_min) == INTEGER_CST
1805 && TREE_CODE (gnu_max) == INTEGER_CST
1806 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1808 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1809 TYPE_MAX_VALUE (gnu_index_subtype),
1810 TYPE_MIN_VALUE (gnu_index_subtype))))))
1811 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1812 = TREE_CONSTANT_OVERFLOW (gnu_min)
1813 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1815 /* Similarly, if the range is null, use bounds of 1..0 for
1816 the sizetype bounds. */
1817 else if ((TYPE_PRECISION (gnu_index_subtype)
1818 > TYPE_PRECISION (sizetype)
1819 || TYPE_UNSIGNED (gnu_index_subtype)
1820 != TYPE_UNSIGNED (sizetype))
1821 && TREE_CODE (gnu_min) == INTEGER_CST
1822 && TREE_CODE (gnu_max) == INTEGER_CST
1823 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1824 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1825 TYPE_MIN_VALUE (gnu_index_subtype)))
1826 gnu_min = size_one_node, gnu_max = size_zero_node;
1828 /* Now compute the size of this bound. We need to provide
1829 GCC with an upper bound to use but have to deal with the
1830 "superflat" case. There are three ways to do this. If we
1831 can prove that the array can never be superflat, we can
1832 just use the high bound of the index subtype. If we can
1833 prove that the low bound minus one can't overflow, we
1834 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1835 the expression hb >= lb ? hb : lb - 1. */
1836 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1838 /* See if the base array type is already flat. If it is, we
1839 are probably compiling an ACVC test, but it will cause the
1840 code below to malfunction if we don't handle it specially. */
1841 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1842 && TREE_CODE (gnu_base_max) == INTEGER_CST
1843 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1844 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1845 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1846 gnu_high = size_zero_node, gnu_min = size_one_node;
1848 /* If gnu_high is now an integer which overflowed, the array
1849 cannot be superflat. */
1850 else if (TREE_CODE (gnu_high) == INTEGER_CST
1851 && TREE_OVERFLOW (gnu_high))
1853 else if (TYPE_UNSIGNED (gnu_base_subtype)
1854 || TREE_CODE (gnu_high) == INTEGER_CST)
1855 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1859 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1863 gnu_index_type[index]
1864 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1866 /* Also compute the maximum size of the array. Here we
1867 see if any constraint on the index type of the base type
1868 can be used in the case of self-referential bound on
1869 the index type of the subtype. We look for a non-"infinite"
1870 and non-self-referential bound from any type involved and
1871 handle each bound separately. */
1873 if ((TREE_CODE (gnu_min) == INTEGER_CST
1874 && !TREE_OVERFLOW (gnu_min)
1875 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1876 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1877 gnu_base_min = gnu_min;
1879 if ((TREE_CODE (gnu_max) == INTEGER_CST
1880 && !TREE_OVERFLOW (gnu_max)
1881 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1882 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1883 gnu_base_max = gnu_max;
1885 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1886 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1887 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1888 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1889 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1890 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1891 max_overflow = true;
1893 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1894 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1897 = size_binop (MAX_EXPR,
1898 size_binop (PLUS_EXPR, size_one_node,
1899 size_binop (MINUS_EXPR, gnu_base_max,
1903 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1904 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1905 max_overflow = true;
1908 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1910 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1911 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1913 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1914 || (TREE_TYPE (gnu_index_subtype)
1915 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1917 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1918 || (TYPE_PRECISION (gnu_index_subtype)
1919 > TYPE_PRECISION (sizetype)))
1920 need_index_type_struct = true;
1923 /* Then flatten: create the array of arrays. */
1925 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1927 /* One of the above calls might have caused us to be elaborated,
1928 so don't blow up if so. */
1929 if (present_gnu_tree (gnat_entity))
1931 maybe_present = true;
1935 /* Get and validate any specified Component_Size, but if Packed,
1936 ignore it since the front end will have taken care of it. */
1938 = validate_size (Component_Size (gnat_entity), gnu_type,
1940 (Is_Bit_Packed_Array (gnat_entity)
1941 ? TYPE_DECL : VAR_DECL),
1942 true, Has_Component_Size_Clause (gnat_entity));
1944 /* If the component type is a RECORD_TYPE that has a self-referential
1945 size, use the maxium size. */
1946 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
1947 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
1948 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
1950 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1952 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
1953 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1954 gnat_entity, "C_PAD", false,
1958 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1959 gnu_type = build_qualified_type (gnu_type,
1960 (TYPE_QUALS (gnu_type)
1961 | TYPE_QUAL_VOLATILE));
1963 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1964 TYPE_SIZE_UNIT (gnu_type));
1965 gnu_max_size = size_binop (MULT_EXPR,
1966 convert (bitsizetype, gnu_max_size),
1967 TYPE_SIZE (gnu_type));
1969 for (index = array_dim - 1; index >= 0; index --)
1971 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1972 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1973 /* If the type below this an multi-array type, then this
1974 does not not have aliased components.
1976 ??? Otherwise, for now, we say that any component of aggregate
1977 type is addressable because the front end may take 'Reference
1978 of it. But we have to make it addressable if it must be passed
1979 by reference or it that is the default. */
1980 TYPE_NONALIASED_COMPONENT (gnu_type)
1981 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1982 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
1983 : (!Has_Aliased_Components (gnat_entity)
1984 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
1987 /* If we are at file level and this is a multi-dimensional array, we
1988 need to make a variable corresponding to the stride of the
1989 inner dimensions. */
1990 if (global_bindings_p () && array_dim > 1)
1992 tree gnu_str_name = get_identifier ("ST");
1995 for (gnu_arr_type = TREE_TYPE (gnu_type);
1996 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1997 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1998 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2000 tree eltype = TREE_TYPE (gnu_arr_type);
2002 TYPE_SIZE (gnu_arr_type)
2003 = elaborate_expression_1 (gnat_entity, gnat_entity,
2004 TYPE_SIZE (gnu_arr_type),
2005 gnu_str_name, definition, 0);
2007 /* ??? For now, store the size as a multiple of the
2008 alignment of the element type in bytes so that we
2009 can see the alignment from the tree. */
2010 TYPE_SIZE_UNIT (gnu_arr_type)
2012 (MULT_EXPR, sizetype,
2013 elaborate_expression_1
2014 (gnat_entity, gnat_entity,
2015 build_binary_op (EXACT_DIV_EXPR, sizetype,
2016 TYPE_SIZE_UNIT (gnu_arr_type),
2017 size_int (TYPE_ALIGN (eltype)
2019 concat_id_with_name (gnu_str_name, "A_U"),
2021 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2025 /* If we need to write out a record type giving the names of
2026 the bounds, do it now. */
2027 if (need_index_type_struct && debug_info_p)
2029 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2030 tree gnu_field_list = NULL_TREE;
2033 TYPE_NAME (gnu_bound_rec_type)
2034 = create_concat_name (gnat_entity, "XA");
2036 for (index = array_dim - 1; index >= 0; index--)
2039 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2041 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2042 gnu_type_name = DECL_NAME (gnu_type_name);
2044 gnu_field = create_field_decl (gnu_type_name,
2047 0, NULL_TREE, NULL_TREE, 0);
2048 TREE_CHAIN (gnu_field) = gnu_field_list;
2049 gnu_field_list = gnu_field;
2052 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2056 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2057 = (Convention (gnat_entity) == Convention_Fortran);
2058 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2059 = Is_Packed_Array_Type (gnat_entity);
2061 /* If our size depends on a placeholder and the maximum size doesn't
2062 overflow, use it. */
2063 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2064 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2065 && TREE_OVERFLOW (gnu_max_size))
2066 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2067 && TREE_OVERFLOW (gnu_max_size_unit))
2070 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2071 TYPE_SIZE (gnu_type));
2072 TYPE_SIZE_UNIT (gnu_type)
2073 = size_binop (MIN_EXPR, gnu_max_size_unit,
2074 TYPE_SIZE_UNIT (gnu_type));
2077 /* Set our alias set to that of our base type. This gives all
2078 array subtypes the same alias set. */
2079 copy_alias_set (gnu_type, gnu_base_type);
2082 /* If this is a packed type, make this type the same as the packed
2083 array type, but do some adjusting in the type first. */
2085 if (Present (Packed_Array_Type (gnat_entity)))
2087 Entity_Id gnat_index;
2088 tree gnu_inner_type;
2090 /* First finish the type we had been making so that we output
2091 debugging information for it */
2093 = build_qualified_type (gnu_type,
2094 (TYPE_QUALS (gnu_type)
2095 | (TYPE_QUAL_VOLATILE
2096 * Treat_As_Volatile (gnat_entity))));
2097 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2098 !Comes_From_Source (gnat_entity),
2099 debug_info_p, gnat_entity);
2100 if (!Comes_From_Source (gnat_entity))
2101 DECL_ARTIFICIAL (gnu_decl) = 1;
2103 /* Save it as our equivalent in case the call below elaborates
2105 save_gnu_tree (gnat_entity, gnu_decl, false);
2107 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2109 this_made_decl = true;
2110 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2111 save_gnu_tree (gnat_entity, NULL_TREE, false);
2113 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2114 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2115 || TYPE_IS_PADDING_P (gnu_inner_type)))
2116 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2118 /* We need to point the type we just made to our index type so
2119 the actual bounds can be put into a template. */
2121 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2122 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2123 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2124 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2126 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2128 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2129 If it is, we need to make another type. */
2130 if (TYPE_MODULAR_P (gnu_inner_type))
2134 gnu_subtype = make_node (INTEGER_TYPE);
2136 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2137 TYPE_MIN_VALUE (gnu_subtype)
2138 = TYPE_MIN_VALUE (gnu_inner_type);
2139 TYPE_MAX_VALUE (gnu_subtype)
2140 = TYPE_MAX_VALUE (gnu_inner_type);
2141 TYPE_PRECISION (gnu_subtype)
2142 = TYPE_PRECISION (gnu_inner_type);
2143 TYPE_UNSIGNED (gnu_subtype)
2144 = TYPE_UNSIGNED (gnu_inner_type);
2145 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2146 layout_type (gnu_subtype);
2148 gnu_inner_type = gnu_subtype;
2151 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2154 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2156 for (gnat_index = First_Index (gnat_entity);
2157 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2158 SET_TYPE_ACTUAL_BOUNDS
2160 tree_cons (NULL_TREE,
2161 get_unpadded_type (Etype (gnat_index)),
2162 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2164 if (Convention (gnat_entity) != Convention_Fortran)
2165 SET_TYPE_ACTUAL_BOUNDS
2167 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2169 if (TREE_CODE (gnu_type) == RECORD_TYPE
2170 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2171 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2175 /* Abort if packed array with no packed array type field set. */
2176 else if (Is_Packed (gnat_entity))
2181 case E_String_Literal_Subtype:
2182 /* Create the type for a string literal. */
2184 Entity_Id gnat_full_type
2185 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2186 && Present (Full_View (Etype (gnat_entity)))
2187 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2188 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2189 tree gnu_string_array_type
2190 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2191 tree gnu_string_index_type
2192 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2193 (TYPE_DOMAIN (gnu_string_array_type))));
2194 tree gnu_lower_bound
2195 = convert (gnu_string_index_type,
2196 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2197 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2198 tree gnu_length = ssize_int (length - 1);
2199 tree gnu_upper_bound
2200 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2202 convert (gnu_string_index_type, gnu_length));
2204 = build_range_type (gnu_string_index_type,
2205 gnu_lower_bound, gnu_upper_bound);
2207 = create_index_type (convert (sizetype,
2208 TYPE_MIN_VALUE (gnu_range_type)),
2210 TYPE_MAX_VALUE (gnu_range_type)),
2214 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2219 /* Record Types and Subtypes
2221 The following fields are defined on record types:
2223 Has_Discriminants True if the record has discriminants
2224 First_Discriminant Points to head of list of discriminants
2225 First_Entity Points to head of list of fields
2226 Is_Tagged_Type True if the record is tagged
2228 Implementation of Ada records and discriminated records:
2230 A record type definition is transformed into the equivalent of a C
2231 struct definition. The fields that are the discriminants which are
2232 found in the Full_Type_Declaration node and the elements of the
2233 Component_List found in the Record_Type_Definition node. The
2234 Component_List can be a recursive structure since each Variant of
2235 the Variant_Part of the Component_List has a Component_List.
2237 Processing of a record type definition comprises starting the list of
2238 field declarations here from the discriminants and the calling the
2239 function components_to_record to add the rest of the fields from the
2240 component list and return the gnu type node. The function
2241 components_to_record will call itself recursively as it traverses
2245 if (Has_Complex_Representation (gnat_entity))
2248 = build_complex_type
2250 (Etype (Defining_Entity
2251 (First (Component_Items
2254 (Declaration_Node (gnat_entity)))))))));
2260 Node_Id full_definition = Declaration_Node (gnat_entity);
2261 Node_Id record_definition = Type_Definition (full_definition);
2262 Entity_Id gnat_field;
2264 tree gnu_field_list = NULL_TREE;
2265 tree gnu_get_parent;
2266 int packed = (Is_Packed (gnat_entity) ? 1
2267 : (Component_Alignment (gnat_entity)
2268 == Calign_Storage_Unit) ? -1
2270 bool has_rep = Has_Specified_Layout (gnat_entity);
2271 bool all_rep = has_rep;
2273 = (Is_Tagged_Type (gnat_entity)
2274 && Nkind (record_definition) == N_Derived_Type_Definition);
2276 /* See if all fields have a rep clause. Stop when we find one
2278 for (gnat_field = First_Entity (gnat_entity);
2279 Present (gnat_field) && all_rep;
2280 gnat_field = Next_Entity (gnat_field))
2281 if ((Ekind (gnat_field) == E_Component
2282 || Ekind (gnat_field) == E_Discriminant)
2283 && No (Component_Clause (gnat_field)))
2286 /* If this is a record extension, go a level further to find the
2287 record definition. Also, verify we have a Parent_Subtype. */
2290 if (!type_annotate_only
2291 || Present (Record_Extension_Part (record_definition)))
2292 record_definition = Record_Extension_Part (record_definition);
2294 if (!type_annotate_only && No (Parent_Subtype (gnat_entity)))
2298 /* Make a node for the record. If we are not defining the record,
2299 suppress expanding incomplete types and save the node as the type
2300 for GNAT_ENTITY. We use the same RECORD_TYPE as for a dummy type
2301 and reset TYPE_DUMMY_P to show it's no longer a dummy.
2303 It is very tempting to delay resetting this bit until we are done
2304 with completing the type, e.g. to let possible intermediate
2305 elaboration of access types designating the record know it is not
2306 complete and arrange for update_pointer_to to fix things up later.
2308 It would be wrong, however, because dummy types are expected only
2309 to be created for Ada incomplete or private types, which is not
2310 what we have here. Doing so would make other parts of gigi think
2311 we are dealing with a really incomplete or private type, and have
2312 nasty side effects, typically on the generation of the associated
2313 debugging information. */
2314 gnu_type = make_dummy_type (gnat_entity);
2315 TYPE_DUMMY_P (gnu_type) = 0;
2317 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2318 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2320 TYPE_ALIGN (gnu_type) = 0;
2321 TYPE_PACKED (gnu_type) = packed || has_rep;
2325 defer_incomplete_level++;
2326 this_deferred = true;
2327 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2328 !Comes_From_Source (gnat_entity),
2329 debug_info_p, gnat_entity);
2330 save_gnu_tree (gnat_entity, gnu_decl, false);
2331 this_made_decl = saved = true;
2334 /* If both a size and rep clause was specified, put the size in
2335 the record type now so that it can get the proper mode. */
2336 if (has_rep && Known_Esize (gnat_entity))
2337 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2339 /* Always set the alignment here so that it can be used to
2340 set the mode, if it is making the alignment stricter. If
2341 it is invalid, it will be checked again below. If this is to
2342 be Atomic, choose a default alignment of a word unless we know
2343 the size and it's smaller. */
2344 if (Known_Alignment (gnat_entity))
2345 TYPE_ALIGN (gnu_type)
2346 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2347 else if (Is_Atomic (gnat_entity))
2348 TYPE_ALIGN (gnu_type)
2349 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2350 : 1 << ((floor_log2 (esize) - 1) + 1));
2352 /* If we have a Parent_Subtype, make a field for the parent. If
2353 this record has rep clauses, force the position to zero. */
2354 if (Present (Parent_Subtype (gnat_entity)))
2358 /* A major complexity here is that the parent subtype will
2359 reference our discriminants. But those must reference
2360 the parent component of this record. So here we will
2361 initialize each of those components to a COMPONENT_REF.
2362 The first operand of that COMPONENT_REF is another
2363 COMPONENT_REF which will be filled in below, once
2364 the parent type can be safely built. */
2366 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2367 build0 (PLACEHOLDER_EXPR, gnu_type),
2368 build_decl (FIELD_DECL, NULL_TREE,
2372 if (Has_Discriminants (gnat_entity))
2373 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2374 Present (gnat_field);
2375 gnat_field = Next_Stored_Discriminant (gnat_field))
2376 if (Present (Corresponding_Discriminant (gnat_field)))
2379 build3 (COMPONENT_REF,
2380 get_unpadded_type (Etype (gnat_field)),
2382 gnat_to_gnu_entity (Corresponding_Discriminant
2388 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2391 = create_field_decl (get_identifier
2392 (Get_Name_String (Name_uParent)),
2393 gnu_parent, gnu_type, 0,
2394 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2395 has_rep ? bitsize_zero_node : 0, 1);
2396 DECL_INTERNAL_P (gnu_field_list) = 1;
2398 TREE_TYPE (gnu_get_parent) = gnu_parent;
2399 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2402 /* Add the fields for the discriminants into the record. */
2403 if (!Is_Unchecked_Union (gnat_entity)
2404 && Has_Discriminants (gnat_entity))
2405 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2406 Present (gnat_field);
2407 gnat_field = Next_Stored_Discriminant (gnat_field))
2409 /* If this is a record extension and this discriminant
2410 is the renaming of another discriminant, we've already
2411 handled the discriminant above. */
2412 if (Present (Parent_Subtype (gnat_entity))
2413 && Present (Corresponding_Discriminant (gnat_field)))
2417 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2419 /* Make an expression using a PLACEHOLDER_EXPR from the
2420 FIELD_DECL node just created and link that with the
2421 corresponding GNAT defining identifier. Then add to the
2423 save_gnu_tree (gnat_field,
2424 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2425 build0 (PLACEHOLDER_EXPR,
2426 DECL_CONTEXT (gnu_field)),
2427 gnu_field, NULL_TREE),
2430 TREE_CHAIN (gnu_field) = gnu_field_list;
2431 gnu_field_list = gnu_field;
2434 /* Put the discriminants into the record (backwards), so we can
2435 know the appropriate discriminant to use for the names of the
2437 TYPE_FIELDS (gnu_type) = gnu_field_list;
2439 /* Add the listed fields into the record and finish up. */
2440 components_to_record (gnu_type, Component_List (record_definition),
2441 gnu_field_list, packed, definition, NULL,
2444 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2445 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2447 /* If this is an extension type, reset the tree for any
2448 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2449 for non-inherited discriminants. */
2450 if (!Is_Unchecked_Union (gnat_entity)
2451 && Has_Discriminants (gnat_entity))
2452 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2453 Present (gnat_field);
2454 gnat_field = Next_Stored_Discriminant (gnat_field))
2456 if (Present (Parent_Subtype (gnat_entity))
2457 && Present (Corresponding_Discriminant (gnat_field)))
2458 save_gnu_tree (gnat_field, NULL_TREE, false);
2461 gnu_field = get_gnu_tree (gnat_field);
2462 save_gnu_tree (gnat_field, NULL_TREE, false);
2463 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1),
2468 /* If it is a tagged record force the type to BLKmode to insure
2469 that these objects will always be placed in memory. Do the
2470 same thing for limited record types. */
2471 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2472 TYPE_MODE (gnu_type) = BLKmode;
2474 /* If this is a derived type, we must make the alias set of this type
2475 the same as that of the type we are derived from. We assume here
2476 that the other type is already frozen. */
2477 if (Etype (gnat_entity) != gnat_entity
2478 && !(Is_Private_Type (Etype (gnat_entity))
2479 && Full_View (Etype (gnat_entity)) == gnat_entity))
2480 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2482 /* Fill in locations of fields. */
2483 annotate_rep (gnat_entity, gnu_type);
2485 /* If there are any entities in the chain corresponding to
2486 components that we did not elaborate, ensure we elaborate their
2487 types if they are Itypes. */
2488 for (gnat_temp = First_Entity (gnat_entity);
2489 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2490 if ((Ekind (gnat_temp) == E_Component
2491 || Ekind (gnat_temp) == E_Discriminant)
2492 && Is_Itype (Etype (gnat_temp))
2493 && !present_gnu_tree (gnat_temp))
2494 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2498 case E_Class_Wide_Subtype:
2499 /* If an equivalent type is present, that is what we should use.
2500 Otherwise, fall through to handle this like a record subtype
2501 since it may have constraints. */
2503 if (Present (Equivalent_Type (gnat_entity)))
2505 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2507 maybe_present = true;
2511 /* ... fall through ... */
2513 case E_Record_Subtype:
2515 /* If Cloned_Subtype is Present it means this record subtype has
2516 identical layout to that type or subtype and we should use
2517 that GCC type for this one. The front end guarantees that
2518 the component list is shared. */
2519 if (Present (Cloned_Subtype (gnat_entity)))
2521 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2523 maybe_present = true;
2526 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2527 changing the type, make a new type with each field having the
2528 type of the field in the new subtype but having the position
2529 computed by transforming every discriminant reference according
2530 to the constraints. We don't see any difference between
2531 private and nonprivate type here since derivations from types should
2532 have been deferred until the completion of the private type. */
2535 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2540 defer_incomplete_level++, this_deferred = true;
2542 /* Get the base type initially for its alignment and sizes. But
2543 if it is a padded type, we do all the other work with the
2545 gnu_type = gnu_orig_type = gnu_base_type
2546 = gnat_to_gnu_type (gnat_base_type);
2548 if (TREE_CODE (gnu_type) == RECORD_TYPE
2549 && TYPE_IS_PADDING_P (gnu_type))
2550 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2552 if (present_gnu_tree (gnat_entity))
2554 maybe_present = true;
2558 /* When the type has discriminants, and these discriminants
2559 affect the shape of what it built, factor them in.
2561 If we are making a subtype of an Unchecked_Union (must be an
2562 Itype), just return the type.
2564 We can't just use Is_Constrained because private subtypes without
2565 discriminants of full types with discriminants with default
2566 expressions are Is_Constrained but aren't constrained! */
2568 if (IN (Ekind (gnat_base_type), Record_Kind)
2569 && !Is_For_Access_Subtype (gnat_entity)
2570 && !Is_Unchecked_Union (gnat_base_type)
2571 && Is_Constrained (gnat_entity)
2572 && Stored_Constraint (gnat_entity) != No_Elist
2573 && Present (Discriminant_Constraint (gnat_entity)))
2575 Entity_Id gnat_field;
2576 Entity_Id gnat_root_type;
2577 tree gnu_field_list = 0;
2579 = compute_field_positions (gnu_orig_type, NULL_TREE,
2580 size_zero_node, bitsize_zero_node,
2583 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2587 /* If this is a derived type, we may be seeing fields from any
2588 original records, so add those positions and discriminant
2589 substitutions to our lists. */
2590 for (gnat_root_type = gnat_base_type;
2591 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2592 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2595 = compute_field_positions
2596 (gnat_to_gnu_type (Etype (gnat_root_type)),
2597 gnu_pos_list, size_zero_node, bitsize_zero_node,
2600 if (Present (Parent_Subtype (gnat_root_type)))
2602 = substitution_list (Parent_Subtype (gnat_root_type),
2603 Empty, gnu_subst_list, definition);
2606 gnu_type = make_node (RECORD_TYPE);
2607 TYPE_NAME (gnu_type) = gnu_entity_id;
2608 TYPE_STUB_DECL (gnu_type)
2609 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2611 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2613 for (gnat_field = First_Entity (gnat_entity);
2614 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2615 if (Ekind (gnat_field) == E_Component
2616 || Ekind (gnat_field) == E_Discriminant)
2619 = gnat_to_gnu_entity
2620 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2622 = TREE_VALUE (purpose_member (gnu_old_field,
2624 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2625 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2627 = gnat_to_gnu_type (Etype (gnat_field));
2628 tree gnu_size = TYPE_SIZE (gnu_field_type);
2629 tree gnu_new_pos = 0;
2630 unsigned int offset_align
2631 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2635 /* If there was a component clause, the field types must be
2636 the same for the type and subtype, so copy the data from
2637 the old field to avoid recomputation here. Also if the
2638 field is justified modular and the optimization in
2639 gnat_to_gnu_field was applied. */
2640 if (Present (Component_Clause
2641 (Original_Record_Component (gnat_field)))
2642 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2643 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2644 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2645 == TREE_TYPE (gnu_old_field)))
2647 gnu_size = DECL_SIZE (gnu_old_field);
2648 gnu_field_type = TREE_TYPE (gnu_old_field);
2651 /* If this was a bitfield, get the size from the old field.
2652 Also ensure the type can be placed into a bitfield. */
2653 else if (DECL_BIT_FIELD (gnu_old_field))
2655 gnu_size = DECL_SIZE (gnu_old_field);
2656 if (TYPE_MODE (gnu_field_type) == BLKmode
2657 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2658 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2659 gnu_field_type = make_packable_type (gnu_field_type);
2662 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2663 for (gnu_temp = gnu_subst_list;
2664 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2665 gnu_pos = substitute_in_expr (gnu_pos,
2666 TREE_PURPOSE (gnu_temp),
2667 TREE_VALUE (gnu_temp));
2669 /* If the size is now a constant, we can set it as the
2670 size of the field when we make it. Otherwise, we need
2671 to deal with it specially. */
2672 if (TREE_CONSTANT (gnu_pos))
2673 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2677 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2678 0, gnu_size, gnu_new_pos,
2679 !DECL_NONADDRESSABLE_P (gnu_old_field));
2681 if (!TREE_CONSTANT (gnu_pos))
2683 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2684 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2685 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2686 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2687 DECL_SIZE (gnu_field) = gnu_size;
2688 DECL_SIZE_UNIT (gnu_field)
2689 = convert (sizetype,
2690 size_binop (CEIL_DIV_EXPR, gnu_size,
2691 bitsize_unit_node));
2692 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2695 DECL_INTERNAL_P (gnu_field)
2696 = DECL_INTERNAL_P (gnu_old_field);
2697 SET_DECL_ORIGINAL_FIELD
2698 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2699 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2701 DECL_DISCRIMINANT_NUMBER (gnu_field)
2702 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2703 TREE_THIS_VOLATILE (gnu_field)
2704 = TREE_THIS_VOLATILE (gnu_old_field);
2705 TREE_CHAIN (gnu_field) = gnu_field_list;
2706 gnu_field_list = gnu_field;
2707 save_gnu_tree (gnat_field, gnu_field, false);
2710 finish_record_type (gnu_type, nreverse (gnu_field_list),
2713 /* Now set the size, alignment and alias set of the new type to
2714 match that of the old one, doing any substitutions, as
2716 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2717 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2718 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2719 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2720 copy_alias_set (gnu_type, gnu_base_type);
2722 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2723 for (gnu_temp = gnu_subst_list;
2724 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2725 TYPE_SIZE (gnu_type)
2726 = substitute_in_expr (TYPE_SIZE (gnu_type),
2727 TREE_PURPOSE (gnu_temp),
2728 TREE_VALUE (gnu_temp));
2730 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2731 for (gnu_temp = gnu_subst_list;
2732 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2733 TYPE_SIZE_UNIT (gnu_type)
2734 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2735 TREE_PURPOSE (gnu_temp),
2736 TREE_VALUE (gnu_temp));
2738 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2739 for (gnu_temp = gnu_subst_list;
2740 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2742 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2743 TREE_PURPOSE (gnu_temp),
2744 TREE_VALUE (gnu_temp)));
2746 /* Recompute the mode of this record type now that we know its
2748 compute_record_mode (gnu_type);
2750 /* Fill in locations of fields. */
2751 annotate_rep (gnat_entity, gnu_type);
2754 /* If we've made a new type, record it and make an XVS type to show
2755 what this is a subtype of. Some debuggers require the XVS
2756 type to be output first, so do it in that order. */
2757 if (gnu_type != gnu_orig_type)
2761 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2762 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2764 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2765 gnu_orig_name = DECL_NAME (gnu_orig_name);
2767 TYPE_NAME (gnu_subtype_marker)
2768 = create_concat_name (gnat_entity, "XVS");
2769 finish_record_type (gnu_subtype_marker,
2770 create_field_decl (gnu_orig_name,
2778 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2779 TYPE_NAME (gnu_type) = gnu_entity_id;
2780 TYPE_STUB_DECL (gnu_type)
2781 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2782 NULL, true, debug_info_p, gnat_entity);
2785 /* Otherwise, go down all the components in the new type and
2786 make them equivalent to those in the base type. */
2788 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2789 gnat_temp = Next_Entity (gnat_temp))
2790 if ((Ekind (gnat_temp) == E_Discriminant
2791 && !Is_Unchecked_Union (gnat_base_type))
2792 || Ekind (gnat_temp) == E_Component)
2793 save_gnu_tree (gnat_temp,
2795 (Original_Record_Component (gnat_temp)), false);
2799 case E_Access_Subprogram_Type:
2800 case E_Anonymous_Access_Subprogram_Type:
2801 /* If we are not defining this entity, and we have incomplete
2802 entities being processed above us, make a dummy type and
2803 fill it in later. */
2804 if (!definition && defer_incomplete_level != 0)
2806 struct incomplete *p
2807 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2810 = build_pointer_type
2811 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2812 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2813 !Comes_From_Source (gnat_entity),
2814 debug_info_p, gnat_entity);
2815 save_gnu_tree (gnat_entity, gnu_decl, false);
2816 this_made_decl = saved = true;
2818 p->old_type = TREE_TYPE (gnu_type);
2819 p->full_type = Directly_Designated_Type (gnat_entity);
2820 p->next = defer_incomplete_list;
2821 defer_incomplete_list = p;
2825 /* ... fall through ... */
2827 case E_Allocator_Type:
2829 case E_Access_Attribute_Type:
2830 case E_Anonymous_Access_Type:
2831 case E_General_Access_Type:
2833 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2834 Entity_Id gnat_desig_full
2835 = ((IN (Ekind (Etype (gnat_desig_type)),
2836 Incomplete_Or_Private_Kind))
2837 ? Full_View (gnat_desig_type) : 0);
2838 /* We want to know if we'll be seeing the freeze node for any
2839 incomplete type we may be pointing to. */
2841 = (Present (gnat_desig_full)
2842 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2843 : In_Extended_Main_Code_Unit (gnat_desig_type));
2844 bool got_fat_p = false;
2845 bool made_dummy = false;
2846 tree gnu_desig_type = NULL_TREE;
2847 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2849 if (!targetm.valid_pointer_mode (p_mode))
2852 if (No (gnat_desig_full)
2853 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2854 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2855 && Present (Equivalent_Type (gnat_desig_type)))))
2857 if (Present (Equivalent_Type (gnat_desig_type)))
2859 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2860 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2861 gnat_desig_full = Full_View (gnat_desig_full);
2863 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2864 Incomplete_Or_Private_Kind))
2865 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2868 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2869 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2871 /* If either the designated type or its full view is an
2872 unconstrained array subtype, replace it with the type it's a
2873 subtype of. This avoids problems with multiple copies of
2874 unconstrained array types. */
2875 if (Ekind (gnat_desig_type) == E_Array_Subtype
2876 && !Is_Constrained (gnat_desig_type))
2877 gnat_desig_type = Etype (gnat_desig_type);
2878 if (Present (gnat_desig_full)
2879 && Ekind (gnat_desig_full) == E_Array_Subtype
2880 && !Is_Constrained (gnat_desig_full))
2881 gnat_desig_full = Etype (gnat_desig_full);
2883 /* If the designated type is a subtype of an incomplete record type,
2884 use the parent type to avoid order of elaboration issues. This
2885 can lose some code efficiency, but there is no alternative. */
2886 if (Present (gnat_desig_full)
2887 && Ekind (gnat_desig_full) == E_Record_Subtype
2888 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2889 gnat_desig_full = Etype (gnat_desig_full);
2891 /* If we are pointing to an incomplete type whose completion is an
2892 unconstrained array, make a fat pointer type instead of a pointer
2893 to VOID. The two types in our fields will be pointers to VOID and
2894 will be replaced in update_pointer_to. Similiarly, if the type
2895 itself is a dummy type or an unconstrained array. Also make
2896 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2899 if ((Present (gnat_desig_full)
2900 && Is_Array_Type (gnat_desig_full)
2901 && !Is_Constrained (gnat_desig_full))
2902 || (present_gnu_tree (gnat_desig_type)
2903 && TYPE_IS_DUMMY_P (TREE_TYPE
2904 (get_gnu_tree (gnat_desig_type)))
2905 && Is_Array_Type (gnat_desig_type)
2906 && !Is_Constrained (gnat_desig_type))
2907 || (present_gnu_tree (gnat_desig_type)
2908 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2909 == UNCONSTRAINED_ARRAY_TYPE)
2910 && !(TYPE_POINTER_TO (TREE_TYPE
2911 (get_gnu_tree (gnat_desig_type)))))
2912 || (No (gnat_desig_full) && !in_main_unit
2913 && defer_incomplete_level
2914 && !present_gnu_tree (gnat_desig_type)
2915 && Is_Array_Type (gnat_desig_type)
2916 && !Is_Constrained (gnat_desig_type)))
2919 = (present_gnu_tree (gnat_desig_type)
2920 ? gnat_to_gnu_type (gnat_desig_type)
2921 : make_dummy_type (gnat_desig_type));
2924 /* Show the dummy we get will be a fat pointer. */
2925 got_fat_p = made_dummy = true;
2927 /* If the call above got something that has a pointer, that
2928 pointer is our type. This could have happened either
2929 because the type was elaborated or because somebody
2930 else executed the code below. */
2931 gnu_type = TYPE_POINTER_TO (gnu_old);
2934 gnu_type = make_node (RECORD_TYPE);
2935 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2936 TYPE_POINTER_TO (gnu_old) = gnu_type;
2938 Sloc_to_locus (Sloc (gnat_entity), &input_location);
2940 = chainon (chainon (NULL_TREE,
2942 (get_identifier ("P_ARRAY"),
2943 ptr_void_type_node, gnu_type,
2945 create_field_decl (get_identifier ("P_BOUNDS"),
2947 gnu_type, 0, 0, 0, 0));
2949 /* Make sure we can place this into a register. */
2950 TYPE_ALIGN (gnu_type)
2951 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2952 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2953 finish_record_type (gnu_type, fields, false, true);
2955 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2956 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2957 = concat_id_with_name (get_entity_name (gnat_desig_type),
2959 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2963 /* If we already know what the full type is, use it. */
2964 else if (Present (gnat_desig_full)
2965 && present_gnu_tree (gnat_desig_full))
2966 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2968 /* Get the type of the thing we are to point to and build a pointer
2969 to it. If it is a reference to an incomplete or private type with a
2970 full view that is a record, make a dummy type node and get the
2971 actual type later when we have verified it is safe. */
2972 else if (!in_main_unit
2973 && !present_gnu_tree (gnat_desig_type)
2974 && Present (gnat_desig_full)
2975 && !present_gnu_tree (gnat_desig_full)
2976 && Is_Record_Type (gnat_desig_full))
2978 gnu_desig_type = make_dummy_type (gnat_desig_type);
2982 /* Likewise if we are pointing to a record or array and we are to defer
2983 elaborating incomplete types. We do this since this access type
2984 may be the full view of some private type. Note that the
2985 unconstrained array case is handled above. */
2986 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
2987 && !present_gnu_tree (gnat_desig_type)
2988 && ((Is_Record_Type (gnat_desig_type)
2989 || Is_Array_Type (gnat_desig_type))
2990 || (Present (gnat_desig_full)
2991 && (Is_Record_Type (gnat_desig_full)
2992 || Is_Array_Type (gnat_desig_full)))))
2994 gnu_desig_type = make_dummy_type (gnat_desig_type);
2997 else if (gnat_desig_type == gnat_entity)
3000 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3002 No_Strict_Aliasing (gnat_entity));
3003 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3006 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3008 /* It is possible that the above call to gnat_to_gnu_type resolved our
3009 type. If so, just return it. */
3010 if (present_gnu_tree (gnat_entity))
3012 maybe_present = true;
3016 /* If we have a GCC type for the designated type, possibly modify it
3017 if we are pointing only to constant objects and then make a pointer
3018 to it. Don't do this for unconstrained arrays. */
3019 if (!gnu_type && gnu_desig_type)
3021 if (Is_Access_Constant (gnat_entity)
3022 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3025 = build_qualified_type
3027 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3029 /* Some extra processing is required if we are building a
3030 pointer to an incomplete type (in the GCC sense). We might
3031 have such a type if we just made a dummy, or directly out
3032 of the call to gnat_to_gnu_type above if we are processing
3033 an access type for a record component designating the
3034 record type itself. */
3035 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3037 /* We must ensure that the pointer to variant we make will
3038 be processed by update_pointer_to when the initial type
3039 is completed. Pretend we made a dummy and let further
3040 processing act as usual. */
3043 /* We must ensure that update_pointer_to will not retrieve
3044 the dummy variant when building a properly qualified
3045 version of the complete type. We take advantage of the
3046 fact that get_qualified_type is requiring TYPE_NAMEs to
3047 match to influence build_qualified_type and then also
3048 update_pointer_to here. */
3049 TYPE_NAME (gnu_desig_type)
3050 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3055 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3056 No_Strict_Aliasing (gnat_entity));
3059 /* If we are not defining this object and we made a dummy pointer,
3060 save our current definition, evaluate the actual type, and replace
3061 the tentative type we made with the actual one. If we are to defer
3062 actually looking up the actual type, make an entry in the
3065 if (!in_main_unit && made_dummy)
3068 = TYPE_FAT_POINTER_P (gnu_type)
3069 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3071 if (esize == POINTER_SIZE
3072 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3074 = build_pointer_type
3075 (TYPE_OBJECT_RECORD_TYPE
3076 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3078 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3079 !Comes_From_Source (gnat_entity),
3080 debug_info_p, gnat_entity);
3081 save_gnu_tree (gnat_entity, gnu_decl, false);
3082 this_made_decl = saved = true;
3084 if (defer_incomplete_level == 0)
3085 /* Note that the call to gnat_to_gnu_type here might have
3086 updated gnu_old_type directly, in which case it is not a
3087 dummy type any more when we get into update_pointer_to.
3089 This may happen for instance when the designated type is a
3090 record type, because their elaboration starts with an
3091 initial node from make_dummy_type, which may yield the same
3092 node as the one we got.
3094 Besides, variants of this non-dummy type might have been
3095 created along the way. update_pointer_to is expected to
3096 properly take care of those situations. */
3097 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3098 gnat_to_gnu_type (gnat_desig_type));
3101 struct incomplete *p
3102 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3104 p->old_type = gnu_old_type;
3105 p->full_type = gnat_desig_type;
3106 p->next = defer_incomplete_list;
3107 defer_incomplete_list = p;
3113 case E_Access_Protected_Subprogram_Type:
3114 case E_Anonymous_Access_Protected_Subprogram_Type:
3115 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3116 gnu_type = build_pointer_type (void_type_node);
3118 /* The runtime representation is the equivalent type. */
3119 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3121 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3122 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3123 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3124 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3125 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3130 case E_Access_Subtype:
3132 /* We treat this as identical to its base type; any constraint is
3133 meaningful only to the front end.
3135 The designated type must be elaborated as well, if it does
3136 not have its own freeze node. Designated (sub)types created
3137 for constrained components of records with discriminants are
3138 not frozen by the front end and thus not elaborated by gigi,
3139 because their use may appear before the base type is frozen,
3140 and because it is not clear that they are needed anywhere in
3141 Gigi. With the current model, there is no correct place where
3142 they could be elaborated. */
3144 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3145 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3146 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3147 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3148 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3150 /* If we are not defining this entity, and we have incomplete
3151 entities being processed above us, make a dummy type and
3152 elaborate it later. */
3153 if (!definition && defer_incomplete_level != 0)
3155 struct incomplete *p
3156 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3158 = build_pointer_type
3159 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3161 p->old_type = TREE_TYPE (gnu_ptr_type);
3162 p->full_type = Directly_Designated_Type (gnat_entity);
3163 p->next = defer_incomplete_list;
3164 defer_incomplete_list = p;
3167 (IN (Ekind (Base_Type (Directly_Designated_Type (gnat_entity))),
3168 Incomplete_Or_Private_Kind))
3171 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3175 maybe_present = true;
3178 /* Subprogram Entities
3180 The following access functions are defined for subprograms (functions
3183 First_Formal The first formal parameter.
3184 Is_Imported Indicates that the subprogram has appeared in
3185 an INTERFACE or IMPORT pragma. For now we
3186 assume that the external language is C.
3187 Is_Inlined True if the subprogram is to be inlined.
3189 In addition for function subprograms we have:
3191 Etype Return type of the function.
3193 Each parameter is first checked by calling must_pass_by_ref on its
3194 type to determine if it is passed by reference. For parameters which
3195 are copied in, if they are Ada IN OUT or OUT parameters, their return
3196 value becomes part of a record which becomes the return type of the
3197 function (C function - note that this applies only to Ada procedures
3198 so there is no Ada return type). Additional code to store back the
3199 parameters will be generated on the caller side. This transformation
3200 is done here, not in the front-end.
3202 The intended result of the transformation can be seen from the
3203 equivalent source rewritings that follow:
3205 struct temp {int a,b};
3206 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3208 end P; return {A,B};
3218 For subprogram types we need to perform mainly the same conversions to
3219 GCC form that are needed for procedures and function declarations. The
3220 only difference is that at the end, we make a type declaration instead
3221 of a function declaration. */
3223 case E_Subprogram_Type:
3227 /* The first GCC parameter declaration (a PARM_DECL node). The
3228 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3229 actually is the head of this parameter list. */
3230 tree gnu_param_list = NULL_TREE;
3231 /* The type returned by a function. If the subprogram is a procedure
3232 this type should be void_type_node. */
3233 tree gnu_return_type = void_type_node;
3234 /* List of fields in return type of procedure with copy in copy out
3236 tree gnu_field_list = NULL_TREE;
3237 /* Non-null for subprograms containing parameters passed by copy in
3238 copy out (Ada IN OUT or OUT parameters not passed by reference),
3239 in which case it is the list of nodes used to specify the values of
3240 the in out/out parameters that are returned as a record upon
3241 procedure return. The TREE_PURPOSE of an element of this list is
3242 a field of the record and the TREE_VALUE is the PARM_DECL
3243 corresponding to that field. This list will be saved in the
3244 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3245 tree gnu_return_list = NULL_TREE;
3246 Entity_Id gnat_param;
3247 bool inline_flag = Is_Inlined (gnat_entity);
3248 bool public_flag = Is_Public (gnat_entity);
3250 = (Is_Public (gnat_entity) && !definition) || imported_p;
3251 bool pure_flag = Is_Pure (gnat_entity);
3252 bool volatile_flag = No_Return (gnat_entity);
3253 bool returns_by_ref = false;
3254 bool returns_unconstrained = false;
3255 bool returns_by_target_ptr = false;
3256 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3257 bool has_copy_in_out = false;
3260 if (kind == E_Subprogram_Type && !definition)
3261 /* A parameter may refer to this type, so defer completion
3262 of any incomplete types. */
3263 defer_incomplete_level++, this_deferred = true;
3265 /* If the subprogram has an alias, it is probably inherited, so
3266 we can use the original one. If the original "subprogram"
3267 is actually an enumeration literal, it may be the first use
3268 of its type, so we must elaborate that type now. */
3269 if (Present (Alias (gnat_entity)))
3271 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3272 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3274 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3277 /* Elaborate any Itypes in the parameters of this entity. */
3278 for (gnat_temp = First_Formal (gnat_entity);
3279 Present (gnat_temp);
3280 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3281 if (Is_Itype (Etype (gnat_temp)))
3282 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3287 if (kind == E_Function || kind == E_Subprogram_Type)
3288 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3290 /* If this function returns by reference, make the actual
3291 return type of this function the pointer and mark the decl. */
3292 if (Returns_By_Ref (gnat_entity))
3294 returns_by_ref = true;
3295 gnu_return_type = build_pointer_type (gnu_return_type);
3298 /* If the Mechanism is By_Reference, ensure the return type uses
3299 the machine's by-reference mechanism, which may not the same
3300 as above (e.g., it might be by passing a fake parameter). */
3301 else if (kind == E_Function
3302 && Mechanism (gnat_entity) == By_Reference)
3304 gnu_return_type = copy_type (gnu_return_type);
3305 TREE_ADDRESSABLE (gnu_return_type) = 1;
3308 /* If we are supposed to return an unconstrained array,
3309 actually return a fat pointer and make a note of that. Return
3310 a pointer to an unconstrained record of variable size. */
3311 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3313 gnu_return_type = TREE_TYPE (gnu_return_type);
3314 returns_unconstrained = true;
3317 /* If the type requires a transient scope, the result is allocated
3318 on the secondary stack, so the result type of the function is
3320 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3322 gnu_return_type = build_pointer_type (gnu_return_type);
3323 returns_unconstrained = true;
3326 /* If the type is a padded type and the underlying type would not
3327 be passed by reference or this function has a foreign convention,
3328 return the underlying type. */
3329 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3330 && TYPE_IS_PADDING_P (gnu_return_type)
3331 && (!default_pass_by_ref (TREE_TYPE
3332 (TYPE_FIELDS (gnu_return_type)))
3333 || Has_Foreign_Convention (gnat_entity)))
3334 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3336 /* If the return type is unconstrained, that means it must have a
3337 maximum size. We convert the function into a procedure and its
3338 caller will pass a pointer to an object of that maximum size as the
3339 first parameter when we call the function. */
3340 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3342 returns_by_target_ptr = true;
3344 = create_param_decl (get_identifier ("TARGET"),
3345 build_reference_type (gnu_return_type),
3347 gnu_return_type = void_type_node;
3350 /* If the return type has a size that overflows, we cannot have
3351 a function that returns that type. This usage doesn't make
3352 sense anyway, so give an error here. */
3353 if (TYPE_SIZE_UNIT (gnu_return_type)
3354 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3356 post_error ("cannot return type whose size overflows",
3358 gnu_return_type = copy_node (gnu_return_type);
3359 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3360 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3361 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3362 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3365 /* Look at all our parameters and get the type of
3366 each. While doing this, build a copy-out structure if
3369 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3370 Present (gnat_param);
3371 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3373 tree gnu_param_name = get_entity_name (gnat_param);
3374 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3375 tree gnu_param, gnu_field;
3376 bool by_ref_p = false;
3377 bool by_descr_p = false;
3378 bool by_component_ptr_p = false;
3379 bool copy_in_copy_out_flag = false;
3380 bool req_by_copy = false, req_by_ref = false;
3382 /* See if a Mechanism was supplied that forced this
3383 parameter to be passed one way or another. */
3384 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3386 else if (Mechanism (gnat_param) == Default)
3388 else if (Mechanism (gnat_param) == By_Copy)
3390 else if (Mechanism (gnat_param) == By_Reference)
3392 else if (Mechanism (gnat_param) <= By_Descriptor)
3394 else if (Mechanism (gnat_param) > 0)
3396 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3397 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3398 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3399 Mechanism (gnat_param)))
3405 post_error ("unsupported mechanism for&", gnat_param);
3407 /* If this is either a foreign function or if the
3408 underlying type won't be passed by refererence, strip off
3409 possible padding type. */
3410 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3411 && TYPE_IS_PADDING_P (gnu_param_type)
3412 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3413 || !must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3414 (gnu_param_type)))))
3415 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3417 /* If this is an IN parameter it is read-only, so make a variant
3418 of the type that is read-only.
3420 ??? However, if this is an unconstrained array, that type can
3421 be very complex. So skip it for now. Likewise for any other
3422 self-referential type. */
3423 if (Ekind (gnat_param) == E_In_Parameter
3424 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3425 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3427 = build_qualified_type (gnu_param_type,
3428 (TYPE_QUALS (gnu_param_type)
3429 | TYPE_QUAL_CONST));
3431 /* For foreign conventions, pass arrays as a pointer to the
3432 underlying type. First check for unconstrained array and get
3433 the underlying array. Then get the component type and build
3435 if (Has_Foreign_Convention (gnat_entity)
3436 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3438 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3439 (TREE_TYPE (gnu_param_type))));
3443 = build_pointer_type
3444 (build_vms_descriptor (gnu_param_type,
3445 Mechanism (gnat_param), gnat_entity));
3447 else if (Has_Foreign_Convention (gnat_entity)
3449 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3451 /* Strip off any multi-dimensional entries, then strip
3452 off the last array to get the component type. */
3453 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3454 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3455 gnu_param_type = TREE_TYPE (gnu_param_type);
3457 by_component_ptr_p = true;
3458 gnu_param_type = TREE_TYPE (gnu_param_type);
3460 if (Ekind (gnat_param) == E_In_Parameter)
3462 = build_qualified_type (gnu_param_type,
3463 (TYPE_QUALS (gnu_param_type)
3464 | TYPE_QUAL_CONST));
3466 gnu_param_type = build_pointer_type (gnu_param_type);
3469 /* Fat pointers are passed as thin pointers for foreign
3471 else if (Has_Foreign_Convention (gnat_entity)
3472 && TYPE_FAT_POINTER_P (gnu_param_type))
3474 = make_type_from_size (gnu_param_type,
3475 size_int (POINTER_SIZE), false);
3477 /* If we must pass or were requested to pass by reference, do so.
3478 If we were requested to pass by copy, do so.
3479 Otherwise, for foreign conventions, pass all in out parameters
3480 or aggregates by reference. For COBOL and Fortran, pass
3481 all integer and FP types that way too. For Convention Ada,
3482 use the standard Ada default. */
3483 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3485 && ((Has_Foreign_Convention (gnat_entity)
3486 && (Ekind (gnat_param) != E_In_Parameter
3487 || AGGREGATE_TYPE_P (gnu_param_type)))
3488 || (((Convention (gnat_entity)
3489 == Convention_Fortran)
3490 || (Convention (gnat_entity)
3491 == Convention_COBOL))
3492 && (INTEGRAL_TYPE_P (gnu_param_type)
3493 || FLOAT_TYPE_P (gnu_param_type)))
3494 /* For convention Ada, see if we pass by reference
3496 || (!Has_Foreign_Convention (gnat_entity)
3497 && default_pass_by_ref (gnu_param_type)))))
3499 gnu_param_type = build_reference_type (gnu_param_type);
3503 else if (Ekind (gnat_param) != E_In_Parameter)
3504 copy_in_copy_out_flag = true;
3506 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3507 post_error ("?cannot pass & by copy", gnat_param);
3509 /* If this is an OUT parameter that isn't passed by reference
3510 and isn't a pointer or aggregate, we don't make a PARM_DECL
3511 for it. Instead, it will be a VAR_DECL created when we process
3512 the procedure. For the special parameter of Valued_Procedure,
3515 An exception is made to cover the RM-6.4.1 rule requiring "by
3516 copy" out parameters with discriminants or implicit initial
3517 values to be handled like in out parameters. These type are
3518 normally built as aggregates, and hence passed by reference,
3519 except for some packed arrays which end up encoded in special
3522 The exception we need to make is then for packed arrays of
3523 records with discriminants or implicit initial values. We have
3524 no light/easy way to check for the latter case, so we merely
3525 check for packed arrays of records. This may lead to useless
3526 copy-in operations, but in very rare cases only, as these would
3527 be exceptions in a set of already exceptional situations. */
3528 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3529 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3531 && !POINTER_TYPE_P (gnu_param_type)
3532 && !AGGREGATE_TYPE_P (gnu_param_type)))
3533 && !(Is_Array_Type (Etype (gnat_param))
3534 && Is_Packed (Etype (gnat_param))
3535 && Is_Composite_Type (Component_Type
3536 (Etype (gnat_param)))))
3537 gnu_param = NULL_TREE;
3542 (gnu_param_name, gnu_param_type,
3543 by_ref_p || by_component_ptr_p
3544 || Ekind (gnat_param) == E_In_Parameter);
3546 DECL_BY_REF_P (gnu_param) = by_ref_p;
3547 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3548 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3549 DECL_POINTS_TO_READONLY_P (gnu_param)
3550 = (Ekind (gnat_param) == E_In_Parameter
3551 && (by_ref_p || by_component_ptr_p));
3552 Sloc_to_locus (Sloc (gnat_param),
3553 &DECL_SOURCE_LOCATION (gnu_param));
3554 save_gnu_tree (gnat_param, gnu_param, false);
3555 gnu_param_list = chainon (gnu_param, gnu_param_list);
3557 /* If a parameter is a pointer, this function may modify
3558 memory through it and thus shouldn't be considered
3559 a pure function. Also, the memory may be modified
3560 between two calls, so they can't be CSE'ed. The latter
3561 case also handles by-ref parameters. */
3562 if (POINTER_TYPE_P (gnu_param_type)
3563 || TYPE_FAT_POINTER_P (gnu_param_type))
3567 if (copy_in_copy_out_flag)
3569 if (!has_copy_in_out)
3571 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3574 gnu_return_type = make_node (RECORD_TYPE);
3575 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3576 has_copy_in_out = true;
3579 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3580 gnu_return_type, 0, 0, 0, 0);
3581 Sloc_to_locus (Sloc (gnat_param),
3582 &DECL_SOURCE_LOCATION (gnu_field));
3583 TREE_CHAIN (gnu_field) = gnu_field_list;
3584 gnu_field_list = gnu_field;
3585 gnu_return_list = tree_cons (gnu_field, gnu_param,
3590 /* Do not compute record for out parameters if subprogram is
3591 stubbed since structures are incomplete for the back-end. */
3593 && Convention (gnat_entity) != Convention_Stubbed)
3594 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3597 /* If we have a CICO list but it has only one entry, we convert
3598 this function into a function that simply returns that one
3600 if (list_length (gnu_return_list) == 1)
3601 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3604 if (Convention (gnat_entity) == Convention_Stdcall)
3607 = (struct attrib *) xmalloc (sizeof (struct attrib));
3609 attr->next = attr_list;
3610 attr->type = ATTR_MACHINE_ATTRIBUTE;
3611 attr->name = get_identifier ("stdcall");
3612 attr->arg = NULL_TREE;
3613 attr->error_point = gnat_entity;
3618 /* Both lists ware built in reverse. */
3619 gnu_param_list = nreverse (gnu_param_list);
3620 gnu_return_list = nreverse (gnu_return_list);
3623 = create_subprog_type (gnu_return_type, gnu_param_list,
3624 gnu_return_list, returns_unconstrained,
3626 Function_Returns_With_DSP (gnat_entity),
3627 returns_by_target_ptr);
3629 /* A subprogram (something that doesn't return anything) shouldn't
3630 be considered Pure since there would be no reason for such a
3631 subprogram. Note that procedures with Out (or In Out) parameters
3632 have already been converted into a function with a return type. */
3633 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3637 = build_qualified_type (gnu_type,
3638 (TYPE_QUALS (gnu_type)
3639 | (TYPE_QUAL_CONST * pure_flag)
3640 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3642 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3644 /* If there was no specified Interface_Name and the external and
3645 internal names of the subprogram are the same, only use the
3646 internal name to allow disambiguation of nested subprograms. */
3647 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3648 gnu_ext_name = NULL_TREE;
3650 /* If we are defining the subprogram and it has an Address clause
3651 we must get the address expression from the saved GCC tree for the
3652 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3653 the address expression here since the front-end has guaranteed
3654 in that case that the elaboration has no effects. If there is
3655 an Address clause and we are not defining the object, just
3656 make it a constant. */
3657 if (Present (Address_Clause (gnat_entity)))
3659 tree gnu_address = NULL_TREE;
3663 = (present_gnu_tree (gnat_entity)
3664 ? get_gnu_tree (gnat_entity)
3665 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3667 save_gnu_tree (gnat_entity, NULL_TREE, false);
3669 gnu_type = build_reference_type (gnu_type);
3671 gnu_address = convert (gnu_type, gnu_address);
3674 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3675 gnu_address, false, Is_Public (gnat_entity),
3676 extern_flag, false, NULL, gnat_entity);
3677 DECL_BY_REF_P (gnu_decl) = 1;
3680 else if (kind == E_Subprogram_Type)
3681 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3682 !Comes_From_Source (gnat_entity),
3683 debug_info_p, gnat_entity);
3686 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3687 gnu_type, gnu_param_list,
3688 inline_flag, public_flag,
3689 extern_flag, attr_list,
3691 DECL_STUBBED_P (gnu_decl)
3692 = Convention (gnat_entity) == Convention_Stubbed;
3697 case E_Incomplete_Type:
3698 case E_Private_Type:
3699 case E_Limited_Private_Type:
3700 case E_Record_Type_With_Private:
3701 case E_Private_Subtype:
3702 case E_Limited_Private_Subtype:
3703 case E_Record_Subtype_With_Private:
3705 /* If this type does not have a full view in the unit we are
3706 compiling, then just get the type from its Etype. */
3707 if (No (Full_View (gnat_entity)))
3709 /* If this is an incomplete type with no full view, it must
3710 be a Taft Amendement type, so just return a dummy type. */
3711 if (kind == E_Incomplete_Type)
3712 gnu_type = make_dummy_type (gnat_entity);
3714 else if (Present (Underlying_Full_View (gnat_entity)))
3715 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3719 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3721 maybe_present = true;
3727 /* Otherwise, if we are not defining the type now, get the
3728 type from the full view. But always get the type from the full
3729 view for define on use types, since otherwise we won't see them! */
3731 else if (!definition
3732 || (Is_Itype (Full_View (gnat_entity))
3733 && No (Freeze_Node (gnat_entity)))
3734 || (Is_Itype (gnat_entity)
3735 && No (Freeze_Node (Full_View (gnat_entity)))))
3737 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3739 maybe_present = true;
3743 /* For incomplete types, make a dummy type entry which will be
3745 gnu_type = make_dummy_type (gnat_entity);
3747 /* Save this type as the full declaration's type so we can do any needed
3748 updates when we see it. */
3749 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3750 !Comes_From_Source (gnat_entity),
3751 debug_info_p, gnat_entity);
3752 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3755 /* Simple class_wide types are always viewed as their root_type
3756 by Gigi unless an Equivalent_Type is specified. */
3757 case E_Class_Wide_Type:
3758 if (Present (Equivalent_Type (gnat_entity)))
3759 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3761 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3763 maybe_present = true;
3767 case E_Task_Subtype:
3768 case E_Protected_Type:
3769 case E_Protected_Subtype:
3770 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3771 gnu_type = void_type_node;
3773 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3775 maybe_present = true;
3779 gnu_decl = create_label_decl (gnu_entity_id);
3784 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3785 we've already saved it, so we don't try to. */
3786 gnu_decl = error_mark_node;
3794 /* If we had a case where we evaluated another type and it might have
3795 defined this one, handle it here. */
3796 if (maybe_present && present_gnu_tree (gnat_entity))
3798 gnu_decl = get_gnu_tree (gnat_entity);
3802 /* If we are processing a type and there is either no decl for it or
3803 we just made one, do some common processing for the type, such as
3804 handling alignment and possible padding. */
3806 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3808 if (Is_Tagged_Type (gnat_entity)
3809 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3810 TYPE_ALIGN_OK (gnu_type) = 1;
3812 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3813 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3815 /* ??? Don't set the size for a String_Literal since it is either
3816 confirming or we don't handle it properly (if the low bound is
3818 if (!gnu_size && kind != E_String_Literal_Subtype)
3819 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3821 Has_Size_Clause (gnat_entity));
3823 /* If a size was specified, see if we can make a new type of that size
3824 by rearranging the type, for example from a fat to a thin pointer. */
3828 = make_type_from_size (gnu_type, gnu_size,
3829 Has_Biased_Representation (gnat_entity));
3831 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3832 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3836 /* If the alignment hasn't already been processed and this is
3837 not an unconstrained array, see if an alignment is specified.
3838 If not, we pick a default alignment for atomic objects. */
3839 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3841 else if (Known_Alignment (gnat_entity))
3842 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3843 TYPE_ALIGN (gnu_type));
3844 else if (Is_Atomic (gnat_entity) && !gnu_size
3845 && host_integerp (TYPE_SIZE (gnu_type), 1)
3846 && integer_pow2p (TYPE_SIZE (gnu_type)))
3847 align = MIN (BIGGEST_ALIGNMENT,
3848 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3849 else if (Is_Atomic (gnat_entity) && gnu_size
3850 && host_integerp (gnu_size, 1)
3851 && integer_pow2p (gnu_size))
3852 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3854 /* See if we need to pad the type. If we did, and made a record,
3855 the name of the new type may be changed. So get it back for
3856 us when we make the new TYPE_DECL below. */
3857 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
3858 true, definition, false);
3859 if (TREE_CODE (gnu_type) == RECORD_TYPE
3860 && TYPE_IS_PADDING_P (gnu_type))
3862 gnu_entity_id = TYPE_NAME (gnu_type);
3863 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3864 gnu_entity_id = DECL_NAME (gnu_entity_id);
3867 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3869 /* If we are at global level, GCC will have applied variable_size to
3870 the type, but that won't have done anything. So, if it's not
3871 a constant or self-referential, call elaborate_expression_1 to
3872 make a variable for the size rather than calculating it each time.
3873 Handle both the RM size and the actual size. */
3874 if (global_bindings_p ()
3875 && TYPE_SIZE (gnu_type)
3876 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
3877 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3879 if (TREE_CODE (gnu_type) == RECORD_TYPE
3880 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3881 TYPE_SIZE (gnu_type), 0))
3883 TYPE_SIZE (gnu_type)
3884 = elaborate_expression_1 (gnat_entity, gnat_entity,
3885 TYPE_SIZE (gnu_type),
3886 get_identifier ("SIZE"),
3888 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3892 TYPE_SIZE (gnu_type)
3893 = elaborate_expression_1 (gnat_entity, gnat_entity,
3894 TYPE_SIZE (gnu_type),
3895 get_identifier ("SIZE"),
3898 /* ??? For now, store the size as a multiple of the alignment
3899 in bytes so that we can see the alignment from the tree. */
3900 TYPE_SIZE_UNIT (gnu_type)
3902 (MULT_EXPR, sizetype,
3903 elaborate_expression_1
3904 (gnat_entity, gnat_entity,
3905 build_binary_op (EXACT_DIV_EXPR, sizetype,
3906 TYPE_SIZE_UNIT (gnu_type),
3907 size_int (TYPE_ALIGN (gnu_type)
3909 get_identifier ("SIZE_A_UNIT"),
3911 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3913 if (TREE_CODE (gnu_type) == RECORD_TYPE)
3916 elaborate_expression_1 (gnat_entity,
3918 TYPE_ADA_SIZE (gnu_type),
3919 get_identifier ("RM_SIZE"),
3924 /* If this is a record type or subtype, call elaborate_expression_1 on
3925 any field position. Do this for both global and local types.
3926 Skip any fields that we haven't made trees for to avoid problems with
3927 class wide types. */
3928 if (IN (kind, Record_Kind))
3929 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3930 gnat_temp = Next_Entity (gnat_temp))
3931 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3933 tree gnu_field = get_gnu_tree (gnat_temp);
3935 /* ??? Unfortunately, GCC needs to be able to prove the
3936 alignment of this offset and if it's a variable, it can't.
3937 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
3938 right now, we have to put in an explicit multiply and
3939 divide by that value. */
3940 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
3941 DECL_FIELD_OFFSET (gnu_field)
3943 (MULT_EXPR, sizetype,
3944 elaborate_expression_1
3945 (gnat_temp, gnat_temp,
3946 build_binary_op (EXACT_DIV_EXPR, sizetype,
3947 DECL_FIELD_OFFSET (gnu_field),
3948 size_int (DECL_OFFSET_ALIGN (gnu_field)
3950 get_identifier ("OFFSET"),
3952 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
3955 gnu_type = build_qualified_type (gnu_type,
3956 (TYPE_QUALS (gnu_type)
3957 | (TYPE_QUAL_VOLATILE
3958 * Treat_As_Volatile (gnat_entity))));
3960 if (Is_Atomic (gnat_entity))
3961 check_ok_for_atomic (gnu_type, gnat_entity, false);
3963 if (Known_Alignment (gnat_entity))
3964 TYPE_USER_ALIGN (gnu_type) = 1;
3967 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3968 !Comes_From_Source (gnat_entity),
3969 debug_info_p, gnat_entity);
3971 TREE_TYPE (gnu_decl) = gnu_type;
3974 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3976 gnu_type = TREE_TYPE (gnu_decl);
3978 /* Back-annotate the Alignment of the type if not already in the
3979 tree. Likewise for sizes. */
3980 if (Unknown_Alignment (gnat_entity))
3981 Set_Alignment (gnat_entity,
3982 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3984 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
3986 /* If the size is self-referential, we annotate the maximum
3987 value of that size. */
3988 tree gnu_size = TYPE_SIZE (gnu_type);
3990 if (CONTAINS_PLACEHOLDER_P (gnu_size))
3991 gnu_size = max_size (gnu_size, true);
3993 Set_Esize (gnat_entity, annotate_value (gnu_size));
3995 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
3997 /* In this mode the tag and the parent components are not
3998 generated by the front-end, so the sizes must be adjusted
4004 if (Is_Derived_Type (gnat_entity))
4007 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4008 Set_Alignment (gnat_entity,
4009 Alignment (Etype (Base_Type (gnat_entity))));
4012 size_offset = POINTER_SIZE;
4014 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4015 Set_Esize (gnat_entity,
4016 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4017 / POINTER_SIZE) * POINTER_SIZE));
4018 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4022 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4023 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4026 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4027 DECL_ARTIFICIAL (gnu_decl) = 1;
4029 if (!debug_info_p && DECL_P (gnu_decl)
4030 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
4031 DECL_IGNORED_P (gnu_decl) = 1;
4033 /* If we haven't already, associate the ..._DECL node that we just made with
4034 the input GNAT entity node. */
4036 save_gnu_tree (gnat_entity, gnu_decl, false);
4038 /* If this is an enumeral or floating-point type, we were not able to set
4039 the bounds since they refer to the type. These bounds are always static.
4041 For enumeration types, also write debugging information and declare the
4042 enumeration literal table, if needed. */
4044 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4045 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4047 tree gnu_scalar_type = gnu_type;
4049 /* If this is a padded type, we need to use the underlying type. */
4050 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4051 && TYPE_IS_PADDING_P (gnu_scalar_type))
4052 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4054 /* If this is a floating point type and we haven't set a floating
4055 point type yet, use this in the evaluation of the bounds. */
4056 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4057 longest_float_type_node = gnu_type;
4059 TYPE_MIN_VALUE (gnu_scalar_type)
4060 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4061 TYPE_MAX_VALUE (gnu_scalar_type)
4062 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4064 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4066 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4068 /* Since this has both a typedef and a tag, avoid outputting
4070 DECL_ARTIFICIAL (gnu_decl) = 1;
4071 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4075 /* If we deferred processing of incomplete types, re-enable it. If there
4076 were no other disables and we have some to process, do so. */
4077 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4079 struct incomplete *incp = defer_incomplete_list;
4080 struct incomplete *next;
4082 defer_incomplete_list = NULL;
4083 for (; incp; incp = next)
4088 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4089 gnat_to_gnu_type (incp->full_type));
4094 /* If we are not defining this type, see if it's in the incomplete list.
4095 If so, handle that list entry now. */
4096 else if (!definition)
4098 struct incomplete *incp;
4100 for (incp = defer_incomplete_list; incp; incp = incp->next)
4101 if (incp->old_type && incp->full_type == gnat_entity)
4103 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4104 TREE_TYPE (gnu_decl));
4105 incp->old_type = NULL_TREE;
4112 if (Is_Packed_Array_Type (gnat_entity)
4113 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4114 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4115 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4116 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4121 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4122 be elaborated at the point of its definition, but do nothing else. */
4125 elaborate_entity (Entity_Id gnat_entity)
4127 switch (Ekind (gnat_entity))
4129 case E_Signed_Integer_Subtype:
4130 case E_Modular_Integer_Subtype:
4131 case E_Enumeration_Subtype:
4132 case E_Ordinary_Fixed_Point_Subtype:
4133 case E_Decimal_Fixed_Point_Subtype:
4134 case E_Floating_Point_Subtype:
4136 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4137 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4139 /* ??? Tests for avoiding static constaint error expression
4140 is needed until the front stops generating bogus conversions
4141 on bounds of real types. */
4143 if (!Raises_Constraint_Error (gnat_lb))
4144 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4145 1, 0, Needs_Debug_Info (gnat_entity));
4146 if (!Raises_Constraint_Error (gnat_hb))
4147 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4148 1, 0, Needs_Debug_Info (gnat_entity));
4154 Node_Id full_definition = Declaration_Node (gnat_entity);
4155 Node_Id record_definition = Type_Definition (full_definition);
4157 /* If this is a record extension, go a level further to find the
4158 record definition. */
4159 if (Nkind (record_definition) == N_Derived_Type_Definition)
4160 record_definition = Record_Extension_Part (record_definition);
4164 case E_Record_Subtype:
4165 case E_Private_Subtype:
4166 case E_Limited_Private_Subtype:
4167 case E_Record_Subtype_With_Private:
4168 if (Is_Constrained (gnat_entity)
4169 && Has_Discriminants (Base_Type (gnat_entity))
4170 && Present (Discriminant_Constraint (gnat_entity)))
4172 Node_Id gnat_discriminant_expr;
4173 Entity_Id gnat_field;
4175 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4176 gnat_discriminant_expr
4177 = First_Elmt (Discriminant_Constraint (gnat_entity));
4178 Present (gnat_field);
4179 gnat_field = Next_Discriminant (gnat_field),
4180 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4181 /* ??? For now, ignore access discriminants. */
4182 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4183 elaborate_expression (Node (gnat_discriminant_expr),
4185 get_entity_name (gnat_field), 1, 0, 0);
4192 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4193 any entities on its entity chain similarly. */
4196 mark_out_of_scope (Entity_Id gnat_entity)
4198 Entity_Id gnat_sub_entity;
4199 unsigned int kind = Ekind (gnat_entity);
4201 /* If this has an entity list, process all in the list. */
4202 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4203 || IN (kind, Private_Kind)
4204 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4205 || kind == E_Function || kind == E_Generic_Function
4206 || kind == E_Generic_Package || kind == E_Generic_Procedure
4207 || kind == E_Loop || kind == E_Operator || kind == E_Package
4208 || kind == E_Package_Body || kind == E_Procedure
4209 || kind == E_Record_Type || kind == E_Record_Subtype
4210 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4211 for (gnat_sub_entity = First_Entity (gnat_entity);
4212 Present (gnat_sub_entity);
4213 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4214 if (Scope (gnat_sub_entity) == gnat_entity
4215 && gnat_sub_entity != gnat_entity)
4216 mark_out_of_scope (gnat_sub_entity);
4218 /* Now clear this if it has been defined, but only do so if it isn't
4219 a subprogram or parameter. We could refine this, but it isn't
4220 worth it. If this is statically allocated, it is supposed to
4221 hang around out of cope. */
4222 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4223 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4225 save_gnu_tree (gnat_entity, NULL_TREE, true);
4226 save_gnu_tree (gnat_entity, error_mark_node, true);
4230 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4231 is a multi-dimensional array type, do this recursively. */
4234 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4236 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4237 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4238 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4240 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4241 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4242 so we need to go down to what does. */
4243 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4245 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4247 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4250 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4251 record_component_aliases (gnu_new_type);
4254 /* Return a TREE_LIST describing the substitutions needed to reflect
4255 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4256 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4257 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
4258 gives the tree for the discriminant and TREE_VALUES is the replacement
4259 value. They are in the form of operands to substitute_in_expr.
4260 DEFINITION is as in gnat_to_gnu_entity. */
4263 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4264 tree gnu_list, bool definition)
4266 Entity_Id gnat_discrim;
4270 gnat_type = Implementation_Base_Type (gnat_subtype);
4272 if (Has_Discriminants (gnat_type))
4273 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4274 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4275 Present (gnat_discrim);
4276 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4277 gnat_value = Next_Elmt (gnat_value))
4278 /* Ignore access discriminants. */
4279 if (!Is_Access_Type (Etype (Node (gnat_value))))
4280 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4281 elaborate_expression
4282 (Node (gnat_value), gnat_subtype,
4283 get_entity_name (gnat_discrim), definition,
4290 /* For the following two functions: for each GNAT entity, the GCC
4291 tree node used as a dummy for that entity, if any. */
4293 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4295 /* Initialize the above table. */
4298 init_dummy_type (void)
4302 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4304 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4305 dummy_node_table[gnat_node] = NULL_TREE;
4307 dummy_node_table -= First_Node_Id;
4310 /* Make a dummy type corresponding to GNAT_TYPE. */
4313 make_dummy_type (Entity_Id gnat_type)
4315 Entity_Id gnat_underlying;
4318 /* Find a full type for GNAT_TYPE, taking into account any class wide
4320 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4321 gnat_type = Equivalent_Type (gnat_type);
4322 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4323 gnat_type = Root_Type (gnat_type);
4325 for (gnat_underlying = gnat_type;
4326 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4327 && Present (Full_View (gnat_underlying)));
4328 gnat_underlying = Full_View (gnat_underlying))
4331 /* If it there already a dummy type, use that one. Else make one. */
4332 if (dummy_node_table[gnat_underlying])
4333 return dummy_node_table[gnat_underlying];
4335 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4337 if (Is_Record_Type (gnat_underlying))
4338 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4339 ? UNION_TYPE : RECORD_TYPE);
4341 gnu_type = make_node (ENUMERAL_TYPE);
4343 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4344 TYPE_DUMMY_P (gnu_type) = 1;
4345 if (AGGREGATE_TYPE_P (gnu_type))
4346 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4348 dummy_node_table[gnat_underlying] = gnu_type;
4353 /* Return true if the size represented by GNU_SIZE can be handled by an
4354 allocation. If STATIC_P is true, consider only what can be done with a
4355 static allocation. */
4358 allocatable_size_p (tree gnu_size, bool static_p)
4360 HOST_WIDE_INT our_size;
4362 /* If this is not a static allocation, the only case we want to forbid
4363 is an overflowing size. That will be converted into a raise a
4366 return !(TREE_CODE (gnu_size) == INTEGER_CST
4367 && TREE_CONSTANT_OVERFLOW (gnu_size));
4369 /* Otherwise, we need to deal with both variable sizes and constant
4370 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4371 since assemblers may not like very large sizes. */
4372 if (!host_integerp (gnu_size, 1))
4375 our_size = tree_low_cst (gnu_size, 1);
4376 return (int) our_size == our_size;
4379 /* Return a list of attributes for GNAT_ENTITY, if any. */
4381 static struct attrib *
4382 build_attr_list (Entity_Id gnat_entity)
4384 struct attrib *attr_list = 0;
4387 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4388 gnat_temp = Next_Rep_Item (gnat_temp))
4389 if (Nkind (gnat_temp) == N_Pragma)
4391 struct attrib *attr;
4392 tree gnu_arg0 = 0, gnu_arg1 = 0;
4393 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4394 enum attr_type etype;
4396 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4397 && Present (Next (First (gnat_assoc)))
4398 && (Nkind (Expression (Next (First (gnat_assoc))))
4399 == N_String_Literal))
4401 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4404 (First (gnat_assoc))))));
4405 if (Present (Next (Next (First (gnat_assoc))))
4406 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4407 == N_String_Literal))
4408 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4412 (First (gnat_assoc)))))));
4415 switch (Get_Pragma_Id (Chars (gnat_temp)))
4417 case Pragma_Machine_Attribute:
4418 etype = ATTR_MACHINE_ATTRIBUTE;
4421 case Pragma_Linker_Alias:
4422 etype = ATTR_LINK_ALIAS;
4425 case Pragma_Linker_Section:
4426 etype = ATTR_LINK_SECTION;
4429 case Pragma_Weak_External:
4430 etype = ATTR_WEAK_EXTERNAL;
4437 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4438 attr->next = attr_list;
4440 attr->name = gnu_arg0;
4441 attr->arg = gnu_arg1;
4443 = Present (Next (First (gnat_assoc)))
4444 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4451 /* Get the unpadded version of a GNAT type. */
4454 get_unpadded_type (Entity_Id gnat_entity)
4456 tree type = gnat_to_gnu_type (gnat_entity);
4458 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4459 type = TREE_TYPE (TYPE_FIELDS (type));
4464 /* Called when we need to protect a variable object using a save_expr. */
4467 maybe_variable (tree gnu_operand)
4469 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4470 || TREE_CODE (gnu_operand) == SAVE_EXPR
4471 || TREE_CODE (gnu_operand) == NULL_EXPR)
4474 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4476 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4477 TREE_TYPE (gnu_operand),
4478 variable_size (TREE_OPERAND (gnu_operand, 0)));
4480 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4481 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4485 return variable_size (gnu_operand);
4488 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4489 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4490 return the GCC tree to use for that expression. GNU_NAME is the
4491 qualification to use if an external name is appropriate and DEFINITION is
4492 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4493 we need a result. Otherwise, we are just elaborating this for
4494 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4495 purposes even if it isn't needed for code generation. */
4498 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4499 tree gnu_name, bool definition, bool need_value,
4504 /* If we already elaborated this expression (e.g., it was involved
4505 in the definition of a private type), use the old value. */
4506 if (present_gnu_tree (gnat_expr))
4507 return get_gnu_tree (gnat_expr);
4509 /* If we don't need a value and this is static or a discriment, we
4510 don't need to do anything. */
4511 else if (!need_value
4512 && (Is_OK_Static_Expression (gnat_expr)
4513 || (Nkind (gnat_expr) == N_Identifier
4514 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4517 /* Otherwise, convert this tree to its GCC equivalant. */
4519 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4520 gnu_name, definition, need_debug);
4522 /* Save the expression in case we try to elaborate this entity again. Since
4523 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4524 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4525 save_gnu_tree (gnat_expr, gnu_expr, true);
4527 return need_value ? gnu_expr : error_mark_node;
4530 /* Similar, but take a GNU expression. */
4533 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4534 tree gnu_expr, tree gnu_name, bool definition,
4537 tree gnu_decl = NULL_TREE;
4538 /* Strip any conversions to see if the expression is a readonly variable.
4539 ??? This really should remain readonly, but we have to think about
4540 the typing of the tree here. */
4541 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4542 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4545 /* In most cases, we won't see a naked FIELD_DECL here because a
4546 discriminant reference will have been replaced with a COMPONENT_REF
4547 when the type is being elaborated. However, there are some cases
4548 involving child types where we will. So convert it to a COMPONENT_REF
4549 here. We have to hope it will be at the highest level of the
4550 expression in these cases. */
4551 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4552 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4553 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4554 gnu_expr, NULL_TREE);
4556 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4557 that is a constant, make a variable that is initialized to contain the
4558 bound when the package containing the definition is elaborated. If
4559 this entity is defined at top level and a bound or discriminant value
4560 isn't a constant or a reference to a discriminant, replace the bound
4561 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4562 rely here on the fact that an expression cannot contain both the
4563 discriminant and some other variable. */
4565 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4566 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4567 && TREE_READONLY (gnu_inner_expr))
4568 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4570 /* If this is a static expression or contains a discriminant, we don't
4571 need the variable for debugging (and can't elaborate anyway if a
4574 && (Is_OK_Static_Expression (gnat_expr)
4575 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4578 /* Now create the variable if we need it. */
4579 if (need_debug || (expr_variable && expr_global))
4581 = create_var_decl (create_concat_name (gnat_entity,
4582 IDENTIFIER_POINTER (gnu_name)),
4583 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true,
4584 Is_Public (gnat_entity), !definition, false, NULL,
4587 /* We only need to use this variable if we are in global context since GCC
4588 can do the right thing in the local case. */
4589 if (expr_global && expr_variable)
4591 else if (!expr_variable)
4594 return maybe_variable (gnu_expr);
4597 /* Create a record type that contains a field of TYPE with a starting bit
4598 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4601 make_aligning_type (tree type, int align, tree size)
4603 tree record_type = make_node (RECORD_TYPE);
4604 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4605 tree size_addr_place = convert (sizetype,
4606 build_unary_op (ADDR_EXPR, NULL_TREE,
4608 tree name = TYPE_NAME (type);
4611 if (TREE_CODE (name) == TYPE_DECL)
4612 name = DECL_NAME (name);
4614 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4616 /* The bit position is obtained by "and"ing the alignment minus 1
4617 with the two's complement of the address and multiplying
4618 by the number of bits per unit. Do all this in sizetype. */
4620 pos = size_binop (MULT_EXPR,
4621 convert (bitsizetype,
4622 size_binop (BIT_AND_EXPR,
4623 size_diffop (size_zero_node,
4625 ssize_int ((align / BITS_PER_UNIT)
4629 field = create_field_decl (get_identifier ("F"), type, record_type,
4631 DECL_BIT_FIELD (field) = 0;
4633 finish_record_type (record_type, field, true, false);
4634 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4635 TYPE_SIZE (record_type)
4636 = size_binop (PLUS_EXPR,
4637 size_binop (MULT_EXPR, convert (bitsizetype, size),
4639 bitsize_int (align));
4640 TYPE_SIZE_UNIT (record_type)
4641 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4642 copy_alias_set (record_type, type);
4646 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4647 being used as the field type of a packed record. See if we can rewrite it
4648 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4649 return the new type. If not, return the original type. */
4652 make_packable_type (tree type)
4654 tree new_type = make_node (TREE_CODE (type));
4655 tree field_list = NULL_TREE;
4658 /* Copy the name and flags from the old type to that of the new and set
4659 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4660 also copy the size. */
4661 TYPE_NAME (new_type) = TYPE_NAME (type);
4662 TYPE_JUSTIFIED_MODULAR_P (new_type)
4663 = TYPE_JUSTIFIED_MODULAR_P (type);
4664 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4666 if (TREE_CODE (type) == RECORD_TYPE)
4667 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4668 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4670 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4671 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4674 TYPE_ALIGN (new_type)
4675 = ((HOST_WIDE_INT) 1
4676 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4678 /* Now copy the fields, keeping the position and size. */
4679 for (old_field = TYPE_FIELDS (type); old_field;
4680 old_field = TREE_CHAIN (old_field))
4682 tree new_field_type = TREE_TYPE (old_field);
4685 if (TYPE_MODE (new_field_type) == BLKmode
4686 && (TREE_CODE (new_field_type) == RECORD_TYPE
4687 || TREE_CODE (new_field_type) == UNION_TYPE
4688 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4689 && host_integerp (TYPE_SIZE (new_field_type), 1))
4690 new_field_type = make_packable_type (new_field_type);
4692 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4693 new_type, TYPE_PACKED (type),
4694 DECL_SIZE (old_field),
4695 bit_position (old_field),
4696 !DECL_NONADDRESSABLE_P (old_field));
4698 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4699 SET_DECL_ORIGINAL_FIELD
4700 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4701 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4703 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4704 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4706 TREE_CHAIN (new_field) = field_list;
4707 field_list = new_field;
4710 finish_record_type (new_type, nreverse (field_list), true, true);
4711 copy_alias_set (new_type, type);
4712 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4715 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4716 if needed. We have already verified that SIZE and TYPE are large enough.
4718 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4721 IS_USER_TYPE is true if we must be sure we complete the original type.
4723 DEFINITION is true if this type is being defined.
4725 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4726 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4730 maybe_pad_type (tree type, tree size, unsigned int align,
4731 Entity_Id gnat_entity, const char *name_trailer,
4732 bool is_user_type, bool definition, bool same_rm_size)
4734 tree orig_size = TYPE_SIZE (type);
4738 /* If TYPE is a padded type, see if it agrees with any size and alignment
4739 we were given. If so, return the original type. Otherwise, strip
4740 off the padding, since we will either be returning the inner type
4741 or repadding it. If no size or alignment is specified, use that of
4742 the original padded type. */
4744 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4747 || operand_equal_p (round_up (size,
4748 MAX (align, TYPE_ALIGN (type))),
4749 round_up (TYPE_SIZE (type),
4750 MAX (align, TYPE_ALIGN (type))),
4752 && (align == 0 || align == TYPE_ALIGN (type)))
4756 size = TYPE_SIZE (type);
4758 align = TYPE_ALIGN (type);
4760 type = TREE_TYPE (TYPE_FIELDS (type));
4761 orig_size = TYPE_SIZE (type);
4764 /* If the size is either not being changed or is being made smaller (which
4765 is not done here (and is only valid for bitfields anyway), show the size
4766 isn't changing. Likewise, clear the alignment if it isn't being
4767 changed. Then return if we aren't doing anything. */
4770 && (operand_equal_p (size, orig_size, 0)
4771 || (TREE_CODE (orig_size) == INTEGER_CST
4772 && tree_int_cst_lt (size, orig_size))))
4775 if (align == TYPE_ALIGN (type))
4778 if (align == 0 && !size)
4781 /* We used to modify the record in place in some cases, but that could
4782 generate incorrect debugging information. So make a new record
4784 record = make_node (RECORD_TYPE);
4786 if (Present (gnat_entity))
4787 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4789 /* If we were making a type, complete the original type and give it a
4792 create_type_decl (get_entity_name (gnat_entity), type,
4793 NULL, !Comes_From_Source (gnat_entity),
4795 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4796 && DECL_IGNORED_P (TYPE_NAME (type))),
4799 /* If we are changing the alignment and the input type is a record with
4800 BLKmode and a small constant size, try to make a form that has an
4801 integral mode. That might allow this record to have an integral mode,
4802 which will be much more efficient. There is no point in doing this if a
4803 size is specified unless it is also smaller than the biggest alignment
4804 and it is incorrect to do this if the size of the original type is not a
4805 multiple of the alignment. */
4807 && TREE_CODE (type) == RECORD_TYPE
4808 && TYPE_MODE (type) == BLKmode
4809 && host_integerp (orig_size, 1)
4810 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4812 || (TREE_CODE (size) == INTEGER_CST
4813 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4814 && tree_low_cst (orig_size, 1) % align == 0)
4815 type = make_packable_type (type);
4817 field = create_field_decl (get_identifier ("F"), type, record, 0,
4818 NULL_TREE, bitsize_zero_node, 1);
4820 DECL_INTERNAL_P (field) = 1;
4821 TYPE_SIZE (record) = size ? size : orig_size;
4822 TYPE_SIZE_UNIT (record)
4823 = convert (sizetype,
4824 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4825 bitsize_unit_node));
4826 TYPE_ALIGN (record) = align;
4827 TYPE_IS_PADDING_P (record) = 1;
4828 TYPE_VOLATILE (record)
4829 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
4830 finish_record_type (record, field, true, false);
4832 /* Keep the RM_Size of the padded record as that of the old record
4834 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
4836 /* Unless debugging information isn't being written for the input type,
4837 write a record that shows what we are a subtype of and also make a
4838 variable that indicates our size, if variable. */
4839 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
4840 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4841 || !DECL_IGNORED_P (TYPE_NAME (type))))
4843 tree marker = make_node (RECORD_TYPE);
4844 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
4845 ? DECL_NAME (TYPE_NAME (record))
4846 : TYPE_NAME (record));
4847 tree orig_name = TYPE_NAME (type);
4849 if (TREE_CODE (orig_name) == TYPE_DECL)
4850 orig_name = DECL_NAME (orig_name);
4852 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4853 finish_record_type (marker,
4854 create_field_decl (orig_name, integer_type_node,
4855 marker, 0, NULL_TREE, NULL_TREE,
4859 if (size && TREE_CODE (size) != INTEGER_CST && definition)
4860 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4861 sizetype, TYPE_SIZE (record), false, false, false,
4862 false, NULL, gnat_entity);
4867 if (CONTAINS_PLACEHOLDER_P (orig_size))
4868 orig_size = max_size (orig_size, true);
4870 /* If the size was widened explicitly, maybe give a warning. */
4871 if (size && Present (gnat_entity)
4872 && !operand_equal_p (size, orig_size, 0)
4873 && !(TREE_CODE (size) == INTEGER_CST
4874 && TREE_CODE (orig_size) == INTEGER_CST
4875 && tree_int_cst_lt (size, orig_size)))
4877 Node_Id gnat_error_node = Empty;
4879 if (Is_Packed_Array_Type (gnat_entity))
4880 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4882 if ((Ekind (gnat_entity) == E_Component
4883 || Ekind (gnat_entity) == E_Discriminant)
4884 && Present (Component_Clause (gnat_entity)))
4885 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4886 else if (Present (Size_Clause (gnat_entity)))
4887 gnat_error_node = Expression (Size_Clause (gnat_entity));
4889 /* Generate message only for entities that come from source, since
4890 if we have an entity created by expansion, the message will be
4891 generated for some other corresponding source entity. */
4892 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4893 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4895 size_diffop (size, orig_size));
4897 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
4898 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4899 gnat_entity, gnat_entity,
4900 size_diffop (size, orig_size));
4906 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4907 the value passed against the list of choices. */
4910 choices_to_gnu (tree operand, Node_Id choices)
4914 tree result = integer_zero_node;
4915 tree this_test, low = 0, high = 0, single = 0;
4917 for (choice = First (choices); Present (choice); choice = Next (choice))
4919 switch (Nkind (choice))
4922 low = gnat_to_gnu (Low_Bound (choice));
4923 high = gnat_to_gnu (High_Bound (choice));
4925 /* There's no good type to use here, so we might as well use
4926 integer_type_node. */
4928 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4929 build_binary_op (GE_EXPR, integer_type_node,
4931 build_binary_op (LE_EXPR, integer_type_node,
4936 case N_Subtype_Indication:
4937 gnat_temp = Range_Expression (Constraint (choice));
4938 low = gnat_to_gnu (Low_Bound (gnat_temp));
4939 high = gnat_to_gnu (High_Bound (gnat_temp));
4942 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4943 build_binary_op (GE_EXPR, integer_type_node,
4945 build_binary_op (LE_EXPR, integer_type_node,
4950 case N_Expanded_Name:
4951 /* This represents either a subtype range, an enumeration
4952 literal, or a constant Ekind says which. If an enumeration
4953 literal or constant, fall through to the next case. */
4954 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4955 && Ekind (Entity (choice)) != E_Constant)
4957 tree type = gnat_to_gnu_type (Entity (choice));
4959 low = TYPE_MIN_VALUE (type);
4960 high = TYPE_MAX_VALUE (type);
4963 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4964 build_binary_op (GE_EXPR, integer_type_node,
4966 build_binary_op (LE_EXPR, integer_type_node,
4970 /* ... fall through ... */
4971 case N_Character_Literal:
4972 case N_Integer_Literal:
4973 single = gnat_to_gnu (choice);
4974 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4978 case N_Others_Choice:
4979 this_test = integer_one_node;
4986 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4993 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4994 placed in GNU_RECORD_TYPE.
4996 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4997 record has a Component_Alignment of Storage_Unit.
4999 DEFINITION is true if this field is for a record being defined. */
5002 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5005 tree gnu_field_id = get_entity_name (gnat_field);
5006 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5007 tree gnu_orig_field_type = gnu_field_type;
5011 bool needs_strict_alignment
5012 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5013 || Treat_As_Volatile (gnat_field));
5015 /* If this field requires strict alignment or contains an item of
5016 variable sized, pretend it isn't packed. */
5017 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5020 /* For packed records, this is one of the few occasions on which we use
5021 the official RM size for discrete or fixed-point components, instead
5022 of the normal GNAT size stored in Esize. See description in Einfo:
5023 "Handling of Type'Size Values" for further details. */
5026 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5027 gnat_field, FIELD_DECL, false, true);
5029 if (Known_Static_Esize (gnat_field))
5030 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5031 gnat_field, FIELD_DECL, false, true);
5033 /* If the field's type is justified modular, the wrapper can prevent
5034 packing so we make the field the type of the inner object unless the
5035 situation forbids it. We may not do that when the field is addressable_p,
5036 typically because in that case this field may later be passed by-ref for
5037 a formal argument expecting the justification. The condition below
5038 is then matching the addressable_p code for COMPONENT_REF. */
5039 if (!Is_Aliased (gnat_field) && flag_strict_aliasing
5040 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5041 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type))
5042 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5044 /* If we are packing this record, have a specified size that's smaller than
5045 that of the field type, or a position is specified, and the field type
5046 is also a record that's BLKmode and with a small constant size, see if
5047 we can get a better form of the type that allows more packing. If we
5048 can, show a size was specified for it if there wasn't one so we know to
5049 make this a bitfield and avoid making things wider. */
5050 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5051 && TYPE_MODE (gnu_field_type) == BLKmode
5052 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5053 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5055 || (gnu_size && tree_int_cst_lt (gnu_size,
5056 TYPE_SIZE (gnu_field_type)))
5057 || Present (Component_Clause (gnat_field))))
5059 gnu_field_type = make_packable_type (gnu_field_type);
5061 if (gnu_field_type != gnu_orig_field_type && !gnu_size)
5062 gnu_size = rm_size (gnu_field_type);
5065 /* If we are packing the record and the field is BLKmode, round the
5066 size up to a byte boundary. */
5067 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5068 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5070 if (Present (Component_Clause (gnat_field)))
5072 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5073 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5074 gnat_field, FIELD_DECL, false, true);
5076 /* Ensure the position does not overlap with the parent subtype,
5078 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5081 = gnat_to_gnu_type (Parent_Subtype
5082 (Underlying_Type (Scope (gnat_field))));
5084 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5085 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5088 ("offset of& must be beyond parent{, minimum allowed is ^}",
5089 First_Bit (Component_Clause (gnat_field)), gnat_field,
5090 TYPE_SIZE_UNIT (gnu_parent));
5094 /* If this field needs strict alignment, ensure the record is
5095 sufficiently aligned and that that position and size are
5096 consistent with the alignment. */
5097 if (needs_strict_alignment)
5099 tree gnu_min_size = round_up (rm_size (gnu_field_type),
5100 TYPE_ALIGN (gnu_field_type));
5102 TYPE_ALIGN (gnu_record_type)
5103 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5105 /* If Atomic, the size must match exactly and if aliased, the size
5106 must not be less than the rounded size. */
5107 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5108 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5111 ("atomic field& must be natural size of type{ (^)}",
5112 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5113 TYPE_SIZE (gnu_field_type));
5115 gnu_size = NULL_TREE;
5118 else if (Is_Aliased (gnat_field)
5119 && gnu_size && tree_int_cst_lt (gnu_size, gnu_min_size))
5122 ("size of aliased field& too small{, minimum required is ^}",
5123 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5125 gnu_size = NULL_TREE;
5128 if (!integer_zerop (size_binop
5129 (TRUNC_MOD_EXPR, gnu_pos,
5130 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5132 if (Is_Aliased (gnat_field))
5134 ("position of aliased field& must be multiple of ^ bits",
5135 First_Bit (Component_Clause (gnat_field)), gnat_field,
5136 TYPE_ALIGN (gnu_field_type));
5138 else if (Treat_As_Volatile (gnat_field))
5140 ("position of volatile field& must be multiple of ^ bits",
5141 First_Bit (Component_Clause (gnat_field)), gnat_field,
5142 TYPE_ALIGN (gnu_field_type));
5144 else if (Strict_Alignment (Etype (gnat_field)))
5146 ("position of & with aliased or tagged components not multiple of ^ bits",
5147 First_Bit (Component_Clause (gnat_field)), gnat_field,
5148 TYPE_ALIGN (gnu_field_type));
5152 gnu_pos = NULL_TREE;
5156 if (Is_Atomic (gnat_field))
5157 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5160 /* If the record has rep clauses and this is the tag field, make a rep
5161 clause for it as well. */
5162 else if (Has_Specified_Layout (Scope (gnat_field))
5163 && Chars (gnat_field) == Name_uTag)
5165 gnu_pos = bitsize_zero_node;
5166 gnu_size = TYPE_SIZE (gnu_field_type);
5169 /* We need to make the size the maximum for the type if it is
5170 self-referential and an unconstrained type. In that case, we can't
5171 pack the field since we can't make a copy to align it. */
5172 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5174 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5175 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5177 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5181 /* If no size is specified (or if there was an error), don't specify a
5184 gnu_pos = NULL_TREE;
5187 /* Unless this field is aliased, we can remove any justified
5188 modular type since it's only needed in the unchecked conversion
5189 case, which doesn't apply here. */
5190 if (!needs_strict_alignment
5191 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5192 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type))
5193 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5196 = make_type_from_size (gnu_field_type, gnu_size,
5197 Has_Biased_Representation (gnat_field));
5198 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5199 "PAD", false, definition, true);
5202 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5203 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
5206 /* Now create the decl for the field. */
5207 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5208 packed, gnu_size, gnu_pos,
5209 Is_Aliased (gnat_field));
5210 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5211 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5213 if (Ekind (gnat_field) == E_Discriminant)
5214 DECL_DISCRIMINANT_NUMBER (gnu_field)
5215 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5220 /* Return true if TYPE is a type with variable size, a padding type with a
5221 field of variable size or is a record that has a field such a field. */
5224 is_variable_size (tree type)
5228 /* We need not be concerned about this at all if we don't have
5229 strict alignment. */
5230 if (!STRICT_ALIGNMENT)
5232 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5234 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5235 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5237 else if (TREE_CODE (type) != RECORD_TYPE
5238 && TREE_CODE (type) != UNION_TYPE
5239 && TREE_CODE (type) != QUAL_UNION_TYPE)
5242 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5243 if (is_variable_size (TREE_TYPE (field)))
5249 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5250 of GCC trees for fields that are in the record and have already been
5251 processed. When called from gnat_to_gnu_entity during the processing of a
5252 record type definition, the GCC nodes for the discriminants will be on
5253 the chain. The other calls to this function are recursive calls from
5254 itself for the Component_List of a variant and the chain is empty.
5256 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5257 for a record type with "pragma component_alignment (storage_unit)".
5259 DEFINITION is true if we are defining this record.
5261 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5262 with a rep clause is to be added. If it is nonzero, that is all that
5263 should be done with such fields.
5265 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5266 laying out the record. This means the alignment only serves to force fields
5267 to be bitfields, but not require the record to be that aligned. This is
5270 ALL_REP, if true, means a rep clause was found for all the fields. This
5271 simplifies the logic since we know we're not in the mixed case.
5273 The processing of the component list fills in the chain with all of the
5274 fields of the record and then the record type is finished. */
5277 components_to_record (tree gnu_record_type, Node_Id component_list,
5278 tree gnu_field_list, int packed, bool definition,
5279 tree *p_gnu_rep_list, bool cancel_alignment,
5282 Node_Id component_decl;
5283 Entity_Id gnat_field;
5284 Node_Id variant_part;
5286 tree gnu_our_rep_list = NULL_TREE;
5287 tree gnu_field, gnu_last;
5288 bool layout_with_rep = false;
5289 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5291 /* For each variable within each component declaration create a GCC field
5292 and add it to the list, skipping any pragmas in the list. */
5294 if (Present (Component_Items (component_list)))
5295 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5296 Present (component_decl);
5297 component_decl = Next_Non_Pragma (component_decl))
5299 gnat_field = Defining_Entity (component_decl);
5301 if (Chars (gnat_field) == Name_uParent)
5302 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5305 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5306 packed, definition);
5308 /* If this is the _Tag field, put it before any discriminants,
5309 instead of after them as is the case for all other fields.
5310 Ignore field of void type if only annotating. */
5311 if (Chars (gnat_field) == Name_uTag)
5312 gnu_field_list = chainon (gnu_field_list, gnu_field);
5315 TREE_CHAIN (gnu_field) = gnu_field_list;
5316 gnu_field_list = gnu_field;
5320 save_gnu_tree (gnat_field, gnu_field, false);
5323 /* At the end of the component list there may be a variant part. */
5324 variant_part = Variant_Part (component_list);
5326 /* If this is an unchecked union, each variant must have exactly one
5327 component, each of which becomes one component of this union. */
5328 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5329 for (variant = First_Non_Pragma (Variants (variant_part));
5331 variant = Next_Non_Pragma (variant))
5334 = First_Non_Pragma (Component_Items (Component_List (variant)));
5335 gnat_field = Defining_Entity (component_decl);
5336 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5338 TREE_CHAIN (gnu_field) = gnu_field_list;
5339 gnu_field_list = gnu_field;
5340 save_gnu_tree (gnat_field, gnu_field, false);
5343 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5344 mutually exclusive and should go in the same memory. To do this we need
5345 to treat each variant as a record whose elements are created from the
5346 component list for the variant. So here we create the records from the
5347 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5348 else if (Present (variant_part))
5350 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5352 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5353 tree gnu_union_field;
5354 tree gnu_variant_list = NULL_TREE;
5355 tree gnu_name = TYPE_NAME (gnu_record_type);
5357 = concat_id_with_name
5358 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5361 if (TREE_CODE (gnu_name) == TYPE_DECL)
5362 gnu_name = DECL_NAME (gnu_name);
5364 TYPE_NAME (gnu_union_type)
5365 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5366 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5368 for (variant = First_Non_Pragma (Variants (variant_part));
5370 variant = Next_Non_Pragma (variant))
5372 tree gnu_variant_type = make_node (RECORD_TYPE);
5373 tree gnu_inner_name;
5376 Get_Variant_Encoding (variant);
5377 gnu_inner_name = get_identifier (Name_Buffer);
5378 TYPE_NAME (gnu_variant_type)
5379 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5380 IDENTIFIER_POINTER (gnu_inner_name));
5382 /* Set the alignment of the inner type in case we need to make
5383 inner objects into bitfields, but then clear it out
5384 so the record actually gets only the alignment required. */
5385 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5386 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5388 /* Similarly, if the outer record has a size specified and all fields
5389 have record rep clauses, we can propagate the size into the
5391 if (all_rep_and_size)
5393 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5394 TYPE_SIZE_UNIT (gnu_variant_type)
5395 = TYPE_SIZE_UNIT (gnu_record_type);
5398 components_to_record (gnu_variant_type, Component_List (variant),
5399 NULL_TREE, packed, definition,
5400 &gnu_our_rep_list, !all_rep_and_size, all_rep);
5402 gnu_qual = choices_to_gnu (gnu_discriminant,
5403 Discrete_Choices (variant));
5405 Set_Present_Expr (variant, annotate_value (gnu_qual));
5406 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5409 ? TYPE_SIZE (gnu_record_type) : 0),
5411 ? bitsize_zero_node : 0),
5414 DECL_INTERNAL_P (gnu_field) = 1;
5415 DECL_QUALIFIER (gnu_field) = gnu_qual;
5416 TREE_CHAIN (gnu_field) = gnu_variant_list;
5417 gnu_variant_list = gnu_field;
5420 /* We use to delete the empty variants from the end. However,
5421 we no longer do that because we need them to generate complete
5422 debugging information for the variant record. Otherwise,
5423 the union type definition will be missing the fields associated
5424 to these empty variants. */
5426 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5427 if (gnu_variant_list)
5429 if (all_rep_and_size)
5431 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5432 TYPE_SIZE_UNIT (gnu_union_type)
5433 = TYPE_SIZE_UNIT (gnu_record_type);
5436 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5437 all_rep_and_size, false);
5440 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5442 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5443 all_rep ? bitsize_zero_node : 0, 0);
5445 DECL_INTERNAL_P (gnu_union_field) = 1;
5446 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5447 gnu_field_list = gnu_union_field;
5451 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5452 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5453 in a separate pass since we want to handle the discriminants but can't
5454 play with them until we've used them in debugging data above.
5456 ??? Note: if we then reorder them, debugging information will be wrong,
5457 but there's nothing that can be done about this at the moment. */
5459 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5461 if (DECL_FIELD_OFFSET (gnu_field))
5463 tree gnu_next = TREE_CHAIN (gnu_field);
5466 gnu_field_list = gnu_next;
5468 TREE_CHAIN (gnu_last) = gnu_next;
5470 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5471 gnu_our_rep_list = gnu_field;
5472 gnu_field = gnu_next;
5476 gnu_last = gnu_field;
5477 gnu_field = TREE_CHAIN (gnu_field);
5481 /* If we have any items in our rep'ed field list, it is not the case that all
5482 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5483 set it and ignore the items. Otherwise, sort the fields by bit position
5484 and put them into their own record if we have any fields without
5486 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5487 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5488 else if (gnu_our_rep_list)
5491 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5492 int len = list_length (gnu_our_rep_list);
5493 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5496 /* Set DECL_SECTION_NAME to increasing integers so we have a
5498 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5499 gnu_field = TREE_CHAIN (gnu_field), i++)
5501 gnu_arr[i] = gnu_field;
5502 DECL_SECTION_NAME (gnu_field) = size_int (i);
5505 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5507 /* Put the fields in the list in order of increasing position, which
5508 means we start from the end. */
5509 gnu_our_rep_list = NULL_TREE;
5510 for (i = len - 1; i >= 0; i--)
5512 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5513 gnu_our_rep_list = gnu_arr[i];
5514 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5515 DECL_SECTION_NAME (gnu_arr[i]) = NULL_TREE;
5520 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5521 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5522 gnu_record_type, 0, 0, 0, 1);
5523 DECL_INTERNAL_P (gnu_field) = 1;
5524 gnu_field_list = chainon (gnu_field_list, gnu_field);
5528 layout_with_rep = true;
5529 gnu_field_list = nreverse (gnu_our_rep_list);
5533 if (cancel_alignment)
5534 TYPE_ALIGN (gnu_record_type) = 0;
5536 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5537 layout_with_rep, false);
5540 /* Called via qsort from the above. Returns -1, 1, depending on the
5541 bit positions and ordinals of the two fields. */
5544 compare_field_bitpos (const PTR rt1, const PTR rt2)
5546 tree *t1 = (tree *) rt1;
5547 tree *t2 = (tree *) rt2;
5549 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5551 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5553 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5559 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5560 placed into an Esize, Component_Bit_Offset, or Component_Size value
5561 in the GNAT tree. */
5564 annotate_value (tree gnu_size)
5566 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5568 Node_Ref_Or_Val ops[3], ret;
5572 /* If back annotation is suppressed by the front end, return No_Uint */
5573 if (!Back_Annotate_Rep_Info)
5576 /* See if we've already saved the value for this node. */
5577 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5578 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5580 /* If we do not return inside this switch, TCODE will be set to the
5581 code to use for a Create_Node operand and LEN (set above) will be
5582 the number of recursive calls for us to make. */
5584 switch (TREE_CODE (gnu_size))
5587 if (TREE_OVERFLOW (gnu_size))
5590 /* This may have come from a conversion from some smaller type,
5591 so ensure this is in bitsizetype. */
5592 gnu_size = convert (bitsizetype, gnu_size);
5594 /* For negative values, use NEGATE_EXPR of the supplied value. */
5595 if (tree_int_cst_sgn (gnu_size) < 0)
5597 /* The rediculous code below is to handle the case of the largest
5598 negative integer. */
5599 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5600 bool adjust = false;
5603 if (TREE_CONSTANT_OVERFLOW (negative_size))
5606 = size_binop (MINUS_EXPR, bitsize_zero_node,
5607 size_binop (PLUS_EXPR, gnu_size,
5612 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5614 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5616 return annotate_value (temp);
5619 if (!host_integerp (gnu_size, 1))
5622 size = tree_low_cst (gnu_size, 1);
5624 /* This peculiar test is to make sure that the size fits in an int
5625 on machines where HOST_WIDE_INT is not "int". */
5626 if (tree_low_cst (gnu_size, 1) == size)
5627 return UI_From_Int (size);
5632 /* The only case we handle here is a simple discriminant reference. */
5633 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5634 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5635 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5636 return Create_Node (Discrim_Val,
5637 annotate_value (DECL_DISCRIMINANT_NUMBER
5638 (TREE_OPERAND (gnu_size, 1))),
5643 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5644 return annotate_value (TREE_OPERAND (gnu_size, 0));
5646 /* Now just list the operations we handle. */
5647 case COND_EXPR: tcode = Cond_Expr; break;
5648 case PLUS_EXPR: tcode = Plus_Expr; break;
5649 case MINUS_EXPR: tcode = Minus_Expr; break;
5650 case MULT_EXPR: tcode = Mult_Expr; break;
5651 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5652 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5653 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5654 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5655 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5656 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5657 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5658 case NEGATE_EXPR: tcode = Negate_Expr; break;
5659 case MIN_EXPR: tcode = Min_Expr; break;
5660 case MAX_EXPR: tcode = Max_Expr; break;
5661 case ABS_EXPR: tcode = Abs_Expr; break;
5662 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5663 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5664 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5665 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5666 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5667 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5668 case LT_EXPR: tcode = Lt_Expr; break;
5669 case LE_EXPR: tcode = Le_Expr; break;
5670 case GT_EXPR: tcode = Gt_Expr; break;
5671 case GE_EXPR: tcode = Ge_Expr; break;
5672 case EQ_EXPR: tcode = Eq_Expr; break;
5673 case NE_EXPR: tcode = Ne_Expr; break;
5679 /* Now get each of the operands that's relevant for this code. If any
5680 cannot be expressed as a repinfo node, say we can't. */
5681 for (i = 0; i < 3; i++)
5684 for (i = 0; i < len; i++)
5686 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5687 if (ops[i] == No_Uint)
5691 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5692 TREE_COMPLEXITY (gnu_size) = ret;
5696 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5697 GCC type, set Component_Bit_Offset and Esize to the position and size
5701 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5705 Entity_Id gnat_field;
5707 /* We operate by first making a list of all field and their positions
5708 (we can get the sizes easily at any time) by a recursive call
5709 and then update all the sizes into the tree. */
5710 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5711 size_zero_node, bitsize_zero_node,
5714 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5715 gnat_field = Next_Entity (gnat_field))
5716 if ((Ekind (gnat_field) == E_Component
5717 || (Ekind (gnat_field) == E_Discriminant
5718 && !Is_Unchecked_Union (Scope (gnat_field)))))
5720 tree parent_offset = bitsize_zero_node;
5723 = purpose_member (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0),
5728 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5730 /* In this mode the tag and parent components have not been
5731 generated, so we add the appropriate offset to each
5732 component. For a component appearing in the current
5733 extension, the offset is the size of the parent. */
5734 if (Is_Derived_Type (gnat_entity)
5735 && Original_Record_Component (gnat_field) == gnat_field)
5737 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5740 parent_offset = bitsize_int (POINTER_SIZE);
5743 Set_Component_Bit_Offset
5746 (size_binop (PLUS_EXPR,
5747 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5748 TREE_VALUE (TREE_VALUE
5749 (TREE_VALUE (gnu_entry)))),
5752 Set_Esize (gnat_field,
5753 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5755 else if (type_annotate_only
5756 && Is_Tagged_Type (gnat_entity)
5757 && Is_Derived_Type (gnat_entity))
5759 /* If there is no gnu_entry, this is an inherited component whose
5760 position is the same as in the parent type. */
5761 Set_Component_Bit_Offset
5763 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5764 Set_Esize (gnat_field,
5765 Esize (Original_Record_Component (gnat_field)));
5770 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5771 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5772 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5773 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5774 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5775 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5779 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
5780 tree gnu_bitpos, unsigned int offset_align)
5783 tree gnu_result = gnu_list;
5785 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5786 gnu_field = TREE_CHAIN (gnu_field))
5788 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5789 DECL_FIELD_BIT_OFFSET (gnu_field));
5790 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5791 DECL_FIELD_OFFSET (gnu_field));
5792 unsigned int our_offset_align
5793 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5796 = tree_cons (gnu_field,
5797 tree_cons (gnu_our_offset,
5798 tree_cons (size_int (our_offset_align),
5799 gnu_our_bitpos, NULL_TREE),
5803 if (DECL_INTERNAL_P (gnu_field))
5805 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5806 gnu_our_offset, gnu_our_bitpos,
5813 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5814 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5815 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5816 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5817 for the size of a field. COMPONENT_P is true if we are being called
5818 to process the Component_Size of GNAT_OBJECT. This is used for error
5819 message handling and to indicate to use the object size of GNU_TYPE.
5820 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
5821 it means that a size of zero should be treated as an unspecified size. */
5824 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
5825 enum tree_code kind, bool component_p, bool zero_ok)
5827 Node_Id gnat_error_node;
5829 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5832 /* Find the node to use for errors. */
5833 if ((Ekind (gnat_object) == E_Component
5834 || Ekind (gnat_object) == E_Discriminant)
5835 && Present (Component_Clause (gnat_object)))
5836 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5837 else if (Present (Size_Clause (gnat_object)))
5838 gnat_error_node = Expression (Size_Clause (gnat_object));
5840 gnat_error_node = gnat_object;
5842 /* Return 0 if no size was specified, either because Esize was not Present or
5843 the specified size was zero. */
5844 if (No (uint_size) || uint_size == No_Uint)
5847 /* Get the size as a tree. Give an error if a size was specified, but cannot
5848 be represented as in sizetype. */
5849 size = UI_To_gnu (uint_size, bitsizetype);
5850 if (TREE_OVERFLOW (size))
5852 post_error_ne (component_p ? "component size of & is too large"
5853 : "size of & is too large",
5854 gnat_error_node, gnat_object);
5858 /* Ignore a negative size since that corresponds to our back-annotation.
5859 Also ignore a zero size unless a size clause exists. */
5860 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
5863 /* The size of objects is always a multiple of a byte. */
5864 if (kind == VAR_DECL
5865 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
5868 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5869 gnat_error_node, gnat_object);
5871 post_error_ne ("size for& is not a multiple of Storage_Unit",
5872 gnat_error_node, gnat_object);
5876 /* If this is an integral type or a packed array type, the front-end has
5877 verified the size, so we need not do it here (which would entail
5878 checking against the bounds). However, if this is an aliased object, it
5879 may not be smaller than the type of the object. */
5880 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
5881 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
5884 /* If the object is a record that contains a template, add the size of
5885 the template to the specified size. */
5886 if (TREE_CODE (gnu_type) == RECORD_TYPE
5887 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5888 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5890 /* Modify the size of the type to be that of the maximum size if it has a
5891 discriminant or the size of a thin pointer if this is a fat pointer. */
5892 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
5893 type_size = max_size (type_size, true);
5894 else if (TYPE_FAT_POINTER_P (gnu_type))
5895 type_size = bitsize_int (POINTER_SIZE);
5897 /* If this is an access type, the minimum size is that given by the smallest
5898 integral mode that's valid for pointers. */
5899 if (TREE_CODE (gnu_type) == POINTER_TYPE)
5901 enum machine_mode p_mode;
5903 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5904 !targetm.valid_pointer_mode (p_mode);
5905 p_mode = GET_MODE_WIDER_MODE (p_mode))
5908 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
5911 /* If the size of the object is a constant, the new size must not be
5913 if (TREE_CODE (type_size) != INTEGER_CST
5914 || TREE_OVERFLOW (type_size)
5915 || tree_int_cst_lt (size, type_size))
5919 ("component size for& too small{, minimum allowed is ^}",
5920 gnat_error_node, gnat_object, type_size);
5922 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5923 gnat_error_node, gnat_object, type_size);
5925 if (kind == VAR_DECL && !component_p
5926 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5927 && !tree_int_cst_lt (size, rm_size (gnu_type)))
5928 post_error_ne_tree_2
5929 ("\\size of ^ is not a multiple of alignment (^ bits)",
5930 gnat_error_node, gnat_object, rm_size (gnu_type),
5931 TYPE_ALIGN (gnu_type));
5933 else if (INTEGRAL_TYPE_P (gnu_type))
5934 post_error_ne ("\\size would be legal if & were not aliased!",
5935 gnat_error_node, gnat_object);
5943 /* Similarly, but both validate and process a value of RM_Size. This
5944 routine is only called for types. */
5947 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
5949 /* Only give an error if a Value_Size clause was explicitly given.
5950 Otherwise, we'd be duplicating an error on the Size clause. */
5951 Node_Id gnat_attr_node
5952 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5953 tree old_size = rm_size (gnu_type);
5956 /* Get the size as a tree. Do nothing if none was specified, either
5957 because RM_Size was not Present or if the specified size was zero.
5958 Give an error if a size was specified, but cannot be represented as
5960 if (No (uint_size) || uint_size == No_Uint)
5963 size = UI_To_gnu (uint_size, bitsizetype);
5964 if (TREE_OVERFLOW (size))
5966 if (Present (gnat_attr_node))
5967 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5973 /* Ignore a negative size since that corresponds to our back-annotation.
5974 Also ignore a zero size unless a size clause exists, a Value_Size
5975 clause exists, or this is an integer type, in which case the
5976 front end will have always set it. */
5977 else if (tree_int_cst_sgn (size) < 0
5978 || (integer_zerop (size) && No (gnat_attr_node)
5979 && !Has_Size_Clause (gnat_entity)
5980 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5983 /* If the old size is self-referential, get the maximum size. */
5984 if (CONTAINS_PLACEHOLDER_P (old_size))
5985 old_size = max_size (old_size, true);
5987 /* If the size of the object is a constant, the new size must not be
5988 smaller (the front end checks this for scalar types). */
5989 if (TREE_CODE (old_size) != INTEGER_CST
5990 || TREE_OVERFLOW (old_size)
5991 || (AGGREGATE_TYPE_P (gnu_type)
5992 && tree_int_cst_lt (size, old_size)))
5994 if (Present (gnat_attr_node))
5996 ("Value_Size for& too small{, minimum allowed is ^}",
5997 gnat_attr_node, gnat_entity, old_size);
6002 /* Otherwise, set the RM_Size. */
6003 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6004 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6005 TYPE_RM_SIZE_NUM (gnu_type) = size;
6006 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6007 TYPE_RM_SIZE_NUM (gnu_type) = size;
6008 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6009 || TREE_CODE (gnu_type) == UNION_TYPE
6010 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6011 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6012 SET_TYPE_ADA_SIZE (gnu_type, size);
6015 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6016 If TYPE is the best type, return it. Otherwise, make a new type. We
6017 only support new integral and pointer types. BIASED_P is nonzero if
6018 we are making a biased type. */
6021 make_type_from_size (tree type, tree size_tree, bool biased_p)
6024 unsigned HOST_WIDE_INT size;
6027 /* If size indicates an error, just return TYPE to avoid propagating the
6028 error. Likewise if it's too large to represent. */
6029 if (!size_tree || !host_integerp (size_tree, 1))
6032 size = tree_low_cst (size_tree, 1);
6033 switch (TREE_CODE (type))
6037 /* Only do something if the type is not already the proper size and is
6038 not a packed array type. */
6039 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6040 || (TYPE_PRECISION (type) == size
6041 && biased_p == (TREE_CODE (type) == INTEGER_CST
6042 && TYPE_BIASED_REPRESENTATION_P (type))))
6045 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6046 && TYPE_BIASED_REPRESENTATION_P (type));
6047 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6049 size = MIN (size, LONG_LONG_TYPE_SIZE);
6051 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6052 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6053 TYPE_MIN_VALUE (new_type)
6054 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6055 TYPE_MAX_VALUE (new_type)
6056 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6057 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6058 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6062 /* Do something if this is a fat pointer, in which case we
6063 may need to return the thin pointer. */
6064 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6067 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6071 /* Only do something if this is a thin pointer, in which case we
6072 may need to return the fat pointer. */
6073 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6075 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6086 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6087 a type or object whose present alignment is ALIGN. If this alignment is
6088 valid, return it. Otherwise, give an error and return ALIGN. */
6091 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6093 Node_Id gnat_error_node = gnat_entity;
6094 unsigned int new_align;
6096 #ifndef MAX_OFILE_ALIGNMENT
6097 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6100 if (Present (Alignment_Clause (gnat_entity)))
6101 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6103 /* Don't worry about checking alignment if alignment was not specified
6104 by the source program and we already posted an error for this entity. */
6106 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6109 /* Within GCC, an alignment is an integer, so we must make sure a
6110 value is specified that fits in that range. Also, alignments of
6111 more than MAX_OFILE_ALIGNMENT can't be supported. */
6113 if (! UI_Is_In_Int_Range (alignment)
6114 || ((new_align = UI_To_Int (alignment))
6115 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6116 post_error_ne_num ("largest supported alignment for& is ^",
6117 gnat_error_node, gnat_entity,
6118 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6119 else if (!(Present (Alignment_Clause (gnat_entity))
6120 && From_At_Mod (Alignment_Clause (gnat_entity)))
6121 && new_align * BITS_PER_UNIT < align)
6122 post_error_ne_num ("alignment for& must be at least ^",
6123 gnat_error_node, gnat_entity,
6124 align / BITS_PER_UNIT);
6126 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6131 /* Verify that OBJECT, a type or decl, is something we can implement
6132 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6133 if we require atomic components. */
6136 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6138 Node_Id gnat_error_point = gnat_entity;
6140 enum machine_mode mode;
6144 /* There are three case of what OBJECT can be. It can be a type, in which
6145 case we take the size, alignment and mode from the type. It can be a
6146 declaration that was indirect, in which case the relevant values are
6147 that of the type being pointed to, or it can be a normal declaration,
6148 in which case the values are of the decl. The code below assumes that
6149 OBJECT is either a type or a decl. */
6150 if (TYPE_P (object))
6152 mode = TYPE_MODE (object);
6153 align = TYPE_ALIGN (object);
6154 size = TYPE_SIZE (object);
6156 else if (DECL_BY_REF_P (object))
6158 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6159 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6160 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6164 mode = DECL_MODE (object);
6165 align = DECL_ALIGN (object);
6166 size = DECL_SIZE (object);
6169 /* Consider all floating-point types atomic and any types that that are
6170 represented by integers no wider than a machine word. */
6171 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6172 || ((GET_MODE_CLASS (mode) == MODE_INT
6173 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6174 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6177 /* For the moment, also allow anything that has an alignment equal
6178 to its size and which is smaller than a word. */
6179 if (size && TREE_CODE (size) == INTEGER_CST
6180 && compare_tree_int (size, align) == 0
6181 && align <= BITS_PER_WORD)
6184 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6185 gnat_node = Next_Rep_Item (gnat_node))
6187 if (!comp_p && Nkind (gnat_node) == N_Pragma
6188 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6189 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6190 else if (comp_p && Nkind (gnat_node) == N_Pragma
6191 && (Get_Pragma_Id (Chars (gnat_node))
6192 == Pragma_Atomic_Components))
6193 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6197 post_error_ne ("atomic access to component of & cannot be guaranteed",
6198 gnat_error_point, gnat_entity);
6200 post_error_ne ("atomic access to & cannot be guaranteed",
6201 gnat_error_point, gnat_entity);
6204 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6205 with all size expressions that contain F updated by replacing F with R.
6206 This is identical to GCC's substitute_in_type except that it knows about
6207 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6208 nothing has changed. */
6211 gnat_substitute_in_type (tree t, tree f, tree r)
6216 switch (TREE_CODE (t))
6222 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6223 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6225 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6226 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6228 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6231 new = build_range_type (TREE_TYPE (t), low, high);
6232 if (TYPE_INDEX_TYPE (t))
6234 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6241 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6242 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6244 tree low = NULL_TREE, high = NULL_TREE;
6246 if (TYPE_MIN_VALUE (t))
6247 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6248 if (TYPE_MAX_VALUE (t))
6249 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6251 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6255 TYPE_MIN_VALUE (t) = low;
6256 TYPE_MAX_VALUE (t) = high;
6261 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6262 if (tem == TREE_TYPE (t))
6265 return build_complex_type (tem);
6273 /* Don't know how to do these yet. */
6278 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6279 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6281 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6284 new = build_array_type (component, domain);
6285 TYPE_SIZE (new) = 0;
6286 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6287 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6289 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6291 /* If we had bounded the sizes of T by a constant, bound the sizes of
6292 NEW by the same constant. */
6293 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6295 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6297 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6298 TYPE_SIZE_UNIT (new)
6299 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6300 TYPE_SIZE_UNIT (new));
6306 case QUAL_UNION_TYPE:
6310 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6311 bool field_has_rep = false;
6312 tree last_field = NULL_TREE;
6314 tree new = copy_type (t);
6316 /* Start out with no fields, make new fields, and chain them
6317 in. If we haven't actually changed the type of any field,
6318 discard everything we've done and return the old type. */
6320 TYPE_FIELDS (new) = NULL_TREE;
6321 TYPE_SIZE (new) = NULL_TREE;
6323 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6325 tree new_field = copy_node (field);
6327 TREE_TYPE (new_field)
6328 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6330 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6331 field_has_rep = true;
6332 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6333 changed_field = true;
6335 /* If this is an internal field and the type of this field is
6336 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6337 the type just has one element, treat that as the field.
6338 But don't do this if we are processing a QUAL_UNION_TYPE. */
6339 if (TREE_CODE (t) != QUAL_UNION_TYPE
6340 && DECL_INTERNAL_P (new_field)
6341 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6342 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6344 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6347 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6350 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6352 /* Make sure omitting the union doesn't change
6354 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6355 new_field = next_new_field;
6359 DECL_CONTEXT (new_field) = new;
6360 SET_DECL_ORIGINAL_FIELD (new_field,
6361 (DECL_ORIGINAL_FIELD (field)
6362 ? DECL_ORIGINAL_FIELD (field) : field));
6364 /* If the size of the old field was set at a constant,
6365 propagate the size in case the type's size was variable.
6366 (This occurs in the case of a variant or discriminated
6367 record with a default size used as a field of another
6369 DECL_SIZE (new_field)
6370 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6371 ? DECL_SIZE (field) : NULL_TREE;
6372 DECL_SIZE_UNIT (new_field)
6373 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6374 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6376 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6378 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6380 if (new_q != DECL_QUALIFIER (new_field))
6381 changed_field = true;
6383 /* Do the substitution inside the qualifier and if we find
6384 that this field will not be present, omit it. */
6385 DECL_QUALIFIER (new_field) = new_q;
6387 if (integer_zerop (DECL_QUALIFIER (new_field)))
6392 TYPE_FIELDS (new) = new_field;
6394 TREE_CHAIN (last_field) = new_field;
6396 last_field = new_field;
6398 /* If this is a qualified type and this field will always be
6399 present, we are done. */
6400 if (TREE_CODE (t) == QUAL_UNION_TYPE
6401 && integer_onep (DECL_QUALIFIER (new_field)))
6405 /* If this used to be a qualified union type, but we now know what
6406 field will be present, make this a normal union. */
6407 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6408 && (!TYPE_FIELDS (new)
6409 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6410 TREE_SET_CODE (new, UNION_TYPE);
6411 else if (!changed_field)
6419 /* If the size was originally a constant use it. */
6420 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6421 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6423 TYPE_SIZE (new) = TYPE_SIZE (t);
6424 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6425 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6436 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6437 needed to represent the object. */
6440 rm_size (tree gnu_type)
6442 /* For integer types, this is the precision. For record types, we store
6443 the size explicitly. For other types, this is just the size. */
6445 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6446 return TYPE_RM_SIZE (gnu_type);
6447 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6448 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6449 /* Return the rm_size of the actual data plus the size of the template. */
6451 size_binop (PLUS_EXPR,
6452 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6453 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6454 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6455 || TREE_CODE (gnu_type) == UNION_TYPE
6456 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6457 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6458 && TYPE_ADA_SIZE (gnu_type))
6459 return TYPE_ADA_SIZE (gnu_type);
6461 return TYPE_SIZE (gnu_type);
6464 /* Return an identifier representing the external name to be used for
6465 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6466 and the specified suffix. */
6469 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6471 const char *str = (!suffix ? "" : suffix);
6472 String_Template temp = {1, strlen (str)};
6473 Fat_Pointer fp = {str, &temp};
6475 Get_External_Name_With_Suffix (gnat_entity, fp);
6478 /* A variable using the Stdcall convention (meaning we are running
6479 on a Windows box) live in a DLL. Here we adjust its name to use
6480 the jump-table, the _imp__NAME contains the address for the NAME
6483 Entity_Kind kind = Ekind (gnat_entity);
6484 const char *prefix = "_imp__";
6485 int plen = strlen (prefix);
6487 if ((kind == E_Variable || kind == E_Constant)
6488 && Convention (gnat_entity) == Convention_Stdcall)
6491 for (k = 0; k <= Name_Len; k++)
6492 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6493 strncpy (Name_Buffer, prefix, plen);
6498 return get_identifier (Name_Buffer);
6501 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6502 fully-qualified name, possibly with type information encoding.
6503 Otherwise, return the name. */
6506 get_entity_name (Entity_Id gnat_entity)
6508 Get_Encoded_Name (gnat_entity);
6509 return get_identifier (Name_Buffer);
6512 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6513 string, return a new IDENTIFIER_NODE that is the concatenation of
6514 the name in GNU_ID and SUFFIX. */
6517 concat_id_with_name (tree gnu_id, const char *suffix)
6519 int len = IDENTIFIER_LENGTH (gnu_id);
6521 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6522 IDENTIFIER_LENGTH (gnu_id));
6523 strncpy (Name_Buffer + len, "___", 3);
6525 strcpy (Name_Buffer + len, suffix);
6526 return get_identifier (Name_Buffer);
6529 #include "gt-ada-decl.h"