1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Elists
; use Elists
;
29 with Einfo
; use Einfo
;
30 with Exp_Disp
; use Exp_Disp
;
31 with Exp_Ch7
; use Exp_Ch7
;
32 with Exp_Tss
; use Exp_Tss
;
33 with Errout
; use Errout
;
34 with Namet
; use Namet
;
35 with Nlists
; use Nlists
;
36 with Nmake
; use Nmake
;
38 with Output
; use Output
;
39 with Restrict
; use Restrict
;
40 with Rident
; use Rident
;
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 Targparm
; use Targparm
;
50 with Tbuild
; use Tbuild
;
51 with Uintp
; use Uintp
;
53 package body Sem_Disp
is
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 procedure Add_Dispatching_Operation
60 (Tagged_Type
: Entity_Id
;
62 -- Add New_Op in the list of primitive operations of Tagged_Type
64 function Check_Controlling_Type
66 Subp
: Entity_Id
) return Entity_Id
;
67 -- T is the tagged type of a formal parameter or the result of Subp.
68 -- If the subprogram has a controlling parameter or result that matches
69 -- the type, then returns the tagged type of that parameter or result
70 -- (returning the designated tagged type in the case of an access
71 -- parameter); otherwise returns empty.
73 -------------------------------
74 -- Add_Dispatching_Operation --
75 -------------------------------
77 procedure Add_Dispatching_Operation
78 (Tagged_Type
: Entity_Id
;
81 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
84 -- The dispatching operation may already be on the list, if it the
85 -- wrapper for an inherited function of a null extension (see exp_ch3
86 -- for the construction of function wrappers). The list of primitive
87 -- operations must not contain duplicates.
89 Append_Unique_Elmt
(New_Op
, List
);
90 end Add_Dispatching_Operation
;
92 -------------------------------
93 -- Check_Controlling_Formals --
94 -------------------------------
96 procedure Check_Controlling_Formals
101 Ctrl_Type
: Entity_Id
;
104 Formal
:= First_Formal
(Subp
);
106 while Present
(Formal
) loop
107 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
109 if Present
(Ctrl_Type
) then
111 -- When the controlling type is concurrent and declared within a
112 -- generic or inside an instance, use its corresponding record
115 if Is_Concurrent_Type
(Ctrl_Type
)
116 and then Present
(Corresponding_Record_Type
(Ctrl_Type
))
118 Ctrl_Type
:= Corresponding_Record_Type
(Ctrl_Type
);
121 if Ctrl_Type
= Typ
then
122 Set_Is_Controlling_Formal
(Formal
);
124 -- Ada 2005 (AI-231): Anonymous access types used in
125 -- controlling parameters exclude null because it is necessary
126 -- to read the tag to dispatch, and null has no tag.
128 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
129 Set_Can_Never_Be_Null
(Etype
(Formal
));
130 Set_Is_Known_Non_Null
(Etype
(Formal
));
133 -- Check that the parameter's nominal subtype statically
134 -- matches the first subtype.
136 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
137 if not Subtypes_Statically_Match
138 (Typ
, Designated_Type
(Etype
(Formal
)))
141 ("parameter subtype does not match controlling type",
145 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
)) then
147 ("parameter subtype does not match controlling type",
151 if Present
(Default_Value
(Formal
)) then
153 -- In Ada 2005, access parameters can have defaults
155 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
156 and then Ada_Version
< Ada_05
159 ("default not allowed for controlling access parameter",
160 Default_Value
(Formal
));
162 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
164 ("default expression must be a tag indeterminate" &
165 " function call", Default_Value
(Formal
));
169 elsif Comes_From_Source
(Subp
) then
171 ("operation can be dispatching in only one type", Subp
);
175 Next_Formal
(Formal
);
178 if Present
(Etype
(Subp
)) then
179 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
181 if Present
(Ctrl_Type
) then
182 if Ctrl_Type
= Typ
then
183 Set_Has_Controlling_Result
(Subp
);
185 -- Check that result subtype statically matches first subtype
186 -- (Ada 2005) : Subp may have a controlling access result.
188 if Subtypes_Statically_Match
(Typ
, Etype
(Subp
))
189 or else (Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
191 Subtypes_Statically_Match
192 (Typ
, Designated_Type
(Etype
(Subp
))))
198 ("result subtype does not match controlling type", Subp
);
201 elsif Comes_From_Source
(Subp
) then
203 ("operation can be dispatching in only one type", Subp
);
207 end Check_Controlling_Formals
;
209 ----------------------------
210 -- Check_Controlling_Type --
211 ----------------------------
213 function Check_Controlling_Type
215 Subp
: Entity_Id
) return Entity_Id
217 Tagged_Type
: Entity_Id
:= Empty
;
220 if Is_Tagged_Type
(T
) then
221 if Is_First_Subtype
(T
) then
224 Tagged_Type
:= Base_Type
(T
);
227 elsif Ekind
(T
) = E_Anonymous_Access_Type
228 and then Is_Tagged_Type
(Designated_Type
(T
))
230 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
231 if Is_First_Subtype
(Designated_Type
(T
)) then
232 Tagged_Type
:= Designated_Type
(T
);
234 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
237 -- Ada 2005 : an incomplete type can be tagged. An operation with
238 -- an access parameter of the type is dispatching.
240 elsif Scope
(Designated_Type
(T
)) = Current_Scope
then
241 Tagged_Type
:= Designated_Type
(T
);
243 -- Ada 2005 (AI-50217)
245 elsif From_With_Type
(Designated_Type
(T
))
246 and then Present
(Non_Limited_View
(Designated_Type
(T
)))
248 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
249 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
251 Tagged_Type
:= Base_Type
(Non_Limited_View
252 (Designated_Type
(T
)));
258 or else Is_Class_Wide_Type
(Tagged_Type
)
262 -- The dispatching type and the primitive operation must be defined
263 -- in the same scope, except in the case of internal operations and
264 -- formal abstract subprograms.
266 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
267 and then (not Is_Generic_Type
(Tagged_Type
)
268 or else not Comes_From_Source
(Subp
)))
270 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
272 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
274 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
276 Is_Abstract_Subprogram
(Subp
))
283 end Check_Controlling_Type
;
285 ----------------------------
286 -- Check_Dispatching_Call --
287 ----------------------------
289 procedure Check_Dispatching_Call
(N
: Node_Id
) is
290 Loc
: constant Source_Ptr
:= Sloc
(N
);
293 Control
: Node_Id
:= Empty
;
295 Subp_Entity
: Entity_Id
;
296 Indeterm_Ancestor_Call
: Boolean := False;
297 Indeterm_Ctrl_Type
: Entity_Id
;
299 Static_Tag
: Node_Id
:= Empty
;
300 -- If a controlling formal has a statically tagged actual, the tag of
301 -- this actual is to be used for any tag-indeterminate actual
303 procedure Check_Dispatching_Context
;
304 -- If the call is tag-indeterminate and the entity being called is
305 -- abstract, verify that the context is a call that will eventually
306 -- provide a tag for dispatching, or has provided one already.
308 -------------------------------
309 -- Check_Dispatching_Context --
310 -------------------------------
312 procedure Check_Dispatching_Context
is
313 Subp
: constant Entity_Id
:= Entity
(Name
(N
));
317 if Is_Abstract_Subprogram
(Subp
)
318 and then No
(Controlling_Argument
(N
))
320 if Present
(Alias
(Subp
))
321 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
322 and then No
(DTC_Entity
(Subp
))
324 -- Private overriding of inherited abstract operation,
327 Set_Entity
(Name
(N
), Alias
(Subp
));
333 while Present
(Par
) loop
335 if (Nkind
(Par
) = N_Function_Call
or else
336 Nkind
(Par
) = N_Procedure_Call_Statement
or else
337 Nkind
(Par
) = N_Assignment_Statement
or else
338 Nkind
(Par
) = N_Op_Eq
or else
339 Nkind
(Par
) = N_Op_Ne
)
340 and then Is_Tagged_Type
(Etype
(Subp
))
344 elsif Nkind
(Par
) = N_Qualified_Expression
345 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
350 if Ekind
(Subp
) = E_Function
then
352 ("call to abstract function must be dispatching", N
);
354 -- This error can occur for a procedure in the case of a
355 -- call to an abstract formal procedure with a statically
360 ("call to abstract procedure must be dispatching",
369 end Check_Dispatching_Context
;
371 -- Start of processing for Check_Dispatching_Call
374 -- Find a controlling argument, if any
376 if Present
(Parameter_Associations
(N
)) then
377 Actual
:= First_Actual
(N
);
379 Subp_Entity
:= Entity
(Name
(N
));
380 Formal
:= First_Formal
(Subp_Entity
);
382 while Present
(Actual
) loop
383 Control
:= Find_Controlling_Arg
(Actual
);
384 exit when Present
(Control
);
386 -- Check for the case where the actual is a tag-indeterminate call
387 -- whose result type is different than the tagged type associated
388 -- with the containing call, but is an ancestor of the type.
390 if Is_Controlling_Formal
(Formal
)
391 and then Is_Tag_Indeterminate
(Actual
)
392 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
393 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
395 Indeterm_Ancestor_Call
:= True;
396 Indeterm_Ctrl_Type
:= Etype
(Formal
);
398 -- If the formal is controlling but the actual is not, the type
399 -- of the actual is statically known, and may be used as the
400 -- controlling tag for some other-indeterminate actual.
402 elsif Is_Controlling_Formal
(Formal
)
403 and then Is_Entity_Name
(Actual
)
404 and then Is_Tagged_Type
(Etype
(Actual
))
406 Static_Tag
:= Actual
;
409 Next_Actual
(Actual
);
410 Next_Formal
(Formal
);
413 -- If the call doesn't have a controlling actual but does have
414 -- an indeterminate actual that requires dispatching treatment,
415 -- then an object is needed that will serve as the controlling
416 -- argument for a dispatching call on the indeterminate actual.
417 -- This can only occur in the unusual situation of a default
418 -- actual given by a tag-indeterminate call and where the type
419 -- of the call is an ancestor of the type associated with a
420 -- containing call to an inherited operation (see AI-239).
421 -- Rather than create an object of the tagged type, which would
422 -- be problematic for various reasons (default initialization,
423 -- discriminants), the tag of the containing call's associated
424 -- tagged type is directly used to control the dispatching.
427 and then Indeterm_Ancestor_Call
428 and then No
(Static_Tag
)
431 Make_Attribute_Reference
(Loc
,
432 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
433 Attribute_Name
=> Name_Tag
);
438 if Present
(Control
) then
440 -- Verify that no controlling arguments are statically tagged
443 Write_Str
("Found Dispatching call");
448 Actual
:= First_Actual
(N
);
450 while Present
(Actual
) loop
451 if Actual
/= Control
then
453 if not Is_Controlling_Actual
(Actual
) then
454 null; -- Can be anything
456 elsif Is_Dynamically_Tagged
(Actual
) then
457 null; -- Valid parameter
459 elsif Is_Tag_Indeterminate
(Actual
) then
461 -- The tag is inherited from the enclosing call (the
462 -- node we are currently analyzing). Explicitly expand
463 -- the actual, since the previous call to Expand
464 -- (from Resolve_Call) had no way of knowing about
465 -- the required dispatching.
467 Propagate_Tag
(Control
, Actual
);
471 ("controlling argument is not dynamically tagged",
477 Next_Actual
(Actual
);
480 -- Mark call as a dispatching call
482 Set_Controlling_Argument
(N
, Control
);
483 Check_Restriction
(No_Dispatching_Calls
, N
);
485 -- If there is a statically tagged actual and a tag-indeterminate
486 -- call to a function of the ancestor (such as that provided by a
487 -- default), then treat this as a dispatching call and propagate
488 -- the tag to the tag-indeterminate call(s).
490 elsif Present
(Static_Tag
) and then Indeterm_Ancestor_Call
then
492 Make_Attribute_Reference
(Loc
,
494 New_Occurrence_Of
(Etype
(Static_Tag
), Loc
),
495 Attribute_Name
=> Name_Tag
);
499 Actual
:= First_Actual
(N
);
500 Formal
:= First_Formal
(Subp_Entity
);
501 while Present
(Actual
) loop
502 if Is_Tag_Indeterminate
(Actual
)
503 and then Is_Controlling_Formal
(Formal
)
505 Propagate_Tag
(Control
, Actual
);
508 Next_Actual
(Actual
);
509 Next_Formal
(Formal
);
512 Check_Dispatching_Context
;
515 -- The call is not dispatching, so check that there aren't any
516 -- tag-indeterminate abstract calls left.
518 Actual
:= First_Actual
(N
);
519 while Present
(Actual
) loop
520 if Is_Tag_Indeterminate
(Actual
) then
522 -- Function call case
524 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
525 Func
:= Entity
(Name
(Original_Node
(Actual
)));
527 -- If the actual is an attribute then it can't be abstract
528 -- (the only current case of a tag-indeterminate attribute
529 -- is the stream Input attribute).
532 Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
536 -- Only other possibility is a qualified expression whose
537 -- constituent expression is itself a call.
543 (Expression
(Original_Node
(Actual
)))));
546 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
548 "call to abstract function must be dispatching", N
);
552 Next_Actual
(Actual
);
555 Check_Dispatching_Context
;
559 -- If dispatching on result, the enclosing call, if any, will
560 -- determine the controlling argument. Otherwise this is the
561 -- primitive operation of the root type.
563 Check_Dispatching_Context
;
565 end Check_Dispatching_Call
;
567 ---------------------------------
568 -- Check_Dispatching_Operation --
569 ---------------------------------
571 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
572 Tagged_Type
: Entity_Id
;
573 Has_Dispatching_Parent
: Boolean := False;
574 Body_Is_Last_Primitive
: Boolean := False;
576 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean;
577 -- Check whether T is derived from a visibly controlled type.
578 -- This is true if the root type is declared in Ada.Finalization.
579 -- If T is derived instead from a private type whose full view
580 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
581 -- does not override the inherited one.
583 ---------------------------
584 -- Is_Visibly_Controlled --
585 ---------------------------
587 function Is_Visibly_Controlled
(T
: Entity_Id
) return Boolean is
588 Root
: constant Entity_Id
:= Root_Type
(T
);
590 return Chars
(Scope
(Root
)) = Name_Finalization
591 and then Chars
(Scope
(Scope
(Root
))) = Name_Ada
592 and then Scope
(Scope
(Scope
(Root
))) = Standard_Standard
;
593 end Is_Visibly_Controlled
;
595 -- Start of processing for Check_Dispatching_Operation
598 if Ekind
(Subp
) /= E_Procedure
and then Ekind
(Subp
) /= E_Function
then
602 Set_Is_Dispatching_Operation
(Subp
, False);
603 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
607 if Ada_Version
= Ada_05
608 and then Present
(Tagged_Type
)
609 and then Is_Concurrent_Type
(Tagged_Type
)
611 -- Protect the frontend against previously detected errors
613 if No
(Corresponding_Record_Type
(Tagged_Type
)) then
617 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
620 -- If Subp is derived from a dispatching operation then it should
621 -- always be treated as dispatching. In this case various checks
622 -- below will be bypassed. Makes sure that late declarations for
623 -- inherited private subprograms are treated as dispatching, even
624 -- if the associated tagged type is already frozen.
626 Has_Dispatching_Parent
:=
627 Present
(Alias
(Subp
))
628 and then Is_Dispatching_Operation
(Alias
(Subp
));
630 if No
(Tagged_Type
) then
632 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
633 -- with an abstract interface type unless the interface acts as a
634 -- parent type in a derivation. If the interface type is a formal
635 -- type then the operation is not primitive and therefore legal.
642 E
:= First_Entity
(Subp
);
643 while Present
(E
) loop
644 if Is_Access_Type
(Etype
(E
)) then
645 Typ
:= Designated_Type
(Etype
(E
));
650 if Comes_From_Source
(Subp
)
651 and then Is_Interface
(Typ
)
652 and then not Is_Class_Wide_Type
(Typ
)
653 and then not Is_Derived_Type
(Typ
)
654 and then not Is_Generic_Type
(Typ
)
655 and then not In_Instance
657 Error_Msg_N
("?declaration of& is too late!", Subp
);
659 ("\spec should appear immediately after declaration of &!",
667 -- In case of functions check also the result type
669 if Ekind
(Subp
) = E_Function
then
670 if Is_Access_Type
(Etype
(Subp
)) then
671 Typ
:= Designated_Type
(Etype
(Subp
));
676 if not Is_Class_Wide_Type
(Typ
)
677 and then Is_Interface
(Typ
)
678 and then not Is_Derived_Type
(Typ
)
680 Error_Msg_N
("?declaration of& is too late!", Subp
);
682 ("\spec should appear immediately after declaration of &!",
690 -- The subprograms build internally after the freezing point (such as
691 -- the Init procedure) are not primitives
693 elsif Is_Frozen
(Tagged_Type
)
694 and then not Comes_From_Source
(Subp
)
695 and then not Has_Dispatching_Parent
699 -- The operation may be a child unit, whose scope is the defining
700 -- package, but which is not a primitive operation of the type.
702 elsif Is_Child_Unit
(Subp
) then
705 -- If the subprogram is not defined in a package spec, the only case
706 -- where it can be a dispatching op is when it overrides an operation
707 -- before the freezing point of the type.
709 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
710 or else In_Package_Body
(Scope
(Subp
)))
711 and then not Has_Dispatching_Parent
713 if not Comes_From_Source
(Subp
)
714 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
718 -- If the type is already frozen, the overriding is not allowed
719 -- except when Old_Subp is not a dispatching operation (which
720 -- can occur when Old_Subp was inherited by an untagged type).
721 -- However, a body with no previous spec freezes the type "after"
722 -- its declaration, and therefore is a legal overriding (unless
723 -- the type has already been frozen). Only the first such body
726 elsif Present
(Old_Subp
)
727 and then Is_Dispatching_Operation
(Old_Subp
)
729 if Comes_From_Source
(Subp
)
731 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
732 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
735 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
736 Decl_Item
: Node_Id
:= Next
(Parent
(Tagged_Type
));
739 -- ??? The checks here for whether the type has been
740 -- frozen prior to the new body are not complete. It's
741 -- not simple to check frozenness at this point since
742 -- the body has already caused the type to be prematurely
743 -- frozen in Analyze_Declarations, but we're forced to
744 -- recheck this here because of the odd rule interpretation
745 -- that allows the overriding if the type wasn't frozen
746 -- prior to the body. The freezing action should probably
747 -- be delayed until after the spec is seen, but that's
748 -- a tricky change to the delicate freezing code.
750 -- Look at each declaration following the type up
751 -- until the new subprogram body. If any of the
752 -- declarations is a body then the type has been
753 -- frozen already so the overriding primitive is
756 while Present
(Decl_Item
)
757 and then (Decl_Item
/= Subp_Body
)
759 if Comes_From_Source
(Decl_Item
)
760 and then (Nkind
(Decl_Item
) in N_Proper_Body
761 or else Nkind
(Decl_Item
) in N_Body_Stub
)
763 Error_Msg_N
("overriding of& is too late!", Subp
);
765 ("\spec should appear immediately after the type!",
773 -- If the subprogram doesn't follow in the list of
774 -- declarations including the type then the type
775 -- has definitely been frozen already and the body
778 if No
(Decl_Item
) then
779 Error_Msg_N
("overriding of& is too late!", Subp
);
781 ("\spec should appear immediately after the type!",
784 elsif Is_Frozen
(Subp
) then
786 -- The subprogram body declares a primitive operation.
787 -- if the subprogram is already frozen, we must update
788 -- its dispatching information explicitly here. The
789 -- information is taken from the overridden subprogram.
791 Body_Is_Last_Primitive
:= True;
793 if Present
(DTC_Entity
(Old_Subp
)) then
794 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
795 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
797 if not Restriction_Active
(No_Dispatching_Calls
) then
798 if Building_Static_DT
(Tagged_Type
) then
800 -- If the static dispatch table has not been
801 -- built then there is nothing else to do now;
802 -- otherwise we notify that we cannot build the
803 -- static dispatch table.
805 if Has_Dispatch_Table
(Tagged_Type
) then
807 ("overriding of& is too late for building" &
808 " static dispatch tables!", Subp
);
810 ("\spec should appear immediately after" &
815 Register_Primitive
(Sloc
(Subp_Body
),
817 Ins_Nod
=> Subp_Body
);
825 Error_Msg_N
("overriding of& is too late!", Subp
);
827 ("\subprogram spec should appear immediately after the type!",
831 -- If the type is not frozen yet and we are not in the overriding
832 -- case it looks suspiciously like an attempt to define a primitive
835 elsif not Is_Frozen
(Tagged_Type
) then
837 ("?not dispatching (must be defined in a package spec)", Subp
);
840 -- When the type is frozen, it is legitimate to define a new
841 -- non-primitive operation.
847 -- Now, we are sure that the scope is a package spec. If the subprogram
848 -- is declared after the freezing point of the type that's an error
850 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
851 Error_Msg_N
("this primitive operation is declared too late", Subp
);
853 ("?no primitive operations for& after this line",
854 Freeze_Node
(Tagged_Type
),
859 Check_Controlling_Formals
(Tagged_Type
, Subp
);
861 -- Now it should be a correct primitive operation, put it in the list
863 if Present
(Old_Subp
) then
864 Check_Subtype_Conformant
(Subp
, Old_Subp
);
866 if (Chars
(Subp
) = Name_Initialize
867 or else Chars
(Subp
) = Name_Adjust
868 or else Chars
(Subp
) = Name_Finalize
)
869 and then Is_Controlled
(Tagged_Type
)
870 and then not Is_Visibly_Controlled
(Tagged_Type
)
872 Set_Is_Overriding_Operation
(Subp
, False);
874 ("operation does not override inherited&?", Subp
, Subp
);
876 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
877 Set_Is_Overriding_Operation
(Subp
);
879 -- Ada 2005 (AI-251): In case of late overriding of a primitive
880 -- that covers abstract interface subprograms we must register it
881 -- in all the secondary dispatch tables associated with abstract
884 if Body_Is_Last_Primitive
then
886 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
891 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
892 while Present
(Elmt
) loop
895 if Present
(Alias
(Prim
))
896 and then Present
(Abstract_Interface_Alias
(Prim
))
897 and then Alias
(Prim
) = Subp
899 Register_Primitive
(Sloc
(Prim
),
901 Ins_Nod
=> Subp_Body
);
907 -- Redisplay the contents of the updated dispatch table
909 if Debug_Flag_ZZ
then
910 Write_Str
("Late overriding: ");
911 Write_DT
(Tagged_Type
);
917 -- If no old subprogram, then we add this as a dispatching operation,
918 -- but we avoid doing this if an error was posted, to prevent annoying
921 elsif not Error_Posted
(Subp
) then
922 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
925 Set_Is_Dispatching_Operation
(Subp
, True);
927 if not Body_Is_Last_Primitive
then
928 Set_DT_Position
(Subp
, No_Uint
);
930 elsif Has_Controlled_Component
(Tagged_Type
)
932 (Chars
(Subp
) = Name_Initialize
933 or else Chars
(Subp
) = Name_Adjust
934 or else Chars
(Subp
) = Name_Finalize
)
937 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
941 Old_Spec
: Entity_Id
;
943 C_Names
: constant array (1 .. 3) of Name_Id
:=
948 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
949 (TSS_Deep_Initialize
,
954 -- Remove previous controlled function, which was constructed
955 -- and analyzed when the type was frozen. This requires
956 -- removing the body of the redefined primitive, as well as
957 -- its specification if needed (there is no spec created for
958 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
959 -- the exception information that may have been generated for
960 -- it when front end zero-cost tables are enabled.
962 for J
in D_Names
'Range loop
963 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
966 and then Chars
(Subp
) = C_Names
(J
)
968 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
970 Set_Is_Eliminated
(Old_P
);
971 Set_Scope
(Old_P
, Scope
(Current_Scope
));
973 if Nkind
(Old_Bod
) = N_Subprogram_Body
974 and then Present
(Corresponding_Spec
(Old_Bod
))
976 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
977 Set_Has_Completion
(Old_Spec
, False);
982 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
984 -- The new operation is added to the actions of the freeze
985 -- node for the type, but this node has already been analyzed,
986 -- so we must retrieve and analyze explicitly the new body.
989 and then Present
(Actions
(F_Node
))
991 Decl
:= Last
(Actions
(F_Node
));
996 end Check_Dispatching_Operation
;
998 ------------------------------------------
999 -- Check_Operation_From_Incomplete_Type --
1000 ------------------------------------------
1002 procedure Check_Operation_From_Incomplete_Type
1006 Full
: constant Entity_Id
:= Full_View
(Typ
);
1007 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1008 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1009 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1011 Prev
: Elmt_Id
:= No_Elmt
;
1013 function Derives_From
(Proc
: Entity_Id
) return Boolean;
1014 -- Check that Subp has the signature of an operation derived from Proc.
1015 -- Subp has an access parameter that designates Typ.
1021 function Derives_From
(Proc
: Entity_Id
) return Boolean is
1025 if Chars
(Proc
) /= Chars
(Subp
) then
1029 F1
:= First_Formal
(Proc
);
1030 F2
:= First_Formal
(Subp
);
1032 while Present
(F1
) and then Present
(F2
) loop
1034 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
1036 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
1039 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
1040 and then Designated_Type
(Etype
(F2
)) /= Full
1045 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
1048 elsif Etype
(F1
) /= Etype
(F2
) then
1056 return No
(F1
) and then No
(F2
);
1059 -- Start of processing for Check_Operation_From_Incomplete_Type
1062 -- The operation may override an inherited one, or may be a new one
1063 -- altogether. The inherited operation will have been hidden by the
1064 -- current one at the point of the type derivation, so it does not
1065 -- appear in the list of primitive operations of the type. We have to
1066 -- find the proper place of insertion in the list of primitive opera-
1067 -- tions by iterating over the list for the parent type.
1069 Op1
:= First_Elmt
(Old_Prim
);
1070 Op2
:= First_Elmt
(New_Prim
);
1072 while Present
(Op1
) and then Present
(Op2
) loop
1074 if Derives_From
(Node
(Op1
)) then
1077 Prepend_Elmt
(Subp
, New_Prim
);
1079 Insert_Elmt_After
(Subp
, Prev
);
1090 -- Operation is a new primitive
1092 Append_Elmt
(Subp
, New_Prim
);
1093 end Check_Operation_From_Incomplete_Type
;
1095 ---------------------------------------
1096 -- Check_Operation_From_Private_View --
1097 ---------------------------------------
1099 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
1100 Tagged_Type
: Entity_Id
;
1103 if Is_Dispatching_Operation
(Alias
(Subp
)) then
1104 Set_Scope
(Subp
, Current_Scope
);
1105 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1107 -- Add Old_Subp to primitive operations if not already present.
1109 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
1110 Append_Unique_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
1112 -- If Old_Subp isn't already marked as dispatching then
1113 -- this is the case of an operation of an untagged private
1114 -- type fulfilled by a tagged type that overrides an
1115 -- inherited dispatching operation, so we set the necessary
1116 -- dispatching attributes here.
1118 if not Is_Dispatching_Operation
(Old_Subp
) then
1120 -- If the untagged type has no discriminants, and the full
1121 -- view is constrained, there will be a spurious mismatch
1122 -- of subtypes on the controlling arguments, because the tagged
1123 -- type is the internal base type introduced in the derivation.
1124 -- Use the original type to verify conformance, rather than the
1127 if not Comes_From_Source
(Tagged_Type
)
1128 and then Has_Discriminants
(Tagged_Type
)
1133 Formal
:= First_Formal
(Old_Subp
);
1134 while Present
(Formal
) loop
1135 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
1136 Tagged_Type
:= Etype
(Formal
);
1139 Next_Formal
(Formal
);
1143 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
1144 Tagged_Type
:= Etype
(Old_Subp
);
1148 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
1149 Set_Is_Dispatching_Operation
(Old_Subp
, True);
1150 Set_DT_Position
(Old_Subp
, No_Uint
);
1153 -- If the old subprogram is an explicit renaming of some other
1154 -- entity, it is not overridden by the inherited subprogram.
1155 -- Otherwise, update its alias and other attributes.
1157 if Present
(Alias
(Old_Subp
))
1158 and then Nkind
(Unit_Declaration_Node
(Old_Subp
))
1159 /= N_Subprogram_Renaming_Declaration
1161 Set_Alias
(Old_Subp
, Alias
(Subp
));
1163 -- The derived subprogram should inherit the abstractness
1164 -- of the parent subprogram (except in the case of a function
1165 -- returning the type). This sets the abstractness properly
1166 -- for cases where a private extension may have inherited
1167 -- an abstract operation, but the full type is derived from
1168 -- a descendant type and inherits a nonabstract version.
1170 if Etype
(Subp
) /= Tagged_Type
then
1171 Set_Is_Abstract_Subprogram
1172 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
1177 end Check_Operation_From_Private_View
;
1179 --------------------------
1180 -- Find_Controlling_Arg --
1181 --------------------------
1183 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1184 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1188 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1189 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1192 -- Dispatching on result case. If expansion is disabled, the node still
1193 -- has the structure of a function call. However, if the function name
1194 -- is an operator and the call was given in infix form, the original
1195 -- node has no controlling result and we must examine the current node.
1197 if Nkind
(N
) = N_Function_Call
1198 and then Present
(Controlling_Argument
(N
))
1199 and then Has_Controlling_Result
(Entity
(Name
(N
)))
1201 return Controlling_Argument
(N
);
1203 -- If expansion is enabled, the call may have been transformed into
1204 -- an indirect call, and we need to recover the original node.
1206 elsif Nkind
(Orig_Node
) = N_Function_Call
1207 and then Present
(Controlling_Argument
(Orig_Node
))
1208 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1210 return Controlling_Argument
(Orig_Node
);
1214 elsif Is_Controlling_Actual
(N
)
1216 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1217 and then Is_Controlling_Actual
(Parent
(N
)))
1221 if Is_Access_Type
(Typ
) then
1222 -- In the case of an Access attribute, use the type of
1223 -- the prefix, since in the case of an actual for an
1224 -- access parameter, the attribute's type may be of a
1225 -- specific designated type, even though the prefix
1226 -- type is class-wide.
1228 if Nkind
(N
) = N_Attribute_Reference
then
1229 Typ
:= Etype
(Prefix
(N
));
1231 -- An allocator is dispatching if the type of qualified
1232 -- expression is class_wide, in which case this is the
1233 -- controlling type.
1235 elsif Nkind
(Orig_Node
) = N_Allocator
1236 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1238 Typ
:= Etype
(Expression
(Orig_Node
));
1241 Typ
:= Designated_Type
(Typ
);
1245 if Is_Class_Wide_Type
(Typ
)
1247 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1248 and then Is_Access_Type
(Etype
(N
))
1249 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1256 end Find_Controlling_Arg
;
1258 ---------------------------
1259 -- Find_Dispatching_Type --
1260 ---------------------------
1262 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1264 Ctrl_Type
: Entity_Id
;
1267 if Present
(DTC_Entity
(Subp
)) then
1268 return Scope
(DTC_Entity
(Subp
));
1271 Formal
:= First_Formal
(Subp
);
1272 while Present
(Formal
) loop
1273 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1275 if Present
(Ctrl_Type
) then
1279 Next_Formal
(Formal
);
1282 -- The subprogram may also be dispatching on result
1284 if Present
(Etype
(Subp
)) then
1285 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
1287 if Present
(Ctrl_Type
) then
1294 end Find_Dispatching_Type
;
1296 ---------------------------
1297 -- Is_Dynamically_Tagged --
1298 ---------------------------
1300 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1302 if Nkind
(N
) = N_Error
then
1305 return Find_Controlling_Arg
(N
) /= Empty
;
1307 end Is_Dynamically_Tagged
;
1309 --------------------------
1310 -- Is_Tag_Indeterminate --
1311 --------------------------
1313 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1316 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1319 if Nkind
(Orig_Node
) = N_Function_Call
1320 and then Is_Entity_Name
(Name
(Orig_Node
))
1322 Nam
:= Entity
(Name
(Orig_Node
));
1324 if not Has_Controlling_Result
(Nam
) then
1327 -- An explicit dereference means that the call has already been
1328 -- expanded and there is no tag to propagate.
1330 elsif Nkind
(N
) = N_Explicit_Dereference
then
1333 -- If there are no actuals, the call is tag-indeterminate
1335 elsif No
(Parameter_Associations
(Orig_Node
)) then
1339 Actual
:= First_Actual
(Orig_Node
);
1340 while Present
(Actual
) loop
1341 if Is_Controlling_Actual
(Actual
)
1342 and then not Is_Tag_Indeterminate
(Actual
)
1344 return False; -- one operand is dispatching
1347 Next_Actual
(Actual
);
1353 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
1354 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
1356 -- Case of a call to the Input attribute (possibly rewritten), which is
1357 -- always tag-indeterminate except when its prefix is a Class attribute.
1359 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
1361 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
1363 Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
1367 -- In Ada 2005 a function that returns an anonymous access type can
1368 -- dispatching, and the dereference of a call to such a function
1369 -- is also tag-indeterminate.
1371 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
1372 and then Ada_Version
>= Ada_05
1374 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
1379 end Is_Tag_Indeterminate
;
1381 ------------------------------------
1382 -- Override_Dispatching_Operation --
1383 ------------------------------------
1385 procedure Override_Dispatching_Operation
1386 (Tagged_Type
: Entity_Id
;
1387 Prev_Op
: Entity_Id
;
1394 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1395 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1397 if No_Return
(Prev_Op
) and then not No_Return
(New_Op
) then
1398 Error_Msg_N
("procedure & must have No_Return pragma", New_Op
);
1399 Error_Msg_N
("\since overridden procedure has No_Return", New_Op
);
1402 -- If there is no previous operation to override, the type declaration
1403 -- was malformed, and an error must have been emitted already.
1405 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1406 while Present
(Elmt
)
1407 and then Node
(Elmt
) /= Prev_Op
1416 Replace_Elmt
(Elmt
, New_Op
);
1418 if Ada_Version
>= Ada_05
1419 and then Has_Abstract_Interfaces
(Tagged_Type
)
1421 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1422 -- entities of the overridden primitive to reference New_Op, and also
1423 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1424 -- that the new operation is subtype conformant with the interface
1425 -- operations that it implements (for operations inherited from the
1426 -- parent itself, this check is made when building the derived type).
1428 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1429 while Present
(Elmt
) loop
1430 Prim
:= Node
(Elmt
);
1432 if Prim
= New_Op
then
1435 -- Note: The check on Is_Subprogram protects the frontend against
1436 -- reading attributes in entities that are not yet fully decorated
1438 elsif Is_Subprogram
(Prim
)
1439 and then Present
(Abstract_Interface_Alias
(Prim
))
1440 and then Alias
(Prim
) = Prev_Op
1441 and then Present
(Etype
(New_Op
))
1443 Set_Alias
(Prim
, New_Op
);
1444 Check_Subtype_Conformant
(New_Op
, Prim
);
1445 Set_Is_Abstract_Subprogram
1446 (Prim
, Is_Abstract_Subprogram
(New_Op
));
1448 -- Ensure that this entity will be expanded to fill the
1449 -- corresponding entry in its dispatch table.
1451 if not Is_Abstract_Subprogram
(Prim
) then
1452 Set_Has_Delayed_Freeze
(Prim
);
1460 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
1461 or else not In_Private_Part
(Current_Scope
)
1463 -- Not a private primitive
1467 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1469 -- Make the overriding operation into an alias of the implicit one.
1470 -- In this fashion a call from outside ends up calling the new body
1471 -- even if non-dispatching, and a call from inside calls the
1472 -- overriding operation because it hides the implicit one. To
1473 -- indicate that the body of Prev_Op is never called, set its
1474 -- dispatch table entity to Empty.
1476 Set_Alias
(Prev_Op
, New_Op
);
1477 Set_DTC_Entity
(Prev_Op
, Empty
);
1480 end Override_Dispatching_Operation
;
1486 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1487 Call_Node
: Node_Id
;
1491 if Nkind
(Actual
) = N_Function_Call
then
1492 Call_Node
:= Actual
;
1494 elsif Nkind
(Actual
) = N_Identifier
1495 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1497 -- Call rewritten as object declaration when stack-checking
1498 -- is enabled. Propagate tag to expression in declaration, which
1499 -- is original call.
1501 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1503 -- Ada 2005: If this is a dereference of a call to a function with a
1504 -- dispatching access-result, the tag is propagated when the dereference
1505 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1507 elsif Nkind
(Actual
) = N_Explicit_Dereference
1508 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
1512 -- Only other possibilities are parenthesized or qualified expression,
1513 -- or an expander-generated unchecked conversion of a function call to
1514 -- a stream Input attribute.
1517 Call_Node
:= Expression
(Actual
);
1520 -- Do not set the Controlling_Argument if already set. This happens
1521 -- in the special case of _Input (see Exp_Attr, case Input).
1523 if No
(Controlling_Argument
(Call_Node
)) then
1524 Set_Controlling_Argument
(Call_Node
, Control
);
1527 Arg
:= First_Actual
(Call_Node
);
1529 while Present
(Arg
) loop
1530 if Is_Tag_Indeterminate
(Arg
) then
1531 Propagate_Tag
(Control
, Arg
);
1537 -- Expansion of dispatching calls is suppressed when VM_Target, because
1538 -- the VM back-ends directly handle the generation of dispatching
1539 -- calls and would have to undo any expansion to an indirect call.
1541 if VM_Target
= No_VM
then
1542 Expand_Dispatching_Call
(Call_Node
);