1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2022, 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 Formal
:= First_Formal
(Subp
);
300 while Present
(Formal
) loop
301 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
303 if Present
(Ctrl_Type
) then
305 -- Obtain the full type in case we are looking at an incomplete
308 if Ekind
(Ctrl_Type
) = E_Incomplete_Type
309 and then Present
(Full_View
(Ctrl_Type
))
311 Ctrl_Type
:= Full_View
(Ctrl_Type
);
314 -- When controlling type is concurrent and declared within a
315 -- generic or inside an instance use corresponding record type.
317 if Is_Concurrent_Type
(Ctrl_Type
)
318 and then Present
(Corresponding_Record_Type
(Ctrl_Type
))
320 Ctrl_Type
:= Corresponding_Record_Type
(Ctrl_Type
);
323 if Ctrl_Type
= Typ
then
324 Set_Is_Controlling_Formal
(Formal
);
326 -- Ada 2005 (AI-231): Anonymous access types that are used in
327 -- controlling parameters exclude null because it is necessary
328 -- to read the tag to dispatch, and null has no tag.
330 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
331 Set_Can_Never_Be_Null
(Etype
(Formal
));
332 Set_Is_Known_Non_Null
(Etype
(Formal
));
335 -- Check that the parameter's nominal subtype statically
336 -- matches the first subtype.
338 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
339 if not Subtypes_Statically_Match
340 (Typ
, Designated_Type
(Etype
(Formal
)))
343 ("parameter subtype does not match controlling type",
347 -- Within a predicate function, the formal may be a subtype
348 -- of a tagged type, given that the predicate is expressed
349 -- in terms of the subtype.
351 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
))
352 and then not Is_Predicate_Function
(Subp
)
355 ("parameter subtype does not match controlling type",
359 if Present
(Default_Value
(Formal
)) then
361 -- In Ada 2005, access parameters can have defaults
363 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
364 and then Ada_Version
< Ada_2005
367 ("default not allowed for controlling access parameter",
368 Default_Value
(Formal
));
370 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
372 ("default expression must be a tag indeterminate" &
373 " function call", Default_Value
(Formal
));
377 elsif Comes_From_Source
(Subp
) then
379 ("operation can be dispatching in only one type", Subp
);
383 Next_Formal
(Formal
);
386 if Ekind
(Subp
) in E_Function | E_Generic_Function
then
387 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
389 if Present
(Ctrl_Type
) then
390 if Ctrl_Type
= Typ
then
391 Set_Has_Controlling_Result
(Subp
);
393 -- Check that result subtype statically matches first subtype
394 -- (Ada 2005): Subp may have a controlling access result.
396 if Subtypes_Statically_Match
(Typ
, Etype
(Subp
))
397 or else (Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
399 Subtypes_Statically_Match
400 (Typ
, Designated_Type
(Etype
(Subp
))))
406 ("result subtype does not match controlling type", Subp
);
409 elsif Comes_From_Source
(Subp
) then
411 ("operation can be dispatching in only one type", Subp
);
415 end Check_Controlling_Formals
;
417 ----------------------------
418 -- Check_Controlling_Type --
419 ----------------------------
421 function Check_Controlling_Type
423 Subp
: Entity_Id
) return Entity_Id
425 Tagged_Type
: Entity_Id
:= Empty
;
428 if Is_Tagged_Type
(T
) then
429 if Is_First_Subtype
(T
) then
432 Tagged_Type
:= Base_Type
(T
);
435 -- If the type is incomplete, it may have been declared without a
436 -- Tagged indication, but the full view may be tagged, in which case
437 -- that is the controlling type of the subprogram. This is one of the
438 -- approx. 579 places in the language where a lookahead would help.
440 elsif Ekind
(T
) = E_Incomplete_Type
441 and then Present
(Full_View
(T
))
442 and then Is_Tagged_Type
(Full_View
(T
))
444 Set_Is_Tagged_Type
(T
);
445 Tagged_Type
:= Full_View
(T
);
447 elsif Ekind
(T
) = E_Anonymous_Access_Type
448 and then Is_Tagged_Type
(Designated_Type
(T
))
450 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
451 if Is_First_Subtype
(Designated_Type
(T
)) then
452 Tagged_Type
:= Designated_Type
(T
);
454 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
457 -- Ada 2005: an incomplete type can be tagged. An operation with an
458 -- access parameter of the type is dispatching.
460 elsif Scope
(Designated_Type
(T
)) = Current_Scope
then
461 Tagged_Type
:= Designated_Type
(T
);
463 -- Ada 2005 (AI-50217)
465 elsif From_Limited_With
(Designated_Type
(T
))
466 and then Has_Non_Limited_View
(Designated_Type
(T
))
467 and then Scope
(Designated_Type
(T
)) = Scope
(Subp
)
469 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
470 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
472 Tagged_Type
:= Base_Type
(Non_Limited_View
473 (Designated_Type
(T
)));
478 if No
(Tagged_Type
) or else Is_Class_Wide_Type
(Tagged_Type
) then
481 -- In the special case of a protected subprogram of a tagged protected
482 -- type that has a formal of a tagged type (or access formal whose type
483 -- designates a tagged type), such a formal is not controlling unless
484 -- it's of the protected type's corresponding record type. The latter
485 -- can occur for the special wrapper subprograms created for protected
486 -- subprograms. Such subprograms may occur in the same scope where some
487 -- formal's tagged type is declared, and we don't want formals of that
488 -- tagged type being marked as controlling, for one thing because they
489 -- aren't controlling from the language point of view, but also because
490 -- this can cause errors for access formals when conformance is checked
491 -- between the spec and body of the protected subprogram (null-exclusion
492 -- status of the formals may be set differently, which is the case that
493 -- led to adding this check).
495 elsif Is_Subprogram
(Subp
)
496 and then Present
(Protected_Subprogram
(Subp
))
497 and then Ekind
(Scope
(Protected_Subprogram
(Subp
))) = E_Protected_Type
499 Base_Type
(Tagged_Type
)
500 /= Corresponding_Record_Type
(Scope
(Protected_Subprogram
(Subp
)))
504 -- The dispatching type and the primitive operation must be defined in
505 -- the same scope, except in the case of internal operations and formal
506 -- abstract subprograms.
508 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
509 and then (not Is_Generic_Type
(Tagged_Type
)
510 or else not Comes_From_Source
(Subp
)))
512 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
514 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
516 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
518 Is_Abstract_Subprogram
(Subp
))
525 end Check_Controlling_Type
;
527 ----------------------------
528 -- Check_Dispatching_Call --
529 ----------------------------
531 procedure Check_Dispatching_Call
(N
: Node_Id
) is
532 Loc
: constant Source_Ptr
:= Sloc
(N
);
535 Control
: Node_Id
:= Empty
;
537 Subp_Entity
: Entity_Id
;
538 Indeterm_Ancestor_Call
: Boolean := False;
539 Indeterm_Ctrl_Type
: Entity_Id
:= Empty
; -- init to avoid warning
541 Static_Tag
: Node_Id
:= Empty
;
542 -- If a controlling formal has a statically tagged actual, the tag of
543 -- this actual is to be used for any tag-indeterminate actual.
545 procedure Check_Direct_Call
;
546 -- In the case when the controlling actual is a class-wide type whose
547 -- root type's completion is a task or protected type, the call is in
548 -- fact direct. This routine detects the above case and modifies the
551 procedure Check_Dispatching_Context
(Call
: Node_Id
);
552 -- If the call is tag-indeterminate and the entity being called is
553 -- abstract, verify that the context is a call that will eventually
554 -- provide a tag for dispatching, or has provided one already.
556 -----------------------
557 -- Check_Direct_Call --
558 -----------------------
560 procedure Check_Direct_Call
is
561 Typ
: Entity_Id
:= Etype
(Control
);
563 -- Predefined primitives do not receive wrappers since they are built
564 -- from scratch for the corresponding record of synchronized types.
565 -- Equality is in general predefined, but is excluded from the check
566 -- when it is user-defined.
568 if Is_Predefined_Dispatching_Operation
(Subp_Entity
)
569 and then not (Is_User_Defined_Equality
(Subp_Entity
)
570 and then Comes_From_Source
(Subp_Entity
)
571 and then Nkind
(Parent
(Subp_Entity
)) =
572 N_Function_Specification
)
577 if Is_Class_Wide_Type
(Typ
) then
578 Typ
:= Root_Type
(Typ
);
581 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
582 Typ
:= Full_View
(Typ
);
585 if Is_Concurrent_Type
(Typ
)
587 Present
(Corresponding_Record_Type
(Typ
))
589 Typ
:= Corresponding_Record_Type
(Typ
);
591 -- The concurrent record's list of primitives should contain a
592 -- wrapper for the entity of the call, retrieve it.
597 Wrapper_Found
: Boolean := False;
600 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
601 while Present
(Prim_Elmt
) loop
602 Prim
:= Node
(Prim_Elmt
);
604 if Is_Primitive_Wrapper
(Prim
)
605 and then Wrapped_Entity
(Prim
) = Subp_Entity
607 Wrapper_Found
:= True;
611 Next_Elmt
(Prim_Elmt
);
614 -- A primitive declared between two views should have a
615 -- corresponding wrapper.
617 pragma Assert
(Wrapper_Found
);
619 -- Modify the call by setting the proper entity
621 Set_Entity
(Name
(N
), Prim
);
624 end Check_Direct_Call
;
626 -------------------------------
627 -- Check_Dispatching_Context --
628 -------------------------------
630 procedure Check_Dispatching_Context
(Call
: Node_Id
) is
631 Subp
: constant Entity_Id
:= Entity
(Name
(Call
));
633 procedure Abstract_Context_Error
;
634 -- Error for abstract call dispatching on result is not dispatching
636 function Has_Controlling_Current_Instance_Actual_In_DIC
637 (Call
: Node_Id
) return Boolean;
638 -- Return True if the subprogram call Call has a controlling actual
639 -- given directly by a current instance referenced within a DIC
642 ----------------------------
643 -- Abstract_Context_Error --
644 ----------------------------
646 procedure Abstract_Context_Error
is
648 if Ekind
(Subp
) = E_Function
then
650 ("call to abstract function must be dispatching", N
);
652 -- This error can occur for a procedure in the case of a call to
653 -- an abstract formal procedure with a statically tagged operand.
657 ("call to abstract procedure must be dispatching", N
);
659 end Abstract_Context_Error
;
661 ----------------------------------------
662 -- Has_Current_Instance_Actual_In_DIC --
663 ----------------------------------------
665 function Has_Controlling_Current_Instance_Actual_In_DIC
666 (Call
: Node_Id
) return Boolean
671 F
:= First_Formal
(Subp_Entity
);
672 A
:= First_Actual
(Call
);
674 while Present
(F
) loop
676 -- Return True if the actual denotes a current instance (which
677 -- will be represented by an in-mode formal of the enclosing
678 -- DIC_Procedure) passed to a controlling formal. We don't have
679 -- to worry about controlling access formals here, because its
680 -- illegal to apply Access (etc.) attributes to a current
681 -- instance within an aspect (by AI12-0068).
683 if Is_Controlling_Formal
(F
)
684 and then Nkind
(A
) = N_Identifier
685 and then Ekind
(Entity
(A
)) = E_In_Parameter
686 and then Is_Subprogram
(Scope
(Entity
(A
)))
687 and then Is_DIC_Procedure
(Scope
(Entity
(A
)))
697 end Has_Controlling_Current_Instance_Actual_In_DIC
;
701 Scop
: constant Entity_Id
:= Current_Scope_No_Loops
;
702 Typ
: constant Entity_Id
:= Etype
(Subp
);
705 -- Start of processing for Check_Dispatching_Context
708 -- Skip checking context of dispatching calls during preanalysis of
709 -- class-wide conditions since at that stage the expression is not
710 -- installed yet on its definite context.
712 if Inside_Class_Condition_Preanalysis
then
716 -- If the called subprogram is a private overriding, replace it
717 -- with its alias, which has the correct body. Verify that the
718 -- two subprograms have the same controlling type (this is not the
719 -- case for an inherited subprogram that has become abstract).
721 if Is_Abstract_Subprogram
(Subp
)
722 and then No
(Controlling_Argument
(Call
))
724 if Present
(Alias
(Subp
))
725 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
726 and then No
(DTC_Entity
(Subp
))
727 and then Find_Dispatching_Type
(Subp
) =
728 Find_Dispatching_Type
(Alias
(Subp
))
730 -- Private overriding of inherited abstract operation, call is
733 Set_Entity
(Name
(N
), Alias
(Subp
));
736 -- If this is a pre/postcondition for an abstract subprogram,
737 -- it may call another abstract function that is a primitive
738 -- of an abstract type. The call is nondispatching but will be
739 -- legal in overridings of the operation. However, if the call
740 -- is tag-indeterminate we want to continue with with the error
741 -- checking below, as this case is illegal even for abstract
742 -- subprograms (see AI12-0170).
744 -- Similarly, as per AI12-0412, a nonabstract subprogram may
745 -- have a class-wide pre/postcondition that includes a call to
746 -- an abstract primitive of the subprogram's controlling type.
747 -- Certain operations (nondispatching calls, 'Access, use as
748 -- a generic actual) applied to such a nonabstract subprogram
749 -- are illegal in the case where the type is abstract (see
750 -- RM 6.1.1(18.2/5)).
752 elsif Is_Subprogram
(Scop
)
753 and then not Is_Tag_Indeterminate
(N
)
755 -- The context is an internally built helper or an indirect
756 -- call wrapper that handles class-wide preconditions
757 (Present
(Class_Preconditions_Subprogram
(Scop
))
759 -- ... or the context is a class-wide pre/postcondition.
761 (In_Pre_Post_Condition
(Call
, Class_Wide_Only
=> True)
763 -- The tagged type associated with the called
764 -- subprogram must be the same as that of the
765 -- subprogram with a class-wide aspect.
767 and then Is_Dispatching_Operation
(Scop
)
768 and then Find_Dispatching_Type
(Subp
)
769 = Find_Dispatching_Type
(Scop
)))
773 -- Similarly to the dispensation for postconditions, a call to
774 -- an abstract function within a Default_Initial_Condition aspect
775 -- can be legal when passed a current instance of the type. Such
776 -- a call will be effectively mapped to a call to a primitive of
777 -- a descendant type (see AI12-0397, as well as AI12-0170), so
778 -- doesn't need to be dispatching. We test for being within a DIC
779 -- procedure, since that's where the call will be analyzed.
781 elsif Is_Subprogram
(Scop
)
782 and then Is_DIC_Procedure
(Scop
)
783 and then Has_Controlling_Current_Instance_Actual_In_DIC
(Call
)
787 elsif Ekind
(Current_Scope
) = E_Function
788 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
789 N_Generic_Subprogram_Declaration
794 -- We need to determine whether the context of the call
795 -- provides a tag to make the call dispatching. This requires
796 -- the call to be the actual in an enclosing call, and that
797 -- actual must be controlling. If the call is an operand of
798 -- equality, the other operand must not be abstract.
800 if not Is_Tagged_Type
(Typ
)
802 (Ekind
(Typ
) = E_Anonymous_Access_Type
803 and then Is_Tagged_Type
(Designated_Type
(Typ
)))
805 Abstract_Context_Error
;
809 Par
:= Parent
(Call
);
811 if Nkind
(Par
) = N_Parameter_Association
then
815 if Nkind
(Par
) = N_Qualified_Expression
816 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
821 if Nkind
(Par
) in N_Subprogram_Call
822 and then Is_Entity_Name
(Name
(Par
))
825 Enc_Subp
: constant Entity_Id
:= Entity
(Name
(Par
));
829 Ret_Type
: Entity_Id
;
832 -- Find controlling formal that can provide tag for the
833 -- tag-indeterminate actual. The corresponding actual
834 -- must be the corresponding class-wide type.
836 F
:= First_Formal
(Enc_Subp
);
837 A
:= First_Actual
(Par
);
839 -- Find controlling type of call. Dereference if function
840 -- returns an access type.
842 Ret_Type
:= Etype
(Call
);
843 if Is_Access_Type
(Etype
(Call
)) then
844 Ret_Type
:= Designated_Type
(Ret_Type
);
847 while Present
(F
) loop
848 Control
:= Etype
(A
);
850 if Is_Access_Type
(Control
) then
851 Control
:= Designated_Type
(Control
);
854 if Is_Controlling_Formal
(F
)
855 and then not (Call
= A
or else Parent
(Call
) = A
)
856 and then Control
= Class_Wide_Type
(Ret_Type
)
865 if Nkind
(Par
) = N_Function_Call
866 and then Is_Tag_Indeterminate
(Par
)
868 -- The parent may be an actual of an enclosing call
870 Check_Dispatching_Context
(Par
);
875 ("call to abstract function must be dispatching",
881 -- For equality operators, one of the operands must be
882 -- statically or dynamically tagged.
884 elsif Nkind
(Par
) in N_Op_Eq | N_Op_Ne
then
885 if N
= Right_Opnd
(Par
)
886 and then Is_Tag_Indeterminate
(Left_Opnd
(Par
))
888 Abstract_Context_Error
;
890 elsif N
= Left_Opnd
(Par
)
891 and then Is_Tag_Indeterminate
(Right_Opnd
(Par
))
893 Abstract_Context_Error
;
898 -- The left-hand side of an assignment provides the tag
900 elsif Nkind
(Par
) = N_Assignment_Statement
then
904 Abstract_Context_Error
;
908 end Check_Dispatching_Context
;
910 -- Start of processing for Check_Dispatching_Call
913 -- Find a controlling argument, if any
915 if Present
(Parameter_Associations
(N
)) then
916 Subp_Entity
:= Entity
(Name
(N
));
918 Actual
:= First_Actual
(N
);
919 Formal
:= First_Formal
(Subp_Entity
);
920 while Present
(Actual
) loop
921 Control
:= Find_Controlling_Arg
(Actual
);
922 exit when Present
(Control
);
924 -- Check for the case where the actual is a tag-indeterminate call
925 -- whose result type is different than the tagged type associated
926 -- with the containing call, but is an ancestor of the type.
928 if Is_Controlling_Formal
(Formal
)
929 and then Is_Tag_Indeterminate
(Actual
)
930 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
931 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
933 Indeterm_Ancestor_Call
:= True;
934 Indeterm_Ctrl_Type
:= Etype
(Formal
);
936 -- If the formal is controlling but the actual is not, the type
937 -- of the actual is statically known, and may be used as the
938 -- controlling tag for some other tag-indeterminate actual.
940 elsif Is_Controlling_Formal
(Formal
)
941 and then Is_Entity_Name
(Actual
)
942 and then Is_Tagged_Type
(Etype
(Actual
))
944 Static_Tag
:= Actual
;
947 Next_Actual
(Actual
);
948 Next_Formal
(Formal
);
951 -- If the call doesn't have a controlling actual but does have an
952 -- indeterminate actual that requires dispatching treatment, then an
953 -- object is needed that will serve as the controlling argument for
954 -- a dispatching call on the indeterminate actual. This can occur
955 -- in the unusual situation of a default actual given by a tag-
956 -- indeterminate call and where the type of the call is an ancestor
957 -- of the type associated with a containing call to an inherited
958 -- operation (see AI-239).
960 -- Rather than create an object of the tagged type, which would
961 -- be problematic for various reasons (default initialization,
962 -- discriminants), the tag of the containing call's associated
963 -- tagged type is directly used to control the dispatching.
966 and then Indeterm_Ancestor_Call
967 and then No
(Static_Tag
)
970 Make_Attribute_Reference
(Loc
,
971 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
972 Attribute_Name
=> Name_Tag
);
977 if Present
(Control
) then
979 -- Verify that no controlling arguments are statically tagged
982 Write_Str
("Found Dispatching call");
987 Actual
:= First_Actual
(N
);
988 while Present
(Actual
) loop
989 if Actual
/= Control
then
991 if not Is_Controlling_Actual
(Actual
) then
992 null; -- Can be anything
994 elsif Is_Dynamically_Tagged
(Actual
) then
995 null; -- Valid parameter
997 elsif Is_Tag_Indeterminate
(Actual
) then
999 -- The tag is inherited from the enclosing call (the node
1000 -- we are currently analyzing). Explicitly expand the
1001 -- actual, since the previous call to Expand (from
1002 -- Resolve_Call) had no way of knowing about the
1003 -- required dispatching.
1005 Propagate_Tag
(Control
, Actual
);
1009 ("controlling argument is not dynamically tagged",
1015 Next_Actual
(Actual
);
1018 -- Mark call as a dispatching call
1020 Set_Controlling_Argument
(N
, Control
);
1021 Check_Restriction
(No_Dispatching_Calls
, N
);
1023 -- The dispatching call may need to be converted into a direct
1024 -- call in certain cases.
1028 -- If there is a statically tagged actual and a tag-indeterminate
1029 -- call to a function of the ancestor (such as that provided by a
1030 -- default), then treat this as a dispatching call and propagate
1031 -- the tag to the tag-indeterminate call(s).
1033 elsif Present
(Static_Tag
) and then Indeterm_Ancestor_Call
then
1035 Make_Attribute_Reference
(Loc
,
1037 New_Occurrence_Of
(Etype
(Static_Tag
), Loc
),
1038 Attribute_Name
=> Name_Tag
);
1042 Actual
:= First_Actual
(N
);
1043 Formal
:= First_Formal
(Subp_Entity
);
1044 while Present
(Actual
) loop
1045 if Is_Tag_Indeterminate
(Actual
)
1046 and then Is_Controlling_Formal
(Formal
)
1048 Propagate_Tag
(Control
, Actual
);
1051 Next_Actual
(Actual
);
1052 Next_Formal
(Formal
);
1055 Check_Dispatching_Context
(N
);
1057 elsif Nkind
(N
) /= N_Function_Call
then
1059 -- The call is not dispatching, so check that there aren't any
1060 -- tag-indeterminate abstract calls left among its actuals.
1062 Actual
:= First_Actual
(N
);
1063 while Present
(Actual
) loop
1064 if Is_Tag_Indeterminate
(Actual
) then
1066 -- Function call case
1068 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
1069 Func
:= Entity
(Name
(Original_Node
(Actual
)));
1071 -- If the actual is an attribute then it can't be abstract
1072 -- (the only current case of a tag-indeterminate attribute
1073 -- is the stream Input attribute).
1075 elsif Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
1079 -- Ditto if it is an explicit dereference
1081 elsif Nkind
(Original_Node
(Actual
)) = N_Explicit_Dereference
1085 -- Only other possibility is a qualified expression whose
1086 -- constituent expression is itself a call.
1090 Entity
(Name
(Original_Node
1091 (Expression
(Original_Node
(Actual
)))));
1094 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
1096 ("call to abstract function must be dispatching",
1101 Next_Actual
(Actual
);
1104 Check_Dispatching_Context
(N
);
1106 elsif Nkind
(Parent
(N
)) in N_Subexpr
then
1107 Check_Dispatching_Context
(N
);
1109 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
1110 and then Is_Class_Wide_Type
(Etype
(Name
(Parent
(N
))))
1114 elsif Is_Abstract_Subprogram
(Subp_Entity
) then
1115 Check_Dispatching_Context
(N
);
1119 -- If this is a nondispatching call to a nonabstract subprogram
1120 -- and the subprogram has any Pre'Class or Post'Class aspects with
1121 -- nonstatic values, then report an error. This is specified by
1122 -- RM 6.1.1(18.2/5) (by AI12-0412).
1124 -- Skip reporting this error on helpers and indirect-call wrappers
1125 -- built to support class-wide preconditions.
1128 and then not Is_Abstract_Subprogram
(Subp_Entity
)
1130 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post
(Subp_Entity
)
1132 (Is_Subprogram
(Current_Scope
)
1134 Present
(Class_Preconditions_Subprogram
(Current_Scope
)))
1137 ("nondispatching call to nonabstract subprogram of "
1138 & "abstract type with nonstatic class-wide "
1139 & "pre/postconditions",
1144 -- If dispatching on result, the enclosing call, if any, will
1145 -- determine the controlling argument. Otherwise this is the
1146 -- primitive operation of the root type.
1148 Check_Dispatching_Context
(N
);
1150 end Check_Dispatching_Call
;
1152 ---------------------------------
1153 -- Check_Dispatching_Operation --
1154 ---------------------------------
1156 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
1157 function Is_Access_To_Subprogram_Wrapper
(E
: Entity_Id
) return Boolean;
1158 -- Return True if E is an access to subprogram wrapper
1160 procedure Warn_On_Late_Primitive_After_Private_Extension
1163 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
1164 -- if it is a public primitive defined after some private extension of
1167 -------------------------------------
1168 -- Is_Access_To_Subprogram_Wrapper --
1169 -------------------------------------
1171 function Is_Access_To_Subprogram_Wrapper
(E
: Entity_Id
) return Boolean
1173 Decl_N
: constant Node_Id
:= Unit_Declaration_Node
(E
);
1174 Par_N
: constant Node_Id
:= Parent
(List_Containing
(Decl_N
));
1177 -- Access to subprogram wrappers are declared in the freezing actions
1179 return Nkind
(Par_N
) = N_Freeze_Entity
1180 and then Ekind
(Entity
(Par_N
)) = E_Access_Subprogram_Type
;
1181 end Is_Access_To_Subprogram_Wrapper
;
1183 ----------------------------------------------------
1184 -- Warn_On_Late_Primitive_After_Private_Extension --
1185 ----------------------------------------------------
1187 procedure Warn_On_Late_Primitive_After_Private_Extension
1194 if Warn_On_Late_Primitives
1195 and then Comes_From_Source
(Prim
)
1196 and then Has_Private_Extension
(Typ
)
1197 and then Is_Package_Or_Generic_Package
(Current_Scope
)
1198 and then not In_Private_Part
(Current_Scope
)
1200 E
:= Next_Entity
(Typ
);
1202 while E
/= Prim
loop
1203 if Ekind
(E
) = E_Record_Type_With_Private
1204 and then Etype
(E
) = Typ
1206 Error_Msg_Name_1
:= Chars
(Typ
);
1207 Error_Msg_Name_2
:= Chars
(E
);
1208 Error_Msg_Sloc
:= Sloc
(E
);
1210 ("?.j?primitive of type % defined after private extension "
1212 Error_Msg_Name_1
:= Chars
(Prim
);
1213 Error_Msg_Name_2
:= Chars
(E
);
1215 ("\spec of % should appear before declaration of type %!",
1223 end Warn_On_Late_Primitive_After_Private_Extension
;
1227 Body_Is_Last_Primitive
: Boolean := False;
1228 Has_Dispatching_Parent
: Boolean := False;
1229 Ovr_Subp
: Entity_Id
:= Empty
;
1230 Tagged_Type
: Entity_Id
;
1232 -- Start of processing for Check_Dispatching_Operation
1235 if Ekind
(Subp
) not in E_Function | E_Procedure
then
1238 -- The Default_Initial_Condition procedure is not a primitive subprogram
1239 -- even if it relates to a tagged type. This routine is not meant to be
1240 -- inherited or overridden.
1242 elsif Is_DIC_Procedure
(Subp
) then
1245 -- The "partial" and "full" type invariant procedures are not primitive
1246 -- subprograms even if they relate to a tagged type. These routines are
1247 -- not meant to be inherited or overridden.
1249 elsif Is_Invariant_Procedure
(Subp
)
1250 or else Is_Partial_Invariant_Procedure
(Subp
)
1254 -- Wrappers of access to subprograms are not primitive subprograms.
1256 elsif Is_Wrapper
(Subp
)
1257 and then Is_Access_To_Subprogram_Wrapper
(Subp
)
1262 Set_Is_Dispatching_Operation
(Subp
, False);
1263 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1265 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1266 -- Required because primitives of concurrent types are attached
1267 -- to the corresponding record (not to the concurrent type).
1269 if Ada_Version
>= Ada_2005
1270 and then Present
(Tagged_Type
)
1271 and then Is_Concurrent_Type
(Tagged_Type
)
1272 and then Present
(Corresponding_Record_Type
(Tagged_Type
))
1274 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
1277 -- (AI-345): The task body procedure is not a primitive of the tagged
1280 if Present
(Tagged_Type
)
1281 and then Is_Concurrent_Record_Type
(Tagged_Type
)
1282 and then Present
(Corresponding_Concurrent_Type
(Tagged_Type
))
1283 and then Is_Task_Type
(Corresponding_Concurrent_Type
(Tagged_Type
))
1284 and then Subp
= Get_Task_Body_Procedure
1285 (Corresponding_Concurrent_Type
(Tagged_Type
))
1290 -- If Subp is derived from a dispatching operation then it should
1291 -- always be treated as dispatching. In this case various checks
1292 -- below will be bypassed. Makes sure that late declarations for
1293 -- inherited private subprograms are treated as dispatching, even
1294 -- if the associated tagged type is already frozen.
1296 Has_Dispatching_Parent
:=
1297 Present
(Alias
(Subp
))
1298 and then Is_Dispatching_Operation
(Alias
(Subp
));
1300 if No
(Tagged_Type
) then
1302 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1303 -- with an abstract interface type unless the interface acts as a
1304 -- parent type in a derivation. If the interface type is a formal
1305 -- type then the operation is not primitive and therefore legal.
1312 E
:= First_Entity
(Subp
);
1313 while Present
(E
) loop
1315 -- For an access parameter, check designated type
1317 if Ekind
(Etype
(E
)) = E_Anonymous_Access_Type
then
1318 Typ
:= Designated_Type
(Etype
(E
));
1323 if Comes_From_Source
(Subp
)
1324 and then Is_Interface
(Typ
)
1325 and then not Is_Class_Wide_Type
(Typ
)
1326 and then not Is_Derived_Type
(Typ
)
1327 and then not Is_Generic_Type
(Typ
)
1328 and then not In_Instance
1330 Error_Msg_N
("??declaration of& is too late!", Subp
);
1331 Error_Msg_NE
-- CODEFIX??
1332 ("\??spec should appear immediately after declaration of "
1333 & "& !", Subp
, Typ
);
1340 -- In case of functions check also the result type
1342 if Ekind
(Subp
) = E_Function
then
1343 if Is_Access_Type
(Etype
(Subp
)) then
1344 Typ
:= Designated_Type
(Etype
(Subp
));
1346 Typ
:= Etype
(Subp
);
1349 -- The following should be better commented, especially since
1350 -- we just added several new conditions here ???
1352 if Comes_From_Source
(Subp
)
1353 and then Is_Interface
(Typ
)
1354 and then not Is_Class_Wide_Type
(Typ
)
1355 and then not Is_Derived_Type
(Typ
)
1356 and then not Is_Generic_Type
(Typ
)
1357 and then not In_Instance
1359 Error_Msg_N
("??declaration of& is too late!", Subp
);
1361 ("\??spec should appear immediately after declaration of "
1362 & "& !", Subp
, Typ
);
1369 -- The subprograms build internally after the freezing point (such as
1370 -- init procs, interface thunks, type support subprograms, and Offset
1371 -- to top functions for accessing interface components in variable
1372 -- size tagged types) are not primitives.
1374 elsif Is_Frozen
(Tagged_Type
)
1375 and then not Comes_From_Source
(Subp
)
1376 and then not Has_Dispatching_Parent
1378 -- Complete decoration of internally built subprograms that override
1379 -- a dispatching primitive. These entities correspond with the
1382 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1383 -- to override functions of nonabstract null extensions. These
1384 -- primitives were added to the list of primitives of the tagged
1385 -- type by Make_Controlling_Function_Wrappers. However, attribute
1386 -- Is_Dispatching_Operation must be set to true.
1388 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1391 -- 3. Subprograms associated with stream attributes (built by
1392 -- New_Stream_Subprogram) or with the Put_Image attribute.
1394 -- 4. Wrappers built for inherited operations with inherited class-
1395 -- wide conditions, where the conditions include calls to other
1396 -- overridden primitives. The wrappers include checks on these
1397 -- modified conditions. (AI12-113).
1399 -- 5. Declarations built for subprograms without separate specs that
1400 -- are eligible for inlining in GNATprove (inside
1401 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1403 if Present
(Old_Subp
)
1404 and then Present
(Overridden_Operation
(Subp
))
1405 and then Is_Dispatching_Operation
(Old_Subp
)
1408 ((Ekind
(Subp
) = E_Function
1409 and then Is_Dispatching_Operation
(Old_Subp
)
1410 and then Is_Null_Extension
(Base_Type
(Etype
(Subp
))))
1413 (Ekind
(Subp
) = E_Procedure
1414 and then Is_Dispatching_Operation
(Old_Subp
)
1415 and then Present
(Alias
(Old_Subp
))
1416 and then Is_Null_Interface_Primitive
1417 (Ultimate_Alias
(Old_Subp
)))
1419 or else Get_TSS_Name
(Subp
) = TSS_Stream_Read
1420 or else Get_TSS_Name
(Subp
) = TSS_Stream_Write
1421 or else Get_TSS_Name
(Subp
) = TSS_Put_Image
1425 and then Present
(LSP_Subprogram
(Subp
)))
1427 or else GNATprove_Mode
);
1429 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1430 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
1431 Set_Is_Dispatching_Operation
(Subp
);
1436 -- The operation may be a child unit, whose scope is the defining
1437 -- package, but which is not a primitive operation of the type.
1439 elsif Is_Child_Unit
(Subp
) then
1442 -- If the subprogram is not defined in a package spec, the only case
1443 -- where it can be a dispatching op is when it overrides an operation
1444 -- before the freezing point of the type.
1446 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
1447 or else In_Package_Body
(Scope
(Subp
)))
1448 and then not Has_Dispatching_Parent
1450 if not Comes_From_Source
(Subp
)
1451 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
1455 -- If the type is already frozen, the overriding is not allowed
1456 -- except when Old_Subp is not a dispatching operation (which can
1457 -- occur when Old_Subp was inherited by an untagged type). However,
1458 -- a body with no previous spec freezes the type *after* its
1459 -- declaration, and therefore is a legal overriding (unless the type
1460 -- has already been frozen). Only the first such body is legal.
1462 elsif Present
(Old_Subp
)
1463 and then Is_Dispatching_Operation
(Old_Subp
)
1465 if Comes_From_Source
(Subp
)
1467 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
1468 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
1471 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1472 Decl_Item
: Node_Id
;
1475 -- ??? The checks here for whether the type has been frozen
1476 -- prior to the new body are not complete. It's not simple
1477 -- to check frozenness at this point since the body has
1478 -- already caused the type to be prematurely frozen in
1479 -- Analyze_Declarations, but we're forced to recheck this
1480 -- here because of the odd rule interpretation that allows
1481 -- the overriding if the type wasn't frozen prior to the
1482 -- body. The freezing action should probably be delayed
1483 -- until after the spec is seen, but that's a tricky
1484 -- change to the delicate freezing code.
1486 -- Look at each declaration following the type up until the
1487 -- new subprogram body. If any of the declarations is a body
1488 -- then the type has been frozen already so the overriding
1489 -- primitive is illegal.
1491 Decl_Item
:= Next
(Parent
(Tagged_Type
));
1492 while Present
(Decl_Item
)
1493 and then (Decl_Item
/= Subp_Body
)
1495 if Comes_From_Source
(Decl_Item
)
1496 and then (Nkind
(Decl_Item
) in N_Proper_Body
1497 or else Nkind
(Decl_Item
) in N_Body_Stub
)
1499 Error_Msg_N
("overriding of& is too late!", Subp
);
1501 ("\spec should appear immediately after the type!",
1509 -- If the subprogram doesn't follow in the list of
1510 -- declarations including the type then the type has
1511 -- definitely been frozen already and the body is illegal.
1513 if No
(Decl_Item
) then
1514 Error_Msg_N
("overriding of& is too late!", Subp
);
1516 ("\spec should appear immediately after the type!",
1519 elsif Is_Frozen
(Subp
) then
1521 -- The subprogram body declares a primitive operation.
1522 -- If the subprogram is already frozen, we must update
1523 -- its dispatching information explicitly here. The
1524 -- information is taken from the overridden subprogram.
1525 -- We must also generate a cross-reference entry because
1526 -- references to other primitives were already created
1527 -- when type was frozen.
1529 Body_Is_Last_Primitive
:= True;
1531 if Present
(DTC_Entity
(Old_Subp
)) then
1532 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
1533 Set_DT_Position_Value
(Subp
, DT_Position
(Old_Subp
));
1535 if not Restriction_Active
(No_Dispatching_Calls
) then
1536 if Building_Static_DT
(Tagged_Type
) then
1538 -- If the static dispatch table has not been
1539 -- built then there is nothing else to do now;
1540 -- otherwise we notify that we cannot build the
1541 -- static dispatch table.
1543 if Has_Dispatch_Table
(Tagged_Type
) then
1545 ("overriding of& is too late for building "
1546 & " static dispatch tables!", Subp
);
1548 ("\spec should appear immediately after "
1549 & "the type!", Subp
);
1552 -- No code required to register primitives in VM
1555 elsif not Tagged_Type_Expansion
then
1559 Insert_Actions_After
(Subp_Body
,
1560 Register_Primitive
(Sloc
(Subp_Body
),
1564 -- Indicate that this is an overriding operation,
1565 -- and replace the overridden entry in the list of
1566 -- primitive operations, which is used for xref
1567 -- generation subsequently.
1569 Generate_Reference
(Tagged_Type
, Subp
, 'P', False);
1570 Override_Dispatching_Operation
1571 (Tagged_Type
, Old_Subp
, Subp
);
1572 Set_Is_Dispatching_Operation
(Subp
);
1574 -- Inherit decoration of controlling formals and
1575 -- controlling result.
1577 if Ekind
(Old_Subp
) = E_Function
1578 and then Has_Controlling_Result
(Old_Subp
)
1580 Set_Has_Controlling_Result
(Subp
);
1583 if Present
(First_Formal
(Old_Subp
)) then
1585 Old_Formal
: Entity_Id
;
1589 Formal
:= First_Formal
(Subp
);
1590 Old_Formal
:= First_Formal
(Old_Subp
);
1592 while Present
(Old_Formal
) loop
1593 Set_Is_Controlling_Formal
(Formal
,
1594 Is_Controlling_Formal
(Old_Formal
));
1596 Next_Formal
(Formal
);
1597 Next_Formal
(Old_Formal
);
1603 Check_Inherited_Conditions
(Tagged_Type
,
1604 Late_Overriding
=> True);
1610 Error_Msg_N
("overriding of& is too late!", Subp
);
1612 ("\subprogram spec should appear immediately after the type!",
1616 -- If the type is not frozen yet and we are not in the overriding
1617 -- case it looks suspiciously like an attempt to define a primitive
1618 -- operation, which requires the declaration to be in a package spec
1619 -- (3.2.3(6)). Only report cases where the type and subprogram are
1620 -- in the same declaration list (by checking the enclosing parent
1621 -- declarations), to avoid spurious warnings on subprograms in
1622 -- instance bodies when the type is declared in the instance spec
1623 -- but hasn't been frozen by the instance body.
1625 elsif not Is_Frozen
(Tagged_Type
)
1626 and then In_Same_List
(Parent
(Tagged_Type
), Parent
(Parent
(Subp
)))
1629 ("??not dispatching (must be defined in a package spec)", Subp
);
1632 -- When the type is frozen, it is legitimate to define a new
1633 -- non-primitive operation.
1639 -- Now, we are sure that the scope is a package spec. If the subprogram
1640 -- is declared after the freezing point of the type that's an error
1642 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
1643 Error_Msg_N
("this primitive operation is declared too late", Subp
);
1645 ("??no primitive operations for& after this line",
1646 Freeze_Node
(Tagged_Type
),
1651 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1653 Ovr_Subp
:= Old_Subp
;
1655 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1656 -- overridden by Subp. This only applies to source subprograms, and
1657 -- their declaration must carry an explicit overriding indicator.
1660 and then Ada_Version
>= Ada_2012
1661 and then Comes_From_Source
(Subp
)
1663 Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Declaration
1665 Ovr_Subp
:= Find_Hidden_Overridden_Primitive
(Subp
);
1667 -- Verify that the proper overriding indicator has been supplied.
1669 if Present
(Ovr_Subp
)
1671 not Must_Override
(Specification
(Unit_Declaration_Node
(Subp
)))
1673 Error_Msg_NE
("missing overriding indicator for&", Subp
, Subp
);
1677 -- Now it should be a correct primitive operation, put it in the list
1679 if Present
(Ovr_Subp
) then
1681 -- If the type has interfaces we complete this check after we set
1682 -- attribute Is_Dispatching_Operation.
1684 Check_Subtype_Conformant
(Subp
, Ovr_Subp
);
1686 -- A primitive operation with the name of a primitive controlled
1687 -- operation does not override a non-visible overriding controlled
1688 -- operation, i.e. one declared in a private part when the full
1689 -- view of a type is controlled. Conversely, it will override a
1690 -- visible operation that may be declared in a partial view when
1691 -- the full view is controlled.
1693 if Chars
(Subp
) in Name_Initialize | Name_Adjust | Name_Finalize
1694 and then Is_Controlled
(Tagged_Type
)
1695 and then not Is_Visibly_Controlled
(Tagged_Type
)
1696 and then not Is_Inherited_Public_Operation
(Ovr_Subp
)
1698 Set_Overridden_Operation
(Subp
, Empty
);
1700 -- If the subprogram specification carries an overriding
1701 -- indicator, no need for the warning: it is either redundant,
1702 -- or else an error will be reported.
1704 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
1706 (Must_Override
(Parent
(Subp
))
1707 or else Must_Not_Override
(Parent
(Subp
)))
1711 -- Here we need the warning
1715 ("operation does not override inherited&??", Subp
, Subp
);
1719 Override_Dispatching_Operation
(Tagged_Type
, Ovr_Subp
, Subp
);
1721 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1722 -- that covers abstract interface subprograms we must register it
1723 -- in all the secondary dispatch tables associated with abstract
1724 -- interfaces. We do this now only if not building static tables,
1725 -- nor when the expander is inactive (we avoid trying to register
1726 -- primitives in semantics-only mode, since the type may not have
1727 -- an associated dispatch table). Otherwise the patch code is
1728 -- emitted after those tables are built, to prevent access before
1729 -- elaboration in gigi.
1731 if Body_Is_Last_Primitive
1732 and then not Building_Static_DT
(Tagged_Type
)
1733 and then Expander_Active
1734 and then Tagged_Type_Expansion
1737 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1742 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1743 while Present
(Elmt
) loop
1744 Prim
:= Node
(Elmt
);
1746 if Present
(Alias
(Prim
))
1747 and then Present
(Interface_Alias
(Prim
))
1748 and then Alias
(Prim
) = Subp
1750 Insert_Actions_After
(Subp_Body
,
1751 Register_Primitive
(Sloc
(Subp_Body
), Prim
=> Prim
));
1757 -- Redisplay the contents of the updated dispatch table
1759 if Debug_Flag_ZZ
then
1760 Write_Str
("Late overriding: ");
1761 Write_DT
(Tagged_Type
);
1767 -- If no old subprogram, then we add this as a dispatching operation,
1768 -- but we avoid doing this if an error was posted, to prevent annoying
1771 elsif not Error_Posted
(Subp
) then
1772 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
1775 Set_Is_Dispatching_Operation
(Subp
, True);
1777 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1778 -- subtype conformance against all the interfaces covered by this
1781 if Present
(Ovr_Subp
)
1782 and then Has_Interfaces
(Tagged_Type
)
1785 Ifaces_List
: Elist_Id
;
1786 Iface_Elmt
: Elmt_Id
;
1787 Iface_Prim_Elmt
: Elmt_Id
;
1788 Iface_Prim
: Entity_Id
;
1789 Ret_Typ
: Entity_Id
;
1792 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
1794 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
1795 while Present
(Iface_Elmt
) loop
1796 if not Is_Ancestor
(Node
(Iface_Elmt
), Tagged_Type
) then
1798 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
1799 while Present
(Iface_Prim_Elmt
) loop
1800 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
1802 if Is_Interface_Conformant
1803 (Tagged_Type
, Iface_Prim
, Subp
)
1805 -- Handle procedures, functions whose return type
1806 -- matches, or functions not returning interfaces
1808 if Ekind
(Subp
) = E_Procedure
1809 or else Etype
(Iface_Prim
) = Etype
(Subp
)
1810 or else not Is_Interface
(Etype
(Iface_Prim
))
1812 Check_Subtype_Conformant
1814 Old_Id
=> Iface_Prim
,
1816 Skip_Controlling_Formals
=> True);
1818 -- Handle functions returning interfaces
1820 elsif Implements_Interface
1821 (Etype
(Subp
), Etype
(Iface_Prim
))
1823 -- Temporarily force both entities to return the
1824 -- same type. Required because Subtype_Conformant
1825 -- does not handle this case.
1827 Ret_Typ
:= Etype
(Iface_Prim
);
1828 Set_Etype
(Iface_Prim
, Etype
(Subp
));
1830 Check_Subtype_Conformant
1832 Old_Id
=> Iface_Prim
,
1834 Skip_Controlling_Formals
=> True);
1836 Set_Etype
(Iface_Prim
, Ret_Typ
);
1840 Next_Elmt
(Iface_Prim_Elmt
);
1844 Next_Elmt
(Iface_Elmt
);
1849 if not Body_Is_Last_Primitive
then
1850 Set_DT_Position_Value
(Subp
, No_Uint
);
1852 elsif Has_Controlled_Component
(Tagged_Type
)
1853 and then Chars
(Subp
) in Name_Initialize
1856 | Name_Finalize_Address
1859 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
1863 Old_Spec
: Entity_Id
;
1865 C_Names
: constant array (1 .. 4) of Name_Id
:=
1869 Name_Finalize_Address
);
1871 D_Names
: constant array (1 .. 4) of TSS_Name_Type
:=
1872 (TSS_Deep_Initialize
,
1875 TSS_Finalize_Address
);
1878 -- Remove previous controlled function which was constructed and
1879 -- analyzed when the type was frozen. This requires removing the
1880 -- body of the redefined primitive, as well as its specification
1881 -- if needed (there is no spec created for Deep_Initialize, see
1882 -- exp_ch3.adb). We must also dismantle the exception information
1883 -- that may have been generated for it when front end zero-cost
1884 -- tables are enabled.
1886 for J
in D_Names
'Range loop
1887 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
1890 and then Chars
(Subp
) = C_Names
(J
)
1892 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
1894 Set_Is_Eliminated
(Old_P
);
1895 Set_Scope
(Old_P
, Scope
(Current_Scope
));
1897 if Nkind
(Old_Bod
) = N_Subprogram_Body
1898 and then Present
(Corresponding_Spec
(Old_Bod
))
1900 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
1901 Set_Has_Completion
(Old_Spec
, False);
1906 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
1908 -- The new operation is added to the actions of the freeze node
1909 -- for the type, but this node has already been analyzed, so we
1910 -- must retrieve and analyze explicitly the new body.
1913 and then Present
(Actions
(F_Node
))
1915 Decl
:= Last
(Actions
(F_Node
));
1921 -- AI12-0279: If the Yield aspect is specified for a dispatching
1922 -- subprogram that inherits the aspect, the specified value shall
1925 if Is_Dispatching_Operation
(Subp
)
1926 and then Is_Primitive_Wrapper
(Subp
)
1927 and then Present
(Wrapped_Entity
(Subp
))
1928 and then Comes_From_Source
(Wrapped_Entity
(Subp
))
1929 and then Present
(Overridden_Operation
(Subp
))
1930 and then Has_Yield_Aspect
(Overridden_Operation
(Subp
))
1931 /= Has_Yield_Aspect
(Wrapped_Entity
(Subp
))
1934 W_Ent
: constant Entity_Id
:= Wrapped_Entity
(Subp
);
1935 W_Decl
: constant Node_Id
:= Parent
(W_Ent
);
1939 if Present
(Aspect_Specifications
(W_Decl
)) then
1940 Asp
:= First
(Aspect_Specifications
(W_Decl
));
1941 while Present
(Asp
) loop
1942 if Chars
(Identifier
(Asp
)) = Name_Yield
then
1943 Error_Msg_Name_1
:= Name_Yield
;
1945 ("specification of inherited aspect% can only confirm "
1946 & "parent value", Asp
);
1953 Set_Has_Yield_Aspect
(Wrapped_Entity
(Subp
));
1957 -- For similarity with record extensions, in Ada 9X the language should
1958 -- have disallowed adding visible operations to a tagged type after
1959 -- deriving a private extension from it. Report a warning if this
1960 -- primitive is defined after a private extension of Tagged_Type.
1962 Warn_On_Late_Primitive_After_Private_Extension
(Tagged_Type
, Subp
);
1963 end Check_Dispatching_Operation
;
1965 ------------------------------------------
1966 -- Check_Operation_From_Incomplete_Type --
1967 ------------------------------------------
1969 procedure Check_Operation_From_Incomplete_Type
1973 Full
: constant Entity_Id
:= Full_View
(Typ
);
1974 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1975 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1976 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1978 Prev
: Elmt_Id
:= No_Elmt
;
1980 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean;
1981 -- Check that Subp has profile of an operation derived from Parent_Subp.
1982 -- Subp must have a parameter or result type that is Typ or an access
1983 -- parameter or access result type that designates Typ.
1989 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean is
1993 if Chars
(Parent_Subp
) /= Chars
(Subp
) then
1997 -- Check that the type of controlling formals is derived from the
1998 -- parent subprogram's controlling formal type (or designated type
1999 -- if the formal type is an anonymous access type).
2001 F1
:= First_Formal
(Parent_Subp
);
2002 F2
:= First_Formal
(Subp
);
2003 while Present
(F1
) and then Present
(F2
) loop
2004 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
2005 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
2007 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
2008 and then Designated_Type
(Etype
(F2
)) /= Full
2013 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
2016 elsif Etype
(F1
) = Parent_Typ
and then Etype
(F2
) /= Full
then
2024 -- Check that a controlling result type is derived from the parent
2025 -- subprogram's result type (or designated type if the result type
2026 -- is an anonymous access type).
2028 if Ekind
(Parent_Subp
) = E_Function
then
2029 if Ekind
(Subp
) /= E_Function
then
2032 elsif Ekind
(Etype
(Parent_Subp
)) = E_Anonymous_Access_Type
then
2033 if Ekind
(Etype
(Subp
)) /= E_Anonymous_Access_Type
then
2036 elsif Designated_Type
(Etype
(Parent_Subp
)) = Parent_Typ
2037 and then Designated_Type
(Etype
(Subp
)) /= Full
2042 elsif Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
then
2045 elsif Etype
(Parent_Subp
) = Parent_Typ
2046 and then Etype
(Subp
) /= Full
2051 elsif Ekind
(Subp
) = E_Function
then
2055 return No
(F1
) and then No
(F2
);
2058 -- Start of processing for Check_Operation_From_Incomplete_Type
2061 -- The operation may override an inherited one, or may be a new one
2062 -- altogether. The inherited operation will have been hidden by the
2063 -- current one at the point of the type derivation, so it does not
2064 -- appear in the list of primitive operations of the type. We have to
2065 -- find the proper place of insertion in the list of primitive opera-
2066 -- tions by iterating over the list for the parent type.
2068 Op1
:= First_Elmt
(Old_Prim
);
2069 Op2
:= First_Elmt
(New_Prim
);
2070 while Present
(Op1
) and then Present
(Op2
) loop
2071 if Derives_From
(Node
(Op1
)) then
2074 -- Avoid adding it to the list of primitives if already there
2076 if Node
(Op2
) /= Subp
then
2077 Prepend_Elmt
(Subp
, New_Prim
);
2081 Insert_Elmt_After
(Subp
, Prev
);
2092 -- Operation is a new primitive
2094 Append_Elmt
(Subp
, New_Prim
);
2095 end Check_Operation_From_Incomplete_Type
;
2097 ---------------------------------------
2098 -- Check_Operation_From_Private_View --
2099 ---------------------------------------
2101 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
2102 Tagged_Type
: Entity_Id
;
2105 if Is_Dispatching_Operation
(Alias
(Subp
)) then
2106 Set_Scope
(Subp
, Current_Scope
);
2107 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
2109 -- Add Old_Subp to primitive operations if not already present
2111 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
2112 Add_Dispatching_Operation
(Tagged_Type
, Old_Subp
);
2114 -- If Old_Subp isn't already marked as dispatching then this is
2115 -- the case of an operation of an untagged private type fulfilled
2116 -- by a tagged type that overrides an inherited dispatching
2117 -- operation, so we set the necessary dispatching attributes here.
2119 if not Is_Dispatching_Operation
(Old_Subp
) then
2121 -- If the untagged type has no discriminants, and the full
2122 -- view is constrained, there will be a spurious mismatch of
2123 -- subtypes on the controlling arguments, because the tagged
2124 -- type is the internal base type introduced in the derivation.
2125 -- Use the original type to verify conformance, rather than the
2128 if not Comes_From_Source
(Tagged_Type
)
2129 and then Has_Discriminants
(Tagged_Type
)
2135 Formal
:= First_Formal
(Old_Subp
);
2136 while Present
(Formal
) loop
2137 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
2138 Tagged_Type
:= Etype
(Formal
);
2141 Next_Formal
(Formal
);
2145 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
2146 Tagged_Type
:= Etype
(Old_Subp
);
2150 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
2151 Set_Is_Dispatching_Operation
(Old_Subp
, True);
2152 Set_DT_Position_Value
(Old_Subp
, No_Uint
);
2155 -- If the old subprogram is an explicit renaming of some other
2156 -- entity, it is not overridden by the inherited subprogram.
2157 -- Otherwise, update its alias and other attributes.
2159 if Present
(Alias
(Old_Subp
))
2160 and then Nkind
(Unit_Declaration_Node
(Old_Subp
)) /=
2161 N_Subprogram_Renaming_Declaration
2163 Set_Alias
(Old_Subp
, Alias
(Subp
));
2165 -- The derived subprogram should inherit the abstractness of
2166 -- the parent subprogram (except in the case of a function
2167 -- returning the type). This sets the abstractness properly
2168 -- for cases where a private extension may have inherited an
2169 -- abstract operation, but the full type is derived from a
2170 -- descendant type and inherits a nonabstract version.
2172 if Etype
(Subp
) /= Tagged_Type
then
2173 Set_Is_Abstract_Subprogram
2174 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
2179 end Check_Operation_From_Private_View
;
2181 --------------------------
2182 -- Find_Controlling_Arg --
2183 --------------------------
2185 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
2186 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
2190 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
2191 return Find_Controlling_Arg
(Expression
(Orig_Node
));
2194 -- Dispatching on result case. If expansion is disabled, the node still
2195 -- has the structure of a function call. However, if the function name
2196 -- is an operator and the call was given in infix form, the original
2197 -- node has no controlling result and we must examine the current node.
2199 if Nkind
(N
) = N_Function_Call
2200 and then Present
(Controlling_Argument
(N
))
2201 and then Has_Controlling_Result
(Entity
(Name
(N
)))
2203 return Controlling_Argument
(N
);
2205 -- If expansion is enabled, the call may have been transformed into
2206 -- an indirect call, and we need to recover the original node.
2208 elsif Nkind
(Orig_Node
) = N_Function_Call
2209 and then Present
(Controlling_Argument
(Orig_Node
))
2210 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
2212 return Controlling_Argument
(Orig_Node
);
2214 -- Type conversions are dynamically tagged if the target type, or its
2215 -- designated type, are classwide. An interface conversion expands into
2216 -- a dereference, so test must be performed on the original node.
2218 elsif Nkind
(Orig_Node
) = N_Type_Conversion
2219 and then Nkind
(N
) = N_Explicit_Dereference
2220 and then Is_Controlling_Actual
(N
)
2223 Target_Type
: constant Entity_Id
:=
2224 Entity
(Subtype_Mark
(Orig_Node
));
2227 if Is_Class_Wide_Type
(Target_Type
) then
2230 elsif Is_Access_Type
(Target_Type
)
2231 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
2242 elsif Is_Controlling_Actual
(N
)
2244 (Nkind
(Parent
(N
)) = N_Qualified_Expression
2245 and then Is_Controlling_Actual
(Parent
(N
)))
2249 if Is_Access_Type
(Typ
) then
2251 -- In the case of an Access attribute, use the type of the prefix,
2252 -- since in the case of an actual for an access parameter, the
2253 -- attribute's type may be of a specific designated type, even
2254 -- though the prefix type is class-wide.
2256 if Nkind
(N
) = N_Attribute_Reference
then
2257 Typ
:= Etype
(Prefix
(N
));
2259 -- An allocator is dispatching if the type of qualified expression
2260 -- is class_wide, in which case this is the controlling type.
2262 elsif Nkind
(Orig_Node
) = N_Allocator
2263 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
2265 Typ
:= Etype
(Expression
(Orig_Node
));
2267 Typ
:= Designated_Type
(Typ
);
2271 if Is_Class_Wide_Type
(Typ
)
2273 (Nkind
(Parent
(N
)) = N_Qualified_Expression
2274 and then Is_Access_Type
(Etype
(N
))
2275 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
2282 end Find_Controlling_Arg
;
2284 ---------------------------
2285 -- Find_Dispatching_Type --
2286 ---------------------------
2288 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
2289 A_Formal
: Entity_Id
;
2291 Ctrl_Type
: Entity_Id
;
2294 if Ekind
(Subp
) in E_Function | E_Procedure
2295 and then Present
(DTC_Entity
(Subp
))
2297 return Scope
(DTC_Entity
(Subp
));
2299 -- For subprograms internally generated by derivations of tagged types
2300 -- use the alias subprogram as a reference to locate the dispatching
2303 elsif not Comes_From_Source
(Subp
)
2304 and then Present
(Alias
(Subp
))
2305 and then Is_Dispatching_Operation
(Alias
(Subp
))
2307 if Ekind
(Alias
(Subp
)) = E_Function
2308 and then Has_Controlling_Result
(Alias
(Subp
))
2310 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
2313 Formal
:= First_Formal
(Subp
);
2314 A_Formal
:= First_Formal
(Alias
(Subp
));
2315 while Present
(A_Formal
) loop
2316 if Is_Controlling_Formal
(A_Formal
) then
2317 return Check_Controlling_Type
(Etype
(Formal
), Subp
);
2320 Next_Formal
(Formal
);
2321 Next_Formal
(A_Formal
);
2324 pragma Assert
(False);
2331 Formal
:= First_Formal
(Subp
);
2332 while Present
(Formal
) loop
2333 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
2335 if Present
(Ctrl_Type
) then
2339 Next_Formal
(Formal
);
2342 -- The subprogram may also be dispatching on result
2344 if Present
(Etype
(Subp
)) then
2345 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
2349 pragma Assert
(not Is_Dispatching_Operation
(Subp
));
2351 end Find_Dispatching_Type
;
2353 --------------------------------------
2354 -- Find_Hidden_Overridden_Primitive --
2355 --------------------------------------
2357 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
2359 Tag_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(S
);
2361 Orig_Prim
: Entity_Id
;
2363 Vis_List
: Elist_Id
;
2366 -- This Ada 2012 rule applies only for type extensions or private
2367 -- extensions, where the parent type is not in a parent unit, and
2368 -- where an operation is never declared but still inherited.
2371 or else not Is_Record_Type
(Tag_Typ
)
2372 or else Etype
(Tag_Typ
) = Tag_Typ
2373 or else In_Open_Scopes
(Scope
(Etype
(Tag_Typ
)))
2378 -- Collect the list of visible ancestor of the tagged type
2380 Vis_List
:= Visible_Ancestors
(Tag_Typ
);
2382 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
2383 while Present
(Elmt
) loop
2384 Prim
:= Node
(Elmt
);
2386 -- Find an inherited hidden dispatching primitive with the name of S
2387 -- and a type-conformant profile.
2389 if Present
(Alias
(Prim
))
2390 and then Is_Hidden
(Alias
(Prim
))
2391 and then Find_Dispatching_Type
(Alias
(Prim
)) /= Tag_Typ
2392 and then Primitive_Names_Match
(S
, Prim
)
2393 and then Type_Conformant
(S
, Prim
)
2396 Vis_Ancestor
: Elmt_Id
;
2400 -- The original corresponding operation of Prim must be an
2401 -- operation of a visible ancestor of the dispatching type S,
2402 -- and the original corresponding operation of S2 must be
2405 Orig_Prim
:= Original_Corresponding_Operation
(Prim
);
2407 if Orig_Prim
/= Prim
2408 and then Is_Immediately_Visible
(Orig_Prim
)
2410 Vis_Ancestor
:= First_Elmt
(Vis_List
);
2411 while Present
(Vis_Ancestor
) loop
2413 First_Elmt
(Primitive_Operations
(Node
(Vis_Ancestor
)));
2414 while Present
(Elmt
) loop
2415 if Node
(Elmt
) = Orig_Prim
then
2416 Set_Overridden_Operation
(S
, Prim
);
2417 Set_Is_Ada_2022_Only
(S
,
2418 Is_Ada_2022_Only
(Prim
));
2419 Set_Alias
(Prim
, Orig_Prim
);
2426 Next_Elmt
(Vis_Ancestor
);
2436 end Find_Hidden_Overridden_Primitive
;
2438 ---------------------------------------
2439 -- Find_Primitive_Covering_Interface --
2440 ---------------------------------------
2442 function Find_Primitive_Covering_Interface
2443 (Tagged_Type
: Entity_Id
;
2444 Iface_Prim
: Entity_Id
) return Entity_Id
2450 pragma Assert
(Is_Interface
(Find_Dispatching_Type
(Iface_Prim
))
2451 or else (Present
(Alias
(Iface_Prim
))
2454 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
2456 -- Search in the homonym chain. Done to speed up locating visible
2457 -- entities and required to catch primitives associated with the partial
2458 -- view of private types when processing the corresponding full view.
2460 E
:= Current_Entity
(Iface_Prim
);
2461 while Present
(E
) loop
2462 if Is_Subprogram
(E
)
2463 and then Is_Dispatching_Operation
(E
)
2464 and then Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
)
2472 -- Search in the list of primitives of the type. Required to locate
2473 -- the covering primitive if the covering primitive is not visible
2474 -- (for example, non-visible inherited primitive of private type).
2476 El
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2477 while Present
(El
) loop
2480 -- Keep separate the management of internal entities that link
2481 -- primitives with interface primitives from tagged type primitives.
2483 if No
(Interface_Alias
(E
)) then
2484 if Present
(Alias
(E
)) then
2486 -- This interface primitive has not been covered yet
2488 if Alias
(E
) = Iface_Prim
then
2491 -- The covering primitive was inherited
2493 elsif Overridden_Operation
(Ultimate_Alias
(E
))
2500 -- Check if E covers the interface primitive (includes case in
2501 -- which E is an inherited private primitive).
2503 if Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
) then
2507 -- Use the internal entity that links the interface primitive with
2508 -- the covering primitive to locate the entity.
2510 elsif Interface_Alias
(E
) = Iface_Prim
then
2520 end Find_Primitive_Covering_Interface
;
2522 ---------------------------
2523 -- Inheritance_Utilities --
2524 ---------------------------
2526 package body Inheritance_Utilities
is
2528 ---------------------------
2529 -- Inherited_Subprograms --
2530 ---------------------------
2532 function Inherited_Subprograms
2534 No_Interfaces
: Boolean := False;
2535 Interfaces_Only
: Boolean := False;
2536 One_Only
: Boolean := False) return Subprogram_List
2538 Result
: Subprogram_List
(1 .. 6000);
2539 -- 6000 here is intended to be infinity. We could use an expandable
2540 -- table, but it would be awfully heavy, and there is no way that we
2541 -- could reasonably exceed this value.
2544 -- Number of entries in Result
2546 Parent_Op
: Entity_Id
;
2547 -- Traverses the Overridden_Operation chain
2549 procedure Store_IS
(E
: Entity_Id
);
2550 -- Stores E in Result if not already stored
2556 procedure Store_IS
(E
: Entity_Id
) is
2558 for J
in 1 .. N
loop
2559 if E
= Result
(J
) then
2568 -- Start of processing for Inherited_Subprograms
2571 pragma Assert
(not (No_Interfaces
and Interfaces_Only
));
2573 -- When used from backends, visibility can be handled differently
2574 -- resulting in no dispatching type being found.
2577 and then Is_Dispatching_Operation
(S
)
2578 and then Present
(Find_DT
(S
))
2580 -- Deal with direct inheritance
2582 if not Interfaces_Only
then
2585 Parent_Op
:= Overridden_Operation
(Parent_Op
);
2586 exit when No
(Parent_Op
)
2587 or else (No_Interfaces
2588 and then Is_Interface
(Find_DT
(Parent_Op
)));
2590 if Is_Subprogram_Or_Generic_Subprogram
(Parent_Op
) then
2591 Store_IS
(Parent_Op
);
2600 -- Now deal with interfaces
2602 if not No_Interfaces
then
2604 Tag_Typ
: Entity_Id
;
2609 Tag_Typ
:= Find_DT
(S
);
2611 -- In the presence of limited views there may be no visible
2612 -- dispatching type. Primitives will be inherited when non-
2613 -- limited view is frozen.
2615 if No
(Tag_Typ
) then
2616 return Result
(1 .. 0);
2618 -- Prevent cascaded errors
2620 elsif Is_Concurrent_Type
(Tag_Typ
)
2621 and then No
(Corresponding_Record_Type
(Tag_Typ
))
2622 and then Serious_Errors_Detected
> 0
2624 return Result
(1 .. 0);
2627 if Is_Concurrent_Type
(Tag_Typ
) then
2628 Tag_Typ
:= Corresponding_Record_Type
(Tag_Typ
);
2631 if Present
(Tag_Typ
)
2632 and then Is_Private_Type
(Tag_Typ
)
2633 and then Present
(Full_View
(Tag_Typ
))
2635 Tag_Typ
:= Full_View
(Tag_Typ
);
2638 -- Search primitive operations of dispatching type
2640 if Present
(Tag_Typ
)
2641 and then Present
(Primitive_Operations
(Tag_Typ
))
2643 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
2644 while Present
(Elmt
) loop
2645 Prim
:= Node
(Elmt
);
2647 -- The following test eliminates some odd cases in
2648 -- which Ekind (Prim) is Void, to be investigated
2651 if not Is_Subprogram_Or_Generic_Subprogram
(Prim
) then
2654 -- For [generic] subprogram, look at interface
2657 elsif Present
(Interface_Alias
(Prim
))
2658 and then Alias
(Prim
) = S
2660 -- We have found a primitive covered by S
2662 Store_IS
(Interface_Alias
(Prim
));
2678 return Result
(1 .. N
);
2679 end Inherited_Subprograms
;
2681 ------------------------------
2682 -- Is_Overriding_Subprogram --
2683 ------------------------------
2685 function Is_Overriding_Subprogram
(E
: Entity_Id
) return Boolean is
2686 Inherited
: constant Subprogram_List
:=
2687 Inherited_Subprograms
(E
, One_Only
=> True);
2689 return Inherited
'Length > 0;
2690 end Is_Overriding_Subprogram
;
2691 end Inheritance_Utilities
;
2693 --------------------------------
2694 -- Inheritance_Utilities_Inst --
2695 --------------------------------
2697 package Inheritance_Utilities_Inst
is new
2698 Inheritance_Utilities
(Find_Dispatching_Type
);
2700 ---------------------------
2701 -- Inherited_Subprograms --
2702 ---------------------------
2704 function Inherited_Subprograms
2706 No_Interfaces
: Boolean := False;
2707 Interfaces_Only
: Boolean := False;
2708 One_Only
: Boolean := False) return Subprogram_List
renames
2709 Inheritance_Utilities_Inst
.Inherited_Subprograms
;
2711 ---------------------------
2712 -- Is_Dynamically_Tagged --
2713 ---------------------------
2715 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
2717 if Nkind
(N
) = N_Error
then
2720 elsif Present
(Find_Controlling_Arg
(N
)) then
2723 -- Special cases: entities, and calls that dispatch on result
2725 elsif Is_Entity_Name
(N
) then
2726 return Is_Class_Wide_Type
(Etype
(N
));
2728 elsif Nkind
(N
) = N_Function_Call
2729 and then Is_Class_Wide_Type
(Etype
(N
))
2733 -- Otherwise check whether call has controlling argument
2738 end Is_Dynamically_Tagged
;
2740 ---------------------------------
2741 -- Is_Null_Interface_Primitive --
2742 ---------------------------------
2744 function Is_Null_Interface_Primitive
(E
: Entity_Id
) return Boolean is
2746 return Comes_From_Source
(E
)
2747 and then Is_Dispatching_Operation
(E
)
2748 and then Ekind
(E
) = E_Procedure
2749 and then Null_Present
(Parent
(E
))
2750 and then Is_Interface
(Find_Dispatching_Type
(E
));
2751 end Is_Null_Interface_Primitive
;
2753 -----------------------------------
2754 -- Is_Inherited_Public_Operation --
2755 -----------------------------------
2757 function Is_Inherited_Public_Operation
(Op
: Entity_Id
) return Boolean is
2758 Pack_Decl
: Node_Id
;
2759 Prim
: Entity_Id
:= Op
;
2760 Scop
: Entity_Id
:= Prim
;
2763 -- Locate the ultimate non-hidden alias entity
2765 while Present
(Alias
(Prim
)) and then not Is_Hidden
(Alias
(Prim
)) loop
2766 pragma Assert
(Alias
(Prim
) /= Prim
);
2767 Prim
:= Alias
(Prim
);
2768 Scop
:= Scope
(Prim
);
2771 if Comes_From_Source
(Prim
) and then Ekind
(Scop
) = E_Package
then
2772 Pack_Decl
:= Unit_Declaration_Node
(Scop
);
2775 Nkind
(Pack_Decl
) = N_Package_Declaration
2776 and then List_Containing
(Unit_Declaration_Node
(Prim
)) =
2777 Visible_Declarations
(Specification
(Pack_Decl
));
2782 end Is_Inherited_Public_Operation
;
2784 ------------------------------
2785 -- Is_Overriding_Subprogram --
2786 ------------------------------
2788 function Is_Overriding_Subprogram
(E
: Entity_Id
) return Boolean renames
2789 Inheritance_Utilities_Inst
.Is_Overriding_Subprogram
;
2791 --------------------------
2792 -- Is_Tag_Indeterminate --
2793 --------------------------
2795 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
2798 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
2801 if Nkind
(Orig_Node
) = N_Function_Call
2802 and then Is_Entity_Name
(Name
(Orig_Node
))
2804 Nam
:= Entity
(Name
(Orig_Node
));
2806 if not Has_Controlling_Result
(Nam
) then
2809 -- The function may have a controlling result, but if the return type
2810 -- is not visibly tagged, then this is not tag-indeterminate.
2812 elsif Is_Access_Type
(Etype
(Nam
))
2813 and then not Is_Tagged_Type
(Designated_Type
(Etype
(Nam
)))
2817 -- An explicit dereference means that the call has already been
2818 -- expanded and there is no tag to propagate.
2820 elsif Nkind
(N
) = N_Explicit_Dereference
then
2823 -- If there are no actuals, the call is tag-indeterminate
2825 elsif No
(Parameter_Associations
(Orig_Node
)) then
2829 Actual
:= First_Actual
(Orig_Node
);
2830 while Present
(Actual
) loop
2831 if Is_Controlling_Actual
(Actual
)
2832 and then not Is_Tag_Indeterminate
(Actual
)
2834 -- One operand is dispatching
2839 Next_Actual
(Actual
);
2845 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
2846 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
2848 -- Case of a call to the Input attribute (possibly rewritten), which is
2849 -- always tag-indeterminate except when its prefix is a Class attribute.
2851 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
2853 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
2854 and then Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
2858 -- In Ada 2005, a function that returns an anonymous access type can be
2859 -- dispatching, and the dereference of a call to such a function can
2860 -- also be tag-indeterminate if the call itself is.
2862 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
2863 and then Ada_Version
>= Ada_2005
2865 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
2870 end Is_Tag_Indeterminate
;
2872 ------------------------------------
2873 -- Override_Dispatching_Operation --
2874 ------------------------------------
2876 procedure Override_Dispatching_Operation
2877 (Tagged_Type
: Entity_Id
;
2878 Prev_Op
: Entity_Id
;
2885 -- If there is no previous operation to override, the type declaration
2886 -- was malformed, and an error must have been emitted already.
2888 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2889 while Present
(Elmt
) and then Node
(Elmt
) /= Prev_Op
loop
2897 -- The location of entities that come from source in the list of
2898 -- primitives of the tagged type must follow their order of occurrence
2899 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2900 -- primitive of an interface that is not implemented by the parents of
2901 -- this tagged type (that is, it is an alias of an interface primitive
2902 -- generated by Derive_Interface_Progenitors), then we must append the
2903 -- new entity at the end of the list of primitives.
2905 if Present
(Alias
(Prev_Op
))
2906 and then Etype
(Tagged_Type
) /= Tagged_Type
2907 and then Is_Interface
(Find_Dispatching_Type
(Alias
(Prev_Op
)))
2908 and then not Is_Ancestor
(Find_Dispatching_Type
(Alias
(Prev_Op
)),
2909 Tagged_Type
, Use_Full_View
=> True)
2910 and then not Implements_Interface
2911 (Etype
(Tagged_Type
),
2912 Find_Dispatching_Type
(Alias
(Prev_Op
)))
2914 Remove_Elmt
(Primitive_Operations
(Tagged_Type
), Elmt
);
2915 Add_Dispatching_Operation
(Tagged_Type
, New_Op
);
2917 -- The new primitive replaces the overridden entity. Required to ensure
2918 -- that overriding primitive is assigned the same dispatch table slot.
2921 Replace_Elmt
(Elmt
, New_Op
);
2924 if Ada_Version
>= Ada_2005
and then Has_Interfaces
(Tagged_Type
) then
2926 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2927 -- entities of the overridden primitive to reference New_Op, and
2928 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2929 -- that the new operation is subtype conformant with the interface
2930 -- operations that it implements (for operations inherited from the
2931 -- parent itself, this check is made when building the derived type).
2933 -- Note: This code is executed with internally generated wrappers of
2934 -- functions with controlling result and late overridings.
2936 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2937 while Present
(Elmt
) loop
2938 Prim
:= Node
(Elmt
);
2940 if Prim
= New_Op
then
2943 -- Note: The check on Is_Subprogram protects the frontend against
2944 -- reading attributes in entities that are not yet fully decorated
2946 elsif Is_Subprogram
(Prim
)
2947 and then Present
(Interface_Alias
(Prim
))
2948 and then Alias
(Prim
) = Prev_Op
2950 Set_Alias
(Prim
, New_Op
);
2952 -- No further decoration needed yet for internally generated
2953 -- wrappers of controlling functions since (at this stage)
2954 -- they are not yet decorated.
2956 if not Is_Wrapper
(New_Op
) then
2957 Check_Subtype_Conformant
(New_Op
, Prim
);
2959 Set_Is_Abstract_Subprogram
(Prim
,
2960 Is_Abstract_Subprogram
(New_Op
));
2962 -- Ensure that this entity will be expanded to fill the
2963 -- corresponding entry in its dispatch table.
2965 if not Is_Abstract_Subprogram
(Prim
) then
2966 Set_Has_Delayed_Freeze
(Prim
);
2975 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
2976 or else not In_Private_Part
(Current_Scope
)
2978 -- Not a private primitive
2982 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
2984 -- Make the overriding operation into an alias of the implicit one.
2985 -- In this fashion a call from outside ends up calling the new body
2986 -- even if non-dispatching, and a call from inside calls the over-
2987 -- riding operation because it hides the implicit one. To indicate
2988 -- that the body of Prev_Op is never called, set its dispatch table
2989 -- entity to Empty. If the overridden operation has a dispatching
2990 -- result, so does the overriding one.
2992 Set_Alias
(Prev_Op
, New_Op
);
2993 Set_DTC_Entity
(Prev_Op
, Empty
);
2994 Set_Has_Controlling_Result
(New_Op
, Has_Controlling_Result
(Prev_Op
));
2995 Set_Is_Ada_2022_Only
(New_Op
, Is_Ada_2022_Only
(Prev_Op
));
2997 end Override_Dispatching_Operation
;
3003 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
3004 Call_Node
: Node_Id
;
3008 if Nkind
(Actual
) = N_Function_Call
then
3009 Call_Node
:= Actual
;
3011 elsif Nkind
(Actual
) = N_Identifier
3012 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
3014 -- Call rewritten as object declaration when stack-checking is
3015 -- enabled. Propagate tag to expression in declaration, which is
3018 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
3020 -- Ada 2005: If this is a dereference of a call to a function with a
3021 -- dispatching access-result, the tag is propagated when the dereference
3022 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
3024 elsif Nkind
(Actual
) = N_Explicit_Dereference
3025 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
3029 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
3030 -- and in that case we can simply return.
3032 elsif Nkind
(Actual
) = N_Attribute_Reference
then
3033 pragma Assert
(Attribute_Name
(Actual
) = Name_Input
);
3037 -- Only other possibilities are parenthesized or qualified expression,
3038 -- or an expander-generated unchecked conversion of a function call to
3039 -- a stream Input attribute.
3042 Call_Node
:= Expression
(Actual
);
3045 -- No action needed if the call has been already expanded
3047 if Is_Expanded_Dispatching_Call
(Call_Node
) then
3051 -- Do not set the Controlling_Argument if already set. This happens in
3052 -- the special case of _Input (see Exp_Attr, case Input).
3054 if No
(Controlling_Argument
(Call_Node
)) then
3055 Set_Controlling_Argument
(Call_Node
, Control
);
3058 Arg
:= First_Actual
(Call_Node
);
3059 while Present
(Arg
) loop
3060 if Is_Tag_Indeterminate
(Arg
) then
3061 Propagate_Tag
(Control
, Arg
);
3067 -- Add class-wide precondition check if the target of this dispatching
3068 -- call has or inherits class-wide preconditions.
3070 Install_Class_Preconditions_Check
(Call_Node
);
3072 -- Expansion of dispatching calls is suppressed on VM targets, because
3073 -- the VM back-ends directly handle the generation of dispatching calls
3074 -- and would have to undo any expansion to an indirect call.
3076 if Tagged_Type_Expansion
then
3078 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
3081 Expand_Dispatching_Call
(Call_Node
);
3083 -- If the controlling argument is an interface type and the type
3084 -- of Call_Node differs then we must add an implicit conversion to
3085 -- force displacement of the pointer to the object to reference
3086 -- the secondary dispatch table of the interface.
3088 if Is_Interface
(Etype
(Control
))
3089 and then Etype
(Control
) /= Call_Typ
3091 -- Cannot use Convert_To because the previous call to
3092 -- Expand_Dispatching_Call leaves decorated the Call_Node
3093 -- with the type of Control.
3096 Make_Type_Conversion
(Sloc
(Call_Node
),
3098 New_Occurrence_Of
(Etype
(Control
), Sloc
(Call_Node
)),
3099 Expression
=> Relocate_Node
(Call_Node
)));
3100 Set_Etype
(Call_Node
, Etype
(Control
));
3101 Set_Analyzed
(Call_Node
);
3103 Expand_Interface_Conversion
(Call_Node
);
3107 -- Expansion of a dispatching call results in an indirect call, which in
3108 -- turn causes current values to be killed (see Resolve_Call), so on VM
3109 -- targets we do the call here to ensure consistent warnings between VM
3110 -- and non-VM targets.
3113 Kill_Current_Values
;