1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Aggr; use Exp_Aggr;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Smem; use Exp_Smem;
40 with Exp_Strm; use Exp_Strm;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Nlists; use Nlists;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Attr; use Sem_Attr;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Mech; use Sem_Mech;
61 with Sem_Res; use Sem_Res;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sinfo; use Sinfo;
65 with Stand; use Stand;
66 with Snames; use Snames;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Validsw; use Validsw;
72 package body Exp_Ch3 is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id;
79 -- Add the declaration of a finalization list to the freeze actions for
80 -- Def_Id, and return its defining identifier.
82 procedure Adjust_Discriminants (Rtype : Entity_Id);
83 -- This is used when freezing a record type. It attempts to construct
84 -- more restrictive subtypes for discriminants so that the max size of
85 -- the record can be calculated more accurately. See the body of this
86 -- procedure for details.
88 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
89 -- Build initialization procedure for given array type. Nod is a node
90 -- used for attachment of any actions required in its construction.
91 -- It also supplies the source location used for the procedure.
93 function Build_Discriminant_Formals
95 Use_Dl : Boolean) return List_Id;
96 -- This function uses the discriminants of a type to build a list of
97 -- formal parameters, used in the following function. If the flag Use_Dl
98 -- is set, the list is built using the already defined discriminals
99 -- of the type. Otherwise new identifiers are created, with the source
100 -- names of the discriminants.
102 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
103 -- This function builds a static aggregate that can serve as the initial
104 -- value for an array type whose bounds are static, and whose component
105 -- type is a composite type that has a static equivalent aggregate.
106 -- The equivalent array aggregate is used both for object initialization
107 -- and for component initialization, when used in the following function.
109 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
110 -- This function builds a static aggregate that can serve as the initial
111 -- value for a record type whose components are scalar and initialized
112 -- with compile-time values, or arrays with similar initialization or
113 -- defaults. When possible, initialization of an object of the type can
114 -- be achieved by using a copy of the aggregate as an initial value, thus
115 -- removing the implicit call that would otherwise constitute elaboration
118 function Build_Master_Renaming
120 T : Entity_Id) return Entity_Id;
121 -- If the designated type of an access type is a task type or contains
122 -- tasks, we make sure that a _Master variable is declared in the current
123 -- scope, and then declare a renaming for it:
125 -- atypeM : Master_Id renames _Master;
127 -- where atyp is the name of the access type. This declaration is used when
128 -- an allocator for the access type is expanded. The node is the full
129 -- declaration of the designated type that contains tasks. The renaming
130 -- declaration is inserted before N, and after the Master declaration.
132 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id);
133 -- Build record initialization procedure. N is the type declaration
134 -- node, and Pe is the corresponding entity for the record type.
136 procedure Build_Slice_Assignment (Typ : Entity_Id);
137 -- Build assignment procedure for one-dimensional arrays of controlled
138 -- types. Other array and slice assignments are expanded in-line, but
139 -- the code expansion for controlled components (when control actions
140 -- are active) can lead to very large blocks that GCC3 handles poorly.
142 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
143 -- Create An Equality function for the non-tagged variant record 'Typ'
144 -- and attach it to the TSS list
146 procedure Check_Stream_Attributes (Typ : Entity_Id);
147 -- Check that if a limited extension has a parent with user-defined stream
148 -- attributes, and does not itself have user-defined stream-attributes,
149 -- then any limited component of the extension also has the corresponding
150 -- user-defined stream attributes.
152 procedure Clean_Task_Names
154 Proc_Id : Entity_Id);
155 -- If an initialization procedure includes calls to generate names
156 -- for task subcomponents, indicate that secondary stack cleanup is
157 -- needed after an initialization. Typ is the component type, and Proc_Id
158 -- the initialization procedure for the enclosing composite type.
160 procedure Expand_Tagged_Root (T : Entity_Id);
161 -- Add a field _Tag at the beginning of the record. This field carries
162 -- the value of the access to the Dispatch table. This procedure is only
163 -- called on root type, the _Tag field being inherited by the descendants.
165 procedure Expand_Record_Controller (T : Entity_Id);
166 -- T must be a record type that Has_Controlled_Component. Add a field
167 -- _controller of type Record_Controller or Limited_Record_Controller
170 procedure Expand_Freeze_Array_Type (N : Node_Id);
171 -- Freeze an array type. Deals with building the initialization procedure,
172 -- creating the packed array type for a packed array and also with the
173 -- creation of the controlling procedures for the controlled case. The
174 -- argument N is the N_Freeze_Entity node for the type.
176 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
177 -- Freeze enumeration type with non-standard representation. Builds the
178 -- array and function needed to convert between enumeration pos and
179 -- enumeration representation values. N is the N_Freeze_Entity node
182 procedure Expand_Freeze_Record_Type (N : Node_Id);
183 -- Freeze record type. Builds all necessary discriminant checking
184 -- and other ancillary functions, and builds dispatch tables where
185 -- needed. The argument N is the N_Freeze_Entity node. This processing
186 -- applies only to E_Record_Type entities, not to class wide types,
187 -- record subtypes, or private types.
189 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
190 -- Treat user-defined stream operations as renaming_as_body if the
191 -- subprogram they rename is not frozen when the type is frozen.
193 procedure Initialization_Warning (E : Entity_Id);
194 -- If static elaboration of the package is requested, indicate
195 -- when a type does meet the conditions for static initialization. If
196 -- E is a type, it has components that have no static initialization.
197 -- if E is an entity, its initial expression is not compile-time known.
199 function Init_Formals (Typ : Entity_Id) return List_Id;
200 -- This function builds the list of formals for an initialization routine.
201 -- The first formal is always _Init with the given type. For task value
202 -- record types and types containing tasks, three additional formals are
205 -- _Master : Master_Id
206 -- _Chain : in out Activation_Chain
207 -- _Task_Name : String
209 -- The caller must append additional entries for discriminants if required.
211 function In_Runtime (E : Entity_Id) return Boolean;
212 -- Check if E is defined in the RTL (in a child of Ada or System). Used
213 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
215 function Is_Variable_Size_Record (E : Entity_Id) return Boolean;
216 -- Returns true if E has variable size components
218 function Make_Eq_Case
221 Discr : Entity_Id := Empty) return List_Id;
222 -- Building block for variant record equality. Defined to share the code
223 -- between the tagged and non-tagged case. Given a Component_List node CL,
224 -- it generates an 'if' followed by a 'case' statement that compares all
225 -- components of local temporaries named X and Y (that are declared as
226 -- formals at some upper level). E provides the Sloc to be used for the
227 -- generated code. Discr is used as the case statement switch in the case
228 -- of Unchecked_Union equality.
232 L : List_Id) return Node_Id;
233 -- Building block for variant record equality. Defined to share the code
234 -- between the tagged and non-tagged case. Given the list of components
235 -- (or discriminants) L, it generates a return statement that compares all
236 -- components of local temporaries named X and Y (that are declared as
237 -- formals at some upper level). E provides the Sloc to be used for the
240 procedure Make_Predefined_Primitive_Specs
241 (Tag_Typ : Entity_Id;
242 Predef_List : out List_Id;
243 Renamed_Eq : out Entity_Id);
244 -- Create a list with the specs of the predefined primitive operations.
245 -- For tagged types that are interfaces all these primitives are defined
248 -- The following entries are present for all tagged types, and provide
249 -- the results of the corresponding attribute applied to the object.
250 -- Dispatching is required in general, since the result of the attribute
251 -- will vary with the actual object subtype.
253 -- _alignment provides result of 'Alignment attribute
254 -- _size provides result of 'Size attribute
255 -- typSR provides result of 'Read attribute
256 -- typSW provides result of 'Write attribute
257 -- typSI provides result of 'Input attribute
258 -- typSO provides result of 'Output attribute
260 -- The following entries are additionally present for non-limited tagged
261 -- types, and implement additional dispatching operations for predefined
264 -- _equality implements "=" operator
265 -- _assign implements assignment operation
266 -- typDF implements deep finalization
267 -- typDA implements deep adjust
269 -- The latter two are empty procedures unless the type contains some
270 -- controlled components that require finalization actions (the deep
271 -- in the name refers to the fact that the action applies to components).
273 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
274 -- returns the value Empty, or else the defining unit name for the
275 -- predefined equality function in the case where the type has a primitive
276 -- operation that is a renaming of predefined equality (but only if there
277 -- is also an overriding user-defined equality function). The returned
278 -- Renamed_Eq will be passed to the corresponding parameter of
279 -- Predefined_Primitive_Bodies.
281 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
282 -- returns True if there are representation clauses for type T that are not
283 -- inherited. If the result is false, the init_proc and the discriminant
284 -- checking functions of the parent can be reused by a derived type.
286 procedure Make_Controlling_Function_Wrappers
287 (Tag_Typ : Entity_Id;
288 Decl_List : out List_Id;
289 Body_List : out List_Id);
290 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
291 -- associated with inherited functions with controlling results which
292 -- are not overridden. The body of each wrapper function consists solely
293 -- of a return statement whose expression is an extension aggregate
294 -- invoking the inherited subprogram's parent subprogram and extended
295 -- with a null association list.
297 procedure Make_Null_Procedure_Specs
298 (Tag_Typ : Entity_Id;
299 Decl_List : out List_Id);
300 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
301 -- null procedures inherited from an interface type that have not been
302 -- overridden. Only one null procedure will be created for a given set of
303 -- inherited null procedures with homographic profiles.
305 function Predef_Spec_Or_Body
310 Ret_Type : Entity_Id := Empty;
311 For_Body : Boolean := False) return Node_Id;
312 -- This function generates the appropriate expansion for a predefined
313 -- primitive operation specified by its name, parameter profile and
314 -- return type (Empty means this is a procedure). If For_Body is false,
315 -- then the returned node is a subprogram declaration. If For_Body is
316 -- true, then the returned node is a empty subprogram body containing
317 -- no declarations and no statements.
319 function Predef_Stream_Attr_Spec
322 Name : TSS_Name_Type;
323 For_Body : Boolean := False) return Node_Id;
324 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
325 -- input and output attribute whose specs are constructed in Exp_Strm.
327 function Predef_Deep_Spec
330 Name : TSS_Name_Type;
331 For_Body : Boolean := False) return Node_Id;
332 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
333 -- and _deep_finalize
335 function Predefined_Primitive_Bodies
336 (Tag_Typ : Entity_Id;
337 Renamed_Eq : Entity_Id) return List_Id;
338 -- Create the bodies of the predefined primitives that are described in
339 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
340 -- the defining unit name of the type's predefined equality as returned
341 -- by Make_Predefined_Primitive_Specs.
343 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
344 -- Freeze entities of all predefined primitive operations. This is needed
345 -- because the bodies of these operations do not normally do any freezing.
347 function Stream_Operation_OK
349 Operation : TSS_Name_Type) return Boolean;
350 -- Check whether the named stream operation must be emitted for a given
351 -- type. The rules for inheritance of stream attributes by type extensions
352 -- are enforced by this function. Furthermore, various restrictions prevent
353 -- the generation of these operations, as a useful optimization or for
354 -- certification purposes.
356 ---------------------
357 -- Add_Final_Chain --
358 ---------------------
360 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id is
361 Loc : constant Source_Ptr := Sloc (Def_Id);
366 Make_Defining_Identifier (Loc,
367 New_External_Name (Chars (Def_Id), 'L'));
369 Append_Freeze_Action (Def_Id,
370 Make_Object_Declaration (Loc,
371 Defining_Identifier => Flist,
373 New_Reference_To (RTE (RE_List_Controller), Loc)));
378 --------------------------
379 -- Adjust_Discriminants --
380 --------------------------
382 -- This procedure attempts to define subtypes for discriminants that are
383 -- more restrictive than those declared. Such a replacement is possible if
384 -- we can demonstrate that values outside the restricted range would cause
385 -- constraint errors in any case. The advantage of restricting the
386 -- discriminant types in this way is that the maximum size of the variant
387 -- record can be calculated more conservatively.
389 -- An example of a situation in which we can perform this type of
390 -- restriction is the following:
392 -- subtype B is range 1 .. 10;
393 -- type Q is array (B range <>) of Integer;
395 -- type V (N : Natural) is record
399 -- In this situation, we can restrict the upper bound of N to 10, since
400 -- any larger value would cause a constraint error in any case.
402 -- There are many situations in which such restriction is possible, but
403 -- for now, we just look for cases like the above, where the component
404 -- in question is a one dimensional array whose upper bound is one of
405 -- the record discriminants. Also the component must not be part of
406 -- any variant part, since then the component does not always exist.
408 procedure Adjust_Discriminants (Rtype : Entity_Id) is
409 Loc : constant Source_Ptr := Sloc (Rtype);
426 Comp := First_Component (Rtype);
427 while Present (Comp) loop
429 -- If our parent is a variant, quit, we do not look at components
430 -- that are in variant parts, because they may not always exist.
432 P := Parent (Comp); -- component declaration
433 P := Parent (P); -- component list
435 exit when Nkind (Parent (P)) = N_Variant;
437 -- We are looking for a one dimensional array type
439 Ctyp := Etype (Comp);
441 if not Is_Array_Type (Ctyp)
442 or else Number_Dimensions (Ctyp) > 1
447 -- The lower bound must be constant, and the upper bound is a
448 -- discriminant (which is a discriminant of the current record).
450 Ityp := Etype (First_Index (Ctyp));
451 Lo := Type_Low_Bound (Ityp);
452 Hi := Type_High_Bound (Ityp);
454 if not Compile_Time_Known_Value (Lo)
455 or else Nkind (Hi) /= N_Identifier
456 or else No (Entity (Hi))
457 or else Ekind (Entity (Hi)) /= E_Discriminant
462 -- We have an array with appropriate bounds
464 Loval := Expr_Value (Lo);
465 Discr := Entity (Hi);
466 Dtyp := Etype (Discr);
468 -- See if the discriminant has a known upper bound
470 Dhi := Type_High_Bound (Dtyp);
472 if not Compile_Time_Known_Value (Dhi) then
476 Dhiv := Expr_Value (Dhi);
478 -- See if base type of component array has known upper bound
480 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
482 if not Compile_Time_Known_Value (Ahi) then
486 Ahiv := Expr_Value (Ahi);
488 -- The condition for doing the restriction is that the high bound
489 -- of the discriminant is greater than the low bound of the array,
490 -- and is also greater than the high bound of the base type index.
492 if Dhiv > Loval and then Dhiv > Ahiv then
494 -- We can reset the upper bound of the discriminant type to
495 -- whichever is larger, the low bound of the component, or
496 -- the high bound of the base type array index.
498 -- We build a subtype that is declared as
500 -- subtype Tnn is discr_type range discr_type'First .. max;
502 -- And insert this declaration into the tree. The type of the
503 -- discriminant is then reset to this more restricted subtype.
505 Tnn := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
507 Insert_Action (Declaration_Node (Rtype),
508 Make_Subtype_Declaration (Loc,
509 Defining_Identifier => Tnn,
510 Subtype_Indication =>
511 Make_Subtype_Indication (Loc,
512 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
514 Make_Range_Constraint (Loc,
518 Make_Attribute_Reference (Loc,
519 Attribute_Name => Name_First,
520 Prefix => New_Occurrence_Of (Dtyp, Loc)),
522 Make_Integer_Literal (Loc,
523 Intval => UI_Max (Loval, Ahiv)))))));
525 Set_Etype (Discr, Tnn);
529 Next_Component (Comp);
531 end Adjust_Discriminants;
533 ---------------------------
534 -- Build_Array_Init_Proc --
535 ---------------------------
537 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
538 Loc : constant Source_Ptr := Sloc (Nod);
539 Comp_Type : constant Entity_Id := Component_Type (A_Type);
540 Index_List : List_Id;
542 Body_Stmts : List_Id;
543 Has_Default_Init : Boolean;
545 function Init_Component return List_Id;
546 -- Create one statement to initialize one array component, designated
547 -- by a full set of indices.
549 function Init_One_Dimension (N : Int) return List_Id;
550 -- Create loop to initialize one dimension of the array. The single
551 -- statement in the loop body initializes the inner dimensions if any,
552 -- or else the single component. Note that this procedure is called
553 -- recursively, with N being the dimension to be initialized. A call
554 -- with N greater than the number of dimensions simply generates the
555 -- component initialization, terminating the recursion.
561 function Init_Component return List_Id is
566 Make_Indexed_Component (Loc,
567 Prefix => Make_Identifier (Loc, Name_uInit),
568 Expressions => Index_List);
570 if Needs_Simple_Initialization (Comp_Type) then
571 Set_Assignment_OK (Comp);
573 Make_Assignment_Statement (Loc,
577 (Comp_Type, Nod, Component_Size (A_Type))));
580 Clean_Task_Names (Comp_Type, Proc_Id);
582 Build_Initialization_Call
583 (Loc, Comp, Comp_Type,
584 In_Init_Proc => True,
585 Enclos_Type => A_Type);
589 ------------------------
590 -- Init_One_Dimension --
591 ------------------------
593 function Init_One_Dimension (N : Int) return List_Id is
597 -- If the component does not need initializing, then there is nothing
598 -- to do here, so we return a null body. This occurs when generating
599 -- the dummy Init_Proc needed for Initialize_Scalars processing.
601 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
602 and then not Needs_Simple_Initialization (Comp_Type)
603 and then not Has_Task (Comp_Type)
605 return New_List (Make_Null_Statement (Loc));
607 -- If all dimensions dealt with, we simply initialize the component
609 elsif N > Number_Dimensions (A_Type) then
610 return Init_Component;
612 -- Here we generate the required loop
616 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
618 Append (New_Reference_To (Index, Loc), Index_List);
621 Make_Implicit_Loop_Statement (Nod,
624 Make_Iteration_Scheme (Loc,
625 Loop_Parameter_Specification =>
626 Make_Loop_Parameter_Specification (Loc,
627 Defining_Identifier => Index,
628 Discrete_Subtype_Definition =>
629 Make_Attribute_Reference (Loc,
630 Prefix => Make_Identifier (Loc, Name_uInit),
631 Attribute_Name => Name_Range,
632 Expressions => New_List (
633 Make_Integer_Literal (Loc, N))))),
634 Statements => Init_One_Dimension (N + 1)));
636 end Init_One_Dimension;
638 -- Start of processing for Build_Array_Init_Proc
641 -- Nothing to generate in the following cases:
643 -- 1. Initialization is suppressed for the type
644 -- 2. The type is a value type, in the CIL sense.
645 -- 3. The type has CIL/JVM convention.
646 -- 4. An initialization already exists for the base type
648 if Suppress_Init_Proc (A_Type)
649 or else Is_Value_Type (Comp_Type)
650 or else Convention (A_Type) = Convention_CIL
651 or else Convention (A_Type) = Convention_Java
652 or else Present (Base_Init_Proc (A_Type))
657 Index_List := New_List;
659 -- We need an initialization procedure if any of the following is true:
661 -- 1. The component type has an initialization procedure
662 -- 2. The component type needs simple initialization
663 -- 3. Tasks are present
664 -- 4. The type is marked as a public entity
666 -- The reason for the public entity test is to deal properly with the
667 -- Initialize_Scalars pragma. This pragma can be set in the client and
668 -- not in the declaring package, this means the client will make a call
669 -- to the initialization procedure (because one of conditions 1-3 must
670 -- apply in this case), and we must generate a procedure (even if it is
671 -- null) to satisfy the call in this case.
673 -- Exception: do not build an array init_proc for a type whose root
674 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
675 -- is no place to put the code, and in any case we handle initialization
676 -- of such types (in the Initialize_Scalars case, that's the only time
677 -- the issue arises) in a special manner anyway which does not need an
680 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
681 or else Needs_Simple_Initialization (Comp_Type)
682 or else Has_Task (Comp_Type);
685 or else (not Restriction_Active (No_Initialize_Scalars)
686 and then Is_Public (A_Type)
687 and then Root_Type (A_Type) /= Standard_String
688 and then Root_Type (A_Type) /= Standard_Wide_String
689 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
692 Make_Defining_Identifier (Loc,
693 Chars => Make_Init_Proc_Name (A_Type));
695 -- If No_Default_Initialization restriction is active, then we don't
696 -- want to build an init_proc, but we need to mark that an init_proc
697 -- would be needed if this restriction was not active (so that we can
698 -- detect attempts to call it), so set a dummy init_proc in place.
699 -- This is only done though when actual default initialization is
700 -- needed (and not done when only Is_Public is True), since otherwise
701 -- objects such as arrays of scalars could be wrongly flagged as
702 -- violating the restriction.
704 if Restriction_Active (No_Default_Initialization) then
705 if Has_Default_Init then
706 Set_Init_Proc (A_Type, Proc_Id);
712 Body_Stmts := Init_One_Dimension (1);
715 Make_Subprogram_Body (Loc,
717 Make_Procedure_Specification (Loc,
718 Defining_Unit_Name => Proc_Id,
719 Parameter_Specifications => Init_Formals (A_Type)),
720 Declarations => New_List,
721 Handled_Statement_Sequence =>
722 Make_Handled_Sequence_Of_Statements (Loc,
723 Statements => Body_Stmts)));
725 Set_Ekind (Proc_Id, E_Procedure);
726 Set_Is_Public (Proc_Id, Is_Public (A_Type));
727 Set_Is_Internal (Proc_Id);
728 Set_Has_Completion (Proc_Id);
730 if not Debug_Generated_Code then
731 Set_Debug_Info_Off (Proc_Id);
734 -- Set inlined unless controlled stuff or tasks around, in which
735 -- case we do not want to inline, because nested stuff may cause
736 -- difficulties in inter-unit inlining, and furthermore there is
737 -- in any case no point in inlining such complex init procs.
739 if not Has_Task (Proc_Id)
740 and then not Needs_Finalization (Proc_Id)
742 Set_Is_Inlined (Proc_Id);
745 -- Associate Init_Proc with type, and determine if the procedure
746 -- is null (happens because of the Initialize_Scalars pragma case,
747 -- where we have to generate a null procedure in case it is called
748 -- by a client with Initialize_Scalars set). Such procedures have
749 -- to be generated, but do not have to be called, so we mark them
750 -- as null to suppress the call.
752 Set_Init_Proc (A_Type, Proc_Id);
754 if List_Length (Body_Stmts) = 1
755 and then Nkind (First (Body_Stmts)) = N_Null_Statement
757 Set_Is_Null_Init_Proc (Proc_Id);
760 -- Try to build a static aggregate to initialize statically
761 -- objects of the type. This can only be done for constrained
762 -- one-dimensional arrays with static bounds.
764 Set_Static_Initialization
766 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
769 end Build_Array_Init_Proc;
771 -----------------------------
772 -- Build_Class_Wide_Master --
773 -----------------------------
775 procedure Build_Class_Wide_Master (T : Entity_Id) is
776 Loc : constant Source_Ptr := Sloc (T);
783 -- Nothing to do if there is no task hierarchy
785 if Restriction_Active (No_Task_Hierarchy) then
789 -- Find declaration that created the access type: either a type
790 -- declaration, or an object declaration with an access definition,
791 -- in which case the type is anonymous.
794 P := Associated_Node_For_Itype (T);
799 -- Nothing to do if we already built a master entity for this scope
801 if not Has_Master_Entity (Scope (T)) then
803 -- First build the master entity
804 -- _Master : constant Master_Id := Current_Master.all;
805 -- and insert it just before the current declaration.
808 Make_Object_Declaration (Loc,
809 Defining_Identifier =>
810 Make_Defining_Identifier (Loc, Name_uMaster),
811 Constant_Present => True,
812 Object_Definition => New_Reference_To (Standard_Integer, Loc),
814 Make_Explicit_Dereference (Loc,
815 New_Reference_To (RTE (RE_Current_Master), Loc)));
817 Insert_Action (P, Decl);
819 Set_Has_Master_Entity (Scope (T));
821 -- Now mark the containing scope as a task master. Masters
822 -- associated with return statements are already marked at
823 -- this stage (see Analyze_Subprogram_Body).
825 if Ekind (Current_Scope) /= E_Return_Statement then
827 while Nkind (Par) /= N_Compilation_Unit loop
830 -- If we fall off the top, we are at the outer level, and the
831 -- environment task is our effective master, so nothing to mark.
834 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
836 Set_Is_Task_Master (Par, True);
843 -- Now define the renaming of the master_id
846 Make_Defining_Identifier (Loc,
847 New_External_Name (Chars (T), 'M'));
850 Make_Object_Renaming_Declaration (Loc,
851 Defining_Identifier => M_Id,
852 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
853 Name => Make_Identifier (Loc, Name_uMaster));
854 Insert_Before (P, Decl);
857 Set_Master_Id (T, M_Id);
860 when RE_Not_Available =>
862 end Build_Class_Wide_Master;
864 --------------------------------
865 -- Build_Discr_Checking_Funcs --
866 --------------------------------
868 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
871 Enclosing_Func_Id : Entity_Id;
876 function Build_Case_Statement
877 (Case_Id : Entity_Id;
878 Variant : Node_Id) return Node_Id;
879 -- Build a case statement containing only two alternatives. The first
880 -- alternative corresponds exactly to the discrete choices given on the
881 -- variant with contains the components that we are generating the
882 -- checks for. If the discriminant is one of these return False. The
883 -- second alternative is an OTHERS choice that will return True
884 -- indicating the discriminant did not match.
886 function Build_Dcheck_Function
887 (Case_Id : Entity_Id;
888 Variant : Node_Id) return Entity_Id;
889 -- Build the discriminant checking function for a given variant
891 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
892 -- Builds the discriminant checking function for each variant of the
893 -- given variant part of the record type.
895 --------------------------
896 -- Build_Case_Statement --
897 --------------------------
899 function Build_Case_Statement
900 (Case_Id : Entity_Id;
901 Variant : Node_Id) return Node_Id
903 Alt_List : constant List_Id := New_List;
904 Actuals_List : List_Id;
906 Case_Alt_Node : Node_Id;
908 Choice_List : List_Id;
910 Return_Node : Node_Id;
913 Case_Node := New_Node (N_Case_Statement, Loc);
915 -- Replace the discriminant which controls the variant, with the name
916 -- of the formal of the checking function.
918 Set_Expression (Case_Node,
919 Make_Identifier (Loc, Chars (Case_Id)));
921 Choice := First (Discrete_Choices (Variant));
923 if Nkind (Choice) = N_Others_Choice then
924 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
926 Choice_List := New_Copy_List (Discrete_Choices (Variant));
929 if not Is_Empty_List (Choice_List) then
930 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
931 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
933 -- In case this is a nested variant, we need to return the result
934 -- of the discriminant checking function for the immediately
935 -- enclosing variant.
937 if Present (Enclosing_Func_Id) then
938 Actuals_List := New_List;
940 D := First_Discriminant (Rec_Id);
941 while Present (D) loop
942 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
943 Next_Discriminant (D);
947 Make_Simple_Return_Statement (Loc,
949 Make_Function_Call (Loc,
951 New_Reference_To (Enclosing_Func_Id, Loc),
952 Parameter_Associations =>
957 Make_Simple_Return_Statement (Loc,
959 New_Reference_To (Standard_False, Loc));
962 Set_Statements (Case_Alt_Node, New_List (Return_Node));
963 Append (Case_Alt_Node, Alt_List);
966 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
967 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
968 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
971 Make_Simple_Return_Statement (Loc,
973 New_Reference_To (Standard_True, Loc));
975 Set_Statements (Case_Alt_Node, New_List (Return_Node));
976 Append (Case_Alt_Node, Alt_List);
978 Set_Alternatives (Case_Node, Alt_List);
980 end Build_Case_Statement;
982 ---------------------------
983 -- Build_Dcheck_Function --
984 ---------------------------
986 function Build_Dcheck_Function
987 (Case_Id : Entity_Id;
988 Variant : Node_Id) return Entity_Id
992 Parameter_List : List_Id;
996 Body_Node := New_Node (N_Subprogram_Body, Loc);
997 Sequence := Sequence + 1;
1000 Make_Defining_Identifier (Loc,
1001 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1003 Spec_Node := New_Node (N_Function_Specification, Loc);
1004 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1006 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1008 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1009 Set_Result_Definition (Spec_Node,
1010 New_Reference_To (Standard_Boolean, Loc));
1011 Set_Specification (Body_Node, Spec_Node);
1012 Set_Declarations (Body_Node, New_List);
1014 Set_Handled_Statement_Sequence (Body_Node,
1015 Make_Handled_Sequence_Of_Statements (Loc,
1016 Statements => New_List (
1017 Build_Case_Statement (Case_Id, Variant))));
1019 Set_Ekind (Func_Id, E_Function);
1020 Set_Mechanism (Func_Id, Default_Mechanism);
1021 Set_Is_Inlined (Func_Id, True);
1022 Set_Is_Pure (Func_Id, True);
1023 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1024 Set_Is_Internal (Func_Id, True);
1026 if not Debug_Generated_Code then
1027 Set_Debug_Info_Off (Func_Id);
1030 Analyze (Body_Node);
1032 Append_Freeze_Action (Rec_Id, Body_Node);
1033 Set_Dcheck_Function (Variant, Func_Id);
1035 end Build_Dcheck_Function;
1037 ----------------------------
1038 -- Build_Dcheck_Functions --
1039 ----------------------------
1041 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1042 Component_List_Node : Node_Id;
1044 Discr_Name : Entity_Id;
1045 Func_Id : Entity_Id;
1047 Saved_Enclosing_Func_Id : Entity_Id;
1050 -- Build the discriminant-checking function for each variant, and
1051 -- label all components of that variant with the function's name.
1052 -- We only Generate a discriminant-checking function when the
1053 -- variant is not empty, to prevent the creation of dead code.
1054 -- The exception to that is when Frontend_Layout_On_Target is set,
1055 -- because the variant record size function generated in package
1056 -- Layout needs to generate calls to all discriminant-checking
1057 -- functions, including those for empty variants.
1059 Discr_Name := Entity (Name (Variant_Part_Node));
1060 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1062 while Present (Variant) loop
1063 Component_List_Node := Component_List (Variant);
1065 if not Null_Present (Component_List_Node)
1066 or else Frontend_Layout_On_Target
1068 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1070 First_Non_Pragma (Component_Items (Component_List_Node));
1072 while Present (Decl) loop
1073 Set_Discriminant_Checking_Func
1074 (Defining_Identifier (Decl), Func_Id);
1076 Next_Non_Pragma (Decl);
1079 if Present (Variant_Part (Component_List_Node)) then
1080 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1081 Enclosing_Func_Id := Func_Id;
1082 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1083 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1087 Next_Non_Pragma (Variant);
1089 end Build_Dcheck_Functions;
1091 -- Start of processing for Build_Discr_Checking_Funcs
1094 -- Only build if not done already
1096 if not Discr_Check_Funcs_Built (N) then
1097 Type_Def := Type_Definition (N);
1099 if Nkind (Type_Def) = N_Record_Definition then
1100 if No (Component_List (Type_Def)) then -- null record.
1103 V := Variant_Part (Component_List (Type_Def));
1106 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1107 if No (Component_List (Record_Extension_Part (Type_Def))) then
1111 (Component_List (Record_Extension_Part (Type_Def)));
1115 Rec_Id := Defining_Identifier (N);
1117 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1119 Enclosing_Func_Id := Empty;
1120 Build_Dcheck_Functions (V);
1123 Set_Discr_Check_Funcs_Built (N);
1125 end Build_Discr_Checking_Funcs;
1127 --------------------------------
1128 -- Build_Discriminant_Formals --
1129 --------------------------------
1131 function Build_Discriminant_Formals
1132 (Rec_Id : Entity_Id;
1133 Use_Dl : Boolean) return List_Id
1135 Loc : Source_Ptr := Sloc (Rec_Id);
1136 Parameter_List : constant List_Id := New_List;
1139 Param_Spec_Node : Node_Id;
1142 if Has_Discriminants (Rec_Id) then
1143 D := First_Discriminant (Rec_Id);
1144 while Present (D) loop
1148 Formal := Discriminal (D);
1150 Formal := Make_Defining_Identifier (Loc, Chars (D));
1154 Make_Parameter_Specification (Loc,
1155 Defining_Identifier => Formal,
1157 New_Reference_To (Etype (D), Loc));
1158 Append (Param_Spec_Node, Parameter_List);
1159 Next_Discriminant (D);
1163 return Parameter_List;
1164 end Build_Discriminant_Formals;
1166 --------------------------------------
1167 -- Build_Equivalent_Array_Aggregate --
1168 --------------------------------------
1170 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1171 Loc : constant Source_Ptr := Sloc (T);
1172 Comp_Type : constant Entity_Id := Component_Type (T);
1173 Index_Type : constant Entity_Id := Etype (First_Index (T));
1174 Proc : constant Entity_Id := Base_Init_Proc (T);
1180 if not Is_Constrained (T)
1181 or else Number_Dimensions (T) > 1
1184 Initialization_Warning (T);
1188 Lo := Type_Low_Bound (Index_Type);
1189 Hi := Type_High_Bound (Index_Type);
1191 if not Compile_Time_Known_Value (Lo)
1192 or else not Compile_Time_Known_Value (Hi)
1194 Initialization_Warning (T);
1198 if Is_Record_Type (Comp_Type)
1199 and then Present (Base_Init_Proc (Comp_Type))
1201 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1204 Initialization_Warning (T);
1209 Initialization_Warning (T);
1213 Aggr := Make_Aggregate (Loc, No_List, New_List);
1214 Set_Etype (Aggr, T);
1215 Set_Aggregate_Bounds (Aggr,
1217 Low_Bound => New_Copy (Lo),
1218 High_Bound => New_Copy (Hi)));
1219 Set_Parent (Aggr, Parent (Proc));
1221 Append_To (Component_Associations (Aggr),
1222 Make_Component_Association (Loc,
1226 Low_Bound => New_Copy (Lo),
1227 High_Bound => New_Copy (Hi))),
1228 Expression => Expr));
1230 if Static_Array_Aggregate (Aggr) then
1233 Initialization_Warning (T);
1236 end Build_Equivalent_Array_Aggregate;
1238 ---------------------------------------
1239 -- Build_Equivalent_Record_Aggregate --
1240 ---------------------------------------
1242 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1245 Comp_Type : Entity_Id;
1247 -- Start of processing for Build_Equivalent_Record_Aggregate
1250 if not Is_Record_Type (T)
1251 or else Has_Discriminants (T)
1252 or else Is_Limited_Type (T)
1253 or else Has_Non_Standard_Rep (T)
1255 Initialization_Warning (T);
1259 Comp := First_Component (T);
1261 -- A null record needs no warning
1267 while Present (Comp) loop
1269 -- Array components are acceptable if initialized by a positional
1270 -- aggregate with static components.
1272 if Is_Array_Type (Etype (Comp)) then
1273 Comp_Type := Component_Type (Etype (Comp));
1275 if Nkind (Parent (Comp)) /= N_Component_Declaration
1276 or else No (Expression (Parent (Comp)))
1277 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1279 Initialization_Warning (T);
1282 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1284 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1286 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1288 Initialization_Warning (T);
1292 not Static_Array_Aggregate (Expression (Parent (Comp)))
1294 Initialization_Warning (T);
1298 elsif Is_Scalar_Type (Etype (Comp)) then
1299 Comp_Type := Etype (Comp);
1301 if Nkind (Parent (Comp)) /= N_Component_Declaration
1302 or else No (Expression (Parent (Comp)))
1303 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1304 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1306 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1308 Initialization_Warning (T);
1312 -- For now, other types are excluded
1315 Initialization_Warning (T);
1319 Next_Component (Comp);
1322 -- All components have static initialization. Build positional aggregate
1323 -- from the given expressions or defaults.
1325 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1326 Set_Parent (Agg, Parent (T));
1328 Comp := First_Component (T);
1329 while Present (Comp) loop
1331 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1332 Next_Component (Comp);
1335 Analyze_And_Resolve (Agg, T);
1337 end Build_Equivalent_Record_Aggregate;
1339 -------------------------------
1340 -- Build_Initialization_Call --
1341 -------------------------------
1343 -- References to a discriminant inside the record type declaration can
1344 -- appear either in the subtype_indication to constrain a record or an
1345 -- array, or as part of a larger expression given for the initial value
1346 -- of a component. In both of these cases N appears in the record
1347 -- initialization procedure and needs to be replaced by the formal
1348 -- parameter of the initialization procedure which corresponds to that
1351 -- In the example below, references to discriminants D1 and D2 in proc_1
1352 -- are replaced by references to formals with the same name
1355 -- A similar replacement is done for calls to any record initialization
1356 -- procedure for any components that are themselves of a record type.
1358 -- type R (D1, D2 : Integer) is record
1359 -- X : Integer := F * D1;
1360 -- Y : Integer := F * D2;
1363 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1367 -- Out_2.X := F * D1;
1368 -- Out_2.Y := F * D2;
1371 function Build_Initialization_Call
1375 In_Init_Proc : Boolean := False;
1376 Enclos_Type : Entity_Id := Empty;
1377 Discr_Map : Elist_Id := New_Elmt_List;
1378 With_Default_Init : Boolean := False;
1379 Constructor_Ref : Node_Id := Empty) return List_Id
1381 Res : constant List_Id := New_List;
1384 Controller_Typ : Entity_Id;
1388 First_Arg : Node_Id;
1389 Full_Init_Type : Entity_Id;
1390 Full_Type : Entity_Id := Typ;
1391 Init_Type : Entity_Id;
1395 pragma Assert (Constructor_Ref = Empty
1396 or else Is_CPP_Constructor_Call (Constructor_Ref));
1398 if No (Constructor_Ref) then
1399 Proc := Base_Init_Proc (Typ);
1401 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1404 pragma Assert (Present (Proc));
1405 Init_Type := Etype (First_Formal (Proc));
1406 Full_Init_Type := Underlying_Type (Init_Type);
1408 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1409 -- is active (in which case we make the call anyway, since in the
1410 -- actual compiled client it may be non null).
1411 -- Also nothing to do for value types.
1413 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1414 or else Is_Value_Type (Typ)
1416 (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1421 -- Go to full view if private type. In the case of successive
1422 -- private derivations, this can require more than one step.
1424 while Is_Private_Type (Full_Type)
1425 and then Present (Full_View (Full_Type))
1427 Full_Type := Full_View (Full_Type);
1430 -- If Typ is derived, the procedure is the initialization procedure for
1431 -- the root type. Wrap the argument in an conversion to make it type
1432 -- honest. Actually it isn't quite type honest, because there can be
1433 -- conflicts of views in the private type case. That is why we set
1434 -- Conversion_OK in the conversion node.
1436 if (Is_Record_Type (Typ)
1437 or else Is_Array_Type (Typ)
1438 or else Is_Private_Type (Typ))
1439 and then Init_Type /= Base_Type (Typ)
1441 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1442 Set_Etype (First_Arg, Init_Type);
1445 First_Arg := Id_Ref;
1448 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1450 -- In the tasks case, add _Master as the value of the _Master parameter
1451 -- and _Chain as the value of the _Chain parameter. At the outer level,
1452 -- these will be variables holding the corresponding values obtained
1453 -- from GNARL. At inner levels, they will be the parameters passed down
1454 -- through the outer routines.
1456 if Has_Task (Full_Type) then
1457 if Restriction_Active (No_Task_Hierarchy) then
1459 -- See comments in System.Tasking.Initialization.Init_RTS
1460 -- for the value 3 (should be rtsfindable constant ???)
1462 Append_To (Args, Make_Integer_Literal (Loc, 3));
1465 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1468 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1470 -- Ada 2005 (AI-287): In case of default initialized components
1471 -- with tasks, we generate a null string actual parameter.
1472 -- This is just a workaround that must be improved later???
1474 if With_Default_Init then
1476 Make_String_Literal (Loc,
1481 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1482 Decl := Last (Decls);
1485 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1486 Append_List (Decls, Res);
1494 -- Add discriminant values if discriminants are present
1496 if Has_Discriminants (Full_Init_Type) then
1497 Discr := First_Discriminant (Full_Init_Type);
1499 while Present (Discr) loop
1501 -- If this is a discriminated concurrent type, the init_proc
1502 -- for the corresponding record is being called. Use that type
1503 -- directly to find the discriminant value, to handle properly
1504 -- intervening renamed discriminants.
1507 T : Entity_Id := Full_Type;
1510 if Is_Protected_Type (T) then
1511 T := Corresponding_Record_Type (T);
1513 elsif Is_Private_Type (T)
1514 and then Present (Underlying_Full_View (T))
1515 and then Is_Protected_Type (Underlying_Full_View (T))
1517 T := Corresponding_Record_Type (Underlying_Full_View (T));
1521 Get_Discriminant_Value (
1524 Discriminant_Constraint (Full_Type));
1527 if In_Init_Proc then
1529 -- Replace any possible references to the discriminant in the
1530 -- call to the record initialization procedure with references
1531 -- to the appropriate formal parameter.
1533 if Nkind (Arg) = N_Identifier
1534 and then Ekind (Entity (Arg)) = E_Discriminant
1536 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1538 -- Case of access discriminants. We replace the reference
1539 -- to the type by a reference to the actual object
1541 elsif Nkind (Arg) = N_Attribute_Reference
1542 and then Is_Access_Type (Etype (Arg))
1543 and then Is_Entity_Name (Prefix (Arg))
1544 and then Is_Type (Entity (Prefix (Arg)))
1547 Make_Attribute_Reference (Loc,
1548 Prefix => New_Copy (Prefix (Id_Ref)),
1549 Attribute_Name => Name_Unrestricted_Access);
1551 -- Otherwise make a copy of the default expression. Note that
1552 -- we use the current Sloc for this, because we do not want the
1553 -- call to appear to be at the declaration point. Within the
1554 -- expression, replace discriminants with their discriminals.
1558 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1562 if Is_Constrained (Full_Type) then
1563 Arg := Duplicate_Subexpr_No_Checks (Arg);
1565 -- The constraints come from the discriminant default exps,
1566 -- they must be reevaluated, so we use New_Copy_Tree but we
1567 -- ensure the proper Sloc (for any embedded calls).
1569 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1573 -- Ada 2005 (AI-287): In case of default initialized components,
1574 -- if the component is constrained with a discriminant of the
1575 -- enclosing type, we need to generate the corresponding selected
1576 -- component node to access the discriminant value. In other cases
1577 -- this is not required, either because we are inside the init
1578 -- proc and we use the corresponding formal, or else because the
1579 -- component is constrained by an expression.
1581 if With_Default_Init
1582 and then Nkind (Id_Ref) = N_Selected_Component
1583 and then Nkind (Arg) = N_Identifier
1584 and then Ekind (Entity (Arg)) = E_Discriminant
1587 Make_Selected_Component (Loc,
1588 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1589 Selector_Name => Arg));
1591 Append_To (Args, Arg);
1594 Next_Discriminant (Discr);
1598 -- If this is a call to initialize the parent component of a derived
1599 -- tagged type, indicate that the tag should not be set in the parent.
1601 if Is_Tagged_Type (Full_Init_Type)
1602 and then not Is_CPP_Class (Full_Init_Type)
1603 and then Nkind (Id_Ref) = N_Selected_Component
1604 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1606 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1608 elsif Present (Constructor_Ref) then
1609 Append_List_To (Args,
1610 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1614 Make_Procedure_Call_Statement (Loc,
1615 Name => New_Occurrence_Of (Proc, Loc),
1616 Parameter_Associations => Args));
1618 if Needs_Finalization (Typ)
1619 and then Nkind (Id_Ref) = N_Selected_Component
1621 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1622 Append_List_To (Res,
1624 Ref => New_Copy_Tree (First_Arg),
1627 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1628 With_Attach => Make_Integer_Literal (Loc, 1)));
1630 -- If the enclosing type is an extension with new controlled
1631 -- components, it has his own record controller. If the parent
1632 -- also had a record controller, attach it to the new one.
1634 -- Build_Init_Statements relies on the fact that in this specific
1635 -- case the last statement of the result is the attach call to
1636 -- the controller. If this is changed, it must be synchronized.
1638 elsif Present (Enclos_Type)
1639 and then Has_New_Controlled_Component (Enclos_Type)
1640 and then Has_Controlled_Component (Typ)
1642 if Is_Inherently_Limited_Type (Typ) then
1643 Controller_Typ := RTE (RE_Limited_Record_Controller);
1645 Controller_Typ := RTE (RE_Record_Controller);
1648 Append_List_To (Res,
1651 Make_Selected_Component (Loc,
1652 Prefix => New_Copy_Tree (First_Arg),
1653 Selector_Name => Make_Identifier (Loc, Name_uController)),
1654 Typ => Controller_Typ,
1655 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1656 With_Attach => Make_Integer_Literal (Loc, 1)));
1663 when RE_Not_Available =>
1665 end Build_Initialization_Call;
1667 ---------------------------
1668 -- Build_Master_Renaming --
1669 ---------------------------
1671 function Build_Master_Renaming
1673 T : Entity_Id) return Entity_Id
1675 Loc : constant Source_Ptr := Sloc (N);
1680 -- Nothing to do if there is no task hierarchy
1682 if Restriction_Active (No_Task_Hierarchy) then
1687 Make_Defining_Identifier (Loc,
1688 New_External_Name (Chars (T), 'M'));
1691 Make_Object_Renaming_Declaration (Loc,
1692 Defining_Identifier => M_Id,
1693 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1694 Name => Make_Identifier (Loc, Name_uMaster));
1695 Insert_Before (N, Decl);
1700 when RE_Not_Available =>
1702 end Build_Master_Renaming;
1704 ---------------------------
1705 -- Build_Master_Renaming --
1706 ---------------------------
1708 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1712 -- Nothing to do if there is no task hierarchy
1714 if Restriction_Active (No_Task_Hierarchy) then
1718 M_Id := Build_Master_Renaming (N, T);
1719 Set_Master_Id (T, M_Id);
1722 when RE_Not_Available =>
1724 end Build_Master_Renaming;
1726 ----------------------------
1727 -- Build_Record_Init_Proc --
1728 ----------------------------
1730 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1731 Loc : Source_Ptr := Sloc (N);
1732 Discr_Map : constant Elist_Id := New_Elmt_List;
1733 Proc_Id : Entity_Id;
1734 Rec_Type : Entity_Id;
1735 Set_Tag : Entity_Id := Empty;
1737 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1738 -- Build a assignment statement node which assigns to record component
1739 -- its default expression if defined. The assignment left hand side is
1740 -- marked Assignment_OK so that initialization of limited private
1741 -- records works correctly, Return also the adjustment call for
1742 -- controlled objects
1744 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1745 -- If the record has discriminants, adds assignment statements to
1746 -- statement list to initialize the discriminant values from the
1747 -- arguments of the initialization procedure.
1749 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1750 -- Build a list representing a sequence of statements which initialize
1751 -- components of the given component list. This may involve building
1752 -- case statements for the variant parts.
1754 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1755 -- Given a non-tagged type-derivation that declares discriminants,
1758 -- type R (R1, R2 : Integer) is record ... end record;
1760 -- type D (D1 : Integer) is new R (1, D1);
1762 -- we make the _init_proc of D be
1764 -- procedure _init_proc(X : D; D1 : Integer) is
1766 -- _init_proc( R(X), 1, D1);
1769 -- This function builds the call statement in this _init_proc.
1771 procedure Build_Init_Procedure;
1772 -- Build the tree corresponding to the procedure specification and body
1773 -- of the initialization procedure (by calling all the preceding
1774 -- auxiliary routines), and install it as the _init TSS.
1776 procedure Build_Offset_To_Top_Functions;
1777 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1778 -- and body of the Offset_To_Top function that is generated when the
1779 -- parent of a type with discriminants has secondary dispatch tables.
1781 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1782 -- Add range checks to components of discriminated records. S is a
1783 -- subtype indication of a record component. Check_List is a list
1784 -- to which the check actions are appended.
1786 function Component_Needs_Simple_Initialization
1787 (T : Entity_Id) return Boolean;
1788 -- Determines if a component needs simple initialization, given its type
1789 -- T. This is the same as Needs_Simple_Initialization except for the
1790 -- following difference: the types Tag and Interface_Tag, that are
1791 -- access types which would normally require simple initialization to
1792 -- null, do not require initialization as components, since they are
1793 -- explicitly initialized by other means.
1795 procedure Constrain_Array
1797 Check_List : List_Id);
1798 -- Called from Build_Record_Checks.
1799 -- Apply a list of index constraints to an unconstrained array type.
1800 -- The first parameter is the entity for the resulting subtype.
1801 -- Check_List is a list to which the check actions are appended.
1803 procedure Constrain_Index
1806 Check_List : List_Id);
1807 -- Process an index constraint in a constrained array declaration.
1808 -- The constraint can be a subtype name, or a range with or without
1809 -- an explicit subtype mark. The index is the corresponding index of the
1810 -- unconstrained array. S is the range expression. Check_List is a list
1811 -- to which the check actions are appended (called from
1812 -- Build_Record_Checks).
1814 function Parent_Subtype_Renaming_Discrims return Boolean;
1815 -- Returns True for base types N that rename discriminants, else False
1817 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1818 -- Determines whether a record initialization procedure needs to be
1819 -- generated for the given record type.
1821 ----------------------
1822 -- Build_Assignment --
1823 ----------------------
1825 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1828 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1829 Kind : Node_Kind := Nkind (N);
1835 Make_Selected_Component (Loc,
1836 Prefix => Make_Identifier (Loc, Name_uInit),
1837 Selector_Name => New_Occurrence_Of (Id, Loc));
1838 Set_Assignment_OK (Lhs);
1840 -- Case of an access attribute applied to the current instance.
1841 -- Replace the reference to the type by a reference to the actual
1842 -- object. (Note that this handles the case of the top level of
1843 -- the expression being given by such an attribute, but does not
1844 -- cover uses nested within an initial value expression. Nested
1845 -- uses are unlikely to occur in practice, but are theoretically
1846 -- possible. It is not clear how to handle them without fully
1847 -- traversing the expression. ???
1849 if Kind = N_Attribute_Reference
1850 and then (Attribute_Name (N) = Name_Unchecked_Access
1852 Attribute_Name (N) = Name_Unrestricted_Access)
1853 and then Is_Entity_Name (Prefix (N))
1854 and then Is_Type (Entity (Prefix (N)))
1855 and then Entity (Prefix (N)) = Rec_Type
1858 Make_Attribute_Reference (Loc,
1859 Prefix => Make_Identifier (Loc, Name_uInit),
1860 Attribute_Name => Name_Unrestricted_Access);
1863 -- Take a copy of Exp to ensure that later copies of this component
1864 -- declaration in derived types see the original tree, not a node
1865 -- rewritten during expansion of the init_proc. If the copy contains
1866 -- itypes, the scope of the new itypes is the init_proc being built.
1868 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1871 Make_Assignment_Statement (Loc,
1873 Expression => Exp));
1875 Set_No_Ctrl_Actions (First (Res));
1877 -- Adjust the tag if tagged (because of possible view conversions).
1878 -- Suppress the tag adjustment when VM_Target because VM tags are
1879 -- represented implicitly in objects.
1881 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1883 Make_Assignment_Statement (Loc,
1885 Make_Selected_Component (Loc,
1886 Prefix => New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1888 New_Reference_To (First_Tag_Component (Typ), Loc)),
1891 Unchecked_Convert_To (RTE (RE_Tag),
1893 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1896 -- Adjust the component if controlled except if it is an aggregate
1897 -- that will be expanded inline.
1899 if Kind = N_Qualified_Expression then
1900 Kind := Nkind (Expression (N));
1903 if Needs_Finalization (Typ)
1904 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1905 and then not Is_Inherently_Limited_Type (Typ)
1908 Ref : constant Node_Id :=
1909 New_Copy_Tree (Lhs, New_Scope => Proc_Id);
1911 Append_List_To (Res,
1915 Flist_Ref => Find_Final_List (Etype (Id), Ref),
1916 With_Attach => Make_Integer_Literal (Loc, 1)));
1923 when RE_Not_Available =>
1925 end Build_Assignment;
1927 ------------------------------------
1928 -- Build_Discriminant_Assignments --
1929 ------------------------------------
1931 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1933 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1936 if Has_Discriminants (Rec_Type)
1937 and then not Is_Unchecked_Union (Rec_Type)
1939 D := First_Discriminant (Rec_Type);
1941 while Present (D) loop
1942 -- Don't generate the assignment for discriminants in derived
1943 -- tagged types if the discriminant is a renaming of some
1944 -- ancestor discriminant. This initialization will be done
1945 -- when initializing the _parent field of the derived record.
1947 if Is_Tagged and then
1948 Present (Corresponding_Discriminant (D))
1954 Append_List_To (Statement_List,
1955 Build_Assignment (D,
1956 New_Reference_To (Discriminal (D), Loc)));
1959 Next_Discriminant (D);
1962 end Build_Discriminant_Assignments;
1964 --------------------------
1965 -- Build_Init_Call_Thru --
1966 --------------------------
1968 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1969 Parent_Proc : constant Entity_Id :=
1970 Base_Init_Proc (Etype (Rec_Type));
1972 Parent_Type : constant Entity_Id :=
1973 Etype (First_Formal (Parent_Proc));
1975 Uparent_Type : constant Entity_Id :=
1976 Underlying_Type (Parent_Type);
1978 First_Discr_Param : Node_Id;
1980 Parent_Discr : Entity_Id;
1981 First_Arg : Node_Id;
1987 -- First argument (_Init) is the object to be initialized.
1988 -- ??? not sure where to get a reasonable Loc for First_Arg
1991 OK_Convert_To (Parent_Type,
1992 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1994 Set_Etype (First_Arg, Parent_Type);
1996 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1998 -- In the tasks case,
1999 -- add _Master as the value of the _Master parameter
2000 -- add _Chain as the value of the _Chain parameter.
2001 -- add _Task_Name as the value of the _Task_Name parameter.
2002 -- At the outer level, these will be variables holding the
2003 -- corresponding values obtained from GNARL or the expander.
2005 -- At inner levels, they will be the parameters passed down through
2006 -- the outer routines.
2008 First_Discr_Param := Next (First (Parameters));
2010 if Has_Task (Rec_Type) then
2011 if Restriction_Active (No_Task_Hierarchy) then
2013 -- See comments in System.Tasking.Initialization.Init_RTS
2016 Append_To (Args, Make_Integer_Literal (Loc, 3));
2018 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2021 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2022 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2023 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2026 -- Append discriminant values
2028 if Has_Discriminants (Uparent_Type) then
2029 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2031 Parent_Discr := First_Discriminant (Uparent_Type);
2032 while Present (Parent_Discr) loop
2034 -- Get the initial value for this discriminant
2035 -- ??? needs to be cleaned up to use parent_Discr_Constr
2039 Discr_Value : Elmt_Id :=
2041 (Stored_Constraint (Rec_Type));
2043 Discr : Entity_Id :=
2044 First_Stored_Discriminant (Uparent_Type);
2046 while Original_Record_Component (Parent_Discr) /= Discr loop
2047 Next_Stored_Discriminant (Discr);
2048 Next_Elmt (Discr_Value);
2051 Arg := Node (Discr_Value);
2054 -- Append it to the list
2056 if Nkind (Arg) = N_Identifier
2057 and then Ekind (Entity (Arg)) = E_Discriminant
2060 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2062 -- Case of access discriminants. We replace the reference
2063 -- to the type by a reference to the actual object.
2065 -- Is above comment right??? Use of New_Copy below seems mighty
2069 Append_To (Args, New_Copy (Arg));
2072 Next_Discriminant (Parent_Discr);
2078 Make_Procedure_Call_Statement (Loc,
2079 Name => New_Occurrence_Of (Parent_Proc, Loc),
2080 Parameter_Associations => Args));
2083 end Build_Init_Call_Thru;
2085 -----------------------------------
2086 -- Build_Offset_To_Top_Functions --
2087 -----------------------------------
2089 procedure Build_Offset_To_Top_Functions is
2091 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2093 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2095 -- return O.Iface_Comp'Position;
2098 ----------------------------------
2099 -- Build_Offset_To_Top_Function --
2100 ----------------------------------
2102 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2103 Body_Node : Node_Id;
2104 Func_Id : Entity_Id;
2105 Spec_Node : Node_Id;
2109 Make_Defining_Identifier (Loc,
2110 Chars => New_Internal_Name ('F'));
2112 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2115 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2117 Spec_Node := New_Node (N_Function_Specification, Loc);
2118 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2119 Set_Parameter_Specifications (Spec_Node, New_List (
2120 Make_Parameter_Specification (Loc,
2121 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2123 Parameter_Type => New_Reference_To (Rec_Type, Loc))));
2124 Set_Result_Definition (Spec_Node,
2125 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2128 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2130 -- return O.Iface_Comp'Position;
2133 Body_Node := New_Node (N_Subprogram_Body, Loc);
2134 Set_Specification (Body_Node, Spec_Node);
2135 Set_Declarations (Body_Node, New_List);
2136 Set_Handled_Statement_Sequence (Body_Node,
2137 Make_Handled_Sequence_Of_Statements (Loc,
2138 Statements => New_List (
2139 Make_Simple_Return_Statement (Loc,
2141 Make_Attribute_Reference (Loc,
2143 Make_Selected_Component (Loc,
2144 Prefix => Make_Identifier (Loc, Name_uO),
2145 Selector_Name => New_Reference_To
2147 Attribute_Name => Name_Position)))));
2149 Set_Ekind (Func_Id, E_Function);
2150 Set_Mechanism (Func_Id, Default_Mechanism);
2151 Set_Is_Internal (Func_Id, True);
2153 if not Debug_Generated_Code then
2154 Set_Debug_Info_Off (Func_Id);
2157 Analyze (Body_Node);
2159 Append_Freeze_Action (Rec_Type, Body_Node);
2160 end Build_Offset_To_Top_Function;
2164 Ifaces_Comp_List : Elist_Id;
2165 Iface_Comp_Elmt : Elmt_Id;
2166 Iface_Comp : Node_Id;
2168 -- Start of processing for Build_Offset_To_Top_Functions
2171 -- Offset_To_Top_Functions are built only for derivations of types
2172 -- with discriminants that cover interface types.
2173 -- Nothing is needed either in case of virtual machines, since
2174 -- interfaces are handled directly by the VM.
2176 if not Is_Tagged_Type (Rec_Type)
2177 or else Etype (Rec_Type) = Rec_Type
2178 or else not Has_Discriminants (Etype (Rec_Type))
2179 or else not Tagged_Type_Expansion
2184 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2186 -- For each interface type with secondary dispatch table we generate
2187 -- the Offset_To_Top_Functions (required to displace the pointer in
2188 -- interface conversions)
2190 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2191 while Present (Iface_Comp_Elmt) loop
2192 Iface_Comp := Node (Iface_Comp_Elmt);
2193 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2195 -- If the interface is a parent of Rec_Type it shares the primary
2196 -- dispatch table and hence there is no need to build the function
2198 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type) then
2199 Build_Offset_To_Top_Function (Iface_Comp);
2202 Next_Elmt (Iface_Comp_Elmt);
2204 end Build_Offset_To_Top_Functions;
2206 --------------------------
2207 -- Build_Init_Procedure --
2208 --------------------------
2210 procedure Build_Init_Procedure is
2211 Body_Node : Node_Id;
2212 Handled_Stmt_Node : Node_Id;
2213 Parameters : List_Id;
2214 Proc_Spec_Node : Node_Id;
2215 Body_Stmts : List_Id;
2216 Record_Extension_Node : Node_Id;
2217 Init_Tags_List : List_Id;
2220 Body_Stmts := New_List;
2221 Body_Node := New_Node (N_Subprogram_Body, Loc);
2222 Set_Ekind (Proc_Id, E_Procedure);
2224 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2225 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2227 Parameters := Init_Formals (Rec_Type);
2228 Append_List_To (Parameters,
2229 Build_Discriminant_Formals (Rec_Type, True));
2231 -- For tagged types, we add a flag to indicate whether the routine
2232 -- is called to initialize a parent component in the init_proc of
2233 -- a type extension. If the flag is false, we do not set the tag
2234 -- because it has been set already in the extension.
2236 if Is_Tagged_Type (Rec_Type)
2237 and then not Is_CPP_Class (Rec_Type)
2240 Make_Defining_Identifier (Loc,
2241 Chars => New_Internal_Name ('P'));
2243 Append_To (Parameters,
2244 Make_Parameter_Specification (Loc,
2245 Defining_Identifier => Set_Tag,
2246 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2247 Expression => New_Occurrence_Of (Standard_True, Loc)));
2250 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2251 Set_Specification (Body_Node, Proc_Spec_Node);
2252 Set_Declarations (Body_Node, New_List);
2254 if Parent_Subtype_Renaming_Discrims then
2256 -- N is a Derived_Type_Definition that renames the parameters
2257 -- of the ancestor type. We initialize it by expanding our
2258 -- discriminants and call the ancestor _init_proc with a
2259 -- type-converted object
2261 Append_List_To (Body_Stmts,
2262 Build_Init_Call_Thru (Parameters));
2264 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2265 Build_Discriminant_Assignments (Body_Stmts);
2267 if not Null_Present (Type_Definition (N)) then
2268 Append_List_To (Body_Stmts,
2269 Build_Init_Statements (
2270 Component_List (Type_Definition (N))));
2274 -- N is a Derived_Type_Definition with a possible non-empty
2275 -- extension. The initialization of a type extension consists
2276 -- in the initialization of the components in the extension.
2278 Build_Discriminant_Assignments (Body_Stmts);
2280 Record_Extension_Node :=
2281 Record_Extension_Part (Type_Definition (N));
2283 if not Null_Present (Record_Extension_Node) then
2285 Stmts : constant List_Id :=
2286 Build_Init_Statements (
2287 Component_List (Record_Extension_Node));
2290 -- The parent field must be initialized first because
2291 -- the offset of the new discriminants may depend on it
2293 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2294 Append_List_To (Body_Stmts, Stmts);
2299 -- Add here the assignment to instantiate the Tag
2301 -- The assignment corresponds to the code:
2303 -- _Init._Tag := Typ'Tag;
2305 -- Suppress the tag assignment when VM_Target because VM tags are
2306 -- represented implicitly in objects. It is also suppressed in case
2307 -- of CPP_Class types because in this case the tag is initialized in
2310 if Is_Tagged_Type (Rec_Type)
2311 and then not Is_CPP_Class (Rec_Type)
2312 and then Tagged_Type_Expansion
2313 and then not No_Run_Time_Mode
2315 -- Initialize the primary tag
2317 Init_Tags_List := New_List (
2318 Make_Assignment_Statement (Loc,
2320 Make_Selected_Component (Loc,
2321 Prefix => Make_Identifier (Loc, Name_uInit),
2323 New_Reference_To (First_Tag_Component (Rec_Type), Loc)),
2327 (Node (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2329 -- Generate the SCIL node associated with the initialization of
2330 -- the tag component.
2332 if Generate_SCIL then
2333 Prepend_To (Init_Tags_List,
2335 (SN_Kind => IP_Tag_Init,
2336 Related_Node => First (Init_Tags_List),
2337 Entity => Rec_Type));
2340 -- Ada 2005 (AI-251): Initialize the secondary tags components
2341 -- located at fixed positions (tags whose position depends on
2342 -- variable size components are initialized later ---see below).
2344 if Ada_Version >= Ada_05
2345 and then not Is_Interface (Rec_Type)
2346 and then Has_Interfaces (Rec_Type)
2350 Target => Make_Identifier (Loc, Name_uInit),
2351 Stmts_List => Init_Tags_List,
2352 Fixed_Comps => True,
2353 Variable_Comps => False);
2356 -- The tag must be inserted before the assignments to other
2357 -- components, because the initial value of the component may
2358 -- depend on the tag (eg. through a dispatching operation on
2359 -- an access to the current type). The tag assignment is not done
2360 -- when initializing the parent component of a type extension,
2361 -- because in that case the tag is set in the extension.
2363 -- Extensions of imported C++ classes add a final complication,
2364 -- because we cannot inhibit tag setting in the constructor for
2365 -- the parent. In that case we insert the tag initialization
2366 -- after the calls to initialize the parent.
2368 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2369 Prepend_To (Body_Stmts,
2370 Make_If_Statement (Loc,
2371 Condition => New_Occurrence_Of (Set_Tag, Loc),
2372 Then_Statements => Init_Tags_List));
2374 -- CPP_Class derivation: In this case the dispatch table of the
2375 -- parent was built in the C++ side and we copy the table of the
2376 -- parent to initialize the new dispatch table.
2383 -- We assume the first init_proc call is for the parent
2385 Nod := First (Body_Stmts);
2386 while Present (Next (Nod))
2387 and then (Nkind (Nod) /= N_Procedure_Call_Statement
2388 or else not Is_Init_Proc (Name (Nod)))
2394 -- ancestor_constructor (_init.parent);
2396 -- inherit_prim_ops (_init._tag, new_dt, num_prims);
2397 -- _init._tag := new_dt;
2400 Prepend_To (Init_Tags_List,
2401 Build_Inherit_Prims (Loc,
2404 Make_Selected_Component (Loc,
2406 Make_Identifier (Loc,
2407 Chars => Name_uInit),
2410 (First_Tag_Component (Rec_Type), Loc)),
2413 (Node (First_Elmt (Access_Disp_Table (Rec_Type))),
2417 (DT_Entry_Count (First_Tag_Component (Rec_Type)))));
2420 Make_If_Statement (Loc,
2421 Condition => New_Occurrence_Of (Set_Tag, Loc),
2422 Then_Statements => Init_Tags_List));
2424 -- We have inherited table of the parent from the CPP side.
2425 -- Now we fill the slots associated with Ada primitives.
2426 -- This needs more work to avoid its execution each time
2427 -- an object is initialized???
2434 E := First_Elmt (Primitive_Operations (Rec_Type));
2435 while Present (E) loop
2438 if not Is_Imported (Prim)
2439 and then Convention (Prim) = Convention_CPP
2440 and then not Present (Interface_Alias (Prim))
2442 Append_List_To (Init_Tags_List,
2443 Register_Primitive (Loc, Prim => Prim));
2452 -- Ada 2005 (AI-251): Initialize the secondary tag components
2453 -- located at variable positions. We delay the generation of this
2454 -- code until here because the value of the attribute 'Position
2455 -- applied to variable size components of the parent type that
2456 -- depend on discriminants is only safely read at runtime after
2457 -- the parent components have been initialized.
2459 if Ada_Version >= Ada_05
2460 and then not Is_Interface (Rec_Type)
2461 and then Has_Interfaces (Rec_Type)
2462 and then Has_Discriminants (Etype (Rec_Type))
2463 and then Is_Variable_Size_Record (Etype (Rec_Type))
2465 Init_Tags_List := New_List;
2469 Target => Make_Identifier (Loc, Name_uInit),
2470 Stmts_List => Init_Tags_List,
2471 Fixed_Comps => False,
2472 Variable_Comps => True);
2474 if Is_Non_Empty_List (Init_Tags_List) then
2475 Append_List_To (Body_Stmts, Init_Tags_List);
2480 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2481 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2482 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2483 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2485 if not Debug_Generated_Code then
2486 Set_Debug_Info_Off (Proc_Id);
2489 -- Associate Init_Proc with type, and determine if the procedure
2490 -- is null (happens because of the Initialize_Scalars pragma case,
2491 -- where we have to generate a null procedure in case it is called
2492 -- by a client with Initialize_Scalars set). Such procedures have
2493 -- to be generated, but do not have to be called, so we mark them
2494 -- as null to suppress the call.
2496 Set_Init_Proc (Rec_Type, Proc_Id);
2498 if List_Length (Body_Stmts) = 1
2499 and then Nkind (First (Body_Stmts)) = N_Null_Statement
2500 and then VM_Target = No_VM
2502 -- Even though the init proc may be null at this time it might get
2503 -- some stuff added to it later by the VM backend.
2505 Set_Is_Null_Init_Proc (Proc_Id);
2507 end Build_Init_Procedure;
2509 ---------------------------
2510 -- Build_Init_Statements --
2511 ---------------------------
2513 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2514 Check_List : constant List_Id := New_List;
2519 Statement_List : List_Id;
2524 Per_Object_Constraint_Components : Boolean;
2526 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2527 -- Components with access discriminants that depend on the current
2528 -- instance must be initialized after all other components.
2530 ---------------------------
2531 -- Has_Access_Constraint --
2532 ---------------------------
2534 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2536 T : constant Entity_Id := Etype (E);
2539 if Has_Per_Object_Constraint (E)
2540 and then Has_Discriminants (T)
2542 Disc := First_Discriminant (T);
2543 while Present (Disc) loop
2544 if Is_Access_Type (Etype (Disc)) then
2548 Next_Discriminant (Disc);
2555 end Has_Access_Constraint;
2557 -- Start of processing for Build_Init_Statements
2560 if Null_Present (Comp_List) then
2561 return New_List (Make_Null_Statement (Loc));
2564 Statement_List := New_List;
2566 -- Loop through visible declarations of task types and protected
2567 -- types moving any expanded code from the spec to the body of the
2570 if Is_Task_Record_Type (Rec_Type)
2571 or else Is_Protected_Record_Type (Rec_Type)
2574 Decl : constant Node_Id :=
2575 Parent (Corresponding_Concurrent_Type (Rec_Type));
2581 if Is_Task_Record_Type (Rec_Type) then
2582 Def := Task_Definition (Decl);
2584 Def := Protected_Definition (Decl);
2587 if Present (Def) then
2588 N1 := First (Visible_Declarations (Def));
2589 while Present (N1) loop
2593 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2594 or else Nkind (N2) in N_Raise_xxx_Error
2595 or else Nkind (N2) = N_Procedure_Call_Statement
2597 Append_To (Statement_List,
2598 New_Copy_Tree (N2, New_Scope => Proc_Id));
2599 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2607 -- Loop through components, skipping pragmas, in 2 steps. The first
2608 -- step deals with regular components. The second step deals with
2609 -- components have per object constraints, and no explicit initia-
2612 Per_Object_Constraint_Components := False;
2614 -- First step : regular components
2616 Decl := First_Non_Pragma (Component_Items (Comp_List));
2617 while Present (Decl) loop
2620 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2622 Id := Defining_Identifier (Decl);
2625 if Has_Access_Constraint (Id)
2626 and then No (Expression (Decl))
2628 -- Skip processing for now and ask for a second pass
2630 Per_Object_Constraint_Components := True;
2633 -- Case of explicit initialization
2635 if Present (Expression (Decl)) then
2636 if Is_CPP_Constructor_Call (Expression (Decl)) then
2638 Build_Initialization_Call
2641 Make_Selected_Component (Loc,
2643 Make_Identifier (Loc, Name_uInit),
2644 Selector_Name => New_Occurrence_Of (Id, Loc)),
2646 In_Init_Proc => True,
2647 Enclos_Type => Rec_Type,
2648 Discr_Map => Discr_Map,
2649 Constructor_Ref => Expression (Decl));
2651 Stmts := Build_Assignment (Id, Expression (Decl));
2654 -- Case of composite component with its own Init_Proc
2656 elsif not Is_Interface (Typ)
2657 and then Has_Non_Null_Base_Init_Proc (Typ)
2660 Build_Initialization_Call
2663 Make_Selected_Component (Loc,
2664 Prefix => Make_Identifier (Loc, Name_uInit),
2665 Selector_Name => New_Occurrence_Of (Id, Loc)),
2667 In_Init_Proc => True,
2668 Enclos_Type => Rec_Type,
2669 Discr_Map => Discr_Map);
2671 Clean_Task_Names (Typ, Proc_Id);
2673 -- Case of component needing simple initialization
2675 elsif Component_Needs_Simple_Initialization (Typ) then
2678 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2680 -- Nothing needed for this case
2686 if Present (Check_List) then
2687 Append_List_To (Statement_List, Check_List);
2690 if Present (Stmts) then
2692 -- Add the initialization of the record controller before
2693 -- the _Parent field is attached to it when the attachment
2694 -- can occur. It does not work to simply initialize the
2695 -- controller first: it must be initialized after the parent
2696 -- if the parent holds discriminants that can be used to
2697 -- compute the offset of the controller. We assume here that
2698 -- the last statement of the initialization call is the
2699 -- attachment of the parent (see Build_Initialization_Call)
2701 if Chars (Id) = Name_uController
2702 and then Rec_Type /= Etype (Rec_Type)
2703 and then Has_Controlled_Component (Etype (Rec_Type))
2704 and then Has_New_Controlled_Component (Rec_Type)
2705 and then Present (Last (Statement_List))
2707 Insert_List_Before (Last (Statement_List), Stmts);
2709 Append_List_To (Statement_List, Stmts);
2714 Next_Non_Pragma (Decl);
2717 if Per_Object_Constraint_Components then
2719 -- Second pass: components with per-object constraints
2721 Decl := First_Non_Pragma (Component_Items (Comp_List));
2722 while Present (Decl) loop
2724 Id := Defining_Identifier (Decl);
2727 if Has_Access_Constraint (Id)
2728 and then No (Expression (Decl))
2730 if Has_Non_Null_Base_Init_Proc (Typ) then
2731 Append_List_To (Statement_List,
2732 Build_Initialization_Call (Loc,
2733 Make_Selected_Component (Loc,
2734 Prefix => Make_Identifier (Loc, Name_uInit),
2735 Selector_Name => New_Occurrence_Of (Id, Loc)),
2737 In_Init_Proc => True,
2738 Enclos_Type => Rec_Type,
2739 Discr_Map => Discr_Map));
2741 Clean_Task_Names (Typ, Proc_Id);
2743 elsif Component_Needs_Simple_Initialization (Typ) then
2744 Append_List_To (Statement_List,
2746 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
2750 Next_Non_Pragma (Decl);
2754 -- Process the variant part
2756 if Present (Variant_Part (Comp_List)) then
2757 Alt_List := New_List;
2758 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2759 while Present (Variant) loop
2760 Loc := Sloc (Variant);
2761 Append_To (Alt_List,
2762 Make_Case_Statement_Alternative (Loc,
2764 New_Copy_List (Discrete_Choices (Variant)),
2766 Build_Init_Statements (Component_List (Variant))));
2767 Next_Non_Pragma (Variant);
2770 -- The expression of the case statement which is a reference
2771 -- to one of the discriminants is replaced by the appropriate
2772 -- formal parameter of the initialization procedure.
2774 Append_To (Statement_List,
2775 Make_Case_Statement (Loc,
2777 New_Reference_To (Discriminal (
2778 Entity (Name (Variant_Part (Comp_List)))), Loc),
2779 Alternatives => Alt_List));
2782 -- For a task record type, add the task create call and calls
2783 -- to bind any interrupt (signal) entries.
2785 if Is_Task_Record_Type (Rec_Type) then
2787 -- In the case of the restricted run time the ATCB has already
2788 -- been preallocated.
2790 if Restricted_Profile then
2791 Append_To (Statement_List,
2792 Make_Assignment_Statement (Loc,
2793 Name => Make_Selected_Component (Loc,
2794 Prefix => Make_Identifier (Loc, Name_uInit),
2795 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2796 Expression => Make_Attribute_Reference (Loc,
2798 Make_Selected_Component (Loc,
2799 Prefix => Make_Identifier (Loc, Name_uInit),
2801 Make_Identifier (Loc, Name_uATCB)),
2802 Attribute_Name => Name_Unchecked_Access)));
2805 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2807 -- Generate the statements which map a string entry name to a
2808 -- task entry index. Note that the task may not have entries.
2810 if Entry_Names_OK then
2811 Names := Build_Entry_Names (Rec_Type);
2813 if Present (Names) then
2814 Append_To (Statement_List, Names);
2819 Task_Type : constant Entity_Id :=
2820 Corresponding_Concurrent_Type (Rec_Type);
2821 Task_Decl : constant Node_Id := Parent (Task_Type);
2822 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2827 if Present (Task_Def) then
2828 Vis_Decl := First (Visible_Declarations (Task_Def));
2829 while Present (Vis_Decl) loop
2830 Loc := Sloc (Vis_Decl);
2832 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2833 if Get_Attribute_Id (Chars (Vis_Decl)) =
2836 Ent := Entity (Name (Vis_Decl));
2838 if Ekind (Ent) = E_Entry then
2839 Append_To (Statement_List,
2840 Make_Procedure_Call_Statement (Loc,
2841 Name => New_Reference_To (
2842 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2843 Parameter_Associations => New_List (
2844 Make_Selected_Component (Loc,
2846 Make_Identifier (Loc, Name_uInit),
2848 Make_Identifier (Loc, Name_uTask_Id)),
2849 Entry_Index_Expression (
2850 Loc, Ent, Empty, Task_Type),
2851 Expression (Vis_Decl))));
2862 -- For a protected type, add statements generated by
2863 -- Make_Initialize_Protection.
2865 if Is_Protected_Record_Type (Rec_Type) then
2866 Append_List_To (Statement_List,
2867 Make_Initialize_Protection (Rec_Type));
2869 -- Generate the statements which map a string entry name to a
2870 -- protected entry index. Note that the protected type may not
2873 if Entry_Names_OK then
2874 Names := Build_Entry_Names (Rec_Type);
2876 if Present (Names) then
2877 Append_To (Statement_List, Names);
2882 -- If no initializations when generated for component declarations
2883 -- corresponding to this Statement_List, append a null statement
2884 -- to the Statement_List to make it a valid Ada tree.
2886 if Is_Empty_List (Statement_List) then
2887 Append (New_Node (N_Null_Statement, Loc), Statement_List);
2890 return Statement_List;
2893 when RE_Not_Available =>
2895 end Build_Init_Statements;
2897 -------------------------
2898 -- Build_Record_Checks --
2899 -------------------------
2901 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2902 Subtype_Mark_Id : Entity_Id;
2905 if Nkind (S) = N_Subtype_Indication then
2906 Find_Type (Subtype_Mark (S));
2907 Subtype_Mark_Id := Entity (Subtype_Mark (S));
2909 -- Remaining processing depends on type
2911 case Ekind (Subtype_Mark_Id) is
2914 Constrain_Array (S, Check_List);
2920 end Build_Record_Checks;
2922 -------------------------------------------
2923 -- Component_Needs_Simple_Initialization --
2924 -------------------------------------------
2926 function Component_Needs_Simple_Initialization
2927 (T : Entity_Id) return Boolean
2931 Needs_Simple_Initialization (T)
2932 and then not Is_RTE (T, RE_Tag)
2934 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
2936 and then not Is_RTE (T, RE_Interface_Tag);
2937 end Component_Needs_Simple_Initialization;
2939 ---------------------
2940 -- Constrain_Array --
2941 ---------------------
2943 procedure Constrain_Array
2945 Check_List : List_Id)
2947 C : constant Node_Id := Constraint (SI);
2948 Number_Of_Constraints : Nat := 0;
2953 T := Entity (Subtype_Mark (SI));
2955 if Ekind (T) in Access_Kind then
2956 T := Designated_Type (T);
2959 S := First (Constraints (C));
2961 while Present (S) loop
2962 Number_Of_Constraints := Number_Of_Constraints + 1;
2966 -- In either case, the index constraint must provide a discrete
2967 -- range for each index of the array type and the type of each
2968 -- discrete range must be the same as that of the corresponding
2969 -- index. (RM 3.6.1)
2971 S := First (Constraints (C));
2972 Index := First_Index (T);
2975 -- Apply constraints to each index type
2977 for J in 1 .. Number_Of_Constraints loop
2978 Constrain_Index (Index, S, Check_List);
2983 end Constrain_Array;
2985 ---------------------
2986 -- Constrain_Index --
2987 ---------------------
2989 procedure Constrain_Index
2992 Check_List : List_Id)
2994 T : constant Entity_Id := Etype (Index);
2997 if Nkind (S) = N_Range then
2998 Process_Range_Expr_In_Decl (S, T, Check_List);
3000 end Constrain_Index;
3002 --------------------------------------
3003 -- Parent_Subtype_Renaming_Discrims --
3004 --------------------------------------
3006 function Parent_Subtype_Renaming_Discrims return Boolean is
3011 if Base_Type (Pe) /= Pe then
3016 or else not Has_Discriminants (Pe)
3017 or else Is_Constrained (Pe)
3018 or else Is_Tagged_Type (Pe)
3023 -- If there are no explicit stored discriminants we have inherited
3024 -- the root type discriminants so far, so no renamings occurred.
3026 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
3030 -- Check if we have done some trivial renaming of the parent
3031 -- discriminants, i.e. something like
3033 -- type DT (X1,X2: int) is new PT (X1,X2);
3035 De := First_Discriminant (Pe);
3036 Dp := First_Discriminant (Etype (Pe));
3038 while Present (De) loop
3039 pragma Assert (Present (Dp));
3041 if Corresponding_Discriminant (De) /= Dp then
3045 Next_Discriminant (De);
3046 Next_Discriminant (Dp);
3049 return Present (Dp);
3050 end Parent_Subtype_Renaming_Discrims;
3052 ------------------------
3053 -- Requires_Init_Proc --
3054 ------------------------
3056 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3057 Comp_Decl : Node_Id;
3062 -- Definitely do not need one if specifically suppressed
3064 if Suppress_Init_Proc (Rec_Id) then
3068 -- If it is a type derived from a type with unknown discriminants,
3069 -- we cannot build an initialization procedure for it.
3071 if Has_Unknown_Discriminants (Rec_Id)
3072 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3077 -- Otherwise we need to generate an initialization procedure if
3078 -- Is_CPP_Class is False and at least one of the following applies:
3080 -- 1. Discriminants are present, since they need to be initialized
3081 -- with the appropriate discriminant constraint expressions.
3082 -- However, the discriminant of an unchecked union does not
3083 -- count, since the discriminant is not present.
3085 -- 2. The type is a tagged type, since the implicit Tag component
3086 -- needs to be initialized with a pointer to the dispatch table.
3088 -- 3. The type contains tasks
3090 -- 4. One or more components has an initial value
3092 -- 5. One or more components is for a type which itself requires
3093 -- an initialization procedure.
3095 -- 6. One or more components is a type that requires simple
3096 -- initialization (see Needs_Simple_Initialization), except
3097 -- that types Tag and Interface_Tag are excluded, since fields
3098 -- of these types are initialized by other means.
3100 -- 7. The type is the record type built for a task type (since at
3101 -- the very least, Create_Task must be called)
3103 -- 8. The type is the record type built for a protected type (since
3104 -- at least Initialize_Protection must be called)
3106 -- 9. The type is marked as a public entity. The reason we add this
3107 -- case (even if none of the above apply) is to properly handle
3108 -- Initialize_Scalars. If a package is compiled without an IS
3109 -- pragma, and the client is compiled with an IS pragma, then
3110 -- the client will think an initialization procedure is present
3111 -- and call it, when in fact no such procedure is required, but
3112 -- since the call is generated, there had better be a routine
3113 -- at the other end of the call, even if it does nothing!)
3115 -- Note: the reason we exclude the CPP_Class case is because in this
3116 -- case the initialization is performed in the C++ side.
3118 if Is_CPP_Class (Rec_Id) then
3121 elsif Is_Interface (Rec_Id) then
3124 elsif (Has_Discriminants (Rec_Id)
3125 and then not Is_Unchecked_Union (Rec_Id))
3126 or else Is_Tagged_Type (Rec_Id)
3127 or else Is_Concurrent_Record_Type (Rec_Id)
3128 or else Has_Task (Rec_Id)
3133 Id := First_Component (Rec_Id);
3134 while Present (Id) loop
3135 Comp_Decl := Parent (Id);
3138 if Present (Expression (Comp_Decl))
3139 or else Has_Non_Null_Base_Init_Proc (Typ)
3140 or else Component_Needs_Simple_Initialization (Typ)
3145 Next_Component (Id);
3148 -- As explained above, a record initialization procedure is needed
3149 -- for public types in case Initialize_Scalars applies to a client.
3150 -- However, such a procedure is not needed in the case where either
3151 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3152 -- applies. No_Initialize_Scalars excludes the possibility of using
3153 -- Initialize_Scalars in any partition, and No_Default_Initialization
3154 -- implies that no initialization should ever be done for objects of
3155 -- the type, so is incompatible with Initialize_Scalars.
3157 if not Restriction_Active (No_Initialize_Scalars)
3158 and then not Restriction_Active (No_Default_Initialization)
3159 and then Is_Public (Rec_Id)
3165 end Requires_Init_Proc;
3167 -- Start of processing for Build_Record_Init_Proc
3170 -- Check for value type, which means no initialization required
3172 Rec_Type := Defining_Identifier (N);
3174 if Is_Value_Type (Rec_Type) then
3178 -- This may be full declaration of a private type, in which case
3179 -- the visible entity is a record, and the private entity has been
3180 -- exchanged with it in the private part of the current package.
3181 -- The initialization procedure is built for the record type, which
3182 -- is retrievable from the private entity.
3184 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3185 Rec_Type := Underlying_Type (Rec_Type);
3188 -- If there are discriminants, build the discriminant map to replace
3189 -- discriminants by their discriminals in complex bound expressions.
3190 -- These only arise for the corresponding records of synchronized types.
3192 if Is_Concurrent_Record_Type (Rec_Type)
3193 and then Has_Discriminants (Rec_Type)
3198 Disc := First_Discriminant (Rec_Type);
3199 while Present (Disc) loop
3200 Append_Elmt (Disc, Discr_Map);
3201 Append_Elmt (Discriminal (Disc), Discr_Map);
3202 Next_Discriminant (Disc);
3207 -- Derived types that have no type extension can use the initialization
3208 -- procedure of their parent and do not need a procedure of their own.
3209 -- This is only correct if there are no representation clauses for the
3210 -- type or its parent, and if the parent has in fact been frozen so
3211 -- that its initialization procedure exists.
3213 if Is_Derived_Type (Rec_Type)
3214 and then not Is_Tagged_Type (Rec_Type)
3215 and then not Is_Unchecked_Union (Rec_Type)
3216 and then not Has_New_Non_Standard_Rep (Rec_Type)
3217 and then not Parent_Subtype_Renaming_Discrims
3218 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3220 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3222 -- Otherwise if we need an initialization procedure, then build one,
3223 -- mark it as public and inlinable and as having a completion.
3225 elsif Requires_Init_Proc (Rec_Type)
3226 or else Is_Unchecked_Union (Rec_Type)
3229 Make_Defining_Identifier (Loc,
3230 Chars => Make_Init_Proc_Name (Rec_Type));
3232 -- If No_Default_Initialization restriction is active, then we don't
3233 -- want to build an init_proc, but we need to mark that an init_proc
3234 -- would be needed if this restriction was not active (so that we can
3235 -- detect attempts to call it), so set a dummy init_proc in place.
3237 if Restriction_Active (No_Default_Initialization) then
3238 Set_Init_Proc (Rec_Type, Proc_Id);
3242 Build_Offset_To_Top_Functions;
3243 Build_Init_Procedure;
3244 Set_Is_Public (Proc_Id, Is_Public (Pe));
3246 -- The initialization of protected records is not worth inlining.
3247 -- In addition, when compiled for another unit for inlining purposes,
3248 -- it may make reference to entities that have not been elaborated
3249 -- yet. The initialization of controlled records contains a nested
3250 -- clean-up procedure that makes it impractical to inline as well,
3251 -- and leads to undefined symbols if inlined in a different unit.
3252 -- Similar considerations apply to task types.
3254 if not Is_Concurrent_Type (Rec_Type)
3255 and then not Has_Task (Rec_Type)
3256 and then not Needs_Finalization (Rec_Type)
3258 Set_Is_Inlined (Proc_Id);
3261 Set_Is_Internal (Proc_Id);
3262 Set_Has_Completion (Proc_Id);
3264 if not Debug_Generated_Code then
3265 Set_Debug_Info_Off (Proc_Id);
3269 Agg : constant Node_Id :=
3270 Build_Equivalent_Record_Aggregate (Rec_Type);
3272 procedure Collect_Itypes (Comp : Node_Id);
3273 -- Generate references to itypes in the aggregate, because
3274 -- the first use of the aggregate may be in a nested scope.
3276 --------------------
3277 -- Collect_Itypes --
3278 --------------------
3280 procedure Collect_Itypes (Comp : Node_Id) is
3283 Typ : constant Entity_Id := Etype (Comp);
3286 if Is_Array_Type (Typ)
3287 and then Is_Itype (Typ)
3289 Ref := Make_Itype_Reference (Loc);
3290 Set_Itype (Ref, Typ);
3291 Append_Freeze_Action (Rec_Type, Ref);
3293 Ref := Make_Itype_Reference (Loc);
3294 Set_Itype (Ref, Etype (First_Index (Typ)));
3295 Append_Freeze_Action (Rec_Type, Ref);
3297 Sub_Aggr := First (Expressions (Comp));
3299 -- Recurse on nested arrays
3301 while Present (Sub_Aggr) loop
3302 Collect_Itypes (Sub_Aggr);
3309 -- If there is a static initialization aggregate for the type,
3310 -- generate itype references for the types of its (sub)components,
3311 -- to prevent out-of-scope errors in the resulting tree.
3312 -- The aggregate may have been rewritten as a Raise node, in which
3313 -- case there are no relevant itypes.
3316 and then Nkind (Agg) = N_Aggregate
3318 Set_Static_Initialization (Proc_Id, Agg);
3323 Comp := First (Component_Associations (Agg));
3324 while Present (Comp) loop
3325 Collect_Itypes (Expression (Comp));
3332 end Build_Record_Init_Proc;
3334 ----------------------------
3335 -- Build_Slice_Assignment --
3336 ----------------------------
3338 -- Generates the following subprogram:
3341 -- (Source, Target : Array_Type,
3342 -- Left_Lo, Left_Hi : Index;
3343 -- Right_Lo, Right_Hi : Index;
3351 -- if Left_Hi < Left_Lo then
3364 -- Target (Li1) := Source (Ri1);
3367 -- exit when Li1 = Left_Lo;
3368 -- Li1 := Index'pred (Li1);
3369 -- Ri1 := Index'pred (Ri1);
3371 -- exit when Li1 = Left_Hi;
3372 -- Li1 := Index'succ (Li1);
3373 -- Ri1 := Index'succ (Ri1);
3378 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3379 Loc : constant Source_Ptr := Sloc (Typ);
3380 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3382 -- Build formal parameters of procedure
3384 Larray : constant Entity_Id :=
3385 Make_Defining_Identifier
3386 (Loc, Chars => New_Internal_Name ('A'));
3387 Rarray : constant Entity_Id :=
3388 Make_Defining_Identifier
3389 (Loc, Chars => New_Internal_Name ('R'));
3390 Left_Lo : constant Entity_Id :=
3391 Make_Defining_Identifier
3392 (Loc, Chars => New_Internal_Name ('L'));
3393 Left_Hi : constant Entity_Id :=
3394 Make_Defining_Identifier
3395 (Loc, Chars => New_Internal_Name ('L'));
3396 Right_Lo : constant Entity_Id :=
3397 Make_Defining_Identifier
3398 (Loc, Chars => New_Internal_Name ('R'));
3399 Right_Hi : constant Entity_Id :=
3400 Make_Defining_Identifier
3401 (Loc, Chars => New_Internal_Name ('R'));
3402 Rev : constant Entity_Id :=
3403 Make_Defining_Identifier
3404 (Loc, Chars => New_Internal_Name ('D'));
3405 Proc_Name : constant Entity_Id :=
3406 Make_Defining_Identifier (Loc,
3407 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3409 Lnn : constant Entity_Id :=
3410 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3411 Rnn : constant Entity_Id :=
3412 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3413 -- Subscripts for left and right sides
3420 -- Build declarations for indices
3425 Make_Object_Declaration (Loc,
3426 Defining_Identifier => Lnn,
3427 Object_Definition =>
3428 New_Occurrence_Of (Index, Loc)));
3431 Make_Object_Declaration (Loc,
3432 Defining_Identifier => Rnn,
3433 Object_Definition =>
3434 New_Occurrence_Of (Index, Loc)));
3438 -- Build test for empty slice case
3441 Make_If_Statement (Loc,
3444 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3445 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3446 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3448 -- Build initializations for indices
3451 F_Init : constant List_Id := New_List;
3452 B_Init : constant List_Id := New_List;
3456 Make_Assignment_Statement (Loc,
3457 Name => New_Occurrence_Of (Lnn, Loc),
3458 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3461 Make_Assignment_Statement (Loc,
3462 Name => New_Occurrence_Of (Rnn, Loc),
3463 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3466 Make_Assignment_Statement (Loc,
3467 Name => New_Occurrence_Of (Lnn, Loc),
3468 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3471 Make_Assignment_Statement (Loc,
3472 Name => New_Occurrence_Of (Rnn, Loc),
3473 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3476 Make_If_Statement (Loc,
3477 Condition => New_Occurrence_Of (Rev, Loc),
3478 Then_Statements => B_Init,
3479 Else_Statements => F_Init));
3482 -- Now construct the assignment statement
3485 Make_Loop_Statement (Loc,
3486 Statements => New_List (
3487 Make_Assignment_Statement (Loc,
3489 Make_Indexed_Component (Loc,
3490 Prefix => New_Occurrence_Of (Larray, Loc),
3491 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3493 Make_Indexed_Component (Loc,
3494 Prefix => New_Occurrence_Of (Rarray, Loc),
3495 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3496 End_Label => Empty);
3498 -- Build the exit condition and increment/decrement statements
3501 F_Ass : constant List_Id := New_List;
3502 B_Ass : constant List_Id := New_List;
3506 Make_Exit_Statement (Loc,
3509 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3510 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3513 Make_Assignment_Statement (Loc,
3514 Name => New_Occurrence_Of (Lnn, Loc),
3516 Make_Attribute_Reference (Loc,
3518 New_Occurrence_Of (Index, Loc),
3519 Attribute_Name => Name_Succ,
3520 Expressions => New_List (
3521 New_Occurrence_Of (Lnn, Loc)))));
3524 Make_Assignment_Statement (Loc,
3525 Name => New_Occurrence_Of (Rnn, Loc),
3527 Make_Attribute_Reference (Loc,
3529 New_Occurrence_Of (Index, Loc),
3530 Attribute_Name => Name_Succ,
3531 Expressions => New_List (
3532 New_Occurrence_Of (Rnn, Loc)))));
3535 Make_Exit_Statement (Loc,
3538 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3539 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3542 Make_Assignment_Statement (Loc,
3543 Name => New_Occurrence_Of (Lnn, Loc),
3545 Make_Attribute_Reference (Loc,
3547 New_Occurrence_Of (Index, Loc),
3548 Attribute_Name => Name_Pred,
3549 Expressions => New_List (
3550 New_Occurrence_Of (Lnn, Loc)))));
3553 Make_Assignment_Statement (Loc,
3554 Name => New_Occurrence_Of (Rnn, Loc),
3556 Make_Attribute_Reference (Loc,
3558 New_Occurrence_Of (Index, Loc),
3559 Attribute_Name => Name_Pred,
3560 Expressions => New_List (
3561 New_Occurrence_Of (Rnn, Loc)))));
3563 Append_To (Statements (Loops),
3564 Make_If_Statement (Loc,
3565 Condition => New_Occurrence_Of (Rev, Loc),
3566 Then_Statements => B_Ass,
3567 Else_Statements => F_Ass));
3570 Append_To (Stats, Loops);
3574 Formals : List_Id := New_List;
3577 Formals := New_List (
3578 Make_Parameter_Specification (Loc,
3579 Defining_Identifier => Larray,
3580 Out_Present => True,
3582 New_Reference_To (Base_Type (Typ), Loc)),
3584 Make_Parameter_Specification (Loc,
3585 Defining_Identifier => Rarray,
3587 New_Reference_To (Base_Type (Typ), Loc)),
3589 Make_Parameter_Specification (Loc,
3590 Defining_Identifier => Left_Lo,
3592 New_Reference_To (Index, Loc)),
3594 Make_Parameter_Specification (Loc,
3595 Defining_Identifier => Left_Hi,
3597 New_Reference_To (Index, Loc)),
3599 Make_Parameter_Specification (Loc,
3600 Defining_Identifier => Right_Lo,
3602 New_Reference_To (Index, Loc)),
3604 Make_Parameter_Specification (Loc,
3605 Defining_Identifier => Right_Hi,
3607 New_Reference_To (Index, Loc)));
3610 Make_Parameter_Specification (Loc,
3611 Defining_Identifier => Rev,
3613 New_Reference_To (Standard_Boolean, Loc)));
3616 Make_Procedure_Specification (Loc,
3617 Defining_Unit_Name => Proc_Name,
3618 Parameter_Specifications => Formals);
3621 Make_Subprogram_Body (Loc,
3622 Specification => Spec,
3623 Declarations => Decls,
3624 Handled_Statement_Sequence =>
3625 Make_Handled_Sequence_Of_Statements (Loc,
3626 Statements => Stats)));
3629 Set_TSS (Typ, Proc_Name);
3630 Set_Is_Pure (Proc_Name);
3631 end Build_Slice_Assignment;
3633 ------------------------------------
3634 -- Build_Variant_Record_Equality --
3635 ------------------------------------
3639 -- function _Equality (X, Y : T) return Boolean is
3641 -- -- Compare discriminants
3643 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3647 -- -- Compare components
3649 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3653 -- -- Compare variant part
3657 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3662 -- if False or else X.Cn /= Y.Cn then
3670 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3671 Loc : constant Source_Ptr := Sloc (Typ);
3673 F : constant Entity_Id :=
3674 Make_Defining_Identifier (Loc,
3675 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3677 X : constant Entity_Id :=
3678 Make_Defining_Identifier (Loc,
3681 Y : constant Entity_Id :=
3682 Make_Defining_Identifier (Loc,
3685 Def : constant Node_Id := Parent (Typ);
3686 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3687 Stmts : constant List_Id := New_List;
3688 Pspecs : constant List_Id := New_List;
3691 -- Derived Unchecked_Union types no longer inherit the equality function
3694 if Is_Derived_Type (Typ)
3695 and then not Is_Unchecked_Union (Typ)
3696 and then not Has_New_Non_Standard_Rep (Typ)
3699 Parent_Eq : constant Entity_Id :=
3700 TSS (Root_Type (Typ), TSS_Composite_Equality);
3703 if Present (Parent_Eq) then
3704 Copy_TSS (Parent_Eq, Typ);
3711 Make_Subprogram_Body (Loc,
3713 Make_Function_Specification (Loc,
3714 Defining_Unit_Name => F,
3715 Parameter_Specifications => Pspecs,
3716 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3717 Declarations => New_List,
3718 Handled_Statement_Sequence =>
3719 Make_Handled_Sequence_Of_Statements (Loc,
3720 Statements => Stmts)));
3723 Make_Parameter_Specification (Loc,
3724 Defining_Identifier => X,
3725 Parameter_Type => New_Reference_To (Typ, Loc)));
3728 Make_Parameter_Specification (Loc,
3729 Defining_Identifier => Y,
3730 Parameter_Type => New_Reference_To (Typ, Loc)));
3732 -- Unchecked_Unions require additional machinery to support equality.
3733 -- Two extra parameters (A and B) are added to the equality function
3734 -- parameter list in order to capture the inferred values of the
3735 -- discriminants in later calls.
3737 if Is_Unchecked_Union (Typ) then
3739 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3741 A : constant Node_Id :=
3742 Make_Defining_Identifier (Loc,
3745 B : constant Node_Id :=
3746 Make_Defining_Identifier (Loc,
3750 -- Add A and B to the parameter list
3753 Make_Parameter_Specification (Loc,
3754 Defining_Identifier => A,
3755 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3758 Make_Parameter_Specification (Loc,
3759 Defining_Identifier => B,
3760 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3762 -- Generate the following header code to compare the inferred
3770 Make_If_Statement (Loc,
3773 Left_Opnd => New_Reference_To (A, Loc),
3774 Right_Opnd => New_Reference_To (B, Loc)),
3775 Then_Statements => New_List (
3776 Make_Simple_Return_Statement (Loc,
3777 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3779 -- Generate component-by-component comparison. Note that we must
3780 -- propagate one of the inferred discriminant formals to act as
3781 -- the case statement switch.
3783 Append_List_To (Stmts,
3784 Make_Eq_Case (Typ, Comps, A));
3788 -- Normal case (not unchecked union)
3793 Discriminant_Specifications (Def)));
3795 Append_List_To (Stmts,
3796 Make_Eq_Case (Typ, Comps));
3800 Make_Simple_Return_Statement (Loc,
3801 Expression => New_Reference_To (Standard_True, Loc)));
3806 if not Debug_Generated_Code then
3807 Set_Debug_Info_Off (F);
3809 end Build_Variant_Record_Equality;
3811 -----------------------------
3812 -- Check_Stream_Attributes --
3813 -----------------------------
3815 procedure Check_Stream_Attributes (Typ : Entity_Id) is
3817 Par_Read : constant Boolean :=
3818 Stream_Attribute_Available (Typ, TSS_Stream_Read)
3819 and then not Has_Specified_Stream_Read (Typ);
3820 Par_Write : constant Boolean :=
3821 Stream_Attribute_Available (Typ, TSS_Stream_Write)
3822 and then not Has_Specified_Stream_Write (Typ);
3824 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3825 -- Check that Comp has a user-specified Nam stream attribute
3831 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3833 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3834 Error_Msg_Name_1 := Nam;
3836 ("|component& in limited extension must have% attribute", Comp);
3840 -- Start of processing for Check_Stream_Attributes
3843 if Par_Read or else Par_Write then
3844 Comp := First_Component (Typ);
3845 while Present (Comp) loop
3846 if Comes_From_Source (Comp)
3847 and then Original_Record_Component (Comp) = Comp
3848 and then Is_Limited_Type (Etype (Comp))
3851 Check_Attr (Name_Read, TSS_Stream_Read);
3855 Check_Attr (Name_Write, TSS_Stream_Write);
3859 Next_Component (Comp);
3862 end Check_Stream_Attributes;
3864 -----------------------------
3865 -- Expand_Record_Extension --
3866 -----------------------------
3868 -- Add a field _parent at the beginning of the record extension. This is
3869 -- used to implement inheritance. Here are some examples of expansion:
3871 -- 1. no discriminants
3872 -- type T2 is new T1 with null record;
3874 -- type T2 is new T1 with record
3878 -- 2. renamed discriminants
3879 -- type T2 (B, C : Int) is new T1 (A => B) with record
3880 -- _Parent : T1 (A => B);
3884 -- 3. inherited discriminants
3885 -- type T2 is new T1 with record -- discriminant A inherited
3886 -- _Parent : T1 (A);
3890 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
3891 Indic : constant Node_Id := Subtype_Indication (Def);
3892 Loc : constant Source_Ptr := Sloc (Def);
3893 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
3894 Par_Subtype : Entity_Id;
3895 Comp_List : Node_Id;
3896 Comp_Decl : Node_Id;
3899 List_Constr : constant List_Id := New_List;
3902 -- Expand_Record_Extension is called directly from the semantics, so
3903 -- we must check to see whether expansion is active before proceeding
3905 if not Expander_Active then
3909 -- This may be a derivation of an untagged private type whose full
3910 -- view is tagged, in which case the Derived_Type_Definition has no
3911 -- extension part. Build an empty one now.
3913 if No (Rec_Ext_Part) then
3915 Make_Record_Definition (Loc,
3917 Component_List => Empty,
3918 Null_Present => True);
3920 Set_Record_Extension_Part (Def, Rec_Ext_Part);
3921 Mark_Rewrite_Insertion (Rec_Ext_Part);
3924 Comp_List := Component_List (Rec_Ext_Part);
3926 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
3928 -- If the derived type inherits its discriminants the type of the
3929 -- _parent field must be constrained by the inherited discriminants
3931 if Has_Discriminants (T)
3932 and then Nkind (Indic) /= N_Subtype_Indication
3933 and then not Is_Constrained (Entity (Indic))
3935 D := First_Discriminant (T);
3936 while Present (D) loop
3937 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
3938 Next_Discriminant (D);
3943 Make_Subtype_Indication (Loc,
3944 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
3946 Make_Index_Or_Discriminant_Constraint (Loc,
3947 Constraints => List_Constr)),
3950 -- Otherwise the original subtype_indication is just what is needed
3953 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
3956 Set_Parent_Subtype (T, Par_Subtype);
3959 Make_Component_Declaration (Loc,
3960 Defining_Identifier => Parent_N,
3961 Component_Definition =>
3962 Make_Component_Definition (Loc,
3963 Aliased_Present => False,
3964 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
3966 if Null_Present (Rec_Ext_Part) then
3967 Set_Component_List (Rec_Ext_Part,
3968 Make_Component_List (Loc,
3969 Component_Items => New_List (Comp_Decl),
3970 Variant_Part => Empty,
3971 Null_Present => False));
3972 Set_Null_Present (Rec_Ext_Part, False);
3974 elsif Null_Present (Comp_List)
3975 or else Is_Empty_List (Component_Items (Comp_List))
3977 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3978 Set_Null_Present (Comp_List, False);
3981 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
3984 Analyze (Comp_Decl);
3985 end Expand_Record_Extension;
3987 ------------------------------------
3988 -- Expand_N_Full_Type_Declaration --
3989 ------------------------------------
3991 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
3992 Def_Id : constant Entity_Id := Defining_Identifier (N);
3993 B_Id : constant Entity_Id := Base_Type (Def_Id);
3997 procedure Build_Master (Def_Id : Entity_Id);
3998 -- Create the master associated with Def_Id
4004 procedure Build_Master (Def_Id : Entity_Id) is
4006 -- Anonymous access types are created for the components of the
4007 -- record parameter for an entry declaration. No master is created
4010 if Has_Task (Designated_Type (Def_Id))
4011 and then Comes_From_Source (N)
4013 Build_Master_Entity (Def_Id);
4014 Build_Master_Renaming (Parent (Def_Id), Def_Id);
4016 -- Create a class-wide master because a Master_Id must be generated
4017 -- for access-to-limited-class-wide types whose root may be extended
4018 -- with task components.
4020 -- Note: This code covers access-to-limited-interfaces because they
4021 -- can be used to reference tasks implementing them.
4023 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
4024 and then Is_Limited_Type (Designated_Type (Def_Id))
4025 and then Tasking_Allowed
4027 -- Do not create a class-wide master for types whose convention is
4028 -- Java since these types cannot embed Ada tasks anyway. Note that
4029 -- the following test cannot catch the following case:
4031 -- package java.lang.Object is
4032 -- type Typ is tagged limited private;
4033 -- type Ref is access all Typ'Class;
4035 -- type Typ is tagged limited ...;
4036 -- pragma Convention (Typ, Java)
4039 -- Because the convention appears after we have done the
4040 -- processing for type Ref.
4042 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
4043 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
4045 Build_Class_Wide_Master (Def_Id);
4049 -- Start of processing for Expand_N_Full_Type_Declaration
4052 if Is_Access_Type (Def_Id) then
4053 Build_Master (Def_Id);
4055 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4056 Expand_Access_Protected_Subprogram_Type (N);
4059 elsif Ada_Version >= Ada_05
4060 and then Is_Array_Type (Def_Id)
4061 and then Is_Access_Type (Component_Type (Def_Id))
4062 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4064 Build_Master (Component_Type (Def_Id));
4066 elsif Has_Task (Def_Id) then
4067 Expand_Previous_Access_Type (Def_Id);
4069 elsif Ada_Version >= Ada_05
4071 (Is_Record_Type (Def_Id)
4072 or else (Is_Array_Type (Def_Id)
4073 and then Is_Record_Type (Component_Type (Def_Id))))
4081 -- Look for the first anonymous access type component
4083 if Is_Array_Type (Def_Id) then
4084 Comp := First_Entity (Component_Type (Def_Id));
4086 Comp := First_Entity (Def_Id);
4089 while Present (Comp) loop
4090 Typ := Etype (Comp);
4092 exit when Is_Access_Type (Typ)
4093 and then Ekind (Typ) = E_Anonymous_Access_Type;
4098 -- If found we add a renaming declaration of master_id and we
4099 -- associate it to each anonymous access type component. Do
4100 -- nothing if the access type already has a master. This will be
4101 -- the case if the array type is the packed array created for a
4102 -- user-defined array type T, where the master_id is created when
4103 -- expanding the declaration for T.
4106 and then Ekind (Typ) = E_Anonymous_Access_Type
4107 and then not Restriction_Active (No_Task_Hierarchy)
4108 and then No (Master_Id (Typ))
4110 -- Do not consider run-times with no tasking support
4112 and then RTE_Available (RE_Current_Master)
4113 and then Has_Task (Non_Limited_Designated_Type (Typ))
4115 Build_Master_Entity (Def_Id);
4116 M_Id := Build_Master_Renaming (N, Def_Id);
4118 if Is_Array_Type (Def_Id) then
4119 Comp := First_Entity (Component_Type (Def_Id));
4121 Comp := First_Entity (Def_Id);
4124 while Present (Comp) loop
4125 Typ := Etype (Comp);
4127 if Is_Access_Type (Typ)
4128 and then Ekind (Typ) = E_Anonymous_Access_Type
4130 Set_Master_Id (Typ, M_Id);
4139 Par_Id := Etype (B_Id);
4141 -- The parent type is private then we need to inherit any TSS operations
4142 -- from the full view.
4144 if Ekind (Par_Id) in Private_Kind
4145 and then Present (Full_View (Par_Id))
4147 Par_Id := Base_Type (Full_View (Par_Id));
4150 if Nkind (Type_Definition (Original_Node (N))) =
4151 N_Derived_Type_Definition
4152 and then not Is_Tagged_Type (Def_Id)
4153 and then Present (Freeze_Node (Par_Id))
4154 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4156 Ensure_Freeze_Node (B_Id);
4157 FN := Freeze_Node (B_Id);
4159 if No (TSS_Elist (FN)) then
4160 Set_TSS_Elist (FN, New_Elmt_List);
4164 T_E : constant Elist_Id := TSS_Elist (FN);
4168 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4169 while Present (Elmt) loop
4170 if Chars (Node (Elmt)) /= Name_uInit then
4171 Append_Elmt (Node (Elmt), T_E);
4177 -- If the derived type itself is private with a full view, then
4178 -- associate the full view with the inherited TSS_Elist as well.
4180 if Ekind (B_Id) in Private_Kind
4181 and then Present (Full_View (B_Id))
4183 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4185 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4189 end Expand_N_Full_Type_Declaration;
4191 ---------------------------------
4192 -- Expand_N_Object_Declaration --
4193 ---------------------------------
4195 -- First we do special processing for objects of a tagged type where this
4196 -- is the point at which the type is frozen. The creation of the dispatch
4197 -- table and the initialization procedure have to be deferred to this
4198 -- point, since we reference previously declared primitive subprograms.
4200 -- For all types, we call an initialization procedure if there is one
4202 procedure Expand_N_Object_Declaration (N : Node_Id) is
4203 Def_Id : constant Entity_Id := Defining_Identifier (N);
4204 Expr : constant Node_Id := Expression (N);
4205 Loc : constant Source_Ptr := Sloc (N);
4206 Typ : constant Entity_Id := Etype (Def_Id);
4207 Base_Typ : constant Entity_Id := Base_Type (Typ);
4212 Init_After : Node_Id := N;
4213 -- Node after which the init proc call is to be inserted. This is
4214 -- normally N, except for the case of a shared passive variable, in
4215 -- which case the init proc call must be inserted only after the bodies
4216 -- of the shared variable procedures have been seen.
4218 function Rewrite_As_Renaming return Boolean;
4219 -- Indicate whether to rewrite a declaration with initialization into an
4220 -- object renaming declaration (see below).
4222 -------------------------
4223 -- Rewrite_As_Renaming --
4224 -------------------------
4226 function Rewrite_As_Renaming return Boolean is
4228 return not Aliased_Present (N)
4229 and then Is_Entity_Name (Expr_Q)
4230 and then Ekind (Entity (Expr_Q)) = E_Variable
4231 and then OK_To_Rename (Entity (Expr_Q))
4232 and then Is_Entity_Name (Object_Definition (N));
4233 end Rewrite_As_Renaming;
4235 -- Start of processing for Expand_N_Object_Declaration
4238 -- Don't do anything for deferred constants. All proper actions will be
4239 -- expanded during the full declaration.
4241 if No (Expr) and Constant_Present (N) then
4245 -- Force construction of dispatch tables of library level tagged types
4247 if Tagged_Type_Expansion
4248 and then Static_Dispatch_Tables
4249 and then Is_Library_Level_Entity (Def_Id)
4250 and then Is_Library_Level_Tagged_Type (Base_Typ)
4251 and then (Ekind (Base_Typ) = E_Record_Type
4252 or else Ekind (Base_Typ) = E_Protected_Type
4253 or else Ekind (Base_Typ) = E_Task_Type)
4254 and then not Has_Dispatch_Table (Base_Typ)
4257 New_Nodes : List_Id := No_List;
4260 if Is_Concurrent_Type (Base_Typ) then
4261 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4263 New_Nodes := Make_DT (Base_Typ, N);
4266 if not Is_Empty_List (New_Nodes) then
4267 Insert_List_Before (N, New_Nodes);
4272 -- Make shared memory routines for shared passive variable
4274 if Is_Shared_Passive (Def_Id) then
4275 Init_After := Make_Shared_Var_Procs (N);
4278 -- If tasks being declared, make sure we have an activation chain
4279 -- defined for the tasks (has no effect if we already have one), and
4280 -- also that a Master variable is established and that the appropriate
4281 -- enclosing construct is established as a task master.
4283 if Has_Task (Typ) then
4284 Build_Activation_Chain_Entity (N);
4285 Build_Master_Entity (Def_Id);
4288 -- Build a list controller for declarations where the type is anonymous
4289 -- access and the designated type is controlled. Only declarations from
4290 -- source files receive such controllers in order to provide the same
4291 -- lifespan for any potential coextensions that may be associated with
4292 -- the object. Finalization lists of internal controlled anonymous
4293 -- access objects are already handled in Expand_N_Allocator.
4295 if Comes_From_Source (N)
4296 and then Ekind (Typ) = E_Anonymous_Access_Type
4297 and then Is_Controlled (Directly_Designated_Type (Typ))
4298 and then No (Associated_Final_Chain (Typ))
4300 Build_Final_List (N, Typ);
4303 -- Default initialization required, and no expression present
4307 -- Expand Initialize call for controlled objects. One may wonder why
4308 -- the Initialize Call is not done in the regular Init procedure
4309 -- attached to the record type. That's because the init procedure is
4310 -- recursively called on each component, including _Parent, thus the
4311 -- Init call for a controlled object would generate not only one
4312 -- Initialize call as it is required but one for each ancestor of
4313 -- its type. This processing is suppressed if No_Initialization set.
4315 if not Needs_Finalization (Typ)
4316 or else No_Initialization (N)
4320 elsif not Abort_Allowed
4321 or else not Comes_From_Source (N)
4323 Insert_Actions_After (Init_After,
4325 Ref => New_Occurrence_Of (Def_Id, Loc),
4326 Typ => Base_Type (Typ),
4327 Flist_Ref => Find_Final_List (Def_Id),
4328 With_Attach => Make_Integer_Literal (Loc, 1)));
4333 -- We need to protect the initialize call
4337 -- Initialize (...);
4339 -- Undefer_Abort.all;
4342 -- ??? this won't protect the initialize call for controlled
4343 -- components which are part of the init proc, so this block
4344 -- should probably also contain the call to _init_proc but this
4345 -- requires some code reorganization...
4348 L : constant List_Id :=
4350 (Ref => New_Occurrence_Of (Def_Id, Loc),
4351 Typ => Base_Type (Typ),
4352 Flist_Ref => Find_Final_List (Def_Id),
4353 With_Attach => Make_Integer_Literal (Loc, 1));
4355 Blk : constant Node_Id :=
4356 Make_Block_Statement (Loc,
4357 Handled_Statement_Sequence =>
4358 Make_Handled_Sequence_Of_Statements (Loc, L));
4361 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4362 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4363 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4364 Insert_Actions_After (Init_After, New_List (Blk));
4365 Expand_At_End_Handler
4366 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4370 -- Call type initialization procedure if there is one. We build the
4371 -- call and put it immediately after the object declaration, so that
4372 -- it will be expanded in the usual manner. Note that this will
4373 -- result in proper handling of defaulted discriminants.
4375 -- Need call if there is a base init proc
4377 if Has_Non_Null_Base_Init_Proc (Typ)
4379 -- Suppress call if No_Initialization set on declaration
4381 and then not No_Initialization (N)
4383 -- Suppress call for special case of value type for VM
4385 and then not Is_Value_Type (Typ)
4387 -- Suppress call if Suppress_Init_Proc set on the type. This is
4388 -- needed for the derived type case, where Suppress_Initialization
4389 -- may be set for the derived type, even if there is an init proc
4390 -- defined for the root type.
4392 and then not Suppress_Init_Proc (Typ)
4394 -- Return without initializing when No_Default_Initialization
4395 -- applies. Note that the actual restriction check occurs later,
4396 -- when the object is frozen, because we don't know yet whether
4397 -- the object is imported, which is a case where the check does
4400 if Restriction_Active (No_Default_Initialization) then
4404 -- The call to the initialization procedure does NOT freeze the
4405 -- object being initialized. This is because the call is not a
4406 -- source level call. This works fine, because the only possible
4407 -- statements depending on freeze status that can appear after the
4408 -- Init_Proc call are rep clauses which can safely appear after
4409 -- actual references to the object. Note that this call may
4410 -- subsequently be removed (if a pragma Import is encountered),
4411 -- or moved to the freeze actions for the object (e.g. if an
4412 -- address clause is applied to the object, causing it to get
4413 -- delayed freezing).
4415 Id_Ref := New_Reference_To (Def_Id, Loc);
4416 Set_Must_Not_Freeze (Id_Ref);
4417 Set_Assignment_OK (Id_Ref);
4420 Init_Expr : constant Node_Id :=
4421 Static_Initialization (Base_Init_Proc (Typ));
4423 if Present (Init_Expr) then
4425 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4428 Initialization_Warning (Id_Ref);
4430 Insert_Actions_After (Init_After,
4431 Build_Initialization_Call (Loc, Id_Ref, Typ));
4435 -- If simple initialization is required, then set an appropriate
4436 -- simple initialization expression in place. This special
4437 -- initialization is required even though No_Init_Flag is present,
4438 -- but is not needed if there was an explicit initialization.
4440 -- An internally generated temporary needs no initialization because
4441 -- it will be assigned subsequently. In particular, there is no point
4442 -- in applying Initialize_Scalars to such a temporary.
4444 elsif Needs_Simple_Initialization (Typ)
4445 and then not Is_Internal (Def_Id)
4446 and then not Has_Init_Expression (N)
4448 Set_No_Initialization (N, False);
4449 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4450 Analyze_And_Resolve (Expression (N), Typ);
4453 -- Generate attribute for Persistent_BSS if needed
4455 if Persistent_BSS_Mode
4456 and then Comes_From_Source (N)
4457 and then Is_Potentially_Persistent_Type (Typ)
4458 and then not Has_Init_Expression (N)
4459 and then Is_Library_Level_Entity (Def_Id)
4465 Make_Linker_Section_Pragma
4466 (Def_Id, Sloc (N), ".persistent.bss");
4467 Insert_After (N, Prag);
4472 -- If access type, then we know it is null if not initialized
4474 if Is_Access_Type (Typ) then
4475 Set_Is_Known_Null (Def_Id);
4478 -- Explicit initialization present
4481 -- Obtain actual expression from qualified expression
4483 if Nkind (Expr) = N_Qualified_Expression then
4484 Expr_Q := Expression (Expr);
4489 -- When we have the appropriate type of aggregate in the expression
4490 -- (it has been determined during analysis of the aggregate by
4491 -- setting the delay flag), let's perform in place assignment and
4492 -- thus avoid creating a temporary.
4494 if Is_Delayed_Aggregate (Expr_Q) then
4495 Convert_Aggr_In_Object_Decl (N);
4497 -- Ada 2005 (AI-318-02): If the initialization expression is a call
4498 -- to a build-in-place function, then access to the declared object
4499 -- must be passed to the function. Currently we limit such functions
4500 -- to those with constrained limited result subtypes, but eventually
4501 -- plan to expand the allowed forms of functions that are treated as
4504 elsif Ada_Version >= Ada_05
4505 and then Is_Build_In_Place_Function_Call (Expr_Q)
4507 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4509 -- The previous call expands the expression initializing the
4510 -- built-in-place object into further code that will be analyzed
4511 -- later. No further expansion needed here.
4515 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4516 -- class-wide object to ensure that we copy the full object,
4517 -- unless we are targetting a VM where interfaces are handled by
4518 -- VM itself. Note that if the root type of Typ is an ancestor
4519 -- of Expr's type, both types share the same dispatch table and
4520 -- there is no need to displace the pointer.
4522 elsif Comes_From_Source (N)
4523 and then Is_Interface (Typ)
4525 pragma Assert (Is_Class_Wide_Type (Typ));
4527 -- If the object is a return object of an inherently limited type,
4528 -- which implies build-in-place treatment, bypass the special
4529 -- treatment of class-wide interface initialization below. In this
4530 -- case, the expansion of the return statement will take care of
4531 -- creating the object (via allocator) and initializing it.
4533 if Is_Return_Object (Def_Id)
4534 and then Is_Inherently_Limited_Type (Typ)
4538 elsif Tagged_Type_Expansion then
4540 Iface : constant Entity_Id := Root_Type (Typ);
4541 Expr_N : Node_Id := Expr;
4542 Expr_Typ : Entity_Id;
4549 -- If the original node of the expression was a conversion
4550 -- to this specific class-wide interface type then we
4551 -- restore the original node to generate code that
4552 -- statically displaces the pointer to the interface
4555 if not Comes_From_Source (Expr_N)
4556 and then Nkind (Expr_N) = N_Unchecked_Type_Conversion
4557 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
4558 and then Etype (Original_Node (Expr_N)) = Typ
4560 Rewrite (Expr_N, Original_Node (Expression (N)));
4563 -- Avoid expansion of redundant interface conversion
4565 if Is_Interface (Etype (Expr_N))
4566 and then Nkind (Expr_N) = N_Type_Conversion
4567 and then Etype (Expr_N) = Typ
4569 Expr_N := Expression (Expr_N);
4570 Set_Expression (N, Expr_N);
4573 Expr_Typ := Base_Type (Etype (Expr_N));
4575 if Is_Class_Wide_Type (Expr_Typ) then
4576 Expr_Typ := Root_Type (Expr_Typ);
4580 -- CW : I'Class := Obj;
4583 -- CW : I'Class renames TiC!(Tmp.I_Tag);
4585 if Comes_From_Source (Expr_N)
4586 and then Nkind (Expr_N) = N_Identifier
4587 and then not Is_Interface (Expr_Typ)
4588 and then (Expr_Typ = Etype (Expr_Typ)
4590 Is_Variable_Size_Record (Etype (Expr_Typ)))
4593 Make_Object_Declaration (Loc,
4594 Defining_Identifier =>
4595 Make_Defining_Identifier (Loc,
4596 New_Internal_Name ('D')),
4597 Object_Definition =>
4598 New_Occurrence_Of (Expr_Typ, Loc),
4600 Unchecked_Convert_To (Expr_Typ,
4601 Relocate_Node (Expr_N)));
4603 -- Statically reference the tag associated with the
4607 Make_Object_Renaming_Declaration (Loc,
4608 Defining_Identifier =>
4609 Make_Defining_Identifier (Loc,
4610 New_Internal_Name ('D')),
4612 New_Occurrence_Of (Typ, Loc),
4614 Unchecked_Convert_To (Typ,
4615 Make_Selected_Component (Loc,
4618 (Defining_Identifier (Decl_1), Loc),
4621 (Find_Interface_Tag (Expr_Typ, Iface),
4627 -- IW : I'Class := Obj;
4629 -- type Equiv_Record is record ... end record;
4630 -- implicit subtype CW is <Class_Wide_Subtype>;
4631 -- Temp : CW := CW!(Obj'Address);
4632 -- IW : I'Class renames Displace (Temp, I'Tag);
4635 -- Generate the equivalent record type
4637 Expand_Subtype_From_Expr
4640 Subtype_Indic => Object_Definition (N),
4641 Exp => Expression (N));
4643 if not Is_Interface (Etype (Expression (N))) then
4644 New_Expr := Relocate_Node (Expression (N));
4647 Make_Explicit_Dereference (Loc,
4648 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4649 Make_Attribute_Reference (Loc,
4650 Prefix => Relocate_Node (Expression (N)),
4651 Attribute_Name => Name_Address)));
4655 Make_Object_Declaration (Loc,
4656 Defining_Identifier =>
4657 Make_Defining_Identifier (Loc,
4658 New_Internal_Name ('D')),
4659 Object_Definition =>
4661 (Etype (Object_Definition (N)), Loc),
4663 Unchecked_Convert_To
4664 (Etype (Object_Definition (N)), New_Expr));
4667 Make_Object_Renaming_Declaration (Loc,
4668 Defining_Identifier =>
4669 Make_Defining_Identifier (Loc,
4670 New_Internal_Name ('D')),
4672 New_Occurrence_Of (Typ, Loc),
4674 Unchecked_Convert_To (Typ,
4675 Make_Explicit_Dereference (Loc,
4676 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4677 Make_Function_Call (Loc,
4679 New_Reference_To (RTE (RE_Displace), Loc),
4680 Parameter_Associations => New_List (
4681 Make_Attribute_Reference (Loc,
4684 (Defining_Identifier (Decl_1), Loc),
4685 Attribute_Name => Name_Address),
4687 Unchecked_Convert_To (RTE (RE_Tag),
4691 (Access_Disp_Table (Iface))),
4695 Insert_Action (N, Decl_1);
4696 Rewrite (N, Decl_2);
4699 -- Replace internal identifier of Decl_2 by the identifier
4700 -- found in the sources. We also have to exchange entities
4701 -- containing their defining identifiers to ensure the
4702 -- correct replacement of the object declaration by this
4703 -- object renaming declaration (because such definings
4704 -- identifier have been previously added by Enter_Name to
4705 -- the current scope). We must preserve the homonym chain
4706 -- of the source entity as well.
4708 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4709 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4710 Exchange_Entities (Defining_Identifier (N), Def_Id);
4717 -- In most cases, we must check that the initial value meets any
4718 -- constraint imposed by the declared type. However, there is one
4719 -- very important exception to this rule. If the entity has an
4720 -- unconstrained nominal subtype, then it acquired its constraints
4721 -- from the expression in the first place, and not only does this
4722 -- mean that the constraint check is not needed, but an attempt to
4723 -- perform the constraint check can cause order of elaboration
4726 if not Is_Constr_Subt_For_U_Nominal (Typ) then
4728 -- If this is an allocator for an aggregate that has been
4729 -- allocated in place, delay checks until assignments are
4730 -- made, because the discriminants are not initialized.
4732 if Nkind (Expr) = N_Allocator
4733 and then No_Initialization (Expr)
4737 Apply_Constraint_Check (Expr, Typ);
4739 -- If the expression has been marked as requiring a range
4740 -- generate it now and reset the flag.
4742 if Do_Range_Check (Expr) then
4743 Set_Do_Range_Check (Expr, False);
4744 Generate_Range_Check (Expr, Typ, CE_Range_Check_Failed);
4749 -- If the type is controlled and not inherently limited, then
4750 -- the target is adjusted after the copy and attached to the
4751 -- finalization list. However, no adjustment is done in the case
4752 -- where the object was initialized by a call to a function whose
4753 -- result is built in place, since no copy occurred. (Eventually
4754 -- we plan to support in-place function results for some cases
4755 -- of nonlimited types. ???) Similarly, no adjustment is required
4756 -- if we are going to rewrite the object declaration into a
4757 -- renaming declaration.
4759 if Needs_Finalization (Typ)
4760 and then not Is_Inherently_Limited_Type (Typ)
4761 and then not Rewrite_As_Renaming
4763 Insert_Actions_After (Init_After,
4765 Ref => New_Reference_To (Def_Id, Loc),
4766 Typ => Base_Type (Typ),
4767 Flist_Ref => Find_Final_List (Def_Id),
4768 With_Attach => Make_Integer_Literal (Loc, 1)));
4771 -- For tagged types, when an init value is given, the tag has to
4772 -- be re-initialized separately in order to avoid the propagation
4773 -- of a wrong tag coming from a view conversion unless the type
4774 -- is class wide (in this case the tag comes from the init value).
4775 -- Suppress the tag assignment when VM_Target because VM tags are
4776 -- represented implicitly in objects. Ditto for types that are
4777 -- CPP_CLASS, and for initializations that are aggregates, because
4778 -- they have to have the right tag.
4780 if Is_Tagged_Type (Typ)
4781 and then not Is_Class_Wide_Type (Typ)
4782 and then not Is_CPP_Class (Typ)
4783 and then Tagged_Type_Expansion
4784 and then Nkind (Expr) /= N_Aggregate
4786 -- The re-assignment of the tag has to be done even if the
4787 -- object is a constant.
4790 Make_Selected_Component (Loc,
4791 Prefix => New_Reference_To (Def_Id, Loc),
4793 New_Reference_To (First_Tag_Component (Typ), Loc));
4795 Set_Assignment_OK (New_Ref);
4797 Insert_After (Init_After,
4798 Make_Assignment_Statement (Loc,
4801 Unchecked_Convert_To (RTE (RE_Tag),
4805 (Access_Disp_Table (Base_Type (Typ)))),
4808 elsif Is_Tagged_Type (Typ)
4809 and then Is_CPP_Constructor_Call (Expr)
4811 -- The call to the initialization procedure does NOT freeze the
4812 -- object being initialized.
4814 Id_Ref := New_Reference_To (Def_Id, Loc);
4815 Set_Must_Not_Freeze (Id_Ref);
4816 Set_Assignment_OK (Id_Ref);
4818 Insert_Actions_After (Init_After,
4819 Build_Initialization_Call (Loc, Id_Ref, Typ,
4820 Constructor_Ref => Expr));
4822 -- We remove here the original call to the constructor
4823 -- to avoid its management in the backend
4825 Set_Expression (N, Empty);
4828 -- For discrete types, set the Is_Known_Valid flag if the
4829 -- initializing value is known to be valid.
4831 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
4832 Set_Is_Known_Valid (Def_Id);
4834 elsif Is_Access_Type (Typ) then
4836 -- For access types set the Is_Known_Non_Null flag if the
4837 -- initializing value is known to be non-null. We can also set
4838 -- Can_Never_Be_Null if this is a constant.
4840 if Known_Non_Null (Expr) then
4841 Set_Is_Known_Non_Null (Def_Id, True);
4843 if Constant_Present (N) then
4844 Set_Can_Never_Be_Null (Def_Id);
4849 -- If validity checking on copies, validate initial expression.
4850 -- But skip this if declaration is for a generic type, since it
4851 -- makes no sense to validate generic types. Not clear if this
4852 -- can happen for legal programs, but it definitely can arise
4853 -- from previous instantiation errors.
4855 if Validity_Checks_On
4856 and then Validity_Check_Copies
4857 and then not Is_Generic_Type (Etype (Def_Id))
4859 Ensure_Valid (Expr);
4860 Set_Is_Known_Valid (Def_Id);
4864 -- Cases where the back end cannot handle the initialization directly
4865 -- In such cases, we expand an assignment that will be appropriately
4866 -- handled by Expand_N_Assignment_Statement.
4868 -- The exclusion of the unconstrained case is wrong, but for now it
4869 -- is too much trouble ???
4871 if (Is_Possibly_Unaligned_Slice (Expr)
4872 or else (Is_Possibly_Unaligned_Object (Expr)
4873 and then not Represented_As_Scalar (Etype (Expr))))
4875 -- The exclusion of the unconstrained case is wrong, but for now
4876 -- it is too much trouble ???
4878 and then not (Is_Array_Type (Etype (Expr))
4879 and then not Is_Constrained (Etype (Expr)))
4882 Stat : constant Node_Id :=
4883 Make_Assignment_Statement (Loc,
4884 Name => New_Reference_To (Def_Id, Loc),
4885 Expression => Relocate_Node (Expr));
4887 Set_Expression (N, Empty);
4888 Set_No_Initialization (N);
4889 Set_Assignment_OK (Name (Stat));
4890 Set_No_Ctrl_Actions (Stat);
4891 Insert_After_And_Analyze (Init_After, Stat);
4895 -- Final transformation, if the initializing expression is an entity
4896 -- for a variable with OK_To_Rename set, then we transform:
4902 -- X : typ renames expr
4904 -- provided that X is not aliased. The aliased case has to be
4905 -- excluded in general because Expr will not be aliased in general.
4907 if Rewrite_As_Renaming then
4909 Make_Object_Renaming_Declaration (Loc,
4910 Defining_Identifier => Defining_Identifier (N),
4911 Subtype_Mark => Object_Definition (N),
4914 -- We do not analyze this renaming declaration, because all its
4915 -- components have already been analyzed, and if we were to go
4916 -- ahead and analyze it, we would in effect be trying to generate
4917 -- another declaration of X, which won't do!
4919 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
4926 when RE_Not_Available =>
4928 end Expand_N_Object_Declaration;
4930 ---------------------------------
4931 -- Expand_N_Subtype_Indication --
4932 ---------------------------------
4934 -- Add a check on the range of the subtype. The static case is partially
4935 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
4936 -- to check here for the static case in order to avoid generating
4937 -- extraneous expanded code. Also deal with validity checking.
4939 procedure Expand_N_Subtype_Indication (N : Node_Id) is
4940 Ran : constant Node_Id := Range_Expression (Constraint (N));
4941 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
4944 if Nkind (Constraint (N)) = N_Range_Constraint then
4945 Validity_Check_Range (Range_Expression (Constraint (N)));
4948 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
4949 Apply_Range_Check (Ran, Typ);
4951 end Expand_N_Subtype_Indication;
4953 ---------------------------
4954 -- Expand_N_Variant_Part --
4955 ---------------------------
4957 -- If the last variant does not contain the Others choice, replace it with
4958 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
4959 -- do not bother to call Analyze on the modified variant part, since it's
4960 -- only effect would be to compute the Others_Discrete_Choices node
4961 -- laboriously, and of course we already know the list of choices that
4962 -- corresponds to the others choice (it's the list we are replacing!)
4964 procedure Expand_N_Variant_Part (N : Node_Id) is
4965 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
4966 Others_Node : Node_Id;
4968 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
4969 Others_Node := Make_Others_Choice (Sloc (Last_Var));
4970 Set_Others_Discrete_Choices
4971 (Others_Node, Discrete_Choices (Last_Var));
4972 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
4974 end Expand_N_Variant_Part;
4976 ---------------------------------
4977 -- Expand_Previous_Access_Type --
4978 ---------------------------------
4980 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
4981 T : Entity_Id := First_Entity (Current_Scope);
4984 -- Find all access types declared in the current scope, whose
4985 -- designated type is Def_Id. If it does not have a Master_Id,
4988 while Present (T) loop
4989 if Is_Access_Type (T)
4990 and then Designated_Type (T) = Def_Id
4991 and then No (Master_Id (T))
4993 Build_Master_Entity (Def_Id);
4994 Build_Master_Renaming (Parent (Def_Id), T);
4999 end Expand_Previous_Access_Type;
5001 ------------------------------
5002 -- Expand_Record_Controller --
5003 ------------------------------
5005 procedure Expand_Record_Controller (T : Entity_Id) is
5006 Def : Node_Id := Type_Definition (Parent (T));
5007 Comp_List : Node_Id;
5008 Comp_Decl : Node_Id;
5010 First_Comp : Node_Id;
5011 Controller_Type : Entity_Id;
5015 if Nkind (Def) = N_Derived_Type_Definition then
5016 Def := Record_Extension_Part (Def);
5019 if Null_Present (Def) then
5020 Set_Component_List (Def,
5021 Make_Component_List (Sloc (Def),
5022 Component_Items => Empty_List,
5023 Variant_Part => Empty,
5024 Null_Present => True));
5027 Comp_List := Component_List (Def);
5029 if Null_Present (Comp_List)
5030 or else Is_Empty_List (Component_Items (Comp_List))
5032 Loc := Sloc (Comp_List);
5034 Loc := Sloc (First (Component_Items (Comp_List)));
5037 if Is_Inherently_Limited_Type (T) then
5038 Controller_Type := RTE (RE_Limited_Record_Controller);
5040 Controller_Type := RTE (RE_Record_Controller);
5043 Ent := Make_Defining_Identifier (Loc, Name_uController);
5046 Make_Component_Declaration (Loc,
5047 Defining_Identifier => Ent,
5048 Component_Definition =>
5049 Make_Component_Definition (Loc,
5050 Aliased_Present => False,
5051 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
5053 if Null_Present (Comp_List)
5054 or else Is_Empty_List (Component_Items (Comp_List))
5056 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5057 Set_Null_Present (Comp_List, False);
5060 -- The controller cannot be placed before the _Parent field since
5061 -- gigi lays out field in order and _parent must be first to preserve
5062 -- the polymorphism of tagged types.
5064 First_Comp := First (Component_Items (Comp_List));
5066 if not Is_Tagged_Type (T) then
5067 Insert_Before (First_Comp, Comp_Decl);
5069 -- if T is a tagged type, place controller declaration after parent
5070 -- field and after eventual tags of interface types.
5073 while Present (First_Comp)
5075 (Chars (Defining_Identifier (First_Comp)) = Name_uParent
5076 or else Is_Tag (Defining_Identifier (First_Comp))
5078 -- Ada 2005 (AI-251): The following condition covers secondary
5079 -- tags but also the adjacent component containing the offset
5080 -- to the base of the object (component generated if the parent
5081 -- has discriminants --- see Add_Interface_Tag_Components).
5082 -- This is required to avoid the addition of the controller
5083 -- between the secondary tag and its adjacent component.
5087 (Defining_Identifier (First_Comp))))
5092 -- An empty tagged extension might consist only of the parent
5093 -- component. Otherwise insert the controller before the first
5094 -- component that is neither parent nor tag.
5096 if Present (First_Comp) then
5097 Insert_Before (First_Comp, Comp_Decl);
5099 Append (Comp_Decl, Component_Items (Comp_List));
5105 Analyze (Comp_Decl);
5106 Set_Ekind (Ent, E_Component);
5107 Init_Component_Location (Ent);
5109 -- Move the _controller entity ahead in the list of internal entities
5110 -- of the enclosing record so that it is selected instead of a
5111 -- potentially inherited one.
5114 E : constant Entity_Id := Last_Entity (T);
5118 pragma Assert (Chars (E) = Name_uController);
5120 Set_Next_Entity (E, First_Entity (T));
5121 Set_First_Entity (T, E);
5123 Comp := Next_Entity (E);
5124 while Next_Entity (Comp) /= E loop
5128 Set_Next_Entity (Comp, Empty);
5129 Set_Last_Entity (T, Comp);
5135 when RE_Not_Available =>
5137 end Expand_Record_Controller;
5139 ------------------------
5140 -- Expand_Tagged_Root --
5141 ------------------------
5143 procedure Expand_Tagged_Root (T : Entity_Id) is
5144 Def : constant Node_Id := Type_Definition (Parent (T));
5145 Comp_List : Node_Id;
5146 Comp_Decl : Node_Id;
5147 Sloc_N : Source_Ptr;
5150 if Null_Present (Def) then
5151 Set_Component_List (Def,
5152 Make_Component_List (Sloc (Def),
5153 Component_Items => Empty_List,
5154 Variant_Part => Empty,
5155 Null_Present => True));
5158 Comp_List := Component_List (Def);
5160 if Null_Present (Comp_List)
5161 or else Is_Empty_List (Component_Items (Comp_List))
5163 Sloc_N := Sloc (Comp_List);
5165 Sloc_N := Sloc (First (Component_Items (Comp_List)));
5169 Make_Component_Declaration (Sloc_N,
5170 Defining_Identifier => First_Tag_Component (T),
5171 Component_Definition =>
5172 Make_Component_Definition (Sloc_N,
5173 Aliased_Present => False,
5174 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5176 if Null_Present (Comp_List)
5177 or else Is_Empty_List (Component_Items (Comp_List))
5179 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5180 Set_Null_Present (Comp_List, False);
5183 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5186 -- We don't Analyze the whole expansion because the tag component has
5187 -- already been analyzed previously. Here we just insure that the tree
5188 -- is coherent with the semantic decoration
5190 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5193 when RE_Not_Available =>
5195 end Expand_Tagged_Root;
5197 ----------------------
5198 -- Clean_Task_Names --
5199 ----------------------
5201 procedure Clean_Task_Names
5203 Proc_Id : Entity_Id)
5207 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5208 and then not Global_Discard_Names
5209 and then Tagged_Type_Expansion
5211 Set_Uses_Sec_Stack (Proc_Id);
5213 end Clean_Task_Names;
5215 ------------------------------
5216 -- Expand_Freeze_Array_Type --
5217 ------------------------------
5219 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5220 Typ : constant Entity_Id := Entity (N);
5221 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5222 Base : constant Entity_Id := Base_Type (Typ);
5225 if not Is_Bit_Packed_Array (Typ) then
5227 -- If the component contains tasks, so does the array type. This may
5228 -- not be indicated in the array type because the component may have
5229 -- been a private type at the point of definition. Same if component
5230 -- type is controlled.
5232 Set_Has_Task (Base, Has_Task (Comp_Typ));
5233 Set_Has_Controlled_Component (Base,
5234 Has_Controlled_Component (Comp_Typ)
5235 or else Is_Controlled (Comp_Typ));
5237 if No (Init_Proc (Base)) then
5239 -- If this is an anonymous array created for a declaration with
5240 -- an initial value, its init_proc will never be called. The
5241 -- initial value itself may have been expanded into assignments,
5242 -- in which case the object declaration is carries the
5243 -- No_Initialization flag.
5246 and then Nkind (Associated_Node_For_Itype (Base)) =
5247 N_Object_Declaration
5248 and then (Present (Expression (Associated_Node_For_Itype (Base)))
5250 No_Initialization (Associated_Node_For_Itype (Base)))
5254 -- We do not need an init proc for string or wide [wide] string,
5255 -- since the only time these need initialization in normalize or
5256 -- initialize scalars mode, and these types are treated specially
5257 -- and do not need initialization procedures.
5259 elsif Root_Type (Base) = Standard_String
5260 or else Root_Type (Base) = Standard_Wide_String
5261 or else Root_Type (Base) = Standard_Wide_Wide_String
5265 -- Otherwise we have to build an init proc for the subtype
5268 Build_Array_Init_Proc (Base, N);
5273 if Has_Controlled_Component (Base) then
5274 Build_Controlling_Procs (Base);
5276 if not Is_Limited_Type (Comp_Typ)
5277 and then Number_Dimensions (Typ) = 1
5279 Build_Slice_Assignment (Typ);
5282 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5283 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5285 Set_Associated_Final_Chain (Comp_Typ, Add_Final_Chain (Typ));
5289 -- For packed case, default initialization, except if the component type
5290 -- is itself a packed structure with an initialization procedure, or
5291 -- initialize/normalize scalars active, and we have a base type, or the
5292 -- type is public, because in that case a client might specify
5293 -- Normalize_Scalars and there better be a public Init_Proc for it.
5295 elsif (Present (Init_Proc (Component_Type (Base)))
5296 and then No (Base_Init_Proc (Base)))
5297 or else (Init_Or_Norm_Scalars and then Base = Typ)
5298 or else Is_Public (Typ)
5300 Build_Array_Init_Proc (Base, N);
5302 end Expand_Freeze_Array_Type;
5304 ------------------------------------
5305 -- Expand_Freeze_Enumeration_Type --
5306 ------------------------------------
5308 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5309 Typ : constant Entity_Id := Entity (N);
5310 Loc : constant Source_Ptr := Sloc (Typ);
5317 Is_Contiguous : Boolean;
5322 pragma Warnings (Off, Func);
5325 -- Various optimizations possible if given representation is contiguous
5327 Is_Contiguous := True;
5329 Ent := First_Literal (Typ);
5330 Last_Repval := Enumeration_Rep (Ent);
5333 while Present (Ent) loop
5334 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5335 Is_Contiguous := False;
5338 Last_Repval := Enumeration_Rep (Ent);
5344 if Is_Contiguous then
5345 Set_Has_Contiguous_Rep (Typ);
5346 Ent := First_Literal (Typ);
5348 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5351 -- Build list of literal references
5356 Ent := First_Literal (Typ);
5357 while Present (Ent) loop
5358 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5364 -- Now build an array declaration
5366 -- typA : array (Natural range 0 .. num - 1) of ctype :=
5367 -- (v, v, v, v, v, ....)
5369 -- where ctype is the corresponding integer type. If the representation
5370 -- is contiguous, we only keep the first literal, which provides the
5371 -- offset for Pos_To_Rep computations.
5374 Make_Defining_Identifier (Loc,
5375 Chars => New_External_Name (Chars (Typ), 'A'));
5377 Append_Freeze_Action (Typ,
5378 Make_Object_Declaration (Loc,
5379 Defining_Identifier => Arr,
5380 Constant_Present => True,
5382 Object_Definition =>
5383 Make_Constrained_Array_Definition (Loc,
5384 Discrete_Subtype_Definitions => New_List (
5385 Make_Subtype_Indication (Loc,
5386 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5388 Make_Range_Constraint (Loc,
5392 Make_Integer_Literal (Loc, 0),
5394 Make_Integer_Literal (Loc, Num - 1))))),
5396 Component_Definition =>
5397 Make_Component_Definition (Loc,
5398 Aliased_Present => False,
5399 Subtype_Indication => New_Reference_To (Typ, Loc))),
5402 Make_Aggregate (Loc,
5403 Expressions => Lst)));
5405 Set_Enum_Pos_To_Rep (Typ, Arr);
5407 -- Now we build the function that converts representation values to
5408 -- position values. This function has the form:
5410 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5413 -- when enum-lit'Enum_Rep => return posval;
5414 -- when enum-lit'Enum_Rep => return posval;
5417 -- [raise Constraint_Error when F "invalid data"]
5422 -- Note: the F parameter determines whether the others case (no valid
5423 -- representation) raises Constraint_Error or returns a unique value
5424 -- of minus one. The latter case is used, e.g. in 'Valid code.
5426 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5427 -- the code generator making inappropriate assumptions about the range
5428 -- of the values in the case where the value is invalid. ityp is a
5429 -- signed or unsigned integer type of appropriate width.
5431 -- Note: if exceptions are not supported, then we suppress the raise
5432 -- and return -1 unconditionally (this is an erroneous program in any
5433 -- case and there is no obligation to raise Constraint_Error here!) We
5434 -- also do this if pragma Restrictions (No_Exceptions) is active.
5436 -- Is this right??? What about No_Exception_Propagation???
5438 -- Representations are signed
5440 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5442 -- The underlying type is signed. Reset the Is_Unsigned_Type
5443 -- explicitly, because it might have been inherited from
5446 Set_Is_Unsigned_Type (Typ, False);
5448 if Esize (Typ) <= Standard_Integer_Size then
5449 Ityp := Standard_Integer;
5451 Ityp := Universal_Integer;
5454 -- Representations are unsigned
5457 if Esize (Typ) <= Standard_Integer_Size then
5458 Ityp := RTE (RE_Unsigned);
5460 Ityp := RTE (RE_Long_Long_Unsigned);
5464 -- The body of the function is a case statement. First collect case
5465 -- alternatives, or optimize the contiguous case.
5469 -- If representation is contiguous, Pos is computed by subtracting
5470 -- the representation of the first literal.
5472 if Is_Contiguous then
5473 Ent := First_Literal (Typ);
5475 if Enumeration_Rep (Ent) = Last_Repval then
5477 -- Another special case: for a single literal, Pos is zero
5479 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5483 Convert_To (Standard_Integer,
5484 Make_Op_Subtract (Loc,
5486 Unchecked_Convert_To (Ityp,
5487 Make_Identifier (Loc, Name_uA)),
5489 Make_Integer_Literal (Loc,
5491 Enumeration_Rep (First_Literal (Typ)))));
5495 Make_Case_Statement_Alternative (Loc,
5496 Discrete_Choices => New_List (
5497 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5499 Make_Integer_Literal (Loc,
5500 Intval => Enumeration_Rep (Ent)),
5502 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5504 Statements => New_List (
5505 Make_Simple_Return_Statement (Loc,
5506 Expression => Pos_Expr))));
5509 Ent := First_Literal (Typ);
5510 while Present (Ent) loop
5512 Make_Case_Statement_Alternative (Loc,
5513 Discrete_Choices => New_List (
5514 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5515 Intval => Enumeration_Rep (Ent))),
5517 Statements => New_List (
5518 Make_Simple_Return_Statement (Loc,
5520 Make_Integer_Literal (Loc,
5521 Intval => Enumeration_Pos (Ent))))));
5527 -- In normal mode, add the others clause with the test
5529 if not No_Exception_Handlers_Set then
5531 Make_Case_Statement_Alternative (Loc,
5532 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5533 Statements => New_List (
5534 Make_Raise_Constraint_Error (Loc,
5535 Condition => Make_Identifier (Loc, Name_uF),
5536 Reason => CE_Invalid_Data),
5537 Make_Simple_Return_Statement (Loc,
5539 Make_Integer_Literal (Loc, -1)))));
5541 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5542 -- active then return -1 (we cannot usefully raise Constraint_Error in
5543 -- this case). See description above for further details.
5547 Make_Case_Statement_Alternative (Loc,
5548 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5549 Statements => New_List (
5550 Make_Simple_Return_Statement (Loc,
5552 Make_Integer_Literal (Loc, -1)))));
5555 -- Now we can build the function body
5558 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5561 Make_Subprogram_Body (Loc,
5563 Make_Function_Specification (Loc,
5564 Defining_Unit_Name => Fent,
5565 Parameter_Specifications => New_List (
5566 Make_Parameter_Specification (Loc,
5567 Defining_Identifier =>
5568 Make_Defining_Identifier (Loc, Name_uA),
5569 Parameter_Type => New_Reference_To (Typ, Loc)),
5570 Make_Parameter_Specification (Loc,
5571 Defining_Identifier =>
5572 Make_Defining_Identifier (Loc, Name_uF),
5573 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5575 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5577 Declarations => Empty_List,
5579 Handled_Statement_Sequence =>
5580 Make_Handled_Sequence_Of_Statements (Loc,
5581 Statements => New_List (
5582 Make_Case_Statement (Loc,
5584 Unchecked_Convert_To (Ityp,
5585 Make_Identifier (Loc, Name_uA)),
5586 Alternatives => Lst))));
5588 Set_TSS (Typ, Fent);
5591 if not Debug_Generated_Code then
5592 Set_Debug_Info_Off (Fent);
5596 when RE_Not_Available =>
5598 end Expand_Freeze_Enumeration_Type;
5600 -------------------------------
5601 -- Expand_Freeze_Record_Type --
5602 -------------------------------
5604 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5606 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id);
5607 -- Add to the list of primitives of Tagged_Types the internal entities
5608 -- associated with interface primitives that are located in secondary
5611 -------------------------------------
5612 -- Add_Internal_Interface_Entities --
5613 -------------------------------------
5615 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
5618 Iface_Elmt : Elmt_Id;
5619 Iface_Prim : Entity_Id;
5620 Ifaces_List : Elist_Id;
5621 New_Subp : Entity_Id := Empty;
5625 pragma Assert (Ada_Version >= Ada_05
5626 and then Is_Record_Type (Tagged_Type)
5627 and then Is_Tagged_Type (Tagged_Type)
5628 and then Has_Interfaces (Tagged_Type)
5629 and then not Is_Interface (Tagged_Type));
5631 Collect_Interfaces (Tagged_Type, Ifaces_List);
5633 Iface_Elmt := First_Elmt (Ifaces_List);
5634 while Present (Iface_Elmt) loop
5635 Iface := Node (Iface_Elmt);
5637 -- Exclude from this processing interfaces that are parents
5638 -- of Tagged_Type because their primitives are located in the
5639 -- primary dispatch table (and hence no auxiliary internal
5640 -- entities are required to handle secondary dispatch tables
5643 if not Is_Ancestor (Iface, Tagged_Type) then
5644 Elmt := First_Elmt (Primitive_Operations (Iface));
5645 while Present (Elmt) loop
5646 Iface_Prim := Node (Elmt);
5648 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
5650 Find_Primitive_Covering_Interface
5651 (Tagged_Type => Tagged_Type,
5652 Iface_Prim => Iface_Prim);
5654 pragma Assert (Present (Prim));
5657 (New_Subp => New_Subp,
5658 Parent_Subp => Iface_Prim,
5659 Derived_Type => Tagged_Type,
5660 Parent_Type => Iface);
5662 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
5663 -- associated with interface types. These entities are
5664 -- only registered in the list of primitives of its
5665 -- corresponding tagged type because they are only used
5666 -- to fill the contents of the secondary dispatch tables.
5667 -- Therefore they are removed from the homonym chains.
5669 Set_Is_Hidden (New_Subp);
5670 Set_Is_Internal (New_Subp);
5671 Set_Alias (New_Subp, Prim);
5672 Set_Is_Abstract_Subprogram (New_Subp,
5673 Is_Abstract_Subprogram (Prim));
5674 Set_Interface_Alias (New_Subp, Iface_Prim);
5676 -- Internal entities associated with interface types are
5677 -- only registered in the list of primitives of the
5678 -- tagged type. They are only used to fill the contents
5679 -- of the secondary dispatch tables. Therefore they are
5680 -- not needed in the homonym chains.
5682 Remove_Homonym (New_Subp);
5684 -- Hidden entities associated with interfaces must have
5685 -- set the Has_Delay_Freeze attribute to ensure that, in
5686 -- case of locally defined tagged types (or compiling
5687 -- with static dispatch tables generation disabled) the
5688 -- corresponding entry of the secondary dispatch table is
5689 -- filled when such entity is frozen.
5691 Set_Has_Delayed_Freeze (New_Subp);
5698 Next_Elmt (Iface_Elmt);
5700 end Add_Internal_Interface_Entities;
5704 Def_Id : constant Node_Id := Entity (N);
5705 Type_Decl : constant Node_Id := Parent (Def_Id);
5707 Comp_Typ : Entity_Id;
5708 Has_Static_DT : Boolean := False;
5709 Predef_List : List_Id;
5711 Flist : Entity_Id := Empty;
5712 -- Finalization list allocated for the case of a type with anonymous
5713 -- access components whose designated type is potentially controlled.
5715 Renamed_Eq : Node_Id := Empty;
5716 -- Defining unit name for the predefined equality function in the case
5717 -- where the type has a primitive operation that is a renaming of
5718 -- predefined equality (but only if there is also an overriding
5719 -- user-defined equality function). Used to pass this entity from
5720 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5722 Wrapper_Decl_List : List_Id := No_List;
5723 Wrapper_Body_List : List_Id := No_List;
5724 Null_Proc_Decl_List : List_Id := No_List;
5726 -- Start of processing for Expand_Freeze_Record_Type
5729 -- Build discriminant checking functions if not a derived type (for
5730 -- derived types that are not tagged types, always use the discriminant
5731 -- checking functions of the parent type). However, for untagged types
5732 -- the derivation may have taken place before the parent was frozen, so
5733 -- we copy explicitly the discriminant checking functions from the
5734 -- parent into the components of the derived type.
5736 if not Is_Derived_Type (Def_Id)
5737 or else Has_New_Non_Standard_Rep (Def_Id)
5738 or else Is_Tagged_Type (Def_Id)
5740 Build_Discr_Checking_Funcs (Type_Decl);
5742 elsif Is_Derived_Type (Def_Id)
5743 and then not Is_Tagged_Type (Def_Id)
5745 -- If we have a derived Unchecked_Union, we do not inherit the
5746 -- discriminant checking functions from the parent type since the
5747 -- discriminants are non existent.
5749 and then not Is_Unchecked_Union (Def_Id)
5750 and then Has_Discriminants (Def_Id)
5753 Old_Comp : Entity_Id;
5757 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5758 Comp := First_Component (Def_Id);
5759 while Present (Comp) loop
5760 if Ekind (Comp) = E_Component
5761 and then Chars (Comp) = Chars (Old_Comp)
5763 Set_Discriminant_Checking_Func (Comp,
5764 Discriminant_Checking_Func (Old_Comp));
5767 Next_Component (Old_Comp);
5768 Next_Component (Comp);
5773 if Is_Derived_Type (Def_Id)
5774 and then Is_Limited_Type (Def_Id)
5775 and then Is_Tagged_Type (Def_Id)
5777 Check_Stream_Attributes (Def_Id);
5780 -- Update task and controlled component flags, because some of the
5781 -- component types may have been private at the point of the record
5784 Comp := First_Component (Def_Id);
5786 while Present (Comp) loop
5787 Comp_Typ := Etype (Comp);
5789 if Has_Task (Comp_Typ) then
5790 Set_Has_Task (Def_Id);
5792 elsif Has_Controlled_Component (Comp_Typ)
5793 or else (Chars (Comp) /= Name_uParent
5794 and then Is_Controlled (Comp_Typ))
5796 Set_Has_Controlled_Component (Def_Id);
5798 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5799 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5802 Flist := Add_Final_Chain (Def_Id);
5805 Set_Associated_Final_Chain (Comp_Typ, Flist);
5808 Next_Component (Comp);
5811 -- Handle constructors of non-tagged CPP_Class types
5813 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
5814 Set_CPP_Constructors (Def_Id);
5817 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5818 -- for regular tagged types as well as for Ada types deriving from a C++
5819 -- Class, but not for tagged types directly corresponding to C++ classes
5820 -- In the later case we assume that it is created in the C++ side and we
5823 if Is_Tagged_Type (Def_Id) then
5825 Static_Dispatch_Tables
5826 and then Is_Library_Level_Tagged_Type (Def_Id);
5828 -- Add the _Tag component
5830 if Underlying_Type (Etype (Def_Id)) = Def_Id then
5831 Expand_Tagged_Root (Def_Id);
5834 if Is_CPP_Class (Def_Id) then
5835 Set_All_DT_Position (Def_Id);
5836 Set_CPP_Constructors (Def_Id);
5838 -- Create the tag entities with a minimum decoration
5840 if Tagged_Type_Expansion then
5841 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5845 if not Has_Static_DT then
5847 -- Usually inherited primitives are not delayed but the first
5848 -- Ada extension of a CPP_Class is an exception since the
5849 -- address of the inherited subprogram has to be inserted in
5850 -- the new Ada Dispatch Table and this is a freezing action.
5852 -- Similarly, if this is an inherited operation whose parent is
5853 -- not frozen yet, it is not in the DT of the parent, and we
5854 -- generate an explicit freeze node for the inherited operation
5855 -- so that it is properly inserted in the DT of the current
5859 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
5863 while Present (Elmt) loop
5864 Subp := Node (Elmt);
5866 if Present (Alias (Subp)) then
5867 if Is_CPP_Class (Etype (Def_Id)) then
5868 Set_Has_Delayed_Freeze (Subp);
5870 elsif Has_Delayed_Freeze (Alias (Subp))
5871 and then not Is_Frozen (Alias (Subp))
5873 Set_Is_Frozen (Subp, False);
5874 Set_Has_Delayed_Freeze (Subp);
5883 -- Unfreeze momentarily the type to add the predefined primitives
5884 -- operations. The reason we unfreeze is so that these predefined
5885 -- operations will indeed end up as primitive operations (which
5886 -- must be before the freeze point).
5888 Set_Is_Frozen (Def_Id, False);
5890 -- Do not add the spec of predefined primitives in case of
5891 -- CPP tagged type derivations that have convention CPP.
5893 if Is_CPP_Class (Root_Type (Def_Id))
5894 and then Convention (Def_Id) = Convention_CPP
5898 -- Do not add the spec of the predefined primitives if we are
5899 -- compiling under restriction No_Dispatching_Calls
5901 elsif not Restriction_Active (No_Dispatching_Calls) then
5902 Make_Predefined_Primitive_Specs
5903 (Def_Id, Predef_List, Renamed_Eq);
5904 Insert_List_Before_And_Analyze (N, Predef_List);
5907 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5908 -- wrapper functions for each nonoverridden inherited function
5909 -- with a controlling result of the type. The wrapper for such
5910 -- a function returns an extension aggregate that invokes the
5911 -- the parent function.
5913 if Ada_Version >= Ada_05
5914 and then not Is_Abstract_Type (Def_Id)
5915 and then Is_Null_Extension (Def_Id)
5917 Make_Controlling_Function_Wrappers
5918 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
5919 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5922 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5923 -- null procedure declarations for each set of homographic null
5924 -- procedures that are inherited from interface types but not
5925 -- overridden. This is done to ensure that the dispatch table
5926 -- entry associated with such null primitives are properly filled.
5928 if Ada_Version >= Ada_05
5929 and then Etype (Def_Id) /= Def_Id
5930 and then not Is_Abstract_Type (Def_Id)
5932 Make_Null_Procedure_Specs (Def_Id, Null_Proc_Decl_List);
5933 Insert_Actions (N, Null_Proc_Decl_List);
5936 -- Ada 2005 (AI-251): Add internal entities associated with
5937 -- secondary dispatch tables to the list of primitives of tagged
5938 -- types that are not interfaces
5940 if Ada_Version >= Ada_05
5941 and then not Is_Interface (Def_Id)
5942 and then Has_Interfaces (Def_Id)
5944 Add_Internal_Interface_Entities (Def_Id);
5947 Set_Is_Frozen (Def_Id);
5948 Set_All_DT_Position (Def_Id);
5950 -- Add the controlled component before the freezing actions
5951 -- referenced in those actions.
5953 if Has_New_Controlled_Component (Def_Id) then
5954 Expand_Record_Controller (Def_Id);
5957 -- Create and decorate the tags. Suppress their creation when
5958 -- VM_Target because the dispatching mechanism is handled
5959 -- internally by the VMs.
5961 if Tagged_Type_Expansion then
5962 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5964 -- Generate dispatch table of locally defined tagged type.
5965 -- Dispatch tables of library level tagged types are built
5966 -- later (see Analyze_Declarations).
5968 if not Has_Static_DT then
5969 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
5973 -- If the type has unknown discriminants, propagate dispatching
5974 -- information to its underlying record view, which does not get
5975 -- its own dispatch table.
5977 if Is_Derived_Type (Def_Id)
5978 and then Has_Unknown_Discriminants (Def_Id)
5979 and then Present (Underlying_Record_View (Def_Id))
5982 Rep : constant Entity_Id :=
5983 Underlying_Record_View (Def_Id);
5985 Set_Access_Disp_Table
5986 (Rep, Access_Disp_Table (Def_Id));
5987 Set_Dispatch_Table_Wrappers
5988 (Rep, Dispatch_Table_Wrappers (Def_Id));
5989 Set_Primitive_Operations
5990 (Rep, Primitive_Operations (Def_Id));
5994 -- Make sure that the primitives Initialize, Adjust and Finalize
5995 -- are Frozen before other TSS subprograms. We don't want them
5998 if Is_Controlled (Def_Id) then
5999 if not Is_Limited_Type (Def_Id) then
6000 Append_Freeze_Actions (Def_Id,
6002 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
6005 Append_Freeze_Actions (Def_Id,
6007 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
6009 Append_Freeze_Actions (Def_Id,
6011 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
6014 -- Freeze rest of primitive operations. There is no need to handle
6015 -- the predefined primitives if we are compiling under restriction
6016 -- No_Dispatching_Calls
6018 if not Restriction_Active (No_Dispatching_Calls) then
6019 Append_Freeze_Actions
6020 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
6024 -- In the non-tagged case, an equality function is provided only for
6025 -- variant records (that are not unchecked unions).
6027 elsif Has_Discriminants (Def_Id)
6028 and then not Is_Limited_Type (Def_Id)
6031 Comps : constant Node_Id :=
6032 Component_List (Type_Definition (Type_Decl));
6036 and then Present (Variant_Part (Comps))
6038 Build_Variant_Record_Equality (Def_Id);
6043 -- Before building the record initialization procedure, if we are
6044 -- dealing with a concurrent record value type, then we must go through
6045 -- the discriminants, exchanging discriminals between the concurrent
6046 -- type and the concurrent record value type. See the section "Handling
6047 -- of Discriminants" in the Einfo spec for details.
6049 if Is_Concurrent_Record_Type (Def_Id)
6050 and then Has_Discriminants (Def_Id)
6053 Ctyp : constant Entity_Id :=
6054 Corresponding_Concurrent_Type (Def_Id);
6055 Conc_Discr : Entity_Id;
6056 Rec_Discr : Entity_Id;
6060 Conc_Discr := First_Discriminant (Ctyp);
6061 Rec_Discr := First_Discriminant (Def_Id);
6063 while Present (Conc_Discr) loop
6064 Temp := Discriminal (Conc_Discr);
6065 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6066 Set_Discriminal (Rec_Discr, Temp);
6068 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6069 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
6071 Next_Discriminant (Conc_Discr);
6072 Next_Discriminant (Rec_Discr);
6077 if Has_Controlled_Component (Def_Id) then
6078 if No (Controller_Component (Def_Id)) then
6079 Expand_Record_Controller (Def_Id);
6082 Build_Controlling_Procs (Def_Id);
6085 Adjust_Discriminants (Def_Id);
6087 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
6089 -- Do not need init for interfaces on e.g. CIL since they're
6090 -- abstract. Helps operation of peverify (the PE Verify tool).
6092 Build_Record_Init_Proc (Type_Decl, Def_Id);
6095 -- For tagged type that are not interfaces, build bodies of primitive
6096 -- operations. Note that we do this after building the record
6097 -- initialization procedure, since the primitive operations may need
6098 -- the initialization routine. There is no need to add predefined
6099 -- primitives of interfaces because all their predefined primitives
6102 if Is_Tagged_Type (Def_Id)
6103 and then not Is_Interface (Def_Id)
6105 -- Do not add the body of predefined primitives in case of
6106 -- CPP tagged type derivations that have convention CPP.
6108 if Is_CPP_Class (Root_Type (Def_Id))
6109 and then Convention (Def_Id) = Convention_CPP
6113 -- Do not add the body of the predefined primitives if we are
6114 -- compiling under restriction No_Dispatching_Calls or if we are
6115 -- compiling a CPP tagged type.
6117 elsif not Restriction_Active (No_Dispatching_Calls) then
6118 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6119 Append_Freeze_Actions (Def_Id, Predef_List);
6122 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6123 -- inherited functions, then add their bodies to the freeze actions.
6125 if Present (Wrapper_Body_List) then
6126 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6129 -- Create extra formals for the primitive operations of the type.
6130 -- This must be done before analyzing the body of the initialization
6131 -- procedure, because a self-referential type might call one of these
6132 -- primitives in the body of the init_proc itself.
6139 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6140 while Present (Elmt) loop
6141 Subp := Node (Elmt);
6142 if not Has_Foreign_Convention (Subp)
6143 and then not Is_Predefined_Dispatching_Operation (Subp)
6145 Create_Extra_Formals (Subp);
6152 end Expand_Freeze_Record_Type;
6154 ------------------------------
6155 -- Freeze_Stream_Operations --
6156 ------------------------------
6158 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6159 Names : constant array (1 .. 4) of TSS_Name_Type :=
6164 Stream_Op : Entity_Id;
6167 -- Primitive operations of tagged types are frozen when the dispatch
6168 -- table is constructed.
6170 if not Comes_From_Source (Typ)
6171 or else Is_Tagged_Type (Typ)
6176 for J in Names'Range loop
6177 Stream_Op := TSS (Typ, Names (J));
6179 if Present (Stream_Op)
6180 and then Is_Subprogram (Stream_Op)
6181 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6182 N_Subprogram_Declaration
6183 and then not Is_Frozen (Stream_Op)
6185 Append_Freeze_Actions
6186 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
6189 end Freeze_Stream_Operations;
6195 -- Full type declarations are expanded at the point at which the type is
6196 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
6197 -- declarations generated by the freezing (e.g. the procedure generated
6198 -- for initialization) are chained in the Actions field list of the freeze
6199 -- node using Append_Freeze_Actions.
6201 function Freeze_Type (N : Node_Id) return Boolean is
6202 Def_Id : constant Entity_Id := Entity (N);
6203 RACW_Seen : Boolean := False;
6204 Result : Boolean := False;
6207 -- Process associated access types needing special processing
6209 if Present (Access_Types_To_Process (N)) then
6211 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
6213 while Present (E) loop
6215 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6216 Validate_RACW_Primitives (Node (E));
6226 -- If there are RACWs designating this type, make stubs now
6228 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
6232 -- Freeze processing for record types
6234 if Is_Record_Type (Def_Id) then
6235 if Ekind (Def_Id) = E_Record_Type then
6236 Expand_Freeze_Record_Type (N);
6238 -- The subtype may have been declared before the type was frozen. If
6239 -- the type has controlled components it is necessary to create the
6240 -- entity for the controller explicitly because it did not exist at
6241 -- the point of the subtype declaration. Only the entity is needed,
6242 -- the back-end will obtain the layout from the type. This is only
6243 -- necessary if this is constrained subtype whose component list is
6244 -- not shared with the base type.
6246 elsif Ekind (Def_Id) = E_Record_Subtype
6247 and then Has_Discriminants (Def_Id)
6248 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
6249 and then Present (Controller_Component (Def_Id))
6252 Old_C : constant Entity_Id := Controller_Component (Def_Id);
6256 if Scope (Old_C) = Base_Type (Def_Id) then
6258 -- The entity is the one in the parent. Create new one
6260 New_C := New_Copy (Old_C);
6261 Set_Parent (New_C, Parent (Old_C));
6262 Push_Scope (Def_Id);
6268 if Is_Itype (Def_Id)
6269 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
6271 -- The freeze node is only used to introduce the controller,
6272 -- the back-end has no use for it for a discriminated
6275 Set_Freeze_Node (Def_Id, Empty);
6276 Set_Has_Delayed_Freeze (Def_Id, False);
6280 -- Similar process if the controller of the subtype is not present
6281 -- but the parent has it. This can happen with constrained
6282 -- record components where the subtype is an itype.
6284 elsif Ekind (Def_Id) = E_Record_Subtype
6285 and then Is_Itype (Def_Id)
6286 and then No (Controller_Component (Def_Id))
6287 and then Present (Controller_Component (Etype (Def_Id)))
6290 Old_C : constant Entity_Id :=
6291 Controller_Component (Etype (Def_Id));
6292 New_C : constant Entity_Id := New_Copy (Old_C);
6295 Set_Next_Entity (New_C, First_Entity (Def_Id));
6296 Set_First_Entity (Def_Id, New_C);
6298 -- The freeze node is only used to introduce the controller,
6299 -- the back-end has no use for it for a discriminated
6302 Set_Freeze_Node (Def_Id, Empty);
6303 Set_Has_Delayed_Freeze (Def_Id, False);
6308 -- Freeze processing for array types
6310 elsif Is_Array_Type (Def_Id) then
6311 Expand_Freeze_Array_Type (N);
6313 -- Freeze processing for access types
6315 -- For pool-specific access types, find out the pool object used for
6316 -- this type, needs actual expansion of it in some cases. Here are the
6317 -- different cases :
6319 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
6320 -- ---> don't use any storage pool
6322 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
6324 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
6326 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6327 -- ---> Storage Pool is the specified one
6329 -- See GNAT Pool packages in the Run-Time for more details
6331 elsif Ekind (Def_Id) = E_Access_Type
6332 or else Ekind (Def_Id) = E_General_Access_Type
6335 Loc : constant Source_Ptr := Sloc (N);
6336 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
6337 Pool_Object : Entity_Id;
6339 Freeze_Action_Typ : Entity_Id;
6344 -- Rep Clause "for Def_Id'Storage_Size use 0;"
6345 -- ---> don't use any storage pool
6347 if No_Pool_Assigned (Def_Id) then
6352 -- Rep Clause : for Def_Id'Storage_Size use Expr.
6354 -- Def_Id__Pool : Stack_Bounded_Pool
6355 -- (Expr, DT'Size, DT'Alignment);
6357 elsif Has_Storage_Size_Clause (Def_Id) then
6363 -- For unconstrained composite types we give a size of zero
6364 -- so that the pool knows that it needs a special algorithm
6365 -- for variable size object allocation.
6367 if Is_Composite_Type (Desig_Type)
6368 and then not Is_Constrained (Desig_Type)
6371 Make_Integer_Literal (Loc, 0);
6374 Make_Integer_Literal (Loc, Maximum_Alignment);
6378 Make_Attribute_Reference (Loc,
6379 Prefix => New_Reference_To (Desig_Type, Loc),
6380 Attribute_Name => Name_Max_Size_In_Storage_Elements);
6383 Make_Attribute_Reference (Loc,
6384 Prefix => New_Reference_To (Desig_Type, Loc),
6385 Attribute_Name => Name_Alignment);
6389 Make_Defining_Identifier (Loc,
6390 Chars => New_External_Name (Chars (Def_Id), 'P'));
6392 -- We put the code associated with the pools in the entity
6393 -- that has the later freeze node, usually the access type
6394 -- but it can also be the designated_type; because the pool
6395 -- code requires both those types to be frozen
6397 if Is_Frozen (Desig_Type)
6398 and then (No (Freeze_Node (Desig_Type))
6399 or else Analyzed (Freeze_Node (Desig_Type)))
6401 Freeze_Action_Typ := Def_Id;
6403 -- A Taft amendment type cannot get the freeze actions
6404 -- since the full view is not there.
6406 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6407 and then No (Full_View (Desig_Type))
6409 Freeze_Action_Typ := Def_Id;
6412 Freeze_Action_Typ := Desig_Type;
6415 Append_Freeze_Action (Freeze_Action_Typ,
6416 Make_Object_Declaration (Loc,
6417 Defining_Identifier => Pool_Object,
6418 Object_Definition =>
6419 Make_Subtype_Indication (Loc,
6422 (RTE (RE_Stack_Bounded_Pool), Loc),
6425 Make_Index_Or_Discriminant_Constraint (Loc,
6426 Constraints => New_List (
6428 -- First discriminant is the Pool Size
6431 Storage_Size_Variable (Def_Id), Loc),
6433 -- Second discriminant is the element size
6437 -- Third discriminant is the alignment
6442 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6446 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6447 -- ---> Storage Pool is the specified one
6449 elsif Present (Associated_Storage_Pool (Def_Id)) then
6451 -- Nothing to do the associated storage pool has been attached
6452 -- when analyzing the rep. clause
6457 -- For access-to-controlled types (including class-wide types and
6458 -- Taft-amendment types which potentially have controlled
6459 -- components), expand the list controller object that will store
6460 -- the dynamically allocated objects. Do not do this
6461 -- transformation for expander-generated access types, but do it
6462 -- for types that are the full view of types derived from other
6463 -- private types. Also suppress the list controller in the case
6464 -- of a designated type with convention Java, since this is used
6465 -- when binding to Java API specs, where there's no equivalent of
6466 -- a finalization list and we don't want to pull in the
6467 -- finalization support if not needed.
6469 if not Comes_From_Source (Def_Id)
6470 and then not Has_Private_Declaration (Def_Id)
6474 elsif (Needs_Finalization (Desig_Type)
6475 and then Convention (Desig_Type) /= Convention_Java
6476 and then Convention (Desig_Type) /= Convention_CIL)
6478 (Is_Incomplete_Or_Private_Type (Desig_Type)
6479 and then No (Full_View (Desig_Type))
6481 -- An exception is made for types defined in the run-time
6482 -- because Ada.Tags.Tag itself is such a type and cannot
6483 -- afford this unnecessary overhead that would generates a
6484 -- loop in the expansion scheme...
6486 and then not In_Runtime (Def_Id)
6488 -- Another exception is if Restrictions (No_Finalization)
6489 -- is active, since then we know nothing is controlled.
6491 and then not Restriction_Active (No_Finalization))
6493 -- If the designated type is not frozen yet, its controlled
6494 -- status must be retrieved explicitly.
6496 or else (Is_Array_Type (Desig_Type)
6497 and then not Is_Frozen (Desig_Type)
6498 and then Needs_Finalization (Component_Type (Desig_Type)))
6500 -- The designated type has controlled anonymous access
6503 or else Has_Controlled_Coextensions (Desig_Type)
6505 Set_Associated_Final_Chain (Def_Id, Add_Final_Chain (Def_Id));
6509 -- Freeze processing for enumeration types
6511 elsif Ekind (Def_Id) = E_Enumeration_Type then
6513 -- We only have something to do if we have a non-standard
6514 -- representation (i.e. at least one literal whose pos value
6515 -- is not the same as its representation)
6517 if Has_Non_Standard_Rep (Def_Id) then
6518 Expand_Freeze_Enumeration_Type (N);
6521 -- Private types that are completed by a derivation from a private
6522 -- type have an internally generated full view, that needs to be
6523 -- frozen. This must be done explicitly because the two views share
6524 -- the freeze node, and the underlying full view is not visible when
6525 -- the freeze node is analyzed.
6527 elsif Is_Private_Type (Def_Id)
6528 and then Is_Derived_Type (Def_Id)
6529 and then Present (Full_View (Def_Id))
6530 and then Is_Itype (Full_View (Def_Id))
6531 and then Has_Private_Declaration (Full_View (Def_Id))
6532 and then Freeze_Node (Full_View (Def_Id)) = N
6534 Set_Entity (N, Full_View (Def_Id));
6535 Result := Freeze_Type (N);
6536 Set_Entity (N, Def_Id);
6538 -- All other types require no expander action. There are such cases
6539 -- (e.g. task types and protected types). In such cases, the freeze
6540 -- nodes are there for use by Gigi.
6544 Freeze_Stream_Operations (N, Def_Id);
6548 when RE_Not_Available =>
6552 -------------------------
6553 -- Get_Simple_Init_Val --
6554 -------------------------
6556 function Get_Simple_Init_Val
6559 Size : Uint := No_Uint) return Node_Id
6561 Loc : constant Source_Ptr := Sloc (N);
6567 -- This is the size to be used for computation of the appropriate
6568 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
6570 IV_Attribute : constant Boolean :=
6571 Nkind (N) = N_Attribute_Reference
6572 and then Attribute_Name (N) = Name_Invalid_Value;
6576 -- These are the values computed by the procedure Check_Subtype_Bounds
6578 procedure Check_Subtype_Bounds;
6579 -- This procedure examines the subtype T, and its ancestor subtypes and
6580 -- derived types to determine the best known information about the
6581 -- bounds of the subtype. After the call Lo_Bound is set either to
6582 -- No_Uint if no information can be determined, or to a value which
6583 -- represents a known low bound, i.e. a valid value of the subtype can
6584 -- not be less than this value. Hi_Bound is similarly set to a known
6585 -- high bound (valid value cannot be greater than this).
6587 --------------------------
6588 -- Check_Subtype_Bounds --
6589 --------------------------
6591 procedure Check_Subtype_Bounds is
6600 Lo_Bound := No_Uint;
6601 Hi_Bound := No_Uint;
6603 -- Loop to climb ancestor subtypes and derived types
6607 if not Is_Discrete_Type (ST1) then
6611 Lo := Type_Low_Bound (ST1);
6612 Hi := Type_High_Bound (ST1);
6614 if Compile_Time_Known_Value (Lo) then
6615 Loval := Expr_Value (Lo);
6617 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6622 if Compile_Time_Known_Value (Hi) then
6623 Hival := Expr_Value (Hi);
6625 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6630 ST2 := Ancestor_Subtype (ST1);
6636 exit when ST1 = ST2;
6639 end Check_Subtype_Bounds;
6641 -- Start of processing for Get_Simple_Init_Val
6644 -- For a private type, we should always have an underlying type
6645 -- (because this was already checked in Needs_Simple_Initialization).
6646 -- What we do is to get the value for the underlying type and then do
6647 -- an Unchecked_Convert to the private type.
6649 if Is_Private_Type (T) then
6650 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6652 -- A special case, if the underlying value is null, then qualify it
6653 -- with the underlying type, so that the null is properly typed
6654 -- Similarly, if it is an aggregate it must be qualified, because an
6655 -- unchecked conversion does not provide a context for it.
6657 if Nkind_In (Val, N_Null, N_Aggregate) then
6659 Make_Qualified_Expression (Loc,
6661 New_Occurrence_Of (Underlying_Type (T), Loc),
6665 Result := Unchecked_Convert_To (T, Val);
6667 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6669 if Nkind (Result) = N_Unchecked_Type_Conversion
6670 and then Is_Scalar_Type (Underlying_Type (T))
6672 Set_No_Truncation (Result);
6677 -- For scalars, we must have normalize/initialize scalars case, or
6678 -- if the node N is an 'Invalid_Value attribute node.
6680 elsif Is_Scalar_Type (T) then
6681 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6683 -- Compute size of object. If it is given by the caller, we can use
6684 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6685 -- we know this covers all cases correctly.
6687 if Size = No_Uint or else Size <= Uint_0 then
6688 Size_To_Use := UI_Max (Uint_1, Esize (T));
6690 Size_To_Use := Size;
6693 -- Maximum size to use is 64 bits, since we will create values
6694 -- of type Unsigned_64 and the range must fit this type.
6696 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6697 Size_To_Use := Uint_64;
6700 -- Check known bounds of subtype
6702 Check_Subtype_Bounds;
6704 -- Processing for Normalize_Scalars case
6706 if Normalize_Scalars and then not IV_Attribute then
6708 -- If zero is invalid, it is a convenient value to use that is
6709 -- for sure an appropriate invalid value in all situations.
6711 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6712 Val := Make_Integer_Literal (Loc, 0);
6714 -- Cases where all one bits is the appropriate invalid value
6716 -- For modular types, all 1 bits is either invalid or valid. If
6717 -- it is valid, then there is nothing that can be done since there
6718 -- are no invalid values (we ruled out zero already).
6720 -- For signed integer types that have no negative values, either
6721 -- there is room for negative values, or there is not. If there
6722 -- is, then all 1 bits may be interpreted as minus one, which is
6723 -- certainly invalid. Alternatively it is treated as the largest
6724 -- positive value, in which case the observation for modular types
6727 -- For float types, all 1-bits is a NaN (not a number), which is
6728 -- certainly an appropriately invalid value.
6730 elsif Is_Unsigned_Type (T)
6731 or else Is_Floating_Point_Type (T)
6732 or else Is_Enumeration_Type (T)
6734 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6736 -- Resolve as Unsigned_64, because the largest number we
6737 -- can generate is out of range of universal integer.
6739 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6741 -- Case of signed types
6745 Signed_Size : constant Uint :=
6746 UI_Min (Uint_63, Size_To_Use - 1);
6749 -- Normally we like to use the most negative number. The
6750 -- one exception is when this number is in the known
6751 -- subtype range and the largest positive number is not in
6752 -- the known subtype range.
6754 -- For this exceptional case, use largest positive value
6756 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6757 and then Lo_Bound <= (-(2 ** Signed_Size))
6758 and then Hi_Bound < 2 ** Signed_Size
6760 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6762 -- Normal case of largest negative value
6765 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6770 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
6773 -- For float types, use float values from System.Scalar_Values
6775 if Is_Floating_Point_Type (T) then
6776 if Root_Type (T) = Standard_Short_Float then
6777 Val_RE := RE_IS_Isf;
6778 elsif Root_Type (T) = Standard_Float then
6779 Val_RE := RE_IS_Ifl;
6780 elsif Root_Type (T) = Standard_Long_Float then
6781 Val_RE := RE_IS_Ilf;
6782 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6783 Val_RE := RE_IS_Ill;
6786 -- If zero is invalid, use zero values from System.Scalar_Values
6788 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6789 if Size_To_Use <= 8 then
6790 Val_RE := RE_IS_Iz1;
6791 elsif Size_To_Use <= 16 then
6792 Val_RE := RE_IS_Iz2;
6793 elsif Size_To_Use <= 32 then
6794 Val_RE := RE_IS_Iz4;
6796 Val_RE := RE_IS_Iz8;
6799 -- For unsigned, use unsigned values from System.Scalar_Values
6801 elsif Is_Unsigned_Type (T) then
6802 if Size_To_Use <= 8 then
6803 Val_RE := RE_IS_Iu1;
6804 elsif Size_To_Use <= 16 then
6805 Val_RE := RE_IS_Iu2;
6806 elsif Size_To_Use <= 32 then
6807 Val_RE := RE_IS_Iu4;
6809 Val_RE := RE_IS_Iu8;
6812 -- For signed, use signed values from System.Scalar_Values
6815 if Size_To_Use <= 8 then
6816 Val_RE := RE_IS_Is1;
6817 elsif Size_To_Use <= 16 then
6818 Val_RE := RE_IS_Is2;
6819 elsif Size_To_Use <= 32 then
6820 Val_RE := RE_IS_Is4;
6822 Val_RE := RE_IS_Is8;
6826 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
6829 -- The final expression is obtained by doing an unchecked conversion
6830 -- of this result to the base type of the required subtype. We use
6831 -- the base type to avoid the unchecked conversion from chopping
6832 -- bits, and then we set Kill_Range_Check to preserve the "bad"
6835 Result := Unchecked_Convert_To (Base_Type (T), Val);
6837 -- Ensure result is not truncated, since we want the "bad" bits
6838 -- and also kill range check on result.
6840 if Nkind (Result) = N_Unchecked_Type_Conversion then
6841 Set_No_Truncation (Result);
6842 Set_Kill_Range_Check (Result, True);
6847 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
6849 elsif Root_Type (T) = Standard_String
6851 Root_Type (T) = Standard_Wide_String
6853 Root_Type (T) = Standard_Wide_Wide_String
6855 pragma Assert (Init_Or_Norm_Scalars);
6858 Make_Aggregate (Loc,
6859 Component_Associations => New_List (
6860 Make_Component_Association (Loc,
6861 Choices => New_List (
6862 Make_Others_Choice (Loc)),
6865 (Component_Type (T), N, Esize (Root_Type (T))))));
6867 -- Access type is initialized to null
6869 elsif Is_Access_Type (T) then
6873 -- No other possibilities should arise, since we should only be
6874 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
6875 -- returned True, indicating one of the above cases held.
6878 raise Program_Error;
6882 when RE_Not_Available =>
6884 end Get_Simple_Init_Val;
6886 ------------------------------
6887 -- Has_New_Non_Standard_Rep --
6888 ------------------------------
6890 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
6892 if not Is_Derived_Type (T) then
6893 return Has_Non_Standard_Rep (T)
6894 or else Has_Non_Standard_Rep (Root_Type (T));
6896 -- If Has_Non_Standard_Rep is not set on the derived type, the
6897 -- representation is fully inherited.
6899 elsif not Has_Non_Standard_Rep (T) then
6903 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
6905 -- May need a more precise check here: the First_Rep_Item may
6906 -- be a stream attribute, which does not affect the representation
6909 end Has_New_Non_Standard_Rep;
6915 function In_Runtime (E : Entity_Id) return Boolean is
6920 while Scope (S1) /= Standard_Standard loop
6924 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
6927 ----------------------------
6928 -- Initialization_Warning --
6929 ----------------------------
6931 procedure Initialization_Warning (E : Entity_Id) is
6932 Warning_Needed : Boolean;
6935 Warning_Needed := False;
6937 if Ekind (Current_Scope) = E_Package
6938 and then Static_Elaboration_Desired (Current_Scope)
6941 if Is_Record_Type (E) then
6942 if Has_Discriminants (E)
6943 or else Is_Limited_Type (E)
6944 or else Has_Non_Standard_Rep (E)
6946 Warning_Needed := True;
6949 -- Verify that at least one component has an initialization
6950 -- expression. No need for a warning on a type if all its
6951 -- components have no initialization.
6957 Comp := First_Component (E);
6958 while Present (Comp) loop
6959 if Ekind (Comp) = E_Discriminant
6961 (Nkind (Parent (Comp)) = N_Component_Declaration
6962 and then Present (Expression (Parent (Comp))))
6964 Warning_Needed := True;
6968 Next_Component (Comp);
6973 if Warning_Needed then
6975 ("Objects of the type cannot be initialized " &
6976 "statically by default?",
6982 Error_Msg_N ("Object cannot be initialized statically?", E);
6985 end Initialization_Warning;
6991 function Init_Formals (Typ : Entity_Id) return List_Id is
6992 Loc : constant Source_Ptr := Sloc (Typ);
6996 -- First parameter is always _Init : in out typ. Note that we need
6997 -- this to be in/out because in the case of the task record value,
6998 -- there are default record fields (_Priority, _Size, -Task_Info)
6999 -- that may be referenced in the generated initialization routine.
7001 Formals := New_List (
7002 Make_Parameter_Specification (Loc,
7003 Defining_Identifier =>
7004 Make_Defining_Identifier (Loc, Name_uInit),
7006 Out_Present => True,
7007 Parameter_Type => New_Reference_To (Typ, Loc)));
7009 -- For task record value, or type that contains tasks, add two more
7010 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7011 -- We also add these parameters for the task record type case.
7014 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7017 Make_Parameter_Specification (Loc,
7018 Defining_Identifier =>
7019 Make_Defining_Identifier (Loc, Name_uMaster),
7020 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
7023 Make_Parameter_Specification (Loc,
7024 Defining_Identifier =>
7025 Make_Defining_Identifier (Loc, Name_uChain),
7027 Out_Present => True,
7029 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
7032 Make_Parameter_Specification (Loc,
7033 Defining_Identifier =>
7034 Make_Defining_Identifier (Loc, Name_uTask_Name),
7037 New_Reference_To (Standard_String, Loc)));
7043 when RE_Not_Available =>
7047 -------------------------
7048 -- Init_Secondary_Tags --
7049 -------------------------
7051 procedure Init_Secondary_Tags
7054 Stmts_List : List_Id;
7055 Fixed_Comps : Boolean := True;
7056 Variable_Comps : Boolean := True)
7058 Loc : constant Source_Ptr := Sloc (Target);
7060 procedure Inherit_CPP_Tag
7063 Tag_Comp : Entity_Id;
7064 Iface_Tag : Node_Id);
7065 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
7066 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7068 procedure Initialize_Tag
7071 Tag_Comp : Entity_Id;
7072 Iface_Tag : Node_Id);
7073 -- Initialize the tag of the secondary dispatch table of Typ associated
7074 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7075 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
7076 -- of Typ CPP tagged type we generate code to inherit the contents of
7077 -- the dispatch table directly from the ancestor.
7079 ---------------------
7080 -- Inherit_CPP_Tag --
7081 ---------------------
7083 procedure Inherit_CPP_Tag
7086 Tag_Comp : Entity_Id;
7087 Iface_Tag : Node_Id)
7090 pragma Assert (Is_CPP_Class (Etype (Typ)));
7092 Append_To (Stmts_List,
7093 Build_Inherit_Prims (Loc,
7096 Make_Selected_Component (Loc,
7097 Prefix => New_Copy_Tree (Target),
7098 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7100 New_Reference_To (Iface_Tag, Loc),
7102 UI_To_Int (DT_Entry_Count (First_Tag_Component (Iface)))));
7103 end Inherit_CPP_Tag;
7105 --------------------
7106 -- Initialize_Tag --
7107 --------------------
7109 procedure Initialize_Tag
7112 Tag_Comp : Entity_Id;
7113 Iface_Tag : Node_Id)
7115 Comp_Typ : Entity_Id;
7116 Offset_To_Top_Comp : Entity_Id := Empty;
7119 -- Initialize the pointer to the secondary DT associated with the
7122 if not Is_Ancestor (Iface, Typ) then
7123 Append_To (Stmts_List,
7124 Make_Assignment_Statement (Loc,
7126 Make_Selected_Component (Loc,
7127 Prefix => New_Copy_Tree (Target),
7128 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7130 New_Reference_To (Iface_Tag, Loc)));
7133 Comp_Typ := Scope (Tag_Comp);
7135 -- Initialize the entries of the table of interfaces. We generate a
7136 -- different call when the parent of the type has variable size
7139 if Comp_Typ /= Etype (Comp_Typ)
7140 and then Is_Variable_Size_Record (Etype (Comp_Typ))
7141 and then Chars (Tag_Comp) /= Name_uTag
7143 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7145 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
7146 -- configurable run-time environment.
7148 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7150 ("variable size record with interface types", Typ);
7155 -- Set_Dynamic_Offset_To_Top
7157 -- Interface_T => Iface'Tag,
7158 -- Offset_Value => n,
7159 -- Offset_Func => Fn'Address)
7161 Append_To (Stmts_List,
7162 Make_Procedure_Call_Statement (Loc,
7163 Name => New_Reference_To
7164 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7165 Parameter_Associations => New_List (
7166 Make_Attribute_Reference (Loc,
7167 Prefix => New_Copy_Tree (Target),
7168 Attribute_Name => Name_Address),
7170 Unchecked_Convert_To (RTE (RE_Tag),
7172 (Node (First_Elmt (Access_Disp_Table (Iface))),
7175 Unchecked_Convert_To
7176 (RTE (RE_Storage_Offset),
7177 Make_Attribute_Reference (Loc,
7179 Make_Selected_Component (Loc,
7180 Prefix => New_Copy_Tree (Target),
7182 New_Reference_To (Tag_Comp, Loc)),
7183 Attribute_Name => Name_Position)),
7185 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7186 Make_Attribute_Reference (Loc,
7187 Prefix => New_Reference_To
7188 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7189 Attribute_Name => Name_Address)))));
7191 -- In this case the next component stores the value of the
7192 -- offset to the top.
7194 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7195 pragma Assert (Present (Offset_To_Top_Comp));
7197 Append_To (Stmts_List,
7198 Make_Assignment_Statement (Loc,
7200 Make_Selected_Component (Loc,
7201 Prefix => New_Copy_Tree (Target),
7202 Selector_Name => New_Reference_To
7203 (Offset_To_Top_Comp, Loc)),
7205 Make_Attribute_Reference (Loc,
7207 Make_Selected_Component (Loc,
7208 Prefix => New_Copy_Tree (Target),
7210 New_Reference_To (Tag_Comp, Loc)),
7211 Attribute_Name => Name_Position)));
7213 -- Normal case: No discriminants in the parent type
7216 -- Don't need to set any value if this interface shares
7217 -- the primary dispatch table.
7219 if not Is_Ancestor (Iface, Typ) then
7220 Append_To (Stmts_List,
7221 Build_Set_Static_Offset_To_Top (Loc,
7222 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
7224 Unchecked_Convert_To (RTE (RE_Storage_Offset),
7225 Make_Attribute_Reference (Loc,
7227 Make_Selected_Component (Loc,
7228 Prefix => New_Copy_Tree (Target),
7230 New_Reference_To (Tag_Comp, Loc)),
7231 Attribute_Name => Name_Position))));
7235 -- Register_Interface_Offset
7237 -- Interface_T => Iface'Tag,
7238 -- Is_Constant => True,
7239 -- Offset_Value => n,
7240 -- Offset_Func => null);
7242 if RTE_Available (RE_Register_Interface_Offset) then
7243 Append_To (Stmts_List,
7244 Make_Procedure_Call_Statement (Loc,
7245 Name => New_Reference_To
7246 (RTE (RE_Register_Interface_Offset), Loc),
7247 Parameter_Associations => New_List (
7248 Make_Attribute_Reference (Loc,
7249 Prefix => New_Copy_Tree (Target),
7250 Attribute_Name => Name_Address),
7252 Unchecked_Convert_To (RTE (RE_Tag),
7254 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
7256 New_Occurrence_Of (Standard_True, Loc),
7258 Unchecked_Convert_To
7259 (RTE (RE_Storage_Offset),
7260 Make_Attribute_Reference (Loc,
7262 Make_Selected_Component (Loc,
7263 Prefix => New_Copy_Tree (Target),
7265 New_Reference_To (Tag_Comp, Loc)),
7266 Attribute_Name => Name_Position)),
7275 Full_Typ : Entity_Id;
7276 Ifaces_List : Elist_Id;
7277 Ifaces_Comp_List : Elist_Id;
7278 Ifaces_Tag_List : Elist_Id;
7279 Iface_Elmt : Elmt_Id;
7280 Iface_Comp_Elmt : Elmt_Id;
7281 Iface_Tag_Elmt : Elmt_Id;
7283 In_Variable_Pos : Boolean;
7285 -- Start of processing for Init_Secondary_Tags
7288 -- Handle private types
7290 if Present (Full_View (Typ)) then
7291 Full_Typ := Full_View (Typ);
7296 Collect_Interfaces_Info
7297 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
7299 Iface_Elmt := First_Elmt (Ifaces_List);
7300 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
7301 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
7302 while Present (Iface_Elmt) loop
7303 Tag_Comp := Node (Iface_Comp_Elmt);
7305 -- If we are compiling under the CPP full ABI compatibility mode and
7306 -- the ancestor is a CPP_Pragma tagged type then we generate code to
7307 -- inherit the contents of the dispatch table directly from the
7310 if Is_CPP_Class (Etype (Full_Typ)) then
7311 Inherit_CPP_Tag (Full_Typ,
7312 Iface => Node (Iface_Elmt),
7313 Tag_Comp => Tag_Comp,
7314 Iface_Tag => Node (Iface_Tag_Elmt));
7316 -- Otherwise generate code to initialize the tag
7319 -- Check if the parent of the record type has variable size
7322 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
7323 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
7325 if (In_Variable_Pos and then Variable_Comps)
7326 or else (not In_Variable_Pos and then Fixed_Comps)
7328 Initialize_Tag (Full_Typ,
7329 Iface => Node (Iface_Elmt),
7330 Tag_Comp => Tag_Comp,
7331 Iface_Tag => Node (Iface_Tag_Elmt));
7335 Next_Elmt (Iface_Elmt);
7336 Next_Elmt (Iface_Comp_Elmt);
7337 Next_Elmt (Iface_Tag_Elmt);
7339 end Init_Secondary_Tags;
7341 -----------------------------
7342 -- Is_Variable_Size_Record --
7343 -----------------------------
7345 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7347 Comp_Typ : Entity_Id;
7350 function Is_Constant_Bound (Exp : Node_Id) return Boolean;
7351 -- To simplify handling of array components. Determines whether the
7352 -- given bound is constant (a constant or enumeration literal, or an
7353 -- integer literal) as opposed to per-object, through an expression
7354 -- or a discriminant.
7356 -----------------------
7357 -- Is_Constant_Bound --
7358 -----------------------
7360 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
7362 if Nkind (Exp) = N_Integer_Literal then
7366 Is_Entity_Name (Exp)
7367 and then Present (Entity (Exp))
7369 (Ekind (Entity (Exp)) = E_Constant
7370 or else Ekind (Entity (Exp)) = E_Enumeration_Literal);
7372 end Is_Constant_Bound;
7374 -- Start of processing for Is_Variable_Sized_Record
7377 pragma Assert (Is_Record_Type (E));
7379 Comp := First_Entity (E);
7380 while Present (Comp) loop
7381 Comp_Typ := Etype (Comp);
7383 if Is_Record_Type (Comp_Typ) then
7385 -- Recursive call if the record type has discriminants
7387 if Has_Discriminants (Comp_Typ)
7388 and then Is_Variable_Size_Record (Comp_Typ)
7393 elsif Is_Array_Type (Comp_Typ) then
7395 -- Check if some index is initialized with a non-constant value
7397 Idx := First_Index (Comp_Typ);
7398 while Present (Idx) loop
7399 if Nkind (Idx) = N_Range then
7400 if not Is_Constant_Bound (Low_Bound (Idx))
7402 not Is_Constant_Bound (High_Bound (Idx))
7408 Idx := Next_Index (Idx);
7416 end Is_Variable_Size_Record;
7418 ----------------------------------------
7419 -- Make_Controlling_Function_Wrappers --
7420 ----------------------------------------
7422 procedure Make_Controlling_Function_Wrappers
7423 (Tag_Typ : Entity_Id;
7424 Decl_List : out List_Id;
7425 Body_List : out List_Id)
7427 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7428 Prim_Elmt : Elmt_Id;
7430 Actual_List : List_Id;
7431 Formal_List : List_Id;
7433 Par_Formal : Entity_Id;
7434 Formal_Node : Node_Id;
7435 Func_Body : Node_Id;
7436 Func_Decl : Node_Id;
7437 Func_Spec : Node_Id;
7438 Return_Stmt : Node_Id;
7441 Decl_List := New_List;
7442 Body_List := New_List;
7444 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7446 while Present (Prim_Elmt) loop
7447 Subp := Node (Prim_Elmt);
7449 -- If a primitive function with a controlling result of the type has
7450 -- not been overridden by the user, then we must create a wrapper
7451 -- function here that effectively overrides it and invokes the
7452 -- (non-abstract) parent function. This can only occur for a null
7453 -- extension. Note that functions with anonymous controlling access
7454 -- results don't qualify and must be overridden. We also exclude
7455 -- Input attributes, since each type will have its own version of
7456 -- Input constructed by the expander. The test for Comes_From_Source
7457 -- is needed to distinguish inherited operations from renamings
7458 -- (which also have Alias set).
7460 -- The function may be abstract, or require_Overriding may be set
7461 -- for it, because tests for null extensions may already have reset
7462 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7463 -- set, functions that need wrappers are recognized by having an
7464 -- alias that returns the parent type.
7466 if Comes_From_Source (Subp)
7467 or else No (Alias (Subp))
7468 or else Ekind (Subp) /= E_Function
7469 or else not Has_Controlling_Result (Subp)
7470 or else Is_Access_Type (Etype (Subp))
7471 or else Is_Abstract_Subprogram (Alias (Subp))
7472 or else Is_TSS (Subp, TSS_Stream_Input)
7476 elsif Is_Abstract_Subprogram (Subp)
7477 or else Requires_Overriding (Subp)
7479 (Is_Null_Extension (Etype (Subp))
7480 and then Etype (Alias (Subp)) /= Etype (Subp))
7482 Formal_List := No_List;
7483 Formal := First_Formal (Subp);
7485 if Present (Formal) then
7486 Formal_List := New_List;
7488 while Present (Formal) loop
7490 (Make_Parameter_Specification
7492 Defining_Identifier =>
7493 Make_Defining_Identifier (Sloc (Formal),
7494 Chars => Chars (Formal)),
7495 In_Present => In_Present (Parent (Formal)),
7496 Out_Present => Out_Present (Parent (Formal)),
7497 Null_Exclusion_Present =>
7498 Null_Exclusion_Present (Parent (Formal)),
7500 New_Reference_To (Etype (Formal), Loc),
7502 New_Copy_Tree (Expression (Parent (Formal)))),
7505 Next_Formal (Formal);
7510 Make_Function_Specification (Loc,
7511 Defining_Unit_Name =>
7512 Make_Defining_Identifier (Loc,
7513 Chars => Chars (Subp)),
7514 Parameter_Specifications => Formal_List,
7515 Result_Definition =>
7516 New_Reference_To (Etype (Subp), Loc));
7518 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7519 Append_To (Decl_List, Func_Decl);
7521 -- Build a wrapper body that calls the parent function. The body
7522 -- contains a single return statement that returns an extension
7523 -- aggregate whose ancestor part is a call to the parent function,
7524 -- passing the formals as actuals (with any controlling arguments
7525 -- converted to the types of the corresponding formals of the
7526 -- parent function, which might be anonymous access types), and
7527 -- having a null extension.
7529 Formal := First_Formal (Subp);
7530 Par_Formal := First_Formal (Alias (Subp));
7531 Formal_Node := First (Formal_List);
7533 if Present (Formal) then
7534 Actual_List := New_List;
7536 Actual_List := No_List;
7539 while Present (Formal) loop
7540 if Is_Controlling_Formal (Formal) then
7541 Append_To (Actual_List,
7542 Make_Type_Conversion (Loc,
7544 New_Occurrence_Of (Etype (Par_Formal), Loc),
7547 (Defining_Identifier (Formal_Node), Loc)));
7552 (Defining_Identifier (Formal_Node), Loc));
7555 Next_Formal (Formal);
7556 Next_Formal (Par_Formal);
7561 Make_Simple_Return_Statement (Loc,
7563 Make_Extension_Aggregate (Loc,
7565 Make_Function_Call (Loc,
7566 Name => New_Reference_To (Alias (Subp), Loc),
7567 Parameter_Associations => Actual_List),
7568 Null_Record_Present => True));
7571 Make_Subprogram_Body (Loc,
7572 Specification => New_Copy_Tree (Func_Spec),
7573 Declarations => Empty_List,
7574 Handled_Statement_Sequence =>
7575 Make_Handled_Sequence_Of_Statements (Loc,
7576 Statements => New_List (Return_Stmt)));
7578 Set_Defining_Unit_Name
7579 (Specification (Func_Body),
7580 Make_Defining_Identifier (Loc, Chars (Subp)));
7582 Append_To (Body_List, Func_Body);
7584 -- Replace the inherited function with the wrapper function
7585 -- in the primitive operations list.
7587 Override_Dispatching_Operation
7588 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7592 Next_Elmt (Prim_Elmt);
7594 end Make_Controlling_Function_Wrappers;
7600 -- <Make_Eq_If shared components>
7602 -- when V1 => <Make_Eq_Case> on subcomponents
7604 -- when Vn => <Make_Eq_Case> on subcomponents
7607 function Make_Eq_Case
7610 Discr : Entity_Id := Empty) return List_Id
7612 Loc : constant Source_Ptr := Sloc (E);
7613 Result : constant List_Id := New_List;
7618 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7620 if No (Variant_Part (CL)) then
7624 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7626 if No (Variant) then
7630 Alt_List := New_List;
7632 while Present (Variant) loop
7633 Append_To (Alt_List,
7634 Make_Case_Statement_Alternative (Loc,
7635 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7636 Statements => Make_Eq_Case (E, Component_List (Variant))));
7638 Next_Non_Pragma (Variant);
7641 -- If we have an Unchecked_Union, use one of the parameters that
7642 -- captures the discriminants.
7644 if Is_Unchecked_Union (E) then
7646 Make_Case_Statement (Loc,
7647 Expression => New_Reference_To (Discr, Loc),
7648 Alternatives => Alt_List));
7652 Make_Case_Statement (Loc,
7654 Make_Selected_Component (Loc,
7655 Prefix => Make_Identifier (Loc, Name_X),
7656 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7657 Alternatives => Alt_List));
7678 -- or a null statement if the list L is empty
7682 L : List_Id) return Node_Id
7684 Loc : constant Source_Ptr := Sloc (E);
7686 Field_Name : Name_Id;
7691 return Make_Null_Statement (Loc);
7696 C := First_Non_Pragma (L);
7697 while Present (C) loop
7698 Field_Name := Chars (Defining_Identifier (C));
7700 -- The tags must not be compared: they are not part of the value.
7701 -- Ditto for the controller component, if present.
7703 -- Note also that in the following, we use Make_Identifier for
7704 -- the component names. Use of New_Reference_To to identify the
7705 -- components would be incorrect because the wrong entities for
7706 -- discriminants could be picked up in the private type case.
7708 if Field_Name /= Name_uTag
7710 Field_Name /= Name_uController
7712 Evolve_Or_Else (Cond,
7715 Make_Selected_Component (Loc,
7716 Prefix => Make_Identifier (Loc, Name_X),
7718 Make_Identifier (Loc, Field_Name)),
7721 Make_Selected_Component (Loc,
7722 Prefix => Make_Identifier (Loc, Name_Y),
7724 Make_Identifier (Loc, Field_Name))));
7727 Next_Non_Pragma (C);
7731 return Make_Null_Statement (Loc);
7735 Make_Implicit_If_Statement (E,
7737 Then_Statements => New_List (
7738 Make_Simple_Return_Statement (Loc,
7739 Expression => New_Occurrence_Of (Standard_False, Loc))));
7744 -------------------------------
7745 -- Make_Null_Procedure_Specs --
7746 -------------------------------
7748 procedure Make_Null_Procedure_Specs
7749 (Tag_Typ : Entity_Id;
7750 Decl_List : out List_Id)
7752 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7755 Formal_List : List_Id;
7756 New_Param_Spec : Node_Id;
7757 Parent_Subp : Entity_Id;
7758 Prim_Elmt : Elmt_Id;
7759 Proc_Decl : Node_Id;
7762 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean;
7763 -- Returns True if E is a null procedure that is an interface primitive
7765 ---------------------------------
7766 -- Is_Null_Interface_Primitive --
7767 ---------------------------------
7769 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
7771 return Comes_From_Source (E)
7772 and then Is_Dispatching_Operation (E)
7773 and then Ekind (E) = E_Procedure
7774 and then Null_Present (Parent (E))
7775 and then Is_Interface (Find_Dispatching_Type (E));
7776 end Is_Null_Interface_Primitive;
7778 -- Start of processing for Make_Null_Procedure_Specs
7781 Decl_List := New_List;
7782 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7783 while Present (Prim_Elmt) loop
7784 Subp := Node (Prim_Elmt);
7786 -- If a null procedure inherited from an interface has not been
7787 -- overridden, then we build a null procedure declaration to
7788 -- override the inherited procedure.
7790 Parent_Subp := Alias (Subp);
7792 if Present (Parent_Subp)
7793 and then Is_Null_Interface_Primitive (Parent_Subp)
7795 Formal_List := No_List;
7796 Formal := First_Formal (Subp);
7798 if Present (Formal) then
7799 Formal_List := New_List;
7801 while Present (Formal) loop
7803 -- Copy the parameter spec including default expressions
7806 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
7808 -- Generate a new defining identifier for the new formal.
7809 -- required because New_Copy_Tree does not duplicate
7810 -- semantic fields (except itypes).
7812 Set_Defining_Identifier (New_Param_Spec,
7813 Make_Defining_Identifier (Sloc (Formal),
7814 Chars => Chars (Formal)));
7816 -- For controlling arguments we must change their
7817 -- parameter type to reference the tagged type (instead
7818 -- of the interface type)
7820 if Is_Controlling_Formal (Formal) then
7821 if Nkind (Parameter_Type (Parent (Formal)))
7824 Set_Parameter_Type (New_Param_Spec,
7825 New_Occurrence_Of (Tag_Typ, Loc));
7828 (Nkind (Parameter_Type (Parent (Formal)))
7829 = N_Access_Definition);
7830 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
7831 New_Occurrence_Of (Tag_Typ, Loc));
7835 Append (New_Param_Spec, Formal_List);
7837 Next_Formal (Formal);
7842 Make_Subprogram_Declaration (Loc,
7843 Make_Procedure_Specification (Loc,
7844 Defining_Unit_Name =>
7845 Make_Defining_Identifier (Loc, Chars (Subp)),
7846 Parameter_Specifications => Formal_List,
7847 Null_Present => True));
7848 Append_To (Decl_List, Proc_Decl);
7849 Analyze (Proc_Decl);
7852 Next_Elmt (Prim_Elmt);
7854 end Make_Null_Procedure_Specs;
7856 -------------------------------------
7857 -- Make_Predefined_Primitive_Specs --
7858 -------------------------------------
7860 procedure Make_Predefined_Primitive_Specs
7861 (Tag_Typ : Entity_Id;
7862 Predef_List : out List_Id;
7863 Renamed_Eq : out Entity_Id)
7865 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7866 Res : constant List_Id := New_List;
7868 Eq_Needed : Boolean;
7870 Eq_Name : Name_Id := Name_Op_Eq;
7872 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
7873 -- Returns true if Prim is a renaming of an unresolved predefined
7874 -- equality operation.
7876 -------------------------------
7877 -- Is_Predefined_Eq_Renaming --
7878 -------------------------------
7880 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
7882 return Chars (Prim) /= Name_Op_Eq
7883 and then Present (Alias (Prim))
7884 and then Comes_From_Source (Prim)
7885 and then Is_Intrinsic_Subprogram (Alias (Prim))
7886 and then Chars (Alias (Prim)) = Name_Op_Eq;
7887 end Is_Predefined_Eq_Renaming;
7889 -- Start of processing for Make_Predefined_Primitive_Specs
7892 Renamed_Eq := Empty;
7896 Append_To (Res, Predef_Spec_Or_Body (Loc,
7899 Profile => New_List (
7900 Make_Parameter_Specification (Loc,
7901 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7902 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7904 Ret_Type => Standard_Long_Long_Integer));
7906 -- Spec of _Alignment
7908 Append_To (Res, Predef_Spec_Or_Body (Loc,
7910 Name => Name_uAlignment,
7911 Profile => New_List (
7912 Make_Parameter_Specification (Loc,
7913 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7914 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7916 Ret_Type => Standard_Integer));
7918 -- Specs for dispatching stream attributes
7921 Stream_Op_TSS_Names :
7922 constant array (Integer range <>) of TSS_Name_Type :=
7929 for Op in Stream_Op_TSS_Names'Range loop
7930 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
7932 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
7933 Stream_Op_TSS_Names (Op)));
7938 -- Spec of "=" is expanded if the type is not limited and if a
7939 -- user defined "=" was not already declared for the non-full
7940 -- view of a private extension
7942 if not Is_Limited_Type (Tag_Typ) then
7944 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7945 while Present (Prim) loop
7947 -- If a primitive is encountered that renames the predefined
7948 -- equality operator before reaching any explicit equality
7949 -- primitive, then we still need to create a predefined
7950 -- equality function, because calls to it can occur via
7951 -- the renaming. A new name is created for the equality
7952 -- to avoid conflicting with any user-defined equality.
7953 -- (Note that this doesn't account for renamings of
7954 -- equality nested within subpackages???)
7956 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7957 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
7959 -- User-defined equality
7961 elsif Chars (Node (Prim)) = Name_Op_Eq
7962 and then Etype (First_Formal (Node (Prim))) =
7963 Etype (Next_Formal (First_Formal (Node (Prim))))
7964 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
7966 if No (Alias (Node (Prim)))
7967 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
7968 N_Subprogram_Renaming_Declaration
7973 -- If the parent is not an interface type and has an abstract
7974 -- equality function, the inherited equality is abstract as
7975 -- well, and no body can be created for it.
7977 elsif not Is_Interface (Etype (Tag_Typ))
7978 and then Present (Alias (Node (Prim)))
7979 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
7984 -- If the type has an equality function corresponding with
7985 -- a primitive defined in an interface type, the inherited
7986 -- equality is abstract as well, and no body can be created
7989 elsif Present (Alias (Node (Prim)))
7990 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
7993 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
8003 -- If a renaming of predefined equality was found but there was no
8004 -- user-defined equality (so Eq_Needed is still true), then set the
8005 -- name back to Name_Op_Eq. But in the case where a user-defined
8006 -- equality was located after such a renaming, then the predefined
8007 -- equality function is still needed, so Eq_Needed must be set back
8010 if Eq_Name /= Name_Op_Eq then
8012 Eq_Name := Name_Op_Eq;
8019 Eq_Spec := Predef_Spec_Or_Body (Loc,
8022 Profile => New_List (
8023 Make_Parameter_Specification (Loc,
8024 Defining_Identifier =>
8025 Make_Defining_Identifier (Loc, Name_X),
8026 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8027 Make_Parameter_Specification (Loc,
8028 Defining_Identifier =>
8029 Make_Defining_Identifier (Loc, Name_Y),
8030 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8031 Ret_Type => Standard_Boolean);
8032 Append_To (Res, Eq_Spec);
8034 if Eq_Name /= Name_Op_Eq then
8035 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
8037 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8038 while Present (Prim) loop
8040 -- Any renamings of equality that appeared before an
8041 -- overriding equality must be updated to refer to the
8042 -- entity for the predefined equality, otherwise calls via
8043 -- the renaming would get incorrectly resolved to call the
8044 -- user-defined equality function.
8046 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8047 Set_Alias (Node (Prim), Renamed_Eq);
8049 -- Exit upon encountering a user-defined equality
8051 elsif Chars (Node (Prim)) = Name_Op_Eq
8052 and then No (Alias (Node (Prim)))
8062 -- Spec for dispatching assignment
8064 Append_To (Res, Predef_Spec_Or_Body (Loc,
8066 Name => Name_uAssign,
8067 Profile => New_List (
8068 Make_Parameter_Specification (Loc,
8069 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8070 Out_Present => True,
8071 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8073 Make_Parameter_Specification (Loc,
8074 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8075 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
8078 -- Ada 2005: Generate declarations for the following primitive
8079 -- operations for limited interfaces and synchronized types that
8080 -- implement a limited interface.
8082 -- Disp_Asynchronous_Select
8083 -- Disp_Conditional_Select
8084 -- Disp_Get_Prim_Op_Kind
8087 -- Disp_Timed_Select
8089 -- These operations cannot be implemented on VM targets, so we simply
8090 -- disable their generation in this case. Disable the generation of
8091 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
8093 if Ada_Version >= Ada_05
8094 and then Tagged_Type_Expansion
8095 and then not Restriction_Active (No_Dispatching_Calls)
8096 and then not Restriction_Active (No_Select_Statements)
8097 and then RTE_Available (RE_Select_Specific_Data)
8099 -- These primitives are defined abstract in interface types
8101 if Is_Interface (Tag_Typ)
8102 and then Is_Limited_Record (Tag_Typ)
8105 Make_Abstract_Subprogram_Declaration (Loc,
8107 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8110 Make_Abstract_Subprogram_Declaration (Loc,
8112 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8115 Make_Abstract_Subprogram_Declaration (Loc,
8117 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8120 Make_Abstract_Subprogram_Declaration (Loc,
8122 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8125 Make_Abstract_Subprogram_Declaration (Loc,
8127 Make_Disp_Requeue_Spec (Tag_Typ)));
8130 Make_Abstract_Subprogram_Declaration (Loc,
8132 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8134 -- If the ancestor is an interface type we declare non-abstract
8135 -- primitives to override the abstract primitives of the interface
8138 elsif (not Is_Interface (Tag_Typ)
8139 and then Is_Interface (Etype (Tag_Typ))
8140 and then Is_Limited_Record (Etype (Tag_Typ)))
8142 (Is_Concurrent_Record_Type (Tag_Typ)
8143 and then Has_Interfaces (Tag_Typ))
8146 Make_Subprogram_Declaration (Loc,
8148 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8151 Make_Subprogram_Declaration (Loc,
8153 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8156 Make_Subprogram_Declaration (Loc,
8158 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8161 Make_Subprogram_Declaration (Loc,
8163 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8166 Make_Subprogram_Declaration (Loc,
8168 Make_Disp_Requeue_Spec (Tag_Typ)));
8171 Make_Subprogram_Declaration (Loc,
8173 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8177 -- Specs for finalization actions that may be required in case a future
8178 -- extension contain a controlled element. We generate those only for
8179 -- root tagged types where they will get dummy bodies or when the type
8180 -- has controlled components and their body must be generated. It is
8181 -- also impossible to provide those for tagged types defined within
8182 -- s-finimp since it would involve circularity problems
8184 if In_Finalization_Root (Tag_Typ) then
8187 -- We also skip these if finalization is not available
8189 elsif Restriction_Active (No_Finalization) then
8192 elsif Etype (Tag_Typ) = Tag_Typ
8193 or else Needs_Finalization (Tag_Typ)
8195 -- Ada 2005 (AI-251): We must also generate these subprograms if
8196 -- the immediate ancestor is an interface to ensure the correct
8197 -- initialization of its dispatch table.
8199 or else (not Is_Interface (Tag_Typ)
8200 and then Is_Interface (Etype (Tag_Typ)))
8202 -- Ada 205 (AI-251): We must also generate these subprograms if
8203 -- the parent of an nonlimited interface is a limited interface
8205 or else (Is_Interface (Tag_Typ)
8206 and then not Is_Limited_Interface (Tag_Typ)
8207 and then Is_Limited_Interface (Etype (Tag_Typ)))
8209 if not Is_Limited_Type (Tag_Typ) then
8211 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
8214 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
8218 end Make_Predefined_Primitive_Specs;
8220 ---------------------------------
8221 -- Needs_Simple_Initialization --
8222 ---------------------------------
8224 function Needs_Simple_Initialization (T : Entity_Id) return Boolean is
8226 -- Check for private type, in which case test applies to the underlying
8227 -- type of the private type.
8229 if Is_Private_Type (T) then
8231 RT : constant Entity_Id := Underlying_Type (T);
8234 if Present (RT) then
8235 return Needs_Simple_Initialization (RT);
8241 -- Cases needing simple initialization are access types, and, if pragma
8242 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
8245 elsif Is_Access_Type (T)
8246 or else (Init_Or_Norm_Scalars and then (Is_Scalar_Type (T)))
8250 -- If Initialize/Normalize_Scalars is in effect, string objects also
8251 -- need initialization, unless they are created in the course of
8252 -- expanding an aggregate (since in the latter case they will be
8253 -- filled with appropriate initializing values before they are used).
8255 elsif Init_Or_Norm_Scalars
8257 (Root_Type (T) = Standard_String
8258 or else Root_Type (T) = Standard_Wide_String
8259 or else Root_Type (T) = Standard_Wide_Wide_String)
8262 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
8269 end Needs_Simple_Initialization;
8271 ----------------------
8272 -- Predef_Deep_Spec --
8273 ----------------------
8275 function Predef_Deep_Spec
8277 Tag_Typ : Entity_Id;
8278 Name : TSS_Name_Type;
8279 For_Body : Boolean := False) return Node_Id
8285 if Name = TSS_Deep_Finalize then
8287 Type_B := Standard_Boolean;
8291 Make_Parameter_Specification (Loc,
8292 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
8294 Out_Present => True,
8296 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
8297 Type_B := Standard_Short_Short_Integer;
8301 Make_Parameter_Specification (Loc,
8302 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8304 Out_Present => True,
8305 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
8308 Make_Parameter_Specification (Loc,
8309 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
8310 Parameter_Type => New_Reference_To (Type_B, Loc)));
8312 return Predef_Spec_Or_Body (Loc,
8313 Name => Make_TSS_Name (Tag_Typ, Name),
8316 For_Body => For_Body);
8319 when RE_Not_Available =>
8321 end Predef_Deep_Spec;
8323 -------------------------
8324 -- Predef_Spec_Or_Body --
8325 -------------------------
8327 function Predef_Spec_Or_Body
8329 Tag_Typ : Entity_Id;
8332 Ret_Type : Entity_Id := Empty;
8333 For_Body : Boolean := False) return Node_Id
8335 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
8339 Set_Is_Public (Id, Is_Public (Tag_Typ));
8341 -- The internal flag is set to mark these declarations because they have
8342 -- specific properties. First, they are primitives even if they are not
8343 -- defined in the type scope (the freezing point is not necessarily in
8344 -- the same scope). Second, the predefined equality can be overridden by
8345 -- a user-defined equality, no body will be generated in this case.
8347 Set_Is_Internal (Id);
8349 if not Debug_Generated_Code then
8350 Set_Debug_Info_Off (Id);
8353 if No (Ret_Type) then
8355 Make_Procedure_Specification (Loc,
8356 Defining_Unit_Name => Id,
8357 Parameter_Specifications => Profile);
8360 Make_Function_Specification (Loc,
8361 Defining_Unit_Name => Id,
8362 Parameter_Specifications => Profile,
8363 Result_Definition =>
8364 New_Reference_To (Ret_Type, Loc));
8367 if Is_Interface (Tag_Typ) then
8368 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8370 -- If body case, return empty subprogram body. Note that this is ill-
8371 -- formed, because there is not even a null statement, and certainly not
8372 -- a return in the function case. The caller is expected to do surgery
8373 -- on the body to add the appropriate stuff.
8376 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8378 -- For the case of an Input attribute predefined for an abstract type,
8379 -- generate an abstract specification. This will never be called, but we
8380 -- need the slot allocated in the dispatching table so that attributes
8381 -- typ'Class'Input and typ'Class'Output will work properly.
8383 elsif Is_TSS (Name, TSS_Stream_Input)
8384 and then Is_Abstract_Type (Tag_Typ)
8386 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8388 -- Normal spec case, where we return a subprogram declaration
8391 return Make_Subprogram_Declaration (Loc, Spec);
8393 end Predef_Spec_Or_Body;
8395 -----------------------------
8396 -- Predef_Stream_Attr_Spec --
8397 -----------------------------
8399 function Predef_Stream_Attr_Spec
8401 Tag_Typ : Entity_Id;
8402 Name : TSS_Name_Type;
8403 For_Body : Boolean := False) return Node_Id
8405 Ret_Type : Entity_Id;
8408 if Name = TSS_Stream_Input then
8409 Ret_Type := Tag_Typ;
8414 return Predef_Spec_Or_Body (Loc,
8415 Name => Make_TSS_Name (Tag_Typ, Name),
8417 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8418 Ret_Type => Ret_Type,
8419 For_Body => For_Body);
8420 end Predef_Stream_Attr_Spec;
8422 ---------------------------------
8423 -- Predefined_Primitive_Bodies --
8424 ---------------------------------
8426 function Predefined_Primitive_Bodies
8427 (Tag_Typ : Entity_Id;
8428 Renamed_Eq : Entity_Id) return List_Id
8430 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8431 Res : constant List_Id := New_List;
8434 Eq_Needed : Boolean;
8438 pragma Warnings (Off, Ent);
8441 pragma Assert (not Is_Interface (Tag_Typ));
8443 -- See if we have a predefined "=" operator
8445 if Present (Renamed_Eq) then
8447 Eq_Name := Chars (Renamed_Eq);
8449 -- If the parent is an interface type then it has defined all the
8450 -- predefined primitives abstract and we need to check if the type
8451 -- has some user defined "=" function to avoid generating it.
8453 elsif Is_Interface (Etype (Tag_Typ)) then
8455 Eq_Name := Name_Op_Eq;
8457 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8458 while Present (Prim) loop
8459 if Chars (Node (Prim)) = Name_Op_Eq
8460 and then not Is_Internal (Node (Prim))
8474 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8475 while Present (Prim) loop
8476 if Chars (Node (Prim)) = Name_Op_Eq
8477 and then Is_Internal (Node (Prim))
8480 Eq_Name := Name_Op_Eq;
8488 -- Body of _Alignment
8490 Decl := Predef_Spec_Or_Body (Loc,
8492 Name => Name_uAlignment,
8493 Profile => New_List (
8494 Make_Parameter_Specification (Loc,
8495 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8496 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8498 Ret_Type => Standard_Integer,
8501 Set_Handled_Statement_Sequence (Decl,
8502 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8503 Make_Simple_Return_Statement (Loc,
8505 Make_Attribute_Reference (Loc,
8506 Prefix => Make_Identifier (Loc, Name_X),
8507 Attribute_Name => Name_Alignment)))));
8509 Append_To (Res, Decl);
8513 Decl := Predef_Spec_Or_Body (Loc,
8516 Profile => New_List (
8517 Make_Parameter_Specification (Loc,
8518 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8519 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8521 Ret_Type => Standard_Long_Long_Integer,
8524 Set_Handled_Statement_Sequence (Decl,
8525 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8526 Make_Simple_Return_Statement (Loc,
8528 Make_Attribute_Reference (Loc,
8529 Prefix => Make_Identifier (Loc, Name_X),
8530 Attribute_Name => Name_Size)))));
8532 Append_To (Res, Decl);
8534 -- Bodies for Dispatching stream IO routines. We need these only for
8535 -- non-limited types (in the limited case there is no dispatching).
8536 -- We also skip them if dispatching or finalization are not available.
8538 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8539 and then No (TSS (Tag_Typ, TSS_Stream_Read))
8541 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8542 Append_To (Res, Decl);
8545 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8546 and then No (TSS (Tag_Typ, TSS_Stream_Write))
8548 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8549 Append_To (Res, Decl);
8552 -- Skip body of _Input for the abstract case, since the corresponding
8553 -- spec is abstract (see Predef_Spec_Or_Body).
8555 if not Is_Abstract_Type (Tag_Typ)
8556 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8557 and then No (TSS (Tag_Typ, TSS_Stream_Input))
8559 Build_Record_Or_Elementary_Input_Function
8560 (Loc, Tag_Typ, Decl, Ent);
8561 Append_To (Res, Decl);
8564 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8565 and then No (TSS (Tag_Typ, TSS_Stream_Output))
8567 Build_Record_Or_Elementary_Output_Procedure
8568 (Loc, Tag_Typ, Decl, Ent);
8569 Append_To (Res, Decl);
8572 -- Ada 2005: Generate bodies for the following primitive operations for
8573 -- limited interfaces and synchronized types that implement a limited
8576 -- disp_asynchronous_select
8577 -- disp_conditional_select
8578 -- disp_get_prim_op_kind
8580 -- disp_timed_select
8582 -- The interface versions will have null bodies
8584 -- These operations cannot be implemented on VM targets, so we simply
8585 -- disable their generation in this case. Disable the generation of
8586 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
8588 if Ada_Version >= Ada_05
8589 and then Tagged_Type_Expansion
8590 and then not Is_Interface (Tag_Typ)
8592 ((Is_Interface (Etype (Tag_Typ))
8593 and then Is_Limited_Record (Etype (Tag_Typ)))
8594 or else (Is_Concurrent_Record_Type (Tag_Typ)
8595 and then Has_Interfaces (Tag_Typ)))
8596 and then not Restriction_Active (No_Dispatching_Calls)
8597 and then not Restriction_Active (No_Select_Statements)
8598 and then RTE_Available (RE_Select_Specific_Data)
8600 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8601 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
8602 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
8603 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
8604 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
8605 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
8608 if not Is_Limited_Type (Tag_Typ)
8609 and then not Is_Interface (Tag_Typ)
8611 -- Body for equality
8615 Predef_Spec_Or_Body (Loc,
8618 Profile => New_List (
8619 Make_Parameter_Specification (Loc,
8620 Defining_Identifier =>
8621 Make_Defining_Identifier (Loc, Name_X),
8622 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8624 Make_Parameter_Specification (Loc,
8625 Defining_Identifier =>
8626 Make_Defining_Identifier (Loc, Name_Y),
8627 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8629 Ret_Type => Standard_Boolean,
8633 Def : constant Node_Id := Parent (Tag_Typ);
8634 Stmts : constant List_Id := New_List;
8635 Variant_Case : Boolean := Has_Discriminants (Tag_Typ);
8636 Comps : Node_Id := Empty;
8637 Typ_Def : Node_Id := Type_Definition (Def);
8640 if Variant_Case then
8641 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8642 Typ_Def := Record_Extension_Part (Typ_Def);
8645 if Present (Typ_Def) then
8646 Comps := Component_List (Typ_Def);
8649 Variant_Case := Present (Comps)
8650 and then Present (Variant_Part (Comps));
8653 if Variant_Case then
8655 Make_Eq_If (Tag_Typ, Discriminant_Specifications (Def)));
8656 Append_List_To (Stmts, Make_Eq_Case (Tag_Typ, Comps));
8658 Make_Simple_Return_Statement (Loc,
8659 Expression => New_Reference_To (Standard_True, Loc)));
8663 Make_Simple_Return_Statement (Loc,
8665 Expand_Record_Equality (Tag_Typ,
8667 Lhs => Make_Identifier (Loc, Name_X),
8668 Rhs => Make_Identifier (Loc, Name_Y),
8669 Bodies => Declarations (Decl))));
8672 Set_Handled_Statement_Sequence (Decl,
8673 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8675 Append_To (Res, Decl);
8678 -- Body for dispatching assignment
8681 Predef_Spec_Or_Body (Loc,
8683 Name => Name_uAssign,
8684 Profile => New_List (
8685 Make_Parameter_Specification (Loc,
8686 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8687 Out_Present => True,
8688 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8690 Make_Parameter_Specification (Loc,
8691 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8692 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8695 Set_Handled_Statement_Sequence (Decl,
8696 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8697 Make_Assignment_Statement (Loc,
8698 Name => Make_Identifier (Loc, Name_X),
8699 Expression => Make_Identifier (Loc, Name_Y)))));
8701 Append_To (Res, Decl);
8704 -- Generate dummy bodies for finalization actions of types that have
8705 -- no controlled components.
8707 -- Skip this processing if we are in the finalization routine in the
8708 -- runtime itself, otherwise we get hopelessly circularly confused!
8710 if In_Finalization_Root (Tag_Typ) then
8713 -- Skip this if finalization is not available
8715 elsif Restriction_Active (No_Finalization) then
8718 elsif (Etype (Tag_Typ) = Tag_Typ
8719 or else Is_Controlled (Tag_Typ)
8721 -- Ada 2005 (AI-251): We must also generate these subprograms
8722 -- if the immediate ancestor of Tag_Typ is an interface to
8723 -- ensure the correct initialization of its dispatch table.
8725 or else (not Is_Interface (Tag_Typ)
8727 Is_Interface (Etype (Tag_Typ))))
8728 and then not Has_Controlled_Component (Tag_Typ)
8730 if not Is_Limited_Type (Tag_Typ) then
8731 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8733 if Is_Controlled (Tag_Typ) then
8734 Set_Handled_Statement_Sequence (Decl,
8735 Make_Handled_Sequence_Of_Statements (Loc,
8737 Ref => Make_Identifier (Loc, Name_V),
8739 Flist_Ref => Make_Identifier (Loc, Name_L),
8740 With_Attach => Make_Identifier (Loc, Name_B))));
8743 Set_Handled_Statement_Sequence (Decl,
8744 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8745 Make_Null_Statement (Loc))));
8748 Append_To (Res, Decl);
8751 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
8753 if Is_Controlled (Tag_Typ) then
8754 Set_Handled_Statement_Sequence (Decl,
8755 Make_Handled_Sequence_Of_Statements (Loc,
8757 Ref => Make_Identifier (Loc, Name_V),
8759 With_Detach => Make_Identifier (Loc, Name_B))));
8762 Set_Handled_Statement_Sequence (Decl,
8763 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8764 Make_Null_Statement (Loc))));
8767 Append_To (Res, Decl);
8771 end Predefined_Primitive_Bodies;
8773 ---------------------------------
8774 -- Predefined_Primitive_Freeze --
8775 ---------------------------------
8777 function Predefined_Primitive_Freeze
8778 (Tag_Typ : Entity_Id) return List_Id
8780 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8781 Res : constant List_Id := New_List;
8786 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8787 while Present (Prim) loop
8788 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8789 Frnodes := Freeze_Entity (Node (Prim), Loc);
8791 if Present (Frnodes) then
8792 Append_List_To (Res, Frnodes);
8800 end Predefined_Primitive_Freeze;
8802 -------------------------
8803 -- Stream_Operation_OK --
8804 -------------------------
8806 function Stream_Operation_OK
8808 Operation : TSS_Name_Type) return Boolean
8810 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
8813 -- Special case of a limited type extension: a default implementation
8814 -- of the stream attributes Read or Write exists if that attribute
8815 -- has been specified or is available for an ancestor type; a default
8816 -- implementation of the attribute Output (resp. Input) exists if the
8817 -- attribute has been specified or Write (resp. Read) is available for
8818 -- an ancestor type. The last condition only applies under Ada 2005.
8820 if Is_Limited_Type (Typ)
8821 and then Is_Tagged_Type (Typ)
8823 if Operation = TSS_Stream_Read then
8824 Has_Predefined_Or_Specified_Stream_Attribute :=
8825 Has_Specified_Stream_Read (Typ);
8827 elsif Operation = TSS_Stream_Write then
8828 Has_Predefined_Or_Specified_Stream_Attribute :=
8829 Has_Specified_Stream_Write (Typ);
8831 elsif Operation = TSS_Stream_Input then
8832 Has_Predefined_Or_Specified_Stream_Attribute :=
8833 Has_Specified_Stream_Input (Typ)
8835 (Ada_Version >= Ada_05
8836 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
8838 elsif Operation = TSS_Stream_Output then
8839 Has_Predefined_Or_Specified_Stream_Attribute :=
8840 Has_Specified_Stream_Output (Typ)
8842 (Ada_Version >= Ada_05
8843 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
8846 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
8848 if not Has_Predefined_Or_Specified_Stream_Attribute
8849 and then Is_Derived_Type (Typ)
8850 and then (Operation = TSS_Stream_Read
8851 or else Operation = TSS_Stream_Write)
8853 Has_Predefined_Or_Specified_Stream_Attribute :=
8855 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
8859 -- If the type is not limited, or else is limited but the attribute is
8860 -- explicitly specified or is predefined for the type, then return True,
8861 -- unless other conditions prevail, such as restrictions prohibiting
8862 -- streams or dispatching operations. We also return True for limited
8863 -- interfaces, because they may be extended by nonlimited types and
8864 -- permit inheritance in this case (addresses cases where an abstract
8865 -- extension doesn't get 'Input declared, as per comments below, but
8866 -- 'Class'Input must still be allowed). Note that attempts to apply
8867 -- stream attributes to a limited interface or its class-wide type
8868 -- (or limited extensions thereof) will still get properly rejected
8869 -- by Check_Stream_Attribute.
8871 -- We exclude the Input operation from being a predefined subprogram in
8872 -- the case where the associated type is an abstract extension, because
8873 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
8874 -- we don't want an abstract version created because types derived from
8875 -- the abstract type may not even have Input available (for example if
8876 -- derived from a private view of the abstract type that doesn't have
8877 -- a visible Input), but a VM such as .NET or the Java VM can treat the
8878 -- operation as inherited anyway, and we don't want an abstract function
8879 -- to be (implicitly) inherited in that case because it can lead to a VM
8882 return (not Is_Limited_Type (Typ)
8883 or else Is_Interface (Typ)
8884 or else Has_Predefined_Or_Specified_Stream_Attribute)
8885 and then (Operation /= TSS_Stream_Input
8886 or else not Is_Abstract_Type (Typ)
8887 or else not Is_Derived_Type (Typ))
8888 and then not Has_Unknown_Discriminants (Typ)
8889 and then not (Is_Interface (Typ)
8890 and then (Is_Task_Interface (Typ)
8891 or else Is_Protected_Interface (Typ)
8892 or else Is_Synchronized_Interface (Typ)))
8893 and then not Restriction_Active (No_Streams)
8894 and then not Restriction_Active (No_Dispatch)
8895 and then not No_Run_Time_Mode
8896 and then RTE_Available (RE_Tag)
8897 and then RTE_Available (RE_Root_Stream_Type);
8898 end Stream_Operation_OK;