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
1246 -- Start of processing for Build_Equivalent_Record_Aggregate
1249 if not Is_Record_Type (T)
1250 or else Has_Discriminants (T)
1251 or else Is_Limited_Type (T)
1252 or else Has_Non_Standard_Rep (T)
1254 Initialization_Warning (T);
1258 Comp := First_Component (T);
1260 -- A null record needs no warning
1266 while Present (Comp) loop
1268 -- Array components are acceptable if initialized by a positional
1269 -- aggregate with static components.
1271 if Is_Array_Type (Etype (Comp)) then
1273 Comp_Type : constant Entity_Id := Component_Type (Etype (Comp));
1276 if Nkind (Parent (Comp)) /= N_Component_Declaration
1277 or else No (Expression (Parent (Comp)))
1278 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1280 Initialization_Warning (T);
1283 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1285 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1286 or else not Compile_Time_Known_Value
1287 (Type_High_Bound (Comp_Type)))
1289 Initialization_Warning (T);
1293 not Static_Array_Aggregate (Expression (Parent (Comp)))
1295 Initialization_Warning (T);
1300 elsif Is_Scalar_Type (Etype (Comp)) then
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)))
1305 Initialization_Warning (T);
1309 -- For now, other types are excluded
1312 Initialization_Warning (T);
1316 Next_Component (Comp);
1319 -- All components have static initialization. Build positional aggregate
1320 -- from the given expressions or defaults.
1322 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1323 Set_Parent (Agg, Parent (T));
1325 Comp := First_Component (T);
1326 while Present (Comp) loop
1328 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1329 Next_Component (Comp);
1332 Analyze_And_Resolve (Agg, T);
1334 end Build_Equivalent_Record_Aggregate;
1336 -------------------------------
1337 -- Build_Initialization_Call --
1338 -------------------------------
1340 -- References to a discriminant inside the record type declaration can
1341 -- appear either in the subtype_indication to constrain a record or an
1342 -- array, or as part of a larger expression given for the initial value
1343 -- of a component. In both of these cases N appears in the record
1344 -- initialization procedure and needs to be replaced by the formal
1345 -- parameter of the initialization procedure which corresponds to that
1348 -- In the example below, references to discriminants D1 and D2 in proc_1
1349 -- are replaced by references to formals with the same name
1352 -- A similar replacement is done for calls to any record initialization
1353 -- procedure for any components that are themselves of a record type.
1355 -- type R (D1, D2 : Integer) is record
1356 -- X : Integer := F * D1;
1357 -- Y : Integer := F * D2;
1360 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1364 -- Out_2.X := F * D1;
1365 -- Out_2.Y := F * D2;
1368 function Build_Initialization_Call
1372 In_Init_Proc : Boolean := False;
1373 Enclos_Type : Entity_Id := Empty;
1374 Discr_Map : Elist_Id := New_Elmt_List;
1375 With_Default_Init : Boolean := False;
1376 Constructor_Ref : Node_Id := Empty) return List_Id
1378 Res : constant List_Id := New_List;
1381 Controller_Typ : Entity_Id;
1385 First_Arg : Node_Id;
1386 Full_Init_Type : Entity_Id;
1387 Full_Type : Entity_Id := Typ;
1388 Init_Type : Entity_Id;
1392 pragma Assert (Constructor_Ref = Empty
1393 or else Is_CPP_Constructor_Call (Constructor_Ref));
1395 if No (Constructor_Ref) then
1396 Proc := Base_Init_Proc (Typ);
1398 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1401 Init_Type := Etype (First_Formal (Proc));
1402 Full_Init_Type := Underlying_Type (Init_Type);
1404 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1405 -- is active (in which case we make the call anyway, since in the
1406 -- actual compiled client it may be non null).
1407 -- Also nothing to do for value types.
1409 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1410 or else Is_Value_Type (Typ)
1411 or else Is_Value_Type (Component_Type (Typ))
1416 -- Go to full view if private type. In the case of successive
1417 -- private derivations, this can require more than one step.
1419 while Is_Private_Type (Full_Type)
1420 and then Present (Full_View (Full_Type))
1422 Full_Type := Full_View (Full_Type);
1425 -- If Typ is derived, the procedure is the initialization procedure for
1426 -- the root type. Wrap the argument in an conversion to make it type
1427 -- honest. Actually it isn't quite type honest, because there can be
1428 -- conflicts of views in the private type case. That is why we set
1429 -- Conversion_OK in the conversion node.
1431 if (Is_Record_Type (Typ)
1432 or else Is_Array_Type (Typ)
1433 or else Is_Private_Type (Typ))
1434 and then Init_Type /= Base_Type (Typ)
1436 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1437 Set_Etype (First_Arg, Init_Type);
1440 First_Arg := Id_Ref;
1443 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1445 -- In the tasks case, add _Master as the value of the _Master parameter
1446 -- and _Chain as the value of the _Chain parameter. At the outer level,
1447 -- these will be variables holding the corresponding values obtained
1448 -- from GNARL. At inner levels, they will be the parameters passed down
1449 -- through the outer routines.
1451 if Has_Task (Full_Type) then
1452 if Restriction_Active (No_Task_Hierarchy) then
1454 -- See comments in System.Tasking.Initialization.Init_RTS
1455 -- for the value 3 (should be rtsfindable constant ???)
1457 Append_To (Args, Make_Integer_Literal (Loc, 3));
1460 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1463 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1465 -- Ada 2005 (AI-287): In case of default initialized components
1466 -- with tasks, we generate a null string actual parameter.
1467 -- This is just a workaround that must be improved later???
1469 if With_Default_Init then
1471 Make_String_Literal (Loc,
1476 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1477 Decl := Last (Decls);
1480 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1481 Append_List (Decls, Res);
1489 -- Add discriminant values if discriminants are present
1491 if Has_Discriminants (Full_Init_Type) then
1492 Discr := First_Discriminant (Full_Init_Type);
1494 while Present (Discr) loop
1496 -- If this is a discriminated concurrent type, the init_proc
1497 -- for the corresponding record is being called. Use that type
1498 -- directly to find the discriminant value, to handle properly
1499 -- intervening renamed discriminants.
1502 T : Entity_Id := Full_Type;
1505 if Is_Protected_Type (T) then
1506 T := Corresponding_Record_Type (T);
1508 elsif Is_Private_Type (T)
1509 and then Present (Underlying_Full_View (T))
1510 and then Is_Protected_Type (Underlying_Full_View (T))
1512 T := Corresponding_Record_Type (Underlying_Full_View (T));
1516 Get_Discriminant_Value (
1519 Discriminant_Constraint (Full_Type));
1522 if In_Init_Proc then
1524 -- Replace any possible references to the discriminant in the
1525 -- call to the record initialization procedure with references
1526 -- to the appropriate formal parameter.
1528 if Nkind (Arg) = N_Identifier
1529 and then Ekind (Entity (Arg)) = E_Discriminant
1531 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1533 -- Case of access discriminants. We replace the reference
1534 -- to the type by a reference to the actual object
1536 elsif Nkind (Arg) = N_Attribute_Reference
1537 and then Is_Access_Type (Etype (Arg))
1538 and then Is_Entity_Name (Prefix (Arg))
1539 and then Is_Type (Entity (Prefix (Arg)))
1542 Make_Attribute_Reference (Loc,
1543 Prefix => New_Copy (Prefix (Id_Ref)),
1544 Attribute_Name => Name_Unrestricted_Access);
1546 -- Otherwise make a copy of the default expression. Note that
1547 -- we use the current Sloc for this, because we do not want the
1548 -- call to appear to be at the declaration point. Within the
1549 -- expression, replace discriminants with their discriminals.
1553 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1557 if Is_Constrained (Full_Type) then
1558 Arg := Duplicate_Subexpr_No_Checks (Arg);
1560 -- The constraints come from the discriminant default exps,
1561 -- they must be reevaluated, so we use New_Copy_Tree but we
1562 -- ensure the proper Sloc (for any embedded calls).
1564 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1568 -- Ada 2005 (AI-287): In case of default initialized components,
1569 -- if the component is constrained with a discriminant of the
1570 -- enclosing type, we need to generate the corresponding selected
1571 -- component node to access the discriminant value. In other cases
1572 -- this is not required, either because we are inside the init
1573 -- proc and we use the corresponding formal, or else because the
1574 -- component is constrained by an expression.
1576 if With_Default_Init
1577 and then Nkind (Id_Ref) = N_Selected_Component
1578 and then Nkind (Arg) = N_Identifier
1579 and then Ekind (Entity (Arg)) = E_Discriminant
1582 Make_Selected_Component (Loc,
1583 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1584 Selector_Name => Arg));
1586 Append_To (Args, Arg);
1589 Next_Discriminant (Discr);
1593 -- If this is a call to initialize the parent component of a derived
1594 -- tagged type, indicate that the tag should not be set in the parent.
1596 if Is_Tagged_Type (Full_Init_Type)
1597 and then not Is_CPP_Class (Full_Init_Type)
1598 and then Nkind (Id_Ref) = N_Selected_Component
1599 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1601 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1603 elsif Present (Constructor_Ref) then
1604 Append_List_To (Args,
1605 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1609 Make_Procedure_Call_Statement (Loc,
1610 Name => New_Occurrence_Of (Proc, Loc),
1611 Parameter_Associations => Args));
1613 if Needs_Finalization (Typ)
1614 and then Nkind (Id_Ref) = N_Selected_Component
1616 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1617 Append_List_To (Res,
1619 Ref => New_Copy_Tree (First_Arg),
1622 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1623 With_Attach => Make_Integer_Literal (Loc, 1)));
1625 -- If the enclosing type is an extension with new controlled
1626 -- components, it has his own record controller. If the parent
1627 -- also had a record controller, attach it to the new one.
1629 -- Build_Init_Statements relies on the fact that in this specific
1630 -- case the last statement of the result is the attach call to
1631 -- the controller. If this is changed, it must be synchronized.
1633 elsif Present (Enclos_Type)
1634 and then Has_New_Controlled_Component (Enclos_Type)
1635 and then Has_Controlled_Component (Typ)
1637 if Is_Inherently_Limited_Type (Typ) then
1638 Controller_Typ := RTE (RE_Limited_Record_Controller);
1640 Controller_Typ := RTE (RE_Record_Controller);
1643 Append_List_To (Res,
1646 Make_Selected_Component (Loc,
1647 Prefix => New_Copy_Tree (First_Arg),
1648 Selector_Name => Make_Identifier (Loc, Name_uController)),
1649 Typ => Controller_Typ,
1650 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1651 With_Attach => Make_Integer_Literal (Loc, 1)));
1658 when RE_Not_Available =>
1660 end Build_Initialization_Call;
1662 ---------------------------
1663 -- Build_Master_Renaming --
1664 ---------------------------
1666 function Build_Master_Renaming
1668 T : Entity_Id) return Entity_Id
1670 Loc : constant Source_Ptr := Sloc (N);
1675 -- Nothing to do if there is no task hierarchy
1677 if Restriction_Active (No_Task_Hierarchy) then
1682 Make_Defining_Identifier (Loc,
1683 New_External_Name (Chars (T), 'M'));
1686 Make_Object_Renaming_Declaration (Loc,
1687 Defining_Identifier => M_Id,
1688 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1689 Name => Make_Identifier (Loc, Name_uMaster));
1690 Insert_Before (N, Decl);
1695 when RE_Not_Available =>
1697 end Build_Master_Renaming;
1699 ---------------------------
1700 -- Build_Master_Renaming --
1701 ---------------------------
1703 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1707 -- Nothing to do if there is no task hierarchy
1709 if Restriction_Active (No_Task_Hierarchy) then
1713 M_Id := Build_Master_Renaming (N, T);
1714 Set_Master_Id (T, M_Id);
1717 when RE_Not_Available =>
1719 end Build_Master_Renaming;
1721 ----------------------------
1722 -- Build_Record_Init_Proc --
1723 ----------------------------
1725 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1726 Loc : Source_Ptr := Sloc (N);
1727 Discr_Map : constant Elist_Id := New_Elmt_List;
1728 Proc_Id : Entity_Id;
1729 Rec_Type : Entity_Id;
1730 Set_Tag : Entity_Id := Empty;
1732 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1733 -- Build a assignment statement node which assigns to record component
1734 -- its default expression if defined. The assignment left hand side is
1735 -- marked Assignment_OK so that initialization of limited private
1736 -- records works correctly, Return also the adjustment call for
1737 -- controlled objects
1739 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1740 -- If the record has discriminants, adds assignment statements to
1741 -- statement list to initialize the discriminant values from the
1742 -- arguments of the initialization procedure.
1744 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1745 -- Build a list representing a sequence of statements which initialize
1746 -- components of the given component list. This may involve building
1747 -- case statements for the variant parts.
1749 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1750 -- Given a non-tagged type-derivation that declares discriminants,
1753 -- type R (R1, R2 : Integer) is record ... end record;
1755 -- type D (D1 : Integer) is new R (1, D1);
1757 -- we make the _init_proc of D be
1759 -- procedure _init_proc(X : D; D1 : Integer) is
1761 -- _init_proc( R(X), 1, D1);
1764 -- This function builds the call statement in this _init_proc.
1766 procedure Build_Init_Procedure;
1767 -- Build the tree corresponding to the procedure specification and body
1768 -- of the initialization procedure (by calling all the preceding
1769 -- auxiliary routines), and install it as the _init TSS.
1771 procedure Build_Offset_To_Top_Functions;
1772 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1773 -- and body of the Offset_To_Top function that is generated when the
1774 -- parent of a type with discriminants has secondary dispatch tables.
1776 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1777 -- Add range checks to components of discriminated records. S is a
1778 -- subtype indication of a record component. Check_List is a list
1779 -- to which the check actions are appended.
1781 function Component_Needs_Simple_Initialization
1782 (T : Entity_Id) return Boolean;
1783 -- Determines if a component needs simple initialization, given its type
1784 -- T. This is the same as Needs_Simple_Initialization except for the
1785 -- following difference: the types Tag and Interface_Tag, that are
1786 -- access types which would normally require simple initialization to
1787 -- null, do not require initialization as components, since they are
1788 -- explicitly initialized by other means.
1790 procedure Constrain_Array
1792 Check_List : List_Id);
1793 -- Called from Build_Record_Checks.
1794 -- Apply a list of index constraints to an unconstrained array type.
1795 -- The first parameter is the entity for the resulting subtype.
1796 -- Check_List is a list to which the check actions are appended.
1798 procedure Constrain_Index
1801 Check_List : List_Id);
1802 -- Process an index constraint in a constrained array declaration.
1803 -- The constraint can be a subtype name, or a range with or without
1804 -- an explicit subtype mark. The index is the corresponding index of the
1805 -- unconstrained array. S is the range expression. Check_List is a list
1806 -- to which the check actions are appended (called from
1807 -- Build_Record_Checks).
1809 function Parent_Subtype_Renaming_Discrims return Boolean;
1810 -- Returns True for base types N that rename discriminants, else False
1812 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1813 -- Determines whether a record initialization procedure needs to be
1814 -- generated for the given record type.
1816 ----------------------
1817 -- Build_Assignment --
1818 ----------------------
1820 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1823 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1824 Kind : Node_Kind := Nkind (N);
1830 Make_Selected_Component (Loc,
1831 Prefix => Make_Identifier (Loc, Name_uInit),
1832 Selector_Name => New_Occurrence_Of (Id, Loc));
1833 Set_Assignment_OK (Lhs);
1835 -- Case of an access attribute applied to the current instance.
1836 -- Replace the reference to the type by a reference to the actual
1837 -- object. (Note that this handles the case of the top level of
1838 -- the expression being given by such an attribute, but does not
1839 -- cover uses nested within an initial value expression. Nested
1840 -- uses are unlikely to occur in practice, but are theoretically
1841 -- possible. It is not clear how to handle them without fully
1842 -- traversing the expression. ???
1844 if Kind = N_Attribute_Reference
1845 and then (Attribute_Name (N) = Name_Unchecked_Access
1847 Attribute_Name (N) = Name_Unrestricted_Access)
1848 and then Is_Entity_Name (Prefix (N))
1849 and then Is_Type (Entity (Prefix (N)))
1850 and then Entity (Prefix (N)) = Rec_Type
1853 Make_Attribute_Reference (Loc,
1854 Prefix => Make_Identifier (Loc, Name_uInit),
1855 Attribute_Name => Name_Unrestricted_Access);
1858 -- Take a copy of Exp to ensure that later copies of this component
1859 -- declaration in derived types see the original tree, not a node
1860 -- rewritten during expansion of the init_proc. If the copy contains
1861 -- itypes, the scope of the new itypes is the init_proc being built.
1863 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1866 Make_Assignment_Statement (Loc,
1868 Expression => Exp));
1870 Set_No_Ctrl_Actions (First (Res));
1872 -- Adjust the tag if tagged (because of possible view conversions).
1873 -- Suppress the tag adjustment when VM_Target because VM tags are
1874 -- represented implicitly in objects.
1876 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1878 Make_Assignment_Statement (Loc,
1880 Make_Selected_Component (Loc,
1881 Prefix => New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1883 New_Reference_To (First_Tag_Component (Typ), Loc)),
1886 Unchecked_Convert_To (RTE (RE_Tag),
1888 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1891 -- Adjust the component if controlled except if it is an aggregate
1892 -- that will be expanded inline.
1894 if Kind = N_Qualified_Expression then
1895 Kind := Nkind (Expression (N));
1898 if Needs_Finalization (Typ)
1899 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1900 and then not Is_Inherently_Limited_Type (Typ)
1903 Ref : constant Node_Id :=
1904 New_Copy_Tree (Lhs, New_Scope => Proc_Id);
1906 Append_List_To (Res,
1910 Flist_Ref => Find_Final_List (Etype (Id), Ref),
1911 With_Attach => Make_Integer_Literal (Loc, 1)));
1918 when RE_Not_Available =>
1920 end Build_Assignment;
1922 ------------------------------------
1923 -- Build_Discriminant_Assignments --
1924 ------------------------------------
1926 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1928 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1931 if Has_Discriminants (Rec_Type)
1932 and then not Is_Unchecked_Union (Rec_Type)
1934 D := First_Discriminant (Rec_Type);
1936 while Present (D) loop
1937 -- Don't generate the assignment for discriminants in derived
1938 -- tagged types if the discriminant is a renaming of some
1939 -- ancestor discriminant. This initialization will be done
1940 -- when initializing the _parent field of the derived record.
1942 if Is_Tagged and then
1943 Present (Corresponding_Discriminant (D))
1949 Append_List_To (Statement_List,
1950 Build_Assignment (D,
1951 New_Reference_To (Discriminal (D), Loc)));
1954 Next_Discriminant (D);
1957 end Build_Discriminant_Assignments;
1959 --------------------------
1960 -- Build_Init_Call_Thru --
1961 --------------------------
1963 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1964 Parent_Proc : constant Entity_Id :=
1965 Base_Init_Proc (Etype (Rec_Type));
1967 Parent_Type : constant Entity_Id :=
1968 Etype (First_Formal (Parent_Proc));
1970 Uparent_Type : constant Entity_Id :=
1971 Underlying_Type (Parent_Type);
1973 First_Discr_Param : Node_Id;
1975 Parent_Discr : Entity_Id;
1976 First_Arg : Node_Id;
1982 -- First argument (_Init) is the object to be initialized.
1983 -- ??? not sure where to get a reasonable Loc for First_Arg
1986 OK_Convert_To (Parent_Type,
1987 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1989 Set_Etype (First_Arg, Parent_Type);
1991 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1993 -- In the tasks case,
1994 -- add _Master as the value of the _Master parameter
1995 -- add _Chain as the value of the _Chain parameter.
1996 -- add _Task_Name as the value of the _Task_Name parameter.
1997 -- At the outer level, these will be variables holding the
1998 -- corresponding values obtained from GNARL or the expander.
2000 -- At inner levels, they will be the parameters passed down through
2001 -- the outer routines.
2003 First_Discr_Param := Next (First (Parameters));
2005 if Has_Task (Rec_Type) then
2006 if Restriction_Active (No_Task_Hierarchy) then
2008 -- See comments in System.Tasking.Initialization.Init_RTS
2011 Append_To (Args, Make_Integer_Literal (Loc, 3));
2013 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2016 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2017 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2018 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2021 -- Append discriminant values
2023 if Has_Discriminants (Uparent_Type) then
2024 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2026 Parent_Discr := First_Discriminant (Uparent_Type);
2027 while Present (Parent_Discr) loop
2029 -- Get the initial value for this discriminant
2030 -- ??? needs to be cleaned up to use parent_Discr_Constr
2034 Discr_Value : Elmt_Id :=
2036 (Stored_Constraint (Rec_Type));
2038 Discr : Entity_Id :=
2039 First_Stored_Discriminant (Uparent_Type);
2041 while Original_Record_Component (Parent_Discr) /= Discr loop
2042 Next_Stored_Discriminant (Discr);
2043 Next_Elmt (Discr_Value);
2046 Arg := Node (Discr_Value);
2049 -- Append it to the list
2051 if Nkind (Arg) = N_Identifier
2052 and then Ekind (Entity (Arg)) = E_Discriminant
2055 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2057 -- Case of access discriminants. We replace the reference
2058 -- to the type by a reference to the actual object.
2060 -- Is above comment right??? Use of New_Copy below seems mighty
2064 Append_To (Args, New_Copy (Arg));
2067 Next_Discriminant (Parent_Discr);
2073 Make_Procedure_Call_Statement (Loc,
2074 Name => New_Occurrence_Of (Parent_Proc, Loc),
2075 Parameter_Associations => Args));
2078 end Build_Init_Call_Thru;
2080 -----------------------------------
2081 -- Build_Offset_To_Top_Functions --
2082 -----------------------------------
2084 procedure Build_Offset_To_Top_Functions is
2086 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2088 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2090 -- return O.Iface_Comp'Position;
2093 ----------------------------------
2094 -- Build_Offset_To_Top_Function --
2095 ----------------------------------
2097 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2098 Body_Node : Node_Id;
2099 Func_Id : Entity_Id;
2100 Spec_Node : Node_Id;
2104 Make_Defining_Identifier (Loc,
2105 Chars => New_Internal_Name ('F'));
2107 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2110 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2112 Spec_Node := New_Node (N_Function_Specification, Loc);
2113 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2114 Set_Parameter_Specifications (Spec_Node, New_List (
2115 Make_Parameter_Specification (Loc,
2116 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2118 Parameter_Type => New_Reference_To (Rec_Type, Loc))));
2119 Set_Result_Definition (Spec_Node,
2120 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2123 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2125 -- return O.Iface_Comp'Position;
2128 Body_Node := New_Node (N_Subprogram_Body, Loc);
2129 Set_Specification (Body_Node, Spec_Node);
2130 Set_Declarations (Body_Node, New_List);
2131 Set_Handled_Statement_Sequence (Body_Node,
2132 Make_Handled_Sequence_Of_Statements (Loc,
2133 Statements => New_List (
2134 Make_Simple_Return_Statement (Loc,
2136 Make_Attribute_Reference (Loc,
2138 Make_Selected_Component (Loc,
2139 Prefix => Make_Identifier (Loc, Name_uO),
2140 Selector_Name => New_Reference_To
2142 Attribute_Name => Name_Position)))));
2144 Set_Ekind (Func_Id, E_Function);
2145 Set_Mechanism (Func_Id, Default_Mechanism);
2146 Set_Is_Internal (Func_Id, True);
2148 if not Debug_Generated_Code then
2149 Set_Debug_Info_Off (Func_Id);
2152 Analyze (Body_Node);
2154 Append_Freeze_Action (Rec_Type, Body_Node);
2155 end Build_Offset_To_Top_Function;
2159 Ifaces_Comp_List : Elist_Id;
2160 Iface_Comp_Elmt : Elmt_Id;
2161 Iface_Comp : Node_Id;
2163 -- Start of processing for Build_Offset_To_Top_Functions
2166 -- Offset_To_Top_Functions are built only for derivations of types
2167 -- with discriminants that cover interface types.
2168 -- Nothing is needed either in case of virtual machines, since
2169 -- interfaces are handled directly by the VM.
2171 if not Is_Tagged_Type (Rec_Type)
2172 or else Etype (Rec_Type) = Rec_Type
2173 or else not Has_Discriminants (Etype (Rec_Type))
2174 or else not Tagged_Type_Expansion
2179 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2181 -- For each interface type with secondary dispatch table we generate
2182 -- the Offset_To_Top_Functions (required to displace the pointer in
2183 -- interface conversions)
2185 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2186 while Present (Iface_Comp_Elmt) loop
2187 Iface_Comp := Node (Iface_Comp_Elmt);
2188 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2190 -- If the interface is a parent of Rec_Type it shares the primary
2191 -- dispatch table and hence there is no need to build the function
2193 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type) then
2194 Build_Offset_To_Top_Function (Iface_Comp);
2197 Next_Elmt (Iface_Comp_Elmt);
2199 end Build_Offset_To_Top_Functions;
2201 --------------------------
2202 -- Build_Init_Procedure --
2203 --------------------------
2205 procedure Build_Init_Procedure is
2206 Body_Node : Node_Id;
2207 Handled_Stmt_Node : Node_Id;
2208 Parameters : List_Id;
2209 Proc_Spec_Node : Node_Id;
2210 Body_Stmts : List_Id;
2211 Record_Extension_Node : Node_Id;
2212 Init_Tags_List : List_Id;
2215 Body_Stmts := New_List;
2216 Body_Node := New_Node (N_Subprogram_Body, Loc);
2217 Set_Ekind (Proc_Id, E_Procedure);
2219 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2220 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2222 Parameters := Init_Formals (Rec_Type);
2223 Append_List_To (Parameters,
2224 Build_Discriminant_Formals (Rec_Type, True));
2226 -- For tagged types, we add a flag to indicate whether the routine
2227 -- is called to initialize a parent component in the init_proc of
2228 -- a type extension. If the flag is false, we do not set the tag
2229 -- because it has been set already in the extension.
2231 if Is_Tagged_Type (Rec_Type)
2232 and then not Is_CPP_Class (Rec_Type)
2235 Make_Defining_Identifier (Loc,
2236 Chars => New_Internal_Name ('P'));
2238 Append_To (Parameters,
2239 Make_Parameter_Specification (Loc,
2240 Defining_Identifier => Set_Tag,
2241 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2242 Expression => New_Occurrence_Of (Standard_True, Loc)));
2245 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2246 Set_Specification (Body_Node, Proc_Spec_Node);
2247 Set_Declarations (Body_Node, New_List);
2249 if Parent_Subtype_Renaming_Discrims then
2251 -- N is a Derived_Type_Definition that renames the parameters
2252 -- of the ancestor type. We initialize it by expanding our
2253 -- discriminants and call the ancestor _init_proc with a
2254 -- type-converted object
2256 Append_List_To (Body_Stmts,
2257 Build_Init_Call_Thru (Parameters));
2259 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2260 Build_Discriminant_Assignments (Body_Stmts);
2262 if not Null_Present (Type_Definition (N)) then
2263 Append_List_To (Body_Stmts,
2264 Build_Init_Statements (
2265 Component_List (Type_Definition (N))));
2269 -- N is a Derived_Type_Definition with a possible non-empty
2270 -- extension. The initialization of a type extension consists
2271 -- in the initialization of the components in the extension.
2273 Build_Discriminant_Assignments (Body_Stmts);
2275 Record_Extension_Node :=
2276 Record_Extension_Part (Type_Definition (N));
2278 if not Null_Present (Record_Extension_Node) then
2280 Stmts : constant List_Id :=
2281 Build_Init_Statements (
2282 Component_List (Record_Extension_Node));
2285 -- The parent field must be initialized first because
2286 -- the offset of the new discriminants may depend on it
2288 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2289 Append_List_To (Body_Stmts, Stmts);
2294 -- Add here the assignment to instantiate the Tag
2296 -- The assignment corresponds to the code:
2298 -- _Init._Tag := Typ'Tag;
2300 -- Suppress the tag assignment when VM_Target because VM tags are
2301 -- represented implicitly in objects. It is also suppressed in case
2302 -- of CPP_Class types because in this case the tag is initialized in
2305 if Is_Tagged_Type (Rec_Type)
2306 and then not Is_CPP_Class (Rec_Type)
2307 and then Tagged_Type_Expansion
2308 and then not No_Run_Time_Mode
2310 -- Initialize the primary tag
2312 Init_Tags_List := New_List (
2313 Make_Assignment_Statement (Loc,
2315 Make_Selected_Component (Loc,
2316 Prefix => Make_Identifier (Loc, Name_uInit),
2318 New_Reference_To (First_Tag_Component (Rec_Type), Loc)),
2322 (Node (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2324 -- Ada 2005 (AI-251): Initialize the secondary tags components
2325 -- located at fixed positions (tags whose position depends on
2326 -- variable size components are initialized later ---see below).
2328 if Ada_Version >= Ada_05
2329 and then not Is_Interface (Rec_Type)
2330 and then Has_Interfaces (Rec_Type)
2334 Target => Make_Identifier (Loc, Name_uInit),
2335 Stmts_List => Init_Tags_List,
2336 Fixed_Comps => True,
2337 Variable_Comps => False);
2340 -- The tag must be inserted before the assignments to other
2341 -- components, because the initial value of the component may
2342 -- depend on the tag (eg. through a dispatching operation on
2343 -- an access to the current type). The tag assignment is not done
2344 -- when initializing the parent component of a type extension,
2345 -- because in that case the tag is set in the extension.
2347 -- Extensions of imported C++ classes add a final complication,
2348 -- because we cannot inhibit tag setting in the constructor for
2349 -- the parent. In that case we insert the tag initialization
2350 -- after the calls to initialize the parent.
2352 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2353 Prepend_To (Body_Stmts,
2354 Make_If_Statement (Loc,
2355 Condition => New_Occurrence_Of (Set_Tag, Loc),
2356 Then_Statements => Init_Tags_List));
2358 -- CPP_Class derivation: In this case the dispatch table of the
2359 -- parent was built in the C++ side and we copy the table of the
2360 -- parent to initialize the new dispatch table.
2367 -- We assume the first init_proc call is for the parent
2369 Nod := First (Body_Stmts);
2370 while Present (Next (Nod))
2371 and then (Nkind (Nod) /= N_Procedure_Call_Statement
2372 or else not Is_Init_Proc (Name (Nod)))
2378 -- ancestor_constructor (_init.parent);
2380 -- inherit_prim_ops (_init._tag, new_dt, num_prims);
2381 -- _init._tag := new_dt;
2384 Prepend_To (Init_Tags_List,
2385 Build_Inherit_Prims (Loc,
2388 Make_Selected_Component (Loc,
2390 Make_Identifier (Loc,
2391 Chars => Name_uInit),
2394 (First_Tag_Component (Rec_Type), Loc)),
2397 (Node (First_Elmt (Access_Disp_Table (Rec_Type))),
2401 (DT_Entry_Count (First_Tag_Component (Rec_Type)))));
2404 Make_If_Statement (Loc,
2405 Condition => New_Occurrence_Of (Set_Tag, Loc),
2406 Then_Statements => Init_Tags_List));
2408 -- We have inherited table of the parent from the CPP side.
2409 -- Now we fill the slots associated with Ada primitives.
2410 -- This needs more work to avoid its execution each time
2411 -- an object is initialized???
2418 E := First_Elmt (Primitive_Operations (Rec_Type));
2419 while Present (E) loop
2422 if not Is_Imported (Prim)
2423 and then Convention (Prim) = Convention_CPP
2424 and then not Present (Interface_Alias (Prim))
2426 Append_List_To (Init_Tags_List,
2427 Register_Primitive (Loc, Prim => Prim));
2436 -- Ada 2005 (AI-251): Initialize the secondary tag components
2437 -- located at variable positions. We delay the generation of this
2438 -- code until here because the value of the attribute 'Position
2439 -- applied to variable size components of the parent type that
2440 -- depend on discriminants is only safely read at runtime after
2441 -- the parent components have been initialized.
2443 if Ada_Version >= Ada_05
2444 and then not Is_Interface (Rec_Type)
2445 and then Has_Interfaces (Rec_Type)
2446 and then Has_Discriminants (Etype (Rec_Type))
2447 and then Is_Variable_Size_Record (Etype (Rec_Type))
2449 Init_Tags_List := New_List;
2453 Target => Make_Identifier (Loc, Name_uInit),
2454 Stmts_List => Init_Tags_List,
2455 Fixed_Comps => False,
2456 Variable_Comps => True);
2458 if Is_Non_Empty_List (Init_Tags_List) then
2459 Append_List_To (Body_Stmts, Init_Tags_List);
2464 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2465 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2466 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2467 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2469 if not Debug_Generated_Code then
2470 Set_Debug_Info_Off (Proc_Id);
2473 -- Associate Init_Proc with type, and determine if the procedure
2474 -- is null (happens because of the Initialize_Scalars pragma case,
2475 -- where we have to generate a null procedure in case it is called
2476 -- by a client with Initialize_Scalars set). Such procedures have
2477 -- to be generated, but do not have to be called, so we mark them
2478 -- as null to suppress the call.
2480 Set_Init_Proc (Rec_Type, Proc_Id);
2482 if List_Length (Body_Stmts) = 1
2483 and then Nkind (First (Body_Stmts)) = N_Null_Statement
2484 and then VM_Target /= CLI_Target
2486 -- Even though the init proc may be null at this time it might get
2487 -- some stuff added to it later by the CIL backend, so always keep
2488 -- it when VM_Target = CLI_Target.
2490 Set_Is_Null_Init_Proc (Proc_Id);
2492 end Build_Init_Procedure;
2494 ---------------------------
2495 -- Build_Init_Statements --
2496 ---------------------------
2498 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2499 Check_List : constant List_Id := New_List;
2504 Statement_List : List_Id;
2509 Per_Object_Constraint_Components : Boolean;
2511 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2512 -- Components with access discriminants that depend on the current
2513 -- instance must be initialized after all other components.
2515 ---------------------------
2516 -- Has_Access_Constraint --
2517 ---------------------------
2519 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2521 T : constant Entity_Id := Etype (E);
2524 if Has_Per_Object_Constraint (E)
2525 and then Has_Discriminants (T)
2527 Disc := First_Discriminant (T);
2528 while Present (Disc) loop
2529 if Is_Access_Type (Etype (Disc)) then
2533 Next_Discriminant (Disc);
2540 end Has_Access_Constraint;
2542 -- Start of processing for Build_Init_Statements
2545 if Null_Present (Comp_List) then
2546 return New_List (Make_Null_Statement (Loc));
2549 Statement_List := New_List;
2551 -- Loop through visible declarations of task types and protected
2552 -- types moving any expanded code from the spec to the body of the
2555 if Is_Task_Record_Type (Rec_Type)
2556 or else Is_Protected_Record_Type (Rec_Type)
2559 Decl : constant Node_Id :=
2560 Parent (Corresponding_Concurrent_Type (Rec_Type));
2566 if Is_Task_Record_Type (Rec_Type) then
2567 Def := Task_Definition (Decl);
2569 Def := Protected_Definition (Decl);
2572 if Present (Def) then
2573 N1 := First (Visible_Declarations (Def));
2574 while Present (N1) loop
2578 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2579 or else Nkind (N2) in N_Raise_xxx_Error
2580 or else Nkind (N2) = N_Procedure_Call_Statement
2582 Append_To (Statement_List,
2583 New_Copy_Tree (N2, New_Scope => Proc_Id));
2584 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2592 -- Loop through components, skipping pragmas, in 2 steps. The first
2593 -- step deals with regular components. The second step deals with
2594 -- components have per object constraints, and no explicit initia-
2597 Per_Object_Constraint_Components := False;
2599 -- First step : regular components
2601 Decl := First_Non_Pragma (Component_Items (Comp_List));
2602 while Present (Decl) loop
2605 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2607 Id := Defining_Identifier (Decl);
2610 if Has_Access_Constraint (Id)
2611 and then No (Expression (Decl))
2613 -- Skip processing for now and ask for a second pass
2615 Per_Object_Constraint_Components := True;
2618 -- Case of explicit initialization
2620 if Present (Expression (Decl)) then
2621 if Is_CPP_Constructor_Call (Expression (Decl)) then
2623 Build_Initialization_Call
2625 Make_Selected_Component (Loc,
2626 Prefix => Make_Identifier (Loc, Name_uInit),
2627 Selector_Name => New_Occurrence_Of (Id, Loc)),
2629 In_Init_Proc => True,
2630 Enclos_Type => Rec_Type,
2631 Discr_Map => Discr_Map,
2632 Constructor_Ref => Expression (Decl));
2634 Stmts := Build_Assignment (Id, Expression (Decl));
2637 -- Case of composite component with its own Init_Proc
2639 elsif not Is_Interface (Typ)
2640 and then Has_Non_Null_Base_Init_Proc (Typ)
2643 Build_Initialization_Call
2645 Make_Selected_Component (Loc,
2646 Prefix => Make_Identifier (Loc, Name_uInit),
2647 Selector_Name => New_Occurrence_Of (Id, Loc)),
2649 In_Init_Proc => True,
2650 Enclos_Type => Rec_Type,
2651 Discr_Map => Discr_Map);
2653 Clean_Task_Names (Typ, Proc_Id);
2655 -- Case of component needing simple initialization
2657 elsif Component_Needs_Simple_Initialization (Typ) then
2660 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2662 -- Nothing needed for this case
2668 if Present (Check_List) then
2669 Append_List_To (Statement_List, Check_List);
2672 if Present (Stmts) then
2674 -- Add the initialization of the record controller before
2675 -- the _Parent field is attached to it when the attachment
2676 -- can occur. It does not work to simply initialize the
2677 -- controller first: it must be initialized after the parent
2678 -- if the parent holds discriminants that can be used to
2679 -- compute the offset of the controller. We assume here that
2680 -- the last statement of the initialization call is the
2681 -- attachment of the parent (see Build_Initialization_Call)
2683 if Chars (Id) = Name_uController
2684 and then Rec_Type /= Etype (Rec_Type)
2685 and then Has_Controlled_Component (Etype (Rec_Type))
2686 and then Has_New_Controlled_Component (Rec_Type)
2687 and then Present (Last (Statement_List))
2689 Insert_List_Before (Last (Statement_List), Stmts);
2691 Append_List_To (Statement_List, Stmts);
2696 Next_Non_Pragma (Decl);
2699 if Per_Object_Constraint_Components then
2701 -- Second pass: components with per-object constraints
2703 Decl := First_Non_Pragma (Component_Items (Comp_List));
2704 while Present (Decl) loop
2706 Id := Defining_Identifier (Decl);
2709 if Has_Access_Constraint (Id)
2710 and then No (Expression (Decl))
2712 if Has_Non_Null_Base_Init_Proc (Typ) then
2713 Append_List_To (Statement_List,
2714 Build_Initialization_Call (Loc,
2715 Make_Selected_Component (Loc,
2716 Prefix => Make_Identifier (Loc, Name_uInit),
2717 Selector_Name => New_Occurrence_Of (Id, Loc)),
2719 In_Init_Proc => True,
2720 Enclos_Type => Rec_Type,
2721 Discr_Map => Discr_Map));
2723 Clean_Task_Names (Typ, Proc_Id);
2725 elsif Component_Needs_Simple_Initialization (Typ) then
2726 Append_List_To (Statement_List,
2728 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
2732 Next_Non_Pragma (Decl);
2736 -- Process the variant part
2738 if Present (Variant_Part (Comp_List)) then
2739 Alt_List := New_List;
2740 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2741 while Present (Variant) loop
2742 Loc := Sloc (Variant);
2743 Append_To (Alt_List,
2744 Make_Case_Statement_Alternative (Loc,
2746 New_Copy_List (Discrete_Choices (Variant)),
2748 Build_Init_Statements (Component_List (Variant))));
2749 Next_Non_Pragma (Variant);
2752 -- The expression of the case statement which is a reference
2753 -- to one of the discriminants is replaced by the appropriate
2754 -- formal parameter of the initialization procedure.
2756 Append_To (Statement_List,
2757 Make_Case_Statement (Loc,
2759 New_Reference_To (Discriminal (
2760 Entity (Name (Variant_Part (Comp_List)))), Loc),
2761 Alternatives => Alt_List));
2764 -- For a task record type, add the task create call and calls
2765 -- to bind any interrupt (signal) entries.
2767 if Is_Task_Record_Type (Rec_Type) then
2769 -- In the case of the restricted run time the ATCB has already
2770 -- been preallocated.
2772 if Restricted_Profile then
2773 Append_To (Statement_List,
2774 Make_Assignment_Statement (Loc,
2775 Name => Make_Selected_Component (Loc,
2776 Prefix => Make_Identifier (Loc, Name_uInit),
2777 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2778 Expression => Make_Attribute_Reference (Loc,
2780 Make_Selected_Component (Loc,
2781 Prefix => Make_Identifier (Loc, Name_uInit),
2783 Make_Identifier (Loc, Name_uATCB)),
2784 Attribute_Name => Name_Unchecked_Access)));
2787 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2789 -- Generate the statements which map a string entry name to a
2790 -- task entry index. Note that the task may not have entries.
2792 if Entry_Names_OK then
2793 Names := Build_Entry_Names (Rec_Type);
2795 if Present (Names) then
2796 Append_To (Statement_List, Names);
2801 Task_Type : constant Entity_Id :=
2802 Corresponding_Concurrent_Type (Rec_Type);
2803 Task_Decl : constant Node_Id := Parent (Task_Type);
2804 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2809 if Present (Task_Def) then
2810 Vis_Decl := First (Visible_Declarations (Task_Def));
2811 while Present (Vis_Decl) loop
2812 Loc := Sloc (Vis_Decl);
2814 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2815 if Get_Attribute_Id (Chars (Vis_Decl)) =
2818 Ent := Entity (Name (Vis_Decl));
2820 if Ekind (Ent) = E_Entry then
2821 Append_To (Statement_List,
2822 Make_Procedure_Call_Statement (Loc,
2823 Name => New_Reference_To (
2824 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2825 Parameter_Associations => New_List (
2826 Make_Selected_Component (Loc,
2828 Make_Identifier (Loc, Name_uInit),
2830 Make_Identifier (Loc, Name_uTask_Id)),
2831 Entry_Index_Expression (
2832 Loc, Ent, Empty, Task_Type),
2833 Expression (Vis_Decl))));
2844 -- For a protected type, add statements generated by
2845 -- Make_Initialize_Protection.
2847 if Is_Protected_Record_Type (Rec_Type) then
2848 Append_List_To (Statement_List,
2849 Make_Initialize_Protection (Rec_Type));
2851 -- Generate the statements which map a string entry name to a
2852 -- protected entry index. Note that the protected type may not
2855 if Entry_Names_OK then
2856 Names := Build_Entry_Names (Rec_Type);
2858 if Present (Names) then
2859 Append_To (Statement_List, Names);
2864 -- If no initializations when generated for component declarations
2865 -- corresponding to this Statement_List, append a null statement
2866 -- to the Statement_List to make it a valid Ada tree.
2868 if Is_Empty_List (Statement_List) then
2869 Append (New_Node (N_Null_Statement, Loc), Statement_List);
2872 return Statement_List;
2875 when RE_Not_Available =>
2877 end Build_Init_Statements;
2879 -------------------------
2880 -- Build_Record_Checks --
2881 -------------------------
2883 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2884 Subtype_Mark_Id : Entity_Id;
2887 if Nkind (S) = N_Subtype_Indication then
2888 Find_Type (Subtype_Mark (S));
2889 Subtype_Mark_Id := Entity (Subtype_Mark (S));
2891 -- Remaining processing depends on type
2893 case Ekind (Subtype_Mark_Id) is
2896 Constrain_Array (S, Check_List);
2902 end Build_Record_Checks;
2904 -------------------------------------------
2905 -- Component_Needs_Simple_Initialization --
2906 -------------------------------------------
2908 function Component_Needs_Simple_Initialization
2909 (T : Entity_Id) return Boolean
2913 Needs_Simple_Initialization (T)
2914 and then not Is_RTE (T, RE_Tag)
2916 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
2918 and then not Is_RTE (T, RE_Interface_Tag);
2919 end Component_Needs_Simple_Initialization;
2921 ---------------------
2922 -- Constrain_Array --
2923 ---------------------
2925 procedure Constrain_Array
2927 Check_List : List_Id)
2929 C : constant Node_Id := Constraint (SI);
2930 Number_Of_Constraints : Nat := 0;
2935 T := Entity (Subtype_Mark (SI));
2937 if Ekind (T) in Access_Kind then
2938 T := Designated_Type (T);
2941 S := First (Constraints (C));
2943 while Present (S) loop
2944 Number_Of_Constraints := Number_Of_Constraints + 1;
2948 -- In either case, the index constraint must provide a discrete
2949 -- range for each index of the array type and the type of each
2950 -- discrete range must be the same as that of the corresponding
2951 -- index. (RM 3.6.1)
2953 S := First (Constraints (C));
2954 Index := First_Index (T);
2957 -- Apply constraints to each index type
2959 for J in 1 .. Number_Of_Constraints loop
2960 Constrain_Index (Index, S, Check_List);
2965 end Constrain_Array;
2967 ---------------------
2968 -- Constrain_Index --
2969 ---------------------
2971 procedure Constrain_Index
2974 Check_List : List_Id)
2976 T : constant Entity_Id := Etype (Index);
2979 if Nkind (S) = N_Range then
2980 Process_Range_Expr_In_Decl (S, T, Check_List);
2982 end Constrain_Index;
2984 --------------------------------------
2985 -- Parent_Subtype_Renaming_Discrims --
2986 --------------------------------------
2988 function Parent_Subtype_Renaming_Discrims return Boolean is
2993 if Base_Type (Pe) /= Pe then
2998 or else not Has_Discriminants (Pe)
2999 or else Is_Constrained (Pe)
3000 or else Is_Tagged_Type (Pe)
3005 -- If there are no explicit stored discriminants we have inherited
3006 -- the root type discriminants so far, so no renamings occurred.
3008 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
3012 -- Check if we have done some trivial renaming of the parent
3013 -- discriminants, i.e. something like
3015 -- type DT (X1,X2: int) is new PT (X1,X2);
3017 De := First_Discriminant (Pe);
3018 Dp := First_Discriminant (Etype (Pe));
3020 while Present (De) loop
3021 pragma Assert (Present (Dp));
3023 if Corresponding_Discriminant (De) /= Dp then
3027 Next_Discriminant (De);
3028 Next_Discriminant (Dp);
3031 return Present (Dp);
3032 end Parent_Subtype_Renaming_Discrims;
3034 ------------------------
3035 -- Requires_Init_Proc --
3036 ------------------------
3038 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3039 Comp_Decl : Node_Id;
3044 -- Definitely do not need one if specifically suppressed
3046 if Suppress_Init_Proc (Rec_Id) then
3050 -- If it is a type derived from a type with unknown discriminants,
3051 -- we cannot build an initialization procedure for it.
3053 if Has_Unknown_Discriminants (Rec_Id)
3054 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3059 -- Otherwise we need to generate an initialization procedure if
3060 -- Is_CPP_Class is False and at least one of the following applies:
3062 -- 1. Discriminants are present, since they need to be initialized
3063 -- with the appropriate discriminant constraint expressions.
3064 -- However, the discriminant of an unchecked union does not
3065 -- count, since the discriminant is not present.
3067 -- 2. The type is a tagged type, since the implicit Tag component
3068 -- needs to be initialized with a pointer to the dispatch table.
3070 -- 3. The type contains tasks
3072 -- 4. One or more components has an initial value
3074 -- 5. One or more components is for a type which itself requires
3075 -- an initialization procedure.
3077 -- 6. One or more components is a type that requires simple
3078 -- initialization (see Needs_Simple_Initialization), except
3079 -- that types Tag and Interface_Tag are excluded, since fields
3080 -- of these types are initialized by other means.
3082 -- 7. The type is the record type built for a task type (since at
3083 -- the very least, Create_Task must be called)
3085 -- 8. The type is the record type built for a protected type (since
3086 -- at least Initialize_Protection must be called)
3088 -- 9. The type is marked as a public entity. The reason we add this
3089 -- case (even if none of the above apply) is to properly handle
3090 -- Initialize_Scalars. If a package is compiled without an IS
3091 -- pragma, and the client is compiled with an IS pragma, then
3092 -- the client will think an initialization procedure is present
3093 -- and call it, when in fact no such procedure is required, but
3094 -- since the call is generated, there had better be a routine
3095 -- at the other end of the call, even if it does nothing!)
3097 -- Note: the reason we exclude the CPP_Class case is because in this
3098 -- case the initialization is performed in the C++ side.
3100 if Is_CPP_Class (Rec_Id) then
3103 elsif Is_Interface (Rec_Id) then
3106 elsif (Has_Discriminants (Rec_Id)
3107 and then not Is_Unchecked_Union (Rec_Id))
3108 or else Is_Tagged_Type (Rec_Id)
3109 or else Is_Concurrent_Record_Type (Rec_Id)
3110 or else Has_Task (Rec_Id)
3115 Id := First_Component (Rec_Id);
3116 while Present (Id) loop
3117 Comp_Decl := Parent (Id);
3120 if Present (Expression (Comp_Decl))
3121 or else Has_Non_Null_Base_Init_Proc (Typ)
3122 or else Component_Needs_Simple_Initialization (Typ)
3127 Next_Component (Id);
3130 -- As explained above, a record initialization procedure is needed
3131 -- for public types in case Initialize_Scalars applies to a client.
3132 -- However, such a procedure is not needed in the case where either
3133 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3134 -- applies. No_Initialize_Scalars excludes the possibility of using
3135 -- Initialize_Scalars in any partition, and No_Default_Initialization
3136 -- implies that no initialization should ever be done for objects of
3137 -- the type, so is incompatible with Initialize_Scalars.
3139 if not Restriction_Active (No_Initialize_Scalars)
3140 and then not Restriction_Active (No_Default_Initialization)
3141 and then Is_Public (Rec_Id)
3147 end Requires_Init_Proc;
3149 -- Start of processing for Build_Record_Init_Proc
3152 -- Check for value type, which means no initialization required
3154 Rec_Type := Defining_Identifier (N);
3156 if Is_Value_Type (Rec_Type) then
3160 -- This may be full declaration of a private type, in which case
3161 -- the visible entity is a record, and the private entity has been
3162 -- exchanged with it in the private part of the current package.
3163 -- The initialization procedure is built for the record type, which
3164 -- is retrievable from the private entity.
3166 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3167 Rec_Type := Underlying_Type (Rec_Type);
3170 -- If there are discriminants, build the discriminant map to replace
3171 -- discriminants by their discriminals in complex bound expressions.
3172 -- These only arise for the corresponding records of synchronized types.
3174 if Is_Concurrent_Record_Type (Rec_Type)
3175 and then Has_Discriminants (Rec_Type)
3180 Disc := First_Discriminant (Rec_Type);
3181 while Present (Disc) loop
3182 Append_Elmt (Disc, Discr_Map);
3183 Append_Elmt (Discriminal (Disc), Discr_Map);
3184 Next_Discriminant (Disc);
3189 -- Derived types that have no type extension can use the initialization
3190 -- procedure of their parent and do not need a procedure of their own.
3191 -- This is only correct if there are no representation clauses for the
3192 -- type or its parent, and if the parent has in fact been frozen so
3193 -- that its initialization procedure exists.
3195 if Is_Derived_Type (Rec_Type)
3196 and then not Is_Tagged_Type (Rec_Type)
3197 and then not Is_Unchecked_Union (Rec_Type)
3198 and then not Has_New_Non_Standard_Rep (Rec_Type)
3199 and then not Parent_Subtype_Renaming_Discrims
3200 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3202 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3204 -- Otherwise if we need an initialization procedure, then build one,
3205 -- mark it as public and inlinable and as having a completion.
3207 elsif Requires_Init_Proc (Rec_Type)
3208 or else Is_Unchecked_Union (Rec_Type)
3211 Make_Defining_Identifier (Loc,
3212 Chars => Make_Init_Proc_Name (Rec_Type));
3214 -- If No_Default_Initialization restriction is active, then we don't
3215 -- want to build an init_proc, but we need to mark that an init_proc
3216 -- would be needed if this restriction was not active (so that we can
3217 -- detect attempts to call it), so set a dummy init_proc in place.
3219 if Restriction_Active (No_Default_Initialization) then
3220 Set_Init_Proc (Rec_Type, Proc_Id);
3224 Build_Offset_To_Top_Functions;
3225 Build_Init_Procedure;
3226 Set_Is_Public (Proc_Id, Is_Public (Pe));
3228 -- The initialization of protected records is not worth inlining.
3229 -- In addition, when compiled for another unit for inlining purposes,
3230 -- it may make reference to entities that have not been elaborated
3231 -- yet. The initialization of controlled records contains a nested
3232 -- clean-up procedure that makes it impractical to inline as well,
3233 -- and leads to undefined symbols if inlined in a different unit.
3234 -- Similar considerations apply to task types.
3236 if not Is_Concurrent_Type (Rec_Type)
3237 and then not Has_Task (Rec_Type)
3238 and then not Needs_Finalization (Rec_Type)
3240 Set_Is_Inlined (Proc_Id);
3243 Set_Is_Internal (Proc_Id);
3244 Set_Has_Completion (Proc_Id);
3246 if not Debug_Generated_Code then
3247 Set_Debug_Info_Off (Proc_Id);
3251 Agg : constant Node_Id :=
3252 Build_Equivalent_Record_Aggregate (Rec_Type);
3254 procedure Collect_Itypes (Comp : Node_Id);
3255 -- Generate references to itypes in the aggregate, because
3256 -- the first use of the aggregate may be in a nested scope.
3258 --------------------
3259 -- Collect_Itypes --
3260 --------------------
3262 procedure Collect_Itypes (Comp : Node_Id) is
3265 Typ : constant Entity_Id := Etype (Comp);
3268 if Is_Array_Type (Typ)
3269 and then Is_Itype (Typ)
3271 Ref := Make_Itype_Reference (Loc);
3272 Set_Itype (Ref, Typ);
3273 Append_Freeze_Action (Rec_Type, Ref);
3275 Ref := Make_Itype_Reference (Loc);
3276 Set_Itype (Ref, Etype (First_Index (Typ)));
3277 Append_Freeze_Action (Rec_Type, Ref);
3279 Sub_Aggr := First (Expressions (Comp));
3281 -- Recurse on nested arrays
3283 while Present (Sub_Aggr) loop
3284 Collect_Itypes (Sub_Aggr);
3291 -- If there is a static initialization aggregate for the type,
3292 -- generate itype references for the types of its (sub)components,
3293 -- to prevent out-of-scope errors in the resulting tree.
3294 -- The aggregate may have been rewritten as a Raise node, in which
3295 -- case there are no relevant itypes.
3298 and then Nkind (Agg) = N_Aggregate
3300 Set_Static_Initialization (Proc_Id, Agg);
3305 Comp := First (Component_Associations (Agg));
3306 while Present (Comp) loop
3307 Collect_Itypes (Expression (Comp));
3314 end Build_Record_Init_Proc;
3316 ----------------------------
3317 -- Build_Slice_Assignment --
3318 ----------------------------
3320 -- Generates the following subprogram:
3323 -- (Source, Target : Array_Type,
3324 -- Left_Lo, Left_Hi : Index;
3325 -- Right_Lo, Right_Hi : Index;
3333 -- if Left_Hi < Left_Lo then
3346 -- Target (Li1) := Source (Ri1);
3349 -- exit when Li1 = Left_Lo;
3350 -- Li1 := Index'pred (Li1);
3351 -- Ri1 := Index'pred (Ri1);
3353 -- exit when Li1 = Left_Hi;
3354 -- Li1 := Index'succ (Li1);
3355 -- Ri1 := Index'succ (Ri1);
3360 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3361 Loc : constant Source_Ptr := Sloc (Typ);
3362 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3364 -- Build formal parameters of procedure
3366 Larray : constant Entity_Id :=
3367 Make_Defining_Identifier
3368 (Loc, Chars => New_Internal_Name ('A'));
3369 Rarray : constant Entity_Id :=
3370 Make_Defining_Identifier
3371 (Loc, Chars => New_Internal_Name ('R'));
3372 Left_Lo : constant Entity_Id :=
3373 Make_Defining_Identifier
3374 (Loc, Chars => New_Internal_Name ('L'));
3375 Left_Hi : constant Entity_Id :=
3376 Make_Defining_Identifier
3377 (Loc, Chars => New_Internal_Name ('L'));
3378 Right_Lo : constant Entity_Id :=
3379 Make_Defining_Identifier
3380 (Loc, Chars => New_Internal_Name ('R'));
3381 Right_Hi : constant Entity_Id :=
3382 Make_Defining_Identifier
3383 (Loc, Chars => New_Internal_Name ('R'));
3384 Rev : constant Entity_Id :=
3385 Make_Defining_Identifier
3386 (Loc, Chars => New_Internal_Name ('D'));
3387 Proc_Name : constant Entity_Id :=
3388 Make_Defining_Identifier (Loc,
3389 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3391 Lnn : constant Entity_Id :=
3392 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3393 Rnn : constant Entity_Id :=
3394 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3395 -- Subscripts for left and right sides
3402 -- Build declarations for indices
3407 Make_Object_Declaration (Loc,
3408 Defining_Identifier => Lnn,
3409 Object_Definition =>
3410 New_Occurrence_Of (Index, Loc)));
3413 Make_Object_Declaration (Loc,
3414 Defining_Identifier => Rnn,
3415 Object_Definition =>
3416 New_Occurrence_Of (Index, Loc)));
3420 -- Build test for empty slice case
3423 Make_If_Statement (Loc,
3426 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3427 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3428 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3430 -- Build initializations for indices
3433 F_Init : constant List_Id := New_List;
3434 B_Init : constant List_Id := New_List;
3438 Make_Assignment_Statement (Loc,
3439 Name => New_Occurrence_Of (Lnn, Loc),
3440 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3443 Make_Assignment_Statement (Loc,
3444 Name => New_Occurrence_Of (Rnn, Loc),
3445 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3448 Make_Assignment_Statement (Loc,
3449 Name => New_Occurrence_Of (Lnn, Loc),
3450 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3453 Make_Assignment_Statement (Loc,
3454 Name => New_Occurrence_Of (Rnn, Loc),
3455 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3458 Make_If_Statement (Loc,
3459 Condition => New_Occurrence_Of (Rev, Loc),
3460 Then_Statements => B_Init,
3461 Else_Statements => F_Init));
3464 -- Now construct the assignment statement
3467 Make_Loop_Statement (Loc,
3468 Statements => New_List (
3469 Make_Assignment_Statement (Loc,
3471 Make_Indexed_Component (Loc,
3472 Prefix => New_Occurrence_Of (Larray, Loc),
3473 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3475 Make_Indexed_Component (Loc,
3476 Prefix => New_Occurrence_Of (Rarray, Loc),
3477 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3478 End_Label => Empty);
3480 -- Build the exit condition and increment/decrement statements
3483 F_Ass : constant List_Id := New_List;
3484 B_Ass : constant List_Id := New_List;
3488 Make_Exit_Statement (Loc,
3491 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3492 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3495 Make_Assignment_Statement (Loc,
3496 Name => New_Occurrence_Of (Lnn, Loc),
3498 Make_Attribute_Reference (Loc,
3500 New_Occurrence_Of (Index, Loc),
3501 Attribute_Name => Name_Succ,
3502 Expressions => New_List (
3503 New_Occurrence_Of (Lnn, Loc)))));
3506 Make_Assignment_Statement (Loc,
3507 Name => New_Occurrence_Of (Rnn, Loc),
3509 Make_Attribute_Reference (Loc,
3511 New_Occurrence_Of (Index, Loc),
3512 Attribute_Name => Name_Succ,
3513 Expressions => New_List (
3514 New_Occurrence_Of (Rnn, Loc)))));
3517 Make_Exit_Statement (Loc,
3520 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3521 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3524 Make_Assignment_Statement (Loc,
3525 Name => New_Occurrence_Of (Lnn, Loc),
3527 Make_Attribute_Reference (Loc,
3529 New_Occurrence_Of (Index, Loc),
3530 Attribute_Name => Name_Pred,
3531 Expressions => New_List (
3532 New_Occurrence_Of (Lnn, Loc)))));
3535 Make_Assignment_Statement (Loc,
3536 Name => New_Occurrence_Of (Rnn, Loc),
3538 Make_Attribute_Reference (Loc,
3540 New_Occurrence_Of (Index, Loc),
3541 Attribute_Name => Name_Pred,
3542 Expressions => New_List (
3543 New_Occurrence_Of (Rnn, Loc)))));
3545 Append_To (Statements (Loops),
3546 Make_If_Statement (Loc,
3547 Condition => New_Occurrence_Of (Rev, Loc),
3548 Then_Statements => B_Ass,
3549 Else_Statements => F_Ass));
3552 Append_To (Stats, Loops);
3556 Formals : List_Id := New_List;
3559 Formals := New_List (
3560 Make_Parameter_Specification (Loc,
3561 Defining_Identifier => Larray,
3562 Out_Present => True,
3564 New_Reference_To (Base_Type (Typ), Loc)),
3566 Make_Parameter_Specification (Loc,
3567 Defining_Identifier => Rarray,
3569 New_Reference_To (Base_Type (Typ), Loc)),
3571 Make_Parameter_Specification (Loc,
3572 Defining_Identifier => Left_Lo,
3574 New_Reference_To (Index, Loc)),
3576 Make_Parameter_Specification (Loc,
3577 Defining_Identifier => Left_Hi,
3579 New_Reference_To (Index, Loc)),
3581 Make_Parameter_Specification (Loc,
3582 Defining_Identifier => Right_Lo,
3584 New_Reference_To (Index, Loc)),
3586 Make_Parameter_Specification (Loc,
3587 Defining_Identifier => Right_Hi,
3589 New_Reference_To (Index, Loc)));
3592 Make_Parameter_Specification (Loc,
3593 Defining_Identifier => Rev,
3595 New_Reference_To (Standard_Boolean, Loc)));
3598 Make_Procedure_Specification (Loc,
3599 Defining_Unit_Name => Proc_Name,
3600 Parameter_Specifications => Formals);
3603 Make_Subprogram_Body (Loc,
3604 Specification => Spec,
3605 Declarations => Decls,
3606 Handled_Statement_Sequence =>
3607 Make_Handled_Sequence_Of_Statements (Loc,
3608 Statements => Stats)));
3611 Set_TSS (Typ, Proc_Name);
3612 Set_Is_Pure (Proc_Name);
3613 end Build_Slice_Assignment;
3615 ------------------------------------
3616 -- Build_Variant_Record_Equality --
3617 ------------------------------------
3621 -- function _Equality (X, Y : T) return Boolean is
3623 -- -- Compare discriminants
3625 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3629 -- -- Compare components
3631 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3635 -- -- Compare variant part
3639 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3644 -- if False or else X.Cn /= Y.Cn then
3652 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3653 Loc : constant Source_Ptr := Sloc (Typ);
3655 F : constant Entity_Id :=
3656 Make_Defining_Identifier (Loc,
3657 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3659 X : constant Entity_Id :=
3660 Make_Defining_Identifier (Loc,
3663 Y : constant Entity_Id :=
3664 Make_Defining_Identifier (Loc,
3667 Def : constant Node_Id := Parent (Typ);
3668 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3669 Stmts : constant List_Id := New_List;
3670 Pspecs : constant List_Id := New_List;
3673 -- Derived Unchecked_Union types no longer inherit the equality function
3676 if Is_Derived_Type (Typ)
3677 and then not Is_Unchecked_Union (Typ)
3678 and then not Has_New_Non_Standard_Rep (Typ)
3681 Parent_Eq : constant Entity_Id :=
3682 TSS (Root_Type (Typ), TSS_Composite_Equality);
3685 if Present (Parent_Eq) then
3686 Copy_TSS (Parent_Eq, Typ);
3693 Make_Subprogram_Body (Loc,
3695 Make_Function_Specification (Loc,
3696 Defining_Unit_Name => F,
3697 Parameter_Specifications => Pspecs,
3698 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3699 Declarations => New_List,
3700 Handled_Statement_Sequence =>
3701 Make_Handled_Sequence_Of_Statements (Loc,
3702 Statements => Stmts)));
3705 Make_Parameter_Specification (Loc,
3706 Defining_Identifier => X,
3707 Parameter_Type => New_Reference_To (Typ, Loc)));
3710 Make_Parameter_Specification (Loc,
3711 Defining_Identifier => Y,
3712 Parameter_Type => New_Reference_To (Typ, Loc)));
3714 -- Unchecked_Unions require additional machinery to support equality.
3715 -- Two extra parameters (A and B) are added to the equality function
3716 -- parameter list in order to capture the inferred values of the
3717 -- discriminants in later calls.
3719 if Is_Unchecked_Union (Typ) then
3721 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3723 A : constant Node_Id :=
3724 Make_Defining_Identifier (Loc,
3727 B : constant Node_Id :=
3728 Make_Defining_Identifier (Loc,
3732 -- Add A and B to the parameter list
3735 Make_Parameter_Specification (Loc,
3736 Defining_Identifier => A,
3737 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3740 Make_Parameter_Specification (Loc,
3741 Defining_Identifier => B,
3742 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3744 -- Generate the following header code to compare the inferred
3752 Make_If_Statement (Loc,
3755 Left_Opnd => New_Reference_To (A, Loc),
3756 Right_Opnd => New_Reference_To (B, Loc)),
3757 Then_Statements => New_List (
3758 Make_Simple_Return_Statement (Loc,
3759 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3761 -- Generate component-by-component comparison. Note that we must
3762 -- propagate one of the inferred discriminant formals to act as
3763 -- the case statement switch.
3765 Append_List_To (Stmts,
3766 Make_Eq_Case (Typ, Comps, A));
3770 -- Normal case (not unchecked union)
3775 Discriminant_Specifications (Def)));
3777 Append_List_To (Stmts,
3778 Make_Eq_Case (Typ, Comps));
3782 Make_Simple_Return_Statement (Loc,
3783 Expression => New_Reference_To (Standard_True, Loc)));
3788 if not Debug_Generated_Code then
3789 Set_Debug_Info_Off (F);
3791 end Build_Variant_Record_Equality;
3793 -----------------------------
3794 -- Check_Stream_Attributes --
3795 -----------------------------
3797 procedure Check_Stream_Attributes (Typ : Entity_Id) is
3799 Par_Read : constant Boolean :=
3800 Stream_Attribute_Available (Typ, TSS_Stream_Read)
3801 and then not Has_Specified_Stream_Read (Typ);
3802 Par_Write : constant Boolean :=
3803 Stream_Attribute_Available (Typ, TSS_Stream_Write)
3804 and then not Has_Specified_Stream_Write (Typ);
3806 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3807 -- Check that Comp has a user-specified Nam stream attribute
3813 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3815 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3816 Error_Msg_Name_1 := Nam;
3818 ("|component& in limited extension must have% attribute", Comp);
3822 -- Start of processing for Check_Stream_Attributes
3825 if Par_Read or else Par_Write then
3826 Comp := First_Component (Typ);
3827 while Present (Comp) loop
3828 if Comes_From_Source (Comp)
3829 and then Original_Record_Component (Comp) = Comp
3830 and then Is_Limited_Type (Etype (Comp))
3833 Check_Attr (Name_Read, TSS_Stream_Read);
3837 Check_Attr (Name_Write, TSS_Stream_Write);
3841 Next_Component (Comp);
3844 end Check_Stream_Attributes;
3846 -----------------------------
3847 -- Expand_Record_Extension --
3848 -----------------------------
3850 -- Add a field _parent at the beginning of the record extension. This is
3851 -- used to implement inheritance. Here are some examples of expansion:
3853 -- 1. no discriminants
3854 -- type T2 is new T1 with null record;
3856 -- type T2 is new T1 with record
3860 -- 2. renamed discriminants
3861 -- type T2 (B, C : Int) is new T1 (A => B) with record
3862 -- _Parent : T1 (A => B);
3866 -- 3. inherited discriminants
3867 -- type T2 is new T1 with record -- discriminant A inherited
3868 -- _Parent : T1 (A);
3872 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
3873 Indic : constant Node_Id := Subtype_Indication (Def);
3874 Loc : constant Source_Ptr := Sloc (Def);
3875 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
3876 Par_Subtype : Entity_Id;
3877 Comp_List : Node_Id;
3878 Comp_Decl : Node_Id;
3881 List_Constr : constant List_Id := New_List;
3884 -- Expand_Record_Extension is called directly from the semantics, so
3885 -- we must check to see whether expansion is active before proceeding
3887 if not Expander_Active then
3891 -- This may be a derivation of an untagged private type whose full
3892 -- view is tagged, in which case the Derived_Type_Definition has no
3893 -- extension part. Build an empty one now.
3895 if No (Rec_Ext_Part) then
3897 Make_Record_Definition (Loc,
3899 Component_List => Empty,
3900 Null_Present => True);
3902 Set_Record_Extension_Part (Def, Rec_Ext_Part);
3903 Mark_Rewrite_Insertion (Rec_Ext_Part);
3906 Comp_List := Component_List (Rec_Ext_Part);
3908 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
3910 -- If the derived type inherits its discriminants the type of the
3911 -- _parent field must be constrained by the inherited discriminants
3913 if Has_Discriminants (T)
3914 and then Nkind (Indic) /= N_Subtype_Indication
3915 and then not Is_Constrained (Entity (Indic))
3917 D := First_Discriminant (T);
3918 while Present (D) loop
3919 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
3920 Next_Discriminant (D);
3925 Make_Subtype_Indication (Loc,
3926 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
3928 Make_Index_Or_Discriminant_Constraint (Loc,
3929 Constraints => List_Constr)),
3932 -- Otherwise the original subtype_indication is just what is needed
3935 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
3938 Set_Parent_Subtype (T, Par_Subtype);
3941 Make_Component_Declaration (Loc,
3942 Defining_Identifier => Parent_N,
3943 Component_Definition =>
3944 Make_Component_Definition (Loc,
3945 Aliased_Present => False,
3946 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
3948 if Null_Present (Rec_Ext_Part) then
3949 Set_Component_List (Rec_Ext_Part,
3950 Make_Component_List (Loc,
3951 Component_Items => New_List (Comp_Decl),
3952 Variant_Part => Empty,
3953 Null_Present => False));
3954 Set_Null_Present (Rec_Ext_Part, False);
3956 elsif Null_Present (Comp_List)
3957 or else Is_Empty_List (Component_Items (Comp_List))
3959 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3960 Set_Null_Present (Comp_List, False);
3963 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
3966 Analyze (Comp_Decl);
3967 end Expand_Record_Extension;
3969 ------------------------------------
3970 -- Expand_N_Full_Type_Declaration --
3971 ------------------------------------
3973 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
3974 Def_Id : constant Entity_Id := Defining_Identifier (N);
3975 B_Id : constant Entity_Id := Base_Type (Def_Id);
3979 procedure Build_Master (Def_Id : Entity_Id);
3980 -- Create the master associated with Def_Id
3986 procedure Build_Master (Def_Id : Entity_Id) is
3988 -- Anonymous access types are created for the components of the
3989 -- record parameter for an entry declaration. No master is created
3992 if Has_Task (Designated_Type (Def_Id))
3993 and then Comes_From_Source (N)
3995 Build_Master_Entity (Def_Id);
3996 Build_Master_Renaming (Parent (Def_Id), Def_Id);
3998 -- Create a class-wide master because a Master_Id must be generated
3999 -- for access-to-limited-class-wide types whose root may be extended
4000 -- with task components.
4002 -- Note: This code covers access-to-limited-interfaces because they
4003 -- can be used to reference tasks implementing them.
4005 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
4006 and then Is_Limited_Type (Designated_Type (Def_Id))
4007 and then Tasking_Allowed
4009 -- Do not create a class-wide master for types whose convention is
4010 -- Java since these types cannot embed Ada tasks anyway. Note that
4011 -- the following test cannot catch the following case:
4013 -- package java.lang.Object is
4014 -- type Typ is tagged limited private;
4015 -- type Ref is access all Typ'Class;
4017 -- type Typ is tagged limited ...;
4018 -- pragma Convention (Typ, Java)
4021 -- Because the convention appears after we have done the
4022 -- processing for type Ref.
4024 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
4025 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
4027 Build_Class_Wide_Master (Def_Id);
4031 -- Start of processing for Expand_N_Full_Type_Declaration
4034 if Is_Access_Type (Def_Id) then
4035 Build_Master (Def_Id);
4037 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4038 Expand_Access_Protected_Subprogram_Type (N);
4041 elsif Ada_Version >= Ada_05
4042 and then Is_Array_Type (Def_Id)
4043 and then Is_Access_Type (Component_Type (Def_Id))
4044 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4046 Build_Master (Component_Type (Def_Id));
4048 elsif Has_Task (Def_Id) then
4049 Expand_Previous_Access_Type (Def_Id);
4051 elsif Ada_Version >= Ada_05
4053 (Is_Record_Type (Def_Id)
4054 or else (Is_Array_Type (Def_Id)
4055 and then Is_Record_Type (Component_Type (Def_Id))))
4063 -- Look for the first anonymous access type component
4065 if Is_Array_Type (Def_Id) then
4066 Comp := First_Entity (Component_Type (Def_Id));
4068 Comp := First_Entity (Def_Id);
4071 while Present (Comp) loop
4072 Typ := Etype (Comp);
4074 exit when Is_Access_Type (Typ)
4075 and then Ekind (Typ) = E_Anonymous_Access_Type;
4080 -- If found we add a renaming declaration of master_id and we
4081 -- associate it to each anonymous access type component. Do
4082 -- nothing if the access type already has a master. This will be
4083 -- the case if the array type is the packed array created for a
4084 -- user-defined array type T, where the master_id is created when
4085 -- expanding the declaration for T.
4088 and then Ekind (Typ) = E_Anonymous_Access_Type
4089 and then not Restriction_Active (No_Task_Hierarchy)
4090 and then No (Master_Id (Typ))
4092 -- Do not consider run-times with no tasking support
4094 and then RTE_Available (RE_Current_Master)
4095 and then Has_Task (Non_Limited_Designated_Type (Typ))
4097 Build_Master_Entity (Def_Id);
4098 M_Id := Build_Master_Renaming (N, Def_Id);
4100 if Is_Array_Type (Def_Id) then
4101 Comp := First_Entity (Component_Type (Def_Id));
4103 Comp := First_Entity (Def_Id);
4106 while Present (Comp) loop
4107 Typ := Etype (Comp);
4109 if Is_Access_Type (Typ)
4110 and then Ekind (Typ) = E_Anonymous_Access_Type
4112 Set_Master_Id (Typ, M_Id);
4121 Par_Id := Etype (B_Id);
4123 -- The parent type is private then we need to inherit any TSS operations
4124 -- from the full view.
4126 if Ekind (Par_Id) in Private_Kind
4127 and then Present (Full_View (Par_Id))
4129 Par_Id := Base_Type (Full_View (Par_Id));
4132 if Nkind (Type_Definition (Original_Node (N))) =
4133 N_Derived_Type_Definition
4134 and then not Is_Tagged_Type (Def_Id)
4135 and then Present (Freeze_Node (Par_Id))
4136 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4138 Ensure_Freeze_Node (B_Id);
4139 FN := Freeze_Node (B_Id);
4141 if No (TSS_Elist (FN)) then
4142 Set_TSS_Elist (FN, New_Elmt_List);
4146 T_E : constant Elist_Id := TSS_Elist (FN);
4150 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4151 while Present (Elmt) loop
4152 if Chars (Node (Elmt)) /= Name_uInit then
4153 Append_Elmt (Node (Elmt), T_E);
4159 -- If the derived type itself is private with a full view, then
4160 -- associate the full view with the inherited TSS_Elist as well.
4162 if Ekind (B_Id) in Private_Kind
4163 and then Present (Full_View (B_Id))
4165 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4167 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4171 end Expand_N_Full_Type_Declaration;
4173 ---------------------------------
4174 -- Expand_N_Object_Declaration --
4175 ---------------------------------
4177 -- First we do special processing for objects of a tagged type where this
4178 -- is the point at which the type is frozen. The creation of the dispatch
4179 -- table and the initialization procedure have to be deferred to this
4180 -- point, since we reference previously declared primitive subprograms.
4182 -- For all types, we call an initialization procedure if there is one
4184 procedure Expand_N_Object_Declaration (N : Node_Id) is
4185 Def_Id : constant Entity_Id := Defining_Identifier (N);
4186 Expr : constant Node_Id := Expression (N);
4187 Loc : constant Source_Ptr := Sloc (N);
4188 Typ : constant Entity_Id := Etype (Def_Id);
4189 Base_Typ : constant Entity_Id := Base_Type (Typ);
4194 Init_After : Node_Id := N;
4195 -- Node after which the init proc call is to be inserted. This is
4196 -- normally N, except for the case of a shared passive variable, in
4197 -- which case the init proc call must be inserted only after the bodies
4198 -- of the shared variable procedures have been seen.
4200 function Rewrite_As_Renaming return Boolean;
4201 -- Indicate whether to rewrite a declaration with initialization into an
4202 -- object renaming declaration (see below).
4204 -------------------------
4205 -- Rewrite_As_Renaming --
4206 -------------------------
4208 function Rewrite_As_Renaming return Boolean is
4210 return not Aliased_Present (N)
4211 and then Is_Entity_Name (Expr_Q)
4212 and then Ekind (Entity (Expr_Q)) = E_Variable
4213 and then OK_To_Rename (Entity (Expr_Q))
4214 and then Is_Entity_Name (Object_Definition (N));
4215 end Rewrite_As_Renaming;
4217 -- Start of processing for Expand_N_Object_Declaration
4220 -- Don't do anything for deferred constants. All proper actions will be
4221 -- expanded during the full declaration.
4223 if No (Expr) and Constant_Present (N) then
4227 -- Force construction of dispatch tables of library level tagged types
4229 if Tagged_Type_Expansion
4230 and then Static_Dispatch_Tables
4231 and then Is_Library_Level_Entity (Def_Id)
4232 and then Is_Library_Level_Tagged_Type (Base_Typ)
4233 and then (Ekind (Base_Typ) = E_Record_Type
4234 or else Ekind (Base_Typ) = E_Protected_Type
4235 or else Ekind (Base_Typ) = E_Task_Type)
4236 and then not Has_Dispatch_Table (Base_Typ)
4239 New_Nodes : List_Id := No_List;
4242 if Is_Concurrent_Type (Base_Typ) then
4243 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4245 New_Nodes := Make_DT (Base_Typ, N);
4248 if not Is_Empty_List (New_Nodes) then
4249 Insert_List_Before (N, New_Nodes);
4254 -- Make shared memory routines for shared passive variable
4256 if Is_Shared_Passive (Def_Id) then
4257 Init_After := Make_Shared_Var_Procs (N);
4260 -- If tasks being declared, make sure we have an activation chain
4261 -- defined for the tasks (has no effect if we already have one), and
4262 -- also that a Master variable is established and that the appropriate
4263 -- enclosing construct is established as a task master.
4265 if Has_Task (Typ) then
4266 Build_Activation_Chain_Entity (N);
4267 Build_Master_Entity (Def_Id);
4270 -- Build a list controller for declarations where the type is anonymous
4271 -- access and the designated type is controlled. Only declarations from
4272 -- source files receive such controllers in order to provide the same
4273 -- lifespan for any potential coextensions that may be associated with
4274 -- the object. Finalization lists of internal controlled anonymous
4275 -- access objects are already handled in Expand_N_Allocator.
4277 if Comes_From_Source (N)
4278 and then Ekind (Typ) = E_Anonymous_Access_Type
4279 and then Is_Controlled (Directly_Designated_Type (Typ))
4280 and then No (Associated_Final_Chain (Typ))
4282 Build_Final_List (N, Typ);
4285 -- Default initialization required, and no expression present
4289 -- Expand Initialize call for controlled objects. One may wonder why
4290 -- the Initialize Call is not done in the regular Init procedure
4291 -- attached to the record type. That's because the init procedure is
4292 -- recursively called on each component, including _Parent, thus the
4293 -- Init call for a controlled object would generate not only one
4294 -- Initialize call as it is required but one for each ancestor of
4295 -- its type. This processing is suppressed if No_Initialization set.
4297 if not Needs_Finalization (Typ)
4298 or else No_Initialization (N)
4302 elsif not Abort_Allowed
4303 or else not Comes_From_Source (N)
4305 Insert_Actions_After (Init_After,
4307 Ref => New_Occurrence_Of (Def_Id, Loc),
4308 Typ => Base_Type (Typ),
4309 Flist_Ref => Find_Final_List (Def_Id),
4310 With_Attach => Make_Integer_Literal (Loc, 1)));
4315 -- We need to protect the initialize call
4319 -- Initialize (...);
4321 -- Undefer_Abort.all;
4324 -- ??? this won't protect the initialize call for controlled
4325 -- components which are part of the init proc, so this block
4326 -- should probably also contain the call to _init_proc but this
4327 -- requires some code reorganization...
4330 L : constant List_Id :=
4332 (Ref => New_Occurrence_Of (Def_Id, Loc),
4333 Typ => Base_Type (Typ),
4334 Flist_Ref => Find_Final_List (Def_Id),
4335 With_Attach => Make_Integer_Literal (Loc, 1));
4337 Blk : constant Node_Id :=
4338 Make_Block_Statement (Loc,
4339 Handled_Statement_Sequence =>
4340 Make_Handled_Sequence_Of_Statements (Loc, L));
4343 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4344 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4345 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4346 Insert_Actions_After (Init_After, New_List (Blk));
4347 Expand_At_End_Handler
4348 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4352 -- Call type initialization procedure if there is one. We build the
4353 -- call and put it immediately after the object declaration, so that
4354 -- it will be expanded in the usual manner. Note that this will
4355 -- result in proper handling of defaulted discriminants.
4357 -- Need call if there is a base init proc
4359 if Has_Non_Null_Base_Init_Proc (Typ)
4361 -- Suppress call if No_Initialization set on declaration
4363 and then not No_Initialization (N)
4365 -- Suppress call for special case of value type for VM
4367 and then not Is_Value_Type (Typ)
4369 -- Suppress call if Suppress_Init_Proc set on the type. This is
4370 -- needed for the derived type case, where Suppress_Initialization
4371 -- may be set for the derived type, even if there is an init proc
4372 -- defined for the root type.
4374 and then not Suppress_Init_Proc (Typ)
4376 -- Return without initializing when No_Default_Initialization
4377 -- applies. Note that the actual restriction check occurs later,
4378 -- when the object is frozen, because we don't know yet whether
4379 -- the object is imported, which is a case where the check does
4382 if Restriction_Active (No_Default_Initialization) then
4386 -- The call to the initialization procedure does NOT freeze the
4387 -- object being initialized. This is because the call is not a
4388 -- source level call. This works fine, because the only possible
4389 -- statements depending on freeze status that can appear after the
4390 -- Init_Proc call are rep clauses which can safely appear after
4391 -- actual references to the object. Note that this call may
4392 -- subsequently be removed (if a pragma Import is encountered),
4393 -- or moved to the freeze actions for the object (e.g. if an
4394 -- address clause is applied to the object, causing it to get
4395 -- delayed freezing).
4397 Id_Ref := New_Reference_To (Def_Id, Loc);
4398 Set_Must_Not_Freeze (Id_Ref);
4399 Set_Assignment_OK (Id_Ref);
4402 Init_Expr : constant Node_Id :=
4403 Static_Initialization (Base_Init_Proc (Typ));
4405 if Present (Init_Expr) then
4407 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4410 Initialization_Warning (Id_Ref);
4412 Insert_Actions_After (Init_After,
4413 Build_Initialization_Call (Loc, Id_Ref, Typ));
4417 -- If simple initialization is required, then set an appropriate
4418 -- simple initialization expression in place. This special
4419 -- initialization is required even though No_Init_Flag is present,
4420 -- but is not needed if there was an explicit initialization.
4422 -- An internally generated temporary needs no initialization because
4423 -- it will be assigned subsequently. In particular, there is no point
4424 -- in applying Initialize_Scalars to such a temporary.
4426 elsif Needs_Simple_Initialization (Typ)
4427 and then not Is_Internal (Def_Id)
4428 and then not Has_Init_Expression (N)
4430 Set_No_Initialization (N, False);
4431 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4432 Analyze_And_Resolve (Expression (N), Typ);
4435 -- Generate attribute for Persistent_BSS if needed
4437 if Persistent_BSS_Mode
4438 and then Comes_From_Source (N)
4439 and then Is_Potentially_Persistent_Type (Typ)
4440 and then not Has_Init_Expression (N)
4441 and then Is_Library_Level_Entity (Def_Id)
4447 Make_Linker_Section_Pragma
4448 (Def_Id, Sloc (N), ".persistent.bss");
4449 Insert_After (N, Prag);
4454 -- If access type, then we know it is null if not initialized
4456 if Is_Access_Type (Typ) then
4457 Set_Is_Known_Null (Def_Id);
4460 -- Explicit initialization present
4463 -- Obtain actual expression from qualified expression
4465 if Nkind (Expr) = N_Qualified_Expression then
4466 Expr_Q := Expression (Expr);
4471 -- When we have the appropriate type of aggregate in the expression
4472 -- (it has been determined during analysis of the aggregate by
4473 -- setting the delay flag), let's perform in place assignment and
4474 -- thus avoid creating a temporary.
4476 if Is_Delayed_Aggregate (Expr_Q) then
4477 Convert_Aggr_In_Object_Decl (N);
4479 -- Ada 2005 (AI-318-02): If the initialization expression is a call
4480 -- to a build-in-place function, then access to the declared object
4481 -- must be passed to the function. Currently we limit such functions
4482 -- to those with constrained limited result subtypes, but eventually
4483 -- plan to expand the allowed forms of functions that are treated as
4486 elsif Ada_Version >= Ada_05
4487 and then Is_Build_In_Place_Function_Call (Expr_Q)
4489 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4491 -- The previous call expands the expression initializing the
4492 -- built-in-place object into further code that will be analyzed
4493 -- later. No further expansion needed here.
4498 -- In most cases, we must check that the initial value meets any
4499 -- constraint imposed by the declared type. However, there is one
4500 -- very important exception to this rule. If the entity has an
4501 -- unconstrained nominal subtype, then it acquired its constraints
4502 -- from the expression in the first place, and not only does this
4503 -- mean that the constraint check is not needed, but an attempt to
4504 -- perform the constraint check can cause order of elaboration
4507 if not Is_Constr_Subt_For_U_Nominal (Typ) then
4509 -- If this is an allocator for an aggregate that has been
4510 -- allocated in place, delay checks until assignments are
4511 -- made, because the discriminants are not initialized.
4513 if Nkind (Expr) = N_Allocator
4514 and then No_Initialization (Expr)
4518 Apply_Constraint_Check (Expr, Typ);
4520 -- If the expression has been marked as requiring a range
4521 -- generate it now and reset the flag.
4523 if Do_Range_Check (Expr) then
4524 Set_Do_Range_Check (Expr, False);
4525 Generate_Range_Check (Expr, Typ, CE_Range_Check_Failed);
4530 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4531 -- class-wide object to ensure that we copy the full object,
4532 -- unless we are targetting a VM where interfaces are handled by
4533 -- VM itself. Note that if the root type of Typ is an ancestor
4534 -- of Expr's type, both types share the same dispatch table and
4535 -- there is no need to displace the pointer.
4538 -- CW : I'Class := Obj;
4540 -- Temp : I'Class := I'Class (Base_Address (Obj'Address));
4541 -- CW : I'Class renames Displace (Temp, I'Tag);
4543 if Is_Interface (Typ)
4544 and then Is_Class_Wide_Type (Typ)
4546 (Is_Class_Wide_Type (Etype (Expr))
4548 not Is_Ancestor (Root_Type (Typ), Etype (Expr)))
4549 and then Comes_From_Source (Def_Id)
4550 and then Tagged_Type_Expansion
4558 Make_Object_Declaration (Loc,
4559 Defining_Identifier =>
4560 Make_Defining_Identifier (Loc,
4561 New_Internal_Name ('D')),
4563 Object_Definition =>
4564 Make_Attribute_Reference (Loc,
4567 (Root_Type (Etype (Def_Id)), Loc),
4568 Attribute_Name => Name_Class),
4571 Unchecked_Convert_To
4572 (Class_Wide_Type (Root_Type (Etype (Def_Id))),
4573 Make_Explicit_Dereference (Loc,
4574 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4575 Make_Function_Call (Loc,
4577 New_Reference_To (RTE (RE_Base_Address),
4579 Parameter_Associations => New_List (
4580 Make_Attribute_Reference (Loc,
4581 Prefix => Relocate_Node (Expr),
4582 Attribute_Name => Name_Address)))))));
4584 Insert_Action (N, Decl_1);
4587 Make_Object_Renaming_Declaration (Loc,
4588 Defining_Identifier =>
4589 Make_Defining_Identifier (Loc,
4590 New_Internal_Name ('D')),
4593 Make_Attribute_Reference (Loc,
4596 (Root_Type (Etype (Def_Id)), Loc),
4597 Attribute_Name => Name_Class),
4600 Unchecked_Convert_To (
4601 Class_Wide_Type (Root_Type (Etype (Def_Id))),
4602 Make_Explicit_Dereference (Loc,
4603 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4604 Make_Function_Call (Loc,
4606 New_Reference_To (RTE (RE_Displace), Loc),
4608 Parameter_Associations => New_List (
4609 Make_Attribute_Reference (Loc,
4612 (Defining_Identifier (Decl_1), Loc),
4613 Attribute_Name => Name_Address),
4615 Unchecked_Convert_To (RTE (RE_Tag),
4620 (Root_Type (Typ)))),
4623 Rewrite (N, Decl_2);
4626 -- Replace internal identifier of Decl_2 by the identifier
4627 -- found in the sources. We also have to exchange entities
4628 -- containing their defining identifiers to ensure the
4629 -- correct replacement of the object declaration by this
4630 -- object renaming declaration (because such definings
4631 -- identifier have been previously added by Enter_Name to
4632 -- the current scope). We must preserve the homonym chain
4633 -- of the source entity as well.
4635 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4636 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4637 Exchange_Entities (Defining_Identifier (N), Def_Id);
4643 -- If the type is controlled and not inherently limited, then
4644 -- the target is adjusted after the copy and attached to the
4645 -- finalization list. However, no adjustment is done in the case
4646 -- where the object was initialized by a call to a function whose
4647 -- result is built in place, since no copy occurred. (Eventually
4648 -- we plan to support in-place function results for some cases
4649 -- of nonlimited types. ???) Similarly, no adjustment is required
4650 -- if we are going to rewrite the object declaration into a
4651 -- renaming declaration.
4653 if Needs_Finalization (Typ)
4654 and then not Is_Inherently_Limited_Type (Typ)
4655 and then not Rewrite_As_Renaming
4657 Insert_Actions_After (Init_After,
4659 Ref => New_Reference_To (Def_Id, Loc),
4660 Typ => Base_Type (Typ),
4661 Flist_Ref => Find_Final_List (Def_Id),
4662 With_Attach => Make_Integer_Literal (Loc, 1)));
4665 -- For tagged types, when an init value is given, the tag has to
4666 -- be re-initialized separately in order to avoid the propagation
4667 -- of a wrong tag coming from a view conversion unless the type
4668 -- is class wide (in this case the tag comes from the init value).
4669 -- Suppress the tag assignment when VM_Target because VM tags are
4670 -- represented implicitly in objects. Ditto for types that are
4671 -- CPP_CLASS, and for initializations that are aggregates, because
4672 -- they have to have the right tag.
4674 if Is_Tagged_Type (Typ)
4675 and then not Is_Class_Wide_Type (Typ)
4676 and then not Is_CPP_Class (Typ)
4677 and then Tagged_Type_Expansion
4678 and then Nkind (Expr) /= N_Aggregate
4680 -- The re-assignment of the tag has to be done even if the
4681 -- object is a constant.
4684 Make_Selected_Component (Loc,
4685 Prefix => New_Reference_To (Def_Id, Loc),
4687 New_Reference_To (First_Tag_Component (Typ), Loc));
4689 Set_Assignment_OK (New_Ref);
4691 Insert_After (Init_After,
4692 Make_Assignment_Statement (Loc,
4695 Unchecked_Convert_To (RTE (RE_Tag),
4699 (Access_Disp_Table (Base_Type (Typ)))),
4702 elsif Is_Tagged_Type (Typ)
4703 and then Is_CPP_Constructor_Call (Expr)
4705 -- The call to the initialization procedure does NOT freeze the
4706 -- object being initialized.
4708 Id_Ref := New_Reference_To (Def_Id, Loc);
4709 Set_Must_Not_Freeze (Id_Ref);
4710 Set_Assignment_OK (Id_Ref);
4712 Insert_Actions_After (Init_After,
4713 Build_Initialization_Call (Loc, Id_Ref, Typ,
4714 Constructor_Ref => Expr));
4716 -- We remove here the original call to the constructor
4717 -- to avoid its management in the backend
4719 Set_Expression (N, Empty);
4722 -- For discrete types, set the Is_Known_Valid flag if the
4723 -- initializing value is known to be valid.
4725 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
4726 Set_Is_Known_Valid (Def_Id);
4728 elsif Is_Access_Type (Typ) then
4730 -- For access types set the Is_Known_Non_Null flag if the
4731 -- initializing value is known to be non-null. We can also set
4732 -- Can_Never_Be_Null if this is a constant.
4734 if Known_Non_Null (Expr) then
4735 Set_Is_Known_Non_Null (Def_Id, True);
4737 if Constant_Present (N) then
4738 Set_Can_Never_Be_Null (Def_Id);
4743 -- If validity checking on copies, validate initial expression.
4744 -- But skip this if declaration is for a generic type, since it
4745 -- makes no sense to validate generic types. Not clear if this
4746 -- can happen for legal programs, but it definitely can arise
4747 -- from previous instantiation errors.
4749 if Validity_Checks_On
4750 and then Validity_Check_Copies
4751 and then not Is_Generic_Type (Etype (Def_Id))
4753 Ensure_Valid (Expr);
4754 Set_Is_Known_Valid (Def_Id);
4758 -- Cases where the back end cannot handle the initialization directly
4759 -- In such cases, we expand an assignment that will be appropriately
4760 -- handled by Expand_N_Assignment_Statement.
4762 -- The exclusion of the unconstrained case is wrong, but for now it
4763 -- is too much trouble ???
4765 if (Is_Possibly_Unaligned_Slice (Expr)
4766 or else (Is_Possibly_Unaligned_Object (Expr)
4767 and then not Represented_As_Scalar (Etype (Expr))))
4769 -- The exclusion of the unconstrained case is wrong, but for now
4770 -- it is too much trouble ???
4772 and then not (Is_Array_Type (Etype (Expr))
4773 and then not Is_Constrained (Etype (Expr)))
4776 Stat : constant Node_Id :=
4777 Make_Assignment_Statement (Loc,
4778 Name => New_Reference_To (Def_Id, Loc),
4779 Expression => Relocate_Node (Expr));
4781 Set_Expression (N, Empty);
4782 Set_No_Initialization (N);
4783 Set_Assignment_OK (Name (Stat));
4784 Set_No_Ctrl_Actions (Stat);
4785 Insert_After_And_Analyze (Init_After, Stat);
4789 -- Final transformation, if the initializing expression is an entity
4790 -- for a variable with OK_To_Rename set, then we transform:
4796 -- X : typ renames expr
4798 -- provided that X is not aliased. The aliased case has to be
4799 -- excluded in general because Expr will not be aliased in general.
4801 if Rewrite_As_Renaming then
4803 Make_Object_Renaming_Declaration (Loc,
4804 Defining_Identifier => Defining_Identifier (N),
4805 Subtype_Mark => Object_Definition (N),
4808 -- We do not analyze this renaming declaration, because all its
4809 -- components have already been analyzed, and if we were to go
4810 -- ahead and analyze it, we would in effect be trying to generate
4811 -- another declaration of X, which won't do!
4813 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
4820 when RE_Not_Available =>
4822 end Expand_N_Object_Declaration;
4824 ---------------------------------
4825 -- Expand_N_Subtype_Indication --
4826 ---------------------------------
4828 -- Add a check on the range of the subtype. The static case is partially
4829 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
4830 -- to check here for the static case in order to avoid generating
4831 -- extraneous expanded code. Also deal with validity checking.
4833 procedure Expand_N_Subtype_Indication (N : Node_Id) is
4834 Ran : constant Node_Id := Range_Expression (Constraint (N));
4835 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
4838 if Nkind (Constraint (N)) = N_Range_Constraint then
4839 Validity_Check_Range (Range_Expression (Constraint (N)));
4842 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
4843 Apply_Range_Check (Ran, Typ);
4845 end Expand_N_Subtype_Indication;
4847 ---------------------------
4848 -- Expand_N_Variant_Part --
4849 ---------------------------
4851 -- If the last variant does not contain the Others choice, replace it with
4852 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
4853 -- do not bother to call Analyze on the modified variant part, since it's
4854 -- only effect would be to compute the Others_Discrete_Choices node
4855 -- laboriously, and of course we already know the list of choices that
4856 -- corresponds to the others choice (it's the list we are replacing!)
4858 procedure Expand_N_Variant_Part (N : Node_Id) is
4859 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
4860 Others_Node : Node_Id;
4862 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
4863 Others_Node := Make_Others_Choice (Sloc (Last_Var));
4864 Set_Others_Discrete_Choices
4865 (Others_Node, Discrete_Choices (Last_Var));
4866 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
4868 end Expand_N_Variant_Part;
4870 ---------------------------------
4871 -- Expand_Previous_Access_Type --
4872 ---------------------------------
4874 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
4875 T : Entity_Id := First_Entity (Current_Scope);
4878 -- Find all access types declared in the current scope, whose
4879 -- designated type is Def_Id. If it does not have a Master_Id,
4882 while Present (T) loop
4883 if Is_Access_Type (T)
4884 and then Designated_Type (T) = Def_Id
4885 and then No (Master_Id (T))
4887 Build_Master_Entity (Def_Id);
4888 Build_Master_Renaming (Parent (Def_Id), T);
4893 end Expand_Previous_Access_Type;
4895 ------------------------------
4896 -- Expand_Record_Controller --
4897 ------------------------------
4899 procedure Expand_Record_Controller (T : Entity_Id) is
4900 Def : Node_Id := Type_Definition (Parent (T));
4901 Comp_List : Node_Id;
4902 Comp_Decl : Node_Id;
4904 First_Comp : Node_Id;
4905 Controller_Type : Entity_Id;
4909 if Nkind (Def) = N_Derived_Type_Definition then
4910 Def := Record_Extension_Part (Def);
4913 if Null_Present (Def) then
4914 Set_Component_List (Def,
4915 Make_Component_List (Sloc (Def),
4916 Component_Items => Empty_List,
4917 Variant_Part => Empty,
4918 Null_Present => True));
4921 Comp_List := Component_List (Def);
4923 if Null_Present (Comp_List)
4924 or else Is_Empty_List (Component_Items (Comp_List))
4926 Loc := Sloc (Comp_List);
4928 Loc := Sloc (First (Component_Items (Comp_List)));
4931 if Is_Inherently_Limited_Type (T) then
4932 Controller_Type := RTE (RE_Limited_Record_Controller);
4934 Controller_Type := RTE (RE_Record_Controller);
4937 Ent := Make_Defining_Identifier (Loc, Name_uController);
4940 Make_Component_Declaration (Loc,
4941 Defining_Identifier => Ent,
4942 Component_Definition =>
4943 Make_Component_Definition (Loc,
4944 Aliased_Present => False,
4945 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
4947 if Null_Present (Comp_List)
4948 or else Is_Empty_List (Component_Items (Comp_List))
4950 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4951 Set_Null_Present (Comp_List, False);
4954 -- The controller cannot be placed before the _Parent field since
4955 -- gigi lays out field in order and _parent must be first to preserve
4956 -- the polymorphism of tagged types.
4958 First_Comp := First (Component_Items (Comp_List));
4960 if not Is_Tagged_Type (T) then
4961 Insert_Before (First_Comp, Comp_Decl);
4963 -- if T is a tagged type, place controller declaration after parent
4964 -- field and after eventual tags of interface types.
4967 while Present (First_Comp)
4969 (Chars (Defining_Identifier (First_Comp)) = Name_uParent
4970 or else Is_Tag (Defining_Identifier (First_Comp))
4972 -- Ada 2005 (AI-251): The following condition covers secondary
4973 -- tags but also the adjacent component containing the offset
4974 -- to the base of the object (component generated if the parent
4975 -- has discriminants --- see Add_Interface_Tag_Components).
4976 -- This is required to avoid the addition of the controller
4977 -- between the secondary tag and its adjacent component.
4981 (Defining_Identifier (First_Comp))))
4986 -- An empty tagged extension might consist only of the parent
4987 -- component. Otherwise insert the controller before the first
4988 -- component that is neither parent nor tag.
4990 if Present (First_Comp) then
4991 Insert_Before (First_Comp, Comp_Decl);
4993 Append (Comp_Decl, Component_Items (Comp_List));
4999 Analyze (Comp_Decl);
5000 Set_Ekind (Ent, E_Component);
5001 Init_Component_Location (Ent);
5003 -- Move the _controller entity ahead in the list of internal entities
5004 -- of the enclosing record so that it is selected instead of a
5005 -- potentially inherited one.
5008 E : constant Entity_Id := Last_Entity (T);
5012 pragma Assert (Chars (E) = Name_uController);
5014 Set_Next_Entity (E, First_Entity (T));
5015 Set_First_Entity (T, E);
5017 Comp := Next_Entity (E);
5018 while Next_Entity (Comp) /= E loop
5022 Set_Next_Entity (Comp, Empty);
5023 Set_Last_Entity (T, Comp);
5029 when RE_Not_Available =>
5031 end Expand_Record_Controller;
5033 ------------------------
5034 -- Expand_Tagged_Root --
5035 ------------------------
5037 procedure Expand_Tagged_Root (T : Entity_Id) is
5038 Def : constant Node_Id := Type_Definition (Parent (T));
5039 Comp_List : Node_Id;
5040 Comp_Decl : Node_Id;
5041 Sloc_N : Source_Ptr;
5044 if Null_Present (Def) then
5045 Set_Component_List (Def,
5046 Make_Component_List (Sloc (Def),
5047 Component_Items => Empty_List,
5048 Variant_Part => Empty,
5049 Null_Present => True));
5052 Comp_List := Component_List (Def);
5054 if Null_Present (Comp_List)
5055 or else Is_Empty_List (Component_Items (Comp_List))
5057 Sloc_N := Sloc (Comp_List);
5059 Sloc_N := Sloc (First (Component_Items (Comp_List)));
5063 Make_Component_Declaration (Sloc_N,
5064 Defining_Identifier => First_Tag_Component (T),
5065 Component_Definition =>
5066 Make_Component_Definition (Sloc_N,
5067 Aliased_Present => False,
5068 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5070 if Null_Present (Comp_List)
5071 or else Is_Empty_List (Component_Items (Comp_List))
5073 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5074 Set_Null_Present (Comp_List, False);
5077 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5080 -- We don't Analyze the whole expansion because the tag component has
5081 -- already been analyzed previously. Here we just insure that the tree
5082 -- is coherent with the semantic decoration
5084 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5087 when RE_Not_Available =>
5089 end Expand_Tagged_Root;
5091 ----------------------
5092 -- Clean_Task_Names --
5093 ----------------------
5095 procedure Clean_Task_Names
5097 Proc_Id : Entity_Id)
5101 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5102 and then not Global_Discard_Names
5103 and then Tagged_Type_Expansion
5105 Set_Uses_Sec_Stack (Proc_Id);
5107 end Clean_Task_Names;
5109 ------------------------------
5110 -- Expand_Freeze_Array_Type --
5111 ------------------------------
5113 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5114 Typ : constant Entity_Id := Entity (N);
5115 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5116 Base : constant Entity_Id := Base_Type (Typ);
5119 if not Is_Bit_Packed_Array (Typ) then
5121 -- If the component contains tasks, so does the array type. This may
5122 -- not be indicated in the array type because the component may have
5123 -- been a private type at the point of definition. Same if component
5124 -- type is controlled.
5126 Set_Has_Task (Base, Has_Task (Comp_Typ));
5127 Set_Has_Controlled_Component (Base,
5128 Has_Controlled_Component (Comp_Typ)
5129 or else Is_Controlled (Comp_Typ));
5131 if No (Init_Proc (Base)) then
5133 -- If this is an anonymous array created for a declaration with
5134 -- an initial value, its init_proc will never be called. The
5135 -- initial value itself may have been expanded into assignments,
5136 -- in which case the object declaration is carries the
5137 -- No_Initialization flag.
5140 and then Nkind (Associated_Node_For_Itype (Base)) =
5141 N_Object_Declaration
5142 and then (Present (Expression (Associated_Node_For_Itype (Base)))
5144 No_Initialization (Associated_Node_For_Itype (Base)))
5148 -- We do not need an init proc for string or wide [wide] string,
5149 -- since the only time these need initialization in normalize or
5150 -- initialize scalars mode, and these types are treated specially
5151 -- and do not need initialization procedures.
5153 elsif Root_Type (Base) = Standard_String
5154 or else Root_Type (Base) = Standard_Wide_String
5155 or else Root_Type (Base) = Standard_Wide_Wide_String
5159 -- Otherwise we have to build an init proc for the subtype
5162 Build_Array_Init_Proc (Base, N);
5167 if Has_Controlled_Component (Base) then
5168 Build_Controlling_Procs (Base);
5170 if not Is_Limited_Type (Comp_Typ)
5171 and then Number_Dimensions (Typ) = 1
5173 Build_Slice_Assignment (Typ);
5176 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5177 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5179 Set_Associated_Final_Chain (Comp_Typ, Add_Final_Chain (Typ));
5183 -- For packed case, default initialization, except if the component type
5184 -- is itself a packed structure with an initialization procedure, or
5185 -- initialize/normalize scalars active, and we have a base type, or the
5186 -- type is public, because in that case a client might specify
5187 -- Normalize_Scalars and there better be a public Init_Proc for it.
5189 elsif (Present (Init_Proc (Component_Type (Base)))
5190 and then No (Base_Init_Proc (Base)))
5191 or else (Init_Or_Norm_Scalars and then Base = Typ)
5192 or else Is_Public (Typ)
5194 Build_Array_Init_Proc (Base, N);
5196 end Expand_Freeze_Array_Type;
5198 ------------------------------------
5199 -- Expand_Freeze_Enumeration_Type --
5200 ------------------------------------
5202 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5203 Typ : constant Entity_Id := Entity (N);
5204 Loc : constant Source_Ptr := Sloc (Typ);
5211 Is_Contiguous : Boolean;
5216 pragma Warnings (Off, Func);
5219 -- Various optimizations possible if given representation is contiguous
5221 Is_Contiguous := True;
5223 Ent := First_Literal (Typ);
5224 Last_Repval := Enumeration_Rep (Ent);
5227 while Present (Ent) loop
5228 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5229 Is_Contiguous := False;
5232 Last_Repval := Enumeration_Rep (Ent);
5238 if Is_Contiguous then
5239 Set_Has_Contiguous_Rep (Typ);
5240 Ent := First_Literal (Typ);
5242 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5245 -- Build list of literal references
5250 Ent := First_Literal (Typ);
5251 while Present (Ent) loop
5252 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5258 -- Now build an array declaration
5260 -- typA : array (Natural range 0 .. num - 1) of ctype :=
5261 -- (v, v, v, v, v, ....)
5263 -- where ctype is the corresponding integer type. If the representation
5264 -- is contiguous, we only keep the first literal, which provides the
5265 -- offset for Pos_To_Rep computations.
5268 Make_Defining_Identifier (Loc,
5269 Chars => New_External_Name (Chars (Typ), 'A'));
5271 Append_Freeze_Action (Typ,
5272 Make_Object_Declaration (Loc,
5273 Defining_Identifier => Arr,
5274 Constant_Present => True,
5276 Object_Definition =>
5277 Make_Constrained_Array_Definition (Loc,
5278 Discrete_Subtype_Definitions => New_List (
5279 Make_Subtype_Indication (Loc,
5280 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5282 Make_Range_Constraint (Loc,
5286 Make_Integer_Literal (Loc, 0),
5288 Make_Integer_Literal (Loc, Num - 1))))),
5290 Component_Definition =>
5291 Make_Component_Definition (Loc,
5292 Aliased_Present => False,
5293 Subtype_Indication => New_Reference_To (Typ, Loc))),
5296 Make_Aggregate (Loc,
5297 Expressions => Lst)));
5299 Set_Enum_Pos_To_Rep (Typ, Arr);
5301 -- Now we build the function that converts representation values to
5302 -- position values. This function has the form:
5304 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5307 -- when enum-lit'Enum_Rep => return posval;
5308 -- when enum-lit'Enum_Rep => return posval;
5311 -- [raise Constraint_Error when F "invalid data"]
5316 -- Note: the F parameter determines whether the others case (no valid
5317 -- representation) raises Constraint_Error or returns a unique value
5318 -- of minus one. The latter case is used, e.g. in 'Valid code.
5320 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5321 -- the code generator making inappropriate assumptions about the range
5322 -- of the values in the case where the value is invalid. ityp is a
5323 -- signed or unsigned integer type of appropriate width.
5325 -- Note: if exceptions are not supported, then we suppress the raise
5326 -- and return -1 unconditionally (this is an erroneous program in any
5327 -- case and there is no obligation to raise Constraint_Error here!) We
5328 -- also do this if pragma Restrictions (No_Exceptions) is active.
5330 -- Is this right??? What about No_Exception_Propagation???
5332 -- Representations are signed
5334 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5336 -- The underlying type is signed. Reset the Is_Unsigned_Type
5337 -- explicitly, because it might have been inherited from
5340 Set_Is_Unsigned_Type (Typ, False);
5342 if Esize (Typ) <= Standard_Integer_Size then
5343 Ityp := Standard_Integer;
5345 Ityp := Universal_Integer;
5348 -- Representations are unsigned
5351 if Esize (Typ) <= Standard_Integer_Size then
5352 Ityp := RTE (RE_Unsigned);
5354 Ityp := RTE (RE_Long_Long_Unsigned);
5358 -- The body of the function is a case statement. First collect case
5359 -- alternatives, or optimize the contiguous case.
5363 -- If representation is contiguous, Pos is computed by subtracting
5364 -- the representation of the first literal.
5366 if Is_Contiguous then
5367 Ent := First_Literal (Typ);
5369 if Enumeration_Rep (Ent) = Last_Repval then
5371 -- Another special case: for a single literal, Pos is zero
5373 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5377 Convert_To (Standard_Integer,
5378 Make_Op_Subtract (Loc,
5380 Unchecked_Convert_To (Ityp,
5381 Make_Identifier (Loc, Name_uA)),
5383 Make_Integer_Literal (Loc,
5385 Enumeration_Rep (First_Literal (Typ)))));
5389 Make_Case_Statement_Alternative (Loc,
5390 Discrete_Choices => New_List (
5391 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5393 Make_Integer_Literal (Loc,
5394 Intval => Enumeration_Rep (Ent)),
5396 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5398 Statements => New_List (
5399 Make_Simple_Return_Statement (Loc,
5400 Expression => Pos_Expr))));
5403 Ent := First_Literal (Typ);
5404 while Present (Ent) loop
5406 Make_Case_Statement_Alternative (Loc,
5407 Discrete_Choices => New_List (
5408 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5409 Intval => Enumeration_Rep (Ent))),
5411 Statements => New_List (
5412 Make_Simple_Return_Statement (Loc,
5414 Make_Integer_Literal (Loc,
5415 Intval => Enumeration_Pos (Ent))))));
5421 -- In normal mode, add the others clause with the test
5423 if not No_Exception_Handlers_Set then
5425 Make_Case_Statement_Alternative (Loc,
5426 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5427 Statements => New_List (
5428 Make_Raise_Constraint_Error (Loc,
5429 Condition => Make_Identifier (Loc, Name_uF),
5430 Reason => CE_Invalid_Data),
5431 Make_Simple_Return_Statement (Loc,
5433 Make_Integer_Literal (Loc, -1)))));
5435 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5436 -- active then return -1 (we cannot usefully raise Constraint_Error in
5437 -- this case). See description above for further details.
5441 Make_Case_Statement_Alternative (Loc,
5442 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5443 Statements => New_List (
5444 Make_Simple_Return_Statement (Loc,
5446 Make_Integer_Literal (Loc, -1)))));
5449 -- Now we can build the function body
5452 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5455 Make_Subprogram_Body (Loc,
5457 Make_Function_Specification (Loc,
5458 Defining_Unit_Name => Fent,
5459 Parameter_Specifications => New_List (
5460 Make_Parameter_Specification (Loc,
5461 Defining_Identifier =>
5462 Make_Defining_Identifier (Loc, Name_uA),
5463 Parameter_Type => New_Reference_To (Typ, Loc)),
5464 Make_Parameter_Specification (Loc,
5465 Defining_Identifier =>
5466 Make_Defining_Identifier (Loc, Name_uF),
5467 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5469 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5471 Declarations => Empty_List,
5473 Handled_Statement_Sequence =>
5474 Make_Handled_Sequence_Of_Statements (Loc,
5475 Statements => New_List (
5476 Make_Case_Statement (Loc,
5478 Unchecked_Convert_To (Ityp,
5479 Make_Identifier (Loc, Name_uA)),
5480 Alternatives => Lst))));
5482 Set_TSS (Typ, Fent);
5485 if not Debug_Generated_Code then
5486 Set_Debug_Info_Off (Fent);
5490 when RE_Not_Available =>
5492 end Expand_Freeze_Enumeration_Type;
5494 -------------------------------
5495 -- Expand_Freeze_Record_Type --
5496 -------------------------------
5498 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5500 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id);
5501 -- Add to the list of primitives of Tagged_Types the internal entities
5502 -- associated with interface primitives that are located in secondary
5505 -------------------------------------
5506 -- Add_Internal_Interface_Entities --
5507 -------------------------------------
5509 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
5512 Iface_Elmt : Elmt_Id;
5513 Iface_Prim : Entity_Id;
5514 Ifaces_List : Elist_Id;
5515 New_Subp : Entity_Id := Empty;
5519 pragma Assert (Ada_Version >= Ada_05
5520 and then Is_Record_Type (Tagged_Type)
5521 and then Is_Tagged_Type (Tagged_Type)
5522 and then Has_Interfaces (Tagged_Type)
5523 and then not Is_Interface (Tagged_Type));
5525 Collect_Interfaces (Tagged_Type, Ifaces_List);
5527 Iface_Elmt := First_Elmt (Ifaces_List);
5528 while Present (Iface_Elmt) loop
5529 Iface := Node (Iface_Elmt);
5531 -- Exclude from this processing interfaces that are parents
5532 -- of Tagged_Type because their primitives are located in the
5533 -- primary dispatch table (and hence no auxiliary internal
5534 -- entities are required to handle secondary dispatch tables
5537 if not Is_Ancestor (Iface, Tagged_Type) then
5538 Elmt := First_Elmt (Primitive_Operations (Iface));
5539 while Present (Elmt) loop
5540 Iface_Prim := Node (Elmt);
5542 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
5544 Find_Primitive_Covering_Interface
5545 (Tagged_Type => Tagged_Type,
5546 Iface_Prim => Iface_Prim);
5548 pragma Assert (Present (Prim));
5551 (New_Subp => New_Subp,
5552 Parent_Subp => Iface_Prim,
5553 Derived_Type => Tagged_Type,
5554 Parent_Type => Iface);
5556 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
5557 -- associated with interface types. These entities are
5558 -- only registered in the list of primitives of its
5559 -- corresponding tagged type because they are only used
5560 -- to fill the contents of the secondary dispatch tables.
5561 -- Therefore they are removed from the homonym chains.
5563 Set_Is_Hidden (New_Subp);
5564 Set_Is_Internal (New_Subp);
5565 Set_Alias (New_Subp, Prim);
5566 Set_Is_Abstract_Subprogram (New_Subp,
5567 Is_Abstract_Subprogram (Prim));
5568 Set_Interface_Alias (New_Subp, Iface_Prim);
5570 -- Internal entities associated with interface types are
5571 -- only registered in the list of primitives of the
5572 -- tagged type. They are only used to fill the contents
5573 -- of the secondary dispatch tables. Therefore they are
5574 -- not needed in the homonym chains.
5576 Remove_Homonym (New_Subp);
5578 -- Hidden entities associated with interfaces must have
5579 -- set the Has_Delay_Freeze attribute to ensure that, in
5580 -- case of locally defined tagged types (or compiling
5581 -- with static dispatch tables generation disabled) the
5582 -- corresponding entry of the secondary dispatch table is
5583 -- filled when such entity is frozen.
5585 Set_Has_Delayed_Freeze (New_Subp);
5592 Next_Elmt (Iface_Elmt);
5594 end Add_Internal_Interface_Entities;
5598 Def_Id : constant Node_Id := Entity (N);
5599 Type_Decl : constant Node_Id := Parent (Def_Id);
5601 Comp_Typ : Entity_Id;
5602 Has_Static_DT : Boolean := False;
5603 Predef_List : List_Id;
5605 Flist : Entity_Id := Empty;
5606 -- Finalization list allocated for the case of a type with anonymous
5607 -- access components whose designated type is potentially controlled.
5609 Renamed_Eq : Node_Id := Empty;
5610 -- Defining unit name for the predefined equality function in the case
5611 -- where the type has a primitive operation that is a renaming of
5612 -- predefined equality (but only if there is also an overriding
5613 -- user-defined equality function). Used to pass this entity from
5614 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5616 Wrapper_Decl_List : List_Id := No_List;
5617 Wrapper_Body_List : List_Id := No_List;
5618 Null_Proc_Decl_List : List_Id := No_List;
5620 -- Start of processing for Expand_Freeze_Record_Type
5623 -- Build discriminant checking functions if not a derived type (for
5624 -- derived types that are not tagged types, always use the discriminant
5625 -- checking functions of the parent type). However, for untagged types
5626 -- the derivation may have taken place before the parent was frozen, so
5627 -- we copy explicitly the discriminant checking functions from the
5628 -- parent into the components of the derived type.
5630 if not Is_Derived_Type (Def_Id)
5631 or else Has_New_Non_Standard_Rep (Def_Id)
5632 or else Is_Tagged_Type (Def_Id)
5634 Build_Discr_Checking_Funcs (Type_Decl);
5636 elsif Is_Derived_Type (Def_Id)
5637 and then not Is_Tagged_Type (Def_Id)
5639 -- If we have a derived Unchecked_Union, we do not inherit the
5640 -- discriminant checking functions from the parent type since the
5641 -- discriminants are non existent.
5643 and then not Is_Unchecked_Union (Def_Id)
5644 and then Has_Discriminants (Def_Id)
5647 Old_Comp : Entity_Id;
5651 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5652 Comp := First_Component (Def_Id);
5653 while Present (Comp) loop
5654 if Ekind (Comp) = E_Component
5655 and then Chars (Comp) = Chars (Old_Comp)
5657 Set_Discriminant_Checking_Func (Comp,
5658 Discriminant_Checking_Func (Old_Comp));
5661 Next_Component (Old_Comp);
5662 Next_Component (Comp);
5667 if Is_Derived_Type (Def_Id)
5668 and then Is_Limited_Type (Def_Id)
5669 and then Is_Tagged_Type (Def_Id)
5671 Check_Stream_Attributes (Def_Id);
5674 -- Update task and controlled component flags, because some of the
5675 -- component types may have been private at the point of the record
5678 Comp := First_Component (Def_Id);
5680 while Present (Comp) loop
5681 Comp_Typ := Etype (Comp);
5683 if Has_Task (Comp_Typ) then
5684 Set_Has_Task (Def_Id);
5686 elsif Has_Controlled_Component (Comp_Typ)
5687 or else (Chars (Comp) /= Name_uParent
5688 and then Is_Controlled (Comp_Typ))
5690 Set_Has_Controlled_Component (Def_Id);
5692 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5693 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5696 Flist := Add_Final_Chain (Def_Id);
5699 Set_Associated_Final_Chain (Comp_Typ, Flist);
5702 Next_Component (Comp);
5705 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5706 -- for regular tagged types as well as for Ada types deriving from a C++
5707 -- Class, but not for tagged types directly corresponding to C++ classes
5708 -- In the later case we assume that it is created in the C++ side and we
5711 if Is_Tagged_Type (Def_Id) then
5713 Static_Dispatch_Tables
5714 and then Is_Library_Level_Tagged_Type (Def_Id);
5716 -- Add the _Tag component
5718 if Underlying_Type (Etype (Def_Id)) = Def_Id then
5719 Expand_Tagged_Root (Def_Id);
5722 if Is_CPP_Class (Def_Id) then
5723 Set_All_DT_Position (Def_Id);
5724 Set_CPP_Constructors (Def_Id);
5726 -- Create the tag entities with a minimum decoration
5728 if Tagged_Type_Expansion then
5729 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5733 if not Has_Static_DT then
5735 -- Usually inherited primitives are not delayed but the first
5736 -- Ada extension of a CPP_Class is an exception since the
5737 -- address of the inherited subprogram has to be inserted in
5738 -- the new Ada Dispatch Table and this is a freezing action.
5740 -- Similarly, if this is an inherited operation whose parent is
5741 -- not frozen yet, it is not in the DT of the parent, and we
5742 -- generate an explicit freeze node for the inherited operation
5743 -- so that it is properly inserted in the DT of the current
5747 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
5751 while Present (Elmt) loop
5752 Subp := Node (Elmt);
5754 if Present (Alias (Subp)) then
5755 if Is_CPP_Class (Etype (Def_Id)) then
5756 Set_Has_Delayed_Freeze (Subp);
5758 elsif Has_Delayed_Freeze (Alias (Subp))
5759 and then not Is_Frozen (Alias (Subp))
5761 Set_Is_Frozen (Subp, False);
5762 Set_Has_Delayed_Freeze (Subp);
5771 -- Unfreeze momentarily the type to add the predefined primitives
5772 -- operations. The reason we unfreeze is so that these predefined
5773 -- operations will indeed end up as primitive operations (which
5774 -- must be before the freeze point).
5776 Set_Is_Frozen (Def_Id, False);
5778 -- Do not add the spec of predefined primitives in case of
5779 -- CPP tagged type derivations that have convention CPP.
5781 if Is_CPP_Class (Root_Type (Def_Id))
5782 and then Convention (Def_Id) = Convention_CPP
5786 -- Do not add the spec of the predefined primitives if we are
5787 -- compiling under restriction No_Dispatching_Calls
5789 elsif not Restriction_Active (No_Dispatching_Calls) then
5790 Make_Predefined_Primitive_Specs
5791 (Def_Id, Predef_List, Renamed_Eq);
5792 Insert_List_Before_And_Analyze (N, Predef_List);
5795 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5796 -- wrapper functions for each nonoverridden inherited function
5797 -- with a controlling result of the type. The wrapper for such
5798 -- a function returns an extension aggregate that invokes the
5799 -- the parent function.
5801 if Ada_Version >= Ada_05
5802 and then not Is_Abstract_Type (Def_Id)
5803 and then Is_Null_Extension (Def_Id)
5805 Make_Controlling_Function_Wrappers
5806 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
5807 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5810 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5811 -- null procedure declarations for each set of homographic null
5812 -- procedures that are inherited from interface types but not
5813 -- overridden. This is done to ensure that the dispatch table
5814 -- entry associated with such null primitives are properly filled.
5816 if Ada_Version >= Ada_05
5817 and then Etype (Def_Id) /= Def_Id
5818 and then not Is_Abstract_Type (Def_Id)
5820 Make_Null_Procedure_Specs (Def_Id, Null_Proc_Decl_List);
5821 Insert_Actions (N, Null_Proc_Decl_List);
5824 -- Ada 2005 (AI-251): Add internal entities associated with
5825 -- secondary dispatch tables to the list of primitives of tagged
5826 -- types that are not interfaces
5828 if Ada_Version >= Ada_05
5829 and then not Is_Interface (Def_Id)
5830 and then Has_Interfaces (Def_Id)
5832 Add_Internal_Interface_Entities (Def_Id);
5835 Set_Is_Frozen (Def_Id);
5836 Set_All_DT_Position (Def_Id);
5838 -- Add the controlled component before the freezing actions
5839 -- referenced in those actions.
5841 if Has_New_Controlled_Component (Def_Id) then
5842 Expand_Record_Controller (Def_Id);
5845 -- Create and decorate the tags. Suppress their creation when
5846 -- VM_Target because the dispatching mechanism is handled
5847 -- internally by the VMs.
5849 if Tagged_Type_Expansion then
5850 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5852 -- Generate dispatch table of locally defined tagged type.
5853 -- Dispatch tables of library level tagged types are built
5854 -- later (see Analyze_Declarations).
5856 if not Has_Static_DT then
5857 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
5861 -- If the type has unknown discriminants, propagate dispatching
5862 -- information to its underlying record view, which does not get
5863 -- its own dispatch table.
5865 if Is_Derived_Type (Def_Id)
5866 and then Has_Unknown_Discriminants (Def_Id)
5867 and then Present (Underlying_Record_View (Def_Id))
5870 Rep : constant Entity_Id :=
5871 Underlying_Record_View (Def_Id);
5873 Set_Access_Disp_Table
5874 (Rep, Access_Disp_Table (Def_Id));
5875 Set_Dispatch_Table_Wrappers
5876 (Rep, Dispatch_Table_Wrappers (Def_Id));
5877 Set_Primitive_Operations
5878 (Rep, Primitive_Operations (Def_Id));
5882 -- Make sure that the primitives Initialize, Adjust and Finalize
5883 -- are Frozen before other TSS subprograms. We don't want them
5886 if Is_Controlled (Def_Id) then
5887 if not Is_Limited_Type (Def_Id) then
5888 Append_Freeze_Actions (Def_Id,
5890 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
5893 Append_Freeze_Actions (Def_Id,
5895 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
5897 Append_Freeze_Actions (Def_Id,
5899 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
5902 -- Freeze rest of primitive operations. There is no need to handle
5903 -- the predefined primitives if we are compiling under restriction
5904 -- No_Dispatching_Calls
5906 if not Restriction_Active (No_Dispatching_Calls) then
5907 Append_Freeze_Actions
5908 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
5912 -- In the non-tagged case, an equality function is provided only for
5913 -- variant records (that are not unchecked unions).
5915 elsif Has_Discriminants (Def_Id)
5916 and then not Is_Limited_Type (Def_Id)
5919 Comps : constant Node_Id :=
5920 Component_List (Type_Definition (Type_Decl));
5924 and then Present (Variant_Part (Comps))
5926 Build_Variant_Record_Equality (Def_Id);
5931 -- Before building the record initialization procedure, if we are
5932 -- dealing with a concurrent record value type, then we must go through
5933 -- the discriminants, exchanging discriminals between the concurrent
5934 -- type and the concurrent record value type. See the section "Handling
5935 -- of Discriminants" in the Einfo spec for details.
5937 if Is_Concurrent_Record_Type (Def_Id)
5938 and then Has_Discriminants (Def_Id)
5941 Ctyp : constant Entity_Id :=
5942 Corresponding_Concurrent_Type (Def_Id);
5943 Conc_Discr : Entity_Id;
5944 Rec_Discr : Entity_Id;
5948 Conc_Discr := First_Discriminant (Ctyp);
5949 Rec_Discr := First_Discriminant (Def_Id);
5951 while Present (Conc_Discr) loop
5952 Temp := Discriminal (Conc_Discr);
5953 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5954 Set_Discriminal (Rec_Discr, Temp);
5956 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5957 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5959 Next_Discriminant (Conc_Discr);
5960 Next_Discriminant (Rec_Discr);
5965 if Has_Controlled_Component (Def_Id) then
5966 if No (Controller_Component (Def_Id)) then
5967 Expand_Record_Controller (Def_Id);
5970 Build_Controlling_Procs (Def_Id);
5973 Adjust_Discriminants (Def_Id);
5975 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
5977 -- Do not need init for interfaces on e.g. CIL since they're
5978 -- abstract. Helps operation of peverify (the PE Verify tool).
5980 Build_Record_Init_Proc (Type_Decl, Def_Id);
5983 -- For tagged type that are not interfaces, build bodies of primitive
5984 -- operations. Note that we do this after building the record
5985 -- initialization procedure, since the primitive operations may need
5986 -- the initialization routine. There is no need to add predefined
5987 -- primitives of interfaces because all their predefined primitives
5990 if Is_Tagged_Type (Def_Id)
5991 and then not Is_Interface (Def_Id)
5993 -- Do not add the body of predefined primitives in case of
5994 -- CPP tagged type derivations that have convention CPP.
5996 if Is_CPP_Class (Root_Type (Def_Id))
5997 and then Convention (Def_Id) = Convention_CPP
6001 -- Do not add the body of the predefined primitives if we are
6002 -- compiling under restriction No_Dispatching_Calls or if we are
6003 -- compiling a CPP tagged type.
6005 elsif not Restriction_Active (No_Dispatching_Calls) then
6006 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6007 Append_Freeze_Actions (Def_Id, Predef_List);
6010 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6011 -- inherited functions, then add their bodies to the freeze actions.
6013 if Present (Wrapper_Body_List) then
6014 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6017 -- Create extra formals for the primitive operations of the type.
6018 -- This must be done before analyzing the body of the initialization
6019 -- procedure, because a self-referential type might call one of these
6020 -- primitives in the body of the init_proc itself.
6027 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6028 while Present (Elmt) loop
6029 Subp := Node (Elmt);
6030 if not Has_Foreign_Convention (Subp)
6031 and then not Is_Predefined_Dispatching_Operation (Subp)
6033 Create_Extra_Formals (Subp);
6040 end Expand_Freeze_Record_Type;
6042 ------------------------------
6043 -- Freeze_Stream_Operations --
6044 ------------------------------
6046 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6047 Names : constant array (1 .. 4) of TSS_Name_Type :=
6052 Stream_Op : Entity_Id;
6055 -- Primitive operations of tagged types are frozen when the dispatch
6056 -- table is constructed.
6058 if not Comes_From_Source (Typ)
6059 or else Is_Tagged_Type (Typ)
6064 for J in Names'Range loop
6065 Stream_Op := TSS (Typ, Names (J));
6067 if Present (Stream_Op)
6068 and then Is_Subprogram (Stream_Op)
6069 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6070 N_Subprogram_Declaration
6071 and then not Is_Frozen (Stream_Op)
6073 Append_Freeze_Actions
6074 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
6077 end Freeze_Stream_Operations;
6083 -- Full type declarations are expanded at the point at which the type is
6084 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
6085 -- declarations generated by the freezing (e.g. the procedure generated
6086 -- for initialization) are chained in the Actions field list of the freeze
6087 -- node using Append_Freeze_Actions.
6089 function Freeze_Type (N : Node_Id) return Boolean is
6090 Def_Id : constant Entity_Id := Entity (N);
6091 RACW_Seen : Boolean := False;
6092 Result : Boolean := False;
6095 -- Process associated access types needing special processing
6097 if Present (Access_Types_To_Process (N)) then
6099 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
6101 while Present (E) loop
6103 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6104 Validate_RACW_Primitives (Node (E));
6114 -- If there are RACWs designating this type, make stubs now
6116 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
6120 -- Freeze processing for record types
6122 if Is_Record_Type (Def_Id) then
6123 if Ekind (Def_Id) = E_Record_Type then
6124 Expand_Freeze_Record_Type (N);
6126 -- The subtype may have been declared before the type was frozen. If
6127 -- the type has controlled components it is necessary to create the
6128 -- entity for the controller explicitly because it did not exist at
6129 -- the point of the subtype declaration. Only the entity is needed,
6130 -- the back-end will obtain the layout from the type. This is only
6131 -- necessary if this is constrained subtype whose component list is
6132 -- not shared with the base type.
6134 elsif Ekind (Def_Id) = E_Record_Subtype
6135 and then Has_Discriminants (Def_Id)
6136 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
6137 and then Present (Controller_Component (Def_Id))
6140 Old_C : constant Entity_Id := Controller_Component (Def_Id);
6144 if Scope (Old_C) = Base_Type (Def_Id) then
6146 -- The entity is the one in the parent. Create new one
6148 New_C := New_Copy (Old_C);
6149 Set_Parent (New_C, Parent (Old_C));
6150 Push_Scope (Def_Id);
6156 if Is_Itype (Def_Id)
6157 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
6159 -- The freeze node is only used to introduce the controller,
6160 -- the back-end has no use for it for a discriminated
6163 Set_Freeze_Node (Def_Id, Empty);
6164 Set_Has_Delayed_Freeze (Def_Id, False);
6168 -- Similar process if the controller of the subtype is not present
6169 -- but the parent has it. This can happen with constrained
6170 -- record components where the subtype is an itype.
6172 elsif Ekind (Def_Id) = E_Record_Subtype
6173 and then Is_Itype (Def_Id)
6174 and then No (Controller_Component (Def_Id))
6175 and then Present (Controller_Component (Etype (Def_Id)))
6178 Old_C : constant Entity_Id :=
6179 Controller_Component (Etype (Def_Id));
6180 New_C : constant Entity_Id := New_Copy (Old_C);
6183 Set_Next_Entity (New_C, First_Entity (Def_Id));
6184 Set_First_Entity (Def_Id, New_C);
6186 -- The freeze node is only used to introduce the controller,
6187 -- the back-end has no use for it for a discriminated
6190 Set_Freeze_Node (Def_Id, Empty);
6191 Set_Has_Delayed_Freeze (Def_Id, False);
6196 -- Freeze processing for array types
6198 elsif Is_Array_Type (Def_Id) then
6199 Expand_Freeze_Array_Type (N);
6201 -- Freeze processing for access types
6203 -- For pool-specific access types, find out the pool object used for
6204 -- this type, needs actual expansion of it in some cases. Here are the
6205 -- different cases :
6207 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
6208 -- ---> don't use any storage pool
6210 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
6212 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
6214 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6215 -- ---> Storage Pool is the specified one
6217 -- See GNAT Pool packages in the Run-Time for more details
6219 elsif Ekind (Def_Id) = E_Access_Type
6220 or else Ekind (Def_Id) = E_General_Access_Type
6223 Loc : constant Source_Ptr := Sloc (N);
6224 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
6225 Pool_Object : Entity_Id;
6227 Freeze_Action_Typ : Entity_Id;
6232 -- Rep Clause "for Def_Id'Storage_Size use 0;"
6233 -- ---> don't use any storage pool
6235 if No_Pool_Assigned (Def_Id) then
6240 -- Rep Clause : for Def_Id'Storage_Size use Expr.
6242 -- Def_Id__Pool : Stack_Bounded_Pool
6243 -- (Expr, DT'Size, DT'Alignment);
6245 elsif Has_Storage_Size_Clause (Def_Id) then
6251 -- For unconstrained composite types we give a size of zero
6252 -- so that the pool knows that it needs a special algorithm
6253 -- for variable size object allocation.
6255 if Is_Composite_Type (Desig_Type)
6256 and then not Is_Constrained (Desig_Type)
6259 Make_Integer_Literal (Loc, 0);
6262 Make_Integer_Literal (Loc, Maximum_Alignment);
6266 Make_Attribute_Reference (Loc,
6267 Prefix => New_Reference_To (Desig_Type, Loc),
6268 Attribute_Name => Name_Max_Size_In_Storage_Elements);
6271 Make_Attribute_Reference (Loc,
6272 Prefix => New_Reference_To (Desig_Type, Loc),
6273 Attribute_Name => Name_Alignment);
6277 Make_Defining_Identifier (Loc,
6278 Chars => New_External_Name (Chars (Def_Id), 'P'));
6280 -- We put the code associated with the pools in the entity
6281 -- that has the later freeze node, usually the access type
6282 -- but it can also be the designated_type; because the pool
6283 -- code requires both those types to be frozen
6285 if Is_Frozen (Desig_Type)
6286 and then (No (Freeze_Node (Desig_Type))
6287 or else Analyzed (Freeze_Node (Desig_Type)))
6289 Freeze_Action_Typ := Def_Id;
6291 -- A Taft amendment type cannot get the freeze actions
6292 -- since the full view is not there.
6294 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6295 and then No (Full_View (Desig_Type))
6297 Freeze_Action_Typ := Def_Id;
6300 Freeze_Action_Typ := Desig_Type;
6303 Append_Freeze_Action (Freeze_Action_Typ,
6304 Make_Object_Declaration (Loc,
6305 Defining_Identifier => Pool_Object,
6306 Object_Definition =>
6307 Make_Subtype_Indication (Loc,
6310 (RTE (RE_Stack_Bounded_Pool), Loc),
6313 Make_Index_Or_Discriminant_Constraint (Loc,
6314 Constraints => New_List (
6316 -- First discriminant is the Pool Size
6319 Storage_Size_Variable (Def_Id), Loc),
6321 -- Second discriminant is the element size
6325 -- Third discriminant is the alignment
6330 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6334 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6335 -- ---> Storage Pool is the specified one
6337 elsif Present (Associated_Storage_Pool (Def_Id)) then
6339 -- Nothing to do the associated storage pool has been attached
6340 -- when analyzing the rep. clause
6345 -- For access-to-controlled types (including class-wide types and
6346 -- Taft-amendment types which potentially have controlled
6347 -- components), expand the list controller object that will store
6348 -- the dynamically allocated objects. Do not do this
6349 -- transformation for expander-generated access types, but do it
6350 -- for types that are the full view of types derived from other
6351 -- private types. Also suppress the list controller in the case
6352 -- of a designated type with convention Java, since this is used
6353 -- when binding to Java API specs, where there's no equivalent of
6354 -- a finalization list and we don't want to pull in the
6355 -- finalization support if not needed.
6357 if not Comes_From_Source (Def_Id)
6358 and then not Has_Private_Declaration (Def_Id)
6362 elsif (Needs_Finalization (Desig_Type)
6363 and then Convention (Desig_Type) /= Convention_Java
6364 and then Convention (Desig_Type) /= Convention_CIL)
6366 (Is_Incomplete_Or_Private_Type (Desig_Type)
6367 and then No (Full_View (Desig_Type))
6369 -- An exception is made for types defined in the run-time
6370 -- because Ada.Tags.Tag itself is such a type and cannot
6371 -- afford this unnecessary overhead that would generates a
6372 -- loop in the expansion scheme...
6374 and then not In_Runtime (Def_Id)
6376 -- Another exception is if Restrictions (No_Finalization)
6377 -- is active, since then we know nothing is controlled.
6379 and then not Restriction_Active (No_Finalization))
6381 -- If the designated type is not frozen yet, its controlled
6382 -- status must be retrieved explicitly.
6384 or else (Is_Array_Type (Desig_Type)
6385 and then not Is_Frozen (Desig_Type)
6386 and then Needs_Finalization (Component_Type (Desig_Type)))
6388 -- The designated type has controlled anonymous access
6391 or else Has_Controlled_Coextensions (Desig_Type)
6393 Set_Associated_Final_Chain (Def_Id, Add_Final_Chain (Def_Id));
6397 -- Freeze processing for enumeration types
6399 elsif Ekind (Def_Id) = E_Enumeration_Type then
6401 -- We only have something to do if we have a non-standard
6402 -- representation (i.e. at least one literal whose pos value
6403 -- is not the same as its representation)
6405 if Has_Non_Standard_Rep (Def_Id) then
6406 Expand_Freeze_Enumeration_Type (N);
6409 -- Private types that are completed by a derivation from a private
6410 -- type have an internally generated full view, that needs to be
6411 -- frozen. This must be done explicitly because the two views share
6412 -- the freeze node, and the underlying full view is not visible when
6413 -- the freeze node is analyzed.
6415 elsif Is_Private_Type (Def_Id)
6416 and then Is_Derived_Type (Def_Id)
6417 and then Present (Full_View (Def_Id))
6418 and then Is_Itype (Full_View (Def_Id))
6419 and then Has_Private_Declaration (Full_View (Def_Id))
6420 and then Freeze_Node (Full_View (Def_Id)) = N
6422 Set_Entity (N, Full_View (Def_Id));
6423 Result := Freeze_Type (N);
6424 Set_Entity (N, Def_Id);
6426 -- All other types require no expander action. There are such cases
6427 -- (e.g. task types and protected types). In such cases, the freeze
6428 -- nodes are there for use by Gigi.
6432 Freeze_Stream_Operations (N, Def_Id);
6436 when RE_Not_Available =>
6440 -------------------------
6441 -- Get_Simple_Init_Val --
6442 -------------------------
6444 function Get_Simple_Init_Val
6447 Size : Uint := No_Uint) return Node_Id
6449 Loc : constant Source_Ptr := Sloc (N);
6455 -- This is the size to be used for computation of the appropriate
6456 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
6458 IV_Attribute : constant Boolean :=
6459 Nkind (N) = N_Attribute_Reference
6460 and then Attribute_Name (N) = Name_Invalid_Value;
6464 -- These are the values computed by the procedure Check_Subtype_Bounds
6466 procedure Check_Subtype_Bounds;
6467 -- This procedure examines the subtype T, and its ancestor subtypes and
6468 -- derived types to determine the best known information about the
6469 -- bounds of the subtype. After the call Lo_Bound is set either to
6470 -- No_Uint if no information can be determined, or to a value which
6471 -- represents a known low bound, i.e. a valid value of the subtype can
6472 -- not be less than this value. Hi_Bound is similarly set to a known
6473 -- high bound (valid value cannot be greater than this).
6475 --------------------------
6476 -- Check_Subtype_Bounds --
6477 --------------------------
6479 procedure Check_Subtype_Bounds is
6488 Lo_Bound := No_Uint;
6489 Hi_Bound := No_Uint;
6491 -- Loop to climb ancestor subtypes and derived types
6495 if not Is_Discrete_Type (ST1) then
6499 Lo := Type_Low_Bound (ST1);
6500 Hi := Type_High_Bound (ST1);
6502 if Compile_Time_Known_Value (Lo) then
6503 Loval := Expr_Value (Lo);
6505 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6510 if Compile_Time_Known_Value (Hi) then
6511 Hival := Expr_Value (Hi);
6513 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6518 ST2 := Ancestor_Subtype (ST1);
6524 exit when ST1 = ST2;
6527 end Check_Subtype_Bounds;
6529 -- Start of processing for Get_Simple_Init_Val
6532 -- For a private type, we should always have an underlying type
6533 -- (because this was already checked in Needs_Simple_Initialization).
6534 -- What we do is to get the value for the underlying type and then do
6535 -- an Unchecked_Convert to the private type.
6537 if Is_Private_Type (T) then
6538 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6540 -- A special case, if the underlying value is null, then qualify it
6541 -- with the underlying type, so that the null is properly typed
6542 -- Similarly, if it is an aggregate it must be qualified, because an
6543 -- unchecked conversion does not provide a context for it.
6545 if Nkind_In (Val, N_Null, N_Aggregate) then
6547 Make_Qualified_Expression (Loc,
6549 New_Occurrence_Of (Underlying_Type (T), Loc),
6553 Result := Unchecked_Convert_To (T, Val);
6555 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6557 if Nkind (Result) = N_Unchecked_Type_Conversion
6558 and then Is_Scalar_Type (Underlying_Type (T))
6560 Set_No_Truncation (Result);
6565 -- For scalars, we must have normalize/initialize scalars case, or
6566 -- if the node N is an 'Invalid_Value attribute node.
6568 elsif Is_Scalar_Type (T) then
6569 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6571 -- Compute size of object. If it is given by the caller, we can use
6572 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6573 -- we know this covers all cases correctly.
6575 if Size = No_Uint or else Size <= Uint_0 then
6576 Size_To_Use := UI_Max (Uint_1, Esize (T));
6578 Size_To_Use := Size;
6581 -- Maximum size to use is 64 bits, since we will create values
6582 -- of type Unsigned_64 and the range must fit this type.
6584 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6585 Size_To_Use := Uint_64;
6588 -- Check known bounds of subtype
6590 Check_Subtype_Bounds;
6592 -- Processing for Normalize_Scalars case
6594 if Normalize_Scalars and then not IV_Attribute then
6596 -- If zero is invalid, it is a convenient value to use that is
6597 -- for sure an appropriate invalid value in all situations.
6599 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6600 Val := Make_Integer_Literal (Loc, 0);
6602 -- Cases where all one bits is the appropriate invalid value
6604 -- For modular types, all 1 bits is either invalid or valid. If
6605 -- it is valid, then there is nothing that can be done since there
6606 -- are no invalid values (we ruled out zero already).
6608 -- For signed integer types that have no negative values, either
6609 -- there is room for negative values, or there is not. If there
6610 -- is, then all 1 bits may be interpreted as minus one, which is
6611 -- certainly invalid. Alternatively it is treated as the largest
6612 -- positive value, in which case the observation for modular types
6615 -- For float types, all 1-bits is a NaN (not a number), which is
6616 -- certainly an appropriately invalid value.
6618 elsif Is_Unsigned_Type (T)
6619 or else Is_Floating_Point_Type (T)
6620 or else Is_Enumeration_Type (T)
6622 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6624 -- Resolve as Unsigned_64, because the largest number we
6625 -- can generate is out of range of universal integer.
6627 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6629 -- Case of signed types
6633 Signed_Size : constant Uint :=
6634 UI_Min (Uint_63, Size_To_Use - 1);
6637 -- Normally we like to use the most negative number. The
6638 -- one exception is when this number is in the known
6639 -- subtype range and the largest positive number is not in
6640 -- the known subtype range.
6642 -- For this exceptional case, use largest positive value
6644 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6645 and then Lo_Bound <= (-(2 ** Signed_Size))
6646 and then Hi_Bound < 2 ** Signed_Size
6648 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6650 -- Normal case of largest negative value
6653 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6658 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
6661 -- For float types, use float values from System.Scalar_Values
6663 if Is_Floating_Point_Type (T) then
6664 if Root_Type (T) = Standard_Short_Float then
6665 Val_RE := RE_IS_Isf;
6666 elsif Root_Type (T) = Standard_Float then
6667 Val_RE := RE_IS_Ifl;
6668 elsif Root_Type (T) = Standard_Long_Float then
6669 Val_RE := RE_IS_Ilf;
6670 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6671 Val_RE := RE_IS_Ill;
6674 -- If zero is invalid, use zero values from System.Scalar_Values
6676 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6677 if Size_To_Use <= 8 then
6678 Val_RE := RE_IS_Iz1;
6679 elsif Size_To_Use <= 16 then
6680 Val_RE := RE_IS_Iz2;
6681 elsif Size_To_Use <= 32 then
6682 Val_RE := RE_IS_Iz4;
6684 Val_RE := RE_IS_Iz8;
6687 -- For unsigned, use unsigned values from System.Scalar_Values
6689 elsif Is_Unsigned_Type (T) then
6690 if Size_To_Use <= 8 then
6691 Val_RE := RE_IS_Iu1;
6692 elsif Size_To_Use <= 16 then
6693 Val_RE := RE_IS_Iu2;
6694 elsif Size_To_Use <= 32 then
6695 Val_RE := RE_IS_Iu4;
6697 Val_RE := RE_IS_Iu8;
6700 -- For signed, use signed values from System.Scalar_Values
6703 if Size_To_Use <= 8 then
6704 Val_RE := RE_IS_Is1;
6705 elsif Size_To_Use <= 16 then
6706 Val_RE := RE_IS_Is2;
6707 elsif Size_To_Use <= 32 then
6708 Val_RE := RE_IS_Is4;
6710 Val_RE := RE_IS_Is8;
6714 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
6717 -- The final expression is obtained by doing an unchecked conversion
6718 -- of this result to the base type of the required subtype. We use
6719 -- the base type to avoid the unchecked conversion from chopping
6720 -- bits, and then we set Kill_Range_Check to preserve the "bad"
6723 Result := Unchecked_Convert_To (Base_Type (T), Val);
6725 -- Ensure result is not truncated, since we want the "bad" bits
6726 -- and also kill range check on result.
6728 if Nkind (Result) = N_Unchecked_Type_Conversion then
6729 Set_No_Truncation (Result);
6730 Set_Kill_Range_Check (Result, True);
6735 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
6737 elsif Root_Type (T) = Standard_String
6739 Root_Type (T) = Standard_Wide_String
6741 Root_Type (T) = Standard_Wide_Wide_String
6743 pragma Assert (Init_Or_Norm_Scalars);
6746 Make_Aggregate (Loc,
6747 Component_Associations => New_List (
6748 Make_Component_Association (Loc,
6749 Choices => New_List (
6750 Make_Others_Choice (Loc)),
6753 (Component_Type (T), N, Esize (Root_Type (T))))));
6755 -- Access type is initialized to null
6757 elsif Is_Access_Type (T) then
6761 -- No other possibilities should arise, since we should only be
6762 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
6763 -- returned True, indicating one of the above cases held.
6766 raise Program_Error;
6770 when RE_Not_Available =>
6772 end Get_Simple_Init_Val;
6774 ------------------------------
6775 -- Has_New_Non_Standard_Rep --
6776 ------------------------------
6778 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
6780 if not Is_Derived_Type (T) then
6781 return Has_Non_Standard_Rep (T)
6782 or else Has_Non_Standard_Rep (Root_Type (T));
6784 -- If Has_Non_Standard_Rep is not set on the derived type, the
6785 -- representation is fully inherited.
6787 elsif not Has_Non_Standard_Rep (T) then
6791 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
6793 -- May need a more precise check here: the First_Rep_Item may
6794 -- be a stream attribute, which does not affect the representation
6797 end Has_New_Non_Standard_Rep;
6803 function In_Runtime (E : Entity_Id) return Boolean is
6808 while Scope (S1) /= Standard_Standard loop
6812 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
6815 ----------------------------
6816 -- Initialization_Warning --
6817 ----------------------------
6819 procedure Initialization_Warning (E : Entity_Id) is
6820 Warning_Needed : Boolean;
6823 Warning_Needed := False;
6825 if Ekind (Current_Scope) = E_Package
6826 and then Static_Elaboration_Desired (Current_Scope)
6829 if Is_Record_Type (E) then
6830 if Has_Discriminants (E)
6831 or else Is_Limited_Type (E)
6832 or else Has_Non_Standard_Rep (E)
6834 Warning_Needed := True;
6837 -- Verify that at least one component has an initialization
6838 -- expression. No need for a warning on a type if all its
6839 -- components have no initialization.
6845 Comp := First_Component (E);
6846 while Present (Comp) loop
6847 if Ekind (Comp) = E_Discriminant
6849 (Nkind (Parent (Comp)) = N_Component_Declaration
6850 and then Present (Expression (Parent (Comp))))
6852 Warning_Needed := True;
6856 Next_Component (Comp);
6861 if Warning_Needed then
6863 ("Objects of the type cannot be initialized " &
6864 "statically by default?",
6870 Error_Msg_N ("Object cannot be initialized statically?", E);
6873 end Initialization_Warning;
6879 function Init_Formals (Typ : Entity_Id) return List_Id is
6880 Loc : constant Source_Ptr := Sloc (Typ);
6884 -- First parameter is always _Init : in out typ. Note that we need
6885 -- this to be in/out because in the case of the task record value,
6886 -- there are default record fields (_Priority, _Size, -Task_Info)
6887 -- that may be referenced in the generated initialization routine.
6889 Formals := New_List (
6890 Make_Parameter_Specification (Loc,
6891 Defining_Identifier =>
6892 Make_Defining_Identifier (Loc, Name_uInit),
6894 Out_Present => True,
6895 Parameter_Type => New_Reference_To (Typ, Loc)));
6897 -- For task record value, or type that contains tasks, add two more
6898 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
6899 -- We also add these parameters for the task record type case.
6902 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
6905 Make_Parameter_Specification (Loc,
6906 Defining_Identifier =>
6907 Make_Defining_Identifier (Loc, Name_uMaster),
6908 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
6911 Make_Parameter_Specification (Loc,
6912 Defining_Identifier =>
6913 Make_Defining_Identifier (Loc, Name_uChain),
6915 Out_Present => True,
6917 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
6920 Make_Parameter_Specification (Loc,
6921 Defining_Identifier =>
6922 Make_Defining_Identifier (Loc, Name_uTask_Name),
6925 New_Reference_To (Standard_String, Loc)));
6931 when RE_Not_Available =>
6935 -------------------------
6936 -- Init_Secondary_Tags --
6937 -------------------------
6939 procedure Init_Secondary_Tags
6942 Stmts_List : List_Id;
6943 Fixed_Comps : Boolean := True;
6944 Variable_Comps : Boolean := True)
6946 Loc : constant Source_Ptr := Sloc (Target);
6948 procedure Inherit_CPP_Tag
6951 Tag_Comp : Entity_Id;
6952 Iface_Tag : Node_Id);
6953 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
6954 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6956 procedure Initialize_Tag
6959 Tag_Comp : Entity_Id;
6960 Iface_Tag : Node_Id);
6961 -- Initialize the tag of the secondary dispatch table of Typ associated
6962 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6963 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
6964 -- of Typ CPP tagged type we generate code to inherit the contents of
6965 -- the dispatch table directly from the ancestor.
6967 ---------------------
6968 -- Inherit_CPP_Tag --
6969 ---------------------
6971 procedure Inherit_CPP_Tag
6974 Tag_Comp : Entity_Id;
6975 Iface_Tag : Node_Id)
6978 pragma Assert (Is_CPP_Class (Etype (Typ)));
6980 Append_To (Stmts_List,
6981 Build_Inherit_Prims (Loc,
6984 Make_Selected_Component (Loc,
6985 Prefix => New_Copy_Tree (Target),
6986 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
6988 New_Reference_To (Iface_Tag, Loc),
6990 UI_To_Int (DT_Entry_Count (First_Tag_Component (Iface)))));
6991 end Inherit_CPP_Tag;
6993 --------------------
6994 -- Initialize_Tag --
6995 --------------------
6997 procedure Initialize_Tag
7000 Tag_Comp : Entity_Id;
7001 Iface_Tag : Node_Id)
7003 Comp_Typ : Entity_Id;
7004 Offset_To_Top_Comp : Entity_Id := Empty;
7007 -- Initialize the pointer to the secondary DT associated with the
7010 if not Is_Ancestor (Iface, Typ) then
7011 Append_To (Stmts_List,
7012 Make_Assignment_Statement (Loc,
7014 Make_Selected_Component (Loc,
7015 Prefix => New_Copy_Tree (Target),
7016 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7018 New_Reference_To (Iface_Tag, Loc)));
7021 Comp_Typ := Scope (Tag_Comp);
7023 -- Initialize the entries of the table of interfaces. We generate a
7024 -- different call when the parent of the type has variable size
7027 if Comp_Typ /= Etype (Comp_Typ)
7028 and then Is_Variable_Size_Record (Etype (Comp_Typ))
7029 and then Chars (Tag_Comp) /= Name_uTag
7031 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7033 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
7034 -- configurable run-time environment.
7036 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7038 ("variable size record with interface types", Typ);
7043 -- Set_Dynamic_Offset_To_Top
7045 -- Interface_T => Iface'Tag,
7046 -- Offset_Value => n,
7047 -- Offset_Func => Fn'Address)
7049 Append_To (Stmts_List,
7050 Make_Procedure_Call_Statement (Loc,
7051 Name => New_Reference_To
7052 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7053 Parameter_Associations => New_List (
7054 Make_Attribute_Reference (Loc,
7055 Prefix => New_Copy_Tree (Target),
7056 Attribute_Name => Name_Address),
7058 Unchecked_Convert_To (RTE (RE_Tag),
7060 (Node (First_Elmt (Access_Disp_Table (Iface))),
7063 Unchecked_Convert_To
7064 (RTE (RE_Storage_Offset),
7065 Make_Attribute_Reference (Loc,
7067 Make_Selected_Component (Loc,
7068 Prefix => New_Copy_Tree (Target),
7070 New_Reference_To (Tag_Comp, Loc)),
7071 Attribute_Name => Name_Position)),
7073 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7074 Make_Attribute_Reference (Loc,
7075 Prefix => New_Reference_To
7076 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7077 Attribute_Name => Name_Address)))));
7079 -- In this case the next component stores the value of the
7080 -- offset to the top.
7082 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7083 pragma Assert (Present (Offset_To_Top_Comp));
7085 Append_To (Stmts_List,
7086 Make_Assignment_Statement (Loc,
7088 Make_Selected_Component (Loc,
7089 Prefix => New_Copy_Tree (Target),
7090 Selector_Name => New_Reference_To
7091 (Offset_To_Top_Comp, Loc)),
7093 Make_Attribute_Reference (Loc,
7095 Make_Selected_Component (Loc,
7096 Prefix => New_Copy_Tree (Target),
7098 New_Reference_To (Tag_Comp, Loc)),
7099 Attribute_Name => Name_Position)));
7101 -- Normal case: No discriminants in the parent type
7104 -- Don't need to set any value if this interface shares
7105 -- the primary dispatch table.
7107 if not Is_Ancestor (Iface, Typ) then
7108 Append_To (Stmts_List,
7109 Build_Set_Static_Offset_To_Top (Loc,
7110 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
7112 Unchecked_Convert_To (RTE (RE_Storage_Offset),
7113 Make_Attribute_Reference (Loc,
7115 Make_Selected_Component (Loc,
7116 Prefix => New_Copy_Tree (Target),
7118 New_Reference_To (Tag_Comp, Loc)),
7119 Attribute_Name => Name_Position))));
7123 -- Register_Interface_Offset
7125 -- Interface_T => Iface'Tag,
7126 -- Is_Constant => True,
7127 -- Offset_Value => n,
7128 -- Offset_Func => null);
7130 if RTE_Available (RE_Register_Interface_Offset) then
7131 Append_To (Stmts_List,
7132 Make_Procedure_Call_Statement (Loc,
7133 Name => New_Reference_To
7134 (RTE (RE_Register_Interface_Offset), Loc),
7135 Parameter_Associations => New_List (
7136 Make_Attribute_Reference (Loc,
7137 Prefix => New_Copy_Tree (Target),
7138 Attribute_Name => Name_Address),
7140 Unchecked_Convert_To (RTE (RE_Tag),
7142 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
7144 New_Occurrence_Of (Standard_True, Loc),
7146 Unchecked_Convert_To
7147 (RTE (RE_Storage_Offset),
7148 Make_Attribute_Reference (Loc,
7150 Make_Selected_Component (Loc,
7151 Prefix => New_Copy_Tree (Target),
7153 New_Reference_To (Tag_Comp, Loc)),
7154 Attribute_Name => Name_Position)),
7163 Full_Typ : Entity_Id;
7164 Ifaces_List : Elist_Id;
7165 Ifaces_Comp_List : Elist_Id;
7166 Ifaces_Tag_List : Elist_Id;
7167 Iface_Elmt : Elmt_Id;
7168 Iface_Comp_Elmt : Elmt_Id;
7169 Iface_Tag_Elmt : Elmt_Id;
7171 In_Variable_Pos : Boolean;
7173 -- Start of processing for Init_Secondary_Tags
7176 -- Handle private types
7178 if Present (Full_View (Typ)) then
7179 Full_Typ := Full_View (Typ);
7184 Collect_Interfaces_Info
7185 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
7187 Iface_Elmt := First_Elmt (Ifaces_List);
7188 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
7189 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
7190 while Present (Iface_Elmt) loop
7191 Tag_Comp := Node (Iface_Comp_Elmt);
7193 -- If we are compiling under the CPP full ABI compatibility mode and
7194 -- the ancestor is a CPP_Pragma tagged type then we generate code to
7195 -- inherit the contents of the dispatch table directly from the
7198 if Is_CPP_Class (Etype (Full_Typ)) then
7199 Inherit_CPP_Tag (Full_Typ,
7200 Iface => Node (Iface_Elmt),
7201 Tag_Comp => Tag_Comp,
7202 Iface_Tag => Node (Iface_Tag_Elmt));
7204 -- Otherwise generate code to initialize the tag
7207 -- Check if the parent of the record type has variable size
7210 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
7211 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
7213 if (In_Variable_Pos and then Variable_Comps)
7214 or else (not In_Variable_Pos and then Fixed_Comps)
7216 Initialize_Tag (Full_Typ,
7217 Iface => Node (Iface_Elmt),
7218 Tag_Comp => Tag_Comp,
7219 Iface_Tag => Node (Iface_Tag_Elmt));
7223 Next_Elmt (Iface_Elmt);
7224 Next_Elmt (Iface_Comp_Elmt);
7225 Next_Elmt (Iface_Tag_Elmt);
7227 end Init_Secondary_Tags;
7229 -----------------------------
7230 -- Is_Variable_Size_Record --
7231 -----------------------------
7233 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7235 Comp_Typ : Entity_Id;
7238 function Is_Constant_Bound (Exp : Node_Id) return Boolean;
7239 -- To simplify handling of array components. Determines whether the
7240 -- given bound is constant (a constant or enumeration literal, or an
7241 -- integer literal) as opposed to per-object, through an expression
7242 -- or a discriminant.
7244 -----------------------
7245 -- Is_Constant_Bound --
7246 -----------------------
7248 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
7250 if Nkind (Exp) = N_Integer_Literal then
7254 Is_Entity_Name (Exp)
7255 and then Present (Entity (Exp))
7257 (Ekind (Entity (Exp)) = E_Constant
7258 or else Ekind (Entity (Exp)) = E_Enumeration_Literal);
7260 end Is_Constant_Bound;
7262 -- Start of processing for Is_Variable_Sized_Record
7265 pragma Assert (Is_Record_Type (E));
7267 Comp := First_Entity (E);
7268 while Present (Comp) loop
7269 Comp_Typ := Etype (Comp);
7271 if Is_Record_Type (Comp_Typ) then
7273 -- Recursive call if the record type has discriminants
7275 if Has_Discriminants (Comp_Typ)
7276 and then Is_Variable_Size_Record (Comp_Typ)
7281 elsif Is_Array_Type (Comp_Typ) then
7283 -- Check if some index is initialized with a non-constant value
7285 Idx := First_Index (Comp_Typ);
7286 while Present (Idx) loop
7287 if Nkind (Idx) = N_Range then
7288 if not Is_Constant_Bound (Low_Bound (Idx))
7290 not Is_Constant_Bound (High_Bound (Idx))
7296 Idx := Next_Index (Idx);
7304 end Is_Variable_Size_Record;
7306 ----------------------------------------
7307 -- Make_Controlling_Function_Wrappers --
7308 ----------------------------------------
7310 procedure Make_Controlling_Function_Wrappers
7311 (Tag_Typ : Entity_Id;
7312 Decl_List : out List_Id;
7313 Body_List : out List_Id)
7315 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7316 Prim_Elmt : Elmt_Id;
7318 Actual_List : List_Id;
7319 Formal_List : List_Id;
7321 Par_Formal : Entity_Id;
7322 Formal_Node : Node_Id;
7323 Func_Body : Node_Id;
7324 Func_Decl : Node_Id;
7325 Func_Spec : Node_Id;
7326 Return_Stmt : Node_Id;
7329 Decl_List := New_List;
7330 Body_List := New_List;
7332 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7334 while Present (Prim_Elmt) loop
7335 Subp := Node (Prim_Elmt);
7337 -- If a primitive function with a controlling result of the type has
7338 -- not been overridden by the user, then we must create a wrapper
7339 -- function here that effectively overrides it and invokes the
7340 -- (non-abstract) parent function. This can only occur for a null
7341 -- extension. Note that functions with anonymous controlling access
7342 -- results don't qualify and must be overridden. We also exclude
7343 -- Input attributes, since each type will have its own version of
7344 -- Input constructed by the expander. The test for Comes_From_Source
7345 -- is needed to distinguish inherited operations from renamings
7346 -- (which also have Alias set).
7348 -- The function may be abstract, or require_Overriding may be set
7349 -- for it, because tests for null extensions may already have reset
7350 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7351 -- set, functions that need wrappers are recognized by having an
7352 -- alias that returns the parent type.
7354 if Comes_From_Source (Subp)
7355 or else No (Alias (Subp))
7356 or else Ekind (Subp) /= E_Function
7357 or else not Has_Controlling_Result (Subp)
7358 or else Is_Access_Type (Etype (Subp))
7359 or else Is_Abstract_Subprogram (Alias (Subp))
7360 or else Is_TSS (Subp, TSS_Stream_Input)
7364 elsif Is_Abstract_Subprogram (Subp)
7365 or else Requires_Overriding (Subp)
7367 (Is_Null_Extension (Etype (Subp))
7368 and then Etype (Alias (Subp)) /= Etype (Subp))
7370 Formal_List := No_List;
7371 Formal := First_Formal (Subp);
7373 if Present (Formal) then
7374 Formal_List := New_List;
7376 while Present (Formal) loop
7378 (Make_Parameter_Specification
7380 Defining_Identifier =>
7381 Make_Defining_Identifier (Sloc (Formal),
7382 Chars => Chars (Formal)),
7383 In_Present => In_Present (Parent (Formal)),
7384 Out_Present => Out_Present (Parent (Formal)),
7385 Null_Exclusion_Present =>
7386 Null_Exclusion_Present (Parent (Formal)),
7388 New_Reference_To (Etype (Formal), Loc),
7390 New_Copy_Tree (Expression (Parent (Formal)))),
7393 Next_Formal (Formal);
7398 Make_Function_Specification (Loc,
7399 Defining_Unit_Name =>
7400 Make_Defining_Identifier (Loc,
7401 Chars => Chars (Subp)),
7402 Parameter_Specifications => Formal_List,
7403 Result_Definition =>
7404 New_Reference_To (Etype (Subp), Loc));
7406 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7407 Append_To (Decl_List, Func_Decl);
7409 -- Build a wrapper body that calls the parent function. The body
7410 -- contains a single return statement that returns an extension
7411 -- aggregate whose ancestor part is a call to the parent function,
7412 -- passing the formals as actuals (with any controlling arguments
7413 -- converted to the types of the corresponding formals of the
7414 -- parent function, which might be anonymous access types), and
7415 -- having a null extension.
7417 Formal := First_Formal (Subp);
7418 Par_Formal := First_Formal (Alias (Subp));
7419 Formal_Node := First (Formal_List);
7421 if Present (Formal) then
7422 Actual_List := New_List;
7424 Actual_List := No_List;
7427 while Present (Formal) loop
7428 if Is_Controlling_Formal (Formal) then
7429 Append_To (Actual_List,
7430 Make_Type_Conversion (Loc,
7432 New_Occurrence_Of (Etype (Par_Formal), Loc),
7435 (Defining_Identifier (Formal_Node), Loc)));
7440 (Defining_Identifier (Formal_Node), Loc));
7443 Next_Formal (Formal);
7444 Next_Formal (Par_Formal);
7449 Make_Simple_Return_Statement (Loc,
7451 Make_Extension_Aggregate (Loc,
7453 Make_Function_Call (Loc,
7454 Name => New_Reference_To (Alias (Subp), Loc),
7455 Parameter_Associations => Actual_List),
7456 Null_Record_Present => True));
7459 Make_Subprogram_Body (Loc,
7460 Specification => New_Copy_Tree (Func_Spec),
7461 Declarations => Empty_List,
7462 Handled_Statement_Sequence =>
7463 Make_Handled_Sequence_Of_Statements (Loc,
7464 Statements => New_List (Return_Stmt)));
7466 Set_Defining_Unit_Name
7467 (Specification (Func_Body),
7468 Make_Defining_Identifier (Loc, Chars (Subp)));
7470 Append_To (Body_List, Func_Body);
7472 -- Replace the inherited function with the wrapper function
7473 -- in the primitive operations list.
7475 Override_Dispatching_Operation
7476 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7480 Next_Elmt (Prim_Elmt);
7482 end Make_Controlling_Function_Wrappers;
7488 -- <Make_Eq_If shared components>
7490 -- when V1 => <Make_Eq_Case> on subcomponents
7492 -- when Vn => <Make_Eq_Case> on subcomponents
7495 function Make_Eq_Case
7498 Discr : Entity_Id := Empty) return List_Id
7500 Loc : constant Source_Ptr := Sloc (E);
7501 Result : constant List_Id := New_List;
7506 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7508 if No (Variant_Part (CL)) then
7512 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7514 if No (Variant) then
7518 Alt_List := New_List;
7520 while Present (Variant) loop
7521 Append_To (Alt_List,
7522 Make_Case_Statement_Alternative (Loc,
7523 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7524 Statements => Make_Eq_Case (E, Component_List (Variant))));
7526 Next_Non_Pragma (Variant);
7529 -- If we have an Unchecked_Union, use one of the parameters that
7530 -- captures the discriminants.
7532 if Is_Unchecked_Union (E) then
7534 Make_Case_Statement (Loc,
7535 Expression => New_Reference_To (Discr, Loc),
7536 Alternatives => Alt_List));
7540 Make_Case_Statement (Loc,
7542 Make_Selected_Component (Loc,
7543 Prefix => Make_Identifier (Loc, Name_X),
7544 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7545 Alternatives => Alt_List));
7566 -- or a null statement if the list L is empty
7570 L : List_Id) return Node_Id
7572 Loc : constant Source_Ptr := Sloc (E);
7574 Field_Name : Name_Id;
7579 return Make_Null_Statement (Loc);
7584 C := First_Non_Pragma (L);
7585 while Present (C) loop
7586 Field_Name := Chars (Defining_Identifier (C));
7588 -- The tags must not be compared: they are not part of the value.
7589 -- Ditto for the controller component, if present.
7591 -- Note also that in the following, we use Make_Identifier for
7592 -- the component names. Use of New_Reference_To to identify the
7593 -- components would be incorrect because the wrong entities for
7594 -- discriminants could be picked up in the private type case.
7596 if Field_Name /= Name_uTag
7598 Field_Name /= Name_uController
7600 Evolve_Or_Else (Cond,
7603 Make_Selected_Component (Loc,
7604 Prefix => Make_Identifier (Loc, Name_X),
7606 Make_Identifier (Loc, Field_Name)),
7609 Make_Selected_Component (Loc,
7610 Prefix => Make_Identifier (Loc, Name_Y),
7612 Make_Identifier (Loc, Field_Name))));
7615 Next_Non_Pragma (C);
7619 return Make_Null_Statement (Loc);
7623 Make_Implicit_If_Statement (E,
7625 Then_Statements => New_List (
7626 Make_Simple_Return_Statement (Loc,
7627 Expression => New_Occurrence_Of (Standard_False, Loc))));
7632 -------------------------------
7633 -- Make_Null_Procedure_Specs --
7634 -------------------------------
7636 procedure Make_Null_Procedure_Specs
7637 (Tag_Typ : Entity_Id;
7638 Decl_List : out List_Id)
7640 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7643 Formal_List : List_Id;
7644 New_Param_Spec : Node_Id;
7645 Parent_Subp : Entity_Id;
7646 Prim_Elmt : Elmt_Id;
7647 Proc_Decl : Node_Id;
7650 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean;
7651 -- Returns True if E is a null procedure that is an interface primitive
7653 ---------------------------------
7654 -- Is_Null_Interface_Primitive --
7655 ---------------------------------
7657 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
7659 return Comes_From_Source (E)
7660 and then Is_Dispatching_Operation (E)
7661 and then Ekind (E) = E_Procedure
7662 and then Null_Present (Parent (E))
7663 and then Is_Interface (Find_Dispatching_Type (E));
7664 end Is_Null_Interface_Primitive;
7666 -- Start of processing for Make_Null_Procedure_Specs
7669 Decl_List := New_List;
7670 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7671 while Present (Prim_Elmt) loop
7672 Subp := Node (Prim_Elmt);
7674 -- If a null procedure inherited from an interface has not been
7675 -- overridden, then we build a null procedure declaration to
7676 -- override the inherited procedure.
7678 Parent_Subp := Alias (Subp);
7680 if Present (Parent_Subp)
7681 and then Is_Null_Interface_Primitive (Parent_Subp)
7683 Formal_List := No_List;
7684 Formal := First_Formal (Subp);
7686 if Present (Formal) then
7687 Formal_List := New_List;
7689 while Present (Formal) loop
7691 -- Copy the parameter spec including default expressions
7694 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
7696 -- Generate a new defining identifier for the new formal.
7697 -- required because New_Copy_Tree does not duplicate
7698 -- semantic fields (except itypes).
7700 Set_Defining_Identifier (New_Param_Spec,
7701 Make_Defining_Identifier (Sloc (Formal),
7702 Chars => Chars (Formal)));
7704 -- For controlling arguments we must change their
7705 -- parameter type to reference the tagged type (instead
7706 -- of the interface type)
7708 if Is_Controlling_Formal (Formal) then
7709 if Nkind (Parameter_Type (Parent (Formal)))
7712 Set_Parameter_Type (New_Param_Spec,
7713 New_Occurrence_Of (Tag_Typ, Loc));
7716 (Nkind (Parameter_Type (Parent (Formal)))
7717 = N_Access_Definition);
7718 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
7719 New_Occurrence_Of (Tag_Typ, Loc));
7723 Append (New_Param_Spec, Formal_List);
7725 Next_Formal (Formal);
7730 Make_Subprogram_Declaration (Loc,
7731 Make_Procedure_Specification (Loc,
7732 Defining_Unit_Name =>
7733 Make_Defining_Identifier (Loc, Chars (Subp)),
7734 Parameter_Specifications => Formal_List,
7735 Null_Present => True));
7736 Append_To (Decl_List, Proc_Decl);
7737 Analyze (Proc_Decl);
7740 Next_Elmt (Prim_Elmt);
7742 end Make_Null_Procedure_Specs;
7744 -------------------------------------
7745 -- Make_Predefined_Primitive_Specs --
7746 -------------------------------------
7748 procedure Make_Predefined_Primitive_Specs
7749 (Tag_Typ : Entity_Id;
7750 Predef_List : out List_Id;
7751 Renamed_Eq : out Entity_Id)
7753 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7754 Res : constant List_Id := New_List;
7756 Eq_Needed : Boolean;
7758 Eq_Name : Name_Id := Name_Op_Eq;
7760 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
7761 -- Returns true if Prim is a renaming of an unresolved predefined
7762 -- equality operation.
7764 -------------------------------
7765 -- Is_Predefined_Eq_Renaming --
7766 -------------------------------
7768 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
7770 return Chars (Prim) /= Name_Op_Eq
7771 and then Present (Alias (Prim))
7772 and then Comes_From_Source (Prim)
7773 and then Is_Intrinsic_Subprogram (Alias (Prim))
7774 and then Chars (Alias (Prim)) = Name_Op_Eq;
7775 end Is_Predefined_Eq_Renaming;
7777 -- Start of processing for Make_Predefined_Primitive_Specs
7780 Renamed_Eq := Empty;
7784 Append_To (Res, Predef_Spec_Or_Body (Loc,
7787 Profile => New_List (
7788 Make_Parameter_Specification (Loc,
7789 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7790 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7792 Ret_Type => Standard_Long_Long_Integer));
7794 -- Spec of _Alignment
7796 Append_To (Res, Predef_Spec_Or_Body (Loc,
7798 Name => Name_uAlignment,
7799 Profile => New_List (
7800 Make_Parameter_Specification (Loc,
7801 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7802 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7804 Ret_Type => Standard_Integer));
7806 -- Specs for dispatching stream attributes
7809 Stream_Op_TSS_Names :
7810 constant array (Integer range <>) of TSS_Name_Type :=
7817 for Op in Stream_Op_TSS_Names'Range loop
7818 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
7820 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
7821 Stream_Op_TSS_Names (Op)));
7826 -- Spec of "=" is expanded if the type is not limited and if a
7827 -- user defined "=" was not already declared for the non-full
7828 -- view of a private extension
7830 if not Is_Limited_Type (Tag_Typ) then
7832 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7833 while Present (Prim) loop
7835 -- If a primitive is encountered that renames the predefined
7836 -- equality operator before reaching any explicit equality
7837 -- primitive, then we still need to create a predefined
7838 -- equality function, because calls to it can occur via
7839 -- the renaming. A new name is created for the equality
7840 -- to avoid conflicting with any user-defined equality.
7841 -- (Note that this doesn't account for renamings of
7842 -- equality nested within subpackages???)
7844 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7845 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
7847 -- User-defined equality
7849 elsif Chars (Node (Prim)) = Name_Op_Eq
7850 and then Etype (First_Formal (Node (Prim))) =
7851 Etype (Next_Formal (First_Formal (Node (Prim))))
7852 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
7854 if No (Alias (Node (Prim)))
7855 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
7856 N_Subprogram_Renaming_Declaration
7861 -- If the parent is not an interface type and has an abstract
7862 -- equality function, the inherited equality is abstract as
7863 -- well, and no body can be created for it.
7865 elsif not Is_Interface (Etype (Tag_Typ))
7866 and then Present (Alias (Node (Prim)))
7867 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
7872 -- If the type has an equality function corresponding with
7873 -- a primitive defined in an interface type, the inherited
7874 -- equality is abstract as well, and no body can be created
7877 elsif Present (Alias (Node (Prim)))
7878 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
7881 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
7891 -- If a renaming of predefined equality was found but there was no
7892 -- user-defined equality (so Eq_Needed is still true), then set the
7893 -- name back to Name_Op_Eq. But in the case where a user-defined
7894 -- equality was located after such a renaming, then the predefined
7895 -- equality function is still needed, so Eq_Needed must be set back
7898 if Eq_Name /= Name_Op_Eq then
7900 Eq_Name := Name_Op_Eq;
7907 Eq_Spec := Predef_Spec_Or_Body (Loc,
7910 Profile => New_List (
7911 Make_Parameter_Specification (Loc,
7912 Defining_Identifier =>
7913 Make_Defining_Identifier (Loc, Name_X),
7914 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7915 Make_Parameter_Specification (Loc,
7916 Defining_Identifier =>
7917 Make_Defining_Identifier (Loc, Name_Y),
7918 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7919 Ret_Type => Standard_Boolean);
7920 Append_To (Res, Eq_Spec);
7922 if Eq_Name /= Name_Op_Eq then
7923 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
7925 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7926 while Present (Prim) loop
7928 -- Any renamings of equality that appeared before an
7929 -- overriding equality must be updated to refer to the
7930 -- entity for the predefined equality, otherwise calls via
7931 -- the renaming would get incorrectly resolved to call the
7932 -- user-defined equality function.
7934 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7935 Set_Alias (Node (Prim), Renamed_Eq);
7937 -- Exit upon encountering a user-defined equality
7939 elsif Chars (Node (Prim)) = Name_Op_Eq
7940 and then No (Alias (Node (Prim)))
7950 -- Spec for dispatching assignment
7952 Append_To (Res, Predef_Spec_Or_Body (Loc,
7954 Name => Name_uAssign,
7955 Profile => New_List (
7956 Make_Parameter_Specification (Loc,
7957 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7958 Out_Present => True,
7959 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7961 Make_Parameter_Specification (Loc,
7962 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
7963 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
7966 -- Ada 2005: Generate declarations for the following primitive
7967 -- operations for limited interfaces and synchronized types that
7968 -- implement a limited interface.
7970 -- Disp_Asynchronous_Select
7971 -- Disp_Conditional_Select
7972 -- Disp_Get_Prim_Op_Kind
7975 -- Disp_Timed_Select
7977 -- These operations cannot be implemented on VM targets, so we simply
7978 -- disable their generation in this case. Disable the generation of
7979 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
7981 if Ada_Version >= Ada_05
7982 and then Tagged_Type_Expansion
7983 and then not Restriction_Active (No_Dispatching_Calls)
7984 and then not Restriction_Active (No_Select_Statements)
7985 and then RTE_Available (RE_Select_Specific_Data)
7987 -- These primitives are defined abstract in interface types
7989 if Is_Interface (Tag_Typ)
7990 and then Is_Limited_Record (Tag_Typ)
7993 Make_Abstract_Subprogram_Declaration (Loc,
7995 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
7998 Make_Abstract_Subprogram_Declaration (Loc,
8000 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8003 Make_Abstract_Subprogram_Declaration (Loc,
8005 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8008 Make_Abstract_Subprogram_Declaration (Loc,
8010 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8013 Make_Abstract_Subprogram_Declaration (Loc,
8015 Make_Disp_Requeue_Spec (Tag_Typ)));
8018 Make_Abstract_Subprogram_Declaration (Loc,
8020 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8022 -- If the ancestor is an interface type we declare non-abstract
8023 -- primitives to override the abstract primitives of the interface
8026 elsif (not Is_Interface (Tag_Typ)
8027 and then Is_Interface (Etype (Tag_Typ))
8028 and then Is_Limited_Record (Etype (Tag_Typ)))
8030 (Is_Concurrent_Record_Type (Tag_Typ)
8031 and then Has_Interfaces (Tag_Typ))
8034 Make_Subprogram_Declaration (Loc,
8036 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8039 Make_Subprogram_Declaration (Loc,
8041 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8044 Make_Subprogram_Declaration (Loc,
8046 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8049 Make_Subprogram_Declaration (Loc,
8051 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8054 Make_Subprogram_Declaration (Loc,
8056 Make_Disp_Requeue_Spec (Tag_Typ)));
8059 Make_Subprogram_Declaration (Loc,
8061 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8065 -- Specs for finalization actions that may be required in case a future
8066 -- extension contain a controlled element. We generate those only for
8067 -- root tagged types where they will get dummy bodies or when the type
8068 -- has controlled components and their body must be generated. It is
8069 -- also impossible to provide those for tagged types defined within
8070 -- s-finimp since it would involve circularity problems
8072 if In_Finalization_Root (Tag_Typ) then
8075 -- We also skip these if finalization is not available
8077 elsif Restriction_Active (No_Finalization) then
8080 elsif Etype (Tag_Typ) = Tag_Typ
8081 or else Needs_Finalization (Tag_Typ)
8083 -- Ada 2005 (AI-251): We must also generate these subprograms if
8084 -- the immediate ancestor is an interface to ensure the correct
8085 -- initialization of its dispatch table.
8087 or else (not Is_Interface (Tag_Typ)
8088 and then Is_Interface (Etype (Tag_Typ)))
8090 -- Ada 205 (AI-251): We must also generate these subprograms if
8091 -- the parent of an nonlimited interface is a limited interface
8093 or else (Is_Interface (Tag_Typ)
8094 and then not Is_Limited_Interface (Tag_Typ)
8095 and then Is_Limited_Interface (Etype (Tag_Typ)))
8097 if not Is_Limited_Type (Tag_Typ) then
8099 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
8102 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
8106 end Make_Predefined_Primitive_Specs;
8108 ---------------------------------
8109 -- Needs_Simple_Initialization --
8110 ---------------------------------
8112 function Needs_Simple_Initialization (T : Entity_Id) return Boolean is
8114 -- Check for private type, in which case test applies to the underlying
8115 -- type of the private type.
8117 if Is_Private_Type (T) then
8119 RT : constant Entity_Id := Underlying_Type (T);
8122 if Present (RT) then
8123 return Needs_Simple_Initialization (RT);
8129 -- Cases needing simple initialization are access types, and, if pragma
8130 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
8133 elsif Is_Access_Type (T)
8134 or else (Init_Or_Norm_Scalars and then (Is_Scalar_Type (T)))
8138 -- If Initialize/Normalize_Scalars is in effect, string objects also
8139 -- need initialization, unless they are created in the course of
8140 -- expanding an aggregate (since in the latter case they will be
8141 -- filled with appropriate initializing values before they are used).
8143 elsif Init_Or_Norm_Scalars
8145 (Root_Type (T) = Standard_String
8146 or else Root_Type (T) = Standard_Wide_String
8147 or else Root_Type (T) = Standard_Wide_Wide_String)
8150 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
8157 end Needs_Simple_Initialization;
8159 ----------------------
8160 -- Predef_Deep_Spec --
8161 ----------------------
8163 function Predef_Deep_Spec
8165 Tag_Typ : Entity_Id;
8166 Name : TSS_Name_Type;
8167 For_Body : Boolean := False) return Node_Id
8173 if Name = TSS_Deep_Finalize then
8175 Type_B := Standard_Boolean;
8179 Make_Parameter_Specification (Loc,
8180 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
8182 Out_Present => True,
8184 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
8185 Type_B := Standard_Short_Short_Integer;
8189 Make_Parameter_Specification (Loc,
8190 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8192 Out_Present => True,
8193 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
8196 Make_Parameter_Specification (Loc,
8197 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
8198 Parameter_Type => New_Reference_To (Type_B, Loc)));
8200 return Predef_Spec_Or_Body (Loc,
8201 Name => Make_TSS_Name (Tag_Typ, Name),
8204 For_Body => For_Body);
8207 when RE_Not_Available =>
8209 end Predef_Deep_Spec;
8211 -------------------------
8212 -- Predef_Spec_Or_Body --
8213 -------------------------
8215 function Predef_Spec_Or_Body
8217 Tag_Typ : Entity_Id;
8220 Ret_Type : Entity_Id := Empty;
8221 For_Body : Boolean := False) return Node_Id
8223 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
8227 Set_Is_Public (Id, Is_Public (Tag_Typ));
8229 -- The internal flag is set to mark these declarations because they have
8230 -- specific properties. First, they are primitives even if they are not
8231 -- defined in the type scope (the freezing point is not necessarily in
8232 -- the same scope). Second, the predefined equality can be overridden by
8233 -- a user-defined equality, no body will be generated in this case.
8235 Set_Is_Internal (Id);
8237 if not Debug_Generated_Code then
8238 Set_Debug_Info_Off (Id);
8241 if No (Ret_Type) then
8243 Make_Procedure_Specification (Loc,
8244 Defining_Unit_Name => Id,
8245 Parameter_Specifications => Profile);
8248 Make_Function_Specification (Loc,
8249 Defining_Unit_Name => Id,
8250 Parameter_Specifications => Profile,
8251 Result_Definition =>
8252 New_Reference_To (Ret_Type, Loc));
8255 if Is_Interface (Tag_Typ) then
8256 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8258 -- If body case, return empty subprogram body. Note that this is ill-
8259 -- formed, because there is not even a null statement, and certainly not
8260 -- a return in the function case. The caller is expected to do surgery
8261 -- on the body to add the appropriate stuff.
8264 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8266 -- For the case of an Input attribute predefined for an abstract type,
8267 -- generate an abstract specification. This will never be called, but we
8268 -- need the slot allocated in the dispatching table so that attributes
8269 -- typ'Class'Input and typ'Class'Output will work properly.
8271 elsif Is_TSS (Name, TSS_Stream_Input)
8272 and then Is_Abstract_Type (Tag_Typ)
8274 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8276 -- Normal spec case, where we return a subprogram declaration
8279 return Make_Subprogram_Declaration (Loc, Spec);
8281 end Predef_Spec_Or_Body;
8283 -----------------------------
8284 -- Predef_Stream_Attr_Spec --
8285 -----------------------------
8287 function Predef_Stream_Attr_Spec
8289 Tag_Typ : Entity_Id;
8290 Name : TSS_Name_Type;
8291 For_Body : Boolean := False) return Node_Id
8293 Ret_Type : Entity_Id;
8296 if Name = TSS_Stream_Input then
8297 Ret_Type := Tag_Typ;
8302 return Predef_Spec_Or_Body (Loc,
8303 Name => Make_TSS_Name (Tag_Typ, Name),
8305 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8306 Ret_Type => Ret_Type,
8307 For_Body => For_Body);
8308 end Predef_Stream_Attr_Spec;
8310 ---------------------------------
8311 -- Predefined_Primitive_Bodies --
8312 ---------------------------------
8314 function Predefined_Primitive_Bodies
8315 (Tag_Typ : Entity_Id;
8316 Renamed_Eq : Entity_Id) return List_Id
8318 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8319 Res : constant List_Id := New_List;
8322 Eq_Needed : Boolean;
8326 pragma Warnings (Off, Ent);
8329 pragma Assert (not Is_Interface (Tag_Typ));
8331 -- See if we have a predefined "=" operator
8333 if Present (Renamed_Eq) then
8335 Eq_Name := Chars (Renamed_Eq);
8337 -- If the parent is an interface type then it has defined all the
8338 -- predefined primitives abstract and we need to check if the type
8339 -- has some user defined "=" function to avoid generating it.
8341 elsif Is_Interface (Etype (Tag_Typ)) then
8343 Eq_Name := Name_Op_Eq;
8345 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8346 while Present (Prim) loop
8347 if Chars (Node (Prim)) = Name_Op_Eq
8348 and then not Is_Internal (Node (Prim))
8362 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8363 while Present (Prim) loop
8364 if Chars (Node (Prim)) = Name_Op_Eq
8365 and then Is_Internal (Node (Prim))
8368 Eq_Name := Name_Op_Eq;
8376 -- Body of _Alignment
8378 Decl := Predef_Spec_Or_Body (Loc,
8380 Name => Name_uAlignment,
8381 Profile => New_List (
8382 Make_Parameter_Specification (Loc,
8383 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8384 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8386 Ret_Type => Standard_Integer,
8389 Set_Handled_Statement_Sequence (Decl,
8390 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8391 Make_Simple_Return_Statement (Loc,
8393 Make_Attribute_Reference (Loc,
8394 Prefix => Make_Identifier (Loc, Name_X),
8395 Attribute_Name => Name_Alignment)))));
8397 Append_To (Res, Decl);
8401 Decl := Predef_Spec_Or_Body (Loc,
8404 Profile => New_List (
8405 Make_Parameter_Specification (Loc,
8406 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8407 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8409 Ret_Type => Standard_Long_Long_Integer,
8412 Set_Handled_Statement_Sequence (Decl,
8413 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8414 Make_Simple_Return_Statement (Loc,
8416 Make_Attribute_Reference (Loc,
8417 Prefix => Make_Identifier (Loc, Name_X),
8418 Attribute_Name => Name_Size)))));
8420 Append_To (Res, Decl);
8422 -- Bodies for Dispatching stream IO routines. We need these only for
8423 -- non-limited types (in the limited case there is no dispatching).
8424 -- We also skip them if dispatching or finalization are not available.
8426 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8427 and then No (TSS (Tag_Typ, TSS_Stream_Read))
8429 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8430 Append_To (Res, Decl);
8433 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8434 and then No (TSS (Tag_Typ, TSS_Stream_Write))
8436 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8437 Append_To (Res, Decl);
8440 -- Skip body of _Input for the abstract case, since the corresponding
8441 -- spec is abstract (see Predef_Spec_Or_Body).
8443 if not Is_Abstract_Type (Tag_Typ)
8444 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8445 and then No (TSS (Tag_Typ, TSS_Stream_Input))
8447 Build_Record_Or_Elementary_Input_Function
8448 (Loc, Tag_Typ, Decl, Ent);
8449 Append_To (Res, Decl);
8452 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8453 and then No (TSS (Tag_Typ, TSS_Stream_Output))
8455 Build_Record_Or_Elementary_Output_Procedure
8456 (Loc, Tag_Typ, Decl, Ent);
8457 Append_To (Res, Decl);
8460 -- Ada 2005: Generate bodies for the following primitive operations for
8461 -- limited interfaces and synchronized types that implement a limited
8464 -- disp_asynchronous_select
8465 -- disp_conditional_select
8466 -- disp_get_prim_op_kind
8468 -- disp_timed_select
8470 -- The interface versions will have null bodies
8472 -- These operations cannot be implemented on VM targets, so we simply
8473 -- disable their generation in this case. Disable the generation of
8474 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
8476 if Ada_Version >= Ada_05
8477 and then Tagged_Type_Expansion
8478 and then not Is_Interface (Tag_Typ)
8480 ((Is_Interface (Etype (Tag_Typ))
8481 and then Is_Limited_Record (Etype (Tag_Typ)))
8482 or else (Is_Concurrent_Record_Type (Tag_Typ)
8483 and then Has_Interfaces (Tag_Typ)))
8484 and then not Restriction_Active (No_Dispatching_Calls)
8485 and then not Restriction_Active (No_Select_Statements)
8486 and then RTE_Available (RE_Select_Specific_Data)
8488 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8489 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
8490 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
8491 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
8492 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
8493 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
8496 if not Is_Limited_Type (Tag_Typ)
8497 and then not Is_Interface (Tag_Typ)
8499 -- Body for equality
8503 Predef_Spec_Or_Body (Loc,
8506 Profile => New_List (
8507 Make_Parameter_Specification (Loc,
8508 Defining_Identifier =>
8509 Make_Defining_Identifier (Loc, Name_X),
8510 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8512 Make_Parameter_Specification (Loc,
8513 Defining_Identifier =>
8514 Make_Defining_Identifier (Loc, Name_Y),
8515 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8517 Ret_Type => Standard_Boolean,
8521 Def : constant Node_Id := Parent (Tag_Typ);
8522 Stmts : constant List_Id := New_List;
8523 Variant_Case : Boolean := Has_Discriminants (Tag_Typ);
8524 Comps : Node_Id := Empty;
8525 Typ_Def : Node_Id := Type_Definition (Def);
8528 if Variant_Case then
8529 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8530 Typ_Def := Record_Extension_Part (Typ_Def);
8533 if Present (Typ_Def) then
8534 Comps := Component_List (Typ_Def);
8537 Variant_Case := Present (Comps)
8538 and then Present (Variant_Part (Comps));
8541 if Variant_Case then
8543 Make_Eq_If (Tag_Typ, Discriminant_Specifications (Def)));
8544 Append_List_To (Stmts, Make_Eq_Case (Tag_Typ, Comps));
8546 Make_Simple_Return_Statement (Loc,
8547 Expression => New_Reference_To (Standard_True, Loc)));
8551 Make_Simple_Return_Statement (Loc,
8553 Expand_Record_Equality (Tag_Typ,
8555 Lhs => Make_Identifier (Loc, Name_X),
8556 Rhs => Make_Identifier (Loc, Name_Y),
8557 Bodies => Declarations (Decl))));
8560 Set_Handled_Statement_Sequence (Decl,
8561 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8563 Append_To (Res, Decl);
8566 -- Body for dispatching assignment
8569 Predef_Spec_Or_Body (Loc,
8571 Name => Name_uAssign,
8572 Profile => New_List (
8573 Make_Parameter_Specification (Loc,
8574 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8575 Out_Present => True,
8576 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8578 Make_Parameter_Specification (Loc,
8579 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8580 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8583 Set_Handled_Statement_Sequence (Decl,
8584 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8585 Make_Assignment_Statement (Loc,
8586 Name => Make_Identifier (Loc, Name_X),
8587 Expression => Make_Identifier (Loc, Name_Y)))));
8589 Append_To (Res, Decl);
8592 -- Generate dummy bodies for finalization actions of types that have
8593 -- no controlled components.
8595 -- Skip this processing if we are in the finalization routine in the
8596 -- runtime itself, otherwise we get hopelessly circularly confused!
8598 if In_Finalization_Root (Tag_Typ) then
8601 -- Skip this if finalization is not available
8603 elsif Restriction_Active (No_Finalization) then
8606 elsif (Etype (Tag_Typ) = Tag_Typ
8607 or else Is_Controlled (Tag_Typ)
8609 -- Ada 2005 (AI-251): We must also generate these subprograms
8610 -- if the immediate ancestor of Tag_Typ is an interface to
8611 -- ensure the correct initialization of its dispatch table.
8613 or else (not Is_Interface (Tag_Typ)
8615 Is_Interface (Etype (Tag_Typ))))
8616 and then not Has_Controlled_Component (Tag_Typ)
8618 if not Is_Limited_Type (Tag_Typ) then
8619 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8621 if Is_Controlled (Tag_Typ) then
8622 Set_Handled_Statement_Sequence (Decl,
8623 Make_Handled_Sequence_Of_Statements (Loc,
8625 Ref => Make_Identifier (Loc, Name_V),
8627 Flist_Ref => Make_Identifier (Loc, Name_L),
8628 With_Attach => Make_Identifier (Loc, Name_B))));
8631 Set_Handled_Statement_Sequence (Decl,
8632 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8633 Make_Null_Statement (Loc))));
8636 Append_To (Res, Decl);
8639 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
8641 if Is_Controlled (Tag_Typ) then
8642 Set_Handled_Statement_Sequence (Decl,
8643 Make_Handled_Sequence_Of_Statements (Loc,
8645 Ref => Make_Identifier (Loc, Name_V),
8647 With_Detach => Make_Identifier (Loc, Name_B))));
8650 Set_Handled_Statement_Sequence (Decl,
8651 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8652 Make_Null_Statement (Loc))));
8655 Append_To (Res, Decl);
8659 end Predefined_Primitive_Bodies;
8661 ---------------------------------
8662 -- Predefined_Primitive_Freeze --
8663 ---------------------------------
8665 function Predefined_Primitive_Freeze
8666 (Tag_Typ : Entity_Id) return List_Id
8668 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8669 Res : constant List_Id := New_List;
8674 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8675 while Present (Prim) loop
8676 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8677 Frnodes := Freeze_Entity (Node (Prim), Loc);
8679 if Present (Frnodes) then
8680 Append_List_To (Res, Frnodes);
8688 end Predefined_Primitive_Freeze;
8690 -------------------------
8691 -- Stream_Operation_OK --
8692 -------------------------
8694 function Stream_Operation_OK
8696 Operation : TSS_Name_Type) return Boolean
8698 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
8701 -- Special case of a limited type extension: a default implementation
8702 -- of the stream attributes Read or Write exists if that attribute
8703 -- has been specified or is available for an ancestor type; a default
8704 -- implementation of the attribute Output (resp. Input) exists if the
8705 -- attribute has been specified or Write (resp. Read) is available for
8706 -- an ancestor type. The last condition only applies under Ada 2005.
8708 if Is_Limited_Type (Typ)
8709 and then Is_Tagged_Type (Typ)
8711 if Operation = TSS_Stream_Read then
8712 Has_Predefined_Or_Specified_Stream_Attribute :=
8713 Has_Specified_Stream_Read (Typ);
8715 elsif Operation = TSS_Stream_Write then
8716 Has_Predefined_Or_Specified_Stream_Attribute :=
8717 Has_Specified_Stream_Write (Typ);
8719 elsif Operation = TSS_Stream_Input then
8720 Has_Predefined_Or_Specified_Stream_Attribute :=
8721 Has_Specified_Stream_Input (Typ)
8723 (Ada_Version >= Ada_05
8724 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
8726 elsif Operation = TSS_Stream_Output then
8727 Has_Predefined_Or_Specified_Stream_Attribute :=
8728 Has_Specified_Stream_Output (Typ)
8730 (Ada_Version >= Ada_05
8731 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
8734 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
8736 if not Has_Predefined_Or_Specified_Stream_Attribute
8737 and then Is_Derived_Type (Typ)
8738 and then (Operation = TSS_Stream_Read
8739 or else Operation = TSS_Stream_Write)
8741 Has_Predefined_Or_Specified_Stream_Attribute :=
8743 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
8747 -- If the type is not limited, or else is limited but the attribute is
8748 -- explicitly specified or is predefined for the type, then return True,
8749 -- unless other conditions prevail, such as restrictions prohibiting
8750 -- streams or dispatching operations. We also return True for limited
8751 -- interfaces, because they may be extended by nonlimited types and
8752 -- permit inheritance in this case (addresses cases where an abstract
8753 -- extension doesn't get 'Input declared, as per comments below, but
8754 -- 'Class'Input must still be allowed). Note that attempts to apply
8755 -- stream attributes to a limited interface or its class-wide type
8756 -- (or limited extensions thereof) will still get properly rejected
8757 -- by Check_Stream_Attribute.
8759 -- We exclude the Input operation from being a predefined subprogram in
8760 -- the case where the associated type is an abstract extension, because
8761 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
8762 -- we don't want an abstract version created because types derived from
8763 -- the abstract type may not even have Input available (for example if
8764 -- derived from a private view of the abstract type that doesn't have
8765 -- a visible Input), but a VM such as .NET or the Java VM can treat the
8766 -- operation as inherited anyway, and we don't want an abstract function
8767 -- to be (implicitly) inherited in that case because it can lead to a VM
8770 return (not Is_Limited_Type (Typ)
8771 or else Is_Interface (Typ)
8772 or else Has_Predefined_Or_Specified_Stream_Attribute)
8773 and then (Operation /= TSS_Stream_Input
8774 or else not Is_Abstract_Type (Typ)
8775 or else not Is_Derived_Type (Typ))
8776 and then not Has_Unknown_Discriminants (Typ)
8777 and then not (Is_Interface (Typ)
8778 and then (Is_Task_Interface (Typ)
8779 or else Is_Protected_Interface (Typ)
8780 or else Is_Synchronized_Interface (Typ)))
8781 and then not Restriction_Active (No_Streams)
8782 and then not Restriction_Active (No_Dispatch)
8783 and then not No_Run_Time_Mode
8784 and then RTE_Available (RE_Tag)
8785 and then RTE_Available (RE_Root_Stream_Type);
8786 end Stream_Operation_OK;