1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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_Discriminant_Formals
93 Use_Dl : Boolean) return List_Id;
94 -- This function uses the discriminants of a type to build a list of
95 -- formal parameters, used in Build_Init_Procedure among other places.
96 -- If the flag Use_Dl is set, the list is built using the already
97 -- defined discriminals of the type, as is the case for concurrent
98 -- types with discriminants. Otherwise new identifiers are created,
99 -- with the source names of the discriminants.
101 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
102 -- This function builds a static aggregate that can serve as the initial
103 -- value for an array type whose bounds are static, and whose component
104 -- type is a composite type that has a static equivalent aggregate.
105 -- The equivalent array aggregate is used both for object initialization
106 -- and for component initialization, when used in the following function.
108 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
109 -- This function builds a static aggregate that can serve as the initial
110 -- value for a record type whose components are scalar and initialized
111 -- with compile-time values, or arrays with similar initialization or
112 -- defaults. When possible, initialization of an object of the type can
113 -- be achieved by using a copy of the aggregate as an initial value, thus
114 -- removing the implicit call that would otherwise constitute elaboration
117 function Build_Master_Renaming
119 T : Entity_Id) return Entity_Id;
120 -- If the designated type of an access type is a task type or contains
121 -- tasks, we make sure that a _Master variable is declared in the current
122 -- scope, and then declare a renaming for it:
124 -- atypeM : Master_Id renames _Master;
126 -- where atyp is the name of the access type. This declaration is used when
127 -- an allocator for the access type is expanded. The node is the full
128 -- declaration of the designated type that contains tasks. The renaming
129 -- declaration is inserted before N, and after the Master declaration.
131 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id);
132 -- Build record initialization procedure. N is the type declaration
133 -- node, and Rec_Ent is the corresponding entity for the record type.
135 procedure Build_Slice_Assignment (Typ : Entity_Id);
136 -- Build assignment procedure for one-dimensional arrays of controlled
137 -- types. Other array and slice assignments are expanded in-line, but
138 -- the code expansion for controlled components (when control actions
139 -- are active) can lead to very large blocks that GCC3 handles poorly.
141 procedure Build_Untagged_Equality (Typ : Entity_Id);
142 -- AI05-0123: Equality on untagged records composes. This procedure
143 -- builds the equality routine for an untagged record that has components
144 -- of a record type that has user-defined primitive equality operations.
145 -- The resulting operation is a TSS subprogram.
147 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
148 -- Create An Equality function for the non-tagged variant record 'Typ'
149 -- and attach it to the TSS list
151 procedure Check_Stream_Attributes (Typ : Entity_Id);
152 -- Check that if a limited extension has a parent with user-defined stream
153 -- attributes, and does not itself have user-defined stream-attributes,
154 -- then any limited component of the extension also has the corresponding
155 -- user-defined stream attributes.
157 procedure Clean_Task_Names
159 Proc_Id : Entity_Id);
160 -- If an initialization procedure includes calls to generate names
161 -- for task subcomponents, indicate that secondary stack cleanup is
162 -- needed after an initialization. Typ is the component type, and Proc_Id
163 -- the initialization procedure for the enclosing composite type.
165 procedure Expand_Tagged_Root (T : Entity_Id);
166 -- Add a field _Tag at the beginning of the record. This field carries
167 -- the value of the access to the Dispatch table. This procedure is only
168 -- called on root type, the _Tag field being inherited by the descendants.
170 procedure Expand_Freeze_Array_Type (N : Node_Id);
171 -- Freeze an array type. Deals with building the initialization procedure,
172 -- creating the packed array type for a packed array and also with the
173 -- creation of the controlling procedures for the controlled case. The
174 -- argument N is the N_Freeze_Entity node for the type.
176 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id);
177 -- Freeze a class-wide type. Build routine Finalize_Address for the purpose
178 -- of finalizing controlled derivations from the class-wide's root type.
180 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
181 -- Freeze enumeration type with non-standard representation. Builds the
182 -- array and function needed to convert between enumeration pos and
183 -- enumeration representation values. N is the N_Freeze_Entity node
186 procedure Expand_Freeze_Record_Type (N : Node_Id);
187 -- Freeze record type. Builds all necessary discriminant checking
188 -- and other ancillary functions, and builds dispatch tables where
189 -- needed. The argument N is the N_Freeze_Entity node. This processing
190 -- applies only to E_Record_Type entities, not to class wide types,
191 -- record subtypes, or private types.
193 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
194 -- Treat user-defined stream operations as renaming_as_body if the
195 -- subprogram they rename is not frozen when the type is frozen.
197 procedure Initialization_Warning (E : Entity_Id);
198 -- If static elaboration of the package is requested, indicate
199 -- when a type does meet the conditions for static initialization. If
200 -- E is a type, it has components that have no static initialization.
201 -- if E is an entity, its initial expression is not compile-time known.
203 function Init_Formals (Typ : Entity_Id) return List_Id;
204 -- This function builds the list of formals for an initialization routine.
205 -- The first formal is always _Init with the given type. For task value
206 -- record types and types containing tasks, three additional formals are
209 -- _Master : Master_Id
210 -- _Chain : in out Activation_Chain
211 -- _Task_Name : String
213 -- The caller must append additional entries for discriminants if required.
215 function In_Runtime (E : Entity_Id) return Boolean;
216 -- Check if E is defined in the RTL (in a child of Ada or System). Used
217 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
219 function Is_Variable_Size_Array (E : Entity_Id) return Boolean;
220 -- Returns true if E has variable size components
222 function Is_Variable_Size_Record (E : Entity_Id) return Boolean;
223 -- Returns true if E has variable size components
225 function Make_Eq_Body
227 Eq_Name : Name_Id) return Node_Id;
228 -- Build the body of a primitive equality operation for a tagged record
229 -- type, or in Ada 2012 for any record type that has components with a
230 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
232 function Make_Eq_Case
235 Discr : Entity_Id := Empty) return List_Id;
236 -- Building block for variant record equality. Defined to share the code
237 -- between the tagged and non-tagged case. Given a Component_List node CL,
238 -- it generates an 'if' followed by a 'case' statement that compares all
239 -- components of local temporaries named X and Y (that are declared as
240 -- formals at some upper level). E provides the Sloc to be used for the
241 -- generated code. Discr is used as the case statement switch in the case
242 -- of Unchecked_Union equality.
246 L : List_Id) return Node_Id;
247 -- Building block for variant record equality. Defined to share the code
248 -- between the tagged and non-tagged case. Given the list of components
249 -- (or discriminants) L, it generates a return statement that compares all
250 -- components of local temporaries named X and Y (that are declared as
251 -- formals at some upper level). E provides the Sloc to be used for the
254 procedure Make_Predefined_Primitive_Specs
255 (Tag_Typ : Entity_Id;
256 Predef_List : out List_Id;
257 Renamed_Eq : out Entity_Id);
258 -- Create a list with the specs of the predefined primitive operations.
259 -- For tagged types that are interfaces all these primitives are defined
262 -- The following entries are present for all tagged types, and provide
263 -- the results of the corresponding attribute applied to the object.
264 -- Dispatching is required in general, since the result of the attribute
265 -- will vary with the actual object subtype.
267 -- _alignment provides result of 'Alignment attribute
268 -- _size provides result of 'Size attribute
269 -- typSR provides result of 'Read attribute
270 -- typSW provides result of 'Write attribute
271 -- typSI provides result of 'Input attribute
272 -- typSO provides result of 'Output attribute
274 -- The following entries are additionally present for non-limited tagged
275 -- types, and implement additional dispatching operations for predefined
278 -- _equality implements "=" operator
279 -- _assign implements assignment operation
280 -- typDF implements deep finalization
281 -- typDA implements deep adjust
283 -- The latter two are empty procedures unless the type contains some
284 -- controlled components that require finalization actions (the deep
285 -- in the name refers to the fact that the action applies to components).
287 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
288 -- returns the value Empty, or else the defining unit name for the
289 -- predefined equality function in the case where the type has a primitive
290 -- operation that is a renaming of predefined equality (but only if there
291 -- is also an overriding user-defined equality function). The returned
292 -- Renamed_Eq will be passed to the corresponding parameter of
293 -- Predefined_Primitive_Bodies.
295 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
296 -- returns True if there are representation clauses for type T that are not
297 -- inherited. If the result is false, the init_proc and the discriminant
298 -- checking functions of the parent can be reused by a derived type.
300 procedure Make_Controlling_Function_Wrappers
301 (Tag_Typ : Entity_Id;
302 Decl_List : out List_Id;
303 Body_List : out List_Id);
304 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
305 -- associated with inherited functions with controlling results which
306 -- are not overridden. The body of each wrapper function consists solely
307 -- of a return statement whose expression is an extension aggregate
308 -- invoking the inherited subprogram's parent subprogram and extended
309 -- with a null association list.
311 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
312 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
313 -- null procedures inherited from an interface type that have not been
314 -- overridden. Only one null procedure will be created for a given set of
315 -- inherited null procedures with homographic profiles.
317 function Predef_Spec_Or_Body
322 Ret_Type : Entity_Id := Empty;
323 For_Body : Boolean := False) return Node_Id;
324 -- This function generates the appropriate expansion for a predefined
325 -- primitive operation specified by its name, parameter profile and
326 -- return type (Empty means this is a procedure). If For_Body is false,
327 -- then the returned node is a subprogram declaration. If For_Body is
328 -- true, then the returned node is a empty subprogram body containing
329 -- no declarations and no statements.
331 function Predef_Stream_Attr_Spec
334 Name : TSS_Name_Type;
335 For_Body : Boolean := False) return Node_Id;
336 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
337 -- input and output attribute whose specs are constructed in Exp_Strm.
339 function Predef_Deep_Spec
342 Name : TSS_Name_Type;
343 For_Body : Boolean := False) return Node_Id;
344 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
345 -- and _deep_finalize
347 function Predefined_Primitive_Bodies
348 (Tag_Typ : Entity_Id;
349 Renamed_Eq : Entity_Id) return List_Id;
350 -- Create the bodies of the predefined primitives that are described in
351 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
352 -- the defining unit name of the type's predefined equality as returned
353 -- by Make_Predefined_Primitive_Specs.
355 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
356 -- Freeze entities of all predefined primitive operations. This is needed
357 -- because the bodies of these operations do not normally do any freezing.
359 function Stream_Operation_OK
361 Operation : TSS_Name_Type) return Boolean;
362 -- Check whether the named stream operation must be emitted for a given
363 -- type. The rules for inheritance of stream attributes by type extensions
364 -- are enforced by this function. Furthermore, various restrictions prevent
365 -- the generation of these operations, as a useful optimization or for
366 -- certification purposes.
368 --------------------------
369 -- Adjust_Discriminants --
370 --------------------------
372 -- This procedure attempts to define subtypes for discriminants that are
373 -- more restrictive than those declared. Such a replacement is possible if
374 -- we can demonstrate that values outside the restricted range would cause
375 -- constraint errors in any case. The advantage of restricting the
376 -- discriminant types in this way is that the maximum size of the variant
377 -- record can be calculated more conservatively.
379 -- An example of a situation in which we can perform this type of
380 -- restriction is the following:
382 -- subtype B is range 1 .. 10;
383 -- type Q is array (B range <>) of Integer;
385 -- type V (N : Natural) is record
389 -- In this situation, we can restrict the upper bound of N to 10, since
390 -- any larger value would cause a constraint error in any case.
392 -- There are many situations in which such restriction is possible, but
393 -- for now, we just look for cases like the above, where the component
394 -- in question is a one dimensional array whose upper bound is one of
395 -- the record discriminants. Also the component must not be part of
396 -- any variant part, since then the component does not always exist.
398 procedure Adjust_Discriminants (Rtype : Entity_Id) is
399 Loc : constant Source_Ptr := Sloc (Rtype);
416 Comp := First_Component (Rtype);
417 while Present (Comp) loop
419 -- If our parent is a variant, quit, we do not look at components
420 -- that are in variant parts, because they may not always exist.
422 P := Parent (Comp); -- component declaration
423 P := Parent (P); -- component list
425 exit when Nkind (Parent (P)) = N_Variant;
427 -- We are looking for a one dimensional array type
429 Ctyp := Etype (Comp);
431 if not Is_Array_Type (Ctyp)
432 or else Number_Dimensions (Ctyp) > 1
437 -- The lower bound must be constant, and the upper bound is a
438 -- discriminant (which is a discriminant of the current record).
440 Ityp := Etype (First_Index (Ctyp));
441 Lo := Type_Low_Bound (Ityp);
442 Hi := Type_High_Bound (Ityp);
444 if not Compile_Time_Known_Value (Lo)
445 or else Nkind (Hi) /= N_Identifier
446 or else No (Entity (Hi))
447 or else Ekind (Entity (Hi)) /= E_Discriminant
452 -- We have an array with appropriate bounds
454 Loval := Expr_Value (Lo);
455 Discr := Entity (Hi);
456 Dtyp := Etype (Discr);
458 -- See if the discriminant has a known upper bound
460 Dhi := Type_High_Bound (Dtyp);
462 if not Compile_Time_Known_Value (Dhi) then
466 Dhiv := Expr_Value (Dhi);
468 -- See if base type of component array has known upper bound
470 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
472 if not Compile_Time_Known_Value (Ahi) then
476 Ahiv := Expr_Value (Ahi);
478 -- The condition for doing the restriction is that the high bound
479 -- of the discriminant is greater than the low bound of the array,
480 -- and is also greater than the high bound of the base type index.
482 if Dhiv > Loval and then Dhiv > Ahiv then
484 -- We can reset the upper bound of the discriminant type to
485 -- whichever is larger, the low bound of the component, or
486 -- the high bound of the base type array index.
488 -- We build a subtype that is declared as
490 -- subtype Tnn is discr_type range discr_type'First .. max;
492 -- And insert this declaration into the tree. The type of the
493 -- discriminant is then reset to this more restricted subtype.
495 Tnn := Make_Temporary (Loc, 'T');
497 Insert_Action (Declaration_Node (Rtype),
498 Make_Subtype_Declaration (Loc,
499 Defining_Identifier => Tnn,
500 Subtype_Indication =>
501 Make_Subtype_Indication (Loc,
502 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
504 Make_Range_Constraint (Loc,
508 Make_Attribute_Reference (Loc,
509 Attribute_Name => Name_First,
510 Prefix => New_Occurrence_Of (Dtyp, Loc)),
512 Make_Integer_Literal (Loc,
513 Intval => UI_Max (Loval, Ahiv)))))));
515 Set_Etype (Discr, Tnn);
519 Next_Component (Comp);
521 end Adjust_Discriminants;
523 ---------------------------
524 -- Build_Array_Init_Proc --
525 ---------------------------
527 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
528 Loc : constant Source_Ptr := Sloc (Nod);
529 Comp_Type : constant Entity_Id := Component_Type (A_Type);
530 Body_Stmts : List_Id;
531 Has_Default_Init : Boolean;
532 Index_List : List_Id;
535 function Init_Component return List_Id;
536 -- Create one statement to initialize one array component, designated
537 -- by a full set of indexes.
539 function Init_One_Dimension (N : Int) return List_Id;
540 -- Create loop to initialize one dimension of the array. The single
541 -- statement in the loop body initializes the inner dimensions if any,
542 -- or else the single component. Note that this procedure is called
543 -- recursively, with N being the dimension to be initialized. A call
544 -- with N greater than the number of dimensions simply generates the
545 -- component initialization, terminating the recursion.
551 function Init_Component return List_Id is
556 Make_Indexed_Component (Loc,
557 Prefix => Make_Identifier (Loc, Name_uInit),
558 Expressions => Index_List);
560 if Has_Default_Aspect (A_Type) then
561 Set_Assignment_OK (Comp);
563 Make_Assignment_Statement (Loc,
566 Convert_To (Comp_Type,
568 (Get_Rep_Item_For_Entity
569 (First_Subtype (A_Type),
570 Name_Default_Component_Value)))));
572 elsif Needs_Simple_Initialization (Comp_Type) then
573 Set_Assignment_OK (Comp);
575 Make_Assignment_Statement (Loc,
579 (Comp_Type, Nod, Component_Size (A_Type))));
582 Clean_Task_Names (Comp_Type, Proc_Id);
584 Build_Initialization_Call
585 (Loc, Comp, Comp_Type,
586 In_Init_Proc => True,
587 Enclos_Type => A_Type);
591 ------------------------
592 -- Init_One_Dimension --
593 ------------------------
595 function Init_One_Dimension (N : Int) return List_Id is
599 -- If the component does not need initializing, then there is nothing
600 -- to do here, so we return a null body. This occurs when generating
601 -- the dummy Init_Proc needed for Initialize_Scalars processing.
603 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
604 and then not Needs_Simple_Initialization (Comp_Type)
605 and then not Has_Task (Comp_Type)
606 and then not Has_Default_Aspect (A_Type)
608 return New_List (Make_Null_Statement (Loc));
610 -- If all dimensions dealt with, we simply initialize the component
612 elsif N > Number_Dimensions (A_Type) then
613 return Init_Component;
615 -- Here we generate the required loop
619 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
621 Append (New_Reference_To (Index, Loc), Index_List);
624 Make_Implicit_Loop_Statement (Nod,
627 Make_Iteration_Scheme (Loc,
628 Loop_Parameter_Specification =>
629 Make_Loop_Parameter_Specification (Loc,
630 Defining_Identifier => Index,
631 Discrete_Subtype_Definition =>
632 Make_Attribute_Reference (Loc,
633 Prefix => Make_Identifier (Loc, Name_uInit),
634 Attribute_Name => Name_Range,
635 Expressions => New_List (
636 Make_Integer_Literal (Loc, N))))),
637 Statements => Init_One_Dimension (N + 1)));
639 end Init_One_Dimension;
641 -- Start of processing for Build_Array_Init_Proc
644 -- Nothing to generate in the following cases:
646 -- 1. Initialization is suppressed for the type
647 -- 2. The type is a value type, in the CIL sense.
648 -- 3. The type has CIL/JVM convention.
649 -- 4. An initialization already exists for the base type
651 if Initialization_Suppressed (A_Type)
652 or else Is_Value_Type (Comp_Type)
653 or else Convention (A_Type) = Convention_CIL
654 or else Convention (A_Type) = Convention_Java
655 or else Present (Base_Init_Proc (A_Type))
660 Index_List := New_List;
662 -- We need an initialization procedure if any of the following is true:
664 -- 1. The component type has an initialization procedure
665 -- 2. The component type needs simple initialization
666 -- 3. Tasks are present
667 -- 4. The type is marked as a public entity
668 -- 5. The array type has a Default_Component_Value aspect
670 -- The reason for the public entity test is to deal properly with the
671 -- Initialize_Scalars pragma. This pragma can be set in the client and
672 -- not in the declaring package, this means the client will make a call
673 -- to the initialization procedure (because one of conditions 1-3 must
674 -- apply in this case), and we must generate a procedure (even if it is
675 -- null) to satisfy the call in this case.
677 -- Exception: do not build an array init_proc for a type whose root
678 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
679 -- is no place to put the code, and in any case we handle initialization
680 -- of such types (in the Initialize_Scalars case, that's the only time
681 -- the issue arises) in a special manner anyway which does not need an
684 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
685 or else Needs_Simple_Initialization (Comp_Type)
686 or else Has_Task (Comp_Type)
687 or else Has_Default_Aspect (A_Type);
690 or else (not Restriction_Active (No_Initialize_Scalars)
691 and then Is_Public (A_Type)
692 and then Root_Type (A_Type) /= Standard_String
693 and then Root_Type (A_Type) /= Standard_Wide_String
694 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
697 Make_Defining_Identifier (Loc,
698 Chars => Make_Init_Proc_Name (A_Type));
700 -- If No_Default_Initialization restriction is active, then we don't
701 -- want to build an init_proc, but we need to mark that an init_proc
702 -- would be needed if this restriction was not active (so that we can
703 -- detect attempts to call it), so set a dummy init_proc in place.
704 -- This is only done though when actual default initialization is
705 -- needed (and not done when only Is_Public is True), since otherwise
706 -- objects such as arrays of scalars could be wrongly flagged as
707 -- violating the restriction.
709 if Restriction_Active (No_Default_Initialization) then
710 if Has_Default_Init then
711 Set_Init_Proc (A_Type, Proc_Id);
717 Body_Stmts := Init_One_Dimension (1);
720 Make_Subprogram_Body (Loc,
722 Make_Procedure_Specification (Loc,
723 Defining_Unit_Name => Proc_Id,
724 Parameter_Specifications => Init_Formals (A_Type)),
725 Declarations => New_List,
726 Handled_Statement_Sequence =>
727 Make_Handled_Sequence_Of_Statements (Loc,
728 Statements => Body_Stmts)));
730 Set_Ekind (Proc_Id, E_Procedure);
731 Set_Is_Public (Proc_Id, Is_Public (A_Type));
732 Set_Is_Internal (Proc_Id);
733 Set_Has_Completion (Proc_Id);
735 if not Debug_Generated_Code then
736 Set_Debug_Info_Off (Proc_Id);
739 -- Set inlined unless controlled stuff or tasks around, in which
740 -- case we do not want to inline, because nested stuff may cause
741 -- difficulties in inter-unit inlining, and furthermore there is
742 -- in any case no point in inlining such complex init procs.
744 if not Has_Task (Proc_Id)
745 and then not Needs_Finalization (Proc_Id)
747 Set_Is_Inlined (Proc_Id);
750 -- Associate Init_Proc with type, and determine if the procedure
751 -- is null (happens because of the Initialize_Scalars pragma case,
752 -- where we have to generate a null procedure in case it is called
753 -- by a client with Initialize_Scalars set). Such procedures have
754 -- to be generated, but do not have to be called, so we mark them
755 -- as null to suppress the call.
757 Set_Init_Proc (A_Type, Proc_Id);
759 if List_Length (Body_Stmts) = 1
761 -- We must skip SCIL nodes because they may have been added to this
762 -- list by Insert_Actions.
764 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
766 Set_Is_Null_Init_Proc (Proc_Id);
769 -- Try to build a static aggregate to statically initialize
770 -- objects of the type. This can only be done for constrained
771 -- one-dimensional arrays with static bounds.
773 Set_Static_Initialization
775 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
778 end Build_Array_Init_Proc;
780 -----------------------------
781 -- Build_Class_Wide_Master --
782 -----------------------------
784 procedure Build_Class_Wide_Master (T : Entity_Id) is
785 Loc : constant Source_Ptr := Sloc (T);
786 Master_Id : Entity_Id;
787 Master_Scope : Entity_Id;
789 Related_Node : Node_Id;
793 -- Nothing to do if there is no task hierarchy
795 if Restriction_Active (No_Task_Hierarchy) then
799 -- Find the declaration that created the access type. It is either a
800 -- type declaration, or an object declaration with an access definition,
801 -- in which case the type is anonymous.
804 Related_Node := Associated_Node_For_Itype (T);
806 Related_Node := Parent (T);
809 Master_Scope := Find_Master_Scope (T);
811 -- Nothing to do if the master scope already contains a _master entity.
812 -- The only exception to this is the following scenario:
821 -- In this case the source scope is marked as having the master entity
822 -- even though the actual declaration appears inside an inner scope. If
823 -- the second transient scope requires a _master, it cannot use the one
824 -- already declared because the entity is not visible.
826 Name_Id := Make_Identifier (Loc, Name_uMaster);
828 if not Has_Master_Entity (Master_Scope)
829 or else No (Current_Entity_In_Scope (Name_Id))
832 Master_Decl : Node_Id;
835 Set_Has_Master_Entity (Master_Scope);
838 -- _master : constant Integer := Current_Master.all;
841 Make_Object_Declaration (Loc,
842 Defining_Identifier =>
843 Make_Defining_Identifier (Loc, Name_uMaster),
844 Constant_Present => True,
846 New_Reference_To (Standard_Integer, Loc),
848 Make_Explicit_Dereference (Loc,
849 New_Reference_To (RTE (RE_Current_Master), Loc)));
851 Insert_Action (Related_Node, Master_Decl);
852 Analyze (Master_Decl);
854 -- Mark the containing scope as a task master. Masters associated
855 -- with return statements are already marked at this stage (see
856 -- Analyze_Subprogram_Body).
858 if Ekind (Current_Scope) /= E_Return_Statement then
860 Par : Node_Id := Related_Node;
863 while Nkind (Par) /= N_Compilation_Unit loop
866 -- If we fall off the top, we are at the outer level, and
867 -- the environment task is our effective master, so
870 if Nkind_In (Par, N_Block_Statement,
874 Set_Is_Task_Master (Par);
884 Make_Defining_Identifier (Loc,
885 New_External_Name (Chars (T), 'M'));
888 -- Mnn renames _master;
891 Make_Object_Renaming_Declaration (Loc,
892 Defining_Identifier => Master_Id,
893 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
896 Insert_Before (Related_Node, Ren_Decl);
899 Set_Master_Id (T, Master_Id);
902 when RE_Not_Available =>
904 end Build_Class_Wide_Master;
906 --------------------------------
907 -- Build_Discr_Checking_Funcs --
908 --------------------------------
910 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
913 Enclosing_Func_Id : Entity_Id;
918 function Build_Case_Statement
919 (Case_Id : Entity_Id;
920 Variant : Node_Id) return Node_Id;
921 -- Build a case statement containing only two alternatives. The first
922 -- alternative corresponds exactly to the discrete choices given on the
923 -- variant with contains the components that we are generating the
924 -- checks for. If the discriminant is one of these return False. The
925 -- second alternative is an OTHERS choice that will return True
926 -- indicating the discriminant did not match.
928 function Build_Dcheck_Function
929 (Case_Id : Entity_Id;
930 Variant : Node_Id) return Entity_Id;
931 -- Build the discriminant checking function for a given variant
933 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
934 -- Builds the discriminant checking function for each variant of the
935 -- given variant part of the record type.
937 --------------------------
938 -- Build_Case_Statement --
939 --------------------------
941 function Build_Case_Statement
942 (Case_Id : Entity_Id;
943 Variant : Node_Id) return Node_Id
945 Alt_List : constant List_Id := New_List;
946 Actuals_List : List_Id;
948 Case_Alt_Node : Node_Id;
950 Choice_List : List_Id;
952 Return_Node : Node_Id;
955 Case_Node := New_Node (N_Case_Statement, Loc);
957 -- Replace the discriminant which controls the variant, with the name
958 -- of the formal of the checking function.
960 Set_Expression (Case_Node, Make_Identifier (Loc, Chars (Case_Id)));
962 Choice := First (Discrete_Choices (Variant));
964 if Nkind (Choice) = N_Others_Choice then
965 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
967 Choice_List := New_Copy_List (Discrete_Choices (Variant));
970 if not Is_Empty_List (Choice_List) then
971 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
972 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
974 -- In case this is a nested variant, we need to return the result
975 -- of the discriminant checking function for the immediately
976 -- enclosing variant.
978 if Present (Enclosing_Func_Id) then
979 Actuals_List := New_List;
981 D := First_Discriminant (Rec_Id);
982 while Present (D) loop
983 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
984 Next_Discriminant (D);
988 Make_Simple_Return_Statement (Loc,
990 Make_Function_Call (Loc,
992 New_Reference_To (Enclosing_Func_Id, Loc),
993 Parameter_Associations =>
998 Make_Simple_Return_Statement (Loc,
1000 New_Reference_To (Standard_False, Loc));
1003 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1004 Append (Case_Alt_Node, Alt_List);
1007 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
1008 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
1009 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
1012 Make_Simple_Return_Statement (Loc,
1014 New_Reference_To (Standard_True, Loc));
1016 Set_Statements (Case_Alt_Node, New_List (Return_Node));
1017 Append (Case_Alt_Node, Alt_List);
1019 Set_Alternatives (Case_Node, Alt_List);
1021 end Build_Case_Statement;
1023 ---------------------------
1024 -- Build_Dcheck_Function --
1025 ---------------------------
1027 function Build_Dcheck_Function
1028 (Case_Id : Entity_Id;
1029 Variant : Node_Id) return Entity_Id
1031 Body_Node : Node_Id;
1032 Func_Id : Entity_Id;
1033 Parameter_List : List_Id;
1034 Spec_Node : Node_Id;
1037 Body_Node := New_Node (N_Subprogram_Body, Loc);
1038 Sequence := Sequence + 1;
1041 Make_Defining_Identifier (Loc,
1042 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1044 Spec_Node := New_Node (N_Function_Specification, Loc);
1045 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1047 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1049 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1050 Set_Result_Definition (Spec_Node,
1051 New_Reference_To (Standard_Boolean, Loc));
1052 Set_Specification (Body_Node, Spec_Node);
1053 Set_Declarations (Body_Node, New_List);
1055 Set_Handled_Statement_Sequence (Body_Node,
1056 Make_Handled_Sequence_Of_Statements (Loc,
1057 Statements => New_List (
1058 Build_Case_Statement (Case_Id, Variant))));
1060 Set_Ekind (Func_Id, E_Function);
1061 Set_Mechanism (Func_Id, Default_Mechanism);
1062 Set_Is_Inlined (Func_Id, True);
1063 Set_Is_Pure (Func_Id, True);
1064 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1065 Set_Is_Internal (Func_Id, True);
1067 if not Debug_Generated_Code then
1068 Set_Debug_Info_Off (Func_Id);
1071 Analyze (Body_Node);
1073 Append_Freeze_Action (Rec_Id, Body_Node);
1074 Set_Dcheck_Function (Variant, Func_Id);
1076 end Build_Dcheck_Function;
1078 ----------------------------
1079 -- Build_Dcheck_Functions --
1080 ----------------------------
1082 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1083 Component_List_Node : Node_Id;
1085 Discr_Name : Entity_Id;
1086 Func_Id : Entity_Id;
1088 Saved_Enclosing_Func_Id : Entity_Id;
1091 -- Build the discriminant-checking function for each variant, and
1092 -- label all components of that variant with the function's name.
1093 -- We only Generate a discriminant-checking function when the
1094 -- variant is not empty, to prevent the creation of dead code.
1095 -- The exception to that is when Frontend_Layout_On_Target is set,
1096 -- because the variant record size function generated in package
1097 -- Layout needs to generate calls to all discriminant-checking
1098 -- functions, including those for empty variants.
1100 Discr_Name := Entity (Name (Variant_Part_Node));
1101 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1103 while Present (Variant) loop
1104 Component_List_Node := Component_List (Variant);
1106 if not Null_Present (Component_List_Node)
1107 or else Frontend_Layout_On_Target
1109 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1111 First_Non_Pragma (Component_Items (Component_List_Node));
1113 while Present (Decl) loop
1114 Set_Discriminant_Checking_Func
1115 (Defining_Identifier (Decl), Func_Id);
1117 Next_Non_Pragma (Decl);
1120 if Present (Variant_Part (Component_List_Node)) then
1121 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1122 Enclosing_Func_Id := Func_Id;
1123 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1124 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1128 Next_Non_Pragma (Variant);
1130 end Build_Dcheck_Functions;
1132 -- Start of processing for Build_Discr_Checking_Funcs
1135 -- Only build if not done already
1137 if not Discr_Check_Funcs_Built (N) then
1138 Type_Def := Type_Definition (N);
1140 if Nkind (Type_Def) = N_Record_Definition then
1141 if No (Component_List (Type_Def)) then -- null record.
1144 V := Variant_Part (Component_List (Type_Def));
1147 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1148 if No (Component_List (Record_Extension_Part (Type_Def))) then
1152 (Component_List (Record_Extension_Part (Type_Def)));
1156 Rec_Id := Defining_Identifier (N);
1158 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1160 Enclosing_Func_Id := Empty;
1161 Build_Dcheck_Functions (V);
1164 Set_Discr_Check_Funcs_Built (N);
1166 end Build_Discr_Checking_Funcs;
1168 --------------------------------
1169 -- Build_Discriminant_Formals --
1170 --------------------------------
1172 function Build_Discriminant_Formals
1173 (Rec_Id : Entity_Id;
1174 Use_Dl : Boolean) return List_Id
1176 Loc : Source_Ptr := Sloc (Rec_Id);
1177 Parameter_List : constant List_Id := New_List;
1180 Formal_Type : Entity_Id;
1181 Param_Spec_Node : Node_Id;
1184 if Has_Discriminants (Rec_Id) then
1185 D := First_Discriminant (Rec_Id);
1186 while Present (D) loop
1190 Formal := Discriminal (D);
1191 Formal_Type := Etype (Formal);
1193 Formal := Make_Defining_Identifier (Loc, Chars (D));
1194 Formal_Type := Etype (D);
1198 Make_Parameter_Specification (Loc,
1199 Defining_Identifier => Formal,
1201 New_Reference_To (Formal_Type, Loc));
1202 Append (Param_Spec_Node, Parameter_List);
1203 Next_Discriminant (D);
1207 return Parameter_List;
1208 end Build_Discriminant_Formals;
1210 --------------------------------------
1211 -- Build_Equivalent_Array_Aggregate --
1212 --------------------------------------
1214 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1215 Loc : constant Source_Ptr := Sloc (T);
1216 Comp_Type : constant Entity_Id := Component_Type (T);
1217 Index_Type : constant Entity_Id := Etype (First_Index (T));
1218 Proc : constant Entity_Id := Base_Init_Proc (T);
1224 if not Is_Constrained (T)
1225 or else Number_Dimensions (T) > 1
1228 Initialization_Warning (T);
1232 Lo := Type_Low_Bound (Index_Type);
1233 Hi := Type_High_Bound (Index_Type);
1235 if not Compile_Time_Known_Value (Lo)
1236 or else not Compile_Time_Known_Value (Hi)
1238 Initialization_Warning (T);
1242 if Is_Record_Type (Comp_Type)
1243 and then Present (Base_Init_Proc (Comp_Type))
1245 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1248 Initialization_Warning (T);
1253 Initialization_Warning (T);
1257 Aggr := Make_Aggregate (Loc, No_List, New_List);
1258 Set_Etype (Aggr, T);
1259 Set_Aggregate_Bounds (Aggr,
1261 Low_Bound => New_Copy (Lo),
1262 High_Bound => New_Copy (Hi)));
1263 Set_Parent (Aggr, Parent (Proc));
1265 Append_To (Component_Associations (Aggr),
1266 Make_Component_Association (Loc,
1270 Low_Bound => New_Copy (Lo),
1271 High_Bound => New_Copy (Hi))),
1272 Expression => Expr));
1274 if Static_Array_Aggregate (Aggr) then
1277 Initialization_Warning (T);
1280 end Build_Equivalent_Array_Aggregate;
1282 ---------------------------------------
1283 -- Build_Equivalent_Record_Aggregate --
1284 ---------------------------------------
1286 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1289 Comp_Type : Entity_Id;
1291 -- Start of processing for Build_Equivalent_Record_Aggregate
1294 if not Is_Record_Type (T)
1295 or else Has_Discriminants (T)
1296 or else Is_Limited_Type (T)
1297 or else Has_Non_Standard_Rep (T)
1299 Initialization_Warning (T);
1303 Comp := First_Component (T);
1305 -- A null record needs no warning
1311 while Present (Comp) loop
1313 -- Array components are acceptable if initialized by a positional
1314 -- aggregate with static components.
1316 if Is_Array_Type (Etype (Comp)) then
1317 Comp_Type := Component_Type (Etype (Comp));
1319 if Nkind (Parent (Comp)) /= N_Component_Declaration
1320 or else No (Expression (Parent (Comp)))
1321 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1323 Initialization_Warning (T);
1326 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1328 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1330 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1332 Initialization_Warning (T);
1336 not Static_Array_Aggregate (Expression (Parent (Comp)))
1338 Initialization_Warning (T);
1342 elsif Is_Scalar_Type (Etype (Comp)) then
1343 Comp_Type := Etype (Comp);
1345 if Nkind (Parent (Comp)) /= N_Component_Declaration
1346 or else No (Expression (Parent (Comp)))
1347 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1348 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1350 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1352 Initialization_Warning (T);
1356 -- For now, other types are excluded
1359 Initialization_Warning (T);
1363 Next_Component (Comp);
1366 -- All components have static initialization. Build positional aggregate
1367 -- from the given expressions or defaults.
1369 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1370 Set_Parent (Agg, Parent (T));
1372 Comp := First_Component (T);
1373 while Present (Comp) loop
1375 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1376 Next_Component (Comp);
1379 Analyze_And_Resolve (Agg, T);
1381 end Build_Equivalent_Record_Aggregate;
1383 -------------------------------
1384 -- Build_Initialization_Call --
1385 -------------------------------
1387 -- References to a discriminant inside the record type declaration can
1388 -- appear either in the subtype_indication to constrain a record or an
1389 -- array, or as part of a larger expression given for the initial value
1390 -- of a component. In both of these cases N appears in the record
1391 -- initialization procedure and needs to be replaced by the formal
1392 -- parameter of the initialization procedure which corresponds to that
1395 -- In the example below, references to discriminants D1 and D2 in proc_1
1396 -- are replaced by references to formals with the same name
1399 -- A similar replacement is done for calls to any record initialization
1400 -- procedure for any components that are themselves of a record type.
1402 -- type R (D1, D2 : Integer) is record
1403 -- X : Integer := F * D1;
1404 -- Y : Integer := F * D2;
1407 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1411 -- Out_2.X := F * D1;
1412 -- Out_2.Y := F * D2;
1415 function Build_Initialization_Call
1419 In_Init_Proc : Boolean := False;
1420 Enclos_Type : Entity_Id := Empty;
1421 Discr_Map : Elist_Id := New_Elmt_List;
1422 With_Default_Init : Boolean := False;
1423 Constructor_Ref : Node_Id := Empty) return List_Id
1425 Res : constant List_Id := New_List;
1431 First_Arg : Node_Id;
1432 Full_Init_Type : Entity_Id;
1433 Full_Type : Entity_Id := Typ;
1434 Init_Type : Entity_Id;
1438 pragma Assert (Constructor_Ref = Empty
1439 or else Is_CPP_Constructor_Call (Constructor_Ref));
1441 if No (Constructor_Ref) then
1442 Proc := Base_Init_Proc (Typ);
1444 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1447 pragma Assert (Present (Proc));
1448 Init_Type := Etype (First_Formal (Proc));
1449 Full_Init_Type := Underlying_Type (Init_Type);
1451 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1452 -- is active (in which case we make the call anyway, since in the
1453 -- actual compiled client it may be non null).
1454 -- Also nothing to do for value types.
1456 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1457 or else Is_Value_Type (Typ)
1459 (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1464 -- Go to full view if private type. In the case of successive
1465 -- private derivations, this can require more than one step.
1467 while Is_Private_Type (Full_Type)
1468 and then Present (Full_View (Full_Type))
1470 Full_Type := Full_View (Full_Type);
1473 -- If Typ is derived, the procedure is the initialization procedure for
1474 -- the root type. Wrap the argument in an conversion to make it type
1475 -- honest. Actually it isn't quite type honest, because there can be
1476 -- conflicts of views in the private type case. That is why we set
1477 -- Conversion_OK in the conversion node.
1479 if (Is_Record_Type (Typ)
1480 or else Is_Array_Type (Typ)
1481 or else Is_Private_Type (Typ))
1482 and then Init_Type /= Base_Type (Typ)
1484 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1485 Set_Etype (First_Arg, Init_Type);
1488 First_Arg := Id_Ref;
1491 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1493 -- In the tasks case, add _Master as the value of the _Master parameter
1494 -- and _Chain as the value of the _Chain parameter. At the outer level,
1495 -- these will be variables holding the corresponding values obtained
1496 -- from GNARL. At inner levels, they will be the parameters passed down
1497 -- through the outer routines.
1499 if Has_Task (Full_Type) then
1500 if Restriction_Active (No_Task_Hierarchy) then
1502 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1504 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1507 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1509 -- Ada 2005 (AI-287): In case of default initialized components
1510 -- with tasks, we generate a null string actual parameter.
1511 -- This is just a workaround that must be improved later???
1513 if With_Default_Init then
1515 Make_String_Literal (Loc,
1520 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1521 Decl := Last (Decls);
1524 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1525 Append_List (Decls, Res);
1533 -- Add discriminant values if discriminants are present
1535 if Has_Discriminants (Full_Init_Type) then
1536 Discr := First_Discriminant (Full_Init_Type);
1538 while Present (Discr) loop
1540 -- If this is a discriminated concurrent type, the init_proc
1541 -- for the corresponding record is being called. Use that type
1542 -- directly to find the discriminant value, to handle properly
1543 -- intervening renamed discriminants.
1546 T : Entity_Id := Full_Type;
1549 if Is_Protected_Type (T) then
1550 T := Corresponding_Record_Type (T);
1552 elsif Is_Private_Type (T)
1553 and then Present (Underlying_Full_View (T))
1554 and then Is_Protected_Type (Underlying_Full_View (T))
1556 T := Corresponding_Record_Type (Underlying_Full_View (T));
1560 Get_Discriminant_Value (
1563 Discriminant_Constraint (Full_Type));
1566 if In_Init_Proc then
1568 -- Replace any possible references to the discriminant in the
1569 -- call to the record initialization procedure with references
1570 -- to the appropriate formal parameter.
1572 if Nkind (Arg) = N_Identifier
1573 and then Ekind (Entity (Arg)) = E_Discriminant
1575 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1577 -- Case of access discriminants. We replace the reference
1578 -- to the type by a reference to the actual object
1580 elsif Nkind (Arg) = N_Attribute_Reference
1581 and then Is_Access_Type (Etype (Arg))
1582 and then Is_Entity_Name (Prefix (Arg))
1583 and then Is_Type (Entity (Prefix (Arg)))
1586 Make_Attribute_Reference (Loc,
1587 Prefix => New_Copy (Prefix (Id_Ref)),
1588 Attribute_Name => Name_Unrestricted_Access);
1590 -- Otherwise make a copy of the default expression. Note that
1591 -- we use the current Sloc for this, because we do not want the
1592 -- call to appear to be at the declaration point. Within the
1593 -- expression, replace discriminants with their discriminals.
1597 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1601 if Is_Constrained (Full_Type) then
1602 Arg := Duplicate_Subexpr_No_Checks (Arg);
1604 -- The constraints come from the discriminant default exps,
1605 -- they must be reevaluated, so we use New_Copy_Tree but we
1606 -- ensure the proper Sloc (for any embedded calls).
1608 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1612 -- Ada 2005 (AI-287): In case of default initialized components,
1613 -- if the component is constrained with a discriminant of the
1614 -- enclosing type, we need to generate the corresponding selected
1615 -- component node to access the discriminant value. In other cases
1616 -- this is not required, either because we are inside the init
1617 -- proc and we use the corresponding formal, or else because the
1618 -- component is constrained by an expression.
1620 if With_Default_Init
1621 and then Nkind (Id_Ref) = N_Selected_Component
1622 and then Nkind (Arg) = N_Identifier
1623 and then Ekind (Entity (Arg)) = E_Discriminant
1626 Make_Selected_Component (Loc,
1627 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1628 Selector_Name => Arg));
1630 Append_To (Args, Arg);
1633 Next_Discriminant (Discr);
1637 -- If this is a call to initialize the parent component of a derived
1638 -- tagged type, indicate that the tag should not be set in the parent.
1640 if Is_Tagged_Type (Full_Init_Type)
1641 and then not Is_CPP_Class (Full_Init_Type)
1642 and then Nkind (Id_Ref) = N_Selected_Component
1643 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1645 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1647 elsif Present (Constructor_Ref) then
1648 Append_List_To (Args,
1649 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1653 Make_Procedure_Call_Statement (Loc,
1654 Name => New_Occurrence_Of (Proc, Loc),
1655 Parameter_Associations => Args));
1657 if Needs_Finalization (Typ)
1658 and then Nkind (Id_Ref) = N_Selected_Component
1660 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1663 (Obj_Ref => New_Copy_Tree (First_Arg),
1671 when RE_Not_Available =>
1673 end Build_Initialization_Call;
1675 ---------------------------
1676 -- Build_Master_Renaming --
1677 ---------------------------
1679 function Build_Master_Renaming
1681 T : Entity_Id) return Entity_Id
1683 Loc : constant Source_Ptr := Sloc (N);
1688 -- Nothing to do if there is no task hierarchy
1690 if Restriction_Active (No_Task_Hierarchy) then
1695 Make_Defining_Identifier (Loc,
1696 New_External_Name (Chars (T), 'M'));
1699 Make_Object_Renaming_Declaration (Loc,
1700 Defining_Identifier => M_Id,
1701 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1702 Name => Make_Identifier (Loc, Name_uMaster));
1703 Insert_Before (N, Decl);
1708 when RE_Not_Available =>
1710 end Build_Master_Renaming;
1712 ---------------------------
1713 -- Build_Master_Renaming --
1714 ---------------------------
1716 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1720 -- Nothing to do if there is no task hierarchy
1722 if Restriction_Active (No_Task_Hierarchy) then
1726 M_Id := Build_Master_Renaming (N, T);
1727 Set_Master_Id (T, M_Id);
1730 when RE_Not_Available =>
1732 end Build_Master_Renaming;
1734 ----------------------------
1735 -- Build_Record_Init_Proc --
1736 ----------------------------
1738 procedure Build_Record_Init_Proc (N : Node_Id; Rec_Ent : Entity_Id) is
1739 Decls : constant List_Id := New_List;
1740 Discr_Map : constant Elist_Id := New_Elmt_List;
1742 Loc : Source_Ptr := Sloc (N);
1743 Proc_Id : Entity_Id;
1744 Rec_Type : Entity_Id;
1745 Set_Tag : Entity_Id := Empty;
1747 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1748 -- Build an assignment statement which assigns the default expression
1749 -- to its corresponding record component if defined. The left hand side
1750 -- of the assignment is marked Assignment_OK so that initialization of
1751 -- limited private records works correctly. This routine may also build
1752 -- an adjustment call if the component is controlled.
1754 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1755 -- If the record has discriminants, add assignment statements to
1756 -- Statement_List to initialize the discriminant values from the
1757 -- arguments of the initialization procedure.
1759 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1760 -- Build a list representing a sequence of statements which initialize
1761 -- components of the given component list. This may involve building
1762 -- case statements for the variant parts. Append any locally declared
1763 -- objects on list Decls.
1765 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1766 -- Given a non-tagged type-derivation that declares discriminants,
1769 -- type R (R1, R2 : Integer) is record ... end record;
1771 -- type D (D1 : Integer) is new R (1, D1);
1773 -- we make the _init_proc of D be
1775 -- procedure _init_proc (X : D; D1 : Integer) is
1777 -- _init_proc (R (X), 1, D1);
1780 -- This function builds the call statement in this _init_proc.
1782 procedure Build_CPP_Init_Procedure;
1783 -- Build the tree corresponding to the procedure specification and body
1784 -- of the IC procedure that initializes the C++ part of the dispatch
1785 -- table of an Ada tagged type that is a derivation of a CPP type.
1786 -- Install it as the CPP_Init TSS.
1788 procedure Build_Init_Procedure;
1789 -- Build the tree corresponding to the procedure specification and body
1790 -- of the initialization procedure and install it as the _init TSS.
1792 procedure Build_Offset_To_Top_Functions;
1793 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1794 -- and body of Offset_To_Top, a function used in conjuction with types
1795 -- having secondary dispatch tables.
1797 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1798 -- Add range checks to components of discriminated records. S is a
1799 -- subtype indication of a record component. Check_List is a list
1800 -- to which the check actions are appended.
1802 function Component_Needs_Simple_Initialization
1803 (T : Entity_Id) return Boolean;
1804 -- Determine if a component needs simple initialization, given its type
1805 -- T. This routine is the same as Needs_Simple_Initialization except for
1806 -- components of type Tag and Interface_Tag. These two access types do
1807 -- not require initialization since they are explicitly initialized by
1810 function Parent_Subtype_Renaming_Discrims return Boolean;
1811 -- Returns True for base types N that rename discriminants, else False
1813 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1814 -- Determine whether a record initialization procedure needs to be
1815 -- generated for the given record type.
1817 ----------------------
1818 -- Build_Assignment --
1819 ----------------------
1821 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1822 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1824 Kind : Node_Kind := Nkind (N);
1831 Make_Selected_Component (Loc,
1832 Prefix => Make_Identifier (Loc, Name_uInit),
1833 Selector_Name => New_Occurrence_Of (Id, Loc));
1834 Set_Assignment_OK (Lhs);
1836 -- Case of an access attribute applied to the current instance.
1837 -- Replace the reference to the type by a reference to the actual
1838 -- object. (Note that this handles the case of the top level of
1839 -- the expression being given by such an attribute, but does not
1840 -- cover uses nested within an initial value expression. Nested
1841 -- uses are unlikely to occur in practice, but are theoretically
1842 -- possible.) It is not clear how to handle them without fully
1843 -- traversing the expression. ???
1845 if Kind = N_Attribute_Reference
1846 and then (Attribute_Name (N) = Name_Unchecked_Access
1848 Attribute_Name (N) = Name_Unrestricted_Access)
1849 and then Is_Entity_Name (Prefix (N))
1850 and then Is_Type (Entity (Prefix (N)))
1851 and then Entity (Prefix (N)) = Rec_Type
1854 Make_Attribute_Reference (Loc,
1856 Make_Identifier (Loc, Name_uInit),
1857 Attribute_Name => Name_Unrestricted_Access);
1860 -- Take a copy of Exp to ensure that later copies of this component
1861 -- declaration in derived types see the original tree, not a node
1862 -- rewritten during expansion of the init_proc. If the copy contains
1863 -- itypes, the scope of the new itypes is the init_proc being built.
1865 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1868 Make_Assignment_Statement (Loc,
1870 Expression => Exp));
1872 Set_No_Ctrl_Actions (First (Res));
1874 -- Adjust the tag if tagged (because of possible view conversions).
1875 -- Suppress the tag adjustment when VM_Target because VM tags are
1876 -- represented implicitly in objects.
1878 if Is_Tagged_Type (Typ)
1879 and then Tagged_Type_Expansion
1882 Make_Assignment_Statement (Loc,
1884 Make_Selected_Component (Loc,
1886 New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1888 New_Reference_To (First_Tag_Component (Typ), Loc)),
1891 Unchecked_Convert_To (RTE (RE_Tag),
1895 (Access_Disp_Table (Underlying_Type (Typ)))),
1899 -- Adjust the component if controlled except if it is an aggregate
1900 -- that will be expanded inline.
1902 if Kind = N_Qualified_Expression then
1903 Kind := Nkind (Expression (N));
1906 if Needs_Finalization (Typ)
1907 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1908 and then not Is_Immutably_Limited_Type (Typ)
1912 (Obj_Ref => New_Copy_Tree (Lhs),
1913 Typ => Etype (Id)));
1919 when RE_Not_Available =>
1921 end Build_Assignment;
1923 ------------------------------------
1924 -- Build_Discriminant_Assignments --
1925 ------------------------------------
1927 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1928 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1932 if Has_Discriminants (Rec_Type)
1933 and then not Is_Unchecked_Union (Rec_Type)
1935 D := First_Discriminant (Rec_Type);
1936 while Present (D) loop
1938 -- Don't generate the assignment for discriminants in derived
1939 -- tagged types if the discriminant is a renaming of some
1940 -- ancestor discriminant. This initialization will be done
1941 -- when initializing the _parent field of the derived record.
1944 and then Present (Corresponding_Discriminant (D))
1950 Append_List_To (Statement_List,
1951 Build_Assignment (D,
1952 New_Reference_To (Discriminal (D), Loc)));
1955 Next_Discriminant (D);
1958 end Build_Discriminant_Assignments;
1960 --------------------------
1961 -- Build_Init_Call_Thru --
1962 --------------------------
1964 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1965 Parent_Proc : constant Entity_Id :=
1966 Base_Init_Proc (Etype (Rec_Type));
1968 Parent_Type : constant Entity_Id :=
1969 Etype (First_Formal (Parent_Proc));
1971 Uparent_Type : constant Entity_Id :=
1972 Underlying_Type (Parent_Type);
1974 First_Discr_Param : Node_Id;
1978 First_Arg : Node_Id;
1979 Parent_Discr : Entity_Id;
1983 -- First argument (_Init) is the object to be initialized.
1984 -- ??? not sure where to get a reasonable Loc for First_Arg
1987 OK_Convert_To (Parent_Type,
1988 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
1990 Set_Etype (First_Arg, Parent_Type);
1992 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
1994 -- In the tasks case,
1995 -- add _Master as the value of the _Master parameter
1996 -- add _Chain as the value of the _Chain parameter.
1997 -- add _Task_Name as the value of the _Task_Name parameter.
1998 -- At the outer level, these will be variables holding the
1999 -- corresponding values obtained from GNARL or the expander.
2001 -- At inner levels, they will be the parameters passed down through
2002 -- the outer routines.
2004 First_Discr_Param := Next (First (Parameters));
2006 if Has_Task (Rec_Type) then
2007 if Restriction_Active (No_Task_Hierarchy) then
2009 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
2011 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2014 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2015 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2016 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2019 -- Append discriminant values
2021 if Has_Discriminants (Uparent_Type) then
2022 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2024 Parent_Discr := First_Discriminant (Uparent_Type);
2025 while Present (Parent_Discr) loop
2027 -- Get the initial value for this discriminant
2028 -- ??? needs to be cleaned up to use parent_Discr_Constr
2032 Discr : Entity_Id :=
2033 First_Stored_Discriminant (Uparent_Type);
2035 Discr_Value : Elmt_Id :=
2036 First_Elmt (Stored_Constraint (Rec_Type));
2039 while Original_Record_Component (Parent_Discr) /= Discr loop
2040 Next_Stored_Discriminant (Discr);
2041 Next_Elmt (Discr_Value);
2044 Arg := Node (Discr_Value);
2047 -- Append it to the list
2049 if Nkind (Arg) = N_Identifier
2050 and then Ekind (Entity (Arg)) = E_Discriminant
2053 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2055 -- Case of access discriminants. We replace the reference
2056 -- to the type by a reference to the actual object.
2058 -- Is above comment right??? Use of New_Copy below seems mighty
2062 Append_To (Args, New_Copy (Arg));
2065 Next_Discriminant (Parent_Discr);
2071 Make_Procedure_Call_Statement (Loc,
2073 New_Occurrence_Of (Parent_Proc, Loc),
2074 Parameter_Associations => Args));
2077 end Build_Init_Call_Thru;
2079 -----------------------------------
2080 -- Build_Offset_To_Top_Functions --
2081 -----------------------------------
2083 procedure Build_Offset_To_Top_Functions is
2085 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2087 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2089 -- return O.Iface_Comp'Position;
2092 ----------------------------------
2093 -- Build_Offset_To_Top_Function --
2094 ----------------------------------
2096 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2097 Body_Node : Node_Id;
2098 Func_Id : Entity_Id;
2099 Spec_Node : Node_Id;
2102 Func_Id := Make_Temporary (Loc, 'F');
2103 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2106 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2108 Spec_Node := New_Node (N_Function_Specification, Loc);
2109 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2110 Set_Parameter_Specifications (Spec_Node, New_List (
2111 Make_Parameter_Specification (Loc,
2112 Defining_Identifier =>
2113 Make_Defining_Identifier (Loc, Name_uO),
2116 New_Reference_To (Rec_Type, Loc))));
2117 Set_Result_Definition (Spec_Node,
2118 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2121 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2123 -- return O.Iface_Comp'Position;
2126 Body_Node := New_Node (N_Subprogram_Body, Loc);
2127 Set_Specification (Body_Node, Spec_Node);
2128 Set_Declarations (Body_Node, New_List);
2129 Set_Handled_Statement_Sequence (Body_Node,
2130 Make_Handled_Sequence_Of_Statements (Loc,
2131 Statements => New_List (
2132 Make_Simple_Return_Statement (Loc,
2134 Make_Attribute_Reference (Loc,
2136 Make_Selected_Component (Loc,
2137 Prefix => Make_Identifier (Loc, Name_uO),
2139 New_Reference_To (Iface_Comp, Loc)),
2140 Attribute_Name => Name_Position)))));
2142 Set_Ekind (Func_Id, E_Function);
2143 Set_Mechanism (Func_Id, Default_Mechanism);
2144 Set_Is_Internal (Func_Id, True);
2146 if not Debug_Generated_Code then
2147 Set_Debug_Info_Off (Func_Id);
2150 Analyze (Body_Node);
2152 Append_Freeze_Action (Rec_Type, Body_Node);
2153 end Build_Offset_To_Top_Function;
2157 Iface_Comp : Node_Id;
2158 Iface_Comp_Elmt : Elmt_Id;
2159 Ifaces_Comp_List : Elist_Id;
2161 -- Start of processing for Build_Offset_To_Top_Functions
2164 -- Offset_To_Top_Functions are built only for derivations of types
2165 -- with discriminants that cover interface types.
2166 -- Nothing is needed either in case of virtual machines, since
2167 -- interfaces are handled directly by the VM.
2169 if not Is_Tagged_Type (Rec_Type)
2170 or else Etype (Rec_Type) = Rec_Type
2171 or else not Has_Discriminants (Etype (Rec_Type))
2172 or else not Tagged_Type_Expansion
2177 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2179 -- For each interface type with secondary dispatch table we generate
2180 -- the Offset_To_Top_Functions (required to displace the pointer in
2181 -- interface conversions)
2183 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2184 while Present (Iface_Comp_Elmt) loop
2185 Iface_Comp := Node (Iface_Comp_Elmt);
2186 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2188 -- If the interface is a parent of Rec_Type it shares the primary
2189 -- dispatch table and hence there is no need to build the function
2191 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type,
2192 Use_Full_View => True)
2194 Build_Offset_To_Top_Function (Iface_Comp);
2197 Next_Elmt (Iface_Comp_Elmt);
2199 end Build_Offset_To_Top_Functions;
2201 ------------------------------
2202 -- Build_CPP_Init_Procedure --
2203 ------------------------------
2205 procedure Build_CPP_Init_Procedure is
2206 Body_Node : Node_Id;
2207 Body_Stmts : List_Id;
2208 Flag_Id : Entity_Id;
2209 Flag_Decl : Node_Id;
2210 Handled_Stmt_Node : Node_Id;
2211 Init_Tags_List : List_Id;
2212 Proc_Id : Entity_Id;
2213 Proc_Spec_Node : Node_Id;
2216 -- Check cases requiring no IC routine
2218 if not Is_CPP_Class (Root_Type (Rec_Type))
2219 or else Is_CPP_Class (Rec_Type)
2220 or else CPP_Num_Prims (Rec_Type) = 0
2221 or else not Tagged_Type_Expansion
2222 or else No_Run_Time_Mode
2229 -- Flag : Boolean := False;
2231 -- procedure Typ_IC is
2234 -- Copy C++ dispatch table slots from parent
2235 -- Update C++ slots of overridden primitives
2239 Flag_Id := Make_Temporary (Loc, 'F');
2242 Make_Object_Declaration (Loc,
2243 Defining_Identifier => Flag_Id,
2244 Object_Definition =>
2245 New_Reference_To (Standard_Boolean, Loc),
2247 New_Reference_To (Standard_True, Loc));
2249 Analyze (Flag_Decl);
2250 Append_Freeze_Action (Rec_Type, Flag_Decl);
2252 Body_Stmts := New_List;
2253 Body_Node := New_Node (N_Subprogram_Body, Loc);
2255 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2258 Make_Defining_Identifier (Loc,
2259 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2261 Set_Ekind (Proc_Id, E_Procedure);
2262 Set_Is_Internal (Proc_Id);
2264 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2266 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2267 Set_Specification (Body_Node, Proc_Spec_Node);
2268 Set_Declarations (Body_Node, New_List);
2270 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2272 Append_To (Init_Tags_List,
2273 Make_Assignment_Statement (Loc,
2275 New_Reference_To (Flag_Id, Loc),
2277 New_Reference_To (Standard_False, Loc)));
2279 Append_To (Body_Stmts,
2280 Make_If_Statement (Loc,
2281 Condition => New_Occurrence_Of (Flag_Id, Loc),
2282 Then_Statements => Init_Tags_List));
2284 Handled_Stmt_Node :=
2285 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2286 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2287 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2288 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2290 if not Debug_Generated_Code then
2291 Set_Debug_Info_Off (Proc_Id);
2294 -- Associate CPP_Init_Proc with type
2296 Set_Init_Proc (Rec_Type, Proc_Id);
2297 end Build_CPP_Init_Procedure;
2299 --------------------------
2300 -- Build_Init_Procedure --
2301 --------------------------
2303 procedure Build_Init_Procedure is
2304 Body_Stmts : List_Id;
2305 Body_Node : Node_Id;
2306 Handled_Stmt_Node : Node_Id;
2307 Init_Tags_List : List_Id;
2308 Parameters : List_Id;
2309 Proc_Spec_Node : Node_Id;
2310 Record_Extension_Node : Node_Id;
2313 Body_Stmts := New_List;
2314 Body_Node := New_Node (N_Subprogram_Body, Loc);
2315 Set_Ekind (Proc_Id, E_Procedure);
2317 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2318 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2320 Parameters := Init_Formals (Rec_Type);
2321 Append_List_To (Parameters,
2322 Build_Discriminant_Formals (Rec_Type, True));
2324 -- For tagged types, we add a flag to indicate whether the routine
2325 -- is called to initialize a parent component in the init_proc of
2326 -- a type extension. If the flag is false, we do not set the tag
2327 -- because it has been set already in the extension.
2329 if Is_Tagged_Type (Rec_Type) then
2330 Set_Tag := Make_Temporary (Loc, 'P');
2332 Append_To (Parameters,
2333 Make_Parameter_Specification (Loc,
2334 Defining_Identifier => Set_Tag,
2336 New_Occurrence_Of (Standard_Boolean, Loc),
2338 New_Occurrence_Of (Standard_True, Loc)));
2341 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2342 Set_Specification (Body_Node, Proc_Spec_Node);
2343 Set_Declarations (Body_Node, Decls);
2345 -- N is a Derived_Type_Definition that renames the parameters of the
2346 -- ancestor type. We initialize it by expanding our discriminants and
2347 -- call the ancestor _init_proc with a type-converted object.
2349 if Parent_Subtype_Renaming_Discrims then
2350 Append_List_To (Body_Stmts, Build_Init_Call_Thru (Parameters));
2352 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2353 Build_Discriminant_Assignments (Body_Stmts);
2355 if not Null_Present (Type_Definition (N)) then
2356 Append_List_To (Body_Stmts,
2357 Build_Init_Statements (
2358 Component_List (Type_Definition (N))));
2361 -- N is a Derived_Type_Definition with a possible non-empty
2362 -- extension. The initialization of a type extension consists in the
2363 -- initialization of the components in the extension.
2366 Build_Discriminant_Assignments (Body_Stmts);
2368 Record_Extension_Node :=
2369 Record_Extension_Part (Type_Definition (N));
2371 if not Null_Present (Record_Extension_Node) then
2373 Stmts : constant List_Id :=
2374 Build_Init_Statements (
2375 Component_List (Record_Extension_Node));
2378 -- The parent field must be initialized first because
2379 -- the offset of the new discriminants may depend on it
2381 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2382 Append_List_To (Body_Stmts, Stmts);
2387 -- Add here the assignment to instantiate the Tag
2389 -- The assignment corresponds to the code:
2391 -- _Init._Tag := Typ'Tag;
2393 -- Suppress the tag assignment when VM_Target because VM tags are
2394 -- represented implicitly in objects. It is also suppressed in case
2395 -- of CPP_Class types because in this case the tag is initialized in
2398 if Is_Tagged_Type (Rec_Type)
2399 and then Tagged_Type_Expansion
2400 and then not No_Run_Time_Mode
2402 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2403 -- the actual object and invoke the IP of the parent (in this
2404 -- order). The tag must be initialized before the call to the IP
2405 -- of the parent and the assignments to other components because
2406 -- the initial value of the components may depend on the tag (eg.
2407 -- through a dispatching operation on an access to the current
2408 -- type). The tag assignment is not done when initializing the
2409 -- parent component of a type extension, because in that case the
2410 -- tag is set in the extension.
2412 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2414 -- Initialize the primary tag component
2416 Init_Tags_List := New_List (
2417 Make_Assignment_Statement (Loc,
2419 Make_Selected_Component (Loc,
2420 Prefix => Make_Identifier (Loc, Name_uInit),
2423 (First_Tag_Component (Rec_Type), Loc)),
2427 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2429 -- Ada 2005 (AI-251): Initialize the secondary tags components
2430 -- located at fixed positions (tags whose position depends on
2431 -- variable size components are initialized later ---see below)
2433 if Ada_Version >= Ada_2005
2434 and then not Is_Interface (Rec_Type)
2435 and then Has_Interfaces (Rec_Type)
2439 Target => Make_Identifier (Loc, Name_uInit),
2440 Stmts_List => Init_Tags_List,
2441 Fixed_Comps => True,
2442 Variable_Comps => False);
2445 Prepend_To (Body_Stmts,
2446 Make_If_Statement (Loc,
2447 Condition => New_Occurrence_Of (Set_Tag, Loc),
2448 Then_Statements => Init_Tags_List));
2450 -- Case 2: CPP type. The imported C++ constructor takes care of
2451 -- tags initialization. No action needed here because the IP
2452 -- is built by Set_CPP_Constructors; in this case the IP is a
2453 -- wrapper that invokes the C++ constructor and copies the C++
2454 -- tags locally. Done to inherit the C++ slots in Ada derivations
2457 elsif Is_CPP_Class (Rec_Type) then
2458 pragma Assert (False);
2461 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2462 -- type derivations. Derivations of imported C++ classes add a
2463 -- complication, because we cannot inhibit tag setting in the
2464 -- constructor for the parent. Hence we initialize the tag after
2465 -- the call to the parent IP (that is, in reverse order compared
2466 -- with pure Ada hierarchies ---see comment on case 1).
2469 -- Initialize the primary tag
2471 Init_Tags_List := New_List (
2472 Make_Assignment_Statement (Loc,
2474 Make_Selected_Component (Loc,
2475 Prefix => Make_Identifier (Loc, Name_uInit),
2478 (First_Tag_Component (Rec_Type), Loc)),
2482 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2484 -- Ada 2005 (AI-251): Initialize the secondary tags components
2485 -- located at fixed positions (tags whose position depends on
2486 -- variable size components are initialized later ---see below)
2488 if Ada_Version >= Ada_2005
2489 and then not Is_Interface (Rec_Type)
2490 and then Has_Interfaces (Rec_Type)
2494 Target => Make_Identifier (Loc, Name_uInit),
2495 Stmts_List => Init_Tags_List,
2496 Fixed_Comps => True,
2497 Variable_Comps => False);
2500 -- Initialize the tag component after invocation of parent IP.
2503 -- parent_IP(_init.parent); // Invokes the C++ constructor
2504 -- [ typIC; ] // Inherit C++ slots from parent
2511 -- Search for the call to the IP of the parent. We assume
2512 -- that the first init_proc call is for the parent.
2514 Ins_Nod := First (Body_Stmts);
2515 while Present (Next (Ins_Nod))
2516 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2517 or else not Is_Init_Proc (Name (Ins_Nod)))
2522 -- The IC routine copies the inherited slots of the C+ part
2523 -- of the dispatch table from the parent and updates the
2524 -- overridden C++ slots.
2526 if CPP_Num_Prims (Rec_Type) > 0 then
2528 Init_DT : Entity_Id;
2532 Init_DT := CPP_Init_Proc (Rec_Type);
2533 pragma Assert (Present (Init_DT));
2536 Make_Procedure_Call_Statement (Loc,
2537 New_Reference_To (Init_DT, Loc));
2538 Insert_After (Ins_Nod, New_Nod);
2540 -- Update location of init tag statements
2546 Insert_List_After (Ins_Nod, Init_Tags_List);
2550 -- Ada 2005 (AI-251): Initialize the secondary tag components
2551 -- located at variable positions. We delay the generation of this
2552 -- code until here because the value of the attribute 'Position
2553 -- applied to variable size components of the parent type that
2554 -- depend on discriminants is only safely read at runtime after
2555 -- the parent components have been initialized.
2557 if Ada_Version >= Ada_2005
2558 and then not Is_Interface (Rec_Type)
2559 and then Has_Interfaces (Rec_Type)
2560 and then Has_Discriminants (Etype (Rec_Type))
2561 and then Is_Variable_Size_Record (Etype (Rec_Type))
2563 Init_Tags_List := New_List;
2567 Target => Make_Identifier (Loc, Name_uInit),
2568 Stmts_List => Init_Tags_List,
2569 Fixed_Comps => False,
2570 Variable_Comps => True);
2572 if Is_Non_Empty_List (Init_Tags_List) then
2573 Append_List_To (Body_Stmts, Init_Tags_List);
2578 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2579 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2582 -- Local_DF_Id (_init, C1, ..., CN);
2586 and then Needs_Finalization (Rec_Type)
2587 and then not Is_Abstract_Type (Rec_Type)
2588 and then not Restriction_Active (No_Exception_Propagation)
2591 Local_DF_Id : Entity_Id;
2594 -- Create a local version of Deep_Finalize which has indication
2595 -- of partial initialization state.
2597 Local_DF_Id := Make_Temporary (Loc, 'F');
2600 Make_Local_Deep_Finalize (Rec_Type, Local_DF_Id));
2602 Set_Exception_Handlers (Handled_Stmt_Node, New_List (
2603 Make_Exception_Handler (Loc,
2604 Exception_Choices => New_List (
2605 Make_Others_Choice (Loc)),
2607 Statements => New_List (
2608 Make_Procedure_Call_Statement (Loc,
2610 New_Reference_To (Local_DF_Id, Loc),
2612 Parameter_Associations => New_List (
2613 Make_Identifier (Loc, Name_uInit),
2614 New_Reference_To (Standard_False, Loc))),
2616 Make_Raise_Statement (Loc)))));
2619 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2622 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2624 if not Debug_Generated_Code then
2625 Set_Debug_Info_Off (Proc_Id);
2628 -- Associate Init_Proc with type, and determine if the procedure
2629 -- is null (happens because of the Initialize_Scalars pragma case,
2630 -- where we have to generate a null procedure in case it is called
2631 -- by a client with Initialize_Scalars set). Such procedures have
2632 -- to be generated, but do not have to be called, so we mark them
2633 -- as null to suppress the call.
2635 Set_Init_Proc (Rec_Type, Proc_Id);
2637 if List_Length (Body_Stmts) = 1
2639 -- We must skip SCIL nodes because they may have been added to this
2640 -- list by Insert_Actions.
2642 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2643 and then VM_Target = No_VM
2645 -- Even though the init proc may be null at this time it might get
2646 -- some stuff added to it later by the VM backend.
2648 Set_Is_Null_Init_Proc (Proc_Id);
2650 end Build_Init_Procedure;
2652 ---------------------------
2653 -- Build_Init_Statements --
2654 ---------------------------
2656 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2657 Checks : constant List_Id := New_List;
2658 Actions : List_Id := No_List;
2659 Counter_Id : Entity_Id := Empty;
2667 procedure Increment_Counter;
2668 -- Generate an "increment by one" statement for the current counter
2669 -- and append it to the list Stmts.
2671 procedure Make_Counter;
2672 -- Create a new counter for the current component list. The routine
2673 -- creates a new defining Id, adds an object declaration and sets
2674 -- the Id generator for the next variant.
2676 -----------------------
2677 -- Increment_Counter --
2678 -----------------------
2680 procedure Increment_Counter is
2683 -- Counter := Counter + 1;
2686 Make_Assignment_Statement (Loc,
2687 Name => New_Reference_To (Counter_Id, Loc),
2690 Left_Opnd => New_Reference_To (Counter_Id, Loc),
2691 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2692 end Increment_Counter;
2698 procedure Make_Counter is
2700 -- Increment the Id generator
2702 Counter := Counter + 1;
2704 -- Create the entity and declaration
2707 Make_Defining_Identifier (Loc,
2708 Chars => New_External_Name ('C', Counter));
2711 -- Cnn : Integer := 0;
2714 Make_Object_Declaration (Loc,
2715 Defining_Identifier => Counter_Id,
2716 Object_Definition =>
2717 New_Reference_To (Standard_Integer, Loc),
2719 Make_Integer_Literal (Loc, 0)));
2722 -- Start of processing for Build_Init_Statements
2725 if Null_Present (Comp_List) then
2726 return New_List (Make_Null_Statement (Loc));
2731 -- Loop through visible declarations of task types and protected
2732 -- types moving any expanded code from the spec to the body of the
2735 if Is_Task_Record_Type (Rec_Type)
2736 or else Is_Protected_Record_Type (Rec_Type)
2739 Decl : constant Node_Id :=
2740 Parent (Corresponding_Concurrent_Type (Rec_Type));
2746 if Is_Task_Record_Type (Rec_Type) then
2747 Def := Task_Definition (Decl);
2749 Def := Protected_Definition (Decl);
2752 if Present (Def) then
2753 N1 := First (Visible_Declarations (Def));
2754 while Present (N1) loop
2758 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2759 or else Nkind (N2) in N_Raise_xxx_Error
2760 or else Nkind (N2) = N_Procedure_Call_Statement
2763 New_Copy_Tree (N2, New_Scope => Proc_Id));
2764 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2772 -- Loop through components, skipping pragmas, in 2 steps. The first
2773 -- step deals with regular components. The second step deals with
2774 -- components have per object constraints, and no explicit initia-
2779 -- First pass : regular components
2781 Decl := First_Non_Pragma (Component_Items (Comp_List));
2782 while Present (Decl) loop
2785 (Subtype_Indication (Component_Definition (Decl)), Checks);
2787 Id := Defining_Identifier (Decl);
2790 -- Leave any processing of per-object constrained component for
2793 if Has_Access_Constraint (Id)
2794 and then No (Expression (Decl))
2798 -- Regular component cases
2801 -- Explicit initialization
2803 if Present (Expression (Decl)) then
2804 if Is_CPP_Constructor_Call (Expression (Decl)) then
2806 Build_Initialization_Call
2809 Make_Selected_Component (Loc,
2811 Make_Identifier (Loc, Name_uInit),
2812 Selector_Name => New_Occurrence_Of (Id, Loc)),
2814 In_Init_Proc => True,
2815 Enclos_Type => Rec_Type,
2816 Discr_Map => Discr_Map,
2817 Constructor_Ref => Expression (Decl));
2819 Actions := Build_Assignment (Id, Expression (Decl));
2822 -- Composite component with its own Init_Proc
2824 elsif not Is_Interface (Typ)
2825 and then Has_Non_Null_Base_Init_Proc (Typ)
2828 Build_Initialization_Call
2830 Make_Selected_Component (Loc,
2831 Prefix => Make_Identifier (Loc, Name_uInit),
2832 Selector_Name => New_Occurrence_Of (Id, Loc)),
2834 In_Init_Proc => True,
2835 Enclos_Type => Rec_Type,
2836 Discr_Map => Discr_Map);
2838 Clean_Task_Names (Typ, Proc_Id);
2840 -- Simple initialization
2842 elsif Component_Needs_Simple_Initialization (Typ) then
2845 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2847 -- Nothing needed for this case
2853 if Present (Checks) then
2854 Append_List_To (Stmts, Checks);
2857 if Present (Actions) then
2858 Append_List_To (Stmts, Actions);
2860 -- Preserve the initialization state in the current counter
2862 if Chars (Id) /= Name_uParent
2863 and then Needs_Finalization (Typ)
2865 if No (Counter_Id) then
2874 Next_Non_Pragma (Decl);
2877 -- Set up tasks and protected object support. This needs to be done
2878 -- before any component with a per-object access discriminant
2879 -- constraint, or any variant part (which may contain such
2880 -- components) is initialized, because the initialization of these
2881 -- components may reference the enclosing concurrent object.
2883 -- For a task record type, add the task create call and calls to bind
2884 -- any interrupt (signal) entries.
2886 if Is_Task_Record_Type (Rec_Type) then
2888 -- In the case of the restricted run time the ATCB has already
2889 -- been preallocated.
2891 if Restricted_Profile then
2893 Make_Assignment_Statement (Loc,
2895 Make_Selected_Component (Loc,
2896 Prefix => Make_Identifier (Loc, Name_uInit),
2897 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2899 Make_Attribute_Reference (Loc,
2901 Make_Selected_Component (Loc,
2902 Prefix => Make_Identifier (Loc, Name_uInit),
2903 Selector_Name => Make_Identifier (Loc, Name_uATCB)),
2904 Attribute_Name => Name_Unchecked_Access)));
2907 Append_To (Stmts, Make_Task_Create_Call (Rec_Type));
2909 -- Generate the statements which map a string entry name to a
2910 -- task entry index. Note that the task may not have entries.
2912 if Entry_Names_OK then
2913 Names := Build_Entry_Names (Rec_Type);
2915 if Present (Names) then
2916 Append_To (Stmts, Names);
2921 Task_Type : constant Entity_Id :=
2922 Corresponding_Concurrent_Type (Rec_Type);
2923 Task_Decl : constant Node_Id := Parent (Task_Type);
2924 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2929 if Present (Task_Def) then
2930 Vis_Decl := First (Visible_Declarations (Task_Def));
2931 while Present (Vis_Decl) loop
2932 Loc := Sloc (Vis_Decl);
2934 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2935 if Get_Attribute_Id (Chars (Vis_Decl)) =
2938 Ent := Entity (Name (Vis_Decl));
2940 if Ekind (Ent) = E_Entry then
2942 Make_Procedure_Call_Statement (Loc,
2944 New_Reference_To (RTE (
2945 RE_Bind_Interrupt_To_Entry), Loc),
2946 Parameter_Associations => New_List (
2947 Make_Selected_Component (Loc,
2949 Make_Identifier (Loc, Name_uInit),
2951 Make_Identifier (Loc, Name_uTask_Id)),
2952 Entry_Index_Expression
2953 (Loc, Ent, Empty, Task_Type),
2954 Expression (Vis_Decl))));
2965 -- For a protected type, add statements generated by
2966 -- Make_Initialize_Protection.
2968 if Is_Protected_Record_Type (Rec_Type) then
2969 Append_List_To (Stmts,
2970 Make_Initialize_Protection (Rec_Type));
2972 -- Generate the statements which map a string entry name to a
2973 -- protected entry index. Note that the protected type may not
2976 if Entry_Names_OK then
2977 Names := Build_Entry_Names (Rec_Type);
2979 if Present (Names) then
2980 Append_To (Stmts, Names);
2985 -- Second pass: components with per-object constraints
2988 Decl := First_Non_Pragma (Component_Items (Comp_List));
2989 while Present (Decl) loop
2991 Id := Defining_Identifier (Decl);
2994 if Has_Access_Constraint (Id)
2995 and then No (Expression (Decl))
2997 if Has_Non_Null_Base_Init_Proc (Typ) then
2998 Append_List_To (Stmts,
2999 Build_Initialization_Call (Loc,
3000 Make_Selected_Component (Loc,
3001 Prefix => Make_Identifier (Loc, Name_uInit),
3002 Selector_Name => New_Occurrence_Of (Id, Loc)),
3004 In_Init_Proc => True,
3005 Enclos_Type => Rec_Type,
3006 Discr_Map => Discr_Map));
3008 Clean_Task_Names (Typ, Proc_Id);
3010 -- Preserve the initialization state in the current
3013 if Needs_Finalization (Typ) then
3014 if No (Counter_Id) then
3021 elsif Component_Needs_Simple_Initialization (Typ) then
3022 Append_List_To (Stmts,
3024 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
3028 Next_Non_Pragma (Decl);
3032 -- Process the variant part
3034 if Present (Variant_Part (Comp_List)) then
3036 Variant_Alts : constant List_Id := New_List;
3041 First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3042 while Present (Variant) loop
3043 Loc := Sloc (Variant);
3044 Append_To (Variant_Alts,
3045 Make_Case_Statement_Alternative (Loc,
3047 New_Copy_List (Discrete_Choices (Variant)),
3049 Build_Init_Statements (Component_List (Variant))));
3050 Next_Non_Pragma (Variant);
3053 -- The expression of the case statement which is a reference
3054 -- to one of the discriminants is replaced by the appropriate
3055 -- formal parameter of the initialization procedure.
3058 Make_Case_Statement (Loc,
3060 New_Reference_To (Discriminal (
3061 Entity (Name (Variant_Part (Comp_List)))), Loc),
3062 Alternatives => Variant_Alts));
3066 -- If no initializations when generated for component declarations
3067 -- corresponding to this Stmts, append a null statement to Stmts to
3068 -- to make it a valid Ada tree.
3070 if Is_Empty_List (Stmts) then
3071 Append (New_Node (N_Null_Statement, Loc), Stmts);
3077 when RE_Not_Available =>
3079 end Build_Init_Statements;
3081 -------------------------
3082 -- Build_Record_Checks --
3083 -------------------------
3085 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3086 Subtype_Mark_Id : Entity_Id;
3088 procedure Constrain_Array
3090 Check_List : List_Id);
3091 -- Apply a list of index constraints to an unconstrained array type.
3092 -- The first parameter is the entity for the resulting subtype.
3093 -- Check_List is a list to which the check actions are appended.
3095 ---------------------
3096 -- Constrain_Array --
3097 ---------------------
3099 procedure Constrain_Array
3101 Check_List : List_Id)
3103 C : constant Node_Id := Constraint (SI);
3104 Number_Of_Constraints : Nat := 0;
3108 procedure Constrain_Index
3111 Check_List : List_Id);
3112 -- Process an index constraint in a constrained array declaration.
3113 -- The constraint can be either a subtype name or a range with or
3114 -- without an explicit subtype mark. Index is the corresponding
3115 -- index of the unconstrained array. S is the range expression.
3116 -- Check_List is a list to which the check actions are appended.
3118 ---------------------
3119 -- Constrain_Index --
3120 ---------------------
3122 procedure Constrain_Index
3125 Check_List : List_Id)
3127 T : constant Entity_Id := Etype (Index);
3130 if Nkind (S) = N_Range then
3131 Process_Range_Expr_In_Decl (S, T, Check_List);
3133 end Constrain_Index;
3135 -- Start of processing for Constrain_Array
3138 T := Entity (Subtype_Mark (SI));
3140 if Ekind (T) in Access_Kind then
3141 T := Designated_Type (T);
3144 S := First (Constraints (C));
3146 while Present (S) loop
3147 Number_Of_Constraints := Number_Of_Constraints + 1;
3151 -- In either case, the index constraint must provide a discrete
3152 -- range for each index of the array type and the type of each
3153 -- discrete range must be the same as that of the corresponding
3154 -- index. (RM 3.6.1)
3156 S := First (Constraints (C));
3157 Index := First_Index (T);
3160 -- Apply constraints to each index type
3162 for J in 1 .. Number_Of_Constraints loop
3163 Constrain_Index (Index, S, Check_List);
3167 end Constrain_Array;
3169 -- Start of processing for Build_Record_Checks
3172 if Nkind (S) = N_Subtype_Indication then
3173 Find_Type (Subtype_Mark (S));
3174 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3176 -- Remaining processing depends on type
3178 case Ekind (Subtype_Mark_Id) is
3181 Constrain_Array (S, Check_List);
3187 end Build_Record_Checks;
3189 -------------------------------------------
3190 -- Component_Needs_Simple_Initialization --
3191 -------------------------------------------
3193 function Component_Needs_Simple_Initialization
3194 (T : Entity_Id) return Boolean
3198 Needs_Simple_Initialization (T)
3199 and then not Is_RTE (T, RE_Tag)
3201 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3203 and then not Is_RTE (T, RE_Interface_Tag);
3204 end Component_Needs_Simple_Initialization;
3206 --------------------------------------
3207 -- Parent_Subtype_Renaming_Discrims --
3208 --------------------------------------
3210 function Parent_Subtype_Renaming_Discrims return Boolean is
3215 if Base_Type (Rec_Ent) /= Rec_Ent then
3219 if Etype (Rec_Ent) = Rec_Ent
3220 or else not Has_Discriminants (Rec_Ent)
3221 or else Is_Constrained (Rec_Ent)
3222 or else Is_Tagged_Type (Rec_Ent)
3227 -- If there are no explicit stored discriminants we have inherited
3228 -- the root type discriminants so far, so no renamings occurred.
3230 if First_Discriminant (Rec_Ent) =
3231 First_Stored_Discriminant (Rec_Ent)
3236 -- Check if we have done some trivial renaming of the parent
3237 -- discriminants, i.e. something like
3239 -- type DT (X1, X2: int) is new PT (X1, X2);
3241 De := First_Discriminant (Rec_Ent);
3242 Dp := First_Discriminant (Etype (Rec_Ent));
3243 while Present (De) loop
3244 pragma Assert (Present (Dp));
3246 if Corresponding_Discriminant (De) /= Dp then
3250 Next_Discriminant (De);
3251 Next_Discriminant (Dp);
3254 return Present (Dp);
3255 end Parent_Subtype_Renaming_Discrims;
3257 ------------------------
3258 -- Requires_Init_Proc --
3259 ------------------------
3261 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3262 Comp_Decl : Node_Id;
3267 -- Definitely do not need one if specifically suppressed
3269 if Initialization_Suppressed (Rec_Id) then
3273 -- If it is a type derived from a type with unknown discriminants,
3274 -- we cannot build an initialization procedure for it.
3276 if Has_Unknown_Discriminants (Rec_Id)
3277 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3282 -- Otherwise we need to generate an initialization procedure if
3283 -- Is_CPP_Class is False and at least one of the following applies:
3285 -- 1. Discriminants are present, since they need to be initialized
3286 -- with the appropriate discriminant constraint expressions.
3287 -- However, the discriminant of an unchecked union does not
3288 -- count, since the discriminant is not present.
3290 -- 2. The type is a tagged type, since the implicit Tag component
3291 -- needs to be initialized with a pointer to the dispatch table.
3293 -- 3. The type contains tasks
3295 -- 4. One or more components has an initial value
3297 -- 5. One or more components is for a type which itself requires
3298 -- an initialization procedure.
3300 -- 6. One or more components is a type that requires simple
3301 -- initialization (see Needs_Simple_Initialization), except
3302 -- that types Tag and Interface_Tag are excluded, since fields
3303 -- of these types are initialized by other means.
3305 -- 7. The type is the record type built for a task type (since at
3306 -- the very least, Create_Task must be called)
3308 -- 8. The type is the record type built for a protected type (since
3309 -- at least Initialize_Protection must be called)
3311 -- 9. The type is marked as a public entity. The reason we add this
3312 -- case (even if none of the above apply) is to properly handle
3313 -- Initialize_Scalars. If a package is compiled without an IS
3314 -- pragma, and the client is compiled with an IS pragma, then
3315 -- the client will think an initialization procedure is present
3316 -- and call it, when in fact no such procedure is required, but
3317 -- since the call is generated, there had better be a routine
3318 -- at the other end of the call, even if it does nothing!)
3320 -- Note: the reason we exclude the CPP_Class case is because in this
3321 -- case the initialization is performed by the C++ constructors, and
3322 -- the IP is built by Set_CPP_Constructors.
3324 if Is_CPP_Class (Rec_Id) then
3327 elsif Is_Interface (Rec_Id) then
3330 elsif (Has_Discriminants (Rec_Id)
3331 and then not Is_Unchecked_Union (Rec_Id))
3332 or else Is_Tagged_Type (Rec_Id)
3333 or else Is_Concurrent_Record_Type (Rec_Id)
3334 or else Has_Task (Rec_Id)
3339 Id := First_Component (Rec_Id);
3340 while Present (Id) loop
3341 Comp_Decl := Parent (Id);
3344 if Present (Expression (Comp_Decl))
3345 or else Has_Non_Null_Base_Init_Proc (Typ)
3346 or else Component_Needs_Simple_Initialization (Typ)
3351 Next_Component (Id);
3354 -- As explained above, a record initialization procedure is needed
3355 -- for public types in case Initialize_Scalars applies to a client.
3356 -- However, such a procedure is not needed in the case where either
3357 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3358 -- applies. No_Initialize_Scalars excludes the possibility of using
3359 -- Initialize_Scalars in any partition, and No_Default_Initialization
3360 -- implies that no initialization should ever be done for objects of
3361 -- the type, so is incompatible with Initialize_Scalars.
3363 if not Restriction_Active (No_Initialize_Scalars)
3364 and then not Restriction_Active (No_Default_Initialization)
3365 and then Is_Public (Rec_Id)
3371 end Requires_Init_Proc;
3373 -- Start of processing for Build_Record_Init_Proc
3376 -- Check for value type, which means no initialization required
3378 Rec_Type := Defining_Identifier (N);
3380 if Is_Value_Type (Rec_Type) then
3384 -- This may be full declaration of a private type, in which case
3385 -- the visible entity is a record, and the private entity has been
3386 -- exchanged with it in the private part of the current package.
3387 -- The initialization procedure is built for the record type, which
3388 -- is retrievable from the private entity.
3390 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3391 Rec_Type := Underlying_Type (Rec_Type);
3394 -- If there are discriminants, build the discriminant map to replace
3395 -- discriminants by their discriminals in complex bound expressions.
3396 -- These only arise for the corresponding records of synchronized types.
3398 if Is_Concurrent_Record_Type (Rec_Type)
3399 and then Has_Discriminants (Rec_Type)
3404 Disc := First_Discriminant (Rec_Type);
3405 while Present (Disc) loop
3406 Append_Elmt (Disc, Discr_Map);
3407 Append_Elmt (Discriminal (Disc), Discr_Map);
3408 Next_Discriminant (Disc);
3413 -- Derived types that have no type extension can use the initialization
3414 -- procedure of their parent and do not need a procedure of their own.
3415 -- This is only correct if there are no representation clauses for the
3416 -- type or its parent, and if the parent has in fact been frozen so
3417 -- that its initialization procedure exists.
3419 if Is_Derived_Type (Rec_Type)
3420 and then not Is_Tagged_Type (Rec_Type)
3421 and then not Is_Unchecked_Union (Rec_Type)
3422 and then not Has_New_Non_Standard_Rep (Rec_Type)
3423 and then not Parent_Subtype_Renaming_Discrims
3424 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3426 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3428 -- Otherwise if we need an initialization procedure, then build one,
3429 -- mark it as public and inlinable and as having a completion.
3431 elsif Requires_Init_Proc (Rec_Type)
3432 or else Is_Unchecked_Union (Rec_Type)
3435 Make_Defining_Identifier (Loc,
3436 Chars => Make_Init_Proc_Name (Rec_Type));
3438 -- If No_Default_Initialization restriction is active, then we don't
3439 -- want to build an init_proc, but we need to mark that an init_proc
3440 -- would be needed if this restriction was not active (so that we can
3441 -- detect attempts to call it), so set a dummy init_proc in place.
3443 if Restriction_Active (No_Default_Initialization) then
3444 Set_Init_Proc (Rec_Type, Proc_Id);
3448 Build_Offset_To_Top_Functions;
3449 Build_CPP_Init_Procedure;
3450 Build_Init_Procedure;
3451 Set_Is_Public (Proc_Id, Is_Public (Rec_Ent));
3453 -- The initialization of protected records is not worth inlining.
3454 -- In addition, when compiled for another unit for inlining purposes,
3455 -- it may make reference to entities that have not been elaborated
3456 -- yet. The initialization of controlled records contains a nested
3457 -- clean-up procedure that makes it impractical to inline as well,
3458 -- and leads to undefined symbols if inlined in a different unit.
3459 -- Similar considerations apply to task types.
3461 if not Is_Concurrent_Type (Rec_Type)
3462 and then not Has_Task (Rec_Type)
3463 and then not Needs_Finalization (Rec_Type)
3465 Set_Is_Inlined (Proc_Id);
3468 Set_Is_Internal (Proc_Id);
3469 Set_Has_Completion (Proc_Id);
3471 if not Debug_Generated_Code then
3472 Set_Debug_Info_Off (Proc_Id);
3476 Agg : constant Node_Id :=
3477 Build_Equivalent_Record_Aggregate (Rec_Type);
3479 procedure Collect_Itypes (Comp : Node_Id);
3480 -- Generate references to itypes in the aggregate, because
3481 -- the first use of the aggregate may be in a nested scope.
3483 --------------------
3484 -- Collect_Itypes --
3485 --------------------
3487 procedure Collect_Itypes (Comp : Node_Id) is
3490 Typ : constant Entity_Id := Etype (Comp);
3493 if Is_Array_Type (Typ)
3494 and then Is_Itype (Typ)
3496 Ref := Make_Itype_Reference (Loc);
3497 Set_Itype (Ref, Typ);
3498 Append_Freeze_Action (Rec_Type, Ref);
3500 Ref := Make_Itype_Reference (Loc);
3501 Set_Itype (Ref, Etype (First_Index (Typ)));
3502 Append_Freeze_Action (Rec_Type, Ref);
3504 Sub_Aggr := First (Expressions (Comp));
3506 -- Recurse on nested arrays
3508 while Present (Sub_Aggr) loop
3509 Collect_Itypes (Sub_Aggr);
3516 -- If there is a static initialization aggregate for the type,
3517 -- generate itype references for the types of its (sub)components,
3518 -- to prevent out-of-scope errors in the resulting tree.
3519 -- The aggregate may have been rewritten as a Raise node, in which
3520 -- case there are no relevant itypes.
3523 and then Nkind (Agg) = N_Aggregate
3525 Set_Static_Initialization (Proc_Id, Agg);
3530 Comp := First (Component_Associations (Agg));
3531 while Present (Comp) loop
3532 Collect_Itypes (Expression (Comp));
3539 end Build_Record_Init_Proc;
3541 ----------------------------
3542 -- Build_Slice_Assignment --
3543 ----------------------------
3545 -- Generates the following subprogram:
3548 -- (Source, Target : Array_Type,
3549 -- Left_Lo, Left_Hi : Index;
3550 -- Right_Lo, Right_Hi : Index;
3558 -- if Left_Hi < Left_Lo then
3571 -- Target (Li1) := Source (Ri1);
3574 -- exit when Li1 = Left_Lo;
3575 -- Li1 := Index'pred (Li1);
3576 -- Ri1 := Index'pred (Ri1);
3578 -- exit when Li1 = Left_Hi;
3579 -- Li1 := Index'succ (Li1);
3580 -- Ri1 := Index'succ (Ri1);
3585 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3586 Loc : constant Source_Ptr := Sloc (Typ);
3587 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3589 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3590 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3591 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3592 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3593 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3594 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3595 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3596 -- Formal parameters of procedure
3598 Proc_Name : constant Entity_Id :=
3599 Make_Defining_Identifier (Loc,
3600 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3602 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3603 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3604 -- Subscripts for left and right sides
3611 -- Build declarations for indexes
3616 Make_Object_Declaration (Loc,
3617 Defining_Identifier => Lnn,
3618 Object_Definition =>
3619 New_Occurrence_Of (Index, Loc)));
3622 Make_Object_Declaration (Loc,
3623 Defining_Identifier => Rnn,
3624 Object_Definition =>
3625 New_Occurrence_Of (Index, Loc)));
3629 -- Build test for empty slice case
3632 Make_If_Statement (Loc,
3635 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3636 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3637 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3639 -- Build initializations for indexes
3642 F_Init : constant List_Id := New_List;
3643 B_Init : constant List_Id := New_List;
3647 Make_Assignment_Statement (Loc,
3648 Name => New_Occurrence_Of (Lnn, Loc),
3649 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3652 Make_Assignment_Statement (Loc,
3653 Name => New_Occurrence_Of (Rnn, Loc),
3654 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3657 Make_Assignment_Statement (Loc,
3658 Name => New_Occurrence_Of (Lnn, Loc),
3659 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3662 Make_Assignment_Statement (Loc,
3663 Name => New_Occurrence_Of (Rnn, Loc),
3664 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3667 Make_If_Statement (Loc,
3668 Condition => New_Occurrence_Of (Rev, Loc),
3669 Then_Statements => B_Init,
3670 Else_Statements => F_Init));
3673 -- Now construct the assignment statement
3676 Make_Loop_Statement (Loc,
3677 Statements => New_List (
3678 Make_Assignment_Statement (Loc,
3680 Make_Indexed_Component (Loc,
3681 Prefix => New_Occurrence_Of (Larray, Loc),
3682 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3684 Make_Indexed_Component (Loc,
3685 Prefix => New_Occurrence_Of (Rarray, Loc),
3686 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3687 End_Label => Empty);
3689 -- Build the exit condition and increment/decrement statements
3692 F_Ass : constant List_Id := New_List;
3693 B_Ass : constant List_Id := New_List;
3697 Make_Exit_Statement (Loc,
3700 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3701 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3704 Make_Assignment_Statement (Loc,
3705 Name => New_Occurrence_Of (Lnn, Loc),
3707 Make_Attribute_Reference (Loc,
3709 New_Occurrence_Of (Index, Loc),
3710 Attribute_Name => Name_Succ,
3711 Expressions => New_List (
3712 New_Occurrence_Of (Lnn, Loc)))));
3715 Make_Assignment_Statement (Loc,
3716 Name => New_Occurrence_Of (Rnn, Loc),
3718 Make_Attribute_Reference (Loc,
3720 New_Occurrence_Of (Index, Loc),
3721 Attribute_Name => Name_Succ,
3722 Expressions => New_List (
3723 New_Occurrence_Of (Rnn, Loc)))));
3726 Make_Exit_Statement (Loc,
3729 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3730 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3733 Make_Assignment_Statement (Loc,
3734 Name => New_Occurrence_Of (Lnn, Loc),
3736 Make_Attribute_Reference (Loc,
3738 New_Occurrence_Of (Index, Loc),
3739 Attribute_Name => Name_Pred,
3740 Expressions => New_List (
3741 New_Occurrence_Of (Lnn, Loc)))));
3744 Make_Assignment_Statement (Loc,
3745 Name => New_Occurrence_Of (Rnn, Loc),
3747 Make_Attribute_Reference (Loc,
3749 New_Occurrence_Of (Index, Loc),
3750 Attribute_Name => Name_Pred,
3751 Expressions => New_List (
3752 New_Occurrence_Of (Rnn, Loc)))));
3754 Append_To (Statements (Loops),
3755 Make_If_Statement (Loc,
3756 Condition => New_Occurrence_Of (Rev, Loc),
3757 Then_Statements => B_Ass,
3758 Else_Statements => F_Ass));
3761 Append_To (Stats, Loops);
3765 Formals : List_Id := New_List;
3768 Formals := New_List (
3769 Make_Parameter_Specification (Loc,
3770 Defining_Identifier => Larray,
3771 Out_Present => True,
3773 New_Reference_To (Base_Type (Typ), Loc)),
3775 Make_Parameter_Specification (Loc,
3776 Defining_Identifier => Rarray,
3778 New_Reference_To (Base_Type (Typ), Loc)),
3780 Make_Parameter_Specification (Loc,
3781 Defining_Identifier => Left_Lo,
3783 New_Reference_To (Index, Loc)),
3785 Make_Parameter_Specification (Loc,
3786 Defining_Identifier => Left_Hi,
3788 New_Reference_To (Index, Loc)),
3790 Make_Parameter_Specification (Loc,
3791 Defining_Identifier => Right_Lo,
3793 New_Reference_To (Index, Loc)),
3795 Make_Parameter_Specification (Loc,
3796 Defining_Identifier => Right_Hi,
3798 New_Reference_To (Index, Loc)));
3801 Make_Parameter_Specification (Loc,
3802 Defining_Identifier => Rev,
3804 New_Reference_To (Standard_Boolean, Loc)));
3807 Make_Procedure_Specification (Loc,
3808 Defining_Unit_Name => Proc_Name,
3809 Parameter_Specifications => Formals);
3812 Make_Subprogram_Body (Loc,
3813 Specification => Spec,
3814 Declarations => Decls,
3815 Handled_Statement_Sequence =>
3816 Make_Handled_Sequence_Of_Statements (Loc,
3817 Statements => Stats)));
3820 Set_TSS (Typ, Proc_Name);
3821 Set_Is_Pure (Proc_Name);
3822 end Build_Slice_Assignment;
3824 -----------------------------
3825 -- Build_Untagged_Equality --
3826 -----------------------------
3828 procedure Build_Untagged_Equality (Typ : Entity_Id) is
3836 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3837 -- Check whether the type T has a user-defined primitive equality. If so
3838 -- return it, else return Empty. If true for a component of Typ, we have
3839 -- to build the primitive equality for it.
3841 ---------------------
3842 -- User_Defined_Eq --
3843 ---------------------
3845 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3850 Op := TSS (T, TSS_Composite_Equality);
3852 if Present (Op) then
3856 Prim := First_Elmt (Collect_Primitive_Operations (T));
3857 while Present (Prim) loop
3860 if Chars (Op) = Name_Op_Eq
3861 and then Etype (Op) = Standard_Boolean
3862 and then Etype (First_Formal (Op)) = T
3863 and then Etype (Next_Formal (First_Formal (Op))) = T
3872 end User_Defined_Eq;
3874 -- Start of processing for Build_Untagged_Equality
3877 -- If a record component has a primitive equality operation, we must
3878 -- build the corresponding one for the current type.
3881 Comp := First_Component (Typ);
3882 while Present (Comp) loop
3883 if Is_Record_Type (Etype (Comp))
3884 and then Present (User_Defined_Eq (Etype (Comp)))
3889 Next_Component (Comp);
3892 -- If there is a user-defined equality for the type, we do not create
3893 -- the implicit one.
3895 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
3897 while Present (Prim) loop
3898 if Chars (Node (Prim)) = Name_Op_Eq
3899 and then Comes_From_Source (Node (Prim))
3901 -- Don't we also need to check formal types and return type as in
3902 -- User_Defined_Eq above???
3905 Eq_Op := Node (Prim);
3913 -- If the type is derived, inherit the operation, if present, from the
3914 -- parent type. It may have been declared after the type derivation. If
3915 -- the parent type itself is derived, it may have inherited an operation
3916 -- that has itself been overridden, so update its alias and related
3917 -- flags. Ditto for inequality.
3919 if No (Eq_Op) and then Is_Derived_Type (Typ) then
3920 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
3921 while Present (Prim) loop
3922 if Chars (Node (Prim)) = Name_Op_Eq then
3923 Copy_TSS (Node (Prim), Typ);
3927 Op : constant Entity_Id := User_Defined_Eq (Typ);
3928 Eq_Op : constant Entity_Id := Node (Prim);
3929 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
3932 if Present (Op) then
3933 Set_Alias (Op, Eq_Op);
3934 Set_Is_Abstract_Subprogram
3935 (Op, Is_Abstract_Subprogram (Eq_Op));
3937 if Chars (Next_Entity (Op)) = Name_Op_Ne then
3938 Set_Is_Abstract_Subprogram
3939 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
3951 -- If not inherited and not user-defined, build body as for a type with
3952 -- tagged components.
3956 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
3957 Op := Defining_Entity (Decl);
3961 if Is_Library_Level_Entity (Typ) then
3965 end Build_Untagged_Equality;
3967 ------------------------------------
3968 -- Build_Variant_Record_Equality --
3969 ------------------------------------
3973 -- function _Equality (X, Y : T) return Boolean is
3975 -- -- Compare discriminants
3977 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3981 -- -- Compare components
3983 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3987 -- -- Compare variant part
3991 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3996 -- if False or else X.Cn /= Y.Cn then
4004 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
4005 Loc : constant Source_Ptr := Sloc (Typ);
4007 F : constant Entity_Id :=
4008 Make_Defining_Identifier (Loc,
4009 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
4011 X : constant Entity_Id :=
4012 Make_Defining_Identifier (Loc,
4015 Y : constant Entity_Id :=
4016 Make_Defining_Identifier (Loc,
4019 Def : constant Node_Id := Parent (Typ);
4020 Comps : constant Node_Id := Component_List (Type_Definition (Def));
4021 Stmts : constant List_Id := New_List;
4022 Pspecs : constant List_Id := New_List;
4025 -- Derived Unchecked_Union types no longer inherit the equality function
4028 if Is_Derived_Type (Typ)
4029 and then not Is_Unchecked_Union (Typ)
4030 and then not Has_New_Non_Standard_Rep (Typ)
4033 Parent_Eq : constant Entity_Id :=
4034 TSS (Root_Type (Typ), TSS_Composite_Equality);
4037 if Present (Parent_Eq) then
4038 Copy_TSS (Parent_Eq, Typ);
4045 Make_Subprogram_Body (Loc,
4047 Make_Function_Specification (Loc,
4048 Defining_Unit_Name => F,
4049 Parameter_Specifications => Pspecs,
4050 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
4051 Declarations => New_List,
4052 Handled_Statement_Sequence =>
4053 Make_Handled_Sequence_Of_Statements (Loc,
4054 Statements => Stmts)));
4057 Make_Parameter_Specification (Loc,
4058 Defining_Identifier => X,
4059 Parameter_Type => New_Reference_To (Typ, Loc)));
4062 Make_Parameter_Specification (Loc,
4063 Defining_Identifier => Y,
4064 Parameter_Type => New_Reference_To (Typ, Loc)));
4066 -- Unchecked_Unions require additional machinery to support equality.
4067 -- Two extra parameters (A and B) are added to the equality function
4068 -- parameter list in order to capture the inferred values of the
4069 -- discriminants in later calls.
4071 if Is_Unchecked_Union (Typ) then
4073 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
4075 A : constant Node_Id :=
4076 Make_Defining_Identifier (Loc,
4079 B : constant Node_Id :=
4080 Make_Defining_Identifier (Loc,
4084 -- Add A and B to the parameter list
4087 Make_Parameter_Specification (Loc,
4088 Defining_Identifier => A,
4089 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4092 Make_Parameter_Specification (Loc,
4093 Defining_Identifier => B,
4094 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4096 -- Generate the following header code to compare the inferred
4104 Make_If_Statement (Loc,
4107 Left_Opnd => New_Reference_To (A, Loc),
4108 Right_Opnd => New_Reference_To (B, Loc)),
4109 Then_Statements => New_List (
4110 Make_Simple_Return_Statement (Loc,
4111 Expression => New_Occurrence_Of (Standard_False, Loc)))));
4113 -- Generate component-by-component comparison. Note that we must
4114 -- propagate one of the inferred discriminant formals to act as
4115 -- the case statement switch.
4117 Append_List_To (Stmts,
4118 Make_Eq_Case (Typ, Comps, A));
4121 -- Normal case (not unchecked union)
4126 Discriminant_Specifications (Def)));
4128 Append_List_To (Stmts,
4129 Make_Eq_Case (Typ, Comps));
4133 Make_Simple_Return_Statement (Loc,
4134 Expression => New_Reference_To (Standard_True, Loc)));
4139 if not Debug_Generated_Code then
4140 Set_Debug_Info_Off (F);
4142 end Build_Variant_Record_Equality;
4144 -----------------------------
4145 -- Check_Stream_Attributes --
4146 -----------------------------
4148 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4150 Par_Read : constant Boolean :=
4151 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4152 and then not Has_Specified_Stream_Read (Typ);
4153 Par_Write : constant Boolean :=
4154 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4155 and then not Has_Specified_Stream_Write (Typ);
4157 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4158 -- Check that Comp has a user-specified Nam stream attribute
4164 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4166 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4167 Error_Msg_Name_1 := Nam;
4169 ("|component& in limited extension must have% attribute", Comp);
4173 -- Start of processing for Check_Stream_Attributes
4176 if Par_Read or else Par_Write then
4177 Comp := First_Component (Typ);
4178 while Present (Comp) loop
4179 if Comes_From_Source (Comp)
4180 and then Original_Record_Component (Comp) = Comp
4181 and then Is_Limited_Type (Etype (Comp))
4184 Check_Attr (Name_Read, TSS_Stream_Read);
4188 Check_Attr (Name_Write, TSS_Stream_Write);
4192 Next_Component (Comp);
4195 end Check_Stream_Attributes;
4197 -----------------------------
4198 -- Expand_Record_Extension --
4199 -----------------------------
4201 -- Add a field _parent at the beginning of the record extension. This is
4202 -- used to implement inheritance. Here are some examples of expansion:
4204 -- 1. no discriminants
4205 -- type T2 is new T1 with null record;
4207 -- type T2 is new T1 with record
4211 -- 2. renamed discriminants
4212 -- type T2 (B, C : Int) is new T1 (A => B) with record
4213 -- _Parent : T1 (A => B);
4217 -- 3. inherited discriminants
4218 -- type T2 is new T1 with record -- discriminant A inherited
4219 -- _Parent : T1 (A);
4223 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4224 Indic : constant Node_Id := Subtype_Indication (Def);
4225 Loc : constant Source_Ptr := Sloc (Def);
4226 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
4227 Par_Subtype : Entity_Id;
4228 Comp_List : Node_Id;
4229 Comp_Decl : Node_Id;
4232 List_Constr : constant List_Id := New_List;
4235 -- Expand_Record_Extension is called directly from the semantics, so
4236 -- we must check to see whether expansion is active before proceeding
4238 if not Expander_Active then
4242 -- This may be a derivation of an untagged private type whose full
4243 -- view is tagged, in which case the Derived_Type_Definition has no
4244 -- extension part. Build an empty one now.
4246 if No (Rec_Ext_Part) then
4248 Make_Record_Definition (Loc,
4250 Component_List => Empty,
4251 Null_Present => True);
4253 Set_Record_Extension_Part (Def, Rec_Ext_Part);
4254 Mark_Rewrite_Insertion (Rec_Ext_Part);
4257 Comp_List := Component_List (Rec_Ext_Part);
4259 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4261 -- If the derived type inherits its discriminants the type of the
4262 -- _parent field must be constrained by the inherited discriminants
4264 if Has_Discriminants (T)
4265 and then Nkind (Indic) /= N_Subtype_Indication
4266 and then not Is_Constrained (Entity (Indic))
4268 D := First_Discriminant (T);
4269 while Present (D) loop
4270 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4271 Next_Discriminant (D);
4276 Make_Subtype_Indication (Loc,
4277 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
4279 Make_Index_Or_Discriminant_Constraint (Loc,
4280 Constraints => List_Constr)),
4283 -- Otherwise the original subtype_indication is just what is needed
4286 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4289 Set_Parent_Subtype (T, Par_Subtype);
4292 Make_Component_Declaration (Loc,
4293 Defining_Identifier => Parent_N,
4294 Component_Definition =>
4295 Make_Component_Definition (Loc,
4296 Aliased_Present => False,
4297 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
4299 if Null_Present (Rec_Ext_Part) then
4300 Set_Component_List (Rec_Ext_Part,
4301 Make_Component_List (Loc,
4302 Component_Items => New_List (Comp_Decl),
4303 Variant_Part => Empty,
4304 Null_Present => False));
4305 Set_Null_Present (Rec_Ext_Part, False);
4307 elsif Null_Present (Comp_List)
4308 or else Is_Empty_List (Component_Items (Comp_List))
4310 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4311 Set_Null_Present (Comp_List, False);
4314 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4317 Analyze (Comp_Decl);
4318 end Expand_Record_Extension;
4320 ------------------------------------
4321 -- Expand_N_Full_Type_Declaration --
4322 ------------------------------------
4324 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4325 Def_Id : constant Entity_Id := Defining_Identifier (N);
4326 B_Id : constant Entity_Id := Base_Type (Def_Id);
4330 procedure Build_Master (Def_Id : Entity_Id);
4331 -- Create the master associated with Def_Id
4337 procedure Build_Master (Def_Id : Entity_Id) is
4339 -- Anonymous access types are created for the components of the
4340 -- record parameter for an entry declaration. No master is created
4343 if Has_Task (Designated_Type (Def_Id))
4344 and then Comes_From_Source (N)
4346 Build_Master_Entity (Def_Id);
4347 Build_Master_Renaming (Parent (Def_Id), Def_Id);
4349 -- Create a class-wide master because a Master_Id must be generated
4350 -- for access-to-limited-class-wide types whose root may be extended
4351 -- with task components.
4353 -- Note: This code covers access-to-limited-interfaces because they
4354 -- can be used to reference tasks implementing them.
4356 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
4357 and then Is_Limited_Type (Designated_Type (Def_Id))
4358 and then Tasking_Allowed
4360 -- Do not create a class-wide master for types whose convention is
4361 -- Java since these types cannot embed Ada tasks anyway. Note that
4362 -- the following test cannot catch the following case:
4364 -- package java.lang.Object is
4365 -- type Typ is tagged limited private;
4366 -- type Ref is access all Typ'Class;
4368 -- type Typ is tagged limited ...;
4369 -- pragma Convention (Typ, Java)
4372 -- Because the convention appears after we have done the
4373 -- processing for type Ref.
4375 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
4376 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
4378 Build_Class_Wide_Master (Def_Id);
4382 -- Start of processing for Expand_N_Full_Type_Declaration
4385 if Is_Access_Type (Def_Id) then
4386 Build_Master (Def_Id);
4388 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4389 Expand_Access_Protected_Subprogram_Type (N);
4392 elsif Ada_Version >= Ada_2005
4393 and then Is_Array_Type (Def_Id)
4394 and then Is_Access_Type (Component_Type (Def_Id))
4395 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4397 Build_Master (Component_Type (Def_Id));
4399 elsif Has_Task (Def_Id) then
4400 Expand_Previous_Access_Type (Def_Id);
4402 elsif Ada_Version >= Ada_2005
4404 (Is_Record_Type (Def_Id)
4405 or else (Is_Array_Type (Def_Id)
4406 and then Is_Record_Type (Component_Type (Def_Id))))
4414 -- Look for the first anonymous access type component
4416 if Is_Array_Type (Def_Id) then
4417 Comp := First_Entity (Component_Type (Def_Id));
4419 Comp := First_Entity (Def_Id);
4422 while Present (Comp) loop
4423 Typ := Etype (Comp);
4425 exit when Is_Access_Type (Typ)
4426 and then Ekind (Typ) = E_Anonymous_Access_Type;
4431 -- If found we add a renaming declaration of master_id and we
4432 -- associate it to each anonymous access type component. Do
4433 -- nothing if the access type already has a master. This will be
4434 -- the case if the array type is the packed array created for a
4435 -- user-defined array type T, where the master_id is created when
4436 -- expanding the declaration for T.
4439 and then Ekind (Typ) = E_Anonymous_Access_Type
4440 and then not Restriction_Active (No_Task_Hierarchy)
4441 and then No (Master_Id (Typ))
4443 -- Do not consider run-times with no tasking support
4445 and then RTE_Available (RE_Current_Master)
4446 and then Has_Task (Non_Limited_Designated_Type (Typ))
4448 Build_Master_Entity (Def_Id);
4449 M_Id := Build_Master_Renaming (N, Def_Id);
4451 if Is_Array_Type (Def_Id) then
4452 Comp := First_Entity (Component_Type (Def_Id));
4454 Comp := First_Entity (Def_Id);
4457 while Present (Comp) loop
4458 Typ := Etype (Comp);
4460 if Is_Access_Type (Typ)
4461 and then Ekind (Typ) = E_Anonymous_Access_Type
4463 Set_Master_Id (Typ, M_Id);
4472 Par_Id := Etype (B_Id);
4474 -- The parent type is private then we need to inherit any TSS operations
4475 -- from the full view.
4477 if Ekind (Par_Id) in Private_Kind
4478 and then Present (Full_View (Par_Id))
4480 Par_Id := Base_Type (Full_View (Par_Id));
4483 if Nkind (Type_Definition (Original_Node (N))) =
4484 N_Derived_Type_Definition
4485 and then not Is_Tagged_Type (Def_Id)
4486 and then Present (Freeze_Node (Par_Id))
4487 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4489 Ensure_Freeze_Node (B_Id);
4490 FN := Freeze_Node (B_Id);
4492 if No (TSS_Elist (FN)) then
4493 Set_TSS_Elist (FN, New_Elmt_List);
4497 T_E : constant Elist_Id := TSS_Elist (FN);
4501 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4502 while Present (Elmt) loop
4503 if Chars (Node (Elmt)) /= Name_uInit then
4504 Append_Elmt (Node (Elmt), T_E);
4510 -- If the derived type itself is private with a full view, then
4511 -- associate the full view with the inherited TSS_Elist as well.
4513 if Ekind (B_Id) in Private_Kind
4514 and then Present (Full_View (B_Id))
4516 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4518 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4522 end Expand_N_Full_Type_Declaration;
4524 ---------------------------------
4525 -- Expand_N_Object_Declaration --
4526 ---------------------------------
4528 procedure Expand_N_Object_Declaration (N : Node_Id) is
4529 Def_Id : constant Entity_Id := Defining_Identifier (N);
4530 Expr : constant Node_Id := Expression (N);
4531 Loc : constant Source_Ptr := Sloc (N);
4532 Typ : constant Entity_Id := Etype (Def_Id);
4533 Base_Typ : constant Entity_Id := Base_Type (Typ);
4538 Init_After : Node_Id := N;
4539 -- Node after which the init proc call is to be inserted. This is
4540 -- normally N, except for the case of a shared passive variable, in
4541 -- which case the init proc call must be inserted only after the bodies
4542 -- of the shared variable procedures have been seen.
4544 function Rewrite_As_Renaming return Boolean;
4545 -- Indicate whether to rewrite a declaration with initialization into an
4546 -- object renaming declaration (see below).
4548 -------------------------
4549 -- Rewrite_As_Renaming --
4550 -------------------------
4552 function Rewrite_As_Renaming return Boolean is
4554 return not Aliased_Present (N)
4555 and then Is_Entity_Name (Expr_Q)
4556 and then Ekind (Entity (Expr_Q)) = E_Variable
4557 and then OK_To_Rename (Entity (Expr_Q))
4558 and then Is_Entity_Name (Object_Definition (N));
4559 end Rewrite_As_Renaming;
4561 -- Start of processing for Expand_N_Object_Declaration
4564 -- Don't do anything for deferred constants. All proper actions will be
4565 -- expanded during the full declaration.
4567 if No (Expr) and Constant_Present (N) then
4571 -- First we do special processing for objects of a tagged type where
4572 -- this is the point at which the type is frozen. The creation of the
4573 -- dispatch table and the initialization procedure have to be deferred
4574 -- to this point, since we reference previously declared primitive
4577 -- Force construction of dispatch tables of library level tagged types
4579 if Tagged_Type_Expansion
4580 and then Static_Dispatch_Tables
4581 and then Is_Library_Level_Entity (Def_Id)
4582 and then Is_Library_Level_Tagged_Type (Base_Typ)
4583 and then (Ekind (Base_Typ) = E_Record_Type
4584 or else Ekind (Base_Typ) = E_Protected_Type
4585 or else Ekind (Base_Typ) = E_Task_Type)
4586 and then not Has_Dispatch_Table (Base_Typ)
4589 New_Nodes : List_Id := No_List;
4592 if Is_Concurrent_Type (Base_Typ) then
4593 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4595 New_Nodes := Make_DT (Base_Typ, N);
4598 if not Is_Empty_List (New_Nodes) then
4599 Insert_List_Before (N, New_Nodes);
4604 -- Make shared memory routines for shared passive variable
4606 if Is_Shared_Passive (Def_Id) then
4607 Init_After := Make_Shared_Var_Procs (N);
4610 -- If tasks being declared, make sure we have an activation chain
4611 -- defined for the tasks (has no effect if we already have one), and
4612 -- also that a Master variable is established and that the appropriate
4613 -- enclosing construct is established as a task master.
4615 if Has_Task (Typ) then
4616 Build_Activation_Chain_Entity (N);
4617 Build_Master_Entity (Def_Id);
4620 -- Default initialization required, and no expression present
4624 -- For the default initialization case, if we have a private type
4625 -- with invariants, and invariant checks are enabled, then insert an
4626 -- invariant check after the object declaration. Note that it is OK
4627 -- to clobber the object with an invalid value since if the exception
4628 -- is raised, then the object will go out of scope.
4630 if Has_Invariants (Typ)
4631 and then Present (Invariant_Procedure (Typ))
4634 Make_Invariant_Call (New_Occurrence_Of (Def_Id, Loc)));
4637 -- Expand Initialize call for controlled objects. One may wonder why
4638 -- the Initialize Call is not done in the regular Init procedure
4639 -- attached to the record type. That's because the init procedure is
4640 -- recursively called on each component, including _Parent, thus the
4641 -- Init call for a controlled object would generate not only one
4642 -- Initialize call as it is required but one for each ancestor of
4643 -- its type. This processing is suppressed if No_Initialization set.
4645 if not Needs_Finalization (Typ)
4646 or else No_Initialization (N)
4650 elsif not Abort_Allowed
4651 or else not Comes_From_Source (N)
4653 Insert_Action_After (Init_After,
4655 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
4656 Typ => Base_Type (Typ)));
4661 -- We need to protect the initialize call
4665 -- Initialize (...);
4667 -- Undefer_Abort.all;
4670 -- ??? this won't protect the initialize call for controlled
4671 -- components which are part of the init proc, so this block
4672 -- should probably also contain the call to _init_proc but this
4673 -- requires some code reorganization...
4676 L : constant List_Id := New_List (
4678 (Obj_Ref => New_Occurrence_Of (Def_Id, Loc),
4679 Typ => Base_Type (Typ)));
4681 Blk : constant Node_Id :=
4682 Make_Block_Statement (Loc,
4683 Handled_Statement_Sequence =>
4684 Make_Handled_Sequence_Of_Statements (Loc, L));
4687 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4688 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4689 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4690 Insert_Actions_After (Init_After, New_List (Blk));
4691 Expand_At_End_Handler
4692 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4696 -- Call type initialization procedure if there is one. We build the
4697 -- call and put it immediately after the object declaration, so that
4698 -- it will be expanded in the usual manner. Note that this will
4699 -- result in proper handling of defaulted discriminants.
4701 -- Need call if there is a base init proc
4703 if Has_Non_Null_Base_Init_Proc (Typ)
4705 -- Suppress call if No_Initialization set on declaration
4707 and then not No_Initialization (N)
4709 -- Suppress call for special case of value type for VM
4711 and then not Is_Value_Type (Typ)
4713 -- Suppress call if initialization suppressed for the type
4715 and then not Initialization_Suppressed (Typ)
4717 -- Return without initializing when No_Default_Initialization
4718 -- applies. Note that the actual restriction check occurs later,
4719 -- when the object is frozen, because we don't know yet whether
4720 -- the object is imported, which is a case where the check does
4723 if Restriction_Active (No_Default_Initialization) then
4727 -- The call to the initialization procedure does NOT freeze the
4728 -- object being initialized. This is because the call is not a
4729 -- source level call. This works fine, because the only possible
4730 -- statements depending on freeze status that can appear after the
4731 -- Init_Proc call are rep clauses which can safely appear after
4732 -- actual references to the object. Note that this call may
4733 -- subsequently be removed (if a pragma Import is encountered),
4734 -- or moved to the freeze actions for the object (e.g. if an
4735 -- address clause is applied to the object, causing it to get
4736 -- delayed freezing).
4738 Id_Ref := New_Reference_To (Def_Id, Loc);
4739 Set_Must_Not_Freeze (Id_Ref);
4740 Set_Assignment_OK (Id_Ref);
4743 Init_Expr : constant Node_Id :=
4744 Static_Initialization (Base_Init_Proc (Typ));
4747 if Present (Init_Expr) then
4749 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4753 Initialization_Warning (Id_Ref);
4755 Insert_Actions_After (Init_After,
4756 Build_Initialization_Call (Loc, Id_Ref, Typ));
4760 -- If simple initialization is required, then set an appropriate
4761 -- simple initialization expression in place. This special
4762 -- initialization is required even though No_Init_Flag is present,
4763 -- but is not needed if there was an explicit initialization.
4765 -- An internally generated temporary needs no initialization because
4766 -- it will be assigned subsequently. In particular, there is no point
4767 -- in applying Initialize_Scalars to such a temporary.
4769 elsif Needs_Simple_Initialization
4772 and then not Has_Following_Address_Clause (N))
4773 and then not Is_Internal (Def_Id)
4774 and then not Has_Init_Expression (N)
4776 Set_No_Initialization (N, False);
4777 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4778 Analyze_And_Resolve (Expression (N), Typ);
4781 -- Generate attribute for Persistent_BSS if needed
4783 if Persistent_BSS_Mode
4784 and then Comes_From_Source (N)
4785 and then Is_Potentially_Persistent_Type (Typ)
4786 and then not Has_Init_Expression (N)
4787 and then Is_Library_Level_Entity (Def_Id)
4793 Make_Linker_Section_Pragma
4794 (Def_Id, Sloc (N), ".persistent.bss");
4795 Insert_After (N, Prag);
4800 -- If access type, then we know it is null if not initialized
4802 if Is_Access_Type (Typ) then
4803 Set_Is_Known_Null (Def_Id);
4806 -- Explicit initialization present
4809 -- Obtain actual expression from qualified expression
4811 if Nkind (Expr) = N_Qualified_Expression then
4812 Expr_Q := Expression (Expr);
4817 -- When we have the appropriate type of aggregate in the expression
4818 -- (it has been determined during analysis of the aggregate by
4819 -- setting the delay flag), let's perform in place assignment and
4820 -- thus avoid creating a temporary.
4822 if Is_Delayed_Aggregate (Expr_Q) then
4823 Convert_Aggr_In_Object_Decl (N);
4825 -- Ada 2005 (AI-318-02): If the initialization expression is a call
4826 -- to a build-in-place function, then access to the declared object
4827 -- must be passed to the function. Currently we limit such functions
4828 -- to those with constrained limited result subtypes, but eventually
4829 -- plan to expand the allowed forms of functions that are treated as
4832 elsif Ada_Version >= Ada_2005
4833 and then Is_Build_In_Place_Function_Call (Expr_Q)
4835 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4837 -- The previous call expands the expression initializing the
4838 -- built-in-place object into further code that will be analyzed
4839 -- later. No further expansion needed here.
4843 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4844 -- class-wide object to ensure that we copy the full object,
4845 -- unless we are targetting a VM where interfaces are handled by
4846 -- VM itself. Note that if the root type of Typ is an ancestor
4847 -- of Expr's type, both types share the same dispatch table and
4848 -- there is no need to displace the pointer.
4850 elsif Comes_From_Source (N)
4851 and then Is_Interface (Typ)
4853 pragma Assert (Is_Class_Wide_Type (Typ));
4855 -- If the object is a return object of an inherently limited type,
4856 -- which implies build-in-place treatment, bypass the special
4857 -- treatment of class-wide interface initialization below. In this
4858 -- case, the expansion of the return statement will take care of
4859 -- creating the object (via allocator) and initializing it.
4861 if Is_Return_Object (Def_Id)
4862 and then Is_Immutably_Limited_Type (Typ)
4866 elsif Tagged_Type_Expansion then
4868 Iface : constant Entity_Id := Root_Type (Typ);
4869 Expr_N : Node_Id := Expr;
4870 Expr_Typ : Entity_Id;
4876 -- If the original node of the expression was a conversion
4877 -- to this specific class-wide interface type then restore
4878 -- the original node because we must copy the object before
4879 -- displacing the pointer to reference the secondary tag
4880 -- component. This code must be kept synchronized with the
4881 -- expansion done by routine Expand_Interface_Conversion
4883 if not Comes_From_Source (Expr_N)
4884 and then Nkind (Expr_N) = N_Explicit_Dereference
4885 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
4886 and then Etype (Original_Node (Expr_N)) = Typ
4888 Rewrite (Expr_N, Original_Node (Expression (N)));
4891 -- Avoid expansion of redundant interface conversion
4893 if Is_Interface (Etype (Expr_N))
4894 and then Nkind (Expr_N) = N_Type_Conversion
4895 and then Etype (Expr_N) = Typ
4897 Expr_N := Expression (Expr_N);
4898 Set_Expression (N, Expr_N);
4901 Obj_Id := Make_Temporary (Loc, 'D', Expr_N);
4902 Expr_Typ := Base_Type (Etype (Expr_N));
4904 if Is_Class_Wide_Type (Expr_Typ) then
4905 Expr_Typ := Root_Type (Expr_Typ);
4909 -- CW : I'Class := Obj;
4912 -- type Ityp is not null access I'Class;
4913 -- CW : I'Class renames Ityp(Tmp.I_Tag'Address).all;
4915 if Comes_From_Source (Expr_N)
4916 and then Nkind (Expr_N) = N_Identifier
4917 and then not Is_Interface (Expr_Typ)
4918 and then Interface_Present_In_Ancestor (Expr_Typ, Typ)
4919 and then (Expr_Typ = Etype (Expr_Typ)
4921 Is_Variable_Size_Record (Etype (Expr_Typ)))
4926 Make_Object_Declaration (Loc,
4927 Defining_Identifier => Obj_Id,
4928 Object_Definition =>
4929 New_Occurrence_Of (Expr_Typ, Loc),
4931 Relocate_Node (Expr_N)));
4933 -- Statically reference the tag associated with the
4937 Make_Selected_Component (Loc,
4938 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4941 (Find_Interface_Tag (Expr_Typ, Iface), Loc));
4944 -- IW : I'Class := Obj;
4946 -- type Equiv_Record is record ... end record;
4947 -- implicit subtype CW is <Class_Wide_Subtype>;
4948 -- Tmp : CW := CW!(Obj);
4949 -- type Ityp is not null access I'Class;
4950 -- IW : I'Class renames
4951 -- Ityp!(Displace (Temp'Address, I'Tag)).all;
4954 -- Generate the equivalent record type and update the
4955 -- subtype indication to reference it.
4957 Expand_Subtype_From_Expr
4960 Subtype_Indic => Object_Definition (N),
4963 if not Is_Interface (Etype (Expr_N)) then
4964 New_Expr := Relocate_Node (Expr_N);
4966 -- For interface types we use 'Address which displaces
4967 -- the pointer to the base of the object (if required)
4971 Unchecked_Convert_To (Etype (Object_Definition (N)),
4972 Make_Explicit_Dereference (Loc,
4973 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4974 Make_Attribute_Reference (Loc,
4975 Prefix => Relocate_Node (Expr_N),
4976 Attribute_Name => Name_Address))));
4982 Make_Object_Declaration (Loc,
4983 Defining_Identifier => Obj_Id,
4984 Object_Definition =>
4986 (Etype (Object_Definition (N)), Loc),
4987 Expression => New_Expr));
4989 -- Dynamically reference the tag associated with the
4993 Make_Function_Call (Loc,
4994 Name => New_Reference_To (RTE (RE_Displace), Loc),
4995 Parameter_Associations => New_List (
4996 Make_Attribute_Reference (Loc,
4997 Prefix => New_Occurrence_Of (Obj_Id, Loc),
4998 Attribute_Name => Name_Address),
5000 (Node (First_Elmt (Access_Disp_Table (Iface))),
5005 Make_Object_Renaming_Declaration (Loc,
5006 Defining_Identifier => Make_Temporary (Loc, 'D'),
5007 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
5008 Name => Convert_Tag_To_Interface (Typ, Tag_Comp)));
5010 Analyze (N, Suppress => All_Checks);
5012 -- Replace internal identifier of rewritten node by the
5013 -- identifier found in the sources. We also have to exchange
5014 -- entities containing their defining identifiers to ensure
5015 -- the correct replacement of the object declaration by this
5016 -- object renaming declaration ---because these identifiers
5017 -- were previously added by Enter_Name to the current scope.
5018 -- We must preserve the homonym chain of the source entity
5021 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
5022 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
5023 Exchange_Entities (Defining_Identifier (N), Def_Id);
5029 -- Common case of explicit object initialization
5032 -- In most cases, we must check that the initial value meets any
5033 -- constraint imposed by the declared type. However, there is one
5034 -- very important exception to this rule. If the entity has an
5035 -- unconstrained nominal subtype, then it acquired its constraints
5036 -- from the expression in the first place, and not only does this
5037 -- mean that the constraint check is not needed, but an attempt to
5038 -- perform the constraint check can cause order of elaboration
5041 if not Is_Constr_Subt_For_U_Nominal (Typ) then
5043 -- If this is an allocator for an aggregate that has been
5044 -- allocated in place, delay checks until assignments are
5045 -- made, because the discriminants are not initialized.
5047 if Nkind (Expr) = N_Allocator
5048 and then No_Initialization (Expr)
5052 -- Otherwise apply a constraint check now if no prev error
5054 elsif Nkind (Expr) /= N_Error then
5055 Apply_Constraint_Check (Expr, Typ);
5057 -- If the expression has been marked as requiring a range
5058 -- generate it now and reset the flag.
5060 if Do_Range_Check (Expr) then
5061 Set_Do_Range_Check (Expr, False);
5063 if not Suppress_Assignment_Checks (N) then
5064 Generate_Range_Check
5065 (Expr, Typ, CE_Range_Check_Failed);
5071 -- If the type is controlled and not inherently limited, then
5072 -- the target is adjusted after the copy and attached to the
5073 -- finalization list. However, no adjustment is done in the case
5074 -- where the object was initialized by a call to a function whose
5075 -- result is built in place, since no copy occurred. (Eventually
5076 -- we plan to support in-place function results for some cases
5077 -- of nonlimited types. ???) Similarly, no adjustment is required
5078 -- if we are going to rewrite the object declaration into a
5079 -- renaming declaration.
5081 if Needs_Finalization (Typ)
5082 and then not Is_Immutably_Limited_Type (Typ)
5083 and then not Rewrite_As_Renaming
5085 Insert_Action_After (Init_After,
5087 Obj_Ref => New_Reference_To (Def_Id, Loc),
5088 Typ => Base_Type (Typ)));
5091 -- For tagged types, when an init value is given, the tag has to
5092 -- be re-initialized separately in order to avoid the propagation
5093 -- of a wrong tag coming from a view conversion unless the type
5094 -- is class wide (in this case the tag comes from the init value).
5095 -- Suppress the tag assignment when VM_Target because VM tags are
5096 -- represented implicitly in objects. Ditto for types that are
5097 -- CPP_CLASS, and for initializations that are aggregates, because
5098 -- they have to have the right tag.
5100 if Is_Tagged_Type (Typ)
5101 and then not Is_Class_Wide_Type (Typ)
5102 and then not Is_CPP_Class (Typ)
5103 and then Tagged_Type_Expansion
5104 and then Nkind (Expr) /= N_Aggregate
5107 Full_Typ : constant Entity_Id := Underlying_Type (Typ);
5110 -- The re-assignment of the tag has to be done even if the
5111 -- object is a constant. The assignment must be analyzed
5112 -- after the declaration.
5115 Make_Selected_Component (Loc,
5116 Prefix => New_Occurrence_Of (Def_Id, Loc),
5118 New_Reference_To (First_Tag_Component (Full_Typ),
5120 Set_Assignment_OK (New_Ref);
5122 Insert_Action_After (Init_After,
5123 Make_Assignment_Statement (Loc,
5126 Unchecked_Convert_To (RTE (RE_Tag),
5128 (Node (First_Elmt (Access_Disp_Table (Full_Typ))),
5132 elsif Is_Tagged_Type (Typ)
5133 and then Is_CPP_Constructor_Call (Expr)
5135 -- The call to the initialization procedure does NOT freeze the
5136 -- object being initialized.
5138 Id_Ref := New_Reference_To (Def_Id, Loc);
5139 Set_Must_Not_Freeze (Id_Ref);
5140 Set_Assignment_OK (Id_Ref);
5142 Insert_Actions_After (Init_After,
5143 Build_Initialization_Call (Loc, Id_Ref, Typ,
5144 Constructor_Ref => Expr));
5146 -- We remove here the original call to the constructor
5147 -- to avoid its management in the backend
5149 Set_Expression (N, Empty);
5152 -- For discrete types, set the Is_Known_Valid flag if the
5153 -- initializing value is known to be valid.
5155 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
5156 Set_Is_Known_Valid (Def_Id);
5158 elsif Is_Access_Type (Typ) then
5160 -- For access types set the Is_Known_Non_Null flag if the
5161 -- initializing value is known to be non-null. We can also set
5162 -- Can_Never_Be_Null if this is a constant.
5164 if Known_Non_Null (Expr) then
5165 Set_Is_Known_Non_Null (Def_Id, True);
5167 if Constant_Present (N) then
5168 Set_Can_Never_Be_Null (Def_Id);
5173 -- If validity checking on copies, validate initial expression.
5174 -- But skip this if declaration is for a generic type, since it
5175 -- makes no sense to validate generic types. Not clear if this
5176 -- can happen for legal programs, but it definitely can arise
5177 -- from previous instantiation errors.
5179 if Validity_Checks_On
5180 and then Validity_Check_Copies
5181 and then not Is_Generic_Type (Etype (Def_Id))
5183 Ensure_Valid (Expr);
5184 Set_Is_Known_Valid (Def_Id);
5188 -- Cases where the back end cannot handle the initialization directly
5189 -- In such cases, we expand an assignment that will be appropriately
5190 -- handled by Expand_N_Assignment_Statement.
5192 -- The exclusion of the unconstrained case is wrong, but for now it
5193 -- is too much trouble ???
5195 if (Is_Possibly_Unaligned_Slice (Expr)
5196 or else (Is_Possibly_Unaligned_Object (Expr)
5197 and then not Represented_As_Scalar (Etype (Expr))))
5198 and then not (Is_Array_Type (Etype (Expr))
5199 and then not Is_Constrained (Etype (Expr)))
5202 Stat : constant Node_Id :=
5203 Make_Assignment_Statement (Loc,
5204 Name => New_Reference_To (Def_Id, Loc),
5205 Expression => Relocate_Node (Expr));
5207 Set_Expression (N, Empty);
5208 Set_No_Initialization (N);
5209 Set_Assignment_OK (Name (Stat));
5210 Set_No_Ctrl_Actions (Stat);
5211 Insert_After_And_Analyze (Init_After, Stat);
5215 -- Final transformation, if the initializing expression is an entity
5216 -- for a variable with OK_To_Rename set, then we transform:
5222 -- X : typ renames expr
5224 -- provided that X is not aliased. The aliased case has to be
5225 -- excluded in general because Expr will not be aliased in general.
5227 if Rewrite_As_Renaming then
5229 Make_Object_Renaming_Declaration (Loc,
5230 Defining_Identifier => Defining_Identifier (N),
5231 Subtype_Mark => Object_Definition (N),
5234 -- We do not analyze this renaming declaration, because all its
5235 -- components have already been analyzed, and if we were to go
5236 -- ahead and analyze it, we would in effect be trying to generate
5237 -- another declaration of X, which won't do!
5239 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5242 -- We do need to deal with debug issues for this renaming
5244 -- First, if entity comes from source, then mark it as needing
5245 -- debug information, even though it is defined by a generated
5246 -- renaming that does not come from source.
5248 if Comes_From_Source (Defining_Identifier (N)) then
5249 Set_Needs_Debug_Info (Defining_Identifier (N));
5252 -- Now call the routine to generate debug info for the renaming
5255 Decl : constant Node_Id := Debug_Renaming_Declaration (N);
5257 if Present (Decl) then
5258 Insert_Action (N, Decl);
5264 if Nkind (N) = N_Object_Declaration
5265 and then Nkind (Object_Definition (N)) = N_Access_Definition
5266 and then not Is_Local_Anonymous_Access (Etype (Def_Id))
5268 -- An Ada 2012 stand-alone object of an anonymous access type
5271 Loc : constant Source_Ptr := Sloc (N);
5273 Level : constant Entity_Id :=
5274 Make_Defining_Identifier (Sloc (N),
5275 Chars => New_External_Name (Chars (Def_Id),
5277 Level_Expr : Node_Id;
5278 Level_Decl : Node_Id;
5280 Set_Ekind (Level, Ekind (Def_Id));
5281 Set_Etype (Level, Standard_Natural);
5282 Set_Scope (Level, Scope (Def_Id));
5285 Level_Expr := Make_Integer_Literal (Loc,
5286 -- accessibility level of null
5287 Intval => Scope_Depth (Standard_Standard));
5289 Level_Expr := Dynamic_Accessibility_Level (Expr);
5292 Level_Decl := Make_Object_Declaration (Loc,
5293 Defining_Identifier => Level,
5294 Object_Definition => New_Occurrence_Of (Standard_Natural, Loc),
5295 Expression => Level_Expr,
5296 Constant_Present => Constant_Present (N),
5297 Has_Init_Expression => True);
5299 Insert_Action_After (Init_After, Level_Decl);
5301 Set_Extra_Accessibility (Def_Id, Level);
5305 -- Exception on library entity not available
5308 when RE_Not_Available =>
5310 end Expand_N_Object_Declaration;
5312 ---------------------------------
5313 -- Expand_N_Subtype_Indication --
5314 ---------------------------------
5316 -- Add a check on the range of the subtype. The static case is partially
5317 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
5318 -- to check here for the static case in order to avoid generating
5319 -- extraneous expanded code. Also deal with validity checking.
5321 procedure Expand_N_Subtype_Indication (N : Node_Id) is
5322 Ran : constant Node_Id := Range_Expression (Constraint (N));
5323 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
5326 if Nkind (Constraint (N)) = N_Range_Constraint then
5327 Validity_Check_Range (Range_Expression (Constraint (N)));
5330 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
5331 Apply_Range_Check (Ran, Typ);
5333 end Expand_N_Subtype_Indication;
5335 ---------------------------
5336 -- Expand_N_Variant_Part --
5337 ---------------------------
5339 -- If the last variant does not contain the Others choice, replace it with
5340 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
5341 -- do not bother to call Analyze on the modified variant part, since its
5342 -- only effect would be to compute the Others_Discrete_Choices node
5343 -- laboriously, and of course we already know the list of choices that
5344 -- corresponds to the others choice (it's the list we are replacing!)
5346 procedure Expand_N_Variant_Part (N : Node_Id) is
5347 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
5348 Others_Node : Node_Id;
5350 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
5351 Others_Node := Make_Others_Choice (Sloc (Last_Var));
5352 Set_Others_Discrete_Choices
5353 (Others_Node, Discrete_Choices (Last_Var));
5354 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
5356 end Expand_N_Variant_Part;
5358 ---------------------------------
5359 -- Expand_Previous_Access_Type --
5360 ---------------------------------
5362 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
5363 T : Entity_Id := First_Entity (Current_Scope);
5366 -- Find all access types declared in the current scope, whose
5367 -- designated type is Def_Id. If it does not have a Master_Id,
5370 while Present (T) loop
5371 if Is_Access_Type (T)
5372 and then Designated_Type (T) = Def_Id
5373 and then No (Master_Id (T))
5375 Build_Master_Entity (Def_Id);
5376 Build_Master_Renaming (Parent (Def_Id), T);
5381 end Expand_Previous_Access_Type;
5383 ------------------------
5384 -- Expand_Tagged_Root --
5385 ------------------------
5387 procedure Expand_Tagged_Root (T : Entity_Id) is
5388 Def : constant Node_Id := Type_Definition (Parent (T));
5389 Comp_List : Node_Id;
5390 Comp_Decl : Node_Id;
5391 Sloc_N : Source_Ptr;
5394 if Null_Present (Def) then
5395 Set_Component_List (Def,
5396 Make_Component_List (Sloc (Def),
5397 Component_Items => Empty_List,
5398 Variant_Part => Empty,
5399 Null_Present => True));
5402 Comp_List := Component_List (Def);
5404 if Null_Present (Comp_List)
5405 or else Is_Empty_List (Component_Items (Comp_List))
5407 Sloc_N := Sloc (Comp_List);
5409 Sloc_N := Sloc (First (Component_Items (Comp_List)));
5413 Make_Component_Declaration (Sloc_N,
5414 Defining_Identifier => First_Tag_Component (T),
5415 Component_Definition =>
5416 Make_Component_Definition (Sloc_N,
5417 Aliased_Present => False,
5418 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5420 if Null_Present (Comp_List)
5421 or else Is_Empty_List (Component_Items (Comp_List))
5423 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5424 Set_Null_Present (Comp_List, False);
5427 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5430 -- We don't Analyze the whole expansion because the tag component has
5431 -- already been analyzed previously. Here we just insure that the tree
5432 -- is coherent with the semantic decoration
5434 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5437 when RE_Not_Available =>
5439 end Expand_Tagged_Root;
5441 ----------------------
5442 -- Clean_Task_Names --
5443 ----------------------
5445 procedure Clean_Task_Names
5447 Proc_Id : Entity_Id)
5451 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5452 and then not Global_Discard_Names
5453 and then Tagged_Type_Expansion
5455 Set_Uses_Sec_Stack (Proc_Id);
5457 end Clean_Task_Names;
5459 ------------------------------
5460 -- Expand_Freeze_Array_Type --
5461 ------------------------------
5463 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5464 Typ : constant Entity_Id := Entity (N);
5465 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5466 Base : constant Entity_Id := Base_Type (Typ);
5469 if not Is_Bit_Packed_Array (Typ) then
5471 -- If the component contains tasks, so does the array type. This may
5472 -- not be indicated in the array type because the component may have
5473 -- been a private type at the point of definition. Same if component
5474 -- type is controlled.
5476 Set_Has_Task (Base, Has_Task (Comp_Typ));
5477 Set_Has_Controlled_Component (Base,
5478 Has_Controlled_Component (Comp_Typ)
5479 or else Is_Controlled (Comp_Typ));
5481 if No (Init_Proc (Base)) then
5483 -- If this is an anonymous array created for a declaration with
5484 -- an initial value, its init_proc will never be called. The
5485 -- initial value itself may have been expanded into assignments,
5486 -- in which case the object declaration is carries the
5487 -- No_Initialization flag.
5490 and then Nkind (Associated_Node_For_Itype (Base)) =
5491 N_Object_Declaration
5492 and then (Present (Expression (Associated_Node_For_Itype (Base)))
5494 No_Initialization (Associated_Node_For_Itype (Base)))
5498 -- We do not need an init proc for string or wide [wide] string,
5499 -- since the only time these need initialization in normalize or
5500 -- initialize scalars mode, and these types are treated specially
5501 -- and do not need initialization procedures.
5503 elsif Root_Type (Base) = Standard_String
5504 or else Root_Type (Base) = Standard_Wide_String
5505 or else Root_Type (Base) = Standard_Wide_Wide_String
5509 -- Otherwise we have to build an init proc for the subtype
5512 Build_Array_Init_Proc (Base, N);
5517 if Has_Controlled_Component (Base) then
5518 Build_Controlling_Procs (Base);
5520 if not Is_Limited_Type (Comp_Typ)
5521 and then Number_Dimensions (Typ) = 1
5523 Build_Slice_Assignment (Typ);
5526 -- ??? Now that masters acts as heterogeneous lists, it might be
5527 -- worthwhile to revisit the global master approach.
5529 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5530 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5532 Build_Finalization_Master (Comp_Typ);
5536 -- For packed case, default initialization, except if the component type
5537 -- is itself a packed structure with an initialization procedure, or
5538 -- initialize/normalize scalars active, and we have a base type, or the
5539 -- type is public, because in that case a client might specify
5540 -- Normalize_Scalars and there better be a public Init_Proc for it.
5542 elsif (Present (Init_Proc (Component_Type (Base)))
5543 and then No (Base_Init_Proc (Base)))
5544 or else (Init_Or_Norm_Scalars and then Base = Typ)
5545 or else Is_Public (Typ)
5547 Build_Array_Init_Proc (Base, N);
5549 end Expand_Freeze_Array_Type;
5551 -----------------------------------
5552 -- Expand_Freeze_Class_Wide_Type --
5553 -----------------------------------
5555 procedure Expand_Freeze_Class_Wide_Type (N : Node_Id) is
5556 Typ : constant Entity_Id := Entity (N);
5557 Root : constant Entity_Id := Root_Type (Typ);
5559 function Is_C_Derivation (Typ : Entity_Id) return Boolean;
5560 -- Given a type, determine whether it is derived from a C or C++ root
5562 ---------------------
5563 -- Is_C_Derivation --
5564 ---------------------
5566 function Is_C_Derivation (Typ : Entity_Id) return Boolean is
5567 T : Entity_Id := Typ;
5572 or else Convention (T) = Convention_C
5573 or else Convention (T) = Convention_CPP
5578 exit when T = Etype (T);
5584 end Is_C_Derivation;
5586 -- Start of processing for Expand_Freeze_Class_Wide_Type
5589 -- Certain run-time configurations and targets do not provide support
5590 -- for controlled types.
5592 if Restriction_Active (No_Finalization) then
5595 -- Do not create TSS routine Finalize_Address when dispatching calls are
5596 -- disabled since the core of the routine is a dispatching call.
5598 elsif Restriction_Active (No_Dispatching_Calls) then
5601 -- Do not create TSS routine Finalize_Address for concurrent class-wide
5602 -- types. Ignore C, C++, CIL and Java types since it is assumed that the
5603 -- non-Ada side will handle their destruction.
5605 elsif Is_Concurrent_Type (Root)
5606 or else Is_C_Derivation (Root)
5607 or else Convention (Typ) = Convention_CIL
5608 or else Convention (Typ) = Convention_CPP
5609 or else Convention (Typ) = Convention_Java
5613 -- Do not create TSS routine Finalize_Address for .NET/JVM because these
5614 -- targets do not support address arithmetic and unchecked conversions.
5616 elsif VM_Target /= No_VM then
5619 -- Do not create TSS routine Finalize_Address when compiling in CodePeer
5620 -- mode since the routine contains an Unchecked_Conversion.
5622 elsif CodePeer_Mode then
5626 -- Create the body of TSS primitive Finalize_Address. This automatically
5627 -- sets the TSS entry for the class-wide type.
5629 Make_Finalize_Address_Body (Typ);
5630 end Expand_Freeze_Class_Wide_Type;
5632 ------------------------------------
5633 -- Expand_Freeze_Enumeration_Type --
5634 ------------------------------------
5636 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5637 Typ : constant Entity_Id := Entity (N);
5638 Loc : constant Source_Ptr := Sloc (Typ);
5645 Is_Contiguous : Boolean;
5650 pragma Warnings (Off, Func);
5653 -- Various optimizations possible if given representation is contiguous
5655 Is_Contiguous := True;
5657 Ent := First_Literal (Typ);
5658 Last_Repval := Enumeration_Rep (Ent);
5661 while Present (Ent) loop
5662 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5663 Is_Contiguous := False;
5666 Last_Repval := Enumeration_Rep (Ent);
5672 if Is_Contiguous then
5673 Set_Has_Contiguous_Rep (Typ);
5674 Ent := First_Literal (Typ);
5676 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5679 -- Build list of literal references
5684 Ent := First_Literal (Typ);
5685 while Present (Ent) loop
5686 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5692 -- Now build an array declaration
5694 -- typA : array (Natural range 0 .. num - 1) of ctype :=
5695 -- (v, v, v, v, v, ....)
5697 -- where ctype is the corresponding integer type. If the representation
5698 -- is contiguous, we only keep the first literal, which provides the
5699 -- offset for Pos_To_Rep computations.
5702 Make_Defining_Identifier (Loc,
5703 Chars => New_External_Name (Chars (Typ), 'A'));
5705 Append_Freeze_Action (Typ,
5706 Make_Object_Declaration (Loc,
5707 Defining_Identifier => Arr,
5708 Constant_Present => True,
5710 Object_Definition =>
5711 Make_Constrained_Array_Definition (Loc,
5712 Discrete_Subtype_Definitions => New_List (
5713 Make_Subtype_Indication (Loc,
5714 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5716 Make_Range_Constraint (Loc,
5720 Make_Integer_Literal (Loc, 0),
5722 Make_Integer_Literal (Loc, Num - 1))))),
5724 Component_Definition =>
5725 Make_Component_Definition (Loc,
5726 Aliased_Present => False,
5727 Subtype_Indication => New_Reference_To (Typ, Loc))),
5730 Make_Aggregate (Loc,
5731 Expressions => Lst)));
5733 Set_Enum_Pos_To_Rep (Typ, Arr);
5735 -- Now we build the function that converts representation values to
5736 -- position values. This function has the form:
5738 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5741 -- when enum-lit'Enum_Rep => return posval;
5742 -- when enum-lit'Enum_Rep => return posval;
5745 -- [raise Constraint_Error when F "invalid data"]
5750 -- Note: the F parameter determines whether the others case (no valid
5751 -- representation) raises Constraint_Error or returns a unique value
5752 -- of minus one. The latter case is used, e.g. in 'Valid code.
5754 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5755 -- the code generator making inappropriate assumptions about the range
5756 -- of the values in the case where the value is invalid. ityp is a
5757 -- signed or unsigned integer type of appropriate width.
5759 -- Note: if exceptions are not supported, then we suppress the raise
5760 -- and return -1 unconditionally (this is an erroneous program in any
5761 -- case and there is no obligation to raise Constraint_Error here!) We
5762 -- also do this if pragma Restrictions (No_Exceptions) is active.
5764 -- Is this right??? What about No_Exception_Propagation???
5766 -- Representations are signed
5768 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5770 -- The underlying type is signed. Reset the Is_Unsigned_Type
5771 -- explicitly, because it might have been inherited from
5774 Set_Is_Unsigned_Type (Typ, False);
5776 if Esize (Typ) <= Standard_Integer_Size then
5777 Ityp := Standard_Integer;
5779 Ityp := Universal_Integer;
5782 -- Representations are unsigned
5785 if Esize (Typ) <= Standard_Integer_Size then
5786 Ityp := RTE (RE_Unsigned);
5788 Ityp := RTE (RE_Long_Long_Unsigned);
5792 -- The body of the function is a case statement. First collect case
5793 -- alternatives, or optimize the contiguous case.
5797 -- If representation is contiguous, Pos is computed by subtracting
5798 -- the representation of the first literal.
5800 if Is_Contiguous then
5801 Ent := First_Literal (Typ);
5803 if Enumeration_Rep (Ent) = Last_Repval then
5805 -- Another special case: for a single literal, Pos is zero
5807 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5811 Convert_To (Standard_Integer,
5812 Make_Op_Subtract (Loc,
5814 Unchecked_Convert_To
5815 (Ityp, Make_Identifier (Loc, Name_uA)),
5817 Make_Integer_Literal (Loc,
5818 Intval => Enumeration_Rep (First_Literal (Typ)))));
5822 Make_Case_Statement_Alternative (Loc,
5823 Discrete_Choices => New_List (
5824 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5826 Make_Integer_Literal (Loc,
5827 Intval => Enumeration_Rep (Ent)),
5829 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5831 Statements => New_List (
5832 Make_Simple_Return_Statement (Loc,
5833 Expression => Pos_Expr))));
5836 Ent := First_Literal (Typ);
5837 while Present (Ent) loop
5839 Make_Case_Statement_Alternative (Loc,
5840 Discrete_Choices => New_List (
5841 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5842 Intval => Enumeration_Rep (Ent))),
5844 Statements => New_List (
5845 Make_Simple_Return_Statement (Loc,
5847 Make_Integer_Literal (Loc,
5848 Intval => Enumeration_Pos (Ent))))));
5854 -- In normal mode, add the others clause with the test
5856 if not No_Exception_Handlers_Set then
5858 Make_Case_Statement_Alternative (Loc,
5859 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5860 Statements => New_List (
5861 Make_Raise_Constraint_Error (Loc,
5862 Condition => Make_Identifier (Loc, Name_uF),
5863 Reason => CE_Invalid_Data),
5864 Make_Simple_Return_Statement (Loc,
5866 Make_Integer_Literal (Loc, -1)))));
5868 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5869 -- active then return -1 (we cannot usefully raise Constraint_Error in
5870 -- this case). See description above for further details.
5874 Make_Case_Statement_Alternative (Loc,
5875 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5876 Statements => New_List (
5877 Make_Simple_Return_Statement (Loc,
5879 Make_Integer_Literal (Loc, -1)))));
5882 -- Now we can build the function body
5885 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5888 Make_Subprogram_Body (Loc,
5890 Make_Function_Specification (Loc,
5891 Defining_Unit_Name => Fent,
5892 Parameter_Specifications => New_List (
5893 Make_Parameter_Specification (Loc,
5894 Defining_Identifier =>
5895 Make_Defining_Identifier (Loc, Name_uA),
5896 Parameter_Type => New_Reference_To (Typ, Loc)),
5897 Make_Parameter_Specification (Loc,
5898 Defining_Identifier =>
5899 Make_Defining_Identifier (Loc, Name_uF),
5900 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5902 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5904 Declarations => Empty_List,
5906 Handled_Statement_Sequence =>
5907 Make_Handled_Sequence_Of_Statements (Loc,
5908 Statements => New_List (
5909 Make_Case_Statement (Loc,
5911 Unchecked_Convert_To
5912 (Ityp, Make_Identifier (Loc, Name_uA)),
5913 Alternatives => Lst))));
5915 Set_TSS (Typ, Fent);
5917 -- Set Pure flag (it will be reset if the current context is not Pure).
5918 -- We also pretend there was a pragma Pure_Function so that for purposes
5919 -- of optimization and constant-folding, we will consider the function
5920 -- Pure even if we are not in a Pure context).
5923 Set_Has_Pragma_Pure_Function (Fent);
5925 -- Unless we are in -gnatD mode, where we are debugging generated code,
5926 -- this is an internal entity for which we don't need debug info.
5928 if not Debug_Generated_Code then
5929 Set_Debug_Info_Off (Fent);
5933 when RE_Not_Available =>
5935 end Expand_Freeze_Enumeration_Type;
5937 -------------------------------
5938 -- Expand_Freeze_Record_Type --
5939 -------------------------------
5941 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5942 Def_Id : constant Node_Id := Entity (N);
5943 Type_Decl : constant Node_Id := Parent (Def_Id);
5945 Comp_Typ : Entity_Id;
5946 Predef_List : List_Id;
5948 Renamed_Eq : Node_Id := Empty;
5949 -- Defining unit name for the predefined equality function in the case
5950 -- where the type has a primitive operation that is a renaming of
5951 -- predefined equality (but only if there is also an overriding
5952 -- user-defined equality function). Used to pass this entity from
5953 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5955 Wrapper_Decl_List : List_Id := No_List;
5956 Wrapper_Body_List : List_Id := No_List;
5958 -- Start of processing for Expand_Freeze_Record_Type
5961 -- Build discriminant checking functions if not a derived type (for
5962 -- derived types that are not tagged types, always use the discriminant
5963 -- checking functions of the parent type). However, for untagged types
5964 -- the derivation may have taken place before the parent was frozen, so
5965 -- we copy explicitly the discriminant checking functions from the
5966 -- parent into the components of the derived type.
5968 if not Is_Derived_Type (Def_Id)
5969 or else Has_New_Non_Standard_Rep (Def_Id)
5970 or else Is_Tagged_Type (Def_Id)
5972 Build_Discr_Checking_Funcs (Type_Decl);
5974 elsif Is_Derived_Type (Def_Id)
5975 and then not Is_Tagged_Type (Def_Id)
5977 -- If we have a derived Unchecked_Union, we do not inherit the
5978 -- discriminant checking functions from the parent type since the
5979 -- discriminants are non existent.
5981 and then not Is_Unchecked_Union (Def_Id)
5982 and then Has_Discriminants (Def_Id)
5985 Old_Comp : Entity_Id;
5989 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5990 Comp := First_Component (Def_Id);
5991 while Present (Comp) loop
5992 if Ekind (Comp) = E_Component
5993 and then Chars (Comp) = Chars (Old_Comp)
5995 Set_Discriminant_Checking_Func (Comp,
5996 Discriminant_Checking_Func (Old_Comp));
5999 Next_Component (Old_Comp);
6000 Next_Component (Comp);
6005 if Is_Derived_Type (Def_Id)
6006 and then Is_Limited_Type (Def_Id)
6007 and then Is_Tagged_Type (Def_Id)
6009 Check_Stream_Attributes (Def_Id);
6012 -- Update task and controlled component flags, because some of the
6013 -- component types may have been private at the point of the record
6016 Comp := First_Component (Def_Id);
6017 while Present (Comp) loop
6018 Comp_Typ := Etype (Comp);
6020 if Has_Task (Comp_Typ) then
6021 Set_Has_Task (Def_Id);
6023 -- Do not set Has_Controlled_Component on a class-wide equivalent
6024 -- type. See Make_CW_Equivalent_Type.
6026 elsif not Is_Class_Wide_Equivalent_Type (Def_Id)
6027 and then (Has_Controlled_Component (Comp_Typ)
6028 or else (Chars (Comp) /= Name_uParent
6029 and then Is_Controlled (Comp_Typ)))
6031 Set_Has_Controlled_Component (Def_Id);
6034 Next_Component (Comp);
6037 -- Handle constructors of non-tagged CPP_Class types
6039 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
6040 Set_CPP_Constructors (Def_Id);
6043 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
6044 -- for regular tagged types as well as for Ada types deriving from a C++
6045 -- Class, but not for tagged types directly corresponding to C++ classes
6046 -- In the later case we assume that it is created in the C++ side and we
6049 if Is_Tagged_Type (Def_Id) then
6051 -- Add the _Tag component
6053 if Underlying_Type (Etype (Def_Id)) = Def_Id then
6054 Expand_Tagged_Root (Def_Id);
6057 if Is_CPP_Class (Def_Id) then
6058 Set_All_DT_Position (Def_Id);
6060 -- Create the tag entities with a minimum decoration
6062 if Tagged_Type_Expansion then
6063 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6066 Set_CPP_Constructors (Def_Id);
6069 if not Building_Static_DT (Def_Id) then
6071 -- Usually inherited primitives are not delayed but the first
6072 -- Ada extension of a CPP_Class is an exception since the
6073 -- address of the inherited subprogram has to be inserted in
6074 -- the new Ada Dispatch Table and this is a freezing action.
6076 -- Similarly, if this is an inherited operation whose parent is
6077 -- not frozen yet, it is not in the DT of the parent, and we
6078 -- generate an explicit freeze node for the inherited operation
6079 -- so it is properly inserted in the DT of the current type.
6086 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6087 while Present (Elmt) loop
6088 Subp := Node (Elmt);
6090 if Present (Alias (Subp)) then
6091 if Is_CPP_Class (Etype (Def_Id)) then
6092 Set_Has_Delayed_Freeze (Subp);
6094 elsif Has_Delayed_Freeze (Alias (Subp))
6095 and then not Is_Frozen (Alias (Subp))
6097 Set_Is_Frozen (Subp, False);
6098 Set_Has_Delayed_Freeze (Subp);
6107 -- Unfreeze momentarily the type to add the predefined primitives
6108 -- operations. The reason we unfreeze is so that these predefined
6109 -- operations will indeed end up as primitive operations (which
6110 -- must be before the freeze point).
6112 Set_Is_Frozen (Def_Id, False);
6114 -- Do not add the spec of predefined primitives in case of
6115 -- CPP tagged type derivations that have convention CPP.
6117 if Is_CPP_Class (Root_Type (Def_Id))
6118 and then Convention (Def_Id) = Convention_CPP
6122 -- Do not add the spec of predefined primitives in case of
6123 -- CIL and Java tagged types
6125 elsif Convention (Def_Id) = Convention_CIL
6126 or else Convention (Def_Id) = Convention_Java
6130 -- Do not add the spec of the predefined primitives if we are
6131 -- compiling under restriction No_Dispatching_Calls.
6133 elsif not Restriction_Active (No_Dispatching_Calls) then
6134 Make_Predefined_Primitive_Specs
6135 (Def_Id, Predef_List, Renamed_Eq);
6136 Insert_List_Before_And_Analyze (N, Predef_List);
6139 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6140 -- wrapper functions for each nonoverridden inherited function
6141 -- with a controlling result of the type. The wrapper for such
6142 -- a function returns an extension aggregate that invokes the
6145 if Ada_Version >= Ada_2005
6146 and then not Is_Abstract_Type (Def_Id)
6147 and then Is_Null_Extension (Def_Id)
6149 Make_Controlling_Function_Wrappers
6150 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6151 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6154 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6155 -- null procedure declarations for each set of homographic null
6156 -- procedures that are inherited from interface types but not
6157 -- overridden. This is done to ensure that the dispatch table
6158 -- entry associated with such null primitives are properly filled.
6160 if Ada_Version >= Ada_2005
6161 and then Etype (Def_Id) /= Def_Id
6162 and then not Is_Abstract_Type (Def_Id)
6163 and then Has_Interfaces (Def_Id)
6165 Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6168 Set_Is_Frozen (Def_Id);
6169 if not Is_Derived_Type (Def_Id)
6170 or else Is_Tagged_Type (Etype (Def_Id))
6172 Set_All_DT_Position (Def_Id);
6175 -- Create and decorate the tags. Suppress their creation when
6176 -- VM_Target because the dispatching mechanism is handled
6177 -- internally by the VMs.
6179 if Tagged_Type_Expansion then
6180 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6182 -- Generate dispatch table of locally defined tagged type.
6183 -- Dispatch tables of library level tagged types are built
6184 -- later (see Analyze_Declarations).
6186 if not Building_Static_DT (Def_Id) then
6187 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
6190 elsif VM_Target /= No_VM then
6191 Append_Freeze_Actions (Def_Id, Make_VM_TSD (Def_Id));
6194 -- If the type has unknown discriminants, propagate dispatching
6195 -- information to its underlying record view, which does not get
6196 -- its own dispatch table.
6198 if Is_Derived_Type (Def_Id)
6199 and then Has_Unknown_Discriminants (Def_Id)
6200 and then Present (Underlying_Record_View (Def_Id))
6203 Rep : constant Entity_Id := Underlying_Record_View (Def_Id);
6205 Set_Access_Disp_Table
6206 (Rep, Access_Disp_Table (Def_Id));
6207 Set_Dispatch_Table_Wrappers
6208 (Rep, Dispatch_Table_Wrappers (Def_Id));
6209 Set_Direct_Primitive_Operations
6210 (Rep, Direct_Primitive_Operations (Def_Id));
6214 -- Make sure that the primitives Initialize, Adjust and Finalize
6215 -- are Frozen before other TSS subprograms. We don't want them
6218 if Is_Controlled (Def_Id) then
6219 if not Is_Limited_Type (Def_Id) then
6220 Append_Freeze_Actions (Def_Id,
6222 (Find_Prim_Op (Def_Id, Name_Adjust), Def_Id));
6225 Append_Freeze_Actions (Def_Id,
6227 (Find_Prim_Op (Def_Id, Name_Initialize), Def_Id));
6229 Append_Freeze_Actions (Def_Id,
6231 (Find_Prim_Op (Def_Id, Name_Finalize), Def_Id));
6234 -- Freeze rest of primitive operations. There is no need to handle
6235 -- the predefined primitives if we are compiling under restriction
6236 -- No_Dispatching_Calls.
6238 if not Restriction_Active (No_Dispatching_Calls) then
6239 Append_Freeze_Actions
6240 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
6244 -- In the non-tagged case, ever since Ada83 an equality function must
6245 -- be provided for variant records that are not unchecked unions.
6246 -- In Ada 2012 the equality function composes, and thus must be built
6247 -- explicitly just as for tagged records.
6249 elsif Has_Discriminants (Def_Id)
6250 and then not Is_Limited_Type (Def_Id)
6253 Comps : constant Node_Id :=
6254 Component_List (Type_Definition (Type_Decl));
6257 and then Present (Variant_Part (Comps))
6259 Build_Variant_Record_Equality (Def_Id);
6263 -- Otherwise create primitive equality operation (AI05-0123)
6265 -- This is done unconditionally to ensure that tools can be linked
6266 -- properly with user programs compiled with older language versions.
6267 -- It might be worth including a switch to revert to a non-composable
6268 -- equality for untagged records, even though no program depending on
6269 -- non-composability has surfaced ???
6271 elsif Comes_From_Source (Def_Id)
6272 and then Convention (Def_Id) = Convention_Ada
6273 and then not Is_Limited_Type (Def_Id)
6275 Build_Untagged_Equality (Def_Id);
6278 -- Before building the record initialization procedure, if we are
6279 -- dealing with a concurrent record value type, then we must go through
6280 -- the discriminants, exchanging discriminals between the concurrent
6281 -- type and the concurrent record value type. See the section "Handling
6282 -- of Discriminants" in the Einfo spec for details.
6284 if Is_Concurrent_Record_Type (Def_Id)
6285 and then Has_Discriminants (Def_Id)
6288 Ctyp : constant Entity_Id :=
6289 Corresponding_Concurrent_Type (Def_Id);
6290 Conc_Discr : Entity_Id;
6291 Rec_Discr : Entity_Id;
6295 Conc_Discr := First_Discriminant (Ctyp);
6296 Rec_Discr := First_Discriminant (Def_Id);
6297 while Present (Conc_Discr) loop
6298 Temp := Discriminal (Conc_Discr);
6299 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6300 Set_Discriminal (Rec_Discr, Temp);
6302 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6303 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
6305 Next_Discriminant (Conc_Discr);
6306 Next_Discriminant (Rec_Discr);
6311 if Has_Controlled_Component (Def_Id) then
6312 Build_Controlling_Procs (Def_Id);
6315 Adjust_Discriminants (Def_Id);
6317 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
6319 -- Do not need init for interfaces on e.g. CIL since they're
6320 -- abstract. Helps operation of peverify (the PE Verify tool).
6322 Build_Record_Init_Proc (Type_Decl, Def_Id);
6325 -- For tagged type that are not interfaces, build bodies of primitive
6326 -- operations. Note: do this after building the record initialization
6327 -- procedure, since the primitive operations may need the initialization
6328 -- routine. There is no need to add predefined primitives of interfaces
6329 -- because all their predefined primitives are abstract.
6331 if Is_Tagged_Type (Def_Id)
6332 and then not Is_Interface (Def_Id)
6334 -- Do not add the body of predefined primitives in case of
6335 -- CPP tagged type derivations that have convention CPP.
6337 if Is_CPP_Class (Root_Type (Def_Id))
6338 and then Convention (Def_Id) = Convention_CPP
6342 -- Do not add the body of predefined primitives in case of
6343 -- CIL and Java tagged types.
6345 elsif Convention (Def_Id) = Convention_CIL
6346 or else Convention (Def_Id) = Convention_Java
6350 -- Do not add the body of the predefined primitives if we are
6351 -- compiling under restriction No_Dispatching_Calls or if we are
6352 -- compiling a CPP tagged type.
6354 elsif not Restriction_Active (No_Dispatching_Calls) then
6356 -- Create the body of TSS primitive Finalize_Address. This must
6357 -- be done before the bodies of all predefined primitives are
6358 -- created. If Def_Id is limited, Stream_Input and Streap_Read
6359 -- may produce build-in-place allocations and for that the
6360 -- expander needs Finalize_Address.
6362 Make_Finalize_Address_Body (Def_Id);
6364 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6365 Append_Freeze_Actions (Def_Id, Predef_List);
6368 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6369 -- inherited functions, then add their bodies to the freeze actions.
6371 if Present (Wrapper_Body_List) then
6372 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6375 -- Create extra formals for the primitive operations of the type.
6376 -- This must be done before analyzing the body of the initialization
6377 -- procedure, because a self-referential type might call one of these
6378 -- primitives in the body of the init_proc itself.
6385 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6386 while Present (Elmt) loop
6387 Subp := Node (Elmt);
6388 if not Has_Foreign_Convention (Subp)
6389 and then not Is_Predefined_Dispatching_Operation (Subp)
6391 Create_Extra_Formals (Subp);
6399 -- Processing for components of anonymous access type that designate
6400 -- a controlled type.
6402 Comp := First_Component (Def_Id);
6403 while Present (Comp) loop
6404 Comp_Typ := Etype (Comp);
6406 if Ekind (Comp_Typ) = E_Anonymous_Access_Type
6407 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
6409 -- Avoid self-references
6411 and then Directly_Designated_Type (Comp_Typ) /= Def_Id
6413 Build_Finalization_Master
6415 Ins_Node => Parent (Def_Id),
6416 Encl_Scope => Scope (Def_Id));
6419 Next_Component (Comp);
6421 end Expand_Freeze_Record_Type;
6423 ------------------------------
6424 -- Freeze_Stream_Operations --
6425 ------------------------------
6427 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6428 Names : constant array (1 .. 4) of TSS_Name_Type :=
6433 Stream_Op : Entity_Id;
6436 -- Primitive operations of tagged types are frozen when the dispatch
6437 -- table is constructed.
6439 if not Comes_From_Source (Typ)
6440 or else Is_Tagged_Type (Typ)
6445 for J in Names'Range loop
6446 Stream_Op := TSS (Typ, Names (J));
6448 if Present (Stream_Op)
6449 and then Is_Subprogram (Stream_Op)
6450 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6451 N_Subprogram_Declaration
6452 and then not Is_Frozen (Stream_Op)
6454 Append_Freeze_Actions (Typ, Freeze_Entity (Stream_Op, N));
6457 end Freeze_Stream_Operations;
6463 -- Full type declarations are expanded at the point at which the type is
6464 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
6465 -- declarations generated by the freezing (e.g. the procedure generated
6466 -- for initialization) are chained in the Actions field list of the freeze
6467 -- node using Append_Freeze_Actions.
6469 function Freeze_Type (N : Node_Id) return Boolean is
6470 Def_Id : constant Entity_Id := Entity (N);
6471 RACW_Seen : Boolean := False;
6472 Result : Boolean := False;
6475 -- Process associated access types needing special processing
6477 if Present (Access_Types_To_Process (N)) then
6479 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
6481 while Present (E) loop
6483 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6484 Validate_RACW_Primitives (Node (E));
6494 -- If there are RACWs designating this type, make stubs now
6496 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
6500 -- Freeze processing for record types
6502 if Is_Record_Type (Def_Id) then
6503 if Ekind (Def_Id) = E_Record_Type then
6504 Expand_Freeze_Record_Type (N);
6506 elsif Is_Class_Wide_Type (Def_Id) then
6507 Expand_Freeze_Class_Wide_Type (N);
6510 -- Freeze processing for array types
6512 elsif Is_Array_Type (Def_Id) then
6513 Expand_Freeze_Array_Type (N);
6515 -- Freeze processing for access types
6517 -- For pool-specific access types, find out the pool object used for
6518 -- this type, needs actual expansion of it in some cases. Here are the
6519 -- different cases :
6521 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
6522 -- ---> don't use any storage pool
6524 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
6526 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
6528 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6529 -- ---> Storage Pool is the specified one
6531 -- See GNAT Pool packages in the Run-Time for more details
6533 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
6535 Loc : constant Source_Ptr := Sloc (N);
6536 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
6537 Pool_Object : Entity_Id;
6539 Freeze_Action_Typ : Entity_Id;
6544 -- Rep Clause "for Def_Id'Storage_Size use 0;"
6545 -- ---> don't use any storage pool
6547 if No_Pool_Assigned (Def_Id) then
6552 -- Rep Clause : for Def_Id'Storage_Size use Expr.
6554 -- Def_Id__Pool : Stack_Bounded_Pool
6555 -- (Expr, DT'Size, DT'Alignment);
6557 elsif Has_Storage_Size_Clause (Def_Id) then
6563 -- For unconstrained composite types we give a size of zero
6564 -- so that the pool knows that it needs a special algorithm
6565 -- for variable size object allocation.
6567 if Is_Composite_Type (Desig_Type)
6568 and then not Is_Constrained (Desig_Type)
6571 Make_Integer_Literal (Loc, 0);
6574 Make_Integer_Literal (Loc, Maximum_Alignment);
6578 Make_Attribute_Reference (Loc,
6579 Prefix => New_Reference_To (Desig_Type, Loc),
6580 Attribute_Name => Name_Max_Size_In_Storage_Elements);
6583 Make_Attribute_Reference (Loc,
6584 Prefix => New_Reference_To (Desig_Type, Loc),
6585 Attribute_Name => Name_Alignment);
6589 Make_Defining_Identifier (Loc,
6590 Chars => New_External_Name (Chars (Def_Id), 'P'));
6592 -- We put the code associated with the pools in the entity
6593 -- that has the later freeze node, usually the access type
6594 -- but it can also be the designated_type; because the pool
6595 -- code requires both those types to be frozen
6597 if Is_Frozen (Desig_Type)
6598 and then (No (Freeze_Node (Desig_Type))
6599 or else Analyzed (Freeze_Node (Desig_Type)))
6601 Freeze_Action_Typ := Def_Id;
6603 -- A Taft amendment type cannot get the freeze actions
6604 -- since the full view is not there.
6606 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6607 and then No (Full_View (Desig_Type))
6609 Freeze_Action_Typ := Def_Id;
6612 Freeze_Action_Typ := Desig_Type;
6615 Append_Freeze_Action (Freeze_Action_Typ,
6616 Make_Object_Declaration (Loc,
6617 Defining_Identifier => Pool_Object,
6618 Object_Definition =>
6619 Make_Subtype_Indication (Loc,
6622 (RTE (RE_Stack_Bounded_Pool), Loc),
6625 Make_Index_Or_Discriminant_Constraint (Loc,
6626 Constraints => New_List (
6628 -- First discriminant is the Pool Size
6631 Storage_Size_Variable (Def_Id), Loc),
6633 -- Second discriminant is the element size
6637 -- Third discriminant is the alignment
6642 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6646 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6647 -- ---> Storage Pool is the specified one
6649 -- When compiling in Ada 2012 mode, ensure that the accessibility
6650 -- level of the subpool access type is not deeper than that of the
6651 -- pool_with_subpools. This check is not performed on .NET/JVM
6652 -- since those targets do not support pools.
6654 elsif Ada_Version >= Ada_2012
6655 and then Present (Associated_Storage_Pool (Def_Id))
6656 and then VM_Target = No_VM
6659 Loc : constant Source_Ptr := Sloc (Def_Id);
6660 Pool : constant Entity_Id :=
6661 Associated_Storage_Pool (Def_Id);
6662 RSPWS : constant Entity_Id :=
6663 RTE (RE_Root_Storage_Pool_With_Subpools);
6666 -- It is known that the accessibility level of the access
6667 -- type is deeper than that of the pool.
6669 if Type_Access_Level (Def_Id) > Object_Access_Level (Pool)
6670 and then not Accessibility_Checks_Suppressed (Def_Id)
6671 and then not Accessibility_Checks_Suppressed (Pool)
6673 -- Static case: the pool is known to be a descendant of
6674 -- Root_Storage_Pool_With_Subpools.
6676 if Is_Ancestor (RSPWS, Etype (Pool)) then
6678 ("?subpool access type has deeper accessibility " &
6679 "level than pool", Def_Id);
6681 Append_Freeze_Action (Def_Id,
6682 Make_Raise_Program_Error (Loc,
6683 Reason => PE_Accessibility_Check_Failed));
6685 -- Dynamic case: when the pool is of a class-wide type,
6686 -- it may or may not support subpools depending on the
6687 -- path of derivation. Generate:
6689 -- if Def_Id in RSPWS'Class then
6690 -- raise Program_Error;
6693 elsif Is_Class_Wide_Type (Etype (Pool)) then
6694 Append_Freeze_Action (Def_Id,
6695 Make_If_Statement (Loc,
6699 New_Reference_To (Pool, Loc),
6702 (Class_Wide_Type (RSPWS), Loc)),
6704 Then_Statements => New_List (
6705 Make_Raise_Program_Error (Loc,
6706 Reason => PE_Accessibility_Check_Failed))));
6712 -- For access-to-controlled types (including class-wide types and
6713 -- Taft-amendment types which potentially have controlled
6714 -- components), expand the list controller object that will store
6715 -- the dynamically allocated objects. Do not do this
6716 -- transformation for expander-generated access types, but do it
6717 -- for types that are the full view of types derived from other
6718 -- private types. Also suppress the list controller in the case
6719 -- of a designated type with convention Java, since this is used
6720 -- when binding to Java API specs, where there's no equivalent of
6721 -- a finalization list and we don't want to pull in the
6722 -- finalization support if not needed.
6724 if not Comes_From_Source (Def_Id)
6725 and then not Has_Private_Declaration (Def_Id)
6729 -- An exception is made for types defined in the run-time because
6730 -- Ada.Tags.Tag itself is such a type and cannot afford this
6731 -- unnecessary overhead that would generates a loop in the
6732 -- expansion scheme. Another exception is if Restrictions
6733 -- (No_Finalization) is active, since then we know nothing is
6736 elsif Restriction_Active (No_Finalization)
6737 or else In_Runtime (Def_Id)
6741 -- The machinery assumes that incomplete or private types are
6742 -- always completed by a controlled full vies.
6744 elsif Needs_Finalization (Desig_Type)
6746 (Is_Incomplete_Or_Private_Type (Desig_Type)
6747 and then No (Full_View (Desig_Type)))
6749 (Is_Array_Type (Desig_Type)
6750 and then Needs_Finalization (Component_Type (Desig_Type)))
6752 Build_Finalization_Master (Def_Id);
6756 -- Freeze processing for enumeration types
6758 elsif Ekind (Def_Id) = E_Enumeration_Type then
6760 -- We only have something to do if we have a non-standard
6761 -- representation (i.e. at least one literal whose pos value
6762 -- is not the same as its representation)
6764 if Has_Non_Standard_Rep (Def_Id) then
6765 Expand_Freeze_Enumeration_Type (N);
6768 -- Private types that are completed by a derivation from a private
6769 -- type have an internally generated full view, that needs to be
6770 -- frozen. This must be done explicitly because the two views share
6771 -- the freeze node, and the underlying full view is not visible when
6772 -- the freeze node is analyzed.
6774 elsif Is_Private_Type (Def_Id)
6775 and then Is_Derived_Type (Def_Id)
6776 and then Present (Full_View (Def_Id))
6777 and then Is_Itype (Full_View (Def_Id))
6778 and then Has_Private_Declaration (Full_View (Def_Id))
6779 and then Freeze_Node (Full_View (Def_Id)) = N
6781 Set_Entity (N, Full_View (Def_Id));
6782 Result := Freeze_Type (N);
6783 Set_Entity (N, Def_Id);
6785 -- All other types require no expander action. There are such cases
6786 -- (e.g. task types and protected types). In such cases, the freeze
6787 -- nodes are there for use by Gigi.
6791 Freeze_Stream_Operations (N, Def_Id);
6795 when RE_Not_Available =>
6799 -------------------------
6800 -- Get_Simple_Init_Val --
6801 -------------------------
6803 function Get_Simple_Init_Val
6806 Size : Uint := No_Uint) return Node_Id
6808 Loc : constant Source_Ptr := Sloc (N);
6814 -- This is the size to be used for computation of the appropriate
6815 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
6817 IV_Attribute : constant Boolean :=
6818 Nkind (N) = N_Attribute_Reference
6819 and then Attribute_Name (N) = Name_Invalid_Value;
6823 -- These are the values computed by the procedure Check_Subtype_Bounds
6825 procedure Check_Subtype_Bounds;
6826 -- This procedure examines the subtype T, and its ancestor subtypes and
6827 -- derived types to determine the best known information about the
6828 -- bounds of the subtype. After the call Lo_Bound is set either to
6829 -- No_Uint if no information can be determined, or to a value which
6830 -- represents a known low bound, i.e. a valid value of the subtype can
6831 -- not be less than this value. Hi_Bound is similarly set to a known
6832 -- high bound (valid value cannot be greater than this).
6834 --------------------------
6835 -- Check_Subtype_Bounds --
6836 --------------------------
6838 procedure Check_Subtype_Bounds is
6847 Lo_Bound := No_Uint;
6848 Hi_Bound := No_Uint;
6850 -- Loop to climb ancestor subtypes and derived types
6854 if not Is_Discrete_Type (ST1) then
6858 Lo := Type_Low_Bound (ST1);
6859 Hi := Type_High_Bound (ST1);
6861 if Compile_Time_Known_Value (Lo) then
6862 Loval := Expr_Value (Lo);
6864 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6869 if Compile_Time_Known_Value (Hi) then
6870 Hival := Expr_Value (Hi);
6872 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6877 ST2 := Ancestor_Subtype (ST1);
6883 exit when ST1 = ST2;
6886 end Check_Subtype_Bounds;
6888 -- Start of processing for Get_Simple_Init_Val
6891 -- For a private type, we should always have an underlying type
6892 -- (because this was already checked in Needs_Simple_Initialization).
6893 -- What we do is to get the value for the underlying type and then do
6894 -- an Unchecked_Convert to the private type.
6896 if Is_Private_Type (T) then
6897 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6899 -- A special case, if the underlying value is null, then qualify it
6900 -- with the underlying type, so that the null is properly typed
6901 -- Similarly, if it is an aggregate it must be qualified, because an
6902 -- unchecked conversion does not provide a context for it.
6904 if Nkind_In (Val, N_Null, N_Aggregate) then
6906 Make_Qualified_Expression (Loc,
6908 New_Occurrence_Of (Underlying_Type (T), Loc),
6912 Result := Unchecked_Convert_To (T, Val);
6914 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6916 if Nkind (Result) = N_Unchecked_Type_Conversion
6917 and then Is_Scalar_Type (Underlying_Type (T))
6919 Set_No_Truncation (Result);
6924 -- Scalars with Default_Value aspect
6926 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
6930 (Get_Rep_Item_For_Entity
6931 (First_Subtype (T), Name_Default_Value)));
6933 -- Otherwise, for scalars, we must have normalize/initialize scalars
6934 -- case, or if the node N is an 'Invalid_Value attribute node.
6936 elsif Is_Scalar_Type (T) then
6937 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6939 -- Compute size of object. If it is given by the caller, we can use
6940 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6941 -- we know this covers all cases correctly.
6943 if Size = No_Uint or else Size <= Uint_0 then
6944 Size_To_Use := UI_Max (Uint_1, Esize (T));
6946 Size_To_Use := Size;
6949 -- Maximum size to use is 64 bits, since we will create values of
6950 -- type Unsigned_64 and the range must fit this type.
6952 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6953 Size_To_Use := Uint_64;
6956 -- Check known bounds of subtype
6958 Check_Subtype_Bounds;
6960 -- Processing for Normalize_Scalars case
6962 if Normalize_Scalars and then not IV_Attribute then
6964 -- If zero is invalid, it is a convenient value to use that is
6965 -- for sure an appropriate invalid value in all situations.
6967 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6968 Val := Make_Integer_Literal (Loc, 0);
6970 -- Cases where all one bits is the appropriate invalid value
6972 -- For modular types, all 1 bits is either invalid or valid. If
6973 -- it is valid, then there is nothing that can be done since there
6974 -- are no invalid values (we ruled out zero already).
6976 -- For signed integer types that have no negative values, either
6977 -- there is room for negative values, or there is not. If there
6978 -- is, then all 1-bits may be interpreted as minus one, which is
6979 -- certainly invalid. Alternatively it is treated as the largest
6980 -- positive value, in which case the observation for modular types
6983 -- For float types, all 1-bits is a NaN (not a number), which is
6984 -- certainly an appropriately invalid value.
6986 elsif Is_Unsigned_Type (T)
6987 or else Is_Floating_Point_Type (T)
6988 or else Is_Enumeration_Type (T)
6990 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6992 -- Resolve as Unsigned_64, because the largest number we can
6993 -- generate is out of range of universal integer.
6995 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6997 -- Case of signed types
7001 Signed_Size : constant Uint :=
7002 UI_Min (Uint_63, Size_To_Use - 1);
7005 -- Normally we like to use the most negative number. The one
7006 -- exception is when this number is in the known subtype
7007 -- range and the largest positive number is not in the known
7010 -- For this exceptional case, use largest positive value
7012 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
7013 and then Lo_Bound <= (-(2 ** Signed_Size))
7014 and then Hi_Bound < 2 ** Signed_Size
7016 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
7018 -- Normal case of largest negative value
7021 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
7026 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
7029 -- For float types, use float values from System.Scalar_Values
7031 if Is_Floating_Point_Type (T) then
7032 if Root_Type (T) = Standard_Short_Float then
7033 Val_RE := RE_IS_Isf;
7034 elsif Root_Type (T) = Standard_Float then
7035 Val_RE := RE_IS_Ifl;
7036 elsif Root_Type (T) = Standard_Long_Float then
7037 Val_RE := RE_IS_Ilf;
7038 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
7039 Val_RE := RE_IS_Ill;
7042 -- If zero is invalid, use zero values from System.Scalar_Values
7044 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
7045 if Size_To_Use <= 8 then
7046 Val_RE := RE_IS_Iz1;
7047 elsif Size_To_Use <= 16 then
7048 Val_RE := RE_IS_Iz2;
7049 elsif Size_To_Use <= 32 then
7050 Val_RE := RE_IS_Iz4;
7052 Val_RE := RE_IS_Iz8;
7055 -- For unsigned, use unsigned values from System.Scalar_Values
7057 elsif Is_Unsigned_Type (T) then
7058 if Size_To_Use <= 8 then
7059 Val_RE := RE_IS_Iu1;
7060 elsif Size_To_Use <= 16 then
7061 Val_RE := RE_IS_Iu2;
7062 elsif Size_To_Use <= 32 then
7063 Val_RE := RE_IS_Iu4;
7065 Val_RE := RE_IS_Iu8;
7068 -- For signed, use signed values from System.Scalar_Values
7071 if Size_To_Use <= 8 then
7072 Val_RE := RE_IS_Is1;
7073 elsif Size_To_Use <= 16 then
7074 Val_RE := RE_IS_Is2;
7075 elsif Size_To_Use <= 32 then
7076 Val_RE := RE_IS_Is4;
7078 Val_RE := RE_IS_Is8;
7082 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
7085 -- The final expression is obtained by doing an unchecked conversion
7086 -- of this result to the base type of the required subtype. We use
7087 -- the base type to prevent the unchecked conversion from chopping
7088 -- bits, and then we set Kill_Range_Check to preserve the "bad"
7091 Result := Unchecked_Convert_To (Base_Type (T), Val);
7093 -- Ensure result is not truncated, since we want the "bad" bits, and
7094 -- also kill range check on result.
7096 if Nkind (Result) = N_Unchecked_Type_Conversion then
7097 Set_No_Truncation (Result);
7098 Set_Kill_Range_Check (Result, True);
7103 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7105 elsif Root_Type (T) = Standard_String
7107 Root_Type (T) = Standard_Wide_String
7109 Root_Type (T) = Standard_Wide_Wide_String
7111 pragma Assert (Init_Or_Norm_Scalars);
7114 Make_Aggregate (Loc,
7115 Component_Associations => New_List (
7116 Make_Component_Association (Loc,
7117 Choices => New_List (
7118 Make_Others_Choice (Loc)),
7121 (Component_Type (T), N, Esize (Root_Type (T))))));
7123 -- Access type is initialized to null
7125 elsif Is_Access_Type (T) then
7126 return Make_Null (Loc);
7128 -- No other possibilities should arise, since we should only be calling
7129 -- Get_Simple_Init_Val if Needs_Simple_Initialization returned True,
7130 -- indicating one of the above cases held.
7133 raise Program_Error;
7137 when RE_Not_Available =>
7139 end Get_Simple_Init_Val;
7141 ------------------------------
7142 -- Has_New_Non_Standard_Rep --
7143 ------------------------------
7145 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7147 if not Is_Derived_Type (T) then
7148 return Has_Non_Standard_Rep (T)
7149 or else Has_Non_Standard_Rep (Root_Type (T));
7151 -- If Has_Non_Standard_Rep is not set on the derived type, the
7152 -- representation is fully inherited.
7154 elsif not Has_Non_Standard_Rep (T) then
7158 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7160 -- May need a more precise check here: the First_Rep_Item may
7161 -- be a stream attribute, which does not affect the representation
7164 end Has_New_Non_Standard_Rep;
7170 function In_Runtime (E : Entity_Id) return Boolean is
7175 while Scope (S1) /= Standard_Standard loop
7179 return Is_RTU (S1, System) or else Is_RTU (S1, Ada);
7182 ----------------------------
7183 -- Initialization_Warning --
7184 ----------------------------
7186 procedure Initialization_Warning (E : Entity_Id) is
7187 Warning_Needed : Boolean;
7190 Warning_Needed := False;
7192 if Ekind (Current_Scope) = E_Package
7193 and then Static_Elaboration_Desired (Current_Scope)
7196 if Is_Record_Type (E) then
7197 if Has_Discriminants (E)
7198 or else Is_Limited_Type (E)
7199 or else Has_Non_Standard_Rep (E)
7201 Warning_Needed := True;
7204 -- Verify that at least one component has an initialization
7205 -- expression. No need for a warning on a type if all its
7206 -- components have no initialization.
7212 Comp := First_Component (E);
7213 while Present (Comp) loop
7214 if Ekind (Comp) = E_Discriminant
7216 (Nkind (Parent (Comp)) = N_Component_Declaration
7217 and then Present (Expression (Parent (Comp))))
7219 Warning_Needed := True;
7223 Next_Component (Comp);
7228 if Warning_Needed then
7230 ("Objects of the type cannot be initialized " &
7231 "statically by default?",
7237 Error_Msg_N ("Object cannot be initialized statically?", E);
7240 end Initialization_Warning;
7246 function Init_Formals (Typ : Entity_Id) return List_Id is
7247 Loc : constant Source_Ptr := Sloc (Typ);
7251 -- First parameter is always _Init : in out typ. Note that we need
7252 -- this to be in/out because in the case of the task record value,
7253 -- there are default record fields (_Priority, _Size, -Task_Info)
7254 -- that may be referenced in the generated initialization routine.
7256 Formals := New_List (
7257 Make_Parameter_Specification (Loc,
7258 Defining_Identifier =>
7259 Make_Defining_Identifier (Loc, Name_uInit),
7261 Out_Present => True,
7262 Parameter_Type => New_Reference_To (Typ, Loc)));
7264 -- For task record value, or type that contains tasks, add two more
7265 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7266 -- We also add these parameters for the task record type case.
7269 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7272 Make_Parameter_Specification (Loc,
7273 Defining_Identifier =>
7274 Make_Defining_Identifier (Loc, Name_uMaster),
7275 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
7278 Make_Parameter_Specification (Loc,
7279 Defining_Identifier =>
7280 Make_Defining_Identifier (Loc, Name_uChain),
7282 Out_Present => True,
7284 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
7287 Make_Parameter_Specification (Loc,
7288 Defining_Identifier =>
7289 Make_Defining_Identifier (Loc, Name_uTask_Name),
7292 New_Reference_To (Standard_String, Loc)));
7298 when RE_Not_Available =>
7302 -------------------------
7303 -- Init_Secondary_Tags --
7304 -------------------------
7306 procedure Init_Secondary_Tags
7309 Stmts_List : List_Id;
7310 Fixed_Comps : Boolean := True;
7311 Variable_Comps : Boolean := True)
7313 Loc : constant Source_Ptr := Sloc (Target);
7315 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
7316 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7318 procedure Initialize_Tag
7321 Tag_Comp : Entity_Id;
7322 Iface_Tag : Node_Id);
7323 -- Initialize the tag of the secondary dispatch table of Typ associated
7324 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7325 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
7326 -- of Typ CPP tagged type we generate code to inherit the contents of
7327 -- the dispatch table directly from the ancestor.
7329 --------------------
7330 -- Initialize_Tag --
7331 --------------------
7333 procedure Initialize_Tag
7336 Tag_Comp : Entity_Id;
7337 Iface_Tag : Node_Id)
7339 Comp_Typ : Entity_Id;
7340 Offset_To_Top_Comp : Entity_Id := Empty;
7343 -- Initialize the pointer to the secondary DT associated with the
7346 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7347 Append_To (Stmts_List,
7348 Make_Assignment_Statement (Loc,
7350 Make_Selected_Component (Loc,
7351 Prefix => New_Copy_Tree (Target),
7352 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7354 New_Reference_To (Iface_Tag, Loc)));
7357 Comp_Typ := Scope (Tag_Comp);
7359 -- Initialize the entries of the table of interfaces. We generate a
7360 -- different call when the parent of the type has variable size
7363 if Comp_Typ /= Etype (Comp_Typ)
7364 and then Is_Variable_Size_Record (Etype (Comp_Typ))
7365 and then Chars (Tag_Comp) /= Name_uTag
7367 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7369 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
7370 -- configurable run-time environment.
7372 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7374 ("variable size record with interface types", Typ);
7379 -- Set_Dynamic_Offset_To_Top
7381 -- Interface_T => Iface'Tag,
7382 -- Offset_Value => n,
7383 -- Offset_Func => Fn'Address)
7385 Append_To (Stmts_List,
7386 Make_Procedure_Call_Statement (Loc,
7387 Name => New_Reference_To
7388 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7389 Parameter_Associations => New_List (
7390 Make_Attribute_Reference (Loc,
7391 Prefix => New_Copy_Tree (Target),
7392 Attribute_Name => Name_Address),
7394 Unchecked_Convert_To (RTE (RE_Tag),
7396 (Node (First_Elmt (Access_Disp_Table (Iface))),
7399 Unchecked_Convert_To
7400 (RTE (RE_Storage_Offset),
7401 Make_Attribute_Reference (Loc,
7403 Make_Selected_Component (Loc,
7404 Prefix => New_Copy_Tree (Target),
7406 New_Reference_To (Tag_Comp, Loc)),
7407 Attribute_Name => Name_Position)),
7409 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7410 Make_Attribute_Reference (Loc,
7411 Prefix => New_Reference_To
7412 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7413 Attribute_Name => Name_Address)))));
7415 -- In this case the next component stores the value of the
7416 -- offset to the top.
7418 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7419 pragma Assert (Present (Offset_To_Top_Comp));
7421 Append_To (Stmts_List,
7422 Make_Assignment_Statement (Loc,
7424 Make_Selected_Component (Loc,
7425 Prefix => New_Copy_Tree (Target),
7426 Selector_Name => New_Reference_To
7427 (Offset_To_Top_Comp, Loc)),
7429 Make_Attribute_Reference (Loc,
7431 Make_Selected_Component (Loc,
7432 Prefix => New_Copy_Tree (Target),
7434 New_Reference_To (Tag_Comp, Loc)),
7435 Attribute_Name => Name_Position)));
7437 -- Normal case: No discriminants in the parent type
7440 -- Don't need to set any value if this interface shares
7441 -- the primary dispatch table.
7443 if not Is_Ancestor (Iface, Typ, Use_Full_View => True) then
7444 Append_To (Stmts_List,
7445 Build_Set_Static_Offset_To_Top (Loc,
7446 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
7448 Unchecked_Convert_To (RTE (RE_Storage_Offset),
7449 Make_Attribute_Reference (Loc,
7451 Make_Selected_Component (Loc,
7452 Prefix => New_Copy_Tree (Target),
7454 New_Reference_To (Tag_Comp, Loc)),
7455 Attribute_Name => Name_Position))));
7459 -- Register_Interface_Offset
7461 -- Interface_T => Iface'Tag,
7462 -- Is_Constant => True,
7463 -- Offset_Value => n,
7464 -- Offset_Func => null);
7466 if RTE_Available (RE_Register_Interface_Offset) then
7467 Append_To (Stmts_List,
7468 Make_Procedure_Call_Statement (Loc,
7469 Name => New_Reference_To
7470 (RTE (RE_Register_Interface_Offset), Loc),
7471 Parameter_Associations => New_List (
7472 Make_Attribute_Reference (Loc,
7473 Prefix => New_Copy_Tree (Target),
7474 Attribute_Name => Name_Address),
7476 Unchecked_Convert_To (RTE (RE_Tag),
7478 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
7480 New_Occurrence_Of (Standard_True, Loc),
7482 Unchecked_Convert_To
7483 (RTE (RE_Storage_Offset),
7484 Make_Attribute_Reference (Loc,
7486 Make_Selected_Component (Loc,
7487 Prefix => New_Copy_Tree (Target),
7489 New_Reference_To (Tag_Comp, Loc)),
7490 Attribute_Name => Name_Position)),
7499 Full_Typ : Entity_Id;
7500 Ifaces_List : Elist_Id;
7501 Ifaces_Comp_List : Elist_Id;
7502 Ifaces_Tag_List : Elist_Id;
7503 Iface_Elmt : Elmt_Id;
7504 Iface_Comp_Elmt : Elmt_Id;
7505 Iface_Tag_Elmt : Elmt_Id;
7507 In_Variable_Pos : Boolean;
7509 -- Start of processing for Init_Secondary_Tags
7512 -- Handle private types
7514 if Present (Full_View (Typ)) then
7515 Full_Typ := Full_View (Typ);
7520 Collect_Interfaces_Info
7521 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
7523 Iface_Elmt := First_Elmt (Ifaces_List);
7524 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
7525 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
7526 while Present (Iface_Elmt) loop
7527 Tag_Comp := Node (Iface_Comp_Elmt);
7529 -- Check if parent of record type has variable size components
7531 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
7532 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
7534 -- If we are compiling under the CPP full ABI compatibility mode and
7535 -- the ancestor is a CPP_Pragma tagged type then we generate code to
7536 -- initialize the secondary tag components from tags that reference
7537 -- secondary tables filled with copy of parent slots.
7539 if Is_CPP_Class (Root_Type (Full_Typ)) then
7541 -- Reject interface components located at variable offset in
7542 -- C++ derivations. This is currently unsupported.
7544 if not Fixed_Comps and then In_Variable_Pos then
7546 -- Locate the first dynamic component of the record. Done to
7547 -- improve the text of the warning.
7551 Comp_Typ : Entity_Id;
7554 Comp := First_Entity (Typ);
7555 while Present (Comp) loop
7556 Comp_Typ := Etype (Comp);
7558 if Ekind (Comp) /= E_Discriminant
7559 and then not Is_Tag (Comp)
7562 (Is_Record_Type (Comp_Typ)
7563 and then Is_Variable_Size_Record
7564 (Base_Type (Comp_Typ)))
7566 (Is_Array_Type (Comp_Typ)
7567 and then Is_Variable_Size_Array (Comp_Typ));
7573 pragma Assert (Present (Comp));
7574 Error_Msg_Node_2 := Comp;
7576 ("parent type & with dynamic component & cannot be parent"
7577 & " of 'C'P'P derivation if new interfaces are present",
7578 Typ, Scope (Original_Record_Component (Comp)));
7581 Sloc (Scope (Original_Record_Component (Comp)));
7583 ("type derived from 'C'P'P type & defined #",
7584 Typ, Scope (Original_Record_Component (Comp)));
7586 -- Avoid duplicated warnings
7591 -- Initialize secondary tags
7594 Append_To (Stmts_List,
7595 Make_Assignment_Statement (Loc,
7597 Make_Selected_Component (Loc,
7598 Prefix => New_Copy_Tree (Target),
7600 New_Reference_To (Node (Iface_Comp_Elmt), Loc)),
7602 New_Reference_To (Node (Iface_Tag_Elmt), Loc)));
7605 -- Otherwise generate code to initialize the tag
7608 if (In_Variable_Pos and then Variable_Comps)
7609 or else (not In_Variable_Pos and then Fixed_Comps)
7611 Initialize_Tag (Full_Typ,
7612 Iface => Node (Iface_Elmt),
7613 Tag_Comp => Tag_Comp,
7614 Iface_Tag => Node (Iface_Tag_Elmt));
7618 Next_Elmt (Iface_Elmt);
7619 Next_Elmt (Iface_Comp_Elmt);
7620 Next_Elmt (Iface_Tag_Elmt);
7622 end Init_Secondary_Tags;
7624 ----------------------------
7625 -- Is_Variable_Size_Array --
7626 ----------------------------
7628 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
7632 pragma Assert (Is_Array_Type (E));
7634 -- Check if some index is initialized with a non-constant value
7636 Idx := First_Index (E);
7637 while Present (Idx) loop
7638 if Nkind (Idx) = N_Range then
7639 if not Is_Constant_Bound (Low_Bound (Idx))
7640 or else not Is_Constant_Bound (High_Bound (Idx))
7646 Idx := Next_Index (Idx);
7650 end Is_Variable_Size_Array;
7652 -----------------------------
7653 -- Is_Variable_Size_Record --
7654 -----------------------------
7656 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7658 Comp_Typ : Entity_Id;
7661 pragma Assert (Is_Record_Type (E));
7663 Comp := First_Entity (E);
7664 while Present (Comp) loop
7665 Comp_Typ := Etype (Comp);
7667 -- Recursive call if the record type has discriminants
7669 if Is_Record_Type (Comp_Typ)
7670 and then Has_Discriminants (Comp_Typ)
7671 and then Is_Variable_Size_Record (Comp_Typ)
7675 elsif Is_Array_Type (Comp_Typ)
7676 and then Is_Variable_Size_Array (Comp_Typ)
7685 end Is_Variable_Size_Record;
7687 ----------------------------------------
7688 -- Make_Controlling_Function_Wrappers --
7689 ----------------------------------------
7691 procedure Make_Controlling_Function_Wrappers
7692 (Tag_Typ : Entity_Id;
7693 Decl_List : out List_Id;
7694 Body_List : out List_Id)
7696 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7697 Prim_Elmt : Elmt_Id;
7699 Actual_List : List_Id;
7700 Formal_List : List_Id;
7702 Par_Formal : Entity_Id;
7703 Formal_Node : Node_Id;
7704 Func_Body : Node_Id;
7705 Func_Decl : Node_Id;
7706 Func_Spec : Node_Id;
7707 Return_Stmt : Node_Id;
7710 Decl_List := New_List;
7711 Body_List := New_List;
7713 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7715 while Present (Prim_Elmt) loop
7716 Subp := Node (Prim_Elmt);
7718 -- If a primitive function with a controlling result of the type has
7719 -- not been overridden by the user, then we must create a wrapper
7720 -- function here that effectively overrides it and invokes the
7721 -- (non-abstract) parent function. This can only occur for a null
7722 -- extension. Note that functions with anonymous controlling access
7723 -- results don't qualify and must be overridden. We also exclude
7724 -- Input attributes, since each type will have its own version of
7725 -- Input constructed by the expander. The test for Comes_From_Source
7726 -- is needed to distinguish inherited operations from renamings
7727 -- (which also have Alias set).
7729 -- The function may be abstract, or require_Overriding may be set
7730 -- for it, because tests for null extensions may already have reset
7731 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7732 -- set, functions that need wrappers are recognized by having an
7733 -- alias that returns the parent type.
7735 if Comes_From_Source (Subp)
7736 or else No (Alias (Subp))
7737 or else Ekind (Subp) /= E_Function
7738 or else not Has_Controlling_Result (Subp)
7739 or else Is_Access_Type (Etype (Subp))
7740 or else Is_Abstract_Subprogram (Alias (Subp))
7741 or else Is_TSS (Subp, TSS_Stream_Input)
7745 elsif Is_Abstract_Subprogram (Subp)
7746 or else Requires_Overriding (Subp)
7748 (Is_Null_Extension (Etype (Subp))
7749 and then Etype (Alias (Subp)) /= Etype (Subp))
7751 Formal_List := No_List;
7752 Formal := First_Formal (Subp);
7754 if Present (Formal) then
7755 Formal_List := New_List;
7757 while Present (Formal) loop
7759 (Make_Parameter_Specification
7761 Defining_Identifier =>
7762 Make_Defining_Identifier (Sloc (Formal),
7763 Chars => Chars (Formal)),
7764 In_Present => In_Present (Parent (Formal)),
7765 Out_Present => Out_Present (Parent (Formal)),
7766 Null_Exclusion_Present =>
7767 Null_Exclusion_Present (Parent (Formal)),
7769 New_Reference_To (Etype (Formal), Loc),
7771 New_Copy_Tree (Expression (Parent (Formal)))),
7774 Next_Formal (Formal);
7779 Make_Function_Specification (Loc,
7780 Defining_Unit_Name =>
7781 Make_Defining_Identifier (Loc,
7782 Chars => Chars (Subp)),
7783 Parameter_Specifications => Formal_List,
7784 Result_Definition =>
7785 New_Reference_To (Etype (Subp), Loc));
7787 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7788 Append_To (Decl_List, Func_Decl);
7790 -- Build a wrapper body that calls the parent function. The body
7791 -- contains a single return statement that returns an extension
7792 -- aggregate whose ancestor part is a call to the parent function,
7793 -- passing the formals as actuals (with any controlling arguments
7794 -- converted to the types of the corresponding formals of the
7795 -- parent function, which might be anonymous access types), and
7796 -- having a null extension.
7798 Formal := First_Formal (Subp);
7799 Par_Formal := First_Formal (Alias (Subp));
7800 Formal_Node := First (Formal_List);
7802 if Present (Formal) then
7803 Actual_List := New_List;
7805 Actual_List := No_List;
7808 while Present (Formal) loop
7809 if Is_Controlling_Formal (Formal) then
7810 Append_To (Actual_List,
7811 Make_Type_Conversion (Loc,
7813 New_Occurrence_Of (Etype (Par_Formal), Loc),
7816 (Defining_Identifier (Formal_Node), Loc)));
7821 (Defining_Identifier (Formal_Node), Loc));
7824 Next_Formal (Formal);
7825 Next_Formal (Par_Formal);
7830 Make_Simple_Return_Statement (Loc,
7832 Make_Extension_Aggregate (Loc,
7834 Make_Function_Call (Loc,
7835 Name => New_Reference_To (Alias (Subp), Loc),
7836 Parameter_Associations => Actual_List),
7837 Null_Record_Present => True));
7840 Make_Subprogram_Body (Loc,
7841 Specification => New_Copy_Tree (Func_Spec),
7842 Declarations => Empty_List,
7843 Handled_Statement_Sequence =>
7844 Make_Handled_Sequence_Of_Statements (Loc,
7845 Statements => New_List (Return_Stmt)));
7847 Set_Defining_Unit_Name
7848 (Specification (Func_Body),
7849 Make_Defining_Identifier (Loc, Chars (Subp)));
7851 Append_To (Body_List, Func_Body);
7853 -- Replace the inherited function with the wrapper function
7854 -- in the primitive operations list.
7856 Override_Dispatching_Operation
7857 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7861 Next_Elmt (Prim_Elmt);
7863 end Make_Controlling_Function_Wrappers;
7869 function Make_Eq_Body
7871 Eq_Name : Name_Id) return Node_Id
7873 Loc : constant Source_Ptr := Sloc (Parent (Typ));
7875 Def : constant Node_Id := Parent (Typ);
7876 Stmts : constant List_Id := New_List;
7877 Variant_Case : Boolean := Has_Discriminants (Typ);
7878 Comps : Node_Id := Empty;
7879 Typ_Def : Node_Id := Type_Definition (Def);
7883 Predef_Spec_Or_Body (Loc,
7886 Profile => New_List (
7887 Make_Parameter_Specification (Loc,
7888 Defining_Identifier =>
7889 Make_Defining_Identifier (Loc, Name_X),
7890 Parameter_Type => New_Reference_To (Typ, Loc)),
7892 Make_Parameter_Specification (Loc,
7893 Defining_Identifier =>
7894 Make_Defining_Identifier (Loc, Name_Y),
7895 Parameter_Type => New_Reference_To (Typ, Loc))),
7897 Ret_Type => Standard_Boolean,
7900 if Variant_Case then
7901 if Nkind (Typ_Def) = N_Derived_Type_Definition then
7902 Typ_Def := Record_Extension_Part (Typ_Def);
7905 if Present (Typ_Def) then
7906 Comps := Component_List (Typ_Def);
7910 Present (Comps) and then Present (Variant_Part (Comps));
7913 if Variant_Case then
7915 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
7916 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
7918 Make_Simple_Return_Statement (Loc,
7919 Expression => New_Reference_To (Standard_True, Loc)));
7923 Make_Simple_Return_Statement (Loc,
7925 Expand_Record_Equality
7928 Lhs => Make_Identifier (Loc, Name_X),
7929 Rhs => Make_Identifier (Loc, Name_Y),
7930 Bodies => Declarations (Decl))));
7933 Set_Handled_Statement_Sequence
7934 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7942 -- <Make_Eq_If shared components>
7944 -- when V1 => <Make_Eq_Case> on subcomponents
7946 -- when Vn => <Make_Eq_Case> on subcomponents
7949 function Make_Eq_Case
7952 Discr : Entity_Id := Empty) return List_Id
7954 Loc : constant Source_Ptr := Sloc (E);
7955 Result : constant List_Id := New_List;
7960 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7962 if No (Variant_Part (CL)) then
7966 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7968 if No (Variant) then
7972 Alt_List := New_List;
7974 while Present (Variant) loop
7975 Append_To (Alt_List,
7976 Make_Case_Statement_Alternative (Loc,
7977 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7978 Statements => Make_Eq_Case (E, Component_List (Variant))));
7980 Next_Non_Pragma (Variant);
7983 -- If we have an Unchecked_Union, use one of the parameters that
7984 -- captures the discriminants.
7986 if Is_Unchecked_Union (E) then
7988 Make_Case_Statement (Loc,
7989 Expression => New_Reference_To (Discr, Loc),
7990 Alternatives => Alt_List));
7994 Make_Case_Statement (Loc,
7996 Make_Selected_Component (Loc,
7997 Prefix => Make_Identifier (Loc, Name_X),
7998 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7999 Alternatives => Alt_List));
8020 -- or a null statement if the list L is empty
8024 L : List_Id) return Node_Id
8026 Loc : constant Source_Ptr := Sloc (E);
8028 Field_Name : Name_Id;
8033 return Make_Null_Statement (Loc);
8038 C := First_Non_Pragma (L);
8039 while Present (C) loop
8040 Field_Name := Chars (Defining_Identifier (C));
8042 -- The tags must not be compared: they are not part of the value.
8043 -- Ditto for parent interfaces because their equality operator is
8046 -- Note also that in the following, we use Make_Identifier for
8047 -- the component names. Use of New_Reference_To to identify the
8048 -- components would be incorrect because the wrong entities for
8049 -- discriminants could be picked up in the private type case.
8051 if Field_Name = Name_uParent
8052 and then Is_Interface (Etype (Defining_Identifier (C)))
8056 elsif Field_Name /= Name_uTag then
8057 Evolve_Or_Else (Cond,
8060 Make_Selected_Component (Loc,
8061 Prefix => Make_Identifier (Loc, Name_X),
8062 Selector_Name => Make_Identifier (Loc, Field_Name)),
8065 Make_Selected_Component (Loc,
8066 Prefix => Make_Identifier (Loc, Name_Y),
8067 Selector_Name => Make_Identifier (Loc, Field_Name))));
8070 Next_Non_Pragma (C);
8074 return Make_Null_Statement (Loc);
8078 Make_Implicit_If_Statement (E,
8080 Then_Statements => New_List (
8081 Make_Simple_Return_Statement (Loc,
8082 Expression => New_Occurrence_Of (Standard_False, Loc))));
8087 -------------------------------
8088 -- Make_Null_Procedure_Specs --
8089 -------------------------------
8091 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8092 Decl_List : constant List_Id := New_List;
8093 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8095 Formal_List : List_Id;
8096 New_Param_Spec : Node_Id;
8097 Parent_Subp : Entity_Id;
8098 Prim_Elmt : Elmt_Id;
8102 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8103 while Present (Prim_Elmt) loop
8104 Subp := Node (Prim_Elmt);
8106 -- If a null procedure inherited from an interface has not been
8107 -- overridden, then we build a null procedure declaration to
8108 -- override the inherited procedure.
8110 Parent_Subp := Alias (Subp);
8112 if Present (Parent_Subp)
8113 and then Is_Null_Interface_Primitive (Parent_Subp)
8115 Formal_List := No_List;
8116 Formal := First_Formal (Subp);
8118 if Present (Formal) then
8119 Formal_List := New_List;
8121 while Present (Formal) loop
8123 -- Copy the parameter spec including default expressions
8126 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
8128 -- Generate a new defining identifier for the new formal.
8129 -- required because New_Copy_Tree does not duplicate
8130 -- semantic fields (except itypes).
8132 Set_Defining_Identifier (New_Param_Spec,
8133 Make_Defining_Identifier (Sloc (Formal),
8134 Chars => Chars (Formal)));
8136 -- For controlling arguments we must change their
8137 -- parameter type to reference the tagged type (instead
8138 -- of the interface type)
8140 if Is_Controlling_Formal (Formal) then
8141 if Nkind (Parameter_Type (Parent (Formal)))
8144 Set_Parameter_Type (New_Param_Spec,
8145 New_Occurrence_Of (Tag_Typ, Loc));
8148 (Nkind (Parameter_Type (Parent (Formal)))
8149 = N_Access_Definition);
8150 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
8151 New_Occurrence_Of (Tag_Typ, Loc));
8155 Append (New_Param_Spec, Formal_List);
8157 Next_Formal (Formal);
8161 Append_To (Decl_List,
8162 Make_Subprogram_Declaration (Loc,
8163 Make_Procedure_Specification (Loc,
8164 Defining_Unit_Name =>
8165 Make_Defining_Identifier (Loc, Chars (Subp)),
8166 Parameter_Specifications => Formal_List,
8167 Null_Present => True)));
8170 Next_Elmt (Prim_Elmt);
8174 end Make_Null_Procedure_Specs;
8176 -------------------------------------
8177 -- Make_Predefined_Primitive_Specs --
8178 -------------------------------------
8180 procedure Make_Predefined_Primitive_Specs
8181 (Tag_Typ : Entity_Id;
8182 Predef_List : out List_Id;
8183 Renamed_Eq : out Entity_Id)
8185 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8186 Res : constant List_Id := New_List;
8187 Eq_Name : Name_Id := Name_Op_Eq;
8188 Eq_Needed : Boolean;
8192 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
8193 -- Returns true if Prim is a renaming of an unresolved predefined
8194 -- equality operation.
8196 -------------------------------
8197 -- Is_Predefined_Eq_Renaming --
8198 -------------------------------
8200 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
8202 return Chars (Prim) /= Name_Op_Eq
8203 and then Present (Alias (Prim))
8204 and then Comes_From_Source (Prim)
8205 and then Is_Intrinsic_Subprogram (Alias (Prim))
8206 and then Chars (Alias (Prim)) = Name_Op_Eq;
8207 end Is_Predefined_Eq_Renaming;
8209 -- Start of processing for Make_Predefined_Primitive_Specs
8212 Renamed_Eq := Empty;
8216 Append_To (Res, Predef_Spec_Or_Body (Loc,
8219 Profile => New_List (
8220 Make_Parameter_Specification (Loc,
8221 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8222 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8224 Ret_Type => Standard_Long_Long_Integer));
8226 -- Spec of _Alignment
8228 Append_To (Res, Predef_Spec_Or_Body (Loc,
8230 Name => Name_uAlignment,
8231 Profile => New_List (
8232 Make_Parameter_Specification (Loc,
8233 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8234 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8236 Ret_Type => Standard_Integer));
8238 -- Specs for dispatching stream attributes
8241 Stream_Op_TSS_Names :
8242 constant array (Integer range <>) of TSS_Name_Type :=
8249 for Op in Stream_Op_TSS_Names'Range loop
8250 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
8252 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
8253 Stream_Op_TSS_Names (Op)));
8258 -- Spec of "=" is expanded if the type is not limited and if a
8259 -- user defined "=" was not already declared for the non-full
8260 -- view of a private extension
8262 if not Is_Limited_Type (Tag_Typ) then
8264 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8265 while Present (Prim) loop
8267 -- If a primitive is encountered that renames the predefined
8268 -- equality operator before reaching any explicit equality
8269 -- primitive, then we still need to create a predefined equality
8270 -- function, because calls to it can occur via the renaming. A new
8271 -- name is created for the equality to avoid conflicting with any
8272 -- user-defined equality. (Note that this doesn't account for
8273 -- renamings of equality nested within subpackages???)
8275 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8276 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
8278 -- User-defined equality
8280 elsif Chars (Node (Prim)) = Name_Op_Eq
8281 and then Etype (First_Formal (Node (Prim))) =
8282 Etype (Next_Formal (First_Formal (Node (Prim))))
8283 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
8285 if No (Alias (Node (Prim)))
8286 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
8287 N_Subprogram_Renaming_Declaration
8292 -- If the parent is not an interface type and has an abstract
8293 -- equality function, the inherited equality is abstract as
8294 -- well, and no body can be created for it.
8296 elsif not Is_Interface (Etype (Tag_Typ))
8297 and then Present (Alias (Node (Prim)))
8298 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
8303 -- If the type has an equality function corresponding with
8304 -- a primitive defined in an interface type, the inherited
8305 -- equality is abstract as well, and no body can be created
8308 elsif Present (Alias (Node (Prim)))
8309 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
8312 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
8322 -- If a renaming of predefined equality was found but there was no
8323 -- user-defined equality (so Eq_Needed is still true), then set the
8324 -- name back to Name_Op_Eq. But in the case where a user-defined
8325 -- equality was located after such a renaming, then the predefined
8326 -- equality function is still needed, so Eq_Needed must be set back
8329 if Eq_Name /= Name_Op_Eq then
8331 Eq_Name := Name_Op_Eq;
8338 Eq_Spec := Predef_Spec_Or_Body (Loc,
8341 Profile => New_List (
8342 Make_Parameter_Specification (Loc,
8343 Defining_Identifier =>
8344 Make_Defining_Identifier (Loc, Name_X),
8345 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8346 Make_Parameter_Specification (Loc,
8347 Defining_Identifier =>
8348 Make_Defining_Identifier (Loc, Name_Y),
8349 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8350 Ret_Type => Standard_Boolean);
8351 Append_To (Res, Eq_Spec);
8353 if Eq_Name /= Name_Op_Eq then
8354 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
8356 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8357 while Present (Prim) loop
8359 -- Any renamings of equality that appeared before an
8360 -- overriding equality must be updated to refer to the
8361 -- entity for the predefined equality, otherwise calls via
8362 -- the renaming would get incorrectly resolved to call the
8363 -- user-defined equality function.
8365 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8366 Set_Alias (Node (Prim), Renamed_Eq);
8368 -- Exit upon encountering a user-defined equality
8370 elsif Chars (Node (Prim)) = Name_Op_Eq
8371 and then No (Alias (Node (Prim)))
8381 -- Spec for dispatching assignment
8383 Append_To (Res, Predef_Spec_Or_Body (Loc,
8385 Name => Name_uAssign,
8386 Profile => New_List (
8387 Make_Parameter_Specification (Loc,
8388 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8389 Out_Present => True,
8390 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8392 Make_Parameter_Specification (Loc,
8393 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8394 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
8397 -- Ada 2005: Generate declarations for the following primitive
8398 -- operations for limited interfaces and synchronized types that
8399 -- implement a limited interface.
8401 -- Disp_Asynchronous_Select
8402 -- Disp_Conditional_Select
8403 -- Disp_Get_Prim_Op_Kind
8406 -- Disp_Timed_Select
8408 -- Disable the generation of these bodies if No_Dispatching_Calls,
8409 -- Ravenscar or ZFP is active.
8411 if Ada_Version >= Ada_2005
8412 and then not Restriction_Active (No_Dispatching_Calls)
8413 and then not Restriction_Active (No_Select_Statements)
8414 and then RTE_Available (RE_Select_Specific_Data)
8416 -- These primitives are defined abstract in interface types
8418 if Is_Interface (Tag_Typ)
8419 and then Is_Limited_Record (Tag_Typ)
8422 Make_Abstract_Subprogram_Declaration (Loc,
8424 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8427 Make_Abstract_Subprogram_Declaration (Loc,
8429 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8432 Make_Abstract_Subprogram_Declaration (Loc,
8434 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8437 Make_Abstract_Subprogram_Declaration (Loc,
8439 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8442 Make_Abstract_Subprogram_Declaration (Loc,
8444 Make_Disp_Requeue_Spec (Tag_Typ)));
8447 Make_Abstract_Subprogram_Declaration (Loc,
8449 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8451 -- If the ancestor is an interface type we declare non-abstract
8452 -- primitives to override the abstract primitives of the interface
8455 -- In VM targets we define these primitives in all root tagged types
8456 -- that are not interface types. Done because in VM targets we don't
8457 -- have secondary dispatch tables and any derivation of Tag_Typ may
8458 -- cover limited interfaces (which always have these primitives since
8459 -- they may be ancestors of synchronized interface types).
8461 elsif (not Is_Interface (Tag_Typ)
8462 and then Is_Interface (Etype (Tag_Typ))
8463 and then Is_Limited_Record (Etype (Tag_Typ)))
8465 (Is_Concurrent_Record_Type (Tag_Typ)
8466 and then Has_Interfaces (Tag_Typ))
8468 (not Tagged_Type_Expansion
8469 and then not Is_Interface (Tag_Typ)
8470 and then Tag_Typ = Root_Type (Tag_Typ))
8473 Make_Subprogram_Declaration (Loc,
8475 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8478 Make_Subprogram_Declaration (Loc,
8480 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8483 Make_Subprogram_Declaration (Loc,
8485 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8488 Make_Subprogram_Declaration (Loc,
8490 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8493 Make_Subprogram_Declaration (Loc,
8495 Make_Disp_Requeue_Spec (Tag_Typ)));
8498 Make_Subprogram_Declaration (Loc,
8500 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8504 -- All tagged types receive their own Deep_Adjust and Deep_Finalize
8505 -- regardless of whether they are controlled or may contain controlled
8508 -- Do not generate the routines if finalization is disabled
8510 if Restriction_Active (No_Finalization) then
8513 -- Finalization is not available for CIL value types
8515 elsif Is_Value_Type (Tag_Typ) then
8519 if not Is_Limited_Type (Tag_Typ) then
8520 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
8523 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
8527 end Make_Predefined_Primitive_Specs;
8529 ---------------------------------
8530 -- Needs_Simple_Initialization --
8531 ---------------------------------
8533 function Needs_Simple_Initialization
8535 Consider_IS : Boolean := True) return Boolean
8537 Consider_IS_NS : constant Boolean :=
8539 or (Initialize_Scalars and Consider_IS);
8542 -- Never need initialization if it is suppressed
8544 if Initialization_Suppressed (T) then
8548 -- Check for private type, in which case test applies to the underlying
8549 -- type of the private type.
8551 if Is_Private_Type (T) then
8553 RT : constant Entity_Id := Underlying_Type (T);
8556 if Present (RT) then
8557 return Needs_Simple_Initialization (RT);
8563 -- Scalar type with Default_Value aspect requires initialization
8565 elsif Is_Scalar_Type (T) and then Has_Default_Aspect (T) then
8568 -- Cases needing simple initialization are access types, and, if pragma
8569 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
8572 elsif Is_Access_Type (T)
8573 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
8577 -- If Initialize/Normalize_Scalars is in effect, string objects also
8578 -- need initialization, unless they are created in the course of
8579 -- expanding an aggregate (since in the latter case they will be
8580 -- filled with appropriate initializing values before they are used).
8582 elsif Consider_IS_NS
8584 (Root_Type (T) = Standard_String
8585 or else Root_Type (T) = Standard_Wide_String
8586 or else Root_Type (T) = Standard_Wide_Wide_String)
8589 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
8596 end Needs_Simple_Initialization;
8598 ----------------------
8599 -- Predef_Deep_Spec --
8600 ----------------------
8602 function Predef_Deep_Spec
8604 Tag_Typ : Entity_Id;
8605 Name : TSS_Name_Type;
8606 For_Body : Boolean := False) return Node_Id
8611 -- V : in out Tag_Typ
8613 Formals := New_List (
8614 Make_Parameter_Specification (Loc,
8615 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8617 Out_Present => True,
8618 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
8620 -- F : Boolean := True
8622 if Name = TSS_Deep_Adjust
8623 or else Name = TSS_Deep_Finalize
8626 Make_Parameter_Specification (Loc,
8627 Defining_Identifier => Make_Defining_Identifier (Loc, Name_F),
8628 Parameter_Type => New_Reference_To (Standard_Boolean, Loc),
8629 Expression => New_Reference_To (Standard_True, Loc)));
8633 Predef_Spec_Or_Body (Loc,
8634 Name => Make_TSS_Name (Tag_Typ, Name),
8637 For_Body => For_Body);
8640 when RE_Not_Available =>
8642 end Predef_Deep_Spec;
8644 -------------------------
8645 -- Predef_Spec_Or_Body --
8646 -------------------------
8648 function Predef_Spec_Or_Body
8650 Tag_Typ : Entity_Id;
8653 Ret_Type : Entity_Id := Empty;
8654 For_Body : Boolean := False) return Node_Id
8656 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
8660 Set_Is_Public (Id, Is_Public (Tag_Typ));
8662 -- The internal flag is set to mark these declarations because they have
8663 -- specific properties. First, they are primitives even if they are not
8664 -- defined in the type scope (the freezing point is not necessarily in
8665 -- the same scope). Second, the predefined equality can be overridden by
8666 -- a user-defined equality, no body will be generated in this case.
8668 Set_Is_Internal (Id);
8670 if not Debug_Generated_Code then
8671 Set_Debug_Info_Off (Id);
8674 if No (Ret_Type) then
8676 Make_Procedure_Specification (Loc,
8677 Defining_Unit_Name => Id,
8678 Parameter_Specifications => Profile);
8681 Make_Function_Specification (Loc,
8682 Defining_Unit_Name => Id,
8683 Parameter_Specifications => Profile,
8684 Result_Definition => New_Reference_To (Ret_Type, Loc));
8687 if Is_Interface (Tag_Typ) then
8688 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8690 -- If body case, return empty subprogram body. Note that this is ill-
8691 -- formed, because there is not even a null statement, and certainly not
8692 -- a return in the function case. The caller is expected to do surgery
8693 -- on the body to add the appropriate stuff.
8696 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8698 -- For the case of an Input attribute predefined for an abstract type,
8699 -- generate an abstract specification. This will never be called, but we
8700 -- need the slot allocated in the dispatching table so that attributes
8701 -- typ'Class'Input and typ'Class'Output will work properly.
8703 elsif Is_TSS (Name, TSS_Stream_Input)
8704 and then Is_Abstract_Type (Tag_Typ)
8706 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8708 -- Normal spec case, where we return a subprogram declaration
8711 return Make_Subprogram_Declaration (Loc, Spec);
8713 end Predef_Spec_Or_Body;
8715 -----------------------------
8716 -- Predef_Stream_Attr_Spec --
8717 -----------------------------
8719 function Predef_Stream_Attr_Spec
8721 Tag_Typ : Entity_Id;
8722 Name : TSS_Name_Type;
8723 For_Body : Boolean := False) return Node_Id
8725 Ret_Type : Entity_Id;
8728 if Name = TSS_Stream_Input then
8729 Ret_Type := Tag_Typ;
8737 Name => Make_TSS_Name (Tag_Typ, Name),
8739 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8740 Ret_Type => Ret_Type,
8741 For_Body => For_Body);
8742 end Predef_Stream_Attr_Spec;
8744 ---------------------------------
8745 -- Predefined_Primitive_Bodies --
8746 ---------------------------------
8748 function Predefined_Primitive_Bodies
8749 (Tag_Typ : Entity_Id;
8750 Renamed_Eq : Entity_Id) return List_Id
8752 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8753 Res : constant List_Id := New_List;
8756 Eq_Needed : Boolean;
8760 pragma Warnings (Off, Ent);
8763 pragma Assert (not Is_Interface (Tag_Typ));
8765 -- See if we have a predefined "=" operator
8767 if Present (Renamed_Eq) then
8769 Eq_Name := Chars (Renamed_Eq);
8771 -- If the parent is an interface type then it has defined all the
8772 -- predefined primitives abstract and we need to check if the type
8773 -- has some user defined "=" function to avoid generating it.
8775 elsif Is_Interface (Etype (Tag_Typ)) then
8777 Eq_Name := Name_Op_Eq;
8779 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8780 while Present (Prim) loop
8781 if Chars (Node (Prim)) = Name_Op_Eq
8782 and then not Is_Internal (Node (Prim))
8796 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8797 while Present (Prim) loop
8798 if Chars (Node (Prim)) = Name_Op_Eq
8799 and then Is_Internal (Node (Prim))
8802 Eq_Name := Name_Op_Eq;
8810 -- Body of _Alignment
8812 Decl := Predef_Spec_Or_Body (Loc,
8814 Name => Name_uAlignment,
8815 Profile => New_List (
8816 Make_Parameter_Specification (Loc,
8817 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8818 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8820 Ret_Type => Standard_Integer,
8823 Set_Handled_Statement_Sequence (Decl,
8824 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8825 Make_Simple_Return_Statement (Loc,
8827 Make_Attribute_Reference (Loc,
8828 Prefix => Make_Identifier (Loc, Name_X),
8829 Attribute_Name => Name_Alignment)))));
8831 Append_To (Res, Decl);
8835 Decl := Predef_Spec_Or_Body (Loc,
8838 Profile => New_List (
8839 Make_Parameter_Specification (Loc,
8840 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8841 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8843 Ret_Type => Standard_Long_Long_Integer,
8846 Set_Handled_Statement_Sequence (Decl,
8847 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8848 Make_Simple_Return_Statement (Loc,
8850 Make_Attribute_Reference (Loc,
8851 Prefix => Make_Identifier (Loc, Name_X),
8852 Attribute_Name => Name_Size)))));
8854 Append_To (Res, Decl);
8856 -- Bodies for Dispatching stream IO routines. We need these only for
8857 -- non-limited types (in the limited case there is no dispatching).
8858 -- We also skip them if dispatching or finalization are not available.
8860 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8861 and then No (TSS (Tag_Typ, TSS_Stream_Read))
8863 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8864 Append_To (Res, Decl);
8867 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8868 and then No (TSS (Tag_Typ, TSS_Stream_Write))
8870 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8871 Append_To (Res, Decl);
8874 -- Skip body of _Input for the abstract case, since the corresponding
8875 -- spec is abstract (see Predef_Spec_Or_Body).
8877 if not Is_Abstract_Type (Tag_Typ)
8878 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8879 and then No (TSS (Tag_Typ, TSS_Stream_Input))
8881 Build_Record_Or_Elementary_Input_Function
8882 (Loc, Tag_Typ, Decl, Ent);
8883 Append_To (Res, Decl);
8886 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8887 and then No (TSS (Tag_Typ, TSS_Stream_Output))
8889 Build_Record_Or_Elementary_Output_Procedure
8890 (Loc, Tag_Typ, Decl, Ent);
8891 Append_To (Res, Decl);
8894 -- Ada 2005: Generate bodies for the following primitive operations for
8895 -- limited interfaces and synchronized types that implement a limited
8898 -- disp_asynchronous_select
8899 -- disp_conditional_select
8900 -- disp_get_prim_op_kind
8902 -- disp_timed_select
8904 -- The interface versions will have null bodies
8906 -- Disable the generation of these bodies if No_Dispatching_Calls,
8907 -- Ravenscar or ZFP is active.
8909 -- In VM targets we define these primitives in all root tagged types
8910 -- that are not interface types. Done because in VM targets we don't
8911 -- have secondary dispatch tables and any derivation of Tag_Typ may
8912 -- cover limited interfaces (which always have these primitives since
8913 -- they may be ancestors of synchronized interface types).
8915 if Ada_Version >= Ada_2005
8916 and then not Is_Interface (Tag_Typ)
8918 ((Is_Interface (Etype (Tag_Typ))
8919 and then Is_Limited_Record (Etype (Tag_Typ)))
8921 (Is_Concurrent_Record_Type (Tag_Typ)
8922 and then Has_Interfaces (Tag_Typ))
8924 (not Tagged_Type_Expansion
8925 and then Tag_Typ = Root_Type (Tag_Typ)))
8926 and then not Restriction_Active (No_Dispatching_Calls)
8927 and then not Restriction_Active (No_Select_Statements)
8928 and then RTE_Available (RE_Select_Specific_Data)
8930 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8931 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
8932 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
8933 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
8934 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
8935 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
8938 if not Is_Limited_Type (Tag_Typ)
8939 and then not Is_Interface (Tag_Typ)
8941 -- Body for equality
8944 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
8945 Append_To (Res, Decl);
8948 -- Body for dispatching assignment
8951 Predef_Spec_Or_Body (Loc,
8953 Name => Name_uAssign,
8954 Profile => New_List (
8955 Make_Parameter_Specification (Loc,
8956 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8957 Out_Present => True,
8958 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8960 Make_Parameter_Specification (Loc,
8961 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8962 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8965 Set_Handled_Statement_Sequence (Decl,
8966 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8967 Make_Assignment_Statement (Loc,
8968 Name => Make_Identifier (Loc, Name_X),
8969 Expression => Make_Identifier (Loc, Name_Y)))));
8971 Append_To (Res, Decl);
8974 -- Generate empty bodies of routines Deep_Adjust and Deep_Finalize for
8975 -- tagged types which do not contain controlled components.
8977 -- Do not generate the routines if finalization is disabled
8979 if Restriction_Active (No_Finalization) then
8982 elsif not Has_Controlled_Component (Tag_Typ) then
8983 if not Is_Limited_Type (Tag_Typ) then
8984 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8986 if Is_Controlled (Tag_Typ) then
8987 Set_Handled_Statement_Sequence (Decl,
8988 Make_Handled_Sequence_Of_Statements (Loc,
8989 Statements => New_List (
8991 Obj_Ref => Make_Identifier (Loc, Name_V),
8994 Set_Handled_Statement_Sequence (Decl,
8995 Make_Handled_Sequence_Of_Statements (Loc,
8996 Statements => New_List (
8997 Make_Null_Statement (Loc))));
9000 Append_To (Res, Decl);
9003 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
9005 if Is_Controlled (Tag_Typ) then
9006 Set_Handled_Statement_Sequence (Decl,
9007 Make_Handled_Sequence_Of_Statements (Loc,
9008 Statements => New_List (
9010 (Obj_Ref => Make_Identifier (Loc, Name_V),
9013 Set_Handled_Statement_Sequence (Decl,
9014 Make_Handled_Sequence_Of_Statements (Loc,
9015 Statements => New_List (Make_Null_Statement (Loc))));
9018 Append_To (Res, Decl);
9022 end Predefined_Primitive_Bodies;
9024 ---------------------------------
9025 -- Predefined_Primitive_Freeze --
9026 ---------------------------------
9028 function Predefined_Primitive_Freeze
9029 (Tag_Typ : Entity_Id) return List_Id
9031 Res : constant List_Id := New_List;
9036 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
9037 while Present (Prim) loop
9038 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
9039 Frnodes := Freeze_Entity (Node (Prim), Tag_Typ);
9041 if Present (Frnodes) then
9042 Append_List_To (Res, Frnodes);
9050 end Predefined_Primitive_Freeze;
9052 -------------------------
9053 -- Stream_Operation_OK --
9054 -------------------------
9056 function Stream_Operation_OK
9058 Operation : TSS_Name_Type) return Boolean
9060 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
9063 -- Special case of a limited type extension: a default implementation
9064 -- of the stream attributes Read or Write exists if that attribute
9065 -- has been specified or is available for an ancestor type; a default
9066 -- implementation of the attribute Output (resp. Input) exists if the
9067 -- attribute has been specified or Write (resp. Read) is available for
9068 -- an ancestor type. The last condition only applies under Ada 2005.
9070 if Is_Limited_Type (Typ)
9071 and then Is_Tagged_Type (Typ)
9073 if Operation = TSS_Stream_Read then
9074 Has_Predefined_Or_Specified_Stream_Attribute :=
9075 Has_Specified_Stream_Read (Typ);
9077 elsif Operation = TSS_Stream_Write then
9078 Has_Predefined_Or_Specified_Stream_Attribute :=
9079 Has_Specified_Stream_Write (Typ);
9081 elsif Operation = TSS_Stream_Input then
9082 Has_Predefined_Or_Specified_Stream_Attribute :=
9083 Has_Specified_Stream_Input (Typ)
9085 (Ada_Version >= Ada_2005
9086 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
9088 elsif Operation = TSS_Stream_Output then
9089 Has_Predefined_Or_Specified_Stream_Attribute :=
9090 Has_Specified_Stream_Output (Typ)
9092 (Ada_Version >= Ada_2005
9093 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
9096 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
9098 if not Has_Predefined_Or_Specified_Stream_Attribute
9099 and then Is_Derived_Type (Typ)
9100 and then (Operation = TSS_Stream_Read
9101 or else Operation = TSS_Stream_Write)
9103 Has_Predefined_Or_Specified_Stream_Attribute :=
9105 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
9109 -- If the type is not limited, or else is limited but the attribute is
9110 -- explicitly specified or is predefined for the type, then return True,
9111 -- unless other conditions prevail, such as restrictions prohibiting
9112 -- streams or dispatching operations. We also return True for limited
9113 -- interfaces, because they may be extended by nonlimited types and
9114 -- permit inheritance in this case (addresses cases where an abstract
9115 -- extension doesn't get 'Input declared, as per comments below, but
9116 -- 'Class'Input must still be allowed). Note that attempts to apply
9117 -- stream attributes to a limited interface or its class-wide type
9118 -- (or limited extensions thereof) will still get properly rejected
9119 -- by Check_Stream_Attribute.
9121 -- We exclude the Input operation from being a predefined subprogram in
9122 -- the case where the associated type is an abstract extension, because
9123 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
9124 -- we don't want an abstract version created because types derived from
9125 -- the abstract type may not even have Input available (for example if
9126 -- derived from a private view of the abstract type that doesn't have
9127 -- a visible Input), but a VM such as .NET or the Java VM can treat the
9128 -- operation as inherited anyway, and we don't want an abstract function
9129 -- to be (implicitly) inherited in that case because it can lead to a VM
9132 -- Do not generate stream routines for type Finalization_Master because
9133 -- a master may never appear in types and therefore cannot be read or
9137 (not Is_Limited_Type (Typ)
9138 or else Is_Interface (Typ)
9139 or else Has_Predefined_Or_Specified_Stream_Attribute)
9141 (Operation /= TSS_Stream_Input
9142 or else not Is_Abstract_Type (Typ)
9143 or else not Is_Derived_Type (Typ))
9144 and then not Has_Unknown_Discriminants (Typ)
9148 (Is_Task_Interface (Typ)
9149 or else Is_Protected_Interface (Typ)
9150 or else Is_Synchronized_Interface (Typ)))
9151 and then not Restriction_Active (No_Streams)
9152 and then not Restriction_Active (No_Dispatch)
9153 and then not No_Run_Time_Mode
9154 and then RTE_Available (RE_Tag)
9155 and then No (Type_Without_Stream_Operation (Typ))
9156 and then RTE_Available (RE_Root_Stream_Type)
9157 and then not Is_RTE (Typ, RE_Finalization_Master);
9158 end Stream_Operation_OK;