1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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
;
41 with Restrict
; use Restrict
;
42 with Rident
; use Rident
;
44 with Sem_Ch6
; use Sem_Ch6
;
45 with Sem_Eval
; use Sem_Eval
;
46 with Sem_Type
; use Sem_Type
;
47 with Sem_Util
; use Sem_Util
;
48 with Snames
; use Snames
;
49 with Stand
; use Stand
;
50 with Sinfo
; use Sinfo
;
51 with Tbuild
; use Tbuild
;
52 with Uintp
; use Uintp
;
54 package body Sem_Disp
is
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Add_Dispatching_Operation
61 (Tagged_Type
: Entity_Id
;
63 -- Add New_Op in the list of primitive operations of Tagged_Type
65 function Check_Controlling_Type
67 Subp
: Entity_Id
) return Entity_Id
;
68 -- T is the tagged type of a formal parameter or the result of Subp.
69 -- If the subprogram has a controlling parameter or result that matches
70 -- the type, then returns the tagged type of that parameter or result
71 -- (returning the designated tagged type in the case of an access
72 -- parameter); otherwise returns empty.
74 -------------------------------
75 -- Add_Dispatching_Operation --
76 -------------------------------
78 procedure Add_Dispatching_Operation
79 (Tagged_Type
: Entity_Id
;
82 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 -- Ada 2005 (AI-231):Anonymous access types used in controlling
113 -- parameters exclude null because it is necessary to read the
114 -- tag to dispatch, and null has no tag.
116 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
117 Set_Can_Never_Be_Null
(Etype
(Formal
));
118 Set_Is_Known_Non_Null
(Etype
(Formal
));
121 -- Check that the parameter's nominal subtype statically
122 -- matches the first subtype.
124 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
125 if not Subtypes_Statically_Match
126 (Typ
, Designated_Type
(Etype
(Formal
)))
129 ("parameter subtype does not match controlling type",
133 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
)) then
135 ("parameter subtype does not match controlling type",
139 if Present
(Default_Value
(Formal
)) then
140 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
142 ("default not allowed for controlling access parameter",
143 Default_Value
(Formal
));
145 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
147 ("default expression must be a tag indeterminate" &
148 " function call", Default_Value
(Formal
));
152 elsif Comes_From_Source
(Subp
) then
154 ("operation can be dispatching in only one type", Subp
);
157 -- Verify that the restriction in E.2.2 (14) is obeyed
160 and then Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
163 ("access parameter of remote object primitive"
164 & " must be controlling",
168 Next_Formal
(Formal
);
171 if Present
(Etype
(Subp
)) then
172 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
174 if Present
(Ctrl_Type
) then
175 if Ctrl_Type
= Typ
then
176 Set_Has_Controlling_Result
(Subp
);
178 -- Check that the result subtype statically matches
179 -- the first subtype.
181 if not Subtypes_Statically_Match
(Typ
, Etype
(Subp
)) then
183 ("result subtype does not match controlling type", Subp
);
186 elsif Comes_From_Source
(Subp
) then
188 ("operation can be dispatching in only one type", Subp
);
191 -- The following check is clearly required, although the RM says
192 -- nothing about return types. If the return type is a limited
193 -- class-wide type declared in the current scope, there is no way
194 -- to declare stream procedures for it, so the return cannot be
198 and then Is_Limited_Type
(Typ
)
199 and then Etype
(Subp
) = Class_Wide_Type
(Typ
)
201 Error_Msg_N
("return type has no stream attributes", Subp
);
204 end Check_Controlling_Formals
;
206 ----------------------------
207 -- Check_Controlling_Type --
208 ----------------------------
210 function Check_Controlling_Type
212 Subp
: Entity_Id
) return Entity_Id
214 Tagged_Type
: Entity_Id
:= Empty
;
217 if Is_Tagged_Type
(T
) then
218 if Is_First_Subtype
(T
) then
221 Tagged_Type
:= Base_Type
(T
);
224 elsif Ekind
(T
) = E_Anonymous_Access_Type
225 and then Is_Tagged_Type
(Designated_Type
(T
))
227 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
228 if Is_First_Subtype
(Designated_Type
(T
)) then
229 Tagged_Type
:= Designated_Type
(T
);
231 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
234 -- Ada 2005 (AI-50217)
236 elsif From_With_Type
(Designated_Type
(T
))
237 and then Present
(Non_Limited_View
(Designated_Type
(T
)))
239 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
240 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
242 Tagged_Type
:= Base_Type
(Non_Limited_View
243 (Designated_Type
(T
)));
249 or else Is_Class_Wide_Type
(Tagged_Type
)
253 -- The dispatching type and the primitive operation must be defined
254 -- in the same scope, except in the case of internal operations and
255 -- formal abstract subprograms.
257 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
258 and then (not Is_Generic_Type
(Tagged_Type
)
259 or else not Comes_From_Source
(Subp
)))
261 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract
(Subp
))
263 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
265 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
274 end Check_Controlling_Type
;
276 ----------------------------
277 -- Check_Dispatching_Call --
278 ----------------------------
280 procedure Check_Dispatching_Call
(N
: Node_Id
) is
283 Control
: Node_Id
:= Empty
;
285 Subp_Entity
: Entity_Id
;
286 Loc
: constant Source_Ptr
:= Sloc
(N
);
287 Indeterm_Ancestor_Call
: Boolean := False;
288 Indeterm_Ctrl_Type
: Entity_Id
;
290 procedure Check_Dispatching_Context
;
291 -- If the call is tag-indeterminate and the entity being called is
292 -- abstract, verify that the context is a call that will eventually
293 -- provide a tag for dispatching, or has provided one already.
295 -------------------------------
296 -- Check_Dispatching_Context --
297 -------------------------------
299 procedure Check_Dispatching_Context
is
300 Subp
: constant Entity_Id
:= Entity
(Name
(N
));
304 if Is_Abstract
(Subp
)
305 and then No
(Controlling_Argument
(N
))
307 if Present
(Alias
(Subp
))
308 and then not Is_Abstract
(Alias
(Subp
))
309 and then No
(DTC_Entity
(Subp
))
311 -- Private overriding of inherited abstract operation,
314 Set_Entity
(Name
(N
), Alias
(Subp
));
320 while Present
(Par
) loop
322 if (Nkind
(Par
) = N_Function_Call
or else
323 Nkind
(Par
) = N_Procedure_Call_Statement
or else
324 Nkind
(Par
) = N_Assignment_Statement
or else
325 Nkind
(Par
) = N_Op_Eq
or else
326 Nkind
(Par
) = N_Op_Ne
)
327 and then Is_Tagged_Type
(Etype
(Subp
))
331 elsif Nkind
(Par
) = N_Qualified_Expression
332 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
337 if Ekind
(Subp
) = E_Function
then
339 ("call to abstract function must be dispatching", N
);
341 -- This error can occur for a procedure in the case of a
342 -- call to an abstract formal procedure with a statically
347 ("call to abstract procedure must be dispatching",
356 end Check_Dispatching_Context
;
358 -- Start of processing for Check_Dispatching_Call
361 -- Find a controlling argument, if any
363 if Present
(Parameter_Associations
(N
)) then
364 Actual
:= First_Actual
(N
);
366 Subp_Entity
:= Entity
(Name
(N
));
367 Formal
:= First_Formal
(Subp_Entity
);
369 while Present
(Actual
) loop
370 Control
:= Find_Controlling_Arg
(Actual
);
371 exit when Present
(Control
);
373 -- Check for the case where the actual is a tag-indeterminate call
374 -- whose result type is different than the tagged type associated
375 -- with the containing call, but is an ancestor of the type.
377 if Is_Controlling_Formal
(Formal
)
378 and then Is_Tag_Indeterminate
(Actual
)
379 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
380 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
382 Indeterm_Ancestor_Call
:= True;
383 Indeterm_Ctrl_Type
:= Etype
(Formal
);
386 Next_Actual
(Actual
);
387 Next_Formal
(Formal
);
390 -- If the call doesn't have a controlling actual but does have
391 -- an indeterminate actual that requires dispatching treatment,
392 -- then an object is needed that will serve as the controlling
393 -- argument for a dispatching call on the indeterminate actual.
394 -- This can only occur in the unusual situation of a default
395 -- actual given by a tag-indeterminate call and where the type
396 -- of the call is an ancestor of the type associated with a
397 -- containing call to an inherited operation (see AI-239).
398 -- Rather than create an object of the tagged type, which would
399 -- be problematic for various reasons (default initialization,
400 -- discriminants), the tag of the containing call's associated
401 -- tagged type is directly used to control the dispatching.
404 and then Indeterm_Ancestor_Call
407 Make_Attribute_Reference
(Loc
,
408 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
409 Attribute_Name
=> Name_Tag
);
413 if Present
(Control
) then
415 -- Verify that no controlling arguments are statically tagged
418 Write_Str
("Found Dispatching call");
423 Actual
:= First_Actual
(N
);
425 while Present
(Actual
) loop
426 if Actual
/= Control
then
428 if not Is_Controlling_Actual
(Actual
) then
429 null; -- Can be anything
431 elsif Is_Dynamically_Tagged
(Actual
) then
432 null; -- Valid parameter
434 elsif Is_Tag_Indeterminate
(Actual
) then
436 -- The tag is inherited from the enclosing call (the
437 -- node we are currently analyzing). Explicitly expand
438 -- the actual, since the previous call to Expand
439 -- (from Resolve_Call) had no way of knowing about
440 -- the required dispatching.
442 Propagate_Tag
(Control
, Actual
);
446 ("controlling argument is not dynamically tagged",
452 Next_Actual
(Actual
);
455 -- Mark call as a dispatching call
457 Set_Controlling_Argument
(N
, Control
);
460 -- The call is not dispatching, so check that there aren't any
461 -- tag-indeterminate abstract calls left.
463 Actual
:= First_Actual
(N
);
465 while Present
(Actual
) loop
466 if Is_Tag_Indeterminate
(Actual
) then
468 -- Function call case
470 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
471 Func
:= Entity
(Name
(Original_Node
(Actual
)));
473 -- If the actual is an attribute then it can't be abstract
474 -- (the only current case of a tag-indeterminate attribute
475 -- is the stream Input attribute).
478 Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
482 -- Only other possibility is a qualified expression whose
483 -- consituent expression is itself a call.
489 (Expression
(Original_Node
(Actual
)))));
492 if Present
(Func
) and then Is_Abstract
(Func
) then
494 "call to abstract function must be dispatching", N
);
498 Next_Actual
(Actual
);
501 Check_Dispatching_Context
;
505 -- If dispatching on result, the enclosing call, if any, will
506 -- determine the controlling argument. Otherwise this is the
507 -- primitive operation of the root type.
509 Check_Dispatching_Context
;
511 end Check_Dispatching_Call
;
513 ---------------------------------
514 -- Check_Dispatching_Operation --
515 ---------------------------------
517 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
518 Tagged_Type
: Entity_Id
;
519 Has_Dispatching_Parent
: Boolean := False;
520 Body_Is_Last_Primitive
: Boolean := False;
522 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean;
523 -- Check whether T is derived from a visibly controlled type.
524 -- This is true if the root type is declared in Ada.Finalization.
525 -- If T is derived instead from a private type whose full view
526 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
527 -- does not override the inherited one.
529 ---------------------------
530 -- Is_Visibly_Controlled --
531 ---------------------------
533 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean is
534 Root
: constant Entity_Id
:= Root_Type
(T
);
536 return Chars
(Scope
(Root
)) = Name_Finalization
537 and then Chars
(Scope
(Scope
(Root
))) = Name_Ada
538 and then Scope
(Scope
(Scope
(Root
))) = Standard_Standard
;
539 end Is_Visibly_Controlled
;
541 -- Start of processing for Check_Dispatching_Operation
544 if Ekind
(Subp
) /= E_Procedure
and then Ekind
(Subp
) /= E_Function
then
548 Set_Is_Dispatching_Operation
(Subp
, False);
549 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
553 if Ada_Version
= Ada_05
554 and then Present
(Tagged_Type
)
555 and then Is_Concurrent_Type
(Tagged_Type
)
557 -- Protect the frontend against previously detected errors
559 if No
(Corresponding_Record_Type
(Tagged_Type
)) then
563 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
566 -- If Subp is derived from a dispatching operation then it should
567 -- always be treated as dispatching. In this case various checks
568 -- below will be bypassed. Makes sure that late declarations for
569 -- inherited private subprograms are treated as dispatching, even
570 -- if the associated tagged type is already frozen.
572 Has_Dispatching_Parent
:=
573 Present
(Alias
(Subp
))
574 and then Is_Dispatching_Operation
(Alias
(Subp
));
576 if No
(Tagged_Type
) then
579 -- The subprograms build internally after the freezing point (such as
580 -- the Init procedure) are not primitives
582 elsif Is_Frozen
(Tagged_Type
)
583 and then not Comes_From_Source
(Subp
)
584 and then not Has_Dispatching_Parent
588 -- The operation may be a child unit, whose scope is the defining
589 -- package, but which is not a primitive operation of the type.
591 elsif Is_Child_Unit
(Subp
) then
594 -- If the subprogram is not defined in a package spec, the only case
595 -- where it can be a dispatching op is when it overrides an operation
596 -- before the freezing point of the type.
598 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
599 or else In_Package_Body
(Scope
(Subp
)))
600 and then not Has_Dispatching_Parent
602 if not Comes_From_Source
(Subp
)
603 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
607 -- If the type is already frozen, the overriding is not allowed
608 -- except when Old_Subp is not a dispatching operation (which
609 -- can occur when Old_Subp was inherited by an untagged type).
610 -- However, a body with no previous spec freezes the type "after"
611 -- its declaration, and therefore is a legal overriding (unless
612 -- the type has already been frozen). Only the first such body
615 elsif Present
(Old_Subp
)
616 and then Is_Dispatching_Operation
(Old_Subp
)
618 if Comes_From_Source
(Subp
)
620 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
621 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
624 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
625 Decl_Item
: Node_Id
:= Next
(Parent
(Tagged_Type
));
628 -- ??? The checks here for whether the type has been
629 -- frozen prior to the new body are not complete. It's
630 -- not simple to check frozenness at this point since
631 -- the body has already caused the type to be prematurely
632 -- frozen in Analyze_Declarations, but we're forced to
633 -- recheck this here because of the odd rule interpretation
634 -- that allows the overriding if the type wasn't frozen
635 -- prior to the body. The freezing action should probably
636 -- be delayed until after the spec is seen, but that's
637 -- a tricky change to the delicate freezing code.
639 -- Look at each declaration following the type up
640 -- until the new subprogram body. If any of the
641 -- declarations is a body then the type has been
642 -- frozen already so the overriding primitive is
645 while Present
(Decl_Item
)
646 and then (Decl_Item
/= Subp_Body
)
648 if Comes_From_Source
(Decl_Item
)
649 and then (Nkind
(Decl_Item
) in N_Proper_Body
650 or else Nkind
(Decl_Item
) in N_Body_Stub
)
652 Error_Msg_N
("overriding of& is too late!", Subp
);
654 ("\spec should appear immediately after the type!",
662 -- If the subprogram doesn't follow in the list of
663 -- declarations including the type then the type
664 -- has definitely been frozen already and the body
667 if No
(Decl_Item
) then
668 Error_Msg_N
("overriding of& is too late!", Subp
);
670 ("\spec should appear immediately after the type!",
673 elsif Is_Frozen
(Subp
) then
675 -- The subprogram body declares a primitive operation.
676 -- if the subprogram is already frozen, we must update
677 -- its dispatching information explicitly here. The
678 -- information is taken from the overridden subprogram.
680 Body_Is_Last_Primitive
:= True;
682 if Present
(DTC_Entity
(Old_Subp
)) then
683 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
684 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
686 if not Restriction_Active
(No_Dispatching_Calls
) then
687 Insert_After
(Subp_Body
,
688 Fill_DT_Entry
(Sloc
(Subp_Body
), Subp
));
695 Error_Msg_N
("overriding of& is too late!", Subp
);
697 ("\subprogram spec should appear immediately after the type!",
701 -- If the type is not frozen yet and we are not in the overridding
702 -- case it looks suspiciously like an attempt to define a primitive
705 elsif not Is_Frozen
(Tagged_Type
) then
707 ("?not dispatching (must be defined in a package spec)", Subp
);
710 -- When the type is frozen, it is legitimate to define a new
711 -- non-primitive operation.
717 -- Now, we are sure that the scope is a package spec. If the subprogram
718 -- is declared after the freezing point ot the type that's an error
720 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
721 Error_Msg_N
("this primitive operation is declared too late", Subp
);
723 ("?no primitive operations for& after this line",
724 Freeze_Node
(Tagged_Type
),
729 Check_Controlling_Formals
(Tagged_Type
, Subp
);
731 -- Now it should be a correct primitive operation, put it in the list
733 if Present
(Old_Subp
) then
734 Check_Subtype_Conformant
(Subp
, Old_Subp
);
735 if (Chars
(Subp
) = Name_Initialize
736 or else Chars
(Subp
) = Name_Adjust
737 or else Chars
(Subp
) = Name_Finalize
)
738 and then Is_Controlled
(Tagged_Type
)
739 and then not Is_Visibly_Controlled
(Tagged_Type
)
741 Set_Is_Overriding_Operation
(Subp
, False);
743 ("operation does not override inherited&?", Subp
, Subp
);
745 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
746 Set_Is_Overriding_Operation
(Subp
);
749 -- If no old subprogram, then we add this as a dispatching operation,
750 -- but we avoid doing this if an error was posted, to prevent annoying
753 elsif not Error_Posted
(Subp
) then
754 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
757 Set_Is_Dispatching_Operation
(Subp
, True);
759 if not Body_Is_Last_Primitive
then
760 Set_DT_Position
(Subp
, No_Uint
);
762 elsif Has_Controlled_Component
(Tagged_Type
)
764 (Chars
(Subp
) = Name_Initialize
765 or else Chars
(Subp
) = Name_Adjust
766 or else Chars
(Subp
) = Name_Finalize
)
769 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
773 Old_Spec
: Entity_Id
;
775 C_Names
: constant array (1 .. 3) of Name_Id
:=
780 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
781 (TSS_Deep_Initialize
,
786 -- Remove previous controlled function, which was constructed
787 -- and analyzed when the type was frozen. This requires
788 -- removing the body of the redefined primitive, as well as
789 -- its specification if needed (there is no spec created for
790 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
791 -- the exception information that may have been generated for
792 -- it when front end zero-cost tables are enabled.
794 for J
in D_Names
'Range loop
795 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
798 and then Chars
(Subp
) = C_Names
(J
)
800 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
802 Set_Is_Eliminated
(Old_P
);
803 Set_Scope
(Old_P
, Scope
(Current_Scope
));
805 if Nkind
(Old_Bod
) = N_Subprogram_Body
806 and then Present
(Corresponding_Spec
(Old_Bod
))
808 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
809 Set_Has_Completion
(Old_Spec
, False);
814 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
816 -- The new operation is added to the actions of the freeze
817 -- node for the type, but this node has already been analyzed,
818 -- so we must retrieve and analyze explicitly the one new body,
821 and then Present
(Actions
(F_Node
))
823 Decl
:= Last
(Actions
(F_Node
));
828 end Check_Dispatching_Operation
;
830 ------------------------------------------
831 -- Check_Operation_From_Incomplete_Type --
832 ------------------------------------------
834 procedure Check_Operation_From_Incomplete_Type
838 Full
: constant Entity_Id
:= Full_View
(Typ
);
839 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
840 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
841 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
843 Prev
: Elmt_Id
:= No_Elmt
;
845 function Derives_From
(Proc
: Entity_Id
) return Boolean;
846 -- Check that Subp has the signature of an operation derived from Proc.
847 -- Subp has an access parameter that designates Typ.
853 function Derives_From
(Proc
: Entity_Id
) return Boolean is
857 if Chars
(Proc
) /= Chars
(Subp
) then
861 F1
:= First_Formal
(Proc
);
862 F2
:= First_Formal
(Subp
);
864 while Present
(F1
) and then Present
(F2
) loop
866 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
868 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
871 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
872 and then Designated_Type
(Etype
(F2
)) /= Full
877 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
880 elsif Etype
(F1
) /= Etype
(F2
) then
888 return No
(F1
) and then No
(F2
);
891 -- Start of processing for Check_Operation_From_Incomplete_Type
894 -- The operation may override an inherited one, or may be a new one
895 -- altogether. The inherited operation will have been hidden by the
896 -- current one at the point of the type derivation, so it does not
897 -- appear in the list of primitive operations of the type. We have to
898 -- find the proper place of insertion in the list of primitive opera-
899 -- tions by iterating over the list for the parent type.
901 Op1
:= First_Elmt
(Old_Prim
);
902 Op2
:= First_Elmt
(New_Prim
);
904 while Present
(Op1
) and then Present
(Op2
) loop
906 if Derives_From
(Node
(Op1
)) then
909 Prepend_Elmt
(Subp
, New_Prim
);
911 Insert_Elmt_After
(Subp
, Prev
);
922 -- Operation is a new primitive
924 Append_Elmt
(Subp
, New_Prim
);
925 end Check_Operation_From_Incomplete_Type
;
927 ---------------------------------------
928 -- Check_Operation_From_Private_View --
929 ---------------------------------------
931 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
932 Tagged_Type
: Entity_Id
;
935 if Is_Dispatching_Operation
(Alias
(Subp
)) then
936 Set_Scope
(Subp
, Current_Scope
);
937 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
939 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
940 Append_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
942 -- If Old_Subp isn't already marked as dispatching then
943 -- this is the case of an operation of an untagged private
944 -- type fulfilled by a tagged type that overrides an
945 -- inherited dispatching operation, so we set the necessary
946 -- dispatching attributes here.
948 if not Is_Dispatching_Operation
(Old_Subp
) then
950 -- If the untagged type has no discriminants, and the full
951 -- view is constrained, there will be a spurious mismatch
952 -- of subtypes on the controlling arguments, because the tagged
953 -- type is the internal base type introduced in the derivation.
954 -- Use the original type to verify conformance, rather than the
957 if not Comes_From_Source
(Tagged_Type
)
958 and then Has_Discriminants
(Tagged_Type
)
963 Formal
:= First_Formal
(Old_Subp
);
964 while Present
(Formal
) loop
965 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
966 Tagged_Type
:= Etype
(Formal
);
969 Next_Formal
(Formal
);
973 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
974 Tagged_Type
:= Etype
(Old_Subp
);
978 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
979 Set_Is_Dispatching_Operation
(Old_Subp
, True);
980 Set_DT_Position
(Old_Subp
, No_Uint
);
983 -- If the old subprogram is an explicit renaming of some other
984 -- entity, it is not overridden by the inherited subprogram.
985 -- Otherwise, update its alias and other attributes.
987 if Present
(Alias
(Old_Subp
))
988 and then Nkind
(Unit_Declaration_Node
(Old_Subp
))
989 /= N_Subprogram_Renaming_Declaration
991 Set_Alias
(Old_Subp
, Alias
(Subp
));
993 -- The derived subprogram should inherit the abstractness
994 -- of the parent subprogram (except in the case of a function
995 -- returning the type). This sets the abstractness properly
996 -- for cases where a private extension may have inherited
997 -- an abstract operation, but the full type is derived from
998 -- a descendant type and inherits a nonabstract version.
1000 if Etype
(Subp
) /= Tagged_Type
then
1001 Set_Is_Abstract
(Old_Subp
, Is_Abstract
(Alias
(Subp
)));
1006 end Check_Operation_From_Private_View
;
1008 --------------------------
1009 -- Find_Controlling_Arg --
1010 --------------------------
1012 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1013 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1017 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1018 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1021 -- Dispatching on result case
1023 if Nkind
(Orig_Node
) = N_Function_Call
1024 and then Present
(Controlling_Argument
(Orig_Node
))
1025 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1027 return Controlling_Argument
(Orig_Node
);
1031 elsif Is_Controlling_Actual
(N
)
1033 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1034 and then Is_Controlling_Actual
(Parent
(N
)))
1038 if Is_Access_Type
(Typ
) then
1039 -- In the case of an Access attribute, use the type of
1040 -- the prefix, since in the case of an actual for an
1041 -- access parameter, the attribute's type may be of a
1042 -- specific designated type, even though the prefix
1043 -- type is class-wide.
1045 if Nkind
(N
) = N_Attribute_Reference
then
1046 Typ
:= Etype
(Prefix
(N
));
1048 -- An allocator is dispatching if the type of qualified
1049 -- expression is class_wide, in which case this is the
1050 -- controlling type.
1052 elsif Nkind
(Orig_Node
) = N_Allocator
1053 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1055 Typ
:= Etype
(Expression
(Orig_Node
));
1058 Typ
:= Designated_Type
(Typ
);
1062 if Is_Class_Wide_Type
(Typ
)
1064 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1065 and then Is_Access_Type
(Etype
(N
))
1066 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1073 end Find_Controlling_Arg
;
1075 ---------------------------
1076 -- Find_Dispatching_Type --
1077 ---------------------------
1079 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1081 Ctrl_Type
: Entity_Id
;
1084 if Present
(DTC_Entity
(Subp
)) then
1085 return Scope
(DTC_Entity
(Subp
));
1088 Formal
:= First_Formal
(Subp
);
1089 while Present
(Formal
) loop
1090 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1092 if Present
(Ctrl_Type
) then
1096 Next_Formal
(Formal
);
1099 -- The subprogram may also be dispatching on result
1101 if Present
(Etype
(Subp
)) then
1102 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
1104 if Present
(Ctrl_Type
) then
1111 end Find_Dispatching_Type
;
1113 ---------------------------
1114 -- Is_Dynamically_Tagged --
1115 ---------------------------
1117 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1119 return Find_Controlling_Arg
(N
) /= Empty
;
1120 end Is_Dynamically_Tagged
;
1122 --------------------------
1123 -- Is_Tag_Indeterminate --
1124 --------------------------
1126 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1129 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1132 if Nkind
(Orig_Node
) = N_Function_Call
1133 and then Is_Entity_Name
(Name
(Orig_Node
))
1135 Nam
:= Entity
(Name
(Orig_Node
));
1137 if not Has_Controlling_Result
(Nam
) then
1140 -- An explicit dereference means that the call has already been
1141 -- expanded and there is no tag to propagate.
1143 elsif Nkind
(N
) = N_Explicit_Dereference
then
1146 -- If there are no actuals, the call is tag-indeterminate
1148 elsif No
(Parameter_Associations
(Orig_Node
)) then
1152 Actual
:= First_Actual
(Orig_Node
);
1153 while Present
(Actual
) loop
1154 if Is_Controlling_Actual
(Actual
)
1155 and then not Is_Tag_Indeterminate
(Actual
)
1157 return False; -- one operand is dispatching
1160 Next_Actual
(Actual
);
1166 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
1167 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
1169 -- Case of a call to the Input attribute (possibly rewritten), which is
1170 -- always tag-indeterminate except when its prefix is a Class attribute.
1172 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
1174 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
1176 Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
1182 end Is_Tag_Indeterminate
;
1184 ------------------------------------
1185 -- Override_Dispatching_Operation --
1186 ------------------------------------
1188 procedure Override_Dispatching_Operation
1189 (Tagged_Type
: Entity_Id
;
1190 Prev_Op
: Entity_Id
;
1193 Op_Elmt
: Elmt_Id
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1198 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean;
1199 -- Traverse the list of aliased entities to check if the overriden
1200 -- entity corresponds with a primitive operation of an abstract
1203 -----------------------------
1204 -- Is_Interface_Subprogram --
1205 -----------------------------
1207 function Is_Interface_Subprogram
(Op
: Entity_Id
) return Boolean is
1212 while Present
(Alias
(Aux
))
1213 and then Present
(DTC_Entity
(Alias
(Aux
)))
1215 if Is_Interface
(Scope
(DTC_Entity
(Alias
(Aux
)))) then
1222 end Is_Interface_Subprogram
;
1224 -- Start of processing for Override_Dispatching_Operation
1227 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1228 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1230 if No_Return
(Prev_Op
) and then not No_Return
(New_Op
) then
1231 Error_Msg_N
("procedure & must have No_Return pragma", New_Op
);
1232 Error_Msg_N
("\since overridden procedure has No_Return", New_Op
);
1235 -- Patch the primitive operation list
1237 while Present
(Op_Elmt
)
1238 and then Node
(Op_Elmt
) /= Prev_Op
1240 Next_Elmt
(Op_Elmt
);
1243 -- If there is no previous operation to override, the type declaration
1244 -- was malformed, and an error must have been emitted already.
1246 if No
(Op_Elmt
) then
1250 -- Ada 2005 (AI-251): Do not replace subprograms inherited from
1251 -- abstract interfaces. They will be used later to generate the
1252 -- corresponding thunks to initialize the Vtable (see subprogram
1253 -- Freeze_Subprogram). The inherited operation itself must also
1254 -- become hidden, to avoid spurious ambiguities; name resolution
1255 -- must pick up only the operation that implements it,
1257 if Is_Interface_Subprogram
(Prev_Op
) then
1258 Set_DT_Position
(Prev_Op
, DT_Position
(Alias
(Prev_Op
)));
1259 Set_Is_Abstract
(Prev_Op
, Is_Abstract
(New_Op
));
1260 Set_Is_Overriding_Operation
(Prev_Op
);
1262 -- Traverse the list of aliased entities to look for the overriden
1263 -- abstract interface subprogram.
1265 E
:= Alias
(Prev_Op
);
1266 while Present
(Alias
(E
))
1267 and then Present
(DTC_Entity
(E
))
1268 and then not (Is_Abstract
(E
))
1269 and then not Is_Interface
(Scope
(DTC_Entity
(E
)))
1274 Set_Abstract_Interface_Alias
(Prev_Op
, E
);
1275 Set_Alias
(Prev_Op
, New_Op
);
1276 Set_Is_Internal
(Prev_Op
);
1277 Set_Is_Hidden
(Prev_Op
);
1279 -- Override predefined primitive operations
1281 if Is_Predefined_Dispatching_Operation
(Prev_Op
) then
1282 Replace_Elmt
(Op_Elmt
, New_Op
);
1286 -- Check if this primitive operation was previously added for another
1289 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1291 while Present
(Elmt
) loop
1292 if Node
(Elmt
) = New_Op
then
1301 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
1307 Replace_Elmt
(Op_Elmt
, New_Op
);
1310 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
1311 or else not In_Private_Part
(Current_Scope
)
1313 -- Not a private primitive
1317 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1319 -- Make the overriding operation into an alias of the implicit one.
1320 -- In this fashion a call from outside ends up calling the new body
1321 -- even if non-dispatching, and a call from inside calls the
1322 -- overriding operation because it hides the implicit one. To
1323 -- indicate that the body of Prev_Op is never called, set its
1324 -- dispatch table entity to Empty.
1326 Set_Alias
(Prev_Op
, New_Op
);
1327 Set_DTC_Entity
(Prev_Op
, Empty
);
1330 end Override_Dispatching_Operation
;
1336 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1337 Call_Node
: Node_Id
;
1341 if Nkind
(Actual
) = N_Function_Call
then
1342 Call_Node
:= Actual
;
1344 elsif Nkind
(Actual
) = N_Identifier
1345 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1347 -- Call rewritten as object declaration when stack-checking
1348 -- is enabled. Propagate tag to expression in declaration, which
1349 -- is original call.
1351 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1353 -- Only other possibilities are parenthesized or qualified expression,
1354 -- or an expander-generated unchecked conversion of a function call to
1355 -- a stream Input attribute.
1358 Call_Node
:= Expression
(Actual
);
1361 -- Do not set the Controlling_Argument if already set. This happens
1362 -- in the special case of _Input (see Exp_Attr, case Input).
1364 if No
(Controlling_Argument
(Call_Node
)) then
1365 Set_Controlling_Argument
(Call_Node
, Control
);
1368 Arg
:= First_Actual
(Call_Node
);
1370 while Present
(Arg
) loop
1371 if Is_Tag_Indeterminate
(Arg
) then
1372 Propagate_Tag
(Control
, Arg
);
1378 -- Expansion of dispatching calls is suppressed when Java_VM, because
1379 -- the JVM back end directly handles the generation of dispatching
1380 -- calls and would have to undo any expansion to an indirect call.
1383 Expand_Dispatching_Call
(Call_Node
);