1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Util
; use Exp_Util
;
32 with Exp_Ch7
; use Exp_Ch7
;
33 with Exp_Tss
; use Exp_Tss
;
34 with Errout
; use Errout
;
35 with Lib
.Xref
; use Lib
.Xref
;
36 with Namet
; use Namet
;
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_Aux
; use Sem_Aux
;
45 with Sem_Ch3
; use Sem_Ch3
;
46 with Sem_Ch6
; use Sem_Ch6
;
47 with Sem_Eval
; use Sem_Eval
;
48 with Sem_Type
; use Sem_Type
;
49 with Sem_Util
; use Sem_Util
;
50 with Snames
; use Snames
;
51 with Sinfo
; use Sinfo
;
52 with Tbuild
; use Tbuild
;
53 with Uintp
; use Uintp
;
55 package body Sem_Disp
is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Add_Dispatching_Operation
62 (Tagged_Type
: Entity_Id
;
64 -- Add New_Op in the list of primitive operations of Tagged_Type
66 function Check_Controlling_Type
68 Subp
: Entity_Id
) return Entity_Id
;
69 -- T is the tagged type of a formal parameter or the result of Subp.
70 -- If the subprogram has a controlling parameter or result that matches
71 -- the type, then returns the tagged type of that parameter or result
72 -- (returning the designated tagged type in the case of an access
73 -- parameter); otherwise returns empty.
75 -------------------------------
76 -- Add_Dispatching_Operation --
77 -------------------------------
79 procedure Add_Dispatching_Operation
80 (Tagged_Type
: Entity_Id
;
83 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
86 -- The dispatching operation may already be on the list, if it is the
87 -- wrapper for an inherited function of a null extension (see Exp_Ch3
88 -- for the construction of function wrappers). The list of primitive
89 -- operations must not contain duplicates.
91 Append_Unique_Elmt
(New_Op
, List
);
92 end Add_Dispatching_Operation
;
94 -------------------------------
95 -- Check_Controlling_Formals --
96 -------------------------------
98 procedure Check_Controlling_Formals
103 Ctrl_Type
: Entity_Id
;
106 Formal
:= First_Formal
(Subp
);
107 while Present
(Formal
) loop
108 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
110 if Present
(Ctrl_Type
) then
112 -- When controlling type is concurrent and declared within a
113 -- generic or inside an instance use corresponding record type.
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 that are 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 Ekind_In
(Subp
, E_Function
, E_Generic_Function
) 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 an
238 -- 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
)));
257 if No
(Tagged_Type
) or else Is_Class_Wide_Type
(Tagged_Type
) then
260 -- The dispatching type and the primitive operation must be defined in
261 -- the same scope, except in the case of internal operations and formal
262 -- abstract subprograms.
264 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
265 and then (not Is_Generic_Type
(Tagged_Type
)
266 or else not Comes_From_Source
(Subp
)))
268 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
270 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
272 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
274 Is_Abstract_Subprogram
(Subp
))
281 end Check_Controlling_Type
;
283 ----------------------------
284 -- Check_Dispatching_Call --
285 ----------------------------
287 procedure Check_Dispatching_Call
(N
: Node_Id
) is
288 Loc
: constant Source_Ptr
:= Sloc
(N
);
291 Control
: Node_Id
:= Empty
;
293 Subp_Entity
: Entity_Id
;
294 Indeterm_Ancestor_Call
: Boolean := False;
295 Indeterm_Ctrl_Type
: Entity_Id
;
297 Static_Tag
: Node_Id
:= Empty
;
298 -- If a controlling formal has a statically tagged actual, the tag of
299 -- this actual is to be used for any tag-indeterminate actual.
301 procedure Check_Direct_Call
;
302 -- In the case when the controlling actual is a class-wide type whose
303 -- root type's completion is a task or protected type, the call is in
304 -- fact direct. This routine detects the above case and modifies the
307 procedure Check_Dispatching_Context
;
308 -- If the call is tag-indeterminate and the entity being called is
309 -- abstract, verify that the context is a call that will eventually
310 -- provide a tag for dispatching, or has provided one already.
312 -----------------------
313 -- Check_Direct_Call --
314 -----------------------
316 procedure Check_Direct_Call
is
317 Typ
: Entity_Id
:= Etype
(Control
);
319 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean;
320 -- Determine whether an entity denotes a user-defined equality
322 ------------------------------
323 -- Is_User_Defined_Equality --
324 ------------------------------
326 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean is
329 Ekind
(Id
) = E_Function
330 and then Chars
(Id
) = Name_Op_Eq
331 and then Comes_From_Source
(Id
)
333 -- Internally generated equalities have a full type declaration
336 and then Nkind
(Parent
(Id
)) = N_Function_Specification
;
337 end Is_User_Defined_Equality
;
339 -- Start of processing for Check_Direct_Call
342 -- Predefined primitives do not receive wrappers since they are built
343 -- from scratch for the corresponding record of synchronized types.
344 -- Equality is in general predefined, but is excluded from the check
345 -- when it is user-defined.
347 if Is_Predefined_Dispatching_Operation
(Subp_Entity
)
348 and then not Is_User_Defined_Equality
(Subp_Entity
)
353 if Is_Class_Wide_Type
(Typ
) then
354 Typ
:= Root_Type
(Typ
);
357 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
358 Typ
:= Full_View
(Typ
);
361 if Is_Concurrent_Type
(Typ
)
363 Present
(Corresponding_Record_Type
(Typ
))
365 Typ
:= Corresponding_Record_Type
(Typ
);
367 -- The concurrent record's list of primitives should contain a
368 -- wrapper for the entity of the call, retrieve it.
373 Wrapper_Found
: Boolean := False;
376 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
377 while Present
(Prim_Elmt
) loop
378 Prim
:= Node
(Prim_Elmt
);
380 if Is_Primitive_Wrapper
(Prim
)
381 and then Wrapped_Entity
(Prim
) = Subp_Entity
383 Wrapper_Found
:= True;
387 Next_Elmt
(Prim_Elmt
);
390 -- A primitive declared between two views should have a
391 -- corresponding wrapper.
393 pragma Assert
(Wrapper_Found
);
395 -- Modify the call by setting the proper entity
397 Set_Entity
(Name
(N
), Prim
);
400 end Check_Direct_Call
;
402 -------------------------------
403 -- Check_Dispatching_Context --
404 -------------------------------
406 procedure Check_Dispatching_Context
is
407 Subp
: constant Entity_Id
:= Entity
(Name
(N
));
411 if Is_Abstract_Subprogram
(Subp
)
412 and then No
(Controlling_Argument
(N
))
414 if Present
(Alias
(Subp
))
415 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
416 and then No
(DTC_Entity
(Subp
))
418 -- Private overriding of inherited abstract operation, call is
421 Set_Entity
(Name
(N
), Alias
(Subp
));
426 while Present
(Par
) loop
427 if Nkind_In
(Par
, N_Function_Call
,
428 N_Procedure_Call_Statement
,
429 N_Assignment_Statement
,
432 and then Is_Tagged_Type
(Etype
(Subp
))
436 elsif Nkind
(Par
) = N_Qualified_Expression
437 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
442 if Ekind
(Subp
) = E_Function
then
444 ("call to abstract function must be dispatching", N
);
446 -- This error can occur for a procedure in the case of a
447 -- call to an abstract formal procedure with a statically
452 ("call to abstract procedure must be dispatching",
461 end Check_Dispatching_Context
;
463 -- Start of processing for Check_Dispatching_Call
466 -- Find a controlling argument, if any
468 if Present
(Parameter_Associations
(N
)) then
469 Subp_Entity
:= Entity
(Name
(N
));
471 Actual
:= First_Actual
(N
);
472 Formal
:= First_Formal
(Subp_Entity
);
473 while Present
(Actual
) loop
474 Control
:= Find_Controlling_Arg
(Actual
);
475 exit when Present
(Control
);
477 -- Check for the case where the actual is a tag-indeterminate call
478 -- whose result type is different than the tagged type associated
479 -- with the containing call, but is an ancestor of the type.
481 if Is_Controlling_Formal
(Formal
)
482 and then Is_Tag_Indeterminate
(Actual
)
483 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
484 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
486 Indeterm_Ancestor_Call
:= True;
487 Indeterm_Ctrl_Type
:= Etype
(Formal
);
489 -- If the formal is controlling but the actual is not, the type
490 -- of the actual is statically known, and may be used as the
491 -- controlling tag for some other tag-indeterminate actual.
493 elsif Is_Controlling_Formal
(Formal
)
494 and then Is_Entity_Name
(Actual
)
495 and then Is_Tagged_Type
(Etype
(Actual
))
497 Static_Tag
:= Actual
;
500 Next_Actual
(Actual
);
501 Next_Formal
(Formal
);
504 -- If the call doesn't have a controlling actual but does have an
505 -- indeterminate actual that requires dispatching treatment, then an
506 -- object is needed that will serve as the controlling argument for a
507 -- dispatching call on the indeterminate actual. This can only occur
508 -- in the unusual situation of a default actual given by a
509 -- tag-indeterminate call and where the type of the call is an
510 -- ancestor of the type associated with a containing call to an
511 -- inherited operation (see AI-239).
513 -- Rather than create an object of the tagged type, which would be
514 -- problematic for various reasons (default initialization,
515 -- discriminants), the tag of the containing call's associated tagged
516 -- type is directly used to control the dispatching.
519 and then Indeterm_Ancestor_Call
520 and then No
(Static_Tag
)
523 Make_Attribute_Reference
(Loc
,
524 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
525 Attribute_Name
=> Name_Tag
);
530 if Present
(Control
) then
532 -- Verify that no controlling arguments are statically tagged
535 Write_Str
("Found Dispatching call");
540 Actual
:= First_Actual
(N
);
541 while Present
(Actual
) loop
542 if Actual
/= Control
then
544 if not Is_Controlling_Actual
(Actual
) then
545 null; -- Can be anything
547 elsif Is_Dynamically_Tagged
(Actual
) then
548 null; -- Valid parameter
550 elsif Is_Tag_Indeterminate
(Actual
) then
552 -- The tag is inherited from the enclosing call (the node
553 -- we are currently analyzing). Explicitly expand the
554 -- actual, since the previous call to Expand (from
555 -- Resolve_Call) had no way of knowing about the required
558 Propagate_Tag
(Control
, Actual
);
562 ("controlling argument is not dynamically tagged",
568 Next_Actual
(Actual
);
571 -- Mark call as a dispatching call
573 Set_Controlling_Argument
(N
, Control
);
574 Check_Restriction
(No_Dispatching_Calls
, N
);
576 -- The dispatching call may need to be converted into a direct
577 -- call in certain cases.
581 -- If there is a statically tagged actual and a tag-indeterminate
582 -- call to a function of the ancestor (such as that provided by a
583 -- default), then treat this as a dispatching call and propagate
584 -- the tag to the tag-indeterminate call(s).
586 elsif Present
(Static_Tag
) and then Indeterm_Ancestor_Call
then
588 Make_Attribute_Reference
(Loc
,
590 New_Occurrence_Of
(Etype
(Static_Tag
), Loc
),
591 Attribute_Name
=> Name_Tag
);
595 Actual
:= First_Actual
(N
);
596 Formal
:= First_Formal
(Subp_Entity
);
597 while Present
(Actual
) loop
598 if Is_Tag_Indeterminate
(Actual
)
599 and then Is_Controlling_Formal
(Formal
)
601 Propagate_Tag
(Control
, Actual
);
604 Next_Actual
(Actual
);
605 Next_Formal
(Formal
);
608 Check_Dispatching_Context
;
611 -- The call is not dispatching, so check that there aren't any
612 -- tag-indeterminate abstract calls left.
614 Actual
:= First_Actual
(N
);
615 while Present
(Actual
) loop
616 if Is_Tag_Indeterminate
(Actual
) then
618 -- Function call case
620 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
621 Func
:= Entity
(Name
(Original_Node
(Actual
)));
623 -- If the actual is an attribute then it can't be abstract
624 -- (the only current case of a tag-indeterminate attribute
625 -- is the stream Input attribute).
628 Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
632 -- Only other possibility is a qualified expression whose
633 -- constituent expression is itself a call.
639 (Expression
(Original_Node
(Actual
)))));
642 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
644 ("call to abstract function must be dispatching", N
);
648 Next_Actual
(Actual
);
651 Check_Dispatching_Context
;
655 -- If dispatching on result, the enclosing call, if any, will
656 -- determine the controlling argument. Otherwise this is the
657 -- primitive operation of the root type.
659 Check_Dispatching_Context
;
661 end Check_Dispatching_Call
;
663 ---------------------------------
664 -- Check_Dispatching_Operation --
665 ---------------------------------
667 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
668 Tagged_Type
: Entity_Id
;
669 Has_Dispatching_Parent
: Boolean := False;
670 Body_Is_Last_Primitive
: Boolean := False;
673 if not Ekind_In
(Subp
, E_Procedure
, E_Function
) then
677 Set_Is_Dispatching_Operation
(Subp
, False);
678 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
680 -- Ada 2005 (AI-345): Use the corresponding record (if available).
681 -- Required because primitives of concurrent types are be attached
682 -- to the corresponding record (not to the concurrent type).
684 if Ada_Version
>= Ada_05
685 and then Present
(Tagged_Type
)
686 and then Is_Concurrent_Type
(Tagged_Type
)
687 and then Present
(Corresponding_Record_Type
(Tagged_Type
))
689 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
692 -- (AI-345): The task body procedure is not a primitive of the tagged
695 if Present
(Tagged_Type
)
696 and then Is_Concurrent_Record_Type
(Tagged_Type
)
697 and then Present
(Corresponding_Concurrent_Type
(Tagged_Type
))
698 and then Is_Task_Type
(Corresponding_Concurrent_Type
(Tagged_Type
))
699 and then Subp
= Get_Task_Body_Procedure
700 (Corresponding_Concurrent_Type
(Tagged_Type
))
705 -- If Subp is derived from a dispatching operation then it should
706 -- always be treated as dispatching. In this case various checks
707 -- below will be bypassed. Makes sure that late declarations for
708 -- inherited private subprograms are treated as dispatching, even
709 -- if the associated tagged type is already frozen.
711 Has_Dispatching_Parent
:=
712 Present
(Alias
(Subp
))
713 and then Is_Dispatching_Operation
(Alias
(Subp
));
715 if No
(Tagged_Type
) then
717 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
718 -- with an abstract interface type unless the interface acts as a
719 -- parent type in a derivation. If the interface type is a formal
720 -- type then the operation is not primitive and therefore legal.
727 E
:= First_Entity
(Subp
);
728 while Present
(E
) loop
730 -- For an access parameter, check designated type
732 if Ekind
(Etype
(E
)) = E_Anonymous_Access_Type
then
733 Typ
:= Designated_Type
(Etype
(E
));
738 if Comes_From_Source
(Subp
)
739 and then Is_Interface
(Typ
)
740 and then not Is_Class_Wide_Type
(Typ
)
741 and then not Is_Derived_Type
(Typ
)
742 and then not Is_Generic_Type
(Typ
)
743 and then not In_Instance
745 Error_Msg_N
("?declaration of& is too late!", Subp
);
746 Error_Msg_NE
-- CODEFIX??
747 ("\spec should appear immediately after declaration of &!",
755 -- In case of functions check also the result type
757 if Ekind
(Subp
) = E_Function
then
758 if Is_Access_Type
(Etype
(Subp
)) then
759 Typ
:= Designated_Type
(Etype
(Subp
));
764 if not Is_Class_Wide_Type
(Typ
)
765 and then Is_Interface
(Typ
)
766 and then not Is_Derived_Type
(Typ
)
768 Error_Msg_N
("?declaration of& is too late!", Subp
);
770 ("\spec should appear immediately after declaration of &!",
778 -- The subprograms build internally after the freezing point (such as
779 -- init procs, interface thunks, type support subprograms, and Offset
780 -- to top functions for accessing interface components in variable
781 -- size tagged types) are not primitives.
783 elsif Is_Frozen
(Tagged_Type
)
784 and then not Comes_From_Source
(Subp
)
785 and then not Has_Dispatching_Parent
787 -- Complete decoration of internally built subprograms that override
788 -- a dispatching primitive. These entities correspond with the
791 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
792 -- to override functions of nonabstract null extensions. These
793 -- primitives were added to the list of primitives of the tagged
794 -- type by Make_Controlling_Function_Wrappers. However, attribute
795 -- Is_Dispatching_Operation must be set to true.
797 -- 2. Subprograms associated with stream attributes (built by
798 -- New_Stream_Subprogram)
800 if Present
(Old_Subp
)
801 and then Is_Overriding_Operation
(Subp
)
802 and then Is_Dispatching_Operation
(Old_Subp
)
805 ((Ekind
(Subp
) = E_Function
806 and then Is_Dispatching_Operation
(Old_Subp
)
807 and then Is_Null_Extension
(Base_Type
(Etype
(Subp
))))
808 or else Get_TSS_Name
(Subp
) = TSS_Stream_Read
809 or else Get_TSS_Name
(Subp
) = TSS_Stream_Write
);
811 Set_Is_Dispatching_Operation
(Subp
);
816 -- The operation may be a child unit, whose scope is the defining
817 -- package, but which is not a primitive operation of the type.
819 elsif Is_Child_Unit
(Subp
) then
822 -- If the subprogram is not defined in a package spec, the only case
823 -- where it can be a dispatching op is when it overrides an operation
824 -- before the freezing point of the type.
826 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
827 or else In_Package_Body
(Scope
(Subp
)))
828 and then not Has_Dispatching_Parent
830 if not Comes_From_Source
(Subp
)
831 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
835 -- If the type is already frozen, the overriding is not allowed
836 -- except when Old_Subp is not a dispatching operation (which can
837 -- occur when Old_Subp was inherited by an untagged type). However,
838 -- a body with no previous spec freezes the type *after* its
839 -- declaration, and therefore is a legal overriding (unless the type
840 -- has already been frozen). Only the first such body is legal.
842 elsif Present
(Old_Subp
)
843 and then Is_Dispatching_Operation
(Old_Subp
)
845 if Comes_From_Source
(Subp
)
847 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
848 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
851 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
855 -- ??? The checks here for whether the type has been
856 -- frozen prior to the new body are not complete. It's
857 -- not simple to check frozenness at this point since
858 -- the body has already caused the type to be prematurely
859 -- frozen in Analyze_Declarations, but we're forced to
860 -- recheck this here because of the odd rule interpretation
861 -- that allows the overriding if the type wasn't frozen
862 -- prior to the body. The freezing action should probably
863 -- be delayed until after the spec is seen, but that's
864 -- a tricky change to the delicate freezing code.
866 -- Look at each declaration following the type up until the
867 -- new subprogram body. If any of the declarations is a body
868 -- then the type has been frozen already so the overriding
869 -- primitive is illegal.
871 Decl_Item
:= Next
(Parent
(Tagged_Type
));
872 while Present
(Decl_Item
)
873 and then (Decl_Item
/= Subp_Body
)
875 if Comes_From_Source
(Decl_Item
)
876 and then (Nkind
(Decl_Item
) in N_Proper_Body
877 or else Nkind
(Decl_Item
) in N_Body_Stub
)
879 Error_Msg_N
("overriding of& is too late!", Subp
);
881 ("\spec should appear immediately after the type!",
889 -- If the subprogram doesn't follow in the list of
890 -- declarations including the type then the type has
891 -- definitely been frozen already and the body is illegal.
893 if No
(Decl_Item
) then
894 Error_Msg_N
("overriding of& is too late!", Subp
);
896 ("\spec should appear immediately after the type!",
899 elsif Is_Frozen
(Subp
) then
901 -- The subprogram body declares a primitive operation.
902 -- if the subprogram is already frozen, we must update
903 -- its dispatching information explicitly here. The
904 -- information is taken from the overridden subprogram.
905 -- We must also generate a cross-reference entry because
906 -- references to other primitives were already created
907 -- when type was frozen.
909 Body_Is_Last_Primitive
:= True;
911 if Present
(DTC_Entity
(Old_Subp
)) then
912 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
913 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
915 if not Restriction_Active
(No_Dispatching_Calls
) then
916 if Building_Static_DT
(Tagged_Type
) then
918 -- If the static dispatch table has not been
919 -- built then there is nothing else to do now;
920 -- otherwise we notify that we cannot build the
921 -- static dispatch table.
923 if Has_Dispatch_Table
(Tagged_Type
) then
925 ("overriding of& is too late for building" &
926 " static dispatch tables!", Subp
);
928 ("\spec should appear immediately after" &
933 Insert_Actions_After
(Subp_Body
,
934 Register_Primitive
(Sloc
(Subp_Body
),
938 -- Indicate that this is an overriding operation,
939 -- and replace the overriden entry in the list of
940 -- primitive operations, which is used for xref
941 -- generation subsequently.
943 Generate_Reference
(Tagged_Type
, Subp
, 'P', False);
944 Override_Dispatching_Operation
945 (Tagged_Type
, Old_Subp
, Subp
);
952 Error_Msg_N
("overriding of& is too late!", Subp
);
954 ("\subprogram spec should appear immediately after the type!",
958 -- If the type is not frozen yet and we are not in the overriding
959 -- case it looks suspiciously like an attempt to define a primitive
960 -- operation, which requires the declaration to be in a package spec
963 elsif not Is_Frozen
(Tagged_Type
) then
965 ("?not dispatching (must be defined in a package spec)", Subp
);
968 -- When the type is frozen, it is legitimate to define a new
969 -- non-primitive operation.
975 -- Now, we are sure that the scope is a package spec. If the subprogram
976 -- is declared after the freezing point of the type that's an error
978 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
979 Error_Msg_N
("this primitive operation is declared too late", Subp
);
981 ("?no primitive operations for& after this line",
982 Freeze_Node
(Tagged_Type
),
987 Check_Controlling_Formals
(Tagged_Type
, Subp
);
989 -- Now it should be a correct primitive operation, put it in the list
991 if Present
(Old_Subp
) then
993 -- If the type has interfaces we complete this check after we set
994 -- attribute Is_Dispatching_Operation.
996 Check_Subtype_Conformant
(Subp
, Old_Subp
);
998 if (Chars
(Subp
) = Name_Initialize
999 or else Chars
(Subp
) = Name_Adjust
1000 or else Chars
(Subp
) = Name_Finalize
)
1001 and then Is_Controlled
(Tagged_Type
)
1002 and then not Is_Visibly_Controlled
(Tagged_Type
)
1004 Set_Is_Overriding_Operation
(Subp
, False);
1006 -- If the subprogram specification carries an overriding
1007 -- indicator, no need for the warning: it is either redundant,
1008 -- or else an error will be reported.
1010 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
1012 (Must_Override
(Parent
(Subp
))
1013 or else Must_Not_Override
(Parent
(Subp
)))
1017 -- Here we need the warning
1021 ("operation does not override inherited&?", Subp
, Subp
);
1025 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
1026 Set_Is_Overriding_Operation
(Subp
);
1028 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1029 -- that covers abstract interface subprograms we must register it
1030 -- in all the secondary dispatch tables associated with abstract
1031 -- interfaces. We do this now only if not building static tables.
1032 -- Otherwise the patch code is emitted after those tables are
1033 -- built, to prevent access_before_elaboration in gigi.
1035 if Body_Is_Last_Primitive
then
1037 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1042 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1043 while Present
(Elmt
) loop
1044 Prim
:= Node
(Elmt
);
1046 if Present
(Alias
(Prim
))
1047 and then Present
(Interface_Alias
(Prim
))
1048 and then Alias
(Prim
) = Subp
1049 and then not Building_Static_DT
(Tagged_Type
)
1051 Insert_Actions_After
(Subp_Body
,
1052 Register_Primitive
(Sloc
(Subp_Body
), Prim
=> Prim
));
1058 -- Redisplay the contents of the updated dispatch table
1060 if Debug_Flag_ZZ
then
1061 Write_Str
("Late overriding: ");
1062 Write_DT
(Tagged_Type
);
1068 -- If the tagged type is a concurrent type then we must be compiling
1069 -- with no code generation (we are either compiling a generic unit or
1070 -- compiling under -gnatc mode) because we have previously tested that
1071 -- no serious errors has been reported. In this case we do not add the
1072 -- primitive to the list of primitives of Tagged_Type but we leave the
1073 -- primitive decorated as a dispatching operation to be able to analyze
1074 -- and report errors associated with the Object.Operation notation.
1076 elsif Is_Concurrent_Type
(Tagged_Type
) then
1077 pragma Assert
(not Expander_Active
);
1080 -- If no old subprogram, then we add this as a dispatching operation,
1081 -- but we avoid doing this if an error was posted, to prevent annoying
1084 elsif not Error_Posted
(Subp
) then
1085 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
1088 Set_Is_Dispatching_Operation
(Subp
, True);
1090 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1091 -- subtype conformance against all the interfaces covered by this
1094 if Present
(Old_Subp
)
1095 and then Has_Interfaces
(Tagged_Type
)
1098 Ifaces_List
: Elist_Id
;
1099 Iface_Elmt
: Elmt_Id
;
1100 Iface_Prim_Elmt
: Elmt_Id
;
1101 Iface_Prim
: Entity_Id
;
1102 Ret_Typ
: Entity_Id
;
1105 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
1107 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
1108 while Present
(Iface_Elmt
) loop
1109 if not Is_Ancestor
(Node
(Iface_Elmt
), Tagged_Type
) then
1111 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
1112 while Present
(Iface_Prim_Elmt
) loop
1113 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
1115 if Is_Interface_Conformant
1116 (Tagged_Type
, Iface_Prim
, Subp
)
1118 -- Handle procedures, functions whose return type
1119 -- matches, or functions not returning interfaces
1121 if Ekind
(Subp
) = E_Procedure
1122 or else Etype
(Iface_Prim
) = Etype
(Subp
)
1123 or else not Is_Interface
(Etype
(Iface_Prim
))
1125 Check_Subtype_Conformant
1127 Old_Id
=> Iface_Prim
,
1129 Skip_Controlling_Formals
=> True);
1131 -- Handle functions returning interfaces
1133 elsif Implements_Interface
1134 (Etype
(Subp
), Etype
(Iface_Prim
))
1136 -- Temporarily force both entities to return the
1137 -- same type. Required because Subtype_Conformant
1138 -- does not handle this case.
1140 Ret_Typ
:= Etype
(Iface_Prim
);
1141 Set_Etype
(Iface_Prim
, Etype
(Subp
));
1143 Check_Subtype_Conformant
1145 Old_Id
=> Iface_Prim
,
1147 Skip_Controlling_Formals
=> True);
1149 Set_Etype
(Iface_Prim
, Ret_Typ
);
1153 Next_Elmt
(Iface_Prim_Elmt
);
1157 Next_Elmt
(Iface_Elmt
);
1162 if not Body_Is_Last_Primitive
then
1163 Set_DT_Position
(Subp
, No_Uint
);
1165 elsif Has_Controlled_Component
(Tagged_Type
)
1167 (Chars
(Subp
) = Name_Initialize
1169 Chars
(Subp
) = Name_Adjust
1171 Chars
(Subp
) = Name_Finalize
)
1174 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
1178 Old_Spec
: Entity_Id
;
1180 C_Names
: constant array (1 .. 3) of Name_Id
:=
1185 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
1186 (TSS_Deep_Initialize
,
1191 -- Remove previous controlled function which was constructed and
1192 -- analyzed when the type was frozen. This requires removing the
1193 -- body of the redefined primitive, as well as its specification
1194 -- if needed (there is no spec created for Deep_Initialize, see
1195 -- exp_ch3.adb). We must also dismantle the exception information
1196 -- that may have been generated for it when front end zero-cost
1197 -- tables are enabled.
1199 for J
in D_Names
'Range loop
1200 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
1203 and then Chars
(Subp
) = C_Names
(J
)
1205 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
1207 Set_Is_Eliminated
(Old_P
);
1208 Set_Scope
(Old_P
, Scope
(Current_Scope
));
1210 if Nkind
(Old_Bod
) = N_Subprogram_Body
1211 and then Present
(Corresponding_Spec
(Old_Bod
))
1213 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
1214 Set_Has_Completion
(Old_Spec
, False);
1219 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
1221 -- The new operation is added to the actions of the freeze node
1222 -- for the type, but this node has already been analyzed, so we
1223 -- must retrieve and analyze explicitly the new body.
1226 and then Present
(Actions
(F_Node
))
1228 Decl
:= Last
(Actions
(F_Node
));
1233 end Check_Dispatching_Operation
;
1235 ------------------------------------------
1236 -- Check_Operation_From_Incomplete_Type --
1237 ------------------------------------------
1239 procedure Check_Operation_From_Incomplete_Type
1243 Full
: constant Entity_Id
:= Full_View
(Typ
);
1244 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1245 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1246 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1248 Prev
: Elmt_Id
:= No_Elmt
;
1250 function Derives_From
(Proc
: Entity_Id
) return Boolean;
1251 -- Check that Subp has the signature of an operation derived from Proc.
1252 -- Subp has an access parameter that designates Typ.
1258 function Derives_From
(Proc
: Entity_Id
) return Boolean is
1262 if Chars
(Proc
) /= Chars
(Subp
) then
1266 F1
:= First_Formal
(Proc
);
1267 F2
:= First_Formal
(Subp
);
1268 while Present
(F1
) and then Present
(F2
) loop
1269 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
1270 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
1272 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
1273 and then Designated_Type
(Etype
(F2
)) /= Full
1278 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
1281 elsif Etype
(F1
) /= Etype
(F2
) then
1289 return No
(F1
) and then No
(F2
);
1292 -- Start of processing for Check_Operation_From_Incomplete_Type
1295 -- The operation may override an inherited one, or may be a new one
1296 -- altogether. The inherited operation will have been hidden by the
1297 -- current one at the point of the type derivation, so it does not
1298 -- appear in the list of primitive operations of the type. We have to
1299 -- find the proper place of insertion in the list of primitive opera-
1300 -- tions by iterating over the list for the parent type.
1302 Op1
:= First_Elmt
(Old_Prim
);
1303 Op2
:= First_Elmt
(New_Prim
);
1304 while Present
(Op1
) and then Present
(Op2
) loop
1305 if Derives_From
(Node
(Op1
)) then
1308 -- Avoid adding it to the list of primitives if already there!
1310 if Node
(Op2
) /= Subp
then
1311 Prepend_Elmt
(Subp
, New_Prim
);
1315 Insert_Elmt_After
(Subp
, Prev
);
1326 -- Operation is a new primitive
1328 Append_Elmt
(Subp
, New_Prim
);
1329 end Check_Operation_From_Incomplete_Type
;
1331 ---------------------------------------
1332 -- Check_Operation_From_Private_View --
1333 ---------------------------------------
1335 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
1336 Tagged_Type
: Entity_Id
;
1339 if Is_Dispatching_Operation
(Alias
(Subp
)) then
1340 Set_Scope
(Subp
, Current_Scope
);
1341 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1343 -- Add Old_Subp to primitive operations if not already present
1345 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
1346 Append_Unique_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
1348 -- If Old_Subp isn't already marked as dispatching then
1349 -- this is the case of an operation of an untagged private
1350 -- type fulfilled by a tagged type that overrides an
1351 -- inherited dispatching operation, so we set the necessary
1352 -- dispatching attributes here.
1354 if not Is_Dispatching_Operation
(Old_Subp
) then
1356 -- If the untagged type has no discriminants, and the full
1357 -- view is constrained, there will be a spurious mismatch
1358 -- of subtypes on the controlling arguments, because the tagged
1359 -- type is the internal base type introduced in the derivation.
1360 -- Use the original type to verify conformance, rather than the
1363 if not Comes_From_Source
(Tagged_Type
)
1364 and then Has_Discriminants
(Tagged_Type
)
1370 Formal
:= First_Formal
(Old_Subp
);
1371 while Present
(Formal
) loop
1372 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
1373 Tagged_Type
:= Etype
(Formal
);
1376 Next_Formal
(Formal
);
1380 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
1381 Tagged_Type
:= Etype
(Old_Subp
);
1385 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
1386 Set_Is_Dispatching_Operation
(Old_Subp
, True);
1387 Set_DT_Position
(Old_Subp
, No_Uint
);
1390 -- If the old subprogram is an explicit renaming of some other
1391 -- entity, it is not overridden by the inherited subprogram.
1392 -- Otherwise, update its alias and other attributes.
1394 if Present
(Alias
(Old_Subp
))
1395 and then Nkind
(Unit_Declaration_Node
(Old_Subp
)) /=
1396 N_Subprogram_Renaming_Declaration
1398 Set_Alias
(Old_Subp
, Alias
(Subp
));
1400 -- The derived subprogram should inherit the abstractness
1401 -- of the parent subprogram (except in the case of a function
1402 -- returning the type). This sets the abstractness properly
1403 -- for cases where a private extension may have inherited
1404 -- an abstract operation, but the full type is derived from
1405 -- a descendant type and inherits a nonabstract version.
1407 if Etype
(Subp
) /= Tagged_Type
then
1408 Set_Is_Abstract_Subprogram
1409 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
1414 end Check_Operation_From_Private_View
;
1416 --------------------------
1417 -- Find_Controlling_Arg --
1418 --------------------------
1420 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1421 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1425 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1426 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1429 -- Dispatching on result case. If expansion is disabled, the node still
1430 -- has the structure of a function call. However, if the function name
1431 -- is an operator and the call was given in infix form, the original
1432 -- node has no controlling result and we must examine the current node.
1434 if Nkind
(N
) = N_Function_Call
1435 and then Present
(Controlling_Argument
(N
))
1436 and then Has_Controlling_Result
(Entity
(Name
(N
)))
1438 return Controlling_Argument
(N
);
1440 -- If expansion is enabled, the call may have been transformed into
1441 -- an indirect call, and we need to recover the original node.
1443 elsif Nkind
(Orig_Node
) = N_Function_Call
1444 and then Present
(Controlling_Argument
(Orig_Node
))
1445 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1447 return Controlling_Argument
(Orig_Node
);
1451 elsif Is_Controlling_Actual
(N
)
1453 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1454 and then Is_Controlling_Actual
(Parent
(N
)))
1458 if Is_Access_Type
(Typ
) then
1460 -- In the case of an Access attribute, use the type of the prefix,
1461 -- since in the case of an actual for an access parameter, the
1462 -- attribute's type may be of a specific designated type, even
1463 -- though the prefix type is class-wide.
1465 if Nkind
(N
) = N_Attribute_Reference
then
1466 Typ
:= Etype
(Prefix
(N
));
1468 -- An allocator is dispatching if the type of qualified expression
1469 -- is class_wide, in which case this is the controlling type.
1471 elsif Nkind
(Orig_Node
) = N_Allocator
1472 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1474 Typ
:= Etype
(Expression
(Orig_Node
));
1476 Typ
:= Designated_Type
(Typ
);
1480 if Is_Class_Wide_Type
(Typ
)
1482 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1483 and then Is_Access_Type
(Etype
(N
))
1484 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1491 end Find_Controlling_Arg
;
1493 ---------------------------
1494 -- Find_Dispatching_Type --
1495 ---------------------------
1497 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1498 A_Formal
: Entity_Id
;
1500 Ctrl_Type
: Entity_Id
;
1503 if Present
(DTC_Entity
(Subp
)) then
1504 return Scope
(DTC_Entity
(Subp
));
1506 -- For subprograms internally generated by derivations of tagged types
1507 -- use the alias subprogram as a reference to locate the dispatching
1510 elsif not Comes_From_Source
(Subp
)
1511 and then Present
(Alias
(Subp
))
1512 and then Is_Dispatching_Operation
(Alias
(Subp
))
1514 if Ekind
(Alias
(Subp
)) = E_Function
1515 and then Has_Controlling_Result
(Alias
(Subp
))
1517 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1520 Formal
:= First_Formal
(Subp
);
1521 A_Formal
:= First_Formal
(Alias
(Subp
));
1522 while Present
(A_Formal
) loop
1523 if Is_Controlling_Formal
(A_Formal
) then
1524 return Check_Controlling_Type
(Etype
(Formal
), Subp
);
1527 Next_Formal
(Formal
);
1528 Next_Formal
(A_Formal
);
1531 pragma Assert
(False);
1538 Formal
:= First_Formal
(Subp
);
1539 while Present
(Formal
) loop
1540 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1542 if Present
(Ctrl_Type
) then
1546 Next_Formal
(Formal
);
1549 -- The subprogram may also be dispatching on result
1551 if Present
(Etype
(Subp
)) then
1552 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1556 pragma Assert
(not Is_Dispatching_Operation
(Subp
));
1558 end Find_Dispatching_Type
;
1560 ---------------------------------------
1561 -- Find_Primitive_Covering_Interface --
1562 ---------------------------------------
1564 function Find_Primitive_Covering_Interface
1565 (Tagged_Type
: Entity_Id
;
1566 Iface_Prim
: Entity_Id
) return Entity_Id
1571 pragma Assert
(Is_Interface
(Find_Dispatching_Type
(Iface_Prim
))
1572 or else (Present
(Alias
(Iface_Prim
))
1575 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
1577 E
:= Current_Entity
(Iface_Prim
);
1578 while Present
(E
) loop
1579 if Is_Subprogram
(E
)
1580 and then Is_Dispatching_Operation
(E
)
1581 and then Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
)
1590 end Find_Primitive_Covering_Interface
;
1592 ---------------------------
1593 -- Is_Dynamically_Tagged --
1594 ---------------------------
1596 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1598 if Nkind
(N
) = N_Error
then
1601 return Find_Controlling_Arg
(N
) /= Empty
;
1603 end Is_Dynamically_Tagged
;
1605 --------------------------
1606 -- Is_Tag_Indeterminate --
1607 --------------------------
1609 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1612 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1615 if Nkind
(Orig_Node
) = N_Function_Call
1616 and then Is_Entity_Name
(Name
(Orig_Node
))
1618 Nam
:= Entity
(Name
(Orig_Node
));
1620 if not Has_Controlling_Result
(Nam
) then
1623 -- An explicit dereference means that the call has already been
1624 -- expanded and there is no tag to propagate.
1626 elsif Nkind
(N
) = N_Explicit_Dereference
then
1629 -- If there are no actuals, the call is tag-indeterminate
1631 elsif No
(Parameter_Associations
(Orig_Node
)) then
1635 Actual
:= First_Actual
(Orig_Node
);
1636 while Present
(Actual
) loop
1637 if Is_Controlling_Actual
(Actual
)
1638 and then not Is_Tag_Indeterminate
(Actual
)
1640 return False; -- one operand is dispatching
1643 Next_Actual
(Actual
);
1649 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
1650 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
1652 -- Case of a call to the Input attribute (possibly rewritten), which is
1653 -- always tag-indeterminate except when its prefix is a Class attribute.
1655 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
1657 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
1659 Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
1663 -- In Ada 2005 a function that returns an anonymous access type can
1664 -- dispatching, and the dereference of a call to such a function
1665 -- is also tag-indeterminate.
1667 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
1668 and then Ada_Version
>= Ada_05
1670 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
1675 end Is_Tag_Indeterminate
;
1677 ------------------------------------
1678 -- Override_Dispatching_Operation --
1679 ------------------------------------
1681 procedure Override_Dispatching_Operation
1682 (Tagged_Type
: Entity_Id
;
1683 Prev_Op
: Entity_Id
;
1690 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1691 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1693 if No_Return
(Prev_Op
) and then not No_Return
(New_Op
) then
1694 Error_Msg_N
("procedure & must have No_Return pragma", New_Op
);
1695 Error_Msg_N
("\since overridden procedure has No_Return", New_Op
);
1698 -- If there is no previous operation to override, the type declaration
1699 -- was malformed, and an error must have been emitted already.
1701 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1702 while Present
(Elmt
)
1703 and then Node
(Elmt
) /= Prev_Op
1712 -- The location of entities that come from source in the list of
1713 -- primitives of the tagged type must follow their order of occurrence
1714 -- in the sources to fulfill the C++ ABI. If the overriden entity is a
1715 -- primitive of an interface that is not an ancestor of this tagged
1716 -- type (that is, it is an entity added to the list of primitives by
1717 -- Derive_Interface_Progenitors), then we must append the new entity
1718 -- at the end of the list of primitives.
1720 if Present
(Alias
(Prev_Op
))
1721 and then Is_Interface
(Find_Dispatching_Type
(Alias
(Prev_Op
)))
1722 and then not Is_Ancestor
(Find_Dispatching_Type
(Alias
(Prev_Op
)),
1725 Remove_Elmt
(Primitive_Operations
(Tagged_Type
), Elmt
);
1726 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
1728 -- The new primitive replaces the overriden entity. Required to ensure
1729 -- that overriding primitive is assigned the same dispatch table slot.
1732 Replace_Elmt
(Elmt
, New_Op
);
1735 if Ada_Version
>= Ada_05
1736 and then Has_Interfaces
(Tagged_Type
)
1738 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1739 -- entities of the overridden primitive to reference New_Op, and also
1740 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1741 -- that the new operation is subtype conformant with the interface
1742 -- operations that it implements (for operations inherited from the
1743 -- parent itself, this check is made when building the derived type).
1745 -- Note: This code is only executed in case of late overriding
1747 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1748 while Present
(Elmt
) loop
1749 Prim
:= Node
(Elmt
);
1751 if Prim
= New_Op
then
1754 -- Note: The check on Is_Subprogram protects the frontend against
1755 -- reading attributes in entities that are not yet fully decorated
1757 elsif Is_Subprogram
(Prim
)
1758 and then Present
(Interface_Alias
(Prim
))
1759 and then Alias
(Prim
) = Prev_Op
1760 and then Present
(Etype
(New_Op
))
1762 Set_Alias
(Prim
, New_Op
);
1763 Check_Subtype_Conformant
(New_Op
, Prim
);
1764 Set_Is_Abstract_Subprogram
(Prim
,
1765 Is_Abstract_Subprogram
(New_Op
));
1767 -- Ensure that this entity will be expanded to fill the
1768 -- corresponding entry in its dispatch table.
1770 if not Is_Abstract_Subprogram
(Prim
) then
1771 Set_Has_Delayed_Freeze
(Prim
);
1779 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
1780 or else not In_Private_Part
(Current_Scope
)
1782 -- Not a private primitive
1786 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
1788 -- Make the overriding operation into an alias of the implicit one.
1789 -- In this fashion a call from outside ends up calling the new body
1790 -- even if non-dispatching, and a call from inside calls the
1791 -- overriding operation because it hides the implicit one. To
1792 -- indicate that the body of Prev_Op is never called, set its
1793 -- dispatch table entity to Empty. If the overridden operation
1794 -- has a dispatching result, so does the overriding one.
1796 Set_Alias
(Prev_Op
, New_Op
);
1797 Set_DTC_Entity
(Prev_Op
, Empty
);
1798 Set_Has_Controlling_Result
(New_Op
, Has_Controlling_Result
(Prev_Op
));
1801 end Override_Dispatching_Operation
;
1807 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
1808 Call_Node
: Node_Id
;
1812 if Nkind
(Actual
) = N_Function_Call
then
1813 Call_Node
:= Actual
;
1815 elsif Nkind
(Actual
) = N_Identifier
1816 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
1818 -- Call rewritten as object declaration when stack-checking is
1819 -- enabled. Propagate tag to expression in declaration, which is
1822 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
1824 -- Ada 2005: If this is a dereference of a call to a function with a
1825 -- dispatching access-result, the tag is propagated when the dereference
1826 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1828 elsif Nkind
(Actual
) = N_Explicit_Dereference
1829 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
1833 -- Only other possibilities are parenthesized or qualified expression,
1834 -- or an expander-generated unchecked conversion of a function call to
1835 -- a stream Input attribute.
1838 Call_Node
:= Expression
(Actual
);
1841 -- Do not set the Controlling_Argument if already set. This happens in
1842 -- the special case of _Input (see Exp_Attr, case Input).
1844 if No
(Controlling_Argument
(Call_Node
)) then
1845 Set_Controlling_Argument
(Call_Node
, Control
);
1848 Arg
:= First_Actual
(Call_Node
);
1850 while Present
(Arg
) loop
1851 if Is_Tag_Indeterminate
(Arg
) then
1852 Propagate_Tag
(Control
, Arg
);
1858 -- Expansion of dispatching calls is suppressed when VM_Target, because
1859 -- the VM back-ends directly handle the generation of dispatching calls
1860 -- and would have to undo any expansion to an indirect call.
1862 if Tagged_Type_Expansion
then
1863 Expand_Dispatching_Call
(Call_Node
);
1865 -- Expansion of a dispatching call results in an indirect call, which in
1866 -- turn causes current values to be killed (see Resolve_Call), so on VM
1867 -- targets we do the call here to ensure consistent warnings between VM
1868 -- and non-VM targets.
1871 Kill_Current_Values
;