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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 Exp_Util
; use Exp_Util
;
35 with Errout
; use Errout
;
36 with Hostparm
; use Hostparm
;
37 with Nlists
; use Nlists
;
38 with Nmake
; use Nmake
;
40 with Output
; use Output
;
42 with Sem_Ch6
; use Sem_Ch6
;
43 with Sem_Eval
; use Sem_Eval
;
44 with Sem_Type
; use Sem_Type
;
45 with Sem_Util
; use Sem_Util
;
46 with Snames
; use Snames
;
47 with Stand
; use Stand
;
48 with Sinfo
; use Sinfo
;
49 with Tbuild
; use Tbuild
;
50 with Uintp
; use Uintp
;
52 package body Sem_Disp
is
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Override_Dispatching_Operation
59 (Tagged_Type
: Entity_Id
;
62 -- Replace an implicit dispatching operation with an explicit one.
63 -- Prev_Op is an inherited primitive operation which is overridden
64 -- by the explicit declaration of New_Op.
66 procedure Add_Dispatching_Operation
67 (Tagged_Type
: Entity_Id
;
69 -- Add New_Op in the list of primitive operations of Tagged_Type
71 function Check_Controlling_Type
73 Subp
: Entity_Id
) return Entity_Id
;
74 -- T is the tagged type of a formal parameter or the result of Subp.
75 -- If the subprogram has a controlling parameter or result that matches
76 -- the type, then returns the tagged type of that parameter or result
77 -- (returning the designated tagged type in the case of an access
78 -- parameter); otherwise returns empty.
80 -------------------------------
81 -- Add_Dispatching_Operation --
82 -------------------------------
84 procedure Add_Dispatching_Operation
85 (Tagged_Type
: Entity_Id
;
88 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
90 Append_Elmt
(New_Op
, List
);
91 end Add_Dispatching_Operation
;
93 -------------------------------
94 -- Check_Controlling_Formals --
95 -------------------------------
97 procedure Check_Controlling_Formals
102 Ctrl_Type
: Entity_Id
;
103 Remote
: constant Boolean :=
104 Is_Remote_Types
(Current_Scope
)
105 and then Comes_From_Source
(Subp
)
106 and then Scope
(Typ
) = Current_Scope
;
109 Formal
:= First_Formal
(Subp
);
111 while Present
(Formal
) loop
112 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
114 if Present
(Ctrl_Type
) then
115 if Ctrl_Type
= Typ
then
116 Set_Is_Controlling_Formal
(Formal
);
118 -- Ada 2005 (AI-231):Anonymous access types used in controlling
119 -- parameters exclude null because it is necessary to read the
120 -- tag to dispatch, and null has no tag.
122 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
123 Set_Can_Never_Be_Null
(Etype
(Formal
));
124 Set_Is_Known_Non_Null
(Etype
(Formal
));
127 -- Check that the parameter's nominal subtype statically
128 -- matches the first subtype.
130 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
131 if not Subtypes_Statically_Match
132 (Typ
, Designated_Type
(Etype
(Formal
)))
135 ("parameter subtype does not match controlling type",
139 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
)) then
141 ("parameter subtype does not match controlling type",
145 if Present
(Default_Value
(Formal
)) then
146 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
148 ("default not allowed for controlling access parameter",
149 Default_Value
(Formal
));
151 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
153 ("default expression must be a tag indeterminate" &
154 " function call", Default_Value
(Formal
));
158 elsif Comes_From_Source
(Subp
) then
160 ("operation can be dispatching in only one type", Subp
);
163 -- Verify that the restriction in E.2.2 (14) is obeyed
166 and then Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
169 ("access parameter of remote object primitive"
170 & " must be controlling",
174 Next_Formal
(Formal
);
177 if Present
(Etype
(Subp
)) then
178 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
180 if Present
(Ctrl_Type
) then
181 if Ctrl_Type
= Typ
then
182 Set_Has_Controlling_Result
(Subp
);
184 -- Check that the result subtype statically matches
185 -- the first subtype.
187 if not Subtypes_Statically_Match
(Typ
, Etype
(Subp
)) then
189 ("result subtype does not match controlling type", Subp
);
192 elsif Comes_From_Source
(Subp
) then
194 ("operation can be dispatching in only one type", Subp
);
197 -- The following check is clearly required, although the RM says
198 -- nothing about return types. If the return type is a limited
199 -- class-wide type declared in the current scope, there is no way
200 -- to declare stream procedures for it, so the return cannot be
204 and then Is_Limited_Type
(Typ
)
205 and then Etype
(Subp
) = Class_Wide_Type
(Typ
)
207 Error_Msg_N
("return type has no stream attributes", Subp
);
210 end Check_Controlling_Formals
;
212 ----------------------------
213 -- Check_Controlling_Type --
214 ----------------------------
216 function Check_Controlling_Type
218 Subp
: Entity_Id
) return Entity_Id
220 Tagged_Type
: Entity_Id
:= Empty
;
223 if Is_Tagged_Type
(T
) then
224 if Is_First_Subtype
(T
) then
227 Tagged_Type
:= Base_Type
(T
);
230 elsif Ekind
(T
) = E_Anonymous_Access_Type
231 and then Is_Tagged_Type
(Designated_Type
(T
))
233 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
234 if Is_First_Subtype
(Designated_Type
(T
)) then
235 Tagged_Type
:= Designated_Type
(T
);
237 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
240 -- Ada 2005 (AI-50217)
242 elsif From_With_Type
(Designated_Type
(T
))
243 and then Present
(Non_Limited_View
(Designated_Type
(T
)))
245 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
246 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
248 Tagged_Type
:= Base_Type
(Non_Limited_View
249 (Designated_Type
(T
)));
255 or else Is_Class_Wide_Type
(Tagged_Type
)
259 -- The dispatching type and the primitive operation must be defined
260 -- in the same scope, except in the case of internal operations and
261 -- formal abstract subprograms.
263 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
264 and then (not Is_Generic_Type
(Tagged_Type
)
265 or else not Comes_From_Source
(Subp
)))
267 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract
(Subp
))
269 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
271 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
280 end Check_Controlling_Type
;
282 ----------------------------
283 -- Check_Dispatching_Call --
284 ----------------------------
286 procedure Check_Dispatching_Call
(N
: Node_Id
) is
289 Control
: Node_Id
:= Empty
;
291 Subp_Entity
: Entity_Id
;
292 Loc
: constant Source_Ptr
:= Sloc
(N
);
293 Indeterm_Ancestor_Call
: Boolean := False;
294 Indeterm_Ctrl_Type
: Entity_Id
;
296 procedure Check_Dispatching_Context
;
297 -- If the call is tag-indeterminate and the entity being called is
298 -- abstract, verify that the context is a call that will eventually
299 -- provide a tag for dispatching, or has provided one already.
301 -------------------------------
302 -- Check_Dispatching_Context --
303 -------------------------------
305 procedure Check_Dispatching_Context
is
306 Subp
: constant Entity_Id
:= Entity
(Name
(N
));
310 if Is_Abstract
(Subp
)
311 and then No
(Controlling_Argument
(N
))
313 if Present
(Alias
(Subp
))
314 and then not Is_Abstract
(Alias
(Subp
))
315 and then No
(DTC_Entity
(Subp
))
317 -- Private overriding of inherited abstract operation,
320 Set_Entity
(Name
(N
), Alias
(Subp
));
326 while Present
(Par
) loop
328 if (Nkind
(Par
) = N_Function_Call
or else
329 Nkind
(Par
) = N_Procedure_Call_Statement
or else
330 Nkind
(Par
) = N_Assignment_Statement
or else
331 Nkind
(Par
) = N_Op_Eq
or else
332 Nkind
(Par
) = N_Op_Ne
)
333 and then Is_Tagged_Type
(Etype
(Subp
))
337 elsif Nkind
(Par
) = N_Qualified_Expression
338 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
343 if Ekind
(Subp
) = E_Function
then
345 ("call to abstract function must be dispatching", N
);
347 -- This error can occur for a procedure in the case of a
348 -- call to an abstract formal procedure with a statically
353 ("call to abstract procedure must be dispatching",
362 end Check_Dispatching_Context
;
364 -- Start of processing for Check_Dispatching_Call
367 -- Find a controlling argument, if any
369 if Present
(Parameter_Associations
(N
)) then
370 Actual
:= First_Actual
(N
);
372 Subp_Entity
:= Entity
(Name
(N
));
373 Formal
:= First_Formal
(Subp_Entity
);
375 while Present
(Actual
) loop
376 Control
:= Find_Controlling_Arg
(Actual
);
377 exit when Present
(Control
);
379 -- Check for the case where the actual is a tag-indeterminate call
380 -- whose result type is different than the tagged type associated
381 -- with the containing call, but is an ancestor of the type.
383 if Is_Controlling_Formal
(Formal
)
384 and then Is_Tag_Indeterminate
(Actual
)
385 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
386 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
388 Indeterm_Ancestor_Call
:= True;
389 Indeterm_Ctrl_Type
:= Etype
(Formal
);
392 Next_Actual
(Actual
);
393 Next_Formal
(Formal
);
396 -- If the call doesn't have a controlling actual but does have
397 -- an indeterminate actual that requires dispatching treatment,
398 -- then an object is needed that will serve as the controlling
399 -- argument for a dispatching call on the indeterminate actual.
400 -- This can only occur in the unusual situation of a default
401 -- actual given by a tag-indeterminate call and where the type
402 -- of the call is an ancestor of the type associated with a
403 -- containing call to an inherited operation (see AI-239).
404 -- Rather than create an object of the tagged type, which would
405 -- be problematic for various reasons (default initialization,
406 -- discriminants), the tag of the containing call's associated
407 -- tagged type is directly used to control the dispatching.
409 if not Present
(Control
)
410 and then Indeterm_Ancestor_Call
413 Make_Attribute_Reference
(Loc
,
414 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
415 Attribute_Name
=> Name_Tag
);
419 if Present
(Control
) then
421 -- Verify that no controlling arguments are statically tagged
424 Write_Str
("Found Dispatching call");
429 Actual
:= First_Actual
(N
);
431 while Present
(Actual
) loop
432 if Actual
/= Control
then
434 if not Is_Controlling_Actual
(Actual
) then
435 null; -- Can be anything
437 elsif Is_Dynamically_Tagged
(Actual
) then
438 null; -- Valid parameter
440 elsif Is_Tag_Indeterminate
(Actual
) then
442 -- The tag is inherited from the enclosing call (the
443 -- node we are currently analyzing). Explicitly expand
444 -- the actual, since the previous call to Expand
445 -- (from Resolve_Call) had no way of knowing about
446 -- the required dispatching.
448 Propagate_Tag
(Control
, Actual
);
452 ("controlling argument is not dynamically tagged",
458 Next_Actual
(Actual
);
461 -- Mark call as a dispatching call
463 Set_Controlling_Argument
(N
, Control
);
466 -- The call is not dispatching, so check that there aren't any
467 -- tag-indeterminate abstract calls left.
469 Actual
:= First_Actual
(N
);
471 while Present
(Actual
) loop
472 if Is_Tag_Indeterminate
(Actual
) then
474 -- Function call case
476 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
477 Func
:= Entity
(Name
(Original_Node
(Actual
)));
479 -- Only other possibility is a qualified expression whose
480 -- consituent expression is itself a call.
486 (Expression
(Original_Node
(Actual
)))));
489 if Is_Abstract
(Func
) then
491 "call to abstract function must be dispatching", N
);
495 Next_Actual
(Actual
);
498 Check_Dispatching_Context
;
502 -- If dispatching on result, the enclosing call, if any, will
503 -- determine the controlling argument. Otherwise this is the
504 -- primitive operation of the root type.
506 Check_Dispatching_Context
;
508 end Check_Dispatching_Call
;
510 ---------------------------------
511 -- Check_Dispatching_Operation --
512 ---------------------------------
514 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
515 Tagged_Type
: Entity_Id
;
516 Has_Dispatching_Parent
: Boolean := False;
517 Body_Is_Last_Primitive
: Boolean := False;
519 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean;
520 -- Check whether T is derived from a visibly controlled type.
521 -- This is true if the root type is declared in Ada.Finalization.
522 -- If T is derived instead from a private type whose full view
523 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
524 -- does not override the inherited one.
526 ---------------------------
527 -- Is_Visibly_Controlled --
528 ---------------------------
530 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean is
531 Root
: constant Entity_Id
:= Root_Type
(T
);
533 return Chars
(Scope
(Root
)) = Name_Finalization
534 and then Chars
(Scope
(Scope
(Root
))) = Name_Ada
535 and then Scope
(Scope
(Scope
(Root
))) = Standard_Standard
;
536 end Is_Visibly_Controlled
;
538 -- Start of processing for Check_Dispatching_Operation
541 if Ekind
(Subp
) /= E_Procedure
and then Ekind
(Subp
) /= E_Function
then
545 Set_Is_Dispatching_Operation
(Subp
, False);
546 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
550 if Ada_Version
= Ada_05
551 and then Present
(Tagged_Type
)
552 and then Is_Concurrent_Type
(Tagged_Type
)
554 -- Protect the frontend against previously detected errors
556 if not Present
(Corresponding_Record_Type
(Tagged_Type
)) then
560 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
563 -- If Subp is derived from a dispatching operation then it should
564 -- always be treated as dispatching. In this case various checks
565 -- below will be bypassed. Makes sure that late declarations for
566 -- inherited private subprograms are treated as dispatching, even
567 -- if the associated tagged type is already frozen.
569 Has_Dispatching_Parent
:=
570 Present
(Alias
(Subp
))
571 and then Is_Dispatching_Operation
(Alias
(Subp
));
573 if No
(Tagged_Type
) then
576 -- The subprograms build internally after the freezing point (such as
577 -- the Init procedure) are not primitives
579 elsif Is_Frozen
(Tagged_Type
)
580 and then not Comes_From_Source
(Subp
)
581 and then not Has_Dispatching_Parent
585 -- The operation may be a child unit, whose scope is the defining
586 -- package, but which is not a primitive operation of the type.
588 elsif Is_Child_Unit
(Subp
) then
591 -- If the subprogram is not defined in a package spec, the only case
592 -- where it can be a dispatching op is when it overrides an operation
593 -- before the freezing point of the type.
595 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
596 or else In_Package_Body
(Scope
(Subp
)))
597 and then not Has_Dispatching_Parent
599 if not Comes_From_Source
(Subp
)
600 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
604 -- If the type is already frozen, the overriding is not allowed
605 -- except when Old_Subp is not a dispatching operation (which
606 -- can occur when Old_Subp was inherited by an untagged type).
607 -- However, a body with no previous spec freezes the type "after"
608 -- its declaration, and therefore is a legal overriding (unless
609 -- the type has already been frozen). Only the first such body
612 elsif Present
(Old_Subp
)
613 and then Is_Dispatching_Operation
(Old_Subp
)
615 if Comes_From_Source
(Subp
)
617 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
618 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
621 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
622 Decl_Item
: Node_Id
:= Next
(Parent
(Tagged_Type
));
625 -- ??? The checks here for whether the type has been
626 -- frozen prior to the new body are not complete. It's
627 -- not simple to check frozenness at this point since
628 -- the body has already caused the type to be prematurely
629 -- frozen in Analyze_Declarations, but we're forced to
630 -- recheck this here because of the odd rule interpretation
631 -- that allows the overriding if the type wasn't frozen
632 -- prior to the body. The freezing action should probably
633 -- be delayed until after the spec is seen, but that's
634 -- a tricky change to the delicate freezing code.
636 -- Look at each declaration following the type up
637 -- until the new subprogram body. If any of the
638 -- declarations is a body then the type has been
639 -- frozen already so the overriding primitive is
642 while Present
(Decl_Item
)
643 and then (Decl_Item
/= Subp_Body
)
645 if Comes_From_Source
(Decl_Item
)
646 and then (Nkind
(Decl_Item
) in N_Proper_Body
647 or else Nkind
(Decl_Item
) in N_Body_Stub
)
649 Error_Msg_N
("overriding of& is too late!", Subp
);
651 ("\spec should appear immediately after the type!",
659 -- If the subprogram doesn't follow in the list of
660 -- declarations including the type then the type
661 -- has definitely been frozen already and the body
664 if not Present
(Decl_Item
) then
665 Error_Msg_N
("overriding of& is too late!", Subp
);
667 ("\spec should appear immediately after the type!",
670 elsif Is_Frozen
(Subp
) then
672 -- The subprogram body declares a primitive operation.
673 -- if the subprogram is already frozen, we must update
674 -- its dispatching information explicitly here. The
675 -- information is taken from the overridden subprogram.
677 Body_Is_Last_Primitive
:= True;
679 if Present
(DTC_Entity
(Old_Subp
)) then
680 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
681 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
683 Subp_Body
, Fill_DT_Entry
(Sloc
(Subp_Body
), Subp
));
689 Error_Msg_N
("overriding of& is too late!", Subp
);
691 ("\subprogram spec should appear immediately after the type!",
695 -- If the type is not frozen yet and we are not in the overridding
696 -- case it looks suspiciously like an attempt to define a primitive
699 elsif not Is_Frozen
(Tagged_Type
) then
701 ("?not dispatching (must be defined in a package spec)", Subp
);
704 -- When the type is frozen, it is legitimate to define a new
705 -- non-primitive operation.
711 -- Now, we are sure that the scope is a package spec. If the subprogram
712 -- is declared after the freezing point ot the type that's an error
714 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
715 Error_Msg_N
("this primitive operation is declared too late", Subp
);
717 ("?no primitive operations for& after this line",
718 Freeze_Node
(Tagged_Type
),
723 Check_Controlling_Formals
(Tagged_Type
, Subp
);
725 -- Now it should be a correct primitive operation, put it in the list
727 if Present
(Old_Subp
) then
728 Check_Subtype_Conformant
(Subp
, Old_Subp
);
729 if (Chars
(Subp
) = Name_Initialize
730 or else Chars
(Subp
) = Name_Adjust
731 or else Chars
(Subp
) = Name_Finalize
)
732 and then Is_Controlled
(Tagged_Type
)
733 and then not Is_Visibly_Controlled
(Tagged_Type
)
735 Set_Is_Overriding_Operation
(Subp
, False);
737 ("operation does not override inherited&?", Subp
, Subp
);
739 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
740 Set_Is_Overriding_Operation
(Subp
);
743 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
746 Set_Is_Dispatching_Operation
(Subp
, True);
748 if not Body_Is_Last_Primitive
then
749 Set_DT_Position
(Subp
, No_Uint
);
751 elsif Has_Controlled_Component
(Tagged_Type
)
753 (Chars
(Subp
) = Name_Initialize
754 or else Chars
(Subp
) = Name_Adjust
755 or else Chars
(Subp
) = Name_Finalize
)
758 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
762 Old_Spec
: Entity_Id
;
764 C_Names
: constant array (1 .. 3) of Name_Id
:=
769 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
770 (TSS_Deep_Initialize
,
775 -- Remove previous controlled function, which was constructed
776 -- and analyzed when the type was frozen. This requires
777 -- removing the body of the redefined primitive, as well as
778 -- its specification if needed (there is no spec created for
779 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
780 -- the exception information that may have been generated for
781 -- it when front end zero-cost tables are enabled.
783 for J
in D_Names
'Range loop
784 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
787 and then Chars
(Subp
) = C_Names
(J
)
789 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
791 Set_Is_Eliminated
(Old_P
);
792 Set_Scope
(Old_P
, Scope
(Current_Scope
));
794 if Nkind
(Old_Bod
) = N_Subprogram_Body
795 and then Present
(Corresponding_Spec
(Old_Bod
))
797 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
798 Set_Has_Completion
(Old_Spec
, False);
803 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
805 -- The new operation is added to the actions of the freeze
806 -- node for the type, but this node has already been analyzed,
807 -- so we must retrieve and analyze explicitly the one new body,
810 and then Present
(Actions
(F_Node
))
812 Decl
:= Last
(Actions
(F_Node
));
817 end Check_Dispatching_Operation
;
819 ------------------------------------------
820 -- Check_Operation_From_Incomplete_Type --
821 ------------------------------------------
823 procedure Check_Operation_From_Incomplete_Type
827 Full
: constant Entity_Id
:= Full_View
(Typ
);
828 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
829 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
830 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
832 Prev
: Elmt_Id
:= No_Elmt
;
834 function Derives_From
(Proc
: Entity_Id
) return Boolean;
835 -- Check that Subp has the signature of an operation derived from Proc.
836 -- Subp has an access parameter that designates Typ.
842 function Derives_From
(Proc
: Entity_Id
) return Boolean is
846 if Chars
(Proc
) /= Chars
(Subp
) then
850 F1
:= First_Formal
(Proc
);
851 F2
:= First_Formal
(Subp
);
853 while Present
(F1
) and then Present
(F2
) loop
855 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
857 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
860 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
861 and then Designated_Type
(Etype
(F2
)) /= Full
866 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
869 elsif Etype
(F1
) /= Etype
(F2
) then
877 return No
(F1
) and then No
(F2
);
880 -- Start of processing for Check_Operation_From_Incomplete_Type
883 -- The operation may override an inherited one, or may be a new one
884 -- altogether. The inherited operation will have been hidden by the
885 -- current one at the point of the type derivation, so it does not
886 -- appear in the list of primitive operations of the type. We have to
887 -- find the proper place of insertion in the list of primitive opera-
888 -- tions by iterating over the list for the parent type.
890 Op1
:= First_Elmt
(Old_Prim
);
891 Op2
:= First_Elmt
(New_Prim
);
893 while Present
(Op1
) and then Present
(Op2
) loop
895 if Derives_From
(Node
(Op1
)) then
898 Prepend_Elmt
(Subp
, New_Prim
);
900 Insert_Elmt_After
(Subp
, Prev
);
911 -- Operation is a new primitive
913 Append_Elmt
(Subp
, New_Prim
);
914 end Check_Operation_From_Incomplete_Type
;
916 ---------------------------------------
917 -- Check_Operation_From_Private_View --
918 ---------------------------------------
920 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
921 Tagged_Type
: Entity_Id
;
924 if Is_Dispatching_Operation
(Alias
(Subp
)) then
925 Set_Scope
(Subp
, Current_Scope
);
926 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
928 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
929 Append_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
931 -- If Old_Subp isn't already marked as dispatching then
932 -- this is the case of an operation of an untagged private
933 -- type fulfilled by a tagged type that overrides an
934 -- inherited dispatching operation, so we set the necessary
935 -- dispatching attributes here.
937 if not Is_Dispatching_Operation
(Old_Subp
) then
939 -- If the untagged type has no discriminants, and the full
940 -- view is constrained, there will be a spurious mismatch
941 -- of subtypes on the controlling arguments, because the tagged
942 -- type is the internal base type introduced in the derivation.
943 -- Use the original type to verify conformance, rather than the
946 if not Comes_From_Source
(Tagged_Type
)
947 and then Has_Discriminants
(Tagged_Type
)
952 Formal
:= First_Formal
(Old_Subp
);
953 while Present
(Formal
) loop
954 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
955 Tagged_Type
:= Etype
(Formal
);
958 Next_Formal
(Formal
);
962 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
963 Tagged_Type
:= Etype
(Old_Subp
);
967 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
968 Set_Is_Dispatching_Operation
(Old_Subp
, True);
969 Set_DT_Position
(Old_Subp
, No_Uint
);
972 -- If the old subprogram is an explicit renaming of some other
973 -- entity, it is not overridden by the inherited subprogram.
974 -- Otherwise, update its alias and other attributes.
976 if Present
(Alias
(Old_Subp
))
977 and then Nkind
(Unit_Declaration_Node
(Old_Subp
))
978 /= N_Subprogram_Renaming_Declaration
980 Set_Alias
(Old_Subp
, Alias
(Subp
));
982 -- The derived subprogram should inherit the abstractness
983 -- of the parent subprogram (except in the case of a function
984 -- returning the type). This sets the abstractness properly
985 -- for cases where a private extension may have inherited
986 -- an abstract operation, but the full type is derived from
987 -- a descendant type and inherits a nonabstract version.
989 if Etype
(Subp
) /= Tagged_Type
then
990 Set_Is_Abstract
(Old_Subp
, Is_Abstract
(Alias
(Subp
)));
995 end Check_Operation_From_Private_View
;
997 --------------------------
998 -- Find_Controlling_Arg --
999 --------------------------
1001 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1002 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1006 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1007 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1010 -- Dispatching on result case
1012 if Nkind
(Orig_Node
) = N_Function_Call
1013 and then Present
(Controlling_Argument
(Orig_Node
))
1014 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1016 return Controlling_Argument
(Orig_Node
);
1020 elsif Is_Controlling_Actual
(N
)
1022 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1023 and then Is_Controlling_Actual
(Parent
(N
)))
1027 if Is_Access_Type
(Typ
) then
1028 -- In the case of an Access attribute, use the type of
1029 -- the prefix, since in the case of an actual for an
1030 -- access parameter, the attribute's type may be of a
1031 -- specific designated type, even though the prefix
1032 -- type is class-wide.
1034 if Nkind
(N
) = N_Attribute_Reference
then
1035 Typ
:= Etype
(Prefix
(N
));
1037 -- An allocator is dispatching if the type of qualified
1038 -- expression is class_wide, in which case this is the
1039 -- controlling type.
1041 elsif Nkind
(Orig_Node
) = N_Allocator
1042 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1044 Typ
:= Etype
(Expression
(Orig_Node
));
1047 Typ
:= Designated_Type
(Typ
);
1051 if Is_Class_Wide_Type
(Typ
)
1053 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1054 and then Is_Access_Type
(Etype
(N
))
1055 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1062 end Find_Controlling_Arg
;
1064 ---------------------------
1065 -- Find_Dispatching_Type --
1066 ---------------------------
1068 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1070 Ctrl_Type
: Entity_Id
;
1073 if Present
(DTC_Entity
(Subp
)) then
1074 return Scope
(DTC_Entity
(Subp
));
1077 Formal
:= First_Formal
(Subp
);
1078 while Present
(Formal
) loop
1079 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1081 if Present
(Ctrl_Type
) then
1085 Next_Formal
(Formal
);
1088 -- The subprogram may also be dispatching on result
1090 if Present
(Etype
(Subp
)) then
1091 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
1093 if Present
(Ctrl_Type
) then
1100 end Find_Dispatching_Type
;
1102 ---------------------------
1103 -- Is_Dynamically_Tagged --
1104 ---------------------------
1106 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1108 return Find_Controlling_Arg
(N
) /= Empty
;
1109 end Is_Dynamically_Tagged
;
1111 --------------------------
1112 -- Is_Tag_Indeterminate --
1113 --------------------------
1115 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1118 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1121 if Nkind
(Orig_Node
) = N_Function_Call
1122 and then Is_Entity_Name
(Name
(Orig_Node
))
1124 Nam
:= Entity
(Name
(Orig_Node
));
1126 if not Has_Controlling_Result
(Nam
) then
1129 -- An explicit dereference means that the call has already been
1130 -- expanded and there is no tag to propagate.
1132 elsif Nkind
(N
) = N_Explicit_Dereference
then
1135 -- If there are no actuals, the call is tag-indeterminate
1137 elsif No
(Parameter_Associations
(Orig_Node
)) then
1141 Actual
:= First_Actual
(Orig_Node
);
1143 while Present
(Actual
) loop
1144 if Is_Controlling_Actual
(Actual
)
1145 and then not Is_Tag_Indeterminate
(Actual
)
1147 return False; -- one operand is dispatching
1150 Next_Actual
(Actual
);
1157 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
1158 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
1163 end Is_Tag_Indeterminate
;
1165 ------------------------------------
1166 -- Override_Dispatching_Operation --
1167 ------------------------------------
1169 procedure Override_Dispatching_Operation
1170 (Tagged_Type
: Entity_Id
;
1171 Prev_Op
: Entity_Id
;
1174 Op_Elmt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1178 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean;
1179 -- Comment requjired ???
1181 -----------------------------
1182 -- Is_Interface_Subprogram --
1183 -----------------------------
1185 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean is
1190 while Present
(Alias
(Aux
))
1191 and then Present
(DTC_Entity
(Alias
(Aux
)))
1193 if Is_Interface
(Scope
(DTC_Entity
(Alias
(Aux
)))) then
1200 end Is_Interface_Subprogram
;
1202 -- Start of processing for Override_Dispatching_Operation
1205 -- Patch the primitive operation list
1207 while Present
(Op_Elmt
)
1208 and then Node
(Op_Elmt
) /= Prev_Op
1210 Next_Elmt
(Op_Elmt
);
1213 -- If there is no previous operation to override, the type declaration
1214 -- was malformed, and an error must have been emitted already.
1216 if No
(Op_Elmt
) then
1220 -- Ada 2005 (AI-251): Do not replace subprograms inherited from
1221 -- abstract interfaces. They will be used later to generate the
1222 -- corresponding thunks to initialize the Vtable (see subprogram
1223 -- Freeze_Subprogram). The inherited operation itself must also
1224 -- become hidden, to avoid spurious ambiguities; name resolution
1225 -- must pick up only the operation that implements it,
1227 if Is_Interface_Subprogram
(Prev_Op
) then
1228 Set_DT_Position
(Prev_Op
, DT_Position
(Alias
(Prev_Op
)));
1229 Set_Is_Abstract
(Prev_Op
, Is_Abstract
(New_Op
));
1230 Set_Is_Overriding_Operation
(Prev_Op
);
1231 Set_Abstract_Interface_Alias
(Prev_Op
, Alias
(Prev_Op
));
1232 Set_Alias
(Prev_Op
, New_Op
);
1233 Set_Is_Internal
(Prev_Op
);
1234 Set_Is_Hidden
(Prev_Op
);
1236 -- Override predefined primitive operations
1238 if Is_Predefined_Dispatching_Operation
(Prev_Op
) then
1239 Replace_Elmt
(Op_Elmt
, New_Op
);
1243 -- Check if this primitive operation was previously added for another
1246 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1248 while Present
(Elmt
) loop
1249 if Node
(Elmt
) = New_Op
then
1258 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
1259 -- Replace_Elmt (Op_Elmt, New_Op); -- why is this commented out???
1264 Replace_Elmt
(Op_Elmt
, New_Op
);
1267 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
1268 or else not In_Private_Part
(Current_Scope
)
1270 -- Not a private primitive
1274 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1276 -- Make the overriding operation into an alias of the implicit one.
1277 -- In this fashion a call from outside ends up calling the new
1278 -- body even if non-dispatching, and a call from inside calls the
1279 -- overriding operation because it hides the implicit one.
1280 -- To indicate that the body of Prev_Op is never called, set its
1281 -- dispatch table entity to Empty.
1283 Set_Alias
(Prev_Op
, New_Op
);
1284 Set_DTC_Entity
(Prev_Op
, Empty
);
1287 end Override_Dispatching_Operation
;
1293 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1294 Call_Node
: Node_Id
;
1298 if Nkind
(Actual
) = N_Function_Call
then
1299 Call_Node
:= Actual
;
1301 elsif Nkind
(Actual
) = N_Identifier
1302 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1304 -- Call rewritten as object declaration when stack-checking
1305 -- is enabled. Propagate tag to expression in declaration, which
1306 -- is original call.
1308 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1310 -- Only other possibility is parenthesized or qualified expression
1313 Call_Node
:= Expression
(Actual
);
1316 -- Do not set the Controlling_Argument if already set. This happens
1317 -- in the special case of _Input (see Exp_Attr, case Input).
1319 if No
(Controlling_Argument
(Call_Node
)) then
1320 Set_Controlling_Argument
(Call_Node
, Control
);
1323 Arg
:= First_Actual
(Call_Node
);
1325 while Present
(Arg
) loop
1326 if Is_Tag_Indeterminate
(Arg
) then
1327 Propagate_Tag
(Control
, Arg
);
1333 -- Expansion of dispatching calls is suppressed when Java_VM, because
1334 -- the JVM back end directly handles the generation of dispatching
1335 -- calls and would have to undo any expansion to an indirect call.
1338 Expand_Dispatching_Call
(Call_Node
);