1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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 Exp_Disp; use Exp_Disp;
32 with Exp_Util; use Exp_Util;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Tss; use Exp_Tss;
35 with Errout; use Errout;
36 with Lib.Xref; use Lib.Xref;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
41 with Output; use Output;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Ch3; use Sem_Ch3;
47 with Sem_Ch6; use Sem_Ch6;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Snames; use Snames;
53 with Sinfo; use Sinfo;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
56 with Warnsw; use Warnsw;
58 package body Sem_Disp is
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 procedure Add_Dispatching_Operation
65 (Tagged_Type : Entity_Id;
67 -- Add New_Op in the list of primitive operations of Tagged_Type
69 function Check_Controlling_Type
71 Subp : Entity_Id) return Entity_Id;
72 -- T is the tagged type of a formal parameter or the result of Subp.
73 -- If the subprogram has a controlling parameter or result that matches
74 -- the type, then returns the tagged type of that parameter or result
75 -- (returning the designated tagged type in the case of an access
76 -- parameter); otherwise returns empty.
78 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
79 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
80 -- type of S that has the same name of S, a type-conformant profile, an
81 -- original corresponding operation O that is a primitive of a visible
82 -- ancestor of the dispatching type of S and O is visible at the point of
83 -- of declaration of S. If the entity is found the Alias of S is set to the
84 -- original corresponding operation S and its Overridden_Operation is set
85 -- to the found entity; otherwise return Empty.
87 -- This routine does not search for non-hidden primitives since they are
88 -- covered by the normal Ada 2005 rules.
90 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
91 -- Check whether a primitive operation is inherited from an operation
92 -- declared in the visible part of its package.
94 -------------------------------
95 -- Add_Dispatching_Operation --
96 -------------------------------
98 procedure Add_Dispatching_Operation
99 (Tagged_Type : Entity_Id;
102 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
105 -- The dispatching operation may already be on the list, if it is the
106 -- wrapper for an inherited function of a null extension (see Exp_Ch3
107 -- for the construction of function wrappers). The list of primitive
108 -- operations must not contain duplicates.
110 -- The Default_Initial_Condition and invariant procedures are not added
111 -- to the list of primitives even when they are generated for a tagged
112 -- type. These routines must not be targets of dispatching calls and
113 -- therefore must not appear in the dispatch table because they already
114 -- utilize class-wide-precondition semantics to handle inheritance and
117 if Is_Suitable_Primitive (New_Op) then
118 Append_Unique_Elmt (New_Op, List);
120 end Add_Dispatching_Operation;
122 --------------------------
123 -- Covered_Interface_Op --
124 --------------------------
126 function Covered_Interface_Op (Prim : Entity_Id) return Entity_Id is
127 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
132 pragma Assert (Is_Dispatching_Operation (Prim));
134 -- Although this is a dispatching primitive we must check if its
135 -- dispatching type is available because it may be the primitive
136 -- of a private type not defined as tagged in its partial view.
138 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
140 -- If the tagged type is frozen then the internal entities associated
141 -- with interfaces are available in the list of primitives of the
142 -- tagged type and can be used to speed up this search.
144 if Is_Frozen (Tagged_Type) then
145 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
146 while Present (Elmt) loop
149 if Present (Interface_Alias (E))
150 and then Alias (E) = Prim
152 return Interface_Alias (E);
158 -- Otherwise we must collect all the interface primitives and check
159 -- if the Prim overrides (implements) some interface primitive.
163 Ifaces_List : Elist_Id;
164 Iface_Elmt : Elmt_Id;
166 Iface_Prim : Entity_Id;
169 Collect_Interfaces (Tagged_Type, Ifaces_List);
170 Iface_Elmt := First_Elmt (Ifaces_List);
171 while Present (Iface_Elmt) loop
172 Iface := Node (Iface_Elmt);
174 Elmt := First_Elmt (Primitive_Operations (Iface));
175 while Present (Elmt) loop
176 Iface_Prim := Node (Elmt);
178 if Chars (Iface_Prim) = Chars (Prim)
179 and then Is_Interface_Conformant
180 (Tagged_Type, Iface_Prim, Prim)
188 Next_Elmt (Iface_Elmt);
195 end Covered_Interface_Op;
197 -------------------------------
198 -- Check_Controlling_Formals --
199 -------------------------------
201 procedure Check_Controlling_Formals
206 Ctrl_Type : Entity_Id;
209 Formal := First_Formal (Subp);
210 while Present (Formal) loop
211 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
213 if Present (Ctrl_Type) then
215 -- Obtain the full type in case we are looking at an incomplete
218 if Ekind (Ctrl_Type) = E_Incomplete_Type
219 and then Present (Full_View (Ctrl_Type))
221 Ctrl_Type := Full_View (Ctrl_Type);
224 -- When controlling type is concurrent and declared within a
225 -- generic or inside an instance use corresponding record type.
227 if Is_Concurrent_Type (Ctrl_Type)
228 and then Present (Corresponding_Record_Type (Ctrl_Type))
230 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
233 if Ctrl_Type = Typ then
234 Set_Is_Controlling_Formal (Formal);
236 -- Ada 2005 (AI-231): Anonymous access types that are used in
237 -- controlling parameters exclude null because it is necessary
238 -- to read the tag to dispatch, and null has no tag.
240 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
241 Set_Can_Never_Be_Null (Etype (Formal));
242 Set_Is_Known_Non_Null (Etype (Formal));
245 -- Check that the parameter's nominal subtype statically
246 -- matches the first subtype.
248 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
249 if not Subtypes_Statically_Match
250 (Typ, Designated_Type (Etype (Formal)))
253 ("parameter subtype does not match controlling type",
257 -- Within a predicate function, the formal may be a subtype
258 -- of a tagged type, given that the predicate is expressed
259 -- in terms of the subtype.
261 elsif not Subtypes_Statically_Match (Typ, Etype (Formal))
262 and then not Is_Predicate_Function (Subp)
265 ("parameter subtype does not match controlling type",
269 if Present (Default_Value (Formal)) then
271 -- In Ada 2005, access parameters can have defaults
273 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
274 and then Ada_Version < Ada_2005
277 ("default not allowed for controlling access parameter",
278 Default_Value (Formal));
280 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
282 ("default expression must be a tag indeterminate" &
283 " function call", Default_Value (Formal));
287 elsif Comes_From_Source (Subp) then
289 ("operation can be dispatching in only one type", Subp);
293 Next_Formal (Formal);
296 if Ekind_In (Subp, E_Function, E_Generic_Function) then
297 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
299 if Present (Ctrl_Type) then
300 if Ctrl_Type = Typ then
301 Set_Has_Controlling_Result (Subp);
303 -- Check that result subtype statically matches first subtype
304 -- (Ada 2005): Subp may have a controlling access result.
306 if Subtypes_Statically_Match (Typ, Etype (Subp))
307 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
309 Subtypes_Statically_Match
310 (Typ, Designated_Type (Etype (Subp))))
316 ("result subtype does not match controlling type", Subp);
319 elsif Comes_From_Source (Subp) then
321 ("operation can be dispatching in only one type", Subp);
325 end Check_Controlling_Formals;
327 ----------------------------
328 -- Check_Controlling_Type --
329 ----------------------------
331 function Check_Controlling_Type
333 Subp : Entity_Id) return Entity_Id
335 Tagged_Type : Entity_Id := Empty;
338 if Is_Tagged_Type (T) then
339 if Is_First_Subtype (T) then
342 Tagged_Type := Base_Type (T);
345 -- If the type is incomplete, it may have been declared without a
346 -- Tagged indication, but the full view may be tagged, in which case
347 -- that is the controlling type of the subprogram. This is one of the
348 -- approx. 579 places in the language where a lookahead would help.
350 elsif Ekind (T) = E_Incomplete_Type
351 and then Present (Full_View (T))
352 and then Is_Tagged_Type (Full_View (T))
354 Set_Is_Tagged_Type (T);
355 Tagged_Type := Full_View (T);
357 elsif Ekind (T) = E_Anonymous_Access_Type
358 and then Is_Tagged_Type (Designated_Type (T))
360 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
361 if Is_First_Subtype (Designated_Type (T)) then
362 Tagged_Type := Designated_Type (T);
364 Tagged_Type := Base_Type (Designated_Type (T));
367 -- Ada 2005: an incomplete type can be tagged. An operation with an
368 -- access parameter of the type is dispatching.
370 elsif Scope (Designated_Type (T)) = Current_Scope then
371 Tagged_Type := Designated_Type (T);
373 -- Ada 2005 (AI-50217)
375 elsif From_Limited_With (Designated_Type (T))
376 and then Has_Non_Limited_View (Designated_Type (T))
377 and then Scope (Designated_Type (T)) = Scope (Subp)
379 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
380 Tagged_Type := Non_Limited_View (Designated_Type (T));
382 Tagged_Type := Base_Type (Non_Limited_View
383 (Designated_Type (T)));
388 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
391 -- The dispatching type and the primitive operation must be defined in
392 -- the same scope, except in the case of internal operations and formal
393 -- abstract subprograms.
395 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
396 and then (not Is_Generic_Type (Tagged_Type)
397 or else not Comes_From_Source (Subp)))
399 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
401 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
403 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
405 Is_Abstract_Subprogram (Subp))
412 end Check_Controlling_Type;
414 ----------------------------
415 -- Check_Dispatching_Call --
416 ----------------------------
418 procedure Check_Dispatching_Call (N : Node_Id) is
419 Loc : constant Source_Ptr := Sloc (N);
422 Control : Node_Id := Empty;
424 Subp_Entity : Entity_Id;
425 Indeterm_Ancestor_Call : Boolean := False;
426 Indeterm_Ctrl_Type : Entity_Id := Empty; -- init to avoid warning
428 Static_Tag : Node_Id := Empty;
429 -- If a controlling formal has a statically tagged actual, the tag of
430 -- this actual is to be used for any tag-indeterminate actual.
432 procedure Check_Direct_Call;
433 -- In the case when the controlling actual is a class-wide type whose
434 -- root type's completion is a task or protected type, the call is in
435 -- fact direct. This routine detects the above case and modifies the
438 procedure Check_Dispatching_Context (Call : Node_Id);
439 -- If the call is tag-indeterminate and the entity being called is
440 -- abstract, verify that the context is a call that will eventually
441 -- provide a tag for dispatching, or has provided one already.
443 -----------------------
444 -- Check_Direct_Call --
445 -----------------------
447 procedure Check_Direct_Call is
448 Typ : Entity_Id := Etype (Control);
450 -- Predefined primitives do not receive wrappers since they are built
451 -- from scratch for the corresponding record of synchronized types.
452 -- Equality is in general predefined, but is excluded from the check
453 -- when it is user-defined.
455 if Is_Predefined_Dispatching_Operation (Subp_Entity)
456 and then not Is_User_Defined_Equality (Subp_Entity)
461 if Is_Class_Wide_Type (Typ) then
462 Typ := Root_Type (Typ);
465 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
466 Typ := Full_View (Typ);
469 if Is_Concurrent_Type (Typ)
471 Present (Corresponding_Record_Type (Typ))
473 Typ := Corresponding_Record_Type (Typ);
475 -- The concurrent record's list of primitives should contain a
476 -- wrapper for the entity of the call, retrieve it.
481 Wrapper_Found : Boolean := False;
484 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
485 while Present (Prim_Elmt) loop
486 Prim := Node (Prim_Elmt);
488 if Is_Primitive_Wrapper (Prim)
489 and then Wrapped_Entity (Prim) = Subp_Entity
491 Wrapper_Found := True;
495 Next_Elmt (Prim_Elmt);
498 -- A primitive declared between two views should have a
499 -- corresponding wrapper.
501 pragma Assert (Wrapper_Found);
503 -- Modify the call by setting the proper entity
505 Set_Entity (Name (N), Prim);
508 end Check_Direct_Call;
510 -------------------------------
511 -- Check_Dispatching_Context --
512 -------------------------------
514 procedure Check_Dispatching_Context (Call : Node_Id) is
515 Subp : constant Entity_Id := Entity (Name (Call));
517 procedure Abstract_Context_Error;
518 -- Error for abstract call dispatching on result is not dispatching
520 ----------------------------
521 -- Abstract_Context_Error --
522 ----------------------------
524 procedure Abstract_Context_Error is
526 if Ekind (Subp) = E_Function then
528 ("call to abstract function must be dispatching", N);
530 -- This error can occur for a procedure in the case of a call to
531 -- an abstract formal procedure with a statically tagged operand.
535 ("call to abstract procedure must be dispatching", N);
537 end Abstract_Context_Error;
541 Scop : constant Entity_Id := Current_Scope_No_Loops;
542 Typ : constant Entity_Id := Etype (Subp);
545 -- Start of processing for Check_Dispatching_Context
548 -- If the called subprogram is a private overriding, replace it
549 -- with its alias, which has the correct body. Verify that the
550 -- two subprograms have the same controlling type (this is not the
551 -- case for an inherited subprogram that has become abstract).
553 if Is_Abstract_Subprogram (Subp)
554 and then No (Controlling_Argument (Call))
556 if Present (Alias (Subp))
557 and then not Is_Abstract_Subprogram (Alias (Subp))
558 and then No (DTC_Entity (Subp))
559 and then Find_Dispatching_Type (Subp) =
560 Find_Dispatching_Type (Alias (Subp))
562 -- Private overriding of inherited abstract operation, call is
565 Set_Entity (Name (N), Alias (Subp));
568 -- An obscure special case: a null procedure may have a class-
569 -- wide pre/postcondition that includes a call to an abstract
570 -- subp. Calls within the expression may not have been rewritten
571 -- as dispatching calls yet, because the null body appears in
572 -- the current declarative part. The expression will be properly
573 -- rewritten/reanalyzed when the postcondition procedure is built.
575 -- Similarly, if this is a pre/postcondition for an abstract
576 -- subprogram, it may call another abstract function which is
577 -- a primitive of an abstract type. The call is non-dispatching
578 -- but will be legal in overridings of the operation.
580 elsif (Is_Subprogram (Scop)
581 or else Chars (Scop) = Name_Postcondition)
583 (Is_Abstract_Subprogram (Scop)
585 (Nkind (Parent (Scop)) = N_Procedure_Specification
586 and then Null_Present (Parent (Scop))))
590 elsif Ekind (Current_Scope) = E_Function
591 and then Nkind (Unit_Declaration_Node (Scop)) =
592 N_Generic_Subprogram_Declaration
597 -- We need to determine whether the context of the call
598 -- provides a tag to make the call dispatching. This requires
599 -- the call to be the actual in an enclosing call, and that
600 -- actual must be controlling. If the call is an operand of
601 -- equality, the other operand must not ve abstract.
603 if not Is_Tagged_Type (Typ)
605 (Ekind (Typ) = E_Anonymous_Access_Type
606 and then Is_Tagged_Type (Designated_Type (Typ)))
608 Abstract_Context_Error;
612 Par := Parent (Call);
614 if Nkind (Par) = N_Parameter_Association then
618 if Nkind (Par) = N_Qualified_Expression
619 or else Nkind (Par) = N_Unchecked_Type_Conversion
624 if Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
625 and then Is_Entity_Name (Name (Par))
628 Enc_Subp : constant Entity_Id := Entity (Name (Par));
632 Ret_Type : Entity_Id;
635 -- Find controlling formal that can provide tag for the
636 -- tag-indeterminate actual. The corresponding actual
637 -- must be the corresponding class-wide type.
639 F := First_Formal (Enc_Subp);
640 A := First_Actual (Par);
642 -- Find controlling type of call. Dereference if function
643 -- returns an access type.
645 Ret_Type := Etype (Call);
646 if Is_Access_Type (Etype (Call)) then
647 Ret_Type := Designated_Type (Ret_Type);
650 while Present (F) loop
651 Control := Etype (A);
653 if Is_Access_Type (Control) then
654 Control := Designated_Type (Control);
657 if Is_Controlling_Formal (F)
658 and then not (Call = A or else Parent (Call) = A)
659 and then Control = Class_Wide_Type (Ret_Type)
668 if Nkind (Par) = N_Function_Call
669 and then Is_Tag_Indeterminate (Par)
671 -- The parent may be an actual of an enclosing call
673 Check_Dispatching_Context (Par);
678 ("call to abstract function must be dispatching",
684 -- For equality operators, one of the operands must be
685 -- statically or dynamically tagged.
687 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
688 if N = Right_Opnd (Par)
689 and then Is_Tag_Indeterminate (Left_Opnd (Par))
691 Abstract_Context_Error;
693 elsif N = Left_Opnd (Par)
694 and then Is_Tag_Indeterminate (Right_Opnd (Par))
696 Abstract_Context_Error;
701 -- The left-hand side of an assignment provides the tag
703 elsif Nkind (Par) = N_Assignment_Statement then
707 Abstract_Context_Error;
711 end Check_Dispatching_Context;
713 -- Start of processing for Check_Dispatching_Call
716 -- Find a controlling argument, if any
718 if Present (Parameter_Associations (N)) then
719 Subp_Entity := Entity (Name (N));
721 Actual := First_Actual (N);
722 Formal := First_Formal (Subp_Entity);
723 while Present (Actual) loop
724 Control := Find_Controlling_Arg (Actual);
725 exit when Present (Control);
727 -- Check for the case where the actual is a tag-indeterminate call
728 -- whose result type is different than the tagged type associated
729 -- with the containing call, but is an ancestor of the type.
731 if Is_Controlling_Formal (Formal)
732 and then Is_Tag_Indeterminate (Actual)
733 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
734 and then Is_Ancestor (Etype (Actual), Etype (Formal))
736 Indeterm_Ancestor_Call := True;
737 Indeterm_Ctrl_Type := Etype (Formal);
739 -- If the formal is controlling but the actual is not, the type
740 -- of the actual is statically known, and may be used as the
741 -- controlling tag for some other tag-indeterminate actual.
743 elsif Is_Controlling_Formal (Formal)
744 and then Is_Entity_Name (Actual)
745 and then Is_Tagged_Type (Etype (Actual))
747 Static_Tag := Actual;
750 Next_Actual (Actual);
751 Next_Formal (Formal);
754 -- If the call doesn't have a controlling actual but does have an
755 -- indeterminate actual that requires dispatching treatment, then an
756 -- object is needed that will serve as the controlling argument for
757 -- a dispatching call on the indeterminate actual. This can occur
758 -- in the unusual situation of a default actual given by a tag-
759 -- indeterminate call and where the type of the call is an ancestor
760 -- of the type associated with a containing call to an inherited
761 -- operation (see AI-239).
763 -- Rather than create an object of the tagged type, which would
764 -- be problematic for various reasons (default initialization,
765 -- discriminants), the tag of the containing call's associated
766 -- tagged type is directly used to control the dispatching.
769 and then Indeterm_Ancestor_Call
770 and then No (Static_Tag)
773 Make_Attribute_Reference (Loc,
774 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
775 Attribute_Name => Name_Tag);
780 if Present (Control) then
782 -- Verify that no controlling arguments are statically tagged
785 Write_Str ("Found Dispatching call");
790 Actual := First_Actual (N);
791 while Present (Actual) loop
792 if Actual /= Control then
794 if not Is_Controlling_Actual (Actual) then
795 null; -- Can be anything
797 elsif Is_Dynamically_Tagged (Actual) then
798 null; -- Valid parameter
800 elsif Is_Tag_Indeterminate (Actual) then
802 -- The tag is inherited from the enclosing call (the node
803 -- we are currently analyzing). Explicitly expand the
804 -- actual, since the previous call to Expand (from
805 -- Resolve_Call) had no way of knowing about the
806 -- required dispatching.
808 Propagate_Tag (Control, Actual);
812 ("controlling argument is not dynamically tagged",
818 Next_Actual (Actual);
821 -- Mark call as a dispatching call
823 Set_Controlling_Argument (N, Control);
824 Check_Restriction (No_Dispatching_Calls, N);
826 -- The dispatching call may need to be converted into a direct
827 -- call in certain cases.
831 -- If there is a statically tagged actual and a tag-indeterminate
832 -- call to a function of the ancestor (such as that provided by a
833 -- default), then treat this as a dispatching call and propagate
834 -- the tag to the tag-indeterminate call(s).
836 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
838 Make_Attribute_Reference (Loc,
840 New_Occurrence_Of (Etype (Static_Tag), Loc),
841 Attribute_Name => Name_Tag);
845 Actual := First_Actual (N);
846 Formal := First_Formal (Subp_Entity);
847 while Present (Actual) loop
848 if Is_Tag_Indeterminate (Actual)
849 and then Is_Controlling_Formal (Formal)
851 Propagate_Tag (Control, Actual);
854 Next_Actual (Actual);
855 Next_Formal (Formal);
858 Check_Dispatching_Context (N);
860 elsif Nkind (N) /= N_Function_Call then
862 -- The call is not dispatching, so check that there aren't any
863 -- tag-indeterminate abstract calls left among its actuals.
865 Actual := First_Actual (N);
866 while Present (Actual) loop
867 if Is_Tag_Indeterminate (Actual) then
869 -- Function call case
871 if Nkind (Original_Node (Actual)) = N_Function_Call then
872 Func := Entity (Name (Original_Node (Actual)));
874 -- If the actual is an attribute then it can't be abstract
875 -- (the only current case of a tag-indeterminate attribute
876 -- is the stream Input attribute).
878 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
882 -- Ditto if it is an explicit dereference
884 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
888 -- Only other possibility is a qualified expression whose
889 -- constituent expression is itself a call.
893 Entity (Name (Original_Node
894 (Expression (Original_Node (Actual)))));
897 if Present (Func) and then Is_Abstract_Subprogram (Func) then
899 ("call to abstract function must be dispatching",
904 Next_Actual (Actual);
907 Check_Dispatching_Context (N);
910 elsif Nkind (Parent (N)) in N_Subexpr then
911 Check_Dispatching_Context (N);
913 elsif Nkind (Parent (N)) = N_Assignment_Statement
914 and then Is_Class_Wide_Type (Etype (Name (Parent (N))))
918 elsif Is_Abstract_Subprogram (Subp_Entity) then
919 Check_Dispatching_Context (N);
924 -- If dispatching on result, the enclosing call, if any, will
925 -- determine the controlling argument. Otherwise this is the
926 -- primitive operation of the root type.
928 Check_Dispatching_Context (N);
930 end Check_Dispatching_Call;
932 ---------------------------------
933 -- Check_Dispatching_Operation --
934 ---------------------------------
936 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
937 procedure Warn_On_Late_Primitive_After_Private_Extension
940 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
941 -- if it is a public primitive defined after some private extension of
944 ----------------------------------------------------
945 -- Warn_On_Late_Primitive_After_Private_Extension --
946 ----------------------------------------------------
948 procedure Warn_On_Late_Primitive_After_Private_Extension
955 if Warn_On_Late_Primitives
956 and then Comes_From_Source (Prim)
957 and then Has_Private_Extension (Typ)
958 and then Is_Package_Or_Generic_Package (Current_Scope)
959 and then not In_Private_Part (Current_Scope)
961 E := Next_Entity (Typ);
964 if Ekind (E) = E_Record_Type_With_Private
965 and then Etype (E) = Typ
967 Error_Msg_Name_1 := Chars (Typ);
968 Error_Msg_Name_2 := Chars (E);
969 Error_Msg_Sloc := Sloc (E);
971 ("?j?primitive of type % defined after private extension "
973 Error_Msg_Name_1 := Chars (Prim);
974 Error_Msg_Name_2 := Chars (E);
976 ("\spec of % should appear before declaration of type %!",
984 end Warn_On_Late_Primitive_After_Private_Extension;
988 Body_Is_Last_Primitive : Boolean := False;
989 Has_Dispatching_Parent : Boolean := False;
990 Ovr_Subp : Entity_Id := Empty;
991 Tagged_Type : Entity_Id;
993 -- Start of processing for Check_Dispatching_Operation
996 if not Ekind_In (Subp, E_Function, E_Procedure) then
999 -- The Default_Initial_Condition procedure is not a primitive subprogram
1000 -- even if it relates to a tagged type. This routine is not meant to be
1001 -- inherited or overridden.
1003 elsif Is_DIC_Procedure (Subp) then
1006 -- The "partial" and "full" type invariant procedures are not primitive
1007 -- subprograms even if they relate to a tagged type. These routines are
1008 -- not meant to be inherited or overridden.
1010 elsif Is_Invariant_Procedure (Subp)
1011 or else Is_Partial_Invariant_Procedure (Subp)
1016 Set_Is_Dispatching_Operation (Subp, False);
1017 Tagged_Type := Find_Dispatching_Type (Subp);
1019 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1020 -- Required because primitives of concurrent types are attached
1021 -- to the corresponding record (not to the concurrent type).
1023 if Ada_Version >= Ada_2005
1024 and then Present (Tagged_Type)
1025 and then Is_Concurrent_Type (Tagged_Type)
1026 and then Present (Corresponding_Record_Type (Tagged_Type))
1028 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
1031 -- (AI-345): The task body procedure is not a primitive of the tagged
1034 if Present (Tagged_Type)
1035 and then Is_Concurrent_Record_Type (Tagged_Type)
1036 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
1037 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
1038 and then Subp = Get_Task_Body_Procedure
1039 (Corresponding_Concurrent_Type (Tagged_Type))
1044 -- If Subp is derived from a dispatching operation then it should
1045 -- always be treated as dispatching. In this case various checks
1046 -- below will be bypassed. Makes sure that late declarations for
1047 -- inherited private subprograms are treated as dispatching, even
1048 -- if the associated tagged type is already frozen.
1050 Has_Dispatching_Parent :=
1051 Present (Alias (Subp))
1052 and then Is_Dispatching_Operation (Alias (Subp));
1054 if No (Tagged_Type) then
1056 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1057 -- with an abstract interface type unless the interface acts as a
1058 -- parent type in a derivation. If the interface type is a formal
1059 -- type then the operation is not primitive and therefore legal.
1066 E := First_Entity (Subp);
1067 while Present (E) loop
1069 -- For an access parameter, check designated type
1071 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
1072 Typ := Designated_Type (Etype (E));
1077 if Comes_From_Source (Subp)
1078 and then Is_Interface (Typ)
1079 and then not Is_Class_Wide_Type (Typ)
1080 and then not Is_Derived_Type (Typ)
1081 and then not Is_Generic_Type (Typ)
1082 and then not In_Instance
1084 Error_Msg_N ("??declaration of& is too late!", Subp);
1085 Error_Msg_NE -- CODEFIX??
1086 ("\??spec should appear immediately after declaration of "
1087 & "& !", Subp, Typ);
1094 -- In case of functions check also the result type
1096 if Ekind (Subp) = E_Function then
1097 if Is_Access_Type (Etype (Subp)) then
1098 Typ := Designated_Type (Etype (Subp));
1100 Typ := Etype (Subp);
1103 -- The following should be better commented, especially since
1104 -- we just added several new conditions here ???
1106 if Comes_From_Source (Subp)
1107 and then Is_Interface (Typ)
1108 and then not Is_Class_Wide_Type (Typ)
1109 and then not Is_Derived_Type (Typ)
1110 and then not Is_Generic_Type (Typ)
1111 and then not In_Instance
1113 Error_Msg_N ("??declaration of& is too late!", Subp);
1115 ("\??spec should appear immediately after declaration of "
1116 & "& !", Subp, Typ);
1123 -- The subprograms build internally after the freezing point (such as
1124 -- init procs, interface thunks, type support subprograms, and Offset
1125 -- to top functions for accessing interface components in variable
1126 -- size tagged types) are not primitives.
1128 elsif Is_Frozen (Tagged_Type)
1129 and then not Comes_From_Source (Subp)
1130 and then not Has_Dispatching_Parent
1132 -- Complete decoration of internally built subprograms that override
1133 -- a dispatching primitive. These entities correspond with the
1136 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1137 -- to override functions of nonabstract null extensions. These
1138 -- primitives were added to the list of primitives of the tagged
1139 -- type by Make_Controlling_Function_Wrappers. However, attribute
1140 -- Is_Dispatching_Operation must be set to true.
1142 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1145 -- 3. Subprograms associated with stream attributes (built by
1146 -- New_Stream_Subprogram)
1148 -- 4. Wrappers built for inherited operations with inherited class-
1149 -- wide conditions, where the conditions include calls to other
1150 -- overridden primitives. The wrappers include checks on these
1151 -- modified conditions. (AI12-113).
1153 -- 5. Declarations built for subprograms without separate specs that
1154 -- are eligible for inlining in GNATprove (inside
1155 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1157 if Present (Old_Subp)
1158 and then Present (Overridden_Operation (Subp))
1159 and then Is_Dispatching_Operation (Old_Subp)
1162 ((Ekind (Subp) = E_Function
1163 and then Is_Dispatching_Operation (Old_Subp)
1164 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1167 (Ekind (Subp) = E_Procedure
1168 and then Is_Dispatching_Operation (Old_Subp)
1169 and then Present (Alias (Old_Subp))
1170 and then Is_Null_Interface_Primitive
1171 (Ultimate_Alias (Old_Subp)))
1173 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1174 or else Get_TSS_Name (Subp) = TSS_Stream_Write
1176 or else Present (Contract (Overridden_Operation (Subp)))
1178 or else GNATprove_Mode);
1180 Check_Controlling_Formals (Tagged_Type, Subp);
1181 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1182 Set_Is_Dispatching_Operation (Subp);
1187 -- The operation may be a child unit, whose scope is the defining
1188 -- package, but which is not a primitive operation of the type.
1190 elsif Is_Child_Unit (Subp) then
1193 -- If the subprogram is not defined in a package spec, the only case
1194 -- where it can be a dispatching op is when it overrides an operation
1195 -- before the freezing point of the type.
1197 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1198 or else In_Package_Body (Scope (Subp)))
1199 and then not Has_Dispatching_Parent
1201 if not Comes_From_Source (Subp)
1202 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1206 -- If the type is already frozen, the overriding is not allowed
1207 -- except when Old_Subp is not a dispatching operation (which can
1208 -- occur when Old_Subp was inherited by an untagged type). However,
1209 -- a body with no previous spec freezes the type *after* its
1210 -- declaration, and therefore is a legal overriding (unless the type
1211 -- has already been frozen). Only the first such body is legal.
1213 elsif Present (Old_Subp)
1214 and then Is_Dispatching_Operation (Old_Subp)
1216 if Comes_From_Source (Subp)
1218 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1219 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1222 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1223 Decl_Item : Node_Id;
1226 -- ??? The checks here for whether the type has been frozen
1227 -- prior to the new body are not complete. It's not simple
1228 -- to check frozenness at this point since the body has
1229 -- already caused the type to be prematurely frozen in
1230 -- Analyze_Declarations, but we're forced to recheck this
1231 -- here because of the odd rule interpretation that allows
1232 -- the overriding if the type wasn't frozen prior to the
1233 -- body. The freezing action should probably be delayed
1234 -- until after the spec is seen, but that's a tricky
1235 -- change to the delicate freezing code.
1237 -- Look at each declaration following the type up until the
1238 -- new subprogram body. If any of the declarations is a body
1239 -- then the type has been frozen already so the overriding
1240 -- primitive is illegal.
1242 Decl_Item := Next (Parent (Tagged_Type));
1243 while Present (Decl_Item)
1244 and then (Decl_Item /= Subp_Body)
1246 if Comes_From_Source (Decl_Item)
1247 and then (Nkind (Decl_Item) in N_Proper_Body
1248 or else Nkind (Decl_Item) in N_Body_Stub)
1250 Error_Msg_N ("overriding of& is too late!", Subp);
1252 ("\spec should appear immediately after the type!",
1260 -- If the subprogram doesn't follow in the list of
1261 -- declarations including the type then the type has
1262 -- definitely been frozen already and the body is illegal.
1264 if No (Decl_Item) then
1265 Error_Msg_N ("overriding of& is too late!", Subp);
1267 ("\spec should appear immediately after the type!",
1270 elsif Is_Frozen (Subp) then
1272 -- The subprogram body declares a primitive operation.
1273 -- If the subprogram is already frozen, we must update
1274 -- its dispatching information explicitly here. The
1275 -- information is taken from the overridden subprogram.
1276 -- We must also generate a cross-reference entry because
1277 -- references to other primitives were already created
1278 -- when type was frozen.
1280 Body_Is_Last_Primitive := True;
1282 if Present (DTC_Entity (Old_Subp)) then
1283 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1284 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1286 if not Restriction_Active (No_Dispatching_Calls) then
1287 if Building_Static_DT (Tagged_Type) then
1289 -- If the static dispatch table has not been
1290 -- built then there is nothing else to do now;
1291 -- otherwise we notify that we cannot build the
1292 -- static dispatch table.
1294 if Has_Dispatch_Table (Tagged_Type) then
1296 ("overriding of& is too late for building "
1297 & " static dispatch tables!", Subp);
1299 ("\spec should appear immediately after "
1300 & "the type!", Subp);
1303 -- No code required to register primitives in VM
1306 elsif not Tagged_Type_Expansion then
1310 Insert_Actions_After (Subp_Body,
1311 Register_Primitive (Sloc (Subp_Body),
1315 -- Indicate that this is an overriding operation,
1316 -- and replace the overridden entry in the list of
1317 -- primitive operations, which is used for xref
1318 -- generation subsequently.
1320 Generate_Reference (Tagged_Type, Subp, 'P', False);
1321 Override_Dispatching_Operation
1322 (Tagged_Type, Old_Subp, Subp);
1329 Error_Msg_N ("overriding of& is too late!", Subp);
1331 ("\subprogram spec should appear immediately after the type!",
1335 -- If the type is not frozen yet and we are not in the overriding
1336 -- case it looks suspiciously like an attempt to define a primitive
1337 -- operation, which requires the declaration to be in a package spec
1338 -- (3.2.3(6)). Only report cases where the type and subprogram are
1339 -- in the same declaration list (by checking the enclosing parent
1340 -- declarations), to avoid spurious warnings on subprograms in
1341 -- instance bodies when the type is declared in the instance spec
1342 -- but hasn't been frozen by the instance body.
1344 elsif not Is_Frozen (Tagged_Type)
1345 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1348 ("??not dispatching (must be defined in a package spec)", Subp);
1351 -- When the type is frozen, it is legitimate to define a new
1352 -- non-primitive operation.
1358 -- Now, we are sure that the scope is a package spec. If the subprogram
1359 -- is declared after the freezing point of the type that's an error
1361 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1362 Error_Msg_N ("this primitive operation is declared too late", Subp);
1364 ("??no primitive operations for& after this line",
1365 Freeze_Node (Tagged_Type),
1370 Check_Controlling_Formals (Tagged_Type, Subp);
1372 Ovr_Subp := Old_Subp;
1374 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1375 -- overridden by Subp. This only applies to source subprograms, and
1376 -- their declaration must carry an explicit overriding indicator.
1379 and then Ada_Version >= Ada_2012
1380 and then Comes_From_Source (Subp)
1382 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1384 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1386 -- Verify that the proper overriding indicator has been supplied.
1388 if Present (Ovr_Subp)
1390 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1392 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1396 -- Now it should be a correct primitive operation, put it in the list
1398 if Present (Ovr_Subp) then
1400 -- If the type has interfaces we complete this check after we set
1401 -- attribute Is_Dispatching_Operation.
1403 Check_Subtype_Conformant (Subp, Ovr_Subp);
1405 -- A primitive operation with the name of a primitive controlled
1406 -- operation does not override a non-visible overriding controlled
1407 -- operation, i.e. one declared in a private part when the full
1408 -- view of a type is controlled. Conversely, it will override a
1409 -- visible operation that may be declared in a partial view when
1410 -- the full view is controlled.
1412 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1413 and then Is_Controlled (Tagged_Type)
1414 and then not Is_Visibly_Controlled (Tagged_Type)
1415 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1417 Set_Overridden_Operation (Subp, Empty);
1419 -- If the subprogram specification carries an overriding
1420 -- indicator, no need for the warning: it is either redundant,
1421 -- or else an error will be reported.
1423 if Nkind (Parent (Subp)) = N_Procedure_Specification
1425 (Must_Override (Parent (Subp))
1426 or else Must_Not_Override (Parent (Subp)))
1430 -- Here we need the warning
1434 ("operation does not override inherited&??", Subp, Subp);
1438 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1440 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1441 -- that covers abstract interface subprograms we must register it
1442 -- in all the secondary dispatch tables associated with abstract
1443 -- interfaces. We do this now only if not building static tables,
1444 -- nor when the expander is inactive (we avoid trying to register
1445 -- primitives in semantics-only mode, since the type may not have
1446 -- an associated dispatch table). Otherwise the patch code is
1447 -- emitted after those tables are built, to prevent access before
1448 -- elaboration in gigi.
1450 if Body_Is_Last_Primitive and then Expander_Active then
1452 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1457 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1458 while Present (Elmt) loop
1459 Prim := Node (Elmt);
1461 -- No code required to register primitives in VM targets
1463 if Present (Alias (Prim))
1464 and then Present (Interface_Alias (Prim))
1465 and then Alias (Prim) = Subp
1466 and then not Building_Static_DT (Tagged_Type)
1467 and then Tagged_Type_Expansion
1469 Insert_Actions_After (Subp_Body,
1470 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1476 -- Redisplay the contents of the updated dispatch table
1478 if Debug_Flag_ZZ then
1479 Write_Str ("Late overriding: ");
1480 Write_DT (Tagged_Type);
1486 -- If no old subprogram, then we add this as a dispatching operation,
1487 -- but we avoid doing this if an error was posted, to prevent annoying
1490 elsif not Error_Posted (Subp) then
1491 Add_Dispatching_Operation (Tagged_Type, Subp);
1494 Set_Is_Dispatching_Operation (Subp, True);
1496 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1497 -- subtype conformance against all the interfaces covered by this
1500 if Present (Ovr_Subp)
1501 and then Has_Interfaces (Tagged_Type)
1504 Ifaces_List : Elist_Id;
1505 Iface_Elmt : Elmt_Id;
1506 Iface_Prim_Elmt : Elmt_Id;
1507 Iface_Prim : Entity_Id;
1508 Ret_Typ : Entity_Id;
1511 Collect_Interfaces (Tagged_Type, Ifaces_List);
1513 Iface_Elmt := First_Elmt (Ifaces_List);
1514 while Present (Iface_Elmt) loop
1515 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1517 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1518 while Present (Iface_Prim_Elmt) loop
1519 Iface_Prim := Node (Iface_Prim_Elmt);
1521 if Is_Interface_Conformant
1522 (Tagged_Type, Iface_Prim, Subp)
1524 -- Handle procedures, functions whose return type
1525 -- matches, or functions not returning interfaces
1527 if Ekind (Subp) = E_Procedure
1528 or else Etype (Iface_Prim) = Etype (Subp)
1529 or else not Is_Interface (Etype (Iface_Prim))
1531 Check_Subtype_Conformant
1533 Old_Id => Iface_Prim,
1535 Skip_Controlling_Formals => True);
1537 -- Handle functions returning interfaces
1539 elsif Implements_Interface
1540 (Etype (Subp), Etype (Iface_Prim))
1542 -- Temporarily force both entities to return the
1543 -- same type. Required because Subtype_Conformant
1544 -- does not handle this case.
1546 Ret_Typ := Etype (Iface_Prim);
1547 Set_Etype (Iface_Prim, Etype (Subp));
1549 Check_Subtype_Conformant
1551 Old_Id => Iface_Prim,
1553 Skip_Controlling_Formals => True);
1555 Set_Etype (Iface_Prim, Ret_Typ);
1559 Next_Elmt (Iface_Prim_Elmt);
1563 Next_Elmt (Iface_Elmt);
1568 if not Body_Is_Last_Primitive then
1569 Set_DT_Position_Value (Subp, No_Uint);
1571 elsif Has_Controlled_Component (Tagged_Type)
1572 and then Nam_In (Chars (Subp), Name_Initialize,
1575 Name_Finalize_Address)
1578 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1582 Old_Spec : Entity_Id;
1584 C_Names : constant array (1 .. 4) of Name_Id :=
1588 Name_Finalize_Address);
1590 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1591 (TSS_Deep_Initialize,
1594 TSS_Finalize_Address);
1597 -- Remove previous controlled function which was constructed and
1598 -- analyzed when the type was frozen. This requires removing the
1599 -- body of the redefined primitive, as well as its specification
1600 -- if needed (there is no spec created for Deep_Initialize, see
1601 -- exp_ch3.adb). We must also dismantle the exception information
1602 -- that may have been generated for it when front end zero-cost
1603 -- tables are enabled.
1605 for J in D_Names'Range loop
1606 Old_P := TSS (Tagged_Type, D_Names (J));
1609 and then Chars (Subp) = C_Names (J)
1611 Old_Bod := Unit_Declaration_Node (Old_P);
1613 Set_Is_Eliminated (Old_P);
1614 Set_Scope (Old_P, Scope (Current_Scope));
1616 if Nkind (Old_Bod) = N_Subprogram_Body
1617 and then Present (Corresponding_Spec (Old_Bod))
1619 Old_Spec := Corresponding_Spec (Old_Bod);
1620 Set_Has_Completion (Old_Spec, False);
1625 Build_Late_Proc (Tagged_Type, Chars (Subp));
1627 -- The new operation is added to the actions of the freeze node
1628 -- for the type, but this node has already been analyzed, so we
1629 -- must retrieve and analyze explicitly the new body.
1632 and then Present (Actions (F_Node))
1634 Decl := Last (Actions (F_Node));
1640 -- AI12-0279: If the Yield aspect is specified for a dispatching
1641 -- subprogram that inherits the aspect, the specified value shall
1644 if Is_Dispatching_Operation (Subp)
1645 and then Is_Primitive_Wrapper (Subp)
1646 and then Present (Wrapped_Entity (Subp))
1647 and then Comes_From_Source (Wrapped_Entity (Subp))
1648 and then Present (Overridden_Operation (Subp))
1649 and then Has_Yield_Aspect (Overridden_Operation (Subp))
1650 /= Has_Yield_Aspect (Wrapped_Entity (Subp))
1653 W_Ent : constant Entity_Id := Wrapped_Entity (Subp);
1654 W_Decl : constant Node_Id := Parent (W_Ent);
1658 if Present (Aspect_Specifications (W_Decl)) then
1659 Asp := First (Aspect_Specifications (W_Decl));
1660 while Present (Asp) loop
1661 if Chars (Identifier (Asp)) = Name_Yield then
1662 Error_Msg_Name_1 := Name_Yield;
1664 ("specification of inherited aspect% can only confirm "
1665 & "parent value", Asp);
1672 Set_Has_Yield_Aspect (Wrapped_Entity (Subp));
1676 -- For similarity with record extensions, in Ada 9X the language should
1677 -- have disallowed adding visible operations to a tagged type after
1678 -- deriving a private extension from it. Report a warning if this
1679 -- primitive is defined after a private extension of Tagged_Type.
1681 Warn_On_Late_Primitive_After_Private_Extension (Tagged_Type, Subp);
1682 end Check_Dispatching_Operation;
1684 ------------------------------------------
1685 -- Check_Operation_From_Incomplete_Type --
1686 ------------------------------------------
1688 procedure Check_Operation_From_Incomplete_Type
1692 Full : constant Entity_Id := Full_View (Typ);
1693 Parent_Typ : constant Entity_Id := Etype (Full);
1694 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1695 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1697 Prev : Elmt_Id := No_Elmt;
1699 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1700 -- Check that Subp has profile of an operation derived from Parent_Subp.
1701 -- Subp must have a parameter or result type that is Typ or an access
1702 -- parameter or access result type that designates Typ.
1708 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1712 if Chars (Parent_Subp) /= Chars (Subp) then
1716 -- Check that the type of controlling formals is derived from the
1717 -- parent subprogram's controlling formal type (or designated type
1718 -- if the formal type is an anonymous access type).
1720 F1 := First_Formal (Parent_Subp);
1721 F2 := First_Formal (Subp);
1722 while Present (F1) and then Present (F2) loop
1723 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1724 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1726 elsif Designated_Type (Etype (F1)) = Parent_Typ
1727 and then Designated_Type (Etype (F2)) /= Full
1732 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1735 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1743 -- Check that a controlling result type is derived from the parent
1744 -- subprogram's result type (or designated type if the result type
1745 -- is an anonymous access type).
1747 if Ekind (Parent_Subp) = E_Function then
1748 if Ekind (Subp) /= E_Function then
1751 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1752 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1755 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1756 and then Designated_Type (Etype (Subp)) /= Full
1761 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1764 elsif Etype (Parent_Subp) = Parent_Typ
1765 and then Etype (Subp) /= Full
1770 elsif Ekind (Subp) = E_Function then
1774 return No (F1) and then No (F2);
1777 -- Start of processing for Check_Operation_From_Incomplete_Type
1780 -- The operation may override an inherited one, or may be a new one
1781 -- altogether. The inherited operation will have been hidden by the
1782 -- current one at the point of the type derivation, so it does not
1783 -- appear in the list of primitive operations of the type. We have to
1784 -- find the proper place of insertion in the list of primitive opera-
1785 -- tions by iterating over the list for the parent type.
1787 Op1 := First_Elmt (Old_Prim);
1788 Op2 := First_Elmt (New_Prim);
1789 while Present (Op1) and then Present (Op2) loop
1790 if Derives_From (Node (Op1)) then
1793 -- Avoid adding it to the list of primitives if already there
1795 if Node (Op2) /= Subp then
1796 Prepend_Elmt (Subp, New_Prim);
1800 Insert_Elmt_After (Subp, Prev);
1811 -- Operation is a new primitive
1813 Append_Elmt (Subp, New_Prim);
1814 end Check_Operation_From_Incomplete_Type;
1816 ---------------------------------------
1817 -- Check_Operation_From_Private_View --
1818 ---------------------------------------
1820 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1821 Tagged_Type : Entity_Id;
1824 if Is_Dispatching_Operation (Alias (Subp)) then
1825 Set_Scope (Subp, Current_Scope);
1826 Tagged_Type := Find_Dispatching_Type (Subp);
1828 -- Add Old_Subp to primitive operations if not already present
1830 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1831 Add_Dispatching_Operation (Tagged_Type, Old_Subp);
1833 -- If Old_Subp isn't already marked as dispatching then this is
1834 -- the case of an operation of an untagged private type fulfilled
1835 -- by a tagged type that overrides an inherited dispatching
1836 -- operation, so we set the necessary dispatching attributes here.
1838 if not Is_Dispatching_Operation (Old_Subp) then
1840 -- If the untagged type has no discriminants, and the full
1841 -- view is constrained, there will be a spurious mismatch of
1842 -- subtypes on the controlling arguments, because the tagged
1843 -- type is the internal base type introduced in the derivation.
1844 -- Use the original type to verify conformance, rather than the
1847 if not Comes_From_Source (Tagged_Type)
1848 and then Has_Discriminants (Tagged_Type)
1854 Formal := First_Formal (Old_Subp);
1855 while Present (Formal) loop
1856 if Tagged_Type = Base_Type (Etype (Formal)) then
1857 Tagged_Type := Etype (Formal);
1860 Next_Formal (Formal);
1864 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1865 Tagged_Type := Etype (Old_Subp);
1869 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1870 Set_Is_Dispatching_Operation (Old_Subp, True);
1871 Set_DT_Position_Value (Old_Subp, No_Uint);
1874 -- If the old subprogram is an explicit renaming of some other
1875 -- entity, it is not overridden by the inherited subprogram.
1876 -- Otherwise, update its alias and other attributes.
1878 if Present (Alias (Old_Subp))
1879 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1880 N_Subprogram_Renaming_Declaration
1882 Set_Alias (Old_Subp, Alias (Subp));
1884 -- The derived subprogram should inherit the abstractness of
1885 -- the parent subprogram (except in the case of a function
1886 -- returning the type). This sets the abstractness properly
1887 -- for cases where a private extension may have inherited an
1888 -- abstract operation, but the full type is derived from a
1889 -- descendant type and inherits a nonabstract version.
1891 if Etype (Subp) /= Tagged_Type then
1892 Set_Is_Abstract_Subprogram
1893 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1898 end Check_Operation_From_Private_View;
1900 --------------------------
1901 -- Find_Controlling_Arg --
1902 --------------------------
1904 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1905 Orig_Node : constant Node_Id := Original_Node (N);
1909 if Nkind (Orig_Node) = N_Qualified_Expression then
1910 return Find_Controlling_Arg (Expression (Orig_Node));
1913 -- Dispatching on result case. If expansion is disabled, the node still
1914 -- has the structure of a function call. However, if the function name
1915 -- is an operator and the call was given in infix form, the original
1916 -- node has no controlling result and we must examine the current node.
1918 if Nkind (N) = N_Function_Call
1919 and then Present (Controlling_Argument (N))
1920 and then Has_Controlling_Result (Entity (Name (N)))
1922 return Controlling_Argument (N);
1924 -- If expansion is enabled, the call may have been transformed into
1925 -- an indirect call, and we need to recover the original node.
1927 elsif Nkind (Orig_Node) = N_Function_Call
1928 and then Present (Controlling_Argument (Orig_Node))
1929 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1931 return Controlling_Argument (Orig_Node);
1933 -- Type conversions are dynamically tagged if the target type, or its
1934 -- designated type, are classwide. An interface conversion expands into
1935 -- a dereference, so test must be performed on the original node.
1937 elsif Nkind (Orig_Node) = N_Type_Conversion
1938 and then Nkind (N) = N_Explicit_Dereference
1939 and then Is_Controlling_Actual (N)
1942 Target_Type : constant Entity_Id :=
1943 Entity (Subtype_Mark (Orig_Node));
1946 if Is_Class_Wide_Type (Target_Type) then
1949 elsif Is_Access_Type (Target_Type)
1950 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1961 elsif Is_Controlling_Actual (N)
1963 (Nkind (Parent (N)) = N_Qualified_Expression
1964 and then Is_Controlling_Actual (Parent (N)))
1968 if Is_Access_Type (Typ) then
1970 -- In the case of an Access attribute, use the type of the prefix,
1971 -- since in the case of an actual for an access parameter, the
1972 -- attribute's type may be of a specific designated type, even
1973 -- though the prefix type is class-wide.
1975 if Nkind (N) = N_Attribute_Reference then
1976 Typ := Etype (Prefix (N));
1978 -- An allocator is dispatching if the type of qualified expression
1979 -- is class_wide, in which case this is the controlling type.
1981 elsif Nkind (Orig_Node) = N_Allocator
1982 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1984 Typ := Etype (Expression (Orig_Node));
1986 Typ := Designated_Type (Typ);
1990 if Is_Class_Wide_Type (Typ)
1992 (Nkind (Parent (N)) = N_Qualified_Expression
1993 and then Is_Access_Type (Etype (N))
1994 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
2001 end Find_Controlling_Arg;
2003 ---------------------------
2004 -- Find_Dispatching_Type --
2005 ---------------------------
2007 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
2008 A_Formal : Entity_Id;
2010 Ctrl_Type : Entity_Id;
2013 if Ekind_In (Subp, E_Function, E_Procedure)
2014 and then Present (DTC_Entity (Subp))
2016 return Scope (DTC_Entity (Subp));
2018 -- For subprograms internally generated by derivations of tagged types
2019 -- use the alias subprogram as a reference to locate the dispatching
2022 elsif not Comes_From_Source (Subp)
2023 and then Present (Alias (Subp))
2024 and then Is_Dispatching_Operation (Alias (Subp))
2026 if Ekind (Alias (Subp)) = E_Function
2027 and then Has_Controlling_Result (Alias (Subp))
2029 return Check_Controlling_Type (Etype (Subp), Subp);
2032 Formal := First_Formal (Subp);
2033 A_Formal := First_Formal (Alias (Subp));
2034 while Present (A_Formal) loop
2035 if Is_Controlling_Formal (A_Formal) then
2036 return Check_Controlling_Type (Etype (Formal), Subp);
2039 Next_Formal (Formal);
2040 Next_Formal (A_Formal);
2043 pragma Assert (False);
2050 Formal := First_Formal (Subp);
2051 while Present (Formal) loop
2052 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
2054 if Present (Ctrl_Type) then
2058 Next_Formal (Formal);
2061 -- The subprogram may also be dispatching on result
2063 if Present (Etype (Subp)) then
2064 return Check_Controlling_Type (Etype (Subp), Subp);
2068 pragma Assert (not Is_Dispatching_Operation (Subp));
2070 end Find_Dispatching_Type;
2072 --------------------------------------
2073 -- Find_Hidden_Overridden_Primitive --
2074 --------------------------------------
2076 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
2078 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
2080 Orig_Prim : Entity_Id;
2082 Vis_List : Elist_Id;
2085 -- This Ada 2012 rule applies only for type extensions or private
2086 -- extensions, where the parent type is not in a parent unit, and
2087 -- where an operation is never declared but still inherited.
2090 or else not Is_Record_Type (Tag_Typ)
2091 or else Etype (Tag_Typ) = Tag_Typ
2092 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
2097 -- Collect the list of visible ancestor of the tagged type
2099 Vis_List := Visible_Ancestors (Tag_Typ);
2101 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2102 while Present (Elmt) loop
2103 Prim := Node (Elmt);
2105 -- Find an inherited hidden dispatching primitive with the name of S
2106 -- and a type-conformant profile.
2108 if Present (Alias (Prim))
2109 and then Is_Hidden (Alias (Prim))
2110 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
2111 and then Primitive_Names_Match (S, Prim)
2112 and then Type_Conformant (S, Prim)
2115 Vis_Ancestor : Elmt_Id;
2119 -- The original corresponding operation of Prim must be an
2120 -- operation of a visible ancestor of the dispatching type S,
2121 -- and the original corresponding operation of S2 must be
2124 Orig_Prim := Original_Corresponding_Operation (Prim);
2126 if Orig_Prim /= Prim
2127 and then Is_Immediately_Visible (Orig_Prim)
2129 Vis_Ancestor := First_Elmt (Vis_List);
2130 while Present (Vis_Ancestor) loop
2132 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
2133 while Present (Elmt) loop
2134 if Node (Elmt) = Orig_Prim then
2135 Set_Overridden_Operation (S, Prim);
2136 Set_Alias (Prim, Orig_Prim);
2143 Next_Elmt (Vis_Ancestor);
2153 end Find_Hidden_Overridden_Primitive;
2155 ---------------------------------------
2156 -- Find_Primitive_Covering_Interface --
2157 ---------------------------------------
2159 function Find_Primitive_Covering_Interface
2160 (Tagged_Type : Entity_Id;
2161 Iface_Prim : Entity_Id) return Entity_Id
2167 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
2168 or else (Present (Alias (Iface_Prim))
2171 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
2173 -- Search in the homonym chain. Done to speed up locating visible
2174 -- entities and required to catch primitives associated with the partial
2175 -- view of private types when processing the corresponding full view.
2177 E := Current_Entity (Iface_Prim);
2178 while Present (E) loop
2179 if Is_Subprogram (E)
2180 and then Is_Dispatching_Operation (E)
2181 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
2189 -- Search in the list of primitives of the type. Required to locate
2190 -- the covering primitive if the covering primitive is not visible
2191 -- (for example, non-visible inherited primitive of private type).
2193 El := First_Elmt (Primitive_Operations (Tagged_Type));
2194 while Present (El) loop
2197 -- Keep separate the management of internal entities that link
2198 -- primitives with interface primitives from tagged type primitives.
2200 if No (Interface_Alias (E)) then
2201 if Present (Alias (E)) then
2203 -- This interface primitive has not been covered yet
2205 if Alias (E) = Iface_Prim then
2208 -- The covering primitive was inherited
2210 elsif Overridden_Operation (Ultimate_Alias (E))
2217 -- Check if E covers the interface primitive (includes case in
2218 -- which E is an inherited private primitive).
2220 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2224 -- Use the internal entity that links the interface primitive with
2225 -- the covering primitive to locate the entity.
2227 elsif Interface_Alias (E) = Iface_Prim then
2237 end Find_Primitive_Covering_Interface;
2239 ---------------------------
2240 -- Inheritance_Utilities --
2241 ---------------------------
2243 package body Inheritance_Utilities is
2245 ---------------------------
2246 -- Inherited_Subprograms --
2247 ---------------------------
2249 function Inherited_Subprograms
2251 No_Interfaces : Boolean := False;
2252 Interfaces_Only : Boolean := False;
2253 One_Only : Boolean := False) return Subprogram_List
2255 Result : Subprogram_List (1 .. 6000);
2256 -- 6000 here is intended to be infinity. We could use an expandable
2257 -- table, but it would be awfully heavy, and there is no way that we
2258 -- could reasonably exceed this value.
2261 -- Number of entries in Result
2263 Parent_Op : Entity_Id;
2264 -- Traverses the Overridden_Operation chain
2266 procedure Store_IS (E : Entity_Id);
2267 -- Stores E in Result if not already stored
2273 procedure Store_IS (E : Entity_Id) is
2275 for J in 1 .. N loop
2276 if E = Result (J) then
2285 -- Start of processing for Inherited_Subprograms
2288 pragma Assert (not (No_Interfaces and Interfaces_Only));
2290 -- When used from backends, visibility can be handled differently
2291 -- resulting in no dispatching type being found.
2294 and then Is_Dispatching_Operation (S)
2295 and then Present (Find_DT (S))
2297 -- Deal with direct inheritance
2299 if not Interfaces_Only then
2302 Parent_Op := Overridden_Operation (Parent_Op);
2303 exit when No (Parent_Op)
2304 or else (No_Interfaces
2305 and then Is_Interface (Find_DT (Parent_Op)));
2307 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2308 Store_IS (Parent_Op);
2317 -- Now deal with interfaces
2319 if not No_Interfaces then
2321 Tag_Typ : Entity_Id;
2326 Tag_Typ := Find_DT (S);
2328 -- In the presence of limited views there may be no visible
2329 -- dispatching type. Primitives will be inherited when non-
2330 -- limited view is frozen.
2332 if No (Tag_Typ) then
2333 return Result (1 .. 0);
2336 if Is_Concurrent_Type (Tag_Typ) then
2337 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2340 -- Search primitive operations of dispatching type
2342 if Present (Tag_Typ)
2343 and then Present (Primitive_Operations (Tag_Typ))
2345 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2346 while Present (Elmt) loop
2347 Prim := Node (Elmt);
2349 -- The following test eliminates some odd cases in
2350 -- which Ekind (Prim) is Void, to be investigated
2353 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2356 -- For [generic] subprogram, look at interface
2359 elsif Present (Interface_Alias (Prim))
2360 and then Alias (Prim) = S
2362 -- We have found a primitive covered by S
2364 Store_IS (Interface_Alias (Prim));
2380 return Result (1 .. N);
2381 end Inherited_Subprograms;
2383 ------------------------------
2384 -- Is_Overriding_Subprogram --
2385 ------------------------------
2387 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2388 Inherited : constant Subprogram_List :=
2389 Inherited_Subprograms (E, One_Only => True);
2391 return Inherited'Length > 0;
2392 end Is_Overriding_Subprogram;
2393 end Inheritance_Utilities;
2395 --------------------------------
2396 -- Inheritance_Utilities_Inst --
2397 --------------------------------
2399 package Inheritance_Utilities_Inst is new
2400 Inheritance_Utilities (Find_Dispatching_Type);
2402 ---------------------------
2403 -- Inherited_Subprograms --
2404 ---------------------------
2406 function Inherited_Subprograms
2408 No_Interfaces : Boolean := False;
2409 Interfaces_Only : Boolean := False;
2410 One_Only : Boolean := False) return Subprogram_List renames
2411 Inheritance_Utilities_Inst.Inherited_Subprograms;
2413 ---------------------------
2414 -- Is_Dynamically_Tagged --
2415 ---------------------------
2417 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2419 if Nkind (N) = N_Error then
2422 elsif Present (Find_Controlling_Arg (N)) then
2425 -- Special cases: entities, and calls that dispatch on result
2427 elsif Is_Entity_Name (N) then
2428 return Is_Class_Wide_Type (Etype (N));
2430 elsif Nkind (N) = N_Function_Call
2431 and then Is_Class_Wide_Type (Etype (N))
2435 -- Otherwise check whether call has controlling argument
2440 end Is_Dynamically_Tagged;
2442 ---------------------------------
2443 -- Is_Null_Interface_Primitive --
2444 ---------------------------------
2446 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2448 return Comes_From_Source (E)
2449 and then Is_Dispatching_Operation (E)
2450 and then Ekind (E) = E_Procedure
2451 and then Null_Present (Parent (E))
2452 and then Is_Interface (Find_Dispatching_Type (E));
2453 end Is_Null_Interface_Primitive;
2455 -----------------------------------
2456 -- Is_Inherited_Public_Operation --
2457 -----------------------------------
2459 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2460 Pack_Decl : Node_Id;
2461 Prim : Entity_Id := Op;
2462 Scop : Entity_Id := Prim;
2465 -- Locate the ultimate non-hidden alias entity
2467 while Present (Alias (Prim)) and then not Is_Hidden (Alias (Prim)) loop
2468 pragma Assert (Alias (Prim) /= Prim);
2469 Prim := Alias (Prim);
2470 Scop := Scope (Prim);
2473 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2474 Pack_Decl := Unit_Declaration_Node (Scop);
2477 Nkind (Pack_Decl) = N_Package_Declaration
2478 and then List_Containing (Unit_Declaration_Node (Prim)) =
2479 Visible_Declarations (Specification (Pack_Decl));
2484 end Is_Inherited_Public_Operation;
2486 ------------------------------
2487 -- Is_Overriding_Subprogram --
2488 ------------------------------
2490 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean renames
2491 Inheritance_Utilities_Inst.Is_Overriding_Subprogram;
2493 --------------------------
2494 -- Is_Tag_Indeterminate --
2495 --------------------------
2497 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2500 Orig_Node : constant Node_Id := Original_Node (N);
2503 if Nkind (Orig_Node) = N_Function_Call
2504 and then Is_Entity_Name (Name (Orig_Node))
2506 Nam := Entity (Name (Orig_Node));
2508 if not Has_Controlling_Result (Nam) then
2511 -- The function may have a controlling result, but if the return type
2512 -- is not visibly tagged, then this is not tag-indeterminate.
2514 elsif Is_Access_Type (Etype (Nam))
2515 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2519 -- An explicit dereference means that the call has already been
2520 -- expanded and there is no tag to propagate.
2522 elsif Nkind (N) = N_Explicit_Dereference then
2525 -- If there are no actuals, the call is tag-indeterminate
2527 elsif No (Parameter_Associations (Orig_Node)) then
2531 Actual := First_Actual (Orig_Node);
2532 while Present (Actual) loop
2533 if Is_Controlling_Actual (Actual)
2534 and then not Is_Tag_Indeterminate (Actual)
2536 -- One operand is dispatching
2541 Next_Actual (Actual);
2547 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2548 return Is_Tag_Indeterminate (Expression (Orig_Node));
2550 -- Case of a call to the Input attribute (possibly rewritten), which is
2551 -- always tag-indeterminate except when its prefix is a Class attribute.
2553 elsif Nkind (Orig_Node) = N_Attribute_Reference
2555 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2556 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2560 -- In Ada 2005, a function that returns an anonymous access type can be
2561 -- dispatching, and the dereference of a call to such a function can
2562 -- also be tag-indeterminate if the call itself is.
2564 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2565 and then Ada_Version >= Ada_2005
2567 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2572 end Is_Tag_Indeterminate;
2574 ------------------------------------
2575 -- Override_Dispatching_Operation --
2576 ------------------------------------
2578 procedure Override_Dispatching_Operation
2579 (Tagged_Type : Entity_Id;
2580 Prev_Op : Entity_Id;
2582 Is_Wrapper : Boolean := False)
2588 -- If there is no previous operation to override, the type declaration
2589 -- was malformed, and an error must have been emitted already.
2591 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2592 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2600 -- The location of entities that come from source in the list of
2601 -- primitives of the tagged type must follow their order of occurrence
2602 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2603 -- primitive of an interface that is not implemented by the parents of
2604 -- this tagged type (that is, it is an alias of an interface primitive
2605 -- generated by Derive_Interface_Progenitors), then we must append the
2606 -- new entity at the end of the list of primitives.
2608 if Present (Alias (Prev_Op))
2609 and then Etype (Tagged_Type) /= Tagged_Type
2610 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2611 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2612 Tagged_Type, Use_Full_View => True)
2613 and then not Implements_Interface
2614 (Etype (Tagged_Type),
2615 Find_Dispatching_Type (Alias (Prev_Op)))
2617 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2618 Add_Dispatching_Operation (Tagged_Type, New_Op);
2620 -- The new primitive replaces the overridden entity. Required to ensure
2621 -- that overriding primitive is assigned the same dispatch table slot.
2624 Replace_Elmt (Elmt, New_Op);
2627 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2629 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2630 -- entities of the overridden primitive to reference New_Op, and
2631 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2632 -- that the new operation is subtype conformant with the interface
2633 -- operations that it implements (for operations inherited from the
2634 -- parent itself, this check is made when building the derived type).
2636 -- Note: This code is executed with internally generated wrappers of
2637 -- functions with controlling result and late overridings.
2639 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2640 while Present (Elmt) loop
2641 Prim := Node (Elmt);
2643 if Prim = New_Op then
2646 -- Note: The check on Is_Subprogram protects the frontend against
2647 -- reading attributes in entities that are not yet fully decorated
2649 elsif Is_Subprogram (Prim)
2650 and then Present (Interface_Alias (Prim))
2651 and then Alias (Prim) = Prev_Op
2653 Set_Alias (Prim, New_Op);
2655 -- No further decoration needed yet for internally generated
2656 -- wrappers of controlling functions since (at this stage)
2657 -- they are not yet decorated.
2659 if not Is_Wrapper then
2660 Check_Subtype_Conformant (New_Op, Prim);
2662 Set_Is_Abstract_Subprogram (Prim,
2663 Is_Abstract_Subprogram (New_Op));
2665 -- Ensure that this entity will be expanded to fill the
2666 -- corresponding entry in its dispatch table.
2668 if not Is_Abstract_Subprogram (Prim) then
2669 Set_Has_Delayed_Freeze (Prim);
2678 if (not Is_Package_Or_Generic_Package (Current_Scope))
2679 or else not In_Private_Part (Current_Scope)
2681 -- Not a private primitive
2685 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2687 -- Make the overriding operation into an alias of the implicit one.
2688 -- In this fashion a call from outside ends up calling the new body
2689 -- even if non-dispatching, and a call from inside calls the over-
2690 -- riding operation because it hides the implicit one. To indicate
2691 -- that the body of Prev_Op is never called, set its dispatch table
2692 -- entity to Empty. If the overridden operation has a dispatching
2693 -- result, so does the overriding one.
2695 Set_Alias (Prev_Op, New_Op);
2696 Set_DTC_Entity (Prev_Op, Empty);
2697 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2699 end Override_Dispatching_Operation;
2705 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2706 Call_Node : Node_Id;
2710 if Nkind (Actual) = N_Function_Call then
2711 Call_Node := Actual;
2713 elsif Nkind (Actual) = N_Identifier
2714 and then Nkind (Original_Node (Actual)) = N_Function_Call
2716 -- Call rewritten as object declaration when stack-checking is
2717 -- enabled. Propagate tag to expression in declaration, which is
2720 Call_Node := Expression (Parent (Entity (Actual)));
2722 -- Ada 2005: If this is a dereference of a call to a function with a
2723 -- dispatching access-result, the tag is propagated when the dereference
2724 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2726 elsif Nkind (Actual) = N_Explicit_Dereference
2727 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2731 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2732 -- and in that case we can simply return.
2734 elsif Nkind (Actual) = N_Attribute_Reference then
2735 pragma Assert (Attribute_Name (Actual) = Name_Input);
2739 -- Only other possibilities are parenthesized or qualified expression,
2740 -- or an expander-generated unchecked conversion of a function call to
2741 -- a stream Input attribute.
2744 Call_Node := Expression (Actual);
2747 -- No action needed if the call has been already expanded
2749 if Is_Expanded_Dispatching_Call (Call_Node) then
2753 -- Do not set the Controlling_Argument if already set. This happens in
2754 -- the special case of _Input (see Exp_Attr, case Input).
2756 if No (Controlling_Argument (Call_Node)) then
2757 Set_Controlling_Argument (Call_Node, Control);
2760 Arg := First_Actual (Call_Node);
2761 while Present (Arg) loop
2762 if Is_Tag_Indeterminate (Arg) then
2763 Propagate_Tag (Control, Arg);
2769 -- Expansion of dispatching calls is suppressed on VM targets, because
2770 -- the VM back-ends directly handle the generation of dispatching calls
2771 -- and would have to undo any expansion to an indirect call.
2773 if Tagged_Type_Expansion then
2775 Call_Typ : constant Entity_Id := Etype (Call_Node);
2778 Expand_Dispatching_Call (Call_Node);
2780 -- If the controlling argument is an interface type and the type
2781 -- of Call_Node differs then we must add an implicit conversion to
2782 -- force displacement of the pointer to the object to reference
2783 -- the secondary dispatch table of the interface.
2785 if Is_Interface (Etype (Control))
2786 and then Etype (Control) /= Call_Typ
2788 -- Cannot use Convert_To because the previous call to
2789 -- Expand_Dispatching_Call leaves decorated the Call_Node
2790 -- with the type of Control.
2793 Make_Type_Conversion (Sloc (Call_Node),
2795 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2796 Expression => Relocate_Node (Call_Node)));
2797 Set_Etype (Call_Node, Etype (Control));
2798 Set_Analyzed (Call_Node);
2800 Expand_Interface_Conversion (Call_Node);
2804 -- Expansion of a dispatching call results in an indirect call, which in
2805 -- turn causes current values to be killed (see Resolve_Call), so on VM
2806 -- targets we do the call here to ensure consistent warnings between VM
2807 -- and non-VM targets.
2810 Kill_Current_Values;