[Ada] Minor tweaks to dispatching support code
[official-gcc.git] / gcc / ada / sem_disp.adb
blob79af10a81f050a00820caa142ff666e1b80b9f13
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-2022, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Util; use Exp_Util;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Tss; use Exp_Tss;
38 with Errout; use Errout;
39 with Freeze; use Freeze;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Type; use Sem_Type;
54 with Sem_Util; use Sem_Util;
55 with Snames; use Snames;
56 with Sinfo; use Sinfo;
57 with Sinfo.Nodes; use Sinfo.Nodes;
58 with Sinfo.Utils; use Sinfo.Utils;
59 with Tbuild; use Tbuild;
60 with Uintp; use Uintp;
61 with Warnsw; use Warnsw;
63 package body Sem_Disp is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Add_Dispatching_Operation
70 (Tagged_Type : Entity_Id;
71 New_Op : Entity_Id);
72 -- Add New_Op in the list of primitive operations of Tagged_Type
74 function Check_Controlling_Type
75 (T : Entity_Id;
76 Subp : Entity_Id) return Entity_Id;
77 -- T is the tagged type of a formal parameter or the result of Subp.
78 -- If the subprogram has a controlling parameter or result that matches
79 -- the type, then returns the tagged type of that parameter or result
80 -- (returning the designated tagged type in the case of an access
81 -- parameter); otherwise returns empty.
83 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
84 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
85 -- type of S that has the same name of S, a type-conformant profile, an
86 -- original corresponding operation O that is a primitive of a visible
87 -- ancestor of the dispatching type of S and O is visible at the point of
88 -- of declaration of S. If the entity is found the Alias of S is set to the
89 -- original corresponding operation S and its Overridden_Operation is set
90 -- to the found entity; otherwise return Empty.
92 -- This routine does not search for non-hidden primitives since they are
93 -- covered by the normal Ada 2005 rules.
95 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
96 -- Check whether a primitive operation is inherited from an operation
97 -- declared in the visible part of its package.
99 -------------------------------
100 -- Add_Dispatching_Operation --
101 -------------------------------
103 procedure Add_Dispatching_Operation
104 (Tagged_Type : Entity_Id;
105 New_Op : Entity_Id)
107 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
109 begin
110 -- The dispatching operation may already be on the list, if it is the
111 -- wrapper for an inherited function of a null extension (see Exp_Ch3
112 -- for the construction of function wrappers). The list of primitive
113 -- operations must not contain duplicates.
115 -- The Default_Initial_Condition and invariant procedures are not added
116 -- to the list of primitives even when they are generated for a tagged
117 -- type. These routines must not be targets of dispatching calls and
118 -- therefore must not appear in the dispatch table because they already
119 -- utilize class-wide-precondition semantics to handle inheritance and
120 -- overriding.
122 if Is_Suitable_Primitive (New_Op) then
123 Append_Unique_Elmt (New_Op, List);
124 end if;
125 end Add_Dispatching_Operation;
127 --------------------------
128 -- Covered_Interface_Op --
129 --------------------------
131 function Covered_Interface_Op (Prim : Entity_Id) return Entity_Id is
132 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
133 Elmt : Elmt_Id;
134 E : Entity_Id;
136 begin
137 pragma Assert (Is_Dispatching_Operation (Prim));
139 -- Although this is a dispatching primitive we must check if its
140 -- dispatching type is available because it may be the primitive
141 -- of a private type not defined as tagged in its partial view.
143 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
145 -- If the tagged type is frozen then the internal entities associated
146 -- with interfaces are available in the list of primitives of the
147 -- tagged type and can be used to speed up this search.
149 if Is_Frozen (Tagged_Type) then
150 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
151 while Present (Elmt) loop
152 E := Node (Elmt);
154 if Present (Interface_Alias (E))
155 and then Alias (E) = Prim
156 then
157 return Interface_Alias (E);
158 end if;
160 Next_Elmt (Elmt);
161 end loop;
163 -- Otherwise we must collect all the interface primitives and check
164 -- if the Prim overrides (implements) some interface primitive.
166 else
167 declare
168 Ifaces_List : Elist_Id;
169 Iface_Elmt : Elmt_Id;
170 Iface : Entity_Id;
171 Iface_Prim : Entity_Id;
173 begin
174 Collect_Interfaces (Tagged_Type, Ifaces_List);
175 Iface_Elmt := First_Elmt (Ifaces_List);
176 while Present (Iface_Elmt) loop
177 Iface := Node (Iface_Elmt);
179 Elmt := First_Elmt (Primitive_Operations (Iface));
180 while Present (Elmt) loop
181 Iface_Prim := Node (Elmt);
183 if Chars (Iface_Prim) = Chars (Prim)
184 and then Is_Interface_Conformant
185 (Tagged_Type, Iface_Prim, Prim)
186 then
187 return Iface_Prim;
188 end if;
190 Next_Elmt (Elmt);
191 end loop;
193 Next_Elmt (Iface_Elmt);
194 end loop;
195 end;
196 end if;
197 end if;
199 return Empty;
200 end Covered_Interface_Op;
202 ----------------------------------
203 -- Covered_Interface_Primitives --
204 ----------------------------------
206 function Covered_Interface_Primitives (Prim : Entity_Id) return Elist_Id is
207 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
208 Elmt : Elmt_Id;
209 E : Entity_Id;
210 Result : Elist_Id := No_Elist;
212 begin
213 pragma Assert (Is_Dispatching_Operation (Prim));
215 -- Although this is a dispatching primitive we must check if its
216 -- dispatching type is available because it may be the primitive
217 -- of a private type not defined as tagged in its partial view.
219 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
221 -- If the tagged type is frozen then the internal entities associated
222 -- with interfaces are available in the list of primitives of the
223 -- tagged type and can be used to speed up this search.
225 if Is_Frozen (Tagged_Type) then
226 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
227 while Present (Elmt) loop
228 E := Node (Elmt);
230 if Present (Interface_Alias (E))
231 and then Alias (E) = Prim
232 then
233 if No (Result) then
234 Result := New_Elmt_List;
235 end if;
237 Append_Elmt (Interface_Alias (E), Result);
238 end if;
240 Next_Elmt (Elmt);
241 end loop;
243 -- Otherwise we must collect all the interface primitives and check
244 -- whether the Prim overrides (implements) some interface primitive.
246 else
247 declare
248 Ifaces_List : Elist_Id;
249 Iface_Elmt : Elmt_Id;
250 Iface : Entity_Id;
251 Iface_Prim : Entity_Id;
253 begin
254 Collect_Interfaces (Tagged_Type, Ifaces_List);
256 Iface_Elmt := First_Elmt (Ifaces_List);
257 while Present (Iface_Elmt) loop
258 Iface := Node (Iface_Elmt);
260 Elmt := First_Elmt (Primitive_Operations (Iface));
261 while Present (Elmt) loop
262 Iface_Prim := Node (Elmt);
264 if Chars (Iface_Prim) = Chars (Prim)
265 and then Is_Interface_Conformant
266 (Tagged_Type, Iface_Prim, Prim)
267 then
268 if No (Result) then
269 Result := New_Elmt_List;
270 end if;
272 Append_Elmt (Iface_Prim, Result);
273 end if;
275 Next_Elmt (Elmt);
276 end loop;
278 Next_Elmt (Iface_Elmt);
279 end loop;
280 end;
281 end if;
282 end if;
284 return Result;
285 end Covered_Interface_Primitives;
287 -------------------------------
288 -- Check_Controlling_Formals --
289 -------------------------------
291 procedure Check_Controlling_Formals
292 (Typ : Entity_Id;
293 Subp : Entity_Id)
295 Formal : Entity_Id;
296 Ctrl_Type : Entity_Id;
298 begin
299 Formal := First_Formal (Subp);
300 while Present (Formal) loop
301 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
303 if Present (Ctrl_Type) then
305 -- Obtain the full type in case we are looking at an incomplete
306 -- view.
308 if Ekind (Ctrl_Type) = E_Incomplete_Type
309 and then Present (Full_View (Ctrl_Type))
310 then
311 Ctrl_Type := Full_View (Ctrl_Type);
312 end if;
314 -- When controlling type is concurrent and declared within a
315 -- generic or inside an instance use corresponding record type.
317 if Is_Concurrent_Type (Ctrl_Type)
318 and then Present (Corresponding_Record_Type (Ctrl_Type))
319 then
320 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
321 end if;
323 if Ctrl_Type = Typ then
324 Set_Is_Controlling_Formal (Formal);
326 -- Ada 2005 (AI-231): Anonymous access types that are used in
327 -- controlling parameters exclude null because it is necessary
328 -- to read the tag to dispatch, and null has no tag.
330 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
331 Set_Can_Never_Be_Null (Etype (Formal));
332 Set_Is_Known_Non_Null (Etype (Formal));
333 end if;
335 -- Check that the parameter's nominal subtype statically
336 -- matches the first subtype.
338 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
339 if not Subtypes_Statically_Match
340 (Typ, Designated_Type (Etype (Formal)))
341 then
342 Error_Msg_N
343 ("parameter subtype does not match controlling type",
344 Formal);
345 end if;
347 -- Within a predicate function, the formal may be a subtype
348 -- of a tagged type, given that the predicate is expressed
349 -- in terms of the subtype.
351 elsif not Subtypes_Statically_Match (Typ, Etype (Formal))
352 and then not Is_Predicate_Function (Subp)
353 then
354 Error_Msg_N
355 ("parameter subtype does not match controlling type",
356 Formal);
357 end if;
359 if Present (Default_Value (Formal)) then
361 -- In Ada 2005, access parameters can have defaults
363 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
364 and then Ada_Version < Ada_2005
365 then
366 Error_Msg_N
367 ("default not allowed for controlling access parameter",
368 Default_Value (Formal));
370 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
371 Error_Msg_N
372 ("default expression must be a tag indeterminate" &
373 " function call", Default_Value (Formal));
374 end if;
375 end if;
377 elsif Comes_From_Source (Subp) then
378 Error_Msg_N
379 ("operation can be dispatching in only one type", Subp);
380 end if;
381 end if;
383 Next_Formal (Formal);
384 end loop;
386 if Ekind (Subp) in E_Function | E_Generic_Function then
387 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
389 if Present (Ctrl_Type) then
390 if Ctrl_Type = Typ then
391 Set_Has_Controlling_Result (Subp);
393 -- Check that result subtype statically matches first subtype
394 -- (Ada 2005): Subp may have a controlling access result.
396 if Subtypes_Statically_Match (Typ, Etype (Subp))
397 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
398 and then
399 Subtypes_Statically_Match
400 (Typ, Designated_Type (Etype (Subp))))
401 then
402 null;
404 else
405 Error_Msg_N
406 ("result subtype does not match controlling type", Subp);
407 end if;
409 elsif Comes_From_Source (Subp) then
410 Error_Msg_N
411 ("operation can be dispatching in only one type", Subp);
412 end if;
413 end if;
414 end if;
415 end Check_Controlling_Formals;
417 ----------------------------
418 -- Check_Controlling_Type --
419 ----------------------------
421 function Check_Controlling_Type
422 (T : Entity_Id;
423 Subp : Entity_Id) return Entity_Id
425 Tagged_Type : Entity_Id := Empty;
427 begin
428 if Is_Tagged_Type (T) then
429 if Is_First_Subtype (T) then
430 Tagged_Type := T;
431 else
432 Tagged_Type := Base_Type (T);
433 end if;
435 -- If the type is incomplete, it may have been declared without a
436 -- Tagged indication, but the full view may be tagged, in which case
437 -- that is the controlling type of the subprogram. This is one of the
438 -- approx. 579 places in the language where a lookahead would help.
440 elsif Ekind (T) = E_Incomplete_Type
441 and then Present (Full_View (T))
442 and then Is_Tagged_Type (Full_View (T))
443 then
444 Set_Is_Tagged_Type (T);
445 Tagged_Type := Full_View (T);
447 elsif Ekind (T) = E_Anonymous_Access_Type
448 and then Is_Tagged_Type (Designated_Type (T))
449 then
450 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
451 if Is_First_Subtype (Designated_Type (T)) then
452 Tagged_Type := Designated_Type (T);
453 else
454 Tagged_Type := Base_Type (Designated_Type (T));
455 end if;
457 -- Ada 2005: an incomplete type can be tagged. An operation with an
458 -- access parameter of the type is dispatching.
460 elsif Scope (Designated_Type (T)) = Current_Scope then
461 Tagged_Type := Designated_Type (T);
463 -- Ada 2005 (AI-50217)
465 elsif From_Limited_With (Designated_Type (T))
466 and then Has_Non_Limited_View (Designated_Type (T))
467 and then Scope (Designated_Type (T)) = Scope (Subp)
468 then
469 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
470 Tagged_Type := Non_Limited_View (Designated_Type (T));
471 else
472 Tagged_Type := Base_Type (Non_Limited_View
473 (Designated_Type (T)));
474 end if;
475 end if;
476 end if;
478 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
479 return Empty;
481 -- In the special case of a protected subprogram of a tagged protected
482 -- type that has a formal of a tagged type (or access formal whose type
483 -- designates a tagged type), such a formal is not controlling unless
484 -- it's of the protected type's corresponding record type. The latter
485 -- can occur for the special wrapper subprograms created for protected
486 -- subprograms. Such subprograms may occur in the same scope where some
487 -- formal's tagged type is declared, and we don't want formals of that
488 -- tagged type being marked as controlling, for one thing because they
489 -- aren't controlling from the language point of view, but also because
490 -- this can cause errors for access formals when conformance is checked
491 -- between the spec and body of the protected subprogram (null-exclusion
492 -- status of the formals may be set differently, which is the case that
493 -- led to adding this check).
495 elsif Is_Subprogram (Subp)
496 and then Present (Protected_Subprogram (Subp))
497 and then Ekind (Scope (Protected_Subprogram (Subp))) = E_Protected_Type
498 and then
499 Base_Type (Tagged_Type)
500 /= Corresponding_Record_Type (Scope (Protected_Subprogram (Subp)))
501 then
502 return Empty;
504 -- The dispatching type and the primitive operation must be defined in
505 -- the same scope, except in the case of internal operations and formal
506 -- abstract subprograms.
508 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
509 and then (not Is_Generic_Type (Tagged_Type)
510 or else not Comes_From_Source (Subp)))
511 or else
512 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
513 or else
514 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
515 and then
516 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
517 and then
518 Is_Abstract_Subprogram (Subp))
519 then
520 return Tagged_Type;
522 else
523 return Empty;
524 end if;
525 end Check_Controlling_Type;
527 ----------------------------
528 -- Check_Dispatching_Call --
529 ----------------------------
531 procedure Check_Dispatching_Call (N : Node_Id) is
532 Loc : constant Source_Ptr := Sloc (N);
533 Actual : Node_Id;
534 Formal : Entity_Id;
535 Control : Node_Id := Empty;
536 Func : Entity_Id;
537 Subp_Entity : Entity_Id;
538 Indeterm_Ancestor_Call : Boolean := False;
539 Indeterm_Ctrl_Type : Entity_Id := Empty; -- init to avoid warning
541 Static_Tag : Node_Id := Empty;
542 -- If a controlling formal has a statically tagged actual, the tag of
543 -- this actual is to be used for any tag-indeterminate actual.
545 procedure Check_Direct_Call;
546 -- In the case when the controlling actual is a class-wide type whose
547 -- root type's completion is a task or protected type, the call is in
548 -- fact direct. This routine detects the above case and modifies the
549 -- call accordingly.
551 procedure Check_Dispatching_Context (Call : Node_Id);
552 -- If the call is tag-indeterminate and the entity being called is
553 -- abstract, verify that the context is a call that will eventually
554 -- provide a tag for dispatching, or has provided one already.
556 -----------------------
557 -- Check_Direct_Call --
558 -----------------------
560 procedure Check_Direct_Call is
561 Typ : Entity_Id := Etype (Control);
562 begin
563 -- Predefined primitives do not receive wrappers since they are built
564 -- from scratch for the corresponding record of synchronized types.
565 -- Equality is in general predefined, but is excluded from the check
566 -- when it is user-defined.
568 if Is_Predefined_Dispatching_Operation (Subp_Entity)
569 and then not (Is_User_Defined_Equality (Subp_Entity)
570 and then Comes_From_Source (Subp_Entity)
571 and then Nkind (Parent (Subp_Entity)) =
572 N_Function_Specification)
573 then
574 return;
575 end if;
577 if Is_Class_Wide_Type (Typ) then
578 Typ := Root_Type (Typ);
579 end if;
581 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
582 Typ := Full_View (Typ);
583 end if;
585 if Is_Concurrent_Type (Typ)
586 and then
587 Present (Corresponding_Record_Type (Typ))
588 then
589 Typ := Corresponding_Record_Type (Typ);
591 -- The concurrent record's list of primitives should contain a
592 -- wrapper for the entity of the call, retrieve it.
594 declare
595 Prim : Entity_Id;
596 Prim_Elmt : Elmt_Id;
597 Wrapper_Found : Boolean := False;
599 begin
600 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
601 while Present (Prim_Elmt) loop
602 Prim := Node (Prim_Elmt);
604 if Is_Primitive_Wrapper (Prim)
605 and then Wrapped_Entity (Prim) = Subp_Entity
606 then
607 Wrapper_Found := True;
608 exit;
609 end if;
611 Next_Elmt (Prim_Elmt);
612 end loop;
614 -- A primitive declared between two views should have a
615 -- corresponding wrapper.
617 pragma Assert (Wrapper_Found);
619 -- Modify the call by setting the proper entity
621 Set_Entity (Name (N), Prim);
622 end;
623 end if;
624 end Check_Direct_Call;
626 -------------------------------
627 -- Check_Dispatching_Context --
628 -------------------------------
630 procedure Check_Dispatching_Context (Call : Node_Id) is
631 Subp : constant Entity_Id := Entity (Name (Call));
633 procedure Abstract_Context_Error;
634 -- Error for abstract call dispatching on result is not dispatching
636 function Has_Controlling_Current_Instance_Actual_In_DIC
637 (Call : Node_Id) return Boolean;
638 -- Return True if the subprogram call Call has a controlling actual
639 -- given directly by a current instance referenced within a DIC
640 -- aspect.
642 ----------------------------
643 -- Abstract_Context_Error --
644 ----------------------------
646 procedure Abstract_Context_Error is
647 begin
648 if Ekind (Subp) = E_Function then
649 Error_Msg_N
650 ("call to abstract function must be dispatching", N);
652 -- This error can occur for a procedure in the case of a call to
653 -- an abstract formal procedure with a statically tagged operand.
655 else
656 Error_Msg_N
657 ("call to abstract procedure must be dispatching", N);
658 end if;
659 end Abstract_Context_Error;
661 ----------------------------------------
662 -- Has_Current_Instance_Actual_In_DIC --
663 ----------------------------------------
665 function Has_Controlling_Current_Instance_Actual_In_DIC
666 (Call : Node_Id) return Boolean
668 A : Node_Id;
669 F : Entity_Id;
670 begin
671 F := First_Formal (Subp_Entity);
672 A := First_Actual (Call);
674 while Present (F) loop
676 -- Return True if the actual denotes a current instance (which
677 -- will be represented by an in-mode formal of the enclosing
678 -- DIC_Procedure) passed to a controlling formal. We don't have
679 -- to worry about controlling access formals here, because its
680 -- illegal to apply Access (etc.) attributes to a current
681 -- instance within an aspect (by AI12-0068).
683 if Is_Controlling_Formal (F)
684 and then Nkind (A) = N_Identifier
685 and then Ekind (Entity (A)) = E_In_Parameter
686 and then Is_Subprogram (Scope (Entity (A)))
687 and then Is_DIC_Procedure (Scope (Entity (A)))
688 then
689 return True;
690 end if;
692 Next_Formal (F);
693 Next_Actual (A);
694 end loop;
696 return False;
697 end Has_Controlling_Current_Instance_Actual_In_DIC;
699 -- Local variables
701 Scop : constant Entity_Id := Current_Scope_No_Loops;
702 Typ : constant Entity_Id := Etype (Subp);
703 Par : Node_Id;
705 -- Start of processing for Check_Dispatching_Context
707 begin
708 -- Skip checking context of dispatching calls during preanalysis of
709 -- class-wide conditions since at that stage the expression is not
710 -- installed yet on its definite context.
712 if Inside_Class_Condition_Preanalysis then
713 return;
714 end if;
716 -- If the called subprogram is a private overriding, replace it
717 -- with its alias, which has the correct body. Verify that the
718 -- two subprograms have the same controlling type (this is not the
719 -- case for an inherited subprogram that has become abstract).
721 if Is_Abstract_Subprogram (Subp)
722 and then No (Controlling_Argument (Call))
723 then
724 if Present (Alias (Subp))
725 and then not Is_Abstract_Subprogram (Alias (Subp))
726 and then No (DTC_Entity (Subp))
727 and then Find_Dispatching_Type (Subp) =
728 Find_Dispatching_Type (Alias (Subp))
729 then
730 -- Private overriding of inherited abstract operation, call is
731 -- legal.
733 Set_Entity (Name (N), Alias (Subp));
734 return;
736 -- If this is a pre/postcondition for an abstract subprogram,
737 -- it may call another abstract function that is a primitive
738 -- of an abstract type. The call is nondispatching but will be
739 -- legal in overridings of the operation. However, if the call
740 -- is tag-indeterminate we want to continue with with the error
741 -- checking below, as this case is illegal even for abstract
742 -- subprograms (see AI12-0170).
744 -- Similarly, as per AI12-0412, a nonabstract subprogram may
745 -- have a class-wide pre/postcondition that includes a call to
746 -- an abstract primitive of the subprogram's controlling type.
747 -- Certain operations (nondispatching calls, 'Access, use as
748 -- a generic actual) applied to such a nonabstract subprogram
749 -- are illegal in the case where the type is abstract (see
750 -- RM 6.1.1(18.2/5)).
752 elsif Is_Subprogram (Scop)
753 and then not Is_Tag_Indeterminate (N)
754 and then
755 -- The context is an internally built helper or an indirect
756 -- call wrapper that handles class-wide preconditions
757 (Present (Class_Preconditions_Subprogram (Scop))
759 -- ... or the context is a class-wide pre/postcondition.
760 or else
761 (In_Pre_Post_Condition (Call, Class_Wide_Only => True)
763 -- The tagged type associated with the called
764 -- subprogram must be the same as that of the
765 -- subprogram with a class-wide aspect.
767 and then Is_Dispatching_Operation (Scop)
768 and then Find_Dispatching_Type (Subp)
769 = Find_Dispatching_Type (Scop)))
770 then
771 null;
773 -- Similarly to the dispensation for postconditions, a call to
774 -- an abstract function within a Default_Initial_Condition aspect
775 -- can be legal when passed a current instance of the type. Such
776 -- a call will be effectively mapped to a call to a primitive of
777 -- a descendant type (see AI12-0397, as well as AI12-0170), so
778 -- doesn't need to be dispatching. We test for being within a DIC
779 -- procedure, since that's where the call will be analyzed.
781 elsif Is_Subprogram (Scop)
782 and then Is_DIC_Procedure (Scop)
783 and then Has_Controlling_Current_Instance_Actual_In_DIC (Call)
784 then
785 null;
787 elsif Ekind (Current_Scope) = E_Function
788 and then Nkind (Unit_Declaration_Node (Scop)) =
789 N_Generic_Subprogram_Declaration
790 then
791 null;
793 else
794 -- We need to determine whether the context of the call
795 -- provides a tag to make the call dispatching. This requires
796 -- the call to be the actual in an enclosing call, and that
797 -- actual must be controlling. If the call is an operand of
798 -- equality, the other operand must not be abstract.
800 if not Is_Tagged_Type (Typ)
801 and then not
802 (Ekind (Typ) = E_Anonymous_Access_Type
803 and then Is_Tagged_Type (Designated_Type (Typ)))
804 then
805 Abstract_Context_Error;
806 return;
807 end if;
809 Par := Parent (Call);
811 if Nkind (Par) = N_Parameter_Association then
812 Par := Parent (Par);
813 end if;
815 if Nkind (Par) = N_Qualified_Expression
816 or else Nkind (Par) = N_Unchecked_Type_Conversion
817 then
818 Par := Parent (Par);
819 end if;
821 if Nkind (Par) in N_Subprogram_Call
822 and then Is_Entity_Name (Name (Par))
823 then
824 declare
825 Enc_Subp : constant Entity_Id := Entity (Name (Par));
826 A : Node_Id;
827 F : Entity_Id;
828 Control : Entity_Id;
829 Ret_Type : Entity_Id;
831 begin
832 -- Find controlling formal that can provide tag for the
833 -- tag-indeterminate actual. The corresponding actual
834 -- must be the corresponding class-wide type.
836 F := First_Formal (Enc_Subp);
837 A := First_Actual (Par);
839 -- Find controlling type of call. Dereference if function
840 -- returns an access type.
842 Ret_Type := Etype (Call);
843 if Is_Access_Type (Etype (Call)) then
844 Ret_Type := Designated_Type (Ret_Type);
845 end if;
847 while Present (F) loop
848 Control := Etype (A);
850 if Is_Access_Type (Control) then
851 Control := Designated_Type (Control);
852 end if;
854 if Is_Controlling_Formal (F)
855 and then not (Call = A or else Parent (Call) = A)
856 and then Control = Class_Wide_Type (Ret_Type)
857 then
858 return;
859 end if;
861 Next_Formal (F);
862 Next_Actual (A);
863 end loop;
865 if Nkind (Par) = N_Function_Call
866 and then Is_Tag_Indeterminate (Par)
867 then
868 -- The parent may be an actual of an enclosing call
870 Check_Dispatching_Context (Par);
871 return;
873 else
874 Error_Msg_N
875 ("call to abstract function must be dispatching",
876 Call);
877 return;
878 end if;
879 end;
881 -- For equality operators, one of the operands must be
882 -- statically or dynamically tagged.
884 elsif Nkind (Par) in N_Op_Eq | N_Op_Ne then
885 if N = Right_Opnd (Par)
886 and then Is_Tag_Indeterminate (Left_Opnd (Par))
887 then
888 Abstract_Context_Error;
890 elsif N = Left_Opnd (Par)
891 and then Is_Tag_Indeterminate (Right_Opnd (Par))
892 then
893 Abstract_Context_Error;
894 end if;
896 return;
898 -- The left-hand side of an assignment provides the tag
900 elsif Nkind (Par) = N_Assignment_Statement then
901 return;
903 else
904 Abstract_Context_Error;
905 end if;
906 end if;
907 end if;
908 end Check_Dispatching_Context;
910 -- Start of processing for Check_Dispatching_Call
912 begin
913 -- Find a controlling argument, if any
915 if Present (Parameter_Associations (N)) then
916 Subp_Entity := Entity (Name (N));
918 Actual := First_Actual (N);
919 Formal := First_Formal (Subp_Entity);
920 while Present (Actual) loop
921 Control := Find_Controlling_Arg (Actual);
922 exit when Present (Control);
924 -- Check for the case where the actual is a tag-indeterminate call
925 -- whose result type is different than the tagged type associated
926 -- with the containing call, but is an ancestor of the type.
928 if Is_Controlling_Formal (Formal)
929 and then Is_Tag_Indeterminate (Actual)
930 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
931 and then Is_Ancestor (Etype (Actual), Etype (Formal))
932 then
933 Indeterm_Ancestor_Call := True;
934 Indeterm_Ctrl_Type := Etype (Formal);
936 -- If the formal is controlling but the actual is not, the type
937 -- of the actual is statically known, and may be used as the
938 -- controlling tag for some other tag-indeterminate actual.
940 elsif Is_Controlling_Formal (Formal)
941 and then Is_Entity_Name (Actual)
942 and then Is_Tagged_Type (Etype (Actual))
943 then
944 Static_Tag := Actual;
945 end if;
947 Next_Actual (Actual);
948 Next_Formal (Formal);
949 end loop;
951 -- If the call doesn't have a controlling actual but does have an
952 -- indeterminate actual that requires dispatching treatment, then an
953 -- object is needed that will serve as the controlling argument for
954 -- a dispatching call on the indeterminate actual. This can occur
955 -- in the unusual situation of a default actual given by a tag-
956 -- indeterminate call and where the type of the call is an ancestor
957 -- of the type associated with a containing call to an inherited
958 -- operation (see AI-239).
960 -- Rather than create an object of the tagged type, which would
961 -- be problematic for various reasons (default initialization,
962 -- discriminants), the tag of the containing call's associated
963 -- tagged type is directly used to control the dispatching.
965 if No (Control)
966 and then Indeterm_Ancestor_Call
967 and then No (Static_Tag)
968 then
969 Control :=
970 Make_Attribute_Reference (Loc,
971 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
972 Attribute_Name => Name_Tag);
974 Analyze (Control);
975 end if;
977 if Present (Control) then
979 -- Verify that no controlling arguments are statically tagged
981 if Debug_Flag_E then
982 Write_Str ("Found Dispatching call");
983 Write_Int (Int (N));
984 Write_Eol;
985 end if;
987 Actual := First_Actual (N);
988 while Present (Actual) loop
989 if Actual /= Control then
991 if not Is_Controlling_Actual (Actual) then
992 null; -- Can be anything
994 elsif Is_Dynamically_Tagged (Actual) then
995 null; -- Valid parameter
997 elsif Is_Tag_Indeterminate (Actual) then
999 -- The tag is inherited from the enclosing call (the node
1000 -- we are currently analyzing). Explicitly expand the
1001 -- actual, since the previous call to Expand (from
1002 -- Resolve_Call) had no way of knowing about the
1003 -- required dispatching.
1005 Propagate_Tag (Control, Actual);
1007 else
1008 Error_Msg_N
1009 ("controlling argument is not dynamically tagged",
1010 Actual);
1011 return;
1012 end if;
1013 end if;
1015 Next_Actual (Actual);
1016 end loop;
1018 -- Mark call as a dispatching call
1020 Set_Controlling_Argument (N, Control);
1021 Check_Restriction (No_Dispatching_Calls, N);
1023 -- The dispatching call may need to be converted into a direct
1024 -- call in certain cases.
1026 Check_Direct_Call;
1028 -- If there is a statically tagged actual and a tag-indeterminate
1029 -- call to a function of the ancestor (such as that provided by a
1030 -- default), then treat this as a dispatching call and propagate
1031 -- the tag to the tag-indeterminate call(s).
1033 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
1034 Control :=
1035 Make_Attribute_Reference (Loc,
1036 Prefix =>
1037 New_Occurrence_Of (Etype (Static_Tag), Loc),
1038 Attribute_Name => Name_Tag);
1040 Analyze (Control);
1042 Actual := First_Actual (N);
1043 Formal := First_Formal (Subp_Entity);
1044 while Present (Actual) loop
1045 if Is_Tag_Indeterminate (Actual)
1046 and then Is_Controlling_Formal (Formal)
1047 then
1048 Propagate_Tag (Control, Actual);
1049 end if;
1051 Next_Actual (Actual);
1052 Next_Formal (Formal);
1053 end loop;
1055 Check_Dispatching_Context (N);
1057 elsif Nkind (N) /= N_Function_Call then
1059 -- The call is not dispatching, so check that there aren't any
1060 -- tag-indeterminate abstract calls left among its actuals.
1062 Actual := First_Actual (N);
1063 while Present (Actual) loop
1064 if Is_Tag_Indeterminate (Actual) then
1066 -- Function call case
1068 if Nkind (Original_Node (Actual)) = N_Function_Call then
1069 Func := Entity (Name (Original_Node (Actual)));
1071 -- If the actual is an attribute then it can't be abstract
1072 -- (the only current case of a tag-indeterminate attribute
1073 -- is the stream Input attribute).
1075 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
1076 then
1077 Func := Empty;
1079 -- Ditto if it is an explicit dereference
1081 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
1082 then
1083 Func := Empty;
1085 -- Only other possibility is a qualified expression whose
1086 -- constituent expression is itself a call.
1088 else
1089 Func :=
1090 Entity (Name (Original_Node
1091 (Expression (Original_Node (Actual)))));
1092 end if;
1094 if Present (Func) and then Is_Abstract_Subprogram (Func) then
1095 Error_Msg_N
1096 ("call to abstract function must be dispatching",
1097 Actual);
1098 end if;
1099 end if;
1101 Next_Actual (Actual);
1102 end loop;
1104 Check_Dispatching_Context (N);
1106 elsif Nkind (Parent (N)) in N_Subexpr then
1107 Check_Dispatching_Context (N);
1109 elsif Nkind (Parent (N)) = N_Assignment_Statement
1110 and then Is_Class_Wide_Type (Etype (Name (Parent (N))))
1111 then
1112 return;
1114 elsif Is_Abstract_Subprogram (Subp_Entity) then
1115 Check_Dispatching_Context (N);
1116 return;
1117 end if;
1119 -- If this is a nondispatching call to a nonabstract subprogram
1120 -- and the subprogram has any Pre'Class or Post'Class aspects with
1121 -- nonstatic values, then report an error. This is specified by
1122 -- RM 6.1.1(18.2/5) (by AI12-0412).
1124 -- Skip reporting this error on helpers and indirect-call wrappers
1125 -- built to support class-wide preconditions.
1127 if No (Control)
1128 and then not Is_Abstract_Subprogram (Subp_Entity)
1129 and then
1130 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Subp_Entity)
1131 and then not
1132 (Is_Subprogram (Current_Scope)
1133 and then
1134 Present (Class_Preconditions_Subprogram (Current_Scope)))
1135 then
1136 Error_Msg_N
1137 ("nondispatching call to nonabstract subprogram of "
1138 & "abstract type with nonstatic class-wide "
1139 & "pre/postconditions",
1141 end if;
1143 else
1144 -- If dispatching on result, the enclosing call, if any, will
1145 -- determine the controlling argument. Otherwise this is the
1146 -- primitive operation of the root type.
1148 Check_Dispatching_Context (N);
1149 end if;
1150 end Check_Dispatching_Call;
1152 ---------------------------------
1153 -- Check_Dispatching_Operation --
1154 ---------------------------------
1156 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
1157 function Is_Access_To_Subprogram_Wrapper (E : Entity_Id) return Boolean;
1158 -- Return True if E is an access to subprogram wrapper
1160 procedure Warn_On_Late_Primitive_After_Private_Extension
1161 (Typ : Entity_Id;
1162 Prim : Entity_Id);
1163 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
1164 -- if it is a public primitive defined after some private extension of
1165 -- the tagged type.
1167 -------------------------------------
1168 -- Is_Access_To_Subprogram_Wrapper --
1169 -------------------------------------
1171 function Is_Access_To_Subprogram_Wrapper (E : Entity_Id) return Boolean
1173 Decl_N : constant Node_Id := Unit_Declaration_Node (E);
1174 Par_N : constant Node_Id := Parent (List_Containing (Decl_N));
1176 begin
1177 -- Access to subprogram wrappers are declared in the freezing actions
1179 return Nkind (Par_N) = N_Freeze_Entity
1180 and then Ekind (Entity (Par_N)) = E_Access_Subprogram_Type;
1181 end Is_Access_To_Subprogram_Wrapper;
1183 ----------------------------------------------------
1184 -- Warn_On_Late_Primitive_After_Private_Extension --
1185 ----------------------------------------------------
1187 procedure Warn_On_Late_Primitive_After_Private_Extension
1188 (Typ : Entity_Id;
1189 Prim : Entity_Id)
1191 E : Entity_Id;
1193 begin
1194 if Warn_On_Late_Primitives
1195 and then Comes_From_Source (Prim)
1196 and then Has_Private_Extension (Typ)
1197 and then Is_Package_Or_Generic_Package (Current_Scope)
1198 and then not In_Private_Part (Current_Scope)
1199 then
1200 E := Next_Entity (Typ);
1202 while E /= Prim loop
1203 if Ekind (E) = E_Record_Type_With_Private
1204 and then Etype (E) = Typ
1205 then
1206 Error_Msg_Name_1 := Chars (Typ);
1207 Error_Msg_Name_2 := Chars (E);
1208 Error_Msg_Sloc := Sloc (E);
1209 Error_Msg_N
1210 ("?.j?primitive of type % defined after private extension "
1211 & "% #?", Prim);
1212 Error_Msg_Name_1 := Chars (Prim);
1213 Error_Msg_Name_2 := Chars (E);
1214 Error_Msg_N
1215 ("\spec of % should appear before declaration of type %!",
1216 Prim);
1217 exit;
1218 end if;
1220 Next_Entity (E);
1221 end loop;
1222 end if;
1223 end Warn_On_Late_Primitive_After_Private_Extension;
1225 -- Local variables
1227 Body_Is_Last_Primitive : Boolean := False;
1228 Has_Dispatching_Parent : Boolean := False;
1229 Ovr_Subp : Entity_Id := Empty;
1230 Tagged_Type : Entity_Id;
1232 -- Start of processing for Check_Dispatching_Operation
1234 begin
1235 if Ekind (Subp) not in E_Function | E_Procedure then
1236 return;
1238 -- The Default_Initial_Condition procedure is not a primitive subprogram
1239 -- even if it relates to a tagged type. This routine is not meant to be
1240 -- inherited or overridden.
1242 elsif Is_DIC_Procedure (Subp) then
1243 return;
1245 -- The "partial" and "full" type invariant procedures are not primitive
1246 -- subprograms even if they relate to a tagged type. These routines are
1247 -- not meant to be inherited or overridden.
1249 elsif Is_Invariant_Procedure (Subp)
1250 or else Is_Partial_Invariant_Procedure (Subp)
1251 then
1252 return;
1254 -- Wrappers of access to subprograms are not primitive subprograms.
1256 elsif Is_Wrapper (Subp)
1257 and then Is_Access_To_Subprogram_Wrapper (Subp)
1258 then
1259 return;
1260 end if;
1262 Set_Is_Dispatching_Operation (Subp, False);
1263 Tagged_Type := Find_Dispatching_Type (Subp);
1265 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1266 -- Required because primitives of concurrent types are attached
1267 -- to the corresponding record (not to the concurrent type).
1269 if Ada_Version >= Ada_2005
1270 and then Present (Tagged_Type)
1271 and then Is_Concurrent_Type (Tagged_Type)
1272 and then Present (Corresponding_Record_Type (Tagged_Type))
1273 then
1274 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
1275 end if;
1277 -- (AI-345): The task body procedure is not a primitive of the tagged
1278 -- type
1280 if Present (Tagged_Type)
1281 and then Is_Concurrent_Record_Type (Tagged_Type)
1282 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
1283 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
1284 and then Subp = Get_Task_Body_Procedure
1285 (Corresponding_Concurrent_Type (Tagged_Type))
1286 then
1287 return;
1288 end if;
1290 -- If Subp is derived from a dispatching operation then it should
1291 -- always be treated as dispatching. In this case various checks
1292 -- below will be bypassed. Makes sure that late declarations for
1293 -- inherited private subprograms are treated as dispatching, even
1294 -- if the associated tagged type is already frozen.
1296 Has_Dispatching_Parent :=
1297 Present (Alias (Subp))
1298 and then Is_Dispatching_Operation (Alias (Subp));
1300 if No (Tagged_Type) then
1302 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1303 -- with an abstract interface type unless the interface acts as a
1304 -- parent type in a derivation. If the interface type is a formal
1305 -- type then the operation is not primitive and therefore legal.
1307 declare
1308 E : Entity_Id;
1309 Typ : Entity_Id;
1311 begin
1312 E := First_Entity (Subp);
1313 while Present (E) loop
1315 -- For an access parameter, check designated type
1317 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
1318 Typ := Designated_Type (Etype (E));
1319 else
1320 Typ := Etype (E);
1321 end if;
1323 if Comes_From_Source (Subp)
1324 and then Is_Interface (Typ)
1325 and then not Is_Class_Wide_Type (Typ)
1326 and then not Is_Derived_Type (Typ)
1327 and then not Is_Generic_Type (Typ)
1328 and then not In_Instance
1329 then
1330 Error_Msg_N ("??declaration of& is too late!", Subp);
1331 Error_Msg_NE -- CODEFIX??
1332 ("\??spec should appear immediately after declaration of "
1333 & "& !", Subp, Typ);
1334 exit;
1335 end if;
1337 Next_Entity (E);
1338 end loop;
1340 -- In case of functions check also the result type
1342 if Ekind (Subp) = E_Function then
1343 if Is_Access_Type (Etype (Subp)) then
1344 Typ := Designated_Type (Etype (Subp));
1345 else
1346 Typ := Etype (Subp);
1347 end if;
1349 -- The following should be better commented, especially since
1350 -- we just added several new conditions here ???
1352 if Comes_From_Source (Subp)
1353 and then Is_Interface (Typ)
1354 and then not Is_Class_Wide_Type (Typ)
1355 and then not Is_Derived_Type (Typ)
1356 and then not Is_Generic_Type (Typ)
1357 and then not In_Instance
1358 then
1359 Error_Msg_N ("??declaration of& is too late!", Subp);
1360 Error_Msg_NE
1361 ("\??spec should appear immediately after declaration of "
1362 & "& !", Subp, Typ);
1363 end if;
1364 end if;
1365 end;
1367 return;
1369 -- The subprograms build internally after the freezing point (such as
1370 -- init procs, interface thunks, type support subprograms, and Offset
1371 -- to top functions for accessing interface components in variable
1372 -- size tagged types) are not primitives.
1374 elsif Is_Frozen (Tagged_Type)
1375 and then not Comes_From_Source (Subp)
1376 and then not Has_Dispatching_Parent
1377 then
1378 -- Complete decoration of internally built subprograms that override
1379 -- a dispatching primitive. These entities correspond with the
1380 -- following cases:
1382 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1383 -- to override functions of nonabstract null extensions. These
1384 -- primitives were added to the list of primitives of the tagged
1385 -- type by Make_Controlling_Function_Wrappers. However, attribute
1386 -- Is_Dispatching_Operation must be set to true.
1388 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1389 -- primitives.
1391 -- 3. Subprograms associated with stream attributes (built by
1392 -- New_Stream_Subprogram) or with the Put_Image attribute.
1394 -- 4. Wrappers built for inherited operations with inherited class-
1395 -- wide conditions, where the conditions include calls to other
1396 -- overridden primitives. The wrappers include checks on these
1397 -- modified conditions. (AI12-113).
1399 -- 5. Declarations built for subprograms without separate specs that
1400 -- are eligible for inlining in GNATprove (inside
1401 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1403 if Present (Old_Subp)
1404 and then Present (Overridden_Operation (Subp))
1405 and then Is_Dispatching_Operation (Old_Subp)
1406 then
1407 pragma Assert
1408 ((Ekind (Subp) = E_Function
1409 and then Is_Dispatching_Operation (Old_Subp)
1410 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1412 or else
1413 (Ekind (Subp) = E_Procedure
1414 and then Is_Dispatching_Operation (Old_Subp)
1415 and then Present (Alias (Old_Subp))
1416 and then Is_Null_Interface_Primitive
1417 (Ultimate_Alias (Old_Subp)))
1419 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1420 or else Get_TSS_Name (Subp) = TSS_Stream_Write
1421 or else Get_TSS_Name (Subp) = TSS_Put_Image
1423 or else
1424 (Is_Wrapper (Subp)
1425 and then Present (LSP_Subprogram (Subp)))
1427 or else GNATprove_Mode);
1429 Check_Controlling_Formals (Tagged_Type, Subp);
1430 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1431 Set_Is_Dispatching_Operation (Subp);
1432 end if;
1434 return;
1436 -- The operation may be a child unit, whose scope is the defining
1437 -- package, but which is not a primitive operation of the type.
1439 elsif Is_Child_Unit (Subp) then
1440 return;
1442 -- If the subprogram is not defined in a package spec, the only case
1443 -- where it can be a dispatching op is when it overrides an operation
1444 -- before the freezing point of the type.
1446 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1447 or else In_Package_Body (Scope (Subp)))
1448 and then not Has_Dispatching_Parent
1449 then
1450 if not Comes_From_Source (Subp)
1451 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1452 then
1453 null;
1455 -- If the type is already frozen, the overriding is not allowed
1456 -- except when Old_Subp is not a dispatching operation (which can
1457 -- occur when Old_Subp was inherited by an untagged type). However,
1458 -- a body with no previous spec freezes the type *after* its
1459 -- declaration, and therefore is a legal overriding (unless the type
1460 -- has already been frozen). Only the first such body is legal.
1462 elsif Present (Old_Subp)
1463 and then Is_Dispatching_Operation (Old_Subp)
1464 then
1465 if Comes_From_Source (Subp)
1466 and then
1467 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1468 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1469 then
1470 declare
1471 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1472 Decl_Item : Node_Id;
1474 begin
1475 -- ??? The checks here for whether the type has been frozen
1476 -- prior to the new body are not complete. It's not simple
1477 -- to check frozenness at this point since the body has
1478 -- already caused the type to be prematurely frozen in
1479 -- Analyze_Declarations, but we're forced to recheck this
1480 -- here because of the odd rule interpretation that allows
1481 -- the overriding if the type wasn't frozen prior to the
1482 -- body. The freezing action should probably be delayed
1483 -- until after the spec is seen, but that's a tricky
1484 -- change to the delicate freezing code.
1486 -- Look at each declaration following the type up until the
1487 -- new subprogram body. If any of the declarations is a body
1488 -- then the type has been frozen already so the overriding
1489 -- primitive is illegal.
1491 Decl_Item := Next (Parent (Tagged_Type));
1492 while Present (Decl_Item)
1493 and then (Decl_Item /= Subp_Body)
1494 loop
1495 if Comes_From_Source (Decl_Item)
1496 and then (Nkind (Decl_Item) in N_Proper_Body
1497 or else Nkind (Decl_Item) in N_Body_Stub)
1498 then
1499 Error_Msg_N ("overriding of& is too late!", Subp);
1500 Error_Msg_N
1501 ("\spec should appear immediately after the type!",
1502 Subp);
1503 exit;
1504 end if;
1506 Next (Decl_Item);
1507 end loop;
1509 -- If the subprogram doesn't follow in the list of
1510 -- declarations including the type then the type has
1511 -- definitely been frozen already and the body is illegal.
1513 if No (Decl_Item) then
1514 Error_Msg_N ("overriding of& is too late!", Subp);
1515 Error_Msg_N
1516 ("\spec should appear immediately after the type!",
1517 Subp);
1519 elsif Is_Frozen (Subp) then
1521 -- The subprogram body declares a primitive operation.
1522 -- If the subprogram is already frozen, we must update
1523 -- its dispatching information explicitly here. The
1524 -- information is taken from the overridden subprogram.
1525 -- We must also generate a cross-reference entry because
1526 -- references to other primitives were already created
1527 -- when type was frozen.
1529 Body_Is_Last_Primitive := True;
1531 if Present (DTC_Entity (Old_Subp)) then
1532 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1533 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1535 if not Restriction_Active (No_Dispatching_Calls) then
1536 if Building_Static_DT (Tagged_Type) then
1538 -- If the static dispatch table has not been
1539 -- built then there is nothing else to do now;
1540 -- otherwise we notify that we cannot build the
1541 -- static dispatch table.
1543 if Has_Dispatch_Table (Tagged_Type) then
1544 Error_Msg_N
1545 ("overriding of& is too late for building "
1546 & " static dispatch tables!", Subp);
1547 Error_Msg_N
1548 ("\spec should appear immediately after "
1549 & "the type!", Subp);
1550 end if;
1552 -- No code required to register primitives in VM
1553 -- targets
1555 elsif not Tagged_Type_Expansion then
1556 null;
1558 else
1559 Insert_Actions_After (Subp_Body,
1560 Register_Primitive (Sloc (Subp_Body),
1561 Prim => Subp));
1562 end if;
1564 -- Indicate that this is an overriding operation,
1565 -- and replace the overridden entry in the list of
1566 -- primitive operations, which is used for xref
1567 -- generation subsequently.
1569 Generate_Reference (Tagged_Type, Subp, 'P', False);
1570 Override_Dispatching_Operation
1571 (Tagged_Type, Old_Subp, Subp);
1572 Set_Is_Dispatching_Operation (Subp);
1574 -- Inherit decoration of controlling formals and
1575 -- controlling result.
1577 if Ekind (Old_Subp) = E_Function
1578 and then Has_Controlling_Result (Old_Subp)
1579 then
1580 Set_Has_Controlling_Result (Subp);
1581 end if;
1583 if Present (First_Formal (Old_Subp)) then
1584 declare
1585 Old_Formal : Entity_Id;
1586 Formal : Entity_Id;
1588 begin
1589 Formal := First_Formal (Subp);
1590 Old_Formal := First_Formal (Old_Subp);
1592 while Present (Old_Formal) loop
1593 Set_Is_Controlling_Formal (Formal,
1594 Is_Controlling_Formal (Old_Formal));
1596 Next_Formal (Formal);
1597 Next_Formal (Old_Formal);
1598 end loop;
1599 end;
1600 end if;
1601 end if;
1603 Check_Inherited_Conditions (Tagged_Type,
1604 Late_Overriding => True);
1605 end if;
1606 end if;
1607 end;
1609 else
1610 Error_Msg_N ("overriding of& is too late!", Subp);
1611 Error_Msg_N
1612 ("\subprogram spec should appear immediately after the type!",
1613 Subp);
1614 end if;
1616 -- If the type is not frozen yet and we are not in the overriding
1617 -- case it looks suspiciously like an attempt to define a primitive
1618 -- operation, which requires the declaration to be in a package spec
1619 -- (3.2.3(6)). Only report cases where the type and subprogram are
1620 -- in the same declaration list (by checking the enclosing parent
1621 -- declarations), to avoid spurious warnings on subprograms in
1622 -- instance bodies when the type is declared in the instance spec
1623 -- but hasn't been frozen by the instance body.
1625 elsif not Is_Frozen (Tagged_Type)
1626 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1627 then
1628 Error_Msg_N
1629 ("??not dispatching (must be defined in a package spec)", Subp);
1630 return;
1632 -- When the type is frozen, it is legitimate to define a new
1633 -- non-primitive operation.
1635 else
1636 return;
1637 end if;
1639 -- Now, we are sure that the scope is a package spec. If the subprogram
1640 -- is declared after the freezing point of the type that's an error
1642 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1643 Error_Msg_N ("this primitive operation is declared too late", Subp);
1644 Error_Msg_NE
1645 ("??no primitive operations for& after this line",
1646 Freeze_Node (Tagged_Type),
1647 Tagged_Type);
1648 return;
1649 end if;
1651 Check_Controlling_Formals (Tagged_Type, Subp);
1653 Ovr_Subp := Old_Subp;
1655 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1656 -- overridden by Subp. This only applies to source subprograms, and
1657 -- their declaration must carry an explicit overriding indicator.
1659 if No (Ovr_Subp)
1660 and then Ada_Version >= Ada_2012
1661 and then Comes_From_Source (Subp)
1662 and then
1663 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1664 then
1665 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1667 -- Verify that the proper overriding indicator has been supplied.
1669 if Present (Ovr_Subp)
1670 and then
1671 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1672 then
1673 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1674 end if;
1675 end if;
1677 -- Now it should be a correct primitive operation, put it in the list
1679 if Present (Ovr_Subp) then
1681 -- If the type has interfaces we complete this check after we set
1682 -- attribute Is_Dispatching_Operation.
1684 Check_Subtype_Conformant (Subp, Ovr_Subp);
1686 -- A primitive operation with the name of a primitive controlled
1687 -- operation does not override a non-visible overriding controlled
1688 -- operation, i.e. one declared in a private part when the full
1689 -- view of a type is controlled. Conversely, it will override a
1690 -- visible operation that may be declared in a partial view when
1691 -- the full view is controlled.
1693 if Chars (Subp) in Name_Initialize | Name_Adjust | Name_Finalize
1694 and then Is_Controlled (Tagged_Type)
1695 and then not Is_Visibly_Controlled (Tagged_Type)
1696 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1697 then
1698 Set_Overridden_Operation (Subp, Empty);
1700 -- If the subprogram specification carries an overriding
1701 -- indicator, no need for the warning: it is either redundant,
1702 -- or else an error will be reported.
1704 if Nkind (Parent (Subp)) = N_Procedure_Specification
1705 and then
1706 (Must_Override (Parent (Subp))
1707 or else Must_Not_Override (Parent (Subp)))
1708 then
1709 null;
1711 -- Here we need the warning
1713 else
1714 Error_Msg_NE
1715 ("operation does not override inherited&??", Subp, Subp);
1716 end if;
1718 else
1719 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1721 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1722 -- that covers abstract interface subprograms we must register it
1723 -- in all the secondary dispatch tables associated with abstract
1724 -- interfaces. We do this now only if not building static tables,
1725 -- nor when the expander is inactive (we avoid trying to register
1726 -- primitives in semantics-only mode, since the type may not have
1727 -- an associated dispatch table). Otherwise the patch code is
1728 -- emitted after those tables are built, to prevent access before
1729 -- elaboration in gigi.
1731 if Body_Is_Last_Primitive
1732 and then not Building_Static_DT (Tagged_Type)
1733 and then Expander_Active
1734 and then Tagged_Type_Expansion
1735 then
1736 declare
1737 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1738 Elmt : Elmt_Id;
1739 Prim : Node_Id;
1741 begin
1742 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1743 while Present (Elmt) loop
1744 Prim := Node (Elmt);
1746 if Present (Alias (Prim))
1747 and then Present (Interface_Alias (Prim))
1748 and then Alias (Prim) = Subp
1749 then
1750 Insert_Actions_After (Subp_Body,
1751 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1752 end if;
1754 Next_Elmt (Elmt);
1755 end loop;
1757 -- Redisplay the contents of the updated dispatch table
1759 if Debug_Flag_ZZ then
1760 Write_Str ("Late overriding: ");
1761 Write_DT (Tagged_Type);
1762 end if;
1763 end;
1764 end if;
1765 end if;
1767 -- If no old subprogram, then we add this as a dispatching operation,
1768 -- but we avoid doing this if an error was posted, to prevent annoying
1769 -- cascaded errors.
1771 elsif not Error_Posted (Subp) then
1772 Add_Dispatching_Operation (Tagged_Type, Subp);
1773 end if;
1775 Set_Is_Dispatching_Operation (Subp, True);
1777 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1778 -- subtype conformance against all the interfaces covered by this
1779 -- primitive.
1781 if Present (Ovr_Subp)
1782 and then Has_Interfaces (Tagged_Type)
1783 then
1784 declare
1785 Ifaces_List : Elist_Id;
1786 Iface_Elmt : Elmt_Id;
1787 Iface_Prim_Elmt : Elmt_Id;
1788 Iface_Prim : Entity_Id;
1789 Ret_Typ : Entity_Id;
1791 begin
1792 Collect_Interfaces (Tagged_Type, Ifaces_List);
1794 Iface_Elmt := First_Elmt (Ifaces_List);
1795 while Present (Iface_Elmt) loop
1796 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1797 Iface_Prim_Elmt :=
1798 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1799 while Present (Iface_Prim_Elmt) loop
1800 Iface_Prim := Node (Iface_Prim_Elmt);
1802 if Is_Interface_Conformant
1803 (Tagged_Type, Iface_Prim, Subp)
1804 then
1805 -- Handle procedures, functions whose return type
1806 -- matches, or functions not returning interfaces
1808 if Ekind (Subp) = E_Procedure
1809 or else Etype (Iface_Prim) = Etype (Subp)
1810 or else not Is_Interface (Etype (Iface_Prim))
1811 then
1812 Check_Subtype_Conformant
1813 (New_Id => Subp,
1814 Old_Id => Iface_Prim,
1815 Err_Loc => Subp,
1816 Skip_Controlling_Formals => True);
1818 -- Handle functions returning interfaces
1820 elsif Implements_Interface
1821 (Etype (Subp), Etype (Iface_Prim))
1822 then
1823 -- Temporarily force both entities to return the
1824 -- same type. Required because Subtype_Conformant
1825 -- does not handle this case.
1827 Ret_Typ := Etype (Iface_Prim);
1828 Set_Etype (Iface_Prim, Etype (Subp));
1830 Check_Subtype_Conformant
1831 (New_Id => Subp,
1832 Old_Id => Iface_Prim,
1833 Err_Loc => Subp,
1834 Skip_Controlling_Formals => True);
1836 Set_Etype (Iface_Prim, Ret_Typ);
1837 end if;
1838 end if;
1840 Next_Elmt (Iface_Prim_Elmt);
1841 end loop;
1842 end if;
1844 Next_Elmt (Iface_Elmt);
1845 end loop;
1846 end;
1847 end if;
1849 if not Body_Is_Last_Primitive then
1850 Set_DT_Position_Value (Subp, No_Uint);
1852 elsif Has_Controlled_Component (Tagged_Type)
1853 and then Chars (Subp) in Name_Initialize
1854 | Name_Adjust
1855 | Name_Finalize
1856 | Name_Finalize_Address
1857 then
1858 declare
1859 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1860 Decl : Node_Id;
1861 Old_P : Entity_Id;
1862 Old_Bod : Node_Id;
1863 Old_Spec : Entity_Id;
1865 C_Names : constant array (1 .. 4) of Name_Id :=
1866 (Name_Initialize,
1867 Name_Adjust,
1868 Name_Finalize,
1869 Name_Finalize_Address);
1871 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1872 (TSS_Deep_Initialize,
1873 TSS_Deep_Adjust,
1874 TSS_Deep_Finalize,
1875 TSS_Finalize_Address);
1877 begin
1878 -- Remove previous controlled function which was constructed and
1879 -- analyzed when the type was frozen. This requires removing the
1880 -- body of the redefined primitive, as well as its specification
1881 -- if needed (there is no spec created for Deep_Initialize, see
1882 -- exp_ch3.adb). We must also dismantle the exception information
1883 -- that may have been generated for it when front end zero-cost
1884 -- tables are enabled.
1886 for J in D_Names'Range loop
1887 Old_P := TSS (Tagged_Type, D_Names (J));
1889 if Present (Old_P)
1890 and then Chars (Subp) = C_Names (J)
1891 then
1892 Old_Bod := Unit_Declaration_Node (Old_P);
1893 Remove (Old_Bod);
1894 Set_Is_Eliminated (Old_P);
1895 Set_Scope (Old_P, Scope (Current_Scope));
1897 if Nkind (Old_Bod) = N_Subprogram_Body
1898 and then Present (Corresponding_Spec (Old_Bod))
1899 then
1900 Old_Spec := Corresponding_Spec (Old_Bod);
1901 Set_Has_Completion (Old_Spec, False);
1902 end if;
1903 end if;
1904 end loop;
1906 Build_Late_Proc (Tagged_Type, Chars (Subp));
1908 -- The new operation is added to the actions of the freeze node
1909 -- for the type, but this node has already been analyzed, so we
1910 -- must retrieve and analyze explicitly the new body.
1912 if Present (F_Node)
1913 and then Present (Actions (F_Node))
1914 then
1915 Decl := Last (Actions (F_Node));
1916 Analyze (Decl);
1917 end if;
1918 end;
1919 end if;
1921 -- AI12-0279: If the Yield aspect is specified for a dispatching
1922 -- subprogram that inherits the aspect, the specified value shall
1923 -- be confirming.
1925 if Is_Dispatching_Operation (Subp)
1926 and then Is_Primitive_Wrapper (Subp)
1927 and then Present (Wrapped_Entity (Subp))
1928 and then Comes_From_Source (Wrapped_Entity (Subp))
1929 and then Present (Overridden_Operation (Subp))
1930 and then Has_Yield_Aspect (Overridden_Operation (Subp))
1931 /= Has_Yield_Aspect (Wrapped_Entity (Subp))
1932 then
1933 declare
1934 W_Ent : constant Entity_Id := Wrapped_Entity (Subp);
1935 W_Decl : constant Node_Id := Parent (W_Ent);
1936 Asp : Node_Id;
1938 begin
1939 if Present (Aspect_Specifications (W_Decl)) then
1940 Asp := First (Aspect_Specifications (W_Decl));
1941 while Present (Asp) loop
1942 if Chars (Identifier (Asp)) = Name_Yield then
1943 Error_Msg_Name_1 := Name_Yield;
1944 Error_Msg_N
1945 ("specification of inherited aspect% can only confirm "
1946 & "parent value", Asp);
1947 end if;
1949 Next (Asp);
1950 end loop;
1951 end if;
1953 Set_Has_Yield_Aspect (Wrapped_Entity (Subp));
1954 end;
1955 end if;
1957 -- For similarity with record extensions, in Ada 9X the language should
1958 -- have disallowed adding visible operations to a tagged type after
1959 -- deriving a private extension from it. Report a warning if this
1960 -- primitive is defined after a private extension of Tagged_Type.
1962 Warn_On_Late_Primitive_After_Private_Extension (Tagged_Type, Subp);
1963 end Check_Dispatching_Operation;
1965 ------------------------------------------
1966 -- Check_Operation_From_Incomplete_Type --
1967 ------------------------------------------
1969 procedure Check_Operation_From_Incomplete_Type
1970 (Subp : Entity_Id;
1971 Typ : Entity_Id)
1973 Full : constant Entity_Id := Full_View (Typ);
1974 Parent_Typ : constant Entity_Id := Etype (Full);
1975 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1976 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1977 Op1, Op2 : Elmt_Id;
1978 Prev : Elmt_Id := No_Elmt;
1980 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1981 -- Check that Subp has profile of an operation derived from Parent_Subp.
1982 -- Subp must have a parameter or result type that is Typ or an access
1983 -- parameter or access result type that designates Typ.
1985 ------------------
1986 -- Derives_From --
1987 ------------------
1989 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1990 F1, F2 : Entity_Id;
1992 begin
1993 if Chars (Parent_Subp) /= Chars (Subp) then
1994 return False;
1995 end if;
1997 -- Check that the type of controlling formals is derived from the
1998 -- parent subprogram's controlling formal type (or designated type
1999 -- if the formal type is an anonymous access type).
2001 F1 := First_Formal (Parent_Subp);
2002 F2 := First_Formal (Subp);
2003 while Present (F1) and then Present (F2) loop
2004 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
2005 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
2006 return False;
2007 elsif Designated_Type (Etype (F1)) = Parent_Typ
2008 and then Designated_Type (Etype (F2)) /= Full
2009 then
2010 return False;
2011 end if;
2013 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
2014 return False;
2016 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
2017 return False;
2018 end if;
2020 Next_Formal (F1);
2021 Next_Formal (F2);
2022 end loop;
2024 -- Check that a controlling result type is derived from the parent
2025 -- subprogram's result type (or designated type if the result type
2026 -- is an anonymous access type).
2028 if Ekind (Parent_Subp) = E_Function then
2029 if Ekind (Subp) /= E_Function then
2030 return False;
2032 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
2033 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
2034 return False;
2036 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
2037 and then Designated_Type (Etype (Subp)) /= Full
2038 then
2039 return False;
2040 end if;
2042 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
2043 return False;
2045 elsif Etype (Parent_Subp) = Parent_Typ
2046 and then Etype (Subp) /= Full
2047 then
2048 return False;
2049 end if;
2051 elsif Ekind (Subp) = E_Function then
2052 return False;
2053 end if;
2055 return No (F1) and then No (F2);
2056 end Derives_From;
2058 -- Start of processing for Check_Operation_From_Incomplete_Type
2060 begin
2061 -- The operation may override an inherited one, or may be a new one
2062 -- altogether. The inherited operation will have been hidden by the
2063 -- current one at the point of the type derivation, so it does not
2064 -- appear in the list of primitive operations of the type. We have to
2065 -- find the proper place of insertion in the list of primitive opera-
2066 -- tions by iterating over the list for the parent type.
2068 Op1 := First_Elmt (Old_Prim);
2069 Op2 := First_Elmt (New_Prim);
2070 while Present (Op1) and then Present (Op2) loop
2071 if Derives_From (Node (Op1)) then
2072 if No (Prev) then
2074 -- Avoid adding it to the list of primitives if already there
2076 if Node (Op2) /= Subp then
2077 Prepend_Elmt (Subp, New_Prim);
2078 end if;
2080 else
2081 Insert_Elmt_After (Subp, Prev);
2082 end if;
2084 return;
2085 end if;
2087 Prev := Op2;
2088 Next_Elmt (Op1);
2089 Next_Elmt (Op2);
2090 end loop;
2092 -- Operation is a new primitive
2094 Append_Elmt (Subp, New_Prim);
2095 end Check_Operation_From_Incomplete_Type;
2097 ---------------------------------------
2098 -- Check_Operation_From_Private_View --
2099 ---------------------------------------
2101 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
2102 Tagged_Type : Entity_Id;
2104 begin
2105 if Is_Dispatching_Operation (Alias (Subp)) then
2106 Set_Scope (Subp, Current_Scope);
2107 Tagged_Type := Find_Dispatching_Type (Subp);
2109 -- Add Old_Subp to primitive operations if not already present
2111 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
2112 Add_Dispatching_Operation (Tagged_Type, Old_Subp);
2114 -- If Old_Subp isn't already marked as dispatching then this is
2115 -- the case of an operation of an untagged private type fulfilled
2116 -- by a tagged type that overrides an inherited dispatching
2117 -- operation, so we set the necessary dispatching attributes here.
2119 if not Is_Dispatching_Operation (Old_Subp) then
2121 -- If the untagged type has no discriminants, and the full
2122 -- view is constrained, there will be a spurious mismatch of
2123 -- subtypes on the controlling arguments, because the tagged
2124 -- type is the internal base type introduced in the derivation.
2125 -- Use the original type to verify conformance, rather than the
2126 -- base type.
2128 if not Comes_From_Source (Tagged_Type)
2129 and then Has_Discriminants (Tagged_Type)
2130 then
2131 declare
2132 Formal : Entity_Id;
2134 begin
2135 Formal := First_Formal (Old_Subp);
2136 while Present (Formal) loop
2137 if Tagged_Type = Base_Type (Etype (Formal)) then
2138 Tagged_Type := Etype (Formal);
2139 end if;
2141 Next_Formal (Formal);
2142 end loop;
2143 end;
2145 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
2146 Tagged_Type := Etype (Old_Subp);
2147 end if;
2148 end if;
2150 Check_Controlling_Formals (Tagged_Type, Old_Subp);
2151 Set_Is_Dispatching_Operation (Old_Subp, True);
2152 Set_DT_Position_Value (Old_Subp, No_Uint);
2153 end if;
2155 -- If the old subprogram is an explicit renaming of some other
2156 -- entity, it is not overridden by the inherited subprogram.
2157 -- Otherwise, update its alias and other attributes.
2159 if Present (Alias (Old_Subp))
2160 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
2161 N_Subprogram_Renaming_Declaration
2162 then
2163 Set_Alias (Old_Subp, Alias (Subp));
2165 -- The derived subprogram should inherit the abstractness of
2166 -- the parent subprogram (except in the case of a function
2167 -- returning the type). This sets the abstractness properly
2168 -- for cases where a private extension may have inherited an
2169 -- abstract operation, but the full type is derived from a
2170 -- descendant type and inherits a nonabstract version.
2172 if Etype (Subp) /= Tagged_Type then
2173 Set_Is_Abstract_Subprogram
2174 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
2175 end if;
2176 end if;
2177 end if;
2178 end if;
2179 end Check_Operation_From_Private_View;
2181 --------------------------
2182 -- Find_Controlling_Arg --
2183 --------------------------
2185 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
2186 Orig_Node : constant Node_Id := Original_Node (N);
2187 Typ : Entity_Id;
2189 begin
2190 if Nkind (Orig_Node) = N_Qualified_Expression then
2191 return Find_Controlling_Arg (Expression (Orig_Node));
2192 end if;
2194 -- Dispatching on result case. If expansion is disabled, the node still
2195 -- has the structure of a function call. However, if the function name
2196 -- is an operator and the call was given in infix form, the original
2197 -- node has no controlling result and we must examine the current node.
2199 if Nkind (N) = N_Function_Call
2200 and then Present (Controlling_Argument (N))
2201 and then Has_Controlling_Result (Entity (Name (N)))
2202 then
2203 return Controlling_Argument (N);
2205 -- If expansion is enabled, the call may have been transformed into
2206 -- an indirect call, and we need to recover the original node.
2208 elsif Nkind (Orig_Node) = N_Function_Call
2209 and then Present (Controlling_Argument (Orig_Node))
2210 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
2211 then
2212 return Controlling_Argument (Orig_Node);
2214 -- Type conversions are dynamically tagged if the target type, or its
2215 -- designated type, are classwide. An interface conversion expands into
2216 -- a dereference, so test must be performed on the original node.
2218 elsif Nkind (Orig_Node) = N_Type_Conversion
2219 and then Nkind (N) = N_Explicit_Dereference
2220 and then Is_Controlling_Actual (N)
2221 then
2222 declare
2223 Target_Type : constant Entity_Id :=
2224 Entity (Subtype_Mark (Orig_Node));
2226 begin
2227 if Is_Class_Wide_Type (Target_Type) then
2228 return N;
2230 elsif Is_Access_Type (Target_Type)
2231 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
2232 then
2233 return N;
2235 else
2236 return Empty;
2237 end if;
2238 end;
2240 -- Normal case
2242 elsif Is_Controlling_Actual (N)
2243 or else
2244 (Nkind (Parent (N)) = N_Qualified_Expression
2245 and then Is_Controlling_Actual (Parent (N)))
2246 then
2247 Typ := Etype (N);
2249 if Is_Access_Type (Typ) then
2251 -- In the case of an Access attribute, use the type of the prefix,
2252 -- since in the case of an actual for an access parameter, the
2253 -- attribute's type may be of a specific designated type, even
2254 -- though the prefix type is class-wide.
2256 if Nkind (N) = N_Attribute_Reference then
2257 Typ := Etype (Prefix (N));
2259 -- An allocator is dispatching if the type of qualified expression
2260 -- is class_wide, in which case this is the controlling type.
2262 elsif Nkind (Orig_Node) = N_Allocator
2263 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
2264 then
2265 Typ := Etype (Expression (Orig_Node));
2266 else
2267 Typ := Designated_Type (Typ);
2268 end if;
2269 end if;
2271 if Is_Class_Wide_Type (Typ)
2272 or else
2273 (Nkind (Parent (N)) = N_Qualified_Expression
2274 and then Is_Access_Type (Etype (N))
2275 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
2276 then
2277 return N;
2278 end if;
2279 end if;
2281 return Empty;
2282 end Find_Controlling_Arg;
2284 ---------------------------
2285 -- Find_Dispatching_Type --
2286 ---------------------------
2288 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
2289 A_Formal : Entity_Id;
2290 Formal : Entity_Id;
2291 Ctrl_Type : Entity_Id;
2293 begin
2294 if Ekind (Subp) in E_Function | E_Procedure
2295 and then Present (DTC_Entity (Subp))
2296 then
2297 return Scope (DTC_Entity (Subp));
2299 -- For subprograms internally generated by derivations of tagged types
2300 -- use the alias subprogram as a reference to locate the dispatching
2301 -- type of Subp.
2303 elsif not Comes_From_Source (Subp)
2304 and then Present (Alias (Subp))
2305 and then Is_Dispatching_Operation (Alias (Subp))
2306 then
2307 if Ekind (Alias (Subp)) = E_Function
2308 and then Has_Controlling_Result (Alias (Subp))
2309 then
2310 return Check_Controlling_Type (Etype (Subp), Subp);
2312 else
2313 Formal := First_Formal (Subp);
2314 A_Formal := First_Formal (Alias (Subp));
2315 while Present (A_Formal) loop
2316 if Is_Controlling_Formal (A_Formal) then
2317 return Check_Controlling_Type (Etype (Formal), Subp);
2318 end if;
2320 Next_Formal (Formal);
2321 Next_Formal (A_Formal);
2322 end loop;
2324 pragma Assert (False);
2325 return Empty;
2326 end if;
2328 -- General case
2330 else
2331 Formal := First_Formal (Subp);
2332 while Present (Formal) loop
2333 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
2335 if Present (Ctrl_Type) then
2336 return Ctrl_Type;
2337 end if;
2339 Next_Formal (Formal);
2340 end loop;
2342 -- The subprogram may also be dispatching on result
2344 if Present (Etype (Subp)) then
2345 return Check_Controlling_Type (Etype (Subp), Subp);
2346 end if;
2347 end if;
2349 pragma Assert (not Is_Dispatching_Operation (Subp));
2350 return Empty;
2351 end Find_Dispatching_Type;
2353 --------------------------------------
2354 -- Find_Hidden_Overridden_Primitive --
2355 --------------------------------------
2357 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
2359 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
2360 Elmt : Elmt_Id;
2361 Orig_Prim : Entity_Id;
2362 Prim : Entity_Id;
2363 Vis_List : Elist_Id;
2365 begin
2366 -- This Ada 2012 rule applies only for type extensions or private
2367 -- extensions, where the parent type is not in a parent unit, and
2368 -- where an operation is never declared but still inherited.
2370 if No (Tag_Typ)
2371 or else not Is_Record_Type (Tag_Typ)
2372 or else Etype (Tag_Typ) = Tag_Typ
2373 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
2374 then
2375 return Empty;
2376 end if;
2378 -- Collect the list of visible ancestor of the tagged type
2380 Vis_List := Visible_Ancestors (Tag_Typ);
2382 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2383 while Present (Elmt) loop
2384 Prim := Node (Elmt);
2386 -- Find an inherited hidden dispatching primitive with the name of S
2387 -- and a type-conformant profile.
2389 if Present (Alias (Prim))
2390 and then Is_Hidden (Alias (Prim))
2391 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
2392 and then Primitive_Names_Match (S, Prim)
2393 and then Type_Conformant (S, Prim)
2394 then
2395 declare
2396 Vis_Ancestor : Elmt_Id;
2397 Elmt : Elmt_Id;
2399 begin
2400 -- The original corresponding operation of Prim must be an
2401 -- operation of a visible ancestor of the dispatching type S,
2402 -- and the original corresponding operation of S2 must be
2403 -- visible.
2405 Orig_Prim := Original_Corresponding_Operation (Prim);
2407 if Orig_Prim /= Prim
2408 and then Is_Immediately_Visible (Orig_Prim)
2409 then
2410 Vis_Ancestor := First_Elmt (Vis_List);
2411 while Present (Vis_Ancestor) loop
2412 Elmt :=
2413 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
2414 while Present (Elmt) loop
2415 if Node (Elmt) = Orig_Prim then
2416 Set_Overridden_Operation (S, Prim);
2417 Set_Is_Ada_2022_Only (S,
2418 Is_Ada_2022_Only (Prim));
2419 Set_Alias (Prim, Orig_Prim);
2420 return Prim;
2421 end if;
2423 Next_Elmt (Elmt);
2424 end loop;
2426 Next_Elmt (Vis_Ancestor);
2427 end loop;
2428 end if;
2429 end;
2430 end if;
2432 Next_Elmt (Elmt);
2433 end loop;
2435 return Empty;
2436 end Find_Hidden_Overridden_Primitive;
2438 ---------------------------------------
2439 -- Find_Primitive_Covering_Interface --
2440 ---------------------------------------
2442 function Find_Primitive_Covering_Interface
2443 (Tagged_Type : Entity_Id;
2444 Iface_Prim : Entity_Id) return Entity_Id
2446 E : Entity_Id;
2447 El : Elmt_Id;
2449 begin
2450 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
2451 or else (Present (Alias (Iface_Prim))
2452 and then
2453 Is_Interface
2454 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
2456 -- Search in the homonym chain. Done to speed up locating visible
2457 -- entities and required to catch primitives associated with the partial
2458 -- view of private types when processing the corresponding full view.
2460 E := Current_Entity (Iface_Prim);
2461 while Present (E) loop
2462 if Is_Subprogram (E)
2463 and then Is_Dispatching_Operation (E)
2464 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
2465 then
2466 return E;
2467 end if;
2469 E := Homonym (E);
2470 end loop;
2472 -- Search in the list of primitives of the type. Required to locate
2473 -- the covering primitive if the covering primitive is not visible
2474 -- (for example, non-visible inherited primitive of private type).
2476 El := First_Elmt (Primitive_Operations (Tagged_Type));
2477 while Present (El) loop
2478 E := Node (El);
2480 -- Keep separate the management of internal entities that link
2481 -- primitives with interface primitives from tagged type primitives.
2483 if No (Interface_Alias (E)) then
2484 if Present (Alias (E)) then
2486 -- This interface primitive has not been covered yet
2488 if Alias (E) = Iface_Prim then
2489 return E;
2491 -- The covering primitive was inherited
2493 elsif Overridden_Operation (Ultimate_Alias (E))
2494 = Iface_Prim
2495 then
2496 return E;
2497 end if;
2498 end if;
2500 -- Check if E covers the interface primitive (includes case in
2501 -- which E is an inherited private primitive).
2503 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2504 return E;
2505 end if;
2507 -- Use the internal entity that links the interface primitive with
2508 -- the covering primitive to locate the entity.
2510 elsif Interface_Alias (E) = Iface_Prim then
2511 return Alias (E);
2512 end if;
2514 Next_Elmt (El);
2515 end loop;
2517 -- Not found
2519 return Empty;
2520 end Find_Primitive_Covering_Interface;
2522 ---------------------------
2523 -- Inheritance_Utilities --
2524 ---------------------------
2526 package body Inheritance_Utilities is
2528 ---------------------------
2529 -- Inherited_Subprograms --
2530 ---------------------------
2532 function Inherited_Subprograms
2533 (S : Entity_Id;
2534 No_Interfaces : Boolean := False;
2535 Interfaces_Only : Boolean := False;
2536 One_Only : Boolean := False) return Subprogram_List
2538 Result : Subprogram_List (1 .. 6000);
2539 -- 6000 here is intended to be infinity. We could use an expandable
2540 -- table, but it would be awfully heavy, and there is no way that we
2541 -- could reasonably exceed this value.
2543 N : Nat := 0;
2544 -- Number of entries in Result
2546 Parent_Op : Entity_Id;
2547 -- Traverses the Overridden_Operation chain
2549 procedure Store_IS (E : Entity_Id);
2550 -- Stores E in Result if not already stored
2552 --------------
2553 -- Store_IS --
2554 --------------
2556 procedure Store_IS (E : Entity_Id) is
2557 begin
2558 for J in 1 .. N loop
2559 if E = Result (J) then
2560 return;
2561 end if;
2562 end loop;
2564 N := N + 1;
2565 Result (N) := E;
2566 end Store_IS;
2568 -- Start of processing for Inherited_Subprograms
2570 begin
2571 pragma Assert (not (No_Interfaces and Interfaces_Only));
2573 -- When used from backends, visibility can be handled differently
2574 -- resulting in no dispatching type being found.
2576 if Present (S)
2577 and then Is_Dispatching_Operation (S)
2578 and then Present (Find_DT (S))
2579 then
2580 -- Deal with direct inheritance
2582 if not Interfaces_Only then
2583 Parent_Op := S;
2584 loop
2585 Parent_Op := Overridden_Operation (Parent_Op);
2586 exit when No (Parent_Op)
2587 or else (No_Interfaces
2588 and then Is_Interface (Find_DT (Parent_Op)));
2590 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2591 Store_IS (Parent_Op);
2593 if One_Only then
2594 goto Done;
2595 end if;
2596 end if;
2597 end loop;
2598 end if;
2600 -- Now deal with interfaces
2602 if not No_Interfaces then
2603 declare
2604 Tag_Typ : Entity_Id;
2605 Prim : Entity_Id;
2606 Elmt : Elmt_Id;
2608 begin
2609 Tag_Typ := Find_DT (S);
2611 -- In the presence of limited views there may be no visible
2612 -- dispatching type. Primitives will be inherited when non-
2613 -- limited view is frozen.
2615 if No (Tag_Typ) then
2616 return Result (1 .. 0);
2618 -- Prevent cascaded errors
2620 elsif Is_Concurrent_Type (Tag_Typ)
2621 and then No (Corresponding_Record_Type (Tag_Typ))
2622 and then Serious_Errors_Detected > 0
2623 then
2624 return Result (1 .. 0);
2625 end if;
2627 if Is_Concurrent_Type (Tag_Typ) then
2628 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2629 end if;
2631 if Present (Tag_Typ)
2632 and then Is_Private_Type (Tag_Typ)
2633 and then Present (Full_View (Tag_Typ))
2634 then
2635 Tag_Typ := Full_View (Tag_Typ);
2636 end if;
2638 -- Search primitive operations of dispatching type
2640 if Present (Tag_Typ)
2641 and then Present (Primitive_Operations (Tag_Typ))
2642 then
2643 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2644 while Present (Elmt) loop
2645 Prim := Node (Elmt);
2647 -- The following test eliminates some odd cases in
2648 -- which Ekind (Prim) is Void, to be investigated
2649 -- further ???
2651 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2652 null;
2654 -- For [generic] subprogram, look at interface
2655 -- alias.
2657 elsif Present (Interface_Alias (Prim))
2658 and then Alias (Prim) = S
2659 then
2660 -- We have found a primitive covered by S
2662 Store_IS (Interface_Alias (Prim));
2664 if One_Only then
2665 goto Done;
2666 end if;
2667 end if;
2669 Next_Elmt (Elmt);
2670 end loop;
2671 end if;
2672 end;
2673 end if;
2674 end if;
2676 <<Done>>
2678 return Result (1 .. N);
2679 end Inherited_Subprograms;
2681 ------------------------------
2682 -- Is_Overriding_Subprogram --
2683 ------------------------------
2685 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2686 Inherited : constant Subprogram_List :=
2687 Inherited_Subprograms (E, One_Only => True);
2688 begin
2689 return Inherited'Length > 0;
2690 end Is_Overriding_Subprogram;
2691 end Inheritance_Utilities;
2693 --------------------------------
2694 -- Inheritance_Utilities_Inst --
2695 --------------------------------
2697 package Inheritance_Utilities_Inst is new
2698 Inheritance_Utilities (Find_Dispatching_Type);
2700 ---------------------------
2701 -- Inherited_Subprograms --
2702 ---------------------------
2704 function Inherited_Subprograms
2705 (S : Entity_Id;
2706 No_Interfaces : Boolean := False;
2707 Interfaces_Only : Boolean := False;
2708 One_Only : Boolean := False) return Subprogram_List renames
2709 Inheritance_Utilities_Inst.Inherited_Subprograms;
2711 ---------------------------
2712 -- Is_Dynamically_Tagged --
2713 ---------------------------
2715 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2716 begin
2717 if Nkind (N) = N_Error then
2718 return False;
2720 elsif Present (Find_Controlling_Arg (N)) then
2721 return True;
2723 -- Special cases: entities, and calls that dispatch on result
2725 elsif Is_Entity_Name (N) then
2726 return Is_Class_Wide_Type (Etype (N));
2728 elsif Nkind (N) = N_Function_Call
2729 and then Is_Class_Wide_Type (Etype (N))
2730 then
2731 return True;
2733 -- Otherwise check whether call has controlling argument
2735 else
2736 return False;
2737 end if;
2738 end Is_Dynamically_Tagged;
2740 ---------------------------------
2741 -- Is_Null_Interface_Primitive --
2742 ---------------------------------
2744 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2745 begin
2746 return Comes_From_Source (E)
2747 and then Is_Dispatching_Operation (E)
2748 and then Ekind (E) = E_Procedure
2749 and then Null_Present (Parent (E))
2750 and then Is_Interface (Find_Dispatching_Type (E));
2751 end Is_Null_Interface_Primitive;
2753 -----------------------------------
2754 -- Is_Inherited_Public_Operation --
2755 -----------------------------------
2757 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2758 Pack_Decl : Node_Id;
2759 Prim : Entity_Id := Op;
2760 Scop : Entity_Id := Prim;
2762 begin
2763 -- Locate the ultimate non-hidden alias entity
2765 while Present (Alias (Prim)) and then not Is_Hidden (Alias (Prim)) loop
2766 pragma Assert (Alias (Prim) /= Prim);
2767 Prim := Alias (Prim);
2768 Scop := Scope (Prim);
2769 end loop;
2771 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2772 Pack_Decl := Unit_Declaration_Node (Scop);
2774 return
2775 Nkind (Pack_Decl) = N_Package_Declaration
2776 and then List_Containing (Unit_Declaration_Node (Prim)) =
2777 Visible_Declarations (Specification (Pack_Decl));
2779 else
2780 return False;
2781 end if;
2782 end Is_Inherited_Public_Operation;
2784 ------------------------------
2785 -- Is_Overriding_Subprogram --
2786 ------------------------------
2788 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean renames
2789 Inheritance_Utilities_Inst.Is_Overriding_Subprogram;
2791 --------------------------
2792 -- Is_Tag_Indeterminate --
2793 --------------------------
2795 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2796 Nam : Entity_Id;
2797 Actual : Node_Id;
2798 Orig_Node : constant Node_Id := Original_Node (N);
2800 begin
2801 if Nkind (Orig_Node) = N_Function_Call
2802 and then Is_Entity_Name (Name (Orig_Node))
2803 then
2804 Nam := Entity (Name (Orig_Node));
2806 if not Has_Controlling_Result (Nam) then
2807 return False;
2809 -- The function may have a controlling result, but if the return type
2810 -- is not visibly tagged, then this is not tag-indeterminate.
2812 elsif Is_Access_Type (Etype (Nam))
2813 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2814 then
2815 return False;
2817 -- An explicit dereference means that the call has already been
2818 -- expanded and there is no tag to propagate.
2820 elsif Nkind (N) = N_Explicit_Dereference then
2821 return False;
2823 -- If there are no actuals, the call is tag-indeterminate
2825 elsif No (Parameter_Associations (Orig_Node)) then
2826 return True;
2828 else
2829 Actual := First_Actual (Orig_Node);
2830 while Present (Actual) loop
2831 if Is_Controlling_Actual (Actual)
2832 and then not Is_Tag_Indeterminate (Actual)
2833 then
2834 -- One operand is dispatching
2836 return False;
2837 end if;
2839 Next_Actual (Actual);
2840 end loop;
2842 return True;
2843 end if;
2845 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2846 return Is_Tag_Indeterminate (Expression (Orig_Node));
2848 -- Case of a call to the Input attribute (possibly rewritten), which is
2849 -- always tag-indeterminate except when its prefix is a Class attribute.
2851 elsif Nkind (Orig_Node) = N_Attribute_Reference
2852 and then
2853 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2854 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2855 then
2856 return True;
2858 -- In Ada 2005, a function that returns an anonymous access type can be
2859 -- dispatching, and the dereference of a call to such a function can
2860 -- also be tag-indeterminate if the call itself is.
2862 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2863 and then Ada_Version >= Ada_2005
2864 then
2865 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2867 else
2868 return False;
2869 end if;
2870 end Is_Tag_Indeterminate;
2872 ------------------------------------
2873 -- Override_Dispatching_Operation --
2874 ------------------------------------
2876 procedure Override_Dispatching_Operation
2877 (Tagged_Type : Entity_Id;
2878 Prev_Op : Entity_Id;
2879 New_Op : Entity_Id)
2881 Elmt : Elmt_Id;
2882 Prim : Node_Id;
2884 begin
2885 -- If there is no previous operation to override, the type declaration
2886 -- was malformed, and an error must have been emitted already.
2888 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2889 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2890 Next_Elmt (Elmt);
2891 end loop;
2893 if No (Elmt) then
2894 return;
2895 end if;
2897 -- The location of entities that come from source in the list of
2898 -- primitives of the tagged type must follow their order of occurrence
2899 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2900 -- primitive of an interface that is not implemented by the parents of
2901 -- this tagged type (that is, it is an alias of an interface primitive
2902 -- generated by Derive_Interface_Progenitors), then we must append the
2903 -- new entity at the end of the list of primitives.
2905 if Present (Alias (Prev_Op))
2906 and then Etype (Tagged_Type) /= Tagged_Type
2907 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2908 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2909 Tagged_Type, Use_Full_View => True)
2910 and then not Implements_Interface
2911 (Etype (Tagged_Type),
2912 Find_Dispatching_Type (Alias (Prev_Op)))
2913 then
2914 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2915 Add_Dispatching_Operation (Tagged_Type, New_Op);
2917 -- The new primitive replaces the overridden entity. Required to ensure
2918 -- that overriding primitive is assigned the same dispatch table slot.
2920 else
2921 Replace_Elmt (Elmt, New_Op);
2922 end if;
2924 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2926 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2927 -- entities of the overridden primitive to reference New_Op, and
2928 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2929 -- that the new operation is subtype conformant with the interface
2930 -- operations that it implements (for operations inherited from the
2931 -- parent itself, this check is made when building the derived type).
2933 -- Note: This code is executed with internally generated wrappers of
2934 -- functions with controlling result and late overridings.
2936 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2937 while Present (Elmt) loop
2938 Prim := Node (Elmt);
2940 if Prim = New_Op then
2941 null;
2943 -- Note: The check on Is_Subprogram protects the frontend against
2944 -- reading attributes in entities that are not yet fully decorated
2946 elsif Is_Subprogram (Prim)
2947 and then Present (Interface_Alias (Prim))
2948 and then Alias (Prim) = Prev_Op
2949 then
2950 Set_Alias (Prim, New_Op);
2952 -- No further decoration needed yet for internally generated
2953 -- wrappers of controlling functions since (at this stage)
2954 -- they are not yet decorated.
2956 if not Is_Wrapper (New_Op) then
2957 Check_Subtype_Conformant (New_Op, Prim);
2959 Set_Is_Abstract_Subprogram (Prim,
2960 Is_Abstract_Subprogram (New_Op));
2962 -- Ensure that this entity will be expanded to fill the
2963 -- corresponding entry in its dispatch table.
2965 if not Is_Abstract_Subprogram (Prim) then
2966 Set_Has_Delayed_Freeze (Prim);
2967 end if;
2968 end if;
2969 end if;
2971 Next_Elmt (Elmt);
2972 end loop;
2973 end if;
2975 if (not Is_Package_Or_Generic_Package (Current_Scope))
2976 or else not In_Private_Part (Current_Scope)
2977 then
2978 -- Not a private primitive
2980 null;
2982 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2984 -- Make the overriding operation into an alias of the implicit one.
2985 -- In this fashion a call from outside ends up calling the new body
2986 -- even if non-dispatching, and a call from inside calls the over-
2987 -- riding operation because it hides the implicit one. To indicate
2988 -- that the body of Prev_Op is never called, set its dispatch table
2989 -- entity to Empty. If the overridden operation has a dispatching
2990 -- result, so does the overriding one.
2992 Set_Alias (Prev_Op, New_Op);
2993 Set_DTC_Entity (Prev_Op, Empty);
2994 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2995 Set_Is_Ada_2022_Only (New_Op, Is_Ada_2022_Only (Prev_Op));
2996 end if;
2997 end Override_Dispatching_Operation;
2999 -------------------
3000 -- Propagate_Tag --
3001 -------------------
3003 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
3004 Call_Node : Node_Id;
3005 Arg : Node_Id;
3007 begin
3008 if Nkind (Actual) = N_Function_Call then
3009 Call_Node := Actual;
3011 elsif Nkind (Actual) = N_Identifier
3012 and then Nkind (Original_Node (Actual)) = N_Function_Call
3013 then
3014 -- Call rewritten as object declaration when stack-checking is
3015 -- enabled. Propagate tag to expression in declaration, which is
3016 -- original call.
3018 Call_Node := Expression (Parent (Entity (Actual)));
3020 -- Ada 2005: If this is a dereference of a call to a function with a
3021 -- dispatching access-result, the tag is propagated when the dereference
3022 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
3024 elsif Nkind (Actual) = N_Explicit_Dereference
3025 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
3026 then
3027 return;
3029 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
3030 -- and in that case we can simply return.
3032 elsif Nkind (Actual) = N_Attribute_Reference then
3033 pragma Assert (Attribute_Name (Actual) = Name_Input);
3035 return;
3037 -- Only other possibilities are parenthesized or qualified expression,
3038 -- or an expander-generated unchecked conversion of a function call to
3039 -- a stream Input attribute.
3041 else
3042 Call_Node := Expression (Actual);
3043 end if;
3045 -- No action needed if the call has been already expanded
3047 if Is_Expanded_Dispatching_Call (Call_Node) then
3048 return;
3049 end if;
3051 -- Do not set the Controlling_Argument if already set. This happens in
3052 -- the special case of _Input (see Exp_Attr, case Input).
3054 if No (Controlling_Argument (Call_Node)) then
3055 Set_Controlling_Argument (Call_Node, Control);
3056 end if;
3058 Arg := First_Actual (Call_Node);
3059 while Present (Arg) loop
3060 if Is_Tag_Indeterminate (Arg) then
3061 Propagate_Tag (Control, Arg);
3062 end if;
3064 Next_Actual (Arg);
3065 end loop;
3067 -- Add class-wide precondition check if the target of this dispatching
3068 -- call has or inherits class-wide preconditions.
3070 Install_Class_Preconditions_Check (Call_Node);
3072 -- Expansion of dispatching calls is suppressed on VM targets, because
3073 -- the VM back-ends directly handle the generation of dispatching calls
3074 -- and would have to undo any expansion to an indirect call.
3076 if Tagged_Type_Expansion then
3077 declare
3078 Call_Typ : constant Entity_Id := Etype (Call_Node);
3080 begin
3081 Expand_Dispatching_Call (Call_Node);
3083 -- If the controlling argument is an interface type and the type
3084 -- of Call_Node differs then we must add an implicit conversion to
3085 -- force displacement of the pointer to the object to reference
3086 -- the secondary dispatch table of the interface.
3088 if Is_Interface (Etype (Control))
3089 and then Etype (Control) /= Call_Typ
3090 then
3091 -- Cannot use Convert_To because the previous call to
3092 -- Expand_Dispatching_Call leaves decorated the Call_Node
3093 -- with the type of Control.
3095 Rewrite (Call_Node,
3096 Make_Type_Conversion (Sloc (Call_Node),
3097 Subtype_Mark =>
3098 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
3099 Expression => Relocate_Node (Call_Node)));
3100 Set_Etype (Call_Node, Etype (Control));
3101 Set_Analyzed (Call_Node);
3103 Expand_Interface_Conversion (Call_Node);
3104 end if;
3105 end;
3107 -- Expansion of a dispatching call results in an indirect call, which in
3108 -- turn causes current values to be killed (see Resolve_Call), so on VM
3109 -- targets we do the call here to ensure consistent warnings between VM
3110 -- and non-VM targets.
3112 else
3113 Kill_Current_Values;
3114 end if;
3115 end Propagate_Tag;
3117 end Sem_Disp;