2014-10-10 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_disp.adb
bloba915ab05e77c005dda482a58a6ecf8183ae7f061
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-2014, 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_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;
39 with Opt; use Opt;
40 with Output; use Output;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
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;
65 New_Op : Entity_Id);
66 -- Add New_Op in the list of primitive operations of Tagged_Type
68 function Check_Controlling_Type
69 (T : Entity_Id;
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 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
78 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
79 -- type of S that has the same name of S, a type-conformant profile, an
80 -- original corresponding operation O that is a primitive of a visible
81 -- ancestor of the dispatching type of S and O is visible at the point of
82 -- of declaration of S. If the entity is found the Alias of S is set to the
83 -- original corresponding operation S and its Overridden_Operation is set
84 -- to the found entity; otherwise return Empty.
86 -- This routine does not search for non-hidden primitives since they are
87 -- covered by the normal Ada 2005 rules.
89 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
90 -- Check whether a primitive operation is inherited from an operation
91 -- declared in the visible part of its package.
93 -------------------------------
94 -- Add_Dispatching_Operation --
95 -------------------------------
97 procedure Add_Dispatching_Operation
98 (Tagged_Type : Entity_Id;
99 New_Op : Entity_Id)
101 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
103 begin
104 -- The dispatching operation may already be on the list, if it is the
105 -- wrapper for an inherited function of a null extension (see Exp_Ch3
106 -- for the construction of function wrappers). The list of primitive
107 -- operations must not contain duplicates.
109 Append_Unique_Elmt (New_Op, List);
110 end Add_Dispatching_Operation;
112 ---------------------------
113 -- Covers_Some_Interface --
114 ---------------------------
116 function Covers_Some_Interface (Prim : Entity_Id) return Boolean is
117 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
118 Elmt : Elmt_Id;
119 E : Entity_Id;
121 begin
122 pragma Assert (Is_Dispatching_Operation (Prim));
124 -- Although this is a dispatching primitive we must check if its
125 -- dispatching type is available because it may be the primitive
126 -- of a private type not defined as tagged in its partial view.
128 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
130 -- If the tagged type is frozen then the internal entities associated
131 -- with interfaces are available in the list of primitives of the
132 -- tagged type and can be used to speed up this search.
134 if Is_Frozen (Tagged_Type) then
135 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
136 while Present (Elmt) loop
137 E := Node (Elmt);
139 if Present (Interface_Alias (E))
140 and then Alias (E) = Prim
141 then
142 return True;
143 end if;
145 Next_Elmt (Elmt);
146 end loop;
148 -- Otherwise we must collect all the interface primitives and check
149 -- if the Prim will override some interface primitive.
151 else
152 declare
153 Ifaces_List : Elist_Id;
154 Iface_Elmt : Elmt_Id;
155 Iface : Entity_Id;
156 Iface_Prim : Entity_Id;
158 begin
159 Collect_Interfaces (Tagged_Type, Ifaces_List);
160 Iface_Elmt := First_Elmt (Ifaces_List);
161 while Present (Iface_Elmt) loop
162 Iface := Node (Iface_Elmt);
164 Elmt := First_Elmt (Primitive_Operations (Iface));
165 while Present (Elmt) loop
166 Iface_Prim := Node (Elmt);
168 if Chars (Iface) = Chars (Prim)
169 and then Is_Interface_Conformant
170 (Tagged_Type, Iface_Prim, Prim)
171 then
172 return True;
173 end if;
175 Next_Elmt (Elmt);
176 end loop;
178 Next_Elmt (Iface_Elmt);
179 end loop;
180 end;
181 end if;
182 end if;
184 return False;
185 end Covers_Some_Interface;
187 -------------------------------
188 -- Check_Controlling_Formals --
189 -------------------------------
191 procedure Check_Controlling_Formals
192 (Typ : Entity_Id;
193 Subp : Entity_Id)
195 Formal : Entity_Id;
196 Ctrl_Type : Entity_Id;
198 begin
199 Formal := First_Formal (Subp);
200 while Present (Formal) loop
201 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
203 if Present (Ctrl_Type) then
205 -- When controlling type is concurrent and declared within a
206 -- generic or inside an instance use corresponding record type.
208 if Is_Concurrent_Type (Ctrl_Type)
209 and then Present (Corresponding_Record_Type (Ctrl_Type))
210 then
211 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
212 end if;
214 if Ctrl_Type = Typ then
215 Set_Is_Controlling_Formal (Formal);
217 -- Ada 2005 (AI-231): Anonymous access types that are used in
218 -- controlling parameters exclude null because it is necessary
219 -- to read the tag to dispatch, and null has no tag.
221 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
222 Set_Can_Never_Be_Null (Etype (Formal));
223 Set_Is_Known_Non_Null (Etype (Formal));
224 end if;
226 -- Check that the parameter's nominal subtype statically
227 -- matches the first subtype.
229 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
230 if not Subtypes_Statically_Match
231 (Typ, Designated_Type (Etype (Formal)))
232 then
233 Error_Msg_N
234 ("parameter subtype does not match controlling type",
235 Formal);
236 end if;
238 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
239 Error_Msg_N
240 ("parameter subtype does not match controlling type",
241 Formal);
242 end if;
244 if Present (Default_Value (Formal)) then
246 -- In Ada 2005, access parameters can have defaults
248 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
249 and then Ada_Version < Ada_2005
250 then
251 Error_Msg_N
252 ("default not allowed for controlling access parameter",
253 Default_Value (Formal));
255 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
256 Error_Msg_N
257 ("default expression must be a tag indeterminate" &
258 " function call", Default_Value (Formal));
259 end if;
260 end if;
262 elsif Comes_From_Source (Subp) then
263 Error_Msg_N
264 ("operation can be dispatching in only one type", Subp);
265 end if;
266 end if;
268 Next_Formal (Formal);
269 end loop;
271 if Ekind_In (Subp, E_Function, E_Generic_Function) then
272 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
274 if Present (Ctrl_Type) then
275 if Ctrl_Type = Typ then
276 Set_Has_Controlling_Result (Subp);
278 -- Check that result subtype statically matches first subtype
279 -- (Ada 2005): Subp may have a controlling access result.
281 if Subtypes_Statically_Match (Typ, Etype (Subp))
282 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
283 and then
284 Subtypes_Statically_Match
285 (Typ, Designated_Type (Etype (Subp))))
286 then
287 null;
289 else
290 Error_Msg_N
291 ("result subtype does not match controlling type", Subp);
292 end if;
294 elsif Comes_From_Source (Subp) then
295 Error_Msg_N
296 ("operation can be dispatching in only one type", Subp);
297 end if;
298 end if;
299 end if;
300 end Check_Controlling_Formals;
302 ----------------------------
303 -- Check_Controlling_Type --
304 ----------------------------
306 function Check_Controlling_Type
307 (T : Entity_Id;
308 Subp : Entity_Id) return Entity_Id
310 Tagged_Type : Entity_Id := Empty;
312 begin
313 if Is_Tagged_Type (T) then
314 if Is_First_Subtype (T) then
315 Tagged_Type := T;
316 else
317 Tagged_Type := Base_Type (T);
318 end if;
320 elsif Ekind (T) = E_Anonymous_Access_Type
321 and then Is_Tagged_Type (Designated_Type (T))
322 then
323 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
324 if Is_First_Subtype (Designated_Type (T)) then
325 Tagged_Type := Designated_Type (T);
326 else
327 Tagged_Type := Base_Type (Designated_Type (T));
328 end if;
330 -- Ada 2005: an incomplete type can be tagged. An operation with an
331 -- access parameter of the type is dispatching.
333 elsif Scope (Designated_Type (T)) = Current_Scope then
334 Tagged_Type := Designated_Type (T);
336 -- Ada 2005 (AI-50217)
338 elsif From_Limited_With (Designated_Type (T))
339 and then Present (Non_Limited_View (Designated_Type (T)))
340 and then Scope (Designated_Type (T)) = Scope (Subp)
341 then
342 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
343 Tagged_Type := Non_Limited_View (Designated_Type (T));
344 else
345 Tagged_Type := Base_Type (Non_Limited_View
346 (Designated_Type (T)));
347 end if;
348 end if;
349 end if;
351 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
352 return Empty;
354 -- The dispatching type and the primitive operation must be defined in
355 -- the same scope, except in the case of internal operations and formal
356 -- abstract subprograms.
358 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
359 and then (not Is_Generic_Type (Tagged_Type)
360 or else not Comes_From_Source (Subp)))
361 or else
362 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
363 or else
364 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
365 and then
366 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
367 and then
368 Is_Abstract_Subprogram (Subp))
369 then
370 return Tagged_Type;
372 else
373 return Empty;
374 end if;
375 end Check_Controlling_Type;
377 ----------------------------
378 -- Check_Dispatching_Call --
379 ----------------------------
381 procedure Check_Dispatching_Call (N : Node_Id) is
382 Loc : constant Source_Ptr := Sloc (N);
383 Actual : Node_Id;
384 Formal : Entity_Id;
385 Control : Node_Id := Empty;
386 Func : Entity_Id;
387 Subp_Entity : Entity_Id;
388 Indeterm_Ancestor_Call : Boolean := False;
389 Indeterm_Ctrl_Type : Entity_Id;
391 Static_Tag : Node_Id := Empty;
392 -- If a controlling formal has a statically tagged actual, the tag of
393 -- this actual is to be used for any tag-indeterminate actual.
395 procedure Check_Direct_Call;
396 -- In the case when the controlling actual is a class-wide type whose
397 -- root type's completion is a task or protected type, the call is in
398 -- fact direct. This routine detects the above case and modifies the
399 -- call accordingly.
401 procedure Check_Dispatching_Context;
402 -- If the call is tag-indeterminate and the entity being called is
403 -- abstract, verify that the context is a call that will eventually
404 -- provide a tag for dispatching, or has provided one already.
406 -----------------------
407 -- Check_Direct_Call --
408 -----------------------
410 procedure Check_Direct_Call is
411 Typ : Entity_Id := Etype (Control);
413 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean;
414 -- Determine whether an entity denotes a user-defined equality
416 ------------------------------
417 -- Is_User_Defined_Equality --
418 ------------------------------
420 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
421 begin
422 return
423 Ekind (Id) = E_Function
424 and then Chars (Id) = Name_Op_Eq
425 and then Comes_From_Source (Id)
427 -- Internally generated equalities have a full type declaration
428 -- as their parent.
430 and then Nkind (Parent (Id)) = N_Function_Specification;
431 end Is_User_Defined_Equality;
433 -- Start of processing for Check_Direct_Call
435 begin
436 -- Predefined primitives do not receive wrappers since they are built
437 -- from scratch for the corresponding record of synchronized types.
438 -- Equality is in general predefined, but is excluded from the check
439 -- when it is user-defined.
441 if Is_Predefined_Dispatching_Operation (Subp_Entity)
442 and then not Is_User_Defined_Equality (Subp_Entity)
443 then
444 return;
445 end if;
447 if Is_Class_Wide_Type (Typ) then
448 Typ := Root_Type (Typ);
449 end if;
451 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
452 Typ := Full_View (Typ);
453 end if;
455 if Is_Concurrent_Type (Typ)
456 and then
457 Present (Corresponding_Record_Type (Typ))
458 then
459 Typ := Corresponding_Record_Type (Typ);
461 -- The concurrent record's list of primitives should contain a
462 -- wrapper for the entity of the call, retrieve it.
464 declare
465 Prim : Entity_Id;
466 Prim_Elmt : Elmt_Id;
467 Wrapper_Found : Boolean := False;
469 begin
470 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
471 while Present (Prim_Elmt) loop
472 Prim := Node (Prim_Elmt);
474 if Is_Primitive_Wrapper (Prim)
475 and then Wrapped_Entity (Prim) = Subp_Entity
476 then
477 Wrapper_Found := True;
478 exit;
479 end if;
481 Next_Elmt (Prim_Elmt);
482 end loop;
484 -- A primitive declared between two views should have a
485 -- corresponding wrapper.
487 pragma Assert (Wrapper_Found);
489 -- Modify the call by setting the proper entity
491 Set_Entity (Name (N), Prim);
492 end;
493 end if;
494 end Check_Direct_Call;
496 -------------------------------
497 -- Check_Dispatching_Context --
498 -------------------------------
500 procedure Check_Dispatching_Context is
501 Subp : constant Entity_Id := Entity (Name (N));
502 Typ : constant Entity_Id := Etype (Subp);
503 Par : Node_Id;
505 procedure Abstract_Context_Error;
506 -- Error for abstract call dispatching on result is not dispatching
508 ----------------------------
509 -- Abstract_Context_Error --
510 ----------------------------
512 procedure Abstract_Context_Error is
513 begin
514 if Ekind (Subp) = E_Function then
515 Error_Msg_N
516 ("call to abstract function must be dispatching", N);
518 -- This error can occur for a procedure in the case of a call to
519 -- an abstract formal procedure with a statically tagged operand.
521 else
522 Error_Msg_N
523 ("call to abstract procedure must be dispatching",
525 end if;
526 end Abstract_Context_Error;
528 -- Start of processing for Check_Dispatching_Context
530 begin
531 if Is_Abstract_Subprogram (Subp)
532 and then No (Controlling_Argument (N))
533 then
534 if Present (Alias (Subp))
535 and then not Is_Abstract_Subprogram (Alias (Subp))
536 and then No (DTC_Entity (Subp))
537 then
538 -- Private overriding of inherited abstract operation, call is
539 -- legal.
541 Set_Entity (Name (N), Alias (Subp));
542 return;
544 -- An obscure special case: a null procedure may have a class-
545 -- wide pre/postcondition that includes a call to an abstract
546 -- subp. Calls within the expression may not have been rewritten
547 -- as dispatching calls yet, because the null body appears in
548 -- the current declarative part. The expression will be properly
549 -- rewritten/reanalyzed when the postcondition procedure is built.
551 -- Similarly, if this is a pre/postcondition for an abstract
552 -- subprogram, it may call another abstract function which is
553 -- a primitive of an abstract type. The call is non-dispatching
554 -- but will be legal in overridings of the operation.
556 elsif In_Spec_Expression
557 and then Is_Subprogram (Current_Scope)
558 and then
559 ((Nkind (Parent (Current_Scope)) = N_Procedure_Specification
560 and then Null_Present (Parent (Current_Scope)))
561 or else Is_Abstract_Subprogram (Current_Scope))
562 then
563 null;
565 else
566 -- We need to determine whether the context of the call
567 -- provides a tag to make the call dispatching. This requires
568 -- the call to be the actual in an enclosing call, and that
569 -- actual must be controlling. If the call is an operand of
570 -- equality, the other operand must not ve abstract.
572 if not Is_Tagged_Type (Typ)
573 and then not
574 (Ekind (Typ) = E_Anonymous_Access_Type
575 and then Is_Tagged_Type (Designated_Type (Typ)))
576 then
577 Abstract_Context_Error;
578 return;
579 end if;
581 Par := Parent (N);
583 if Nkind (Par) = N_Parameter_Association then
584 Par := Parent (Par);
585 end if;
587 while Present (Par) loop
588 if Nkind_In (Par, N_Function_Call,
589 N_Procedure_Call_Statement)
590 and then Is_Entity_Name (Name (Par))
591 then
592 declare
593 A : Node_Id;
594 F : Entity_Id;
596 begin
597 -- Find formal for which call is the actual.
599 F := First_Formal (Entity (Name (Par)));
600 A := First_Actual (Par);
601 while Present (F) loop
602 if Is_Controlling_Formal (F)
603 and then (N = A or else Parent (N) = A)
604 then
605 return;
606 end if;
608 Next_Formal (F);
609 Next_Actual (A);
610 end loop;
612 Error_Msg_N
613 ("call to abstract function must be dispatching", N);
614 return;
615 end;
617 -- For equalitiy operators, one of the operands must be
618 -- statically or dynamically tagged.
620 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
621 if N = Right_Opnd (Par)
622 and then Is_Tag_Indeterminate (Left_Opnd (Par))
623 then
624 Abstract_Context_Error;
626 elsif N = Left_Opnd (Par)
627 and then Is_Tag_Indeterminate (Right_Opnd (Par))
628 then
629 Abstract_Context_Error;
630 end if;
632 return;
634 elsif Nkind (Par) = N_Assignment_Statement then
635 return;
637 elsif Nkind (Par) = N_Qualified_Expression
638 or else Nkind (Par) = N_Unchecked_Type_Conversion
639 then
640 Par := Parent (Par);
642 else
643 Abstract_Context_Error;
644 return;
645 end if;
646 end loop;
647 end if;
648 end if;
649 end Check_Dispatching_Context;
651 -- Start of processing for Check_Dispatching_Call
653 begin
654 -- Find a controlling argument, if any
656 if Present (Parameter_Associations (N)) then
657 Subp_Entity := Entity (Name (N));
659 Actual := First_Actual (N);
660 Formal := First_Formal (Subp_Entity);
661 while Present (Actual) loop
662 Control := Find_Controlling_Arg (Actual);
663 exit when Present (Control);
665 -- Check for the case where the actual is a tag-indeterminate call
666 -- whose result type is different than the tagged type associated
667 -- with the containing call, but is an ancestor of the type.
669 if Is_Controlling_Formal (Formal)
670 and then Is_Tag_Indeterminate (Actual)
671 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
672 and then Is_Ancestor (Etype (Actual), Etype (Formal))
673 then
674 Indeterm_Ancestor_Call := True;
675 Indeterm_Ctrl_Type := Etype (Formal);
677 -- If the formal is controlling but the actual is not, the type
678 -- of the actual is statically known, and may be used as the
679 -- controlling tag for some other tag-indeterminate actual.
681 elsif Is_Controlling_Formal (Formal)
682 and then Is_Entity_Name (Actual)
683 and then Is_Tagged_Type (Etype (Actual))
684 then
685 Static_Tag := Actual;
686 end if;
688 Next_Actual (Actual);
689 Next_Formal (Formal);
690 end loop;
692 -- If the call doesn't have a controlling actual but does have an
693 -- indeterminate actual that requires dispatching treatment, then an
694 -- object is needed that will serve as the controlling argument for
695 -- a dispatching call on the indeterminate actual. This can only
696 -- occur in the unusual situation of a default actual given by
697 -- a tag-indeterminate call and where the type of the call is an
698 -- ancestor of the type associated with a containing call to an
699 -- inherited operation (see AI-239).
701 -- Rather than create an object of the tagged type, which would
702 -- be problematic for various reasons (default initialization,
703 -- discriminants), the tag of the containing call's associated
704 -- tagged type is directly used to control the dispatching.
706 if No (Control)
707 and then Indeterm_Ancestor_Call
708 and then No (Static_Tag)
709 then
710 Control :=
711 Make_Attribute_Reference (Loc,
712 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
713 Attribute_Name => Name_Tag);
715 Analyze (Control);
716 end if;
718 if Present (Control) then
720 -- Verify that no controlling arguments are statically tagged
722 if Debug_Flag_E then
723 Write_Str ("Found Dispatching call");
724 Write_Int (Int (N));
725 Write_Eol;
726 end if;
728 Actual := First_Actual (N);
729 while Present (Actual) loop
730 if Actual /= Control then
732 if not Is_Controlling_Actual (Actual) then
733 null; -- Can be anything
735 elsif Is_Dynamically_Tagged (Actual) then
736 null; -- Valid parameter
738 elsif Is_Tag_Indeterminate (Actual) then
740 -- The tag is inherited from the enclosing call (the node
741 -- we are currently analyzing). Explicitly expand the
742 -- actual, since the previous call to Expand (from
743 -- Resolve_Call) had no way of knowing about the
744 -- required dispatching.
746 Propagate_Tag (Control, Actual);
748 else
749 Error_Msg_N
750 ("controlling argument is not dynamically tagged",
751 Actual);
752 return;
753 end if;
754 end if;
756 Next_Actual (Actual);
757 end loop;
759 -- Mark call as a dispatching call
761 Set_Controlling_Argument (N, Control);
762 Check_Restriction (No_Dispatching_Calls, N);
764 -- The dispatching call may need to be converted into a direct
765 -- call in certain cases.
767 Check_Direct_Call;
769 -- If there is a statically tagged actual and a tag-indeterminate
770 -- call to a function of the ancestor (such as that provided by a
771 -- default), then treat this as a dispatching call and propagate
772 -- the tag to the tag-indeterminate call(s).
774 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
775 Control :=
776 Make_Attribute_Reference (Loc,
777 Prefix =>
778 New_Occurrence_Of (Etype (Static_Tag), Loc),
779 Attribute_Name => Name_Tag);
781 Analyze (Control);
783 Actual := First_Actual (N);
784 Formal := First_Formal (Subp_Entity);
785 while Present (Actual) loop
786 if Is_Tag_Indeterminate (Actual)
787 and then Is_Controlling_Formal (Formal)
788 then
789 Propagate_Tag (Control, Actual);
790 end if;
792 Next_Actual (Actual);
793 Next_Formal (Formal);
794 end loop;
796 Check_Dispatching_Context;
798 else
799 -- The call is not dispatching, so check that there aren't any
800 -- tag-indeterminate abstract calls left.
802 Actual := First_Actual (N);
803 while Present (Actual) loop
804 if Is_Tag_Indeterminate (Actual) then
806 -- Function call case
808 if Nkind (Original_Node (Actual)) = N_Function_Call then
809 Func := Entity (Name (Original_Node (Actual)));
811 -- If the actual is an attribute then it can't be abstract
812 -- (the only current case of a tag-indeterminate attribute
813 -- is the stream Input attribute).
815 elsif
816 Nkind (Original_Node (Actual)) = N_Attribute_Reference
817 then
818 Func := Empty;
820 -- Only other possibility is a qualified expression whose
821 -- constituent expression is itself a call.
823 else
824 Func :=
825 Entity (Name
826 (Original_Node
827 (Expression (Original_Node (Actual)))));
828 end if;
830 if Present (Func) and then Is_Abstract_Subprogram (Func) then
831 Error_Msg_N
832 ("call to abstract function must be dispatching", N);
833 end if;
834 end if;
836 Next_Actual (Actual);
837 end loop;
839 Check_Dispatching_Context;
840 end if;
842 else
843 -- If dispatching on result, the enclosing call, if any, will
844 -- determine the controlling argument. Otherwise this is the
845 -- primitive operation of the root type.
847 Check_Dispatching_Context;
848 end if;
849 end Check_Dispatching_Call;
851 ---------------------------------
852 -- Check_Dispatching_Operation --
853 ---------------------------------
855 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
856 Tagged_Type : Entity_Id;
857 Has_Dispatching_Parent : Boolean := False;
858 Body_Is_Last_Primitive : Boolean := False;
859 Ovr_Subp : Entity_Id := Empty;
861 begin
862 if not Ekind_In (Subp, E_Procedure, E_Function) then
863 return;
864 end if;
866 Set_Is_Dispatching_Operation (Subp, False);
867 Tagged_Type := Find_Dispatching_Type (Subp);
869 -- Ada 2005 (AI-345): Use the corresponding record (if available).
870 -- Required because primitives of concurrent types are attached
871 -- to the corresponding record (not to the concurrent type).
873 if Ada_Version >= Ada_2005
874 and then Present (Tagged_Type)
875 and then Is_Concurrent_Type (Tagged_Type)
876 and then Present (Corresponding_Record_Type (Tagged_Type))
877 then
878 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
879 end if;
881 -- (AI-345): The task body procedure is not a primitive of the tagged
882 -- type
884 if Present (Tagged_Type)
885 and then Is_Concurrent_Record_Type (Tagged_Type)
886 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
887 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
888 and then Subp = Get_Task_Body_Procedure
889 (Corresponding_Concurrent_Type (Tagged_Type))
890 then
891 return;
892 end if;
894 -- If Subp is derived from a dispatching operation then it should
895 -- always be treated as dispatching. In this case various checks
896 -- below will be bypassed. Makes sure that late declarations for
897 -- inherited private subprograms are treated as dispatching, even
898 -- if the associated tagged type is already frozen.
900 Has_Dispatching_Parent :=
901 Present (Alias (Subp))
902 and then Is_Dispatching_Operation (Alias (Subp));
904 if No (Tagged_Type) then
906 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
907 -- with an abstract interface type unless the interface acts as a
908 -- parent type in a derivation. If the interface type is a formal
909 -- type then the operation is not primitive and therefore legal.
911 declare
912 E : Entity_Id;
913 Typ : Entity_Id;
915 begin
916 E := First_Entity (Subp);
917 while Present (E) loop
919 -- For an access parameter, check designated type
921 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
922 Typ := Designated_Type (Etype (E));
923 else
924 Typ := Etype (E);
925 end if;
927 if Comes_From_Source (Subp)
928 and then Is_Interface (Typ)
929 and then not Is_Class_Wide_Type (Typ)
930 and then not Is_Derived_Type (Typ)
931 and then not Is_Generic_Type (Typ)
932 and then not In_Instance
933 then
934 Error_Msg_N ("??declaration of& is too late!", Subp);
935 Error_Msg_NE -- CODEFIX??
936 ("\??spec should appear immediately after declaration "
937 & "of & !", Subp, Typ);
938 exit;
939 end if;
941 Next_Entity (E);
942 end loop;
944 -- In case of functions check also the result type
946 if Ekind (Subp) = E_Function then
947 if Is_Access_Type (Etype (Subp)) then
948 Typ := Designated_Type (Etype (Subp));
949 else
950 Typ := Etype (Subp);
951 end if;
953 -- The following should be better commented, especially since
954 -- we just added several new conditions here ???
956 if Comes_From_Source (Subp)
957 and then Is_Interface (Typ)
958 and then not Is_Class_Wide_Type (Typ)
959 and then not Is_Derived_Type (Typ)
960 and then not Is_Generic_Type (Typ)
961 and then not In_Instance
962 then
963 Error_Msg_N ("??declaration of& is too late!", Subp);
964 Error_Msg_NE
965 ("\??spec should appear immediately after declaration "
966 & "of & !", Subp, Typ);
967 end if;
968 end if;
969 end;
971 return;
973 -- The subprograms build internally after the freezing point (such as
974 -- init procs, interface thunks, type support subprograms, and Offset
975 -- to top functions for accessing interface components in variable
976 -- size tagged types) are not primitives.
978 elsif Is_Frozen (Tagged_Type)
979 and then not Comes_From_Source (Subp)
980 and then not Has_Dispatching_Parent
981 then
982 -- Complete decoration of internally built subprograms that override
983 -- a dispatching primitive. These entities correspond with the
984 -- following cases:
986 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
987 -- to override functions of nonabstract null extensions. These
988 -- primitives were added to the list of primitives of the tagged
989 -- type by Make_Controlling_Function_Wrappers. However, attribute
990 -- Is_Dispatching_Operation must be set to true.
992 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
993 -- primitives.
995 -- 3. Subprograms associated with stream attributes (built by
996 -- New_Stream_Subprogram)
998 if Present (Old_Subp)
999 and then Present (Overridden_Operation (Subp))
1000 and then Is_Dispatching_Operation (Old_Subp)
1001 then
1002 pragma Assert
1003 ((Ekind (Subp) = E_Function
1004 and then Is_Dispatching_Operation (Old_Subp)
1005 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1006 or else
1007 (Ekind (Subp) = E_Procedure
1008 and then Is_Dispatching_Operation (Old_Subp)
1009 and then Present (Alias (Old_Subp))
1010 and then Is_Null_Interface_Primitive
1011 (Ultimate_Alias (Old_Subp)))
1012 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1013 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
1015 Check_Controlling_Formals (Tagged_Type, Subp);
1016 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1017 Set_Is_Dispatching_Operation (Subp);
1018 end if;
1020 return;
1022 -- The operation may be a child unit, whose scope is the defining
1023 -- package, but which is not a primitive operation of the type.
1025 elsif Is_Child_Unit (Subp) then
1026 return;
1028 -- If the subprogram is not defined in a package spec, the only case
1029 -- where it can be a dispatching op is when it overrides an operation
1030 -- before the freezing point of the type.
1032 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1033 or else In_Package_Body (Scope (Subp)))
1034 and then not Has_Dispatching_Parent
1035 then
1036 if not Comes_From_Source (Subp)
1037 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1038 then
1039 null;
1041 -- If the type is already frozen, the overriding is not allowed
1042 -- except when Old_Subp is not a dispatching operation (which can
1043 -- occur when Old_Subp was inherited by an untagged type). However,
1044 -- a body with no previous spec freezes the type *after* its
1045 -- declaration, and therefore is a legal overriding (unless the type
1046 -- has already been frozen). Only the first such body is legal.
1048 elsif Present (Old_Subp)
1049 and then Is_Dispatching_Operation (Old_Subp)
1050 then
1051 if Comes_From_Source (Subp)
1052 and then
1053 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1054 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1055 then
1056 declare
1057 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1058 Decl_Item : Node_Id;
1060 begin
1061 -- ??? The checks here for whether the type has been frozen
1062 -- prior to the new body are not complete. It's not simple
1063 -- to check frozenness at this point since the body has
1064 -- already caused the type to be prematurely frozen in
1065 -- Analyze_Declarations, but we're forced to recheck this
1066 -- here because of the odd rule interpretation that allows
1067 -- the overriding if the type wasn't frozen prior to the
1068 -- body. The freezing action should probably be delayed
1069 -- until after the spec is seen, but that's a tricky
1070 -- change to the delicate freezing code.
1072 -- Look at each declaration following the type up until the
1073 -- new subprogram body. If any of the declarations is a body
1074 -- then the type has been frozen already so the overriding
1075 -- primitive is illegal.
1077 Decl_Item := Next (Parent (Tagged_Type));
1078 while Present (Decl_Item)
1079 and then (Decl_Item /= Subp_Body)
1080 loop
1081 if Comes_From_Source (Decl_Item)
1082 and then (Nkind (Decl_Item) in N_Proper_Body
1083 or else Nkind (Decl_Item) in N_Body_Stub)
1084 then
1085 Error_Msg_N ("overriding of& is too late!", Subp);
1086 Error_Msg_N
1087 ("\spec should appear immediately after the type!",
1088 Subp);
1089 exit;
1090 end if;
1092 Next (Decl_Item);
1093 end loop;
1095 -- If the subprogram doesn't follow in the list of
1096 -- declarations including the type then the type has
1097 -- definitely been frozen already and the body is illegal.
1099 if No (Decl_Item) then
1100 Error_Msg_N ("overriding of& is too late!", Subp);
1101 Error_Msg_N
1102 ("\spec should appear immediately after the type!",
1103 Subp);
1105 elsif Is_Frozen (Subp) then
1107 -- The subprogram body declares a primitive operation.
1108 -- If the subprogram is already frozen, we must update
1109 -- its dispatching information explicitly here. The
1110 -- information is taken from the overridden subprogram.
1111 -- We must also generate a cross-reference entry because
1112 -- references to other primitives were already created
1113 -- when type was frozen.
1115 Body_Is_Last_Primitive := True;
1117 if Present (DTC_Entity (Old_Subp)) then
1118 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1119 Set_DT_Position (Subp, DT_Position (Old_Subp));
1121 if not Restriction_Active (No_Dispatching_Calls) then
1122 if Building_Static_DT (Tagged_Type) then
1124 -- If the static dispatch table has not been
1125 -- built then there is nothing else to do now;
1126 -- otherwise we notify that we cannot build the
1127 -- static dispatch table.
1129 if Has_Dispatch_Table (Tagged_Type) then
1130 Error_Msg_N
1131 ("overriding of& is too late for building "
1132 & " static dispatch tables!", Subp);
1133 Error_Msg_N
1134 ("\spec should appear immediately after "
1135 & "the type!", Subp);
1136 end if;
1138 -- No code required to register primitives in VM
1139 -- targets
1141 elsif VM_Target /= No_VM then
1142 null;
1144 else
1145 Insert_Actions_After (Subp_Body,
1146 Register_Primitive (Sloc (Subp_Body),
1147 Prim => Subp));
1148 end if;
1150 -- Indicate that this is an overriding operation,
1151 -- and replace the overridden entry in the list of
1152 -- primitive operations, which is used for xref
1153 -- generation subsequently.
1155 Generate_Reference (Tagged_Type, Subp, 'P', False);
1156 Override_Dispatching_Operation
1157 (Tagged_Type, Old_Subp, Subp);
1158 end if;
1159 end if;
1160 end if;
1161 end;
1163 else
1164 Error_Msg_N ("overriding of& is too late!", Subp);
1165 Error_Msg_N
1166 ("\subprogram spec should appear immediately after the type!",
1167 Subp);
1168 end if;
1170 -- If the type is not frozen yet and we are not in the overriding
1171 -- case it looks suspiciously like an attempt to define a primitive
1172 -- operation, which requires the declaration to be in a package spec
1173 -- (3.2.3(6)). Only report cases where the type and subprogram are
1174 -- in the same declaration list (by checking the enclosing parent
1175 -- declarations), to avoid spurious warnings on subprograms in
1176 -- instance bodies when the type is declared in the instance spec
1177 -- but hasn't been frozen by the instance body.
1179 elsif not Is_Frozen (Tagged_Type)
1180 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1181 then
1182 Error_Msg_N
1183 ("??not dispatching (must be defined in a package spec)", Subp);
1184 return;
1186 -- When the type is frozen, it is legitimate to define a new
1187 -- non-primitive operation.
1189 else
1190 return;
1191 end if;
1193 -- Now, we are sure that the scope is a package spec. If the subprogram
1194 -- is declared after the freezing point of the type that's an error
1196 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1197 Error_Msg_N ("this primitive operation is declared too late", Subp);
1198 Error_Msg_NE
1199 ("??no primitive operations for& after this line",
1200 Freeze_Node (Tagged_Type),
1201 Tagged_Type);
1202 return;
1203 end if;
1205 Check_Controlling_Formals (Tagged_Type, Subp);
1207 Ovr_Subp := Old_Subp;
1209 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1210 -- overridden by Subp. This only applies to source subprograms, and
1211 -- their declaration must carry an explicit overriding indicator.
1213 if No (Ovr_Subp)
1214 and then Ada_Version >= Ada_2012
1215 and then Comes_From_Source (Subp)
1216 and then
1217 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1218 then
1219 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1221 -- Verify that the proper overriding indicator has been supplied.
1223 if Present (Ovr_Subp)
1224 and then
1225 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1226 then
1227 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1228 end if;
1229 end if;
1231 -- Now it should be a correct primitive operation, put it in the list
1233 if Present (Ovr_Subp) then
1235 -- If the type has interfaces we complete this check after we set
1236 -- attribute Is_Dispatching_Operation.
1238 Check_Subtype_Conformant (Subp, Ovr_Subp);
1240 -- A primitive operation with the name of a primitive controlled
1241 -- operation does not override a non-visible overriding controlled
1242 -- operation, i.e. one declared in a private part when the full
1243 -- view of a type is controlled. Conversely, it will override a
1244 -- visible operation that may be declared in a partial view when
1245 -- the full view is controlled.
1247 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1248 and then Is_Controlled (Tagged_Type)
1249 and then not Is_Visibly_Controlled (Tagged_Type)
1250 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1251 then
1252 Set_Overridden_Operation (Subp, Empty);
1254 -- If the subprogram specification carries an overriding
1255 -- indicator, no need for the warning: it is either redundant,
1256 -- or else an error will be reported.
1258 if Nkind (Parent (Subp)) = N_Procedure_Specification
1259 and then
1260 (Must_Override (Parent (Subp))
1261 or else Must_Not_Override (Parent (Subp)))
1262 then
1263 null;
1265 -- Here we need the warning
1267 else
1268 Error_Msg_NE
1269 ("operation does not override inherited&??", Subp, Subp);
1270 end if;
1272 else
1273 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1275 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1276 -- that covers abstract interface subprograms we must register it
1277 -- in all the secondary dispatch tables associated with abstract
1278 -- interfaces. We do this now only if not building static tables,
1279 -- nor when the expander is inactive (we avoid trying to register
1280 -- primitives in semantics-only mode, since the type may not have
1281 -- an associated dispatch table). Otherwise the patch code is
1282 -- emitted after those tables are built, to prevent access before
1283 -- elaboration in gigi.
1285 if Body_Is_Last_Primitive and then Expander_Active then
1286 declare
1287 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1288 Elmt : Elmt_Id;
1289 Prim : Node_Id;
1291 begin
1292 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1293 while Present (Elmt) loop
1294 Prim := Node (Elmt);
1296 -- No code required to register primitives in VM targets
1298 if Present (Alias (Prim))
1299 and then Present (Interface_Alias (Prim))
1300 and then Alias (Prim) = Subp
1301 and then not Building_Static_DT (Tagged_Type)
1302 and then VM_Target = No_VM
1303 then
1304 Insert_Actions_After (Subp_Body,
1305 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1306 end if;
1308 Next_Elmt (Elmt);
1309 end loop;
1311 -- Redisplay the contents of the updated dispatch table
1313 if Debug_Flag_ZZ then
1314 Write_Str ("Late overriding: ");
1315 Write_DT (Tagged_Type);
1316 end if;
1317 end;
1318 end if;
1319 end if;
1321 -- If the tagged type is a concurrent type then we must be compiling
1322 -- with no code generation (we are either compiling a generic unit or
1323 -- compiling under -gnatc mode) because we have previously tested that
1324 -- no serious errors has been reported. In this case we do not add the
1325 -- primitive to the list of primitives of Tagged_Type but we leave the
1326 -- primitive decorated as a dispatching operation to be able to analyze
1327 -- and report errors associated with the Object.Operation notation.
1329 elsif Is_Concurrent_Type (Tagged_Type) then
1330 pragma Assert (not Expander_Active);
1331 null;
1333 -- If no old subprogram, then we add this as a dispatching operation,
1334 -- but we avoid doing this if an error was posted, to prevent annoying
1335 -- cascaded errors.
1337 elsif not Error_Posted (Subp) then
1338 Add_Dispatching_Operation (Tagged_Type, Subp);
1339 end if;
1341 Set_Is_Dispatching_Operation (Subp, True);
1343 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1344 -- subtype conformance against all the interfaces covered by this
1345 -- primitive.
1347 if Present (Ovr_Subp)
1348 and then Has_Interfaces (Tagged_Type)
1349 then
1350 declare
1351 Ifaces_List : Elist_Id;
1352 Iface_Elmt : Elmt_Id;
1353 Iface_Prim_Elmt : Elmt_Id;
1354 Iface_Prim : Entity_Id;
1355 Ret_Typ : Entity_Id;
1357 begin
1358 Collect_Interfaces (Tagged_Type, Ifaces_List);
1360 Iface_Elmt := First_Elmt (Ifaces_List);
1361 while Present (Iface_Elmt) loop
1362 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1363 Iface_Prim_Elmt :=
1364 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1365 while Present (Iface_Prim_Elmt) loop
1366 Iface_Prim := Node (Iface_Prim_Elmt);
1368 if Is_Interface_Conformant
1369 (Tagged_Type, Iface_Prim, Subp)
1370 then
1371 -- Handle procedures, functions whose return type
1372 -- matches, or functions not returning interfaces
1374 if Ekind (Subp) = E_Procedure
1375 or else Etype (Iface_Prim) = Etype (Subp)
1376 or else not Is_Interface (Etype (Iface_Prim))
1377 then
1378 Check_Subtype_Conformant
1379 (New_Id => Subp,
1380 Old_Id => Iface_Prim,
1381 Err_Loc => Subp,
1382 Skip_Controlling_Formals => True);
1384 -- Handle functions returning interfaces
1386 elsif Implements_Interface
1387 (Etype (Subp), Etype (Iface_Prim))
1388 then
1389 -- Temporarily force both entities to return the
1390 -- same type. Required because Subtype_Conformant
1391 -- does not handle this case.
1393 Ret_Typ := Etype (Iface_Prim);
1394 Set_Etype (Iface_Prim, Etype (Subp));
1396 Check_Subtype_Conformant
1397 (New_Id => Subp,
1398 Old_Id => Iface_Prim,
1399 Err_Loc => Subp,
1400 Skip_Controlling_Formals => True);
1402 Set_Etype (Iface_Prim, Ret_Typ);
1403 end if;
1404 end if;
1406 Next_Elmt (Iface_Prim_Elmt);
1407 end loop;
1408 end if;
1410 Next_Elmt (Iface_Elmt);
1411 end loop;
1412 end;
1413 end if;
1415 if not Body_Is_Last_Primitive then
1416 Set_DT_Position (Subp, No_Uint);
1418 elsif Has_Controlled_Component (Tagged_Type)
1419 and then Nam_In (Chars (Subp), Name_Initialize,
1420 Name_Adjust,
1421 Name_Finalize,
1422 Name_Finalize_Address)
1423 then
1424 declare
1425 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1426 Decl : Node_Id;
1427 Old_P : Entity_Id;
1428 Old_Bod : Node_Id;
1429 Old_Spec : Entity_Id;
1431 C_Names : constant array (1 .. 4) of Name_Id :=
1432 (Name_Initialize,
1433 Name_Adjust,
1434 Name_Finalize,
1435 Name_Finalize_Address);
1437 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1438 (TSS_Deep_Initialize,
1439 TSS_Deep_Adjust,
1440 TSS_Deep_Finalize,
1441 TSS_Finalize_Address);
1443 begin
1444 -- Remove previous controlled function which was constructed and
1445 -- analyzed when the type was frozen. This requires removing the
1446 -- body of the redefined primitive, as well as its specification
1447 -- if needed (there is no spec created for Deep_Initialize, see
1448 -- exp_ch3.adb). We must also dismantle the exception information
1449 -- that may have been generated for it when front end zero-cost
1450 -- tables are enabled.
1452 for J in D_Names'Range loop
1453 Old_P := TSS (Tagged_Type, D_Names (J));
1455 if Present (Old_P)
1456 and then Chars (Subp) = C_Names (J)
1457 then
1458 Old_Bod := Unit_Declaration_Node (Old_P);
1459 Remove (Old_Bod);
1460 Set_Is_Eliminated (Old_P);
1461 Set_Scope (Old_P, Scope (Current_Scope));
1463 if Nkind (Old_Bod) = N_Subprogram_Body
1464 and then Present (Corresponding_Spec (Old_Bod))
1465 then
1466 Old_Spec := Corresponding_Spec (Old_Bod);
1467 Set_Has_Completion (Old_Spec, False);
1468 end if;
1469 end if;
1470 end loop;
1472 Build_Late_Proc (Tagged_Type, Chars (Subp));
1474 -- The new operation is added to the actions of the freeze node
1475 -- for the type, but this node has already been analyzed, so we
1476 -- must retrieve and analyze explicitly the new body.
1478 if Present (F_Node)
1479 and then Present (Actions (F_Node))
1480 then
1481 Decl := Last (Actions (F_Node));
1482 Analyze (Decl);
1483 end if;
1484 end;
1485 end if;
1486 end Check_Dispatching_Operation;
1488 ------------------------------------------
1489 -- Check_Operation_From_Incomplete_Type --
1490 ------------------------------------------
1492 procedure Check_Operation_From_Incomplete_Type
1493 (Subp : Entity_Id;
1494 Typ : Entity_Id)
1496 Full : constant Entity_Id := Full_View (Typ);
1497 Parent_Typ : constant Entity_Id := Etype (Full);
1498 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1499 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1500 Op1, Op2 : Elmt_Id;
1501 Prev : Elmt_Id := No_Elmt;
1503 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1504 -- Check that Subp has profile of an operation derived from Parent_Subp.
1505 -- Subp must have a parameter or result type that is Typ or an access
1506 -- parameter or access result type that designates Typ.
1508 ------------------
1509 -- Derives_From --
1510 ------------------
1512 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1513 F1, F2 : Entity_Id;
1515 begin
1516 if Chars (Parent_Subp) /= Chars (Subp) then
1517 return False;
1518 end if;
1520 -- Check that the type of controlling formals is derived from the
1521 -- parent subprogram's controlling formal type (or designated type
1522 -- if the formal type is an anonymous access type).
1524 F1 := First_Formal (Parent_Subp);
1525 F2 := First_Formal (Subp);
1526 while Present (F1) and then Present (F2) loop
1527 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1528 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1529 return False;
1530 elsif Designated_Type (Etype (F1)) = Parent_Typ
1531 and then Designated_Type (Etype (F2)) /= Full
1532 then
1533 return False;
1534 end if;
1536 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1537 return False;
1539 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1540 return False;
1541 end if;
1543 Next_Formal (F1);
1544 Next_Formal (F2);
1545 end loop;
1547 -- Check that a controlling result type is derived from the parent
1548 -- subprogram's result type (or designated type if the result type
1549 -- is an anonymous access type).
1551 if Ekind (Parent_Subp) = E_Function then
1552 if Ekind (Subp) /= E_Function then
1553 return False;
1555 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1556 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1557 return False;
1559 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1560 and then Designated_Type (Etype (Subp)) /= Full
1561 then
1562 return False;
1563 end if;
1565 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1566 return False;
1568 elsif Etype (Parent_Subp) = Parent_Typ
1569 and then Etype (Subp) /= Full
1570 then
1571 return False;
1572 end if;
1574 elsif Ekind (Subp) = E_Function then
1575 return False;
1576 end if;
1578 return No (F1) and then No (F2);
1579 end Derives_From;
1581 -- Start of processing for Check_Operation_From_Incomplete_Type
1583 begin
1584 -- The operation may override an inherited one, or may be a new one
1585 -- altogether. The inherited operation will have been hidden by the
1586 -- current one at the point of the type derivation, so it does not
1587 -- appear in the list of primitive operations of the type. We have to
1588 -- find the proper place of insertion in the list of primitive opera-
1589 -- tions by iterating over the list for the parent type.
1591 Op1 := First_Elmt (Old_Prim);
1592 Op2 := First_Elmt (New_Prim);
1593 while Present (Op1) and then Present (Op2) loop
1594 if Derives_From (Node (Op1)) then
1595 if No (Prev) then
1597 -- Avoid adding it to the list of primitives if already there
1599 if Node (Op2) /= Subp then
1600 Prepend_Elmt (Subp, New_Prim);
1601 end if;
1603 else
1604 Insert_Elmt_After (Subp, Prev);
1605 end if;
1607 return;
1608 end if;
1610 Prev := Op2;
1611 Next_Elmt (Op1);
1612 Next_Elmt (Op2);
1613 end loop;
1615 -- Operation is a new primitive
1617 Append_Elmt (Subp, New_Prim);
1618 end Check_Operation_From_Incomplete_Type;
1620 ---------------------------------------
1621 -- Check_Operation_From_Private_View --
1622 ---------------------------------------
1624 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1625 Tagged_Type : Entity_Id;
1627 begin
1628 if Is_Dispatching_Operation (Alias (Subp)) then
1629 Set_Scope (Subp, Current_Scope);
1630 Tagged_Type := Find_Dispatching_Type (Subp);
1632 -- Add Old_Subp to primitive operations if not already present
1634 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1635 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1637 -- If Old_Subp isn't already marked as dispatching then this is
1638 -- the case of an operation of an untagged private type fulfilled
1639 -- by a tagged type that overrides an inherited dispatching
1640 -- operation, so we set the necessary dispatching attributes here.
1642 if not Is_Dispatching_Operation (Old_Subp) then
1644 -- If the untagged type has no discriminants, and the full
1645 -- view is constrained, there will be a spurious mismatch of
1646 -- subtypes on the controlling arguments, because the tagged
1647 -- type is the internal base type introduced in the derivation.
1648 -- Use the original type to verify conformance, rather than the
1649 -- base type.
1651 if not Comes_From_Source (Tagged_Type)
1652 and then Has_Discriminants (Tagged_Type)
1653 then
1654 declare
1655 Formal : Entity_Id;
1657 begin
1658 Formal := First_Formal (Old_Subp);
1659 while Present (Formal) loop
1660 if Tagged_Type = Base_Type (Etype (Formal)) then
1661 Tagged_Type := Etype (Formal);
1662 end if;
1664 Next_Formal (Formal);
1665 end loop;
1666 end;
1668 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1669 Tagged_Type := Etype (Old_Subp);
1670 end if;
1671 end if;
1673 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1674 Set_Is_Dispatching_Operation (Old_Subp, True);
1675 Set_DT_Position (Old_Subp, No_Uint);
1676 end if;
1678 -- If the old subprogram is an explicit renaming of some other
1679 -- entity, it is not overridden by the inherited subprogram.
1680 -- Otherwise, update its alias and other attributes.
1682 if Present (Alias (Old_Subp))
1683 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1684 N_Subprogram_Renaming_Declaration
1685 then
1686 Set_Alias (Old_Subp, Alias (Subp));
1688 -- The derived subprogram should inherit the abstractness of
1689 -- the parent subprogram (except in the case of a function
1690 -- returning the type). This sets the abstractness properly
1691 -- for cases where a private extension may have inherited an
1692 -- abstract operation, but the full type is derived from a
1693 -- descendant type and inherits a nonabstract version.
1695 if Etype (Subp) /= Tagged_Type then
1696 Set_Is_Abstract_Subprogram
1697 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1698 end if;
1699 end if;
1700 end if;
1701 end if;
1702 end Check_Operation_From_Private_View;
1704 --------------------------
1705 -- Find_Controlling_Arg --
1706 --------------------------
1708 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1709 Orig_Node : constant Node_Id := Original_Node (N);
1710 Typ : Entity_Id;
1712 begin
1713 if Nkind (Orig_Node) = N_Qualified_Expression then
1714 return Find_Controlling_Arg (Expression (Orig_Node));
1715 end if;
1717 -- Dispatching on result case. If expansion is disabled, the node still
1718 -- has the structure of a function call. However, if the function name
1719 -- is an operator and the call was given in infix form, the original
1720 -- node has no controlling result and we must examine the current node.
1722 if Nkind (N) = N_Function_Call
1723 and then Present (Controlling_Argument (N))
1724 and then Has_Controlling_Result (Entity (Name (N)))
1725 then
1726 return Controlling_Argument (N);
1728 -- If expansion is enabled, the call may have been transformed into
1729 -- an indirect call, and we need to recover the original node.
1731 elsif Nkind (Orig_Node) = N_Function_Call
1732 and then Present (Controlling_Argument (Orig_Node))
1733 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1734 then
1735 return Controlling_Argument (Orig_Node);
1737 -- Type conversions are dynamically tagged if the target type, or its
1738 -- designated type, are classwide. An interface conversion expands into
1739 -- a dereference, so test must be performed on the original node.
1741 elsif Nkind (Orig_Node) = N_Type_Conversion
1742 and then Nkind (N) = N_Explicit_Dereference
1743 and then Is_Controlling_Actual (N)
1744 then
1745 declare
1746 Target_Type : constant Entity_Id :=
1747 Entity (Subtype_Mark (Orig_Node));
1749 begin
1750 if Is_Class_Wide_Type (Target_Type) then
1751 return N;
1753 elsif Is_Access_Type (Target_Type)
1754 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1755 then
1756 return N;
1758 else
1759 return Empty;
1760 end if;
1761 end;
1763 -- Normal case
1765 elsif Is_Controlling_Actual (N)
1766 or else
1767 (Nkind (Parent (N)) = N_Qualified_Expression
1768 and then Is_Controlling_Actual (Parent (N)))
1769 then
1770 Typ := Etype (N);
1772 if Is_Access_Type (Typ) then
1774 -- In the case of an Access attribute, use the type of the prefix,
1775 -- since in the case of an actual for an access parameter, the
1776 -- attribute's type may be of a specific designated type, even
1777 -- though the prefix type is class-wide.
1779 if Nkind (N) = N_Attribute_Reference then
1780 Typ := Etype (Prefix (N));
1782 -- An allocator is dispatching if the type of qualified expression
1783 -- is class_wide, in which case this is the controlling type.
1785 elsif Nkind (Orig_Node) = N_Allocator
1786 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1787 then
1788 Typ := Etype (Expression (Orig_Node));
1789 else
1790 Typ := Designated_Type (Typ);
1791 end if;
1792 end if;
1794 if Is_Class_Wide_Type (Typ)
1795 or else
1796 (Nkind (Parent (N)) = N_Qualified_Expression
1797 and then Is_Access_Type (Etype (N))
1798 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1799 then
1800 return N;
1801 end if;
1802 end if;
1804 return Empty;
1805 end Find_Controlling_Arg;
1807 ---------------------------
1808 -- Find_Dispatching_Type --
1809 ---------------------------
1811 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1812 A_Formal : Entity_Id;
1813 Formal : Entity_Id;
1814 Ctrl_Type : Entity_Id;
1816 begin
1817 if Ekind_In (Subp, E_Function, E_Procedure)
1818 and then Present (DTC_Entity (Subp))
1819 then
1820 return Scope (DTC_Entity (Subp));
1822 -- For subprograms internally generated by derivations of tagged types
1823 -- use the alias subprogram as a reference to locate the dispatching
1824 -- type of Subp.
1826 elsif not Comes_From_Source (Subp)
1827 and then Present (Alias (Subp))
1828 and then Is_Dispatching_Operation (Alias (Subp))
1829 then
1830 if Ekind (Alias (Subp)) = E_Function
1831 and then Has_Controlling_Result (Alias (Subp))
1832 then
1833 return Check_Controlling_Type (Etype (Subp), Subp);
1835 else
1836 Formal := First_Formal (Subp);
1837 A_Formal := First_Formal (Alias (Subp));
1838 while Present (A_Formal) loop
1839 if Is_Controlling_Formal (A_Formal) then
1840 return Check_Controlling_Type (Etype (Formal), Subp);
1841 end if;
1843 Next_Formal (Formal);
1844 Next_Formal (A_Formal);
1845 end loop;
1847 pragma Assert (False);
1848 return Empty;
1849 end if;
1851 -- General case
1853 else
1854 Formal := First_Formal (Subp);
1855 while Present (Formal) loop
1856 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1858 if Present (Ctrl_Type) then
1859 return Ctrl_Type;
1860 end if;
1862 Next_Formal (Formal);
1863 end loop;
1865 -- The subprogram may also be dispatching on result
1867 if Present (Etype (Subp)) then
1868 return Check_Controlling_Type (Etype (Subp), Subp);
1869 end if;
1870 end if;
1872 pragma Assert (not Is_Dispatching_Operation (Subp));
1873 return Empty;
1874 end Find_Dispatching_Type;
1876 --------------------------------------
1877 -- Find_Hidden_Overridden_Primitive --
1878 --------------------------------------
1880 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
1882 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
1883 Elmt : Elmt_Id;
1884 Orig_Prim : Entity_Id;
1885 Prim : Entity_Id;
1886 Vis_List : Elist_Id;
1888 begin
1889 -- This Ada 2012 rule applies only for type extensions or private
1890 -- extensions, where the parent type is not in a parent unit, and
1891 -- where an operation is never declared but still inherited.
1893 if No (Tag_Typ)
1894 or else not Is_Record_Type (Tag_Typ)
1895 or else Etype (Tag_Typ) = Tag_Typ
1896 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
1897 then
1898 return Empty;
1899 end if;
1901 -- Collect the list of visible ancestor of the tagged type
1903 Vis_List := Visible_Ancestors (Tag_Typ);
1905 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
1906 while Present (Elmt) loop
1907 Prim := Node (Elmt);
1909 -- Find an inherited hidden dispatching primitive with the name of S
1910 -- and a type-conformant profile.
1912 if Present (Alias (Prim))
1913 and then Is_Hidden (Alias (Prim))
1914 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
1915 and then Primitive_Names_Match (S, Prim)
1916 and then Type_Conformant (S, Prim)
1917 then
1918 declare
1919 Vis_Ancestor : Elmt_Id;
1920 Elmt : Elmt_Id;
1922 begin
1923 -- The original corresponding operation of Prim must be an
1924 -- operation of a visible ancestor of the dispatching type S,
1925 -- and the original corresponding operation of S2 must be
1926 -- visible.
1928 Orig_Prim := Original_Corresponding_Operation (Prim);
1930 if Orig_Prim /= Prim
1931 and then Is_Immediately_Visible (Orig_Prim)
1932 then
1933 Vis_Ancestor := First_Elmt (Vis_List);
1934 while Present (Vis_Ancestor) loop
1935 Elmt :=
1936 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
1937 while Present (Elmt) loop
1938 if Node (Elmt) = Orig_Prim then
1939 Set_Overridden_Operation (S, Prim);
1940 Set_Alias (Prim, Orig_Prim);
1941 return Prim;
1942 end if;
1944 Next_Elmt (Elmt);
1945 end loop;
1947 Next_Elmt (Vis_Ancestor);
1948 end loop;
1949 end if;
1950 end;
1951 end if;
1953 Next_Elmt (Elmt);
1954 end loop;
1956 return Empty;
1957 end Find_Hidden_Overridden_Primitive;
1959 ---------------------------------------
1960 -- Find_Primitive_Covering_Interface --
1961 ---------------------------------------
1963 function Find_Primitive_Covering_Interface
1964 (Tagged_Type : Entity_Id;
1965 Iface_Prim : Entity_Id) return Entity_Id
1967 E : Entity_Id;
1968 El : Elmt_Id;
1970 begin
1971 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1972 or else (Present (Alias (Iface_Prim))
1973 and then
1974 Is_Interface
1975 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1977 -- Search in the homonym chain. Done to speed up locating visible
1978 -- entities and required to catch primitives associated with the partial
1979 -- view of private types when processing the corresponding full view.
1981 E := Current_Entity (Iface_Prim);
1982 while Present (E) loop
1983 if Is_Subprogram (E)
1984 and then Is_Dispatching_Operation (E)
1985 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1986 then
1987 return E;
1988 end if;
1990 E := Homonym (E);
1991 end loop;
1993 -- Search in the list of primitives of the type. Required to locate
1994 -- the covering primitive if the covering primitive is not visible
1995 -- (for example, non-visible inherited primitive of private type).
1997 El := First_Elmt (Primitive_Operations (Tagged_Type));
1998 while Present (El) loop
1999 E := Node (El);
2001 -- Keep separate the management of internal entities that link
2002 -- primitives with interface primitives from tagged type primitives.
2004 if No (Interface_Alias (E)) then
2005 if Present (Alias (E)) then
2007 -- This interface primitive has not been covered yet
2009 if Alias (E) = Iface_Prim then
2010 return E;
2012 -- The covering primitive was inherited
2014 elsif Overridden_Operation (Ultimate_Alias (E))
2015 = Iface_Prim
2016 then
2017 return E;
2018 end if;
2019 end if;
2021 -- Check if E covers the interface primitive (includes case in
2022 -- which E is an inherited private primitive).
2024 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2025 return E;
2026 end if;
2028 -- Use the internal entity that links the interface primitive with
2029 -- the covering primitive to locate the entity.
2031 elsif Interface_Alias (E) = Iface_Prim then
2032 return Alias (E);
2033 end if;
2035 Next_Elmt (El);
2036 end loop;
2038 -- Not found
2040 return Empty;
2041 end Find_Primitive_Covering_Interface;
2043 ---------------------------
2044 -- Inherited_Subprograms --
2045 ---------------------------
2047 function Inherited_Subprograms
2048 (S : Entity_Id;
2049 No_Interfaces : Boolean := False;
2050 Interfaces_Only : Boolean := False) return Subprogram_List
2052 Result : Subprogram_List (1 .. 6000);
2053 -- 6000 here is intended to be infinity. We could use an expandable
2054 -- table, but it would be awfully heavy, and there is no way that we
2055 -- could reasonably exceed this value.
2057 N : Int := 0;
2058 -- Number of entries in Result
2060 Parent_Op : Entity_Id;
2061 -- Traverses the Overridden_Operation chain
2063 procedure Store_IS (E : Entity_Id);
2064 -- Stores E in Result if not already stored
2066 --------------
2067 -- Store_IS --
2068 --------------
2070 procedure Store_IS (E : Entity_Id) is
2071 begin
2072 for J in 1 .. N loop
2073 if E = Result (J) then
2074 return;
2075 end if;
2076 end loop;
2078 N := N + 1;
2079 Result (N) := E;
2080 end Store_IS;
2082 -- Start of processing for Inherited_Subprograms
2084 begin
2085 pragma Assert (not (No_Interfaces and Interfaces_Only));
2087 if Present (S) and then Is_Dispatching_Operation (S) then
2089 -- Deal with direct inheritance
2091 if not Interfaces_Only then
2092 Parent_Op := S;
2093 loop
2094 Parent_Op := Overridden_Operation (Parent_Op);
2095 exit when No (Parent_Op)
2096 or else
2097 (No_Interfaces
2098 and then
2099 Is_Interface (Find_Dispatching_Type (Parent_Op)));
2101 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2102 Store_IS (Parent_Op);
2103 end if;
2104 end loop;
2105 end if;
2107 -- Now deal with interfaces
2109 if not No_Interfaces then
2110 declare
2111 Tag_Typ : Entity_Id;
2112 Prim : Entity_Id;
2113 Elmt : Elmt_Id;
2115 begin
2116 Tag_Typ := Find_Dispatching_Type (S);
2118 if Is_Concurrent_Type (Tag_Typ) then
2119 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2120 end if;
2122 -- Search primitive operations of dispatching type
2124 if Present (Tag_Typ)
2125 and then Present (Primitive_Operations (Tag_Typ))
2126 then
2127 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2128 while Present (Elmt) loop
2129 Prim := Node (Elmt);
2131 -- The following test eliminates some odd cases in which
2132 -- Ekind (Prim) is Void, to be investigated further ???
2134 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2135 null;
2137 -- For [generic] subprogram, look at interface alias
2139 elsif Present (Interface_Alias (Prim))
2140 and then Alias (Prim) = S
2141 then
2142 -- We have found a primitive covered by S
2144 Store_IS (Interface_Alias (Prim));
2145 end if;
2147 Next_Elmt (Elmt);
2148 end loop;
2149 end if;
2150 end;
2151 end if;
2152 end if;
2154 return Result (1 .. N);
2155 end Inherited_Subprograms;
2157 ---------------------------
2158 -- Is_Dynamically_Tagged --
2159 ---------------------------
2161 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2162 begin
2163 if Nkind (N) = N_Error then
2164 return False;
2165 else
2166 return Find_Controlling_Arg (N) /= Empty;
2167 end if;
2168 end Is_Dynamically_Tagged;
2170 ---------------------------------
2171 -- Is_Null_Interface_Primitive --
2172 ---------------------------------
2174 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2175 begin
2176 return Comes_From_Source (E)
2177 and then Is_Dispatching_Operation (E)
2178 and then Ekind (E) = E_Procedure
2179 and then Null_Present (Parent (E))
2180 and then Is_Interface (Find_Dispatching_Type (E));
2181 end Is_Null_Interface_Primitive;
2183 -----------------------------------
2184 -- Is_Inherited_Public_Operation --
2185 -----------------------------------
2187 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2188 Prim : constant Entity_Id := Alias (Op);
2189 Scop : constant Entity_Id := Scope (Prim);
2190 Pack_Decl : Node_Id;
2192 begin
2193 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2194 Pack_Decl := Unit_Declaration_Node (Scop);
2195 return Nkind (Pack_Decl) = N_Package_Declaration
2196 and then List_Containing (Unit_Declaration_Node (Prim)) =
2197 Visible_Declarations (Specification (Pack_Decl));
2199 else
2200 return False;
2201 end if;
2202 end Is_Inherited_Public_Operation;
2204 --------------------------
2205 -- Is_Tag_Indeterminate --
2206 --------------------------
2208 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2209 Nam : Entity_Id;
2210 Actual : Node_Id;
2211 Orig_Node : constant Node_Id := Original_Node (N);
2213 begin
2214 if Nkind (Orig_Node) = N_Function_Call
2215 and then Is_Entity_Name (Name (Orig_Node))
2216 then
2217 Nam := Entity (Name (Orig_Node));
2219 if not Has_Controlling_Result (Nam) then
2220 return False;
2222 -- The function may have a controlling result, but if the return type
2223 -- is not visibly tagged, then this is not tag-indeterminate.
2225 elsif Is_Access_Type (Etype (Nam))
2226 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2227 then
2228 return False;
2230 -- An explicit dereference means that the call has already been
2231 -- expanded and there is no tag to propagate.
2233 elsif Nkind (N) = N_Explicit_Dereference then
2234 return False;
2236 -- If there are no actuals, the call is tag-indeterminate
2238 elsif No (Parameter_Associations (Orig_Node)) then
2239 return True;
2241 else
2242 Actual := First_Actual (Orig_Node);
2243 while Present (Actual) loop
2244 if Is_Controlling_Actual (Actual)
2245 and then not Is_Tag_Indeterminate (Actual)
2246 then
2247 -- One operand is dispatching
2249 return False;
2250 end if;
2252 Next_Actual (Actual);
2253 end loop;
2255 return True;
2256 end if;
2258 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2259 return Is_Tag_Indeterminate (Expression (Orig_Node));
2261 -- Case of a call to the Input attribute (possibly rewritten), which is
2262 -- always tag-indeterminate except when its prefix is a Class attribute.
2264 elsif Nkind (Orig_Node) = N_Attribute_Reference
2265 and then
2266 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2267 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2268 then
2269 return True;
2271 -- In Ada 2005, a function that returns an anonymous access type can be
2272 -- dispatching, and the dereference of a call to such a function can
2273 -- also be tag-indeterminate if the call itself is.
2275 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2276 and then Ada_Version >= Ada_2005
2277 then
2278 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2280 else
2281 return False;
2282 end if;
2283 end Is_Tag_Indeterminate;
2285 ------------------------------------
2286 -- Override_Dispatching_Operation --
2287 ------------------------------------
2289 procedure Override_Dispatching_Operation
2290 (Tagged_Type : Entity_Id;
2291 Prev_Op : Entity_Id;
2292 New_Op : Entity_Id;
2293 Is_Wrapper : Boolean := False)
2295 Elmt : Elmt_Id;
2296 Prim : Node_Id;
2298 begin
2299 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2300 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2302 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2303 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2304 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2305 end if;
2307 -- If there is no previous operation to override, the type declaration
2308 -- was malformed, and an error must have been emitted already.
2310 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2311 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2312 Next_Elmt (Elmt);
2313 end loop;
2315 if No (Elmt) then
2316 return;
2317 end if;
2319 -- The location of entities that come from source in the list of
2320 -- primitives of the tagged type must follow their order of occurrence
2321 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2322 -- primitive of an interface that is not implemented by the parents of
2323 -- this tagged type (that is, it is an alias of an interface primitive
2324 -- generated by Derive_Interface_Progenitors), then we must append the
2325 -- new entity at the end of the list of primitives.
2327 if Present (Alias (Prev_Op))
2328 and then Etype (Tagged_Type) /= Tagged_Type
2329 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2330 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2331 Tagged_Type, Use_Full_View => True)
2332 and then not Implements_Interface
2333 (Etype (Tagged_Type),
2334 Find_Dispatching_Type (Alias (Prev_Op)))
2335 then
2336 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2337 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
2339 -- The new primitive replaces the overridden entity. Required to ensure
2340 -- that overriding primitive is assigned the same dispatch table slot.
2342 else
2343 Replace_Elmt (Elmt, New_Op);
2344 end if;
2346 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2348 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2349 -- entities of the overridden primitive to reference New_Op, and
2350 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2351 -- that the new operation is subtype conformant with the interface
2352 -- operations that it implements (for operations inherited from the
2353 -- parent itself, this check is made when building the derived type).
2355 -- Note: This code is executed with internally generated wrappers of
2356 -- functions with controlling result and late overridings.
2358 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2359 while Present (Elmt) loop
2360 Prim := Node (Elmt);
2362 if Prim = New_Op then
2363 null;
2365 -- Note: The check on Is_Subprogram protects the frontend against
2366 -- reading attributes in entities that are not yet fully decorated
2368 elsif Is_Subprogram (Prim)
2369 and then Present (Interface_Alias (Prim))
2370 and then Alias (Prim) = Prev_Op
2371 then
2372 Set_Alias (Prim, New_Op);
2374 -- No further decoration needed yet for internally generated
2375 -- wrappers of controlling functions since (at this stage)
2376 -- they are not yet decorated.
2378 if not Is_Wrapper then
2379 Check_Subtype_Conformant (New_Op, Prim);
2381 Set_Is_Abstract_Subprogram (Prim,
2382 Is_Abstract_Subprogram (New_Op));
2384 -- Ensure that this entity will be expanded to fill the
2385 -- corresponding entry in its dispatch table.
2387 if not Is_Abstract_Subprogram (Prim) then
2388 Set_Has_Delayed_Freeze (Prim);
2389 end if;
2390 end if;
2391 end if;
2393 Next_Elmt (Elmt);
2394 end loop;
2395 end if;
2397 if (not Is_Package_Or_Generic_Package (Current_Scope))
2398 or else not In_Private_Part (Current_Scope)
2399 then
2400 -- Not a private primitive
2402 null;
2404 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2406 -- Make the overriding operation into an alias of the implicit one.
2407 -- In this fashion a call from outside ends up calling the new body
2408 -- even if non-dispatching, and a call from inside calls the over-
2409 -- riding operation because it hides the implicit one. To indicate
2410 -- that the body of Prev_Op is never called, set its dispatch table
2411 -- entity to Empty. If the overridden operation has a dispatching
2412 -- result, so does the overriding one.
2414 Set_Alias (Prev_Op, New_Op);
2415 Set_DTC_Entity (Prev_Op, Empty);
2416 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2417 return;
2418 end if;
2419 end Override_Dispatching_Operation;
2421 -------------------
2422 -- Propagate_Tag --
2423 -------------------
2425 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2426 Call_Node : Node_Id;
2427 Arg : Node_Id;
2429 begin
2430 if Nkind (Actual) = N_Function_Call then
2431 Call_Node := Actual;
2433 elsif Nkind (Actual) = N_Identifier
2434 and then Nkind (Original_Node (Actual)) = N_Function_Call
2435 then
2436 -- Call rewritten as object declaration when stack-checking is
2437 -- enabled. Propagate tag to expression in declaration, which is
2438 -- original call.
2440 Call_Node := Expression (Parent (Entity (Actual)));
2442 -- Ada 2005: If this is a dereference of a call to a function with a
2443 -- dispatching access-result, the tag is propagated when the dereference
2444 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2446 elsif Nkind (Actual) = N_Explicit_Dereference
2447 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2448 then
2449 return;
2451 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2452 -- and in that case we can simply return.
2454 elsif Nkind (Actual) = N_Attribute_Reference then
2455 pragma Assert (Attribute_Name (Actual) = Name_Input);
2457 return;
2459 -- Only other possibilities are parenthesized or qualified expression,
2460 -- or an expander-generated unchecked conversion of a function call to
2461 -- a stream Input attribute.
2463 else
2464 Call_Node := Expression (Actual);
2465 end if;
2467 -- No action needed if the call has been already expanded
2469 if Is_Expanded_Dispatching_Call (Call_Node) then
2470 return;
2471 end if;
2473 -- Do not set the Controlling_Argument if already set. This happens in
2474 -- the special case of _Input (see Exp_Attr, case Input).
2476 if No (Controlling_Argument (Call_Node)) then
2477 Set_Controlling_Argument (Call_Node, Control);
2478 end if;
2480 Arg := First_Actual (Call_Node);
2481 while Present (Arg) loop
2482 if Is_Tag_Indeterminate (Arg) then
2483 Propagate_Tag (Control, Arg);
2484 end if;
2486 Next_Actual (Arg);
2487 end loop;
2489 -- Expansion of dispatching calls is suppressed when VM_Target, because
2490 -- the VM back-ends directly handle the generation of dispatching calls
2491 -- and would have to undo any expansion to an indirect call.
2493 if Tagged_Type_Expansion then
2494 declare
2495 Call_Typ : constant Entity_Id := Etype (Call_Node);
2497 begin
2498 Expand_Dispatching_Call (Call_Node);
2500 -- If the controlling argument is an interface type and the type
2501 -- of Call_Node differs then we must add an implicit conversion to
2502 -- force displacement of the pointer to the object to reference
2503 -- the secondary dispatch table of the interface.
2505 if Is_Interface (Etype (Control))
2506 and then Etype (Control) /= Call_Typ
2507 then
2508 -- Cannot use Convert_To because the previous call to
2509 -- Expand_Dispatching_Call leaves decorated the Call_Node
2510 -- with the type of Control.
2512 Rewrite (Call_Node,
2513 Make_Type_Conversion (Sloc (Call_Node),
2514 Subtype_Mark =>
2515 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2516 Expression => Relocate_Node (Call_Node)));
2517 Set_Etype (Call_Node, Etype (Control));
2518 Set_Analyzed (Call_Node);
2520 Expand_Interface_Conversion (Call_Node);
2521 end if;
2522 end;
2524 -- Expansion of a dispatching call results in an indirect call, which in
2525 -- turn causes current values to be killed (see Resolve_Call), so on VM
2526 -- targets we do the call here to ensure consistent warnings between VM
2527 -- and non-VM targets.
2529 else
2530 Kill_Current_Values;
2531 end if;
2532 end Propagate_Tag;
2534 end Sem_Disp;