1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Output; use Output;
39 with Restrict; use Restrict;
40 with Rident; use Rident;
42 with Sem_Ch6; use Sem_Ch6;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Type; use Sem_Type;
45 with Sem_Util; use Sem_Util;
46 with Snames; use Snames;
47 with Stand; use Stand;
48 with Sinfo; use Sinfo;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Uintp; use Uintp;
53 package body Sem_Disp is
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 procedure Add_Dispatching_Operation
60 (Tagged_Type : Entity_Id;
62 -- Add New_Op in the list of primitive operations of Tagged_Type
64 function Check_Controlling_Type
66 Subp : Entity_Id) return Entity_Id;
67 -- T is the tagged type of a formal parameter or the result of Subp.
68 -- If the subprogram has a controlling parameter or result that matches
69 -- the type, then returns the tagged type of that parameter or result
70 -- (returning the designated tagged type in the case of an access
71 -- parameter); otherwise returns empty.
73 -------------------------------
74 -- Add_Dispatching_Operation --
75 -------------------------------
77 procedure Add_Dispatching_Operation
78 (Tagged_Type : Entity_Id;
81 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
83 Append_Elmt (New_Op, List);
84 end Add_Dispatching_Operation;
86 -------------------------------
87 -- Check_Controlling_Formals --
88 -------------------------------
90 procedure Check_Controlling_Formals
95 Ctrl_Type : Entity_Id;
98 Formal := First_Formal (Subp);
100 while Present (Formal) loop
101 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
103 if Present (Ctrl_Type) then
105 -- When the controlling type is concurrent and declared within a
106 -- generic or inside an instance, use its corresponding record
109 if Is_Concurrent_Type (Ctrl_Type)
110 and then Present (Corresponding_Record_Type (Ctrl_Type))
112 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
115 if Ctrl_Type = Typ then
116 Set_Is_Controlling_Formal (Formal);
118 -- Ada 2005 (AI-231): Anonymous access types used in
119 -- controlling parameters exclude null because it is necessary
120 -- to read the tag to dispatch, and null has no tag.
122 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
123 Set_Can_Never_Be_Null (Etype (Formal));
124 Set_Is_Known_Non_Null (Etype (Formal));
127 -- Check that the parameter's nominal subtype statically
128 -- matches the first subtype.
130 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
131 if not Subtypes_Statically_Match
132 (Typ, Designated_Type (Etype (Formal)))
135 ("parameter subtype does not match controlling type",
139 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
141 ("parameter subtype does not match controlling type",
145 if Present (Default_Value (Formal)) then
146 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
148 ("default not allowed for controlling access parameter",
149 Default_Value (Formal));
151 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
153 ("default expression must be a tag indeterminate" &
154 " function call", Default_Value (Formal));
158 elsif Comes_From_Source (Subp) then
160 ("operation can be dispatching in only one type", Subp);
164 Next_Formal (Formal);
167 if Present (Etype (Subp)) then
168 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
170 if Present (Ctrl_Type) then
171 if Ctrl_Type = Typ then
172 Set_Has_Controlling_Result (Subp);
174 -- Check that result subtype statically matches first subtype
175 -- (Ada 2005) : Subp may have a controlling access result.
177 if Subtypes_Statically_Match (Typ, Etype (Subp))
178 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
180 Subtypes_Statically_Match
181 (Typ, Designated_Type (Etype (Subp))))
187 ("result subtype does not match controlling type", Subp);
190 elsif Comes_From_Source (Subp) then
192 ("operation can be dispatching in only one type", Subp);
196 end Check_Controlling_Formals;
198 ----------------------------
199 -- Check_Controlling_Type --
200 ----------------------------
202 function Check_Controlling_Type
204 Subp : Entity_Id) return Entity_Id
206 Tagged_Type : Entity_Id := Empty;
209 if Is_Tagged_Type (T) then
210 if Is_First_Subtype (T) then
213 Tagged_Type := Base_Type (T);
216 elsif Ekind (T) = E_Anonymous_Access_Type
217 and then Is_Tagged_Type (Designated_Type (T))
219 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
220 if Is_First_Subtype (Designated_Type (T)) then
221 Tagged_Type := Designated_Type (T);
223 Tagged_Type := Base_Type (Designated_Type (T));
226 -- Ada 2005 : an incomplete type can be tagged. An operation with
227 -- an access parameter of the type is dispatching.
229 elsif Scope (Designated_Type (T)) = Current_Scope then
230 Tagged_Type := Designated_Type (T);
232 -- Ada 2005 (AI-50217)
234 elsif From_With_Type (Designated_Type (T))
235 and then Present (Non_Limited_View (Designated_Type (T)))
237 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
238 Tagged_Type := Non_Limited_View (Designated_Type (T));
240 Tagged_Type := Base_Type (Non_Limited_View
241 (Designated_Type (T)));
247 or else Is_Class_Wide_Type (Tagged_Type)
251 -- The dispatching type and the primitive operation must be defined
252 -- in the same scope, except in the case of internal operations and
253 -- formal abstract subprograms.
255 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
256 and then (not Is_Generic_Type (Tagged_Type)
257 or else not Comes_From_Source (Subp)))
259 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
261 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
263 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
265 Is_Abstract_Subprogram (Subp))
272 end Check_Controlling_Type;
274 ----------------------------
275 -- Check_Dispatching_Call --
276 ----------------------------
278 procedure Check_Dispatching_Call (N : Node_Id) is
279 Loc : constant Source_Ptr := Sloc (N);
282 Control : Node_Id := Empty;
284 Subp_Entity : Entity_Id;
285 Indeterm_Ancestor_Call : Boolean := False;
286 Indeterm_Ctrl_Type : Entity_Id;
288 procedure Check_Dispatching_Context;
289 -- If the call is tag-indeterminate and the entity being called is
290 -- abstract, verify that the context is a call that will eventually
291 -- provide a tag for dispatching, or has provided one already.
293 -------------------------------
294 -- Check_Dispatching_Context --
295 -------------------------------
297 procedure Check_Dispatching_Context is
298 Subp : constant Entity_Id := Entity (Name (N));
302 if Is_Abstract_Subprogram (Subp)
303 and then No (Controlling_Argument (N))
305 if Present (Alias (Subp))
306 and then not Is_Abstract_Subprogram (Alias (Subp))
307 and then No (DTC_Entity (Subp))
309 -- Private overriding of inherited abstract operation,
312 Set_Entity (Name (N), Alias (Subp));
318 while Present (Par) loop
320 if (Nkind (Par) = N_Function_Call or else
321 Nkind (Par) = N_Procedure_Call_Statement or else
322 Nkind (Par) = N_Assignment_Statement or else
323 Nkind (Par) = N_Op_Eq or else
324 Nkind (Par) = N_Op_Ne)
325 and then Is_Tagged_Type (Etype (Subp))
329 elsif Nkind (Par) = N_Qualified_Expression
330 or else Nkind (Par) = N_Unchecked_Type_Conversion
335 if Ekind (Subp) = E_Function then
337 ("call to abstract function must be dispatching", N);
339 -- This error can occur for a procedure in the case of a
340 -- call to an abstract formal procedure with a statically
345 ("call to abstract procedure must be dispatching",
354 end Check_Dispatching_Context;
356 -- Start of processing for Check_Dispatching_Call
359 -- Find a controlling argument, if any
361 if Present (Parameter_Associations (N)) then
362 Actual := First_Actual (N);
364 Subp_Entity := Entity (Name (N));
365 Formal := First_Formal (Subp_Entity);
367 while Present (Actual) loop
368 Control := Find_Controlling_Arg (Actual);
369 exit when Present (Control);
371 -- Check for the case where the actual is a tag-indeterminate call
372 -- whose result type is different than the tagged type associated
373 -- with the containing call, but is an ancestor of the type.
375 if Is_Controlling_Formal (Formal)
376 and then Is_Tag_Indeterminate (Actual)
377 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
378 and then Is_Ancestor (Etype (Actual), Etype (Formal))
380 Indeterm_Ancestor_Call := True;
381 Indeterm_Ctrl_Type := Etype (Formal);
384 Next_Actual (Actual);
385 Next_Formal (Formal);
388 -- If the call doesn't have a controlling actual but does have
389 -- an indeterminate actual that requires dispatching treatment,
390 -- then an object is needed that will serve as the controlling
391 -- argument for a dispatching call on the indeterminate actual.
392 -- This can only occur in the unusual situation of a default
393 -- actual given by a tag-indeterminate call and where the type
394 -- of the call is an ancestor of the type associated with a
395 -- containing call to an inherited operation (see AI-239).
396 -- Rather than create an object of the tagged type, which would
397 -- be problematic for various reasons (default initialization,
398 -- discriminants), the tag of the containing call's associated
399 -- tagged type is directly used to control the dispatching.
402 and then Indeterm_Ancestor_Call
405 Make_Attribute_Reference (Loc,
406 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
407 Attribute_Name => Name_Tag);
411 if Present (Control) then
413 -- Verify that no controlling arguments are statically tagged
416 Write_Str ("Found Dispatching call");
421 Actual := First_Actual (N);
423 while Present (Actual) loop
424 if Actual /= Control then
426 if not Is_Controlling_Actual (Actual) then
427 null; -- Can be anything
429 elsif Is_Dynamically_Tagged (Actual) then
430 null; -- Valid parameter
432 elsif Is_Tag_Indeterminate (Actual) then
434 -- The tag is inherited from the enclosing call (the
435 -- node we are currently analyzing). Explicitly expand
436 -- the actual, since the previous call to Expand
437 -- (from Resolve_Call) had no way of knowing about
438 -- the required dispatching.
440 Propagate_Tag (Control, Actual);
444 ("controlling argument is not dynamically tagged",
450 Next_Actual (Actual);
453 -- Mark call as a dispatching call
455 Set_Controlling_Argument (N, Control);
456 Check_Restriction (No_Dispatching_Calls, N);
459 -- The call is not dispatching, so check that there aren't any
460 -- tag-indeterminate abstract calls left.
462 Actual := First_Actual (N);
464 while Present (Actual) loop
465 if Is_Tag_Indeterminate (Actual) then
467 -- Function call case
469 if Nkind (Original_Node (Actual)) = N_Function_Call then
470 Func := Entity (Name (Original_Node (Actual)));
472 -- If the actual is an attribute then it can't be abstract
473 -- (the only current case of a tag-indeterminate attribute
474 -- is the stream Input attribute).
477 Nkind (Original_Node (Actual)) = N_Attribute_Reference
481 -- Only other possibility is a qualified expression whose
482 -- constituent expression is itself a call.
488 (Expression (Original_Node (Actual)))));
491 if Present (Func) and then Is_Abstract_Subprogram (Func) then
493 "call to abstract function must be dispatching", N);
497 Next_Actual (Actual);
500 Check_Dispatching_Context;
504 -- If dispatching on result, the enclosing call, if any, will
505 -- determine the controlling argument. Otherwise this is the
506 -- primitive operation of the root type.
508 Check_Dispatching_Context;
510 end Check_Dispatching_Call;
512 ---------------------------------
513 -- Check_Dispatching_Operation --
514 ---------------------------------
516 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
517 Tagged_Type : Entity_Id;
518 Has_Dispatching_Parent : Boolean := False;
519 Body_Is_Last_Primitive : Boolean := False;
521 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
522 -- Check whether T is derived from a visibly controlled type.
523 -- This is true if the root type is declared in Ada.Finalization.
524 -- If T is derived instead from a private type whose full view
525 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
526 -- does not override the inherited one.
528 ---------------------------
529 -- Is_Visibly_Controlled --
530 ---------------------------
532 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
533 Root : constant Entity_Id := Root_Type (T);
535 return Chars (Scope (Root)) = Name_Finalization
536 and then Chars (Scope (Scope (Root))) = Name_Ada
537 and then Scope (Scope (Scope (Root))) = Standard_Standard;
538 end Is_Visibly_Controlled;
540 -- Start of processing for Check_Dispatching_Operation
543 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
547 Set_Is_Dispatching_Operation (Subp, False);
548 Tagged_Type := Find_Dispatching_Type (Subp);
552 if Ada_Version = Ada_05
553 and then Present (Tagged_Type)
554 and then Is_Concurrent_Type (Tagged_Type)
556 -- Protect the frontend against previously detected errors
558 if No (Corresponding_Record_Type (Tagged_Type)) then
562 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
565 -- If Subp is derived from a dispatching operation then it should
566 -- always be treated as dispatching. In this case various checks
567 -- below will be bypassed. Makes sure that late declarations for
568 -- inherited private subprograms are treated as dispatching, even
569 -- if the associated tagged type is already frozen.
571 Has_Dispatching_Parent :=
572 Present (Alias (Subp))
573 and then Is_Dispatching_Operation (Alias (Subp));
575 if No (Tagged_Type) then
577 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
578 -- with an abstract interface type unless the interface acts as a
579 -- parent type in a derivation. If the interface type is a formal
580 -- type then the operation is not primitive and therefore legal.
587 E := First_Entity (Subp);
588 while Present (E) loop
589 if Is_Access_Type (Etype (E)) then
590 Typ := Designated_Type (Etype (E));
595 if Comes_From_Source (Subp)
596 and then Is_Interface (Typ)
597 and then not Is_Class_Wide_Type (Typ)
598 and then not Is_Derived_Type (Typ)
599 and then not Is_Generic_Type (Typ)
600 and then not In_Instance
602 Error_Msg_N ("?declaration of& is too late!", Subp);
604 ("\spec should appear immediately after declaration of &!",
612 -- In case of functions check also the result type
614 if Ekind (Subp) = E_Function then
615 if Is_Access_Type (Etype (Subp)) then
616 Typ := Designated_Type (Etype (Subp));
621 if not Is_Class_Wide_Type (Typ)
622 and then Is_Interface (Typ)
623 and then not Is_Derived_Type (Typ)
625 Error_Msg_N ("?declaration of& is too late!", Subp);
627 ("\spec should appear immediately after declaration of &!",
635 -- The subprograms build internally after the freezing point (such as
636 -- the Init procedure) are not primitives
638 elsif Is_Frozen (Tagged_Type)
639 and then not Comes_From_Source (Subp)
640 and then not Has_Dispatching_Parent
644 -- The operation may be a child unit, whose scope is the defining
645 -- package, but which is not a primitive operation of the type.
647 elsif Is_Child_Unit (Subp) then
650 -- If the subprogram is not defined in a package spec, the only case
651 -- where it can be a dispatching op is when it overrides an operation
652 -- before the freezing point of the type.
654 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
655 or else In_Package_Body (Scope (Subp)))
656 and then not Has_Dispatching_Parent
658 if not Comes_From_Source (Subp)
659 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
663 -- If the type is already frozen, the overriding is not allowed
664 -- except when Old_Subp is not a dispatching operation (which
665 -- can occur when Old_Subp was inherited by an untagged type).
666 -- However, a body with no previous spec freezes the type "after"
667 -- its declaration, and therefore is a legal overriding (unless
668 -- the type has already been frozen). Only the first such body
671 elsif Present (Old_Subp)
672 and then Is_Dispatching_Operation (Old_Subp)
674 if Comes_From_Source (Subp)
676 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
677 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
680 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
681 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
684 -- ??? The checks here for whether the type has been
685 -- frozen prior to the new body are not complete. It's
686 -- not simple to check frozenness at this point since
687 -- the body has already caused the type to be prematurely
688 -- frozen in Analyze_Declarations, but we're forced to
689 -- recheck this here because of the odd rule interpretation
690 -- that allows the overriding if the type wasn't frozen
691 -- prior to the body. The freezing action should probably
692 -- be delayed until after the spec is seen, but that's
693 -- a tricky change to the delicate freezing code.
695 -- Look at each declaration following the type up
696 -- until the new subprogram body. If any of the
697 -- declarations is a body then the type has been
698 -- frozen already so the overriding primitive is
701 while Present (Decl_Item)
702 and then (Decl_Item /= Subp_Body)
704 if Comes_From_Source (Decl_Item)
705 and then (Nkind (Decl_Item) in N_Proper_Body
706 or else Nkind (Decl_Item) in N_Body_Stub)
708 Error_Msg_N ("overriding of& is too late!", Subp);
710 ("\spec should appear immediately after the type!",
718 -- If the subprogram doesn't follow in the list of
719 -- declarations including the type then the type
720 -- has definitely been frozen already and the body
723 if No (Decl_Item) then
724 Error_Msg_N ("overriding of& is too late!", Subp);
726 ("\spec should appear immediately after the type!",
729 elsif Is_Frozen (Subp) then
731 -- The subprogram body declares a primitive operation.
732 -- if the subprogram is already frozen, we must update
733 -- its dispatching information explicitly here. The
734 -- information is taken from the overridden subprogram.
736 Body_Is_Last_Primitive := True;
738 if Present (DTC_Entity (Old_Subp)) then
739 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
740 Set_DT_Position (Subp, DT_Position (Old_Subp));
742 if not Restriction_Active (No_Dispatching_Calls) then
743 if Building_Static_DT (Tagged_Type) then
745 -- If the static dispatch table has not been
746 -- built then there is nothing else to do now;
747 -- otherwise we notify that we cannot build the
748 -- static dispatch table.
750 if Has_Dispatch_Table (Tagged_Type) then
752 ("overriding of& is too late for building" &
753 " static dispatch tables!", Subp);
755 ("\spec should appear immediately after" &
760 Register_Primitive (Sloc (Subp_Body),
762 Ins_Nod => Subp_Body);
770 Error_Msg_N ("overriding of& is too late!", Subp);
772 ("\subprogram spec should appear immediately after the type!",
776 -- If the type is not frozen yet and we are not in the overriding
777 -- case it looks suspiciously like an attempt to define a primitive
780 elsif not Is_Frozen (Tagged_Type) then
782 ("?not dispatching (must be defined in a package spec)", Subp);
785 -- When the type is frozen, it is legitimate to define a new
786 -- non-primitive operation.
792 -- Now, we are sure that the scope is a package spec. If the subprogram
793 -- is declared after the freezing point of the type that's an error
795 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
796 Error_Msg_N ("this primitive operation is declared too late", Subp);
798 ("?no primitive operations for& after this line",
799 Freeze_Node (Tagged_Type),
804 Check_Controlling_Formals (Tagged_Type, Subp);
806 -- Now it should be a correct primitive operation, put it in the list
808 if Present (Old_Subp) then
809 Check_Subtype_Conformant (Subp, Old_Subp);
811 if (Chars (Subp) = Name_Initialize
812 or else Chars (Subp) = Name_Adjust
813 or else Chars (Subp) = Name_Finalize)
814 and then Is_Controlled (Tagged_Type)
815 and then not Is_Visibly_Controlled (Tagged_Type)
817 Set_Is_Overriding_Operation (Subp, False);
819 ("operation does not override inherited&?", Subp, Subp);
821 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
822 Set_Is_Overriding_Operation (Subp);
824 -- Ada 2005 (AI-251): In case of late overriding of a primitive
825 -- that covers abstract interface subprograms we must register it
826 -- in all the secondary dispatch tables associated with abstract
829 if Body_Is_Last_Primitive then
831 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
836 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
837 while Present (Elmt) loop
840 if Present (Alias (Prim))
841 and then Present (Abstract_Interface_Alias (Prim))
842 and then Alias (Prim) = Subp
844 Register_Primitive (Sloc (Prim),
846 Ins_Nod => Subp_Body);
852 -- Redisplay the contents of the updated dispatch table
854 if Debug_Flag_ZZ then
855 Write_Str ("Late overriding: ");
856 Write_DT (Tagged_Type);
862 -- If no old subprogram, then we add this as a dispatching operation,
863 -- but we avoid doing this if an error was posted, to prevent annoying
866 elsif not Error_Posted (Subp) then
867 Add_Dispatching_Operation (Tagged_Type, Subp);
870 Set_Is_Dispatching_Operation (Subp, True);
872 if not Body_Is_Last_Primitive then
873 Set_DT_Position (Subp, No_Uint);
875 elsif Has_Controlled_Component (Tagged_Type)
877 (Chars (Subp) = Name_Initialize
878 or else Chars (Subp) = Name_Adjust
879 or else Chars (Subp) = Name_Finalize)
882 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
886 Old_Spec : Entity_Id;
888 C_Names : constant array (1 .. 3) of Name_Id :=
893 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
894 (TSS_Deep_Initialize,
899 -- Remove previous controlled function, which was constructed
900 -- and analyzed when the type was frozen. This requires
901 -- removing the body of the redefined primitive, as well as
902 -- its specification if needed (there is no spec created for
903 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
904 -- the exception information that may have been generated for
905 -- it when front end zero-cost tables are enabled.
907 for J in D_Names'Range loop
908 Old_P := TSS (Tagged_Type, D_Names (J));
911 and then Chars (Subp) = C_Names (J)
913 Old_Bod := Unit_Declaration_Node (Old_P);
915 Set_Is_Eliminated (Old_P);
916 Set_Scope (Old_P, Scope (Current_Scope));
918 if Nkind (Old_Bod) = N_Subprogram_Body
919 and then Present (Corresponding_Spec (Old_Bod))
921 Old_Spec := Corresponding_Spec (Old_Bod);
922 Set_Has_Completion (Old_Spec, False);
927 Build_Late_Proc (Tagged_Type, Chars (Subp));
929 -- The new operation is added to the actions of the freeze
930 -- node for the type, but this node has already been analyzed,
931 -- so we must retrieve and analyze explicitly the new body.
934 and then Present (Actions (F_Node))
936 Decl := Last (Actions (F_Node));
941 end Check_Dispatching_Operation;
943 ------------------------------------------
944 -- Check_Operation_From_Incomplete_Type --
945 ------------------------------------------
947 procedure Check_Operation_From_Incomplete_Type
951 Full : constant Entity_Id := Full_View (Typ);
952 Parent_Typ : constant Entity_Id := Etype (Full);
953 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
954 New_Prim : constant Elist_Id := Primitive_Operations (Full);
956 Prev : Elmt_Id := No_Elmt;
958 function Derives_From (Proc : Entity_Id) return Boolean;
959 -- Check that Subp has the signature of an operation derived from Proc.
960 -- Subp has an access parameter that designates Typ.
966 function Derives_From (Proc : Entity_Id) return Boolean is
970 if Chars (Proc) /= Chars (Subp) then
974 F1 := First_Formal (Proc);
975 F2 := First_Formal (Subp);
977 while Present (F1) and then Present (F2) loop
979 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
981 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
984 elsif Designated_Type (Etype (F1)) = Parent_Typ
985 and then Designated_Type (Etype (F2)) /= Full
990 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
993 elsif Etype (F1) /= Etype (F2) then
1001 return No (F1) and then No (F2);
1004 -- Start of processing for Check_Operation_From_Incomplete_Type
1007 -- The operation may override an inherited one, or may be a new one
1008 -- altogether. The inherited operation will have been hidden by the
1009 -- current one at the point of the type derivation, so it does not
1010 -- appear in the list of primitive operations of the type. We have to
1011 -- find the proper place of insertion in the list of primitive opera-
1012 -- tions by iterating over the list for the parent type.
1014 Op1 := First_Elmt (Old_Prim);
1015 Op2 := First_Elmt (New_Prim);
1017 while Present (Op1) and then Present (Op2) loop
1019 if Derives_From (Node (Op1)) then
1022 Prepend_Elmt (Subp, New_Prim);
1024 Insert_Elmt_After (Subp, Prev);
1035 -- Operation is a new primitive
1037 Append_Elmt (Subp, New_Prim);
1038 end Check_Operation_From_Incomplete_Type;
1040 ---------------------------------------
1041 -- Check_Operation_From_Private_View --
1042 ---------------------------------------
1044 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1045 Tagged_Type : Entity_Id;
1048 if Is_Dispatching_Operation (Alias (Subp)) then
1049 Set_Scope (Subp, Current_Scope);
1050 Tagged_Type := Find_Dispatching_Type (Subp);
1052 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1053 Append_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1055 -- If Old_Subp isn't already marked as dispatching then
1056 -- this is the case of an operation of an untagged private
1057 -- type fulfilled by a tagged type that overrides an
1058 -- inherited dispatching operation, so we set the necessary
1059 -- dispatching attributes here.
1061 if not Is_Dispatching_Operation (Old_Subp) then
1063 -- If the untagged type has no discriminants, and the full
1064 -- view is constrained, there will be a spurious mismatch
1065 -- of subtypes on the controlling arguments, because the tagged
1066 -- type is the internal base type introduced in the derivation.
1067 -- Use the original type to verify conformance, rather than the
1070 if not Comes_From_Source (Tagged_Type)
1071 and then Has_Discriminants (Tagged_Type)
1076 Formal := First_Formal (Old_Subp);
1077 while Present (Formal) loop
1078 if Tagged_Type = Base_Type (Etype (Formal)) then
1079 Tagged_Type := Etype (Formal);
1082 Next_Formal (Formal);
1086 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1087 Tagged_Type := Etype (Old_Subp);
1091 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1092 Set_Is_Dispatching_Operation (Old_Subp, True);
1093 Set_DT_Position (Old_Subp, No_Uint);
1096 -- If the old subprogram is an explicit renaming of some other
1097 -- entity, it is not overridden by the inherited subprogram.
1098 -- Otherwise, update its alias and other attributes.
1100 if Present (Alias (Old_Subp))
1101 and then Nkind (Unit_Declaration_Node (Old_Subp))
1102 /= N_Subprogram_Renaming_Declaration
1104 Set_Alias (Old_Subp, Alias (Subp));
1106 -- The derived subprogram should inherit the abstractness
1107 -- of the parent subprogram (except in the case of a function
1108 -- returning the type). This sets the abstractness properly
1109 -- for cases where a private extension may have inherited
1110 -- an abstract operation, but the full type is derived from
1111 -- a descendant type and inherits a nonabstract version.
1113 if Etype (Subp) /= Tagged_Type then
1114 Set_Is_Abstract_Subprogram
1115 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1120 end Check_Operation_From_Private_View;
1122 --------------------------
1123 -- Find_Controlling_Arg --
1124 --------------------------
1126 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1127 Orig_Node : constant Node_Id := Original_Node (N);
1131 if Nkind (Orig_Node) = N_Qualified_Expression then
1132 return Find_Controlling_Arg (Expression (Orig_Node));
1135 -- Dispatching on result case. If expansion is disabled, the node still
1136 -- has the structure of a function call. However, if the function name
1137 -- is an operator and the call was given in infix form, the original
1138 -- node has no controlling result and we must examine the current node.
1140 if Nkind (N) = N_Function_Call
1141 and then Present (Controlling_Argument (N))
1142 and then Has_Controlling_Result (Entity (Name (N)))
1144 return Controlling_Argument (N);
1146 -- If expansion is enabled, the call may have been transformed into
1147 -- an indirect call, and we need to recover the original node.
1149 elsif Nkind (Orig_Node) = N_Function_Call
1150 and then Present (Controlling_Argument (Orig_Node))
1151 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1153 return Controlling_Argument (Orig_Node);
1157 elsif Is_Controlling_Actual (N)
1159 (Nkind (Parent (N)) = N_Qualified_Expression
1160 and then Is_Controlling_Actual (Parent (N)))
1164 if Is_Access_Type (Typ) then
1165 -- In the case of an Access attribute, use the type of
1166 -- the prefix, since in the case of an actual for an
1167 -- access parameter, the attribute's type may be of a
1168 -- specific designated type, even though the prefix
1169 -- type is class-wide.
1171 if Nkind (N) = N_Attribute_Reference then
1172 Typ := Etype (Prefix (N));
1174 -- An allocator is dispatching if the type of qualified
1175 -- expression is class_wide, in which case this is the
1176 -- controlling type.
1178 elsif Nkind (Orig_Node) = N_Allocator
1179 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1181 Typ := Etype (Expression (Orig_Node));
1184 Typ := Designated_Type (Typ);
1188 if Is_Class_Wide_Type (Typ)
1190 (Nkind (Parent (N)) = N_Qualified_Expression
1191 and then Is_Access_Type (Etype (N))
1192 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1199 end Find_Controlling_Arg;
1201 ---------------------------
1202 -- Find_Dispatching_Type --
1203 ---------------------------
1205 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1207 Ctrl_Type : Entity_Id;
1210 if Present (DTC_Entity (Subp)) then
1211 return Scope (DTC_Entity (Subp));
1214 Formal := First_Formal (Subp);
1215 while Present (Formal) loop
1216 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1218 if Present (Ctrl_Type) then
1222 Next_Formal (Formal);
1225 -- The subprogram may also be dispatching on result
1227 if Present (Etype (Subp)) then
1228 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1230 if Present (Ctrl_Type) then
1237 end Find_Dispatching_Type;
1239 ---------------------------
1240 -- Is_Dynamically_Tagged --
1241 ---------------------------
1243 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1245 if Nkind (N) = N_Error then
1248 return Find_Controlling_Arg (N) /= Empty;
1250 end Is_Dynamically_Tagged;
1252 --------------------------
1253 -- Is_Tag_Indeterminate --
1254 --------------------------
1256 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1259 Orig_Node : constant Node_Id := Original_Node (N);
1262 if Nkind (Orig_Node) = N_Function_Call
1263 and then Is_Entity_Name (Name (Orig_Node))
1265 Nam := Entity (Name (Orig_Node));
1267 if not Has_Controlling_Result (Nam) then
1270 -- An explicit dereference means that the call has already been
1271 -- expanded and there is no tag to propagate.
1273 elsif Nkind (N) = N_Explicit_Dereference then
1276 -- If there are no actuals, the call is tag-indeterminate
1278 elsif No (Parameter_Associations (Orig_Node)) then
1282 Actual := First_Actual (Orig_Node);
1283 while Present (Actual) loop
1284 if Is_Controlling_Actual (Actual)
1285 and then not Is_Tag_Indeterminate (Actual)
1287 return False; -- one operand is dispatching
1290 Next_Actual (Actual);
1296 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1297 return Is_Tag_Indeterminate (Expression (Orig_Node));
1299 -- Case of a call to the Input attribute (possibly rewritten), which is
1300 -- always tag-indeterminate except when its prefix is a Class attribute.
1302 elsif Nkind (Orig_Node) = N_Attribute_Reference
1304 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1306 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1310 -- In Ada 2005 a function that returns an anonymous access type can
1311 -- dispatching, and the dereference of a call to such a function
1312 -- is also tag-indeterminate.
1314 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1315 and then Ada_Version >= Ada_05
1317 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1322 end Is_Tag_Indeterminate;
1324 ------------------------------------
1325 -- Override_Dispatching_Operation --
1326 ------------------------------------
1328 procedure Override_Dispatching_Operation
1329 (Tagged_Type : Entity_Id;
1330 Prev_Op : Entity_Id;
1337 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1338 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1340 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1341 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1342 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1345 -- If there is no previous operation to override, the type declaration
1346 -- was malformed, and an error must have been emitted already.
1348 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1349 while Present (Elmt)
1350 and then Node (Elmt) /= Prev_Op
1359 Replace_Elmt (Elmt, New_Op);
1361 if Ada_Version >= Ada_05
1362 and then Has_Abstract_Interfaces (Tagged_Type)
1364 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1365 -- entities of the overridden primitive to reference New_Op, and also
1366 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1367 -- that the new operation is subtype conformant with the interface
1368 -- operations that it implements (for operations inherited from the
1369 -- parent itself, this check is made when building the derived type).
1371 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1372 while Present (Elmt) loop
1373 Prim := Node (Elmt);
1375 if Prim = New_Op then
1378 -- Note: The check on Is_Subprogram protects the frontend against
1379 -- reading attributes in entities that are not yet fully decorated
1381 elsif Is_Subprogram (Prim)
1382 and then Present (Abstract_Interface_Alias (Prim))
1383 and then Alias (Prim) = Prev_Op
1385 Set_Alias (Prim, New_Op);
1386 Check_Subtype_Conformant (New_Op, Prim);
1387 Set_Is_Abstract_Subprogram
1388 (Prim, Is_Abstract_Subprogram (New_Op));
1390 -- Ensure that this entity will be expanded to fill the
1391 -- corresponding entry in its dispatch table.
1393 if not Is_Abstract_Subprogram (Prim) then
1394 Set_Has_Delayed_Freeze (Prim);
1402 if (not Is_Package_Or_Generic_Package (Current_Scope))
1403 or else not In_Private_Part (Current_Scope)
1405 -- Not a private primitive
1409 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1411 -- Make the overriding operation into an alias of the implicit one.
1412 -- In this fashion a call from outside ends up calling the new body
1413 -- even if non-dispatching, and a call from inside calls the
1414 -- overriding operation because it hides the implicit one. To
1415 -- indicate that the body of Prev_Op is never called, set its
1416 -- dispatch table entity to Empty.
1418 Set_Alias (Prev_Op, New_Op);
1419 Set_DTC_Entity (Prev_Op, Empty);
1422 end Override_Dispatching_Operation;
1428 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1429 Call_Node : Node_Id;
1433 if Nkind (Actual) = N_Function_Call then
1434 Call_Node := Actual;
1436 elsif Nkind (Actual) = N_Identifier
1437 and then Nkind (Original_Node (Actual)) = N_Function_Call
1439 -- Call rewritten as object declaration when stack-checking
1440 -- is enabled. Propagate tag to expression in declaration, which
1441 -- is original call.
1443 Call_Node := Expression (Parent (Entity (Actual)));
1445 -- Ada 2005: If this is a dereference of a call to a function with a
1446 -- dispatching access-result, the tag is propagated when the dereference
1447 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1449 elsif Nkind (Actual) = N_Explicit_Dereference
1450 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1454 -- Only other possibilities are parenthesized or qualified expression,
1455 -- or an expander-generated unchecked conversion of a function call to
1456 -- a stream Input attribute.
1459 Call_Node := Expression (Actual);
1462 -- Do not set the Controlling_Argument if already set. This happens
1463 -- in the special case of _Input (see Exp_Attr, case Input).
1465 if No (Controlling_Argument (Call_Node)) then
1466 Set_Controlling_Argument (Call_Node, Control);
1469 Arg := First_Actual (Call_Node);
1471 while Present (Arg) loop
1472 if Is_Tag_Indeterminate (Arg) then
1473 Propagate_Tag (Control, Arg);
1479 -- Expansion of dispatching calls is suppressed when VM_Target, because
1480 -- the VM back-ends directly handle the generation of dispatching
1481 -- calls and would have to undo any expansion to an indirect call.
1483 if VM_Target = No_VM then
1484 Expand_Dispatching_Call (Call_Node);