1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Aggr; use Exp_Aggr;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Smem; use Exp_Smem;
40 with Exp_Strm; use Exp_Strm;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Nlists; use Nlists;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Attr; use Sem_Attr;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
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 Entity_Id);
242 -- Create a list with the specs of the predefined primitive operations.
243 -- For tagged types that are interfaces all these primitives are defined
246 -- The following entries are present for all tagged types, and provide
247 -- the results of the corresponding attribute applied to the object.
248 -- Dispatching is required in general, since the result of the attribute
249 -- will vary with the actual object subtype.
251 -- _alignment provides result of 'Alignment attribute
252 -- _size provides result of 'Size attribute
253 -- typSR provides result of 'Read attribute
254 -- typSW provides result of 'Write attribute
255 -- typSI provides result of 'Input attribute
256 -- typSO provides result of 'Output attribute
258 -- The following entries are additionally present for non-limited tagged
259 -- types, and implement additional dispatching operations for predefined
262 -- _equality implements "=" operator
263 -- _assign implements assignment operation
264 -- typDF implements deep finalization
265 -- typDA implements deep adjust
267 -- The latter two are empty procedures unless the type contains some
268 -- controlled components that require finalization actions (the deep
269 -- in the name refers to the fact that the action applies to components).
271 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
272 -- returns the value Empty, or else the defining unit name for the
273 -- predefined equality function in the case where the type has a primitive
274 -- operation that is a renaming of predefined equality (but only if there
275 -- is also an overriding user-defined equality function). The returned
276 -- Renamed_Eq will be passed to the corresponding parameter of
277 -- Predefined_Primitive_Bodies.
279 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
280 -- returns True if there are representation clauses for type T that are not
281 -- inherited. If the result is false, the init_proc and the discriminant
282 -- checking functions of the parent can be reused by a derived type.
284 procedure Make_Controlling_Function_Wrappers
285 (Tag_Typ : Entity_Id;
286 Decl_List : out List_Id;
287 Body_List : out List_Id);
288 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
289 -- associated with inherited functions with controlling results which
290 -- are not overridden. The body of each wrapper function consists solely
291 -- of a return statement whose expression is an extension aggregate
292 -- invoking the inherited subprogram's parent subprogram and extended
293 -- with a null association list.
295 procedure Make_Null_Procedure_Specs
296 (Tag_Typ : Entity_Id;
297 Decl_List : out List_Id);
298 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
299 -- null procedures inherited from an interface type that have not been
300 -- overridden. Only one null procedure will be created for a given set of
301 -- inherited null procedures with homographic profiles.
303 function Predef_Spec_Or_Body
308 Ret_Type : Entity_Id := Empty;
309 For_Body : Boolean := False) return Node_Id;
310 -- This function generates the appropriate expansion for a predefined
311 -- primitive operation specified by its name, parameter profile and
312 -- return type (Empty means this is a procedure). If For_Body is false,
313 -- then the returned node is a subprogram declaration. If For_Body is
314 -- true, then the returned node is a empty subprogram body containing
315 -- no declarations and no statements.
317 function Predef_Stream_Attr_Spec
320 Name : TSS_Name_Type;
321 For_Body : Boolean := False) return Node_Id;
322 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
323 -- input and output attribute whose specs are constructed in Exp_Strm.
325 function Predef_Deep_Spec
328 Name : TSS_Name_Type;
329 For_Body : Boolean := False) return Node_Id;
330 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
331 -- and _deep_finalize
333 function Predefined_Primitive_Bodies
334 (Tag_Typ : Entity_Id;
335 Renamed_Eq : Entity_Id) return List_Id;
336 -- Create the bodies of the predefined primitives that are described in
337 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
338 -- the defining unit name of the type's predefined equality as returned
339 -- by Make_Predefined_Primitive_Specs.
341 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
342 -- Freeze entities of all predefined primitive operations. This is needed
343 -- because the bodies of these operations do not normally do any freezing.
345 function Stream_Operation_OK
347 Operation : TSS_Name_Type) return Boolean;
348 -- Check whether the named stream operation must be emitted for a given
349 -- type. The rules for inheritance of stream attributes by type extensions
350 -- are enforced by this function. Furthermore, various restrictions prevent
351 -- the generation of these operations, as a useful optimization or for
352 -- certification purposes.
354 ---------------------
355 -- Add_Final_Chain --
356 ---------------------
358 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id is
359 Loc : constant Source_Ptr := Sloc (Def_Id);
364 Make_Defining_Identifier (Loc,
365 New_External_Name (Chars (Def_Id), 'L'));
367 Append_Freeze_Action (Def_Id,
368 Make_Object_Declaration (Loc,
369 Defining_Identifier => Flist,
371 New_Reference_To (RTE (RE_List_Controller), Loc)));
376 --------------------------
377 -- Adjust_Discriminants --
378 --------------------------
380 -- This procedure attempts to define subtypes for discriminants that are
381 -- more restrictive than those declared. Such a replacement is possible if
382 -- we can demonstrate that values outside the restricted range would cause
383 -- constraint errors in any case. The advantage of restricting the
384 -- discriminant types in this way is that the maximum size of the variant
385 -- record can be calculated more conservatively.
387 -- An example of a situation in which we can perform this type of
388 -- restriction is the following:
390 -- subtype B is range 1 .. 10;
391 -- type Q is array (B range <>) of Integer;
393 -- type V (N : Natural) is record
397 -- In this situation, we can restrict the upper bound of N to 10, since
398 -- any larger value would cause a constraint error in any case.
400 -- There are many situations in which such restriction is possible, but
401 -- for now, we just look for cases like the above, where the component
402 -- in question is a one dimensional array whose upper bound is one of
403 -- the record discriminants. Also the component must not be part of
404 -- any variant part, since then the component does not always exist.
406 procedure Adjust_Discriminants (Rtype : Entity_Id) is
407 Loc : constant Source_Ptr := Sloc (Rtype);
424 Comp := First_Component (Rtype);
425 while Present (Comp) loop
427 -- If our parent is a variant, quit, we do not look at components
428 -- that are in variant parts, because they may not always exist.
430 P := Parent (Comp); -- component declaration
431 P := Parent (P); -- component list
433 exit when Nkind (Parent (P)) = N_Variant;
435 -- We are looking for a one dimensional array type
437 Ctyp := Etype (Comp);
439 if not Is_Array_Type (Ctyp)
440 or else Number_Dimensions (Ctyp) > 1
445 -- The lower bound must be constant, and the upper bound is a
446 -- discriminant (which is a discriminant of the current record).
448 Ityp := Etype (First_Index (Ctyp));
449 Lo := Type_Low_Bound (Ityp);
450 Hi := Type_High_Bound (Ityp);
452 if not Compile_Time_Known_Value (Lo)
453 or else Nkind (Hi) /= N_Identifier
454 or else No (Entity (Hi))
455 or else Ekind (Entity (Hi)) /= E_Discriminant
460 -- We have an array with appropriate bounds
462 Loval := Expr_Value (Lo);
463 Discr := Entity (Hi);
464 Dtyp := Etype (Discr);
466 -- See if the discriminant has a known upper bound
468 Dhi := Type_High_Bound (Dtyp);
470 if not Compile_Time_Known_Value (Dhi) then
474 Dhiv := Expr_Value (Dhi);
476 -- See if base type of component array has known upper bound
478 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
480 if not Compile_Time_Known_Value (Ahi) then
484 Ahiv := Expr_Value (Ahi);
486 -- The condition for doing the restriction is that the high bound
487 -- of the discriminant is greater than the low bound of the array,
488 -- and is also greater than the high bound of the base type index.
490 if Dhiv > Loval and then Dhiv > Ahiv then
492 -- We can reset the upper bound of the discriminant type to
493 -- whichever is larger, the low bound of the component, or
494 -- the high bound of the base type array index.
496 -- We build a subtype that is declared as
498 -- subtype Tnn is discr_type range discr_type'First .. max;
500 -- And insert this declaration into the tree. The type of the
501 -- discriminant is then reset to this more restricted subtype.
503 Tnn := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
505 Insert_Action (Declaration_Node (Rtype),
506 Make_Subtype_Declaration (Loc,
507 Defining_Identifier => Tnn,
508 Subtype_Indication =>
509 Make_Subtype_Indication (Loc,
510 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
512 Make_Range_Constraint (Loc,
516 Make_Attribute_Reference (Loc,
517 Attribute_Name => Name_First,
518 Prefix => New_Occurrence_Of (Dtyp, Loc)),
520 Make_Integer_Literal (Loc,
521 Intval => UI_Max (Loval, Ahiv)))))));
523 Set_Etype (Discr, Tnn);
527 Next_Component (Comp);
529 end Adjust_Discriminants;
531 ---------------------------
532 -- Build_Array_Init_Proc --
533 ---------------------------
535 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
536 Loc : constant Source_Ptr := Sloc (Nod);
537 Comp_Type : constant Entity_Id := Component_Type (A_Type);
538 Index_List : List_Id;
540 Body_Stmts : List_Id;
541 Has_Default_Init : Boolean;
543 function Init_Component return List_Id;
544 -- Create one statement to initialize one array component, designated
545 -- by a full set of indices.
547 function Init_One_Dimension (N : Int) return List_Id;
548 -- Create loop to initialize one dimension of the array. The single
549 -- statement in the loop body initializes the inner dimensions if any,
550 -- or else the single component. Note that this procedure is called
551 -- recursively, with N being the dimension to be initialized. A call
552 -- with N greater than the number of dimensions simply generates the
553 -- component initialization, terminating the recursion.
559 function Init_Component return List_Id is
564 Make_Indexed_Component (Loc,
565 Prefix => Make_Identifier (Loc, Name_uInit),
566 Expressions => Index_List);
568 if Needs_Simple_Initialization (Comp_Type) then
569 Set_Assignment_OK (Comp);
571 Make_Assignment_Statement (Loc,
575 (Comp_Type, Nod, Component_Size (A_Type))));
578 Clean_Task_Names (Comp_Type, Proc_Id);
580 Build_Initialization_Call
581 (Loc, Comp, Comp_Type,
582 In_Init_Proc => True,
583 Enclos_Type => A_Type);
587 ------------------------
588 -- Init_One_Dimension --
589 ------------------------
591 function Init_One_Dimension (N : Int) return List_Id is
595 -- If the component does not need initializing, then there is nothing
596 -- to do here, so we return a null body. This occurs when generating
597 -- the dummy Init_Proc needed for Initialize_Scalars processing.
599 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
600 and then not Needs_Simple_Initialization (Comp_Type)
601 and then not Has_Task (Comp_Type)
603 return New_List (Make_Null_Statement (Loc));
605 -- If all dimensions dealt with, we simply initialize the component
607 elsif N > Number_Dimensions (A_Type) then
608 return Init_Component;
610 -- Here we generate the required loop
614 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
616 Append (New_Reference_To (Index, Loc), Index_List);
619 Make_Implicit_Loop_Statement (Nod,
622 Make_Iteration_Scheme (Loc,
623 Loop_Parameter_Specification =>
624 Make_Loop_Parameter_Specification (Loc,
625 Defining_Identifier => Index,
626 Discrete_Subtype_Definition =>
627 Make_Attribute_Reference (Loc,
628 Prefix => Make_Identifier (Loc, Name_uInit),
629 Attribute_Name => Name_Range,
630 Expressions => New_List (
631 Make_Integer_Literal (Loc, N))))),
632 Statements => Init_One_Dimension (N + 1)));
634 end Init_One_Dimension;
636 -- Start of processing for Build_Array_Init_Proc
639 -- Nothing to generate in the following cases:
641 -- 1. Initialization is suppressed for the type
642 -- 2. The type is a value type, in the CIL sense.
643 -- 3. An initialization already exists for the base type
645 if Suppress_Init_Proc (A_Type)
646 or else Is_Value_Type (Comp_Type)
647 or else Present (Base_Init_Proc (A_Type))
652 Index_List := New_List;
654 -- We need an initialization procedure if any of the following is true:
656 -- 1. The component type has an initialization procedure
657 -- 2. The component type needs simple initialization
658 -- 3. Tasks are present
659 -- 4. The type is marked as a public entity
661 -- The reason for the public entity test is to deal properly with the
662 -- Initialize_Scalars pragma. This pragma can be set in the client and
663 -- not in the declaring package, this means the client will make a call
664 -- to the initialization procedure (because one of conditions 1-3 must
665 -- apply in this case), and we must generate a procedure (even if it is
666 -- null) to satisfy the call in this case.
668 -- Exception: do not build an array init_proc for a type whose root
669 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
670 -- is no place to put the code, and in any case we handle initialization
671 -- of such types (in the Initialize_Scalars case, that's the only time
672 -- the issue arises) in a special manner anyway which does not need an
675 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
676 or else Needs_Simple_Initialization (Comp_Type)
677 or else Has_Task (Comp_Type);
680 or else (not Restriction_Active (No_Initialize_Scalars)
681 and then Is_Public (A_Type)
682 and then Root_Type (A_Type) /= Standard_String
683 and then Root_Type (A_Type) /= Standard_Wide_String
684 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
687 Make_Defining_Identifier (Loc,
688 Chars => Make_Init_Proc_Name (A_Type));
690 -- If No_Default_Initialization restriction is active, then we don't
691 -- want to build an init_proc, but we need to mark that an init_proc
692 -- would be needed if this restriction was not active (so that we can
693 -- detect attempts to call it), so set a dummy init_proc in place.
694 -- This is only done though when actual default initialization is
695 -- needed (and not done when only Is_Public is True), since otherwise
696 -- objects such as arrays of scalars could be wrongly flagged as
697 -- violating the restriction.
699 if Restriction_Active (No_Default_Initialization) then
700 if Has_Default_Init then
701 Set_Init_Proc (A_Type, Proc_Id);
707 Body_Stmts := Init_One_Dimension (1);
710 Make_Subprogram_Body (Loc,
712 Make_Procedure_Specification (Loc,
713 Defining_Unit_Name => Proc_Id,
714 Parameter_Specifications => Init_Formals (A_Type)),
715 Declarations => New_List,
716 Handled_Statement_Sequence =>
717 Make_Handled_Sequence_Of_Statements (Loc,
718 Statements => Body_Stmts)));
720 Set_Ekind (Proc_Id, E_Procedure);
721 Set_Is_Public (Proc_Id, Is_Public (A_Type));
722 Set_Is_Internal (Proc_Id);
723 Set_Has_Completion (Proc_Id);
725 if not Debug_Generated_Code then
726 Set_Debug_Info_Off (Proc_Id);
729 -- Set inlined unless controlled stuff or tasks around, in which
730 -- case we do not want to inline, because nested stuff may cause
731 -- difficulties in inter-unit inlining, and furthermore there is
732 -- in any case no point in inlining such complex init procs.
734 if not Has_Task (Proc_Id)
735 and then not Needs_Finalization (Proc_Id)
737 Set_Is_Inlined (Proc_Id);
740 -- Associate Init_Proc with type, and determine if the procedure
741 -- is null (happens because of the Initialize_Scalars pragma case,
742 -- where we have to generate a null procedure in case it is called
743 -- by a client with Initialize_Scalars set). Such procedures have
744 -- to be generated, but do not have to be called, so we mark them
745 -- as null to suppress the call.
747 Set_Init_Proc (A_Type, Proc_Id);
749 if List_Length (Body_Stmts) = 1
750 and then Nkind (First (Body_Stmts)) = N_Null_Statement
752 Set_Is_Null_Init_Proc (Proc_Id);
755 -- Try to build a static aggregate to initialize statically
756 -- objects of the type. This can only be done for constrained
757 -- one-dimensional arrays with static bounds.
759 Set_Static_Initialization
761 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
764 end Build_Array_Init_Proc;
766 -----------------------------
767 -- Build_Class_Wide_Master --
768 -----------------------------
770 procedure Build_Class_Wide_Master (T : Entity_Id) is
771 Loc : constant Source_Ptr := Sloc (T);
778 -- Nothing to do if there is no task hierarchy
780 if Restriction_Active (No_Task_Hierarchy) then
784 -- Find declaration that created the access type: either a type
785 -- declaration, or an object declaration with an access definition,
786 -- in which case the type is anonymous.
789 P := Associated_Node_For_Itype (T);
794 -- Nothing to do if we already built a master entity for this scope
796 if not Has_Master_Entity (Scope (T)) then
798 -- First build the master entity
799 -- _Master : constant Master_Id := Current_Master.all;
800 -- and insert it just before the current declaration.
803 Make_Object_Declaration (Loc,
804 Defining_Identifier =>
805 Make_Defining_Identifier (Loc, Name_uMaster),
806 Constant_Present => True,
807 Object_Definition => New_Reference_To (Standard_Integer, Loc),
809 Make_Explicit_Dereference (Loc,
810 New_Reference_To (RTE (RE_Current_Master), Loc)));
812 Insert_Action (P, Decl);
814 Set_Has_Master_Entity (Scope (T));
816 -- Now mark the containing scope as a task master
819 while Nkind (Par) /= N_Compilation_Unit loop
822 -- If we fall off the top, we are at the outer level, and the
823 -- environment task is our effective master, so nothing to mark.
826 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
828 Set_Is_Task_Master (Par, True);
834 -- Now define the renaming of the master_id
837 Make_Defining_Identifier (Loc,
838 New_External_Name (Chars (T), 'M'));
841 Make_Object_Renaming_Declaration (Loc,
842 Defining_Identifier => M_Id,
843 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
844 Name => Make_Identifier (Loc, Name_uMaster));
845 Insert_Before (P, Decl);
848 Set_Master_Id (T, M_Id);
851 when RE_Not_Available =>
853 end Build_Class_Wide_Master;
855 --------------------------------
856 -- Build_Discr_Checking_Funcs --
857 --------------------------------
859 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
862 Enclosing_Func_Id : Entity_Id;
867 function Build_Case_Statement
868 (Case_Id : Entity_Id;
869 Variant : Node_Id) return Node_Id;
870 -- Build a case statement containing only two alternatives. The first
871 -- alternative corresponds exactly to the discrete choices given on the
872 -- variant with contains the components that we are generating the
873 -- checks for. If the discriminant is one of these return False. The
874 -- second alternative is an OTHERS choice that will return True
875 -- indicating the discriminant did not match.
877 function Build_Dcheck_Function
878 (Case_Id : Entity_Id;
879 Variant : Node_Id) return Entity_Id;
880 -- Build the discriminant checking function for a given variant
882 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
883 -- Builds the discriminant checking function for each variant of the
884 -- given variant part of the record type.
886 --------------------------
887 -- Build_Case_Statement --
888 --------------------------
890 function Build_Case_Statement
891 (Case_Id : Entity_Id;
892 Variant : Node_Id) return Node_Id
894 Alt_List : constant List_Id := New_List;
895 Actuals_List : List_Id;
897 Case_Alt_Node : Node_Id;
899 Choice_List : List_Id;
901 Return_Node : Node_Id;
904 Case_Node := New_Node (N_Case_Statement, Loc);
906 -- Replace the discriminant which controls the variant, with the name
907 -- of the formal of the checking function.
909 Set_Expression (Case_Node,
910 Make_Identifier (Loc, Chars (Case_Id)));
912 Choice := First (Discrete_Choices (Variant));
914 if Nkind (Choice) = N_Others_Choice then
915 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
917 Choice_List := New_Copy_List (Discrete_Choices (Variant));
920 if not Is_Empty_List (Choice_List) then
921 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
922 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
924 -- In case this is a nested variant, we need to return the result
925 -- of the discriminant checking function for the immediately
926 -- enclosing variant.
928 if Present (Enclosing_Func_Id) then
929 Actuals_List := New_List;
931 D := First_Discriminant (Rec_Id);
932 while Present (D) loop
933 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
934 Next_Discriminant (D);
938 Make_Simple_Return_Statement (Loc,
940 Make_Function_Call (Loc,
942 New_Reference_To (Enclosing_Func_Id, Loc),
943 Parameter_Associations =>
948 Make_Simple_Return_Statement (Loc,
950 New_Reference_To (Standard_False, Loc));
953 Set_Statements (Case_Alt_Node, New_List (Return_Node));
954 Append (Case_Alt_Node, Alt_List);
957 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
958 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
959 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
962 Make_Simple_Return_Statement (Loc,
964 New_Reference_To (Standard_True, Loc));
966 Set_Statements (Case_Alt_Node, New_List (Return_Node));
967 Append (Case_Alt_Node, Alt_List);
969 Set_Alternatives (Case_Node, Alt_List);
971 end Build_Case_Statement;
973 ---------------------------
974 -- Build_Dcheck_Function --
975 ---------------------------
977 function Build_Dcheck_Function
978 (Case_Id : Entity_Id;
979 Variant : Node_Id) return Entity_Id
983 Parameter_List : List_Id;
987 Body_Node := New_Node (N_Subprogram_Body, Loc);
988 Sequence := Sequence + 1;
991 Make_Defining_Identifier (Loc,
992 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
994 Spec_Node := New_Node (N_Function_Specification, Loc);
995 Set_Defining_Unit_Name (Spec_Node, Func_Id);
997 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
999 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1000 Set_Result_Definition (Spec_Node,
1001 New_Reference_To (Standard_Boolean, Loc));
1002 Set_Specification (Body_Node, Spec_Node);
1003 Set_Declarations (Body_Node, New_List);
1005 Set_Handled_Statement_Sequence (Body_Node,
1006 Make_Handled_Sequence_Of_Statements (Loc,
1007 Statements => New_List (
1008 Build_Case_Statement (Case_Id, Variant))));
1010 Set_Ekind (Func_Id, E_Function);
1011 Set_Mechanism (Func_Id, Default_Mechanism);
1012 Set_Is_Inlined (Func_Id, True);
1013 Set_Is_Pure (Func_Id, True);
1014 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1015 Set_Is_Internal (Func_Id, True);
1017 if not Debug_Generated_Code then
1018 Set_Debug_Info_Off (Func_Id);
1021 Analyze (Body_Node);
1023 Append_Freeze_Action (Rec_Id, Body_Node);
1024 Set_Dcheck_Function (Variant, Func_Id);
1026 end Build_Dcheck_Function;
1028 ----------------------------
1029 -- Build_Dcheck_Functions --
1030 ----------------------------
1032 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1033 Component_List_Node : Node_Id;
1035 Discr_Name : Entity_Id;
1036 Func_Id : Entity_Id;
1038 Saved_Enclosing_Func_Id : Entity_Id;
1041 -- Build the discriminant-checking function for each variant, and
1042 -- label all components of that variant with the function's name.
1043 -- We only Generate a discriminant-checking function when the
1044 -- variant is not empty, to prevent the creation of dead code.
1045 -- The exception to that is when Frontend_Layout_On_Target is set,
1046 -- because the variant record size function generated in package
1047 -- Layout needs to generate calls to all discriminant-checking
1048 -- functions, including those for empty variants.
1050 Discr_Name := Entity (Name (Variant_Part_Node));
1051 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1053 while Present (Variant) loop
1054 Component_List_Node := Component_List (Variant);
1056 if not Null_Present (Component_List_Node)
1057 or else Frontend_Layout_On_Target
1059 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1061 First_Non_Pragma (Component_Items (Component_List_Node));
1063 while Present (Decl) loop
1064 Set_Discriminant_Checking_Func
1065 (Defining_Identifier (Decl), Func_Id);
1067 Next_Non_Pragma (Decl);
1070 if Present (Variant_Part (Component_List_Node)) then
1071 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1072 Enclosing_Func_Id := Func_Id;
1073 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1074 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1078 Next_Non_Pragma (Variant);
1080 end Build_Dcheck_Functions;
1082 -- Start of processing for Build_Discr_Checking_Funcs
1085 -- Only build if not done already
1087 if not Discr_Check_Funcs_Built (N) then
1088 Type_Def := Type_Definition (N);
1090 if Nkind (Type_Def) = N_Record_Definition then
1091 if No (Component_List (Type_Def)) then -- null record.
1094 V := Variant_Part (Component_List (Type_Def));
1097 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1098 if No (Component_List (Record_Extension_Part (Type_Def))) then
1102 (Component_List (Record_Extension_Part (Type_Def)));
1106 Rec_Id := Defining_Identifier (N);
1108 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1110 Enclosing_Func_Id := Empty;
1111 Build_Dcheck_Functions (V);
1114 Set_Discr_Check_Funcs_Built (N);
1116 end Build_Discr_Checking_Funcs;
1118 --------------------------------
1119 -- Build_Discriminant_Formals --
1120 --------------------------------
1122 function Build_Discriminant_Formals
1123 (Rec_Id : Entity_Id;
1124 Use_Dl : Boolean) return List_Id
1126 Loc : Source_Ptr := Sloc (Rec_Id);
1127 Parameter_List : constant List_Id := New_List;
1130 Param_Spec_Node : Node_Id;
1133 if Has_Discriminants (Rec_Id) then
1134 D := First_Discriminant (Rec_Id);
1135 while Present (D) loop
1139 Formal := Discriminal (D);
1141 Formal := Make_Defining_Identifier (Loc, Chars (D));
1145 Make_Parameter_Specification (Loc,
1146 Defining_Identifier => Formal,
1148 New_Reference_To (Etype (D), Loc));
1149 Append (Param_Spec_Node, Parameter_List);
1150 Next_Discriminant (D);
1154 return Parameter_List;
1155 end Build_Discriminant_Formals;
1157 --------------------------------------
1158 -- Build_Equivalent_Array_Aggregate --
1159 --------------------------------------
1161 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1162 Loc : constant Source_Ptr := Sloc (T);
1163 Comp_Type : constant Entity_Id := Component_Type (T);
1164 Index_Type : constant Entity_Id := Etype (First_Index (T));
1165 Proc : constant Entity_Id := Base_Init_Proc (T);
1171 if not Is_Constrained (T)
1172 or else Number_Dimensions (T) > 1
1175 Initialization_Warning (T);
1179 Lo := Type_Low_Bound (Index_Type);
1180 Hi := Type_High_Bound (Index_Type);
1182 if not Compile_Time_Known_Value (Lo)
1183 or else not Compile_Time_Known_Value (Hi)
1185 Initialization_Warning (T);
1189 if Is_Record_Type (Comp_Type)
1190 and then Present (Base_Init_Proc (Comp_Type))
1192 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1195 Initialization_Warning (T);
1200 Initialization_Warning (T);
1204 Aggr := Make_Aggregate (Loc, No_List, New_List);
1205 Set_Etype (Aggr, T);
1206 Set_Aggregate_Bounds (Aggr,
1208 Low_Bound => New_Copy (Lo),
1209 High_Bound => New_Copy (Hi)));
1210 Set_Parent (Aggr, Parent (Proc));
1212 Append_To (Component_Associations (Aggr),
1213 Make_Component_Association (Loc,
1217 Low_Bound => New_Copy (Lo),
1218 High_Bound => New_Copy (Hi))),
1219 Expression => Expr));
1221 if Static_Array_Aggregate (Aggr) then
1224 Initialization_Warning (T);
1227 end Build_Equivalent_Array_Aggregate;
1229 ---------------------------------------
1230 -- Build_Equivalent_Record_Aggregate --
1231 ---------------------------------------
1233 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1237 -- Start of processing for Build_Equivalent_Record_Aggregate
1240 if not Is_Record_Type (T)
1241 or else Has_Discriminants (T)
1242 or else Is_Limited_Type (T)
1243 or else Has_Non_Standard_Rep (T)
1245 Initialization_Warning (T);
1249 Comp := First_Component (T);
1251 -- A null record needs no warning
1257 while Present (Comp) loop
1259 -- Array components are acceptable if initialized by a positional
1260 -- aggregate with static components.
1262 if Is_Array_Type (Etype (Comp)) then
1264 Comp_Type : constant Entity_Id := Component_Type (Etype (Comp));
1267 if Nkind (Parent (Comp)) /= N_Component_Declaration
1268 or else No (Expression (Parent (Comp)))
1269 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1271 Initialization_Warning (T);
1274 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1276 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1277 or else not Compile_Time_Known_Value
1278 (Type_High_Bound (Comp_Type)))
1280 Initialization_Warning (T);
1284 not Static_Array_Aggregate (Expression (Parent (Comp)))
1286 Initialization_Warning (T);
1291 elsif Is_Scalar_Type (Etype (Comp)) then
1292 if Nkind (Parent (Comp)) /= N_Component_Declaration
1293 or else No (Expression (Parent (Comp)))
1294 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1296 Initialization_Warning (T);
1300 -- For now, other types are excluded
1303 Initialization_Warning (T);
1307 Next_Component (Comp);
1310 -- All components have static initialization. Build positional
1311 -- aggregate from the given expressions or defaults.
1313 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1314 Set_Parent (Agg, Parent (T));
1316 Comp := First_Component (T);
1317 while Present (Comp) loop
1319 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1320 Next_Component (Comp);
1323 Analyze_And_Resolve (Agg, T);
1325 end Build_Equivalent_Record_Aggregate;
1327 -------------------------------
1328 -- Build_Initialization_Call --
1329 -------------------------------
1331 -- References to a discriminant inside the record type declaration can
1332 -- appear either in the subtype_indication to constrain a record or an
1333 -- array, or as part of a larger expression given for the initial value
1334 -- of a component. In both of these cases N appears in the record
1335 -- initialization procedure and needs to be replaced by the formal
1336 -- parameter of the initialization procedure which corresponds to that
1339 -- In the example below, references to discriminants D1 and D2 in proc_1
1340 -- are replaced by references to formals with the same name
1343 -- A similar replacement is done for calls to any record initialization
1344 -- procedure for any components that are themselves of a record type.
1346 -- type R (D1, D2 : Integer) is record
1347 -- X : Integer := F * D1;
1348 -- Y : Integer := F * D2;
1351 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1355 -- Out_2.X := F * D1;
1356 -- Out_2.Y := F * D2;
1359 function Build_Initialization_Call
1363 In_Init_Proc : Boolean := False;
1364 Enclos_Type : Entity_Id := Empty;
1365 Discr_Map : Elist_Id := New_Elmt_List;
1366 With_Default_Init : Boolean := False) return List_Id
1368 First_Arg : Node_Id;
1374 Proc : constant Entity_Id := Base_Init_Proc (Typ);
1375 Init_Type : constant Entity_Id := Etype (First_Formal (Proc));
1376 Full_Init_Type : constant Entity_Id := Underlying_Type (Init_Type);
1377 Res : constant List_Id := New_List;
1378 Full_Type : Entity_Id := Typ;
1379 Controller_Typ : Entity_Id;
1382 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1383 -- is active (in which case we make the call anyway, since in the
1384 -- actual compiled client it may be non null).
1385 -- Also nothing to do for value types.
1387 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1388 or else Is_Value_Type (Typ)
1389 or else Is_Value_Type (Component_Type (Typ))
1394 -- Go to full view if private type. In the case of successive
1395 -- private derivations, this can require more than one step.
1397 while Is_Private_Type (Full_Type)
1398 and then Present (Full_View (Full_Type))
1400 Full_Type := Full_View (Full_Type);
1403 -- If Typ is derived, the procedure is the initialization procedure for
1404 -- the root type. Wrap the argument in an conversion to make it type
1405 -- honest. Actually it isn't quite type honest, because there can be
1406 -- conflicts of views in the private type case. That is why we set
1407 -- Conversion_OK in the conversion node.
1409 if (Is_Record_Type (Typ)
1410 or else Is_Array_Type (Typ)
1411 or else Is_Private_Type (Typ))
1412 and then Init_Type /= Base_Type (Typ)
1414 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1415 Set_Etype (First_Arg, Init_Type);
1418 First_Arg := Id_Ref;
1421 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1423 -- In the tasks case, add _Master as the value of the _Master parameter
1424 -- and _Chain as the value of the _Chain parameter. At the outer level,
1425 -- these will be variables holding the corresponding values obtained
1426 -- from GNARL. At inner levels, they will be the parameters passed down
1427 -- through the outer routines.
1429 if Has_Task (Full_Type) then
1430 if Restriction_Active (No_Task_Hierarchy) then
1432 -- See comments in System.Tasking.Initialization.Init_RTS
1433 -- for the value 3 (should be rtsfindable constant ???)
1435 Append_To (Args, Make_Integer_Literal (Loc, 3));
1438 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1441 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1443 -- Ada 2005 (AI-287): In case of default initialized components
1444 -- with tasks, we generate a null string actual parameter.
1445 -- This is just a workaround that must be improved later???
1447 if With_Default_Init then
1449 Make_String_Literal (Loc,
1454 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1455 Decl := Last (Decls);
1458 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1459 Append_List (Decls, Res);
1467 -- Add discriminant values if discriminants are present
1469 if Has_Discriminants (Full_Init_Type) then
1470 Discr := First_Discriminant (Full_Init_Type);
1472 while Present (Discr) loop
1474 -- If this is a discriminated concurrent type, the init_proc
1475 -- for the corresponding record is being called. Use that type
1476 -- directly to find the discriminant value, to handle properly
1477 -- intervening renamed discriminants.
1480 T : Entity_Id := Full_Type;
1483 if Is_Protected_Type (T) then
1484 T := Corresponding_Record_Type (T);
1486 elsif Is_Private_Type (T)
1487 and then Present (Underlying_Full_View (T))
1488 and then Is_Protected_Type (Underlying_Full_View (T))
1490 T := Corresponding_Record_Type (Underlying_Full_View (T));
1494 Get_Discriminant_Value (
1497 Discriminant_Constraint (Full_Type));
1500 if In_Init_Proc then
1502 -- Replace any possible references to the discriminant in the
1503 -- call to the record initialization procedure with references
1504 -- to the appropriate formal parameter.
1506 if Nkind (Arg) = N_Identifier
1507 and then Ekind (Entity (Arg)) = E_Discriminant
1509 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1511 -- Case of access discriminants. We replace the reference
1512 -- to the type by a reference to the actual object
1514 elsif Nkind (Arg) = N_Attribute_Reference
1515 and then Is_Access_Type (Etype (Arg))
1516 and then Is_Entity_Name (Prefix (Arg))
1517 and then Is_Type (Entity (Prefix (Arg)))
1520 Make_Attribute_Reference (Loc,
1521 Prefix => New_Copy (Prefix (Id_Ref)),
1522 Attribute_Name => Name_Unrestricted_Access);
1524 -- Otherwise make a copy of the default expression. Note that
1525 -- we use the current Sloc for this, because we do not want the
1526 -- call to appear to be at the declaration point. Within the
1527 -- expression, replace discriminants with their discriminals.
1531 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1535 if Is_Constrained (Full_Type) then
1536 Arg := Duplicate_Subexpr_No_Checks (Arg);
1538 -- The constraints come from the discriminant default exps,
1539 -- they must be reevaluated, so we use New_Copy_Tree but we
1540 -- ensure the proper Sloc (for any embedded calls).
1542 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1546 -- Ada 2005 (AI-287): In case of default initialized components,
1547 -- we need to generate the corresponding selected component node
1548 -- to access the discriminant value. In other cases this is not
1549 -- required because we are inside the init proc and we use the
1550 -- corresponding formal.
1552 if With_Default_Init
1553 and then Nkind (Id_Ref) = N_Selected_Component
1554 and then Nkind (Arg) = N_Identifier
1557 Make_Selected_Component (Loc,
1558 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1559 Selector_Name => Arg));
1561 Append_To (Args, Arg);
1564 Next_Discriminant (Discr);
1568 -- If this is a call to initialize the parent component of a derived
1569 -- tagged type, indicate that the tag should not be set in the parent.
1571 if Is_Tagged_Type (Full_Init_Type)
1572 and then not Is_CPP_Class (Full_Init_Type)
1573 and then Nkind (Id_Ref) = N_Selected_Component
1574 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1576 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1580 Make_Procedure_Call_Statement (Loc,
1581 Name => New_Occurrence_Of (Proc, Loc),
1582 Parameter_Associations => Args));
1584 if Needs_Finalization (Typ)
1585 and then Nkind (Id_Ref) = N_Selected_Component
1587 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1588 Append_List_To (Res,
1590 Ref => New_Copy_Tree (First_Arg),
1593 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1594 With_Attach => Make_Integer_Literal (Loc, 1)));
1596 -- If the enclosing type is an extension with new controlled
1597 -- components, it has his own record controller. If the parent
1598 -- also had a record controller, attach it to the new one.
1600 -- Build_Init_Statements relies on the fact that in this specific
1601 -- case the last statement of the result is the attach call to
1602 -- the controller. If this is changed, it must be synchronized.
1604 elsif Present (Enclos_Type)
1605 and then Has_New_Controlled_Component (Enclos_Type)
1606 and then Has_Controlled_Component (Typ)
1608 if Is_Inherently_Limited_Type (Typ) then
1609 Controller_Typ := RTE (RE_Limited_Record_Controller);
1611 Controller_Typ := RTE (RE_Record_Controller);
1614 Append_List_To (Res,
1617 Make_Selected_Component (Loc,
1618 Prefix => New_Copy_Tree (First_Arg),
1619 Selector_Name => Make_Identifier (Loc, Name_uController)),
1620 Typ => Controller_Typ,
1621 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1622 With_Attach => Make_Integer_Literal (Loc, 1)));
1629 when RE_Not_Available =>
1631 end Build_Initialization_Call;
1633 ---------------------------
1634 -- Build_Master_Renaming --
1635 ---------------------------
1637 function Build_Master_Renaming
1639 T : Entity_Id) return Entity_Id
1641 Loc : constant Source_Ptr := Sloc (N);
1646 -- Nothing to do if there is no task hierarchy
1648 if Restriction_Active (No_Task_Hierarchy) then
1653 Make_Defining_Identifier (Loc,
1654 New_External_Name (Chars (T), 'M'));
1657 Make_Object_Renaming_Declaration (Loc,
1658 Defining_Identifier => M_Id,
1659 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1660 Name => Make_Identifier (Loc, Name_uMaster));
1661 Insert_Before (N, Decl);
1666 when RE_Not_Available =>
1668 end Build_Master_Renaming;
1670 ---------------------------
1671 -- Build_Master_Renaming --
1672 ---------------------------
1674 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1678 -- Nothing to do if there is no task hierarchy
1680 if Restriction_Active (No_Task_Hierarchy) then
1684 M_Id := Build_Master_Renaming (N, T);
1685 Set_Master_Id (T, M_Id);
1688 when RE_Not_Available =>
1690 end Build_Master_Renaming;
1692 ----------------------------
1693 -- Build_Record_Init_Proc --
1694 ----------------------------
1696 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1697 Loc : Source_Ptr := Sloc (N);
1698 Discr_Map : constant Elist_Id := New_Elmt_List;
1699 Proc_Id : Entity_Id;
1700 Rec_Type : Entity_Id;
1701 Set_Tag : Entity_Id := Empty;
1703 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1704 -- Build a assignment statement node which assigns to record component
1705 -- its default expression if defined. The assignment left hand side is
1706 -- marked Assignment_OK so that initialization of limited private
1707 -- records works correctly, Return also the adjustment call for
1708 -- controlled objects
1710 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1711 -- If the record has discriminants, adds assignment statements to
1712 -- statement list to initialize the discriminant values from the
1713 -- arguments of the initialization procedure.
1715 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1716 -- Build a list representing a sequence of statements which initialize
1717 -- components of the given component list. This may involve building
1718 -- case statements for the variant parts.
1720 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1721 -- Given a non-tagged type-derivation that declares discriminants,
1724 -- type R (R1, R2 : Integer) is record ... end record;
1726 -- type D (D1 : Integer) is new R (1, D1);
1728 -- we make the _init_proc of D be
1730 -- procedure _init_proc(X : D; D1 : Integer) is
1732 -- _init_proc( R(X), 1, D1);
1735 -- This function builds the call statement in this _init_proc.
1737 procedure Build_Init_Procedure;
1738 -- Build the tree corresponding to the procedure specification and body
1739 -- of the initialization procedure (by calling all the preceding
1740 -- auxiliary routines), and install it as the _init TSS.
1742 procedure Build_Offset_To_Top_Functions;
1743 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1744 -- and body of the Offset_To_Top function that is generated when the
1745 -- parent of a type with discriminants has secondary dispatch tables.
1747 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1748 -- Add range checks to components of discriminated records. S is a
1749 -- subtype indication of a record component. Check_List is a list
1750 -- to which the check actions are appended.
1752 function Component_Needs_Simple_Initialization
1753 (T : Entity_Id) return Boolean;
1754 -- Determines if a component needs simple initialization, given its type
1755 -- T. This is the same as Needs_Simple_Initialization except for the
1756 -- following difference: the types Tag and Interface_Tag, that are
1757 -- access types which would normally require simple initialization to
1758 -- null, do not require initialization as components, since they are
1759 -- explicitly initialized by other means.
1761 procedure Constrain_Array
1763 Check_List : List_Id);
1764 -- Called from Build_Record_Checks.
1765 -- Apply a list of index constraints to an unconstrained array type.
1766 -- The first parameter is the entity for the resulting subtype.
1767 -- Check_List is a list to which the check actions are appended.
1769 procedure Constrain_Index
1772 Check_List : List_Id);
1773 -- Process an index constraint in a constrained array declaration.
1774 -- The constraint can be a subtype name, or a range with or without
1775 -- an explicit subtype mark. The index is the corresponding index of the
1776 -- unconstrained array. S is the range expression. Check_List is a list
1777 -- to which the check actions are appended (called from
1778 -- Build_Record_Checks).
1780 function Parent_Subtype_Renaming_Discrims return Boolean;
1781 -- Returns True for base types N that rename discriminants, else False
1783 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1784 -- Determines whether a record initialization procedure needs to be
1785 -- generated for the given record type.
1787 ----------------------
1788 -- Build_Assignment --
1789 ----------------------
1791 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1794 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1795 Kind : Node_Kind := Nkind (N);
1801 Make_Selected_Component (Loc,
1802 Prefix => Make_Identifier (Loc, Name_uInit),
1803 Selector_Name => New_Occurrence_Of (Id, Loc));
1804 Set_Assignment_OK (Lhs);
1806 -- Case of an access attribute applied to the current instance.
1807 -- Replace the reference to the type by a reference to the actual
1808 -- object. (Note that this handles the case of the top level of
1809 -- the expression being given by such an attribute, but does not
1810 -- cover uses nested within an initial value expression. Nested
1811 -- uses are unlikely to occur in practice, but are theoretically
1812 -- possible. It is not clear how to handle them without fully
1813 -- traversing the expression. ???
1815 if Kind = N_Attribute_Reference
1816 and then (Attribute_Name (N) = Name_Unchecked_Access
1818 Attribute_Name (N) = Name_Unrestricted_Access)
1819 and then Is_Entity_Name (Prefix (N))
1820 and then Is_Type (Entity (Prefix (N)))
1821 and then Entity (Prefix (N)) = Rec_Type
1824 Make_Attribute_Reference (Loc,
1825 Prefix => Make_Identifier (Loc, Name_uInit),
1826 Attribute_Name => Name_Unrestricted_Access);
1829 -- Take a copy of Exp to ensure that later copies of this component
1830 -- declaration in derived types see the original tree, not a node
1831 -- rewritten during expansion of the init_proc.
1833 Exp := New_Copy_Tree (Exp);
1836 Make_Assignment_Statement (Loc,
1838 Expression => Exp));
1840 Set_No_Ctrl_Actions (First (Res));
1842 -- Adjust the tag if tagged (because of possible view conversions).
1843 -- Suppress the tag adjustment when VM_Target because VM tags are
1844 -- represented implicitly in objects.
1846 if Is_Tagged_Type (Typ) and then VM_Target = No_VM then
1848 Make_Assignment_Statement (Loc,
1850 Make_Selected_Component (Loc,
1851 Prefix => New_Copy_Tree (Lhs),
1853 New_Reference_To (First_Tag_Component (Typ), Loc)),
1856 Unchecked_Convert_To (RTE (RE_Tag),
1858 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1861 -- Adjust the component if controlled except if it is an aggregate
1862 -- that will be expanded inline
1864 if Kind = N_Qualified_Expression then
1865 Kind := Nkind (Expression (N));
1868 if Needs_Finalization (Typ)
1869 and then not (Kind = N_Aggregate or else Kind = N_Extension_Aggregate)
1870 and then not Is_Inherently_Limited_Type (Typ)
1872 Append_List_To (Res,
1874 Ref => New_Copy_Tree (Lhs),
1877 Find_Final_List (Etype (Id), New_Copy_Tree (Lhs)),
1878 With_Attach => Make_Integer_Literal (Loc, 1)));
1884 when RE_Not_Available =>
1886 end Build_Assignment;
1888 ------------------------------------
1889 -- Build_Discriminant_Assignments --
1890 ------------------------------------
1892 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1894 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1897 if Has_Discriminants (Rec_Type)
1898 and then not Is_Unchecked_Union (Rec_Type)
1900 D := First_Discriminant (Rec_Type);
1902 while Present (D) loop
1903 -- Don't generate the assignment for discriminants in derived
1904 -- tagged types if the discriminant is a renaming of some
1905 -- ancestor discriminant. This initialization will be done
1906 -- when initializing the _parent field of the derived record.
1908 if Is_Tagged and then
1909 Present (Corresponding_Discriminant (D))
1915 Append_List_To (Statement_List,
1916 Build_Assignment (D,
1917 New_Reference_To (Discriminal (D), Loc)));
1920 Next_Discriminant (D);
1923 end Build_Discriminant_Assignments;
1925 --------------------------
1926 -- Build_Init_Call_Thru --
1927 --------------------------
1929 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1930 Parent_Proc : constant Entity_Id :=
1931 Base_Init_Proc (Etype (Rec_Type));
1933 Parent_Type : constant Entity_Id :=
1934 Etype (First_Formal (Parent_Proc));
1936 Uparent_Type : constant Entity_Id :=
1937 Underlying_Type (Parent_Type);
1939 First_Discr_Param : Node_Id;
1941 Parent_Discr : Entity_Id;
1942 First_Arg : Node_Id;
1948 -- First argument (_Init) is the object to be initialized.
1949 -- ??? not sure where to get a reasonable Loc for First_Arg
1952 OK_Convert_To (Parent_Type,
1953 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1955 Set_Etype (First_Arg, Parent_Type);
1957 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1959 -- In the tasks case,
1960 -- add _Master as the value of the _Master parameter
1961 -- add _Chain as the value of the _Chain parameter.
1962 -- add _Task_Name as the value of the _Task_Name parameter.
1963 -- At the outer level, these will be variables holding the
1964 -- corresponding values obtained from GNARL or the expander.
1966 -- At inner levels, they will be the parameters passed down through
1967 -- the outer routines.
1969 First_Discr_Param := Next (First (Parameters));
1971 if Has_Task (Rec_Type) then
1972 if Restriction_Active (No_Task_Hierarchy) then
1974 -- See comments in System.Tasking.Initialization.Init_RTS
1977 Append_To (Args, Make_Integer_Literal (Loc, 3));
1979 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1982 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1983 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
1984 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
1987 -- Append discriminant values
1989 if Has_Discriminants (Uparent_Type) then
1990 pragma Assert (not Is_Tagged_Type (Uparent_Type));
1992 Parent_Discr := First_Discriminant (Uparent_Type);
1993 while Present (Parent_Discr) loop
1995 -- Get the initial value for this discriminant
1996 -- ??? needs to be cleaned up to use parent_Discr_Constr
2000 Discr_Value : Elmt_Id :=
2002 (Stored_Constraint (Rec_Type));
2004 Discr : Entity_Id :=
2005 First_Stored_Discriminant (Uparent_Type);
2007 while Original_Record_Component (Parent_Discr) /= Discr loop
2008 Next_Stored_Discriminant (Discr);
2009 Next_Elmt (Discr_Value);
2012 Arg := Node (Discr_Value);
2015 -- Append it to the list
2017 if Nkind (Arg) = N_Identifier
2018 and then Ekind (Entity (Arg)) = E_Discriminant
2021 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2023 -- Case of access discriminants. We replace the reference
2024 -- to the type by a reference to the actual object.
2026 -- Is above comment right??? Use of New_Copy below seems mighty
2030 Append_To (Args, New_Copy (Arg));
2033 Next_Discriminant (Parent_Discr);
2039 Make_Procedure_Call_Statement (Loc,
2040 Name => New_Occurrence_Of (Parent_Proc, Loc),
2041 Parameter_Associations => Args));
2044 end Build_Init_Call_Thru;
2046 -----------------------------------
2047 -- Build_Offset_To_Top_Functions --
2048 -----------------------------------
2050 procedure Build_Offset_To_Top_Functions is
2052 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2054 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2056 -- return O.Iface_Comp'Position;
2059 ------------------------------
2060 -- Build_Offset_To_Top_Body --
2061 ------------------------------
2063 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2064 Body_Node : Node_Id;
2065 Func_Id : Entity_Id;
2066 Spec_Node : Node_Id;
2070 Make_Defining_Identifier (Loc,
2071 Chars => New_Internal_Name ('F'));
2073 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2076 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2078 Spec_Node := New_Node (N_Function_Specification, Loc);
2079 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2080 Set_Parameter_Specifications (Spec_Node, New_List (
2081 Make_Parameter_Specification (Loc,
2082 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2084 Parameter_Type => New_Reference_To (Rec_Type, Loc))));
2085 Set_Result_Definition (Spec_Node,
2086 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2089 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2091 -- return O.Iface_Comp'Position;
2094 Body_Node := New_Node (N_Subprogram_Body, Loc);
2095 Set_Specification (Body_Node, Spec_Node);
2096 Set_Declarations (Body_Node, New_List);
2097 Set_Handled_Statement_Sequence (Body_Node,
2098 Make_Handled_Sequence_Of_Statements (Loc,
2099 Statements => New_List (
2100 Make_Simple_Return_Statement (Loc,
2102 Make_Attribute_Reference (Loc,
2104 Make_Selected_Component (Loc,
2105 Prefix => Make_Identifier (Loc, Name_uO),
2106 Selector_Name => New_Reference_To
2108 Attribute_Name => Name_Position)))));
2110 Set_Ekind (Func_Id, E_Function);
2111 Set_Mechanism (Func_Id, Default_Mechanism);
2112 Set_Is_Internal (Func_Id, True);
2114 if not Debug_Generated_Code then
2115 Set_Debug_Info_Off (Func_Id);
2118 Analyze (Body_Node);
2120 Append_Freeze_Action (Rec_Type, Body_Node);
2121 end Build_Offset_To_Top_Function;
2125 Ifaces_Comp_List : Elist_Id;
2126 Iface_Comp_Elmt : Elmt_Id;
2127 Iface_Comp : Node_Id;
2129 -- Start of processing for Build_Offset_To_Top_Functions
2132 -- Offset_To_Top_Functions are built only for derivations of types
2133 -- with discriminants that cover interface types.
2134 -- Nothing is needed either in case of virtual machines, since
2135 -- interfaces are handled directly by the VM.
2137 if not Is_Tagged_Type (Rec_Type)
2138 or else Etype (Rec_Type) = Rec_Type
2139 or else not Has_Discriminants (Etype (Rec_Type))
2140 or else VM_Target /= No_VM
2145 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2147 -- For each interface type with secondary dispatch table we generate
2148 -- the Offset_To_Top_Functions (required to displace the pointer in
2149 -- interface conversions)
2151 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2152 while Present (Iface_Comp_Elmt) loop
2153 Iface_Comp := Node (Iface_Comp_Elmt);
2154 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2156 -- If the interface is a parent of Rec_Type it shares the primary
2157 -- dispatch table and hence there is no need to build the function
2159 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type) then
2160 Build_Offset_To_Top_Function (Iface_Comp);
2163 Next_Elmt (Iface_Comp_Elmt);
2165 end Build_Offset_To_Top_Functions;
2167 --------------------------
2168 -- Build_Init_Procedure --
2169 --------------------------
2171 procedure Build_Init_Procedure is
2172 Body_Node : Node_Id;
2173 Handled_Stmt_Node : Node_Id;
2174 Parameters : List_Id;
2175 Proc_Spec_Node : Node_Id;
2176 Body_Stmts : List_Id;
2177 Record_Extension_Node : Node_Id;
2178 Init_Tags_List : List_Id;
2181 Body_Stmts := New_List;
2182 Body_Node := New_Node (N_Subprogram_Body, Loc);
2183 Set_Ekind (Proc_Id, E_Procedure);
2185 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2186 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2188 Parameters := Init_Formals (Rec_Type);
2189 Append_List_To (Parameters,
2190 Build_Discriminant_Formals (Rec_Type, True));
2192 -- For tagged types, we add a flag to indicate whether the routine
2193 -- is called to initialize a parent component in the init_proc of
2194 -- a type extension. If the flag is false, we do not set the tag
2195 -- because it has been set already in the extension.
2197 if Is_Tagged_Type (Rec_Type)
2198 and then not Is_CPP_Class (Rec_Type)
2201 Make_Defining_Identifier (Loc,
2202 Chars => New_Internal_Name ('P'));
2204 Append_To (Parameters,
2205 Make_Parameter_Specification (Loc,
2206 Defining_Identifier => Set_Tag,
2207 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2208 Expression => New_Occurrence_Of (Standard_True, Loc)));
2211 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2212 Set_Specification (Body_Node, Proc_Spec_Node);
2213 Set_Declarations (Body_Node, New_List);
2215 if Parent_Subtype_Renaming_Discrims then
2217 -- N is a Derived_Type_Definition that renames the parameters
2218 -- of the ancestor type. We initialize it by expanding our
2219 -- discriminants and call the ancestor _init_proc with a
2220 -- type-converted object
2222 Append_List_To (Body_Stmts,
2223 Build_Init_Call_Thru (Parameters));
2225 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2226 Build_Discriminant_Assignments (Body_Stmts);
2228 if not Null_Present (Type_Definition (N)) then
2229 Append_List_To (Body_Stmts,
2230 Build_Init_Statements (
2231 Component_List (Type_Definition (N))));
2235 -- N is a Derived_Type_Definition with a possible non-empty
2236 -- extension. The initialization of a type extension consists
2237 -- in the initialization of the components in the extension.
2239 Build_Discriminant_Assignments (Body_Stmts);
2241 Record_Extension_Node :=
2242 Record_Extension_Part (Type_Definition (N));
2244 if not Null_Present (Record_Extension_Node) then
2246 Stmts : constant List_Id :=
2247 Build_Init_Statements (
2248 Component_List (Record_Extension_Node));
2251 -- The parent field must be initialized first because
2252 -- the offset of the new discriminants may depend on it
2254 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2255 Append_List_To (Body_Stmts, Stmts);
2260 -- Add here the assignment to instantiate the Tag
2262 -- The assignment corresponds to the code:
2264 -- _Init._Tag := Typ'Tag;
2266 -- Suppress the tag assignment when VM_Target because VM tags are
2267 -- represented implicitly in objects. It is also suppressed in case
2268 -- of CPP_Class types because in this case the tag is initialized in
2271 if Is_Tagged_Type (Rec_Type)
2272 and then not Is_CPP_Class (Rec_Type)
2273 and then VM_Target = No_VM
2274 and then not No_Run_Time_Mode
2276 -- Initialize the primary tag
2278 Init_Tags_List := New_List (
2279 Make_Assignment_Statement (Loc,
2281 Make_Selected_Component (Loc,
2282 Prefix => Make_Identifier (Loc, Name_uInit),
2284 New_Reference_To (First_Tag_Component (Rec_Type), Loc)),
2288 (Node (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2290 -- Ada 2005 (AI-251): Initialize the secondary tags components
2291 -- located at fixed positions (tags whose position depends on
2292 -- variable size components are initialized later ---see below).
2294 if Ada_Version >= Ada_05
2295 and then not Is_Interface (Rec_Type)
2296 and then Has_Interfaces (Rec_Type)
2300 Target => Make_Identifier (Loc, Name_uInit),
2301 Stmts_List => Init_Tags_List,
2302 Fixed_Comps => True,
2303 Variable_Comps => False);
2306 -- The tag must be inserted before the assignments to other
2307 -- components, because the initial value of the component may
2308 -- depend on the tag (eg. through a dispatching operation on
2309 -- an access to the current type). The tag assignment is not done
2310 -- when initializing the parent component of a type extension,
2311 -- because in that case the tag is set in the extension.
2313 -- Extensions of imported C++ classes add a final complication,
2314 -- because we cannot inhibit tag setting in the constructor for
2315 -- the parent. In that case we insert the tag initialization
2316 -- after the calls to initialize the parent.
2318 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2319 Prepend_To (Body_Stmts,
2320 Make_If_Statement (Loc,
2321 Condition => New_Occurrence_Of (Set_Tag, Loc),
2322 Then_Statements => Init_Tags_List));
2324 -- CPP_Class derivation: In this case the dispatch table of the
2325 -- parent was built in the C++ side and we copy the table of the
2326 -- parent to initialize the new dispatch table.
2333 -- We assume the first init_proc call is for the parent
2335 Nod := First (Body_Stmts);
2336 while Present (Next (Nod))
2337 and then (Nkind (Nod) /= N_Procedure_Call_Statement
2338 or else not Is_Init_Proc (Name (Nod)))
2344 -- ancestor_constructor (_init.parent);
2346 -- inherit_prim_ops (_init._tag, new_dt, num_prims);
2347 -- _init._tag := new_dt;
2350 Prepend_To (Init_Tags_List,
2351 Build_Inherit_Prims (Loc,
2354 Make_Selected_Component (Loc,
2356 Make_Identifier (Loc,
2357 Chars => Name_uInit),
2360 (First_Tag_Component (Rec_Type), Loc)),
2363 (Node (First_Elmt (Access_Disp_Table (Rec_Type))),
2367 (DT_Entry_Count (First_Tag_Component (Rec_Type)))));
2370 Make_If_Statement (Loc,
2371 Condition => New_Occurrence_Of (Set_Tag, Loc),
2372 Then_Statements => Init_Tags_List));
2374 -- We have inherited table of the parent from the CPP side.
2375 -- Now we fill the slots associated with Ada primitives.
2376 -- This needs more work to avoid its execution each time
2377 -- an object is initialized???
2384 E := First_Elmt (Primitive_Operations (Rec_Type));
2385 while Present (E) loop
2388 if not Is_Imported (Prim)
2389 and then Convention (Prim) = Convention_CPP
2390 and then not Present (Interface_Alias (Prim))
2392 Register_Primitive (Loc,
2394 Ins_Nod => Last (Init_Tags_List));
2403 -- Ada 2005 (AI-251): Initialize the secondary tag components
2404 -- located at variable positions. We delay the generation of this
2405 -- code until here because the value of the attribute 'Position
2406 -- applied to variable size components of the parent type that
2407 -- depend on discriminants is only safely read at runtime after
2408 -- the parent components have been initialized.
2410 if Ada_Version >= Ada_05
2411 and then not Is_Interface (Rec_Type)
2412 and then Has_Interfaces (Rec_Type)
2413 and then Has_Discriminants (Etype (Rec_Type))
2414 and then Is_Variable_Size_Record (Etype (Rec_Type))
2416 Init_Tags_List := New_List;
2420 Target => Make_Identifier (Loc, Name_uInit),
2421 Stmts_List => Init_Tags_List,
2422 Fixed_Comps => False,
2423 Variable_Comps => True);
2425 if Is_Non_Empty_List (Init_Tags_List) then
2426 Append_List_To (Body_Stmts, Init_Tags_List);
2431 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2432 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2433 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2434 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2436 if not Debug_Generated_Code then
2437 Set_Debug_Info_Off (Proc_Id);
2440 -- Associate Init_Proc with type, and determine if the procedure
2441 -- is null (happens because of the Initialize_Scalars pragma case,
2442 -- where we have to generate a null procedure in case it is called
2443 -- by a client with Initialize_Scalars set). Such procedures have
2444 -- to be generated, but do not have to be called, so we mark them
2445 -- as null to suppress the call.
2447 Set_Init_Proc (Rec_Type, Proc_Id);
2449 if List_Length (Body_Stmts) = 1
2450 and then Nkind (First (Body_Stmts)) = N_Null_Statement
2451 and then VM_Target /= CLI_Target
2453 -- Even though the init proc may be null at this time it might get
2454 -- some stuff added to it later by the CIL backend, so always keep
2455 -- it when VM_Target = CLI_Target.
2457 Set_Is_Null_Init_Proc (Proc_Id);
2459 end Build_Init_Procedure;
2461 ---------------------------
2462 -- Build_Init_Statements --
2463 ---------------------------
2465 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2466 Check_List : constant List_Id := New_List;
2471 Statement_List : List_Id;
2476 Per_Object_Constraint_Components : Boolean;
2478 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2479 -- Components with access discriminants that depend on the current
2480 -- instance must be initialized after all other components.
2482 ---------------------------
2483 -- Has_Access_Constraint --
2484 ---------------------------
2486 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2488 T : constant Entity_Id := Etype (E);
2491 if Has_Per_Object_Constraint (E)
2492 and then Has_Discriminants (T)
2494 Disc := First_Discriminant (T);
2495 while Present (Disc) loop
2496 if Is_Access_Type (Etype (Disc)) then
2500 Next_Discriminant (Disc);
2507 end Has_Access_Constraint;
2509 -- Start of processing for Build_Init_Statements
2512 if Null_Present (Comp_List) then
2513 return New_List (Make_Null_Statement (Loc));
2516 Statement_List := New_List;
2518 -- Loop through components, skipping pragmas, in 2 steps. The first
2519 -- step deals with regular components. The second step deals with
2520 -- components have per object constraints, and no explicit initia-
2523 Per_Object_Constraint_Components := False;
2525 -- First step : regular components
2527 Decl := First_Non_Pragma (Component_Items (Comp_List));
2528 while Present (Decl) loop
2531 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2533 Id := Defining_Identifier (Decl);
2536 if Has_Access_Constraint (Id)
2537 and then No (Expression (Decl))
2539 -- Skip processing for now and ask for a second pass
2541 Per_Object_Constraint_Components := True;
2544 -- Case of explicit initialization
2546 if Present (Expression (Decl)) then
2547 Stmts := Build_Assignment (Id, Expression (Decl));
2549 -- Case of composite component with its own Init_Proc
2551 elsif not Is_Interface (Typ)
2552 and then Has_Non_Null_Base_Init_Proc (Typ)
2555 Build_Initialization_Call
2557 Make_Selected_Component (Loc,
2558 Prefix => Make_Identifier (Loc, Name_uInit),
2559 Selector_Name => New_Occurrence_Of (Id, Loc)),
2561 In_Init_Proc => True,
2562 Enclos_Type => Rec_Type,
2563 Discr_Map => Discr_Map);
2565 Clean_Task_Names (Typ, Proc_Id);
2567 -- Case of component needing simple initialization
2569 elsif Component_Needs_Simple_Initialization (Typ) then
2572 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2574 -- Nothing needed for this case
2580 if Present (Check_List) then
2581 Append_List_To (Statement_List, Check_List);
2584 if Present (Stmts) then
2586 -- Add the initialization of the record controller before
2587 -- the _Parent field is attached to it when the attachment
2588 -- can occur. It does not work to simply initialize the
2589 -- controller first: it must be initialized after the parent
2590 -- if the parent holds discriminants that can be used to
2591 -- compute the offset of the controller. We assume here that
2592 -- the last statement of the initialization call is the
2593 -- attachment of the parent (see Build_Initialization_Call)
2595 if Chars (Id) = Name_uController
2596 and then Rec_Type /= Etype (Rec_Type)
2597 and then Has_Controlled_Component (Etype (Rec_Type))
2598 and then Has_New_Controlled_Component (Rec_Type)
2599 and then Present (Last (Statement_List))
2601 Insert_List_Before (Last (Statement_List), Stmts);
2603 Append_List_To (Statement_List, Stmts);
2608 Next_Non_Pragma (Decl);
2611 if Per_Object_Constraint_Components then
2613 -- Second pass: components with per-object constraints
2615 Decl := First_Non_Pragma (Component_Items (Comp_List));
2616 while Present (Decl) loop
2618 Id := Defining_Identifier (Decl);
2621 if Has_Access_Constraint (Id)
2622 and then No (Expression (Decl))
2624 if Has_Non_Null_Base_Init_Proc (Typ) then
2625 Append_List_To (Statement_List,
2626 Build_Initialization_Call (Loc,
2627 Make_Selected_Component (Loc,
2628 Prefix => Make_Identifier (Loc, Name_uInit),
2629 Selector_Name => New_Occurrence_Of (Id, Loc)),
2631 In_Init_Proc => True,
2632 Enclos_Type => Rec_Type,
2633 Discr_Map => Discr_Map));
2635 Clean_Task_Names (Typ, Proc_Id);
2637 elsif Component_Needs_Simple_Initialization (Typ) then
2638 Append_List_To (Statement_List,
2640 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
2644 Next_Non_Pragma (Decl);
2648 -- Process the variant part
2650 if Present (Variant_Part (Comp_List)) then
2651 Alt_List := New_List;
2652 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2653 while Present (Variant) loop
2654 Loc := Sloc (Variant);
2655 Append_To (Alt_List,
2656 Make_Case_Statement_Alternative (Loc,
2658 New_Copy_List (Discrete_Choices (Variant)),
2660 Build_Init_Statements (Component_List (Variant))));
2661 Next_Non_Pragma (Variant);
2664 -- The expression of the case statement which is a reference
2665 -- to one of the discriminants is replaced by the appropriate
2666 -- formal parameter of the initialization procedure.
2668 Append_To (Statement_List,
2669 Make_Case_Statement (Loc,
2671 New_Reference_To (Discriminal (
2672 Entity (Name (Variant_Part (Comp_List)))), Loc),
2673 Alternatives => Alt_List));
2676 -- For a task record type, add the task create call and calls
2677 -- to bind any interrupt (signal) entries.
2679 if Is_Task_Record_Type (Rec_Type) then
2681 -- In the case of the restricted run time the ATCB has already
2682 -- been preallocated.
2684 if Restricted_Profile then
2685 Append_To (Statement_List,
2686 Make_Assignment_Statement (Loc,
2687 Name => Make_Selected_Component (Loc,
2688 Prefix => Make_Identifier (Loc, Name_uInit),
2689 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2690 Expression => Make_Attribute_Reference (Loc,
2692 Make_Selected_Component (Loc,
2693 Prefix => Make_Identifier (Loc, Name_uInit),
2695 Make_Identifier (Loc, Name_uATCB)),
2696 Attribute_Name => Name_Unchecked_Access)));
2699 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2701 -- Generate the statements which map a string entry name to a
2702 -- task entry index. Note that the task may not have entries.
2704 if Entry_Names_OK then
2705 Names := Build_Entry_Names (Rec_Type);
2707 if Present (Names) then
2708 Append_To (Statement_List, Names);
2713 Task_Type : constant Entity_Id :=
2714 Corresponding_Concurrent_Type (Rec_Type);
2715 Task_Decl : constant Node_Id := Parent (Task_Type);
2716 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2721 if Present (Task_Def) then
2722 Vis_Decl := First (Visible_Declarations (Task_Def));
2723 while Present (Vis_Decl) loop
2724 Loc := Sloc (Vis_Decl);
2726 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2727 if Get_Attribute_Id (Chars (Vis_Decl)) =
2730 Ent := Entity (Name (Vis_Decl));
2732 if Ekind (Ent) = E_Entry then
2733 Append_To (Statement_List,
2734 Make_Procedure_Call_Statement (Loc,
2735 Name => New_Reference_To (
2736 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2737 Parameter_Associations => New_List (
2738 Make_Selected_Component (Loc,
2740 Make_Identifier (Loc, Name_uInit),
2742 Make_Identifier (Loc, Name_uTask_Id)),
2743 Entry_Index_Expression (
2744 Loc, Ent, Empty, Task_Type),
2745 Expression (Vis_Decl))));
2756 -- For a protected type, add statements generated by
2757 -- Make_Initialize_Protection.
2759 if Is_Protected_Record_Type (Rec_Type) then
2760 Append_List_To (Statement_List,
2761 Make_Initialize_Protection (Rec_Type));
2763 -- Generate the statements which map a string entry name to a
2764 -- protected entry index. Note that the protected type may not
2767 if Entry_Names_OK then
2768 Names := Build_Entry_Names (Rec_Type);
2770 if Present (Names) then
2771 Append_To (Statement_List, Names);
2776 -- If no initializations when generated for component declarations
2777 -- corresponding to this Statement_List, append a null statement
2778 -- to the Statement_List to make it a valid Ada tree.
2780 if Is_Empty_List (Statement_List) then
2781 Append (New_Node (N_Null_Statement, Loc), Statement_List);
2784 return Statement_List;
2787 when RE_Not_Available =>
2789 end Build_Init_Statements;
2791 -------------------------
2792 -- Build_Record_Checks --
2793 -------------------------
2795 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
2796 Subtype_Mark_Id : Entity_Id;
2799 if Nkind (S) = N_Subtype_Indication then
2800 Find_Type (Subtype_Mark (S));
2801 Subtype_Mark_Id := Entity (Subtype_Mark (S));
2803 -- Remaining processing depends on type
2805 case Ekind (Subtype_Mark_Id) is
2808 Constrain_Array (S, Check_List);
2814 end Build_Record_Checks;
2816 -------------------------------------------
2817 -- Component_Needs_Simple_Initialization --
2818 -------------------------------------------
2820 function Component_Needs_Simple_Initialization
2821 (T : Entity_Id) return Boolean
2825 Needs_Simple_Initialization (T)
2826 and then not Is_RTE (T, RE_Tag)
2828 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
2830 and then not Is_RTE (T, RE_Interface_Tag);
2831 end Component_Needs_Simple_Initialization;
2833 ---------------------
2834 -- Constrain_Array --
2835 ---------------------
2837 procedure Constrain_Array
2839 Check_List : List_Id)
2841 C : constant Node_Id := Constraint (SI);
2842 Number_Of_Constraints : Nat := 0;
2847 T := Entity (Subtype_Mark (SI));
2849 if Ekind (T) in Access_Kind then
2850 T := Designated_Type (T);
2853 S := First (Constraints (C));
2855 while Present (S) loop
2856 Number_Of_Constraints := Number_Of_Constraints + 1;
2860 -- In either case, the index constraint must provide a discrete
2861 -- range for each index of the array type and the type of each
2862 -- discrete range must be the same as that of the corresponding
2863 -- index. (RM 3.6.1)
2865 S := First (Constraints (C));
2866 Index := First_Index (T);
2869 -- Apply constraints to each index type
2871 for J in 1 .. Number_Of_Constraints loop
2872 Constrain_Index (Index, S, Check_List);
2877 end Constrain_Array;
2879 ---------------------
2880 -- Constrain_Index --
2881 ---------------------
2883 procedure Constrain_Index
2886 Check_List : List_Id)
2888 T : constant Entity_Id := Etype (Index);
2891 if Nkind (S) = N_Range then
2892 Process_Range_Expr_In_Decl (S, T, Check_List);
2894 end Constrain_Index;
2896 --------------------------------------
2897 -- Parent_Subtype_Renaming_Discrims --
2898 --------------------------------------
2900 function Parent_Subtype_Renaming_Discrims return Boolean is
2905 if Base_Type (Pe) /= Pe then
2910 or else not Has_Discriminants (Pe)
2911 or else Is_Constrained (Pe)
2912 or else Is_Tagged_Type (Pe)
2917 -- If there are no explicit stored discriminants we have inherited
2918 -- the root type discriminants so far, so no renamings occurred.
2920 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
2924 -- Check if we have done some trivial renaming of the parent
2925 -- discriminants, i.e. something like
2927 -- type DT (X1,X2: int) is new PT (X1,X2);
2929 De := First_Discriminant (Pe);
2930 Dp := First_Discriminant (Etype (Pe));
2932 while Present (De) loop
2933 pragma Assert (Present (Dp));
2935 if Corresponding_Discriminant (De) /= Dp then
2939 Next_Discriminant (De);
2940 Next_Discriminant (Dp);
2943 return Present (Dp);
2944 end Parent_Subtype_Renaming_Discrims;
2946 ------------------------
2947 -- Requires_Init_Proc --
2948 ------------------------
2950 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
2951 Comp_Decl : Node_Id;
2956 -- Definitely do not need one if specifically suppressed
2958 if Suppress_Init_Proc (Rec_Id) then
2962 -- If it is a type derived from a type with unknown discriminants,
2963 -- we cannot build an initialization procedure for it.
2965 if Has_Unknown_Discriminants (Rec_Id) then
2969 -- Otherwise we need to generate an initialization procedure if
2970 -- Is_CPP_Class is False and at least one of the following applies:
2972 -- 1. Discriminants are present, since they need to be initialized
2973 -- with the appropriate discriminant constraint expressions.
2974 -- However, the discriminant of an unchecked union does not
2975 -- count, since the discriminant is not present.
2977 -- 2. The type is a tagged type, since the implicit Tag component
2978 -- needs to be initialized with a pointer to the dispatch table.
2980 -- 3. The type contains tasks
2982 -- 4. One or more components has an initial value
2984 -- 5. One or more components is for a type which itself requires
2985 -- an initialization procedure.
2987 -- 6. One or more components is a type that requires simple
2988 -- initialization (see Needs_Simple_Initialization), except
2989 -- that types Tag and Interface_Tag are excluded, since fields
2990 -- of these types are initialized by other means.
2992 -- 7. The type is the record type built for a task type (since at
2993 -- the very least, Create_Task must be called)
2995 -- 8. The type is the record type built for a protected type (since
2996 -- at least Initialize_Protection must be called)
2998 -- 9. The type is marked as a public entity. The reason we add this
2999 -- case (even if none of the above apply) is to properly handle
3000 -- Initialize_Scalars. If a package is compiled without an IS
3001 -- pragma, and the client is compiled with an IS pragma, then
3002 -- the client will think an initialization procedure is present
3003 -- and call it, when in fact no such procedure is required, but
3004 -- since the call is generated, there had better be a routine
3005 -- at the other end of the call, even if it does nothing!)
3007 -- Note: the reason we exclude the CPP_Class case is because in this
3008 -- case the initialization is performed in the C++ side.
3010 if Is_CPP_Class (Rec_Id) then
3013 elsif Is_Interface (Rec_Id) then
3016 elsif (Has_Discriminants (Rec_Id)
3017 and then not Is_Unchecked_Union (Rec_Id))
3018 or else Is_Tagged_Type (Rec_Id)
3019 or else Is_Concurrent_Record_Type (Rec_Id)
3020 or else Has_Task (Rec_Id)
3025 Id := First_Component (Rec_Id);
3026 while Present (Id) loop
3027 Comp_Decl := Parent (Id);
3030 if Present (Expression (Comp_Decl))
3031 or else Has_Non_Null_Base_Init_Proc (Typ)
3032 or else Component_Needs_Simple_Initialization (Typ)
3037 Next_Component (Id);
3040 -- As explained above, a record initialization procedure is needed
3041 -- for public types in case Initialize_Scalars applies to a client.
3042 -- However, such a procedure is not needed in the case where either
3043 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3044 -- apply. No_Initialize_Scalars excludes the possibility of using
3045 -- Initialize_Scalars in any partition, and No_Default_Initialization
3046 -- implies that no initialization should ever be done for objects of
3047 -- the type, so is incompatible with Initialize_Scalars.
3049 if not Restriction_Active (No_Initialize_Scalars)
3050 and then not Restriction_Active (No_Default_Initialization)
3051 and then Is_Public (Rec_Id)
3057 end Requires_Init_Proc;
3059 -- Start of processing for Build_Record_Init_Proc
3062 -- Check for value type, which means no initialization required
3064 Rec_Type := Defining_Identifier (N);
3066 if Is_Value_Type (Rec_Type) then
3070 -- This may be full declaration of a private type, in which case
3071 -- the visible entity is a record, and the private entity has been
3072 -- exchanged with it in the private part of the current package.
3073 -- The initialization procedure is built for the record type, which
3074 -- is retrievable from the private entity.
3076 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3077 Rec_Type := Underlying_Type (Rec_Type);
3080 -- If there are discriminants, build the discriminant map to replace
3081 -- discriminants by their discriminals in complex bound expressions.
3082 -- These only arise for the corresponding records of protected types.
3084 if Is_Concurrent_Record_Type (Rec_Type)
3085 and then Has_Discriminants (Rec_Type)
3090 Disc := First_Discriminant (Rec_Type);
3091 while Present (Disc) loop
3092 Append_Elmt (Disc, Discr_Map);
3093 Append_Elmt (Discriminal (Disc), Discr_Map);
3094 Next_Discriminant (Disc);
3099 -- Derived types that have no type extension can use the initialization
3100 -- procedure of their parent and do not need a procedure of their own.
3101 -- This is only correct if there are no representation clauses for the
3102 -- type or its parent, and if the parent has in fact been frozen so
3103 -- that its initialization procedure exists.
3105 if Is_Derived_Type (Rec_Type)
3106 and then not Is_Tagged_Type (Rec_Type)
3107 and then not Is_Unchecked_Union (Rec_Type)
3108 and then not Has_New_Non_Standard_Rep (Rec_Type)
3109 and then not Parent_Subtype_Renaming_Discrims
3110 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3112 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3114 -- Otherwise if we need an initialization procedure, then build one,
3115 -- mark it as public and inlinable and as having a completion.
3117 elsif Requires_Init_Proc (Rec_Type)
3118 or else Is_Unchecked_Union (Rec_Type)
3121 Make_Defining_Identifier (Loc,
3122 Chars => Make_Init_Proc_Name (Rec_Type));
3124 -- If No_Default_Initialization restriction is active, then we don't
3125 -- want to build an init_proc, but we need to mark that an init_proc
3126 -- would be needed if this restriction was not active (so that we can
3127 -- detect attempts to call it), so set a dummy init_proc in place.
3129 if Restriction_Active (No_Default_Initialization) then
3130 Set_Init_Proc (Rec_Type, Proc_Id);
3134 Build_Offset_To_Top_Functions;
3135 Build_Init_Procedure;
3136 Set_Is_Public (Proc_Id, Is_Public (Pe));
3138 -- The initialization of protected records is not worth inlining.
3139 -- In addition, when compiled for another unit for inlining purposes,
3140 -- it may make reference to entities that have not been elaborated
3141 -- yet. The initialization of controlled records contains a nested
3142 -- clean-up procedure that makes it impractical to inline as well,
3143 -- and leads to undefined symbols if inlined in a different unit.
3144 -- Similar considerations apply to task types.
3146 if not Is_Concurrent_Type (Rec_Type)
3147 and then not Has_Task (Rec_Type)
3148 and then not Needs_Finalization (Rec_Type)
3150 Set_Is_Inlined (Proc_Id);
3153 Set_Is_Internal (Proc_Id);
3154 Set_Has_Completion (Proc_Id);
3156 if not Debug_Generated_Code then
3157 Set_Debug_Info_Off (Proc_Id);
3161 Agg : constant Node_Id :=
3162 Build_Equivalent_Record_Aggregate (Rec_Type);
3164 procedure Collect_Itypes (Comp : Node_Id);
3165 -- Generate references to itypes in the aggregate, because
3166 -- the first use of the aggregate may be in a nested scope.
3168 --------------------
3169 -- Collect_Itypes --
3170 --------------------
3172 procedure Collect_Itypes (Comp : Node_Id) is
3175 Typ : constant Entity_Id := Etype (Comp);
3178 if Is_Array_Type (Typ)
3179 and then Is_Itype (Typ)
3181 Ref := Make_Itype_Reference (Loc);
3182 Set_Itype (Ref, Typ);
3183 Append_Freeze_Action (Rec_Type, Ref);
3185 Ref := Make_Itype_Reference (Loc);
3186 Set_Itype (Ref, Etype (First_Index (Typ)));
3187 Append_Freeze_Action (Rec_Type, Ref);
3189 Sub_Aggr := First (Expressions (Comp));
3191 -- Recurse on nested arrays
3193 while Present (Sub_Aggr) loop
3194 Collect_Itypes (Sub_Aggr);
3201 -- If there is a static initialization aggregate for the type,
3202 -- generate itype references for the types of its (sub)components,
3203 -- to prevent out-of-scope errors in the resulting tree.
3204 -- The aggregate may have been rewritten as a Raise node, in which
3205 -- case there are no relevant itypes.
3208 and then Nkind (Agg) = N_Aggregate
3210 Set_Static_Initialization (Proc_Id, Agg);
3215 Comp := First (Component_Associations (Agg));
3216 while Present (Comp) loop
3217 Collect_Itypes (Expression (Comp));
3224 end Build_Record_Init_Proc;
3226 ----------------------------
3227 -- Build_Slice_Assignment --
3228 ----------------------------
3230 -- Generates the following subprogram:
3233 -- (Source, Target : Array_Type,
3234 -- Left_Lo, Left_Hi : Index;
3235 -- Right_Lo, Right_Hi : Index;
3243 -- if Left_Hi < Left_Lo then
3256 -- Target (Li1) := Source (Ri1);
3259 -- exit when Li1 = Left_Lo;
3260 -- Li1 := Index'pred (Li1);
3261 -- Ri1 := Index'pred (Ri1);
3263 -- exit when Li1 = Left_Hi;
3264 -- Li1 := Index'succ (Li1);
3265 -- Ri1 := Index'succ (Ri1);
3270 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3271 Loc : constant Source_Ptr := Sloc (Typ);
3272 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3274 -- Build formal parameters of procedure
3276 Larray : constant Entity_Id :=
3277 Make_Defining_Identifier
3278 (Loc, Chars => New_Internal_Name ('A'));
3279 Rarray : constant Entity_Id :=
3280 Make_Defining_Identifier
3281 (Loc, Chars => New_Internal_Name ('R'));
3282 Left_Lo : constant Entity_Id :=
3283 Make_Defining_Identifier
3284 (Loc, Chars => New_Internal_Name ('L'));
3285 Left_Hi : constant Entity_Id :=
3286 Make_Defining_Identifier
3287 (Loc, Chars => New_Internal_Name ('L'));
3288 Right_Lo : constant Entity_Id :=
3289 Make_Defining_Identifier
3290 (Loc, Chars => New_Internal_Name ('R'));
3291 Right_Hi : constant Entity_Id :=
3292 Make_Defining_Identifier
3293 (Loc, Chars => New_Internal_Name ('R'));
3294 Rev : constant Entity_Id :=
3295 Make_Defining_Identifier
3296 (Loc, Chars => New_Internal_Name ('D'));
3297 Proc_Name : constant Entity_Id :=
3298 Make_Defining_Identifier (Loc,
3299 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3301 Lnn : constant Entity_Id :=
3302 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3303 Rnn : constant Entity_Id :=
3304 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3305 -- Subscripts for left and right sides
3312 -- Build declarations for indices
3317 Make_Object_Declaration (Loc,
3318 Defining_Identifier => Lnn,
3319 Object_Definition =>
3320 New_Occurrence_Of (Index, Loc)));
3323 Make_Object_Declaration (Loc,
3324 Defining_Identifier => Rnn,
3325 Object_Definition =>
3326 New_Occurrence_Of (Index, Loc)));
3330 -- Build test for empty slice case
3333 Make_If_Statement (Loc,
3336 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3337 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3338 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3340 -- Build initializations for indices
3343 F_Init : constant List_Id := New_List;
3344 B_Init : constant List_Id := New_List;
3348 Make_Assignment_Statement (Loc,
3349 Name => New_Occurrence_Of (Lnn, Loc),
3350 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3353 Make_Assignment_Statement (Loc,
3354 Name => New_Occurrence_Of (Rnn, Loc),
3355 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3358 Make_Assignment_Statement (Loc,
3359 Name => New_Occurrence_Of (Lnn, Loc),
3360 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3363 Make_Assignment_Statement (Loc,
3364 Name => New_Occurrence_Of (Rnn, Loc),
3365 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3368 Make_If_Statement (Loc,
3369 Condition => New_Occurrence_Of (Rev, Loc),
3370 Then_Statements => B_Init,
3371 Else_Statements => F_Init));
3374 -- Now construct the assignment statement
3377 Make_Loop_Statement (Loc,
3378 Statements => New_List (
3379 Make_Assignment_Statement (Loc,
3381 Make_Indexed_Component (Loc,
3382 Prefix => New_Occurrence_Of (Larray, Loc),
3383 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3385 Make_Indexed_Component (Loc,
3386 Prefix => New_Occurrence_Of (Rarray, Loc),
3387 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3388 End_Label => Empty);
3390 -- Build the exit condition and increment/decrement statements
3393 F_Ass : constant List_Id := New_List;
3394 B_Ass : constant List_Id := New_List;
3398 Make_Exit_Statement (Loc,
3401 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3402 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3405 Make_Assignment_Statement (Loc,
3406 Name => New_Occurrence_Of (Lnn, Loc),
3408 Make_Attribute_Reference (Loc,
3410 New_Occurrence_Of (Index, Loc),
3411 Attribute_Name => Name_Succ,
3412 Expressions => New_List (
3413 New_Occurrence_Of (Lnn, Loc)))));
3416 Make_Assignment_Statement (Loc,
3417 Name => New_Occurrence_Of (Rnn, Loc),
3419 Make_Attribute_Reference (Loc,
3421 New_Occurrence_Of (Index, Loc),
3422 Attribute_Name => Name_Succ,
3423 Expressions => New_List (
3424 New_Occurrence_Of (Rnn, Loc)))));
3427 Make_Exit_Statement (Loc,
3430 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3431 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3434 Make_Assignment_Statement (Loc,
3435 Name => New_Occurrence_Of (Lnn, Loc),
3437 Make_Attribute_Reference (Loc,
3439 New_Occurrence_Of (Index, Loc),
3440 Attribute_Name => Name_Pred,
3441 Expressions => New_List (
3442 New_Occurrence_Of (Lnn, Loc)))));
3445 Make_Assignment_Statement (Loc,
3446 Name => New_Occurrence_Of (Rnn, Loc),
3448 Make_Attribute_Reference (Loc,
3450 New_Occurrence_Of (Index, Loc),
3451 Attribute_Name => Name_Pred,
3452 Expressions => New_List (
3453 New_Occurrence_Of (Rnn, Loc)))));
3455 Append_To (Statements (Loops),
3456 Make_If_Statement (Loc,
3457 Condition => New_Occurrence_Of (Rev, Loc),
3458 Then_Statements => B_Ass,
3459 Else_Statements => F_Ass));
3462 Append_To (Stats, Loops);
3466 Formals : List_Id := New_List;
3469 Formals := New_List (
3470 Make_Parameter_Specification (Loc,
3471 Defining_Identifier => Larray,
3472 Out_Present => True,
3474 New_Reference_To (Base_Type (Typ), Loc)),
3476 Make_Parameter_Specification (Loc,
3477 Defining_Identifier => Rarray,
3479 New_Reference_To (Base_Type (Typ), Loc)),
3481 Make_Parameter_Specification (Loc,
3482 Defining_Identifier => Left_Lo,
3484 New_Reference_To (Index, Loc)),
3486 Make_Parameter_Specification (Loc,
3487 Defining_Identifier => Left_Hi,
3489 New_Reference_To (Index, Loc)),
3491 Make_Parameter_Specification (Loc,
3492 Defining_Identifier => Right_Lo,
3494 New_Reference_To (Index, Loc)),
3496 Make_Parameter_Specification (Loc,
3497 Defining_Identifier => Right_Hi,
3499 New_Reference_To (Index, Loc)));
3502 Make_Parameter_Specification (Loc,
3503 Defining_Identifier => Rev,
3505 New_Reference_To (Standard_Boolean, Loc)));
3508 Make_Procedure_Specification (Loc,
3509 Defining_Unit_Name => Proc_Name,
3510 Parameter_Specifications => Formals);
3513 Make_Subprogram_Body (Loc,
3514 Specification => Spec,
3515 Declarations => Decls,
3516 Handled_Statement_Sequence =>
3517 Make_Handled_Sequence_Of_Statements (Loc,
3518 Statements => Stats)));
3521 Set_TSS (Typ, Proc_Name);
3522 Set_Is_Pure (Proc_Name);
3523 end Build_Slice_Assignment;
3525 ------------------------------------
3526 -- Build_Variant_Record_Equality --
3527 ------------------------------------
3531 -- function _Equality (X, Y : T) return Boolean is
3533 -- -- Compare discriminants
3535 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3539 -- -- Compare components
3541 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3545 -- -- Compare variant part
3549 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3554 -- if False or else X.Cn /= Y.Cn then
3562 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3563 Loc : constant Source_Ptr := Sloc (Typ);
3565 F : constant Entity_Id :=
3566 Make_Defining_Identifier (Loc,
3567 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3569 X : constant Entity_Id :=
3570 Make_Defining_Identifier (Loc,
3573 Y : constant Entity_Id :=
3574 Make_Defining_Identifier (Loc,
3577 Def : constant Node_Id := Parent (Typ);
3578 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3579 Stmts : constant List_Id := New_List;
3580 Pspecs : constant List_Id := New_List;
3583 -- Derived Unchecked_Union types no longer inherit the equality function
3586 if Is_Derived_Type (Typ)
3587 and then not Is_Unchecked_Union (Typ)
3588 and then not Has_New_Non_Standard_Rep (Typ)
3591 Parent_Eq : constant Entity_Id :=
3592 TSS (Root_Type (Typ), TSS_Composite_Equality);
3595 if Present (Parent_Eq) then
3596 Copy_TSS (Parent_Eq, Typ);
3603 Make_Subprogram_Body (Loc,
3605 Make_Function_Specification (Loc,
3606 Defining_Unit_Name => F,
3607 Parameter_Specifications => Pspecs,
3608 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3609 Declarations => New_List,
3610 Handled_Statement_Sequence =>
3611 Make_Handled_Sequence_Of_Statements (Loc,
3612 Statements => Stmts)));
3615 Make_Parameter_Specification (Loc,
3616 Defining_Identifier => X,
3617 Parameter_Type => New_Reference_To (Typ, Loc)));
3620 Make_Parameter_Specification (Loc,
3621 Defining_Identifier => Y,
3622 Parameter_Type => New_Reference_To (Typ, Loc)));
3624 -- Unchecked_Unions require additional machinery to support equality.
3625 -- Two extra parameters (A and B) are added to the equality function
3626 -- parameter list in order to capture the inferred values of the
3627 -- discriminants in later calls.
3629 if Is_Unchecked_Union (Typ) then
3631 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
3633 A : constant Node_Id :=
3634 Make_Defining_Identifier (Loc,
3637 B : constant Node_Id :=
3638 Make_Defining_Identifier (Loc,
3642 -- Add A and B to the parameter list
3645 Make_Parameter_Specification (Loc,
3646 Defining_Identifier => A,
3647 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3650 Make_Parameter_Specification (Loc,
3651 Defining_Identifier => B,
3652 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
3654 -- Generate the following header code to compare the inferred
3662 Make_If_Statement (Loc,
3665 Left_Opnd => New_Reference_To (A, Loc),
3666 Right_Opnd => New_Reference_To (B, Loc)),
3667 Then_Statements => New_List (
3668 Make_Simple_Return_Statement (Loc,
3669 Expression => New_Occurrence_Of (Standard_False, Loc)))));
3671 -- Generate component-by-component comparison. Note that we must
3672 -- propagate one of the inferred discriminant formals to act as
3673 -- the case statement switch.
3675 Append_List_To (Stmts,
3676 Make_Eq_Case (Typ, Comps, A));
3680 -- Normal case (not unchecked union)
3685 Discriminant_Specifications (Def)));
3687 Append_List_To (Stmts,
3688 Make_Eq_Case (Typ, Comps));
3692 Make_Simple_Return_Statement (Loc,
3693 Expression => New_Reference_To (Standard_True, Loc)));
3698 if not Debug_Generated_Code then
3699 Set_Debug_Info_Off (F);
3701 end Build_Variant_Record_Equality;
3703 -----------------------------
3704 -- Check_Stream_Attributes --
3705 -----------------------------
3707 procedure Check_Stream_Attributes (Typ : Entity_Id) is
3709 Par_Read : constant Boolean :=
3710 Stream_Attribute_Available (Typ, TSS_Stream_Read)
3711 and then not Has_Specified_Stream_Read (Typ);
3712 Par_Write : constant Boolean :=
3713 Stream_Attribute_Available (Typ, TSS_Stream_Write)
3714 and then not Has_Specified_Stream_Write (Typ);
3716 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
3717 -- Check that Comp has a user-specified Nam stream attribute
3723 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
3725 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
3726 Error_Msg_Name_1 := Nam;
3728 ("|component& in limited extension must have% attribute", Comp);
3732 -- Start of processing for Check_Stream_Attributes
3735 if Par_Read or else Par_Write then
3736 Comp := First_Component (Typ);
3737 while Present (Comp) loop
3738 if Comes_From_Source (Comp)
3739 and then Original_Record_Component (Comp) = Comp
3740 and then Is_Limited_Type (Etype (Comp))
3743 Check_Attr (Name_Read, TSS_Stream_Read);
3747 Check_Attr (Name_Write, TSS_Stream_Write);
3751 Next_Component (Comp);
3754 end Check_Stream_Attributes;
3756 -----------------------------
3757 -- Expand_Record_Extension --
3758 -----------------------------
3760 -- Add a field _parent at the beginning of the record extension. This is
3761 -- used to implement inheritance. Here are some examples of expansion:
3763 -- 1. no discriminants
3764 -- type T2 is new T1 with null record;
3766 -- type T2 is new T1 with record
3770 -- 2. renamed discriminants
3771 -- type T2 (B, C : Int) is new T1 (A => B) with record
3772 -- _Parent : T1 (A => B);
3776 -- 3. inherited discriminants
3777 -- type T2 is new T1 with record -- discriminant A inherited
3778 -- _Parent : T1 (A);
3782 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
3783 Indic : constant Node_Id := Subtype_Indication (Def);
3784 Loc : constant Source_Ptr := Sloc (Def);
3785 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
3786 Par_Subtype : Entity_Id;
3787 Comp_List : Node_Id;
3788 Comp_Decl : Node_Id;
3791 List_Constr : constant List_Id := New_List;
3794 -- Expand_Record_Extension is called directly from the semantics, so
3795 -- we must check to see whether expansion is active before proceeding
3797 if not Expander_Active then
3801 -- This may be a derivation of an untagged private type whose full
3802 -- view is tagged, in which case the Derived_Type_Definition has no
3803 -- extension part. Build an empty one now.
3805 if No (Rec_Ext_Part) then
3807 Make_Record_Definition (Loc,
3809 Component_List => Empty,
3810 Null_Present => True);
3812 Set_Record_Extension_Part (Def, Rec_Ext_Part);
3813 Mark_Rewrite_Insertion (Rec_Ext_Part);
3816 Comp_List := Component_List (Rec_Ext_Part);
3818 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
3820 -- If the derived type inherits its discriminants the type of the
3821 -- _parent field must be constrained by the inherited discriminants
3823 if Has_Discriminants (T)
3824 and then Nkind (Indic) /= N_Subtype_Indication
3825 and then not Is_Constrained (Entity (Indic))
3827 D := First_Discriminant (T);
3828 while Present (D) loop
3829 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
3830 Next_Discriminant (D);
3835 Make_Subtype_Indication (Loc,
3836 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
3838 Make_Index_Or_Discriminant_Constraint (Loc,
3839 Constraints => List_Constr)),
3842 -- Otherwise the original subtype_indication is just what is needed
3845 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
3848 Set_Parent_Subtype (T, Par_Subtype);
3851 Make_Component_Declaration (Loc,
3852 Defining_Identifier => Parent_N,
3853 Component_Definition =>
3854 Make_Component_Definition (Loc,
3855 Aliased_Present => False,
3856 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
3858 if Null_Present (Rec_Ext_Part) then
3859 Set_Component_List (Rec_Ext_Part,
3860 Make_Component_List (Loc,
3861 Component_Items => New_List (Comp_Decl),
3862 Variant_Part => Empty,
3863 Null_Present => False));
3864 Set_Null_Present (Rec_Ext_Part, False);
3866 elsif Null_Present (Comp_List)
3867 or else Is_Empty_List (Component_Items (Comp_List))
3869 Set_Component_Items (Comp_List, New_List (Comp_Decl));
3870 Set_Null_Present (Comp_List, False);
3873 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
3876 Analyze (Comp_Decl);
3877 end Expand_Record_Extension;
3879 ------------------------------------
3880 -- Expand_N_Full_Type_Declaration --
3881 ------------------------------------
3883 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
3884 Def_Id : constant Entity_Id := Defining_Identifier (N);
3885 B_Id : constant Entity_Id := Base_Type (Def_Id);
3889 procedure Build_Master (Def_Id : Entity_Id);
3890 -- Create the master associated with Def_Id
3896 procedure Build_Master (Def_Id : Entity_Id) is
3898 -- Anonymous access types are created for the components of the
3899 -- record parameter for an entry declaration. No master is created
3902 if Has_Task (Designated_Type (Def_Id))
3903 and then Comes_From_Source (N)
3905 Build_Master_Entity (Def_Id);
3906 Build_Master_Renaming (Parent (Def_Id), Def_Id);
3908 -- Create a class-wide master because a Master_Id must be generated
3909 -- for access-to-limited-class-wide types whose root may be extended
3910 -- with task components, and for access-to-limited-interfaces because
3911 -- they can be used to reference tasks implementing such interface.
3913 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
3914 and then (Is_Limited_Type (Designated_Type (Def_Id))
3916 (Is_Interface (Designated_Type (Def_Id))
3918 Is_Limited_Interface (Designated_Type (Def_Id))))
3919 and then Tasking_Allowed
3921 -- Do not create a class-wide master for types whose convention is
3922 -- Java since these types cannot embed Ada tasks anyway. Note that
3923 -- the following test cannot catch the following case:
3925 -- package java.lang.Object is
3926 -- type Typ is tagged limited private;
3927 -- type Ref is access all Typ'Class;
3929 -- type Typ is tagged limited ...;
3930 -- pragma Convention (Typ, Java)
3933 -- Because the convention appears after we have done the
3934 -- processing for type Ref.
3936 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
3937 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
3939 Build_Class_Wide_Master (Def_Id);
3943 -- Start of processing for Expand_N_Full_Type_Declaration
3946 if Is_Access_Type (Def_Id) then
3947 Build_Master (Def_Id);
3949 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
3950 Expand_Access_Protected_Subprogram_Type (N);
3953 elsif Ada_Version >= Ada_05
3954 and then Is_Array_Type (Def_Id)
3955 and then Is_Access_Type (Component_Type (Def_Id))
3956 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
3958 Build_Master (Component_Type (Def_Id));
3960 elsif Has_Task (Def_Id) then
3961 Expand_Previous_Access_Type (Def_Id);
3963 elsif Ada_Version >= Ada_05
3965 (Is_Record_Type (Def_Id)
3966 or else (Is_Array_Type (Def_Id)
3967 and then Is_Record_Type (Component_Type (Def_Id))))
3975 -- Look for the first anonymous access type component
3977 if Is_Array_Type (Def_Id) then
3978 Comp := First_Entity (Component_Type (Def_Id));
3980 Comp := First_Entity (Def_Id);
3983 while Present (Comp) loop
3984 Typ := Etype (Comp);
3986 exit when Is_Access_Type (Typ)
3987 and then Ekind (Typ) = E_Anonymous_Access_Type;
3992 -- If found we add a renaming declaration of master_id and we
3993 -- associate it to each anonymous access type component. Do
3994 -- nothing if the access type already has a master. This will be
3995 -- the case if the array type is the packed array created for a
3996 -- user-defined array type T, where the master_id is created when
3997 -- expanding the declaration for T.
4000 and then Ekind (Typ) = E_Anonymous_Access_Type
4001 and then not Restriction_Active (No_Task_Hierarchy)
4002 and then No (Master_Id (Typ))
4004 -- Do not consider run-times with no tasking support
4006 and then RTE_Available (RE_Current_Master)
4007 and then Has_Task (Non_Limited_Designated_Type (Typ))
4009 Build_Master_Entity (Def_Id);
4010 M_Id := Build_Master_Renaming (N, Def_Id);
4012 if Is_Array_Type (Def_Id) then
4013 Comp := First_Entity (Component_Type (Def_Id));
4015 Comp := First_Entity (Def_Id);
4018 while Present (Comp) loop
4019 Typ := Etype (Comp);
4021 if Is_Access_Type (Typ)
4022 and then Ekind (Typ) = E_Anonymous_Access_Type
4024 Set_Master_Id (Typ, M_Id);
4033 Par_Id := Etype (B_Id);
4035 -- The parent type is private then we need to inherit any TSS operations
4036 -- from the full view.
4038 if Ekind (Par_Id) in Private_Kind
4039 and then Present (Full_View (Par_Id))
4041 Par_Id := Base_Type (Full_View (Par_Id));
4044 if Nkind (Type_Definition (Original_Node (N))) =
4045 N_Derived_Type_Definition
4046 and then not Is_Tagged_Type (Def_Id)
4047 and then Present (Freeze_Node (Par_Id))
4048 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4050 Ensure_Freeze_Node (B_Id);
4051 FN := Freeze_Node (B_Id);
4053 if No (TSS_Elist (FN)) then
4054 Set_TSS_Elist (FN, New_Elmt_List);
4058 T_E : constant Elist_Id := TSS_Elist (FN);
4062 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4063 while Present (Elmt) loop
4064 if Chars (Node (Elmt)) /= Name_uInit then
4065 Append_Elmt (Node (Elmt), T_E);
4071 -- If the derived type itself is private with a full view, then
4072 -- associate the full view with the inherited TSS_Elist as well.
4074 if Ekind (B_Id) in Private_Kind
4075 and then Present (Full_View (B_Id))
4077 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4079 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4083 end Expand_N_Full_Type_Declaration;
4085 ---------------------------------
4086 -- Expand_N_Object_Declaration --
4087 ---------------------------------
4089 -- First we do special processing for objects of a tagged type where this
4090 -- is the point at which the type is frozen. The creation of the dispatch
4091 -- table and the initialization procedure have to be deferred to this
4092 -- point, since we reference previously declared primitive subprograms.
4094 -- For all types, we call an initialization procedure if there is one
4096 procedure Expand_N_Object_Declaration (N : Node_Id) is
4097 Def_Id : constant Entity_Id := Defining_Identifier (N);
4098 Expr : constant Node_Id := Expression (N);
4099 Loc : constant Source_Ptr := Sloc (N);
4100 Typ : constant Entity_Id := Etype (Def_Id);
4101 Base_Typ : constant Entity_Id := Base_Type (Typ);
4105 BIP_Call : Boolean := False;
4107 Init_After : Node_Id := N;
4108 -- Node after which the init proc call is to be inserted. This is
4109 -- normally N, except for the case of a shared passive variable, in
4110 -- which case the init proc call must be inserted only after the bodies
4111 -- of the shared variable procedures have been seen.
4114 -- Don't do anything for deferred constants. All proper actions will
4115 -- be expanded during the full declaration.
4117 if No (Expr) and Constant_Present (N) then
4121 -- Force construction of dispatch tables of library level tagged types
4123 if VM_Target = No_VM
4124 and then Static_Dispatch_Tables
4125 and then Is_Library_Level_Entity (Def_Id)
4126 and then Is_Library_Level_Tagged_Type (Base_Typ)
4127 and then (Ekind (Base_Typ) = E_Record_Type
4128 or else Ekind (Base_Typ) = E_Protected_Type
4129 or else Ekind (Base_Typ) = E_Task_Type)
4130 and then not Has_Dispatch_Table (Base_Typ)
4133 New_Nodes : List_Id := No_List;
4136 if Is_Concurrent_Type (Base_Typ) then
4137 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4139 New_Nodes := Make_DT (Base_Typ, N);
4142 if not Is_Empty_List (New_Nodes) then
4143 Insert_List_Before (N, New_Nodes);
4148 -- Make shared memory routines for shared passive variable
4150 if Is_Shared_Passive (Def_Id) then
4151 Init_After := Make_Shared_Var_Procs (N);
4154 -- If tasks being declared, make sure we have an activation chain
4155 -- defined for the tasks (has no effect if we already have one), and
4156 -- also that a Master variable is established and that the appropriate
4157 -- enclosing construct is established as a task master.
4159 if Has_Task (Typ) then
4160 Build_Activation_Chain_Entity (N);
4161 Build_Master_Entity (Def_Id);
4164 -- Build a list controller for declarations where the type is anonymous
4165 -- access and the designated type is controlled. Only declarations from
4166 -- source files receive such controllers in order to provide the same
4167 -- lifespan for any potential coextensions that may be associated with
4168 -- the object. Finalization lists of internal controlled anonymous
4169 -- access objects are already handled in Expand_N_Allocator.
4171 if Comes_From_Source (N)
4172 and then Ekind (Typ) = E_Anonymous_Access_Type
4173 and then Is_Controlled (Directly_Designated_Type (Typ))
4174 and then No (Associated_Final_Chain (Typ))
4176 Build_Final_List (N, Typ);
4179 -- Default initialization required, and no expression present
4183 -- Expand Initialize call for controlled objects. One may wonder why
4184 -- the Initialize Call is not done in the regular Init procedure
4185 -- attached to the record type. That's because the init procedure is
4186 -- recursively called on each component, including _Parent, thus the
4187 -- Init call for a controlled object would generate not only one
4188 -- Initialize call as it is required but one for each ancestor of
4189 -- its type. This processing is suppressed if No_Initialization set.
4191 if not Needs_Finalization (Typ)
4192 or else No_Initialization (N)
4196 elsif not Abort_Allowed
4197 or else not Comes_From_Source (N)
4199 Insert_Actions_After (Init_After,
4201 Ref => New_Occurrence_Of (Def_Id, Loc),
4202 Typ => Base_Type (Typ),
4203 Flist_Ref => Find_Final_List (Def_Id),
4204 With_Attach => Make_Integer_Literal (Loc, 1)));
4209 -- We need to protect the initialize call
4213 -- Initialize (...);
4215 -- Undefer_Abort.all;
4218 -- ??? this won't protect the initialize call for controlled
4219 -- components which are part of the init proc, so this block
4220 -- should probably also contain the call to _init_proc but this
4221 -- requires some code reorganization...
4224 L : constant List_Id :=
4226 (Ref => New_Occurrence_Of (Def_Id, Loc),
4227 Typ => Base_Type (Typ),
4228 Flist_Ref => Find_Final_List (Def_Id),
4229 With_Attach => Make_Integer_Literal (Loc, 1));
4231 Blk : constant Node_Id :=
4232 Make_Block_Statement (Loc,
4233 Handled_Statement_Sequence =>
4234 Make_Handled_Sequence_Of_Statements (Loc, L));
4237 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4238 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4239 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4240 Insert_Actions_After (Init_After, New_List (Blk));
4241 Expand_At_End_Handler
4242 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4246 -- Call type initialization procedure if there is one. We build the
4247 -- call and put it immediately after the object declaration, so that
4248 -- it will be expanded in the usual manner. Note that this will
4249 -- result in proper handling of defaulted discriminants.
4251 -- Need call if there is a base init proc
4253 if Has_Non_Null_Base_Init_Proc (Typ)
4255 -- Suppress call if No_Initialization set on declaration
4257 and then not No_Initialization (N)
4259 -- Suppress call for special case of value type for VM
4261 and then not Is_Value_Type (Typ)
4263 -- Suppress call if Suppress_Init_Proc set on the type. This is
4264 -- needed for the derived type case, where Suppress_Initialization
4265 -- may be set for the derived type, even if there is an init proc
4266 -- defined for the root type.
4268 and then not Suppress_Init_Proc (Typ)
4270 -- Return without initializing when No_Default_Initialization
4271 -- applies. Note that the actual restriction check occurs later,
4272 -- when the object is frozen, because we don't know yet whether
4273 -- the object is imported, which is a case where the check does
4276 if Restriction_Active (No_Default_Initialization) then
4280 -- The call to the initialization procedure does NOT freeze the
4281 -- object being initialized. This is because the call is not a
4282 -- source level call. This works fine, because the only possible
4283 -- statements depending on freeze status that can appear after the
4284 -- _Init call are rep clauses which can safely appear after actual
4285 -- references to the object.
4287 Id_Ref := New_Reference_To (Def_Id, Loc);
4288 Set_Must_Not_Freeze (Id_Ref);
4289 Set_Assignment_OK (Id_Ref);
4292 Init_Expr : constant Node_Id :=
4293 Static_Initialization (Base_Init_Proc (Typ));
4295 if Present (Init_Expr) then
4297 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4300 Initialization_Warning (Id_Ref);
4302 Insert_Actions_After (Init_After,
4303 Build_Initialization_Call (Loc, Id_Ref, Typ));
4307 -- If simple initialization is required, then set an appropriate
4308 -- simple initialization expression in place. This special
4309 -- initialization is required even though No_Init_Flag is present,
4310 -- but is not needed if there was an explicit initialization.
4312 -- An internally generated temporary needs no initialization because
4313 -- it will be assigned subsequently. In particular, there is no point
4314 -- in applying Initialize_Scalars to such a temporary.
4316 elsif Needs_Simple_Initialization (Typ)
4317 and then not Is_Internal (Def_Id)
4318 and then not Has_Init_Expression (N)
4320 Set_No_Initialization (N, False);
4321 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4322 Analyze_And_Resolve (Expression (N), Typ);
4325 -- Generate attribute for Persistent_BSS if needed
4327 if Persistent_BSS_Mode
4328 and then Comes_From_Source (N)
4329 and then Is_Potentially_Persistent_Type (Typ)
4330 and then not Has_Init_Expression (N)
4331 and then Is_Library_Level_Entity (Def_Id)
4337 Make_Linker_Section_Pragma
4338 (Def_Id, Sloc (N), ".persistent.bss");
4339 Insert_After (N, Prag);
4344 -- If access type, then we know it is null if not initialized
4346 if Is_Access_Type (Typ) then
4347 Set_Is_Known_Null (Def_Id);
4350 -- Explicit initialization present
4353 -- Obtain actual expression from qualified expression
4355 if Nkind (Expr) = N_Qualified_Expression then
4356 Expr_Q := Expression (Expr);
4361 -- When we have the appropriate type of aggregate in the expression
4362 -- (it has been determined during analysis of the aggregate by
4363 -- setting the delay flag), let's perform in place assignment and
4364 -- thus avoid creating a temporary.
4366 if Is_Delayed_Aggregate (Expr_Q) then
4367 Convert_Aggr_In_Object_Decl (N);
4370 -- Ada 2005 (AI-318-02): If the initialization expression is a
4371 -- call to a build-in-place function, then access to the declared
4372 -- object must be passed to the function. Currently we limit such
4373 -- functions to those with constrained limited result subtypes,
4374 -- but eventually we plan to expand the allowed forms of functions
4375 -- that are treated as build-in-place.
4377 if Ada_Version >= Ada_05
4378 and then Is_Build_In_Place_Function_Call (Expr_Q)
4380 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4384 -- In most cases, we must check that the initial value meets any
4385 -- constraint imposed by the declared type. However, there is one
4386 -- very important exception to this rule. If the entity has an
4387 -- unconstrained nominal subtype, then it acquired its constraints
4388 -- from the expression in the first place, and not only does this
4389 -- mean that the constraint check is not needed, but an attempt to
4390 -- perform the constraint check can cause order order of
4391 -- elaboration problems.
4393 if not Is_Constr_Subt_For_U_Nominal (Typ) then
4395 -- If this is an allocator for an aggregate that has been
4396 -- allocated in place, delay checks until assignments are
4397 -- made, because the discriminants are not initialized.
4399 if Nkind (Expr) = N_Allocator
4400 and then No_Initialization (Expr)
4404 Apply_Constraint_Check (Expr, Typ);
4408 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4409 -- class-wide object to ensure that we copy the full object,
4410 -- unless we are targetting a VM where interfaces are handled by
4411 -- VM itself. Note that if the root type of Typ is an ancestor
4412 -- of Expr's type, both types share the same dispatch table and
4413 -- there is no need to displace the pointer.
4416 -- CW : I'Class := Obj;
4418 -- Temp : I'Class := I'Class (Base_Address (Obj'Address));
4419 -- CW : I'Class renames Displace (Temp, I'Tag);
4421 if Is_Interface (Typ)
4422 and then Is_Class_Wide_Type (Typ)
4424 (Is_Class_Wide_Type (Etype (Expr))
4426 not Is_Ancestor (Root_Type (Typ), Etype (Expr)))
4427 and then Comes_From_Source (Def_Id)
4428 and then VM_Target = No_VM
4436 Make_Object_Declaration (Loc,
4437 Defining_Identifier =>
4438 Make_Defining_Identifier (Loc,
4439 New_Internal_Name ('D')),
4441 Object_Definition =>
4442 Make_Attribute_Reference (Loc,
4445 (Root_Type (Etype (Def_Id)), Loc),
4446 Attribute_Name => Name_Class),
4449 Unchecked_Convert_To
4450 (Class_Wide_Type (Root_Type (Etype (Def_Id))),
4451 Make_Explicit_Dereference (Loc,
4452 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4453 Make_Function_Call (Loc,
4455 New_Reference_To (RTE (RE_Base_Address),
4457 Parameter_Associations => New_List (
4458 Make_Attribute_Reference (Loc,
4459 Prefix => Relocate_Node (Expr),
4460 Attribute_Name => Name_Address)))))));
4462 Insert_Action (N, Decl_1);
4465 Make_Object_Renaming_Declaration (Loc,
4466 Defining_Identifier =>
4467 Make_Defining_Identifier (Loc,
4468 New_Internal_Name ('D')),
4471 Make_Attribute_Reference (Loc,
4474 (Root_Type (Etype (Def_Id)), Loc),
4475 Attribute_Name => Name_Class),
4478 Unchecked_Convert_To (
4479 Class_Wide_Type (Root_Type (Etype (Def_Id))),
4480 Make_Explicit_Dereference (Loc,
4481 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4482 Make_Function_Call (Loc,
4484 New_Reference_To (RTE (RE_Displace), Loc),
4486 Parameter_Associations => New_List (
4487 Make_Attribute_Reference (Loc,
4490 (Defining_Identifier (Decl_1), Loc),
4491 Attribute_Name => Name_Address),
4493 Unchecked_Convert_To (RTE (RE_Tag),
4498 (Root_Type (Typ)))),
4501 Rewrite (N, Decl_2);
4504 -- Replace internal identifier of Decl_2 by the identifier
4505 -- found in the sources. We also have to exchange entities
4506 -- containing their defining identifiers to ensure the
4507 -- correct replacement of the object declaration by this
4508 -- object renaming declaration (because such definings
4509 -- identifier have been previously added by Enter_Name to
4510 -- the current scope). We must preserve the homonym chain
4511 -- of the source entity as well.
4513 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4514 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4515 Exchange_Entities (Defining_Identifier (N), Def_Id);
4521 -- If the type is controlled and not inherently limited, then
4522 -- the target is adjusted after the copy and attached to the
4523 -- finalization list. However, no adjustment is done in the case
4524 -- where the object was initialized by a call to a function whose
4525 -- result is built in place, since no copy occurred. (Eventually
4526 -- we plan to support in-place function results for some cases
4527 -- of nonlimited types. ???)
4529 if Needs_Finalization (Typ)
4530 and then not Is_Inherently_Limited_Type (Typ)
4531 and then not BIP_Call
4533 Insert_Actions_After (Init_After,
4535 Ref => New_Reference_To (Def_Id, Loc),
4536 Typ => Base_Type (Typ),
4537 Flist_Ref => Find_Final_List (Def_Id),
4538 With_Attach => Make_Integer_Literal (Loc, 1)));
4541 -- For tagged types, when an init value is given, the tag has to
4542 -- be re-initialized separately in order to avoid the propagation
4543 -- of a wrong tag coming from a view conversion unless the type
4544 -- is class wide (in this case the tag comes from the init value).
4545 -- Suppress the tag assignment when VM_Target because VM tags are
4546 -- represented implicitly in objects. Ditto for types that are
4547 -- CPP_CLASS, and for initializations that are aggregates, because
4548 -- they have to have the right tag.
4550 if Is_Tagged_Type (Typ)
4551 and then not Is_Class_Wide_Type (Typ)
4552 and then not Is_CPP_Class (Typ)
4553 and then VM_Target = No_VM
4554 and then Nkind (Expr) /= N_Aggregate
4556 -- The re-assignment of the tag has to be done even if the
4557 -- object is a constant.
4560 Make_Selected_Component (Loc,
4561 Prefix => New_Reference_To (Def_Id, Loc),
4563 New_Reference_To (First_Tag_Component (Typ), Loc));
4565 Set_Assignment_OK (New_Ref);
4567 Insert_After (Init_After,
4568 Make_Assignment_Statement (Loc,
4571 Unchecked_Convert_To (RTE (RE_Tag),
4575 (Access_Disp_Table (Base_Type (Typ)))),
4578 -- For discrete types, set the Is_Known_Valid flag if the
4579 -- initializing value is known to be valid.
4581 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
4582 Set_Is_Known_Valid (Def_Id);
4584 elsif Is_Access_Type (Typ) then
4586 -- For access types set the Is_Known_Non_Null flag if the
4587 -- initializing value is known to be non-null. We can also set
4588 -- Can_Never_Be_Null if this is a constant.
4590 if Known_Non_Null (Expr) then
4591 Set_Is_Known_Non_Null (Def_Id, True);
4593 if Constant_Present (N) then
4594 Set_Can_Never_Be_Null (Def_Id);
4599 -- If validity checking on copies, validate initial expression.
4600 -- But skip this if declaration is for a generic type, since it
4601 -- makes no sense to validate generic types. Not clear if this
4602 -- can happen for legal programs, but it definitely can arise
4603 -- from previous instantiation errors.
4605 if Validity_Checks_On
4606 and then Validity_Check_Copies
4607 and then not Is_Generic_Type (Etype (Def_Id))
4609 Ensure_Valid (Expr);
4610 Set_Is_Known_Valid (Def_Id);
4614 -- Cases where the back end cannot handle the initialization directly
4615 -- In such cases, we expand an assignment that will be appropriately
4616 -- handled by Expand_N_Assignment_Statement.
4618 -- The exclusion of the unconstrained case is wrong, but for now it
4619 -- is too much trouble ???
4621 if (Is_Possibly_Unaligned_Slice (Expr)
4622 or else (Is_Possibly_Unaligned_Object (Expr)
4623 and then not Represented_As_Scalar (Etype (Expr))))
4625 -- The exclusion of the unconstrained case is wrong, but for now
4626 -- it is too much trouble ???
4628 and then not (Is_Array_Type (Etype (Expr))
4629 and then not Is_Constrained (Etype (Expr)))
4632 Stat : constant Node_Id :=
4633 Make_Assignment_Statement (Loc,
4634 Name => New_Reference_To (Def_Id, Loc),
4635 Expression => Relocate_Node (Expr));
4637 Set_Expression (N, Empty);
4638 Set_No_Initialization (N);
4639 Set_Assignment_OK (Name (Stat));
4640 Set_No_Ctrl_Actions (Stat);
4641 Insert_After_And_Analyze (Init_After, Stat);
4647 when RE_Not_Available =>
4649 end Expand_N_Object_Declaration;
4651 ---------------------------------
4652 -- Expand_N_Subtype_Indication --
4653 ---------------------------------
4655 -- Add a check on the range of the subtype. The static case is partially
4656 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
4657 -- to check here for the static case in order to avoid generating
4658 -- extraneous expanded code. Also deal with validity checking.
4660 procedure Expand_N_Subtype_Indication (N : Node_Id) is
4661 Ran : constant Node_Id := Range_Expression (Constraint (N));
4662 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
4665 if Nkind (Constraint (N)) = N_Range_Constraint then
4666 Validity_Check_Range (Range_Expression (Constraint (N)));
4669 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
4670 Apply_Range_Check (Ran, Typ);
4672 end Expand_N_Subtype_Indication;
4674 ---------------------------
4675 -- Expand_N_Variant_Part --
4676 ---------------------------
4678 -- If the last variant does not contain the Others choice, replace it with
4679 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
4680 -- do not bother to call Analyze on the modified variant part, since it's
4681 -- only effect would be to compute the Others_Discrete_Choices node
4682 -- laboriously, and of course we already know the list of choices that
4683 -- corresponds to the others choice (it's the list we are replacing!)
4685 procedure Expand_N_Variant_Part (N : Node_Id) is
4686 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
4687 Others_Node : Node_Id;
4689 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
4690 Others_Node := Make_Others_Choice (Sloc (Last_Var));
4691 Set_Others_Discrete_Choices
4692 (Others_Node, Discrete_Choices (Last_Var));
4693 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
4695 end Expand_N_Variant_Part;
4697 ---------------------------------
4698 -- Expand_Previous_Access_Type --
4699 ---------------------------------
4701 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
4702 T : Entity_Id := First_Entity (Current_Scope);
4705 -- Find all access types declared in the current scope, whose
4706 -- designated type is Def_Id. If it does not have a Master_Id,
4709 while Present (T) loop
4710 if Is_Access_Type (T)
4711 and then Designated_Type (T) = Def_Id
4712 and then No (Master_Id (T))
4714 Build_Master_Entity (Def_Id);
4715 Build_Master_Renaming (Parent (Def_Id), T);
4720 end Expand_Previous_Access_Type;
4722 ------------------------------
4723 -- Expand_Record_Controller --
4724 ------------------------------
4726 procedure Expand_Record_Controller (T : Entity_Id) is
4727 Def : Node_Id := Type_Definition (Parent (T));
4728 Comp_List : Node_Id;
4729 Comp_Decl : Node_Id;
4731 First_Comp : Node_Id;
4732 Controller_Type : Entity_Id;
4736 if Nkind (Def) = N_Derived_Type_Definition then
4737 Def := Record_Extension_Part (Def);
4740 if Null_Present (Def) then
4741 Set_Component_List (Def,
4742 Make_Component_List (Sloc (Def),
4743 Component_Items => Empty_List,
4744 Variant_Part => Empty,
4745 Null_Present => True));
4748 Comp_List := Component_List (Def);
4750 if Null_Present (Comp_List)
4751 or else Is_Empty_List (Component_Items (Comp_List))
4753 Loc := Sloc (Comp_List);
4755 Loc := Sloc (First (Component_Items (Comp_List)));
4758 if Is_Inherently_Limited_Type (T) then
4759 Controller_Type := RTE (RE_Limited_Record_Controller);
4761 Controller_Type := RTE (RE_Record_Controller);
4764 Ent := Make_Defining_Identifier (Loc, Name_uController);
4767 Make_Component_Declaration (Loc,
4768 Defining_Identifier => Ent,
4769 Component_Definition =>
4770 Make_Component_Definition (Loc,
4771 Aliased_Present => False,
4772 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
4774 if Null_Present (Comp_List)
4775 or else Is_Empty_List (Component_Items (Comp_List))
4777 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4778 Set_Null_Present (Comp_List, False);
4781 -- The controller cannot be placed before the _Parent field since
4782 -- gigi lays out field in order and _parent must be first to preserve
4783 -- the polymorphism of tagged types.
4785 First_Comp := First (Component_Items (Comp_List));
4787 if not Is_Tagged_Type (T) then
4788 Insert_Before (First_Comp, Comp_Decl);
4790 -- if T is a tagged type, place controller declaration after parent
4791 -- field and after eventual tags of interface types.
4794 while Present (First_Comp)
4796 (Chars (Defining_Identifier (First_Comp)) = Name_uParent
4797 or else Is_Tag (Defining_Identifier (First_Comp))
4799 -- Ada 2005 (AI-251): The following condition covers secondary
4800 -- tags but also the adjacent component containing the offset
4801 -- to the base of the object (component generated if the parent
4802 -- has discriminants --- see Add_Interface_Tag_Components).
4803 -- This is required to avoid the addition of the controller
4804 -- between the secondary tag and its adjacent component.
4808 (Defining_Identifier (First_Comp))))
4813 -- An empty tagged extension might consist only of the parent
4814 -- component. Otherwise insert the controller before the first
4815 -- component that is neither parent nor tag.
4817 if Present (First_Comp) then
4818 Insert_Before (First_Comp, Comp_Decl);
4820 Append (Comp_Decl, Component_Items (Comp_List));
4826 Analyze (Comp_Decl);
4827 Set_Ekind (Ent, E_Component);
4828 Init_Component_Location (Ent);
4830 -- Move the _controller entity ahead in the list of internal entities
4831 -- of the enclosing record so that it is selected instead of a
4832 -- potentially inherited one.
4835 E : constant Entity_Id := Last_Entity (T);
4839 pragma Assert (Chars (E) = Name_uController);
4841 Set_Next_Entity (E, First_Entity (T));
4842 Set_First_Entity (T, E);
4844 Comp := Next_Entity (E);
4845 while Next_Entity (Comp) /= E loop
4849 Set_Next_Entity (Comp, Empty);
4850 Set_Last_Entity (T, Comp);
4856 when RE_Not_Available =>
4858 end Expand_Record_Controller;
4860 ------------------------
4861 -- Expand_Tagged_Root --
4862 ------------------------
4864 procedure Expand_Tagged_Root (T : Entity_Id) is
4865 Def : constant Node_Id := Type_Definition (Parent (T));
4866 Comp_List : Node_Id;
4867 Comp_Decl : Node_Id;
4868 Sloc_N : Source_Ptr;
4871 if Null_Present (Def) then
4872 Set_Component_List (Def,
4873 Make_Component_List (Sloc (Def),
4874 Component_Items => Empty_List,
4875 Variant_Part => Empty,
4876 Null_Present => True));
4879 Comp_List := Component_List (Def);
4881 if Null_Present (Comp_List)
4882 or else Is_Empty_List (Component_Items (Comp_List))
4884 Sloc_N := Sloc (Comp_List);
4886 Sloc_N := Sloc (First (Component_Items (Comp_List)));
4890 Make_Component_Declaration (Sloc_N,
4891 Defining_Identifier => First_Tag_Component (T),
4892 Component_Definition =>
4893 Make_Component_Definition (Sloc_N,
4894 Aliased_Present => False,
4895 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
4897 if Null_Present (Comp_List)
4898 or else Is_Empty_List (Component_Items (Comp_List))
4900 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4901 Set_Null_Present (Comp_List, False);
4904 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4907 -- We don't Analyze the whole expansion because the tag component has
4908 -- already been analyzed previously. Here we just insure that the tree
4909 -- is coherent with the semantic decoration
4911 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
4914 when RE_Not_Available =>
4916 end Expand_Tagged_Root;
4918 ----------------------
4919 -- Clean_Task_Names --
4920 ----------------------
4922 procedure Clean_Task_Names
4924 Proc_Id : Entity_Id)
4928 and then not Restriction_Active (No_Implicit_Heap_Allocations)
4929 and then not Global_Discard_Names
4930 and then VM_Target = No_VM
4932 Set_Uses_Sec_Stack (Proc_Id);
4934 end Clean_Task_Names;
4936 -----------------------
4937 -- Freeze_Array_Type --
4938 -----------------------
4940 procedure Freeze_Array_Type (N : Node_Id) is
4941 Typ : constant Entity_Id := Entity (N);
4942 Comp_Typ : constant Entity_Id := Component_Type (Typ);
4943 Base : constant Entity_Id := Base_Type (Typ);
4946 if not Is_Bit_Packed_Array (Typ) then
4948 -- If the component contains tasks, so does the array type. This may
4949 -- not be indicated in the array type because the component may have
4950 -- been a private type at the point of definition. Same if component
4951 -- type is controlled.
4953 Set_Has_Task (Base, Has_Task (Comp_Typ));
4954 Set_Has_Controlled_Component (Base,
4955 Has_Controlled_Component (Comp_Typ)
4956 or else Is_Controlled (Comp_Typ));
4958 if No (Init_Proc (Base)) then
4960 -- If this is an anonymous array created for a declaration with
4961 -- an initial value, its init_proc will never be called. The
4962 -- initial value itself may have been expanded into assignments,
4963 -- in which case the object declaration is carries the
4964 -- No_Initialization flag.
4967 and then Nkind (Associated_Node_For_Itype (Base)) =
4968 N_Object_Declaration
4969 and then (Present (Expression (Associated_Node_For_Itype (Base)))
4971 No_Initialization (Associated_Node_For_Itype (Base)))
4975 -- We do not need an init proc for string or wide [wide] string,
4976 -- since the only time these need initialization in normalize or
4977 -- initialize scalars mode, and these types are treated specially
4978 -- and do not need initialization procedures.
4980 elsif Root_Type (Base) = Standard_String
4981 or else Root_Type (Base) = Standard_Wide_String
4982 or else Root_Type (Base) = Standard_Wide_Wide_String
4986 -- Otherwise we have to build an init proc for the subtype
4989 Build_Array_Init_Proc (Base, N);
4994 if Has_Controlled_Component (Base) then
4995 Build_Controlling_Procs (Base);
4997 if not Is_Limited_Type (Comp_Typ)
4998 and then Number_Dimensions (Typ) = 1
5000 Build_Slice_Assignment (Typ);
5003 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5004 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5006 Set_Associated_Final_Chain (Comp_Typ, Add_Final_Chain (Typ));
5010 -- For packed case, default initialization, except if the component type
5011 -- is itself a packed structure with an initialization procedure, or
5012 -- initialize/normalize scalars active, and we have a base type, or the
5013 -- type is public, because in that case a client might specify
5014 -- Normalize_Scalars and there better be a public Init_Proc for it.
5016 elsif (Present (Init_Proc (Component_Type (Base)))
5017 and then No (Base_Init_Proc (Base)))
5018 or else (Init_Or_Norm_Scalars and then Base = Typ)
5019 or else Is_Public (Typ)
5021 Build_Array_Init_Proc (Base, N);
5023 end Freeze_Array_Type;
5025 -----------------------------
5026 -- Freeze_Enumeration_Type --
5027 -----------------------------
5029 procedure Freeze_Enumeration_Type (N : Node_Id) is
5030 Typ : constant Entity_Id := Entity (N);
5031 Loc : constant Source_Ptr := Sloc (Typ);
5038 Is_Contiguous : Boolean;
5043 pragma Warnings (Off, Func);
5046 -- Various optimizations possible if given representation is contiguous
5048 Is_Contiguous := True;
5050 Ent := First_Literal (Typ);
5051 Last_Repval := Enumeration_Rep (Ent);
5054 while Present (Ent) loop
5055 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5056 Is_Contiguous := False;
5059 Last_Repval := Enumeration_Rep (Ent);
5065 if Is_Contiguous then
5066 Set_Has_Contiguous_Rep (Typ);
5067 Ent := First_Literal (Typ);
5069 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5072 -- Build list of literal references
5077 Ent := First_Literal (Typ);
5078 while Present (Ent) loop
5079 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5085 -- Now build an array declaration
5087 -- typA : array (Natural range 0 .. num - 1) of ctype :=
5088 -- (v, v, v, v, v, ....)
5090 -- where ctype is the corresponding integer type. If the representation
5091 -- is contiguous, we only keep the first literal, which provides the
5092 -- offset for Pos_To_Rep computations.
5095 Make_Defining_Identifier (Loc,
5096 Chars => New_External_Name (Chars (Typ), 'A'));
5098 Append_Freeze_Action (Typ,
5099 Make_Object_Declaration (Loc,
5100 Defining_Identifier => Arr,
5101 Constant_Present => True,
5103 Object_Definition =>
5104 Make_Constrained_Array_Definition (Loc,
5105 Discrete_Subtype_Definitions => New_List (
5106 Make_Subtype_Indication (Loc,
5107 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5109 Make_Range_Constraint (Loc,
5113 Make_Integer_Literal (Loc, 0),
5115 Make_Integer_Literal (Loc, Num - 1))))),
5117 Component_Definition =>
5118 Make_Component_Definition (Loc,
5119 Aliased_Present => False,
5120 Subtype_Indication => New_Reference_To (Typ, Loc))),
5123 Make_Aggregate (Loc,
5124 Expressions => Lst)));
5126 Set_Enum_Pos_To_Rep (Typ, Arr);
5128 -- Now we build the function that converts representation values to
5129 -- position values. This function has the form:
5131 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5134 -- when enum-lit'Enum_Rep => return posval;
5135 -- when enum-lit'Enum_Rep => return posval;
5138 -- [raise Constraint_Error when F "invalid data"]
5143 -- Note: the F parameter determines whether the others case (no valid
5144 -- representation) raises Constraint_Error or returns a unique value
5145 -- of minus one. The latter case is used, e.g. in 'Valid code.
5147 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5148 -- the code generator making inappropriate assumptions about the range
5149 -- of the values in the case where the value is invalid. ityp is a
5150 -- signed or unsigned integer type of appropriate width.
5152 -- Note: if exceptions are not supported, then we suppress the raise
5153 -- and return -1 unconditionally (this is an erroneous program in any
5154 -- case and there is no obligation to raise Constraint_Error here!) We
5155 -- also do this if pragma Restrictions (No_Exceptions) is active.
5157 -- Is this right??? What about No_Exception_Propagation???
5159 -- Representations are signed
5161 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5163 -- The underlying type is signed. Reset the Is_Unsigned_Type
5164 -- explicitly, because it might have been inherited from
5167 Set_Is_Unsigned_Type (Typ, False);
5169 if Esize (Typ) <= Standard_Integer_Size then
5170 Ityp := Standard_Integer;
5172 Ityp := Universal_Integer;
5175 -- Representations are unsigned
5178 if Esize (Typ) <= Standard_Integer_Size then
5179 Ityp := RTE (RE_Unsigned);
5181 Ityp := RTE (RE_Long_Long_Unsigned);
5185 -- The body of the function is a case statement. First collect case
5186 -- alternatives, or optimize the contiguous case.
5190 -- If representation is contiguous, Pos is computed by subtracting
5191 -- the representation of the first literal.
5193 if Is_Contiguous then
5194 Ent := First_Literal (Typ);
5196 if Enumeration_Rep (Ent) = Last_Repval then
5198 -- Another special case: for a single literal, Pos is zero
5200 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5204 Convert_To (Standard_Integer,
5205 Make_Op_Subtract (Loc,
5207 Unchecked_Convert_To (Ityp,
5208 Make_Identifier (Loc, Name_uA)),
5210 Make_Integer_Literal (Loc,
5212 Enumeration_Rep (First_Literal (Typ)))));
5216 Make_Case_Statement_Alternative (Loc,
5217 Discrete_Choices => New_List (
5218 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5220 Make_Integer_Literal (Loc,
5221 Intval => Enumeration_Rep (Ent)),
5223 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5225 Statements => New_List (
5226 Make_Simple_Return_Statement (Loc,
5227 Expression => Pos_Expr))));
5230 Ent := First_Literal (Typ);
5231 while Present (Ent) loop
5233 Make_Case_Statement_Alternative (Loc,
5234 Discrete_Choices => New_List (
5235 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5236 Intval => Enumeration_Rep (Ent))),
5238 Statements => New_List (
5239 Make_Simple_Return_Statement (Loc,
5241 Make_Integer_Literal (Loc,
5242 Intval => Enumeration_Pos (Ent))))));
5248 -- In normal mode, add the others clause with the test
5250 if not No_Exception_Handlers_Set then
5252 Make_Case_Statement_Alternative (Loc,
5253 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5254 Statements => New_List (
5255 Make_Raise_Constraint_Error (Loc,
5256 Condition => Make_Identifier (Loc, Name_uF),
5257 Reason => CE_Invalid_Data),
5258 Make_Simple_Return_Statement (Loc,
5260 Make_Integer_Literal (Loc, -1)))));
5262 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5263 -- active then return -1 (we cannot usefully raise Constraint_Error in
5264 -- this case). See description above for further details.
5268 Make_Case_Statement_Alternative (Loc,
5269 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5270 Statements => New_List (
5271 Make_Simple_Return_Statement (Loc,
5273 Make_Integer_Literal (Loc, -1)))));
5276 -- Now we can build the function body
5279 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5282 Make_Subprogram_Body (Loc,
5284 Make_Function_Specification (Loc,
5285 Defining_Unit_Name => Fent,
5286 Parameter_Specifications => New_List (
5287 Make_Parameter_Specification (Loc,
5288 Defining_Identifier =>
5289 Make_Defining_Identifier (Loc, Name_uA),
5290 Parameter_Type => New_Reference_To (Typ, Loc)),
5291 Make_Parameter_Specification (Loc,
5292 Defining_Identifier =>
5293 Make_Defining_Identifier (Loc, Name_uF),
5294 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5296 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5298 Declarations => Empty_List,
5300 Handled_Statement_Sequence =>
5301 Make_Handled_Sequence_Of_Statements (Loc,
5302 Statements => New_List (
5303 Make_Case_Statement (Loc,
5305 Unchecked_Convert_To (Ityp,
5306 Make_Identifier (Loc, Name_uA)),
5307 Alternatives => Lst))));
5309 Set_TSS (Typ, Fent);
5312 if not Debug_Generated_Code then
5313 Set_Debug_Info_Off (Fent);
5317 when RE_Not_Available =>
5319 end Freeze_Enumeration_Type;
5321 ------------------------
5322 -- Freeze_Record_Type --
5323 ------------------------
5325 procedure Freeze_Record_Type (N : Node_Id) is
5327 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id);
5328 -- Add to the list of primitives of Tagged_Types the internal entities
5329 -- associated with interface primitives that are located in secondary
5332 -------------------------------------
5333 -- Add_Internal_Interface_Entities --
5334 -------------------------------------
5336 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
5339 Iface_Elmt : Elmt_Id;
5340 Iface_Prim : Entity_Id;
5341 Ifaces_List : Elist_Id;
5342 New_Subp : Entity_Id := Empty;
5346 pragma Assert (Ada_Version >= Ada_05
5347 and then Is_Record_Type (Tagged_Type)
5348 and then Is_Tagged_Type (Tagged_Type)
5349 and then Has_Interfaces (Tagged_Type)
5350 and then not Is_Interface (Tagged_Type));
5352 Collect_Interfaces (Tagged_Type, Ifaces_List);
5354 Iface_Elmt := First_Elmt (Ifaces_List);
5355 while Present (Iface_Elmt) loop
5356 Iface := Node (Iface_Elmt);
5358 -- Exclude from this processing interfaces that are parents
5359 -- of Tagged_Type because their primitives are located in the
5360 -- primary dispatch table (and hence no auxiliary internal
5361 -- entities are required to handle secondary dispatch tables
5364 if not Is_Ancestor (Iface, Tagged_Type) then
5365 Elmt := First_Elmt (Primitive_Operations (Iface));
5366 while Present (Elmt) loop
5367 Iface_Prim := Node (Elmt);
5369 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
5371 Find_Primitive_Covering_Interface
5372 (Tagged_Type => Tagged_Type,
5373 Iface_Prim => Iface_Prim);
5375 pragma Assert (Present (Prim));
5378 (New_Subp => New_Subp,
5379 Parent_Subp => Iface_Prim,
5380 Derived_Type => Tagged_Type,
5381 Parent_Type => Iface);
5383 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
5384 -- associated with interface types. These entities are
5385 -- only registered in the list of primitives of its
5386 -- corresponding tagged type because they are only used
5387 -- to fill the contents of the secondary dispatch tables.
5388 -- Therefore they are removed from the homonym chains.
5390 Set_Is_Hidden (New_Subp);
5391 Set_Is_Internal (New_Subp);
5392 Set_Alias (New_Subp, Prim);
5393 Set_Is_Abstract_Subprogram (New_Subp,
5394 Is_Abstract_Subprogram (Prim));
5395 Set_Interface_Alias (New_Subp, Iface_Prim);
5397 -- Internal entities associated with interface types are
5398 -- only registered in the list of primitives of the
5399 -- tagged type. They are only used to fill the contents
5400 -- of the secondary dispatch tables. Therefore they are
5401 -- not needed in the homonym chains.
5403 Remove_Homonym (New_Subp);
5405 -- Hidden entities associated with interfaces must have
5406 -- set the Has_Delay_Freeze attribute to ensure that, in
5407 -- case of locally defined tagged types (or compiling
5408 -- with static dispatch tables generation disabled) the
5409 -- corresponding entry of the secondary dispatch table is
5410 -- filled when such entity is frozen.
5412 Set_Has_Delayed_Freeze (New_Subp);
5419 Next_Elmt (Iface_Elmt);
5421 end Add_Internal_Interface_Entities;
5425 Def_Id : constant Node_Id := Entity (N);
5426 Type_Decl : constant Node_Id := Parent (Def_Id);
5428 Comp_Typ : Entity_Id;
5429 Has_Static_DT : Boolean := False;
5430 Predef_List : List_Id;
5432 Flist : Entity_Id := Empty;
5433 -- Finalization list allocated for the case of a type with anonymous
5434 -- access components whose designated type is potentially controlled.
5436 Renamed_Eq : Node_Id := Empty;
5437 -- Defining unit name for the predefined equality function in the case
5438 -- where the type has a primitive operation that is a renaming of
5439 -- predefined equality (but only if there is also an overriding
5440 -- user-defined equality function). Used to pass this entity from
5441 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5443 Wrapper_Decl_List : List_Id := No_List;
5444 Wrapper_Body_List : List_Id := No_List;
5445 Null_Proc_Decl_List : List_Id := No_List;
5447 -- Start of processing for Freeze_Record_Type
5450 -- Build discriminant checking functions if not a derived type (for
5451 -- derived types that are not tagged types, always use the discriminant
5452 -- checking functions of the parent type). However, for untagged types
5453 -- the derivation may have taken place before the parent was frozen, so
5454 -- we copy explicitly the discriminant checking functions from the
5455 -- parent into the components of the derived type.
5457 if not Is_Derived_Type (Def_Id)
5458 or else Has_New_Non_Standard_Rep (Def_Id)
5459 or else Is_Tagged_Type (Def_Id)
5461 Build_Discr_Checking_Funcs (Type_Decl);
5463 elsif Is_Derived_Type (Def_Id)
5464 and then not Is_Tagged_Type (Def_Id)
5466 -- If we have a derived Unchecked_Union, we do not inherit the
5467 -- discriminant checking functions from the parent type since the
5468 -- discriminants are non existent.
5470 and then not Is_Unchecked_Union (Def_Id)
5471 and then Has_Discriminants (Def_Id)
5474 Old_Comp : Entity_Id;
5478 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5479 Comp := First_Component (Def_Id);
5480 while Present (Comp) loop
5481 if Ekind (Comp) = E_Component
5482 and then Chars (Comp) = Chars (Old_Comp)
5484 Set_Discriminant_Checking_Func (Comp,
5485 Discriminant_Checking_Func (Old_Comp));
5488 Next_Component (Old_Comp);
5489 Next_Component (Comp);
5494 if Is_Derived_Type (Def_Id)
5495 and then Is_Limited_Type (Def_Id)
5496 and then Is_Tagged_Type (Def_Id)
5498 Check_Stream_Attributes (Def_Id);
5501 -- Update task and controlled component flags, because some of the
5502 -- component types may have been private at the point of the record
5505 Comp := First_Component (Def_Id);
5507 while Present (Comp) loop
5508 Comp_Typ := Etype (Comp);
5510 if Has_Task (Comp_Typ) then
5511 Set_Has_Task (Def_Id);
5513 elsif Has_Controlled_Component (Comp_Typ)
5514 or else (Chars (Comp) /= Name_uParent
5515 and then Is_Controlled (Comp_Typ))
5517 Set_Has_Controlled_Component (Def_Id);
5519 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5520 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5523 Flist := Add_Final_Chain (Def_Id);
5526 Set_Associated_Final_Chain (Comp_Typ, Flist);
5529 Next_Component (Comp);
5532 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5533 -- for regular tagged types as well as for Ada types deriving from a C++
5534 -- Class, but not for tagged types directly corresponding to C++ classes
5535 -- In the later case we assume that it is created in the C++ side and we
5538 if Is_Tagged_Type (Def_Id) then
5540 Static_Dispatch_Tables
5541 and then Is_Library_Level_Tagged_Type (Def_Id);
5543 -- Add the _Tag component
5545 if Underlying_Type (Etype (Def_Id)) = Def_Id then
5546 Expand_Tagged_Root (Def_Id);
5549 if Is_CPP_Class (Def_Id) then
5550 Set_All_DT_Position (Def_Id);
5551 Set_Default_Constructor (Def_Id);
5553 -- Create the tag entities with a minimum decoration
5555 if VM_Target = No_VM then
5556 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5560 if not Has_Static_DT then
5562 -- Usually inherited primitives are not delayed but the first
5563 -- Ada extension of a CPP_Class is an exception since the
5564 -- address of the inherited subprogram has to be inserted in
5565 -- the new Ada Dispatch Table and this is a freezing action.
5567 -- Similarly, if this is an inherited operation whose parent is
5568 -- not frozen yet, it is not in the DT of the parent, and we
5569 -- generate an explicit freeze node for the inherited operation
5570 -- so that it is properly inserted in the DT of the current
5574 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
5578 while Present (Elmt) loop
5579 Subp := Node (Elmt);
5581 if Present (Alias (Subp)) then
5582 if Is_CPP_Class (Etype (Def_Id)) then
5583 Set_Has_Delayed_Freeze (Subp);
5585 elsif Has_Delayed_Freeze (Alias (Subp))
5586 and then not Is_Frozen (Alias (Subp))
5588 Set_Is_Frozen (Subp, False);
5589 Set_Has_Delayed_Freeze (Subp);
5598 -- Unfreeze momentarily the type to add the predefined primitives
5599 -- operations. The reason we unfreeze is so that these predefined
5600 -- operations will indeed end up as primitive operations (which
5601 -- must be before the freeze point).
5603 Set_Is_Frozen (Def_Id, False);
5605 -- Do not add the spec of predefined primitives in case of
5606 -- CPP tagged type derivations that have convention CPP.
5608 if Is_CPP_Class (Root_Type (Def_Id))
5609 and then Convention (Def_Id) = Convention_CPP
5613 -- Do not add the spec of the predefined primitives if we are
5614 -- compiling under restriction No_Dispatching_Calls
5616 elsif not Restriction_Active (No_Dispatching_Calls) then
5617 Make_Predefined_Primitive_Specs
5618 (Def_Id, Predef_List, Renamed_Eq);
5619 Insert_List_Before_And_Analyze (N, Predef_List);
5622 -- Ada 2005 (AI-391): For a nonabstract null extension, create
5623 -- wrapper functions for each nonoverridden inherited function
5624 -- with a controlling result of the type. The wrapper for such
5625 -- a function returns an extension aggregate that invokes the
5626 -- the parent function.
5628 if Ada_Version >= Ada_05
5629 and then not Is_Abstract_Type (Def_Id)
5630 and then Is_Null_Extension (Def_Id)
5632 Make_Controlling_Function_Wrappers
5633 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
5634 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
5637 -- Ada 2005 (AI-251): For a nonabstract type extension, build
5638 -- null procedure declarations for each set of homographic null
5639 -- procedures that are inherited from interface types but not
5640 -- overridden. This is done to ensure that the dispatch table
5641 -- entry associated with such null primitives are properly filled.
5643 if Ada_Version >= Ada_05
5644 and then Etype (Def_Id) /= Def_Id
5645 and then not Is_Abstract_Type (Def_Id)
5647 Make_Null_Procedure_Specs (Def_Id, Null_Proc_Decl_List);
5648 Insert_Actions (N, Null_Proc_Decl_List);
5651 -- Ada 2005 (AI-251): Add internal entities associated with
5652 -- secondary dispatch tables to the list of primitives of tagged
5653 -- types that are not interfaces
5655 if Ada_Version >= Ada_05
5656 and then not Is_Interface (Def_Id)
5657 and then Has_Interfaces (Def_Id)
5659 Add_Internal_Interface_Entities (Def_Id);
5662 Set_Is_Frozen (Def_Id);
5663 Set_All_DT_Position (Def_Id);
5665 -- Add the controlled component before the freezing actions
5666 -- referenced in those actions.
5668 if Has_New_Controlled_Component (Def_Id) then
5669 Expand_Record_Controller (Def_Id);
5672 -- Create and decorate the tags. Suppress their creation when
5673 -- VM_Target because the dispatching mechanism is handled
5674 -- internally by the VMs.
5676 if VM_Target = No_VM then
5677 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
5679 -- Generate dispatch table of locally defined tagged type.
5680 -- Dispatch tables of library level tagged types are built
5681 -- later (see Analyze_Declarations).
5683 if VM_Target = No_VM
5684 and then not Has_Static_DT
5686 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
5690 -- Make sure that the primitives Initialize, Adjust and Finalize
5691 -- are Frozen before other TSS subprograms. We don't want them
5694 if Is_Controlled (Def_Id) then
5695 if not Is_Limited_Type (Def_Id) then
5696 Append_Freeze_Actions (Def_Id,
5698 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
5701 Append_Freeze_Actions (Def_Id,
5703 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
5705 Append_Freeze_Actions (Def_Id,
5707 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
5710 -- Freeze rest of primitive operations. There is no need to handle
5711 -- the predefined primitives if we are compiling under restriction
5712 -- No_Dispatching_Calls
5714 if not Restriction_Active (No_Dispatching_Calls) then
5715 Append_Freeze_Actions
5716 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
5720 -- In the non-tagged case, an equality function is provided only for
5721 -- variant records (that are not unchecked unions).
5723 elsif Has_Discriminants (Def_Id)
5724 and then not Is_Limited_Type (Def_Id)
5727 Comps : constant Node_Id :=
5728 Component_List (Type_Definition (Type_Decl));
5732 and then Present (Variant_Part (Comps))
5734 Build_Variant_Record_Equality (Def_Id);
5739 -- Before building the record initialization procedure, if we are
5740 -- dealing with a concurrent record value type, then we must go through
5741 -- the discriminants, exchanging discriminals between the concurrent
5742 -- type and the concurrent record value type. See the section "Handling
5743 -- of Discriminants" in the Einfo spec for details.
5745 if Is_Concurrent_Record_Type (Def_Id)
5746 and then Has_Discriminants (Def_Id)
5749 Ctyp : constant Entity_Id :=
5750 Corresponding_Concurrent_Type (Def_Id);
5751 Conc_Discr : Entity_Id;
5752 Rec_Discr : Entity_Id;
5756 Conc_Discr := First_Discriminant (Ctyp);
5757 Rec_Discr := First_Discriminant (Def_Id);
5759 while Present (Conc_Discr) loop
5760 Temp := Discriminal (Conc_Discr);
5761 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
5762 Set_Discriminal (Rec_Discr, Temp);
5764 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
5765 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
5767 Next_Discriminant (Conc_Discr);
5768 Next_Discriminant (Rec_Discr);
5773 if Has_Controlled_Component (Def_Id) then
5774 if No (Controller_Component (Def_Id)) then
5775 Expand_Record_Controller (Def_Id);
5778 Build_Controlling_Procs (Def_Id);
5781 Adjust_Discriminants (Def_Id);
5783 if VM_Target = No_VM or else not Is_Interface (Def_Id) then
5785 -- Do not need init for interfaces on e.g. CIL since they're
5786 -- abstract. Helps operation of peverify (the PE Verify tool).
5788 Build_Record_Init_Proc (Type_Decl, Def_Id);
5791 -- For tagged type that are not interfaces, build bodies of primitive
5792 -- operations. Note that we do this after building the record
5793 -- initialization procedure, since the primitive operations may need
5794 -- the initialization routine. There is no need to add predefined
5795 -- primitives of interfaces because all their predefined primitives
5798 if Is_Tagged_Type (Def_Id)
5799 and then not Is_Interface (Def_Id)
5801 -- Do not add the body of predefined primitives in case of
5802 -- CPP tagged type derivations that have convention CPP.
5804 if Is_CPP_Class (Root_Type (Def_Id))
5805 and then Convention (Def_Id) = Convention_CPP
5809 -- Do not add the body of the predefined primitives if we are
5810 -- compiling under restriction No_Dispatching_Calls or if we are
5811 -- compiling a CPP tagged type.
5813 elsif not Restriction_Active (No_Dispatching_Calls) then
5814 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
5815 Append_Freeze_Actions (Def_Id, Predef_List);
5818 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
5819 -- inherited functions, then add their bodies to the freeze actions.
5821 if Present (Wrapper_Body_List) then
5822 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
5825 end Freeze_Record_Type;
5827 ------------------------------
5828 -- Freeze_Stream_Operations --
5829 ------------------------------
5831 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
5832 Names : constant array (1 .. 4) of TSS_Name_Type :=
5837 Stream_Op : Entity_Id;
5840 -- Primitive operations of tagged types are frozen when the dispatch
5841 -- table is constructed.
5843 if not Comes_From_Source (Typ)
5844 or else Is_Tagged_Type (Typ)
5849 for J in Names'Range loop
5850 Stream_Op := TSS (Typ, Names (J));
5852 if Present (Stream_Op)
5853 and then Is_Subprogram (Stream_Op)
5854 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
5855 N_Subprogram_Declaration
5856 and then not Is_Frozen (Stream_Op)
5858 Append_Freeze_Actions
5859 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
5862 end Freeze_Stream_Operations;
5868 -- Full type declarations are expanded at the point at which the type is
5869 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
5870 -- declarations generated by the freezing (e.g. the procedure generated
5871 -- for initialization) are chained in the Actions field list of the freeze
5872 -- node using Append_Freeze_Actions.
5874 function Freeze_Type (N : Node_Id) return Boolean is
5875 Def_Id : constant Entity_Id := Entity (N);
5876 RACW_Seen : Boolean := False;
5877 Result : Boolean := False;
5880 -- Process associated access types needing special processing
5882 if Present (Access_Types_To_Process (N)) then
5884 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
5886 while Present (E) loop
5888 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
5889 Validate_RACW_Primitives (Node (E));
5899 -- If there are RACWs designating this type, make stubs now
5901 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
5905 -- Freeze processing for record types
5907 if Is_Record_Type (Def_Id) then
5908 if Ekind (Def_Id) = E_Record_Type then
5909 Freeze_Record_Type (N);
5911 -- The subtype may have been declared before the type was frozen. If
5912 -- the type has controlled components it is necessary to create the
5913 -- entity for the controller explicitly because it did not exist at
5914 -- the point of the subtype declaration. Only the entity is needed,
5915 -- the back-end will obtain the layout from the type. This is only
5916 -- necessary if this is constrained subtype whose component list is
5917 -- not shared with the base type.
5919 elsif Ekind (Def_Id) = E_Record_Subtype
5920 and then Has_Discriminants (Def_Id)
5921 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
5922 and then Present (Controller_Component (Def_Id))
5925 Old_C : constant Entity_Id := Controller_Component (Def_Id);
5929 if Scope (Old_C) = Base_Type (Def_Id) then
5931 -- The entity is the one in the parent. Create new one
5933 New_C := New_Copy (Old_C);
5934 Set_Parent (New_C, Parent (Old_C));
5935 Push_Scope (Def_Id);
5941 if Is_Itype (Def_Id)
5942 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
5944 -- The freeze node is only used to introduce the controller,
5945 -- the back-end has no use for it for a discriminated
5948 Set_Freeze_Node (Def_Id, Empty);
5949 Set_Has_Delayed_Freeze (Def_Id, False);
5953 -- Similar process if the controller of the subtype is not present
5954 -- but the parent has it. This can happen with constrained
5955 -- record components where the subtype is an itype.
5957 elsif Ekind (Def_Id) = E_Record_Subtype
5958 and then Is_Itype (Def_Id)
5959 and then No (Controller_Component (Def_Id))
5960 and then Present (Controller_Component (Etype (Def_Id)))
5963 Old_C : constant Entity_Id :=
5964 Controller_Component (Etype (Def_Id));
5965 New_C : constant Entity_Id := New_Copy (Old_C);
5968 Set_Next_Entity (New_C, First_Entity (Def_Id));
5969 Set_First_Entity (Def_Id, New_C);
5971 -- The freeze node is only used to introduce the controller,
5972 -- the back-end has no use for it for a discriminated
5975 Set_Freeze_Node (Def_Id, Empty);
5976 Set_Has_Delayed_Freeze (Def_Id, False);
5981 -- Freeze processing for array types
5983 elsif Is_Array_Type (Def_Id) then
5984 Freeze_Array_Type (N);
5986 -- Freeze processing for access types
5988 -- For pool-specific access types, find out the pool object used for
5989 -- this type, needs actual expansion of it in some cases. Here are the
5990 -- different cases :
5992 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
5993 -- ---> don't use any storage pool
5995 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
5997 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
5999 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6000 -- ---> Storage Pool is the specified one
6002 -- See GNAT Pool packages in the Run-Time for more details
6004 elsif Ekind (Def_Id) = E_Access_Type
6005 or else Ekind (Def_Id) = E_General_Access_Type
6008 Loc : constant Source_Ptr := Sloc (N);
6009 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
6010 Pool_Object : Entity_Id;
6012 Freeze_Action_Typ : Entity_Id;
6017 -- Rep Clause "for Def_Id'Storage_Size use 0;"
6018 -- ---> don't use any storage pool
6020 if No_Pool_Assigned (Def_Id) then
6025 -- Rep Clause : for Def_Id'Storage_Size use Expr.
6027 -- Def_Id__Pool : Stack_Bounded_Pool
6028 -- (Expr, DT'Size, DT'Alignment);
6030 elsif Has_Storage_Size_Clause (Def_Id) then
6036 -- For unconstrained composite types we give a size of zero
6037 -- so that the pool knows that it needs a special algorithm
6038 -- for variable size object allocation.
6040 if Is_Composite_Type (Desig_Type)
6041 and then not Is_Constrained (Desig_Type)
6044 Make_Integer_Literal (Loc, 0);
6047 Make_Integer_Literal (Loc, Maximum_Alignment);
6051 Make_Attribute_Reference (Loc,
6052 Prefix => New_Reference_To (Desig_Type, Loc),
6053 Attribute_Name => Name_Max_Size_In_Storage_Elements);
6056 Make_Attribute_Reference (Loc,
6057 Prefix => New_Reference_To (Desig_Type, Loc),
6058 Attribute_Name => Name_Alignment);
6062 Make_Defining_Identifier (Loc,
6063 Chars => New_External_Name (Chars (Def_Id), 'P'));
6065 -- We put the code associated with the pools in the entity
6066 -- that has the later freeze node, usually the access type
6067 -- but it can also be the designated_type; because the pool
6068 -- code requires both those types to be frozen
6070 if Is_Frozen (Desig_Type)
6071 and then (No (Freeze_Node (Desig_Type))
6072 or else Analyzed (Freeze_Node (Desig_Type)))
6074 Freeze_Action_Typ := Def_Id;
6076 -- A Taft amendment type cannot get the freeze actions
6077 -- since the full view is not there.
6079 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6080 and then No (Full_View (Desig_Type))
6082 Freeze_Action_Typ := Def_Id;
6085 Freeze_Action_Typ := Desig_Type;
6088 Append_Freeze_Action (Freeze_Action_Typ,
6089 Make_Object_Declaration (Loc,
6090 Defining_Identifier => Pool_Object,
6091 Object_Definition =>
6092 Make_Subtype_Indication (Loc,
6095 (RTE (RE_Stack_Bounded_Pool), Loc),
6098 Make_Index_Or_Discriminant_Constraint (Loc,
6099 Constraints => New_List (
6101 -- First discriminant is the Pool Size
6104 Storage_Size_Variable (Def_Id), Loc),
6106 -- Second discriminant is the element size
6110 -- Third discriminant is the alignment
6115 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6119 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6120 -- ---> Storage Pool is the specified one
6122 elsif Present (Associated_Storage_Pool (Def_Id)) then
6124 -- Nothing to do the associated storage pool has been attached
6125 -- when analyzing the rep. clause
6130 -- For access-to-controlled types (including class-wide types and
6131 -- Taft-amendment types which potentially have controlled
6132 -- components), expand the list controller object that will store
6133 -- the dynamically allocated objects. Do not do this
6134 -- transformation for expander-generated access types, but do it
6135 -- for types that are the full view of types derived from other
6136 -- private types. Also suppress the list controller in the case
6137 -- of a designated type with convention Java, since this is used
6138 -- when binding to Java API specs, where there's no equivalent of
6139 -- a finalization list and we don't want to pull in the
6140 -- finalization support if not needed.
6142 if not Comes_From_Source (Def_Id)
6143 and then not Has_Private_Declaration (Def_Id)
6147 elsif (Needs_Finalization (Desig_Type)
6148 and then Convention (Desig_Type) /= Convention_Java
6149 and then Convention (Desig_Type) /= Convention_CIL)
6151 (Is_Incomplete_Or_Private_Type (Desig_Type)
6152 and then No (Full_View (Desig_Type))
6154 -- An exception is made for types defined in the run-time
6155 -- because Ada.Tags.Tag itself is such a type and cannot
6156 -- afford this unnecessary overhead that would generates a
6157 -- loop in the expansion scheme...
6159 and then not In_Runtime (Def_Id)
6161 -- Another exception is if Restrictions (No_Finalization)
6162 -- is active, since then we know nothing is controlled.
6164 and then not Restriction_Active (No_Finalization))
6166 -- If the designated type is not frozen yet, its controlled
6167 -- status must be retrieved explicitly.
6169 or else (Is_Array_Type (Desig_Type)
6170 and then not Is_Frozen (Desig_Type)
6171 and then Needs_Finalization (Component_Type (Desig_Type)))
6173 -- The designated type has controlled anonymous access
6176 or else Has_Controlled_Coextensions (Desig_Type)
6178 Set_Associated_Final_Chain (Def_Id, Add_Final_Chain (Def_Id));
6182 -- Freeze processing for enumeration types
6184 elsif Ekind (Def_Id) = E_Enumeration_Type then
6186 -- We only have something to do if we have a non-standard
6187 -- representation (i.e. at least one literal whose pos value
6188 -- is not the same as its representation)
6190 if Has_Non_Standard_Rep (Def_Id) then
6191 Freeze_Enumeration_Type (N);
6194 -- Private types that are completed by a derivation from a private
6195 -- type have an internally generated full view, that needs to be
6196 -- frozen. This must be done explicitly because the two views share
6197 -- the freeze node, and the underlying full view is not visible when
6198 -- the freeze node is analyzed.
6200 elsif Is_Private_Type (Def_Id)
6201 and then Is_Derived_Type (Def_Id)
6202 and then Present (Full_View (Def_Id))
6203 and then Is_Itype (Full_View (Def_Id))
6204 and then Has_Private_Declaration (Full_View (Def_Id))
6205 and then Freeze_Node (Full_View (Def_Id)) = N
6207 Set_Entity (N, Full_View (Def_Id));
6208 Result := Freeze_Type (N);
6209 Set_Entity (N, Def_Id);
6211 -- All other types require no expander action. There are such cases
6212 -- (e.g. task types and protected types). In such cases, the freeze
6213 -- nodes are there for use by Gigi.
6217 Freeze_Stream_Operations (N, Def_Id);
6221 when RE_Not_Available =>
6225 -------------------------
6226 -- Get_Simple_Init_Val --
6227 -------------------------
6229 function Get_Simple_Init_Val
6232 Size : Uint := No_Uint) return Node_Id
6234 Loc : constant Source_Ptr := Sloc (N);
6240 -- This is the size to be used for computation of the appropriate
6241 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
6243 IV_Attribute : constant Boolean :=
6244 Nkind (N) = N_Attribute_Reference
6245 and then Attribute_Name (N) = Name_Invalid_Value;
6249 -- These are the values computed by the procedure Check_Subtype_Bounds
6251 procedure Check_Subtype_Bounds;
6252 -- This procedure examines the subtype T, and its ancestor subtypes and
6253 -- derived types to determine the best known information about the
6254 -- bounds of the subtype. After the call Lo_Bound is set either to
6255 -- No_Uint if no information can be determined, or to a value which
6256 -- represents a known low bound, i.e. a valid value of the subtype can
6257 -- not be less than this value. Hi_Bound is similarly set to a known
6258 -- high bound (valid value cannot be greater than this).
6260 --------------------------
6261 -- Check_Subtype_Bounds --
6262 --------------------------
6264 procedure Check_Subtype_Bounds is
6273 Lo_Bound := No_Uint;
6274 Hi_Bound := No_Uint;
6276 -- Loop to climb ancestor subtypes and derived types
6280 if not Is_Discrete_Type (ST1) then
6284 Lo := Type_Low_Bound (ST1);
6285 Hi := Type_High_Bound (ST1);
6287 if Compile_Time_Known_Value (Lo) then
6288 Loval := Expr_Value (Lo);
6290 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6295 if Compile_Time_Known_Value (Hi) then
6296 Hival := Expr_Value (Hi);
6298 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6303 ST2 := Ancestor_Subtype (ST1);
6309 exit when ST1 = ST2;
6312 end Check_Subtype_Bounds;
6314 -- Start of processing for Get_Simple_Init_Val
6317 -- For a private type, we should always have an underlying type
6318 -- (because this was already checked in Needs_Simple_Initialization).
6319 -- What we do is to get the value for the underlying type and then do
6320 -- an Unchecked_Convert to the private type.
6322 if Is_Private_Type (T) then
6323 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6325 -- A special case, if the underlying value is null, then qualify it
6326 -- with the underlying type, so that the null is properly typed
6327 -- Similarly, if it is an aggregate it must be qualified, because an
6328 -- unchecked conversion does not provide a context for it.
6330 if Nkind_In (Val, N_Null, N_Aggregate) then
6332 Make_Qualified_Expression (Loc,
6334 New_Occurrence_Of (Underlying_Type (T), Loc),
6338 Result := Unchecked_Convert_To (T, Val);
6340 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6342 if Nkind (Result) = N_Unchecked_Type_Conversion
6343 and then Is_Scalar_Type (Underlying_Type (T))
6345 Set_No_Truncation (Result);
6350 -- For scalars, we must have normalize/initialize scalars case, or
6351 -- if the node N is an 'Invalid_Value attribute node.
6353 elsif Is_Scalar_Type (T) then
6354 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6356 -- Compute size of object. If it is given by the caller, we can use
6357 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6358 -- we know this covers all cases correctly.
6360 if Size = No_Uint or else Size <= Uint_0 then
6361 Size_To_Use := UI_Max (Uint_1, Esize (T));
6363 Size_To_Use := Size;
6366 -- Maximum size to use is 64 bits, since we will create values
6367 -- of type Unsigned_64 and the range must fit this type.
6369 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6370 Size_To_Use := Uint_64;
6373 -- Check known bounds of subtype
6375 Check_Subtype_Bounds;
6377 -- Processing for Normalize_Scalars case
6379 if Normalize_Scalars and then not IV_Attribute then
6381 -- If zero is invalid, it is a convenient value to use that is
6382 -- for sure an appropriate invalid value in all situations.
6384 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6385 Val := Make_Integer_Literal (Loc, 0);
6387 -- Cases where all one bits is the appropriate invalid value
6389 -- For modular types, all 1 bits is either invalid or valid. If
6390 -- it is valid, then there is nothing that can be done since there
6391 -- are no invalid values (we ruled out zero already).
6393 -- For signed integer types that have no negative values, either
6394 -- there is room for negative values, or there is not. If there
6395 -- is, then all 1 bits may be interpreted as minus one, which is
6396 -- certainly invalid. Alternatively it is treated as the largest
6397 -- positive value, in which case the observation for modular types
6400 -- For float types, all 1-bits is a NaN (not a number), which is
6401 -- certainly an appropriately invalid value.
6403 elsif Is_Unsigned_Type (T)
6404 or else Is_Floating_Point_Type (T)
6405 or else Is_Enumeration_Type (T)
6407 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6409 -- Resolve as Unsigned_64, because the largest number we
6410 -- can generate is out of range of universal integer.
6412 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6414 -- Case of signed types
6418 Signed_Size : constant Uint :=
6419 UI_Min (Uint_63, Size_To_Use - 1);
6422 -- Normally we like to use the most negative number. The
6423 -- one exception is when this number is in the known
6424 -- subtype range and the largest positive number is not in
6425 -- the known subtype range.
6427 -- For this exceptional case, use largest positive value
6429 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6430 and then Lo_Bound <= (-(2 ** Signed_Size))
6431 and then Hi_Bound < 2 ** Signed_Size
6433 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6435 -- Normal case of largest negative value
6438 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6443 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
6446 -- For float types, use float values from System.Scalar_Values
6448 if Is_Floating_Point_Type (T) then
6449 if Root_Type (T) = Standard_Short_Float then
6450 Val_RE := RE_IS_Isf;
6451 elsif Root_Type (T) = Standard_Float then
6452 Val_RE := RE_IS_Ifl;
6453 elsif Root_Type (T) = Standard_Long_Float then
6454 Val_RE := RE_IS_Ilf;
6455 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6456 Val_RE := RE_IS_Ill;
6459 -- If zero is invalid, use zero values from System.Scalar_Values
6461 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6462 if Size_To_Use <= 8 then
6463 Val_RE := RE_IS_Iz1;
6464 elsif Size_To_Use <= 16 then
6465 Val_RE := RE_IS_Iz2;
6466 elsif Size_To_Use <= 32 then
6467 Val_RE := RE_IS_Iz4;
6469 Val_RE := RE_IS_Iz8;
6472 -- For unsigned, use unsigned values from System.Scalar_Values
6474 elsif Is_Unsigned_Type (T) then
6475 if Size_To_Use <= 8 then
6476 Val_RE := RE_IS_Iu1;
6477 elsif Size_To_Use <= 16 then
6478 Val_RE := RE_IS_Iu2;
6479 elsif Size_To_Use <= 32 then
6480 Val_RE := RE_IS_Iu4;
6482 Val_RE := RE_IS_Iu8;
6485 -- For signed, use signed values from System.Scalar_Values
6488 if Size_To_Use <= 8 then
6489 Val_RE := RE_IS_Is1;
6490 elsif Size_To_Use <= 16 then
6491 Val_RE := RE_IS_Is2;
6492 elsif Size_To_Use <= 32 then
6493 Val_RE := RE_IS_Is4;
6495 Val_RE := RE_IS_Is8;
6499 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
6502 -- The final expression is obtained by doing an unchecked conversion
6503 -- of this result to the base type of the required subtype. We use
6504 -- the base type to avoid the unchecked conversion from chopping
6505 -- bits, and then we set Kill_Range_Check to preserve the "bad"
6508 Result := Unchecked_Convert_To (Base_Type (T), Val);
6510 -- Ensure result is not truncated, since we want the "bad" bits
6511 -- and also kill range check on result.
6513 if Nkind (Result) = N_Unchecked_Type_Conversion then
6514 Set_No_Truncation (Result);
6515 Set_Kill_Range_Check (Result, True);
6520 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
6522 elsif Root_Type (T) = Standard_String
6524 Root_Type (T) = Standard_Wide_String
6526 Root_Type (T) = Standard_Wide_Wide_String
6528 pragma Assert (Init_Or_Norm_Scalars);
6531 Make_Aggregate (Loc,
6532 Component_Associations => New_List (
6533 Make_Component_Association (Loc,
6534 Choices => New_List (
6535 Make_Others_Choice (Loc)),
6538 (Component_Type (T), N, Esize (Root_Type (T))))));
6540 -- Access type is initialized to null
6542 elsif Is_Access_Type (T) then
6546 -- No other possibilities should arise, since we should only be
6547 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
6548 -- returned True, indicating one of the above cases held.
6551 raise Program_Error;
6555 when RE_Not_Available =>
6557 end Get_Simple_Init_Val;
6559 ------------------------------
6560 -- Has_New_Non_Standard_Rep --
6561 ------------------------------
6563 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
6565 if not Is_Derived_Type (T) then
6566 return Has_Non_Standard_Rep (T)
6567 or else Has_Non_Standard_Rep (Root_Type (T));
6569 -- If Has_Non_Standard_Rep is not set on the derived type, the
6570 -- representation is fully inherited.
6572 elsif not Has_Non_Standard_Rep (T) then
6576 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
6578 -- May need a more precise check here: the First_Rep_Item may
6579 -- be a stream attribute, which does not affect the representation
6582 end Has_New_Non_Standard_Rep;
6588 function In_Runtime (E : Entity_Id) return Boolean is
6593 while Scope (S1) /= Standard_Standard loop
6597 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
6600 ----------------------------
6601 -- Initialization_Warning --
6602 ----------------------------
6604 procedure Initialization_Warning (E : Entity_Id) is
6605 Warning_Needed : Boolean;
6608 Warning_Needed := False;
6610 if Ekind (Current_Scope) = E_Package
6611 and then Static_Elaboration_Desired (Current_Scope)
6614 if Is_Record_Type (E) then
6615 if Has_Discriminants (E)
6616 or else Is_Limited_Type (E)
6617 or else Has_Non_Standard_Rep (E)
6619 Warning_Needed := True;
6622 -- Verify that at least one component has an initialization
6623 -- expression. No need for a warning on a type if all its
6624 -- components have no initialization.
6630 Comp := First_Component (E);
6631 while Present (Comp) loop
6632 if Ekind (Comp) = E_Discriminant
6634 (Nkind (Parent (Comp)) = N_Component_Declaration
6635 and then Present (Expression (Parent (Comp))))
6637 Warning_Needed := True;
6641 Next_Component (Comp);
6646 if Warning_Needed then
6648 ("Objects of the type cannot be initialized " &
6649 "statically by default?",
6655 Error_Msg_N ("Object cannot be initialized statically?", E);
6658 end Initialization_Warning;
6664 function Init_Formals (Typ : Entity_Id) return List_Id is
6665 Loc : constant Source_Ptr := Sloc (Typ);
6669 -- First parameter is always _Init : in out typ. Note that we need
6670 -- this to be in/out because in the case of the task record value,
6671 -- there are default record fields (_Priority, _Size, -Task_Info)
6672 -- that may be referenced in the generated initialization routine.
6674 Formals := New_List (
6675 Make_Parameter_Specification (Loc,
6676 Defining_Identifier =>
6677 Make_Defining_Identifier (Loc, Name_uInit),
6679 Out_Present => True,
6680 Parameter_Type => New_Reference_To (Typ, Loc)));
6682 -- For task record value, or type that contains tasks, add two more
6683 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
6684 -- We also add these parameters for the task record type case.
6687 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
6690 Make_Parameter_Specification (Loc,
6691 Defining_Identifier =>
6692 Make_Defining_Identifier (Loc, Name_uMaster),
6693 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
6696 Make_Parameter_Specification (Loc,
6697 Defining_Identifier =>
6698 Make_Defining_Identifier (Loc, Name_uChain),
6700 Out_Present => True,
6702 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
6705 Make_Parameter_Specification (Loc,
6706 Defining_Identifier =>
6707 Make_Defining_Identifier (Loc, Name_uTask_Name),
6710 New_Reference_To (Standard_String, Loc)));
6716 when RE_Not_Available =>
6720 -------------------------
6721 -- Init_Secondary_Tags --
6722 -------------------------
6724 procedure Init_Secondary_Tags
6727 Stmts_List : List_Id;
6728 Fixed_Comps : Boolean := True;
6729 Variable_Comps : Boolean := True)
6731 Loc : constant Source_Ptr := Sloc (Target);
6733 procedure Inherit_CPP_Tag
6736 Tag_Comp : Entity_Id;
6737 Iface_Tag : Node_Id);
6738 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
6739 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6741 procedure Initialize_Tag
6744 Tag_Comp : Entity_Id;
6745 Iface_Tag : Node_Id);
6746 -- Initialize the tag of the secondary dispatch table of Typ associated
6747 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
6748 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
6749 -- of Typ CPP tagged type we generate code to inherit the contents of
6750 -- the dispatch table directly from the ancestor.
6752 ---------------------
6753 -- Inherit_CPP_Tag --
6754 ---------------------
6756 procedure Inherit_CPP_Tag
6759 Tag_Comp : Entity_Id;
6760 Iface_Tag : Node_Id)
6763 pragma Assert (Is_CPP_Class (Etype (Typ)));
6765 Append_To (Stmts_List,
6766 Build_Inherit_Prims (Loc,
6769 Make_Selected_Component (Loc,
6770 Prefix => New_Copy_Tree (Target),
6771 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
6773 New_Reference_To (Iface_Tag, Loc),
6775 UI_To_Int (DT_Entry_Count (First_Tag_Component (Iface)))));
6776 end Inherit_CPP_Tag;
6778 --------------------
6779 -- Initialize_Tag --
6780 --------------------
6782 procedure Initialize_Tag
6785 Tag_Comp : Entity_Id;
6786 Iface_Tag : Node_Id)
6788 Comp_Typ : Entity_Id;
6789 Offset_To_Top_Comp : Entity_Id := Empty;
6792 -- Initialize the pointer to the secondary DT associated with the
6795 if not Is_Ancestor (Iface, Typ) then
6796 Append_To (Stmts_List,
6797 Make_Assignment_Statement (Loc,
6799 Make_Selected_Component (Loc,
6800 Prefix => New_Copy_Tree (Target),
6801 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
6803 New_Reference_To (Iface_Tag, Loc)));
6806 Comp_Typ := Scope (Tag_Comp);
6808 -- Initialize the entries of the table of interfaces. We generate a
6809 -- different call when the parent of the type has variable size
6812 if Comp_Typ /= Etype (Comp_Typ)
6813 and then Is_Variable_Size_Record (Etype (Comp_Typ))
6814 and then Chars (Tag_Comp) /= Name_uTag
6817 (Present (DT_Offset_To_Top_Func (Tag_Comp)));
6819 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
6820 -- configurable run-time environment.
6822 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
6824 ("variable size record with interface types", Typ);
6829 -- Set_Dynamic_Offset_To_Top
6831 -- Interface_T => Iface'Tag,
6832 -- Offset_Value => n,
6833 -- Offset_Func => Fn'Address)
6835 Append_To (Stmts_List,
6836 Make_Procedure_Call_Statement (Loc,
6837 Name => New_Reference_To
6838 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
6839 Parameter_Associations => New_List (
6840 Make_Attribute_Reference (Loc,
6841 Prefix => New_Copy_Tree (Target),
6842 Attribute_Name => Name_Address),
6844 Unchecked_Convert_To (RTE (RE_Tag),
6846 (Node (First_Elmt (Access_Disp_Table (Iface))),
6849 Unchecked_Convert_To
6850 (RTE (RE_Storage_Offset),
6851 Make_Attribute_Reference (Loc,
6853 Make_Selected_Component (Loc,
6854 Prefix => New_Copy_Tree (Target),
6856 New_Reference_To (Tag_Comp, Loc)),
6857 Attribute_Name => Name_Position)),
6859 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
6860 Make_Attribute_Reference (Loc,
6861 Prefix => New_Reference_To
6862 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
6863 Attribute_Name => Name_Address)))));
6865 -- In this case the next component stores the value of the
6866 -- offset to the top.
6868 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
6869 pragma Assert (Present (Offset_To_Top_Comp));
6871 Append_To (Stmts_List,
6872 Make_Assignment_Statement (Loc,
6874 Make_Selected_Component (Loc,
6875 Prefix => New_Copy_Tree (Target),
6876 Selector_Name => New_Reference_To
6877 (Offset_To_Top_Comp, Loc)),
6879 Make_Attribute_Reference (Loc,
6881 Make_Selected_Component (Loc,
6882 Prefix => New_Copy_Tree (Target),
6884 New_Reference_To (Tag_Comp, Loc)),
6885 Attribute_Name => Name_Position)));
6887 -- Normal case: No discriminants in the parent type
6890 -- Don't need to set any value if this interface shares
6891 -- the primary dispatch table.
6893 if not Is_Ancestor (Iface, Typ) then
6894 Append_To (Stmts_List,
6895 Build_Set_Static_Offset_To_Top (Loc,
6896 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
6898 Unchecked_Convert_To (RTE (RE_Storage_Offset),
6899 Make_Attribute_Reference (Loc,
6901 Make_Selected_Component (Loc,
6902 Prefix => New_Copy_Tree (Target),
6904 New_Reference_To (Tag_Comp, Loc)),
6905 Attribute_Name => Name_Position))));
6909 -- Register_Interface_Offset
6911 -- Interface_T => Iface'Tag,
6912 -- Is_Constant => True,
6913 -- Offset_Value => n,
6914 -- Offset_Func => null);
6916 if RTE_Available (RE_Register_Interface_Offset) then
6917 Append_To (Stmts_List,
6918 Make_Procedure_Call_Statement (Loc,
6919 Name => New_Reference_To
6920 (RTE (RE_Register_Interface_Offset), Loc),
6921 Parameter_Associations => New_List (
6922 Make_Attribute_Reference (Loc,
6923 Prefix => New_Copy_Tree (Target),
6924 Attribute_Name => Name_Address),
6926 Unchecked_Convert_To (RTE (RE_Tag),
6928 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
6930 New_Occurrence_Of (Standard_True, Loc),
6932 Unchecked_Convert_To
6933 (RTE (RE_Storage_Offset),
6934 Make_Attribute_Reference (Loc,
6936 Make_Selected_Component (Loc,
6937 Prefix => New_Copy_Tree (Target),
6939 New_Reference_To (Tag_Comp, Loc)),
6940 Attribute_Name => Name_Position)),
6949 Full_Typ : Entity_Id;
6950 Ifaces_List : Elist_Id;
6951 Ifaces_Comp_List : Elist_Id;
6952 Ifaces_Tag_List : Elist_Id;
6953 Iface_Elmt : Elmt_Id;
6954 Iface_Comp_Elmt : Elmt_Id;
6955 Iface_Tag_Elmt : Elmt_Id;
6957 In_Variable_Pos : Boolean;
6959 -- Start of processing for Init_Secondary_Tags
6962 -- Handle private types
6964 if Present (Full_View (Typ)) then
6965 Full_Typ := Full_View (Typ);
6970 Collect_Interfaces_Info
6971 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
6973 Iface_Elmt := First_Elmt (Ifaces_List);
6974 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
6975 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
6976 while Present (Iface_Elmt) loop
6977 Tag_Comp := Node (Iface_Comp_Elmt);
6979 -- If we are compiling under the CPP full ABI compatibility mode and
6980 -- the ancestor is a CPP_Pragma tagged type then we generate code to
6981 -- inherit the contents of the dispatch table directly from the
6984 if Is_CPP_Class (Etype (Full_Typ)) then
6985 Inherit_CPP_Tag (Full_Typ,
6986 Iface => Node (Iface_Elmt),
6987 Tag_Comp => Tag_Comp,
6988 Iface_Tag => Node (Iface_Tag_Elmt));
6990 -- Otherwise generate code to initialize the tag
6993 -- Check if the parent of the record type has variable size
6996 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
6997 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
6999 if (In_Variable_Pos and then Variable_Comps)
7000 or else (not In_Variable_Pos and then Fixed_Comps)
7002 Initialize_Tag (Full_Typ,
7003 Iface => Node (Iface_Elmt),
7004 Tag_Comp => Tag_Comp,
7005 Iface_Tag => Node (Iface_Tag_Elmt));
7009 Next_Elmt (Iface_Elmt);
7010 Next_Elmt (Iface_Comp_Elmt);
7011 Next_Elmt (Iface_Tag_Elmt);
7013 end Init_Secondary_Tags;
7015 -----------------------------
7016 -- Is_Variable_Size_Record --
7017 -----------------------------
7019 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7021 Comp_Typ : Entity_Id;
7024 function Is_Constant_Bound (Exp : Node_Id) return Boolean;
7025 -- To simplify handling of array components. Determines whether the
7026 -- given bound is constant (a constant or enumeration literal, or an
7027 -- integer literal) as opposed to per-object, through an expression
7028 -- or a discriminant.
7030 -----------------------
7031 -- Is_Constant_Bound --
7032 -----------------------
7034 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
7036 if Nkind (Exp) = N_Integer_Literal then
7040 Is_Entity_Name (Exp)
7041 and then Present (Entity (Exp))
7043 (Ekind (Entity (Exp)) = E_Constant
7044 or else Ekind (Entity (Exp)) = E_Enumeration_Literal);
7046 end Is_Constant_Bound;
7048 -- Start of processing for Is_Variable_Sized_Record
7051 pragma Assert (Is_Record_Type (E));
7053 Comp := First_Entity (E);
7054 while Present (Comp) loop
7055 Comp_Typ := Etype (Comp);
7057 if Is_Record_Type (Comp_Typ) then
7059 -- Recursive call if the record type has discriminants
7061 if Has_Discriminants (Comp_Typ)
7062 and then Is_Variable_Size_Record (Comp_Typ)
7067 elsif Is_Array_Type (Comp_Typ) then
7069 -- Check if some index is initialized with a non-constant value
7071 Idx := First_Index (Comp_Typ);
7072 while Present (Idx) loop
7073 if Nkind (Idx) = N_Range then
7074 if not Is_Constant_Bound (Low_Bound (Idx))
7076 not Is_Constant_Bound (High_Bound (Idx))
7082 Idx := Next_Index (Idx);
7090 end Is_Variable_Size_Record;
7092 ----------------------------------------
7093 -- Make_Controlling_Function_Wrappers --
7094 ----------------------------------------
7096 procedure Make_Controlling_Function_Wrappers
7097 (Tag_Typ : Entity_Id;
7098 Decl_List : out List_Id;
7099 Body_List : out List_Id)
7101 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7102 Prim_Elmt : Elmt_Id;
7104 Actual_List : List_Id;
7105 Formal_List : List_Id;
7107 Par_Formal : Entity_Id;
7108 Formal_Node : Node_Id;
7109 Func_Body : Node_Id;
7110 Func_Decl : Node_Id;
7111 Func_Spec : Node_Id;
7112 Return_Stmt : Node_Id;
7115 Decl_List := New_List;
7116 Body_List := New_List;
7118 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7120 while Present (Prim_Elmt) loop
7121 Subp := Node (Prim_Elmt);
7123 -- If a primitive function with a controlling result of the type has
7124 -- not been overridden by the user, then we must create a wrapper
7125 -- function here that effectively overrides it and invokes the
7126 -- (non-abstract) parent function. This can only occur for a null
7127 -- extension. Note that functions with anonymous controlling access
7128 -- results don't qualify and must be overridden. We also exclude
7129 -- Input attributes, since each type will have its own version of
7130 -- Input constructed by the expander. The test for Comes_From_Source
7131 -- is needed to distinguish inherited operations from renamings
7132 -- (which also have Alias set).
7134 -- The function may be abstract, or require_Overriding may be set
7135 -- for it, because tests for null extensions may already have reset
7136 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7137 -- set, functions that need wrappers are recognized by having an
7138 -- alias that returns the parent type.
7140 if Comes_From_Source (Subp)
7141 or else No (Alias (Subp))
7142 or else Ekind (Subp) /= E_Function
7143 or else not Has_Controlling_Result (Subp)
7144 or else Is_Access_Type (Etype (Subp))
7145 or else Is_Abstract_Subprogram (Alias (Subp))
7146 or else Is_TSS (Subp, TSS_Stream_Input)
7150 elsif Is_Abstract_Subprogram (Subp)
7151 or else Requires_Overriding (Subp)
7153 (Is_Null_Extension (Etype (Subp))
7154 and then Etype (Alias (Subp)) /= Etype (Subp))
7156 Formal_List := No_List;
7157 Formal := First_Formal (Subp);
7159 if Present (Formal) then
7160 Formal_List := New_List;
7162 while Present (Formal) loop
7164 (Make_Parameter_Specification
7166 Defining_Identifier =>
7167 Make_Defining_Identifier (Sloc (Formal),
7168 Chars => Chars (Formal)),
7169 In_Present => In_Present (Parent (Formal)),
7170 Out_Present => Out_Present (Parent (Formal)),
7171 Null_Exclusion_Present =>
7172 Null_Exclusion_Present (Parent (Formal)),
7174 New_Reference_To (Etype (Formal), Loc),
7176 New_Copy_Tree (Expression (Parent (Formal)))),
7179 Next_Formal (Formal);
7184 Make_Function_Specification (Loc,
7185 Defining_Unit_Name =>
7186 Make_Defining_Identifier (Loc,
7187 Chars => Chars (Subp)),
7188 Parameter_Specifications => Formal_List,
7189 Result_Definition =>
7190 New_Reference_To (Etype (Subp), Loc));
7192 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7193 Append_To (Decl_List, Func_Decl);
7195 -- Build a wrapper body that calls the parent function. The body
7196 -- contains a single return statement that returns an extension
7197 -- aggregate whose ancestor part is a call to the parent function,
7198 -- passing the formals as actuals (with any controlling arguments
7199 -- converted to the types of the corresponding formals of the
7200 -- parent function, which might be anonymous access types), and
7201 -- having a null extension.
7203 Formal := First_Formal (Subp);
7204 Par_Formal := First_Formal (Alias (Subp));
7205 Formal_Node := First (Formal_List);
7207 if Present (Formal) then
7208 Actual_List := New_List;
7210 Actual_List := No_List;
7213 while Present (Formal) loop
7214 if Is_Controlling_Formal (Formal) then
7215 Append_To (Actual_List,
7216 Make_Type_Conversion (Loc,
7218 New_Occurrence_Of (Etype (Par_Formal), Loc),
7221 (Defining_Identifier (Formal_Node), Loc)));
7226 (Defining_Identifier (Formal_Node), Loc));
7229 Next_Formal (Formal);
7230 Next_Formal (Par_Formal);
7235 Make_Simple_Return_Statement (Loc,
7237 Make_Extension_Aggregate (Loc,
7239 Make_Function_Call (Loc,
7240 Name => New_Reference_To (Alias (Subp), Loc),
7241 Parameter_Associations => Actual_List),
7242 Null_Record_Present => True));
7245 Make_Subprogram_Body (Loc,
7246 Specification => New_Copy_Tree (Func_Spec),
7247 Declarations => Empty_List,
7248 Handled_Statement_Sequence =>
7249 Make_Handled_Sequence_Of_Statements (Loc,
7250 Statements => New_List (Return_Stmt)));
7252 Set_Defining_Unit_Name
7253 (Specification (Func_Body),
7254 Make_Defining_Identifier (Loc, Chars (Subp)));
7256 Append_To (Body_List, Func_Body);
7258 -- Replace the inherited function with the wrapper function
7259 -- in the primitive operations list.
7261 Override_Dispatching_Operation
7262 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7266 Next_Elmt (Prim_Elmt);
7268 end Make_Controlling_Function_Wrappers;
7274 -- <Make_Eq_If shared components>
7276 -- when V1 => <Make_Eq_Case> on subcomponents
7278 -- when Vn => <Make_Eq_Case> on subcomponents
7281 function Make_Eq_Case
7284 Discr : Entity_Id := Empty) return List_Id
7286 Loc : constant Source_Ptr := Sloc (E);
7287 Result : constant List_Id := New_List;
7292 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7294 if No (Variant_Part (CL)) then
7298 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7300 if No (Variant) then
7304 Alt_List := New_List;
7306 while Present (Variant) loop
7307 Append_To (Alt_List,
7308 Make_Case_Statement_Alternative (Loc,
7309 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7310 Statements => Make_Eq_Case (E, Component_List (Variant))));
7312 Next_Non_Pragma (Variant);
7315 -- If we have an Unchecked_Union, use one of the parameters that
7316 -- captures the discriminants.
7318 if Is_Unchecked_Union (E) then
7320 Make_Case_Statement (Loc,
7321 Expression => New_Reference_To (Discr, Loc),
7322 Alternatives => Alt_List));
7326 Make_Case_Statement (Loc,
7328 Make_Selected_Component (Loc,
7329 Prefix => Make_Identifier (Loc, Name_X),
7330 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7331 Alternatives => Alt_List));
7352 -- or a null statement if the list L is empty
7356 L : List_Id) return Node_Id
7358 Loc : constant Source_Ptr := Sloc (E);
7360 Field_Name : Name_Id;
7365 return Make_Null_Statement (Loc);
7370 C := First_Non_Pragma (L);
7371 while Present (C) loop
7372 Field_Name := Chars (Defining_Identifier (C));
7374 -- The tags must not be compared: they are not part of the value.
7375 -- Ditto for the controller component, if present.
7377 -- Note also that in the following, we use Make_Identifier for
7378 -- the component names. Use of New_Reference_To to identify the
7379 -- components would be incorrect because the wrong entities for
7380 -- discriminants could be picked up in the private type case.
7382 if Field_Name /= Name_uTag
7384 Field_Name /= Name_uController
7386 Evolve_Or_Else (Cond,
7389 Make_Selected_Component (Loc,
7390 Prefix => Make_Identifier (Loc, Name_X),
7392 Make_Identifier (Loc, Field_Name)),
7395 Make_Selected_Component (Loc,
7396 Prefix => Make_Identifier (Loc, Name_Y),
7398 Make_Identifier (Loc, Field_Name))));
7401 Next_Non_Pragma (C);
7405 return Make_Null_Statement (Loc);
7409 Make_Implicit_If_Statement (E,
7411 Then_Statements => New_List (
7412 Make_Simple_Return_Statement (Loc,
7413 Expression => New_Occurrence_Of (Standard_False, Loc))));
7418 -------------------------------
7419 -- Make_Null_Procedure_Specs --
7420 -------------------------------
7422 procedure Make_Null_Procedure_Specs
7423 (Tag_Typ : Entity_Id;
7424 Decl_List : out List_Id)
7426 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7428 Formal_List : List_Id;
7429 Parent_Subp : Entity_Id;
7430 Prim_Elmt : Elmt_Id;
7431 Proc_Spec : Node_Id;
7432 Proc_Decl : Node_Id;
7435 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean;
7436 -- Returns True if E is a null procedure that is an interface primitive
7438 ---------------------------------
7439 -- Is_Null_Interface_Primitive --
7440 ---------------------------------
7442 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
7444 return Comes_From_Source (E)
7445 and then Is_Dispatching_Operation (E)
7446 and then Ekind (E) = E_Procedure
7447 and then Null_Present (Parent (E))
7448 and then Is_Interface (Find_Dispatching_Type (E));
7449 end Is_Null_Interface_Primitive;
7451 -- Start of processing for Make_Null_Procedure_Specs
7454 Decl_List := New_List;
7455 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7456 while Present (Prim_Elmt) loop
7457 Subp := Node (Prim_Elmt);
7459 -- If a null procedure inherited from an interface has not been
7460 -- overridden, then we build a null procedure declaration to
7461 -- override the inherited procedure.
7463 Parent_Subp := Alias (Subp);
7465 if Present (Parent_Subp)
7466 and then Is_Null_Interface_Primitive (Parent_Subp)
7468 Formal_List := No_List;
7469 Formal := First_Formal (Subp);
7471 if Present (Formal) then
7472 Formal_List := New_List;
7474 while Present (Formal) loop
7476 (Make_Parameter_Specification (Loc,
7477 Defining_Identifier =>
7478 Make_Defining_Identifier (Sloc (Formal),
7479 Chars => Chars (Formal)),
7480 In_Present => In_Present (Parent (Formal)),
7481 Out_Present => Out_Present (Parent (Formal)),
7482 Null_Exclusion_Present =>
7483 Null_Exclusion_Present (Parent (Formal)),
7485 New_Reference_To (Etype (Formal), Loc),
7487 New_Copy_Tree (Expression (Parent (Formal)))),
7490 Next_Formal (Formal);
7495 Make_Procedure_Specification (Loc,
7496 Defining_Unit_Name =>
7497 Make_Defining_Identifier (Loc, Chars (Subp)),
7498 Parameter_Specifications => Formal_List);
7499 Set_Null_Present (Proc_Spec);
7501 Proc_Decl := Make_Subprogram_Declaration (Loc, Proc_Spec);
7502 Append_To (Decl_List, Proc_Decl);
7503 Analyze (Proc_Decl);
7506 Next_Elmt (Prim_Elmt);
7508 end Make_Null_Procedure_Specs;
7510 -------------------------------------
7511 -- Make_Predefined_Primitive_Specs --
7512 -------------------------------------
7514 procedure Make_Predefined_Primitive_Specs
7515 (Tag_Typ : Entity_Id;
7516 Predef_List : out List_Id;
7517 Renamed_Eq : out Entity_Id)
7519 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7520 Res : constant List_Id := New_List;
7522 Eq_Needed : Boolean;
7524 Eq_Name : Name_Id := Name_Op_Eq;
7526 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
7527 -- Returns true if Prim is a renaming of an unresolved predefined
7528 -- equality operation.
7530 -------------------------------
7531 -- Is_Predefined_Eq_Renaming --
7532 -------------------------------
7534 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
7536 return Chars (Prim) /= Name_Op_Eq
7537 and then Present (Alias (Prim))
7538 and then Comes_From_Source (Prim)
7539 and then Is_Intrinsic_Subprogram (Alias (Prim))
7540 and then Chars (Alias (Prim)) = Name_Op_Eq;
7541 end Is_Predefined_Eq_Renaming;
7543 -- Start of processing for Make_Predefined_Primitive_Specs
7546 Renamed_Eq := Empty;
7550 Append_To (Res, Predef_Spec_Or_Body (Loc,
7553 Profile => New_List (
7554 Make_Parameter_Specification (Loc,
7555 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7556 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7558 Ret_Type => Standard_Long_Long_Integer));
7560 -- Spec of _Alignment
7562 Append_To (Res, Predef_Spec_Or_Body (Loc,
7564 Name => Name_uAlignment,
7565 Profile => New_List (
7566 Make_Parameter_Specification (Loc,
7567 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7568 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7570 Ret_Type => Standard_Integer));
7572 -- Specs for dispatching stream attributes
7575 Stream_Op_TSS_Names :
7576 constant array (Integer range <>) of TSS_Name_Type :=
7583 for Op in Stream_Op_TSS_Names'Range loop
7584 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
7586 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
7587 Stream_Op_TSS_Names (Op)));
7592 -- Spec of "=" is expanded if the type is not limited and if a
7593 -- user defined "=" was not already declared for the non-full
7594 -- view of a private extension
7596 if not Is_Limited_Type (Tag_Typ) then
7598 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7599 while Present (Prim) loop
7601 -- If a primitive is encountered that renames the predefined
7602 -- equality operator before reaching any explicit equality
7603 -- primitive, then we still need to create a predefined
7604 -- equality function, because calls to it can occur via
7605 -- the renaming. A new name is created for the equality
7606 -- to avoid conflicting with any user-defined equality.
7607 -- (Note that this doesn't account for renamings of
7608 -- equality nested within subpackages???)
7610 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7611 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
7613 -- User-defined equality
7615 elsif Chars (Node (Prim)) = Name_Op_Eq
7616 and then Etype (First_Formal (Node (Prim))) =
7617 Etype (Next_Formal (First_Formal (Node (Prim))))
7618 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
7620 if No (Alias (Node (Prim)))
7621 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
7622 N_Subprogram_Renaming_Declaration
7627 -- If the parent is not an interface type and has an abstract
7628 -- equality function, the inherited equality is abstract as
7629 -- well, and no body can be created for it.
7631 elsif not Is_Interface (Etype (Tag_Typ))
7632 and then Present (Alias (Node (Prim)))
7633 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
7638 -- If the type has an equality function corresponding with
7639 -- a primitive defined in an interface type, the inherited
7640 -- equality is abstract as well, and no body can be created
7643 elsif Present (Alias (Node (Prim)))
7644 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
7647 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
7657 -- If a renaming of predefined equality was found but there was no
7658 -- user-defined equality (so Eq_Needed is still true), then set the
7659 -- name back to Name_Op_Eq. But in the case where a user-defined
7660 -- equality was located after such a renaming, then the predefined
7661 -- equality function is still needed, so Eq_Needed must be set back
7664 if Eq_Name /= Name_Op_Eq then
7666 Eq_Name := Name_Op_Eq;
7673 Eq_Spec := Predef_Spec_Or_Body (Loc,
7676 Profile => New_List (
7677 Make_Parameter_Specification (Loc,
7678 Defining_Identifier =>
7679 Make_Defining_Identifier (Loc, Name_X),
7680 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7681 Make_Parameter_Specification (Loc,
7682 Defining_Identifier =>
7683 Make_Defining_Identifier (Loc, Name_Y),
7684 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
7685 Ret_Type => Standard_Boolean);
7686 Append_To (Res, Eq_Spec);
7688 if Eq_Name /= Name_Op_Eq then
7689 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
7691 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
7692 while Present (Prim) loop
7694 -- Any renamings of equality that appeared before an
7695 -- overriding equality must be updated to refer to the
7696 -- entity for the predefined equality, otherwise calls via
7697 -- the renaming would get incorrectly resolved to call the
7698 -- user-defined equality function.
7700 if Is_Predefined_Eq_Renaming (Node (Prim)) then
7701 Set_Alias (Node (Prim), Renamed_Eq);
7703 -- Exit upon encountering a user-defined equality
7705 elsif Chars (Node (Prim)) = Name_Op_Eq
7706 and then No (Alias (Node (Prim)))
7716 -- Spec for dispatching assignment
7718 Append_To (Res, Predef_Spec_Or_Body (Loc,
7720 Name => Name_uAssign,
7721 Profile => New_List (
7722 Make_Parameter_Specification (Loc,
7723 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
7724 Out_Present => True,
7725 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
7727 Make_Parameter_Specification (Loc,
7728 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
7729 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
7732 -- Ada 2005: Generate declarations for the following primitive
7733 -- operations for limited interfaces and synchronized types that
7734 -- implement a limited interface.
7736 -- Disp_Asynchronous_Select
7737 -- Disp_Conditional_Select
7738 -- Disp_Get_Prim_Op_Kind
7741 -- Disp_Timed_Select
7743 -- These operations cannot be implemented on VM targets, so we simply
7744 -- disable their generation in this case. We also disable generation
7745 -- of these bodies if No_Dispatching_Calls is active.
7747 if Ada_Version >= Ada_05
7748 and then VM_Target = No_VM
7749 and then RTE_Available (RE_Select_Specific_Data)
7751 -- These primitives are defined abstract in interface types
7753 if Is_Interface (Tag_Typ)
7754 and then Is_Limited_Record (Tag_Typ)
7757 Make_Abstract_Subprogram_Declaration (Loc,
7759 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
7762 Make_Abstract_Subprogram_Declaration (Loc,
7764 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
7767 Make_Abstract_Subprogram_Declaration (Loc,
7769 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
7772 Make_Abstract_Subprogram_Declaration (Loc,
7774 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
7777 Make_Abstract_Subprogram_Declaration (Loc,
7779 Make_Disp_Requeue_Spec (Tag_Typ)));
7782 Make_Abstract_Subprogram_Declaration (Loc,
7784 Make_Disp_Timed_Select_Spec (Tag_Typ)));
7786 -- If the ancestor is an interface type we declare non-abstract
7787 -- primitives to override the abstract primitives of the interface
7790 elsif (not Is_Interface (Tag_Typ)
7791 and then Is_Interface (Etype (Tag_Typ))
7792 and then Is_Limited_Record (Etype (Tag_Typ)))
7794 (Is_Concurrent_Record_Type (Tag_Typ)
7795 and then Has_Interfaces (Tag_Typ))
7798 Make_Subprogram_Declaration (Loc,
7800 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
7803 Make_Subprogram_Declaration (Loc,
7805 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
7808 Make_Subprogram_Declaration (Loc,
7810 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
7813 Make_Subprogram_Declaration (Loc,
7815 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
7818 Make_Subprogram_Declaration (Loc,
7820 Make_Disp_Requeue_Spec (Tag_Typ)));
7823 Make_Subprogram_Declaration (Loc,
7825 Make_Disp_Timed_Select_Spec (Tag_Typ)));
7829 -- Specs for finalization actions that may be required in case a future
7830 -- extension contain a controlled element. We generate those only for
7831 -- root tagged types where they will get dummy bodies or when the type
7832 -- has controlled components and their body must be generated. It is
7833 -- also impossible to provide those for tagged types defined within
7834 -- s-finimp since it would involve circularity problems
7836 if In_Finalization_Root (Tag_Typ) then
7839 -- We also skip these if finalization is not available
7841 elsif Restriction_Active (No_Finalization) then
7844 elsif Etype (Tag_Typ) = Tag_Typ
7845 or else Needs_Finalization (Tag_Typ)
7847 -- Ada 2005 (AI-251): We must also generate these subprograms if
7848 -- the immediate ancestor is an interface to ensure the correct
7849 -- initialization of its dispatch table.
7851 or else (not Is_Interface (Tag_Typ)
7852 and then Is_Interface (Etype (Tag_Typ)))
7854 -- Ada 205 (AI-251): We must also generate these subprograms if
7855 -- the parent of an nonlimited interface is a limited interface
7857 or else (Is_Interface (Tag_Typ)
7858 and then not Is_Limited_Interface (Tag_Typ)
7859 and then Is_Limited_Interface (Etype (Tag_Typ)))
7861 if not Is_Limited_Type (Tag_Typ) then
7863 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
7866 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
7870 end Make_Predefined_Primitive_Specs;
7872 ---------------------------------
7873 -- Needs_Simple_Initialization --
7874 ---------------------------------
7876 function Needs_Simple_Initialization (T : Entity_Id) return Boolean is
7878 -- Check for private type, in which case test applies to the underlying
7879 -- type of the private type.
7881 if Is_Private_Type (T) then
7883 RT : constant Entity_Id := Underlying_Type (T);
7886 if Present (RT) then
7887 return Needs_Simple_Initialization (RT);
7893 -- Cases needing simple initialization are access types, and, if pragma
7894 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
7897 elsif Is_Access_Type (T)
7898 or else (Init_Or_Norm_Scalars and then (Is_Scalar_Type (T)))
7902 -- If Initialize/Normalize_Scalars is in effect, string objects also
7903 -- need initialization, unless they are created in the course of
7904 -- expanding an aggregate (since in the latter case they will be
7905 -- filled with appropriate initializing values before they are used).
7907 elsif Init_Or_Norm_Scalars
7909 (Root_Type (T) = Standard_String
7910 or else Root_Type (T) = Standard_Wide_String
7911 or else Root_Type (T) = Standard_Wide_Wide_String)
7914 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
7921 end Needs_Simple_Initialization;
7923 ----------------------
7924 -- Predef_Deep_Spec --
7925 ----------------------
7927 function Predef_Deep_Spec
7929 Tag_Typ : Entity_Id;
7930 Name : TSS_Name_Type;
7931 For_Body : Boolean := False) return Node_Id
7937 if Name = TSS_Deep_Finalize then
7939 Type_B := Standard_Boolean;
7943 Make_Parameter_Specification (Loc,
7944 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
7946 Out_Present => True,
7948 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
7949 Type_B := Standard_Short_Short_Integer;
7953 Make_Parameter_Specification (Loc,
7954 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
7956 Out_Present => True,
7957 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
7960 Make_Parameter_Specification (Loc,
7961 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
7962 Parameter_Type => New_Reference_To (Type_B, Loc)));
7964 return Predef_Spec_Or_Body (Loc,
7965 Name => Make_TSS_Name (Tag_Typ, Name),
7968 For_Body => For_Body);
7971 when RE_Not_Available =>
7973 end Predef_Deep_Spec;
7975 -------------------------
7976 -- Predef_Spec_Or_Body --
7977 -------------------------
7979 function Predef_Spec_Or_Body
7981 Tag_Typ : Entity_Id;
7984 Ret_Type : Entity_Id := Empty;
7985 For_Body : Boolean := False) return Node_Id
7987 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
7991 Set_Is_Public (Id, Is_Public (Tag_Typ));
7993 -- The internal flag is set to mark these declarations because they have
7994 -- specific properties. First, they are primitives even if they are not
7995 -- defined in the type scope (the freezing point is not necessarily in
7996 -- the same scope). Second, the predefined equality can be overridden by
7997 -- a user-defined equality, no body will be generated in this case.
7999 Set_Is_Internal (Id);
8001 if not Debug_Generated_Code then
8002 Set_Debug_Info_Off (Id);
8005 if No (Ret_Type) then
8007 Make_Procedure_Specification (Loc,
8008 Defining_Unit_Name => Id,
8009 Parameter_Specifications => Profile);
8012 Make_Function_Specification (Loc,
8013 Defining_Unit_Name => Id,
8014 Parameter_Specifications => Profile,
8015 Result_Definition =>
8016 New_Reference_To (Ret_Type, Loc));
8019 if Is_Interface (Tag_Typ) then
8020 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8022 -- If body case, return empty subprogram body. Note that this is ill-
8023 -- formed, because there is not even a null statement, and certainly not
8024 -- a return in the function case. The caller is expected to do surgery
8025 -- on the body to add the appropriate stuff.
8028 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8030 -- For the case of an Input attribute predefined for an abstract type,
8031 -- generate an abstract specification. This will never be called, but we
8032 -- need the slot allocated in the dispatching table so that attributes
8033 -- typ'Class'Input and typ'Class'Output will work properly.
8035 elsif Is_TSS (Name, TSS_Stream_Input)
8036 and then Is_Abstract_Type (Tag_Typ)
8038 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8040 -- Normal spec case, where we return a subprogram declaration
8043 return Make_Subprogram_Declaration (Loc, Spec);
8045 end Predef_Spec_Or_Body;
8047 -----------------------------
8048 -- Predef_Stream_Attr_Spec --
8049 -----------------------------
8051 function Predef_Stream_Attr_Spec
8053 Tag_Typ : Entity_Id;
8054 Name : TSS_Name_Type;
8055 For_Body : Boolean := False) return Node_Id
8057 Ret_Type : Entity_Id;
8060 if Name = TSS_Stream_Input then
8061 Ret_Type := Tag_Typ;
8066 return Predef_Spec_Or_Body (Loc,
8067 Name => Make_TSS_Name (Tag_Typ, Name),
8069 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8070 Ret_Type => Ret_Type,
8071 For_Body => For_Body);
8072 end Predef_Stream_Attr_Spec;
8074 ---------------------------------
8075 -- Predefined_Primitive_Bodies --
8076 ---------------------------------
8078 function Predefined_Primitive_Bodies
8079 (Tag_Typ : Entity_Id;
8080 Renamed_Eq : Entity_Id) return List_Id
8082 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8083 Res : constant List_Id := New_List;
8086 Eq_Needed : Boolean;
8090 pragma Warnings (Off, Ent);
8093 pragma Assert (not Is_Interface (Tag_Typ));
8095 -- See if we have a predefined "=" operator
8097 if Present (Renamed_Eq) then
8099 Eq_Name := Chars (Renamed_Eq);
8101 -- If the parent is an interface type then it has defined all the
8102 -- predefined primitives abstract and we need to check if the type
8103 -- has some user defined "=" function to avoid generating it.
8105 elsif Is_Interface (Etype (Tag_Typ)) then
8107 Eq_Name := Name_Op_Eq;
8109 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8110 while Present (Prim) loop
8111 if Chars (Node (Prim)) = Name_Op_Eq
8112 and then not Is_Internal (Node (Prim))
8126 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8127 while Present (Prim) loop
8128 if Chars (Node (Prim)) = Name_Op_Eq
8129 and then Is_Internal (Node (Prim))
8132 Eq_Name := Name_Op_Eq;
8140 -- Body of _Alignment
8142 Decl := Predef_Spec_Or_Body (Loc,
8144 Name => Name_uAlignment,
8145 Profile => New_List (
8146 Make_Parameter_Specification (Loc,
8147 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8148 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8150 Ret_Type => Standard_Integer,
8153 Set_Handled_Statement_Sequence (Decl,
8154 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8155 Make_Simple_Return_Statement (Loc,
8157 Make_Attribute_Reference (Loc,
8158 Prefix => Make_Identifier (Loc, Name_X),
8159 Attribute_Name => Name_Alignment)))));
8161 Append_To (Res, Decl);
8165 Decl := Predef_Spec_Or_Body (Loc,
8168 Profile => New_List (
8169 Make_Parameter_Specification (Loc,
8170 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8171 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8173 Ret_Type => Standard_Long_Long_Integer,
8176 Set_Handled_Statement_Sequence (Decl,
8177 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8178 Make_Simple_Return_Statement (Loc,
8180 Make_Attribute_Reference (Loc,
8181 Prefix => Make_Identifier (Loc, Name_X),
8182 Attribute_Name => Name_Size)))));
8184 Append_To (Res, Decl);
8186 -- Bodies for Dispatching stream IO routines. We need these only for
8187 -- non-limited types (in the limited case there is no dispatching).
8188 -- We also skip them if dispatching or finalization are not available.
8190 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8191 and then No (TSS (Tag_Typ, TSS_Stream_Read))
8193 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8194 Append_To (Res, Decl);
8197 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8198 and then No (TSS (Tag_Typ, TSS_Stream_Write))
8200 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8201 Append_To (Res, Decl);
8204 -- Skip body of _Input for the abstract case, since the corresponding
8205 -- spec is abstract (see Predef_Spec_Or_Body).
8207 if not Is_Abstract_Type (Tag_Typ)
8208 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8209 and then No (TSS (Tag_Typ, TSS_Stream_Input))
8211 Build_Record_Or_Elementary_Input_Function
8212 (Loc, Tag_Typ, Decl, Ent);
8213 Append_To (Res, Decl);
8216 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8217 and then No (TSS (Tag_Typ, TSS_Stream_Output))
8219 Build_Record_Or_Elementary_Output_Procedure
8220 (Loc, Tag_Typ, Decl, Ent);
8221 Append_To (Res, Decl);
8224 -- Ada 2005: Generate bodies for the following primitive operations for
8225 -- limited interfaces and synchronized types that implement a limited
8228 -- disp_asynchronous_select
8229 -- disp_conditional_select
8230 -- disp_get_prim_op_kind
8232 -- disp_timed_select
8234 -- The interface versions will have null bodies
8236 -- These operations cannot be implemented on VM targets, so we simply
8237 -- disable their generation in this case. We also disable generation
8238 -- of these bodies if No_Dispatching_Calls is active.
8240 if Ada_Version >= Ada_05
8241 and then VM_Target = No_VM
8242 and then not Restriction_Active (No_Dispatching_Calls)
8243 and then not Is_Interface (Tag_Typ)
8245 ((Is_Interface (Etype (Tag_Typ))
8246 and then Is_Limited_Record (Etype (Tag_Typ)))
8247 or else (Is_Concurrent_Record_Type (Tag_Typ)
8248 and then Has_Interfaces (Tag_Typ)))
8249 and then RTE_Available (RE_Select_Specific_Data)
8251 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8252 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
8253 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
8254 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
8255 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
8256 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
8259 if not Is_Limited_Type (Tag_Typ)
8260 and then not Is_Interface (Tag_Typ)
8262 -- Body for equality
8266 Predef_Spec_Or_Body (Loc,
8269 Profile => New_List (
8270 Make_Parameter_Specification (Loc,
8271 Defining_Identifier =>
8272 Make_Defining_Identifier (Loc, Name_X),
8273 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8275 Make_Parameter_Specification (Loc,
8276 Defining_Identifier =>
8277 Make_Defining_Identifier (Loc, Name_Y),
8278 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8280 Ret_Type => Standard_Boolean,
8284 Def : constant Node_Id := Parent (Tag_Typ);
8285 Stmts : constant List_Id := New_List;
8286 Variant_Case : Boolean := Has_Discriminants (Tag_Typ);
8287 Comps : Node_Id := Empty;
8288 Typ_Def : Node_Id := Type_Definition (Def);
8291 if Variant_Case then
8292 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8293 Typ_Def := Record_Extension_Part (Typ_Def);
8296 if Present (Typ_Def) then
8297 Comps := Component_List (Typ_Def);
8300 Variant_Case := Present (Comps)
8301 and then Present (Variant_Part (Comps));
8304 if Variant_Case then
8306 Make_Eq_If (Tag_Typ, Discriminant_Specifications (Def)));
8307 Append_List_To (Stmts, Make_Eq_Case (Tag_Typ, Comps));
8309 Make_Simple_Return_Statement (Loc,
8310 Expression => New_Reference_To (Standard_True, Loc)));
8314 Make_Simple_Return_Statement (Loc,
8316 Expand_Record_Equality (Tag_Typ,
8318 Lhs => Make_Identifier (Loc, Name_X),
8319 Rhs => Make_Identifier (Loc, Name_Y),
8320 Bodies => Declarations (Decl))));
8323 Set_Handled_Statement_Sequence (Decl,
8324 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8326 Append_To (Res, Decl);
8329 -- Body for dispatching assignment
8332 Predef_Spec_Or_Body (Loc,
8334 Name => Name_uAssign,
8335 Profile => New_List (
8336 Make_Parameter_Specification (Loc,
8337 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8338 Out_Present => True,
8339 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8341 Make_Parameter_Specification (Loc,
8342 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8343 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8346 Set_Handled_Statement_Sequence (Decl,
8347 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8348 Make_Assignment_Statement (Loc,
8349 Name => Make_Identifier (Loc, Name_X),
8350 Expression => Make_Identifier (Loc, Name_Y)))));
8352 Append_To (Res, Decl);
8355 -- Generate dummy bodies for finalization actions of types that have
8356 -- no controlled components.
8358 -- Skip this processing if we are in the finalization routine in the
8359 -- runtime itself, otherwise we get hopelessly circularly confused!
8361 if In_Finalization_Root (Tag_Typ) then
8364 -- Skip this if finalization is not available
8366 elsif Restriction_Active (No_Finalization) then
8369 elsif (Etype (Tag_Typ) = Tag_Typ
8370 or else Is_Controlled (Tag_Typ)
8372 -- Ada 2005 (AI-251): We must also generate these subprograms
8373 -- if the immediate ancestor of Tag_Typ is an interface to
8374 -- ensure the correct initialization of its dispatch table.
8376 or else (not Is_Interface (Tag_Typ)
8378 Is_Interface (Etype (Tag_Typ))))
8379 and then not Has_Controlled_Component (Tag_Typ)
8381 if not Is_Limited_Type (Tag_Typ) then
8382 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8384 if Is_Controlled (Tag_Typ) then
8385 Set_Handled_Statement_Sequence (Decl,
8386 Make_Handled_Sequence_Of_Statements (Loc,
8388 Ref => Make_Identifier (Loc, Name_V),
8390 Flist_Ref => Make_Identifier (Loc, Name_L),
8391 With_Attach => Make_Identifier (Loc, Name_B))));
8394 Set_Handled_Statement_Sequence (Decl,
8395 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8396 Make_Null_Statement (Loc))));
8399 Append_To (Res, Decl);
8402 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
8404 if Is_Controlled (Tag_Typ) then
8405 Set_Handled_Statement_Sequence (Decl,
8406 Make_Handled_Sequence_Of_Statements (Loc,
8408 Ref => Make_Identifier (Loc, Name_V),
8410 With_Detach => Make_Identifier (Loc, Name_B))));
8413 Set_Handled_Statement_Sequence (Decl,
8414 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8415 Make_Null_Statement (Loc))));
8418 Append_To (Res, Decl);
8422 end Predefined_Primitive_Bodies;
8424 ---------------------------------
8425 -- Predefined_Primitive_Freeze --
8426 ---------------------------------
8428 function Predefined_Primitive_Freeze
8429 (Tag_Typ : Entity_Id) return List_Id
8431 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8432 Res : constant List_Id := New_List;
8437 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8438 while Present (Prim) loop
8439 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8440 Frnodes := Freeze_Entity (Node (Prim), Loc);
8442 if Present (Frnodes) then
8443 Append_List_To (Res, Frnodes);
8451 end Predefined_Primitive_Freeze;
8453 -------------------------
8454 -- Stream_Operation_OK --
8455 -------------------------
8457 function Stream_Operation_OK
8459 Operation : TSS_Name_Type) return Boolean
8461 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
8464 -- Special case of a limited type extension: a default implementation
8465 -- of the stream attributes Read or Write exists if that attribute
8466 -- has been specified or is available for an ancestor type; a default
8467 -- implementation of the attribute Output (resp. Input) exists if the
8468 -- attribute has been specified or Write (resp. Read) is available for
8469 -- an ancestor type. The last condition only applies under Ada 2005.
8471 if Is_Limited_Type (Typ)
8472 and then Is_Tagged_Type (Typ)
8474 if Operation = TSS_Stream_Read then
8475 Has_Predefined_Or_Specified_Stream_Attribute :=
8476 Has_Specified_Stream_Read (Typ);
8478 elsif Operation = TSS_Stream_Write then
8479 Has_Predefined_Or_Specified_Stream_Attribute :=
8480 Has_Specified_Stream_Write (Typ);
8482 elsif Operation = TSS_Stream_Input then
8483 Has_Predefined_Or_Specified_Stream_Attribute :=
8484 Has_Specified_Stream_Input (Typ)
8486 (Ada_Version >= Ada_05
8487 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
8489 elsif Operation = TSS_Stream_Output then
8490 Has_Predefined_Or_Specified_Stream_Attribute :=
8491 Has_Specified_Stream_Output (Typ)
8493 (Ada_Version >= Ada_05
8494 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
8497 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
8499 if not Has_Predefined_Or_Specified_Stream_Attribute
8500 and then Is_Derived_Type (Typ)
8501 and then (Operation = TSS_Stream_Read
8502 or else Operation = TSS_Stream_Write)
8504 Has_Predefined_Or_Specified_Stream_Attribute :=
8506 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
8510 -- If the type is not limited, or else is limited but the attribute is
8511 -- explicitly specified or is predefined for the type, then return True,
8512 -- unless other conditions prevail, such as restrictions prohibiting
8513 -- streams or dispatching operations.
8515 -- We exclude the Input operation from being a predefined subprogram in
8516 -- the case where the associated type is an abstract extension, because
8517 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
8518 -- we don't want an abstract version created because types derived from
8519 -- the abstract type may not even have Input available (for example if
8520 -- derived from a private view of the abstract type that doesn't have
8521 -- a visible Input), but a VM such as .NET or the Java VM can treat the
8522 -- operation as inherited anyway, and we don't want an abstract function
8523 -- to be (implicitly) inherited in that case because it can lead to a VM
8526 return (not Is_Limited_Type (Typ)
8527 or else Has_Predefined_Or_Specified_Stream_Attribute)
8528 and then (Operation /= TSS_Stream_Input
8529 or else not Is_Abstract_Type (Typ)
8530 or else not Is_Derived_Type (Typ))
8531 and then not Has_Unknown_Discriminants (Typ)
8532 and then not (Is_Interface (Typ)
8533 and then (Is_Task_Interface (Typ)
8534 or else Is_Protected_Interface (Typ)
8535 or else Is_Synchronized_Interface (Typ)))
8536 and then not Restriction_Active (No_Streams)
8537 and then not Restriction_Active (No_Dispatch)
8538 and then not No_Run_Time_Mode
8539 and then RTE_Available (RE_Tag)
8540 and then RTE_Available (RE_Root_Stream_Type);
8541 end Stream_Operation_OK;