1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Debug
; use Debug
;
29 with Elists
; use Elists
;
30 with Einfo
; use Einfo
;
31 with Einfo
.Entities
; use Einfo
.Entities
;
32 with Einfo
.Utils
; use Einfo
.Utils
;
33 with Exp_Disp
; use Exp_Disp
;
34 with Exp_Util
; use Exp_Util
;
35 with Exp_Ch6
; use Exp_Ch6
;
36 with Exp_Ch7
; use Exp_Ch7
;
37 with Exp_Tss
; use Exp_Tss
;
38 with Errout
; use Errout
;
39 with Freeze
; use Freeze
;
40 with Lib
.Xref
; use Lib
.Xref
;
41 with Namet
; use Namet
;
42 with Nlists
; use Nlists
;
43 with Nmake
; use Nmake
;
45 with Output
; use Output
;
46 with Restrict
; use Restrict
;
47 with Rident
; use Rident
;
49 with Sem_Aux
; use Sem_Aux
;
50 with Sem_Ch6
; use Sem_Ch6
;
51 with Sem_Ch8
; use Sem_Ch8
;
52 with Sem_Eval
; use Sem_Eval
;
53 with Sem_Type
; use Sem_Type
;
54 with Sem_Util
; use Sem_Util
;
55 with Snames
; use Snames
;
56 with Sinfo
; use Sinfo
;
57 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
58 with Sinfo
.Utils
; use Sinfo
.Utils
;
59 with Tbuild
; use Tbuild
;
60 with Uintp
; use Uintp
;
61 with Warnsw
; use Warnsw
;
63 package body Sem_Disp
is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Add_Dispatching_Operation
70 (Tagged_Type
: Entity_Id
;
72 -- Add New_Op in the list of primitive operations of Tagged_Type
74 function Check_Controlling_Type
76 Subp
: Entity_Id
) return Entity_Id
;
77 -- T is the tagged type of a formal parameter or the result of Subp.
78 -- If the subprogram has a controlling parameter or result that matches
79 -- the type, then returns the tagged type of that parameter or result
80 -- (returning the designated tagged type in the case of an access
81 -- parameter); otherwise returns empty.
83 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
;
84 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
85 -- type of S that has the same name of S, a type-conformant profile, an
86 -- original corresponding operation O that is a primitive of a visible
87 -- ancestor of the dispatching type of S and O is visible at the point of
88 -- of declaration of S. If the entity is found the Alias of S is set to the
89 -- original corresponding operation S and its Overridden_Operation is set
90 -- to the found entity; otherwise return Empty.
92 -- This routine does not search for non-hidden primitives since they are
93 -- covered by the normal Ada 2005 rules.
95 function Is_Inherited_Public_Operation
(Op
: Entity_Id
) return Boolean;
96 -- Check whether a primitive operation is inherited from an operation
97 -- declared in the visible part of its package.
99 -------------------------------
100 -- Add_Dispatching_Operation --
101 -------------------------------
103 procedure Add_Dispatching_Operation
104 (Tagged_Type
: Entity_Id
;
107 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
110 -- The dispatching operation may already be on the list, if it is the
111 -- wrapper for an inherited function of a null extension (see Exp_Ch3
112 -- for the construction of function wrappers). The list of primitive
113 -- operations must not contain duplicates.
115 -- The Default_Initial_Condition and invariant procedures are not added
116 -- to the list of primitives even when they are generated for a tagged
117 -- type. These routines must not be targets of dispatching calls and
118 -- therefore must not appear in the dispatch table because they already
119 -- utilize class-wide-precondition semantics to handle inheritance and
122 if Is_Suitable_Primitive
(New_Op
) then
123 Append_Unique_Elmt
(New_Op
, List
);
125 end Add_Dispatching_Operation
;
127 --------------------------
128 -- Covered_Interface_Op --
129 --------------------------
131 function Covered_Interface_Op
(Prim
: Entity_Id
) return Entity_Id
is
132 Tagged_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
137 pragma Assert
(Is_Dispatching_Operation
(Prim
));
139 -- Although this is a dispatching primitive we must check if its
140 -- dispatching type is available because it may be the primitive
141 -- of a private type not defined as tagged in its partial view.
143 if Present
(Tagged_Type
) and then Has_Interfaces
(Tagged_Type
) then
145 -- If the tagged type is frozen then the internal entities associated
146 -- with interfaces are available in the list of primitives of the
147 -- tagged type and can be used to speed up this search.
149 if Is_Frozen
(Tagged_Type
) then
150 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
151 while Present
(Elmt
) loop
154 if Present
(Interface_Alias
(E
))
155 and then Alias
(E
) = Prim
157 return Interface_Alias
(E
);
163 -- Otherwise we must collect all the interface primitives and check
164 -- if the Prim overrides (implements) some interface primitive.
168 Ifaces_List
: Elist_Id
;
169 Iface_Elmt
: Elmt_Id
;
171 Iface_Prim
: Entity_Id
;
174 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
175 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
176 while Present
(Iface_Elmt
) loop
177 Iface
:= Node
(Iface_Elmt
);
179 Elmt
:= First_Elmt
(Primitive_Operations
(Iface
));
180 while Present
(Elmt
) loop
181 Iface_Prim
:= Node
(Elmt
);
183 if Chars
(Iface_Prim
) = Chars
(Prim
)
184 and then Is_Interface_Conformant
185 (Tagged_Type
, Iface_Prim
, Prim
)
193 Next_Elmt
(Iface_Elmt
);
200 end Covered_Interface_Op
;
202 ----------------------------------
203 -- Covered_Interface_Primitives --
204 ----------------------------------
206 function Covered_Interface_Primitives
(Prim
: Entity_Id
) return Elist_Id
is
207 Tagged_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
210 Result
: Elist_Id
:= No_Elist
;
213 pragma Assert
(Is_Dispatching_Operation
(Prim
));
215 -- Although this is a dispatching primitive we must check if its
216 -- dispatching type is available because it may be the primitive
217 -- of a private type not defined as tagged in its partial view.
219 if Present
(Tagged_Type
) and then Has_Interfaces
(Tagged_Type
) then
221 -- If the tagged type is frozen then the internal entities associated
222 -- with interfaces are available in the list of primitives of the
223 -- tagged type and can be used to speed up this search.
225 if Is_Frozen
(Tagged_Type
) then
226 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
227 while Present
(Elmt
) loop
230 if Present
(Interface_Alias
(E
))
231 and then Alias
(E
) = Prim
234 Result
:= New_Elmt_List
;
237 Append_Elmt
(Interface_Alias
(E
), Result
);
243 -- Otherwise we must collect all the interface primitives and check
244 -- whether the Prim overrides (implements) some interface primitive.
248 Ifaces_List
: Elist_Id
;
249 Iface_Elmt
: Elmt_Id
;
251 Iface_Prim
: Entity_Id
;
254 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
256 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
257 while Present
(Iface_Elmt
) loop
258 Iface
:= Node
(Iface_Elmt
);
260 Elmt
:= First_Elmt
(Primitive_Operations
(Iface
));
261 while Present
(Elmt
) loop
262 Iface_Prim
:= Node
(Elmt
);
264 if Chars
(Iface_Prim
) = Chars
(Prim
)
265 and then Is_Interface_Conformant
266 (Tagged_Type
, Iface_Prim
, Prim
)
269 Result
:= New_Elmt_List
;
272 Append_Elmt
(Iface_Prim
, Result
);
278 Next_Elmt
(Iface_Elmt
);
285 end Covered_Interface_Primitives
;
287 -------------------------------
288 -- Check_Controlling_Formals --
289 -------------------------------
291 procedure Check_Controlling_Formals
296 Ctrl_Type
: Entity_Id
;
299 -- We skip the check for thunks
301 if Is_Thunk
(Subp
) then
305 Formal
:= First_Formal
(Subp
);
306 while Present
(Formal
) loop
307 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
309 if Present
(Ctrl_Type
) then
311 -- Obtain the full type in case we are looking at an incomplete
314 if Ekind
(Ctrl_Type
) = E_Incomplete_Type
315 and then Present
(Full_View
(Ctrl_Type
))
317 Ctrl_Type
:= Full_View
(Ctrl_Type
);
320 -- When controlling type is concurrent and declared within a
321 -- generic or inside an instance use corresponding record type.
323 if Is_Concurrent_Type
(Ctrl_Type
)
324 and then Present
(Corresponding_Record_Type
(Ctrl_Type
))
326 Ctrl_Type
:= Corresponding_Record_Type
(Ctrl_Type
);
329 if Ctrl_Type
= Typ
then
330 Set_Is_Controlling_Formal
(Formal
);
332 -- Ada 2005 (AI-231): Anonymous access types that are used in
333 -- controlling parameters exclude null because it is necessary
334 -- to read the tag to dispatch, and null has no tag.
336 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
337 Set_Can_Never_Be_Null
(Etype
(Formal
));
338 Set_Is_Known_Non_Null
(Etype
(Formal
));
341 -- Check that the parameter's nominal subtype statically
342 -- matches the first subtype.
344 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
345 if not Subtypes_Statically_Match
346 (Typ
, Designated_Type
(Etype
(Formal
)))
349 ("parameter subtype does not match controlling type",
353 -- Within a predicate function, the formal may be a subtype
354 -- of a tagged type, given that the predicate is expressed
355 -- in terms of the subtype.
357 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
))
358 and then not Is_Predicate_Function
(Subp
)
361 ("parameter subtype does not match controlling type",
365 if Present
(Default_Value
(Formal
)) then
367 -- In Ada 2005, access parameters can have defaults
369 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
370 and then Ada_Version
< Ada_2005
373 ("default not allowed for controlling access parameter",
374 Default_Value
(Formal
));
376 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
378 ("default expression must be a tag indeterminate" &
379 " function call", Default_Value
(Formal
));
383 elsif Comes_From_Source
(Subp
) then
385 ("operation can be dispatching in only one type", Subp
);
389 Next_Formal
(Formal
);
392 if Ekind
(Subp
) in E_Function | E_Generic_Function
then
393 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
395 if Present
(Ctrl_Type
) then
396 if Ctrl_Type
= Typ
then
397 Set_Has_Controlling_Result
(Subp
);
399 -- Check that result subtype statically matches first subtype
400 -- (Ada 2005): Subp may have a controlling access result.
402 if Subtypes_Statically_Match
(Typ
, Etype
(Subp
))
403 or else (Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
405 Subtypes_Statically_Match
406 (Typ
, Designated_Type
(Etype
(Subp
))))
412 ("result subtype does not match controlling type", Subp
);
415 elsif Comes_From_Source
(Subp
) then
417 ("operation can be dispatching in only one type", Subp
);
421 end Check_Controlling_Formals
;
423 ----------------------------
424 -- Check_Controlling_Type --
425 ----------------------------
427 function Check_Controlling_Type
429 Subp
: Entity_Id
) return Entity_Id
431 Tagged_Type
: Entity_Id
:= Empty
;
434 if Is_Tagged_Type
(T
) then
435 if Is_First_Subtype
(T
) then
438 Tagged_Type
:= Base_Type
(T
);
441 -- If the type is incomplete, it may have been declared without a
442 -- Tagged indication, but the full view may be tagged, in which case
443 -- that is the controlling type of the subprogram. This is one of the
444 -- approx. 579 places in the language where a lookahead would help.
446 elsif Ekind
(T
) = E_Incomplete_Type
447 and then Present
(Full_View
(T
))
448 and then Is_Tagged_Type
(Full_View
(T
))
450 Set_Is_Tagged_Type
(T
);
451 Tagged_Type
:= Full_View
(T
);
453 elsif Ekind
(T
) = E_Anonymous_Access_Type
454 and then Is_Tagged_Type
(Designated_Type
(T
))
456 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
457 if Is_First_Subtype
(Designated_Type
(T
)) then
458 Tagged_Type
:= Designated_Type
(T
);
460 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
463 -- Ada 2005: an incomplete type can be tagged. An operation with an
464 -- access parameter of the type is dispatching.
466 elsif Scope
(Designated_Type
(T
)) = Current_Scope
then
467 Tagged_Type
:= Designated_Type
(T
);
469 -- Ada 2005 (AI-50217)
471 elsif From_Limited_With
(Designated_Type
(T
))
472 and then Has_Non_Limited_View
(Designated_Type
(T
))
473 and then Scope
(Designated_Type
(T
)) = Scope
(Subp
)
475 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
476 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
478 Tagged_Type
:= Base_Type
(Non_Limited_View
479 (Designated_Type
(T
)));
484 if No
(Tagged_Type
) or else Is_Class_Wide_Type
(Tagged_Type
) then
487 -- In the special case of a protected subprogram of a tagged protected
488 -- type that has a formal of a tagged type (or access formal whose type
489 -- designates a tagged type), such a formal is not controlling unless
490 -- it's of the protected type's corresponding record type. The latter
491 -- can occur for the special wrapper subprograms created for protected
492 -- subprograms. Such subprograms may occur in the same scope where some
493 -- formal's tagged type is declared, and we don't want formals of that
494 -- tagged type being marked as controlling, for one thing because they
495 -- aren't controlling from the language point of view, but also because
496 -- this can cause errors for access formals when conformance is checked
497 -- between the spec and body of the protected subprogram (null-exclusion
498 -- status of the formals may be set differently, which is the case that
499 -- led to adding this check).
501 elsif Is_Subprogram
(Subp
)
502 and then Present
(Protected_Subprogram
(Subp
))
503 and then Ekind
(Scope
(Protected_Subprogram
(Subp
))) = E_Protected_Type
505 Base_Type
(Tagged_Type
)
506 /= Corresponding_Record_Type
(Scope
(Protected_Subprogram
(Subp
)))
510 -- The dispatching type and the primitive operation must be defined in
511 -- the same scope, except in the case of abstract formal subprograms.
513 elsif (Scope
(Subp
) = Scope
(Tagged_Type
)
514 and then (not Is_Generic_Type
(Tagged_Type
)
515 or else not Comes_From_Source
(Subp
)))
517 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
519 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
521 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
523 Is_Abstract_Subprogram
(Subp
))
530 end Check_Controlling_Type
;
532 ----------------------------
533 -- Check_Dispatching_Call --
534 ----------------------------
536 procedure Check_Dispatching_Call
(N
: Node_Id
) is
537 Loc
: constant Source_Ptr
:= Sloc
(N
);
540 Control
: Node_Id
:= Empty
;
542 Subp_Entity
: Entity_Id
;
544 Indeterm_Ctrl_Type
: Entity_Id
:= Empty
;
545 -- Type of a controlling formal whose actual is a tag-indeterminate call
546 -- whose result type is different from, but is an ancestor of, the type.
548 Static_Tag
: Node_Id
:= Empty
;
549 -- If a controlling formal has a statically tagged actual, the tag of
550 -- this actual is to be used for any tag-indeterminate actual.
552 procedure Check_Direct_Call
;
553 -- In the case when the controlling actual is a class-wide type whose
554 -- root type's completion is a task or protected type, the call is in
555 -- fact direct. This routine detects the above case and modifies the
558 procedure Check_Dispatching_Context
(Call
: Node_Id
);
559 -- If the call is tag-indeterminate and the entity being called is
560 -- abstract, verify that the context is a call that will eventually
561 -- provide a tag for dispatching, or has provided one already.
563 -----------------------
564 -- Check_Direct_Call --
565 -----------------------
567 procedure Check_Direct_Call
is
568 Typ
: Entity_Id
:= Etype
(Control
);
570 -- Predefined primitives do not receive wrappers since they are built
571 -- from scratch for the corresponding record of synchronized types.
572 -- Equality is in general predefined, but is excluded from the check
573 -- when it is user-defined.
575 if Is_Predefined_Dispatching_Operation
(Subp_Entity
)
576 and then not (Is_User_Defined_Equality
(Subp_Entity
)
577 and then Comes_From_Source
(Subp_Entity
)
578 and then Nkind
(Parent
(Subp_Entity
)) =
579 N_Function_Specification
)
584 if Is_Class_Wide_Type
(Typ
) then
585 Typ
:= Root_Type
(Typ
);
588 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
589 Typ
:= Full_View
(Typ
);
592 if Is_Concurrent_Type
(Typ
)
594 Present
(Corresponding_Record_Type
(Typ
))
596 Typ
:= Corresponding_Record_Type
(Typ
);
598 -- The concurrent record's list of primitives should contain a
599 -- wrapper for the entity of the call, retrieve it.
604 Wrapper_Found
: Boolean := False;
607 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
608 while Present
(Prim_Elmt
) loop
609 Prim
:= Node
(Prim_Elmt
);
611 if Is_Primitive_Wrapper
(Prim
)
612 and then Wrapped_Entity
(Prim
) = Subp_Entity
614 Wrapper_Found
:= True;
618 Next_Elmt
(Prim_Elmt
);
621 -- A primitive declared between two views should have a
622 -- corresponding wrapper.
624 pragma Assert
(Wrapper_Found
);
626 -- Modify the call by setting the proper entity
628 Set_Entity
(Name
(N
), Prim
);
631 end Check_Direct_Call
;
633 -------------------------------
634 -- Check_Dispatching_Context --
635 -------------------------------
637 procedure Check_Dispatching_Context
(Call
: Node_Id
) is
638 Subp
: constant Entity_Id
:= Entity
(Name
(Call
));
640 procedure Abstract_Context_Error
;
641 -- Error for abstract call dispatching on result is not dispatching
643 function Has_Controlling_Current_Instance_Actual_In_DIC
644 (Call
: Node_Id
) return Boolean;
645 -- Return True if the subprogram call Call has a controlling actual
646 -- given directly by a current instance referenced within a DIC
649 ----------------------------
650 -- Abstract_Context_Error --
651 ----------------------------
653 procedure Abstract_Context_Error
is
655 if Ekind
(Subp
) = E_Function
then
657 ("call to abstract function must be dispatching", N
);
659 -- This error can occur for a procedure in the case of a call to
660 -- an abstract formal procedure with a statically tagged operand.
664 ("call to abstract procedure must be dispatching", N
);
666 end Abstract_Context_Error
;
668 ----------------------------------------
669 -- Has_Current_Instance_Actual_In_DIC --
670 ----------------------------------------
672 function Has_Controlling_Current_Instance_Actual_In_DIC
673 (Call
: Node_Id
) return Boolean
678 F
:= First_Formal
(Subp_Entity
);
679 A
:= First_Actual
(Call
);
681 while Present
(F
) loop
683 -- Return True if the actual denotes a current instance (which
684 -- will be represented by an in-mode formal of the enclosing
685 -- DIC_Procedure) passed to a controlling formal. We don't have
686 -- to worry about controlling access formals here, because its
687 -- illegal to apply Access (etc.) attributes to a current
688 -- instance within an aspect (by AI12-0068).
690 if Is_Controlling_Formal
(F
)
691 and then Nkind
(A
) = N_Identifier
692 and then Ekind
(Entity
(A
)) = E_In_Parameter
693 and then Is_Subprogram
(Scope
(Entity
(A
)))
694 and then Is_DIC_Procedure
(Scope
(Entity
(A
)))
704 end Has_Controlling_Current_Instance_Actual_In_DIC
;
708 Scop
: constant Entity_Id
:= Current_Scope_No_Loops
;
709 Typ
: constant Entity_Id
:= Etype
(Subp
);
712 -- Start of processing for Check_Dispatching_Context
715 -- Skip checking context of dispatching calls during preanalysis of
716 -- class-wide conditions since at that stage the expression is not
717 -- installed yet on its definite context.
719 if Inside_Class_Condition_Preanalysis
then
723 -- If the called subprogram is a private overriding, replace it
724 -- with its alias, which has the correct body. Verify that the
725 -- two subprograms have the same controlling type (this is not the
726 -- case for an inherited subprogram that has become abstract).
728 if Is_Abstract_Subprogram
(Subp
)
729 and then No
(Controlling_Argument
(Call
))
731 if Present
(Alias
(Subp
))
732 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
733 and then No
(DTC_Entity
(Subp
))
734 and then Find_Dispatching_Type
(Subp
) =
735 Find_Dispatching_Type
(Alias
(Subp
))
737 -- Private overriding of inherited abstract operation, call is
740 Set_Entity
(Name
(N
), Alias
(Subp
));
743 -- If this is a pre/postcondition for an abstract subprogram,
744 -- it may call another abstract function that is a primitive
745 -- of an abstract type. The call is nondispatching but will be
746 -- legal in overridings of the operation. However, if the call
747 -- is tag-indeterminate we want to continue with with the error
748 -- checking below, as this case is illegal even for abstract
749 -- subprograms (see AI12-0170).
751 -- Similarly, as per AI12-0412, a nonabstract subprogram may
752 -- have a class-wide pre/postcondition that includes a call to
753 -- an abstract primitive of the subprogram's controlling type.
754 -- Certain operations (nondispatching calls, 'Access, use as
755 -- a generic actual) applied to such a nonabstract subprogram
756 -- are illegal in the case where the type is abstract (see
757 -- RM 6.1.1(18.2/5)).
759 elsif Is_Subprogram
(Scop
)
760 and then not Is_Tag_Indeterminate
(N
)
762 -- The context is an internally built helper or an indirect
763 -- call wrapper that handles class-wide preconditions
764 (Present
(Class_Preconditions_Subprogram
(Scop
))
766 -- ... or the context is a class-wide pre/postcondition.
768 (In_Pre_Post_Condition
(Call
, Class_Wide_Only
=> True)
770 -- The tagged type associated with the called
771 -- subprogram must be the same as that of the
772 -- subprogram with a class-wide aspect.
774 and then Is_Dispatching_Operation
(Scop
)
775 and then Find_Dispatching_Type
(Subp
)
776 = Find_Dispatching_Type
(Scop
)))
780 -- Similarly to the dispensation for postconditions, a call to
781 -- an abstract function within a Default_Initial_Condition aspect
782 -- can be legal when passed a current instance of the type. Such
783 -- a call will be effectively mapped to a call to a primitive of
784 -- a descendant type (see AI12-0397, as well as AI12-0170), so
785 -- doesn't need to be dispatching. We test for being within a DIC
786 -- procedure, since that's where the call will be analyzed.
788 elsif Is_Subprogram
(Scop
)
789 and then Is_DIC_Procedure
(Scop
)
790 and then Has_Controlling_Current_Instance_Actual_In_DIC
(Call
)
794 elsif Ekind
(Current_Scope
) = E_Function
795 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
796 N_Generic_Subprogram_Declaration
801 -- We need to determine whether the context of the call
802 -- provides a tag to make the call dispatching. This requires
803 -- the call to be the actual in an enclosing call, and that
804 -- actual must be controlling. If the call is an operand of
805 -- equality, the other operand must not be abstract.
807 if not Is_Tagged_Type
(Typ
)
809 (Ekind
(Typ
) = E_Anonymous_Access_Type
810 and then Is_Tagged_Type
(Designated_Type
(Typ
)))
812 Abstract_Context_Error
;
816 Par
:= Parent
(Call
);
818 if Nkind
(Par
) = N_Parameter_Association
then
822 if Nkind
(Par
) = N_Qualified_Expression
823 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
828 if Nkind
(Par
) in N_Subprogram_Call
829 and then Is_Entity_Name
(Name
(Par
))
832 Enc_Subp
: constant Entity_Id
:= Entity
(Name
(Par
));
836 Ret_Type
: Entity_Id
;
839 -- Find controlling formal that can provide tag for the
840 -- tag-indeterminate actual. The corresponding actual
841 -- must be the corresponding class-wide type.
843 F
:= First_Formal
(Enc_Subp
);
844 A
:= First_Actual
(Par
);
846 -- Find controlling type of call. Dereference if function
847 -- returns an access type.
849 Ret_Type
:= Etype
(Call
);
850 if Is_Access_Type
(Etype
(Call
)) then
851 Ret_Type
:= Designated_Type
(Ret_Type
);
854 while Present
(F
) loop
855 Control
:= Etype
(A
);
857 if Is_Access_Type
(Control
) then
858 Control
:= Designated_Type
(Control
);
861 if Is_Controlling_Formal
(F
)
862 and then not (Call
= A
or else Parent
(Call
) = A
)
863 and then Control
= Class_Wide_Type
(Ret_Type
)
872 if Nkind
(Par
) = N_Function_Call
873 and then Is_Tag_Indeterminate
(Par
)
875 -- The parent may be an actual of an enclosing call
877 Check_Dispatching_Context
(Par
);
882 ("call to abstract function must be dispatching",
888 -- For equality operators, one of the operands must be
889 -- statically or dynamically tagged.
891 elsif Nkind
(Par
) in N_Op_Eq | N_Op_Ne
then
892 if N
= Right_Opnd
(Par
)
893 and then Is_Tag_Indeterminate
(Left_Opnd
(Par
))
895 Abstract_Context_Error
;
897 elsif N
= Left_Opnd
(Par
)
898 and then Is_Tag_Indeterminate
(Right_Opnd
(Par
))
900 Abstract_Context_Error
;
905 -- The left-hand side of an assignment provides the tag
907 elsif Nkind
(Par
) = N_Assignment_Statement
then
911 Abstract_Context_Error
;
915 end Check_Dispatching_Context
;
917 -- Start of processing for Check_Dispatching_Call
920 -- Find a controlling argument, if any
922 if Present
(Parameter_Associations
(N
)) then
923 Subp_Entity
:= Entity
(Name
(N
));
925 Actual
:= First_Actual
(N
);
926 Formal
:= First_Formal
(Subp_Entity
);
927 while Present
(Actual
) loop
928 Control
:= Find_Controlling_Arg
(Actual
);
929 exit when Present
(Control
);
931 -- Check for the case where the actual is a tag-indeterminate call
932 -- whose result type is different than the tagged type associated
933 -- with the containing call, but is an ancestor of the type.
935 if Is_Controlling_Formal
(Formal
)
936 and then Is_Tag_Indeterminate
(Actual
)
937 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
938 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
940 Indeterm_Ctrl_Type
:= Etype
(Formal
);
942 -- If the formal is controlling but the actual is not, the type
943 -- of the actual is statically known, and may be used as the
944 -- controlling tag for some other tag-indeterminate actual.
946 elsif Is_Controlling_Formal
(Formal
)
947 and then Is_Entity_Name
(Actual
)
948 and then Is_Tagged_Type
(Etype
(Actual
))
950 Static_Tag
:= Etype
(Actual
);
953 Next_Actual
(Actual
);
954 Next_Formal
(Formal
);
957 if Present
(Control
) then
959 -- Verify that no controlling arguments are statically tagged
962 Write_Str
("Found Dispatching call");
967 Actual
:= First_Actual
(N
);
968 while Present
(Actual
) loop
969 if Actual
/= Control
then
971 if not Is_Controlling_Actual
(Actual
) then
972 null; -- Can be anything
974 elsif Is_Dynamically_Tagged
(Actual
) then
975 null; -- Valid parameter
977 elsif Is_Tag_Indeterminate
(Actual
) then
979 -- The tag is inherited from the enclosing call (the node
980 -- we are currently analyzing). Explicitly expand the
981 -- actual, since the previous call to Expand (from
982 -- Resolve_Call) had no way of knowing about the
983 -- required dispatching.
985 Propagate_Tag
(Control
, Actual
);
989 ("controlling argument is not dynamically tagged",
995 Next_Actual
(Actual
);
998 -- Mark call as a dispatching call
1000 Set_Controlling_Argument
(N
, Control
);
1001 Check_Restriction
(No_Dispatching_Calls
, N
);
1003 -- The dispatching call may need to be converted into a direct
1004 -- call in certain cases.
1008 -- If the call doesn't have a controlling actual but does have an
1009 -- indeterminate actual that requires dispatching treatment, then an
1010 -- object is needed that will serve as the controlling argument for
1011 -- a dispatching call on the indeterminate actual. This can occur
1012 -- in the unusual situation of a default actual given by a tag-
1013 -- indeterminate call and where the type of the call is an ancestor
1014 -- of the type associated with a containing call to an inherited
1015 -- operation (see AI-239).
1017 -- Rather than create an object of the tagged type, which would
1018 -- be problematic for various reasons (default initialization,
1019 -- discriminants), the tag of the containing call's associated
1020 -- tagged type is directly used to control the dispatching.
1022 elsif Present
(Indeterm_Ctrl_Type
) then
1023 if Present
(Static_Tag
) then
1025 Make_Attribute_Reference
(Loc
,
1027 New_Occurrence_Of
(Static_Tag
, Loc
),
1028 Attribute_Name
=> Name_Tag
);
1032 Make_Attribute_Reference
(Loc
,
1034 New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
1035 Attribute_Name
=> Name_Tag
);
1040 Actual
:= First_Actual
(N
);
1041 Formal
:= First_Formal
(Subp_Entity
);
1042 while Present
(Actual
) loop
1043 if Is_Tag_Indeterminate
(Actual
)
1044 and then Is_Controlling_Formal
(Formal
)
1046 Propagate_Tag
(Control
, Actual
);
1049 Next_Actual
(Actual
);
1050 Next_Formal
(Formal
);
1053 Check_Dispatching_Context
(N
);
1055 elsif Nkind
(N
) /= N_Function_Call
then
1057 -- The call is not dispatching, so check that there aren't any
1058 -- tag-indeterminate abstract calls left among its actuals.
1060 Actual
:= First_Actual
(N
);
1061 while Present
(Actual
) loop
1062 if Is_Tag_Indeterminate
(Actual
) then
1064 -- Function call case
1066 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
1067 Func
:= Entity
(Name
(Original_Node
(Actual
)));
1069 -- If the actual is an attribute then it can't be abstract
1070 -- (the only current case of a tag-indeterminate attribute
1071 -- is the stream Input attribute).
1073 elsif Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
1077 -- Ditto if it is an explicit dereference
1079 elsif Nkind
(Original_Node
(Actual
)) = N_Explicit_Dereference
1083 -- Only other possibility is a qualified expression whose
1084 -- constituent expression is itself a call.
1088 Entity
(Name
(Original_Node
1089 (Expression
(Original_Node
(Actual
)))));
1092 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
1094 ("call to abstract function must be dispatching",
1099 Next_Actual
(Actual
);
1102 Check_Dispatching_Context
(N
);
1104 elsif Nkind
(Parent
(N
)) in N_Subexpr
then
1105 Check_Dispatching_Context
(N
);
1107 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
1108 and then Is_Class_Wide_Type
(Etype
(Name
(Parent
(N
))))
1112 elsif Is_Abstract_Subprogram
(Subp_Entity
) then
1113 Check_Dispatching_Context
(N
);
1117 -- If this is a nondispatching call to a nonabstract subprogram
1118 -- and the subprogram has any Pre'Class or Post'Class aspects with
1119 -- nonstatic values, then report an error. This is specified by
1120 -- RM 6.1.1(18.2/5) (by AI12-0412).
1122 -- Skip reporting this error on helpers and indirect-call wrappers
1123 -- built to support class-wide preconditions.
1126 and then not Is_Abstract_Subprogram
(Subp_Entity
)
1128 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post
(Subp_Entity
)
1130 (Is_Subprogram
(Current_Scope
)
1132 Present
(Class_Preconditions_Subprogram
(Current_Scope
)))
1135 ("nondispatching call to nonabstract subprogram of "
1136 & "abstract type with nonstatic class-wide "
1137 & "pre/postconditions",
1142 -- If dispatching on result, the enclosing call, if any, will
1143 -- determine the controlling argument. Otherwise this is the
1144 -- primitive operation of the root type.
1146 Check_Dispatching_Context
(N
);
1148 end Check_Dispatching_Call
;
1150 ---------------------------------
1151 -- Check_Dispatching_Operation --
1152 ---------------------------------
1154 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
1155 function Is_Access_To_Subprogram_Wrapper
(E
: Entity_Id
) return Boolean;
1156 -- Return True if E is an access to subprogram wrapper
1158 procedure Warn_On_Late_Primitive_After_Private_Extension
1161 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
1162 -- if it is a public primitive defined after some private extension of
1165 -------------------------------------
1166 -- Is_Access_To_Subprogram_Wrapper --
1167 -------------------------------------
1169 function Is_Access_To_Subprogram_Wrapper
(E
: Entity_Id
) return Boolean
1171 Decl_N
: constant Node_Id
:= Unit_Declaration_Node
(E
);
1172 Par_N
: constant Node_Id
:= Parent
(List_Containing
(Decl_N
));
1175 -- Access to subprogram wrappers are declared in the freezing actions
1177 return Nkind
(Par_N
) = N_Freeze_Entity
1178 and then Ekind
(Entity
(Par_N
)) = E_Access_Subprogram_Type
;
1179 end Is_Access_To_Subprogram_Wrapper
;
1181 ----------------------------------------------------
1182 -- Warn_On_Late_Primitive_After_Private_Extension --
1183 ----------------------------------------------------
1185 procedure Warn_On_Late_Primitive_After_Private_Extension
1192 if Warn_On_Late_Primitives
1193 and then Comes_From_Source
(Prim
)
1194 and then Has_Private_Extension
(Typ
)
1195 and then Is_Package_Or_Generic_Package
(Current_Scope
)
1196 and then not In_Private_Part
(Current_Scope
)
1198 E
:= Next_Entity
(Typ
);
1200 while E
/= Prim
loop
1201 if Ekind
(E
) = E_Record_Type_With_Private
1202 and then Etype
(E
) = Typ
1204 Error_Msg_Name_1
:= Chars
(Typ
);
1205 Error_Msg_Name_2
:= Chars
(E
);
1206 Error_Msg_Sloc
:= Sloc
(E
);
1208 ("?.j?primitive of type % defined after private extension "
1210 Error_Msg_Name_1
:= Chars
(Prim
);
1211 Error_Msg_Name_2
:= Chars
(E
);
1213 ("\spec of % should appear before declaration of type %!",
1221 end Warn_On_Late_Primitive_After_Private_Extension
;
1225 Body_Is_Last_Primitive
: Boolean := False;
1226 Has_Dispatching_Parent
: Boolean := False;
1227 Ovr_Subp
: Entity_Id
:= Empty
;
1228 Tagged_Type
: Entity_Id
;
1230 -- Start of processing for Check_Dispatching_Operation
1233 if Ekind
(Subp
) not in E_Function | E_Procedure
then
1236 -- The Default_Initial_Condition procedure is not a primitive subprogram
1237 -- even if it relates to a tagged type. This routine is not meant to be
1238 -- inherited or overridden.
1240 elsif Is_DIC_Procedure
(Subp
) then
1243 -- The "partial" and "full" type invariant procedures are not primitive
1244 -- subprograms even if they relate to a tagged type. These routines are
1245 -- not meant to be inherited or overridden.
1247 elsif Is_Invariant_Procedure
(Subp
)
1248 or else Is_Partial_Invariant_Procedure
(Subp
)
1252 -- Wrappers of access to subprograms are not primitive subprograms.
1254 elsif Is_Wrapper
(Subp
)
1255 and then Is_Access_To_Subprogram_Wrapper
(Subp
)
1260 Set_Is_Dispatching_Operation
(Subp
, False);
1261 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1263 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1264 -- Required because primitives of concurrent types are attached
1265 -- to the corresponding record (not to the concurrent type).
1267 if Ada_Version
>= Ada_2005
1268 and then Present
(Tagged_Type
)
1269 and then Is_Concurrent_Type
(Tagged_Type
)
1270 and then Present
(Corresponding_Record_Type
(Tagged_Type
))
1272 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
1275 -- (AI-345): The task body procedure is not a primitive of the tagged
1278 if Present
(Tagged_Type
)
1279 and then Is_Concurrent_Record_Type
(Tagged_Type
)
1280 and then Present
(Corresponding_Concurrent_Type
(Tagged_Type
))
1281 and then Is_Task_Type
(Corresponding_Concurrent_Type
(Tagged_Type
))
1282 and then Subp
= Get_Task_Body_Procedure
1283 (Corresponding_Concurrent_Type
(Tagged_Type
))
1288 -- If Subp is derived from a dispatching operation then it should
1289 -- always be treated as dispatching. In this case various checks
1290 -- below will be bypassed. Makes sure that late declarations for
1291 -- inherited private subprograms are treated as dispatching, even
1292 -- if the associated tagged type is already frozen.
1294 Has_Dispatching_Parent
:=
1295 Present
(Alias
(Subp
))
1296 and then Is_Dispatching_Operation
(Alias
(Subp
));
1298 if No
(Tagged_Type
) then
1300 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1301 -- with an abstract interface type unless the interface acts as a
1302 -- parent type in a derivation. If the interface type is a formal
1303 -- type then the operation is not primitive and therefore legal.
1310 E
:= First_Entity
(Subp
);
1311 while Present
(E
) loop
1313 -- For an access parameter, check designated type
1315 if Ekind
(Etype
(E
)) = E_Anonymous_Access_Type
then
1316 Typ
:= Designated_Type
(Etype
(E
));
1321 if Comes_From_Source
(Subp
)
1322 and then Is_Interface
(Typ
)
1323 and then not Is_Class_Wide_Type
(Typ
)
1324 and then not Is_Derived_Type
(Typ
)
1325 and then not Is_Generic_Type
(Typ
)
1326 and then not In_Instance
1328 Error_Msg_N
("??declaration of& is too late!", Subp
);
1329 Error_Msg_NE
-- CODEFIX??
1330 ("\??spec should appear immediately after declaration of "
1331 & "& !", Subp
, Typ
);
1338 -- In case of functions check also the result type
1340 if Ekind
(Subp
) = E_Function
then
1341 if Is_Access_Type
(Etype
(Subp
)) then
1342 Typ
:= Designated_Type
(Etype
(Subp
));
1344 Typ
:= Etype
(Subp
);
1347 -- The following should be better commented, especially since
1348 -- we just added several new conditions here ???
1350 if Comes_From_Source
(Subp
)
1351 and then Is_Interface
(Typ
)
1352 and then not Is_Class_Wide_Type
(Typ
)
1353 and then not Is_Derived_Type
(Typ
)
1354 and then not Is_Generic_Type
(Typ
)
1355 and then not In_Instance
1357 Error_Msg_N
("??declaration of& is too late!", Subp
);
1359 ("\??spec should appear immediately after declaration of "
1360 & "& !", Subp
, Typ
);
1367 -- The subprograms build internally after the freezing point (such as
1368 -- init procs, interface thunks, type support subprograms, and Offset
1369 -- to top functions for accessing interface components in variable
1370 -- size tagged types) are not primitives.
1372 elsif Is_Frozen
(Tagged_Type
)
1373 and then not Comes_From_Source
(Subp
)
1374 and then not Has_Dispatching_Parent
1376 -- Complete decoration of internally built subprograms that override
1377 -- a dispatching primitive. These entities correspond with the
1380 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1381 -- to override functions of nonabstract null extensions. These
1382 -- primitives were added to the list of primitives of the tagged
1383 -- type by Make_Controlling_Function_Wrappers. However, attribute
1384 -- Is_Dispatching_Operation must be set to true.
1386 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1389 -- 3. Subprograms associated with stream attributes (built by
1390 -- New_Stream_Subprogram) or with the Put_Image attribute.
1392 -- 4. Wrappers built for inherited operations with inherited class-
1393 -- wide conditions, where the conditions include calls to other
1394 -- overridden primitives. The wrappers include checks on these
1395 -- modified conditions. (AI12-113).
1397 -- 5. Declarations built for subprograms without separate specs that
1398 -- are eligible for inlining in GNATprove (inside
1399 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1401 if Present
(Old_Subp
)
1402 and then Present
(Overridden_Operation
(Subp
))
1403 and then Is_Dispatching_Operation
(Old_Subp
)
1406 ((Ekind
(Subp
) = E_Function
1407 and then Is_Dispatching_Operation
(Old_Subp
)
1408 and then Is_Null_Extension
(Base_Type
(Etype
(Subp
))))
1411 (Ekind
(Subp
) = E_Procedure
1412 and then Is_Dispatching_Operation
(Old_Subp
)
1413 and then Present
(Alias
(Old_Subp
))
1414 and then Is_Null_Interface_Primitive
1415 (Ultimate_Alias
(Old_Subp
)))
1417 or else Get_TSS_Name
(Subp
) = TSS_Stream_Read
1418 or else Get_TSS_Name
(Subp
) = TSS_Stream_Write
1419 or else Get_TSS_Name
(Subp
) = TSS_Put_Image
1423 and then Present
(LSP_Subprogram
(Subp
)))
1425 or else GNATprove_Mode
);
1427 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1428 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
1429 Set_Is_Dispatching_Operation
(Subp
);
1434 -- The operation may be a child unit, whose scope is the defining
1435 -- package, but which is not a primitive operation of the type.
1437 elsif Is_Child_Unit
(Subp
) then
1440 -- If the subprogram is not defined in a package spec, the only case
1441 -- where it can be a dispatching op is when it overrides an operation
1442 -- before the freezing point of the type.
1444 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
1445 or else In_Package_Body
(Scope
(Subp
)))
1446 and then not Has_Dispatching_Parent
1448 if not Comes_From_Source
(Subp
)
1449 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
1453 -- If the type is already frozen, the overriding is not allowed
1454 -- except when Old_Subp is not a dispatching operation (which can
1455 -- occur when Old_Subp was inherited by an untagged type). However,
1456 -- a body with no previous spec freezes the type *after* its
1457 -- declaration, and therefore is a legal overriding (unless the type
1458 -- has already been frozen). Only the first such body is legal.
1460 elsif Present
(Old_Subp
)
1461 and then Is_Dispatching_Operation
(Old_Subp
)
1463 if Comes_From_Source
(Subp
)
1465 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
1466 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
1469 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1470 Decl_Item
: Node_Id
;
1473 -- ??? The checks here for whether the type has been frozen
1474 -- prior to the new body are not complete. It's not simple
1475 -- to check frozenness at this point since the body has
1476 -- already caused the type to be prematurely frozen in
1477 -- Analyze_Declarations, but we're forced to recheck this
1478 -- here because of the odd rule interpretation that allows
1479 -- the overriding if the type wasn't frozen prior to the
1480 -- body. The freezing action should probably be delayed
1481 -- until after the spec is seen, but that's a tricky
1482 -- change to the delicate freezing code.
1484 -- Look at each declaration following the type up until the
1485 -- new subprogram body. If any of the declarations is a body
1486 -- then the type has been frozen already so the overriding
1487 -- primitive is illegal.
1489 Decl_Item
:= Next
(Parent
(Tagged_Type
));
1490 while Present
(Decl_Item
)
1491 and then (Decl_Item
/= Subp_Body
)
1493 if Comes_From_Source
(Decl_Item
)
1494 and then (Nkind
(Decl_Item
) in N_Proper_Body
1495 or else Nkind
(Decl_Item
) in N_Body_Stub
)
1497 Error_Msg_N
("overriding of& is too late!", Subp
);
1499 ("\spec should appear immediately after the type!",
1507 -- If the subprogram doesn't follow in the list of
1508 -- declarations including the type then the type has
1509 -- definitely been frozen already and the body is illegal.
1511 if No
(Decl_Item
) then
1512 Error_Msg_N
("overriding of& is too late!", Subp
);
1514 ("\spec should appear immediately after the type!",
1519 -- The subprogram body declares a primitive operation.
1520 -- We must update its dispatching information here. The
1521 -- information is taken from the overridden subprogram.
1522 -- We must also generate a cross-reference entry because
1523 -- references to other primitives were already created
1524 -- when type was frozen.
1526 Body_Is_Last_Primitive
:= True;
1528 if Present
(DTC_Entity
(Old_Subp
)) then
1529 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
1530 Set_DT_Position_Value
(Subp
, DT_Position
(Old_Subp
));
1532 if not Restriction_Active
(No_Dispatching_Calls
) then
1533 if Building_Static_DT
(Tagged_Type
) then
1535 -- If the static dispatch table has not been
1536 -- built then there is nothing else to do now;
1537 -- otherwise we notify that we cannot build the
1538 -- static dispatch table.
1540 if Has_Dispatch_Table
(Tagged_Type
) then
1542 ("overriding of& is too late for building "
1543 & " static dispatch tables!", Subp
);
1545 ("\spec should appear immediately after "
1546 & "the type!", Subp
);
1549 -- No code required to register primitives in VM
1552 elsif not Tagged_Type_Expansion
then
1556 Insert_Actions_After
(Subp_Body
,
1557 Register_Primitive
(Sloc
(Subp_Body
),
1561 -- Indicate that this is an overriding operation,
1562 -- and replace the overridden entry in the list of
1563 -- primitive operations, which is used for xref
1564 -- generation subsequently.
1566 Generate_Reference
(Tagged_Type
, Subp
, 'P', False);
1567 Override_Dispatching_Operation
1568 (Tagged_Type
, Old_Subp
, Subp
);
1569 Set_Is_Dispatching_Operation
(Subp
);
1571 -- Inherit decoration of controlling formals and
1572 -- controlling result.
1574 if Ekind
(Old_Subp
) = E_Function
1575 and then Has_Controlling_Result
(Old_Subp
)
1577 Set_Has_Controlling_Result
(Subp
);
1580 if Present
(First_Formal
(Old_Subp
)) then
1582 Old_Formal
: Entity_Id
;
1586 Formal
:= First_Formal
(Subp
);
1587 Old_Formal
:= First_Formal
(Old_Subp
);
1589 while Present
(Old_Formal
) loop
1590 Set_Is_Controlling_Formal
(Formal
,
1591 Is_Controlling_Formal
(Old_Formal
));
1593 Next_Formal
(Formal
);
1594 Next_Formal
(Old_Formal
);
1600 Check_Inherited_Conditions
(Tagged_Type
,
1601 Late_Overriding
=> True);
1607 Error_Msg_N
("overriding of& is too late!", Subp
);
1609 ("\subprogram spec should appear immediately after the type!",
1613 -- If the type is not frozen yet and we are not in the overriding
1614 -- case it looks suspiciously like an attempt to define a primitive
1615 -- operation, which requires the declaration to be in a package spec
1616 -- (3.2.3(6)). Only report cases where the type and subprogram are
1617 -- in the same declaration list (by checking the enclosing parent
1618 -- declarations), to avoid spurious warnings on subprograms in
1619 -- instance bodies when the type is declared in the instance spec
1620 -- but hasn't been frozen by the instance body.
1622 elsif not Is_Frozen
(Tagged_Type
)
1623 and then In_Same_List
(Parent
(Tagged_Type
), Parent
(Parent
(Subp
)))
1626 ("??not dispatching (must be defined in a package spec)", Subp
);
1629 -- When the type is frozen, it is legitimate to define a new
1630 -- non-primitive operation.
1636 -- Now, we are sure that the scope is a package spec. If the subprogram
1637 -- is declared after the freezing point of the type that's an error
1639 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
1640 Error_Msg_N
("this primitive operation is declared too late", Subp
);
1642 ("??no primitive operations for& after this line",
1643 Freeze_Node
(Tagged_Type
),
1648 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1650 Ovr_Subp
:= Old_Subp
;
1652 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1653 -- overridden by Subp. This only applies to source subprograms, and
1654 -- their declaration must carry an explicit overriding indicator.
1657 and then Ada_Version
>= Ada_2012
1658 and then Comes_From_Source
(Subp
)
1660 Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Declaration
1662 Ovr_Subp
:= Find_Hidden_Overridden_Primitive
(Subp
);
1664 -- Verify that the proper overriding indicator has been supplied.
1666 if Present
(Ovr_Subp
)
1668 not Must_Override
(Specification
(Unit_Declaration_Node
(Subp
)))
1670 Error_Msg_NE
("missing overriding indicator for&", Subp
, Subp
);
1674 -- Now it should be a correct primitive operation, put it in the list
1676 if Present
(Ovr_Subp
) then
1678 -- If the type has interfaces we complete this check after we set
1679 -- attribute Is_Dispatching_Operation.
1681 Check_Subtype_Conformant
(Subp
, Ovr_Subp
);
1683 -- A primitive operation with the name of a primitive controlled
1684 -- operation does not override a non-visible overriding controlled
1685 -- operation, i.e. one declared in a private part when the full
1686 -- view of a type is controlled. Conversely, it will override a
1687 -- visible operation that may be declared in a partial view when
1688 -- the full view is controlled.
1690 if Chars
(Subp
) in Name_Initialize | Name_Adjust | Name_Finalize
1691 and then Is_Controlled
(Tagged_Type
)
1692 and then not Is_Visibly_Controlled
(Tagged_Type
)
1693 and then not Is_Inherited_Public_Operation
(Ovr_Subp
)
1695 Set_Overridden_Operation
(Subp
, Empty
);
1697 -- If the subprogram specification carries an overriding
1698 -- indicator, no need for the warning: it is either redundant,
1699 -- or else an error will be reported.
1701 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
1703 (Must_Override
(Parent
(Subp
))
1704 or else Must_Not_Override
(Parent
(Subp
)))
1708 -- Here we need the warning
1712 ("operation does not override inherited&??", Subp
, Subp
);
1716 Override_Dispatching_Operation
(Tagged_Type
, Ovr_Subp
, Subp
);
1718 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1719 -- that covers abstract interface subprograms we must register it
1720 -- in all the secondary dispatch tables associated with abstract
1721 -- interfaces. We do this now only if not building static tables,
1722 -- nor when the expander is inactive (we avoid trying to register
1723 -- primitives in semantics-only mode, since the type may not have
1724 -- an associated dispatch table). Otherwise the patch code is
1725 -- emitted after those tables are built, to prevent access before
1726 -- elaboration in gigi.
1728 if Body_Is_Last_Primitive
1729 and then not Building_Static_DT
(Tagged_Type
)
1730 and then Expander_Active
1731 and then Tagged_Type_Expansion
1734 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1739 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1740 while Present
(Elmt
) loop
1741 Prim
:= Node
(Elmt
);
1743 if Present
(Alias
(Prim
))
1744 and then Present
(Interface_Alias
(Prim
))
1745 and then Alias
(Prim
) = Subp
1747 Insert_Actions_After
(Subp_Body
,
1748 Register_Primitive
(Sloc
(Subp_Body
), Prim
=> Prim
));
1754 -- Redisplay the contents of the updated dispatch table
1756 if Debug_Flag_ZZ
then
1757 Write_Str
("Late overriding: ");
1758 Write_DT
(Tagged_Type
);
1764 -- If no old subprogram, then we add this as a dispatching operation,
1765 -- but we avoid doing this if an error was posted, to prevent annoying
1768 elsif not Error_Posted
(Subp
) then
1769 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
1772 Set_Is_Dispatching_Operation
(Subp
, True);
1774 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1775 -- subtype conformance against all the interfaces covered by this
1778 if Present
(Ovr_Subp
)
1779 and then Has_Interfaces
(Tagged_Type
)
1782 Ifaces_List
: Elist_Id
;
1783 Iface_Elmt
: Elmt_Id
;
1784 Iface_Prim_Elmt
: Elmt_Id
;
1785 Iface_Prim
: Entity_Id
;
1786 Ret_Typ
: Entity_Id
;
1789 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
1791 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
1792 while Present
(Iface_Elmt
) loop
1793 if not Is_Ancestor
(Node
(Iface_Elmt
), Tagged_Type
) then
1795 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
1796 while Present
(Iface_Prim_Elmt
) loop
1797 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
1799 if Is_Interface_Conformant
1800 (Tagged_Type
, Iface_Prim
, Subp
)
1802 -- Handle procedures, functions whose return type
1803 -- matches, or functions not returning interfaces
1805 if Ekind
(Subp
) = E_Procedure
1806 or else Etype
(Iface_Prim
) = Etype
(Subp
)
1807 or else not Is_Interface
(Etype
(Iface_Prim
))
1809 Check_Subtype_Conformant
1811 Old_Id
=> Iface_Prim
,
1813 Skip_Controlling_Formals
=> True);
1815 -- Handle functions returning interfaces
1817 elsif Implements_Interface
1818 (Etype
(Subp
), Etype
(Iface_Prim
))
1820 -- Temporarily force both entities to return the
1821 -- same type. Required because Subtype_Conformant
1822 -- does not handle this case.
1824 Ret_Typ
:= Etype
(Iface_Prim
);
1825 Set_Etype
(Iface_Prim
, Etype
(Subp
));
1827 Check_Subtype_Conformant
1829 Old_Id
=> Iface_Prim
,
1831 Skip_Controlling_Formals
=> True);
1833 Set_Etype
(Iface_Prim
, Ret_Typ
);
1837 Next_Elmt
(Iface_Prim_Elmt
);
1841 Next_Elmt
(Iface_Elmt
);
1846 if not Body_Is_Last_Primitive
then
1847 Set_DT_Position_Value
(Subp
, No_Uint
);
1849 elsif Has_Controlled_Component
(Tagged_Type
)
1850 and then Chars
(Subp
) in Name_Initialize
1853 | Name_Finalize_Address
1856 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
1860 Old_Spec
: Entity_Id
;
1862 C_Names
: constant array (1 .. 4) of Name_Id
:=
1866 Name_Finalize_Address
);
1868 D_Names
: constant array (1 .. 4) of TSS_Name_Type
:=
1869 (TSS_Deep_Initialize
,
1872 TSS_Finalize_Address
);
1875 -- Remove previous controlled function which was constructed and
1876 -- analyzed when the type was frozen. This requires removing the
1877 -- body of the redefined primitive, as well as its specification
1878 -- if needed (there is no spec created for Deep_Initialize, see
1879 -- exp_ch3.adb). We must also dismantle the exception information
1880 -- that may have been generated for it when front end zero-cost
1881 -- tables are enabled.
1883 for J
in D_Names
'Range loop
1884 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
1887 and then Chars
(Subp
) = C_Names
(J
)
1889 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
1891 Set_Is_Eliminated
(Old_P
);
1892 Set_Scope
(Old_P
, Scope
(Current_Scope
));
1894 if Nkind
(Old_Bod
) = N_Subprogram_Body
1895 and then Present
(Corresponding_Spec
(Old_Bod
))
1897 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
1898 Set_Has_Completion
(Old_Spec
, False);
1903 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
1905 -- The new operation is added to the actions of the freeze node
1906 -- for the type, but this node has already been analyzed, so we
1907 -- must retrieve and analyze explicitly the new body.
1910 and then Present
(Actions
(F_Node
))
1912 Decl
:= Last
(Actions
(F_Node
));
1918 -- AI12-0279: If the Yield aspect is specified for a dispatching
1919 -- subprogram that inherits the aspect, the specified value shall
1922 if Is_Dispatching_Operation
(Subp
)
1923 and then Is_Primitive_Wrapper
(Subp
)
1924 and then Present
(Wrapped_Entity
(Subp
))
1925 and then Comes_From_Source
(Wrapped_Entity
(Subp
))
1926 and then Present
(Overridden_Operation
(Subp
))
1927 and then Has_Yield_Aspect
(Overridden_Operation
(Subp
))
1928 /= Has_Yield_Aspect
(Wrapped_Entity
(Subp
))
1931 W_Ent
: constant Entity_Id
:= Wrapped_Entity
(Subp
);
1932 W_Decl
: constant Node_Id
:= Parent
(W_Ent
);
1936 if Present
(Aspect_Specifications
(W_Decl
)) then
1937 Asp
:= First
(Aspect_Specifications
(W_Decl
));
1938 while Present
(Asp
) loop
1939 if Chars
(Identifier
(Asp
)) = Name_Yield
then
1940 Error_Msg_Name_1
:= Name_Yield
;
1942 ("specification of inherited aspect% can only confirm "
1943 & "parent value", Asp
);
1950 Set_Has_Yield_Aspect
(Wrapped_Entity
(Subp
));
1954 -- For similarity with record extensions, in Ada 9X the language should
1955 -- have disallowed adding visible operations to a tagged type after
1956 -- deriving a private extension from it. Report a warning if this
1957 -- primitive is defined after a private extension of Tagged_Type.
1959 Warn_On_Late_Primitive_After_Private_Extension
(Tagged_Type
, Subp
);
1960 end Check_Dispatching_Operation
;
1962 ------------------------------------------
1963 -- Check_Operation_From_Incomplete_Type --
1964 ------------------------------------------
1966 procedure Check_Operation_From_Incomplete_Type
1970 Full
: constant Entity_Id
:= Full_View
(Typ
);
1971 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1972 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1973 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1975 Prev
: Elmt_Id
:= No_Elmt
;
1977 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean;
1978 -- Check that Subp has profile of an operation derived from Parent_Subp.
1979 -- Subp must have a parameter or result type that is Typ or an access
1980 -- parameter or access result type that designates Typ.
1986 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean is
1990 if Chars
(Parent_Subp
) /= Chars
(Subp
) then
1994 -- Check that the type of controlling formals is derived from the
1995 -- parent subprogram's controlling formal type (or designated type
1996 -- if the formal type is an anonymous access type).
1998 F1
:= First_Formal
(Parent_Subp
);
1999 F2
:= First_Formal
(Subp
);
2000 while Present
(F1
) and then Present
(F2
) loop
2001 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
2002 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
2004 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
2005 and then Designated_Type
(Etype
(F2
)) /= Full
2010 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
2013 elsif Etype
(F1
) = Parent_Typ
and then Etype
(F2
) /= Full
then
2021 -- Check that a controlling result type is derived from the parent
2022 -- subprogram's result type (or designated type if the result type
2023 -- is an anonymous access type).
2025 if Ekind
(Parent_Subp
) = E_Function
then
2026 if Ekind
(Subp
) /= E_Function
then
2029 elsif Ekind
(Etype
(Parent_Subp
)) = E_Anonymous_Access_Type
then
2030 if Ekind
(Etype
(Subp
)) /= E_Anonymous_Access_Type
then
2033 elsif Designated_Type
(Etype
(Parent_Subp
)) = Parent_Typ
2034 and then Designated_Type
(Etype
(Subp
)) /= Full
2039 elsif Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
then
2042 elsif Etype
(Parent_Subp
) = Parent_Typ
2043 and then Etype
(Subp
) /= Full
2048 elsif Ekind
(Subp
) = E_Function
then
2052 return No
(F1
) and then No
(F2
);
2055 -- Start of processing for Check_Operation_From_Incomplete_Type
2058 -- The operation may override an inherited one, or may be a new one
2059 -- altogether. The inherited operation will have been hidden by the
2060 -- current one at the point of the type derivation, so it does not
2061 -- appear in the list of primitive operations of the type. We have to
2062 -- find the proper place of insertion in the list of primitive opera-
2063 -- tions by iterating over the list for the parent type.
2065 Op1
:= First_Elmt
(Old_Prim
);
2066 Op2
:= First_Elmt
(New_Prim
);
2067 while Present
(Op1
) and then Present
(Op2
) loop
2068 if Derives_From
(Node
(Op1
)) then
2071 -- Avoid adding it to the list of primitives if already there
2073 if Node
(Op2
) /= Subp
then
2074 Prepend_Elmt
(Subp
, New_Prim
);
2078 Insert_Elmt_After
(Subp
, Prev
);
2089 -- Operation is a new primitive
2091 Append_Elmt
(Subp
, New_Prim
);
2092 end Check_Operation_From_Incomplete_Type
;
2094 ---------------------------------------
2095 -- Check_Operation_From_Private_View --
2096 ---------------------------------------
2098 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
2099 Tagged_Type
: Entity_Id
;
2102 if Is_Dispatching_Operation
(Alias
(Subp
)) then
2103 Set_Scope
(Subp
, Current_Scope
);
2104 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
2106 -- Add Old_Subp to primitive operations if not already present
2108 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
2109 Add_Dispatching_Operation
(Tagged_Type
, Old_Subp
);
2111 -- If Old_Subp isn't already marked as dispatching then this is
2112 -- the case of an operation of an untagged private type fulfilled
2113 -- by a tagged type that overrides an inherited dispatching
2114 -- operation, so we set the necessary dispatching attributes here.
2116 if not Is_Dispatching_Operation
(Old_Subp
) then
2118 -- If the untagged type has no discriminants, and the full
2119 -- view is constrained, there will be a spurious mismatch of
2120 -- subtypes on the controlling arguments, because the tagged
2121 -- type is the internal base type introduced in the derivation.
2122 -- Use the original type to verify conformance, rather than the
2125 if not Comes_From_Source
(Tagged_Type
)
2126 and then Has_Discriminants
(Tagged_Type
)
2132 Formal
:= First_Formal
(Old_Subp
);
2133 while Present
(Formal
) loop
2134 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
2135 Tagged_Type
:= Etype
(Formal
);
2138 Next_Formal
(Formal
);
2142 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
2143 Tagged_Type
:= Etype
(Old_Subp
);
2147 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
2148 Set_Is_Dispatching_Operation
(Old_Subp
, True);
2149 Set_DT_Position_Value
(Old_Subp
, No_Uint
);
2152 -- If the old subprogram is an explicit renaming of some other
2153 -- entity, it is not overridden by the inherited subprogram.
2154 -- Otherwise, update its alias and other attributes.
2156 if Present
(Alias
(Old_Subp
))
2157 and then Nkind
(Unit_Declaration_Node
(Old_Subp
)) /=
2158 N_Subprogram_Renaming_Declaration
2160 Set_Alias
(Old_Subp
, Alias
(Subp
));
2162 -- The derived subprogram should inherit the abstractness of
2163 -- the parent subprogram (except in the case of a function
2164 -- returning the type). This sets the abstractness properly
2165 -- for cases where a private extension may have inherited an
2166 -- abstract operation, but the full type is derived from a
2167 -- descendant type and inherits a nonabstract version.
2169 if Etype
(Subp
) /= Tagged_Type
then
2170 Set_Is_Abstract_Subprogram
2171 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
2176 end Check_Operation_From_Private_View
;
2178 --------------------------
2179 -- Find_Controlling_Arg --
2180 --------------------------
2182 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
2183 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
2187 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
2188 return Find_Controlling_Arg
(Expression
(Orig_Node
));
2191 -- Dispatching on result case. If expansion is disabled, the node still
2192 -- has the structure of a function call. However, if the function name
2193 -- is an operator and the call was given in infix form, the original
2194 -- node has no controlling result and we must examine the current node.
2196 if Nkind
(N
) = N_Function_Call
2197 and then Present
(Controlling_Argument
(N
))
2198 and then Has_Controlling_Result
(Entity
(Name
(N
)))
2200 return Controlling_Argument
(N
);
2202 -- If expansion is enabled, the call may have been transformed into
2203 -- an indirect call, and we need to recover the original node.
2205 elsif Nkind
(Orig_Node
) = N_Function_Call
2206 and then Present
(Controlling_Argument
(Orig_Node
))
2207 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
2209 return Controlling_Argument
(Orig_Node
);
2211 -- Type conversions are dynamically tagged if the target type, or its
2212 -- designated type, are classwide. An interface conversion expands into
2213 -- a dereference, so test must be performed on the original node.
2215 elsif Nkind
(Orig_Node
) = N_Type_Conversion
2216 and then Nkind
(N
) = N_Explicit_Dereference
2217 and then Is_Controlling_Actual
(N
)
2220 Target_Type
: constant Entity_Id
:=
2221 Entity
(Subtype_Mark
(Orig_Node
));
2224 if Is_Class_Wide_Type
(Target_Type
) then
2227 elsif Is_Access_Type
(Target_Type
)
2228 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
2239 elsif Is_Controlling_Actual
(N
)
2241 (Nkind
(Parent
(N
)) = N_Qualified_Expression
2242 and then Is_Controlling_Actual
(Parent
(N
)))
2246 if Is_Access_Type
(Typ
) then
2248 -- In the case of an Access attribute, use the type of the prefix,
2249 -- since in the case of an actual for an access parameter, the
2250 -- attribute's type may be of a specific designated type, even
2251 -- though the prefix type is class-wide.
2253 if Nkind
(N
) = N_Attribute_Reference
then
2254 Typ
:= Etype
(Prefix
(N
));
2256 -- An allocator is dispatching if the type of qualified expression
2257 -- is class_wide, in which case this is the controlling type.
2259 elsif Nkind
(Orig_Node
) = N_Allocator
2260 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
2262 Typ
:= Etype
(Expression
(Orig_Node
));
2264 Typ
:= Designated_Type
(Typ
);
2268 if Is_Class_Wide_Type
(Typ
)
2270 (Nkind
(Parent
(N
)) = N_Qualified_Expression
2271 and then Is_Access_Type
(Etype
(N
))
2272 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
2279 end Find_Controlling_Arg
;
2281 ---------------------------
2282 -- Find_Dispatching_Type --
2283 ---------------------------
2285 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
2286 A_Formal
: Entity_Id
;
2288 Ctrl_Type
: Entity_Id
;
2291 if Ekind
(Subp
) in E_Function | E_Procedure
2292 and then Present
(DTC_Entity
(Subp
))
2294 return Scope
(DTC_Entity
(Subp
));
2296 -- For subprograms internally generated by derivations of tagged types
2297 -- use the alias subprogram as a reference to locate the dispatching
2300 elsif not Comes_From_Source
(Subp
)
2301 and then Present
(Alias
(Subp
))
2302 and then Is_Dispatching_Operation
(Alias
(Subp
))
2304 if Ekind
(Alias
(Subp
)) = E_Function
2305 and then Has_Controlling_Result
(Alias
(Subp
))
2307 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
2310 Formal
:= First_Formal
(Subp
);
2311 A_Formal
:= First_Formal
(Alias
(Subp
));
2312 while Present
(A_Formal
) loop
2313 if Is_Controlling_Formal
(A_Formal
) then
2314 return Check_Controlling_Type
(Etype
(Formal
), Subp
);
2317 Next_Formal
(Formal
);
2318 Next_Formal
(A_Formal
);
2321 pragma Assert
(False);
2328 Formal
:= First_Formal
(Subp
);
2329 while Present
(Formal
) loop
2330 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
2332 if Present
(Ctrl_Type
) then
2336 Next_Formal
(Formal
);
2339 -- The subprogram may also be dispatching on result
2341 if Present
(Etype
(Subp
)) then
2342 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
2346 pragma Assert
(not Is_Dispatching_Operation
(Subp
));
2348 end Find_Dispatching_Type
;
2350 --------------------------------------
2351 -- Find_Hidden_Overridden_Primitive --
2352 --------------------------------------
2354 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
2356 Tag_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(S
);
2358 Orig_Prim
: Entity_Id
;
2360 Vis_List
: Elist_Id
;
2363 -- This Ada 2012 rule applies only for type extensions or private
2364 -- extensions, where the parent type is not in a parent unit, and
2365 -- where an operation is never declared but still inherited.
2368 or else not Is_Record_Type
(Tag_Typ
)
2369 or else Etype
(Tag_Typ
) = Tag_Typ
2370 or else In_Open_Scopes
(Scope
(Etype
(Tag_Typ
)))
2375 -- Collect the list of visible ancestor of the tagged type
2377 Vis_List
:= Visible_Ancestors
(Tag_Typ
);
2379 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
2380 while Present
(Elmt
) loop
2381 Prim
:= Node
(Elmt
);
2383 -- Find an inherited hidden dispatching primitive with the name of S
2384 -- and a type-conformant profile.
2386 if Present
(Alias
(Prim
))
2387 and then Is_Hidden
(Alias
(Prim
))
2388 and then Find_Dispatching_Type
(Alias
(Prim
)) /= Tag_Typ
2389 and then Primitive_Names_Match
(S
, Prim
)
2390 and then Type_Conformant
(S
, Prim
)
2393 Vis_Ancestor
: Elmt_Id
;
2397 -- The original corresponding operation of Prim must be an
2398 -- operation of a visible ancestor of the dispatching type S,
2399 -- and the original corresponding operation of S2 must be
2402 Orig_Prim
:= Original_Corresponding_Operation
(Prim
);
2404 if Orig_Prim
/= Prim
2405 and then Is_Immediately_Visible
(Orig_Prim
)
2407 Vis_Ancestor
:= First_Elmt
(Vis_List
);
2408 while Present
(Vis_Ancestor
) loop
2410 First_Elmt
(Primitive_Operations
(Node
(Vis_Ancestor
)));
2411 while Present
(Elmt
) loop
2412 if Node
(Elmt
) = Orig_Prim
then
2413 Set_Overridden_Operation
(S
, Prim
);
2414 Set_Is_Ada_2022_Only
(S
,
2415 Is_Ada_2022_Only
(Prim
));
2416 Set_Alias
(Prim
, Orig_Prim
);
2423 Next_Elmt
(Vis_Ancestor
);
2433 end Find_Hidden_Overridden_Primitive
;
2435 ---------------------------------------
2436 -- Find_Primitive_Covering_Interface --
2437 ---------------------------------------
2439 function Find_Primitive_Covering_Interface
2440 (Tagged_Type
: Entity_Id
;
2441 Iface_Prim
: Entity_Id
) return Entity_Id
2447 pragma Assert
(Is_Interface
(Find_Dispatching_Type
(Iface_Prim
))
2448 or else (Present
(Alias
(Iface_Prim
))
2451 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
2453 -- Search in the homonym chain. Done to speed up locating visible
2454 -- entities and required to catch primitives associated with the partial
2455 -- view of private types when processing the corresponding full view.
2457 E
:= Current_Entity
(Iface_Prim
);
2458 while Present
(E
) loop
2459 if Is_Subprogram
(E
)
2460 and then Is_Dispatching_Operation
(E
)
2461 and then Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
)
2469 -- Search in the list of primitives of the type. Required to locate
2470 -- the covering primitive if the covering primitive is not visible
2471 -- (for example, non-visible inherited primitive of private type).
2473 El
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2474 while Present
(El
) loop
2477 -- Keep separate the management of internal entities that link
2478 -- primitives with interface primitives from tagged type primitives.
2480 if No
(Interface_Alias
(E
)) then
2481 if Present
(Alias
(E
)) then
2483 -- This interface primitive has not been covered yet
2485 if Alias
(E
) = Iface_Prim
then
2488 -- The covering primitive was inherited
2490 elsif Overridden_Operation
(Ultimate_Alias
(E
))
2497 -- Check if E covers the interface primitive (includes case in
2498 -- which E is an inherited private primitive).
2500 if Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
) then
2504 -- Use the internal entity that links the interface primitive with
2505 -- the covering primitive to locate the entity.
2507 elsif Interface_Alias
(E
) = Iface_Prim
then
2517 end Find_Primitive_Covering_Interface
;
2519 ---------------------------
2520 -- Inheritance_Utilities --
2521 ---------------------------
2523 package body Inheritance_Utilities
is
2525 ---------------------------
2526 -- Inherited_Subprograms --
2527 ---------------------------
2529 function Inherited_Subprograms
2531 No_Interfaces
: Boolean := False;
2532 Interfaces_Only
: Boolean := False;
2533 One_Only
: Boolean := False) return Subprogram_List
2535 Result
: Subprogram_List
(1 .. 6000);
2536 -- 6000 here is intended to be infinity. We could use an expandable
2537 -- table, but it would be awfully heavy, and there is no way that we
2538 -- could reasonably exceed this value.
2541 -- Number of entries in Result
2543 Parent_Op
: Entity_Id
;
2544 -- Traverses the Overridden_Operation chain
2546 procedure Store_IS
(E
: Entity_Id
);
2547 -- Stores E in Result if not already stored
2553 procedure Store_IS
(E
: Entity_Id
) is
2555 for J
in 1 .. N
loop
2556 if E
= Result
(J
) then
2565 -- Start of processing for Inherited_Subprograms
2568 pragma Assert
(not (No_Interfaces
and Interfaces_Only
));
2570 -- When used from backends, visibility can be handled differently
2571 -- resulting in no dispatching type being found.
2574 and then Is_Dispatching_Operation
(S
)
2575 and then Present
(Find_DT
(S
))
2577 -- Deal with direct inheritance
2579 if not Interfaces_Only
then
2582 Parent_Op
:= Overridden_Operation
(Parent_Op
);
2583 exit when No
(Parent_Op
)
2584 or else (No_Interfaces
2585 and then Is_Interface
(Find_DT
(Parent_Op
)));
2587 if Is_Subprogram_Or_Generic_Subprogram
(Parent_Op
) then
2588 Store_IS
(Parent_Op
);
2597 -- Now deal with interfaces
2599 if not No_Interfaces
then
2601 Tag_Typ
: Entity_Id
;
2606 Tag_Typ
:= Find_DT
(S
);
2608 -- In the presence of limited views there may be no visible
2609 -- dispatching type. Primitives will be inherited when non-
2610 -- limited view is frozen.
2612 if No
(Tag_Typ
) then
2613 return Result
(1 .. 0);
2615 -- Prevent cascaded errors
2617 elsif Is_Concurrent_Type
(Tag_Typ
)
2618 and then No
(Corresponding_Record_Type
(Tag_Typ
))
2619 and then Serious_Errors_Detected
> 0
2621 return Result
(1 .. 0);
2624 if Is_Concurrent_Type
(Tag_Typ
) then
2625 Tag_Typ
:= Corresponding_Record_Type
(Tag_Typ
);
2628 if Present
(Tag_Typ
)
2629 and then Is_Private_Type
(Tag_Typ
)
2630 and then Present
(Full_View
(Tag_Typ
))
2632 Tag_Typ
:= Full_View
(Tag_Typ
);
2635 -- Search primitive operations of dispatching type
2637 if Present
(Tag_Typ
)
2638 and then Present
(Primitive_Operations
(Tag_Typ
))
2640 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
2641 while Present
(Elmt
) loop
2642 Prim
:= Node
(Elmt
);
2644 -- The following test eliminates some odd cases in
2645 -- which Ekind (Prim) is Void, to be investigated
2648 if not Is_Subprogram_Or_Generic_Subprogram
(Prim
) then
2651 -- For [generic] subprogram, look at interface
2654 elsif Present
(Interface_Alias
(Prim
))
2655 and then Alias
(Prim
) = S
2657 -- We have found a primitive covered by S
2659 Store_IS
(Interface_Alias
(Prim
));
2675 return Result
(1 .. N
);
2676 end Inherited_Subprograms
;
2678 ------------------------------
2679 -- Is_Overriding_Subprogram --
2680 ------------------------------
2682 function Is_Overriding_Subprogram
(E
: Entity_Id
) return Boolean is
2683 Inherited
: constant Subprogram_List
:=
2684 Inherited_Subprograms
(E
, One_Only
=> True);
2686 return Inherited
'Length > 0;
2687 end Is_Overriding_Subprogram
;
2688 end Inheritance_Utilities
;
2690 --------------------------------
2691 -- Inheritance_Utilities_Inst --
2692 --------------------------------
2694 package Inheritance_Utilities_Inst
is new
2695 Inheritance_Utilities
(Find_Dispatching_Type
);
2697 ---------------------------
2698 -- Inherited_Subprograms --
2699 ---------------------------
2701 function Inherited_Subprograms
2703 No_Interfaces
: Boolean := False;
2704 Interfaces_Only
: Boolean := False;
2705 One_Only
: Boolean := False) return Subprogram_List
renames
2706 Inheritance_Utilities_Inst
.Inherited_Subprograms
;
2708 ---------------------------
2709 -- Is_Dynamically_Tagged --
2710 ---------------------------
2712 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
2714 if Nkind
(N
) = N_Error
then
2717 elsif Present
(Find_Controlling_Arg
(N
)) then
2720 -- Special cases: entities, and calls that dispatch on result
2722 elsif Is_Entity_Name
(N
) then
2723 return Is_Class_Wide_Type
(Etype
(N
));
2725 elsif Nkind
(N
) = N_Function_Call
2726 and then Is_Class_Wide_Type
(Etype
(N
))
2730 -- Otherwise check whether call has controlling argument
2735 end Is_Dynamically_Tagged
;
2737 ---------------------------------
2738 -- Is_Null_Interface_Primitive --
2739 ---------------------------------
2741 function Is_Null_Interface_Primitive
(E
: Entity_Id
) return Boolean is
2743 return Comes_From_Source
(E
)
2744 and then Is_Dispatching_Operation
(E
)
2745 and then Ekind
(E
) = E_Procedure
2746 and then Null_Present
(Parent
(E
))
2747 and then Is_Interface
(Find_Dispatching_Type
(E
));
2748 end Is_Null_Interface_Primitive
;
2750 -----------------------------------
2751 -- Is_Inherited_Public_Operation --
2752 -----------------------------------
2754 function Is_Inherited_Public_Operation
(Op
: Entity_Id
) return Boolean is
2755 Pack_Decl
: Node_Id
;
2756 Prim
: Entity_Id
:= Op
;
2757 Scop
: Entity_Id
:= Prim
;
2760 -- Locate the ultimate non-hidden alias entity
2762 while Present
(Alias
(Prim
)) and then not Is_Hidden
(Alias
(Prim
)) loop
2763 pragma Assert
(Alias
(Prim
) /= Prim
);
2764 Prim
:= Alias
(Prim
);
2765 Scop
:= Scope
(Prim
);
2768 if Comes_From_Source
(Prim
) and then Ekind
(Scop
) = E_Package
then
2769 Pack_Decl
:= Unit_Declaration_Node
(Scop
);
2772 Nkind
(Pack_Decl
) = N_Package_Declaration
2773 and then List_Containing
(Unit_Declaration_Node
(Prim
)) =
2774 Visible_Declarations
(Specification
(Pack_Decl
));
2779 end Is_Inherited_Public_Operation
;
2781 ------------------------------
2782 -- Is_Overriding_Subprogram --
2783 ------------------------------
2785 function Is_Overriding_Subprogram
(E
: Entity_Id
) return Boolean renames
2786 Inheritance_Utilities_Inst
.Is_Overriding_Subprogram
;
2788 --------------------------
2789 -- Is_Tag_Indeterminate --
2790 --------------------------
2792 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
2795 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
2798 if Nkind
(Orig_Node
) = N_Function_Call
2799 and then Is_Entity_Name
(Name
(Orig_Node
))
2801 Nam
:= Entity
(Name
(Orig_Node
));
2803 if not Has_Controlling_Result
(Nam
) then
2806 -- The function may have a controlling result, but if the return type
2807 -- is not visibly tagged, then this is not tag-indeterminate.
2809 elsif Is_Access_Type
(Etype
(Nam
))
2810 and then not Is_Tagged_Type
(Designated_Type
(Etype
(Nam
)))
2814 -- An explicit dereference means that the call has already been
2815 -- expanded and there is no tag to propagate.
2817 elsif Nkind
(N
) = N_Explicit_Dereference
then
2820 -- If there are no actuals, the call is tag-indeterminate
2822 elsif No
(Parameter_Associations
(Orig_Node
)) then
2826 Actual
:= First_Actual
(Orig_Node
);
2827 while Present
(Actual
) loop
2828 if Is_Controlling_Actual
(Actual
)
2829 and then not Is_Tag_Indeterminate
(Actual
)
2831 -- One operand is dispatching
2836 Next_Actual
(Actual
);
2842 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
2843 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
2845 -- Case of a call to the Input attribute (possibly rewritten), which is
2846 -- always tag-indeterminate except when its prefix is a Class attribute.
2848 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
2850 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
2851 and then Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
2855 -- In Ada 2005, a function that returns an anonymous access type can be
2856 -- dispatching, and the dereference of a call to such a function can
2857 -- also be tag-indeterminate if the call itself is.
2859 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
2860 and then Ada_Version
>= Ada_2005
2862 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
2867 end Is_Tag_Indeterminate
;
2869 ------------------------------------
2870 -- Override_Dispatching_Operation --
2871 ------------------------------------
2873 procedure Override_Dispatching_Operation
2874 (Tagged_Type
: Entity_Id
;
2875 Prev_Op
: Entity_Id
;
2882 -- If there is no previous operation to override, the type declaration
2883 -- was malformed, and an error must have been emitted already.
2885 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2886 while Present
(Elmt
) and then Node
(Elmt
) /= Prev_Op
loop
2894 -- The location of entities that come from source in the list of
2895 -- primitives of the tagged type must follow their order of occurrence
2896 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2897 -- primitive of an interface that is not implemented by the parents of
2898 -- this tagged type (that is, it is an alias of an interface primitive
2899 -- generated by Derive_Interface_Progenitors), then we must append the
2900 -- new entity at the end of the list of primitives.
2902 if Present
(Alias
(Prev_Op
))
2903 and then Etype
(Tagged_Type
) /= Tagged_Type
2904 and then Is_Interface
(Find_Dispatching_Type
(Alias
(Prev_Op
)))
2905 and then not Is_Ancestor
(Find_Dispatching_Type
(Alias
(Prev_Op
)),
2906 Tagged_Type
, Use_Full_View
=> True)
2907 and then not Implements_Interface
2908 (Etype
(Tagged_Type
),
2909 Find_Dispatching_Type
(Alias
(Prev_Op
)))
2911 Remove_Elmt
(Primitive_Operations
(Tagged_Type
), Elmt
);
2912 Add_Dispatching_Operation
(Tagged_Type
, New_Op
);
2914 -- The new primitive replaces the overridden entity. Required to ensure
2915 -- that overriding primitive is assigned the same dispatch table slot.
2918 Replace_Elmt
(Elmt
, New_Op
);
2921 if Ada_Version
>= Ada_2005
and then Has_Interfaces
(Tagged_Type
) then
2923 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2924 -- entities of the overridden primitive to reference New_Op, and
2925 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2926 -- that the new operation is subtype conformant with the interface
2927 -- operations that it implements (for operations inherited from the
2928 -- parent itself, this check is made when building the derived type).
2930 -- Note: This code is executed with internally generated wrappers of
2931 -- functions with controlling result and late overridings.
2933 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2934 while Present
(Elmt
) loop
2935 Prim
:= Node
(Elmt
);
2937 if Prim
= New_Op
then
2940 -- Note: The check on Is_Subprogram protects the frontend against
2941 -- reading attributes in entities that are not yet fully decorated
2943 elsif Is_Subprogram
(Prim
)
2944 and then Present
(Interface_Alias
(Prim
))
2945 and then Alias
(Prim
) = Prev_Op
2947 Set_Alias
(Prim
, New_Op
);
2949 -- No further decoration needed yet for internally generated
2950 -- wrappers of controlling functions since (at this stage)
2951 -- they are not yet decorated.
2953 if not Is_Wrapper
(New_Op
) then
2954 Check_Subtype_Conformant
(New_Op
, Prim
);
2956 Set_Is_Abstract_Subprogram
(Prim
,
2957 Is_Abstract_Subprogram
(New_Op
));
2959 -- Ensure that this entity will be expanded to fill the
2960 -- corresponding entry in its dispatch table.
2962 if not Is_Abstract_Subprogram
(Prim
) then
2963 Set_Has_Delayed_Freeze
(Prim
);
2972 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
2973 or else not In_Private_Part
(Current_Scope
)
2975 -- Not a private primitive
2979 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
2981 -- Make the overriding operation into an alias of the implicit one.
2982 -- In this fashion a call from outside ends up calling the new body
2983 -- even if non-dispatching, and a call from inside calls the over-
2984 -- riding operation because it hides the implicit one. To indicate
2985 -- that the body of Prev_Op is never called, set its dispatch table
2986 -- entity to Empty. If the overridden operation has a dispatching
2987 -- result, so does the overriding one.
2989 Set_Alias
(Prev_Op
, New_Op
);
2990 Set_DTC_Entity
(Prev_Op
, Empty
);
2991 Set_Has_Controlling_Result
(New_Op
, Has_Controlling_Result
(Prev_Op
));
2992 Set_Is_Ada_2022_Only
(New_Op
, Is_Ada_2022_Only
(Prev_Op
));
2994 end Override_Dispatching_Operation
;
3000 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
3001 Call_Node
: Node_Id
;
3005 if Nkind
(Actual
) = N_Function_Call
then
3006 Call_Node
:= Actual
;
3008 elsif Nkind
(Actual
) = N_Identifier
3009 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
3011 -- Call rewritten as object declaration when stack-checking is
3012 -- enabled. Propagate tag to expression in declaration, which is
3015 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
3017 -- Ada 2005: If this is a dereference of a call to a function with a
3018 -- dispatching access-result, the tag is propagated when the dereference
3019 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
3021 elsif Nkind
(Actual
) = N_Explicit_Dereference
3022 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
3026 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
3027 -- and in that case we can simply return.
3029 elsif Nkind
(Actual
) = N_Attribute_Reference
then
3030 pragma Assert
(Attribute_Name
(Actual
) = Name_Input
);
3034 -- Only other possibilities are parenthesized or qualified expression,
3035 -- or an expander-generated unchecked conversion of a function call to
3036 -- a stream Input attribute.
3039 Call_Node
:= Expression
(Actual
);
3042 -- No action needed if the call has been already expanded
3044 if Is_Expanded_Dispatching_Call
(Call_Node
) then
3048 -- Do not set the Controlling_Argument if already set. This happens in
3049 -- the special case of _Input (see Exp_Attr, case Input).
3051 if No
(Controlling_Argument
(Call_Node
)) then
3052 Set_Controlling_Argument
(Call_Node
, Control
);
3055 Arg
:= First_Actual
(Call_Node
);
3056 while Present
(Arg
) loop
3057 if Is_Tag_Indeterminate
(Arg
) then
3058 Propagate_Tag
(Control
, Arg
);
3064 -- Add class-wide precondition check if the target of this dispatching
3065 -- call has or inherits class-wide preconditions.
3067 Install_Class_Preconditions_Check
(Call_Node
);
3069 -- Expansion of dispatching calls is suppressed on VM targets, because
3070 -- the VM back-ends directly handle the generation of dispatching calls
3071 -- and would have to undo any expansion to an indirect call.
3073 if Tagged_Type_Expansion
then
3075 Call_Typ
: Entity_Id
:= Etype
(Call_Node
);
3076 Ctrl_Typ
: Entity_Id
:= Etype
(Control
);
3079 Expand_Dispatching_Call
(Call_Node
);
3081 if Is_Class_Wide_Type
(Call_Typ
) then
3082 Call_Typ
:= Root_Type
(Call_Typ
);
3085 if Is_Class_Wide_Type
(Ctrl_Typ
) then
3086 Ctrl_Typ
:= Root_Type
(Ctrl_Typ
);
3089 -- If the controlling argument is an interface type and the type
3090 -- of Call_Node differs then we must add an implicit conversion to
3091 -- force displacement of the pointer to the object to reference
3092 -- the secondary dispatch table of the interface.
3094 if Is_Interface
(Ctrl_Typ
)
3095 and then Ctrl_Typ
/= Call_Typ
3097 -- Cannot use Convert_To because the previous call to
3098 -- Expand_Dispatching_Call leaves decorated the Call_Node
3099 -- with the type of Control.
3102 Make_Type_Conversion
(Sloc
(Call_Node
),
3104 New_Occurrence_Of
(Etype
(Control
), Sloc
(Call_Node
)),
3105 Expression
=> Relocate_Node
(Call_Node
)));
3106 Set_Etype
(Call_Node
, Etype
(Control
));
3107 Set_Analyzed
(Call_Node
);
3109 Expand_Interface_Conversion
(Call_Node
);
3113 -- Expansion of a dispatching call results in an indirect call, which in
3114 -- turn causes current values to be killed (see Resolve_Call), so on VM
3115 -- targets we do the call here to ensure consistent warnings between VM
3116 -- and non-VM targets.
3119 Kill_Current_Values
;