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 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
;
76 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
77 -- type of S that has the same name of S, a type-conformant profile, an
78 -- original corresponding operation O that is a primitive of a visible
79 -- ancestor of the dispatching type of S and O is visible at the point of
80 -- of declaration of S. If the entity is found the Alias of S is set to the
81 -- original corresponding operation S and its Overridden_Operation is set
82 -- to the found entity; otherwise return Empty.
84 -- This routine does not search for non-hidden primitives since they are
85 -- covered by the normal Ada 2005 rules.
87 -------------------------------
88 -- Add_Dispatching_Operation --
89 -------------------------------
91 procedure Add_Dispatching_Operation
92 (Tagged_Type
: Entity_Id
;
95 List
: constant Elist_Id
:= Primitive_Operations
(Tagged_Type
);
98 -- The dispatching operation may already be on the list, if it is the
99 -- wrapper for an inherited function of a null extension (see Exp_Ch3
100 -- for the construction of function wrappers). The list of primitive
101 -- operations must not contain duplicates.
103 Append_Unique_Elmt
(New_Op
, List
);
104 end Add_Dispatching_Operation
;
106 ---------------------------
107 -- Covers_Some_Interface --
108 ---------------------------
110 function Covers_Some_Interface
(Prim
: Entity_Id
) return Boolean is
111 Tagged_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Prim
);
116 pragma Assert
(Is_Dispatching_Operation
(Prim
));
118 -- Although this is a dispatching primitive we must check if its
119 -- dispatching type is available because it may be the primitive
120 -- of a private type not defined as tagged in its partial view.
122 if Present
(Tagged_Type
) and then Has_Interfaces
(Tagged_Type
) then
124 -- If the tagged type is frozen then the internal entities associated
125 -- with interfaces are available in the list of primitives of the
126 -- tagged type and can be used to speed up this search.
128 if Is_Frozen
(Tagged_Type
) then
129 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
130 while Present
(Elmt
) loop
133 if Present
(Interface_Alias
(E
))
134 and then Alias
(E
) = Prim
142 -- Otherwise we must collect all the interface primitives and check
143 -- if the Prim will override some interface primitive.
147 Ifaces_List
: Elist_Id
;
148 Iface_Elmt
: Elmt_Id
;
150 Iface_Prim
: Entity_Id
;
153 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
154 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
155 while Present
(Iface_Elmt
) loop
156 Iface
:= Node
(Iface_Elmt
);
158 Elmt
:= First_Elmt
(Primitive_Operations
(Iface
));
159 while Present
(Elmt
) loop
160 Iface_Prim
:= Node
(Elmt
);
162 if Chars
(E
) = Chars
(Prim
)
163 and then Is_Interface_Conformant
164 (Tagged_Type
, Iface_Prim
, Prim
)
172 Next_Elmt
(Iface_Elmt
);
179 end Covers_Some_Interface
;
181 -------------------------------
182 -- Check_Controlling_Formals --
183 -------------------------------
185 procedure Check_Controlling_Formals
190 Ctrl_Type
: Entity_Id
;
193 Formal
:= First_Formal
(Subp
);
194 while Present
(Formal
) loop
195 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
197 if Present
(Ctrl_Type
) then
199 -- When controlling type is concurrent and declared within a
200 -- generic or inside an instance use corresponding record type.
202 if Is_Concurrent_Type
(Ctrl_Type
)
203 and then Present
(Corresponding_Record_Type
(Ctrl_Type
))
205 Ctrl_Type
:= Corresponding_Record_Type
(Ctrl_Type
);
208 if Ctrl_Type
= Typ
then
209 Set_Is_Controlling_Formal
(Formal
);
211 -- Ada 2005 (AI-231): Anonymous access types that are used in
212 -- controlling parameters exclude null because it is necessary
213 -- to read the tag to dispatch, and null has no tag.
215 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
216 Set_Can_Never_Be_Null
(Etype
(Formal
));
217 Set_Is_Known_Non_Null
(Etype
(Formal
));
220 -- Check that the parameter's nominal subtype statically
221 -- matches the first subtype.
223 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
then
224 if not Subtypes_Statically_Match
225 (Typ
, Designated_Type
(Etype
(Formal
)))
228 ("parameter subtype does not match controlling type",
232 elsif not Subtypes_Statically_Match
(Typ
, Etype
(Formal
)) then
234 ("parameter subtype does not match controlling type",
238 if Present
(Default_Value
(Formal
)) then
240 -- In Ada 2005, access parameters can have defaults
242 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
243 and then Ada_Version
< Ada_2005
246 ("default not allowed for controlling access parameter",
247 Default_Value
(Formal
));
249 elsif not Is_Tag_Indeterminate
(Default_Value
(Formal
)) then
251 ("default expression must be a tag indeterminate" &
252 " function call", Default_Value
(Formal
));
256 elsif Comes_From_Source
(Subp
) then
258 ("operation can be dispatching in only one type", Subp
);
262 Next_Formal
(Formal
);
265 if Ekind_In
(Subp
, E_Function
, E_Generic_Function
) then
266 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Subp
), Subp
);
268 if Present
(Ctrl_Type
) then
269 if Ctrl_Type
= Typ
then
270 Set_Has_Controlling_Result
(Subp
);
272 -- Check that result subtype statically matches first subtype
273 -- (Ada 2005): Subp may have a controlling access result.
275 if Subtypes_Statically_Match
(Typ
, Etype
(Subp
))
276 or else (Ekind
(Etype
(Subp
)) = E_Anonymous_Access_Type
278 Subtypes_Statically_Match
279 (Typ
, Designated_Type
(Etype
(Subp
))))
285 ("result subtype does not match controlling type", Subp
);
288 elsif Comes_From_Source
(Subp
) then
290 ("operation can be dispatching in only one type", Subp
);
294 end Check_Controlling_Formals
;
296 ----------------------------
297 -- Check_Controlling_Type --
298 ----------------------------
300 function Check_Controlling_Type
302 Subp
: Entity_Id
) return Entity_Id
304 Tagged_Type
: Entity_Id
:= Empty
;
307 if Is_Tagged_Type
(T
) then
308 if Is_First_Subtype
(T
) then
311 Tagged_Type
:= Base_Type
(T
);
314 elsif Ekind
(T
) = E_Anonymous_Access_Type
315 and then Is_Tagged_Type
(Designated_Type
(T
))
317 if Ekind
(Designated_Type
(T
)) /= E_Incomplete_Type
then
318 if Is_First_Subtype
(Designated_Type
(T
)) then
319 Tagged_Type
:= Designated_Type
(T
);
321 Tagged_Type
:= Base_Type
(Designated_Type
(T
));
324 -- Ada 2005: an incomplete type can be tagged. An operation with an
325 -- access parameter of the type is dispatching.
327 elsif Scope
(Designated_Type
(T
)) = Current_Scope
then
328 Tagged_Type
:= Designated_Type
(T
);
330 -- Ada 2005 (AI-50217)
332 elsif From_With_Type
(Designated_Type
(T
))
333 and then Present
(Non_Limited_View
(Designated_Type
(T
)))
335 if Is_First_Subtype
(Non_Limited_View
(Designated_Type
(T
))) then
336 Tagged_Type
:= Non_Limited_View
(Designated_Type
(T
));
338 Tagged_Type
:= Base_Type
(Non_Limited_View
339 (Designated_Type
(T
)));
344 if No
(Tagged_Type
) or else Is_Class_Wide_Type
(Tagged_Type
) then
347 -- The dispatching type and the primitive operation must be defined in
348 -- the same scope, except in the case of internal operations and formal
349 -- abstract subprograms.
351 elsif ((Scope
(Subp
) = Scope
(Tagged_Type
) or else Is_Internal
(Subp
))
352 and then (not Is_Generic_Type
(Tagged_Type
)
353 or else not Comes_From_Source
(Subp
)))
355 (Is_Formal_Subprogram
(Subp
) and then Is_Abstract_Subprogram
(Subp
))
357 (Nkind
(Parent
(Parent
(Subp
))) = N_Subprogram_Renaming_Declaration
359 Present
(Corresponding_Formal_Spec
(Parent
(Parent
(Subp
))))
361 Is_Abstract_Subprogram
(Subp
))
368 end Check_Controlling_Type
;
370 ----------------------------
371 -- Check_Dispatching_Call --
372 ----------------------------
374 procedure Check_Dispatching_Call
(N
: Node_Id
) is
375 Loc
: constant Source_Ptr
:= Sloc
(N
);
378 Control
: Node_Id
:= Empty
;
380 Subp_Entity
: Entity_Id
;
381 Indeterm_Ancestor_Call
: Boolean := False;
382 Indeterm_Ctrl_Type
: Entity_Id
;
384 Static_Tag
: Node_Id
:= Empty
;
385 -- If a controlling formal has a statically tagged actual, the tag of
386 -- this actual is to be used for any tag-indeterminate actual.
388 procedure Check_Direct_Call
;
389 -- In the case when the controlling actual is a class-wide type whose
390 -- root type's completion is a task or protected type, the call is in
391 -- fact direct. This routine detects the above case and modifies the
394 procedure Check_Dispatching_Context
;
395 -- If the call is tag-indeterminate and the entity being called is
396 -- abstract, verify that the context is a call that will eventually
397 -- provide a tag for dispatching, or has provided one already.
399 -----------------------
400 -- Check_Direct_Call --
401 -----------------------
403 procedure Check_Direct_Call
is
404 Typ
: Entity_Id
:= Etype
(Control
);
406 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean;
407 -- Determine whether an entity denotes a user-defined equality
409 ------------------------------
410 -- Is_User_Defined_Equality --
411 ------------------------------
413 function Is_User_Defined_Equality
(Id
: Entity_Id
) return Boolean is
416 Ekind
(Id
) = E_Function
417 and then Chars
(Id
) = Name_Op_Eq
418 and then Comes_From_Source
(Id
)
420 -- Internally generated equalities have a full type declaration
423 and then Nkind
(Parent
(Id
)) = N_Function_Specification
;
424 end Is_User_Defined_Equality
;
426 -- Start of processing for Check_Direct_Call
429 -- Predefined primitives do not receive wrappers since they are built
430 -- from scratch for the corresponding record of synchronized types.
431 -- Equality is in general predefined, but is excluded from the check
432 -- when it is user-defined.
434 if Is_Predefined_Dispatching_Operation
(Subp_Entity
)
435 and then not Is_User_Defined_Equality
(Subp_Entity
)
440 if Is_Class_Wide_Type
(Typ
) then
441 Typ
:= Root_Type
(Typ
);
444 if Is_Private_Type
(Typ
) and then Present
(Full_View
(Typ
)) then
445 Typ
:= Full_View
(Typ
);
448 if Is_Concurrent_Type
(Typ
)
450 Present
(Corresponding_Record_Type
(Typ
))
452 Typ
:= Corresponding_Record_Type
(Typ
);
454 -- The concurrent record's list of primitives should contain a
455 -- wrapper for the entity of the call, retrieve it.
460 Wrapper_Found
: Boolean := False;
463 Prim_Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
464 while Present
(Prim_Elmt
) loop
465 Prim
:= Node
(Prim_Elmt
);
467 if Is_Primitive_Wrapper
(Prim
)
468 and then Wrapped_Entity
(Prim
) = Subp_Entity
470 Wrapper_Found
:= True;
474 Next_Elmt
(Prim_Elmt
);
477 -- A primitive declared between two views should have a
478 -- corresponding wrapper.
480 pragma Assert
(Wrapper_Found
);
482 -- Modify the call by setting the proper entity
484 Set_Entity
(Name
(N
), Prim
);
487 end Check_Direct_Call
;
489 -------------------------------
490 -- Check_Dispatching_Context --
491 -------------------------------
493 procedure Check_Dispatching_Context
is
494 Subp
: constant Entity_Id
:= Entity
(Name
(N
));
498 if Is_Abstract_Subprogram
(Subp
)
499 and then No
(Controlling_Argument
(N
))
501 if Present
(Alias
(Subp
))
502 and then not Is_Abstract_Subprogram
(Alias
(Subp
))
503 and then No
(DTC_Entity
(Subp
))
505 -- Private overriding of inherited abstract operation, call is
508 Set_Entity
(Name
(N
), Alias
(Subp
));
513 while Present
(Par
) loop
514 if Nkind_In
(Par
, N_Function_Call
,
515 N_Procedure_Call_Statement
,
516 N_Assignment_Statement
,
519 and then Is_Tagged_Type
(Etype
(Subp
))
523 elsif Nkind
(Par
) = N_Qualified_Expression
524 or else Nkind
(Par
) = N_Unchecked_Type_Conversion
529 if Ekind
(Subp
) = E_Function
then
531 ("call to abstract function must be dispatching", N
);
533 -- This error can occur for a procedure in the case of a
534 -- call to an abstract formal procedure with a statically
539 ("call to abstract procedure must be dispatching",
548 end Check_Dispatching_Context
;
550 -- Start of processing for Check_Dispatching_Call
553 -- Find a controlling argument, if any
555 if Present
(Parameter_Associations
(N
)) then
556 Subp_Entity
:= Entity
(Name
(N
));
558 Actual
:= First_Actual
(N
);
559 Formal
:= First_Formal
(Subp_Entity
);
560 while Present
(Actual
) loop
561 Control
:= Find_Controlling_Arg
(Actual
);
562 exit when Present
(Control
);
564 -- Check for the case where the actual is a tag-indeterminate call
565 -- whose result type is different than the tagged type associated
566 -- with the containing call, but is an ancestor of the type.
568 if Is_Controlling_Formal
(Formal
)
569 and then Is_Tag_Indeterminate
(Actual
)
570 and then Base_Type
(Etype
(Actual
)) /= Base_Type
(Etype
(Formal
))
571 and then Is_Ancestor
(Etype
(Actual
), Etype
(Formal
))
573 Indeterm_Ancestor_Call
:= True;
574 Indeterm_Ctrl_Type
:= Etype
(Formal
);
576 -- If the formal is controlling but the actual is not, the type
577 -- of the actual is statically known, and may be used as the
578 -- controlling tag for some other tag-indeterminate actual.
580 elsif Is_Controlling_Formal
(Formal
)
581 and then Is_Entity_Name
(Actual
)
582 and then Is_Tagged_Type
(Etype
(Actual
))
584 Static_Tag
:= Actual
;
587 Next_Actual
(Actual
);
588 Next_Formal
(Formal
);
591 -- If the call doesn't have a controlling actual but does have an
592 -- indeterminate actual that requires dispatching treatment, then an
593 -- object is needed that will serve as the controlling argument for a
594 -- dispatching call on the indeterminate actual. This can only occur
595 -- in the unusual situation of a default actual given by a
596 -- tag-indeterminate call and where the type of the call is an
597 -- ancestor of the type associated with a containing call to an
598 -- inherited operation (see AI-239).
600 -- Rather than create an object of the tagged type, which would be
601 -- problematic for various reasons (default initialization,
602 -- discriminants), the tag of the containing call's associated tagged
603 -- type is directly used to control the dispatching.
606 and then Indeterm_Ancestor_Call
607 and then No
(Static_Tag
)
610 Make_Attribute_Reference
(Loc
,
611 Prefix
=> New_Occurrence_Of
(Indeterm_Ctrl_Type
, Loc
),
612 Attribute_Name
=> Name_Tag
);
617 if Present
(Control
) then
619 -- Verify that no controlling arguments are statically tagged
622 Write_Str
("Found Dispatching call");
627 Actual
:= First_Actual
(N
);
628 while Present
(Actual
) loop
629 if Actual
/= Control
then
631 if not Is_Controlling_Actual
(Actual
) then
632 null; -- Can be anything
634 elsif Is_Dynamically_Tagged
(Actual
) then
635 null; -- Valid parameter
637 elsif Is_Tag_Indeterminate
(Actual
) then
639 -- The tag is inherited from the enclosing call (the node
640 -- we are currently analyzing). Explicitly expand the
641 -- actual, since the previous call to Expand (from
642 -- Resolve_Call) had no way of knowing about the required
645 Propagate_Tag
(Control
, Actual
);
649 ("controlling argument is not dynamically tagged",
655 Next_Actual
(Actual
);
658 -- Mark call as a dispatching call
660 Set_Controlling_Argument
(N
, Control
);
661 Check_Restriction
(No_Dispatching_Calls
, N
);
663 -- The dispatching call may need to be converted into a direct
664 -- call in certain cases.
668 -- If there is a statically tagged actual and a tag-indeterminate
669 -- call to a function of the ancestor (such as that provided by a
670 -- default), then treat this as a dispatching call and propagate
671 -- the tag to the tag-indeterminate call(s).
673 elsif Present
(Static_Tag
) and then Indeterm_Ancestor_Call
then
675 Make_Attribute_Reference
(Loc
,
677 New_Occurrence_Of
(Etype
(Static_Tag
), Loc
),
678 Attribute_Name
=> Name_Tag
);
682 Actual
:= First_Actual
(N
);
683 Formal
:= First_Formal
(Subp_Entity
);
684 while Present
(Actual
) loop
685 if Is_Tag_Indeterminate
(Actual
)
686 and then Is_Controlling_Formal
(Formal
)
688 Propagate_Tag
(Control
, Actual
);
691 Next_Actual
(Actual
);
692 Next_Formal
(Formal
);
695 Check_Dispatching_Context
;
698 -- The call is not dispatching, so check that there aren't any
699 -- tag-indeterminate abstract calls left.
701 Actual
:= First_Actual
(N
);
702 while Present
(Actual
) loop
703 if Is_Tag_Indeterminate
(Actual
) then
705 -- Function call case
707 if Nkind
(Original_Node
(Actual
)) = N_Function_Call
then
708 Func
:= Entity
(Name
(Original_Node
(Actual
)));
710 -- If the actual is an attribute then it can't be abstract
711 -- (the only current case of a tag-indeterminate attribute
712 -- is the stream Input attribute).
715 Nkind
(Original_Node
(Actual
)) = N_Attribute_Reference
719 -- Only other possibility is a qualified expression whose
720 -- constituent expression is itself a call.
726 (Expression
(Original_Node
(Actual
)))));
729 if Present
(Func
) and then Is_Abstract_Subprogram
(Func
) then
731 ("call to abstract function must be dispatching", N
);
735 Next_Actual
(Actual
);
738 Check_Dispatching_Context
;
742 -- If dispatching on result, the enclosing call, if any, will
743 -- determine the controlling argument. Otherwise this is the
744 -- primitive operation of the root type.
746 Check_Dispatching_Context
;
748 end Check_Dispatching_Call
;
750 ---------------------------------
751 -- Check_Dispatching_Operation --
752 ---------------------------------
754 procedure Check_Dispatching_Operation
(Subp
, Old_Subp
: Entity_Id
) is
755 Tagged_Type
: Entity_Id
;
756 Has_Dispatching_Parent
: Boolean := False;
757 Body_Is_Last_Primitive
: Boolean := False;
758 Ovr_Subp
: Entity_Id
:= Empty
;
761 if not Ekind_In
(Subp
, E_Procedure
, E_Function
) then
765 Set_Is_Dispatching_Operation
(Subp
, False);
766 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
768 -- Ada 2005 (AI-345): Use the corresponding record (if available).
769 -- Required because primitives of concurrent types are be attached
770 -- to the corresponding record (not to the concurrent type).
772 if Ada_Version
>= Ada_2005
773 and then Present
(Tagged_Type
)
774 and then Is_Concurrent_Type
(Tagged_Type
)
775 and then Present
(Corresponding_Record_Type
(Tagged_Type
))
777 Tagged_Type
:= Corresponding_Record_Type
(Tagged_Type
);
780 -- (AI-345): The task body procedure is not a primitive of the tagged
783 if Present
(Tagged_Type
)
784 and then Is_Concurrent_Record_Type
(Tagged_Type
)
785 and then Present
(Corresponding_Concurrent_Type
(Tagged_Type
))
786 and then Is_Task_Type
(Corresponding_Concurrent_Type
(Tagged_Type
))
787 and then Subp
= Get_Task_Body_Procedure
788 (Corresponding_Concurrent_Type
(Tagged_Type
))
793 -- If Subp is derived from a dispatching operation then it should
794 -- always be treated as dispatching. In this case various checks
795 -- below will be bypassed. Makes sure that late declarations for
796 -- inherited private subprograms are treated as dispatching, even
797 -- if the associated tagged type is already frozen.
799 Has_Dispatching_Parent
:=
800 Present
(Alias
(Subp
))
801 and then Is_Dispatching_Operation
(Alias
(Subp
));
803 if No
(Tagged_Type
) then
805 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
806 -- with an abstract interface type unless the interface acts as a
807 -- parent type in a derivation. If the interface type is a formal
808 -- type then the operation is not primitive and therefore legal.
815 E
:= First_Entity
(Subp
);
816 while Present
(E
) loop
818 -- For an access parameter, check designated type
820 if Ekind
(Etype
(E
)) = E_Anonymous_Access_Type
then
821 Typ
:= Designated_Type
(Etype
(E
));
826 if Comes_From_Source
(Subp
)
827 and then Is_Interface
(Typ
)
828 and then not Is_Class_Wide_Type
(Typ
)
829 and then not Is_Derived_Type
(Typ
)
830 and then not Is_Generic_Type
(Typ
)
831 and then not In_Instance
833 Error_Msg_N
("?declaration of& is too late!", Subp
);
834 Error_Msg_NE
-- CODEFIX??
835 ("\spec should appear immediately after declaration of &!",
843 -- In case of functions check also the result type
845 if Ekind
(Subp
) = E_Function
then
846 if Is_Access_Type
(Etype
(Subp
)) then
847 Typ
:= Designated_Type
(Etype
(Subp
));
852 if not Is_Class_Wide_Type
(Typ
)
853 and then Is_Interface
(Typ
)
854 and then not Is_Derived_Type
(Typ
)
856 Error_Msg_N
("?declaration of& is too late!", Subp
);
858 ("\spec should appear immediately after declaration of &!",
866 -- The subprograms build internally after the freezing point (such as
867 -- init procs, interface thunks, type support subprograms, and Offset
868 -- to top functions for accessing interface components in variable
869 -- size tagged types) are not primitives.
871 elsif Is_Frozen
(Tagged_Type
)
872 and then not Comes_From_Source
(Subp
)
873 and then not Has_Dispatching_Parent
875 -- Complete decoration of internally built subprograms that override
876 -- a dispatching primitive. These entities correspond with the
879 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
880 -- to override functions of nonabstract null extensions. These
881 -- primitives were added to the list of primitives of the tagged
882 -- type by Make_Controlling_Function_Wrappers. However, attribute
883 -- Is_Dispatching_Operation must be set to true.
885 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
888 -- 3. Subprograms associated with stream attributes (built by
889 -- New_Stream_Subprogram)
891 if Present
(Old_Subp
)
892 and then Present
(Overridden_Operation
(Subp
))
893 and then Is_Dispatching_Operation
(Old_Subp
)
896 ((Ekind
(Subp
) = E_Function
897 and then Is_Dispatching_Operation
(Old_Subp
)
898 and then Is_Null_Extension
(Base_Type
(Etype
(Subp
))))
900 (Ekind
(Subp
) = E_Procedure
901 and then Is_Dispatching_Operation
(Old_Subp
)
902 and then Present
(Alias
(Old_Subp
))
903 and then Is_Null_Interface_Primitive
904 (Ultimate_Alias
(Old_Subp
)))
905 or else Get_TSS_Name
(Subp
) = TSS_Stream_Read
906 or else Get_TSS_Name
(Subp
) = TSS_Stream_Write
);
908 Check_Controlling_Formals
(Tagged_Type
, Subp
);
909 Override_Dispatching_Operation
(Tagged_Type
, Old_Subp
, Subp
);
910 Set_Is_Dispatching_Operation
(Subp
);
915 -- The operation may be a child unit, whose scope is the defining
916 -- package, but which is not a primitive operation of the type.
918 elsif Is_Child_Unit
(Subp
) then
921 -- If the subprogram is not defined in a package spec, the only case
922 -- where it can be a dispatching op is when it overrides an operation
923 -- before the freezing point of the type.
925 elsif ((not Is_Package_Or_Generic_Package
(Scope
(Subp
)))
926 or else In_Package_Body
(Scope
(Subp
)))
927 and then not Has_Dispatching_Parent
929 if not Comes_From_Source
(Subp
)
930 or else (Present
(Old_Subp
) and then not Is_Frozen
(Tagged_Type
))
934 -- If the type is already frozen, the overriding is not allowed
935 -- except when Old_Subp is not a dispatching operation (which can
936 -- occur when Old_Subp was inherited by an untagged type). However,
937 -- a body with no previous spec freezes the type *after* its
938 -- declaration, and therefore is a legal overriding (unless the type
939 -- has already been frozen). Only the first such body is legal.
941 elsif Present
(Old_Subp
)
942 and then Is_Dispatching_Operation
(Old_Subp
)
944 if Comes_From_Source
(Subp
)
946 (Nkind
(Unit_Declaration_Node
(Subp
)) = N_Subprogram_Body
947 or else Nkind
(Unit_Declaration_Node
(Subp
)) in N_Body_Stub
)
950 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
954 -- ??? The checks here for whether the type has been
955 -- frozen prior to the new body are not complete. It's
956 -- not simple to check frozenness at this point since
957 -- the body has already caused the type to be prematurely
958 -- frozen in Analyze_Declarations, but we're forced to
959 -- recheck this here because of the odd rule interpretation
960 -- that allows the overriding if the type wasn't frozen
961 -- prior to the body. The freezing action should probably
962 -- be delayed until after the spec is seen, but that's
963 -- a tricky change to the delicate freezing code.
965 -- Look at each declaration following the type up until the
966 -- new subprogram body. If any of the declarations is a body
967 -- then the type has been frozen already so the overriding
968 -- primitive is illegal.
970 Decl_Item
:= Next
(Parent
(Tagged_Type
));
971 while Present
(Decl_Item
)
972 and then (Decl_Item
/= Subp_Body
)
974 if Comes_From_Source
(Decl_Item
)
975 and then (Nkind
(Decl_Item
) in N_Proper_Body
976 or else Nkind
(Decl_Item
) in N_Body_Stub
)
978 Error_Msg_N
("overriding of& is too late!", Subp
);
980 ("\spec should appear immediately after the type!",
988 -- If the subprogram doesn't follow in the list of
989 -- declarations including the type then the type has
990 -- definitely been frozen already and the body is illegal.
992 if No
(Decl_Item
) then
993 Error_Msg_N
("overriding of& is too late!", Subp
);
995 ("\spec should appear immediately after the type!",
998 elsif Is_Frozen
(Subp
) then
1000 -- The subprogram body declares a primitive operation.
1001 -- if the subprogram is already frozen, we must update
1002 -- its dispatching information explicitly here. The
1003 -- information is taken from the overridden subprogram.
1004 -- We must also generate a cross-reference entry because
1005 -- references to other primitives were already created
1006 -- when type was frozen.
1008 Body_Is_Last_Primitive
:= True;
1010 if Present
(DTC_Entity
(Old_Subp
)) then
1011 Set_DTC_Entity
(Subp
, DTC_Entity
(Old_Subp
));
1012 Set_DT_Position
(Subp
, DT_Position
(Old_Subp
));
1014 if not Restriction_Active
(No_Dispatching_Calls
) then
1015 if Building_Static_DT
(Tagged_Type
) then
1017 -- If the static dispatch table has not been
1018 -- built then there is nothing else to do now;
1019 -- otherwise we notify that we cannot build the
1020 -- static dispatch table.
1022 if Has_Dispatch_Table
(Tagged_Type
) then
1024 ("overriding of& is too late for building" &
1025 " static dispatch tables!", Subp
);
1027 ("\spec should appear immediately after" &
1028 " the type!", Subp
);
1032 Insert_Actions_After
(Subp_Body
,
1033 Register_Primitive
(Sloc
(Subp_Body
),
1037 -- Indicate that this is an overriding operation,
1038 -- and replace the overridden entry in the list of
1039 -- primitive operations, which is used for xref
1040 -- generation subsequently.
1042 Generate_Reference
(Tagged_Type
, Subp
, 'P', False);
1043 Override_Dispatching_Operation
1044 (Tagged_Type
, Old_Subp
, Subp
);
1051 Error_Msg_N
("overriding of& is too late!", Subp
);
1053 ("\subprogram spec should appear immediately after the type!",
1057 -- If the type is not frozen yet and we are not in the overriding
1058 -- case it looks suspiciously like an attempt to define a primitive
1059 -- operation, which requires the declaration to be in a package spec
1060 -- (3.2.3(6)). Only report cases where the type and subprogram are
1061 -- in the same declaration list (by checking the enclosing parent
1062 -- declarations), to avoid spurious warnings on subprograms in
1063 -- instance bodies when the type is declared in the instance spec but
1064 -- hasn't been frozen by the instance body.
1066 elsif not Is_Frozen
(Tagged_Type
)
1067 and then In_Same_List
(Parent
(Tagged_Type
), Parent
(Parent
(Subp
)))
1070 ("?not dispatching (must be defined in a package spec)", Subp
);
1073 -- When the type is frozen, it is legitimate to define a new
1074 -- non-primitive operation.
1080 -- Now, we are sure that the scope is a package spec. If the subprogram
1081 -- is declared after the freezing point of the type that's an error
1083 elsif Is_Frozen
(Tagged_Type
) and then not Has_Dispatching_Parent
then
1084 Error_Msg_N
("this primitive operation is declared too late", Subp
);
1086 ("?no primitive operations for& after this line",
1087 Freeze_Node
(Tagged_Type
),
1092 Check_Controlling_Formals
(Tagged_Type
, Subp
);
1094 Ovr_Subp
:= Old_Subp
;
1096 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1097 -- overridden by Subp
1100 and then Ada_Version
>= Ada_2012
1102 Ovr_Subp
:= Find_Hidden_Overridden_Primitive
(Subp
);
1105 -- Now it should be a correct primitive operation, put it in the list
1107 if Present
(Ovr_Subp
) then
1109 -- If the type has interfaces we complete this check after we set
1110 -- attribute Is_Dispatching_Operation.
1112 Check_Subtype_Conformant
(Subp
, Ovr_Subp
);
1114 if (Chars
(Subp
) = Name_Initialize
1115 or else Chars
(Subp
) = Name_Adjust
1116 or else Chars
(Subp
) = Name_Finalize
)
1117 and then Is_Controlled
(Tagged_Type
)
1118 and then not Is_Visibly_Controlled
(Tagged_Type
)
1120 Set_Overridden_Operation
(Subp
, Empty
);
1122 -- If the subprogram specification carries an overriding
1123 -- indicator, no need for the warning: it is either redundant,
1124 -- or else an error will be reported.
1126 if Nkind
(Parent
(Subp
)) = N_Procedure_Specification
1128 (Must_Override
(Parent
(Subp
))
1129 or else Must_Not_Override
(Parent
(Subp
)))
1133 -- Here we need the warning
1137 ("operation does not override inherited&?", Subp
, Subp
);
1141 Override_Dispatching_Operation
(Tagged_Type
, Ovr_Subp
, Subp
);
1143 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1144 -- that covers abstract interface subprograms we must register it
1145 -- in all the secondary dispatch tables associated with abstract
1146 -- interfaces. We do this now only if not building static tables.
1147 -- Otherwise the patch code is emitted after those tables are
1148 -- built, to prevent access_before_elaboration in gigi.
1150 if Body_Is_Last_Primitive
then
1152 Subp_Body
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
1157 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1158 while Present
(Elmt
) loop
1159 Prim
:= Node
(Elmt
);
1161 if Present
(Alias
(Prim
))
1162 and then Present
(Interface_Alias
(Prim
))
1163 and then Alias
(Prim
) = Subp
1164 and then not Building_Static_DT
(Tagged_Type
)
1166 Insert_Actions_After
(Subp_Body
,
1167 Register_Primitive
(Sloc
(Subp_Body
), Prim
=> Prim
));
1173 -- Redisplay the contents of the updated dispatch table
1175 if Debug_Flag_ZZ
then
1176 Write_Str
("Late overriding: ");
1177 Write_DT
(Tagged_Type
);
1183 -- If the tagged type is a concurrent type then we must be compiling
1184 -- with no code generation (we are either compiling a generic unit or
1185 -- compiling under -gnatc mode) because we have previously tested that
1186 -- no serious errors has been reported. In this case we do not add the
1187 -- primitive to the list of primitives of Tagged_Type but we leave the
1188 -- primitive decorated as a dispatching operation to be able to analyze
1189 -- and report errors associated with the Object.Operation notation.
1191 elsif Is_Concurrent_Type
(Tagged_Type
) then
1192 pragma Assert
(not Expander_Active
);
1195 -- If no old subprogram, then we add this as a dispatching operation,
1196 -- but we avoid doing this if an error was posted, to prevent annoying
1199 elsif not Error_Posted
(Subp
) then
1200 Add_Dispatching_Operation
(Tagged_Type
, Subp
);
1203 Set_Is_Dispatching_Operation
(Subp
, True);
1205 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1206 -- subtype conformance against all the interfaces covered by this
1209 if Present
(Ovr_Subp
)
1210 and then Has_Interfaces
(Tagged_Type
)
1213 Ifaces_List
: Elist_Id
;
1214 Iface_Elmt
: Elmt_Id
;
1215 Iface_Prim_Elmt
: Elmt_Id
;
1216 Iface_Prim
: Entity_Id
;
1217 Ret_Typ
: Entity_Id
;
1220 Collect_Interfaces
(Tagged_Type
, Ifaces_List
);
1222 Iface_Elmt
:= First_Elmt
(Ifaces_List
);
1223 while Present
(Iface_Elmt
) loop
1224 if not Is_Ancestor
(Node
(Iface_Elmt
), Tagged_Type
) then
1226 First_Elmt
(Primitive_Operations
(Node
(Iface_Elmt
)));
1227 while Present
(Iface_Prim_Elmt
) loop
1228 Iface_Prim
:= Node
(Iface_Prim_Elmt
);
1230 if Is_Interface_Conformant
1231 (Tagged_Type
, Iface_Prim
, Subp
)
1233 -- Handle procedures, functions whose return type
1234 -- matches, or functions not returning interfaces
1236 if Ekind
(Subp
) = E_Procedure
1237 or else Etype
(Iface_Prim
) = Etype
(Subp
)
1238 or else not Is_Interface
(Etype
(Iface_Prim
))
1240 Check_Subtype_Conformant
1242 Old_Id
=> Iface_Prim
,
1244 Skip_Controlling_Formals
=> True);
1246 -- Handle functions returning interfaces
1248 elsif Implements_Interface
1249 (Etype
(Subp
), Etype
(Iface_Prim
))
1251 -- Temporarily force both entities to return the
1252 -- same type. Required because Subtype_Conformant
1253 -- does not handle this case.
1255 Ret_Typ
:= Etype
(Iface_Prim
);
1256 Set_Etype
(Iface_Prim
, Etype
(Subp
));
1258 Check_Subtype_Conformant
1260 Old_Id
=> Iface_Prim
,
1262 Skip_Controlling_Formals
=> True);
1264 Set_Etype
(Iface_Prim
, Ret_Typ
);
1268 Next_Elmt
(Iface_Prim_Elmt
);
1272 Next_Elmt
(Iface_Elmt
);
1277 if not Body_Is_Last_Primitive
then
1278 Set_DT_Position
(Subp
, No_Uint
);
1280 elsif Has_Controlled_Component
(Tagged_Type
)
1282 (Chars
(Subp
) = Name_Initialize
1284 Chars
(Subp
) = Name_Adjust
1286 Chars
(Subp
) = Name_Finalize
)
1289 F_Node
: constant Node_Id
:= Freeze_Node
(Tagged_Type
);
1293 Old_Spec
: Entity_Id
;
1295 C_Names
: constant array (1 .. 3) of Name_Id
:=
1300 D_Names
: constant array (1 .. 3) of TSS_Name_Type
:=
1301 (TSS_Deep_Initialize
,
1306 -- Remove previous controlled function which was constructed and
1307 -- analyzed when the type was frozen. This requires removing the
1308 -- body of the redefined primitive, as well as its specification
1309 -- if needed (there is no spec created for Deep_Initialize, see
1310 -- exp_ch3.adb). We must also dismantle the exception information
1311 -- that may have been generated for it when front end zero-cost
1312 -- tables are enabled.
1314 for J
in D_Names
'Range loop
1315 Old_P
:= TSS
(Tagged_Type
, D_Names
(J
));
1318 and then Chars
(Subp
) = C_Names
(J
)
1320 Old_Bod
:= Unit_Declaration_Node
(Old_P
);
1322 Set_Is_Eliminated
(Old_P
);
1323 Set_Scope
(Old_P
, Scope
(Current_Scope
));
1325 if Nkind
(Old_Bod
) = N_Subprogram_Body
1326 and then Present
(Corresponding_Spec
(Old_Bod
))
1328 Old_Spec
:= Corresponding_Spec
(Old_Bod
);
1329 Set_Has_Completion
(Old_Spec
, False);
1334 Build_Late_Proc
(Tagged_Type
, Chars
(Subp
));
1336 -- The new operation is added to the actions of the freeze node
1337 -- for the type, but this node has already been analyzed, so we
1338 -- must retrieve and analyze explicitly the new body.
1341 and then Present
(Actions
(F_Node
))
1343 Decl
:= Last
(Actions
(F_Node
));
1348 end Check_Dispatching_Operation
;
1350 ------------------------------------------
1351 -- Check_Operation_From_Incomplete_Type --
1352 ------------------------------------------
1354 procedure Check_Operation_From_Incomplete_Type
1358 Full
: constant Entity_Id
:= Full_View
(Typ
);
1359 Parent_Typ
: constant Entity_Id
:= Etype
(Full
);
1360 Old_Prim
: constant Elist_Id
:= Primitive_Operations
(Parent_Typ
);
1361 New_Prim
: constant Elist_Id
:= Primitive_Operations
(Full
);
1363 Prev
: Elmt_Id
:= No_Elmt
;
1365 function Derives_From
(Proc
: Entity_Id
) return Boolean;
1366 -- Check that Subp has the signature of an operation derived from Proc.
1367 -- Subp has an access parameter that designates Typ.
1373 function Derives_From
(Proc
: Entity_Id
) return Boolean is
1377 if Chars
(Proc
) /= Chars
(Subp
) then
1381 F1
:= First_Formal
(Proc
);
1382 F2
:= First_Formal
(Subp
);
1383 while Present
(F1
) and then Present
(F2
) loop
1384 if Ekind
(Etype
(F1
)) = E_Anonymous_Access_Type
then
1385 if Ekind
(Etype
(F2
)) /= E_Anonymous_Access_Type
then
1387 elsif Designated_Type
(Etype
(F1
)) = Parent_Typ
1388 and then Designated_Type
(Etype
(F2
)) /= Full
1393 elsif Ekind
(Etype
(F2
)) = E_Anonymous_Access_Type
then
1396 elsif Etype
(F1
) /= Etype
(F2
) then
1404 return No
(F1
) and then No
(F2
);
1407 -- Start of processing for Check_Operation_From_Incomplete_Type
1410 -- The operation may override an inherited one, or may be a new one
1411 -- altogether. The inherited operation will have been hidden by the
1412 -- current one at the point of the type derivation, so it does not
1413 -- appear in the list of primitive operations of the type. We have to
1414 -- find the proper place of insertion in the list of primitive opera-
1415 -- tions by iterating over the list for the parent type.
1417 Op1
:= First_Elmt
(Old_Prim
);
1418 Op2
:= First_Elmt
(New_Prim
);
1419 while Present
(Op1
) and then Present
(Op2
) loop
1420 if Derives_From
(Node
(Op1
)) then
1423 -- Avoid adding it to the list of primitives if already there!
1425 if Node
(Op2
) /= Subp
then
1426 Prepend_Elmt
(Subp
, New_Prim
);
1430 Insert_Elmt_After
(Subp
, Prev
);
1441 -- Operation is a new primitive
1443 Append_Elmt
(Subp
, New_Prim
);
1444 end Check_Operation_From_Incomplete_Type
;
1446 ---------------------------------------
1447 -- Check_Operation_From_Private_View --
1448 ---------------------------------------
1450 procedure Check_Operation_From_Private_View
(Subp
, Old_Subp
: Entity_Id
) is
1451 Tagged_Type
: Entity_Id
;
1454 if Is_Dispatching_Operation
(Alias
(Subp
)) then
1455 Set_Scope
(Subp
, Current_Scope
);
1456 Tagged_Type
:= Find_Dispatching_Type
(Subp
);
1458 -- Add Old_Subp to primitive operations if not already present
1460 if Present
(Tagged_Type
) and then Is_Tagged_Type
(Tagged_Type
) then
1461 Append_Unique_Elmt
(Old_Subp
, Primitive_Operations
(Tagged_Type
));
1463 -- If Old_Subp isn't already marked as dispatching then
1464 -- this is the case of an operation of an untagged private
1465 -- type fulfilled by a tagged type that overrides an
1466 -- inherited dispatching operation, so we set the necessary
1467 -- dispatching attributes here.
1469 if not Is_Dispatching_Operation
(Old_Subp
) then
1471 -- If the untagged type has no discriminants, and the full
1472 -- view is constrained, there will be a spurious mismatch
1473 -- of subtypes on the controlling arguments, because the tagged
1474 -- type is the internal base type introduced in the derivation.
1475 -- Use the original type to verify conformance, rather than the
1478 if not Comes_From_Source
(Tagged_Type
)
1479 and then Has_Discriminants
(Tagged_Type
)
1485 Formal
:= First_Formal
(Old_Subp
);
1486 while Present
(Formal
) loop
1487 if Tagged_Type
= Base_Type
(Etype
(Formal
)) then
1488 Tagged_Type
:= Etype
(Formal
);
1491 Next_Formal
(Formal
);
1495 if Tagged_Type
= Base_Type
(Etype
(Old_Subp
)) then
1496 Tagged_Type
:= Etype
(Old_Subp
);
1500 Check_Controlling_Formals
(Tagged_Type
, Old_Subp
);
1501 Set_Is_Dispatching_Operation
(Old_Subp
, True);
1502 Set_DT_Position
(Old_Subp
, No_Uint
);
1505 -- If the old subprogram is an explicit renaming of some other
1506 -- entity, it is not overridden by the inherited subprogram.
1507 -- Otherwise, update its alias and other attributes.
1509 if Present
(Alias
(Old_Subp
))
1510 and then Nkind
(Unit_Declaration_Node
(Old_Subp
)) /=
1511 N_Subprogram_Renaming_Declaration
1513 Set_Alias
(Old_Subp
, Alias
(Subp
));
1515 -- The derived subprogram should inherit the abstractness
1516 -- of the parent subprogram (except in the case of a function
1517 -- returning the type). This sets the abstractness properly
1518 -- for cases where a private extension may have inherited
1519 -- an abstract operation, but the full type is derived from
1520 -- a descendant type and inherits a nonabstract version.
1522 if Etype
(Subp
) /= Tagged_Type
then
1523 Set_Is_Abstract_Subprogram
1524 (Old_Subp
, Is_Abstract_Subprogram
(Alias
(Subp
)));
1529 end Check_Operation_From_Private_View
;
1531 --------------------------
1532 -- Find_Controlling_Arg --
1533 --------------------------
1535 function Find_Controlling_Arg
(N
: Node_Id
) return Node_Id
is
1536 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1540 if Nkind
(Orig_Node
) = N_Qualified_Expression
then
1541 return Find_Controlling_Arg
(Expression
(Orig_Node
));
1544 -- Dispatching on result case. If expansion is disabled, the node still
1545 -- has the structure of a function call. However, if the function name
1546 -- is an operator and the call was given in infix form, the original
1547 -- node has no controlling result and we must examine the current node.
1549 if Nkind
(N
) = N_Function_Call
1550 and then Present
(Controlling_Argument
(N
))
1551 and then Has_Controlling_Result
(Entity
(Name
(N
)))
1553 return Controlling_Argument
(N
);
1555 -- If expansion is enabled, the call may have been transformed into
1556 -- an indirect call, and we need to recover the original node.
1558 elsif Nkind
(Orig_Node
) = N_Function_Call
1559 and then Present
(Controlling_Argument
(Orig_Node
))
1560 and then Has_Controlling_Result
(Entity
(Name
(Orig_Node
)))
1562 return Controlling_Argument
(Orig_Node
);
1566 elsif Is_Controlling_Actual
(N
)
1568 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1569 and then Is_Controlling_Actual
(Parent
(N
)))
1573 if Is_Access_Type
(Typ
) then
1575 -- In the case of an Access attribute, use the type of the prefix,
1576 -- since in the case of an actual for an access parameter, the
1577 -- attribute's type may be of a specific designated type, even
1578 -- though the prefix type is class-wide.
1580 if Nkind
(N
) = N_Attribute_Reference
then
1581 Typ
:= Etype
(Prefix
(N
));
1583 -- An allocator is dispatching if the type of qualified expression
1584 -- is class_wide, in which case this is the controlling type.
1586 elsif Nkind
(Orig_Node
) = N_Allocator
1587 and then Nkind
(Expression
(Orig_Node
)) = N_Qualified_Expression
1589 Typ
:= Etype
(Expression
(Orig_Node
));
1591 Typ
:= Designated_Type
(Typ
);
1595 if Is_Class_Wide_Type
(Typ
)
1597 (Nkind
(Parent
(N
)) = N_Qualified_Expression
1598 and then Is_Access_Type
(Etype
(N
))
1599 and then Is_Class_Wide_Type
(Designated_Type
(Etype
(N
))))
1606 end Find_Controlling_Arg
;
1608 ---------------------------
1609 -- Find_Dispatching_Type --
1610 ---------------------------
1612 function Find_Dispatching_Type
(Subp
: Entity_Id
) return Entity_Id
is
1613 A_Formal
: Entity_Id
;
1615 Ctrl_Type
: Entity_Id
;
1618 if Present
(DTC_Entity
(Subp
)) then
1619 return Scope
(DTC_Entity
(Subp
));
1621 -- For subprograms internally generated by derivations of tagged types
1622 -- use the alias subprogram as a reference to locate the dispatching
1625 elsif not Comes_From_Source
(Subp
)
1626 and then Present
(Alias
(Subp
))
1627 and then Is_Dispatching_Operation
(Alias
(Subp
))
1629 if Ekind
(Alias
(Subp
)) = E_Function
1630 and then Has_Controlling_Result
(Alias
(Subp
))
1632 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1635 Formal
:= First_Formal
(Subp
);
1636 A_Formal
:= First_Formal
(Alias
(Subp
));
1637 while Present
(A_Formal
) loop
1638 if Is_Controlling_Formal
(A_Formal
) then
1639 return Check_Controlling_Type
(Etype
(Formal
), Subp
);
1642 Next_Formal
(Formal
);
1643 Next_Formal
(A_Formal
);
1646 pragma Assert
(False);
1653 Formal
:= First_Formal
(Subp
);
1654 while Present
(Formal
) loop
1655 Ctrl_Type
:= Check_Controlling_Type
(Etype
(Formal
), Subp
);
1657 if Present
(Ctrl_Type
) then
1661 Next_Formal
(Formal
);
1664 -- The subprogram may also be dispatching on result
1666 if Present
(Etype
(Subp
)) then
1667 return Check_Controlling_Type
(Etype
(Subp
), Subp
);
1671 pragma Assert
(not Is_Dispatching_Operation
(Subp
));
1673 end Find_Dispatching_Type
;
1675 --------------------------------------
1676 -- Find_Hidden_Overridden_Primitive --
1677 --------------------------------------
1679 function Find_Hidden_Overridden_Primitive
(S
: Entity_Id
) return Entity_Id
1681 Tag_Typ
: constant Entity_Id
:= Find_Dispatching_Type
(S
);
1683 Orig_Prim
: Entity_Id
;
1685 Vis_List
: Elist_Id
;
1688 -- This Ada 2012 rule is valid only for type extensions or private
1692 or else not Is_Record_Type
(Tag_Typ
)
1693 or else Etype
(Tag_Typ
) = Tag_Typ
1698 -- Collect the list of visible ancestor of the tagged type
1700 Vis_List
:= Visible_Ancestors
(Tag_Typ
);
1702 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
1703 while Present
(Elmt
) loop
1704 Prim
:= Node
(Elmt
);
1706 -- Find an inherited hidden dispatching primitive with the name of S
1707 -- and a type-conformant profile.
1709 if Present
(Alias
(Prim
))
1710 and then Is_Hidden
(Alias
(Prim
))
1711 and then Find_Dispatching_Type
(Alias
(Prim
)) /= Tag_Typ
1712 and then Primitive_Names_Match
(S
, Prim
)
1713 and then Type_Conformant
(S
, Prim
)
1716 Vis_Ancestor
: Elmt_Id
;
1720 -- The original corresponding operation of Prim must be an
1721 -- operation of a visible ancestor of the dispatching type
1722 -- S, and the original corresponding operation of S2 must
1725 Orig_Prim
:= Original_Corresponding_Operation
(Prim
);
1727 if Orig_Prim
/= Prim
1728 and then Is_Immediately_Visible
(Orig_Prim
)
1730 Vis_Ancestor
:= First_Elmt
(Vis_List
);
1731 while Present
(Vis_Ancestor
) loop
1733 First_Elmt
(Primitive_Operations
(Node
(Vis_Ancestor
)));
1734 while Present
(Elmt
) loop
1735 if Node
(Elmt
) = Orig_Prim
then
1736 Set_Overridden_Operation
(S
, Prim
);
1737 Set_Alias
(Prim
, Orig_Prim
);
1744 Next_Elmt
(Vis_Ancestor
);
1754 end Find_Hidden_Overridden_Primitive
;
1756 ---------------------------------------
1757 -- Find_Primitive_Covering_Interface --
1758 ---------------------------------------
1760 function Find_Primitive_Covering_Interface
1761 (Tagged_Type
: Entity_Id
;
1762 Iface_Prim
: Entity_Id
) return Entity_Id
1768 pragma Assert
(Is_Interface
(Find_Dispatching_Type
(Iface_Prim
))
1769 or else (Present
(Alias
(Iface_Prim
))
1772 (Find_Dispatching_Type
(Ultimate_Alias
(Iface_Prim
)))));
1774 -- Search in the homonym chain. Done to speed up locating visible
1775 -- entities and required to catch primitives associated with the partial
1776 -- view of private types when processing the corresponding full view.
1778 E
:= Current_Entity
(Iface_Prim
);
1779 while Present
(E
) loop
1780 if Is_Subprogram
(E
)
1781 and then Is_Dispatching_Operation
(E
)
1782 and then Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
)
1790 -- Search in the list of primitives of the type. Required to locate the
1791 -- covering primitive if the covering primitive is not visible (for
1792 -- example, non-visible inherited primitive of private type).
1794 El
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
1795 while Present
(El
) loop
1798 -- Keep separate the management of internal entities that link
1799 -- primitives with interface primitives from tagged type primitives.
1801 if No
(Interface_Alias
(E
)) then
1802 if Present
(Alias
(E
)) then
1804 -- This interface primitive has not been covered yet
1806 if Alias
(E
) = Iface_Prim
then
1809 -- The covering primitive was inherited
1811 elsif Overridden_Operation
(Ultimate_Alias
(E
))
1818 -- Check if E covers the interface primitive (includes case in
1819 -- which E is an inherited private primitive).
1821 if Is_Interface_Conformant
(Tagged_Type
, Iface_Prim
, E
) then
1825 -- Use the internal entity that links the interface primitive with
1826 -- the covering primitive to locate the entity.
1828 elsif Interface_Alias
(E
) = Iface_Prim
then
1838 end Find_Primitive_Covering_Interface
;
1840 ---------------------------
1841 -- Inherited_Subprograms --
1842 ---------------------------
1844 function Inherited_Subprograms
(S
: Entity_Id
) return Subprogram_List
is
1845 Result
: Subprogram_List
(1 .. 6000);
1846 -- 6000 here is intended to be infinity. We could use an expandable
1847 -- table, but it would be awfully heavy, and there is no way that we
1848 -- could reasonably exceed this value.
1851 -- Number of entries in Result
1853 Parent_Op
: Entity_Id
;
1854 -- Traverses the Overridden_Operation chain
1856 procedure Store_IS
(E
: Entity_Id
);
1857 -- Stores E in Result if not already stored
1863 procedure Store_IS
(E
: Entity_Id
) is
1865 for J
in 1 .. N
loop
1866 if E
= Result
(J
) then
1875 -- Start of processing for Inherited_Subprograms
1878 if Present
(S
) and then Is_Dispatching_Operation
(S
) then
1880 -- Deal with direct inheritance
1884 Parent_Op
:= Overridden_Operation
(Parent_Op
);
1885 exit when No
(Parent_Op
);
1887 if Is_Subprogram
(Parent_Op
)
1888 or else Is_Generic_Subprogram
(Parent_Op
)
1890 Store_IS
(Parent_Op
);
1894 -- Now deal with interfaces
1897 Tag_Typ
: Entity_Id
;
1902 Tag_Typ
:= Find_Dispatching_Type
(S
);
1904 if Is_Concurrent_Type
(Tag_Typ
) then
1905 Tag_Typ
:= Corresponding_Record_Type
(Tag_Typ
);
1908 -- Search primitive operations of dispatching type
1910 if Present
(Tag_Typ
)
1911 and then Present
(Primitive_Operations
(Tag_Typ
))
1913 Elmt
:= First_Elmt
(Primitive_Operations
(Tag_Typ
));
1914 while Present
(Elmt
) loop
1915 Prim
:= Node
(Elmt
);
1917 -- The following test eliminates some odd cases in which
1918 -- Ekind (Prim) is Void, to be investigated further ???
1920 if not (Is_Subprogram
(Prim
)
1922 Is_Generic_Subprogram
(Prim
))
1926 -- For [generic] subprogram, look at interface alias
1928 elsif Present
(Interface_Alias
(Prim
))
1929 and then Alias
(Prim
) = S
1931 -- We have found a primitive covered by S
1933 Store_IS
(Interface_Alias
(Prim
));
1942 return Result
(1 .. N
);
1943 end Inherited_Subprograms
;
1945 ---------------------------
1946 -- Is_Dynamically_Tagged --
1947 ---------------------------
1949 function Is_Dynamically_Tagged
(N
: Node_Id
) return Boolean is
1951 if Nkind
(N
) = N_Error
then
1954 return Find_Controlling_Arg
(N
) /= Empty
;
1956 end Is_Dynamically_Tagged
;
1958 ---------------------------------
1959 -- Is_Null_Interface_Primitive --
1960 ---------------------------------
1962 function Is_Null_Interface_Primitive
(E
: Entity_Id
) return Boolean is
1964 return Comes_From_Source
(E
)
1965 and then Is_Dispatching_Operation
(E
)
1966 and then Ekind
(E
) = E_Procedure
1967 and then Null_Present
(Parent
(E
))
1968 and then Is_Interface
(Find_Dispatching_Type
(E
));
1969 end Is_Null_Interface_Primitive
;
1971 --------------------------
1972 -- Is_Tag_Indeterminate --
1973 --------------------------
1975 function Is_Tag_Indeterminate
(N
: Node_Id
) return Boolean is
1978 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
1981 if Nkind
(Orig_Node
) = N_Function_Call
1982 and then Is_Entity_Name
(Name
(Orig_Node
))
1984 Nam
:= Entity
(Name
(Orig_Node
));
1986 if not Has_Controlling_Result
(Nam
) then
1989 -- An explicit dereference means that the call has already been
1990 -- expanded and there is no tag to propagate.
1992 elsif Nkind
(N
) = N_Explicit_Dereference
then
1995 -- If there are no actuals, the call is tag-indeterminate
1997 elsif No
(Parameter_Associations
(Orig_Node
)) then
2001 Actual
:= First_Actual
(Orig_Node
);
2002 while Present
(Actual
) loop
2003 if Is_Controlling_Actual
(Actual
)
2004 and then not Is_Tag_Indeterminate
(Actual
)
2006 return False; -- one operand is dispatching
2009 Next_Actual
(Actual
);
2015 elsif Nkind
(Orig_Node
) = N_Qualified_Expression
then
2016 return Is_Tag_Indeterminate
(Expression
(Orig_Node
));
2018 -- Case of a call to the Input attribute (possibly rewritten), which is
2019 -- always tag-indeterminate except when its prefix is a Class attribute.
2021 elsif Nkind
(Orig_Node
) = N_Attribute_Reference
2023 Get_Attribute_Id
(Attribute_Name
(Orig_Node
)) = Attribute_Input
2025 Nkind
(Prefix
(Orig_Node
)) /= N_Attribute_Reference
2029 -- In Ada 2005 a function that returns an anonymous access type can
2030 -- dispatching, and the dereference of a call to such a function
2031 -- is also tag-indeterminate.
2033 elsif Nkind
(Orig_Node
) = N_Explicit_Dereference
2034 and then Ada_Version
>= Ada_2005
2036 return Is_Tag_Indeterminate
(Prefix
(Orig_Node
));
2041 end Is_Tag_Indeterminate
;
2043 ------------------------------------
2044 -- Override_Dispatching_Operation --
2045 ------------------------------------
2047 procedure Override_Dispatching_Operation
2048 (Tagged_Type
: Entity_Id
;
2049 Prev_Op
: Entity_Id
;
2056 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2057 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
2059 if No_Return
(Prev_Op
) and then not No_Return
(New_Op
) then
2060 Error_Msg_N
("procedure & must have No_Return pragma", New_Op
);
2061 Error_Msg_N
("\since overridden procedure has No_Return", New_Op
);
2064 -- If there is no previous operation to override, the type declaration
2065 -- was malformed, and an error must have been emitted already.
2067 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2068 while Present
(Elmt
)
2069 and then Node
(Elmt
) /= Prev_Op
2078 -- The location of entities that come from source in the list of
2079 -- primitives of the tagged type must follow their order of occurrence
2080 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2081 -- primitive of an interface that is not an ancestor of this tagged
2082 -- type (that is, it is an entity added to the list of primitives by
2083 -- Derive_Interface_Progenitors), then we must append the new entity
2084 -- at the end of the list of primitives.
2086 if Present
(Alias
(Prev_Op
))
2087 and then Is_Interface
(Find_Dispatching_Type
(Alias
(Prev_Op
)))
2088 and then not Is_Ancestor
(Find_Dispatching_Type
(Alias
(Prev_Op
)),
2091 Remove_Elmt
(Primitive_Operations
(Tagged_Type
), Elmt
);
2092 Append_Elmt
(New_Op
, Primitive_Operations
(Tagged_Type
));
2094 -- The new primitive replaces the overridden entity. Required to ensure
2095 -- that overriding primitive is assigned the same dispatch table slot.
2098 Replace_Elmt
(Elmt
, New_Op
);
2101 if Ada_Version
>= Ada_2005
2102 and then Has_Interfaces
(Tagged_Type
)
2104 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2105 -- entities of the overridden primitive to reference New_Op, and also
2106 -- propagate the proper value of Is_Abstract_Subprogram. Verify
2107 -- that the new operation is subtype conformant with the interface
2108 -- operations that it implements (for operations inherited from the
2109 -- parent itself, this check is made when building the derived type).
2111 -- Note: This code is only executed in case of late overriding
2113 Elmt
:= First_Elmt
(Primitive_Operations
(Tagged_Type
));
2114 while Present
(Elmt
) loop
2115 Prim
:= Node
(Elmt
);
2117 if Prim
= New_Op
then
2120 -- Note: The check on Is_Subprogram protects the frontend against
2121 -- reading attributes in entities that are not yet fully decorated
2123 elsif Is_Subprogram
(Prim
)
2124 and then Present
(Interface_Alias
(Prim
))
2125 and then Alias
(Prim
) = Prev_Op
2126 and then Present
(Etype
(New_Op
))
2128 Set_Alias
(Prim
, New_Op
);
2129 Check_Subtype_Conformant
(New_Op
, Prim
);
2130 Set_Is_Abstract_Subprogram
(Prim
,
2131 Is_Abstract_Subprogram
(New_Op
));
2133 -- Ensure that this entity will be expanded to fill the
2134 -- corresponding entry in its dispatch table.
2136 if not Is_Abstract_Subprogram
(Prim
) then
2137 Set_Has_Delayed_Freeze
(Prim
);
2145 if (not Is_Package_Or_Generic_Package
(Current_Scope
))
2146 or else not In_Private_Part
(Current_Scope
)
2148 -- Not a private primitive
2152 else pragma Assert
(Is_Inherited_Operation
(Prev_Op
));
2154 -- Make the overriding operation into an alias of the implicit one.
2155 -- In this fashion a call from outside ends up calling the new body
2156 -- even if non-dispatching, and a call from inside calls the over-
2157 -- riding operation because it hides the implicit one. To indicate
2158 -- that the body of Prev_Op is never called, set its dispatch table
2159 -- entity to Empty. If the overridden operation has a dispatching
2160 -- result, so does the overriding one.
2162 Set_Alias
(Prev_Op
, New_Op
);
2163 Set_DTC_Entity
(Prev_Op
, Empty
);
2164 Set_Has_Controlling_Result
(New_Op
, Has_Controlling_Result
(Prev_Op
));
2167 end Override_Dispatching_Operation
;
2173 procedure Propagate_Tag
(Control
: Node_Id
; Actual
: Node_Id
) is
2174 Call_Node
: Node_Id
;
2178 if Nkind
(Actual
) = N_Function_Call
then
2179 Call_Node
:= Actual
;
2181 elsif Nkind
(Actual
) = N_Identifier
2182 and then Nkind
(Original_Node
(Actual
)) = N_Function_Call
2184 -- Call rewritten as object declaration when stack-checking is
2185 -- enabled. Propagate tag to expression in declaration, which is
2188 Call_Node
:= Expression
(Parent
(Entity
(Actual
)));
2190 -- Ada 2005: If this is a dereference of a call to a function with a
2191 -- dispatching access-result, the tag is propagated when the dereference
2192 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2194 elsif Nkind
(Actual
) = N_Explicit_Dereference
2195 and then Nkind
(Original_Node
(Prefix
(Actual
))) = N_Function_Call
2199 -- Only other possibilities are parenthesized or qualified expression,
2200 -- or an expander-generated unchecked conversion of a function call to
2201 -- a stream Input attribute.
2204 Call_Node
:= Expression
(Actual
);
2207 -- Do not set the Controlling_Argument if already set. This happens in
2208 -- the special case of _Input (see Exp_Attr, case Input).
2210 if No
(Controlling_Argument
(Call_Node
)) then
2211 Set_Controlling_Argument
(Call_Node
, Control
);
2214 Arg
:= First_Actual
(Call_Node
);
2215 while Present
(Arg
) loop
2216 if Is_Tag_Indeterminate
(Arg
) then
2217 Propagate_Tag
(Control
, Arg
);
2223 -- Expansion of dispatching calls is suppressed when VM_Target, because
2224 -- the VM back-ends directly handle the generation of dispatching calls
2225 -- and would have to undo any expansion to an indirect call.
2227 if Tagged_Type_Expansion
then
2229 Call_Typ
: constant Entity_Id
:= Etype
(Call_Node
);
2232 Expand_Dispatching_Call
(Call_Node
);
2234 -- If the controlling argument is an interface type and the type
2235 -- of Call_Node differs then we must add an implicit conversion to
2236 -- force displacement of the pointer to the object to reference
2237 -- the secondary dispatch table of the interface.
2239 if Is_Interface
(Etype
(Control
))
2240 and then Etype
(Control
) /= Call_Typ
2242 -- Cannot use Convert_To because the previous call to
2243 -- Expand_Dispatching_Call leaves decorated the Call_Node
2244 -- with the type of Control.
2247 Make_Type_Conversion
(Sloc
(Call_Node
),
2249 New_Occurrence_Of
(Etype
(Control
), Sloc
(Call_Node
)),
2250 Expression
=> Relocate_Node
(Call_Node
)));
2251 Set_Etype
(Call_Node
, Etype
(Control
));
2252 Set_Analyzed
(Call_Node
);
2254 Expand_Interface_Conversion
(Call_Node
, Is_Static
=> False);
2258 -- Expansion of a dispatching call results in an indirect call, which in
2259 -- turn causes current values to be killed (see Resolve_Call), so on VM
2260 -- targets we do the call here to ensure consistent warnings between VM
2261 -- and non-VM targets.
2264 Kill_Current_Values
;