1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 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;
38 with Output; use Output;
40 with Sem_Ch6; use Sem_Ch6;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Util; use Sem_Util;
43 with Snames; use Snames;
44 with Sinfo; use Sinfo;
45 with Uintp; use Uintp;
47 package body Sem_Disp is
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Override_Dispatching_Operation
54 (Tagged_Type : Entity_Id;
57 -- Replace an implicit dispatching operation with an explicit one.
58 -- Prev_Op is an inherited primitive operation which is overridden
59 -- by the explicit declaration of New_Op.
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
70 -- T is the type of a formal parameter of subp. Returns the tagged
71 -- if the parameter can be a controlling argument, empty otherwise
73 --------------------------------
74 -- Add_Dispatching_Operation --
75 --------------------------------
77 procedure Add_Dispatching_Operation
78 (Tagged_Type : Entity_Id;
81 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
84 Append_Elmt (New_Op, List);
85 end Add_Dispatching_Operation;
87 -------------------------------
88 -- Check_Controlling_Formals --
89 -------------------------------
91 procedure Check_Controlling_Formals
96 Ctrl_Type : Entity_Id;
97 Remote : constant Boolean :=
98 Is_Remote_Types (Current_Scope)
99 and then Comes_From_Source (Subp)
100 and then Scope (Typ) = Current_Scope;
103 Formal := First_Formal (Subp);
105 while Present (Formal) loop
106 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
108 if Present (Ctrl_Type) then
109 if Ctrl_Type = Typ then
110 Set_Is_Controlling_Formal (Formal);
112 -- Check that the parameter's nominal subtype statically
113 -- matches the first subtype.
115 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
116 if not Subtypes_Statically_Match
117 (Typ, Designated_Type (Etype (Formal)))
120 ("parameter subtype does not match controlling type",
124 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
126 ("parameter subtype does not match controlling type",
130 if Present (Default_Value (Formal)) then
131 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
133 ("default not allowed for controlling access parameter",
134 Default_Value (Formal));
136 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
138 ("default expression must be a tag indeterminate" &
139 " function call", Default_Value (Formal));
143 elsif Comes_From_Source (Subp) then
145 ("operation can be dispatching in only one type", Subp);
148 -- Verify that the restriction in E.2.2 (14) is obeyed
151 and then Ekind (Etype (Formal)) = E_Anonymous_Access_Type
154 ("Access parameter of a remote subprogram must be controlling",
158 Next_Formal (Formal);
161 if Present (Etype (Subp)) then
162 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
164 if Present (Ctrl_Type) then
165 if Ctrl_Type = Typ then
166 Set_Has_Controlling_Result (Subp);
168 -- Check that the result subtype statically matches
169 -- the first subtype.
171 if not Subtypes_Statically_Match (Typ, Etype (Subp)) then
173 ("result subtype does not match controlling type", Subp);
176 elsif Comes_From_Source (Subp) then
178 ("operation can be dispatching in only one type", Subp);
181 -- The following check is clearly required, although the RM says
182 -- nothing about return types. If the return type is a limited
183 -- class-wide type declared in the current scope, there is no way
184 -- to declare stream procedures for it, so the return cannot be
188 and then Is_Limited_Type (Typ)
189 and then Etype (Subp) = Class_Wide_Type (Typ)
191 Error_Msg_N ("return type has no stream attributes", Subp);
194 end Check_Controlling_Formals;
196 ----------------------------
197 -- Check_Controlling_Type --
198 ----------------------------
200 function Check_Controlling_Type
205 Tagged_Type : Entity_Id := Empty;
208 if Is_Tagged_Type (T) then
209 if Is_First_Subtype (T) then
212 Tagged_Type := Base_Type (T);
215 elsif Ekind (T) = E_Anonymous_Access_Type
216 and then Is_Tagged_Type (Designated_Type (T))
217 and then Ekind (Designated_Type (T)) /= E_Incomplete_Type
219 if Is_First_Subtype (Designated_Type (T)) then
220 Tagged_Type := Designated_Type (T);
222 Tagged_Type := Base_Type (Designated_Type (T));
227 or else Is_Class_Wide_Type (Tagged_Type)
231 -- The dispatching type and the primitive operation must be defined
232 -- in the same scope except for internal operations.
234 elsif (Scope (Subp) = Scope (Tagged_Type)
235 or else Is_Internal (Subp))
237 (not Is_Generic_Type (Tagged_Type)
238 or else not Comes_From_Source (Subp))
245 end Check_Controlling_Type;
247 ----------------------------
248 -- Check_Dispatching_Call --
249 ----------------------------
251 procedure Check_Dispatching_Call (N : Node_Id) is
253 Control : Node_Id := Empty;
256 procedure Check_Dispatching_Context;
257 -- If the call is tag-indeterminate and the entity being called is
258 -- abstract, verify that the context is a call that will eventually
259 -- provide a tag for dispatching, or has provided one already.
261 -------------------------------
262 -- Check_Dispatching_Context --
263 -------------------------------
265 procedure Check_Dispatching_Context is
266 Func : constant Entity_Id := Entity (Name (N));
270 if Is_Abstract (Func)
271 and then No (Controlling_Argument (N))
273 if Present (Alias (Func))
274 and then not Is_Abstract (Alias (Func))
275 and then No (DTC_Entity (Func))
277 -- Private overriding of inherited abstract operation,
280 Set_Entity (Name (N), Alias (Func));
286 while Present (Par) loop
288 if (Nkind (Par) = N_Function_Call or else
289 Nkind (Par) = N_Procedure_Call_Statement or else
290 Nkind (Par) = N_Assignment_Statement or else
291 Nkind (Par) = N_Op_Eq or else
292 Nkind (Par) = N_Op_Ne)
293 and then Is_Tagged_Type (Etype (Func))
297 elsif Nkind (Par) = N_Qualified_Expression
298 or else Nkind (Par) = N_Unchecked_Type_Conversion
304 ("call to abstract function must be dispatching", N);
310 end Check_Dispatching_Context;
312 -- Start of processing for Check_Dispatching_Call
315 -- Find a controlling argument, if any
317 if Present (Parameter_Associations (N)) then
318 Actual := First_Actual (N);
320 while Present (Actual) loop
321 Control := Find_Controlling_Arg (Actual);
322 exit when Present (Control);
323 Next_Actual (Actual);
326 if Present (Control) then
328 -- Verify that no controlling arguments are statically tagged
331 Write_Str ("Found Dispatching call");
336 Actual := First_Actual (N);
338 while Present (Actual) loop
339 if Actual /= Control then
341 if not Is_Controlling_Actual (Actual) then
342 null; -- can be anything
344 elsif Is_Dynamically_Tagged (Actual) then
345 null; -- valid parameter
347 elsif Is_Tag_Indeterminate (Actual) then
349 -- The tag is inherited from the enclosing call (the
350 -- node we are currently analyzing). Explicitly expand
351 -- the actual, since the previous call to Expand
352 -- (from Resolve_Call) had no way of knowing about
353 -- the required dispatching.
355 Propagate_Tag (Control, Actual);
359 ("controlling argument is not dynamically tagged",
365 Next_Actual (Actual);
368 -- Mark call as a dispatching call
370 Set_Controlling_Argument (N, Control);
373 -- The call is not dispatching, check that there isn't any
374 -- tag indeterminate abstract call left
376 Actual := First_Actual (N);
378 while Present (Actual) loop
379 if Is_Tag_Indeterminate (Actual) then
381 -- Function call case
383 if Nkind (Original_Node (Actual)) = N_Function_Call then
384 Func := Entity (Name (Original_Node (Actual)));
386 -- Only other possibility is a qualified expression whose
387 -- consituent expression is itself a call.
393 (Expression (Original_Node (Actual)))));
396 if Is_Abstract (Func) then
398 "call to abstract function must be dispatching", N);
402 Next_Actual (Actual);
405 Check_Dispatching_Context;
409 -- If dispatching on result, the enclosing call, if any, will
410 -- determine the controlling argument. Otherwise this is the
411 -- primitive operation of the root type.
413 Check_Dispatching_Context;
415 end Check_Dispatching_Call;
417 ---------------------------------
418 -- Check_Dispatching_Operation --
419 ---------------------------------
421 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
422 Tagged_Type : Entity_Id;
423 Has_Dispatching_Parent : Boolean := False;
424 Body_Is_Last_Primitive : Boolean := False;
427 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
431 Set_Is_Dispatching_Operation (Subp, False);
432 Tagged_Type := Find_Dispatching_Type (Subp);
434 -- If Subp is derived from a dispatching operation then it should
435 -- always be treated as dispatching. In this case various checks
436 -- below will be bypassed. Makes sure that late declarations for
437 -- inherited private subprograms are treated as dispatching, even
438 -- if the associated tagged type is already frozen.
440 Has_Dispatching_Parent :=
441 Present (Alias (Subp))
442 and then Is_Dispatching_Operation (Alias (Subp));
444 if No (Tagged_Type) then
447 -- The subprograms build internally after the freezing point (such as
448 -- the Init procedure) are not primitives
450 elsif Is_Frozen (Tagged_Type)
451 and then not Comes_From_Source (Subp)
452 and then not Has_Dispatching_Parent
456 -- The operation may be a child unit, whose scope is the defining
457 -- package, but which is not a primitive operation of the type.
459 elsif Is_Child_Unit (Subp) then
462 -- If the subprogram is not defined in a package spec, the only case
463 -- where it can be a dispatching op is when it overrides an operation
464 -- before the freezing point of the type.
466 elsif ((not Is_Package (Scope (Subp)))
467 or else In_Package_Body (Scope (Subp)))
468 and then not Has_Dispatching_Parent
470 if not Comes_From_Source (Subp)
471 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
475 -- If the type is already frozen, the overriding is not allowed
476 -- except when Old_Subp is not a dispatching operation (which
477 -- can occur when Old_Subp was inherited by an untagged type).
478 -- However, a body with no previous spec freezes the type "after"
479 -- its declaration, and therefore is a legal overriding (unless
480 -- the type has already been frozen). Only the first such body
483 elsif Present (Old_Subp)
484 and then Is_Dispatching_Operation (Old_Subp)
486 if Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
487 and then Comes_From_Source (Subp)
490 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
491 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
494 -- ??? The checks here for whether the type has been
495 -- frozen prior to the new body are not complete. It's
496 -- not simple to check frozenness at this point since
497 -- the body has already caused the type to be prematurely
498 -- frozen in Analyze_Declarations, but we're forced to
499 -- recheck this here because of the odd rule interpretation
500 -- that allows the overriding if the type wasn't frozen
501 -- prior to the body. The freezing action should probably
502 -- be delayed until after the spec is seen, but that's
503 -- a tricky change to the delicate freezing code.
505 -- Look at each declaration following the type up
506 -- until the new subprogram body. If any of the
507 -- declarations is a body then the type has been
508 -- frozen already so the overriding primitive is
511 while Present (Decl_Item)
512 and then (Decl_Item /= Subp_Body)
514 if Comes_From_Source (Decl_Item)
515 and then (Nkind (Decl_Item) in N_Proper_Body
516 or else Nkind (Decl_Item) in N_Body_Stub)
518 Error_Msg_N ("overriding of& is too late!", Subp);
520 ("\spec should appear immediately after the type!",
528 -- If the subprogram doesn't follow in the list of
529 -- declarations including the type then the type
530 -- has definitely been frozen already and the body
533 if not Present (Decl_Item) then
534 Error_Msg_N ("overriding of& is too late!", Subp);
536 ("\spec should appear immediately after the type!",
539 elsif Is_Frozen (Subp) then
541 -- The subprogram body declares a primitive operation.
542 -- if the subprogram is already frozen, we must update
543 -- its dispatching information explicitly here. The
544 -- information is taken from the overridden subprogram.
546 Body_Is_Last_Primitive := True;
548 if Present (DTC_Entity (Old_Subp)) then
549 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
550 Set_DT_Position (Subp, DT_Position (Old_Subp));
552 Subp_Body, Fill_DT_Entry (Sloc (Subp_Body), Subp));
558 Error_Msg_N ("overriding of& is too late!", Subp);
560 ("\subprogram spec should appear immediately after the type!",
564 -- If the type is not frozen yet and we are not in the overridding
565 -- case it looks suspiciously like an attempt to define a primitive
568 elsif not Is_Frozen (Tagged_Type) then
570 ("?not dispatching (must be defined in a package spec)", Subp);
573 -- When the type is frozen, it is legitimate to define a new
574 -- non-primitive operation.
580 -- Now, we are sure that the scope is a package spec. If the subprogram
581 -- is declared after the freezing point ot the type that's an error
583 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
584 Error_Msg_N ("this primitive operation is declared too late", Subp);
586 ("?no primitive operations for& after this line",
587 Freeze_Node (Tagged_Type),
592 Check_Controlling_Formals (Tagged_Type, Subp);
594 -- Now it should be a correct primitive operation, put it in the list
596 if Present (Old_Subp) then
597 Check_Subtype_Conformant (Subp, Old_Subp);
598 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
599 Set_Is_Overriding_Operation (Subp);
601 Add_Dispatching_Operation (Tagged_Type, Subp);
604 Set_Is_Dispatching_Operation (Subp, True);
606 if not Body_Is_Last_Primitive then
607 Set_DT_Position (Subp, No_Uint);
609 elsif Has_Controlled_Component (Tagged_Type)
611 (Chars (Subp) = Name_Initialize
612 or else Chars (Subp) = Name_Adjust
613 or else Chars (Subp) = Name_Finalize)
616 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
620 Old_Spec : Entity_Id;
622 C_Names : constant array (1 .. 3) of Name_Id :=
627 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
628 (TSS_Deep_Initialize,
633 -- Remove previous controlled function, which was constructed
634 -- and analyzed when the type was frozen. This requires
635 -- removing the body of the redefined primitive, as well as
636 -- its specification if needed (there is no spec created for
637 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
638 -- the exception information that may have been generated for
639 -- it when front end zero-cost tables are enabled.
641 for J in D_Names'Range loop
642 Old_P := TSS (Tagged_Type, D_Names (J));
645 and then Chars (Subp) = C_Names (J)
647 Old_Bod := Unit_Declaration_Node (Old_P);
649 Set_Is_Eliminated (Old_P);
650 Set_Scope (Old_P, Scope (Current_Scope));
652 if Nkind (Old_Bod) = N_Subprogram_Body
653 and then Present (Corresponding_Spec (Old_Bod))
655 Old_Spec := Corresponding_Spec (Old_Bod);
656 Set_Has_Completion (Old_Spec, False);
658 if Exception_Mechanism = Front_End_ZCX_Exceptions then
659 Set_Has_Subprogram_Descriptor (Old_Spec, False);
660 Set_Handler_Records (Old_Spec, No_List);
661 Set_Is_Eliminated (Old_Spec);
668 Build_Late_Proc (Tagged_Type, Chars (Subp));
670 -- The new operation is added to the actions of the freeze
671 -- node for the type, but this node has already been analyzed,
672 -- so we must retrieve and analyze explicitly the one new body,
675 and then Present (Actions (F_Node))
677 Decl := Last (Actions (F_Node));
682 end Check_Dispatching_Operation;
684 ------------------------------------------
685 -- Check_Operation_From_Incomplete_Type --
686 ------------------------------------------
688 procedure Check_Operation_From_Incomplete_Type
692 Full : constant Entity_Id := Full_View (Typ);
693 Parent_Typ : constant Entity_Id := Etype (Full);
694 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
695 New_Prim : constant Elist_Id := Primitive_Operations (Full);
697 Prev : Elmt_Id := No_Elmt;
699 function Derives_From (Proc : Entity_Id) return Boolean;
700 -- Check that Subp has the signature of an operation derived from Proc.
701 -- Subp has an access parameter that designates Typ.
707 function Derives_From (Proc : Entity_Id) return Boolean is
711 if Chars (Proc) /= Chars (Subp) then
715 F1 := First_Formal (Proc);
716 F2 := First_Formal (Subp);
718 while Present (F1) and then Present (F2) loop
720 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
722 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
725 elsif Designated_Type (Etype (F1)) = Parent_Typ
726 and then Designated_Type (Etype (F2)) /= Full
731 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
734 elsif Etype (F1) /= Etype (F2) then
742 return No (F1) and then No (F2);
745 -- Start of processing for Check_Operation_From_Incomplete_Type
748 -- The operation may override an inherited one, or may be a new one
749 -- altogether. The inherited operation will have been hidden by the
750 -- current one at the point of the type derivation, so it does not
751 -- appear in the list of primitive operations of the type. We have to
752 -- find the proper place of insertion in the list of primitive opera-
753 -- tions by iterating over the list for the parent type.
755 Op1 := First_Elmt (Old_Prim);
756 Op2 := First_Elmt (New_Prim);
758 while Present (Op1) and then Present (Op2) loop
760 if Derives_From (Node (Op1)) then
763 Prepend_Elmt (Subp, New_Prim);
765 Insert_Elmt_After (Subp, Prev);
776 -- Operation is a new primitive
778 Append_Elmt (Subp, New_Prim);
779 end Check_Operation_From_Incomplete_Type;
781 ---------------------------------------
782 -- Check_Operation_From_Private_View --
783 ---------------------------------------
785 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
786 Tagged_Type : Entity_Id;
789 if Is_Dispatching_Operation (Alias (Subp)) then
790 Set_Scope (Subp, Current_Scope);
791 Tagged_Type := Find_Dispatching_Type (Subp);
793 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
794 Append_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
796 -- If Old_Subp isn't already marked as dispatching then
797 -- this is the case of an operation of an untagged private
798 -- type fulfilled by a tagged type that overrides an
799 -- inherited dispatching operation, so we set the necessary
800 -- dispatching attributes here.
802 if not Is_Dispatching_Operation (Old_Subp) then
804 -- If the untagged type has no discriminants, and the full
805 -- view is constrained, there will be a spurious mismatch
806 -- of subtypes on the controlling arguments, because the tagged
807 -- type is the internal base type introduced in the derivation.
808 -- Use the original type to verify conformance, rather than the
811 if not Comes_From_Source (Tagged_Type)
812 and then Has_Discriminants (Tagged_Type)
817 Formal := First_Formal (Old_Subp);
818 while Present (Formal) loop
819 if Tagged_Type = Base_Type (Etype (Formal)) then
820 Tagged_Type := Etype (Formal);
823 Next_Formal (Formal);
827 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
828 Tagged_Type := Etype (Old_Subp);
832 Check_Controlling_Formals (Tagged_Type, Old_Subp);
833 Set_Is_Dispatching_Operation (Old_Subp, True);
834 Set_DT_Position (Old_Subp, No_Uint);
837 -- If the old subprogram is an explicit renaming of some other
838 -- entity, it is not overridden by the inherited subprogram.
839 -- Otherwise, update its alias and other attributes.
841 if Present (Alias (Old_Subp))
842 and then Nkind (Unit_Declaration_Node (Old_Subp))
843 /= N_Subprogram_Renaming_Declaration
845 Set_Alias (Old_Subp, Alias (Subp));
847 -- The derived subprogram should inherit the abstractness
849 -- of the parent subprogram (except in the case of a function
850 -- returning the type). This sets the abstractness properly
851 -- for cases where a private extension may have inherited
852 -- an abstract operation, but the full type is derived from
853 -- a descendant type and inherits a nonabstract version.
855 if Etype (Subp) /= Tagged_Type then
856 Set_Is_Abstract (Old_Subp, Is_Abstract (Alias (Subp)));
861 end Check_Operation_From_Private_View;
863 --------------------------
864 -- Find_Controlling_Arg --
865 --------------------------
867 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
868 Orig_Node : constant Node_Id := Original_Node (N);
872 if Nkind (Orig_Node) = N_Qualified_Expression then
873 return Find_Controlling_Arg (Expression (Orig_Node));
876 -- Dispatching on result case
878 if Nkind (Orig_Node) = N_Function_Call
879 and then Present (Controlling_Argument (Orig_Node))
880 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
882 return Controlling_Argument (Orig_Node);
886 elsif Is_Controlling_Actual (N)
888 (Nkind (Parent (N)) = N_Qualified_Expression
889 and then Is_Controlling_Actual (Parent (N)))
893 if Is_Access_Type (Typ) then
894 -- In the case of an Access attribute, use the type of
895 -- the prefix, since in the case of an actual for an
896 -- access parameter, the attribute's type may be of a
897 -- specific designated type, even though the prefix
898 -- type is class-wide.
900 if Nkind (N) = N_Attribute_Reference then
901 Typ := Etype (Prefix (N));
903 -- An allocator is dispatching if the type of qualified
904 -- expression is class_wide, in which case this is the
907 elsif Nkind (Orig_Node) = N_Allocator
908 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
910 Typ := Etype (Expression (Orig_Node));
913 Typ := Designated_Type (Typ);
917 if Is_Class_Wide_Type (Typ)
919 (Nkind (Parent (N)) = N_Qualified_Expression
920 and then Is_Access_Type (Etype (N))
921 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
928 end Find_Controlling_Arg;
930 ---------------------------
931 -- Find_Dispatching_Type --
932 ---------------------------
934 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
936 Ctrl_Type : Entity_Id;
939 if Present (DTC_Entity (Subp)) then
940 return Scope (DTC_Entity (Subp));
943 Formal := First_Formal (Subp);
944 while Present (Formal) loop
945 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
947 if Present (Ctrl_Type) then
951 Next_Formal (Formal);
954 -- The subprogram may also be dispatching on result
956 if Present (Etype (Subp)) then
957 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
959 if Present (Ctrl_Type) then
966 end Find_Dispatching_Type;
968 ---------------------------
969 -- Is_Dynamically_Tagged --
970 ---------------------------
972 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
974 return Find_Controlling_Arg (N) /= Empty;
975 end Is_Dynamically_Tagged;
977 --------------------------
978 -- Is_Tag_Indeterminate --
979 --------------------------
981 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
984 Orig_Node : constant Node_Id := Original_Node (N);
987 if Nkind (Orig_Node) = N_Function_Call
988 and then Is_Entity_Name (Name (Orig_Node))
990 Nam := Entity (Name (Orig_Node));
992 if not Has_Controlling_Result (Nam) then
995 -- An explicit dereference means that the call has already been
996 -- expanded and there is no tag to propagate.
998 elsif Nkind (N) = N_Explicit_Dereference then
1001 -- If there are no actuals, the call is tag-indeterminate
1003 elsif No (Parameter_Associations (Orig_Node)) then
1007 Actual := First_Actual (Orig_Node);
1009 while Present (Actual) loop
1010 if Is_Controlling_Actual (Actual)
1011 and then not Is_Tag_Indeterminate (Actual)
1013 return False; -- one operand is dispatching
1016 Next_Actual (Actual);
1023 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1024 return Is_Tag_Indeterminate (Expression (Orig_Node));
1029 end Is_Tag_Indeterminate;
1031 ------------------------------------
1032 -- Override_Dispatching_Operation --
1033 ------------------------------------
1035 procedure Override_Dispatching_Operation
1036 (Tagged_Type : Entity_Id;
1037 Prev_Op : Entity_Id;
1040 Op_Elmt : Elmt_Id := First_Elmt (Primitive_Operations (Tagged_Type));
1043 -- Patch the primitive operation list
1045 while Present (Op_Elmt)
1046 and then Node (Op_Elmt) /= Prev_Op
1048 Next_Elmt (Op_Elmt);
1051 -- If there is no previous operation to override, the type declaration
1052 -- was malformed, and an error must have been emitted already.
1054 if No (Op_Elmt) then
1058 Replace_Elmt (Op_Elmt, New_Op);
1060 if (not Is_Package (Current_Scope))
1061 or else not In_Private_Part (Current_Scope)
1063 -- Not a private primitive
1067 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1069 -- Make the overriding operation into an alias of the implicit one.
1070 -- In this fashion a call from outside ends up calling the new
1071 -- body even if non-dispatching, and a call from inside calls the
1072 -- overriding operation because it hides the implicit one.
1073 -- To indicate that the body of Prev_Op is never called, set its
1074 -- dispatch table entity to Empty.
1076 Set_Alias (Prev_Op, New_Op);
1077 Set_DTC_Entity (Prev_Op, Empty);
1080 end Override_Dispatching_Operation;
1086 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1087 Call_Node : Node_Id;
1091 if Nkind (Actual) = N_Function_Call then
1092 Call_Node := Actual;
1094 elsif Nkind (Actual) = N_Identifier
1095 and then Nkind (Original_Node (Actual)) = N_Function_Call
1097 -- Call rewritten as object declaration when stack-checking
1098 -- is enabled. Propagate tag to expression in declaration, which
1099 -- is original call.
1101 Call_Node := Expression (Parent (Entity (Actual)));
1103 -- Only other possibility is parenthesized or qualified expression
1106 Call_Node := Expression (Actual);
1109 -- Do not set the Controlling_Argument if already set. This happens
1110 -- in the special case of _Input (see Exp_Attr, case Input).
1112 if No (Controlling_Argument (Call_Node)) then
1113 Set_Controlling_Argument (Call_Node, Control);
1116 Arg := First_Actual (Call_Node);
1118 while Present (Arg) loop
1119 if Is_Tag_Indeterminate (Arg) then
1120 Propagate_Tag (Control, Arg);
1126 -- Expansion of dispatching calls is suppressed when Java_VM, because
1127 -- the JVM back end directly handles the generation of dispatching
1128 -- calls and would have to undo any expansion to an indirect call.
1131 Expand_Dispatch_Call (Call_Node);