1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Eval; use Sem_Eval;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames; use Snames;
51 with Sinfo; use Sinfo;
52 with Tbuild; use Tbuild;
53 with Uintp; use Uintp;
55 package body Sem_Disp is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Add_Dispatching_Operation
62 (Tagged_Type : Entity_Id;
64 -- Add New_Op in the list of primitive operations of Tagged_Type
66 function Check_Controlling_Type
68 Subp : Entity_Id) return Entity_Id;
69 -- T is the tagged type of a formal parameter or the result of Subp.
70 -- If the subprogram has a controlling parameter or result that matches
71 -- the type, then returns the tagged type of that parameter or result
72 -- (returning the designated tagged type in the case of an access
73 -- parameter); otherwise returns empty.
75 -------------------------------
76 -- Add_Dispatching_Operation --
77 -------------------------------
79 procedure Add_Dispatching_Operation
80 (Tagged_Type : Entity_Id;
83 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
86 -- The dispatching operation may already be on the list, if it is the
87 -- wrapper for an inherited function of a null extension (see Exp_Ch3
88 -- for the construction of function wrappers). The list of primitive
89 -- operations must not contain duplicates.
91 Append_Unique_Elmt (New_Op, List);
92 end Add_Dispatching_Operation;
94 ---------------------------
95 -- Covers_Some_Interface --
96 ---------------------------
98 function Covers_Some_Interface (Prim : Entity_Id) return Boolean is
99 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
104 pragma Assert (Is_Dispatching_Operation (Prim));
106 -- Although this is a dispatching primitive we must check if its
107 -- dispatching type is available because it may be the primitive
108 -- of a private type not defined as tagged in its partial view.
110 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
112 -- If the tagged type is frozen then the internal entities associated
113 -- with interfaces are available in the list of primitives of the
114 -- tagged type and can be used to speed up this search.
116 if Is_Frozen (Tagged_Type) then
117 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
118 while Present (Elmt) loop
121 if Present (Interface_Alias (E))
122 and then Alias (E) = Prim
130 -- Otherwise we must collect all the interface primitives and check
131 -- if the Prim will override some interface primitive.
135 Ifaces_List : Elist_Id;
136 Iface_Elmt : Elmt_Id;
138 Iface_Prim : Entity_Id;
141 Collect_Interfaces (Tagged_Type, Ifaces_List);
142 Iface_Elmt := First_Elmt (Ifaces_List);
143 while Present (Iface_Elmt) loop
144 Iface := Node (Iface_Elmt);
146 Elmt := First_Elmt (Primitive_Operations (Iface));
147 while Present (Elmt) loop
148 Iface_Prim := Node (Elmt);
150 if Chars (E) = Chars (Prim)
151 and then Is_Interface_Conformant
152 (Tagged_Type, Iface_Prim, Prim)
160 Next_Elmt (Iface_Elmt);
167 end Covers_Some_Interface;
169 -------------------------------
170 -- Check_Controlling_Formals --
171 -------------------------------
173 procedure Check_Controlling_Formals
178 Ctrl_Type : Entity_Id;
181 Formal := First_Formal (Subp);
182 while Present (Formal) loop
183 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
185 if Present (Ctrl_Type) then
187 -- When controlling type is concurrent and declared within a
188 -- generic or inside an instance use corresponding record type.
190 if Is_Concurrent_Type (Ctrl_Type)
191 and then Present (Corresponding_Record_Type (Ctrl_Type))
193 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
196 if Ctrl_Type = Typ then
197 Set_Is_Controlling_Formal (Formal);
199 -- Ada 2005 (AI-231): Anonymous access types that are used in
200 -- controlling parameters exclude null because it is necessary
201 -- to read the tag to dispatch, and null has no tag.
203 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
204 Set_Can_Never_Be_Null (Etype (Formal));
205 Set_Is_Known_Non_Null (Etype (Formal));
208 -- Check that the parameter's nominal subtype statically
209 -- matches the first subtype.
211 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
212 if not Subtypes_Statically_Match
213 (Typ, Designated_Type (Etype (Formal)))
216 ("parameter subtype does not match controlling type",
220 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
222 ("parameter subtype does not match controlling type",
226 if Present (Default_Value (Formal)) then
228 -- In Ada 2005, access parameters can have defaults
230 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
231 and then Ada_Version < Ada_2005
234 ("default not allowed for controlling access parameter",
235 Default_Value (Formal));
237 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
239 ("default expression must be a tag indeterminate" &
240 " function call", Default_Value (Formal));
244 elsif Comes_From_Source (Subp) then
246 ("operation can be dispatching in only one type", Subp);
250 Next_Formal (Formal);
253 if Ekind_In (Subp, E_Function, E_Generic_Function) then
254 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
256 if Present (Ctrl_Type) then
257 if Ctrl_Type = Typ then
258 Set_Has_Controlling_Result (Subp);
260 -- Check that result subtype statically matches first subtype
261 -- (Ada 2005): Subp may have a controlling access result.
263 if Subtypes_Statically_Match (Typ, Etype (Subp))
264 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
266 Subtypes_Statically_Match
267 (Typ, Designated_Type (Etype (Subp))))
273 ("result subtype does not match controlling type", Subp);
276 elsif Comes_From_Source (Subp) then
278 ("operation can be dispatching in only one type", Subp);
282 end Check_Controlling_Formals;
284 ----------------------------
285 -- Check_Controlling_Type --
286 ----------------------------
288 function Check_Controlling_Type
290 Subp : Entity_Id) return Entity_Id
292 Tagged_Type : Entity_Id := Empty;
295 if Is_Tagged_Type (T) then
296 if Is_First_Subtype (T) then
299 Tagged_Type := Base_Type (T);
302 elsif Ekind (T) = E_Anonymous_Access_Type
303 and then Is_Tagged_Type (Designated_Type (T))
305 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
306 if Is_First_Subtype (Designated_Type (T)) then
307 Tagged_Type := Designated_Type (T);
309 Tagged_Type := Base_Type (Designated_Type (T));
312 -- Ada 2005: an incomplete type can be tagged. An operation with an
313 -- access parameter of the type is dispatching.
315 elsif Scope (Designated_Type (T)) = Current_Scope then
316 Tagged_Type := Designated_Type (T);
318 -- Ada 2005 (AI-50217)
320 elsif From_With_Type (Designated_Type (T))
321 and then Present (Non_Limited_View (Designated_Type (T)))
323 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
324 Tagged_Type := Non_Limited_View (Designated_Type (T));
326 Tagged_Type := Base_Type (Non_Limited_View
327 (Designated_Type (T)));
332 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
335 -- The dispatching type and the primitive operation must be defined in
336 -- the same scope, except in the case of internal operations and formal
337 -- abstract subprograms.
339 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
340 and then (not Is_Generic_Type (Tagged_Type)
341 or else not Comes_From_Source (Subp)))
343 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
345 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
347 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
349 Is_Abstract_Subprogram (Subp))
356 end Check_Controlling_Type;
358 ----------------------------
359 -- Check_Dispatching_Call --
360 ----------------------------
362 procedure Check_Dispatching_Call (N : Node_Id) is
363 Loc : constant Source_Ptr := Sloc (N);
366 Control : Node_Id := Empty;
368 Subp_Entity : Entity_Id;
369 Indeterm_Ancestor_Call : Boolean := False;
370 Indeterm_Ctrl_Type : Entity_Id;
372 Static_Tag : Node_Id := Empty;
373 -- If a controlling formal has a statically tagged actual, the tag of
374 -- this actual is to be used for any tag-indeterminate actual.
376 procedure Check_Direct_Call;
377 -- In the case when the controlling actual is a class-wide type whose
378 -- root type's completion is a task or protected type, the call is in
379 -- fact direct. This routine detects the above case and modifies the
382 procedure Check_Dispatching_Context;
383 -- If the call is tag-indeterminate and the entity being called is
384 -- abstract, verify that the context is a call that will eventually
385 -- provide a tag for dispatching, or has provided one already.
387 -----------------------
388 -- Check_Direct_Call --
389 -----------------------
391 procedure Check_Direct_Call is
392 Typ : Entity_Id := Etype (Control);
394 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean;
395 -- Determine whether an entity denotes a user-defined equality
397 ------------------------------
398 -- Is_User_Defined_Equality --
399 ------------------------------
401 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
404 Ekind (Id) = E_Function
405 and then Chars (Id) = Name_Op_Eq
406 and then Comes_From_Source (Id)
408 -- Internally generated equalities have a full type declaration
411 and then Nkind (Parent (Id)) = N_Function_Specification;
412 end Is_User_Defined_Equality;
414 -- Start of processing for Check_Direct_Call
417 -- Predefined primitives do not receive wrappers since they are built
418 -- from scratch for the corresponding record of synchronized types.
419 -- Equality is in general predefined, but is excluded from the check
420 -- when it is user-defined.
422 if Is_Predefined_Dispatching_Operation (Subp_Entity)
423 and then not Is_User_Defined_Equality (Subp_Entity)
428 if Is_Class_Wide_Type (Typ) then
429 Typ := Root_Type (Typ);
432 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
433 Typ := Full_View (Typ);
436 if Is_Concurrent_Type (Typ)
438 Present (Corresponding_Record_Type (Typ))
440 Typ := Corresponding_Record_Type (Typ);
442 -- The concurrent record's list of primitives should contain a
443 -- wrapper for the entity of the call, retrieve it.
448 Wrapper_Found : Boolean := False;
451 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
452 while Present (Prim_Elmt) loop
453 Prim := Node (Prim_Elmt);
455 if Is_Primitive_Wrapper (Prim)
456 and then Wrapped_Entity (Prim) = Subp_Entity
458 Wrapper_Found := True;
462 Next_Elmt (Prim_Elmt);
465 -- A primitive declared between two views should have a
466 -- corresponding wrapper.
468 pragma Assert (Wrapper_Found);
470 -- Modify the call by setting the proper entity
472 Set_Entity (Name (N), Prim);
475 end Check_Direct_Call;
477 -------------------------------
478 -- Check_Dispatching_Context --
479 -------------------------------
481 procedure Check_Dispatching_Context is
482 Subp : constant Entity_Id := Entity (Name (N));
486 if Is_Abstract_Subprogram (Subp)
487 and then No (Controlling_Argument (N))
489 if Present (Alias (Subp))
490 and then not Is_Abstract_Subprogram (Alias (Subp))
491 and then No (DTC_Entity (Subp))
493 -- Private overriding of inherited abstract operation, call is
496 Set_Entity (Name (N), Alias (Subp));
501 while Present (Par) loop
502 if Nkind_In (Par, N_Function_Call,
503 N_Procedure_Call_Statement,
504 N_Assignment_Statement,
507 and then Is_Tagged_Type (Etype (Subp))
511 elsif Nkind (Par) = N_Qualified_Expression
512 or else Nkind (Par) = N_Unchecked_Type_Conversion
517 if Ekind (Subp) = E_Function then
519 ("call to abstract function must be dispatching", N);
521 -- This error can occur for a procedure in the case of a
522 -- call to an abstract formal procedure with a statically
527 ("call to abstract procedure must be dispatching",
536 end Check_Dispatching_Context;
538 -- Start of processing for Check_Dispatching_Call
541 -- Find a controlling argument, if any
543 if Present (Parameter_Associations (N)) then
544 Subp_Entity := Entity (Name (N));
546 Actual := First_Actual (N);
547 Formal := First_Formal (Subp_Entity);
548 while Present (Actual) loop
549 Control := Find_Controlling_Arg (Actual);
550 exit when Present (Control);
552 -- Check for the case where the actual is a tag-indeterminate call
553 -- whose result type is different than the tagged type associated
554 -- with the containing call, but is an ancestor of the type.
556 if Is_Controlling_Formal (Formal)
557 and then Is_Tag_Indeterminate (Actual)
558 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
559 and then Is_Ancestor (Etype (Actual), Etype (Formal))
561 Indeterm_Ancestor_Call := True;
562 Indeterm_Ctrl_Type := Etype (Formal);
564 -- If the formal is controlling but the actual is not, the type
565 -- of the actual is statically known, and may be used as the
566 -- controlling tag for some other tag-indeterminate actual.
568 elsif Is_Controlling_Formal (Formal)
569 and then Is_Entity_Name (Actual)
570 and then Is_Tagged_Type (Etype (Actual))
572 Static_Tag := Actual;
575 Next_Actual (Actual);
576 Next_Formal (Formal);
579 -- If the call doesn't have a controlling actual but does have an
580 -- indeterminate actual that requires dispatching treatment, then an
581 -- object is needed that will serve as the controlling argument for a
582 -- dispatching call on the indeterminate actual. This can only occur
583 -- in the unusual situation of a default actual given by a
584 -- tag-indeterminate call and where the type of the call is an
585 -- ancestor of the type associated with a containing call to an
586 -- inherited operation (see AI-239).
588 -- Rather than create an object of the tagged type, which would be
589 -- problematic for various reasons (default initialization,
590 -- discriminants), the tag of the containing call's associated tagged
591 -- type is directly used to control the dispatching.
594 and then Indeterm_Ancestor_Call
595 and then No (Static_Tag)
598 Make_Attribute_Reference (Loc,
599 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
600 Attribute_Name => Name_Tag);
605 if Present (Control) then
607 -- Verify that no controlling arguments are statically tagged
610 Write_Str ("Found Dispatching call");
615 Actual := First_Actual (N);
616 while Present (Actual) loop
617 if Actual /= Control then
619 if not Is_Controlling_Actual (Actual) then
620 null; -- Can be anything
622 elsif Is_Dynamically_Tagged (Actual) then
623 null; -- Valid parameter
625 elsif Is_Tag_Indeterminate (Actual) then
627 -- The tag is inherited from the enclosing call (the node
628 -- we are currently analyzing). Explicitly expand the
629 -- actual, since the previous call to Expand (from
630 -- Resolve_Call) had no way of knowing about the required
633 Propagate_Tag (Control, Actual);
637 ("controlling argument is not dynamically tagged",
643 Next_Actual (Actual);
646 -- Mark call as a dispatching call
648 Set_Controlling_Argument (N, Control);
649 Check_Restriction (No_Dispatching_Calls, N);
651 -- The dispatching call may need to be converted into a direct
652 -- call in certain cases.
656 -- If there is a statically tagged actual and a tag-indeterminate
657 -- call to a function of the ancestor (such as that provided by a
658 -- default), then treat this as a dispatching call and propagate
659 -- the tag to the tag-indeterminate call(s).
661 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
663 Make_Attribute_Reference (Loc,
665 New_Occurrence_Of (Etype (Static_Tag), Loc),
666 Attribute_Name => Name_Tag);
670 Actual := First_Actual (N);
671 Formal := First_Formal (Subp_Entity);
672 while Present (Actual) loop
673 if Is_Tag_Indeterminate (Actual)
674 and then Is_Controlling_Formal (Formal)
676 Propagate_Tag (Control, Actual);
679 Next_Actual (Actual);
680 Next_Formal (Formal);
683 Check_Dispatching_Context;
686 -- The call is not dispatching, so check that there aren't any
687 -- tag-indeterminate abstract calls left.
689 Actual := First_Actual (N);
690 while Present (Actual) loop
691 if Is_Tag_Indeterminate (Actual) then
693 -- Function call case
695 if Nkind (Original_Node (Actual)) = N_Function_Call then
696 Func := Entity (Name (Original_Node (Actual)));
698 -- If the actual is an attribute then it can't be abstract
699 -- (the only current case of a tag-indeterminate attribute
700 -- is the stream Input attribute).
703 Nkind (Original_Node (Actual)) = N_Attribute_Reference
707 -- Only other possibility is a qualified expression whose
708 -- constituent expression is itself a call.
714 (Expression (Original_Node (Actual)))));
717 if Present (Func) and then Is_Abstract_Subprogram (Func) then
719 ("call to abstract function must be dispatching", N);
723 Next_Actual (Actual);
726 Check_Dispatching_Context;
730 -- If dispatching on result, the enclosing call, if any, will
731 -- determine the controlling argument. Otherwise this is the
732 -- primitive operation of the root type.
734 Check_Dispatching_Context;
736 end Check_Dispatching_Call;
738 ---------------------------------
739 -- Check_Dispatching_Operation --
740 ---------------------------------
742 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
743 Tagged_Type : Entity_Id;
744 Has_Dispatching_Parent : Boolean := False;
745 Body_Is_Last_Primitive : Boolean := False;
748 if not Ekind_In (Subp, E_Procedure, E_Function) then
752 Set_Is_Dispatching_Operation (Subp, False);
753 Tagged_Type := Find_Dispatching_Type (Subp);
755 -- Ada 2005 (AI-345): Use the corresponding record (if available).
756 -- Required because primitives of concurrent types are be attached
757 -- to the corresponding record (not to the concurrent type).
759 if Ada_Version >= Ada_2005
760 and then Present (Tagged_Type)
761 and then Is_Concurrent_Type (Tagged_Type)
762 and then Present (Corresponding_Record_Type (Tagged_Type))
764 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
767 -- (AI-345): The task body procedure is not a primitive of the tagged
770 if Present (Tagged_Type)
771 and then Is_Concurrent_Record_Type (Tagged_Type)
772 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
773 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
774 and then Subp = Get_Task_Body_Procedure
775 (Corresponding_Concurrent_Type (Tagged_Type))
780 -- If Subp is derived from a dispatching operation then it should
781 -- always be treated as dispatching. In this case various checks
782 -- below will be bypassed. Makes sure that late declarations for
783 -- inherited private subprograms are treated as dispatching, even
784 -- if the associated tagged type is already frozen.
786 Has_Dispatching_Parent :=
787 Present (Alias (Subp))
788 and then Is_Dispatching_Operation (Alias (Subp));
790 if No (Tagged_Type) then
792 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
793 -- with an abstract interface type unless the interface acts as a
794 -- parent type in a derivation. If the interface type is a formal
795 -- type then the operation is not primitive and therefore legal.
802 E := First_Entity (Subp);
803 while Present (E) loop
805 -- For an access parameter, check designated type
807 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
808 Typ := Designated_Type (Etype (E));
813 if Comes_From_Source (Subp)
814 and then Is_Interface (Typ)
815 and then not Is_Class_Wide_Type (Typ)
816 and then not Is_Derived_Type (Typ)
817 and then not Is_Generic_Type (Typ)
818 and then not In_Instance
820 Error_Msg_N ("?declaration of& is too late!", Subp);
821 Error_Msg_NE -- CODEFIX??
822 ("\spec should appear immediately after declaration of &!",
830 -- In case of functions check also the result type
832 if Ekind (Subp) = E_Function then
833 if Is_Access_Type (Etype (Subp)) then
834 Typ := Designated_Type (Etype (Subp));
839 if not Is_Class_Wide_Type (Typ)
840 and then Is_Interface (Typ)
841 and then not Is_Derived_Type (Typ)
843 Error_Msg_N ("?declaration of& is too late!", Subp);
845 ("\spec should appear immediately after declaration of &!",
853 -- The subprograms build internally after the freezing point (such as
854 -- init procs, interface thunks, type support subprograms, and Offset
855 -- to top functions for accessing interface components in variable
856 -- size tagged types) are not primitives.
858 elsif Is_Frozen (Tagged_Type)
859 and then not Comes_From_Source (Subp)
860 and then not Has_Dispatching_Parent
862 -- Complete decoration of internally built subprograms that override
863 -- a dispatching primitive. These entities correspond with the
866 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
867 -- to override functions of nonabstract null extensions. These
868 -- primitives were added to the list of primitives of the tagged
869 -- type by Make_Controlling_Function_Wrappers. However, attribute
870 -- Is_Dispatching_Operation must be set to true.
872 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
875 -- 3. Subprograms associated with stream attributes (built by
876 -- New_Stream_Subprogram)
878 if Present (Old_Subp)
879 and then Is_Overriding_Operation (Subp)
880 and then Is_Dispatching_Operation (Old_Subp)
883 ((Ekind (Subp) = E_Function
884 and then Is_Dispatching_Operation (Old_Subp)
885 and then Is_Null_Extension (Base_Type (Etype (Subp))))
887 (Ekind (Subp) = E_Procedure
888 and then Is_Dispatching_Operation (Old_Subp)
889 and then Present (Alias (Old_Subp))
890 and then Is_Null_Interface_Primitive
891 (Ultimate_Alias (Old_Subp)))
892 or else Get_TSS_Name (Subp) = TSS_Stream_Read
893 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
895 Check_Controlling_Formals (Tagged_Type, Subp);
896 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
897 Set_Is_Dispatching_Operation (Subp);
902 -- The operation may be a child unit, whose scope is the defining
903 -- package, but which is not a primitive operation of the type.
905 elsif Is_Child_Unit (Subp) then
908 -- If the subprogram is not defined in a package spec, the only case
909 -- where it can be a dispatching op is when it overrides an operation
910 -- before the freezing point of the type.
912 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
913 or else In_Package_Body (Scope (Subp)))
914 and then not Has_Dispatching_Parent
916 if not Comes_From_Source (Subp)
917 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
921 -- If the type is already frozen, the overriding is not allowed
922 -- except when Old_Subp is not a dispatching operation (which can
923 -- occur when Old_Subp was inherited by an untagged type). However,
924 -- a body with no previous spec freezes the type *after* its
925 -- declaration, and therefore is a legal overriding (unless the type
926 -- has already been frozen). Only the first such body is legal.
928 elsif Present (Old_Subp)
929 and then Is_Dispatching_Operation (Old_Subp)
931 if Comes_From_Source (Subp)
933 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
934 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
937 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
941 -- ??? The checks here for whether the type has been
942 -- frozen prior to the new body are not complete. It's
943 -- not simple to check frozenness at this point since
944 -- the body has already caused the type to be prematurely
945 -- frozen in Analyze_Declarations, but we're forced to
946 -- recheck this here because of the odd rule interpretation
947 -- that allows the overriding if the type wasn't frozen
948 -- prior to the body. The freezing action should probably
949 -- be delayed until after the spec is seen, but that's
950 -- a tricky change to the delicate freezing code.
952 -- Look at each declaration following the type up until the
953 -- new subprogram body. If any of the declarations is a body
954 -- then the type has been frozen already so the overriding
955 -- primitive is illegal.
957 Decl_Item := Next (Parent (Tagged_Type));
958 while Present (Decl_Item)
959 and then (Decl_Item /= Subp_Body)
961 if Comes_From_Source (Decl_Item)
962 and then (Nkind (Decl_Item) in N_Proper_Body
963 or else Nkind (Decl_Item) in N_Body_Stub)
965 Error_Msg_N ("overriding of& is too late!", Subp);
967 ("\spec should appear immediately after the type!",
975 -- If the subprogram doesn't follow in the list of
976 -- declarations including the type then the type has
977 -- definitely been frozen already and the body is illegal.
979 if No (Decl_Item) then
980 Error_Msg_N ("overriding of& is too late!", Subp);
982 ("\spec should appear immediately after the type!",
985 elsif Is_Frozen (Subp) then
987 -- The subprogram body declares a primitive operation.
988 -- if the subprogram is already frozen, we must update
989 -- its dispatching information explicitly here. The
990 -- information is taken from the overridden subprogram.
991 -- We must also generate a cross-reference entry because
992 -- references to other primitives were already created
993 -- when type was frozen.
995 Body_Is_Last_Primitive := True;
997 if Present (DTC_Entity (Old_Subp)) then
998 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
999 Set_DT_Position (Subp, DT_Position (Old_Subp));
1001 if not Restriction_Active (No_Dispatching_Calls) then
1002 if Building_Static_DT (Tagged_Type) then
1004 -- If the static dispatch table has not been
1005 -- built then there is nothing else to do now;
1006 -- otherwise we notify that we cannot build the
1007 -- static dispatch table.
1009 if Has_Dispatch_Table (Tagged_Type) then
1011 ("overriding of& is too late for building" &
1012 " static dispatch tables!", Subp);
1014 ("\spec should appear immediately after" &
1015 " the type!", Subp);
1019 Insert_Actions_After (Subp_Body,
1020 Register_Primitive (Sloc (Subp_Body),
1024 -- Indicate that this is an overriding operation,
1025 -- and replace the overriden entry in the list of
1026 -- primitive operations, which is used for xref
1027 -- generation subsequently.
1029 Generate_Reference (Tagged_Type, Subp, 'P', False);
1030 Override_Dispatching_Operation
1031 (Tagged_Type, Old_Subp, Subp);
1038 Error_Msg_N ("overriding of& is too late!", Subp);
1040 ("\subprogram spec should appear immediately after the type!",
1044 -- If the type is not frozen yet and we are not in the overriding
1045 -- case it looks suspiciously like an attempt to define a primitive
1046 -- operation, which requires the declaration to be in a package spec
1049 elsif not Is_Frozen (Tagged_Type) then
1051 ("?not dispatching (must be defined in a package spec)", Subp);
1054 -- When the type is frozen, it is legitimate to define a new
1055 -- non-primitive operation.
1061 -- Now, we are sure that the scope is a package spec. If the subprogram
1062 -- is declared after the freezing point of the type that's an error
1064 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1065 Error_Msg_N ("this primitive operation is declared too late", Subp);
1067 ("?no primitive operations for& after this line",
1068 Freeze_Node (Tagged_Type),
1073 Check_Controlling_Formals (Tagged_Type, Subp);
1075 -- Now it should be a correct primitive operation, put it in the list
1077 if Present (Old_Subp) then
1079 -- If the type has interfaces we complete this check after we set
1080 -- attribute Is_Dispatching_Operation.
1082 Check_Subtype_Conformant (Subp, Old_Subp);
1084 if (Chars (Subp) = Name_Initialize
1085 or else Chars (Subp) = Name_Adjust
1086 or else Chars (Subp) = Name_Finalize)
1087 and then Is_Controlled (Tagged_Type)
1088 and then not Is_Visibly_Controlled (Tagged_Type)
1090 Set_Is_Overriding_Operation (Subp, False);
1092 -- If the subprogram specification carries an overriding
1093 -- indicator, no need for the warning: it is either redundant,
1094 -- or else an error will be reported.
1096 if Nkind (Parent (Subp)) = N_Procedure_Specification
1098 (Must_Override (Parent (Subp))
1099 or else Must_Not_Override (Parent (Subp)))
1103 -- Here we need the warning
1107 ("operation does not override inherited&?", Subp, Subp);
1111 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1112 Set_Is_Overriding_Operation (Subp);
1114 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1115 -- that covers abstract interface subprograms we must register it
1116 -- in all the secondary dispatch tables associated with abstract
1117 -- interfaces. We do this now only if not building static tables.
1118 -- Otherwise the patch code is emitted after those tables are
1119 -- built, to prevent access_before_elaboration in gigi.
1121 if Body_Is_Last_Primitive then
1123 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1128 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1129 while Present (Elmt) loop
1130 Prim := Node (Elmt);
1132 if Present (Alias (Prim))
1133 and then Present (Interface_Alias (Prim))
1134 and then Alias (Prim) = Subp
1135 and then not Building_Static_DT (Tagged_Type)
1137 Insert_Actions_After (Subp_Body,
1138 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1144 -- Redisplay the contents of the updated dispatch table
1146 if Debug_Flag_ZZ then
1147 Write_Str ("Late overriding: ");
1148 Write_DT (Tagged_Type);
1154 -- If the tagged type is a concurrent type then we must be compiling
1155 -- with no code generation (we are either compiling a generic unit or
1156 -- compiling under -gnatc mode) because we have previously tested that
1157 -- no serious errors has been reported. In this case we do not add the
1158 -- primitive to the list of primitives of Tagged_Type but we leave the
1159 -- primitive decorated as a dispatching operation to be able to analyze
1160 -- and report errors associated with the Object.Operation notation.
1162 elsif Is_Concurrent_Type (Tagged_Type) then
1163 pragma Assert (not Expander_Active);
1166 -- If no old subprogram, then we add this as a dispatching operation,
1167 -- but we avoid doing this if an error was posted, to prevent annoying
1170 elsif not Error_Posted (Subp) then
1171 Add_Dispatching_Operation (Tagged_Type, Subp);
1174 Set_Is_Dispatching_Operation (Subp, True);
1176 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1177 -- subtype conformance against all the interfaces covered by this
1180 if Present (Old_Subp)
1181 and then Has_Interfaces (Tagged_Type)
1184 Ifaces_List : Elist_Id;
1185 Iface_Elmt : Elmt_Id;
1186 Iface_Prim_Elmt : Elmt_Id;
1187 Iface_Prim : Entity_Id;
1188 Ret_Typ : Entity_Id;
1191 Collect_Interfaces (Tagged_Type, Ifaces_List);
1193 Iface_Elmt := First_Elmt (Ifaces_List);
1194 while Present (Iface_Elmt) loop
1195 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1197 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1198 while Present (Iface_Prim_Elmt) loop
1199 Iface_Prim := Node (Iface_Prim_Elmt);
1201 if Is_Interface_Conformant
1202 (Tagged_Type, Iface_Prim, Subp)
1204 -- Handle procedures, functions whose return type
1205 -- matches, or functions not returning interfaces
1207 if Ekind (Subp) = E_Procedure
1208 or else Etype (Iface_Prim) = Etype (Subp)
1209 or else not Is_Interface (Etype (Iface_Prim))
1211 Check_Subtype_Conformant
1213 Old_Id => Iface_Prim,
1215 Skip_Controlling_Formals => True);
1217 -- Handle functions returning interfaces
1219 elsif Implements_Interface
1220 (Etype (Subp), Etype (Iface_Prim))
1222 -- Temporarily force both entities to return the
1223 -- same type. Required because Subtype_Conformant
1224 -- does not handle this case.
1226 Ret_Typ := Etype (Iface_Prim);
1227 Set_Etype (Iface_Prim, Etype (Subp));
1229 Check_Subtype_Conformant
1231 Old_Id => Iface_Prim,
1233 Skip_Controlling_Formals => True);
1235 Set_Etype (Iface_Prim, Ret_Typ);
1239 Next_Elmt (Iface_Prim_Elmt);
1243 Next_Elmt (Iface_Elmt);
1248 if not Body_Is_Last_Primitive then
1249 Set_DT_Position (Subp, No_Uint);
1251 elsif Has_Controlled_Component (Tagged_Type)
1253 (Chars (Subp) = Name_Initialize
1255 Chars (Subp) = Name_Adjust
1257 Chars (Subp) = Name_Finalize)
1260 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1264 Old_Spec : Entity_Id;
1266 C_Names : constant array (1 .. 3) of Name_Id :=
1271 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1272 (TSS_Deep_Initialize,
1277 -- Remove previous controlled function which was constructed and
1278 -- analyzed when the type was frozen. This requires removing the
1279 -- body of the redefined primitive, as well as its specification
1280 -- if needed (there is no spec created for Deep_Initialize, see
1281 -- exp_ch3.adb). We must also dismantle the exception information
1282 -- that may have been generated for it when front end zero-cost
1283 -- tables are enabled.
1285 for J in D_Names'Range loop
1286 Old_P := TSS (Tagged_Type, D_Names (J));
1289 and then Chars (Subp) = C_Names (J)
1291 Old_Bod := Unit_Declaration_Node (Old_P);
1293 Set_Is_Eliminated (Old_P);
1294 Set_Scope (Old_P, Scope (Current_Scope));
1296 if Nkind (Old_Bod) = N_Subprogram_Body
1297 and then Present (Corresponding_Spec (Old_Bod))
1299 Old_Spec := Corresponding_Spec (Old_Bod);
1300 Set_Has_Completion (Old_Spec, False);
1305 Build_Late_Proc (Tagged_Type, Chars (Subp));
1307 -- The new operation is added to the actions of the freeze node
1308 -- for the type, but this node has already been analyzed, so we
1309 -- must retrieve and analyze explicitly the new body.
1312 and then Present (Actions (F_Node))
1314 Decl := Last (Actions (F_Node));
1319 end Check_Dispatching_Operation;
1321 ------------------------------------------
1322 -- Check_Operation_From_Incomplete_Type --
1323 ------------------------------------------
1325 procedure Check_Operation_From_Incomplete_Type
1329 Full : constant Entity_Id := Full_View (Typ);
1330 Parent_Typ : constant Entity_Id := Etype (Full);
1331 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1332 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1334 Prev : Elmt_Id := No_Elmt;
1336 function Derives_From (Proc : Entity_Id) return Boolean;
1337 -- Check that Subp has the signature of an operation derived from Proc.
1338 -- Subp has an access parameter that designates Typ.
1344 function Derives_From (Proc : Entity_Id) return Boolean is
1348 if Chars (Proc) /= Chars (Subp) then
1352 F1 := First_Formal (Proc);
1353 F2 := First_Formal (Subp);
1354 while Present (F1) and then Present (F2) loop
1355 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1356 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1358 elsif Designated_Type (Etype (F1)) = Parent_Typ
1359 and then Designated_Type (Etype (F2)) /= Full
1364 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1367 elsif Etype (F1) /= Etype (F2) then
1375 return No (F1) and then No (F2);
1378 -- Start of processing for Check_Operation_From_Incomplete_Type
1381 -- The operation may override an inherited one, or may be a new one
1382 -- altogether. The inherited operation will have been hidden by the
1383 -- current one at the point of the type derivation, so it does not
1384 -- appear in the list of primitive operations of the type. We have to
1385 -- find the proper place of insertion in the list of primitive opera-
1386 -- tions by iterating over the list for the parent type.
1388 Op1 := First_Elmt (Old_Prim);
1389 Op2 := First_Elmt (New_Prim);
1390 while Present (Op1) and then Present (Op2) loop
1391 if Derives_From (Node (Op1)) then
1394 -- Avoid adding it to the list of primitives if already there!
1396 if Node (Op2) /= Subp then
1397 Prepend_Elmt (Subp, New_Prim);
1401 Insert_Elmt_After (Subp, Prev);
1412 -- Operation is a new primitive
1414 Append_Elmt (Subp, New_Prim);
1415 end Check_Operation_From_Incomplete_Type;
1417 ---------------------------------------
1418 -- Check_Operation_From_Private_View --
1419 ---------------------------------------
1421 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1422 Tagged_Type : Entity_Id;
1425 if Is_Dispatching_Operation (Alias (Subp)) then
1426 Set_Scope (Subp, Current_Scope);
1427 Tagged_Type := Find_Dispatching_Type (Subp);
1429 -- Add Old_Subp to primitive operations if not already present
1431 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1432 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1434 -- If Old_Subp isn't already marked as dispatching then
1435 -- this is the case of an operation of an untagged private
1436 -- type fulfilled by a tagged type that overrides an
1437 -- inherited dispatching operation, so we set the necessary
1438 -- dispatching attributes here.
1440 if not Is_Dispatching_Operation (Old_Subp) then
1442 -- If the untagged type has no discriminants, and the full
1443 -- view is constrained, there will be a spurious mismatch
1444 -- of subtypes on the controlling arguments, because the tagged
1445 -- type is the internal base type introduced in the derivation.
1446 -- Use the original type to verify conformance, rather than the
1449 if not Comes_From_Source (Tagged_Type)
1450 and then Has_Discriminants (Tagged_Type)
1456 Formal := First_Formal (Old_Subp);
1457 while Present (Formal) loop
1458 if Tagged_Type = Base_Type (Etype (Formal)) then
1459 Tagged_Type := Etype (Formal);
1462 Next_Formal (Formal);
1466 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1467 Tagged_Type := Etype (Old_Subp);
1471 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1472 Set_Is_Dispatching_Operation (Old_Subp, True);
1473 Set_DT_Position (Old_Subp, No_Uint);
1476 -- If the old subprogram is an explicit renaming of some other
1477 -- entity, it is not overridden by the inherited subprogram.
1478 -- Otherwise, update its alias and other attributes.
1480 if Present (Alias (Old_Subp))
1481 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1482 N_Subprogram_Renaming_Declaration
1484 Set_Alias (Old_Subp, Alias (Subp));
1486 -- The derived subprogram should inherit the abstractness
1487 -- of the parent subprogram (except in the case of a function
1488 -- returning the type). This sets the abstractness properly
1489 -- for cases where a private extension may have inherited
1490 -- an abstract operation, but the full type is derived from
1491 -- a descendant type and inherits a nonabstract version.
1493 if Etype (Subp) /= Tagged_Type then
1494 Set_Is_Abstract_Subprogram
1495 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1500 end Check_Operation_From_Private_View;
1502 --------------------------
1503 -- Find_Controlling_Arg --
1504 --------------------------
1506 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1507 Orig_Node : constant Node_Id := Original_Node (N);
1511 if Nkind (Orig_Node) = N_Qualified_Expression then
1512 return Find_Controlling_Arg (Expression (Orig_Node));
1515 -- Dispatching on result case. If expansion is disabled, the node still
1516 -- has the structure of a function call. However, if the function name
1517 -- is an operator and the call was given in infix form, the original
1518 -- node has no controlling result and we must examine the current node.
1520 if Nkind (N) = N_Function_Call
1521 and then Present (Controlling_Argument (N))
1522 and then Has_Controlling_Result (Entity (Name (N)))
1524 return Controlling_Argument (N);
1526 -- If expansion is enabled, the call may have been transformed into
1527 -- an indirect call, and we need to recover the original node.
1529 elsif Nkind (Orig_Node) = N_Function_Call
1530 and then Present (Controlling_Argument (Orig_Node))
1531 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1533 return Controlling_Argument (Orig_Node);
1537 elsif Is_Controlling_Actual (N)
1539 (Nkind (Parent (N)) = N_Qualified_Expression
1540 and then Is_Controlling_Actual (Parent (N)))
1544 if Is_Access_Type (Typ) then
1546 -- In the case of an Access attribute, use the type of the prefix,
1547 -- since in the case of an actual for an access parameter, the
1548 -- attribute's type may be of a specific designated type, even
1549 -- though the prefix type is class-wide.
1551 if Nkind (N) = N_Attribute_Reference then
1552 Typ := Etype (Prefix (N));
1554 -- An allocator is dispatching if the type of qualified expression
1555 -- is class_wide, in which case this is the controlling type.
1557 elsif Nkind (Orig_Node) = N_Allocator
1558 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1560 Typ := Etype (Expression (Orig_Node));
1562 Typ := Designated_Type (Typ);
1566 if Is_Class_Wide_Type (Typ)
1568 (Nkind (Parent (N)) = N_Qualified_Expression
1569 and then Is_Access_Type (Etype (N))
1570 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1577 end Find_Controlling_Arg;
1579 ---------------------------
1580 -- Find_Dispatching_Type --
1581 ---------------------------
1583 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1584 A_Formal : Entity_Id;
1586 Ctrl_Type : Entity_Id;
1589 if Present (DTC_Entity (Subp)) then
1590 return Scope (DTC_Entity (Subp));
1592 -- For subprograms internally generated by derivations of tagged types
1593 -- use the alias subprogram as a reference to locate the dispatching
1596 elsif not Comes_From_Source (Subp)
1597 and then Present (Alias (Subp))
1598 and then Is_Dispatching_Operation (Alias (Subp))
1600 if Ekind (Alias (Subp)) = E_Function
1601 and then Has_Controlling_Result (Alias (Subp))
1603 return Check_Controlling_Type (Etype (Subp), Subp);
1606 Formal := First_Formal (Subp);
1607 A_Formal := First_Formal (Alias (Subp));
1608 while Present (A_Formal) loop
1609 if Is_Controlling_Formal (A_Formal) then
1610 return Check_Controlling_Type (Etype (Formal), Subp);
1613 Next_Formal (Formal);
1614 Next_Formal (A_Formal);
1617 pragma Assert (False);
1624 Formal := First_Formal (Subp);
1625 while Present (Formal) loop
1626 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1628 if Present (Ctrl_Type) then
1632 Next_Formal (Formal);
1635 -- The subprogram may also be dispatching on result
1637 if Present (Etype (Subp)) then
1638 return Check_Controlling_Type (Etype (Subp), Subp);
1642 pragma Assert (not Is_Dispatching_Operation (Subp));
1644 end Find_Dispatching_Type;
1646 ---------------------------------------
1647 -- Find_Primitive_Covering_Interface --
1648 ---------------------------------------
1650 function Find_Primitive_Covering_Interface
1651 (Tagged_Type : Entity_Id;
1652 Iface_Prim : Entity_Id) return Entity_Id
1658 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1659 or else (Present (Alias (Iface_Prim))
1662 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1664 -- Search in the homonym chain. Done to speed up locating visible
1665 -- entities and required to catch primitives associated with the partial
1666 -- view of private types when processing the corresponding full view.
1668 E := Current_Entity (Iface_Prim);
1669 while Present (E) loop
1670 if Is_Subprogram (E)
1671 and then Is_Dispatching_Operation (E)
1672 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1680 -- Search in the list of primitives of the type. Required to locate the
1681 -- covering primitive if the covering primitive is not visible (for
1682 -- example, non-visible inherited primitive of private type).
1684 El := First_Elmt (Primitive_Operations (Tagged_Type));
1685 while Present (El) loop
1688 -- Keep separate the management of internal entities that link
1689 -- primitives with interface primitives from tagged type primitives.
1691 if No (Interface_Alias (E)) then
1692 if Present (Alias (E)) then
1694 -- This interface primitive has not been covered yet
1696 if Alias (E) = Iface_Prim then
1699 -- The covering primitive was inherited
1701 elsif Overridden_Operation (Ultimate_Alias (E))
1708 -- Use the internal entity that links the interface primitive with
1709 -- the covering primitive to locate the entity
1711 elsif Interface_Alias (E) = Iface_Prim then
1721 end Find_Primitive_Covering_Interface;
1723 ---------------------------
1724 -- Is_Dynamically_Tagged --
1725 ---------------------------
1727 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1729 if Nkind (N) = N_Error then
1732 return Find_Controlling_Arg (N) /= Empty;
1734 end Is_Dynamically_Tagged;
1736 ---------------------------------
1737 -- Is_Null_Interface_Primitive --
1738 ---------------------------------
1740 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
1742 return Comes_From_Source (E)
1743 and then Is_Dispatching_Operation (E)
1744 and then Ekind (E) = E_Procedure
1745 and then Null_Present (Parent (E))
1746 and then Is_Interface (Find_Dispatching_Type (E));
1747 end Is_Null_Interface_Primitive;
1749 --------------------------
1750 -- Is_Tag_Indeterminate --
1751 --------------------------
1753 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1756 Orig_Node : constant Node_Id := Original_Node (N);
1759 if Nkind (Orig_Node) = N_Function_Call
1760 and then Is_Entity_Name (Name (Orig_Node))
1762 Nam := Entity (Name (Orig_Node));
1764 if not Has_Controlling_Result (Nam) then
1767 -- An explicit dereference means that the call has already been
1768 -- expanded and there is no tag to propagate.
1770 elsif Nkind (N) = N_Explicit_Dereference then
1773 -- If there are no actuals, the call is tag-indeterminate
1775 elsif No (Parameter_Associations (Orig_Node)) then
1779 Actual := First_Actual (Orig_Node);
1780 while Present (Actual) loop
1781 if Is_Controlling_Actual (Actual)
1782 and then not Is_Tag_Indeterminate (Actual)
1784 return False; -- one operand is dispatching
1787 Next_Actual (Actual);
1793 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1794 return Is_Tag_Indeterminate (Expression (Orig_Node));
1796 -- Case of a call to the Input attribute (possibly rewritten), which is
1797 -- always tag-indeterminate except when its prefix is a Class attribute.
1799 elsif Nkind (Orig_Node) = N_Attribute_Reference
1801 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1803 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1807 -- In Ada 2005 a function that returns an anonymous access type can
1808 -- dispatching, and the dereference of a call to such a function
1809 -- is also tag-indeterminate.
1811 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1812 and then Ada_Version >= Ada_2005
1814 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1819 end Is_Tag_Indeterminate;
1821 ------------------------------------
1822 -- Override_Dispatching_Operation --
1823 ------------------------------------
1825 procedure Override_Dispatching_Operation
1826 (Tagged_Type : Entity_Id;
1827 Prev_Op : Entity_Id;
1834 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1835 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1837 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1838 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1839 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1842 -- If there is no previous operation to override, the type declaration
1843 -- was malformed, and an error must have been emitted already.
1845 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1846 while Present (Elmt)
1847 and then Node (Elmt) /= Prev_Op
1856 -- The location of entities that come from source in the list of
1857 -- primitives of the tagged type must follow their order of occurrence
1858 -- in the sources to fulfill the C++ ABI. If the overriden entity is a
1859 -- primitive of an interface that is not an ancestor of this tagged
1860 -- type (that is, it is an entity added to the list of primitives by
1861 -- Derive_Interface_Progenitors), then we must append the new entity
1862 -- at the end of the list of primitives.
1864 if Present (Alias (Prev_Op))
1865 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
1866 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
1869 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
1870 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
1872 -- The new primitive replaces the overriden entity. Required to ensure
1873 -- that overriding primitive is assigned the same dispatch table slot.
1876 Replace_Elmt (Elmt, New_Op);
1879 if Ada_Version >= Ada_2005
1880 and then Has_Interfaces (Tagged_Type)
1882 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1883 -- entities of the overridden primitive to reference New_Op, and also
1884 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1885 -- that the new operation is subtype conformant with the interface
1886 -- operations that it implements (for operations inherited from the
1887 -- parent itself, this check is made when building the derived type).
1889 -- Note: This code is only executed in case of late overriding
1891 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1892 while Present (Elmt) loop
1893 Prim := Node (Elmt);
1895 if Prim = New_Op then
1898 -- Note: The check on Is_Subprogram protects the frontend against
1899 -- reading attributes in entities that are not yet fully decorated
1901 elsif Is_Subprogram (Prim)
1902 and then Present (Interface_Alias (Prim))
1903 and then Alias (Prim) = Prev_Op
1904 and then Present (Etype (New_Op))
1906 Set_Alias (Prim, New_Op);
1907 Check_Subtype_Conformant (New_Op, Prim);
1908 Set_Is_Abstract_Subprogram (Prim,
1909 Is_Abstract_Subprogram (New_Op));
1911 -- Ensure that this entity will be expanded to fill the
1912 -- corresponding entry in its dispatch table.
1914 if not Is_Abstract_Subprogram (Prim) then
1915 Set_Has_Delayed_Freeze (Prim);
1923 if (not Is_Package_Or_Generic_Package (Current_Scope))
1924 or else not In_Private_Part (Current_Scope)
1926 -- Not a private primitive
1930 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1932 -- Make the overriding operation into an alias of the implicit one.
1933 -- In this fashion a call from outside ends up calling the new body
1934 -- even if non-dispatching, and a call from inside calls the
1935 -- overriding operation because it hides the implicit one. To
1936 -- indicate that the body of Prev_Op is never called, set its
1937 -- dispatch table entity to Empty. If the overridden operation
1938 -- has a dispatching result, so does the overriding one.
1940 Set_Alias (Prev_Op, New_Op);
1941 Set_DTC_Entity (Prev_Op, Empty);
1942 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
1945 end Override_Dispatching_Operation;
1951 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1952 Call_Node : Node_Id;
1956 if Nkind (Actual) = N_Function_Call then
1957 Call_Node := Actual;
1959 elsif Nkind (Actual) = N_Identifier
1960 and then Nkind (Original_Node (Actual)) = N_Function_Call
1962 -- Call rewritten as object declaration when stack-checking is
1963 -- enabled. Propagate tag to expression in declaration, which is
1966 Call_Node := Expression (Parent (Entity (Actual)));
1968 -- Ada 2005: If this is a dereference of a call to a function with a
1969 -- dispatching access-result, the tag is propagated when the dereference
1970 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1972 elsif Nkind (Actual) = N_Explicit_Dereference
1973 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1977 -- Only other possibilities are parenthesized or qualified expression,
1978 -- or an expander-generated unchecked conversion of a function call to
1979 -- a stream Input attribute.
1982 Call_Node := Expression (Actual);
1985 -- Do not set the Controlling_Argument if already set. This happens in
1986 -- the special case of _Input (see Exp_Attr, case Input).
1988 if No (Controlling_Argument (Call_Node)) then
1989 Set_Controlling_Argument (Call_Node, Control);
1992 Arg := First_Actual (Call_Node);
1994 while Present (Arg) loop
1995 if Is_Tag_Indeterminate (Arg) then
1996 Propagate_Tag (Control, Arg);
2002 -- Expansion of dispatching calls is suppressed when VM_Target, because
2003 -- the VM back-ends directly handle the generation of dispatching calls
2004 -- and would have to undo any expansion to an indirect call.
2006 if Tagged_Type_Expansion then
2008 Call_Typ : constant Entity_Id := Etype (Call_Node);
2011 Expand_Dispatching_Call (Call_Node);
2013 -- If the controlling argument is an interface type and the type
2014 -- of Call_Node differs then we must add an implicit conversion to
2015 -- force displacement of the pointer to the object to reference
2016 -- the secondary dispatch table of the interface.
2018 if Is_Interface (Etype (Control))
2019 and then Etype (Control) /= Call_Typ
2021 -- Cannot use Convert_To because the previous call to
2022 -- Expand_Dispatching_Call leaves decorated the Call_Node
2023 -- with the type of Control.
2026 Make_Type_Conversion (Sloc (Call_Node),
2028 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2029 Expression => Relocate_Node (Call_Node)));
2030 Set_Etype (Call_Node, Etype (Control));
2031 Set_Analyzed (Call_Node);
2033 Expand_Interface_Conversion (Call_Node, Is_Static => False);
2037 -- Expansion of a dispatching call results in an indirect call, which in
2038 -- turn causes current values to be killed (see Resolve_Call), so on VM
2039 -- targets we do the call here to ensure consistent warnings between VM
2040 -- and non-VM targets.
2043 Kill_Current_Values;