1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
11 * Copyright (C) 1992-2001, Free Software Foundation, Inc. *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
24 * GNAT was originally developed by the GNAT team at New York University. *
25 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
27 ****************************************************************************/
54 /* Setting this to 1 suppresses hashing of types. */
55 extern int debug_no_type_hash;
57 /* Provide default values for the macros controlling stack checking.
58 This is copied from GCC's expr.h. */
60 #ifndef STACK_CHECK_BUILTIN
61 #define STACK_CHECK_BUILTIN 0
63 #ifndef STACK_CHECK_PROBE_INTERVAL
64 #define STACK_CHECK_PROBE_INTERVAL 4096
66 #ifndef STACK_CHECK_MAX_FRAME_SIZE
67 #define STACK_CHECK_MAX_FRAME_SIZE \
68 (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
70 #ifndef STACK_CHECK_MAX_VAR_SIZE
71 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
74 /* These two variables are used to defer recursively expanding incomplete
75 types while we are processing a record or subprogram type. */
77 static int defer_incomplete_level = 0;
78 static struct incomplete
80 struct incomplete *next;
83 } *defer_incomplete_list = 0;
85 static tree substitution_list PARAMS ((Entity_Id, Entity_Id,
87 static int allocatable_size_p PARAMS ((tree, int));
88 static struct attrib *build_attr_list PARAMS ((Entity_Id));
89 static tree elaborate_expression PARAMS ((Node_Id, Entity_Id, tree,
91 static tree elaborate_expression_1 PARAMS ((Node_Id, Entity_Id, tree,
93 static tree make_packable_type PARAMS ((tree));
94 static tree maybe_pad_type PARAMS ((tree, tree, unsigned int,
95 Entity_Id, const char *, int,
97 static tree gnat_to_gnu_field PARAMS ((Entity_Id, tree, int, int));
98 static void components_to_record PARAMS ((tree, Node_Id, tree, int,
99 int, tree *, int, int));
100 static int compare_field_bitpos PARAMS ((const PTR, const PTR));
101 static Uint annotate_value PARAMS ((tree));
102 static void annotate_rep PARAMS ((Entity_Id, tree));
103 static tree compute_field_positions PARAMS ((tree, tree, tree, tree));
104 static tree validate_size PARAMS ((Uint, tree, Entity_Id,
105 enum tree_code, int, int));
106 static void set_rm_size PARAMS ((Uint, tree, Entity_Id));
107 static tree make_type_from_size PARAMS ((tree, tree, int));
108 static unsigned int validate_alignment PARAMS ((Uint, Entity_Id,
110 static void check_ok_for_atomic PARAMS ((tree, Entity_Id, int));
112 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
113 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
114 refer to an Ada type. */
117 gnat_to_gnu_type (gnat_entity)
118 Entity_Id gnat_entity;
122 /* Convert the ada entity type into a GCC TYPE_DECL node. */
123 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
124 if (TREE_CODE (gnu_decl) != TYPE_DECL)
127 return TREE_TYPE (gnu_decl);
130 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
131 entity, this routine returns the equivalent GCC tree for that entity
132 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
135 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
136 initial value (in GCC tree form). This is optional for variables.
137 For renamed entities, GNU_EXPR gives the object being renamed.
139 DEFINITION is nonzero if this call is intended for a definition. This is
140 used for separate compilation where it necessary to know whether an
141 external declaration or a definition should be created if the GCC equivalent
142 was not created previously. The value of 1 is normally used for a non-zero
143 DEFINITION, but a value of 2 is used in special circumstances, defined in
147 gnat_to_gnu_entity (gnat_entity, gnu_expr, definition)
148 Entity_Id gnat_entity;
154 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
155 GNAT tree. This node will be associated with the GNAT node by calling
156 the save_gnu_tree routine at the end of the `switch' statement. */
158 /* Nonzero if we have already saved gnu_decl as a gnat association. */
160 /* Nonzero if we incremented defer_incomplete_level. */
161 int this_deferred = 0;
162 /* Nonzero if we incremented force_global. */
164 /* Nonzero if we should check to see if elaborated during processing. */
165 int maybe_present = 0;
166 /* Nonzero if we made GNU_DECL and its type here. */
167 int this_made_decl = 0;
168 struct attrib *attr_list = 0;
169 int debug_info_p = (Needs_Debug_Info (gnat_entity)
170 || debug_info_level == DINFO_LEVEL_VERBOSE);
171 Entity_Kind kind = Ekind (gnat_entity);
174 = ((Known_Esize (gnat_entity)
175 && UI_Is_In_Int_Range (Esize (gnat_entity)))
176 ? MIN (UI_To_Int (Esize (gnat_entity)),
177 IN (kind, Float_Kind)
178 ? LONG_DOUBLE_TYPE_SIZE
179 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
180 : LONG_LONG_TYPE_SIZE)
181 : LONG_LONG_TYPE_SIZE);
184 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
185 || From_With_Type (gnat_entity));
186 unsigned int align = 0;
188 /* Since a use of an Itype is a definition, process it as such if it
189 is not in a with'ed unit. */
191 if (! definition && Is_Itype (gnat_entity)
192 && ! present_gnu_tree (gnat_entity)
193 && In_Extended_Main_Code_Unit (gnat_entity))
195 /* Ensure that we are in a subprogram mentioned in the Scope
196 chain of this entity, our current scope is global,
197 or that we encountered a task or entry (where we can't currently
198 accurately check scoping). */
199 if (current_function_decl == 0
200 || DECL_ELABORATION_PROC_P (current_function_decl))
202 process_type (gnat_entity);
203 return get_gnu_tree (gnat_entity);
206 for (gnat_temp = Scope (gnat_entity);
207 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
209 if (Is_Type (gnat_temp))
210 gnat_temp = Underlying_Type (gnat_temp);
212 if (Ekind (gnat_temp) == E_Subprogram_Body)
214 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
216 if (IN (Ekind (gnat_temp), Subprogram_Kind)
217 && Present (Protected_Body_Subprogram (gnat_temp)))
218 gnat_temp = Protected_Body_Subprogram (gnat_temp);
220 if (Ekind (gnat_temp) == E_Entry
221 || Ekind (gnat_temp) == E_Entry_Family
222 || Ekind (gnat_temp) == E_Task_Type
223 || (IN (Ekind (gnat_temp), Subprogram_Kind)
224 && present_gnu_tree (gnat_temp)
225 && (current_function_decl
226 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
228 process_type (gnat_entity);
229 return get_gnu_tree (gnat_entity);
233 /* gigi abort 122 means that the entity "gnat_entity" has an incorrect
234 scope, i.e. that its scope does not correspond to the subprogram
235 in which it is declared */
239 /* If this is entity 0, something went badly wrong. */
240 if (gnat_entity == 0)
243 /* If we've already processed this entity, return what we got last time.
244 If we are defining the node, we should not have already processed it.
245 In that case, we will abort below when we try to save a new GCC tree for
246 this object. We also need to handle the case of getting a dummy type
247 when a Full_View exists. */
249 if (present_gnu_tree (gnat_entity)
251 || (Is_Type (gnat_entity) && imported_p)))
253 gnu_decl = get_gnu_tree (gnat_entity);
255 if (TREE_CODE (gnu_decl) == TYPE_DECL
256 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
257 && IN (kind, Incomplete_Or_Private_Kind)
258 && Present (Full_View (gnat_entity)))
260 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
263 save_gnu_tree (gnat_entity, NULL_TREE, 0);
264 save_gnu_tree (gnat_entity, gnu_decl, 0);
270 /* If this is a numeric or enumeral type, or an access type, a nonzero
271 Esize must be specified unless it was specified by the programmer. */
272 if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
273 || (IN (kind, Access_Kind)
274 && kind != E_Access_Protected_Subprogram_Type
275 && kind != E_Access_Subtype))
276 && Unknown_Esize (gnat_entity)
277 && ! Has_Size_Clause (gnat_entity))
280 /* Likewise, RM_Size must be specified for all discrete and fixed-point
282 if (IN (kind, Discrete_Or_Fixed_Point_Kind)
283 && Unknown_RM_Size (gnat_entity))
286 /* Get the name of the entity and set up the line number and filename of
287 the original definition for use in any decl we make. */
289 gnu_entity_id = get_entity_name (gnat_entity);
290 set_lineno (gnat_entity, 0);
292 /* If we get here, it means we have not yet done anything with this
293 entity. If we are not defining it here, it must be external,
294 otherwise we should have defined it already. */
295 if (! definition && ! Is_Public (gnat_entity)
296 && ! type_annotate_only
297 && kind != E_Discriminant && kind != E_Component
299 && ! (kind == E_Constant && Present (Full_View (gnat_entity)))
301 && !IN (kind, Type_Kind)
306 /* For cases when we are not defining (i.e., we are referencing from
307 another compilation unit) Public entities, show we are at global level
308 for the purpose of computing sizes. Don't do this for components or
309 discriminants since the relevant test is whether or not the record is
311 if (! definition && Is_Public (gnat_entity)
312 && ! Is_Statically_Allocated (gnat_entity)
313 && kind != E_Discriminant && kind != E_Component)
314 force_global++, this_global = 1;
316 /* Handle any attributes. */
317 if (Has_Gigi_Rep_Item (gnat_entity))
318 attr_list = build_attr_list (gnat_entity);
323 /* If this is a use of a deferred constant, get its full
325 if (! definition && Present (Full_View (gnat_entity)))
327 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
328 gnu_expr, definition);
333 /* If we have an external constant that we are not defining,
334 get the expression that is was defined to represent. We
335 may throw that expression away later if it is not a
338 && Present (Expression (Declaration_Node (gnat_entity)))
339 && ! No_Initialization (Declaration_Node (gnat_entity)))
340 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
342 /* Ignore deferred constant definitions; they are processed fully in the
343 front-end. For deferred constant references, get the full
344 definition. On the other hand, constants that are renamings are
345 handled like variable renamings. If No_Initialization is set, this is
346 not a deferred constant but a constant whose value is built
349 if (definition && gnu_expr == 0
350 && ! No_Initialization (Declaration_Node (gnat_entity))
351 && No (Renamed_Object (gnat_entity)))
353 gnu_decl = error_mark_node;
357 else if (! definition && IN (kind, Incomplete_Or_Private_Kind)
358 && Present (Full_View (gnat_entity)))
360 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
369 /* If this is not a VMS exception, treat it as a normal object.
370 Otherwise, make an object at the specific address of character
371 type, point to it, and convert it to integer, and mask off
373 if (! Is_VMS_Exception (gnat_entity))
376 /* Allocate the global object that we use to get the value of the
378 gnu_decl = create_var_decl (gnu_entity_id,
379 (Present (Interface_Name (gnat_entity))
380 ? create_concat_name (gnat_entity, 0)
382 char_type_node, NULL_TREE, 0, 0, 1, 1,
385 /* Now return the expression giving the desired value. */
387 = build_binary_op (BIT_AND_EXPR, integer_type_node,
388 convert (integer_type_node,
389 build_unary_op (ADDR_EXPR, NULL_TREE,
391 build_unary_op (NEGATE_EXPR, integer_type_node,
392 build_int_2 (7, 0)));
394 save_gnu_tree (gnat_entity, gnu_decl, 1);
401 /* The GNAT record where the component was defined. */
402 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
404 /* If the variable is an inherited record component (in the case of
405 extended record types), just return the inherited entity, which
406 must be a FIELD_DECL. Likewise for discriminants.
407 For discriminants of untagged records which have explicit
408 girder discriminants, return the entity for the corresponding
409 girder discriminant. Also use Original_Record_Component
410 if the record has a private extension. */
412 if ((Base_Type (gnat_record) == gnat_record
413 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
414 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
415 && Present (Original_Record_Component (gnat_entity))
416 && Original_Record_Component (gnat_entity) != gnat_entity)
419 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
420 gnu_expr, definition);
425 /* If the enclosing record has explicit girder discriminants,
426 then it is an untagged record. If the Corresponding_Discriminant
427 is not empty then this must be a renamed discriminant and its
428 Original_Record_Component must point to the corresponding explicit
429 girder discriminant (i.e., we should have taken the previous
432 else if (Present (Corresponding_Discriminant (gnat_entity))
433 && Is_Tagged_Type (gnat_record))
435 /* A tagged record has no explicit girder discriminants. */
437 if (First_Discriminant (gnat_record)
438 != First_Girder_Discriminant (gnat_record))
442 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
443 gnu_expr, definition);
448 /* If the enclosing record has explicit girder discriminants,
449 then it is an untagged record. If the Corresponding_Discriminant
450 is not empty then this must be a renamed discriminant and its
451 Original_Record_Component must point to the corresponding explicit
452 girder discriminant (i.e., we should have taken the first
455 else if (Present (Corresponding_Discriminant (gnat_entity))
456 && (First_Discriminant (gnat_record)
457 != First_Girder_Discriminant (gnat_record)))
460 /* Otherwise, if we are not defining this and we have no GCC type
461 for the containing record, make one for it. Then we should
462 have made our own equivalent. */
463 else if (! definition && ! present_gnu_tree (gnat_record))
465 /* ??? If this is in a record whose scope is a protected
466 type and we have an Original_Record_Component, use it.
467 This is a workaround for major problems in protected type
469 if (Is_Protected_Type (Scope (Scope (gnat_entity)))
470 && Present (Original_Record_Component (gnat_entity)))
473 = gnat_to_gnu_entity (Original_Record_Component
475 gnu_expr, definition);
480 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
481 gnu_decl = get_gnu_tree (gnat_entity);
486 /* Here we have no GCC type and this is a reference rather than a
487 definition. This should never happen. Most likely the cause is a
488 reference before declaration in the gnat tree for gnat_entity. */
493 case E_Loop_Parameter:
494 case E_Out_Parameter:
497 /* Simple variables, loop variables, OUT parameters, and exceptions. */
502 = ((kind == E_Constant || kind == E_Variable)
503 && ! Is_Statically_Allocated (gnat_entity)
504 && Is_True_Constant (gnat_entity)
505 && (((Nkind (Declaration_Node (gnat_entity))
506 == N_Object_Declaration)
507 && Present (Expression (Declaration_Node (gnat_entity))))
508 || Present (Renamed_Object (gnat_entity))));
509 int inner_const_flag = const_flag;
510 int static_p = Is_Statically_Allocated (gnat_entity);
511 tree gnu_ext_name = NULL_TREE;
513 if (Present (Renamed_Object (gnat_entity)) && ! definition)
515 if (kind == E_Exception)
516 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
519 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
522 /* Get the type after elaborating the renamed object. */
523 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
525 /* If this is a loop variable, its type should be the base type.
526 This is because the code for processing a loop determines whether
527 a normal loop end test can be done by comparing the bounds of the
528 loop against those of the base type, which is presumed to be the
529 size used for computation. But this is not correct when the size
530 of the subtype is smaller than the type. */
531 if (kind == E_Loop_Parameter)
532 gnu_type = get_base_type (gnu_type);
534 /* Reject non-renamed objects whose types are unconstrained arrays or
535 any object whose type is a dummy type or VOID_TYPE. */
537 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
538 && No (Renamed_Object (gnat_entity)))
539 || TYPE_IS_DUMMY_P (gnu_type)
540 || TREE_CODE (gnu_type) == VOID_TYPE)
542 if (type_annotate_only)
543 return error_mark_node;
548 /* If we are defining the object, see if it has a Size value and
549 validate it if so. Then get the new type, if any. */
551 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
552 gnat_entity, VAR_DECL, 0,
553 Has_Size_Clause (gnat_entity));
558 = make_type_from_size (gnu_type, gnu_size,
559 Has_Biased_Representation (gnat_entity));
561 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
565 /* If this object has self-referential size, it must be a record with
566 a default value. We are supposed to allocate an object of the
567 maximum size in this case unless it is a constant with an
568 initializing expression, in which case we can get the size from
569 that. Note that the resulting size may still be a variable, so
570 this may end up with an indirect allocation. */
572 if (No (Renamed_Object (gnat_entity))
573 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
574 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
576 if (gnu_expr != 0 && kind == E_Constant)
578 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
579 if (TREE_CODE (gnu_size) != INTEGER_CST
580 && contains_placeholder_p (gnu_size))
582 tree gnu_temp = gnu_expr;
584 /* Strip off any conversions in GNU_EXPR since
585 they can't be changing the size to allocate. */
586 while (TREE_CODE (gnu_temp) == UNCHECKED_CONVERT_EXPR)
587 gnu_temp = TREE_OPERAND (gnu_temp, 0);
589 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_temp));
590 if (TREE_CODE (gnu_size) != INTEGER_CST
591 && contains_placeholder_p (gnu_size))
592 gnu_size = build (WITH_RECORD_EXPR, bitsizetype,
597 /* We may have no GNU_EXPR because No_Initialization is
598 set even though there's an Expression. */
599 else if (kind == E_Constant
600 && (Nkind (Declaration_Node (gnat_entity))
601 == N_Object_Declaration)
602 && Present (Expression (Declaration_Node (gnat_entity))))
604 = TYPE_SIZE (gnat_to_gnu_type
606 (Expression (Declaration_Node (gnat_entity)))));
608 gnu_size = max_size (TYPE_SIZE (gnu_type), 1);
611 /* If the size is zero bytes, make it one byte since some linkers
612 have trouble with zero-sized objects. But if this will have a
613 template, that will make it nonzero. */
614 if (((gnu_size != 0 && integer_zerop (gnu_size))
615 || (TYPE_SIZE (gnu_type) != 0
616 && integer_zerop (TYPE_SIZE (gnu_type))))
617 && (! Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
618 || ! Is_Array_Type (Etype (gnat_entity))))
619 gnu_size = bitsize_unit_node;
621 /* If an alignment is specified, use it if valid. Note that
622 exceptions are objects but don't have alignments. */
623 if (kind != E_Exception && Known_Alignment (gnat_entity))
625 if (No (Alignment (gnat_entity)))
629 = validate_alignment (Alignment (gnat_entity), gnat_entity,
630 TYPE_ALIGN (gnu_type));
633 /* If this is an atomic object with no specified size and alignment,
634 but where the size of the type is a constant, set the alignment to
635 the lowest power of two greater than the size, or to the
636 biggest meaningful alignment, whichever is smaller. */
638 if (Is_Atomic (gnat_entity) && gnu_size == 0 && align == 0
639 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
641 if (! host_integerp (TYPE_SIZE (gnu_type), 1)
642 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
644 align = BIGGEST_ALIGNMENT;
646 align = ((unsigned int) 1
647 << (floor_log2 (tree_low_cst
648 (TYPE_SIZE (gnu_type), 1) - 1)
652 #ifdef MINIMUM_ATOMIC_ALIGNMENT
653 /* If the size is a constant and no alignment is specified, force
654 the alignment to be the minimum valid atomic alignment. The
655 restriction on constant size avoids problems with variable-size
656 temporaries; if the size is variable, there's no issue with
657 atomic access. Also don't do this for a constant, since it isn't
658 necessary and can interfere with constant replacement. Finally,
659 do not do it for Out parameters since that creates an
660 size inconsistency with In parameters. */
661 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
662 && ! FLOAT_TYPE_P (gnu_type)
663 && ! const_flag && No (Renamed_Object (gnat_entity))
664 && ! imported_p && No (Address_Clause (gnat_entity))
665 && kind != E_Out_Parameter
666 && (gnu_size != 0 ? TREE_CODE (gnu_size) == INTEGER_CST
667 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
668 align = MINIMUM_ATOMIC_ALIGNMENT;
671 /* If the object is set to have atomic components, find the component
672 type and validate it.
674 ??? Note that we ignore Has_Volatile_Components on objects; it's
675 not at all clear what to do in that case. */
677 if (Has_Atomic_Components (gnat_entity))
680 = (TREE_CODE (gnu_type) == ARRAY_TYPE
681 ? TREE_TYPE (gnu_type) : gnu_type);
683 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
684 && TYPE_MULTI_ARRAY_P (gnu_inner))
685 gnu_inner = TREE_TYPE (gnu_inner);
687 check_ok_for_atomic (gnu_inner, gnat_entity, 1);
690 /* Make a new type with the desired size and alignment, if needed. */
691 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
692 gnat_entity, "PAD", 0, definition, 1);
694 /* Make a volatile version of this object's type if we are to
695 make the object volatile. Note that 13.3(19) says that we
696 should treat other types of objects as volatile as well. */
697 if ((Is_Volatile (gnat_entity)
698 || Is_Exported (gnat_entity)
699 || Is_Imported (gnat_entity)
700 || Present (Address_Clause (gnat_entity)))
701 && ! TYPE_VOLATILE (gnu_type))
702 gnu_type = build_qualified_type (gnu_type,
703 (TYPE_QUALS (gnu_type)
704 | TYPE_QUAL_VOLATILE));
706 /* If this is an aliased object with an unconstrained nominal subtype,
707 make a type that includes the template. */
708 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
709 && Is_Array_Type (Etype (gnat_entity))
710 && ! type_annotate_only)
713 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
715 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
718 = build_unc_object_type (gnu_temp_type, gnu_type,
719 concat_id_with_name (gnu_entity_id,
724 /* Convert the expression to the type of the object except in the
725 case where the object's type is unconstrained or the object's type
726 is a padded record whose field is of self-referential size. In
727 the former case, converting will generate unnecessary evaluations
728 of the CONSTRUCTOR to compute the size and in the latter case, we
729 want to only copy the actual data. */
731 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
732 && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
733 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
734 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
735 && TYPE_IS_PADDING_P (gnu_type)
736 && (contains_placeholder_p
737 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
738 gnu_expr = convert (gnu_type, gnu_expr);
740 /* See if this is a renaming. If this is a constant renaming,
741 treat it as a normal variable whose initial value is what
742 is being renamed. We cannot do this if the type is
743 unconstrained or class-wide.
745 Otherwise, if what we are renaming is a reference, we can simply
746 return a stabilized version of that reference, after forcing
747 any SAVE_EXPRs to be evaluated. But, if this is at global level,
748 we can only do this if we know no SAVE_EXPRs will be made.
749 Otherwise, make this into a constant pointer to the object we are
752 if (Present (Renamed_Object (gnat_entity)))
754 /* If the renamed object had padding, strip off the reference
755 to the inner object and reset our type. */
756 if (TREE_CODE (gnu_expr) == COMPONENT_REF
757 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
759 && (TYPE_IS_PADDING_P
760 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
762 gnu_expr = TREE_OPERAND (gnu_expr, 0);
763 gnu_type = TREE_TYPE (gnu_expr);
767 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
768 && TYPE_MODE (gnu_type) != BLKmode
769 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
770 && !Is_Array_Type (Etype (gnat_entity)))
773 /* If this is a declaration or reference, we can just use that
774 declaration or reference as this entity. */
775 else if ((DECL_P (gnu_expr)
776 || TREE_CODE_CLASS (TREE_CODE (gnu_expr)) == 'r')
777 && ! Materialize_Entity (gnat_entity)
778 && (! global_bindings_p ()
779 || (staticp (gnu_expr)
780 && ! TREE_SIDE_EFFECTS (gnu_expr))))
782 set_lineno (gnat_entity, ! global_bindings_p ());
783 gnu_decl = gnat_stabilize_reference (gnu_expr, 1);
784 save_gnu_tree (gnat_entity, gnu_decl, 1);
787 if (! global_bindings_p ())
788 expand_expr_stmt (build1 (CONVERT_EXPR, void_type_node,
794 inner_const_flag = TREE_READONLY (gnu_expr);
796 gnu_type = build_reference_type (gnu_type);
797 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
803 /* If this is an aliased object whose nominal subtype is unconstrained,
804 the object is a record that contains both the template and
805 the object. If there is an initializer, it will have already
806 been converted to the right type, but we need to create the
807 template if there is no initializer. */
808 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
809 && TYPE_CONTAINS_TEMPLATE_P (gnu_type)
815 (TYPE_FIELDS (gnu_type),
817 (TREE_TYPE (TYPE_FIELDS (gnu_type)),
818 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))),
822 /* If this is a pointer and it does not have an initializing
823 expression, initialize it to NULL. */
825 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
827 gnu_expr = integer_zero_node;
829 /* If we are defining the object and it has an Address clause we must
830 get the address expression from the saved GCC tree for the
831 object if the object has a Freeze_Node. Otherwise, we elaborate
832 the address expression here since the front-end has guaranteed
833 in that case that the elaboration has no effects. Note that
834 only the latter mechanism is currently in use. */
835 if (definition && Present (Address_Clause (gnat_entity)))
838 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
839 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
841 save_gnu_tree (gnat_entity, NULL_TREE, 0);
843 /* Ignore the size. It's either meaningless or was handled
846 gnu_type = build_reference_type (gnu_type);
847 gnu_address = convert (gnu_type, gnu_address);
849 const_flag = ! Is_Public (gnat_entity);
851 /* If we don't have an initializing expression for the underlying
852 variable, the initializing expression for the pointer is the
853 specified address. Otherwise, we have to make a COMPOUND_EXPR
854 to assign both the address and the initial value. */
856 gnu_expr = gnu_address;
859 = build (COMPOUND_EXPR, gnu_type,
861 (MODIFY_EXPR, NULL_TREE,
862 build_unary_op (INDIRECT_REF, NULL_TREE,
868 /* If it has an address clause and we are not defining it, mark it
869 as an indirect object. Likewise for Stdcall objects that are
871 if ((! definition && Present (Address_Clause (gnat_entity)))
872 || (Is_Imported (gnat_entity)
873 && Convention (gnat_entity) == Convention_Stdcall))
875 gnu_type = build_reference_type (gnu_type);
880 /* If we are at top level and this object is of variable size,
881 make the actual type a hidden pointer to the real type and
882 make the initializer be a memory allocation and initialization.
883 Likewise for objects we aren't defining (presumed to be
884 external references from other packages), but there we do
885 not set up an initialization.
887 If the object's size overflows, make an allocator too, so that
888 Storage_Error gets raised. Note that we will never free
889 such memory, so we presume it never will get allocated. */
891 if (! allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
892 global_bindings_p () || ! definition
895 && ! allocatable_size_p (gnu_size,
896 global_bindings_p () || ! definition
899 gnu_type = build_reference_type (gnu_type);
904 /* Get the data part of GNU_EXPR in case this was a
905 aliased object whose nominal subtype is unconstrained.
906 In that case the pointer above will be a thin pointer and
907 build_allocator will automatically make the template and
908 constructor already made above. */
912 tree gnu_alloc_type = TREE_TYPE (gnu_type);
914 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
915 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
918 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
920 = build_component_ref
921 (gnu_expr, NULL_TREE,
922 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))));
925 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
926 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
927 && ! Is_Imported (gnat_entity))
928 post_error ("Storage_Error will be raised at run-time?",
931 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
941 /* If this object would go into the stack and has an alignment
942 larger than the default largest alignment, make a variable
943 to hold the "aligning type" with a modified initial value,
944 if any, then point to it and make that the value of this
945 variable, which is now indirect. */
947 if (! global_bindings_p () && ! static_p && definition
948 && ! imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
951 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
952 TYPE_SIZE_UNIT (gnu_type));
957 = build_constructor (gnu_new_type,
958 tree_cons (TYPE_FIELDS (gnu_new_type),
959 gnu_expr, NULL_TREE));
960 set_lineno (gnat_entity, 1);
962 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
963 NULL_TREE, gnu_new_type, gnu_expr,
966 gnu_type = build_reference_type (gnu_type);
969 (ADDR_EXPR, gnu_type,
970 build_component_ref (gnu_new_var, NULL_TREE,
971 TYPE_FIELDS (gnu_new_type)));
978 /* Convert the expression to the type of the object except in the
979 case where the object's type is unconstrained or the object's type
980 is a padded record whose field is of self-referential size. In
981 the former case, converting will generate unnecessary evaluations
982 of the CONSTRUCTOR to compute the size and in the latter case, we
983 want to only copy the actual data. */
985 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
986 && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
987 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
988 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
989 && TYPE_IS_PADDING_P (gnu_type)
990 && (contains_placeholder_p
991 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
992 gnu_expr = convert (gnu_type, gnu_expr);
994 /* This name is external or there was a name specified, use it.
995 Don't use the Interface_Name if there is an address clause.
997 if ((Present (Interface_Name (gnat_entity))
998 && No (Address_Clause (gnat_entity)))
999 || (Is_Public (gnat_entity)
1000 && (! Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1001 gnu_ext_name = create_concat_name (gnat_entity, 0);
1004 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1005 | TYPE_QUAL_CONST));
1007 /* If this is constant initialized to a static constant and the
1008 object has an aggregrate type, force it to be statically
1010 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1011 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1012 && (AGGREGATE_TYPE_P (gnu_type)
1013 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1014 && TYPE_IS_PADDING_P (gnu_type))))
1017 set_lineno (gnat_entity, ! global_bindings_p ());
1018 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1019 gnu_expr, const_flag,
1020 Is_Public (gnat_entity),
1021 imported_p || !definition,
1022 static_p, attr_list);
1024 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1025 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1027 if (definition && DECL_SIZE (gnu_decl) != 0
1028 && gnu_block_stack != 0
1029 && TREE_VALUE (gnu_block_stack) != 0
1030 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1031 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1032 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1033 STACK_CHECK_MAX_VAR_SIZE))))
1034 update_setjmp_buf (TREE_VALUE (gnu_block_stack));
1036 /* If this is a public constant or we're not optimizing and we're not
1037 making a VAR_DECL for it, make one just for export or debugger
1038 use. Likewise if the address is taken or if the object or type is
1040 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1041 && (Is_Public (gnat_entity)
1043 || Address_Taken (gnat_entity)
1044 || Is_Aliased (gnat_entity)
1045 || Is_Aliased (Etype (gnat_entity))))
1046 DECL_CONST_CORRESPONDING_VAR (gnu_decl)
1047 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1048 gnu_expr, 0, Is_Public (gnat_entity), 0,
1051 if (Is_Atomic (gnat_entity))
1052 check_ok_for_atomic (gnu_decl, gnat_entity, 0);
1054 /* If this is declared in a block that contains an block with an
1055 exception handler, we must force this variable in memory to
1056 suppress an invalid optimization. */
1057 if (Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1059 mark_addressable (gnu_decl);
1060 flush_addressof (gnu_decl);
1063 /* Back-annotate the Alignment of the object if not already in the
1064 tree. Likewise for Esize if the object is of a constant size. */
1065 if (Unknown_Alignment (gnat_entity))
1066 Set_Alignment (gnat_entity,
1067 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1069 if (Unknown_Esize (gnat_entity)
1070 && DECL_SIZE (gnu_decl) != 0)
1072 tree gnu_back_size = DECL_SIZE (gnu_decl);
1074 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1075 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1077 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1078 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1080 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1086 /* Return a TYPE_DECL for "void" that we previously made. */
1087 gnu_decl = void_type_decl_node;
1090 case E_Enumeration_Type:
1091 /* A special case, for the types Character and Wide_Character in
1092 Standard, we do not list all the literals. So if the literals
1093 are not specified, make this an unsigned type. */
1094 if (No (First_Literal (gnat_entity)))
1096 gnu_type = make_unsigned_type (esize);
1100 /* Normal case of non-character type, or non-Standard character type */
1102 /* Here we have a list of enumeral constants in First_Literal.
1103 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1104 the list to be places into TYPE_FIELDS. Each node in the list
1105 is a TREE_LIST node whose TREE_VALUE is the literal name
1106 and whose TREE_PURPOSE is the value of the literal.
1108 Esize contains the number of bits needed to represent the enumeral
1109 type, Type_Low_Bound also points to the first literal and
1110 Type_High_Bound points to the last literal. */
1112 Entity_Id gnat_literal;
1113 tree gnu_literal_list = NULL_TREE;
1115 if (Is_Unsigned_Type (gnat_entity))
1116 gnu_type = make_unsigned_type (esize);
1118 gnu_type = make_signed_type (esize);
1120 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1122 for (gnat_literal = First_Literal (gnat_entity);
1123 Present (gnat_literal);
1124 gnat_literal = Next_Literal (gnat_literal))
1126 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1129 = create_var_decl (get_entity_name (gnat_literal),
1130 0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1132 save_gnu_tree (gnat_literal, gnu_literal, 0);
1133 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1134 gnu_value, gnu_literal_list);
1137 TYPE_FIELDS (gnu_type) = nreverse (gnu_literal_list);
1139 /* Note that the bounds are updated at the end of this function
1140 because to avoid an infinite recursion when we get the bounds of
1141 this type, since those bounds are objects of this type. */
1145 case E_Signed_Integer_Type:
1146 case E_Ordinary_Fixed_Point_Type:
1147 case E_Decimal_Fixed_Point_Type:
1148 /* For integer types, just make a signed type the appropriate number
1150 gnu_type = make_signed_type (esize);
1153 case E_Modular_Integer_Type:
1154 /* For modular types, make the unsigned type of the proper number of
1155 bits and then set up the modulus, if required. */
1157 enum machine_mode mode;
1161 if (Is_Packed_Array_Type (gnat_entity))
1162 esize = UI_To_Int (RM_Size (gnat_entity));
1164 /* Find the smallest mode at least ESIZE bits wide and make a class
1167 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1168 GET_MODE_BITSIZE (mode) < esize;
1169 mode = GET_MODE_WIDER_MODE (mode))
1172 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1173 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1174 = Is_Packed_Array_Type (gnat_entity);
1176 /* Get the modulus in this type. If it overflows, assume it is because
1177 it is equal to 2**Esize. Note that there is no overflow checking
1178 done on unsigned type, so we detect the overflow by looking for
1179 a modulus of zero, which is otherwise invalid. */
1180 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1182 if (! integer_zerop (gnu_modulus))
1184 TYPE_MODULAR_P (gnu_type) = 1;
1185 TYPE_MODULUS (gnu_type) = gnu_modulus;
1186 gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1187 convert (gnu_type, integer_one_node)));
1190 /* If we have to set TYPE_PRECISION different from its natural value,
1191 make a subtype to do do. Likewise if there is a modulus and
1192 it is not one greater than TYPE_MAX_VALUE. */
1193 if (TYPE_PRECISION (gnu_type) != esize
1194 || (TYPE_MODULAR_P (gnu_type)
1195 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1197 tree gnu_subtype = make_node (INTEGER_TYPE);
1199 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1200 TREE_TYPE (gnu_subtype) = gnu_type;
1201 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1202 TYPE_MAX_VALUE (gnu_subtype)
1203 = TYPE_MODULAR_P (gnu_type)
1204 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1205 TYPE_PRECISION (gnu_subtype) = esize;
1206 TREE_UNSIGNED (gnu_subtype) = 1;
1207 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1208 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1209 = Is_Packed_Array_Type (gnat_entity);
1210 layout_type (gnu_subtype);
1212 gnu_type = gnu_subtype;
1217 case E_Signed_Integer_Subtype:
1218 case E_Enumeration_Subtype:
1219 case E_Modular_Integer_Subtype:
1220 case E_Ordinary_Fixed_Point_Subtype:
1221 case E_Decimal_Fixed_Point_Subtype:
1223 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1224 that we do not want to call build_range_type since we would
1225 like each subtype node to be distinct. This will be important
1226 when memory aliasing is implemented.
1228 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1229 parent type; this fact is used by the arithmetic conversion
1232 We elaborate the Ancestor_Subtype if it is not in the current
1233 unit and one of our bounds is non-static. We do this to ensure
1234 consistent naming in the case where several subtypes share the same
1235 bounds by always elaborating the first such subtype first, thus
1239 && Present (Ancestor_Subtype (gnat_entity))
1240 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1241 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1242 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1243 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1244 gnu_expr, definition);
1246 gnu_type = make_node (INTEGER_TYPE);
1247 if (Is_Packed_Array_Type (gnat_entity))
1250 esize = UI_To_Int (RM_Size (gnat_entity));
1251 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1254 TYPE_PRECISION (gnu_type) = esize;
1255 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1257 TYPE_MIN_VALUE (gnu_type)
1258 = convert (TREE_TYPE (gnu_type),
1259 elaborate_expression (Type_Low_Bound (gnat_entity),
1261 get_identifier ("L"), definition, 1,
1262 Needs_Debug_Info (gnat_entity)));
1264 TYPE_MAX_VALUE (gnu_type)
1265 = convert (TREE_TYPE (gnu_type),
1266 elaborate_expression (Type_High_Bound (gnat_entity),
1268 get_identifier ("U"), definition, 1,
1269 Needs_Debug_Info (gnat_entity)));
1271 /* One of the above calls might have caused us to be elaborated,
1272 so don't blow up if so. */
1273 if (present_gnu_tree (gnat_entity))
1279 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1280 = Has_Biased_Representation (gnat_entity);
1282 /* This should be an unsigned type if the lower bound is constant
1283 and non-negative or if the base type is unsigned; a signed type
1285 TREE_UNSIGNED (gnu_type)
1286 = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1287 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1288 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1289 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1290 || Is_Unsigned_Type (gnat_entity));
1292 layout_type (gnu_type);
1294 if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1296 tree gnu_field_type = gnu_type;
1299 TYPE_RM_SIZE_INT (gnu_field_type)
1300 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1301 gnu_type = make_node (RECORD_TYPE);
1302 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1303 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1304 TYPE_PACKED (gnu_type) = 1;
1305 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1306 gnu_field_type, gnu_type, 1, 0, 0, 1),
1307 finish_record_type (gnu_type, gnu_field, 0, 0);
1308 TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1309 TYPE_ADA_SIZE (gnu_type) = bitsize_int (esize);
1314 case E_Floating_Point_Type:
1315 /* If this is a VAX floating-point type, use an integer of the proper
1316 size. All the operations will be handled with ASM statements. */
1317 if (Vax_Float (gnat_entity))
1319 gnu_type = make_signed_type (esize);
1320 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1321 TYPE_DIGITS_VALUE (gnu_type)
1322 = UI_To_Int (Digits_Value (gnat_entity));
1326 /* The type of the Low and High bounds can be our type if this is
1327 a type from Standard, so set them at the end of the function. */
1328 gnu_type = make_node (REAL_TYPE);
1329 TYPE_PRECISION (gnu_type) = esize;
1330 layout_type (gnu_type);
1333 case E_Floating_Point_Subtype:
1334 if (Vax_Float (gnat_entity))
1336 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1341 enum machine_mode mode;
1344 && Present (Ancestor_Subtype (gnat_entity))
1345 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1346 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1347 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1348 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1349 gnu_expr, definition);
1351 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1352 (GET_MODE_WIDER_MODE (mode) != VOIDmode
1353 && GET_MODE_BITSIZE (GET_MODE_WIDER_MODE (mode)) <= esize);
1354 mode = GET_MODE_WIDER_MODE (mode))
1357 gnu_type = make_node (REAL_TYPE);
1358 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1359 TYPE_PRECISION (gnu_type) = GET_MODE_BITSIZE (mode);
1361 TYPE_MIN_VALUE (gnu_type)
1362 = convert (TREE_TYPE (gnu_type),
1363 elaborate_expression (Type_Low_Bound (gnat_entity),
1364 gnat_entity, get_identifier ("L"),
1366 Needs_Debug_Info (gnat_entity)));
1368 TYPE_MAX_VALUE (gnu_type)
1369 = convert (TREE_TYPE (gnu_type),
1370 elaborate_expression (Type_High_Bound (gnat_entity),
1371 gnat_entity, get_identifier ("U"),
1373 Needs_Debug_Info (gnat_entity)));
1375 /* One of the above calls might have caused us to be elaborated,
1376 so don't blow up if so. */
1377 if (present_gnu_tree (gnat_entity))
1383 layout_type (gnu_type);
1387 /* Array and String Types and Subtypes
1389 Unconstrained array types are represented by E_Array_Type and
1390 constrained array types are represented by E_Array_Subtype. There
1391 are no actual objects of an unconstrained array type; all we have
1392 are pointers to that type.
1394 The following fields are defined on array types and subtypes:
1396 Component_Type Component type of the array.
1397 Number_Dimensions Number of dimensions (an int).
1398 First_Index Type of first index. */
1403 tree gnu_template_fields = NULL_TREE;
1404 tree gnu_template_type = make_node (RECORD_TYPE);
1405 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1406 tree gnu_fat_type = make_node (RECORD_TYPE);
1407 int ndim = Number_Dimensions (gnat_entity);
1409 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1411 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1412 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1413 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1414 tree gnu_comp_size = 0;
1415 tree gnu_max_size = size_one_node;
1416 tree gnu_max_size_unit;
1418 Entity_Id gnat_ind_subtype;
1419 Entity_Id gnat_ind_base_subtype;
1420 tree gnu_template_reference;
1423 TYPE_NAME (gnu_template_type)
1424 = create_concat_name (gnat_entity, "XUB");
1425 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1426 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1427 TREE_READONLY (gnu_template_type) = 1;
1429 /* Make a node for the array. If we are not defining the array
1430 suppress expanding incomplete types and save the node as the type
1432 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1435 defer_incomplete_level++;
1436 this_deferred = this_made_decl = 1;
1437 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1438 ! Comes_From_Source (gnat_entity),
1440 save_gnu_tree (gnat_entity, gnu_decl, 0);
1444 /* Build the fat pointer type. Use a "void *" object instead of
1445 a pointer to the array type since we don't have the array type
1446 yet (it will reference the fat pointer via the bounds). */
1447 tem = chainon (chainon (NULL_TREE,
1448 create_field_decl (get_identifier ("P_ARRAY"),
1450 gnu_fat_type, 0, 0, 0, 0)),
1451 create_field_decl (get_identifier ("P_BOUNDS"),
1453 gnu_fat_type, 0, 0, 0, 0));
1455 /* Make sure we can put this into a register. */
1456 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1457 finish_record_type (gnu_fat_type, tem, 0, 1);
1459 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1460 is the fat pointer. This will be used to access the individual
1461 fields once we build them. */
1462 tem = build (COMPONENT_REF, gnu_ptr_template,
1463 build (PLACEHOLDER_EXPR, gnu_fat_type),
1464 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1465 gnu_template_reference
1466 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1467 TREE_READONLY (gnu_template_reference) = 1;
1469 /* Now create the GCC type for each index and add the fields for
1470 that index to the template. */
1471 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1472 gnat_ind_base_subtype
1473 = First_Index (Implementation_Base_Type (gnat_entity));
1474 index < ndim && index >= 0;
1476 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1477 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1479 char field_name[10];
1480 tree gnu_ind_subtype
1481 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1482 tree gnu_base_subtype
1483 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1485 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1487 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1488 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1490 /* Make the FIELD_DECLs for the minimum and maximum of this
1491 type and then make extractions of that field from the
1493 set_lineno (gnat_entity, 0);
1494 sprintf (field_name, "LB%d", index);
1495 gnu_min_field = create_field_decl (get_identifier (field_name),
1497 gnu_template_type, 0, 0, 0, 0);
1498 field_name[0] = 'U';
1499 gnu_max_field = create_field_decl (get_identifier (field_name),
1501 gnu_template_type, 0, 0, 0, 0);
1503 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1505 /* We can't use build_component_ref here since the template
1506 type isn't complete yet. */
1507 gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1508 gnu_template_reference, gnu_min_field);
1509 gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1510 gnu_template_reference, gnu_max_field);
1511 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1513 /* Make a range type with the new ranges, but using
1514 the Ada subtype. Then we convert to sizetype. */
1515 gnu_index_types[index]
1516 = create_index_type (convert (sizetype, gnu_min),
1517 convert (sizetype, gnu_max),
1518 build_range_type (gnu_ind_subtype,
1520 /* Update the maximum size of the array, in elements. */
1522 = size_binop (MULT_EXPR, gnu_max_size,
1523 size_binop (PLUS_EXPR, size_one_node,
1524 size_binop (MINUS_EXPR, gnu_base_max,
1528 TYPE_NAME (gnu_index_types[index])
1529 = create_concat_name (gnat_entity, field_name);
1532 for (index = 0; index < ndim; index++)
1534 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1536 /* Install all the fields into the template. */
1537 finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1538 TREE_READONLY (gnu_template_type) = 1;
1540 /* Now make the array of arrays and update the pointer to the array
1541 in the fat pointer. Note that it is the first field. */
1543 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1545 /* Get and validate any specified Component_Size, but if Packed,
1546 ignore it since the front end will have taken care of it. Also,
1547 allow sizes not a multiple of Storage_Unit if packed. */
1549 = validate_size (Component_Size (gnat_entity), tem,
1551 (Is_Bit_Packed_Array (gnat_entity)
1552 ? TYPE_DECL : VAR_DECL), 1,
1553 Has_Component_Size_Clause (gnat_entity));
1555 if (Has_Atomic_Components (gnat_entity))
1556 check_ok_for_atomic (tem, gnat_entity, 1);
1558 /* If the component type is a RECORD_TYPE that has a self-referential
1559 size, use the maxium size. */
1560 if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
1561 && TREE_CODE (TYPE_SIZE (tem)) != INTEGER_CST
1562 && contains_placeholder_p (TYPE_SIZE (tem)))
1563 gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1565 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1567 tem = make_type_from_size (tem, gnu_comp_size, 0);
1568 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1569 "C_PAD", 0, definition, 1);
1572 if (Has_Volatile_Components (gnat_entity))
1573 tem = build_qualified_type (tem,
1574 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1576 /* If Component_Size is not already specified, annotate it with the
1577 size of the component. */
1578 if (Unknown_Component_Size (gnat_entity))
1579 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1581 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1582 size_binop (MULT_EXPR, gnu_max_size,
1583 TYPE_SIZE_UNIT (tem)));
1584 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1585 size_binop (MULT_EXPR,
1586 convert (bitsizetype,
1590 for (index = ndim - 1; index >= 0; index--)
1592 tem = build_array_type (tem, gnu_index_types[index]);
1593 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1594 TYPE_NONALIASED_COMPONENT (tem)
1595 = ! Has_Aliased_Components (gnat_entity);
1598 /* If an alignment is specified, use it if valid. But ignore it for
1599 types that represent the unpacked base type for packed arrays. */
1600 if (No (Packed_Array_Type (gnat_entity))
1601 && Known_Alignment (gnat_entity))
1603 if (No (Alignment (gnat_entity)))
1607 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1611 TYPE_CONVENTION_FORTRAN_P (tem)
1612 = (Convention (gnat_entity) == Convention_Fortran);
1613 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1615 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1616 corresponding fat pointer. */
1617 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1618 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1619 TYPE_MODE (gnu_type) = BLKmode;
1620 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1621 TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type) = gnu_type;
1623 /* If the maximum size doesn't overflow, use it. */
1624 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1625 && ! TREE_OVERFLOW (gnu_max_size))
1628 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1629 TYPE_SIZE_UNIT (tem)
1630 = size_binop (MIN_EXPR, gnu_max_size_unit,
1631 TYPE_SIZE_UNIT (tem));
1634 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1635 tem, 0, ! Comes_From_Source (gnat_entity),
1637 rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1640 /* Create a record type for the object and its template and
1641 set the template at a negative offset. */
1642 tem = build_unc_object_type (gnu_template_type, tem,
1643 create_concat_name (gnat_entity, "XUT"));
1644 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1645 = size_binop (MINUS_EXPR, size_zero_node,
1646 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1647 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1648 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1649 = bitsize_zero_node;
1650 TYPE_UNCONSTRAINED_ARRAY (tem) = gnu_type;
1651 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1653 /* Give the thin pointer type a name. */
1654 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1655 build_pointer_type (tem), 0,
1656 ! Comes_From_Source (gnat_entity), debug_info_p);
1660 case E_String_Subtype:
1661 case E_Array_Subtype:
1663 /* This is the actual data type for array variables. Multidimensional
1664 arrays are implemented in the gnu tree as arrays of arrays. Note
1665 that for the moment arrays which have sparse enumeration subtypes as
1666 index components create sparse arrays, which is obviously space
1667 inefficient but so much easier to code for now.
1669 Also note that the subtype never refers to the unconstrained
1670 array type, which is somewhat at variance with Ada semantics.
1672 First check to see if this is simply a renaming of the array
1673 type. If so, the result is the array type. */
1675 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1676 if (! Is_Constrained (gnat_entity))
1681 int array_dim = Number_Dimensions (gnat_entity);
1683 = ((Convention (gnat_entity) == Convention_Fortran)
1684 ? array_dim - 1 : 0);
1686 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1687 Entity_Id gnat_ind_subtype;
1688 Entity_Id gnat_ind_base_subtype;
1689 tree gnu_base_type = gnu_type;
1690 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1691 tree gnu_comp_size = 0;
1692 tree gnu_max_size = size_one_node;
1693 tree gnu_max_size_unit;
1694 int need_index_type_struct = 0;
1695 int max_overflow = 0;
1697 /* First create the gnu types for each index. Create types for
1698 debugging information to point to the index types if the
1699 are not integer types, have variable bounds, or are
1700 wider than sizetype. */
1702 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1703 gnat_ind_base_subtype
1704 = First_Index (Implementation_Base_Type (gnat_entity));
1705 index < array_dim && index >= 0;
1707 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1708 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1710 tree gnu_index_subtype
1711 = get_unpadded_type (Etype (gnat_ind_subtype));
1713 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1715 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1716 tree gnu_base_subtype
1717 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1719 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1721 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1722 tree gnu_base_type = get_base_type (gnu_base_subtype);
1723 tree gnu_base_base_min
1724 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1725 tree gnu_base_base_max
1726 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1730 /* If the minimum and maximum values both overflow in
1731 SIZETYPE, but the difference in the original type
1732 does not overflow in SIZETYPE, ignore the overflow
1734 if ((TYPE_PRECISION (gnu_index_subtype)
1735 > TYPE_PRECISION (sizetype))
1736 && TREE_CODE (gnu_min) == INTEGER_CST
1737 && TREE_CODE (gnu_max) == INTEGER_CST
1738 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1740 (fold (build (MINUS_EXPR, gnu_index_subtype,
1741 TYPE_MAX_VALUE (gnu_index_subtype),
1742 TYPE_MIN_VALUE (gnu_index_subtype))))))
1743 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1744 = TREE_CONSTANT_OVERFLOW (gnu_min)
1745 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1747 /* Similarly, if the range is null, use bounds of 1..0 for
1748 the sizetype bounds. */
1749 else if ((TYPE_PRECISION (gnu_index_subtype)
1750 > TYPE_PRECISION (sizetype))
1751 && TREE_CODE (gnu_min) == INTEGER_CST
1752 && TREE_CODE (gnu_max) == INTEGER_CST
1753 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1754 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1755 TYPE_MIN_VALUE (gnu_index_subtype)))
1756 gnu_min = size_one_node, gnu_max = size_zero_node;
1758 /* Now compute the size of this bound. We need to provide
1759 GCC with an upper bound to use but have to deal with the
1760 "superflat" case. There are three ways to do this. If we
1761 can prove that the array can never be superflat, we can
1762 just use the high bound of the index subtype. If we can
1763 prove that the low bound minus one can't overflow, we
1764 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1765 the expression hb >= lb ? hb : lb - 1. */
1766 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1768 /* See if the base array type is already flat. If it is, we
1769 are probably compiling an ACVC test, but it will cause the
1770 code below to malfunction if we don't handle it specially. */
1771 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1772 && TREE_CODE (gnu_base_max) == INTEGER_CST
1773 && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1774 && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1775 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1776 gnu_high = size_zero_node, gnu_min = size_one_node;
1778 /* If gnu_high is now an integer which overflowed, the array
1779 cannot be superflat. */
1780 else if (TREE_CODE (gnu_high) == INTEGER_CST
1781 && TREE_OVERFLOW (gnu_high))
1783 else if (TREE_UNSIGNED (gnu_base_subtype)
1784 || TREE_CODE (gnu_high) == INTEGER_CST)
1785 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1789 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1793 gnu_index_type[index]
1794 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1796 /* Also compute the maximum size of the array. Here we
1797 see if any constraint on the index type of the base type
1798 can be used in the case of self-referential bound on
1799 the index type of the subtype. We look for a non-"infinite"
1800 and non-self-referential bound from any type involved and
1801 handle each bound separately. */
1803 if ((TREE_CODE (gnu_min) == INTEGER_CST
1804 && ! TREE_OVERFLOW (gnu_min)
1805 && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
1806 || (TREE_CODE (gnu_min) != INTEGER_CST
1807 && ! contains_placeholder_p (gnu_min)))
1808 gnu_base_min = gnu_min;
1810 if ((TREE_CODE (gnu_max) == INTEGER_CST
1811 && ! TREE_OVERFLOW (gnu_max)
1812 && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
1813 || (TREE_CODE (gnu_max) != INTEGER_CST
1814 && ! contains_placeholder_p (gnu_max)))
1815 gnu_base_max = gnu_max;
1817 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1818 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1819 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1820 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1821 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1822 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1825 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1826 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1829 = size_binop (MAX_EXPR,
1830 size_binop (PLUS_EXPR, size_one_node,
1831 size_binop (MINUS_EXPR, gnu_base_max,
1835 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1836 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1840 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1842 if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1843 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1845 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1846 || (TREE_TYPE (gnu_index_subtype) != 0
1847 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1849 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1850 || (TYPE_PRECISION (gnu_index_subtype)
1851 > TYPE_PRECISION (sizetype)))
1852 need_index_type_struct = 1;
1855 /* Then flatten: create the array of arrays. */
1857 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1859 /* One of the above calls might have caused us to be elaborated,
1860 so don't blow up if so. */
1861 if (present_gnu_tree (gnat_entity))
1867 /* Get and validate any specified Component_Size, but if Packed,
1868 ignore it since the front end will have taken care of it. Also,
1869 allow sizes not a multiple of Storage_Unit if packed. */
1871 = validate_size (Component_Size (gnat_entity), gnu_type,
1873 (Is_Bit_Packed_Array (gnat_entity)
1874 ? TYPE_DECL : VAR_DECL),
1875 1, Has_Component_Size_Clause (gnat_entity));
1877 /* If the component type is a RECORD_TYPE that has a self-referential
1878 size, use the maxium size. */
1879 if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
1880 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1881 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
1882 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1884 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1886 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1887 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1888 gnat_entity, "C_PAD", 0,
1892 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1893 gnu_type = build_qualified_type (gnu_type,
1894 (TYPE_QUALS (gnu_type)
1895 | TYPE_QUAL_VOLATILE));
1897 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1898 TYPE_SIZE_UNIT (gnu_type));
1899 gnu_max_size = size_binop (MULT_EXPR,
1900 convert (bitsizetype, gnu_max_size),
1901 TYPE_SIZE (gnu_type));
1903 /* We don't want any array types shared for two reasons: first,
1904 we want to keep differently-named types distinct; second,
1905 setting TYPE_MULTI_ARRAY_TYPE of one type can clobber
1907 debug_no_type_hash = 1;
1908 for (index = array_dim - 1; index >= 0; index --)
1910 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1911 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1912 TYPE_NONALIASED_COMPONENT (gnu_type)
1913 = ! Has_Aliased_Components (gnat_entity);
1916 /* If we are at file level and this is a multi-dimensional array, we
1917 need to make a variable corresponding to the stride of the
1918 inner dimensions. */
1919 if (global_bindings_p () && array_dim > 1)
1921 tree gnu_str_name = get_identifier ("ST");
1924 for (gnu_arr_type = TREE_TYPE (gnu_type);
1925 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1926 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1927 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1929 TYPE_SIZE (gnu_arr_type)
1930 = elaborate_expression_1 (gnat_entity, gnat_entity,
1931 TYPE_SIZE (gnu_arr_type),
1932 gnu_str_name, definition, 0);
1933 TYPE_SIZE_UNIT (gnu_arr_type)
1934 = elaborate_expression_1
1935 (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1936 concat_id_with_name (gnu_str_name, "U"), definition, 0);
1940 /* If we need to write out a record type giving the names of
1941 the bounds, do it now. */
1942 if (need_index_type_struct && debug_info_p)
1944 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1945 tree gnu_field_list = 0;
1948 TYPE_NAME (gnu_bound_rec_type)
1949 = create_concat_name (gnat_entity, "XA");
1951 for (index = array_dim - 1; index >= 0; index--)
1954 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1956 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1957 gnu_type_name = DECL_NAME (gnu_type_name);
1959 gnu_field = create_field_decl (gnu_type_name,
1962 0, NULL_TREE, NULL_TREE, 0);
1963 TREE_CHAIN (gnu_field) = gnu_field_list;
1964 gnu_field_list = gnu_field;
1967 finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
1970 debug_no_type_hash = 0;
1971 TYPE_CONVENTION_FORTRAN_P (gnu_type)
1972 = (Convention (gnat_entity) == Convention_Fortran);
1974 /* If our size depends on a placeholder and the maximum size doesn't
1975 overflow, use it. */
1976 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1977 && contains_placeholder_p (TYPE_SIZE (gnu_type))
1978 && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
1979 && TREE_OVERFLOW (gnu_max_size))
1982 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
1983 TYPE_SIZE (gnu_type));
1984 TYPE_SIZE_UNIT (gnu_type)
1985 = size_binop (MIN_EXPR, gnu_max_size_unit,
1986 TYPE_SIZE_UNIT (gnu_type));
1989 /* Set our alias set to that of our base type. This gives all
1990 array subtypes the same alias set. */
1991 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
1992 record_component_aliases (gnu_type);
1995 /* If this is a packed type, make this type the same as the packed
1996 array type, but do some adjusting in the type first. */
1998 if (Present (Packed_Array_Type (gnat_entity)))
2000 Entity_Id gnat_index;
2001 tree gnu_inner_type;
2003 /* First finish the type we had been making so that we output
2004 debugging information for it */
2005 gnu_type = build_qualified_type (gnu_type,
2006 (TYPE_QUALS (gnu_type)
2007 | (TYPE_QUAL_VOLATILE
2008 * Is_Volatile (gnat_entity))));
2009 set_lineno (gnat_entity, 0);
2010 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2011 ! Comes_From_Source (gnat_entity),
2013 if (! Comes_From_Source (gnat_entity))
2014 DECL_ARTIFICIAL (gnu_decl) = 1;
2016 /* Save it as our equivalent in case the call below elaborates
2018 save_gnu_tree (gnat_entity, gnu_decl, 0);
2020 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2023 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2024 save_gnu_tree (gnat_entity, NULL_TREE, 0);
2026 if (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2027 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2028 || TYPE_IS_PADDING_P (gnu_inner_type)))
2029 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2031 /* We need to point the type we just made to our index type so
2032 the actual bounds can be put into a template. */
2034 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2035 && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2036 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2037 && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2039 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2041 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2042 If it is, we need to make another type. */
2043 if (TYPE_MODULAR_P (gnu_inner_type))
2047 gnu_subtype = make_node (INTEGER_TYPE);
2049 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2050 TYPE_MIN_VALUE (gnu_subtype)
2051 = TYPE_MIN_VALUE (gnu_inner_type);
2052 TYPE_MAX_VALUE (gnu_subtype)
2053 = TYPE_MAX_VALUE (gnu_inner_type);
2054 TYPE_PRECISION (gnu_subtype)
2055 = TYPE_PRECISION (gnu_inner_type);
2056 TREE_UNSIGNED (gnu_subtype)
2057 = TREE_UNSIGNED (gnu_inner_type);
2058 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2059 layout_type (gnu_subtype);
2061 gnu_inner_type = gnu_subtype;
2064 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2067 TYPE_ACTUAL_BOUNDS (gnu_inner_type) = NULL_TREE;
2069 for (gnat_index = First_Index (gnat_entity);
2070 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2071 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2072 = tree_cons (NULL_TREE,
2073 get_unpadded_type (Etype (gnat_index)),
2074 TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2076 if (Convention (gnat_entity) != Convention_Fortran)
2077 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2078 = nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2080 if (TREE_CODE (gnu_type) == RECORD_TYPE
2081 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2082 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2086 /* Abort if packed array with no packed array type field set. */
2087 else if (Is_Packed (gnat_entity))
2092 case E_String_Literal_Subtype:
2093 /* Create the type for a string literal. */
2095 Entity_Id gnat_full_type
2096 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2097 && Present (Full_View (Etype (gnat_entity)))
2098 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2099 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2100 tree gnu_string_array_type
2101 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2102 tree gnu_string_index_type
2103 = TREE_TYPE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_string_array_type)));
2104 tree gnu_lower_bound
2105 = convert (gnu_string_index_type,
2106 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2107 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2108 tree gnu_length = ssize_int (length - 1);
2109 tree gnu_upper_bound
2110 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2112 convert (gnu_string_index_type, gnu_length));
2114 = build_range_type (gnu_string_index_type,
2115 gnu_lower_bound, gnu_upper_bound);
2117 = create_index_type (convert (sizetype,
2118 TYPE_MIN_VALUE (gnu_range_type)),
2120 TYPE_MAX_VALUE (gnu_range_type)),
2124 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2129 /* Record Types and Subtypes
2131 The following fields are defined on record types:
2133 Has_Discriminants True if the record has discriminants
2134 First_Discriminant Points to head of list of discriminants
2135 First_Entity Points to head of list of fields
2136 Is_Tagged_Type True if the record is tagged
2138 Implementation of Ada records and discriminated records:
2140 A record type definition is transformed into the equivalent of a C
2141 struct definition. The fields that are the discriminants which are
2142 found in the Full_Type_Declaration node and the elements of the
2143 Component_List found in the Record_Type_Definition node. The
2144 Component_List can be a recursive structure since each Variant of
2145 the Variant_Part of the Component_List has a Component_List.
2147 Processing of a record type definition comprises starting the list of
2148 field declarations here from the discriminants and the calling the
2149 function components_to_record to add the rest of the fields from the
2150 component list and return the gnu type node. The function
2151 components_to_record will call itself recursively as it traverses
2156 if (Has_Complex_Representation (gnat_entity))
2159 = build_complex_type
2161 (Etype (Defining_Entity
2162 (First (Component_Items
2165 (Declaration_Node (gnat_entity)))))))));
2167 /* ??? For now, don't use Complex if the real type is shorter than
2169 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (gnu_type)))
2176 Node_Id full_definition = Declaration_Node (gnat_entity);
2177 Node_Id record_definition = Type_Definition (full_definition);
2178 Entity_Id gnat_field;
2180 tree gnu_field_list = NULL_TREE;
2181 tree gnu_get_parent;
2182 int packed = (Is_Packed (gnat_entity) ? 1
2183 : (Component_Alignment (gnat_entity)
2184 == Calign_Storage_Unit) ? -1
2186 int has_rep = Has_Specified_Layout (gnat_entity);
2187 int all_rep = has_rep;
2189 = (Is_Tagged_Type (gnat_entity)
2190 && Nkind (record_definition) == N_Derived_Type_Definition);
2192 /* See if all fields have a rep clause. Stop when we find one
2194 for (gnat_field = First_Entity (gnat_entity);
2195 Present (gnat_field) && all_rep;
2196 gnat_field = Next_Entity (gnat_field))
2197 if ((Ekind (gnat_field) == E_Component
2198 || Ekind (gnat_field) == E_Discriminant)
2199 && No (Component_Clause (gnat_field)))
2202 /* If this is a record extension, go a level further to find the
2203 record definition. Also, verify we have a Parent_Subtype. */
2206 if (! type_annotate_only
2207 || Present (Record_Extension_Part (record_definition)))
2208 record_definition = Record_Extension_Part (record_definition);
2210 if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2214 /* Make a node for the record. If we are not defining the record,
2215 suppress expanding incomplete types and save the node as the type
2216 for GNAT_ENTITY. We use the same RECORD_TYPE as was made
2217 for a dummy type and then show it's no longer a dummy. */
2218 gnu_type = make_dummy_type (gnat_entity);
2219 TYPE_DUMMY_P (gnu_type) = 0;
2220 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2221 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2223 TYPE_ALIGN (gnu_type) = 0;
2224 TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2228 defer_incomplete_level++;
2230 set_lineno (gnat_entity, 0);
2231 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2232 ! Comes_From_Source (gnat_entity),
2234 save_gnu_tree (gnat_entity, gnu_decl, 0);
2235 this_made_decl = saved = 1;
2238 /* If both a size and rep clause was specified, put the size in
2239 the record type now so that it can get the proper mode. */
2240 if (has_rep && Known_Esize (gnat_entity))
2241 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2243 /* Always set the alignment here so that it can be used to
2244 set the mode, if it is making the alignment stricter. If
2245 it is invalid, it will be checked again below. If this is to
2246 be Atomic, choose a default alignment of a word. */
2248 if (Known_Alignment (gnat_entity))
2249 TYPE_ALIGN (gnu_type)
2250 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2251 else if (Is_Atomic (gnat_entity))
2252 TYPE_ALIGN (gnu_type) = BITS_PER_WORD;
2254 /* If we have a Parent_Subtype, make a field for the parent. If
2255 this record has rep clauses, force the position to zero. */
2256 if (Present (Parent_Subtype (gnat_entity)))
2260 /* A major complexity here is that the parent subtype will
2261 reference our discriminants. But those must reference
2262 the parent component of this record. So here we will
2263 initialize each of those components to a COMPONENT_REF.
2264 The first operand of that COMPONENT_REF is another
2265 COMPONENT_REF which will be filled in below, once
2266 the parent type can be safely built. */
2268 gnu_get_parent = build (COMPONENT_REF, void_type_node,
2269 build (PLACEHOLDER_EXPR, gnu_type),
2270 build_decl (FIELD_DECL, NULL_TREE,
2273 if (Has_Discriminants (gnat_entity))
2274 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2275 Present (gnat_field);
2276 gnat_field = Next_Girder_Discriminant (gnat_field))
2277 if (Present (Corresponding_Discriminant (gnat_field)))
2280 build (COMPONENT_REF,
2281 get_unpadded_type (Etype (gnat_field)),
2283 gnat_to_gnu_entity (Corresponding_Discriminant
2288 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2291 = create_field_decl (get_identifier
2292 (Get_Name_String (Name_uParent)),
2293 gnu_parent, gnu_type, 0,
2294 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2295 has_rep ? bitsize_zero_node : 0, 1);
2296 DECL_INTERNAL_P (gnu_field_list) = 1;
2298 TREE_TYPE (gnu_get_parent) = gnu_parent;
2299 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2302 /* Add the fields for the discriminants into the record. */
2303 if (! Is_Unchecked_Union (gnat_entity)
2304 && Has_Discriminants (gnat_entity))
2305 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2306 Present (gnat_field);
2307 gnat_field = Next_Girder_Discriminant (gnat_field))
2309 /* If this is a record extension and this discriminant
2310 is the renaming of another discriminant, we've already
2311 handled the discriminant above. */
2312 if (Present (Parent_Subtype (gnat_entity))
2313 && Present (Corresponding_Discriminant (gnat_field)))
2317 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2319 /* Make an expression using a PLACEHOLDER_EXPR from the
2320 FIELD_DECL node just created and link that with the
2321 corresponding GNAT defining identifier. Then add to the
2323 save_gnu_tree (gnat_field,
2324 build (COMPONENT_REF, TREE_TYPE (gnu_field),
2325 build (PLACEHOLDER_EXPR,
2326 DECL_CONTEXT (gnu_field)),
2330 TREE_CHAIN (gnu_field) = gnu_field_list;
2331 gnu_field_list = gnu_field;
2334 /* Put the discriminants into the record (backwards), so we can
2335 know the appropriate discriminant to use for the names of the
2337 TYPE_FIELDS (gnu_type) = gnu_field_list;
2339 /* Add the listed fields into the record and finish up. */
2340 components_to_record (gnu_type, Component_List (record_definition),
2341 gnu_field_list, packed, definition, 0,
2344 TYPE_DUMMY_P (gnu_type) = 0;
2345 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2346 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2348 /* If this is an extension type, reset the tree for any
2349 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2350 for non-inherited discriminants. */
2351 if (! Is_Unchecked_Union (gnat_entity)
2352 && Has_Discriminants (gnat_entity))
2353 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2354 Present (gnat_field);
2355 gnat_field = Next_Girder_Discriminant (gnat_field))
2357 if (Present (Parent_Subtype (gnat_entity))
2358 && Present (Corresponding_Discriminant (gnat_field)))
2359 save_gnu_tree (gnat_field, NULL_TREE, 0);
2362 gnu_field = get_gnu_tree (gnat_field);
2363 save_gnu_tree (gnat_field, NULL_TREE, 0);
2364 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2368 /* If it is a tagged record force the type to BLKmode to insure
2369 that these objects will always be placed in memory. Do the
2370 same thing for limited record types. */
2372 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2373 TYPE_MODE (gnu_type) = BLKmode;
2375 /* Fill in locations of fields. */
2376 annotate_rep (gnat_entity, gnu_type);
2378 /* If there are any entities in the chain corresponding to
2379 components that we did not elaborate, ensure we elaborate their
2380 types if they are Itypes. */
2381 for (gnat_temp = First_Entity (gnat_entity);
2382 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2383 if ((Ekind (gnat_temp) == E_Component
2384 || Ekind (gnat_temp) == E_Discriminant)
2385 && Is_Itype (Etype (gnat_temp))
2386 && ! present_gnu_tree (gnat_temp))
2387 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2391 case E_Class_Wide_Subtype:
2392 /* If an equivalent type is present, that is what we should use.
2393 Otherwise, fall through to handle this like a record subtype
2394 since it may have constraints. */
2396 if (Present (Equivalent_Type (gnat_entity)))
2398 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2403 /* ... fall through ... */
2405 case E_Record_Subtype:
2407 /* If Cloned_Subtype is Present it means this record subtype has
2408 identical layout to that type or subtype and we should use
2409 that GCC type for this one. The front end guarantees that
2410 the component list is shared. */
2411 if (Present (Cloned_Subtype (gnat_entity)))
2413 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2418 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2419 changing the type, make a new type with each field having the
2420 type of the field in the new subtype but having the position
2421 computed by transforming every discriminant reference according
2422 to the constraints. We don't see any difference between
2423 private and nonprivate type here since derivations from types should
2424 have been deferred until the completion of the private type. */
2427 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2432 defer_incomplete_level++, this_deferred = 1;
2434 /* Get the base type initially for its alignment and sizes. But
2435 if it is a padded type, we do all the other work with the
2437 gnu_type = gnu_orig_type = gnu_base_type
2438 = gnat_to_gnu_type (gnat_base_type);
2440 if (TREE_CODE (gnu_type) == RECORD_TYPE
2441 && TYPE_IS_PADDING_P (gnu_type))
2442 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2444 if (present_gnu_tree (gnat_entity))
2450 /* When the type has discriminants, and these discriminants
2451 affect the shape of what it built, factor them in.
2453 If we are making a subtype of an Unchecked_Union (must be an
2454 Itype), just return the type.
2456 We can't just use Is_Constrained because private subtypes without
2457 discriminants of full types with discriminants with default
2458 expressions are Is_Constrained but aren't constrained! */
2460 if (IN (Ekind (gnat_base_type), Record_Kind)
2461 && ! Is_For_Access_Subtype (gnat_entity)
2462 && ! Is_Unchecked_Union (gnat_base_type)
2463 && Is_Constrained (gnat_entity)
2464 && Girder_Constraint (gnat_entity) != No_Elist
2465 && Present (Discriminant_Constraint (gnat_entity)))
2467 Entity_Id gnat_field;
2468 Entity_Id gnat_root_type;
2469 tree gnu_field_list = 0;
2471 = compute_field_positions (gnu_orig_type, NULL_TREE,
2472 size_zero_node, bitsize_zero_node);
2474 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2478 /* If this is a derived type, we may be seeing fields from any
2479 original records, so add those positions and discriminant
2480 substitutions to our lists. */
2481 for (gnat_root_type = gnat_base_type;
2482 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2483 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2486 = compute_field_positions
2487 (gnat_to_gnu_type (Etype (gnat_root_type)),
2488 gnu_pos_list, size_zero_node, bitsize_zero_node);
2490 if (Present (Parent_Subtype (gnat_root_type)))
2492 = substitution_list (Parent_Subtype (gnat_root_type),
2493 Empty, gnu_subst_list, definition);
2496 gnu_type = make_node (RECORD_TYPE);
2497 TYPE_NAME (gnu_type) = gnu_entity_id;
2498 TYPE_STUB_DECL (gnu_type)
2499 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2500 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2502 for (gnat_field = First_Entity (gnat_entity);
2503 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2504 if (Ekind (gnat_field) == E_Component
2505 || Ekind (gnat_field) == E_Discriminant)
2508 = gnat_to_gnu_entity
2509 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2511 = TREE_VALUE (purpose_member (gnu_old_field,
2513 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2514 tree gnu_bitpos = TREE_VALUE (gnu_offset);
2516 = gnat_to_gnu_type (Etype (gnat_field));
2517 tree gnu_size = TYPE_SIZE (gnu_field_type);
2518 tree gnu_new_pos = 0;
2521 /* If there was a component clause, the field types must be
2522 the same for the type and subtype, so copy the data from
2523 the old field to avoid recomputation here. */
2524 if (Present (Component_Clause
2525 (Original_Record_Component (gnat_field))))
2527 gnu_size = DECL_SIZE (gnu_old_field);
2528 gnu_field_type = TREE_TYPE (gnu_old_field);
2531 /* If this was a bitfield, get the size from the old field.
2532 Also ensure the type can be placed into a bitfield. */
2533 else if (DECL_BIT_FIELD (gnu_old_field))
2535 gnu_size = DECL_SIZE (gnu_old_field);
2536 if (TYPE_MODE (gnu_field_type) == BLKmode
2537 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2538 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2539 gnu_field_type = make_packable_type (gnu_field_type);
2542 if (TREE_CODE (gnu_pos) != INTEGER_CST
2543 && contains_placeholder_p (gnu_pos))
2544 for (gnu_temp = gnu_subst_list;
2545 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2546 gnu_pos = substitute_in_expr (gnu_pos,
2547 TREE_PURPOSE (gnu_temp),
2548 TREE_VALUE (gnu_temp));
2550 /* If the size is now a constant, we can set it as the
2551 size of the field when we make it. Otherwise, we need
2552 to deal with it specially. */
2553 if (TREE_CONSTANT (gnu_pos))
2554 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2558 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2559 0, gnu_size, gnu_new_pos,
2560 ! DECL_NONADDRESSABLE_P (gnu_old_field));
2562 if (! TREE_CONSTANT (gnu_pos))
2564 normalize_offset (&gnu_pos, &gnu_bitpos,
2565 DECL_OFFSET_ALIGN (gnu_old_field));
2566 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2567 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2568 SET_DECL_OFFSET_ALIGN
2569 (gnu_field, DECL_OFFSET_ALIGN (gnu_old_field));
2570 DECL_SIZE (gnu_field) = gnu_size;
2571 DECL_SIZE_UNIT (gnu_field)
2572 = convert (sizetype,
2573 size_binop (CEIL_DIV_EXPR, gnu_size,
2574 bitsize_unit_node));
2575 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2578 DECL_INTERNAL_P (gnu_field)
2579 = DECL_INTERNAL_P (gnu_old_field);
2580 DECL_ORIGINAL_FIELD (gnu_field)
2581 = DECL_ORIGINAL_FIELD (gnu_old_field) != 0
2582 ? DECL_ORIGINAL_FIELD (gnu_old_field) : gnu_old_field;
2583 DECL_DISCRIMINANT_NUMBER (gnu_field)
2584 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2585 TREE_THIS_VOLATILE (gnu_field)
2586 = TREE_THIS_VOLATILE (gnu_old_field);
2587 TREE_CHAIN (gnu_field) = gnu_field_list;
2588 gnu_field_list = gnu_field;
2589 save_gnu_tree (gnat_field, gnu_field, 0);
2592 finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2594 /* Now set the size, alignment and alias set of the new type to
2595 match that of the old one, doing any substitutions, as
2597 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2598 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2599 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2600 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2601 TYPE_ADA_SIZE (gnu_type) = TYPE_ADA_SIZE (gnu_base_type);
2603 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
2604 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
2605 for (gnu_temp = gnu_subst_list;
2606 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2607 TYPE_SIZE (gnu_type)
2608 = substitute_in_expr (TYPE_SIZE (gnu_type),
2609 TREE_PURPOSE (gnu_temp),
2610 TREE_VALUE (gnu_temp));
2612 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_type)) != INTEGER_CST
2613 && contains_placeholder_p (TYPE_SIZE_UNIT (gnu_type)))
2614 for (gnu_temp = gnu_subst_list;
2615 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2616 TYPE_SIZE_UNIT (gnu_type)
2617 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2618 TREE_PURPOSE (gnu_temp),
2619 TREE_VALUE (gnu_temp));
2621 if (TYPE_ADA_SIZE (gnu_type) != 0
2622 && TREE_CODE (TYPE_ADA_SIZE (gnu_type)) != INTEGER_CST
2623 && contains_placeholder_p (TYPE_ADA_SIZE (gnu_type)))
2624 for (gnu_temp = gnu_subst_list;
2625 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2626 TYPE_ADA_SIZE (gnu_type)
2627 = substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2628 TREE_PURPOSE (gnu_temp),
2629 TREE_VALUE (gnu_temp));
2631 /* Recompute the mode of this record type now that we know its
2633 compute_record_mode (gnu_type);
2635 /* Fill in locations of fields. */
2636 annotate_rep (gnat_entity, gnu_type);
2639 /* If we've made a new type, record it and make an XVS type to show
2640 what this is a subtype of. Some debuggers require the XVS
2641 type to be output first, so do it in that order. */
2642 if (gnu_type != gnu_orig_type)
2646 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2647 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2649 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2650 gnu_orig_name = DECL_NAME (gnu_orig_name);
2652 TYPE_NAME (gnu_subtype_marker)
2653 = create_concat_name (gnat_entity, "XVS");
2654 finish_record_type (gnu_subtype_marker,
2655 create_field_decl (gnu_orig_name,
2663 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2664 TYPE_NAME (gnu_type) = gnu_entity_id;
2665 TYPE_STUB_DECL (gnu_type)
2666 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2668 DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2669 DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2670 rest_of_type_compilation (gnu_type, global_bindings_p ());
2673 /* Otherwise, go down all the components in the new type and
2674 make them equivalent to those in the base type. */
2676 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2677 gnat_temp = Next_Entity (gnat_temp))
2678 if ((Ekind (gnat_temp) == E_Discriminant
2679 && ! Is_Unchecked_Union (gnat_base_type))
2680 || Ekind (gnat_temp) == E_Component)
2681 save_gnu_tree (gnat_temp,
2683 (Original_Record_Component (gnat_temp)), 0);
2687 case E_Access_Subprogram_Type:
2688 /* If we are not defining this entity, and we have incomplete
2689 entities being processed above us, make a dummy type and
2690 fill it in later. */
2691 if (! definition && defer_incomplete_level != 0)
2693 struct incomplete *p
2694 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2697 = build_pointer_type
2698 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2699 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2700 ! Comes_From_Source (gnat_entity),
2702 save_gnu_tree (gnat_entity, gnu_decl, 0);
2703 this_made_decl = saved = 1;
2705 p->old_type = TREE_TYPE (gnu_type);
2706 p->full_type = Directly_Designated_Type (gnat_entity);
2707 p->next = defer_incomplete_list;
2708 defer_incomplete_list = p;
2712 /* ... fall through ... */
2714 case E_Allocator_Type:
2716 case E_Access_Attribute_Type:
2717 case E_Anonymous_Access_Type:
2718 case E_General_Access_Type:
2720 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2721 Entity_Id gnat_desig_full
2722 = ((IN (Ekind (Etype (gnat_desig_type)),
2723 Incomplete_Or_Private_Kind))
2724 ? Full_View (gnat_desig_type) : 0);
2725 /* We want to know if we'll be seeing the freeze node for any
2726 incomplete type we may be pointing to. */
2728 = (Present (gnat_desig_full)
2729 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2730 : In_Extended_Main_Code_Unit (gnat_desig_type));
2734 if (No (gnat_desig_full)
2735 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2736 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2737 && Present (Equivalent_Type (gnat_desig_type)))))
2739 if (Present (Equivalent_Type (gnat_desig_type)))
2741 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2742 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2743 gnat_desig_full = Full_View (gnat_desig_full);
2745 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2746 Incomplete_Or_Private_Kind))
2747 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2750 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2751 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2753 /* If either the designated type or its full view is an
2754 unconstrained array subtype, replace it with the type it's a
2755 subtype of. This avoids problems with multiple copies of
2756 unconstrained array types. */
2757 if (Ekind (gnat_desig_type) == E_Array_Subtype
2758 && ! Is_Constrained (gnat_desig_type))
2759 gnat_desig_type = Etype (gnat_desig_type);
2760 if (Present (gnat_desig_full)
2761 && Ekind (gnat_desig_full) == E_Array_Subtype
2762 && ! Is_Constrained (gnat_desig_full))
2763 gnat_desig_full = Etype (gnat_desig_full);
2765 /* If we are pointing to an incomplete type whose completion is an
2766 unconstrained array, make a fat pointer type instead of a pointer
2767 to VOID. The two types in our fields will be pointers to VOID and
2768 will be replaced in update_pointer_to. Similiarly, if the type
2769 itself is a dummy type or an unconstrained array. Also make
2770 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2773 if ((Present (gnat_desig_full)
2774 && Is_Array_Type (gnat_desig_full)
2775 && ! Is_Constrained (gnat_desig_full))
2776 || (present_gnu_tree (gnat_desig_type)
2777 && TYPE_IS_DUMMY_P (TREE_TYPE
2778 (get_gnu_tree (gnat_desig_type)))
2779 && Is_Array_Type (gnat_desig_type)
2780 && ! Is_Constrained (gnat_desig_type))
2781 || (present_gnu_tree (gnat_desig_type)
2782 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2783 == UNCONSTRAINED_ARRAY_TYPE)
2784 && (TYPE_POINTER_TO (TREE_TYPE
2785 (get_gnu_tree (gnat_desig_type)))
2787 || (No (gnat_desig_full) && ! in_main_unit
2788 && defer_incomplete_level != 0
2789 && ! present_gnu_tree (gnat_desig_type)
2790 && Is_Array_Type (gnat_desig_type)
2791 && ! Is_Constrained (gnat_desig_type)))
2794 = (present_gnu_tree (gnat_desig_type)
2795 ? gnat_to_gnu_type (gnat_desig_type)
2796 : make_dummy_type (gnat_desig_type));
2799 /* Show the dummy we get will be a fat pointer. */
2800 got_fat_p = made_dummy = 1;
2802 /* If the call above got something that has a pointer, that
2803 pointer is our type. This could have happened either
2804 because the type was elaborated or because somebody
2805 else executed the code below. */
2806 gnu_type = TYPE_POINTER_TO (gnu_old);
2809 gnu_type = make_node (RECORD_TYPE);
2810 TYPE_UNCONSTRAINED_ARRAY (gnu_type) = gnu_old;
2811 TYPE_POINTER_TO (gnu_old) = gnu_type;
2813 set_lineno (gnat_entity, 0);
2815 = chainon (chainon (NULL_TREE,
2817 (get_identifier ("P_ARRAY"),
2818 ptr_void_type_node, gnu_type,
2820 create_field_decl (get_identifier ("P_BOUNDS"),
2822 gnu_type, 0, 0, 0, 0));
2824 /* Make sure we can place this into a register. */
2825 TYPE_ALIGN (gnu_type)
2826 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2827 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2828 finish_record_type (gnu_type, fields, 0, 1);
2830 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2831 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2832 = concat_id_with_name (get_entity_name (gnat_desig_type),
2834 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2838 /* If we already know what the full type is, use it. */
2839 else if (Present (gnat_desig_full)
2840 && present_gnu_tree (gnat_desig_full))
2842 = build_pointer_type (TREE_TYPE (get_gnu_tree (gnat_desig_full)));
2844 /* Get the type of the thing we are to point to and build a pointer
2845 to it. If it is a reference to an incomplete or private type with a
2846 full view that is a record, make a dummy type node and get the
2847 actual type later when we have verified it is safe. */
2848 else if (! in_main_unit
2849 && ! present_gnu_tree (gnat_desig_type)
2850 && Present (gnat_desig_full)
2851 && ! present_gnu_tree (gnat_desig_full)
2852 && Is_Record_Type (gnat_desig_full))
2854 gnu_type = build_pointer_type (make_dummy_type (gnat_desig_type));
2858 /* Likewise if we are pointing to a record or array and we are to defer
2859 elaborating incomplete types. We do this since this access type
2860 may be the full view of some private type. Note that the
2861 unconstrained array case is handled above. */
2862 else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2863 && ! present_gnu_tree (gnat_desig_type)
2864 && ((Is_Record_Type (gnat_desig_type)
2865 || Is_Array_Type (gnat_desig_type))
2866 || (Present (gnat_desig_full)
2867 && (Is_Record_Type (gnat_desig_full)
2868 || Is_Array_Type (gnat_desig_full)))))
2870 gnu_type = build_pointer_type (make_dummy_type (gnat_desig_type));
2873 else if (gnat_desig_type == gnat_entity)
2875 gnu_type = build_pointer_type (make_node (VOID_TYPE));
2876 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2879 gnu_type = build_pointer_type (gnat_to_gnu_type (gnat_desig_type));
2881 /* It is possible that the above call to gnat_to_gnu_type resolved our
2882 type. If so, just return it. */
2883 if (present_gnu_tree (gnat_entity))
2889 /* If we are not defining this object and we made a dummy pointer,
2890 save our current definition, evaluate the actual type, and replace
2891 the tentative type we made with the actual one. If we are to defer
2892 actually looking up the actual type, make an entry in the
2895 if (! in_main_unit && made_dummy)
2898 = TYPE_FAT_POINTER_P (gnu_type)
2899 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
2901 if (esize == POINTER_SIZE
2902 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
2904 = build_pointer_type
2905 (TYPE_OBJECT_RECORD_TYPE
2906 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
2908 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2909 ! Comes_From_Source (gnat_entity),
2911 save_gnu_tree (gnat_entity, gnu_decl, 0);
2912 this_made_decl = saved = 1;
2914 if (defer_incomplete_level == 0)
2916 (gnu_old_type, gnat_to_gnu_type (gnat_desig_type));
2919 struct incomplete *p
2920 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2922 p->old_type = gnu_old_type;
2923 p->full_type = gnat_desig_type;
2924 p->next = defer_incomplete_list;
2925 defer_incomplete_list = p;
2931 case E_Access_Protected_Subprogram_Type:
2932 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
2933 gnu_type = build_pointer_type (void_type_node);
2935 /* The runtime representation is the equivalent type. */
2936 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2938 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2939 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2940 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
2941 && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
2942 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2947 case E_Access_Subtype:
2949 /* We treat this as identical to its base type; any constraint is
2950 meaningful only to the front end.
2952 The designated type must be elaborated as well, if it does
2953 not have its own freeze node. Designated (sub)types created
2954 for constrained components of records with discriminants are
2955 not frozen by the front end and thus not elaborated by gigi,
2956 because their use may appear before the base type is frozen,
2957 and because it is not clear that they are needed anywhere in
2958 Gigi. With the current model, there is no correct place where
2959 they could be elaborated. */
2961 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2962 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2963 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2964 && Is_Frozen (Directly_Designated_Type (gnat_entity))
2965 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
2967 /* If we are not defining this entity, and we have incomplete
2968 entities being processed above us, make a dummy type and
2969 elaborate it later. */
2970 if (! definition && defer_incomplete_level != 0)
2972 struct incomplete *p
2973 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2975 = build_pointer_type
2976 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2978 p->old_type = TREE_TYPE (gnu_ptr_type);
2979 p->full_type = Directly_Designated_Type (gnat_entity);
2980 p->next = defer_incomplete_list;
2981 defer_incomplete_list = p;
2984 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2991 /* Subprogram Entities
2993 The following access functions are defined for subprograms (functions
2996 First_Formal The first formal parameter.
2997 Is_Imported Indicates that the subprogram has appeared in
2998 an INTERFACE or IMPORT pragma. For now we
2999 assume that the external language is C.
3000 Is_Inlined True if the subprogram is to be inlined.
3002 In addition for function subprograms we have:
3004 Etype Return type of the function.
3006 Each parameter is first checked by calling must_pass_by_ref on its
3007 type to determine if it is passed by reference. For parameters which
3008 are copied in, if they are Ada IN OUT or OUT parameters, their return
3009 value becomes part of a record which becomes the return type of the
3010 function (C function - note that this applies only to Ada procedures
3011 so there is no Ada return type). Additional code to store back the
3012 parameters will be generated on the caller side. This transformation
3013 is done here, not in the front-end.
3015 The intended result of the transformation can be seen from the
3016 equivalent source rewritings that follow:
3018 struct temp {int a,b};
3019 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3021 end P; return {A,B};
3031 For subprogram types we need to perform mainly the same conversions to
3032 GCC form that are needed for procedures and function declarations. The
3033 only difference is that at the end, we make a type declaration instead
3034 of a function declaration. */
3036 case E_Subprogram_Type:
3040 /* The first GCC parameter declaration (a PARM_DECL node). The
3041 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3042 actually is the head of this parameter list. */
3043 tree gnu_param_list = NULL_TREE;
3044 /* The type returned by a function. If the subprogram is a procedure
3045 this type should be void_type_node. */
3046 tree gnu_return_type = void_type_node;
3047 /* List of fields in return type of procedure with copy in copy out
3049 tree gnu_field_list = NULL_TREE;
3050 /* Non-null for subprograms containing parameters passed by copy in
3051 copy out (Ada IN OUT or OUT parameters not passed by reference),
3052 in which case it is the list of nodes used to specify the values of
3053 the in out/out parameters that are returned as a record upon
3054 procedure return. The TREE_PURPOSE of an element of this list is
3055 a field of the record and the TREE_VALUE is the PARM_DECL
3056 corresponding to that field. This list will be saved in the
3057 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3058 tree gnu_return_list = NULL_TREE;
3059 Entity_Id gnat_param;
3060 int inline_flag = Is_Inlined (gnat_entity);
3061 int public_flag = Is_Public (gnat_entity);
3063 = (Is_Public (gnat_entity) && !definition) || imported_p;
3064 int pure_flag = Is_Pure (gnat_entity);
3065 int volatile_flag = No_Return (gnat_entity);
3066 int returns_by_ref = 0;
3067 int returns_unconstrained = 0;
3068 tree gnu_ext_name = NULL_TREE;
3069 int has_copy_in_out = 0;
3072 if (kind == E_Subprogram_Type && ! definition)
3073 /* A parameter may refer to this type, so defer completion
3074 of any incomplete types. */
3075 defer_incomplete_level++, this_deferred = 1;
3077 /* If the subprogram has an alias, it is probably inherited, so
3078 we can use the original one. If the original "subprogram"
3079 is actually an enumeration literal, it may be the first use
3080 of its type, so we must elaborate that type now. */
3081 if (Present (Alias (gnat_entity)))
3083 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3084 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3086 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3089 /* Elaborate any Itypes in the parameters of this entity. */
3090 for (gnat_temp = First_Formal (gnat_entity);
3091 Present (gnat_temp);
3092 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3093 if (Is_Itype (Etype (gnat_temp)))
3094 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3099 if (kind == E_Function || kind == E_Subprogram_Type)
3100 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3102 /* If this function returns by reference, make the actual
3103 return type of this function the pointer and mark the decl. */
3104 if (Returns_By_Ref (gnat_entity))
3108 gnu_return_type = build_pointer_type (gnu_return_type);
3111 /* If we are supposed to return an unconstrained array,
3112 actually return a fat pointer and make a note of that. Return
3113 a pointer to an unconstrained record of variable size. */
3114 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3116 gnu_return_type = TREE_TYPE (gnu_return_type);
3117 returns_unconstrained = 1;
3120 /* If the type requires a transient scope, the result is allocated
3121 on the secondary stack, so the result type of the function is
3123 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3125 gnu_return_type = build_pointer_type (gnu_return_type);
3126 returns_unconstrained = 1;
3129 /* If the type is a padded type and the underlying type would not
3130 be passed by reference or this function has a foreign convention,
3131 return the underlying type. */
3132 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3133 && TYPE_IS_PADDING_P (gnu_return_type)
3134 && (! default_pass_by_ref (TREE_TYPE
3135 (TYPE_FIELDS (gnu_return_type)))
3136 || Has_Foreign_Convention (gnat_entity)))
3137 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3139 /* Look at all our parameters and get the type of
3140 each. While doing this, build a copy-out structure if
3143 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3144 Present (gnat_param);
3145 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3147 tree gnu_param_name = get_entity_name (gnat_param);
3148 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3149 tree gnu_param, gnu_field;
3152 int by_component_ptr_p = 0;
3153 int copy_in_copy_out_flag = 0;
3154 int req_by_copy = 0, req_by_ref = 0;
3156 /* See if a Mechanism was supplied that forced this
3157 parameter to be passed one way or another. */
3158 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3160 else if (Mechanism (gnat_param) == Default)
3162 else if (Mechanism (gnat_param) == By_Copy)
3164 else if (Mechanism (gnat_param) == By_Reference)
3166 else if (Mechanism (gnat_param) <= By_Descriptor)
3168 else if (Mechanism (gnat_param) > 0)
3170 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3171 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3172 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3173 Mechanism (gnat_param)))
3179 post_error ("unsupported mechanism for&", gnat_param);
3181 /* If this is either a foreign function or if the
3182 underlying type won't be passed by refererence, strip off
3183 possible padding type. */
3184 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3185 && TYPE_IS_PADDING_P (gnu_param_type)
3186 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3187 || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3188 (gnu_param_type)))))
3189 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3191 /* If this is an IN parameter it is read-only, so make a variant
3192 of the type that is read-only.
3194 ??? However, if this is an unconstrained array, that type can
3195 be very complex. So skip it for now. Likewise for any other
3196 self-referential type. */
3197 if (Ekind (gnat_param) == E_In_Parameter
3198 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3199 && ! (TYPE_SIZE (gnu_param_type) != 0
3200 && TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3201 && contains_placeholder_p (TYPE_SIZE (gnu_param_type))))
3203 = build_qualified_type (gnu_param_type,
3204 (TYPE_QUALS (gnu_param_type)
3205 | TYPE_QUAL_CONST));
3207 /* For foreign conventions, pass arrays as a pointer to the
3208 underlying type. First check for unconstrained array and get
3209 the underlying array. Then get the component type and build
3211 if (Has_Foreign_Convention (gnat_entity)
3212 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3214 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3215 (TREE_TYPE (gnu_param_type))));
3219 = build_pointer_type
3220 (build_vms_descriptor (gnu_param_type,
3221 Mechanism (gnat_param),
3224 else if (Has_Foreign_Convention (gnat_entity)
3226 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3228 /* Strip off any multi-dimensional entries, then strip
3229 off the last array to get the component type. */
3230 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3231 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3232 gnu_param_type = TREE_TYPE (gnu_param_type);
3234 by_component_ptr_p = 1;
3235 gnu_param_type = TREE_TYPE (gnu_param_type);
3237 if (Ekind (gnat_param) == E_In_Parameter)
3239 = build_qualified_type (gnu_param_type,
3240 (TYPE_QUALS (gnu_param_type)
3241 | TYPE_QUAL_CONST));
3243 gnu_param_type = build_pointer_type (gnu_param_type);
3246 /* Fat pointers are passed as thin pointers for foreign
3248 else if (Has_Foreign_Convention (gnat_entity)
3249 && TYPE_FAT_POINTER_P (gnu_param_type))
3251 = make_type_from_size (gnu_param_type,
3252 size_int (POINTER_SIZE), 0);
3254 /* If we must pass or were requested to pass by reference, do so.
3255 If we were requested to pass by copy, do so.
3256 Otherwise, for foreign conventions, pass all in out parameters
3257 or aggregates by reference. For COBOL and Fortran, pass
3258 all integer and FP types that way too. For Convention Ada,
3259 use the standard Ada default. */
3260 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3262 && ((Has_Foreign_Convention (gnat_entity)
3263 && (Ekind (gnat_param) != E_In_Parameter
3264 || AGGREGATE_TYPE_P (gnu_param_type)))
3265 || (((Convention (gnat_entity)
3266 == Convention_Fortran)
3267 || (Convention (gnat_entity)
3268 == Convention_COBOL))
3269 && (INTEGRAL_TYPE_P (gnu_param_type)
3270 || FLOAT_TYPE_P (gnu_param_type)))
3271 /* For convention Ada, see if we pass by reference
3273 || (! Has_Foreign_Convention (gnat_entity)
3274 && default_pass_by_ref (gnu_param_type)))))
3276 gnu_param_type = build_reference_type (gnu_param_type);
3280 else if (Ekind (gnat_param) != E_In_Parameter)
3281 copy_in_copy_out_flag = 1;
3283 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3284 post_error ("?cannot pass & by copy", gnat_param);
3286 /* If this is an OUT parameter that isn't passed by reference
3287 and isn't a pointer or aggregate, we don't make a PARM_DECL
3288 for it. Instead, it will be a VAR_DECL created when we process
3289 the procedure. For the special parameter of Valued_Procedure,
3290 never pass it in. */
3291 if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3292 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3294 && ! POINTER_TYPE_P (gnu_param_type)
3295 && ! AGGREGATE_TYPE_P (gnu_param_type))))
3299 set_lineno (gnat_param, 0);
3302 (gnu_param_name, gnu_param_type,
3303 by_ref_p || by_component_ptr_p
3304 || Ekind (gnat_param) == E_In_Parameter);
3306 DECL_BY_REF_P (gnu_param) = by_ref_p;
3307 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3308 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3309 DECL_POINTS_TO_READONLY_P (gnu_param)
3310 = (Ekind (gnat_param) == E_In_Parameter
3311 && (by_ref_p || by_component_ptr_p));
3312 save_gnu_tree (gnat_param, gnu_param, 0);
3313 gnu_param_list = chainon (gnu_param, gnu_param_list);
3315 /* If a parameter is a pointer, this function may modify
3316 memory through it and thus shouldn't be considered
3317 a pure function. Also, the memory may be modified
3318 between two calls, so they can't be CSE'ed. The latter
3319 case also handles by-ref parameters. */
3320 if (POINTER_TYPE_P (gnu_param_type)
3321 || TYPE_FAT_POINTER_P (gnu_param_type))
3325 if (copy_in_copy_out_flag)
3327 if (! has_copy_in_out)
3329 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3332 gnu_return_type = make_node (RECORD_TYPE);
3333 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3334 has_copy_in_out = 1;
3337 set_lineno (gnat_param, 0);
3338 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3339 gnu_return_type, 0, 0, 0, 0);
3340 TREE_CHAIN (gnu_field) = gnu_field_list;
3341 gnu_field_list = gnu_field;
3342 gnu_return_list = tree_cons (gnu_field, gnu_param,
3347 /* Do not compute record for out parameters if subprogram is
3348 stubbed since structures are incomplete for the back-end. */
3349 if (gnu_field_list != 0
3350 && Convention (gnat_entity) != Convention_Stubbed)
3351 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3354 /* If we have a CICO list but it has only one entry, we convert
3355 this function into a function that simply returns that one
3357 if (list_length (gnu_return_list) == 1)
3358 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3360 if (Convention (gnat_entity) == Convention_Stdcall)
3363 = (struct attrib *) xmalloc (sizeof (struct attrib));
3365 attr->next = attr_list;
3366 attr->type = ATTR_MACHINE_ATTRIBUTE;
3367 attr->name = get_identifier ("stdcall");
3368 attr->arg = NULL_TREE;
3369 attr->error_point = gnat_entity;
3373 /* Both lists ware built in reverse. */
3374 gnu_param_list = nreverse (gnu_param_list);
3375 gnu_return_list = nreverse (gnu_return_list);
3378 = create_subprog_type (gnu_return_type, gnu_param_list,
3379 gnu_return_list, returns_unconstrained,
3381 Function_Returns_With_DSP (gnat_entity));
3383 /* ??? For now, don't consider nested fuctions pure. */
3384 if (! global_bindings_p ())
3388 = build_qualified_type (gnu_type,
3389 (TYPE_QUALS (gnu_type)
3390 | (TYPE_QUAL_CONST * pure_flag)
3391 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3393 /* Top-level or external functions need to have an assembler name.
3394 This is passed to create_subprog_decl through the ext_name argument.
3395 For Pragma Interface subprograms with no Pragma Interface_Name, the
3396 simple name already in entity_name is correct, and this is what is
3397 gotten when ext_name is NULL. If Interface_Name is specified, then
3398 the name is extracted from the N_String_Literal node containing the
3399 string specified in the Pragma. If there is no Pragma Interface,
3400 then the Ada fully qualified name is created. */
3402 if (Present (Interface_Name (gnat_entity))
3403 || ! (Is_Imported (gnat_entity) || Is_Exported (gnat_entity)))
3404 gnu_ext_name = create_concat_name (gnat_entity, 0);
3406 set_lineno (gnat_entity, 0);
3408 /* If we are defining the subprogram and it has an Address clause
3409 we must get the address expression from the saved GCC tree for the
3410 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3411 the address expression here since the front-end has guaranteed
3412 in that case that the elaboration has no effects. If there is
3413 an Address clause and we are not defining the object, just
3414 make it a constant. */
3415 if (Present (Address_Clause (gnat_entity)))
3417 tree gnu_address = 0;
3421 = (present_gnu_tree (gnat_entity)
3422 ? get_gnu_tree (gnat_entity)
3423 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3425 save_gnu_tree (gnat_entity, NULL_TREE, 0);
3427 gnu_type = build_reference_type (gnu_type);
3428 if (gnu_address != 0)
3429 gnu_address = convert (gnu_type, gnu_address);
3432 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3433 gnu_address, 0, Is_Public (gnat_entity),
3435 DECL_BY_REF_P (gnu_decl) = 1;
3438 else if (kind == E_Subprogram_Type)
3439 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3440 ! Comes_From_Source (gnat_entity),
3444 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3445 gnu_type, gnu_param_list,
3446 inline_flag, public_flag,
3447 extern_flag, attr_list);
3448 DECL_STUBBED_P (gnu_decl)
3449 = Convention (gnat_entity) == Convention_Stubbed;
3454 case E_Incomplete_Type:
3455 case E_Private_Type:
3456 case E_Limited_Private_Type:
3457 case E_Record_Type_With_Private:
3458 case E_Private_Subtype:
3459 case E_Limited_Private_Subtype:
3460 case E_Record_Subtype_With_Private:
3462 /* If this type does not have a full view in the unit we are
3463 compiling, then just get the type from its Etype. */
3464 if (No (Full_View (gnat_entity)))
3466 /* If this is an incomplete type with no full view, it must
3467 be a Taft Amendement type, so just return a dummy type. */
3468 if (kind == E_Incomplete_Type)
3469 gnu_type = make_dummy_type (gnat_entity);
3471 else if (Present (Underlying_Full_View (gnat_entity)))
3472 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3476 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3484 /* Otherwise, if we are not defining the type now, get the
3485 type from the full view. But always get the type from the full
3486 view for define on use types, since otherwise we won't see them! */
3488 else if (! definition
3489 || (Is_Itype (Full_View (gnat_entity))
3490 && No (Freeze_Node (gnat_entity)))
3491 || (Is_Itype (gnat_entity)
3492 && No (Freeze_Node (Full_View (gnat_entity)))))
3494 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3500 /* For incomplete types, make a dummy type entry which will be
3502 gnu_type = make_dummy_type (gnat_entity);
3504 /* Save this type as the full declaration's type so we can do any needed
3505 updates when we see it. */
3506 set_lineno (gnat_entity, 0);
3507 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3508 ! Comes_From_Source (gnat_entity),
3510 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3513 /* Simple class_wide types are always viewed as their root_type
3514 by Gigi unless an Equivalent_Type is specified. */
3515 case E_Class_Wide_Type:
3516 if (Present (Equivalent_Type (gnat_entity)))
3517 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3519 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3525 case E_Task_Subtype:
3526 case E_Protected_Type:
3527 case E_Protected_Subtype:
3528 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3529 gnu_type = void_type_node;
3531 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3537 gnu_decl = create_label_decl (gnu_entity_id);
3542 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3543 we've already saved it, so we don't try to. */
3544 gnu_decl = error_mark_node;
3552 /* If we had a case where we evaluated another type and it might have
3553 defined this one, handle it here. */
3554 if (maybe_present && present_gnu_tree (gnat_entity))
3556 gnu_decl = get_gnu_tree (gnat_entity);
3560 /* If we are processing a type and there is either no decl for it or
3561 we just made one, do some common processing for the type, such as
3562 handling alignment and possible padding. */
3564 if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3566 if (Is_Tagged_Type (gnat_entity))
3567 TYPE_ALIGN_OK_P (gnu_type) = 1;
3569 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3570 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3572 /* ??? Don't set the size for a String_Literal since it is either
3573 confirming or we don't handle it properly (if the low bound is
3575 if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3576 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3577 TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3579 /* If a size was specified, see if we can make a new type of that size
3580 by rearranging the type, for example from a fat to a thin pointer. */
3584 = make_type_from_size (gnu_type, gnu_size,
3585 Has_Biased_Representation (gnat_entity));
3587 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3588 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3592 /* If the alignment hasn't already been processed and this is
3593 not an unconstrained array, see if an alignment is specified.
3594 If not, we pick a default alignment for atomic objects. */
3595 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3597 else if (Known_Alignment (gnat_entity))
3598 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3599 TYPE_ALIGN (gnu_type));
3600 else if (Is_Atomic (gnat_entity) && gnu_size == 0
3601 && host_integerp (TYPE_SIZE (gnu_type), 1)
3602 && integer_pow2p (TYPE_SIZE (gnu_type)))
3603 align = MIN (BIGGEST_ALIGNMENT,
3604 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3605 else if (Is_Atomic (gnat_entity) && gnu_size != 0
3606 && host_integerp (gnu_size, 1)
3607 && integer_pow2p (gnu_size))
3608 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3610 /* See if we need to pad the type. If we did, and made a record,
3611 the name of the new type may be changed. So get it back for
3612 us when we make the new TYPE_DECL below. */
3613 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3614 gnat_entity, "PAD", 1, definition, 0);
3615 if (TREE_CODE (gnu_type) == RECORD_TYPE
3616 && TYPE_IS_PADDING_P (gnu_type))
3618 gnu_entity_id = TYPE_NAME (gnu_type);
3619 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3620 gnu_entity_id = DECL_NAME (gnu_entity_id);
3623 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3625 /* If we are at global level, GCC will have applied variable_size to
3626 the type, but that won't have done anything. So, if it's not
3627 a constant or self-referential, call elaborate_expression_1 to
3628 make a variable for the size rather than calculating it each time.
3629 Handle both the RM size and the actual size. */
3630 if (global_bindings_p ()
3631 && TYPE_SIZE (gnu_type) != 0
3632 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
3633 && ! contains_placeholder_p (TYPE_SIZE (gnu_type)))
3635 if (TREE_CODE (gnu_type) == RECORD_TYPE
3636 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3637 TYPE_SIZE (gnu_type), 0))
3638 TYPE_ADA_SIZE (gnu_type) = TYPE_SIZE (gnu_type)
3639 = elaborate_expression_1 (gnat_entity, gnat_entity,
3640 TYPE_SIZE (gnu_type),
3641 get_identifier ("SIZE"),
3643 else if (TREE_CODE (gnu_type) == RECORD_TYPE)
3645 TYPE_ADA_SIZE (gnu_type)
3646 = elaborate_expression_1 (gnat_entity, gnat_entity,
3647 TYPE_ADA_SIZE (gnu_type),
3648 get_identifier ("RM_SIZE"),
3650 TYPE_SIZE (gnu_type)
3651 = elaborate_expression_1 (gnat_entity, gnat_entity,
3652 TYPE_SIZE (gnu_type),
3653 get_identifier ("SIZE"),
3655 TYPE_SIZE_UNIT (gnu_type)
3656 = elaborate_expression_1 (gnat_entity, gnat_entity,
3657 TYPE_SIZE_UNIT (gnu_type),
3658 get_identifier ("SIZE_UNIT"),
3663 TYPE_SIZE (gnu_type)
3664 = elaborate_expression_1 (gnat_entity, gnat_entity,
3665 TYPE_SIZE (gnu_type),
3666 get_identifier ("SIZE"),
3668 TYPE_SIZE_UNIT (gnu_type)
3669 = elaborate_expression_1 (gnat_entity, gnat_entity,
3670 TYPE_SIZE_UNIT (gnu_type),
3671 get_identifier ("SIZE_UNIT"),
3676 /* If this is a record type or subtype, call elaborate_expression_1 on
3677 any field position. Do this for both global and local types.
3678 Skip any fields that we haven't made trees for to avoid problems with
3679 class wide types. */
3680 if (IN (kind, Record_Kind))
3681 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3682 gnat_temp = Next_Entity (gnat_temp))
3683 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3685 tree gnu_field = get_gnu_tree (gnat_temp);
3687 if (TREE_CODE (DECL_FIELD_OFFSET (gnu_field)) != INTEGER_CST
3688 && ! contains_placeholder_p (DECL_FIELD_OFFSET (gnu_field)))
3689 DECL_FIELD_OFFSET (gnu_field)
3690 = elaborate_expression_1 (gnat_temp, gnat_temp,
3691 DECL_FIELD_OFFSET (gnu_field),
3692 get_identifier ("OFFSET"),
3696 gnu_type = build_qualified_type (gnu_type,
3697 (TYPE_QUALS (gnu_type)
3698 | (TYPE_QUAL_VOLATILE
3699 * Is_Volatile (gnat_entity))));
3701 if (Is_Atomic (gnat_entity))
3702 check_ok_for_atomic (gnu_type, gnat_entity, 0);
3704 if (Known_Alignment (gnat_entity))
3705 TYPE_USER_ALIGN (gnu_type) = 1;
3709 set_lineno (gnat_entity, 0);
3710 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3711 ! Comes_From_Source (gnat_entity),
3715 TREE_TYPE (gnu_decl) = gnu_type;
3718 if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3720 gnu_type = TREE_TYPE (gnu_decl);
3722 /* Back-annotate the Alignment of the type if not already in the
3723 tree. Likewise for sizes. */
3724 if (Unknown_Alignment (gnat_entity))
3725 Set_Alignment (gnat_entity,
3726 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3728 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3730 /* If the size is self-referential, we annotate the maximum
3731 value of that size. */
3732 tree gnu_size = TYPE_SIZE (gnu_type);
3734 if (contains_placeholder_p (gnu_size))
3735 gnu_size = max_size (gnu_size, 1);
3737 Set_Esize (gnat_entity, annotate_value (gnu_size));
3740 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3741 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3744 if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3745 DECL_ARTIFICIAL (gnu_decl) = 1;
3747 if (! debug_info_p && DECL_P (gnu_decl)
3748 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3749 DECL_IGNORED_P (gnu_decl) = 1;
3751 /* If this decl is really indirect, adjust it. */
3752 if (TREE_CODE (gnu_decl) == VAR_DECL)
3753 adjust_decl_rtl (gnu_decl);
3755 /* If we haven't already, associate the ..._DECL node that we just made with
3756 the input GNAT entity node. */
3758 save_gnu_tree (gnat_entity, gnu_decl, 0);
3760 /* If this is an enumeral or floating-point type, we were not able to set
3761 the bounds since they refer to the type. These bounds are always static.
3763 For enumeration types, also write debugging information and declare the
3764 enumeration literal table, if needed. */
3766 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3767 || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3769 tree gnu_scalar_type = gnu_type;
3771 /* If this is a padded type, we need to use the underlying type. */
3772 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3773 && TYPE_IS_PADDING_P (gnu_scalar_type))
3774 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3776 /* If this is a floating point type and we haven't set a floating
3777 point type yet, use this in the evaluation of the bounds. */
3778 if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3779 longest_float_type_node = gnu_type;
3781 TYPE_MIN_VALUE (gnu_scalar_type)
3782 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3783 TYPE_MAX_VALUE (gnu_scalar_type)
3784 = gnat_to_gnu (Type_High_Bound (gnat_entity));
3786 if (kind == E_Enumeration_Type)
3788 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3790 /* Since this has both a typedef and a tag, avoid outputting
3792 DECL_ARTIFICIAL (gnu_decl) = 1;
3793 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
3797 /* If we deferred processing of incomplete types, re-enable it. If there
3798 were no other disables and we have some to process, do so. */
3799 if (this_deferred && --defer_incomplete_level == 0
3800 && defer_incomplete_list != 0)
3802 struct incomplete *incp = defer_incomplete_list;
3803 struct incomplete *next;
3805 defer_incomplete_list = 0;
3806 for (; incp; incp = next)
3810 if (incp->old_type != 0)
3811 update_pointer_to (incp->old_type,
3812 gnat_to_gnu_type (incp->full_type));
3817 /* If we are not defining this type, see if it's in the incomplete list.
3818 If so, handle that list entry now. */
3819 else if (! definition)
3821 struct incomplete *incp;
3823 for (incp = defer_incomplete_list; incp; incp = incp->next)
3824 if (incp->old_type != 0 && incp->full_type == gnat_entity)
3826 update_pointer_to (incp->old_type, TREE_TYPE (gnu_decl));
3834 if (Is_Packed_Array_Type (gnat_entity)
3835 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
3836 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
3837 && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
3838 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
3843 /* Given GNAT_ENTITY, elaborate all expressions that are required to
3844 be elaborated at the point of its definition, but do nothing else. */
3847 elaborate_entity (gnat_entity)
3848 Entity_Id gnat_entity;
3850 switch (Ekind (gnat_entity))
3852 case E_Signed_Integer_Subtype:
3853 case E_Modular_Integer_Subtype:
3854 case E_Enumeration_Subtype:
3855 case E_Ordinary_Fixed_Point_Subtype:
3856 case E_Decimal_Fixed_Point_Subtype:
3857 case E_Floating_Point_Subtype:
3859 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
3860 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
3862 /* ??? Tests for avoiding static constaint error expression
3863 is needed until the front stops generating bogus conversions
3864 on bounds of real types. */
3866 if (! Raises_Constraint_Error (gnat_lb))
3867 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
3868 1, 0, Needs_Debug_Info (gnat_entity));
3869 if (! Raises_Constraint_Error (gnat_hb))
3870 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
3871 1, 0, Needs_Debug_Info (gnat_entity));
3877 Node_Id full_definition = Declaration_Node (gnat_entity);
3878 Node_Id record_definition = Type_Definition (full_definition);
3880 /* If this is a record extension, go a level further to find the
3881 record definition. */
3882 if (Nkind (record_definition) == N_Derived_Type_Definition)
3883 record_definition = Record_Extension_Part (record_definition);
3887 case E_Record_Subtype:
3888 case E_Private_Subtype:
3889 case E_Limited_Private_Subtype:
3890 case E_Record_Subtype_With_Private:
3891 if (Is_Constrained (gnat_entity)
3892 && Has_Discriminants (Base_Type (gnat_entity))
3893 && Present (Discriminant_Constraint (gnat_entity)))
3895 Node_Id gnat_discriminant_expr;
3896 Entity_Id gnat_field;
3898 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
3899 gnat_discriminant_expr
3900 = First_Elmt (Discriminant_Constraint (gnat_entity));
3901 Present (gnat_field);
3902 gnat_field = Next_Discriminant (gnat_field),
3903 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
3904 /* ??? For now, ignore access discriminants. */
3905 if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
3906 elaborate_expression (Node (gnat_discriminant_expr),
3908 get_entity_name (gnat_field), 1, 0, 0);
3915 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
3916 any entities on its entity chain similarly. */
3919 mark_out_of_scope (gnat_entity)
3920 Entity_Id gnat_entity;
3922 Entity_Id gnat_sub_entity;
3923 unsigned int kind = Ekind (gnat_entity);
3925 /* If this has an entity list, process all in the list. */
3926 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
3927 || IN (kind, Private_Kind)
3928 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
3929 || kind == E_Function || kind == E_Generic_Function
3930 || kind == E_Generic_Package || kind == E_Generic_Procedure
3931 || kind == E_Loop || kind == E_Operator || kind == E_Package
3932 || kind == E_Package_Body || kind == E_Procedure
3933 || kind == E_Record_Type || kind == E_Record_Subtype
3934 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
3935 for (gnat_sub_entity = First_Entity (gnat_entity);
3936 Present (gnat_sub_entity);
3937 gnat_sub_entity = Next_Entity (gnat_sub_entity))
3938 if (Scope (gnat_sub_entity) == gnat_entity
3939 && gnat_sub_entity != gnat_entity)
3940 mark_out_of_scope (gnat_sub_entity);
3942 /* Now clear this if it has been defined, but only do so if it isn't
3943 a subprogram or parameter. We could refine this, but it isn't
3944 worth it. If this is statically allocated, it is supposed to
3945 hang around out of cope. */
3946 if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
3947 && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
3949 save_gnu_tree (gnat_entity, NULL_TREE, 1);
3950 save_gnu_tree (gnat_entity, error_mark_node, 1);
3954 /* Return a TREE_LIST describing the substitutions needed to reflect
3955 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
3956 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
3957 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
3958 gives the tree for the discriminant and TREE_VALUES is the replacement
3959 value. They are in the form of operands to substitute_in_expr.
3960 DEFINITION is as in gnat_to_gnu_entity. */
3963 substitution_list (gnat_subtype, gnat_type, gnu_list, definition)
3964 Entity_Id gnat_subtype;
3965 Entity_Id gnat_type;
3969 Entity_Id gnat_discrim;
3973 gnat_type = Implementation_Base_Type (gnat_subtype);
3975 if (Has_Discriminants (gnat_type))
3976 for (gnat_discrim = First_Girder_Discriminant (gnat_type),
3977 gnat_value = First_Elmt (Girder_Constraint (gnat_subtype));
3978 Present (gnat_discrim);
3979 gnat_discrim = Next_Girder_Discriminant (gnat_discrim),
3980 gnat_value = Next_Elmt (gnat_value))
3981 /* Ignore access discriminants. */
3982 if (! Is_Access_Type (Etype (Node (gnat_value))))
3983 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
3984 elaborate_expression
3985 (Node (gnat_value), gnat_subtype,
3986 get_entity_name (gnat_discrim), definition,
3993 /* For the following two functions: for each GNAT entity, the GCC
3994 tree node used as a dummy for that entity, if any. */
3996 static tree *dummy_node_table;
3998 /* Initialize the above table. */
4005 dummy_node_table = (tree *) xmalloc (max_gnat_nodes * sizeof (tree));
4006 ggc_add_tree_root (dummy_node_table, max_gnat_nodes);
4008 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4009 dummy_node_table[gnat_node] = NULL_TREE;
4011 dummy_node_table -= First_Node_Id;
4014 /* Make a dummy type corresponding to GNAT_TYPE. */
4017 make_dummy_type (gnat_type)
4018 Entity_Id gnat_type;
4020 Entity_Id gnat_underlying;
4023 /* Find a full type for GNAT_TYPE, taking into account any class wide
4025 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4026 gnat_type = Equivalent_Type (gnat_type);
4027 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4028 gnat_type = Root_Type (gnat_type);
4030 for (gnat_underlying = gnat_type;
4031 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4032 && Present (Full_View (gnat_underlying)));
4033 gnat_underlying = Full_View (gnat_underlying))
4036 /* If it there already a dummy type, use that one. Else make one. */
4037 if (dummy_node_table[gnat_underlying])
4038 return dummy_node_table[gnat_underlying];
4040 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4042 if (Is_Record_Type (gnat_underlying))
4043 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4044 ? UNION_TYPE : RECORD_TYPE);
4046 gnu_type = make_node (ENUMERAL_TYPE);
4048 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4049 if (AGGREGATE_TYPE_P (gnu_type))
4050 TYPE_STUB_DECL (gnu_type)
4051 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4053 TYPE_DUMMY_P (gnu_type) = 1;
4054 dummy_node_table[gnat_underlying] = gnu_type;
4059 /* Return 1 if the size represented by GNU_SIZE can be handled by an
4060 allocation. If STATIC_P is non-zero, consider only what can be
4061 done with a static allocation. */
4064 allocatable_size_p (gnu_size, static_p)
4068 /* If this is not a static allocation, the only case we want to forbid
4069 is an overflowing size. That will be converted into a raise a
4072 return ! (TREE_CODE (gnu_size) == INTEGER_CST
4073 && TREE_CONSTANT_OVERFLOW (gnu_size));
4075 /* Otherwise, we need to deal with both variable sizes and constant
4076 sizes that won't fit in a host int. */
4077 return host_integerp (gnu_size, 1);
4080 /* Return a list of attributes for GNAT_ENTITY, if any. */
4082 static struct attrib *
4083 build_attr_list (gnat_entity)
4084 Entity_Id gnat_entity;
4086 struct attrib *attr_list = 0;
4089 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4090 gnat_temp = Next_Rep_Item (gnat_temp))
4091 if (Nkind (gnat_temp) == N_Pragma)
4093 struct attrib *attr;
4094 tree gnu_arg0 = 0, gnu_arg1 = 0;
4095 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4096 enum attr_type etype;
4098 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4099 && Present (Next (First (gnat_assoc)))
4100 && (Nkind (Expression (Next (First (gnat_assoc))))
4101 == N_String_Literal))
4103 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4106 (First (gnat_assoc))))));
4107 if (Present (Next (Next (First (gnat_assoc))))
4108 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4109 == N_String_Literal))
4110 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4114 (First (gnat_assoc)))))));
4117 switch (Get_Pragma_Id (Chars (gnat_temp)))
4119 case Pragma_Machine_Attribute:
4120 etype = ATTR_MACHINE_ATTRIBUTE;
4123 case Pragma_Linker_Alias:
4124 etype = ATTR_LINK_ALIAS;
4127 case Pragma_Linker_Section:
4128 etype = ATTR_LINK_SECTION;
4131 case Pragma_Weak_External:
4132 etype = ATTR_WEAK_EXTERNAL;
4139 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4140 attr->next = attr_list;
4142 attr->name = gnu_arg0;
4143 attr->arg = gnu_arg1;
4145 = Present (Next (First (gnat_assoc)))
4146 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4153 /* Get the unpadded version of a GNAT type. */
4156 get_unpadded_type (gnat_entity)
4157 Entity_Id gnat_entity;
4159 tree type = gnat_to_gnu_type (gnat_entity);
4161 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4162 type = TREE_TYPE (TYPE_FIELDS (type));
4167 /* Called when we need to protect a variable object using a save_expr. */
4170 maybe_variable (gnu_operand, gnat_node)
4174 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4175 || TREE_CODE (gnu_operand) == SAVE_EXPR
4176 || TREE_CODE (gnu_operand) == NULL_EXPR)
4179 /* If we will be generating code, make sure we are at the proper
4181 if (! global_bindings_p () && ! TREE_CONSTANT (gnu_operand)
4182 && ! contains_placeholder_p (gnu_operand))
4183 set_lineno (gnat_node, 1);
4185 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4186 return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4187 variable_size (TREE_OPERAND (gnu_operand, 0)));
4189 return variable_size (gnu_operand);
4192 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4193 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4194 return the GCC tree to use for that expression. GNU_NAME is the
4195 qualification to use if an external name is appropriate and DEFINITION is
4196 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4197 we need a result. Otherwise, we are just elaborating this for
4198 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4199 purposes even if it isn't needed for code generation. */
4202 elaborate_expression (gnat_expr, gnat_entity, gnu_name, definition,
4203 need_value, need_debug)
4205 Entity_Id gnat_entity;
4213 /* If we already elaborated this expression (e.g., it was involved
4214 in the definition of a private type), use the old value. */
4215 if (present_gnu_tree (gnat_expr))
4216 return get_gnu_tree (gnat_expr);
4218 /* If we don't need a value and this is static or a discriment, we
4219 don't need to do anything. */
4220 else if (! need_value
4221 && (Is_OK_Static_Expression (gnat_expr)
4222 || (Nkind (gnat_expr) == N_Identifier
4223 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4226 /* Otherwise, convert this tree to its GCC equivalant. */
4228 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4229 gnu_name, definition, need_debug);
4231 /* Save the expression in case we try to elaborate this entity again.
4232 Since this is not a DECL, don't check it. If this is a constant,
4233 don't save it since GNAT_EXPR might be used more than once. Also,
4234 don't save if it's a discriminant. */
4235 if (! TREE_CONSTANT (gnu_expr) && ! contains_placeholder_p (gnu_expr))
4236 save_gnu_tree (gnat_expr, gnu_expr, 1);
4238 return need_value ? gnu_expr : error_mark_node;
4241 /* Similar, but take a GNU expression. */
4244 elaborate_expression_1 (gnat_expr, gnat_entity, gnu_expr, gnu_name, definition,
4247 Entity_Id gnat_entity;
4254 tree gnu_inner_expr = gnu_expr;
4256 int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4258 /* Strip any conversions to see if the expression is a readonly variable.
4259 ??? This really should remain readonly, but we have to think about
4260 the typing of the tree here. */
4261 while (TREE_CODE (gnu_inner_expr) == NOP_EXPR
4262 && TREE_CODE (gnu_inner_expr) == CONVERT_EXPR)
4263 gnu_inner_expr = TREE_OPERAND (gnu_inner_expr, 0);
4265 /* In most cases, we won't see a naked FIELD_DECL here because a
4266 discriminant reference will have been replaced with a COMPONENT_REF
4267 when the type is being elaborated. However, there are some cases
4268 involving child types where we will. So convert it to a COMPONENT_REF
4269 here. We have to hope it will be at the highest level of the
4270 expression in these cases. */
4271 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4272 gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4273 build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4277 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4278 that is a constant, make a variable that is initialized to contain the
4279 bound when the package containing the definition is elaborated. If
4280 this entity is defined at top level and a bound or discriminant value
4281 isn't a constant or a reference to a discriminant, replace the bound
4282 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4283 rely here on the fact that an expression cannot contain both the
4284 discriminant and some other variable. */
4286 expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4287 && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4288 && TREE_READONLY (gnu_inner_expr))
4289 && ! contains_placeholder_p (gnu_expr));
4291 /* If this is a static expression or contains a discriminant, we don't
4292 need the variable for debugging (and can't elaborate anyway if a
4295 && (Is_OK_Static_Expression (gnat_expr)
4296 || contains_placeholder_p (gnu_expr)))
4299 /* Now create the variable if we need it. */
4300 if (need_debug || (expr_variable && expr_global))
4302 set_lineno (gnat_entity, ! global_bindings_p ());
4304 = create_var_decl (create_concat_name (gnat_entity,
4305 IDENTIFIER_POINTER (gnu_name)),
4306 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4307 Is_Public (gnat_entity), ! definition, 0, 0);
4310 /* We only need to use this variable if we are in global context since GCC
4311 can do the right thing in the local case. */
4312 if (expr_global && expr_variable)
4315 return maybe_variable (gnu_expr, gnat_expr);
4318 /* Create a record type that contains a field of TYPE with a starting bit
4319 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4322 make_aligning_type (type, align, size)
4327 tree record_type = make_node (RECORD_TYPE);
4328 tree place = build (PLACEHOLDER_EXPR, record_type);
4329 tree size_addr_place = convert (sizetype,
4330 build_unary_op (ADDR_EXPR, NULL_TREE,
4332 tree name = TYPE_NAME (type);
4335 if (TREE_CODE (name) == TYPE_DECL)
4336 name = DECL_NAME (name);
4338 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4340 /* The bit position is obtained by "and"ing the alignment minus 1
4341 with the two's complement of the address and multiplying
4342 by the number of bits per unit. Do all this in sizetype. */
4344 pos = size_binop (MULT_EXPR,
4345 convert (bitsizetype,
4346 size_binop (BIT_AND_EXPR,
4347 size_diffop (size_zero_node,
4349 ssize_int ((align / BITS_PER_UNIT)
4353 field = create_field_decl (get_identifier ("F"), type, record_type,
4355 DECL_BIT_FIELD (field) = 0;
4357 finish_record_type (record_type, field, 1, 0);
4358 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4359 TYPE_SIZE (record_type)
4360 = size_binop (PLUS_EXPR,
4361 size_binop (MULT_EXPR, convert (bitsizetype, size),
4363 bitsize_int (align));
4364 TYPE_SIZE_UNIT (record_type)
4365 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4370 /* TYPE is a RECORD_TYPE with BLKmode that's being used as the field
4371 type of a packed record. See if we can rewrite it as a record that has
4372 a non-BLKmode type, which we can pack tighter. If so, return the
4373 new type. If not, return the original type. */
4376 make_packable_type (type)
4379 tree new_type = make_node (RECORD_TYPE);
4380 tree field_list = NULL_TREE;
4383 /* Copy the name and flags from the old type to that of the new and set
4384 the alignment to try for an integral type. */
4385 TYPE_NAME (new_type) = TYPE_NAME (type);
4386 TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4387 = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4388 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4390 TYPE_ALIGN (new_type)
4391 = ((HOST_WIDE_INT) 1
4392 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4394 /* Now copy the fields, keeping the position and size. */
4395 for (old_field = TYPE_FIELDS (type); old_field != 0;
4396 old_field = TREE_CHAIN (old_field))
4399 = create_field_decl (DECL_NAME (old_field), TREE_TYPE (old_field),
4400 new_type, TYPE_PACKED (type),
4401 DECL_SIZE (old_field),
4402 bit_position (old_field),
4403 ! DECL_NONADDRESSABLE_P (old_field));
4405 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4406 DECL_ORIGINAL_FIELD (new_field)
4407 = (DECL_ORIGINAL_FIELD (old_field) != 0
4408 ? DECL_ORIGINAL_FIELD (old_field) : old_field);
4409 TREE_CHAIN (new_field) = field_list;
4410 field_list = new_field;
4413 finish_record_type (new_type, nreverse (field_list), 1, 1);
4414 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4417 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4418 if needed. We have already verified that SIZE and TYPE are large enough.
4420 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4423 IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4425 DEFINITION is nonzero if this type is being defined.
4427 SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4428 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4432 maybe_pad_type (type, size, align, gnat_entity, name_trailer,
4433 is_user_type, definition, same_rm_size)
4437 Entity_Id gnat_entity;
4438 const char *name_trailer;
4443 tree orig_size = TYPE_SIZE (type);
4447 /* If TYPE is a padded type, see if it agrees with any size and alignment
4448 we were given. If so, return the original type. Otherwise, strip
4449 off the padding, since we will either be returning the inner type
4450 or repadding it. If no size or alignment is specified, use that of
4451 the original padded type. */
4453 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4456 || operand_equal_p (round_up (size,
4457 MAX (align, TYPE_ALIGN (type))),
4458 round_up (TYPE_SIZE (type),
4459 MAX (align, TYPE_ALIGN (type))),
4461 && (align == 0 || align == TYPE_ALIGN (type)))
4465 size = TYPE_SIZE (type);
4467 align = TYPE_ALIGN (type);
4469 type = TREE_TYPE (TYPE_FIELDS (type));
4470 orig_size = TYPE_SIZE (type);
4473 /* If the size is either not being changed or is being made smaller (which
4474 is not done here (and is only valid for bitfields anyway), show the size
4475 isn't changing. Likewise, clear the alignment if it isn't being
4476 changed. Then return if we aren't doing anything. */
4479 && (operand_equal_p (size, orig_size, 0)
4480 || (TREE_CODE (orig_size) == INTEGER_CST
4481 && tree_int_cst_lt (size, orig_size))))
4484 if (align == TYPE_ALIGN (type))
4487 if (align == 0 && size == 0)
4490 /* We used to modify the record in place in some cases, but that could
4491 generate incorrect debugging information. So make a new record
4493 record = make_node (RECORD_TYPE);
4495 if (Present (gnat_entity))
4496 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4498 /* If we were making a type, complete the original type and give it a
4501 create_type_decl (get_entity_name (gnat_entity), type,
4502 0, ! Comes_From_Source (gnat_entity),
4503 ! (TYPE_NAME (type) != 0
4504 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4505 && DECL_IGNORED_P (TYPE_NAME (type))));
4507 /* If we are changing the alignment and the input type is a record with
4508 BLKmode and a small constant size, try to make a form that has an
4509 integral mode. That might allow this record to have an integral mode,
4510 which will be much more efficient. There is no point in doing this if a
4511 size is specified unless it is also smaller than the biggest alignment
4512 and it is incorrect to do this if the size of the original type is not a
4513 multiple of the alignment. */
4515 && TREE_CODE (type) == RECORD_TYPE
4516 && TYPE_MODE (type) == BLKmode
4517 && host_integerp (orig_size, 1)
4518 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4520 || (TREE_CODE (size) == INTEGER_CST
4521 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4522 && tree_low_cst (orig_size, 1) % align == 0)
4523 type = make_packable_type (type);
4525 field = create_field_decl (get_identifier ("F"), type, record, 0,
4526 NULL_TREE, bitsize_zero_node, 1);
4528 DECL_INTERNAL_P (field) = 1;
4529 TYPE_SIZE (record) = size != 0 ? size : orig_size;
4530 TYPE_SIZE_UNIT (record)
4531 = convert (sizetype,
4532 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4533 bitsize_unit_node));
4534 TYPE_ALIGN (record) = align;
4535 TYPE_IS_PADDING_P (record) = 1;
4536 TYPE_VOLATILE (record)
4537 = Present (gnat_entity) && Is_Volatile (gnat_entity);
4538 finish_record_type (record, field, 1, 0);
4540 /* Keep the RM_Size of the padded record as that of the old record
4542 TYPE_ADA_SIZE (record) = same_rm_size ? size : rm_size (type);
4544 /* Unless debugging information isn't being written for the input type,
4545 write a record that shows what we are a subtype of and also make a
4546 variable that indicates our size, if variable. */
4547 if (TYPE_NAME (record) != 0
4548 && AGGREGATE_TYPE_P (type)
4549 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4550 || ! DECL_IGNORED_P (TYPE_NAME (type))))
4552 tree marker = make_node (RECORD_TYPE);
4553 tree name = DECL_NAME (TYPE_NAME (record));
4554 tree orig_name = TYPE_NAME (type);
4556 if (TREE_CODE (orig_name) == TYPE_DECL)
4557 orig_name = DECL_NAME (orig_name);
4559 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4560 finish_record_type (marker,
4561 create_field_decl (orig_name, integer_type_node,
4562 marker, 0, NULL_TREE, NULL_TREE,
4566 if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4567 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4568 sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4574 if (TREE_CODE (orig_size) != INTEGER_CST
4575 && contains_placeholder_p (orig_size))
4576 orig_size = max_size (orig_size, 1);
4578 /* If the size was widened explicitly, maybe give a warning. */
4579 if (size != 0 && Present (gnat_entity)
4580 && ! operand_equal_p (size, orig_size, 0)
4581 && ! (TREE_CODE (size) == INTEGER_CST
4582 && TREE_CODE (orig_size) == INTEGER_CST
4583 && tree_int_cst_lt (size, orig_size)))
4585 Node_Id gnat_error_node = Empty;
4587 if (Is_Packed_Array_Type (gnat_entity))
4588 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4590 if ((Ekind (gnat_entity) == E_Component
4591 || Ekind (gnat_entity) == E_Discriminant)
4592 && Present (Component_Clause (gnat_entity)))
4593 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4594 else if (Present (Size_Clause (gnat_entity)))
4595 gnat_error_node = Expression (Size_Clause (gnat_entity));
4597 /* Generate message only for entities that come from source, since
4598 if we have an entity created by expansion, the message will be
4599 generated for some other corresponding source entity. */
4600 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4601 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4603 size_diffop (size, orig_size));
4605 else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4606 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4607 gnat_entity, gnat_entity,
4608 size_diffop (size, orig_size));
4614 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4615 the value passed against the list of choices. */
4618 choices_to_gnu (operand, choices)
4624 tree result = integer_zero_node;
4625 tree this_test, low = 0, high = 0, single = 0;
4627 for (choice = First (choices); Present (choice); choice = Next (choice))
4629 switch (Nkind (choice))
4632 low = gnat_to_gnu (Low_Bound (choice));
4633 high = gnat_to_gnu (High_Bound (choice));
4635 /* There's no good type to use here, so we might as well use
4636 integer_type_node. */
4638 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4639 build_binary_op (GE_EXPR, integer_type_node,
4641 build_binary_op (LE_EXPR, integer_type_node,
4646 case N_Subtype_Indication:
4647 gnat_temp = Range_Expression (Constraint (choice));
4648 low = gnat_to_gnu (Low_Bound (gnat_temp));
4649 high = gnat_to_gnu (High_Bound (gnat_temp));
4652 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4653 build_binary_op (GE_EXPR, integer_type_node,
4655 build_binary_op (LE_EXPR, integer_type_node,
4660 case N_Expanded_Name:
4661 /* This represents either a subtype range, an enumeration
4662 literal, or a constant Ekind says which. If an enumeration
4663 literal or constant, fall through to the next case. */
4664 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4665 && Ekind (Entity (choice)) != E_Constant)
4667 tree type = gnat_to_gnu_type (Entity (choice));
4669 low = TYPE_MIN_VALUE (type);
4670 high = TYPE_MAX_VALUE (type);
4673 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4674 build_binary_op (GE_EXPR, integer_type_node,
4676 build_binary_op (LE_EXPR, integer_type_node,
4680 /* ... fall through ... */
4681 case N_Character_Literal:
4682 case N_Integer_Literal:
4683 single = gnat_to_gnu (choice);
4684 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4688 case N_Others_Choice:
4689 this_test = integer_one_node;
4696 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4703 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4704 placed in GNU_RECORD_TYPE.
4706 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4707 record has a Component_Alignment of Storage_Unit.
4709 DEFINITION is nonzero if this field is for a record being defined. */
4712 gnat_to_gnu_field (gnat_field, gnu_record_type, packed, definition)
4713 Entity_Id gnat_field;
4714 tree gnu_record_type;
4718 tree gnu_field_id = get_entity_name (gnat_field);
4719 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4720 tree gnu_orig_field_type = gnu_field_type;
4724 int needs_strict_alignment
4725 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
4726 || Is_Volatile (gnat_field));
4728 /* If this field requires strict alignment pretend it isn't packed. */
4729 if (needs_strict_alignment)
4732 /* For packed records, this is one of the few occasions on which we use
4733 the official RM size for discrete or fixed-point components, instead
4734 of the normal GNAT size stored in Esize. See description in Einfo:
4735 "Handling of Type'Size Values" for further details. */
4738 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4739 gnat_field, FIELD_DECL, 0, 1);
4741 if (Known_Static_Esize (gnat_field))
4742 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4743 gnat_field, FIELD_DECL, 0, 1);
4745 /* If we are packing this record and the field type is also a record
4746 that's BLKmode and with a small constant size, see if we can get a
4747 better form of the type that allows more packing. If we can, show
4748 a size was specified for it if there wasn't one so we know to
4749 make this a bitfield and avoid making things wider. */
4750 if (packed && TREE_CODE (gnu_field_type) == RECORD_TYPE
4751 && TYPE_MODE (gnu_field_type) == BLKmode
4752 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
4753 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0)
4755 gnu_field_type = make_packable_type (gnu_field_type);
4757 if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
4758 gnu_size = rm_size (gnu_field_type);
4761 if (Present (Component_Clause (gnat_field)))
4763 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
4764 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4765 gnat_field, FIELD_DECL, 0, 1);
4767 /* Ensure the position does not overlap with the parent subtype,
4769 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
4772 = gnat_to_gnu_type (Parent_Subtype
4773 (Underlying_Type (Scope (gnat_field))));
4775 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
4776 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
4779 ("offset of& must be beyond parent{, minimum allowed is ^}",
4780 First_Bit (Component_Clause (gnat_field)), gnat_field,
4781 TYPE_SIZE_UNIT (gnu_parent));
4785 /* If this field needs strict alignment, ensure the record is
4786 sufficiently aligned and that that position and size are
4787 consistent with the alignment. */
4788 if (needs_strict_alignment)
4790 tree gnu_min_size = round_up (rm_size (gnu_field_type),
4791 TYPE_ALIGN (gnu_field_type));
4793 TYPE_ALIGN (gnu_record_type)
4794 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
4796 /* If Atomic, the size must match exactly and if aliased, the size
4797 must not be less than the rounded size. */
4798 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
4799 && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
4802 ("atomic field& must be natural size of type{ (^)}",
4803 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4804 TYPE_SIZE (gnu_field_type));
4809 else if (Is_Aliased (gnat_field)
4811 && tree_int_cst_lt (gnu_size, gnu_min_size))
4814 ("size of aliased field& too small{, minimum required is ^}",
4815 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4820 if (! integer_zerop (size_binop
4821 (TRUNC_MOD_EXPR, gnu_pos,
4822 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
4824 if (Is_Aliased (gnat_field))
4826 ("position of aliased field& must be multiple of ^ bits",
4827 Component_Clause (gnat_field), gnat_field,
4828 TYPE_ALIGN (gnu_field_type));
4830 else if (Is_Volatile (gnat_field))
4832 ("position of volatile field& must be multiple of ^ bits",
4833 First_Bit (Component_Clause (gnat_field)), gnat_field,
4834 TYPE_ALIGN (gnu_field_type));
4836 else if (Strict_Alignment (Etype (gnat_field)))
4838 ("position of & with aliased or tagged components not multiple of ^ bits",
4839 First_Bit (Component_Clause (gnat_field)), gnat_field,
4840 TYPE_ALIGN (gnu_field_type));
4847 /* If an error set the size to zero, show we have no position
4853 if (Is_Atomic (gnat_field))
4854 check_ok_for_atomic (gnu_field_type, gnat_field, 0);
4856 if (gnu_pos !=0 && TYPE_MODE (gnu_field_type) == BLKmode
4857 && (! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
4858 bitsize_unit_node))))
4860 /* Try to see if we can make this a packable type. If we
4862 if (TREE_CODE (gnu_field_type) == RECORD_TYPE)
4863 gnu_field_type = make_packable_type (gnu_field_type);
4865 if (TYPE_MODE (gnu_field_type) == BLKmode)
4867 post_error_ne ("fields of& must start at storage unit boundary",
4868 First_Bit (Component_Clause (gnat_field)),
4869 Etype (gnat_field));
4875 /* If the record has rep clauses and this is the tag field, make a rep
4876 clause for it as well. */
4877 else if (Has_Specified_Layout (Scope (gnat_field))
4878 && Chars (gnat_field) == Name_uTag)
4880 gnu_pos = bitsize_zero_node;
4881 gnu_size = TYPE_SIZE (gnu_field_type);
4884 /* We need to make the size the maximum for the type if it is
4885 self-referential and an unconstrained type. */
4886 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4888 && ! TREE_CONSTANT (TYPE_SIZE (gnu_field_type))
4889 && contains_placeholder_p (TYPE_SIZE (gnu_field_type))
4890 && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
4891 gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
4893 /* If no size is specified (or if there was an error), don't specify a
4899 /* Unless this field is aliased, we can remove any left-justified
4900 modular type since it's only needed in the unchecked conversion
4901 case, which doesn't apply here. */
4902 if (! needs_strict_alignment
4903 && TREE_CODE (gnu_field_type) == RECORD_TYPE
4904 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4905 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4908 = make_type_from_size (gnu_field_type, gnu_size,
4909 Has_Biased_Representation (gnat_field));
4910 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
4911 gnat_field, "PAD", 0, definition, 1);
4914 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4915 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
4918 set_lineno (gnat_field, 0);
4919 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
4920 packed, gnu_size, gnu_pos,
4921 Is_Aliased (gnat_field));
4923 TREE_THIS_VOLATILE (gnu_field) = Is_Volatile (gnat_field);
4925 if (Ekind (gnat_field) == E_Discriminant)
4926 DECL_DISCRIMINANT_NUMBER (gnu_field)
4927 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
4932 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
4933 of GCC trees for fields that are in the record and have already been
4934 processed. When called from gnat_to_gnu_entity during the processing of a
4935 record type definition, the GCC nodes for the discriminants will be on
4936 the chain. The other calls to this function are recursive calls from
4937 itself for the Component_List of a variant and the chain is empty.
4939 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
4940 for a record type with "pragma component_alignment (storage_unit)".
4942 FINISH_RECORD is nonzero if this call will supply all of the remaining
4943 fields of the record.
4945 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
4946 with a rep clause is to be added. If it is nonzero, that is all that
4947 should be done with such fields.
4949 CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
4950 before laying out the record. This means the alignment only serves
4951 to force fields to be bitfields, but not require the record to be
4952 that aligned. This is used for variants.
4954 ALL_REP, if nonzero, means that a rep clause was found for all the
4955 fields. This simplifies the logic since we know we're not in the mixed
4958 The processing of the component list fills in the chain with all of the
4959 fields of the record and then the record type is finished. */
4962 components_to_record (gnu_record_type, component_list, gnu_field_list, packed,
4963 definition, p_gnu_rep_list, cancel_alignment, all_rep)
4964 tree gnu_record_type;
4965 Node_Id component_list;
4966 tree gnu_field_list;
4969 tree *p_gnu_rep_list;
4970 int cancel_alignment;
4973 Node_Id component_decl;
4974 Entity_Id gnat_field;
4975 Node_Id variant_part;
4977 tree gnu_our_rep_list = NULL_TREE;
4978 tree gnu_field, gnu_last;
4979 int layout_with_rep = 0;
4981 /* For each variable within each component declaration create a GCC field
4982 and add it to the list, skipping any pragmas in the list. */
4984 if (Present (Component_Items (component_list)))
4985 for (component_decl = First_Non_Pragma (Component_Items (component_list));
4986 Present (component_decl);
4987 component_decl = Next_Non_Pragma (component_decl))
4989 gnat_field = Defining_Entity (component_decl);
4991 if (Chars (gnat_field) == Name_uParent)
4992 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
4995 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
4996 packed, definition);
4998 /* If this is the _Tag field, put it before any discriminants,
4999 instead of after them as is the case for all other fields. */
5000 if (Chars (gnat_field) == Name_uTag)
5001 gnu_field_list = chainon (gnu_field_list, gnu_field);
5004 TREE_CHAIN (gnu_field) = gnu_field_list;
5005 gnu_field_list = gnu_field;
5009 save_gnu_tree (gnat_field, gnu_field, 0);
5012 /* At the end of the component list there may be a variant part. */
5013 variant_part = Variant_Part (component_list);
5015 /* If this is an unchecked union, each variant must have exactly one
5016 component, each of which becomes one component of this union. */
5017 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5018 for (variant = First_Non_Pragma (Variants (variant_part));
5020 variant = Next_Non_Pragma (variant))
5023 = First_Non_Pragma (Component_Items (Component_List (variant)));
5024 gnat_field = Defining_Entity (component_decl);
5025 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5027 TREE_CHAIN (gnu_field) = gnu_field_list;
5028 gnu_field_list = gnu_field;
5029 save_gnu_tree (gnat_field, gnu_field, 0);
5032 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5033 mutually exclusive and should go in the same memory. To do this we need
5034 to treat each variant as a record whose elements are created from the
5035 component list for the variant. So here we create the records from the
5036 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5037 else if (Present (variant_part))
5039 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5041 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5042 tree gnu_union_field;
5043 tree gnu_variant_list = NULL_TREE;
5044 tree gnu_name = TYPE_NAME (gnu_record_type);
5046 = concat_id_with_name
5047 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5050 if (TREE_CODE (gnu_name) == TYPE_DECL)
5051 gnu_name = DECL_NAME (gnu_name);
5053 TYPE_NAME (gnu_union_type)
5054 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5055 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5057 for (variant = First_Non_Pragma (Variants (variant_part));
5059 variant = Next_Non_Pragma (variant))
5061 tree gnu_variant_type = make_node (RECORD_TYPE);
5062 tree gnu_inner_name;
5065 Get_Variant_Encoding (variant);
5066 gnu_inner_name = get_identifier (Name_Buffer);
5067 TYPE_NAME (gnu_variant_type)
5068 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5069 IDENTIFIER_POINTER (gnu_inner_name));
5071 /* Set the alignment of the inner type in case we need to make
5072 inner objects into bitfields, but then clear it out
5073 so the record actually gets only the alignment required. */
5074 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5075 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5076 components_to_record (gnu_variant_type, Component_List (variant),
5077 NULL_TREE, packed, definition,
5078 &gnu_our_rep_list, 1, all_rep);
5080 gnu_qual = choices_to_gnu (gnu_discriminant,
5081 Discrete_Choices (variant));
5083 Set_Present_Expr (variant, annotate_value (gnu_qual));
5084 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5085 gnu_union_type, 0, 0, 0, 1);
5086 DECL_INTERNAL_P (gnu_field) = 1;
5087 DECL_QUALIFIER (gnu_field) = gnu_qual;
5088 TREE_CHAIN (gnu_field) = gnu_variant_list;
5089 gnu_variant_list = gnu_field;
5092 /* We can delete any empty variants from the end. This may leave none
5093 left. Note we cannot delete variants from anywhere else. */
5094 while (gnu_variant_list != 0
5095 && TYPE_FIELDS (TREE_TYPE (gnu_variant_list)) == 0)
5096 gnu_variant_list = TREE_CHAIN (gnu_variant_list);
5098 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5099 if (gnu_variant_list != 0)
5101 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5105 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5107 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5108 all_rep ? bitsize_zero_node : 0, 1);
5110 DECL_INTERNAL_P (gnu_union_field) = 1;
5111 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5112 gnu_field_list = gnu_union_field;
5116 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5117 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5118 in a separate pass since we want to handle the discriminants but can't
5119 play with them until we've used them in debugging data above.
5121 ??? Note: if we then reorder them, debugging information will be wrong,
5122 but there's nothing that can be done about this at the moment. */
5124 for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5126 if (DECL_FIELD_OFFSET (gnu_field) != 0)
5128 tree gnu_next = TREE_CHAIN (gnu_field);
5131 gnu_field_list = gnu_next;
5133 TREE_CHAIN (gnu_last) = gnu_next;
5135 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5136 gnu_our_rep_list = gnu_field;
5137 gnu_field = gnu_next;
5141 gnu_last = gnu_field;
5142 gnu_field = TREE_CHAIN (gnu_field);
5146 /* If we have any items in our rep'ed field list, it is not the case that all
5147 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5148 set it and ignore the items. Otherwise, sort the fields by bit position
5149 and put them into their own record if we have any fields without
5151 if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5152 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5153 else if (gnu_our_rep_list != 0)
5156 = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5157 int len = list_length (gnu_our_rep_list);
5158 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5161 /* Set DECL_SECTION_NAME to increasing integers so we have a
5163 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5164 gnu_field = TREE_CHAIN (gnu_field), i++)
5166 gnu_arr[i] = gnu_field;
5167 DECL_SECTION_NAME (gnu_field) = size_int (i);
5170 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5172 /* Put the fields in the list in order of increasing position, which
5173 means we start from the end. */
5174 gnu_our_rep_list = NULL_TREE;
5175 for (i = len - 1; i >= 0; i--)
5177 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5178 gnu_our_rep_list = gnu_arr[i];
5179 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5180 DECL_SECTION_NAME (gnu_arr[i]) = 0;
5183 if (gnu_field_list != 0)
5185 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5186 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5187 gnu_record_type, 0, 0, 0, 1);
5188 DECL_INTERNAL_P (gnu_field) = 1;
5189 gnu_field_list = chainon (gnu_field_list, gnu_field);
5193 layout_with_rep = 1;
5194 gnu_field_list = nreverse (gnu_our_rep_list);
5198 if (cancel_alignment)
5199 TYPE_ALIGN (gnu_record_type) = 0;
5201 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5202 layout_with_rep, 0);
5205 /* Called via qsort from the above. Returns -1, 1, depending on the
5206 bit positions and ordinals of the two fields. */
5209 compare_field_bitpos (rt1, rt2)
5213 tree *t1 = (tree *) rt1;
5214 tree *t2 = (tree *) rt2;
5216 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5218 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5220 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5226 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5227 placed into an Esize, Component_Bit_Offset, or Component_Size value
5228 in the GNAT tree. */
5231 annotate_value (gnu_size)
5234 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5236 Node_Ref_Or_Val ops[3];
5240 /* If we do not return inside this switch, TCODE will be set to the
5241 code to use for a Create_Node operand and LEN (set above) will be
5242 the number of recursive calls for us to make. */
5244 switch (TREE_CODE (gnu_size))
5247 if (TREE_OVERFLOW (gnu_size))
5250 /* This may have come from a conversion from some smaller type,
5251 so ensure this is in bitsizetype. */
5252 gnu_size = convert (bitsizetype, gnu_size);
5254 /* For negative values, use NEGATE_EXPR of the supplied value. */
5255 if (tree_int_cst_sgn (gnu_size) < 0)
5257 /* The rediculous code below is to handle the case of the largest
5258 negative integer. */
5259 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5263 if (TREE_CONSTANT_OVERFLOW (negative_size))
5266 = size_binop (MINUS_EXPR, bitsize_zero_node,
5267 size_binop (PLUS_EXPR, gnu_size,
5272 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5274 temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5276 return annotate_value (temp);
5279 if (! host_integerp (gnu_size, 1))
5282 size = tree_low_cst (gnu_size, 1);
5284 /* This peculiar test is to make sure that the size fits in an int
5285 on machines where HOST_WIDE_INT is not "int". */
5286 if (tree_low_cst (gnu_size, 1) == size)
5287 return UI_From_Int (size);
5292 /* The only case we handle here is a simple discriminant reference. */
5293 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5294 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5295 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5296 return Create_Node (Discrim_Val,
5297 annotate_value (DECL_DISCRIMINANT_NUMBER
5298 (TREE_OPERAND (gnu_size, 1))),
5303 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5304 return annotate_value (TREE_OPERAND (gnu_size, 0));
5306 /* Now just list the operations we handle. */
5307 case COND_EXPR: tcode = Cond_Expr; break;
5308 case PLUS_EXPR: tcode = Plus_Expr; break;
5309 case MINUS_EXPR: tcode = Minus_Expr; break;
5310 case MULT_EXPR: tcode = Mult_Expr; break;
5311 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5312 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5313 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5314 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5315 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5316 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5317 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5318 case NEGATE_EXPR: tcode = Negate_Expr; break;
5319 case MIN_EXPR: tcode = Min_Expr; break;
5320 case MAX_EXPR: tcode = Max_Expr; break;
5321 case ABS_EXPR: tcode = Abs_Expr; break;
5322 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5323 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5324 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5325 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5326 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5327 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5328 case LT_EXPR: tcode = Lt_Expr; break;
5329 case LE_EXPR: tcode = Le_Expr; break;
5330 case GT_EXPR: tcode = Gt_Expr; break;
5331 case GE_EXPR: tcode = Ge_Expr; break;
5332 case EQ_EXPR: tcode = Eq_Expr; break;
5333 case NE_EXPR: tcode = Ne_Expr; break;
5339 /* Now get each of the operands that's relevant for this code. If any
5340 cannot be expressed as a repinfo node, say we can't. */
5341 for (i = 0; i < 3; i++)
5344 for (i = 0; i < len; i++)
5346 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5347 if (ops[i] == No_Uint)
5351 return Create_Node (tcode, ops[0], ops[1], ops[2]);
5354 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5355 GCC type, set Component_Bit_Offset and Esize to the position and size
5359 annotate_rep (gnat_entity, gnu_type)
5360 Entity_Id gnat_entity;
5365 Entity_Id gnat_field;
5367 /* We operate by first making a list of all field and their positions
5368 (we can get the sizes easily at any time) by a recursive call
5369 and then update all the sizes into the tree. */
5370 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5371 size_zero_node, bitsize_zero_node);
5373 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5374 gnat_field = Next_Entity (gnat_field))
5375 if ((Ekind (gnat_field) == E_Component
5376 || (Ekind (gnat_field) == E_Discriminant
5377 && ! Is_Unchecked_Union (Scope (gnat_field))))
5378 && 0 != (gnu_entry = purpose_member (gnat_to_gnu_entity (gnat_field,
5382 Set_Component_Bit_Offset
5384 annotate_value (bit_from_pos
5385 (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5386 TREE_VALUE (TREE_VALUE (gnu_entry)))));
5388 Set_Esize (gnat_field,
5389 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5393 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is
5394 the FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the
5395 byte position and TREE_VALUE being the bit position. GNU_POS is to
5396 be added to the position, GNU_BITPOS to the bit position, and GNU_LIST
5397 is the entries so far. */
5400 compute_field_positions (gnu_type, gnu_list, gnu_pos, gnu_bitpos)
5407 tree gnu_result = gnu_list;
5409 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5410 gnu_field = TREE_CHAIN (gnu_field))
5412 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5413 DECL_FIELD_BIT_OFFSET (gnu_field));
5414 tree gnu_our_pos = size_binop (PLUS_EXPR, gnu_pos,
5415 DECL_FIELD_OFFSET (gnu_field));
5418 = tree_cons (gnu_field,
5419 tree_cons (gnu_our_pos, gnu_our_bitpos, NULL_TREE),
5422 if (DECL_INTERNAL_P (gnu_field))
5424 = compute_field_positions (TREE_TYPE (gnu_field),
5425 gnu_result, gnu_our_pos, gnu_our_bitpos);
5431 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5432 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5433 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5434 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5435 for the size of a field. COMPONENT_P is true if we are being called
5436 to process the Component_Size of GNAT_OBJECT. This is used for error
5437 message handling and to indicate to use the object size of GNU_TYPE.
5438 ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5439 it means that a size of zero should be treated as an unspecified size. */
5442 validate_size (uint_size, gnu_type, gnat_object, kind, component_p, zero_ok)
5445 Entity_Id gnat_object;
5446 enum tree_code kind;
5450 Node_Id gnat_error_node;
5452 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5455 if (type_size != 0 && TREE_CODE (type_size) != INTEGER_CST
5456 && contains_placeholder_p (type_size))
5457 type_size = max_size (type_size, 1);
5459 if (TYPE_FAT_POINTER_P (gnu_type))
5460 type_size = bitsize_int (POINTER_SIZE);
5462 if ((Ekind (gnat_object) == E_Component
5463 || Ekind (gnat_object) == E_Discriminant)
5464 && Present (Component_Clause (gnat_object)))
5465 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5466 else if (Present (Size_Clause (gnat_object)))
5467 gnat_error_node = Expression (Size_Clause (gnat_object));
5469 gnat_error_node = gnat_object;
5471 /* Don't give errors on packed array types; we'll be giving the error on
5472 the type itself soon enough. */
5473 if (Is_Packed_Array_Type (gnat_object))
5474 gnat_error_node = Empty;
5476 /* Get the size as a tree. Return 0 if none was specified, either because
5477 Esize was not Present or if the specified size was zero. Give an error
5478 if a size was specified, but cannot be represented as in sizetype. If
5479 the size is negative, it was a back-annotation of a variable size and
5480 should be treated as not specified. */
5481 if (No (uint_size) || uint_size == No_Uint)
5484 size = UI_To_gnu (uint_size, bitsizetype);
5485 if (TREE_OVERFLOW (size))
5488 post_error_ne ("component size of & is too large",
5489 gnat_error_node, gnat_object);
5491 post_error_ne ("size of & is too large", gnat_error_node, gnat_object);
5496 /* Ignore a negative size since that corresponds to our back-annotation.
5497 Also ignore a zero size unless a size clause exists. */
5498 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
5501 /* The size of objects is always a multiple of a byte. */
5502 if (kind == VAR_DECL
5503 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5504 bitsize_unit_node)))
5507 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5508 gnat_error_node, gnat_object);
5510 post_error_ne ("size for& is not a multiple of Storage_Unit",
5511 gnat_error_node, gnat_object);
5515 /* If this is an integral type, the front-end has verified the size, so we
5516 need not do it here (which would entail checking against the bounds).
5517 However, if this is an aliased object, it may not be smaller than the
5518 type of the object. */
5519 if (INTEGRAL_TYPE_P (gnu_type) && ! TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
5520 && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5523 /* If the object is a record that contains a template, add the size of
5524 the template to the specified size. */
5525 if (TREE_CODE (gnu_type) == RECORD_TYPE
5526 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5527 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5529 /* If the size of the object is a constant, the new size must not be
5531 if (TREE_CODE (type_size) != INTEGER_CST
5532 || TREE_OVERFLOW (type_size)
5533 || tree_int_cst_lt (size, type_size))
5537 ("component size for& too small{, minimum allowed is ^}",
5538 gnat_error_node, gnat_object, type_size);
5540 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5541 gnat_error_node, gnat_object, type_size);
5543 if (kind == VAR_DECL && ! component_p
5544 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5545 && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5546 post_error_ne_tree_2
5547 ("\\size of ^ rounded up to multiple of alignment (^ bits)",
5548 gnat_error_node, gnat_object, rm_size (gnu_type),
5549 TYPE_ALIGN (gnu_type));
5551 else if (INTEGRAL_TYPE_P (gnu_type))
5552 post_error_ne ("\\size would be legal if & were not aliased!",
5553 gnat_error_node, gnat_object);
5561 /* Similarly, but both validate and process a value of RM_Size. This
5562 routine is only called for types. */
5565 set_rm_size (uint_size, gnu_type, gnat_entity)
5568 Entity_Id gnat_entity;
5570 /* Only give an error if a Value_Size clause was explicitly given.
5571 Otherwise, we'd be duplicating an error on the Size clause. */
5572 Node_Id gnat_attr_node
5573 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5574 tree old_size = rm_size (gnu_type);
5577 /* Get the size as a tree. Do nothing if none was specified, either
5578 because RM_Size was not Present or if the specified size was zero.
5579 Give an error if a size was specified, but cannot be represented as
5581 if (No (uint_size) || uint_size == No_Uint)
5584 size = UI_To_gnu (uint_size, bitsizetype);
5585 if (TREE_OVERFLOW (size))
5587 if (Present (gnat_attr_node))
5588 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5594 /* Ignore a negative size since that corresponds to our back-annotation.
5595 Also ignore a zero size unless a size clause exists, a Value_Size
5596 clause exists, or this is an integer type, in which case the
5597 front end will have always set it. */
5598 else if (tree_int_cst_sgn (size) < 0
5599 || (integer_zerop (size) && No (gnat_attr_node)
5600 && ! Has_Size_Clause (gnat_entity)
5601 && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5604 /* If the old size is self-referential, get the maximum size. */
5605 if (TREE_CODE (old_size) != INTEGER_CST
5606 && contains_placeholder_p (old_size))
5607 old_size = max_size (old_size, 1);
5609 /* If the size of the object is a constant, the new size must not be
5610 smaller (the front end checks this for scalar types). */
5611 if (TREE_CODE (old_size) != INTEGER_CST
5612 || TREE_OVERFLOW (old_size)
5613 || (AGGREGATE_TYPE_P (gnu_type)
5614 && tree_int_cst_lt (size, old_size)))
5616 if (Present (gnat_attr_node))
5618 ("Value_Size for& too small{, minimum allowed is ^}",
5619 gnat_attr_node, gnat_entity, old_size);
5624 /* Otherwise, set the RM_Size. */
5625 if (TREE_CODE (gnu_type) == INTEGER_TYPE
5626 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5627 TYPE_RM_SIZE_INT (gnu_type) = size;
5628 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
5629 TYPE_RM_SIZE_ENUM (gnu_type) = size;
5630 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5631 || TREE_CODE (gnu_type) == UNION_TYPE
5632 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5633 && ! TYPE_IS_FAT_POINTER_P (gnu_type))
5634 TYPE_ADA_SIZE (gnu_type) = size;
5637 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5638 If TYPE is the best type, return it. Otherwise, make a new type. We
5639 only support new integral and pointer types. BIASED_P is nonzero if
5640 we are making a biased type. */
5643 make_type_from_size (type, size_tree, biased_p)
5649 unsigned HOST_WIDE_INT size;
5651 /* If size indicates an error, just return TYPE to avoid propagating the
5652 error. Likewise if it's too large to represent. */
5653 if (size_tree == 0 || ! host_integerp (size_tree, 1))
5656 size = tree_low_cst (size_tree, 1);
5657 switch (TREE_CODE (type))
5661 /* Only do something if the type is not already the proper size and is
5662 not a packed array type. */
5663 if (TYPE_PACKED_ARRAY_TYPE_P (type)
5664 || (TYPE_PRECISION (type) == size
5665 && biased_p == (TREE_CODE (type) == INTEGER_CST
5666 && TYPE_BIASED_REPRESENTATION_P (type))))
5669 size = MIN (size, LONG_LONG_TYPE_SIZE);
5670 new_type = make_signed_type (size);
5671 TREE_TYPE (new_type)
5672 = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
5673 TYPE_MIN_VALUE (new_type)
5674 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
5675 TYPE_MAX_VALUE (new_type)
5676 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
5677 TYPE_BIASED_REPRESENTATION_P (new_type)
5678 = ((TREE_CODE (type) == INTEGER_TYPE
5679 && TYPE_BIASED_REPRESENTATION_P (type))
5681 TREE_UNSIGNED (new_type)
5682 = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
5683 TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
5687 /* Do something if this is a fat pointer, in which case we
5688 may need to return the thin pointer. */
5689 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
5692 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
5696 /* Only do something if this is a thin pointer, in which case we
5697 may need to return the fat pointer. */
5698 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
5700 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
5711 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
5712 a type or object whose present alignment is ALIGN. If this alignment is
5713 valid, return it. Otherwise, give an error and return ALIGN. */
5716 validate_alignment (alignment, gnat_entity, align)
5718 Entity_Id gnat_entity;
5721 Node_Id gnat_error_node = gnat_entity;
5722 unsigned int new_align;
5724 #ifndef MAX_OFILE_ALIGNMENT
5725 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
5728 if (Present (Alignment_Clause (gnat_entity)))
5729 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
5731 /* Within GCC, an alignment is an integer, so we must make sure a
5732 value is specified that fits in that range. Also, alignments of
5733 more than MAX_OFILE_ALIGNMENT can't be supported. */
5735 if (! UI_Is_In_Int_Range (alignment)
5736 || ((new_align = UI_To_Int (alignment))
5737 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
5738 post_error_ne_num ("largest supported alignment for& is ^",
5739 gnat_error_node, gnat_entity,
5740 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
5741 else if (! (Present (Alignment_Clause (gnat_entity))
5742 && From_At_Mod (Alignment_Clause (gnat_entity)))
5743 && new_align * BITS_PER_UNIT < align)
5744 post_error_ne_num ("alignment for& must be at least ^",
5745 gnat_error_node, gnat_entity,
5746 align / BITS_PER_UNIT);
5748 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
5753 /* Verify that OBJECT, a type or decl, is something we can implement
5754 atomically. If not, give an error for GNAT_ENTITY. COMP_P is nonzero
5755 if we require atomic components. */
5758 check_ok_for_atomic (object, gnat_entity, comp_p)
5760 Entity_Id gnat_entity;
5763 Node_Id gnat_error_point = gnat_entity;
5765 enum machine_mode mode;
5769 /* There are three case of what OBJECT can be. It can be a type, in which
5770 case we take the size, alignment and mode from the type. It can be a
5771 declaration that was indirect, in which case the relevant values are
5772 that of the type being pointed to, or it can be a normal declaration,
5773 in which case the values are of the decl. The code below assumes that
5774 OBJECT is either a type or a decl. */
5775 if (TYPE_P (object))
5777 mode = TYPE_MODE (object);
5778 align = TYPE_ALIGN (object);
5779 size = TYPE_SIZE (object);
5781 else if (DECL_BY_REF_P (object))
5783 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
5784 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
5785 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
5789 mode = DECL_MODE (object);
5790 align = DECL_ALIGN (object);
5791 size = DECL_SIZE (object);
5794 /* Consider all floating-point types atomic and any types that that are
5795 represented by integers no wider than a machine word. */
5796 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5797 || ((GET_MODE_CLASS (mode) == MODE_INT
5798 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5799 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
5802 /* For the moment, also allow anything that has an alignment equal
5803 to its size and which is smaller than a word. */
5804 if (TREE_CODE (size) == INTEGER_CST
5805 && compare_tree_int (size, align) == 0
5806 && align <= BITS_PER_WORD)
5809 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
5810 gnat_node = Next_Rep_Item (gnat_node))
5812 if (! comp_p && Nkind (gnat_node) == N_Pragma
5813 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
5814 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5815 else if (comp_p && Nkind (gnat_node) == N_Pragma
5816 && (Get_Pragma_Id (Chars (gnat_node))
5817 == Pragma_Atomic_Components))
5818 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5822 post_error_ne ("atomic access to component of & cannot be guaranteed",
5823 gnat_error_point, gnat_entity);
5825 post_error_ne ("atomic access to & cannot be guaranteed",
5826 gnat_error_point, gnat_entity);
5829 /* Given a type T, a FIELD_DECL F, and a replacement value R,
5830 return a new type with all size expressions that contain F
5831 updated by replacing F with R. This is identical to GCC's
5832 substitute_in_type except that it knows about TYPE_INDEX_TYPE.
5833 If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
5837 gnat_substitute_in_type (t, f, r)
5843 switch (TREE_CODE (t))
5849 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
5850 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5851 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
5852 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5854 tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5855 tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5857 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5860 new = build_range_type (TREE_TYPE (t), low, high);
5861 if (TYPE_INDEX_TYPE (t))
5862 TYPE_INDEX_TYPE (new)
5863 = gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r);
5870 if ((TYPE_MIN_VALUE (t) != 0
5871 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
5872 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5873 || (TYPE_MAX_VALUE (t) != 0
5874 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
5875 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5877 tree low = 0, high = 0;
5879 if (TYPE_MIN_VALUE (t))
5880 low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5881 if (TYPE_MAX_VALUE (t))
5882 high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5884 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5888 TYPE_MIN_VALUE (t) = low;
5889 TYPE_MAX_VALUE (t) = high;
5894 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5895 if (tem == TREE_TYPE (t))
5898 return build_complex_type (tem);
5906 /* Don't know how to do these yet. */
5911 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5912 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
5914 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5917 new = build_array_type (component, domain);
5918 TYPE_SIZE (new) = 0;
5919 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
5920 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
5922 TYPE_ALIGN (new) = TYPE_ALIGN (t);
5928 case QUAL_UNION_TYPE:
5932 = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
5933 int field_has_rep = 0;
5934 tree last_field = 0;
5936 tree new = copy_type (t);
5938 /* Start out with no fields, make new fields, and chain them
5939 in. If we haven't actually changed the type of any field,
5940 discard everything we've done and return the old type. */
5942 TYPE_FIELDS (new) = 0;
5943 TYPE_SIZE (new) = 0;
5945 for (field = TYPE_FIELDS (t); field;
5946 field = TREE_CHAIN (field))
5948 tree new_field = copy_node (field);
5950 TREE_TYPE (new_field)
5951 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
5953 if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
5955 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
5958 /* If this is an internal field and the type of this field is
5959 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
5960 the type just has one element, treat that as the field.
5961 But don't do this if we are processing a QUAL_UNION_TYPE. */
5962 if (TREE_CODE (t) != QUAL_UNION_TYPE
5963 && DECL_INTERNAL_P (new_field)
5964 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
5965 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
5967 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
5970 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
5973 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
5975 /* Make sure omitting the union doesn't change
5977 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
5978 new_field = next_new_field;
5982 DECL_CONTEXT (new_field) = new;
5983 DECL_ORIGINAL_FIELD (new_field)
5984 = DECL_ORIGINAL_FIELD (field) != 0
5985 ? DECL_ORIGINAL_FIELD (field) : field;
5987 /* If the size of the old field was set at a constant,
5988 propagate the size in case the type's size was variable.
5989 (This occurs in the case of a variant or discriminated
5990 record with a default size used as a field of another
5992 DECL_SIZE (new_field)
5993 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
5994 ? DECL_SIZE (field) : 0;
5995 DECL_SIZE_UNIT (new_field)
5996 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
5997 ? DECL_SIZE_UNIT (field) : 0;
5999 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6001 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6003 if (new_q != DECL_QUALIFIER (new_field))
6006 /* Do the substitution inside the qualifier and if we find
6007 that this field will not be present, omit it. */
6008 DECL_QUALIFIER (new_field) = new_q;
6010 if (integer_zerop (DECL_QUALIFIER (new_field)))
6014 if (last_field == 0)
6015 TYPE_FIELDS (new) = new_field;
6017 TREE_CHAIN (last_field) = new_field;
6019 last_field = new_field;
6021 /* If this is a qualified type and this field will always be
6022 present, we are done. */
6023 if (TREE_CODE (t) == QUAL_UNION_TYPE
6024 && integer_onep (DECL_QUALIFIER (new_field)))
6028 /* If this used to be a qualified union type, but we now know what
6029 field will be present, make this a normal union. */
6030 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6031 && (TYPE_FIELDS (new) == 0
6032 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6033 TREE_SET_CODE (new, UNION_TYPE);
6034 else if (! changed_field)
6042 /* If the size was originally a constant use it. */
6043 if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6044 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6046 TYPE_SIZE (new) = TYPE_SIZE (t);
6047 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6048 TYPE_ADA_SIZE (new) = TYPE_ADA_SIZE (t);
6059 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6060 needed to represent the object. */
6066 /* For integer types, this is the precision. For record types, we store
6067 the size explicitly. For other types, this is just the size. */
6069 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6070 return TYPE_RM_SIZE (gnu_type);
6071 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6072 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6073 /* Return the rm_size of the actual data plus the size of the template. */
6075 size_binop (PLUS_EXPR,
6076 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6077 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6078 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6079 || TREE_CODE (gnu_type) == UNION_TYPE
6080 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6081 && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6082 && TYPE_ADA_SIZE (gnu_type) != 0)
6083 return TYPE_ADA_SIZE (gnu_type);
6085 return TYPE_SIZE (gnu_type);
6088 /* Return an identifier representing the external name to be used for
6089 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6090 and the specified suffix. */
6093 create_concat_name (gnat_entity, suffix)
6094 Entity_Id gnat_entity;
6097 const char *str = (suffix == 0 ? "" : suffix);
6098 String_Template temp = {1, strlen (str)};
6099 Fat_Pointer fp = {str, &temp};
6101 Get_External_Name_With_Suffix (gnat_entity, fp);
6103 return get_identifier (Name_Buffer);
6106 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6107 fully-qualified name, possibly with type information encoding.
6108 Otherwise, return the name. */
6111 get_entity_name (gnat_entity)
6112 Entity_Id gnat_entity;
6114 Get_Encoded_Name (gnat_entity);
6115 return get_identifier (Name_Buffer);
6118 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6119 string, return a new IDENTIFIER_NODE that is the concatenation of
6120 the name in GNU_ID and SUFFIX. */
6123 concat_id_with_name (gnu_id, suffix)
6127 int len = IDENTIFIER_LENGTH (gnu_id);
6129 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6130 IDENTIFIER_LENGTH (gnu_id));
6131 strncpy (Name_Buffer + len, "___", 3);
6133 strcpy (Name_Buffer + len, suffix);
6134 return get_identifier (Name_Buffer);