1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Elists
; use Elists
;
29 with Einfo
; use Einfo
;
30 with Exp_Disp
; use Exp_Disp
;
31 with Exp_Util
; use Exp_Util
;
32 with Exp_Ch7
; use Exp_Ch7
;
33 with Exp_Tss
; use Exp_Tss
;
34 with Errout
; use Errout
;
35 with Lib
.Xref
; use Lib
.Xref
;
36 with Namet
; use Namet
;
37 with Nlists
; use Nlists
;
38 with Nmake
; use Nmake
;
40 with Output
; use Output
;
41 with Restrict
; use Restrict
;
42 with Rident
; use Rident
;
44 with Sem_Aux
; use Sem_Aux
;
45 with Sem_Ch3
; use Sem_Ch3
;
46 with Sem_Ch6
; use Sem_Ch6
;
47 with Sem_Ch8
; use Sem_Ch8
;
48 with Sem_Eval
; use Sem_Eval
;
49 with Sem_Type
; use Sem_Type
;
50 with Sem_Util
; use Sem_Util
;
51 with Snames
; use Snames
;
52 with Sinfo
; use Sinfo
;
53 with Tbuild
; use Tbuild
;
54 with Uintp
; use Uintp
;
56 package body Sem_Disp
is
58 -----------------------
59 -- Local Subprograms --
60 -----------------------
62 procedure Add_Dispatching_Operation
63 (Tagged_Type
: Entity_Id
;
65 -- Add New_Op in the list of primitive operations of Tagged_Type
67 function Check_Controlling_Type
69 Subp
: Entity_Id
) return Entity_Id
;
70 -- T is the tagged type of a formal parameter or the result of Subp.
71 -- If the subprogram has a controlling parameter or result that matches
72 -- the type, then returns the tagged type of that parameter or result
73 -- (returning the designated tagged type in the case of an access
74 -- parameter); otherwise returns empty.
76 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
;
77 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
78 -- type of S that has the same name of S, a type-conformant profile, an
79 -- original corresponding operation O that is a primitive of a visible
80 -- ancestor of the dispatching type of S and O is visible at the point of
81 -- of declaration of S. If the entity is found the Alias of S is set to the
82 -- original corresponding operation S and its Overridden_Operation is set
83 -- to the found entity; otherwise return Empty.
85 -- This routine does not search for non-hidden primitives since they are
86 -- covered by the normal Ada 2005 rules.
88 function Is_Inherited_Public_Operation
(Op
: Entity_Id
) return Boolean;
89 -- Check whether a primitive operation is inherited from an operation
90 -- declared in the visible part of its package.
92 -------------------------------
93 -- Add_Dispatching_Operation --
94 -------------------------------
96 procedure Add_Dispatching_Operation
97 (Tagged_Type
: Entity_Id
;
100 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
103 -- The dispatching operation may already be on the list, if it is the
104 -- wrapper for an inherited function of a null extension (see Exp_Ch3
105 -- for the construction of function wrappers). The list of primitive
106 -- operations must not contain duplicates.
108 Append_Unique_Elmt
(New_Op
, List
);
109 end Add_Dispatching_Operation
;
111 ---------------------------
112 -- Covers_Some_Interface --
113 ---------------------------
115 function Covers_Some_Interface
(Prim
: Entity_Id
) return Boolean is
116 Tagged_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
121 pragma Assert
(Is_Dispatching_Operation
(Prim
));
123 -- Although this is a dispatching primitive we must check if its
124 -- dispatching type is available because it may be the primitive
125 -- of a private type not defined as tagged in its partial view.
127 if Present
(Tagged_Type
) and then Has_Interfaces
(Tagged_Type
) then
129 -- If the tagged type is frozen then the internal entities associated
130 -- with interfaces are available in the list of primitives of the
131 -- tagged type and can be used to speed up this search.
133 if Is_Frozen
(Tagged_Type
) then
134 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
135 while Present
(Elmt
) loop
138 if Present
(Interface_Alias
(E
))
139 and then Alias
(E
) = Prim
147 -- Otherwise we must collect all the interface primitives and check
148 -- if the Prim will override some interface primitive.
152 Ifaces_List
: Elist_Id
;
153 Iface_Elmt
: Elmt_Id
;
155 Iface_Prim
: Entity_Id
;
158 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
159 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
160 while Present
(Iface_Elmt
) loop
161 Iface
:= Node
(Iface_Elmt
);
163 Elmt
:= First_Elmt
(Primitive_Operations
(Iface
));
164 while Present
(Elmt
) loop
165 Iface_Prim
:= Node
(Elmt
);
167 if Chars
(Iface
) = Chars
(Prim
)
168 and then Is_Interface_Conformant
169 (Tagged_Type
, Iface_Prim
, Prim
)
177 Next_Elmt
(Iface_Elmt
);
184 end Covers_Some_Interface
;
186 -------------------------------
187 -- Check_Controlling_Formals --
188 -------------------------------
190 procedure Check_Controlling_Formals
195 Ctrl_Type
: Entity_Id
;
198 Formal
:= First_Formal
(Subp
);
199 while Present
(Formal
) loop
200 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
202 if Present
(Ctrl_Type
) then
204 -- When controlling type is concurrent and declared within a
205 -- generic or inside an instance use corresponding record type.
207 if Is_Concurrent_Type
(Ctrl_Type
)
208 and then Present
(Corresponding_Record_Type
(Ctrl_Type
))
210 Ctrl_Type
:= Corresponding_Record_Type
(Ctrl_Type
);
213 if Ctrl_Type
= Typ
then
214 Set_Is_Controlling_Formal
(Formal
);
216 -- Ada 2005 (AI-231): Anonymous access types that are used in
217 -- controlling parameters exclude null because it is necessary
218 -- to read the tag to dispatch, and null has no tag.
220 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
221 Set_Can_Never_Be_Null
(Etype
(Formal
));
222 Set_Is_Known_Non_Null
(Etype
(Formal
));
225 -- Check that the parameter's nominal subtype statically
226 -- matches the first subtype.
228 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
229 if not Subtypes_Statically_Match
230 (Typ
, Designated_Type
(Etype
(Formal
)))
233 ("parameter subtype does not match controlling type",
237 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
)) then
239 ("parameter subtype does not match controlling type",
243 if Present
(Default_Value
(Formal
)) then
245 -- In Ada 2005, access parameters can have defaults
247 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
248 and then Ada_Version
< Ada_2005
251 ("default not allowed for controlling access parameter",
252 Default_Value
(Formal
));
254 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
256 ("default expression must be a tag indeterminate" &
257 " function call", Default_Value
(Formal
));
261 elsif Comes_From_Source
(Subp
) then
263 ("operation can be dispatching in only one type", Subp
);
267 Next_Formal
(Formal
);
270 if Ekind_In
(Subp
, E_Function
, E_Generic_Function
) then
271 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
273 if Present
(Ctrl_Type
) then
274 if Ctrl_Type
= Typ
then
275 Set_Has_Controlling_Result
(Subp
);
277 -- Check that result subtype statically matches first subtype
278 -- (Ada 2005): Subp may have a controlling access result.
280 if Subtypes_Statically_Match
(Typ
, Etype
(Subp
))
281 or else (Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
283 Subtypes_Statically_Match
284 (Typ
, Designated_Type
(Etype
(Subp
))))
290 ("result subtype does not match controlling type", Subp
);
293 elsif Comes_From_Source
(Subp
) then
295 ("operation can be dispatching in only one type", Subp
);
299 end Check_Controlling_Formals
;
301 ----------------------------
302 -- Check_Controlling_Type --
303 ----------------------------
305 function Check_Controlling_Type
307 Subp
: Entity_Id
) return Entity_Id
309 Tagged_Type
: Entity_Id
:= Empty
;
312 if Is_Tagged_Type
(T
) then
313 if Is_First_Subtype
(T
) then
316 Tagged_Type
:= Base_Type
(T
);
319 -- If the type is incomplete, it may have been declared without a
320 -- Tagged indication, but the full view may be tagged, in which case
321 -- that is the controlling type of the subprogram. This is one of the
322 -- approx. 579 places in the language where a lookahead would help.
324 elsif Ekind
(T
) = E_Incomplete_Type
325 and then Present
(Full_View
(T
))
326 and then Is_Tagged_Type
(Full_View
(T
))
328 Set_Is_Tagged_Type
(T
);
329 Tagged_Type
:= Full_View
(T
);
331 elsif Ekind
(T
) = E_Anonymous_Access_Type
332 and then Is_Tagged_Type
(Designated_Type
(T
))
334 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
335 if Is_First_Subtype
(Designated_Type
(T
)) then
336 Tagged_Type
:= Designated_Type
(T
);
338 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
341 -- Ada 2005: an incomplete type can be tagged. An operation with an
342 -- access parameter of the type is dispatching.
344 elsif Scope
(Designated_Type
(T
)) = Current_Scope
then
345 Tagged_Type
:= Designated_Type
(T
);
347 -- Ada 2005 (AI-50217)
349 elsif From_Limited_With
(Designated_Type
(T
))
350 and then Has_Non_Limited_View
(Designated_Type
(T
))
351 and then Scope
(Designated_Type
(T
)) = Scope
(Subp
)
353 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
354 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
356 Tagged_Type
:= Base_Type
(Non_Limited_View
357 (Designated_Type
(T
)));
362 if No
(Tagged_Type
) or else Is_Class_Wide_Type
(Tagged_Type
) then
365 -- The dispatching type and the primitive operation must be defined in
366 -- the same scope, except in the case of internal operations and formal
367 -- abstract subprograms.
369 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
370 and then (not Is_Generic_Type
(Tagged_Type
)
371 or else not Comes_From_Source
(Subp
)))
373 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
375 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
377 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
379 Is_Abstract_Subprogram
(Subp
))
386 end Check_Controlling_Type
;
388 ----------------------------
389 -- Check_Dispatching_Call --
390 ----------------------------
392 procedure Check_Dispatching_Call
(N
: Node_Id
) is
393 Loc
: constant Source_Ptr
:= Sloc
(N
);
396 Control
: Node_Id
:= Empty
;
398 Subp_Entity
: Entity_Id
;
399 Indeterm_Ancestor_Call
: Boolean := False;
400 Indeterm_Ctrl_Type
: Entity_Id
;
402 Static_Tag
: Node_Id
:= Empty
;
403 -- If a controlling formal has a statically tagged actual, the tag of
404 -- this actual is to be used for any tag-indeterminate actual.
406 procedure Check_Direct_Call
;
407 -- In the case when the controlling actual is a class-wide type whose
408 -- root type's completion is a task or protected type, the call is in
409 -- fact direct. This routine detects the above case and modifies the
412 procedure Check_Dispatching_Context
(Call
: Node_Id
);
413 -- If the call is tag-indeterminate and the entity being called is
414 -- abstract, verify that the context is a call that will eventually
415 -- provide a tag for dispatching, or has provided one already.
417 -----------------------
418 -- Check_Direct_Call --
419 -----------------------
421 procedure Check_Direct_Call
is
422 Typ
: Entity_Id
:= Etype
(Control
);
424 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean;
425 -- Determine whether an entity denotes a user-defined equality
427 ------------------------------
428 -- Is_User_Defined_Equality --
429 ------------------------------
431 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean is
434 Ekind
(Id
) = E_Function
435 and then Chars
(Id
) = Name_Op_Eq
436 and then Comes_From_Source
(Id
)
438 -- Internally generated equalities have a full type declaration
441 and then Nkind
(Parent
(Id
)) = N_Function_Specification
;
442 end Is_User_Defined_Equality
;
444 -- Start of processing for Check_Direct_Call
447 -- Predefined primitives do not receive wrappers since they are built
448 -- from scratch for the corresponding record of synchronized types.
449 -- Equality is in general predefined, but is excluded from the check
450 -- when it is user-defined.
452 if Is_Predefined_Dispatching_Operation
(Subp_Entity
)
453 and then not Is_User_Defined_Equality
(Subp_Entity
)
458 if Is_Class_Wide_Type
(Typ
) then
459 Typ
:= Root_Type
(Typ
);
462 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
463 Typ
:= Full_View
(Typ
);
466 if Is_Concurrent_Type
(Typ
)
468 Present
(Corresponding_Record_Type
(Typ
))
470 Typ
:= Corresponding_Record_Type
(Typ
);
472 -- The concurrent record's list of primitives should contain a
473 -- wrapper for the entity of the call, retrieve it.
478 Wrapper_Found
: Boolean := False;
481 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
482 while Present
(Prim_Elmt
) loop
483 Prim
:= Node
(Prim_Elmt
);
485 if Is_Primitive_Wrapper
(Prim
)
486 and then Wrapped_Entity
(Prim
) = Subp_Entity
488 Wrapper_Found
:= True;
492 Next_Elmt
(Prim_Elmt
);
495 -- A primitive declared between two views should have a
496 -- corresponding wrapper.
498 pragma Assert
(Wrapper_Found
);
500 -- Modify the call by setting the proper entity
502 Set_Entity
(Name
(N
), Prim
);
505 end Check_Direct_Call
;
507 -------------------------------
508 -- Check_Dispatching_Context --
509 -------------------------------
511 procedure Check_Dispatching_Context
(Call
: Node_Id
) is
512 Subp
: constant Entity_Id
:= Entity
(Name
(Call
));
514 procedure Abstract_Context_Error
;
515 -- Error for abstract call dispatching on result is not dispatching
517 ----------------------------
518 -- Abstract_Context_Error --
519 ----------------------------
521 procedure Abstract_Context_Error
is
523 if Ekind
(Subp
) = E_Function
then
525 ("call to abstract function must be dispatching", N
);
527 -- This error can occur for a procedure in the case of a call to
528 -- an abstract formal procedure with a statically tagged operand.
532 ("call to abstract procedure must be dispatching", N
);
534 end Abstract_Context_Error
;
538 Scop
: constant Entity_Id
:= Current_Scope_No_Loops
;
539 Typ
: constant Entity_Id
:= Etype
(Subp
);
542 -- Start of processing for Check_Dispatching_Context
545 if Is_Abstract_Subprogram
(Subp
)
546 and then No
(Controlling_Argument
(Call
))
548 if Present
(Alias
(Subp
))
549 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
550 and then No
(DTC_Entity
(Subp
))
552 -- Private overriding of inherited abstract operation, call is
555 Set_Entity
(Name
(N
), Alias
(Subp
));
558 -- An obscure special case: a null procedure may have a class-
559 -- wide pre/postcondition that includes a call to an abstract
560 -- subp. Calls within the expression may not have been rewritten
561 -- as dispatching calls yet, because the null body appears in
562 -- the current declarative part. The expression will be properly
563 -- rewritten/reanalyzed when the postcondition procedure is built.
565 -- Similarly, if this is a pre/postcondition for an abstract
566 -- subprogram, it may call another abstract function which is
567 -- a primitive of an abstract type. The call is non-dispatching
568 -- but will be legal in overridings of the operation.
570 elsif In_Spec_Expression
572 (Is_Subprogram
(Scop
)
573 or else Chars
(Scop
) = Name_Postcondition
)
575 (Is_Abstract_Subprogram
(Scop
)
577 (Nkind
(Parent
(Scop
)) = N_Procedure_Specification
578 and then Null_Present
(Parent
(Scop
))))
582 elsif Ekind
(Current_Scope
) = E_Function
583 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
584 N_Generic_Subprogram_Declaration
589 -- We need to determine whether the context of the call
590 -- provides a tag to make the call dispatching. This requires
591 -- the call to be the actual in an enclosing call, and that
592 -- actual must be controlling. If the call is an operand of
593 -- equality, the other operand must not ve abstract.
595 if not Is_Tagged_Type
(Typ
)
597 (Ekind
(Typ
) = E_Anonymous_Access_Type
598 and then Is_Tagged_Type
(Designated_Type
(Typ
)))
600 Abstract_Context_Error
;
604 Par
:= Parent
(Call
);
606 if Nkind
(Par
) = N_Parameter_Association
then
610 if Nkind
(Par
) = N_Qualified_Expression
611 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
616 if Nkind_In
(Par
, N_Function_Call
, N_Procedure_Call_Statement
)
617 and then Is_Entity_Name
(Name
(Par
))
620 Enc_Subp
: constant Entity_Id
:= Entity
(Name
(Par
));
624 Ret_Type
: Entity_Id
;
627 -- Find controlling formal that can provide tag for the
628 -- tag-indeterminate actual. The corresponding actual
629 -- must be the corresponding class-wide type.
631 F
:= First_Formal
(Enc_Subp
);
632 A
:= First_Actual
(Par
);
634 -- Find controlling type of call. Dereference if function
635 -- returns an access type.
637 Ret_Type
:= Etype
(Call
);
638 if Is_Access_Type
(Etype
(Call
)) then
639 Ret_Type
:= Designated_Type
(Ret_Type
);
642 while Present
(F
) loop
643 Control
:= Etype
(A
);
645 if Is_Access_Type
(Control
) then
646 Control
:= Designated_Type
(Control
);
649 if Is_Controlling_Formal
(F
)
650 and then not (Call
= A
or else Parent
(Call
) = A
)
651 and then Control
= Class_Wide_Type
(Ret_Type
)
660 if Nkind
(Par
) = N_Function_Call
661 and then Is_Tag_Indeterminate
(Par
)
663 -- The parent may be an actual of an enclosing call
665 Check_Dispatching_Context
(Par
);
670 ("call to abstract function must be dispatching",
676 -- For equality operators, one of the operands must be
677 -- statically or dynamically tagged.
679 elsif Nkind_In
(Par
, N_Op_Eq
, N_Op_Ne
) then
680 if N
= Right_Opnd
(Par
)
681 and then Is_Tag_Indeterminate
(Left_Opnd
(Par
))
683 Abstract_Context_Error
;
685 elsif N
= Left_Opnd
(Par
)
686 and then Is_Tag_Indeterminate
(Right_Opnd
(Par
))
688 Abstract_Context_Error
;
693 -- The left-hand side of an assignment provides the tag
695 elsif Nkind
(Par
) = N_Assignment_Statement
then
699 Abstract_Context_Error
;
703 end Check_Dispatching_Context
;
705 -- Start of processing for Check_Dispatching_Call
708 -- Find a controlling argument, if any
710 if Present
(Parameter_Associations
(N
)) then
711 Subp_Entity
:= Entity
(Name
(N
));
713 Actual
:= First_Actual
(N
);
714 Formal
:= First_Formal
(Subp_Entity
);
715 while Present
(Actual
) loop
716 Control
:= Find_Controlling_Arg
(Actual
);
717 exit when Present
(Control
);
719 -- Check for the case where the actual is a tag-indeterminate call
720 -- whose result type is different than the tagged type associated
721 -- with the containing call, but is an ancestor of the type.
723 if Is_Controlling_Formal
(Formal
)
724 and then Is_Tag_Indeterminate
(Actual
)
725 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
726 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
728 Indeterm_Ancestor_Call
:= True;
729 Indeterm_Ctrl_Type
:= Etype
(Formal
);
731 -- If the formal is controlling but the actual is not, the type
732 -- of the actual is statically known, and may be used as the
733 -- controlling tag for some other tag-indeterminate actual.
735 elsif Is_Controlling_Formal
(Formal
)
736 and then Is_Entity_Name
(Actual
)
737 and then Is_Tagged_Type
(Etype
(Actual
))
739 Static_Tag
:= Actual
;
742 Next_Actual
(Actual
);
743 Next_Formal
(Formal
);
746 -- If the call doesn't have a controlling actual but does have an
747 -- indeterminate actual that requires dispatching treatment, then an
748 -- object is needed that will serve as the controlling argument for
749 -- a dispatching call on the indeterminate actual. This can occur
750 -- in the unusual situation of a default actual given by a tag-
751 -- indeterminate call and where the type of the call is an ancestor
752 -- of the type associated with a containing call to an inherited
753 -- operation (see AI-239).
755 -- Rather than create an object of the tagged type, which would
756 -- be problematic for various reasons (default initialization,
757 -- discriminants), the tag of the containing call's associated
758 -- tagged type is directly used to control the dispatching.
761 and then Indeterm_Ancestor_Call
762 and then No
(Static_Tag
)
765 Make_Attribute_Reference
(Loc
,
766 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
767 Attribute_Name
=> Name_Tag
);
772 if Present
(Control
) then
774 -- Verify that no controlling arguments are statically tagged
777 Write_Str
("Found Dispatching call");
782 Actual
:= First_Actual
(N
);
783 while Present
(Actual
) loop
784 if Actual
/= Control
then
786 if not Is_Controlling_Actual
(Actual
) then
787 null; -- Can be anything
789 elsif Is_Dynamically_Tagged
(Actual
) then
790 null; -- Valid parameter
792 elsif Is_Tag_Indeterminate
(Actual
) then
794 -- The tag is inherited from the enclosing call (the node
795 -- we are currently analyzing). Explicitly expand the
796 -- actual, since the previous call to Expand (from
797 -- Resolve_Call) had no way of knowing about the
798 -- required dispatching.
800 Propagate_Tag
(Control
, Actual
);
804 ("controlling argument is not dynamically tagged",
810 Next_Actual
(Actual
);
813 -- Mark call as a dispatching call
815 Set_Controlling_Argument
(N
, Control
);
816 Check_Restriction
(No_Dispatching_Calls
, N
);
818 -- The dispatching call may need to be converted into a direct
819 -- call in certain cases.
823 -- If there is a statically tagged actual and a tag-indeterminate
824 -- call to a function of the ancestor (such as that provided by a
825 -- default), then treat this as a dispatching call and propagate
826 -- the tag to the tag-indeterminate call(s).
828 elsif Present
(Static_Tag
) and then Indeterm_Ancestor_Call
then
830 Make_Attribute_Reference
(Loc
,
832 New_Occurrence_Of
(Etype
(Static_Tag
), Loc
),
833 Attribute_Name
=> Name_Tag
);
837 Actual
:= First_Actual
(N
);
838 Formal
:= First_Formal
(Subp_Entity
);
839 while Present
(Actual
) loop
840 if Is_Tag_Indeterminate
(Actual
)
841 and then Is_Controlling_Formal
(Formal
)
843 Propagate_Tag
(Control
, Actual
);
846 Next_Actual
(Actual
);
847 Next_Formal
(Formal
);
850 Check_Dispatching_Context
(N
);
852 elsif Nkind
(N
) /= N_Function_Call
then
854 -- The call is not dispatching, so check that there aren't any
855 -- tag-indeterminate abstract calls left among its actuals.
857 Actual
:= First_Actual
(N
);
858 while Present
(Actual
) loop
859 if Is_Tag_Indeterminate
(Actual
) then
861 -- Function call case
863 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
864 Func
:= Entity
(Name
(Original_Node
(Actual
)));
866 -- If the actual is an attribute then it can't be abstract
867 -- (the only current case of a tag-indeterminate attribute
868 -- is the stream Input attribute).
870 elsif Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
874 -- Ditto if it is an explicit dereference
876 elsif Nkind
(Original_Node
(Actual
)) = N_Explicit_Dereference
880 -- Only other possibility is a qualified expression whose
881 -- constituent expression is itself a call.
885 Entity
(Name
(Original_Node
886 (Expression
(Original_Node
(Actual
)))));
889 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
891 ("call to abstract function must be dispatching",
896 Next_Actual
(Actual
);
899 Check_Dispatching_Context
(N
);
902 elsif Nkind
(Parent
(N
)) in N_Subexpr
then
903 Check_Dispatching_Context
(N
);
905 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
906 and then Is_Class_Wide_Type
(Etype
(Name
(Parent
(N
))))
910 elsif Is_Abstract_Subprogram
(Subp_Entity
) then
911 Check_Dispatching_Context
(N
);
916 -- If dispatching on result, the enclosing call, if any, will
917 -- determine the controlling argument. Otherwise this is the
918 -- primitive operation of the root type.
920 Check_Dispatching_Context
(N
);
922 end Check_Dispatching_Call
;
924 ---------------------------------
925 -- Check_Dispatching_Operation --
926 ---------------------------------
928 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
929 Tagged_Type
: Entity_Id
;
930 Has_Dispatching_Parent
: Boolean := False;
931 Body_Is_Last_Primitive
: Boolean := False;
932 Ovr_Subp
: Entity_Id
:= Empty
;
935 if not Ekind_In
(Subp
, E_Procedure
, E_Function
) then
939 Set_Is_Dispatching_Operation
(Subp
, False);
940 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
942 -- Ada 2005 (AI-345): Use the corresponding record (if available).
943 -- Required because primitives of concurrent types are attached
944 -- to the corresponding record (not to the concurrent type).
946 if Ada_Version
>= Ada_2005
947 and then Present
(Tagged_Type
)
948 and then Is_Concurrent_Type
(Tagged_Type
)
949 and then Present
(Corresponding_Record_Type
(Tagged_Type
))
951 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
954 -- (AI-345): The task body procedure is not a primitive of the tagged
957 if Present
(Tagged_Type
)
958 and then Is_Concurrent_Record_Type
(Tagged_Type
)
959 and then Present
(Corresponding_Concurrent_Type
(Tagged_Type
))
960 and then Is_Task_Type
(Corresponding_Concurrent_Type
(Tagged_Type
))
961 and then Subp
= Get_Task_Body_Procedure
962 (Corresponding_Concurrent_Type
(Tagged_Type
))
967 -- If Subp is derived from a dispatching operation then it should
968 -- always be treated as dispatching. In this case various checks
969 -- below will be bypassed. Makes sure that late declarations for
970 -- inherited private subprograms are treated as dispatching, even
971 -- if the associated tagged type is already frozen.
973 Has_Dispatching_Parent
:=
974 Present
(Alias
(Subp
))
975 and then Is_Dispatching_Operation
(Alias
(Subp
));
977 if No
(Tagged_Type
) then
979 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
980 -- with an abstract interface type unless the interface acts as a
981 -- parent type in a derivation. If the interface type is a formal
982 -- type then the operation is not primitive and therefore legal.
989 E
:= First_Entity
(Subp
);
990 while Present
(E
) loop
992 -- For an access parameter, check designated type
994 if Ekind
(Etype
(E
)) = E_Anonymous_Access_Type
then
995 Typ
:= Designated_Type
(Etype
(E
));
1000 if Comes_From_Source
(Subp
)
1001 and then Is_Interface
(Typ
)
1002 and then not Is_Class_Wide_Type
(Typ
)
1003 and then not Is_Derived_Type
(Typ
)
1004 and then not Is_Generic_Type
(Typ
)
1005 and then not In_Instance
1007 Error_Msg_N
("??declaration of& is too late!", Subp
);
1008 Error_Msg_NE
-- CODEFIX??
1009 ("\??spec should appear immediately after declaration "
1010 & "of & !", Subp
, Typ
);
1017 -- In case of functions check also the result type
1019 if Ekind
(Subp
) = E_Function
then
1020 if Is_Access_Type
(Etype
(Subp
)) then
1021 Typ
:= Designated_Type
(Etype
(Subp
));
1023 Typ
:= Etype
(Subp
);
1026 -- The following should be better commented, especially since
1027 -- we just added several new conditions here ???
1029 if Comes_From_Source
(Subp
)
1030 and then Is_Interface
(Typ
)
1031 and then not Is_Class_Wide_Type
(Typ
)
1032 and then not Is_Derived_Type
(Typ
)
1033 and then not Is_Generic_Type
(Typ
)
1034 and then not In_Instance
1036 Error_Msg_N
("??declaration of& is too late!", Subp
);
1038 ("\??spec should appear immediately after declaration "
1039 & "of & !", Subp
, Typ
);
1046 -- The subprograms build internally after the freezing point (such as
1047 -- init procs, interface thunks, type support subprograms, and Offset
1048 -- to top functions for accessing interface components in variable
1049 -- size tagged types) are not primitives.
1051 elsif Is_Frozen
(Tagged_Type
)
1052 and then not Comes_From_Source
(Subp
)
1053 and then not Has_Dispatching_Parent
1055 -- Complete decoration of internally built subprograms that override
1056 -- a dispatching primitive. These entities correspond with the
1059 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1060 -- to override functions of nonabstract null extensions. These
1061 -- primitives were added to the list of primitives of the tagged
1062 -- type by Make_Controlling_Function_Wrappers. However, attribute
1063 -- Is_Dispatching_Operation must be set to true.
1065 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1068 -- 3. Subprograms associated with stream attributes (built by
1069 -- New_Stream_Subprogram)
1071 if Present
(Old_Subp
)
1072 and then Present
(Overridden_Operation
(Subp
))
1073 and then Is_Dispatching_Operation
(Old_Subp
)
1076 ((Ekind
(Subp
) = E_Function
1077 and then Is_Dispatching_Operation
(Old_Subp
)
1078 and then Is_Null_Extension
(Base_Type
(Etype
(Subp
))))
1080 (Ekind
(Subp
) = E_Procedure
1081 and then Is_Dispatching_Operation
(Old_Subp
)
1082 and then Present
(Alias
(Old_Subp
))
1083 and then Is_Null_Interface_Primitive
1084 (Ultimate_Alias
(Old_Subp
)))
1085 or else Get_TSS_Name
(Subp
) = TSS_Stream_Read
1086 or else Get_TSS_Name
(Subp
) = TSS_Stream_Write
);
1088 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1089 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
1090 Set_Is_Dispatching_Operation
(Subp
);
1095 -- The operation may be a child unit, whose scope is the defining
1096 -- package, but which is not a primitive operation of the type.
1098 elsif Is_Child_Unit
(Subp
) then
1101 -- If the subprogram is not defined in a package spec, the only case
1102 -- where it can be a dispatching op is when it overrides an operation
1103 -- before the freezing point of the type.
1105 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
1106 or else In_Package_Body
(Scope
(Subp
)))
1107 and then not Has_Dispatching_Parent
1109 if not Comes_From_Source
(Subp
)
1110 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
1114 -- If the type is already frozen, the overriding is not allowed
1115 -- except when Old_Subp is not a dispatching operation (which can
1116 -- occur when Old_Subp was inherited by an untagged type). However,
1117 -- a body with no previous spec freezes the type *after* its
1118 -- declaration, and therefore is a legal overriding (unless the type
1119 -- has already been frozen). Only the first such body is legal.
1121 elsif Present
(Old_Subp
)
1122 and then Is_Dispatching_Operation
(Old_Subp
)
1124 if Comes_From_Source
(Subp
)
1126 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
1127 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
1130 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1131 Decl_Item
: Node_Id
;
1134 -- ??? The checks here for whether the type has been frozen
1135 -- prior to the new body are not complete. It's not simple
1136 -- to check frozenness at this point since the body has
1137 -- already caused the type to be prematurely frozen in
1138 -- Analyze_Declarations, but we're forced to recheck this
1139 -- here because of the odd rule interpretation that allows
1140 -- the overriding if the type wasn't frozen prior to the
1141 -- body. The freezing action should probably be delayed
1142 -- until after the spec is seen, but that's a tricky
1143 -- change to the delicate freezing code.
1145 -- Look at each declaration following the type up until the
1146 -- new subprogram body. If any of the declarations is a body
1147 -- then the type has been frozen already so the overriding
1148 -- primitive is illegal.
1150 Decl_Item
:= Next
(Parent
(Tagged_Type
));
1151 while Present
(Decl_Item
)
1152 and then (Decl_Item
/= Subp_Body
)
1154 if Comes_From_Source
(Decl_Item
)
1155 and then (Nkind
(Decl_Item
) in N_Proper_Body
1156 or else Nkind
(Decl_Item
) in N_Body_Stub
)
1158 Error_Msg_N
("overriding of& is too late!", Subp
);
1160 ("\spec should appear immediately after the type!",
1168 -- If the subprogram doesn't follow in the list of
1169 -- declarations including the type then the type has
1170 -- definitely been frozen already and the body is illegal.
1172 if No
(Decl_Item
) then
1173 Error_Msg_N
("overriding of& is too late!", Subp
);
1175 ("\spec should appear immediately after the type!",
1178 elsif Is_Frozen
(Subp
) then
1180 -- The subprogram body declares a primitive operation.
1181 -- If the subprogram is already frozen, we must update
1182 -- its dispatching information explicitly here. The
1183 -- information is taken from the overridden subprogram.
1184 -- We must also generate a cross-reference entry because
1185 -- references to other primitives were already created
1186 -- when type was frozen.
1188 Body_Is_Last_Primitive
:= True;
1190 if Present
(DTC_Entity
(Old_Subp
)) then
1191 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
1192 Set_DT_Position_Value
(Subp
, DT_Position
(Old_Subp
));
1194 if not Restriction_Active
(No_Dispatching_Calls
) then
1195 if Building_Static_DT
(Tagged_Type
) then
1197 -- If the static dispatch table has not been
1198 -- built then there is nothing else to do now;
1199 -- otherwise we notify that we cannot build the
1200 -- static dispatch table.
1202 if Has_Dispatch_Table
(Tagged_Type
) then
1204 ("overriding of& is too late for building "
1205 & " static dispatch tables!", Subp
);
1207 ("\spec should appear immediately after "
1208 & "the type!", Subp
);
1211 -- No code required to register primitives in VM
1214 elsif not Tagged_Type_Expansion
then
1218 Insert_Actions_After
(Subp_Body
,
1219 Register_Primitive
(Sloc
(Subp_Body
),
1223 -- Indicate that this is an overriding operation,
1224 -- and replace the overridden entry in the list of
1225 -- primitive operations, which is used for xref
1226 -- generation subsequently.
1228 Generate_Reference
(Tagged_Type
, Subp
, 'P', False);
1229 Override_Dispatching_Operation
1230 (Tagged_Type
, Old_Subp
, Subp
);
1237 Error_Msg_N
("overriding of& is too late!", Subp
);
1239 ("\subprogram spec should appear immediately after the type!",
1243 -- If the type is not frozen yet and we are not in the overriding
1244 -- case it looks suspiciously like an attempt to define a primitive
1245 -- operation, which requires the declaration to be in a package spec
1246 -- (3.2.3(6)). Only report cases where the type and subprogram are
1247 -- in the same declaration list (by checking the enclosing parent
1248 -- declarations), to avoid spurious warnings on subprograms in
1249 -- instance bodies when the type is declared in the instance spec
1250 -- but hasn't been frozen by the instance body.
1252 elsif not Is_Frozen
(Tagged_Type
)
1253 and then In_Same_List
(Parent
(Tagged_Type
), Parent
(Parent
(Subp
)))
1256 ("??not dispatching (must be defined in a package spec)", Subp
);
1259 -- When the type is frozen, it is legitimate to define a new
1260 -- non-primitive operation.
1266 -- Now, we are sure that the scope is a package spec. If the subprogram
1267 -- is declared after the freezing point of the type that's an error
1269 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
1270 Error_Msg_N
("this primitive operation is declared too late", Subp
);
1272 ("??no primitive operations for& after this line",
1273 Freeze_Node
(Tagged_Type
),
1278 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1280 Ovr_Subp
:= Old_Subp
;
1282 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1283 -- overridden by Subp. This only applies to source subprograms, and
1284 -- their declaration must carry an explicit overriding indicator.
1287 and then Ada_Version
>= Ada_2012
1288 and then Comes_From_Source
(Subp
)
1290 Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Declaration
1292 Ovr_Subp
:= Find_Hidden_Overridden_Primitive
(Subp
);
1294 -- Verify that the proper overriding indicator has been supplied.
1296 if Present
(Ovr_Subp
)
1298 not Must_Override
(Specification
(Unit_Declaration_Node
(Subp
)))
1300 Error_Msg_NE
("missing overriding indicator for&", Subp
, Subp
);
1304 -- Now it should be a correct primitive operation, put it in the list
1306 if Present
(Ovr_Subp
) then
1308 -- If the type has interfaces we complete this check after we set
1309 -- attribute Is_Dispatching_Operation.
1311 Check_Subtype_Conformant
(Subp
, Ovr_Subp
);
1313 -- A primitive operation with the name of a primitive controlled
1314 -- operation does not override a non-visible overriding controlled
1315 -- operation, i.e. one declared in a private part when the full
1316 -- view of a type is controlled. Conversely, it will override a
1317 -- visible operation that may be declared in a partial view when
1318 -- the full view is controlled.
1320 if Nam_In
(Chars
(Subp
), Name_Initialize
, Name_Adjust
, Name_Finalize
)
1321 and then Is_Controlled
(Tagged_Type
)
1322 and then not Is_Visibly_Controlled
(Tagged_Type
)
1323 and then not Is_Inherited_Public_Operation
(Ovr_Subp
)
1325 Set_Overridden_Operation
(Subp
, Empty
);
1327 -- If the subprogram specification carries an overriding
1328 -- indicator, no need for the warning: it is either redundant,
1329 -- or else an error will be reported.
1331 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
1333 (Must_Override
(Parent
(Subp
))
1334 or else Must_Not_Override
(Parent
(Subp
)))
1338 -- Here we need the warning
1342 ("operation does not override inherited&??", Subp
, Subp
);
1346 Override_Dispatching_Operation
(Tagged_Type
, Ovr_Subp
, Subp
);
1348 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1349 -- that covers abstract interface subprograms we must register it
1350 -- in all the secondary dispatch tables associated with abstract
1351 -- interfaces. We do this now only if not building static tables,
1352 -- nor when the expander is inactive (we avoid trying to register
1353 -- primitives in semantics-only mode, since the type may not have
1354 -- an associated dispatch table). Otherwise the patch code is
1355 -- emitted after those tables are built, to prevent access before
1356 -- elaboration in gigi.
1358 if Body_Is_Last_Primitive
and then Expander_Active
then
1360 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1365 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1366 while Present
(Elmt
) loop
1367 Prim
:= Node
(Elmt
);
1369 -- No code required to register primitives in VM targets
1371 if Present
(Alias
(Prim
))
1372 and then Present
(Interface_Alias
(Prim
))
1373 and then Alias
(Prim
) = Subp
1374 and then not Building_Static_DT
(Tagged_Type
)
1375 and then Tagged_Type_Expansion
1377 Insert_Actions_After
(Subp_Body
,
1378 Register_Primitive
(Sloc
(Subp_Body
), Prim
=> Prim
));
1384 -- Redisplay the contents of the updated dispatch table
1386 if Debug_Flag_ZZ
then
1387 Write_Str
("Late overriding: ");
1388 Write_DT
(Tagged_Type
);
1394 -- If the tagged type is a concurrent type then we must be compiling
1395 -- with no code generation (we are either compiling a generic unit or
1396 -- compiling under -gnatc mode) because we have previously tested that
1397 -- no serious errors has been reported. In this case we do not add the
1398 -- primitive to the list of primitives of Tagged_Type but we leave the
1399 -- primitive decorated as a dispatching operation to be able to analyze
1400 -- and report errors associated with the Object.Operation notation.
1402 elsif Is_Concurrent_Type
(Tagged_Type
) then
1403 pragma Assert
(not Expander_Active
);
1405 -- Attach operation to list of primitives of the synchronized type
1406 -- itself, for ASIS use.
1408 Append_Elmt
(Subp
, Direct_Primitive_Operations
(Tagged_Type
));
1410 -- If no old subprogram, then we add this as a dispatching operation,
1411 -- but we avoid doing this if an error was posted, to prevent annoying
1414 elsif not Error_Posted
(Subp
) then
1415 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
1418 Set_Is_Dispatching_Operation
(Subp
, True);
1420 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1421 -- subtype conformance against all the interfaces covered by this
1424 if Present
(Ovr_Subp
)
1425 and then Has_Interfaces
(Tagged_Type
)
1428 Ifaces_List
: Elist_Id
;
1429 Iface_Elmt
: Elmt_Id
;
1430 Iface_Prim_Elmt
: Elmt_Id
;
1431 Iface_Prim
: Entity_Id
;
1432 Ret_Typ
: Entity_Id
;
1435 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
1437 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
1438 while Present
(Iface_Elmt
) loop
1439 if not Is_Ancestor
(Node
(Iface_Elmt
), Tagged_Type
) then
1441 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
1442 while Present
(Iface_Prim_Elmt
) loop
1443 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
1445 if Is_Interface_Conformant
1446 (Tagged_Type
, Iface_Prim
, Subp
)
1448 -- Handle procedures, functions whose return type
1449 -- matches, or functions not returning interfaces
1451 if Ekind
(Subp
) = E_Procedure
1452 or else Etype
(Iface_Prim
) = Etype
(Subp
)
1453 or else not Is_Interface
(Etype
(Iface_Prim
))
1455 Check_Subtype_Conformant
1457 Old_Id
=> Iface_Prim
,
1459 Skip_Controlling_Formals
=> True);
1461 -- Handle functions returning interfaces
1463 elsif Implements_Interface
1464 (Etype
(Subp
), Etype
(Iface_Prim
))
1466 -- Temporarily force both entities to return the
1467 -- same type. Required because Subtype_Conformant
1468 -- does not handle this case.
1470 Ret_Typ
:= Etype
(Iface_Prim
);
1471 Set_Etype
(Iface_Prim
, Etype
(Subp
));
1473 Check_Subtype_Conformant
1475 Old_Id
=> Iface_Prim
,
1477 Skip_Controlling_Formals
=> True);
1479 Set_Etype
(Iface_Prim
, Ret_Typ
);
1483 Next_Elmt
(Iface_Prim_Elmt
);
1487 Next_Elmt
(Iface_Elmt
);
1492 if not Body_Is_Last_Primitive
then
1493 Set_DT_Position_Value
(Subp
, No_Uint
);
1495 elsif Has_Controlled_Component
(Tagged_Type
)
1496 and then Nam_In
(Chars
(Subp
), Name_Initialize
,
1499 Name_Finalize_Address
)
1502 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
1506 Old_Spec
: Entity_Id
;
1508 C_Names
: constant array (1 .. 4) of Name_Id
:=
1512 Name_Finalize_Address
);
1514 D_Names
: constant array (1 .. 4) of TSS_Name_Type
:=
1515 (TSS_Deep_Initialize
,
1518 TSS_Finalize_Address
);
1521 -- Remove previous controlled function which was constructed and
1522 -- analyzed when the type was frozen. This requires removing the
1523 -- body of the redefined primitive, as well as its specification
1524 -- if needed (there is no spec created for Deep_Initialize, see
1525 -- exp_ch3.adb). We must also dismantle the exception information
1526 -- that may have been generated for it when front end zero-cost
1527 -- tables are enabled.
1529 for J
in D_Names
'Range loop
1530 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
1533 and then Chars
(Subp
) = C_Names
(J
)
1535 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
1537 Set_Is_Eliminated
(Old_P
);
1538 Set_Scope
(Old_P
, Scope
(Current_Scope
));
1540 if Nkind
(Old_Bod
) = N_Subprogram_Body
1541 and then Present
(Corresponding_Spec
(Old_Bod
))
1543 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
1544 Set_Has_Completion
(Old_Spec
, False);
1549 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
1551 -- The new operation is added to the actions of the freeze node
1552 -- for the type, but this node has already been analyzed, so we
1553 -- must retrieve and analyze explicitly the new body.
1556 and then Present
(Actions
(F_Node
))
1558 Decl
:= Last
(Actions
(F_Node
));
1563 end Check_Dispatching_Operation
;
1565 ------------------------------------------
1566 -- Check_Operation_From_Incomplete_Type --
1567 ------------------------------------------
1569 procedure Check_Operation_From_Incomplete_Type
1573 Full
: constant Entity_Id
:= Full_View
(Typ
);
1574 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1575 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1576 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1578 Prev
: Elmt_Id
:= No_Elmt
;
1580 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean;
1581 -- Check that Subp has profile of an operation derived from Parent_Subp.
1582 -- Subp must have a parameter or result type that is Typ or an access
1583 -- parameter or access result type that designates Typ.
1589 function Derives_From
(Parent_Subp
: Entity_Id
) return Boolean is
1593 if Chars
(Parent_Subp
) /= Chars
(Subp
) then
1597 -- Check that the type of controlling formals is derived from the
1598 -- parent subprogram's controlling formal type (or designated type
1599 -- if the formal type is an anonymous access type).
1601 F1
:= First_Formal
(Parent_Subp
);
1602 F2
:= First_Formal
(Subp
);
1603 while Present
(F1
) and then Present
(F2
) loop
1604 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
1605 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
1607 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
1608 and then Designated_Type
(Etype
(F2
)) /= Full
1613 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
1616 elsif Etype
(F1
) = Parent_Typ
and then Etype
(F2
) /= Full
then
1624 -- Check that a controlling result type is derived from the parent
1625 -- subprogram's result type (or designated type if the result type
1626 -- is an anonymous access type).
1628 if Ekind
(Parent_Subp
) = E_Function
then
1629 if Ekind
(Subp
) /= E_Function
then
1632 elsif Ekind
(Etype
(Parent_Subp
)) = E_Anonymous_Access_Type
then
1633 if Ekind
(Etype
(Subp
)) /= E_Anonymous_Access_Type
then
1636 elsif Designated_Type
(Etype
(Parent_Subp
)) = Parent_Typ
1637 and then Designated_Type
(Etype
(Subp
)) /= Full
1642 elsif Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
then
1645 elsif Etype
(Parent_Subp
) = Parent_Typ
1646 and then Etype
(Subp
) /= Full
1651 elsif Ekind
(Subp
) = E_Function
then
1655 return No
(F1
) and then No
(F2
);
1658 -- Start of processing for Check_Operation_From_Incomplete_Type
1661 -- The operation may override an inherited one, or may be a new one
1662 -- altogether. The inherited operation will have been hidden by the
1663 -- current one at the point of the type derivation, so it does not
1664 -- appear in the list of primitive operations of the type. We have to
1665 -- find the proper place of insertion in the list of primitive opera-
1666 -- tions by iterating over the list for the parent type.
1668 Op1
:= First_Elmt
(Old_Prim
);
1669 Op2
:= First_Elmt
(New_Prim
);
1670 while Present
(Op1
) and then Present
(Op2
) loop
1671 if Derives_From
(Node
(Op1
)) then
1674 -- Avoid adding it to the list of primitives if already there
1676 if Node
(Op2
) /= Subp
then
1677 Prepend_Elmt
(Subp
, New_Prim
);
1681 Insert_Elmt_After
(Subp
, Prev
);
1692 -- Operation is a new primitive
1694 Append_Elmt
(Subp
, New_Prim
);
1695 end Check_Operation_From_Incomplete_Type
;
1697 ---------------------------------------
1698 -- Check_Operation_From_Private_View --
1699 ---------------------------------------
1701 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
1702 Tagged_Type
: Entity_Id
;
1705 if Is_Dispatching_Operation
(Alias
(Subp
)) then
1706 Set_Scope
(Subp
, Current_Scope
);
1707 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1709 -- Add Old_Subp to primitive operations if not already present
1711 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
1712 Append_Unique_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
1714 -- If Old_Subp isn't already marked as dispatching then this is
1715 -- the case of an operation of an untagged private type fulfilled
1716 -- by a tagged type that overrides an inherited dispatching
1717 -- operation, so we set the necessary dispatching attributes here.
1719 if not Is_Dispatching_Operation
(Old_Subp
) then
1721 -- If the untagged type has no discriminants, and the full
1722 -- view is constrained, there will be a spurious mismatch of
1723 -- subtypes on the controlling arguments, because the tagged
1724 -- type is the internal base type introduced in the derivation.
1725 -- Use the original type to verify conformance, rather than the
1728 if not Comes_From_Source
(Tagged_Type
)
1729 and then Has_Discriminants
(Tagged_Type
)
1735 Formal
:= First_Formal
(Old_Subp
);
1736 while Present
(Formal
) loop
1737 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
1738 Tagged_Type
:= Etype
(Formal
);
1741 Next_Formal
(Formal
);
1745 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
1746 Tagged_Type
:= Etype
(Old_Subp
);
1750 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
1751 Set_Is_Dispatching_Operation
(Old_Subp
, True);
1752 Set_DT_Position_Value
(Old_Subp
, No_Uint
);
1755 -- If the old subprogram is an explicit renaming of some other
1756 -- entity, it is not overridden by the inherited subprogram.
1757 -- Otherwise, update its alias and other attributes.
1759 if Present
(Alias
(Old_Subp
))
1760 and then Nkind
(Unit_Declaration_Node
(Old_Subp
)) /=
1761 N_Subprogram_Renaming_Declaration
1763 Set_Alias
(Old_Subp
, Alias
(Subp
));
1765 -- The derived subprogram should inherit the abstractness of
1766 -- the parent subprogram (except in the case of a function
1767 -- returning the type). This sets the abstractness properly
1768 -- for cases where a private extension may have inherited an
1769 -- abstract operation, but the full type is derived from a
1770 -- descendant type and inherits a nonabstract version.
1772 if Etype
(Subp
) /= Tagged_Type
then
1773 Set_Is_Abstract_Subprogram
1774 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
1779 end Check_Operation_From_Private_View
;
1781 --------------------------
1782 -- Find_Controlling_Arg --
1783 --------------------------
1785 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1786 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1790 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1791 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1794 -- Dispatching on result case. If expansion is disabled, the node still
1795 -- has the structure of a function call. However, if the function name
1796 -- is an operator and the call was given in infix form, the original
1797 -- node has no controlling result and we must examine the current node.
1799 if Nkind
(N
) = N_Function_Call
1800 and then Present
(Controlling_Argument
(N
))
1801 and then Has_Controlling_Result
(Entity
(Name
(N
)))
1803 return Controlling_Argument
(N
);
1805 -- If expansion is enabled, the call may have been transformed into
1806 -- an indirect call, and we need to recover the original node.
1808 elsif Nkind
(Orig_Node
) = N_Function_Call
1809 and then Present
(Controlling_Argument
(Orig_Node
))
1810 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1812 return Controlling_Argument
(Orig_Node
);
1814 -- Type conversions are dynamically tagged if the target type, or its
1815 -- designated type, are classwide. An interface conversion expands into
1816 -- a dereference, so test must be performed on the original node.
1818 elsif Nkind
(Orig_Node
) = N_Type_Conversion
1819 and then Nkind
(N
) = N_Explicit_Dereference
1820 and then Is_Controlling_Actual
(N
)
1823 Target_Type
: constant Entity_Id
:=
1824 Entity
(Subtype_Mark
(Orig_Node
));
1827 if Is_Class_Wide_Type
(Target_Type
) then
1830 elsif Is_Access_Type
(Target_Type
)
1831 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
1842 elsif Is_Controlling_Actual
(N
)
1844 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1845 and then Is_Controlling_Actual
(Parent
(N
)))
1849 if Is_Access_Type
(Typ
) then
1851 -- In the case of an Access attribute, use the type of the prefix,
1852 -- since in the case of an actual for an access parameter, the
1853 -- attribute's type may be of a specific designated type, even
1854 -- though the prefix type is class-wide.
1856 if Nkind
(N
) = N_Attribute_Reference
then
1857 Typ
:= Etype
(Prefix
(N
));
1859 -- An allocator is dispatching if the type of qualified expression
1860 -- is class_wide, in which case this is the controlling type.
1862 elsif Nkind
(Orig_Node
) = N_Allocator
1863 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1865 Typ
:= Etype
(Expression
(Orig_Node
));
1867 Typ
:= Designated_Type
(Typ
);
1871 if Is_Class_Wide_Type
(Typ
)
1873 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1874 and then Is_Access_Type
(Etype
(N
))
1875 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1882 end Find_Controlling_Arg
;
1884 ---------------------------
1885 -- Find_Dispatching_Type --
1886 ---------------------------
1888 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1889 A_Formal
: Entity_Id
;
1891 Ctrl_Type
: Entity_Id
;
1894 if Ekind_In
(Subp
, E_Function
, E_Procedure
)
1895 and then Present
(DTC_Entity
(Subp
))
1897 return Scope
(DTC_Entity
(Subp
));
1899 -- For subprograms internally generated by derivations of tagged types
1900 -- use the alias subprogram as a reference to locate the dispatching
1903 elsif not Comes_From_Source
(Subp
)
1904 and then Present
(Alias
(Subp
))
1905 and then Is_Dispatching_Operation
(Alias
(Subp
))
1907 if Ekind
(Alias
(Subp
)) = E_Function
1908 and then Has_Controlling_Result
(Alias
(Subp
))
1910 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1913 Formal
:= First_Formal
(Subp
);
1914 A_Formal
:= First_Formal
(Alias
(Subp
));
1915 while Present
(A_Formal
) loop
1916 if Is_Controlling_Formal
(A_Formal
) then
1917 return Check_Controlling_Type
(Etype
(Formal
), Subp
);
1920 Next_Formal
(Formal
);
1921 Next_Formal
(A_Formal
);
1924 pragma Assert
(False);
1931 Formal
:= First_Formal
(Subp
);
1932 while Present
(Formal
) loop
1933 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1935 if Present
(Ctrl_Type
) then
1939 Next_Formal
(Formal
);
1942 -- The subprogram may also be dispatching on result
1944 if Present
(Etype
(Subp
)) then
1945 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1949 pragma Assert
(not Is_Dispatching_Operation
(Subp
));
1951 end Find_Dispatching_Type
;
1953 --------------------------------------
1954 -- Find_Hidden_Overridden_Primitive --
1955 --------------------------------------
1957 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
1959 Tag_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(S
);
1961 Orig_Prim
: Entity_Id
;
1963 Vis_List
: Elist_Id
;
1966 -- This Ada 2012 rule applies only for type extensions or private
1967 -- extensions, where the parent type is not in a parent unit, and
1968 -- where an operation is never declared but still inherited.
1971 or else not Is_Record_Type
(Tag_Typ
)
1972 or else Etype
(Tag_Typ
) = Tag_Typ
1973 or else In_Open_Scopes
(Scope
(Etype
(Tag_Typ
)))
1978 -- Collect the list of visible ancestor of the tagged type
1980 Vis_List
:= Visible_Ancestors
(Tag_Typ
);
1982 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
1983 while Present
(Elmt
) loop
1984 Prim
:= Node
(Elmt
);
1986 -- Find an inherited hidden dispatching primitive with the name of S
1987 -- and a type-conformant profile.
1989 if Present
(Alias
(Prim
))
1990 and then Is_Hidden
(Alias
(Prim
))
1991 and then Find_Dispatching_Type
(Alias
(Prim
)) /= Tag_Typ
1992 and then Primitive_Names_Match
(S
, Prim
)
1993 and then Type_Conformant
(S
, Prim
)
1996 Vis_Ancestor
: Elmt_Id
;
2000 -- The original corresponding operation of Prim must be an
2001 -- operation of a visible ancestor of the dispatching type S,
2002 -- and the original corresponding operation of S2 must be
2005 Orig_Prim
:= Original_Corresponding_Operation
(Prim
);
2007 if Orig_Prim
/= Prim
2008 and then Is_Immediately_Visible
(Orig_Prim
)
2010 Vis_Ancestor
:= First_Elmt
(Vis_List
);
2011 while Present
(Vis_Ancestor
) loop
2013 First_Elmt
(Primitive_Operations
(Node
(Vis_Ancestor
)));
2014 while Present
(Elmt
) loop
2015 if Node
(Elmt
) = Orig_Prim
then
2016 Set_Overridden_Operation
(S
, Prim
);
2017 Set_Alias
(Prim
, Orig_Prim
);
2024 Next_Elmt
(Vis_Ancestor
);
2034 end Find_Hidden_Overridden_Primitive
;
2036 ---------------------------------------
2037 -- Find_Primitive_Covering_Interface --
2038 ---------------------------------------
2040 function Find_Primitive_Covering_Interface
2041 (Tagged_Type
: Entity_Id
;
2042 Iface_Prim
: Entity_Id
) return Entity_Id
2048 pragma Assert
(Is_Interface
(Find_Dispatching_Type
(Iface_Prim
))
2049 or else (Present
(Alias
(Iface_Prim
))
2052 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
2054 -- Search in the homonym chain. Done to speed up locating visible
2055 -- entities and required to catch primitives associated with the partial
2056 -- view of private types when processing the corresponding full view.
2058 E
:= Current_Entity
(Iface_Prim
);
2059 while Present
(E
) loop
2060 if Is_Subprogram
(E
)
2061 and then Is_Dispatching_Operation
(E
)
2062 and then Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
)
2070 -- Search in the list of primitives of the type. Required to locate
2071 -- the covering primitive if the covering primitive is not visible
2072 -- (for example, non-visible inherited primitive of private type).
2074 El
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2075 while Present
(El
) loop
2078 -- Keep separate the management of internal entities that link
2079 -- primitives with interface primitives from tagged type primitives.
2081 if No
(Interface_Alias
(E
)) then
2082 if Present
(Alias
(E
)) then
2084 -- This interface primitive has not been covered yet
2086 if Alias
(E
) = Iface_Prim
then
2089 -- The covering primitive was inherited
2091 elsif Overridden_Operation
(Ultimate_Alias
(E
))
2098 -- Check if E covers the interface primitive (includes case in
2099 -- which E is an inherited private primitive).
2101 if Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
) then
2105 -- Use the internal entity that links the interface primitive with
2106 -- the covering primitive to locate the entity.
2108 elsif Interface_Alias
(E
) = Iface_Prim
then
2118 end Find_Primitive_Covering_Interface
;
2120 ---------------------------
2121 -- Inherited_Subprograms --
2122 ---------------------------
2124 function Inherited_Subprograms
2126 No_Interfaces
: Boolean := False;
2127 Interfaces_Only
: Boolean := False;
2128 One_Only
: Boolean := False) return Subprogram_List
2130 Result
: Subprogram_List
(1 .. 6000);
2131 -- 6000 here is intended to be infinity. We could use an expandable
2132 -- table, but it would be awfully heavy, and there is no way that we
2133 -- could reasonably exceed this value.
2136 -- Number of entries in Result
2138 Parent_Op
: Entity_Id
;
2139 -- Traverses the Overridden_Operation chain
2141 procedure Store_IS
(E
: Entity_Id
);
2142 -- Stores E in Result if not already stored
2148 procedure Store_IS
(E
: Entity_Id
) is
2150 for J
in 1 .. N
loop
2151 if E
= Result
(J
) then
2160 -- Start of processing for Inherited_Subprograms
2163 pragma Assert
(not (No_Interfaces
and Interfaces_Only
));
2165 if Present
(S
) and then Is_Dispatching_Operation
(S
) then
2167 -- Deal with direct inheritance
2169 if not Interfaces_Only
then
2172 Parent_Op
:= Overridden_Operation
(Parent_Op
);
2173 exit when No
(Parent_Op
)
2177 Is_Interface
(Find_Dispatching_Type
(Parent_Op
)));
2179 if Is_Subprogram_Or_Generic_Subprogram
(Parent_Op
) then
2180 Store_IS
(Parent_Op
);
2189 -- Now deal with interfaces
2191 if not No_Interfaces
then
2193 Tag_Typ
: Entity_Id
;
2198 Tag_Typ
:= Find_Dispatching_Type
(S
);
2200 -- In the presence of limited views there may be no visible
2201 -- dispatching type. Primitives will be inherited when non-
2202 -- limited view is frozen.
2204 if No
(Tag_Typ
) then
2205 return Result
(1 .. 0);
2208 if Is_Concurrent_Type
(Tag_Typ
) then
2209 Tag_Typ
:= Corresponding_Record_Type
(Tag_Typ
);
2212 -- Search primitive operations of dispatching type
2214 if Present
(Tag_Typ
)
2215 and then Present
(Primitive_Operations
(Tag_Typ
))
2217 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
2218 while Present
(Elmt
) loop
2219 Prim
:= Node
(Elmt
);
2221 -- The following test eliminates some odd cases in which
2222 -- Ekind (Prim) is Void, to be investigated further ???
2224 if not Is_Subprogram_Or_Generic_Subprogram
(Prim
) then
2227 -- For [generic] subprogram, look at interface alias
2229 elsif Present
(Interface_Alias
(Prim
))
2230 and then Alias
(Prim
) = S
2232 -- We have found a primitive covered by S
2234 Store_IS
(Interface_Alias
(Prim
));
2250 return Result
(1 .. N
);
2251 end Inherited_Subprograms
;
2253 ---------------------------
2254 -- Is_Dynamically_Tagged --
2255 ---------------------------
2257 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
2259 if Nkind
(N
) = N_Error
then
2262 elsif Present
(Find_Controlling_Arg
(N
)) then
2265 -- Special cases: entities, and calls that dispatch on result
2267 elsif Is_Entity_Name
(N
) then
2268 return Is_Class_Wide_Type
(Etype
(N
));
2270 elsif Nkind
(N
) = N_Function_Call
2271 and then Is_Class_Wide_Type
(Etype
(N
))
2275 -- Otherwise check whether call has controlling argument
2280 end Is_Dynamically_Tagged
;
2282 ---------------------------------
2283 -- Is_Null_Interface_Primitive --
2284 ---------------------------------
2286 function Is_Null_Interface_Primitive
(E
: Entity_Id
) return Boolean is
2288 return Comes_From_Source
(E
)
2289 and then Is_Dispatching_Operation
(E
)
2290 and then Ekind
(E
) = E_Procedure
2291 and then Null_Present
(Parent
(E
))
2292 and then Is_Interface
(Find_Dispatching_Type
(E
));
2293 end Is_Null_Interface_Primitive
;
2295 -----------------------------------
2296 -- Is_Inherited_Public_Operation --
2297 -----------------------------------
2299 function Is_Inherited_Public_Operation
(Op
: Entity_Id
) return Boolean is
2300 Prim
: constant Entity_Id
:= Alias
(Op
);
2301 Scop
: constant Entity_Id
:= Scope
(Prim
);
2302 Pack_Decl
: Node_Id
;
2305 if Comes_From_Source
(Prim
) and then Ekind
(Scop
) = E_Package
then
2306 Pack_Decl
:= Unit_Declaration_Node
(Scop
);
2307 return Nkind
(Pack_Decl
) = N_Package_Declaration
2308 and then List_Containing
(Unit_Declaration_Node
(Prim
)) =
2309 Visible_Declarations
(Specification
(Pack_Decl
));
2314 end Is_Inherited_Public_Operation
;
2316 ------------------------------
2317 -- Is_Overriding_Subprogram --
2318 ------------------------------
2320 function Is_Overriding_Subprogram
(E
: Entity_Id
) return Boolean is
2321 Inherited
: constant Subprogram_List
:=
2322 Inherited_Subprograms
(E
, One_Only
=> True);
2324 return Inherited
'Length > 0;
2325 end Is_Overriding_Subprogram
;
2327 --------------------------
2328 -- Is_Tag_Indeterminate --
2329 --------------------------
2331 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
2334 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
2337 if Nkind
(Orig_Node
) = N_Function_Call
2338 and then Is_Entity_Name
(Name
(Orig_Node
))
2340 Nam
:= Entity
(Name
(Orig_Node
));
2342 if not Has_Controlling_Result
(Nam
) then
2345 -- The function may have a controlling result, but if the return type
2346 -- is not visibly tagged, then this is not tag-indeterminate.
2348 elsif Is_Access_Type
(Etype
(Nam
))
2349 and then not Is_Tagged_Type
(Designated_Type
(Etype
(Nam
)))
2353 -- An explicit dereference means that the call has already been
2354 -- expanded and there is no tag to propagate.
2356 elsif Nkind
(N
) = N_Explicit_Dereference
then
2359 -- If there are no actuals, the call is tag-indeterminate
2361 elsif No
(Parameter_Associations
(Orig_Node
)) then
2365 Actual
:= First_Actual
(Orig_Node
);
2366 while Present
(Actual
) loop
2367 if Is_Controlling_Actual
(Actual
)
2368 and then not Is_Tag_Indeterminate
(Actual
)
2370 -- One operand is dispatching
2375 Next_Actual
(Actual
);
2381 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
2382 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
2384 -- Case of a call to the Input attribute (possibly rewritten), which is
2385 -- always tag-indeterminate except when its prefix is a Class attribute.
2387 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
2389 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
2390 and then Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
2394 -- In Ada 2005, a function that returns an anonymous access type can be
2395 -- dispatching, and the dereference of a call to such a function can
2396 -- also be tag-indeterminate if the call itself is.
2398 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
2399 and then Ada_Version
>= Ada_2005
2401 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
2406 end Is_Tag_Indeterminate
;
2408 ------------------------------------
2409 -- Override_Dispatching_Operation --
2410 ------------------------------------
2412 procedure Override_Dispatching_Operation
2413 (Tagged_Type
: Entity_Id
;
2414 Prev_Op
: Entity_Id
;
2416 Is_Wrapper
: Boolean := False)
2422 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2423 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2425 if No_Return
(Prev_Op
) and then not No_Return
(New_Op
) then
2426 Error_Msg_N
("procedure & must have No_Return pragma", New_Op
);
2427 Error_Msg_N
("\since overridden procedure has No_Return", New_Op
);
2430 -- If there is no previous operation to override, the type declaration
2431 -- was malformed, and an error must have been emitted already.
2433 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2434 while Present
(Elmt
) and then Node
(Elmt
) /= Prev_Op
loop
2442 -- The location of entities that come from source in the list of
2443 -- primitives of the tagged type must follow their order of occurrence
2444 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2445 -- primitive of an interface that is not implemented by the parents of
2446 -- this tagged type (that is, it is an alias of an interface primitive
2447 -- generated by Derive_Interface_Progenitors), then we must append the
2448 -- new entity at the end of the list of primitives.
2450 if Present
(Alias
(Prev_Op
))
2451 and then Etype
(Tagged_Type
) /= Tagged_Type
2452 and then Is_Interface
(Find_Dispatching_Type
(Alias
(Prev_Op
)))
2453 and then not Is_Ancestor
(Find_Dispatching_Type
(Alias
(Prev_Op
)),
2454 Tagged_Type
, Use_Full_View
=> True)
2455 and then not Implements_Interface
2456 (Etype
(Tagged_Type
),
2457 Find_Dispatching_Type
(Alias
(Prev_Op
)))
2459 Remove_Elmt
(Primitive_Operations
(Tagged_Type
), Elmt
);
2460 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
2462 -- The new primitive replaces the overridden entity. Required to ensure
2463 -- that overriding primitive is assigned the same dispatch table slot.
2466 Replace_Elmt
(Elmt
, New_Op
);
2469 if Ada_Version
>= Ada_2005
and then Has_Interfaces
(Tagged_Type
) then
2471 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2472 -- entities of the overridden primitive to reference New_Op, and
2473 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2474 -- that the new operation is subtype conformant with the interface
2475 -- operations that it implements (for operations inherited from the
2476 -- parent itself, this check is made when building the derived type).
2478 -- Note: This code is executed with internally generated wrappers of
2479 -- functions with controlling result and late overridings.
2481 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2482 while Present
(Elmt
) loop
2483 Prim
:= Node
(Elmt
);
2485 if Prim
= New_Op
then
2488 -- Note: The check on Is_Subprogram protects the frontend against
2489 -- reading attributes in entities that are not yet fully decorated
2491 elsif Is_Subprogram
(Prim
)
2492 and then Present
(Interface_Alias
(Prim
))
2493 and then Alias
(Prim
) = Prev_Op
2495 Set_Alias
(Prim
, New_Op
);
2497 -- No further decoration needed yet for internally generated
2498 -- wrappers of controlling functions since (at this stage)
2499 -- they are not yet decorated.
2501 if not Is_Wrapper
then
2502 Check_Subtype_Conformant
(New_Op
, Prim
);
2504 Set_Is_Abstract_Subprogram
(Prim
,
2505 Is_Abstract_Subprogram
(New_Op
));
2507 -- Ensure that this entity will be expanded to fill the
2508 -- corresponding entry in its dispatch table.
2510 if not Is_Abstract_Subprogram
(Prim
) then
2511 Set_Has_Delayed_Freeze
(Prim
);
2520 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
2521 or else not In_Private_Part
(Current_Scope
)
2523 -- Not a private primitive
2527 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
2529 -- Make the overriding operation into an alias of the implicit one.
2530 -- In this fashion a call from outside ends up calling the new body
2531 -- even if non-dispatching, and a call from inside calls the over-
2532 -- riding operation because it hides the implicit one. To indicate
2533 -- that the body of Prev_Op is never called, set its dispatch table
2534 -- entity to Empty. If the overridden operation has a dispatching
2535 -- result, so does the overriding one.
2537 Set_Alias
(Prev_Op
, New_Op
);
2538 Set_DTC_Entity
(Prev_Op
, Empty
);
2539 Set_Has_Controlling_Result
(New_Op
, Has_Controlling_Result
(Prev_Op
));
2542 end Override_Dispatching_Operation
;
2548 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
2549 Call_Node
: Node_Id
;
2553 if Nkind
(Actual
) = N_Function_Call
then
2554 Call_Node
:= Actual
;
2556 elsif Nkind
(Actual
) = N_Identifier
2557 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
2559 -- Call rewritten as object declaration when stack-checking is
2560 -- enabled. Propagate tag to expression in declaration, which is
2563 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
2565 -- Ada 2005: If this is a dereference of a call to a function with a
2566 -- dispatching access-result, the tag is propagated when the dereference
2567 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2569 elsif Nkind
(Actual
) = N_Explicit_Dereference
2570 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
2574 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2575 -- and in that case we can simply return.
2577 elsif Nkind
(Actual
) = N_Attribute_Reference
then
2578 pragma Assert
(Attribute_Name
(Actual
) = Name_Input
);
2582 -- Only other possibilities are parenthesized or qualified expression,
2583 -- or an expander-generated unchecked conversion of a function call to
2584 -- a stream Input attribute.
2587 Call_Node
:= Expression
(Actual
);
2590 -- No action needed if the call has been already expanded
2592 if Is_Expanded_Dispatching_Call
(Call_Node
) then
2596 -- Do not set the Controlling_Argument if already set. This happens in
2597 -- the special case of _Input (see Exp_Attr, case Input).
2599 if No
(Controlling_Argument
(Call_Node
)) then
2600 Set_Controlling_Argument
(Call_Node
, Control
);
2603 Arg
:= First_Actual
(Call_Node
);
2604 while Present
(Arg
) loop
2605 if Is_Tag_Indeterminate
(Arg
) then
2606 Propagate_Tag
(Control
, Arg
);
2612 -- Expansion of dispatching calls is suppressed on VM targets, because
2613 -- the VM back-ends directly handle the generation of dispatching calls
2614 -- and would have to undo any expansion to an indirect call.
2616 if Tagged_Type_Expansion
then
2618 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
2621 Expand_Dispatching_Call
(Call_Node
);
2623 -- If the controlling argument is an interface type and the type
2624 -- of Call_Node differs then we must add an implicit conversion to
2625 -- force displacement of the pointer to the object to reference
2626 -- the secondary dispatch table of the interface.
2628 if Is_Interface
(Etype
(Control
))
2629 and then Etype
(Control
) /= Call_Typ
2631 -- Cannot use Convert_To because the previous call to
2632 -- Expand_Dispatching_Call leaves decorated the Call_Node
2633 -- with the type of Control.
2636 Make_Type_Conversion
(Sloc
(Call_Node
),
2638 New_Occurrence_Of
(Etype
(Control
), Sloc
(Call_Node
)),
2639 Expression
=> Relocate_Node
(Call_Node
)));
2640 Set_Etype
(Call_Node
, Etype
(Control
));
2641 Set_Analyzed
(Call_Node
);
2643 Expand_Interface_Conversion
(Call_Node
);
2647 -- Expansion of a dispatching call results in an indirect call, which in
2648 -- turn causes current values to be killed (see Resolve_Call), so on VM
2649 -- targets we do the call here to ensure consistent warnings between VM
2650 -- and non-VM targets.
2653 Kill_Current_Values
;