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
)
553 and then not Is_Empty_Elmt_List
555 (Corresponding_Record_Type
(Tagged_Type
)))
557 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
560 -- If Subp is derived from a dispatching operation then it should
561 -- always be treated as dispatching. In this case various checks
562 -- below will be bypassed. Makes sure that late declarations for
563 -- inherited private subprograms are treated as dispatching, even
564 -- if the associated tagged type is already frozen.
566 Has_Dispatching_Parent
:=
567 Present
(Alias
(Subp
))
568 and then Is_Dispatching_Operation
(Alias
(Subp
));
570 if No
(Tagged_Type
) then
573 -- The subprograms build internally after the freezing point (such as
574 -- the Init procedure) are not primitives
576 elsif Is_Frozen
(Tagged_Type
)
577 and then not Comes_From_Source
(Subp
)
578 and then not Has_Dispatching_Parent
582 -- The operation may be a child unit, whose scope is the defining
583 -- package, but which is not a primitive operation of the type.
585 elsif Is_Child_Unit
(Subp
) then
588 -- If the subprogram is not defined in a package spec, the only case
589 -- where it can be a dispatching op is when it overrides an operation
590 -- before the freezing point of the type.
592 elsif ((not Is_Package
(Scope
(Subp
)))
593 or else In_Package_Body
(Scope
(Subp
)))
594 and then not Has_Dispatching_Parent
596 if not Comes_From_Source
(Subp
)
597 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
601 -- If the type is already frozen, the overriding is not allowed
602 -- except when Old_Subp is not a dispatching operation (which
603 -- can occur when Old_Subp was inherited by an untagged type).
604 -- However, a body with no previous spec freezes the type "after"
605 -- its declaration, and therefore is a legal overriding (unless
606 -- the type has already been frozen). Only the first such body
609 elsif Present
(Old_Subp
)
610 and then Is_Dispatching_Operation
(Old_Subp
)
612 if Comes_From_Source
(Subp
)
614 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
615 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
618 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
619 Decl_Item
: Node_Id
:= Next
(Parent
(Tagged_Type
));
622 -- ??? The checks here for whether the type has been
623 -- frozen prior to the new body are not complete. It's
624 -- not simple to check frozenness at this point since
625 -- the body has already caused the type to be prematurely
626 -- frozen in Analyze_Declarations, but we're forced to
627 -- recheck this here because of the odd rule interpretation
628 -- that allows the overriding if the type wasn't frozen
629 -- prior to the body. The freezing action should probably
630 -- be delayed until after the spec is seen, but that's
631 -- a tricky change to the delicate freezing code.
633 -- Look at each declaration following the type up
634 -- until the new subprogram body. If any of the
635 -- declarations is a body then the type has been
636 -- frozen already so the overriding primitive is
639 while Present
(Decl_Item
)
640 and then (Decl_Item
/= Subp_Body
)
642 if Comes_From_Source
(Decl_Item
)
643 and then (Nkind
(Decl_Item
) in N_Proper_Body
644 or else Nkind
(Decl_Item
) in N_Body_Stub
)
646 Error_Msg_N
("overriding of& is too late!", Subp
);
648 ("\spec should appear immediately after the type!",
656 -- If the subprogram doesn't follow in the list of
657 -- declarations including the type then the type
658 -- has definitely been frozen already and the body
661 if not Present
(Decl_Item
) then
662 Error_Msg_N
("overriding of& is too late!", Subp
);
664 ("\spec should appear immediately after the type!",
667 elsif Is_Frozen
(Subp
) then
669 -- The subprogram body declares a primitive operation.
670 -- if the subprogram is already frozen, we must update
671 -- its dispatching information explicitly here. The
672 -- information is taken from the overridden subprogram.
674 Body_Is_Last_Primitive
:= True;
676 if Present
(DTC_Entity
(Old_Subp
)) then
677 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
678 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
680 Subp_Body
, Fill_DT_Entry
(Sloc
(Subp_Body
), Subp
));
686 Error_Msg_N
("overriding of& is too late!", Subp
);
688 ("\subprogram spec should appear immediately after the type!",
692 -- If the type is not frozen yet and we are not in the overridding
693 -- case it looks suspiciously like an attempt to define a primitive
696 elsif not Is_Frozen
(Tagged_Type
) then
698 ("?not dispatching (must be defined in a package spec)", Subp
);
701 -- When the type is frozen, it is legitimate to define a new
702 -- non-primitive operation.
708 -- Now, we are sure that the scope is a package spec. If the subprogram
709 -- is declared after the freezing point ot the type that's an error
711 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
712 Error_Msg_N
("this primitive operation is declared too late", Subp
);
714 ("?no primitive operations for& after this line",
715 Freeze_Node
(Tagged_Type
),
720 Check_Controlling_Formals
(Tagged_Type
, Subp
);
722 -- Now it should be a correct primitive operation, put it in the list
724 if Present
(Old_Subp
) then
725 Check_Subtype_Conformant
(Subp
, Old_Subp
);
726 if (Chars
(Subp
) = Name_Initialize
727 or else Chars
(Subp
) = Name_Adjust
728 or else Chars
(Subp
) = Name_Finalize
)
729 and then Is_Controlled
(Tagged_Type
)
730 and then not Is_Visibly_Controlled
(Tagged_Type
)
732 Set_Is_Overriding_Operation
(Subp
, False);
734 ("operation does not override inherited&?", Subp
, Subp
);
736 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
737 Set_Is_Overriding_Operation
(Subp
);
740 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
743 Set_Is_Dispatching_Operation
(Subp
, True);
745 if not Body_Is_Last_Primitive
then
746 Set_DT_Position
(Subp
, No_Uint
);
748 elsif Has_Controlled_Component
(Tagged_Type
)
750 (Chars
(Subp
) = Name_Initialize
751 or else Chars
(Subp
) = Name_Adjust
752 or else Chars
(Subp
) = Name_Finalize
)
755 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
759 Old_Spec
: Entity_Id
;
761 C_Names
: constant array (1 .. 3) of Name_Id
:=
766 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
767 (TSS_Deep_Initialize
,
772 -- Remove previous controlled function, which was constructed
773 -- and analyzed when the type was frozen. This requires
774 -- removing the body of the redefined primitive, as well as
775 -- its specification if needed (there is no spec created for
776 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
777 -- the exception information that may have been generated for
778 -- it when front end zero-cost tables are enabled.
780 for J
in D_Names
'Range loop
781 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
784 and then Chars
(Subp
) = C_Names
(J
)
786 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
788 Set_Is_Eliminated
(Old_P
);
789 Set_Scope
(Old_P
, Scope
(Current_Scope
));
791 if Nkind
(Old_Bod
) = N_Subprogram_Body
792 and then Present
(Corresponding_Spec
(Old_Bod
))
794 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
795 Set_Has_Completion
(Old_Spec
, False);
800 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
802 -- The new operation is added to the actions of the freeze
803 -- node for the type, but this node has already been analyzed,
804 -- so we must retrieve and analyze explicitly the one new body,
807 and then Present
(Actions
(F_Node
))
809 Decl
:= Last
(Actions
(F_Node
));
814 end Check_Dispatching_Operation
;
816 ------------------------------------------
817 -- Check_Operation_From_Incomplete_Type --
818 ------------------------------------------
820 procedure Check_Operation_From_Incomplete_Type
824 Full
: constant Entity_Id
:= Full_View
(Typ
);
825 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
826 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
827 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
829 Prev
: Elmt_Id
:= No_Elmt
;
831 function Derives_From
(Proc
: Entity_Id
) return Boolean;
832 -- Check that Subp has the signature of an operation derived from Proc.
833 -- Subp has an access parameter that designates Typ.
839 function Derives_From
(Proc
: Entity_Id
) return Boolean is
843 if Chars
(Proc
) /= Chars
(Subp
) then
847 F1
:= First_Formal
(Proc
);
848 F2
:= First_Formal
(Subp
);
850 while Present
(F1
) and then Present
(F2
) loop
852 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
854 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
857 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
858 and then Designated_Type
(Etype
(F2
)) /= Full
863 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
866 elsif Etype
(F1
) /= Etype
(F2
) then
874 return No
(F1
) and then No
(F2
);
877 -- Start of processing for Check_Operation_From_Incomplete_Type
880 -- The operation may override an inherited one, or may be a new one
881 -- altogether. The inherited operation will have been hidden by the
882 -- current one at the point of the type derivation, so it does not
883 -- appear in the list of primitive operations of the type. We have to
884 -- find the proper place of insertion in the list of primitive opera-
885 -- tions by iterating over the list for the parent type.
887 Op1
:= First_Elmt
(Old_Prim
);
888 Op2
:= First_Elmt
(New_Prim
);
890 while Present
(Op1
) and then Present
(Op2
) loop
892 if Derives_From
(Node
(Op1
)) then
895 Prepend_Elmt
(Subp
, New_Prim
);
897 Insert_Elmt_After
(Subp
, Prev
);
908 -- Operation is a new primitive
910 Append_Elmt
(Subp
, New_Prim
);
911 end Check_Operation_From_Incomplete_Type
;
913 ---------------------------------------
914 -- Check_Operation_From_Private_View --
915 ---------------------------------------
917 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
918 Tagged_Type
: Entity_Id
;
921 if Is_Dispatching_Operation
(Alias
(Subp
)) then
922 Set_Scope
(Subp
, Current_Scope
);
923 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
925 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
926 Append_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
928 -- If Old_Subp isn't already marked as dispatching then
929 -- this is the case of an operation of an untagged private
930 -- type fulfilled by a tagged type that overrides an
931 -- inherited dispatching operation, so we set the necessary
932 -- dispatching attributes here.
934 if not Is_Dispatching_Operation
(Old_Subp
) then
936 -- If the untagged type has no discriminants, and the full
937 -- view is constrained, there will be a spurious mismatch
938 -- of subtypes on the controlling arguments, because the tagged
939 -- type is the internal base type introduced in the derivation.
940 -- Use the original type to verify conformance, rather than the
943 if not Comes_From_Source
(Tagged_Type
)
944 and then Has_Discriminants
(Tagged_Type
)
949 Formal
:= First_Formal
(Old_Subp
);
950 while Present
(Formal
) loop
951 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
952 Tagged_Type
:= Etype
(Formal
);
955 Next_Formal
(Formal
);
959 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
960 Tagged_Type
:= Etype
(Old_Subp
);
964 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
965 Set_Is_Dispatching_Operation
(Old_Subp
, True);
966 Set_DT_Position
(Old_Subp
, No_Uint
);
969 -- If the old subprogram is an explicit renaming of some other
970 -- entity, it is not overridden by the inherited subprogram.
971 -- Otherwise, update its alias and other attributes.
973 if Present
(Alias
(Old_Subp
))
974 and then Nkind
(Unit_Declaration_Node
(Old_Subp
))
975 /= N_Subprogram_Renaming_Declaration
977 Set_Alias
(Old_Subp
, Alias
(Subp
));
979 -- The derived subprogram should inherit the abstractness
980 -- of the parent subprogram (except in the case of a function
981 -- returning the type). This sets the abstractness properly
982 -- for cases where a private extension may have inherited
983 -- an abstract operation, but the full type is derived from
984 -- a descendant type and inherits a nonabstract version.
986 if Etype
(Subp
) /= Tagged_Type
then
987 Set_Is_Abstract
(Old_Subp
, Is_Abstract
(Alias
(Subp
)));
992 end Check_Operation_From_Private_View
;
994 --------------------------
995 -- Find_Controlling_Arg --
996 --------------------------
998 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
999 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1003 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1004 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1007 -- Dispatching on result case
1009 if Nkind
(Orig_Node
) = N_Function_Call
1010 and then Present
(Controlling_Argument
(Orig_Node
))
1011 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1013 return Controlling_Argument
(Orig_Node
);
1017 elsif Is_Controlling_Actual
(N
)
1019 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1020 and then Is_Controlling_Actual
(Parent
(N
)))
1024 if Is_Access_Type
(Typ
) then
1025 -- In the case of an Access attribute, use the type of
1026 -- the prefix, since in the case of an actual for an
1027 -- access parameter, the attribute's type may be of a
1028 -- specific designated type, even though the prefix
1029 -- type is class-wide.
1031 if Nkind
(N
) = N_Attribute_Reference
then
1032 Typ
:= Etype
(Prefix
(N
));
1034 -- An allocator is dispatching if the type of qualified
1035 -- expression is class_wide, in which case this is the
1036 -- controlling type.
1038 elsif Nkind
(Orig_Node
) = N_Allocator
1039 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1041 Typ
:= Etype
(Expression
(Orig_Node
));
1044 Typ
:= Designated_Type
(Typ
);
1048 if Is_Class_Wide_Type
(Typ
)
1050 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1051 and then Is_Access_Type
(Etype
(N
))
1052 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1059 end Find_Controlling_Arg
;
1061 ---------------------------
1062 -- Find_Dispatching_Type --
1063 ---------------------------
1065 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1067 Ctrl_Type
: Entity_Id
;
1070 if Present
(DTC_Entity
(Subp
)) then
1071 return Scope
(DTC_Entity
(Subp
));
1074 Formal
:= First_Formal
(Subp
);
1075 while Present
(Formal
) loop
1076 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1078 if Present
(Ctrl_Type
) then
1082 Next_Formal
(Formal
);
1085 -- The subprogram may also be dispatching on result
1087 if Present
(Etype
(Subp
)) then
1088 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
1090 if Present
(Ctrl_Type
) then
1097 end Find_Dispatching_Type
;
1099 ---------------------------
1100 -- Is_Dynamically_Tagged --
1101 ---------------------------
1103 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1105 return Find_Controlling_Arg
(N
) /= Empty
;
1106 end Is_Dynamically_Tagged
;
1108 --------------------------
1109 -- Is_Tag_Indeterminate --
1110 --------------------------
1112 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1115 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1118 if Nkind
(Orig_Node
) = N_Function_Call
1119 and then Is_Entity_Name
(Name
(Orig_Node
))
1121 Nam
:= Entity
(Name
(Orig_Node
));
1123 if not Has_Controlling_Result
(Nam
) then
1126 -- An explicit dereference means that the call has already been
1127 -- expanded and there is no tag to propagate.
1129 elsif Nkind
(N
) = N_Explicit_Dereference
then
1132 -- If there are no actuals, the call is tag-indeterminate
1134 elsif No
(Parameter_Associations
(Orig_Node
)) then
1138 Actual
:= First_Actual
(Orig_Node
);
1140 while Present
(Actual
) loop
1141 if Is_Controlling_Actual
(Actual
)
1142 and then not Is_Tag_Indeterminate
(Actual
)
1144 return False; -- one operand is dispatching
1147 Next_Actual
(Actual
);
1154 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
1155 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
1160 end Is_Tag_Indeterminate
;
1162 ------------------------------------
1163 -- Override_Dispatching_Operation --
1164 ------------------------------------
1166 procedure Override_Dispatching_Operation
1167 (Tagged_Type
: Entity_Id
;
1168 Prev_Op
: Entity_Id
;
1171 Op_Elmt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1175 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean;
1176 -- Comment requjired ???
1178 -----------------------------
1179 -- Is_Interface_Subprogram --
1180 -----------------------------
1182 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean is
1187 while Present
(Alias
(Aux
))
1188 and then Present
(DTC_Entity
(Alias
(Aux
)))
1190 if Is_Interface
(Scope
(DTC_Entity
(Alias
(Aux
)))) then
1197 end Is_Interface_Subprogram
;
1199 -- Start of processing for Override_Dispatching_Operation
1202 -- Patch the primitive operation list
1204 while Present
(Op_Elmt
)
1205 and then Node
(Op_Elmt
) /= Prev_Op
1207 Next_Elmt
(Op_Elmt
);
1210 -- If there is no previous operation to override, the type declaration
1211 -- was malformed, and an error must have been emitted already.
1213 if No
(Op_Elmt
) then
1217 -- Ada 2005 (AI-251): Do not replace subprograms inherited from
1218 -- abstract interfaces. They will be used later to generate the
1219 -- corresponding thunks to initialize the Vtable (see subprogram
1220 -- Freeze_Subprogram). The inherited operation itself must also
1221 -- become hidden, to avoid spurious ambiguities; name resolution
1222 -- must pick up only the operation that implements it,
1224 if Is_Interface_Subprogram
(Prev_Op
) then
1225 Set_DT_Position
(Prev_Op
, DT_Position
(Alias
(Prev_Op
)));
1226 Set_Is_Abstract
(Prev_Op
, Is_Abstract
(New_Op
));
1227 Set_Is_Overriding_Operation
(Prev_Op
);
1228 Set_Abstract_Interface_Alias
(Prev_Op
, Alias
(Prev_Op
));
1229 Set_Alias
(Prev_Op
, New_Op
);
1230 Set_Is_Internal
(Prev_Op
);
1231 Set_Is_Hidden
(Prev_Op
);
1233 -- Override predefined primitive operations
1235 if Is_Predefined_Dispatching_Operation
(Prev_Op
) then
1236 Replace_Elmt
(Op_Elmt
, New_Op
);
1240 -- Check if this primitive operation was previously added for another
1243 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1245 while Present
(Elmt
) loop
1246 if Node
(Elmt
) = New_Op
then
1255 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
1256 -- Replace_Elmt (Op_Elmt, New_Op); -- why is this commented out???
1261 Replace_Elmt
(Op_Elmt
, New_Op
);
1264 if (not Is_Package
(Current_Scope
))
1265 or else not In_Private_Part
(Current_Scope
)
1267 -- Not a private primitive
1271 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1273 -- Make the overriding operation into an alias of the implicit one.
1274 -- In this fashion a call from outside ends up calling the new
1275 -- body even if non-dispatching, and a call from inside calls the
1276 -- overriding operation because it hides the implicit one.
1277 -- To indicate that the body of Prev_Op is never called, set its
1278 -- dispatch table entity to Empty.
1280 Set_Alias
(Prev_Op
, New_Op
);
1281 Set_DTC_Entity
(Prev_Op
, Empty
);
1284 end Override_Dispatching_Operation
;
1290 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1291 Call_Node
: Node_Id
;
1295 if Nkind
(Actual
) = N_Function_Call
then
1296 Call_Node
:= Actual
;
1298 elsif Nkind
(Actual
) = N_Identifier
1299 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1301 -- Call rewritten as object declaration when stack-checking
1302 -- is enabled. Propagate tag to expression in declaration, which
1303 -- is original call.
1305 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1307 -- Only other possibility is parenthesized or qualified expression
1310 Call_Node
:= Expression
(Actual
);
1313 -- Do not set the Controlling_Argument if already set. This happens
1314 -- in the special case of _Input (see Exp_Attr, case Input).
1316 if No
(Controlling_Argument
(Call_Node
)) then
1317 Set_Controlling_Argument
(Call_Node
, Control
);
1320 Arg
:= First_Actual
(Call_Node
);
1322 while Present
(Arg
) loop
1323 if Is_Tag_Indeterminate
(Arg
) then
1324 Propagate_Tag
(Control
, Arg
);
1330 -- Expansion of dispatching calls is suppressed when Java_VM, because
1331 -- the JVM back end directly handles the generation of dispatching
1332 -- calls and would have to undo any expansion to an indirect call.
1335 Expand_Dispatching_Call
(Call_Node
);