1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Exp_Disp; use Exp_Disp;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Tss; use Exp_Tss;
34 with Errout; use Errout;
35 with Hostparm; use Hostparm;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
39 with Output; use Output;
41 with Sem_Ch6; use Sem_Ch6;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Type; use Sem_Type;
44 with Sem_Util; use Sem_Util;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Sinfo; use Sinfo;
48 with Tbuild; use Tbuild;
49 with Uintp; use Uintp;
51 package body Sem_Disp is
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Override_Dispatching_Operation
58 (Tagged_Type : Entity_Id;
61 -- Replace an implicit dispatching operation with an explicit one.
62 -- Prev_Op is an inherited primitive operation which is overridden
63 -- by the explicit declaration of New_Op.
65 procedure Add_Dispatching_Operation
66 (Tagged_Type : Entity_Id;
68 -- Add New_Op in the list of primitive operations of Tagged_Type
70 function Check_Controlling_Type
72 Subp : Entity_Id) return Entity_Id;
73 -- T is the tagged type of a formal parameter or the result of Subp.
74 -- If the subprogram has a controlling parameter or result that matches
75 -- the type, then returns the tagged type of that parameter or result
76 -- (returning the designated tagged type in the case of an access
77 -- parameter); otherwise returns empty.
79 -------------------------------
80 -- Add_Dispatching_Operation --
81 -------------------------------
83 procedure Add_Dispatching_Operation
84 (Tagged_Type : Entity_Id;
87 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
89 Append_Elmt (New_Op, List);
90 end Add_Dispatching_Operation;
92 -------------------------------
93 -- Check_Controlling_Formals --
94 -------------------------------
96 procedure Check_Controlling_Formals
101 Ctrl_Type : Entity_Id;
102 Remote : constant Boolean :=
103 Is_Remote_Types (Current_Scope)
104 and then Comes_From_Source (Subp)
105 and then Scope (Typ) = Current_Scope;
108 Formal := First_Formal (Subp);
110 while Present (Formal) loop
111 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
113 if Present (Ctrl_Type) then
114 if Ctrl_Type = Typ then
115 Set_Is_Controlling_Formal (Formal);
117 -- Check that the parameter's nominal subtype statically
118 -- matches the first subtype.
120 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
121 if not Subtypes_Statically_Match
122 (Typ, Designated_Type (Etype (Formal)))
125 ("parameter subtype does not match controlling type",
129 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
131 ("parameter subtype does not match controlling type",
135 if Present (Default_Value (Formal)) then
136 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
138 ("default not allowed for controlling access parameter",
139 Default_Value (Formal));
141 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
143 ("default expression must be a tag indeterminate" &
144 " function call", Default_Value (Formal));
148 elsif Comes_From_Source (Subp) then
150 ("operation can be dispatching in only one type", Subp);
153 -- Verify that the restriction in E.2.2 (14) is obeyed
156 and then Ekind (Etype (Formal)) = E_Anonymous_Access_Type
159 ("access parameter of remote object primitive"
160 & " must be controlling",
164 Next_Formal (Formal);
167 if Present (Etype (Subp)) then
168 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
170 if Present (Ctrl_Type) then
171 if Ctrl_Type = Typ then
172 Set_Has_Controlling_Result (Subp);
174 -- Check that the result subtype statically matches
175 -- the first subtype.
177 if not Subtypes_Statically_Match (Typ, Etype (Subp)) then
179 ("result subtype does not match controlling type", Subp);
182 elsif Comes_From_Source (Subp) then
184 ("operation can be dispatching in only one type", Subp);
187 -- The following check is clearly required, although the RM says
188 -- nothing about return types. If the return type is a limited
189 -- class-wide type declared in the current scope, there is no way
190 -- to declare stream procedures for it, so the return cannot be
194 and then Is_Limited_Type (Typ)
195 and then Etype (Subp) = Class_Wide_Type (Typ)
197 Error_Msg_N ("return type has no stream attributes", Subp);
200 end Check_Controlling_Formals;
202 ----------------------------
203 -- Check_Controlling_Type --
204 ----------------------------
206 function Check_Controlling_Type
208 Subp : Entity_Id) return Entity_Id
210 Tagged_Type : Entity_Id := Empty;
213 if Is_Tagged_Type (T) then
214 if Is_First_Subtype (T) then
217 Tagged_Type := Base_Type (T);
220 elsif Ekind (T) = E_Anonymous_Access_Type
221 and then Is_Tagged_Type (Designated_Type (T))
222 and then Ekind (Designated_Type (T)) /= E_Incomplete_Type
224 if Is_First_Subtype (Designated_Type (T)) then
225 Tagged_Type := Designated_Type (T);
227 Tagged_Type := Base_Type (Designated_Type (T));
232 or else Is_Class_Wide_Type (Tagged_Type)
236 -- The dispatching type and the primitive operation must be defined
237 -- in the same scope, except in the case of internal operations and
238 -- formal abstract subprograms.
240 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
241 and then (not Is_Generic_Type (Tagged_Type)
242 or else not Comes_From_Source (Subp)))
244 (Is_Formal_Subprogram (Subp) and then Is_Abstract (Subp))
246 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
248 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
257 end Check_Controlling_Type;
259 ----------------------------
260 -- Check_Dispatching_Call --
261 ----------------------------
263 procedure Check_Dispatching_Call (N : Node_Id) is
266 Control : Node_Id := Empty;
268 Subp_Entity : Entity_Id;
269 Loc : constant Source_Ptr := Sloc (N);
270 Indeterm_Ancestor_Call : Boolean := False;
271 Indeterm_Ctrl_Type : Entity_Id;
273 procedure Check_Dispatching_Context;
274 -- If the call is tag-indeterminate and the entity being called is
275 -- abstract, verify that the context is a call that will eventually
276 -- provide a tag for dispatching, or has provided one already.
278 -------------------------------
279 -- Check_Dispatching_Context --
280 -------------------------------
282 procedure Check_Dispatching_Context is
283 Subp : constant Entity_Id := Entity (Name (N));
287 if Is_Abstract (Subp)
288 and then No (Controlling_Argument (N))
290 if Present (Alias (Subp))
291 and then not Is_Abstract (Alias (Subp))
292 and then No (DTC_Entity (Subp))
294 -- Private overriding of inherited abstract operation,
297 Set_Entity (Name (N), Alias (Subp));
303 while Present (Par) loop
305 if (Nkind (Par) = N_Function_Call or else
306 Nkind (Par) = N_Procedure_Call_Statement or else
307 Nkind (Par) = N_Assignment_Statement or else
308 Nkind (Par) = N_Op_Eq or else
309 Nkind (Par) = N_Op_Ne)
310 and then Is_Tagged_Type (Etype (Subp))
314 elsif Nkind (Par) = N_Qualified_Expression
315 or else Nkind (Par) = N_Unchecked_Type_Conversion
320 if Ekind (Subp) = E_Function then
322 ("call to abstract function must be dispatching", N);
324 -- This error can occur for a procedure in the case of a
325 -- call to an abstract formal procedure with a statically
330 ("call to abstract procedure must be dispatching",
339 end Check_Dispatching_Context;
341 -- Start of processing for Check_Dispatching_Call
344 -- Find a controlling argument, if any
346 if Present (Parameter_Associations (N)) then
347 Actual := First_Actual (N);
349 Subp_Entity := Entity (Name (N));
350 Formal := First_Formal (Subp_Entity);
352 while Present (Actual) loop
353 Control := Find_Controlling_Arg (Actual);
354 exit when Present (Control);
356 -- Check for the case where the actual is a tag-indeterminate call
357 -- whose result type is different than the tagged type associated
358 -- with the containing call, but is an ancestor of the type.
360 if Is_Controlling_Formal (Formal)
361 and then Is_Tag_Indeterminate (Actual)
362 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
363 and then Is_Ancestor (Etype (Actual), Etype (Formal))
365 Indeterm_Ancestor_Call := True;
366 Indeterm_Ctrl_Type := Etype (Formal);
369 Next_Actual (Actual);
370 Next_Formal (Formal);
373 -- If the call doesn't have a controlling actual but does have
374 -- an indeterminate actual that requires dispatching treatment,
375 -- then an object is needed that will serve as the controlling
376 -- argument for a dispatching call on the indeterminate actual.
377 -- This can only occur in the unusual situation of a default
378 -- actual given by a tag-indeterminate call and where the type
379 -- of the call is an ancestor of the type associated with a
380 -- containing call to an inherited operation (see AI-239).
381 -- Rather than create an object of the tagged type, which would
382 -- be problematic for various reasons (default initialization,
383 -- discriminants), the tag of the containing call's associated
384 -- tagged type is directly used to control the dispatching.
386 if not Present (Control)
387 and then Indeterm_Ancestor_Call
390 Make_Attribute_Reference (Loc,
391 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
392 Attribute_Name => Name_Tag);
396 if Present (Control) then
398 -- Verify that no controlling arguments are statically tagged
401 Write_Str ("Found Dispatching call");
406 Actual := First_Actual (N);
408 while Present (Actual) loop
409 if Actual /= Control then
411 if not Is_Controlling_Actual (Actual) then
412 null; -- Can be anything
414 elsif Is_Dynamically_Tagged (Actual) then
415 null; -- Valid parameter
417 elsif Is_Tag_Indeterminate (Actual) then
419 -- The tag is inherited from the enclosing call (the
420 -- node we are currently analyzing). Explicitly expand
421 -- the actual, since the previous call to Expand
422 -- (from Resolve_Call) had no way of knowing about
423 -- the required dispatching.
425 Propagate_Tag (Control, Actual);
429 ("controlling argument is not dynamically tagged",
435 Next_Actual (Actual);
438 -- Mark call as a dispatching call
440 Set_Controlling_Argument (N, Control);
443 -- The call is not dispatching, so check that there aren't any
444 -- tag-indeterminate abstract calls left.
446 Actual := First_Actual (N);
448 while Present (Actual) loop
449 if Is_Tag_Indeterminate (Actual) then
451 -- Function call case
453 if Nkind (Original_Node (Actual)) = N_Function_Call then
454 Func := Entity (Name (Original_Node (Actual)));
456 -- Only other possibility is a qualified expression whose
457 -- consituent expression is itself a call.
463 (Expression (Original_Node (Actual)))));
466 if Is_Abstract (Func) then
468 "call to abstract function must be dispatching", N);
472 Next_Actual (Actual);
475 Check_Dispatching_Context;
479 -- If dispatching on result, the enclosing call, if any, will
480 -- determine the controlling argument. Otherwise this is the
481 -- primitive operation of the root type.
483 Check_Dispatching_Context;
485 end Check_Dispatching_Call;
487 ---------------------------------
488 -- Check_Dispatching_Operation --
489 ---------------------------------
491 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
492 Tagged_Type : Entity_Id;
493 Has_Dispatching_Parent : Boolean := False;
494 Body_Is_Last_Primitive : Boolean := False;
496 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
497 -- Check whether T is derived from a visibly controlled type.
498 -- This is true if the root type is declared in Ada.Finalization.
499 -- If T is derived instead from a private type whose full view
500 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
501 -- does not override the inherited one.
503 ---------------------------
504 -- Is_Visibly_Controlled --
505 ---------------------------
507 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
508 Root : constant Entity_Id := Root_Type (T);
510 return Chars (Scope (Root)) = Name_Finalization
511 and then Chars (Scope (Scope (Root))) = Name_Ada
512 and then Scope (Scope (Scope (Root))) = Standard_Standard;
513 end Is_Visibly_Controlled;
515 -- Start of processing for Check_Dispatching_Operation
518 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
522 Set_Is_Dispatching_Operation (Subp, False);
523 Tagged_Type := Find_Dispatching_Type (Subp);
525 -- If Subp is derived from a dispatching operation then it should
526 -- always be treated as dispatching. In this case various checks
527 -- below will be bypassed. Makes sure that late declarations for
528 -- inherited private subprograms are treated as dispatching, even
529 -- if the associated tagged type is already frozen.
531 Has_Dispatching_Parent :=
532 Present (Alias (Subp))
533 and then Is_Dispatching_Operation (Alias (Subp));
535 if No (Tagged_Type) then
538 -- The subprograms build internally after the freezing point (such as
539 -- the Init procedure) are not primitives
541 elsif Is_Frozen (Tagged_Type)
542 and then not Comes_From_Source (Subp)
543 and then not Has_Dispatching_Parent
547 -- The operation may be a child unit, whose scope is the defining
548 -- package, but which is not a primitive operation of the type.
550 elsif Is_Child_Unit (Subp) then
553 -- If the subprogram is not defined in a package spec, the only case
554 -- where it can be a dispatching op is when it overrides an operation
555 -- before the freezing point of the type.
557 elsif ((not Is_Package (Scope (Subp)))
558 or else In_Package_Body (Scope (Subp)))
559 and then not Has_Dispatching_Parent
561 if not Comes_From_Source (Subp)
562 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
566 -- If the type is already frozen, the overriding is not allowed
567 -- except when Old_Subp is not a dispatching operation (which
568 -- can occur when Old_Subp was inherited by an untagged type).
569 -- However, a body with no previous spec freezes the type "after"
570 -- its declaration, and therefore is a legal overriding (unless
571 -- the type has already been frozen). Only the first such body
574 elsif Present (Old_Subp)
575 and then Is_Dispatching_Operation (Old_Subp)
577 if Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
578 and then Comes_From_Source (Subp)
581 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
582 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
585 -- ??? The checks here for whether the type has been
586 -- frozen prior to the new body are not complete. It's
587 -- not simple to check frozenness at this point since
588 -- the body has already caused the type to be prematurely
589 -- frozen in Analyze_Declarations, but we're forced to
590 -- recheck this here because of the odd rule interpretation
591 -- that allows the overriding if the type wasn't frozen
592 -- prior to the body. The freezing action should probably
593 -- be delayed until after the spec is seen, but that's
594 -- a tricky change to the delicate freezing code.
596 -- Look at each declaration following the type up
597 -- until the new subprogram body. If any of the
598 -- declarations is a body then the type has been
599 -- frozen already so the overriding primitive is
602 while Present (Decl_Item)
603 and then (Decl_Item /= Subp_Body)
605 if Comes_From_Source (Decl_Item)
606 and then (Nkind (Decl_Item) in N_Proper_Body
607 or else Nkind (Decl_Item) in N_Body_Stub)
609 Error_Msg_N ("overriding of& is too late!", Subp);
611 ("\spec should appear immediately after the type!",
619 -- If the subprogram doesn't follow in the list of
620 -- declarations including the type then the type
621 -- has definitely been frozen already and the body
624 if not Present (Decl_Item) then
625 Error_Msg_N ("overriding of& is too late!", Subp);
627 ("\spec should appear immediately after the type!",
630 elsif Is_Frozen (Subp) then
632 -- The subprogram body declares a primitive operation.
633 -- if the subprogram is already frozen, we must update
634 -- its dispatching information explicitly here. The
635 -- information is taken from the overridden subprogram.
637 Body_Is_Last_Primitive := True;
639 if Present (DTC_Entity (Old_Subp)) then
640 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
641 Set_DT_Position (Subp, DT_Position (Old_Subp));
643 Subp_Body, Fill_DT_Entry (Sloc (Subp_Body), Subp));
649 Error_Msg_N ("overriding of& is too late!", Subp);
651 ("\subprogram spec should appear immediately after the type!",
655 -- If the type is not frozen yet and we are not in the overridding
656 -- case it looks suspiciously like an attempt to define a primitive
659 elsif not Is_Frozen (Tagged_Type) then
661 ("?not dispatching (must be defined in a package spec)", Subp);
664 -- When the type is frozen, it is legitimate to define a new
665 -- non-primitive operation.
671 -- Now, we are sure that the scope is a package spec. If the subprogram
672 -- is declared after the freezing point ot the type that's an error
674 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
675 Error_Msg_N ("this primitive operation is declared too late", Subp);
677 ("?no primitive operations for& after this line",
678 Freeze_Node (Tagged_Type),
683 Check_Controlling_Formals (Tagged_Type, Subp);
685 -- Now it should be a correct primitive operation, put it in the list
687 if Present (Old_Subp) then
688 Check_Subtype_Conformant (Subp, Old_Subp);
689 if (Chars (Subp) = Name_Initialize
690 or else Chars (Subp) = Name_Adjust
691 or else Chars (Subp) = Name_Finalize)
692 and then Is_Controlled (Tagged_Type)
693 and then not Is_Visibly_Controlled (Tagged_Type)
695 Set_Is_Overriding_Operation (Subp, False);
697 ("operation does not override inherited&?", Subp, Subp);
699 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
700 Set_Is_Overriding_Operation (Subp);
703 Add_Dispatching_Operation (Tagged_Type, Subp);
706 Set_Is_Dispatching_Operation (Subp, True);
708 if not Body_Is_Last_Primitive then
709 Set_DT_Position (Subp, No_Uint);
711 elsif Has_Controlled_Component (Tagged_Type)
713 (Chars (Subp) = Name_Initialize
714 or else Chars (Subp) = Name_Adjust
715 or else Chars (Subp) = Name_Finalize)
718 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
722 Old_Spec : Entity_Id;
724 C_Names : constant array (1 .. 3) of Name_Id :=
729 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
730 (TSS_Deep_Initialize,
735 -- Remove previous controlled function, which was constructed
736 -- and analyzed when the type was frozen. This requires
737 -- removing the body of the redefined primitive, as well as
738 -- its specification if needed (there is no spec created for
739 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
740 -- the exception information that may have been generated for
741 -- it when front end zero-cost tables are enabled.
743 for J in D_Names'Range loop
744 Old_P := TSS (Tagged_Type, D_Names (J));
747 and then Chars (Subp) = C_Names (J)
749 Old_Bod := Unit_Declaration_Node (Old_P);
751 Set_Is_Eliminated (Old_P);
752 Set_Scope (Old_P, Scope (Current_Scope));
754 if Nkind (Old_Bod) = N_Subprogram_Body
755 and then Present (Corresponding_Spec (Old_Bod))
757 Old_Spec := Corresponding_Spec (Old_Bod);
758 Set_Has_Completion (Old_Spec, False);
760 if Exception_Mechanism = Front_End_ZCX_Exceptions then
761 Set_Has_Subprogram_Descriptor (Old_Spec, False);
762 Set_Handler_Records (Old_Spec, No_List);
763 Set_Is_Eliminated (Old_Spec);
770 Build_Late_Proc (Tagged_Type, Chars (Subp));
772 -- The new operation is added to the actions of the freeze
773 -- node for the type, but this node has already been analyzed,
774 -- so we must retrieve and analyze explicitly the one new body,
777 and then Present (Actions (F_Node))
779 Decl := Last (Actions (F_Node));
784 end Check_Dispatching_Operation;
786 ------------------------------------------
787 -- Check_Operation_From_Incomplete_Type --
788 ------------------------------------------
790 procedure Check_Operation_From_Incomplete_Type
794 Full : constant Entity_Id := Full_View (Typ);
795 Parent_Typ : constant Entity_Id := Etype (Full);
796 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
797 New_Prim : constant Elist_Id := Primitive_Operations (Full);
799 Prev : Elmt_Id := No_Elmt;
801 function Derives_From (Proc : Entity_Id) return Boolean;
802 -- Check that Subp has the signature of an operation derived from Proc.
803 -- Subp has an access parameter that designates Typ.
809 function Derives_From (Proc : Entity_Id) return Boolean is
813 if Chars (Proc) /= Chars (Subp) then
817 F1 := First_Formal (Proc);
818 F2 := First_Formal (Subp);
820 while Present (F1) and then Present (F2) loop
822 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
824 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
827 elsif Designated_Type (Etype (F1)) = Parent_Typ
828 and then Designated_Type (Etype (F2)) /= Full
833 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
836 elsif Etype (F1) /= Etype (F2) then
844 return No (F1) and then No (F2);
847 -- Start of processing for Check_Operation_From_Incomplete_Type
850 -- The operation may override an inherited one, or may be a new one
851 -- altogether. The inherited operation will have been hidden by the
852 -- current one at the point of the type derivation, so it does not
853 -- appear in the list of primitive operations of the type. We have to
854 -- find the proper place of insertion in the list of primitive opera-
855 -- tions by iterating over the list for the parent type.
857 Op1 := First_Elmt (Old_Prim);
858 Op2 := First_Elmt (New_Prim);
860 while Present (Op1) and then Present (Op2) loop
862 if Derives_From (Node (Op1)) then
865 Prepend_Elmt (Subp, New_Prim);
867 Insert_Elmt_After (Subp, Prev);
878 -- Operation is a new primitive
880 Append_Elmt (Subp, New_Prim);
881 end Check_Operation_From_Incomplete_Type;
883 ---------------------------------------
884 -- Check_Operation_From_Private_View --
885 ---------------------------------------
887 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
888 Tagged_Type : Entity_Id;
891 if Is_Dispatching_Operation (Alias (Subp)) then
892 Set_Scope (Subp, Current_Scope);
893 Tagged_Type := Find_Dispatching_Type (Subp);
895 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
896 Append_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
898 -- If Old_Subp isn't already marked as dispatching then
899 -- this is the case of an operation of an untagged private
900 -- type fulfilled by a tagged type that overrides an
901 -- inherited dispatching operation, so we set the necessary
902 -- dispatching attributes here.
904 if not Is_Dispatching_Operation (Old_Subp) then
906 -- If the untagged type has no discriminants, and the full
907 -- view is constrained, there will be a spurious mismatch
908 -- of subtypes on the controlling arguments, because the tagged
909 -- type is the internal base type introduced in the derivation.
910 -- Use the original type to verify conformance, rather than the
913 if not Comes_From_Source (Tagged_Type)
914 and then Has_Discriminants (Tagged_Type)
919 Formal := First_Formal (Old_Subp);
920 while Present (Formal) loop
921 if Tagged_Type = Base_Type (Etype (Formal)) then
922 Tagged_Type := Etype (Formal);
925 Next_Formal (Formal);
929 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
930 Tagged_Type := Etype (Old_Subp);
934 Check_Controlling_Formals (Tagged_Type, Old_Subp);
935 Set_Is_Dispatching_Operation (Old_Subp, True);
936 Set_DT_Position (Old_Subp, No_Uint);
939 -- If the old subprogram is an explicit renaming of some other
940 -- entity, it is not overridden by the inherited subprogram.
941 -- Otherwise, update its alias and other attributes.
943 if Present (Alias (Old_Subp))
944 and then Nkind (Unit_Declaration_Node (Old_Subp))
945 /= N_Subprogram_Renaming_Declaration
947 Set_Alias (Old_Subp, Alias (Subp));
949 -- The derived subprogram should inherit the abstractness
951 -- of the parent subprogram (except in the case of a function
952 -- returning the type). This sets the abstractness properly
953 -- for cases where a private extension may have inherited
954 -- an abstract operation, but the full type is derived from
955 -- a descendant type and inherits a nonabstract version.
957 if Etype (Subp) /= Tagged_Type then
958 Set_Is_Abstract (Old_Subp, Is_Abstract (Alias (Subp)));
963 end Check_Operation_From_Private_View;
965 --------------------------
966 -- Find_Controlling_Arg --
967 --------------------------
969 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
970 Orig_Node : constant Node_Id := Original_Node (N);
974 if Nkind (Orig_Node) = N_Qualified_Expression then
975 return Find_Controlling_Arg (Expression (Orig_Node));
978 -- Dispatching on result case
980 if Nkind (Orig_Node) = N_Function_Call
981 and then Present (Controlling_Argument (Orig_Node))
982 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
984 return Controlling_Argument (Orig_Node);
988 elsif Is_Controlling_Actual (N)
990 (Nkind (Parent (N)) = N_Qualified_Expression
991 and then Is_Controlling_Actual (Parent (N)))
995 if Is_Access_Type (Typ) then
996 -- In the case of an Access attribute, use the type of
997 -- the prefix, since in the case of an actual for an
998 -- access parameter, the attribute's type may be of a
999 -- specific designated type, even though the prefix
1000 -- type is class-wide.
1002 if Nkind (N) = N_Attribute_Reference then
1003 Typ := Etype (Prefix (N));
1005 -- An allocator is dispatching if the type of qualified
1006 -- expression is class_wide, in which case this is the
1007 -- controlling type.
1009 elsif Nkind (Orig_Node) = N_Allocator
1010 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1012 Typ := Etype (Expression (Orig_Node));
1015 Typ := Designated_Type (Typ);
1019 if Is_Class_Wide_Type (Typ)
1021 (Nkind (Parent (N)) = N_Qualified_Expression
1022 and then Is_Access_Type (Etype (N))
1023 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1030 end Find_Controlling_Arg;
1032 ---------------------------
1033 -- Find_Dispatching_Type --
1034 ---------------------------
1036 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1038 Ctrl_Type : Entity_Id;
1041 if Present (DTC_Entity (Subp)) then
1042 return Scope (DTC_Entity (Subp));
1045 Formal := First_Formal (Subp);
1046 while Present (Formal) loop
1047 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1049 if Present (Ctrl_Type) then
1053 Next_Formal (Formal);
1056 -- The subprogram may also be dispatching on result
1058 if Present (Etype (Subp)) then
1059 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1061 if Present (Ctrl_Type) then
1068 end Find_Dispatching_Type;
1070 ---------------------------
1071 -- Is_Dynamically_Tagged --
1072 ---------------------------
1074 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1076 return Find_Controlling_Arg (N) /= Empty;
1077 end Is_Dynamically_Tagged;
1079 --------------------------
1080 -- Is_Tag_Indeterminate --
1081 --------------------------
1083 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1086 Orig_Node : constant Node_Id := Original_Node (N);
1089 if Nkind (Orig_Node) = N_Function_Call
1090 and then Is_Entity_Name (Name (Orig_Node))
1092 Nam := Entity (Name (Orig_Node));
1094 if not Has_Controlling_Result (Nam) then
1097 -- An explicit dereference means that the call has already been
1098 -- expanded and there is no tag to propagate.
1100 elsif Nkind (N) = N_Explicit_Dereference then
1103 -- If there are no actuals, the call is tag-indeterminate
1105 elsif No (Parameter_Associations (Orig_Node)) then
1109 Actual := First_Actual (Orig_Node);
1111 while Present (Actual) loop
1112 if Is_Controlling_Actual (Actual)
1113 and then not Is_Tag_Indeterminate (Actual)
1115 return False; -- one operand is dispatching
1118 Next_Actual (Actual);
1125 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1126 return Is_Tag_Indeterminate (Expression (Orig_Node));
1131 end Is_Tag_Indeterminate;
1133 ------------------------------------
1134 -- Override_Dispatching_Operation --
1135 ------------------------------------
1137 procedure Override_Dispatching_Operation
1138 (Tagged_Type : Entity_Id;
1139 Prev_Op : Entity_Id;
1142 Op_Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Tagged_Type));
1145 -- Patch the primitive operation list
1147 while Present (Op_Elmt)
1148 and then Node (Op_Elmt) /= Prev_Op
1150 Next_Elmt (Op_Elmt);
1153 -- If there is no previous operation to override, the type declaration
1154 -- was malformed, and an error must have been emitted already.
1156 if No (Op_Elmt) then
1160 Replace_Elmt (Op_Elmt, New_Op);
1162 if (not Is_Package (Current_Scope))
1163 or else not In_Private_Part (Current_Scope)
1165 -- Not a private primitive
1169 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1171 -- Make the overriding operation into an alias of the implicit one.
1172 -- In this fashion a call from outside ends up calling the new
1173 -- body even if non-dispatching, and a call from inside calls the
1174 -- overriding operation because it hides the implicit one.
1175 -- To indicate that the body of Prev_Op is never called, set its
1176 -- dispatch table entity to Empty.
1178 Set_Alias (Prev_Op, New_Op);
1179 Set_DTC_Entity (Prev_Op, Empty);
1182 end Override_Dispatching_Operation;
1188 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1189 Call_Node : Node_Id;
1193 if Nkind (Actual) = N_Function_Call then
1194 Call_Node := Actual;
1196 elsif Nkind (Actual) = N_Identifier
1197 and then Nkind (Original_Node (Actual)) = N_Function_Call
1199 -- Call rewritten as object declaration when stack-checking
1200 -- is enabled. Propagate tag to expression in declaration, which
1201 -- is original call.
1203 Call_Node := Expression (Parent (Entity (Actual)));
1205 -- Only other possibility is parenthesized or qualified expression
1208 Call_Node := Expression (Actual);
1211 -- Do not set the Controlling_Argument if already set. This happens
1212 -- in the special case of _Input (see Exp_Attr, case Input).
1214 if No (Controlling_Argument (Call_Node)) then
1215 Set_Controlling_Argument (Call_Node, Control);
1218 Arg := First_Actual (Call_Node);
1220 while Present (Arg) loop
1221 if Is_Tag_Indeterminate (Arg) then
1222 Propagate_Tag (Control, Arg);
1228 -- Expansion of dispatching calls is suppressed when Java_VM, because
1229 -- the JVM back end directly handles the generation of dispatching
1230 -- calls and would have to undo any expansion to an indirect call.
1233 Expand_Dispatching_Call (Call_Node);