1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Smem; use Exp_Smem;
41 with Exp_Strm; use Exp_Strm;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Attr; use Sem_Attr;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Mech; use Sem_Mech;
62 with Sem_Res; use Sem_Res;
63 with Sem_SCIL; use Sem_SCIL;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sinfo; use Sinfo;
67 with Stand; use Stand;
68 with Snames; use Snames;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Validsw; use Validsw;
74 package body Exp_Ch3 is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
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_Array_Invariant_Proc
93 Nod : Node_Id) return Node_Id;
94 -- If the component of type of array type has invariants, build procedure
95 -- that checks invariant on all components of the array. Ada 2012 specifies
96 -- that an invariant on some type T must be applied to in-out parameters
97 -- and return values that include a part of type T. If the array type has
98 -- an otherwise specified invariant, the component check procedure is
99 -- called from within the user-specified invariant. Otherwise this becomes
100 -- the invariant procedure for the array type.
102 function Build_Record_Invariant_Proc
104 Nod : Node_Id) return Node_Id;
105 -- Ditto for record types.
107 function Build_Discriminant_Formals
109 Use_Dl : Boolean) return List_Id;
110 -- This function uses the discriminants of a type to build a list of
111 -- formal parameters, used in Build_Init_Procedure among other places.
112 -- If the flag Use_Dl is set, the list is built using the already
113 -- defined discriminals of the type, as is the case for concurrent
114 -- types with discriminants. Otherwise new identifiers are created,
115 -- with the source names of the discriminants.
117 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
118 -- This function builds a static aggregate that can serve as the initial
119 -- value for an array type whose bounds are static, and whose component
120 -- type is a composite type that has a static equivalent aggregate.
121 -- The equivalent array aggregate is used both for object initialization
122 -- and for component initialization, when used in the following function.
124 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
125 -- This function builds a static aggregate that can serve as the initial
126 -- value for a record type whose components are scalar and initialized
127 -- with compile-time values, or arrays with similar initialization or
128 -- defaults. When possible, initialization of an object of the type can
129 -- be achieved by using a copy of the aggregate as an initial value, thus
130 -- removing the implicit call that would otherwise constitute elaboration
133 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
134 -- Build record initialization procedure. N is the type declaration
135 -- node, and Rec_Ent is the corresponding entity for the record type.
137 procedure Build_Slice_Assignment (Typ : Entity_Id);
138 -- Build assignment procedure for one-dimensional arrays of controlled
139 -- types. Other array and slice assignments are expanded in-line, but
140 -- the code expansion for controlled components (when control actions
141 -- are active) can lead to very large blocks that GCC3 handles poorly.
143 procedure Build_Untagged_Equality (Typ : Entity_Id);
144 -- AI05-0123: Equality on untagged records composes. This procedure
145 -- builds the equality routine for an untagged record that has components
146 -- of a record type that has user-defined primitive equality operations.
147 -- The resulting operation is a TSS subprogram.
149 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
150 -- Create An Equality function for the non-tagged variant record 'Typ'
151 -- and attach it to the TSS list
153 procedure Check_Stream_Attributes (Typ : Entity_Id);
154 -- Check that if a limited extension has a parent with user-defined stream
155 -- attributes, and does not itself have user-defined stream-attributes,
156 -- then any limited component of the extension also has the corresponding
157 -- user-defined stream attributes.
159 procedure Clean_Task_Names
161 Proc_Id : Entity_Id);
162 -- If an initialization procedure includes calls to generate names
163 -- for task subcomponents, indicate that secondary stack cleanup is
164 -- needed after an initialization. Typ is the component type, and Proc_Id
165 -- the initialization procedure for the enclosing composite type.
167 procedure Expand_Tagged_Root (T : Entity_Id);
168 -- Add a field _Tag at the beginning of the record. This field carries
169 -- the value of the access to the Dispatch table. This procedure is only
170 -- called on root type, the _Tag field being inherited by the descendants.
172 procedure Expand_Freeze_Array_Type (N : Node_Id);
173 -- Freeze an array type. Deals with building the initialization procedure,
174 -- creating the packed array type for a packed array and also with the
175 -- creation of the controlling procedures for the controlled case. The
176 -- argument N is the N_Freeze_Entity node for the type.
178 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
179 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
180 -- of finalizing controlled derivations from the class-wide's root type.
182 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
183 -- Freeze enumeration type with non-standard representation. Builds the
184 -- array and function needed to convert between enumeration pos and
185 -- enumeration representation values. N is the N_Freeze_Entity node
188 procedure Expand_Freeze_Record_Type (N : Node_Id);
189 -- Freeze record type. Builds all necessary discriminant checking
190 -- and other ancillary functions, and builds dispatch tables where
191 -- needed. The argument N is the N_Freeze_Entity node. This processing
192 -- applies only to E_Record_Type entities, not to class wide types,
193 -- record subtypes, or private types.
195 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
196 -- Treat user-defined stream operations as renaming_as_body if the
197 -- subprogram they rename is not frozen when the type is frozen.
199 procedure Insert_Component_Invariant_Checks
203 -- If a composite type has invariants and also has components with defined
204 -- invariants. the component invariant procedure is inserted into the user-
205 -- defined invariant procedure and added to the checks to be performed.
207 procedure Initialization_Warning (E : Entity_Id);
208 -- If static elaboration of the package is requested, indicate
209 -- when a type does meet the conditions for static initialization. If
210 -- E is a type, it has components that have no static initialization.
211 -- if E is an entity, its initial expression is not compile-time known.
213 function Init_Formals (Typ : Entity_Id) return List_Id;
214 -- This function builds the list of formals for an initialization routine.
215 -- The first formal is always _Init with the given type. For task value
216 -- record types and types containing tasks, three additional formals are
219 -- _Master : Master_Id
220 -- _Chain : in out Activation_Chain
221 -- _Task_Name : String
223 -- The caller must append additional entries for discriminants if required.
225 function In_Runtime (E : Entity_Id) return Boolean;
226 -- Check if E is defined in the RTL (in a child of Ada or System). Used
227 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
229 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean;
230 -- Returns true if Prim is a user defined equality function
232 function Make_Eq_Body
234 Eq_Name : Name_Id) return Node_Id;
235 -- Build the body of a primitive equality operation for a tagged record
236 -- type, or in Ada 2012 for any record type that has components with a
237 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
239 function Make_Eq_Case
242 Discr : Entity_Id := Empty) return List_Id;
243 -- Building block for variant record equality. Defined to share the code
244 -- between the tagged and non-tagged case. Given a Component_List node CL,
245 -- it generates an 'if' followed by a 'case' statement that compares all
246 -- components of local temporaries named X and Y (that are declared as
247 -- formals at some upper level). E provides the Sloc to be used for the
248 -- generated code. Discr is used as the case statement switch in the case
249 -- of Unchecked_Union equality.
253 L : List_Id) return Node_Id;
254 -- Building block for variant record equality. Defined to share the code
255 -- between the tagged and non-tagged case. Given the list of components
256 -- (or discriminants) L, it generates a return statement that compares all
257 -- components of local temporaries named X and Y (that are declared as
258 -- formals at some upper level). E provides the Sloc to be used for the
261 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id;
262 -- Search for a renaming of the inequality dispatching primitive of
263 -- this tagged type. If found then build and return the corresponding
264 -- rename-as-body inequality subprogram; otherwise return Empty.
266 procedure Make_Predefined_Primitive_Specs
267 (Tag_Typ : Entity_Id;
268 Predef_List : out List_Id;
269 Renamed_Eq : out Entity_Id);
270 -- Create a list with the specs of the predefined primitive operations.
271 -- For tagged types that are interfaces all these primitives are defined
274 -- The following entries are present for all tagged types, and provide
275 -- the results of the corresponding attribute applied to the object.
276 -- Dispatching is required in general, since the result of the attribute
277 -- will vary with the actual object subtype.
279 -- _size provides result of 'Size attribute
280 -- typSR provides result of 'Read attribute
281 -- typSW provides result of 'Write attribute
282 -- typSI provides result of 'Input attribute
283 -- typSO provides result of 'Output attribute
285 -- The following entries are additionally present for non-limited tagged
286 -- types, and implement additional dispatching operations for predefined
289 -- _equality implements "=" operator
290 -- _assign implements assignment operation
291 -- typDF implements deep finalization
292 -- typDA implements deep adjust
294 -- The latter two are empty procedures unless the type contains some
295 -- controlled components that require finalization actions (the deep
296 -- in the name refers to the fact that the action applies to components).
298 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
299 -- returns the value Empty, or else the defining unit name for the
300 -- predefined equality function in the case where the type has a primitive
301 -- operation that is a renaming of predefined equality (but only if there
302 -- is also an overriding user-defined equality function). The returned
303 -- Renamed_Eq will be passed to the corresponding parameter of
304 -- Predefined_Primitive_Bodies.
306 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
307 -- returns True if there are representation clauses for type T that are not
308 -- inherited. If the result is false, the init_proc and the discriminant
309 -- checking functions of the parent can be reused by a derived type.
311 procedure Make_Controlling_Function_Wrappers
312 (Tag_Typ : Entity_Id;
313 Decl_List : out List_Id;
314 Body_List : out List_Id);
315 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
316 -- associated with inherited functions with controlling results which
317 -- are not overridden. The body of each wrapper function consists solely
318 -- of a return statement whose expression is an extension aggregate
319 -- invoking the inherited subprogram's parent subprogram and extended
320 -- with a null association list.
322 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
323 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
324 -- null procedures inherited from an interface type that have not been
325 -- overridden. Only one null procedure will be created for a given set of
326 -- inherited null procedures with homographic profiles.
328 function Predef_Spec_Or_Body
333 Ret_Type : Entity_Id := Empty;
334 For_Body : Boolean := False) return Node_Id;
335 -- This function generates the appropriate expansion for a predefined
336 -- primitive operation specified by its name, parameter profile and
337 -- return type (Empty means this is a procedure). If For_Body is false,
338 -- then the returned node is a subprogram declaration. If For_Body is
339 -- true, then the returned node is a empty subprogram body containing
340 -- no declarations and no statements.
342 function Predef_Stream_Attr_Spec
345 Name : TSS_Name_Type;
346 For_Body : Boolean := False) return Node_Id;
347 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
348 -- input and output attribute whose specs are constructed in Exp_Strm.
350 function Predef_Deep_Spec
353 Name : TSS_Name_Type;
354 For_Body : Boolean := False) return Node_Id;
355 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
356 -- and _deep_finalize
358 function Predefined_Primitive_Bodies
359 (Tag_Typ : Entity_Id;
360 Renamed_Eq : Entity_Id) return List_Id;
361 -- Create the bodies of the predefined primitives that are described in
362 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
363 -- the defining unit name of the type's predefined equality as returned
364 -- by Make_Predefined_Primitive_Specs.
366 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
367 -- Freeze entities of all predefined primitive operations. This is needed
368 -- because the bodies of these operations do not normally do any freezing.
370 function Stream_Operation_OK
372 Operation : TSS_Name_Type) return Boolean;
373 -- Check whether the named stream operation must be emitted for a given
374 -- type. The rules for inheritance of stream attributes by type extensions
375 -- are enforced by this function. Furthermore, various restrictions prevent
376 -- the generation of these operations, as a useful optimization or for
377 -- certification purposes.
379 --------------------------
380 -- Adjust_Discriminants --
381 --------------------------
383 -- This procedure attempts to define subtypes for discriminants that are
384 -- more restrictive than those declared. Such a replacement is possible if
385 -- we can demonstrate that values outside the restricted range would cause
386 -- constraint errors in any case. The advantage of restricting the
387 -- discriminant types in this way is that the maximum size of the variant
388 -- record can be calculated more conservatively.
390 -- An example of a situation in which we can perform this type of
391 -- restriction is the following:
393 -- subtype B is range 1 .. 10;
394 -- type Q is array (B range <>) of Integer;
396 -- type V (N : Natural) is record
400 -- In this situation, we can restrict the upper bound of N to 10, since
401 -- any larger value would cause a constraint error in any case.
403 -- There are many situations in which such restriction is possible, but
404 -- for now, we just look for cases like the above, where the component
405 -- in question is a one dimensional array whose upper bound is one of
406 -- the record discriminants. Also the component must not be part of
407 -- any variant part, since then the component does not always exist.
409 procedure Adjust_Discriminants (Rtype : Entity_Id) is
410 Loc : constant Source_Ptr := Sloc (Rtype);
427 Comp := First_Component (Rtype);
428 while Present (Comp) loop
430 -- If our parent is a variant, quit, we do not look at components
431 -- that are in variant parts, because they may not always exist.
433 P := Parent (Comp); -- component declaration
434 P := Parent (P); -- component list
436 exit when Nkind (Parent (P)) = N_Variant;
438 -- We are looking for a one dimensional array type
440 Ctyp := Etype (Comp);
442 if not Is_Array_Type (Ctyp)
443 or else Number_Dimensions (Ctyp) > 1
448 -- The lower bound must be constant, and the upper bound is a
449 -- discriminant (which is a discriminant of the current record).
451 Ityp := Etype (First_Index (Ctyp));
452 Lo := Type_Low_Bound (Ityp);
453 Hi := Type_High_Bound (Ityp);
455 if not Compile_Time_Known_Value (Lo)
456 or else Nkind (Hi) /= N_Identifier
457 or else No (Entity (Hi))
458 or else Ekind (Entity (Hi)) /= E_Discriminant
463 -- We have an array with appropriate bounds
465 Loval := Expr_Value (Lo);
466 Discr := Entity (Hi);
467 Dtyp := Etype (Discr);
469 -- See if the discriminant has a known upper bound
471 Dhi := Type_High_Bound (Dtyp);
473 if not Compile_Time_Known_Value (Dhi) then
477 Dhiv := Expr_Value (Dhi);
479 -- See if base type of component array has known upper bound
481 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
483 if not Compile_Time_Known_Value (Ahi) then
487 Ahiv := Expr_Value (Ahi);
489 -- The condition for doing the restriction is that the high bound
490 -- of the discriminant is greater than the low bound of the array,
491 -- and is also greater than the high bound of the base type index.
493 if Dhiv > Loval and then Dhiv > Ahiv then
495 -- We can reset the upper bound of the discriminant type to
496 -- whichever is larger, the low bound of the component, or
497 -- the high bound of the base type array index.
499 -- We build a subtype that is declared as
501 -- subtype Tnn is discr_type range discr_type'First .. max;
503 -- And insert this declaration into the tree. The type of the
504 -- discriminant is then reset to this more restricted subtype.
506 Tnn := Make_Temporary (Loc, 'T');
508 Insert_Action (Declaration_Node (Rtype),
509 Make_Subtype_Declaration (Loc,
510 Defining_Identifier => Tnn,
511 Subtype_Indication =>
512 Make_Subtype_Indication (Loc,
513 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
515 Make_Range_Constraint (Loc,
519 Make_Attribute_Reference (Loc,
520 Attribute_Name => Name_First,
521 Prefix => New_Occurrence_Of (Dtyp, Loc)),
523 Make_Integer_Literal (Loc,
524 Intval => UI_Max (Loval, Ahiv)))))));
526 Set_Etype (Discr, Tnn);
530 Next_Component (Comp);
532 end Adjust_Discriminants;
534 ---------------------------
535 -- Build_Array_Init_Proc --
536 ---------------------------
538 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
539 Comp_Type : constant Entity_Id := Component_Type (A_Type);
540 Body_Stmts : List_Id;
541 Has_Default_Init : Boolean;
542 Index_List : List_Id;
546 function Init_Component return List_Id;
547 -- Create one statement to initialize one array component, designated
548 -- by a full set of indexes.
550 function Init_One_Dimension (N : Int) return List_Id;
551 -- Create loop to initialize one dimension of the array. The single
552 -- statement in the loop body initializes the inner dimensions if any,
553 -- or else the single component. Note that this procedure is called
554 -- recursively, with N being the dimension to be initialized. A call
555 -- with N greater than the number of dimensions simply generates the
556 -- component initialization, terminating the recursion.
562 function Init_Component return List_Id is
567 Make_Indexed_Component (Loc,
568 Prefix => Make_Identifier (Loc, Name_uInit),
569 Expressions => Index_List);
571 if Has_Default_Aspect (A_Type) then
572 Set_Assignment_OK (Comp);
574 Make_Assignment_Statement (Loc,
577 Convert_To (Comp_Type,
578 Default_Aspect_Component_Value (First_Subtype (A_Type)))));
580 elsif Needs_Simple_Initialization (Comp_Type) then
581 Set_Assignment_OK (Comp);
583 Make_Assignment_Statement (Loc,
587 (Comp_Type, Nod, Component_Size (A_Type))));
590 Clean_Task_Names (Comp_Type, Proc_Id);
592 Build_Initialization_Call
593 (Loc, Comp, Comp_Type,
594 In_Init_Proc => True,
595 Enclos_Type => A_Type);
599 ------------------------
600 -- Init_One_Dimension --
601 ------------------------
603 function Init_One_Dimension (N : Int) return List_Id is
607 -- If the component does not need initializing, then there is nothing
608 -- to do here, so we return a null body. This occurs when generating
609 -- the dummy Init_Proc needed for Initialize_Scalars processing.
611 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
612 and then not Needs_Simple_Initialization (Comp_Type)
613 and then not Has_Task (Comp_Type)
614 and then not Has_Default_Aspect (A_Type)
616 return New_List (Make_Null_Statement (Loc));
618 -- If all dimensions dealt with, we simply initialize the component
620 elsif N > Number_Dimensions (A_Type) then
621 return Init_Component;
623 -- Here we generate the required loop
627 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
629 Append (New_Reference_To (Index, Loc), Index_List);
632 Make_Implicit_Loop_Statement (Nod,
635 Make_Iteration_Scheme (Loc,
636 Loop_Parameter_Specification =>
637 Make_Loop_Parameter_Specification (Loc,
638 Defining_Identifier => Index,
639 Discrete_Subtype_Definition =>
640 Make_Attribute_Reference (Loc,
641 Prefix => Make_Identifier (Loc, Name_uInit),
642 Attribute_Name => Name_Range,
643 Expressions => New_List (
644 Make_Integer_Literal (Loc, N))))),
645 Statements => Init_One_Dimension (N + 1)));
647 end Init_One_Dimension;
649 -- Start of processing for Build_Array_Init_Proc
652 -- The init proc is created when analyzing the freeze node for the type,
653 -- but it properly belongs with the array type declaration. However, if
654 -- the freeze node is for a subtype of a type declared in another unit
655 -- it seems preferable to use the freeze node as the source location of
656 -- the init proc. In any case this is preferable for gcov usage, and
657 -- the Sloc is not otherwise used by the compiler.
659 if In_Open_Scopes (Scope (A_Type)) then
660 Loc := Sloc (A_Type);
665 -- Nothing to generate in the following cases:
667 -- 1. Initialization is suppressed for the type
668 -- 2. The type is a value type, in the CIL sense.
669 -- 3. The type has CIL/JVM convention.
670 -- 4. An initialization already exists for the base type
672 if Initialization_Suppressed (A_Type)
673 or else Is_Value_Type (Comp_Type)
674 or else Convention (A_Type) = Convention_CIL
675 or else Convention (A_Type) = Convention_Java
676 or else Present (Base_Init_Proc (A_Type))
681 Index_List := New_List;
683 -- We need an initialization procedure if any of the following is true:
685 -- 1. The component type has an initialization procedure
686 -- 2. The component type needs simple initialization
687 -- 3. Tasks are present
688 -- 4. The type is marked as a public entity
689 -- 5. The array type has a Default_Component_Value aspect
691 -- The reason for the public entity test is to deal properly with the
692 -- Initialize_Scalars pragma. This pragma can be set in the client and
693 -- not in the declaring package, this means the client will make a call
694 -- to the initialization procedure (because one of conditions 1-3 must
695 -- apply in this case), and we must generate a procedure (even if it is
696 -- null) to satisfy the call in this case.
698 -- Exception: do not build an array init_proc for a type whose root
699 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
700 -- is no place to put the code, and in any case we handle initialization
701 -- of such types (in the Initialize_Scalars case, that's the only time
702 -- the issue arises) in a special manner anyway which does not need an
705 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
706 or else Needs_Simple_Initialization (Comp_Type)
707 or else Has_Task (Comp_Type)
708 or else Has_Default_Aspect (A_Type);
711 or else (not Restriction_Active (No_Initialize_Scalars)
712 and then Is_Public (A_Type)
713 and then Root_Type (A_Type) /= Standard_String
714 and then Root_Type (A_Type) /= Standard_Wide_String
715 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
718 Make_Defining_Identifier (Loc,
719 Chars => Make_Init_Proc_Name (A_Type));
721 -- If No_Default_Initialization restriction is active, then we don't
722 -- want to build an init_proc, but we need to mark that an init_proc
723 -- would be needed if this restriction was not active (so that we can
724 -- detect attempts to call it), so set a dummy init_proc in place.
725 -- This is only done though when actual default initialization is
726 -- needed (and not done when only Is_Public is True), since otherwise
727 -- objects such as arrays of scalars could be wrongly flagged as
728 -- violating the restriction.
730 if Restriction_Active (No_Default_Initialization) then
731 if Has_Default_Init then
732 Set_Init_Proc (A_Type, Proc_Id);
738 Body_Stmts := Init_One_Dimension (1);
741 Make_Subprogram_Body (Loc,
743 Make_Procedure_Specification (Loc,
744 Defining_Unit_Name => Proc_Id,
745 Parameter_Specifications => Init_Formals (A_Type)),
746 Declarations => New_List,
747 Handled_Statement_Sequence =>
748 Make_Handled_Sequence_Of_Statements (Loc,
749 Statements => Body_Stmts)));
751 Set_Ekind (Proc_Id, E_Procedure);
752 Set_Is_Public (Proc_Id, Is_Public (A_Type));
753 Set_Is_Internal (Proc_Id);
754 Set_Has_Completion (Proc_Id);
756 if not Debug_Generated_Code then
757 Set_Debug_Info_Off (Proc_Id);
760 -- Set inlined unless controlled stuff or tasks around, in which
761 -- case we do not want to inline, because nested stuff may cause
762 -- difficulties in inter-unit inlining, and furthermore there is
763 -- in any case no point in inlining such complex init procs.
765 if not Has_Task (Proc_Id)
766 and then not Needs_Finalization (Proc_Id)
768 Set_Is_Inlined (Proc_Id);
771 -- Associate Init_Proc with type, and determine if the procedure
772 -- is null (happens because of the Initialize_Scalars pragma case,
773 -- where we have to generate a null procedure in case it is called
774 -- by a client with Initialize_Scalars set). Such procedures have
775 -- to be generated, but do not have to be called, so we mark them
776 -- as null to suppress the call.
778 Set_Init_Proc (A_Type, Proc_Id);
780 if List_Length (Body_Stmts) = 1
782 -- We must skip SCIL nodes because they may have been added to this
783 -- list by Insert_Actions.
785 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
787 Set_Is_Null_Init_Proc (Proc_Id);
790 -- Try to build a static aggregate to statically initialize
791 -- objects of the type. This can only be done for constrained
792 -- one-dimensional arrays with static bounds.
794 Set_Static_Initialization
796 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
799 end Build_Array_Init_Proc;
801 --------------------------------
802 -- Build_Array_Invariant_Proc --
803 --------------------------------
805 function Build_Array_Invariant_Proc
807 Nod : Node_Id) return Node_Id
809 Loc : constant Source_Ptr := Sloc (Nod);
811 Object_Name : constant Name_Id := New_Internal_Name ('I');
812 -- Name for argument of invariant procedure
814 Object_Entity : constant Node_Id :=
815 Make_Defining_Identifier (Loc, Object_Name);
816 -- The procedure declaration entity for the argument
818 Body_Stmts : List_Id;
819 Index_List : List_Id;
823 function Build_Component_Invariant_Call return Node_Id;
824 -- Create one statement to verify invariant on one array component,
825 -- designated by a full set of indexes.
827 function Check_One_Dimension (N : Int) return List_Id;
828 -- Create loop to check on one dimension of the array. The single
829 -- statement in the loop body checks the inner dimensions if any, or
830 -- else a single component. This procedure is called recursively, with
831 -- N being the dimension to be initialized. A call with N greater than
832 -- the number of dimensions generates the component initialization
833 -- and terminates the recursion.
835 ------------------------------------
836 -- Build_Component_Invariant_Call --
837 ------------------------------------
839 function Build_Component_Invariant_Call return Node_Id is
843 Make_Indexed_Component (Loc,
844 Prefix => New_Occurrence_Of (Object_Entity, Loc),
845 Expressions => Index_List);
847 Make_Procedure_Call_Statement (Loc,
850 (Invariant_Procedure (Component_Type (A_Type)), Loc),
851 Parameter_Associations => New_List (Comp));
852 end Build_Component_Invariant_Call;
854 -------------------------
855 -- Check_One_Dimension --
856 -------------------------
858 function Check_One_Dimension (N : Int) return List_Id is
862 -- If all dimensions dealt with, we simply check invariant of the
865 if N > Number_Dimensions (A_Type) then
866 return New_List (Build_Component_Invariant_Call);
868 -- Else generate one loop and recurse
872 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
874 Append (New_Reference_To (Index, Loc), Index_List);
877 Make_Implicit_Loop_Statement (Nod,
880 Make_Iteration_Scheme (Loc,
881 Loop_Parameter_Specification =>
882 Make_Loop_Parameter_Specification (Loc,
883 Defining_Identifier => Index,
884 Discrete_Subtype_Definition =>
885 Make_Attribute_Reference (Loc,
887 New_Occurrence_Of (Object_Entity, Loc),
888 Attribute_Name => Name_Range,
889 Expressions => New_List (
890 Make_Integer_Literal (Loc, N))))),
891 Statements => Check_One_Dimension (N + 1)));
893 end Check_One_Dimension;
895 -- Start of processing for Build_Array_Invariant_Proc
898 Index_List := New_List;
901 Make_Defining_Identifier (Loc,
902 Chars => New_External_Name (Chars (A_Type), "CInvariant"));
904 Body_Stmts := Check_One_Dimension (1);
907 Make_Subprogram_Body (Loc,
909 Make_Procedure_Specification (Loc,
910 Defining_Unit_Name => Proc_Id,
911 Parameter_Specifications => New_List (
912 Make_Parameter_Specification (Loc,
913 Defining_Identifier => Object_Entity,
914 Parameter_Type => New_Occurrence_Of (A_Type, Loc)))),
916 Declarations => Empty_List,
917 Handled_Statement_Sequence =>
918 Make_Handled_Sequence_Of_Statements (Loc,
919 Statements => Body_Stmts));
921 Set_Ekind (Proc_Id, E_Procedure);
922 Set_Is_Public (Proc_Id, Is_Public (A_Type));
923 Set_Is_Internal (Proc_Id);
924 Set_Has_Completion (Proc_Id);
926 if not Debug_Generated_Code then
927 Set_Debug_Info_Off (Proc_Id);
931 end Build_Array_Invariant_Proc;
933 --------------------------------
934 -- Build_Discr_Checking_Funcs --
935 --------------------------------
937 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
940 Enclosing_Func_Id : Entity_Id;
945 function Build_Case_Statement
946 (Case_Id : Entity_Id;
947 Variant : Node_Id) return Node_Id;
948 -- Build a case statement containing only two alternatives. The first
949 -- alternative corresponds exactly to the discrete choices given on the
950 -- variant with contains the components that we are generating the
951 -- checks for. If the discriminant is one of these return False. The
952 -- second alternative is an OTHERS choice that will return True
953 -- indicating the discriminant did not match.
955 function Build_Dcheck_Function
956 (Case_Id : Entity_Id;
957 Variant : Node_Id) return Entity_Id;
958 -- Build the discriminant checking function for a given variant
960 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
961 -- Builds the discriminant checking function for each variant of the
962 -- given variant part of the record type.
964 --------------------------
965 -- Build_Case_Statement --
966 --------------------------
968 function Build_Case_Statement
969 (Case_Id : Entity_Id;
970 Variant : Node_Id) return Node_Id
972 Alt_List : constant List_Id := New_List;
973 Actuals_List : List_Id;
975 Case_Alt_Node : Node_Id;
977 Choice_List : List_Id;
979 Return_Node : Node_Id;
982 Case_Node := New_Node (N_Case_Statement, Loc);
984 -- Replace the discriminant which controls the variant, with the name
985 -- of the formal of the checking function.
987 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
989 Choice := First (Discrete_Choices (Variant));
991 if Nkind (Choice) = N_Others_Choice then
992 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
994 Choice_List := New_Copy_List (Discrete_Choices (Variant));
997 if not Is_Empty_List (Choice_List) then
998 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
999 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1001 -- In case this is a nested variant, we need to return the result
1002 -- of the discriminant checking function for the immediately
1003 -- enclosing variant.
1005 if Present (Enclosing_Func_Id) then
1006 Actuals_List := New_List;
1008 D := First_Discriminant (Rec_Id);
1009 while Present (D) loop
1010 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
1011 Next_Discriminant (D);
1015 Make_Simple_Return_Statement (Loc,
1017 Make_Function_Call (Loc,
1019 New_Reference_To (Enclosing_Func_Id, Loc),
1020 Parameter_Associations =>
1025 Make_Simple_Return_Statement (Loc,
1027 New_Reference_To (Standard_False, Loc));
1030 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1031 Append (Case_Alt_Node, Alt_List);
1034 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1035 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
1036 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1039 Make_Simple_Return_Statement (Loc,
1041 New_Reference_To (Standard_True, Loc));
1043 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1044 Append (Case_Alt_Node, Alt_List);
1046 Set_Alternatives (Case_Node, Alt_List);
1048 end Build_Case_Statement;
1050 ---------------------------
1051 -- Build_Dcheck_Function --
1052 ---------------------------
1054 function Build_Dcheck_Function
1055 (Case_Id : Entity_Id;
1056 Variant : Node_Id) return Entity_Id
1058 Body_Node : Node_Id;
1059 Func_Id : Entity_Id;
1060 Parameter_List : List_Id;
1061 Spec_Node : Node_Id;
1064 Body_Node := New_Node (N_Subprogram_Body, Loc);
1065 Sequence := Sequence + 1;
1068 Make_Defining_Identifier (Loc,
1069 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1071 Spec_Node := New_Node (N_Function_Specification, Loc);
1072 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1074 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1076 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1077 Set_Result_Definition (Spec_Node,
1078 New_Reference_To (Standard_Boolean, Loc));
1079 Set_Specification (Body_Node, Spec_Node);
1080 Set_Declarations (Body_Node, New_List);
1082 Set_Handled_Statement_Sequence (Body_Node,
1083 Make_Handled_Sequence_Of_Statements (Loc,
1084 Statements => New_List (
1085 Build_Case_Statement (Case_Id, Variant))));
1087 Set_Ekind (Func_Id, E_Function);
1088 Set_Mechanism (Func_Id, Default_Mechanism);
1089 Set_Is_Inlined (Func_Id, True);
1090 Set_Is_Pure (Func_Id, True);
1091 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1092 Set_Is_Internal (Func_Id, True);
1094 if not Debug_Generated_Code then
1095 Set_Debug_Info_Off (Func_Id);
1098 Analyze (Body_Node);
1100 Append_Freeze_Action (Rec_Id, Body_Node);
1101 Set_Dcheck_Function (Variant, Func_Id);
1103 end Build_Dcheck_Function;
1105 ----------------------------
1106 -- Build_Dcheck_Functions --
1107 ----------------------------
1109 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1110 Component_List_Node : Node_Id;
1112 Discr_Name : Entity_Id;
1113 Func_Id : Entity_Id;
1115 Saved_Enclosing_Func_Id : Entity_Id;
1118 -- Build the discriminant-checking function for each variant, and
1119 -- label all components of that variant with the function's name.
1120 -- We only Generate a discriminant-checking function when the
1121 -- variant is not empty, to prevent the creation of dead code.
1122 -- The exception to that is when Frontend_Layout_On_Target is set,
1123 -- because the variant record size function generated in package
1124 -- Layout needs to generate calls to all discriminant-checking
1125 -- functions, including those for empty variants.
1127 Discr_Name := Entity (Name (Variant_Part_Node));
1128 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1130 while Present (Variant) loop
1131 Component_List_Node := Component_List (Variant);
1133 if not Null_Present (Component_List_Node)
1134 or else Frontend_Layout_On_Target
1136 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1138 First_Non_Pragma (Component_Items (Component_List_Node));
1140 while Present (Decl) loop
1141 Set_Discriminant_Checking_Func
1142 (Defining_Identifier (Decl), Func_Id);
1144 Next_Non_Pragma (Decl);
1147 if Present (Variant_Part (Component_List_Node)) then
1148 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1149 Enclosing_Func_Id := Func_Id;
1150 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1151 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1155 Next_Non_Pragma (Variant);
1157 end Build_Dcheck_Functions;
1159 -- Start of processing for Build_Discr_Checking_Funcs
1162 -- Only build if not done already
1164 if not Discr_Check_Funcs_Built (N) then
1165 Type_Def := Type_Definition (N);
1167 if Nkind (Type_Def) = N_Record_Definition then
1168 if No (Component_List (Type_Def)) then -- null record.
1171 V := Variant_Part (Component_List (Type_Def));
1174 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1175 if No (Component_List (Record_Extension_Part (Type_Def))) then
1179 (Component_List (Record_Extension_Part (Type_Def)));
1183 Rec_Id := Defining_Identifier (N);
1185 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1187 Enclosing_Func_Id := Empty;
1188 Build_Dcheck_Functions (V);
1191 Set_Discr_Check_Funcs_Built (N);
1193 end Build_Discr_Checking_Funcs;
1195 --------------------------------
1196 -- Build_Discriminant_Formals --
1197 --------------------------------
1199 function Build_Discriminant_Formals
1200 (Rec_Id : Entity_Id;
1201 Use_Dl : Boolean) return List_Id
1203 Loc : Source_Ptr := Sloc (Rec_Id);
1204 Parameter_List : constant List_Id := New_List;
1207 Formal_Type : Entity_Id;
1208 Param_Spec_Node : Node_Id;
1211 if Has_Discriminants (Rec_Id) then
1212 D := First_Discriminant (Rec_Id);
1213 while Present (D) loop
1217 Formal := Discriminal (D);
1218 Formal_Type := Etype (Formal);
1220 Formal := Make_Defining_Identifier (Loc, Chars (D));
1221 Formal_Type := Etype (D);
1225 Make_Parameter_Specification (Loc,
1226 Defining_Identifier => Formal,
1228 New_Reference_To (Formal_Type, Loc));
1229 Append (Param_Spec_Node, Parameter_List);
1230 Next_Discriminant (D);
1234 return Parameter_List;
1235 end Build_Discriminant_Formals;
1237 --------------------------------------
1238 -- Build_Equivalent_Array_Aggregate --
1239 --------------------------------------
1241 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1242 Loc : constant Source_Ptr := Sloc (T);
1243 Comp_Type : constant Entity_Id := Component_Type (T);
1244 Index_Type : constant Entity_Id := Etype (First_Index (T));
1245 Proc : constant Entity_Id := Base_Init_Proc (T);
1251 if not Is_Constrained (T)
1252 or else Number_Dimensions (T) > 1
1255 Initialization_Warning (T);
1259 Lo := Type_Low_Bound (Index_Type);
1260 Hi := Type_High_Bound (Index_Type);
1262 if not Compile_Time_Known_Value (Lo)
1263 or else not Compile_Time_Known_Value (Hi)
1265 Initialization_Warning (T);
1269 if Is_Record_Type (Comp_Type)
1270 and then Present (Base_Init_Proc (Comp_Type))
1272 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1275 Initialization_Warning (T);
1280 Initialization_Warning (T);
1284 Aggr := Make_Aggregate (Loc, No_List, New_List);
1285 Set_Etype (Aggr, T);
1286 Set_Aggregate_Bounds (Aggr,
1288 Low_Bound => New_Copy (Lo),
1289 High_Bound => New_Copy (Hi)));
1290 Set_Parent (Aggr, Parent (Proc));
1292 Append_To (Component_Associations (Aggr),
1293 Make_Component_Association (Loc,
1297 Low_Bound => New_Copy (Lo),
1298 High_Bound => New_Copy (Hi))),
1299 Expression => Expr));
1301 if Static_Array_Aggregate (Aggr) then
1304 Initialization_Warning (T);
1307 end Build_Equivalent_Array_Aggregate;
1309 ---------------------------------------
1310 -- Build_Equivalent_Record_Aggregate --
1311 ---------------------------------------
1313 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1316 Comp_Type : Entity_Id;
1318 -- Start of processing for Build_Equivalent_Record_Aggregate
1321 if not Is_Record_Type (T)
1322 or else Has_Discriminants (T)
1323 or else Is_Limited_Type (T)
1324 or else Has_Non_Standard_Rep (T)
1326 Initialization_Warning (T);
1330 Comp := First_Component (T);
1332 -- A null record needs no warning
1338 while Present (Comp) loop
1340 -- Array components are acceptable if initialized by a positional
1341 -- aggregate with static components.
1343 if Is_Array_Type (Etype (Comp)) then
1344 Comp_Type := Component_Type (Etype (Comp));
1346 if Nkind (Parent (Comp)) /= N_Component_Declaration
1347 or else No (Expression (Parent (Comp)))
1348 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1350 Initialization_Warning (T);
1353 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1355 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1357 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1359 Initialization_Warning (T);
1363 not Static_Array_Aggregate (Expression (Parent (Comp)))
1365 Initialization_Warning (T);
1369 elsif Is_Scalar_Type (Etype (Comp)) then
1370 Comp_Type := Etype (Comp);
1372 if Nkind (Parent (Comp)) /= N_Component_Declaration
1373 or else No (Expression (Parent (Comp)))
1374 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1375 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1377 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1379 Initialization_Warning (T);
1383 -- For now, other types are excluded
1386 Initialization_Warning (T);
1390 Next_Component (Comp);
1393 -- All components have static initialization. Build positional aggregate
1394 -- from the given expressions or defaults.
1396 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1397 Set_Parent (Agg, Parent (T));
1399 Comp := First_Component (T);
1400 while Present (Comp) loop
1402 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1403 Next_Component (Comp);
1406 Analyze_And_Resolve (Agg, T);
1408 end Build_Equivalent_Record_Aggregate;
1410 -------------------------------
1411 -- Build_Initialization_Call --
1412 -------------------------------
1414 -- References to a discriminant inside the record type declaration can
1415 -- appear either in the subtype_indication to constrain a record or an
1416 -- array, or as part of a larger expression given for the initial value
1417 -- of a component. In both of these cases N appears in the record
1418 -- initialization procedure and needs to be replaced by the formal
1419 -- parameter of the initialization procedure which corresponds to that
1422 -- In the example below, references to discriminants D1 and D2 in proc_1
1423 -- are replaced by references to formals with the same name
1426 -- A similar replacement is done for calls to any record initialization
1427 -- procedure for any components that are themselves of a record type.
1429 -- type R (D1, D2 : Integer) is record
1430 -- X : Integer := F * D1;
1431 -- Y : Integer := F * D2;
1434 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1438 -- Out_2.X := F * D1;
1439 -- Out_2.Y := F * D2;
1442 function Build_Initialization_Call
1446 In_Init_Proc : Boolean := False;
1447 Enclos_Type : Entity_Id := Empty;
1448 Discr_Map : Elist_Id := New_Elmt_List;
1449 With_Default_Init : Boolean := False;
1450 Constructor_Ref : Node_Id := Empty) return List_Id
1452 Res : constant List_Id := New_List;
1458 First_Arg : Node_Id;
1459 Full_Init_Type : Entity_Id;
1460 Full_Type : Entity_Id := Typ;
1461 Init_Type : Entity_Id;
1465 pragma Assert (Constructor_Ref = Empty
1466 or else Is_CPP_Constructor_Call (Constructor_Ref));
1468 if No (Constructor_Ref) then
1469 Proc := Base_Init_Proc (Typ);
1471 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1474 pragma Assert (Present (Proc));
1475 Init_Type := Etype (First_Formal (Proc));
1476 Full_Init_Type := Underlying_Type (Init_Type);
1478 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1479 -- is active (in which case we make the call anyway, since in the
1480 -- actual compiled client it may be non null).
1481 -- Also nothing to do for value types.
1483 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1484 or else Is_Value_Type (Typ)
1486 (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1491 -- Go to full view if private type. In the case of successive
1492 -- private derivations, this can require more than one step.
1494 while Is_Private_Type (Full_Type)
1495 and then Present (Full_View (Full_Type))
1497 Full_Type := Full_View (Full_Type);
1500 -- If Typ is derived, the procedure is the initialization procedure for
1501 -- the root type. Wrap the argument in an conversion to make it type
1502 -- honest. Actually it isn't quite type honest, because there can be
1503 -- conflicts of views in the private type case. That is why we set
1504 -- Conversion_OK in the conversion node.
1506 if (Is_Record_Type (Typ)
1507 or else Is_Array_Type (Typ)
1508 or else Is_Private_Type (Typ))
1509 and then Init_Type /= Base_Type (Typ)
1511 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1512 Set_Etype (First_Arg, Init_Type);
1515 First_Arg := Id_Ref;
1518 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1520 -- In the tasks case, add _Master as the value of the _Master parameter
1521 -- and _Chain as the value of the _Chain parameter. At the outer level,
1522 -- these will be variables holding the corresponding values obtained
1523 -- from GNARL. At inner levels, they will be the parameters passed down
1524 -- through the outer routines.
1526 if Has_Task (Full_Type) then
1527 if Restriction_Active (No_Task_Hierarchy) then
1529 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1531 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1534 -- Add _Chain (not done for sequential elaboration policy, see
1535 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
1537 if Partition_Elaboration_Policy /= 'S' then
1538 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1541 -- Ada 2005 (AI-287): In case of default initialized components
1542 -- with tasks, we generate a null string actual parameter.
1543 -- This is just a workaround that must be improved later???
1545 if With_Default_Init then
1547 Make_String_Literal (Loc,
1552 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1553 Decl := Last (Decls);
1556 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1557 Append_List (Decls, Res);
1565 -- Add discriminant values if discriminants are present
1567 if Has_Discriminants (Full_Init_Type) then
1568 Discr := First_Discriminant (Full_Init_Type);
1570 while Present (Discr) loop
1572 -- If this is a discriminated concurrent type, the init_proc
1573 -- for the corresponding record is being called. Use that type
1574 -- directly to find the discriminant value, to handle properly
1575 -- intervening renamed discriminants.
1578 T : Entity_Id := Full_Type;
1581 if Is_Protected_Type (T) then
1582 T := Corresponding_Record_Type (T);
1584 elsif Is_Private_Type (T)
1585 and then Present (Underlying_Full_View (T))
1586 and then Is_Protected_Type (Underlying_Full_View (T))
1588 T := Corresponding_Record_Type (Underlying_Full_View (T));
1592 Get_Discriminant_Value (
1595 Discriminant_Constraint (Full_Type));
1598 -- If the target has access discriminants, and is constrained by
1599 -- an access to the enclosing construct, i.e. a current instance,
1600 -- replace the reference to the type by a reference to the object.
1602 if Nkind (Arg) = N_Attribute_Reference
1603 and then Is_Access_Type (Etype (Arg))
1604 and then Is_Entity_Name (Prefix (Arg))
1605 and then Is_Type (Entity (Prefix (Arg)))
1608 Make_Attribute_Reference (Loc,
1609 Prefix => New_Copy (Prefix (Id_Ref)),
1610 Attribute_Name => Name_Unrestricted_Access);
1612 elsif In_Init_Proc then
1614 -- Replace any possible references to the discriminant in the
1615 -- call to the record initialization procedure with references
1616 -- to the appropriate formal parameter.
1618 if Nkind (Arg) = N_Identifier
1619 and then Ekind (Entity (Arg)) = E_Discriminant
1621 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1623 -- Otherwise make a copy of the default expression. Note that
1624 -- we use the current Sloc for this, because we do not want the
1625 -- call to appear to be at the declaration point. Within the
1626 -- expression, replace discriminants with their discriminals.
1630 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1634 if Is_Constrained (Full_Type) then
1635 Arg := Duplicate_Subexpr_No_Checks (Arg);
1637 -- The constraints come from the discriminant default exps,
1638 -- they must be reevaluated, so we use New_Copy_Tree but we
1639 -- ensure the proper Sloc (for any embedded calls).
1641 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1645 -- Ada 2005 (AI-287): In case of default initialized components,
1646 -- if the component is constrained with a discriminant of the
1647 -- enclosing type, we need to generate the corresponding selected
1648 -- component node to access the discriminant value. In other cases
1649 -- this is not required, either because we are inside the init
1650 -- proc and we use the corresponding formal, or else because the
1651 -- component is constrained by an expression.
1653 if With_Default_Init
1654 and then Nkind (Id_Ref) = N_Selected_Component
1655 and then Nkind (Arg) = N_Identifier
1656 and then Ekind (Entity (Arg)) = E_Discriminant
1659 Make_Selected_Component (Loc,
1660 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1661 Selector_Name => Arg));
1663 Append_To (Args, Arg);
1666 Next_Discriminant (Discr);
1670 -- If this is a call to initialize the parent component of a derived
1671 -- tagged type, indicate that the tag should not be set in the parent.
1673 if Is_Tagged_Type (Full_Init_Type)
1674 and then not Is_CPP_Class (Full_Init_Type)
1675 and then Nkind (Id_Ref) = N_Selected_Component
1676 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1678 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1680 elsif Present (Constructor_Ref) then
1681 Append_List_To (Args,
1682 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1686 Make_Procedure_Call_Statement (Loc,
1687 Name => New_Occurrence_Of (Proc, Loc),
1688 Parameter_Associations => Args));
1690 if Needs_Finalization (Typ)
1691 and then Nkind (Id_Ref) = N_Selected_Component
1693 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1696 (Obj_Ref => New_Copy_Tree (First_Arg),
1701 -- When the object is either protected or a task, create static strings
1702 -- which denote the names of entries and families. Associate the strings
1703 -- with the concurrent object's Protection_Entries or ATCB. This is a
1704 -- VMS Debug feature.
1706 if OpenVMS_On_Target
1707 and then Is_Concurrent_Type (Typ)
1708 and then Entry_Names_OK
1710 Build_Entry_Names (Id_Ref, Typ, Res);
1716 when RE_Not_Available =>
1718 end Build_Initialization_Call;
1720 ----------------------------
1721 -- Build_Record_Init_Proc --
1722 ----------------------------
1724 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1725 Decls : constant List_Id := New_List;
1726 Discr_Map : constant Elist_Id := New_Elmt_List;
1727 Loc : constant Source_Ptr := Sloc (Rec_Ent);
1729 Proc_Id : Entity_Id;
1730 Rec_Type : Entity_Id;
1731 Set_Tag : Entity_Id := Empty;
1733 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1734 -- Build an assignment statement which assigns the default expression
1735 -- to its corresponding record component if defined. The left hand side
1736 -- of the assignment is marked Assignment_OK so that initialization of
1737 -- limited private records works correctly. This routine may also build
1738 -- an adjustment call if the component is controlled.
1740 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1741 -- If the record has discriminants, add assignment statements to
1742 -- Statement_List to initialize the discriminant values from the
1743 -- arguments of the initialization procedure.
1745 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1746 -- Build a list representing a sequence of statements which initialize
1747 -- components of the given component list. This may involve building
1748 -- case statements for the variant parts. Append any locally declared
1749 -- objects on list Decls.
1751 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1752 -- Given a non-tagged type-derivation that declares discriminants,
1755 -- type R (R1, R2 : Integer) is record ... end record;
1757 -- type D (D1 : Integer) is new R (1, D1);
1759 -- we make the _init_proc of D be
1761 -- procedure _init_proc (X : D; D1 : Integer) is
1763 -- _init_proc (R (X), 1, D1);
1766 -- This function builds the call statement in this _init_proc.
1768 procedure Build_CPP_Init_Procedure;
1769 -- Build the tree corresponding to the procedure specification and body
1770 -- of the IC procedure that initializes the C++ part of the dispatch
1771 -- table of an Ada tagged type that is a derivation of a CPP type.
1772 -- Install it as the CPP_Init TSS.
1774 procedure Build_Init_Procedure;
1775 -- Build the tree corresponding to the procedure specification and body
1776 -- of the initialization procedure and install it as the _init TSS.
1778 procedure Build_Offset_To_Top_Functions;
1779 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1780 -- and body of Offset_To_Top, a function used in conjuction with types
1781 -- having secondary dispatch tables.
1783 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1784 -- Add range checks to components of discriminated records. S is a
1785 -- subtype indication of a record component. Check_List is a list
1786 -- to which the check actions are appended.
1788 function Component_Needs_Simple_Initialization
1789 (T : Entity_Id) return Boolean;
1790 -- Determine if a component needs simple initialization, given its type
1791 -- T. This routine is the same as Needs_Simple_Initialization except for
1792 -- components of type Tag and Interface_Tag. These two access types do
1793 -- not require initialization since they are explicitly initialized by
1796 function Parent_Subtype_Renaming_Discrims return Boolean;
1797 -- Returns True for base types N that rename discriminants, else False
1799 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1800 -- Determine whether a record initialization procedure needs to be
1801 -- generated for the given record type.
1803 ----------------------
1804 -- Build_Assignment --
1805 ----------------------
1807 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1808 N_Loc : constant Source_Ptr := Sloc (N);
1809 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1811 Kind : Node_Kind := Nkind (N);
1817 Make_Selected_Component (N_Loc,
1818 Prefix => Make_Identifier (Loc, Name_uInit),
1819 Selector_Name => New_Occurrence_Of (Id, N_Loc));
1820 Set_Assignment_OK (Lhs);
1822 -- Case of an access attribute applied to the current instance.
1823 -- Replace the reference to the type by a reference to the actual
1824 -- object. (Note that this handles the case of the top level of
1825 -- the expression being given by such an attribute, but does not
1826 -- cover uses nested within an initial value expression. Nested
1827 -- uses are unlikely to occur in practice, but are theoretically
1828 -- possible.) It is not clear how to handle them without fully
1829 -- traversing the expression. ???
1831 if Kind = N_Attribute_Reference
1832 and then Nam_In (Attribute_Name (N), Name_Unchecked_Access,
1833 Name_Unrestricted_Access)
1834 and then Is_Entity_Name (Prefix (N))
1835 and then Is_Type (Entity (Prefix (N)))
1836 and then Entity (Prefix (N)) = Rec_Type
1839 Make_Attribute_Reference (N_Loc,
1841 Make_Identifier (N_Loc, Name_uInit),
1842 Attribute_Name => Name_Unrestricted_Access);
1845 -- Take a copy of Exp to ensure that later copies of this component
1846 -- declaration in derived types see the original tree, not a node
1847 -- rewritten during expansion of the init_proc. If the copy contains
1848 -- itypes, the scope of the new itypes is the init_proc being built.
1850 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1853 Make_Assignment_Statement (Loc,
1855 Expression => Exp));
1857 Set_No_Ctrl_Actions (First (Res));
1859 -- Adjust the tag if tagged (because of possible view conversions).
1860 -- Suppress the tag adjustment when VM_Target because VM tags are
1861 -- represented implicitly in objects.
1863 if Is_Tagged_Type (Typ)
1864 and then Tagged_Type_Expansion
1867 Make_Assignment_Statement (N_Loc,
1869 Make_Selected_Component (N_Loc,
1871 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1873 New_Reference_To (First_Tag_Component (Typ), N_Loc)),
1876 Unchecked_Convert_To (RTE (RE_Tag),
1880 (Access_Disp_Table (Underlying_Type (Typ)))),
1884 -- Adjust the component if controlled except if it is an aggregate
1885 -- that will be expanded inline.
1887 if Kind = N_Qualified_Expression then
1888 Kind := Nkind (Expression (N));
1891 if Needs_Finalization (Typ)
1892 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1893 and then not Is_Immutably_Limited_Type (Typ)
1897 (Obj_Ref => New_Copy_Tree (Lhs),
1898 Typ => Etype (Id)));
1904 when RE_Not_Available =>
1906 end Build_Assignment;
1908 ------------------------------------
1909 -- Build_Discriminant_Assignments --
1910 ------------------------------------
1912 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1913 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1918 if Has_Discriminants (Rec_Type)
1919 and then not Is_Unchecked_Union (Rec_Type)
1921 D := First_Discriminant (Rec_Type);
1922 while Present (D) loop
1924 -- Don't generate the assignment for discriminants in derived
1925 -- tagged types if the discriminant is a renaming of some
1926 -- ancestor discriminant. This initialization will be done
1927 -- when initializing the _parent field of the derived record.
1930 and then Present (Corresponding_Discriminant (D))
1936 Append_List_To (Statement_List,
1937 Build_Assignment (D,
1938 New_Reference_To (Discriminal (D), D_Loc)));
1941 Next_Discriminant (D);
1944 end Build_Discriminant_Assignments;
1946 --------------------------
1947 -- Build_Init_Call_Thru --
1948 --------------------------
1950 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1951 Parent_Proc : constant Entity_Id :=
1952 Base_Init_Proc (Etype (Rec_Type));
1954 Parent_Type : constant Entity_Id :=
1955 Etype (First_Formal (Parent_Proc));
1957 Uparent_Type : constant Entity_Id :=
1958 Underlying_Type (Parent_Type);
1960 First_Discr_Param : Node_Id;
1964 First_Arg : Node_Id;
1965 Parent_Discr : Entity_Id;
1969 -- First argument (_Init) is the object to be initialized.
1970 -- ??? not sure where to get a reasonable Loc for First_Arg
1973 OK_Convert_To (Parent_Type,
1974 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1976 Set_Etype (First_Arg, Parent_Type);
1978 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1980 -- In the tasks case,
1981 -- add _Master as the value of the _Master parameter
1982 -- add _Chain as the value of the _Chain parameter.
1983 -- add _Task_Name as the value of the _Task_Name parameter.
1984 -- At the outer level, these will be variables holding the
1985 -- corresponding values obtained from GNARL or the expander.
1987 -- At inner levels, they will be the parameters passed down through
1988 -- the outer routines.
1990 First_Discr_Param := Next (First (Parameters));
1992 if Has_Task (Rec_Type) then
1993 if Restriction_Active (No_Task_Hierarchy) then
1995 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1997 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2000 -- Add _Chain (not done for sequential elaboration policy, see
2001 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
2003 if Partition_Elaboration_Policy /= 'S' then
2004 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2007 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2008 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2011 -- Append discriminant values
2013 if Has_Discriminants (Uparent_Type) then
2014 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2016 Parent_Discr := First_Discriminant (Uparent_Type);
2017 while Present (Parent_Discr) loop
2019 -- Get the initial value for this discriminant
2020 -- ??? needs to be cleaned up to use parent_Discr_Constr
2024 Discr : Entity_Id :=
2025 First_Stored_Discriminant (Uparent_Type);
2027 Discr_Value : Elmt_Id :=
2028 First_Elmt (Stored_Constraint (Rec_Type));
2031 while Original_Record_Component (Parent_Discr) /= Discr loop
2032 Next_Stored_Discriminant (Discr);
2033 Next_Elmt (Discr_Value);
2036 Arg := Node (Discr_Value);
2039 -- Append it to the list
2041 if Nkind (Arg) = N_Identifier
2042 and then Ekind (Entity (Arg)) = E_Discriminant
2045 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2047 -- Case of access discriminants. We replace the reference
2048 -- to the type by a reference to the actual object.
2050 -- Is above comment right??? Use of New_Copy below seems mighty
2054 Append_To (Args, New_Copy (Arg));
2057 Next_Discriminant (Parent_Discr);
2063 Make_Procedure_Call_Statement (Loc,
2065 New_Occurrence_Of (Parent_Proc, Loc),
2066 Parameter_Associations => Args));
2069 end Build_Init_Call_Thru;
2071 -----------------------------------
2072 -- Build_Offset_To_Top_Functions --
2073 -----------------------------------
2075 procedure Build_Offset_To_Top_Functions is
2077 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2079 -- function Fxx (O : Address) return Storage_Offset is
2080 -- type Acc is access all <Typ>;
2082 -- return Acc!(O).Iface_Comp'Position;
2085 ----------------------------------
2086 -- Build_Offset_To_Top_Function --
2087 ----------------------------------
2089 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2090 Body_Node : Node_Id;
2091 Func_Id : Entity_Id;
2092 Spec_Node : Node_Id;
2093 Acc_Type : Entity_Id;
2096 Func_Id := Make_Temporary (Loc, 'F');
2097 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2100 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2102 Spec_Node := New_Node (N_Function_Specification, Loc);
2103 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2104 Set_Parameter_Specifications (Spec_Node, New_List (
2105 Make_Parameter_Specification (Loc,
2106 Defining_Identifier =>
2107 Make_Defining_Identifier (Loc, Name_uO),
2110 New_Reference_To (RTE (RE_Address), Loc))));
2111 Set_Result_Definition (Spec_Node,
2112 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2115 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2117 -- return O.Iface_Comp'Position;
2120 Body_Node := New_Node (N_Subprogram_Body, Loc);
2121 Set_Specification (Body_Node, Spec_Node);
2123 Acc_Type := Make_Temporary (Loc, 'T');
2124 Set_Declarations (Body_Node, New_List (
2125 Make_Full_Type_Declaration (Loc,
2126 Defining_Identifier => Acc_Type,
2128 Make_Access_To_Object_Definition (Loc,
2129 All_Present => True,
2130 Null_Exclusion_Present => False,
2131 Constant_Present => False,
2132 Subtype_Indication =>
2133 New_Reference_To (Rec_Type, Loc)))));
2135 Set_Handled_Statement_Sequence (Body_Node,
2136 Make_Handled_Sequence_Of_Statements (Loc,
2137 Statements => New_List (
2138 Make_Simple_Return_Statement (Loc,
2140 Make_Attribute_Reference (Loc,
2142 Make_Selected_Component (Loc,
2144 Unchecked_Convert_To (Acc_Type,
2145 Make_Identifier (Loc, Name_uO)),
2147 New_Reference_To (Iface_Comp, Loc)),
2148 Attribute_Name => Name_Position)))));
2150 Set_Ekind (Func_Id, E_Function);
2151 Set_Mechanism (Func_Id, Default_Mechanism);
2152 Set_Is_Internal (Func_Id, True);
2154 if not Debug_Generated_Code then
2155 Set_Debug_Info_Off (Func_Id);
2158 Analyze (Body_Node);
2160 Append_Freeze_Action (Rec_Type, Body_Node);
2161 end Build_Offset_To_Top_Function;
2165 Iface_Comp : Node_Id;
2166 Iface_Comp_Elmt : Elmt_Id;
2167 Ifaces_Comp_List : Elist_Id;
2169 -- Start of processing for Build_Offset_To_Top_Functions
2172 -- Offset_To_Top_Functions are built only for derivations of types
2173 -- with discriminants that cover interface types.
2174 -- Nothing is needed either in case of virtual machines, since
2175 -- interfaces are handled directly by the VM.
2177 if not Is_Tagged_Type (Rec_Type)
2178 or else Etype (Rec_Type) = Rec_Type
2179 or else not Has_Discriminants (Etype (Rec_Type))
2180 or else not Tagged_Type_Expansion
2185 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2187 -- For each interface type with secondary dispatch table we generate
2188 -- the Offset_To_Top_Functions (required to displace the pointer in
2189 -- interface conversions)
2191 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2192 while Present (Iface_Comp_Elmt) loop
2193 Iface_Comp := Node (Iface_Comp_Elmt);
2194 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2196 -- If the interface is a parent of Rec_Type it shares the primary
2197 -- dispatch table and hence there is no need to build the function
2199 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2200 Use_Full_View => True)
2202 Build_Offset_To_Top_Function (Iface_Comp);
2205 Next_Elmt (Iface_Comp_Elmt);
2207 end Build_Offset_To_Top_Functions;
2209 ------------------------------
2210 -- Build_CPP_Init_Procedure --
2211 ------------------------------
2213 procedure Build_CPP_Init_Procedure is
2214 Body_Node : Node_Id;
2215 Body_Stmts : List_Id;
2216 Flag_Id : Entity_Id;
2217 Flag_Decl : Node_Id;
2218 Handled_Stmt_Node : Node_Id;
2219 Init_Tags_List : List_Id;
2220 Proc_Id : Entity_Id;
2221 Proc_Spec_Node : Node_Id;
2224 -- Check cases requiring no IC routine
2226 if not Is_CPP_Class (Root_Type (Rec_Type))
2227 or else Is_CPP_Class (Rec_Type)
2228 or else CPP_Num_Prims (Rec_Type) = 0
2229 or else not Tagged_Type_Expansion
2230 or else No_Run_Time_Mode
2237 -- Flag : Boolean := False;
2239 -- procedure Typ_IC is
2242 -- Copy C++ dispatch table slots from parent
2243 -- Update C++ slots of overridden primitives
2247 Flag_Id := Make_Temporary (Loc, 'F');
2250 Make_Object_Declaration (Loc,
2251 Defining_Identifier => Flag_Id,
2252 Object_Definition =>
2253 New_Reference_To (Standard_Boolean, Loc),
2255 New_Reference_To (Standard_True, Loc));
2257 Analyze (Flag_Decl);
2258 Append_Freeze_Action (Rec_Type, Flag_Decl);
2260 Body_Stmts := New_List;
2261 Body_Node := New_Node (N_Subprogram_Body, Loc);
2263 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2266 Make_Defining_Identifier (Loc,
2267 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2269 Set_Ekind (Proc_Id, E_Procedure);
2270 Set_Is_Internal (Proc_Id);
2272 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2274 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2275 Set_Specification (Body_Node, Proc_Spec_Node);
2276 Set_Declarations (Body_Node, New_List);
2278 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2280 Append_To (Init_Tags_List,
2281 Make_Assignment_Statement (Loc,
2283 New_Reference_To (Flag_Id, Loc),
2285 New_Reference_To (Standard_False, Loc)));
2287 Append_To (Body_Stmts,
2288 Make_If_Statement (Loc,
2289 Condition => New_Occurrence_Of (Flag_Id, Loc),
2290 Then_Statements => Init_Tags_List));
2292 Handled_Stmt_Node :=
2293 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2294 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2295 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2296 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2298 if not Debug_Generated_Code then
2299 Set_Debug_Info_Off (Proc_Id);
2302 -- Associate CPP_Init_Proc with type
2304 Set_Init_Proc (Rec_Type, Proc_Id);
2305 end Build_CPP_Init_Procedure;
2307 --------------------------
2308 -- Build_Init_Procedure --
2309 --------------------------
2311 procedure Build_Init_Procedure is
2312 Body_Stmts : List_Id;
2313 Body_Node : Node_Id;
2314 Handled_Stmt_Node : Node_Id;
2315 Init_Tags_List : List_Id;
2316 Parameters : List_Id;
2317 Proc_Spec_Node : Node_Id;
2318 Record_Extension_Node : Node_Id;
2321 Body_Stmts := New_List;
2322 Body_Node := New_Node (N_Subprogram_Body, Loc);
2323 Set_Ekind (Proc_Id, E_Procedure);
2325 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2326 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2328 Parameters := Init_Formals (Rec_Type);
2329 Append_List_To (Parameters,
2330 Build_Discriminant_Formals (Rec_Type, True));
2332 -- For tagged types, we add a flag to indicate whether the routine
2333 -- is called to initialize a parent component in the init_proc of
2334 -- a type extension. If the flag is false, we do not set the tag
2335 -- because it has been set already in the extension.
2337 if Is_Tagged_Type (Rec_Type) then
2338 Set_Tag := Make_Temporary (Loc, 'P');
2340 Append_To (Parameters,
2341 Make_Parameter_Specification (Loc,
2342 Defining_Identifier => Set_Tag,
2344 New_Occurrence_Of (Standard_Boolean, Loc),
2346 New_Occurrence_Of (Standard_True, Loc)));
2349 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2350 Set_Specification (Body_Node, Proc_Spec_Node);
2351 Set_Declarations (Body_Node, Decls);
2353 -- N is a Derived_Type_Definition that renames the parameters of the
2354 -- ancestor type. We initialize it by expanding our discriminants and
2355 -- call the ancestor _init_proc with a type-converted object.
2357 if Parent_Subtype_Renaming_Discrims then
2358 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2360 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2361 Build_Discriminant_Assignments (Body_Stmts);
2363 if not Null_Present (Type_Definition (N)) then
2364 Append_List_To (Body_Stmts,
2365 Build_Init_Statements (
2366 Component_List (Type_Definition (N))));
2369 -- N is a Derived_Type_Definition with a possible non-empty
2370 -- extension. The initialization of a type extension consists in the
2371 -- initialization of the components in the extension.
2374 Build_Discriminant_Assignments (Body_Stmts);
2376 Record_Extension_Node :=
2377 Record_Extension_Part (Type_Definition (N));
2379 if not Null_Present (Record_Extension_Node) then
2381 Stmts : constant List_Id :=
2382 Build_Init_Statements (
2383 Component_List (Record_Extension_Node));
2386 -- The parent field must be initialized first because
2387 -- the offset of the new discriminants may depend on it
2389 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2390 Append_List_To (Body_Stmts, Stmts);
2395 -- Add here the assignment to instantiate the Tag
2397 -- The assignment corresponds to the code:
2399 -- _Init._Tag := Typ'Tag;
2401 -- Suppress the tag assignment when VM_Target because VM tags are
2402 -- represented implicitly in objects. It is also suppressed in case
2403 -- of CPP_Class types because in this case the tag is initialized in
2406 if Is_Tagged_Type (Rec_Type)
2407 and then Tagged_Type_Expansion
2408 and then not No_Run_Time_Mode
2410 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2411 -- the actual object and invoke the IP of the parent (in this
2412 -- order). The tag must be initialized before the call to the IP
2413 -- of the parent and the assignments to other components because
2414 -- the initial value of the components may depend on the tag (eg.
2415 -- through a dispatching operation on an access to the current
2416 -- type). The tag assignment is not done when initializing the
2417 -- parent component of a type extension, because in that case the
2418 -- tag is set in the extension.
2420 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2422 -- Initialize the primary tag component
2424 Init_Tags_List := New_List (
2425 Make_Assignment_Statement (Loc,
2427 Make_Selected_Component (Loc,
2428 Prefix => Make_Identifier (Loc, Name_uInit),
2431 (First_Tag_Component (Rec_Type), Loc)),
2435 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2437 -- Ada 2005 (AI-251): Initialize the secondary tags components
2438 -- located at fixed positions (tags whose position depends on
2439 -- variable size components are initialized later ---see below)
2441 if Ada_Version >= Ada_2005
2442 and then not Is_Interface (Rec_Type)
2443 and then Has_Interfaces (Rec_Type)
2447 Target => Make_Identifier (Loc, Name_uInit),
2448 Stmts_List => Init_Tags_List,
2449 Fixed_Comps => True,
2450 Variable_Comps => False);
2453 Prepend_To (Body_Stmts,
2454 Make_If_Statement (Loc,
2455 Condition => New_Occurrence_Of (Set_Tag, Loc),
2456 Then_Statements => Init_Tags_List));
2458 -- Case 2: CPP type. The imported C++ constructor takes care of
2459 -- tags initialization. No action needed here because the IP
2460 -- is built by Set_CPP_Constructors; in this case the IP is a
2461 -- wrapper that invokes the C++ constructor and copies the C++
2462 -- tags locally. Done to inherit the C++ slots in Ada derivations
2465 elsif Is_CPP_Class (Rec_Type) then
2466 pragma Assert (False);
2469 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2470 -- type derivations. Derivations of imported C++ classes add a
2471 -- complication, because we cannot inhibit tag setting in the
2472 -- constructor for the parent. Hence we initialize the tag after
2473 -- the call to the parent IP (that is, in reverse order compared
2474 -- with pure Ada hierarchies ---see comment on case 1).
2477 -- Initialize the primary tag
2479 Init_Tags_List := New_List (
2480 Make_Assignment_Statement (Loc,
2482 Make_Selected_Component (Loc,
2483 Prefix => Make_Identifier (Loc, Name_uInit),
2486 (First_Tag_Component (Rec_Type), Loc)),
2490 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2492 -- Ada 2005 (AI-251): Initialize the secondary tags components
2493 -- located at fixed positions (tags whose position depends on
2494 -- variable size components are initialized later ---see below)
2496 if Ada_Version >= Ada_2005
2497 and then not Is_Interface (Rec_Type)
2498 and then Has_Interfaces (Rec_Type)
2502 Target => Make_Identifier (Loc, Name_uInit),
2503 Stmts_List => Init_Tags_List,
2504 Fixed_Comps => True,
2505 Variable_Comps => False);
2508 -- Initialize the tag component after invocation of parent IP.
2511 -- parent_IP(_init.parent); // Invokes the C++ constructor
2512 -- [ typIC; ] // Inherit C++ slots from parent
2519 -- Search for the call to the IP of the parent. We assume
2520 -- that the first init_proc call is for the parent.
2522 Ins_Nod := First (Body_Stmts);
2523 while Present (Next (Ins_Nod))
2524 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2525 or else not Is_Init_Proc (Name (Ins_Nod)))
2530 -- The IC routine copies the inherited slots of the C+ part
2531 -- of the dispatch table from the parent and updates the
2532 -- overridden C++ slots.
2534 if CPP_Num_Prims (Rec_Type) > 0 then
2536 Init_DT : Entity_Id;
2540 Init_DT := CPP_Init_Proc (Rec_Type);
2541 pragma Assert (Present (Init_DT));
2544 Make_Procedure_Call_Statement (Loc,
2545 New_Reference_To (Init_DT, Loc));
2546 Insert_After (Ins_Nod, New_Nod);
2548 -- Update location of init tag statements
2554 Insert_List_After (Ins_Nod, Init_Tags_List);
2558 -- Ada 2005 (AI-251): Initialize the secondary tag components
2559 -- located at variable positions. We delay the generation of this
2560 -- code until here because the value of the attribute 'Position
2561 -- applied to variable size components of the parent type that
2562 -- depend on discriminants is only safely read at runtime after
2563 -- the parent components have been initialized.
2565 if Ada_Version >= Ada_2005
2566 and then not Is_Interface (Rec_Type)
2567 and then Has_Interfaces (Rec_Type)
2568 and then Has_Discriminants (Etype (Rec_Type))
2569 and then Is_Variable_Size_Record (Etype (Rec_Type))
2571 Init_Tags_List := New_List;
2575 Target => Make_Identifier (Loc, Name_uInit),
2576 Stmts_List => Init_Tags_List,
2577 Fixed_Comps => False,
2578 Variable_Comps => True);
2580 if Is_Non_Empty_List (Init_Tags_List) then
2581 Append_List_To (Body_Stmts, Init_Tags_List);
2586 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2587 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2590 -- Local_DF_Id (_init, C1, ..., CN);
2594 and then Needs_Finalization (Rec_Type)
2595 and then not Is_Abstract_Type (Rec_Type)
2596 and then not Restriction_Active (No_Exception_Propagation)
2599 Local_DF_Id : Entity_Id;
2602 -- Create a local version of Deep_Finalize which has indication
2603 -- of partial initialization state.
2605 Local_DF_Id := Make_Temporary (Loc, 'F');
2608 Make_Local_Deep_Finalize (Rec_Type, Local_DF_Id));
2610 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2611 Make_Exception_Handler (Loc,
2612 Exception_Choices => New_List (
2613 Make_Others_Choice (Loc)),
2615 Statements => New_List (
2616 Make_Procedure_Call_Statement (Loc,
2618 New_Reference_To (Local_DF_Id, Loc),
2620 Parameter_Associations => New_List (
2621 Make_Identifier (Loc, Name_uInit),
2622 New_Reference_To (Standard_False, Loc))),
2624 Make_Raise_Statement (Loc)))));
2627 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2630 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2632 if not Debug_Generated_Code then
2633 Set_Debug_Info_Off (Proc_Id);
2636 -- Associate Init_Proc with type, and determine if the procedure
2637 -- is null (happens because of the Initialize_Scalars pragma case,
2638 -- where we have to generate a null procedure in case it is called
2639 -- by a client with Initialize_Scalars set). Such procedures have
2640 -- to be generated, but do not have to be called, so we mark them
2641 -- as null to suppress the call.
2643 Set_Init_Proc (Rec_Type, Proc_Id);
2645 if List_Length (Body_Stmts) = 1
2647 -- We must skip SCIL nodes because they may have been added to this
2648 -- list by Insert_Actions.
2650 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2651 and then VM_Target = No_VM
2653 -- Even though the init proc may be null at this time it might get
2654 -- some stuff added to it later by the VM backend.
2656 Set_Is_Null_Init_Proc (Proc_Id);
2658 end Build_Init_Procedure;
2660 ---------------------------
2661 -- Build_Init_Statements --
2662 ---------------------------
2664 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2665 Checks : constant List_Id := New_List;
2666 Actions : List_Id := No_List;
2667 Comp_Loc : Source_Ptr;
2668 Counter_Id : Entity_Id := Empty;
2675 procedure Increment_Counter (Loc : Source_Ptr);
2676 -- Generate an "increment by one" statement for the current counter
2677 -- and append it to the list Stmts.
2679 procedure Make_Counter (Loc : Source_Ptr);
2680 -- Create a new counter for the current component list. The routine
2681 -- creates a new defining Id, adds an object declaration and sets
2682 -- the Id generator for the next variant.
2684 -----------------------
2685 -- Increment_Counter --
2686 -----------------------
2688 procedure Increment_Counter (Loc : Source_Ptr) is
2691 -- Counter := Counter + 1;
2694 Make_Assignment_Statement (Loc,
2695 Name => New_Reference_To (Counter_Id, Loc),
2698 Left_Opnd => New_Reference_To (Counter_Id, Loc),
2699 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2700 end Increment_Counter;
2706 procedure Make_Counter (Loc : Source_Ptr) is
2708 -- Increment the Id generator
2710 Counter := Counter + 1;
2712 -- Create the entity and declaration
2715 Make_Defining_Identifier (Loc,
2716 Chars => New_External_Name ('C', Counter));
2719 -- Cnn : Integer := 0;
2722 Make_Object_Declaration (Loc,
2723 Defining_Identifier => Counter_Id,
2724 Object_Definition =>
2725 New_Reference_To (Standard_Integer, Loc),
2727 Make_Integer_Literal (Loc, 0)));
2730 -- Start of processing for Build_Init_Statements
2733 if Null_Present (Comp_List) then
2734 return New_List (Make_Null_Statement (Loc));
2739 -- Loop through visible declarations of task types and protected
2740 -- types moving any expanded code from the spec to the body of the
2743 if Is_Task_Record_Type (Rec_Type)
2744 or else Is_Protected_Record_Type (Rec_Type)
2747 Decl : constant Node_Id :=
2748 Parent (Corresponding_Concurrent_Type (Rec_Type));
2754 if Is_Task_Record_Type (Rec_Type) then
2755 Def := Task_Definition (Decl);
2757 Def := Protected_Definition (Decl);
2760 if Present (Def) then
2761 N1 := First (Visible_Declarations (Def));
2762 while Present (N1) loop
2766 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2767 or else Nkind (N2) in N_Raise_xxx_Error
2768 or else Nkind (N2) = N_Procedure_Call_Statement
2771 New_Copy_Tree (N2, New_Scope => Proc_Id));
2772 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2780 -- Loop through components, skipping pragmas, in 2 steps. The first
2781 -- step deals with regular components. The second step deals with
2782 -- components that have per object constraints and no explicit
2787 -- First pass : regular components
2789 Decl := First_Non_Pragma (Component_Items (Comp_List));
2790 while Present (Decl) loop
2791 Comp_Loc := Sloc (Decl);
2793 (Subtype_Indication (Component_Definition (Decl)), Checks);
2795 Id := Defining_Identifier (Decl);
2798 -- Leave any processing of per-object constrained component for
2801 if Has_Access_Constraint (Id) and then No (Expression (Decl)) then
2804 -- Regular component cases
2807 -- Explicit initialization
2809 if Present (Expression (Decl)) then
2810 if Is_CPP_Constructor_Call (Expression (Decl)) then
2812 Build_Initialization_Call
2815 Make_Selected_Component (Comp_Loc,
2817 Make_Identifier (Comp_Loc, Name_uInit),
2819 New_Occurrence_Of (Id, Comp_Loc)),
2821 In_Init_Proc => True,
2822 Enclos_Type => Rec_Type,
2823 Discr_Map => Discr_Map,
2824 Constructor_Ref => Expression (Decl));
2826 Actions := Build_Assignment (Id, Expression (Decl));
2829 -- CPU, Dispatching_Domain, Priority and Size components are
2830 -- filled with the corresponding rep item expression of the
2831 -- concurrent type (if any).
2833 elsif Ekind (Scope (Id)) = E_Record_Type
2834 and then Present (Corresponding_Concurrent_Type (Scope (Id)))
2835 and then Nam_In (Chars (Id), Name_uCPU,
2836 Name_uDispatching_Domain,
2845 if Chars (Id) = Name_uCPU then
2848 elsif Chars (Id) = Name_uDispatching_Domain then
2849 Nam := Name_Dispatching_Domain;
2851 elsif Chars (Id) = Name_uPriority then
2852 Nam := Name_Priority;
2855 -- Get the Rep Item (aspect specification, attribute
2856 -- definition clause or pragma) of the corresponding
2861 (Corresponding_Concurrent_Type (Scope (Id)),
2863 Check_Parents => False);
2865 if Present (Ritem) then
2869 if Nkind (Ritem) = N_Pragma then
2870 Exp := First (Pragma_Argument_Associations (Ritem));
2872 if Nkind (Exp) = N_Pragma_Argument_Association then
2873 Exp := Expression (Exp);
2876 -- Conversion for Priority expression
2878 if Nam = Name_Priority then
2879 if Pragma_Name (Ritem) = Name_Priority
2880 and then not GNAT_Mode
2882 Exp := Convert_To (RTE (RE_Priority), Exp);
2885 Convert_To (RTE (RE_Any_Priority), Exp);
2889 -- Aspect/Attribute definition clause case
2892 Exp := Expression (Ritem);
2894 -- Conversion for Priority expression
2896 if Nam = Name_Priority then
2897 if Chars (Ritem) = Name_Priority
2898 and then not GNAT_Mode
2900 Exp := Convert_To (RTE (RE_Priority), Exp);
2903 Convert_To (RTE (RE_Any_Priority), Exp);
2908 -- Conversion for Dispatching_Domain value
2910 if Nam = Name_Dispatching_Domain then
2912 Unchecked_Convert_To
2913 (RTE (RE_Dispatching_Domain_Access), Exp);
2916 Actions := Build_Assignment (Id, Exp);
2918 -- Nothing needed if no Rep Item
2925 -- Composite component with its own Init_Proc
2927 elsif not Is_Interface (Typ)
2928 and then Has_Non_Null_Base_Init_Proc (Typ)
2931 Build_Initialization_Call
2933 Make_Selected_Component (Comp_Loc,
2935 Make_Identifier (Comp_Loc, Name_uInit),
2936 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
2938 In_Init_Proc => True,
2939 Enclos_Type => Rec_Type,
2940 Discr_Map => Discr_Map);
2942 Clean_Task_Names (Typ, Proc_Id);
2944 -- Simple initialization
2946 elsif Component_Needs_Simple_Initialization (Typ) then
2949 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2951 -- Nothing needed for this case
2957 if Present (Checks) then
2958 Append_List_To (Stmts, Checks);
2961 if Present (Actions) then
2962 Append_List_To (Stmts, Actions);
2964 -- Preserve the initialization state in the current counter
2966 if Chars (Id) /= Name_uParent
2967 and then Needs_Finalization (Typ)
2969 if No (Counter_Id) then
2970 Make_Counter (Comp_Loc);
2973 Increment_Counter (Comp_Loc);
2978 Next_Non_Pragma (Decl);
2981 -- Set up tasks and protected object support. This needs to be done
2982 -- before any component with a per-object access discriminant
2983 -- constraint, or any variant part (which may contain such
2984 -- components) is initialized, because the initialization of these
2985 -- components may reference the enclosing concurrent object.
2987 -- For a task record type, add the task create call and calls to bind
2988 -- any interrupt (signal) entries.
2990 if Is_Task_Record_Type (Rec_Type) then
2992 -- In the case of the restricted run time the ATCB has already
2993 -- been preallocated.
2995 if Restricted_Profile then
2997 Make_Assignment_Statement (Loc,
2999 Make_Selected_Component (Loc,
3000 Prefix => Make_Identifier (Loc, Name_uInit),
3001 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
3003 Make_Attribute_Reference (Loc,
3005 Make_Selected_Component (Loc,
3006 Prefix => Make_Identifier (Loc, Name_uInit),
3007 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
3008 Attribute_Name => Name_Unchecked_Access)));
3011 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
3014 Task_Type : constant Entity_Id :=
3015 Corresponding_Concurrent_Type (Rec_Type);
3016 Task_Decl : constant Node_Id := Parent (Task_Type);
3017 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
3018 Decl_Loc : Source_Ptr;
3023 if Present (Task_Def) then
3024 Vis_Decl := First (Visible_Declarations (Task_Def));
3025 while Present (Vis_Decl) loop
3026 Decl_Loc := Sloc (Vis_Decl);
3028 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
3029 if Get_Attribute_Id (Chars (Vis_Decl)) =
3032 Ent := Entity (Name (Vis_Decl));
3034 if Ekind (Ent) = E_Entry then
3036 Make_Procedure_Call_Statement (Decl_Loc,
3038 New_Reference_To (RTE (
3039 RE_Bind_Interrupt_To_Entry), Decl_Loc),
3040 Parameter_Associations => New_List (
3041 Make_Selected_Component (Decl_Loc,
3043 Make_Identifier (Decl_Loc, Name_uInit),
3046 (Decl_Loc, Name_uTask_Id)),
3047 Entry_Index_Expression
3048 (Decl_Loc, Ent, Empty, Task_Type),
3049 Expression (Vis_Decl))));
3060 -- For a protected type, add statements generated by
3061 -- Make_Initialize_Protection.
3063 if Is_Protected_Record_Type (Rec_Type) then
3064 Append_List_To (Stmts,
3065 Make_Initialize_Protection (Rec_Type));
3068 -- Second pass: components with per-object constraints
3071 Decl := First_Non_Pragma (Component_Items (Comp_List));
3072 while Present (Decl) loop
3073 Comp_Loc := Sloc (Decl);
3074 Id := Defining_Identifier (Decl);
3077 if Has_Access_Constraint (Id)
3078 and then No (Expression (Decl))
3080 if Has_Non_Null_Base_Init_Proc (Typ) then
3081 Append_List_To (Stmts,
3082 Build_Initialization_Call (Comp_Loc,
3083 Make_Selected_Component (Comp_Loc,
3085 Make_Identifier (Comp_Loc, Name_uInit),
3086 Selector_Name => New_Occurrence_Of (Id, Comp_Loc)),
3088 In_Init_Proc => True,
3089 Enclos_Type => Rec_Type,
3090 Discr_Map => Discr_Map));
3092 Clean_Task_Names (Typ, Proc_Id);
3094 -- Preserve initialization state in the current counter
3096 if Needs_Finalization (Typ) then
3097 if No (Counter_Id) then
3098 Make_Counter (Comp_Loc);
3101 Increment_Counter (Comp_Loc);
3104 elsif Component_Needs_Simple_Initialization (Typ) then
3105 Append_List_To (Stmts,
3107 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3111 Next_Non_Pragma (Decl);
3115 -- Process the variant part
3117 if Present (Variant_Part (Comp_List)) then
3119 Variant_Alts : constant List_Id := New_List;
3120 Var_Loc : Source_Ptr;
3125 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3126 while Present (Variant) loop
3127 Var_Loc := Sloc (Variant);
3128 Append_To (Variant_Alts,
3129 Make_Case_Statement_Alternative (Var_Loc,
3131 New_Copy_List (Discrete_Choices (Variant)),
3133 Build_Init_Statements (Component_List (Variant))));
3134 Next_Non_Pragma (Variant);
3137 -- The expression of the case statement which is a reference
3138 -- to one of the discriminants is replaced by the appropriate
3139 -- formal parameter of the initialization procedure.
3142 Make_Case_Statement (Var_Loc,
3144 New_Reference_To (Discriminal (
3145 Entity (Name (Variant_Part (Comp_List)))), Var_Loc),
3146 Alternatives => Variant_Alts));
3150 -- If no initializations when generated for component declarations
3151 -- corresponding to this Stmts, append a null statement to Stmts to
3152 -- to make it a valid Ada tree.
3154 if Is_Empty_List (Stmts) then
3155 Append (Make_Null_Statement (Loc), Stmts);
3161 when RE_Not_Available =>
3163 end Build_Init_Statements;
3165 -------------------------
3166 -- Build_Record_Checks --
3167 -------------------------
3169 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3170 Subtype_Mark_Id : Entity_Id;
3172 procedure Constrain_Array
3174 Check_List : List_Id);
3175 -- Apply a list of index constraints to an unconstrained array type.
3176 -- The first parameter is the entity for the resulting subtype.
3177 -- Check_List is a list to which the check actions are appended.
3179 ---------------------
3180 -- Constrain_Array --
3181 ---------------------
3183 procedure Constrain_Array
3185 Check_List : List_Id)
3187 C : constant Node_Id := Constraint (SI);
3188 Number_Of_Constraints : Nat := 0;
3192 procedure Constrain_Index
3195 Check_List : List_Id);
3196 -- Process an index constraint in a constrained array declaration.
3197 -- The constraint can be either a subtype name or a range with or
3198 -- without an explicit subtype mark. Index is the corresponding
3199 -- index of the unconstrained array. S is the range expression.
3200 -- Check_List is a list to which the check actions are appended.
3202 ---------------------
3203 -- Constrain_Index --
3204 ---------------------
3206 procedure Constrain_Index
3209 Check_List : List_Id)
3211 T : constant Entity_Id := Etype (Index);
3214 if Nkind (S) = N_Range then
3215 Process_Range_Expr_In_Decl (S, T, Check_List);
3217 end Constrain_Index;
3219 -- Start of processing for Constrain_Array
3222 T := Entity (Subtype_Mark (SI));
3224 if Ekind (T) in Access_Kind then
3225 T := Designated_Type (T);
3228 S := First (Constraints (C));
3230 while Present (S) loop
3231 Number_Of_Constraints := Number_Of_Constraints + 1;
3235 -- In either case, the index constraint must provide a discrete
3236 -- range for each index of the array type and the type of each
3237 -- discrete range must be the same as that of the corresponding
3238 -- index. (RM 3.6.1)
3240 S := First (Constraints (C));
3241 Index := First_Index (T);
3244 -- Apply constraints to each index type
3246 for J in 1 .. Number_Of_Constraints loop
3247 Constrain_Index (Index, S, Check_List);
3251 end Constrain_Array;
3253 -- Start of processing for Build_Record_Checks
3256 if Nkind (S) = N_Subtype_Indication then
3257 Find_Type (Subtype_Mark (S));
3258 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3260 -- Remaining processing depends on type
3262 case Ekind (Subtype_Mark_Id) is
3265 Constrain_Array (S, Check_List);
3271 end Build_Record_Checks;
3273 -------------------------------------------
3274 -- Component_Needs_Simple_Initialization --
3275 -------------------------------------------
3277 function Component_Needs_Simple_Initialization
3278 (T : Entity_Id) return Boolean
3282 Needs_Simple_Initialization (T)
3283 and then not Is_RTE (T, RE_Tag)
3285 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3287 and then not Is_RTE (T, RE_Interface_Tag);
3288 end Component_Needs_Simple_Initialization;
3290 --------------------------------------
3291 -- Parent_Subtype_Renaming_Discrims --
3292 --------------------------------------
3294 function Parent_Subtype_Renaming_Discrims return Boolean is
3299 if Base_Type (Rec_Ent) /= Rec_Ent then
3303 if Etype (Rec_Ent) = Rec_Ent
3304 or else not Has_Discriminants (Rec_Ent)
3305 or else Is_Constrained (Rec_Ent)
3306 or else Is_Tagged_Type (Rec_Ent)
3311 -- If there are no explicit stored discriminants we have inherited
3312 -- the root type discriminants so far, so no renamings occurred.
3314 if First_Discriminant (Rec_Ent) =
3315 First_Stored_Discriminant (Rec_Ent)
3320 -- Check if we have done some trivial renaming of the parent
3321 -- discriminants, i.e. something like
3323 -- type DT (X1, X2: int) is new PT (X1, X2);
3325 De := First_Discriminant (Rec_Ent);
3326 Dp := First_Discriminant (Etype (Rec_Ent));
3327 while Present (De) loop
3328 pragma Assert (Present (Dp));
3330 if Corresponding_Discriminant (De) /= Dp then
3334 Next_Discriminant (De);
3335 Next_Discriminant (Dp);
3338 return Present (Dp);
3339 end Parent_Subtype_Renaming_Discrims;
3341 ------------------------
3342 -- Requires_Init_Proc --
3343 ------------------------
3345 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3346 Comp_Decl : Node_Id;
3351 -- Definitely do not need one if specifically suppressed
3353 if Initialization_Suppressed (Rec_Id) then
3357 -- If it is a type derived from a type with unknown discriminants,
3358 -- we cannot build an initialization procedure for it.
3360 if Has_Unknown_Discriminants (Rec_Id)
3361 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3366 -- Otherwise we need to generate an initialization procedure if
3367 -- Is_CPP_Class is False and at least one of the following applies:
3369 -- 1. Discriminants are present, since they need to be initialized
3370 -- with the appropriate discriminant constraint expressions.
3371 -- However, the discriminant of an unchecked union does not
3372 -- count, since the discriminant is not present.
3374 -- 2. The type is a tagged type, since the implicit Tag component
3375 -- needs to be initialized with a pointer to the dispatch table.
3377 -- 3. The type contains tasks
3379 -- 4. One or more components has an initial value
3381 -- 5. One or more components is for a type which itself requires
3382 -- an initialization procedure.
3384 -- 6. One or more components is a type that requires simple
3385 -- initialization (see Needs_Simple_Initialization), except
3386 -- that types Tag and Interface_Tag are excluded, since fields
3387 -- of these types are initialized by other means.
3389 -- 7. The type is the record type built for a task type (since at
3390 -- the very least, Create_Task must be called)
3392 -- 8. The type is the record type built for a protected type (since
3393 -- at least Initialize_Protection must be called)
3395 -- 9. The type is marked as a public entity. The reason we add this
3396 -- case (even if none of the above apply) is to properly handle
3397 -- Initialize_Scalars. If a package is compiled without an IS
3398 -- pragma, and the client is compiled with an IS pragma, then
3399 -- the client will think an initialization procedure is present
3400 -- and call it, when in fact no such procedure is required, but
3401 -- since the call is generated, there had better be a routine
3402 -- at the other end of the call, even if it does nothing!)
3404 -- Note: the reason we exclude the CPP_Class case is because in this
3405 -- case the initialization is performed by the C++ constructors, and
3406 -- the IP is built by Set_CPP_Constructors.
3408 if Is_CPP_Class (Rec_Id) then
3411 elsif Is_Interface (Rec_Id) then
3414 elsif (Has_Discriminants (Rec_Id)
3415 and then not Is_Unchecked_Union (Rec_Id))
3416 or else Is_Tagged_Type (Rec_Id)
3417 or else Is_Concurrent_Record_Type (Rec_Id)
3418 or else Has_Task (Rec_Id)
3423 Id := First_Component (Rec_Id);
3424 while Present (Id) loop
3425 Comp_Decl := Parent (Id);
3428 if Present (Expression (Comp_Decl))
3429 or else Has_Non_Null_Base_Init_Proc (Typ)
3430 or else Component_Needs_Simple_Initialization (Typ)
3435 Next_Component (Id);
3438 -- As explained above, a record initialization procedure is needed
3439 -- for public types in case Initialize_Scalars applies to a client.
3440 -- However, such a procedure is not needed in the case where either
3441 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3442 -- applies. No_Initialize_Scalars excludes the possibility of using
3443 -- Initialize_Scalars in any partition, and No_Default_Initialization
3444 -- implies that no initialization should ever be done for objects of
3445 -- the type, so is incompatible with Initialize_Scalars.
3447 if not Restriction_Active (No_Initialize_Scalars)
3448 and then not Restriction_Active (No_Default_Initialization)
3449 and then Is_Public (Rec_Id)
3455 end Requires_Init_Proc;
3457 -- Start of processing for Build_Record_Init_Proc
3460 -- Check for value type, which means no initialization required
3462 Rec_Type := Defining_Identifier (N);
3464 if Is_Value_Type (Rec_Type) then
3468 -- This may be full declaration of a private type, in which case
3469 -- the visible entity is a record, and the private entity has been
3470 -- exchanged with it in the private part of the current package.
3471 -- The initialization procedure is built for the record type, which
3472 -- is retrievable from the private entity.
3474 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3475 Rec_Type := Underlying_Type (Rec_Type);
3478 -- If there are discriminants, build the discriminant map to replace
3479 -- discriminants by their discriminals in complex bound expressions.
3480 -- These only arise for the corresponding records of synchronized types.
3482 if Is_Concurrent_Record_Type (Rec_Type)
3483 and then Has_Discriminants (Rec_Type)
3488 Disc := First_Discriminant (Rec_Type);
3489 while Present (Disc) loop
3490 Append_Elmt (Disc, Discr_Map);
3491 Append_Elmt (Discriminal (Disc), Discr_Map);
3492 Next_Discriminant (Disc);
3497 -- Derived types that have no type extension can use the initialization
3498 -- procedure of their parent and do not need a procedure of their own.
3499 -- This is only correct if there are no representation clauses for the
3500 -- type or its parent, and if the parent has in fact been frozen so
3501 -- that its initialization procedure exists.
3503 if Is_Derived_Type (Rec_Type)
3504 and then not Is_Tagged_Type (Rec_Type)
3505 and then not Is_Unchecked_Union (Rec_Type)
3506 and then not Has_New_Non_Standard_Rep (Rec_Type)
3507 and then not Parent_Subtype_Renaming_Discrims
3508 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3510 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3512 -- Otherwise if we need an initialization procedure, then build one,
3513 -- mark it as public and inlinable and as having a completion.
3515 elsif Requires_Init_Proc (Rec_Type)
3516 or else Is_Unchecked_Union (Rec_Type)
3519 Make_Defining_Identifier (Loc,
3520 Chars => Make_Init_Proc_Name (Rec_Type));
3522 -- If No_Default_Initialization restriction is active, then we don't
3523 -- want to build an init_proc, but we need to mark that an init_proc
3524 -- would be needed if this restriction was not active (so that we can
3525 -- detect attempts to call it), so set a dummy init_proc in place.
3527 if Restriction_Active (No_Default_Initialization) then
3528 Set_Init_Proc (Rec_Type, Proc_Id);
3532 Build_Offset_To_Top_Functions;
3533 Build_CPP_Init_Procedure;
3534 Build_Init_Procedure;
3535 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3537 -- The initialization of protected records is not worth inlining.
3538 -- In addition, when compiled for another unit for inlining purposes,
3539 -- it may make reference to entities that have not been elaborated
3540 -- yet. The initialization of controlled records contains a nested
3541 -- clean-up procedure that makes it impractical to inline as well,
3542 -- and leads to undefined symbols if inlined in a different unit.
3543 -- Similar considerations apply to task types.
3545 if not Is_Concurrent_Type (Rec_Type)
3546 and then not Has_Task (Rec_Type)
3547 and then not Needs_Finalization (Rec_Type)
3549 Set_Is_Inlined (Proc_Id);
3552 Set_Is_Internal (Proc_Id);
3553 Set_Has_Completion (Proc_Id);
3555 if not Debug_Generated_Code then
3556 Set_Debug_Info_Off (Proc_Id);
3560 Agg : constant Node_Id :=
3561 Build_Equivalent_Record_Aggregate (Rec_Type);
3563 procedure Collect_Itypes (Comp : Node_Id);
3564 -- Generate references to itypes in the aggregate, because
3565 -- the first use of the aggregate may be in a nested scope.
3567 --------------------
3568 -- Collect_Itypes --
3569 --------------------
3571 procedure Collect_Itypes (Comp : Node_Id) is
3574 Typ : constant Entity_Id := Etype (Comp);
3577 if Is_Array_Type (Typ)
3578 and then Is_Itype (Typ)
3580 Ref := Make_Itype_Reference (Loc);
3581 Set_Itype (Ref, Typ);
3582 Append_Freeze_Action (Rec_Type, Ref);
3584 Ref := Make_Itype_Reference (Loc);
3585 Set_Itype (Ref, Etype (First_Index (Typ)));
3586 Append_Freeze_Action (Rec_Type, Ref);
3588 Sub_Aggr := First (Expressions (Comp));
3590 -- Recurse on nested arrays
3592 while Present (Sub_Aggr) loop
3593 Collect_Itypes (Sub_Aggr);
3600 -- If there is a static initialization aggregate for the type,
3601 -- generate itype references for the types of its (sub)components,
3602 -- to prevent out-of-scope errors in the resulting tree.
3603 -- The aggregate may have been rewritten as a Raise node, in which
3604 -- case there are no relevant itypes.
3607 and then Nkind (Agg) = N_Aggregate
3609 Set_Static_Initialization (Proc_Id, Agg);
3614 Comp := First (Component_Associations (Agg));
3615 while Present (Comp) loop
3616 Collect_Itypes (Expression (Comp));
3623 end Build_Record_Init_Proc;
3625 --------------------------------
3626 -- Build_Record_Invariant_Proc --
3627 --------------------------------
3629 function Build_Record_Invariant_Proc
3630 (R_Type : Entity_Id;
3631 Nod : Node_Id) return Node_Id
3633 Loc : constant Source_Ptr := Sloc (Nod);
3635 Object_Name : constant Name_Id := New_Internal_Name ('I');
3636 -- Name for argument of invariant procedure
3638 Object_Entity : constant Node_Id :=
3639 Make_Defining_Identifier (Loc, Object_Name);
3640 -- The procedure declaration entity for the argument
3642 Invariant_Found : Boolean;
3643 -- Set if any component needs an invariant check.
3645 Proc_Id : Entity_Id;
3646 Proc_Body : Node_Id;
3650 function Build_Invariant_Checks (Comp_List : Node_Id) return List_Id;
3651 -- Recursive procedure that generates a list of checks for components
3652 -- that need it, and recurses through variant parts when present.
3654 function Build_Component_Invariant_Call (Comp : Entity_Id)
3656 -- Build call to invariant procedure for a record component.
3658 ------------------------------------
3659 -- Build_Component_Invariant_Call --
3660 ------------------------------------
3662 function Build_Component_Invariant_Call (Comp : Entity_Id)
3670 Invariant_Found := True;
3671 Typ := Etype (Comp);
3674 Make_Selected_Component (Loc,
3675 Prefix => New_Occurrence_Of (Object_Entity, Loc),
3676 Selector_Name => New_Occurrence_Of (Comp, Loc));
3678 if Is_Access_Type (Typ) then
3679 Sel_Comp := Make_Explicit_Dereference (Loc, Sel_Comp);
3680 Typ := Designated_Type (Typ);
3684 Make_Procedure_Call_Statement (Loc,
3686 New_Occurrence_Of (Invariant_Procedure (Typ), Loc),
3687 Parameter_Associations => New_List (Sel_Comp));
3689 if Is_Access_Type (Etype (Comp)) then
3691 Make_If_Statement (Loc,
3694 Left_Opnd => Make_Null (Loc),
3696 Make_Selected_Component (Loc,
3697 Prefix => New_Occurrence_Of (Object_Entity, Loc),
3698 Selector_Name => New_Occurrence_Of (Comp, Loc))),
3699 Then_Statements => New_List (Call));
3703 end Build_Component_Invariant_Call;
3705 ----------------------------
3706 -- Build_Invariant_Checks --
3707 ----------------------------
3709 function Build_Invariant_Checks (Comp_List : Node_Id) return List_Id is
3716 Decl := First_Non_Pragma (Component_Items (Comp_List));
3717 while Present (Decl) loop
3718 if Nkind (Decl) = N_Component_Declaration then
3719 Id := Defining_Identifier (Decl);
3721 if Has_Invariants (Etype (Id))
3722 and then In_Open_Scopes (Scope (R_Type))
3724 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3726 elsif Is_Access_Type (Etype (Id))
3727 and then not Is_Access_Constant (Etype (Id))
3728 and then Has_Invariants (Designated_Type (Etype (Id)))
3729 and then In_Open_Scopes (Scope (Designated_Type (Etype (Id))))
3731 Append_To (Stmts, Build_Component_Invariant_Call (Id));
3738 if Present (Variant_Part (Comp_List)) then
3740 Variant_Alts : constant List_Id := New_List;
3741 Var_Loc : Source_Ptr;
3743 Variant_Stmts : List_Id;
3747 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3748 while Present (Variant) loop
3750 Build_Invariant_Checks (Component_List (Variant));
3751 Var_Loc := Sloc (Variant);
3752 Append_To (Variant_Alts,
3753 Make_Case_Statement_Alternative (Var_Loc,
3755 New_Copy_List (Discrete_Choices (Variant)),
3756 Statements => Variant_Stmts));
3758 Next_Non_Pragma (Variant);
3761 -- The expression in the case statement is the reference to
3762 -- the discriminant of the target object.
3765 Make_Case_Statement (Var_Loc,
3767 Make_Selected_Component (Var_Loc,
3768 Prefix => New_Occurrence_Of (Object_Entity, Var_Loc),
3769 Selector_Name => New_Occurrence_Of
3771 (Name (Variant_Part (Comp_List))), Var_Loc)),
3772 Alternatives => Variant_Alts));
3777 end Build_Invariant_Checks;
3779 -- Start of processing for Build_Record_Invariant_Proc
3782 Invariant_Found := False;
3783 Type_Def := Type_Definition (Parent (R_Type));
3785 if Nkind (Type_Def) = N_Record_Definition
3786 and then not Null_Present (Type_Def)
3788 Stmts := Build_Invariant_Checks (Component_List (Type_Def));
3793 if not Invariant_Found then
3798 Make_Defining_Identifier (Loc,
3799 Chars => New_External_Name (Chars (R_Type), "Invariant"));
3802 Make_Subprogram_Body (Loc,
3804 Make_Procedure_Specification (Loc,
3805 Defining_Unit_Name => Proc_Id,
3806 Parameter_Specifications => New_List (
3807 Make_Parameter_Specification (Loc,
3808 Defining_Identifier => Object_Entity,
3809 Parameter_Type => New_Occurrence_Of (R_Type, Loc)))),
3811 Declarations => Empty_List,
3812 Handled_Statement_Sequence =>
3813 Make_Handled_Sequence_Of_Statements (Loc,
3814 Statements => Stmts));
3816 Set_Ekind (Proc_Id, E_Procedure);
3817 Set_Is_Public (Proc_Id, Is_Public (R_Type));
3818 Set_Is_Internal (Proc_Id);
3819 Set_Has_Completion (Proc_Id);
3822 -- Insert_After (Nod, Proc_Body);
3823 -- Analyze (Proc_Body);
3824 end Build_Record_Invariant_Proc;
3826 ----------------------------
3827 -- Build_Slice_Assignment --
3828 ----------------------------
3830 -- Generates the following subprogram:
3833 -- (Source, Target : Array_Type,
3834 -- Left_Lo, Left_Hi : Index;
3835 -- Right_Lo, Right_Hi : Index;
3843 -- if Left_Hi < Left_Lo then
3856 -- Target (Li1) := Source (Ri1);
3859 -- exit when Li1 = Left_Lo;
3860 -- Li1 := Index'pred (Li1);
3861 -- Ri1 := Index'pred (Ri1);
3863 -- exit when Li1 = Left_Hi;
3864 -- Li1 := Index'succ (Li1);
3865 -- Ri1 := Index'succ (Ri1);
3870 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3871 Loc : constant Source_Ptr := Sloc (Typ);
3872 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3874 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3875 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3876 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3877 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3878 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3879 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3880 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3881 -- Formal parameters of procedure
3883 Proc_Name : constant Entity_Id :=
3884 Make_Defining_Identifier (Loc,
3885 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3887 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3888 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3889 -- Subscripts for left and right sides
3896 -- Build declarations for indexes
3901 Make_Object_Declaration (Loc,
3902 Defining_Identifier => Lnn,
3903 Object_Definition =>
3904 New_Occurrence_Of (Index, Loc)));
3907 Make_Object_Declaration (Loc,
3908 Defining_Identifier => Rnn,
3909 Object_Definition =>
3910 New_Occurrence_Of (Index, Loc)));
3914 -- Build test for empty slice case
3917 Make_If_Statement (Loc,
3920 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3921 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3922 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3924 -- Build initializations for indexes
3927 F_Init : constant List_Id := New_List;
3928 B_Init : constant List_Id := New_List;
3932 Make_Assignment_Statement (Loc,
3933 Name => New_Occurrence_Of (Lnn, Loc),
3934 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3937 Make_Assignment_Statement (Loc,
3938 Name => New_Occurrence_Of (Rnn, Loc),
3939 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3942 Make_Assignment_Statement (Loc,
3943 Name => New_Occurrence_Of (Lnn, Loc),
3944 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3947 Make_Assignment_Statement (Loc,
3948 Name => New_Occurrence_Of (Rnn, Loc),
3949 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3952 Make_If_Statement (Loc,
3953 Condition => New_Occurrence_Of (Rev, Loc),
3954 Then_Statements => B_Init,
3955 Else_Statements => F_Init));
3958 -- Now construct the assignment statement
3961 Make_Loop_Statement (Loc,
3962 Statements => New_List (
3963 Make_Assignment_Statement (Loc,
3965 Make_Indexed_Component (Loc,
3966 Prefix => New_Occurrence_Of (Larray, Loc),
3967 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3969 Make_Indexed_Component (Loc,
3970 Prefix => New_Occurrence_Of (Rarray, Loc),
3971 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3972 End_Label => Empty);
3974 -- Build the exit condition and increment/decrement statements
3977 F_Ass : constant List_Id := New_List;
3978 B_Ass : constant List_Id := New_List;
3982 Make_Exit_Statement (Loc,
3985 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3986 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3989 Make_Assignment_Statement (Loc,
3990 Name => New_Occurrence_Of (Lnn, Loc),
3992 Make_Attribute_Reference (Loc,
3994 New_Occurrence_Of (Index, Loc),
3995 Attribute_Name => Name_Succ,
3996 Expressions => New_List (
3997 New_Occurrence_Of (Lnn, Loc)))));
4000 Make_Assignment_Statement (Loc,
4001 Name => New_Occurrence_Of (Rnn, Loc),
4003 Make_Attribute_Reference (Loc,
4005 New_Occurrence_Of (Index, Loc),
4006 Attribute_Name => Name_Succ,
4007 Expressions => New_List (
4008 New_Occurrence_Of (Rnn, Loc)))));
4011 Make_Exit_Statement (Loc,
4014 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4015 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
4018 Make_Assignment_Statement (Loc,
4019 Name => New_Occurrence_Of (Lnn, Loc),
4021 Make_Attribute_Reference (Loc,
4023 New_Occurrence_Of (Index, Loc),
4024 Attribute_Name => Name_Pred,
4025 Expressions => New_List (
4026 New_Occurrence_Of (Lnn, Loc)))));
4029 Make_Assignment_Statement (Loc,
4030 Name => New_Occurrence_Of (Rnn, Loc),
4032 Make_Attribute_Reference (Loc,
4034 New_Occurrence_Of (Index, Loc),
4035 Attribute_Name => Name_Pred,
4036 Expressions => New_List (
4037 New_Occurrence_Of (Rnn, Loc)))));
4039 Append_To (Statements (Loops),
4040 Make_If_Statement (Loc,
4041 Condition => New_Occurrence_Of (Rev, Loc),
4042 Then_Statements => B_Ass,
4043 Else_Statements => F_Ass));
4046 Append_To (Stats, Loops);
4050 Formals : List_Id := New_List;
4053 Formals := New_List (
4054 Make_Parameter_Specification (Loc,
4055 Defining_Identifier => Larray,
4056 Out_Present => True,
4058 New_Reference_To (Base_Type (Typ), Loc)),
4060 Make_Parameter_Specification (Loc,
4061 Defining_Identifier => Rarray,
4063 New_Reference_To (Base_Type (Typ), Loc)),
4065 Make_Parameter_Specification (Loc,
4066 Defining_Identifier => Left_Lo,
4068 New_Reference_To (Index, Loc)),
4070 Make_Parameter_Specification (Loc,
4071 Defining_Identifier => Left_Hi,
4073 New_Reference_To (Index, Loc)),
4075 Make_Parameter_Specification (Loc,
4076 Defining_Identifier => Right_Lo,
4078 New_Reference_To (Index, Loc)),
4080 Make_Parameter_Specification (Loc,
4081 Defining_Identifier => Right_Hi,
4083 New_Reference_To (Index, Loc)));
4086 Make_Parameter_Specification (Loc,
4087 Defining_Identifier => Rev,
4089 New_Reference_To (Standard_Boolean, Loc)));
4092 Make_Procedure_Specification (Loc,
4093 Defining_Unit_Name => Proc_Name,
4094 Parameter_Specifications => Formals);
4097 Make_Subprogram_Body (Loc,
4098 Specification => Spec,
4099 Declarations => Decls,
4100 Handled_Statement_Sequence =>
4101 Make_Handled_Sequence_Of_Statements (Loc,
4102 Statements => Stats)));
4105 Set_TSS (Typ, Proc_Name);
4106 Set_Is_Pure (Proc_Name);
4107 end Build_Slice_Assignment;
4109 -----------------------------
4110 -- Build_Untagged_Equality --
4111 -----------------------------
4113 procedure Build_Untagged_Equality (Typ : Entity_Id) is
4121 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
4122 -- Check whether the type T has a user-defined primitive equality. If so
4123 -- return it, else return Empty. If true for a component of Typ, we have
4124 -- to build the primitive equality for it.
4126 ---------------------
4127 -- User_Defined_Eq --
4128 ---------------------
4130 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
4135 Op := TSS (T, TSS_Composite_Equality);
4137 if Present (Op) then
4141 Prim := First_Elmt (Collect_Primitive_Operations (T));
4142 while Present (Prim) loop
4145 if Chars (Op) = Name_Op_Eq
4146 and then Etype (Op) = Standard_Boolean
4147 and then Etype (First_Formal (Op)) = T
4148 and then Etype (Next_Formal (First_Formal (Op))) = T
4157 end User_Defined_Eq;
4159 -- Start of processing for Build_Untagged_Equality
4162 -- If a record component has a primitive equality operation, we must
4163 -- build the corresponding one for the current type.
4166 Comp := First_Component (Typ);
4167 while Present (Comp) loop
4168 if Is_Record_Type (Etype (Comp))
4169 and then Present (User_Defined_Eq (Etype (Comp)))
4174 Next_Component (Comp);
4177 -- If there is a user-defined equality for the type, we do not create
4178 -- the implicit one.
4180 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
4182 while Present (Prim) loop
4183 if Chars (Node (Prim)) = Name_Op_Eq
4184 and then Comes_From_Source (Node (Prim))
4186 -- Don't we also need to check formal types and return type as in
4187 -- User_Defined_Eq above???
4190 Eq_Op := Node (Prim);
4198 -- If the type is derived, inherit the operation, if present, from the
4199 -- parent type. It may have been declared after the type derivation. If
4200 -- the parent type itself is derived, it may have inherited an operation
4201 -- that has itself been overridden, so update its alias and related
4202 -- flags. Ditto for inequality.
4204 if No (Eq_Op) and then Is_Derived_Type (Typ) then
4205 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
4206 while Present (Prim) loop
4207 if Chars (Node (Prim)) = Name_Op_Eq then
4208 Copy_TSS (Node (Prim), Typ);
4212 Op : constant Entity_Id := User_Defined_Eq (Typ);
4213 Eq_Op : constant Entity_Id := Node (Prim);
4214 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
4217 if Present (Op) then
4218 Set_Alias (Op, Eq_Op);
4219 Set_Is_Abstract_Subprogram
4220 (Op, Is_Abstract_Subprogram (Eq_Op));
4222 if Chars (Next_Entity (Op)) = Name_Op_Ne then
4223 Set_Is_Abstract_Subprogram
4224 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
4236 -- If not inherited and not user-defined, build body as for a type with
4237 -- tagged components.
4241 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
4242 Op := Defining_Entity (Decl);
4246 if Is_Library_Level_Entity (Typ) then
4250 end Build_Untagged_Equality;
4252 ------------------------------------
4253 -- Build_Variant_Record_Equality --
4254 ------------------------------------
4258 -- function _Equality (X, Y : T) return Boolean is
4260 -- -- Compare discriminants
4262 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
4266 -- -- Compare components
4268 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
4272 -- -- Compare variant part
4276 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
4281 -- if False or else X.Cn /= Y.Cn then
4289 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4290 Loc : constant Source_Ptr := Sloc (Typ);
4292 F : constant Entity_Id :=
4293 Make_Defining_Identifier (Loc,
4294 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4296 X : constant Entity_Id :=
4297 Make_Defining_Identifier (Loc,
4300 Y : constant Entity_Id :=
4301 Make_Defining_Identifier (Loc,
4304 Def : constant Node_Id := Parent (Typ);
4305 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4306 Stmts : constant List_Id := New_List;
4307 Pspecs : constant List_Id := New_List;
4310 -- Derived Unchecked_Union types no longer inherit the equality function
4313 if Is_Derived_Type (Typ)
4314 and then not Is_Unchecked_Union (Typ)
4315 and then not Has_New_Non_Standard_Rep (Typ)
4318 Parent_Eq : constant Entity_Id :=
4319 TSS (Root_Type (Typ), TSS_Composite_Equality);
4322 if Present (Parent_Eq) then
4323 Copy_TSS (Parent_Eq, Typ);
4330 Make_Subprogram_Body (Loc,
4332 Make_Function_Specification (Loc,
4333 Defining_Unit_Name => F,
4334 Parameter_Specifications => Pspecs,
4335 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
4336 Declarations => New_List,
4337 Handled_Statement_Sequence =>
4338 Make_Handled_Sequence_Of_Statements (Loc,
4339 Statements => Stmts)));
4342 Make_Parameter_Specification (Loc,
4343 Defining_Identifier => X,
4344 Parameter_Type => New_Reference_To (Typ, Loc)));
4347 Make_Parameter_Specification (Loc,
4348 Defining_Identifier => Y,
4349 Parameter_Type => New_Reference_To (Typ, Loc)));
4351 -- Unchecked_Unions require additional machinery to support equality.
4352 -- Two extra parameters (A and B) are added to the equality function
4353 -- parameter list in order to capture the inferred values of the
4354 -- discriminants in later calls.
4356 if Is_Unchecked_Union (Typ) then
4358 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
4360 A : constant Node_Id :=
4361 Make_Defining_Identifier (Loc,
4364 B : constant Node_Id :=
4365 Make_Defining_Identifier (Loc,
4369 -- Add A and B to the parameter list
4372 Make_Parameter_Specification (Loc,
4373 Defining_Identifier => A,
4374 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4377 Make_Parameter_Specification (Loc,
4378 Defining_Identifier => B,
4379 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4381 -- Generate the following header code to compare the inferred
4389 Make_If_Statement (Loc,
4392 Left_Opnd => New_Reference_To (A, Loc),
4393 Right_Opnd => New_Reference_To (B, Loc)),
4394 Then_Statements => New_List (
4395 Make_Simple_Return_Statement (Loc,
4396 Expression => New_Occurrence_Of (Standard_False, Loc)))));
4398 -- Generate component-by-component comparison. Note that we must
4399 -- propagate one of the inferred discriminant formals to act as
4400 -- the case statement switch.
4402 Append_List_To (Stmts,
4403 Make_Eq_Case (Typ, Comps, A));
4406 -- Normal case (not unchecked union)
4411 Discriminant_Specifications (Def)));
4413 Append_List_To (Stmts,
4414 Make_Eq_Case (Typ, Comps));
4418 Make_Simple_Return_Statement (Loc,
4419 Expression => New_Reference_To (Standard_True, Loc)));
4424 if not Debug_Generated_Code then
4425 Set_Debug_Info_Off (F);
4427 end Build_Variant_Record_Equality;
4429 -----------------------------
4430 -- Check_Stream_Attributes --
4431 -----------------------------
4433 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4435 Par_Read : constant Boolean :=
4436 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4437 and then not Has_Specified_Stream_Read (Typ);
4438 Par_Write : constant Boolean :=
4439 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4440 and then not Has_Specified_Stream_Write (Typ);
4442 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4443 -- Check that Comp has a user-specified Nam stream attribute
4449 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4451 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4452 Error_Msg_Name_1 := Nam;
4454 ("|component& in limited extension must have% attribute", Comp);
4458 -- Start of processing for Check_Stream_Attributes
4461 if Par_Read or else Par_Write then
4462 Comp := First_Component (Typ);
4463 while Present (Comp) loop
4464 if Comes_From_Source (Comp)
4465 and then Original_Record_Component (Comp) = Comp
4466 and then Is_Limited_Type (Etype (Comp))
4469 Check_Attr (Name_Read, TSS_Stream_Read);
4473 Check_Attr (Name_Write, TSS_Stream_Write);
4477 Next_Component (Comp);
4480 end Check_Stream_Attributes;
4482 -----------------------------
4483 -- Expand_Record_Extension --
4484 -----------------------------
4486 -- Add a field _parent at the beginning of the record extension. This is
4487 -- used to implement inheritance. Here are some examples of expansion:
4489 -- 1. no discriminants
4490 -- type T2 is new T1 with null record;
4492 -- type T2 is new T1 with record
4496 -- 2. renamed discriminants
4497 -- type T2 (B, C : Int) is new T1 (A => B) with record
4498 -- _Parent : T1 (A => B);
4502 -- 3. inherited discriminants
4503 -- type T2 is new T1 with record -- discriminant A inherited
4504 -- _Parent : T1 (A);
4508 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4509 Indic : constant Node_Id := Subtype_Indication (Def);
4510 Loc : constant Source_Ptr := Sloc (Def);
4511 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
4512 Par_Subtype : Entity_Id;
4513 Comp_List : Node_Id;
4514 Comp_Decl : Node_Id;
4517 List_Constr : constant List_Id := New_List;
4520 -- Expand_Record_Extension is called directly from the semantics, so
4521 -- we must check to see whether expansion is active before proceeding
4523 if not Expander_Active then
4527 -- This may be a derivation of an untagged private type whose full
4528 -- view is tagged, in which case the Derived_Type_Definition has no
4529 -- extension part. Build an empty one now.
4531 if No (Rec_Ext_Part) then
4533 Make_Record_Definition (Loc,
4535 Component_List => Empty,
4536 Null_Present => True);
4538 Set_Record_Extension_Part (Def, Rec_Ext_Part);
4539 Mark_Rewrite_Insertion (Rec_Ext_Part);
4542 Comp_List := Component_List (Rec_Ext_Part);
4544 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4546 -- If the derived type inherits its discriminants the type of the
4547 -- _parent field must be constrained by the inherited discriminants
4549 if Has_Discriminants (T)
4550 and then Nkind (Indic) /= N_Subtype_Indication
4551 and then not Is_Constrained (Entity (Indic))
4553 D := First_Discriminant (T);
4554 while Present (D) loop
4555 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4556 Next_Discriminant (D);
4561 Make_Subtype_Indication (Loc,
4562 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
4564 Make_Index_Or_Discriminant_Constraint (Loc,
4565 Constraints => List_Constr)),
4568 -- Otherwise the original subtype_indication is just what is needed
4571 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4574 Set_Parent_Subtype (T, Par_Subtype);
4577 Make_Component_Declaration (Loc,
4578 Defining_Identifier => Parent_N,
4579 Component_Definition =>
4580 Make_Component_Definition (Loc,
4581 Aliased_Present => False,
4582 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
4584 if Null_Present (Rec_Ext_Part) then
4585 Set_Component_List (Rec_Ext_Part,
4586 Make_Component_List (Loc,
4587 Component_Items => New_List (Comp_Decl),
4588 Variant_Part => Empty,
4589 Null_Present => False));
4590 Set_Null_Present (Rec_Ext_Part, False);
4592 elsif Null_Present (Comp_List)
4593 or else Is_Empty_List (Component_Items (Comp_List))
4595 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4596 Set_Null_Present (Comp_List, False);
4599 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4602 Analyze (Comp_Decl);
4603 end Expand_Record_Extension;
4605 ------------------------------------
4606 -- Expand_N_Full_Type_Declaration --
4607 ------------------------------------
4609 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4611 procedure Build_Master (Ptr_Typ : Entity_Id);
4612 -- Create the master associated with Ptr_Typ
4618 procedure Build_Master (Ptr_Typ : Entity_Id) is
4619 Desig_Typ : constant Entity_Id := Designated_Type (Ptr_Typ);
4622 -- Anonymous access types are created for the components of the
4623 -- record parameter for an entry declaration. No master is created
4626 if Comes_From_Source (N)
4627 and then Has_Task (Desig_Typ)
4629 Build_Master_Entity (Ptr_Typ);
4630 Build_Master_Renaming (Ptr_Typ);
4632 -- Create a class-wide master because a Master_Id must be generated
4633 -- for access-to-limited-class-wide types whose root may be extended
4634 -- with task components.
4636 -- Note: This code covers access-to-limited-interfaces because they
4637 -- can be used to reference tasks implementing them.
4639 elsif Is_Limited_Class_Wide_Type (Desig_Typ)
4640 and then Tasking_Allowed
4642 -- Do not create a class-wide master for types whose convention is
4643 -- Java since these types cannot embed Ada tasks anyway. Note that
4644 -- the following test cannot catch the following case:
4646 -- package java.lang.Object is
4647 -- type Typ is tagged limited private;
4648 -- type Ref is access all Typ'Class;
4650 -- type Typ is tagged limited ...;
4651 -- pragma Convention (Typ, Java)
4654 -- Because the convention appears after we have done the
4655 -- processing for type Ref.
4657 and then Convention (Desig_Typ) /= Convention_Java
4658 and then Convention (Desig_Typ) /= Convention_CIL
4660 Build_Class_Wide_Master (Ptr_Typ);
4664 -- Local declarations
4666 Def_Id : constant Entity_Id := Defining_Identifier (N);
4667 B_Id : constant Entity_Id := Base_Type (Def_Id);
4671 -- Start of processing for Expand_N_Full_Type_Declaration
4674 if Is_Access_Type (Def_Id) then
4675 Build_Master (Def_Id);
4677 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4678 Expand_Access_Protected_Subprogram_Type (N);
4681 -- Array of anonymous access-to-task pointers
4683 elsif Ada_Version >= Ada_2005
4684 and then Is_Array_Type (Def_Id)
4685 and then Is_Access_Type (Component_Type (Def_Id))
4686 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4688 Build_Master (Component_Type (Def_Id));
4690 elsif Has_Task (Def_Id) then
4691 Expand_Previous_Access_Type (Def_Id);
4693 -- Check the components of a record type or array of records for
4694 -- anonymous access-to-task pointers.
4696 elsif Ada_Version >= Ada_2005
4697 and then (Is_Record_Type (Def_Id)
4699 (Is_Array_Type (Def_Id)
4700 and then Is_Record_Type (Component_Type (Def_Id))))
4709 if Is_Array_Type (Def_Id) then
4710 Comp := First_Entity (Component_Type (Def_Id));
4712 Comp := First_Entity (Def_Id);
4715 -- Examine all components looking for anonymous access-to-task
4719 while Present (Comp) loop
4720 Typ := Etype (Comp);
4722 if Ekind (Typ) = E_Anonymous_Access_Type
4723 and then Has_Task (Available_View (Designated_Type (Typ)))
4724 and then No (Master_Id (Typ))
4726 -- Ensure that the record or array type have a _master
4729 Build_Master_Entity (Def_Id);
4730 Build_Master_Renaming (Typ);
4731 M_Id := Master_Id (Typ);
4735 -- Reuse the same master to service any additional types
4738 Set_Master_Id (Typ, M_Id);
4747 Par_Id := Etype (B_Id);
4749 -- The parent type is private then we need to inherit any TSS operations
4750 -- from the full view.
4752 if Ekind (Par_Id) in Private_Kind
4753 and then Present (Full_View (Par_Id))
4755 Par_Id := Base_Type (Full_View (Par_Id));
4758 if Nkind (Type_Definition (Original_Node (N))) =
4759 N_Derived_Type_Definition
4760 and then not Is_Tagged_Type (Def_Id)
4761 and then Present (Freeze_Node (Par_Id))
4762 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4764 Ensure_Freeze_Node (B_Id);
4765 FN := Freeze_Node (B_Id);
4767 if No (TSS_Elist (FN)) then
4768 Set_TSS_Elist (FN, New_Elmt_List);
4772 T_E : constant Elist_Id := TSS_Elist (FN);
4776 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4777 while Present (Elmt) loop
4778 if Chars (Node (Elmt)) /= Name_uInit then
4779 Append_Elmt (Node (Elmt), T_E);
4785 -- If the derived type itself is private with a full view, then
4786 -- associate the full view with the inherited TSS_Elist as well.
4788 if Ekind (B_Id) in Private_Kind
4789 and then Present (Full_View (B_Id))
4791 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4793 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4797 end Expand_N_Full_Type_Declaration;
4799 ---------------------------------
4800 -- Expand_N_Object_Declaration --
4801 ---------------------------------
4803 procedure Expand_N_Object_Declaration (N : Node_Id) is
4804 Def_Id : constant Entity_Id := Defining_Identifier (N);
4805 Expr : constant Node_Id := Expression (N);
4806 Loc : constant Source_Ptr := Sloc (N);
4807 Typ : constant Entity_Id := Etype (Def_Id);
4808 Base_Typ : constant Entity_Id := Base_Type (Typ);
4813 Init_After : Node_Id := N;
4814 -- Node after which the init proc call is to be inserted. This is
4815 -- normally N, except for the case of a shared passive variable, in
4816 -- which case the init proc call must be inserted only after the bodies
4817 -- of the shared variable procedures have been seen.
4819 function Build_Equivalent_Aggregate return Boolean;
4820 -- If the object has a constrained discriminated type and no initial
4821 -- value, it may be possible to build an equivalent aggregate instead,
4822 -- and prevent an actual call to the initialization procedure.
4824 function Rewrite_As_Renaming return Boolean;
4825 -- Indicate whether to rewrite a declaration with initialization into an
4826 -- object renaming declaration (see below).
4828 --------------------------------
4829 -- Build_Equivalent_Aggregate --
4830 --------------------------------
4832 function Build_Equivalent_Aggregate return Boolean is
4836 Full_Type : Entity_Id;
4841 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
4842 Full_Type := Full_View (Typ);
4845 -- Only perform this transformation if Elaboration_Code is forbidden
4846 -- or undesirable, and if this is a global entity of a constrained
4849 -- If Initialize_Scalars might be active this transformation cannot
4850 -- be performed either, because it will lead to different semantics
4851 -- or because elaboration code will in fact be created.
4853 if Ekind (Full_Type) /= E_Record_Subtype
4854 or else not Has_Discriminants (Full_Type)
4855 or else not Is_Constrained (Full_Type)
4856 or else Is_Controlled (Full_Type)
4857 or else Is_Limited_Type (Full_Type)
4858 or else not Restriction_Active (No_Initialize_Scalars)
4863 if Ekind (Current_Scope) = E_Package
4865 (Restriction_Active (No_Elaboration_Code)
4866 or else Is_Preelaborated (Current_Scope))
4869 -- Building a static aggregate is possible if the discriminants
4870 -- have static values and the other components have static
4871 -- defaults or none.
4873 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4874 while Present (Discr) loop
4875 if not Is_OK_Static_Expression (Node (Discr)) then
4882 -- Check that initialized components are OK, and that non-
4883 -- initialized components do not require a call to their own
4884 -- initialization procedure.
4886 Comp := First_Component (Full_Type);
4887 while Present (Comp) loop
4888 if Ekind (Comp) = E_Component
4889 and then Present (Expression (Parent (Comp)))
4891 not Is_OK_Static_Expression (Expression (Parent (Comp)))
4895 elsif Has_Non_Null_Base_Init_Proc (Etype (Comp)) then
4900 Next_Component (Comp);
4903 -- Everything is static, assemble the aggregate, discriminant
4907 Make_Aggregate (Loc,
4908 Expressions => New_List,
4909 Component_Associations => New_List);
4911 Discr := First_Elmt (Discriminant_Constraint (Full_Type));
4912 while Present (Discr) loop
4913 Append_To (Expressions (Aggr), New_Copy (Node (Discr)));
4917 -- Now collect values of initialized components.
4919 Comp := First_Component (Full_Type);
4920 while Present (Comp) loop
4921 if Ekind (Comp) = E_Component
4922 and then Present (Expression (Parent (Comp)))
4924 Append_To (Component_Associations (Aggr),
4925 Make_Component_Association (Loc,
4926 Choices => New_List (New_Occurrence_Of (Comp, Loc)),
4927 Expression => New_Copy_Tree
4928 (Expression (Parent (Comp)))));
4931 Next_Component (Comp);
4934 -- Finally, box-initialize remaining components.
4936 Append_To (Component_Associations (Aggr),
4937 Make_Component_Association (Loc,
4938 Choices => New_List (Make_Others_Choice (Loc)),
4939 Expression => Empty));
4940 Set_Box_Present (Last (Component_Associations (Aggr)));
4941 Set_Expression (N, Aggr);
4943 if Typ /= Full_Type then
4944 Analyze_And_Resolve (Aggr, Full_View (Base_Type (Full_Type)));
4945 Rewrite (Aggr, Unchecked_Convert_To (Typ, Aggr));
4946 Analyze_And_Resolve (Aggr, Typ);
4948 Analyze_And_Resolve (Aggr, Full_Type);
4956 end Build_Equivalent_Aggregate;
4958 -------------------------
4959 -- Rewrite_As_Renaming --
4960 -------------------------
4962 function Rewrite_As_Renaming return Boolean is
4964 return not Aliased_Present (N)
4965 and then Is_Entity_Name (Expr_Q)
4966 and then Ekind (Entity (Expr_Q)) = E_Variable
4967 and then OK_To_Rename (Entity (Expr_Q))
4968 and then Is_Entity_Name (Object_Definition (N));
4969 end Rewrite_As_Renaming;
4971 -- Start of processing for Expand_N_Object_Declaration
4974 -- Don't do anything for deferred constants. All proper actions will be
4975 -- expanded during the full declaration.
4977 if No (Expr) and Constant_Present (N) then
4981 -- First we do special processing for objects of a tagged type where
4982 -- this is the point at which the type is frozen. The creation of the
4983 -- dispatch table and the initialization procedure have to be deferred
4984 -- to this point, since we reference previously declared primitive
4987 -- Force construction of dispatch tables of library level tagged types
4989 if Tagged_Type_Expansion
4990 and then Static_Dispatch_Tables
4991 and then Is_Library_Level_Entity (Def_Id)
4992 and then Is_Library_Level_Tagged_Type (Base_Typ)
4993 and then (Ekind (Base_Typ) = E_Record_Type
4994 or else Ekind (Base_Typ) = E_Protected_Type
4995 or else Ekind (Base_Typ) = E_Task_Type)
4996 and then not Has_Dispatch_Table (Base_Typ)
4999 New_Nodes : List_Id := No_List;
5002 if Is_Concurrent_Type (Base_Typ) then
5003 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
5005 New_Nodes := Make_DT (Base_Typ, N);
5008 if not Is_Empty_List (New_Nodes) then
5009 Insert_List_Before (N, New_Nodes);
5014 -- Make shared memory routines for shared passive variable
5016 if Is_Shared_Passive (Def_Id) then
5017 Init_After := Make_Shared_Var_Procs (N);
5020 -- If tasks being declared, make sure we have an activation chain
5021 -- defined for the tasks (has no effect if we already have one), and
5022 -- also that a Master variable is established and that the appropriate
5023 -- enclosing construct is established as a task master.
5025 if Has_Task (Typ) then
5026 Build_Activation_Chain_Entity (N);
5027 Build_Master_Entity (Def_Id);
5030 -- Default initialization required, and no expression present
5034 -- For the default initialization case, if we have a private type
5035 -- with invariants, and invariant checks are enabled, then insert an
5036 -- invariant check after the object declaration. Note that it is OK
5037 -- to clobber the object with an invalid value since if the exception
5038 -- is raised, then the object will go out of scope.
5040 if Has_Invariants (Typ)
5041 and then Present (Invariant_Procedure (Typ))
5044 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
5047 -- Expand Initialize call for controlled objects. One may wonder why
5048 -- the Initialize Call is not done in the regular Init procedure
5049 -- attached to the record type. That's because the init procedure is
5050 -- recursively called on each component, including _Parent, thus the
5051 -- Init call for a controlled object would generate not only one
5052 -- Initialize call as it is required but one for each ancestor of
5053 -- its type. This processing is suppressed if No_Initialization set.
5055 if not Needs_Finalization (Typ)
5056 or else No_Initialization (N)
5060 elsif not Abort_Allowed
5061 or else not Comes_From_Source (N)
5063 Insert_Action_After (Init_After,
5065 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5066 Typ => Base_Type (Typ)));
5071 -- We need to protect the initialize call
5075 -- Initialize (...);
5077 -- Undefer_Abort.all;
5080 -- ??? this won't protect the initialize call for controlled
5081 -- components which are part of the init proc, so this block
5082 -- should probably also contain the call to _init_proc but this
5083 -- requires some code reorganization...
5086 L : constant List_Id := New_List (
5088 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
5089 Typ => Base_Type (Typ)));
5091 Blk : constant Node_Id :=
5092 Make_Block_Statement (Loc,
5093 Handled_Statement_Sequence =>
5094 Make_Handled_Sequence_Of_Statements (Loc, L));
5097 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
5098 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
5099 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
5100 Insert_Actions_After (Init_After, New_List (Blk));
5101 Expand_At_End_Handler
5102 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
5106 -- Call type initialization procedure if there is one. We build the
5107 -- call and put it immediately after the object declaration, so that
5108 -- it will be expanded in the usual manner. Note that this will
5109 -- result in proper handling of defaulted discriminants.
5111 -- Need call if there is a base init proc
5113 if Has_Non_Null_Base_Init_Proc (Typ)
5115 -- Suppress call if No_Initialization set on declaration
5117 and then not No_Initialization (N)
5119 -- Suppress call for special case of value type for VM
5121 and then not Is_Value_Type (Typ)
5123 -- Suppress call if initialization suppressed for the type
5125 and then not Initialization_Suppressed (Typ)
5127 -- Return without initializing when No_Default_Initialization
5128 -- applies. Note that the actual restriction check occurs later,
5129 -- when the object is frozen, because we don't know yet whether
5130 -- the object is imported, which is a case where the check does
5133 if Restriction_Active (No_Default_Initialization) then
5137 -- The call to the initialization procedure does NOT freeze the
5138 -- object being initialized. This is because the call is not a
5139 -- source level call. This works fine, because the only possible
5140 -- statements depending on freeze status that can appear after the
5141 -- Init_Proc call are rep clauses which can safely appear after
5142 -- actual references to the object. Note that this call may
5143 -- subsequently be removed (if a pragma Import is encountered),
5144 -- or moved to the freeze actions for the object (e.g. if an
5145 -- address clause is applied to the object, causing it to get
5146 -- delayed freezing).
5148 Id_Ref := New_Reference_To (Def_Id, Loc);
5149 Set_Must_Not_Freeze (Id_Ref);
5150 Set_Assignment_OK (Id_Ref);
5153 Init_Expr : constant Node_Id :=
5154 Static_Initialization (Base_Init_Proc (Typ));
5157 if Present (Init_Expr) then
5159 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
5162 -- If type has discriminants, try to build equivalent aggregate
5163 -- using discriminant values from the declaration. This
5164 -- is a useful optimization, in particular if restriction
5165 -- No_Elaboration_Code is active.
5167 elsif Build_Equivalent_Aggregate then
5171 Initialization_Warning (Id_Ref);
5173 Insert_Actions_After (Init_After,
5174 Build_Initialization_Call (Loc, Id_Ref, Typ));
5178 -- If simple initialization is required, then set an appropriate
5179 -- simple initialization expression in place. This special
5180 -- initialization is required even though No_Init_Flag is present,
5181 -- but is not needed if there was an explicit initialization.
5183 -- An internally generated temporary needs no initialization because
5184 -- it will be assigned subsequently. In particular, there is no point
5185 -- in applying Initialize_Scalars to such a temporary.
5187 elsif Needs_Simple_Initialization
5190 and then not Has_Following_Address_Clause (N))
5191 and then not Is_Internal (Def_Id)
5192 and then not Has_Init_Expression (N)
5194 Set_No_Initialization (N, False);
5195 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
5196 Analyze_And_Resolve (Expression (N), Typ);
5199 -- Generate attribute for Persistent_BSS if needed
5201 if Persistent_BSS_Mode
5202 and then Comes_From_Source (N)
5203 and then Is_Potentially_Persistent_Type (Typ)
5204 and then not Has_Init_Expression (N)
5205 and then Is_Library_Level_Entity (Def_Id)
5211 Make_Linker_Section_Pragma
5212 (Def_Id, Sloc (N), ".persistent.bss");
5213 Insert_After (N, Prag);
5218 -- If access type, then we know it is null if not initialized
5220 if Is_Access_Type (Typ) then
5221 Set_Is_Known_Null (Def_Id);
5224 -- Explicit initialization present
5227 -- Obtain actual expression from qualified expression
5229 if Nkind (Expr) = N_Qualified_Expression then
5230 Expr_Q := Expression (Expr);
5235 -- When we have the appropriate type of aggregate in the expression
5236 -- (it has been determined during analysis of the aggregate by
5237 -- setting the delay flag), let's perform in place assignment and
5238 -- thus avoid creating a temporary.
5240 if Is_Delayed_Aggregate (Expr_Q) then
5241 Convert_Aggr_In_Object_Decl (N);
5243 -- Ada 2005 (AI-318-02): If the initialization expression is a call
5244 -- to a build-in-place function, then access to the declared object
5245 -- must be passed to the function. Currently we limit such functions
5246 -- to those with constrained limited result subtypes, but eventually
5247 -- plan to expand the allowed forms of functions that are treated as
5250 elsif Ada_Version >= Ada_2005
5251 and then Is_Build_In_Place_Function_Call (Expr_Q)
5253 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
5255 -- The previous call expands the expression initializing the
5256 -- built-in-place object into further code that will be analyzed
5257 -- later. No further expansion needed here.
5261 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
5262 -- class-wide interface object to ensure that we copy the full
5263 -- object, unless we are targetting a VM where interfaces are handled
5264 -- by VM itself. Note that if the root type of Typ is an ancestor of
5265 -- Expr's type, both types share the same dispatch table and there is
5266 -- no need to displace the pointer.
5268 elsif Is_Interface (Typ)
5270 -- Avoid never-ending recursion because if Equivalent_Type is set
5271 -- then we've done it already and must not do it again!
5274 (Nkind (Object_Definition (N)) = N_Identifier
5276 Present (Equivalent_Type (Entity (Object_Definition (N)))))
5278 pragma Assert (Is_Class_Wide_Type (Typ));
5280 -- If the object is a return object of an inherently limited type,
5281 -- which implies build-in-place treatment, bypass the special
5282 -- treatment of class-wide interface initialization below. In this
5283 -- case, the expansion of the return statement will take care of
5284 -- creating the object (via allocator) and initializing it.
5286 if Is_Return_Object (Def_Id)
5287 and then Is_Immutably_Limited_Type (Typ)
5291 elsif Tagged_Type_Expansion then
5293 Iface : constant Entity_Id := Root_Type (Typ);
5294 Expr_N : Node_Id := Expr;
5295 Expr_Typ : Entity_Id;
5301 -- If the original node of the expression was a conversion
5302 -- to this specific class-wide interface type then restore
5303 -- the original node because we must copy the object before
5304 -- displacing the pointer to reference the secondary tag
5305 -- component. This code must be kept synchronized with the
5306 -- expansion done by routine Expand_Interface_Conversion
5308 if not Comes_From_Source (Expr_N)
5309 and then Nkind (Expr_N) = N_Explicit_Dereference
5310 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
5311 and then Etype (Original_Node (Expr_N)) = Typ
5313 Rewrite (Expr_N, Original_Node (Expression (N)));
5316 -- Avoid expansion of redundant interface conversion
5318 if Is_Interface (Etype (Expr_N))
5319 and then Nkind (Expr_N) = N_Type_Conversion
5320 and then Etype (Expr_N) = Typ
5322 Expr_N := Expression (Expr_N);
5323 Set_Expression (N, Expr_N);
5326 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
5327 Expr_Typ := Base_Type (Etype (Expr_N));
5329 if Is_Class_Wide_Type (Expr_Typ) then
5330 Expr_Typ := Root_Type (Expr_Typ);
5334 -- CW : I'Class := Obj;
5337 -- type Ityp is not null access I'Class;
5338 -- CW : I'Class renames Ityp(Tmp.I_Tag'Address).all;
5340 if Comes_From_Source (Expr_N)
5341 and then Nkind (Expr_N) = N_Identifier
5342 and then not Is_Interface (Expr_Typ)
5343 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
5344 and then (Expr_Typ = Etype (Expr_Typ)
5346 Is_Variable_Size_Record (Etype (Expr_Typ)))
5351 Make_Object_Declaration (Loc,
5352 Defining_Identifier => Obj_Id,
5353 Object_Definition =>
5354 New_Occurrence_Of (Expr_Typ, Loc),
5356 Relocate_Node (Expr_N)));
5358 -- Statically reference the tag associated with the
5362 Make_Selected_Component (Loc,
5363 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5366 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
5369 -- IW : I'Class := Obj;
5371 -- type Equiv_Record is record ... end record;
5372 -- implicit subtype CW is <Class_Wide_Subtype>;
5373 -- Tmp : CW := CW!(Obj);
5374 -- type Ityp is not null access I'Class;
5375 -- IW : I'Class renames
5376 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
5379 -- Generate the equivalent record type and update the
5380 -- subtype indication to reference it.
5382 Expand_Subtype_From_Expr
5385 Subtype_Indic => Object_Definition (N),
5388 if not Is_Interface (Etype (Expr_N)) then
5389 New_Expr := Relocate_Node (Expr_N);
5391 -- For interface types we use 'Address which displaces
5392 -- the pointer to the base of the object (if required)
5396 Unchecked_Convert_To (Etype (Object_Definition (N)),
5397 Make_Explicit_Dereference (Loc,
5398 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
5399 Make_Attribute_Reference (Loc,
5400 Prefix => Relocate_Node (Expr_N),
5401 Attribute_Name => Name_Address))));
5406 if not Is_Limited_Record (Expr_Typ) then
5408 Make_Object_Declaration (Loc,
5409 Defining_Identifier => Obj_Id,
5410 Object_Definition =>
5412 (Etype (Object_Definition (N)), Loc),
5413 Expression => New_Expr));
5415 -- Rename limited type object since they cannot be copied
5416 -- This case occurs when the initialization expression
5417 -- has been previously expanded into a temporary object.
5419 else pragma Assert (not Comes_From_Source (Expr_Q));
5421 Make_Object_Renaming_Declaration (Loc,
5422 Defining_Identifier => Obj_Id,
5425 (Etype (Object_Definition (N)), Loc),
5427 Unchecked_Convert_To
5428 (Etype (Object_Definition (N)), New_Expr)));
5431 -- Dynamically reference the tag associated with the
5435 Make_Function_Call (Loc,
5436 Name => New_Reference_To (RTE (RE_Displace), Loc),
5437 Parameter_Associations => New_List (
5438 Make_Attribute_Reference (Loc,
5439 Prefix => New_Occurrence_Of (Obj_Id, Loc),
5440 Attribute_Name => Name_Address),
5442 (Node (First_Elmt (Access_Disp_Table (Iface))),
5447 Make_Object_Renaming_Declaration (Loc,
5448 Defining_Identifier => Make_Temporary (Loc, 'D'),
5449 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5450 Name => Convert_Tag_To_Interface (Typ, Tag_Comp)));
5452 -- If the original entity comes from source, then mark the
5453 -- new entity as needing debug information, even though it's
5454 -- defined by a generated renaming that does not come from
5455 -- source, so that Materialize_Entity will be set on the
5456 -- entity when Debug_Renaming_Declaration is called during
5459 if Comes_From_Source (Def_Id) then
5460 Set_Debug_Info_Needed (Defining_Identifier (N));
5463 Analyze (N, Suppress => All_Checks);
5465 -- Replace internal identifier of rewritten node by the
5466 -- identifier found in the sources. We also have to exchange
5467 -- entities containing their defining identifiers to ensure
5468 -- the correct replacement of the object declaration by this
5469 -- object renaming declaration because these identifiers
5470 -- were previously added by Enter_Name to the current scope.
5471 -- We must preserve the homonym chain of the source entity
5472 -- as well. We must also preserve the kind of the entity,
5473 -- which may be a constant. Preserve entity chain because
5474 -- itypes may have been generated already, and the full
5475 -- chain must be preserved for final freezing. Finally,
5476 -- preserve Comes_From_Source setting, so that debugging
5477 -- and cross-referencing information is properly kept.
5480 New_Id : constant Entity_Id := Defining_Identifier (N);
5481 Next_Temp : constant Entity_Id := Next_Entity (New_Id);
5482 S_Flag : constant Boolean :=
5483 Comes_From_Source (Def_Id);
5486 Set_Next_Entity (New_Id, Next_Entity (Def_Id));
5487 Set_Next_Entity (Def_Id, Next_Temp);
5489 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
5490 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
5491 Set_Ekind (Defining_Identifier (N), Ekind (Def_Id));
5493 Set_Comes_From_Source (Def_Id, False);
5494 Exchange_Entities (Defining_Identifier (N), Def_Id);
5495 Set_Comes_From_Source (Def_Id, S_Flag);
5502 -- Common case of explicit object initialization
5505 -- In most cases, we must check that the initial value meets any
5506 -- constraint imposed by the declared type. However, there is one
5507 -- very important exception to this rule. If the entity has an
5508 -- unconstrained nominal subtype, then it acquired its constraints
5509 -- from the expression in the first place, and not only does this
5510 -- mean that the constraint check is not needed, but an attempt to
5511 -- perform the constraint check can cause order of elaboration
5514 if not Is_Constr_Subt_For_U_Nominal (Typ) then
5516 -- If this is an allocator for an aggregate that has been
5517 -- allocated in place, delay checks until assignments are
5518 -- made, because the discriminants are not initialized.
5520 if Nkind (Expr) = N_Allocator
5521 and then No_Initialization (Expr)
5525 -- Otherwise apply a constraint check now if no prev error
5527 elsif Nkind (Expr) /= N_Error then
5528 Apply_Constraint_Check (Expr, Typ);
5530 -- If the expression has been marked as requiring a range
5531 -- generate it now and reset the flag.
5533 if Do_Range_Check (Expr) then
5534 Set_Do_Range_Check (Expr, False);
5536 if not Suppress_Assignment_Checks (N) then
5537 Generate_Range_Check
5538 (Expr, Typ, CE_Range_Check_Failed);
5544 -- If the type is controlled and not inherently limited, then
5545 -- the target is adjusted after the copy and attached to the
5546 -- finalization list. However, no adjustment is done in the case
5547 -- where the object was initialized by a call to a function whose
5548 -- result is built in place, since no copy occurred. (Eventually
5549 -- we plan to support in-place function results for some cases
5550 -- of nonlimited types. ???) Similarly, no adjustment is required
5551 -- if we are going to rewrite the object declaration into a
5552 -- renaming declaration.
5554 if Needs_Finalization (Typ)
5555 and then not Is_Immutably_Limited_Type (Typ)
5556 and then not Rewrite_As_Renaming
5558 Insert_Action_After (Init_After,
5560 Obj_Ref => New_Reference_To (Def_Id, Loc),
5561 Typ => Base_Type (Typ)));
5564 -- For tagged types, when an init value is given, the tag has to
5565 -- be re-initialized separately in order to avoid the propagation
5566 -- of a wrong tag coming from a view conversion unless the type
5567 -- is class wide (in this case the tag comes from the init value).
5568 -- Suppress the tag assignment when VM_Target because VM tags are
5569 -- represented implicitly in objects. Ditto for types that are
5570 -- CPP_CLASS, and for initializations that are aggregates, because
5571 -- they have to have the right tag.
5573 if Is_Tagged_Type (Typ)
5574 and then not Is_Class_Wide_Type (Typ)
5575 and then not Is_CPP_Class (Typ)
5576 and then Tagged_Type_Expansion
5577 and then Nkind (Expr) /= N_Aggregate
5578 and then (Nkind (Expr) /= N_Qualified_Expression
5579 or else Nkind (Expression (Expr)) /= N_Aggregate)
5582 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
5585 -- The re-assignment of the tag has to be done even if the
5586 -- object is a constant. The assignment must be analyzed
5587 -- after the declaration.
5590 Make_Selected_Component (Loc,
5591 Prefix => New_Occurrence_Of (Def_Id, Loc),
5593 New_Reference_To (First_Tag_Component (Full_Typ),
5595 Set_Assignment_OK (New_Ref);
5597 Insert_Action_After (Init_After,
5598 Make_Assignment_Statement (Loc,
5601 Unchecked_Convert_To (RTE (RE_Tag),
5603 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
5607 -- Handle C++ constructor calls. Note that we do not check that
5608 -- Typ is a tagged type since the equivalent Ada type of a C++
5609 -- class that has no virtual methods is a non-tagged limited
5612 elsif Is_CPP_Constructor_Call (Expr) then
5614 -- The call to the initialization procedure does NOT freeze the
5615 -- object being initialized.
5617 Id_Ref := New_Reference_To (Def_Id, Loc);
5618 Set_Must_Not_Freeze (Id_Ref);
5619 Set_Assignment_OK (Id_Ref);
5621 Insert_Actions_After (Init_After,
5622 Build_Initialization_Call (Loc, Id_Ref, Typ,
5623 Constructor_Ref => Expr));
5625 -- We remove here the original call to the constructor
5626 -- to avoid its management in the backend
5628 Set_Expression (N, Empty);
5631 -- For discrete types, set the Is_Known_Valid flag if the
5632 -- initializing value is known to be valid.
5634 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
5635 Set_Is_Known_Valid (Def_Id);
5637 elsif Is_Access_Type (Typ) then
5639 -- For access types set the Is_Known_Non_Null flag if the
5640 -- initializing value is known to be non-null. We can also set
5641 -- Can_Never_Be_Null if this is a constant.
5643 if Known_Non_Null (Expr) then
5644 Set_Is_Known_Non_Null (Def_Id, True);
5646 if Constant_Present (N) then
5647 Set_Can_Never_Be_Null (Def_Id);
5652 -- If validity checking on copies, validate initial expression.
5653 -- But skip this if declaration is for a generic type, since it
5654 -- makes no sense to validate generic types. Not clear if this
5655 -- can happen for legal programs, but it definitely can arise
5656 -- from previous instantiation errors.
5658 if Validity_Checks_On
5659 and then Validity_Check_Copies
5660 and then not Is_Generic_Type (Etype (Def_Id))
5662 Ensure_Valid (Expr);
5663 Set_Is_Known_Valid (Def_Id);
5667 -- Cases where the back end cannot handle the initialization directly
5668 -- In such cases, we expand an assignment that will be appropriately
5669 -- handled by Expand_N_Assignment_Statement.
5671 -- The exclusion of the unconstrained case is wrong, but for now it
5672 -- is too much trouble ???
5674 if (Is_Possibly_Unaligned_Slice (Expr)
5675 or else (Is_Possibly_Unaligned_Object (Expr)
5676 and then not Represented_As_Scalar (Etype (Expr))))
5677 and then not (Is_Array_Type (Etype (Expr))
5678 and then not Is_Constrained (Etype (Expr)))
5681 Stat : constant Node_Id :=
5682 Make_Assignment_Statement (Loc,
5683 Name => New_Reference_To (Def_Id, Loc),
5684 Expression => Relocate_Node (Expr));
5686 Set_Expression (N, Empty);
5687 Set_No_Initialization (N);
5688 Set_Assignment_OK (Name (Stat));
5689 Set_No_Ctrl_Actions (Stat);
5690 Insert_After_And_Analyze (Init_After, Stat);
5694 -- Final transformation, if the initializing expression is an entity
5695 -- for a variable with OK_To_Rename set, then we transform:
5701 -- X : typ renames expr
5703 -- provided that X is not aliased. The aliased case has to be
5704 -- excluded in general because Expr will not be aliased in general.
5706 if Rewrite_As_Renaming then
5708 Make_Object_Renaming_Declaration (Loc,
5709 Defining_Identifier => Defining_Identifier (N),
5710 Subtype_Mark => Object_Definition (N),
5713 -- We do not analyze this renaming declaration, because all its
5714 -- components have already been analyzed, and if we were to go
5715 -- ahead and analyze it, we would in effect be trying to generate
5716 -- another declaration of X, which won't do!
5718 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5721 -- We do need to deal with debug issues for this renaming
5723 -- First, if entity comes from source, then mark it as needing
5724 -- debug information, even though it is defined by a generated
5725 -- renaming that does not come from source.
5727 if Comes_From_Source (Defining_Identifier (N)) then
5728 Set_Debug_Info_Needed (Defining_Identifier (N));
5731 -- Now call the routine to generate debug info for the renaming
5734 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
5736 if Present (Decl) then
5737 Insert_Action (N, Decl);
5743 if Nkind (N) = N_Object_Declaration
5744 and then Nkind (Object_Definition (N)) = N_Access_Definition
5745 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
5747 -- An Ada 2012 stand-alone object of an anonymous access type
5750 Loc : constant Source_Ptr := Sloc (N);
5752 Level : constant Entity_Id :=
5753 Make_Defining_Identifier (Sloc (N),
5755 New_External_Name (Chars (Def_Id), Suffix => "L"));
5757 Level_Expr : Node_Id;
5758 Level_Decl : Node_Id;
5761 Set_Ekind (Level, Ekind (Def_Id));
5762 Set_Etype (Level, Standard_Natural);
5763 Set_Scope (Level, Scope (Def_Id));
5767 -- Set accessibility level of null
5770 Make_Integer_Literal (Loc, Scope_Depth (Standard_Standard));
5773 Level_Expr := Dynamic_Accessibility_Level (Expr);
5776 Level_Decl := Make_Object_Declaration (Loc,
5777 Defining_Identifier => Level,
5778 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc),
5779 Expression => Level_Expr,
5780 Constant_Present => Constant_Present (N),
5781 Has_Init_Expression => True);
5783 Insert_Action_After (Init_After, Level_Decl);
5785 Set_Extra_Accessibility (Def_Id, Level);
5789 -- Exception on library entity not available
5792 when RE_Not_Available =>
5794 end Expand_N_Object_Declaration;
5796 ---------------------------------
5797 -- Expand_N_Subtype_Indication --
5798 ---------------------------------
5800 -- Add a check on the range of the subtype. The static case is partially
5801 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
5802 -- to check here for the static case in order to avoid generating
5803 -- extraneous expanded code. Also deal with validity checking.
5805 procedure Expand_N_Subtype_Indication (N : Node_Id) is
5806 Ran : constant Node_Id := Range_Expression (Constraint (N));
5807 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
5810 if Nkind (Constraint (N)) = N_Range_Constraint then
5811 Validity_Check_Range (Range_Expression (Constraint (N)));
5814 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
5815 Apply_Range_Check (Ran, Typ);
5817 end Expand_N_Subtype_Indication;
5819 ---------------------------
5820 -- Expand_N_Variant_Part --
5821 ---------------------------
5823 -- If the last variant does not contain the Others choice, replace it with
5824 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
5825 -- do not bother to call Analyze on the modified variant part, since its
5826 -- only effect would be to compute the Others_Discrete_Choices node
5827 -- laboriously, and of course we already know the list of choices that
5828 -- corresponds to the others choice (it's the list we are replacing!)
5830 procedure Expand_N_Variant_Part (N : Node_Id) is
5831 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
5832 Others_Node : Node_Id;
5834 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
5835 Others_Node := Make_Others_Choice (Sloc (Last_Var));
5836 Set_Others_Discrete_Choices
5837 (Others_Node, Discrete_Choices (Last_Var));
5838 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
5840 end Expand_N_Variant_Part;
5842 ---------------------------------
5843 -- Expand_Previous_Access_Type --
5844 ---------------------------------
5846 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
5847 Ptr_Typ : Entity_Id;
5850 -- Find all access types in the current scope whose designated type is
5851 -- Def_Id and build master renamings for them.
5853 Ptr_Typ := First_Entity (Current_Scope);
5854 while Present (Ptr_Typ) loop
5855 if Is_Access_Type (Ptr_Typ)
5856 and then Designated_Type (Ptr_Typ) = Def_Id
5857 and then No (Master_Id (Ptr_Typ))
5859 -- Ensure that the designated type has a master
5861 Build_Master_Entity (Def_Id);
5863 -- Private and incomplete types complicate the insertion of master
5864 -- renamings because the access type may precede the full view of
5865 -- the designated type. For this reason, the master renamings are
5866 -- inserted relative to the designated type.
5868 Build_Master_Renaming (Ptr_Typ, Ins_Nod => Parent (Def_Id));
5871 Next_Entity (Ptr_Typ);
5873 end Expand_Previous_Access_Type;
5875 ------------------------
5876 -- Expand_Tagged_Root --
5877 ------------------------
5879 procedure Expand_Tagged_Root (T : Entity_Id) is
5880 Def : constant Node_Id := Type_Definition (Parent (T));
5881 Comp_List : Node_Id;
5882 Comp_Decl : Node_Id;
5883 Sloc_N : Source_Ptr;
5886 if Null_Present (Def) then
5887 Set_Component_List (Def,
5888 Make_Component_List (Sloc (Def),
5889 Component_Items => Empty_List,
5890 Variant_Part => Empty,
5891 Null_Present => True));
5894 Comp_List := Component_List (Def);
5896 if Null_Present (Comp_List)
5897 or else Is_Empty_List (Component_Items (Comp_List))
5899 Sloc_N := Sloc (Comp_List);
5901 Sloc_N := Sloc (First (Component_Items (Comp_List)));
5905 Make_Component_Declaration (Sloc_N,
5906 Defining_Identifier => First_Tag_Component (T),
5907 Component_Definition =>
5908 Make_Component_Definition (Sloc_N,
5909 Aliased_Present => False,
5910 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5912 if Null_Present (Comp_List)
5913 or else Is_Empty_List (Component_Items (Comp_List))
5915 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5916 Set_Null_Present (Comp_List, False);
5919 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5922 -- We don't Analyze the whole expansion because the tag component has
5923 -- already been analyzed previously. Here we just insure that the tree
5924 -- is coherent with the semantic decoration
5926 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5929 when RE_Not_Available =>
5931 end Expand_Tagged_Root;
5933 ----------------------
5934 -- Clean_Task_Names --
5935 ----------------------
5937 procedure Clean_Task_Names
5939 Proc_Id : Entity_Id)
5943 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5944 and then not Global_Discard_Names
5945 and then Tagged_Type_Expansion
5947 Set_Uses_Sec_Stack (Proc_Id);
5949 end Clean_Task_Names;
5951 ------------------------------
5952 -- Expand_Freeze_Array_Type --
5953 ------------------------------
5955 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5956 Typ : constant Entity_Id := Entity (N);
5957 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5958 Base : constant Entity_Id := Base_Type (Typ);
5961 if not Is_Bit_Packed_Array (Typ) then
5963 -- If the component contains tasks, so does the array type. This may
5964 -- not be indicated in the array type because the component may have
5965 -- been a private type at the point of definition. Same if component
5966 -- type is controlled.
5968 Set_Has_Task (Base, Has_Task (Comp_Typ));
5969 Set_Has_Controlled_Component (Base,
5970 Has_Controlled_Component (Comp_Typ)
5971 or else Is_Controlled (Comp_Typ));
5973 if No (Init_Proc (Base)) then
5975 -- If this is an anonymous array created for a declaration with
5976 -- an initial value, its init_proc will never be called. The
5977 -- initial value itself may have been expanded into assignments,
5978 -- in which case the object declaration is carries the
5979 -- No_Initialization flag.
5982 and then Nkind (Associated_Node_For_Itype (Base)) =
5983 N_Object_Declaration
5984 and then (Present (Expression (Associated_Node_For_Itype (Base)))
5986 No_Initialization (Associated_Node_For_Itype (Base)))
5990 -- We do not need an init proc for string or wide [wide] string,
5991 -- since the only time these need initialization in normalize or
5992 -- initialize scalars mode, and these types are treated specially
5993 -- and do not need initialization procedures.
5995 elsif Root_Type (Base) = Standard_String
5996 or else Root_Type (Base) = Standard_Wide_String
5997 or else Root_Type (Base) = Standard_Wide_Wide_String
6001 -- Otherwise we have to build an init proc for the subtype
6004 Build_Array_Init_Proc (Base, N);
6009 if Has_Controlled_Component (Base) then
6010 Build_Controlling_Procs (Base);
6012 if not Is_Limited_Type (Comp_Typ)
6013 and then Number_Dimensions (Typ) = 1
6015 Build_Slice_Assignment (Typ);
6019 -- Create a finalization master to service the anonymous access
6020 -- components of the array.
6022 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6023 and then Needs_Finalization (Designated_Type (Comp_Typ))
6025 Build_Finalization_Master
6027 Ins_Node => Parent (Typ),
6028 Encl_Scope => Scope (Typ));
6032 -- For packed case, default initialization, except if the component type
6033 -- is itself a packed structure with an initialization procedure, or
6034 -- initialize/normalize scalars active, and we have a base type, or the
6035 -- type is public, because in that case a client might specify
6036 -- Normalize_Scalars and there better be a public Init_Proc for it.
6038 elsif (Present (Init_Proc (Component_Type (Base)))
6039 and then No (Base_Init_Proc (Base)))
6040 or else (Init_Or_Norm_Scalars and then Base = Typ)
6041 or else Is_Public (Typ)
6043 Build_Array_Init_Proc (Base, N);
6046 if Has_Invariants (Component_Type (Base))
6047 and then In_Open_Scopes (Scope (Component_Type (Base)))
6049 -- Generate component invariant checking procedure. This is only
6050 -- relevant if the array type is within the scope of the component
6051 -- type. Otherwise an array object can only be built using the public
6052 -- subprograms for the component type, and calls to those will have
6053 -- invariant checks.
6055 Insert_Component_Invariant_Checks
6056 (N, Base, Build_Array_Invariant_Proc (Base, N));
6058 end Expand_Freeze_Array_Type;
6060 -----------------------------------
6061 -- Expand_Freeze_Class_Wide_Type --
6062 -----------------------------------
6064 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
6065 Typ : constant Entity_Id := Entity (N);
6066 Root : constant Entity_Id := Root_Type (Typ);
6068 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
6069 -- Given a type, determine whether it is derived from a C or C++ root
6071 ---------------------
6072 -- Is_C_Derivation --
6073 ---------------------
6075 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
6076 T : Entity_Id := Typ;
6081 or else Convention (T) = Convention_C
6082 or else Convention (T) = Convention_CPP
6087 exit when T = Etype (T);
6093 end Is_C_Derivation;
6095 -- Start of processing for Expand_Freeze_Class_Wide_Type
6098 -- Certain run-time configurations and targets do not provide support
6099 -- for controlled types.
6101 if Restriction_Active (No_Finalization) then
6104 -- Do not create TSS routine Finalize_Address when dispatching calls are
6105 -- disabled since the core of the routine is a dispatching call.
6107 elsif Restriction_Active (No_Dispatching_Calls) then
6110 -- Do not create TSS routine Finalize_Address for concurrent class-wide
6111 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
6112 -- non-Ada side will handle their destruction.
6114 elsif Is_Concurrent_Type (Root)
6115 or else Is_C_Derivation (Root)
6116 or else Convention (Typ) = Convention_CIL
6117 or else Convention (Typ) = Convention_CPP
6118 or else Convention (Typ) = Convention_Java
6122 -- Do not create TSS routine Finalize_Address for .NET/JVM because these
6123 -- targets do not support address arithmetic and unchecked conversions.
6125 elsif VM_Target /= No_VM then
6128 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
6129 -- mode since the routine contains an Unchecked_Conversion.
6131 elsif CodePeer_Mode then
6134 -- Do not create TSS routine Finalize_Address when compiling in Alfa
6135 -- mode because it is not necessary and results in useless expansion.
6137 elsif Alfa_Mode then
6141 -- Create the body of TSS primitive Finalize_Address. This automatically
6142 -- sets the TSS entry for the class-wide type.
6144 Make_Finalize_Address_Body (Typ);
6145 end Expand_Freeze_Class_Wide_Type;
6147 ------------------------------------
6148 -- Expand_Freeze_Enumeration_Type --
6149 ------------------------------------
6151 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
6152 Typ : constant Entity_Id := Entity (N);
6153 Loc : constant Source_Ptr := Sloc (Typ);
6160 Is_Contiguous : Boolean;
6165 pragma Warnings (Off, Func);
6168 -- Various optimizations possible if given representation is contiguous
6170 Is_Contiguous := True;
6172 Ent := First_Literal (Typ);
6173 Last_Repval := Enumeration_Rep (Ent);
6176 while Present (Ent) loop
6177 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
6178 Is_Contiguous := False;
6181 Last_Repval := Enumeration_Rep (Ent);
6187 if Is_Contiguous then
6188 Set_Has_Contiguous_Rep (Typ);
6189 Ent := First_Literal (Typ);
6191 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
6194 -- Build list of literal references
6199 Ent := First_Literal (Typ);
6200 while Present (Ent) loop
6201 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
6207 -- Now build an array declaration
6209 -- typA : array (Natural range 0 .. num - 1) of ctype :=
6210 -- (v, v, v, v, v, ....)
6212 -- where ctype is the corresponding integer type. If the representation
6213 -- is contiguous, we only keep the first literal, which provides the
6214 -- offset for Pos_To_Rep computations.
6217 Make_Defining_Identifier (Loc,
6218 Chars => New_External_Name (Chars (Typ), 'A'));
6220 Append_Freeze_Action (Typ,
6221 Make_Object_Declaration (Loc,
6222 Defining_Identifier => Arr,
6223 Constant_Present => True,
6225 Object_Definition =>
6226 Make_Constrained_Array_Definition (Loc,
6227 Discrete_Subtype_Definitions => New_List (
6228 Make_Subtype_Indication (Loc,
6229 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
6231 Make_Range_Constraint (Loc,
6235 Make_Integer_Literal (Loc, 0),
6237 Make_Integer_Literal (Loc, Num - 1))))),
6239 Component_Definition =>
6240 Make_Component_Definition (Loc,
6241 Aliased_Present => False,
6242 Subtype_Indication => New_Reference_To (Typ, Loc))),
6245 Make_Aggregate (Loc,
6246 Expressions => Lst)));
6248 Set_Enum_Pos_To_Rep (Typ, Arr);
6250 -- Now we build the function that converts representation values to
6251 -- position values. This function has the form:
6253 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
6256 -- when enum-lit'Enum_Rep => return posval;
6257 -- when enum-lit'Enum_Rep => return posval;
6260 -- [raise Constraint_Error when F "invalid data"]
6265 -- Note: the F parameter determines whether the others case (no valid
6266 -- representation) raises Constraint_Error or returns a unique value
6267 -- of minus one. The latter case is used, e.g. in 'Valid code.
6269 -- Note: the reason we use Enum_Rep values in the case here is to avoid
6270 -- the code generator making inappropriate assumptions about the range
6271 -- of the values in the case where the value is invalid. ityp is a
6272 -- signed or unsigned integer type of appropriate width.
6274 -- Note: if exceptions are not supported, then we suppress the raise
6275 -- and return -1 unconditionally (this is an erroneous program in any
6276 -- case and there is no obligation to raise Constraint_Error here!) We
6277 -- also do this if pragma Restrictions (No_Exceptions) is active.
6279 -- Is this right??? What about No_Exception_Propagation???
6281 -- Representations are signed
6283 if Enumeration_Rep (First_Literal (Typ)) < 0 then
6285 -- The underlying type is signed. Reset the Is_Unsigned_Type
6286 -- explicitly, because it might have been inherited from
6289 Set_Is_Unsigned_Type (Typ, False);
6291 if Esize (Typ) <= Standard_Integer_Size then
6292 Ityp := Standard_Integer;
6294 Ityp := Universal_Integer;
6297 -- Representations are unsigned
6300 if Esize (Typ) <= Standard_Integer_Size then
6301 Ityp := RTE (RE_Unsigned);
6303 Ityp := RTE (RE_Long_Long_Unsigned);
6307 -- The body of the function is a case statement. First collect case
6308 -- alternatives, or optimize the contiguous case.
6312 -- If representation is contiguous, Pos is computed by subtracting
6313 -- the representation of the first literal.
6315 if Is_Contiguous then
6316 Ent := First_Literal (Typ);
6318 if Enumeration_Rep (Ent) = Last_Repval then
6320 -- Another special case: for a single literal, Pos is zero
6322 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
6326 Convert_To (Standard_Integer,
6327 Make_Op_Subtract (Loc,
6329 Unchecked_Convert_To
6330 (Ityp, Make_Identifier (Loc, Name_uA)),
6332 Make_Integer_Literal (Loc,
6333 Intval => Enumeration_Rep (First_Literal (Typ)))));
6337 Make_Case_Statement_Alternative (Loc,
6338 Discrete_Choices => New_List (
6339 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
6341 Make_Integer_Literal (Loc,
6342 Intval => Enumeration_Rep (Ent)),
6344 Make_Integer_Literal (Loc, Intval => Last_Repval))),
6346 Statements => New_List (
6347 Make_Simple_Return_Statement (Loc,
6348 Expression => Pos_Expr))));
6351 Ent := First_Literal (Typ);
6352 while Present (Ent) loop
6354 Make_Case_Statement_Alternative (Loc,
6355 Discrete_Choices => New_List (
6356 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
6357 Intval => Enumeration_Rep (Ent))),
6359 Statements => New_List (
6360 Make_Simple_Return_Statement (Loc,
6362 Make_Integer_Literal (Loc,
6363 Intval => Enumeration_Pos (Ent))))));
6369 -- In normal mode, add the others clause with the test
6371 if not No_Exception_Handlers_Set then
6373 Make_Case_Statement_Alternative (Loc,
6374 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6375 Statements => New_List (
6376 Make_Raise_Constraint_Error (Loc,
6377 Condition => Make_Identifier (Loc, Name_uF),
6378 Reason => CE_Invalid_Data),
6379 Make_Simple_Return_Statement (Loc,
6381 Make_Integer_Literal (Loc, -1)))));
6383 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
6384 -- active then return -1 (we cannot usefully raise Constraint_Error in
6385 -- this case). See description above for further details.
6389 Make_Case_Statement_Alternative (Loc,
6390 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
6391 Statements => New_List (
6392 Make_Simple_Return_Statement (Loc,
6394 Make_Integer_Literal (Loc, -1)))));
6397 -- Now we can build the function body
6400 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
6403 Make_Subprogram_Body (Loc,
6405 Make_Function_Specification (Loc,
6406 Defining_Unit_Name => Fent,
6407 Parameter_Specifications => New_List (
6408 Make_Parameter_Specification (Loc,
6409 Defining_Identifier =>
6410 Make_Defining_Identifier (Loc, Name_uA),
6411 Parameter_Type => New_Reference_To (Typ, Loc)),
6412 Make_Parameter_Specification (Loc,
6413 Defining_Identifier =>
6414 Make_Defining_Identifier (Loc, Name_uF),
6415 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
6417 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
6419 Declarations => Empty_List,
6421 Handled_Statement_Sequence =>
6422 Make_Handled_Sequence_Of_Statements (Loc,
6423 Statements => New_List (
6424 Make_Case_Statement (Loc,
6426 Unchecked_Convert_To
6427 (Ityp, Make_Identifier (Loc, Name_uA)),
6428 Alternatives => Lst))));
6430 Set_TSS (Typ, Fent);
6432 -- Set Pure flag (it will be reset if the current context is not Pure).
6433 -- We also pretend there was a pragma Pure_Function so that for purposes
6434 -- of optimization and constant-folding, we will consider the function
6435 -- Pure even if we are not in a Pure context).
6438 Set_Has_Pragma_Pure_Function (Fent);
6440 -- Unless we are in -gnatD mode, where we are debugging generated code,
6441 -- this is an internal entity for which we don't need debug info.
6443 if not Debug_Generated_Code then
6444 Set_Debug_Info_Off (Fent);
6448 when RE_Not_Available =>
6450 end Expand_Freeze_Enumeration_Type;
6452 -------------------------------
6453 -- Expand_Freeze_Record_Type --
6454 -------------------------------
6456 procedure Expand_Freeze_Record_Type (N : Node_Id) is
6457 Def_Id : constant Node_Id := Entity (N);
6458 Type_Decl : constant Node_Id := Parent (Def_Id);
6460 Comp_Typ : Entity_Id;
6462 Predef_List : List_Id;
6464 Renamed_Eq : Node_Id := Empty;
6465 -- Defining unit name for the predefined equality function in the case
6466 -- where the type has a primitive operation that is a renaming of
6467 -- predefined equality (but only if there is also an overriding
6468 -- user-defined equality function). Used to pass this entity from
6469 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
6471 Wrapper_Decl_List : List_Id := No_List;
6472 Wrapper_Body_List : List_Id := No_List;
6474 -- Start of processing for Expand_Freeze_Record_Type
6477 -- Build discriminant checking functions if not a derived type (for
6478 -- derived types that are not tagged types, always use the discriminant
6479 -- checking functions of the parent type). However, for untagged types
6480 -- the derivation may have taken place before the parent was frozen, so
6481 -- we copy explicitly the discriminant checking functions from the
6482 -- parent into the components of the derived type.
6484 if not Is_Derived_Type (Def_Id)
6485 or else Has_New_Non_Standard_Rep (Def_Id)
6486 or else Is_Tagged_Type (Def_Id)
6488 Build_Discr_Checking_Funcs (Type_Decl);
6490 elsif Is_Derived_Type (Def_Id)
6491 and then not Is_Tagged_Type (Def_Id)
6493 -- If we have a derived Unchecked_Union, we do not inherit the
6494 -- discriminant checking functions from the parent type since the
6495 -- discriminants are non existent.
6497 and then not Is_Unchecked_Union (Def_Id)
6498 and then Has_Discriminants (Def_Id)
6501 Old_Comp : Entity_Id;
6505 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
6506 Comp := First_Component (Def_Id);
6507 while Present (Comp) loop
6508 if Ekind (Comp) = E_Component
6509 and then Chars (Comp) = Chars (Old_Comp)
6511 Set_Discriminant_Checking_Func (Comp,
6512 Discriminant_Checking_Func (Old_Comp));
6515 Next_Component (Old_Comp);
6516 Next_Component (Comp);
6521 if Is_Derived_Type (Def_Id)
6522 and then Is_Limited_Type (Def_Id)
6523 and then Is_Tagged_Type (Def_Id)
6525 Check_Stream_Attributes (Def_Id);
6528 -- Update task and controlled component flags, because some of the
6529 -- component types may have been private at the point of the record
6530 -- declaration. Detect anonymous access-to-controlled components.
6534 Comp := First_Component (Def_Id);
6535 while Present (Comp) loop
6536 Comp_Typ := Etype (Comp);
6538 if Has_Task (Comp_Typ) then
6539 Set_Has_Task (Def_Id);
6541 -- Do not set Has_Controlled_Component on a class-wide equivalent
6542 -- type. See Make_CW_Equivalent_Type.
6544 elsif not Is_Class_Wide_Equivalent_Type (Def_Id)
6545 and then (Has_Controlled_Component (Comp_Typ)
6546 or else (Chars (Comp) /= Name_uParent
6547 and then Is_Controlled (Comp_Typ)))
6549 Set_Has_Controlled_Component (Def_Id);
6551 -- Non-self-referential anonymous access-to-controlled component
6553 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
6554 and then Needs_Finalization (Designated_Type (Comp_Typ))
6555 and then Designated_Type (Comp_Typ) /= Def_Id
6560 Next_Component (Comp);
6563 -- Handle constructors of non-tagged CPP_Class types
6565 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
6566 Set_CPP_Constructors (Def_Id);
6569 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
6570 -- for regular tagged types as well as for Ada types deriving from a C++
6571 -- Class, but not for tagged types directly corresponding to C++ classes
6572 -- In the later case we assume that it is created in the C++ side and we
6575 if Is_Tagged_Type (Def_Id) then
6577 -- Add the _Tag component
6579 if Underlying_Type (Etype (Def_Id)) = Def_Id then
6580 Expand_Tagged_Root (Def_Id);
6583 if Is_CPP_Class (Def_Id) then
6584 Set_All_DT_Position (Def_Id);
6586 -- Create the tag entities with a minimum decoration
6588 if Tagged_Type_Expansion then
6589 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6592 Set_CPP_Constructors (Def_Id);
6595 if not Building_Static_DT (Def_Id) then
6597 -- Usually inherited primitives are not delayed but the first
6598 -- Ada extension of a CPP_Class is an exception since the
6599 -- address of the inherited subprogram has to be inserted in
6600 -- the new Ada Dispatch Table and this is a freezing action.
6602 -- Similarly, if this is an inherited operation whose parent is
6603 -- not frozen yet, it is not in the DT of the parent, and we
6604 -- generate an explicit freeze node for the inherited operation
6605 -- so it is properly inserted in the DT of the current type.
6612 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6613 while Present (Elmt) loop
6614 Subp := Node (Elmt);
6616 if Present (Alias (Subp)) then
6617 if Is_CPP_Class (Etype (Def_Id)) then
6618 Set_Has_Delayed_Freeze (Subp);
6620 elsif Has_Delayed_Freeze (Alias (Subp))
6621 and then not Is_Frozen (Alias (Subp))
6623 Set_Is_Frozen (Subp, False);
6624 Set_Has_Delayed_Freeze (Subp);
6633 -- Unfreeze momentarily the type to add the predefined primitives
6634 -- operations. The reason we unfreeze is so that these predefined
6635 -- operations will indeed end up as primitive operations (which
6636 -- must be before the freeze point).
6638 Set_Is_Frozen (Def_Id, False);
6640 -- Do not add the spec of predefined primitives in case of
6641 -- CPP tagged type derivations that have convention CPP.
6643 if Is_CPP_Class (Root_Type (Def_Id))
6644 and then Convention (Def_Id) = Convention_CPP
6648 -- Do not add the spec of predefined primitives in case of
6649 -- CIL and Java tagged types
6651 elsif Convention (Def_Id) = Convention_CIL
6652 or else Convention (Def_Id) = Convention_Java
6656 -- Do not add the spec of the predefined primitives if we are
6657 -- compiling under restriction No_Dispatching_Calls.
6659 elsif not Restriction_Active (No_Dispatching_Calls) then
6660 Make_Predefined_Primitive_Specs
6661 (Def_Id, Predef_List, Renamed_Eq);
6662 Insert_List_Before_And_Analyze (N, Predef_List);
6665 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6666 -- wrapper functions for each nonoverridden inherited function
6667 -- with a controlling result of the type. The wrapper for such
6668 -- a function returns an extension aggregate that invokes the
6671 if Ada_Version >= Ada_2005
6672 and then not Is_Abstract_Type (Def_Id)
6673 and then Is_Null_Extension (Def_Id)
6675 Make_Controlling_Function_Wrappers
6676 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6677 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6680 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6681 -- null procedure declarations for each set of homographic null
6682 -- procedures that are inherited from interface types but not
6683 -- overridden. This is done to ensure that the dispatch table
6684 -- entry associated with such null primitives are properly filled.
6686 if Ada_Version >= Ada_2005
6687 and then Etype (Def_Id) /= Def_Id
6688 and then not Is_Abstract_Type (Def_Id)
6689 and then Has_Interfaces (Def_Id)
6691 Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6694 Set_Is_Frozen (Def_Id);
6695 if not Is_Derived_Type (Def_Id)
6696 or else Is_Tagged_Type (Etype (Def_Id))
6698 Set_All_DT_Position (Def_Id);
6701 -- Create and decorate the tags. Suppress their creation when
6702 -- VM_Target because the dispatching mechanism is handled
6703 -- internally by the VMs.
6705 if Tagged_Type_Expansion then
6706 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6708 -- Generate dispatch table of locally defined tagged type.
6709 -- Dispatch tables of library level tagged types are built
6710 -- later (see Analyze_Declarations).
6712 if not Building_Static_DT (Def_Id) then
6713 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
6716 elsif VM_Target /= No_VM then
6717 Append_Freeze_Actions (Def_Id, Make_VM_TSD (Def_Id));
6720 -- If the type has unknown discriminants, propagate dispatching
6721 -- information to its underlying record view, which does not get
6722 -- its own dispatch table.
6724 if Is_Derived_Type (Def_Id)
6725 and then Has_Unknown_Discriminants (Def_Id)
6726 and then Present (Underlying_Record_View (Def_Id))
6729 Rep : constant Entity_Id := Underlying_Record_View (Def_Id);
6731 Set_Access_Disp_Table
6732 (Rep, Access_Disp_Table (Def_Id));
6733 Set_Dispatch_Table_Wrappers
6734 (Rep, Dispatch_Table_Wrappers (Def_Id));
6735 Set_Direct_Primitive_Operations
6736 (Rep, Direct_Primitive_Operations (Def_Id));
6740 -- Make sure that the primitives Initialize, Adjust and Finalize
6741 -- are Frozen before other TSS subprograms. We don't want them
6744 if Is_Controlled (Def_Id) then
6745 if not Is_Limited_Type (Def_Id) then
6746 Append_Freeze_Actions (Def_Id,
6748 (Find_Prim_Op (Def_Id, Name_Adjust), Def_Id));
6751 Append_Freeze_Actions (Def_Id,
6753 (Find_Prim_Op (Def_Id, Name_Initialize), Def_Id));
6755 Append_Freeze_Actions (Def_Id,
6757 (Find_Prim_Op (Def_Id, Name_Finalize), Def_Id));
6760 -- Freeze rest of primitive operations. There is no need to handle
6761 -- the predefined primitives if we are compiling under restriction
6762 -- No_Dispatching_Calls.
6764 if not Restriction_Active (No_Dispatching_Calls) then
6765 Append_Freeze_Actions
6766 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
6770 -- In the non-tagged case, ever since Ada 83 an equality function must
6771 -- be provided for variant records that are not unchecked unions.
6772 -- In Ada 2012 the equality function composes, and thus must be built
6773 -- explicitly just as for tagged records.
6775 elsif Has_Discriminants (Def_Id)
6776 and then not Is_Limited_Type (Def_Id)
6779 Comps : constant Node_Id :=
6780 Component_List (Type_Definition (Type_Decl));
6783 and then Present (Variant_Part (Comps))
6785 Build_Variant_Record_Equality (Def_Id);
6789 -- Otherwise create primitive equality operation (AI05-0123)
6791 -- This is done unconditionally to ensure that tools can be linked
6792 -- properly with user programs compiled with older language versions.
6793 -- In addition, this is needed because "=" composes for bounded strings
6794 -- in all language versions (see Exp_Ch4.Expand_Composite_Equality).
6796 elsif Comes_From_Source (Def_Id)
6797 and then Convention (Def_Id) = Convention_Ada
6798 and then not Is_Limited_Type (Def_Id)
6800 Build_Untagged_Equality (Def_Id);
6803 -- Before building the record initialization procedure, if we are
6804 -- dealing with a concurrent record value type, then we must go through
6805 -- the discriminants, exchanging discriminals between the concurrent
6806 -- type and the concurrent record value type. See the section "Handling
6807 -- of Discriminants" in the Einfo spec for details.
6809 if Is_Concurrent_Record_Type (Def_Id)
6810 and then Has_Discriminants (Def_Id)
6813 Ctyp : constant Entity_Id :=
6814 Corresponding_Concurrent_Type (Def_Id);
6815 Conc_Discr : Entity_Id;
6816 Rec_Discr : Entity_Id;
6820 Conc_Discr := First_Discriminant (Ctyp);
6821 Rec_Discr := First_Discriminant (Def_Id);
6822 while Present (Conc_Discr) loop
6823 Temp := Discriminal (Conc_Discr);
6824 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6825 Set_Discriminal (Rec_Discr, Temp);
6827 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6828 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
6830 Next_Discriminant (Conc_Discr);
6831 Next_Discriminant (Rec_Discr);
6836 if Has_Controlled_Component (Def_Id) then
6837 Build_Controlling_Procs (Def_Id);
6840 Adjust_Discriminants (Def_Id);
6842 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
6844 -- Do not need init for interfaces on e.g. CIL since they're
6845 -- abstract. Helps operation of peverify (the PE Verify tool).
6847 Build_Record_Init_Proc (Type_Decl, Def_Id);
6850 -- For tagged type that are not interfaces, build bodies of primitive
6851 -- operations. Note: do this after building the record initialization
6852 -- procedure, since the primitive operations may need the initialization
6853 -- routine. There is no need to add predefined primitives of interfaces
6854 -- because all their predefined primitives are abstract.
6856 if Is_Tagged_Type (Def_Id)
6857 and then not Is_Interface (Def_Id)
6859 -- Do not add the body of predefined primitives in case of
6860 -- CPP tagged type derivations that have convention CPP.
6862 if Is_CPP_Class (Root_Type (Def_Id))
6863 and then Convention (Def_Id) = Convention_CPP
6867 -- Do not add the body of predefined primitives in case of
6868 -- CIL and Java tagged types.
6870 elsif Convention (Def_Id) = Convention_CIL
6871 or else Convention (Def_Id) = Convention_Java
6875 -- Do not add the body of the predefined primitives if we are
6876 -- compiling under restriction No_Dispatching_Calls or if we are
6877 -- compiling a CPP tagged type.
6879 elsif not Restriction_Active (No_Dispatching_Calls) then
6881 -- Create the body of TSS primitive Finalize_Address. This must
6882 -- be done before the bodies of all predefined primitives are
6883 -- created. If Def_Id is limited, Stream_Input and Stream_Read
6884 -- may produce build-in-place allocations and for those the
6885 -- expander needs Finalize_Address. Do not create the body of
6886 -- Finalize_Address in Alfa mode since it is not needed.
6888 if not Alfa_Mode then
6889 Make_Finalize_Address_Body (Def_Id);
6892 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6893 Append_Freeze_Actions (Def_Id, Predef_List);
6896 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6897 -- inherited functions, then add their bodies to the freeze actions.
6899 if Present (Wrapper_Body_List) then
6900 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6903 -- Create extra formals for the primitive operations of the type.
6904 -- This must be done before analyzing the body of the initialization
6905 -- procedure, because a self-referential type might call one of these
6906 -- primitives in the body of the init_proc itself.
6913 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6914 while Present (Elmt) loop
6915 Subp := Node (Elmt);
6916 if not Has_Foreign_Convention (Subp)
6917 and then not Is_Predefined_Dispatching_Operation (Subp)
6919 Create_Extra_Formals (Subp);
6927 -- Create a heterogeneous finalization master to service the anonymous
6928 -- access-to-controlled components of the record type.
6932 Encl_Scope : constant Entity_Id := Scope (Def_Id);
6933 Ins_Node : constant Node_Id := Parent (Def_Id);
6934 Loc : constant Source_Ptr := Sloc (Def_Id);
6935 Fin_Mas_Id : Entity_Id;
6937 Attributes_Set : Boolean := False;
6938 Master_Built : Boolean := False;
6939 -- Two flags which control the creation and initialization of a
6940 -- common heterogeneous master.
6943 Comp := First_Component (Def_Id);
6944 while Present (Comp) loop
6945 Comp_Typ := Etype (Comp);
6947 -- A non-self-referential anonymous access-to-controlled
6950 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6951 and then Needs_Finalization (Designated_Type (Comp_Typ))
6952 and then Designated_Type (Comp_Typ) /= Def_Id
6954 if VM_Target = No_VM then
6956 -- Build a homogeneous master for the first anonymous
6957 -- access-to-controlled component. This master may be
6958 -- converted into a heterogeneous collection if more
6959 -- components are to follow.
6961 if not Master_Built then
6962 Master_Built := True;
6964 -- All anonymous access-to-controlled types allocate
6965 -- on the global pool.
6967 Set_Associated_Storage_Pool (Comp_Typ,
6968 Get_Global_Pool_For_Access_Type (Comp_Typ));
6970 Build_Finalization_Master
6972 Ins_Node => Ins_Node,
6973 Encl_Scope => Encl_Scope);
6975 Fin_Mas_Id := Finalization_Master (Comp_Typ);
6977 -- Subsequent anonymous access-to-controlled components
6978 -- reuse the already available master.
6981 -- All anonymous access-to-controlled types allocate
6982 -- on the global pool.
6984 Set_Associated_Storage_Pool (Comp_Typ,
6985 Get_Global_Pool_For_Access_Type (Comp_Typ));
6987 -- Shared the master among multiple components
6989 Set_Finalization_Master (Comp_Typ, Fin_Mas_Id);
6991 -- Convert the master into a heterogeneous collection.
6994 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
6996 if not Attributes_Set then
6997 Attributes_Set := True;
6999 Insert_Action (Ins_Node,
7000 Make_Procedure_Call_Statement (Loc,
7003 (RTE (RE_Set_Is_Heterogeneous), Loc),
7004 Parameter_Associations => New_List (
7005 New_Reference_To (Fin_Mas_Id, Loc))));
7009 -- Since .NET/JVM targets do not support heterogeneous
7010 -- masters, each component must have its own master.
7013 Build_Finalization_Master
7015 Ins_Node => Ins_Node,
7016 Encl_Scope => Encl_Scope);
7020 Next_Component (Comp);
7025 -- Check whether individual components have a defined invariant,
7026 -- and add the corresponding component invariant checks.
7028 Insert_Component_Invariant_Checks
7029 (N, Def_Id, Build_Record_Invariant_Proc (Def_Id, N));
7030 end Expand_Freeze_Record_Type;
7032 ------------------------------
7033 -- Freeze_Stream_Operations --
7034 ------------------------------
7036 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
7037 Names : constant array (1 .. 4) of TSS_Name_Type :=
7042 Stream_Op : Entity_Id;
7045 -- Primitive operations of tagged types are frozen when the dispatch
7046 -- table is constructed.
7048 if not Comes_From_Source (Typ)
7049 or else Is_Tagged_Type (Typ)
7054 for J in Names'Range loop
7055 Stream_Op := TSS (Typ, Names (J));
7057 if Present (Stream_Op)
7058 and then Is_Subprogram (Stream_Op)
7059 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
7060 N_Subprogram_Declaration
7061 and then not Is_Frozen (Stream_Op)
7063 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
7066 end Freeze_Stream_Operations;
7072 -- Full type declarations are expanded at the point at which the type is
7073 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
7074 -- declarations generated by the freezing (e.g. the procedure generated
7075 -- for initialization) are chained in the Actions field list of the freeze
7076 -- node using Append_Freeze_Actions.
7078 function Freeze_Type (N : Node_Id) return Boolean is
7079 Def_Id : constant Entity_Id := Entity (N);
7080 RACW_Seen : Boolean := False;
7081 Result : Boolean := False;
7084 -- Process associated access types needing special processing
7086 if Present (Access_Types_To_Process (N)) then
7088 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
7090 while Present (E) loop
7092 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
7093 Validate_RACW_Primitives (Node (E));
7103 -- If there are RACWs designating this type, make stubs now
7105 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
7109 -- Freeze processing for record types
7111 if Is_Record_Type (Def_Id) then
7112 if Ekind (Def_Id) = E_Record_Type then
7113 Expand_Freeze_Record_Type (N);
7115 elsif Is_Class_Wide_Type (Def_Id) then
7116 Expand_Freeze_Class_Wide_Type (N);
7119 -- Freeze processing for array types
7121 elsif Is_Array_Type (Def_Id) then
7122 Expand_Freeze_Array_Type (N);
7124 -- Freeze processing for access types
7126 -- For pool-specific access types, find out the pool object used for
7127 -- this type, needs actual expansion of it in some cases. Here are the
7128 -- different cases :
7130 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
7131 -- ---> don't use any storage pool
7133 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
7135 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
7137 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7138 -- ---> Storage Pool is the specified one
7140 -- See GNAT Pool packages in the Run-Time for more details
7142 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
7144 Loc : constant Source_Ptr := Sloc (N);
7145 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
7146 Pool_Object : Entity_Id;
7148 Freeze_Action_Typ : Entity_Id;
7153 -- Rep Clause "for Def_Id'Storage_Size use 0;"
7154 -- ---> don't use any storage pool
7156 if No_Pool_Assigned (Def_Id) then
7161 -- Rep Clause : for Def_Id'Storage_Size use Expr.
7163 -- Def_Id__Pool : Stack_Bounded_Pool
7164 -- (Expr, DT'Size, DT'Alignment);
7166 elsif Has_Storage_Size_Clause (Def_Id) then
7172 -- For unconstrained composite types we give a size of zero
7173 -- so that the pool knows that it needs a special algorithm
7174 -- for variable size object allocation.
7176 if Is_Composite_Type (Desig_Type)
7177 and then not Is_Constrained (Desig_Type)
7180 Make_Integer_Literal (Loc, 0);
7183 Make_Integer_Literal (Loc, Maximum_Alignment);
7187 Make_Attribute_Reference (Loc,
7188 Prefix => New_Reference_To (Desig_Type, Loc),
7189 Attribute_Name => Name_Max_Size_In_Storage_Elements);
7192 Make_Attribute_Reference (Loc,
7193 Prefix => New_Reference_To (Desig_Type, Loc),
7194 Attribute_Name => Name_Alignment);
7198 Make_Defining_Identifier (Loc,
7199 Chars => New_External_Name (Chars (Def_Id), 'P'));
7201 -- We put the code associated with the pools in the entity
7202 -- that has the later freeze node, usually the access type
7203 -- but it can also be the designated_type; because the pool
7204 -- code requires both those types to be frozen
7206 if Is_Frozen (Desig_Type)
7207 and then (No (Freeze_Node (Desig_Type))
7208 or else Analyzed (Freeze_Node (Desig_Type)))
7210 Freeze_Action_Typ := Def_Id;
7212 -- A Taft amendment type cannot get the freeze actions
7213 -- since the full view is not there.
7215 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
7216 and then No (Full_View (Desig_Type))
7218 Freeze_Action_Typ := Def_Id;
7221 Freeze_Action_Typ := Desig_Type;
7224 Append_Freeze_Action (Freeze_Action_Typ,
7225 Make_Object_Declaration (Loc,
7226 Defining_Identifier => Pool_Object,
7227 Object_Definition =>
7228 Make_Subtype_Indication (Loc,
7231 (RTE (RE_Stack_Bounded_Pool), Loc),
7234 Make_Index_Or_Discriminant_Constraint (Loc,
7235 Constraints => New_List (
7237 -- First discriminant is the Pool Size
7240 Storage_Size_Variable (Def_Id), Loc),
7242 -- Second discriminant is the element size
7246 -- Third discriminant is the alignment
7251 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
7255 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
7256 -- ---> Storage Pool is the specified one
7258 -- When compiling in Ada 2012 mode, ensure that the accessibility
7259 -- level of the subpool access type is not deeper than that of the
7260 -- pool_with_subpools. This check is not performed on .NET/JVM
7261 -- since those targets do not support pools.
7263 elsif Ada_Version >= Ada_2012
7264 and then Present (Associated_Storage_Pool (Def_Id))
7265 and then VM_Target = No_VM
7268 Loc : constant Source_Ptr := Sloc (Def_Id);
7269 Pool : constant Entity_Id :=
7270 Associated_Storage_Pool (Def_Id);
7271 RSPWS : constant Entity_Id :=
7272 RTE (RE_Root_Storage_Pool_With_Subpools);
7275 -- It is known that the accessibility level of the access
7276 -- type is deeper than that of the pool.
7278 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
7279 and then not Accessibility_Checks_Suppressed (Def_Id)
7280 and then not Accessibility_Checks_Suppressed (Pool)
7282 -- Static case: the pool is known to be a descendant of
7283 -- Root_Storage_Pool_With_Subpools.
7285 if Is_Ancestor (RSPWS, Etype (Pool)) then
7287 ("??subpool access type has deeper accessibility " &
7288 "level than pool", Def_Id);
7290 Append_Freeze_Action (Def_Id,
7291 Make_Raise_Program_Error (Loc,
7292 Reason => PE_Accessibility_Check_Failed));
7294 -- Dynamic case: when the pool is of a class-wide type,
7295 -- it may or may not support subpools depending on the
7296 -- path of derivation. Generate:
7298 -- if Def_Id in RSPWS'Class then
7299 -- raise Program_Error;
7302 elsif Is_Class_Wide_Type (Etype (Pool)) then
7303 Append_Freeze_Action (Def_Id,
7304 Make_If_Statement (Loc,
7308 New_Reference_To (Pool, Loc),
7311 (Class_Wide_Type (RSPWS), Loc)),
7313 Then_Statements => New_List (
7314 Make_Raise_Program_Error (Loc,
7315 Reason => PE_Accessibility_Check_Failed))));
7321 -- For access-to-controlled types (including class-wide types and
7322 -- Taft-amendment types, which potentially have controlled
7323 -- components), expand the list controller object that will store
7324 -- the dynamically allocated objects. Don't do this transformation
7325 -- for expander-generated access types, but do it for types that
7326 -- are the full view of types derived from other private types.
7327 -- Also suppress the list controller in the case of a designated
7328 -- type with convention Java, since this is used when binding to
7329 -- Java API specs, where there's no equivalent of a finalization
7330 -- list and we don't want to pull in the finalization support if
7333 if not Comes_From_Source (Def_Id)
7334 and then not Has_Private_Declaration (Def_Id)
7338 -- An exception is made for types defined in the run-time because
7339 -- Ada.Tags.Tag itself is such a type and cannot afford this
7340 -- unnecessary overhead that would generates a loop in the
7341 -- expansion scheme. Another exception is if Restrictions
7342 -- (No_Finalization) is active, since then we know nothing is
7345 elsif Restriction_Active (No_Finalization)
7346 or else In_Runtime (Def_Id)
7350 -- Assume that incomplete and private types are always completed
7351 -- by a controlled full view.
7353 elsif Needs_Finalization (Desig_Type)
7355 (Is_Incomplete_Or_Private_Type (Desig_Type)
7356 and then No (Full_View (Desig_Type)))
7358 (Is_Array_Type (Desig_Type)
7359 and then Needs_Finalization (Component_Type (Desig_Type)))
7361 Build_Finalization_Master (Def_Id);
7365 -- Freeze processing for enumeration types
7367 elsif Ekind (Def_Id) = E_Enumeration_Type then
7369 -- We only have something to do if we have a non-standard
7370 -- representation (i.e. at least one literal whose pos value
7371 -- is not the same as its representation)
7373 if Has_Non_Standard_Rep (Def_Id) then
7374 Expand_Freeze_Enumeration_Type (N);
7377 -- Private types that are completed by a derivation from a private
7378 -- type have an internally generated full view, that needs to be
7379 -- frozen. This must be done explicitly because the two views share
7380 -- the freeze node, and the underlying full view is not visible when
7381 -- the freeze node is analyzed.
7383 elsif Is_Private_Type (Def_Id)
7384 and then Is_Derived_Type (Def_Id)
7385 and then Present (Full_View (Def_Id))
7386 and then Is_Itype (Full_View (Def_Id))
7387 and then Has_Private_Declaration (Full_View (Def_Id))
7388 and then Freeze_Node (Full_View (Def_Id)) = N
7390 Set_Entity (N, Full_View (Def_Id));
7391 Result := Freeze_Type (N);
7392 Set_Entity (N, Def_Id);
7394 -- All other types require no expander action. There are such cases
7395 -- (e.g. task types and protected types). In such cases, the freeze
7396 -- nodes are there for use by Gigi.
7400 Freeze_Stream_Operations (N, Def_Id);
7404 when RE_Not_Available =>
7408 -------------------------
7409 -- Get_Simple_Init_Val --
7410 -------------------------
7412 function Get_Simple_Init_Val
7415 Size : Uint := No_Uint) return Node_Id
7417 Loc : constant Source_Ptr := Sloc (N);
7423 -- This is the size to be used for computation of the appropriate
7424 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
7426 IV_Attribute : constant Boolean :=
7427 Nkind (N) = N_Attribute_Reference
7428 and then Attribute_Name (N) = Name_Invalid_Value;
7432 -- These are the values computed by the procedure Check_Subtype_Bounds
7434 procedure Check_Subtype_Bounds;
7435 -- This procedure examines the subtype T, and its ancestor subtypes and
7436 -- derived types to determine the best known information about the
7437 -- bounds of the subtype. After the call Lo_Bound is set either to
7438 -- No_Uint if no information can be determined, or to a value which
7439 -- represents a known low bound, i.e. a valid value of the subtype can
7440 -- not be less than this value. Hi_Bound is similarly set to a known
7441 -- high bound (valid value cannot be greater than this).
7443 --------------------------
7444 -- Check_Subtype_Bounds --
7445 --------------------------
7447 procedure Check_Subtype_Bounds is
7456 Lo_Bound := No_Uint;
7457 Hi_Bound := No_Uint;
7459 -- Loop to climb ancestor subtypes and derived types
7463 if not Is_Discrete_Type (ST1) then
7467 Lo := Type_Low_Bound (ST1);
7468 Hi := Type_High_Bound (ST1);
7470 if Compile_Time_Known_Value (Lo) then
7471 Loval := Expr_Value (Lo);
7473 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
7478 if Compile_Time_Known_Value (Hi) then
7479 Hival := Expr_Value (Hi);
7481 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
7486 ST2 := Ancestor_Subtype (ST1);
7492 exit when ST1 = ST2;
7495 end Check_Subtype_Bounds;
7497 -- Start of processing for Get_Simple_Init_Val
7500 -- For a private type, we should always have an underlying type
7501 -- (because this was already checked in Needs_Simple_Initialization).
7502 -- What we do is to get the value for the underlying type and then do
7503 -- an Unchecked_Convert to the private type.
7505 if Is_Private_Type (T) then
7506 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
7508 -- A special case, if the underlying value is null, then qualify it
7509 -- with the underlying type, so that the null is properly typed
7510 -- Similarly, if it is an aggregate it must be qualified, because an
7511 -- unchecked conversion does not provide a context for it.
7513 if Nkind_In (Val, N_Null, N_Aggregate) then
7515 Make_Qualified_Expression (Loc,
7517 New_Occurrence_Of (Underlying_Type (T), Loc),
7521 Result := Unchecked_Convert_To (T, Val);
7523 -- Don't truncate result (important for Initialize/Normalize_Scalars)
7525 if Nkind (Result) = N_Unchecked_Type_Conversion
7526 and then Is_Scalar_Type (Underlying_Type (T))
7528 Set_No_Truncation (Result);
7533 -- Scalars with Default_Value aspect. The first subtype may now be
7534 -- private, so retrieve value from underlying type.
7536 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
7537 if Is_Private_Type (First_Subtype (T)) then
7538 return Unchecked_Convert_To (T,
7539 Default_Aspect_Value (Full_View (First_Subtype (T))));
7542 Convert_To (T, Default_Aspect_Value (First_Subtype (T)));
7545 -- Otherwise, for scalars, we must have normalize/initialize scalars
7546 -- case, or if the node N is an 'Invalid_Value attribute node.
7548 elsif Is_Scalar_Type (T) then
7549 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
7551 -- Compute size of object. If it is given by the caller, we can use
7552 -- it directly, otherwise we use Esize (T) as an estimate. As far as
7553 -- we know this covers all cases correctly.
7555 if Size = No_Uint or else Size <= Uint_0 then
7556 Size_To_Use := UI_Max (Uint_1, Esize (T));
7558 Size_To_Use := Size;
7561 -- Maximum size to use is 64 bits, since we will create values of
7562 -- type Unsigned_64 and the range must fit this type.
7564 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
7565 Size_To_Use := Uint_64;
7568 -- Check known bounds of subtype
7570 Check_Subtype_Bounds;
7572 -- Processing for Normalize_Scalars case
7574 if Normalize_Scalars and then not IV_Attribute then
7576 -- If zero is invalid, it is a convenient value to use that is
7577 -- for sure an appropriate invalid value in all situations.
7579 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7580 Val := Make_Integer_Literal (Loc, 0);
7582 -- Cases where all one bits is the appropriate invalid value
7584 -- For modular types, all 1 bits is either invalid or valid. If
7585 -- it is valid, then there is nothing that can be done since there
7586 -- are no invalid values (we ruled out zero already).
7588 -- For signed integer types that have no negative values, either
7589 -- there is room for negative values, or there is not. If there
7590 -- is, then all 1-bits may be interpreted as minus one, which is
7591 -- certainly invalid. Alternatively it is treated as the largest
7592 -- positive value, in which case the observation for modular types
7595 -- For float types, all 1-bits is a NaN (not a number), which is
7596 -- certainly an appropriately invalid value.
7598 elsif Is_Unsigned_Type (T)
7599 or else Is_Floating_Point_Type (T)
7600 or else Is_Enumeration_Type (T)
7602 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
7604 -- Resolve as Unsigned_64, because the largest number we can
7605 -- generate is out of range of universal integer.
7607 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
7609 -- Case of signed types
7613 Signed_Size : constant Uint :=
7614 UI_Min (Uint_63, Size_To_Use - 1);
7617 -- Normally we like to use the most negative number. The one
7618 -- exception is when this number is in the known subtype
7619 -- range and the largest positive number is not in the known
7622 -- For this exceptional case, use largest positive value
7624 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7625 and then Lo_Bound <= (-(2 ** Signed_Size))
7626 and then Hi_Bound < 2 ** Signed_Size
7628 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7630 -- Normal case of largest negative value
7633 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7638 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7641 -- For float types, use float values from System.Scalar_Values
7643 if Is_Floating_Point_Type (T) then
7644 if Root_Type (T) = Standard_Short_Float then
7645 Val_RE := RE_IS_Isf;
7646 elsif Root_Type (T) = Standard_Float then
7647 Val_RE := RE_IS_Ifl;
7648 elsif Root_Type (T) = Standard_Long_Float then
7649 Val_RE := RE_IS_Ilf;
7650 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7651 Val_RE := RE_IS_Ill;
7654 -- If zero is invalid, use zero values from System.Scalar_Values
7656 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7657 if Size_To_Use <= 8 then
7658 Val_RE := RE_IS_Iz1;
7659 elsif Size_To_Use <= 16 then
7660 Val_RE := RE_IS_Iz2;
7661 elsif Size_To_Use <= 32 then
7662 Val_RE := RE_IS_Iz4;
7664 Val_RE := RE_IS_Iz8;
7667 -- For unsigned, use unsigned values from System.Scalar_Values
7669 elsif Is_Unsigned_Type (T) then
7670 if Size_To_Use <= 8 then
7671 Val_RE := RE_IS_Iu1;
7672 elsif Size_To_Use <= 16 then
7673 Val_RE := RE_IS_Iu2;
7674 elsif Size_To_Use <= 32 then
7675 Val_RE := RE_IS_Iu4;
7677 Val_RE := RE_IS_Iu8;
7680 -- For signed, use signed values from System.Scalar_Values
7683 if Size_To_Use <= 8 then
7684 Val_RE := RE_IS_Is1;
7685 elsif Size_To_Use <= 16 then
7686 Val_RE := RE_IS_Is2;
7687 elsif Size_To_Use <= 32 then
7688 Val_RE := RE_IS_Is4;
7690 Val_RE := RE_IS_Is8;
7694 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7697 -- The final expression is obtained by doing an unchecked conversion
7698 -- of this result to the base type of the required subtype. We use
7699 -- the base type to prevent the unchecked conversion from chopping
7700 -- bits, and then we set Kill_Range_Check to preserve the "bad"
7703 Result := Unchecked_Convert_To (Base_Type (T), Val);
7705 -- Ensure result is not truncated, since we want the "bad" bits, and
7706 -- also kill range check on result.
7708 if Nkind (Result) = N_Unchecked_Type_Conversion then
7709 Set_No_Truncation (Result);
7710 Set_Kill_Range_Check (Result, True);
7715 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7717 elsif Root_Type (T) = Standard_String
7719 Root_Type (T) = Standard_Wide_String
7721 Root_Type (T) = Standard_Wide_Wide_String
7723 pragma Assert (Init_Or_Norm_Scalars);
7726 Make_Aggregate (Loc,
7727 Component_Associations => New_List (
7728 Make_Component_Association (Loc,
7729 Choices => New_List (
7730 Make_Others_Choice (Loc)),
7733 (Component_Type (T), N, Esize (Root_Type (T))))));
7735 -- Access type is initialized to null
7737 elsif Is_Access_Type (T) then
7738 return Make_Null (Loc);
7740 -- No other possibilities should arise, since we should only be calling
7741 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7742 -- indicating one of the above cases held.
7745 raise Program_Error;
7749 when RE_Not_Available =>
7751 end Get_Simple_Init_Val;
7753 ------------------------------
7754 -- Has_New_Non_Standard_Rep --
7755 ------------------------------
7757 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7759 if not Is_Derived_Type (T) then
7760 return Has_Non_Standard_Rep (T)
7761 or else Has_Non_Standard_Rep (Root_Type (T));
7763 -- If Has_Non_Standard_Rep is not set on the derived type, the
7764 -- representation is fully inherited.
7766 elsif not Has_Non_Standard_Rep (T) then
7770 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7772 -- May need a more precise check here: the First_Rep_Item may
7773 -- be a stream attribute, which does not affect the representation
7776 end Has_New_Non_Standard_Rep;
7782 function In_Runtime (E : Entity_Id) return Boolean is
7787 while Scope (S1) /= Standard_Standard loop
7791 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7794 ---------------------------------------
7795 -- Insert_Component_Invariant_Checks --
7796 ---------------------------------------
7798 procedure Insert_Component_Invariant_Checks
7803 Loc : constant Source_Ptr := Sloc (Typ);
7804 Proc_Id : Entity_Id;
7807 if Present (Proc) then
7808 Proc_Id := Defining_Entity (Proc);
7810 if not Has_Invariants (Typ) then
7811 Set_Has_Invariants (Typ);
7812 Set_Is_Invariant_Procedure (Proc_Id);
7813 Set_Invariant_Procedure (Typ, Proc_Id);
7814 Insert_After (N, Proc);
7819 -- Find already created invariant body, insert body of component
7820 -- invariant proc in it, and add call after other checks.
7824 Inv_Id : constant Entity_Id := Invariant_Procedure (Typ);
7825 Call : constant Node_Id :=
7826 Make_Procedure_Call_Statement (Loc,
7827 Name => New_Occurrence_Of (Proc_Id, Loc),
7828 Parameter_Associations =>
7830 (New_Reference_To (First_Formal (Inv_Id), Loc)));
7834 -- The invariant body has not been analyzed yet, so we do a
7835 -- sequential search forward, and retrieve it by name.
7838 while Present (Bod) loop
7839 exit when Nkind (Bod) = N_Subprogram_Body
7840 and then Chars (Defining_Entity (Bod)) = Chars (Inv_Id);
7844 Append_To (Declarations (Bod), Proc);
7845 Append_To (Statements (Handled_Statement_Sequence (Bod)), Call);
7849 end Insert_Component_Invariant_Checks;
7851 ----------------------------
7852 -- Initialization_Warning --
7853 ----------------------------
7855 procedure Initialization_Warning (E : Entity_Id) is
7856 Warning_Needed : Boolean;
7859 Warning_Needed := False;
7861 if Ekind (Current_Scope) = E_Package
7862 and then Static_Elaboration_Desired (Current_Scope)
7865 if Is_Record_Type (E) then
7866 if Has_Discriminants (E)
7867 or else Is_Limited_Type (E)
7868 or else Has_Non_Standard_Rep (E)
7870 Warning_Needed := True;
7873 -- Verify that at least one component has an initialization
7874 -- expression. No need for a warning on a type if all its
7875 -- components have no initialization.
7881 Comp := First_Component (E);
7882 while Present (Comp) loop
7883 if Ekind (Comp) = E_Discriminant
7885 (Nkind (Parent (Comp)) = N_Component_Declaration
7886 and then Present (Expression (Parent (Comp))))
7888 Warning_Needed := True;
7892 Next_Component (Comp);
7897 if Warning_Needed then
7899 ("Objects of the type cannot be initialized "
7900 & "statically by default??", Parent (E));
7905 Error_Msg_N ("Object cannot be initialized statically??", E);
7908 end Initialization_Warning;
7914 function Init_Formals (Typ : Entity_Id) return List_Id is
7915 Loc : constant Source_Ptr := Sloc (Typ);
7919 -- First parameter is always _Init : in out typ. Note that we need
7920 -- this to be in/out because in the case of the task record value,
7921 -- there are default record fields (_Priority, _Size, -Task_Info)
7922 -- that may be referenced in the generated initialization routine.
7924 Formals := New_List (
7925 Make_Parameter_Specification (Loc,
7926 Defining_Identifier =>
7927 Make_Defining_Identifier (Loc, Name_uInit),
7929 Out_Present => True,
7930 Parameter_Type => New_Reference_To (Typ, Loc)));
7932 -- For task record value, or type that contains tasks, add two more
7933 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7934 -- We also add these parameters for the task record type case.
7937 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7940 Make_Parameter_Specification (Loc,
7941 Defining_Identifier =>
7942 Make_Defining_Identifier (Loc, Name_uMaster),
7944 New_Reference_To (RTE (RE_Master_Id), Loc)));
7946 -- Add _Chain (not done for sequential elaboration policy, see
7947 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
7949 if Partition_Elaboration_Policy /= 'S' then
7951 Make_Parameter_Specification (Loc,
7952 Defining_Identifier =>
7953 Make_Defining_Identifier (Loc, Name_uChain),
7955 Out_Present => True,
7957 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
7961 Make_Parameter_Specification (Loc,
7962 Defining_Identifier =>
7963 Make_Defining_Identifier (Loc, Name_uTask_Name),
7965 Parameter_Type => New_Reference_To (Standard_String, Loc)));
7971 when RE_Not_Available =>
7975 -------------------------
7976 -- Init_Secondary_Tags --
7977 -------------------------
7979 procedure Init_Secondary_Tags
7982 Stmts_List : List_Id;
7983 Fixed_Comps : Boolean := True;
7984 Variable_Comps : Boolean := True)
7986 Loc : constant Source_Ptr := Sloc (Target);
7988 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
7989 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7991 procedure Initialize_Tag
7994 Tag_Comp : Entity_Id;
7995 Iface_Tag : Node_Id);
7996 -- Initialize the tag of the secondary dispatch table of Typ associated
7997 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7998 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
7999 -- of Typ CPP tagged type we generate code to inherit the contents of
8000 -- the dispatch table directly from the ancestor.
8002 --------------------
8003 -- Initialize_Tag --
8004 --------------------
8006 procedure Initialize_Tag
8009 Tag_Comp : Entity_Id;
8010 Iface_Tag : Node_Id)
8012 Comp_Typ : Entity_Id;
8013 Offset_To_Top_Comp : Entity_Id := Empty;
8016 -- Initialize the pointer to the secondary DT associated with the
8019 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8020 Append_To (Stmts_List,
8021 Make_Assignment_Statement (Loc,
8023 Make_Selected_Component (Loc,
8024 Prefix => New_Copy_Tree (Target),
8025 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
8027 New_Reference_To (Iface_Tag, Loc)));
8030 Comp_Typ := Scope (Tag_Comp);
8032 -- Initialize the entries of the table of interfaces. We generate a
8033 -- different call when the parent of the type has variable size
8036 if Comp_Typ /= Etype (Comp_Typ)
8037 and then Is_Variable_Size_Record (Etype (Comp_Typ))
8038 and then Chars (Tag_Comp) /= Name_uTag
8040 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
8042 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
8043 -- configurable run-time environment.
8045 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
8047 ("variable size record with interface types", Typ);
8052 -- Set_Dynamic_Offset_To_Top
8054 -- Interface_T => Iface'Tag,
8055 -- Offset_Value => n,
8056 -- Offset_Func => Fn'Address)
8058 Append_To (Stmts_List,
8059 Make_Procedure_Call_Statement (Loc,
8060 Name => New_Reference_To
8061 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
8062 Parameter_Associations => New_List (
8063 Make_Attribute_Reference (Loc,
8064 Prefix => New_Copy_Tree (Target),
8065 Attribute_Name => Name_Address),
8067 Unchecked_Convert_To (RTE (RE_Tag),
8069 (Node (First_Elmt (Access_Disp_Table (Iface))),
8072 Unchecked_Convert_To
8073 (RTE (RE_Storage_Offset),
8074 Make_Attribute_Reference (Loc,
8076 Make_Selected_Component (Loc,
8077 Prefix => New_Copy_Tree (Target),
8079 New_Reference_To (Tag_Comp, Loc)),
8080 Attribute_Name => Name_Position)),
8082 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
8083 Make_Attribute_Reference (Loc,
8084 Prefix => New_Reference_To
8085 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
8086 Attribute_Name => Name_Address)))));
8088 -- In this case the next component stores the value of the
8089 -- offset to the top.
8091 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
8092 pragma Assert (Present (Offset_To_Top_Comp));
8094 Append_To (Stmts_List,
8095 Make_Assignment_Statement (Loc,
8097 Make_Selected_Component (Loc,
8098 Prefix => New_Copy_Tree (Target),
8099 Selector_Name => New_Reference_To
8100 (Offset_To_Top_Comp, Loc)),
8102 Make_Attribute_Reference (Loc,
8104 Make_Selected_Component (Loc,
8105 Prefix => New_Copy_Tree (Target),
8106 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
8107 Attribute_Name => Name_Position)));
8109 -- Normal case: No discriminants in the parent type
8112 -- Don't need to set any value if this interface shares the
8113 -- primary dispatch table.
8115 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
8116 Append_To (Stmts_List,
8117 Build_Set_Static_Offset_To_Top (Loc,
8118 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
8120 Unchecked_Convert_To (RTE (RE_Storage_Offset),
8121 Make_Attribute_Reference (Loc,
8123 Make_Selected_Component (Loc,
8124 Prefix => New_Copy_Tree (Target),
8126 New_Reference_To (Tag_Comp, Loc)),
8127 Attribute_Name => Name_Position))));
8131 -- Register_Interface_Offset
8133 -- Interface_T => Iface'Tag,
8134 -- Is_Constant => True,
8135 -- Offset_Value => n,
8136 -- Offset_Func => null);
8138 if RTE_Available (RE_Register_Interface_Offset) then
8139 Append_To (Stmts_List,
8140 Make_Procedure_Call_Statement (Loc,
8141 Name => New_Reference_To
8142 (RTE (RE_Register_Interface_Offset), Loc),
8143 Parameter_Associations => New_List (
8144 Make_Attribute_Reference (Loc,
8145 Prefix => New_Copy_Tree (Target),
8146 Attribute_Name => Name_Address),
8148 Unchecked_Convert_To (RTE (RE_Tag),
8150 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
8152 New_Occurrence_Of (Standard_True, Loc),
8154 Unchecked_Convert_To
8155 (RTE (RE_Storage_Offset),
8156 Make_Attribute_Reference (Loc,
8158 Make_Selected_Component (Loc,
8159 Prefix => New_Copy_Tree (Target),
8161 New_Reference_To (Tag_Comp, Loc)),
8162 Attribute_Name => Name_Position)),
8171 Full_Typ : Entity_Id;
8172 Ifaces_List : Elist_Id;
8173 Ifaces_Comp_List : Elist_Id;
8174 Ifaces_Tag_List : Elist_Id;
8175 Iface_Elmt : Elmt_Id;
8176 Iface_Comp_Elmt : Elmt_Id;
8177 Iface_Tag_Elmt : Elmt_Id;
8179 In_Variable_Pos : Boolean;
8181 -- Start of processing for Init_Secondary_Tags
8184 -- Handle private types
8186 if Present (Full_View (Typ)) then
8187 Full_Typ := Full_View (Typ);
8192 Collect_Interfaces_Info
8193 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
8195 Iface_Elmt := First_Elmt (Ifaces_List);
8196 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
8197 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
8198 while Present (Iface_Elmt) loop
8199 Tag_Comp := Node (Iface_Comp_Elmt);
8201 -- Check if parent of record type has variable size components
8203 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
8204 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
8206 -- If we are compiling under the CPP full ABI compatibility mode and
8207 -- the ancestor is a CPP_Pragma tagged type then we generate code to
8208 -- initialize the secondary tag components from tags that reference
8209 -- secondary tables filled with copy of parent slots.
8211 if Is_CPP_Class (Root_Type (Full_Typ)) then
8213 -- Reject interface components located at variable offset in
8214 -- C++ derivations. This is currently unsupported.
8216 if not Fixed_Comps and then In_Variable_Pos then
8218 -- Locate the first dynamic component of the record. Done to
8219 -- improve the text of the warning.
8223 Comp_Typ : Entity_Id;
8226 Comp := First_Entity (Typ);
8227 while Present (Comp) loop
8228 Comp_Typ := Etype (Comp);
8230 if Ekind (Comp) /= E_Discriminant
8231 and then not Is_Tag (Comp)
8234 (Is_Record_Type (Comp_Typ)
8235 and then Is_Variable_Size_Record
8236 (Base_Type (Comp_Typ)))
8238 (Is_Array_Type (Comp_Typ)
8239 and then Is_Variable_Size_Array (Comp_Typ));
8245 pragma Assert (Present (Comp));
8246 Error_Msg_Node_2 := Comp;
8248 ("parent type & with dynamic component & cannot be parent"
8249 & " of 'C'P'P derivation if new interfaces are present",
8250 Typ, Scope (Original_Record_Component (Comp)));
8253 Sloc (Scope (Original_Record_Component (Comp)));
8255 ("type derived from 'C'P'P type & defined #",
8256 Typ, Scope (Original_Record_Component (Comp)));
8258 -- Avoid duplicated warnings
8263 -- Initialize secondary tags
8266 Append_To (Stmts_List,
8267 Make_Assignment_Statement (Loc,
8269 Make_Selected_Component (Loc,
8270 Prefix => New_Copy_Tree (Target),
8272 New_Reference_To (Node (Iface_Comp_Elmt), Loc)),
8274 New_Reference_To (Node (Iface_Tag_Elmt), Loc)));
8277 -- Otherwise generate code to initialize the tag
8280 if (In_Variable_Pos and then Variable_Comps)
8281 or else (not In_Variable_Pos and then Fixed_Comps)
8283 Initialize_Tag (Full_Typ,
8284 Iface => Node (Iface_Elmt),
8285 Tag_Comp => Tag_Comp,
8286 Iface_Tag => Node (Iface_Tag_Elmt));
8290 Next_Elmt (Iface_Elmt);
8291 Next_Elmt (Iface_Comp_Elmt);
8292 Next_Elmt (Iface_Tag_Elmt);
8294 end Init_Secondary_Tags;
8296 ------------------------
8297 -- Is_User_Defined_Eq --
8298 ------------------------
8300 function Is_User_Defined_Equality (Prim : Node_Id) return Boolean is
8302 return Chars (Prim) = Name_Op_Eq
8303 and then Etype (First_Formal (Prim)) =
8304 Etype (Next_Formal (First_Formal (Prim)))
8305 and then Base_Type (Etype (Prim)) = Standard_Boolean;
8306 end Is_User_Defined_Equality;
8308 ----------------------------------------
8309 -- Make_Controlling_Function_Wrappers --
8310 ----------------------------------------
8312 procedure Make_Controlling_Function_Wrappers
8313 (Tag_Typ : Entity_Id;
8314 Decl_List : out List_Id;
8315 Body_List : out List_Id)
8317 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8318 Prim_Elmt : Elmt_Id;
8320 Actual_List : List_Id;
8321 Formal_List : List_Id;
8323 Par_Formal : Entity_Id;
8324 Formal_Node : Node_Id;
8325 Func_Body : Node_Id;
8326 Func_Decl : Node_Id;
8327 Func_Spec : Node_Id;
8328 Return_Stmt : Node_Id;
8331 Decl_List := New_List;
8332 Body_List := New_List;
8334 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8336 while Present (Prim_Elmt) loop
8337 Subp := Node (Prim_Elmt);
8339 -- If a primitive function with a controlling result of the type has
8340 -- not been overridden by the user, then we must create a wrapper
8341 -- function here that effectively overrides it and invokes the
8342 -- (non-abstract) parent function. This can only occur for a null
8343 -- extension. Note that functions with anonymous controlling access
8344 -- results don't qualify and must be overridden. We also exclude
8345 -- Input attributes, since each type will have its own version of
8346 -- Input constructed by the expander. The test for Comes_From_Source
8347 -- is needed to distinguish inherited operations from renamings
8348 -- (which also have Alias set). We exclude internal entities with
8349 -- Interface_Alias to avoid generating duplicated wrappers since
8350 -- the primitive which covers the interface is also available in
8351 -- the list of primitive operations.
8353 -- The function may be abstract, or require_Overriding may be set
8354 -- for it, because tests for null extensions may already have reset
8355 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
8356 -- set, functions that need wrappers are recognized by having an
8357 -- alias that returns the parent type.
8359 if Comes_From_Source (Subp)
8360 or else No (Alias (Subp))
8361 or else Present (Interface_Alias (Subp))
8362 or else Ekind (Subp) /= E_Function
8363 or else not Has_Controlling_Result (Subp)
8364 or else Is_Access_Type (Etype (Subp))
8365 or else Is_Abstract_Subprogram (Alias (Subp))
8366 or else Is_TSS (Subp, TSS_Stream_Input)
8370 elsif Is_Abstract_Subprogram (Subp)
8371 or else Requires_Overriding (Subp)
8373 (Is_Null_Extension (Etype (Subp))
8374 and then Etype (Alias (Subp)) /= Etype (Subp))
8376 Formal_List := No_List;
8377 Formal := First_Formal (Subp);
8379 if Present (Formal) then
8380 Formal_List := New_List;
8382 while Present (Formal) loop
8384 (Make_Parameter_Specification
8386 Defining_Identifier =>
8387 Make_Defining_Identifier (Sloc (Formal),
8388 Chars => Chars (Formal)),
8389 In_Present => In_Present (Parent (Formal)),
8390 Out_Present => Out_Present (Parent (Formal)),
8391 Null_Exclusion_Present =>
8392 Null_Exclusion_Present (Parent (Formal)),
8394 New_Reference_To (Etype (Formal), Loc),
8396 New_Copy_Tree (Expression (Parent (Formal)))),
8399 Next_Formal (Formal);
8404 Make_Function_Specification (Loc,
8405 Defining_Unit_Name =>
8406 Make_Defining_Identifier (Loc,
8407 Chars => Chars (Subp)),
8408 Parameter_Specifications => Formal_List,
8409 Result_Definition =>
8410 New_Reference_To (Etype (Subp), Loc));
8412 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
8413 Append_To (Decl_List, Func_Decl);
8415 -- Build a wrapper body that calls the parent function. The body
8416 -- contains a single return statement that returns an extension
8417 -- aggregate whose ancestor part is a call to the parent function,
8418 -- passing the formals as actuals (with any controlling arguments
8419 -- converted to the types of the corresponding formals of the
8420 -- parent function, which might be anonymous access types), and
8421 -- having a null extension.
8423 Formal := First_Formal (Subp);
8424 Par_Formal := First_Formal (Alias (Subp));
8425 Formal_Node := First (Formal_List);
8427 if Present (Formal) then
8428 Actual_List := New_List;
8430 Actual_List := No_List;
8433 while Present (Formal) loop
8434 if Is_Controlling_Formal (Formal) then
8435 Append_To (Actual_List,
8436 Make_Type_Conversion (Loc,
8438 New_Occurrence_Of (Etype (Par_Formal), Loc),
8441 (Defining_Identifier (Formal_Node), Loc)));
8446 (Defining_Identifier (Formal_Node), Loc));
8449 Next_Formal (Formal);
8450 Next_Formal (Par_Formal);
8455 Make_Simple_Return_Statement (Loc,
8457 Make_Extension_Aggregate (Loc,
8459 Make_Function_Call (Loc,
8460 Name => New_Reference_To (Alias (Subp), Loc),
8461 Parameter_Associations => Actual_List),
8462 Null_Record_Present => True));
8465 Make_Subprogram_Body (Loc,
8466 Specification => New_Copy_Tree (Func_Spec),
8467 Declarations => Empty_List,
8468 Handled_Statement_Sequence =>
8469 Make_Handled_Sequence_Of_Statements (Loc,
8470 Statements => New_List (Return_Stmt)));
8472 Set_Defining_Unit_Name
8473 (Specification (Func_Body),
8474 Make_Defining_Identifier (Loc, Chars (Subp)));
8476 Append_To (Body_List, Func_Body);
8478 -- Replace the inherited function with the wrapper function in the
8479 -- primitive operations list. We add the minimum decoration needed
8480 -- to override interface primitives.
8482 Set_Ekind (Defining_Unit_Name (Func_Spec), E_Function);
8484 Override_Dispatching_Operation
8485 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec),
8486 Is_Wrapper => True);
8490 Next_Elmt (Prim_Elmt);
8492 end Make_Controlling_Function_Wrappers;
8498 function Make_Eq_Body
8500 Eq_Name : Name_Id) return Node_Id
8502 Loc : constant Source_Ptr := Sloc (Parent (Typ));
8504 Def : constant Node_Id := Parent (Typ);
8505 Stmts : constant List_Id := New_List;
8506 Variant_Case : Boolean := Has_Discriminants (Typ);
8507 Comps : Node_Id := Empty;
8508 Typ_Def : Node_Id := Type_Definition (Def);
8512 Predef_Spec_Or_Body (Loc,
8515 Profile => New_List (
8516 Make_Parameter_Specification (Loc,
8517 Defining_Identifier =>
8518 Make_Defining_Identifier (Loc, Name_X),
8519 Parameter_Type => New_Reference_To (Typ, Loc)),
8521 Make_Parameter_Specification (Loc,
8522 Defining_Identifier =>
8523 Make_Defining_Identifier (Loc, Name_Y),
8524 Parameter_Type => New_Reference_To (Typ, Loc))),
8526 Ret_Type => Standard_Boolean,
8529 if Variant_Case then
8530 if Nkind (Typ_Def) = N_Derived_Type_Definition then
8531 Typ_Def := Record_Extension_Part (Typ_Def);
8534 if Present (Typ_Def) then
8535 Comps := Component_List (Typ_Def);
8539 Present (Comps) and then Present (Variant_Part (Comps));
8542 if Variant_Case then
8544 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
8545 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
8547 Make_Simple_Return_Statement (Loc,
8548 Expression => New_Reference_To (Standard_True, Loc)));
8552 Make_Simple_Return_Statement (Loc,
8554 Expand_Record_Equality
8557 Lhs => Make_Identifier (Loc, Name_X),
8558 Rhs => Make_Identifier (Loc, Name_Y),
8559 Bodies => Declarations (Decl))));
8562 Set_Handled_Statement_Sequence
8563 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8571 -- <Make_Eq_If shared components>
8573 -- when V1 => <Make_Eq_Case> on subcomponents
8575 -- when Vn => <Make_Eq_Case> on subcomponents
8578 function Make_Eq_Case
8581 Discr : Entity_Id := Empty) return List_Id
8583 Loc : constant Source_Ptr := Sloc (E);
8584 Result : constant List_Id := New_List;
8589 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
8591 if No (Variant_Part (CL)) then
8595 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
8597 if No (Variant) then
8601 Alt_List := New_List;
8603 while Present (Variant) loop
8604 Append_To (Alt_List,
8605 Make_Case_Statement_Alternative (Loc,
8606 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
8607 Statements => Make_Eq_Case (E, Component_List (Variant))));
8609 Next_Non_Pragma (Variant);
8612 -- If we have an Unchecked_Union, use one of the parameters that
8613 -- captures the discriminants.
8615 if Is_Unchecked_Union (E) then
8617 Make_Case_Statement (Loc,
8618 Expression => New_Reference_To (Discr, Loc),
8619 Alternatives => Alt_List));
8623 Make_Case_Statement (Loc,
8625 Make_Selected_Component (Loc,
8626 Prefix => Make_Identifier (Loc, Name_X),
8627 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
8628 Alternatives => Alt_List));
8649 -- or a null statement if the list L is empty
8653 L : List_Id) return Node_Id
8655 Loc : constant Source_Ptr := Sloc (E);
8657 Field_Name : Name_Id;
8662 return Make_Null_Statement (Loc);
8667 C := First_Non_Pragma (L);
8668 while Present (C) loop
8669 Field_Name := Chars (Defining_Identifier (C));
8671 -- The tags must not be compared: they are not part of the value.
8672 -- Ditto for parent interfaces because their equality operator is
8675 -- Note also that in the following, we use Make_Identifier for
8676 -- the component names. Use of New_Reference_To to identify the
8677 -- components would be incorrect because the wrong entities for
8678 -- discriminants could be picked up in the private type case.
8680 if Field_Name = Name_uParent
8681 and then Is_Interface (Etype (Defining_Identifier (C)))
8685 elsif Field_Name /= Name_uTag then
8686 Evolve_Or_Else (Cond,
8689 Make_Selected_Component (Loc,
8690 Prefix => Make_Identifier (Loc, Name_X),
8691 Selector_Name => Make_Identifier (Loc, Field_Name)),
8694 Make_Selected_Component (Loc,
8695 Prefix => Make_Identifier (Loc, Name_Y),
8696 Selector_Name => Make_Identifier (Loc, Field_Name))));
8699 Next_Non_Pragma (C);
8703 return Make_Null_Statement (Loc);
8707 Make_Implicit_If_Statement (E,
8709 Then_Statements => New_List (
8710 Make_Simple_Return_Statement (Loc,
8711 Expression => New_Occurrence_Of (Standard_False, Loc))));
8716 --------------------
8718 --------------------
8720 function Make_Neq_Body (Tag_Typ : Entity_Id) return Node_Id is
8722 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean;
8723 -- Returns true if Prim is a renaming of an unresolved predefined
8724 -- inequality operation.
8726 --------------------------------
8727 -- Is_Predefined_Neq_Renaming --
8728 --------------------------------
8730 function Is_Predefined_Neq_Renaming (Prim : Node_Id) return Boolean is
8732 return Chars (Prim) /= Name_Op_Ne
8733 and then Present (Alias (Prim))
8734 and then Comes_From_Source (Prim)
8735 and then Is_Intrinsic_Subprogram (Alias (Prim))
8736 and then Chars (Alias (Prim)) = Name_Op_Ne;
8737 end Is_Predefined_Neq_Renaming;
8741 Loc : constant Source_Ptr := Sloc (Parent (Tag_Typ));
8742 Stmts : constant List_Id := New_List;
8744 Eq_Prim : Entity_Id;
8745 Left_Op : Entity_Id;
8746 Renaming_Prim : Entity_Id;
8747 Right_Op : Entity_Id;
8750 -- Start of processing for Make_Neq_Body
8753 -- For a call on a renaming of a dispatching subprogram that is
8754 -- overridden, if the overriding occurred before the renaming, then
8755 -- the body executed is that of the overriding declaration, even if the
8756 -- overriding declaration is not visible at the place of the renaming;
8757 -- otherwise, the inherited or predefined subprogram is called, see
8760 -- Stage 1: Search for a renaming of the inequality primitive and also
8761 -- search for an overriding of the equality primitive located before the
8762 -- renaming declaration.
8770 Renaming_Prim := Empty;
8772 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8773 while Present (Elmt) loop
8774 Prim := Node (Elmt);
8776 if Is_User_Defined_Equality (Prim)
8777 and then No (Alias (Prim))
8779 if No (Renaming_Prim) then
8780 pragma Assert (No (Eq_Prim));
8784 elsif Is_Predefined_Neq_Renaming (Prim) then
8785 Renaming_Prim := Prim;
8792 -- No further action needed if no renaming was found
8794 if No (Renaming_Prim) then
8798 -- Stage 2: Replace the renaming declaration by a subprogram declaration
8799 -- (required to add its body)
8801 Decl := Parent (Parent (Renaming_Prim));
8803 Make_Subprogram_Declaration (Loc,
8804 Specification => Specification (Decl)));
8805 Set_Analyzed (Decl);
8807 -- Remove the decoration of intrinsic renaming subprogram
8809 Set_Is_Intrinsic_Subprogram (Renaming_Prim, False);
8810 Set_Convention (Renaming_Prim, Convention_Ada);
8811 Set_Alias (Renaming_Prim, Empty);
8812 Set_Has_Completion (Renaming_Prim, False);
8814 -- Stage 3: Build the corresponding body
8816 Left_Op := First_Formal (Renaming_Prim);
8817 Right_Op := Next_Formal (Left_Op);
8820 Predef_Spec_Or_Body (Loc,
8822 Name => Chars (Renaming_Prim),
8823 Profile => New_List (
8824 Make_Parameter_Specification (Loc,
8825 Defining_Identifier =>
8826 Make_Defining_Identifier (Loc, Chars (Left_Op)),
8827 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8829 Make_Parameter_Specification (Loc,
8830 Defining_Identifier =>
8831 Make_Defining_Identifier (Loc, Chars (Right_Op)),
8832 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8834 Ret_Type => Standard_Boolean,
8837 -- If the overriding of the equality primitive occurred before the
8838 -- renaming, then generate:
8840 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8842 -- return not Oeq (X, Y);
8845 if Present (Eq_Prim) then
8848 -- Otherwise build a nested subprogram which performs the predefined
8849 -- evaluation of the equality operator. That is, generate:
8851 -- function <Neq_Name> (X : Y : Typ) return Boolean is
8852 -- function Oeq (X : Y) return Boolean is
8854 -- <<body of default implementation>>
8857 -- return not Oeq (X, Y);
8862 Local_Subp : Node_Id;
8864 Local_Subp := Make_Eq_Body (Tag_Typ, Name_Op_Eq);
8865 Set_Declarations (Decl, New_List (Local_Subp));
8866 Target := Defining_Entity (Local_Subp);
8871 Make_Simple_Return_Statement (Loc,
8874 Make_Function_Call (Loc,
8875 Name => New_Reference_To (Target, Loc),
8876 Parameter_Associations => New_List (
8877 Make_Identifier (Loc, Chars (Left_Op)),
8878 Make_Identifier (Loc, Chars (Right_Op)))))));
8880 Set_Handled_Statement_Sequence
8881 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8885 -------------------------------
8886 -- Make_Null_Procedure_Specs --
8887 -------------------------------
8889 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8890 Decl_List : constant List_Id := New_List;
8891 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8893 Formal_List : List_Id;
8894 New_Param_Spec : Node_Id;
8895 Parent_Subp : Entity_Id;
8896 Prim_Elmt : Elmt_Id;
8900 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8901 while Present (Prim_Elmt) loop
8902 Subp := Node (Prim_Elmt);
8904 -- If a null procedure inherited from an interface has not been
8905 -- overridden, then we build a null procedure declaration to
8906 -- override the inherited procedure.
8908 Parent_Subp := Alias (Subp);
8910 if Present (Parent_Subp)
8911 and then Is_Null_Interface_Primitive (Parent_Subp)
8913 Formal_List := No_List;
8914 Formal := First_Formal (Subp);
8916 if Present (Formal) then
8917 Formal_List := New_List;
8919 while Present (Formal) loop
8921 -- Copy the parameter spec including default expressions
8924 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
8926 -- Generate a new defining identifier for the new formal.
8927 -- required because New_Copy_Tree does not duplicate
8928 -- semantic fields (except itypes).
8930 Set_Defining_Identifier (New_Param_Spec,
8931 Make_Defining_Identifier (Sloc (Formal),
8932 Chars => Chars (Formal)));
8934 -- For controlling arguments we must change their
8935 -- parameter type to reference the tagged type (instead
8936 -- of the interface type)
8938 if Is_Controlling_Formal (Formal) then
8939 if Nkind (Parameter_Type (Parent (Formal)))
8942 Set_Parameter_Type (New_Param_Spec,
8943 New_Occurrence_Of (Tag_Typ, Loc));
8946 (Nkind (Parameter_Type (Parent (Formal)))
8947 = N_Access_Definition);
8948 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
8949 New_Occurrence_Of (Tag_Typ, Loc));
8953 Append (New_Param_Spec, Formal_List);
8955 Next_Formal (Formal);
8959 Append_To (Decl_List,
8960 Make_Subprogram_Declaration (Loc,
8961 Make_Procedure_Specification (Loc,
8962 Defining_Unit_Name =>
8963 Make_Defining_Identifier (Loc, Chars (Subp)),
8964 Parameter_Specifications => Formal_List,
8965 Null_Present => True)));
8968 Next_Elmt (Prim_Elmt);
8972 end Make_Null_Procedure_Specs;
8974 -------------------------------------
8975 -- Make_Predefined_Primitive_Specs --
8976 -------------------------------------
8978 procedure Make_Predefined_Primitive_Specs
8979 (Tag_Typ : Entity_Id;
8980 Predef_List : out List_Id;
8981 Renamed_Eq : out Entity_Id)
8983 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
8984 -- Returns true if Prim is a renaming of an unresolved predefined
8985 -- equality operation.
8987 -------------------------------
8988 -- Is_Predefined_Eq_Renaming --
8989 -------------------------------
8991 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
8993 return Chars (Prim) /= Name_Op_Eq
8994 and then Present (Alias (Prim))
8995 and then Comes_From_Source (Prim)
8996 and then Is_Intrinsic_Subprogram (Alias (Prim))
8997 and then Chars (Alias (Prim)) = Name_Op_Eq;
8998 end Is_Predefined_Eq_Renaming;
9002 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9003 Res : constant List_Id := New_List;
9004 Eq_Name : Name_Id := Name_Op_Eq;
9005 Eq_Needed : Boolean;
9009 Has_Predef_Eq_Renaming : Boolean := False;
9010 -- Set to True if Tag_Typ has a primitive that renames the predefined
9011 -- equality operator. Used to implement (RM 8-5-4(8)).
9013 -- Start of processing for Make_Predefined_Primitive_Specs
9016 Renamed_Eq := Empty;
9020 Append_To (Res, Predef_Spec_Or_Body (Loc,
9023 Profile => New_List (
9024 Make_Parameter_Specification (Loc,
9025 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9026 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
9028 Ret_Type => Standard_Long_Long_Integer));
9030 -- Specs for dispatching stream attributes
9033 Stream_Op_TSS_Names :
9034 constant array (Integer range <>) of TSS_Name_Type :=
9041 for Op in Stream_Op_TSS_Names'Range loop
9042 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
9044 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
9045 Stream_Op_TSS_Names (Op)));
9050 -- Spec of "=" is expanded if the type is not limited and if a user
9051 -- defined "=" was not already declared for the non-full view of a
9052 -- private extension
9054 if not Is_Limited_Type (Tag_Typ) then
9056 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9057 while Present (Prim) loop
9059 -- If a primitive is encountered that renames the predefined
9060 -- equality operator before reaching any explicit equality
9061 -- primitive, then we still need to create a predefined equality
9062 -- function, because calls to it can occur via the renaming. A
9063 -- new name is created for the equality to avoid conflicting with
9064 -- any user-defined equality. (Note that this doesn't account for
9065 -- renamings of equality nested within subpackages???)
9067 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9068 Has_Predef_Eq_Renaming := True;
9069 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
9071 -- User-defined equality
9073 elsif Is_User_Defined_Equality (Node (Prim)) then
9074 if No (Alias (Node (Prim)))
9075 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
9076 N_Subprogram_Renaming_Declaration
9081 -- If the parent is not an interface type and has an abstract
9082 -- equality function, the inherited equality is abstract as
9083 -- well, and no body can be created for it.
9085 elsif not Is_Interface (Etype (Tag_Typ))
9086 and then Present (Alias (Node (Prim)))
9087 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
9092 -- If the type has an equality function corresponding with
9093 -- a primitive defined in an interface type, the inherited
9094 -- equality is abstract as well, and no body can be created
9097 elsif Present (Alias (Node (Prim)))
9098 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
9101 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
9111 -- If a renaming of predefined equality was found but there was no
9112 -- user-defined equality (so Eq_Needed is still true), then set the
9113 -- name back to Name_Op_Eq. But in the case where a user-defined
9114 -- equality was located after such a renaming, then the predefined
9115 -- equality function is still needed, so Eq_Needed must be set back
9118 if Eq_Name /= Name_Op_Eq then
9120 Eq_Name := Name_Op_Eq;
9127 Eq_Spec := Predef_Spec_Or_Body (Loc,
9130 Profile => New_List (
9131 Make_Parameter_Specification (Loc,
9132 Defining_Identifier =>
9133 Make_Defining_Identifier (Loc, Name_X),
9134 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
9135 Make_Parameter_Specification (Loc,
9136 Defining_Identifier =>
9137 Make_Defining_Identifier (Loc, Name_Y),
9138 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
9139 Ret_Type => Standard_Boolean);
9140 Append_To (Res, Eq_Spec);
9142 if Has_Predef_Eq_Renaming then
9143 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
9145 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9146 while Present (Prim) loop
9148 -- Any renamings of equality that appeared before an
9149 -- overriding equality must be updated to refer to the
9150 -- entity for the predefined equality, otherwise calls via
9151 -- the renaming would get incorrectly resolved to call the
9152 -- user-defined equality function.
9154 if Is_Predefined_Eq_Renaming (Node (Prim)) then
9155 Set_Alias (Node (Prim), Renamed_Eq);
9157 -- Exit upon encountering a user-defined equality
9159 elsif Chars (Node (Prim)) = Name_Op_Eq
9160 and then No (Alias (Node (Prim)))
9170 -- Spec for dispatching assignment
9172 Append_To (Res, Predef_Spec_Or_Body (Loc,
9174 Name => Name_uAssign,
9175 Profile => New_List (
9176 Make_Parameter_Specification (Loc,
9177 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9178 Out_Present => True,
9179 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
9181 Make_Parameter_Specification (Loc,
9182 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9183 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
9186 -- Ada 2005: Generate declarations for the following primitive
9187 -- operations for limited interfaces and synchronized types that
9188 -- implement a limited interface.
9190 -- Disp_Asynchronous_Select
9191 -- Disp_Conditional_Select
9192 -- Disp_Get_Prim_Op_Kind
9195 -- Disp_Timed_Select
9197 -- Disable the generation of these bodies if No_Dispatching_Calls,
9198 -- Ravenscar or ZFP is active.
9200 if Ada_Version >= Ada_2005
9201 and then not Restriction_Active (No_Dispatching_Calls)
9202 and then not Restriction_Active (No_Select_Statements)
9203 and then RTE_Available (RE_Select_Specific_Data)
9205 -- These primitives are defined abstract in interface types
9207 if Is_Interface (Tag_Typ)
9208 and then Is_Limited_Record (Tag_Typ)
9211 Make_Abstract_Subprogram_Declaration (Loc,
9213 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9216 Make_Abstract_Subprogram_Declaration (Loc,
9218 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9221 Make_Abstract_Subprogram_Declaration (Loc,
9223 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9226 Make_Abstract_Subprogram_Declaration (Loc,
9228 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9231 Make_Abstract_Subprogram_Declaration (Loc,
9233 Make_Disp_Requeue_Spec (Tag_Typ)));
9236 Make_Abstract_Subprogram_Declaration (Loc,
9238 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9240 -- If the ancestor is an interface type we declare non-abstract
9241 -- primitives to override the abstract primitives of the interface
9244 -- In VM targets we define these primitives in all root tagged types
9245 -- that are not interface types. Done because in VM targets we don't
9246 -- have secondary dispatch tables and any derivation of Tag_Typ may
9247 -- cover limited interfaces (which always have these primitives since
9248 -- they may be ancestors of synchronized interface types).
9250 elsif (not Is_Interface (Tag_Typ)
9251 and then Is_Interface (Etype (Tag_Typ))
9252 and then Is_Limited_Record (Etype (Tag_Typ)))
9254 (Is_Concurrent_Record_Type (Tag_Typ)
9255 and then Has_Interfaces (Tag_Typ))
9257 (not Tagged_Type_Expansion
9258 and then not Is_Interface (Tag_Typ)
9259 and then Tag_Typ = Root_Type (Tag_Typ))
9262 Make_Subprogram_Declaration (Loc,
9264 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
9267 Make_Subprogram_Declaration (Loc,
9269 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
9272 Make_Subprogram_Declaration (Loc,
9274 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
9277 Make_Subprogram_Declaration (Loc,
9279 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
9282 Make_Subprogram_Declaration (Loc,
9284 Make_Disp_Requeue_Spec (Tag_Typ)));
9287 Make_Subprogram_Declaration (Loc,
9289 Make_Disp_Timed_Select_Spec (Tag_Typ)));
9293 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
9294 -- regardless of whether they are controlled or may contain controlled
9297 -- Do not generate the routines if finalization is disabled
9299 if Restriction_Active (No_Finalization) then
9302 -- Finalization is not available for CIL value types
9304 elsif Is_Value_Type (Tag_Typ) then
9308 if not Is_Limited_Type (Tag_Typ) then
9309 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
9312 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
9316 end Make_Predefined_Primitive_Specs;
9318 ---------------------------------
9319 -- Needs_Simple_Initialization --
9320 ---------------------------------
9322 function Needs_Simple_Initialization
9324 Consider_IS : Boolean := True) return Boolean
9326 Consider_IS_NS : constant Boolean :=
9328 or (Initialize_Scalars and Consider_IS);
9331 -- Never need initialization if it is suppressed
9333 if Initialization_Suppressed (T) then
9337 -- Check for private type, in which case test applies to the underlying
9338 -- type of the private type.
9340 if Is_Private_Type (T) then
9342 RT : constant Entity_Id := Underlying_Type (T);
9345 if Present (RT) then
9346 return Needs_Simple_Initialization (RT);
9352 -- Scalar type with Default_Value aspect requires initialization
9354 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
9357 -- Cases needing simple initialization are access types, and, if pragma
9358 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
9361 elsif Is_Access_Type (T)
9362 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
9366 -- If Initialize/Normalize_Scalars is in effect, string objects also
9367 -- need initialization, unless they are created in the course of
9368 -- expanding an aggregate (since in the latter case they will be
9369 -- filled with appropriate initializing values before they are used).
9371 elsif Consider_IS_NS
9373 (Root_Type (T) = Standard_String
9374 or else Root_Type (T) = Standard_Wide_String
9375 or else Root_Type (T) = Standard_Wide_Wide_String)
9378 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
9385 end Needs_Simple_Initialization;
9387 ----------------------
9388 -- Predef_Deep_Spec --
9389 ----------------------
9391 function Predef_Deep_Spec
9393 Tag_Typ : Entity_Id;
9394 Name : TSS_Name_Type;
9395 For_Body : Boolean := False) return Node_Id
9400 -- V : in out Tag_Typ
9402 Formals := New_List (
9403 Make_Parameter_Specification (Loc,
9404 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
9406 Out_Present => True,
9407 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
9409 -- F : Boolean := True
9411 if Name = TSS_Deep_Adjust
9412 or else Name = TSS_Deep_Finalize
9415 Make_Parameter_Specification (Loc,
9416 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
9417 Parameter_Type => New_Reference_To (Standard_Boolean, Loc),
9418 Expression => New_Reference_To (Standard_True, Loc)));
9422 Predef_Spec_Or_Body (Loc,
9423 Name => Make_TSS_Name (Tag_Typ, Name),
9426 For_Body => For_Body);
9429 when RE_Not_Available =>
9431 end Predef_Deep_Spec;
9433 -------------------------
9434 -- Predef_Spec_Or_Body --
9435 -------------------------
9437 function Predef_Spec_Or_Body
9439 Tag_Typ : Entity_Id;
9442 Ret_Type : Entity_Id := Empty;
9443 For_Body : Boolean := False) return Node_Id
9445 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
9449 Set_Is_Public (Id, Is_Public (Tag_Typ));
9451 -- The internal flag is set to mark these declarations because they have
9452 -- specific properties. First, they are primitives even if they are not
9453 -- defined in the type scope (the freezing point is not necessarily in
9454 -- the same scope). Second, the predefined equality can be overridden by
9455 -- a user-defined equality, no body will be generated in this case.
9457 Set_Is_Internal (Id);
9459 if not Debug_Generated_Code then
9460 Set_Debug_Info_Off (Id);
9463 if No (Ret_Type) then
9465 Make_Procedure_Specification (Loc,
9466 Defining_Unit_Name => Id,
9467 Parameter_Specifications => Profile);
9470 Make_Function_Specification (Loc,
9471 Defining_Unit_Name => Id,
9472 Parameter_Specifications => Profile,
9473 Result_Definition => New_Reference_To (Ret_Type, Loc));
9476 if Is_Interface (Tag_Typ) then
9477 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9479 -- If body case, return empty subprogram body. Note that this is ill-
9480 -- formed, because there is not even a null statement, and certainly not
9481 -- a return in the function case. The caller is expected to do surgery
9482 -- on the body to add the appropriate stuff.
9485 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
9487 -- For the case of an Input attribute predefined for an abstract type,
9488 -- generate an abstract specification. This will never be called, but we
9489 -- need the slot allocated in the dispatching table so that attributes
9490 -- typ'Class'Input and typ'Class'Output will work properly.
9492 elsif Is_TSS (Name, TSS_Stream_Input)
9493 and then Is_Abstract_Type (Tag_Typ)
9495 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
9497 -- Normal spec case, where we return a subprogram declaration
9500 return Make_Subprogram_Declaration (Loc, Spec);
9502 end Predef_Spec_Or_Body;
9504 -----------------------------
9505 -- Predef_Stream_Attr_Spec --
9506 -----------------------------
9508 function Predef_Stream_Attr_Spec
9510 Tag_Typ : Entity_Id;
9511 Name : TSS_Name_Type;
9512 For_Body : Boolean := False) return Node_Id
9514 Ret_Type : Entity_Id;
9517 if Name = TSS_Stream_Input then
9518 Ret_Type := Tag_Typ;
9526 Name => Make_TSS_Name (Tag_Typ, Name),
9528 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
9529 Ret_Type => Ret_Type,
9530 For_Body => For_Body);
9531 end Predef_Stream_Attr_Spec;
9533 ---------------------------------
9534 -- Predefined_Primitive_Bodies --
9535 ---------------------------------
9537 function Predefined_Primitive_Bodies
9538 (Tag_Typ : Entity_Id;
9539 Renamed_Eq : Entity_Id) return List_Id
9541 Loc : constant Source_Ptr := Sloc (Tag_Typ);
9542 Res : constant List_Id := New_List;
9545 Eq_Needed : Boolean;
9549 pragma Warnings (Off, Ent);
9552 pragma Assert (not Is_Interface (Tag_Typ));
9554 -- See if we have a predefined "=" operator
9556 if Present (Renamed_Eq) then
9558 Eq_Name := Chars (Renamed_Eq);
9560 -- If the parent is an interface type then it has defined all the
9561 -- predefined primitives abstract and we need to check if the type
9562 -- has some user defined "=" function to avoid generating it.
9564 elsif Is_Interface (Etype (Tag_Typ)) then
9566 Eq_Name := Name_Op_Eq;
9568 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9569 while Present (Prim) loop
9570 if Chars (Node (Prim)) = Name_Op_Eq
9571 and then not Is_Internal (Node (Prim))
9585 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9586 while Present (Prim) loop
9587 if Chars (Node (Prim)) = Name_Op_Eq
9588 and then Is_Internal (Node (Prim))
9591 Eq_Name := Name_Op_Eq;
9601 Decl := Predef_Spec_Or_Body (Loc,
9604 Profile => New_List (
9605 Make_Parameter_Specification (Loc,
9606 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9607 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
9609 Ret_Type => Standard_Long_Long_Integer,
9612 Set_Handled_Statement_Sequence (Decl,
9613 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9614 Make_Simple_Return_Statement (Loc,
9616 Make_Attribute_Reference (Loc,
9617 Prefix => Make_Identifier (Loc, Name_X),
9618 Attribute_Name => Name_Size)))));
9620 Append_To (Res, Decl);
9622 -- Bodies for Dispatching stream IO routines. We need these only for
9623 -- non-limited types (in the limited case there is no dispatching).
9624 -- We also skip them if dispatching or finalization are not available.
9626 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
9627 and then No (TSS (Tag_Typ, TSS_Stream_Read))
9629 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
9630 Append_To (Res, Decl);
9633 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
9634 and then No (TSS (Tag_Typ, TSS_Stream_Write))
9636 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
9637 Append_To (Res, Decl);
9640 -- Skip body of _Input for the abstract case, since the corresponding
9641 -- spec is abstract (see Predef_Spec_Or_Body).
9643 if not Is_Abstract_Type (Tag_Typ)
9644 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
9645 and then No (TSS (Tag_Typ, TSS_Stream_Input))
9647 Build_Record_Or_Elementary_Input_Function
9648 (Loc, Tag_Typ, Decl, Ent);
9649 Append_To (Res, Decl);
9652 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
9653 and then No (TSS (Tag_Typ, TSS_Stream_Output))
9655 Build_Record_Or_Elementary_Output_Procedure
9656 (Loc, Tag_Typ, Decl, Ent);
9657 Append_To (Res, Decl);
9660 -- Ada 2005: Generate bodies for the following primitive operations for
9661 -- limited interfaces and synchronized types that implement a limited
9664 -- disp_asynchronous_select
9665 -- disp_conditional_select
9666 -- disp_get_prim_op_kind
9668 -- disp_timed_select
9670 -- The interface versions will have null bodies
9672 -- Disable the generation of these bodies if No_Dispatching_Calls,
9673 -- Ravenscar or ZFP is active.
9675 -- In VM targets we define these primitives in all root tagged types
9676 -- that are not interface types. Done because in VM targets we don't
9677 -- have secondary dispatch tables and any derivation of Tag_Typ may
9678 -- cover limited interfaces (which always have these primitives since
9679 -- they may be ancestors of synchronized interface types).
9681 if Ada_Version >= Ada_2005
9682 and then not Is_Interface (Tag_Typ)
9684 ((Is_Interface (Etype (Tag_Typ))
9685 and then Is_Limited_Record (Etype (Tag_Typ)))
9687 (Is_Concurrent_Record_Type (Tag_Typ)
9688 and then Has_Interfaces (Tag_Typ))
9690 (not Tagged_Type_Expansion
9691 and then Tag_Typ = Root_Type (Tag_Typ)))
9692 and then not Restriction_Active (No_Dispatching_Calls)
9693 and then not Restriction_Active (No_Select_Statements)
9694 and then RTE_Available (RE_Select_Specific_Data)
9696 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
9697 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
9698 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
9699 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
9700 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
9701 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
9704 if not Is_Limited_Type (Tag_Typ)
9705 and then not Is_Interface (Tag_Typ)
9707 -- Body for equality
9710 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
9711 Append_To (Res, Decl);
9714 -- Body for inequality (if required!)
9716 Decl := Make_Neq_Body (Tag_Typ);
9718 if Present (Decl) then
9719 Append_To (Res, Decl);
9722 -- Body for dispatching assignment
9725 Predef_Spec_Or_Body (Loc,
9727 Name => Name_uAssign,
9728 Profile => New_List (
9729 Make_Parameter_Specification (Loc,
9730 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
9731 Out_Present => True,
9732 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
9734 Make_Parameter_Specification (Loc,
9735 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
9736 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
9739 Set_Handled_Statement_Sequence (Decl,
9740 Make_Handled_Sequence_Of_Statements (Loc, New_List (
9741 Make_Assignment_Statement (Loc,
9742 Name => Make_Identifier (Loc, Name_X),
9743 Expression => Make_Identifier (Loc, Name_Y)))));
9745 Append_To (Res, Decl);
9748 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
9749 -- tagged types which do not contain controlled components.
9751 -- Do not generate the routines if finalization is disabled
9753 if Restriction_Active (No_Finalization) then
9756 elsif not Has_Controlled_Component (Tag_Typ) then
9757 if not Is_Limited_Type (Tag_Typ) then
9758 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
9760 if Is_Controlled (Tag_Typ) then
9761 Set_Handled_Statement_Sequence (Decl,
9762 Make_Handled_Sequence_Of_Statements (Loc,
9763 Statements => New_List (
9765 Obj_Ref => Make_Identifier (Loc, Name_V),
9768 Set_Handled_Statement_Sequence (Decl,
9769 Make_Handled_Sequence_Of_Statements (Loc,
9770 Statements => New_List (
9771 Make_Null_Statement (Loc))));
9774 Append_To (Res, Decl);
9777 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
9779 if Is_Controlled (Tag_Typ) then
9780 Set_Handled_Statement_Sequence (Decl,
9781 Make_Handled_Sequence_Of_Statements (Loc,
9782 Statements => New_List (
9784 (Obj_Ref => Make_Identifier (Loc, Name_V),
9787 Set_Handled_Statement_Sequence (Decl,
9788 Make_Handled_Sequence_Of_Statements (Loc,
9789 Statements => New_List (Make_Null_Statement (Loc))));
9792 Append_To (Res, Decl);
9796 end Predefined_Primitive_Bodies;
9798 ---------------------------------
9799 -- Predefined_Primitive_Freeze --
9800 ---------------------------------
9802 function Predefined_Primitive_Freeze
9803 (Tag_Typ : Entity_Id) return List_Id
9805 Res : constant List_Id := New_List;
9810 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9811 while Present (Prim) loop
9812 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
9813 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
9815 if Present (Frnodes) then
9816 Append_List_To (Res, Frnodes);
9824 end Predefined_Primitive_Freeze;
9826 -------------------------
9827 -- Stream_Operation_OK --
9828 -------------------------
9830 function Stream_Operation_OK
9832 Operation : TSS_Name_Type) return Boolean
9834 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
9837 -- Special case of a limited type extension: a default implementation
9838 -- of the stream attributes Read or Write exists if that attribute
9839 -- has been specified or is available for an ancestor type; a default
9840 -- implementation of the attribute Output (resp. Input) exists if the
9841 -- attribute has been specified or Write (resp. Read) is available for
9842 -- an ancestor type. The last condition only applies under Ada 2005.
9844 if Is_Limited_Type (Typ)
9845 and then Is_Tagged_Type (Typ)
9847 if Operation = TSS_Stream_Read then
9848 Has_Predefined_Or_Specified_Stream_Attribute :=
9849 Has_Specified_Stream_Read (Typ);
9851 elsif Operation = TSS_Stream_Write then
9852 Has_Predefined_Or_Specified_Stream_Attribute :=
9853 Has_Specified_Stream_Write (Typ);
9855 elsif Operation = TSS_Stream_Input then
9856 Has_Predefined_Or_Specified_Stream_Attribute :=
9857 Has_Specified_Stream_Input (Typ)
9859 (Ada_Version >= Ada_2005
9860 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
9862 elsif Operation = TSS_Stream_Output then
9863 Has_Predefined_Or_Specified_Stream_Attribute :=
9864 Has_Specified_Stream_Output (Typ)
9866 (Ada_Version >= Ada_2005
9867 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
9870 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
9872 if not Has_Predefined_Or_Specified_Stream_Attribute
9873 and then Is_Derived_Type (Typ)
9874 and then (Operation = TSS_Stream_Read
9875 or else Operation = TSS_Stream_Write)
9877 Has_Predefined_Or_Specified_Stream_Attribute :=
9879 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
9883 -- If the type is not limited, or else is limited but the attribute is
9884 -- explicitly specified or is predefined for the type, then return True,
9885 -- unless other conditions prevail, such as restrictions prohibiting
9886 -- streams or dispatching operations. We also return True for limited
9887 -- interfaces, because they may be extended by nonlimited types and
9888 -- permit inheritance in this case (addresses cases where an abstract
9889 -- extension doesn't get 'Input declared, as per comments below, but
9890 -- 'Class'Input must still be allowed). Note that attempts to apply
9891 -- stream attributes to a limited interface or its class-wide type
9892 -- (or limited extensions thereof) will still get properly rejected
9893 -- by Check_Stream_Attribute.
9895 -- We exclude the Input operation from being a predefined subprogram in
9896 -- the case where the associated type is an abstract extension, because
9897 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
9898 -- we don't want an abstract version created because types derived from
9899 -- the abstract type may not even have Input available (for example if
9900 -- derived from a private view of the abstract type that doesn't have
9901 -- a visible Input), but a VM such as .NET or the Java VM can treat the
9902 -- operation as inherited anyway, and we don't want an abstract function
9903 -- to be (implicitly) inherited in that case because it can lead to a VM
9906 -- Do not generate stream routines for type Finalization_Master because
9907 -- a master may never appear in types and therefore cannot be read or
9911 (not Is_Limited_Type (Typ)
9912 or else Is_Interface (Typ)
9913 or else Has_Predefined_Or_Specified_Stream_Attribute)
9915 (Operation /= TSS_Stream_Input
9916 or else not Is_Abstract_Type (Typ)
9917 or else not Is_Derived_Type (Typ))
9918 and then not Has_Unknown_Discriminants (Typ)
9922 (Is_Task_Interface (Typ)
9923 or else Is_Protected_Interface (Typ)
9924 or else Is_Synchronized_Interface (Typ)))
9925 and then not Restriction_Active (No_Streams)
9926 and then not Restriction_Active (No_Dispatch)
9927 and then not No_Run_Time_Mode
9928 and then RTE_Available (RE_Tag)
9929 and then No (Type_Without_Stream_Operation (Typ))
9930 and then RTE_Available (RE_Root_Stream_Type)
9931 and then not Is_RTE (Typ, RE_Finalization_Master);
9932 end Stream_Operation_OK;