1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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;
55 with Warnsw; use Warnsw;
57 package body Sem_Disp is
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Add_Dispatching_Operation
64 (Tagged_Type : Entity_Id;
66 -- Add New_Op in the list of primitive operations of Tagged_Type
68 function Check_Controlling_Type
70 Subp : Entity_Id) return Entity_Id;
71 -- T is the tagged type of a formal parameter or the result of Subp.
72 -- If the subprogram has a controlling parameter or result that matches
73 -- the type, then returns the tagged type of that parameter or result
74 -- (returning the designated tagged type in the case of an access
75 -- parameter); otherwise returns empty.
77 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
78 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
79 -- type of S that has the same name of S, a type-conformant profile, an
80 -- original corresponding operation O that is a primitive of a visible
81 -- ancestor of the dispatching type of S and O is visible at the point of
82 -- of declaration of S. If the entity is found the Alias of S is set to the
83 -- original corresponding operation S and its Overridden_Operation is set
84 -- to the found entity; otherwise return Empty.
86 -- This routine does not search for non-hidden primitives since they are
87 -- covered by the normal Ada 2005 rules.
89 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
90 -- Check whether a primitive operation is inherited from an operation
91 -- declared in the visible part of its package.
93 -------------------------------
94 -- Add_Dispatching_Operation --
95 -------------------------------
97 procedure Add_Dispatching_Operation
98 (Tagged_Type : Entity_Id;
101 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
104 -- The dispatching operation may already be on the list, if it is the
105 -- wrapper for an inherited function of a null extension (see Exp_Ch3
106 -- for the construction of function wrappers). The list of primitive
107 -- operations must not contain duplicates.
109 -- The Default_Initial_Condition and invariant procedures are not added
110 -- to the list of primitives even when they are generated for a tagged
111 -- type. These routines must not be targets of dispatching calls and
112 -- therefore must not appear in the dispatch table because they already
113 -- utilize class-wide-precondition semantics to handle inheritance and
116 if Is_Suitable_Primitive (New_Op) then
117 Append_Unique_Elmt (New_Op, List);
119 end Add_Dispatching_Operation;
121 --------------------------
122 -- Covered_Interface_Op --
123 --------------------------
125 function Covered_Interface_Op (Prim : Entity_Id) return Entity_Id is
126 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
131 pragma Assert (Is_Dispatching_Operation (Prim));
133 -- Although this is a dispatching primitive we must check if its
134 -- dispatching type is available because it may be the primitive
135 -- of a private type not defined as tagged in its partial view.
137 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
139 -- If the tagged type is frozen then the internal entities associated
140 -- with interfaces are available in the list of primitives of the
141 -- tagged type and can be used to speed up this search.
143 if Is_Frozen (Tagged_Type) then
144 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
145 while Present (Elmt) loop
148 if Present (Interface_Alias (E))
149 and then Alias (E) = Prim
151 return Interface_Alias (E);
157 -- Otherwise we must collect all the interface primitives and check
158 -- if the Prim overrides (implements) some interface primitive.
162 Ifaces_List : Elist_Id;
163 Iface_Elmt : Elmt_Id;
165 Iface_Prim : Entity_Id;
168 Collect_Interfaces (Tagged_Type, Ifaces_List);
169 Iface_Elmt := First_Elmt (Ifaces_List);
170 while Present (Iface_Elmt) loop
171 Iface := Node (Iface_Elmt);
173 Elmt := First_Elmt (Primitive_Operations (Iface));
174 while Present (Elmt) loop
175 Iface_Prim := Node (Elmt);
177 if Chars (Iface_Prim) = Chars (Prim)
178 and then Is_Interface_Conformant
179 (Tagged_Type, Iface_Prim, Prim)
187 Next_Elmt (Iface_Elmt);
194 end Covered_Interface_Op;
196 -------------------------------
197 -- Check_Controlling_Formals --
198 -------------------------------
200 procedure Check_Controlling_Formals
205 Ctrl_Type : Entity_Id;
208 Formal := First_Formal (Subp);
209 while Present (Formal) loop
210 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
212 if Present (Ctrl_Type) then
214 -- Obtain the full type in case we are looking at an incomplete
217 if Ekind (Ctrl_Type) = E_Incomplete_Type
218 and then Present (Full_View (Ctrl_Type))
220 Ctrl_Type := Full_View (Ctrl_Type);
223 -- When controlling type is concurrent and declared within a
224 -- generic or inside an instance use corresponding record type.
226 if Is_Concurrent_Type (Ctrl_Type)
227 and then Present (Corresponding_Record_Type (Ctrl_Type))
229 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
232 if Ctrl_Type = Typ then
233 Set_Is_Controlling_Formal (Formal);
235 -- Ada 2005 (AI-231): Anonymous access types that are used in
236 -- controlling parameters exclude null because it is necessary
237 -- to read the tag to dispatch, and null has no tag.
239 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
240 Set_Can_Never_Be_Null (Etype (Formal));
241 Set_Is_Known_Non_Null (Etype (Formal));
244 -- Check that the parameter's nominal subtype statically
245 -- matches the first subtype.
247 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
248 if not Subtypes_Statically_Match
249 (Typ, Designated_Type (Etype (Formal)))
252 ("parameter subtype does not match controlling type",
256 -- Within a predicate function, the formal may be a subtype
257 -- of a tagged type, given that the predicate is expressed
258 -- in terms of the subtype.
260 elsif not Subtypes_Statically_Match (Typ, Etype (Formal))
261 and then not Is_Predicate_Function (Subp)
264 ("parameter subtype does not match controlling type",
268 if Present (Default_Value (Formal)) then
270 -- In Ada 2005, access parameters can have defaults
272 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
273 and then Ada_Version < Ada_2005
276 ("default not allowed for controlling access parameter",
277 Default_Value (Formal));
279 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
281 ("default expression must be a tag indeterminate" &
282 " function call", Default_Value (Formal));
286 elsif Comes_From_Source (Subp) then
288 ("operation can be dispatching in only one type", Subp);
292 Next_Formal (Formal);
295 if Ekind_In (Subp, E_Function, E_Generic_Function) then
296 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
298 if Present (Ctrl_Type) then
299 if Ctrl_Type = Typ then
300 Set_Has_Controlling_Result (Subp);
302 -- Check that result subtype statically matches first subtype
303 -- (Ada 2005): Subp may have a controlling access result.
305 if Subtypes_Statically_Match (Typ, Etype (Subp))
306 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
308 Subtypes_Statically_Match
309 (Typ, Designated_Type (Etype (Subp))))
315 ("result subtype does not match controlling type", Subp);
318 elsif Comes_From_Source (Subp) then
320 ("operation can be dispatching in only one type", Subp);
324 end Check_Controlling_Formals;
326 ----------------------------
327 -- Check_Controlling_Type --
328 ----------------------------
330 function Check_Controlling_Type
332 Subp : Entity_Id) return Entity_Id
334 Tagged_Type : Entity_Id := Empty;
337 if Is_Tagged_Type (T) then
338 if Is_First_Subtype (T) then
341 Tagged_Type := Base_Type (T);
344 -- If the type is incomplete, it may have been declared without a
345 -- Tagged indication, but the full view may be tagged, in which case
346 -- that is the controlling type of the subprogram. This is one of the
347 -- approx. 579 places in the language where a lookahead would help.
349 elsif Ekind (T) = E_Incomplete_Type
350 and then Present (Full_View (T))
351 and then Is_Tagged_Type (Full_View (T))
353 Set_Is_Tagged_Type (T);
354 Tagged_Type := Full_View (T);
356 elsif Ekind (T) = E_Anonymous_Access_Type
357 and then Is_Tagged_Type (Designated_Type (T))
359 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
360 if Is_First_Subtype (Designated_Type (T)) then
361 Tagged_Type := Designated_Type (T);
363 Tagged_Type := Base_Type (Designated_Type (T));
366 -- Ada 2005: an incomplete type can be tagged. An operation with an
367 -- access parameter of the type is dispatching.
369 elsif Scope (Designated_Type (T)) = Current_Scope then
370 Tagged_Type := Designated_Type (T);
372 -- Ada 2005 (AI-50217)
374 elsif From_Limited_With (Designated_Type (T))
375 and then Has_Non_Limited_View (Designated_Type (T))
376 and then Scope (Designated_Type (T)) = Scope (Subp)
378 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
379 Tagged_Type := Non_Limited_View (Designated_Type (T));
381 Tagged_Type := Base_Type (Non_Limited_View
382 (Designated_Type (T)));
387 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
390 -- The dispatching type and the primitive operation must be defined in
391 -- the same scope, except in the case of internal operations and formal
392 -- abstract subprograms.
394 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
395 and then (not Is_Generic_Type (Tagged_Type)
396 or else not Comes_From_Source (Subp)))
398 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
400 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
402 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
404 Is_Abstract_Subprogram (Subp))
411 end Check_Controlling_Type;
413 ----------------------------
414 -- Check_Dispatching_Call --
415 ----------------------------
417 procedure Check_Dispatching_Call (N : Node_Id) is
418 Loc : constant Source_Ptr := Sloc (N);
421 Control : Node_Id := Empty;
423 Subp_Entity : Entity_Id;
424 Indeterm_Ancestor_Call : Boolean := False;
425 Indeterm_Ctrl_Type : Entity_Id := Empty; -- init to avoid warning
427 Static_Tag : Node_Id := Empty;
428 -- If a controlling formal has a statically tagged actual, the tag of
429 -- this actual is to be used for any tag-indeterminate actual.
431 procedure Check_Direct_Call;
432 -- In the case when the controlling actual is a class-wide type whose
433 -- root type's completion is a task or protected type, the call is in
434 -- fact direct. This routine detects the above case and modifies the
437 procedure Check_Dispatching_Context (Call : Node_Id);
438 -- If the call is tag-indeterminate and the entity being called is
439 -- abstract, verify that the context is a call that will eventually
440 -- provide a tag for dispatching, or has provided one already.
442 -----------------------
443 -- Check_Direct_Call --
444 -----------------------
446 procedure Check_Direct_Call is
447 Typ : Entity_Id := Etype (Control);
449 -- Predefined primitives do not receive wrappers since they are built
450 -- from scratch for the corresponding record of synchronized types.
451 -- Equality is in general predefined, but is excluded from the check
452 -- when it is user-defined.
454 if Is_Predefined_Dispatching_Operation (Subp_Entity)
455 and then not Is_User_Defined_Equality (Subp_Entity)
460 if Is_Class_Wide_Type (Typ) then
461 Typ := Root_Type (Typ);
464 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
465 Typ := Full_View (Typ);
468 if Is_Concurrent_Type (Typ)
470 Present (Corresponding_Record_Type (Typ))
472 Typ := Corresponding_Record_Type (Typ);
474 -- The concurrent record's list of primitives should contain a
475 -- wrapper for the entity of the call, retrieve it.
480 Wrapper_Found : Boolean := False;
483 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
484 while Present (Prim_Elmt) loop
485 Prim := Node (Prim_Elmt);
487 if Is_Primitive_Wrapper (Prim)
488 and then Wrapped_Entity (Prim) = Subp_Entity
490 Wrapper_Found := True;
494 Next_Elmt (Prim_Elmt);
497 -- A primitive declared between two views should have a
498 -- corresponding wrapper.
500 pragma Assert (Wrapper_Found);
502 -- Modify the call by setting the proper entity
504 Set_Entity (Name (N), Prim);
507 end Check_Direct_Call;
509 -------------------------------
510 -- Check_Dispatching_Context --
511 -------------------------------
513 procedure Check_Dispatching_Context (Call : Node_Id) is
514 Subp : constant Entity_Id := Entity (Name (Call));
516 procedure Abstract_Context_Error;
517 -- Error for abstract call dispatching on result is not dispatching
519 ----------------------------
520 -- Abstract_Context_Error --
521 ----------------------------
523 procedure Abstract_Context_Error is
525 if Ekind (Subp) = E_Function then
527 ("call to abstract function must be dispatching", N);
529 -- This error can occur for a procedure in the case of a call to
530 -- an abstract formal procedure with a statically tagged operand.
534 ("call to abstract procedure must be dispatching", N);
536 end Abstract_Context_Error;
540 Scop : constant Entity_Id := Current_Scope_No_Loops;
541 Typ : constant Entity_Id := Etype (Subp);
544 -- Start of processing for Check_Dispatching_Context
547 -- If the called subprogram is a private overriding, replace it
548 -- with its alias, which has the correct body. Verify that the
549 -- two subprograms have the same controlling type (this is not the
550 -- case for an inherited subprogram that has become abstract).
552 if Is_Abstract_Subprogram (Subp)
553 and then No (Controlling_Argument (Call))
555 if Present (Alias (Subp))
556 and then not Is_Abstract_Subprogram (Alias (Subp))
557 and then No (DTC_Entity (Subp))
558 and then Find_Dispatching_Type (Subp) =
559 Find_Dispatching_Type (Alias (Subp))
561 -- Private overriding of inherited abstract operation, call is
564 Set_Entity (Name (N), Alias (Subp));
567 -- An obscure special case: a null procedure may have a class-
568 -- wide pre/postcondition that includes a call to an abstract
569 -- subp. Calls within the expression may not have been rewritten
570 -- as dispatching calls yet, because the null body appears in
571 -- the current declarative part. The expression will be properly
572 -- rewritten/reanalyzed when the postcondition procedure is built.
574 -- Similarly, if this is a pre/postcondition for an abstract
575 -- subprogram, it may call another abstract function which is
576 -- a primitive of an abstract type. The call is non-dispatching
577 -- but will be legal in overridings of the operation.
579 elsif (Is_Subprogram (Scop)
580 or else Chars (Scop) = Name_Postcondition)
582 (Is_Abstract_Subprogram (Scop)
584 (Nkind (Parent (Scop)) = N_Procedure_Specification
585 and then Null_Present (Parent (Scop))))
589 elsif Ekind (Current_Scope) = E_Function
590 and then Nkind (Unit_Declaration_Node (Scop)) =
591 N_Generic_Subprogram_Declaration
596 -- We need to determine whether the context of the call
597 -- provides a tag to make the call dispatching. This requires
598 -- the call to be the actual in an enclosing call, and that
599 -- actual must be controlling. If the call is an operand of
600 -- equality, the other operand must not ve abstract.
602 if not Is_Tagged_Type (Typ)
604 (Ekind (Typ) = E_Anonymous_Access_Type
605 and then Is_Tagged_Type (Designated_Type (Typ)))
607 Abstract_Context_Error;
611 Par := Parent (Call);
613 if Nkind (Par) = N_Parameter_Association then
617 if Nkind (Par) = N_Qualified_Expression
618 or else Nkind (Par) = N_Unchecked_Type_Conversion
623 if Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
624 and then Is_Entity_Name (Name (Par))
627 Enc_Subp : constant Entity_Id := Entity (Name (Par));
631 Ret_Type : Entity_Id;
634 -- Find controlling formal that can provide tag for the
635 -- tag-indeterminate actual. The corresponding actual
636 -- must be the corresponding class-wide type.
638 F := First_Formal (Enc_Subp);
639 A := First_Actual (Par);
641 -- Find controlling type of call. Dereference if function
642 -- returns an access type.
644 Ret_Type := Etype (Call);
645 if Is_Access_Type (Etype (Call)) then
646 Ret_Type := Designated_Type (Ret_Type);
649 while Present (F) loop
650 Control := Etype (A);
652 if Is_Access_Type (Control) then
653 Control := Designated_Type (Control);
656 if Is_Controlling_Formal (F)
657 and then not (Call = A or else Parent (Call) = A)
658 and then Control = Class_Wide_Type (Ret_Type)
667 if Nkind (Par) = N_Function_Call
668 and then Is_Tag_Indeterminate (Par)
670 -- The parent may be an actual of an enclosing call
672 Check_Dispatching_Context (Par);
677 ("call to abstract function must be dispatching",
683 -- For equality operators, one of the operands must be
684 -- statically or dynamically tagged.
686 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
687 if N = Right_Opnd (Par)
688 and then Is_Tag_Indeterminate (Left_Opnd (Par))
690 Abstract_Context_Error;
692 elsif N = Left_Opnd (Par)
693 and then Is_Tag_Indeterminate (Right_Opnd (Par))
695 Abstract_Context_Error;
700 -- The left-hand side of an assignment provides the tag
702 elsif Nkind (Par) = N_Assignment_Statement then
706 Abstract_Context_Error;
710 end Check_Dispatching_Context;
712 -- Start of processing for Check_Dispatching_Call
715 -- Find a controlling argument, if any
717 if Present (Parameter_Associations (N)) then
718 Subp_Entity := Entity (Name (N));
720 Actual := First_Actual (N);
721 Formal := First_Formal (Subp_Entity);
722 while Present (Actual) loop
723 Control := Find_Controlling_Arg (Actual);
724 exit when Present (Control);
726 -- Check for the case where the actual is a tag-indeterminate call
727 -- whose result type is different than the tagged type associated
728 -- with the containing call, but is an ancestor of the type.
730 if Is_Controlling_Formal (Formal)
731 and then Is_Tag_Indeterminate (Actual)
732 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
733 and then Is_Ancestor (Etype (Actual), Etype (Formal))
735 Indeterm_Ancestor_Call := True;
736 Indeterm_Ctrl_Type := Etype (Formal);
738 -- If the formal is controlling but the actual is not, the type
739 -- of the actual is statically known, and may be used as the
740 -- controlling tag for some other tag-indeterminate actual.
742 elsif Is_Controlling_Formal (Formal)
743 and then Is_Entity_Name (Actual)
744 and then Is_Tagged_Type (Etype (Actual))
746 Static_Tag := Actual;
749 Next_Actual (Actual);
750 Next_Formal (Formal);
753 -- If the call doesn't have a controlling actual but does have an
754 -- indeterminate actual that requires dispatching treatment, then an
755 -- object is needed that will serve as the controlling argument for
756 -- a dispatching call on the indeterminate actual. This can occur
757 -- in the unusual situation of a default actual given by a tag-
758 -- indeterminate call and where the type of the call is an ancestor
759 -- of the type associated with a containing call to an inherited
760 -- operation (see AI-239).
762 -- Rather than create an object of the tagged type, which would
763 -- be problematic for various reasons (default initialization,
764 -- discriminants), the tag of the containing call's associated
765 -- tagged type is directly used to control the dispatching.
768 and then Indeterm_Ancestor_Call
769 and then No (Static_Tag)
772 Make_Attribute_Reference (Loc,
773 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
774 Attribute_Name => Name_Tag);
779 if Present (Control) then
781 -- Verify that no controlling arguments are statically tagged
784 Write_Str ("Found Dispatching call");
789 Actual := First_Actual (N);
790 while Present (Actual) loop
791 if Actual /= Control then
793 if not Is_Controlling_Actual (Actual) then
794 null; -- Can be anything
796 elsif Is_Dynamically_Tagged (Actual) then
797 null; -- Valid parameter
799 elsif Is_Tag_Indeterminate (Actual) then
801 -- The tag is inherited from the enclosing call (the node
802 -- we are currently analyzing). Explicitly expand the
803 -- actual, since the previous call to Expand (from
804 -- Resolve_Call) had no way of knowing about the
805 -- required dispatching.
807 Propagate_Tag (Control, Actual);
811 ("controlling argument is not dynamically tagged",
817 Next_Actual (Actual);
820 -- Mark call as a dispatching call
822 Set_Controlling_Argument (N, Control);
823 Check_Restriction (No_Dispatching_Calls, N);
825 -- The dispatching call may need to be converted into a direct
826 -- call in certain cases.
830 -- If there is a statically tagged actual and a tag-indeterminate
831 -- call to a function of the ancestor (such as that provided by a
832 -- default), then treat this as a dispatching call and propagate
833 -- the tag to the tag-indeterminate call(s).
835 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
837 Make_Attribute_Reference (Loc,
839 New_Occurrence_Of (Etype (Static_Tag), Loc),
840 Attribute_Name => Name_Tag);
844 Actual := First_Actual (N);
845 Formal := First_Formal (Subp_Entity);
846 while Present (Actual) loop
847 if Is_Tag_Indeterminate (Actual)
848 and then Is_Controlling_Formal (Formal)
850 Propagate_Tag (Control, Actual);
853 Next_Actual (Actual);
854 Next_Formal (Formal);
857 Check_Dispatching_Context (N);
859 elsif Nkind (N) /= N_Function_Call then
861 -- The call is not dispatching, so check that there aren't any
862 -- tag-indeterminate abstract calls left among its actuals.
864 Actual := First_Actual (N);
865 while Present (Actual) loop
866 if Is_Tag_Indeterminate (Actual) then
868 -- Function call case
870 if Nkind (Original_Node (Actual)) = N_Function_Call then
871 Func := Entity (Name (Original_Node (Actual)));
873 -- If the actual is an attribute then it can't be abstract
874 -- (the only current case of a tag-indeterminate attribute
875 -- is the stream Input attribute).
877 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
881 -- Ditto if it is an explicit dereference
883 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
887 -- Only other possibility is a qualified expression whose
888 -- constituent expression is itself a call.
892 Entity (Name (Original_Node
893 (Expression (Original_Node (Actual)))));
896 if Present (Func) and then Is_Abstract_Subprogram (Func) then
898 ("call to abstract function must be dispatching",
903 Next_Actual (Actual);
906 Check_Dispatching_Context (N);
909 elsif Nkind (Parent (N)) in N_Subexpr then
910 Check_Dispatching_Context (N);
912 elsif Nkind (Parent (N)) = N_Assignment_Statement
913 and then Is_Class_Wide_Type (Etype (Name (Parent (N))))
917 elsif Is_Abstract_Subprogram (Subp_Entity) then
918 Check_Dispatching_Context (N);
923 -- If dispatching on result, the enclosing call, if any, will
924 -- determine the controlling argument. Otherwise this is the
925 -- primitive operation of the root type.
927 Check_Dispatching_Context (N);
929 end Check_Dispatching_Call;
931 ---------------------------------
932 -- Check_Dispatching_Operation --
933 ---------------------------------
935 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
936 procedure Warn_On_Late_Primitive_After_Private_Extension
939 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
940 -- if it is a public primitive defined after some private extension of
943 ----------------------------------------------------
944 -- Warn_On_Late_Primitive_After_Private_Extension --
945 ----------------------------------------------------
947 procedure Warn_On_Late_Primitive_After_Private_Extension
954 if Warn_On_Late_Primitives
955 and then Comes_From_Source (Prim)
956 and then Has_Private_Extension (Typ)
957 and then Is_Package_Or_Generic_Package (Current_Scope)
958 and then not In_Private_Part (Current_Scope)
960 E := Next_Entity (Typ);
963 if Ekind (E) = E_Record_Type_With_Private
964 and then Etype (E) = Typ
966 Error_Msg_Name_1 := Chars (Typ);
967 Error_Msg_Name_2 := Chars (E);
968 Error_Msg_Sloc := Sloc (E);
970 ("?j?primitive of type % defined after private extension "
972 Error_Msg_Name_1 := Chars (Prim);
973 Error_Msg_Name_2 := Chars (E);
975 ("\spec of % should appear before declaration of type %!",
983 end Warn_On_Late_Primitive_After_Private_Extension;
987 Body_Is_Last_Primitive : Boolean := False;
988 Has_Dispatching_Parent : Boolean := False;
989 Ovr_Subp : Entity_Id := Empty;
990 Tagged_Type : Entity_Id;
992 -- Start of processing for Check_Dispatching_Operation
995 if not Ekind_In (Subp, E_Function, E_Procedure) then
998 -- The Default_Initial_Condition procedure is not a primitive subprogram
999 -- even if it relates to a tagged type. This routine is not meant to be
1000 -- inherited or overridden.
1002 elsif Is_DIC_Procedure (Subp) then
1005 -- The "partial" and "full" type invariant procedures are not primitive
1006 -- subprograms even if they relate to a tagged type. These routines are
1007 -- not meant to be inherited or overridden.
1009 elsif Is_Invariant_Procedure (Subp)
1010 or else Is_Partial_Invariant_Procedure (Subp)
1015 Set_Is_Dispatching_Operation (Subp, False);
1016 Tagged_Type := Find_Dispatching_Type (Subp);
1018 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1019 -- Required because primitives of concurrent types are attached
1020 -- to the corresponding record (not to the concurrent type).
1022 if Ada_Version >= Ada_2005
1023 and then Present (Tagged_Type)
1024 and then Is_Concurrent_Type (Tagged_Type)
1025 and then Present (Corresponding_Record_Type (Tagged_Type))
1027 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
1030 -- (AI-345): The task body procedure is not a primitive of the tagged
1033 if Present (Tagged_Type)
1034 and then Is_Concurrent_Record_Type (Tagged_Type)
1035 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
1036 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
1037 and then Subp = Get_Task_Body_Procedure
1038 (Corresponding_Concurrent_Type (Tagged_Type))
1043 -- If Subp is derived from a dispatching operation then it should
1044 -- always be treated as dispatching. In this case various checks
1045 -- below will be bypassed. Makes sure that late declarations for
1046 -- inherited private subprograms are treated as dispatching, even
1047 -- if the associated tagged type is already frozen.
1049 Has_Dispatching_Parent :=
1050 Present (Alias (Subp))
1051 and then Is_Dispatching_Operation (Alias (Subp));
1053 if No (Tagged_Type) then
1055 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1056 -- with an abstract interface type unless the interface acts as a
1057 -- parent type in a derivation. If the interface type is a formal
1058 -- type then the operation is not primitive and therefore legal.
1065 E := First_Entity (Subp);
1066 while Present (E) loop
1068 -- For an access parameter, check designated type
1070 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
1071 Typ := Designated_Type (Etype (E));
1076 if Comes_From_Source (Subp)
1077 and then Is_Interface (Typ)
1078 and then not Is_Class_Wide_Type (Typ)
1079 and then not Is_Derived_Type (Typ)
1080 and then not Is_Generic_Type (Typ)
1081 and then not In_Instance
1083 Error_Msg_N ("??declaration of& is too late!", Subp);
1084 Error_Msg_NE -- CODEFIX??
1085 ("\??spec should appear immediately after declaration of "
1086 & "& !", Subp, Typ);
1093 -- In case of functions check also the result type
1095 if Ekind (Subp) = E_Function then
1096 if Is_Access_Type (Etype (Subp)) then
1097 Typ := Designated_Type (Etype (Subp));
1099 Typ := Etype (Subp);
1102 -- The following should be better commented, especially since
1103 -- we just added several new conditions here ???
1105 if Comes_From_Source (Subp)
1106 and then Is_Interface (Typ)
1107 and then not Is_Class_Wide_Type (Typ)
1108 and then not Is_Derived_Type (Typ)
1109 and then not Is_Generic_Type (Typ)
1110 and then not In_Instance
1112 Error_Msg_N ("??declaration of& is too late!", Subp);
1114 ("\??spec should appear immediately after declaration of "
1115 & "& !", Subp, Typ);
1122 -- The subprograms build internally after the freezing point (such as
1123 -- init procs, interface thunks, type support subprograms, and Offset
1124 -- to top functions for accessing interface components in variable
1125 -- size tagged types) are not primitives.
1127 elsif Is_Frozen (Tagged_Type)
1128 and then not Comes_From_Source (Subp)
1129 and then not Has_Dispatching_Parent
1131 -- Complete decoration of internally built subprograms that override
1132 -- a dispatching primitive. These entities correspond with the
1135 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1136 -- to override functions of nonabstract null extensions. These
1137 -- primitives were added to the list of primitives of the tagged
1138 -- type by Make_Controlling_Function_Wrappers. However, attribute
1139 -- Is_Dispatching_Operation must be set to true.
1141 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1144 -- 3. Subprograms associated with stream attributes (built by
1145 -- New_Stream_Subprogram)
1147 -- 4. Wrappers built for inherited operations with inherited class-
1148 -- wide conditions, where the conditions include calls to other
1149 -- overridden primitives. The wrappers include checks on these
1150 -- modified conditions. (AI12-113).
1152 -- 5. Declarations built for subprograms without separate specs that
1153 -- are eligible for inlining in GNATprove (inside
1154 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1156 if Present (Old_Subp)
1157 and then Present (Overridden_Operation (Subp))
1158 and then Is_Dispatching_Operation (Old_Subp)
1161 ((Ekind (Subp) = E_Function
1162 and then Is_Dispatching_Operation (Old_Subp)
1163 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1166 (Ekind (Subp) = E_Procedure
1167 and then Is_Dispatching_Operation (Old_Subp)
1168 and then Present (Alias (Old_Subp))
1169 and then Is_Null_Interface_Primitive
1170 (Ultimate_Alias (Old_Subp)))
1172 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1173 or else Get_TSS_Name (Subp) = TSS_Stream_Write
1175 or else Present (Contract (Overridden_Operation (Subp)))
1177 or else GNATprove_Mode);
1179 Check_Controlling_Formals (Tagged_Type, Subp);
1180 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1181 Set_Is_Dispatching_Operation (Subp);
1186 -- The operation may be a child unit, whose scope is the defining
1187 -- package, but which is not a primitive operation of the type.
1189 elsif Is_Child_Unit (Subp) then
1192 -- If the subprogram is not defined in a package spec, the only case
1193 -- where it can be a dispatching op is when it overrides an operation
1194 -- before the freezing point of the type.
1196 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1197 or else In_Package_Body (Scope (Subp)))
1198 and then not Has_Dispatching_Parent
1200 if not Comes_From_Source (Subp)
1201 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1205 -- If the type is already frozen, the overriding is not allowed
1206 -- except when Old_Subp is not a dispatching operation (which can
1207 -- occur when Old_Subp was inherited by an untagged type). However,
1208 -- a body with no previous spec freezes the type *after* its
1209 -- declaration, and therefore is a legal overriding (unless the type
1210 -- has already been frozen). Only the first such body is legal.
1212 elsif Present (Old_Subp)
1213 and then Is_Dispatching_Operation (Old_Subp)
1215 if Comes_From_Source (Subp)
1217 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1218 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1221 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1222 Decl_Item : Node_Id;
1225 -- ??? The checks here for whether the type has been frozen
1226 -- prior to the new body are not complete. It's not simple
1227 -- to check frozenness at this point since the body has
1228 -- already caused the type to be prematurely frozen in
1229 -- Analyze_Declarations, but we're forced to recheck this
1230 -- here because of the odd rule interpretation that allows
1231 -- the overriding if the type wasn't frozen prior to the
1232 -- body. The freezing action should probably be delayed
1233 -- until after the spec is seen, but that's a tricky
1234 -- change to the delicate freezing code.
1236 -- Look at each declaration following the type up until the
1237 -- new subprogram body. If any of the declarations is a body
1238 -- then the type has been frozen already so the overriding
1239 -- primitive is illegal.
1241 Decl_Item := Next (Parent (Tagged_Type));
1242 while Present (Decl_Item)
1243 and then (Decl_Item /= Subp_Body)
1245 if Comes_From_Source (Decl_Item)
1246 and then (Nkind (Decl_Item) in N_Proper_Body
1247 or else Nkind (Decl_Item) in N_Body_Stub)
1249 Error_Msg_N ("overriding of& is too late!", Subp);
1251 ("\spec should appear immediately after the type!",
1259 -- If the subprogram doesn't follow in the list of
1260 -- declarations including the type then the type has
1261 -- definitely been frozen already and the body is illegal.
1263 if No (Decl_Item) then
1264 Error_Msg_N ("overriding of& is too late!", Subp);
1266 ("\spec should appear immediately after the type!",
1269 elsif Is_Frozen (Subp) then
1271 -- The subprogram body declares a primitive operation.
1272 -- If the subprogram is already frozen, we must update
1273 -- its dispatching information explicitly here. The
1274 -- information is taken from the overridden subprogram.
1275 -- We must also generate a cross-reference entry because
1276 -- references to other primitives were already created
1277 -- when type was frozen.
1279 Body_Is_Last_Primitive := True;
1281 if Present (DTC_Entity (Old_Subp)) then
1282 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1283 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1285 if not Restriction_Active (No_Dispatching_Calls) then
1286 if Building_Static_DT (Tagged_Type) then
1288 -- If the static dispatch table has not been
1289 -- built then there is nothing else to do now;
1290 -- otherwise we notify that we cannot build the
1291 -- static dispatch table.
1293 if Has_Dispatch_Table (Tagged_Type) then
1295 ("overriding of& is too late for building "
1296 & " static dispatch tables!", Subp);
1298 ("\spec should appear immediately after "
1299 & "the type!", Subp);
1302 -- No code required to register primitives in VM
1305 elsif not Tagged_Type_Expansion then
1309 Insert_Actions_After (Subp_Body,
1310 Register_Primitive (Sloc (Subp_Body),
1314 -- Indicate that this is an overriding operation,
1315 -- and replace the overridden entry in the list of
1316 -- primitive operations, which is used for xref
1317 -- generation subsequently.
1319 Generate_Reference (Tagged_Type, Subp, 'P', False);
1320 Override_Dispatching_Operation
1321 (Tagged_Type, Old_Subp, Subp);
1328 Error_Msg_N ("overriding of& is too late!", Subp);
1330 ("\subprogram spec should appear immediately after the type!",
1334 -- If the type is not frozen yet and we are not in the overriding
1335 -- case it looks suspiciously like an attempt to define a primitive
1336 -- operation, which requires the declaration to be in a package spec
1337 -- (3.2.3(6)). Only report cases where the type and subprogram are
1338 -- in the same declaration list (by checking the enclosing parent
1339 -- declarations), to avoid spurious warnings on subprograms in
1340 -- instance bodies when the type is declared in the instance spec
1341 -- but hasn't been frozen by the instance body.
1343 elsif not Is_Frozen (Tagged_Type)
1344 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1347 ("??not dispatching (must be defined in a package spec)", Subp);
1350 -- When the type is frozen, it is legitimate to define a new
1351 -- non-primitive operation.
1357 -- Now, we are sure that the scope is a package spec. If the subprogram
1358 -- is declared after the freezing point of the type that's an error
1360 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1361 Error_Msg_N ("this primitive operation is declared too late", Subp);
1363 ("??no primitive operations for& after this line",
1364 Freeze_Node (Tagged_Type),
1369 Check_Controlling_Formals (Tagged_Type, Subp);
1371 Ovr_Subp := Old_Subp;
1373 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1374 -- overridden by Subp. This only applies to source subprograms, and
1375 -- their declaration must carry an explicit overriding indicator.
1378 and then Ada_Version >= Ada_2012
1379 and then Comes_From_Source (Subp)
1381 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1383 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1385 -- Verify that the proper overriding indicator has been supplied.
1387 if Present (Ovr_Subp)
1389 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1391 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1395 -- Now it should be a correct primitive operation, put it in the list
1397 if Present (Ovr_Subp) then
1399 -- If the type has interfaces we complete this check after we set
1400 -- attribute Is_Dispatching_Operation.
1402 Check_Subtype_Conformant (Subp, Ovr_Subp);
1404 -- A primitive operation with the name of a primitive controlled
1405 -- operation does not override a non-visible overriding controlled
1406 -- operation, i.e. one declared in a private part when the full
1407 -- view of a type is controlled. Conversely, it will override a
1408 -- visible operation that may be declared in a partial view when
1409 -- the full view is controlled.
1411 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1412 and then Is_Controlled (Tagged_Type)
1413 and then not Is_Visibly_Controlled (Tagged_Type)
1414 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1416 Set_Overridden_Operation (Subp, Empty);
1418 -- If the subprogram specification carries an overriding
1419 -- indicator, no need for the warning: it is either redundant,
1420 -- or else an error will be reported.
1422 if Nkind (Parent (Subp)) = N_Procedure_Specification
1424 (Must_Override (Parent (Subp))
1425 or else Must_Not_Override (Parent (Subp)))
1429 -- Here we need the warning
1433 ("operation does not override inherited&??", Subp, Subp);
1437 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1439 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1440 -- that covers abstract interface subprograms we must register it
1441 -- in all the secondary dispatch tables associated with abstract
1442 -- interfaces. We do this now only if not building static tables,
1443 -- nor when the expander is inactive (we avoid trying to register
1444 -- primitives in semantics-only mode, since the type may not have
1445 -- an associated dispatch table). Otherwise the patch code is
1446 -- emitted after those tables are built, to prevent access before
1447 -- elaboration in gigi.
1449 if Body_Is_Last_Primitive and then Expander_Active then
1451 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1456 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1457 while Present (Elmt) loop
1458 Prim := Node (Elmt);
1460 -- No code required to register primitives in VM targets
1462 if Present (Alias (Prim))
1463 and then Present (Interface_Alias (Prim))
1464 and then Alias (Prim) = Subp
1465 and then not Building_Static_DT (Tagged_Type)
1466 and then Tagged_Type_Expansion
1468 Insert_Actions_After (Subp_Body,
1469 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1475 -- Redisplay the contents of the updated dispatch table
1477 if Debug_Flag_ZZ then
1478 Write_Str ("Late overriding: ");
1479 Write_DT (Tagged_Type);
1485 -- If the tagged type is a concurrent type then we must be compiling
1486 -- with no code generation (we are either compiling a generic unit or
1487 -- compiling under -gnatc mode) because we have previously tested that
1488 -- no serious errors has been reported. In this case we do not add the
1489 -- primitive to the list of primitives of Tagged_Type but we leave the
1490 -- primitive decorated as a dispatching operation to be able to analyze
1491 -- and report errors associated with the Object.Operation notation.
1493 elsif Is_Concurrent_Type (Tagged_Type) then
1494 pragma Assert (not Expander_Active);
1496 -- Attach operation to list of primitives of the synchronized type
1497 -- itself, for ASIS use.
1499 Add_Dispatching_Operation (Tagged_Type, Subp);
1501 -- If no old subprogram, then we add this as a dispatching operation,
1502 -- but we avoid doing this if an error was posted, to prevent annoying
1505 elsif not Error_Posted (Subp) then
1506 Add_Dispatching_Operation (Tagged_Type, Subp);
1509 Set_Is_Dispatching_Operation (Subp, True);
1511 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1512 -- subtype conformance against all the interfaces covered by this
1515 if Present (Ovr_Subp)
1516 and then Has_Interfaces (Tagged_Type)
1519 Ifaces_List : Elist_Id;
1520 Iface_Elmt : Elmt_Id;
1521 Iface_Prim_Elmt : Elmt_Id;
1522 Iface_Prim : Entity_Id;
1523 Ret_Typ : Entity_Id;
1526 Collect_Interfaces (Tagged_Type, Ifaces_List);
1528 Iface_Elmt := First_Elmt (Ifaces_List);
1529 while Present (Iface_Elmt) loop
1530 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1532 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1533 while Present (Iface_Prim_Elmt) loop
1534 Iface_Prim := Node (Iface_Prim_Elmt);
1536 if Is_Interface_Conformant
1537 (Tagged_Type, Iface_Prim, Subp)
1539 -- Handle procedures, functions whose return type
1540 -- matches, or functions not returning interfaces
1542 if Ekind (Subp) = E_Procedure
1543 or else Etype (Iface_Prim) = Etype (Subp)
1544 or else not Is_Interface (Etype (Iface_Prim))
1546 Check_Subtype_Conformant
1548 Old_Id => Iface_Prim,
1550 Skip_Controlling_Formals => True);
1552 -- Handle functions returning interfaces
1554 elsif Implements_Interface
1555 (Etype (Subp), Etype (Iface_Prim))
1557 -- Temporarily force both entities to return the
1558 -- same type. Required because Subtype_Conformant
1559 -- does not handle this case.
1561 Ret_Typ := Etype (Iface_Prim);
1562 Set_Etype (Iface_Prim, Etype (Subp));
1564 Check_Subtype_Conformant
1566 Old_Id => Iface_Prim,
1568 Skip_Controlling_Formals => True);
1570 Set_Etype (Iface_Prim, Ret_Typ);
1574 Next_Elmt (Iface_Prim_Elmt);
1578 Next_Elmt (Iface_Elmt);
1583 if not Body_Is_Last_Primitive then
1584 Set_DT_Position_Value (Subp, No_Uint);
1586 elsif Has_Controlled_Component (Tagged_Type)
1587 and then Nam_In (Chars (Subp), Name_Initialize,
1590 Name_Finalize_Address)
1593 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1597 Old_Spec : Entity_Id;
1599 C_Names : constant array (1 .. 4) of Name_Id :=
1603 Name_Finalize_Address);
1605 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1606 (TSS_Deep_Initialize,
1609 TSS_Finalize_Address);
1612 -- Remove previous controlled function which was constructed and
1613 -- analyzed when the type was frozen. This requires removing the
1614 -- body of the redefined primitive, as well as its specification
1615 -- if needed (there is no spec created for Deep_Initialize, see
1616 -- exp_ch3.adb). We must also dismantle the exception information
1617 -- that may have been generated for it when front end zero-cost
1618 -- tables are enabled.
1620 for J in D_Names'Range loop
1621 Old_P := TSS (Tagged_Type, D_Names (J));
1624 and then Chars (Subp) = C_Names (J)
1626 Old_Bod := Unit_Declaration_Node (Old_P);
1628 Set_Is_Eliminated (Old_P);
1629 Set_Scope (Old_P, Scope (Current_Scope));
1631 if Nkind (Old_Bod) = N_Subprogram_Body
1632 and then Present (Corresponding_Spec (Old_Bod))
1634 Old_Spec := Corresponding_Spec (Old_Bod);
1635 Set_Has_Completion (Old_Spec, False);
1640 Build_Late_Proc (Tagged_Type, Chars (Subp));
1642 -- The new operation is added to the actions of the freeze node
1643 -- for the type, but this node has already been analyzed, so we
1644 -- must retrieve and analyze explicitly the new body.
1647 and then Present (Actions (F_Node))
1649 Decl := Last (Actions (F_Node));
1655 -- For similarity with record extensions, in Ada 9X the language should
1656 -- have disallowed adding visible operations to a tagged type after
1657 -- deriving a private extension from it. Report a warning if this
1658 -- primitive is defined after a private extension of Tagged_Type.
1660 Warn_On_Late_Primitive_After_Private_Extension (Tagged_Type, Subp);
1661 end Check_Dispatching_Operation;
1663 ------------------------------------------
1664 -- Check_Operation_From_Incomplete_Type --
1665 ------------------------------------------
1667 procedure Check_Operation_From_Incomplete_Type
1671 Full : constant Entity_Id := Full_View (Typ);
1672 Parent_Typ : constant Entity_Id := Etype (Full);
1673 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1674 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1676 Prev : Elmt_Id := No_Elmt;
1678 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1679 -- Check that Subp has profile of an operation derived from Parent_Subp.
1680 -- Subp must have a parameter or result type that is Typ or an access
1681 -- parameter or access result type that designates Typ.
1687 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1691 if Chars (Parent_Subp) /= Chars (Subp) then
1695 -- Check that the type of controlling formals is derived from the
1696 -- parent subprogram's controlling formal type (or designated type
1697 -- if the formal type is an anonymous access type).
1699 F1 := First_Formal (Parent_Subp);
1700 F2 := First_Formal (Subp);
1701 while Present (F1) and then Present (F2) loop
1702 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1703 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1705 elsif Designated_Type (Etype (F1)) = Parent_Typ
1706 and then Designated_Type (Etype (F2)) /= Full
1711 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1714 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1722 -- Check that a controlling result type is derived from the parent
1723 -- subprogram's result type (or designated type if the result type
1724 -- is an anonymous access type).
1726 if Ekind (Parent_Subp) = E_Function then
1727 if Ekind (Subp) /= E_Function then
1730 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1731 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1734 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1735 and then Designated_Type (Etype (Subp)) /= Full
1740 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1743 elsif Etype (Parent_Subp) = Parent_Typ
1744 and then Etype (Subp) /= Full
1749 elsif Ekind (Subp) = E_Function then
1753 return No (F1) and then No (F2);
1756 -- Start of processing for Check_Operation_From_Incomplete_Type
1759 -- The operation may override an inherited one, or may be a new one
1760 -- altogether. The inherited operation will have been hidden by the
1761 -- current one at the point of the type derivation, so it does not
1762 -- appear in the list of primitive operations of the type. We have to
1763 -- find the proper place of insertion in the list of primitive opera-
1764 -- tions by iterating over the list for the parent type.
1766 Op1 := First_Elmt (Old_Prim);
1767 Op2 := First_Elmt (New_Prim);
1768 while Present (Op1) and then Present (Op2) loop
1769 if Derives_From (Node (Op1)) then
1772 -- Avoid adding it to the list of primitives if already there
1774 if Node (Op2) /= Subp then
1775 Prepend_Elmt (Subp, New_Prim);
1779 Insert_Elmt_After (Subp, Prev);
1790 -- Operation is a new primitive
1792 Append_Elmt (Subp, New_Prim);
1793 end Check_Operation_From_Incomplete_Type;
1795 ---------------------------------------
1796 -- Check_Operation_From_Private_View --
1797 ---------------------------------------
1799 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1800 Tagged_Type : Entity_Id;
1803 if Is_Dispatching_Operation (Alias (Subp)) then
1804 Set_Scope (Subp, Current_Scope);
1805 Tagged_Type := Find_Dispatching_Type (Subp);
1807 -- Add Old_Subp to primitive operations if not already present
1809 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1810 Add_Dispatching_Operation (Tagged_Type, Old_Subp);
1812 -- If Old_Subp isn't already marked as dispatching then this is
1813 -- the case of an operation of an untagged private type fulfilled
1814 -- by a tagged type that overrides an inherited dispatching
1815 -- operation, so we set the necessary dispatching attributes here.
1817 if not Is_Dispatching_Operation (Old_Subp) then
1819 -- If the untagged type has no discriminants, and the full
1820 -- view is constrained, there will be a spurious mismatch of
1821 -- subtypes on the controlling arguments, because the tagged
1822 -- type is the internal base type introduced in the derivation.
1823 -- Use the original type to verify conformance, rather than the
1826 if not Comes_From_Source (Tagged_Type)
1827 and then Has_Discriminants (Tagged_Type)
1833 Formal := First_Formal (Old_Subp);
1834 while Present (Formal) loop
1835 if Tagged_Type = Base_Type (Etype (Formal)) then
1836 Tagged_Type := Etype (Formal);
1839 Next_Formal (Formal);
1843 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1844 Tagged_Type := Etype (Old_Subp);
1848 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1849 Set_Is_Dispatching_Operation (Old_Subp, True);
1850 Set_DT_Position_Value (Old_Subp, No_Uint);
1853 -- If the old subprogram is an explicit renaming of some other
1854 -- entity, it is not overridden by the inherited subprogram.
1855 -- Otherwise, update its alias and other attributes.
1857 if Present (Alias (Old_Subp))
1858 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1859 N_Subprogram_Renaming_Declaration
1861 Set_Alias (Old_Subp, Alias (Subp));
1863 -- The derived subprogram should inherit the abstractness of
1864 -- the parent subprogram (except in the case of a function
1865 -- returning the type). This sets the abstractness properly
1866 -- for cases where a private extension may have inherited an
1867 -- abstract operation, but the full type is derived from a
1868 -- descendant type and inherits a nonabstract version.
1870 if Etype (Subp) /= Tagged_Type then
1871 Set_Is_Abstract_Subprogram
1872 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1877 end Check_Operation_From_Private_View;
1879 --------------------------
1880 -- Find_Controlling_Arg --
1881 --------------------------
1883 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1884 Orig_Node : constant Node_Id := Original_Node (N);
1888 if Nkind (Orig_Node) = N_Qualified_Expression then
1889 return Find_Controlling_Arg (Expression (Orig_Node));
1892 -- Dispatching on result case. If expansion is disabled, the node still
1893 -- has the structure of a function call. However, if the function name
1894 -- is an operator and the call was given in infix form, the original
1895 -- node has no controlling result and we must examine the current node.
1897 if Nkind (N) = N_Function_Call
1898 and then Present (Controlling_Argument (N))
1899 and then Has_Controlling_Result (Entity (Name (N)))
1901 return Controlling_Argument (N);
1903 -- If expansion is enabled, the call may have been transformed into
1904 -- an indirect call, and we need to recover the original node.
1906 elsif Nkind (Orig_Node) = N_Function_Call
1907 and then Present (Controlling_Argument (Orig_Node))
1908 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1910 return Controlling_Argument (Orig_Node);
1912 -- Type conversions are dynamically tagged if the target type, or its
1913 -- designated type, are classwide. An interface conversion expands into
1914 -- a dereference, so test must be performed on the original node.
1916 elsif Nkind (Orig_Node) = N_Type_Conversion
1917 and then Nkind (N) = N_Explicit_Dereference
1918 and then Is_Controlling_Actual (N)
1921 Target_Type : constant Entity_Id :=
1922 Entity (Subtype_Mark (Orig_Node));
1925 if Is_Class_Wide_Type (Target_Type) then
1928 elsif Is_Access_Type (Target_Type)
1929 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1940 elsif Is_Controlling_Actual (N)
1942 (Nkind (Parent (N)) = N_Qualified_Expression
1943 and then Is_Controlling_Actual (Parent (N)))
1947 if Is_Access_Type (Typ) then
1949 -- In the case of an Access attribute, use the type of the prefix,
1950 -- since in the case of an actual for an access parameter, the
1951 -- attribute's type may be of a specific designated type, even
1952 -- though the prefix type is class-wide.
1954 if Nkind (N) = N_Attribute_Reference then
1955 Typ := Etype (Prefix (N));
1957 -- An allocator is dispatching if the type of qualified expression
1958 -- is class_wide, in which case this is the controlling type.
1960 elsif Nkind (Orig_Node) = N_Allocator
1961 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1963 Typ := Etype (Expression (Orig_Node));
1965 Typ := Designated_Type (Typ);
1969 if Is_Class_Wide_Type (Typ)
1971 (Nkind (Parent (N)) = N_Qualified_Expression
1972 and then Is_Access_Type (Etype (N))
1973 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1980 end Find_Controlling_Arg;
1982 ---------------------------
1983 -- Find_Dispatching_Type --
1984 ---------------------------
1986 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1987 A_Formal : Entity_Id;
1989 Ctrl_Type : Entity_Id;
1992 if Ekind_In (Subp, E_Function, E_Procedure)
1993 and then Present (DTC_Entity (Subp))
1995 return Scope (DTC_Entity (Subp));
1997 -- For subprograms internally generated by derivations of tagged types
1998 -- use the alias subprogram as a reference to locate the dispatching
2001 elsif not Comes_From_Source (Subp)
2002 and then Present (Alias (Subp))
2003 and then Is_Dispatching_Operation (Alias (Subp))
2005 if Ekind (Alias (Subp)) = E_Function
2006 and then Has_Controlling_Result (Alias (Subp))
2008 return Check_Controlling_Type (Etype (Subp), Subp);
2011 Formal := First_Formal (Subp);
2012 A_Formal := First_Formal (Alias (Subp));
2013 while Present (A_Formal) loop
2014 if Is_Controlling_Formal (A_Formal) then
2015 return Check_Controlling_Type (Etype (Formal), Subp);
2018 Next_Formal (Formal);
2019 Next_Formal (A_Formal);
2022 pragma Assert (False);
2029 Formal := First_Formal (Subp);
2030 while Present (Formal) loop
2031 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
2033 if Present (Ctrl_Type) then
2037 Next_Formal (Formal);
2040 -- The subprogram may also be dispatching on result
2042 if Present (Etype (Subp)) then
2043 return Check_Controlling_Type (Etype (Subp), Subp);
2047 pragma Assert (not Is_Dispatching_Operation (Subp));
2049 end Find_Dispatching_Type;
2051 --------------------------------------
2052 -- Find_Hidden_Overridden_Primitive --
2053 --------------------------------------
2055 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
2057 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
2059 Orig_Prim : Entity_Id;
2061 Vis_List : Elist_Id;
2064 -- This Ada 2012 rule applies only for type extensions or private
2065 -- extensions, where the parent type is not in a parent unit, and
2066 -- where an operation is never declared but still inherited.
2069 or else not Is_Record_Type (Tag_Typ)
2070 or else Etype (Tag_Typ) = Tag_Typ
2071 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
2076 -- Collect the list of visible ancestor of the tagged type
2078 Vis_List := Visible_Ancestors (Tag_Typ);
2080 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2081 while Present (Elmt) loop
2082 Prim := Node (Elmt);
2084 -- Find an inherited hidden dispatching primitive with the name of S
2085 -- and a type-conformant profile.
2087 if Present (Alias (Prim))
2088 and then Is_Hidden (Alias (Prim))
2089 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
2090 and then Primitive_Names_Match (S, Prim)
2091 and then Type_Conformant (S, Prim)
2094 Vis_Ancestor : Elmt_Id;
2098 -- The original corresponding operation of Prim must be an
2099 -- operation of a visible ancestor of the dispatching type S,
2100 -- and the original corresponding operation of S2 must be
2103 Orig_Prim := Original_Corresponding_Operation (Prim);
2105 if Orig_Prim /= Prim
2106 and then Is_Immediately_Visible (Orig_Prim)
2108 Vis_Ancestor := First_Elmt (Vis_List);
2109 while Present (Vis_Ancestor) loop
2111 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
2112 while Present (Elmt) loop
2113 if Node (Elmt) = Orig_Prim then
2114 Set_Overridden_Operation (S, Prim);
2115 Set_Alias (Prim, Orig_Prim);
2122 Next_Elmt (Vis_Ancestor);
2132 end Find_Hidden_Overridden_Primitive;
2134 ---------------------------------------
2135 -- Find_Primitive_Covering_Interface --
2136 ---------------------------------------
2138 function Find_Primitive_Covering_Interface
2139 (Tagged_Type : Entity_Id;
2140 Iface_Prim : Entity_Id) return Entity_Id
2146 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
2147 or else (Present (Alias (Iface_Prim))
2150 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
2152 -- Search in the homonym chain. Done to speed up locating visible
2153 -- entities and required to catch primitives associated with the partial
2154 -- view of private types when processing the corresponding full view.
2156 E := Current_Entity (Iface_Prim);
2157 while Present (E) loop
2158 if Is_Subprogram (E)
2159 and then Is_Dispatching_Operation (E)
2160 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
2168 -- Search in the list of primitives of the type. Required to locate
2169 -- the covering primitive if the covering primitive is not visible
2170 -- (for example, non-visible inherited primitive of private type).
2172 El := First_Elmt (Primitive_Operations (Tagged_Type));
2173 while Present (El) loop
2176 -- Keep separate the management of internal entities that link
2177 -- primitives with interface primitives from tagged type primitives.
2179 if No (Interface_Alias (E)) then
2180 if Present (Alias (E)) then
2182 -- This interface primitive has not been covered yet
2184 if Alias (E) = Iface_Prim then
2187 -- The covering primitive was inherited
2189 elsif Overridden_Operation (Ultimate_Alias (E))
2196 -- Check if E covers the interface primitive (includes case in
2197 -- which E is an inherited private primitive).
2199 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2203 -- Use the internal entity that links the interface primitive with
2204 -- the covering primitive to locate the entity.
2206 elsif Interface_Alias (E) = Iface_Prim then
2216 end Find_Primitive_Covering_Interface;
2218 ---------------------------
2219 -- Inheritance_Utilities --
2220 ---------------------------
2222 package body Inheritance_Utilities is
2224 ---------------------------
2225 -- Inherited_Subprograms --
2226 ---------------------------
2228 function Inherited_Subprograms
2230 No_Interfaces : Boolean := False;
2231 Interfaces_Only : Boolean := False;
2232 One_Only : Boolean := False) return Subprogram_List
2234 Result : Subprogram_List (1 .. 6000);
2235 -- 6000 here is intended to be infinity. We could use an expandable
2236 -- table, but it would be awfully heavy, and there is no way that we
2237 -- could reasonably exceed this value.
2240 -- Number of entries in Result
2242 Parent_Op : Entity_Id;
2243 -- Traverses the Overridden_Operation chain
2245 procedure Store_IS (E : Entity_Id);
2246 -- Stores E in Result if not already stored
2252 procedure Store_IS (E : Entity_Id) is
2254 for J in 1 .. N loop
2255 if E = Result (J) then
2264 -- Start of processing for Inherited_Subprograms
2267 pragma Assert (not (No_Interfaces and Interfaces_Only));
2269 -- When used from backends, visibility can be handled differently
2270 -- resulting in no dispatching type being found.
2273 and then Is_Dispatching_Operation (S)
2274 and then Present (Find_DT (S))
2276 -- Deal with direct inheritance
2278 if not Interfaces_Only then
2281 Parent_Op := Overridden_Operation (Parent_Op);
2282 exit when No (Parent_Op)
2283 or else (No_Interfaces
2284 and then Is_Interface (Find_DT (Parent_Op)));
2286 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2287 Store_IS (Parent_Op);
2296 -- Now deal with interfaces
2298 if not No_Interfaces then
2300 Tag_Typ : Entity_Id;
2305 Tag_Typ := Find_DT (S);
2307 -- In the presence of limited views there may be no visible
2308 -- dispatching type. Primitives will be inherited when non-
2309 -- limited view is frozen.
2311 if No (Tag_Typ) then
2312 return Result (1 .. 0);
2315 if Is_Concurrent_Type (Tag_Typ) then
2316 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2319 -- Search primitive operations of dispatching type
2321 if Present (Tag_Typ)
2322 and then Present (Primitive_Operations (Tag_Typ))
2324 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2325 while Present (Elmt) loop
2326 Prim := Node (Elmt);
2328 -- The following test eliminates some odd cases in
2329 -- which Ekind (Prim) is Void, to be investigated
2332 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2335 -- For [generic] subprogram, look at interface
2338 elsif Present (Interface_Alias (Prim))
2339 and then Alias (Prim) = S
2341 -- We have found a primitive covered by S
2343 Store_IS (Interface_Alias (Prim));
2359 return Result (1 .. N);
2360 end Inherited_Subprograms;
2362 ------------------------------
2363 -- Is_Overriding_Subprogram --
2364 ------------------------------
2366 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2367 Inherited : constant Subprogram_List :=
2368 Inherited_Subprograms (E, One_Only => True);
2370 return Inherited'Length > 0;
2371 end Is_Overriding_Subprogram;
2372 end Inheritance_Utilities;
2374 --------------------------------
2375 -- Inheritance_Utilities_Inst --
2376 --------------------------------
2378 package Inheritance_Utilities_Inst is new
2379 Inheritance_Utilities (Find_Dispatching_Type);
2381 ---------------------------
2382 -- Inherited_Subprograms --
2383 ---------------------------
2385 function Inherited_Subprograms
2387 No_Interfaces : Boolean := False;
2388 Interfaces_Only : Boolean := False;
2389 One_Only : Boolean := False) return Subprogram_List renames
2390 Inheritance_Utilities_Inst.Inherited_Subprograms;
2392 ---------------------------
2393 -- Is_Dynamically_Tagged --
2394 ---------------------------
2396 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2398 if Nkind (N) = N_Error then
2401 elsif Present (Find_Controlling_Arg (N)) then
2404 -- Special cases: entities, and calls that dispatch on result
2406 elsif Is_Entity_Name (N) then
2407 return Is_Class_Wide_Type (Etype (N));
2409 elsif Nkind (N) = N_Function_Call
2410 and then Is_Class_Wide_Type (Etype (N))
2414 -- Otherwise check whether call has controlling argument
2419 end Is_Dynamically_Tagged;
2421 ---------------------------------
2422 -- Is_Null_Interface_Primitive --
2423 ---------------------------------
2425 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2427 return Comes_From_Source (E)
2428 and then Is_Dispatching_Operation (E)
2429 and then Ekind (E) = E_Procedure
2430 and then Null_Present (Parent (E))
2431 and then Is_Interface (Find_Dispatching_Type (E));
2432 end Is_Null_Interface_Primitive;
2434 -----------------------------------
2435 -- Is_Inherited_Public_Operation --
2436 -----------------------------------
2438 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2439 Pack_Decl : Node_Id;
2440 Prim : Entity_Id := Op;
2441 Scop : Entity_Id := Prim;
2444 -- Locate the ultimate non-hidden alias entity
2446 while Present (Alias (Prim)) and then not Is_Hidden (Alias (Prim)) loop
2447 pragma Assert (Alias (Prim) /= Prim);
2448 Prim := Alias (Prim);
2449 Scop := Scope (Prim);
2452 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2453 Pack_Decl := Unit_Declaration_Node (Scop);
2456 Nkind (Pack_Decl) = N_Package_Declaration
2457 and then List_Containing (Unit_Declaration_Node (Prim)) =
2458 Visible_Declarations (Specification (Pack_Decl));
2463 end Is_Inherited_Public_Operation;
2465 ------------------------------
2466 -- Is_Overriding_Subprogram --
2467 ------------------------------
2469 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean renames
2470 Inheritance_Utilities_Inst.Is_Overriding_Subprogram;
2472 --------------------------
2473 -- Is_Tag_Indeterminate --
2474 --------------------------
2476 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2479 Orig_Node : constant Node_Id := Original_Node (N);
2482 if Nkind (Orig_Node) = N_Function_Call
2483 and then Is_Entity_Name (Name (Orig_Node))
2485 Nam := Entity (Name (Orig_Node));
2487 if not Has_Controlling_Result (Nam) then
2490 -- The function may have a controlling result, but if the return type
2491 -- is not visibly tagged, then this is not tag-indeterminate.
2493 elsif Is_Access_Type (Etype (Nam))
2494 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2498 -- An explicit dereference means that the call has already been
2499 -- expanded and there is no tag to propagate.
2501 elsif Nkind (N) = N_Explicit_Dereference then
2504 -- If there are no actuals, the call is tag-indeterminate
2506 elsif No (Parameter_Associations (Orig_Node)) then
2510 Actual := First_Actual (Orig_Node);
2511 while Present (Actual) loop
2512 if Is_Controlling_Actual (Actual)
2513 and then not Is_Tag_Indeterminate (Actual)
2515 -- One operand is dispatching
2520 Next_Actual (Actual);
2526 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2527 return Is_Tag_Indeterminate (Expression (Orig_Node));
2529 -- Case of a call to the Input attribute (possibly rewritten), which is
2530 -- always tag-indeterminate except when its prefix is a Class attribute.
2532 elsif Nkind (Orig_Node) = N_Attribute_Reference
2534 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2535 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2539 -- In Ada 2005, a function that returns an anonymous access type can be
2540 -- dispatching, and the dereference of a call to such a function can
2541 -- also be tag-indeterminate if the call itself is.
2543 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2544 and then Ada_Version >= Ada_2005
2546 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2551 end Is_Tag_Indeterminate;
2553 ------------------------------------
2554 -- Override_Dispatching_Operation --
2555 ------------------------------------
2557 procedure Override_Dispatching_Operation
2558 (Tagged_Type : Entity_Id;
2559 Prev_Op : Entity_Id;
2561 Is_Wrapper : Boolean := False)
2567 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2568 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2570 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2571 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2572 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2575 -- If there is no previous operation to override, the type declaration
2576 -- was malformed, and an error must have been emitted already.
2578 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2579 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2587 -- The location of entities that come from source in the list of
2588 -- primitives of the tagged type must follow their order of occurrence
2589 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2590 -- primitive of an interface that is not implemented by the parents of
2591 -- this tagged type (that is, it is an alias of an interface primitive
2592 -- generated by Derive_Interface_Progenitors), then we must append the
2593 -- new entity at the end of the list of primitives.
2595 if Present (Alias (Prev_Op))
2596 and then Etype (Tagged_Type) /= Tagged_Type
2597 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2598 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2599 Tagged_Type, Use_Full_View => True)
2600 and then not Implements_Interface
2601 (Etype (Tagged_Type),
2602 Find_Dispatching_Type (Alias (Prev_Op)))
2604 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2605 Add_Dispatching_Operation (Tagged_Type, New_Op);
2607 -- The new primitive replaces the overridden entity. Required to ensure
2608 -- that overriding primitive is assigned the same dispatch table slot.
2611 Replace_Elmt (Elmt, New_Op);
2614 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2616 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2617 -- entities of the overridden primitive to reference New_Op, and
2618 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2619 -- that the new operation is subtype conformant with the interface
2620 -- operations that it implements (for operations inherited from the
2621 -- parent itself, this check is made when building the derived type).
2623 -- Note: This code is executed with internally generated wrappers of
2624 -- functions with controlling result and late overridings.
2626 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2627 while Present (Elmt) loop
2628 Prim := Node (Elmt);
2630 if Prim = New_Op then
2633 -- Note: The check on Is_Subprogram protects the frontend against
2634 -- reading attributes in entities that are not yet fully decorated
2636 elsif Is_Subprogram (Prim)
2637 and then Present (Interface_Alias (Prim))
2638 and then Alias (Prim) = Prev_Op
2640 Set_Alias (Prim, New_Op);
2642 -- No further decoration needed yet for internally generated
2643 -- wrappers of controlling functions since (at this stage)
2644 -- they are not yet decorated.
2646 if not Is_Wrapper then
2647 Check_Subtype_Conformant (New_Op, Prim);
2649 Set_Is_Abstract_Subprogram (Prim,
2650 Is_Abstract_Subprogram (New_Op));
2652 -- Ensure that this entity will be expanded to fill the
2653 -- corresponding entry in its dispatch table.
2655 if not Is_Abstract_Subprogram (Prim) then
2656 Set_Has_Delayed_Freeze (Prim);
2665 if (not Is_Package_Or_Generic_Package (Current_Scope))
2666 or else not In_Private_Part (Current_Scope)
2668 -- Not a private primitive
2672 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2674 -- Make the overriding operation into an alias of the implicit one.
2675 -- In this fashion a call from outside ends up calling the new body
2676 -- even if non-dispatching, and a call from inside calls the over-
2677 -- riding operation because it hides the implicit one. To indicate
2678 -- that the body of Prev_Op is never called, set its dispatch table
2679 -- entity to Empty. If the overridden operation has a dispatching
2680 -- result, so does the overriding one.
2682 Set_Alias (Prev_Op, New_Op);
2683 Set_DTC_Entity (Prev_Op, Empty);
2684 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2687 end Override_Dispatching_Operation;
2693 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2694 Call_Node : Node_Id;
2698 if Nkind (Actual) = N_Function_Call then
2699 Call_Node := Actual;
2701 elsif Nkind (Actual) = N_Identifier
2702 and then Nkind (Original_Node (Actual)) = N_Function_Call
2704 -- Call rewritten as object declaration when stack-checking is
2705 -- enabled. Propagate tag to expression in declaration, which is
2708 Call_Node := Expression (Parent (Entity (Actual)));
2710 -- Ada 2005: If this is a dereference of a call to a function with a
2711 -- dispatching access-result, the tag is propagated when the dereference
2712 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2714 elsif Nkind (Actual) = N_Explicit_Dereference
2715 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2719 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2720 -- and in that case we can simply return.
2722 elsif Nkind (Actual) = N_Attribute_Reference then
2723 pragma Assert (Attribute_Name (Actual) = Name_Input);
2727 -- Only other possibilities are parenthesized or qualified expression,
2728 -- or an expander-generated unchecked conversion of a function call to
2729 -- a stream Input attribute.
2732 Call_Node := Expression (Actual);
2735 -- No action needed if the call has been already expanded
2737 if Is_Expanded_Dispatching_Call (Call_Node) then
2741 -- Do not set the Controlling_Argument if already set. This happens in
2742 -- the special case of _Input (see Exp_Attr, case Input).
2744 if No (Controlling_Argument (Call_Node)) then
2745 Set_Controlling_Argument (Call_Node, Control);
2748 Arg := First_Actual (Call_Node);
2749 while Present (Arg) loop
2750 if Is_Tag_Indeterminate (Arg) then
2751 Propagate_Tag (Control, Arg);
2757 -- Expansion of dispatching calls is suppressed on VM targets, because
2758 -- the VM back-ends directly handle the generation of dispatching calls
2759 -- and would have to undo any expansion to an indirect call.
2761 if Tagged_Type_Expansion then
2763 Call_Typ : constant Entity_Id := Etype (Call_Node);
2766 Expand_Dispatching_Call (Call_Node);
2768 -- If the controlling argument is an interface type and the type
2769 -- of Call_Node differs then we must add an implicit conversion to
2770 -- force displacement of the pointer to the object to reference
2771 -- the secondary dispatch table of the interface.
2773 if Is_Interface (Etype (Control))
2774 and then Etype (Control) /= Call_Typ
2776 -- Cannot use Convert_To because the previous call to
2777 -- Expand_Dispatching_Call leaves decorated the Call_Node
2778 -- with the type of Control.
2781 Make_Type_Conversion (Sloc (Call_Node),
2783 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2784 Expression => Relocate_Node (Call_Node)));
2785 Set_Etype (Call_Node, Etype (Control));
2786 Set_Analyzed (Call_Node);
2788 Expand_Interface_Conversion (Call_Node);
2792 -- Expansion of a dispatching call results in an indirect call, which in
2793 -- turn causes current values to be killed (see Resolve_Call), so on VM
2794 -- targets we do the call here to ensure consistent warnings between VM
2795 -- and non-VM targets.
2798 Kill_Current_Values;