1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Aggr; use Exp_Aggr;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Ch11; use Exp_Ch11;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Smem; use Exp_Smem;
40 with Exp_Strm; use Exp_Strm;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Namet; use Namet;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Attr; use Sem_Attr;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Mech; use Sem_Mech;
61 with Sem_Res; use Sem_Res;
62 with Sem_SCIL; use Sem_SCIL;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sinfo; use Sinfo;
66 with Stand; use Stand;
67 with Snames; use Snames;
68 with Targparm; use Targparm;
69 with Tbuild; use Tbuild;
70 with Ttypes; use Ttypes;
71 with Validsw; use Validsw;
73 package body Exp_Ch3 is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id;
80 -- Add the declaration of a finalization list to the freeze actions for
81 -- Def_Id, and return its defining identifier.
83 procedure Adjust_Discriminants (Rtype : Entity_Id);
84 -- This is used when freezing a record type. It attempts to construct
85 -- more restrictive subtypes for discriminants so that the max size of
86 -- the record can be calculated more accurately. See the body of this
87 -- procedure for details.
89 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id);
90 -- Build initialization procedure for given array type. Nod is a node
91 -- used for attachment of any actions required in its construction.
92 -- It also supplies the source location used for the procedure.
94 function Build_Discriminant_Formals
96 Use_Dl : Boolean) return List_Id;
97 -- This function uses the discriminants of a type to build a list of
98 -- formal parameters, used in Build_Init_Procedure among other places.
99 -- If the flag Use_Dl is set, the list is built using the already
100 -- defined discriminals of the type, as is the case for concurrent
101 -- types with discriminants. Otherwise new identifiers are created,
102 -- with the source names of the discriminants.
104 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id;
105 -- This function builds a static aggregate that can serve as the initial
106 -- value for an array type whose bounds are static, and whose component
107 -- type is a composite type that has a static equivalent aggregate.
108 -- The equivalent array aggregate is used both for object initialization
109 -- and for component initialization, when used in the following function.
111 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id;
112 -- This function builds a static aggregate that can serve as the initial
113 -- value for a record type whose components are scalar and initialized
114 -- with compile-time values, or arrays with similar initialization or
115 -- defaults. When possible, initialization of an object of the type can
116 -- be achieved by using a copy of the aggregate as an initial value, thus
117 -- removing the implicit call that would otherwise constitute elaboration
120 function Build_Master_Renaming
122 T : Entity_Id) return Entity_Id;
123 -- If the designated type of an access type is a task type or contains
124 -- tasks, we make sure that a _Master variable is declared in the current
125 -- scope, and then declare a renaming for it:
127 -- atypeM : Master_Id renames _Master;
129 -- where atyp is the name of the access type. This declaration is used when
130 -- an allocator for the access type is expanded. The node is the full
131 -- declaration of the designated type that contains tasks. The renaming
132 -- declaration is inserted before N, and after the Master declaration.
134 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id);
135 -- Build record initialization procedure. N is the type declaration
136 -- node, and Pe is the corresponding entity for the record type.
138 procedure Build_Slice_Assignment (Typ : Entity_Id);
139 -- Build assignment procedure for one-dimensional arrays of controlled
140 -- types. Other array and slice assignments are expanded in-line, but
141 -- the code expansion for controlled components (when control actions
142 -- are active) can lead to very large blocks that GCC3 handles poorly.
144 procedure Build_Untagged_Equality (Typ : Entity_Id);
145 -- AI05-0123: Equality on untagged records composes. This procedure
146 -- builds the equality routine for an untagged record that has components
147 -- of a record type that has user-defined primitive equality operations.
148 -- The resulting operation is a TSS subprogram.
150 procedure Build_Variant_Record_Equality (Typ : Entity_Id);
151 -- Create An Equality function for the non-tagged variant record 'Typ'
152 -- and attach it to the TSS list
154 procedure Check_Stream_Attributes (Typ : Entity_Id);
155 -- Check that if a limited extension has a parent with user-defined stream
156 -- attributes, and does not itself have user-defined stream-attributes,
157 -- then any limited component of the extension also has the corresponding
158 -- user-defined stream attributes.
160 procedure Clean_Task_Names
162 Proc_Id : Entity_Id);
163 -- If an initialization procedure includes calls to generate names
164 -- for task subcomponents, indicate that secondary stack cleanup is
165 -- needed after an initialization. Typ is the component type, and Proc_Id
166 -- the initialization procedure for the enclosing composite type.
168 procedure Expand_Tagged_Root (T : Entity_Id);
169 -- Add a field _Tag at the beginning of the record. This field carries
170 -- the value of the access to the Dispatch table. This procedure is only
171 -- called on root type, the _Tag field being inherited by the descendants.
173 procedure Expand_Record_Controller (T : Entity_Id);
174 -- T must be a record type that Has_Controlled_Component. Add a field
175 -- _controller of type Record_Controller or Limited_Record_Controller
178 procedure Expand_Freeze_Array_Type (N : Node_Id);
179 -- Freeze an array type. Deals with building the initialization procedure,
180 -- creating the packed array type for a packed array and also with the
181 -- creation of the controlling procedures for the controlled case. The
182 -- argument N is the N_Freeze_Entity node for the type.
184 procedure Expand_Freeze_Enumeration_Type (N : Node_Id);
185 -- Freeze enumeration type with non-standard representation. Builds the
186 -- array and function needed to convert between enumeration pos and
187 -- enumeration representation values. N is the N_Freeze_Entity node
190 procedure Expand_Freeze_Record_Type (N : Node_Id);
191 -- Freeze record type. Builds all necessary discriminant checking
192 -- and other ancillary functions, and builds dispatch tables where
193 -- needed. The argument N is the N_Freeze_Entity node. This processing
194 -- applies only to E_Record_Type entities, not to class wide types,
195 -- record subtypes, or private types.
197 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id);
198 -- Treat user-defined stream operations as renaming_as_body if the
199 -- subprogram they rename is not frozen when the type is frozen.
201 procedure Initialization_Warning (E : Entity_Id);
202 -- If static elaboration of the package is requested, indicate
203 -- when a type does meet the conditions for static initialization. If
204 -- E is a type, it has components that have no static initialization.
205 -- if E is an entity, its initial expression is not compile-time known.
207 function Init_Formals (Typ : Entity_Id) return List_Id;
208 -- This function builds the list of formals for an initialization routine.
209 -- The first formal is always _Init with the given type. For task value
210 -- record types and types containing tasks, three additional formals are
213 -- _Master : Master_Id
214 -- _Chain : in out Activation_Chain
215 -- _Task_Name : String
217 -- The caller must append additional entries for discriminants if required.
219 function In_Runtime (E : Entity_Id) return Boolean;
220 -- Check if E is defined in the RTL (in a child of Ada or System). Used
221 -- to avoid to bring in the overhead of _Input, _Output for tagged types.
223 function Is_Variable_Size_Array (E : Entity_Id) return Boolean;
224 -- Returns true if E has variable size components
226 function Is_Variable_Size_Record (E : Entity_Id) return Boolean;
227 -- Returns true if E has variable size components
229 function Make_Eq_Body
231 Eq_Name : Name_Id) return Node_Id;
232 -- Build the body of a primitive equality operation for a tagged record
233 -- type, or in Ada2012 for any record type that has components with a
234 -- user-defined equality. Factored out of Predefined_Primitive_Bodies.
236 function Make_Eq_Case
239 Discr : Entity_Id := Empty) return List_Id;
240 -- Building block for variant record equality. Defined to share the code
241 -- between the tagged and non-tagged case. Given a Component_List node CL,
242 -- it generates an 'if' followed by a 'case' statement that compares all
243 -- components of local temporaries named X and Y (that are declared as
244 -- formals at some upper level). E provides the Sloc to be used for the
245 -- generated code. Discr is used as the case statement switch in the case
246 -- of Unchecked_Union equality.
250 L : List_Id) return Node_Id;
251 -- Building block for variant record equality. Defined to share the code
252 -- between the tagged and non-tagged case. Given the list of components
253 -- (or discriminants) L, it generates a return statement that compares all
254 -- components of local temporaries named X and Y (that are declared as
255 -- formals at some upper level). E provides the Sloc to be used for the
258 procedure Make_Predefined_Primitive_Specs
259 (Tag_Typ : Entity_Id;
260 Predef_List : out List_Id;
261 Renamed_Eq : out Entity_Id);
262 -- Create a list with the specs of the predefined primitive operations.
263 -- For tagged types that are interfaces all these primitives are defined
266 -- The following entries are present for all tagged types, and provide
267 -- the results of the corresponding attribute applied to the object.
268 -- Dispatching is required in general, since the result of the attribute
269 -- will vary with the actual object subtype.
271 -- _alignment provides result of 'Alignment attribute
272 -- _size provides result of 'Size attribute
273 -- typSR provides result of 'Read attribute
274 -- typSW provides result of 'Write attribute
275 -- typSI provides result of 'Input attribute
276 -- typSO provides result of 'Output attribute
278 -- The following entries are additionally present for non-limited tagged
279 -- types, and implement additional dispatching operations for predefined
282 -- _equality implements "=" operator
283 -- _assign implements assignment operation
284 -- typDF implements deep finalization
285 -- typDA implements deep adjust
287 -- The latter two are empty procedures unless the type contains some
288 -- controlled components that require finalization actions (the deep
289 -- in the name refers to the fact that the action applies to components).
291 -- The list is returned in Predef_List. The Parameter Renamed_Eq either
292 -- returns the value Empty, or else the defining unit name for the
293 -- predefined equality function in the case where the type has a primitive
294 -- operation that is a renaming of predefined equality (but only if there
295 -- is also an overriding user-defined equality function). The returned
296 -- Renamed_Eq will be passed to the corresponding parameter of
297 -- Predefined_Primitive_Bodies.
299 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean;
300 -- returns True if there are representation clauses for type T that are not
301 -- inherited. If the result is false, the init_proc and the discriminant
302 -- checking functions of the parent can be reused by a derived type.
304 procedure Make_Controlling_Function_Wrappers
305 (Tag_Typ : Entity_Id;
306 Decl_List : out List_Id;
307 Body_List : out List_Id);
308 -- Ada 2005 (AI-391): Makes specs and bodies for the wrapper functions
309 -- associated with inherited functions with controlling results which
310 -- are not overridden. The body of each wrapper function consists solely
311 -- of a return statement whose expression is an extension aggregate
312 -- invoking the inherited subprogram's parent subprogram and extended
313 -- with a null association list.
315 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id;
316 -- Ada 2005 (AI-251): Makes specs for null procedures associated with any
317 -- null procedures inherited from an interface type that have not been
318 -- overridden. Only one null procedure will be created for a given set of
319 -- inherited null procedures with homographic profiles.
321 function Predef_Spec_Or_Body
326 Ret_Type : Entity_Id := Empty;
327 For_Body : Boolean := False) return Node_Id;
328 -- This function generates the appropriate expansion for a predefined
329 -- primitive operation specified by its name, parameter profile and
330 -- return type (Empty means this is a procedure). If For_Body is false,
331 -- then the returned node is a subprogram declaration. If For_Body is
332 -- true, then the returned node is a empty subprogram body containing
333 -- no declarations and no statements.
335 function Predef_Stream_Attr_Spec
338 Name : TSS_Name_Type;
339 For_Body : Boolean := False) return Node_Id;
340 -- Specialized version of Predef_Spec_Or_Body that apply to read, write,
341 -- input and output attribute whose specs are constructed in Exp_Strm.
343 function Predef_Deep_Spec
346 Name : TSS_Name_Type;
347 For_Body : Boolean := False) return Node_Id;
348 -- Specialized version of Predef_Spec_Or_Body that apply to _deep_adjust
349 -- and _deep_finalize
351 function Predefined_Primitive_Bodies
352 (Tag_Typ : Entity_Id;
353 Renamed_Eq : Entity_Id) return List_Id;
354 -- Create the bodies of the predefined primitives that are described in
355 -- Predefined_Primitive_Specs. When not empty, Renamed_Eq must denote
356 -- the defining unit name of the type's predefined equality as returned
357 -- by Make_Predefined_Primitive_Specs.
359 function Predefined_Primitive_Freeze (Tag_Typ : Entity_Id) return List_Id;
360 -- Freeze entities of all predefined primitive operations. This is needed
361 -- because the bodies of these operations do not normally do any freezing.
363 function Stream_Operation_OK
365 Operation : TSS_Name_Type) return Boolean;
366 -- Check whether the named stream operation must be emitted for a given
367 -- type. The rules for inheritance of stream attributes by type extensions
368 -- are enforced by this function. Furthermore, various restrictions prevent
369 -- the generation of these operations, as a useful optimization or for
370 -- certification purposes.
372 ---------------------
373 -- Add_Final_Chain --
374 ---------------------
376 function Add_Final_Chain (Def_Id : Entity_Id) return Entity_Id is
377 Loc : constant Source_Ptr := Sloc (Def_Id);
382 Make_Defining_Identifier (Loc,
383 New_External_Name (Chars (Def_Id), 'L'));
385 Append_Freeze_Action (Def_Id,
386 Make_Object_Declaration (Loc,
387 Defining_Identifier => Flist,
389 New_Reference_To (RTE (RE_List_Controller), Loc)));
394 --------------------------
395 -- Adjust_Discriminants --
396 --------------------------
398 -- This procedure attempts to define subtypes for discriminants that are
399 -- more restrictive than those declared. Such a replacement is possible if
400 -- we can demonstrate that values outside the restricted range would cause
401 -- constraint errors in any case. The advantage of restricting the
402 -- discriminant types in this way is that the maximum size of the variant
403 -- record can be calculated more conservatively.
405 -- An example of a situation in which we can perform this type of
406 -- restriction is the following:
408 -- subtype B is range 1 .. 10;
409 -- type Q is array (B range <>) of Integer;
411 -- type V (N : Natural) is record
415 -- In this situation, we can restrict the upper bound of N to 10, since
416 -- any larger value would cause a constraint error in any case.
418 -- There are many situations in which such restriction is possible, but
419 -- for now, we just look for cases like the above, where the component
420 -- in question is a one dimensional array whose upper bound is one of
421 -- the record discriminants. Also the component must not be part of
422 -- any variant part, since then the component does not always exist.
424 procedure Adjust_Discriminants (Rtype : Entity_Id) is
425 Loc : constant Source_Ptr := Sloc (Rtype);
442 Comp := First_Component (Rtype);
443 while Present (Comp) loop
445 -- If our parent is a variant, quit, we do not look at components
446 -- that are in variant parts, because they may not always exist.
448 P := Parent (Comp); -- component declaration
449 P := Parent (P); -- component list
451 exit when Nkind (Parent (P)) = N_Variant;
453 -- We are looking for a one dimensional array type
455 Ctyp := Etype (Comp);
457 if not Is_Array_Type (Ctyp)
458 or else Number_Dimensions (Ctyp) > 1
463 -- The lower bound must be constant, and the upper bound is a
464 -- discriminant (which is a discriminant of the current record).
466 Ityp := Etype (First_Index (Ctyp));
467 Lo := Type_Low_Bound (Ityp);
468 Hi := Type_High_Bound (Ityp);
470 if not Compile_Time_Known_Value (Lo)
471 or else Nkind (Hi) /= N_Identifier
472 or else No (Entity (Hi))
473 or else Ekind (Entity (Hi)) /= E_Discriminant
478 -- We have an array with appropriate bounds
480 Loval := Expr_Value (Lo);
481 Discr := Entity (Hi);
482 Dtyp := Etype (Discr);
484 -- See if the discriminant has a known upper bound
486 Dhi := Type_High_Bound (Dtyp);
488 if not Compile_Time_Known_Value (Dhi) then
492 Dhiv := Expr_Value (Dhi);
494 -- See if base type of component array has known upper bound
496 Ahi := Type_High_Bound (Etype (First_Index (Base_Type (Ctyp))));
498 if not Compile_Time_Known_Value (Ahi) then
502 Ahiv := Expr_Value (Ahi);
504 -- The condition for doing the restriction is that the high bound
505 -- of the discriminant is greater than the low bound of the array,
506 -- and is also greater than the high bound of the base type index.
508 if Dhiv > Loval and then Dhiv > Ahiv then
510 -- We can reset the upper bound of the discriminant type to
511 -- whichever is larger, the low bound of the component, or
512 -- the high bound of the base type array index.
514 -- We build a subtype that is declared as
516 -- subtype Tnn is discr_type range discr_type'First .. max;
518 -- And insert this declaration into the tree. The type of the
519 -- discriminant is then reset to this more restricted subtype.
521 Tnn := Make_Temporary (Loc, 'T');
523 Insert_Action (Declaration_Node (Rtype),
524 Make_Subtype_Declaration (Loc,
525 Defining_Identifier => Tnn,
526 Subtype_Indication =>
527 Make_Subtype_Indication (Loc,
528 Subtype_Mark => New_Occurrence_Of (Dtyp, Loc),
530 Make_Range_Constraint (Loc,
534 Make_Attribute_Reference (Loc,
535 Attribute_Name => Name_First,
536 Prefix => New_Occurrence_Of (Dtyp, Loc)),
538 Make_Integer_Literal (Loc,
539 Intval => UI_Max (Loval, Ahiv)))))));
541 Set_Etype (Discr, Tnn);
545 Next_Component (Comp);
547 end Adjust_Discriminants;
549 ---------------------------
550 -- Build_Array_Init_Proc --
551 ---------------------------
553 procedure Build_Array_Init_Proc (A_Type : Entity_Id; Nod : Node_Id) is
554 Loc : constant Source_Ptr := Sloc (Nod);
555 Comp_Type : constant Entity_Id := Component_Type (A_Type);
556 Index_List : List_Id;
558 Body_Stmts : List_Id;
559 Has_Default_Init : Boolean;
561 function Init_Component return List_Id;
562 -- Create one statement to initialize one array component, designated
563 -- by a full set of indices.
565 function Init_One_Dimension (N : Int) return List_Id;
566 -- Create loop to initialize one dimension of the array. The single
567 -- statement in the loop body initializes the inner dimensions if any,
568 -- or else the single component. Note that this procedure is called
569 -- recursively, with N being the dimension to be initialized. A call
570 -- with N greater than the number of dimensions simply generates the
571 -- component initialization, terminating the recursion.
577 function Init_Component return List_Id is
582 Make_Indexed_Component (Loc,
583 Prefix => Make_Identifier (Loc, Name_uInit),
584 Expressions => Index_List);
586 if Needs_Simple_Initialization (Comp_Type) then
587 Set_Assignment_OK (Comp);
589 Make_Assignment_Statement (Loc,
593 (Comp_Type, Nod, Component_Size (A_Type))));
596 Clean_Task_Names (Comp_Type, Proc_Id);
598 Build_Initialization_Call
599 (Loc, Comp, Comp_Type,
600 In_Init_Proc => True,
601 Enclos_Type => A_Type);
605 ------------------------
606 -- Init_One_Dimension --
607 ------------------------
609 function Init_One_Dimension (N : Int) return List_Id is
613 -- If the component does not need initializing, then there is nothing
614 -- to do here, so we return a null body. This occurs when generating
615 -- the dummy Init_Proc needed for Initialize_Scalars processing.
617 if not Has_Non_Null_Base_Init_Proc (Comp_Type)
618 and then not Needs_Simple_Initialization (Comp_Type)
619 and then not Has_Task (Comp_Type)
621 return New_List (Make_Null_Statement (Loc));
623 -- If all dimensions dealt with, we simply initialize the component
625 elsif N > Number_Dimensions (A_Type) then
626 return Init_Component;
628 -- Here we generate the required loop
632 Make_Defining_Identifier (Loc, New_External_Name ('J', N));
634 Append (New_Reference_To (Index, Loc), Index_List);
637 Make_Implicit_Loop_Statement (Nod,
640 Make_Iteration_Scheme (Loc,
641 Loop_Parameter_Specification =>
642 Make_Loop_Parameter_Specification (Loc,
643 Defining_Identifier => Index,
644 Discrete_Subtype_Definition =>
645 Make_Attribute_Reference (Loc,
646 Prefix => Make_Identifier (Loc, Name_uInit),
647 Attribute_Name => Name_Range,
648 Expressions => New_List (
649 Make_Integer_Literal (Loc, N))))),
650 Statements => Init_One_Dimension (N + 1)));
652 end Init_One_Dimension;
654 -- Start of processing for Build_Array_Init_Proc
657 -- Nothing to generate in the following cases:
659 -- 1. Initialization is suppressed for the type
660 -- 2. The type is a value type, in the CIL sense.
661 -- 3. The type has CIL/JVM convention.
662 -- 4. An initialization already exists for the base type
664 if Suppress_Init_Proc (A_Type)
665 or else Is_Value_Type (Comp_Type)
666 or else Convention (A_Type) = Convention_CIL
667 or else Convention (A_Type) = Convention_Java
668 or else Present (Base_Init_Proc (A_Type))
673 Index_List := New_List;
675 -- We need an initialization procedure if any of the following is true:
677 -- 1. The component type has an initialization procedure
678 -- 2. The component type needs simple initialization
679 -- 3. Tasks are present
680 -- 4. The type is marked as a public entity
682 -- The reason for the public entity test is to deal properly with the
683 -- Initialize_Scalars pragma. This pragma can be set in the client and
684 -- not in the declaring package, this means the client will make a call
685 -- to the initialization procedure (because one of conditions 1-3 must
686 -- apply in this case), and we must generate a procedure (even if it is
687 -- null) to satisfy the call in this case.
689 -- Exception: do not build an array init_proc for a type whose root
690 -- type is Standard.String or Standard.Wide_[Wide_]String, since there
691 -- is no place to put the code, and in any case we handle initialization
692 -- of such types (in the Initialize_Scalars case, that's the only time
693 -- the issue arises) in a special manner anyway which does not need an
696 Has_Default_Init := Has_Non_Null_Base_Init_Proc (Comp_Type)
697 or else Needs_Simple_Initialization (Comp_Type)
698 or else Has_Task (Comp_Type);
701 or else (not Restriction_Active (No_Initialize_Scalars)
702 and then Is_Public (A_Type)
703 and then Root_Type (A_Type) /= Standard_String
704 and then Root_Type (A_Type) /= Standard_Wide_String
705 and then Root_Type (A_Type) /= Standard_Wide_Wide_String)
708 Make_Defining_Identifier (Loc,
709 Chars => Make_Init_Proc_Name (A_Type));
711 -- If No_Default_Initialization restriction is active, then we don't
712 -- want to build an init_proc, but we need to mark that an init_proc
713 -- would be needed if this restriction was not active (so that we can
714 -- detect attempts to call it), so set a dummy init_proc in place.
715 -- This is only done though when actual default initialization is
716 -- needed (and not done when only Is_Public is True), since otherwise
717 -- objects such as arrays of scalars could be wrongly flagged as
718 -- violating the restriction.
720 if Restriction_Active (No_Default_Initialization) then
721 if Has_Default_Init then
722 Set_Init_Proc (A_Type, Proc_Id);
728 Body_Stmts := Init_One_Dimension (1);
731 Make_Subprogram_Body (Loc,
733 Make_Procedure_Specification (Loc,
734 Defining_Unit_Name => Proc_Id,
735 Parameter_Specifications => Init_Formals (A_Type)),
736 Declarations => New_List,
737 Handled_Statement_Sequence =>
738 Make_Handled_Sequence_Of_Statements (Loc,
739 Statements => Body_Stmts)));
741 Set_Ekind (Proc_Id, E_Procedure);
742 Set_Is_Public (Proc_Id, Is_Public (A_Type));
743 Set_Is_Internal (Proc_Id);
744 Set_Has_Completion (Proc_Id);
746 if not Debug_Generated_Code then
747 Set_Debug_Info_Off (Proc_Id);
750 -- Set inlined unless controlled stuff or tasks around, in which
751 -- case we do not want to inline, because nested stuff may cause
752 -- difficulties in inter-unit inlining, and furthermore there is
753 -- in any case no point in inlining such complex init procs.
755 if not Has_Task (Proc_Id)
756 and then not Needs_Finalization (Proc_Id)
758 Set_Is_Inlined (Proc_Id);
761 -- Associate Init_Proc with type, and determine if the procedure
762 -- is null (happens because of the Initialize_Scalars pragma case,
763 -- where we have to generate a null procedure in case it is called
764 -- by a client with Initialize_Scalars set). Such procedures have
765 -- to be generated, but do not have to be called, so we mark them
766 -- as null to suppress the call.
768 Set_Init_Proc (A_Type, Proc_Id);
770 if List_Length (Body_Stmts) = 1
772 -- We must skip SCIL nodes because they may have been added to this
773 -- list by Insert_Actions.
775 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
777 Set_Is_Null_Init_Proc (Proc_Id);
780 -- Try to build a static aggregate to initialize statically
781 -- objects of the type. This can only be done for constrained
782 -- one-dimensional arrays with static bounds.
784 Set_Static_Initialization
786 Build_Equivalent_Array_Aggregate (First_Subtype (A_Type)));
789 end Build_Array_Init_Proc;
791 -----------------------------
792 -- Build_Class_Wide_Master --
793 -----------------------------
795 procedure Build_Class_Wide_Master (T : Entity_Id) is
796 Loc : constant Source_Ptr := Sloc (T);
804 -- Nothing to do if there is no task hierarchy
806 if Restriction_Active (No_Task_Hierarchy) then
810 -- Find declaration that created the access type: either a type
811 -- declaration, or an object declaration with an access definition,
812 -- in which case the type is anonymous.
815 P := Associated_Node_For_Itype (T);
820 Scop := Find_Master_Scope (T);
822 -- Nothing to do if we already built a master entity for this scope
824 if not Has_Master_Entity (Scop) then
826 -- First build the master entity
827 -- _Master : constant Master_Id := Current_Master.all;
828 -- and insert it just before the current declaration.
831 Make_Object_Declaration (Loc,
832 Defining_Identifier =>
833 Make_Defining_Identifier (Loc, Name_uMaster),
834 Constant_Present => True,
835 Object_Definition => New_Reference_To (Standard_Integer, Loc),
837 Make_Explicit_Dereference (Loc,
838 New_Reference_To (RTE (RE_Current_Master), Loc)));
840 Set_Has_Master_Entity (Scop);
841 Insert_Action (P, Decl);
844 -- Now mark the containing scope as a task master. Masters
845 -- associated with return statements are already marked at
846 -- this stage (see Analyze_Subprogram_Body).
848 if Ekind (Current_Scope) /= E_Return_Statement then
850 while Nkind (Par) /= N_Compilation_Unit loop
853 -- If we fall off the top, we are at the outer level, and the
854 -- environment task is our effective master, so nothing to mark.
857 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
859 Set_Is_Task_Master (Par, True);
866 -- Now define the renaming of the master_id
869 Make_Defining_Identifier (Loc,
870 New_External_Name (Chars (T), 'M'));
873 Make_Object_Renaming_Declaration (Loc,
874 Defining_Identifier => M_Id,
875 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
876 Name => Make_Identifier (Loc, Name_uMaster));
877 Insert_Before (P, Decl);
880 Set_Master_Id (T, M_Id);
883 when RE_Not_Available =>
885 end Build_Class_Wide_Master;
887 --------------------------------
888 -- Build_Discr_Checking_Funcs --
889 --------------------------------
891 procedure Build_Discr_Checking_Funcs (N : Node_Id) is
894 Enclosing_Func_Id : Entity_Id;
899 function Build_Case_Statement
900 (Case_Id : Entity_Id;
901 Variant : Node_Id) return Node_Id;
902 -- Build a case statement containing only two alternatives. The first
903 -- alternative corresponds exactly to the discrete choices given on the
904 -- variant with contains the components that we are generating the
905 -- checks for. If the discriminant is one of these return False. The
906 -- second alternative is an OTHERS choice that will return True
907 -- indicating the discriminant did not match.
909 function Build_Dcheck_Function
910 (Case_Id : Entity_Id;
911 Variant : Node_Id) return Entity_Id;
912 -- Build the discriminant checking function for a given variant
914 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id);
915 -- Builds the discriminant checking function for each variant of the
916 -- given variant part of the record type.
918 --------------------------
919 -- Build_Case_Statement --
920 --------------------------
922 function Build_Case_Statement
923 (Case_Id : Entity_Id;
924 Variant : Node_Id) return Node_Id
926 Alt_List : constant List_Id := New_List;
927 Actuals_List : List_Id;
929 Case_Alt_Node : Node_Id;
931 Choice_List : List_Id;
933 Return_Node : Node_Id;
936 Case_Node := New_Node (N_Case_Statement, Loc);
938 -- Replace the discriminant which controls the variant, with the name
939 -- of the formal of the checking function.
941 Set_Expression (Case_Node,
942 Make_Identifier (Loc, Chars (Case_Id)));
944 Choice := First (Discrete_Choices (Variant));
946 if Nkind (Choice) = N_Others_Choice then
947 Choice_List := New_Copy_List (Others_Discrete_Choices (Choice));
949 Choice_List := New_Copy_List (Discrete_Choices (Variant));
952 if not Is_Empty_List (Choice_List) then
953 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
954 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
956 -- In case this is a nested variant, we need to return the result
957 -- of the discriminant checking function for the immediately
958 -- enclosing variant.
960 if Present (Enclosing_Func_Id) then
961 Actuals_List := New_List;
963 D := First_Discriminant (Rec_Id);
964 while Present (D) loop
965 Append (Make_Identifier (Loc, Chars (D)), Actuals_List);
966 Next_Discriminant (D);
970 Make_Simple_Return_Statement (Loc,
972 Make_Function_Call (Loc,
974 New_Reference_To (Enclosing_Func_Id, Loc),
975 Parameter_Associations =>
980 Make_Simple_Return_Statement (Loc,
982 New_Reference_To (Standard_False, Loc));
985 Set_Statements (Case_Alt_Node, New_List (Return_Node));
986 Append (Case_Alt_Node, Alt_List);
989 Case_Alt_Node := New_Node (N_Case_Statement_Alternative, Loc);
990 Choice_List := New_List (New_Node (N_Others_Choice, Loc));
991 Set_Discrete_Choices (Case_Alt_Node, Choice_List);
994 Make_Simple_Return_Statement (Loc,
996 New_Reference_To (Standard_True, Loc));
998 Set_Statements (Case_Alt_Node, New_List (Return_Node));
999 Append (Case_Alt_Node, Alt_List);
1001 Set_Alternatives (Case_Node, Alt_List);
1003 end Build_Case_Statement;
1005 ---------------------------
1006 -- Build_Dcheck_Function --
1007 ---------------------------
1009 function Build_Dcheck_Function
1010 (Case_Id : Entity_Id;
1011 Variant : Node_Id) return Entity_Id
1013 Body_Node : Node_Id;
1014 Func_Id : Entity_Id;
1015 Parameter_List : List_Id;
1016 Spec_Node : Node_Id;
1019 Body_Node := New_Node (N_Subprogram_Body, Loc);
1020 Sequence := Sequence + 1;
1023 Make_Defining_Identifier (Loc,
1024 Chars => New_External_Name (Chars (Rec_Id), 'D', Sequence));
1026 Spec_Node := New_Node (N_Function_Specification, Loc);
1027 Set_Defining_Unit_Name (Spec_Node, Func_Id);
1029 Parameter_List := Build_Discriminant_Formals (Rec_Id, False);
1031 Set_Parameter_Specifications (Spec_Node, Parameter_List);
1032 Set_Result_Definition (Spec_Node,
1033 New_Reference_To (Standard_Boolean, Loc));
1034 Set_Specification (Body_Node, Spec_Node);
1035 Set_Declarations (Body_Node, New_List);
1037 Set_Handled_Statement_Sequence (Body_Node,
1038 Make_Handled_Sequence_Of_Statements (Loc,
1039 Statements => New_List (
1040 Build_Case_Statement (Case_Id, Variant))));
1042 Set_Ekind (Func_Id, E_Function);
1043 Set_Mechanism (Func_Id, Default_Mechanism);
1044 Set_Is_Inlined (Func_Id, True);
1045 Set_Is_Pure (Func_Id, True);
1046 Set_Is_Public (Func_Id, Is_Public (Rec_Id));
1047 Set_Is_Internal (Func_Id, True);
1049 if not Debug_Generated_Code then
1050 Set_Debug_Info_Off (Func_Id);
1053 Analyze (Body_Node);
1055 Append_Freeze_Action (Rec_Id, Body_Node);
1056 Set_Dcheck_Function (Variant, Func_Id);
1058 end Build_Dcheck_Function;
1060 ----------------------------
1061 -- Build_Dcheck_Functions --
1062 ----------------------------
1064 procedure Build_Dcheck_Functions (Variant_Part_Node : Node_Id) is
1065 Component_List_Node : Node_Id;
1067 Discr_Name : Entity_Id;
1068 Func_Id : Entity_Id;
1070 Saved_Enclosing_Func_Id : Entity_Id;
1073 -- Build the discriminant-checking function for each variant, and
1074 -- label all components of that variant with the function's name.
1075 -- We only Generate a discriminant-checking function when the
1076 -- variant is not empty, to prevent the creation of dead code.
1077 -- The exception to that is when Frontend_Layout_On_Target is set,
1078 -- because the variant record size function generated in package
1079 -- Layout needs to generate calls to all discriminant-checking
1080 -- functions, including those for empty variants.
1082 Discr_Name := Entity (Name (Variant_Part_Node));
1083 Variant := First_Non_Pragma (Variants (Variant_Part_Node));
1085 while Present (Variant) loop
1086 Component_List_Node := Component_List (Variant);
1088 if not Null_Present (Component_List_Node)
1089 or else Frontend_Layout_On_Target
1091 Func_Id := Build_Dcheck_Function (Discr_Name, Variant);
1093 First_Non_Pragma (Component_Items (Component_List_Node));
1095 while Present (Decl) loop
1096 Set_Discriminant_Checking_Func
1097 (Defining_Identifier (Decl), Func_Id);
1099 Next_Non_Pragma (Decl);
1102 if Present (Variant_Part (Component_List_Node)) then
1103 Saved_Enclosing_Func_Id := Enclosing_Func_Id;
1104 Enclosing_Func_Id := Func_Id;
1105 Build_Dcheck_Functions (Variant_Part (Component_List_Node));
1106 Enclosing_Func_Id := Saved_Enclosing_Func_Id;
1110 Next_Non_Pragma (Variant);
1112 end Build_Dcheck_Functions;
1114 -- Start of processing for Build_Discr_Checking_Funcs
1117 -- Only build if not done already
1119 if not Discr_Check_Funcs_Built (N) then
1120 Type_Def := Type_Definition (N);
1122 if Nkind (Type_Def) = N_Record_Definition then
1123 if No (Component_List (Type_Def)) then -- null record.
1126 V := Variant_Part (Component_List (Type_Def));
1129 else pragma Assert (Nkind (Type_Def) = N_Derived_Type_Definition);
1130 if No (Component_List (Record_Extension_Part (Type_Def))) then
1134 (Component_List (Record_Extension_Part (Type_Def)));
1138 Rec_Id := Defining_Identifier (N);
1140 if Present (V) and then not Is_Unchecked_Union (Rec_Id) then
1142 Enclosing_Func_Id := Empty;
1143 Build_Dcheck_Functions (V);
1146 Set_Discr_Check_Funcs_Built (N);
1148 end Build_Discr_Checking_Funcs;
1150 --------------------------------
1151 -- Build_Discriminant_Formals --
1152 --------------------------------
1154 function Build_Discriminant_Formals
1155 (Rec_Id : Entity_Id;
1156 Use_Dl : Boolean) return List_Id
1158 Loc : Source_Ptr := Sloc (Rec_Id);
1159 Parameter_List : constant List_Id := New_List;
1162 Formal_Type : Entity_Id;
1163 Param_Spec_Node : Node_Id;
1166 if Has_Discriminants (Rec_Id) then
1167 D := First_Discriminant (Rec_Id);
1168 while Present (D) loop
1172 Formal := Discriminal (D);
1173 Formal_Type := Etype (Formal);
1175 Formal := Make_Defining_Identifier (Loc, Chars (D));
1176 Formal_Type := Etype (D);
1180 Make_Parameter_Specification (Loc,
1181 Defining_Identifier => Formal,
1183 New_Reference_To (Formal_Type, Loc));
1184 Append (Param_Spec_Node, Parameter_List);
1185 Next_Discriminant (D);
1189 return Parameter_List;
1190 end Build_Discriminant_Formals;
1192 --------------------------------------
1193 -- Build_Equivalent_Array_Aggregate --
1194 --------------------------------------
1196 function Build_Equivalent_Array_Aggregate (T : Entity_Id) return Node_Id is
1197 Loc : constant Source_Ptr := Sloc (T);
1198 Comp_Type : constant Entity_Id := Component_Type (T);
1199 Index_Type : constant Entity_Id := Etype (First_Index (T));
1200 Proc : constant Entity_Id := Base_Init_Proc (T);
1206 if not Is_Constrained (T)
1207 or else Number_Dimensions (T) > 1
1210 Initialization_Warning (T);
1214 Lo := Type_Low_Bound (Index_Type);
1215 Hi := Type_High_Bound (Index_Type);
1217 if not Compile_Time_Known_Value (Lo)
1218 or else not Compile_Time_Known_Value (Hi)
1220 Initialization_Warning (T);
1224 if Is_Record_Type (Comp_Type)
1225 and then Present (Base_Init_Proc (Comp_Type))
1227 Expr := Static_Initialization (Base_Init_Proc (Comp_Type));
1230 Initialization_Warning (T);
1235 Initialization_Warning (T);
1239 Aggr := Make_Aggregate (Loc, No_List, New_List);
1240 Set_Etype (Aggr, T);
1241 Set_Aggregate_Bounds (Aggr,
1243 Low_Bound => New_Copy (Lo),
1244 High_Bound => New_Copy (Hi)));
1245 Set_Parent (Aggr, Parent (Proc));
1247 Append_To (Component_Associations (Aggr),
1248 Make_Component_Association (Loc,
1252 Low_Bound => New_Copy (Lo),
1253 High_Bound => New_Copy (Hi))),
1254 Expression => Expr));
1256 if Static_Array_Aggregate (Aggr) then
1259 Initialization_Warning (T);
1262 end Build_Equivalent_Array_Aggregate;
1264 ---------------------------------------
1265 -- Build_Equivalent_Record_Aggregate --
1266 ---------------------------------------
1268 function Build_Equivalent_Record_Aggregate (T : Entity_Id) return Node_Id is
1271 Comp_Type : Entity_Id;
1273 -- Start of processing for Build_Equivalent_Record_Aggregate
1276 if not Is_Record_Type (T)
1277 or else Has_Discriminants (T)
1278 or else Is_Limited_Type (T)
1279 or else Has_Non_Standard_Rep (T)
1281 Initialization_Warning (T);
1285 Comp := First_Component (T);
1287 -- A null record needs no warning
1293 while Present (Comp) loop
1295 -- Array components are acceptable if initialized by a positional
1296 -- aggregate with static components.
1298 if Is_Array_Type (Etype (Comp)) then
1299 Comp_Type := Component_Type (Etype (Comp));
1301 if Nkind (Parent (Comp)) /= N_Component_Declaration
1302 or else No (Expression (Parent (Comp)))
1303 or else Nkind (Expression (Parent (Comp))) /= N_Aggregate
1305 Initialization_Warning (T);
1308 elsif Is_Scalar_Type (Component_Type (Etype (Comp)))
1310 (not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1312 not Compile_Time_Known_Value (Type_High_Bound (Comp_Type)))
1314 Initialization_Warning (T);
1318 not Static_Array_Aggregate (Expression (Parent (Comp)))
1320 Initialization_Warning (T);
1324 elsif Is_Scalar_Type (Etype (Comp)) then
1325 Comp_Type := Etype (Comp);
1327 if Nkind (Parent (Comp)) /= N_Component_Declaration
1328 or else No (Expression (Parent (Comp)))
1329 or else not Compile_Time_Known_Value (Expression (Parent (Comp)))
1330 or else not Compile_Time_Known_Value (Type_Low_Bound (Comp_Type))
1332 Compile_Time_Known_Value (Type_High_Bound (Comp_Type))
1334 Initialization_Warning (T);
1338 -- For now, other types are excluded
1341 Initialization_Warning (T);
1345 Next_Component (Comp);
1348 -- All components have static initialization. Build positional aggregate
1349 -- from the given expressions or defaults.
1351 Agg := Make_Aggregate (Sloc (T), New_List, New_List);
1352 Set_Parent (Agg, Parent (T));
1354 Comp := First_Component (T);
1355 while Present (Comp) loop
1357 (New_Copy_Tree (Expression (Parent (Comp))), Expressions (Agg));
1358 Next_Component (Comp);
1361 Analyze_And_Resolve (Agg, T);
1363 end Build_Equivalent_Record_Aggregate;
1365 -------------------------------
1366 -- Build_Initialization_Call --
1367 -------------------------------
1369 -- References to a discriminant inside the record type declaration can
1370 -- appear either in the subtype_indication to constrain a record or an
1371 -- array, or as part of a larger expression given for the initial value
1372 -- of a component. In both of these cases N appears in the record
1373 -- initialization procedure and needs to be replaced by the formal
1374 -- parameter of the initialization procedure which corresponds to that
1377 -- In the example below, references to discriminants D1 and D2 in proc_1
1378 -- are replaced by references to formals with the same name
1381 -- A similar replacement is done for calls to any record initialization
1382 -- procedure for any components that are themselves of a record type.
1384 -- type R (D1, D2 : Integer) is record
1385 -- X : Integer := F * D1;
1386 -- Y : Integer := F * D2;
1389 -- procedure proc_1 (Out_2 : out R; D1 : Integer; D2 : Integer) is
1393 -- Out_2.X := F * D1;
1394 -- Out_2.Y := F * D2;
1397 function Build_Initialization_Call
1401 In_Init_Proc : Boolean := False;
1402 Enclos_Type : Entity_Id := Empty;
1403 Discr_Map : Elist_Id := New_Elmt_List;
1404 With_Default_Init : Boolean := False;
1405 Constructor_Ref : Node_Id := Empty) return List_Id
1407 Res : constant List_Id := New_List;
1410 Controller_Typ : Entity_Id;
1414 First_Arg : Node_Id;
1415 Full_Init_Type : Entity_Id;
1416 Full_Type : Entity_Id := Typ;
1417 Init_Type : Entity_Id;
1421 pragma Assert (Constructor_Ref = Empty
1422 or else Is_CPP_Constructor_Call (Constructor_Ref));
1424 if No (Constructor_Ref) then
1425 Proc := Base_Init_Proc (Typ);
1427 Proc := Base_Init_Proc (Typ, Entity (Name (Constructor_Ref)));
1430 pragma Assert (Present (Proc));
1431 Init_Type := Etype (First_Formal (Proc));
1432 Full_Init_Type := Underlying_Type (Init_Type);
1434 -- Nothing to do if the Init_Proc is null, unless Initialize_Scalars
1435 -- is active (in which case we make the call anyway, since in the
1436 -- actual compiled client it may be non null).
1437 -- Also nothing to do for value types.
1439 if (Is_Null_Init_Proc (Proc) and then not Init_Or_Norm_Scalars)
1440 or else Is_Value_Type (Typ)
1442 (Is_Array_Type (Typ) and then Is_Value_Type (Component_Type (Typ)))
1447 -- Go to full view if private type. In the case of successive
1448 -- private derivations, this can require more than one step.
1450 while Is_Private_Type (Full_Type)
1451 and then Present (Full_View (Full_Type))
1453 Full_Type := Full_View (Full_Type);
1456 -- If Typ is derived, the procedure is the initialization procedure for
1457 -- the root type. Wrap the argument in an conversion to make it type
1458 -- honest. Actually it isn't quite type honest, because there can be
1459 -- conflicts of views in the private type case. That is why we set
1460 -- Conversion_OK in the conversion node.
1462 if (Is_Record_Type (Typ)
1463 or else Is_Array_Type (Typ)
1464 or else Is_Private_Type (Typ))
1465 and then Init_Type /= Base_Type (Typ)
1467 First_Arg := OK_Convert_To (Etype (Init_Type), Id_Ref);
1468 Set_Etype (First_Arg, Init_Type);
1471 First_Arg := Id_Ref;
1474 Args := New_List (Convert_Concurrent (First_Arg, Typ));
1476 -- In the tasks case, add _Master as the value of the _Master parameter
1477 -- and _Chain as the value of the _Chain parameter. At the outer level,
1478 -- these will be variables holding the corresponding values obtained
1479 -- from GNARL. At inner levels, they will be the parameters passed down
1480 -- through the outer routines.
1482 if Has_Task (Full_Type) then
1483 if Restriction_Active (No_Task_Hierarchy) then
1485 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
1487 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
1490 Append_To (Args, Make_Identifier (Loc, Name_uChain));
1492 -- Ada 2005 (AI-287): In case of default initialized components
1493 -- with tasks, we generate a null string actual parameter.
1494 -- This is just a workaround that must be improved later???
1496 if With_Default_Init then
1498 Make_String_Literal (Loc,
1503 Build_Task_Image_Decls (Loc, Id_Ref, Enclos_Type, In_Init_Proc);
1504 Decl := Last (Decls);
1507 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
1508 Append_List (Decls, Res);
1516 -- Add discriminant values if discriminants are present
1518 if Has_Discriminants (Full_Init_Type) then
1519 Discr := First_Discriminant (Full_Init_Type);
1521 while Present (Discr) loop
1523 -- If this is a discriminated concurrent type, the init_proc
1524 -- for the corresponding record is being called. Use that type
1525 -- directly to find the discriminant value, to handle properly
1526 -- intervening renamed discriminants.
1529 T : Entity_Id := Full_Type;
1532 if Is_Protected_Type (T) then
1533 T := Corresponding_Record_Type (T);
1535 elsif Is_Private_Type (T)
1536 and then Present (Underlying_Full_View (T))
1537 and then Is_Protected_Type (Underlying_Full_View (T))
1539 T := Corresponding_Record_Type (Underlying_Full_View (T));
1543 Get_Discriminant_Value (
1546 Discriminant_Constraint (Full_Type));
1549 if In_Init_Proc then
1551 -- Replace any possible references to the discriminant in the
1552 -- call to the record initialization procedure with references
1553 -- to the appropriate formal parameter.
1555 if Nkind (Arg) = N_Identifier
1556 and then Ekind (Entity (Arg)) = E_Discriminant
1558 Arg := New_Reference_To (Discriminal (Entity (Arg)), Loc);
1560 -- Case of access discriminants. We replace the reference
1561 -- to the type by a reference to the actual object
1563 elsif Nkind (Arg) = N_Attribute_Reference
1564 and then Is_Access_Type (Etype (Arg))
1565 and then Is_Entity_Name (Prefix (Arg))
1566 and then Is_Type (Entity (Prefix (Arg)))
1569 Make_Attribute_Reference (Loc,
1570 Prefix => New_Copy (Prefix (Id_Ref)),
1571 Attribute_Name => Name_Unrestricted_Access);
1573 -- Otherwise make a copy of the default expression. Note that
1574 -- we use the current Sloc for this, because we do not want the
1575 -- call to appear to be at the declaration point. Within the
1576 -- expression, replace discriminants with their discriminals.
1580 New_Copy_Tree (Arg, Map => Discr_Map, New_Sloc => Loc);
1584 if Is_Constrained (Full_Type) then
1585 Arg := Duplicate_Subexpr_No_Checks (Arg);
1587 -- The constraints come from the discriminant default exps,
1588 -- they must be reevaluated, so we use New_Copy_Tree but we
1589 -- ensure the proper Sloc (for any embedded calls).
1591 Arg := New_Copy_Tree (Arg, New_Sloc => Loc);
1595 -- Ada 2005 (AI-287): In case of default initialized components,
1596 -- if the component is constrained with a discriminant of the
1597 -- enclosing type, we need to generate the corresponding selected
1598 -- component node to access the discriminant value. In other cases
1599 -- this is not required, either because we are inside the init
1600 -- proc and we use the corresponding formal, or else because the
1601 -- component is constrained by an expression.
1603 if With_Default_Init
1604 and then Nkind (Id_Ref) = N_Selected_Component
1605 and then Nkind (Arg) = N_Identifier
1606 and then Ekind (Entity (Arg)) = E_Discriminant
1609 Make_Selected_Component (Loc,
1610 Prefix => New_Copy_Tree (Prefix (Id_Ref)),
1611 Selector_Name => Arg));
1613 Append_To (Args, Arg);
1616 Next_Discriminant (Discr);
1620 -- If this is a call to initialize the parent component of a derived
1621 -- tagged type, indicate that the tag should not be set in the parent.
1623 if Is_Tagged_Type (Full_Init_Type)
1624 and then not Is_CPP_Class (Full_Init_Type)
1625 and then Nkind (Id_Ref) = N_Selected_Component
1626 and then Chars (Selector_Name (Id_Ref)) = Name_uParent
1628 Append_To (Args, New_Occurrence_Of (Standard_False, Loc));
1630 elsif Present (Constructor_Ref) then
1631 Append_List_To (Args,
1632 New_Copy_List (Parameter_Associations (Constructor_Ref)));
1636 Make_Procedure_Call_Statement (Loc,
1637 Name => New_Occurrence_Of (Proc, Loc),
1638 Parameter_Associations => Args));
1640 if Needs_Finalization (Typ)
1641 and then Nkind (Id_Ref) = N_Selected_Component
1643 if Chars (Selector_Name (Id_Ref)) /= Name_uParent then
1644 Append_List_To (Res,
1646 Ref => New_Copy_Tree (First_Arg),
1649 Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1650 With_Attach => Make_Integer_Literal (Loc, 1)));
1652 -- If the enclosing type is an extension with new controlled
1653 -- components, it has his own record controller. If the parent
1654 -- also had a record controller, attach it to the new one.
1656 -- Build_Init_Statements relies on the fact that in this specific
1657 -- case the last statement of the result is the attach call to
1658 -- the controller. If this is changed, it must be synchronized.
1660 elsif Present (Enclos_Type)
1661 and then Has_New_Controlled_Component (Enclos_Type)
1662 and then Has_Controlled_Component (Typ)
1664 if Is_Inherently_Limited_Type (Typ) then
1665 Controller_Typ := RTE (RE_Limited_Record_Controller);
1667 Controller_Typ := RTE (RE_Record_Controller);
1670 Append_List_To (Res,
1673 Make_Selected_Component (Loc,
1674 Prefix => New_Copy_Tree (First_Arg),
1675 Selector_Name => Make_Identifier (Loc, Name_uController)),
1676 Typ => Controller_Typ,
1677 Flist_Ref => Find_Final_List (Typ, New_Copy_Tree (First_Arg)),
1678 With_Attach => Make_Integer_Literal (Loc, 1)));
1685 when RE_Not_Available =>
1687 end Build_Initialization_Call;
1689 ---------------------------
1690 -- Build_Master_Renaming --
1691 ---------------------------
1693 function Build_Master_Renaming
1695 T : Entity_Id) return Entity_Id
1697 Loc : constant Source_Ptr := Sloc (N);
1702 -- Nothing to do if there is no task hierarchy
1704 if Restriction_Active (No_Task_Hierarchy) then
1709 Make_Defining_Identifier (Loc,
1710 New_External_Name (Chars (T), 'M'));
1713 Make_Object_Renaming_Declaration (Loc,
1714 Defining_Identifier => M_Id,
1715 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
1716 Name => Make_Identifier (Loc, Name_uMaster));
1717 Insert_Before (N, Decl);
1722 when RE_Not_Available =>
1724 end Build_Master_Renaming;
1726 ---------------------------
1727 -- Build_Master_Renaming --
1728 ---------------------------
1730 procedure Build_Master_Renaming (N : Node_Id; T : Entity_Id) is
1734 -- Nothing to do if there is no task hierarchy
1736 if Restriction_Active (No_Task_Hierarchy) then
1740 M_Id := Build_Master_Renaming (N, T);
1741 Set_Master_Id (T, M_Id);
1744 when RE_Not_Available =>
1746 end Build_Master_Renaming;
1748 ----------------------------
1749 -- Build_Record_Init_Proc --
1750 ----------------------------
1752 procedure Build_Record_Init_Proc (N : Node_Id; Pe : Entity_Id) is
1753 Loc : Source_Ptr := Sloc (N);
1754 Discr_Map : constant Elist_Id := New_Elmt_List;
1755 Proc_Id : Entity_Id;
1756 Rec_Type : Entity_Id;
1757 Set_Tag : Entity_Id := Empty;
1759 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id;
1760 -- Build a assignment statement node which assigns to record component
1761 -- its default expression if defined. The assignment left hand side is
1762 -- marked Assignment_OK so that initialization of limited private
1763 -- records works correctly, Return also the adjustment call for
1764 -- controlled objects
1766 procedure Build_Discriminant_Assignments (Statement_List : List_Id);
1767 -- If the record has discriminants, adds assignment statements to
1768 -- statement list to initialize the discriminant values from the
1769 -- arguments of the initialization procedure.
1771 function Build_Init_Statements (Comp_List : Node_Id) return List_Id;
1772 -- Build a list representing a sequence of statements which initialize
1773 -- components of the given component list. This may involve building
1774 -- case statements for the variant parts.
1776 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id;
1777 -- Given a non-tagged type-derivation that declares discriminants,
1780 -- type R (R1, R2 : Integer) is record ... end record;
1782 -- type D (D1 : Integer) is new R (1, D1);
1784 -- we make the _init_proc of D be
1786 -- procedure _init_proc(X : D; D1 : Integer) is
1788 -- _init_proc( R(X), 1, D1);
1791 -- This function builds the call statement in this _init_proc.
1793 procedure Build_CPP_Init_Procedure;
1794 -- Build the tree corresponding to the procedure specification and body
1795 -- of the IC procedure that initializes the C++ part of the dispatch
1796 -- table of an Ada tagged type that is a derivation of a CPP type.
1797 -- Install it as the CPP_Init TSS.
1799 procedure Build_Init_Procedure;
1800 -- Build the tree corresponding to the procedure specification and body
1801 -- of the initialization procedure (by calling all the preceding
1802 -- auxiliary routines), and install it as the _init TSS.
1804 procedure Build_Offset_To_Top_Functions;
1805 -- Ada 2005 (AI-251): Build the tree corresponding to the procedure spec
1806 -- and body of the Offset_To_Top function that is generated when the
1807 -- parent of a type with discriminants has secondary dispatch tables.
1809 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id);
1810 -- Add range checks to components of discriminated records. S is a
1811 -- subtype indication of a record component. Check_List is a list
1812 -- to which the check actions are appended.
1814 function Component_Needs_Simple_Initialization
1815 (T : Entity_Id) return Boolean;
1816 -- Determines if a component needs simple initialization, given its type
1817 -- T. This is the same as Needs_Simple_Initialization except for the
1818 -- following difference: the types Tag and Interface_Tag, that are
1819 -- access types which would normally require simple initialization to
1820 -- null, do not require initialization as components, since they are
1821 -- explicitly initialized by other means.
1823 procedure Constrain_Array
1825 Check_List : List_Id);
1826 -- Called from Build_Record_Checks.
1827 -- Apply a list of index constraints to an unconstrained array type.
1828 -- The first parameter is the entity for the resulting subtype.
1829 -- Check_List is a list to which the check actions are appended.
1831 procedure Constrain_Index
1834 Check_List : List_Id);
1835 -- Process an index constraint in a constrained array declaration.
1836 -- The constraint can be a subtype name, or a range with or without
1837 -- an explicit subtype mark. The index is the corresponding index of the
1838 -- unconstrained array. S is the range expression. Check_List is a list
1839 -- to which the check actions are appended (called from
1840 -- Build_Record_Checks).
1842 function Parent_Subtype_Renaming_Discrims return Boolean;
1843 -- Returns True for base types N that rename discriminants, else False
1845 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean;
1846 -- Determines whether a record initialization procedure needs to be
1847 -- generated for the given record type.
1849 ----------------------
1850 -- Build_Assignment --
1851 ----------------------
1853 function Build_Assignment (Id : Entity_Id; N : Node_Id) return List_Id is
1856 Typ : constant Entity_Id := Underlying_Type (Etype (Id));
1857 Kind : Node_Kind := Nkind (N);
1863 Make_Selected_Component (Loc,
1864 Prefix => Make_Identifier (Loc, Name_uInit),
1865 Selector_Name => New_Occurrence_Of (Id, Loc));
1866 Set_Assignment_OK (Lhs);
1868 -- Case of an access attribute applied to the current instance.
1869 -- Replace the reference to the type by a reference to the actual
1870 -- object. (Note that this handles the case of the top level of
1871 -- the expression being given by such an attribute, but does not
1872 -- cover uses nested within an initial value expression. Nested
1873 -- uses are unlikely to occur in practice, but are theoretically
1874 -- possible. It is not clear how to handle them without fully
1875 -- traversing the expression. ???
1877 if Kind = N_Attribute_Reference
1878 and then (Attribute_Name (N) = Name_Unchecked_Access
1880 Attribute_Name (N) = Name_Unrestricted_Access)
1881 and then Is_Entity_Name (Prefix (N))
1882 and then Is_Type (Entity (Prefix (N)))
1883 and then Entity (Prefix (N)) = Rec_Type
1886 Make_Attribute_Reference (Loc,
1887 Prefix => Make_Identifier (Loc, Name_uInit),
1888 Attribute_Name => Name_Unrestricted_Access);
1891 -- Take a copy of Exp to ensure that later copies of this component
1892 -- declaration in derived types see the original tree, not a node
1893 -- rewritten during expansion of the init_proc. If the copy contains
1894 -- itypes, the scope of the new itypes is the init_proc being built.
1896 Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id);
1899 Make_Assignment_Statement (Loc,
1901 Expression => Exp));
1903 Set_No_Ctrl_Actions (First (Res));
1905 -- Adjust the tag if tagged (because of possible view conversions).
1906 -- Suppress the tag adjustment when VM_Target because VM tags are
1907 -- represented implicitly in objects.
1909 if Is_Tagged_Type (Typ) and then Tagged_Type_Expansion then
1911 Make_Assignment_Statement (Loc,
1913 Make_Selected_Component (Loc,
1914 Prefix => New_Copy_Tree (Lhs, New_Scope => Proc_Id),
1916 New_Reference_To (First_Tag_Component (Typ), Loc)),
1919 Unchecked_Convert_To (RTE (RE_Tag),
1921 (Node (First_Elmt (Access_Disp_Table (Typ))), Loc))));
1924 -- Adjust the component if controlled except if it is an aggregate
1925 -- that will be expanded inline.
1927 if Kind = N_Qualified_Expression then
1928 Kind := Nkind (Expression (N));
1931 if Needs_Finalization (Typ)
1932 and then not (Nkind_In (Kind, N_Aggregate, N_Extension_Aggregate))
1933 and then not Is_Inherently_Limited_Type (Typ)
1936 Ref : constant Node_Id :=
1937 New_Copy_Tree (Lhs, New_Scope => Proc_Id);
1939 Append_List_To (Res,
1943 Flist_Ref => Find_Final_List (Etype (Id), Ref),
1944 With_Attach => Make_Integer_Literal (Loc, 1)));
1951 when RE_Not_Available =>
1953 end Build_Assignment;
1955 ------------------------------------
1956 -- Build_Discriminant_Assignments --
1957 ------------------------------------
1959 procedure Build_Discriminant_Assignments (Statement_List : List_Id) is
1961 Is_Tagged : constant Boolean := Is_Tagged_Type (Rec_Type);
1964 if Has_Discriminants (Rec_Type)
1965 and then not Is_Unchecked_Union (Rec_Type)
1967 D := First_Discriminant (Rec_Type);
1969 while Present (D) loop
1971 -- Don't generate the assignment for discriminants in derived
1972 -- tagged types if the discriminant is a renaming of some
1973 -- ancestor discriminant. This initialization will be done
1974 -- when initializing the _parent field of the derived record.
1976 if Is_Tagged and then
1977 Present (Corresponding_Discriminant (D))
1983 Append_List_To (Statement_List,
1984 Build_Assignment (D,
1985 New_Reference_To (Discriminal (D), Loc)));
1988 Next_Discriminant (D);
1991 end Build_Discriminant_Assignments;
1993 --------------------------
1994 -- Build_Init_Call_Thru --
1995 --------------------------
1997 function Build_Init_Call_Thru (Parameters : List_Id) return List_Id is
1998 Parent_Proc : constant Entity_Id :=
1999 Base_Init_Proc (Etype (Rec_Type));
2001 Parent_Type : constant Entity_Id :=
2002 Etype (First_Formal (Parent_Proc));
2004 Uparent_Type : constant Entity_Id :=
2005 Underlying_Type (Parent_Type);
2007 First_Discr_Param : Node_Id;
2009 Parent_Discr : Entity_Id;
2010 First_Arg : Node_Id;
2016 -- First argument (_Init) is the object to be initialized.
2017 -- ??? not sure where to get a reasonable Loc for First_Arg
2020 OK_Convert_To (Parent_Type,
2021 New_Reference_To (Defining_Identifier (First (Parameters)), Loc));
2023 Set_Etype (First_Arg, Parent_Type);
2025 Args := New_List (Convert_Concurrent (First_Arg, Rec_Type));
2027 -- In the tasks case,
2028 -- add _Master as the value of the _Master parameter
2029 -- add _Chain as the value of the _Chain parameter.
2030 -- add _Task_Name as the value of the _Task_Name parameter.
2031 -- At the outer level, these will be variables holding the
2032 -- corresponding values obtained from GNARL or the expander.
2034 -- At inner levels, they will be the parameters passed down through
2035 -- the outer routines.
2037 First_Discr_Param := Next (First (Parameters));
2039 if Has_Task (Rec_Type) then
2040 if Restriction_Active (No_Task_Hierarchy) then
2042 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
2044 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
2047 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2048 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
2049 First_Discr_Param := Next (Next (Next (First_Discr_Param)));
2052 -- Append discriminant values
2054 if Has_Discriminants (Uparent_Type) then
2055 pragma Assert (not Is_Tagged_Type (Uparent_Type));
2057 Parent_Discr := First_Discriminant (Uparent_Type);
2058 while Present (Parent_Discr) loop
2060 -- Get the initial value for this discriminant
2061 -- ??? needs to be cleaned up to use parent_Discr_Constr
2065 Discr_Value : Elmt_Id :=
2067 (Stored_Constraint (Rec_Type));
2069 Discr : Entity_Id :=
2070 First_Stored_Discriminant (Uparent_Type);
2072 while Original_Record_Component (Parent_Discr) /= Discr loop
2073 Next_Stored_Discriminant (Discr);
2074 Next_Elmt (Discr_Value);
2077 Arg := Node (Discr_Value);
2080 -- Append it to the list
2082 if Nkind (Arg) = N_Identifier
2083 and then Ekind (Entity (Arg)) = E_Discriminant
2086 New_Reference_To (Discriminal (Entity (Arg)), Loc));
2088 -- Case of access discriminants. We replace the reference
2089 -- to the type by a reference to the actual object.
2091 -- Is above comment right??? Use of New_Copy below seems mighty
2095 Append_To (Args, New_Copy (Arg));
2098 Next_Discriminant (Parent_Discr);
2104 Make_Procedure_Call_Statement (Loc,
2105 Name => New_Occurrence_Of (Parent_Proc, Loc),
2106 Parameter_Associations => Args));
2109 end Build_Init_Call_Thru;
2111 -----------------------------------
2112 -- Build_Offset_To_Top_Functions --
2113 -----------------------------------
2115 procedure Build_Offset_To_Top_Functions is
2117 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id);
2119 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2121 -- return O.Iface_Comp'Position;
2124 ----------------------------------
2125 -- Build_Offset_To_Top_Function --
2126 ----------------------------------
2128 procedure Build_Offset_To_Top_Function (Iface_Comp : Entity_Id) is
2129 Body_Node : Node_Id;
2130 Func_Id : Entity_Id;
2131 Spec_Node : Node_Id;
2134 Func_Id := Make_Temporary (Loc, 'F');
2135 Set_DT_Offset_To_Top_Func (Iface_Comp, Func_Id);
2138 -- function Fxx (O : in Rec_Typ) return Storage_Offset;
2140 Spec_Node := New_Node (N_Function_Specification, Loc);
2141 Set_Defining_Unit_Name (Spec_Node, Func_Id);
2142 Set_Parameter_Specifications (Spec_Node, New_List (
2143 Make_Parameter_Specification (Loc,
2144 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2146 Parameter_Type => New_Reference_To (Rec_Type, Loc))));
2147 Set_Result_Definition (Spec_Node,
2148 New_Reference_To (RTE (RE_Storage_Offset), Loc));
2151 -- function Fxx (O : in Rec_Typ) return Storage_Offset is
2153 -- return O.Iface_Comp'Position;
2156 Body_Node := New_Node (N_Subprogram_Body, Loc);
2157 Set_Specification (Body_Node, Spec_Node);
2158 Set_Declarations (Body_Node, New_List);
2159 Set_Handled_Statement_Sequence (Body_Node,
2160 Make_Handled_Sequence_Of_Statements (Loc,
2161 Statements => New_List (
2162 Make_Simple_Return_Statement (Loc,
2164 Make_Attribute_Reference (Loc,
2166 Make_Selected_Component (Loc,
2167 Prefix => Make_Identifier (Loc, Name_uO),
2168 Selector_Name => New_Reference_To
2170 Attribute_Name => Name_Position)))));
2172 Set_Ekind (Func_Id, E_Function);
2173 Set_Mechanism (Func_Id, Default_Mechanism);
2174 Set_Is_Internal (Func_Id, True);
2176 if not Debug_Generated_Code then
2177 Set_Debug_Info_Off (Func_Id);
2180 Analyze (Body_Node);
2182 Append_Freeze_Action (Rec_Type, Body_Node);
2183 end Build_Offset_To_Top_Function;
2187 Ifaces_Comp_List : Elist_Id;
2188 Iface_Comp_Elmt : Elmt_Id;
2189 Iface_Comp : Node_Id;
2191 -- Start of processing for Build_Offset_To_Top_Functions
2194 -- Offset_To_Top_Functions are built only for derivations of types
2195 -- with discriminants that cover interface types.
2196 -- Nothing is needed either in case of virtual machines, since
2197 -- interfaces are handled directly by the VM.
2199 if not Is_Tagged_Type (Rec_Type)
2200 or else Etype (Rec_Type) = Rec_Type
2201 or else not Has_Discriminants (Etype (Rec_Type))
2202 or else not Tagged_Type_Expansion
2207 Collect_Interface_Components (Rec_Type, Ifaces_Comp_List);
2209 -- For each interface type with secondary dispatch table we generate
2210 -- the Offset_To_Top_Functions (required to displace the pointer in
2211 -- interface conversions)
2213 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
2214 while Present (Iface_Comp_Elmt) loop
2215 Iface_Comp := Node (Iface_Comp_Elmt);
2216 pragma Assert (Is_Interface (Related_Type (Iface_Comp)));
2218 -- If the interface is a parent of Rec_Type it shares the primary
2219 -- dispatch table and hence there is no need to build the function
2221 if not Is_Ancestor (Related_Type (Iface_Comp), Rec_Type) then
2222 Build_Offset_To_Top_Function (Iface_Comp);
2225 Next_Elmt (Iface_Comp_Elmt);
2227 end Build_Offset_To_Top_Functions;
2229 ------------------------------
2230 -- Build_CPP_Init_Procedure --
2231 ------------------------------
2233 procedure Build_CPP_Init_Procedure is
2234 Body_Node : Node_Id;
2235 Body_Stmts : List_Id;
2236 Flag_Id : Entity_Id;
2237 Flag_Decl : Node_Id;
2238 Handled_Stmt_Node : Node_Id;
2239 Init_Tags_List : List_Id;
2240 Proc_Id : Entity_Id;
2241 Proc_Spec_Node : Node_Id;
2244 -- Check cases requiring no IC routine
2246 if not Is_CPP_Class (Root_Type (Rec_Type))
2247 or else Is_CPP_Class (Rec_Type)
2248 or else CPP_Num_Prims (Rec_Type) = 0
2249 or else not Tagged_Type_Expansion
2250 or else No_Run_Time_Mode
2257 -- Flag : Boolean := False;
2259 -- procedure Typ_IC is
2262 -- Copy C++ dispatch table slots from parent
2263 -- Update C++ slots of overridden primitives
2267 Flag_Id := Make_Temporary (Loc, 'F');
2270 Make_Object_Declaration (Loc,
2271 Defining_Identifier => Flag_Id,
2272 Object_Definition =>
2273 New_Reference_To (Standard_Boolean, Loc),
2275 New_Reference_To (Standard_True, Loc));
2277 Analyze (Flag_Decl);
2278 Append_Freeze_Action (Rec_Type, Flag_Decl);
2280 Body_Stmts := New_List;
2281 Body_Node := New_Node (N_Subprogram_Body, Loc);
2283 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2286 Make_Defining_Identifier (Loc,
2287 Chars => Make_TSS_Name (Rec_Type, TSS_CPP_Init_Proc));
2289 Set_Ekind (Proc_Id, E_Procedure);
2290 Set_Is_Internal (Proc_Id);
2292 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2294 Set_Parameter_Specifications (Proc_Spec_Node, New_List);
2295 Set_Specification (Body_Node, Proc_Spec_Node);
2296 Set_Declarations (Body_Node, New_List);
2298 Init_Tags_List := Build_Inherit_CPP_Prims (Rec_Type);
2300 Append_To (Init_Tags_List,
2301 Make_Assignment_Statement (Loc,
2303 New_Reference_To (Flag_Id, Loc),
2305 New_Reference_To (Standard_False, Loc)));
2307 Append_To (Body_Stmts,
2308 Make_If_Statement (Loc,
2309 Condition => New_Occurrence_Of (Flag_Id, Loc),
2310 Then_Statements => Init_Tags_List));
2312 Handled_Stmt_Node :=
2313 New_Node (N_Handled_Sequence_Of_Statements, Loc);
2314 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2315 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2316 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2318 if not Debug_Generated_Code then
2319 Set_Debug_Info_Off (Proc_Id);
2322 -- Associate CPP_Init_Proc with type
2324 Set_Init_Proc (Rec_Type, Proc_Id);
2325 end Build_CPP_Init_Procedure;
2327 --------------------------
2328 -- Build_Init_Procedure --
2329 --------------------------
2331 procedure Build_Init_Procedure is
2332 Body_Node : Node_Id;
2333 Handled_Stmt_Node : Node_Id;
2334 Parameters : List_Id;
2335 Proc_Spec_Node : Node_Id;
2336 Body_Stmts : List_Id;
2337 Record_Extension_Node : Node_Id;
2338 Init_Tags_List : List_Id;
2341 Body_Stmts := New_List;
2342 Body_Node := New_Node (N_Subprogram_Body, Loc);
2343 Set_Ekind (Proc_Id, E_Procedure);
2345 Proc_Spec_Node := New_Node (N_Procedure_Specification, Loc);
2346 Set_Defining_Unit_Name (Proc_Spec_Node, Proc_Id);
2348 Parameters := Init_Formals (Rec_Type);
2349 Append_List_To (Parameters,
2350 Build_Discriminant_Formals (Rec_Type, True));
2352 -- For tagged types, we add a flag to indicate whether the routine
2353 -- is called to initialize a parent component in the init_proc of
2354 -- a type extension. If the flag is false, we do not set the tag
2355 -- because it has been set already in the extension.
2357 if Is_Tagged_Type (Rec_Type) then
2358 Set_Tag := Make_Temporary (Loc, 'P');
2360 Append_To (Parameters,
2361 Make_Parameter_Specification (Loc,
2362 Defining_Identifier => Set_Tag,
2363 Parameter_Type => New_Occurrence_Of (Standard_Boolean, Loc),
2364 Expression => New_Occurrence_Of (Standard_True, Loc)));
2367 Set_Parameter_Specifications (Proc_Spec_Node, Parameters);
2368 Set_Specification (Body_Node, Proc_Spec_Node);
2369 Set_Declarations (Body_Node, New_List);
2371 if Parent_Subtype_Renaming_Discrims then
2373 -- N is a Derived_Type_Definition that renames the parameters
2374 -- of the ancestor type. We initialize it by expanding our
2375 -- discriminants and call the ancestor _init_proc with a
2376 -- type-converted object
2378 Append_List_To (Body_Stmts,
2379 Build_Init_Call_Thru (Parameters));
2381 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
2382 Build_Discriminant_Assignments (Body_Stmts);
2384 if not Null_Present (Type_Definition (N)) then
2385 Append_List_To (Body_Stmts,
2386 Build_Init_Statements (
2387 Component_List (Type_Definition (N))));
2391 -- N is a Derived_Type_Definition with a possible non-empty
2392 -- extension. The initialization of a type extension consists
2393 -- in the initialization of the components in the extension.
2395 Build_Discriminant_Assignments (Body_Stmts);
2397 Record_Extension_Node :=
2398 Record_Extension_Part (Type_Definition (N));
2400 if not Null_Present (Record_Extension_Node) then
2402 Stmts : constant List_Id :=
2403 Build_Init_Statements (
2404 Component_List (Record_Extension_Node));
2407 -- The parent field must be initialized first because
2408 -- the offset of the new discriminants may depend on it
2410 Prepend_To (Body_Stmts, Remove_Head (Stmts));
2411 Append_List_To (Body_Stmts, Stmts);
2416 -- Add here the assignment to instantiate the Tag
2418 -- The assignment corresponds to the code:
2420 -- _Init._Tag := Typ'Tag;
2422 -- Suppress the tag assignment when VM_Target because VM tags are
2423 -- represented implicitly in objects. It is also suppressed in case
2424 -- of CPP_Class types because in this case the tag is initialized in
2427 if Is_Tagged_Type (Rec_Type)
2428 and then Tagged_Type_Expansion
2429 and then not No_Run_Time_Mode
2431 -- Case 1: Ada tagged types with no CPP ancestor. Set the tags of
2432 -- the actual object and invoke the IP of the parent (in this
2433 -- order). The tag must be initialized before the call to the IP
2434 -- of the parent and the assignments to other components because
2435 -- the initial value of the components may depend on the tag (eg.
2436 -- through a dispatching operation on an access to the current
2437 -- type). The tag assignment is not done when initializing the
2438 -- parent component of a type extension, because in that case the
2439 -- tag is set in the extension.
2441 if not Is_CPP_Class (Root_Type (Rec_Type)) then
2443 -- Initialize the primary tag component
2445 Init_Tags_List := New_List (
2446 Make_Assignment_Statement (Loc,
2448 Make_Selected_Component (Loc,
2449 Prefix => Make_Identifier (Loc, Name_uInit),
2452 (First_Tag_Component (Rec_Type), Loc)),
2456 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2458 -- Ada 2005 (AI-251): Initialize the secondary tags components
2459 -- located at fixed positions (tags whose position depends on
2460 -- variable size components are initialized later ---see below)
2462 if Ada_Version >= Ada_05
2463 and then not Is_Interface (Rec_Type)
2464 and then Has_Interfaces (Rec_Type)
2468 Target => Make_Identifier (Loc, Name_uInit),
2469 Stmts_List => Init_Tags_List,
2470 Fixed_Comps => True,
2471 Variable_Comps => False);
2474 Prepend_To (Body_Stmts,
2475 Make_If_Statement (Loc,
2476 Condition => New_Occurrence_Of (Set_Tag, Loc),
2477 Then_Statements => Init_Tags_List));
2479 -- Case 2: CPP type. The imported C++ constructor takes care of
2480 -- tags initialization. No action needed here because the IP
2481 -- is built by Set_CPP_Constructors; in this case the IP is a
2482 -- wrapper that invokes the C++ constructor and copies the C++
2483 -- tags locally. Done to inherit the C++ slots in Ada derivations
2486 elsif Is_CPP_Class (Rec_Type) then
2487 pragma Assert (False);
2490 -- Case 3: Combined hierarchy containing C++ types and Ada tagged
2491 -- type derivations. Derivations of imported C++ classes add a
2492 -- complication, because we cannot inhibit tag setting in the
2493 -- constructor for the parent. Hence we initialize the tag after
2494 -- the call to the parent IP (that is, in reverse order compared
2495 -- with pure Ada hierarchies ---see comment on case 1).
2498 -- Initialize the primary tag
2500 Init_Tags_List := New_List (
2501 Make_Assignment_Statement (Loc,
2503 Make_Selected_Component (Loc,
2504 Prefix => Make_Identifier (Loc, Name_uInit),
2507 (First_Tag_Component (Rec_Type), Loc)),
2511 (First_Elmt (Access_Disp_Table (Rec_Type))), Loc)));
2513 -- Ada 2005 (AI-251): Initialize the secondary tags components
2514 -- located at fixed positions (tags whose position depends on
2515 -- variable size components are initialized later ---see below)
2517 if Ada_Version >= Ada_05
2518 and then not Is_Interface (Rec_Type)
2519 and then Has_Interfaces (Rec_Type)
2523 Target => Make_Identifier (Loc, Name_uInit),
2524 Stmts_List => Init_Tags_List,
2525 Fixed_Comps => True,
2526 Variable_Comps => False);
2529 -- Initialize the tag component after invocation of parent IP.
2532 -- parent_IP(_init.parent); // Invokes the C++ constructor
2533 -- [ typIC; ] // Inherit C++ slots from parent
2540 -- Search for the call to the IP of the parent. We assume
2541 -- that the first init_proc call is for the parent.
2543 Ins_Nod := First (Body_Stmts);
2544 while Present (Next (Ins_Nod))
2545 and then (Nkind (Ins_Nod) /= N_Procedure_Call_Statement
2546 or else not Is_Init_Proc (Name (Ins_Nod)))
2551 -- The IC routine copies the inherited slots of the C+ part
2552 -- of the dispatch table from the parent and updates the
2553 -- overridden C++ slots.
2555 if CPP_Num_Prims (Rec_Type) > 0 then
2557 Init_DT : Entity_Id;
2561 Init_DT := CPP_Init_Proc (Rec_Type);
2562 pragma Assert (Present (Init_DT));
2565 Make_Procedure_Call_Statement (Loc,
2566 New_Reference_To (Init_DT, Loc));
2567 Insert_After (Ins_Nod, New_Nod);
2569 -- Update location of init tag statements
2575 Insert_List_After (Ins_Nod, Init_Tags_List);
2579 -- Ada 2005 (AI-251): Initialize the secondary tag components
2580 -- located at variable positions. We delay the generation of this
2581 -- code until here because the value of the attribute 'Position
2582 -- applied to variable size components of the parent type that
2583 -- depend on discriminants is only safely read at runtime after
2584 -- the parent components have been initialized.
2586 if Ada_Version >= Ada_05
2587 and then not Is_Interface (Rec_Type)
2588 and then Has_Interfaces (Rec_Type)
2589 and then Has_Discriminants (Etype (Rec_Type))
2590 and then Is_Variable_Size_Record (Etype (Rec_Type))
2592 Init_Tags_List := New_List;
2596 Target => Make_Identifier (Loc, Name_uInit),
2597 Stmts_List => Init_Tags_List,
2598 Fixed_Comps => False,
2599 Variable_Comps => True);
2601 if Is_Non_Empty_List (Init_Tags_List) then
2602 Append_List_To (Body_Stmts, Init_Tags_List);
2607 Handled_Stmt_Node := New_Node (N_Handled_Sequence_Of_Statements, Loc);
2608 Set_Statements (Handled_Stmt_Node, Body_Stmts);
2609 Set_Exception_Handlers (Handled_Stmt_Node, No_List);
2610 Set_Handled_Statement_Sequence (Body_Node, Handled_Stmt_Node);
2612 if not Debug_Generated_Code then
2613 Set_Debug_Info_Off (Proc_Id);
2616 -- Associate Init_Proc with type, and determine if the procedure
2617 -- is null (happens because of the Initialize_Scalars pragma case,
2618 -- where we have to generate a null procedure in case it is called
2619 -- by a client with Initialize_Scalars set). Such procedures have
2620 -- to be generated, but do not have to be called, so we mark them
2621 -- as null to suppress the call.
2623 Set_Init_Proc (Rec_Type, Proc_Id);
2625 if List_Length (Body_Stmts) = 1
2627 -- We must skip SCIL nodes because they may have been added to this
2628 -- list by Insert_Actions.
2630 and then Nkind (First_Non_SCIL_Node (Body_Stmts)) = N_Null_Statement
2631 and then VM_Target = No_VM
2633 -- Even though the init proc may be null at this time it might get
2634 -- some stuff added to it later by the VM backend.
2636 Set_Is_Null_Init_Proc (Proc_Id);
2638 end Build_Init_Procedure;
2640 ---------------------------
2641 -- Build_Init_Statements --
2642 ---------------------------
2644 function Build_Init_Statements (Comp_List : Node_Id) return List_Id is
2645 Check_List : constant List_Id := New_List;
2650 Statement_List : List_Id;
2655 Per_Object_Constraint_Components : Boolean;
2657 function Has_Access_Constraint (E : Entity_Id) return Boolean;
2658 -- Components with access discriminants that depend on the current
2659 -- instance must be initialized after all other components.
2661 ---------------------------
2662 -- Has_Access_Constraint --
2663 ---------------------------
2665 function Has_Access_Constraint (E : Entity_Id) return Boolean is
2667 T : constant Entity_Id := Etype (E);
2670 if Has_Per_Object_Constraint (E)
2671 and then Has_Discriminants (T)
2673 Disc := First_Discriminant (T);
2674 while Present (Disc) loop
2675 if Is_Access_Type (Etype (Disc)) then
2679 Next_Discriminant (Disc);
2686 end Has_Access_Constraint;
2688 -- Start of processing for Build_Init_Statements
2691 if Null_Present (Comp_List) then
2692 return New_List (Make_Null_Statement (Loc));
2695 Statement_List := New_List;
2697 -- Loop through visible declarations of task types and protected
2698 -- types moving any expanded code from the spec to the body of the
2701 if Is_Task_Record_Type (Rec_Type)
2702 or else Is_Protected_Record_Type (Rec_Type)
2705 Decl : constant Node_Id :=
2706 Parent (Corresponding_Concurrent_Type (Rec_Type));
2712 if Is_Task_Record_Type (Rec_Type) then
2713 Def := Task_Definition (Decl);
2715 Def := Protected_Definition (Decl);
2718 if Present (Def) then
2719 N1 := First (Visible_Declarations (Def));
2720 while Present (N1) loop
2724 if Nkind (N2) in N_Statement_Other_Than_Procedure_Call
2725 or else Nkind (N2) in N_Raise_xxx_Error
2726 or else Nkind (N2) = N_Procedure_Call_Statement
2728 Append_To (Statement_List,
2729 New_Copy_Tree (N2, New_Scope => Proc_Id));
2730 Rewrite (N2, Make_Null_Statement (Sloc (N2)));
2738 -- Loop through components, skipping pragmas, in 2 steps. The first
2739 -- step deals with regular components. The second step deals with
2740 -- components have per object constraints, and no explicit initia-
2743 Per_Object_Constraint_Components := False;
2745 -- First step : regular components
2747 Decl := First_Non_Pragma (Component_Items (Comp_List));
2748 while Present (Decl) loop
2751 (Subtype_Indication (Component_Definition (Decl)), Check_List);
2753 Id := Defining_Identifier (Decl);
2756 if Has_Access_Constraint (Id)
2757 and then No (Expression (Decl))
2759 -- Skip processing for now and ask for a second pass
2761 Per_Object_Constraint_Components := True;
2764 -- Case of explicit initialization
2766 if Present (Expression (Decl)) then
2767 if Is_CPP_Constructor_Call (Expression (Decl)) then
2769 Build_Initialization_Call
2772 Make_Selected_Component (Loc,
2774 Make_Identifier (Loc, Name_uInit),
2775 Selector_Name => New_Occurrence_Of (Id, Loc)),
2777 In_Init_Proc => True,
2778 Enclos_Type => Rec_Type,
2779 Discr_Map => Discr_Map,
2780 Constructor_Ref => Expression (Decl));
2782 Stmts := Build_Assignment (Id, Expression (Decl));
2785 -- Case of composite component with its own Init_Proc
2787 elsif not Is_Interface (Typ)
2788 and then Has_Non_Null_Base_Init_Proc (Typ)
2791 Build_Initialization_Call
2794 Make_Selected_Component (Loc,
2795 Prefix => Make_Identifier (Loc, Name_uInit),
2796 Selector_Name => New_Occurrence_Of (Id, Loc)),
2798 In_Init_Proc => True,
2799 Enclos_Type => Rec_Type,
2800 Discr_Map => Discr_Map);
2802 Clean_Task_Names (Typ, Proc_Id);
2804 -- Case of component needing simple initialization
2806 elsif Component_Needs_Simple_Initialization (Typ) then
2809 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id)));
2811 -- Nothing needed for this case
2817 if Present (Check_List) then
2818 Append_List_To (Statement_List, Check_List);
2821 if Present (Stmts) then
2823 -- Add the initialization of the record controller before
2824 -- the _Parent field is attached to it when the attachment
2825 -- can occur. It does not work to simply initialize the
2826 -- controller first: it must be initialized after the parent
2827 -- if the parent holds discriminants that can be used to
2828 -- compute the offset of the controller. We assume here that
2829 -- the last statement of the initialization call is the
2830 -- attachment of the parent (see Build_Initialization_Call)
2832 if Chars (Id) = Name_uController
2833 and then Rec_Type /= Etype (Rec_Type)
2834 and then Has_Controlled_Component (Etype (Rec_Type))
2835 and then Has_New_Controlled_Component (Rec_Type)
2836 and then Present (Last (Statement_List))
2838 Insert_List_Before (Last (Statement_List), Stmts);
2840 Append_List_To (Statement_List, Stmts);
2845 Next_Non_Pragma (Decl);
2848 -- Set up tasks and protected object support. This needs to be done
2849 -- before any component with a per-object access discriminant
2850 -- constraint, or any variant part (which may contain such
2851 -- components) is initialized, because the initialization of these
2852 -- components may reference the enclosing concurrent object.
2854 -- For a task record type, add the task create call and calls
2855 -- to bind any interrupt (signal) entries.
2857 if Is_Task_Record_Type (Rec_Type) then
2859 -- In the case of the restricted run time the ATCB has already
2860 -- been preallocated.
2862 if Restricted_Profile then
2863 Append_To (Statement_List,
2864 Make_Assignment_Statement (Loc,
2865 Name => Make_Selected_Component (Loc,
2866 Prefix => Make_Identifier (Loc, Name_uInit),
2867 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)),
2868 Expression => Make_Attribute_Reference (Loc,
2870 Make_Selected_Component (Loc,
2871 Prefix => Make_Identifier (Loc, Name_uInit),
2873 Make_Identifier (Loc, Name_uATCB)),
2874 Attribute_Name => Name_Unchecked_Access)));
2877 Append_To (Statement_List, Make_Task_Create_Call (Rec_Type));
2879 -- Generate the statements which map a string entry name to a
2880 -- task entry index. Note that the task may not have entries.
2882 if Entry_Names_OK then
2883 Names := Build_Entry_Names (Rec_Type);
2885 if Present (Names) then
2886 Append_To (Statement_List, Names);
2891 Task_Type : constant Entity_Id :=
2892 Corresponding_Concurrent_Type (Rec_Type);
2893 Task_Decl : constant Node_Id := Parent (Task_Type);
2894 Task_Def : constant Node_Id := Task_Definition (Task_Decl);
2899 if Present (Task_Def) then
2900 Vis_Decl := First (Visible_Declarations (Task_Def));
2901 while Present (Vis_Decl) loop
2902 Loc := Sloc (Vis_Decl);
2904 if Nkind (Vis_Decl) = N_Attribute_Definition_Clause then
2905 if Get_Attribute_Id (Chars (Vis_Decl)) =
2908 Ent := Entity (Name (Vis_Decl));
2910 if Ekind (Ent) = E_Entry then
2911 Append_To (Statement_List,
2912 Make_Procedure_Call_Statement (Loc,
2913 Name => New_Reference_To (
2914 RTE (RE_Bind_Interrupt_To_Entry), Loc),
2915 Parameter_Associations => New_List (
2916 Make_Selected_Component (Loc,
2918 Make_Identifier (Loc, Name_uInit),
2920 Make_Identifier (Loc, Name_uTask_Id)),
2921 Entry_Index_Expression (
2922 Loc, Ent, Empty, Task_Type),
2923 Expression (Vis_Decl))));
2934 -- For a protected type, add statements generated by
2935 -- Make_Initialize_Protection.
2937 if Is_Protected_Record_Type (Rec_Type) then
2938 Append_List_To (Statement_List,
2939 Make_Initialize_Protection (Rec_Type));
2941 -- Generate the statements which map a string entry name to a
2942 -- protected entry index. Note that the protected type may not
2945 if Entry_Names_OK then
2946 Names := Build_Entry_Names (Rec_Type);
2948 if Present (Names) then
2949 Append_To (Statement_List, Names);
2954 if Per_Object_Constraint_Components then
2956 -- Second pass: components with per-object constraints
2958 Decl := First_Non_Pragma (Component_Items (Comp_List));
2959 while Present (Decl) loop
2961 Id := Defining_Identifier (Decl);
2964 if Has_Access_Constraint (Id)
2965 and then No (Expression (Decl))
2967 if Has_Non_Null_Base_Init_Proc (Typ) then
2968 Append_List_To (Statement_List,
2969 Build_Initialization_Call (Loc,
2970 Make_Selected_Component (Loc,
2971 Prefix => Make_Identifier (Loc, Name_uInit),
2972 Selector_Name => New_Occurrence_Of (Id, Loc)),
2974 In_Init_Proc => True,
2975 Enclos_Type => Rec_Type,
2976 Discr_Map => Discr_Map));
2978 Clean_Task_Names (Typ, Proc_Id);
2980 elsif Component_Needs_Simple_Initialization (Typ) then
2981 Append_List_To (Statement_List,
2983 (Id, Get_Simple_Init_Val (Typ, N, Esize (Id))));
2987 Next_Non_Pragma (Decl);
2991 -- Process the variant part
2993 if Present (Variant_Part (Comp_List)) then
2994 Alt_List := New_List;
2995 Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
2996 while Present (Variant) loop
2997 Loc := Sloc (Variant);
2998 Append_To (Alt_List,
2999 Make_Case_Statement_Alternative (Loc,
3001 New_Copy_List (Discrete_Choices (Variant)),
3003 Build_Init_Statements (Component_List (Variant))));
3004 Next_Non_Pragma (Variant);
3007 -- The expression of the case statement which is a reference
3008 -- to one of the discriminants is replaced by the appropriate
3009 -- formal parameter of the initialization procedure.
3011 Append_To (Statement_List,
3012 Make_Case_Statement (Loc,
3014 New_Reference_To (Discriminal (
3015 Entity (Name (Variant_Part (Comp_List)))), Loc),
3016 Alternatives => Alt_List));
3019 -- If no initializations when generated for component declarations
3020 -- corresponding to this Statement_List, append a null statement
3021 -- to the Statement_List to make it a valid Ada tree.
3023 if Is_Empty_List (Statement_List) then
3024 Append (New_Node (N_Null_Statement, Loc), Statement_List);
3027 return Statement_List;
3030 when RE_Not_Available =>
3032 end Build_Init_Statements;
3034 -------------------------
3035 -- Build_Record_Checks --
3036 -------------------------
3038 procedure Build_Record_Checks (S : Node_Id; Check_List : List_Id) is
3039 Subtype_Mark_Id : Entity_Id;
3042 if Nkind (S) = N_Subtype_Indication then
3043 Find_Type (Subtype_Mark (S));
3044 Subtype_Mark_Id := Entity (Subtype_Mark (S));
3046 -- Remaining processing depends on type
3048 case Ekind (Subtype_Mark_Id) is
3051 Constrain_Array (S, Check_List);
3057 end Build_Record_Checks;
3059 -------------------------------------------
3060 -- Component_Needs_Simple_Initialization --
3061 -------------------------------------------
3063 function Component_Needs_Simple_Initialization
3064 (T : Entity_Id) return Boolean
3068 Needs_Simple_Initialization (T)
3069 and then not Is_RTE (T, RE_Tag)
3071 -- Ada 2005 (AI-251): Check also the tag of abstract interfaces
3073 and then not Is_RTE (T, RE_Interface_Tag);
3074 end Component_Needs_Simple_Initialization;
3076 ---------------------
3077 -- Constrain_Array --
3078 ---------------------
3080 procedure Constrain_Array
3082 Check_List : List_Id)
3084 C : constant Node_Id := Constraint (SI);
3085 Number_Of_Constraints : Nat := 0;
3090 T := Entity (Subtype_Mark (SI));
3092 if Ekind (T) in Access_Kind then
3093 T := Designated_Type (T);
3096 S := First (Constraints (C));
3098 while Present (S) loop
3099 Number_Of_Constraints := Number_Of_Constraints + 1;
3103 -- In either case, the index constraint must provide a discrete
3104 -- range for each index of the array type and the type of each
3105 -- discrete range must be the same as that of the corresponding
3106 -- index. (RM 3.6.1)
3108 S := First (Constraints (C));
3109 Index := First_Index (T);
3112 -- Apply constraints to each index type
3114 for J in 1 .. Number_Of_Constraints loop
3115 Constrain_Index (Index, S, Check_List);
3120 end Constrain_Array;
3122 ---------------------
3123 -- Constrain_Index --
3124 ---------------------
3126 procedure Constrain_Index
3129 Check_List : List_Id)
3131 T : constant Entity_Id := Etype (Index);
3134 if Nkind (S) = N_Range then
3135 Process_Range_Expr_In_Decl (S, T, Check_List);
3137 end Constrain_Index;
3139 --------------------------------------
3140 -- Parent_Subtype_Renaming_Discrims --
3141 --------------------------------------
3143 function Parent_Subtype_Renaming_Discrims return Boolean is
3148 if Base_Type (Pe) /= Pe then
3153 or else not Has_Discriminants (Pe)
3154 or else Is_Constrained (Pe)
3155 or else Is_Tagged_Type (Pe)
3160 -- If there are no explicit stored discriminants we have inherited
3161 -- the root type discriminants so far, so no renamings occurred.
3163 if First_Discriminant (Pe) = First_Stored_Discriminant (Pe) then
3167 -- Check if we have done some trivial renaming of the parent
3168 -- discriminants, i.e. something like
3170 -- type DT (X1,X2: int) is new PT (X1,X2);
3172 De := First_Discriminant (Pe);
3173 Dp := First_Discriminant (Etype (Pe));
3175 while Present (De) loop
3176 pragma Assert (Present (Dp));
3178 if Corresponding_Discriminant (De) /= Dp then
3182 Next_Discriminant (De);
3183 Next_Discriminant (Dp);
3186 return Present (Dp);
3187 end Parent_Subtype_Renaming_Discrims;
3189 ------------------------
3190 -- Requires_Init_Proc --
3191 ------------------------
3193 function Requires_Init_Proc (Rec_Id : Entity_Id) return Boolean is
3194 Comp_Decl : Node_Id;
3199 -- Definitely do not need one if specifically suppressed
3201 if Suppress_Init_Proc (Rec_Id) then
3205 -- If it is a type derived from a type with unknown discriminants,
3206 -- we cannot build an initialization procedure for it.
3208 if Has_Unknown_Discriminants (Rec_Id)
3209 or else Has_Unknown_Discriminants (Etype (Rec_Id))
3214 -- Otherwise we need to generate an initialization procedure if
3215 -- Is_CPP_Class is False and at least one of the following applies:
3217 -- 1. Discriminants are present, since they need to be initialized
3218 -- with the appropriate discriminant constraint expressions.
3219 -- However, the discriminant of an unchecked union does not
3220 -- count, since the discriminant is not present.
3222 -- 2. The type is a tagged type, since the implicit Tag component
3223 -- needs to be initialized with a pointer to the dispatch table.
3225 -- 3. The type contains tasks
3227 -- 4. One or more components has an initial value
3229 -- 5. One or more components is for a type which itself requires
3230 -- an initialization procedure.
3232 -- 6. One or more components is a type that requires simple
3233 -- initialization (see Needs_Simple_Initialization), except
3234 -- that types Tag and Interface_Tag are excluded, since fields
3235 -- of these types are initialized by other means.
3237 -- 7. The type is the record type built for a task type (since at
3238 -- the very least, Create_Task must be called)
3240 -- 8. The type is the record type built for a protected type (since
3241 -- at least Initialize_Protection must be called)
3243 -- 9. The type is marked as a public entity. The reason we add this
3244 -- case (even if none of the above apply) is to properly handle
3245 -- Initialize_Scalars. If a package is compiled without an IS
3246 -- pragma, and the client is compiled with an IS pragma, then
3247 -- the client will think an initialization procedure is present
3248 -- and call it, when in fact no such procedure is required, but
3249 -- since the call is generated, there had better be a routine
3250 -- at the other end of the call, even if it does nothing!)
3252 -- Note: the reason we exclude the CPP_Class case is because in this
3253 -- case the initialization is performed by the C++ constructors, and
3254 -- the IP is built by Set_CPP_Constructors.
3256 if Is_CPP_Class (Rec_Id) then
3259 elsif Is_Interface (Rec_Id) then
3262 elsif (Has_Discriminants (Rec_Id)
3263 and then not Is_Unchecked_Union (Rec_Id))
3264 or else Is_Tagged_Type (Rec_Id)
3265 or else Is_Concurrent_Record_Type (Rec_Id)
3266 or else Has_Task (Rec_Id)
3271 Id := First_Component (Rec_Id);
3272 while Present (Id) loop
3273 Comp_Decl := Parent (Id);
3276 if Present (Expression (Comp_Decl))
3277 or else Has_Non_Null_Base_Init_Proc (Typ)
3278 or else Component_Needs_Simple_Initialization (Typ)
3283 Next_Component (Id);
3286 -- As explained above, a record initialization procedure is needed
3287 -- for public types in case Initialize_Scalars applies to a client.
3288 -- However, such a procedure is not needed in the case where either
3289 -- of restrictions No_Initialize_Scalars or No_Default_Initialization
3290 -- applies. No_Initialize_Scalars excludes the possibility of using
3291 -- Initialize_Scalars in any partition, and No_Default_Initialization
3292 -- implies that no initialization should ever be done for objects of
3293 -- the type, so is incompatible with Initialize_Scalars.
3295 if not Restriction_Active (No_Initialize_Scalars)
3296 and then not Restriction_Active (No_Default_Initialization)
3297 and then Is_Public (Rec_Id)
3303 end Requires_Init_Proc;
3305 -- Start of processing for Build_Record_Init_Proc
3308 -- Check for value type, which means no initialization required
3310 Rec_Type := Defining_Identifier (N);
3312 if Is_Value_Type (Rec_Type) then
3316 -- This may be full declaration of a private type, in which case
3317 -- the visible entity is a record, and the private entity has been
3318 -- exchanged with it in the private part of the current package.
3319 -- The initialization procedure is built for the record type, which
3320 -- is retrievable from the private entity.
3322 if Is_Incomplete_Or_Private_Type (Rec_Type) then
3323 Rec_Type := Underlying_Type (Rec_Type);
3326 -- If there are discriminants, build the discriminant map to replace
3327 -- discriminants by their discriminals in complex bound expressions.
3328 -- These only arise for the corresponding records of synchronized types.
3330 if Is_Concurrent_Record_Type (Rec_Type)
3331 and then Has_Discriminants (Rec_Type)
3336 Disc := First_Discriminant (Rec_Type);
3337 while Present (Disc) loop
3338 Append_Elmt (Disc, Discr_Map);
3339 Append_Elmt (Discriminal (Disc), Discr_Map);
3340 Next_Discriminant (Disc);
3345 -- Derived types that have no type extension can use the initialization
3346 -- procedure of their parent and do not need a procedure of their own.
3347 -- This is only correct if there are no representation clauses for the
3348 -- type or its parent, and if the parent has in fact been frozen so
3349 -- that its initialization procedure exists.
3351 if Is_Derived_Type (Rec_Type)
3352 and then not Is_Tagged_Type (Rec_Type)
3353 and then not Is_Unchecked_Union (Rec_Type)
3354 and then not Has_New_Non_Standard_Rep (Rec_Type)
3355 and then not Parent_Subtype_Renaming_Discrims
3356 and then Has_Non_Null_Base_Init_Proc (Etype (Rec_Type))
3358 Copy_TSS (Base_Init_Proc (Etype (Rec_Type)), Rec_Type);
3360 -- Otherwise if we need an initialization procedure, then build one,
3361 -- mark it as public and inlinable and as having a completion.
3363 elsif Requires_Init_Proc (Rec_Type)
3364 or else Is_Unchecked_Union (Rec_Type)
3367 Make_Defining_Identifier (Loc,
3368 Chars => Make_Init_Proc_Name (Rec_Type));
3370 -- If No_Default_Initialization restriction is active, then we don't
3371 -- want to build an init_proc, but we need to mark that an init_proc
3372 -- would be needed if this restriction was not active (so that we can
3373 -- detect attempts to call it), so set a dummy init_proc in place.
3375 if Restriction_Active (No_Default_Initialization) then
3376 Set_Init_Proc (Rec_Type, Proc_Id);
3380 Build_Offset_To_Top_Functions;
3381 Build_CPP_Init_Procedure;
3382 Build_Init_Procedure;
3383 Set_Is_Public (Proc_Id, Is_Public (Pe));
3385 -- The initialization of protected records is not worth inlining.
3386 -- In addition, when compiled for another unit for inlining purposes,
3387 -- it may make reference to entities that have not been elaborated
3388 -- yet. The initialization of controlled records contains a nested
3389 -- clean-up procedure that makes it impractical to inline as well,
3390 -- and leads to undefined symbols if inlined in a different unit.
3391 -- Similar considerations apply to task types.
3393 if not Is_Concurrent_Type (Rec_Type)
3394 and then not Has_Task (Rec_Type)
3395 and then not Needs_Finalization (Rec_Type)
3397 Set_Is_Inlined (Proc_Id);
3400 Set_Is_Internal (Proc_Id);
3401 Set_Has_Completion (Proc_Id);
3403 if not Debug_Generated_Code then
3404 Set_Debug_Info_Off (Proc_Id);
3408 Agg : constant Node_Id :=
3409 Build_Equivalent_Record_Aggregate (Rec_Type);
3411 procedure Collect_Itypes (Comp : Node_Id);
3412 -- Generate references to itypes in the aggregate, because
3413 -- the first use of the aggregate may be in a nested scope.
3415 --------------------
3416 -- Collect_Itypes --
3417 --------------------
3419 procedure Collect_Itypes (Comp : Node_Id) is
3422 Typ : constant Entity_Id := Etype (Comp);
3425 if Is_Array_Type (Typ)
3426 and then Is_Itype (Typ)
3428 Ref := Make_Itype_Reference (Loc);
3429 Set_Itype (Ref, Typ);
3430 Append_Freeze_Action (Rec_Type, Ref);
3432 Ref := Make_Itype_Reference (Loc);
3433 Set_Itype (Ref, Etype (First_Index (Typ)));
3434 Append_Freeze_Action (Rec_Type, Ref);
3436 Sub_Aggr := First (Expressions (Comp));
3438 -- Recurse on nested arrays
3440 while Present (Sub_Aggr) loop
3441 Collect_Itypes (Sub_Aggr);
3448 -- If there is a static initialization aggregate for the type,
3449 -- generate itype references for the types of its (sub)components,
3450 -- to prevent out-of-scope errors in the resulting tree.
3451 -- The aggregate may have been rewritten as a Raise node, in which
3452 -- case there are no relevant itypes.
3455 and then Nkind (Agg) = N_Aggregate
3457 Set_Static_Initialization (Proc_Id, Agg);
3462 Comp := First (Component_Associations (Agg));
3463 while Present (Comp) loop
3464 Collect_Itypes (Expression (Comp));
3471 end Build_Record_Init_Proc;
3473 ----------------------------
3474 -- Build_Slice_Assignment --
3475 ----------------------------
3477 -- Generates the following subprogram:
3480 -- (Source, Target : Array_Type,
3481 -- Left_Lo, Left_Hi : Index;
3482 -- Right_Lo, Right_Hi : Index;
3490 -- if Left_Hi < Left_Lo then
3503 -- Target (Li1) := Source (Ri1);
3506 -- exit when Li1 = Left_Lo;
3507 -- Li1 := Index'pred (Li1);
3508 -- Ri1 := Index'pred (Ri1);
3510 -- exit when Li1 = Left_Hi;
3511 -- Li1 := Index'succ (Li1);
3512 -- Ri1 := Index'succ (Ri1);
3517 procedure Build_Slice_Assignment (Typ : Entity_Id) is
3518 Loc : constant Source_Ptr := Sloc (Typ);
3519 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
3521 Larray : constant Entity_Id := Make_Temporary (Loc, 'A');
3522 Rarray : constant Entity_Id := Make_Temporary (Loc, 'R');
3523 Left_Lo : constant Entity_Id := Make_Temporary (Loc, 'L');
3524 Left_Hi : constant Entity_Id := Make_Temporary (Loc, 'L');
3525 Right_Lo : constant Entity_Id := Make_Temporary (Loc, 'R');
3526 Right_Hi : constant Entity_Id := Make_Temporary (Loc, 'R');
3527 Rev : constant Entity_Id := Make_Temporary (Loc, 'D');
3528 -- Formal parameters of procedure
3530 Proc_Name : constant Entity_Id :=
3531 Make_Defining_Identifier (Loc,
3532 Chars => Make_TSS_Name (Typ, TSS_Slice_Assign));
3534 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L');
3535 Rnn : constant Entity_Id := Make_Temporary (Loc, 'R');
3536 -- Subscripts for left and right sides
3543 -- Build declarations for indices
3548 Make_Object_Declaration (Loc,
3549 Defining_Identifier => Lnn,
3550 Object_Definition =>
3551 New_Occurrence_Of (Index, Loc)));
3554 Make_Object_Declaration (Loc,
3555 Defining_Identifier => Rnn,
3556 Object_Definition =>
3557 New_Occurrence_Of (Index, Loc)));
3561 -- Build test for empty slice case
3564 Make_If_Statement (Loc,
3567 Left_Opnd => New_Occurrence_Of (Left_Hi, Loc),
3568 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc)),
3569 Then_Statements => New_List (Make_Simple_Return_Statement (Loc))));
3571 -- Build initializations for indices
3574 F_Init : constant List_Id := New_List;
3575 B_Init : constant List_Id := New_List;
3579 Make_Assignment_Statement (Loc,
3580 Name => New_Occurrence_Of (Lnn, Loc),
3581 Expression => New_Occurrence_Of (Left_Lo, Loc)));
3584 Make_Assignment_Statement (Loc,
3585 Name => New_Occurrence_Of (Rnn, Loc),
3586 Expression => New_Occurrence_Of (Right_Lo, Loc)));
3589 Make_Assignment_Statement (Loc,
3590 Name => New_Occurrence_Of (Lnn, Loc),
3591 Expression => New_Occurrence_Of (Left_Hi, Loc)));
3594 Make_Assignment_Statement (Loc,
3595 Name => New_Occurrence_Of (Rnn, Loc),
3596 Expression => New_Occurrence_Of (Right_Hi, Loc)));
3599 Make_If_Statement (Loc,
3600 Condition => New_Occurrence_Of (Rev, Loc),
3601 Then_Statements => B_Init,
3602 Else_Statements => F_Init));
3605 -- Now construct the assignment statement
3608 Make_Loop_Statement (Loc,
3609 Statements => New_List (
3610 Make_Assignment_Statement (Loc,
3612 Make_Indexed_Component (Loc,
3613 Prefix => New_Occurrence_Of (Larray, Loc),
3614 Expressions => New_List (New_Occurrence_Of (Lnn, Loc))),
3616 Make_Indexed_Component (Loc,
3617 Prefix => New_Occurrence_Of (Rarray, Loc),
3618 Expressions => New_List (New_Occurrence_Of (Rnn, Loc))))),
3619 End_Label => Empty);
3621 -- Build the exit condition and increment/decrement statements
3624 F_Ass : constant List_Id := New_List;
3625 B_Ass : constant List_Id := New_List;
3629 Make_Exit_Statement (Loc,
3632 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3633 Right_Opnd => New_Occurrence_Of (Left_Hi, Loc))));
3636 Make_Assignment_Statement (Loc,
3637 Name => New_Occurrence_Of (Lnn, Loc),
3639 Make_Attribute_Reference (Loc,
3641 New_Occurrence_Of (Index, Loc),
3642 Attribute_Name => Name_Succ,
3643 Expressions => New_List (
3644 New_Occurrence_Of (Lnn, Loc)))));
3647 Make_Assignment_Statement (Loc,
3648 Name => New_Occurrence_Of (Rnn, Loc),
3650 Make_Attribute_Reference (Loc,
3652 New_Occurrence_Of (Index, Loc),
3653 Attribute_Name => Name_Succ,
3654 Expressions => New_List (
3655 New_Occurrence_Of (Rnn, Loc)))));
3658 Make_Exit_Statement (Loc,
3661 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3662 Right_Opnd => New_Occurrence_Of (Left_Lo, Loc))));
3665 Make_Assignment_Statement (Loc,
3666 Name => New_Occurrence_Of (Lnn, Loc),
3668 Make_Attribute_Reference (Loc,
3670 New_Occurrence_Of (Index, Loc),
3671 Attribute_Name => Name_Pred,
3672 Expressions => New_List (
3673 New_Occurrence_Of (Lnn, Loc)))));
3676 Make_Assignment_Statement (Loc,
3677 Name => New_Occurrence_Of (Rnn, Loc),
3679 Make_Attribute_Reference (Loc,
3681 New_Occurrence_Of (Index, Loc),
3682 Attribute_Name => Name_Pred,
3683 Expressions => New_List (
3684 New_Occurrence_Of (Rnn, Loc)))));
3686 Append_To (Statements (Loops),
3687 Make_If_Statement (Loc,
3688 Condition => New_Occurrence_Of (Rev, Loc),
3689 Then_Statements => B_Ass,
3690 Else_Statements => F_Ass));
3693 Append_To (Stats, Loops);
3697 Formals : List_Id := New_List;
3700 Formals := New_List (
3701 Make_Parameter_Specification (Loc,
3702 Defining_Identifier => Larray,
3703 Out_Present => True,
3705 New_Reference_To (Base_Type (Typ), Loc)),
3707 Make_Parameter_Specification (Loc,
3708 Defining_Identifier => Rarray,
3710 New_Reference_To (Base_Type (Typ), Loc)),
3712 Make_Parameter_Specification (Loc,
3713 Defining_Identifier => Left_Lo,
3715 New_Reference_To (Index, Loc)),
3717 Make_Parameter_Specification (Loc,
3718 Defining_Identifier => Left_Hi,
3720 New_Reference_To (Index, Loc)),
3722 Make_Parameter_Specification (Loc,
3723 Defining_Identifier => Right_Lo,
3725 New_Reference_To (Index, Loc)),
3727 Make_Parameter_Specification (Loc,
3728 Defining_Identifier => Right_Hi,
3730 New_Reference_To (Index, Loc)));
3733 Make_Parameter_Specification (Loc,
3734 Defining_Identifier => Rev,
3736 New_Reference_To (Standard_Boolean, Loc)));
3739 Make_Procedure_Specification (Loc,
3740 Defining_Unit_Name => Proc_Name,
3741 Parameter_Specifications => Formals);
3744 Make_Subprogram_Body (Loc,
3745 Specification => Spec,
3746 Declarations => Decls,
3747 Handled_Statement_Sequence =>
3748 Make_Handled_Sequence_Of_Statements (Loc,
3749 Statements => Stats)));
3752 Set_TSS (Typ, Proc_Name);
3753 Set_Is_Pure (Proc_Name);
3754 end Build_Slice_Assignment;
3756 -----------------------------
3757 -- Build_Untagged_Equality --
3758 -----------------------------
3760 procedure Build_Untagged_Equality (Typ : Entity_Id) is
3768 function User_Defined_Eq (T : Entity_Id) return Entity_Id;
3769 -- Check whether the type T has a user-defined primitive equality. If so
3770 -- return it, else return Empty. If true for a component of Typ, we have
3771 -- to build the primitive equality for it.
3773 ---------------------
3774 -- User_Defined_Eq --
3775 ---------------------
3777 function User_Defined_Eq (T : Entity_Id) return Entity_Id is
3782 Op := TSS (T, TSS_Composite_Equality);
3784 if Present (Op) then
3788 Prim := First_Elmt (Collect_Primitive_Operations (T));
3789 while Present (Prim) loop
3792 if Chars (Op) = Name_Op_Eq
3793 and then Etype (Op) = Standard_Boolean
3794 and then Etype (First_Formal (Op)) = T
3795 and then Etype (Next_Formal (First_Formal (Op))) = T
3804 end User_Defined_Eq;
3806 -- Start of processing for Build_Untagged_Equality
3809 -- If a record component has a primitive equality operation, we must
3810 -- build the corresponding one for the current type.
3813 Comp := First_Component (Typ);
3814 while Present (Comp) loop
3815 if Is_Record_Type (Etype (Comp))
3816 and then Present (User_Defined_Eq (Etype (Comp)))
3821 Next_Component (Comp);
3824 -- If there is a user-defined equality for the type, we do not create
3825 -- the implicit one.
3827 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
3829 while Present (Prim) loop
3830 if Chars (Node (Prim)) = Name_Op_Eq
3831 and then Comes_From_Source (Node (Prim))
3833 -- Don't we also need to check formal types and return type as in
3834 -- User_Defined_Eq above???
3837 Eq_Op := Node (Prim);
3845 -- If the type is derived, inherit the operation, if present, from the
3846 -- parent type. It may have been declared after the type derivation. If
3847 -- the parent type itself is derived, it may have inherited an operation
3848 -- that has itself been overridden, so update its alias and related
3849 -- flags. Ditto for inequality.
3851 if No (Eq_Op) and then Is_Derived_Type (Typ) then
3852 Prim := First_Elmt (Collect_Primitive_Operations (Etype (Typ)));
3853 while Present (Prim) loop
3854 if Chars (Node (Prim)) = Name_Op_Eq then
3855 Copy_TSS (Node (Prim), Typ);
3859 Op : constant Entity_Id := User_Defined_Eq (Typ);
3860 Eq_Op : constant Entity_Id := Node (Prim);
3861 NE_Op : constant Entity_Id := Next_Entity (Eq_Op);
3864 if Present (Op) then
3865 Set_Alias (Op, Eq_Op);
3866 Set_Is_Abstract_Subprogram
3867 (Op, Is_Abstract_Subprogram (Eq_Op));
3869 if Chars (Next_Entity (Op)) = Name_Op_Ne then
3870 Set_Is_Abstract_Subprogram
3871 (Next_Entity (Op), Is_Abstract_Subprogram (NE_Op));
3883 -- If not inherited and not user-defined, build body as for a type with
3884 -- tagged components.
3888 Make_Eq_Body (Typ, Make_TSS_Name (Typ, TSS_Composite_Equality));
3889 Op := Defining_Entity (Decl);
3893 if Is_Library_Level_Entity (Typ) then
3897 end Build_Untagged_Equality;
3899 ------------------------------------
3900 -- Build_Variant_Record_Equality --
3901 ------------------------------------
3905 -- function _Equality (X, Y : T) return Boolean is
3907 -- -- Compare discriminants
3909 -- if False or else X.D1 /= Y.D1 or else X.D2 /= Y.D2 then
3913 -- -- Compare components
3915 -- if False or else X.C1 /= Y.C1 or else X.C2 /= Y.C2 then
3919 -- -- Compare variant part
3923 -- if False or else X.C2 /= Y.C2 or else X.C3 /= Y.C3 then
3928 -- if False or else X.Cn /= Y.Cn then
3936 procedure Build_Variant_Record_Equality (Typ : Entity_Id) is
3937 Loc : constant Source_Ptr := Sloc (Typ);
3939 F : constant Entity_Id :=
3940 Make_Defining_Identifier (Loc,
3941 Chars => Make_TSS_Name (Typ, TSS_Composite_Equality));
3943 X : constant Entity_Id :=
3944 Make_Defining_Identifier (Loc,
3947 Y : constant Entity_Id :=
3948 Make_Defining_Identifier (Loc,
3951 Def : constant Node_Id := Parent (Typ);
3952 Comps : constant Node_Id := Component_List (Type_Definition (Def));
3953 Stmts : constant List_Id := New_List;
3954 Pspecs : constant List_Id := New_List;
3957 -- Derived Unchecked_Union types no longer inherit the equality function
3960 if Is_Derived_Type (Typ)
3961 and then not Is_Unchecked_Union (Typ)
3962 and then not Has_New_Non_Standard_Rep (Typ)
3965 Parent_Eq : constant Entity_Id :=
3966 TSS (Root_Type (Typ), TSS_Composite_Equality);
3969 if Present (Parent_Eq) then
3970 Copy_TSS (Parent_Eq, Typ);
3977 Make_Subprogram_Body (Loc,
3979 Make_Function_Specification (Loc,
3980 Defining_Unit_Name => F,
3981 Parameter_Specifications => Pspecs,
3982 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
3983 Declarations => New_List,
3984 Handled_Statement_Sequence =>
3985 Make_Handled_Sequence_Of_Statements (Loc,
3986 Statements => Stmts)));
3989 Make_Parameter_Specification (Loc,
3990 Defining_Identifier => X,
3991 Parameter_Type => New_Reference_To (Typ, Loc)));
3994 Make_Parameter_Specification (Loc,
3995 Defining_Identifier => Y,
3996 Parameter_Type => New_Reference_To (Typ, Loc)));
3998 -- Unchecked_Unions require additional machinery to support equality.
3999 -- Two extra parameters (A and B) are added to the equality function
4000 -- parameter list in order to capture the inferred values of the
4001 -- discriminants in later calls.
4003 if Is_Unchecked_Union (Typ) then
4005 Discr_Type : constant Node_Id := Etype (First_Discriminant (Typ));
4007 A : constant Node_Id :=
4008 Make_Defining_Identifier (Loc,
4011 B : constant Node_Id :=
4012 Make_Defining_Identifier (Loc,
4016 -- Add A and B to the parameter list
4019 Make_Parameter_Specification (Loc,
4020 Defining_Identifier => A,
4021 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4024 Make_Parameter_Specification (Loc,
4025 Defining_Identifier => B,
4026 Parameter_Type => New_Reference_To (Discr_Type, Loc)));
4028 -- Generate the following header code to compare the inferred
4036 Make_If_Statement (Loc,
4039 Left_Opnd => New_Reference_To (A, Loc),
4040 Right_Opnd => New_Reference_To (B, Loc)),
4041 Then_Statements => New_List (
4042 Make_Simple_Return_Statement (Loc,
4043 Expression => New_Occurrence_Of (Standard_False, Loc)))));
4045 -- Generate component-by-component comparison. Note that we must
4046 -- propagate one of the inferred discriminant formals to act as
4047 -- the case statement switch.
4049 Append_List_To (Stmts,
4050 Make_Eq_Case (Typ, Comps, A));
4054 -- Normal case (not unchecked union)
4059 Discriminant_Specifications (Def)));
4061 Append_List_To (Stmts,
4062 Make_Eq_Case (Typ, Comps));
4066 Make_Simple_Return_Statement (Loc,
4067 Expression => New_Reference_To (Standard_True, Loc)));
4072 if not Debug_Generated_Code then
4073 Set_Debug_Info_Off (F);
4075 end Build_Variant_Record_Equality;
4077 -----------------------------
4078 -- Check_Stream_Attributes --
4079 -----------------------------
4081 procedure Check_Stream_Attributes (Typ : Entity_Id) is
4083 Par_Read : constant Boolean :=
4084 Stream_Attribute_Available (Typ, TSS_Stream_Read)
4085 and then not Has_Specified_Stream_Read (Typ);
4086 Par_Write : constant Boolean :=
4087 Stream_Attribute_Available (Typ, TSS_Stream_Write)
4088 and then not Has_Specified_Stream_Write (Typ);
4090 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type);
4091 -- Check that Comp has a user-specified Nam stream attribute
4097 procedure Check_Attr (Nam : Name_Id; TSS_Nam : TSS_Name_Type) is
4099 if not Stream_Attribute_Available (Etype (Comp), TSS_Nam) then
4100 Error_Msg_Name_1 := Nam;
4102 ("|component& in limited extension must have% attribute", Comp);
4106 -- Start of processing for Check_Stream_Attributes
4109 if Par_Read or else Par_Write then
4110 Comp := First_Component (Typ);
4111 while Present (Comp) loop
4112 if Comes_From_Source (Comp)
4113 and then Original_Record_Component (Comp) = Comp
4114 and then Is_Limited_Type (Etype (Comp))
4117 Check_Attr (Name_Read, TSS_Stream_Read);
4121 Check_Attr (Name_Write, TSS_Stream_Write);
4125 Next_Component (Comp);
4128 end Check_Stream_Attributes;
4130 -----------------------------
4131 -- Expand_Record_Extension --
4132 -----------------------------
4134 -- Add a field _parent at the beginning of the record extension. This is
4135 -- used to implement inheritance. Here are some examples of expansion:
4137 -- 1. no discriminants
4138 -- type T2 is new T1 with null record;
4140 -- type T2 is new T1 with record
4144 -- 2. renamed discriminants
4145 -- type T2 (B, C : Int) is new T1 (A => B) with record
4146 -- _Parent : T1 (A => B);
4150 -- 3. inherited discriminants
4151 -- type T2 is new T1 with record -- discriminant A inherited
4152 -- _Parent : T1 (A);
4156 procedure Expand_Record_Extension (T : Entity_Id; Def : Node_Id) is
4157 Indic : constant Node_Id := Subtype_Indication (Def);
4158 Loc : constant Source_Ptr := Sloc (Def);
4159 Rec_Ext_Part : Node_Id := Record_Extension_Part (Def);
4160 Par_Subtype : Entity_Id;
4161 Comp_List : Node_Id;
4162 Comp_Decl : Node_Id;
4165 List_Constr : constant List_Id := New_List;
4168 -- Expand_Record_Extension is called directly from the semantics, so
4169 -- we must check to see whether expansion is active before proceeding
4171 if not Expander_Active then
4175 -- This may be a derivation of an untagged private type whose full
4176 -- view is tagged, in which case the Derived_Type_Definition has no
4177 -- extension part. Build an empty one now.
4179 if No (Rec_Ext_Part) then
4181 Make_Record_Definition (Loc,
4183 Component_List => Empty,
4184 Null_Present => True);
4186 Set_Record_Extension_Part (Def, Rec_Ext_Part);
4187 Mark_Rewrite_Insertion (Rec_Ext_Part);
4190 Comp_List := Component_List (Rec_Ext_Part);
4192 Parent_N := Make_Defining_Identifier (Loc, Name_uParent);
4194 -- If the derived type inherits its discriminants the type of the
4195 -- _parent field must be constrained by the inherited discriminants
4197 if Has_Discriminants (T)
4198 and then Nkind (Indic) /= N_Subtype_Indication
4199 and then not Is_Constrained (Entity (Indic))
4201 D := First_Discriminant (T);
4202 while Present (D) loop
4203 Append_To (List_Constr, New_Occurrence_Of (D, Loc));
4204 Next_Discriminant (D);
4209 Make_Subtype_Indication (Loc,
4210 Subtype_Mark => New_Reference_To (Entity (Indic), Loc),
4212 Make_Index_Or_Discriminant_Constraint (Loc,
4213 Constraints => List_Constr)),
4216 -- Otherwise the original subtype_indication is just what is needed
4219 Par_Subtype := Process_Subtype (New_Copy_Tree (Indic), Def);
4222 Set_Parent_Subtype (T, Par_Subtype);
4225 Make_Component_Declaration (Loc,
4226 Defining_Identifier => Parent_N,
4227 Component_Definition =>
4228 Make_Component_Definition (Loc,
4229 Aliased_Present => False,
4230 Subtype_Indication => New_Reference_To (Par_Subtype, Loc)));
4232 if Null_Present (Rec_Ext_Part) then
4233 Set_Component_List (Rec_Ext_Part,
4234 Make_Component_List (Loc,
4235 Component_Items => New_List (Comp_Decl),
4236 Variant_Part => Empty,
4237 Null_Present => False));
4238 Set_Null_Present (Rec_Ext_Part, False);
4240 elsif Null_Present (Comp_List)
4241 or else Is_Empty_List (Component_Items (Comp_List))
4243 Set_Component_Items (Comp_List, New_List (Comp_Decl));
4244 Set_Null_Present (Comp_List, False);
4247 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
4250 Analyze (Comp_Decl);
4251 end Expand_Record_Extension;
4253 ------------------------------------
4254 -- Expand_N_Full_Type_Declaration --
4255 ------------------------------------
4257 procedure Expand_N_Full_Type_Declaration (N : Node_Id) is
4258 Def_Id : constant Entity_Id := Defining_Identifier (N);
4259 B_Id : constant Entity_Id := Base_Type (Def_Id);
4263 procedure Build_Master (Def_Id : Entity_Id);
4264 -- Create the master associated with Def_Id
4270 procedure Build_Master (Def_Id : Entity_Id) is
4272 -- Anonymous access types are created for the components of the
4273 -- record parameter for an entry declaration. No master is created
4276 if Has_Task (Designated_Type (Def_Id))
4277 and then Comes_From_Source (N)
4279 Build_Master_Entity (Def_Id);
4280 Build_Master_Renaming (Parent (Def_Id), Def_Id);
4282 -- Create a class-wide master because a Master_Id must be generated
4283 -- for access-to-limited-class-wide types whose root may be extended
4284 -- with task components.
4286 -- Note: This code covers access-to-limited-interfaces because they
4287 -- can be used to reference tasks implementing them.
4289 elsif Is_Class_Wide_Type (Designated_Type (Def_Id))
4290 and then Is_Limited_Type (Designated_Type (Def_Id))
4291 and then Tasking_Allowed
4293 -- Do not create a class-wide master for types whose convention is
4294 -- Java since these types cannot embed Ada tasks anyway. Note that
4295 -- the following test cannot catch the following case:
4297 -- package java.lang.Object is
4298 -- type Typ is tagged limited private;
4299 -- type Ref is access all Typ'Class;
4301 -- type Typ is tagged limited ...;
4302 -- pragma Convention (Typ, Java)
4305 -- Because the convention appears after we have done the
4306 -- processing for type Ref.
4308 and then Convention (Designated_Type (Def_Id)) /= Convention_Java
4309 and then Convention (Designated_Type (Def_Id)) /= Convention_CIL
4311 Build_Class_Wide_Master (Def_Id);
4315 -- Start of processing for Expand_N_Full_Type_Declaration
4318 if Is_Access_Type (Def_Id) then
4319 Build_Master (Def_Id);
4321 if Ekind (Def_Id) = E_Access_Protected_Subprogram_Type then
4322 Expand_Access_Protected_Subprogram_Type (N);
4325 elsif Ada_Version >= Ada_05
4326 and then Is_Array_Type (Def_Id)
4327 and then Is_Access_Type (Component_Type (Def_Id))
4328 and then Ekind (Component_Type (Def_Id)) = E_Anonymous_Access_Type
4330 Build_Master (Component_Type (Def_Id));
4332 elsif Has_Task (Def_Id) then
4333 Expand_Previous_Access_Type (Def_Id);
4335 elsif Ada_Version >= Ada_05
4337 (Is_Record_Type (Def_Id)
4338 or else (Is_Array_Type (Def_Id)
4339 and then Is_Record_Type (Component_Type (Def_Id))))
4347 -- Look for the first anonymous access type component
4349 if Is_Array_Type (Def_Id) then
4350 Comp := First_Entity (Component_Type (Def_Id));
4352 Comp := First_Entity (Def_Id);
4355 while Present (Comp) loop
4356 Typ := Etype (Comp);
4358 exit when Is_Access_Type (Typ)
4359 and then Ekind (Typ) = E_Anonymous_Access_Type;
4364 -- If found we add a renaming declaration of master_id and we
4365 -- associate it to each anonymous access type component. Do
4366 -- nothing if the access type already has a master. This will be
4367 -- the case if the array type is the packed array created for a
4368 -- user-defined array type T, where the master_id is created when
4369 -- expanding the declaration for T.
4372 and then Ekind (Typ) = E_Anonymous_Access_Type
4373 and then not Restriction_Active (No_Task_Hierarchy)
4374 and then No (Master_Id (Typ))
4376 -- Do not consider run-times with no tasking support
4378 and then RTE_Available (RE_Current_Master)
4379 and then Has_Task (Non_Limited_Designated_Type (Typ))
4381 Build_Master_Entity (Def_Id);
4382 M_Id := Build_Master_Renaming (N, Def_Id);
4384 if Is_Array_Type (Def_Id) then
4385 Comp := First_Entity (Component_Type (Def_Id));
4387 Comp := First_Entity (Def_Id);
4390 while Present (Comp) loop
4391 Typ := Etype (Comp);
4393 if Is_Access_Type (Typ)
4394 and then Ekind (Typ) = E_Anonymous_Access_Type
4396 Set_Master_Id (Typ, M_Id);
4405 Par_Id := Etype (B_Id);
4407 -- The parent type is private then we need to inherit any TSS operations
4408 -- from the full view.
4410 if Ekind (Par_Id) in Private_Kind
4411 and then Present (Full_View (Par_Id))
4413 Par_Id := Base_Type (Full_View (Par_Id));
4416 if Nkind (Type_Definition (Original_Node (N))) =
4417 N_Derived_Type_Definition
4418 and then not Is_Tagged_Type (Def_Id)
4419 and then Present (Freeze_Node (Par_Id))
4420 and then Present (TSS_Elist (Freeze_Node (Par_Id)))
4422 Ensure_Freeze_Node (B_Id);
4423 FN := Freeze_Node (B_Id);
4425 if No (TSS_Elist (FN)) then
4426 Set_TSS_Elist (FN, New_Elmt_List);
4430 T_E : constant Elist_Id := TSS_Elist (FN);
4434 Elmt := First_Elmt (TSS_Elist (Freeze_Node (Par_Id)));
4435 while Present (Elmt) loop
4436 if Chars (Node (Elmt)) /= Name_uInit then
4437 Append_Elmt (Node (Elmt), T_E);
4443 -- If the derived type itself is private with a full view, then
4444 -- associate the full view with the inherited TSS_Elist as well.
4446 if Ekind (B_Id) in Private_Kind
4447 and then Present (Full_View (B_Id))
4449 Ensure_Freeze_Node (Base_Type (Full_View (B_Id)));
4451 (Freeze_Node (Base_Type (Full_View (B_Id))), TSS_Elist (FN));
4455 end Expand_N_Full_Type_Declaration;
4457 ---------------------------------
4458 -- Expand_N_Object_Declaration --
4459 ---------------------------------
4461 -- First we do special processing for objects of a tagged type where this
4462 -- is the point at which the type is frozen. The creation of the dispatch
4463 -- table and the initialization procedure have to be deferred to this
4464 -- point, since we reference previously declared primitive subprograms.
4466 -- For all types, we call an initialization procedure if there is one
4468 procedure Expand_N_Object_Declaration (N : Node_Id) is
4469 Def_Id : constant Entity_Id := Defining_Identifier (N);
4470 Expr : constant Node_Id := Expression (N);
4471 Loc : constant Source_Ptr := Sloc (N);
4472 Typ : constant Entity_Id := Etype (Def_Id);
4473 Base_Typ : constant Entity_Id := Base_Type (Typ);
4478 Init_After : Node_Id := N;
4479 -- Node after which the init proc call is to be inserted. This is
4480 -- normally N, except for the case of a shared passive variable, in
4481 -- which case the init proc call must be inserted only after the bodies
4482 -- of the shared variable procedures have been seen.
4484 function Rewrite_As_Renaming return Boolean;
4485 -- Indicate whether to rewrite a declaration with initialization into an
4486 -- object renaming declaration (see below).
4488 -------------------------
4489 -- Rewrite_As_Renaming --
4490 -------------------------
4492 function Rewrite_As_Renaming return Boolean is
4494 return not Aliased_Present (N)
4495 and then Is_Entity_Name (Expr_Q)
4496 and then Ekind (Entity (Expr_Q)) = E_Variable
4497 and then OK_To_Rename (Entity (Expr_Q))
4498 and then Is_Entity_Name (Object_Definition (N));
4499 end Rewrite_As_Renaming;
4501 -- Start of processing for Expand_N_Object_Declaration
4504 -- Don't do anything for deferred constants. All proper actions will be
4505 -- expanded during the full declaration.
4507 if No (Expr) and Constant_Present (N) then
4511 -- Force construction of dispatch tables of library level tagged types
4513 if Tagged_Type_Expansion
4514 and then Static_Dispatch_Tables
4515 and then Is_Library_Level_Entity (Def_Id)
4516 and then Is_Library_Level_Tagged_Type (Base_Typ)
4517 and then (Ekind (Base_Typ) = E_Record_Type
4518 or else Ekind (Base_Typ) = E_Protected_Type
4519 or else Ekind (Base_Typ) = E_Task_Type)
4520 and then not Has_Dispatch_Table (Base_Typ)
4523 New_Nodes : List_Id := No_List;
4526 if Is_Concurrent_Type (Base_Typ) then
4527 New_Nodes := Make_DT (Corresponding_Record_Type (Base_Typ), N);
4529 New_Nodes := Make_DT (Base_Typ, N);
4532 if not Is_Empty_List (New_Nodes) then
4533 Insert_List_Before (N, New_Nodes);
4538 -- Make shared memory routines for shared passive variable
4540 if Is_Shared_Passive (Def_Id) then
4541 Init_After := Make_Shared_Var_Procs (N);
4544 -- If tasks being declared, make sure we have an activation chain
4545 -- defined for the tasks (has no effect if we already have one), and
4546 -- also that a Master variable is established and that the appropriate
4547 -- enclosing construct is established as a task master.
4549 if Has_Task (Typ) then
4550 Build_Activation_Chain_Entity (N);
4551 Build_Master_Entity (Def_Id);
4554 -- Build a list controller for declarations where the type is anonymous
4555 -- access and the designated type is controlled. Only declarations from
4556 -- source files receive such controllers in order to provide the same
4557 -- lifespan for any potential coextensions that may be associated with
4558 -- the object. Finalization lists of internal controlled anonymous
4559 -- access objects are already handled in Expand_N_Allocator.
4561 if Comes_From_Source (N)
4562 and then Ekind (Typ) = E_Anonymous_Access_Type
4563 and then Is_Controlled (Directly_Designated_Type (Typ))
4564 and then No (Associated_Final_Chain (Typ))
4566 Build_Final_List (N, Typ);
4569 -- Default initialization required, and no expression present
4573 -- Expand Initialize call for controlled objects. One may wonder why
4574 -- the Initialize Call is not done in the regular Init procedure
4575 -- attached to the record type. That's because the init procedure is
4576 -- recursively called on each component, including _Parent, thus the
4577 -- Init call for a controlled object would generate not only one
4578 -- Initialize call as it is required but one for each ancestor of
4579 -- its type. This processing is suppressed if No_Initialization set.
4581 if not Needs_Finalization (Typ)
4582 or else No_Initialization (N)
4586 elsif not Abort_Allowed
4587 or else not Comes_From_Source (N)
4589 Insert_Actions_After (Init_After,
4591 Ref => New_Occurrence_Of (Def_Id, Loc),
4592 Typ => Base_Type (Typ),
4593 Flist_Ref => Find_Final_List (Def_Id),
4594 With_Attach => Make_Integer_Literal (Loc, 1)));
4599 -- We need to protect the initialize call
4603 -- Initialize (...);
4605 -- Undefer_Abort.all;
4608 -- ??? this won't protect the initialize call for controlled
4609 -- components which are part of the init proc, so this block
4610 -- should probably also contain the call to _init_proc but this
4611 -- requires some code reorganization...
4614 L : constant List_Id :=
4616 (Ref => New_Occurrence_Of (Def_Id, Loc),
4617 Typ => Base_Type (Typ),
4618 Flist_Ref => Find_Final_List (Def_Id),
4619 With_Attach => Make_Integer_Literal (Loc, 1));
4621 Blk : constant Node_Id :=
4622 Make_Block_Statement (Loc,
4623 Handled_Statement_Sequence =>
4624 Make_Handled_Sequence_Of_Statements (Loc, L));
4627 Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
4628 Set_At_End_Proc (Handled_Statement_Sequence (Blk),
4629 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
4630 Insert_Actions_After (Init_After, New_List (Blk));
4631 Expand_At_End_Handler
4632 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
4636 -- Call type initialization procedure if there is one. We build the
4637 -- call and put it immediately after the object declaration, so that
4638 -- it will be expanded in the usual manner. Note that this will
4639 -- result in proper handling of defaulted discriminants.
4641 -- Need call if there is a base init proc
4643 if Has_Non_Null_Base_Init_Proc (Typ)
4645 -- Suppress call if No_Initialization set on declaration
4647 and then not No_Initialization (N)
4649 -- Suppress call for special case of value type for VM
4651 and then not Is_Value_Type (Typ)
4653 -- Suppress call if Suppress_Init_Proc set on the type. This is
4654 -- needed for the derived type case, where Suppress_Initialization
4655 -- may be set for the derived type, even if there is an init proc
4656 -- defined for the root type.
4658 and then not Suppress_Init_Proc (Typ)
4660 -- Return without initializing when No_Default_Initialization
4661 -- applies. Note that the actual restriction check occurs later,
4662 -- when the object is frozen, because we don't know yet whether
4663 -- the object is imported, which is a case where the check does
4666 if Restriction_Active (No_Default_Initialization) then
4670 -- The call to the initialization procedure does NOT freeze the
4671 -- object being initialized. This is because the call is not a
4672 -- source level call. This works fine, because the only possible
4673 -- statements depending on freeze status that can appear after the
4674 -- Init_Proc call are rep clauses which can safely appear after
4675 -- actual references to the object. Note that this call may
4676 -- subsequently be removed (if a pragma Import is encountered),
4677 -- or moved to the freeze actions for the object (e.g. if an
4678 -- address clause is applied to the object, causing it to get
4679 -- delayed freezing).
4681 Id_Ref := New_Reference_To (Def_Id, Loc);
4682 Set_Must_Not_Freeze (Id_Ref);
4683 Set_Assignment_OK (Id_Ref);
4686 Init_Expr : constant Node_Id :=
4687 Static_Initialization (Base_Init_Proc (Typ));
4689 if Present (Init_Expr) then
4691 (N, New_Copy_Tree (Init_Expr, New_Scope => Current_Scope));
4694 Initialization_Warning (Id_Ref);
4696 Insert_Actions_After (Init_After,
4697 Build_Initialization_Call (Loc, Id_Ref, Typ));
4701 -- If simple initialization is required, then set an appropriate
4702 -- simple initialization expression in place. This special
4703 -- initialization is required even though No_Init_Flag is present,
4704 -- but is not needed if there was an explicit initialization.
4706 -- An internally generated temporary needs no initialization because
4707 -- it will be assigned subsequently. In particular, there is no point
4708 -- in applying Initialize_Scalars to such a temporary.
4710 elsif Needs_Simple_Initialization
4713 and then not Has_Following_Address_Clause (N))
4714 and then not Is_Internal (Def_Id)
4715 and then not Has_Init_Expression (N)
4717 Set_No_Initialization (N, False);
4718 Set_Expression (N, Get_Simple_Init_Val (Typ, N, Esize (Def_Id)));
4719 Analyze_And_Resolve (Expression (N), Typ);
4722 -- Generate attribute for Persistent_BSS if needed
4724 if Persistent_BSS_Mode
4725 and then Comes_From_Source (N)
4726 and then Is_Potentially_Persistent_Type (Typ)
4727 and then not Has_Init_Expression (N)
4728 and then Is_Library_Level_Entity (Def_Id)
4734 Make_Linker_Section_Pragma
4735 (Def_Id, Sloc (N), ".persistent.bss");
4736 Insert_After (N, Prag);
4741 -- If access type, then we know it is null if not initialized
4743 if Is_Access_Type (Typ) then
4744 Set_Is_Known_Null (Def_Id);
4747 -- Explicit initialization present
4750 -- Obtain actual expression from qualified expression
4752 if Nkind (Expr) = N_Qualified_Expression then
4753 Expr_Q := Expression (Expr);
4758 -- When we have the appropriate type of aggregate in the expression
4759 -- (it has been determined during analysis of the aggregate by
4760 -- setting the delay flag), let's perform in place assignment and
4761 -- thus avoid creating a temporary.
4763 if Is_Delayed_Aggregate (Expr_Q) then
4764 Convert_Aggr_In_Object_Decl (N);
4766 -- Ada 2005 (AI-318-02): If the initialization expression is a call
4767 -- to a build-in-place function, then access to the declared object
4768 -- must be passed to the function. Currently we limit such functions
4769 -- to those with constrained limited result subtypes, but eventually
4770 -- plan to expand the allowed forms of functions that are treated as
4773 elsif Ada_Version >= Ada_05
4774 and then Is_Build_In_Place_Function_Call (Expr_Q)
4776 Make_Build_In_Place_Call_In_Object_Declaration (N, Expr_Q);
4778 -- The previous call expands the expression initializing the
4779 -- built-in-place object into further code that will be analyzed
4780 -- later. No further expansion needed here.
4784 -- Ada 2005 (AI-251): Rewrite the expression that initializes a
4785 -- class-wide object to ensure that we copy the full object,
4786 -- unless we are targetting a VM where interfaces are handled by
4787 -- VM itself. Note that if the root type of Typ is an ancestor
4788 -- of Expr's type, both types share the same dispatch table and
4789 -- there is no need to displace the pointer.
4791 elsif Comes_From_Source (N)
4792 and then Is_Interface (Typ)
4794 pragma Assert (Is_Class_Wide_Type (Typ));
4796 -- If the object is a return object of an inherently limited type,
4797 -- which implies build-in-place treatment, bypass the special
4798 -- treatment of class-wide interface initialization below. In this
4799 -- case, the expansion of the return statement will take care of
4800 -- creating the object (via allocator) and initializing it.
4802 if Is_Return_Object (Def_Id)
4803 and then Is_Inherently_Limited_Type (Typ)
4807 elsif Tagged_Type_Expansion then
4809 Iface : constant Entity_Id := Root_Type (Typ);
4810 Expr_N : Node_Id := Expr;
4811 Expr_Typ : Entity_Id;
4818 -- If the original node of the expression was a conversion
4819 -- to this specific class-wide interface type then we
4820 -- restore the original node to generate code that
4821 -- statically displaces the pointer to the interface
4824 if not Comes_From_Source (Expr_N)
4825 and then Nkind (Expr_N) = N_Unchecked_Type_Conversion
4826 and then Nkind (Original_Node (Expr_N)) = N_Type_Conversion
4827 and then Etype (Original_Node (Expr_N)) = Typ
4829 Rewrite (Expr_N, Original_Node (Expression (N)));
4832 -- Avoid expansion of redundant interface conversion
4834 if Is_Interface (Etype (Expr_N))
4835 and then Nkind (Expr_N) = N_Type_Conversion
4836 and then Etype (Expr_N) = Typ
4838 Expr_N := Expression (Expr_N);
4839 Set_Expression (N, Expr_N);
4842 Expr_Typ := Base_Type (Etype (Expr_N));
4844 if Is_Class_Wide_Type (Expr_Typ) then
4845 Expr_Typ := Root_Type (Expr_Typ);
4849 -- CW : I'Class := Obj;
4852 -- CW : I'Class renames TiC!(Tmp.I_Tag);
4854 if Comes_From_Source (Expr_N)
4855 and then Nkind (Expr_N) = N_Identifier
4856 and then not Is_Interface (Expr_Typ)
4857 and then (Expr_Typ = Etype (Expr_Typ)
4859 Is_Variable_Size_Record (Etype (Expr_Typ)))
4862 Make_Object_Declaration (Loc,
4863 Defining_Identifier =>
4864 Make_Temporary (Loc, 'D', Expr_N),
4865 Object_Definition =>
4866 New_Occurrence_Of (Expr_Typ, Loc),
4868 Unchecked_Convert_To (Expr_Typ,
4869 Relocate_Node (Expr_N)));
4871 -- Statically reference the tag associated with the
4875 Make_Object_Renaming_Declaration (Loc,
4876 Defining_Identifier => Make_Temporary (Loc, 'D'),
4877 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4879 Unchecked_Convert_To (Typ,
4880 Make_Selected_Component (Loc,
4883 (Defining_Identifier (Decl_1), Loc),
4886 (Find_Interface_Tag (Expr_Typ, Iface),
4892 -- IW : I'Class := Obj;
4894 -- type Equiv_Record is record ... end record;
4895 -- implicit subtype CW is <Class_Wide_Subtype>;
4896 -- Temp : CW := CW!(Obj'Address);
4897 -- IW : I'Class renames Displace (Temp, I'Tag);
4900 -- Generate the equivalent record type
4902 Expand_Subtype_From_Expr
4905 Subtype_Indic => Object_Definition (N),
4906 Exp => Expression (N));
4908 if not Is_Interface (Etype (Expression (N))) then
4909 New_Expr := Relocate_Node (Expression (N));
4912 Make_Explicit_Dereference (Loc,
4913 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4914 Make_Attribute_Reference (Loc,
4915 Prefix => Relocate_Node (Expression (N)),
4916 Attribute_Name => Name_Address)));
4920 Make_Object_Declaration (Loc,
4921 Defining_Identifier =>
4922 Make_Temporary (Loc, 'D', New_Expr),
4923 Object_Definition =>
4925 (Etype (Object_Definition (N)), Loc),
4927 Unchecked_Convert_To
4928 (Etype (Object_Definition (N)), New_Expr));
4931 Make_Object_Renaming_Declaration (Loc,
4932 Defining_Identifier => Make_Temporary (Loc, 'D'),
4933 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
4935 Unchecked_Convert_To (Typ,
4936 Make_Explicit_Dereference (Loc,
4937 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
4938 Make_Function_Call (Loc,
4940 New_Reference_To (RTE (RE_Displace), Loc),
4941 Parameter_Associations => New_List (
4942 Make_Attribute_Reference (Loc,
4945 (Defining_Identifier (Decl_1), Loc),
4946 Attribute_Name => Name_Address),
4948 Unchecked_Convert_To (RTE (RE_Tag),
4952 (Access_Disp_Table (Iface))),
4956 Insert_Action (N, Decl_1);
4957 Rewrite (N, Decl_2);
4960 -- Replace internal identifier of Decl_2 by the identifier
4961 -- found in the sources. We also have to exchange entities
4962 -- containing their defining identifiers to ensure the
4963 -- correct replacement of the object declaration by this
4964 -- object renaming declaration (because such definings
4965 -- identifier have been previously added by Enter_Name to
4966 -- the current scope). We must preserve the homonym chain
4967 -- of the source entity as well.
4969 Set_Chars (Defining_Identifier (N), Chars (Def_Id));
4970 Set_Homonym (Defining_Identifier (N), Homonym (Def_Id));
4971 Exchange_Entities (Defining_Identifier (N), Def_Id);
4978 -- In most cases, we must check that the initial value meets any
4979 -- constraint imposed by the declared type. However, there is one
4980 -- very important exception to this rule. If the entity has an
4981 -- unconstrained nominal subtype, then it acquired its constraints
4982 -- from the expression in the first place, and not only does this
4983 -- mean that the constraint check is not needed, but an attempt to
4984 -- perform the constraint check can cause order of elaboration
4987 if not Is_Constr_Subt_For_U_Nominal (Typ) then
4989 -- If this is an allocator for an aggregate that has been
4990 -- allocated in place, delay checks until assignments are
4991 -- made, because the discriminants are not initialized.
4993 if Nkind (Expr) = N_Allocator
4994 and then No_Initialization (Expr)
4998 Apply_Constraint_Check (Expr, Typ);
5000 -- If the expression has been marked as requiring a range
5001 -- generate it now and reset the flag.
5003 if Do_Range_Check (Expr) then
5004 Set_Do_Range_Check (Expr, False);
5005 Generate_Range_Check (Expr, Typ, CE_Range_Check_Failed);
5010 -- If the type is controlled and not inherently limited, then
5011 -- the target is adjusted after the copy and attached to the
5012 -- finalization list. However, no adjustment is done in the case
5013 -- where the object was initialized by a call to a function whose
5014 -- result is built in place, since no copy occurred. (Eventually
5015 -- we plan to support in-place function results for some cases
5016 -- of nonlimited types. ???) Similarly, no adjustment is required
5017 -- if we are going to rewrite the object declaration into a
5018 -- renaming declaration.
5020 if Needs_Finalization (Typ)
5021 and then not Is_Inherently_Limited_Type (Typ)
5022 and then not Rewrite_As_Renaming
5024 Insert_Actions_After (Init_After,
5026 Ref => New_Reference_To (Def_Id, Loc),
5027 Typ => Base_Type (Typ),
5028 Flist_Ref => Find_Final_List (Def_Id),
5029 With_Attach => Make_Integer_Literal (Loc, 1)));
5032 -- For tagged types, when an init value is given, the tag has to
5033 -- be re-initialized separately in order to avoid the propagation
5034 -- of a wrong tag coming from a view conversion unless the type
5035 -- is class wide (in this case the tag comes from the init value).
5036 -- Suppress the tag assignment when VM_Target because VM tags are
5037 -- represented implicitly in objects. Ditto for types that are
5038 -- CPP_CLASS, and for initializations that are aggregates, because
5039 -- they have to have the right tag.
5041 if Is_Tagged_Type (Typ)
5042 and then not Is_Class_Wide_Type (Typ)
5043 and then not Is_CPP_Class (Typ)
5044 and then Tagged_Type_Expansion
5045 and then Nkind (Expr) /= N_Aggregate
5047 -- The re-assignment of the tag has to be done even if the
5048 -- object is a constant.
5051 Make_Selected_Component (Loc,
5052 Prefix => New_Reference_To (Def_Id, Loc),
5054 New_Reference_To (First_Tag_Component (Typ), Loc));
5056 Set_Assignment_OK (New_Ref);
5058 Insert_After (Init_After,
5059 Make_Assignment_Statement (Loc,
5062 Unchecked_Convert_To (RTE (RE_Tag),
5066 (Access_Disp_Table (Base_Type (Typ)))),
5069 elsif Is_Tagged_Type (Typ)
5070 and then Is_CPP_Constructor_Call (Expr)
5072 -- The call to the initialization procedure does NOT freeze the
5073 -- object being initialized.
5075 Id_Ref := New_Reference_To (Def_Id, Loc);
5076 Set_Must_Not_Freeze (Id_Ref);
5077 Set_Assignment_OK (Id_Ref);
5079 Insert_Actions_After (Init_After,
5080 Build_Initialization_Call (Loc, Id_Ref, Typ,
5081 Constructor_Ref => Expr));
5083 -- We remove here the original call to the constructor
5084 -- to avoid its management in the backend
5086 Set_Expression (N, Empty);
5089 -- For discrete types, set the Is_Known_Valid flag if the
5090 -- initializing value is known to be valid.
5092 elsif Is_Discrete_Type (Typ) and then Expr_Known_Valid (Expr) then
5093 Set_Is_Known_Valid (Def_Id);
5095 elsif Is_Access_Type (Typ) then
5097 -- For access types set the Is_Known_Non_Null flag if the
5098 -- initializing value is known to be non-null. We can also set
5099 -- Can_Never_Be_Null if this is a constant.
5101 if Known_Non_Null (Expr) then
5102 Set_Is_Known_Non_Null (Def_Id, True);
5104 if Constant_Present (N) then
5105 Set_Can_Never_Be_Null (Def_Id);
5110 -- If validity checking on copies, validate initial expression.
5111 -- But skip this if declaration is for a generic type, since it
5112 -- makes no sense to validate generic types. Not clear if this
5113 -- can happen for legal programs, but it definitely can arise
5114 -- from previous instantiation errors.
5116 if Validity_Checks_On
5117 and then Validity_Check_Copies
5118 and then not Is_Generic_Type (Etype (Def_Id))
5120 Ensure_Valid (Expr);
5121 Set_Is_Known_Valid (Def_Id);
5125 -- Cases where the back end cannot handle the initialization directly
5126 -- In such cases, we expand an assignment that will be appropriately
5127 -- handled by Expand_N_Assignment_Statement.
5129 -- The exclusion of the unconstrained case is wrong, but for now it
5130 -- is too much trouble ???
5132 if (Is_Possibly_Unaligned_Slice (Expr)
5133 or else (Is_Possibly_Unaligned_Object (Expr)
5134 and then not Represented_As_Scalar (Etype (Expr))))
5136 -- The exclusion of the unconstrained case is wrong, but for now
5137 -- it is too much trouble ???
5139 and then not (Is_Array_Type (Etype (Expr))
5140 and then not Is_Constrained (Etype (Expr)))
5143 Stat : constant Node_Id :=
5144 Make_Assignment_Statement (Loc,
5145 Name => New_Reference_To (Def_Id, Loc),
5146 Expression => Relocate_Node (Expr));
5148 Set_Expression (N, Empty);
5149 Set_No_Initialization (N);
5150 Set_Assignment_OK (Name (Stat));
5151 Set_No_Ctrl_Actions (Stat);
5152 Insert_After_And_Analyze (Init_After, Stat);
5156 -- Final transformation, if the initializing expression is an entity
5157 -- for a variable with OK_To_Rename set, then we transform:
5163 -- X : typ renames expr
5165 -- provided that X is not aliased. The aliased case has to be
5166 -- excluded in general because Expr will not be aliased in general.
5168 if Rewrite_As_Renaming then
5170 Make_Object_Renaming_Declaration (Loc,
5171 Defining_Identifier => Defining_Identifier (N),
5172 Subtype_Mark => Object_Definition (N),
5175 -- We do not analyze this renaming declaration, because all its
5176 -- components have already been analyzed, and if we were to go
5177 -- ahead and analyze it, we would in effect be trying to generate
5178 -- another declaration of X, which won't do!
5180 Set_Renamed_Object (Defining_Identifier (N), Expr_Q);
5187 when RE_Not_Available =>
5189 end Expand_N_Object_Declaration;
5191 ---------------------------------
5192 -- Expand_N_Subtype_Indication --
5193 ---------------------------------
5195 -- Add a check on the range of the subtype. The static case is partially
5196 -- duplicated by Process_Range_Expr_In_Decl in Sem_Ch3, but we still need
5197 -- to check here for the static case in order to avoid generating
5198 -- extraneous expanded code. Also deal with validity checking.
5200 procedure Expand_N_Subtype_Indication (N : Node_Id) is
5201 Ran : constant Node_Id := Range_Expression (Constraint (N));
5202 Typ : constant Entity_Id := Entity (Subtype_Mark (N));
5205 if Nkind (Constraint (N)) = N_Range_Constraint then
5206 Validity_Check_Range (Range_Expression (Constraint (N)));
5209 if Nkind_In (Parent (N), N_Constrained_Array_Definition, N_Slice) then
5210 Apply_Range_Check (Ran, Typ);
5212 end Expand_N_Subtype_Indication;
5214 ---------------------------
5215 -- Expand_N_Variant_Part --
5216 ---------------------------
5218 -- If the last variant does not contain the Others choice, replace it with
5219 -- an N_Others_Choice node since Gigi always wants an Others. Note that we
5220 -- do not bother to call Analyze on the modified variant part, since it's
5221 -- only effect would be to compute the Others_Discrete_Choices node
5222 -- laboriously, and of course we already know the list of choices that
5223 -- corresponds to the others choice (it's the list we are replacing!)
5225 procedure Expand_N_Variant_Part (N : Node_Id) is
5226 Last_Var : constant Node_Id := Last_Non_Pragma (Variants (N));
5227 Others_Node : Node_Id;
5229 if Nkind (First (Discrete_Choices (Last_Var))) /= N_Others_Choice then
5230 Others_Node := Make_Others_Choice (Sloc (Last_Var));
5231 Set_Others_Discrete_Choices
5232 (Others_Node, Discrete_Choices (Last_Var));
5233 Set_Discrete_Choices (Last_Var, New_List (Others_Node));
5235 end Expand_N_Variant_Part;
5237 ---------------------------------
5238 -- Expand_Previous_Access_Type --
5239 ---------------------------------
5241 procedure Expand_Previous_Access_Type (Def_Id : Entity_Id) is
5242 T : Entity_Id := First_Entity (Current_Scope);
5245 -- Find all access types declared in the current scope, whose
5246 -- designated type is Def_Id. If it does not have a Master_Id,
5249 while Present (T) loop
5250 if Is_Access_Type (T)
5251 and then Designated_Type (T) = Def_Id
5252 and then No (Master_Id (T))
5254 Build_Master_Entity (Def_Id);
5255 Build_Master_Renaming (Parent (Def_Id), T);
5260 end Expand_Previous_Access_Type;
5262 ------------------------------
5263 -- Expand_Record_Controller --
5264 ------------------------------
5266 procedure Expand_Record_Controller (T : Entity_Id) is
5267 Def : Node_Id := Type_Definition (Parent (T));
5268 Comp_List : Node_Id;
5269 Comp_Decl : Node_Id;
5271 First_Comp : Node_Id;
5272 Controller_Type : Entity_Id;
5276 if Nkind (Def) = N_Derived_Type_Definition then
5277 Def := Record_Extension_Part (Def);
5280 if Null_Present (Def) then
5281 Set_Component_List (Def,
5282 Make_Component_List (Sloc (Def),
5283 Component_Items => Empty_List,
5284 Variant_Part => Empty,
5285 Null_Present => True));
5288 Comp_List := Component_List (Def);
5290 if Null_Present (Comp_List)
5291 or else Is_Empty_List (Component_Items (Comp_List))
5293 Loc := Sloc (Comp_List);
5295 Loc := Sloc (First (Component_Items (Comp_List)));
5298 if Is_Inherently_Limited_Type (T) then
5299 Controller_Type := RTE (RE_Limited_Record_Controller);
5301 Controller_Type := RTE (RE_Record_Controller);
5304 Ent := Make_Defining_Identifier (Loc, Name_uController);
5307 Make_Component_Declaration (Loc,
5308 Defining_Identifier => Ent,
5309 Component_Definition =>
5310 Make_Component_Definition (Loc,
5311 Aliased_Present => False,
5312 Subtype_Indication => New_Reference_To (Controller_Type, Loc)));
5314 if Null_Present (Comp_List)
5315 or else Is_Empty_List (Component_Items (Comp_List))
5317 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5318 Set_Null_Present (Comp_List, False);
5321 -- The controller cannot be placed before the _Parent field since
5322 -- gigi lays out field in order and _parent must be first to preserve
5323 -- the polymorphism of tagged types.
5325 First_Comp := First (Component_Items (Comp_List));
5327 if not Is_Tagged_Type (T) then
5328 Insert_Before (First_Comp, Comp_Decl);
5330 -- if T is a tagged type, place controller declaration after parent
5331 -- field and after eventual tags of interface types.
5334 while Present (First_Comp)
5336 (Chars (Defining_Identifier (First_Comp)) = Name_uParent
5337 or else Is_Tag (Defining_Identifier (First_Comp))
5339 -- Ada 2005 (AI-251): The following condition covers secondary
5340 -- tags but also the adjacent component containing the offset
5341 -- to the base of the object (component generated if the parent
5342 -- has discriminants --- see Add_Interface_Tag_Components).
5343 -- This is required to avoid the addition of the controller
5344 -- between the secondary tag and its adjacent component.
5348 (Defining_Identifier (First_Comp))))
5353 -- An empty tagged extension might consist only of the parent
5354 -- component. Otherwise insert the controller before the first
5355 -- component that is neither parent nor tag.
5357 if Present (First_Comp) then
5358 Insert_Before (First_Comp, Comp_Decl);
5360 Append (Comp_Decl, Component_Items (Comp_List));
5366 Analyze (Comp_Decl);
5367 Set_Ekind (Ent, E_Component);
5368 Init_Component_Location (Ent);
5370 -- Move the _controller entity ahead in the list of internal entities
5371 -- of the enclosing record so that it is selected instead of a
5372 -- potentially inherited one.
5375 E : constant Entity_Id := Last_Entity (T);
5379 pragma Assert (Chars (E) = Name_uController);
5381 Set_Next_Entity (E, First_Entity (T));
5382 Set_First_Entity (T, E);
5384 Comp := Next_Entity (E);
5385 while Next_Entity (Comp) /= E loop
5389 Set_Next_Entity (Comp, Empty);
5390 Set_Last_Entity (T, Comp);
5396 when RE_Not_Available =>
5398 end Expand_Record_Controller;
5400 ------------------------
5401 -- Expand_Tagged_Root --
5402 ------------------------
5404 procedure Expand_Tagged_Root (T : Entity_Id) is
5405 Def : constant Node_Id := Type_Definition (Parent (T));
5406 Comp_List : Node_Id;
5407 Comp_Decl : Node_Id;
5408 Sloc_N : Source_Ptr;
5411 if Null_Present (Def) then
5412 Set_Component_List (Def,
5413 Make_Component_List (Sloc (Def),
5414 Component_Items => Empty_List,
5415 Variant_Part => Empty,
5416 Null_Present => True));
5419 Comp_List := Component_List (Def);
5421 if Null_Present (Comp_List)
5422 or else Is_Empty_List (Component_Items (Comp_List))
5424 Sloc_N := Sloc (Comp_List);
5426 Sloc_N := Sloc (First (Component_Items (Comp_List)));
5430 Make_Component_Declaration (Sloc_N,
5431 Defining_Identifier => First_Tag_Component (T),
5432 Component_Definition =>
5433 Make_Component_Definition (Sloc_N,
5434 Aliased_Present => False,
5435 Subtype_Indication => New_Reference_To (RTE (RE_Tag), Sloc_N)));
5437 if Null_Present (Comp_List)
5438 or else Is_Empty_List (Component_Items (Comp_List))
5440 Set_Component_Items (Comp_List, New_List (Comp_Decl));
5441 Set_Null_Present (Comp_List, False);
5444 Insert_Before (First (Component_Items (Comp_List)), Comp_Decl);
5447 -- We don't Analyze the whole expansion because the tag component has
5448 -- already been analyzed previously. Here we just insure that the tree
5449 -- is coherent with the semantic decoration
5451 Find_Type (Subtype_Indication (Component_Definition (Comp_Decl)));
5454 when RE_Not_Available =>
5456 end Expand_Tagged_Root;
5458 ----------------------
5459 -- Clean_Task_Names --
5460 ----------------------
5462 procedure Clean_Task_Names
5464 Proc_Id : Entity_Id)
5468 and then not Restriction_Active (No_Implicit_Heap_Allocations)
5469 and then not Global_Discard_Names
5470 and then Tagged_Type_Expansion
5472 Set_Uses_Sec_Stack (Proc_Id);
5474 end Clean_Task_Names;
5476 ------------------------------
5477 -- Expand_Freeze_Array_Type --
5478 ------------------------------
5480 procedure Expand_Freeze_Array_Type (N : Node_Id) is
5481 Typ : constant Entity_Id := Entity (N);
5482 Comp_Typ : constant Entity_Id := Component_Type (Typ);
5483 Base : constant Entity_Id := Base_Type (Typ);
5486 if not Is_Bit_Packed_Array (Typ) then
5488 -- If the component contains tasks, so does the array type. This may
5489 -- not be indicated in the array type because the component may have
5490 -- been a private type at the point of definition. Same if component
5491 -- type is controlled.
5493 Set_Has_Task (Base, Has_Task (Comp_Typ));
5494 Set_Has_Controlled_Component (Base,
5495 Has_Controlled_Component (Comp_Typ)
5496 or else Is_Controlled (Comp_Typ));
5498 if No (Init_Proc (Base)) then
5500 -- If this is an anonymous array created for a declaration with
5501 -- an initial value, its init_proc will never be called. The
5502 -- initial value itself may have been expanded into assignments,
5503 -- in which case the object declaration is carries the
5504 -- No_Initialization flag.
5507 and then Nkind (Associated_Node_For_Itype (Base)) =
5508 N_Object_Declaration
5509 and then (Present (Expression (Associated_Node_For_Itype (Base)))
5511 No_Initialization (Associated_Node_For_Itype (Base)))
5515 -- We do not need an init proc for string or wide [wide] string,
5516 -- since the only time these need initialization in normalize or
5517 -- initialize scalars mode, and these types are treated specially
5518 -- and do not need initialization procedures.
5520 elsif Root_Type (Base) = Standard_String
5521 or else Root_Type (Base) = Standard_Wide_String
5522 or else Root_Type (Base) = Standard_Wide_Wide_String
5526 -- Otherwise we have to build an init proc for the subtype
5529 Build_Array_Init_Proc (Base, N);
5534 if Has_Controlled_Component (Base) then
5535 Build_Controlling_Procs (Base);
5537 if not Is_Limited_Type (Comp_Typ)
5538 and then Number_Dimensions (Typ) = 1
5540 Build_Slice_Assignment (Typ);
5543 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5544 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5546 Set_Associated_Final_Chain (Comp_Typ, Add_Final_Chain (Typ));
5550 -- For packed case, default initialization, except if the component type
5551 -- is itself a packed structure with an initialization procedure, or
5552 -- initialize/normalize scalars active, and we have a base type, or the
5553 -- type is public, because in that case a client might specify
5554 -- Normalize_Scalars and there better be a public Init_Proc for it.
5556 elsif (Present (Init_Proc (Component_Type (Base)))
5557 and then No (Base_Init_Proc (Base)))
5558 or else (Init_Or_Norm_Scalars and then Base = Typ)
5559 or else Is_Public (Typ)
5561 Build_Array_Init_Proc (Base, N);
5563 end Expand_Freeze_Array_Type;
5565 ------------------------------------
5566 -- Expand_Freeze_Enumeration_Type --
5567 ------------------------------------
5569 procedure Expand_Freeze_Enumeration_Type (N : Node_Id) is
5570 Typ : constant Entity_Id := Entity (N);
5571 Loc : constant Source_Ptr := Sloc (Typ);
5578 Is_Contiguous : Boolean;
5583 pragma Warnings (Off, Func);
5586 -- Various optimizations possible if given representation is contiguous
5588 Is_Contiguous := True;
5590 Ent := First_Literal (Typ);
5591 Last_Repval := Enumeration_Rep (Ent);
5594 while Present (Ent) loop
5595 if Enumeration_Rep (Ent) - Last_Repval /= 1 then
5596 Is_Contiguous := False;
5599 Last_Repval := Enumeration_Rep (Ent);
5605 if Is_Contiguous then
5606 Set_Has_Contiguous_Rep (Typ);
5607 Ent := First_Literal (Typ);
5609 Lst := New_List (New_Reference_To (Ent, Sloc (Ent)));
5612 -- Build list of literal references
5617 Ent := First_Literal (Typ);
5618 while Present (Ent) loop
5619 Append_To (Lst, New_Reference_To (Ent, Sloc (Ent)));
5625 -- Now build an array declaration
5627 -- typA : array (Natural range 0 .. num - 1) of ctype :=
5628 -- (v, v, v, v, v, ....)
5630 -- where ctype is the corresponding integer type. If the representation
5631 -- is contiguous, we only keep the first literal, which provides the
5632 -- offset for Pos_To_Rep computations.
5635 Make_Defining_Identifier (Loc,
5636 Chars => New_External_Name (Chars (Typ), 'A'));
5638 Append_Freeze_Action (Typ,
5639 Make_Object_Declaration (Loc,
5640 Defining_Identifier => Arr,
5641 Constant_Present => True,
5643 Object_Definition =>
5644 Make_Constrained_Array_Definition (Loc,
5645 Discrete_Subtype_Definitions => New_List (
5646 Make_Subtype_Indication (Loc,
5647 Subtype_Mark => New_Reference_To (Standard_Natural, Loc),
5649 Make_Range_Constraint (Loc,
5653 Make_Integer_Literal (Loc, 0),
5655 Make_Integer_Literal (Loc, Num - 1))))),
5657 Component_Definition =>
5658 Make_Component_Definition (Loc,
5659 Aliased_Present => False,
5660 Subtype_Indication => New_Reference_To (Typ, Loc))),
5663 Make_Aggregate (Loc,
5664 Expressions => Lst)));
5666 Set_Enum_Pos_To_Rep (Typ, Arr);
5668 -- Now we build the function that converts representation values to
5669 -- position values. This function has the form:
5671 -- function _Rep_To_Pos (A : etype; F : Boolean) return Integer is
5674 -- when enum-lit'Enum_Rep => return posval;
5675 -- when enum-lit'Enum_Rep => return posval;
5678 -- [raise Constraint_Error when F "invalid data"]
5683 -- Note: the F parameter determines whether the others case (no valid
5684 -- representation) raises Constraint_Error or returns a unique value
5685 -- of minus one. The latter case is used, e.g. in 'Valid code.
5687 -- Note: the reason we use Enum_Rep values in the case here is to avoid
5688 -- the code generator making inappropriate assumptions about the range
5689 -- of the values in the case where the value is invalid. ityp is a
5690 -- signed or unsigned integer type of appropriate width.
5692 -- Note: if exceptions are not supported, then we suppress the raise
5693 -- and return -1 unconditionally (this is an erroneous program in any
5694 -- case and there is no obligation to raise Constraint_Error here!) We
5695 -- also do this if pragma Restrictions (No_Exceptions) is active.
5697 -- Is this right??? What about No_Exception_Propagation???
5699 -- Representations are signed
5701 if Enumeration_Rep (First_Literal (Typ)) < 0 then
5703 -- The underlying type is signed. Reset the Is_Unsigned_Type
5704 -- explicitly, because it might have been inherited from
5707 Set_Is_Unsigned_Type (Typ, False);
5709 if Esize (Typ) <= Standard_Integer_Size then
5710 Ityp := Standard_Integer;
5712 Ityp := Universal_Integer;
5715 -- Representations are unsigned
5718 if Esize (Typ) <= Standard_Integer_Size then
5719 Ityp := RTE (RE_Unsigned);
5721 Ityp := RTE (RE_Long_Long_Unsigned);
5725 -- The body of the function is a case statement. First collect case
5726 -- alternatives, or optimize the contiguous case.
5730 -- If representation is contiguous, Pos is computed by subtracting
5731 -- the representation of the first literal.
5733 if Is_Contiguous then
5734 Ent := First_Literal (Typ);
5736 if Enumeration_Rep (Ent) = Last_Repval then
5738 -- Another special case: for a single literal, Pos is zero
5740 Pos_Expr := Make_Integer_Literal (Loc, Uint_0);
5744 Convert_To (Standard_Integer,
5745 Make_Op_Subtract (Loc,
5747 Unchecked_Convert_To (Ityp,
5748 Make_Identifier (Loc, Name_uA)),
5750 Make_Integer_Literal (Loc,
5752 Enumeration_Rep (First_Literal (Typ)))));
5756 Make_Case_Statement_Alternative (Loc,
5757 Discrete_Choices => New_List (
5758 Make_Range (Sloc (Enumeration_Rep_Expr (Ent)),
5760 Make_Integer_Literal (Loc,
5761 Intval => Enumeration_Rep (Ent)),
5763 Make_Integer_Literal (Loc, Intval => Last_Repval))),
5765 Statements => New_List (
5766 Make_Simple_Return_Statement (Loc,
5767 Expression => Pos_Expr))));
5770 Ent := First_Literal (Typ);
5771 while Present (Ent) loop
5773 Make_Case_Statement_Alternative (Loc,
5774 Discrete_Choices => New_List (
5775 Make_Integer_Literal (Sloc (Enumeration_Rep_Expr (Ent)),
5776 Intval => Enumeration_Rep (Ent))),
5778 Statements => New_List (
5779 Make_Simple_Return_Statement (Loc,
5781 Make_Integer_Literal (Loc,
5782 Intval => Enumeration_Pos (Ent))))));
5788 -- In normal mode, add the others clause with the test
5790 if not No_Exception_Handlers_Set then
5792 Make_Case_Statement_Alternative (Loc,
5793 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5794 Statements => New_List (
5795 Make_Raise_Constraint_Error (Loc,
5796 Condition => Make_Identifier (Loc, Name_uF),
5797 Reason => CE_Invalid_Data),
5798 Make_Simple_Return_Statement (Loc,
5800 Make_Integer_Literal (Loc, -1)))));
5802 -- If either of the restrictions No_Exceptions_Handlers/Propagation is
5803 -- active then return -1 (we cannot usefully raise Constraint_Error in
5804 -- this case). See description above for further details.
5808 Make_Case_Statement_Alternative (Loc,
5809 Discrete_Choices => New_List (Make_Others_Choice (Loc)),
5810 Statements => New_List (
5811 Make_Simple_Return_Statement (Loc,
5813 Make_Integer_Literal (Loc, -1)))));
5816 -- Now we can build the function body
5819 Make_Defining_Identifier (Loc, Make_TSS_Name (Typ, TSS_Rep_To_Pos));
5822 Make_Subprogram_Body (Loc,
5824 Make_Function_Specification (Loc,
5825 Defining_Unit_Name => Fent,
5826 Parameter_Specifications => New_List (
5827 Make_Parameter_Specification (Loc,
5828 Defining_Identifier =>
5829 Make_Defining_Identifier (Loc, Name_uA),
5830 Parameter_Type => New_Reference_To (Typ, Loc)),
5831 Make_Parameter_Specification (Loc,
5832 Defining_Identifier =>
5833 Make_Defining_Identifier (Loc, Name_uF),
5834 Parameter_Type => New_Reference_To (Standard_Boolean, Loc))),
5836 Result_Definition => New_Reference_To (Standard_Integer, Loc)),
5838 Declarations => Empty_List,
5840 Handled_Statement_Sequence =>
5841 Make_Handled_Sequence_Of_Statements (Loc,
5842 Statements => New_List (
5843 Make_Case_Statement (Loc,
5845 Unchecked_Convert_To (Ityp,
5846 Make_Identifier (Loc, Name_uA)),
5847 Alternatives => Lst))));
5849 Set_TSS (Typ, Fent);
5852 if not Debug_Generated_Code then
5853 Set_Debug_Info_Off (Fent);
5857 when RE_Not_Available =>
5859 end Expand_Freeze_Enumeration_Type;
5861 -------------------------------
5862 -- Expand_Freeze_Record_Type --
5863 -------------------------------
5865 procedure Expand_Freeze_Record_Type (N : Node_Id) is
5866 Def_Id : constant Node_Id := Entity (N);
5867 Type_Decl : constant Node_Id := Parent (Def_Id);
5869 Comp_Typ : Entity_Id;
5870 Has_Static_DT : Boolean := False;
5871 Predef_List : List_Id;
5873 Flist : Entity_Id := Empty;
5874 -- Finalization list allocated for the case of a type with anonymous
5875 -- access components whose designated type is potentially controlled.
5877 Renamed_Eq : Node_Id := Empty;
5878 -- Defining unit name for the predefined equality function in the case
5879 -- where the type has a primitive operation that is a renaming of
5880 -- predefined equality (but only if there is also an overriding
5881 -- user-defined equality function). Used to pass this entity from
5882 -- Make_Predefined_Primitive_Specs to Predefined_Primitive_Bodies.
5884 Wrapper_Decl_List : List_Id := No_List;
5885 Wrapper_Body_List : List_Id := No_List;
5887 -- Start of processing for Expand_Freeze_Record_Type
5890 -- Build discriminant checking functions if not a derived type (for
5891 -- derived types that are not tagged types, always use the discriminant
5892 -- checking functions of the parent type). However, for untagged types
5893 -- the derivation may have taken place before the parent was frozen, so
5894 -- we copy explicitly the discriminant checking functions from the
5895 -- parent into the components of the derived type.
5897 if not Is_Derived_Type (Def_Id)
5898 or else Has_New_Non_Standard_Rep (Def_Id)
5899 or else Is_Tagged_Type (Def_Id)
5901 Build_Discr_Checking_Funcs (Type_Decl);
5903 elsif Is_Derived_Type (Def_Id)
5904 and then not Is_Tagged_Type (Def_Id)
5906 -- If we have a derived Unchecked_Union, we do not inherit the
5907 -- discriminant checking functions from the parent type since the
5908 -- discriminants are non existent.
5910 and then not Is_Unchecked_Union (Def_Id)
5911 and then Has_Discriminants (Def_Id)
5914 Old_Comp : Entity_Id;
5918 First_Component (Base_Type (Underlying_Type (Etype (Def_Id))));
5919 Comp := First_Component (Def_Id);
5920 while Present (Comp) loop
5921 if Ekind (Comp) = E_Component
5922 and then Chars (Comp) = Chars (Old_Comp)
5924 Set_Discriminant_Checking_Func (Comp,
5925 Discriminant_Checking_Func (Old_Comp));
5928 Next_Component (Old_Comp);
5929 Next_Component (Comp);
5934 if Is_Derived_Type (Def_Id)
5935 and then Is_Limited_Type (Def_Id)
5936 and then Is_Tagged_Type (Def_Id)
5938 Check_Stream_Attributes (Def_Id);
5941 -- Update task and controlled component flags, because some of the
5942 -- component types may have been private at the point of the record
5945 Comp := First_Component (Def_Id);
5947 while Present (Comp) loop
5948 Comp_Typ := Etype (Comp);
5950 if Has_Task (Comp_Typ) then
5951 Set_Has_Task (Def_Id);
5953 -- Do not set Has_Controlled_Component on a class-wide equivalent
5954 -- type. See Make_CW_Equivalent_Type.
5956 elsif not Is_Class_Wide_Equivalent_Type (Def_Id)
5957 and then (Has_Controlled_Component (Comp_Typ)
5958 or else (Chars (Comp) /= Name_uParent
5959 and then Is_Controlled (Comp_Typ)))
5961 Set_Has_Controlled_Component (Def_Id);
5963 elsif Ekind (Comp_Typ) = E_Anonymous_Access_Type
5964 and then Needs_Finalization (Directly_Designated_Type (Comp_Typ))
5967 Flist := Add_Final_Chain (Def_Id);
5970 Set_Associated_Final_Chain (Comp_Typ, Flist);
5973 Next_Component (Comp);
5976 -- Handle constructors of non-tagged CPP_Class types
5978 if not Is_Tagged_Type (Def_Id) and then Is_CPP_Class (Def_Id) then
5979 Set_CPP_Constructors (Def_Id);
5982 -- Creation of the Dispatch Table. Note that a Dispatch Table is built
5983 -- for regular tagged types as well as for Ada types deriving from a C++
5984 -- Class, but not for tagged types directly corresponding to C++ classes
5985 -- In the later case we assume that it is created in the C++ side and we
5988 if Is_Tagged_Type (Def_Id) then
5990 Static_Dispatch_Tables
5991 and then Is_Library_Level_Tagged_Type (Def_Id);
5993 -- Add the _Tag component
5995 if Underlying_Type (Etype (Def_Id)) = Def_Id then
5996 Expand_Tagged_Root (Def_Id);
5999 if Is_CPP_Class (Def_Id) then
6000 Set_All_DT_Position (Def_Id);
6002 -- Create the tag entities with a minimum decoration
6004 if Tagged_Type_Expansion then
6005 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6008 Set_CPP_Constructors (Def_Id);
6011 if not Has_Static_DT then
6013 -- Usually inherited primitives are not delayed but the first
6014 -- Ada extension of a CPP_Class is an exception since the
6015 -- address of the inherited subprogram has to be inserted in
6016 -- the new Ada Dispatch Table and this is a freezing action.
6018 -- Similarly, if this is an inherited operation whose parent is
6019 -- not frozen yet, it is not in the DT of the parent, and we
6020 -- generate an explicit freeze node for the inherited operation
6021 -- so that it is properly inserted in the DT of the current
6025 Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Def_Id));
6029 while Present (Elmt) loop
6030 Subp := Node (Elmt);
6032 if Present (Alias (Subp)) then
6033 if Is_CPP_Class (Etype (Def_Id)) then
6034 Set_Has_Delayed_Freeze (Subp);
6036 elsif Has_Delayed_Freeze (Alias (Subp))
6037 and then not Is_Frozen (Alias (Subp))
6039 Set_Is_Frozen (Subp, False);
6040 Set_Has_Delayed_Freeze (Subp);
6049 -- Unfreeze momentarily the type to add the predefined primitives
6050 -- operations. The reason we unfreeze is so that these predefined
6051 -- operations will indeed end up as primitive operations (which
6052 -- must be before the freeze point).
6054 Set_Is_Frozen (Def_Id, False);
6056 -- Do not add the spec of predefined primitives in case of
6057 -- CPP tagged type derivations that have convention CPP.
6059 if Is_CPP_Class (Root_Type (Def_Id))
6060 and then Convention (Def_Id) = Convention_CPP
6064 -- Do not add the spec of the predefined primitives if we are
6065 -- compiling under restriction No_Dispatching_Calls
6067 elsif not Restriction_Active (No_Dispatching_Calls) then
6068 Make_Predefined_Primitive_Specs
6069 (Def_Id, Predef_List, Renamed_Eq);
6070 Insert_List_Before_And_Analyze (N, Predef_List);
6073 -- Ada 2005 (AI-391): For a nonabstract null extension, create
6074 -- wrapper functions for each nonoverridden inherited function
6075 -- with a controlling result of the type. The wrapper for such
6076 -- a function returns an extension aggregate that invokes the
6077 -- the parent function.
6079 if Ada_Version >= Ada_05
6080 and then not Is_Abstract_Type (Def_Id)
6081 and then Is_Null_Extension (Def_Id)
6083 Make_Controlling_Function_Wrappers
6084 (Def_Id, Wrapper_Decl_List, Wrapper_Body_List);
6085 Insert_List_Before_And_Analyze (N, Wrapper_Decl_List);
6088 -- Ada 2005 (AI-251): For a nonabstract type extension, build
6089 -- null procedure declarations for each set of homographic null
6090 -- procedures that are inherited from interface types but not
6091 -- overridden. This is done to ensure that the dispatch table
6092 -- entry associated with such null primitives are properly filled.
6094 if Ada_Version >= Ada_05
6095 and then Etype (Def_Id) /= Def_Id
6096 and then not Is_Abstract_Type (Def_Id)
6097 and then Has_Interfaces (Def_Id)
6099 Insert_Actions (N, Make_Null_Procedure_Specs (Def_Id));
6102 Set_Is_Frozen (Def_Id);
6103 Set_All_DT_Position (Def_Id);
6105 -- Add the controlled component before the freezing actions
6106 -- referenced in those actions.
6108 if Has_New_Controlled_Component (Def_Id) then
6109 Expand_Record_Controller (Def_Id);
6112 -- Create and decorate the tags. Suppress their creation when
6113 -- VM_Target because the dispatching mechanism is handled
6114 -- internally by the VMs.
6116 if Tagged_Type_Expansion then
6117 Append_Freeze_Actions (Def_Id, Make_Tags (Def_Id));
6119 -- Generate dispatch table of locally defined tagged type.
6120 -- Dispatch tables of library level tagged types are built
6121 -- later (see Analyze_Declarations).
6123 if not Has_Static_DT then
6124 Append_Freeze_Actions (Def_Id, Make_DT (Def_Id));
6128 -- If the type has unknown discriminants, propagate dispatching
6129 -- information to its underlying record view, which does not get
6130 -- its own dispatch table.
6132 if Is_Derived_Type (Def_Id)
6133 and then Has_Unknown_Discriminants (Def_Id)
6134 and then Present (Underlying_Record_View (Def_Id))
6137 Rep : constant Entity_Id :=
6138 Underlying_Record_View (Def_Id);
6140 Set_Access_Disp_Table
6141 (Rep, Access_Disp_Table (Def_Id));
6142 Set_Dispatch_Table_Wrappers
6143 (Rep, Dispatch_Table_Wrappers (Def_Id));
6144 Set_Primitive_Operations
6145 (Rep, Primitive_Operations (Def_Id));
6149 -- Make sure that the primitives Initialize, Adjust and Finalize
6150 -- are Frozen before other TSS subprograms. We don't want them
6153 if Is_Controlled (Def_Id) then
6154 if not Is_Limited_Type (Def_Id) then
6155 Append_Freeze_Actions (Def_Id,
6157 (Find_Prim_Op (Def_Id, Name_Adjust), Sloc (Def_Id)));
6160 Append_Freeze_Actions (Def_Id,
6162 (Find_Prim_Op (Def_Id, Name_Initialize), Sloc (Def_Id)));
6164 Append_Freeze_Actions (Def_Id,
6166 (Find_Prim_Op (Def_Id, Name_Finalize), Sloc (Def_Id)));
6169 -- Freeze rest of primitive operations. There is no need to handle
6170 -- the predefined primitives if we are compiling under restriction
6171 -- No_Dispatching_Calls
6173 if not Restriction_Active (No_Dispatching_Calls) then
6174 Append_Freeze_Actions
6175 (Def_Id, Predefined_Primitive_Freeze (Def_Id));
6179 -- In the non-tagged case, ever since Ada83 an equality function must
6180 -- be provided for variant records that are not unchecked unions.
6181 -- In Ada2012 the equality function composes, and thus must be built
6182 -- explicitly just as for tagged records.
6184 elsif Has_Discriminants (Def_Id)
6185 and then not Is_Limited_Type (Def_Id)
6188 Comps : constant Node_Id :=
6189 Component_List (Type_Definition (Type_Decl));
6193 and then Present (Variant_Part (Comps))
6195 Build_Variant_Record_Equality (Def_Id);
6199 elsif Ada_Version >= Ada_12
6200 and then Comes_From_Source (Def_Id)
6201 and then Convention (Def_Id) = Convention_Ada
6203 Build_Untagged_Equality (Def_Id);
6206 -- Before building the record initialization procedure, if we are
6207 -- dealing with a concurrent record value type, then we must go through
6208 -- the discriminants, exchanging discriminals between the concurrent
6209 -- type and the concurrent record value type. See the section "Handling
6210 -- of Discriminants" in the Einfo spec for details.
6212 if Is_Concurrent_Record_Type (Def_Id)
6213 and then Has_Discriminants (Def_Id)
6216 Ctyp : constant Entity_Id :=
6217 Corresponding_Concurrent_Type (Def_Id);
6218 Conc_Discr : Entity_Id;
6219 Rec_Discr : Entity_Id;
6223 Conc_Discr := First_Discriminant (Ctyp);
6224 Rec_Discr := First_Discriminant (Def_Id);
6225 while Present (Conc_Discr) loop
6226 Temp := Discriminal (Conc_Discr);
6227 Set_Discriminal (Conc_Discr, Discriminal (Rec_Discr));
6228 Set_Discriminal (Rec_Discr, Temp);
6230 Set_Discriminal_Link (Discriminal (Conc_Discr), Conc_Discr);
6231 Set_Discriminal_Link (Discriminal (Rec_Discr), Rec_Discr);
6233 Next_Discriminant (Conc_Discr);
6234 Next_Discriminant (Rec_Discr);
6239 if Has_Controlled_Component (Def_Id) then
6240 if No (Controller_Component (Def_Id)) then
6241 Expand_Record_Controller (Def_Id);
6244 Build_Controlling_Procs (Def_Id);
6247 Adjust_Discriminants (Def_Id);
6249 if Tagged_Type_Expansion or else not Is_Interface (Def_Id) then
6251 -- Do not need init for interfaces on e.g. CIL since they're
6252 -- abstract. Helps operation of peverify (the PE Verify tool).
6254 Build_Record_Init_Proc (Type_Decl, Def_Id);
6257 -- For tagged type that are not interfaces, build bodies of primitive
6258 -- operations. Note that we do this after building the record
6259 -- initialization procedure, since the primitive operations may need
6260 -- the initialization routine. There is no need to add predefined
6261 -- primitives of interfaces because all their predefined primitives
6264 if Is_Tagged_Type (Def_Id)
6265 and then not Is_Interface (Def_Id)
6267 -- Do not add the body of predefined primitives in case of
6268 -- CPP tagged type derivations that have convention CPP.
6270 if Is_CPP_Class (Root_Type (Def_Id))
6271 and then Convention (Def_Id) = Convention_CPP
6275 -- Do not add the body of the predefined primitives if we are
6276 -- compiling under restriction No_Dispatching_Calls or if we are
6277 -- compiling a CPP tagged type.
6279 elsif not Restriction_Active (No_Dispatching_Calls) then
6280 Predef_List := Predefined_Primitive_Bodies (Def_Id, Renamed_Eq);
6281 Append_Freeze_Actions (Def_Id, Predef_List);
6284 -- Ada 2005 (AI-391): If any wrappers were created for nonoverridden
6285 -- inherited functions, then add their bodies to the freeze actions.
6287 if Present (Wrapper_Body_List) then
6288 Append_Freeze_Actions (Def_Id, Wrapper_Body_List);
6291 -- Create extra formals for the primitive operations of the type.
6292 -- This must be done before analyzing the body of the initialization
6293 -- procedure, because a self-referential type might call one of these
6294 -- primitives in the body of the init_proc itself.
6301 Elmt := First_Elmt (Primitive_Operations (Def_Id));
6302 while Present (Elmt) loop
6303 Subp := Node (Elmt);
6304 if not Has_Foreign_Convention (Subp)
6305 and then not Is_Predefined_Dispatching_Operation (Subp)
6307 Create_Extra_Formals (Subp);
6314 end Expand_Freeze_Record_Type;
6316 ------------------------------
6317 -- Freeze_Stream_Operations --
6318 ------------------------------
6320 procedure Freeze_Stream_Operations (N : Node_Id; Typ : Entity_Id) is
6321 Names : constant array (1 .. 4) of TSS_Name_Type :=
6326 Stream_Op : Entity_Id;
6329 -- Primitive operations of tagged types are frozen when the dispatch
6330 -- table is constructed.
6332 if not Comes_From_Source (Typ)
6333 or else Is_Tagged_Type (Typ)
6338 for J in Names'Range loop
6339 Stream_Op := TSS (Typ, Names (J));
6341 if Present (Stream_Op)
6342 and then Is_Subprogram (Stream_Op)
6343 and then Nkind (Unit_Declaration_Node (Stream_Op)) =
6344 N_Subprogram_Declaration
6345 and then not Is_Frozen (Stream_Op)
6347 Append_Freeze_Actions
6348 (Typ, Freeze_Entity (Stream_Op, Sloc (N)));
6351 end Freeze_Stream_Operations;
6357 -- Full type declarations are expanded at the point at which the type is
6358 -- frozen. The formal N is the Freeze_Node for the type. Any statements or
6359 -- declarations generated by the freezing (e.g. the procedure generated
6360 -- for initialization) are chained in the Actions field list of the freeze
6361 -- node using Append_Freeze_Actions.
6363 function Freeze_Type (N : Node_Id) return Boolean is
6364 Def_Id : constant Entity_Id := Entity (N);
6365 RACW_Seen : Boolean := False;
6366 Result : Boolean := False;
6369 -- Process associated access types needing special processing
6371 if Present (Access_Types_To_Process (N)) then
6373 E : Elmt_Id := First_Elmt (Access_Types_To_Process (N));
6375 while Present (E) loop
6377 if Is_Remote_Access_To_Class_Wide_Type (Node (E)) then
6378 Validate_RACW_Primitives (Node (E));
6388 -- If there are RACWs designating this type, make stubs now
6390 Remote_Types_Tagged_Full_View_Encountered (Def_Id);
6394 -- Freeze processing for record types
6396 if Is_Record_Type (Def_Id) then
6397 if Ekind (Def_Id) = E_Record_Type then
6398 Expand_Freeze_Record_Type (N);
6400 -- The subtype may have been declared before the type was frozen. If
6401 -- the type has controlled components it is necessary to create the
6402 -- entity for the controller explicitly because it did not exist at
6403 -- the point of the subtype declaration. Only the entity is needed,
6404 -- the back-end will obtain the layout from the type. This is only
6405 -- necessary if this is constrained subtype whose component list is
6406 -- not shared with the base type.
6408 elsif Ekind (Def_Id) = E_Record_Subtype
6409 and then Has_Discriminants (Def_Id)
6410 and then Last_Entity (Def_Id) /= Last_Entity (Base_Type (Def_Id))
6411 and then Present (Controller_Component (Def_Id))
6414 Old_C : constant Entity_Id := Controller_Component (Def_Id);
6418 if Scope (Old_C) = Base_Type (Def_Id) then
6420 -- The entity is the one in the parent. Create new one
6422 New_C := New_Copy (Old_C);
6423 Set_Parent (New_C, Parent (Old_C));
6424 Push_Scope (Def_Id);
6430 if Is_Itype (Def_Id)
6431 and then Is_Record_Type (Underlying_Type (Scope (Def_Id)))
6433 -- The freeze node is only used to introduce the controller,
6434 -- the back-end has no use for it for a discriminated
6437 Set_Freeze_Node (Def_Id, Empty);
6438 Set_Has_Delayed_Freeze (Def_Id, False);
6442 -- Similar process if the controller of the subtype is not present
6443 -- but the parent has it. This can happen with constrained
6444 -- record components where the subtype is an itype.
6446 elsif Ekind (Def_Id) = E_Record_Subtype
6447 and then Is_Itype (Def_Id)
6448 and then No (Controller_Component (Def_Id))
6449 and then Present (Controller_Component (Etype (Def_Id)))
6452 Old_C : constant Entity_Id :=
6453 Controller_Component (Etype (Def_Id));
6454 New_C : constant Entity_Id := New_Copy (Old_C);
6457 Set_Next_Entity (New_C, First_Entity (Def_Id));
6458 Set_First_Entity (Def_Id, New_C);
6460 -- The freeze node is only used to introduce the controller,
6461 -- the back-end has no use for it for a discriminated
6464 Set_Freeze_Node (Def_Id, Empty);
6465 Set_Has_Delayed_Freeze (Def_Id, False);
6470 -- Freeze processing for array types
6472 elsif Is_Array_Type (Def_Id) then
6473 Expand_Freeze_Array_Type (N);
6475 -- Freeze processing for access types
6477 -- For pool-specific access types, find out the pool object used for
6478 -- this type, needs actual expansion of it in some cases. Here are the
6479 -- different cases :
6481 -- 1. Rep Clause "for Def_Id'Storage_Size use 0;"
6482 -- ---> don't use any storage pool
6484 -- 2. Rep Clause : for Def_Id'Storage_Size use Expr.
6486 -- Def_Id__Pool : Stack_Bounded_Pool (Expr, DT'Size, DT'Alignment);
6488 -- 3. Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6489 -- ---> Storage Pool is the specified one
6491 -- See GNAT Pool packages in the Run-Time for more details
6493 elsif Ekind_In (Def_Id, E_Access_Type, E_General_Access_Type) then
6495 Loc : constant Source_Ptr := Sloc (N);
6496 Desig_Type : constant Entity_Id := Designated_Type (Def_Id);
6497 Pool_Object : Entity_Id;
6499 Freeze_Action_Typ : Entity_Id;
6504 -- Rep Clause "for Def_Id'Storage_Size use 0;"
6505 -- ---> don't use any storage pool
6507 if No_Pool_Assigned (Def_Id) then
6512 -- Rep Clause : for Def_Id'Storage_Size use Expr.
6514 -- Def_Id__Pool : Stack_Bounded_Pool
6515 -- (Expr, DT'Size, DT'Alignment);
6517 elsif Has_Storage_Size_Clause (Def_Id) then
6523 -- For unconstrained composite types we give a size of zero
6524 -- so that the pool knows that it needs a special algorithm
6525 -- for variable size object allocation.
6527 if Is_Composite_Type (Desig_Type)
6528 and then not Is_Constrained (Desig_Type)
6531 Make_Integer_Literal (Loc, 0);
6534 Make_Integer_Literal (Loc, Maximum_Alignment);
6538 Make_Attribute_Reference (Loc,
6539 Prefix => New_Reference_To (Desig_Type, Loc),
6540 Attribute_Name => Name_Max_Size_In_Storage_Elements);
6543 Make_Attribute_Reference (Loc,
6544 Prefix => New_Reference_To (Desig_Type, Loc),
6545 Attribute_Name => Name_Alignment);
6549 Make_Defining_Identifier (Loc,
6550 Chars => New_External_Name (Chars (Def_Id), 'P'));
6552 -- We put the code associated with the pools in the entity
6553 -- that has the later freeze node, usually the access type
6554 -- but it can also be the designated_type; because the pool
6555 -- code requires both those types to be frozen
6557 if Is_Frozen (Desig_Type)
6558 and then (No (Freeze_Node (Desig_Type))
6559 or else Analyzed (Freeze_Node (Desig_Type)))
6561 Freeze_Action_Typ := Def_Id;
6563 -- A Taft amendment type cannot get the freeze actions
6564 -- since the full view is not there.
6566 elsif Is_Incomplete_Or_Private_Type (Desig_Type)
6567 and then No (Full_View (Desig_Type))
6569 Freeze_Action_Typ := Def_Id;
6572 Freeze_Action_Typ := Desig_Type;
6575 Append_Freeze_Action (Freeze_Action_Typ,
6576 Make_Object_Declaration (Loc,
6577 Defining_Identifier => Pool_Object,
6578 Object_Definition =>
6579 Make_Subtype_Indication (Loc,
6582 (RTE (RE_Stack_Bounded_Pool), Loc),
6585 Make_Index_Or_Discriminant_Constraint (Loc,
6586 Constraints => New_List (
6588 -- First discriminant is the Pool Size
6591 Storage_Size_Variable (Def_Id), Loc),
6593 -- Second discriminant is the element size
6597 -- Third discriminant is the alignment
6602 Set_Associated_Storage_Pool (Def_Id, Pool_Object);
6606 -- Rep Clause "for Def_Id'Storage_Pool use a_Pool_Object"
6607 -- ---> Storage Pool is the specified one
6609 elsif Present (Associated_Storage_Pool (Def_Id)) then
6611 -- Nothing to do the associated storage pool has been attached
6612 -- when analyzing the rep. clause
6617 -- For access-to-controlled types (including class-wide types and
6618 -- Taft-amendment types which potentially have controlled
6619 -- components), expand the list controller object that will store
6620 -- the dynamically allocated objects. Do not do this
6621 -- transformation for expander-generated access types, but do it
6622 -- for types that are the full view of types derived from other
6623 -- private types. Also suppress the list controller in the case
6624 -- of a designated type with convention Java, since this is used
6625 -- when binding to Java API specs, where there's no equivalent of
6626 -- a finalization list and we don't want to pull in the
6627 -- finalization support if not needed.
6629 if not Comes_From_Source (Def_Id)
6630 and then not Has_Private_Declaration (Def_Id)
6634 elsif (Needs_Finalization (Desig_Type)
6635 and then Convention (Desig_Type) /= Convention_Java
6636 and then Convention (Desig_Type) /= Convention_CIL)
6638 (Is_Incomplete_Or_Private_Type (Desig_Type)
6639 and then No (Full_View (Desig_Type))
6641 -- An exception is made for types defined in the run-time
6642 -- because Ada.Tags.Tag itself is such a type and cannot
6643 -- afford this unnecessary overhead that would generates a
6644 -- loop in the expansion scheme...
6646 and then not In_Runtime (Def_Id)
6648 -- Another exception is if Restrictions (No_Finalization)
6649 -- is active, since then we know nothing is controlled.
6651 and then not Restriction_Active (No_Finalization))
6653 -- If the designated type is not frozen yet, its controlled
6654 -- status must be retrieved explicitly.
6656 or else (Is_Array_Type (Desig_Type)
6657 and then not Is_Frozen (Desig_Type)
6658 and then Needs_Finalization (Component_Type (Desig_Type)))
6660 -- The designated type has controlled anonymous access
6663 or else Has_Controlled_Coextensions (Desig_Type)
6665 Set_Associated_Final_Chain (Def_Id, Add_Final_Chain (Def_Id));
6669 -- Freeze processing for enumeration types
6671 elsif Ekind (Def_Id) = E_Enumeration_Type then
6673 -- We only have something to do if we have a non-standard
6674 -- representation (i.e. at least one literal whose pos value
6675 -- is not the same as its representation)
6677 if Has_Non_Standard_Rep (Def_Id) then
6678 Expand_Freeze_Enumeration_Type (N);
6681 -- Private types that are completed by a derivation from a private
6682 -- type have an internally generated full view, that needs to be
6683 -- frozen. This must be done explicitly because the two views share
6684 -- the freeze node, and the underlying full view is not visible when
6685 -- the freeze node is analyzed.
6687 elsif Is_Private_Type (Def_Id)
6688 and then Is_Derived_Type (Def_Id)
6689 and then Present (Full_View (Def_Id))
6690 and then Is_Itype (Full_View (Def_Id))
6691 and then Has_Private_Declaration (Full_View (Def_Id))
6692 and then Freeze_Node (Full_View (Def_Id)) = N
6694 Set_Entity (N, Full_View (Def_Id));
6695 Result := Freeze_Type (N);
6696 Set_Entity (N, Def_Id);
6698 -- All other types require no expander action. There are such cases
6699 -- (e.g. task types and protected types). In such cases, the freeze
6700 -- nodes are there for use by Gigi.
6704 Freeze_Stream_Operations (N, Def_Id);
6708 when RE_Not_Available =>
6712 -------------------------
6713 -- Get_Simple_Init_Val --
6714 -------------------------
6716 function Get_Simple_Init_Val
6719 Size : Uint := No_Uint) return Node_Id
6721 Loc : constant Source_Ptr := Sloc (N);
6727 -- This is the size to be used for computation of the appropriate
6728 -- initial value for the Normalize_Scalars and Initialize_Scalars case.
6730 IV_Attribute : constant Boolean :=
6731 Nkind (N) = N_Attribute_Reference
6732 and then Attribute_Name (N) = Name_Invalid_Value;
6736 -- These are the values computed by the procedure Check_Subtype_Bounds
6738 procedure Check_Subtype_Bounds;
6739 -- This procedure examines the subtype T, and its ancestor subtypes and
6740 -- derived types to determine the best known information about the
6741 -- bounds of the subtype. After the call Lo_Bound is set either to
6742 -- No_Uint if no information can be determined, or to a value which
6743 -- represents a known low bound, i.e. a valid value of the subtype can
6744 -- not be less than this value. Hi_Bound is similarly set to a known
6745 -- high bound (valid value cannot be greater than this).
6747 --------------------------
6748 -- Check_Subtype_Bounds --
6749 --------------------------
6751 procedure Check_Subtype_Bounds is
6760 Lo_Bound := No_Uint;
6761 Hi_Bound := No_Uint;
6763 -- Loop to climb ancestor subtypes and derived types
6767 if not Is_Discrete_Type (ST1) then
6771 Lo := Type_Low_Bound (ST1);
6772 Hi := Type_High_Bound (ST1);
6774 if Compile_Time_Known_Value (Lo) then
6775 Loval := Expr_Value (Lo);
6777 if Lo_Bound = No_Uint or else Lo_Bound < Loval then
6782 if Compile_Time_Known_Value (Hi) then
6783 Hival := Expr_Value (Hi);
6785 if Hi_Bound = No_Uint or else Hi_Bound > Hival then
6790 ST2 := Ancestor_Subtype (ST1);
6796 exit when ST1 = ST2;
6799 end Check_Subtype_Bounds;
6801 -- Start of processing for Get_Simple_Init_Val
6804 -- For a private type, we should always have an underlying type
6805 -- (because this was already checked in Needs_Simple_Initialization).
6806 -- What we do is to get the value for the underlying type and then do
6807 -- an Unchecked_Convert to the private type.
6809 if Is_Private_Type (T) then
6810 Val := Get_Simple_Init_Val (Underlying_Type (T), N, Size);
6812 -- A special case, if the underlying value is null, then qualify it
6813 -- with the underlying type, so that the null is properly typed
6814 -- Similarly, if it is an aggregate it must be qualified, because an
6815 -- unchecked conversion does not provide a context for it.
6817 if Nkind_In (Val, N_Null, N_Aggregate) then
6819 Make_Qualified_Expression (Loc,
6821 New_Occurrence_Of (Underlying_Type (T), Loc),
6825 Result := Unchecked_Convert_To (T, Val);
6827 -- Don't truncate result (important for Initialize/Normalize_Scalars)
6829 if Nkind (Result) = N_Unchecked_Type_Conversion
6830 and then Is_Scalar_Type (Underlying_Type (T))
6832 Set_No_Truncation (Result);
6837 -- For scalars, we must have normalize/initialize scalars case, or
6838 -- if the node N is an 'Invalid_Value attribute node.
6840 elsif Is_Scalar_Type (T) then
6841 pragma Assert (Init_Or_Norm_Scalars or IV_Attribute);
6843 -- Compute size of object. If it is given by the caller, we can use
6844 -- it directly, otherwise we use Esize (T) as an estimate. As far as
6845 -- we know this covers all cases correctly.
6847 if Size = No_Uint or else Size <= Uint_0 then
6848 Size_To_Use := UI_Max (Uint_1, Esize (T));
6850 Size_To_Use := Size;
6853 -- Maximum size to use is 64 bits, since we will create values
6854 -- of type Unsigned_64 and the range must fit this type.
6856 if Size_To_Use /= No_Uint and then Size_To_Use > Uint_64 then
6857 Size_To_Use := Uint_64;
6860 -- Check known bounds of subtype
6862 Check_Subtype_Bounds;
6864 -- Processing for Normalize_Scalars case
6866 if Normalize_Scalars and then not IV_Attribute then
6868 -- If zero is invalid, it is a convenient value to use that is
6869 -- for sure an appropriate invalid value in all situations.
6871 if Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6872 Val := Make_Integer_Literal (Loc, 0);
6874 -- Cases where all one bits is the appropriate invalid value
6876 -- For modular types, all 1 bits is either invalid or valid. If
6877 -- it is valid, then there is nothing that can be done since there
6878 -- are no invalid values (we ruled out zero already).
6880 -- For signed integer types that have no negative values, either
6881 -- there is room for negative values, or there is not. If there
6882 -- is, then all 1 bits may be interpreted as minus one, which is
6883 -- certainly invalid. Alternatively it is treated as the largest
6884 -- positive value, in which case the observation for modular types
6887 -- For float types, all 1-bits is a NaN (not a number), which is
6888 -- certainly an appropriately invalid value.
6890 elsif Is_Unsigned_Type (T)
6891 or else Is_Floating_Point_Type (T)
6892 or else Is_Enumeration_Type (T)
6894 Val := Make_Integer_Literal (Loc, 2 ** Size_To_Use - 1);
6896 -- Resolve as Unsigned_64, because the largest number we
6897 -- can generate is out of range of universal integer.
6899 Analyze_And_Resolve (Val, RTE (RE_Unsigned_64));
6901 -- Case of signed types
6905 Signed_Size : constant Uint :=
6906 UI_Min (Uint_63, Size_To_Use - 1);
6909 -- Normally we like to use the most negative number. The
6910 -- one exception is when this number is in the known
6911 -- subtype range and the largest positive number is not in
6912 -- the known subtype range.
6914 -- For this exceptional case, use largest positive value
6916 if Lo_Bound /= No_Uint and then Hi_Bound /= No_Uint
6917 and then Lo_Bound <= (-(2 ** Signed_Size))
6918 and then Hi_Bound < 2 ** Signed_Size
6920 Val := Make_Integer_Literal (Loc, 2 ** Signed_Size - 1);
6922 -- Normal case of largest negative value
6925 Val := Make_Integer_Literal (Loc, -(2 ** Signed_Size));
6930 -- Here for Initialize_Scalars case (or Invalid_Value attribute used)
6933 -- For float types, use float values from System.Scalar_Values
6935 if Is_Floating_Point_Type (T) then
6936 if Root_Type (T) = Standard_Short_Float then
6937 Val_RE := RE_IS_Isf;
6938 elsif Root_Type (T) = Standard_Float then
6939 Val_RE := RE_IS_Ifl;
6940 elsif Root_Type (T) = Standard_Long_Float then
6941 Val_RE := RE_IS_Ilf;
6942 else pragma Assert (Root_Type (T) = Standard_Long_Long_Float);
6943 Val_RE := RE_IS_Ill;
6946 -- If zero is invalid, use zero values from System.Scalar_Values
6948 elsif Lo_Bound /= No_Uint and then Lo_Bound > Uint_0 then
6949 if Size_To_Use <= 8 then
6950 Val_RE := RE_IS_Iz1;
6951 elsif Size_To_Use <= 16 then
6952 Val_RE := RE_IS_Iz2;
6953 elsif Size_To_Use <= 32 then
6954 Val_RE := RE_IS_Iz4;
6956 Val_RE := RE_IS_Iz8;
6959 -- For unsigned, use unsigned values from System.Scalar_Values
6961 elsif Is_Unsigned_Type (T) then
6962 if Size_To_Use <= 8 then
6963 Val_RE := RE_IS_Iu1;
6964 elsif Size_To_Use <= 16 then
6965 Val_RE := RE_IS_Iu2;
6966 elsif Size_To_Use <= 32 then
6967 Val_RE := RE_IS_Iu4;
6969 Val_RE := RE_IS_Iu8;
6972 -- For signed, use signed values from System.Scalar_Values
6975 if Size_To_Use <= 8 then
6976 Val_RE := RE_IS_Is1;
6977 elsif Size_To_Use <= 16 then
6978 Val_RE := RE_IS_Is2;
6979 elsif Size_To_Use <= 32 then
6980 Val_RE := RE_IS_Is4;
6982 Val_RE := RE_IS_Is8;
6986 Val := New_Occurrence_Of (RTE (Val_RE), Loc);
6989 -- The final expression is obtained by doing an unchecked conversion
6990 -- of this result to the base type of the required subtype. We use
6991 -- the base type to avoid the unchecked conversion from chopping
6992 -- bits, and then we set Kill_Range_Check to preserve the "bad"
6995 Result := Unchecked_Convert_To (Base_Type (T), Val);
6997 -- Ensure result is not truncated, since we want the "bad" bits
6998 -- and also kill range check on result.
7000 if Nkind (Result) = N_Unchecked_Type_Conversion then
7001 Set_No_Truncation (Result);
7002 Set_Kill_Range_Check (Result, True);
7007 -- String or Wide_[Wide]_String (must have Initialize_Scalars set)
7009 elsif Root_Type (T) = Standard_String
7011 Root_Type (T) = Standard_Wide_String
7013 Root_Type (T) = Standard_Wide_Wide_String
7015 pragma Assert (Init_Or_Norm_Scalars);
7018 Make_Aggregate (Loc,
7019 Component_Associations => New_List (
7020 Make_Component_Association (Loc,
7021 Choices => New_List (
7022 Make_Others_Choice (Loc)),
7025 (Component_Type (T), N, Esize (Root_Type (T))))));
7027 -- Access type is initialized to null
7029 elsif Is_Access_Type (T) then
7033 -- No other possibilities should arise, since we should only be
7034 -- calling Get_Simple_Init_Val if Needs_Simple_Initialization
7035 -- returned True, indicating one of the above cases held.
7038 raise Program_Error;
7042 when RE_Not_Available =>
7044 end Get_Simple_Init_Val;
7046 ------------------------------
7047 -- Has_New_Non_Standard_Rep --
7048 ------------------------------
7050 function Has_New_Non_Standard_Rep (T : Entity_Id) return Boolean is
7052 if not Is_Derived_Type (T) then
7053 return Has_Non_Standard_Rep (T)
7054 or else Has_Non_Standard_Rep (Root_Type (T));
7056 -- If Has_Non_Standard_Rep is not set on the derived type, the
7057 -- representation is fully inherited.
7059 elsif not Has_Non_Standard_Rep (T) then
7063 return First_Rep_Item (T) /= First_Rep_Item (Root_Type (T));
7065 -- May need a more precise check here: the First_Rep_Item may
7066 -- be a stream attribute, which does not affect the representation
7069 end Has_New_Non_Standard_Rep;
7075 function In_Runtime (E : Entity_Id) return Boolean is
7080 while Scope (S1) /= Standard_Standard loop
7084 return Chars (S1) = Name_System or else Chars (S1) = Name_Ada;
7087 ----------------------------
7088 -- Initialization_Warning --
7089 ----------------------------
7091 procedure Initialization_Warning (E : Entity_Id) is
7092 Warning_Needed : Boolean;
7095 Warning_Needed := False;
7097 if Ekind (Current_Scope) = E_Package
7098 and then Static_Elaboration_Desired (Current_Scope)
7101 if Is_Record_Type (E) then
7102 if Has_Discriminants (E)
7103 or else Is_Limited_Type (E)
7104 or else Has_Non_Standard_Rep (E)
7106 Warning_Needed := True;
7109 -- Verify that at least one component has an initialization
7110 -- expression. No need for a warning on a type if all its
7111 -- components have no initialization.
7117 Comp := First_Component (E);
7118 while Present (Comp) loop
7119 if Ekind (Comp) = E_Discriminant
7121 (Nkind (Parent (Comp)) = N_Component_Declaration
7122 and then Present (Expression (Parent (Comp))))
7124 Warning_Needed := True;
7128 Next_Component (Comp);
7133 if Warning_Needed then
7135 ("Objects of the type cannot be initialized " &
7136 "statically by default?",
7142 Error_Msg_N ("Object cannot be initialized statically?", E);
7145 end Initialization_Warning;
7151 function Init_Formals (Typ : Entity_Id) return List_Id is
7152 Loc : constant Source_Ptr := Sloc (Typ);
7156 -- First parameter is always _Init : in out typ. Note that we need
7157 -- this to be in/out because in the case of the task record value,
7158 -- there are default record fields (_Priority, _Size, -Task_Info)
7159 -- that may be referenced in the generated initialization routine.
7161 Formals := New_List (
7162 Make_Parameter_Specification (Loc,
7163 Defining_Identifier =>
7164 Make_Defining_Identifier (Loc, Name_uInit),
7166 Out_Present => True,
7167 Parameter_Type => New_Reference_To (Typ, Loc)));
7169 -- For task record value, or type that contains tasks, add two more
7170 -- formals, _Master : Master_Id and _Chain : in out Activation_Chain
7171 -- We also add these parameters for the task record type case.
7174 or else (Is_Record_Type (Typ) and then Is_Task_Record_Type (Typ))
7177 Make_Parameter_Specification (Loc,
7178 Defining_Identifier =>
7179 Make_Defining_Identifier (Loc, Name_uMaster),
7180 Parameter_Type => New_Reference_To (RTE (RE_Master_Id), Loc)));
7183 Make_Parameter_Specification (Loc,
7184 Defining_Identifier =>
7185 Make_Defining_Identifier (Loc, Name_uChain),
7187 Out_Present => True,
7189 New_Reference_To (RTE (RE_Activation_Chain), Loc)));
7192 Make_Parameter_Specification (Loc,
7193 Defining_Identifier =>
7194 Make_Defining_Identifier (Loc, Name_uTask_Name),
7197 New_Reference_To (Standard_String, Loc)));
7203 when RE_Not_Available =>
7207 -------------------------
7208 -- Init_Secondary_Tags --
7209 -------------------------
7211 procedure Init_Secondary_Tags
7214 Stmts_List : List_Id;
7215 Fixed_Comps : Boolean := True;
7216 Variable_Comps : Boolean := True)
7218 Loc : constant Source_Ptr := Sloc (Target);
7220 -- Inherit the C++ tag of the secondary dispatch table of Typ associated
7221 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7223 procedure Initialize_Tag
7226 Tag_Comp : Entity_Id;
7227 Iface_Tag : Node_Id);
7228 -- Initialize the tag of the secondary dispatch table of Typ associated
7229 -- with Iface. Tag_Comp is the component of Typ that stores Iface_Tag.
7230 -- Compiling under the CPP full ABI compatibility mode, if the ancestor
7231 -- of Typ CPP tagged type we generate code to inherit the contents of
7232 -- the dispatch table directly from the ancestor.
7234 --------------------
7235 -- Initialize_Tag --
7236 --------------------
7238 procedure Initialize_Tag
7241 Tag_Comp : Entity_Id;
7242 Iface_Tag : Node_Id)
7244 Comp_Typ : Entity_Id;
7245 Offset_To_Top_Comp : Entity_Id := Empty;
7248 -- Initialize the pointer to the secondary DT associated with the
7251 if not Is_Ancestor (Iface, Typ) then
7252 Append_To (Stmts_List,
7253 Make_Assignment_Statement (Loc,
7255 Make_Selected_Component (Loc,
7256 Prefix => New_Copy_Tree (Target),
7257 Selector_Name => New_Reference_To (Tag_Comp, Loc)),
7259 New_Reference_To (Iface_Tag, Loc)));
7262 Comp_Typ := Scope (Tag_Comp);
7264 -- Initialize the entries of the table of interfaces. We generate a
7265 -- different call when the parent of the type has variable size
7268 if Comp_Typ /= Etype (Comp_Typ)
7269 and then Is_Variable_Size_Record (Etype (Comp_Typ))
7270 and then Chars (Tag_Comp) /= Name_uTag
7272 pragma Assert (Present (DT_Offset_To_Top_Func (Tag_Comp)));
7274 -- Issue error if Set_Dynamic_Offset_To_Top is not available in a
7275 -- configurable run-time environment.
7277 if not RTE_Available (RE_Set_Dynamic_Offset_To_Top) then
7279 ("variable size record with interface types", Typ);
7284 -- Set_Dynamic_Offset_To_Top
7286 -- Interface_T => Iface'Tag,
7287 -- Offset_Value => n,
7288 -- Offset_Func => Fn'Address)
7290 Append_To (Stmts_List,
7291 Make_Procedure_Call_Statement (Loc,
7292 Name => New_Reference_To
7293 (RTE (RE_Set_Dynamic_Offset_To_Top), Loc),
7294 Parameter_Associations => New_List (
7295 Make_Attribute_Reference (Loc,
7296 Prefix => New_Copy_Tree (Target),
7297 Attribute_Name => Name_Address),
7299 Unchecked_Convert_To (RTE (RE_Tag),
7301 (Node (First_Elmt (Access_Disp_Table (Iface))),
7304 Unchecked_Convert_To
7305 (RTE (RE_Storage_Offset),
7306 Make_Attribute_Reference (Loc,
7308 Make_Selected_Component (Loc,
7309 Prefix => New_Copy_Tree (Target),
7311 New_Reference_To (Tag_Comp, Loc)),
7312 Attribute_Name => Name_Position)),
7314 Unchecked_Convert_To (RTE (RE_Offset_To_Top_Function_Ptr),
7315 Make_Attribute_Reference (Loc,
7316 Prefix => New_Reference_To
7317 (DT_Offset_To_Top_Func (Tag_Comp), Loc),
7318 Attribute_Name => Name_Address)))));
7320 -- In this case the next component stores the value of the
7321 -- offset to the top.
7323 Offset_To_Top_Comp := Next_Entity (Tag_Comp);
7324 pragma Assert (Present (Offset_To_Top_Comp));
7326 Append_To (Stmts_List,
7327 Make_Assignment_Statement (Loc,
7329 Make_Selected_Component (Loc,
7330 Prefix => New_Copy_Tree (Target),
7331 Selector_Name => New_Reference_To
7332 (Offset_To_Top_Comp, Loc)),
7334 Make_Attribute_Reference (Loc,
7336 Make_Selected_Component (Loc,
7337 Prefix => New_Copy_Tree (Target),
7339 New_Reference_To (Tag_Comp, Loc)),
7340 Attribute_Name => Name_Position)));
7342 -- Normal case: No discriminants in the parent type
7345 -- Don't need to set any value if this interface shares
7346 -- the primary dispatch table.
7348 if not Is_Ancestor (Iface, Typ) then
7349 Append_To (Stmts_List,
7350 Build_Set_Static_Offset_To_Top (Loc,
7351 Iface_Tag => New_Reference_To (Iface_Tag, Loc),
7353 Unchecked_Convert_To (RTE (RE_Storage_Offset),
7354 Make_Attribute_Reference (Loc,
7356 Make_Selected_Component (Loc,
7357 Prefix => New_Copy_Tree (Target),
7359 New_Reference_To (Tag_Comp, Loc)),
7360 Attribute_Name => Name_Position))));
7364 -- Register_Interface_Offset
7366 -- Interface_T => Iface'Tag,
7367 -- Is_Constant => True,
7368 -- Offset_Value => n,
7369 -- Offset_Func => null);
7371 if RTE_Available (RE_Register_Interface_Offset) then
7372 Append_To (Stmts_List,
7373 Make_Procedure_Call_Statement (Loc,
7374 Name => New_Reference_To
7375 (RTE (RE_Register_Interface_Offset), Loc),
7376 Parameter_Associations => New_List (
7377 Make_Attribute_Reference (Loc,
7378 Prefix => New_Copy_Tree (Target),
7379 Attribute_Name => Name_Address),
7381 Unchecked_Convert_To (RTE (RE_Tag),
7383 (Node (First_Elmt (Access_Disp_Table (Iface))), Loc)),
7385 New_Occurrence_Of (Standard_True, Loc),
7387 Unchecked_Convert_To
7388 (RTE (RE_Storage_Offset),
7389 Make_Attribute_Reference (Loc,
7391 Make_Selected_Component (Loc,
7392 Prefix => New_Copy_Tree (Target),
7394 New_Reference_To (Tag_Comp, Loc)),
7395 Attribute_Name => Name_Position)),
7404 Full_Typ : Entity_Id;
7405 Ifaces_List : Elist_Id;
7406 Ifaces_Comp_List : Elist_Id;
7407 Ifaces_Tag_List : Elist_Id;
7408 Iface_Elmt : Elmt_Id;
7409 Iface_Comp_Elmt : Elmt_Id;
7410 Iface_Tag_Elmt : Elmt_Id;
7412 In_Variable_Pos : Boolean;
7414 -- Start of processing for Init_Secondary_Tags
7417 -- Handle private types
7419 if Present (Full_View (Typ)) then
7420 Full_Typ := Full_View (Typ);
7425 Collect_Interfaces_Info
7426 (Full_Typ, Ifaces_List, Ifaces_Comp_List, Ifaces_Tag_List);
7428 Iface_Elmt := First_Elmt (Ifaces_List);
7429 Iface_Comp_Elmt := First_Elmt (Ifaces_Comp_List);
7430 Iface_Tag_Elmt := First_Elmt (Ifaces_Tag_List);
7431 while Present (Iface_Elmt) loop
7432 Tag_Comp := Node (Iface_Comp_Elmt);
7434 -- Check if parent of record type has variable size components
7436 In_Variable_Pos := Scope (Tag_Comp) /= Etype (Scope (Tag_Comp))
7437 and then Is_Variable_Size_Record (Etype (Scope (Tag_Comp)));
7439 -- If we are compiling under the CPP full ABI compatibility mode and
7440 -- the ancestor is a CPP_Pragma tagged type then we generate code to
7441 -- initialize the secondary tag components from tags that reference
7442 -- secondary tables filled with copy of parent slots.
7444 if Is_CPP_Class (Root_Type (Full_Typ)) then
7446 -- Reject interface components located at variable offset in
7447 -- C++ derivations. This is currently unsupported.
7449 if not Fixed_Comps and then In_Variable_Pos then
7451 -- Locate the first dynamic component of the record. Done to
7452 -- improve the text of the warning.
7456 Comp_Typ : Entity_Id;
7459 Comp := First_Entity (Typ);
7460 while Present (Comp) loop
7461 Comp_Typ := Etype (Comp);
7463 if Ekind (Comp) /= E_Discriminant
7464 and then not Is_Tag (Comp)
7467 (Is_Record_Type (Comp_Typ)
7468 and then Is_Variable_Size_Record
7469 (Base_Type (Comp_Typ)))
7471 (Is_Array_Type (Comp_Typ)
7472 and then Is_Variable_Size_Array (Comp_Typ));
7478 pragma Assert (Present (Comp));
7479 Error_Msg_Node_2 := Comp;
7481 ("parent type & with dynamic component & cannot be parent"
7482 & " of 'C'P'P derivation if new interfaces are present",
7483 Typ, Scope (Original_Record_Component (Comp)));
7486 Sloc (Scope (Original_Record_Component (Comp)));
7488 ("type derived from 'C'P'P type & defined #",
7489 Typ, Scope (Original_Record_Component (Comp)));
7491 -- Avoid duplicated warnings
7496 -- Initialize secondary tags
7499 Append_To (Stmts_List,
7500 Make_Assignment_Statement (Loc,
7502 Make_Selected_Component (Loc,
7503 Prefix => New_Copy_Tree (Target),
7505 New_Reference_To (Node (Iface_Comp_Elmt), Loc)),
7507 New_Reference_To (Node (Iface_Tag_Elmt), Loc)));
7510 -- Otherwise generate code to initialize the tag
7513 if (In_Variable_Pos and then Variable_Comps)
7514 or else (not In_Variable_Pos and then Fixed_Comps)
7516 Initialize_Tag (Full_Typ,
7517 Iface => Node (Iface_Elmt),
7518 Tag_Comp => Tag_Comp,
7519 Iface_Tag => Node (Iface_Tag_Elmt));
7523 Next_Elmt (Iface_Elmt);
7524 Next_Elmt (Iface_Comp_Elmt);
7525 Next_Elmt (Iface_Tag_Elmt);
7527 end Init_Secondary_Tags;
7529 ----------------------------
7530 -- Is_Variable_Size_Array --
7531 ----------------------------
7533 function Is_Variable_Size_Array (E : Entity_Id) return Boolean is
7535 function Is_Constant_Bound (Exp : Node_Id) return Boolean;
7536 -- To simplify handling of array components. Determines whether the
7537 -- given bound is constant (a constant or enumeration literal, or an
7538 -- integer literal) as opposed to per-object, through an expression
7539 -- or a discriminant.
7541 -----------------------
7542 -- Is_Constant_Bound --
7543 -----------------------
7545 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
7547 if Nkind (Exp) = N_Integer_Literal then
7551 Is_Entity_Name (Exp)
7552 and then Present (Entity (Exp))
7554 (Ekind (Entity (Exp)) = E_Constant
7555 or else Ekind (Entity (Exp)) = E_Enumeration_Literal);
7557 end Is_Constant_Bound;
7563 -- Start of processing for Is_Variable_Sized_Array
7566 pragma Assert (Is_Array_Type (E));
7568 -- Check if some index is initialized with a non-constant value
7570 Idx := First_Index (E);
7571 while Present (Idx) loop
7572 if Nkind (Idx) = N_Range then
7573 if not Is_Constant_Bound (Low_Bound (Idx))
7574 or else not Is_Constant_Bound (High_Bound (Idx))
7580 Idx := Next_Index (Idx);
7584 end Is_Variable_Size_Array;
7586 -----------------------------
7587 -- Is_Variable_Size_Record --
7588 -----------------------------
7590 function Is_Variable_Size_Record (E : Entity_Id) return Boolean is
7592 Comp_Typ : Entity_Id;
7595 pragma Assert (Is_Record_Type (E));
7597 Comp := First_Entity (E);
7598 while Present (Comp) loop
7599 Comp_Typ := Etype (Comp);
7601 -- Recursive call if the record type has discriminants
7603 if Is_Record_Type (Comp_Typ)
7604 and then Has_Discriminants (Comp_Typ)
7605 and then Is_Variable_Size_Record (Comp_Typ)
7609 elsif Is_Array_Type (Comp_Typ)
7610 and then Is_Variable_Size_Array (Comp_Typ)
7619 end Is_Variable_Size_Record;
7621 ----------------------------------------
7622 -- Make_Controlling_Function_Wrappers --
7623 ----------------------------------------
7625 procedure Make_Controlling_Function_Wrappers
7626 (Tag_Typ : Entity_Id;
7627 Decl_List : out List_Id;
7628 Body_List : out List_Id)
7630 Loc : constant Source_Ptr := Sloc (Tag_Typ);
7631 Prim_Elmt : Elmt_Id;
7633 Actual_List : List_Id;
7634 Formal_List : List_Id;
7636 Par_Formal : Entity_Id;
7637 Formal_Node : Node_Id;
7638 Func_Body : Node_Id;
7639 Func_Decl : Node_Id;
7640 Func_Spec : Node_Id;
7641 Return_Stmt : Node_Id;
7644 Decl_List := New_List;
7645 Body_List := New_List;
7647 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
7649 while Present (Prim_Elmt) loop
7650 Subp := Node (Prim_Elmt);
7652 -- If a primitive function with a controlling result of the type has
7653 -- not been overridden by the user, then we must create a wrapper
7654 -- function here that effectively overrides it and invokes the
7655 -- (non-abstract) parent function. This can only occur for a null
7656 -- extension. Note that functions with anonymous controlling access
7657 -- results don't qualify and must be overridden. We also exclude
7658 -- Input attributes, since each type will have its own version of
7659 -- Input constructed by the expander. The test for Comes_From_Source
7660 -- is needed to distinguish inherited operations from renamings
7661 -- (which also have Alias set).
7663 -- The function may be abstract, or require_Overriding may be set
7664 -- for it, because tests for null extensions may already have reset
7665 -- the Is_Abstract_Subprogram_Flag. If Requires_Overriding is not
7666 -- set, functions that need wrappers are recognized by having an
7667 -- alias that returns the parent type.
7669 if Comes_From_Source (Subp)
7670 or else No (Alias (Subp))
7671 or else Ekind (Subp) /= E_Function
7672 or else not Has_Controlling_Result (Subp)
7673 or else Is_Access_Type (Etype (Subp))
7674 or else Is_Abstract_Subprogram (Alias (Subp))
7675 or else Is_TSS (Subp, TSS_Stream_Input)
7679 elsif Is_Abstract_Subprogram (Subp)
7680 or else Requires_Overriding (Subp)
7682 (Is_Null_Extension (Etype (Subp))
7683 and then Etype (Alias (Subp)) /= Etype (Subp))
7685 Formal_List := No_List;
7686 Formal := First_Formal (Subp);
7688 if Present (Formal) then
7689 Formal_List := New_List;
7691 while Present (Formal) loop
7693 (Make_Parameter_Specification
7695 Defining_Identifier =>
7696 Make_Defining_Identifier (Sloc (Formal),
7697 Chars => Chars (Formal)),
7698 In_Present => In_Present (Parent (Formal)),
7699 Out_Present => Out_Present (Parent (Formal)),
7700 Null_Exclusion_Present =>
7701 Null_Exclusion_Present (Parent (Formal)),
7703 New_Reference_To (Etype (Formal), Loc),
7705 New_Copy_Tree (Expression (Parent (Formal)))),
7708 Next_Formal (Formal);
7713 Make_Function_Specification (Loc,
7714 Defining_Unit_Name =>
7715 Make_Defining_Identifier (Loc,
7716 Chars => Chars (Subp)),
7717 Parameter_Specifications => Formal_List,
7718 Result_Definition =>
7719 New_Reference_To (Etype (Subp), Loc));
7721 Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
7722 Append_To (Decl_List, Func_Decl);
7724 -- Build a wrapper body that calls the parent function. The body
7725 -- contains a single return statement that returns an extension
7726 -- aggregate whose ancestor part is a call to the parent function,
7727 -- passing the formals as actuals (with any controlling arguments
7728 -- converted to the types of the corresponding formals of the
7729 -- parent function, which might be anonymous access types), and
7730 -- having a null extension.
7732 Formal := First_Formal (Subp);
7733 Par_Formal := First_Formal (Alias (Subp));
7734 Formal_Node := First (Formal_List);
7736 if Present (Formal) then
7737 Actual_List := New_List;
7739 Actual_List := No_List;
7742 while Present (Formal) loop
7743 if Is_Controlling_Formal (Formal) then
7744 Append_To (Actual_List,
7745 Make_Type_Conversion (Loc,
7747 New_Occurrence_Of (Etype (Par_Formal), Loc),
7750 (Defining_Identifier (Formal_Node), Loc)));
7755 (Defining_Identifier (Formal_Node), Loc));
7758 Next_Formal (Formal);
7759 Next_Formal (Par_Formal);
7764 Make_Simple_Return_Statement (Loc,
7766 Make_Extension_Aggregate (Loc,
7768 Make_Function_Call (Loc,
7769 Name => New_Reference_To (Alias (Subp), Loc),
7770 Parameter_Associations => Actual_List),
7771 Null_Record_Present => True));
7774 Make_Subprogram_Body (Loc,
7775 Specification => New_Copy_Tree (Func_Spec),
7776 Declarations => Empty_List,
7777 Handled_Statement_Sequence =>
7778 Make_Handled_Sequence_Of_Statements (Loc,
7779 Statements => New_List (Return_Stmt)));
7781 Set_Defining_Unit_Name
7782 (Specification (Func_Body),
7783 Make_Defining_Identifier (Loc, Chars (Subp)));
7785 Append_To (Body_List, Func_Body);
7787 -- Replace the inherited function with the wrapper function
7788 -- in the primitive operations list.
7790 Override_Dispatching_Operation
7791 (Tag_Typ, Subp, New_Op => Defining_Unit_Name (Func_Spec));
7795 Next_Elmt (Prim_Elmt);
7797 end Make_Controlling_Function_Wrappers;
7803 function Make_Eq_Body
7805 Eq_Name : Name_Id) return Node_Id
7807 Loc : constant Source_Ptr := Sloc (Parent (Typ));
7809 Def : constant Node_Id := Parent (Typ);
7810 Stmts : constant List_Id := New_List;
7811 Variant_Case : Boolean := Has_Discriminants (Typ);
7812 Comps : Node_Id := Empty;
7813 Typ_Def : Node_Id := Type_Definition (Def);
7817 Predef_Spec_Or_Body (Loc,
7820 Profile => New_List (
7821 Make_Parameter_Specification (Loc,
7822 Defining_Identifier =>
7823 Make_Defining_Identifier (Loc, Name_X),
7824 Parameter_Type => New_Reference_To (Typ, Loc)),
7826 Make_Parameter_Specification (Loc,
7827 Defining_Identifier =>
7828 Make_Defining_Identifier (Loc, Name_Y),
7829 Parameter_Type => New_Reference_To (Typ, Loc))),
7831 Ret_Type => Standard_Boolean,
7834 if Variant_Case then
7835 if Nkind (Typ_Def) = N_Derived_Type_Definition then
7836 Typ_Def := Record_Extension_Part (Typ_Def);
7839 if Present (Typ_Def) then
7840 Comps := Component_List (Typ_Def);
7844 Present (Comps) and then Present (Variant_Part (Comps));
7847 if Variant_Case then
7849 Make_Eq_If (Typ, Discriminant_Specifications (Def)));
7850 Append_List_To (Stmts, Make_Eq_Case (Typ, Comps));
7852 Make_Simple_Return_Statement (Loc,
7853 Expression => New_Reference_To (Standard_True, Loc)));
7857 Make_Simple_Return_Statement (Loc,
7859 Expand_Record_Equality
7862 Lhs => Make_Identifier (Loc, Name_X),
7863 Rhs => Make_Identifier (Loc, Name_Y),
7864 Bodies => Declarations (Decl))));
7867 Set_Handled_Statement_Sequence
7868 (Decl, Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7876 -- <Make_Eq_If shared components>
7878 -- when V1 => <Make_Eq_Case> on subcomponents
7880 -- when Vn => <Make_Eq_Case> on subcomponents
7883 function Make_Eq_Case
7886 Discr : Entity_Id := Empty) return List_Id
7888 Loc : constant Source_Ptr := Sloc (E);
7889 Result : constant List_Id := New_List;
7894 Append_To (Result, Make_Eq_If (E, Component_Items (CL)));
7896 if No (Variant_Part (CL)) then
7900 Variant := First_Non_Pragma (Variants (Variant_Part (CL)));
7902 if No (Variant) then
7906 Alt_List := New_List;
7908 while Present (Variant) loop
7909 Append_To (Alt_List,
7910 Make_Case_Statement_Alternative (Loc,
7911 Discrete_Choices => New_Copy_List (Discrete_Choices (Variant)),
7912 Statements => Make_Eq_Case (E, Component_List (Variant))));
7914 Next_Non_Pragma (Variant);
7917 -- If we have an Unchecked_Union, use one of the parameters that
7918 -- captures the discriminants.
7920 if Is_Unchecked_Union (E) then
7922 Make_Case_Statement (Loc,
7923 Expression => New_Reference_To (Discr, Loc),
7924 Alternatives => Alt_List));
7928 Make_Case_Statement (Loc,
7930 Make_Selected_Component (Loc,
7931 Prefix => Make_Identifier (Loc, Name_X),
7932 Selector_Name => New_Copy (Name (Variant_Part (CL)))),
7933 Alternatives => Alt_List));
7954 -- or a null statement if the list L is empty
7958 L : List_Id) return Node_Id
7960 Loc : constant Source_Ptr := Sloc (E);
7962 Field_Name : Name_Id;
7967 return Make_Null_Statement (Loc);
7972 C := First_Non_Pragma (L);
7973 while Present (C) loop
7974 Field_Name := Chars (Defining_Identifier (C));
7976 -- The tags must not be compared: they are not part of the value.
7977 -- Ditto for the controller component, if present.
7979 -- Note also that in the following, we use Make_Identifier for
7980 -- the component names. Use of New_Reference_To to identify the
7981 -- components would be incorrect because the wrong entities for
7982 -- discriminants could be picked up in the private type case.
7984 if Field_Name /= Name_uTag
7986 Field_Name /= Name_uController
7988 Evolve_Or_Else (Cond,
7991 Make_Selected_Component (Loc,
7992 Prefix => Make_Identifier (Loc, Name_X),
7994 Make_Identifier (Loc, Field_Name)),
7997 Make_Selected_Component (Loc,
7998 Prefix => Make_Identifier (Loc, Name_Y),
8000 Make_Identifier (Loc, Field_Name))));
8003 Next_Non_Pragma (C);
8007 return Make_Null_Statement (Loc);
8011 Make_Implicit_If_Statement (E,
8013 Then_Statements => New_List (
8014 Make_Simple_Return_Statement (Loc,
8015 Expression => New_Occurrence_Of (Standard_False, Loc))));
8020 -------------------------------
8021 -- Make_Null_Procedure_Specs --
8022 -------------------------------
8024 function Make_Null_Procedure_Specs (Tag_Typ : Entity_Id) return List_Id is
8025 Decl_List : constant List_Id := New_List;
8026 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8028 Formal_List : List_Id;
8029 New_Param_Spec : Node_Id;
8030 Parent_Subp : Entity_Id;
8031 Prim_Elmt : Elmt_Id;
8035 Prim_Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
8036 while Present (Prim_Elmt) loop
8037 Subp := Node (Prim_Elmt);
8039 -- If a null procedure inherited from an interface has not been
8040 -- overridden, then we build a null procedure declaration to
8041 -- override the inherited procedure.
8043 Parent_Subp := Alias (Subp);
8045 if Present (Parent_Subp)
8046 and then Is_Null_Interface_Primitive (Parent_Subp)
8048 Formal_List := No_List;
8049 Formal := First_Formal (Subp);
8051 if Present (Formal) then
8052 Formal_List := New_List;
8054 while Present (Formal) loop
8056 -- Copy the parameter spec including default expressions
8059 New_Copy_Tree (Parent (Formal), New_Sloc => Loc);
8061 -- Generate a new defining identifier for the new formal.
8062 -- required because New_Copy_Tree does not duplicate
8063 -- semantic fields (except itypes).
8065 Set_Defining_Identifier (New_Param_Spec,
8066 Make_Defining_Identifier (Sloc (Formal),
8067 Chars => Chars (Formal)));
8069 -- For controlling arguments we must change their
8070 -- parameter type to reference the tagged type (instead
8071 -- of the interface type)
8073 if Is_Controlling_Formal (Formal) then
8074 if Nkind (Parameter_Type (Parent (Formal)))
8077 Set_Parameter_Type (New_Param_Spec,
8078 New_Occurrence_Of (Tag_Typ, Loc));
8081 (Nkind (Parameter_Type (Parent (Formal)))
8082 = N_Access_Definition);
8083 Set_Subtype_Mark (Parameter_Type (New_Param_Spec),
8084 New_Occurrence_Of (Tag_Typ, Loc));
8088 Append (New_Param_Spec, Formal_List);
8090 Next_Formal (Formal);
8094 Append_To (Decl_List,
8095 Make_Subprogram_Declaration (Loc,
8096 Make_Procedure_Specification (Loc,
8097 Defining_Unit_Name =>
8098 Make_Defining_Identifier (Loc, Chars (Subp)),
8099 Parameter_Specifications => Formal_List,
8100 Null_Present => True)));
8103 Next_Elmt (Prim_Elmt);
8107 end Make_Null_Procedure_Specs;
8109 -------------------------------------
8110 -- Make_Predefined_Primitive_Specs --
8111 -------------------------------------
8113 procedure Make_Predefined_Primitive_Specs
8114 (Tag_Typ : Entity_Id;
8115 Predef_List : out List_Id;
8116 Renamed_Eq : out Entity_Id)
8118 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8119 Res : constant List_Id := New_List;
8121 Eq_Needed : Boolean;
8123 Eq_Name : Name_Id := Name_Op_Eq;
8125 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean;
8126 -- Returns true if Prim is a renaming of an unresolved predefined
8127 -- equality operation.
8129 -------------------------------
8130 -- Is_Predefined_Eq_Renaming --
8131 -------------------------------
8133 function Is_Predefined_Eq_Renaming (Prim : Node_Id) return Boolean is
8135 return Chars (Prim) /= Name_Op_Eq
8136 and then Present (Alias (Prim))
8137 and then Comes_From_Source (Prim)
8138 and then Is_Intrinsic_Subprogram (Alias (Prim))
8139 and then Chars (Alias (Prim)) = Name_Op_Eq;
8140 end Is_Predefined_Eq_Renaming;
8142 -- Start of processing for Make_Predefined_Primitive_Specs
8145 Renamed_Eq := Empty;
8149 Append_To (Res, Predef_Spec_Or_Body (Loc,
8152 Profile => New_List (
8153 Make_Parameter_Specification (Loc,
8154 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8155 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8157 Ret_Type => Standard_Long_Long_Integer));
8159 -- Spec of _Alignment
8161 Append_To (Res, Predef_Spec_Or_Body (Loc,
8163 Name => Name_uAlignment,
8164 Profile => New_List (
8165 Make_Parameter_Specification (Loc,
8166 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8167 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8169 Ret_Type => Standard_Integer));
8171 -- Specs for dispatching stream attributes
8174 Stream_Op_TSS_Names :
8175 constant array (Integer range <>) of TSS_Name_Type :=
8182 for Op in Stream_Op_TSS_Names'Range loop
8183 if Stream_Operation_OK (Tag_Typ, Stream_Op_TSS_Names (Op)) then
8185 Predef_Stream_Attr_Spec (Loc, Tag_Typ,
8186 Stream_Op_TSS_Names (Op)));
8191 -- Spec of "=" is expanded if the type is not limited and if a
8192 -- user defined "=" was not already declared for the non-full
8193 -- view of a private extension
8195 if not Is_Limited_Type (Tag_Typ) then
8197 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8198 while Present (Prim) loop
8200 -- If a primitive is encountered that renames the predefined
8201 -- equality operator before reaching any explicit equality
8202 -- primitive, then we still need to create a predefined equality
8203 -- function, because calls to it can occur via the renaming. A new
8204 -- name is created for the equality to avoid conflicting with any
8205 -- user-defined equality. (Note that this doesn't account for
8206 -- renamings of equality nested within subpackages???)
8208 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8209 Eq_Name := New_External_Name (Chars (Node (Prim)), 'E');
8211 -- User-defined equality
8213 elsif Chars (Node (Prim)) = Name_Op_Eq
8214 and then Etype (First_Formal (Node (Prim))) =
8215 Etype (Next_Formal (First_Formal (Node (Prim))))
8216 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean
8218 if No (Alias (Node (Prim)))
8219 or else Nkind (Unit_Declaration_Node (Node (Prim))) =
8220 N_Subprogram_Renaming_Declaration
8225 -- If the parent is not an interface type and has an abstract
8226 -- equality function, the inherited equality is abstract as
8227 -- well, and no body can be created for it.
8229 elsif not Is_Interface (Etype (Tag_Typ))
8230 and then Present (Alias (Node (Prim)))
8231 and then Is_Abstract_Subprogram (Alias (Node (Prim)))
8236 -- If the type has an equality function corresponding with
8237 -- a primitive defined in an interface type, the inherited
8238 -- equality is abstract as well, and no body can be created
8241 elsif Present (Alias (Node (Prim)))
8242 and then Comes_From_Source (Ultimate_Alias (Node (Prim)))
8245 (Find_Dispatching_Type (Ultimate_Alias (Node (Prim))))
8255 -- If a renaming of predefined equality was found but there was no
8256 -- user-defined equality (so Eq_Needed is still true), then set the
8257 -- name back to Name_Op_Eq. But in the case where a user-defined
8258 -- equality was located after such a renaming, then the predefined
8259 -- equality function is still needed, so Eq_Needed must be set back
8262 if Eq_Name /= Name_Op_Eq then
8264 Eq_Name := Name_Op_Eq;
8271 Eq_Spec := Predef_Spec_Or_Body (Loc,
8274 Profile => New_List (
8275 Make_Parameter_Specification (Loc,
8276 Defining_Identifier =>
8277 Make_Defining_Identifier (Loc, Name_X),
8278 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8279 Make_Parameter_Specification (Loc,
8280 Defining_Identifier =>
8281 Make_Defining_Identifier (Loc, Name_Y),
8282 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8283 Ret_Type => Standard_Boolean);
8284 Append_To (Res, Eq_Spec);
8286 if Eq_Name /= Name_Op_Eq then
8287 Renamed_Eq := Defining_Unit_Name (Specification (Eq_Spec));
8289 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8290 while Present (Prim) loop
8292 -- Any renamings of equality that appeared before an
8293 -- overriding equality must be updated to refer to the
8294 -- entity for the predefined equality, otherwise calls via
8295 -- the renaming would get incorrectly resolved to call the
8296 -- user-defined equality function.
8298 if Is_Predefined_Eq_Renaming (Node (Prim)) then
8299 Set_Alias (Node (Prim), Renamed_Eq);
8301 -- Exit upon encountering a user-defined equality
8303 elsif Chars (Node (Prim)) = Name_Op_Eq
8304 and then No (Alias (Node (Prim)))
8314 -- Spec for dispatching assignment
8316 Append_To (Res, Predef_Spec_Or_Body (Loc,
8318 Name => Name_uAssign,
8319 Profile => New_List (
8320 Make_Parameter_Specification (Loc,
8321 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8322 Out_Present => True,
8323 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8325 Make_Parameter_Specification (Loc,
8326 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8327 Parameter_Type => New_Reference_To (Tag_Typ, Loc)))));
8330 -- Ada 2005: Generate declarations for the following primitive
8331 -- operations for limited interfaces and synchronized types that
8332 -- implement a limited interface.
8334 -- Disp_Asynchronous_Select
8335 -- Disp_Conditional_Select
8336 -- Disp_Get_Prim_Op_Kind
8339 -- Disp_Timed_Select
8341 -- These operations cannot be implemented on VM targets, so we simply
8342 -- disable their generation in this case. Disable the generation of
8343 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
8345 if Ada_Version >= Ada_05
8346 and then Tagged_Type_Expansion
8347 and then not Restriction_Active (No_Dispatching_Calls)
8348 and then not Restriction_Active (No_Select_Statements)
8349 and then RTE_Available (RE_Select_Specific_Data)
8351 -- These primitives are defined abstract in interface types
8353 if Is_Interface (Tag_Typ)
8354 and then Is_Limited_Record (Tag_Typ)
8357 Make_Abstract_Subprogram_Declaration (Loc,
8359 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8362 Make_Abstract_Subprogram_Declaration (Loc,
8364 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8367 Make_Abstract_Subprogram_Declaration (Loc,
8369 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8372 Make_Abstract_Subprogram_Declaration (Loc,
8374 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8377 Make_Abstract_Subprogram_Declaration (Loc,
8379 Make_Disp_Requeue_Spec (Tag_Typ)));
8382 Make_Abstract_Subprogram_Declaration (Loc,
8384 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8386 -- If the ancestor is an interface type we declare non-abstract
8387 -- primitives to override the abstract primitives of the interface
8390 elsif (not Is_Interface (Tag_Typ)
8391 and then Is_Interface (Etype (Tag_Typ))
8392 and then Is_Limited_Record (Etype (Tag_Typ)))
8394 (Is_Concurrent_Record_Type (Tag_Typ)
8395 and then Has_Interfaces (Tag_Typ))
8398 Make_Subprogram_Declaration (Loc,
8400 Make_Disp_Asynchronous_Select_Spec (Tag_Typ)));
8403 Make_Subprogram_Declaration (Loc,
8405 Make_Disp_Conditional_Select_Spec (Tag_Typ)));
8408 Make_Subprogram_Declaration (Loc,
8410 Make_Disp_Get_Prim_Op_Kind_Spec (Tag_Typ)));
8413 Make_Subprogram_Declaration (Loc,
8415 Make_Disp_Get_Task_Id_Spec (Tag_Typ)));
8418 Make_Subprogram_Declaration (Loc,
8420 Make_Disp_Requeue_Spec (Tag_Typ)));
8423 Make_Subprogram_Declaration (Loc,
8425 Make_Disp_Timed_Select_Spec (Tag_Typ)));
8429 -- Specs for finalization actions that may be required in case a future
8430 -- extension contain a controlled element. We generate those only for
8431 -- root tagged types where they will get dummy bodies or when the type
8432 -- has controlled components and their body must be generated. It is
8433 -- also impossible to provide those for tagged types defined within
8434 -- s-finimp since it would involve circularity problems
8436 if In_Finalization_Root (Tag_Typ) then
8439 -- We also skip these if finalization is not available
8441 elsif Restriction_Active (No_Finalization) then
8444 -- Skip these for CIL Value types, where finalization is not available
8446 elsif Is_Value_Type (Tag_Typ) then
8449 elsif Etype (Tag_Typ) = Tag_Typ
8450 or else Needs_Finalization (Tag_Typ)
8452 -- Ada 2005 (AI-251): We must also generate these subprograms if
8453 -- the immediate ancestor is an interface to ensure the correct
8454 -- initialization of its dispatch table.
8456 or else (not Is_Interface (Tag_Typ)
8457 and then Is_Interface (Etype (Tag_Typ)))
8459 -- Ada 205 (AI-251): We must also generate these subprograms if
8460 -- the parent of an nonlimited interface is a limited interface
8462 or else (Is_Interface (Tag_Typ)
8463 and then not Is_Limited_Interface (Tag_Typ)
8464 and then Is_Limited_Interface (Etype (Tag_Typ)))
8466 if not Is_Limited_Type (Tag_Typ) then
8468 Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust));
8471 Append_To (Res, Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize));
8475 end Make_Predefined_Primitive_Specs;
8477 ---------------------------------
8478 -- Needs_Simple_Initialization --
8479 ---------------------------------
8481 function Needs_Simple_Initialization
8483 Consider_IS : Boolean := True) return Boolean
8485 Consider_IS_NS : constant Boolean :=
8487 or (Initialize_Scalars and Consider_IS);
8490 -- Check for private type, in which case test applies to the underlying
8491 -- type of the private type.
8493 if Is_Private_Type (T) then
8495 RT : constant Entity_Id := Underlying_Type (T);
8498 if Present (RT) then
8499 return Needs_Simple_Initialization (RT);
8505 -- Cases needing simple initialization are access types, and, if pragma
8506 -- Normalize_Scalars or Initialize_Scalars is in effect, then all scalar
8509 elsif Is_Access_Type (T)
8510 or else (Consider_IS_NS and then (Is_Scalar_Type (T)))
8514 -- If Initialize/Normalize_Scalars is in effect, string objects also
8515 -- need initialization, unless they are created in the course of
8516 -- expanding an aggregate (since in the latter case they will be
8517 -- filled with appropriate initializing values before they are used).
8519 elsif Consider_IS_NS
8521 (Root_Type (T) = Standard_String
8522 or else Root_Type (T) = Standard_Wide_String
8523 or else Root_Type (T) = Standard_Wide_Wide_String)
8526 or else Nkind (Associated_Node_For_Itype (T)) /= N_Aggregate)
8533 end Needs_Simple_Initialization;
8535 ----------------------
8536 -- Predef_Deep_Spec --
8537 ----------------------
8539 function Predef_Deep_Spec
8541 Tag_Typ : Entity_Id;
8542 Name : TSS_Name_Type;
8543 For_Body : Boolean := False) return Node_Id
8549 if Name = TSS_Deep_Finalize then
8551 Type_B := Standard_Boolean;
8555 Make_Parameter_Specification (Loc,
8556 Defining_Identifier => Make_Defining_Identifier (Loc, Name_L),
8558 Out_Present => True,
8560 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
8561 Type_B := Standard_Short_Short_Integer;
8565 Make_Parameter_Specification (Loc,
8566 Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
8568 Out_Present => True,
8569 Parameter_Type => New_Reference_To (Tag_Typ, Loc)));
8572 Make_Parameter_Specification (Loc,
8573 Defining_Identifier => Make_Defining_Identifier (Loc, Name_B),
8574 Parameter_Type => New_Reference_To (Type_B, Loc)));
8576 return Predef_Spec_Or_Body (Loc,
8577 Name => Make_TSS_Name (Tag_Typ, Name),
8580 For_Body => For_Body);
8583 when RE_Not_Available =>
8585 end Predef_Deep_Spec;
8587 -------------------------
8588 -- Predef_Spec_Or_Body --
8589 -------------------------
8591 function Predef_Spec_Or_Body
8593 Tag_Typ : Entity_Id;
8596 Ret_Type : Entity_Id := Empty;
8597 For_Body : Boolean := False) return Node_Id
8599 Id : constant Entity_Id := Make_Defining_Identifier (Loc, Name);
8603 Set_Is_Public (Id, Is_Public (Tag_Typ));
8605 -- The internal flag is set to mark these declarations because they have
8606 -- specific properties. First, they are primitives even if they are not
8607 -- defined in the type scope (the freezing point is not necessarily in
8608 -- the same scope). Second, the predefined equality can be overridden by
8609 -- a user-defined equality, no body will be generated in this case.
8611 Set_Is_Internal (Id);
8613 if not Debug_Generated_Code then
8614 Set_Debug_Info_Off (Id);
8617 if No (Ret_Type) then
8619 Make_Procedure_Specification (Loc,
8620 Defining_Unit_Name => Id,
8621 Parameter_Specifications => Profile);
8624 Make_Function_Specification (Loc,
8625 Defining_Unit_Name => Id,
8626 Parameter_Specifications => Profile,
8627 Result_Definition =>
8628 New_Reference_To (Ret_Type, Loc));
8631 if Is_Interface (Tag_Typ) then
8632 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8634 -- If body case, return empty subprogram body. Note that this is ill-
8635 -- formed, because there is not even a null statement, and certainly not
8636 -- a return in the function case. The caller is expected to do surgery
8637 -- on the body to add the appropriate stuff.
8640 return Make_Subprogram_Body (Loc, Spec, Empty_List, Empty);
8642 -- For the case of an Input attribute predefined for an abstract type,
8643 -- generate an abstract specification. This will never be called, but we
8644 -- need the slot allocated in the dispatching table so that attributes
8645 -- typ'Class'Input and typ'Class'Output will work properly.
8647 elsif Is_TSS (Name, TSS_Stream_Input)
8648 and then Is_Abstract_Type (Tag_Typ)
8650 return Make_Abstract_Subprogram_Declaration (Loc, Spec);
8652 -- Normal spec case, where we return a subprogram declaration
8655 return Make_Subprogram_Declaration (Loc, Spec);
8657 end Predef_Spec_Or_Body;
8659 -----------------------------
8660 -- Predef_Stream_Attr_Spec --
8661 -----------------------------
8663 function Predef_Stream_Attr_Spec
8665 Tag_Typ : Entity_Id;
8666 Name : TSS_Name_Type;
8667 For_Body : Boolean := False) return Node_Id
8669 Ret_Type : Entity_Id;
8672 if Name = TSS_Stream_Input then
8673 Ret_Type := Tag_Typ;
8678 return Predef_Spec_Or_Body (Loc,
8679 Name => Make_TSS_Name (Tag_Typ, Name),
8681 Profile => Build_Stream_Attr_Profile (Loc, Tag_Typ, Name),
8682 Ret_Type => Ret_Type,
8683 For_Body => For_Body);
8684 end Predef_Stream_Attr_Spec;
8686 ---------------------------------
8687 -- Predefined_Primitive_Bodies --
8688 ---------------------------------
8690 function Predefined_Primitive_Bodies
8691 (Tag_Typ : Entity_Id;
8692 Renamed_Eq : Entity_Id) return List_Id
8694 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8695 Res : constant List_Id := New_List;
8698 Eq_Needed : Boolean;
8702 pragma Warnings (Off, Ent);
8705 pragma Assert (not Is_Interface (Tag_Typ));
8707 -- See if we have a predefined "=" operator
8709 if Present (Renamed_Eq) then
8711 Eq_Name := Chars (Renamed_Eq);
8713 -- If the parent is an interface type then it has defined all the
8714 -- predefined primitives abstract and we need to check if the type
8715 -- has some user defined "=" function to avoid generating it.
8717 elsif Is_Interface (Etype (Tag_Typ)) then
8719 Eq_Name := Name_Op_Eq;
8721 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8722 while Present (Prim) loop
8723 if Chars (Node (Prim)) = Name_Op_Eq
8724 and then not Is_Internal (Node (Prim))
8738 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8739 while Present (Prim) loop
8740 if Chars (Node (Prim)) = Name_Op_Eq
8741 and then Is_Internal (Node (Prim))
8744 Eq_Name := Name_Op_Eq;
8752 -- Body of _Alignment
8754 Decl := Predef_Spec_Or_Body (Loc,
8756 Name => Name_uAlignment,
8757 Profile => New_List (
8758 Make_Parameter_Specification (Loc,
8759 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8760 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8762 Ret_Type => Standard_Integer,
8765 Set_Handled_Statement_Sequence (Decl,
8766 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8767 Make_Simple_Return_Statement (Loc,
8769 Make_Attribute_Reference (Loc,
8770 Prefix => Make_Identifier (Loc, Name_X),
8771 Attribute_Name => Name_Alignment)))));
8773 Append_To (Res, Decl);
8777 Decl := Predef_Spec_Or_Body (Loc,
8780 Profile => New_List (
8781 Make_Parameter_Specification (Loc,
8782 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8783 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8785 Ret_Type => Standard_Long_Long_Integer,
8788 Set_Handled_Statement_Sequence (Decl,
8789 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8790 Make_Simple_Return_Statement (Loc,
8792 Make_Attribute_Reference (Loc,
8793 Prefix => Make_Identifier (Loc, Name_X),
8794 Attribute_Name => Name_Size)))));
8796 Append_To (Res, Decl);
8798 -- Bodies for Dispatching stream IO routines. We need these only for
8799 -- non-limited types (in the limited case there is no dispatching).
8800 -- We also skip them if dispatching or finalization are not available.
8802 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Read)
8803 and then No (TSS (Tag_Typ, TSS_Stream_Read))
8805 Build_Record_Read_Procedure (Loc, Tag_Typ, Decl, Ent);
8806 Append_To (Res, Decl);
8809 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Write)
8810 and then No (TSS (Tag_Typ, TSS_Stream_Write))
8812 Build_Record_Write_Procedure (Loc, Tag_Typ, Decl, Ent);
8813 Append_To (Res, Decl);
8816 -- Skip body of _Input for the abstract case, since the corresponding
8817 -- spec is abstract (see Predef_Spec_Or_Body).
8819 if not Is_Abstract_Type (Tag_Typ)
8820 and then Stream_Operation_OK (Tag_Typ, TSS_Stream_Input)
8821 and then No (TSS (Tag_Typ, TSS_Stream_Input))
8823 Build_Record_Or_Elementary_Input_Function
8824 (Loc, Tag_Typ, Decl, Ent);
8825 Append_To (Res, Decl);
8828 if Stream_Operation_OK (Tag_Typ, TSS_Stream_Output)
8829 and then No (TSS (Tag_Typ, TSS_Stream_Output))
8831 Build_Record_Or_Elementary_Output_Procedure
8832 (Loc, Tag_Typ, Decl, Ent);
8833 Append_To (Res, Decl);
8836 -- Ada 2005: Generate bodies for the following primitive operations for
8837 -- limited interfaces and synchronized types that implement a limited
8840 -- disp_asynchronous_select
8841 -- disp_conditional_select
8842 -- disp_get_prim_op_kind
8844 -- disp_timed_select
8846 -- The interface versions will have null bodies
8848 -- These operations cannot be implemented on VM targets, so we simply
8849 -- disable their generation in this case. Disable the generation of
8850 -- these bodies if No_Dispatching_Calls, Ravenscar or ZFP is active.
8852 if Ada_Version >= Ada_05
8853 and then Tagged_Type_Expansion
8854 and then not Is_Interface (Tag_Typ)
8856 ((Is_Interface (Etype (Tag_Typ))
8857 and then Is_Limited_Record (Etype (Tag_Typ)))
8858 or else (Is_Concurrent_Record_Type (Tag_Typ)
8859 and then Has_Interfaces (Tag_Typ)))
8860 and then not Restriction_Active (No_Dispatching_Calls)
8861 and then not Restriction_Active (No_Select_Statements)
8862 and then RTE_Available (RE_Select_Specific_Data)
8864 Append_To (Res, Make_Disp_Asynchronous_Select_Body (Tag_Typ));
8865 Append_To (Res, Make_Disp_Conditional_Select_Body (Tag_Typ));
8866 Append_To (Res, Make_Disp_Get_Prim_Op_Kind_Body (Tag_Typ));
8867 Append_To (Res, Make_Disp_Get_Task_Id_Body (Tag_Typ));
8868 Append_To (Res, Make_Disp_Requeue_Body (Tag_Typ));
8869 Append_To (Res, Make_Disp_Timed_Select_Body (Tag_Typ));
8872 if not Is_Limited_Type (Tag_Typ)
8873 and then not Is_Interface (Tag_Typ)
8875 -- Body for equality
8878 Decl := Make_Eq_Body (Tag_Typ, Eq_Name);
8879 Append_To (Res, Decl);
8882 -- Body for dispatching assignment
8885 Predef_Spec_Or_Body (Loc,
8887 Name => Name_uAssign,
8888 Profile => New_List (
8889 Make_Parameter_Specification (Loc,
8890 Defining_Identifier => Make_Defining_Identifier (Loc, Name_X),
8891 Out_Present => True,
8892 Parameter_Type => New_Reference_To (Tag_Typ, Loc)),
8894 Make_Parameter_Specification (Loc,
8895 Defining_Identifier => Make_Defining_Identifier (Loc, Name_Y),
8896 Parameter_Type => New_Reference_To (Tag_Typ, Loc))),
8899 Set_Handled_Statement_Sequence (Decl,
8900 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8901 Make_Assignment_Statement (Loc,
8902 Name => Make_Identifier (Loc, Name_X),
8903 Expression => Make_Identifier (Loc, Name_Y)))));
8905 Append_To (Res, Decl);
8908 -- Generate dummy bodies for finalization actions of types that have
8909 -- no controlled components.
8911 -- Skip this processing if we are in the finalization routine in the
8912 -- runtime itself, otherwise we get hopelessly circularly confused!
8914 if In_Finalization_Root (Tag_Typ) then
8917 -- Skip this if finalization is not available
8919 elsif Restriction_Active (No_Finalization) then
8922 elsif (Etype (Tag_Typ) = Tag_Typ
8923 or else Is_Controlled (Tag_Typ)
8925 -- Ada 2005 (AI-251): We must also generate these subprograms
8926 -- if the immediate ancestor of Tag_Typ is an interface to
8927 -- ensure the correct initialization of its dispatch table.
8929 or else (not Is_Interface (Tag_Typ)
8931 Is_Interface (Etype (Tag_Typ))))
8932 and then not Has_Controlled_Component (Tag_Typ)
8934 if not Is_Limited_Type (Tag_Typ) then
8935 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Adjust, True);
8937 if Is_Controlled (Tag_Typ) then
8938 Set_Handled_Statement_Sequence (Decl,
8939 Make_Handled_Sequence_Of_Statements (Loc,
8941 Ref => Make_Identifier (Loc, Name_V),
8943 Flist_Ref => Make_Identifier (Loc, Name_L),
8944 With_Attach => Make_Identifier (Loc, Name_B))));
8947 Set_Handled_Statement_Sequence (Decl,
8948 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8949 Make_Null_Statement (Loc))));
8952 Append_To (Res, Decl);
8955 Decl := Predef_Deep_Spec (Loc, Tag_Typ, TSS_Deep_Finalize, True);
8957 if Is_Controlled (Tag_Typ) then
8958 Set_Handled_Statement_Sequence (Decl,
8959 Make_Handled_Sequence_Of_Statements (Loc,
8961 Ref => Make_Identifier (Loc, Name_V),
8963 With_Detach => Make_Identifier (Loc, Name_B))));
8966 Set_Handled_Statement_Sequence (Decl,
8967 Make_Handled_Sequence_Of_Statements (Loc, New_List (
8968 Make_Null_Statement (Loc))));
8971 Append_To (Res, Decl);
8975 end Predefined_Primitive_Bodies;
8977 ---------------------------------
8978 -- Predefined_Primitive_Freeze --
8979 ---------------------------------
8981 function Predefined_Primitive_Freeze
8982 (Tag_Typ : Entity_Id) return List_Id
8984 Loc : constant Source_Ptr := Sloc (Tag_Typ);
8985 Res : constant List_Id := New_List;
8990 Prim := First_Elmt (Primitive_Operations (Tag_Typ));
8991 while Present (Prim) loop
8992 if Is_Predefined_Dispatching_Operation (Node (Prim)) then
8993 Frnodes := Freeze_Entity (Node (Prim), Loc);
8995 if Present (Frnodes) then
8996 Append_List_To (Res, Frnodes);
9004 end Predefined_Primitive_Freeze;
9006 -------------------------
9007 -- Stream_Operation_OK --
9008 -------------------------
9010 function Stream_Operation_OK
9012 Operation : TSS_Name_Type) return Boolean
9014 Has_Predefined_Or_Specified_Stream_Attribute : Boolean := False;
9017 -- Special case of a limited type extension: a default implementation
9018 -- of the stream attributes Read or Write exists if that attribute
9019 -- has been specified or is available for an ancestor type; a default
9020 -- implementation of the attribute Output (resp. Input) exists if the
9021 -- attribute has been specified or Write (resp. Read) is available for
9022 -- an ancestor type. The last condition only applies under Ada 2005.
9024 if Is_Limited_Type (Typ)
9025 and then Is_Tagged_Type (Typ)
9027 if Operation = TSS_Stream_Read then
9028 Has_Predefined_Or_Specified_Stream_Attribute :=
9029 Has_Specified_Stream_Read (Typ);
9031 elsif Operation = TSS_Stream_Write then
9032 Has_Predefined_Or_Specified_Stream_Attribute :=
9033 Has_Specified_Stream_Write (Typ);
9035 elsif Operation = TSS_Stream_Input then
9036 Has_Predefined_Or_Specified_Stream_Attribute :=
9037 Has_Specified_Stream_Input (Typ)
9039 (Ada_Version >= Ada_05
9040 and then Stream_Operation_OK (Typ, TSS_Stream_Read));
9042 elsif Operation = TSS_Stream_Output then
9043 Has_Predefined_Or_Specified_Stream_Attribute :=
9044 Has_Specified_Stream_Output (Typ)
9046 (Ada_Version >= Ada_05
9047 and then Stream_Operation_OK (Typ, TSS_Stream_Write));
9050 -- Case of inherited TSS_Stream_Read or TSS_Stream_Write
9052 if not Has_Predefined_Or_Specified_Stream_Attribute
9053 and then Is_Derived_Type (Typ)
9054 and then (Operation = TSS_Stream_Read
9055 or else Operation = TSS_Stream_Write)
9057 Has_Predefined_Or_Specified_Stream_Attribute :=
9059 (Find_Inherited_TSS (Base_Type (Etype (Typ)), Operation));
9063 -- If the type is not limited, or else is limited but the attribute is
9064 -- explicitly specified or is predefined for the type, then return True,
9065 -- unless other conditions prevail, such as restrictions prohibiting
9066 -- streams or dispatching operations. We also return True for limited
9067 -- interfaces, because they may be extended by nonlimited types and
9068 -- permit inheritance in this case (addresses cases where an abstract
9069 -- extension doesn't get 'Input declared, as per comments below, but
9070 -- 'Class'Input must still be allowed). Note that attempts to apply
9071 -- stream attributes to a limited interface or its class-wide type
9072 -- (or limited extensions thereof) will still get properly rejected
9073 -- by Check_Stream_Attribute.
9075 -- We exclude the Input operation from being a predefined subprogram in
9076 -- the case where the associated type is an abstract extension, because
9077 -- the attribute is not callable in that case, per 13.13.2(49/2). Also,
9078 -- we don't want an abstract version created because types derived from
9079 -- the abstract type may not even have Input available (for example if
9080 -- derived from a private view of the abstract type that doesn't have
9081 -- a visible Input), but a VM such as .NET or the Java VM can treat the
9082 -- operation as inherited anyway, and we don't want an abstract function
9083 -- to be (implicitly) inherited in that case because it can lead to a VM
9086 return (not Is_Limited_Type (Typ)
9087 or else Is_Interface (Typ)
9088 or else Has_Predefined_Or_Specified_Stream_Attribute)
9089 and then (Operation /= TSS_Stream_Input
9090 or else not Is_Abstract_Type (Typ)
9091 or else not Is_Derived_Type (Typ))
9092 and then not Has_Unknown_Discriminants (Typ)
9093 and then not (Is_Interface (Typ)
9094 and then (Is_Task_Interface (Typ)
9095 or else Is_Protected_Interface (Typ)
9096 or else Is_Synchronized_Interface (Typ)))
9097 and then not Restriction_Active (No_Streams)
9098 and then not Restriction_Active (No_Dispatch)
9099 and then not No_Run_Time_Mode
9100 and then RTE_Available (RE_Tag)
9101 and then RTE_Available (RE_Root_Stream_Type);
9102 end Stream_Operation_OK;