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 -- (AI-345): The task body procedure is not a primitive of the tagged
624 if Present (Tagged_Type)
625 and then Is_Concurrent_Record_Type (Tagged_Type)
626 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
627 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
628 and then Subp = Get_Task_Body_Procedure
629 (Corresponding_Concurrent_Type (Tagged_Type))
634 -- If Subp is derived from a dispatching operation then it should
635 -- always be treated as dispatching. In this case various checks
636 -- below will be bypassed. Makes sure that late declarations for
637 -- inherited private subprograms are treated as dispatching, even
638 -- if the associated tagged type is already frozen.
640 Has_Dispatching_Parent :=
641 Present (Alias (Subp))
642 and then Is_Dispatching_Operation (Alias (Subp));
644 if No (Tagged_Type) then
646 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
647 -- with an abstract interface type unless the interface acts as a
648 -- parent type in a derivation. If the interface type is a formal
649 -- type then the operation is not primitive and therefore legal.
656 E := First_Entity (Subp);
657 while Present (E) loop
659 -- For an access parameter, check designated type.
661 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
662 Typ := Designated_Type (Etype (E));
667 if Comes_From_Source (Subp)
668 and then Is_Interface (Typ)
669 and then not Is_Class_Wide_Type (Typ)
670 and then not Is_Derived_Type (Typ)
671 and then not Is_Generic_Type (Typ)
672 and then not In_Instance
674 Error_Msg_N ("?declaration of& is too late!", Subp);
676 ("\spec should appear immediately after declaration of &!",
684 -- In case of functions check also the result type
686 if Ekind (Subp) = E_Function then
687 if Is_Access_Type (Etype (Subp)) then
688 Typ := Designated_Type (Etype (Subp));
693 if not Is_Class_Wide_Type (Typ)
694 and then Is_Interface (Typ)
695 and then not Is_Derived_Type (Typ)
697 Error_Msg_N ("?declaration of& is too late!", Subp);
699 ("\spec should appear immediately after declaration of &!",
707 -- The subprograms build internally after the freezing point (such as
708 -- the Init procedure) are not primitives
710 elsif Is_Frozen (Tagged_Type)
711 and then not Comes_From_Source (Subp)
712 and then not Has_Dispatching_Parent
716 -- The operation may be a child unit, whose scope is the defining
717 -- package, but which is not a primitive operation of the type.
719 elsif Is_Child_Unit (Subp) then
722 -- If the subprogram is not defined in a package spec, the only case
723 -- where it can be a dispatching op is when it overrides an operation
724 -- before the freezing point of the type.
726 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
727 or else In_Package_Body (Scope (Subp)))
728 and then not Has_Dispatching_Parent
730 if not Comes_From_Source (Subp)
731 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
735 -- If the type is already frozen, the overriding is not allowed
736 -- except when Old_Subp is not a dispatching operation (which
737 -- can occur when Old_Subp was inherited by an untagged type).
738 -- However, a body with no previous spec freezes the type "after"
739 -- its declaration, and therefore is a legal overriding (unless
740 -- the type has already been frozen). Only the first such body
743 elsif Present (Old_Subp)
744 and then Is_Dispatching_Operation (Old_Subp)
746 if Comes_From_Source (Subp)
748 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
749 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
752 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
753 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
756 -- ??? The checks here for whether the type has been
757 -- frozen prior to the new body are not complete. It's
758 -- not simple to check frozenness at this point since
759 -- the body has already caused the type to be prematurely
760 -- frozen in Analyze_Declarations, but we're forced to
761 -- recheck this here because of the odd rule interpretation
762 -- that allows the overriding if the type wasn't frozen
763 -- prior to the body. The freezing action should probably
764 -- be delayed until after the spec is seen, but that's
765 -- a tricky change to the delicate freezing code.
767 -- Look at each declaration following the type up
768 -- until the new subprogram body. If any of the
769 -- declarations is a body then the type has been
770 -- frozen already so the overriding primitive is
773 while Present (Decl_Item)
774 and then (Decl_Item /= Subp_Body)
776 if Comes_From_Source (Decl_Item)
777 and then (Nkind (Decl_Item) in N_Proper_Body
778 or else Nkind (Decl_Item) in N_Body_Stub)
780 Error_Msg_N ("overriding of& is too late!", Subp);
782 ("\spec should appear immediately after the type!",
790 -- If the subprogram doesn't follow in the list of
791 -- declarations including the type then the type
792 -- has definitely been frozen already and the body
795 if No (Decl_Item) then
796 Error_Msg_N ("overriding of& is too late!", Subp);
798 ("\spec should appear immediately after the type!",
801 elsif Is_Frozen (Subp) then
803 -- The subprogram body declares a primitive operation.
804 -- if the subprogram is already frozen, we must update
805 -- its dispatching information explicitly here. The
806 -- information is taken from the overridden subprogram.
807 -- We must also generate a cross-reference entry because
808 -- references to other primitives were already created
809 -- when type was frozen.
811 Body_Is_Last_Primitive := True;
813 if Present (DTC_Entity (Old_Subp)) then
814 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
815 Set_DT_Position (Subp, DT_Position (Old_Subp));
817 if not Restriction_Active (No_Dispatching_Calls) then
818 if Building_Static_DT (Tagged_Type) then
820 -- If the static dispatch table has not been
821 -- built then there is nothing else to do now;
822 -- otherwise we notify that we cannot build the
823 -- static dispatch table.
825 if Has_Dispatch_Table (Tagged_Type) then
827 ("overriding of& is too late for building" &
828 " static dispatch tables!", Subp);
830 ("\spec should appear immediately after" &
835 Register_Primitive (Sloc (Subp_Body),
837 Ins_Nod => Subp_Body);
840 Generate_Reference (Tagged_Type, Subp, 'p', False);
847 Error_Msg_N ("overriding of& is too late!", Subp);
849 ("\subprogram spec should appear immediately after the type!",
853 -- If the type is not frozen yet and we are not in the overriding
854 -- case it looks suspiciously like an attempt to define a primitive
857 elsif not Is_Frozen (Tagged_Type) then
859 ("?not dispatching (must be defined in a package spec)", Subp);
862 -- When the type is frozen, it is legitimate to define a new
863 -- non-primitive operation.
869 -- Now, we are sure that the scope is a package spec. If the subprogram
870 -- is declared after the freezing point of the type that's an error
872 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
873 Error_Msg_N ("this primitive operation is declared too late", Subp);
875 ("?no primitive operations for& after this line",
876 Freeze_Node (Tagged_Type),
881 Check_Controlling_Formals (Tagged_Type, Subp);
883 -- Now it should be a correct primitive operation, put it in the list
885 if Present (Old_Subp) then
887 -- If the type has interfaces we complete this check after we
888 -- set attribute Is_Dispatching_Operation
890 Check_Subtype_Conformant (Subp, Old_Subp);
892 if (Chars (Subp) = Name_Initialize
893 or else Chars (Subp) = Name_Adjust
894 or else Chars (Subp) = Name_Finalize)
895 and then Is_Controlled (Tagged_Type)
896 and then not Is_Visibly_Controlled (Tagged_Type)
898 Set_Is_Overriding_Operation (Subp, False);
900 ("operation does not override inherited&?", Subp, Subp);
902 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
903 Set_Is_Overriding_Operation (Subp);
905 -- Ada 2005 (AI-251): In case of late overriding of a primitive
906 -- that covers abstract interface subprograms we must register it
907 -- in all the secondary dispatch tables associated with abstract
910 if Body_Is_Last_Primitive then
912 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
917 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
918 while Present (Elmt) loop
921 if Present (Alias (Prim))
922 and then Present (Interface_Alias (Prim))
923 and then Alias (Prim) = Subp
925 Register_Primitive (Sloc (Prim),
927 Ins_Nod => Subp_Body);
933 -- Redisplay the contents of the updated dispatch table
935 if Debug_Flag_ZZ then
936 Write_Str ("Late overriding: ");
937 Write_DT (Tagged_Type);
943 -- If no old subprogram, then we add this as a dispatching operation,
944 -- but we avoid doing this if an error was posted, to prevent annoying
947 elsif not Error_Posted (Subp) then
948 Add_Dispatching_Operation (Tagged_Type, Subp);
951 Set_Is_Dispatching_Operation (Subp, True);
953 -- Ada 2005 (AI-251): If the type implements interfaces we must check
954 -- subtype conformance against all the interfaces covered by this
957 if Present (Old_Subp)
958 and then Has_Interfaces (Tagged_Type)
961 Ifaces_List : Elist_Id;
962 Iface_Elmt : Elmt_Id;
963 Iface_Prim_Elmt : Elmt_Id;
964 Iface_Prim : Entity_Id;
968 Collect_Interfaces (Tagged_Type, Ifaces_List);
970 Iface_Elmt := First_Elmt (Ifaces_List);
971 while Present (Iface_Elmt) loop
972 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
974 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
975 while Present (Iface_Prim_Elmt) loop
976 Iface_Prim := Node (Iface_Prim_Elmt);
978 if Is_Interface_Conformant
979 (Tagged_Type, Iface_Prim, Subp)
981 -- Handle procedures, functions whose return type
982 -- matches, or functions not returning interfaces
984 if Ekind (Subp) = E_Procedure
985 or else Etype (Iface_Prim) = Etype (Subp)
986 or else not Is_Interface (Etype (Iface_Prim))
988 Check_Subtype_Conformant
990 Old_Id => Iface_Prim,
992 Skip_Controlling_Formals => True);
994 -- Handle functions returning interfaces
996 elsif Implements_Interface
997 (Etype (Subp), Etype (Iface_Prim))
999 -- Temporarily force both entities to return the
1000 -- same type. Required because Subtype_Conformant
1001 -- does not handle this case.
1003 Ret_Typ := Etype (Iface_Prim);
1004 Set_Etype (Iface_Prim, Etype (Subp));
1006 Check_Subtype_Conformant
1008 Old_Id => Iface_Prim,
1010 Skip_Controlling_Formals => True);
1012 Set_Etype (Iface_Prim, Ret_Typ);
1016 Next_Elmt (Iface_Prim_Elmt);
1020 Next_Elmt (Iface_Elmt);
1025 if not Body_Is_Last_Primitive then
1026 Set_DT_Position (Subp, No_Uint);
1028 elsif Has_Controlled_Component (Tagged_Type)
1030 (Chars (Subp) = Name_Initialize
1031 or else Chars (Subp) = Name_Adjust
1032 or else Chars (Subp) = Name_Finalize)
1035 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1039 Old_Spec : Entity_Id;
1041 C_Names : constant array (1 .. 3) of Name_Id :=
1046 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1047 (TSS_Deep_Initialize,
1052 -- Remove previous controlled function, which was constructed
1053 -- and analyzed when the type was frozen. This requires
1054 -- removing the body of the redefined primitive, as well as
1055 -- its specification if needed (there is no spec created for
1056 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
1057 -- the exception information that may have been generated for
1058 -- it when front end zero-cost tables are enabled.
1060 for J in D_Names'Range loop
1061 Old_P := TSS (Tagged_Type, D_Names (J));
1064 and then Chars (Subp) = C_Names (J)
1066 Old_Bod := Unit_Declaration_Node (Old_P);
1068 Set_Is_Eliminated (Old_P);
1069 Set_Scope (Old_P, Scope (Current_Scope));
1071 if Nkind (Old_Bod) = N_Subprogram_Body
1072 and then Present (Corresponding_Spec (Old_Bod))
1074 Old_Spec := Corresponding_Spec (Old_Bod);
1075 Set_Has_Completion (Old_Spec, False);
1080 Build_Late_Proc (Tagged_Type, Chars (Subp));
1082 -- The new operation is added to the actions of the freeze
1083 -- node for the type, but this node has already been analyzed,
1084 -- so we must retrieve and analyze explicitly the new body.
1087 and then Present (Actions (F_Node))
1089 Decl := Last (Actions (F_Node));
1094 end Check_Dispatching_Operation;
1096 ------------------------------------------
1097 -- Check_Operation_From_Incomplete_Type --
1098 ------------------------------------------
1100 procedure Check_Operation_From_Incomplete_Type
1104 Full : constant Entity_Id := Full_View (Typ);
1105 Parent_Typ : constant Entity_Id := Etype (Full);
1106 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1107 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1109 Prev : Elmt_Id := No_Elmt;
1111 function Derives_From (Proc : Entity_Id) return Boolean;
1112 -- Check that Subp has the signature of an operation derived from Proc.
1113 -- Subp has an access parameter that designates Typ.
1119 function Derives_From (Proc : Entity_Id) return Boolean is
1123 if Chars (Proc) /= Chars (Subp) then
1127 F1 := First_Formal (Proc);
1128 F2 := First_Formal (Subp);
1130 while Present (F1) and then Present (F2) loop
1132 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1134 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1137 elsif Designated_Type (Etype (F1)) = Parent_Typ
1138 and then Designated_Type (Etype (F2)) /= Full
1143 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1146 elsif Etype (F1) /= Etype (F2) then
1154 return No (F1) and then No (F2);
1157 -- Start of processing for Check_Operation_From_Incomplete_Type
1160 -- The operation may override an inherited one, or may be a new one
1161 -- altogether. The inherited operation will have been hidden by the
1162 -- current one at the point of the type derivation, so it does not
1163 -- appear in the list of primitive operations of the type. We have to
1164 -- find the proper place of insertion in the list of primitive opera-
1165 -- tions by iterating over the list for the parent type.
1167 Op1 := First_Elmt (Old_Prim);
1168 Op2 := First_Elmt (New_Prim);
1170 while Present (Op1) and then Present (Op2) loop
1172 if Derives_From (Node (Op1)) then
1176 -- Avoid adding it to the list of primitives if already there!
1178 if Node (Op2) /= Subp then
1179 Prepend_Elmt (Subp, New_Prim);
1183 Insert_Elmt_After (Subp, Prev);
1194 -- Operation is a new primitive
1196 Append_Elmt (Subp, New_Prim);
1197 end Check_Operation_From_Incomplete_Type;
1199 ---------------------------------------
1200 -- Check_Operation_From_Private_View --
1201 ---------------------------------------
1203 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1204 Tagged_Type : Entity_Id;
1207 if Is_Dispatching_Operation (Alias (Subp)) then
1208 Set_Scope (Subp, Current_Scope);
1209 Tagged_Type := Find_Dispatching_Type (Subp);
1211 -- Add Old_Subp to primitive operations if not already present.
1213 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1214 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1216 -- If Old_Subp isn't already marked as dispatching then
1217 -- this is the case of an operation of an untagged private
1218 -- type fulfilled by a tagged type that overrides an
1219 -- inherited dispatching operation, so we set the necessary
1220 -- dispatching attributes here.
1222 if not Is_Dispatching_Operation (Old_Subp) then
1224 -- If the untagged type has no discriminants, and the full
1225 -- view is constrained, there will be a spurious mismatch
1226 -- of subtypes on the controlling arguments, because the tagged
1227 -- type is the internal base type introduced in the derivation.
1228 -- Use the original type to verify conformance, rather than the
1231 if not Comes_From_Source (Tagged_Type)
1232 and then Has_Discriminants (Tagged_Type)
1237 Formal := First_Formal (Old_Subp);
1238 while Present (Formal) loop
1239 if Tagged_Type = Base_Type (Etype (Formal)) then
1240 Tagged_Type := Etype (Formal);
1243 Next_Formal (Formal);
1247 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1248 Tagged_Type := Etype (Old_Subp);
1252 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1253 Set_Is_Dispatching_Operation (Old_Subp, True);
1254 Set_DT_Position (Old_Subp, No_Uint);
1257 -- If the old subprogram is an explicit renaming of some other
1258 -- entity, it is not overridden by the inherited subprogram.
1259 -- Otherwise, update its alias and other attributes.
1261 if Present (Alias (Old_Subp))
1262 and then Nkind (Unit_Declaration_Node (Old_Subp))
1263 /= N_Subprogram_Renaming_Declaration
1265 Set_Alias (Old_Subp, Alias (Subp));
1267 -- The derived subprogram should inherit the abstractness
1268 -- of the parent subprogram (except in the case of a function
1269 -- returning the type). This sets the abstractness properly
1270 -- for cases where a private extension may have inherited
1271 -- an abstract operation, but the full type is derived from
1272 -- a descendant type and inherits a nonabstract version.
1274 if Etype (Subp) /= Tagged_Type then
1275 Set_Is_Abstract_Subprogram
1276 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1281 end Check_Operation_From_Private_View;
1283 --------------------------
1284 -- Find_Controlling_Arg --
1285 --------------------------
1287 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1288 Orig_Node : constant Node_Id := Original_Node (N);
1292 if Nkind (Orig_Node) = N_Qualified_Expression then
1293 return Find_Controlling_Arg (Expression (Orig_Node));
1296 -- Dispatching on result case. If expansion is disabled, the node still
1297 -- has the structure of a function call. However, if the function name
1298 -- is an operator and the call was given in infix form, the original
1299 -- node has no controlling result and we must examine the current node.
1301 if Nkind (N) = N_Function_Call
1302 and then Present (Controlling_Argument (N))
1303 and then Has_Controlling_Result (Entity (Name (N)))
1305 return Controlling_Argument (N);
1307 -- If expansion is enabled, the call may have been transformed into
1308 -- an indirect call, and we need to recover the original node.
1310 elsif Nkind (Orig_Node) = N_Function_Call
1311 and then Present (Controlling_Argument (Orig_Node))
1312 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1314 return Controlling_Argument (Orig_Node);
1318 elsif Is_Controlling_Actual (N)
1320 (Nkind (Parent (N)) = N_Qualified_Expression
1321 and then Is_Controlling_Actual (Parent (N)))
1325 if Is_Access_Type (Typ) then
1326 -- In the case of an Access attribute, use the type of
1327 -- the prefix, since in the case of an actual for an
1328 -- access parameter, the attribute's type may be of a
1329 -- specific designated type, even though the prefix
1330 -- type is class-wide.
1332 if Nkind (N) = N_Attribute_Reference then
1333 Typ := Etype (Prefix (N));
1335 -- An allocator is dispatching if the type of qualified
1336 -- expression is class_wide, in which case this is the
1337 -- controlling type.
1339 elsif Nkind (Orig_Node) = N_Allocator
1340 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1342 Typ := Etype (Expression (Orig_Node));
1345 Typ := Designated_Type (Typ);
1349 if Is_Class_Wide_Type (Typ)
1351 (Nkind (Parent (N)) = N_Qualified_Expression
1352 and then Is_Access_Type (Etype (N))
1353 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1360 end Find_Controlling_Arg;
1362 ---------------------------
1363 -- Find_Dispatching_Type --
1364 ---------------------------
1366 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1368 Ctrl_Type : Entity_Id;
1371 if Present (DTC_Entity (Subp)) then
1372 return Scope (DTC_Entity (Subp));
1375 Formal := First_Formal (Subp);
1376 while Present (Formal) loop
1377 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1379 if Present (Ctrl_Type) then
1383 Next_Formal (Formal);
1386 -- The subprogram may also be dispatching on result
1388 if Present (Etype (Subp)) then
1389 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1391 if Present (Ctrl_Type) then
1398 end Find_Dispatching_Type;
1400 ---------------------------------------
1401 -- Find_Primitive_Covering_Interface --
1402 ---------------------------------------
1404 function Find_Primitive_Covering_Interface
1405 (Tagged_Type : Entity_Id;
1406 Iface_Prim : Entity_Id) return Entity_Id
1411 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1412 or else (Present (Alias (Iface_Prim))
1415 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1417 E := Current_Entity (Iface_Prim);
1418 while Present (E) loop
1419 if Is_Subprogram (E)
1420 and then Is_Dispatching_Operation (E)
1421 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1430 end Find_Primitive_Covering_Interface;
1432 ---------------------------
1433 -- Is_Dynamically_Tagged --
1434 ---------------------------
1436 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1438 if Nkind (N) = N_Error then
1441 return Find_Controlling_Arg (N) /= Empty;
1443 end Is_Dynamically_Tagged;
1445 --------------------------
1446 -- Is_Tag_Indeterminate --
1447 --------------------------
1449 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1452 Orig_Node : constant Node_Id := Original_Node (N);
1455 if Nkind (Orig_Node) = N_Function_Call
1456 and then Is_Entity_Name (Name (Orig_Node))
1458 Nam := Entity (Name (Orig_Node));
1460 if not Has_Controlling_Result (Nam) then
1463 -- An explicit dereference means that the call has already been
1464 -- expanded and there is no tag to propagate.
1466 elsif Nkind (N) = N_Explicit_Dereference then
1469 -- If there are no actuals, the call is tag-indeterminate
1471 elsif No (Parameter_Associations (Orig_Node)) then
1475 Actual := First_Actual (Orig_Node);
1476 while Present (Actual) loop
1477 if Is_Controlling_Actual (Actual)
1478 and then not Is_Tag_Indeterminate (Actual)
1480 return False; -- one operand is dispatching
1483 Next_Actual (Actual);
1489 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1490 return Is_Tag_Indeterminate (Expression (Orig_Node));
1492 -- Case of a call to the Input attribute (possibly rewritten), which is
1493 -- always tag-indeterminate except when its prefix is a Class attribute.
1495 elsif Nkind (Orig_Node) = N_Attribute_Reference
1497 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1499 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1503 -- In Ada 2005 a function that returns an anonymous access type can
1504 -- dispatching, and the dereference of a call to such a function
1505 -- is also tag-indeterminate.
1507 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1508 and then Ada_Version >= Ada_05
1510 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1515 end Is_Tag_Indeterminate;
1517 ------------------------------------
1518 -- Override_Dispatching_Operation --
1519 ------------------------------------
1521 procedure Override_Dispatching_Operation
1522 (Tagged_Type : Entity_Id;
1523 Prev_Op : Entity_Id;
1530 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1531 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1533 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1534 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1535 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1538 -- If there is no previous operation to override, the type declaration
1539 -- was malformed, and an error must have been emitted already.
1541 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1542 while Present (Elmt)
1543 and then Node (Elmt) /= Prev_Op
1552 Replace_Elmt (Elmt, New_Op);
1554 if Ada_Version >= Ada_05
1555 and then Has_Interfaces (Tagged_Type)
1557 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1558 -- entities of the overridden primitive to reference New_Op, and also
1559 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1560 -- that the new operation is subtype conformant with the interface
1561 -- operations that it implements (for operations inherited from the
1562 -- parent itself, this check is made when building the derived type).
1564 -- Note: This code is only executed in case of late overriding
1566 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1567 while Present (Elmt) loop
1568 Prim := Node (Elmt);
1570 if Prim = New_Op then
1573 -- Note: The check on Is_Subprogram protects the frontend against
1574 -- reading attributes in entities that are not yet fully decorated
1576 elsif Is_Subprogram (Prim)
1577 and then Present (Interface_Alias (Prim))
1578 and then Alias (Prim) = Prev_Op
1579 and then Present (Etype (New_Op))
1581 Set_Alias (Prim, New_Op);
1582 Check_Subtype_Conformant (New_Op, Prim);
1583 Set_Is_Abstract_Subprogram (Prim,
1584 Is_Abstract_Subprogram (New_Op));
1586 -- Ensure that this entity will be expanded to fill the
1587 -- corresponding entry in its dispatch table.
1589 if not Is_Abstract_Subprogram (Prim) then
1590 Set_Has_Delayed_Freeze (Prim);
1598 if (not Is_Package_Or_Generic_Package (Current_Scope))
1599 or else not In_Private_Part (Current_Scope)
1601 -- Not a private primitive
1605 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1607 -- Make the overriding operation into an alias of the implicit one.
1608 -- In this fashion a call from outside ends up calling the new body
1609 -- even if non-dispatching, and a call from inside calls the
1610 -- overriding operation because it hides the implicit one. To
1611 -- indicate that the body of Prev_Op is never called, set its
1612 -- dispatch table entity to Empty.
1614 Set_Alias (Prev_Op, New_Op);
1615 Set_DTC_Entity (Prev_Op, Empty);
1618 end Override_Dispatching_Operation;
1624 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1625 Call_Node : Node_Id;
1629 if Nkind (Actual) = N_Function_Call then
1630 Call_Node := Actual;
1632 elsif Nkind (Actual) = N_Identifier
1633 and then Nkind (Original_Node (Actual)) = N_Function_Call
1635 -- Call rewritten as object declaration when stack-checking
1636 -- is enabled. Propagate tag to expression in declaration, which
1637 -- is original call.
1639 Call_Node := Expression (Parent (Entity (Actual)));
1641 -- Ada 2005: If this is a dereference of a call to a function with a
1642 -- dispatching access-result, the tag is propagated when the dereference
1643 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1645 elsif Nkind (Actual) = N_Explicit_Dereference
1646 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1650 -- Only other possibilities are parenthesized or qualified expression,
1651 -- or an expander-generated unchecked conversion of a function call to
1652 -- a stream Input attribute.
1655 Call_Node := Expression (Actual);
1658 -- Do not set the Controlling_Argument if already set. This happens
1659 -- in the special case of _Input (see Exp_Attr, case Input).
1661 if No (Controlling_Argument (Call_Node)) then
1662 Set_Controlling_Argument (Call_Node, Control);
1665 Arg := First_Actual (Call_Node);
1667 while Present (Arg) loop
1668 if Is_Tag_Indeterminate (Arg) then
1669 Propagate_Tag (Control, Arg);
1675 -- Expansion of dispatching calls is suppressed when VM_Target, because
1676 -- the VM back-ends directly handle the generation of dispatching
1677 -- calls and would have to undo any expansion to an indirect call.
1679 if VM_Target = No_VM then
1680 Expand_Dispatching_Call (Call_Node);
1682 -- Expansion of a dispatching call results in an indirect call, which in
1683 -- turn causes current values to be killed (see Resolve_Call), so on VM
1684 -- targets we do the call here to ensure consistent warnings between VM
1685 -- and non-VM targets.
1688 Kill_Current_Values;