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_Util; use Exp_Util;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Tss; use Exp_Tss;
34 with Errout; use Errout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
40 with Output; use Output;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch6; use Sem_Ch6;
46 with Sem_Elim; use Sem_Elim;
47 with Sem_Eval; use Sem_Eval;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Sinfo; use Sinfo;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
57 package body Sem_Disp is
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Add_Dispatching_Operation
64 (Tagged_Type : Entity_Id;
66 -- Add New_Op in the list of primitive operations of Tagged_Type
68 function Check_Controlling_Type
70 Subp : Entity_Id) return Entity_Id;
71 -- T is the tagged type of a formal parameter or the result of Subp.
72 -- If the subprogram has a controlling parameter or result that matches
73 -- the type, then returns the tagged type of that parameter or result
74 -- (returning the designated tagged type in the case of an access
75 -- parameter); otherwise returns empty.
77 -------------------------------
78 -- Add_Dispatching_Operation --
79 -------------------------------
81 procedure Add_Dispatching_Operation
82 (Tagged_Type : Entity_Id;
85 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
88 -- The dispatching operation may already be on the list, if it is the
89 -- wrapper for an inherited function of a null extension (see Exp_Ch3
90 -- for the construction of function wrappers). The list of primitive
91 -- operations must not contain duplicates.
93 Append_Unique_Elmt (New_Op, List);
94 end Add_Dispatching_Operation;
96 -------------------------------
97 -- Check_Controlling_Formals --
98 -------------------------------
100 procedure Check_Controlling_Formals
105 Ctrl_Type : Entity_Id;
108 Formal := First_Formal (Subp);
110 while Present (Formal) loop
111 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
113 if Present (Ctrl_Type) then
115 -- When the controlling type is concurrent and declared within a
116 -- generic or inside an instance, use its corresponding record
119 if Is_Concurrent_Type (Ctrl_Type)
120 and then Present (Corresponding_Record_Type (Ctrl_Type))
122 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
125 if Ctrl_Type = Typ then
126 Set_Is_Controlling_Formal (Formal);
128 -- Ada 2005 (AI-231): Anonymous access types used in
129 -- controlling parameters exclude null because it is necessary
130 -- to read the tag to dispatch, and null has no tag.
132 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
133 Set_Can_Never_Be_Null (Etype (Formal));
134 Set_Is_Known_Non_Null (Etype (Formal));
137 -- Check that the parameter's nominal subtype statically
138 -- matches the first subtype.
140 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
141 if not Subtypes_Statically_Match
142 (Typ, Designated_Type (Etype (Formal)))
145 ("parameter subtype does not match controlling type",
149 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
151 ("parameter subtype does not match controlling type",
155 if Present (Default_Value (Formal)) then
157 -- In Ada 2005, access parameters can have defaults
159 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
160 and then Ada_Version < Ada_05
163 ("default not allowed for controlling access parameter",
164 Default_Value (Formal));
166 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
168 ("default expression must be a tag indeterminate" &
169 " function call", Default_Value (Formal));
173 elsif Comes_From_Source (Subp) then
175 ("operation can be dispatching in only one type", Subp);
179 Next_Formal (Formal);
182 if Present (Etype (Subp)) then
183 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
185 if Present (Ctrl_Type) then
186 if Ctrl_Type = Typ then
187 Set_Has_Controlling_Result (Subp);
189 -- Check that result subtype statically matches first subtype
190 -- (Ada 2005): Subp may have a controlling access result.
192 if Subtypes_Statically_Match (Typ, Etype (Subp))
193 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
195 Subtypes_Statically_Match
196 (Typ, Designated_Type (Etype (Subp))))
202 ("result subtype does not match controlling type", Subp);
205 elsif Comes_From_Source (Subp) then
207 ("operation can be dispatching in only one type", Subp);
211 end Check_Controlling_Formals;
213 ----------------------------
214 -- Check_Controlling_Type --
215 ----------------------------
217 function Check_Controlling_Type
219 Subp : Entity_Id) return Entity_Id
221 Tagged_Type : Entity_Id := Empty;
224 if Is_Tagged_Type (T) then
225 if Is_First_Subtype (T) then
228 Tagged_Type := Base_Type (T);
231 elsif Ekind (T) = E_Anonymous_Access_Type
232 and then Is_Tagged_Type (Designated_Type (T))
234 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
235 if Is_First_Subtype (Designated_Type (T)) then
236 Tagged_Type := Designated_Type (T);
238 Tagged_Type := Base_Type (Designated_Type (T));
241 -- Ada 2005: an incomplete type can be tagged. An operation with an
242 -- access parameter of the type is dispatching.
244 elsif Scope (Designated_Type (T)) = Current_Scope then
245 Tagged_Type := Designated_Type (T);
247 -- Ada 2005 (AI-50217)
249 elsif From_With_Type (Designated_Type (T))
250 and then Present (Non_Limited_View (Designated_Type (T)))
252 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
253 Tagged_Type := Non_Limited_View (Designated_Type (T));
255 Tagged_Type := Base_Type (Non_Limited_View
256 (Designated_Type (T)));
261 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
264 -- The dispatching type and the primitive operation must be defined in
265 -- the same scope, except in the case of internal operations and formal
266 -- abstract subprograms.
268 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
269 and then (not Is_Generic_Type (Tagged_Type)
270 or else not Comes_From_Source (Subp)))
272 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
274 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
276 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
278 Is_Abstract_Subprogram (Subp))
285 end Check_Controlling_Type;
287 ----------------------------
288 -- Check_Dispatching_Call --
289 ----------------------------
291 procedure Check_Dispatching_Call (N : Node_Id) is
292 Loc : constant Source_Ptr := Sloc (N);
295 Control : Node_Id := Empty;
297 Subp_Entity : Entity_Id;
298 Indeterm_Ancestor_Call : Boolean := False;
299 Indeterm_Ctrl_Type : Entity_Id;
301 Static_Tag : Node_Id := Empty;
302 -- If a controlling formal has a statically tagged actual, the tag of
303 -- this actual is to be used for any tag-indeterminate actual.
305 procedure Check_Dispatching_Context;
306 -- If the call is tag-indeterminate and the entity being called is
307 -- abstract, verify that the context is a call that will eventually
308 -- provide a tag for dispatching, or has provided one already.
310 -------------------------------
311 -- Check_Dispatching_Context --
312 -------------------------------
314 procedure Check_Dispatching_Context is
315 Subp : constant Entity_Id := Entity (Name (N));
319 if Is_Abstract_Subprogram (Subp)
320 and then No (Controlling_Argument (N))
322 if Present (Alias (Subp))
323 and then not Is_Abstract_Subprogram (Alias (Subp))
324 and then No (DTC_Entity (Subp))
326 -- Private overriding of inherited abstract operation, call is
329 Set_Entity (Name (N), Alias (Subp));
335 while Present (Par) loop
337 if (Nkind (Par) = N_Function_Call or else
338 Nkind (Par) = N_Procedure_Call_Statement or else
339 Nkind (Par) = N_Assignment_Statement or else
340 Nkind (Par) = N_Op_Eq or else
341 Nkind (Par) = N_Op_Ne)
342 and then Is_Tagged_Type (Etype (Subp))
346 elsif Nkind (Par) = N_Qualified_Expression
347 or else Nkind (Par) = N_Unchecked_Type_Conversion
352 if Ekind (Subp) = E_Function then
354 ("call to abstract function must be dispatching", N);
356 -- This error can occur for a procedure in the case of a
357 -- call to an abstract formal procedure with a statically
362 ("call to abstract procedure must be dispatching",
371 end Check_Dispatching_Context;
373 -- Start of processing for Check_Dispatching_Call
376 -- Find a controlling argument, if any
378 if Present (Parameter_Associations (N)) then
379 Actual := First_Actual (N);
381 Subp_Entity := Entity (Name (N));
382 Formal := First_Formal (Subp_Entity);
384 while Present (Actual) loop
385 Control := Find_Controlling_Arg (Actual);
386 exit when Present (Control);
388 -- Check for the case where the actual is a tag-indeterminate call
389 -- whose result type is different than the tagged type associated
390 -- with the containing call, but is an ancestor of the type.
392 if Is_Controlling_Formal (Formal)
393 and then Is_Tag_Indeterminate (Actual)
394 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
395 and then Is_Ancestor (Etype (Actual), Etype (Formal))
397 Indeterm_Ancestor_Call := True;
398 Indeterm_Ctrl_Type := Etype (Formal);
400 -- If the formal is controlling but the actual is not, the type
401 -- of the actual is statically known, and may be used as the
402 -- controlling tag for some other tag-indeterminate actual.
404 elsif Is_Controlling_Formal (Formal)
405 and then Is_Entity_Name (Actual)
406 and then Is_Tagged_Type (Etype (Actual))
408 Static_Tag := Actual;
411 Next_Actual (Actual);
412 Next_Formal (Formal);
415 -- If the call doesn't have a controlling actual but does have an
416 -- indeterminate actual that requires dispatching treatment, then an
417 -- object is needed that will serve as the controlling argument for a
418 -- dispatching call on the indeterminate actual. This can only occur
419 -- in the unusual situation of a default actual given by a
420 -- tag-indeterminate call and where the type of the call is an
421 -- ancestor of the type associated with a containing call to an
422 -- inherited operation (see AI-239).
424 -- Rather than create an object of the tagged type, which would be
425 -- problematic for various reasons (default initialization,
426 -- discriminants), the tag of the containing call's associated tagged
427 -- type is directly used to control the dispatching.
430 and then Indeterm_Ancestor_Call
431 and then No (Static_Tag)
434 Make_Attribute_Reference (Loc,
435 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
436 Attribute_Name => Name_Tag);
441 if Present (Control) then
443 -- Verify that no controlling arguments are statically tagged
446 Write_Str ("Found Dispatching call");
451 Actual := First_Actual (N);
453 while Present (Actual) loop
454 if Actual /= Control then
456 if not Is_Controlling_Actual (Actual) then
457 null; -- Can be anything
459 elsif Is_Dynamically_Tagged (Actual) then
460 null; -- Valid parameter
462 elsif Is_Tag_Indeterminate (Actual) then
464 -- The tag is inherited from the enclosing call (the node
465 -- we are currently analyzing). Explicitly expand the
466 -- actual, since the previous call to Expand (from
467 -- Resolve_Call) had no way of knowing about the required
470 Propagate_Tag (Control, Actual);
474 ("controlling argument is not dynamically tagged",
480 Next_Actual (Actual);
483 -- Mark call as a dispatching call
485 Set_Controlling_Argument (N, Control);
486 Check_Restriction (No_Dispatching_Calls, N);
488 if Is_Eliminated (Ultimate_Alias (Subp_Entity)) then
489 Eliminate_Error_Msg (N, Ultimate_Alias (Subp_Entity));
492 -- If there is a statically tagged actual and a tag-indeterminate
493 -- call to a function of the ancestor (such as that provided by a
494 -- default), then treat this as a dispatching call and propagate
495 -- the tag to the tag-indeterminate call(s).
497 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
499 Make_Attribute_Reference (Loc,
501 New_Occurrence_Of (Etype (Static_Tag), Loc),
502 Attribute_Name => Name_Tag);
506 Actual := First_Actual (N);
507 Formal := First_Formal (Subp_Entity);
508 while Present (Actual) loop
509 if Is_Tag_Indeterminate (Actual)
510 and then Is_Controlling_Formal (Formal)
512 Propagate_Tag (Control, Actual);
515 Next_Actual (Actual);
516 Next_Formal (Formal);
519 Check_Dispatching_Context;
522 -- The call is not dispatching, so check that there aren't any
523 -- tag-indeterminate abstract calls left.
525 Actual := First_Actual (N);
526 while Present (Actual) loop
527 if Is_Tag_Indeterminate (Actual) then
529 -- Function call case
531 if Nkind (Original_Node (Actual)) = N_Function_Call then
532 Func := Entity (Name (Original_Node (Actual)));
534 -- If the actual is an attribute then it can't be abstract
535 -- (the only current case of a tag-indeterminate attribute
536 -- is the stream Input attribute).
539 Nkind (Original_Node (Actual)) = N_Attribute_Reference
543 -- Only other possibility is a qualified expression whose
544 -- constituent expression is itself a call.
550 (Expression (Original_Node (Actual)))));
553 if Present (Func) and then Is_Abstract_Subprogram (Func) then
555 "call to abstract function must be dispatching", N);
559 Next_Actual (Actual);
562 Check_Dispatching_Context;
566 -- If dispatching on result, the enclosing call, if any, will
567 -- determine the controlling argument. Otherwise this is the
568 -- primitive operation of the root type.
570 Check_Dispatching_Context;
572 end Check_Dispatching_Call;
574 ---------------------------------
575 -- Check_Dispatching_Operation --
576 ---------------------------------
578 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
579 Tagged_Type : Entity_Id;
580 Has_Dispatching_Parent : Boolean := False;
581 Body_Is_Last_Primitive : Boolean := False;
583 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
584 -- Check whether T is derived from a visibly controlled type.
585 -- This is true if the root type is declared in Ada.Finalization.
586 -- If T is derived instead from a private type whose full view
587 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
588 -- does not override the inherited one.
590 ---------------------------
591 -- Is_Visibly_Controlled --
592 ---------------------------
594 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
595 Root : constant Entity_Id := Root_Type (T);
597 return Chars (Scope (Root)) = Name_Finalization
598 and then Chars (Scope (Scope (Root))) = Name_Ada
599 and then Scope (Scope (Scope (Root))) = Standard_Standard;
600 end Is_Visibly_Controlled;
602 -- Start of processing for Check_Dispatching_Operation
605 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
609 Set_Is_Dispatching_Operation (Subp, False);
610 Tagged_Type := Find_Dispatching_Type (Subp);
614 if Ada_Version = Ada_05
615 and then Present (Tagged_Type)
616 and then Is_Concurrent_Type (Tagged_Type)
618 -- Protect the frontend against previously detected errors
620 if No (Corresponding_Record_Type (Tagged_Type)) then
624 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
627 -- (AI-345): The task body procedure is not a primitive of the tagged
630 if Present (Tagged_Type)
631 and then Is_Concurrent_Record_Type (Tagged_Type)
632 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
633 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
634 and then Subp = Get_Task_Body_Procedure
635 (Corresponding_Concurrent_Type (Tagged_Type))
640 -- If Subp is derived from a dispatching operation then it should
641 -- always be treated as dispatching. In this case various checks
642 -- below will be bypassed. Makes sure that late declarations for
643 -- inherited private subprograms are treated as dispatching, even
644 -- if the associated tagged type is already frozen.
646 Has_Dispatching_Parent :=
647 Present (Alias (Subp))
648 and then Is_Dispatching_Operation (Alias (Subp));
650 if No (Tagged_Type) then
652 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
653 -- with an abstract interface type unless the interface acts as a
654 -- parent type in a derivation. If the interface type is a formal
655 -- type then the operation is not primitive and therefore legal.
662 E := First_Entity (Subp);
663 while Present (E) loop
665 -- For an access parameter, check designated type.
667 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
668 Typ := Designated_Type (Etype (E));
673 if Comes_From_Source (Subp)
674 and then Is_Interface (Typ)
675 and then not Is_Class_Wide_Type (Typ)
676 and then not Is_Derived_Type (Typ)
677 and then not Is_Generic_Type (Typ)
678 and then not In_Instance
680 Error_Msg_N ("?declaration of& is too late!", Subp);
682 ("\spec should appear immediately after declaration of &!",
690 -- In case of functions check also the result type
692 if Ekind (Subp) = E_Function then
693 if Is_Access_Type (Etype (Subp)) then
694 Typ := Designated_Type (Etype (Subp));
699 if not Is_Class_Wide_Type (Typ)
700 and then Is_Interface (Typ)
701 and then not Is_Derived_Type (Typ)
703 Error_Msg_N ("?declaration of& is too late!", Subp);
705 ("\spec should appear immediately after declaration of &!",
713 -- The subprograms build internally after the freezing point (such as
714 -- the Init procedure) are not primitives
716 elsif Is_Frozen (Tagged_Type)
717 and then not Comes_From_Source (Subp)
718 and then not Has_Dispatching_Parent
722 -- The operation may be a child unit, whose scope is the defining
723 -- package, but which is not a primitive operation of the type.
725 elsif Is_Child_Unit (Subp) then
728 -- If the subprogram is not defined in a package spec, the only case
729 -- where it can be a dispatching op is when it overrides an operation
730 -- before the freezing point of the type.
732 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
733 or else In_Package_Body (Scope (Subp)))
734 and then not Has_Dispatching_Parent
736 if not Comes_From_Source (Subp)
737 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
741 -- If the type is already frozen, the overriding is not allowed
742 -- except when Old_Subp is not a dispatching operation (which
743 -- can occur when Old_Subp was inherited by an untagged type).
744 -- However, a body with no previous spec freezes the type "after"
745 -- its declaration, and therefore is a legal overriding (unless
746 -- the type has already been frozen). Only the first such body
749 elsif Present (Old_Subp)
750 and then Is_Dispatching_Operation (Old_Subp)
752 if Comes_From_Source (Subp)
754 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
755 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
758 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
759 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
762 -- ??? The checks here for whether the type has been
763 -- frozen prior to the new body are not complete. It's
764 -- not simple to check frozenness at this point since
765 -- the body has already caused the type to be prematurely
766 -- frozen in Analyze_Declarations, but we're forced to
767 -- recheck this here because of the odd rule interpretation
768 -- that allows the overriding if the type wasn't frozen
769 -- prior to the body. The freezing action should probably
770 -- be delayed until after the spec is seen, but that's
771 -- a tricky change to the delicate freezing code.
773 -- Look at each declaration following the type up until the
774 -- new subprogram body. If any of the declarations is a body
775 -- then the type has been frozen already so the overriding
776 -- primitive is illegal.
778 while Present (Decl_Item)
779 and then (Decl_Item /= Subp_Body)
781 if Comes_From_Source (Decl_Item)
782 and then (Nkind (Decl_Item) in N_Proper_Body
783 or else Nkind (Decl_Item) in N_Body_Stub)
785 Error_Msg_N ("overriding of& is too late!", Subp);
787 ("\spec should appear immediately after the type!",
795 -- If the subprogram doesn't follow in the list of
796 -- declarations including the type then the type has
797 -- definitely been frozen already and the body is illegal.
799 if No (Decl_Item) then
800 Error_Msg_N ("overriding of& is too late!", Subp);
802 ("\spec should appear immediately after the type!",
805 elsif Is_Frozen (Subp) then
807 -- The subprogram body declares a primitive operation.
808 -- if the subprogram is already frozen, we must update
809 -- its dispatching information explicitly here. The
810 -- information is taken from the overridden subprogram.
811 -- We must also generate a cross-reference entry because
812 -- references to other primitives were already created
813 -- when type was frozen.
815 Body_Is_Last_Primitive := True;
817 if Present (DTC_Entity (Old_Subp)) then
818 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
819 Set_DT_Position (Subp, DT_Position (Old_Subp));
821 if not Restriction_Active (No_Dispatching_Calls) then
822 if Building_Static_DT (Tagged_Type) then
824 -- If the static dispatch table has not been
825 -- built then there is nothing else to do now;
826 -- otherwise we notify that we cannot build the
827 -- static dispatch table.
829 if Has_Dispatch_Table (Tagged_Type) then
831 ("overriding of& is too late for building" &
832 " static dispatch tables!", Subp);
834 ("\spec should appear immediately after" &
839 Insert_Actions_After (Subp_Body,
840 Register_Primitive (Sloc (Subp_Body),
844 Generate_Reference (Tagged_Type, Subp, 'p', False);
851 Error_Msg_N ("overriding of& is too late!", Subp);
853 ("\subprogram spec should appear immediately after the type!",
857 -- If the type is not frozen yet and we are not in the overriding
858 -- case it looks suspiciously like an attempt to define a primitive
859 -- operation, which requires the declaration to be in a package spec
862 elsif not Is_Frozen (Tagged_Type) then
864 ("?not dispatching (must be defined in a package spec)", Subp);
867 -- When the type is frozen, it is legitimate to define a new
868 -- non-primitive operation.
874 -- Now, we are sure that the scope is a package spec. If the subprogram
875 -- is declared after the freezing point of the type that's an error
877 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
878 Error_Msg_N ("this primitive operation is declared too late", Subp);
880 ("?no primitive operations for& after this line",
881 Freeze_Node (Tagged_Type),
886 Check_Controlling_Formals (Tagged_Type, Subp);
888 -- Now it should be a correct primitive operation, put it in the list
890 if Present (Old_Subp) then
892 -- If the type has interfaces we complete this check after we set
893 -- attribute Is_Dispatching_Operation.
895 Check_Subtype_Conformant (Subp, Old_Subp);
897 if (Chars (Subp) = Name_Initialize
898 or else Chars (Subp) = Name_Adjust
899 or else Chars (Subp) = Name_Finalize)
900 and then Is_Controlled (Tagged_Type)
901 and then not Is_Visibly_Controlled (Tagged_Type)
903 Set_Is_Overriding_Operation (Subp, False);
905 ("operation does not override inherited&?", Subp, Subp);
907 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
908 Set_Is_Overriding_Operation (Subp);
910 -- Ada 2005 (AI-251): In case of late overriding of a primitive
911 -- that covers abstract interface subprograms we must register it
912 -- in all the secondary dispatch tables associated with abstract
913 -- interfaces. We do this now only if not building static tables.
914 -- Otherwise the patch code is emitted after those tables are
915 -- built, to prevent access_before_elaboration in gigi.
917 if Body_Is_Last_Primitive then
919 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
924 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
925 while Present (Elmt) loop
928 if Present (Alias (Prim))
929 and then Present (Interface_Alias (Prim))
930 and then Alias (Prim) = Subp
931 and then not Building_Static_DT (Tagged_Type)
933 Insert_Actions_After (Subp_Body,
934 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
940 -- Redisplay the contents of the updated dispatch table
942 if Debug_Flag_ZZ then
943 Write_Str ("Late overriding: ");
944 Write_DT (Tagged_Type);
950 -- If no old subprogram, then we add this as a dispatching operation,
951 -- but we avoid doing this if an error was posted, to prevent annoying
954 elsif not Error_Posted (Subp) then
955 Add_Dispatching_Operation (Tagged_Type, Subp);
958 Set_Is_Dispatching_Operation (Subp, True);
960 -- Ada 2005 (AI-251): If the type implements interfaces we must check
961 -- subtype conformance against all the interfaces covered by this
964 if Present (Old_Subp)
965 and then Has_Interfaces (Tagged_Type)
968 Ifaces_List : Elist_Id;
969 Iface_Elmt : Elmt_Id;
970 Iface_Prim_Elmt : Elmt_Id;
971 Iface_Prim : Entity_Id;
975 Collect_Interfaces (Tagged_Type, Ifaces_List);
977 Iface_Elmt := First_Elmt (Ifaces_List);
978 while Present (Iface_Elmt) loop
979 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
981 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
982 while Present (Iface_Prim_Elmt) loop
983 Iface_Prim := Node (Iface_Prim_Elmt);
985 if Is_Interface_Conformant
986 (Tagged_Type, Iface_Prim, Subp)
988 -- Handle procedures, functions whose return type
989 -- matches, or functions not returning interfaces
991 if Ekind (Subp) = E_Procedure
992 or else Etype (Iface_Prim) = Etype (Subp)
993 or else not Is_Interface (Etype (Iface_Prim))
995 Check_Subtype_Conformant
997 Old_Id => Iface_Prim,
999 Skip_Controlling_Formals => True);
1001 -- Handle functions returning interfaces
1003 elsif Implements_Interface
1004 (Etype (Subp), Etype (Iface_Prim))
1006 -- Temporarily force both entities to return the
1007 -- same type. Required because Subtype_Conformant
1008 -- does not handle this case.
1010 Ret_Typ := Etype (Iface_Prim);
1011 Set_Etype (Iface_Prim, Etype (Subp));
1013 Check_Subtype_Conformant
1015 Old_Id => Iface_Prim,
1017 Skip_Controlling_Formals => True);
1019 Set_Etype (Iface_Prim, Ret_Typ);
1023 Next_Elmt (Iface_Prim_Elmt);
1027 Next_Elmt (Iface_Elmt);
1032 if not Body_Is_Last_Primitive then
1033 Set_DT_Position (Subp, No_Uint);
1035 elsif Has_Controlled_Component (Tagged_Type)
1037 (Chars (Subp) = Name_Initialize
1038 or else Chars (Subp) = Name_Adjust
1039 or else Chars (Subp) = Name_Finalize)
1042 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1046 Old_Spec : Entity_Id;
1048 C_Names : constant array (1 .. 3) of Name_Id :=
1053 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1054 (TSS_Deep_Initialize,
1059 -- Remove previous controlled function, which was constructed
1060 -- and analyzed when the type was frozen. This requires
1061 -- removing the body of the redefined primitive, as well as
1062 -- its specification if needed (there is no spec created for
1063 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
1064 -- the exception information that may have been generated for
1065 -- it when front end zero-cost tables are enabled.
1067 for J in D_Names'Range loop
1068 Old_P := TSS (Tagged_Type, D_Names (J));
1071 and then Chars (Subp) = C_Names (J)
1073 Old_Bod := Unit_Declaration_Node (Old_P);
1075 Set_Is_Eliminated (Old_P);
1076 Set_Scope (Old_P, Scope (Current_Scope));
1078 if Nkind (Old_Bod) = N_Subprogram_Body
1079 and then Present (Corresponding_Spec (Old_Bod))
1081 Old_Spec := Corresponding_Spec (Old_Bod);
1082 Set_Has_Completion (Old_Spec, False);
1087 Build_Late_Proc (Tagged_Type, Chars (Subp));
1089 -- The new operation is added to the actions of the freeze
1090 -- node for the type, but this node has already been analyzed,
1091 -- so we must retrieve and analyze explicitly the new body.
1094 and then Present (Actions (F_Node))
1096 Decl := Last (Actions (F_Node));
1101 end Check_Dispatching_Operation;
1103 ------------------------------------------
1104 -- Check_Operation_From_Incomplete_Type --
1105 ------------------------------------------
1107 procedure Check_Operation_From_Incomplete_Type
1111 Full : constant Entity_Id := Full_View (Typ);
1112 Parent_Typ : constant Entity_Id := Etype (Full);
1113 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1114 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1116 Prev : Elmt_Id := No_Elmt;
1118 function Derives_From (Proc : Entity_Id) return Boolean;
1119 -- Check that Subp has the signature of an operation derived from Proc.
1120 -- Subp has an access parameter that designates Typ.
1126 function Derives_From (Proc : Entity_Id) return Boolean is
1130 if Chars (Proc) /= Chars (Subp) then
1134 F1 := First_Formal (Proc);
1135 F2 := First_Formal (Subp);
1137 while Present (F1) and then Present (F2) loop
1139 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1141 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1144 elsif Designated_Type (Etype (F1)) = Parent_Typ
1145 and then Designated_Type (Etype (F2)) /= Full
1150 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1153 elsif Etype (F1) /= Etype (F2) then
1161 return No (F1) and then No (F2);
1164 -- Start of processing for Check_Operation_From_Incomplete_Type
1167 -- The operation may override an inherited one, or may be a new one
1168 -- altogether. The inherited operation will have been hidden by the
1169 -- current one at the point of the type derivation, so it does not
1170 -- appear in the list of primitive operations of the type. We have to
1171 -- find the proper place of insertion in the list of primitive opera-
1172 -- tions by iterating over the list for the parent type.
1174 Op1 := First_Elmt (Old_Prim);
1175 Op2 := First_Elmt (New_Prim);
1177 while Present (Op1) and then Present (Op2) loop
1179 if Derives_From (Node (Op1)) then
1183 -- Avoid adding it to the list of primitives if already there!
1185 if Node (Op2) /= Subp then
1186 Prepend_Elmt (Subp, New_Prim);
1190 Insert_Elmt_After (Subp, Prev);
1201 -- Operation is a new primitive
1203 Append_Elmt (Subp, New_Prim);
1204 end Check_Operation_From_Incomplete_Type;
1206 ---------------------------------------
1207 -- Check_Operation_From_Private_View --
1208 ---------------------------------------
1210 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1211 Tagged_Type : Entity_Id;
1214 if Is_Dispatching_Operation (Alias (Subp)) then
1215 Set_Scope (Subp, Current_Scope);
1216 Tagged_Type := Find_Dispatching_Type (Subp);
1218 -- Add Old_Subp to primitive operations if not already present.
1220 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1221 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1223 -- If Old_Subp isn't already marked as dispatching then
1224 -- this is the case of an operation of an untagged private
1225 -- type fulfilled by a tagged type that overrides an
1226 -- inherited dispatching operation, so we set the necessary
1227 -- dispatching attributes here.
1229 if not Is_Dispatching_Operation (Old_Subp) then
1231 -- If the untagged type has no discriminants, and the full
1232 -- view is constrained, there will be a spurious mismatch
1233 -- of subtypes on the controlling arguments, because the tagged
1234 -- type is the internal base type introduced in the derivation.
1235 -- Use the original type to verify conformance, rather than the
1238 if not Comes_From_Source (Tagged_Type)
1239 and then Has_Discriminants (Tagged_Type)
1244 Formal := First_Formal (Old_Subp);
1245 while Present (Formal) loop
1246 if Tagged_Type = Base_Type (Etype (Formal)) then
1247 Tagged_Type := Etype (Formal);
1250 Next_Formal (Formal);
1254 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1255 Tagged_Type := Etype (Old_Subp);
1259 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1260 Set_Is_Dispatching_Operation (Old_Subp, True);
1261 Set_DT_Position (Old_Subp, No_Uint);
1264 -- If the old subprogram is an explicit renaming of some other
1265 -- entity, it is not overridden by the inherited subprogram.
1266 -- Otherwise, update its alias and other attributes.
1268 if Present (Alias (Old_Subp))
1269 and then Nkind (Unit_Declaration_Node (Old_Subp))
1270 /= N_Subprogram_Renaming_Declaration
1272 Set_Alias (Old_Subp, Alias (Subp));
1274 -- The derived subprogram should inherit the abstractness
1275 -- of the parent subprogram (except in the case of a function
1276 -- returning the type). This sets the abstractness properly
1277 -- for cases where a private extension may have inherited
1278 -- an abstract operation, but the full type is derived from
1279 -- a descendant type and inherits a nonabstract version.
1281 if Etype (Subp) /= Tagged_Type then
1282 Set_Is_Abstract_Subprogram
1283 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1288 end Check_Operation_From_Private_View;
1290 --------------------------
1291 -- Find_Controlling_Arg --
1292 --------------------------
1294 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1295 Orig_Node : constant Node_Id := Original_Node (N);
1299 if Nkind (Orig_Node) = N_Qualified_Expression then
1300 return Find_Controlling_Arg (Expression (Orig_Node));
1303 -- Dispatching on result case. If expansion is disabled, the node still
1304 -- has the structure of a function call. However, if the function name
1305 -- is an operator and the call was given in infix form, the original
1306 -- node has no controlling result and we must examine the current node.
1308 if Nkind (N) = N_Function_Call
1309 and then Present (Controlling_Argument (N))
1310 and then Has_Controlling_Result (Entity (Name (N)))
1312 return Controlling_Argument (N);
1314 -- If expansion is enabled, the call may have been transformed into
1315 -- an indirect call, and we need to recover the original node.
1317 elsif Nkind (Orig_Node) = N_Function_Call
1318 and then Present (Controlling_Argument (Orig_Node))
1319 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1321 return Controlling_Argument (Orig_Node);
1325 elsif Is_Controlling_Actual (N)
1327 (Nkind (Parent (N)) = N_Qualified_Expression
1328 and then Is_Controlling_Actual (Parent (N)))
1332 if Is_Access_Type (Typ) then
1333 -- In the case of an Access attribute, use the type of
1334 -- the prefix, since in the case of an actual for an
1335 -- access parameter, the attribute's type may be of a
1336 -- specific designated type, even though the prefix
1337 -- type is class-wide.
1339 if Nkind (N) = N_Attribute_Reference then
1340 Typ := Etype (Prefix (N));
1342 -- An allocator is dispatching if the type of qualified
1343 -- expression is class_wide, in which case this is the
1344 -- controlling type.
1346 elsif Nkind (Orig_Node) = N_Allocator
1347 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1349 Typ := Etype (Expression (Orig_Node));
1352 Typ := Designated_Type (Typ);
1356 if Is_Class_Wide_Type (Typ)
1358 (Nkind (Parent (N)) = N_Qualified_Expression
1359 and then Is_Access_Type (Etype (N))
1360 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1367 end Find_Controlling_Arg;
1369 ---------------------------
1370 -- Find_Dispatching_Type --
1371 ---------------------------
1373 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1375 Ctrl_Type : Entity_Id;
1378 if Present (DTC_Entity (Subp)) then
1379 return Scope (DTC_Entity (Subp));
1382 Formal := First_Formal (Subp);
1383 while Present (Formal) loop
1384 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1386 if Present (Ctrl_Type) then
1390 Next_Formal (Formal);
1393 -- The subprogram may also be dispatching on result
1395 if Present (Etype (Subp)) then
1396 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1398 if Present (Ctrl_Type) then
1405 end Find_Dispatching_Type;
1407 ---------------------------------------
1408 -- Find_Primitive_Covering_Interface --
1409 ---------------------------------------
1411 function Find_Primitive_Covering_Interface
1412 (Tagged_Type : Entity_Id;
1413 Iface_Prim : Entity_Id) return Entity_Id
1418 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1419 or else (Present (Alias (Iface_Prim))
1422 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1424 E := Current_Entity (Iface_Prim);
1425 while Present (E) loop
1426 if Is_Subprogram (E)
1427 and then Is_Dispatching_Operation (E)
1428 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1437 end Find_Primitive_Covering_Interface;
1439 ---------------------------
1440 -- Is_Dynamically_Tagged --
1441 ---------------------------
1443 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1445 if Nkind (N) = N_Error then
1448 return Find_Controlling_Arg (N) /= Empty;
1450 end Is_Dynamically_Tagged;
1452 --------------------------
1453 -- Is_Tag_Indeterminate --
1454 --------------------------
1456 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1459 Orig_Node : constant Node_Id := Original_Node (N);
1462 if Nkind (Orig_Node) = N_Function_Call
1463 and then Is_Entity_Name (Name (Orig_Node))
1465 Nam := Entity (Name (Orig_Node));
1467 if not Has_Controlling_Result (Nam) then
1470 -- An explicit dereference means that the call has already been
1471 -- expanded and there is no tag to propagate.
1473 elsif Nkind (N) = N_Explicit_Dereference then
1476 -- If there are no actuals, the call is tag-indeterminate
1478 elsif No (Parameter_Associations (Orig_Node)) then
1482 Actual := First_Actual (Orig_Node);
1483 while Present (Actual) loop
1484 if Is_Controlling_Actual (Actual)
1485 and then not Is_Tag_Indeterminate (Actual)
1487 return False; -- one operand is dispatching
1490 Next_Actual (Actual);
1496 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1497 return Is_Tag_Indeterminate (Expression (Orig_Node));
1499 -- Case of a call to the Input attribute (possibly rewritten), which is
1500 -- always tag-indeterminate except when its prefix is a Class attribute.
1502 elsif Nkind (Orig_Node) = N_Attribute_Reference
1504 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1506 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1510 -- In Ada 2005 a function that returns an anonymous access type can
1511 -- dispatching, and the dereference of a call to such a function
1512 -- is also tag-indeterminate.
1514 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1515 and then Ada_Version >= Ada_05
1517 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1522 end Is_Tag_Indeterminate;
1524 ------------------------------------
1525 -- Override_Dispatching_Operation --
1526 ------------------------------------
1528 procedure Override_Dispatching_Operation
1529 (Tagged_Type : Entity_Id;
1530 Prev_Op : Entity_Id;
1537 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1538 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1540 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1541 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1542 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1545 -- If there is no previous operation to override, the type declaration
1546 -- was malformed, and an error must have been emitted already.
1548 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1549 while Present (Elmt)
1550 and then Node (Elmt) /= Prev_Op
1559 Replace_Elmt (Elmt, New_Op);
1561 if Ada_Version >= Ada_05
1562 and then Has_Interfaces (Tagged_Type)
1564 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1565 -- entities of the overridden primitive to reference New_Op, and also
1566 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1567 -- that the new operation is subtype conformant with the interface
1568 -- operations that it implements (for operations inherited from the
1569 -- parent itself, this check is made when building the derived type).
1571 -- Note: This code is only executed in case of late overriding
1573 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1574 while Present (Elmt) loop
1575 Prim := Node (Elmt);
1577 if Prim = New_Op then
1580 -- Note: The check on Is_Subprogram protects the frontend against
1581 -- reading attributes in entities that are not yet fully decorated
1583 elsif Is_Subprogram (Prim)
1584 and then Present (Interface_Alias (Prim))
1585 and then Alias (Prim) = Prev_Op
1586 and then Present (Etype (New_Op))
1588 Set_Alias (Prim, New_Op);
1589 Check_Subtype_Conformant (New_Op, Prim);
1590 Set_Is_Abstract_Subprogram (Prim,
1591 Is_Abstract_Subprogram (New_Op));
1593 -- Ensure that this entity will be expanded to fill the
1594 -- corresponding entry in its dispatch table.
1596 if not Is_Abstract_Subprogram (Prim) then
1597 Set_Has_Delayed_Freeze (Prim);
1605 if (not Is_Package_Or_Generic_Package (Current_Scope))
1606 or else not In_Private_Part (Current_Scope)
1608 -- Not a private primitive
1612 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1614 -- Make the overriding operation into an alias of the implicit one.
1615 -- In this fashion a call from outside ends up calling the new body
1616 -- even if non-dispatching, and a call from inside calls the
1617 -- overriding operation because it hides the implicit one. To
1618 -- indicate that the body of Prev_Op is never called, set its
1619 -- dispatch table entity to Empty.
1621 Set_Alias (Prev_Op, New_Op);
1622 Set_DTC_Entity (Prev_Op, Empty);
1625 end Override_Dispatching_Operation;
1631 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1632 Call_Node : Node_Id;
1636 if Nkind (Actual) = N_Function_Call then
1637 Call_Node := Actual;
1639 elsif Nkind (Actual) = N_Identifier
1640 and then Nkind (Original_Node (Actual)) = N_Function_Call
1642 -- Call rewritten as object declaration when stack-checking
1643 -- is enabled. Propagate tag to expression in declaration, which
1644 -- is original call.
1646 Call_Node := Expression (Parent (Entity (Actual)));
1648 -- Ada 2005: If this is a dereference of a call to a function with a
1649 -- dispatching access-result, the tag is propagated when the dereference
1650 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1652 elsif Nkind (Actual) = N_Explicit_Dereference
1653 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1657 -- Only other possibilities are parenthesized or qualified expression,
1658 -- or an expander-generated unchecked conversion of a function call to
1659 -- a stream Input attribute.
1662 Call_Node := Expression (Actual);
1665 -- Do not set the Controlling_Argument if already set. This happens
1666 -- in the special case of _Input (see Exp_Attr, case Input).
1668 if No (Controlling_Argument (Call_Node)) then
1669 Set_Controlling_Argument (Call_Node, Control);
1672 Arg := First_Actual (Call_Node);
1674 while Present (Arg) loop
1675 if Is_Tag_Indeterminate (Arg) then
1676 Propagate_Tag (Control, Arg);
1682 -- Expansion of dispatching calls is suppressed when VM_Target, because
1683 -- the VM back-ends directly handle the generation of dispatching
1684 -- calls and would have to undo any expansion to an indirect call.
1686 if VM_Target = No_VM then
1687 Expand_Dispatching_Call (Call_Node);
1689 -- Expansion of a dispatching call results in an indirect call, which in
1690 -- turn causes current values to be killed (see Resolve_Call), so on VM
1691 -- targets we do the call here to ensure consistent warnings between VM
1692 -- and non-VM targets.
1695 Kill_Current_Values;