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_Aux; use Sem_Aux;
44 with Sem_Ch6; use Sem_Ch6;
45 with Sem_Elim; use Sem_Elim;
46 with Sem_Eval; use Sem_Eval;
47 with Sem_Type; use Sem_Type;
48 with Sem_Util; use Sem_Util;
49 with Snames; use Snames;
50 with Stand; use Stand;
51 with Sinfo; use Sinfo;
52 with Targparm; use Targparm;
53 with Tbuild; use Tbuild;
54 with Uintp; use Uintp;
56 package body Sem_Disp is
58 -----------------------
59 -- Local Subprograms --
60 -----------------------
62 procedure Add_Dispatching_Operation
63 (Tagged_Type : Entity_Id;
65 -- Add New_Op in the list of primitive operations of Tagged_Type
67 function Check_Controlling_Type
69 Subp : Entity_Id) return Entity_Id;
70 -- T is the tagged type of a formal parameter or the result of Subp.
71 -- If the subprogram has a controlling parameter or result that matches
72 -- the type, then returns the tagged type of that parameter or result
73 -- (returning the designated tagged type in the case of an access
74 -- parameter); otherwise returns empty.
76 -------------------------------
77 -- Add_Dispatching_Operation --
78 -------------------------------
80 procedure Add_Dispatching_Operation
81 (Tagged_Type : Entity_Id;
84 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
87 -- The dispatching operation may already be on the list, if it is the
88 -- wrapper for an inherited function of a null extension (see Exp_Ch3
89 -- for the construction of function wrappers). The list of primitive
90 -- operations must not contain duplicates.
92 Append_Unique_Elmt (New_Op, List);
93 end Add_Dispatching_Operation;
95 -------------------------------
96 -- Check_Controlling_Formals --
97 -------------------------------
99 procedure Check_Controlling_Formals
104 Ctrl_Type : Entity_Id;
107 Formal := First_Formal (Subp);
109 while Present (Formal) loop
110 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
112 if Present (Ctrl_Type) then
114 -- When the controlling type is concurrent and declared within a
115 -- generic or inside an instance, use its corresponding record
118 if Is_Concurrent_Type (Ctrl_Type)
119 and then Present (Corresponding_Record_Type (Ctrl_Type))
121 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
124 if Ctrl_Type = Typ then
125 Set_Is_Controlling_Formal (Formal);
127 -- Ada 2005 (AI-231): Anonymous access types used in
128 -- controlling parameters exclude null because it is necessary
129 -- to read the tag to dispatch, and null has no tag.
131 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
132 Set_Can_Never_Be_Null (Etype (Formal));
133 Set_Is_Known_Non_Null (Etype (Formal));
136 -- Check that the parameter's nominal subtype statically
137 -- matches the first subtype.
139 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
140 if not Subtypes_Statically_Match
141 (Typ, Designated_Type (Etype (Formal)))
144 ("parameter subtype does not match controlling type",
148 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
150 ("parameter subtype does not match controlling type",
154 if Present (Default_Value (Formal)) then
156 -- In Ada 2005, access parameters can have defaults
158 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
159 and then Ada_Version < Ada_05
162 ("default not allowed for controlling access parameter",
163 Default_Value (Formal));
165 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
167 ("default expression must be a tag indeterminate" &
168 " function call", Default_Value (Formal));
172 elsif Comes_From_Source (Subp) then
174 ("operation can be dispatching in only one type", Subp);
178 Next_Formal (Formal);
181 if Present (Etype (Subp)) then
182 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
184 if Present (Ctrl_Type) then
185 if Ctrl_Type = Typ then
186 Set_Has_Controlling_Result (Subp);
188 -- Check that result subtype statically matches first subtype
189 -- (Ada 2005): Subp may have a controlling access result.
191 if Subtypes_Statically_Match (Typ, Etype (Subp))
192 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
194 Subtypes_Statically_Match
195 (Typ, Designated_Type (Etype (Subp))))
201 ("result subtype does not match controlling type", Subp);
204 elsif Comes_From_Source (Subp) then
206 ("operation can be dispatching in only one type", Subp);
210 end Check_Controlling_Formals;
212 ----------------------------
213 -- Check_Controlling_Type --
214 ----------------------------
216 function Check_Controlling_Type
218 Subp : Entity_Id) return Entity_Id
220 Tagged_Type : Entity_Id := Empty;
223 if Is_Tagged_Type (T) then
224 if Is_First_Subtype (T) then
227 Tagged_Type := Base_Type (T);
230 elsif Ekind (T) = E_Anonymous_Access_Type
231 and then Is_Tagged_Type (Designated_Type (T))
233 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
234 if Is_First_Subtype (Designated_Type (T)) then
235 Tagged_Type := Designated_Type (T);
237 Tagged_Type := Base_Type (Designated_Type (T));
240 -- Ada 2005: an incomplete type can be tagged. An operation with an
241 -- access parameter of the type is dispatching.
243 elsif Scope (Designated_Type (T)) = Current_Scope then
244 Tagged_Type := Designated_Type (T);
246 -- Ada 2005 (AI-50217)
248 elsif From_With_Type (Designated_Type (T))
249 and then Present (Non_Limited_View (Designated_Type (T)))
251 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
252 Tagged_Type := Non_Limited_View (Designated_Type (T));
254 Tagged_Type := Base_Type (Non_Limited_View
255 (Designated_Type (T)));
260 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
263 -- The dispatching type and the primitive operation must be defined in
264 -- the same scope, except in the case of internal operations and formal
265 -- 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, call is
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 tag-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 an
415 -- indeterminate actual that requires dispatching treatment, then an
416 -- object is needed that will serve as the controlling argument for a
417 -- dispatching call on the indeterminate actual. This can only occur
418 -- in the unusual situation of a default actual given by a
419 -- tag-indeterminate call and where the type of the call is an
420 -- ancestor of the type associated with a containing call to an
421 -- inherited operation (see AI-239).
423 -- Rather than create an object of the tagged type, which would be
424 -- problematic for various reasons (default initialization,
425 -- discriminants), the tag of the containing call's associated tagged
426 -- type is directly used to control the dispatching.
429 and then Indeterm_Ancestor_Call
430 and then No (Static_Tag)
433 Make_Attribute_Reference (Loc,
434 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
435 Attribute_Name => Name_Tag);
440 if Present (Control) then
442 -- Verify that no controlling arguments are statically tagged
445 Write_Str ("Found Dispatching call");
450 Actual := First_Actual (N);
452 while Present (Actual) loop
453 if Actual /= Control then
455 if not Is_Controlling_Actual (Actual) then
456 null; -- Can be anything
458 elsif Is_Dynamically_Tagged (Actual) then
459 null; -- Valid parameter
461 elsif Is_Tag_Indeterminate (Actual) then
463 -- The tag is inherited from the enclosing call (the node
464 -- we are currently analyzing). Explicitly expand the
465 -- actual, since the previous call to Expand (from
466 -- Resolve_Call) had no way of knowing about the required
469 Propagate_Tag (Control, Actual);
473 ("controlling argument is not dynamically tagged",
479 Next_Actual (Actual);
482 -- Mark call as a dispatching call
484 Set_Controlling_Argument (N, Control);
485 Check_Restriction (No_Dispatching_Calls, N);
487 if Is_Eliminated (Ultimate_Alias (Subp_Entity)) then
488 Eliminate_Error_Msg (N, Ultimate_Alias (Subp_Entity));
491 -- If there is a statically tagged actual and a tag-indeterminate
492 -- call to a function of the ancestor (such as that provided by a
493 -- default), then treat this as a dispatching call and propagate
494 -- the tag to the tag-indeterminate call(s).
496 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
498 Make_Attribute_Reference (Loc,
500 New_Occurrence_Of (Etype (Static_Tag), Loc),
501 Attribute_Name => Name_Tag);
505 Actual := First_Actual (N);
506 Formal := First_Formal (Subp_Entity);
507 while Present (Actual) loop
508 if Is_Tag_Indeterminate (Actual)
509 and then Is_Controlling_Formal (Formal)
511 Propagate_Tag (Control, Actual);
514 Next_Actual (Actual);
515 Next_Formal (Formal);
518 Check_Dispatching_Context;
521 -- The call is not dispatching, so check that there aren't any
522 -- tag-indeterminate abstract calls left.
524 Actual := First_Actual (N);
525 while Present (Actual) loop
526 if Is_Tag_Indeterminate (Actual) then
528 -- Function call case
530 if Nkind (Original_Node (Actual)) = N_Function_Call then
531 Func := Entity (Name (Original_Node (Actual)));
533 -- If the actual is an attribute then it can't be abstract
534 -- (the only current case of a tag-indeterminate attribute
535 -- is the stream Input attribute).
538 Nkind (Original_Node (Actual)) = N_Attribute_Reference
542 -- Only other possibility is a qualified expression whose
543 -- constituent expression is itself a call.
549 (Expression (Original_Node (Actual)))));
552 if Present (Func) and then Is_Abstract_Subprogram (Func) then
554 "call to abstract function must be dispatching", N);
558 Next_Actual (Actual);
561 Check_Dispatching_Context;
565 -- If dispatching on result, the enclosing call, if any, will
566 -- determine the controlling argument. Otherwise this is the
567 -- primitive operation of the root type.
569 Check_Dispatching_Context;
571 end Check_Dispatching_Call;
573 ---------------------------------
574 -- Check_Dispatching_Operation --
575 ---------------------------------
577 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
578 Tagged_Type : Entity_Id;
579 Has_Dispatching_Parent : Boolean := False;
580 Body_Is_Last_Primitive : Boolean := False;
582 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
583 -- Check whether T is derived from a visibly controlled type.
584 -- This is true if the root type is declared in Ada.Finalization.
585 -- If T is derived instead from a private type whose full view
586 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
587 -- does not override the inherited one.
589 ---------------------------
590 -- Is_Visibly_Controlled --
591 ---------------------------
593 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
594 Root : constant Entity_Id := Root_Type (T);
596 return Chars (Scope (Root)) = Name_Finalization
597 and then Chars (Scope (Scope (Root))) = Name_Ada
598 and then Scope (Scope (Scope (Root))) = Standard_Standard;
599 end Is_Visibly_Controlled;
601 -- Start of processing for Check_Dispatching_Operation
604 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
608 Set_Is_Dispatching_Operation (Subp, False);
609 Tagged_Type := Find_Dispatching_Type (Subp);
613 if Ada_Version = Ada_05
614 and then Present (Tagged_Type)
615 and then Is_Concurrent_Type (Tagged_Type)
617 -- Protect the frontend against previously detected errors
619 if No (Corresponding_Record_Type (Tagged_Type)) then
623 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
626 -- (AI-345): The task body procedure is not a primitive of the tagged
629 if Present (Tagged_Type)
630 and then Is_Concurrent_Record_Type (Tagged_Type)
631 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
632 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
633 and then Subp = Get_Task_Body_Procedure
634 (Corresponding_Concurrent_Type (Tagged_Type))
639 -- If Subp is derived from a dispatching operation then it should
640 -- always be treated as dispatching. In this case various checks
641 -- below will be bypassed. Makes sure that late declarations for
642 -- inherited private subprograms are treated as dispatching, even
643 -- if the associated tagged type is already frozen.
645 Has_Dispatching_Parent :=
646 Present (Alias (Subp))
647 and then Is_Dispatching_Operation (Alias (Subp));
649 if No (Tagged_Type) then
651 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
652 -- with an abstract interface type unless the interface acts as a
653 -- parent type in a derivation. If the interface type is a formal
654 -- type then the operation is not primitive and therefore legal.
661 E := First_Entity (Subp);
662 while Present (E) loop
664 -- For an access parameter, check designated type.
666 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
667 Typ := Designated_Type (Etype (E));
672 if Comes_From_Source (Subp)
673 and then Is_Interface (Typ)
674 and then not Is_Class_Wide_Type (Typ)
675 and then not Is_Derived_Type (Typ)
676 and then not Is_Generic_Type (Typ)
677 and then not In_Instance
679 Error_Msg_N ("?declaration of& is too late!", Subp);
681 ("\spec should appear immediately after declaration of &!",
689 -- In case of functions check also the result type
691 if Ekind (Subp) = E_Function then
692 if Is_Access_Type (Etype (Subp)) then
693 Typ := Designated_Type (Etype (Subp));
698 if not Is_Class_Wide_Type (Typ)
699 and then Is_Interface (Typ)
700 and then not Is_Derived_Type (Typ)
702 Error_Msg_N ("?declaration of& is too late!", Subp);
704 ("\spec should appear immediately after declaration of &!",
712 -- The subprograms build internally after the freezing point (such as
713 -- the Init procedure) are not primitives
715 elsif Is_Frozen (Tagged_Type)
716 and then not Comes_From_Source (Subp)
717 and then not Has_Dispatching_Parent
721 -- The operation may be a child unit, whose scope is the defining
722 -- package, but which is not a primitive operation of the type.
724 elsif Is_Child_Unit (Subp) then
727 -- If the subprogram is not defined in a package spec, the only case
728 -- where it can be a dispatching op is when it overrides an operation
729 -- before the freezing point of the type.
731 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
732 or else In_Package_Body (Scope (Subp)))
733 and then not Has_Dispatching_Parent
735 if not Comes_From_Source (Subp)
736 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
740 -- If the type is already frozen, the overriding is not allowed
741 -- except when Old_Subp is not a dispatching operation (which
742 -- can occur when Old_Subp was inherited by an untagged type).
743 -- However, a body with no previous spec freezes the type "after"
744 -- its declaration, and therefore is a legal overriding (unless
745 -- the type has already been frozen). Only the first such body
748 elsif Present (Old_Subp)
749 and then Is_Dispatching_Operation (Old_Subp)
751 if Comes_From_Source (Subp)
753 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
754 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
757 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
758 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
761 -- ??? The checks here for whether the type has been
762 -- frozen prior to the new body are not complete. It's
763 -- not simple to check frozenness at this point since
764 -- the body has already caused the type to be prematurely
765 -- frozen in Analyze_Declarations, but we're forced to
766 -- recheck this here because of the odd rule interpretation
767 -- that allows the overriding if the type wasn't frozen
768 -- prior to the body. The freezing action should probably
769 -- be delayed until after the spec is seen, but that's
770 -- a tricky change to the delicate freezing code.
772 -- Look at each declaration following the type up until the
773 -- new subprogram body. If any of the declarations is a body
774 -- then the type has been frozen already so the overriding
775 -- primitive is illegal.
777 while Present (Decl_Item)
778 and then (Decl_Item /= Subp_Body)
780 if Comes_From_Source (Decl_Item)
781 and then (Nkind (Decl_Item) in N_Proper_Body
782 or else Nkind (Decl_Item) in N_Body_Stub)
784 Error_Msg_N ("overriding of& is too late!", Subp);
786 ("\spec should appear immediately after the type!",
794 -- If the subprogram doesn't follow in the list of
795 -- declarations including the type then the type has
796 -- definitely been frozen already and the body is illegal.
798 if No (Decl_Item) then
799 Error_Msg_N ("overriding of& is too late!", Subp);
801 ("\spec should appear immediately after the type!",
804 elsif Is_Frozen (Subp) then
806 -- The subprogram body declares a primitive operation.
807 -- if the subprogram is already frozen, we must update
808 -- its dispatching information explicitly here. The
809 -- information is taken from the overridden subprogram.
810 -- We must also generate a cross-reference entry because
811 -- references to other primitives were already created
812 -- when type was frozen.
814 Body_Is_Last_Primitive := True;
816 if Present (DTC_Entity (Old_Subp)) then
817 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
818 Set_DT_Position (Subp, DT_Position (Old_Subp));
820 if not Restriction_Active (No_Dispatching_Calls) then
821 if Building_Static_DT (Tagged_Type) then
823 -- If the static dispatch table has not been
824 -- built then there is nothing else to do now;
825 -- otherwise we notify that we cannot build the
826 -- static dispatch table.
828 if Has_Dispatch_Table (Tagged_Type) then
830 ("overriding of& is too late for building" &
831 " static dispatch tables!", Subp);
833 ("\spec should appear immediately after" &
838 Register_Primitive (Sloc (Subp_Body),
840 Ins_Nod => Subp_Body);
843 Generate_Reference (Tagged_Type, Subp, 'p', False);
850 Error_Msg_N ("overriding of& is too late!", Subp);
852 ("\subprogram spec should appear immediately after the type!",
856 -- If the type is not frozen yet and we are not in the overriding
857 -- case it looks suspiciously like an attempt to define a primitive
858 -- operation, which requires the declaration to be in a package spec
861 elsif not Is_Frozen (Tagged_Type) then
863 ("?not dispatching (must be defined in a package spec)", Subp);
866 -- When the type is frozen, it is legitimate to define a new
867 -- non-primitive operation.
873 -- Now, we are sure that the scope is a package spec. If the subprogram
874 -- is declared after the freezing point of the type that's an error
876 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
877 Error_Msg_N ("this primitive operation is declared too late", Subp);
879 ("?no primitive operations for& after this line",
880 Freeze_Node (Tagged_Type),
885 Check_Controlling_Formals (Tagged_Type, Subp);
887 -- Now it should be a correct primitive operation, put it in the list
889 if Present (Old_Subp) then
891 -- If the type has interfaces we complete this check after we set
892 -- attribute Is_Dispatching_Operation.
894 Check_Subtype_Conformant (Subp, Old_Subp);
896 if (Chars (Subp) = Name_Initialize
897 or else Chars (Subp) = Name_Adjust
898 or else Chars (Subp) = Name_Finalize)
899 and then Is_Controlled (Tagged_Type)
900 and then not Is_Visibly_Controlled (Tagged_Type)
902 Set_Is_Overriding_Operation (Subp, False);
904 ("operation does not override inherited&?", Subp, Subp);
906 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
907 Set_Is_Overriding_Operation (Subp);
909 -- Ada 2005 (AI-251): In case of late overriding of a primitive
910 -- that covers abstract interface subprograms we must register it
911 -- in all the secondary dispatch tables associated with abstract
914 if Body_Is_Last_Primitive then
916 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
921 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
922 while Present (Elmt) loop
925 if Present (Alias (Prim))
926 and then Present (Interface_Alias (Prim))
927 and then Alias (Prim) = Subp
929 Register_Primitive (Sloc (Prim),
931 Ins_Nod => Subp_Body);
937 -- Redisplay the contents of the updated dispatch table
939 if Debug_Flag_ZZ then
940 Write_Str ("Late overriding: ");
941 Write_DT (Tagged_Type);
947 -- If no old subprogram, then we add this as a dispatching operation,
948 -- but we avoid doing this if an error was posted, to prevent annoying
951 elsif not Error_Posted (Subp) then
952 Add_Dispatching_Operation (Tagged_Type, Subp);
955 Set_Is_Dispatching_Operation (Subp, True);
957 -- Ada 2005 (AI-251): If the type implements interfaces we must check
958 -- subtype conformance against all the interfaces covered by this
961 if Present (Old_Subp)
962 and then Has_Interfaces (Tagged_Type)
965 Ifaces_List : Elist_Id;
966 Iface_Elmt : Elmt_Id;
967 Iface_Prim_Elmt : Elmt_Id;
968 Iface_Prim : Entity_Id;
972 Collect_Interfaces (Tagged_Type, Ifaces_List);
974 Iface_Elmt := First_Elmt (Ifaces_List);
975 while Present (Iface_Elmt) loop
976 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
978 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
979 while Present (Iface_Prim_Elmt) loop
980 Iface_Prim := Node (Iface_Prim_Elmt);
982 if Is_Interface_Conformant
983 (Tagged_Type, Iface_Prim, Subp)
985 -- Handle procedures, functions whose return type
986 -- matches, or functions not returning interfaces
988 if Ekind (Subp) = E_Procedure
989 or else Etype (Iface_Prim) = Etype (Subp)
990 or else not Is_Interface (Etype (Iface_Prim))
992 Check_Subtype_Conformant
994 Old_Id => Iface_Prim,
996 Skip_Controlling_Formals => True);
998 -- Handle functions returning interfaces
1000 elsif Implements_Interface
1001 (Etype (Subp), Etype (Iface_Prim))
1003 -- Temporarily force both entities to return the
1004 -- same type. Required because Subtype_Conformant
1005 -- does not handle this case.
1007 Ret_Typ := Etype (Iface_Prim);
1008 Set_Etype (Iface_Prim, Etype (Subp));
1010 Check_Subtype_Conformant
1012 Old_Id => Iface_Prim,
1014 Skip_Controlling_Formals => True);
1016 Set_Etype (Iface_Prim, Ret_Typ);
1020 Next_Elmt (Iface_Prim_Elmt);
1024 Next_Elmt (Iface_Elmt);
1029 if not Body_Is_Last_Primitive then
1030 Set_DT_Position (Subp, No_Uint);
1032 elsif Has_Controlled_Component (Tagged_Type)
1034 (Chars (Subp) = Name_Initialize
1035 or else Chars (Subp) = Name_Adjust
1036 or else Chars (Subp) = Name_Finalize)
1039 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1043 Old_Spec : Entity_Id;
1045 C_Names : constant array (1 .. 3) of Name_Id :=
1050 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1051 (TSS_Deep_Initialize,
1056 -- Remove previous controlled function, which was constructed
1057 -- and analyzed when the type was frozen. This requires
1058 -- removing the body of the redefined primitive, as well as
1059 -- its specification if needed (there is no spec created for
1060 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
1061 -- the exception information that may have been generated for
1062 -- it when front end zero-cost tables are enabled.
1064 for J in D_Names'Range loop
1065 Old_P := TSS (Tagged_Type, D_Names (J));
1068 and then Chars (Subp) = C_Names (J)
1070 Old_Bod := Unit_Declaration_Node (Old_P);
1072 Set_Is_Eliminated (Old_P);
1073 Set_Scope (Old_P, Scope (Current_Scope));
1075 if Nkind (Old_Bod) = N_Subprogram_Body
1076 and then Present (Corresponding_Spec (Old_Bod))
1078 Old_Spec := Corresponding_Spec (Old_Bod);
1079 Set_Has_Completion (Old_Spec, False);
1084 Build_Late_Proc (Tagged_Type, Chars (Subp));
1086 -- The new operation is added to the actions of the freeze
1087 -- node for the type, but this node has already been analyzed,
1088 -- so we must retrieve and analyze explicitly the new body.
1091 and then Present (Actions (F_Node))
1093 Decl := Last (Actions (F_Node));
1098 end Check_Dispatching_Operation;
1100 ------------------------------------------
1101 -- Check_Operation_From_Incomplete_Type --
1102 ------------------------------------------
1104 procedure Check_Operation_From_Incomplete_Type
1108 Full : constant Entity_Id := Full_View (Typ);
1109 Parent_Typ : constant Entity_Id := Etype (Full);
1110 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1111 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1113 Prev : Elmt_Id := No_Elmt;
1115 function Derives_From (Proc : Entity_Id) return Boolean;
1116 -- Check that Subp has the signature of an operation derived from Proc.
1117 -- Subp has an access parameter that designates Typ.
1123 function Derives_From (Proc : Entity_Id) return Boolean is
1127 if Chars (Proc) /= Chars (Subp) then
1131 F1 := First_Formal (Proc);
1132 F2 := First_Formal (Subp);
1134 while Present (F1) and then Present (F2) loop
1136 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1138 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1141 elsif Designated_Type (Etype (F1)) = Parent_Typ
1142 and then Designated_Type (Etype (F2)) /= Full
1147 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1150 elsif Etype (F1) /= Etype (F2) then
1158 return No (F1) and then No (F2);
1161 -- Start of processing for Check_Operation_From_Incomplete_Type
1164 -- The operation may override an inherited one, or may be a new one
1165 -- altogether. The inherited operation will have been hidden by the
1166 -- current one at the point of the type derivation, so it does not
1167 -- appear in the list of primitive operations of the type. We have to
1168 -- find the proper place of insertion in the list of primitive opera-
1169 -- tions by iterating over the list for the parent type.
1171 Op1 := First_Elmt (Old_Prim);
1172 Op2 := First_Elmt (New_Prim);
1174 while Present (Op1) and then Present (Op2) loop
1176 if Derives_From (Node (Op1)) then
1180 -- Avoid adding it to the list of primitives if already there!
1182 if Node (Op2) /= Subp then
1183 Prepend_Elmt (Subp, New_Prim);
1187 Insert_Elmt_After (Subp, Prev);
1198 -- Operation is a new primitive
1200 Append_Elmt (Subp, New_Prim);
1201 end Check_Operation_From_Incomplete_Type;
1203 ---------------------------------------
1204 -- Check_Operation_From_Private_View --
1205 ---------------------------------------
1207 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1208 Tagged_Type : Entity_Id;
1211 if Is_Dispatching_Operation (Alias (Subp)) then
1212 Set_Scope (Subp, Current_Scope);
1213 Tagged_Type := Find_Dispatching_Type (Subp);
1215 -- Add Old_Subp to primitive operations if not already present.
1217 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1218 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1220 -- If Old_Subp isn't already marked as dispatching then
1221 -- this is the case of an operation of an untagged private
1222 -- type fulfilled by a tagged type that overrides an
1223 -- inherited dispatching operation, so we set the necessary
1224 -- dispatching attributes here.
1226 if not Is_Dispatching_Operation (Old_Subp) then
1228 -- If the untagged type has no discriminants, and the full
1229 -- view is constrained, there will be a spurious mismatch
1230 -- of subtypes on the controlling arguments, because the tagged
1231 -- type is the internal base type introduced in the derivation.
1232 -- Use the original type to verify conformance, rather than the
1235 if not Comes_From_Source (Tagged_Type)
1236 and then Has_Discriminants (Tagged_Type)
1241 Formal := First_Formal (Old_Subp);
1242 while Present (Formal) loop
1243 if Tagged_Type = Base_Type (Etype (Formal)) then
1244 Tagged_Type := Etype (Formal);
1247 Next_Formal (Formal);
1251 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1252 Tagged_Type := Etype (Old_Subp);
1256 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1257 Set_Is_Dispatching_Operation (Old_Subp, True);
1258 Set_DT_Position (Old_Subp, No_Uint);
1261 -- If the old subprogram is an explicit renaming of some other
1262 -- entity, it is not overridden by the inherited subprogram.
1263 -- Otherwise, update its alias and other attributes.
1265 if Present (Alias (Old_Subp))
1266 and then Nkind (Unit_Declaration_Node (Old_Subp))
1267 /= N_Subprogram_Renaming_Declaration
1269 Set_Alias (Old_Subp, Alias (Subp));
1271 -- The derived subprogram should inherit the abstractness
1272 -- of the parent subprogram (except in the case of a function
1273 -- returning the type). This sets the abstractness properly
1274 -- for cases where a private extension may have inherited
1275 -- an abstract operation, but the full type is derived from
1276 -- a descendant type and inherits a nonabstract version.
1278 if Etype (Subp) /= Tagged_Type then
1279 Set_Is_Abstract_Subprogram
1280 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1285 end Check_Operation_From_Private_View;
1287 --------------------------
1288 -- Find_Controlling_Arg --
1289 --------------------------
1291 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1292 Orig_Node : constant Node_Id := Original_Node (N);
1296 if Nkind (Orig_Node) = N_Qualified_Expression then
1297 return Find_Controlling_Arg (Expression (Orig_Node));
1300 -- Dispatching on result case. If expansion is disabled, the node still
1301 -- has the structure of a function call. However, if the function name
1302 -- is an operator and the call was given in infix form, the original
1303 -- node has no controlling result and we must examine the current node.
1305 if Nkind (N) = N_Function_Call
1306 and then Present (Controlling_Argument (N))
1307 and then Has_Controlling_Result (Entity (Name (N)))
1309 return Controlling_Argument (N);
1311 -- If expansion is enabled, the call may have been transformed into
1312 -- an indirect call, and we need to recover the original node.
1314 elsif Nkind (Orig_Node) = N_Function_Call
1315 and then Present (Controlling_Argument (Orig_Node))
1316 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1318 return Controlling_Argument (Orig_Node);
1322 elsif Is_Controlling_Actual (N)
1324 (Nkind (Parent (N)) = N_Qualified_Expression
1325 and then Is_Controlling_Actual (Parent (N)))
1329 if Is_Access_Type (Typ) then
1330 -- In the case of an Access attribute, use the type of
1331 -- the prefix, since in the case of an actual for an
1332 -- access parameter, the attribute's type may be of a
1333 -- specific designated type, even though the prefix
1334 -- type is class-wide.
1336 if Nkind (N) = N_Attribute_Reference then
1337 Typ := Etype (Prefix (N));
1339 -- An allocator is dispatching if the type of qualified
1340 -- expression is class_wide, in which case this is the
1341 -- controlling type.
1343 elsif Nkind (Orig_Node) = N_Allocator
1344 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1346 Typ := Etype (Expression (Orig_Node));
1349 Typ := Designated_Type (Typ);
1353 if Is_Class_Wide_Type (Typ)
1355 (Nkind (Parent (N)) = N_Qualified_Expression
1356 and then Is_Access_Type (Etype (N))
1357 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1364 end Find_Controlling_Arg;
1366 ---------------------------
1367 -- Find_Dispatching_Type --
1368 ---------------------------
1370 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1372 Ctrl_Type : Entity_Id;
1375 if Present (DTC_Entity (Subp)) then
1376 return Scope (DTC_Entity (Subp));
1379 Formal := First_Formal (Subp);
1380 while Present (Formal) loop
1381 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1383 if Present (Ctrl_Type) then
1387 Next_Formal (Formal);
1390 -- The subprogram may also be dispatching on result
1392 if Present (Etype (Subp)) then
1393 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1395 if Present (Ctrl_Type) then
1402 end Find_Dispatching_Type;
1404 ---------------------------------------
1405 -- Find_Primitive_Covering_Interface --
1406 ---------------------------------------
1408 function Find_Primitive_Covering_Interface
1409 (Tagged_Type : Entity_Id;
1410 Iface_Prim : Entity_Id) return Entity_Id
1415 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1416 or else (Present (Alias (Iface_Prim))
1419 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1421 E := Current_Entity (Iface_Prim);
1422 while Present (E) loop
1423 if Is_Subprogram (E)
1424 and then Is_Dispatching_Operation (E)
1425 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1434 end Find_Primitive_Covering_Interface;
1436 ---------------------------
1437 -- Is_Dynamically_Tagged --
1438 ---------------------------
1440 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1442 if Nkind (N) = N_Error then
1445 return Find_Controlling_Arg (N) /= Empty;
1447 end Is_Dynamically_Tagged;
1449 --------------------------
1450 -- Is_Tag_Indeterminate --
1451 --------------------------
1453 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1456 Orig_Node : constant Node_Id := Original_Node (N);
1459 if Nkind (Orig_Node) = N_Function_Call
1460 and then Is_Entity_Name (Name (Orig_Node))
1462 Nam := Entity (Name (Orig_Node));
1464 if not Has_Controlling_Result (Nam) then
1467 -- An explicit dereference means that the call has already been
1468 -- expanded and there is no tag to propagate.
1470 elsif Nkind (N) = N_Explicit_Dereference then
1473 -- If there are no actuals, the call is tag-indeterminate
1475 elsif No (Parameter_Associations (Orig_Node)) then
1479 Actual := First_Actual (Orig_Node);
1480 while Present (Actual) loop
1481 if Is_Controlling_Actual (Actual)
1482 and then not Is_Tag_Indeterminate (Actual)
1484 return False; -- one operand is dispatching
1487 Next_Actual (Actual);
1493 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1494 return Is_Tag_Indeterminate (Expression (Orig_Node));
1496 -- Case of a call to the Input attribute (possibly rewritten), which is
1497 -- always tag-indeterminate except when its prefix is a Class attribute.
1499 elsif Nkind (Orig_Node) = N_Attribute_Reference
1501 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1503 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1507 -- In Ada 2005 a function that returns an anonymous access type can
1508 -- dispatching, and the dereference of a call to such a function
1509 -- is also tag-indeterminate.
1511 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1512 and then Ada_Version >= Ada_05
1514 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1519 end Is_Tag_Indeterminate;
1521 ------------------------------------
1522 -- Override_Dispatching_Operation --
1523 ------------------------------------
1525 procedure Override_Dispatching_Operation
1526 (Tagged_Type : Entity_Id;
1527 Prev_Op : Entity_Id;
1534 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1535 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1537 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1538 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1539 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1542 -- If there is no previous operation to override, the type declaration
1543 -- was malformed, and an error must have been emitted already.
1545 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1546 while Present (Elmt)
1547 and then Node (Elmt) /= Prev_Op
1556 Replace_Elmt (Elmt, New_Op);
1558 if Ada_Version >= Ada_05
1559 and then Has_Interfaces (Tagged_Type)
1561 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1562 -- entities of the overridden primitive to reference New_Op, and also
1563 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1564 -- that the new operation is subtype conformant with the interface
1565 -- operations that it implements (for operations inherited from the
1566 -- parent itself, this check is made when building the derived type).
1568 -- Note: This code is only executed in case of late overriding
1570 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1571 while Present (Elmt) loop
1572 Prim := Node (Elmt);
1574 if Prim = New_Op then
1577 -- Note: The check on Is_Subprogram protects the frontend against
1578 -- reading attributes in entities that are not yet fully decorated
1580 elsif Is_Subprogram (Prim)
1581 and then Present (Interface_Alias (Prim))
1582 and then Alias (Prim) = Prev_Op
1583 and then Present (Etype (New_Op))
1585 Set_Alias (Prim, New_Op);
1586 Check_Subtype_Conformant (New_Op, Prim);
1587 Set_Is_Abstract_Subprogram (Prim,
1588 Is_Abstract_Subprogram (New_Op));
1590 -- Ensure that this entity will be expanded to fill the
1591 -- corresponding entry in its dispatch table.
1593 if not Is_Abstract_Subprogram (Prim) then
1594 Set_Has_Delayed_Freeze (Prim);
1602 if (not Is_Package_Or_Generic_Package (Current_Scope))
1603 or else not In_Private_Part (Current_Scope)
1605 -- Not a private primitive
1609 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1611 -- Make the overriding operation into an alias of the implicit one.
1612 -- In this fashion a call from outside ends up calling the new body
1613 -- even if non-dispatching, and a call from inside calls the
1614 -- overriding operation because it hides the implicit one. To
1615 -- indicate that the body of Prev_Op is never called, set its
1616 -- dispatch table entity to Empty.
1618 Set_Alias (Prev_Op, New_Op);
1619 Set_DTC_Entity (Prev_Op, Empty);
1622 end Override_Dispatching_Operation;
1628 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1629 Call_Node : Node_Id;
1633 if Nkind (Actual) = N_Function_Call then
1634 Call_Node := Actual;
1636 elsif Nkind (Actual) = N_Identifier
1637 and then Nkind (Original_Node (Actual)) = N_Function_Call
1639 -- Call rewritten as object declaration when stack-checking
1640 -- is enabled. Propagate tag to expression in declaration, which
1641 -- is original call.
1643 Call_Node := Expression (Parent (Entity (Actual)));
1645 -- Ada 2005: If this is a dereference of a call to a function with a
1646 -- dispatching access-result, the tag is propagated when the dereference
1647 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1649 elsif Nkind (Actual) = N_Explicit_Dereference
1650 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1654 -- Only other possibilities are parenthesized or qualified expression,
1655 -- or an expander-generated unchecked conversion of a function call to
1656 -- a stream Input attribute.
1659 Call_Node := Expression (Actual);
1662 -- Do not set the Controlling_Argument if already set. This happens
1663 -- in the special case of _Input (see Exp_Attr, case Input).
1665 if No (Controlling_Argument (Call_Node)) then
1666 Set_Controlling_Argument (Call_Node, Control);
1669 Arg := First_Actual (Call_Node);
1671 while Present (Arg) loop
1672 if Is_Tag_Indeterminate (Arg) then
1673 Propagate_Tag (Control, Arg);
1679 -- Expansion of dispatching calls is suppressed when VM_Target, because
1680 -- the VM back-ends directly handle the generation of dispatching
1681 -- calls and would have to undo any expansion to an indirect call.
1683 if VM_Target = No_VM then
1684 Expand_Dispatching_Call (Call_Node);
1686 -- Expansion of a dispatching call results in an indirect call, which in
1687 -- turn causes current values to be killed (see Resolve_Call), so on VM
1688 -- targets we do the call here to ensure consistent warnings between VM
1689 -- and non-VM targets.
1692 Kill_Current_Values;