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 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
76 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
77 -- type of S that has the same name of S, a type-conformant profile, an
78 -- original corresponding operation O that is a primitive of a visible
79 -- ancestor of the dispatching type of S and O is visible at the point of
80 -- of declaration of S. If the entity is found the Alias of S is set to the
81 -- original corresponding operation S and its Overridden_Operation is set
82 -- to the found entity; otherwise return Empty.
84 -- This routine does not search for non-hidden primitives since they are
85 -- covered by the normal Ada 2005 rules.
87 -------------------------------
88 -- Add_Dispatching_Operation --
89 -------------------------------
91 procedure Add_Dispatching_Operation
92 (Tagged_Type : Entity_Id;
95 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
98 -- The dispatching operation may already be on the list, if it is the
99 -- wrapper for an inherited function of a null extension (see Exp_Ch3
100 -- for the construction of function wrappers). The list of primitive
101 -- operations must not contain duplicates.
103 Append_Unique_Elmt (New_Op, List);
104 end Add_Dispatching_Operation;
106 ---------------------------
107 -- Covers_Some_Interface --
108 ---------------------------
110 function Covers_Some_Interface (Prim : Entity_Id) return Boolean is
111 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
116 pragma Assert (Is_Dispatching_Operation (Prim));
118 -- Although this is a dispatching primitive we must check if its
119 -- dispatching type is available because it may be the primitive
120 -- of a private type not defined as tagged in its partial view.
122 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
124 -- If the tagged type is frozen then the internal entities associated
125 -- with interfaces are available in the list of primitives of the
126 -- tagged type and can be used to speed up this search.
128 if Is_Frozen (Tagged_Type) then
129 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
130 while Present (Elmt) loop
133 if Present (Interface_Alias (E))
134 and then Alias (E) = Prim
142 -- Otherwise we must collect all the interface primitives and check
143 -- if the Prim will override some interface primitive.
147 Ifaces_List : Elist_Id;
148 Iface_Elmt : Elmt_Id;
150 Iface_Prim : Entity_Id;
153 Collect_Interfaces (Tagged_Type, Ifaces_List);
154 Iface_Elmt := First_Elmt (Ifaces_List);
155 while Present (Iface_Elmt) loop
156 Iface := Node (Iface_Elmt);
158 Elmt := First_Elmt (Primitive_Operations (Iface));
159 while Present (Elmt) loop
160 Iface_Prim := Node (Elmt);
162 if Chars (E) = Chars (Prim)
163 and then Is_Interface_Conformant
164 (Tagged_Type, Iface_Prim, Prim)
172 Next_Elmt (Iface_Elmt);
179 end Covers_Some_Interface;
181 -------------------------------
182 -- Check_Controlling_Formals --
183 -------------------------------
185 procedure Check_Controlling_Formals
190 Ctrl_Type : Entity_Id;
193 Formal := First_Formal (Subp);
194 while Present (Formal) loop
195 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
197 if Present (Ctrl_Type) then
199 -- When controlling type is concurrent and declared within a
200 -- generic or inside an instance use corresponding record type.
202 if Is_Concurrent_Type (Ctrl_Type)
203 and then Present (Corresponding_Record_Type (Ctrl_Type))
205 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
208 if Ctrl_Type = Typ then
209 Set_Is_Controlling_Formal (Formal);
211 -- Ada 2005 (AI-231): Anonymous access types that are used in
212 -- controlling parameters exclude null because it is necessary
213 -- to read the tag to dispatch, and null has no tag.
215 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
216 Set_Can_Never_Be_Null (Etype (Formal));
217 Set_Is_Known_Non_Null (Etype (Formal));
220 -- Check that the parameter's nominal subtype statically
221 -- matches the first subtype.
223 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
224 if not Subtypes_Statically_Match
225 (Typ, Designated_Type (Etype (Formal)))
228 ("parameter subtype does not match controlling type",
232 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
234 ("parameter subtype does not match controlling type",
238 if Present (Default_Value (Formal)) then
240 -- In Ada 2005, access parameters can have defaults
242 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
243 and then Ada_Version < Ada_2005
246 ("default not allowed for controlling access parameter",
247 Default_Value (Formal));
249 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
251 ("default expression must be a tag indeterminate" &
252 " function call", Default_Value (Formal));
256 elsif Comes_From_Source (Subp) then
258 ("operation can be dispatching in only one type", Subp);
262 Next_Formal (Formal);
265 if Ekind_In (Subp, E_Function, E_Generic_Function) then
266 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
268 if Present (Ctrl_Type) then
269 if Ctrl_Type = Typ then
270 Set_Has_Controlling_Result (Subp);
272 -- Check that result subtype statically matches first subtype
273 -- (Ada 2005): Subp may have a controlling access result.
275 if Subtypes_Statically_Match (Typ, Etype (Subp))
276 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
278 Subtypes_Statically_Match
279 (Typ, Designated_Type (Etype (Subp))))
285 ("result subtype does not match controlling type", Subp);
288 elsif Comes_From_Source (Subp) then
290 ("operation can be dispatching in only one type", Subp);
294 end Check_Controlling_Formals;
296 ----------------------------
297 -- Check_Controlling_Type --
298 ----------------------------
300 function Check_Controlling_Type
302 Subp : Entity_Id) return Entity_Id
304 Tagged_Type : Entity_Id := Empty;
307 if Is_Tagged_Type (T) then
308 if Is_First_Subtype (T) then
311 Tagged_Type := Base_Type (T);
314 elsif Ekind (T) = E_Anonymous_Access_Type
315 and then Is_Tagged_Type (Designated_Type (T))
317 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
318 if Is_First_Subtype (Designated_Type (T)) then
319 Tagged_Type := Designated_Type (T);
321 Tagged_Type := Base_Type (Designated_Type (T));
324 -- Ada 2005: an incomplete type can be tagged. An operation with an
325 -- access parameter of the type is dispatching.
327 elsif Scope (Designated_Type (T)) = Current_Scope then
328 Tagged_Type := Designated_Type (T);
330 -- Ada 2005 (AI-50217)
332 elsif From_With_Type (Designated_Type (T))
333 and then Present (Non_Limited_View (Designated_Type (T)))
335 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
336 Tagged_Type := Non_Limited_View (Designated_Type (T));
338 Tagged_Type := Base_Type (Non_Limited_View
339 (Designated_Type (T)));
344 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
347 -- The dispatching type and the primitive operation must be defined in
348 -- the same scope, except in the case of internal operations and formal
349 -- abstract subprograms.
351 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
352 and then (not Is_Generic_Type (Tagged_Type)
353 or else not Comes_From_Source (Subp)))
355 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
357 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
359 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
361 Is_Abstract_Subprogram (Subp))
368 end Check_Controlling_Type;
370 ----------------------------
371 -- Check_Dispatching_Call --
372 ----------------------------
374 procedure Check_Dispatching_Call (N : Node_Id) is
375 Loc : constant Source_Ptr := Sloc (N);
378 Control : Node_Id := Empty;
380 Subp_Entity : Entity_Id;
381 Indeterm_Ancestor_Call : Boolean := False;
382 Indeterm_Ctrl_Type : Entity_Id;
384 Static_Tag : Node_Id := Empty;
385 -- If a controlling formal has a statically tagged actual, the tag of
386 -- this actual is to be used for any tag-indeterminate actual.
388 procedure Check_Direct_Call;
389 -- In the case when the controlling actual is a class-wide type whose
390 -- root type's completion is a task or protected type, the call is in
391 -- fact direct. This routine detects the above case and modifies the
394 procedure Check_Dispatching_Context;
395 -- If the call is tag-indeterminate and the entity being called is
396 -- abstract, verify that the context is a call that will eventually
397 -- provide a tag for dispatching, or has provided one already.
399 -----------------------
400 -- Check_Direct_Call --
401 -----------------------
403 procedure Check_Direct_Call is
404 Typ : Entity_Id := Etype (Control);
406 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean;
407 -- Determine whether an entity denotes a user-defined equality
409 ------------------------------
410 -- Is_User_Defined_Equality --
411 ------------------------------
413 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
416 Ekind (Id) = E_Function
417 and then Chars (Id) = Name_Op_Eq
418 and then Comes_From_Source (Id)
420 -- Internally generated equalities have a full type declaration
423 and then Nkind (Parent (Id)) = N_Function_Specification;
424 end Is_User_Defined_Equality;
426 -- Start of processing for Check_Direct_Call
429 -- Predefined primitives do not receive wrappers since they are built
430 -- from scratch for the corresponding record of synchronized types.
431 -- Equality is in general predefined, but is excluded from the check
432 -- when it is user-defined.
434 if Is_Predefined_Dispatching_Operation (Subp_Entity)
435 and then not Is_User_Defined_Equality (Subp_Entity)
440 if Is_Class_Wide_Type (Typ) then
441 Typ := Root_Type (Typ);
444 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
445 Typ := Full_View (Typ);
448 if Is_Concurrent_Type (Typ)
450 Present (Corresponding_Record_Type (Typ))
452 Typ := Corresponding_Record_Type (Typ);
454 -- The concurrent record's list of primitives should contain a
455 -- wrapper for the entity of the call, retrieve it.
460 Wrapper_Found : Boolean := False;
463 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
464 while Present (Prim_Elmt) loop
465 Prim := Node (Prim_Elmt);
467 if Is_Primitive_Wrapper (Prim)
468 and then Wrapped_Entity (Prim) = Subp_Entity
470 Wrapper_Found := True;
474 Next_Elmt (Prim_Elmt);
477 -- A primitive declared between two views should have a
478 -- corresponding wrapper.
480 pragma Assert (Wrapper_Found);
482 -- Modify the call by setting the proper entity
484 Set_Entity (Name (N), Prim);
487 end Check_Direct_Call;
489 -------------------------------
490 -- Check_Dispatching_Context --
491 -------------------------------
493 procedure Check_Dispatching_Context is
494 Subp : constant Entity_Id := Entity (Name (N));
498 if Is_Abstract_Subprogram (Subp)
499 and then No (Controlling_Argument (N))
501 if Present (Alias (Subp))
502 and then not Is_Abstract_Subprogram (Alias (Subp))
503 and then No (DTC_Entity (Subp))
505 -- Private overriding of inherited abstract operation, call is
508 Set_Entity (Name (N), Alias (Subp));
513 while Present (Par) loop
514 if Nkind_In (Par, N_Function_Call,
515 N_Procedure_Call_Statement,
516 N_Assignment_Statement,
519 and then Is_Tagged_Type (Etype (Subp))
523 elsif Nkind (Par) = N_Qualified_Expression
524 or else Nkind (Par) = N_Unchecked_Type_Conversion
529 if Ekind (Subp) = E_Function then
531 ("call to abstract function must be dispatching", N);
533 -- This error can occur for a procedure in the case of a
534 -- call to an abstract formal procedure with a statically
539 ("call to abstract procedure must be dispatching",
548 end Check_Dispatching_Context;
550 -- Start of processing for Check_Dispatching_Call
553 -- Find a controlling argument, if any
555 if Present (Parameter_Associations (N)) then
556 Subp_Entity := Entity (Name (N));
558 Actual := First_Actual (N);
559 Formal := First_Formal (Subp_Entity);
560 while Present (Actual) loop
561 Control := Find_Controlling_Arg (Actual);
562 exit when Present (Control);
564 -- Check for the case where the actual is a tag-indeterminate call
565 -- whose result type is different than the tagged type associated
566 -- with the containing call, but is an ancestor of the type.
568 if Is_Controlling_Formal (Formal)
569 and then Is_Tag_Indeterminate (Actual)
570 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
571 and then Is_Ancestor (Etype (Actual), Etype (Formal))
573 Indeterm_Ancestor_Call := True;
574 Indeterm_Ctrl_Type := Etype (Formal);
576 -- If the formal is controlling but the actual is not, the type
577 -- of the actual is statically known, and may be used as the
578 -- controlling tag for some other tag-indeterminate actual.
580 elsif Is_Controlling_Formal (Formal)
581 and then Is_Entity_Name (Actual)
582 and then Is_Tagged_Type (Etype (Actual))
584 Static_Tag := Actual;
587 Next_Actual (Actual);
588 Next_Formal (Formal);
591 -- If the call doesn't have a controlling actual but does have an
592 -- indeterminate actual that requires dispatching treatment, then an
593 -- object is needed that will serve as the controlling argument for a
594 -- dispatching call on the indeterminate actual. This can only occur
595 -- in the unusual situation of a default actual given by a
596 -- tag-indeterminate call and where the type of the call is an
597 -- ancestor of the type associated with a containing call to an
598 -- inherited operation (see AI-239).
600 -- Rather than create an object of the tagged type, which would be
601 -- problematic for various reasons (default initialization,
602 -- discriminants), the tag of the containing call's associated tagged
603 -- type is directly used to control the dispatching.
606 and then Indeterm_Ancestor_Call
607 and then No (Static_Tag)
610 Make_Attribute_Reference (Loc,
611 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
612 Attribute_Name => Name_Tag);
617 if Present (Control) then
619 -- Verify that no controlling arguments are statically tagged
622 Write_Str ("Found Dispatching call");
627 Actual := First_Actual (N);
628 while Present (Actual) loop
629 if Actual /= Control then
631 if not Is_Controlling_Actual (Actual) then
632 null; -- Can be anything
634 elsif Is_Dynamically_Tagged (Actual) then
635 null; -- Valid parameter
637 elsif Is_Tag_Indeterminate (Actual) then
639 -- The tag is inherited from the enclosing call (the node
640 -- we are currently analyzing). Explicitly expand the
641 -- actual, since the previous call to Expand (from
642 -- Resolve_Call) had no way of knowing about the required
645 Propagate_Tag (Control, Actual);
649 ("controlling argument is not dynamically tagged",
655 Next_Actual (Actual);
658 -- Mark call as a dispatching call
660 Set_Controlling_Argument (N, Control);
661 Check_Restriction (No_Dispatching_Calls, N);
663 -- The dispatching call may need to be converted into a direct
664 -- call in certain cases.
668 -- If there is a statically tagged actual and a tag-indeterminate
669 -- call to a function of the ancestor (such as that provided by a
670 -- default), then treat this as a dispatching call and propagate
671 -- the tag to the tag-indeterminate call(s).
673 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
675 Make_Attribute_Reference (Loc,
677 New_Occurrence_Of (Etype (Static_Tag), Loc),
678 Attribute_Name => Name_Tag);
682 Actual := First_Actual (N);
683 Formal := First_Formal (Subp_Entity);
684 while Present (Actual) loop
685 if Is_Tag_Indeterminate (Actual)
686 and then Is_Controlling_Formal (Formal)
688 Propagate_Tag (Control, Actual);
691 Next_Actual (Actual);
692 Next_Formal (Formal);
695 Check_Dispatching_Context;
698 -- The call is not dispatching, so check that there aren't any
699 -- tag-indeterminate abstract calls left.
701 Actual := First_Actual (N);
702 while Present (Actual) loop
703 if Is_Tag_Indeterminate (Actual) then
705 -- Function call case
707 if Nkind (Original_Node (Actual)) = N_Function_Call then
708 Func := Entity (Name (Original_Node (Actual)));
710 -- If the actual is an attribute then it can't be abstract
711 -- (the only current case of a tag-indeterminate attribute
712 -- is the stream Input attribute).
715 Nkind (Original_Node (Actual)) = N_Attribute_Reference
719 -- Only other possibility is a qualified expression whose
720 -- constituent expression is itself a call.
726 (Expression (Original_Node (Actual)))));
729 if Present (Func) and then Is_Abstract_Subprogram (Func) then
731 ("call to abstract function must be dispatching", N);
735 Next_Actual (Actual);
738 Check_Dispatching_Context;
742 -- If dispatching on result, the enclosing call, if any, will
743 -- determine the controlling argument. Otherwise this is the
744 -- primitive operation of the root type.
746 Check_Dispatching_Context;
748 end Check_Dispatching_Call;
750 ---------------------------------
751 -- Check_Dispatching_Operation --
752 ---------------------------------
754 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
755 Tagged_Type : Entity_Id;
756 Has_Dispatching_Parent : Boolean := False;
757 Body_Is_Last_Primitive : Boolean := False;
758 Ovr_Subp : Entity_Id := Empty;
761 if not Ekind_In (Subp, E_Procedure, E_Function) then
765 Set_Is_Dispatching_Operation (Subp, False);
766 Tagged_Type := Find_Dispatching_Type (Subp);
768 -- Ada 2005 (AI-345): Use the corresponding record (if available).
769 -- Required because primitives of concurrent types are be attached
770 -- to the corresponding record (not to the concurrent type).
772 if Ada_Version >= Ada_2005
773 and then Present (Tagged_Type)
774 and then Is_Concurrent_Type (Tagged_Type)
775 and then Present (Corresponding_Record_Type (Tagged_Type))
777 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
780 -- (AI-345): The task body procedure is not a primitive of the tagged
783 if Present (Tagged_Type)
784 and then Is_Concurrent_Record_Type (Tagged_Type)
785 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
786 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
787 and then Subp = Get_Task_Body_Procedure
788 (Corresponding_Concurrent_Type (Tagged_Type))
793 -- If Subp is derived from a dispatching operation then it should
794 -- always be treated as dispatching. In this case various checks
795 -- below will be bypassed. Makes sure that late declarations for
796 -- inherited private subprograms are treated as dispatching, even
797 -- if the associated tagged type is already frozen.
799 Has_Dispatching_Parent :=
800 Present (Alias (Subp))
801 and then Is_Dispatching_Operation (Alias (Subp));
803 if No (Tagged_Type) then
805 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
806 -- with an abstract interface type unless the interface acts as a
807 -- parent type in a derivation. If the interface type is a formal
808 -- type then the operation is not primitive and therefore legal.
815 E := First_Entity (Subp);
816 while Present (E) loop
818 -- For an access parameter, check designated type
820 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
821 Typ := Designated_Type (Etype (E));
826 if Comes_From_Source (Subp)
827 and then Is_Interface (Typ)
828 and then not Is_Class_Wide_Type (Typ)
829 and then not Is_Derived_Type (Typ)
830 and then not Is_Generic_Type (Typ)
831 and then not In_Instance
833 Error_Msg_N ("?declaration of& is too late!", Subp);
834 Error_Msg_NE -- CODEFIX??
835 ("\spec should appear immediately after declaration of &!",
843 -- In case of functions check also the result type
845 if Ekind (Subp) = E_Function then
846 if Is_Access_Type (Etype (Subp)) then
847 Typ := Designated_Type (Etype (Subp));
852 if not Is_Class_Wide_Type (Typ)
853 and then Is_Interface (Typ)
854 and then not Is_Derived_Type (Typ)
856 Error_Msg_N ("?declaration of& is too late!", Subp);
858 ("\spec should appear immediately after declaration of &!",
866 -- The subprograms build internally after the freezing point (such as
867 -- init procs, interface thunks, type support subprograms, and Offset
868 -- to top functions for accessing interface components in variable
869 -- size tagged types) are not primitives.
871 elsif Is_Frozen (Tagged_Type)
872 and then not Comes_From_Source (Subp)
873 and then not Has_Dispatching_Parent
875 -- Complete decoration of internally built subprograms that override
876 -- a dispatching primitive. These entities correspond with the
879 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
880 -- to override functions of nonabstract null extensions. These
881 -- primitives were added to the list of primitives of the tagged
882 -- type by Make_Controlling_Function_Wrappers. However, attribute
883 -- Is_Dispatching_Operation must be set to true.
885 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
888 -- 3. Subprograms associated with stream attributes (built by
889 -- New_Stream_Subprogram)
891 if Present (Old_Subp)
892 and then Is_Overriding_Operation (Subp)
893 and then Is_Dispatching_Operation (Old_Subp)
896 ((Ekind (Subp) = E_Function
897 and then Is_Dispatching_Operation (Old_Subp)
898 and then Is_Null_Extension (Base_Type (Etype (Subp))))
900 (Ekind (Subp) = E_Procedure
901 and then Is_Dispatching_Operation (Old_Subp)
902 and then Present (Alias (Old_Subp))
903 and then Is_Null_Interface_Primitive
904 (Ultimate_Alias (Old_Subp)))
905 or else Get_TSS_Name (Subp) = TSS_Stream_Read
906 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
908 Check_Controlling_Formals (Tagged_Type, Subp);
909 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
910 Set_Is_Dispatching_Operation (Subp);
915 -- The operation may be a child unit, whose scope is the defining
916 -- package, but which is not a primitive operation of the type.
918 elsif Is_Child_Unit (Subp) then
921 -- If the subprogram is not defined in a package spec, the only case
922 -- where it can be a dispatching op is when it overrides an operation
923 -- before the freezing point of the type.
925 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
926 or else In_Package_Body (Scope (Subp)))
927 and then not Has_Dispatching_Parent
929 if not Comes_From_Source (Subp)
930 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
934 -- If the type is already frozen, the overriding is not allowed
935 -- except when Old_Subp is not a dispatching operation (which can
936 -- occur when Old_Subp was inherited by an untagged type). However,
937 -- a body with no previous spec freezes the type *after* its
938 -- declaration, and therefore is a legal overriding (unless the type
939 -- has already been frozen). Only the first such body is legal.
941 elsif Present (Old_Subp)
942 and then Is_Dispatching_Operation (Old_Subp)
944 if Comes_From_Source (Subp)
946 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
947 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
950 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
954 -- ??? The checks here for whether the type has been
955 -- frozen prior to the new body are not complete. It's
956 -- not simple to check frozenness at this point since
957 -- the body has already caused the type to be prematurely
958 -- frozen in Analyze_Declarations, but we're forced to
959 -- recheck this here because of the odd rule interpretation
960 -- that allows the overriding if the type wasn't frozen
961 -- prior to the body. The freezing action should probably
962 -- be delayed until after the spec is seen, but that's
963 -- a tricky change to the delicate freezing code.
965 -- Look at each declaration following the type up until the
966 -- new subprogram body. If any of the declarations is a body
967 -- then the type has been frozen already so the overriding
968 -- primitive is illegal.
970 Decl_Item := Next (Parent (Tagged_Type));
971 while Present (Decl_Item)
972 and then (Decl_Item /= Subp_Body)
974 if Comes_From_Source (Decl_Item)
975 and then (Nkind (Decl_Item) in N_Proper_Body
976 or else Nkind (Decl_Item) in N_Body_Stub)
978 Error_Msg_N ("overriding of& is too late!", Subp);
980 ("\spec should appear immediately after the type!",
988 -- If the subprogram doesn't follow in the list of
989 -- declarations including the type then the type has
990 -- definitely been frozen already and the body is illegal.
992 if No (Decl_Item) then
993 Error_Msg_N ("overriding of& is too late!", Subp);
995 ("\spec should appear immediately after the type!",
998 elsif Is_Frozen (Subp) then
1000 -- The subprogram body declares a primitive operation.
1001 -- if the subprogram is already frozen, we must update
1002 -- its dispatching information explicitly here. The
1003 -- information is taken from the overridden subprogram.
1004 -- We must also generate a cross-reference entry because
1005 -- references to other primitives were already created
1006 -- when type was frozen.
1008 Body_Is_Last_Primitive := True;
1010 if Present (DTC_Entity (Old_Subp)) then
1011 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1012 Set_DT_Position (Subp, DT_Position (Old_Subp));
1014 if not Restriction_Active (No_Dispatching_Calls) then
1015 if Building_Static_DT (Tagged_Type) then
1017 -- If the static dispatch table has not been
1018 -- built then there is nothing else to do now;
1019 -- otherwise we notify that we cannot build the
1020 -- static dispatch table.
1022 if Has_Dispatch_Table (Tagged_Type) then
1024 ("overriding of& is too late for building" &
1025 " static dispatch tables!", Subp);
1027 ("\spec should appear immediately after" &
1028 " the type!", Subp);
1032 Insert_Actions_After (Subp_Body,
1033 Register_Primitive (Sloc (Subp_Body),
1037 -- Indicate that this is an overriding operation,
1038 -- and replace the overriden entry in the list of
1039 -- primitive operations, which is used for xref
1040 -- generation subsequently.
1042 Generate_Reference (Tagged_Type, Subp, 'P', False);
1043 Override_Dispatching_Operation
1044 (Tagged_Type, Old_Subp, Subp);
1051 Error_Msg_N ("overriding of& is too late!", Subp);
1053 ("\subprogram spec should appear immediately after the type!",
1057 -- If the type is not frozen yet and we are not in the overriding
1058 -- case it looks suspiciously like an attempt to define a primitive
1059 -- operation, which requires the declaration to be in a package spec
1060 -- (3.2.3(6)). Only report cases where the type and subprogram are
1061 -- in the same declaration list (by checking the enclosing parent
1062 -- declarations), to avoid spurious warnings on subprograms in
1063 -- instance bodies when the type is declared in the instance spec but
1064 -- hasn't been frozen by the instance body.
1066 elsif not Is_Frozen (Tagged_Type)
1067 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1070 ("?not dispatching (must be defined in a package spec)", Subp);
1073 -- When the type is frozen, it is legitimate to define a new
1074 -- non-primitive operation.
1080 -- Now, we are sure that the scope is a package spec. If the subprogram
1081 -- is declared after the freezing point of the type that's an error
1083 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1084 Error_Msg_N ("this primitive operation is declared too late", Subp);
1086 ("?no primitive operations for& after this line",
1087 Freeze_Node (Tagged_Type),
1092 Check_Controlling_Formals (Tagged_Type, Subp);
1094 Ovr_Subp := Old_Subp;
1096 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1097 -- overridden by Subp
1100 and then Ada_Version >= Ada_2012
1102 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1105 -- Now it should be a correct primitive operation, put it in the list
1107 if Present (Ovr_Subp) then
1109 -- If the type has interfaces we complete this check after we set
1110 -- attribute Is_Dispatching_Operation.
1112 Check_Subtype_Conformant (Subp, Ovr_Subp);
1114 if (Chars (Subp) = Name_Initialize
1115 or else Chars (Subp) = Name_Adjust
1116 or else Chars (Subp) = Name_Finalize)
1117 and then Is_Controlled (Tagged_Type)
1118 and then not Is_Visibly_Controlled (Tagged_Type)
1120 Set_Is_Overriding_Operation (Subp, False);
1122 -- If the subprogram specification carries an overriding
1123 -- indicator, no need for the warning: it is either redundant,
1124 -- or else an error will be reported.
1126 if Nkind (Parent (Subp)) = N_Procedure_Specification
1128 (Must_Override (Parent (Subp))
1129 or else Must_Not_Override (Parent (Subp)))
1133 -- Here we need the warning
1137 ("operation does not override inherited&?", Subp, Subp);
1141 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1142 Set_Is_Overriding_Operation (Subp);
1144 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1145 -- that covers abstract interface subprograms we must register it
1146 -- in all the secondary dispatch tables associated with abstract
1147 -- interfaces. We do this now only if not building static tables.
1148 -- Otherwise the patch code is emitted after those tables are
1149 -- built, to prevent access_before_elaboration in gigi.
1151 if Body_Is_Last_Primitive then
1153 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1158 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1159 while Present (Elmt) loop
1160 Prim := Node (Elmt);
1162 if Present (Alias (Prim))
1163 and then Present (Interface_Alias (Prim))
1164 and then Alias (Prim) = Subp
1165 and then not Building_Static_DT (Tagged_Type)
1167 Insert_Actions_After (Subp_Body,
1168 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1174 -- Redisplay the contents of the updated dispatch table
1176 if Debug_Flag_ZZ then
1177 Write_Str ("Late overriding: ");
1178 Write_DT (Tagged_Type);
1184 -- If the tagged type is a concurrent type then we must be compiling
1185 -- with no code generation (we are either compiling a generic unit or
1186 -- compiling under -gnatc mode) because we have previously tested that
1187 -- no serious errors has been reported. In this case we do not add the
1188 -- primitive to the list of primitives of Tagged_Type but we leave the
1189 -- primitive decorated as a dispatching operation to be able to analyze
1190 -- and report errors associated with the Object.Operation notation.
1192 elsif Is_Concurrent_Type (Tagged_Type) then
1193 pragma Assert (not Expander_Active);
1196 -- If no old subprogram, then we add this as a dispatching operation,
1197 -- but we avoid doing this if an error was posted, to prevent annoying
1200 elsif not Error_Posted (Subp) then
1201 Add_Dispatching_Operation (Tagged_Type, Subp);
1204 Set_Is_Dispatching_Operation (Subp, True);
1206 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1207 -- subtype conformance against all the interfaces covered by this
1210 if Present (Ovr_Subp)
1211 and then Has_Interfaces (Tagged_Type)
1214 Ifaces_List : Elist_Id;
1215 Iface_Elmt : Elmt_Id;
1216 Iface_Prim_Elmt : Elmt_Id;
1217 Iface_Prim : Entity_Id;
1218 Ret_Typ : Entity_Id;
1221 Collect_Interfaces (Tagged_Type, Ifaces_List);
1223 Iface_Elmt := First_Elmt (Ifaces_List);
1224 while Present (Iface_Elmt) loop
1225 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1227 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1228 while Present (Iface_Prim_Elmt) loop
1229 Iface_Prim := Node (Iface_Prim_Elmt);
1231 if Is_Interface_Conformant
1232 (Tagged_Type, Iface_Prim, Subp)
1234 -- Handle procedures, functions whose return type
1235 -- matches, or functions not returning interfaces
1237 if Ekind (Subp) = E_Procedure
1238 or else Etype (Iface_Prim) = Etype (Subp)
1239 or else not Is_Interface (Etype (Iface_Prim))
1241 Check_Subtype_Conformant
1243 Old_Id => Iface_Prim,
1245 Skip_Controlling_Formals => True);
1247 -- Handle functions returning interfaces
1249 elsif Implements_Interface
1250 (Etype (Subp), Etype (Iface_Prim))
1252 -- Temporarily force both entities to return the
1253 -- same type. Required because Subtype_Conformant
1254 -- does not handle this case.
1256 Ret_Typ := Etype (Iface_Prim);
1257 Set_Etype (Iface_Prim, Etype (Subp));
1259 Check_Subtype_Conformant
1261 Old_Id => Iface_Prim,
1263 Skip_Controlling_Formals => True);
1265 Set_Etype (Iface_Prim, Ret_Typ);
1269 Next_Elmt (Iface_Prim_Elmt);
1273 Next_Elmt (Iface_Elmt);
1278 if not Body_Is_Last_Primitive then
1279 Set_DT_Position (Subp, No_Uint);
1281 elsif Has_Controlled_Component (Tagged_Type)
1283 (Chars (Subp) = Name_Initialize
1285 Chars (Subp) = Name_Adjust
1287 Chars (Subp) = Name_Finalize)
1290 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1294 Old_Spec : Entity_Id;
1296 C_Names : constant array (1 .. 3) of Name_Id :=
1301 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1302 (TSS_Deep_Initialize,
1307 -- Remove previous controlled function which was constructed and
1308 -- analyzed when the type was frozen. This requires removing the
1309 -- body of the redefined primitive, as well as its specification
1310 -- if needed (there is no spec created for Deep_Initialize, see
1311 -- exp_ch3.adb). We must also dismantle the exception information
1312 -- that may have been generated for it when front end zero-cost
1313 -- tables are enabled.
1315 for J in D_Names'Range loop
1316 Old_P := TSS (Tagged_Type, D_Names (J));
1319 and then Chars (Subp) = C_Names (J)
1321 Old_Bod := Unit_Declaration_Node (Old_P);
1323 Set_Is_Eliminated (Old_P);
1324 Set_Scope (Old_P, Scope (Current_Scope));
1326 if Nkind (Old_Bod) = N_Subprogram_Body
1327 and then Present (Corresponding_Spec (Old_Bod))
1329 Old_Spec := Corresponding_Spec (Old_Bod);
1330 Set_Has_Completion (Old_Spec, False);
1335 Build_Late_Proc (Tagged_Type, Chars (Subp));
1337 -- The new operation is added to the actions of the freeze node
1338 -- for the type, but this node has already been analyzed, so we
1339 -- must retrieve and analyze explicitly the new body.
1342 and then Present (Actions (F_Node))
1344 Decl := Last (Actions (F_Node));
1349 end Check_Dispatching_Operation;
1351 ------------------------------------------
1352 -- Check_Operation_From_Incomplete_Type --
1353 ------------------------------------------
1355 procedure Check_Operation_From_Incomplete_Type
1359 Full : constant Entity_Id := Full_View (Typ);
1360 Parent_Typ : constant Entity_Id := Etype (Full);
1361 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1362 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1364 Prev : Elmt_Id := No_Elmt;
1366 function Derives_From (Proc : Entity_Id) return Boolean;
1367 -- Check that Subp has the signature of an operation derived from Proc.
1368 -- Subp has an access parameter that designates Typ.
1374 function Derives_From (Proc : Entity_Id) return Boolean is
1378 if Chars (Proc) /= Chars (Subp) then
1382 F1 := First_Formal (Proc);
1383 F2 := First_Formal (Subp);
1384 while Present (F1) and then Present (F2) loop
1385 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1386 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1388 elsif Designated_Type (Etype (F1)) = Parent_Typ
1389 and then Designated_Type (Etype (F2)) /= Full
1394 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1397 elsif Etype (F1) /= Etype (F2) then
1405 return No (F1) and then No (F2);
1408 -- Start of processing for Check_Operation_From_Incomplete_Type
1411 -- The operation may override an inherited one, or may be a new one
1412 -- altogether. The inherited operation will have been hidden by the
1413 -- current one at the point of the type derivation, so it does not
1414 -- appear in the list of primitive operations of the type. We have to
1415 -- find the proper place of insertion in the list of primitive opera-
1416 -- tions by iterating over the list for the parent type.
1418 Op1 := First_Elmt (Old_Prim);
1419 Op2 := First_Elmt (New_Prim);
1420 while Present (Op1) and then Present (Op2) loop
1421 if Derives_From (Node (Op1)) then
1424 -- Avoid adding it to the list of primitives if already there!
1426 if Node (Op2) /= Subp then
1427 Prepend_Elmt (Subp, New_Prim);
1431 Insert_Elmt_After (Subp, Prev);
1442 -- Operation is a new primitive
1444 Append_Elmt (Subp, New_Prim);
1445 end Check_Operation_From_Incomplete_Type;
1447 ---------------------------------------
1448 -- Check_Operation_From_Private_View --
1449 ---------------------------------------
1451 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1452 Tagged_Type : Entity_Id;
1455 if Is_Dispatching_Operation (Alias (Subp)) then
1456 Set_Scope (Subp, Current_Scope);
1457 Tagged_Type := Find_Dispatching_Type (Subp);
1459 -- Add Old_Subp to primitive operations if not already present
1461 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1462 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1464 -- If Old_Subp isn't already marked as dispatching then
1465 -- this is the case of an operation of an untagged private
1466 -- type fulfilled by a tagged type that overrides an
1467 -- inherited dispatching operation, so we set the necessary
1468 -- dispatching attributes here.
1470 if not Is_Dispatching_Operation (Old_Subp) then
1472 -- If the untagged type has no discriminants, and the full
1473 -- view is constrained, there will be a spurious mismatch
1474 -- of subtypes on the controlling arguments, because the tagged
1475 -- type is the internal base type introduced in the derivation.
1476 -- Use the original type to verify conformance, rather than the
1479 if not Comes_From_Source (Tagged_Type)
1480 and then Has_Discriminants (Tagged_Type)
1486 Formal := First_Formal (Old_Subp);
1487 while Present (Formal) loop
1488 if Tagged_Type = Base_Type (Etype (Formal)) then
1489 Tagged_Type := Etype (Formal);
1492 Next_Formal (Formal);
1496 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1497 Tagged_Type := Etype (Old_Subp);
1501 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1502 Set_Is_Dispatching_Operation (Old_Subp, True);
1503 Set_DT_Position (Old_Subp, No_Uint);
1506 -- If the old subprogram is an explicit renaming of some other
1507 -- entity, it is not overridden by the inherited subprogram.
1508 -- Otherwise, update its alias and other attributes.
1510 if Present (Alias (Old_Subp))
1511 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1512 N_Subprogram_Renaming_Declaration
1514 Set_Alias (Old_Subp, Alias (Subp));
1516 -- The derived subprogram should inherit the abstractness
1517 -- of the parent subprogram (except in the case of a function
1518 -- returning the type). This sets the abstractness properly
1519 -- for cases where a private extension may have inherited
1520 -- an abstract operation, but the full type is derived from
1521 -- a descendant type and inherits a nonabstract version.
1523 if Etype (Subp) /= Tagged_Type then
1524 Set_Is_Abstract_Subprogram
1525 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1530 end Check_Operation_From_Private_View;
1532 --------------------------
1533 -- Find_Controlling_Arg --
1534 --------------------------
1536 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1537 Orig_Node : constant Node_Id := Original_Node (N);
1541 if Nkind (Orig_Node) = N_Qualified_Expression then
1542 return Find_Controlling_Arg (Expression (Orig_Node));
1545 -- Dispatching on result case. If expansion is disabled, the node still
1546 -- has the structure of a function call. However, if the function name
1547 -- is an operator and the call was given in infix form, the original
1548 -- node has no controlling result and we must examine the current node.
1550 if Nkind (N) = N_Function_Call
1551 and then Present (Controlling_Argument (N))
1552 and then Has_Controlling_Result (Entity (Name (N)))
1554 return Controlling_Argument (N);
1556 -- If expansion is enabled, the call may have been transformed into
1557 -- an indirect call, and we need to recover the original node.
1559 elsif Nkind (Orig_Node) = N_Function_Call
1560 and then Present (Controlling_Argument (Orig_Node))
1561 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1563 return Controlling_Argument (Orig_Node);
1567 elsif Is_Controlling_Actual (N)
1569 (Nkind (Parent (N)) = N_Qualified_Expression
1570 and then Is_Controlling_Actual (Parent (N)))
1574 if Is_Access_Type (Typ) then
1576 -- In the case of an Access attribute, use the type of the prefix,
1577 -- since in the case of an actual for an access parameter, the
1578 -- attribute's type may be of a specific designated type, even
1579 -- though the prefix type is class-wide.
1581 if Nkind (N) = N_Attribute_Reference then
1582 Typ := Etype (Prefix (N));
1584 -- An allocator is dispatching if the type of qualified expression
1585 -- is class_wide, in which case this is the controlling type.
1587 elsif Nkind (Orig_Node) = N_Allocator
1588 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1590 Typ := Etype (Expression (Orig_Node));
1592 Typ := Designated_Type (Typ);
1596 if Is_Class_Wide_Type (Typ)
1598 (Nkind (Parent (N)) = N_Qualified_Expression
1599 and then Is_Access_Type (Etype (N))
1600 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1607 end Find_Controlling_Arg;
1609 ---------------------------
1610 -- Find_Dispatching_Type --
1611 ---------------------------
1613 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1614 A_Formal : Entity_Id;
1616 Ctrl_Type : Entity_Id;
1619 if Present (DTC_Entity (Subp)) then
1620 return Scope (DTC_Entity (Subp));
1622 -- For subprograms internally generated by derivations of tagged types
1623 -- use the alias subprogram as a reference to locate the dispatching
1626 elsif not Comes_From_Source (Subp)
1627 and then Present (Alias (Subp))
1628 and then Is_Dispatching_Operation (Alias (Subp))
1630 if Ekind (Alias (Subp)) = E_Function
1631 and then Has_Controlling_Result (Alias (Subp))
1633 return Check_Controlling_Type (Etype (Subp), Subp);
1636 Formal := First_Formal (Subp);
1637 A_Formal := First_Formal (Alias (Subp));
1638 while Present (A_Formal) loop
1639 if Is_Controlling_Formal (A_Formal) then
1640 return Check_Controlling_Type (Etype (Formal), Subp);
1643 Next_Formal (Formal);
1644 Next_Formal (A_Formal);
1647 pragma Assert (False);
1654 Formal := First_Formal (Subp);
1655 while Present (Formal) loop
1656 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1658 if Present (Ctrl_Type) then
1662 Next_Formal (Formal);
1665 -- The subprogram may also be dispatching on result
1667 if Present (Etype (Subp)) then
1668 return Check_Controlling_Type (Etype (Subp), Subp);
1672 pragma Assert (not Is_Dispatching_Operation (Subp));
1674 end Find_Dispatching_Type;
1676 --------------------------------------
1677 -- Find_Hidden_Overridden_Primitive --
1678 --------------------------------------
1680 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
1682 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
1684 Orig_Prim : Entity_Id;
1686 Vis_List : Elist_Id;
1689 -- This Ada 2012 rule is valid only for type extensions or private
1693 or else not Is_Record_Type (Tag_Typ)
1694 or else Etype (Tag_Typ) = Tag_Typ
1699 -- Collect the list of visible ancestor of the tagged type
1701 Vis_List := Visible_Ancestors (Tag_Typ);
1703 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
1704 while Present (Elmt) loop
1705 Prim := Node (Elmt);
1707 -- Find an inherited hidden dispatching primitive with the name of S
1708 -- and a type-conformant profile
1710 if Present (Alias (Prim))
1711 and then Is_Hidden (Alias (Prim))
1712 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
1713 and then Primitive_Names_Match (S, Prim)
1714 and then Type_Conformant (S, Prim)
1717 Vis_Ancestor : Elmt_Id;
1721 -- The original corresponding operation of Prim must be an
1722 -- operation of a visible ancestor of the dispatching type
1723 -- of S, and the original corresponding operation of S2 must
1726 Orig_Prim := Original_Corresponding_Operation (Prim);
1728 if Orig_Prim /= Prim
1729 and then Is_Immediately_Visible (Orig_Prim)
1731 Vis_Ancestor := First_Elmt (Vis_List);
1733 while Present (Vis_Ancestor) loop
1735 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
1736 while Present (Elmt) loop
1737 if Node (Elmt) = Orig_Prim then
1738 Set_Overridden_Operation (S, Prim);
1739 Set_Alias (Prim, Orig_Prim);
1747 Next_Elmt (Vis_Ancestor);
1757 end Find_Hidden_Overridden_Primitive;
1759 ---------------------------------------
1760 -- Find_Primitive_Covering_Interface --
1761 ---------------------------------------
1763 function Find_Primitive_Covering_Interface
1764 (Tagged_Type : Entity_Id;
1765 Iface_Prim : Entity_Id) return Entity_Id
1771 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1772 or else (Present (Alias (Iface_Prim))
1775 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1777 -- Search in the homonym chain. Done to speed up locating visible
1778 -- entities and required to catch primitives associated with the partial
1779 -- view of private types when processing the corresponding full view.
1781 E := Current_Entity (Iface_Prim);
1782 while Present (E) loop
1783 if Is_Subprogram (E)
1784 and then Is_Dispatching_Operation (E)
1785 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1793 -- Search in the list of primitives of the type. Required to locate the
1794 -- covering primitive if the covering primitive is not visible (for
1795 -- example, non-visible inherited primitive of private type).
1797 El := First_Elmt (Primitive_Operations (Tagged_Type));
1798 while Present (El) loop
1801 -- Keep separate the management of internal entities that link
1802 -- primitives with interface primitives from tagged type primitives.
1804 if No (Interface_Alias (E)) then
1805 if Present (Alias (E)) then
1807 -- This interface primitive has not been covered yet
1809 if Alias (E) = Iface_Prim then
1812 -- The covering primitive was inherited
1814 elsif Overridden_Operation (Ultimate_Alias (E))
1821 -- Use the internal entity that links the interface primitive with
1822 -- the covering primitive to locate the entity
1824 elsif Interface_Alias (E) = Iface_Prim then
1834 end Find_Primitive_Covering_Interface;
1836 ---------------------------
1837 -- Inherited_Subprograms --
1838 ---------------------------
1840 function Inherited_Subprograms (S : Entity_Id) return Subprogram_List is
1841 Result : Subprogram_List (1 .. 6000);
1842 -- 6000 here is intended to be infinity. We could use an expandable
1843 -- table, but it would be awfully heavy, and there is no way that we
1844 -- could reasonably exceed this value.
1847 -- Number of entries in Result
1849 Parent_Op : Entity_Id;
1850 -- Traverses the Overridden_Operation chain
1852 procedure Store_IS (E : Entity_Id);
1853 -- Stores E in Result if not already stored
1859 procedure Store_IS (E : Entity_Id) is
1861 for J in 1 .. N loop
1862 if E = Result (J) then
1871 -- Start of processing for Inherited_Subprograms
1874 if Present (S) and then Is_Dispatching_Operation (S) then
1876 -- Deal with direct inheritance
1880 Parent_Op := Overridden_Operation (Parent_Op);
1881 exit when No (Parent_Op);
1883 if Is_Subprogram (Parent_Op)
1884 or else Is_Generic_Subprogram (Parent_Op)
1886 Store_IS (Parent_Op);
1890 -- Now deal with interfaces
1893 Tag_Typ : Entity_Id;
1898 Tag_Typ := Find_Dispatching_Type (S);
1900 if Is_Concurrent_Type (Tag_Typ) then
1901 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
1904 -- Search primitive operations of dispatching type
1906 if Present (Tag_Typ)
1907 and then Present (Primitive_Operations (Tag_Typ))
1909 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
1910 while Present (Elmt) loop
1911 Prim := Node (Elmt);
1913 -- The following test eliminates some odd cases in which
1914 -- Ekind (Prim) is Void, to be investigated further ???
1916 if not (Is_Subprogram (Prim)
1918 Is_Generic_Subprogram (Prim))
1922 -- For [generic] subprogram, look at interface alias
1924 elsif Present (Interface_Alias (Prim))
1925 and then Alias (Prim) = S
1927 -- We have found a primitive covered by S
1929 Store_IS (Interface_Alias (Prim));
1938 return Result (1 .. N);
1939 end Inherited_Subprograms;
1941 ---------------------------
1942 -- Is_Dynamically_Tagged --
1943 ---------------------------
1945 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1947 if Nkind (N) = N_Error then
1950 return Find_Controlling_Arg (N) /= Empty;
1952 end Is_Dynamically_Tagged;
1954 ---------------------------------
1955 -- Is_Null_Interface_Primitive --
1956 ---------------------------------
1958 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
1960 return Comes_From_Source (E)
1961 and then Is_Dispatching_Operation (E)
1962 and then Ekind (E) = E_Procedure
1963 and then Null_Present (Parent (E))
1964 and then Is_Interface (Find_Dispatching_Type (E));
1965 end Is_Null_Interface_Primitive;
1967 --------------------------
1968 -- Is_Tag_Indeterminate --
1969 --------------------------
1971 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1974 Orig_Node : constant Node_Id := Original_Node (N);
1977 if Nkind (Orig_Node) = N_Function_Call
1978 and then Is_Entity_Name (Name (Orig_Node))
1980 Nam := Entity (Name (Orig_Node));
1982 if not Has_Controlling_Result (Nam) then
1985 -- An explicit dereference means that the call has already been
1986 -- expanded and there is no tag to propagate.
1988 elsif Nkind (N) = N_Explicit_Dereference then
1991 -- If there are no actuals, the call is tag-indeterminate
1993 elsif No (Parameter_Associations (Orig_Node)) then
1997 Actual := First_Actual (Orig_Node);
1998 while Present (Actual) loop
1999 if Is_Controlling_Actual (Actual)
2000 and then not Is_Tag_Indeterminate (Actual)
2002 return False; -- one operand is dispatching
2005 Next_Actual (Actual);
2011 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2012 return Is_Tag_Indeterminate (Expression (Orig_Node));
2014 -- Case of a call to the Input attribute (possibly rewritten), which is
2015 -- always tag-indeterminate except when its prefix is a Class attribute.
2017 elsif Nkind (Orig_Node) = N_Attribute_Reference
2019 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2021 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2025 -- In Ada 2005 a function that returns an anonymous access type can
2026 -- dispatching, and the dereference of a call to such a function
2027 -- is also tag-indeterminate.
2029 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2030 and then Ada_Version >= Ada_2005
2032 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2037 end Is_Tag_Indeterminate;
2039 ------------------------------------
2040 -- Override_Dispatching_Operation --
2041 ------------------------------------
2043 procedure Override_Dispatching_Operation
2044 (Tagged_Type : Entity_Id;
2045 Prev_Op : Entity_Id;
2052 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2053 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
2055 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2056 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2057 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2060 -- If there is no previous operation to override, the type declaration
2061 -- was malformed, and an error must have been emitted already.
2063 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2064 while Present (Elmt)
2065 and then Node (Elmt) /= Prev_Op
2074 -- The location of entities that come from source in the list of
2075 -- primitives of the tagged type must follow their order of occurrence
2076 -- in the sources to fulfill the C++ ABI. If the overriden entity is a
2077 -- primitive of an interface that is not an ancestor of this tagged
2078 -- type (that is, it is an entity added to the list of primitives by
2079 -- Derive_Interface_Progenitors), then we must append the new entity
2080 -- at the end of the list of primitives.
2082 if Present (Alias (Prev_Op))
2083 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2084 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2087 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2088 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
2090 -- The new primitive replaces the overriden entity. Required to ensure
2091 -- that overriding primitive is assigned the same dispatch table slot.
2094 Replace_Elmt (Elmt, New_Op);
2097 if Ada_Version >= Ada_2005
2098 and then Has_Interfaces (Tagged_Type)
2100 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2101 -- entities of the overridden primitive to reference New_Op, and also
2102 -- propagate the proper value of Is_Abstract_Subprogram. Verify
2103 -- that the new operation is subtype conformant with the interface
2104 -- operations that it implements (for operations inherited from the
2105 -- parent itself, this check is made when building the derived type).
2107 -- Note: This code is only executed in case of late overriding
2109 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2110 while Present (Elmt) loop
2111 Prim := Node (Elmt);
2113 if Prim = New_Op then
2116 -- Note: The check on Is_Subprogram protects the frontend against
2117 -- reading attributes in entities that are not yet fully decorated
2119 elsif Is_Subprogram (Prim)
2120 and then Present (Interface_Alias (Prim))
2121 and then Alias (Prim) = Prev_Op
2122 and then Present (Etype (New_Op))
2124 Set_Alias (Prim, New_Op);
2125 Check_Subtype_Conformant (New_Op, Prim);
2126 Set_Is_Abstract_Subprogram (Prim,
2127 Is_Abstract_Subprogram (New_Op));
2129 -- Ensure that this entity will be expanded to fill the
2130 -- corresponding entry in its dispatch table.
2132 if not Is_Abstract_Subprogram (Prim) then
2133 Set_Has_Delayed_Freeze (Prim);
2141 if (not Is_Package_Or_Generic_Package (Current_Scope))
2142 or else not In_Private_Part (Current_Scope)
2144 -- Not a private primitive
2148 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2150 -- Make the overriding operation into an alias of the implicit one.
2151 -- In this fashion a call from outside ends up calling the new body
2152 -- even if non-dispatching, and a call from inside calls the
2153 -- overriding operation because it hides the implicit one. To
2154 -- indicate that the body of Prev_Op is never called, set its
2155 -- dispatch table entity to Empty. If the overridden operation
2156 -- has a dispatching result, so does the overriding one.
2158 Set_Alias (Prev_Op, New_Op);
2159 Set_DTC_Entity (Prev_Op, Empty);
2160 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2163 end Override_Dispatching_Operation;
2169 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2170 Call_Node : Node_Id;
2174 if Nkind (Actual) = N_Function_Call then
2175 Call_Node := Actual;
2177 elsif Nkind (Actual) = N_Identifier
2178 and then Nkind (Original_Node (Actual)) = N_Function_Call
2180 -- Call rewritten as object declaration when stack-checking is
2181 -- enabled. Propagate tag to expression in declaration, which is
2184 Call_Node := Expression (Parent (Entity (Actual)));
2186 -- Ada 2005: If this is a dereference of a call to a function with a
2187 -- dispatching access-result, the tag is propagated when the dereference
2188 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2190 elsif Nkind (Actual) = N_Explicit_Dereference
2191 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2195 -- Only other possibilities are parenthesized or qualified expression,
2196 -- or an expander-generated unchecked conversion of a function call to
2197 -- a stream Input attribute.
2200 Call_Node := Expression (Actual);
2203 -- Do not set the Controlling_Argument if already set. This happens in
2204 -- the special case of _Input (see Exp_Attr, case Input).
2206 if No (Controlling_Argument (Call_Node)) then
2207 Set_Controlling_Argument (Call_Node, Control);
2210 Arg := First_Actual (Call_Node);
2212 while Present (Arg) loop
2213 if Is_Tag_Indeterminate (Arg) then
2214 Propagate_Tag (Control, Arg);
2220 -- Expansion of dispatching calls is suppressed when VM_Target, because
2221 -- the VM back-ends directly handle the generation of dispatching calls
2222 -- and would have to undo any expansion to an indirect call.
2224 if Tagged_Type_Expansion then
2226 Call_Typ : constant Entity_Id := Etype (Call_Node);
2229 Expand_Dispatching_Call (Call_Node);
2231 -- If the controlling argument is an interface type and the type
2232 -- of Call_Node differs then we must add an implicit conversion to
2233 -- force displacement of the pointer to the object to reference
2234 -- the secondary dispatch table of the interface.
2236 if Is_Interface (Etype (Control))
2237 and then Etype (Control) /= Call_Typ
2239 -- Cannot use Convert_To because the previous call to
2240 -- Expand_Dispatching_Call leaves decorated the Call_Node
2241 -- with the type of Control.
2244 Make_Type_Conversion (Sloc (Call_Node),
2246 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2247 Expression => Relocate_Node (Call_Node)));
2248 Set_Etype (Call_Node, Etype (Control));
2249 Set_Analyzed (Call_Node);
2251 Expand_Interface_Conversion (Call_Node, Is_Static => False);
2255 -- Expansion of a dispatching call results in an indirect call, which in
2256 -- turn causes current values to be killed (see Resolve_Call), so on VM
2257 -- targets we do the call here to ensure consistent warnings between VM
2258 -- and non-VM targets.
2261 Kill_Current_Values;