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, int);
85 static int allocatable_size_p (tree, int);
86 static struct attrib *build_attr_list (Entity_Id);
87 static tree elaborate_expression (Node_Id, Entity_Id, tree, int, int, int);
88 static int is_variable_size (tree);
89 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree, int, int);
90 static tree make_packable_type (tree);
91 static tree maybe_pad_type (tree, tree, unsigned int, Entity_Id, const char *,
93 static tree gnat_to_gnu_field (Entity_Id, tree, int, int);
94 static void components_to_record (tree, Node_Id, tree, int, int, tree *,
96 static int compare_field_bitpos (const PTR, const PTR);
97 static Uint annotate_value (tree);
98 static void annotate_rep (Entity_Id, tree);
99 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
100 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, int, int);
101 static void set_rm_size (Uint, tree, Entity_Id);
102 static tree make_type_from_size (tree, tree, int);
103 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
104 static void check_ok_for_atomic (tree, Entity_Id, int);
106 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
107 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
108 refer to an Ada type. */
111 gnat_to_gnu_type (Entity_Id gnat_entity)
115 /* The back end never attempts to annotate generic types */
116 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
117 return void_type_node;
119 /* Convert the ada entity type into a GCC TYPE_DECL node. */
120 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
121 if (TREE_CODE (gnu_decl) != TYPE_DECL)
124 return TREE_TYPE (gnu_decl);
127 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
128 entity, this routine returns the equivalent GCC tree for that entity
129 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
132 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
133 initial value (in GCC tree form). This is optional for variables.
134 For renamed entities, GNU_EXPR gives the object being renamed.
136 DEFINITION is nonzero if this call is intended for a definition. This is
137 used for separate compilation where it necessary to know whether an
138 external declaration or a definition should be created if the GCC equivalent
139 was not created previously. The value of 1 is normally used for a non-zero
140 DEFINITION, but a value of 2 is used in special circumstances, defined in
144 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
148 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
149 GNAT tree. This node will be associated with the GNAT node by calling
150 the save_gnu_tree routine at the end of the `switch' statement. */
152 /* Nonzero if we have already saved gnu_decl as a gnat association. */
154 /* Nonzero if we incremented defer_incomplete_level. */
155 int this_deferred = 0;
156 /* Nonzero if we incremented force_global. */
158 /* Nonzero if we should check to see if elaborated during processing. */
159 int maybe_present = 0;
160 /* Nonzero if we made GNU_DECL and its type here. */
161 int this_made_decl = 0;
162 struct attrib *attr_list = 0;
163 int debug_info_p = (Needs_Debug_Info (gnat_entity)
164 || debug_info_level == DINFO_LEVEL_VERBOSE);
165 Entity_Kind kind = Ekind (gnat_entity);
168 = ((Known_Esize (gnat_entity)
169 && UI_Is_In_Int_Range (Esize (gnat_entity)))
170 ? MIN (UI_To_Int (Esize (gnat_entity)),
171 IN (kind, Float_Kind)
172 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
173 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
174 : LONG_LONG_TYPE_SIZE)
175 : LONG_LONG_TYPE_SIZE);
178 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
179 || From_With_Type (gnat_entity));
180 unsigned int align = 0;
182 /* Since a use of an Itype is a definition, process it as such if it
183 is not in a with'ed unit. */
185 if (! definition && Is_Itype (gnat_entity)
186 && ! present_gnu_tree (gnat_entity)
187 && In_Extended_Main_Code_Unit (gnat_entity))
189 /* Ensure that we are in a subprogram mentioned in the Scope
190 chain of this entity, our current scope is global,
191 or that we encountered a task or entry (where we can't currently
192 accurately check scoping). */
193 if (current_function_decl == 0
194 || DECL_ELABORATION_PROC_P (current_function_decl))
196 process_type (gnat_entity);
197 return get_gnu_tree (gnat_entity);
200 for (gnat_temp = Scope (gnat_entity);
201 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
203 if (Is_Type (gnat_temp))
204 gnat_temp = Underlying_Type (gnat_temp);
206 if (Ekind (gnat_temp) == E_Subprogram_Body)
208 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
210 if (IN (Ekind (gnat_temp), Subprogram_Kind)
211 && Present (Protected_Body_Subprogram (gnat_temp)))
212 gnat_temp = Protected_Body_Subprogram (gnat_temp);
214 if (Ekind (gnat_temp) == E_Entry
215 || Ekind (gnat_temp) == E_Entry_Family
216 || Ekind (gnat_temp) == E_Task_Type
217 || (IN (Ekind (gnat_temp), Subprogram_Kind)
218 && present_gnu_tree (gnat_temp)
219 && (current_function_decl
220 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
222 process_type (gnat_entity);
223 return get_gnu_tree (gnat_entity);
227 /* gigi abort 122 means that the entity "gnat_entity" has an incorrect
228 scope, i.e. that its scope does not correspond to the subprogram
229 in which it is declared */
233 /* If this is entity 0, something went badly wrong. */
234 if (gnat_entity == 0)
237 /* If we've already processed this entity, return what we got last time.
238 If we are defining the node, we should not have already processed it.
239 In that case, we will abort below when we try to save a new GCC tree for
240 this object. We also need to handle the case of getting a dummy type
241 when a Full_View exists. */
243 if (present_gnu_tree (gnat_entity)
245 || (Is_Type (gnat_entity) && imported_p)))
247 gnu_decl = get_gnu_tree (gnat_entity);
249 if (TREE_CODE (gnu_decl) == TYPE_DECL
250 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
251 && IN (kind, Incomplete_Or_Private_Kind)
252 && Present (Full_View (gnat_entity)))
254 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
257 save_gnu_tree (gnat_entity, NULL_TREE, 0);
258 save_gnu_tree (gnat_entity, gnu_decl, 0);
264 /* If this is a numeric or enumeral type, or an access type, a nonzero
265 Esize must be specified unless it was specified by the programmer. */
266 if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
267 || (IN (kind, Access_Kind)
268 && kind != E_Access_Protected_Subprogram_Type
269 && kind != E_Access_Subtype))
270 && Unknown_Esize (gnat_entity)
271 && ! Has_Size_Clause (gnat_entity))
274 /* Likewise, RM_Size must be specified for all discrete and fixed-point
276 if (IN (kind, Discrete_Or_Fixed_Point_Kind)
277 && Unknown_RM_Size (gnat_entity))
280 /* Get the name of the entity and set up the line number and filename of
281 the original definition for use in any decl we make. */
283 gnu_entity_id = get_entity_name (gnat_entity);
284 set_lineno (gnat_entity, 0);
286 /* If we get here, it means we have not yet done anything with this
287 entity. If we are not defining it here, it must be external,
288 otherwise we should have defined it already. */
289 if (! definition && ! Is_Public (gnat_entity)
290 && ! type_annotate_only
291 && kind != E_Discriminant && kind != E_Component
293 && ! (kind == E_Constant && Present (Full_View (gnat_entity)))
295 && !IN (kind, Type_Kind)
300 /* For cases when we are not defining (i.e., we are referencing from
301 another compilation unit) Public entities, show we are at global level
302 for the purpose of computing sizes. Don't do this for components or
303 discriminants since the relevant test is whether or not the record is
305 if (! definition && Is_Public (gnat_entity)
306 && ! Is_Statically_Allocated (gnat_entity)
307 && kind != E_Discriminant && kind != E_Component)
308 force_global++, this_global = 1;
310 /* Handle any attributes. */
311 if (Has_Gigi_Rep_Item (gnat_entity))
312 attr_list = build_attr_list (gnat_entity);
317 /* If this is a use of a deferred constant, get its full
319 if (! definition && Present (Full_View (gnat_entity)))
321 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
322 gnu_expr, definition);
327 /* If we have an external constant that we are not defining,
328 get the expression that is was defined to represent. We
329 may throw that expression away later if it is not a
331 Do not retrieve the expression if it is an aggregate, because
332 in complex instantiation contexts it may not be expanded */
335 && Present (Expression (Declaration_Node (gnat_entity)))
336 && ! No_Initialization (Declaration_Node (gnat_entity))
337 && Nkind (Expression (Declaration_Node (gnat_entity)))
339 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
341 /* Ignore deferred constant definitions; they are processed fully in the
342 front-end. For deferred constant references, get the full
343 definition. On the other hand, constants that are renamings are
344 handled like variable renamings. If No_Initialization is set, this is
345 not a deferred constant but a constant whose value is built
348 if (definition && gnu_expr == 0
349 && ! No_Initialization (Declaration_Node (gnat_entity))
350 && No (Renamed_Object (gnat_entity)))
352 gnu_decl = error_mark_node;
356 else if (! definition && IN (kind, Incomplete_Or_Private_Kind)
357 && Present (Full_View (gnat_entity)))
359 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
368 /* We used to special case VMS exceptions here to directly map them to
369 their associated condition code. Since this code had to be masked
370 dynamically to strip off the severity bits, this caused trouble in
371 the GCC/ZCX case because the "type" pointers we store in the tables
372 have to be static. We now don't special case here anymore, and let
373 the regular processing take place, which leaves us with a regular
374 exception data object for VMS exceptions too. The condition code
375 mapping is taken care of by the front end and the bitmasking by the
382 /* The GNAT record where the component was defined. */
383 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
385 /* If the variable is an inherited record component (in the case of
386 extended record types), just return the inherited entity, which
387 must be a FIELD_DECL. Likewise for discriminants.
388 For discriminants of untagged records which have explicit
389 stored discriminants, return the entity for the corresponding
390 stored discriminant. Also use Original_Record_Component
391 if the record has a private extension. */
393 if ((Base_Type (gnat_record) == gnat_record
394 || Ekind (Scope (gnat_entity)) == E_Private_Subtype
395 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
396 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
397 && Present (Original_Record_Component (gnat_entity))
398 && Original_Record_Component (gnat_entity) != gnat_entity)
401 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
402 gnu_expr, definition);
407 /* If the enclosing record has explicit stored discriminants,
408 then it is an untagged record. If the Corresponding_Discriminant
409 is not empty then this must be a renamed discriminant and its
410 Original_Record_Component must point to the corresponding explicit
411 stored discriminant (i.e., we should have taken the previous
414 else if (Present (Corresponding_Discriminant (gnat_entity))
415 && Is_Tagged_Type (gnat_record))
417 /* A tagged record has no explicit stored discriminants. */
419 if (First_Discriminant (gnat_record)
420 != First_Stored_Discriminant (gnat_record))
424 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
425 gnu_expr, definition);
430 /* If the enclosing record has explicit stored discriminants,
431 then it is an untagged record. If the Corresponding_Discriminant
432 is not empty then this must be a renamed discriminant and its
433 Original_Record_Component must point to the corresponding explicit
434 stored discriminant (i.e., we should have taken the first
437 else if (Present (Corresponding_Discriminant (gnat_entity))
438 && (First_Discriminant (gnat_record)
439 != First_Stored_Discriminant (gnat_record)))
442 /* Otherwise, if we are not defining this and we have no GCC type
443 for the containing record, make one for it. Then we should
444 have made our own equivalent. */
445 else if (! definition && ! present_gnu_tree (gnat_record))
447 /* ??? If this is in a record whose scope is a protected
448 type and we have an Original_Record_Component, use it.
449 This is a workaround for major problems in protected type
452 Entity_Id Scop = Scope (Scope (gnat_entity));
453 if ((Is_Protected_Type (Scop)
454 || (Is_Private_Type (Scop)
455 && Present (Full_View (Scop))
456 && Is_Protected_Type (Full_View (Scop))))
457 && Present (Original_Record_Component (gnat_entity)))
460 = gnat_to_gnu_entity (Original_Record_Component
462 gnu_expr, definition);
467 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
468 gnu_decl = get_gnu_tree (gnat_entity);
473 /* Here we have no GCC type and this is a reference rather than a
474 definition. This should never happen. Most likely the cause is a
475 reference before declaration in the gnat tree for gnat_entity. */
480 case E_Loop_Parameter:
481 case E_Out_Parameter:
484 /* Simple variables, loop variables, OUT parameters, and exceptions. */
489 = ((kind == E_Constant || kind == E_Variable)
490 && ! Is_Statically_Allocated (gnat_entity)
491 && Is_True_Constant (gnat_entity)
492 && (((Nkind (Declaration_Node (gnat_entity))
493 == N_Object_Declaration)
494 && Present (Expression (Declaration_Node (gnat_entity))))
495 || Present (Renamed_Object (gnat_entity))));
496 int inner_const_flag = const_flag;
497 int static_p = Is_Statically_Allocated (gnat_entity);
498 tree gnu_ext_name = NULL_TREE;
500 if (Present (Renamed_Object (gnat_entity)) && ! definition)
502 if (kind == E_Exception)
503 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
506 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
509 /* Get the type after elaborating the renamed object. */
510 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
512 /* If this is a loop variable, its type should be the base type.
513 This is because the code for processing a loop determines whether
514 a normal loop end test can be done by comparing the bounds of the
515 loop against those of the base type, which is presumed to be the
516 size used for computation. But this is not correct when the size
517 of the subtype is smaller than the type. */
518 if (kind == E_Loop_Parameter)
519 gnu_type = get_base_type (gnu_type);
521 /* Reject non-renamed objects whose types are unconstrained arrays or
522 any object whose type is a dummy type or VOID_TYPE. */
524 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
525 && No (Renamed_Object (gnat_entity)))
526 || TYPE_IS_DUMMY_P (gnu_type)
527 || TREE_CODE (gnu_type) == VOID_TYPE)
529 if (type_annotate_only)
530 return error_mark_node;
535 /* If we are defining the object, see if it has a Size value and
536 validate it if so. If we are not defining the object and a Size
537 clause applies, simply retrieve the value. We don't want to ignore
538 the clause and it is expected to have been validated already. Then
539 get the new type, if any. */
541 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
542 gnat_entity, VAR_DECL, 0,
543 Has_Size_Clause (gnat_entity));
544 else if (Has_Size_Clause (gnat_entity))
545 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
550 = make_type_from_size (gnu_type, gnu_size,
551 Has_Biased_Representation (gnat_entity));
553 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
557 /* If this object has self-referential size, it must be a record with
558 a default value. We are supposed to allocate an object of the
559 maximum size in this case unless it is a constant with an
560 initializing expression, in which case we can get the size from
561 that. Note that the resulting size may still be a variable, so
562 this may end up with an indirect allocation. */
564 if (No (Renamed_Object (gnat_entity))
565 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
567 if (gnu_expr != 0 && kind == E_Constant)
569 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
570 if (CONTAINS_PLACEHOLDER_P (gnu_size))
571 gnu_size = build (WITH_RECORD_EXPR, bitsizetype,
575 /* We may have no GNU_EXPR because No_Initialization is
576 set even though there's an Expression. */
577 else if (kind == E_Constant
578 && (Nkind (Declaration_Node (gnat_entity))
579 == N_Object_Declaration)
580 && Present (Expression (Declaration_Node (gnat_entity))))
582 = TYPE_SIZE (gnat_to_gnu_type
584 (Expression (Declaration_Node (gnat_entity)))));
586 gnu_size = max_size (TYPE_SIZE (gnu_type), 1);
589 /* If the size is zero bytes, make it one byte since some linkers have
590 trouble with zero-sized objects. If the object will have a
591 template, that will make it nonzero so don't bother. Also avoid
592 doing that for an object renaming or an object with an address
593 clause, as we would lose useful information on the view size
594 (e.g. for null array slices) and we are not allocating the object
596 if (((gnu_size != 0 && integer_zerop (gnu_size))
597 || (TYPE_SIZE (gnu_type) != 0
598 && integer_zerop (TYPE_SIZE (gnu_type))))
599 && (! Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
600 || ! Is_Array_Type (Etype (gnat_entity)))
601 && ! Present (Renamed_Object (gnat_entity))
602 && ! Present (Address_Clause (gnat_entity)))
603 gnu_size = bitsize_unit_node;
605 /* If an alignment is specified, use it if valid. Note that
606 exceptions are objects but don't have alignments. */
607 if (kind != E_Exception && Known_Alignment (gnat_entity))
609 if (No (Alignment (gnat_entity)))
613 = validate_alignment (Alignment (gnat_entity), gnat_entity,
614 TYPE_ALIGN (gnu_type));
617 /* If this is an atomic object with no specified size and alignment,
618 but where the size of the type is a constant, set the alignment to
619 the lowest power of two greater than the size, or to the
620 biggest meaningful alignment, whichever is smaller. */
622 if (Is_Atomic (gnat_entity) && gnu_size == 0 && align == 0
623 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
625 if (! host_integerp (TYPE_SIZE (gnu_type), 1)
626 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
628 align = BIGGEST_ALIGNMENT;
630 align = ((unsigned int) 1
631 << (floor_log2 (tree_low_cst
632 (TYPE_SIZE (gnu_type), 1) - 1)
636 /* If the object is set to have atomic components, find the component
637 type and validate it.
639 ??? Note that we ignore Has_Volatile_Components on objects; it's
640 not at all clear what to do in that case. */
642 if (Has_Atomic_Components (gnat_entity))
645 = (TREE_CODE (gnu_type) == ARRAY_TYPE
646 ? TREE_TYPE (gnu_type) : gnu_type);
648 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
649 && TYPE_MULTI_ARRAY_P (gnu_inner))
650 gnu_inner = TREE_TYPE (gnu_inner);
652 check_ok_for_atomic (gnu_inner, gnat_entity, 1);
655 /* Now check if the type of the object allows atomic access. Note
656 that we must test the type, even if this object has size and
657 alignment to allow such access, because we will be going
658 inside the padded record to assign to the object. We could fix
659 this by always copying via an intermediate value, but it's not
660 clear it's worth the effort. */
661 if (Is_Atomic (gnat_entity))
662 check_ok_for_atomic (gnu_type, gnat_entity, 0);
664 /* If this is an aliased object with an unconstrained nominal subtype,
665 make a type that includes the template. */
666 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
667 && Is_Array_Type (Etype (gnat_entity))
668 && ! type_annotate_only)
671 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
673 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
676 = build_unc_object_type (gnu_temp_type, gnu_type,
677 concat_id_with_name (gnu_entity_id,
681 #ifdef MINIMUM_ATOMIC_ALIGNMENT
682 /* If the size is a constant and no alignment is specified, force
683 the alignment to be the minimum valid atomic alignment. The
684 restriction on constant size avoids problems with variable-size
685 temporaries; if the size is variable, there's no issue with
686 atomic access. Also don't do this for a constant, since it isn't
687 necessary and can interfere with constant replacement. Finally,
688 do not do it for Out parameters since that creates an
689 size inconsistency with In parameters. */
690 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
691 && ! FLOAT_TYPE_P (gnu_type)
692 && ! const_flag && No (Renamed_Object (gnat_entity))
693 && ! imported_p && No (Address_Clause (gnat_entity))
694 && kind != E_Out_Parameter
695 && (gnu_size != 0 ? TREE_CODE (gnu_size) == INTEGER_CST
696 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
697 align = MINIMUM_ATOMIC_ALIGNMENT;
700 /* Make a new type with the desired size and alignment, if needed. */
701 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
702 gnat_entity, "PAD", 0, definition, 1);
704 /* Make a volatile version of this object's type if we are to
705 make the object volatile. Note that 13.3(19) says that we
706 should treat other types of objects as volatile as well. */
707 if ((Treat_As_Volatile (gnat_entity)
708 || Is_Exported (gnat_entity)
709 || Is_Imported (gnat_entity)
710 || Present (Address_Clause (gnat_entity)))
711 && ! TYPE_VOLATILE (gnu_type))
712 gnu_type = build_qualified_type (gnu_type,
713 (TYPE_QUALS (gnu_type)
714 | TYPE_QUAL_VOLATILE));
716 /* Convert the expression to the type of the object except in the
717 case where the object's type is unconstrained or the object's type
718 is a padded record whose field is of self-referential size. In
719 the former case, converting will generate unnecessary evaluations
720 of the CONSTRUCTOR to compute the size and in the latter case, we
721 want to only copy the actual data. */
723 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
724 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
725 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
726 && TYPE_IS_PADDING_P (gnu_type)
727 && (CONTAINS_PLACEHOLDER_P
728 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
729 gnu_expr = convert (gnu_type, gnu_expr);
731 /* See if this is a renaming. If this is a constant renaming,
732 treat it as a normal variable whose initial value is what
733 is being renamed. We cannot do this if the type is
734 unconstrained or class-wide.
736 Otherwise, if what we are renaming is a reference, we can simply
737 return a stabilized version of that reference, after forcing
738 any SAVE_EXPRs to be evaluated. But, if this is at global level,
739 we can only do this if we know no SAVE_EXPRs will be made.
740 Otherwise, make this into a constant pointer to the object we are
743 if (Present (Renamed_Object (gnat_entity)))
745 /* If the renamed object had padding, strip off the reference
746 to the inner object and reset our type. */
747 if (TREE_CODE (gnu_expr) == COMPONENT_REF
748 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
750 && (TYPE_IS_PADDING_P
751 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
753 gnu_expr = TREE_OPERAND (gnu_expr, 0);
754 gnu_type = TREE_TYPE (gnu_expr);
758 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
759 && TYPE_MODE (gnu_type) != BLKmode
760 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
761 && !Is_Array_Type (Etype (gnat_entity)))
764 /* If this is a declaration or reference, we can just use that
765 declaration or reference as this entity. */
766 else if ((DECL_P (gnu_expr)
767 || TREE_CODE_CLASS (TREE_CODE (gnu_expr)) == 'r')
768 && ! Materialize_Entity (gnat_entity)
769 && (! global_bindings_p ()
770 || (staticp (gnu_expr)
771 && ! TREE_SIDE_EFFECTS (gnu_expr))))
773 set_lineno (gnat_entity, ! global_bindings_p ());
774 gnu_decl = gnat_stabilize_reference (gnu_expr, 1);
775 save_gnu_tree (gnat_entity, gnu_decl, 1);
778 if (! global_bindings_p ())
779 expand_expr_stmt (build1 (CONVERT_EXPR, void_type_node,
785 inner_const_flag = TREE_READONLY (gnu_expr);
787 gnu_type = build_reference_type (gnu_type);
788 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
794 /* If this is an aliased object whose nominal subtype is unconstrained,
795 the object is a record that contains both the template and
796 the object. If there is an initializer, it will have already
797 been converted to the right type, but we need to create the
798 template if there is no initializer. */
799 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
800 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
801 /* Beware that padding might have been introduced
802 via maybe_pad_type above. */
803 || (TYPE_IS_PADDING_P (gnu_type)
804 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
806 && TYPE_CONTAINS_TEMPLATE_P
807 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
811 = TYPE_IS_PADDING_P (gnu_type)
812 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
813 : TYPE_FIELDS (gnu_type);
816 = gnat_build_constructor
820 build_template (TREE_TYPE (template_field),
821 TREE_TYPE (TREE_CHAIN (template_field)),
826 /* If this is a pointer and it does not have an initializing
827 expression, initialize it to NULL, unless the obect is
830 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
831 && !Is_Imported (gnat_entity)
833 gnu_expr = integer_zero_node;
835 /* If we are defining the object and it has an Address clause we must
836 get the address expression from the saved GCC tree for the
837 object if the object has a Freeze_Node. Otherwise, we elaborate
838 the address expression here since the front-end has guaranteed
839 in that case that the elaboration has no effects. Note that
840 only the latter mechanism is currently in use. */
841 if (definition && Present (Address_Clause (gnat_entity)))
844 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
845 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
847 save_gnu_tree (gnat_entity, NULL_TREE, 0);
849 /* Ignore the size. It's either meaningless or was handled
852 gnu_type = build_reference_type (gnu_type);
853 gnu_address = convert (gnu_type, gnu_address);
855 const_flag = ! Is_Public (gnat_entity);
857 /* If we don't have an initializing expression for the underlying
858 variable, the initializing expression for the pointer is the
859 specified address. Otherwise, we have to make a COMPOUND_EXPR
860 to assign both the address and the initial value. */
862 gnu_expr = gnu_address;
865 = build (COMPOUND_EXPR, gnu_type,
867 (MODIFY_EXPR, NULL_TREE,
868 build_unary_op (INDIRECT_REF, NULL_TREE,
874 /* If it has an address clause and we are not defining it, mark it
875 as an indirect object. Likewise for Stdcall objects that are
877 if ((! definition && Present (Address_Clause (gnat_entity)))
878 || (Is_Imported (gnat_entity)
879 && Convention (gnat_entity) == Convention_Stdcall))
881 gnu_type = build_reference_type (gnu_type);
886 /* If we are at top level and this object is of variable size,
887 make the actual type a hidden pointer to the real type and
888 make the initializer be a memory allocation and initialization.
889 Likewise for objects we aren't defining (presumed to be
890 external references from other packages), but there we do
891 not set up an initialization.
893 If the object's size overflows, make an allocator too, so that
894 Storage_Error gets raised. Note that we will never free
895 such memory, so we presume it never will get allocated. */
897 if (! allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
898 global_bindings_p () || ! definition
901 && ! allocatable_size_p (gnu_size,
902 global_bindings_p () || ! definition
905 gnu_type = build_reference_type (gnu_type);
910 /* Get the data part of GNU_EXPR in case this was a
911 aliased object whose nominal subtype is unconstrained.
912 In that case the pointer above will be a thin pointer and
913 build_allocator will automatically make the template and
914 constructor already made above. */
918 tree gnu_alloc_type = TREE_TYPE (gnu_type);
920 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
921 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
924 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
926 = build_component_ref
927 (gnu_expr, NULL_TREE,
928 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))), 0);
931 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
932 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
933 && ! Is_Imported (gnat_entity))
934 post_error ("Storage_Error will be raised at run-time?",
937 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
938 gnu_type, 0, 0, gnat_entity);
947 /* If this object would go into the stack and has an alignment
948 larger than the default largest alignment, make a variable
949 to hold the "aligning type" with a modified initial value,
950 if any, then point to it and make that the value of this
951 variable, which is now indirect. */
953 if (! global_bindings_p () && ! static_p && definition
954 && ! imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
957 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
958 TYPE_SIZE_UNIT (gnu_type));
961 set_lineno (gnat_entity, 1);
963 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
964 NULL_TREE, gnu_new_type, gnu_expr,
970 (MODIFY_EXPR, NULL_TREE,
971 build_component_ref (gnu_new_var, NULL_TREE,
972 TYPE_FIELDS (gnu_new_type), 0),
975 gnu_type = build_reference_type (gnu_type);
978 (ADDR_EXPR, gnu_type,
979 build_component_ref (gnu_new_var, NULL_TREE,
980 TYPE_FIELDS (gnu_new_type), 0));
987 /* Convert the expression to the type of the object except in the
988 case where the object's type is unconstrained or the object's type
989 is a padded record whose field is of self-referential size. In
990 the former case, converting will generate unnecessary evaluations
991 of the CONSTRUCTOR to compute the size and in the latter case, we
992 want to only copy the actual data. */
994 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
995 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
996 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
997 && TYPE_IS_PADDING_P (gnu_type)
998 && (CONTAINS_PLACEHOLDER_P
999 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1000 gnu_expr = convert (gnu_type, gnu_expr);
1002 /* If this name is external or there was a name specified, use it,
1003 unless this is a VMS exception object since this would conflict
1004 with the symbol we need to export in addition. Don't use the
1005 Interface_Name if there is an address clause (see CD30005). */
1006 if (! Is_VMS_Exception (gnat_entity)
1008 ((Present (Interface_Name (gnat_entity))
1009 && No (Address_Clause (gnat_entity)))
1011 (Is_Public (gnat_entity)
1012 && (! Is_Imported (gnat_entity) || Is_Exported (gnat_entity)))))
1013 gnu_ext_name = create_concat_name (gnat_entity, 0);
1016 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1017 | TYPE_QUAL_CONST));
1019 /* If this is constant initialized to a static constant and the
1020 object has an aggregrate type, force it to be statically
1022 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1023 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1024 && (AGGREGATE_TYPE_P (gnu_type)
1025 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1026 && TYPE_IS_PADDING_P (gnu_type))))
1029 set_lineno (gnat_entity, ! global_bindings_p ());
1030 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1031 gnu_expr, const_flag,
1032 Is_Public (gnat_entity),
1033 imported_p || !definition,
1034 static_p, attr_list);
1036 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1037 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1039 /* If we have an address clause and we've made this indirect, it's
1040 not enough to merely mark the type as volatile since volatile
1041 references only conflict with other volatile references while this
1042 reference must conflict with all other references. So ensure that
1043 the dereferenced value has alias set 0. */
1044 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1045 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1047 if (definition && DECL_SIZE (gnu_decl) != 0
1048 && gnu_block_stack != 0
1049 && TREE_VALUE (gnu_block_stack) != 0
1050 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1051 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1052 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1053 STACK_CHECK_MAX_VAR_SIZE))))
1054 update_setjmp_buf (TREE_VALUE (gnu_block_stack));
1056 /* If this is a public constant or we're not optimizing and we're not
1057 making a VAR_DECL for it, make one just for export or debugger
1058 use. Likewise if the address is taken or if the object or type is
1060 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1061 && (Is_Public (gnat_entity)
1063 || Address_Taken (gnat_entity)
1064 || Is_Aliased (gnat_entity)
1065 || Is_Aliased (Etype (gnat_entity))))
1066 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl,
1067 create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1068 gnu_expr, 0, Is_Public (gnat_entity), 0,
1071 /* If this is declared in a block that contains an block with an
1072 exception handler, we must force this variable in memory to
1073 suppress an invalid optimization. */
1074 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1075 && Exception_Mechanism != GCC_ZCX)
1077 gnat_mark_addressable (gnu_decl);
1078 flush_addressof (gnu_decl);
1081 /* Back-annotate the Alignment of the object if not already in the
1082 tree. Likewise for Esize if the object is of a constant size.
1083 But if the "object" is actually a pointer to an object, the
1084 alignment and size are the same as teh type, so don't back-annotate
1085 the values for the pointer. */
1086 if (! used_by_ref && Unknown_Alignment (gnat_entity))
1087 Set_Alignment (gnat_entity,
1088 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1090 if (! used_by_ref && Unknown_Esize (gnat_entity)
1091 && DECL_SIZE (gnu_decl) != 0)
1093 tree gnu_back_size = DECL_SIZE (gnu_decl);
1095 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1096 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1098 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1099 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1101 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1107 /* Return a TYPE_DECL for "void" that we previously made. */
1108 gnu_decl = void_type_decl_node;
1111 case E_Enumeration_Type:
1112 /* A special case, for the types Character and Wide_Character in
1113 Standard, we do not list all the literals. So if the literals
1114 are not specified, make this an unsigned type. */
1115 if (No (First_Literal (gnat_entity)))
1117 gnu_type = make_unsigned_type (esize);
1121 /* Normal case of non-character type, or non-Standard character type */
1123 /* Here we have a list of enumeral constants in First_Literal.
1124 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1125 the list to be places into TYPE_FIELDS. Each node in the list
1126 is a TREE_LIST node whose TREE_VALUE is the literal name
1127 and whose TREE_PURPOSE is the value of the literal.
1129 Esize contains the number of bits needed to represent the enumeral
1130 type, Type_Low_Bound also points to the first literal and
1131 Type_High_Bound points to the last literal. */
1133 Entity_Id gnat_literal;
1134 tree gnu_literal_list = NULL_TREE;
1136 if (Is_Unsigned_Type (gnat_entity))
1137 gnu_type = make_unsigned_type (esize);
1139 gnu_type = make_signed_type (esize);
1141 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1143 for (gnat_literal = First_Literal (gnat_entity);
1144 Present (gnat_literal);
1145 gnat_literal = Next_Literal (gnat_literal))
1147 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1150 = create_var_decl (get_entity_name (gnat_literal),
1151 0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1153 save_gnu_tree (gnat_literal, gnu_literal, 0);
1154 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1155 gnu_value, gnu_literal_list);
1158 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1160 /* Note that the bounds are updated at the end of this function
1161 because to avoid an infinite recursion when we get the bounds of
1162 this type, since those bounds are objects of this type. */
1166 case E_Signed_Integer_Type:
1167 case E_Ordinary_Fixed_Point_Type:
1168 case E_Decimal_Fixed_Point_Type:
1169 /* For integer types, just make a signed type the appropriate number
1171 gnu_type = make_signed_type (esize);
1174 case E_Modular_Integer_Type:
1175 /* For modular types, make the unsigned type of the proper number of
1176 bits and then set up the modulus, if required. */
1178 enum machine_mode mode;
1182 if (Is_Packed_Array_Type (gnat_entity))
1183 esize = UI_To_Int (RM_Size (gnat_entity));
1185 /* Find the smallest mode at least ESIZE bits wide and make a class
1188 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1189 GET_MODE_BITSIZE (mode) < esize;
1190 mode = GET_MODE_WIDER_MODE (mode))
1193 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1194 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1195 = Is_Packed_Array_Type (gnat_entity);
1197 /* Get the modulus in this type. If it overflows, assume it is because
1198 it is equal to 2**Esize. Note that there is no overflow checking
1199 done on unsigned type, so we detect the overflow by looking for
1200 a modulus of zero, which is otherwise invalid. */
1201 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1203 if (! integer_zerop (gnu_modulus))
1205 TYPE_MODULAR_P (gnu_type) = 1;
1206 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1207 gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1208 convert (gnu_type, integer_one_node)));
1211 /* If we have to set TYPE_PRECISION different from its natural value,
1212 make a subtype to do do. Likewise if there is a modulus and
1213 it is not one greater than TYPE_MAX_VALUE. */
1214 if (TYPE_PRECISION (gnu_type) != esize
1215 || (TYPE_MODULAR_P (gnu_type)
1216 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1218 tree gnu_subtype = make_node (INTEGER_TYPE);
1220 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1221 TREE_TYPE (gnu_subtype) = gnu_type;
1222 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1223 TYPE_MAX_VALUE (gnu_subtype)
1224 = TYPE_MODULAR_P (gnu_type)
1225 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1226 TYPE_PRECISION (gnu_subtype) = esize;
1227 TREE_UNSIGNED (gnu_subtype) = 1;
1228 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1229 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1230 = Is_Packed_Array_Type (gnat_entity);
1231 layout_type (gnu_subtype);
1233 gnu_type = gnu_subtype;
1238 case E_Signed_Integer_Subtype:
1239 case E_Enumeration_Subtype:
1240 case E_Modular_Integer_Subtype:
1241 case E_Ordinary_Fixed_Point_Subtype:
1242 case E_Decimal_Fixed_Point_Subtype:
1244 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1245 that we do not want to call build_range_type since we would
1246 like each subtype node to be distinct. This will be important
1247 when memory aliasing is implemented.
1249 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1250 parent type; this fact is used by the arithmetic conversion
1253 We elaborate the Ancestor_Subtype if it is not in the current
1254 unit and one of our bounds is non-static. We do this to ensure
1255 consistent naming in the case where several subtypes share the same
1256 bounds by always elaborating the first such subtype first, thus
1260 && Present (Ancestor_Subtype (gnat_entity))
1261 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1262 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1263 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1264 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1265 gnu_expr, definition);
1267 gnu_type = make_node (INTEGER_TYPE);
1268 if (Is_Packed_Array_Type (gnat_entity))
1270 esize = UI_To_Int (RM_Size (gnat_entity));
1271 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1274 TYPE_PRECISION (gnu_type) = esize;
1275 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1277 TYPE_MIN_VALUE (gnu_type)
1278 = convert (TREE_TYPE (gnu_type),
1279 elaborate_expression (Type_Low_Bound (gnat_entity),
1281 get_identifier ("L"), definition, 1,
1282 Needs_Debug_Info (gnat_entity)));
1284 TYPE_MAX_VALUE (gnu_type)
1285 = convert (TREE_TYPE (gnu_type),
1286 elaborate_expression (Type_High_Bound (gnat_entity),
1288 get_identifier ("U"), definition, 1,
1289 Needs_Debug_Info (gnat_entity)));
1291 /* One of the above calls might have caused us to be elaborated,
1292 so don't blow up if so. */
1293 if (present_gnu_tree (gnat_entity))
1299 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1300 = Has_Biased_Representation (gnat_entity);
1302 /* This should be an unsigned type if the lower bound is constant
1303 and non-negative or if the base type is unsigned; a signed type
1305 TREE_UNSIGNED (gnu_type)
1306 = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1307 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1308 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1309 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1310 || Is_Unsigned_Type (gnat_entity));
1312 layout_type (gnu_type);
1314 /* If the type we are dealing with is to represent a packed array,
1315 we need to have the bits left justified on big-endian targets
1316 (see exp_packd.ads). We build a record with a bitfield of the
1317 appropriate size to achieve this. */
1318 if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1320 tree gnu_field_type = gnu_type;
1323 TYPE_RM_SIZE_INT (gnu_field_type)
1324 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1325 gnu_type = make_node (RECORD_TYPE);
1326 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1327 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1328 TYPE_PACKED (gnu_type) = 1;
1330 /* Don't notify the field as "addressable", since we won't be taking
1331 it's address and it would prevent create_field_decl from making a
1333 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1334 gnu_field_type, gnu_type, 1, 0, 0, 0);
1336 finish_record_type (gnu_type, gnu_field, 0, 0);
1337 TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1338 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1343 case E_Floating_Point_Type:
1344 /* If this is a VAX floating-point type, use an integer of the proper
1345 size. All the operations will be handled with ASM statements. */
1346 if (Vax_Float (gnat_entity))
1348 gnu_type = make_signed_type (esize);
1349 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1350 SET_TYPE_DIGITS_VALUE (gnu_type,
1351 UI_To_gnu (Digits_Value (gnat_entity),
1356 /* The type of the Low and High bounds can be our type if this is
1357 a type from Standard, so set them at the end of the function. */
1358 gnu_type = make_node (REAL_TYPE);
1359 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1360 layout_type (gnu_type);
1363 case E_Floating_Point_Subtype:
1364 if (Vax_Float (gnat_entity))
1366 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1372 && Present (Ancestor_Subtype (gnat_entity))
1373 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1374 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1375 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1376 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1377 gnu_expr, definition);
1379 gnu_type = make_node (REAL_TYPE);
1380 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1381 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1383 TYPE_MIN_VALUE (gnu_type)
1384 = convert (TREE_TYPE (gnu_type),
1385 elaborate_expression (Type_Low_Bound (gnat_entity),
1386 gnat_entity, get_identifier ("L"),
1388 Needs_Debug_Info (gnat_entity)));
1390 TYPE_MAX_VALUE (gnu_type)
1391 = convert (TREE_TYPE (gnu_type),
1392 elaborate_expression (Type_High_Bound (gnat_entity),
1393 gnat_entity, get_identifier ("U"),
1395 Needs_Debug_Info (gnat_entity)));
1397 /* One of the above calls might have caused us to be elaborated,
1398 so don't blow up if so. */
1399 if (present_gnu_tree (gnat_entity))
1405 layout_type (gnu_type);
1409 /* Array and String Types and Subtypes
1411 Unconstrained array types are represented by E_Array_Type and
1412 constrained array types are represented by E_Array_Subtype. There
1413 are no actual objects of an unconstrained array type; all we have
1414 are pointers to that type.
1416 The following fields are defined on array types and subtypes:
1418 Component_Type Component type of the array.
1419 Number_Dimensions Number of dimensions (an int).
1420 First_Index Type of first index. */
1425 tree gnu_template_fields = NULL_TREE;
1426 tree gnu_template_type = make_node (RECORD_TYPE);
1427 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1428 tree gnu_fat_type = make_node (RECORD_TYPE);
1429 int ndim = Number_Dimensions (gnat_entity);
1431 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1433 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1434 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1435 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1436 tree gnu_comp_size = 0;
1437 tree gnu_max_size = size_one_node;
1438 tree gnu_max_size_unit;
1440 Entity_Id gnat_ind_subtype;
1441 Entity_Id gnat_ind_base_subtype;
1442 tree gnu_template_reference;
1445 TYPE_NAME (gnu_template_type)
1446 = create_concat_name (gnat_entity, "XUB");
1447 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1448 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1449 TREE_READONLY (gnu_template_type) = 1;
1451 /* Make a node for the array. If we are not defining the array
1452 suppress expanding incomplete types and save the node as the type
1454 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1457 defer_incomplete_level++;
1458 this_deferred = this_made_decl = 1;
1459 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1460 ! Comes_From_Source (gnat_entity),
1462 save_gnu_tree (gnat_entity, gnu_decl, 0);
1466 /* Build the fat pointer type. Use a "void *" object instead of
1467 a pointer to the array type since we don't have the array type
1468 yet (it will reference the fat pointer via the bounds). */
1469 tem = chainon (chainon (NULL_TREE,
1470 create_field_decl (get_identifier ("P_ARRAY"),
1472 gnu_fat_type, 0, 0, 0, 0)),
1473 create_field_decl (get_identifier ("P_BOUNDS"),
1475 gnu_fat_type, 0, 0, 0, 0));
1477 /* Make sure we can put this into a register. */
1478 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1479 finish_record_type (gnu_fat_type, tem, 0, 1);
1481 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1482 is the fat pointer. This will be used to access the individual
1483 fields once we build them. */
1484 tem = build (COMPONENT_REF, gnu_ptr_template,
1485 build (PLACEHOLDER_EXPR, gnu_fat_type),
1486 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1487 gnu_template_reference
1488 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1489 TREE_READONLY (gnu_template_reference) = 1;
1491 /* Now create the GCC type for each index and add the fields for
1492 that index to the template. */
1493 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1494 gnat_ind_base_subtype
1495 = First_Index (Implementation_Base_Type (gnat_entity));
1496 index < ndim && index >= 0;
1498 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1499 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1501 char field_name[10];
1502 tree gnu_ind_subtype
1503 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1504 tree gnu_base_subtype
1505 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1507 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1509 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1510 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1512 /* Make the FIELD_DECLs for the minimum and maximum of this
1513 type and then make extractions of that field from the
1515 set_lineno (gnat_entity, 0);
1516 sprintf (field_name, "LB%d", index);
1517 gnu_min_field = create_field_decl (get_identifier (field_name),
1519 gnu_template_type, 0, 0, 0, 0);
1520 field_name[0] = 'U';
1521 gnu_max_field = create_field_decl (get_identifier (field_name),
1523 gnu_template_type, 0, 0, 0, 0);
1525 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1527 /* We can't use build_component_ref here since the template
1528 type isn't complete yet. */
1529 gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1530 gnu_template_reference, gnu_min_field);
1531 gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1532 gnu_template_reference, gnu_max_field);
1533 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1535 /* Make a range type with the new ranges, but using
1536 the Ada subtype. Then we convert to sizetype. */
1537 gnu_index_types[index]
1538 = create_index_type (convert (sizetype, gnu_min),
1539 convert (sizetype, gnu_max),
1540 build_range_type (gnu_ind_subtype,
1542 /* Update the maximum size of the array, in elements. */
1544 = size_binop (MULT_EXPR, gnu_max_size,
1545 size_binop (PLUS_EXPR, size_one_node,
1546 size_binop (MINUS_EXPR, gnu_base_max,
1549 TYPE_NAME (gnu_index_types[index])
1550 = create_concat_name (gnat_entity, field_name);
1553 for (index = 0; index < ndim; index++)
1555 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1557 /* Install all the fields into the template. */
1558 finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1559 TREE_READONLY (gnu_template_type) = 1;
1561 /* Now make the array of arrays and update the pointer to the array
1562 in the fat pointer. Note that it is the first field. */
1564 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1566 /* Get and validate any specified Component_Size, but if Packed,
1567 ignore it since the front end will have taken care of it. */
1569 = validate_size (Component_Size (gnat_entity), tem,
1571 (Is_Bit_Packed_Array (gnat_entity)
1572 ? TYPE_DECL : VAR_DECL), 1,
1573 Has_Component_Size_Clause (gnat_entity));
1575 if (Has_Atomic_Components (gnat_entity))
1576 check_ok_for_atomic (tem, gnat_entity, 1);
1578 /* If the component type is a RECORD_TYPE that has a self-referential
1579 size, use the maxium size. */
1580 if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
1581 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1582 gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1584 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1586 tem = make_type_from_size (tem, gnu_comp_size, 0);
1587 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1588 "C_PAD", 0, definition, 1);
1591 if (Has_Volatile_Components (gnat_entity))
1592 tem = build_qualified_type (tem,
1593 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1595 /* If Component_Size is not already specified, annotate it with the
1596 size of the component. */
1597 if (Unknown_Component_Size (gnat_entity))
1598 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1600 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1601 size_binop (MULT_EXPR, gnu_max_size,
1602 TYPE_SIZE_UNIT (tem)));
1603 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1604 size_binop (MULT_EXPR,
1605 convert (bitsizetype,
1609 for (index = ndim - 1; index >= 0; index--)
1611 tem = build_array_type (tem, gnu_index_types[index]);
1612 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1614 /* If the type below this an multi-array type, then this
1615 does not not have aliased components.
1617 ??? Otherwise, for now, we say that any component of aggregate
1618 type is addressable because the front end may take 'Reference
1619 of it. But we have to make it addressable if it must be passed
1620 by reference or it that is the default. */
1621 TYPE_NONALIASED_COMPONENT (tem)
1622 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1623 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1624 : (! Has_Aliased_Components (gnat_entity)
1625 && ! AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1628 /* If an alignment is specified, use it if valid. But ignore it for
1629 types that represent the unpacked base type for packed arrays. */
1630 if (No (Packed_Array_Type (gnat_entity))
1631 && Known_Alignment (gnat_entity))
1633 if (No (Alignment (gnat_entity)))
1637 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1641 TYPE_CONVENTION_FORTRAN_P (tem)
1642 = (Convention (gnat_entity) == Convention_Fortran);
1643 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1645 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1646 corresponding fat pointer. */
1647 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1648 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1649 TYPE_MODE (gnu_type) = BLKmode;
1650 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1651 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1653 /* If the maximum size doesn't overflow, use it. */
1654 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1655 && ! TREE_OVERFLOW (gnu_max_size))
1657 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1658 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1659 && ! TREE_OVERFLOW (gnu_max_size_unit))
1660 TYPE_SIZE_UNIT (tem)
1661 = size_binop (MIN_EXPR, gnu_max_size_unit,
1662 TYPE_SIZE_UNIT (tem));
1664 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1665 tem, 0, ! Comes_From_Source (gnat_entity),
1667 rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1669 /* Create a record type for the object and its template and
1670 set the template at a negative offset. */
1671 tem = build_unc_object_type (gnu_template_type, tem,
1672 create_concat_name (gnat_entity, "XUT"));
1673 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1674 = size_binop (MINUS_EXPR, size_zero_node,
1675 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1676 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1677 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1678 = bitsize_zero_node;
1679 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1680 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1682 /* Give the thin pointer type a name. */
1683 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1684 build_pointer_type (tem), 0,
1685 ! Comes_From_Source (gnat_entity), debug_info_p);
1689 case E_String_Subtype:
1690 case E_Array_Subtype:
1692 /* This is the actual data type for array variables. Multidimensional
1693 arrays are implemented in the gnu tree as arrays of arrays. Note
1694 that for the moment arrays which have sparse enumeration subtypes as
1695 index components create sparse arrays, which is obviously space
1696 inefficient but so much easier to code for now.
1698 Also note that the subtype never refers to the unconstrained
1699 array type, which is somewhat at variance with Ada semantics.
1701 First check to see if this is simply a renaming of the array
1702 type. If so, the result is the array type. */
1704 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1705 if (! Is_Constrained (gnat_entity))
1710 int array_dim = Number_Dimensions (gnat_entity);
1712 = ((Convention (gnat_entity) == Convention_Fortran)
1713 ? array_dim - 1 : 0);
1715 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1716 Entity_Id gnat_ind_subtype;
1717 Entity_Id gnat_ind_base_subtype;
1718 tree gnu_base_type = gnu_type;
1719 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1720 tree gnu_comp_size = 0;
1721 tree gnu_max_size = size_one_node;
1722 tree gnu_max_size_unit;
1723 int need_index_type_struct = 0;
1724 int max_overflow = 0;
1726 /* First create the gnu types for each index. Create types for
1727 debugging information to point to the index types if the
1728 are not integer types, have variable bounds, or are
1729 wider than sizetype. */
1731 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1732 gnat_ind_base_subtype
1733 = First_Index (Implementation_Base_Type (gnat_entity));
1734 index < array_dim && index >= 0;
1736 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1737 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1739 tree gnu_index_subtype
1740 = get_unpadded_type (Etype (gnat_ind_subtype));
1742 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1744 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1745 tree gnu_base_subtype
1746 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1748 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1750 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1751 tree gnu_base_type = get_base_type (gnu_base_subtype);
1752 tree gnu_base_base_min
1753 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1754 tree gnu_base_base_max
1755 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1759 /* If the minimum and maximum values both overflow in
1760 SIZETYPE, but the difference in the original type
1761 does not overflow in SIZETYPE, ignore the overflow
1763 if ((TYPE_PRECISION (gnu_index_subtype)
1764 > TYPE_PRECISION (sizetype))
1765 && TREE_CODE (gnu_min) == INTEGER_CST
1766 && TREE_CODE (gnu_max) == INTEGER_CST
1767 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1769 (fold (build (MINUS_EXPR, gnu_index_subtype,
1770 TYPE_MAX_VALUE (gnu_index_subtype),
1771 TYPE_MIN_VALUE (gnu_index_subtype))))))
1772 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1773 = TREE_CONSTANT_OVERFLOW (gnu_min)
1774 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1776 /* Similarly, if the range is null, use bounds of 1..0 for
1777 the sizetype bounds. */
1778 else if ((TYPE_PRECISION (gnu_index_subtype)
1779 > TYPE_PRECISION (sizetype))
1780 && TREE_CODE (gnu_min) == INTEGER_CST
1781 && TREE_CODE (gnu_max) == INTEGER_CST
1782 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1783 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1784 TYPE_MIN_VALUE (gnu_index_subtype)))
1785 gnu_min = size_one_node, gnu_max = size_zero_node;
1787 /* Now compute the size of this bound. We need to provide
1788 GCC with an upper bound to use but have to deal with the
1789 "superflat" case. There are three ways to do this. If we
1790 can prove that the array can never be superflat, we can
1791 just use the high bound of the index subtype. If we can
1792 prove that the low bound minus one can't overflow, we
1793 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1794 the expression hb >= lb ? hb : lb - 1. */
1795 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1797 /* See if the base array type is already flat. If it is, we
1798 are probably compiling an ACVC test, but it will cause the
1799 code below to malfunction if we don't handle it specially. */
1800 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1801 && TREE_CODE (gnu_base_max) == INTEGER_CST
1802 && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1803 && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1804 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1805 gnu_high = size_zero_node, gnu_min = size_one_node;
1807 /* If gnu_high is now an integer which overflowed, the array
1808 cannot be superflat. */
1809 else if (TREE_CODE (gnu_high) == INTEGER_CST
1810 && TREE_OVERFLOW (gnu_high))
1812 else if (TREE_UNSIGNED (gnu_base_subtype)
1813 || TREE_CODE (gnu_high) == INTEGER_CST)
1814 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1818 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1822 gnu_index_type[index]
1823 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1825 /* Also compute the maximum size of the array. Here we
1826 see if any constraint on the index type of the base type
1827 can be used in the case of self-referential bound on
1828 the index type of the subtype. We look for a non-"infinite"
1829 and non-self-referential bound from any type involved and
1830 handle each bound separately. */
1832 if ((TREE_CODE (gnu_min) == INTEGER_CST
1833 && ! TREE_OVERFLOW (gnu_min)
1834 && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
1835 || ! CONTAINS_PLACEHOLDER_P (gnu_min))
1836 gnu_base_min = gnu_min;
1838 if ((TREE_CODE (gnu_max) == INTEGER_CST
1839 && ! TREE_OVERFLOW (gnu_max)
1840 && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
1841 || ! CONTAINS_PLACEHOLDER_P (gnu_max))
1842 gnu_base_max = gnu_max;
1844 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1845 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1846 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1847 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1848 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1849 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1852 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1853 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1856 = size_binop (MAX_EXPR,
1857 size_binop (PLUS_EXPR, size_one_node,
1858 size_binop (MINUS_EXPR, gnu_base_max,
1862 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1863 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1867 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1869 if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1870 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1872 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1873 || (TREE_TYPE (gnu_index_subtype) != 0
1874 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1876 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1877 || (TYPE_PRECISION (gnu_index_subtype)
1878 > TYPE_PRECISION (sizetype)))
1879 need_index_type_struct = 1;
1882 /* Then flatten: create the array of arrays. */
1884 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1886 /* One of the above calls might have caused us to be elaborated,
1887 so don't blow up if so. */
1888 if (present_gnu_tree (gnat_entity))
1894 /* Get and validate any specified Component_Size, but if Packed,
1895 ignore it since the front end will have taken care of it. */
1897 = validate_size (Component_Size (gnat_entity), gnu_type,
1899 (Is_Bit_Packed_Array (gnat_entity)
1900 ? TYPE_DECL : VAR_DECL),
1901 1, Has_Component_Size_Clause (gnat_entity));
1903 /* If the component type is a RECORD_TYPE that has a self-referential
1904 size, use the maxium size. */
1905 if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
1906 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
1907 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1909 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1911 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1912 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1913 gnat_entity, "C_PAD", 0,
1917 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1918 gnu_type = build_qualified_type (gnu_type,
1919 (TYPE_QUALS (gnu_type)
1920 | TYPE_QUAL_VOLATILE));
1922 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1923 TYPE_SIZE_UNIT (gnu_type));
1924 gnu_max_size = size_binop (MULT_EXPR,
1925 convert (bitsizetype, gnu_max_size),
1926 TYPE_SIZE (gnu_type));
1928 for (index = array_dim - 1; index >= 0; index --)
1930 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1931 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1932 /* If the type below this an multi-array type, then this
1933 does not not have aliased components.
1935 ??? Otherwise, for now, we say that any component of aggregate
1936 type is addressable because the front end may take 'Reference
1937 of it. But we have to make it addressable if it must be passed
1938 by reference or it that is the default. */
1939 TYPE_NONALIASED_COMPONENT (gnu_type)
1940 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1941 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
1942 : (! Has_Aliased_Components (gnat_entity)
1943 && ! AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
1946 /* If we are at file level and this is a multi-dimensional array, we
1947 need to make a variable corresponding to the stride of the
1948 inner dimensions. */
1949 if (global_bindings_p () && array_dim > 1)
1951 tree gnu_str_name = get_identifier ("ST");
1954 for (gnu_arr_type = TREE_TYPE (gnu_type);
1955 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1956 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1957 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1959 TYPE_SIZE (gnu_arr_type)
1960 = elaborate_expression_1 (gnat_entity, gnat_entity,
1961 TYPE_SIZE (gnu_arr_type),
1962 gnu_str_name, definition, 0);
1963 TYPE_SIZE_UNIT (gnu_arr_type)
1964 = elaborate_expression_1
1965 (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1966 concat_id_with_name (gnu_str_name, "U"), definition, 0);
1970 /* If we need to write out a record type giving the names of
1971 the bounds, do it now. */
1972 if (need_index_type_struct && debug_info_p)
1974 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1975 tree gnu_field_list = 0;
1978 TYPE_NAME (gnu_bound_rec_type)
1979 = create_concat_name (gnat_entity, "XA");
1981 for (index = array_dim - 1; index >= 0; index--)
1984 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1986 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1987 gnu_type_name = DECL_NAME (gnu_type_name);
1989 gnu_field = create_field_decl (gnu_type_name,
1992 0, NULL_TREE, NULL_TREE, 0);
1993 TREE_CHAIN (gnu_field) = gnu_field_list;
1994 gnu_field_list = gnu_field;
1997 finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
2000 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2001 = (Convention (gnat_entity) == Convention_Fortran);
2002 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2003 = Is_Packed_Array_Type (gnat_entity);
2005 /* If our size depends on a placeholder and the maximum size doesn't
2006 overflow, use it. */
2007 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2008 && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
2009 && TREE_OVERFLOW (gnu_max_size))
2010 && ! (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2011 && TREE_OVERFLOW (gnu_max_size_unit))
2014 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2015 TYPE_SIZE (gnu_type));
2016 TYPE_SIZE_UNIT (gnu_type)
2017 = size_binop (MIN_EXPR, gnu_max_size_unit,
2018 TYPE_SIZE_UNIT (gnu_type));
2021 /* Set our alias set to that of our base type. This gives all
2022 array subtypes the same alias set. */
2023 copy_alias_set (gnu_type, gnu_base_type);
2026 /* If this is a packed type, make this type the same as the packed
2027 array type, but do some adjusting in the type first. */
2029 if (Present (Packed_Array_Type (gnat_entity)))
2031 Entity_Id gnat_index;
2032 tree gnu_inner_type;
2034 /* First finish the type we had been making so that we output
2035 debugging information for it */
2036 gnu_type = build_qualified_type (gnu_type,
2037 (TYPE_QUALS (gnu_type)
2038 | (TYPE_QUAL_VOLATILE
2039 * Treat_As_Volatile (gnat_entity))));
2040 set_lineno (gnat_entity, 0);
2041 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2042 ! Comes_From_Source (gnat_entity),
2044 if (! Comes_From_Source (gnat_entity))
2045 DECL_ARTIFICIAL (gnu_decl) = 1;
2047 /* Save it as our equivalent in case the call below elaborates
2049 save_gnu_tree (gnat_entity, gnu_decl, 0);
2051 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2054 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2055 save_gnu_tree (gnat_entity, NULL_TREE, 0);
2057 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2058 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2059 || TYPE_IS_PADDING_P (gnu_inner_type)))
2060 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2062 /* We need to point the type we just made to our index type so
2063 the actual bounds can be put into a template. */
2065 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2066 && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2067 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2068 && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2070 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2072 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2073 If it is, we need to make another type. */
2074 if (TYPE_MODULAR_P (gnu_inner_type))
2078 gnu_subtype = make_node (INTEGER_TYPE);
2080 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2081 TYPE_MIN_VALUE (gnu_subtype)
2082 = TYPE_MIN_VALUE (gnu_inner_type);
2083 TYPE_MAX_VALUE (gnu_subtype)
2084 = TYPE_MAX_VALUE (gnu_inner_type);
2085 TYPE_PRECISION (gnu_subtype)
2086 = TYPE_PRECISION (gnu_inner_type);
2087 TREE_UNSIGNED (gnu_subtype)
2088 = TREE_UNSIGNED (gnu_inner_type);
2089 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2090 layout_type (gnu_subtype);
2092 gnu_inner_type = gnu_subtype;
2095 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2098 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2100 for (gnat_index = First_Index (gnat_entity);
2101 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2102 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type,
2103 tree_cons (NULL_TREE,
2104 get_unpadded_type (Etype (gnat_index)),
2105 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2107 if (Convention (gnat_entity) != Convention_Fortran)
2108 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type,
2109 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2111 if (TREE_CODE (gnu_type) == RECORD_TYPE
2112 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2113 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2117 /* Abort if packed array with no packed array type field set. */
2118 else if (Is_Packed (gnat_entity))
2123 case E_String_Literal_Subtype:
2124 /* Create the type for a string literal. */
2126 Entity_Id gnat_full_type
2127 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2128 && Present (Full_View (Etype (gnat_entity)))
2129 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2130 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2131 tree gnu_string_array_type
2132 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2133 tree gnu_string_index_type
2134 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2135 (TYPE_DOMAIN (gnu_string_array_type))));
2136 tree gnu_lower_bound
2137 = convert (gnu_string_index_type,
2138 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2139 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2140 tree gnu_length = ssize_int (length - 1);
2141 tree gnu_upper_bound
2142 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2144 convert (gnu_string_index_type, gnu_length));
2146 = build_range_type (gnu_string_index_type,
2147 gnu_lower_bound, gnu_upper_bound);
2149 = create_index_type (convert (sizetype,
2150 TYPE_MIN_VALUE (gnu_range_type)),
2152 TYPE_MAX_VALUE (gnu_range_type)),
2156 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2161 /* Record Types and Subtypes
2163 The following fields are defined on record types:
2165 Has_Discriminants True if the record has discriminants
2166 First_Discriminant Points to head of list of discriminants
2167 First_Entity Points to head of list of fields
2168 Is_Tagged_Type True if the record is tagged
2170 Implementation of Ada records and discriminated records:
2172 A record type definition is transformed into the equivalent of a C
2173 struct definition. The fields that are the discriminants which are
2174 found in the Full_Type_Declaration node and the elements of the
2175 Component_List found in the Record_Type_Definition node. The
2176 Component_List can be a recursive structure since each Variant of
2177 the Variant_Part of the Component_List has a Component_List.
2179 Processing of a record type definition comprises starting the list of
2180 field declarations here from the discriminants and the calling the
2181 function components_to_record to add the rest of the fields from the
2182 component list and return the gnu type node. The function
2183 components_to_record will call itself recursively as it traverses
2187 if (Has_Complex_Representation (gnat_entity))
2190 = build_complex_type
2192 (Etype (Defining_Entity
2193 (First (Component_Items
2196 (Declaration_Node (gnat_entity)))))))));
2202 Node_Id full_definition = Declaration_Node (gnat_entity);
2203 Node_Id record_definition = Type_Definition (full_definition);
2204 Entity_Id gnat_field;
2206 tree gnu_field_list = NULL_TREE;
2207 tree gnu_get_parent;
2208 int packed = (Is_Packed (gnat_entity) ? 1
2209 : (Component_Alignment (gnat_entity)
2210 == Calign_Storage_Unit) ? -1
2212 int has_rep = Has_Specified_Layout (gnat_entity);
2213 int all_rep = has_rep;
2215 = (Is_Tagged_Type (gnat_entity)
2216 && Nkind (record_definition) == N_Derived_Type_Definition);
2218 /* See if all fields have a rep clause. Stop when we find one
2220 for (gnat_field = First_Entity (gnat_entity);
2221 Present (gnat_field) && all_rep;
2222 gnat_field = Next_Entity (gnat_field))
2223 if ((Ekind (gnat_field) == E_Component
2224 || Ekind (gnat_field) == E_Discriminant)
2225 && No (Component_Clause (gnat_field)))
2228 /* If this is a record extension, go a level further to find the
2229 record definition. Also, verify we have a Parent_Subtype. */
2232 if (! type_annotate_only
2233 || Present (Record_Extension_Part (record_definition)))
2234 record_definition = Record_Extension_Part (record_definition);
2236 if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2240 /* Make a node for the record. If we are not defining the record,
2241 suppress expanding incomplete types and save the node as the type
2242 for GNAT_ENTITY. We use the same RECORD_TYPE as for a dummy type
2243 and reset TYPE_DUMMY_P to show it's no longer a dummy.
2245 It is very tempting to delay resetting this bit until we are done
2246 with completing the type, e.g. to let possible intermediate
2247 elaboration of access types designating the record know it is not
2248 complete and arrange for update_pointer_to to fix things up later.
2250 It would be wrong, however, because dummy types are expected only
2251 to be created for Ada incomplete or private types, which is not
2252 what we have here. Doing so would make other parts of gigi think
2253 we are dealing with a really incomplete or private type, and have
2254 nasty side effects, typically on the generation of the associated
2255 debugging information. */
2256 gnu_type = make_dummy_type (gnat_entity);
2257 TYPE_DUMMY_P (gnu_type) = 0;
2259 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2260 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2262 TYPE_ALIGN (gnu_type) = 0;
2263 TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2267 defer_incomplete_level++;
2269 set_lineno (gnat_entity, 0);
2270 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2271 ! Comes_From_Source (gnat_entity),
2273 save_gnu_tree (gnat_entity, gnu_decl, 0);
2274 this_made_decl = saved = 1;
2277 /* If both a size and rep clause was specified, put the size in
2278 the record type now so that it can get the proper mode. */
2279 if (has_rep && Known_Esize (gnat_entity))
2280 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2282 /* Always set the alignment here so that it can be used to
2283 set the mode, if it is making the alignment stricter. If
2284 it is invalid, it will be checked again below. If this is to
2285 be Atomic, choose a default alignment of a word unless we know
2286 the size and it's smaller. */
2287 if (Known_Alignment (gnat_entity))
2288 TYPE_ALIGN (gnu_type)
2289 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2290 else if (Is_Atomic (gnat_entity))
2291 TYPE_ALIGN (gnu_type)
2292 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2293 : 1 << ((floor_log2 (esize) - 1) + 1));
2295 /* If we have a Parent_Subtype, make a field for the parent. If
2296 this record has rep clauses, force the position to zero. */
2297 if (Present (Parent_Subtype (gnat_entity)))
2301 /* A major complexity here is that the parent subtype will
2302 reference our discriminants. But those must reference
2303 the parent component of this record. So here we will
2304 initialize each of those components to a COMPONENT_REF.
2305 The first operand of that COMPONENT_REF is another
2306 COMPONENT_REF which will be filled in below, once
2307 the parent type can be safely built. */
2309 gnu_get_parent = build (COMPONENT_REF, void_type_node,
2310 build (PLACEHOLDER_EXPR, gnu_type),
2311 build_decl (FIELD_DECL, NULL_TREE,
2314 if (Has_Discriminants (gnat_entity))
2315 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2316 Present (gnat_field);
2317 gnat_field = Next_Stored_Discriminant (gnat_field))
2318 if (Present (Corresponding_Discriminant (gnat_field)))
2321 build (COMPONENT_REF,
2322 get_unpadded_type (Etype (gnat_field)),
2324 gnat_to_gnu_entity (Corresponding_Discriminant
2329 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2332 = create_field_decl (get_identifier
2333 (Get_Name_String (Name_uParent)),
2334 gnu_parent, gnu_type, 0,
2335 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2336 has_rep ? bitsize_zero_node : 0, 1);
2337 DECL_INTERNAL_P (gnu_field_list) = 1;
2339 TREE_TYPE (gnu_get_parent) = gnu_parent;
2340 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2343 /* Add the fields for the discriminants into the record. */
2344 if (! Is_Unchecked_Union (gnat_entity)
2345 && Has_Discriminants (gnat_entity))
2346 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2347 Present (gnat_field);
2348 gnat_field = Next_Stored_Discriminant (gnat_field))
2350 /* If this is a record extension and this discriminant
2351 is the renaming of another discriminant, we've already
2352 handled the discriminant above. */
2353 if (Present (Parent_Subtype (gnat_entity))
2354 && Present (Corresponding_Discriminant (gnat_field)))
2358 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2360 /* Make an expression using a PLACEHOLDER_EXPR from the
2361 FIELD_DECL node just created and link that with the
2362 corresponding GNAT defining identifier. Then add to the
2364 save_gnu_tree (gnat_field,
2365 build (COMPONENT_REF, TREE_TYPE (gnu_field),
2366 build (PLACEHOLDER_EXPR,
2367 DECL_CONTEXT (gnu_field)),
2371 TREE_CHAIN (gnu_field) = gnu_field_list;
2372 gnu_field_list = gnu_field;
2375 /* Put the discriminants into the record (backwards), so we can
2376 know the appropriate discriminant to use for the names of the
2378 TYPE_FIELDS (gnu_type) = gnu_field_list;
2380 /* Add the listed fields into the record and finish up. */
2381 components_to_record (gnu_type, Component_List (record_definition),
2382 gnu_field_list, packed, definition, 0,
2385 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2386 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2388 /* If this is an extension type, reset the tree for any
2389 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2390 for non-inherited discriminants. */
2391 if (! Is_Unchecked_Union (gnat_entity)
2392 && Has_Discriminants (gnat_entity))
2393 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2394 Present (gnat_field);
2395 gnat_field = Next_Stored_Discriminant (gnat_field))
2397 if (Present (Parent_Subtype (gnat_entity))
2398 && Present (Corresponding_Discriminant (gnat_field)))
2399 save_gnu_tree (gnat_field, NULL_TREE, 0);
2402 gnu_field = get_gnu_tree (gnat_field);
2403 save_gnu_tree (gnat_field, NULL_TREE, 0);
2404 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2408 /* If it is a tagged record force the type to BLKmode to insure
2409 that these objects will always be placed in memory. Do the
2410 same thing for limited record types. */
2411 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2412 TYPE_MODE (gnu_type) = BLKmode;
2414 /* If this is a derived type, we must make the alias set of this type
2415 the same as that of the type we are derived from. We assume here
2416 that the other type is already frozen. */
2417 if (Etype (gnat_entity) != gnat_entity
2418 && ! (Is_Private_Type (Etype (gnat_entity))
2419 && Full_View (Etype (gnat_entity)) == gnat_entity))
2420 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2422 /* Fill in locations of fields. */
2423 annotate_rep (gnat_entity, gnu_type);
2425 /* If there are any entities in the chain corresponding to
2426 components that we did not elaborate, ensure we elaborate their
2427 types if they are Itypes. */
2428 for (gnat_temp = First_Entity (gnat_entity);
2429 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2430 if ((Ekind (gnat_temp) == E_Component
2431 || Ekind (gnat_temp) == E_Discriminant)
2432 && Is_Itype (Etype (gnat_temp))
2433 && ! present_gnu_tree (gnat_temp))
2434 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2438 case E_Class_Wide_Subtype:
2439 /* If an equivalent type is present, that is what we should use.
2440 Otherwise, fall through to handle this like a record subtype
2441 since it may have constraints. */
2443 if (Present (Equivalent_Type (gnat_entity)))
2445 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2451 /* ... fall through ... */
2453 case E_Record_Subtype:
2455 /* If Cloned_Subtype is Present it means this record subtype has
2456 identical layout to that type or subtype and we should use
2457 that GCC type for this one. The front end guarantees that
2458 the component list is shared. */
2459 if (Present (Cloned_Subtype (gnat_entity)))
2461 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2466 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2467 changing the type, make a new type with each field having the
2468 type of the field in the new subtype but having the position
2469 computed by transforming every discriminant reference according
2470 to the constraints. We don't see any difference between
2471 private and nonprivate type here since derivations from types should
2472 have been deferred until the completion of the private type. */
2475 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2480 defer_incomplete_level++, this_deferred = 1;
2482 /* Get the base type initially for its alignment and sizes. But
2483 if it is a padded type, we do all the other work with the
2485 gnu_type = gnu_orig_type = gnu_base_type
2486 = gnat_to_gnu_type (gnat_base_type);
2488 if (TREE_CODE (gnu_type) == RECORD_TYPE
2489 && TYPE_IS_PADDING_P (gnu_type))
2490 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2492 if (present_gnu_tree (gnat_entity))
2498 /* When the type has discriminants, and these discriminants
2499 affect the shape of what it built, factor them in.
2501 If we are making a subtype of an Unchecked_Union (must be an
2502 Itype), just return the type.
2504 We can't just use Is_Constrained because private subtypes without
2505 discriminants of full types with discriminants with default
2506 expressions are Is_Constrained but aren't constrained! */
2508 if (IN (Ekind (gnat_base_type), Record_Kind)
2509 && ! Is_For_Access_Subtype (gnat_entity)
2510 && ! Is_Unchecked_Union (gnat_base_type)
2511 && Is_Constrained (gnat_entity)
2512 && Stored_Constraint (gnat_entity) != No_Elist
2513 && Present (Discriminant_Constraint (gnat_entity)))
2515 Entity_Id gnat_field;
2516 Entity_Id gnat_root_type;
2517 tree gnu_field_list = 0;
2519 = compute_field_positions (gnu_orig_type, NULL_TREE,
2520 size_zero_node, bitsize_zero_node,
2523 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2527 /* If this is a derived type, we may be seeing fields from any
2528 original records, so add those positions and discriminant
2529 substitutions to our lists. */
2530 for (gnat_root_type = gnat_base_type;
2531 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2532 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2535 = compute_field_positions
2536 (gnat_to_gnu_type (Etype (gnat_root_type)),
2537 gnu_pos_list, size_zero_node, bitsize_zero_node,
2540 if (Present (Parent_Subtype (gnat_root_type)))
2542 = substitution_list (Parent_Subtype (gnat_root_type),
2543 Empty, gnu_subst_list, definition);
2546 gnu_type = make_node (RECORD_TYPE);
2547 TYPE_NAME (gnu_type) = gnu_entity_id;
2548 TYPE_STUB_DECL (gnu_type)
2549 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2550 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2552 for (gnat_field = First_Entity (gnat_entity);
2553 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2554 if (Ekind (gnat_field) == E_Component
2555 || Ekind (gnat_field) == E_Discriminant)
2558 = gnat_to_gnu_entity
2559 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2561 = TREE_VALUE (purpose_member (gnu_old_field,
2563 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2564 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2566 = gnat_to_gnu_type (Etype (gnat_field));
2567 tree gnu_size = TYPE_SIZE (gnu_field_type);
2568 tree gnu_new_pos = 0;
2569 unsigned int offset_align
2570 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2574 /* If there was a component clause, the field types must be
2575 the same for the type and subtype, so copy the data from
2576 the old field to avoid recomputation here. */
2577 if (Present (Component_Clause
2578 (Original_Record_Component (gnat_field))))
2580 gnu_size = DECL_SIZE (gnu_old_field);
2581 gnu_field_type = TREE_TYPE (gnu_old_field);
2584 /* If this was a bitfield, get the size from the old field.
2585 Also ensure the type can be placed into a bitfield. */
2586 else if (DECL_BIT_FIELD (gnu_old_field))
2588 gnu_size = DECL_SIZE (gnu_old_field);
2589 if (TYPE_MODE (gnu_field_type) == BLKmode
2590 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2591 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2592 gnu_field_type = make_packable_type (gnu_field_type);
2595 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2596 for (gnu_temp = gnu_subst_list;
2597 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2598 gnu_pos = substitute_in_expr (gnu_pos,
2599 TREE_PURPOSE (gnu_temp),
2600 TREE_VALUE (gnu_temp));
2602 /* If the size is now a constant, we can set it as the
2603 size of the field when we make it. Otherwise, we need
2604 to deal with it specially. */
2605 if (TREE_CONSTANT (gnu_pos))
2606 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2610 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2611 0, gnu_size, gnu_new_pos,
2612 ! DECL_NONADDRESSABLE_P (gnu_old_field));
2614 if (! TREE_CONSTANT (gnu_pos))
2616 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2617 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2618 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2619 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2620 DECL_SIZE (gnu_field) = gnu_size;
2621 DECL_SIZE_UNIT (gnu_field)
2622 = convert (sizetype,
2623 size_binop (CEIL_DIV_EXPR, gnu_size,
2624 bitsize_unit_node));
2625 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2628 DECL_INTERNAL_P (gnu_field)
2629 = DECL_INTERNAL_P (gnu_old_field);
2630 SET_DECL_ORIGINAL_FIELD (gnu_field,
2631 (DECL_ORIGINAL_FIELD (gnu_old_field) != 0
2632 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2634 DECL_DISCRIMINANT_NUMBER (gnu_field)
2635 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2636 TREE_THIS_VOLATILE (gnu_field)
2637 = TREE_THIS_VOLATILE (gnu_old_field);
2638 TREE_CHAIN (gnu_field) = gnu_field_list;
2639 gnu_field_list = gnu_field;
2640 save_gnu_tree (gnat_field, gnu_field, 0);
2643 finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2645 /* Now set the size, alignment and alias set of the new type to
2646 match that of the old one, doing any substitutions, as
2648 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2649 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2650 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2651 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2652 copy_alias_set (gnu_type, gnu_base_type);
2654 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2655 for (gnu_temp = gnu_subst_list;
2656 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2657 TYPE_SIZE (gnu_type)
2658 = substitute_in_expr (TYPE_SIZE (gnu_type),
2659 TREE_PURPOSE (gnu_temp),
2660 TREE_VALUE (gnu_temp));
2662 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2663 for (gnu_temp = gnu_subst_list;
2664 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2665 TYPE_SIZE_UNIT (gnu_type)
2666 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2667 TREE_PURPOSE (gnu_temp),
2668 TREE_VALUE (gnu_temp));
2670 if (TYPE_ADA_SIZE (gnu_type) != 0
2671 && CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2672 for (gnu_temp = gnu_subst_list;
2673 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2674 SET_TYPE_ADA_SIZE (gnu_type,
2675 substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2676 TREE_PURPOSE (gnu_temp),
2677 TREE_VALUE (gnu_temp)));
2679 /* Recompute the mode of this record type now that we know its
2681 compute_record_mode (gnu_type);
2683 /* Fill in locations of fields. */
2684 annotate_rep (gnat_entity, gnu_type);
2687 /* If we've made a new type, record it and make an XVS type to show
2688 what this is a subtype of. Some debuggers require the XVS
2689 type to be output first, so do it in that order. */
2690 if (gnu_type != gnu_orig_type)
2694 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2695 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2697 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2698 gnu_orig_name = DECL_NAME (gnu_orig_name);
2700 TYPE_NAME (gnu_subtype_marker)
2701 = create_concat_name (gnat_entity, "XVS");
2702 finish_record_type (gnu_subtype_marker,
2703 create_field_decl (gnu_orig_name,
2711 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2712 TYPE_NAME (gnu_type) = gnu_entity_id;
2713 TYPE_STUB_DECL (gnu_type)
2714 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2716 DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2717 DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2718 rest_of_type_compilation (gnu_type, global_bindings_p ());
2721 /* Otherwise, go down all the components in the new type and
2722 make them equivalent to those in the base type. */
2724 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2725 gnat_temp = Next_Entity (gnat_temp))
2726 if ((Ekind (gnat_temp) == E_Discriminant
2727 && ! Is_Unchecked_Union (gnat_base_type))
2728 || Ekind (gnat_temp) == E_Component)
2729 save_gnu_tree (gnat_temp,
2731 (Original_Record_Component (gnat_temp)), 0);
2735 case E_Access_Subprogram_Type:
2736 /* If we are not defining this entity, and we have incomplete
2737 entities being processed above us, make a dummy type and
2738 fill it in later. */
2739 if (! definition && defer_incomplete_level != 0)
2741 struct incomplete *p
2742 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2745 = build_pointer_type
2746 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2747 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2748 ! Comes_From_Source (gnat_entity),
2750 save_gnu_tree (gnat_entity, gnu_decl, 0);
2751 this_made_decl = saved = 1;
2753 p->old_type = TREE_TYPE (gnu_type);
2754 p->full_type = Directly_Designated_Type (gnat_entity);
2755 p->next = defer_incomplete_list;
2756 defer_incomplete_list = p;
2760 /* ... fall through ... */
2762 case E_Allocator_Type:
2764 case E_Access_Attribute_Type:
2765 case E_Anonymous_Access_Type:
2766 case E_General_Access_Type:
2768 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2769 Entity_Id gnat_desig_full
2770 = ((IN (Ekind (Etype (gnat_desig_type)),
2771 Incomplete_Or_Private_Kind))
2772 ? Full_View (gnat_desig_type) : 0);
2773 /* We want to know if we'll be seeing the freeze node for any
2774 incomplete type we may be pointing to. */
2776 = (Present (gnat_desig_full)
2777 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2778 : In_Extended_Main_Code_Unit (gnat_desig_type));
2781 tree gnu_desig_type = 0;
2782 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2784 if (!targetm.valid_pointer_mode (p_mode))
2787 if (No (gnat_desig_full)
2788 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2789 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2790 && Present (Equivalent_Type (gnat_desig_type)))))
2792 if (Present (Equivalent_Type (gnat_desig_type)))
2794 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2795 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2796 gnat_desig_full = Full_View (gnat_desig_full);
2798 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2799 Incomplete_Or_Private_Kind))
2800 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2803 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2804 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2806 /* If either the designated type or its full view is an
2807 unconstrained array subtype, replace it with the type it's a
2808 subtype of. This avoids problems with multiple copies of
2809 unconstrained array types. */
2810 if (Ekind (gnat_desig_type) == E_Array_Subtype
2811 && ! Is_Constrained (gnat_desig_type))
2812 gnat_desig_type = Etype (gnat_desig_type);
2813 if (Present (gnat_desig_full)
2814 && Ekind (gnat_desig_full) == E_Array_Subtype
2815 && ! Is_Constrained (gnat_desig_full))
2816 gnat_desig_full = Etype (gnat_desig_full);
2818 /* If the designated type is a subtype of an incomplete record type,
2819 use the parent type to avoid order of elaboration issues. This
2820 can lose some code efficiency, but there is no alternative. */
2821 if (Present (gnat_desig_full)
2822 && Ekind (gnat_desig_full) == E_Record_Subtype
2823 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2824 gnat_desig_full = Etype (gnat_desig_full);
2826 /* If we are pointing to an incomplete type whose completion is an
2827 unconstrained array, make a fat pointer type instead of a pointer
2828 to VOID. The two types in our fields will be pointers to VOID and
2829 will be replaced in update_pointer_to. Similiarly, if the type
2830 itself is a dummy type or an unconstrained array. Also make
2831 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2834 if ((Present (gnat_desig_full)
2835 && Is_Array_Type (gnat_desig_full)
2836 && ! Is_Constrained (gnat_desig_full))
2837 || (present_gnu_tree (gnat_desig_type)
2838 && TYPE_IS_DUMMY_P (TREE_TYPE
2839 (get_gnu_tree (gnat_desig_type)))
2840 && Is_Array_Type (gnat_desig_type)
2841 && ! Is_Constrained (gnat_desig_type))
2842 || (present_gnu_tree (gnat_desig_type)
2843 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2844 == UNCONSTRAINED_ARRAY_TYPE)
2845 && (TYPE_POINTER_TO (TREE_TYPE
2846 (get_gnu_tree (gnat_desig_type)))
2848 || (No (gnat_desig_full) && ! in_main_unit
2849 && defer_incomplete_level != 0
2850 && ! present_gnu_tree (gnat_desig_type)
2851 && Is_Array_Type (gnat_desig_type)
2852 && ! Is_Constrained (gnat_desig_type)))
2855 = (present_gnu_tree (gnat_desig_type)
2856 ? gnat_to_gnu_type (gnat_desig_type)
2857 : make_dummy_type (gnat_desig_type));
2860 /* Show the dummy we get will be a fat pointer. */
2861 got_fat_p = made_dummy = 1;
2863 /* If the call above got something that has a pointer, that
2864 pointer is our type. This could have happened either
2865 because the type was elaborated or because somebody
2866 else executed the code below. */
2867 gnu_type = TYPE_POINTER_TO (gnu_old);
2870 gnu_type = make_node (RECORD_TYPE);
2871 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2872 TYPE_POINTER_TO (gnu_old) = gnu_type;
2874 set_lineno (gnat_entity, 0);
2876 = chainon (chainon (NULL_TREE,
2878 (get_identifier ("P_ARRAY"),
2879 ptr_void_type_node, gnu_type,
2881 create_field_decl (get_identifier ("P_BOUNDS"),
2883 gnu_type, 0, 0, 0, 0));
2885 /* Make sure we can place this into a register. */
2886 TYPE_ALIGN (gnu_type)
2887 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2888 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2889 finish_record_type (gnu_type, fields, 0, 1);
2891 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2892 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2893 = concat_id_with_name (get_entity_name (gnat_desig_type),
2895 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2899 /* If we already know what the full type is, use it. */
2900 else if (Present (gnat_desig_full)
2901 && present_gnu_tree (gnat_desig_full))
2902 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2904 /* Get the type of the thing we are to point to and build a pointer
2905 to it. If it is a reference to an incomplete or private type with a
2906 full view that is a record, make a dummy type node and get the
2907 actual type later when we have verified it is safe. */
2908 else if (! in_main_unit
2909 && ! present_gnu_tree (gnat_desig_type)
2910 && Present (gnat_desig_full)
2911 && ! present_gnu_tree (gnat_desig_full)
2912 && Is_Record_Type (gnat_desig_full))
2914 gnu_desig_type = make_dummy_type (gnat_desig_type);
2918 /* Likewise if we are pointing to a record or array and we are to defer
2919 elaborating incomplete types. We do this since this access type
2920 may be the full view of some private type. Note that the
2921 unconstrained array case is handled above. */
2922 else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2923 && ! present_gnu_tree (gnat_desig_type)
2924 && ((Is_Record_Type (gnat_desig_type)
2925 || Is_Array_Type (gnat_desig_type))
2926 || (Present (gnat_desig_full)
2927 && (Is_Record_Type (gnat_desig_full)
2928 || Is_Array_Type (gnat_desig_full)))))
2930 gnu_desig_type = make_dummy_type (gnat_desig_type);
2933 else if (gnat_desig_type == gnat_entity)
2935 gnu_type = build_pointer_type_for_mode (make_node (VOID_TYPE),
2937 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2940 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
2942 /* It is possible that the above call to gnat_to_gnu_type resolved our
2943 type. If so, just return it. */
2944 if (present_gnu_tree (gnat_entity))
2950 /* If we have a GCC type for the designated type, possibly modify it
2951 if we are pointing only to constant objects and then make a pointer
2952 to it. Don't do this for unconstrained arrays. */
2953 if (gnu_type == 0 && gnu_desig_type != 0)
2955 if (Is_Access_Constant (gnat_entity)
2956 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
2959 = build_qualified_type
2961 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
2963 /* Some extra processing is required if we are building a
2964 pointer to an incomplete type (in the GCC sense). We might
2965 have such a type if we just made a dummy, or directly out
2966 of the call to gnat_to_gnu_type above if we are processing
2967 an access type for a record component designating the
2968 record type itself. */
2969 if (! COMPLETE_TYPE_P (gnu_desig_type))
2971 /* We must ensure that the pointer to variant we make will
2972 be processed by update_pointer_to when the initial type
2973 is completed. Pretend we made a dummy and let further
2974 processing act as usual. */
2977 /* We must ensure that update_pointer_to will not retrieve
2978 the dummy variant when building a properly qualified
2979 version of the complete type. We take advantage of the
2980 fact that get_qualified_type is requiring TYPE_NAMEs to
2981 match to influence build_qualified_type and then also
2982 update_pointer_to here. */
2983 TYPE_NAME (gnu_desig_type)
2984 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
2988 gnu_type = build_pointer_type_for_mode (gnu_desig_type, p_mode);
2991 /* If we are not defining this object and we made a dummy pointer,
2992 save our current definition, evaluate the actual type, and replace
2993 the tentative type we made with the actual one. If we are to defer
2994 actually looking up the actual type, make an entry in the
2997 if (! in_main_unit && made_dummy)
3000 = TYPE_FAT_POINTER_P (gnu_type)
3001 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3003 if (esize == POINTER_SIZE
3004 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3006 = build_pointer_type
3007 (TYPE_OBJECT_RECORD_TYPE
3008 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3010 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3011 ! Comes_From_Source (gnat_entity),
3013 save_gnu_tree (gnat_entity, gnu_decl, 0);
3014 this_made_decl = saved = 1;
3016 if (defer_incomplete_level == 0)
3018 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3019 gnat_to_gnu_type (gnat_desig_type));
3020 /* Note that the call to gnat_to_gnu_type here might have
3021 updated gnu_old_type directly, in which case it is not a
3022 dummy type any more when we get into update_pointer_to.
3024 This may happen for instance when the designated type is a
3025 record type, because their elaboration starts with an
3026 initial node from make_dummy_type, which may yield the same
3027 node as the one we got.
3029 Besides, variants of this non-dummy type might have been
3030 created along the way. update_pointer_to is expected to
3031 properly take care of those situations. */
3035 struct incomplete *p
3036 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3038 p->old_type = gnu_old_type;
3039 p->full_type = gnat_desig_type;
3040 p->next = defer_incomplete_list;
3041 defer_incomplete_list = p;
3047 case E_Access_Protected_Subprogram_Type:
3048 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3049 gnu_type = build_pointer_type (void_type_node);
3051 /* The runtime representation is the equivalent type. */
3052 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3054 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3055 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
3056 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3057 && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3058 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3063 case E_Access_Subtype:
3065 /* We treat this as identical to its base type; any constraint is
3066 meaningful only to the front end.
3068 The designated type must be elaborated as well, if it does
3069 not have its own freeze node. Designated (sub)types created
3070 for constrained components of records with discriminants are
3071 not frozen by the front end and thus not elaborated by gigi,
3072 because their use may appear before the base type is frozen,
3073 and because it is not clear that they are needed anywhere in
3074 Gigi. With the current model, there is no correct place where
3075 they could be elaborated. */
3077 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3078 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3079 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
3080 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3081 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3083 /* If we are not defining this entity, and we have incomplete
3084 entities being processed above us, make a dummy type and
3085 elaborate it later. */
3086 if (! definition && defer_incomplete_level != 0)
3088 struct incomplete *p
3089 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3091 = build_pointer_type
3092 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3094 p->old_type = TREE_TYPE (gnu_ptr_type);
3095 p->full_type = Directly_Designated_Type (gnat_entity);
3096 p->next = defer_incomplete_list;
3097 defer_incomplete_list = p;
3100 (IN (Ekind (Base_Type (Directly_Designated_Type (gnat_entity))),
3101 Incomplete_Or_Private_Kind))
3104 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3111 /* Subprogram Entities
3113 The following access functions are defined for subprograms (functions
3116 First_Formal The first formal parameter.
3117 Is_Imported Indicates that the subprogram has appeared in
3118 an INTERFACE or IMPORT pragma. For now we
3119 assume that the external language is C.
3120 Is_Inlined True if the subprogram is to be inlined.
3122 In addition for function subprograms we have:
3124 Etype Return type of the function.
3126 Each parameter is first checked by calling must_pass_by_ref on its
3127 type to determine if it is passed by reference. For parameters which
3128 are copied in, if they are Ada IN OUT or OUT parameters, their return
3129 value becomes part of a record which becomes the return type of the
3130 function (C function - note that this applies only to Ada procedures
3131 so there is no Ada return type). Additional code to store back the
3132 parameters will be generated on the caller side. This transformation
3133 is done here, not in the front-end.
3135 The intended result of the transformation can be seen from the
3136 equivalent source rewritings that follow:
3138 struct temp {int a,b};
3139 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3141 end P; return {A,B};
3151 For subprogram types we need to perform mainly the same conversions to
3152 GCC form that are needed for procedures and function declarations. The
3153 only difference is that at the end, we make a type declaration instead
3154 of a function declaration. */
3156 case E_Subprogram_Type:
3160 /* The first GCC parameter declaration (a PARM_DECL node). The
3161 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3162 actually is the head of this parameter list. */
3163 tree gnu_param_list = NULL_TREE;
3164 /* The type returned by a function. If the subprogram is a procedure
3165 this type should be void_type_node. */
3166 tree gnu_return_type = void_type_node;
3167 /* List of fields in return type of procedure with copy in copy out
3169 tree gnu_field_list = NULL_TREE;
3170 /* Non-null for subprograms containing parameters passed by copy in
3171 copy out (Ada IN OUT or OUT parameters not passed by reference),
3172 in which case it is the list of nodes used to specify the values of
3173 the in out/out parameters that are returned as a record upon
3174 procedure return. The TREE_PURPOSE of an element of this list is
3175 a field of the record and the TREE_VALUE is the PARM_DECL
3176 corresponding to that field. This list will be saved in the
3177 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3178 tree gnu_return_list = NULL_TREE;
3179 Entity_Id gnat_param;
3180 int inline_flag = Is_Inlined (gnat_entity);
3181 int public_flag = Is_Public (gnat_entity);
3183 = (Is_Public (gnat_entity) && !definition) || imported_p;
3184 int pure_flag = Is_Pure (gnat_entity);
3185 int volatile_flag = No_Return (gnat_entity);
3186 int returns_by_ref = 0;
3187 int returns_unconstrained = 0;
3188 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3189 int has_copy_in_out = 0;
3192 if (kind == E_Subprogram_Type && ! definition)
3193 /* A parameter may refer to this type, so defer completion
3194 of any incomplete types. */
3195 defer_incomplete_level++, this_deferred = 1;
3197 /* If the subprogram has an alias, it is probably inherited, so
3198 we can use the original one. If the original "subprogram"
3199 is actually an enumeration literal, it may be the first use
3200 of its type, so we must elaborate that type now. */
3201 if (Present (Alias (gnat_entity)))
3203 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3204 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3206 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3209 /* Elaborate any Itypes in the parameters of this entity. */
3210 for (gnat_temp = First_Formal (gnat_entity);
3211 Present (gnat_temp);
3212 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3213 if (Is_Itype (Etype (gnat_temp)))
3214 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3219 if (kind == E_Function || kind == E_Subprogram_Type)
3220 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3222 /* If this function returns by reference, make the actual
3223 return type of this function the pointer and mark the decl. */
3224 if (Returns_By_Ref (gnat_entity))
3227 gnu_return_type = build_pointer_type (gnu_return_type);
3230 /* If the Mechanism is By_Reference, ensure the return type uses
3231 the machine's by-reference mechanism, which may not the same
3232 as above (e.g., it might be by passing a fake parameter). */
3233 else if (kind == E_Function
3234 && Mechanism (gnat_entity) == By_Reference)
3236 gnu_return_type = copy_type (gnu_return_type);
3237 TREE_ADDRESSABLE (gnu_return_type) = 1;
3240 /* If we are supposed to return an unconstrained array,
3241 actually return a fat pointer and make a note of that. Return
3242 a pointer to an unconstrained record of variable size. */
3243 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3245 gnu_return_type = TREE_TYPE (gnu_return_type);
3246 returns_unconstrained = 1;
3249 /* If the type requires a transient scope, the result is allocated
3250 on the secondary stack, so the result type of the function is
3252 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3254 gnu_return_type = build_pointer_type (gnu_return_type);
3255 returns_unconstrained = 1;
3258 /* If the type is a padded type and the underlying type would not
3259 be passed by reference or this function has a foreign convention,
3260 return the underlying type. */
3261 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3262 && TYPE_IS_PADDING_P (gnu_return_type)
3263 && (! default_pass_by_ref (TREE_TYPE
3264 (TYPE_FIELDS (gnu_return_type)))
3265 || Has_Foreign_Convention (gnat_entity)))
3266 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3268 /* Look at all our parameters and get the type of
3269 each. While doing this, build a copy-out structure if
3272 /* If the return type has a size that overflows, we cannot have
3273 a function that returns that type. This usage doesn't make
3274 sense anyway, so give an error here. */
3275 if (TYPE_SIZE_UNIT (gnu_return_type)
3276 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3278 post_error ("cannot return type whose size overflows",
3280 gnu_return_type = copy_node (gnu_return_type);
3281 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3282 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3283 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3284 TYPE_NEXT_VARIANT (gnu_return_type) = 0;
3287 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3288 Present (gnat_param);
3289 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3291 tree gnu_param_name = get_entity_name (gnat_param);
3292 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3293 tree gnu_param, gnu_field;
3296 int by_component_ptr_p = 0;
3297 int copy_in_copy_out_flag = 0;
3298 int req_by_copy = 0, req_by_ref = 0;
3300 /* See if a Mechanism was supplied that forced this
3301 parameter to be passed one way or another. */
3302 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3304 else if (Mechanism (gnat_param) == Default)
3306 else if (Mechanism (gnat_param) == By_Copy)
3308 else if (Mechanism (gnat_param) == By_Reference)
3310 else if (Mechanism (gnat_param) <= By_Descriptor)
3312 else if (Mechanism (gnat_param) > 0)
3314 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3315 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3316 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3317 Mechanism (gnat_param)))
3323 post_error ("unsupported mechanism for&", gnat_param);
3325 /* If this is either a foreign function or if the
3326 underlying type won't be passed by refererence, strip off
3327 possible padding type. */
3328 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3329 && TYPE_IS_PADDING_P (gnu_param_type)
3330 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3331 || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3332 (gnu_param_type)))))
3333 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3335 /* If this is an IN parameter it is read-only, so make a variant
3336 of the type that is read-only.
3338 ??? However, if this is an unconstrained array, that type can
3339 be very complex. So skip it for now. Likewise for any other
3340 self-referential type. */
3341 if (Ekind (gnat_param) == E_In_Parameter
3342 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3343 && ! (TYPE_SIZE (gnu_param_type) != 0
3344 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type))))
3346 = build_qualified_type (gnu_param_type,
3347 (TYPE_QUALS (gnu_param_type)
3348 | TYPE_QUAL_CONST));
3350 /* For foreign conventions, pass arrays as a pointer to the
3351 underlying type. First check for unconstrained array and get
3352 the underlying array. Then get the component type and build
3354 if (Has_Foreign_Convention (gnat_entity)
3355 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3357 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3358 (TREE_TYPE (gnu_param_type))));
3362 = build_pointer_type
3363 (build_vms_descriptor (gnu_param_type,
3364 Mechanism (gnat_param),
3367 else if (Has_Foreign_Convention (gnat_entity)
3369 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3371 /* Strip off any multi-dimensional entries, then strip
3372 off the last array to get the component type. */
3373 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3374 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3375 gnu_param_type = TREE_TYPE (gnu_param_type);
3377 by_component_ptr_p = 1;
3378 gnu_param_type = TREE_TYPE (gnu_param_type);
3380 if (Ekind (gnat_param) == E_In_Parameter)
3382 = build_qualified_type (gnu_param_type,
3383 (TYPE_QUALS (gnu_param_type)
3384 | TYPE_QUAL_CONST));
3386 gnu_param_type = build_pointer_type (gnu_param_type);
3389 /* Fat pointers are passed as thin pointers for foreign
3391 else if (Has_Foreign_Convention (gnat_entity)
3392 && TYPE_FAT_POINTER_P (gnu_param_type))
3394 = make_type_from_size (gnu_param_type,
3395 size_int (POINTER_SIZE), 0);
3397 /* If we must pass or were requested to pass by reference, do so.
3398 If we were requested to pass by copy, do so.
3399 Otherwise, for foreign conventions, pass all in out parameters
3400 or aggregates by reference. For COBOL and Fortran, pass
3401 all integer and FP types that way too. For Convention Ada,
3402 use the standard Ada default. */
3403 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3405 && ((Has_Foreign_Convention (gnat_entity)
3406 && (Ekind (gnat_param) != E_In_Parameter
3407 || AGGREGATE_TYPE_P (gnu_param_type)))
3408 || (((Convention (gnat_entity)
3409 == Convention_Fortran)
3410 || (Convention (gnat_entity)
3411 == Convention_COBOL))
3412 && (INTEGRAL_TYPE_P (gnu_param_type)
3413 || FLOAT_TYPE_P (gnu_param_type)))
3414 /* For convention Ada, see if we pass by reference
3416 || (! Has_Foreign_Convention (gnat_entity)
3417 && default_pass_by_ref (gnu_param_type)))))
3419 gnu_param_type = build_reference_type (gnu_param_type);
3423 else if (Ekind (gnat_param) != E_In_Parameter)
3424 copy_in_copy_out_flag = 1;
3426 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3427 post_error ("?cannot pass & by copy", gnat_param);
3429 /* If this is an OUT parameter that isn't passed by reference
3430 and isn't a pointer or aggregate, we don't make a PARM_DECL
3431 for it. Instead, it will be a VAR_DECL created when we process
3432 the procedure. For the special parameter of Valued_Procedure,
3435 An exception is made to cover the RM-6.4.1 rule requiring "by
3436 copy" out parameters with discriminants or implicit initial
3437 values to be handled like in out parameters. These type are
3438 normally built as aggregates, and hence passed by reference,
3439 except for some packed arrays which end up encoded in special
3442 The exception we need to make is then for packed arrays of
3443 records with discriminants or implicit initial values. We have
3444 no light/easy way to check for the latter case, so we merely
3445 check for packed arrays of records. This may lead to useless
3446 copy-in operations, but in very rare cases only, as these would
3447 be exceptions in a set of already exceptional situations. */
3448 if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3449 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3451 && ! POINTER_TYPE_P (gnu_param_type)
3452 && ! AGGREGATE_TYPE_P (gnu_param_type)))
3453 && ! (Is_Array_Type (Etype (gnat_param))
3454 && Is_Packed (Etype (gnat_param))
3455 && Is_Composite_Type (Component_Type
3456 (Etype (gnat_param)))))
3460 set_lineno (gnat_param, 0);
3463 (gnu_param_name, gnu_param_type,
3464 by_ref_p || by_component_ptr_p
3465 || Ekind (gnat_param) == E_In_Parameter);
3467 DECL_BY_REF_P (gnu_param) = by_ref_p;
3468 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3469 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3470 DECL_POINTS_TO_READONLY_P (gnu_param)
3471 = (Ekind (gnat_param) == E_In_Parameter
3472 && (by_ref_p || by_component_ptr_p));
3473 save_gnu_tree (gnat_param, gnu_param, 0);
3474 gnu_param_list = chainon (gnu_param, gnu_param_list);
3476 /* If a parameter is a pointer, this function may modify
3477 memory through it and thus shouldn't be considered
3478 a pure function. Also, the memory may be modified
3479 between two calls, so they can't be CSE'ed. The latter
3480 case also handles by-ref parameters. */
3481 if (POINTER_TYPE_P (gnu_param_type)
3482 || TYPE_FAT_POINTER_P (gnu_param_type))
3486 if (copy_in_copy_out_flag)
3488 if (! has_copy_in_out)
3490 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3493 gnu_return_type = make_node (RECORD_TYPE);
3494 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3495 has_copy_in_out = 1;
3498 set_lineno (gnat_param, 0);
3499 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3500 gnu_return_type, 0, 0, 0, 0);
3501 TREE_CHAIN (gnu_field) = gnu_field_list;
3502 gnu_field_list = gnu_field;
3503 gnu_return_list = tree_cons (gnu_field, gnu_param,
3508 /* Do not compute record for out parameters if subprogram is
3509 stubbed since structures are incomplete for the back-end. */
3510 if (gnu_field_list != 0
3511 && Convention (gnat_entity) != Convention_Stubbed)
3512 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3515 /* If we have a CICO list but it has only one entry, we convert
3516 this function into a function that simply returns that one
3518 if (list_length (gnu_return_list) == 1)
3519 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3522 if (Convention (gnat_entity) == Convention_Stdcall)
3525 = (struct attrib *) xmalloc (sizeof (struct attrib));
3527 attr->next = attr_list;
3528 attr->type = ATTR_MACHINE_ATTRIBUTE;
3529 attr->name = get_identifier ("stdcall");
3530 attr->arg = NULL_TREE;
3531 attr->error_point = gnat_entity;
3536 /* Both lists ware built in reverse. */
3537 gnu_param_list = nreverse (gnu_param_list);
3538 gnu_return_list = nreverse (gnu_return_list);
3541 = create_subprog_type (gnu_return_type, gnu_param_list,
3542 gnu_return_list, returns_unconstrained,
3544 Function_Returns_With_DSP (gnat_entity));
3546 /* ??? For now, don't consider nested functions pure. */
3547 if (! global_bindings_p ())
3550 /* A subprogram (something that doesn't return anything) shouldn't
3551 be considered Pure since there would be no reason for such a
3552 subprogram. Note that procedures with Out (or In Out) parameters
3553 have already been converted into a function with a return type. */
3554 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3558 = build_qualified_type (gnu_type,
3559 (TYPE_QUALS (gnu_type)
3560 | (TYPE_QUAL_CONST * pure_flag)
3561 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3563 set_lineno (gnat_entity, 0);
3565 /* If there was no specified Interface_Name and the external and
3566 internal names of the subprogram are the same, only use the
3567 internal name to allow disambiguation of nested subprograms. */
3568 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3571 /* If we are defining the subprogram and it has an Address clause
3572 we must get the address expression from the saved GCC tree for the
3573 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3574 the address expression here since the front-end has guaranteed
3575 in that case that the elaboration has no effects. If there is
3576 an Address clause and we are not defining the object, just
3577 make it a constant. */
3578 if (Present (Address_Clause (gnat_entity)))
3580 tree gnu_address = 0;
3584 = (present_gnu_tree (gnat_entity)
3585 ? get_gnu_tree (gnat_entity)
3586 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3588 save_gnu_tree (gnat_entity, NULL_TREE, 0);
3590 gnu_type = build_reference_type (gnu_type);
3591 if (gnu_address != 0)
3592 gnu_address = convert (gnu_type, gnu_address);
3595 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3596 gnu_address, 0, Is_Public (gnat_entity),
3598 DECL_BY_REF_P (gnu_decl) = 1;
3601 else if (kind == E_Subprogram_Type)
3602 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3603 ! Comes_From_Source (gnat_entity),
3607 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3608 gnu_type, gnu_param_list,
3609 inline_flag, public_flag,
3610 extern_flag, attr_list);
3611 DECL_STUBBED_P (gnu_decl)
3612 = Convention (gnat_entity) == Convention_Stubbed;
3617 case E_Incomplete_Type:
3618 case E_Private_Type:
3619 case E_Limited_Private_Type:
3620 case E_Record_Type_With_Private:
3621 case E_Private_Subtype:
3622 case E_Limited_Private_Subtype:
3623 case E_Record_Subtype_With_Private:
3625 /* If this type does not have a full view in the unit we are
3626 compiling, then just get the type from its Etype. */
3627 if (No (Full_View (gnat_entity)))
3629 /* If this is an incomplete type with no full view, it must
3630 be a Taft Amendement type, so just return a dummy type. */
3631 if (kind == E_Incomplete_Type)
3632 gnu_type = make_dummy_type (gnat_entity);
3634 else if (Present (Underlying_Full_View (gnat_entity)))
3635 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3639 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3647 /* Otherwise, if we are not defining the type now, get the
3648 type from the full view. But always get the type from the full
3649 view for define on use types, since otherwise we won't see them! */
3651 else if (! definition
3652 || (Is_Itype (Full_View (gnat_entity))
3653 && No (Freeze_Node (gnat_entity)))
3654 || (Is_Itype (gnat_entity)
3655 && No (Freeze_Node (Full_View (gnat_entity)))))
3657 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3663 /* For incomplete types, make a dummy type entry which will be
3665 gnu_type = make_dummy_type (gnat_entity);
3667 /* Save this type as the full declaration's type so we can do any needed
3668 updates when we see it. */
3669 set_lineno (gnat_entity, 0);
3670 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3671 ! Comes_From_Source (gnat_entity),
3673 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3676 /* Simple class_wide types are always viewed as their root_type
3677 by Gigi unless an Equivalent_Type is specified. */
3678 case E_Class_Wide_Type:
3679 if (Present (Equivalent_Type (gnat_entity)))
3680 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3682 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3688 case E_Task_Subtype:
3689 case E_Protected_Type:
3690 case E_Protected_Subtype:
3691 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3692 gnu_type = void_type_node;
3694 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3700 gnu_decl = create_label_decl (gnu_entity_id);
3705 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3706 we've already saved it, so we don't try to. */
3707 gnu_decl = error_mark_node;
3715 /* If we had a case where we evaluated another type and it might have
3716 defined this one, handle it here. */
3717 if (maybe_present && present_gnu_tree (gnat_entity))
3719 gnu_decl = get_gnu_tree (gnat_entity);
3723 /* If we are processing a type and there is either no decl for it or
3724 we just made one, do some common processing for the type, such as
3725 handling alignment and possible padding. */
3727 if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3729 if (Is_Tagged_Type (gnat_entity)
3730 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3731 TYPE_ALIGN_OK (gnu_type) = 1;
3733 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3734 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3736 /* ??? Don't set the size for a String_Literal since it is either
3737 confirming or we don't handle it properly (if the low bound is
3739 if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3740 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3741 TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3743 /* If a size was specified, see if we can make a new type of that size
3744 by rearranging the type, for example from a fat to a thin pointer. */
3748 = make_type_from_size (gnu_type, gnu_size,
3749 Has_Biased_Representation (gnat_entity));
3751 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3752 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3756 /* If the alignment hasn't already been processed and this is
3757 not an unconstrained array, see if an alignment is specified.
3758 If not, we pick a default alignment for atomic objects. */
3759 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3761 else if (Known_Alignment (gnat_entity))
3762 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3763 TYPE_ALIGN (gnu_type));
3764 else if (Is_Atomic (gnat_entity) && gnu_size == 0
3765 && host_integerp (TYPE_SIZE (gnu_type), 1)
3766 && integer_pow2p (TYPE_SIZE (gnu_type)))
3767 align = MIN (BIGGEST_ALIGNMENT,
3768 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3769 else if (Is_Atomic (gnat_entity) && gnu_size != 0
3770 && host_integerp (gnu_size, 1)
3771 && integer_pow2p (gnu_size))
3772 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3774 /* See if we need to pad the type. If we did, and made a record,
3775 the name of the new type may be changed. So get it back for
3776 us when we make the new TYPE_DECL below. */
3777 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3778 gnat_entity, "PAD", 1, definition, 0);
3779 if (TREE_CODE (gnu_type) == RECORD_TYPE
3780 && TYPE_IS_PADDING_P (gnu_type))
3782 gnu_entity_id = TYPE_NAME (gnu_type);
3783 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3784 gnu_entity_id = DECL_NAME (gnu_entity_id);
3787 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3789 /* If we are at global level, GCC will have applied variable_size to
3790 the type, but that won't have done anything. So, if it's not
3791 a constant or self-referential, call elaborate_expression_1 to
3792 make a variable for the size rather than calculating it each time.
3793 Handle both the RM size and the actual size. */
3794 if (global_bindings_p ()
3795 && TYPE_SIZE (gnu_type) != 0
3796 && ! TREE_CONSTANT (TYPE_SIZE (gnu_type))
3797 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3799 if (TREE_CODE (gnu_type) == RECORD_TYPE
3800 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3801 TYPE_SIZE (gnu_type), 0))
3803 TYPE_SIZE (gnu_type)
3804 = elaborate_expression_1 (gnat_entity, gnat_entity,
3805 TYPE_SIZE (gnu_type),
3806 get_identifier ("SIZE"),
3808 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3812 TYPE_SIZE (gnu_type)
3813 = elaborate_expression_1 (gnat_entity, gnat_entity,
3814 TYPE_SIZE (gnu_type),
3815 get_identifier ("SIZE"),
3818 /* ??? For now, store the size as a multiple of the alignment
3819 in bytes so that we can see the alignment from the tree. */
3820 TYPE_SIZE_UNIT (gnu_type)
3822 (MULT_EXPR, sizetype,
3823 elaborate_expression_1
3824 (gnat_entity, gnat_entity,
3825 build_binary_op (EXACT_DIV_EXPR, sizetype,
3826 TYPE_SIZE_UNIT (gnu_type),
3827 size_int (TYPE_ALIGN (gnu_type)
3829 get_identifier ("SIZE_A_UNIT"),
3831 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3833 if (TREE_CODE (gnu_type) == RECORD_TYPE)
3834 SET_TYPE_ADA_SIZE (gnu_type,
3835 elaborate_expression_1 (gnat_entity, gnat_entity,
3836 TYPE_ADA_SIZE (gnu_type),
3837 get_identifier ("RM_SIZE"),
3842 /* If this is a record type or subtype, call elaborate_expression_1 on
3843 any field position. Do this for both global and local types.
3844 Skip any fields that we haven't made trees for to avoid problems with
3845 class wide types. */
3846 if (IN (kind, Record_Kind))
3847 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3848 gnat_temp = Next_Entity (gnat_temp))
3849 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3851 tree gnu_field = get_gnu_tree (gnat_temp);
3853 /* ??? Unfortunately, GCC needs to be able to prove the
3854 alignment of this offset and if it's a variable, it can't.
3855 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
3856 right now, we have to put in an explicit multiply and
3857 divide by that value. */
3858 if (! CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
3859 DECL_FIELD_OFFSET (gnu_field)
3861 (MULT_EXPR, sizetype,
3862 elaborate_expression_1
3863 (gnat_temp, gnat_temp,
3864 build_binary_op (EXACT_DIV_EXPR, sizetype,
3865 DECL_FIELD_OFFSET (gnu_field),
3866 size_int (DECL_OFFSET_ALIGN (gnu_field)
3868 get_identifier ("OFFSET"),
3870 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
3873 gnu_type = build_qualified_type (gnu_type,
3874 (TYPE_QUALS (gnu_type)
3875 | (TYPE_QUAL_VOLATILE
3876 * Treat_As_Volatile (gnat_entity))));
3878 if (Is_Atomic (gnat_entity))
3879 check_ok_for_atomic (gnu_type, gnat_entity, 0);
3881 if (Known_Alignment (gnat_entity))
3882 TYPE_USER_ALIGN (gnu_type) = 1;
3886 set_lineno (gnat_entity, 0);
3887 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3888 ! Comes_From_Source (gnat_entity),
3892 TREE_TYPE (gnu_decl) = gnu_type;
3895 if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3897 gnu_type = TREE_TYPE (gnu_decl);
3899 /* Back-annotate the Alignment of the type if not already in the
3900 tree. Likewise for sizes. */
3901 if (Unknown_Alignment (gnat_entity))
3902 Set_Alignment (gnat_entity,
3903 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3905 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3907 /* If the size is self-referential, we annotate the maximum
3908 value of that size. */
3909 tree gnu_size = TYPE_SIZE (gnu_type);
3911 if (CONTAINS_PLACEHOLDER_P (gnu_size))
3912 gnu_size = max_size (gnu_size, 1);
3914 Set_Esize (gnat_entity, annotate_value (gnu_size));
3916 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
3918 /* In this mode the tag and the parent components are not
3919 generated by the front-end, so the sizes must be adjusted
3925 if (Is_Derived_Type (gnat_entity))
3928 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
3929 Set_Alignment (gnat_entity,
3930 Alignment (Etype (Base_Type (gnat_entity))));
3933 size_offset = POINTER_SIZE;
3935 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
3936 Set_Esize (gnat_entity,
3937 UI_From_Int (((new_size + (POINTER_SIZE - 1))
3938 / POINTER_SIZE) * POINTER_SIZE));
3939 Set_RM_Size (gnat_entity, Esize (gnat_entity));
3943 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3944 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3947 if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3948 DECL_ARTIFICIAL (gnu_decl) = 1;
3950 if (! debug_info_p && DECL_P (gnu_decl)
3951 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3952 DECL_IGNORED_P (gnu_decl) = 1;
3954 /* If this decl is really indirect, adjust it. */
3955 if (TREE_CODE (gnu_decl) == VAR_DECL)
3956 adjust_decl_rtl (gnu_decl);
3958 /* If we haven't already, associate the ..._DECL node that we just made with
3959 the input GNAT entity node. */
3961 save_gnu_tree (gnat_entity, gnu_decl, 0);
3963 /* If this is an enumeral or floating-point type, we were not able to set
3964 the bounds since they refer to the type. These bounds are always static.
3966 For enumeration types, also write debugging information and declare the
3967 enumeration literal table, if needed. */
3969 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3970 || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3972 tree gnu_scalar_type = gnu_type;
3974 /* If this is a padded type, we need to use the underlying type. */
3975 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3976 && TYPE_IS_PADDING_P (gnu_scalar_type))
3977 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3979 /* If this is a floating point type and we haven't set a floating
3980 point type yet, use this in the evaluation of the bounds. */
3981 if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3982 longest_float_type_node = gnu_type;
3984 TYPE_MIN_VALUE (gnu_scalar_type)
3985 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3986 TYPE_MAX_VALUE (gnu_scalar_type)
3987 = gnat_to_gnu (Type_High_Bound (gnat_entity));
3989 if (kind == E_Enumeration_Type)
3991 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3993 /* Since this has both a typedef and a tag, avoid outputting
3995 DECL_ARTIFICIAL (gnu_decl) = 1;
3996 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4000 /* If we deferred processing of incomplete types, re-enable it. If there
4001 were no other disables and we have some to process, do so. */
4002 if (this_deferred && --defer_incomplete_level == 0
4003 && defer_incomplete_list != 0)
4005 struct incomplete *incp = defer_incomplete_list;
4006 struct incomplete *next;
4008 defer_incomplete_list = 0;
4009 for (; incp; incp = next)
4013 if (incp->old_type != 0)
4014 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4015 gnat_to_gnu_type (incp->full_type));
4020 /* If we are not defining this type, see if it's in the incomplete list.
4021 If so, handle that list entry now. */
4022 else if (! definition)
4024 struct incomplete *incp;
4026 for (incp = defer_incomplete_list; incp; incp = incp->next)
4027 if (incp->old_type != 0 && incp->full_type == gnat_entity)
4029 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4030 TREE_TYPE (gnu_decl));
4038 if (Is_Packed_Array_Type (gnat_entity)
4039 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4040 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4041 && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4042 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4047 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4048 be elaborated at the point of its definition, but do nothing else. */
4051 elaborate_entity (Entity_Id gnat_entity)
4053 switch (Ekind (gnat_entity))
4055 case E_Signed_Integer_Subtype:
4056 case E_Modular_Integer_Subtype:
4057 case E_Enumeration_Subtype:
4058 case E_Ordinary_Fixed_Point_Subtype:
4059 case E_Decimal_Fixed_Point_Subtype:
4060 case E_Floating_Point_Subtype:
4062 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4063 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4065 /* ??? Tests for avoiding static constaint error expression
4066 is needed until the front stops generating bogus conversions
4067 on bounds of real types. */
4069 if (! Raises_Constraint_Error (gnat_lb))
4070 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4071 1, 0, Needs_Debug_Info (gnat_entity));
4072 if (! Raises_Constraint_Error (gnat_hb))
4073 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4074 1, 0, Needs_Debug_Info (gnat_entity));
4080 Node_Id full_definition = Declaration_Node (gnat_entity);
4081 Node_Id record_definition = Type_Definition (full_definition);
4083 /* If this is a record extension, go a level further to find the
4084 record definition. */
4085 if (Nkind (record_definition) == N_Derived_Type_Definition)
4086 record_definition = Record_Extension_Part (record_definition);
4090 case E_Record_Subtype:
4091 case E_Private_Subtype:
4092 case E_Limited_Private_Subtype:
4093 case E_Record_Subtype_With_Private:
4094 if (Is_Constrained (gnat_entity)
4095 && Has_Discriminants (Base_Type (gnat_entity))
4096 && Present (Discriminant_Constraint (gnat_entity)))
4098 Node_Id gnat_discriminant_expr;
4099 Entity_Id gnat_field;
4101 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4102 gnat_discriminant_expr
4103 = First_Elmt (Discriminant_Constraint (gnat_entity));
4104 Present (gnat_field);
4105 gnat_field = Next_Discriminant (gnat_field),
4106 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4107 /* ??? For now, ignore access discriminants. */
4108 if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4109 elaborate_expression (Node (gnat_discriminant_expr),
4111 get_entity_name (gnat_field), 1, 0, 0);
4118 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4119 any entities on its entity chain similarly. */
4122 mark_out_of_scope (Entity_Id gnat_entity)
4124 Entity_Id gnat_sub_entity;
4125 unsigned int kind = Ekind (gnat_entity);
4127 /* If this has an entity list, process all in the list. */
4128 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4129 || IN (kind, Private_Kind)
4130 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4131 || kind == E_Function || kind == E_Generic_Function
4132 || kind == E_Generic_Package || kind == E_Generic_Procedure
4133 || kind == E_Loop || kind == E_Operator || kind == E_Package
4134 || kind == E_Package_Body || kind == E_Procedure
4135 || kind == E_Record_Type || kind == E_Record_Subtype
4136 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4137 for (gnat_sub_entity = First_Entity (gnat_entity);
4138 Present (gnat_sub_entity);
4139 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4140 if (Scope (gnat_sub_entity) == gnat_entity
4141 && gnat_sub_entity != gnat_entity)
4142 mark_out_of_scope (gnat_sub_entity);
4144 /* Now clear this if it has been defined, but only do so if it isn't
4145 a subprogram or parameter. We could refine this, but it isn't
4146 worth it. If this is statically allocated, it is supposed to
4147 hang around out of cope. */
4148 if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
4149 && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
4151 save_gnu_tree (gnat_entity, NULL_TREE, 1);
4152 save_gnu_tree (gnat_entity, error_mark_node, 1);
4156 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4157 is a multi-dimensional array type, do this recursively. */
4160 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4162 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4163 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4164 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4166 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4167 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4168 so we need to go down to what does. */
4169 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4171 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4173 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4176 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4177 record_component_aliases (gnu_new_type);
4180 /* Return a TREE_LIST describing the substitutions needed to reflect
4181 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4182 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4183 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
4184 gives the tree for the discriminant and TREE_VALUES is the replacement
4185 value. They are in the form of operands to substitute_in_expr.
4186 DEFINITION is as in gnat_to_gnu_entity. */
4189 substitution_list (Entity_Id gnat_subtype,
4190 Entity_Id gnat_type,
4194 Entity_Id gnat_discrim;
4198 gnat_type = Implementation_Base_Type (gnat_subtype);
4200 if (Has_Discriminants (gnat_type))
4201 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4202 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4203 Present (gnat_discrim);
4204 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4205 gnat_value = Next_Elmt (gnat_value))
4206 /* Ignore access discriminants. */
4207 if (! Is_Access_Type (Etype (Node (gnat_value))))
4208 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4209 elaborate_expression
4210 (Node (gnat_value), gnat_subtype,
4211 get_entity_name (gnat_discrim), definition,
4218 /* For the following two functions: for each GNAT entity, the GCC
4219 tree node used as a dummy for that entity, if any. */
4221 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4223 /* Initialize the above table. */
4226 init_dummy_type (void)
4230 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4232 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4233 dummy_node_table[gnat_node] = NULL_TREE;
4235 dummy_node_table -= First_Node_Id;
4238 /* Make a dummy type corresponding to GNAT_TYPE. */
4241 make_dummy_type (Entity_Id gnat_type)
4243 Entity_Id gnat_underlying;
4246 /* Find a full type for GNAT_TYPE, taking into account any class wide
4248 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4249 gnat_type = Equivalent_Type (gnat_type);
4250 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4251 gnat_type = Root_Type (gnat_type);
4253 for (gnat_underlying = gnat_type;
4254 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4255 && Present (Full_View (gnat_underlying)));
4256 gnat_underlying = Full_View (gnat_underlying))
4259 /* If it there already a dummy type, use that one. Else make one. */
4260 if (dummy_node_table[gnat_underlying])
4261 return dummy_node_table[gnat_underlying];
4263 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4265 if (Is_Record_Type (gnat_underlying))
4266 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4267 ? UNION_TYPE : RECORD_TYPE);
4269 gnu_type = make_node (ENUMERAL_TYPE);
4271 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4272 if (AGGREGATE_TYPE_P (gnu_type))
4273 TYPE_STUB_DECL (gnu_type)
4274 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4276 TYPE_DUMMY_P (gnu_type) = 1;
4277 dummy_node_table[gnat_underlying] = gnu_type;
4282 /* Return 1 if the size represented by GNU_SIZE can be handled by an
4283 allocation. If STATIC_P is non-zero, consider only what can be
4284 done with a static allocation. */
4287 allocatable_size_p (tree gnu_size, int static_p)
4289 HOST_WIDE_INT our_size;
4291 /* If this is not a static allocation, the only case we want to forbid
4292 is an overflowing size. That will be converted into a raise a
4295 return ! (TREE_CODE (gnu_size) == INTEGER_CST
4296 && TREE_CONSTANT_OVERFLOW (gnu_size));
4298 /* Otherwise, we need to deal with both variable sizes and constant
4299 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4300 since assemblers may not like very large sizes. */
4301 if (!host_integerp (gnu_size, 1))
4304 our_size = tree_low_cst (gnu_size, 1);
4305 return (int) our_size == our_size;
4308 /* Return a list of attributes for GNAT_ENTITY, if any. */
4310 static struct attrib *
4311 build_attr_list (Entity_Id gnat_entity)
4313 struct attrib *attr_list = 0;
4316 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4317 gnat_temp = Next_Rep_Item (gnat_temp))
4318 if (Nkind (gnat_temp) == N_Pragma)
4320 struct attrib *attr;
4321 tree gnu_arg0 = 0, gnu_arg1 = 0;
4322 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4323 enum attr_type etype;
4325 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4326 && Present (Next (First (gnat_assoc)))
4327 && (Nkind (Expression (Next (First (gnat_assoc))))
4328 == N_String_Literal))
4330 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4333 (First (gnat_assoc))))));
4334 if (Present (Next (Next (First (gnat_assoc))))
4335 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4336 == N_String_Literal))
4337 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4341 (First (gnat_assoc)))))));
4344 switch (Get_Pragma_Id (Chars (gnat_temp)))
4346 case Pragma_Machine_Attribute:
4347 etype = ATTR_MACHINE_ATTRIBUTE;
4350 case Pragma_Linker_Alias:
4351 etype = ATTR_LINK_ALIAS;
4354 case Pragma_Linker_Section:
4355 etype = ATTR_LINK_SECTION;
4358 case Pragma_Weak_External:
4359 etype = ATTR_WEAK_EXTERNAL;
4366 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4367 attr->next = attr_list;
4369 attr->name = gnu_arg0;
4370 attr->arg = gnu_arg1;
4372 = Present (Next (First (gnat_assoc)))
4373 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4380 /* Get the unpadded version of a GNAT type. */
4383 get_unpadded_type (Entity_Id gnat_entity)
4385 tree type = gnat_to_gnu_type (gnat_entity);
4387 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4388 type = TREE_TYPE (TYPE_FIELDS (type));
4393 /* Called when we need to protect a variable object using a save_expr. */
4396 maybe_variable (tree gnu_operand, Node_Id gnat_node)
4398 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4399 || TREE_CODE (gnu_operand) == SAVE_EXPR
4400 || TREE_CODE (gnu_operand) == NULL_EXPR)
4403 /* If we will be generating code, make sure we are at the proper
4405 if (! global_bindings_p () && ! CONTAINS_PLACEHOLDER_P (gnu_operand))
4406 set_lineno (gnat_node, 1);
4408 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4409 return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4410 variable_size (TREE_OPERAND (gnu_operand, 0)));
4412 return variable_size (gnu_operand);
4415 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4416 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4417 return the GCC tree to use for that expression. GNU_NAME is the
4418 qualification to use if an external name is appropriate and DEFINITION is
4419 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4420 we need a result. Otherwise, we are just elaborating this for
4421 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4422 purposes even if it isn't needed for code generation. */
4425 elaborate_expression (Node_Id gnat_expr,
4426 Entity_Id gnat_entity,
4434 /* If we already elaborated this expression (e.g., it was involved
4435 in the definition of a private type), use the old value. */
4436 if (present_gnu_tree (gnat_expr))
4437 return get_gnu_tree (gnat_expr);
4439 /* If we don't need a value and this is static or a discriment, we
4440 don't need to do anything. */
4441 else if (! need_value
4442 && (Is_OK_Static_Expression (gnat_expr)
4443 || (Nkind (gnat_expr) == N_Identifier
4444 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4447 /* Otherwise, convert this tree to its GCC equivalant. */
4449 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4450 gnu_name, definition, need_debug);
4452 /* Save the expression in case we try to elaborate this entity again.
4453 Since this is not a DECL, don't check it. If this is a constant,
4454 don't save it since GNAT_EXPR might be used more than once. Also,
4455 don't save if it's a discriminant. */
4456 if (! CONTAINS_PLACEHOLDER_P (gnu_expr))
4457 save_gnu_tree (gnat_expr, gnu_expr, 1);
4459 return need_value ? gnu_expr : error_mark_node;
4462 /* Similar, but take a GNU expression. */
4465 elaborate_expression_1 (Node_Id gnat_expr,
4466 Entity_Id gnat_entity,
4473 /* Strip any conversions to see if the expression is a readonly variable.
4474 ??? This really should remain readonly, but we have to think about
4475 the typing of the tree here. */
4476 tree gnu_inner_expr = remove_conversions (gnu_expr, 1);
4477 int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4480 /* In most cases, we won't see a naked FIELD_DECL here because a
4481 discriminant reference will have been replaced with a COMPONENT_REF
4482 when the type is being elaborated. However, there are some cases
4483 involving child types where we will. So convert it to a COMPONENT_REF
4484 here. We have to hope it will be at the highest level of the
4485 expression in these cases. */
4486 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4487 gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4488 build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4491 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4492 that is a constant, make a variable that is initialized to contain the
4493 bound when the package containing the definition is elaborated. If
4494 this entity is defined at top level and a bound or discriminant value
4495 isn't a constant or a reference to a discriminant, replace the bound
4496 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4497 rely here on the fact that an expression cannot contain both the
4498 discriminant and some other variable. */
4500 expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4501 && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4502 && TREE_READONLY (gnu_inner_expr))
4503 && ! CONTAINS_PLACEHOLDER_P (gnu_expr));
4505 /* If this is a static expression or contains a discriminant, we don't
4506 need the variable for debugging (and can't elaborate anyway if a
4509 && (Is_OK_Static_Expression (gnat_expr)
4510 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4513 /* Now create the variable if we need it. */
4514 if (need_debug || (expr_variable && expr_global))
4516 set_lineno (gnat_entity, ! global_bindings_p ());
4518 = create_var_decl (create_concat_name (gnat_entity,
4519 IDENTIFIER_POINTER (gnu_name)),
4520 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4521 Is_Public (gnat_entity), ! definition, 0, 0);
4524 /* We only need to use this variable if we are in global context since GCC
4525 can do the right thing in the local case. */
4526 if (expr_global && expr_variable)
4528 else if (! expr_variable)
4531 return maybe_variable (gnu_expr, gnat_expr);
4534 /* Create a record type that contains a field of TYPE with a starting bit
4535 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4538 make_aligning_type (tree type, int align, tree size)
4540 tree record_type = make_node (RECORD_TYPE);
4541 tree place = build (PLACEHOLDER_EXPR, record_type);
4542 tree size_addr_place = convert (sizetype,
4543 build_unary_op (ADDR_EXPR, NULL_TREE,
4545 tree name = TYPE_NAME (type);
4548 if (TREE_CODE (name) == TYPE_DECL)
4549 name = DECL_NAME (name);
4551 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4553 /* The bit position is obtained by "and"ing the alignment minus 1
4554 with the two's complement of the address and multiplying
4555 by the number of bits per unit. Do all this in sizetype. */
4557 pos = size_binop (MULT_EXPR,
4558 convert (bitsizetype,
4559 size_binop (BIT_AND_EXPR,
4560 size_diffop (size_zero_node,
4562 ssize_int ((align / BITS_PER_UNIT)
4566 field = create_field_decl (get_identifier ("F"), type, record_type,
4568 DECL_BIT_FIELD (field) = 0;
4570 finish_record_type (record_type, field, 1, 0);
4571 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4572 TYPE_SIZE (record_type)
4573 = size_binop (PLUS_EXPR,
4574 size_binop (MULT_EXPR, convert (bitsizetype, size),
4576 bitsize_int (align));
4577 TYPE_SIZE_UNIT (record_type)
4578 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4579 copy_alias_set (record_type, type);
4583 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4584 being used as the field type of a packed record. See if we can rewrite it
4585 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4586 return the new type. If not, return the original type. */
4589 make_packable_type (tree type)
4591 tree new_type = make_node (TREE_CODE (type));
4592 tree field_list = NULL_TREE;
4595 /* Copy the name and flags from the old type to that of the new and set
4596 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4597 also copy the size. */
4598 TYPE_NAME (new_type) = TYPE_NAME (type);
4599 TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4600 = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4601 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4602 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4603 if (TREE_CODE (type) == QUAL_UNION_TYPE)
4605 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4606 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4609 TYPE_ALIGN (new_type)
4610 = ((HOST_WIDE_INT) 1
4611 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4613 /* Now copy the fields, keeping the position and size. */
4614 for (old_field = TYPE_FIELDS (type); old_field != 0;
4615 old_field = TREE_CHAIN (old_field))
4617 tree new_field_type = TREE_TYPE (old_field);
4620 if (TYPE_MODE (new_field_type) == BLKmode
4621 && (TREE_CODE (new_field_type) == RECORD_TYPE
4622 || TREE_CODE (new_field_type) == UNION_TYPE
4623 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4624 && host_integerp (TYPE_SIZE (new_field_type), 1))
4625 new_field_type = make_packable_type (new_field_type);
4627 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4628 new_type, TYPE_PACKED (type),
4629 DECL_SIZE (old_field),
4630 bit_position (old_field),
4631 ! DECL_NONADDRESSABLE_P (old_field));
4633 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4634 SET_DECL_ORIGINAL_FIELD (new_field,
4635 (DECL_ORIGINAL_FIELD (old_field) != 0
4636 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4638 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4639 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4641 TREE_CHAIN (new_field) = field_list;
4642 field_list = new_field;
4645 finish_record_type (new_type, nreverse (field_list), 1, 1);
4646 copy_alias_set (new_type, type);
4647 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4650 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4651 if needed. We have already verified that SIZE and TYPE are large enough.
4653 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4656 IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4658 DEFINITION is nonzero if this type is being defined.
4660 SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4661 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4665 maybe_pad_type (tree type,
4668 Entity_Id gnat_entity,
4669 const char *name_trailer,
4674 tree orig_size = TYPE_SIZE (type);
4678 /* If TYPE is a padded type, see if it agrees with any size and alignment
4679 we were given. If so, return the original type. Otherwise, strip
4680 off the padding, since we will either be returning the inner type
4681 or repadding it. If no size or alignment is specified, use that of
4682 the original padded type. */
4684 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4687 || operand_equal_p (round_up (size,
4688 MAX (align, TYPE_ALIGN (type))),
4689 round_up (TYPE_SIZE (type),
4690 MAX (align, TYPE_ALIGN (type))),
4692 && (align == 0 || align == TYPE_ALIGN (type)))
4696 size = TYPE_SIZE (type);
4698 align = TYPE_ALIGN (type);
4700 type = TREE_TYPE (TYPE_FIELDS (type));
4701 orig_size = TYPE_SIZE (type);
4704 /* If the size is either not being changed or is being made smaller (which
4705 is not done here (and is only valid for bitfields anyway), show the size
4706 isn't changing. Likewise, clear the alignment if it isn't being
4707 changed. Then return if we aren't doing anything. */
4710 && (operand_equal_p (size, orig_size, 0)
4711 || (TREE_CODE (orig_size) == INTEGER_CST
4712 && tree_int_cst_lt (size, orig_size))))
4715 if (align == TYPE_ALIGN (type))
4718 if (align == 0 && size == 0)
4721 /* We used to modify the record in place in some cases, but that could
4722 generate incorrect debugging information. So make a new record
4724 record = make_node (RECORD_TYPE);
4726 if (Present (gnat_entity))
4727 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4729 /* If we were making a type, complete the original type and give it a
4732 create_type_decl (get_entity_name (gnat_entity), type,
4733 0, ! Comes_From_Source (gnat_entity),
4734 ! (TYPE_NAME (type) != 0
4735 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4736 && DECL_IGNORED_P (TYPE_NAME (type))));
4738 /* If we are changing the alignment and the input type is a record with
4739 BLKmode and a small constant size, try to make a form that has an
4740 integral mode. That might allow this record to have an integral mode,
4741 which will be much more efficient. There is no point in doing this if a
4742 size is specified unless it is also smaller than the biggest alignment
4743 and it is incorrect to do this if the size of the original type is not a
4744 multiple of the alignment. */
4746 && TREE_CODE (type) == RECORD_TYPE
4747 && TYPE_MODE (type) == BLKmode
4748 && host_integerp (orig_size, 1)
4749 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4751 || (TREE_CODE (size) == INTEGER_CST
4752 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4753 && tree_low_cst (orig_size, 1) % align == 0)
4754 type = make_packable_type (type);
4756 field = create_field_decl (get_identifier ("F"), type, record, 0,
4757 NULL_TREE, bitsize_zero_node, 1);
4759 DECL_INTERNAL_P (field) = 1;
4760 TYPE_SIZE (record) = size != 0 ? size : orig_size;
4761 TYPE_SIZE_UNIT (record)
4762 = convert (sizetype,
4763 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4764 bitsize_unit_node));
4765 TYPE_ALIGN (record) = align;
4766 TYPE_IS_PADDING_P (record) = 1;
4767 TYPE_VOLATILE (record)
4768 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
4769 finish_record_type (record, field, 1, 0);
4771 /* Keep the RM_Size of the padded record as that of the old record
4773 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
4775 /* Unless debugging information isn't being written for the input type,
4776 write a record that shows what we are a subtype of and also make a
4777 variable that indicates our size, if variable. */
4778 if (TYPE_NAME (record) != 0
4779 && AGGREGATE_TYPE_P (type)
4780 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4781 || ! DECL_IGNORED_P (TYPE_NAME (type))))
4783 tree marker = make_node (RECORD_TYPE);
4784 tree name = DECL_NAME (TYPE_NAME (record));
4785 tree orig_name = TYPE_NAME (type);
4787 if (TREE_CODE (orig_name) == TYPE_DECL)
4788 orig_name = DECL_NAME (orig_name);
4790 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4791 finish_record_type (marker,
4792 create_field_decl (orig_name, integer_type_node,
4793 marker, 0, NULL_TREE, NULL_TREE,
4797 if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4798 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4799 sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4805 if (CONTAINS_PLACEHOLDER_P (orig_size))
4806 orig_size = max_size (orig_size, 1);
4808 /* If the size was widened explicitly, maybe give a warning. */
4809 if (size != 0 && Present (gnat_entity)
4810 && ! operand_equal_p (size, orig_size, 0)
4811 && ! (TREE_CODE (size) == INTEGER_CST
4812 && TREE_CODE (orig_size) == INTEGER_CST
4813 && tree_int_cst_lt (size, orig_size)))
4815 Node_Id gnat_error_node = Empty;
4817 if (Is_Packed_Array_Type (gnat_entity))
4818 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4820 if ((Ekind (gnat_entity) == E_Component
4821 || Ekind (gnat_entity) == E_Discriminant)
4822 && Present (Component_Clause (gnat_entity)))
4823 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4824 else if (Present (Size_Clause (gnat_entity)))
4825 gnat_error_node = Expression (Size_Clause (gnat_entity));
4827 /* Generate message only for entities that come from source, since
4828 if we have an entity created by expansion, the message will be
4829 generated for some other corresponding source entity. */
4830 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4831 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4833 size_diffop (size, orig_size));
4835 else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4836 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4837 gnat_entity, gnat_entity,
4838 size_diffop (size, orig_size));
4844 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4845 the value passed against the list of choices. */
4848 choices_to_gnu (tree operand, Node_Id choices)
4852 tree result = integer_zero_node;
4853 tree this_test, low = 0, high = 0, single = 0;
4855 for (choice = First (choices); Present (choice); choice = Next (choice))
4857 switch (Nkind (choice))
4860 low = gnat_to_gnu (Low_Bound (choice));
4861 high = gnat_to_gnu (High_Bound (choice));
4863 /* There's no good type to use here, so we might as well use
4864 integer_type_node. */
4866 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4867 build_binary_op (GE_EXPR, integer_type_node,
4869 build_binary_op (LE_EXPR, integer_type_node,
4874 case N_Subtype_Indication:
4875 gnat_temp = Range_Expression (Constraint (choice));
4876 low = gnat_to_gnu (Low_Bound (gnat_temp));
4877 high = gnat_to_gnu (High_Bound (gnat_temp));
4880 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4881 build_binary_op (GE_EXPR, integer_type_node,
4883 build_binary_op (LE_EXPR, integer_type_node,
4888 case N_Expanded_Name:
4889 /* This represents either a subtype range, an enumeration
4890 literal, or a constant Ekind says which. If an enumeration
4891 literal or constant, fall through to the next case. */
4892 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4893 && Ekind (Entity (choice)) != E_Constant)
4895 tree type = gnat_to_gnu_type (Entity (choice));
4897 low = TYPE_MIN_VALUE (type);
4898 high = TYPE_MAX_VALUE (type);
4901 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4902 build_binary_op (GE_EXPR, integer_type_node,
4904 build_binary_op (LE_EXPR, integer_type_node,
4908 /* ... fall through ... */
4909 case N_Character_Literal:
4910 case N_Integer_Literal:
4911 single = gnat_to_gnu (choice);
4912 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4916 case N_Others_Choice:
4917 this_test = integer_one_node;
4924 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4931 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4932 placed in GNU_RECORD_TYPE.
4934 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4935 record has a Component_Alignment of Storage_Unit.
4937 DEFINITION is nonzero if this field is for a record being defined. */
4940 gnat_to_gnu_field (Entity_Id gnat_field,
4941 tree gnu_record_type,
4945 tree gnu_field_id = get_entity_name (gnat_field);
4946 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4947 tree gnu_orig_field_type = gnu_field_type;
4951 int needs_strict_alignment
4952 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
4953 || Treat_As_Volatile (gnat_field));
4955 /* If this field requires strict alignment or contains an item of
4956 variable sized, pretend it isn't packed. */
4957 if (needs_strict_alignment || is_variable_size (gnu_field_type))
4960 /* For packed records, this is one of the few occasions on which we use
4961 the official RM size for discrete or fixed-point components, instead
4962 of the normal GNAT size stored in Esize. See description in Einfo:
4963 "Handling of Type'Size Values" for further details. */
4966 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4967 gnat_field, FIELD_DECL, 0, 1);
4969 if (Known_Static_Esize (gnat_field))
4970 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4971 gnat_field, FIELD_DECL, 0, 1);
4973 /* If the field's type is left-justified modular, the wrapper can prevent
4974 packing so we make the field the type of the inner object unless the
4975 situation forbids it. We may not do that when the field is addressable_p,
4976 typically because in that case this field may later be passed by-ref for
4977 a formal argument expecting the left justification. The condition below
4978 is then matching the addressable_p code for COMPONENT_REF. */
4979 if (! Is_Aliased (gnat_field) && flag_strict_aliasing
4980 && TREE_CODE (gnu_field_type) == RECORD_TYPE
4981 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4982 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4984 /* If we are packing this record or we have a specified size that's
4985 smaller than that of the field type and the field type is also a record
4986 that's BLKmode and with a small constant size, see if we can get a
4987 better form of the type that allows more packing. If we can, show
4988 a size was specified for it if there wasn't one so we know to
4989 make this a bitfield and avoid making things wider. */
4990 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4991 && TYPE_MODE (gnu_field_type) == BLKmode
4992 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
4993 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
4995 || (gnu_size != 0 && tree_int_cst_lt (gnu_size,
4996 TYPE_SIZE (gnu_field_type)))))
4998 gnu_field_type = make_packable_type (gnu_field_type);
5000 if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
5001 gnu_size = rm_size (gnu_field_type);
5004 /* If we are packing the record and the field is BLKmode, round the
5005 size up to a byte boundary. */
5006 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size != 0)
5007 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5009 if (Present (Component_Clause (gnat_field)))
5011 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5012 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5013 gnat_field, FIELD_DECL, 0, 1);
5015 /* Ensure the position does not overlap with the parent subtype,
5017 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5020 = gnat_to_gnu_type (Parent_Subtype
5021 (Underlying_Type (Scope (gnat_field))));
5023 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5024 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5027 ("offset of& must be beyond parent{, minimum allowed is ^}",
5028 First_Bit (Component_Clause (gnat_field)), gnat_field,
5029 TYPE_SIZE_UNIT (gnu_parent));
5033 /* If this field needs strict alignment, ensure the record is
5034 sufficiently aligned and that that position and size are
5035 consistent with the alignment. */
5036 if (needs_strict_alignment)
5038 tree gnu_min_size = round_up (rm_size (gnu_field_type),
5039 TYPE_ALIGN (gnu_field_type));
5041 TYPE_ALIGN (gnu_record_type)
5042 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5044 /* If Atomic, the size must match exactly and if aliased, the size
5045 must not be less than the rounded size. */
5046 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5047 && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5050 ("atomic field& must be natural size of type{ (^)}",
5051 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5052 TYPE_SIZE (gnu_field_type));
5057 else if (Is_Aliased (gnat_field)
5059 && tree_int_cst_lt (gnu_size, gnu_min_size))
5062 ("size of aliased field& too small{, minimum required is ^}",
5063 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5068 if (! integer_zerop (size_binop
5069 (TRUNC_MOD_EXPR, gnu_pos,
5070 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5072 if (Is_Aliased (gnat_field))
5074 ("position of aliased field& must be multiple of ^ bits",
5075 First_Bit (Component_Clause (gnat_field)), gnat_field,
5076 TYPE_ALIGN (gnu_field_type));
5078 else if (Treat_As_Volatile (gnat_field))
5080 ("position of volatile field& must be multiple of ^ bits",
5081 First_Bit (Component_Clause (gnat_field)), gnat_field,
5082 TYPE_ALIGN (gnu_field_type));
5084 else if (Strict_Alignment (Etype (gnat_field)))
5086 ("position of & with aliased or tagged components not multiple of ^ bits",
5087 First_Bit (Component_Clause (gnat_field)), gnat_field,
5088 TYPE_ALIGN (gnu_field_type));
5095 /* If an error set the size to zero, show we have no position
5101 if (Is_Atomic (gnat_field))
5102 check_ok_for_atomic (gnu_field_type, gnat_field, 0);
5105 /* If the record has rep clauses and this is the tag field, make a rep
5106 clause for it as well. */
5107 else if (Has_Specified_Layout (Scope (gnat_field))
5108 && Chars (gnat_field) == Name_uTag)
5110 gnu_pos = bitsize_zero_node;
5111 gnu_size = TYPE_SIZE (gnu_field_type);
5114 /* We need to make the size the maximum for the type if it is
5115 self-referential and an unconstrained type. In that case, we can't
5116 pack the field since we can't make a copy to align it. */
5117 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5119 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5120 && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
5122 gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
5126 /* If no size is specified (or if there was an error), don't specify a
5132 /* Unless this field is aliased, we can remove any left-justified
5133 modular type since it's only needed in the unchecked conversion
5134 case, which doesn't apply here. */
5135 if (! needs_strict_alignment
5136 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5137 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
5138 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5141 = make_type_from_size (gnu_field_type, gnu_size,
5142 Has_Biased_Representation (gnat_field));
5143 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
5144 gnat_field, "PAD", 0, definition, 1);
5147 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5148 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
5151 /* Now create the decl for the field. */
5152 set_lineno (gnat_field, 0);
5153 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5154 packed, gnu_size, gnu_pos,
5155 Is_Aliased (gnat_field));
5157 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5159 if (Ekind (gnat_field) == E_Discriminant)
5160 DECL_DISCRIMINANT_NUMBER (gnu_field)
5161 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5166 /* Return 1 if TYPE is a type with variable size, a padding type with a field
5167 of variable size or is a record that has a field such a field. */
5170 is_variable_size (tree type)
5174 /* We need not be concerned about this at all if we don't have
5175 strict alignment. */
5176 if (! STRICT_ALIGNMENT)
5178 else if (! TREE_CONSTANT (TYPE_SIZE (type)))
5180 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5181 && ! TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5183 else if (TREE_CODE (type) != RECORD_TYPE
5184 && TREE_CODE (type) != UNION_TYPE
5185 && TREE_CODE (type) != QUAL_UNION_TYPE)
5188 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
5189 if (is_variable_size (TREE_TYPE (field)))
5195 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5196 of GCC trees for fields that are in the record and have already been
5197 processed. When called from gnat_to_gnu_entity during the processing of a
5198 record type definition, the GCC nodes for the discriminants will be on
5199 the chain. The other calls to this function are recursive calls from
5200 itself for the Component_List of a variant and the chain is empty.
5202 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5203 for a record type with "pragma component_alignment (storage_unit)".
5205 FINISH_RECORD is nonzero if this call will supply all of the remaining
5206 fields of the record.
5208 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5209 with a rep clause is to be added. If it is nonzero, that is all that
5210 should be done with such fields.
5212 CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
5213 before laying out the record. This means the alignment only serves
5214 to force fields to be bitfields, but not require the record to be
5215 that aligned. This is used for variants.
5217 ALL_REP, if nonzero, means that a rep clause was found for all the
5218 fields. This simplifies the logic since we know we're not in the mixed
5221 The processing of the component list fills in the chain with all of the
5222 fields of the record and then the record type is finished. */
5225 components_to_record (tree gnu_record_type,
5226 Node_Id component_list,
5227 tree gnu_field_list,
5230 tree *p_gnu_rep_list,
5231 int cancel_alignment,
5234 Node_Id component_decl;
5235 Entity_Id gnat_field;
5236 Node_Id variant_part;
5238 tree gnu_our_rep_list = NULL_TREE;
5239 tree gnu_field, gnu_last;
5240 int layout_with_rep = 0;
5241 int all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type) != 0;
5243 /* For each variable within each component declaration create a GCC field
5244 and add it to the list, skipping any pragmas in the list. */
5246 if (Present (Component_Items (component_list)))
5247 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5248 Present (component_decl);
5249 component_decl = Next_Non_Pragma (component_decl))
5251 gnat_field = Defining_Entity (component_decl);
5253 if (Chars (gnat_field) == Name_uParent)
5254 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5257 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5258 packed, definition);
5260 /* If this is the _Tag field, put it before any discriminants,
5261 instead of after them as is the case for all other fields.
5262 Ignore field of void type if only annotating. */
5263 if (Chars (gnat_field) == Name_uTag)
5264 gnu_field_list = chainon (gnu_field_list, gnu_field);
5267 TREE_CHAIN (gnu_field) = gnu_field_list;
5268 gnu_field_list = gnu_field;
5272 save_gnu_tree (gnat_field, gnu_field, 0);
5275 /* At the end of the component list there may be a variant part. */
5276 variant_part = Variant_Part (component_list);
5278 /* If this is an unchecked union, each variant must have exactly one
5279 component, each of which becomes one component of this union. */
5280 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5281 for (variant = First_Non_Pragma (Variants (variant_part));
5283 variant = Next_Non_Pragma (variant))
5286 = First_Non_Pragma (Component_Items (Component_List (variant)));
5287 gnat_field = Defining_Entity (component_decl);
5288 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5290 TREE_CHAIN (gnu_field) = gnu_field_list;
5291 gnu_field_list = gnu_field;
5292 save_gnu_tree (gnat_field, gnu_field, 0);
5295 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5296 mutually exclusive and should go in the same memory. To do this we need
5297 to treat each variant as a record whose elements are created from the
5298 component list for the variant. So here we create the records from the
5299 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5300 else if (Present (variant_part))
5302 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5304 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5305 tree gnu_union_field;
5306 tree gnu_variant_list = NULL_TREE;
5307 tree gnu_name = TYPE_NAME (gnu_record_type);
5309 = concat_id_with_name
5310 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5313 if (TREE_CODE (gnu_name) == TYPE_DECL)
5314 gnu_name = DECL_NAME (gnu_name);
5316 TYPE_NAME (gnu_union_type)
5317 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5318 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5320 for (variant = First_Non_Pragma (Variants (variant_part));
5322 variant = Next_Non_Pragma (variant))
5324 tree gnu_variant_type = make_node (RECORD_TYPE);
5325 tree gnu_inner_name;
5328 Get_Variant_Encoding (variant);
5329 gnu_inner_name = get_identifier (Name_Buffer);
5330 TYPE_NAME (gnu_variant_type)
5331 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5332 IDENTIFIER_POINTER (gnu_inner_name));
5334 /* Set the alignment of the inner type in case we need to make
5335 inner objects into bitfields, but then clear it out
5336 so the record actually gets only the alignment required. */
5337 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5338 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5340 /* Similarly, if the outer record has a size specified and all fields
5341 have record rep clauses, we can propagate the size into the
5343 if (all_rep_and_size)
5345 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5346 TYPE_SIZE_UNIT (gnu_variant_type)
5347 = TYPE_SIZE_UNIT (gnu_record_type);
5350 components_to_record (gnu_variant_type, Component_List (variant),
5351 NULL_TREE, packed, definition,
5352 &gnu_our_rep_list, !all_rep_and_size, all_rep);
5354 gnu_qual = choices_to_gnu (gnu_discriminant,
5355 Discrete_Choices (variant));
5357 Set_Present_Expr (variant, annotate_value (gnu_qual));
5358 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5361 ? TYPE_SIZE (gnu_record_type) : 0),
5363 ? bitsize_zero_node : 0),
5366 DECL_INTERNAL_P (gnu_field) = 1;
5367 DECL_QUALIFIER (gnu_field) = gnu_qual;
5368 TREE_CHAIN (gnu_field) = gnu_variant_list;
5369 gnu_variant_list = gnu_field;
5372 /* We use to delete the empty variants from the end. However,
5373 we no longer do that because we need them to generate complete
5374 debugging information for the variant record. Otherwise,
5375 the union type definition will be missing the fields associated
5376 to these empty variants. */
5378 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5379 if (gnu_variant_list != 0)
5381 if (all_rep_and_size)
5383 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5384 TYPE_SIZE_UNIT (gnu_union_type)
5385 = TYPE_SIZE_UNIT (gnu_record_type);
5388 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5389 all_rep_and_size, 0);
5392 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5394 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5395 all_rep ? bitsize_zero_node : 0, 0);
5397 DECL_INTERNAL_P (gnu_union_field) = 1;
5398 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5399 gnu_field_list = gnu_union_field;
5403 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5404 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5405 in a separate pass since we want to handle the discriminants but can't
5406 play with them until we've used them in debugging data above.
5408 ??? Note: if we then reorder them, debugging information will be wrong,
5409 but there's nothing that can be done about this at the moment. */
5411 for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5413 if (DECL_FIELD_OFFSET (gnu_field) != 0)
5415 tree gnu_next = TREE_CHAIN (gnu_field);
5418 gnu_field_list = gnu_next;
5420 TREE_CHAIN (gnu_last) = gnu_next;
5422 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5423 gnu_our_rep_list = gnu_field;
5424 gnu_field = gnu_next;
5428 gnu_last = gnu_field;
5429 gnu_field = TREE_CHAIN (gnu_field);
5433 /* If we have any items in our rep'ed field list, it is not the case that all
5434 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5435 set it and ignore the items. Otherwise, sort the fields by bit position
5436 and put them into their own record if we have any fields without
5438 if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5439 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5440 else if (gnu_our_rep_list != 0)
5443 = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5444 int len = list_length (gnu_our_rep_list);
5445 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5448 /* Set DECL_SECTION_NAME to increasing integers so we have a
5450 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5451 gnu_field = TREE_CHAIN (gnu_field), i++)
5453 gnu_arr[i] = gnu_field;
5454 DECL_SECTION_NAME (gnu_field) = size_int (i);
5457 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5459 /* Put the fields in the list in order of increasing position, which
5460 means we start from the end. */
5461 gnu_our_rep_list = NULL_TREE;
5462 for (i = len - 1; i >= 0; i--)
5464 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5465 gnu_our_rep_list = gnu_arr[i];
5466 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5467 DECL_SECTION_NAME (gnu_arr[i]) = 0;
5470 if (gnu_field_list != 0)
5472 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5473 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5474 gnu_record_type, 0, 0, 0, 1);
5475 DECL_INTERNAL_P (gnu_field) = 1;
5476 gnu_field_list = chainon (gnu_field_list, gnu_field);
5480 layout_with_rep = 1;
5481 gnu_field_list = nreverse (gnu_our_rep_list);
5485 if (cancel_alignment)
5486 TYPE_ALIGN (gnu_record_type) = 0;
5488 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5489 layout_with_rep, 0);
5492 /* Called via qsort from the above. Returns -1, 1, depending on the
5493 bit positions and ordinals of the two fields. */
5496 compare_field_bitpos (const PTR rt1, const PTR rt2)
5498 tree *t1 = (tree *) rt1;
5499 tree *t2 = (tree *) rt2;
5501 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5503 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5505 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5511 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5512 placed into an Esize, Component_Bit_Offset, or Component_Size value
5513 in the GNAT tree. */
5516 annotate_value (tree gnu_size)
5518 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5520 Node_Ref_Or_Val ops[3], ret;
5524 /* If back annotation is suppressed by the front end, return No_Uint */
5525 if (!Back_Annotate_Rep_Info)
5528 /* See if we've already saved the value for this node. */
5529 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (gnu_size)))
5530 && TREE_COMPLEXITY (gnu_size) != 0)
5531 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5533 /* If we do not return inside this switch, TCODE will be set to the
5534 code to use for a Create_Node operand and LEN (set above) will be
5535 the number of recursive calls for us to make. */
5537 switch (TREE_CODE (gnu_size))
5540 if (TREE_OVERFLOW (gnu_size))
5543 /* This may have come from a conversion from some smaller type,
5544 so ensure this is in bitsizetype. */
5545 gnu_size = convert (bitsizetype, gnu_size);
5547 /* For negative values, use NEGATE_EXPR of the supplied value. */
5548 if (tree_int_cst_sgn (gnu_size) < 0)
5550 /* The rediculous code below is to handle the case of the largest
5551 negative integer. */
5552 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5556 if (TREE_CONSTANT_OVERFLOW (negative_size))
5559 = size_binop (MINUS_EXPR, bitsize_zero_node,
5560 size_binop (PLUS_EXPR, gnu_size,
5565 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5567 temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5569 return annotate_value (temp);
5572 if (! host_integerp (gnu_size, 1))
5575 size = tree_low_cst (gnu_size, 1);
5577 /* This peculiar test is to make sure that the size fits in an int
5578 on machines where HOST_WIDE_INT is not "int". */
5579 if (tree_low_cst (gnu_size, 1) == size)
5580 return UI_From_Int (size);
5585 /* The only case we handle here is a simple discriminant reference. */
5586 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5587 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5588 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5589 return Create_Node (Discrim_Val,
5590 annotate_value (DECL_DISCRIMINANT_NUMBER
5591 (TREE_OPERAND (gnu_size, 1))),
5596 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5597 return annotate_value (TREE_OPERAND (gnu_size, 0));
5599 /* Now just list the operations we handle. */
5600 case COND_EXPR: tcode = Cond_Expr; break;
5601 case PLUS_EXPR: tcode = Plus_Expr; break;
5602 case MINUS_EXPR: tcode = Minus_Expr; break;
5603 case MULT_EXPR: tcode = Mult_Expr; break;
5604 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5605 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5606 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5607 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5608 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5609 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5610 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5611 case NEGATE_EXPR: tcode = Negate_Expr; break;
5612 case MIN_EXPR: tcode = Min_Expr; break;
5613 case MAX_EXPR: tcode = Max_Expr; break;
5614 case ABS_EXPR: tcode = Abs_Expr; break;
5615 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5616 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5617 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5618 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5619 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5620 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5621 case LT_EXPR: tcode = Lt_Expr; break;
5622 case LE_EXPR: tcode = Le_Expr; break;
5623 case GT_EXPR: tcode = Gt_Expr; break;
5624 case GE_EXPR: tcode = Ge_Expr; break;
5625 case EQ_EXPR: tcode = Eq_Expr; break;
5626 case NE_EXPR: tcode = Ne_Expr; break;
5632 /* Now get each of the operands that's relevant for this code. If any
5633 cannot be expressed as a repinfo node, say we can't. */
5634 for (i = 0; i < 3; i++)
5637 for (i = 0; i < len; i++)
5639 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5640 if (ops[i] == No_Uint)
5644 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5645 TREE_COMPLEXITY (gnu_size) = ret;
5649 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5650 GCC type, set Component_Bit_Offset and Esize to the position and size
5654 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5658 Entity_Id gnat_field;
5660 /* We operate by first making a list of all field and their positions
5661 (we can get the sizes easily at any time) by a recursive call
5662 and then update all the sizes into the tree. */
5663 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5664 size_zero_node, bitsize_zero_node,
5667 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5668 gnat_field = Next_Entity (gnat_field))
5669 if ((Ekind (gnat_field) == E_Component
5670 || (Ekind (gnat_field) == E_Discriminant
5671 && ! Is_Unchecked_Union (Scope (gnat_field)))))
5673 tree parent_offset = bitsize_zero_node;
5676 = purpose_member (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0),
5681 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5683 /* In this mode the tag and parent components have not been
5684 generated, so we add the appropriate offset to each
5685 component. For a component appearing in the current
5686 extension, the offset is the size of the parent. */
5687 if (Is_Derived_Type (gnat_entity)
5688 && Original_Record_Component (gnat_field) == gnat_field)
5690 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5693 parent_offset = bitsize_int (POINTER_SIZE);
5696 Set_Component_Bit_Offset
5699 (size_binop (PLUS_EXPR,
5700 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5701 TREE_VALUE (TREE_VALUE
5702 (TREE_VALUE (gnu_entry)))),
5705 Set_Esize (gnat_field,
5706 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5708 else if (type_annotate_only
5709 && Is_Tagged_Type (gnat_entity)
5710 && Is_Derived_Type (gnat_entity))
5712 /* If there is no gnu_entry, this is an inherited component whose
5713 position is the same as in the parent type. */
5714 Set_Component_Bit_Offset
5716 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5717 Set_Esize (gnat_field,
5718 Esize (Original_Record_Component (gnat_field)));
5723 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5724 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5725 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5726 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5727 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5728 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5732 compute_field_positions (tree gnu_type,
5736 unsigned int offset_align)
5739 tree gnu_result = gnu_list;
5741 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5742 gnu_field = TREE_CHAIN (gnu_field))
5744 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5745 DECL_FIELD_BIT_OFFSET (gnu_field));
5746 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5747 DECL_FIELD_OFFSET (gnu_field));
5748 unsigned int our_offset_align
5749 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5752 = tree_cons (gnu_field,
5753 tree_cons (gnu_our_offset,
5754 tree_cons (size_int (our_offset_align),
5755 gnu_our_bitpos, NULL_TREE),
5759 if (DECL_INTERNAL_P (gnu_field))
5761 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5762 gnu_our_offset, gnu_our_bitpos,
5769 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5770 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5771 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5772 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5773 for the size of a field. COMPONENT_P is true if we are being called
5774 to process the Component_Size of GNAT_OBJECT. This is used for error
5775 message handling and to indicate to use the object size of GNU_TYPE.
5776 ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5777 it means that a size of zero should be treated as an unspecified size. */
5780 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
5781 enum tree_code kind, int component_p, int zero_ok)
5783 Node_Id gnat_error_node;
5785 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5788 /* Find the node to use for errors. */
5789 if ((Ekind (gnat_object) == E_Component
5790 || Ekind (gnat_object) == E_Discriminant)
5791 && Present (Component_Clause (gnat_object)))
5792 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5793 else if (Present (Size_Clause (gnat_object)))
5794 gnat_error_node = Expression (Size_Clause (gnat_object));
5796 gnat_error_node = gnat_object;
5798 /* Return 0 if no size was specified, either because Esize was not Present or
5799 the specified size was zero. */
5800 if (No (uint_size) || uint_size == No_Uint)
5803 /* Get the size as a tree. Give an error if a size was specified, but cannot
5804 be represented as in sizetype. */
5805 size = UI_To_gnu (uint_size, bitsizetype);
5806 if (TREE_OVERFLOW (size))
5808 post_error_ne (component_p ? "component size of & is too large"
5809 : "size of & is too large",
5810 gnat_error_node, gnat_object);
5813 /* Ignore a negative size since that corresponds to our back-annotation.
5814 Also ignore a zero size unless a size clause exists. */
5815 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
5818 /* The size of objects is always a multiple of a byte. */
5819 if (kind == VAR_DECL
5820 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5821 bitsize_unit_node)))
5824 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5825 gnat_error_node, gnat_object);
5827 post_error_ne ("size for& is not a multiple of Storage_Unit",
5828 gnat_error_node, gnat_object);
5832 /* If this is an integral type or a packed array type, the front-end has
5833 verified the size, so we need not do it here (which would entail
5834 checking against the bounds). However, if this is an aliased object, it
5835 may not be smaller than the type of the object. */
5836 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
5837 && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5840 /* If the object is a record that contains a template, add the size of
5841 the template to the specified size. */
5842 if (TREE_CODE (gnu_type) == RECORD_TYPE
5843 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5844 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5846 /* Modify the size of the type to be that of the maximum size if it has a
5847 discriminant or the size of a thin pointer if this is a fat pointer. */
5848 if (type_size != 0 && CONTAINS_PLACEHOLDER_P (type_size))
5849 type_size = max_size (type_size, 1);
5850 else if (TYPE_FAT_POINTER_P (gnu_type))
5851 type_size = bitsize_int (POINTER_SIZE);
5853 /* If this is an access type, the minimum size is that given by the smallest
5854 integral mode that's valid for pointers. */
5855 if (TREE_CODE (gnu_type) == POINTER_TYPE)
5857 enum machine_mode p_mode;
5859 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5860 !targetm.valid_pointer_mode (p_mode);
5861 p_mode = GET_MODE_WIDER_MODE (p_mode))
5864 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
5867 /* If the size of the object is a constant, the new size must not be
5869 if (TREE_CODE (type_size) != INTEGER_CST
5870 || TREE_OVERFLOW (type_size)
5871 || tree_int_cst_lt (size, type_size))
5875 ("component size for& too small{, minimum allowed is ^}",
5876 gnat_error_node, gnat_object, type_size);
5878 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5879 gnat_error_node, gnat_object, type_size);
5881 if (kind == VAR_DECL && ! component_p
5882 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5883 && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5884 post_error_ne_tree_2
5885 ("\\size of ^ is not a multiple of alignment (^ bits)",
5886 gnat_error_node, gnat_object, rm_size (gnu_type),
5887 TYPE_ALIGN (gnu_type));
5889 else if (INTEGRAL_TYPE_P (gnu_type))
5890 post_error_ne ("\\size would be legal if & were not aliased!",
5891 gnat_error_node, gnat_object);
5899 /* Similarly, but both validate and process a value of RM_Size. This
5900 routine is only called for types. */
5903 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
5905 /* Only give an error if a Value_Size clause was explicitly given.
5906 Otherwise, we'd be duplicating an error on the Size clause. */
5907 Node_Id gnat_attr_node
5908 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5909 tree old_size = rm_size (gnu_type);
5912 /* Get the size as a tree. Do nothing if none was specified, either
5913 because RM_Size was not Present or if the specified size was zero.
5914 Give an error if a size was specified, but cannot be represented as
5916 if (No (uint_size) || uint_size == No_Uint)
5919 size = UI_To_gnu (uint_size, bitsizetype);
5920 if (TREE_OVERFLOW (size))
5922 if (Present (gnat_attr_node))
5923 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5929 /* Ignore a negative size since that corresponds to our back-annotation.
5930 Also ignore a zero size unless a size clause exists, a Value_Size
5931 clause exists, or this is an integer type, in which case the
5932 front end will have always set it. */
5933 else if (tree_int_cst_sgn (size) < 0
5934 || (integer_zerop (size) && No (gnat_attr_node)
5935 && ! Has_Size_Clause (gnat_entity)
5936 && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5939 /* If the old size is self-referential, get the maximum size. */
5940 if (CONTAINS_PLACEHOLDER_P (old_size))
5941 old_size = max_size (old_size, 1);
5943 /* If the size of the object is a constant, the new size must not be
5944 smaller (the front end checks this for scalar types). */
5945 if (TREE_CODE (old_size) != INTEGER_CST
5946 || TREE_OVERFLOW (old_size)
5947 || (AGGREGATE_TYPE_P (gnu_type)
5948 && tree_int_cst_lt (size, old_size)))
5950 if (Present (gnat_attr_node))
5952 ("Value_Size for& too small{, minimum allowed is ^}",
5953 gnat_attr_node, gnat_entity, old_size);
5958 /* Otherwise, set the RM_Size. */
5959 if (TREE_CODE (gnu_type) == INTEGER_TYPE
5960 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5961 TYPE_RM_SIZE_INT (gnu_type) = size;
5962 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
5963 SET_TYPE_RM_SIZE_ENUM (gnu_type, size);
5964 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5965 || TREE_CODE (gnu_type) == UNION_TYPE
5966 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5967 && ! TYPE_IS_FAT_POINTER_P (gnu_type))
5968 SET_TYPE_ADA_SIZE (gnu_type, size);
5971 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5972 If TYPE is the best type, return it. Otherwise, make a new type. We
5973 only support new integral and pointer types. BIASED_P is nonzero if
5974 we are making a biased type. */
5977 make_type_from_size (tree type, tree size_tree, int biased_p)
5980 unsigned HOST_WIDE_INT size;
5982 /* If size indicates an error, just return TYPE to avoid propagating the
5983 error. Likewise if it's too large to represent. */
5984 if (size_tree == 0 || ! host_integerp (size_tree, 1))
5987 size = tree_low_cst (size_tree, 1);
5988 switch (TREE_CODE (type))
5992 /* Only do something if the type is not already the proper size and is
5993 not a packed array type. */
5994 if (TYPE_PACKED_ARRAY_TYPE_P (type)
5995 || (TYPE_PRECISION (type) == size
5996 && biased_p == (TREE_CODE (type) == INTEGER_CST
5997 && TYPE_BIASED_REPRESENTATION_P (type))))
6000 size = MIN (size, LONG_LONG_TYPE_SIZE);
6001 new_type = make_signed_type (size);
6002 TREE_TYPE (new_type)
6003 = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
6004 TYPE_MIN_VALUE (new_type)
6005 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6006 TYPE_MAX_VALUE (new_type)
6007 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6008 TYPE_BIASED_REPRESENTATION_P (new_type)
6009 = ((TREE_CODE (type) == INTEGER_TYPE
6010 && TYPE_BIASED_REPRESENTATION_P (type))
6012 TREE_UNSIGNED (new_type)
6013 = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
6014 TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
6018 /* Do something if this is a fat pointer, in which case we
6019 may need to return the thin pointer. */
6020 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6023 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6027 /* Only do something if this is a thin pointer, in which case we
6028 may need to return the fat pointer. */
6029 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6031 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6042 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6043 a type or object whose present alignment is ALIGN. If this alignment is
6044 valid, return it. Otherwise, give an error and return ALIGN. */
6047 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6049 Node_Id gnat_error_node = gnat_entity;
6050 unsigned int new_align;
6052 #ifndef MAX_OFILE_ALIGNMENT
6053 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6056 if (Present (Alignment_Clause (gnat_entity)))
6057 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6059 /* Don't worry about checking alignment if alignment was not specified
6060 by the source program and we already posted an error for this entity. */
6062 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6065 /* Within GCC, an alignment is an integer, so we must make sure a
6066 value is specified that fits in that range. Also, alignments of
6067 more than MAX_OFILE_ALIGNMENT can't be supported. */
6069 if (! UI_Is_In_Int_Range (alignment)
6070 || ((new_align = UI_To_Int (alignment))
6071 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6072 post_error_ne_num ("largest supported alignment for& is ^",
6073 gnat_error_node, gnat_entity,
6074 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6075 else if (! (Present (Alignment_Clause (gnat_entity))
6076 && From_At_Mod (Alignment_Clause (gnat_entity)))
6077 && new_align * BITS_PER_UNIT < align)
6078 post_error_ne_num ("alignment for& must be at least ^",
6079 gnat_error_node, gnat_entity,
6080 align / BITS_PER_UNIT);
6082 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6087 /* Verify that OBJECT, a type or decl, is something we can implement
6088 atomically. If not, give an error for GNAT_ENTITY. COMP_P is nonzero
6089 if we require atomic components. */
6092 check_ok_for_atomic (tree object, Entity_Id gnat_entity, int comp_p)
6094 Node_Id gnat_error_point = gnat_entity;
6096 enum machine_mode mode;
6100 /* There are three case of what OBJECT can be. It can be a type, in which
6101 case we take the size, alignment and mode from the type. It can be a
6102 declaration that was indirect, in which case the relevant values are
6103 that of the type being pointed to, or it can be a normal declaration,
6104 in which case the values are of the decl. The code below assumes that
6105 OBJECT is either a type or a decl. */
6106 if (TYPE_P (object))
6108 mode = TYPE_MODE (object);
6109 align = TYPE_ALIGN (object);
6110 size = TYPE_SIZE (object);
6112 else if (DECL_BY_REF_P (object))
6114 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6115 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6116 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6120 mode = DECL_MODE (object);
6121 align = DECL_ALIGN (object);
6122 size = DECL_SIZE (object);
6125 /* Consider all floating-point types atomic and any types that that are
6126 represented by integers no wider than a machine word. */
6127 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6128 || ((GET_MODE_CLASS (mode) == MODE_INT
6129 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6130 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6133 /* For the moment, also allow anything that has an alignment equal
6134 to its size and which is smaller than a word. */
6135 if (size != 0 && TREE_CODE (size) == INTEGER_CST
6136 && compare_tree_int (size, align) == 0
6137 && align <= BITS_PER_WORD)
6140 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6141 gnat_node = Next_Rep_Item (gnat_node))
6143 if (! comp_p && Nkind (gnat_node) == N_Pragma
6144 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6145 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6146 else if (comp_p && Nkind (gnat_node) == N_Pragma
6147 && (Get_Pragma_Id (Chars (gnat_node))
6148 == Pragma_Atomic_Components))
6149 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6153 post_error_ne ("atomic access to component of & cannot be guaranteed",
6154 gnat_error_point, gnat_entity);
6156 post_error_ne ("atomic access to & cannot be guaranteed",
6157 gnat_error_point, gnat_entity);
6160 /* Given a type T, a FIELD_DECL F, and a replacement value R,
6161 return a new type with all size expressions that contain F
6162 updated by replacing F with R. This is identical to GCC's
6163 substitute_in_type except that it knows about TYPE_INDEX_TYPE.
6164 If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
6168 gnat_substitute_in_type (tree t, tree f, tree r)
6173 switch (TREE_CODE (t))
6179 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6180 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6182 tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
6183 tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
6185 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6188 new = build_range_type (TREE_TYPE (t), low, high);
6189 if (TYPE_INDEX_TYPE (t))
6190 SET_TYPE_INDEX_TYPE (new,
6191 gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6198 if ((TYPE_MIN_VALUE (t) != 0
6199 && CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t)))
6200 || (TYPE_MAX_VALUE (t) != 0
6201 && CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t))))
6203 tree low = 0, high = 0;
6205 if (TYPE_MIN_VALUE (t))
6206 low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
6207 if (TYPE_MAX_VALUE (t))
6208 high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
6210 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6214 TYPE_MIN_VALUE (t) = low;
6215 TYPE_MAX_VALUE (t) = high;
6220 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6221 if (tem == TREE_TYPE (t))
6224 return build_complex_type (tem);
6232 /* Don't know how to do these yet. */
6237 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6238 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6240 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6243 new = build_array_type (component, domain);
6244 TYPE_SIZE (new) = 0;
6245 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6246 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6248 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6254 case QUAL_UNION_TYPE:
6258 = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
6259 int field_has_rep = 0;
6260 tree last_field = 0;
6262 tree new = copy_type (t);
6264 /* Start out with no fields, make new fields, and chain them
6265 in. If we haven't actually changed the type of any field,
6266 discard everything we've done and return the old type. */
6268 TYPE_FIELDS (new) = 0;
6269 TYPE_SIZE (new) = 0;
6271 for (field = TYPE_FIELDS (t); field;
6272 field = TREE_CHAIN (field))
6274 tree new_field = copy_node (field);
6276 TREE_TYPE (new_field)
6277 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6279 if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
6281 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6284 /* If this is an internal field and the type of this field is
6285 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6286 the type just has one element, treat that as the field.
6287 But don't do this if we are processing a QUAL_UNION_TYPE. */
6288 if (TREE_CODE (t) != QUAL_UNION_TYPE
6289 && DECL_INTERNAL_P (new_field)
6290 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6291 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6293 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
6296 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
6299 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6301 /* Make sure omitting the union doesn't change
6303 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6304 new_field = next_new_field;
6308 DECL_CONTEXT (new_field) = new;
6309 SET_DECL_ORIGINAL_FIELD (new_field,
6310 (DECL_ORIGINAL_FIELD (field) != 0
6311 ? DECL_ORIGINAL_FIELD (field) : field));
6313 /* If the size of the old field was set at a constant,
6314 propagate the size in case the type's size was variable.
6315 (This occurs in the case of a variant or discriminated
6316 record with a default size used as a field of another
6318 DECL_SIZE (new_field)
6319 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6320 ? DECL_SIZE (field) : 0;
6321 DECL_SIZE_UNIT (new_field)
6322 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6323 ? DECL_SIZE_UNIT (field) : 0;
6325 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6327 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6329 if (new_q != DECL_QUALIFIER (new_field))
6332 /* Do the substitution inside the qualifier and if we find
6333 that this field will not be present, omit it. */
6334 DECL_QUALIFIER (new_field) = new_q;
6336 if (integer_zerop (DECL_QUALIFIER (new_field)))
6340 if (last_field == 0)
6341 TYPE_FIELDS (new) = new_field;
6343 TREE_CHAIN (last_field) = new_field;
6345 last_field = new_field;
6347 /* If this is a qualified type and this field will always be
6348 present, we are done. */
6349 if (TREE_CODE (t) == QUAL_UNION_TYPE
6350 && integer_onep (DECL_QUALIFIER (new_field)))
6354 /* If this used to be a qualified union type, but we now know what
6355 field will be present, make this a normal union. */
6356 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6357 && (TYPE_FIELDS (new) == 0
6358 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6359 TREE_SET_CODE (new, UNION_TYPE);
6360 else if (! changed_field)
6368 /* If the size was originally a constant use it. */
6369 if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6370 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6372 TYPE_SIZE (new) = TYPE_SIZE (t);
6373 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6374 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6385 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6386 needed to represent the object. */
6389 rm_size (tree gnu_type)
6391 /* For integer types, this is the precision. For record types, we store
6392 the size explicitly. For other types, this is just the size. */
6394 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6395 return TYPE_RM_SIZE (gnu_type);
6396 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6397 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6398 /* Return the rm_size of the actual data plus the size of the template. */
6400 size_binop (PLUS_EXPR,
6401 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6402 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6403 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6404 || TREE_CODE (gnu_type) == UNION_TYPE
6405 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6406 && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6407 && TYPE_ADA_SIZE (gnu_type) != 0)
6408 return TYPE_ADA_SIZE (gnu_type);
6410 return TYPE_SIZE (gnu_type);
6413 /* Return an identifier representing the external name to be used for
6414 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6415 and the specified suffix. */
6418 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6420 const char *str = (suffix == 0 ? "" : suffix);
6421 String_Template temp = {1, strlen (str)};
6422 Fat_Pointer fp = {str, &temp};
6424 Get_External_Name_With_Suffix (gnat_entity, fp);
6427 /* A variable using the Stdcall convention (meaning we are running
6428 on a Windows box) live in a DLL. Here we adjust its name to use
6429 the jump-table, the _imp__NAME contains the address for the NAME
6433 Entity_Kind kind = Ekind (gnat_entity);
6434 const char *prefix = "_imp__";
6435 int plen = strlen (prefix);
6437 if ((kind == E_Variable || kind == E_Constant)
6438 && Convention (gnat_entity) == Convention_Stdcall)
6441 for (k = 0; k <= Name_Len; k++)
6442 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6443 strncpy (Name_Buffer, prefix, plen);
6448 return get_identifier (Name_Buffer);
6451 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6452 fully-qualified name, possibly with type information encoding.
6453 Otherwise, return the name. */
6456 get_entity_name (Entity_Id gnat_entity)
6458 Get_Encoded_Name (gnat_entity);
6459 return get_identifier (Name_Buffer);
6462 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6463 string, return a new IDENTIFIER_NODE that is the concatenation of
6464 the name in GNU_ID and SUFFIX. */
6467 concat_id_with_name (tree gnu_id, const char *suffix)
6469 int len = IDENTIFIER_LENGTH (gnu_id);
6471 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6472 IDENTIFIER_LENGTH (gnu_id));
6473 strncpy (Name_Buffer + len, "___", 3);
6475 strcpy (Name_Buffer + len, suffix);
6476 return get_identifier (Name_Buffer);
6479 #include "gt-ada-decl.h"