1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Elists; use Elists;
29 with Einfo; use Einfo;
30 with Exp_Disp; use Exp_Disp;
31 with Exp_Ch7; use Exp_Ch7;
32 with Exp_Tss; use Exp_Tss;
33 with Errout; use Errout;
34 with Lib.Xref; use Lib.Xref;
35 with Namet; use Namet;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
39 with Output; use Output;
40 with Restrict; use Restrict;
41 with Rident; use Rident;
43 with Sem_Ch6; use Sem_Ch6;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Type; use Sem_Type;
46 with Sem_Util; use Sem_Util;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Sinfo; use Sinfo;
50 with Targparm; use Targparm;
51 with Tbuild; use Tbuild;
52 with Uintp; use Uintp;
54 package body Sem_Disp is
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Add_Dispatching_Operation
61 (Tagged_Type : Entity_Id;
63 -- Add New_Op in the list of primitive operations of Tagged_Type
65 function Check_Controlling_Type
67 Subp : Entity_Id) return Entity_Id;
68 -- T is the tagged type of a formal parameter or the result of Subp.
69 -- If the subprogram has a controlling parameter or result that matches
70 -- the type, then returns the tagged type of that parameter or result
71 -- (returning the designated tagged type in the case of an access
72 -- parameter); otherwise returns empty.
74 -------------------------------
75 -- Add_Dispatching_Operation --
76 -------------------------------
78 procedure Add_Dispatching_Operation
79 (Tagged_Type : Entity_Id;
82 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
85 -- The dispatching operation may already be on the list, if it the
86 -- wrapper for an inherited function of a null extension (see exp_ch3
87 -- for the construction of function wrappers). The list of primitive
88 -- operations must not contain duplicates.
90 Append_Unique_Elmt (New_Op, List);
91 end Add_Dispatching_Operation;
93 -------------------------------
94 -- Check_Controlling_Formals --
95 -------------------------------
97 procedure Check_Controlling_Formals
102 Ctrl_Type : Entity_Id;
105 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 the controlling type is concurrent and declared within a
113 -- generic or inside an instance, use its corresponding record
116 if Is_Concurrent_Type (Ctrl_Type)
117 and then Present (Corresponding_Record_Type (Ctrl_Type))
119 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
122 if Ctrl_Type = Typ then
123 Set_Is_Controlling_Formal (Formal);
125 -- Ada 2005 (AI-231): Anonymous access types used in
126 -- controlling parameters exclude null because it is necessary
127 -- to read the tag to dispatch, and null has no tag.
129 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
130 Set_Can_Never_Be_Null (Etype (Formal));
131 Set_Is_Known_Non_Null (Etype (Formal));
134 -- Check that the parameter's nominal subtype statically
135 -- matches the first subtype.
137 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
138 if not Subtypes_Statically_Match
139 (Typ, Designated_Type (Etype (Formal)))
142 ("parameter subtype does not match controlling type",
146 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
148 ("parameter subtype does not match controlling type",
152 if Present (Default_Value (Formal)) then
154 -- In Ada 2005, access parameters can have defaults
156 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
157 and then Ada_Version < Ada_05
160 ("default not allowed for controlling access parameter",
161 Default_Value (Formal));
163 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
165 ("default expression must be a tag indeterminate" &
166 " function call", Default_Value (Formal));
170 elsif Comes_From_Source (Subp) then
172 ("operation can be dispatching in only one type", Subp);
176 Next_Formal (Formal);
179 if Present (Etype (Subp)) then
180 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
182 if Present (Ctrl_Type) then
183 if Ctrl_Type = Typ then
184 Set_Has_Controlling_Result (Subp);
186 -- Check that result subtype statically matches first subtype
187 -- (Ada 2005) : Subp may have a controlling access result.
189 if Subtypes_Statically_Match (Typ, Etype (Subp))
190 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
192 Subtypes_Statically_Match
193 (Typ, Designated_Type (Etype (Subp))))
199 ("result subtype does not match controlling type", Subp);
202 elsif Comes_From_Source (Subp) then
204 ("operation can be dispatching in only one type", Subp);
208 end Check_Controlling_Formals;
210 ----------------------------
211 -- Check_Controlling_Type --
212 ----------------------------
214 function Check_Controlling_Type
216 Subp : Entity_Id) return Entity_Id
218 Tagged_Type : Entity_Id := Empty;
221 if Is_Tagged_Type (T) then
222 if Is_First_Subtype (T) then
225 Tagged_Type := Base_Type (T);
228 elsif Ekind (T) = E_Anonymous_Access_Type
229 and then Is_Tagged_Type (Designated_Type (T))
231 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
232 if Is_First_Subtype (Designated_Type (T)) then
233 Tagged_Type := Designated_Type (T);
235 Tagged_Type := Base_Type (Designated_Type (T));
238 -- Ada 2005 : an incomplete type can be tagged. An operation with
239 -- an access parameter of the type is dispatching.
241 elsif Scope (Designated_Type (T)) = Current_Scope then
242 Tagged_Type := Designated_Type (T);
244 -- Ada 2005 (AI-50217)
246 elsif From_With_Type (Designated_Type (T))
247 and then Present (Non_Limited_View (Designated_Type (T)))
249 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
250 Tagged_Type := Non_Limited_View (Designated_Type (T));
252 Tagged_Type := Base_Type (Non_Limited_View
253 (Designated_Type (T)));
259 or else Is_Class_Wide_Type (Tagged_Type)
263 -- The dispatching type and the primitive operation must be defined
264 -- in the same scope, except in the case of internal operations and
265 -- formal abstract subprograms.
267 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
268 and then (not Is_Generic_Type (Tagged_Type)
269 or else not Comes_From_Source (Subp)))
271 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
273 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
275 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
277 Is_Abstract_Subprogram (Subp))
284 end Check_Controlling_Type;
286 ----------------------------
287 -- Check_Dispatching_Call --
288 ----------------------------
290 procedure Check_Dispatching_Call (N : Node_Id) is
291 Loc : constant Source_Ptr := Sloc (N);
294 Control : Node_Id := Empty;
296 Subp_Entity : Entity_Id;
297 Indeterm_Ancestor_Call : Boolean := False;
298 Indeterm_Ctrl_Type : Entity_Id;
300 Static_Tag : Node_Id := Empty;
301 -- If a controlling formal has a statically tagged actual, the tag of
302 -- this actual is to be used for any tag-indeterminate actual
304 procedure Check_Dispatching_Context;
305 -- If the call is tag-indeterminate and the entity being called is
306 -- abstract, verify that the context is a call that will eventually
307 -- provide a tag for dispatching, or has provided one already.
309 -------------------------------
310 -- Check_Dispatching_Context --
311 -------------------------------
313 procedure Check_Dispatching_Context is
314 Subp : constant Entity_Id := Entity (Name (N));
318 if Is_Abstract_Subprogram (Subp)
319 and then No (Controlling_Argument (N))
321 if Present (Alias (Subp))
322 and then not Is_Abstract_Subprogram (Alias (Subp))
323 and then No (DTC_Entity (Subp))
325 -- Private overriding of inherited abstract operation,
328 Set_Entity (Name (N), Alias (Subp));
334 while Present (Par) loop
336 if (Nkind (Par) = N_Function_Call or else
337 Nkind (Par) = N_Procedure_Call_Statement or else
338 Nkind (Par) = N_Assignment_Statement or else
339 Nkind (Par) = N_Op_Eq or else
340 Nkind (Par) = N_Op_Ne)
341 and then Is_Tagged_Type (Etype (Subp))
345 elsif Nkind (Par) = N_Qualified_Expression
346 or else Nkind (Par) = N_Unchecked_Type_Conversion
351 if Ekind (Subp) = E_Function then
353 ("call to abstract function must be dispatching", N);
355 -- This error can occur for a procedure in the case of a
356 -- call to an abstract formal procedure with a statically
361 ("call to abstract procedure must be dispatching",
370 end Check_Dispatching_Context;
372 -- Start of processing for Check_Dispatching_Call
375 -- Find a controlling argument, if any
377 if Present (Parameter_Associations (N)) then
378 Actual := First_Actual (N);
380 Subp_Entity := Entity (Name (N));
381 Formal := First_Formal (Subp_Entity);
383 while Present (Actual) loop
384 Control := Find_Controlling_Arg (Actual);
385 exit when Present (Control);
387 -- Check for the case where the actual is a tag-indeterminate call
388 -- whose result type is different than the tagged type associated
389 -- with the containing call, but is an ancestor of the type.
391 if Is_Controlling_Formal (Formal)
392 and then Is_Tag_Indeterminate (Actual)
393 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
394 and then Is_Ancestor (Etype (Actual), Etype (Formal))
396 Indeterm_Ancestor_Call := True;
397 Indeterm_Ctrl_Type := Etype (Formal);
399 -- If the formal is controlling but the actual is not, the type
400 -- of the actual is statically known, and may be used as the
401 -- controlling tag for some other-indeterminate actual.
403 elsif Is_Controlling_Formal (Formal)
404 and then Is_Entity_Name (Actual)
405 and then Is_Tagged_Type (Etype (Actual))
407 Static_Tag := Actual;
410 Next_Actual (Actual);
411 Next_Formal (Formal);
414 -- If the call doesn't have a controlling actual but does have
415 -- an indeterminate actual that requires dispatching treatment,
416 -- then an object is needed that will serve as the controlling
417 -- argument for a dispatching call on the indeterminate actual.
418 -- This can only occur in the unusual situation of a default
419 -- actual given by a tag-indeterminate call and where the type
420 -- of the call is an ancestor of the type associated with a
421 -- containing call to an inherited operation (see AI-239).
422 -- Rather than create an object of the tagged type, which would
423 -- be problematic for various reasons (default initialization,
424 -- discriminants), the tag of the containing call's associated
425 -- tagged type is directly used to control the dispatching.
428 and then Indeterm_Ancestor_Call
429 and then No (Static_Tag)
432 Make_Attribute_Reference (Loc,
433 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
434 Attribute_Name => Name_Tag);
439 if Present (Control) then
441 -- Verify that no controlling arguments are statically tagged
444 Write_Str ("Found Dispatching call");
449 Actual := First_Actual (N);
451 while Present (Actual) loop
452 if Actual /= Control then
454 if not Is_Controlling_Actual (Actual) then
455 null; -- Can be anything
457 elsif Is_Dynamically_Tagged (Actual) then
458 null; -- Valid parameter
460 elsif Is_Tag_Indeterminate (Actual) then
462 -- The tag is inherited from the enclosing call (the
463 -- node we are currently analyzing). Explicitly expand
464 -- the actual, since the previous call to Expand
465 -- (from Resolve_Call) had no way of knowing about
466 -- the required dispatching.
468 Propagate_Tag (Control, Actual);
472 ("controlling argument is not dynamically tagged",
478 Next_Actual (Actual);
481 -- Mark call as a dispatching call
483 Set_Controlling_Argument (N, Control);
484 Check_Restriction (No_Dispatching_Calls, N);
486 -- If there is a statically tagged actual and a tag-indeterminate
487 -- call to a function of the ancestor (such as that provided by a
488 -- default), then treat this as a dispatching call and propagate
489 -- the tag to the tag-indeterminate call(s).
491 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
493 Make_Attribute_Reference (Loc,
495 New_Occurrence_Of (Etype (Static_Tag), Loc),
496 Attribute_Name => Name_Tag);
500 Actual := First_Actual (N);
501 Formal := First_Formal (Subp_Entity);
502 while Present (Actual) loop
503 if Is_Tag_Indeterminate (Actual)
504 and then Is_Controlling_Formal (Formal)
506 Propagate_Tag (Control, Actual);
509 Next_Actual (Actual);
510 Next_Formal (Formal);
513 Check_Dispatching_Context;
516 -- The call is not dispatching, so check that there aren't any
517 -- tag-indeterminate abstract calls left.
519 Actual := First_Actual (N);
520 while Present (Actual) loop
521 if Is_Tag_Indeterminate (Actual) then
523 -- Function call case
525 if Nkind (Original_Node (Actual)) = N_Function_Call then
526 Func := Entity (Name (Original_Node (Actual)));
528 -- If the actual is an attribute then it can't be abstract
529 -- (the only current case of a tag-indeterminate attribute
530 -- is the stream Input attribute).
533 Nkind (Original_Node (Actual)) = N_Attribute_Reference
537 -- Only other possibility is a qualified expression whose
538 -- constituent expression is itself a call.
544 (Expression (Original_Node (Actual)))));
547 if Present (Func) and then Is_Abstract_Subprogram (Func) then
549 "call to abstract function must be dispatching", N);
553 Next_Actual (Actual);
556 Check_Dispatching_Context;
560 -- If dispatching on result, the enclosing call, if any, will
561 -- determine the controlling argument. Otherwise this is the
562 -- primitive operation of the root type.
564 Check_Dispatching_Context;
566 end Check_Dispatching_Call;
568 ---------------------------------
569 -- Check_Dispatching_Operation --
570 ---------------------------------
572 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
573 Tagged_Type : Entity_Id;
574 Has_Dispatching_Parent : Boolean := False;
575 Body_Is_Last_Primitive : Boolean := False;
577 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
578 -- Check whether T is derived from a visibly controlled type.
579 -- This is true if the root type is declared in Ada.Finalization.
580 -- If T is derived instead from a private type whose full view
581 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
582 -- does not override the inherited one.
584 ---------------------------
585 -- Is_Visibly_Controlled --
586 ---------------------------
588 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
589 Root : constant Entity_Id := Root_Type (T);
591 return Chars (Scope (Root)) = Name_Finalization
592 and then Chars (Scope (Scope (Root))) = Name_Ada
593 and then Scope (Scope (Scope (Root))) = Standard_Standard;
594 end Is_Visibly_Controlled;
596 -- Start of processing for Check_Dispatching_Operation
599 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
603 Set_Is_Dispatching_Operation (Subp, False);
604 Tagged_Type := Find_Dispatching_Type (Subp);
608 if Ada_Version = Ada_05
609 and then Present (Tagged_Type)
610 and then Is_Concurrent_Type (Tagged_Type)
612 -- Protect the frontend against previously detected errors
614 if No (Corresponding_Record_Type (Tagged_Type)) then
618 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
621 -- If Subp is derived from a dispatching operation then it should
622 -- always be treated as dispatching. In this case various checks
623 -- below will be bypassed. Makes sure that late declarations for
624 -- inherited private subprograms are treated as dispatching, even
625 -- if the associated tagged type is already frozen.
627 Has_Dispatching_Parent :=
628 Present (Alias (Subp))
629 and then Is_Dispatching_Operation (Alias (Subp));
631 if No (Tagged_Type) then
633 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
634 -- with an abstract interface type unless the interface acts as a
635 -- parent type in a derivation. If the interface type is a formal
636 -- type then the operation is not primitive and therefore legal.
643 E := First_Entity (Subp);
644 while Present (E) loop
646 -- For an access parameter, check designated type.
648 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
649 Typ := Designated_Type (Etype (E));
654 if Comes_From_Source (Subp)
655 and then Is_Interface (Typ)
656 and then not Is_Class_Wide_Type (Typ)
657 and then not Is_Derived_Type (Typ)
658 and then not Is_Generic_Type (Typ)
659 and then not In_Instance
661 Error_Msg_N ("?declaration of& is too late!", Subp);
663 ("\spec should appear immediately after declaration of &!",
671 -- In case of functions check also the result type
673 if Ekind (Subp) = E_Function then
674 if Is_Access_Type (Etype (Subp)) then
675 Typ := Designated_Type (Etype (Subp));
680 if not Is_Class_Wide_Type (Typ)
681 and then Is_Interface (Typ)
682 and then not Is_Derived_Type (Typ)
684 Error_Msg_N ("?declaration of& is too late!", Subp);
686 ("\spec should appear immediately after declaration of &!",
694 -- The subprograms build internally after the freezing point (such as
695 -- the Init procedure) are not primitives
697 elsif Is_Frozen (Tagged_Type)
698 and then not Comes_From_Source (Subp)
699 and then not Has_Dispatching_Parent
703 -- The operation may be a child unit, whose scope is the defining
704 -- package, but which is not a primitive operation of the type.
706 elsif Is_Child_Unit (Subp) then
709 -- If the subprogram is not defined in a package spec, the only case
710 -- where it can be a dispatching op is when it overrides an operation
711 -- before the freezing point of the type.
713 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
714 or else In_Package_Body (Scope (Subp)))
715 and then not Has_Dispatching_Parent
717 if not Comes_From_Source (Subp)
718 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
722 -- If the type is already frozen, the overriding is not allowed
723 -- except when Old_Subp is not a dispatching operation (which
724 -- can occur when Old_Subp was inherited by an untagged type).
725 -- However, a body with no previous spec freezes the type "after"
726 -- its declaration, and therefore is a legal overriding (unless
727 -- the type has already been frozen). Only the first such body
730 elsif Present (Old_Subp)
731 and then Is_Dispatching_Operation (Old_Subp)
733 if Comes_From_Source (Subp)
735 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
736 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
739 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
740 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
743 -- ??? The checks here for whether the type has been
744 -- frozen prior to the new body are not complete. It's
745 -- not simple to check frozenness at this point since
746 -- the body has already caused the type to be prematurely
747 -- frozen in Analyze_Declarations, but we're forced to
748 -- recheck this here because of the odd rule interpretation
749 -- that allows the overriding if the type wasn't frozen
750 -- prior to the body. The freezing action should probably
751 -- be delayed until after the spec is seen, but that's
752 -- a tricky change to the delicate freezing code.
754 -- Look at each declaration following the type up
755 -- until the new subprogram body. If any of the
756 -- declarations is a body then the type has been
757 -- frozen already so the overriding primitive is
760 while Present (Decl_Item)
761 and then (Decl_Item /= Subp_Body)
763 if Comes_From_Source (Decl_Item)
764 and then (Nkind (Decl_Item) in N_Proper_Body
765 or else Nkind (Decl_Item) in N_Body_Stub)
767 Error_Msg_N ("overriding of& is too late!", Subp);
769 ("\spec should appear immediately after the type!",
777 -- If the subprogram doesn't follow in the list of
778 -- declarations including the type then the type
779 -- has definitely been frozen already and the body
782 if No (Decl_Item) then
783 Error_Msg_N ("overriding of& is too late!", Subp);
785 ("\spec should appear immediately after the type!",
788 elsif Is_Frozen (Subp) then
790 -- The subprogram body declares a primitive operation.
791 -- if the subprogram is already frozen, we must update
792 -- its dispatching information explicitly here. The
793 -- information is taken from the overridden subprogram.
794 -- We must also generate a cross-reference entry because
795 -- references to other primitives were already created
796 -- when type was frozen.
798 Body_Is_Last_Primitive := True;
800 if Present (DTC_Entity (Old_Subp)) then
801 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
802 Set_DT_Position (Subp, DT_Position (Old_Subp));
804 if not Restriction_Active (No_Dispatching_Calls) then
805 if Building_Static_DT (Tagged_Type) then
807 -- If the static dispatch table has not been
808 -- built then there is nothing else to do now;
809 -- otherwise we notify that we cannot build the
810 -- static dispatch table.
812 if Has_Dispatch_Table (Tagged_Type) then
814 ("overriding of& is too late for building" &
815 " static dispatch tables!", Subp);
817 ("\spec should appear immediately after" &
822 Register_Primitive (Sloc (Subp_Body),
824 Ins_Nod => Subp_Body);
827 Generate_Reference (Tagged_Type, Subp, 'p', False);
834 Error_Msg_N ("overriding of& is too late!", Subp);
836 ("\subprogram spec should appear immediately after the type!",
840 -- If the type is not frozen yet and we are not in the overriding
841 -- case it looks suspiciously like an attempt to define a primitive
844 elsif not Is_Frozen (Tagged_Type) then
846 ("?not dispatching (must be defined in a package spec)", Subp);
849 -- When the type is frozen, it is legitimate to define a new
850 -- non-primitive operation.
856 -- Now, we are sure that the scope is a package spec. If the subprogram
857 -- is declared after the freezing point of the type that's an error
859 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
860 Error_Msg_N ("this primitive operation is declared too late", Subp);
862 ("?no primitive operations for& after this line",
863 Freeze_Node (Tagged_Type),
868 Check_Controlling_Formals (Tagged_Type, Subp);
870 -- Now it should be a correct primitive operation, put it in the list
872 if Present (Old_Subp) then
873 Check_Subtype_Conformant (Subp, Old_Subp);
875 if (Chars (Subp) = Name_Initialize
876 or else Chars (Subp) = Name_Adjust
877 or else Chars (Subp) = Name_Finalize)
878 and then Is_Controlled (Tagged_Type)
879 and then not Is_Visibly_Controlled (Tagged_Type)
881 Set_Is_Overriding_Operation (Subp, False);
883 ("operation does not override inherited&?", Subp, Subp);
885 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
886 Set_Is_Overriding_Operation (Subp);
888 -- Ada 2005 (AI-251): In case of late overriding of a primitive
889 -- that covers abstract interface subprograms we must register it
890 -- in all the secondary dispatch tables associated with abstract
893 if Body_Is_Last_Primitive then
895 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
900 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
901 while Present (Elmt) loop
904 if Present (Alias (Prim))
905 and then Present (Abstract_Interface_Alias (Prim))
906 and then Alias (Prim) = Subp
908 Register_Primitive (Sloc (Prim),
910 Ins_Nod => Subp_Body);
916 -- Redisplay the contents of the updated dispatch table
918 if Debug_Flag_ZZ then
919 Write_Str ("Late overriding: ");
920 Write_DT (Tagged_Type);
926 -- If no old subprogram, then we add this as a dispatching operation,
927 -- but we avoid doing this if an error was posted, to prevent annoying
930 elsif not Error_Posted (Subp) then
931 Add_Dispatching_Operation (Tagged_Type, Subp);
934 Set_Is_Dispatching_Operation (Subp, True);
936 if not Body_Is_Last_Primitive then
937 Set_DT_Position (Subp, No_Uint);
939 elsif Has_Controlled_Component (Tagged_Type)
941 (Chars (Subp) = Name_Initialize
942 or else Chars (Subp) = Name_Adjust
943 or else Chars (Subp) = Name_Finalize)
946 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
950 Old_Spec : Entity_Id;
952 C_Names : constant array (1 .. 3) of Name_Id :=
957 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
958 (TSS_Deep_Initialize,
963 -- Remove previous controlled function, which was constructed
964 -- and analyzed when the type was frozen. This requires
965 -- removing the body of the redefined primitive, as well as
966 -- its specification if needed (there is no spec created for
967 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
968 -- the exception information that may have been generated for
969 -- it when front end zero-cost tables are enabled.
971 for J in D_Names'Range loop
972 Old_P := TSS (Tagged_Type, D_Names (J));
975 and then Chars (Subp) = C_Names (J)
977 Old_Bod := Unit_Declaration_Node (Old_P);
979 Set_Is_Eliminated (Old_P);
980 Set_Scope (Old_P, Scope (Current_Scope));
982 if Nkind (Old_Bod) = N_Subprogram_Body
983 and then Present (Corresponding_Spec (Old_Bod))
985 Old_Spec := Corresponding_Spec (Old_Bod);
986 Set_Has_Completion (Old_Spec, False);
991 Build_Late_Proc (Tagged_Type, Chars (Subp));
993 -- The new operation is added to the actions of the freeze
994 -- node for the type, but this node has already been analyzed,
995 -- so we must retrieve and analyze explicitly the new body.
998 and then Present (Actions (F_Node))
1000 Decl := Last (Actions (F_Node));
1005 end Check_Dispatching_Operation;
1007 ------------------------------------------
1008 -- Check_Operation_From_Incomplete_Type --
1009 ------------------------------------------
1011 procedure Check_Operation_From_Incomplete_Type
1015 Full : constant Entity_Id := Full_View (Typ);
1016 Parent_Typ : constant Entity_Id := Etype (Full);
1017 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1018 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1020 Prev : Elmt_Id := No_Elmt;
1022 function Derives_From (Proc : Entity_Id) return Boolean;
1023 -- Check that Subp has the signature of an operation derived from Proc.
1024 -- Subp has an access parameter that designates Typ.
1030 function Derives_From (Proc : Entity_Id) return Boolean is
1034 if Chars (Proc) /= Chars (Subp) then
1038 F1 := First_Formal (Proc);
1039 F2 := First_Formal (Subp);
1041 while Present (F1) and then Present (F2) loop
1043 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1045 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1048 elsif Designated_Type (Etype (F1)) = Parent_Typ
1049 and then Designated_Type (Etype (F2)) /= Full
1054 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1057 elsif Etype (F1) /= Etype (F2) then
1065 return No (F1) and then No (F2);
1068 -- Start of processing for Check_Operation_From_Incomplete_Type
1071 -- The operation may override an inherited one, or may be a new one
1072 -- altogether. The inherited operation will have been hidden by the
1073 -- current one at the point of the type derivation, so it does not
1074 -- appear in the list of primitive operations of the type. We have to
1075 -- find the proper place of insertion in the list of primitive opera-
1076 -- tions by iterating over the list for the parent type.
1078 Op1 := First_Elmt (Old_Prim);
1079 Op2 := First_Elmt (New_Prim);
1081 while Present (Op1) and then Present (Op2) loop
1083 if Derives_From (Node (Op1)) then
1086 Prepend_Elmt (Subp, New_Prim);
1088 Insert_Elmt_After (Subp, Prev);
1099 -- Operation is a new primitive
1101 Append_Elmt (Subp, New_Prim);
1102 end Check_Operation_From_Incomplete_Type;
1104 ---------------------------------------
1105 -- Check_Operation_From_Private_View --
1106 ---------------------------------------
1108 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1109 Tagged_Type : Entity_Id;
1112 if Is_Dispatching_Operation (Alias (Subp)) then
1113 Set_Scope (Subp, Current_Scope);
1114 Tagged_Type := Find_Dispatching_Type (Subp);
1116 -- Add Old_Subp to primitive operations if not already present.
1118 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1119 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1121 -- If Old_Subp isn't already marked as dispatching then
1122 -- this is the case of an operation of an untagged private
1123 -- type fulfilled by a tagged type that overrides an
1124 -- inherited dispatching operation, so we set the necessary
1125 -- dispatching attributes here.
1127 if not Is_Dispatching_Operation (Old_Subp) then
1129 -- If the untagged type has no discriminants, and the full
1130 -- view is constrained, there will be a spurious mismatch
1131 -- of subtypes on the controlling arguments, because the tagged
1132 -- type is the internal base type introduced in the derivation.
1133 -- Use the original type to verify conformance, rather than the
1136 if not Comes_From_Source (Tagged_Type)
1137 and then Has_Discriminants (Tagged_Type)
1142 Formal := First_Formal (Old_Subp);
1143 while Present (Formal) loop
1144 if Tagged_Type = Base_Type (Etype (Formal)) then
1145 Tagged_Type := Etype (Formal);
1148 Next_Formal (Formal);
1152 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1153 Tagged_Type := Etype (Old_Subp);
1157 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1158 Set_Is_Dispatching_Operation (Old_Subp, True);
1159 Set_DT_Position (Old_Subp, No_Uint);
1162 -- If the old subprogram is an explicit renaming of some other
1163 -- entity, it is not overridden by the inherited subprogram.
1164 -- Otherwise, update its alias and other attributes.
1166 if Present (Alias (Old_Subp))
1167 and then Nkind (Unit_Declaration_Node (Old_Subp))
1168 /= N_Subprogram_Renaming_Declaration
1170 Set_Alias (Old_Subp, Alias (Subp));
1172 -- The derived subprogram should inherit the abstractness
1173 -- of the parent subprogram (except in the case of a function
1174 -- returning the type). This sets the abstractness properly
1175 -- for cases where a private extension may have inherited
1176 -- an abstract operation, but the full type is derived from
1177 -- a descendant type and inherits a nonabstract version.
1179 if Etype (Subp) /= Tagged_Type then
1180 Set_Is_Abstract_Subprogram
1181 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1186 end Check_Operation_From_Private_View;
1188 --------------------------
1189 -- Find_Controlling_Arg --
1190 --------------------------
1192 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1193 Orig_Node : constant Node_Id := Original_Node (N);
1197 if Nkind (Orig_Node) = N_Qualified_Expression then
1198 return Find_Controlling_Arg (Expression (Orig_Node));
1201 -- Dispatching on result case. If expansion is disabled, the node still
1202 -- has the structure of a function call. However, if the function name
1203 -- is an operator and the call was given in infix form, the original
1204 -- node has no controlling result and we must examine the current node.
1206 if Nkind (N) = N_Function_Call
1207 and then Present (Controlling_Argument (N))
1208 and then Has_Controlling_Result (Entity (Name (N)))
1210 return Controlling_Argument (N);
1212 -- If expansion is enabled, the call may have been transformed into
1213 -- an indirect call, and we need to recover the original node.
1215 elsif Nkind (Orig_Node) = N_Function_Call
1216 and then Present (Controlling_Argument (Orig_Node))
1217 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1219 return Controlling_Argument (Orig_Node);
1223 elsif Is_Controlling_Actual (N)
1225 (Nkind (Parent (N)) = N_Qualified_Expression
1226 and then Is_Controlling_Actual (Parent (N)))
1230 if Is_Access_Type (Typ) then
1231 -- In the case of an Access attribute, use the type of
1232 -- the prefix, since in the case of an actual for an
1233 -- access parameter, the attribute's type may be of a
1234 -- specific designated type, even though the prefix
1235 -- type is class-wide.
1237 if Nkind (N) = N_Attribute_Reference then
1238 Typ := Etype (Prefix (N));
1240 -- An allocator is dispatching if the type of qualified
1241 -- expression is class_wide, in which case this is the
1242 -- controlling type.
1244 elsif Nkind (Orig_Node) = N_Allocator
1245 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1247 Typ := Etype (Expression (Orig_Node));
1250 Typ := Designated_Type (Typ);
1254 if Is_Class_Wide_Type (Typ)
1256 (Nkind (Parent (N)) = N_Qualified_Expression
1257 and then Is_Access_Type (Etype (N))
1258 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1265 end Find_Controlling_Arg;
1267 ---------------------------
1268 -- Find_Dispatching_Type --
1269 ---------------------------
1271 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1273 Ctrl_Type : Entity_Id;
1276 if Present (DTC_Entity (Subp)) then
1277 return Scope (DTC_Entity (Subp));
1280 Formal := First_Formal (Subp);
1281 while Present (Formal) loop
1282 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1284 if Present (Ctrl_Type) then
1288 Next_Formal (Formal);
1291 -- The subprogram may also be dispatching on result
1293 if Present (Etype (Subp)) then
1294 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1296 if Present (Ctrl_Type) then
1303 end Find_Dispatching_Type;
1305 ---------------------------
1306 -- Is_Dynamically_Tagged --
1307 ---------------------------
1309 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1311 if Nkind (N) = N_Error then
1314 return Find_Controlling_Arg (N) /= Empty;
1316 end Is_Dynamically_Tagged;
1318 --------------------------
1319 -- Is_Tag_Indeterminate --
1320 --------------------------
1322 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1325 Orig_Node : constant Node_Id := Original_Node (N);
1328 if Nkind (Orig_Node) = N_Function_Call
1329 and then Is_Entity_Name (Name (Orig_Node))
1331 Nam := Entity (Name (Orig_Node));
1333 if not Has_Controlling_Result (Nam) then
1336 -- An explicit dereference means that the call has already been
1337 -- expanded and there is no tag to propagate.
1339 elsif Nkind (N) = N_Explicit_Dereference then
1342 -- If there are no actuals, the call is tag-indeterminate
1344 elsif No (Parameter_Associations (Orig_Node)) then
1348 Actual := First_Actual (Orig_Node);
1349 while Present (Actual) loop
1350 if Is_Controlling_Actual (Actual)
1351 and then not Is_Tag_Indeterminate (Actual)
1353 return False; -- one operand is dispatching
1356 Next_Actual (Actual);
1362 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1363 return Is_Tag_Indeterminate (Expression (Orig_Node));
1365 -- Case of a call to the Input attribute (possibly rewritten), which is
1366 -- always tag-indeterminate except when its prefix is a Class attribute.
1368 elsif Nkind (Orig_Node) = N_Attribute_Reference
1370 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1372 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1376 -- In Ada 2005 a function that returns an anonymous access type can
1377 -- dispatching, and the dereference of a call to such a function
1378 -- is also tag-indeterminate.
1380 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1381 and then Ada_Version >= Ada_05
1383 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1388 end Is_Tag_Indeterminate;
1390 ------------------------------------
1391 -- Override_Dispatching_Operation --
1392 ------------------------------------
1394 procedure Override_Dispatching_Operation
1395 (Tagged_Type : Entity_Id;
1396 Prev_Op : Entity_Id;
1403 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1404 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1406 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1407 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1408 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1411 -- If there is no previous operation to override, the type declaration
1412 -- was malformed, and an error must have been emitted already.
1414 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1415 while Present (Elmt)
1416 and then Node (Elmt) /= Prev_Op
1425 Replace_Elmt (Elmt, New_Op);
1427 if Ada_Version >= Ada_05
1428 and then Has_Abstract_Interfaces (Tagged_Type)
1430 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1431 -- entities of the overridden primitive to reference New_Op, and also
1432 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1433 -- that the new operation is subtype conformant with the interface
1434 -- operations that it implements (for operations inherited from the
1435 -- parent itself, this check is made when building the derived type).
1437 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1438 while Present (Elmt) loop
1439 Prim := Node (Elmt);
1441 if Prim = New_Op then
1444 -- Note: The check on Is_Subprogram protects the frontend against
1445 -- reading attributes in entities that are not yet fully decorated
1447 elsif Is_Subprogram (Prim)
1448 and then Present (Abstract_Interface_Alias (Prim))
1449 and then Alias (Prim) = Prev_Op
1450 and then Present (Etype (New_Op))
1452 Set_Alias (Prim, New_Op);
1453 Check_Subtype_Conformant (New_Op, Prim);
1454 Set_Is_Abstract_Subprogram
1455 (Prim, Is_Abstract_Subprogram (New_Op));
1457 -- Ensure that this entity will be expanded to fill the
1458 -- corresponding entry in its dispatch table.
1460 if not Is_Abstract_Subprogram (Prim) then
1461 Set_Has_Delayed_Freeze (Prim);
1469 if (not Is_Package_Or_Generic_Package (Current_Scope))
1470 or else not In_Private_Part (Current_Scope)
1472 -- Not a private primitive
1476 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1478 -- Make the overriding operation into an alias of the implicit one.
1479 -- In this fashion a call from outside ends up calling the new body
1480 -- even if non-dispatching, and a call from inside calls the
1481 -- overriding operation because it hides the implicit one. To
1482 -- indicate that the body of Prev_Op is never called, set its
1483 -- dispatch table entity to Empty.
1485 Set_Alias (Prev_Op, New_Op);
1486 Set_DTC_Entity (Prev_Op, Empty);
1489 end Override_Dispatching_Operation;
1495 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1496 Call_Node : Node_Id;
1500 if Nkind (Actual) = N_Function_Call then
1501 Call_Node := Actual;
1503 elsif Nkind (Actual) = N_Identifier
1504 and then Nkind (Original_Node (Actual)) = N_Function_Call
1506 -- Call rewritten as object declaration when stack-checking
1507 -- is enabled. Propagate tag to expression in declaration, which
1508 -- is original call.
1510 Call_Node := Expression (Parent (Entity (Actual)));
1512 -- Ada 2005: If this is a dereference of a call to a function with a
1513 -- dispatching access-result, the tag is propagated when the dereference
1514 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1516 elsif Nkind (Actual) = N_Explicit_Dereference
1517 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1521 -- Only other possibilities are parenthesized or qualified expression,
1522 -- or an expander-generated unchecked conversion of a function call to
1523 -- a stream Input attribute.
1526 Call_Node := Expression (Actual);
1529 -- Do not set the Controlling_Argument if already set. This happens
1530 -- in the special case of _Input (see Exp_Attr, case Input).
1532 if No (Controlling_Argument (Call_Node)) then
1533 Set_Controlling_Argument (Call_Node, Control);
1536 Arg := First_Actual (Call_Node);
1538 while Present (Arg) loop
1539 if Is_Tag_Indeterminate (Arg) then
1540 Propagate_Tag (Control, Arg);
1546 -- Expansion of dispatching calls is suppressed when VM_Target, because
1547 -- the VM back-ends directly handle the generation of dispatching
1548 -- calls and would have to undo any expansion to an indirect call.
1550 if VM_Target = No_VM then
1551 Expand_Dispatching_Call (Call_Node);
1553 -- Expansion of a dispatching call results in an indirect call, which in
1554 -- turn causes current values to be killed (see Resolve_Call), so on VM
1555 -- targets we do the call here to ensure consistent warnings between VM
1556 -- and non-VM targets.
1559 Kill_Current_Values;