2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_disp.adb
blobd61976e7cbe458d17c6843b1abbeb4282ca2a788
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-2015, 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 Has_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 elsif Ekind (Current_Scope) = E_Function
566 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
567 N_Generic_Subprogram_Declaration
568 then
569 null;
571 else
572 -- We need to determine whether the context of the call
573 -- provides a tag to make the call dispatching. This requires
574 -- the call to be the actual in an enclosing call, and that
575 -- actual must be controlling. If the call is an operand of
576 -- equality, the other operand must not ve abstract.
578 if not Is_Tagged_Type (Typ)
579 and then not
580 (Ekind (Typ) = E_Anonymous_Access_Type
581 and then Is_Tagged_Type (Designated_Type (Typ)))
582 then
583 Abstract_Context_Error;
584 return;
585 end if;
587 Par := Parent (N);
589 if Nkind (Par) = N_Parameter_Association then
590 Par := Parent (Par);
591 end if;
593 while Present (Par) loop
594 if Nkind_In (Par, N_Function_Call,
595 N_Procedure_Call_Statement)
596 and then Is_Entity_Name (Name (Par))
597 then
598 declare
599 A : Node_Id;
600 F : Entity_Id;
602 begin
603 -- Find formal for which call is the actual.
605 F := First_Formal (Entity (Name (Par)));
606 A := First_Actual (Par);
607 while Present (F) loop
608 if Is_Controlling_Formal (F)
609 and then (N = A or else Parent (N) = A)
610 then
611 return;
612 end if;
614 Next_Formal (F);
615 Next_Actual (A);
616 end loop;
618 Error_Msg_N
619 ("call to abstract function must be dispatching", N);
620 return;
621 end;
623 -- For equalitiy operators, one of the operands must be
624 -- statically or dynamically tagged.
626 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
627 if N = Right_Opnd (Par)
628 and then Is_Tag_Indeterminate (Left_Opnd (Par))
629 then
630 Abstract_Context_Error;
632 elsif N = Left_Opnd (Par)
633 and then Is_Tag_Indeterminate (Right_Opnd (Par))
634 then
635 Abstract_Context_Error;
636 end if;
638 return;
640 elsif Nkind (Par) = N_Assignment_Statement then
641 return;
643 elsif Nkind (Par) = N_Qualified_Expression
644 or else Nkind (Par) = N_Unchecked_Type_Conversion
645 then
646 Par := Parent (Par);
648 else
649 Abstract_Context_Error;
650 return;
651 end if;
652 end loop;
653 end if;
654 end if;
655 end Check_Dispatching_Context;
657 -- Start of processing for Check_Dispatching_Call
659 begin
660 -- Find a controlling argument, if any
662 if Present (Parameter_Associations (N)) then
663 Subp_Entity := Entity (Name (N));
665 Actual := First_Actual (N);
666 Formal := First_Formal (Subp_Entity);
667 while Present (Actual) loop
668 Control := Find_Controlling_Arg (Actual);
669 exit when Present (Control);
671 -- Check for the case where the actual is a tag-indeterminate call
672 -- whose result type is different than the tagged type associated
673 -- with the containing call, but is an ancestor of the type.
675 if Is_Controlling_Formal (Formal)
676 and then Is_Tag_Indeterminate (Actual)
677 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
678 and then Is_Ancestor (Etype (Actual), Etype (Formal))
679 then
680 Indeterm_Ancestor_Call := True;
681 Indeterm_Ctrl_Type := Etype (Formal);
683 -- If the formal is controlling but the actual is not, the type
684 -- of the actual is statically known, and may be used as the
685 -- controlling tag for some other tag-indeterminate actual.
687 elsif Is_Controlling_Formal (Formal)
688 and then Is_Entity_Name (Actual)
689 and then Is_Tagged_Type (Etype (Actual))
690 then
691 Static_Tag := Actual;
692 end if;
694 Next_Actual (Actual);
695 Next_Formal (Formal);
696 end loop;
698 -- If the call doesn't have a controlling actual but does have an
699 -- indeterminate actual that requires dispatching treatment, then an
700 -- object is needed that will serve as the controlling argument for
701 -- a dispatching call on the indeterminate actual. This can only
702 -- occur in the unusual situation of a default actual given by
703 -- a tag-indeterminate call and where the type of the call is an
704 -- ancestor of the type associated with a containing call to an
705 -- inherited operation (see AI-239).
707 -- Rather than create an object of the tagged type, which would
708 -- be problematic for various reasons (default initialization,
709 -- discriminants), the tag of the containing call's associated
710 -- tagged type is directly used to control the dispatching.
712 if No (Control)
713 and then Indeterm_Ancestor_Call
714 and then No (Static_Tag)
715 then
716 Control :=
717 Make_Attribute_Reference (Loc,
718 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
719 Attribute_Name => Name_Tag);
721 Analyze (Control);
722 end if;
724 if Present (Control) then
726 -- Verify that no controlling arguments are statically tagged
728 if Debug_Flag_E then
729 Write_Str ("Found Dispatching call");
730 Write_Int (Int (N));
731 Write_Eol;
732 end if;
734 Actual := First_Actual (N);
735 while Present (Actual) loop
736 if Actual /= Control then
738 if not Is_Controlling_Actual (Actual) then
739 null; -- Can be anything
741 elsif Is_Dynamically_Tagged (Actual) then
742 null; -- Valid parameter
744 elsif Is_Tag_Indeterminate (Actual) then
746 -- The tag is inherited from the enclosing call (the node
747 -- we are currently analyzing). Explicitly expand the
748 -- actual, since the previous call to Expand (from
749 -- Resolve_Call) had no way of knowing about the
750 -- required dispatching.
752 Propagate_Tag (Control, Actual);
754 else
755 Error_Msg_N
756 ("controlling argument is not dynamically tagged",
757 Actual);
758 return;
759 end if;
760 end if;
762 Next_Actual (Actual);
763 end loop;
765 -- Mark call as a dispatching call
767 Set_Controlling_Argument (N, Control);
768 Check_Restriction (No_Dispatching_Calls, N);
770 -- The dispatching call may need to be converted into a direct
771 -- call in certain cases.
773 Check_Direct_Call;
775 -- If there is a statically tagged actual and a tag-indeterminate
776 -- call to a function of the ancestor (such as that provided by a
777 -- default), then treat this as a dispatching call and propagate
778 -- the tag to the tag-indeterminate call(s).
780 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
781 Control :=
782 Make_Attribute_Reference (Loc,
783 Prefix =>
784 New_Occurrence_Of (Etype (Static_Tag), Loc),
785 Attribute_Name => Name_Tag);
787 Analyze (Control);
789 Actual := First_Actual (N);
790 Formal := First_Formal (Subp_Entity);
791 while Present (Actual) loop
792 if Is_Tag_Indeterminate (Actual)
793 and then Is_Controlling_Formal (Formal)
794 then
795 Propagate_Tag (Control, Actual);
796 end if;
798 Next_Actual (Actual);
799 Next_Formal (Formal);
800 end loop;
802 Check_Dispatching_Context;
804 else
805 -- The call is not dispatching, so check that there aren't any
806 -- tag-indeterminate abstract calls left.
808 Actual := First_Actual (N);
809 while Present (Actual) loop
810 if Is_Tag_Indeterminate (Actual) then
812 -- Function call case
814 if Nkind (Original_Node (Actual)) = N_Function_Call then
815 Func := Entity (Name (Original_Node (Actual)));
817 -- If the actual is an attribute then it can't be abstract
818 -- (the only current case of a tag-indeterminate attribute
819 -- is the stream Input attribute).
821 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
822 then
823 Func := Empty;
825 -- Ditto if it is an explicit dereference.
827 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
828 then
829 Func := Empty;
831 -- Only other possibility is a qualified expression whose
832 -- constituent expression is itself a call.
834 else
835 Func :=
836 Entity (Name (Original_Node
837 (Expression (Original_Node (Actual)))));
838 end if;
840 if Present (Func) and then Is_Abstract_Subprogram (Func) then
841 Error_Msg_N
842 ("call to abstract function must be dispatching", N);
843 end if;
844 end if;
846 Next_Actual (Actual);
847 end loop;
849 Check_Dispatching_Context;
850 end if;
852 else
853 -- If dispatching on result, the enclosing call, if any, will
854 -- determine the controlling argument. Otherwise this is the
855 -- primitive operation of the root type.
857 Check_Dispatching_Context;
858 end if;
859 end Check_Dispatching_Call;
861 ---------------------------------
862 -- Check_Dispatching_Operation --
863 ---------------------------------
865 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
866 Tagged_Type : Entity_Id;
867 Has_Dispatching_Parent : Boolean := False;
868 Body_Is_Last_Primitive : Boolean := False;
869 Ovr_Subp : Entity_Id := Empty;
871 begin
872 if not Ekind_In (Subp, E_Procedure, E_Function) then
873 return;
874 end if;
876 Set_Is_Dispatching_Operation (Subp, False);
877 Tagged_Type := Find_Dispatching_Type (Subp);
879 -- Ada 2005 (AI-345): Use the corresponding record (if available).
880 -- Required because primitives of concurrent types are attached
881 -- to the corresponding record (not to the concurrent type).
883 if Ada_Version >= Ada_2005
884 and then Present (Tagged_Type)
885 and then Is_Concurrent_Type (Tagged_Type)
886 and then Present (Corresponding_Record_Type (Tagged_Type))
887 then
888 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
889 end if;
891 -- (AI-345): The task body procedure is not a primitive of the tagged
892 -- type
894 if Present (Tagged_Type)
895 and then Is_Concurrent_Record_Type (Tagged_Type)
896 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
897 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
898 and then Subp = Get_Task_Body_Procedure
899 (Corresponding_Concurrent_Type (Tagged_Type))
900 then
901 return;
902 end if;
904 -- If Subp is derived from a dispatching operation then it should
905 -- always be treated as dispatching. In this case various checks
906 -- below will be bypassed. Makes sure that late declarations for
907 -- inherited private subprograms are treated as dispatching, even
908 -- if the associated tagged type is already frozen.
910 Has_Dispatching_Parent :=
911 Present (Alias (Subp))
912 and then Is_Dispatching_Operation (Alias (Subp));
914 if No (Tagged_Type) then
916 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
917 -- with an abstract interface type unless the interface acts as a
918 -- parent type in a derivation. If the interface type is a formal
919 -- type then the operation is not primitive and therefore legal.
921 declare
922 E : Entity_Id;
923 Typ : Entity_Id;
925 begin
926 E := First_Entity (Subp);
927 while Present (E) loop
929 -- For an access parameter, check designated type
931 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
932 Typ := Designated_Type (Etype (E));
933 else
934 Typ := Etype (E);
935 end if;
937 if Comes_From_Source (Subp)
938 and then Is_Interface (Typ)
939 and then not Is_Class_Wide_Type (Typ)
940 and then not Is_Derived_Type (Typ)
941 and then not Is_Generic_Type (Typ)
942 and then not In_Instance
943 then
944 Error_Msg_N ("??declaration of& is too late!", Subp);
945 Error_Msg_NE -- CODEFIX??
946 ("\??spec should appear immediately after declaration "
947 & "of & !", Subp, Typ);
948 exit;
949 end if;
951 Next_Entity (E);
952 end loop;
954 -- In case of functions check also the result type
956 if Ekind (Subp) = E_Function then
957 if Is_Access_Type (Etype (Subp)) then
958 Typ := Designated_Type (Etype (Subp));
959 else
960 Typ := Etype (Subp);
961 end if;
963 -- The following should be better commented, especially since
964 -- we just added several new conditions here ???
966 if Comes_From_Source (Subp)
967 and then Is_Interface (Typ)
968 and then not Is_Class_Wide_Type (Typ)
969 and then not Is_Derived_Type (Typ)
970 and then not Is_Generic_Type (Typ)
971 and then not In_Instance
972 then
973 Error_Msg_N ("??declaration of& is too late!", Subp);
974 Error_Msg_NE
975 ("\??spec should appear immediately after declaration "
976 & "of & !", Subp, Typ);
977 end if;
978 end if;
979 end;
981 return;
983 -- The subprograms build internally after the freezing point (such as
984 -- init procs, interface thunks, type support subprograms, and Offset
985 -- to top functions for accessing interface components in variable
986 -- size tagged types) are not primitives.
988 elsif Is_Frozen (Tagged_Type)
989 and then not Comes_From_Source (Subp)
990 and then not Has_Dispatching_Parent
991 then
992 -- Complete decoration of internally built subprograms that override
993 -- a dispatching primitive. These entities correspond with the
994 -- following cases:
996 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
997 -- to override functions of nonabstract null extensions. These
998 -- primitives were added to the list of primitives of the tagged
999 -- type by Make_Controlling_Function_Wrappers. However, attribute
1000 -- Is_Dispatching_Operation must be set to true.
1002 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1003 -- primitives.
1005 -- 3. Subprograms associated with stream attributes (built by
1006 -- New_Stream_Subprogram)
1008 if Present (Old_Subp)
1009 and then Present (Overridden_Operation (Subp))
1010 and then Is_Dispatching_Operation (Old_Subp)
1011 then
1012 pragma Assert
1013 ((Ekind (Subp) = E_Function
1014 and then Is_Dispatching_Operation (Old_Subp)
1015 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1016 or else
1017 (Ekind (Subp) = E_Procedure
1018 and then Is_Dispatching_Operation (Old_Subp)
1019 and then Present (Alias (Old_Subp))
1020 and then Is_Null_Interface_Primitive
1021 (Ultimate_Alias (Old_Subp)))
1022 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1023 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
1025 Check_Controlling_Formals (Tagged_Type, Subp);
1026 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1027 Set_Is_Dispatching_Operation (Subp);
1028 end if;
1030 return;
1032 -- The operation may be a child unit, whose scope is the defining
1033 -- package, but which is not a primitive operation of the type.
1035 elsif Is_Child_Unit (Subp) then
1036 return;
1038 -- If the subprogram is not defined in a package spec, the only case
1039 -- where it can be a dispatching op is when it overrides an operation
1040 -- before the freezing point of the type.
1042 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1043 or else In_Package_Body (Scope (Subp)))
1044 and then not Has_Dispatching_Parent
1045 then
1046 if not Comes_From_Source (Subp)
1047 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1048 then
1049 null;
1051 -- If the type is already frozen, the overriding is not allowed
1052 -- except when Old_Subp is not a dispatching operation (which can
1053 -- occur when Old_Subp was inherited by an untagged type). However,
1054 -- a body with no previous spec freezes the type *after* its
1055 -- declaration, and therefore is a legal overriding (unless the type
1056 -- has already been frozen). Only the first such body is legal.
1058 elsif Present (Old_Subp)
1059 and then Is_Dispatching_Operation (Old_Subp)
1060 then
1061 if Comes_From_Source (Subp)
1062 and then
1063 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1064 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1065 then
1066 declare
1067 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1068 Decl_Item : Node_Id;
1070 begin
1071 -- ??? The checks here for whether the type has been frozen
1072 -- prior to the new body are not complete. It's not simple
1073 -- to check frozenness at this point since the body has
1074 -- already caused the type to be prematurely frozen in
1075 -- Analyze_Declarations, but we're forced to recheck this
1076 -- here because of the odd rule interpretation that allows
1077 -- the overriding if the type wasn't frozen prior to the
1078 -- body. The freezing action should probably be delayed
1079 -- until after the spec is seen, but that's a tricky
1080 -- change to the delicate freezing code.
1082 -- Look at each declaration following the type up until the
1083 -- new subprogram body. If any of the declarations is a body
1084 -- then the type has been frozen already so the overriding
1085 -- primitive is illegal.
1087 Decl_Item := Next (Parent (Tagged_Type));
1088 while Present (Decl_Item)
1089 and then (Decl_Item /= Subp_Body)
1090 loop
1091 if Comes_From_Source (Decl_Item)
1092 and then (Nkind (Decl_Item) in N_Proper_Body
1093 or else Nkind (Decl_Item) in N_Body_Stub)
1094 then
1095 Error_Msg_N ("overriding of& is too late!", Subp);
1096 Error_Msg_N
1097 ("\spec should appear immediately after the type!",
1098 Subp);
1099 exit;
1100 end if;
1102 Next (Decl_Item);
1103 end loop;
1105 -- If the subprogram doesn't follow in the list of
1106 -- declarations including the type then the type has
1107 -- definitely been frozen already and the body is illegal.
1109 if No (Decl_Item) then
1110 Error_Msg_N ("overriding of& is too late!", Subp);
1111 Error_Msg_N
1112 ("\spec should appear immediately after the type!",
1113 Subp);
1115 elsif Is_Frozen (Subp) then
1117 -- The subprogram body declares a primitive operation.
1118 -- If the subprogram is already frozen, we must update
1119 -- its dispatching information explicitly here. The
1120 -- information is taken from the overridden subprogram.
1121 -- We must also generate a cross-reference entry because
1122 -- references to other primitives were already created
1123 -- when type was frozen.
1125 Body_Is_Last_Primitive := True;
1127 if Present (DTC_Entity (Old_Subp)) then
1128 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1129 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1131 if not Restriction_Active (No_Dispatching_Calls) then
1132 if Building_Static_DT (Tagged_Type) then
1134 -- If the static dispatch table has not been
1135 -- built then there is nothing else to do now;
1136 -- otherwise we notify that we cannot build the
1137 -- static dispatch table.
1139 if Has_Dispatch_Table (Tagged_Type) then
1140 Error_Msg_N
1141 ("overriding of& is too late for building "
1142 & " static dispatch tables!", Subp);
1143 Error_Msg_N
1144 ("\spec should appear immediately after "
1145 & "the type!", Subp);
1146 end if;
1148 -- No code required to register primitives in VM
1149 -- targets
1151 elsif VM_Target /= No_VM then
1152 null;
1154 else
1155 Insert_Actions_After (Subp_Body,
1156 Register_Primitive (Sloc (Subp_Body),
1157 Prim => Subp));
1158 end if;
1160 -- Indicate that this is an overriding operation,
1161 -- and replace the overridden entry in the list of
1162 -- primitive operations, which is used for xref
1163 -- generation subsequently.
1165 Generate_Reference (Tagged_Type, Subp, 'P', False);
1166 Override_Dispatching_Operation
1167 (Tagged_Type, Old_Subp, Subp);
1168 end if;
1169 end if;
1170 end if;
1171 end;
1173 else
1174 Error_Msg_N ("overriding of& is too late!", Subp);
1175 Error_Msg_N
1176 ("\subprogram spec should appear immediately after the type!",
1177 Subp);
1178 end if;
1180 -- If the type is not frozen yet and we are not in the overriding
1181 -- case it looks suspiciously like an attempt to define a primitive
1182 -- operation, which requires the declaration to be in a package spec
1183 -- (3.2.3(6)). Only report cases where the type and subprogram are
1184 -- in the same declaration list (by checking the enclosing parent
1185 -- declarations), to avoid spurious warnings on subprograms in
1186 -- instance bodies when the type is declared in the instance spec
1187 -- but hasn't been frozen by the instance body.
1189 elsif not Is_Frozen (Tagged_Type)
1190 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1191 then
1192 Error_Msg_N
1193 ("??not dispatching (must be defined in a package spec)", Subp);
1194 return;
1196 -- When the type is frozen, it is legitimate to define a new
1197 -- non-primitive operation.
1199 else
1200 return;
1201 end if;
1203 -- Now, we are sure that the scope is a package spec. If the subprogram
1204 -- is declared after the freezing point of the type that's an error
1206 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1207 Error_Msg_N ("this primitive operation is declared too late", Subp);
1208 Error_Msg_NE
1209 ("??no primitive operations for& after this line",
1210 Freeze_Node (Tagged_Type),
1211 Tagged_Type);
1212 return;
1213 end if;
1215 Check_Controlling_Formals (Tagged_Type, Subp);
1217 Ovr_Subp := Old_Subp;
1219 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1220 -- overridden by Subp. This only applies to source subprograms, and
1221 -- their declaration must carry an explicit overriding indicator.
1223 if No (Ovr_Subp)
1224 and then Ada_Version >= Ada_2012
1225 and then Comes_From_Source (Subp)
1226 and then
1227 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1228 then
1229 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1231 -- Verify that the proper overriding indicator has been supplied.
1233 if Present (Ovr_Subp)
1234 and then
1235 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1236 then
1237 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1238 end if;
1239 end if;
1241 -- Now it should be a correct primitive operation, put it in the list
1243 if Present (Ovr_Subp) then
1245 -- If the type has interfaces we complete this check after we set
1246 -- attribute Is_Dispatching_Operation.
1248 Check_Subtype_Conformant (Subp, Ovr_Subp);
1250 -- A primitive operation with the name of a primitive controlled
1251 -- operation does not override a non-visible overriding controlled
1252 -- operation, i.e. one declared in a private part when the full
1253 -- view of a type is controlled. Conversely, it will override a
1254 -- visible operation that may be declared in a partial view when
1255 -- the full view is controlled.
1257 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1258 and then Is_Controlled (Tagged_Type)
1259 and then not Is_Visibly_Controlled (Tagged_Type)
1260 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1261 then
1262 Set_Overridden_Operation (Subp, Empty);
1264 -- If the subprogram specification carries an overriding
1265 -- indicator, no need for the warning: it is either redundant,
1266 -- or else an error will be reported.
1268 if Nkind (Parent (Subp)) = N_Procedure_Specification
1269 and then
1270 (Must_Override (Parent (Subp))
1271 or else Must_Not_Override (Parent (Subp)))
1272 then
1273 null;
1275 -- Here we need the warning
1277 else
1278 Error_Msg_NE
1279 ("operation does not override inherited&??", Subp, Subp);
1280 end if;
1282 else
1283 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1285 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1286 -- that covers abstract interface subprograms we must register it
1287 -- in all the secondary dispatch tables associated with abstract
1288 -- interfaces. We do this now only if not building static tables,
1289 -- nor when the expander is inactive (we avoid trying to register
1290 -- primitives in semantics-only mode, since the type may not have
1291 -- an associated dispatch table). Otherwise the patch code is
1292 -- emitted after those tables are built, to prevent access before
1293 -- elaboration in gigi.
1295 if Body_Is_Last_Primitive and then Expander_Active then
1296 declare
1297 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1298 Elmt : Elmt_Id;
1299 Prim : Node_Id;
1301 begin
1302 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1303 while Present (Elmt) loop
1304 Prim := Node (Elmt);
1306 -- No code required to register primitives in VM targets
1308 if Present (Alias (Prim))
1309 and then Present (Interface_Alias (Prim))
1310 and then Alias (Prim) = Subp
1311 and then not Building_Static_DT (Tagged_Type)
1312 and then VM_Target = No_VM
1313 then
1314 Insert_Actions_After (Subp_Body,
1315 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1316 end if;
1318 Next_Elmt (Elmt);
1319 end loop;
1321 -- Redisplay the contents of the updated dispatch table
1323 if Debug_Flag_ZZ then
1324 Write_Str ("Late overriding: ");
1325 Write_DT (Tagged_Type);
1326 end if;
1327 end;
1328 end if;
1329 end if;
1331 -- If the tagged type is a concurrent type then we must be compiling
1332 -- with no code generation (we are either compiling a generic unit or
1333 -- compiling under -gnatc mode) because we have previously tested that
1334 -- no serious errors has been reported. In this case we do not add the
1335 -- primitive to the list of primitives of Tagged_Type but we leave the
1336 -- primitive decorated as a dispatching operation to be able to analyze
1337 -- and report errors associated with the Object.Operation notation.
1339 elsif Is_Concurrent_Type (Tagged_Type) then
1340 pragma Assert (not Expander_Active);
1342 -- Attach operation to list of primitives of the synchronized type
1343 -- itself, for ASIS use.
1345 Append_Elmt (Subp, Direct_Primitive_Operations (Tagged_Type));
1347 -- If no old subprogram, then we add this as a dispatching operation,
1348 -- but we avoid doing this if an error was posted, to prevent annoying
1349 -- cascaded errors.
1351 elsif not Error_Posted (Subp) then
1352 Add_Dispatching_Operation (Tagged_Type, Subp);
1353 end if;
1355 Set_Is_Dispatching_Operation (Subp, True);
1357 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1358 -- subtype conformance against all the interfaces covered by this
1359 -- primitive.
1361 if Present (Ovr_Subp)
1362 and then Has_Interfaces (Tagged_Type)
1363 then
1364 declare
1365 Ifaces_List : Elist_Id;
1366 Iface_Elmt : Elmt_Id;
1367 Iface_Prim_Elmt : Elmt_Id;
1368 Iface_Prim : Entity_Id;
1369 Ret_Typ : Entity_Id;
1371 begin
1372 Collect_Interfaces (Tagged_Type, Ifaces_List);
1374 Iface_Elmt := First_Elmt (Ifaces_List);
1375 while Present (Iface_Elmt) loop
1376 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1377 Iface_Prim_Elmt :=
1378 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1379 while Present (Iface_Prim_Elmt) loop
1380 Iface_Prim := Node (Iface_Prim_Elmt);
1382 if Is_Interface_Conformant
1383 (Tagged_Type, Iface_Prim, Subp)
1384 then
1385 -- Handle procedures, functions whose return type
1386 -- matches, or functions not returning interfaces
1388 if Ekind (Subp) = E_Procedure
1389 or else Etype (Iface_Prim) = Etype (Subp)
1390 or else not Is_Interface (Etype (Iface_Prim))
1391 then
1392 Check_Subtype_Conformant
1393 (New_Id => Subp,
1394 Old_Id => Iface_Prim,
1395 Err_Loc => Subp,
1396 Skip_Controlling_Formals => True);
1398 -- Handle functions returning interfaces
1400 elsif Implements_Interface
1401 (Etype (Subp), Etype (Iface_Prim))
1402 then
1403 -- Temporarily force both entities to return the
1404 -- same type. Required because Subtype_Conformant
1405 -- does not handle this case.
1407 Ret_Typ := Etype (Iface_Prim);
1408 Set_Etype (Iface_Prim, Etype (Subp));
1410 Check_Subtype_Conformant
1411 (New_Id => Subp,
1412 Old_Id => Iface_Prim,
1413 Err_Loc => Subp,
1414 Skip_Controlling_Formals => True);
1416 Set_Etype (Iface_Prim, Ret_Typ);
1417 end if;
1418 end if;
1420 Next_Elmt (Iface_Prim_Elmt);
1421 end loop;
1422 end if;
1424 Next_Elmt (Iface_Elmt);
1425 end loop;
1426 end;
1427 end if;
1429 if not Body_Is_Last_Primitive then
1430 Set_DT_Position_Value (Subp, No_Uint);
1432 elsif Has_Controlled_Component (Tagged_Type)
1433 and then Nam_In (Chars (Subp), Name_Initialize,
1434 Name_Adjust,
1435 Name_Finalize,
1436 Name_Finalize_Address)
1437 then
1438 declare
1439 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1440 Decl : Node_Id;
1441 Old_P : Entity_Id;
1442 Old_Bod : Node_Id;
1443 Old_Spec : Entity_Id;
1445 C_Names : constant array (1 .. 4) of Name_Id :=
1446 (Name_Initialize,
1447 Name_Adjust,
1448 Name_Finalize,
1449 Name_Finalize_Address);
1451 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1452 (TSS_Deep_Initialize,
1453 TSS_Deep_Adjust,
1454 TSS_Deep_Finalize,
1455 TSS_Finalize_Address);
1457 begin
1458 -- Remove previous controlled function which was constructed and
1459 -- analyzed when the type was frozen. This requires removing the
1460 -- body of the redefined primitive, as well as its specification
1461 -- if needed (there is no spec created for Deep_Initialize, see
1462 -- exp_ch3.adb). We must also dismantle the exception information
1463 -- that may have been generated for it when front end zero-cost
1464 -- tables are enabled.
1466 for J in D_Names'Range loop
1467 Old_P := TSS (Tagged_Type, D_Names (J));
1469 if Present (Old_P)
1470 and then Chars (Subp) = C_Names (J)
1471 then
1472 Old_Bod := Unit_Declaration_Node (Old_P);
1473 Remove (Old_Bod);
1474 Set_Is_Eliminated (Old_P);
1475 Set_Scope (Old_P, Scope (Current_Scope));
1477 if Nkind (Old_Bod) = N_Subprogram_Body
1478 and then Present (Corresponding_Spec (Old_Bod))
1479 then
1480 Old_Spec := Corresponding_Spec (Old_Bod);
1481 Set_Has_Completion (Old_Spec, False);
1482 end if;
1483 end if;
1484 end loop;
1486 Build_Late_Proc (Tagged_Type, Chars (Subp));
1488 -- The new operation is added to the actions of the freeze node
1489 -- for the type, but this node has already been analyzed, so we
1490 -- must retrieve and analyze explicitly the new body.
1492 if Present (F_Node)
1493 and then Present (Actions (F_Node))
1494 then
1495 Decl := Last (Actions (F_Node));
1496 Analyze (Decl);
1497 end if;
1498 end;
1499 end if;
1500 end Check_Dispatching_Operation;
1502 ------------------------------------------
1503 -- Check_Operation_From_Incomplete_Type --
1504 ------------------------------------------
1506 procedure Check_Operation_From_Incomplete_Type
1507 (Subp : Entity_Id;
1508 Typ : Entity_Id)
1510 Full : constant Entity_Id := Full_View (Typ);
1511 Parent_Typ : constant Entity_Id := Etype (Full);
1512 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1513 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1514 Op1, Op2 : Elmt_Id;
1515 Prev : Elmt_Id := No_Elmt;
1517 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1518 -- Check that Subp has profile of an operation derived from Parent_Subp.
1519 -- Subp must have a parameter or result type that is Typ or an access
1520 -- parameter or access result type that designates Typ.
1522 ------------------
1523 -- Derives_From --
1524 ------------------
1526 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1527 F1, F2 : Entity_Id;
1529 begin
1530 if Chars (Parent_Subp) /= Chars (Subp) then
1531 return False;
1532 end if;
1534 -- Check that the type of controlling formals is derived from the
1535 -- parent subprogram's controlling formal type (or designated type
1536 -- if the formal type is an anonymous access type).
1538 F1 := First_Formal (Parent_Subp);
1539 F2 := First_Formal (Subp);
1540 while Present (F1) and then Present (F2) loop
1541 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1542 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1543 return False;
1544 elsif Designated_Type (Etype (F1)) = Parent_Typ
1545 and then Designated_Type (Etype (F2)) /= Full
1546 then
1547 return False;
1548 end if;
1550 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1551 return False;
1553 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1554 return False;
1555 end if;
1557 Next_Formal (F1);
1558 Next_Formal (F2);
1559 end loop;
1561 -- Check that a controlling result type is derived from the parent
1562 -- subprogram's result type (or designated type if the result type
1563 -- is an anonymous access type).
1565 if Ekind (Parent_Subp) = E_Function then
1566 if Ekind (Subp) /= E_Function then
1567 return False;
1569 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1570 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1571 return False;
1573 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1574 and then Designated_Type (Etype (Subp)) /= Full
1575 then
1576 return False;
1577 end if;
1579 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1580 return False;
1582 elsif Etype (Parent_Subp) = Parent_Typ
1583 and then Etype (Subp) /= Full
1584 then
1585 return False;
1586 end if;
1588 elsif Ekind (Subp) = E_Function then
1589 return False;
1590 end if;
1592 return No (F1) and then No (F2);
1593 end Derives_From;
1595 -- Start of processing for Check_Operation_From_Incomplete_Type
1597 begin
1598 -- The operation may override an inherited one, or may be a new one
1599 -- altogether. The inherited operation will have been hidden by the
1600 -- current one at the point of the type derivation, so it does not
1601 -- appear in the list of primitive operations of the type. We have to
1602 -- find the proper place of insertion in the list of primitive opera-
1603 -- tions by iterating over the list for the parent type.
1605 Op1 := First_Elmt (Old_Prim);
1606 Op2 := First_Elmt (New_Prim);
1607 while Present (Op1) and then Present (Op2) loop
1608 if Derives_From (Node (Op1)) then
1609 if No (Prev) then
1611 -- Avoid adding it to the list of primitives if already there
1613 if Node (Op2) /= Subp then
1614 Prepend_Elmt (Subp, New_Prim);
1615 end if;
1617 else
1618 Insert_Elmt_After (Subp, Prev);
1619 end if;
1621 return;
1622 end if;
1624 Prev := Op2;
1625 Next_Elmt (Op1);
1626 Next_Elmt (Op2);
1627 end loop;
1629 -- Operation is a new primitive
1631 Append_Elmt (Subp, New_Prim);
1632 end Check_Operation_From_Incomplete_Type;
1634 ---------------------------------------
1635 -- Check_Operation_From_Private_View --
1636 ---------------------------------------
1638 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1639 Tagged_Type : Entity_Id;
1641 begin
1642 if Is_Dispatching_Operation (Alias (Subp)) then
1643 Set_Scope (Subp, Current_Scope);
1644 Tagged_Type := Find_Dispatching_Type (Subp);
1646 -- Add Old_Subp to primitive operations if not already present
1648 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1649 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1651 -- If Old_Subp isn't already marked as dispatching then this is
1652 -- the case of an operation of an untagged private type fulfilled
1653 -- by a tagged type that overrides an inherited dispatching
1654 -- operation, so we set the necessary dispatching attributes here.
1656 if not Is_Dispatching_Operation (Old_Subp) then
1658 -- If the untagged type has no discriminants, and the full
1659 -- view is constrained, there will be a spurious mismatch of
1660 -- subtypes on the controlling arguments, because the tagged
1661 -- type is the internal base type introduced in the derivation.
1662 -- Use the original type to verify conformance, rather than the
1663 -- base type.
1665 if not Comes_From_Source (Tagged_Type)
1666 and then Has_Discriminants (Tagged_Type)
1667 then
1668 declare
1669 Formal : Entity_Id;
1671 begin
1672 Formal := First_Formal (Old_Subp);
1673 while Present (Formal) loop
1674 if Tagged_Type = Base_Type (Etype (Formal)) then
1675 Tagged_Type := Etype (Formal);
1676 end if;
1678 Next_Formal (Formal);
1679 end loop;
1680 end;
1682 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1683 Tagged_Type := Etype (Old_Subp);
1684 end if;
1685 end if;
1687 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1688 Set_Is_Dispatching_Operation (Old_Subp, True);
1689 Set_DT_Position_Value (Old_Subp, No_Uint);
1690 end if;
1692 -- If the old subprogram is an explicit renaming of some other
1693 -- entity, it is not overridden by the inherited subprogram.
1694 -- Otherwise, update its alias and other attributes.
1696 if Present (Alias (Old_Subp))
1697 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1698 N_Subprogram_Renaming_Declaration
1699 then
1700 Set_Alias (Old_Subp, Alias (Subp));
1702 -- The derived subprogram should inherit the abstractness of
1703 -- the parent subprogram (except in the case of a function
1704 -- returning the type). This sets the abstractness properly
1705 -- for cases where a private extension may have inherited an
1706 -- abstract operation, but the full type is derived from a
1707 -- descendant type and inherits a nonabstract version.
1709 if Etype (Subp) /= Tagged_Type then
1710 Set_Is_Abstract_Subprogram
1711 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1712 end if;
1713 end if;
1714 end if;
1715 end if;
1716 end Check_Operation_From_Private_View;
1718 --------------------------
1719 -- Find_Controlling_Arg --
1720 --------------------------
1722 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1723 Orig_Node : constant Node_Id := Original_Node (N);
1724 Typ : Entity_Id;
1726 begin
1727 if Nkind (Orig_Node) = N_Qualified_Expression then
1728 return Find_Controlling_Arg (Expression (Orig_Node));
1729 end if;
1731 -- Dispatching on result case. If expansion is disabled, the node still
1732 -- has the structure of a function call. However, if the function name
1733 -- is an operator and the call was given in infix form, the original
1734 -- node has no controlling result and we must examine the current node.
1736 if Nkind (N) = N_Function_Call
1737 and then Present (Controlling_Argument (N))
1738 and then Has_Controlling_Result (Entity (Name (N)))
1739 then
1740 return Controlling_Argument (N);
1742 -- If expansion is enabled, the call may have been transformed into
1743 -- an indirect call, and we need to recover the original node.
1745 elsif Nkind (Orig_Node) = N_Function_Call
1746 and then Present (Controlling_Argument (Orig_Node))
1747 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1748 then
1749 return Controlling_Argument (Orig_Node);
1751 -- Type conversions are dynamically tagged if the target type, or its
1752 -- designated type, are classwide. An interface conversion expands into
1753 -- a dereference, so test must be performed on the original node.
1755 elsif Nkind (Orig_Node) = N_Type_Conversion
1756 and then Nkind (N) = N_Explicit_Dereference
1757 and then Is_Controlling_Actual (N)
1758 then
1759 declare
1760 Target_Type : constant Entity_Id :=
1761 Entity (Subtype_Mark (Orig_Node));
1763 begin
1764 if Is_Class_Wide_Type (Target_Type) then
1765 return N;
1767 elsif Is_Access_Type (Target_Type)
1768 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1769 then
1770 return N;
1772 else
1773 return Empty;
1774 end if;
1775 end;
1777 -- Normal case
1779 elsif Is_Controlling_Actual (N)
1780 or else
1781 (Nkind (Parent (N)) = N_Qualified_Expression
1782 and then Is_Controlling_Actual (Parent (N)))
1783 then
1784 Typ := Etype (N);
1786 if Is_Access_Type (Typ) then
1788 -- In the case of an Access attribute, use the type of the prefix,
1789 -- since in the case of an actual for an access parameter, the
1790 -- attribute's type may be of a specific designated type, even
1791 -- though the prefix type is class-wide.
1793 if Nkind (N) = N_Attribute_Reference then
1794 Typ := Etype (Prefix (N));
1796 -- An allocator is dispatching if the type of qualified expression
1797 -- is class_wide, in which case this is the controlling type.
1799 elsif Nkind (Orig_Node) = N_Allocator
1800 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1801 then
1802 Typ := Etype (Expression (Orig_Node));
1803 else
1804 Typ := Designated_Type (Typ);
1805 end if;
1806 end if;
1808 if Is_Class_Wide_Type (Typ)
1809 or else
1810 (Nkind (Parent (N)) = N_Qualified_Expression
1811 and then Is_Access_Type (Etype (N))
1812 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1813 then
1814 return N;
1815 end if;
1816 end if;
1818 return Empty;
1819 end Find_Controlling_Arg;
1821 ---------------------------
1822 -- Find_Dispatching_Type --
1823 ---------------------------
1825 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1826 A_Formal : Entity_Id;
1827 Formal : Entity_Id;
1828 Ctrl_Type : Entity_Id;
1830 begin
1831 if Ekind_In (Subp, E_Function, E_Procedure)
1832 and then Present (DTC_Entity (Subp))
1833 then
1834 return Scope (DTC_Entity (Subp));
1836 -- For subprograms internally generated by derivations of tagged types
1837 -- use the alias subprogram as a reference to locate the dispatching
1838 -- type of Subp.
1840 elsif not Comes_From_Source (Subp)
1841 and then Present (Alias (Subp))
1842 and then Is_Dispatching_Operation (Alias (Subp))
1843 then
1844 if Ekind (Alias (Subp)) = E_Function
1845 and then Has_Controlling_Result (Alias (Subp))
1846 then
1847 return Check_Controlling_Type (Etype (Subp), Subp);
1849 else
1850 Formal := First_Formal (Subp);
1851 A_Formal := First_Formal (Alias (Subp));
1852 while Present (A_Formal) loop
1853 if Is_Controlling_Formal (A_Formal) then
1854 return Check_Controlling_Type (Etype (Formal), Subp);
1855 end if;
1857 Next_Formal (Formal);
1858 Next_Formal (A_Formal);
1859 end loop;
1861 pragma Assert (False);
1862 return Empty;
1863 end if;
1865 -- General case
1867 else
1868 Formal := First_Formal (Subp);
1869 while Present (Formal) loop
1870 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1872 if Present (Ctrl_Type) then
1873 return Ctrl_Type;
1874 end if;
1876 Next_Formal (Formal);
1877 end loop;
1879 -- The subprogram may also be dispatching on result
1881 if Present (Etype (Subp)) then
1882 return Check_Controlling_Type (Etype (Subp), Subp);
1883 end if;
1884 end if;
1886 pragma Assert (not Is_Dispatching_Operation (Subp));
1887 return Empty;
1888 end Find_Dispatching_Type;
1890 --------------------------------------
1891 -- Find_Hidden_Overridden_Primitive --
1892 --------------------------------------
1894 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
1896 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
1897 Elmt : Elmt_Id;
1898 Orig_Prim : Entity_Id;
1899 Prim : Entity_Id;
1900 Vis_List : Elist_Id;
1902 begin
1903 -- This Ada 2012 rule applies only for type extensions or private
1904 -- extensions, where the parent type is not in a parent unit, and
1905 -- where an operation is never declared but still inherited.
1907 if No (Tag_Typ)
1908 or else not Is_Record_Type (Tag_Typ)
1909 or else Etype (Tag_Typ) = Tag_Typ
1910 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
1911 then
1912 return Empty;
1913 end if;
1915 -- Collect the list of visible ancestor of the tagged type
1917 Vis_List := Visible_Ancestors (Tag_Typ);
1919 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
1920 while Present (Elmt) loop
1921 Prim := Node (Elmt);
1923 -- Find an inherited hidden dispatching primitive with the name of S
1924 -- and a type-conformant profile.
1926 if Present (Alias (Prim))
1927 and then Is_Hidden (Alias (Prim))
1928 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
1929 and then Primitive_Names_Match (S, Prim)
1930 and then Type_Conformant (S, Prim)
1931 then
1932 declare
1933 Vis_Ancestor : Elmt_Id;
1934 Elmt : Elmt_Id;
1936 begin
1937 -- The original corresponding operation of Prim must be an
1938 -- operation of a visible ancestor of the dispatching type S,
1939 -- and the original corresponding operation of S2 must be
1940 -- visible.
1942 Orig_Prim := Original_Corresponding_Operation (Prim);
1944 if Orig_Prim /= Prim
1945 and then Is_Immediately_Visible (Orig_Prim)
1946 then
1947 Vis_Ancestor := First_Elmt (Vis_List);
1948 while Present (Vis_Ancestor) loop
1949 Elmt :=
1950 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
1951 while Present (Elmt) loop
1952 if Node (Elmt) = Orig_Prim then
1953 Set_Overridden_Operation (S, Prim);
1954 Set_Alias (Prim, Orig_Prim);
1955 return Prim;
1956 end if;
1958 Next_Elmt (Elmt);
1959 end loop;
1961 Next_Elmt (Vis_Ancestor);
1962 end loop;
1963 end if;
1964 end;
1965 end if;
1967 Next_Elmt (Elmt);
1968 end loop;
1970 return Empty;
1971 end Find_Hidden_Overridden_Primitive;
1973 ---------------------------------------
1974 -- Find_Primitive_Covering_Interface --
1975 ---------------------------------------
1977 function Find_Primitive_Covering_Interface
1978 (Tagged_Type : Entity_Id;
1979 Iface_Prim : Entity_Id) return Entity_Id
1981 E : Entity_Id;
1982 El : Elmt_Id;
1984 begin
1985 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1986 or else (Present (Alias (Iface_Prim))
1987 and then
1988 Is_Interface
1989 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1991 -- Search in the homonym chain. Done to speed up locating visible
1992 -- entities and required to catch primitives associated with the partial
1993 -- view of private types when processing the corresponding full view.
1995 E := Current_Entity (Iface_Prim);
1996 while Present (E) loop
1997 if Is_Subprogram (E)
1998 and then Is_Dispatching_Operation (E)
1999 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
2000 then
2001 return E;
2002 end if;
2004 E := Homonym (E);
2005 end loop;
2007 -- Search in the list of primitives of the type. Required to locate
2008 -- the covering primitive if the covering primitive is not visible
2009 -- (for example, non-visible inherited primitive of private type).
2011 El := First_Elmt (Primitive_Operations (Tagged_Type));
2012 while Present (El) loop
2013 E := Node (El);
2015 -- Keep separate the management of internal entities that link
2016 -- primitives with interface primitives from tagged type primitives.
2018 if No (Interface_Alias (E)) then
2019 if Present (Alias (E)) then
2021 -- This interface primitive has not been covered yet
2023 if Alias (E) = Iface_Prim then
2024 return E;
2026 -- The covering primitive was inherited
2028 elsif Overridden_Operation (Ultimate_Alias (E))
2029 = Iface_Prim
2030 then
2031 return E;
2032 end if;
2033 end if;
2035 -- Check if E covers the interface primitive (includes case in
2036 -- which E is an inherited private primitive).
2038 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2039 return E;
2040 end if;
2042 -- Use the internal entity that links the interface primitive with
2043 -- the covering primitive to locate the entity.
2045 elsif Interface_Alias (E) = Iface_Prim then
2046 return Alias (E);
2047 end if;
2049 Next_Elmt (El);
2050 end loop;
2052 -- Not found
2054 return Empty;
2055 end Find_Primitive_Covering_Interface;
2057 ---------------------------
2058 -- Inherited_Subprograms --
2059 ---------------------------
2061 function Inherited_Subprograms
2062 (S : Entity_Id;
2063 No_Interfaces : Boolean := False;
2064 Interfaces_Only : Boolean := False;
2065 One_Only : Boolean := False) return Subprogram_List
2067 Result : Subprogram_List (1 .. 6000);
2068 -- 6000 here is intended to be infinity. We could use an expandable
2069 -- table, but it would be awfully heavy, and there is no way that we
2070 -- could reasonably exceed this value.
2072 N : Int := 0;
2073 -- Number of entries in Result
2075 Parent_Op : Entity_Id;
2076 -- Traverses the Overridden_Operation chain
2078 procedure Store_IS (E : Entity_Id);
2079 -- Stores E in Result if not already stored
2081 --------------
2082 -- Store_IS --
2083 --------------
2085 procedure Store_IS (E : Entity_Id) is
2086 begin
2087 for J in 1 .. N loop
2088 if E = Result (J) then
2089 return;
2090 end if;
2091 end loop;
2093 N := N + 1;
2094 Result (N) := E;
2095 end Store_IS;
2097 -- Start of processing for Inherited_Subprograms
2099 begin
2100 pragma Assert (not (No_Interfaces and Interfaces_Only));
2102 if Present (S) and then Is_Dispatching_Operation (S) then
2104 -- Deal with direct inheritance
2106 if not Interfaces_Only then
2107 Parent_Op := S;
2108 loop
2109 Parent_Op := Overridden_Operation (Parent_Op);
2110 exit when No (Parent_Op)
2111 or else
2112 (No_Interfaces
2113 and then
2114 Is_Interface (Find_Dispatching_Type (Parent_Op)));
2116 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2117 Store_IS (Parent_Op);
2119 if One_Only then
2120 goto Done;
2121 end if;
2122 end if;
2123 end loop;
2124 end if;
2126 -- Now deal with interfaces
2128 if not No_Interfaces then
2129 declare
2130 Tag_Typ : Entity_Id;
2131 Prim : Entity_Id;
2132 Elmt : Elmt_Id;
2134 begin
2135 Tag_Typ := Find_Dispatching_Type (S);
2137 -- In the presence of limited views there may be no visible
2138 -- dispatching type. Primitives will be inherited when non-
2139 -- limited view is frozen.
2141 if No (Tag_Typ) then
2142 return Result (1 .. 0);
2143 end if;
2145 if Is_Concurrent_Type (Tag_Typ) then
2146 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2147 end if;
2149 -- Search primitive operations of dispatching type
2151 if Present (Tag_Typ)
2152 and then Present (Primitive_Operations (Tag_Typ))
2153 then
2154 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2155 while Present (Elmt) loop
2156 Prim := Node (Elmt);
2158 -- The following test eliminates some odd cases in which
2159 -- Ekind (Prim) is Void, to be investigated further ???
2161 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2162 null;
2164 -- For [generic] subprogram, look at interface alias
2166 elsif Present (Interface_Alias (Prim))
2167 and then Alias (Prim) = S
2168 then
2169 -- We have found a primitive covered by S
2171 Store_IS (Interface_Alias (Prim));
2173 if One_Only then
2174 goto Done;
2175 end if;
2176 end if;
2178 Next_Elmt (Elmt);
2179 end loop;
2180 end if;
2181 end;
2182 end if;
2183 end if;
2185 <<Done>>
2187 return Result (1 .. N);
2188 end Inherited_Subprograms;
2190 ---------------------------
2191 -- Is_Dynamically_Tagged --
2192 ---------------------------
2194 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2195 begin
2196 if Nkind (N) = N_Error then
2197 return False;
2199 elsif Present (Find_Controlling_Arg (N)) then
2200 return True;
2202 -- Special cases: entities, and calls that dispatch on result
2204 elsif Is_Entity_Name (N) then
2205 return Is_Class_Wide_Type (Etype (N));
2207 elsif Nkind (N) = N_Function_Call
2208 and then Is_Class_Wide_Type (Etype (N))
2209 then
2210 return True;
2212 -- Otherwise check whether call has controlling argument
2214 else
2215 return False;
2216 end if;
2217 end Is_Dynamically_Tagged;
2219 ---------------------------------
2220 -- Is_Null_Interface_Primitive --
2221 ---------------------------------
2223 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2224 begin
2225 return Comes_From_Source (E)
2226 and then Is_Dispatching_Operation (E)
2227 and then Ekind (E) = E_Procedure
2228 and then Null_Present (Parent (E))
2229 and then Is_Interface (Find_Dispatching_Type (E));
2230 end Is_Null_Interface_Primitive;
2232 -----------------------------------
2233 -- Is_Inherited_Public_Operation --
2234 -----------------------------------
2236 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2237 Prim : constant Entity_Id := Alias (Op);
2238 Scop : constant Entity_Id := Scope (Prim);
2239 Pack_Decl : Node_Id;
2241 begin
2242 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2243 Pack_Decl := Unit_Declaration_Node (Scop);
2244 return Nkind (Pack_Decl) = N_Package_Declaration
2245 and then List_Containing (Unit_Declaration_Node (Prim)) =
2246 Visible_Declarations (Specification (Pack_Decl));
2248 else
2249 return False;
2250 end if;
2251 end Is_Inherited_Public_Operation;
2253 ------------------------------
2254 -- Is_Overriding_Subprogram --
2255 ------------------------------
2257 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2258 Inherited : constant Subprogram_List :=
2259 Inherited_Subprograms (E, One_Only => True);
2260 begin
2261 return Inherited'Length > 0;
2262 end Is_Overriding_Subprogram;
2264 --------------------------
2265 -- Is_Tag_Indeterminate --
2266 --------------------------
2268 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2269 Nam : Entity_Id;
2270 Actual : Node_Id;
2271 Orig_Node : constant Node_Id := Original_Node (N);
2273 begin
2274 if Nkind (Orig_Node) = N_Function_Call
2275 and then Is_Entity_Name (Name (Orig_Node))
2276 then
2277 Nam := Entity (Name (Orig_Node));
2279 if not Has_Controlling_Result (Nam) then
2280 return False;
2282 -- The function may have a controlling result, but if the return type
2283 -- is not visibly tagged, then this is not tag-indeterminate.
2285 elsif Is_Access_Type (Etype (Nam))
2286 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2287 then
2288 return False;
2290 -- An explicit dereference means that the call has already been
2291 -- expanded and there is no tag to propagate.
2293 elsif Nkind (N) = N_Explicit_Dereference then
2294 return False;
2296 -- If there are no actuals, the call is tag-indeterminate
2298 elsif No (Parameter_Associations (Orig_Node)) then
2299 return True;
2301 else
2302 Actual := First_Actual (Orig_Node);
2303 while Present (Actual) loop
2304 if Is_Controlling_Actual (Actual)
2305 and then not Is_Tag_Indeterminate (Actual)
2306 then
2307 -- One operand is dispatching
2309 return False;
2310 end if;
2312 Next_Actual (Actual);
2313 end loop;
2315 return True;
2316 end if;
2318 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2319 return Is_Tag_Indeterminate (Expression (Orig_Node));
2321 -- Case of a call to the Input attribute (possibly rewritten), which is
2322 -- always tag-indeterminate except when its prefix is a Class attribute.
2324 elsif Nkind (Orig_Node) = N_Attribute_Reference
2325 and then
2326 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2327 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2328 then
2329 return True;
2331 -- In Ada 2005, a function that returns an anonymous access type can be
2332 -- dispatching, and the dereference of a call to such a function can
2333 -- also be tag-indeterminate if the call itself is.
2335 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2336 and then Ada_Version >= Ada_2005
2337 then
2338 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2340 else
2341 return False;
2342 end if;
2343 end Is_Tag_Indeterminate;
2345 ------------------------------------
2346 -- Override_Dispatching_Operation --
2347 ------------------------------------
2349 procedure Override_Dispatching_Operation
2350 (Tagged_Type : Entity_Id;
2351 Prev_Op : Entity_Id;
2352 New_Op : Entity_Id;
2353 Is_Wrapper : Boolean := False)
2355 Elmt : Elmt_Id;
2356 Prim : Node_Id;
2358 begin
2359 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2360 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2362 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2363 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2364 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2365 end if;
2367 -- If there is no previous operation to override, the type declaration
2368 -- was malformed, and an error must have been emitted already.
2370 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2371 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2372 Next_Elmt (Elmt);
2373 end loop;
2375 if No (Elmt) then
2376 return;
2377 end if;
2379 -- The location of entities that come from source in the list of
2380 -- primitives of the tagged type must follow their order of occurrence
2381 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2382 -- primitive of an interface that is not implemented by the parents of
2383 -- this tagged type (that is, it is an alias of an interface primitive
2384 -- generated by Derive_Interface_Progenitors), then we must append the
2385 -- new entity at the end of the list of primitives.
2387 if Present (Alias (Prev_Op))
2388 and then Etype (Tagged_Type) /= Tagged_Type
2389 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2390 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2391 Tagged_Type, Use_Full_View => True)
2392 and then not Implements_Interface
2393 (Etype (Tagged_Type),
2394 Find_Dispatching_Type (Alias (Prev_Op)))
2395 then
2396 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2397 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
2399 -- The new primitive replaces the overridden entity. Required to ensure
2400 -- that overriding primitive is assigned the same dispatch table slot.
2402 else
2403 Replace_Elmt (Elmt, New_Op);
2404 end if;
2406 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2408 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2409 -- entities of the overridden primitive to reference New_Op, and
2410 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2411 -- that the new operation is subtype conformant with the interface
2412 -- operations that it implements (for operations inherited from the
2413 -- parent itself, this check is made when building the derived type).
2415 -- Note: This code is executed with internally generated wrappers of
2416 -- functions with controlling result and late overridings.
2418 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2419 while Present (Elmt) loop
2420 Prim := Node (Elmt);
2422 if Prim = New_Op then
2423 null;
2425 -- Note: The check on Is_Subprogram protects the frontend against
2426 -- reading attributes in entities that are not yet fully decorated
2428 elsif Is_Subprogram (Prim)
2429 and then Present (Interface_Alias (Prim))
2430 and then Alias (Prim) = Prev_Op
2431 then
2432 Set_Alias (Prim, New_Op);
2434 -- No further decoration needed yet for internally generated
2435 -- wrappers of controlling functions since (at this stage)
2436 -- they are not yet decorated.
2438 if not Is_Wrapper then
2439 Check_Subtype_Conformant (New_Op, Prim);
2441 Set_Is_Abstract_Subprogram (Prim,
2442 Is_Abstract_Subprogram (New_Op));
2444 -- Ensure that this entity will be expanded to fill the
2445 -- corresponding entry in its dispatch table.
2447 if not Is_Abstract_Subprogram (Prim) then
2448 Set_Has_Delayed_Freeze (Prim);
2449 end if;
2450 end if;
2451 end if;
2453 Next_Elmt (Elmt);
2454 end loop;
2455 end if;
2457 if (not Is_Package_Or_Generic_Package (Current_Scope))
2458 or else not In_Private_Part (Current_Scope)
2459 then
2460 -- Not a private primitive
2462 null;
2464 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2466 -- Make the overriding operation into an alias of the implicit one.
2467 -- In this fashion a call from outside ends up calling the new body
2468 -- even if non-dispatching, and a call from inside calls the over-
2469 -- riding operation because it hides the implicit one. To indicate
2470 -- that the body of Prev_Op is never called, set its dispatch table
2471 -- entity to Empty. If the overridden operation has a dispatching
2472 -- result, so does the overriding one.
2474 Set_Alias (Prev_Op, New_Op);
2475 Set_DTC_Entity (Prev_Op, Empty);
2476 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2477 return;
2478 end if;
2479 end Override_Dispatching_Operation;
2481 -------------------
2482 -- Propagate_Tag --
2483 -------------------
2485 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2486 Call_Node : Node_Id;
2487 Arg : Node_Id;
2489 begin
2490 if Nkind (Actual) = N_Function_Call then
2491 Call_Node := Actual;
2493 elsif Nkind (Actual) = N_Identifier
2494 and then Nkind (Original_Node (Actual)) = N_Function_Call
2495 then
2496 -- Call rewritten as object declaration when stack-checking is
2497 -- enabled. Propagate tag to expression in declaration, which is
2498 -- original call.
2500 Call_Node := Expression (Parent (Entity (Actual)));
2502 -- Ada 2005: If this is a dereference of a call to a function with a
2503 -- dispatching access-result, the tag is propagated when the dereference
2504 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2506 elsif Nkind (Actual) = N_Explicit_Dereference
2507 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2508 then
2509 return;
2511 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2512 -- and in that case we can simply return.
2514 elsif Nkind (Actual) = N_Attribute_Reference then
2515 pragma Assert (Attribute_Name (Actual) = Name_Input);
2517 return;
2519 -- Only other possibilities are parenthesized or qualified expression,
2520 -- or an expander-generated unchecked conversion of a function call to
2521 -- a stream Input attribute.
2523 else
2524 Call_Node := Expression (Actual);
2525 end if;
2527 -- No action needed if the call has been already expanded
2529 if Is_Expanded_Dispatching_Call (Call_Node) then
2530 return;
2531 end if;
2533 -- Do not set the Controlling_Argument if already set. This happens in
2534 -- the special case of _Input (see Exp_Attr, case Input).
2536 if No (Controlling_Argument (Call_Node)) then
2537 Set_Controlling_Argument (Call_Node, Control);
2538 end if;
2540 Arg := First_Actual (Call_Node);
2541 while Present (Arg) loop
2542 if Is_Tag_Indeterminate (Arg) then
2543 Propagate_Tag (Control, Arg);
2544 end if;
2546 Next_Actual (Arg);
2547 end loop;
2549 -- Expansion of dispatching calls is suppressed when VM_Target, because
2550 -- the VM back-ends directly handle the generation of dispatching calls
2551 -- and would have to undo any expansion to an indirect call.
2553 if Tagged_Type_Expansion then
2554 declare
2555 Call_Typ : constant Entity_Id := Etype (Call_Node);
2557 begin
2558 Expand_Dispatching_Call (Call_Node);
2560 -- If the controlling argument is an interface type and the type
2561 -- of Call_Node differs then we must add an implicit conversion to
2562 -- force displacement of the pointer to the object to reference
2563 -- the secondary dispatch table of the interface.
2565 if Is_Interface (Etype (Control))
2566 and then Etype (Control) /= Call_Typ
2567 then
2568 -- Cannot use Convert_To because the previous call to
2569 -- Expand_Dispatching_Call leaves decorated the Call_Node
2570 -- with the type of Control.
2572 Rewrite (Call_Node,
2573 Make_Type_Conversion (Sloc (Call_Node),
2574 Subtype_Mark =>
2575 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2576 Expression => Relocate_Node (Call_Node)));
2577 Set_Etype (Call_Node, Etype (Control));
2578 Set_Analyzed (Call_Node);
2580 Expand_Interface_Conversion (Call_Node);
2581 end if;
2582 end;
2584 -- Expansion of a dispatching call results in an indirect call, which in
2585 -- turn causes current values to be killed (see Resolve_Call), so on VM
2586 -- targets we do the call here to ensure consistent warnings between VM
2587 -- and non-VM targets.
2589 else
2590 Kill_Current_Values;
2591 end if;
2592 end Propagate_Tag;
2594 end Sem_Disp;