Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / sem_disp.adb
blob5924039e6f9a0b9d580df995f7df56ce5907b3e6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ D I S P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
37 with Opt; use Opt;
38 with Output; use Output;
39 with Restrict; use Restrict;
40 with Rident; use Rident;
41 with Sem; use Sem;
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;
61 New_Op : Entity_Id);
62 -- Add New_Op in the list of primitive operations of Tagged_Type
64 function Check_Controlling_Type
65 (T : Entity_Id;
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;
79 New_Op : Entity_Id)
81 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
82 begin
83 Append_Elmt (New_Op, List);
84 end Add_Dispatching_Operation;
86 -------------------------------
87 -- Check_Controlling_Formals --
88 -------------------------------
90 procedure Check_Controlling_Formals
91 (Typ : Entity_Id;
92 Subp : Entity_Id)
94 Formal : Entity_Id;
95 Ctrl_Type : Entity_Id;
97 begin
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
107 -- type.
109 if Is_Concurrent_Type (Ctrl_Type)
110 and then Present (Corresponding_Record_Type (Ctrl_Type))
111 then
112 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
113 end if;
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));
125 end if;
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)))
133 then
134 Error_Msg_N
135 ("parameter subtype does not match controlling type",
136 Formal);
137 end if;
139 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
140 Error_Msg_N
141 ("parameter subtype does not match controlling type",
142 Formal);
143 end if;
145 if Present (Default_Value (Formal)) then
146 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
147 Error_Msg_N
148 ("default not allowed for controlling access parameter",
149 Default_Value (Formal));
151 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
152 Error_Msg_N
153 ("default expression must be a tag indeterminate" &
154 " function call", Default_Value (Formal));
155 end if;
156 end if;
158 elsif Comes_From_Source (Subp) then
159 Error_Msg_N
160 ("operation can be dispatching in only one type", Subp);
161 end if;
162 end if;
164 Next_Formal (Formal);
165 end loop;
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
179 and then
180 Subtypes_Statically_Match
181 (Typ, Designated_Type (Etype (Subp))))
182 then
183 null;
185 else
186 Error_Msg_N
187 ("result subtype does not match controlling type", Subp);
188 end if;
190 elsif Comes_From_Source (Subp) then
191 Error_Msg_N
192 ("operation can be dispatching in only one type", Subp);
193 end if;
194 end if;
195 end if;
196 end Check_Controlling_Formals;
198 ----------------------------
199 -- Check_Controlling_Type --
200 ----------------------------
202 function Check_Controlling_Type
203 (T : Entity_Id;
204 Subp : Entity_Id) return Entity_Id
206 Tagged_Type : Entity_Id := Empty;
208 begin
209 if Is_Tagged_Type (T) then
210 if Is_First_Subtype (T) then
211 Tagged_Type := T;
212 else
213 Tagged_Type := Base_Type (T);
214 end if;
216 elsif Ekind (T) = E_Anonymous_Access_Type
217 and then Is_Tagged_Type (Designated_Type (T))
218 then
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);
222 else
223 Tagged_Type := Base_Type (Designated_Type (T));
224 end if;
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)))
236 then
237 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
238 Tagged_Type := Non_Limited_View (Designated_Type (T));
239 else
240 Tagged_Type := Base_Type (Non_Limited_View
241 (Designated_Type (T)));
242 end if;
243 end if;
244 end if;
246 if No (Tagged_Type)
247 or else Is_Class_Wide_Type (Tagged_Type)
248 then
249 return Empty;
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)))
258 or else
259 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
260 or else
261 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
262 and then
263 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
264 and then
265 Is_Abstract_Subprogram (Subp))
266 then
267 return Tagged_Type;
269 else
270 return Empty;
271 end if;
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);
280 Actual : Node_Id;
281 Formal : Entity_Id;
282 Control : Node_Id := Empty;
283 Func : Entity_Id;
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));
299 Par : Node_Id;
301 begin
302 if Is_Abstract_Subprogram (Subp)
303 and then No (Controlling_Argument (N))
304 then
305 if Present (Alias (Subp))
306 and then not Is_Abstract_Subprogram (Alias (Subp))
307 and then No (DTC_Entity (Subp))
308 then
309 -- Private overriding of inherited abstract operation,
310 -- call is legal.
312 Set_Entity (Name (N), Alias (Subp));
313 return;
315 else
316 Par := Parent (N);
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))
326 then
327 return;
329 elsif Nkind (Par) = N_Qualified_Expression
330 or else Nkind (Par) = N_Unchecked_Type_Conversion
331 then
332 Par := Parent (Par);
334 else
335 if Ekind (Subp) = E_Function then
336 Error_Msg_N
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
341 -- tagged operand.
343 else
344 Error_Msg_N
345 ("call to abstract procedure must be dispatching",
347 end if;
349 return;
350 end if;
351 end loop;
352 end if;
353 end if;
354 end Check_Dispatching_Context;
356 -- Start of processing for Check_Dispatching_Call
358 begin
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))
379 then
380 Indeterm_Ancestor_Call := True;
381 Indeterm_Ctrl_Type := Etype (Formal);
382 end if;
384 Next_Actual (Actual);
385 Next_Formal (Formal);
386 end loop;
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.
401 if No (Control)
402 and then Indeterm_Ancestor_Call
403 then
404 Control :=
405 Make_Attribute_Reference (Loc,
406 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
407 Attribute_Name => Name_Tag);
408 Analyze (Control);
409 end if;
411 if Present (Control) then
413 -- Verify that no controlling arguments are statically tagged
415 if Debug_Flag_E then
416 Write_Str ("Found Dispatching call");
417 Write_Int (Int (N));
418 Write_Eol;
419 end if;
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);
442 else
443 Error_Msg_N
444 ("controlling argument is not dynamically tagged",
445 Actual);
446 return;
447 end if;
448 end if;
450 Next_Actual (Actual);
451 end loop;
453 -- Mark call as a dispatching call
455 Set_Controlling_Argument (N, Control);
456 Check_Restriction (No_Dispatching_Calls, N);
458 else
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).
476 elsif
477 Nkind (Original_Node (Actual)) = N_Attribute_Reference
478 then
479 Func := Empty;
481 -- Only other possibility is a qualified expression whose
482 -- constituent expression is itself a call.
484 else
485 Func :=
486 Entity (Name
487 (Original_Node
488 (Expression (Original_Node (Actual)))));
489 end if;
491 if Present (Func) and then Is_Abstract_Subprogram (Func) then
492 Error_Msg_N (
493 "call to abstract function must be dispatching", N);
494 end if;
495 end if;
497 Next_Actual (Actual);
498 end loop;
500 Check_Dispatching_Context;
501 end if;
503 else
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;
509 end if;
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);
534 begin
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
542 begin
543 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
544 return;
545 end if;
547 Set_Is_Dispatching_Operation (Subp, False);
548 Tagged_Type := Find_Dispatching_Type (Subp);
550 -- Ada 2005 (AI-345)
552 if Ada_Version = Ada_05
553 and then Present (Tagged_Type)
554 and then Is_Concurrent_Type (Tagged_Type)
555 then
556 -- Protect the frontend against previously detected errors
558 if No (Corresponding_Record_Type (Tagged_Type)) then
559 return;
560 end if;
562 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
563 end if;
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.
582 declare
583 E : Entity_Id;
584 Typ : Entity_Id;
586 begin
587 E := First_Entity (Subp);
588 while Present (E) loop
589 if Is_Access_Type (Etype (E)) then
590 Typ := Designated_Type (Etype (E));
591 else
592 Typ := Etype (E);
593 end if;
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
601 then
602 Error_Msg_N ("?declaration of& is too late!", Subp);
603 Error_Msg_NE
604 ("\spec should appear immediately after declaration of &!",
605 Subp, Typ);
606 exit;
607 end if;
609 Next_Entity (E);
610 end loop;
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));
617 else
618 Typ := Etype (Subp);
619 end if;
621 if not Is_Class_Wide_Type (Typ)
622 and then Is_Interface (Typ)
623 and then not Is_Derived_Type (Typ)
624 then
625 Error_Msg_N ("?declaration of& is too late!", Subp);
626 Error_Msg_NE
627 ("\spec should appear immediately after declaration of &!",
628 Subp, Typ);
629 end if;
630 end if;
631 end;
633 return;
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
641 then
642 return;
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
648 return;
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
657 then
658 if not Comes_From_Source (Subp)
659 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
660 then
661 null;
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
669 -- is legal.
671 elsif Present (Old_Subp)
672 and then Is_Dispatching_Operation (Old_Subp)
673 then
674 if Comes_From_Source (Subp)
675 and then
676 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
677 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
678 then
679 declare
680 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
681 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
683 begin
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
699 -- illegal.
701 while Present (Decl_Item)
702 and then (Decl_Item /= Subp_Body)
703 loop
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)
707 then
708 Error_Msg_N ("overriding of& is too late!", Subp);
709 Error_Msg_N
710 ("\spec should appear immediately after the type!",
711 Subp);
712 exit;
713 end if;
715 Next (Decl_Item);
716 end loop;
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
721 -- is illegal.
723 if No (Decl_Item) then
724 Error_Msg_N ("overriding of& is too late!", Subp);
725 Error_Msg_N
726 ("\spec should appear immediately after the type!",
727 Subp);
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
751 Error_Msg_N
752 ("overriding of& is too late for building" &
753 " static dispatch tables!", Subp);
754 Error_Msg_N
755 ("\spec should appear immediately after" &
756 " the type!", Subp);
757 end if;
759 else
760 Register_Primitive (Sloc (Subp_Body),
761 Prim => Subp,
762 Ins_Nod => Subp_Body);
763 end if;
764 end if;
765 end if;
766 end if;
767 end;
769 else
770 Error_Msg_N ("overriding of& is too late!", Subp);
771 Error_Msg_N
772 ("\subprogram spec should appear immediately after the type!",
773 Subp);
774 end if;
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
778 -- operation.
780 elsif not Is_Frozen (Tagged_Type) then
781 Error_Msg_N
782 ("?not dispatching (must be defined in a package spec)", Subp);
783 return;
785 -- When the type is frozen, it is legitimate to define a new
786 -- non-primitive operation.
788 else
789 return;
790 end if;
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);
797 Error_Msg_NE
798 ("?no primitive operations for& after this line",
799 Freeze_Node (Tagged_Type),
800 Tagged_Type);
801 return;
802 end if;
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)
816 then
817 Set_Is_Overriding_Operation (Subp, False);
818 Error_Msg_NE
819 ("operation does not override inherited&?", Subp, Subp);
820 else
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
827 -- interfaces.
829 if Body_Is_Last_Primitive then
830 declare
831 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
832 Elmt : Elmt_Id;
833 Prim : Node_Id;
835 begin
836 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
837 while Present (Elmt) loop
838 Prim := Node (Elmt);
840 if Present (Alias (Prim))
841 and then Present (Abstract_Interface_Alias (Prim))
842 and then Alias (Prim) = Subp
843 then
844 Register_Primitive (Sloc (Prim),
845 Prim => Prim,
846 Ins_Nod => Subp_Body);
847 end if;
849 Next_Elmt (Elmt);
850 end loop;
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);
857 end if;
858 end;
859 end if;
860 end if;
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
864 -- cascaded errors.
866 elsif not Error_Posted (Subp) then
867 Add_Dispatching_Operation (Tagged_Type, Subp);
868 end if;
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)
876 and then
877 (Chars (Subp) = Name_Initialize
878 or else Chars (Subp) = Name_Adjust
879 or else Chars (Subp) = Name_Finalize)
880 then
881 declare
882 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
883 Decl : Node_Id;
884 Old_P : Entity_Id;
885 Old_Bod : Node_Id;
886 Old_Spec : Entity_Id;
888 C_Names : constant array (1 .. 3) of Name_Id :=
889 (Name_Initialize,
890 Name_Adjust,
891 Name_Finalize);
893 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
894 (TSS_Deep_Initialize,
895 TSS_Deep_Adjust,
896 TSS_Deep_Finalize);
898 begin
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));
910 if Present (Old_P)
911 and then Chars (Subp) = C_Names (J)
912 then
913 Old_Bod := Unit_Declaration_Node (Old_P);
914 Remove (Old_Bod);
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))
920 then
921 Old_Spec := Corresponding_Spec (Old_Bod);
922 Set_Has_Completion (Old_Spec, False);
923 end if;
924 end if;
925 end loop;
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.
933 if Present (F_Node)
934 and then Present (Actions (F_Node))
935 then
936 Decl := Last (Actions (F_Node));
937 Analyze (Decl);
938 end if;
939 end;
940 end if;
941 end Check_Dispatching_Operation;
943 ------------------------------------------
944 -- Check_Operation_From_Incomplete_Type --
945 ------------------------------------------
947 procedure Check_Operation_From_Incomplete_Type
948 (Subp : Entity_Id;
949 Typ : Entity_Id)
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);
955 Op1, Op2 : Elmt_Id;
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.
962 ------------------
963 -- Derives_From --
964 ------------------
966 function Derives_From (Proc : Entity_Id) return Boolean is
967 F1, F2 : Entity_Id;
969 begin
970 if Chars (Proc) /= Chars (Subp) then
971 return False;
972 end if;
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
982 return False;
984 elsif Designated_Type (Etype (F1)) = Parent_Typ
985 and then Designated_Type (Etype (F2)) /= Full
986 then
987 return False;
988 end if;
990 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
991 return False;
993 elsif Etype (F1) /= Etype (F2) then
994 return False;
995 end if;
997 Next_Formal (F1);
998 Next_Formal (F2);
999 end loop;
1001 return No (F1) and then No (F2);
1002 end Derives_From;
1004 -- Start of processing for Check_Operation_From_Incomplete_Type
1006 begin
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
1021 if No (Prev) then
1022 Prepend_Elmt (Subp, New_Prim);
1023 else
1024 Insert_Elmt_After (Subp, Prev);
1025 end if;
1027 return;
1028 end if;
1030 Prev := Op2;
1031 Next_Elmt (Op1);
1032 Next_Elmt (Op2);
1033 end loop;
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;
1047 begin
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
1068 -- base type.
1070 if not Comes_From_Source (Tagged_Type)
1071 and then Has_Discriminants (Tagged_Type)
1072 then
1073 declare
1074 Formal : Entity_Id;
1075 begin
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);
1080 end if;
1082 Next_Formal (Formal);
1083 end loop;
1084 end;
1086 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1087 Tagged_Type := Etype (Old_Subp);
1088 end if;
1089 end if;
1091 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1092 Set_Is_Dispatching_Operation (Old_Subp, True);
1093 Set_DT_Position (Old_Subp, No_Uint);
1094 end if;
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
1103 then
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)));
1116 end if;
1117 end if;
1118 end if;
1119 end if;
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);
1128 Typ : Entity_Id;
1130 begin
1131 if Nkind (Orig_Node) = N_Qualified_Expression then
1132 return Find_Controlling_Arg (Expression (Orig_Node));
1133 end if;
1135 -- Dispatching on result case
1137 if Nkind (Orig_Node) = N_Function_Call
1138 and then Present (Controlling_Argument (Orig_Node))
1139 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1140 then
1141 return Controlling_Argument (Orig_Node);
1143 -- Normal case
1145 elsif Is_Controlling_Actual (N)
1146 or else
1147 (Nkind (Parent (N)) = N_Qualified_Expression
1148 and then Is_Controlling_Actual (Parent (N)))
1149 then
1150 Typ := Etype (N);
1152 if Is_Access_Type (Typ) then
1153 -- In the case of an Access attribute, use the type of
1154 -- the prefix, since in the case of an actual for an
1155 -- access parameter, the attribute's type may be of a
1156 -- specific designated type, even though the prefix
1157 -- type is class-wide.
1159 if Nkind (N) = N_Attribute_Reference then
1160 Typ := Etype (Prefix (N));
1162 -- An allocator is dispatching if the type of qualified
1163 -- expression is class_wide, in which case this is the
1164 -- controlling type.
1166 elsif Nkind (Orig_Node) = N_Allocator
1167 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1168 then
1169 Typ := Etype (Expression (Orig_Node));
1171 else
1172 Typ := Designated_Type (Typ);
1173 end if;
1174 end if;
1176 if Is_Class_Wide_Type (Typ)
1177 or else
1178 (Nkind (Parent (N)) = N_Qualified_Expression
1179 and then Is_Access_Type (Etype (N))
1180 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1181 then
1182 return N;
1183 end if;
1184 end if;
1186 return Empty;
1187 end Find_Controlling_Arg;
1189 ---------------------------
1190 -- Find_Dispatching_Type --
1191 ---------------------------
1193 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1194 Formal : Entity_Id;
1195 Ctrl_Type : Entity_Id;
1197 begin
1198 if Present (DTC_Entity (Subp)) then
1199 return Scope (DTC_Entity (Subp));
1201 else
1202 Formal := First_Formal (Subp);
1203 while Present (Formal) loop
1204 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1206 if Present (Ctrl_Type) then
1207 return Ctrl_Type;
1208 end if;
1210 Next_Formal (Formal);
1211 end loop;
1213 -- The subprogram may also be dispatching on result
1215 if Present (Etype (Subp)) then
1216 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
1218 if Present (Ctrl_Type) then
1219 return Ctrl_Type;
1220 end if;
1221 end if;
1222 end if;
1224 return Empty;
1225 end Find_Dispatching_Type;
1227 ---------------------------
1228 -- Is_Dynamically_Tagged --
1229 ---------------------------
1231 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1232 begin
1233 if Nkind (N) = N_Error then
1234 return False;
1235 else
1236 return Find_Controlling_Arg (N) /= Empty;
1237 end if;
1238 end Is_Dynamically_Tagged;
1240 --------------------------
1241 -- Is_Tag_Indeterminate --
1242 --------------------------
1244 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1245 Nam : Entity_Id;
1246 Actual : Node_Id;
1247 Orig_Node : constant Node_Id := Original_Node (N);
1249 begin
1250 if Nkind (Orig_Node) = N_Function_Call
1251 and then Is_Entity_Name (Name (Orig_Node))
1252 then
1253 Nam := Entity (Name (Orig_Node));
1255 if not Has_Controlling_Result (Nam) then
1256 return False;
1258 -- An explicit dereference means that the call has already been
1259 -- expanded and there is no tag to propagate.
1261 elsif Nkind (N) = N_Explicit_Dereference then
1262 return False;
1264 -- If there are no actuals, the call is tag-indeterminate
1266 elsif No (Parameter_Associations (Orig_Node)) then
1267 return True;
1269 else
1270 Actual := First_Actual (Orig_Node);
1271 while Present (Actual) loop
1272 if Is_Controlling_Actual (Actual)
1273 and then not Is_Tag_Indeterminate (Actual)
1274 then
1275 return False; -- one operand is dispatching
1276 end if;
1278 Next_Actual (Actual);
1279 end loop;
1281 return True;
1282 end if;
1284 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1285 return Is_Tag_Indeterminate (Expression (Orig_Node));
1287 -- Case of a call to the Input attribute (possibly rewritten), which is
1288 -- always tag-indeterminate except when its prefix is a Class attribute.
1290 elsif Nkind (Orig_Node) = N_Attribute_Reference
1291 and then
1292 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1293 and then
1294 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1295 then
1296 return True;
1298 -- In Ada 2005 a function that returns an anonymous access type can
1299 -- dispatching, and the dereference of a call to such a function
1300 -- is also tag-indeterminate.
1302 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1303 and then Ada_Version >= Ada_05
1304 then
1305 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1307 else
1308 return False;
1309 end if;
1310 end Is_Tag_Indeterminate;
1312 ------------------------------------
1313 -- Override_Dispatching_Operation --
1314 ------------------------------------
1316 procedure Override_Dispatching_Operation
1317 (Tagged_Type : Entity_Id;
1318 Prev_Op : Entity_Id;
1319 New_Op : Entity_Id)
1321 Elmt : Elmt_Id;
1322 Prim : Node_Id;
1324 begin
1325 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1326 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1328 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1329 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1330 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1331 end if;
1333 -- If there is no previous operation to override, the type declaration
1334 -- was malformed, and an error must have been emitted already.
1336 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1337 while Present (Elmt)
1338 and then Node (Elmt) /= Prev_Op
1339 loop
1340 Next_Elmt (Elmt);
1341 end loop;
1343 if No (Elmt) then
1344 return;
1345 end if;
1347 Replace_Elmt (Elmt, New_Op);
1349 if Ada_Version >= Ada_05
1350 and then Has_Abstract_Interfaces (Tagged_Type)
1351 then
1352 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1353 -- entities of the overridden primitive to reference New_Op, and also
1354 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1355 -- that the new operation is subtype conformant with the interface
1356 -- operations that it implements (for operations inherited from the
1357 -- parent itself, this check is made when building the derived type).
1359 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1360 while Present (Elmt) loop
1361 Prim := Node (Elmt);
1363 if Prim = New_Op then
1364 null;
1366 -- Note: The check on Is_Subprogram protects the frontend against
1367 -- reading attributes in entities that are not yet fully decorated
1369 elsif Is_Subprogram (Prim)
1370 and then Present (Abstract_Interface_Alias (Prim))
1371 and then Alias (Prim) = Prev_Op
1372 then
1373 Set_Alias (Prim, New_Op);
1374 Check_Subtype_Conformant (New_Op, Prim);
1375 Set_Is_Abstract_Subprogram
1376 (Prim, Is_Abstract_Subprogram (New_Op));
1378 -- Ensure that this entity will be expanded to fill the
1379 -- corresponding entry in its dispatch table.
1381 if not Is_Abstract_Subprogram (Prim) then
1382 Set_Has_Delayed_Freeze (Prim);
1383 end if;
1384 end if;
1386 Next_Elmt (Elmt);
1387 end loop;
1388 end if;
1390 if (not Is_Package_Or_Generic_Package (Current_Scope))
1391 or else not In_Private_Part (Current_Scope)
1392 then
1393 -- Not a private primitive
1395 null;
1397 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1399 -- Make the overriding operation into an alias of the implicit one.
1400 -- In this fashion a call from outside ends up calling the new body
1401 -- even if non-dispatching, and a call from inside calls the
1402 -- overriding operation because it hides the implicit one. To
1403 -- indicate that the body of Prev_Op is never called, set its
1404 -- dispatch table entity to Empty.
1406 Set_Alias (Prev_Op, New_Op);
1407 Set_DTC_Entity (Prev_Op, Empty);
1408 return;
1409 end if;
1410 end Override_Dispatching_Operation;
1412 -------------------
1413 -- Propagate_Tag --
1414 -------------------
1416 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1417 Call_Node : Node_Id;
1418 Arg : Node_Id;
1420 begin
1421 if Nkind (Actual) = N_Function_Call then
1422 Call_Node := Actual;
1424 elsif Nkind (Actual) = N_Identifier
1425 and then Nkind (Original_Node (Actual)) = N_Function_Call
1426 then
1427 -- Call rewritten as object declaration when stack-checking
1428 -- is enabled. Propagate tag to expression in declaration, which
1429 -- is original call.
1431 Call_Node := Expression (Parent (Entity (Actual)));
1433 -- Ada 2005: If this is a dereference of a call to a function with a
1434 -- dispatching access-result, the tag is propagated when the dereference
1435 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1437 elsif Nkind (Actual) = N_Explicit_Dereference
1438 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1439 then
1440 return;
1442 -- Only other possibilities are parenthesized or qualified expression,
1443 -- or an expander-generated unchecked conversion of a function call to
1444 -- a stream Input attribute.
1446 else
1447 Call_Node := Expression (Actual);
1448 end if;
1450 -- Do not set the Controlling_Argument if already set. This happens
1451 -- in the special case of _Input (see Exp_Attr, case Input).
1453 if No (Controlling_Argument (Call_Node)) then
1454 Set_Controlling_Argument (Call_Node, Control);
1455 end if;
1457 Arg := First_Actual (Call_Node);
1459 while Present (Arg) loop
1460 if Is_Tag_Indeterminate (Arg) then
1461 Propagate_Tag (Control, Arg);
1462 end if;
1464 Next_Actual (Arg);
1465 end loop;
1467 -- Expansion of dispatching calls is suppressed when VM_Target, because
1468 -- the VM back-ends directly handle the generation of dispatching
1469 -- calls and would have to undo any expansion to an indirect call.
1471 if VM_Target = No_VM then
1472 Expand_Dispatching_Call (Call_Node);
1473 end if;
1474 end Propagate_Tag;
1476 end Sem_Disp;