1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002 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 (1) 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
:= Present
(Alias
(Subp
))
441 and then Is_Dispatching_Operation
(Alias
(Subp
));
443 if No
(Tagged_Type
) then
446 -- The subprograms build internally after the freezing point (such as
447 -- the Init procedure) are not primitives
449 elsif Is_Frozen
(Tagged_Type
)
450 and then not Comes_From_Source
(Subp
)
451 and then not Has_Dispatching_Parent
455 -- The operation may be a child unit, whose scope is the defining
456 -- package, but which is not a primitive operation of the type.
458 elsif Is_Child_Unit
(Subp
) then
461 -- If the subprogram is not defined in a package spec, the only case
462 -- where it can be a dispatching op is when it overrides an operation
463 -- before the freezing point of the type.
465 elsif ((not Is_Package
(Scope
(Subp
)))
466 or else In_Package_Body
(Scope
(Subp
)))
467 and then not Has_Dispatching_Parent
469 if not Comes_From_Source
(Subp
)
470 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
474 -- If the type is already frozen, the overriding is not allowed
475 -- except when Old_Subp is not a dispatching operation (which
476 -- can occur when Old_Subp was inherited by an untagged type).
477 -- However, a body with no previous spec freezes the type "after"
478 -- its declaration, and therefore is a legal overriding (unless
479 -- the type has already been frozen). Only the first such body
482 elsif Present
(Old_Subp
)
483 and then Is_Dispatching_Operation
(Old_Subp
)
485 if Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
486 and then Comes_From_Source
(Subp
)
489 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
490 Decl_Item
: Node_Id
:= Next
(Parent
(Tagged_Type
));
493 -- ??? The checks here for whether the type has been
494 -- frozen prior to the new body are not complete. It's
495 -- not simple to check frozenness at this point since
496 -- the body has already caused the type to be prematurely
497 -- frozen in Analyze_Declarations, but we're forced to
498 -- recheck this here because of the odd rule interpretation
499 -- that allows the overriding if the type wasn't frozen
500 -- prior to the body. The freezing action should probably
501 -- be delayed until after the spec is seen, but that's
502 -- a tricky change to the delicate freezing code.
504 -- Look at each declaration following the type up
505 -- until the new subprogram body. If any of the
506 -- declarations is a body then the type has been
507 -- frozen already so the overriding primitive is
510 while Present
(Decl_Item
)
511 and then (Decl_Item
/= Subp_Body
)
513 if Comes_From_Source
(Decl_Item
)
514 and then (Nkind
(Decl_Item
) in N_Proper_Body
515 or else Nkind
(Decl_Item
) in N_Body_Stub
)
517 Error_Msg_N
("overriding of& is too late!", Subp
);
519 ("\spec should appear immediately after the type!",
527 -- If the subprogram doesn't follow in the list of
528 -- declarations including the type then the type
529 -- has definitely been frozen already and the body
532 if not Present
(Decl_Item
) then
533 Error_Msg_N
("overriding of& is too late!", Subp
);
535 ("\spec should appear immediately after the type!",
538 elsif Is_Frozen
(Subp
) then
540 -- the subprogram body declares a primitive operation.
541 -- if the subprogram is already frozen, we must update
542 -- its dispatching information explicitly here. The
543 -- information is taken from the overridden subprogram.
545 Body_Is_Last_Primitive
:= True;
547 if Present
(DTC_Entity
(Old_Subp
)) then
548 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
549 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
551 Subp_Body
, Fill_DT_Entry
(Sloc
(Subp_Body
), Subp
));
557 Error_Msg_N
("overriding of& is too late!", Subp
);
559 ("\subprogram spec should appear immediately after the type!",
563 -- If the type is not frozen yet and we are not in the overridding
564 -- case it looks suspiciously like an attempt to define a primitive
567 elsif not Is_Frozen
(Tagged_Type
) then
569 ("?not dispatching (must be defined in a package spec)", Subp
);
572 -- When the type is frozen, it is legitimate to define a new
573 -- non-primitive operation.
579 -- Now, we are sure that the scope is a package spec. If the subprogram
580 -- is declared after the freezing point ot the type that's an error
582 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
583 Error_Msg_N
("this primitive operation is declared too late", Subp
);
585 ("?no primitive operations for& after this line",
586 Freeze_Node
(Tagged_Type
),
591 Check_Controlling_Formals
(Tagged_Type
, Subp
);
593 -- Now it should be a correct primitive operation, put it in the list
595 if Present
(Old_Subp
) then
596 Check_Subtype_Conformant
(Subp
, Old_Subp
);
597 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
600 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
603 Set_Is_Dispatching_Operation
(Subp
, True);
605 if not Body_Is_Last_Primitive
then
606 Set_DT_Position
(Subp
, No_Uint
);
608 elsif Has_Controlled_Component
(Tagged_Type
)
610 (Chars
(Subp
) = Name_Initialize
611 or else Chars
(Subp
) = Name_Adjust
612 or else Chars
(Subp
) = Name_Finalize
)
615 F_Node
: Node_Id
:= Freeze_Node
(Tagged_Type
);
619 Old_Spec
: Entity_Id
;
621 C_Names
: constant array (1 .. 3) of Name_Id
:=
626 D_Names
: constant array (1 .. 3) of Name_Id
:=
627 (Name_uDeep_Initialize
,
629 Name_uDeep_Finalize
);
632 -- Remove previous controlled function, which was constructed
633 -- and analyzed when the type was frozen. This requires
634 -- removing the body of the redefined primitive, as well as its
635 -- specification if needed (there is no spec created for
636 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
637 -- the exception information that may have been generated for it
638 -- when zero-cost is enabled.
640 for J
in D_Names
'Range loop
641 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
644 and then Chars
(Subp
) = C_Names
(J
)
646 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
648 Set_Is_Eliminated
(Old_P
);
649 Set_Scope
(Old_P
, Scope
(Current_Scope
));
651 if Nkind
(Old_Bod
) = N_Subprogram_Body
652 and then Present
(Corresponding_Spec
(Old_Bod
))
654 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
655 Set_Has_Completion
(Old_Spec
, False);
657 if Exception_Mechanism
= Front_End_ZCX
then
658 Set_Has_Subprogram_Descriptor
(Old_Spec
, False);
659 Set_Handler_Records
(Old_Spec
, No_List
);
660 Set_Is_Eliminated
(Old_Spec
);
667 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
669 -- The new operation is added to the actions of the freeze
670 -- node for the type, but this node has already been analyzed,
671 -- so we must retrieve and analyze explicitly the one new body,
674 and then Present
(Actions
(F_Node
))
676 Decl
:= Last
(Actions
(F_Node
));
681 end Check_Dispatching_Operation
;
683 ------------------------------------------
684 -- Check_Operation_From_Incomplete_Type --
685 ------------------------------------------
687 procedure Check_Operation_From_Incomplete_Type
691 Full
: constant Entity_Id
:= Full_View
(Typ
);
692 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
693 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
694 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
696 Prev
: Elmt_Id
:= No_Elmt
;
698 function Derives_From
(Proc
: Entity_Id
) return Boolean;
699 -- Check that Subp has the signature of an operation derived from Proc.
700 -- Subp has an access parameter that designates Typ.
706 function Derives_From
(Proc
: Entity_Id
) return Boolean is
710 if Chars
(Proc
) /= Chars
(Subp
) then
714 F1
:= First_Formal
(Proc
);
715 F2
:= First_Formal
(Subp
);
717 while Present
(F1
) and then Present
(F2
) loop
719 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
721 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
724 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
725 and then Designated_Type
(Etype
(F2
)) /= Full
730 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
733 elsif Etype
(F1
) /= Etype
(F2
) then
741 return No
(F1
) and then No
(F2
);
744 -- Start of processing for Check_Operation_From_Incomplete_Type
747 -- The operation may override an inherited one, or may be a new one
748 -- altogether. The inherited operation will have been hidden by the
749 -- current one at the point of the type derivation, so it does not
750 -- appear in the list of primitive operations of the type. We have to
751 -- find the proper place of insertion in the list of primitive opera-
752 -- tions by iterating over the list for the parent type.
754 Op1
:= First_Elmt
(Old_Prim
);
755 Op2
:= First_Elmt
(New_Prim
);
757 while Present
(Op1
) and then Present
(Op2
) loop
759 if Derives_From
(Node
(Op1
)) then
762 Prepend_Elmt
(Subp
, New_Prim
);
764 Insert_Elmt_After
(Subp
, Prev
);
775 -- Operation is a new primitive.
777 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
803 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
804 Set_Is_Dispatching_Operation
(Old_Subp
, True);
805 Set_DT_Position
(Old_Subp
, No_Uint
);
808 -- If the old subprogram is an explicit renaming of some other
809 -- entity, it is not overridden by the inherited subprogram.
810 -- Otherwise, update its alias and other attributes.
812 if Present
(Alias
(Old_Subp
))
813 and then Nkind
(Unit_Declaration_Node
(Old_Subp
))
814 /= N_Subprogram_Renaming_Declaration
816 Set_Alias
(Old_Subp
, Alias
(Subp
));
818 -- The derived subprogram should inherit the abstractness
819 -- of the parent subprogram (except in the case of a function
820 -- returning the type). This sets the abstractness properly
821 -- for cases where a private extension may have inherited
822 -- an abstract operation, but the full type is derived from
823 -- a descendant type and inherits a nonabstract version.
825 if Etype
(Subp
) /= Tagged_Type
then
826 Set_Is_Abstract
(Old_Subp
, Is_Abstract
(Alias
(Subp
)));
831 end Check_Operation_From_Private_View
;
833 --------------------------
834 -- Find_Controlling_Arg --
835 --------------------------
837 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
838 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
842 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
843 return Find_Controlling_Arg
(Expression
(Orig_Node
));
846 -- Dispatching on result case
848 if Nkind
(Orig_Node
) = N_Function_Call
849 and then Present
(Controlling_Argument
(Orig_Node
))
850 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
852 return Controlling_Argument
(Orig_Node
);
856 elsif Is_Controlling_Actual
(N
) then
859 if Is_Access_Type
(Typ
) then
860 -- In the case of an Access attribute, use the type of
861 -- the prefix, since in the case of an actual for an
862 -- access parameter, the attribute's type may be of a
863 -- specific designated type, even though the prefix
864 -- type is class-wide.
866 if Nkind
(N
) = N_Attribute_Reference
then
867 Typ
:= Etype
(Prefix
(N
));
869 -- An allocator is dispatching if the type of qualified
870 -- expression is class_wide, in which case this is the
873 elsif Nkind
(Orig_Node
) = N_Allocator
874 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
876 Typ
:= Etype
(Expression
(Orig_Node
));
879 Typ
:= Designated_Type
(Typ
);
883 if Is_Class_Wide_Type
(Typ
) then
889 end Find_Controlling_Arg
;
891 ---------------------------
892 -- Find_Dispatching_Type --
893 ---------------------------
895 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
897 Ctrl_Type
: Entity_Id
;
900 if Present
(DTC_Entity
(Subp
)) then
901 return Scope
(DTC_Entity
(Subp
));
904 Formal
:= First_Formal
(Subp
);
905 while Present
(Formal
) loop
906 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
908 if Present
(Ctrl_Type
) then
912 Next_Formal
(Formal
);
915 -- The subprogram may also be dispatching on result
917 if Present
(Etype
(Subp
)) then
918 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
920 if Present
(Ctrl_Type
) then
927 end Find_Dispatching_Type
;
929 ---------------------------
930 -- Is_Dynamically_Tagged --
931 ---------------------------
933 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
935 return Find_Controlling_Arg
(N
) /= Empty
;
936 end Is_Dynamically_Tagged
;
938 --------------------------
939 -- Is_Tag_Indeterminate --
940 --------------------------
942 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
945 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
948 if Nkind
(Orig_Node
) = N_Function_Call
949 and then Is_Entity_Name
(Name
(Orig_Node
))
951 Nam
:= Entity
(Name
(Orig_Node
));
953 if not Has_Controlling_Result
(Nam
) then
956 -- If there are no actuals, the call is tag-indeterminate
958 elsif No
(Parameter_Associations
(Orig_Node
)) then
962 Actual
:= First_Actual
(Orig_Node
);
964 while Present
(Actual
) loop
965 if Is_Controlling_Actual
(Actual
)
966 and then not Is_Tag_Indeterminate
(Actual
)
968 return False; -- one operand is dispatching
971 Next_Actual
(Actual
);
978 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
979 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
984 end Is_Tag_Indeterminate
;
986 ------------------------------------
987 -- Override_Dispatching_Operation --
988 ------------------------------------
990 procedure Override_Dispatching_Operation
991 (Tagged_Type
: Entity_Id
;
995 Op_Elmt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
998 -- Patch the primitive operation list
1000 while Present
(Op_Elmt
)
1001 and then Node
(Op_Elmt
) /= Prev_Op
1003 Next_Elmt
(Op_Elmt
);
1006 -- If there is no previous operation to override, the type declaration
1007 -- was malformed, and an error must have been emitted already.
1009 if No
(Op_Elmt
) then
1013 Replace_Elmt
(Op_Elmt
, New_Op
);
1015 if (not Is_Package
(Current_Scope
))
1016 or else not In_Private_Part
(Current_Scope
)
1018 -- Not a private primitive
1022 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1024 -- Make the overriding operation into an alias of the implicit one.
1025 -- In this fashion a call from outside ends up calling the new
1026 -- body even if non-dispatching, and a call from inside calls the
1027 -- overriding operation because it hides the implicit one.
1028 -- To indicate that the body of Prev_Op is never called, set its
1029 -- dispatch table entity to Empty.
1031 Set_Alias
(Prev_Op
, New_Op
);
1032 Set_DTC_Entity
(Prev_Op
, Empty
);
1035 end Override_Dispatching_Operation
;
1041 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1042 Call_Node
: Node_Id
;
1046 if Nkind
(Actual
) = N_Function_Call
then
1047 Call_Node
:= Actual
;
1049 elsif Nkind
(Actual
) = N_Identifier
1050 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1052 -- Call rewritten as object declaration when stack-checking
1053 -- is enabled. Propagate tag to expression in declaration, which
1054 -- is original call.
1056 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1058 -- Only other possibility is parenthesized or qualified expression
1061 Call_Node
:= Expression
(Actual
);
1064 -- Do not set the Controlling_Argument if already set. This happens
1065 -- in the special case of _Input (see Exp_Attr, case Input).
1067 if No
(Controlling_Argument
(Call_Node
)) then
1068 Set_Controlling_Argument
(Call_Node
, Control
);
1071 Arg
:= First_Actual
(Call_Node
);
1073 while Present
(Arg
) loop
1074 if Is_Tag_Indeterminate
(Arg
) then
1075 Propagate_Tag
(Control
, Arg
);
1081 -- Expansion of dispatching calls is suppressed when Java_VM, because
1082 -- the JVM back end directly handles the generation of dispatching
1083 -- calls and would have to undo any expansion to an indirect call.
1086 Expand_Dispatch_Call
(Call_Node
);