1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Ch11; use Exp_Ch11;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Smem; use Exp_Smem;
41 with Exp_Strm; use Exp_Strm;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Nlists; use Nlists;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Attr; use Sem_Attr;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Stand; use Stand;
64 with Snames; use Snames;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Validsw; use Validsw;
70 package body Exp_Ch3 is
72 -----------------------
73 -- Local Subprograms --
74 -----------------------
76 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id;
77 -- Add the declaration of a finalization list to the freeze actions for
78 -- Def_Id, and return its defining identifier.
80 procedure Adjust_Discriminants (Rtype : Entity_Id);
81 -- This is used when freezing a record type. It attempts to construct
82 -- more restrictive subtypes for discriminants so that the max size of
83 -- the record can be calculated more accurately. See the body of this
84 -- procedure for details.
86 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
87 -- Build initialization procedure for given array type. Nod is a node
88 -- used for attachment of any actions required in its construction.
89 -- It also supplies the source location used for the procedure.
91 function Build_Discriminant_Formals
93 Use_Dl : Boolean) return List_Id;
94 -- This function uses the discriminants of a type to build a list of
95 -- formal parameters, used in the following function. If the flag Use_Dl
96 -- is set, the list is built using the already defined discriminals
97 -- of the type. Otherwise new identifiers are created, with the source
98 -- names of the discriminants.
100 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
101 -- This function builds a static aggregate that can serve as the initial
102 -- value for an array type whose bounds are static, and whose component
103 -- type is a composite type that has a static equivalent aggregate.
104 -- The equivalent array aggregate is used both for object initialization
105 -- and for component initialization, when used in the following function.
107 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
108 -- This function builds a static aggregate that can serve as the initial
109 -- value for a record type whose components are scalar and initialized
110 -- with compile-time values, or arrays with similar initialization or
111 -- defaults. When possible, initialization of an object of the type can
112 -- be achieved by using a copy of the aggregate as an initial value, thus
113 -- removing the implicit call that would otherwise constitute elaboration
116 function Build_Master_Renaming
118 T : Entity_Id) return Entity_Id;
119 -- If the designated type of an access type is a task type or contains
120 -- tasks, we make sure that a _Master variable is declared in the current
121 -- scope, and then declare a renaming for it:
123 -- atypeM : Master_Id renames _Master;
125 -- where atyp is the name of the access type. This declaration is used when
126 -- an allocator for the access type is expanded. The node is the full
127 -- declaration of the designated type that contains tasks. The renaming
128 -- declaration is inserted before N, and after the Master declaration.
130 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id);
131 -- Build record initialization procedure. N is the type declaration
132 -- node, and Pe is the corresponding entity for the record type.
134 procedure Build_Slice_Assignment (Typ : Entity_Id);
135 -- Build assignment procedure for one-dimensional arrays of controlled
136 -- types. Other array and slice assignments are expanded in-line, but
137 -- the code expansion for controlled components (when control actions
138 -- are active) can lead to very large blocks that GCC3 handles poorly.
140 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
141 -- Create An Equality function for the non-tagged variant record 'Typ'
142 -- and attach it to the TSS list
144 procedure Check_Stream_Attributes (Typ : Entity_Id);
145 -- Check that if a limited extension has a parent with user-defined stream
146 -- attributes, and does not itself have user-defined stream-attributes,
147 -- then any limited component of the extension also has the corresponding
148 -- user-defined stream attributes.
150 procedure Clean_Task_Names
152 Proc_Id : Entity_Id);
153 -- If an initialization procedure includes calls to generate names
154 -- for task subcomponents, indicate that secondary stack cleanup is
155 -- needed after an initialization. Typ is the component type, and Proc_Id
156 -- the initialization procedure for the enclosing composite type.
158 procedure Expand_Tagged_Root (T : Entity_Id);
159 -- Add a field _Tag at the beginning of the record. This field carries
160 -- the value of the access to the Dispatch table. This procedure is only
161 -- called on root type, the _Tag field being inherited by the descendants.
163 procedure Expand_Record_Controller (T : Entity_Id);
164 -- T must be a record type that Has_Controlled_Component. Add a field
165 -- _controller of type Record_Controller or Limited_Record_Controller
168 procedure Freeze_Array_Type (N : Node_Id);
169 -- Freeze an array type. Deals with building the initialization procedure,
170 -- creating the packed array type for a packed array and also with the
171 -- creation of the controlling procedures for the controlled case. The
172 -- argument N is the N_Freeze_Entity node for the type.
174 procedure Freeze_Enumeration_Type (N : Node_Id);
175 -- Freeze enumeration type with non-standard representation. Builds the
176 -- array and function needed to convert between enumeration pos and
177 -- enumeration representation values. N is the N_Freeze_Entity node
180 procedure Freeze_Record_Type (N : Node_Id);
181 -- Freeze record type. Builds all necessary discriminant checking
182 -- and other ancillary functions, and builds dispatch tables where
183 -- needed. The argument N is the N_Freeze_Entity node. This processing
184 -- applies only to E_Record_Type entities, not to class wide types,
185 -- record subtypes, or private types.
187 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
188 -- Treat user-defined stream operations as renaming_as_body if the
189 -- subprogram they rename is not frozen when the type is frozen.
191 procedure Initialization_Warning (E : Entity_Id);
192 -- If static elaboration of the package is requested, indicate
193 -- when a type does meet the conditions for static initialization. If
194 -- E is a type, it has components that have no static initialization.
195 -- if E is an entity, its initial expression is not compile-time known.
197 function Init_Formals (Typ : Entity_Id) return List_Id;
198 -- This function builds the list of formals for an initialization routine.
199 -- The first formal is always _Init with the given type. For task value
200 -- record types and types containing tasks, three additional formals are
203 -- _Master : Master_Id
204 -- _Chain : in out Activation_Chain
205 -- _Task_Name : String
207 -- The caller must append additional entries for discriminants if required.
209 function In_Runtime (E : Entity_Id) return Boolean;
210 -- Check if E is defined in the RTL (in a child of Ada or System). Used
211 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
213 function Is_Variable_Size_Record (E : Entity_Id) return Boolean;
214 -- Returns true if E has variable size components
216 function Make_Eq_Case
219 Discr : Entity_Id := Empty) return List_Id;
220 -- Building block for variant record equality. Defined to share the code
221 -- between the tagged and non-tagged case. Given a Component_List node CL,
222 -- it generates an 'if' followed by a 'case' statement that compares all
223 -- components of local temporaries named X and Y (that are declared as
224 -- formals at some upper level). E provides the Sloc to be used for the
225 -- generated code. Discr is used as the case statement switch in the case
226 -- of Unchecked_Union equality.
230 L : List_Id) return Node_Id;
231 -- Building block for variant record equality. Defined to share the code
232 -- between the tagged and non-tagged case. Given the list of components
233 -- (or discriminants) L, it generates a return statement that compares all
234 -- components of local temporaries named X and Y (that are declared as
235 -- formals at some upper level). E provides the Sloc to be used for the
238 procedure Make_Predefined_Primitive_Specs
239 (Tag_Typ : Entity_Id;
240 Predef_List : out List_Id;
241 Renamed_Eq : out Node_Id);
242 -- Create a list with the specs of the predefined primitive operations.
243 -- The following entries are present for all tagged types, and provide
244 -- the results of the corresponding attribute applied to the object.
245 -- Dispatching is required in general, since the result of the attribute
246 -- will vary with the actual object subtype.
248 -- _alignment provides result of 'Alignment attribute
249 -- _size provides result of 'Size attribute
250 -- typSR provides result of 'Read attribute
251 -- typSW provides result of 'Write attribute
252 -- typSI provides result of 'Input attribute
253 -- typSO provides result of 'Output attribute
255 -- The following entries are additionally present for non-limited tagged
256 -- types, and implement additional dispatching operations for predefined
259 -- _equality implements "=" operator
260 -- _assign implements assignment operation
261 -- typDF implements deep finalization
262 -- typDA implements deep adjust
264 -- The latter two are empty procedures unless the type contains some
265 -- controlled components that require finalization actions (the deep
266 -- in the name refers to the fact that the action applies to components).
268 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
269 -- returns the value Empty, or else the defining unit name for the
270 -- predefined equality function in the case where the type has a primitive
271 -- operation that is a renaming of predefined equality (but only if there
272 -- is also an overriding user-defined equality function). The returned
273 -- Renamed_Eq will be passed to the corresponding parameter of
274 -- Predefined_Primitive_Bodies.
276 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
277 -- returns True if there are representation clauses for type T that are not
278 -- inherited. If the result is false, the init_proc and the discriminant
279 -- checking functions of the parent can be reused by a derived type.
281 procedure Make_Controlling_Function_Wrappers
282 (Tag_Typ : Entity_Id;
283 Decl_List : out List_Id;
284 Body_List : out List_Id);
285 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
286 -- associated with inherited functions with controlling results which
287 -- are not overridden. The body of each wrapper function consists solely
288 -- of a return statement whose expression is an extension aggregate
289 -- invoking the inherited subprogram's parent subprogram and extended
290 -- with a null association list.
292 procedure Make_Null_Procedure_Specs
293 (Tag_Typ : Entity_Id;
294 Decl_List : out List_Id);
295 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
296 -- null procedures inherited from an interface type that have not been
297 -- overridden. Only one null procedure will be created for a given set of
298 -- inherited null procedures with homographic profiles.
300 function Predef_Spec_Or_Body
305 Ret_Type : Entity_Id := Empty;
306 For_Body : Boolean := False) return Node_Id;
307 -- This function generates the appropriate expansion for a predefined
308 -- primitive operation specified by its name, parameter profile and
309 -- return type (Empty means this is a procedure). If For_Body is false,
310 -- then the returned node is a subprogram declaration. If For_Body is
311 -- true, then the returned node is a empty subprogram body containing
312 -- no declarations and no statements.
314 function Predef_Stream_Attr_Spec
317 Name : TSS_Name_Type;
318 For_Body : Boolean := False) return Node_Id;
319 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
320 -- input and output attribute whose specs are constructed in Exp_Strm.
322 function Predef_Deep_Spec
325 Name : TSS_Name_Type;
326 For_Body : Boolean := False) return Node_Id;
327 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
328 -- and _deep_finalize
330 function Predefined_Primitive_Bodies
331 (Tag_Typ : Entity_Id;
332 Renamed_Eq : Node_Id) return List_Id;
333 -- Create the bodies of the predefined primitives that are described in
334 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
335 -- the defining unit name of the type's predefined equality as returned
336 -- by Make_Predefined_Primitive_Specs.
338 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
339 -- Freeze entities of all predefined primitive operations. This is needed
340 -- because the bodies of these operations do not normally do any freezing.
342 function Stream_Operation_OK
344 Operation : TSS_Name_Type) return Boolean;
345 -- Check whether the named stream operation must be emitted for a given
346 -- type. The rules for inheritance of stream attributes by type extensions
347 -- are enforced by this function. Furthermore, various restrictions prevent
348 -- the generation of these operations, as a useful optimization or for
349 -- certification purposes.
351 ---------------------
352 -- Add_Final_Chain --
353 ---------------------
355 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id is
356 Loc : constant Source_Ptr := Sloc (Def_Id);
361 Make_Defining_Identifier (Loc,
362 New_External_Name (Chars (Def_Id), 'L'));
364 Append_Freeze_Action (Def_Id,
365 Make_Object_Declaration (Loc,
366 Defining_Identifier => Flist,
368 New_Reference_To (RTE (RE_List_Controller), Loc)));
373 --------------------------
374 -- Adjust_Discriminants --
375 --------------------------
377 -- This procedure attempts to define subtypes for discriminants that are
378 -- more restrictive than those declared. Such a replacement is possible if
379 -- we can demonstrate that values outside the restricted range would cause
380 -- constraint errors in any case. The advantage of restricting the
381 -- discriminant types in this way is that the maximum size of the variant
382 -- record can be calculated more conservatively.
384 -- An example of a situation in which we can perform this type of
385 -- restriction is the following:
387 -- subtype B is range 1 .. 10;
388 -- type Q is array (B range <>) of Integer;
390 -- type V (N : Natural) is record
394 -- In this situation, we can restrict the upper bound of N to 10, since
395 -- any larger value would cause a constraint error in any case.
397 -- There are many situations in which such restriction is possible, but
398 -- for now, we just look for cases like the above, where the component
399 -- in question is a one dimensional array whose upper bound is one of
400 -- the record discriminants. Also the component must not be part of
401 -- any variant part, since then the component does not always exist.
403 procedure Adjust_Discriminants (Rtype : Entity_Id) is
404 Loc : constant Source_Ptr := Sloc (Rtype);
421 Comp := First_Component (Rtype);
422 while Present (Comp) loop
424 -- If our parent is a variant, quit, we do not look at components
425 -- that are in variant parts, because they may not always exist.
427 P := Parent (Comp); -- component declaration
428 P := Parent (P); -- component list
430 exit when Nkind (Parent (P)) = N_Variant;
432 -- We are looking for a one dimensional array type
434 Ctyp := Etype (Comp);
436 if not Is_Array_Type (Ctyp)
437 or else Number_Dimensions (Ctyp) > 1
442 -- The lower bound must be constant, and the upper bound is a
443 -- discriminant (which is a discriminant of the current record).
445 Ityp := Etype (First_Index (Ctyp));
446 Lo := Type_Low_Bound (Ityp);
447 Hi := Type_High_Bound (Ityp);
449 if not Compile_Time_Known_Value (Lo)
450 or else Nkind (Hi) /= N_Identifier
451 or else No (Entity (Hi))
452 or else Ekind (Entity (Hi)) /= E_Discriminant
457 -- We have an array with appropriate bounds
459 Loval := Expr_Value (Lo);
460 Discr := Entity (Hi);
461 Dtyp := Etype (Discr);
463 -- See if the discriminant has a known upper bound
465 Dhi := Type_High_Bound (Dtyp);
467 if not Compile_Time_Known_Value (Dhi) then
471 Dhiv := Expr_Value (Dhi);
473 -- See if base type of component array has known upper bound
475 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
477 if not Compile_Time_Known_Value (Ahi) then
481 Ahiv := Expr_Value (Ahi);
483 -- The condition for doing the restriction is that the high bound
484 -- of the discriminant is greater than the low bound of the array,
485 -- and is also greater than the high bound of the base type index.
487 if Dhiv > Loval and then Dhiv > Ahiv then
489 -- We can reset the upper bound of the discriminant type to
490 -- whichever is larger, the low bound of the component, or
491 -- the high bound of the base type array index.
493 -- We build a subtype that is declared as
495 -- subtype Tnn is discr_type range discr_type'First .. max;
497 -- And insert this declaration into the tree. The type of the
498 -- discriminant is then reset to this more restricted subtype.
500 Tnn := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
502 Insert_Action (Declaration_Node (Rtype),
503 Make_Subtype_Declaration (Loc,
504 Defining_Identifier => Tnn,
505 Subtype_Indication =>
506 Make_Subtype_Indication (Loc,
507 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
509 Make_Range_Constraint (Loc,
513 Make_Attribute_Reference (Loc,
514 Attribute_Name => Name_First,
515 Prefix => New_Occurrence_Of (Dtyp, Loc)),
517 Make_Integer_Literal (Loc,
518 Intval => UI_Max (Loval, Ahiv)))))));
520 Set_Etype (Discr, Tnn);
524 Next_Component (Comp);
526 end Adjust_Discriminants;
528 ---------------------------
529 -- Build_Array_Init_Proc --
530 ---------------------------
532 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
533 Loc : constant Source_Ptr := Sloc (Nod);
534 Comp_Type : constant Entity_Id := Component_Type (A_Type);
535 Index_List : List_Id;
537 Body_Stmts : List_Id;
539 function Init_Component return List_Id;
540 -- Create one statement to initialize one array component, designated
541 -- by a full set of indices.
543 function Init_One_Dimension (N : Int) return List_Id;
544 -- Create loop to initialize one dimension of the array. The single
545 -- statement in the loop body initializes the inner dimensions if any,
546 -- or else the single component. Note that this procedure is called
547 -- recursively, with N being the dimension to be initialized. A call
548 -- with N greater than the number of dimensions simply generates the
549 -- component initialization, terminating the recursion.
555 function Init_Component return List_Id is
560 Make_Indexed_Component (Loc,
561 Prefix => Make_Identifier (Loc, Name_uInit),
562 Expressions => Index_List);
564 if Needs_Simple_Initialization (Comp_Type) then
565 Set_Assignment_OK (Comp);
567 Make_Assignment_Statement (Loc,
571 (Comp_Type, Loc, Component_Size (A_Type))));
574 Clean_Task_Names (Comp_Type, Proc_Id);
576 Build_Initialization_Call
577 (Loc, Comp, Comp_Type,
578 In_Init_Proc => True,
579 Enclos_Type => A_Type);
583 ------------------------
584 -- Init_One_Dimension --
585 ------------------------
587 function Init_One_Dimension (N : Int) return List_Id is
591 -- If the component does not need initializing, then there is nothing
592 -- to do here, so we return a null body. This occurs when generating
593 -- the dummy Init_Proc needed for Initialize_Scalars processing.
595 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
596 and then not Needs_Simple_Initialization (Comp_Type)
597 and then not Has_Task (Comp_Type)
599 return New_List (Make_Null_Statement (Loc));
601 -- If all dimensions dealt with, we simply initialize the component
603 elsif N > Number_Dimensions (A_Type) then
604 return Init_Component;
606 -- Here we generate the required loop
610 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
612 Append (New_Reference_To (Index, Loc), Index_List);
615 Make_Implicit_Loop_Statement (Nod,
618 Make_Iteration_Scheme (Loc,
619 Loop_Parameter_Specification =>
620 Make_Loop_Parameter_Specification (Loc,
621 Defining_Identifier => Index,
622 Discrete_Subtype_Definition =>
623 Make_Attribute_Reference (Loc,
624 Prefix => Make_Identifier (Loc, Name_uInit),
625 Attribute_Name => Name_Range,
626 Expressions => New_List (
627 Make_Integer_Literal (Loc, N))))),
628 Statements => Init_One_Dimension (N + 1)));
630 end Init_One_Dimension;
632 -- Start of processing for Build_Array_Init_Proc
635 if Suppress_Init_Proc (A_Type) or else Is_Value_Type (Comp_Type) then
639 Index_List := New_List;
641 -- We need an initialization procedure if any of the following is true:
643 -- 1. The component type has an initialization procedure
644 -- 2. The component type needs simple initialization
645 -- 3. Tasks are present
646 -- 4. The type is marked as a public entity
648 -- The reason for the public entity test is to deal properly with the
649 -- Initialize_Scalars pragma. This pragma can be set in the client and
650 -- not in the declaring package, this means the client will make a call
651 -- to the initialization procedure (because one of conditions 1-3 must
652 -- apply in this case), and we must generate a procedure (even if it is
653 -- null) to satisfy the call in this case.
655 -- Exception: do not build an array init_proc for a type whose root
656 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
657 -- is no place to put the code, and in any case we handle initialization
658 -- of such types (in the Initialize_Scalars case, that's the only time
659 -- the issue arises) in a special manner anyway which does not need an
662 if Has_Non_Null_Base_Init_Proc (Comp_Type)
663 or else Needs_Simple_Initialization (Comp_Type)
664 or else Has_Task (Comp_Type)
665 or else (not Restriction_Active (No_Initialize_Scalars)
666 and then Is_Public (A_Type)
667 and then Root_Type (A_Type) /= Standard_String
668 and then Root_Type (A_Type) /= Standard_Wide_String
669 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
672 Make_Defining_Identifier (Loc, Make_Init_Proc_Name (A_Type));
674 Body_Stmts := Init_One_Dimension (1);
677 Make_Subprogram_Body (Loc,
679 Make_Procedure_Specification (Loc,
680 Defining_Unit_Name => Proc_Id,
681 Parameter_Specifications => Init_Formals (A_Type)),
682 Declarations => New_List,
683 Handled_Statement_Sequence =>
684 Make_Handled_Sequence_Of_Statements (Loc,
685 Statements => Body_Stmts)));
687 Set_Ekind (Proc_Id, E_Procedure);
688 Set_Is_Public (Proc_Id, Is_Public (A_Type));
689 Set_Is_Internal (Proc_Id);
690 Set_Has_Completion (Proc_Id);
692 if not Debug_Generated_Code then
693 Set_Debug_Info_Off (Proc_Id);
696 -- Set inlined unless controlled stuff or tasks around, in which
697 -- case we do not want to inline, because nested stuff may cause
698 -- difficulties in inter-unit inlining, and furthermore there is
699 -- in any case no point in inlining such complex init procs.
701 if not Has_Task (Proc_Id)
702 and then not Controlled_Type (Proc_Id)
704 Set_Is_Inlined (Proc_Id);
707 -- Associate Init_Proc with type, and determine if the procedure
708 -- is null (happens because of the Initialize_Scalars pragma case,
709 -- where we have to generate a null procedure in case it is called
710 -- by a client with Initialize_Scalars set). Such procedures have
711 -- to be generated, but do not have to be called, so we mark them
712 -- as null to suppress the call.
714 Set_Init_Proc (A_Type, Proc_Id);
716 if List_Length (Body_Stmts) = 1
717 and then Nkind (First (Body_Stmts)) = N_Null_Statement
719 Set_Is_Null_Init_Proc (Proc_Id);
722 -- Try to build a static aggregate to initialize statically
723 -- objects of the type. This can only be done for constrained
724 -- one-dimensional arrays with static bounds.
726 Set_Static_Initialization
728 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
731 end Build_Array_Init_Proc;
733 -----------------------------
734 -- Build_Class_Wide_Master --
735 -----------------------------
737 procedure Build_Class_Wide_Master (T : Entity_Id) is
738 Loc : constant Source_Ptr := Sloc (T);
745 -- Nothing to do if there is no task hierarchy
747 if Restriction_Active (No_Task_Hierarchy) then
751 -- Find declaration that created the access type: either a type
752 -- declaration, or an object declaration with an access definition,
753 -- in which case the type is anonymous.
756 P := Associated_Node_For_Itype (T);
761 -- Nothing to do if we already built a master entity for this scope
763 if not Has_Master_Entity (Scope (T)) then
765 -- First build the master entity
766 -- _Master : constant Master_Id := Current_Master.all;
767 -- and insert it just before the current declaration.
770 Make_Object_Declaration (Loc,
771 Defining_Identifier =>
772 Make_Defining_Identifier (Loc, Name_uMaster),
773 Constant_Present => True,
774 Object_Definition => New_Reference_To (Standard_Integer, Loc),
776 Make_Explicit_Dereference (Loc,
777 New_Reference_To (RTE (RE_Current_Master), Loc)));
779 Insert_Action (P, Decl);
781 Set_Has_Master_Entity (Scope (T));
783 -- Now mark the containing scope as a task master
786 while Nkind (Par) /= N_Compilation_Unit loop
789 -- If we fall off the top, we are at the outer level, and the
790 -- environment task is our effective master, so nothing to mark.
792 if Nkind (Par) = N_Task_Body
793 or else Nkind (Par) = N_Block_Statement
794 or else Nkind (Par) = N_Subprogram_Body
796 Set_Is_Task_Master (Par, True);
802 -- Now define the renaming of the master_id
805 Make_Defining_Identifier (Loc,
806 New_External_Name (Chars (T), 'M'));
809 Make_Object_Renaming_Declaration (Loc,
810 Defining_Identifier => M_Id,
811 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
812 Name => Make_Identifier (Loc, Name_uMaster));
813 Insert_Before (P, Decl);
816 Set_Master_Id (T, M_Id);
819 when RE_Not_Available =>
821 end Build_Class_Wide_Master;
823 --------------------------------
824 -- Build_Discr_Checking_Funcs --
825 --------------------------------
827 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
830 Enclosing_Func_Id : Entity_Id;
835 function Build_Case_Statement
836 (Case_Id : Entity_Id;
837 Variant : Node_Id) return Node_Id;
838 -- Build a case statement containing only two alternatives. The first
839 -- alternative corresponds exactly to the discrete choices given on the
840 -- variant with contains the components that we are generating the
841 -- checks for. If the discriminant is one of these return False. The
842 -- second alternative is an OTHERS choice that will return True
843 -- indicating the discriminant did not match.
845 function Build_Dcheck_Function
846 (Case_Id : Entity_Id;
847 Variant : Node_Id) return Entity_Id;
848 -- Build the discriminant checking function for a given variant
850 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
851 -- Builds the discriminant checking function for each variant of the
852 -- given variant part of the record type.
854 --------------------------
855 -- Build_Case_Statement --
856 --------------------------
858 function Build_Case_Statement
859 (Case_Id : Entity_Id;
860 Variant : Node_Id) return Node_Id
862 Alt_List : constant List_Id := New_List;
863 Actuals_List : List_Id;
865 Case_Alt_Node : Node_Id;
867 Choice_List : List_Id;
869 Return_Node : Node_Id;
872 Case_Node := New_Node (N_Case_Statement, Loc);
874 -- Replace the discriminant which controls the variant, with the name
875 -- of the formal of the checking function.
877 Set_Expression (Case_Node,
878 Make_Identifier (Loc, Chars (Case_Id)));
880 Choice := First (Discrete_Choices (Variant));
882 if Nkind (Choice) = N_Others_Choice then
883 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
885 Choice_List := New_Copy_List (Discrete_Choices (Variant));
888 if not Is_Empty_List (Choice_List) then
889 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
890 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
892 -- In case this is a nested variant, we need to return the result
893 -- of the discriminant checking function for the immediately
894 -- enclosing variant.
896 if Present (Enclosing_Func_Id) then
897 Actuals_List := New_List;
899 D := First_Discriminant (Rec_Id);
900 while Present (D) loop
901 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
902 Next_Discriminant (D);
906 Make_Simple_Return_Statement (Loc,
908 Make_Function_Call (Loc,
910 New_Reference_To (Enclosing_Func_Id, Loc),
911 Parameter_Associations =>
916 Make_Simple_Return_Statement (Loc,
918 New_Reference_To (Standard_False, Loc));
921 Set_Statements (Case_Alt_Node, New_List (Return_Node));
922 Append (Case_Alt_Node, Alt_List);
925 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
926 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
927 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
930 Make_Simple_Return_Statement (Loc,
932 New_Reference_To (Standard_True, Loc));
934 Set_Statements (Case_Alt_Node, New_List (Return_Node));
935 Append (Case_Alt_Node, Alt_List);
937 Set_Alternatives (Case_Node, Alt_List);
939 end Build_Case_Statement;
941 ---------------------------
942 -- Build_Dcheck_Function --
943 ---------------------------
945 function Build_Dcheck_Function
946 (Case_Id : Entity_Id;
947 Variant : Node_Id) return Entity_Id
951 Parameter_List : List_Id;
955 Body_Node := New_Node (N_Subprogram_Body, Loc);
956 Sequence := Sequence + 1;
959 Make_Defining_Identifier (Loc,
960 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
962 Spec_Node := New_Node (N_Function_Specification, Loc);
963 Set_Defining_Unit_Name (Spec_Node, Func_Id);
965 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
967 Set_Parameter_Specifications (Spec_Node, Parameter_List);
968 Set_Result_Definition (Spec_Node,
969 New_Reference_To (Standard_Boolean, Loc));
970 Set_Specification (Body_Node, Spec_Node);
971 Set_Declarations (Body_Node, New_List);
973 Set_Handled_Statement_Sequence (Body_Node,
974 Make_Handled_Sequence_Of_Statements (Loc,
975 Statements => New_List (
976 Build_Case_Statement (Case_Id, Variant))));
978 Set_Ekind (Func_Id, E_Function);
979 Set_Mechanism (Func_Id, Default_Mechanism);
980 Set_Is_Inlined (Func_Id, True);
981 Set_Is_Pure (Func_Id, True);
982 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
983 Set_Is_Internal (Func_Id, True);
985 if not Debug_Generated_Code then
986 Set_Debug_Info_Off (Func_Id);
991 Append_Freeze_Action (Rec_Id, Body_Node);
992 Set_Dcheck_Function (Variant, Func_Id);
994 end Build_Dcheck_Function;
996 ----------------------------
997 -- Build_Dcheck_Functions --
998 ----------------------------
1000 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1001 Component_List_Node : Node_Id;
1003 Discr_Name : Entity_Id;
1004 Func_Id : Entity_Id;
1006 Saved_Enclosing_Func_Id : Entity_Id;
1009 -- Build the discriminant checking function for each variant, label
1010 -- all components of that variant with the function's name.
1012 Discr_Name := Entity (Name (Variant_Part_Node));
1013 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1015 while Present (Variant) loop
1016 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1017 Component_List_Node := Component_List (Variant);
1019 if not Null_Present (Component_List_Node) then
1021 First_Non_Pragma (Component_Items (Component_List_Node));
1023 while Present (Decl) loop
1024 Set_Discriminant_Checking_Func
1025 (Defining_Identifier (Decl), Func_Id);
1027 Next_Non_Pragma (Decl);
1030 if Present (Variant_Part (Component_List_Node)) then
1031 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1032 Enclosing_Func_Id := Func_Id;
1033 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1034 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1038 Next_Non_Pragma (Variant);
1040 end Build_Dcheck_Functions;
1042 -- Start of processing for Build_Discr_Checking_Funcs
1045 -- Only build if not done already
1047 if not Discr_Check_Funcs_Built (N) then
1048 Type_Def := Type_Definition (N);
1050 if Nkind (Type_Def) = N_Record_Definition then
1051 if No (Component_List (Type_Def)) then -- null record.
1054 V := Variant_Part (Component_List (Type_Def));
1057 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1058 if No (Component_List (Record_Extension_Part (Type_Def))) then
1062 (Component_List (Record_Extension_Part (Type_Def)));
1066 Rec_Id := Defining_Identifier (N);
1068 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1070 Enclosing_Func_Id := Empty;
1071 Build_Dcheck_Functions (V);
1074 Set_Discr_Check_Funcs_Built (N);
1076 end Build_Discr_Checking_Funcs;
1078 --------------------------------
1079 -- Build_Discriminant_Formals --
1080 --------------------------------
1082 function Build_Discriminant_Formals
1083 (Rec_Id : Entity_Id;
1084 Use_Dl : Boolean) return List_Id
1086 Loc : Source_Ptr := Sloc (Rec_Id);
1087 Parameter_List : constant List_Id := New_List;
1090 Param_Spec_Node : Node_Id;
1093 if Has_Discriminants (Rec_Id) then
1094 D := First_Discriminant (Rec_Id);
1095 while Present (D) loop
1099 Formal := Discriminal (D);
1101 Formal := Make_Defining_Identifier (Loc, Chars (D));
1105 Make_Parameter_Specification (Loc,
1106 Defining_Identifier => Formal,
1108 New_Reference_To (Etype (D), Loc));
1109 Append (Param_Spec_Node, Parameter_List);
1110 Next_Discriminant (D);
1114 return Parameter_List;
1115 end Build_Discriminant_Formals;
1117 --------------------------------------
1118 -- Build_Equivalent_Array_Aggregate --
1119 --------------------------------------
1121 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1122 Loc : constant Source_Ptr := Sloc (T);
1123 Comp_Type : constant Entity_Id := Component_Type (T);
1124 Index_Type : constant Entity_Id := Etype (First_Index (T));
1125 Proc : constant Entity_Id := Base_Init_Proc (T);
1131 if not Is_Constrained (T)
1132 or else Number_Dimensions (T) > 1
1135 Initialization_Warning (T);
1139 Lo := Type_Low_Bound (Index_Type);
1140 Hi := Type_High_Bound (Index_Type);
1142 if not Compile_Time_Known_Value (Lo)
1143 or else not Compile_Time_Known_Value (Hi)
1145 Initialization_Warning (T);
1149 if Is_Record_Type (Comp_Type)
1150 and then Present (Base_Init_Proc (Comp_Type))
1152 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1155 Initialization_Warning (T);
1160 Initialization_Warning (T);
1164 Aggr := Make_Aggregate (Loc, No_List, New_List);
1165 Set_Etype (Aggr, T);
1166 Set_Aggregate_Bounds (Aggr,
1168 Low_Bound => New_Copy (Lo),
1169 High_Bound => New_Copy (Hi)));
1170 Set_Parent (Aggr, Parent (Proc));
1172 Append_To (Component_Associations (Aggr),
1173 Make_Component_Association (Loc,
1177 Low_Bound => New_Copy (Lo),
1178 High_Bound => New_Copy (Hi))),
1179 Expression => Expr));
1181 if Static_Array_Aggregate (Aggr) then
1184 Initialization_Warning (T);
1187 end Build_Equivalent_Array_Aggregate;
1189 ---------------------------------------
1190 -- Build_Equivalent_Record_Aggregate --
1191 ---------------------------------------
1193 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1197 -- Start of processing for Build_Equivalent_Record_Aggregate
1200 if not Is_Record_Type (T)
1201 or else Has_Discriminants (T)
1202 or else Is_Limited_Type (T)
1203 or else Has_Non_Standard_Rep (T)
1205 Initialization_Warning (T);
1209 Comp := First_Component (T);
1211 -- A null record needs no warning
1217 while Present (Comp) loop
1219 -- Array components are acceptable if initialized by a positional
1220 -- aggregate with static components.
1222 if Is_Array_Type (Etype (Comp)) then
1224 Comp_Type : constant Entity_Id := Component_Type (Etype (Comp));
1227 if Nkind (Parent (Comp)) /= N_Component_Declaration
1228 or else No (Expression (Parent (Comp)))
1229 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1231 Initialization_Warning (T);
1234 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1236 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1237 or else not Compile_Time_Known_Value
1238 (Type_High_Bound (Comp_Type)))
1240 Initialization_Warning (T);
1244 not Static_Array_Aggregate (Expression (Parent (Comp)))
1246 Initialization_Warning (T);
1251 elsif Is_Scalar_Type (Etype (Comp)) then
1252 if Nkind (Parent (Comp)) /= N_Component_Declaration
1253 or else No (Expression (Parent (Comp)))
1254 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1256 Initialization_Warning (T);
1260 -- For now, other types are excluded
1263 Initialization_Warning (T);
1267 Next_Component (Comp);
1270 -- All components have static initialization. Build positional
1271 -- aggregate from the given expressions or defaults.
1273 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1274 Set_Parent (Agg, Parent (T));
1276 Comp := First_Component (T);
1277 while Present (Comp) loop
1279 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1280 Next_Component (Comp);
1283 Analyze_And_Resolve (Agg, T);
1285 end Build_Equivalent_Record_Aggregate;
1287 -------------------------------
1288 -- Build_Initialization_Call --
1289 -------------------------------
1291 -- References to a discriminant inside the record type declaration can
1292 -- appear either in the subtype_indication to constrain a record or an
1293 -- array, or as part of a larger expression given for the initial value
1294 -- of a component. In both of these cases N appears in the record
1295 -- initialization procedure and needs to be replaced by the formal
1296 -- parameter of the initialization procedure which corresponds to that
1299 -- In the example below, references to discriminants D1 and D2 in proc_1
1300 -- are replaced by references to formals with the same name
1303 -- A similar replacement is done for calls to any record initialization
1304 -- procedure for any components that are themselves of a record type.
1306 -- type R (D1, D2 : Integer) is record
1307 -- X : Integer := F * D1;
1308 -- Y : Integer := F * D2;
1311 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1315 -- Out_2.X := F * D1;
1316 -- Out_2.Y := F * D2;
1319 function Build_Initialization_Call
1323 In_Init_Proc : Boolean := False;
1324 Enclos_Type : Entity_Id := Empty;
1325 Discr_Map : Elist_Id := New_Elmt_List;
1326 With_Default_Init : Boolean := False) return List_Id
1328 First_Arg : Node_Id;
1334 Proc : constant Entity_Id := Base_Init_Proc (Typ);
1335 Init_Type : constant Entity_Id := Etype (First_Formal (Proc));
1336 Full_Init_Type : constant Entity_Id := Underlying_Type (Init_Type);
1337 Res : constant List_Id := New_List;
1338 Full_Type : Entity_Id := Typ;
1339 Controller_Typ : Entity_Id;
1342 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1343 -- is active (in which case we make the call anyway, since in the
1344 -- actual compiled client it may be non null).
1345 -- Also nothing to do for value types.
1347 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1348 or else Is_Value_Type (Typ)
1349 or else Is_Value_Type (Component_Type (Typ))
1354 -- Go to full view if private type. In the case of successive
1355 -- private derivations, this can require more than one step.
1357 while Is_Private_Type (Full_Type)
1358 and then Present (Full_View (Full_Type))
1360 Full_Type := Full_View (Full_Type);
1363 -- If Typ is derived, the procedure is the initialization procedure for
1364 -- the root type. Wrap the argument in an conversion to make it type
1365 -- honest. Actually it isn't quite type honest, because there can be
1366 -- conflicts of views in the private type case. That is why we set
1367 -- Conversion_OK in the conversion node.
1369 if (Is_Record_Type (Typ)
1370 or else Is_Array_Type (Typ)
1371 or else Is_Private_Type (Typ))
1372 and then Init_Type /= Base_Type (Typ)
1374 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1375 Set_Etype (First_Arg, Init_Type);
1378 First_Arg := Id_Ref;
1381 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1383 -- In the tasks case, add _Master as the value of the _Master parameter
1384 -- and _Chain as the value of the _Chain parameter. At the outer level,
1385 -- these will be variables holding the corresponding values obtained
1386 -- from GNARL. At inner levels, they will be the parameters passed down
1387 -- through the outer routines.
1389 if Has_Task (Full_Type) then
1390 if Restriction_Active (No_Task_Hierarchy) then
1392 -- See comments in System.Tasking.Initialization.Init_RTS
1393 -- for the value 3 (should be rtsfindable constant ???)
1395 Append_To (Args, Make_Integer_Literal (Loc, 3));
1398 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1401 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1403 -- Ada 2005 (AI-287): In case of default initialized components
1404 -- with tasks, we generate a null string actual parameter.
1405 -- This is just a workaround that must be improved later???
1407 if With_Default_Init then
1409 Make_String_Literal (Loc,
1414 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1415 Decl := Last (Decls);
1418 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1419 Append_List (Decls, Res);
1427 -- Add discriminant values if discriminants are present
1429 if Has_Discriminants (Full_Init_Type) then
1430 Discr := First_Discriminant (Full_Init_Type);
1432 while Present (Discr) loop
1434 -- If this is a discriminated concurrent type, the init_proc
1435 -- for the corresponding record is being called. Use that type
1436 -- directly to find the discriminant value, to handle properly
1437 -- intervening renamed discriminants.
1440 T : Entity_Id := Full_Type;
1443 if Is_Protected_Type (T) then
1444 T := Corresponding_Record_Type (T);
1446 elsif Is_Private_Type (T)
1447 and then Present (Underlying_Full_View (T))
1448 and then Is_Protected_Type (Underlying_Full_View (T))
1450 T := Corresponding_Record_Type (Underlying_Full_View (T));
1454 Get_Discriminant_Value (
1457 Discriminant_Constraint (Full_Type));
1460 if In_Init_Proc then
1462 -- Replace any possible references to the discriminant in the
1463 -- call to the record initialization procedure with references
1464 -- to the appropriate formal parameter.
1466 if Nkind (Arg) = N_Identifier
1467 and then Ekind (Entity (Arg)) = E_Discriminant
1469 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1471 -- Case of access discriminants. We replace the reference
1472 -- to the type by a reference to the actual object
1474 elsif Nkind (Arg) = N_Attribute_Reference
1475 and then Is_Access_Type (Etype (Arg))
1476 and then Is_Entity_Name (Prefix (Arg))
1477 and then Is_Type (Entity (Prefix (Arg)))
1480 Make_Attribute_Reference (Loc,
1481 Prefix => New_Copy (Prefix (Id_Ref)),
1482 Attribute_Name => Name_Unrestricted_Access);
1484 -- Otherwise make a copy of the default expression. Note that
1485 -- we use the current Sloc for this, because we do not want the
1486 -- call to appear to be at the declaration point. Within the
1487 -- expression, replace discriminants with their discriminals.
1491 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1495 if Is_Constrained (Full_Type) then
1496 Arg := Duplicate_Subexpr_No_Checks (Arg);
1498 -- The constraints come from the discriminant default exps,
1499 -- they must be reevaluated, so we use New_Copy_Tree but we
1500 -- ensure the proper Sloc (for any embedded calls).
1502 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1506 -- Ada 2005 (AI-287) In case of default initialized components,
1507 -- we need to generate the corresponding selected component node
1508 -- to access the discriminant value. In other cases this is not
1509 -- required because we are inside the init proc and we use the
1510 -- corresponding formal.
1512 if With_Default_Init
1513 and then Nkind (Id_Ref) = N_Selected_Component
1514 and then Nkind (Arg) = N_Identifier
1517 Make_Selected_Component (Loc,
1518 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1519 Selector_Name => Arg));
1521 Append_To (Args, Arg);
1524 Next_Discriminant (Discr);
1528 -- If this is a call to initialize the parent component of a derived
1529 -- tagged type, indicate that the tag should not be set in the parent.
1531 if Is_Tagged_Type (Full_Init_Type)
1532 and then not Is_CPP_Class (Full_Init_Type)
1533 and then Nkind (Id_Ref) = N_Selected_Component
1534 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1536 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1540 Make_Procedure_Call_Statement (Loc,
1541 Name => New_Occurrence_Of (Proc, Loc),
1542 Parameter_Associations => Args));
1544 if Controlled_Type (Typ)
1545 and then Nkind (Id_Ref) = N_Selected_Component
1547 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1548 Append_List_To (Res,
1550 Ref => New_Copy_Tree (First_Arg),
1553 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1554 With_Attach => Make_Integer_Literal (Loc, 1)));
1556 -- If the enclosing type is an extension with new controlled
1557 -- components, it has his own record controller. If the parent
1558 -- also had a record controller, attach it to the new one.
1560 -- Build_Init_Statements relies on the fact that in this specific
1561 -- case the last statement of the result is the attach call to
1562 -- the controller. If this is changed, it must be synchronized.
1564 elsif Present (Enclos_Type)
1565 and then Has_New_Controlled_Component (Enclos_Type)
1566 and then Has_Controlled_Component (Typ)
1568 if Is_Inherently_Limited_Type (Typ) then
1569 Controller_Typ := RTE (RE_Limited_Record_Controller);
1571 Controller_Typ := RTE (RE_Record_Controller);
1574 Append_List_To (Res,
1577 Make_Selected_Component (Loc,
1578 Prefix => New_Copy_Tree (First_Arg),
1579 Selector_Name => Make_Identifier (Loc, Name_uController)),
1580 Typ => Controller_Typ,
1581 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1582 With_Attach => Make_Integer_Literal (Loc, 1)));
1589 when RE_Not_Available =>
1591 end Build_Initialization_Call;
1593 ---------------------------
1594 -- Build_Master_Renaming --
1595 ---------------------------
1597 function Build_Master_Renaming
1599 T : Entity_Id) return Entity_Id
1601 Loc : constant Source_Ptr := Sloc (N);
1606 -- Nothing to do if there is no task hierarchy
1608 if Restriction_Active (No_Task_Hierarchy) then
1613 Make_Defining_Identifier (Loc,
1614 New_External_Name (Chars (T), 'M'));
1617 Make_Object_Renaming_Declaration (Loc,
1618 Defining_Identifier => M_Id,
1619 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1620 Name => Make_Identifier (Loc, Name_uMaster));
1621 Insert_Before (N, Decl);
1626 when RE_Not_Available =>
1628 end Build_Master_Renaming;
1630 ---------------------------
1631 -- Build_Master_Renaming --
1632 ---------------------------
1634 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1638 -- Nothing to do if there is no task hierarchy
1640 if Restriction_Active (No_Task_Hierarchy) then
1644 M_Id := Build_Master_Renaming (N, T);
1645 Set_Master_Id (T, M_Id);
1648 when RE_Not_Available =>
1650 end Build_Master_Renaming;
1652 ----------------------------
1653 -- Build_Record_Init_Proc --
1654 ----------------------------
1656 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1657 Loc : Source_Ptr := Sloc (N);
1658 Discr_Map : constant Elist_Id := New_Elmt_List;
1659 Proc_Id : Entity_Id;
1660 Rec_Type : Entity_Id;
1661 Set_Tag : Entity_Id := Empty;
1663 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1664 -- Build a assignment statement node which assigns to record component
1665 -- its default expression if defined. The assignment left hand side is
1666 -- marked Assignment_OK so that initialization of limited private
1667 -- records works correctly, Return also the adjustment call for
1668 -- controlled objects
1670 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1671 -- If the record has discriminants, adds assignment statements to
1672 -- statement list to initialize the discriminant values from the
1673 -- arguments of the initialization procedure.
1675 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1676 -- Build a list representing a sequence of statements which initialize
1677 -- components of the given component list. This may involve building
1678 -- case statements for the variant parts.
1680 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1681 -- Given a non-tagged type-derivation that declares discriminants,
1684 -- type R (R1, R2 : Integer) is record ... end record;
1686 -- type D (D1 : Integer) is new R (1, D1);
1688 -- we make the _init_proc of D be
1690 -- procedure _init_proc(X : D; D1 : Integer) is
1692 -- _init_proc( R(X), 1, D1);
1695 -- This function builds the call statement in this _init_proc.
1697 procedure Build_Init_Procedure;
1698 -- Build the tree corresponding to the procedure specification and body
1699 -- of the initialization procedure (by calling all the preceding
1700 -- auxiliary routines), and install it as the _init TSS.
1702 procedure Build_Offset_To_Top_Functions;
1703 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1704 -- and body of the Offset_To_Top function that is generated when the
1705 -- parent of a type with discriminants has secondary dispatch tables.
1707 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1708 -- Add range checks to components of discriminated records. S is a
1709 -- subtype indication of a record component. Check_List is a list
1710 -- to which the check actions are appended.
1712 function Component_Needs_Simple_Initialization
1713 (T : Entity_Id) return Boolean;
1714 -- Determines if a component needs simple initialization, given its type
1715 -- T. This is the same as Needs_Simple_Initialization except for the
1716 -- following difference: the types Tag and Interface_Tag, that are
1717 -- access types which would normally require simple initialization to
1718 -- null, do not require initialization as components, since they are
1719 -- explicitly initialized by other means.
1721 procedure Constrain_Array
1723 Check_List : List_Id);
1724 -- Called from Build_Record_Checks.
1725 -- Apply a list of index constraints to an unconstrained array type.
1726 -- The first parameter is the entity for the resulting subtype.
1727 -- Check_List is a list to which the check actions are appended.
1729 procedure Constrain_Index
1732 Check_List : List_Id);
1733 -- Process an index constraint in a constrained array declaration.
1734 -- The constraint can be a subtype name, or a range with or without
1735 -- an explicit subtype mark. The index is the corresponding index of the
1736 -- unconstrained array. S is the range expression. Check_List is a list
1737 -- to which the check actions are appended (called from
1738 -- Build_Record_Checks).
1740 function Parent_Subtype_Renaming_Discrims return Boolean;
1741 -- Returns True for base types N that rename discriminants, else False
1743 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1744 -- Determines whether a record initialization procedure needs to be
1745 -- generated for the given record type.
1747 ----------------------
1748 -- Build_Assignment --
1749 ----------------------
1751 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1754 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1755 Kind : Node_Kind := Nkind (N);
1761 Make_Selected_Component (Loc,
1762 Prefix => Make_Identifier (Loc, Name_uInit),
1763 Selector_Name => New_Occurrence_Of (Id, Loc));
1764 Set_Assignment_OK (Lhs);
1766 -- Case of an access attribute applied to the current instance.
1767 -- Replace the reference to the type by a reference to the actual
1768 -- object. (Note that this handles the case of the top level of
1769 -- the expression being given by such an attribute, but does not
1770 -- cover uses nested within an initial value expression. Nested
1771 -- uses are unlikely to occur in practice, but are theoretically
1772 -- possible. It is not clear how to handle them without fully
1773 -- traversing the expression. ???
1775 if Kind = N_Attribute_Reference
1776 and then (Attribute_Name (N) = Name_Unchecked_Access
1778 Attribute_Name (N) = Name_Unrestricted_Access)
1779 and then Is_Entity_Name (Prefix (N))
1780 and then Is_Type (Entity (Prefix (N)))
1781 and then Entity (Prefix (N)) = Rec_Type
1784 Make_Attribute_Reference (Loc,
1785 Prefix => Make_Identifier (Loc, Name_uInit),
1786 Attribute_Name => Name_Unrestricted_Access);
1789 -- Ada 2005 (AI-231): Add the run-time check if required
1791 if Ada_Version >= Ada_05
1792 and then Can_Never_Be_Null (Etype (Id)) -- Lhs
1794 if Known_Null (Exp) then
1796 Make_Raise_Constraint_Error (Sloc (Exp),
1797 Reason => CE_Null_Not_Allowed));
1799 elsif Present (Etype (Exp))
1800 and then not Can_Never_Be_Null (Etype (Exp))
1802 Install_Null_Excluding_Check (Exp);
1806 -- Take a copy of Exp to ensure that later copies of this component
1807 -- declaration in derived types see the original tree, not a node
1808 -- rewritten during expansion of the init_proc.
1810 Exp := New_Copy_Tree (Exp);
1813 Make_Assignment_Statement (Loc,
1815 Expression => Exp));
1817 Set_No_Ctrl_Actions (First (Res));
1819 -- Adjust the tag if tagged (because of possible view conversions).
1820 -- Suppress the tag adjustment when VM_Target because VM tags are
1821 -- represented implicitly in objects.
1823 if Is_Tagged_Type (Typ) and then VM_Target = No_VM then
1825 Make_Assignment_Statement (Loc,
1827 Make_Selected_Component (Loc,
1828 Prefix => New_Copy_Tree (Lhs),
1830 New_Reference_To (First_Tag_Component (Typ), Loc)),
1833 Unchecked_Convert_To (RTE (RE_Tag),
1835 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1838 -- Adjust the component if controlled except if it is an aggregate
1839 -- that will be expanded inline
1841 if Kind = N_Qualified_Expression then
1842 Kind := Nkind (Expression (N));
1845 if Controlled_Type (Typ)
1846 and then not (Kind = N_Aggregate or else Kind = N_Extension_Aggregate)
1847 and then not Is_Inherently_Limited_Type (Typ)
1849 Append_List_To (Res,
1851 Ref => New_Copy_Tree (Lhs),
1854 Find_Final_List (Etype (Id), New_Copy_Tree (Lhs)),
1855 With_Attach => Make_Integer_Literal (Loc, 1)));
1861 when RE_Not_Available =>
1863 end Build_Assignment;
1865 ------------------------------------
1866 -- Build_Discriminant_Assignments --
1867 ------------------------------------
1869 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1871 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1874 if Has_Discriminants (Rec_Type)
1875 and then not Is_Unchecked_Union (Rec_Type)
1877 D := First_Discriminant (Rec_Type);
1879 while Present (D) loop
1880 -- Don't generate the assignment for discriminants in derived
1881 -- tagged types if the discriminant is a renaming of some
1882 -- ancestor discriminant. This initialization will be done
1883 -- when initializing the _parent field of the derived record.
1885 if Is_Tagged and then
1886 Present (Corresponding_Discriminant (D))
1892 Append_List_To (Statement_List,
1893 Build_Assignment (D,
1894 New_Reference_To (Discriminal (D), Loc)));
1897 Next_Discriminant (D);
1900 end Build_Discriminant_Assignments;
1902 --------------------------
1903 -- Build_Init_Call_Thru --
1904 --------------------------
1906 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1907 Parent_Proc : constant Entity_Id :=
1908 Base_Init_Proc (Etype (Rec_Type));
1910 Parent_Type : constant Entity_Id :=
1911 Etype (First_Formal (Parent_Proc));
1913 Uparent_Type : constant Entity_Id :=
1914 Underlying_Type (Parent_Type);
1916 First_Discr_Param : Node_Id;
1918 Parent_Discr : Entity_Id;
1919 First_Arg : Node_Id;
1925 -- First argument (_Init) is the object to be initialized.
1926 -- ??? not sure where to get a reasonable Loc for First_Arg
1929 OK_Convert_To (Parent_Type,
1930 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1932 Set_Etype (First_Arg, Parent_Type);
1934 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1936 -- In the tasks case,
1937 -- add _Master as the value of the _Master parameter
1938 -- add _Chain as the value of the _Chain parameter.
1939 -- add _Task_Name as the value of the _Task_Name parameter.
1940 -- At the outer level, these will be variables holding the
1941 -- corresponding values obtained from GNARL or the expander.
1943 -- At inner levels, they will be the parameters passed down through
1944 -- the outer routines.
1946 First_Discr_Param := Next (First (Parameters));
1948 if Has_Task (Rec_Type) then
1949 if Restriction_Active (No_Task_Hierarchy) then
1951 -- See comments in System.Tasking.Initialization.Init_RTS
1954 Append_To (Args, Make_Integer_Literal (Loc, 3));
1956 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1959 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1960 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1961 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1964 -- Append discriminant values
1966 if Has_Discriminants (Uparent_Type) then
1967 pragma Assert (not Is_Tagged_Type (Uparent_Type));
1969 Parent_Discr := First_Discriminant (Uparent_Type);
1970 while Present (Parent_Discr) loop
1972 -- Get the initial value for this discriminant
1973 -- ??? needs to be cleaned up to use parent_Discr_Constr
1977 Discr_Value : Elmt_Id :=
1979 (Stored_Constraint (Rec_Type));
1981 Discr : Entity_Id :=
1982 First_Stored_Discriminant (Uparent_Type);
1984 while Original_Record_Component (Parent_Discr) /= Discr loop
1985 Next_Stored_Discriminant (Discr);
1986 Next_Elmt (Discr_Value);
1989 Arg := Node (Discr_Value);
1992 -- Append it to the list
1994 if Nkind (Arg) = N_Identifier
1995 and then Ekind (Entity (Arg)) = E_Discriminant
1998 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2000 -- Case of access discriminants. We replace the reference
2001 -- to the type by a reference to the actual object.
2003 -- Is above comment right??? Use of New_Copy below seems mighty
2007 Append_To (Args, New_Copy (Arg));
2010 Next_Discriminant (Parent_Discr);
2016 Make_Procedure_Call_Statement (Loc,
2017 Name => New_Occurrence_Of (Parent_Proc, Loc),
2018 Parameter_Associations => Args));
2021 end Build_Init_Call_Thru;
2023 -----------------------------------
2024 -- Build_Offset_To_Top_Functions --
2025 -----------------------------------
2027 procedure Build_Offset_To_Top_Functions is
2029 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2031 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2033 -- return O.Iface_Comp'Position;
2036 ------------------------------
2037 -- Build_Offset_To_Top_Body --
2038 ------------------------------
2040 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2041 Body_Node : Node_Id;
2042 Func_Id : Entity_Id;
2043 Spec_Node : Node_Id;
2047 Make_Defining_Identifier (Loc,
2048 Chars => New_Internal_Name ('F'));
2050 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2053 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2055 Spec_Node := New_Node (N_Function_Specification, Loc);
2056 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2057 Set_Parameter_Specifications (Spec_Node, New_List (
2058 Make_Parameter_Specification (Loc,
2059 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2061 Parameter_Type => New_Reference_To (Rec_Type, Loc))));
2062 Set_Result_Definition (Spec_Node,
2063 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2066 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2068 -- return O.Iface_Comp'Position;
2071 Body_Node := New_Node (N_Subprogram_Body, Loc);
2072 Set_Specification (Body_Node, Spec_Node);
2073 Set_Declarations (Body_Node, New_List);
2074 Set_Handled_Statement_Sequence (Body_Node,
2075 Make_Handled_Sequence_Of_Statements (Loc,
2076 Statements => New_List (
2077 Make_Simple_Return_Statement (Loc,
2079 Make_Attribute_Reference (Loc,
2081 Make_Selected_Component (Loc,
2082 Prefix => Make_Identifier (Loc, Name_uO),
2083 Selector_Name => New_Reference_To
2085 Attribute_Name => Name_Position)))));
2087 Set_Ekind (Func_Id, E_Function);
2088 Set_Mechanism (Func_Id, Default_Mechanism);
2089 Set_Is_Internal (Func_Id, True);
2091 if not Debug_Generated_Code then
2092 Set_Debug_Info_Off (Func_Id);
2095 Analyze (Body_Node);
2097 Append_Freeze_Action (Rec_Type, Body_Node);
2098 end Build_Offset_To_Top_Function;
2102 Ifaces_List : Elist_Id;
2103 Ifaces_Comp_List : Elist_Id;
2104 Ifaces_Tag_List : Elist_Id;
2105 Iface_Elmt : Elmt_Id;
2106 Comp_Elmt : Elmt_Id;
2108 -- Start of processing for Build_Offset_To_Top_Functions
2111 -- Offset_To_Top_Functions are built only for derivations of types
2112 -- with discriminants that cover interface types.
2114 if not Is_Tagged_Type (Rec_Type)
2115 or else Etype (Rec_Type) = Rec_Type
2116 or else not Has_Discriminants (Etype (Rec_Type))
2121 Collect_Interfaces_Info (Rec_Type,
2122 Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
2124 -- For each interface type with secondary dispatch table we generate
2125 -- the Offset_To_Top_Functions (required to displace the pointer in
2126 -- interface conversions)
2128 Iface_Elmt := First_Elmt (Ifaces_List);
2129 Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2130 while Present (Iface_Elmt) loop
2132 -- If the interface is a parent of Rec_Type it shares the primary
2133 -- dispatch table and hence there is no need to build the function
2135 if not Is_Parent (Node (Iface_Elmt), Rec_Type) then
2136 Build_Offset_To_Top_Function (Iface_Comp => Node (Comp_Elmt));
2139 Next_Elmt (Iface_Elmt);
2140 Next_Elmt (Comp_Elmt);
2142 end Build_Offset_To_Top_Functions;
2144 --------------------------
2145 -- Build_Init_Procedure --
2146 --------------------------
2148 procedure Build_Init_Procedure is
2149 Body_Node : Node_Id;
2150 Handled_Stmt_Node : Node_Id;
2151 Parameters : List_Id;
2152 Proc_Spec_Node : Node_Id;
2153 Body_Stmts : List_Id;
2154 Record_Extension_Node : Node_Id;
2155 Init_Tags_List : List_Id;
2158 Body_Stmts := New_List;
2159 Body_Node := New_Node (N_Subprogram_Body, Loc);
2162 Make_Defining_Identifier (Loc,
2163 Chars => Make_Init_Proc_Name (Rec_Type));
2164 Set_Ekind (Proc_Id, E_Procedure);
2166 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2167 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2169 Parameters := Init_Formals (Rec_Type);
2170 Append_List_To (Parameters,
2171 Build_Discriminant_Formals (Rec_Type, True));
2173 -- For tagged types, we add a flag to indicate whether the routine
2174 -- is called to initialize a parent component in the init_proc of
2175 -- a type extension. If the flag is false, we do not set the tag
2176 -- because it has been set already in the extension.
2178 if Is_Tagged_Type (Rec_Type)
2179 and then not Is_CPP_Class (Rec_Type)
2182 Make_Defining_Identifier (Loc,
2183 Chars => New_Internal_Name ('P'));
2185 Append_To (Parameters,
2186 Make_Parameter_Specification (Loc,
2187 Defining_Identifier => Set_Tag,
2188 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2189 Expression => New_Occurrence_Of (Standard_True, Loc)));
2192 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2193 Set_Specification (Body_Node, Proc_Spec_Node);
2194 Set_Declarations (Body_Node, New_List);
2196 if Parent_Subtype_Renaming_Discrims then
2198 -- N is a Derived_Type_Definition that renames the parameters
2199 -- of the ancestor type. We initialize it by expanding our
2200 -- discriminants and call the ancestor _init_proc with a
2201 -- type-converted object
2203 Append_List_To (Body_Stmts,
2204 Build_Init_Call_Thru (Parameters));
2206 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2207 Build_Discriminant_Assignments (Body_Stmts);
2209 if not Null_Present (Type_Definition (N)) then
2210 Append_List_To (Body_Stmts,
2211 Build_Init_Statements (
2212 Component_List (Type_Definition (N))));
2216 -- N is a Derived_Type_Definition with a possible non-empty
2217 -- extension. The initialization of a type extension consists
2218 -- in the initialization of the components in the extension.
2220 Build_Discriminant_Assignments (Body_Stmts);
2222 Record_Extension_Node :=
2223 Record_Extension_Part (Type_Definition (N));
2225 if not Null_Present (Record_Extension_Node) then
2227 Stmts : constant List_Id :=
2228 Build_Init_Statements (
2229 Component_List (Record_Extension_Node));
2232 -- The parent field must be initialized first because
2233 -- the offset of the new discriminants may depend on it
2235 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2236 Append_List_To (Body_Stmts, Stmts);
2241 -- Add here the assignment to instantiate the Tag
2243 -- The assignment corresponds to the code:
2245 -- _Init._Tag := Typ'Tag;
2247 -- Suppress the tag assignment when VM_Target because VM tags are
2248 -- represented implicitly in objects. It is also suppressed in case
2249 -- of CPP_Class types because in this case the tag is initialized in
2252 if Is_Tagged_Type (Rec_Type)
2253 and then not Is_CPP_Class (Rec_Type)
2254 and then VM_Target = No_VM
2255 and then not No_Run_Time_Mode
2257 -- Initialize the primary tag
2259 Init_Tags_List := New_List (
2260 Make_Assignment_Statement (Loc,
2262 Make_Selected_Component (Loc,
2263 Prefix => Make_Identifier (Loc, Name_uInit),
2265 New_Reference_To (First_Tag_Component (Rec_Type), Loc)),
2269 (Node (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2271 -- Ada 2005 (AI-251): Initialize the secondary tags components
2272 -- located at fixed positions (tags whose position depends on
2273 -- variable size components are initialized later ---see below).
2275 if Ada_Version >= Ada_05
2276 and then not Is_Interface (Rec_Type)
2277 and then Has_Abstract_Interfaces (Rec_Type)
2281 Target => Make_Identifier (Loc, Name_uInit),
2282 Stmts_List => Init_Tags_List,
2283 Fixed_Comps => True,
2284 Variable_Comps => False);
2287 -- The tag must be inserted before the assignments to other
2288 -- components, because the initial value of the component may
2289 -- depend on the tag (eg. through a dispatching operation on
2290 -- an access to the current type). The tag assignment is not done
2291 -- when initializing the parent component of a type extension,
2292 -- because in that case the tag is set in the extension.
2294 -- Extensions of imported C++ classes add a final complication,
2295 -- because we cannot inhibit tag setting in the constructor for
2296 -- the parent. In that case we insert the tag initialization
2297 -- after the calls to initialize the parent.
2299 if not Is_CPP_Class (Etype (Rec_Type)) then
2300 Prepend_To (Body_Stmts,
2301 Make_If_Statement (Loc,
2302 Condition => New_Occurrence_Of (Set_Tag, Loc),
2303 Then_Statements => Init_Tags_List));
2305 -- CPP_Class: In this case the dispatch table of the parent was
2306 -- built in the C++ side and we copy the table of the parent to
2307 -- initialize the new dispatch table.
2314 -- We assume the first init_proc call is for the parent
2316 Nod := First (Body_Stmts);
2317 while Present (Next (Nod))
2318 and then (Nkind (Nod) /= N_Procedure_Call_Statement
2319 or else not Is_Init_Proc (Name (Nod)))
2325 -- ancestor_constructor (_init.parent);
2327 -- inherit_prim_ops (_init._tag, new_dt, num_prims);
2328 -- _init._tag := new_dt;
2331 Prepend_To (Init_Tags_List,
2332 Build_Inherit_Prims (Loc,
2335 Make_Selected_Component (Loc,
2337 Make_Identifier (Loc,
2338 Chars => Name_uInit),
2341 (First_Tag_Component (Rec_Type), Loc)),
2344 (Node (First_Elmt (Access_Disp_Table (Rec_Type))),
2348 (DT_Entry_Count (First_Tag_Component (Rec_Type)))));
2351 Make_If_Statement (Loc,
2352 Condition => New_Occurrence_Of (Set_Tag, Loc),
2353 Then_Statements => Init_Tags_List));
2355 -- We have inherited table of the parent from the CPP side.
2356 -- Now we fill the slots associated with Ada primitives.
2357 -- This needs more work to avoid its execution each time
2358 -- an object is initialized???
2365 E := First_Elmt (Primitive_Operations (Rec_Type));
2366 while Present (E) loop
2369 if not Is_Imported (Prim)
2370 and then Convention (Prim) = Convention_CPP
2371 and then not Present (Abstract_Interface_Alias
2374 Register_Primitive (Loc,
2376 Ins_Nod => Last (Init_Tags_List));
2385 -- Ada 2005 (AI-251): Initialize the secondary tag components
2386 -- located at variable positions. We delay the generation of this
2387 -- code until here because the value of the attribute 'Position
2388 -- applied to variable size components of the parent type that
2389 -- depend on discriminants is only safely read at runtime after
2390 -- the parent components have been initialized.
2392 if Ada_Version >= Ada_05
2393 and then not Is_Interface (Rec_Type)
2394 and then Has_Abstract_Interfaces (Rec_Type)
2395 and then Has_Discriminants (Etype (Rec_Type))
2396 and then Is_Variable_Size_Record (Etype (Rec_Type))
2398 Init_Tags_List := New_List;
2402 Target => Make_Identifier (Loc, Name_uInit),
2403 Stmts_List => Init_Tags_List,
2404 Fixed_Comps => False,
2405 Variable_Comps => True);
2407 if Is_Non_Empty_List (Init_Tags_List) then
2408 Append_List_To (Body_Stmts, Init_Tags_List);
2413 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2414 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2415 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2416 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2418 if not Debug_Generated_Code then
2419 Set_Debug_Info_Off (Proc_Id);
2422 -- Associate Init_Proc with type, and determine if the procedure
2423 -- is null (happens because of the Initialize_Scalars pragma case,
2424 -- where we have to generate a null procedure in case it is called
2425 -- by a client with Initialize_Scalars set). Such procedures have
2426 -- to be generated, but do not have to be called, so we mark them
2427 -- as null to suppress the call.
2429 Set_Init_Proc (Rec_Type, Proc_Id);
2431 if List_Length (Body_Stmts) = 1
2432 and then Nkind (First (Body_Stmts)) = N_Null_Statement
2433 and then VM_Target /= CLI_Target
2435 -- Even though the init proc may be null at this time it might get
2436 -- some stuff added to it later by the CIL backend, so always keep
2437 -- it when VM_Target = CLI_Target.
2439 Set_Is_Null_Init_Proc (Proc_Id);
2441 end Build_Init_Procedure;
2443 ---------------------------
2444 -- Build_Init_Statements --
2445 ---------------------------
2447 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2448 Check_List : constant List_Id := New_List;
2450 Statement_List : List_Id;
2453 Per_Object_Constraint_Components : Boolean;
2461 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2462 -- Components with access discriminants that depend on the current
2463 -- instance must be initialized after all other components.
2465 ---------------------------
2466 -- Has_Access_Constraint --
2467 ---------------------------
2469 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2471 T : constant Entity_Id := Etype (E);
2474 if Has_Per_Object_Constraint (E)
2475 and then Has_Discriminants (T)
2477 Disc := First_Discriminant (T);
2478 while Present (Disc) loop
2479 if Is_Access_Type (Etype (Disc)) then
2483 Next_Discriminant (Disc);
2490 end Has_Access_Constraint;
2492 -- Start of processing for Build_Init_Statements
2495 if Null_Present (Comp_List) then
2496 return New_List (Make_Null_Statement (Loc));
2499 Statement_List := New_List;
2501 -- Loop through components, skipping pragmas, in 2 steps. The first
2502 -- step deals with regular components. The second step deals with
2503 -- components have per object constraints, and no explicit initia-
2506 Per_Object_Constraint_Components := False;
2508 -- First step : regular components
2510 Decl := First_Non_Pragma (Component_Items (Comp_List));
2511 while Present (Decl) loop
2514 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2516 Id := Defining_Identifier (Decl);
2519 if Has_Access_Constraint (Id)
2520 and then No (Expression (Decl))
2522 -- Skip processing for now and ask for a second pass
2524 Per_Object_Constraint_Components := True;
2527 -- Case of explicit initialization
2529 if Present (Expression (Decl)) then
2530 Stmts := Build_Assignment (Id, Expression (Decl));
2532 -- Case of composite component with its own Init_Proc
2534 elsif not Is_Interface (Typ)
2535 and then Has_Non_Null_Base_Init_Proc (Typ)
2538 Build_Initialization_Call
2540 Make_Selected_Component (Loc,
2541 Prefix => Make_Identifier (Loc, Name_uInit),
2542 Selector_Name => New_Occurrence_Of (Id, Loc)),
2544 In_Init_Proc => True,
2545 Enclos_Type => Rec_Type,
2546 Discr_Map => Discr_Map);
2548 Clean_Task_Names (Typ, Proc_Id);
2550 -- Case of component needing simple initialization
2552 elsif Component_Needs_Simple_Initialization (Typ) then
2555 (Id, Get_Simple_Init_Val (Typ, Loc, Esize (Id)));
2557 -- Nothing needed for this case
2563 if Present (Check_List) then
2564 Append_List_To (Statement_List, Check_List);
2567 if Present (Stmts) then
2569 -- Add the initialization of the record controller before
2570 -- the _Parent field is attached to it when the attachment
2571 -- can occur. It does not work to simply initialize the
2572 -- controller first: it must be initialized after the parent
2573 -- if the parent holds discriminants that can be used to
2574 -- compute the offset of the controller. We assume here that
2575 -- the last statement of the initialization call is the
2576 -- attachment of the parent (see Build_Initialization_Call)
2578 if Chars (Id) = Name_uController
2579 and then Rec_Type /= Etype (Rec_Type)
2580 and then Has_Controlled_Component (Etype (Rec_Type))
2581 and then Has_New_Controlled_Component (Rec_Type)
2582 and then Present (Last (Statement_List))
2584 Insert_List_Before (Last (Statement_List), Stmts);
2586 Append_List_To (Statement_List, Stmts);
2591 Next_Non_Pragma (Decl);
2594 if Per_Object_Constraint_Components then
2596 -- Second pass: components with per-object constraints
2598 Decl := First_Non_Pragma (Component_Items (Comp_List));
2599 while Present (Decl) loop
2601 Id := Defining_Identifier (Decl);
2604 if Has_Access_Constraint (Id)
2605 and then No (Expression (Decl))
2607 if Has_Non_Null_Base_Init_Proc (Typ) then
2608 Append_List_To (Statement_List,
2609 Build_Initialization_Call (Loc,
2610 Make_Selected_Component (Loc,
2611 Prefix => Make_Identifier (Loc, Name_uInit),
2612 Selector_Name => New_Occurrence_Of (Id, Loc)),
2614 In_Init_Proc => True,
2615 Enclos_Type => Rec_Type,
2616 Discr_Map => Discr_Map));
2618 Clean_Task_Names (Typ, Proc_Id);
2620 elsif Component_Needs_Simple_Initialization (Typ) then
2621 Append_List_To (Statement_List,
2623 (Id, Get_Simple_Init_Val (Typ, Loc, Esize (Id))));
2627 Next_Non_Pragma (Decl);
2631 -- Process the variant part
2633 if Present (Variant_Part (Comp_List)) then
2634 Alt_List := New_List;
2635 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2636 while Present (Variant) loop
2637 Loc := Sloc (Variant);
2638 Append_To (Alt_List,
2639 Make_Case_Statement_Alternative (Loc,
2641 New_Copy_List (Discrete_Choices (Variant)),
2643 Build_Init_Statements (Component_List (Variant))));
2644 Next_Non_Pragma (Variant);
2647 -- The expression of the case statement which is a reference
2648 -- to one of the discriminants is replaced by the appropriate
2649 -- formal parameter of the initialization procedure.
2651 Append_To (Statement_List,
2652 Make_Case_Statement (Loc,
2654 New_Reference_To (Discriminal (
2655 Entity (Name (Variant_Part (Comp_List)))), Loc),
2656 Alternatives => Alt_List));
2659 -- For a task record type, add the task create call and calls
2660 -- to bind any interrupt (signal) entries.
2662 if Is_Task_Record_Type (Rec_Type) then
2664 -- In the case of the restricted run time the ATCB has already
2665 -- been preallocated.
2667 if Restricted_Profile then
2668 Append_To (Statement_List,
2669 Make_Assignment_Statement (Loc,
2670 Name => Make_Selected_Component (Loc,
2671 Prefix => Make_Identifier (Loc, Name_uInit),
2672 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2673 Expression => Make_Attribute_Reference (Loc,
2675 Make_Selected_Component (Loc,
2676 Prefix => Make_Identifier (Loc, Name_uInit),
2678 Make_Identifier (Loc, Name_uATCB)),
2679 Attribute_Name => Name_Unchecked_Access)));
2682 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2685 Task_Type : constant Entity_Id :=
2686 Corresponding_Concurrent_Type (Rec_Type);
2687 Task_Decl : constant Node_Id := Parent (Task_Type);
2688 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2693 if Present (Task_Def) then
2694 Vis_Decl := First (Visible_Declarations (Task_Def));
2695 while Present (Vis_Decl) loop
2696 Loc := Sloc (Vis_Decl);
2698 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2699 if Get_Attribute_Id (Chars (Vis_Decl)) =
2702 Ent := Entity (Name (Vis_Decl));
2704 if Ekind (Ent) = E_Entry then
2705 Append_To (Statement_List,
2706 Make_Procedure_Call_Statement (Loc,
2707 Name => New_Reference_To (
2708 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2709 Parameter_Associations => New_List (
2710 Make_Selected_Component (Loc,
2712 Make_Identifier (Loc, Name_uInit),
2714 Make_Identifier (Loc, Name_uTask_Id)),
2715 Entry_Index_Expression (
2716 Loc, Ent, Empty, Task_Type),
2717 Expression (Vis_Decl))));
2728 -- For a protected type, add statements generated by
2729 -- Make_Initialize_Protection.
2731 if Is_Protected_Record_Type (Rec_Type) then
2732 Append_List_To (Statement_List,
2733 Make_Initialize_Protection (Rec_Type));
2736 -- If no initializations when generated for component declarations
2737 -- corresponding to this Statement_List, append a null statement
2738 -- to the Statement_List to make it a valid Ada tree.
2740 if Is_Empty_List (Statement_List) then
2741 Append (New_Node (N_Null_Statement, Loc), Statement_List);
2744 return Statement_List;
2747 when RE_Not_Available =>
2749 end Build_Init_Statements;
2751 -------------------------
2752 -- Build_Record_Checks --
2753 -------------------------
2755 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2756 Subtype_Mark_Id : Entity_Id;
2759 if Nkind (S) = N_Subtype_Indication then
2760 Find_Type (Subtype_Mark (S));
2761 Subtype_Mark_Id := Entity (Subtype_Mark (S));
2763 -- Remaining processing depends on type
2765 case Ekind (Subtype_Mark_Id) is
2768 Constrain_Array (S, Check_List);
2774 end Build_Record_Checks;
2776 -------------------------------------------
2777 -- Component_Needs_Simple_Initialization --
2778 -------------------------------------------
2780 function Component_Needs_Simple_Initialization
2781 (T : Entity_Id) return Boolean
2785 Needs_Simple_Initialization (T)
2786 and then not Is_RTE (T, RE_Tag)
2788 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
2790 and then not Is_RTE (T, RE_Interface_Tag);
2791 end Component_Needs_Simple_Initialization;
2793 ---------------------
2794 -- Constrain_Array --
2795 ---------------------
2797 procedure Constrain_Array
2799 Check_List : List_Id)
2801 C : constant Node_Id := Constraint (SI);
2802 Number_Of_Constraints : Nat := 0;
2807 T := Entity (Subtype_Mark (SI));
2809 if Ekind (T) in Access_Kind then
2810 T := Designated_Type (T);
2813 S := First (Constraints (C));
2815 while Present (S) loop
2816 Number_Of_Constraints := Number_Of_Constraints + 1;
2820 -- In either case, the index constraint must provide a discrete
2821 -- range for each index of the array type and the type of each
2822 -- discrete range must be the same as that of the corresponding
2823 -- index. (RM 3.6.1)
2825 S := First (Constraints (C));
2826 Index := First_Index (T);
2829 -- Apply constraints to each index type
2831 for J in 1 .. Number_Of_Constraints loop
2832 Constrain_Index (Index, S, Check_List);
2837 end Constrain_Array;
2839 ---------------------
2840 -- Constrain_Index --
2841 ---------------------
2843 procedure Constrain_Index
2846 Check_List : List_Id)
2848 T : constant Entity_Id := Etype (Index);
2851 if Nkind (S) = N_Range then
2852 Process_Range_Expr_In_Decl (S, T, Check_List);
2854 end Constrain_Index;
2856 --------------------------------------
2857 -- Parent_Subtype_Renaming_Discrims --
2858 --------------------------------------
2860 function Parent_Subtype_Renaming_Discrims return Boolean is
2865 if Base_Type (Pe) /= Pe then
2870 or else not Has_Discriminants (Pe)
2871 or else Is_Constrained (Pe)
2872 or else Is_Tagged_Type (Pe)
2877 -- If there are no explicit stored discriminants we have inherited
2878 -- the root type discriminants so far, so no renamings occurred.
2880 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
2884 -- Check if we have done some trivial renaming of the parent
2885 -- discriminants, i.e. something like
2887 -- type DT (X1,X2: int) is new PT (X1,X2);
2889 De := First_Discriminant (Pe);
2890 Dp := First_Discriminant (Etype (Pe));
2892 while Present (De) loop
2893 pragma Assert (Present (Dp));
2895 if Corresponding_Discriminant (De) /= Dp then
2899 Next_Discriminant (De);
2900 Next_Discriminant (Dp);
2903 return Present (Dp);
2904 end Parent_Subtype_Renaming_Discrims;
2906 ------------------------
2907 -- Requires_Init_Proc --
2908 ------------------------
2910 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
2911 Comp_Decl : Node_Id;
2916 -- Definitely do not need one if specifically suppressed
2918 if Suppress_Init_Proc (Rec_Id) then
2922 -- If it is a type derived from a type with unknown discriminants,
2923 -- we cannot build an initialization procedure for it.
2925 if Has_Unknown_Discriminants (Rec_Id) then
2929 -- Otherwise we need to generate an initialization procedure if
2930 -- Is_CPP_Class is False and at least one of the following applies:
2932 -- 1. Discriminants are present, since they need to be initialized
2933 -- with the appropriate discriminant constraint expressions.
2934 -- However, the discriminant of an unchecked union does not
2935 -- count, since the discriminant is not present.
2937 -- 2. The type is a tagged type, since the implicit Tag component
2938 -- needs to be initialized with a pointer to the dispatch table.
2940 -- 3. The type contains tasks
2942 -- 4. One or more components has an initial value
2944 -- 5. One or more components is for a type which itself requires
2945 -- an initialization procedure.
2947 -- 6. One or more components is a type that requires simple
2948 -- initialization (see Needs_Simple_Initialization), except
2949 -- that types Tag and Interface_Tag are excluded, since fields
2950 -- of these types are initialized by other means.
2952 -- 7. The type is the record type built for a task type (since at
2953 -- the very least, Create_Task must be called)
2955 -- 8. The type is the record type built for a protected type (since
2956 -- at least Initialize_Protection must be called)
2958 -- 9. The type is marked as a public entity. The reason we add this
2959 -- case (even if none of the above apply) is to properly handle
2960 -- Initialize_Scalars. If a package is compiled without an IS
2961 -- pragma, and the client is compiled with an IS pragma, then
2962 -- the client will think an initialization procedure is present
2963 -- and call it, when in fact no such procedure is required, but
2964 -- since the call is generated, there had better be a routine
2965 -- at the other end of the call, even if it does nothing!)
2967 -- Note: the reason we exclude the CPP_Class case is because in this
2968 -- case the initialization is performed in the C++ side.
2970 if Is_CPP_Class (Rec_Id) then
2973 elsif Is_Interface (Rec_Id) then
2976 elsif not Restriction_Active (No_Initialize_Scalars)
2977 and then Is_Public (Rec_Id)
2981 elsif (Has_Discriminants (Rec_Id)
2982 and then not Is_Unchecked_Union (Rec_Id))
2983 or else Is_Tagged_Type (Rec_Id)
2984 or else Is_Concurrent_Record_Type (Rec_Id)
2985 or else Has_Task (Rec_Id)
2990 Id := First_Component (Rec_Id);
2992 while Present (Id) loop
2993 Comp_Decl := Parent (Id);
2996 if Present (Expression (Comp_Decl))
2997 or else Has_Non_Null_Base_Init_Proc (Typ)
2998 or else Component_Needs_Simple_Initialization (Typ)
3003 Next_Component (Id);
3007 end Requires_Init_Proc;
3009 -- Start of processing for Build_Record_Init_Proc
3012 Rec_Type := Defining_Identifier (N);
3014 if Is_Value_Type (Rec_Type) then
3018 -- This may be full declaration of a private type, in which case
3019 -- the visible entity is a record, and the private entity has been
3020 -- exchanged with it in the private part of the current package.
3021 -- The initialization procedure is built for the record type, which
3022 -- is retrievable from the private entity.
3024 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3025 Rec_Type := Underlying_Type (Rec_Type);
3028 -- If there are discriminants, build the discriminant map to replace
3029 -- discriminants by their discriminals in complex bound expressions.
3030 -- These only arise for the corresponding records of protected types.
3032 if Is_Concurrent_Record_Type (Rec_Type)
3033 and then Has_Discriminants (Rec_Type)
3038 Disc := First_Discriminant (Rec_Type);
3039 while Present (Disc) loop
3040 Append_Elmt (Disc, Discr_Map);
3041 Append_Elmt (Discriminal (Disc), Discr_Map);
3042 Next_Discriminant (Disc);
3047 -- Derived types that have no type extension can use the initialization
3048 -- procedure of their parent and do not need a procedure of their own.
3049 -- This is only correct if there are no representation clauses for the
3050 -- type or its parent, and if the parent has in fact been frozen so
3051 -- that its initialization procedure exists.
3053 if Is_Derived_Type (Rec_Type)
3054 and then not Is_Tagged_Type (Rec_Type)
3055 and then not Is_Unchecked_Union (Rec_Type)
3056 and then not Has_New_Non_Standard_Rep (Rec_Type)
3057 and then not Parent_Subtype_Renaming_Discrims
3058 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3060 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3062 -- Otherwise if we need an initialization procedure, then build one,
3063 -- mark it as public and inlinable and as having a completion.
3065 elsif Requires_Init_Proc (Rec_Type)
3066 or else Is_Unchecked_Union (Rec_Type)
3068 Build_Offset_To_Top_Functions;
3069 Build_Init_Procedure;
3070 Set_Is_Public (Proc_Id, Is_Public (Pe));
3072 -- The initialization of protected records is not worth inlining.
3073 -- In addition, when compiled for another unit for inlining purposes,
3074 -- it may make reference to entities that have not been elaborated
3075 -- yet. The initialization of controlled records contains a nested
3076 -- clean-up procedure that makes it impractical to inline as well,
3077 -- and leads to undefined symbols if inlined in a different unit.
3078 -- Similar considerations apply to task types.
3080 if not Is_Concurrent_Type (Rec_Type)
3081 and then not Has_Task (Rec_Type)
3082 and then not Controlled_Type (Rec_Type)
3084 Set_Is_Inlined (Proc_Id);
3087 Set_Is_Internal (Proc_Id);
3088 Set_Has_Completion (Proc_Id);
3090 if not Debug_Generated_Code then
3091 Set_Debug_Info_Off (Proc_Id);
3094 Set_Static_Initialization
3095 (Proc_Id, Build_Equivalent_Record_Aggregate (Rec_Type));
3097 end Build_Record_Init_Proc;
3099 ----------------------------
3100 -- Build_Slice_Assignment --
3101 ----------------------------
3103 -- Generates the following subprogram:
3106 -- (Source, Target : Array_Type,
3107 -- Left_Lo, Left_Hi : Index;
3108 -- Right_Lo, Right_Hi : Index;
3125 -- exit when Li1 < Left_Lo;
3127 -- exit when Li1 > Left_Hi;
3130 -- Target (Li1) := Source (Ri1);
3133 -- Li1 := Index'pred (Li1);
3134 -- Ri1 := Index'pred (Ri1);
3136 -- Li1 := Index'succ (Li1);
3137 -- Ri1 := Index'succ (Ri1);
3142 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3143 Loc : constant Source_Ptr := Sloc (Typ);
3144 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3146 -- Build formal parameters of procedure
3148 Larray : constant Entity_Id :=
3149 Make_Defining_Identifier
3150 (Loc, Chars => New_Internal_Name ('A'));
3151 Rarray : constant Entity_Id :=
3152 Make_Defining_Identifier
3153 (Loc, Chars => New_Internal_Name ('R'));
3154 Left_Lo : constant Entity_Id :=
3155 Make_Defining_Identifier
3156 (Loc, Chars => New_Internal_Name ('L'));
3157 Left_Hi : constant Entity_Id :=
3158 Make_Defining_Identifier
3159 (Loc, Chars => New_Internal_Name ('L'));
3160 Right_Lo : constant Entity_Id :=
3161 Make_Defining_Identifier
3162 (Loc, Chars => New_Internal_Name ('R'));
3163 Right_Hi : constant Entity_Id :=
3164 Make_Defining_Identifier
3165 (Loc, Chars => New_Internal_Name ('R'));
3166 Rev : constant Entity_Id :=
3167 Make_Defining_Identifier
3168 (Loc, Chars => New_Internal_Name ('D'));
3169 Proc_Name : constant Entity_Id :=
3170 Make_Defining_Identifier (Loc,
3171 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3173 Lnn : constant Entity_Id :=
3174 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3175 Rnn : constant Entity_Id :=
3176 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3177 -- Subscripts for left and right sides
3184 -- Build declarations for indices
3189 Make_Object_Declaration (Loc,
3190 Defining_Identifier => Lnn,
3191 Object_Definition =>
3192 New_Occurrence_Of (Index, Loc)));
3195 Make_Object_Declaration (Loc,
3196 Defining_Identifier => Rnn,
3197 Object_Definition =>
3198 New_Occurrence_Of (Index, Loc)));
3202 -- Build initializations for indices
3205 F_Init : constant List_Id := New_List;
3206 B_Init : constant List_Id := New_List;
3210 Make_Assignment_Statement (Loc,
3211 Name => New_Occurrence_Of (Lnn, Loc),
3212 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3215 Make_Assignment_Statement (Loc,
3216 Name => New_Occurrence_Of (Rnn, Loc),
3217 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3220 Make_Assignment_Statement (Loc,
3221 Name => New_Occurrence_Of (Lnn, Loc),
3222 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3225 Make_Assignment_Statement (Loc,
3226 Name => New_Occurrence_Of (Rnn, Loc),
3227 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3230 Make_If_Statement (Loc,
3231 Condition => New_Occurrence_Of (Rev, Loc),
3232 Then_Statements => B_Init,
3233 Else_Statements => F_Init));
3236 -- Now construct the assignment statement
3239 Make_Loop_Statement (Loc,
3240 Statements => New_List (
3241 Make_Assignment_Statement (Loc,
3243 Make_Indexed_Component (Loc,
3244 Prefix => New_Occurrence_Of (Larray, Loc),
3245 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3247 Make_Indexed_Component (Loc,
3248 Prefix => New_Occurrence_Of (Rarray, Loc),
3249 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3250 End_Label => Empty);
3252 -- Build exit condition
3255 F_Ass : constant List_Id := New_List;
3256 B_Ass : constant List_Id := New_List;
3260 Make_Exit_Statement (Loc,
3263 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3264 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3267 Make_Exit_Statement (Loc,
3270 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3271 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3273 Prepend_To (Statements (Loops),
3274 Make_If_Statement (Loc,
3275 Condition => New_Occurrence_Of (Rev, Loc),
3276 Then_Statements => B_Ass,
3277 Else_Statements => F_Ass));
3280 -- Build the increment/decrement statements
3283 F_Ass : constant List_Id := New_List;
3284 B_Ass : constant List_Id := New_List;
3288 Make_Assignment_Statement (Loc,
3289 Name => New_Occurrence_Of (Lnn, Loc),
3291 Make_Attribute_Reference (Loc,
3293 New_Occurrence_Of (Index, Loc),
3294 Attribute_Name => Name_Succ,
3295 Expressions => New_List (
3296 New_Occurrence_Of (Lnn, Loc)))));
3299 Make_Assignment_Statement (Loc,
3300 Name => New_Occurrence_Of (Rnn, Loc),
3302 Make_Attribute_Reference (Loc,
3304 New_Occurrence_Of (Index, Loc),
3305 Attribute_Name => Name_Succ,
3306 Expressions => New_List (
3307 New_Occurrence_Of (Rnn, Loc)))));
3310 Make_Assignment_Statement (Loc,
3311 Name => New_Occurrence_Of (Lnn, Loc),
3313 Make_Attribute_Reference (Loc,
3315 New_Occurrence_Of (Index, Loc),
3316 Attribute_Name => Name_Pred,
3317 Expressions => New_List (
3318 New_Occurrence_Of (Lnn, Loc)))));
3321 Make_Assignment_Statement (Loc,
3322 Name => New_Occurrence_Of (Rnn, Loc),
3324 Make_Attribute_Reference (Loc,
3326 New_Occurrence_Of (Index, Loc),
3327 Attribute_Name => Name_Pred,
3328 Expressions => New_List (
3329 New_Occurrence_Of (Rnn, Loc)))));
3331 Append_To (Statements (Loops),
3332 Make_If_Statement (Loc,
3333 Condition => New_Occurrence_Of (Rev, Loc),
3334 Then_Statements => B_Ass,
3335 Else_Statements => F_Ass));
3338 Append_To (Stats, Loops);
3342 Formals : List_Id := New_List;
3345 Formals := New_List (
3346 Make_Parameter_Specification (Loc,
3347 Defining_Identifier => Larray,
3348 Out_Present => True,
3350 New_Reference_To (Base_Type (Typ), Loc)),
3352 Make_Parameter_Specification (Loc,
3353 Defining_Identifier => Rarray,
3355 New_Reference_To (Base_Type (Typ), Loc)),
3357 Make_Parameter_Specification (Loc,
3358 Defining_Identifier => Left_Lo,
3360 New_Reference_To (Index, Loc)),
3362 Make_Parameter_Specification (Loc,
3363 Defining_Identifier => Left_Hi,
3365 New_Reference_To (Index, Loc)),
3367 Make_Parameter_Specification (Loc,
3368 Defining_Identifier => Right_Lo,
3370 New_Reference_To (Index, Loc)),
3372 Make_Parameter_Specification (Loc,
3373 Defining_Identifier => Right_Hi,
3375 New_Reference_To (Index, Loc)));
3378 Make_Parameter_Specification (Loc,
3379 Defining_Identifier => Rev,
3381 New_Reference_To (Standard_Boolean, Loc)));
3384 Make_Procedure_Specification (Loc,
3385 Defining_Unit_Name => Proc_Name,
3386 Parameter_Specifications => Formals);
3389 Make_Subprogram_Body (Loc,
3390 Specification => Spec,
3391 Declarations => Decls,
3392 Handled_Statement_Sequence =>
3393 Make_Handled_Sequence_Of_Statements (Loc,
3394 Statements => Stats)));
3397 Set_TSS (Typ, Proc_Name);
3398 Set_Is_Pure (Proc_Name);
3399 end Build_Slice_Assignment;
3401 ------------------------------------
3402 -- Build_Variant_Record_Equality --
3403 ------------------------------------
3407 -- function _Equality (X, Y : T) return Boolean is
3409 -- -- Compare discriminants
3411 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3415 -- -- Compare components
3417 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3421 -- -- Compare variant part
3425 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3430 -- if False or else X.Cn /= Y.Cn then
3438 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3439 Loc : constant Source_Ptr := Sloc (Typ);
3441 F : constant Entity_Id :=
3442 Make_Defining_Identifier (Loc,
3443 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3445 X : constant Entity_Id :=
3446 Make_Defining_Identifier (Loc,
3449 Y : constant Entity_Id :=
3450 Make_Defining_Identifier (Loc,
3453 Def : constant Node_Id := Parent (Typ);
3454 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3455 Stmts : constant List_Id := New_List;
3456 Pspecs : constant List_Id := New_List;
3459 -- Derived Unchecked_Union types no longer inherit the equality function
3462 if Is_Derived_Type (Typ)
3463 and then not Is_Unchecked_Union (Typ)
3464 and then not Has_New_Non_Standard_Rep (Typ)
3467 Parent_Eq : constant Entity_Id :=
3468 TSS (Root_Type (Typ), TSS_Composite_Equality);
3471 if Present (Parent_Eq) then
3472 Copy_TSS (Parent_Eq, Typ);
3479 Make_Subprogram_Body (Loc,
3481 Make_Function_Specification (Loc,
3482 Defining_Unit_Name => F,
3483 Parameter_Specifications => Pspecs,
3484 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3485 Declarations => New_List,
3486 Handled_Statement_Sequence =>
3487 Make_Handled_Sequence_Of_Statements (Loc,
3488 Statements => Stmts)));
3491 Make_Parameter_Specification (Loc,
3492 Defining_Identifier => X,
3493 Parameter_Type => New_Reference_To (Typ, Loc)));
3496 Make_Parameter_Specification (Loc,
3497 Defining_Identifier => Y,
3498 Parameter_Type => New_Reference_To (Typ, Loc)));
3500 -- Unchecked_Unions require additional machinery to support equality.
3501 -- Two extra parameters (A and B) are added to the equality function
3502 -- parameter list in order to capture the inferred values of the
3503 -- discriminants in later calls.
3505 if Is_Unchecked_Union (Typ) then
3507 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3509 A : constant Node_Id :=
3510 Make_Defining_Identifier (Loc,
3513 B : constant Node_Id :=
3514 Make_Defining_Identifier (Loc,
3518 -- Add A and B to the parameter list
3521 Make_Parameter_Specification (Loc,
3522 Defining_Identifier => A,
3523 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3526 Make_Parameter_Specification (Loc,
3527 Defining_Identifier => B,
3528 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3530 -- Generate the following header code to compare the inferred
3538 Make_If_Statement (Loc,
3541 Left_Opnd => New_Reference_To (A, Loc),
3542 Right_Opnd => New_Reference_To (B, Loc)),
3543 Then_Statements => New_List (
3544 Make_Simple_Return_Statement (Loc,
3545 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3547 -- Generate component-by-component comparison. Note that we must
3548 -- propagate one of the inferred discriminant formals to act as
3549 -- the case statement switch.
3551 Append_List_To (Stmts,
3552 Make_Eq_Case (Typ, Comps, A));
3556 -- Normal case (not unchecked union)
3561 Discriminant_Specifications (Def)));
3563 Append_List_To (Stmts,
3564 Make_Eq_Case (Typ, Comps));
3568 Make_Simple_Return_Statement (Loc,
3569 Expression => New_Reference_To (Standard_True, Loc)));
3574 if not Debug_Generated_Code then
3575 Set_Debug_Info_Off (F);
3577 end Build_Variant_Record_Equality;
3579 -----------------------------
3580 -- Check_Stream_Attributes --
3581 -----------------------------
3583 procedure Check_Stream_Attributes (Typ : Entity_Id) is
3585 Par_Read : constant Boolean :=
3586 Stream_Attribute_Available (Typ, TSS_Stream_Read)
3587 and then not Has_Specified_Stream_Read (Typ);
3588 Par_Write : constant Boolean :=
3589 Stream_Attribute_Available (Typ, TSS_Stream_Write)
3590 and then not Has_Specified_Stream_Write (Typ);
3592 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3593 -- Check that Comp has a user-specified Nam stream attribute
3599 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3601 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3602 Error_Msg_Name_1 := Nam;
3604 ("|component& in limited extension must have% attribute", Comp);
3608 -- Start of processing for Check_Stream_Attributes
3611 if Par_Read or else Par_Write then
3612 Comp := First_Component (Typ);
3613 while Present (Comp) loop
3614 if Comes_From_Source (Comp)
3615 and then Original_Record_Component (Comp) = Comp
3616 and then Is_Limited_Type (Etype (Comp))
3619 Check_Attr (Name_Read, TSS_Stream_Read);
3623 Check_Attr (Name_Write, TSS_Stream_Write);
3627 Next_Component (Comp);
3630 end Check_Stream_Attributes;
3632 -----------------------------
3633 -- Expand_Record_Extension --
3634 -----------------------------
3636 -- Add a field _parent at the beginning of the record extension. This is
3637 -- used to implement inheritance. Here are some examples of expansion:
3639 -- 1. no discriminants
3640 -- type T2 is new T1 with null record;
3642 -- type T2 is new T1 with record
3646 -- 2. renamed discriminants
3647 -- type T2 (B, C : Int) is new T1 (A => B) with record
3648 -- _Parent : T1 (A => B);
3652 -- 3. inherited discriminants
3653 -- type T2 is new T1 with record -- discriminant A inherited
3654 -- _Parent : T1 (A);
3658 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
3659 Indic : constant Node_Id := Subtype_Indication (Def);
3660 Loc : constant Source_Ptr := Sloc (Def);
3661 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
3662 Par_Subtype : Entity_Id;
3663 Comp_List : Node_Id;
3664 Comp_Decl : Node_Id;
3667 List_Constr : constant List_Id := New_List;
3670 -- Expand_Record_Extension is called directly from the semantics, so
3671 -- we must check to see whether expansion is active before proceeding
3673 if not Expander_Active then
3677 -- This may be a derivation of an untagged private type whose full
3678 -- view is tagged, in which case the Derived_Type_Definition has no
3679 -- extension part. Build an empty one now.
3681 if No (Rec_Ext_Part) then
3683 Make_Record_Definition (Loc,
3685 Component_List => Empty,
3686 Null_Present => True);
3688 Set_Record_Extension_Part (Def, Rec_Ext_Part);
3689 Mark_Rewrite_Insertion (Rec_Ext_Part);
3692 Comp_List := Component_List (Rec_Ext_Part);
3694 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
3696 -- If the derived type inherits its discriminants the type of the
3697 -- _parent field must be constrained by the inherited discriminants
3699 if Has_Discriminants (T)
3700 and then Nkind (Indic) /= N_Subtype_Indication
3701 and then not Is_Constrained (Entity (Indic))
3703 D := First_Discriminant (T);
3704 while Present (D) loop
3705 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
3706 Next_Discriminant (D);
3711 Make_Subtype_Indication (Loc,
3712 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
3714 Make_Index_Or_Discriminant_Constraint (Loc,
3715 Constraints => List_Constr)),
3718 -- Otherwise the original subtype_indication is just what is needed
3721 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
3724 Set_Parent_Subtype (T, Par_Subtype);
3727 Make_Component_Declaration (Loc,
3728 Defining_Identifier => Parent_N,
3729 Component_Definition =>
3730 Make_Component_Definition (Loc,
3731 Aliased_Present => False,
3732 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
3734 if Null_Present (Rec_Ext_Part) then
3735 Set_Component_List (Rec_Ext_Part,
3736 Make_Component_List (Loc,
3737 Component_Items => New_List (Comp_Decl),
3738 Variant_Part => Empty,
3739 Null_Present => False));
3740 Set_Null_Present (Rec_Ext_Part, False);
3742 elsif Null_Present (Comp_List)
3743 or else Is_Empty_List (Component_Items (Comp_List))
3745 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3746 Set_Null_Present (Comp_List, False);
3749 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
3752 Analyze (Comp_Decl);
3753 end Expand_Record_Extension;
3755 ------------------------------------
3756 -- Expand_N_Full_Type_Declaration --
3757 ------------------------------------
3759 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
3760 Def_Id : constant Entity_Id := Defining_Identifier (N);
3761 B_Id : constant Entity_Id := Base_Type (Def_Id);
3765 procedure Build_Master (Def_Id : Entity_Id);
3766 -- Create the master associated with Def_Id
3772 procedure Build_Master (Def_Id : Entity_Id) is
3774 -- Anonymous access types are created for the components of the
3775 -- record parameter for an entry declaration. No master is created
3778 if Has_Task (Designated_Type (Def_Id))
3779 and then Comes_From_Source (N)
3781 Build_Master_Entity (Def_Id);
3782 Build_Master_Renaming (Parent (Def_Id), Def_Id);
3784 -- Create a class-wide master because a Master_Id must be generated
3785 -- for access-to-limited-class-wide types whose root may be extended
3786 -- with task components, and for access-to-limited-interfaces because
3787 -- they can be used to reference tasks implementing such interface.
3789 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
3790 and then (Is_Limited_Type (Designated_Type (Def_Id))
3792 (Is_Interface (Designated_Type (Def_Id))
3794 Is_Limited_Interface (Designated_Type (Def_Id))))
3795 and then Tasking_Allowed
3797 -- Do not create a class-wide master for types whose convention is
3798 -- Java since these types cannot embed Ada tasks anyway. Note that
3799 -- the following test cannot catch the following case:
3801 -- package java.lang.Object is
3802 -- type Typ is tagged limited private;
3803 -- type Ref is access all Typ'Class;
3805 -- type Typ is tagged limited ...;
3806 -- pragma Convention (Typ, Java)
3809 -- Because the convention appears after we have done the
3810 -- processing for type Ref.
3812 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
3813 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
3815 Build_Class_Wide_Master (Def_Id);
3819 -- Start of processing for Expand_N_Full_Type_Declaration
3822 if Is_Access_Type (Def_Id) then
3823 Build_Master (Def_Id);
3825 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
3826 Expand_Access_Protected_Subprogram_Type (N);
3829 elsif Ada_Version >= Ada_05
3830 and then Is_Array_Type (Def_Id)
3831 and then Is_Access_Type (Component_Type (Def_Id))
3832 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
3834 Build_Master (Component_Type (Def_Id));
3836 elsif Has_Task (Def_Id) then
3837 Expand_Previous_Access_Type (Def_Id);
3839 elsif Ada_Version >= Ada_05
3841 (Is_Record_Type (Def_Id)
3842 or else (Is_Array_Type (Def_Id)
3843 and then Is_Record_Type (Component_Type (Def_Id))))
3851 -- Look for the first anonymous access type component
3853 if Is_Array_Type (Def_Id) then
3854 Comp := First_Entity (Component_Type (Def_Id));
3856 Comp := First_Entity (Def_Id);
3859 while Present (Comp) loop
3860 Typ := Etype (Comp);
3862 exit when Is_Access_Type (Typ)
3863 and then Ekind (Typ) = E_Anonymous_Access_Type;
3868 -- If found we add a renaming declaration of master_id and we
3869 -- associate it to each anonymous access type component. Do
3870 -- nothing if the access type already has a master. This will be
3871 -- the case if the array type is the packed array created for a
3872 -- user-defined array type T, where the master_id is created when
3873 -- expanding the declaration for T.
3876 and then Ekind (Typ) = E_Anonymous_Access_Type
3877 and then not Restriction_Active (No_Task_Hierarchy)
3878 and then No (Master_Id (Typ))
3880 -- Do not consider run-times with no tasking support
3882 and then RTE_Available (RE_Current_Master)
3883 and then Has_Task (Non_Limited_Designated_Type (Typ))
3885 Build_Master_Entity (Def_Id);
3886 M_Id := Build_Master_Renaming (N, Def_Id);
3888 if Is_Array_Type (Def_Id) then
3889 Comp := First_Entity (Component_Type (Def_Id));
3891 Comp := First_Entity (Def_Id);
3894 while Present (Comp) loop
3895 Typ := Etype (Comp);
3897 if Is_Access_Type (Typ)
3898 and then Ekind (Typ) = E_Anonymous_Access_Type
3900 Set_Master_Id (Typ, M_Id);
3909 Par_Id := Etype (B_Id);
3911 -- The parent type is private then we need to inherit any TSS operations
3912 -- from the full view.
3914 if Ekind (Par_Id) in Private_Kind
3915 and then Present (Full_View (Par_Id))
3917 Par_Id := Base_Type (Full_View (Par_Id));
3920 if Nkind (Type_Definition (Original_Node (N))) =
3921 N_Derived_Type_Definition
3922 and then not Is_Tagged_Type (Def_Id)
3923 and then Present (Freeze_Node (Par_Id))
3924 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
3926 Ensure_Freeze_Node (B_Id);
3927 FN := Freeze_Node (B_Id);
3929 if No (TSS_Elist (FN)) then
3930 Set_TSS_Elist (FN, New_Elmt_List);
3934 T_E : constant Elist_Id := TSS_Elist (FN);
3938 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
3939 while Present (Elmt) loop
3940 if Chars (Node (Elmt)) /= Name_uInit then
3941 Append_Elmt (Node (Elmt), T_E);
3947 -- If the derived type itself is private with a full view, then
3948 -- associate the full view with the inherited TSS_Elist as well.
3950 if Ekind (B_Id) in Private_Kind
3951 and then Present (Full_View (B_Id))
3953 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
3955 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
3959 end Expand_N_Full_Type_Declaration;
3961 ---------------------------------
3962 -- Expand_N_Object_Declaration --
3963 ---------------------------------
3965 -- First we do special processing for objects of a tagged type where this
3966 -- is the point at which the type is frozen. The creation of the dispatch
3967 -- table and the initialization procedure have to be deferred to this
3968 -- point, since we reference previously declared primitive subprograms.
3970 -- For all types, we call an initialization procedure if there is one
3972 procedure Expand_N_Object_Declaration (N : Node_Id) is
3973 Def_Id : constant Entity_Id := Defining_Identifier (N);
3974 Expr : constant Node_Id := Expression (N);
3975 Loc : constant Source_Ptr := Sloc (N);
3976 Typ : constant Entity_Id := Etype (Def_Id);
3980 BIP_Call : Boolean := False;
3983 -- Don't do anything for deferred constants. All proper actions will
3984 -- be expanded during the full declaration.
3986 if No (Expr) and Constant_Present (N) then
3990 -- Force construction of dispatch tables of library level tagged types
3992 if VM_Target = No_VM
3993 and then Static_Dispatch_Tables
3994 and then Is_Library_Level_Entity (Def_Id)
3995 and then Is_Library_Level_Tagged_Type (Typ)
3996 and then (Ekind (Typ) = E_Record_Type
3997 or else Ekind (Typ) = E_Protected_Type
3998 or else Ekind (Typ) = E_Task_Type)
3999 and then not Has_Dispatch_Table (Typ)
4002 New_Nodes : List_Id := No_List;
4005 if Is_Concurrent_Type (Typ) then
4006 New_Nodes := Make_DT (Corresponding_Record_Type (Typ), N);
4008 New_Nodes := Make_DT (Typ, N);
4011 if not Is_Empty_List (New_Nodes) then
4012 Insert_List_Before (N, New_Nodes);
4017 -- Make shared memory routines for shared passive variable
4019 if Is_Shared_Passive (Def_Id) then
4020 Make_Shared_Var_Procs (N);
4023 -- If tasks being declared, make sure we have an activation chain
4024 -- defined for the tasks (has no effect if we already have one), and
4025 -- also that a Master variable is established and that the appropriate
4026 -- enclosing construct is established as a task master.
4028 if Has_Task (Typ) then
4029 Build_Activation_Chain_Entity (N);
4030 Build_Master_Entity (Def_Id);
4033 -- Build a list controller for declarations where the type is anonymous
4034 -- access and the designated type is controlled. Only declarations from
4035 -- source files receive such controllers in order to provide the same
4036 -- lifespan for any potential coextensions that may be associated with
4037 -- the object. Finalization lists of internal controlled anonymous
4038 -- access objects are already handled in Expand_N_Allocator.
4040 if Comes_From_Source (N)
4041 and then Ekind (Typ) = E_Anonymous_Access_Type
4042 and then Is_Controlled (Directly_Designated_Type (Typ))
4043 and then No (Associated_Final_Chain (Typ))
4045 Build_Final_List (N, Typ);
4048 -- Default initialization required, and no expression present
4052 -- Expand Initialize call for controlled objects. One may wonder why
4053 -- the Initialize Call is not done in the regular Init procedure
4054 -- attached to the record type. That's because the init procedure is
4055 -- recursively called on each component, including _Parent, thus the
4056 -- Init call for a controlled object would generate not only one
4057 -- Initialize call as it is required but one for each ancestor of
4058 -- its type. This processing is suppressed if No_Initialization set.
4060 if not Controlled_Type (Typ)
4061 or else No_Initialization (N)
4065 elsif not Abort_Allowed
4066 or else not Comes_From_Source (N)
4068 Insert_Actions_After (N,
4070 Ref => New_Occurrence_Of (Def_Id, Loc),
4071 Typ => Base_Type (Typ),
4072 Flist_Ref => Find_Final_List (Def_Id),
4073 With_Attach => Make_Integer_Literal (Loc, 1)));
4078 -- We need to protect the initialize call
4082 -- Initialize (...);
4084 -- Undefer_Abort.all;
4087 -- ??? this won't protect the initialize call for controlled
4088 -- components which are part of the init proc, so this block
4089 -- should probably also contain the call to _init_proc but this
4090 -- requires some code reorganization...
4093 L : constant List_Id :=
4095 (Ref => New_Occurrence_Of (Def_Id, Loc),
4096 Typ => Base_Type (Typ),
4097 Flist_Ref => Find_Final_List (Def_Id),
4098 With_Attach => Make_Integer_Literal (Loc, 1));
4100 Blk : constant Node_Id :=
4101 Make_Block_Statement (Loc,
4102 Handled_Statement_Sequence =>
4103 Make_Handled_Sequence_Of_Statements (Loc, L));
4106 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4107 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4108 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4109 Insert_Actions_After (N, New_List (Blk));
4110 Expand_At_End_Handler
4111 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4115 -- Call type initialization procedure if there is one. We build the
4116 -- call and put it immediately after the object declaration, so that
4117 -- it will be expanded in the usual manner. Note that this will
4118 -- result in proper handling of defaulted discriminants.
4120 -- Need call if there is a base init proc
4122 if Has_Non_Null_Base_Init_Proc (Typ)
4124 -- Suppress call if No_Initialization set on declaration
4126 and then not No_Initialization (N)
4128 -- Suppress call for special case of value type for VM
4130 and then not Is_Value_Type (Typ)
4132 -- Suppress call if Suppress_Init_Proc set on the type. This is
4133 -- needed for the derived type case, where Suppress_Initialization
4134 -- may be set for the derived type, even if there is an init proc
4135 -- defined for the root type.
4137 and then not Suppress_Init_Proc (Typ)
4139 -- The call to the initialization procedure does NOT freeze the
4140 -- object being initialized. This is because the call is not a
4141 -- source level call. This works fine, because the only possible
4142 -- statements depending on freeze status that can appear after the
4143 -- _Init call are rep clauses which can safely appear after actual
4144 -- references to the object.
4146 Id_Ref := New_Reference_To (Def_Id, Loc);
4147 Set_Must_Not_Freeze (Id_Ref);
4148 Set_Assignment_OK (Id_Ref);
4151 Init_Expr : constant Node_Id :=
4152 Static_Initialization (Base_Init_Proc (Typ));
4154 if Present (Init_Expr) then
4156 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4159 Initialization_Warning (Id_Ref);
4161 Insert_Actions_After (N,
4162 Build_Initialization_Call (Loc, Id_Ref, Typ));
4166 -- If simple initialization is required, then set an appropriate
4167 -- simple initialization expression in place. This special
4168 -- initialization is required even though No_Init_Flag is present,
4169 -- but is not needed if there was an explicit initialization.
4171 -- An internally generated temporary needs no initialization because
4172 -- it will be assigned subsequently. In particular, there is no point
4173 -- in applying Initialize_Scalars to such a temporary.
4175 elsif Needs_Simple_Initialization (Typ)
4176 and then not Is_Internal (Def_Id)
4177 and then not Has_Init_Expression (N)
4179 Set_No_Initialization (N, False);
4180 Set_Expression (N, Get_Simple_Init_Val (Typ, Loc, Esize (Def_Id)));
4181 Analyze_And_Resolve (Expression (N), Typ);
4184 -- Generate attribute for Persistent_BSS if needed
4186 if Persistent_BSS_Mode
4187 and then Comes_From_Source (N)
4188 and then Is_Potentially_Persistent_Type (Typ)
4189 and then not Has_Init_Expression (N)
4190 and then Is_Library_Level_Entity (Def_Id)
4196 Make_Linker_Section_Pragma
4197 (Def_Id, Sloc (N), ".persistent.bss");
4198 Insert_After (N, Prag);
4203 -- If access type, then we know it is null if not initialized
4205 if Is_Access_Type (Typ) then
4206 Set_Is_Known_Null (Def_Id);
4209 -- Explicit initialization present
4212 -- Obtain actual expression from qualified expression
4214 if Nkind (Expr) = N_Qualified_Expression then
4215 Expr_Q := Expression (Expr);
4220 -- When we have the appropriate type of aggregate in the expression
4221 -- (it has been determined during analysis of the aggregate by
4222 -- setting the delay flag), let's perform in place assignment and
4223 -- thus avoid creating a temporary.
4225 if Is_Delayed_Aggregate (Expr_Q) then
4226 Convert_Aggr_In_Object_Decl (N);
4229 -- Ada 2005 (AI-318-02): If the initialization expression is a
4230 -- call to a build-in-place function, then access to the declared
4231 -- object must be passed to the function. Currently we limit such
4232 -- functions to those with constrained limited result subtypes,
4233 -- but eventually we plan to expand the allowed forms of functions
4234 -- that are treated as build-in-place.
4236 if Ada_Version >= Ada_05
4237 and then Is_Build_In_Place_Function_Call (Expr_Q)
4239 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4243 -- In most cases, we must check that the initial value meets any
4244 -- constraint imposed by the declared type. However, there is one
4245 -- very important exception to this rule. If the entity has an
4246 -- unconstrained nominal subtype, then it acquired its constraints
4247 -- from the expression in the first place, and not only does this
4248 -- mean that the constraint check is not needed, but an attempt to
4249 -- perform the constraint check can cause order order of
4250 -- elaboration problems.
4252 if not Is_Constr_Subt_For_U_Nominal (Typ) then
4254 -- If this is an allocator for an aggregate that has been
4255 -- allocated in place, delay checks until assignments are
4256 -- made, because the discriminants are not initialized.
4258 if Nkind (Expr) = N_Allocator
4259 and then No_Initialization (Expr)
4263 Apply_Constraint_Check (Expr, Typ);
4267 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4268 -- class-wide object to ensure that we copy the full object.
4271 -- CW : I'Class := Obj;
4273 -- CW__1 : I'Class := I'Class (Base_Address (Obj'Address));
4274 -- CW : I'Class renames Displace (CW__1, I'Tag);
4276 if Is_Interface (Typ)
4277 and then Is_Class_Wide_Type (Etype (Expr))
4278 and then Comes_From_Source (Def_Id)
4286 Make_Object_Declaration (Loc,
4287 Defining_Identifier =>
4288 Make_Defining_Identifier (Loc,
4289 New_Internal_Name ('D')),
4291 Object_Definition =>
4292 Make_Attribute_Reference (Loc,
4295 (Root_Type (Etype (Def_Id)), Loc),
4296 Attribute_Name => Name_Class),
4299 Unchecked_Convert_To
4300 (Class_Wide_Type (Root_Type (Etype (Def_Id))),
4301 Make_Explicit_Dereference (Loc,
4302 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4303 Make_Function_Call (Loc,
4305 New_Reference_To (RTE (RE_Base_Address),
4307 Parameter_Associations => New_List (
4308 Make_Attribute_Reference (Loc,
4309 Prefix => Relocate_Node (Expr),
4310 Attribute_Name => Name_Address)))))));
4312 Insert_Action (N, Decl_1);
4315 Make_Object_Renaming_Declaration (Loc,
4316 Defining_Identifier =>
4317 Make_Defining_Identifier (Loc,
4318 New_Internal_Name ('D')),
4321 Make_Attribute_Reference (Loc,
4324 (Root_Type (Etype (Def_Id)), Loc),
4325 Attribute_Name => Name_Class),
4328 Unchecked_Convert_To (
4329 Class_Wide_Type (Root_Type (Etype (Def_Id))),
4330 Make_Explicit_Dereference (Loc,
4331 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4332 Make_Function_Call (Loc,
4334 New_Reference_To (RTE (RE_Displace), Loc),
4336 Parameter_Associations => New_List (
4337 Make_Attribute_Reference (Loc,
4340 (Defining_Identifier (Decl_1), Loc),
4341 Attribute_Name => Name_Address),
4343 Unchecked_Convert_To (RTE (RE_Tag),
4348 (Root_Type (Typ)))),
4351 Rewrite (N, Decl_2);
4354 -- Replace internal identifier of Decl_2 by the identifier
4355 -- found in the sources. We also have to exchange entities
4356 -- containing their defining identifiers to ensure the
4357 -- correct replacement of the object declaration by this
4358 -- object renaming declaration (because such definings
4359 -- identifier have been previously added by Enter_Name to
4360 -- the current scope). We must preserve the homonym chain
4361 -- of the source entity as well.
4363 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4364 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4365 Exchange_Entities (Defining_Identifier (N), Def_Id);
4371 -- If the type is controlled and not limited then the target is
4372 -- adjusted after the copy and attached to the finalization list.
4373 -- However, no adjustment is done in the case where the object was
4374 -- initialized by a call to a function whose result is built in
4375 -- place, since no copy occurred. (We eventually plan to support
4376 -- in-place function results for some nonlimited types. ???)
4378 if Controlled_Type (Typ)
4379 and then not Is_Limited_Type (Typ)
4380 and then not BIP_Call
4382 Insert_Actions_After (N,
4384 Ref => New_Reference_To (Def_Id, Loc),
4385 Typ => Base_Type (Typ),
4386 Flist_Ref => Find_Final_List (Def_Id),
4387 With_Attach => Make_Integer_Literal (Loc, 1)));
4390 -- For tagged types, when an init value is given, the tag has to
4391 -- be re-initialized separately in order to avoid the propagation
4392 -- of a wrong tag coming from a view conversion unless the type
4393 -- is class wide (in this case the tag comes from the init value).
4394 -- Suppress the tag assignment when VM_Target because VM tags are
4395 -- represented implicitly in objects. Ditto for types that are
4396 -- CPP_CLASS, and for initializations that are aggregates, because
4397 -- they have to have the right tag.
4399 if Is_Tagged_Type (Typ)
4400 and then not Is_Class_Wide_Type (Typ)
4401 and then not Is_CPP_Class (Typ)
4402 and then VM_Target = No_VM
4403 and then Nkind (Expr) /= N_Aggregate
4405 -- The re-assignment of the tag has to be done even if the
4406 -- object is a constant.
4409 Make_Selected_Component (Loc,
4410 Prefix => New_Reference_To (Def_Id, Loc),
4412 New_Reference_To (First_Tag_Component (Typ), Loc));
4414 Set_Assignment_OK (New_Ref);
4417 Make_Assignment_Statement (Loc,
4420 Unchecked_Convert_To (RTE (RE_Tag),
4424 (Access_Disp_Table (Base_Type (Typ)))),
4427 -- For discrete types, set the Is_Known_Valid flag if the
4428 -- initializing value is known to be valid.
4430 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
4431 Set_Is_Known_Valid (Def_Id);
4433 elsif Is_Access_Type (Typ) then
4435 -- For access types set the Is_Known_Non_Null flag if the
4436 -- initializing value is known to be non-null. We can also set
4437 -- Can_Never_Be_Null if this is a constant.
4439 if Known_Non_Null (Expr) then
4440 Set_Is_Known_Non_Null (Def_Id, True);
4442 if Constant_Present (N) then
4443 Set_Can_Never_Be_Null (Def_Id);
4448 -- If validity checking on copies, validate initial expression
4450 if Validity_Checks_On
4451 and then Validity_Check_Copies
4453 Ensure_Valid (Expr);
4454 Set_Is_Known_Valid (Def_Id);
4458 -- Cases where the back end cannot handle the initialization directly
4459 -- In such cases, we expand an assignment that will be appropriately
4460 -- handled by Expand_N_Assignment_Statement.
4462 -- The exclusion of the unconstrained case is wrong, but for now it
4463 -- is too much trouble ???
4465 if (Is_Possibly_Unaligned_Slice (Expr)
4466 or else (Is_Possibly_Unaligned_Object (Expr)
4467 and then not Represented_As_Scalar (Etype (Expr))))
4469 -- The exclusion of the unconstrained case is wrong, but for now
4470 -- it is too much trouble ???
4472 and then not (Is_Array_Type (Etype (Expr))
4473 and then not Is_Constrained (Etype (Expr)))
4476 Stat : constant Node_Id :=
4477 Make_Assignment_Statement (Loc,
4478 Name => New_Reference_To (Def_Id, Loc),
4479 Expression => Relocate_Node (Expr));
4481 Set_Expression (N, Empty);
4482 Set_No_Initialization (N);
4483 Set_Assignment_OK (Name (Stat));
4484 Set_No_Ctrl_Actions (Stat);
4485 Insert_After (N, Stat);
4492 when RE_Not_Available =>
4494 end Expand_N_Object_Declaration;
4496 ---------------------------------
4497 -- Expand_N_Subtype_Indication --
4498 ---------------------------------
4500 -- Add a check on the range of the subtype. The static case is partially
4501 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
4502 -- to check here for the static case in order to avoid generating
4503 -- extraneous expanded code. Also deal with validity checking.
4505 procedure Expand_N_Subtype_Indication (N : Node_Id) is
4506 Ran : constant Node_Id := Range_Expression (Constraint (N));
4507 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
4510 if Nkind (Constraint (N)) = N_Range_Constraint then
4511 Validity_Check_Range (Range_Expression (Constraint (N)));
4514 if Nkind (Parent (N)) = N_Constrained_Array_Definition
4516 Nkind (Parent (N)) = N_Slice
4518 Apply_Range_Check (Ran, Typ);
4520 end Expand_N_Subtype_Indication;
4522 ---------------------------
4523 -- Expand_N_Variant_Part --
4524 ---------------------------
4526 -- If the last variant does not contain the Others choice, replace it with
4527 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
4528 -- do not bother to call Analyze on the modified variant part, since it's
4529 -- only effect would be to compute the Others_Discrete_Choices node
4530 -- laboriously, and of course we already know the list of choices that
4531 -- corresponds to the others choice (it's the list we are replacing!)
4533 procedure Expand_N_Variant_Part (N : Node_Id) is
4534 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
4535 Others_Node : Node_Id;
4537 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
4538 Others_Node := Make_Others_Choice (Sloc (Last_Var));
4539 Set_Others_Discrete_Choices
4540 (Others_Node, Discrete_Choices (Last_Var));
4541 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
4543 end Expand_N_Variant_Part;
4545 ---------------------------------
4546 -- Expand_Previous_Access_Type --
4547 ---------------------------------
4549 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
4550 T : Entity_Id := First_Entity (Current_Scope);
4553 -- Find all access types declared in the current scope, whose
4554 -- designated type is Def_Id.
4556 while Present (T) loop
4557 if Is_Access_Type (T)
4558 and then Designated_Type (T) = Def_Id
4560 Build_Master_Entity (Def_Id);
4561 Build_Master_Renaming (Parent (Def_Id), T);
4566 end Expand_Previous_Access_Type;
4568 ------------------------------
4569 -- Expand_Record_Controller --
4570 ------------------------------
4572 procedure Expand_Record_Controller (T : Entity_Id) is
4573 Def : Node_Id := Type_Definition (Parent (T));
4574 Comp_List : Node_Id;
4575 Comp_Decl : Node_Id;
4577 First_Comp : Node_Id;
4578 Controller_Type : Entity_Id;
4582 if Nkind (Def) = N_Derived_Type_Definition then
4583 Def := Record_Extension_Part (Def);
4586 if Null_Present (Def) then
4587 Set_Component_List (Def,
4588 Make_Component_List (Sloc (Def),
4589 Component_Items => Empty_List,
4590 Variant_Part => Empty,
4591 Null_Present => True));
4594 Comp_List := Component_List (Def);
4596 if Null_Present (Comp_List)
4597 or else Is_Empty_List (Component_Items (Comp_List))
4599 Loc := Sloc (Comp_List);
4601 Loc := Sloc (First (Component_Items (Comp_List)));
4604 if Is_Inherently_Limited_Type (T) then
4605 Controller_Type := RTE (RE_Limited_Record_Controller);
4607 Controller_Type := RTE (RE_Record_Controller);
4610 Ent := Make_Defining_Identifier (Loc, Name_uController);
4613 Make_Component_Declaration (Loc,
4614 Defining_Identifier => Ent,
4615 Component_Definition =>
4616 Make_Component_Definition (Loc,
4617 Aliased_Present => False,
4618 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
4620 if Null_Present (Comp_List)
4621 or else Is_Empty_List (Component_Items (Comp_List))
4623 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4624 Set_Null_Present (Comp_List, False);
4627 -- The controller cannot be placed before the _Parent field since
4628 -- gigi lays out field in order and _parent must be first to preserve
4629 -- the polymorphism of tagged types.
4631 First_Comp := First (Component_Items (Comp_List));
4633 if not Is_Tagged_Type (T) then
4634 Insert_Before (First_Comp, Comp_Decl);
4636 -- if T is a tagged type, place controller declaration after parent
4637 -- field and after eventual tags of interface types.
4640 while Present (First_Comp)
4642 (Chars (Defining_Identifier (First_Comp)) = Name_uParent
4643 or else Is_Tag (Defining_Identifier (First_Comp))
4645 -- Ada 2005 (AI-251): The following condition covers secondary
4646 -- tags but also the adjacent component contanining the offset
4647 -- to the base of the object (component generated if the parent
4648 -- has discriminants --- see Add_Interface_Tag_Components).
4649 -- This is required to avoid the addition of the controller
4650 -- between the secondary tag and its adjacent component.
4654 (Defining_Identifier (First_Comp))))
4659 -- An empty tagged extension might consist only of the parent
4660 -- component. Otherwise insert the controller before the first
4661 -- component that is neither parent nor tag.
4663 if Present (First_Comp) then
4664 Insert_Before (First_Comp, Comp_Decl);
4666 Append (Comp_Decl, Component_Items (Comp_List));
4672 Analyze (Comp_Decl);
4673 Set_Ekind (Ent, E_Component);
4674 Init_Component_Location (Ent);
4676 -- Move the _controller entity ahead in the list of internal entities
4677 -- of the enclosing record so that it is selected instead of a
4678 -- potentially inherited one.
4681 E : constant Entity_Id := Last_Entity (T);
4685 pragma Assert (Chars (E) = Name_uController);
4687 Set_Next_Entity (E, First_Entity (T));
4688 Set_First_Entity (T, E);
4690 Comp := Next_Entity (E);
4691 while Next_Entity (Comp) /= E loop
4695 Set_Next_Entity (Comp, Empty);
4696 Set_Last_Entity (T, Comp);
4702 when RE_Not_Available =>
4704 end Expand_Record_Controller;
4706 ------------------------
4707 -- Expand_Tagged_Root --
4708 ------------------------
4710 procedure Expand_Tagged_Root (T : Entity_Id) is
4711 Def : constant Node_Id := Type_Definition (Parent (T));
4712 Comp_List : Node_Id;
4713 Comp_Decl : Node_Id;
4714 Sloc_N : Source_Ptr;
4717 if Null_Present (Def) then
4718 Set_Component_List (Def,
4719 Make_Component_List (Sloc (Def),
4720 Component_Items => Empty_List,
4721 Variant_Part => Empty,
4722 Null_Present => True));
4725 Comp_List := Component_List (Def);
4727 if Null_Present (Comp_List)
4728 or else Is_Empty_List (Component_Items (Comp_List))
4730 Sloc_N := Sloc (Comp_List);
4732 Sloc_N := Sloc (First (Component_Items (Comp_List)));
4736 Make_Component_Declaration (Sloc_N,
4737 Defining_Identifier => First_Tag_Component (T),
4738 Component_Definition =>
4739 Make_Component_Definition (Sloc_N,
4740 Aliased_Present => False,
4741 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
4743 if Null_Present (Comp_List)
4744 or else Is_Empty_List (Component_Items (Comp_List))
4746 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4747 Set_Null_Present (Comp_List, False);
4750 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4753 -- We don't Analyze the whole expansion because the tag component has
4754 -- already been analyzed previously. Here we just insure that the tree
4755 -- is coherent with the semantic decoration
4757 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
4760 when RE_Not_Available =>
4762 end Expand_Tagged_Root;
4764 ----------------------
4765 -- Clean_Task_Names --
4766 ----------------------
4768 procedure Clean_Task_Names
4770 Proc_Id : Entity_Id)
4774 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4775 and then not Global_Discard_Names
4776 and then VM_Target = No_VM
4778 Set_Uses_Sec_Stack (Proc_Id);
4780 end Clean_Task_Names;
4782 -----------------------
4783 -- Freeze_Array_Type --
4784 -----------------------
4786 procedure Freeze_Array_Type (N : Node_Id) is
4787 Typ : constant Entity_Id := Entity (N);
4788 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4789 Base : constant Entity_Id := Base_Type (Typ);
4792 if not Is_Bit_Packed_Array (Typ) then
4794 -- If the component contains tasks, so does the array type. This may
4795 -- not be indicated in the array type because the component may have
4796 -- been a private type at the point of definition. Same if component
4797 -- type is controlled.
4799 Set_Has_Task (Base, Has_Task (Comp_Typ));
4800 Set_Has_Controlled_Component (Base,
4801 Has_Controlled_Component (Comp_Typ)
4802 or else Is_Controlled (Comp_Typ));
4804 if No (Init_Proc (Base)) then
4806 -- If this is an anonymous array created for a declaration with
4807 -- an initial value, its init_proc will never be called. The
4808 -- initial value itself may have been expanded into assignments,
4809 -- in which case the object declaration is carries the
4810 -- No_Initialization flag.
4813 and then Nkind (Associated_Node_For_Itype (Base)) =
4814 N_Object_Declaration
4815 and then (Present (Expression (Associated_Node_For_Itype (Base)))
4817 No_Initialization (Associated_Node_For_Itype (Base)))
4821 -- We do not need an init proc for string or wide [wide] string,
4822 -- since the only time these need initialization in normalize or
4823 -- initialize scalars mode, and these types are treated specially
4824 -- and do not need initialization procedures.
4826 elsif Root_Type (Base) = Standard_String
4827 or else Root_Type (Base) = Standard_Wide_String
4828 or else Root_Type (Base) = Standard_Wide_Wide_String
4832 -- Otherwise we have to build an init proc for the subtype
4835 Build_Array_Init_Proc (Base, N);
4840 if Has_Controlled_Component (Base) then
4841 Build_Controlling_Procs (Base);
4843 if not Is_Limited_Type (Comp_Typ)
4844 and then Number_Dimensions (Typ) = 1
4846 Build_Slice_Assignment (Typ);
4849 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
4850 and then Controlled_Type (Directly_Designated_Type (Comp_Typ))
4852 Set_Associated_Final_Chain (Comp_Typ, Add_Final_Chain (Typ));
4856 -- For packed case, default initialization, except if the component type
4857 -- is itself a packed structure with an initialization procedure, or
4858 -- initialize/normalize scalars active, and we have a base type.
4860 elsif (Present (Init_Proc (Component_Type (Base)))
4861 and then No (Base_Init_Proc (Base)))
4862 or else (Init_Or_Norm_Scalars and then Base = Typ)
4864 Build_Array_Init_Proc (Base, N);
4866 end Freeze_Array_Type;
4868 -----------------------------
4869 -- Freeze_Enumeration_Type --
4870 -----------------------------
4872 procedure Freeze_Enumeration_Type (N : Node_Id) is
4873 Typ : constant Entity_Id := Entity (N);
4874 Loc : constant Source_Ptr := Sloc (Typ);
4881 Is_Contiguous : Boolean;
4886 pragma Warnings (Off, Func);
4889 -- Various optimizations possible if given representation is contiguous
4891 Is_Contiguous := True;
4893 Ent := First_Literal (Typ);
4894 Last_Repval := Enumeration_Rep (Ent);
4897 while Present (Ent) loop
4898 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
4899 Is_Contiguous := False;
4902 Last_Repval := Enumeration_Rep (Ent);
4908 if Is_Contiguous then
4909 Set_Has_Contiguous_Rep (Typ);
4910 Ent := First_Literal (Typ);
4912 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
4915 -- Build list of literal references
4920 Ent := First_Literal (Typ);
4921 while Present (Ent) loop
4922 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
4928 -- Now build an array declaration
4930 -- typA : array (Natural range 0 .. num - 1) of ctype :=
4931 -- (v, v, v, v, v, ....)
4933 -- where ctype is the corresponding integer type. If the representation
4934 -- is contiguous, we only keep the first literal, which provides the
4935 -- offset for Pos_To_Rep computations.
4938 Make_Defining_Identifier (Loc,
4939 Chars => New_External_Name (Chars (Typ), 'A'));
4941 Append_Freeze_Action (Typ,
4942 Make_Object_Declaration (Loc,
4943 Defining_Identifier => Arr,
4944 Constant_Present => True,
4946 Object_Definition =>
4947 Make_Constrained_Array_Definition (Loc,
4948 Discrete_Subtype_Definitions => New_List (
4949 Make_Subtype_Indication (Loc,
4950 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
4952 Make_Range_Constraint (Loc,
4956 Make_Integer_Literal (Loc, 0),
4958 Make_Integer_Literal (Loc, Num - 1))))),
4960 Component_Definition =>
4961 Make_Component_Definition (Loc,
4962 Aliased_Present => False,
4963 Subtype_Indication => New_Reference_To (Typ, Loc))),
4966 Make_Aggregate (Loc,
4967 Expressions => Lst)));
4969 Set_Enum_Pos_To_Rep (Typ, Arr);
4971 -- Now we build the function that converts representation values to
4972 -- position values. This function has the form:
4974 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
4977 -- when enum-lit'Enum_Rep => return posval;
4978 -- when enum-lit'Enum_Rep => return posval;
4981 -- [raise Constraint_Error when F "invalid data"]
4986 -- Note: the F parameter determines whether the others case (no valid
4987 -- representation) raises Constraint_Error or returns a unique value
4988 -- of minus one. The latter case is used, e.g. in 'Valid code.
4990 -- Note: the reason we use Enum_Rep values in the case here is to avoid
4991 -- the code generator making inappropriate assumptions about the range
4992 -- of the values in the case where the value is invalid. ityp is a
4993 -- signed or unsigned integer type of appropriate width.
4995 -- Note: if exceptions are not supported, then we suppress the raise
4996 -- and return -1 unconditionally (this is an erroneous program in any
4997 -- case and there is no obligation to raise Constraint_Error here!) We
4998 -- also do this if pragma Restrictions (No_Exceptions) is active.
5000 -- Is this right??? What about No_Exception_Propagation???
5002 -- Representations are signed
5004 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5006 -- The underlying type is signed. Reset the Is_Unsigned_Type
5007 -- explicitly, because it might have been inherited from
5010 Set_Is_Unsigned_Type (Typ, False);
5012 if Esize (Typ) <= Standard_Integer_Size then
5013 Ityp := Standard_Integer;
5015 Ityp := Universal_Integer;
5018 -- Representations are unsigned
5021 if Esize (Typ) <= Standard_Integer_Size then
5022 Ityp := RTE (RE_Unsigned);
5024 Ityp := RTE (RE_Long_Long_Unsigned);
5028 -- The body of the function is a case statement. First collect case
5029 -- alternatives, or optimize the contiguous case.
5033 -- If representation is contiguous, Pos is computed by subtracting
5034 -- the representation of the first literal.
5036 if Is_Contiguous then
5037 Ent := First_Literal (Typ);
5039 if Enumeration_Rep (Ent) = Last_Repval then
5041 -- Another special case: for a single literal, Pos is zero
5043 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5047 Convert_To (Standard_Integer,
5048 Make_Op_Subtract (Loc,
5050 Unchecked_Convert_To (Ityp,
5051 Make_Identifier (Loc, Name_uA)),
5053 Make_Integer_Literal (Loc,
5055 Enumeration_Rep (First_Literal (Typ)))));
5059 Make_Case_Statement_Alternative (Loc,
5060 Discrete_Choices => New_List (
5061 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5063 Make_Integer_Literal (Loc,
5064 Intval => Enumeration_Rep (Ent)),
5066 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5068 Statements => New_List (
5069 Make_Simple_Return_Statement (Loc,
5070 Expression => Pos_Expr))));
5073 Ent := First_Literal (Typ);
5074 while Present (Ent) loop
5076 Make_Case_Statement_Alternative (Loc,
5077 Discrete_Choices => New_List (
5078 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5079 Intval => Enumeration_Rep (Ent))),
5081 Statements => New_List (
5082 Make_Simple_Return_Statement (Loc,
5084 Make_Integer_Literal (Loc,
5085 Intval => Enumeration_Pos (Ent))))));
5091 -- In normal mode, add the others clause with the test
5093 if not No_Exception_Handlers_Set then
5095 Make_Case_Statement_Alternative (Loc,
5096 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5097 Statements => New_List (
5098 Make_Raise_Constraint_Error (Loc,
5099 Condition => Make_Identifier (Loc, Name_uF),
5100 Reason => CE_Invalid_Data),
5101 Make_Simple_Return_Statement (Loc,
5103 Make_Integer_Literal (Loc, -1)))));
5105 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5106 -- active then return -1 (we cannot usefully raise Constraint_Error in
5107 -- this case). See description above for further details.
5111 Make_Case_Statement_Alternative (Loc,
5112 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5113 Statements => New_List (
5114 Make_Simple_Return_Statement (Loc,
5116 Make_Integer_Literal (Loc, -1)))));
5119 -- Now we can build the function body
5122 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5125 Make_Subprogram_Body (Loc,
5127 Make_Function_Specification (Loc,
5128 Defining_Unit_Name => Fent,
5129 Parameter_Specifications => New_List (
5130 Make_Parameter_Specification (Loc,
5131 Defining_Identifier =>
5132 Make_Defining_Identifier (Loc, Name_uA),
5133 Parameter_Type => New_Reference_To (Typ, Loc)),
5134 Make_Parameter_Specification (Loc,
5135 Defining_Identifier =>
5136 Make_Defining_Identifier (Loc, Name_uF),
5137 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5139 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5141 Declarations => Empty_List,
5143 Handled_Statement_Sequence =>
5144 Make_Handled_Sequence_Of_Statements (Loc,
5145 Statements => New_List (
5146 Make_Case_Statement (Loc,
5148 Unchecked_Convert_To (Ityp,
5149 Make_Identifier (Loc, Name_uA)),
5150 Alternatives => Lst))));
5152 Set_TSS (Typ, Fent);
5155 if not Debug_Generated_Code then
5156 Set_Debug_Info_Off (Fent);
5160 when RE_Not_Available =>
5162 end Freeze_Enumeration_Type;
5164 ------------------------
5165 -- Freeze_Record_Type --
5166 ------------------------
5168 procedure Freeze_Record_Type (N : Node_Id) is
5169 Def_Id : constant Node_Id := Entity (N);
5170 Type_Decl : constant Node_Id := Parent (Def_Id);
5172 Comp_Typ : Entity_Id;
5173 Has_Static_DT : Boolean := False;
5174 Predef_List : List_Id;
5176 Flist : Entity_Id := Empty;
5177 -- Finalization list allocated for the case of a type with anonymous
5178 -- access components whose designated type is potentially controlled.
5180 Renamed_Eq : Node_Id := Empty;
5181 -- Could use some comments ???
5183 Wrapper_Decl_List : List_Id := No_List;
5184 Wrapper_Body_List : List_Id := No_List;
5185 Null_Proc_Decl_List : List_Id := No_List;
5188 -- Build discriminant checking functions if not a derived type (for
5189 -- derived types that are not tagged types, always use the discriminant
5190 -- checking functions of the parent type). However, for untagged types
5191 -- the derivation may have taken place before the parent was frozen, so
5192 -- we copy explicitly the discriminant checking functions from the
5193 -- parent into the components of the derived type.
5195 if not Is_Derived_Type (Def_Id)
5196 or else Has_New_Non_Standard_Rep (Def_Id)
5197 or else Is_Tagged_Type (Def_Id)
5199 Build_Discr_Checking_Funcs (Type_Decl);
5201 elsif Is_Derived_Type (Def_Id)
5202 and then not Is_Tagged_Type (Def_Id)
5204 -- If we have a derived Unchecked_Union, we do not inherit the
5205 -- discriminant checking functions from the parent type since the
5206 -- discriminants are non existent.
5208 and then not Is_Unchecked_Union (Def_Id)
5209 and then Has_Discriminants (Def_Id)
5212 Old_Comp : Entity_Id;
5216 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5217 Comp := First_Component (Def_Id);
5218 while Present (Comp) loop
5219 if Ekind (Comp) = E_Component
5220 and then Chars (Comp) = Chars (Old_Comp)
5222 Set_Discriminant_Checking_Func (Comp,
5223 Discriminant_Checking_Func (Old_Comp));
5226 Next_Component (Old_Comp);
5227 Next_Component (Comp);
5232 if Is_Derived_Type (Def_Id)
5233 and then Is_Limited_Type (Def_Id)
5234 and then Is_Tagged_Type (Def_Id)
5236 Check_Stream_Attributes (Def_Id);
5239 -- Update task and controlled component flags, because some of the
5240 -- component types may have been private at the point of the record
5243 Comp := First_Component (Def_Id);
5245 while Present (Comp) loop
5246 Comp_Typ := Etype (Comp);
5248 if Has_Task (Comp_Typ) then
5249 Set_Has_Task (Def_Id);
5251 elsif Has_Controlled_Component (Comp_Typ)
5252 or else (Chars (Comp) /= Name_uParent
5253 and then Is_Controlled (Comp_Typ))
5255 Set_Has_Controlled_Component (Def_Id);
5257 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5258 and then Controlled_Type (Directly_Designated_Type (Comp_Typ))
5261 Flist := Add_Final_Chain (Def_Id);
5264 Set_Associated_Final_Chain (Comp_Typ, Flist);
5267 Next_Component (Comp);
5270 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5271 -- for regular tagged types as well as for Ada types deriving from a C++
5272 -- Class, but not for tagged types directly corresponding to C++ classes
5273 -- In the later case we assume that it is created in the C++ side and we
5276 if Is_Tagged_Type (Def_Id) then
5278 Static_Dispatch_Tables
5279 and then Is_Library_Level_Tagged_Type (Def_Id);
5281 -- Add the _Tag component
5283 if Underlying_Type (Etype (Def_Id)) = Def_Id then
5284 Expand_Tagged_Root (Def_Id);
5287 if Is_CPP_Class (Def_Id) then
5288 Set_All_DT_Position (Def_Id);
5289 Set_Default_Constructor (Def_Id);
5291 -- Create the tag entities with a minimum decoration
5293 if VM_Target = No_VM then
5294 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5298 if not Has_Static_DT then
5300 -- Usually inherited primitives are not delayed but the first
5301 -- Ada extension of a CPP_Class is an exception since the
5302 -- address of the inherited subprogram has to be inserted in
5303 -- the new Ada Dispatch Table and this is a freezing action.
5305 -- Similarly, if this is an inherited operation whose parent is
5306 -- not frozen yet, it is not in the DT of the parent, and we
5307 -- generate an explicit freeze node for the inherited operation
5308 -- so that it is properly inserted in the DT of the current
5312 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
5316 while Present (Elmt) loop
5317 Subp := Node (Elmt);
5319 if Present (Alias (Subp)) then
5320 if Is_CPP_Class (Etype (Def_Id)) then
5321 Set_Has_Delayed_Freeze (Subp);
5323 elsif Has_Delayed_Freeze (Alias (Subp))
5324 and then not Is_Frozen (Alias (Subp))
5326 Set_Is_Frozen (Subp, False);
5327 Set_Has_Delayed_Freeze (Subp);
5336 -- Unfreeze momentarily the type to add the predefined primitives
5337 -- operations. The reason we unfreeze is so that these predefined
5338 -- operations will indeed end up as primitive operations (which
5339 -- must be before the freeze point).
5341 Set_Is_Frozen (Def_Id, False);
5343 -- Do not add the spec of the predefined primitives if we are
5344 -- compiling under restriction No_Dispatching_Calls
5346 if not Restriction_Active (No_Dispatching_Calls) then
5347 Make_Predefined_Primitive_Specs
5348 (Def_Id, Predef_List, Renamed_Eq);
5349 Insert_List_Before_And_Analyze (N, Predef_List);
5352 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5353 -- wrapper functions for each nonoverridden inherited function
5354 -- with a controlling result of the type. The wrapper for such
5355 -- a function returns an extension aggregate that invokes the
5356 -- the parent function.
5358 if Ada_Version >= Ada_05
5359 and then not Is_Abstract_Type (Def_Id)
5360 and then Is_Null_Extension (Def_Id)
5362 Make_Controlling_Function_Wrappers
5363 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
5364 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5367 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5368 -- null procedure declarations for each set of homographic null
5369 -- procedures that are inherited from interface types but not
5370 -- overridden. This is done to ensure that the dispatch table
5371 -- entry associated with such null primitives are properly filled.
5373 if Ada_Version >= Ada_05
5374 and then Etype (Def_Id) /= Def_Id
5375 and then not Is_Abstract_Type (Def_Id)
5377 Make_Null_Procedure_Specs (Def_Id, Null_Proc_Decl_List);
5378 Insert_Actions (N, Null_Proc_Decl_List);
5381 Set_Is_Frozen (Def_Id);
5382 Set_All_DT_Position (Def_Id);
5384 -- Add the controlled component before the freezing actions
5385 -- referenced in those actions.
5387 if Has_New_Controlled_Component (Def_Id) then
5388 Expand_Record_Controller (Def_Id);
5391 -- Create and decorate the tags. Suppress their creation when
5392 -- VM_Target because the dispatching mechanism is handled
5393 -- internally by the VMs.
5395 if VM_Target = No_VM then
5396 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5398 -- Generate dispatch table of locally defined tagged type.
5399 -- Dispatch tables of library level tagged types are built
5400 -- later (see Analyze_Declarations).
5402 if VM_Target = No_VM
5403 and then not Has_Static_DT
5405 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
5409 -- Make sure that the primitives Initialize, Adjust and Finalize
5410 -- are Frozen before other TSS subprograms. We don't want them
5413 if Is_Controlled (Def_Id) then
5414 if not Is_Limited_Type (Def_Id) then
5415 Append_Freeze_Actions (Def_Id,
5417 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
5420 Append_Freeze_Actions (Def_Id,
5422 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
5424 Append_Freeze_Actions (Def_Id,
5426 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
5429 -- Freeze rest of primitive operations. There is no need to handle
5430 -- the predefined primitives if we are compiling under restriction
5431 -- No_Dispatching_Calls
5433 if not Restriction_Active (No_Dispatching_Calls) then
5434 Append_Freeze_Actions
5435 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
5439 -- In the non-tagged case, an equality function is provided only for
5440 -- variant records (that are not unchecked unions).
5442 elsif Has_Discriminants (Def_Id)
5443 and then not Is_Limited_Type (Def_Id)
5446 Comps : constant Node_Id :=
5447 Component_List (Type_Definition (Type_Decl));
5451 and then Present (Variant_Part (Comps))
5453 Build_Variant_Record_Equality (Def_Id);
5458 -- Before building the record initialization procedure, if we are
5459 -- dealing with a concurrent record value type, then we must go through
5460 -- the discriminants, exchanging discriminals between the concurrent
5461 -- type and the concurrent record value type. See the section "Handling
5462 -- of Discriminants" in the Einfo spec for details.
5464 if Is_Concurrent_Record_Type (Def_Id)
5465 and then Has_Discriminants (Def_Id)
5468 Ctyp : constant Entity_Id :=
5469 Corresponding_Concurrent_Type (Def_Id);
5470 Conc_Discr : Entity_Id;
5471 Rec_Discr : Entity_Id;
5475 Conc_Discr := First_Discriminant (Ctyp);
5476 Rec_Discr := First_Discriminant (Def_Id);
5478 while Present (Conc_Discr) loop
5479 Temp := Discriminal (Conc_Discr);
5480 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5481 Set_Discriminal (Rec_Discr, Temp);
5483 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5484 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5486 Next_Discriminant (Conc_Discr);
5487 Next_Discriminant (Rec_Discr);
5492 if Has_Controlled_Component (Def_Id) then
5493 if No (Controller_Component (Def_Id)) then
5494 Expand_Record_Controller (Def_Id);
5497 Build_Controlling_Procs (Def_Id);
5500 Adjust_Discriminants (Def_Id);
5502 if VM_Target = No_VM or else not Is_Interface (Def_Id) then
5504 -- Do not need init for interfaces on e.g. CIL since they're
5505 -- abstract. Helps operation of peverify (the PE Verify tool).
5507 Build_Record_Init_Proc (Type_Decl, Def_Id);
5510 -- For tagged type, build bodies of primitive operations. Note that we
5511 -- do this after building the record initialization experiment, since
5512 -- the primitive operations may need the initialization routine
5514 if Is_Tagged_Type (Def_Id) then
5516 -- Do not add the body of the predefined primitives if we are
5517 -- compiling under restriction No_Dispatching_Calls
5519 if not Restriction_Active (No_Dispatching_Calls) then
5520 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
5521 Append_Freeze_Actions (Def_Id, Predef_List);
5524 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5525 -- inherited functions, then add their bodies to the freeze actions.
5527 if Present (Wrapper_Body_List) then
5528 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
5531 end Freeze_Record_Type;
5533 ------------------------------
5534 -- Freeze_Stream_Operations --
5535 ------------------------------
5537 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
5538 Names : constant array (1 .. 4) of TSS_Name_Type :=
5543 Stream_Op : Entity_Id;
5546 -- Primitive operations of tagged types are frozen when the dispatch
5547 -- table is constructed.
5549 if not Comes_From_Source (Typ)
5550 or else Is_Tagged_Type (Typ)
5555 for J in Names'Range loop
5556 Stream_Op := TSS (Typ, Names (J));
5558 if Present (Stream_Op)
5559 and then Is_Subprogram (Stream_Op)
5560 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
5561 N_Subprogram_Declaration
5562 and then not Is_Frozen (Stream_Op)
5564 Append_Freeze_Actions
5565 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
5568 end Freeze_Stream_Operations;
5574 -- Full type declarations are expanded at the point at which the type is
5575 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
5576 -- declarations generated by the freezing (e.g. the procedure generated
5577 -- for initialization) are chained in the Actions field list of the freeze
5578 -- node using Append_Freeze_Actions.
5580 function Freeze_Type (N : Node_Id) return Boolean is
5581 Def_Id : constant Entity_Id := Entity (N);
5582 RACW_Seen : Boolean := False;
5583 Result : Boolean := False;
5586 -- Process associated access types needing special processing
5588 if Present (Access_Types_To_Process (N)) then
5590 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
5592 while Present (E) loop
5594 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
5595 Validate_RACW_Primitives (Node (E));
5605 -- If there are RACWs designating this type, make stubs now
5607 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
5611 -- Freeze processing for record types
5613 if Is_Record_Type (Def_Id) then
5614 if Ekind (Def_Id) = E_Record_Type then
5615 Freeze_Record_Type (N);
5617 -- The subtype may have been declared before the type was frozen. If
5618 -- the type has controlled components it is necessary to create the
5619 -- entity for the controller explicitly because it did not exist at
5620 -- the point of the subtype declaration. Only the entity is needed,
5621 -- the back-end will obtain the layout from the type. This is only
5622 -- necessary if this is constrained subtype whose component list is
5623 -- not shared with the base type.
5625 elsif Ekind (Def_Id) = E_Record_Subtype
5626 and then Has_Discriminants (Def_Id)
5627 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
5628 and then Present (Controller_Component (Def_Id))
5631 Old_C : constant Entity_Id := Controller_Component (Def_Id);
5635 if Scope (Old_C) = Base_Type (Def_Id) then
5637 -- The entity is the one in the parent. Create new one
5639 New_C := New_Copy (Old_C);
5640 Set_Parent (New_C, Parent (Old_C));
5641 Push_Scope (Def_Id);
5647 if Is_Itype (Def_Id)
5648 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
5650 -- The freeze node is only used to introduce the controller,
5651 -- the back-end has no use for it for a discriminated
5654 Set_Freeze_Node (Def_Id, Empty);
5655 Set_Has_Delayed_Freeze (Def_Id, False);
5659 -- Similar process if the controller of the subtype is not present
5660 -- but the parent has it. This can happen with constrained
5661 -- record components where the subtype is an itype.
5663 elsif Ekind (Def_Id) = E_Record_Subtype
5664 and then Is_Itype (Def_Id)
5665 and then No (Controller_Component (Def_Id))
5666 and then Present (Controller_Component (Etype (Def_Id)))
5669 Old_C : constant Entity_Id :=
5670 Controller_Component (Etype (Def_Id));
5671 New_C : constant Entity_Id := New_Copy (Old_C);
5674 Set_Next_Entity (New_C, First_Entity (Def_Id));
5675 Set_First_Entity (Def_Id, New_C);
5677 -- The freeze node is only used to introduce the controller,
5678 -- the back-end has no use for it for a discriminated
5681 Set_Freeze_Node (Def_Id, Empty);
5682 Set_Has_Delayed_Freeze (Def_Id, False);
5687 -- Freeze processing for array types
5689 elsif Is_Array_Type (Def_Id) then
5690 Freeze_Array_Type (N);
5692 -- Freeze processing for access types
5694 -- For pool-specific access types, find out the pool object used for
5695 -- this type, needs actual expansion of it in some cases. Here are the
5696 -- different cases :
5698 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
5699 -- ---> don't use any storage pool
5701 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
5703 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
5705 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
5706 -- ---> Storage Pool is the specified one
5708 -- See GNAT Pool packages in the Run-Time for more details
5710 elsif Ekind (Def_Id) = E_Access_Type
5711 or else Ekind (Def_Id) = E_General_Access_Type
5714 Loc : constant Source_Ptr := Sloc (N);
5715 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
5716 Pool_Object : Entity_Id;
5719 Freeze_Action_Typ : Entity_Id;
5722 if Has_Storage_Size_Clause (Def_Id) then
5723 Siz_Exp := Expression (Parent (Storage_Size_Variable (Def_Id)));
5730 -- Rep Clause "for Def_Id'Storage_Size use 0;"
5731 -- ---> don't use any storage pool
5733 if Has_Storage_Size_Clause (Def_Id)
5734 and then Compile_Time_Known_Value (Siz_Exp)
5735 and then Expr_Value (Siz_Exp) = 0
5741 -- Rep Clause : for Def_Id'Storage_Size use Expr.
5743 -- Def_Id__Pool : Stack_Bounded_Pool
5744 -- (Expr, DT'Size, DT'Alignment);
5746 elsif Has_Storage_Size_Clause (Def_Id) then
5752 -- For unconstrained composite types we give a size of zero
5753 -- so that the pool knows that it needs a special algorithm
5754 -- for variable size object allocation.
5756 if Is_Composite_Type (Desig_Type)
5757 and then not Is_Constrained (Desig_Type)
5760 Make_Integer_Literal (Loc, 0);
5763 Make_Integer_Literal (Loc, Maximum_Alignment);
5767 Make_Attribute_Reference (Loc,
5768 Prefix => New_Reference_To (Desig_Type, Loc),
5769 Attribute_Name => Name_Max_Size_In_Storage_Elements);
5772 Make_Attribute_Reference (Loc,
5773 Prefix => New_Reference_To (Desig_Type, Loc),
5774 Attribute_Name => Name_Alignment);
5778 Make_Defining_Identifier (Loc,
5779 Chars => New_External_Name (Chars (Def_Id), 'P'));
5781 -- We put the code associated with the pools in the entity
5782 -- that has the later freeze node, usually the access type
5783 -- but it can also be the designated_type; because the pool
5784 -- code requires both those types to be frozen
5786 if Is_Frozen (Desig_Type)
5787 and then (No (Freeze_Node (Desig_Type))
5788 or else Analyzed (Freeze_Node (Desig_Type)))
5790 Freeze_Action_Typ := Def_Id;
5792 -- A Taft amendment type cannot get the freeze actions
5793 -- since the full view is not there.
5795 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
5796 and then No (Full_View (Desig_Type))
5798 Freeze_Action_Typ := Def_Id;
5801 Freeze_Action_Typ := Desig_Type;
5804 Append_Freeze_Action (Freeze_Action_Typ,
5805 Make_Object_Declaration (Loc,
5806 Defining_Identifier => Pool_Object,
5807 Object_Definition =>
5808 Make_Subtype_Indication (Loc,
5811 (RTE (RE_Stack_Bounded_Pool), Loc),
5814 Make_Index_Or_Discriminant_Constraint (Loc,
5815 Constraints => New_List (
5817 -- First discriminant is the Pool Size
5820 Storage_Size_Variable (Def_Id), Loc),
5822 -- Second discriminant is the element size
5826 -- Third discriminant is the alignment
5831 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
5835 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
5836 -- ---> Storage Pool is the specified one
5838 elsif Present (Associated_Storage_Pool (Def_Id)) then
5840 -- Nothing to do the associated storage pool has been attached
5841 -- when analyzing the rep. clause
5846 -- For access-to-controlled types (including class-wide types and
5847 -- Taft-amendment types which potentially have controlled
5848 -- components), expand the list controller object that will store
5849 -- the dynamically allocated objects. Do not do this
5850 -- transformation for expander-generated access types, but do it
5851 -- for types that are the full view of types derived from other
5852 -- private types. Also suppress the list controller in the case
5853 -- of a designated type with convention Java, since this is used
5854 -- when binding to Java API specs, where there's no equivalent of
5855 -- a finalization list and we don't want to pull in the
5856 -- finalization support if not needed.
5858 if not Comes_From_Source (Def_Id)
5859 and then not Has_Private_Declaration (Def_Id)
5863 elsif (Controlled_Type (Desig_Type)
5864 and then Convention (Desig_Type) /= Convention_Java
5865 and then Convention (Desig_Type) /= Convention_CIL)
5867 (Is_Incomplete_Or_Private_Type (Desig_Type)
5868 and then No (Full_View (Desig_Type))
5870 -- An exception is made for types defined in the run-time
5871 -- because Ada.Tags.Tag itself is such a type and cannot
5872 -- afford this unnecessary overhead that would generates a
5873 -- loop in the expansion scheme...
5875 and then not In_Runtime (Def_Id)
5877 -- Another exception is if Restrictions (No_Finalization)
5878 -- is active, since then we know nothing is controlled.
5880 and then not Restriction_Active (No_Finalization))
5882 -- If the designated type is not frozen yet, its controlled
5883 -- status must be retrieved explicitly.
5885 or else (Is_Array_Type (Desig_Type)
5886 and then not Is_Frozen (Desig_Type)
5887 and then Controlled_Type (Component_Type (Desig_Type)))
5889 -- The designated type has controlled anonymous access
5892 or else Has_Controlled_Coextensions (Desig_Type)
5894 Set_Associated_Final_Chain (Def_Id, Add_Final_Chain (Def_Id));
5898 -- Freeze processing for enumeration types
5900 elsif Ekind (Def_Id) = E_Enumeration_Type then
5902 -- We only have something to do if we have a non-standard
5903 -- representation (i.e. at least one literal whose pos value
5904 -- is not the same as its representation)
5906 if Has_Non_Standard_Rep (Def_Id) then
5907 Freeze_Enumeration_Type (N);
5910 -- Private types that are completed by a derivation from a private
5911 -- type have an internally generated full view, that needs to be
5912 -- frozen. This must be done explicitly because the two views share
5913 -- the freeze node, and the underlying full view is not visible when
5914 -- the freeze node is analyzed.
5916 elsif Is_Private_Type (Def_Id)
5917 and then Is_Derived_Type (Def_Id)
5918 and then Present (Full_View (Def_Id))
5919 and then Is_Itype (Full_View (Def_Id))
5920 and then Has_Private_Declaration (Full_View (Def_Id))
5921 and then Freeze_Node (Full_View (Def_Id)) = N
5923 Set_Entity (N, Full_View (Def_Id));
5924 Result := Freeze_Type (N);
5925 Set_Entity (N, Def_Id);
5927 -- All other types require no expander action. There are such cases
5928 -- (e.g. task types and protected types). In such cases, the freeze
5929 -- nodes are there for use by Gigi.
5933 Freeze_Stream_Operations (N, Def_Id);
5937 when RE_Not_Available =>
5941 -------------------------
5942 -- Get_Simple_Init_Val --
5943 -------------------------
5945 function Get_Simple_Init_Val
5948 Size : Uint := No_Uint) return Node_Id
5955 -- This is the size to be used for computation of the appropriate
5956 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
5960 -- These are the values computed by the procedure Check_Subtype_Bounds
5962 procedure Check_Subtype_Bounds;
5963 -- This procedure examines the subtype T, and its ancestor subtypes and
5964 -- derived types to determine the best known information about the
5965 -- bounds of the subtype. After the call Lo_Bound is set either to
5966 -- No_Uint if no information can be determined, or to a value which
5967 -- represents a known low bound, i.e. a valid value of the subtype can
5968 -- not be less than this value. Hi_Bound is similarly set to a known
5969 -- high bound (valid value cannot be greater than this).
5971 --------------------------
5972 -- Check_Subtype_Bounds --
5973 --------------------------
5975 procedure Check_Subtype_Bounds is
5984 Lo_Bound := No_Uint;
5985 Hi_Bound := No_Uint;
5987 -- Loop to climb ancestor subtypes and derived types
5991 if not Is_Discrete_Type (ST1) then
5995 Lo := Type_Low_Bound (ST1);
5996 Hi := Type_High_Bound (ST1);
5998 if Compile_Time_Known_Value (Lo) then
5999 Loval := Expr_Value (Lo);
6001 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6006 if Compile_Time_Known_Value (Hi) then
6007 Hival := Expr_Value (Hi);
6009 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6014 ST2 := Ancestor_Subtype (ST1);
6020 exit when ST1 = ST2;
6023 end Check_Subtype_Bounds;
6025 -- Start of processing for Get_Simple_Init_Val
6028 -- For a private type, we should always have an underlying type
6029 -- (because this was already checked in Needs_Simple_Initialization).
6030 -- What we do is to get the value for the underlying type and then do
6031 -- an Unchecked_Convert to the private type.
6033 if Is_Private_Type (T) then
6034 Val := Get_Simple_Init_Val (Underlying_Type (T), Loc, Size);
6036 -- A special case, if the underlying value is null, then qualify it
6037 -- with the underlying type, so that the null is properly typed
6038 -- Similarly, if it is an aggregate it must be qualified, because an
6039 -- unchecked conversion does not provide a context for it.
6041 if Nkind (Val) = N_Null
6042 or else Nkind (Val) = N_Aggregate
6045 Make_Qualified_Expression (Loc,
6047 New_Occurrence_Of (Underlying_Type (T), Loc),
6051 Result := Unchecked_Convert_To (T, Val);
6053 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6055 if Nkind (Result) = N_Unchecked_Type_Conversion
6056 and then Is_Scalar_Type (Underlying_Type (T))
6058 Set_No_Truncation (Result);
6063 -- For scalars, we must have normalize/initialize scalars case
6065 elsif Is_Scalar_Type (T) then
6066 pragma Assert (Init_Or_Norm_Scalars);
6068 -- Compute size of object. If it is given by the caller, we can use
6069 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6070 -- we know this covers all cases correctly.
6072 if Size = No_Uint or else Size <= Uint_0 then
6073 Size_To_Use := UI_Max (Uint_1, Esize (T));
6075 Size_To_Use := Size;
6078 -- Maximum size to use is 64 bits, since we will create values
6079 -- of type Unsigned_64 and the range must fit this type.
6081 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6082 Size_To_Use := Uint_64;
6085 -- Check known bounds of subtype
6087 Check_Subtype_Bounds;
6089 -- Processing for Normalize_Scalars case
6091 if Normalize_Scalars then
6093 -- If zero is invalid, it is a convenient value to use that is
6094 -- for sure an appropriate invalid value in all situations.
6096 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6097 Val := Make_Integer_Literal (Loc, 0);
6099 -- Cases where all one bits is the appropriate invalid value
6101 -- For modular types, all 1 bits is either invalid or valid. If
6102 -- it is valid, then there is nothing that can be done since there
6103 -- are no invalid values (we ruled out zero already).
6105 -- For signed integer types that have no negative values, either
6106 -- there is room for negative values, or there is not. If there
6107 -- is, then all 1 bits may be interpreted as minus one, which is
6108 -- certainly invalid. Alternatively it is treated as the largest
6109 -- positive value, in which case the observation for modular types
6112 -- For float types, all 1-bits is a NaN (not a number), which is
6113 -- certainly an appropriately invalid value.
6115 elsif Is_Unsigned_Type (T)
6116 or else Is_Floating_Point_Type (T)
6117 or else Is_Enumeration_Type (T)
6119 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6121 -- Resolve as Unsigned_64, because the largest number we
6122 -- can generate is out of range of universal integer.
6124 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6126 -- Case of signed types
6130 Signed_Size : constant Uint :=
6131 UI_Min (Uint_63, Size_To_Use - 1);
6134 -- Normally we like to use the most negative number. The
6135 -- one exception is when this number is in the known
6136 -- subtype range and the largest positive number is not in
6137 -- the known subtype range.
6139 -- For this exceptional case, use largest positive value
6141 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6142 and then Lo_Bound <= (-(2 ** Signed_Size))
6143 and then Hi_Bound < 2 ** Signed_Size
6145 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6147 -- Normal case of largest negative value
6150 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6155 -- Here for Initialize_Scalars case
6158 -- For float types, use float values from System.Scalar_Values
6160 if Is_Floating_Point_Type (T) then
6161 if Root_Type (T) = Standard_Short_Float then
6162 Val_RE := RE_IS_Isf;
6163 elsif Root_Type (T) = Standard_Float then
6164 Val_RE := RE_IS_Ifl;
6165 elsif Root_Type (T) = Standard_Long_Float then
6166 Val_RE := RE_IS_Ilf;
6167 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6168 Val_RE := RE_IS_Ill;
6171 -- If zero is invalid, use zero values from System.Scalar_Values
6173 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6174 if Size_To_Use <= 8 then
6175 Val_RE := RE_IS_Iz1;
6176 elsif Size_To_Use <= 16 then
6177 Val_RE := RE_IS_Iz2;
6178 elsif Size_To_Use <= 32 then
6179 Val_RE := RE_IS_Iz4;
6181 Val_RE := RE_IS_Iz8;
6184 -- For unsigned, use unsigned values from System.Scalar_Values
6186 elsif Is_Unsigned_Type (T) then
6187 if Size_To_Use <= 8 then
6188 Val_RE := RE_IS_Iu1;
6189 elsif Size_To_Use <= 16 then
6190 Val_RE := RE_IS_Iu2;
6191 elsif Size_To_Use <= 32 then
6192 Val_RE := RE_IS_Iu4;
6194 Val_RE := RE_IS_Iu8;
6197 -- For signed, use signed values from System.Scalar_Values
6200 if Size_To_Use <= 8 then
6201 Val_RE := RE_IS_Is1;
6202 elsif Size_To_Use <= 16 then
6203 Val_RE := RE_IS_Is2;
6204 elsif Size_To_Use <= 32 then
6205 Val_RE := RE_IS_Is4;
6207 Val_RE := RE_IS_Is8;
6211 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
6214 -- The final expression is obtained by doing an unchecked conversion
6215 -- of this result to the base type of the required subtype. We use
6216 -- the base type to avoid the unchecked conversion from chopping
6217 -- bits, and then we set Kill_Range_Check to preserve the "bad"
6220 Result := Unchecked_Convert_To (Base_Type (T), Val);
6222 -- Ensure result is not truncated, since we want the "bad" bits
6223 -- and also kill range check on result.
6225 if Nkind (Result) = N_Unchecked_Type_Conversion then
6226 Set_No_Truncation (Result);
6227 Set_Kill_Range_Check (Result, True);
6232 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
6234 elsif Root_Type (T) = Standard_String
6236 Root_Type (T) = Standard_Wide_String
6238 Root_Type (T) = Standard_Wide_Wide_String
6240 pragma Assert (Init_Or_Norm_Scalars);
6243 Make_Aggregate (Loc,
6244 Component_Associations => New_List (
6245 Make_Component_Association (Loc,
6246 Choices => New_List (
6247 Make_Others_Choice (Loc)),
6250 (Component_Type (T), Loc, Esize (Root_Type (T))))));
6252 -- Access type is initialized to null
6254 elsif Is_Access_Type (T) then
6258 -- No other possibilities should arise, since we should only be
6259 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
6260 -- returned True, indicating one of the above cases held.
6263 raise Program_Error;
6267 when RE_Not_Available =>
6269 end Get_Simple_Init_Val;
6271 ------------------------------
6272 -- Has_New_Non_Standard_Rep --
6273 ------------------------------
6275 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
6277 if not Is_Derived_Type (T) then
6278 return Has_Non_Standard_Rep (T)
6279 or else Has_Non_Standard_Rep (Root_Type (T));
6281 -- If Has_Non_Standard_Rep is not set on the derived type, the
6282 -- representation is fully inherited.
6284 elsif not Has_Non_Standard_Rep (T) then
6288 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
6290 -- May need a more precise check here: the First_Rep_Item may
6291 -- be a stream attribute, which does not affect the representation
6294 end Has_New_Non_Standard_Rep;
6300 function In_Runtime (E : Entity_Id) return Boolean is
6305 while Scope (S1) /= Standard_Standard loop
6309 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
6312 ----------------------------
6313 -- Initialization_Warning --
6314 ----------------------------
6316 procedure Initialization_Warning (E : Entity_Id) is
6317 Warning_Needed : Boolean;
6320 Warning_Needed := False;
6322 if Ekind (Current_Scope) = E_Package
6323 and then Static_Elaboration_Desired (Current_Scope)
6326 if Is_Record_Type (E) then
6327 if Has_Discriminants (E)
6328 or else Is_Limited_Type (E)
6329 or else Has_Non_Standard_Rep (E)
6331 Warning_Needed := True;
6334 -- Verify that at least one component has an initializtion
6335 -- expression. No need for a warning on a type if all its
6336 -- components have no initialization.
6342 Comp := First_Component (E);
6343 while Present (Comp) loop
6344 if Ekind (Comp) = E_Discriminant
6346 (Nkind (Parent (Comp)) = N_Component_Declaration
6347 and then Present (Expression (Parent (Comp))))
6349 Warning_Needed := True;
6353 Next_Component (Comp);
6358 if Warning_Needed then
6360 ("Objects of the type cannot be initialized " &
6361 "statically by default?",
6367 Error_Msg_N ("Object cannot be initialized statically?", E);
6370 end Initialization_Warning;
6376 function Init_Formals (Typ : Entity_Id) return List_Id is
6377 Loc : constant Source_Ptr := Sloc (Typ);
6381 -- First parameter is always _Init : in out typ. Note that we need
6382 -- this to be in/out because in the case of the task record value,
6383 -- there are default record fields (_Priority, _Size, -Task_Info)
6384 -- that may be referenced in the generated initialization routine.
6386 Formals := New_List (
6387 Make_Parameter_Specification (Loc,
6388 Defining_Identifier =>
6389 Make_Defining_Identifier (Loc, Name_uInit),
6391 Out_Present => True,
6392 Parameter_Type => New_Reference_To (Typ, Loc)));
6394 -- For task record value, or type that contains tasks, add two more
6395 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
6396 -- We also add these parameters for the task record type case.
6399 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
6402 Make_Parameter_Specification (Loc,
6403 Defining_Identifier =>
6404 Make_Defining_Identifier (Loc, Name_uMaster),
6405 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
6408 Make_Parameter_Specification (Loc,
6409 Defining_Identifier =>
6410 Make_Defining_Identifier (Loc, Name_uChain),
6412 Out_Present => True,
6414 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
6417 Make_Parameter_Specification (Loc,
6418 Defining_Identifier =>
6419 Make_Defining_Identifier (Loc, Name_uTask_Name),
6422 New_Reference_To (Standard_String, Loc)));
6428 when RE_Not_Available =>
6432 -------------------------
6433 -- Init_Secondary_Tags --
6434 -------------------------
6436 procedure Init_Secondary_Tags
6439 Stmts_List : List_Id;
6440 Fixed_Comps : Boolean := True;
6441 Variable_Comps : Boolean := True)
6443 Loc : constant Source_Ptr := Sloc (Target);
6445 procedure Inherit_CPP_Tag
6448 Tag_Comp : Entity_Id;
6449 Iface_Tag : Node_Id);
6450 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
6451 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6453 procedure Initialize_Tag
6456 Tag_Comp : Entity_Id;
6457 Iface_Tag : Node_Id);
6458 -- Initialize the tag of the secondary dispatch table of Typ associated
6459 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6460 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
6461 -- of Typ CPP tagged type we generate code to inherit the contents of
6462 -- the dispatch table directly from the ancestor.
6464 ---------------------
6465 -- Inherit_CPP_Tag --
6466 ---------------------
6468 procedure Inherit_CPP_Tag
6471 Tag_Comp : Entity_Id;
6472 Iface_Tag : Node_Id)
6475 pragma Assert (Is_CPP_Class (Etype (Typ)));
6477 Append_To (Stmts_List,
6478 Build_Inherit_Prims (Loc,
6481 Make_Selected_Component (Loc,
6482 Prefix => New_Copy_Tree (Target),
6483 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
6485 New_Reference_To (Iface_Tag, Loc),
6487 UI_To_Int (DT_Entry_Count (First_Tag_Component (Iface)))));
6488 end Inherit_CPP_Tag;
6490 --------------------
6491 -- Initialize_Tag --
6492 --------------------
6494 procedure Initialize_Tag
6497 Tag_Comp : Entity_Id;
6498 Iface_Tag : Node_Id)
6500 Comp_Typ : Entity_Id;
6501 Offset_To_Top_Comp : Entity_Id := Empty;
6504 -- Initialize the pointer to the secondary DT associated with the
6507 if not Is_Parent (Iface, Typ) then
6508 Append_To (Stmts_List,
6509 Make_Assignment_Statement (Loc,
6511 Make_Selected_Component (Loc,
6512 Prefix => New_Copy_Tree (Target),
6513 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
6515 New_Reference_To (Iface_Tag, Loc)));
6518 -- Issue error if Set_Offset_To_Top is not available in a
6519 -- configurable run-time environment.
6521 if not RTE_Available (RE_Set_Offset_To_Top) then
6522 Error_Msg_CRT ("abstract interface types", Typ);
6526 Comp_Typ := Scope (Tag_Comp);
6528 -- Initialize the entries of the table of interfaces. We generate a
6529 -- different call when the parent of the type has variable size
6532 if Comp_Typ /= Etype (Comp_Typ)
6533 and then Is_Variable_Size_Record (Etype (Comp_Typ))
6534 and then Chars (Tag_Comp) /= Name_uTag
6537 (Present (DT_Offset_To_Top_Func (Tag_Comp)));
6540 -- Set_Offset_To_Top
6542 -- Interface_T => Iface'Tag,
6543 -- Is_Constant => False,
6544 -- Offset_Value => n,
6545 -- Offset_Func => Fn'Address)
6547 Append_To (Stmts_List,
6548 Make_Procedure_Call_Statement (Loc,
6549 Name => New_Reference_To (RTE (RE_Set_Offset_To_Top), Loc),
6550 Parameter_Associations => New_List (
6551 Make_Attribute_Reference (Loc,
6552 Prefix => New_Copy_Tree (Target),
6553 Attribute_Name => Name_Address),
6555 Unchecked_Convert_To (RTE (RE_Tag),
6557 (Node (First_Elmt (Access_Disp_Table (Iface))),
6560 New_Occurrence_Of (Standard_False, Loc),
6562 Unchecked_Convert_To
6563 (RTE (RE_Storage_Offset),
6564 Make_Attribute_Reference (Loc,
6566 Make_Selected_Component (Loc,
6567 Prefix => New_Copy_Tree (Target),
6569 New_Reference_To (Tag_Comp, Loc)),
6570 Attribute_Name => Name_Position)),
6572 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
6573 Make_Attribute_Reference (Loc,
6574 Prefix => New_Reference_To
6575 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
6576 Attribute_Name => Name_Address)))));
6578 -- In this case the next component stores the value of the
6579 -- offset to the top.
6581 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
6582 pragma Assert (Present (Offset_To_Top_Comp));
6584 Append_To (Stmts_List,
6585 Make_Assignment_Statement (Loc,
6587 Make_Selected_Component (Loc,
6588 Prefix => New_Copy_Tree (Target),
6589 Selector_Name => New_Reference_To
6590 (Offset_To_Top_Comp, Loc)),
6592 Make_Attribute_Reference (Loc,
6594 Make_Selected_Component (Loc,
6595 Prefix => New_Copy_Tree (Target),
6597 New_Reference_To (Tag_Comp, Loc)),
6598 Attribute_Name => Name_Position)));
6600 -- Normal case: No discriminants in the parent type
6604 -- Set_Offset_To_Top
6606 -- Interface_T => Iface'Tag,
6607 -- Is_Constant => True,
6608 -- Offset_Value => n,
6609 -- Offset_Func => null);
6611 Append_To (Stmts_List,
6612 Make_Procedure_Call_Statement (Loc,
6613 Name => New_Reference_To
6614 (RTE (RE_Set_Offset_To_Top), Loc),
6615 Parameter_Associations => New_List (
6616 Make_Attribute_Reference (Loc,
6617 Prefix => New_Copy_Tree (Target),
6618 Attribute_Name => Name_Address),
6620 Unchecked_Convert_To (RTE (RE_Tag),
6623 (Access_Disp_Table (Iface))),
6626 New_Occurrence_Of (Standard_True, Loc),
6628 Unchecked_Convert_To
6629 (RTE (RE_Storage_Offset),
6630 Make_Attribute_Reference (Loc,
6632 Make_Selected_Component (Loc,
6633 Prefix => New_Copy_Tree (Target),
6635 New_Reference_To (Tag_Comp, Loc)),
6636 Attribute_Name => Name_Position)),
6644 Full_Typ : Entity_Id;
6645 Ifaces_List : Elist_Id;
6646 Ifaces_Comp_List : Elist_Id;
6647 Ifaces_Tag_List : Elist_Id;
6648 Iface_Elmt : Elmt_Id;
6649 Iface_Comp_Elmt : Elmt_Id;
6650 Iface_Tag_Elmt : Elmt_Id;
6652 In_Variable_Pos : Boolean;
6654 -- Start of processing for Init_Secondary_Tags
6657 -- Handle private types
6659 if Present (Full_View (Typ)) then
6660 Full_Typ := Full_View (Typ);
6665 Collect_Interfaces_Info
6666 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
6668 Iface_Elmt := First_Elmt (Ifaces_List);
6669 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
6670 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
6671 while Present (Iface_Elmt) loop
6672 Tag_Comp := Node (Iface_Comp_Elmt);
6674 -- If we are compiling under the CPP full ABI compatibility mode and
6675 -- the ancestor is a CPP_Pragma tagged type then we generate code to
6676 -- inherit the contents of the dispatch table directly from the
6679 if Is_CPP_Class (Etype (Full_Typ)) then
6680 Inherit_CPP_Tag (Full_Typ,
6681 Iface => Node (Iface_Elmt),
6682 Tag_Comp => Tag_Comp,
6683 Iface_Tag => Node (Iface_Tag_Elmt));
6685 -- Otherwise we generate code to initialize the tag
6688 -- Check if the parent of the record type has variable size
6691 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
6692 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
6694 if (In_Variable_Pos and then Variable_Comps)
6695 or else (not In_Variable_Pos and then Fixed_Comps)
6697 Initialize_Tag (Full_Typ,
6698 Iface => Node (Iface_Elmt),
6699 Tag_Comp => Tag_Comp,
6700 Iface_Tag => Node (Iface_Tag_Elmt));
6704 Next_Elmt (Iface_Elmt);
6705 Next_Elmt (Iface_Comp_Elmt);
6706 Next_Elmt (Iface_Tag_Elmt);
6708 end Init_Secondary_Tags;
6710 -----------------------------
6711 -- Is_Variable_Size_Record --
6712 -----------------------------
6714 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
6716 Comp_Typ : Entity_Id;
6720 pragma Assert (Is_Record_Type (E));
6722 Comp := First_Entity (E);
6723 while Present (Comp) loop
6724 Comp_Typ := Etype (Comp);
6726 if Is_Record_Type (Comp_Typ) then
6728 -- Recursive call if the record type has discriminants
6730 if Has_Discriminants (Comp_Typ)
6731 and then Is_Variable_Size_Record (Comp_Typ)
6736 elsif Is_Array_Type (Comp_Typ) then
6738 -- Check if some index is initialized with a non-constant value
6740 Idx := First_Index (Comp_Typ);
6741 while Present (Idx) loop
6742 if Nkind (Idx) = N_Range then
6743 if (Nkind (Low_Bound (Idx)) = N_Identifier
6744 and then Present (Entity (Low_Bound (Idx)))
6745 and then Ekind (Entity (Low_Bound (Idx))) /= E_Constant)
6747 (Nkind (High_Bound (Idx)) = N_Identifier
6748 and then Present (Entity (High_Bound (Idx)))
6749 and then Ekind (Entity (High_Bound (Idx))) /= E_Constant)
6755 Idx := Next_Index (Idx);
6763 end Is_Variable_Size_Record;
6765 ----------------------------------------
6766 -- Make_Controlling_Function_Wrappers --
6767 ----------------------------------------
6769 procedure Make_Controlling_Function_Wrappers
6770 (Tag_Typ : Entity_Id;
6771 Decl_List : out List_Id;
6772 Body_List : out List_Id)
6774 Loc : constant Source_Ptr := Sloc (Tag_Typ);
6775 Prim_Elmt : Elmt_Id;
6777 Actual_List : List_Id;
6778 Formal_List : List_Id;
6780 Par_Formal : Entity_Id;
6781 Formal_Node : Node_Id;
6782 Func_Spec : Node_Id;
6783 Func_Decl : Node_Id;
6784 Func_Body : Node_Id;
6785 Return_Stmt : Node_Id;
6788 Decl_List := New_List;
6789 Body_List := New_List;
6791 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
6793 while Present (Prim_Elmt) loop
6794 Subp := Node (Prim_Elmt);
6796 -- If a primitive function with a controlling result of the type has
6797 -- not been overridden by the user, then we must create a wrapper
6798 -- function here that effectively overrides it and invokes the
6799 -- (non-abstract) parent function. This can only occur for a null
6800 -- extension. Note that functions with anonymous controlling access
6801 -- results don't qualify and must be overridden. We also exclude
6802 -- Input attributes, since each type will have its own version of
6803 -- Input constructed by the expander. The test for Comes_From_Source
6804 -- is needed to distinguish inherited operations from renamings
6805 -- (which also have Alias set).
6807 -- The function may be abstract, or require_Overriding may be set
6808 -- for it, because tests for null extensions may already have reset
6809 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
6810 -- set, functions that need wrappers are recognized by having an
6811 -- alias that returns the parent type.
6813 if Comes_From_Source (Subp)
6814 or else No (Alias (Subp))
6815 or else Ekind (Subp) /= E_Function
6816 or else not Has_Controlling_Result (Subp)
6817 or else Is_Access_Type (Etype (Subp))
6818 or else Is_Abstract_Subprogram (Alias (Subp))
6819 or else Is_TSS (Subp, TSS_Stream_Input)
6823 elsif Is_Abstract_Subprogram (Subp)
6824 or else Requires_Overriding (Subp)
6826 (Is_Null_Extension (Etype (Subp))
6827 and then Etype (Alias (Subp)) /= Etype (Subp))
6829 Formal_List := No_List;
6830 Formal := First_Formal (Subp);
6832 if Present (Formal) then
6833 Formal_List := New_List;
6835 while Present (Formal) loop
6837 (Make_Parameter_Specification
6839 Defining_Identifier =>
6840 Make_Defining_Identifier (Sloc (Formal),
6841 Chars => Chars (Formal)),
6842 In_Present => In_Present (Parent (Formal)),
6843 Out_Present => Out_Present (Parent (Formal)),
6844 Null_Exclusion_Present =>
6845 Null_Exclusion_Present (Parent (Formal)),
6847 New_Reference_To (Etype (Formal), Loc),
6849 New_Copy_Tree (Expression (Parent (Formal)))),
6852 Next_Formal (Formal);
6857 Make_Function_Specification (Loc,
6858 Defining_Unit_Name =>
6859 Make_Defining_Identifier (Loc,
6860 Chars => Chars (Subp)),
6861 Parameter_Specifications => Formal_List,
6862 Result_Definition =>
6863 New_Reference_To (Etype (Subp), Loc));
6865 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
6866 Append_To (Decl_List, Func_Decl);
6868 -- Build a wrapper body that calls the parent function. The body
6869 -- contains a single return statement that returns an extension
6870 -- aggregate whose ancestor part is a call to the parent function,
6871 -- passing the formals as actuals (with any controlling arguments
6872 -- converted to the types of the corresponding formals of the
6873 -- parent function, which might be anonymous access types), and
6874 -- having a null extension.
6876 Formal := First_Formal (Subp);
6877 Par_Formal := First_Formal (Alias (Subp));
6878 Formal_Node := First (Formal_List);
6880 if Present (Formal) then
6881 Actual_List := New_List;
6883 Actual_List := No_List;
6886 while Present (Formal) loop
6887 if Is_Controlling_Formal (Formal) then
6888 Append_To (Actual_List,
6889 Make_Type_Conversion (Loc,
6891 New_Occurrence_Of (Etype (Par_Formal), Loc),
6894 (Defining_Identifier (Formal_Node), Loc)));
6899 (Defining_Identifier (Formal_Node), Loc));
6902 Next_Formal (Formal);
6903 Next_Formal (Par_Formal);
6908 Make_Simple_Return_Statement (Loc,
6910 Make_Extension_Aggregate (Loc,
6912 Make_Function_Call (Loc,
6913 Name => New_Reference_To (Alias (Subp), Loc),
6914 Parameter_Associations => Actual_List),
6915 Null_Record_Present => True));
6918 Make_Subprogram_Body (Loc,
6919 Specification => New_Copy_Tree (Func_Spec),
6920 Declarations => Empty_List,
6921 Handled_Statement_Sequence =>
6922 Make_Handled_Sequence_Of_Statements (Loc,
6923 Statements => New_List (Return_Stmt)));
6925 Set_Defining_Unit_Name
6926 (Specification (Func_Body),
6927 Make_Defining_Identifier (Loc, Chars (Subp)));
6929 Append_To (Body_List, Func_Body);
6931 -- Replace the inherited function with the wrapper function
6932 -- in the primitive operations list.
6934 Override_Dispatching_Operation
6935 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
6939 Next_Elmt (Prim_Elmt);
6941 end Make_Controlling_Function_Wrappers;
6947 -- <Make_Eq_if shared components>
6949 -- when V1 => <Make_Eq_Case> on subcomponents
6951 -- when Vn => <Make_Eq_Case> on subcomponents
6954 function Make_Eq_Case
6957 Discr : Entity_Id := Empty) return List_Id
6959 Loc : constant Source_Ptr := Sloc (E);
6960 Result : constant List_Id := New_List;
6965 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
6967 if No (Variant_Part (CL)) then
6971 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
6973 if No (Variant) then
6977 Alt_List := New_List;
6979 while Present (Variant) loop
6980 Append_To (Alt_List,
6981 Make_Case_Statement_Alternative (Loc,
6982 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
6983 Statements => Make_Eq_Case (E, Component_List (Variant))));
6985 Next_Non_Pragma (Variant);
6988 -- If we have an Unchecked_Union, use one of the parameters that
6989 -- captures the discriminants.
6991 if Is_Unchecked_Union (E) then
6993 Make_Case_Statement (Loc,
6994 Expression => New_Reference_To (Discr, Loc),
6995 Alternatives => Alt_List));
6999 Make_Case_Statement (Loc,
7001 Make_Selected_Component (Loc,
7002 Prefix => Make_Identifier (Loc, Name_X),
7003 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7004 Alternatives => Alt_List));
7025 -- or a null statement if the list L is empty
7029 L : List_Id) return Node_Id
7031 Loc : constant Source_Ptr := Sloc (E);
7033 Field_Name : Name_Id;
7038 return Make_Null_Statement (Loc);
7043 C := First_Non_Pragma (L);
7044 while Present (C) loop
7045 Field_Name := Chars (Defining_Identifier (C));
7047 -- The tags must not be compared: they are not part of the value.
7048 -- Ditto for the controller component, if present.
7050 -- Note also that in the following, we use Make_Identifier for
7051 -- the component names. Use of New_Reference_To to identify the
7052 -- components would be incorrect because the wrong entities for
7053 -- discriminants could be picked up in the private type case.
7055 if Field_Name /= Name_uTag
7057 Field_Name /= Name_uController
7059 Evolve_Or_Else (Cond,
7062 Make_Selected_Component (Loc,
7063 Prefix => Make_Identifier (Loc, Name_X),
7065 Make_Identifier (Loc, Field_Name)),
7068 Make_Selected_Component (Loc,
7069 Prefix => Make_Identifier (Loc, Name_Y),
7071 Make_Identifier (Loc, Field_Name))));
7074 Next_Non_Pragma (C);
7078 return Make_Null_Statement (Loc);
7082 Make_Implicit_If_Statement (E,
7084 Then_Statements => New_List (
7085 Make_Simple_Return_Statement (Loc,
7086 Expression => New_Occurrence_Of (Standard_False, Loc))));
7091 -------------------------------
7092 -- Make_Null_Procedure_Specs --
7093 -------------------------------
7095 procedure Make_Null_Procedure_Specs
7096 (Tag_Typ : Entity_Id;
7097 Decl_List : out List_Id)
7099 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7101 Formal_List : List_Id;
7102 Parent_Subp : Entity_Id;
7103 Prim_Elmt : Elmt_Id;
7104 Proc_Spec : Node_Id;
7105 Proc_Decl : Node_Id;
7108 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean;
7109 -- Returns True if E is a null procedure that is an interface primitive
7111 ---------------------------------
7112 -- Is_Null_Interface_Primitive --
7113 ---------------------------------
7115 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
7117 return Comes_From_Source (E)
7118 and then Is_Dispatching_Operation (E)
7119 and then Ekind (E) = E_Procedure
7120 and then Null_Present (Parent (E))
7121 and then Is_Interface (Find_Dispatching_Type (E));
7122 end Is_Null_Interface_Primitive;
7124 -- Start of processing for Make_Null_Procedure_Specs
7127 Decl_List := New_List;
7128 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7129 while Present (Prim_Elmt) loop
7130 Subp := Node (Prim_Elmt);
7132 -- If a null procedure inherited from an interface has not been
7133 -- overridden, then we build a null procedure declaration to
7134 -- override the inherited procedure.
7136 Parent_Subp := Alias (Subp);
7138 if Present (Parent_Subp)
7139 and then Is_Null_Interface_Primitive (Parent_Subp)
7141 Formal_List := No_List;
7142 Formal := First_Formal (Subp);
7144 if Present (Formal) then
7145 Formal_List := New_List;
7147 while Present (Formal) loop
7149 (Make_Parameter_Specification (Loc,
7150 Defining_Identifier =>
7151 Make_Defining_Identifier (Sloc (Formal),
7152 Chars => Chars (Formal)),
7153 In_Present => In_Present (Parent (Formal)),
7154 Out_Present => Out_Present (Parent (Formal)),
7155 Null_Exclusion_Present =>
7156 Null_Exclusion_Present (Parent (Formal)),
7158 New_Reference_To (Etype (Formal), Loc),
7160 New_Copy_Tree (Expression (Parent (Formal)))),
7163 Next_Formal (Formal);
7168 Make_Procedure_Specification (Loc,
7169 Defining_Unit_Name =>
7170 Make_Defining_Identifier (Loc, Chars (Subp)),
7171 Parameter_Specifications => Formal_List);
7172 Set_Null_Present (Proc_Spec);
7174 Proc_Decl := Make_Subprogram_Declaration (Loc, Proc_Spec);
7175 Append_To (Decl_List, Proc_Decl);
7176 Analyze (Proc_Decl);
7179 Next_Elmt (Prim_Elmt);
7181 end Make_Null_Procedure_Specs;
7183 -------------------------------------
7184 -- Make_Predefined_Primitive_Specs --
7185 -------------------------------------
7187 procedure Make_Predefined_Primitive_Specs
7188 (Tag_Typ : Entity_Id;
7189 Predef_List : out List_Id;
7190 Renamed_Eq : out Node_Id)
7192 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7193 Res : constant List_Id := New_List;
7195 Eq_Needed : Boolean;
7197 Eq_Name : Name_Id := Name_Op_Eq;
7199 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
7200 -- Returns true if Prim is a renaming of an unresolved predefined
7201 -- equality operation.
7203 -------------------------------
7204 -- Is_Predefined_Eq_Renaming --
7205 -------------------------------
7207 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
7209 return Chars (Prim) /= Name_Op_Eq
7210 and then Present (Alias (Prim))
7211 and then Comes_From_Source (Prim)
7212 and then Is_Intrinsic_Subprogram (Alias (Prim))
7213 and then Chars (Alias (Prim)) = Name_Op_Eq;
7214 end Is_Predefined_Eq_Renaming;
7216 -- Start of processing for Make_Predefined_Primitive_Specs
7219 Renamed_Eq := Empty;
7223 Append_To (Res, Predef_Spec_Or_Body (Loc,
7226 Profile => New_List (
7227 Make_Parameter_Specification (Loc,
7228 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7229 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7231 Ret_Type => Standard_Long_Long_Integer));
7233 -- Spec of _Alignment
7235 Append_To (Res, Predef_Spec_Or_Body (Loc,
7237 Name => Name_uAlignment,
7238 Profile => New_List (
7239 Make_Parameter_Specification (Loc,
7240 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7241 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7243 Ret_Type => Standard_Integer));
7245 -- Specs for dispatching stream attributes
7248 Stream_Op_TSS_Names :
7249 constant array (Integer range <>) of TSS_Name_Type :=
7255 for Op in Stream_Op_TSS_Names'Range loop
7256 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
7258 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
7259 Stream_Op_TSS_Names (Op)));
7264 -- Spec of "=" if expanded if the type is not limited and if a
7265 -- user defined "=" was not already declared for the non-full
7266 -- view of a private extension
7268 if not Is_Limited_Type (Tag_Typ) then
7271 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7272 while Present (Prim) loop
7274 -- If a primitive is encountered that renames the predefined
7275 -- equality operator before reaching any explicit equality
7276 -- primitive, then we still need to create a predefined
7277 -- equality function, because calls to it can occur via
7278 -- the renaming. A new name is created for the equality
7279 -- to avoid conflicting with any user-defined equality.
7280 -- (Note that this doesn't account for renamings of
7281 -- equality nested within subpackages???)
7283 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7284 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
7286 elsif Chars (Node (Prim)) = Name_Op_Eq
7287 and then (No (Alias (Node (Prim)))
7288 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
7289 N_Subprogram_Renaming_Declaration)
7290 and then Etype (First_Formal (Node (Prim))) =
7291 Etype (Next_Formal (First_Formal (Node (Prim))))
7292 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
7298 -- If the parent equality is abstract, the inherited equality is
7299 -- abstract as well, and no body can be created for for it.
7301 elsif Chars (Node (Prim)) = Name_Op_Eq
7302 and then Present (Alias (Node (Prim)))
7303 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
7312 -- If a renaming of predefined equality was found but there was no
7313 -- user-defined equality (so Eq_Needed is still true), then set the
7314 -- name back to Name_Op_Eq. But in the case where a user-defined
7315 -- equality was located after such a renaming, then the predefined
7316 -- equality function is still needed, so Eq_Needed must be set back
7319 if Eq_Name /= Name_Op_Eq then
7321 Eq_Name := Name_Op_Eq;
7328 Eq_Spec := Predef_Spec_Or_Body (Loc,
7331 Profile => New_List (
7332 Make_Parameter_Specification (Loc,
7333 Defining_Identifier =>
7334 Make_Defining_Identifier (Loc, Name_X),
7335 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7336 Make_Parameter_Specification (Loc,
7337 Defining_Identifier =>
7338 Make_Defining_Identifier (Loc, Name_Y),
7339 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7340 Ret_Type => Standard_Boolean);
7341 Append_To (Res, Eq_Spec);
7343 if Eq_Name /= Name_Op_Eq then
7344 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
7346 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7347 while Present (Prim) loop
7349 -- Any renamings of equality that appeared before an
7350 -- overriding equality must be updated to refer to the
7351 -- entity for the predefined equality, otherwise calls via
7352 -- the renaming would get incorrectly resolved to call the
7353 -- user-defined equality function.
7355 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7356 Set_Alias (Node (Prim), Renamed_Eq);
7358 -- Exit upon encountering a user-defined equality
7360 elsif Chars (Node (Prim)) = Name_Op_Eq
7361 and then No (Alias (Node (Prim)))
7371 -- Spec for dispatching assignment
7373 Append_To (Res, Predef_Spec_Or_Body (Loc,
7375 Name => Name_uAssign,
7376 Profile => New_List (
7377 Make_Parameter_Specification (Loc,
7378 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7379 Out_Present => True,
7380 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7382 Make_Parameter_Specification (Loc,
7383 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
7384 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
7387 -- Ada 2005: Generate declarations for the following primitive
7388 -- operations for limited interfaces and synchronized types that
7389 -- implement a limited interface.
7391 -- disp_asynchronous_select
7392 -- disp_conditional_select
7393 -- disp_get_prim_op_kind
7395 -- disp_timed_select
7397 -- These operations cannot be implemented on VM targets, so we simply
7398 -- disable their generation in this case. We also disable generation
7399 -- of these bodies if No_Dispatching_Calls is active.
7401 if Ada_Version >= Ada_05
7402 and then VM_Target = No_VM
7404 ((Is_Interface (Tag_Typ) and then Is_Limited_Record (Tag_Typ))
7405 or else (Is_Concurrent_Record_Type (Tag_Typ)
7406 and then Has_Abstract_Interfaces (Tag_Typ)))
7409 Make_Subprogram_Declaration (Loc,
7411 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
7414 Make_Subprogram_Declaration (Loc,
7416 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
7419 Make_Subprogram_Declaration (Loc,
7421 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
7424 Make_Subprogram_Declaration (Loc,
7426 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
7429 Make_Subprogram_Declaration (Loc,
7431 Make_Disp_Timed_Select_Spec (Tag_Typ)));
7434 -- Specs for finalization actions that may be required in case a future
7435 -- extension contain a controlled element. We generate those only for
7436 -- root tagged types where they will get dummy bodies or when the type
7437 -- has controlled components and their body must be generated. It is
7438 -- also impossible to provide those for tagged types defined within
7439 -- s-finimp since it would involve circularity problems
7441 if In_Finalization_Root (Tag_Typ) then
7444 -- We also skip these if finalization is not available
7446 elsif Restriction_Active (No_Finalization) then
7449 elsif Etype (Tag_Typ) = Tag_Typ
7450 or else Controlled_Type (Tag_Typ)
7452 -- Ada 2005 (AI-251): We must also generate these subprograms if
7453 -- the immediate ancestor is an interface to ensure the correct
7454 -- initialization of its dispatch table.
7456 or else (not Is_Interface (Tag_Typ)
7458 Is_Interface (Etype (Tag_Typ)))
7460 if not Is_Limited_Type (Tag_Typ) then
7462 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
7465 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
7469 end Make_Predefined_Primitive_Specs;
7471 ---------------------------------
7472 -- Needs_Simple_Initialization --
7473 ---------------------------------
7475 function Needs_Simple_Initialization (T : Entity_Id) return Boolean is
7477 -- Check for private type, in which case test applies to the underlying
7478 -- type of the private type.
7480 if Is_Private_Type (T) then
7482 RT : constant Entity_Id := Underlying_Type (T);
7485 if Present (RT) then
7486 return Needs_Simple_Initialization (RT);
7492 -- Cases needing simple initialization are access types, and, if pragma
7493 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
7496 elsif Is_Access_Type (T)
7497 or else (Init_Or_Norm_Scalars and then (Is_Scalar_Type (T)))
7501 -- If Initialize/Normalize_Scalars is in effect, string objects also
7502 -- need initialization, unless they are created in the course of
7503 -- expanding an aggregate (since in the latter case they will be
7504 -- filled with appropriate initializing values before they are used).
7506 elsif Init_Or_Norm_Scalars
7508 (Root_Type (T) = Standard_String
7509 or else Root_Type (T) = Standard_Wide_String
7510 or else Root_Type (T) = Standard_Wide_Wide_String)
7513 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
7520 end Needs_Simple_Initialization;
7522 ----------------------
7523 -- Predef_Deep_Spec --
7524 ----------------------
7526 function Predef_Deep_Spec
7528 Tag_Typ : Entity_Id;
7529 Name : TSS_Name_Type;
7530 For_Body : Boolean := False) return Node_Id
7536 if Name = TSS_Deep_Finalize then
7538 Type_B := Standard_Boolean;
7542 Make_Parameter_Specification (Loc,
7543 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
7545 Out_Present => True,
7547 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
7548 Type_B := Standard_Short_Short_Integer;
7552 Make_Parameter_Specification (Loc,
7553 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
7555 Out_Present => True,
7556 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
7559 Make_Parameter_Specification (Loc,
7560 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
7561 Parameter_Type => New_Reference_To (Type_B, Loc)));
7563 return Predef_Spec_Or_Body (Loc,
7564 Name => Make_TSS_Name (Tag_Typ, Name),
7567 For_Body => For_Body);
7570 when RE_Not_Available =>
7572 end Predef_Deep_Spec;
7574 -------------------------
7575 -- Predef_Spec_Or_Body --
7576 -------------------------
7578 function Predef_Spec_Or_Body
7580 Tag_Typ : Entity_Id;
7583 Ret_Type : Entity_Id := Empty;
7584 For_Body : Boolean := False) return Node_Id
7586 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
7590 Set_Is_Public (Id, Is_Public (Tag_Typ));
7592 -- The internal flag is set to mark these declarations because they have
7593 -- specific properties. First, they are primitives even if they are not
7594 -- defined in the type scope (the freezing point is not necessarily in
7595 -- the same scope). Second, the predefined equality can be overridden by
7596 -- a user-defined equality, no body will be generated in this case.
7598 Set_Is_Internal (Id);
7600 if not Debug_Generated_Code then
7601 Set_Debug_Info_Off (Id);
7604 if No (Ret_Type) then
7606 Make_Procedure_Specification (Loc,
7607 Defining_Unit_Name => Id,
7608 Parameter_Specifications => Profile);
7611 Make_Function_Specification (Loc,
7612 Defining_Unit_Name => Id,
7613 Parameter_Specifications => Profile,
7614 Result_Definition =>
7615 New_Reference_To (Ret_Type, Loc));
7618 -- If body case, return empty subprogram body. Note that this is ill-
7619 -- formed, because there is not even a null statement, and certainly not
7620 -- a return in the function case. The caller is expected to do surgery
7621 -- on the body to add the appropriate stuff.
7624 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
7626 -- For the case of Input/Output attributes applied to an abstract type,
7627 -- generate abstract specifications. These will never be called, but we
7628 -- need the slots allocated in the dispatching table so that attributes
7629 -- typ'Class'Input and typ'Class'Output will work properly.
7631 elsif (Is_TSS (Name, TSS_Stream_Input)
7633 Is_TSS (Name, TSS_Stream_Output))
7634 and then Is_Abstract_Type (Tag_Typ)
7636 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
7638 -- Normal spec case, where we return a subprogram declaration
7641 return Make_Subprogram_Declaration (Loc, Spec);
7643 end Predef_Spec_Or_Body;
7645 -----------------------------
7646 -- Predef_Stream_Attr_Spec --
7647 -----------------------------
7649 function Predef_Stream_Attr_Spec
7651 Tag_Typ : Entity_Id;
7652 Name : TSS_Name_Type;
7653 For_Body : Boolean := False) return Node_Id
7655 Ret_Type : Entity_Id;
7658 if Name = TSS_Stream_Input then
7659 Ret_Type := Tag_Typ;
7664 return Predef_Spec_Or_Body (Loc,
7665 Name => Make_TSS_Name (Tag_Typ, Name),
7667 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
7668 Ret_Type => Ret_Type,
7669 For_Body => For_Body);
7670 end Predef_Stream_Attr_Spec;
7672 ---------------------------------
7673 -- Predefined_Primitive_Bodies --
7674 ---------------------------------
7676 function Predefined_Primitive_Bodies
7677 (Tag_Typ : Entity_Id;
7678 Renamed_Eq : Node_Id) return List_Id
7680 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7681 Res : constant List_Id := New_List;
7684 Eq_Needed : Boolean;
7689 -- See if we have a predefined "=" operator
7691 if Present (Renamed_Eq) then
7693 Eq_Name := Chars (Renamed_Eq);
7699 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7700 while Present (Prim) loop
7701 if Chars (Node (Prim)) = Name_Op_Eq
7702 and then Is_Internal (Node (Prim))
7705 Eq_Name := Name_Op_Eq;
7712 -- Body of _Alignment
7714 Decl := Predef_Spec_Or_Body (Loc,
7716 Name => Name_uAlignment,
7717 Profile => New_List (
7718 Make_Parameter_Specification (Loc,
7719 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7720 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7722 Ret_Type => Standard_Integer,
7725 Set_Handled_Statement_Sequence (Decl,
7726 Make_Handled_Sequence_Of_Statements (Loc, New_List (
7727 Make_Simple_Return_Statement (Loc,
7729 Make_Attribute_Reference (Loc,
7730 Prefix => Make_Identifier (Loc, Name_X),
7731 Attribute_Name => Name_Alignment)))));
7733 Append_To (Res, Decl);
7737 Decl := Predef_Spec_Or_Body (Loc,
7740 Profile => New_List (
7741 Make_Parameter_Specification (Loc,
7742 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7743 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7745 Ret_Type => Standard_Long_Long_Integer,
7748 Set_Handled_Statement_Sequence (Decl,
7749 Make_Handled_Sequence_Of_Statements (Loc, New_List (
7750 Make_Simple_Return_Statement (Loc,
7752 Make_Attribute_Reference (Loc,
7753 Prefix => Make_Identifier (Loc, Name_X),
7754 Attribute_Name => Name_Size)))));
7756 Append_To (Res, Decl);
7758 -- Bodies for Dispatching stream IO routines. We need these only for
7759 -- non-limited types (in the limited case there is no dispatching).
7760 -- We also skip them if dispatching or finalization are not available.
7762 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
7763 and then No (TSS (Tag_Typ, TSS_Stream_Read))
7765 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
7766 Append_To (Res, Decl);
7769 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
7770 and then No (TSS (Tag_Typ, TSS_Stream_Write))
7772 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
7773 Append_To (Res, Decl);
7776 -- Skip bodies of _Input and _Output for the abstract case, since the
7777 -- corresponding specs are abstract (see Predef_Spec_Or_Body).
7779 if not Is_Abstract_Type (Tag_Typ) then
7780 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
7781 and then No (TSS (Tag_Typ, TSS_Stream_Input))
7783 Build_Record_Or_Elementary_Input_Function
7784 (Loc, Tag_Typ, Decl, Ent);
7785 Append_To (Res, Decl);
7788 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
7789 and then No (TSS (Tag_Typ, TSS_Stream_Output))
7791 Build_Record_Or_Elementary_Output_Procedure
7792 (Loc, Tag_Typ, Decl, Ent);
7793 Append_To (Res, Decl);
7797 -- Ada 2005: Generate bodies for the following primitive operations for
7798 -- limited interfaces and synchronized types that implement a limited
7801 -- disp_asynchronous_select
7802 -- disp_conditional_select
7803 -- disp_get_prim_op_kind
7805 -- disp_timed_select
7807 -- The interface versions will have null bodies
7809 -- These operations cannot be implemented on VM targets, so we simply
7810 -- disable their generation in this case. We also disable generation
7811 -- of these bodies if No_Dispatching_Calls is active.
7813 if Ada_Version >= Ada_05
7814 and then VM_Target = No_VM
7815 and then not Restriction_Active (No_Dispatching_Calls)
7817 ((Is_Interface (Tag_Typ) and then Is_Limited_Record (Tag_Typ))
7818 or else (Is_Concurrent_Record_Type (Tag_Typ)
7819 and then Has_Abstract_Interfaces (Tag_Typ)))
7821 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
7822 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
7823 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
7824 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
7825 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
7828 if not Is_Limited_Type (Tag_Typ) then
7830 -- Body for equality
7834 Predef_Spec_Or_Body (Loc,
7837 Profile => New_List (
7838 Make_Parameter_Specification (Loc,
7839 Defining_Identifier =>
7840 Make_Defining_Identifier (Loc, Name_X),
7841 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7843 Make_Parameter_Specification (Loc,
7844 Defining_Identifier =>
7845 Make_Defining_Identifier (Loc, Name_Y),
7846 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7848 Ret_Type => Standard_Boolean,
7852 Def : constant Node_Id := Parent (Tag_Typ);
7853 Stmts : constant List_Id := New_List;
7854 Variant_Case : Boolean := Has_Discriminants (Tag_Typ);
7855 Comps : Node_Id := Empty;
7856 Typ_Def : Node_Id := Type_Definition (Def);
7859 if Variant_Case then
7860 if Nkind (Typ_Def) = N_Derived_Type_Definition then
7861 Typ_Def := Record_Extension_Part (Typ_Def);
7864 if Present (Typ_Def) then
7865 Comps := Component_List (Typ_Def);
7868 Variant_Case := Present (Comps)
7869 and then Present (Variant_Part (Comps));
7872 if Variant_Case then
7874 Make_Eq_If (Tag_Typ, Discriminant_Specifications (Def)));
7875 Append_List_To (Stmts, Make_Eq_Case (Tag_Typ, Comps));
7877 Make_Simple_Return_Statement (Loc,
7878 Expression => New_Reference_To (Standard_True, Loc)));
7882 Make_Simple_Return_Statement (Loc,
7884 Expand_Record_Equality (Tag_Typ,
7886 Lhs => Make_Identifier (Loc, Name_X),
7887 Rhs => Make_Identifier (Loc, Name_Y),
7888 Bodies => Declarations (Decl))));
7891 Set_Handled_Statement_Sequence (Decl,
7892 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7894 Append_To (Res, Decl);
7897 -- Body for dispatching assignment
7900 Predef_Spec_Or_Body (Loc,
7902 Name => Name_uAssign,
7903 Profile => New_List (
7904 Make_Parameter_Specification (Loc,
7905 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7906 Out_Present => True,
7907 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7909 Make_Parameter_Specification (Loc,
7910 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
7911 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7914 Set_Handled_Statement_Sequence (Decl,
7915 Make_Handled_Sequence_Of_Statements (Loc, New_List (
7916 Make_Assignment_Statement (Loc,
7917 Name => Make_Identifier (Loc, Name_X),
7918 Expression => Make_Identifier (Loc, Name_Y)))));
7920 Append_To (Res, Decl);
7923 -- Generate dummy bodies for finalization actions of types that have
7924 -- no controlled components.
7926 -- Skip this processing if we are in the finalization routine in the
7927 -- runtime itself, otherwise we get hopelessly circularly confused!
7929 if In_Finalization_Root (Tag_Typ) then
7932 -- Skip this if finalization is not available
7934 elsif Restriction_Active (No_Finalization) then
7937 elsif (Etype (Tag_Typ) = Tag_Typ
7938 or else Is_Controlled (Tag_Typ)
7940 -- Ada 2005 (AI-251): We must also generate these subprograms
7941 -- if the immediate ancestor of Tag_Typ is an interface to
7942 -- ensure the correct initialization of its dispatch table.
7944 or else (not Is_Interface (Tag_Typ)
7946 Is_Interface (Etype (Tag_Typ))))
7947 and then not Has_Controlled_Component (Tag_Typ)
7949 if not Is_Limited_Type (Tag_Typ) then
7950 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
7952 if Is_Controlled (Tag_Typ) then
7953 Set_Handled_Statement_Sequence (Decl,
7954 Make_Handled_Sequence_Of_Statements (Loc,
7956 Ref => Make_Identifier (Loc, Name_V),
7958 Flist_Ref => Make_Identifier (Loc, Name_L),
7959 With_Attach => Make_Identifier (Loc, Name_B))));
7962 Set_Handled_Statement_Sequence (Decl,
7963 Make_Handled_Sequence_Of_Statements (Loc, New_List (
7964 Make_Null_Statement (Loc))));
7967 Append_To (Res, Decl);
7970 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
7972 if Is_Controlled (Tag_Typ) then
7973 Set_Handled_Statement_Sequence (Decl,
7974 Make_Handled_Sequence_Of_Statements (Loc,
7976 Ref => Make_Identifier (Loc, Name_V),
7978 With_Detach => Make_Identifier (Loc, Name_B))));
7981 Set_Handled_Statement_Sequence (Decl,
7982 Make_Handled_Sequence_Of_Statements (Loc, New_List (
7983 Make_Null_Statement (Loc))));
7986 Append_To (Res, Decl);
7990 end Predefined_Primitive_Bodies;
7992 ---------------------------------
7993 -- Predefined_Primitive_Freeze --
7994 ---------------------------------
7996 function Predefined_Primitive_Freeze
7997 (Tag_Typ : Entity_Id) return List_Id
7999 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8000 Res : constant List_Id := New_List;
8005 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8006 while Present (Prim) loop
8007 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8008 Frnodes := Freeze_Entity (Node (Prim), Loc);
8010 if Present (Frnodes) then
8011 Append_List_To (Res, Frnodes);
8019 end Predefined_Primitive_Freeze;
8021 -------------------------
8022 -- Stream_Operation_OK --
8023 -------------------------
8025 function Stream_Operation_OK
8027 Operation : TSS_Name_Type) return Boolean
8029 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
8032 -- Special case of a limited type extension: a default implementation
8033 -- of the stream attributes Read or Write exists if that attribute
8034 -- has been specified or is available for an ancestor type; a default
8035 -- implementation of the attribute Output (resp. Input) exists if the
8036 -- attribute has been specified or Write (resp. Read) is available for
8037 -- an ancestor type. The last condition only applies under Ada 2005.
8039 if Is_Limited_Type (Typ)
8040 and then Is_Tagged_Type (Typ)
8042 if Operation = TSS_Stream_Read then
8043 Has_Predefined_Or_Specified_Stream_Attribute :=
8044 Has_Specified_Stream_Read (Typ);
8046 elsif Operation = TSS_Stream_Write then
8047 Has_Predefined_Or_Specified_Stream_Attribute :=
8048 Has_Specified_Stream_Write (Typ);
8050 elsif Operation = TSS_Stream_Input then
8051 Has_Predefined_Or_Specified_Stream_Attribute :=
8052 Has_Specified_Stream_Input (Typ)
8054 (Ada_Version >= Ada_05
8055 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
8057 elsif Operation = TSS_Stream_Output then
8058 Has_Predefined_Or_Specified_Stream_Attribute :=
8059 Has_Specified_Stream_Output (Typ)
8061 (Ada_Version >= Ada_05
8062 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
8065 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
8067 if not Has_Predefined_Or_Specified_Stream_Attribute
8068 and then Is_Derived_Type (Typ)
8069 and then (Operation = TSS_Stream_Read
8070 or else Operation = TSS_Stream_Write)
8072 Has_Predefined_Or_Specified_Stream_Attribute :=
8074 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
8078 return (not Is_Limited_Type (Typ)
8079 or else Has_Predefined_Or_Specified_Stream_Attribute)
8080 and then not Has_Unknown_Discriminants (Typ)
8081 and then not (Is_Interface (Typ)
8082 and then (Is_Task_Interface (Typ)
8083 or else Is_Protected_Interface (Typ)
8084 or else Is_Synchronized_Interface (Typ)))
8085 and then not Restriction_Active (No_Streams)
8086 and then not Restriction_Active (No_Dispatch)
8087 and then not No_Run_Time_Mode
8088 and then RTE_Available (RE_Tag)
8089 and then RTE_Available (RE_Root_Stream_Type);
8090 end Stream_Operation_OK;