Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / gcc / ada / sem_disp.adb
blobf135bba8770e7a173c950ebd06f97c3a054f1775
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-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Elists; use Elists;
29 with Einfo; use Einfo;
30 with Exp_Disp; use Exp_Disp;
31 with Exp_Util; use Exp_Util;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Tss; use Exp_Tss;
34 with Errout; use Errout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Output; use Output;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
52 with Sinfo; use Sinfo;
53 with Tbuild; use Tbuild;
54 with Uintp; use Uintp;
55 with Warnsw; use Warnsw;
57 package body Sem_Disp is
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Add_Dispatching_Operation
64 (Tagged_Type : Entity_Id;
65 New_Op : Entity_Id);
66 -- Add New_Op in the list of primitive operations of Tagged_Type
68 function Check_Controlling_Type
69 (T : Entity_Id;
70 Subp : Entity_Id) return Entity_Id;
71 -- T is the tagged type of a formal parameter or the result of Subp.
72 -- If the subprogram has a controlling parameter or result that matches
73 -- the type, then returns the tagged type of that parameter or result
74 -- (returning the designated tagged type in the case of an access
75 -- parameter); otherwise returns empty.
77 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
78 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
79 -- type of S that has the same name of S, a type-conformant profile, an
80 -- original corresponding operation O that is a primitive of a visible
81 -- ancestor of the dispatching type of S and O is visible at the point of
82 -- of declaration of S. If the entity is found the Alias of S is set to the
83 -- original corresponding operation S and its Overridden_Operation is set
84 -- to the found entity; otherwise return Empty.
86 -- This routine does not search for non-hidden primitives since they are
87 -- covered by the normal Ada 2005 rules.
89 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
90 -- Check whether a primitive operation is inherited from an operation
91 -- declared in the visible part of its package.
93 -------------------------------
94 -- Add_Dispatching_Operation --
95 -------------------------------
97 procedure Add_Dispatching_Operation
98 (Tagged_Type : Entity_Id;
99 New_Op : Entity_Id)
101 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
103 begin
104 -- The dispatching operation may already be on the list, if it is the
105 -- wrapper for an inherited function of a null extension (see Exp_Ch3
106 -- for the construction of function wrappers). The list of primitive
107 -- operations must not contain duplicates.
109 -- The Default_Initial_Condition and invariant procedures are not added
110 -- to the list of primitives even when they are generated for a tagged
111 -- type. These routines must not be targets of dispatching calls and
112 -- therefore must not appear in the dispatch table because they already
113 -- utilize class-wide-precondition semantics to handle inheritance and
114 -- overriding.
116 if Is_Suitable_Primitive (New_Op) then
117 Append_Unique_Elmt (New_Op, List);
118 end if;
119 end Add_Dispatching_Operation;
121 --------------------------
122 -- Covered_Interface_Op --
123 --------------------------
125 function Covered_Interface_Op (Prim : Entity_Id) return Entity_Id is
126 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
127 Elmt : Elmt_Id;
128 E : Entity_Id;
130 begin
131 pragma Assert (Is_Dispatching_Operation (Prim));
133 -- Although this is a dispatching primitive we must check if its
134 -- dispatching type is available because it may be the primitive
135 -- of a private type not defined as tagged in its partial view.
137 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
139 -- If the tagged type is frozen then the internal entities associated
140 -- with interfaces are available in the list of primitives of the
141 -- tagged type and can be used to speed up this search.
143 if Is_Frozen (Tagged_Type) then
144 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
145 while Present (Elmt) loop
146 E := Node (Elmt);
148 if Present (Interface_Alias (E))
149 and then Alias (E) = Prim
150 then
151 return Interface_Alias (E);
152 end if;
154 Next_Elmt (Elmt);
155 end loop;
157 -- Otherwise we must collect all the interface primitives and check
158 -- if the Prim overrides (implements) some interface primitive.
160 else
161 declare
162 Ifaces_List : Elist_Id;
163 Iface_Elmt : Elmt_Id;
164 Iface : Entity_Id;
165 Iface_Prim : Entity_Id;
167 begin
168 Collect_Interfaces (Tagged_Type, Ifaces_List);
169 Iface_Elmt := First_Elmt (Ifaces_List);
170 while Present (Iface_Elmt) loop
171 Iface := Node (Iface_Elmt);
173 Elmt := First_Elmt (Primitive_Operations (Iface));
174 while Present (Elmt) loop
175 Iface_Prim := Node (Elmt);
177 if Chars (Iface_Prim) = Chars (Prim)
178 and then Is_Interface_Conformant
179 (Tagged_Type, Iface_Prim, Prim)
180 then
181 return Iface_Prim;
182 end if;
184 Next_Elmt (Elmt);
185 end loop;
187 Next_Elmt (Iface_Elmt);
188 end loop;
189 end;
190 end if;
191 end if;
193 return Empty;
194 end Covered_Interface_Op;
196 -------------------------------
197 -- Check_Controlling_Formals --
198 -------------------------------
200 procedure Check_Controlling_Formals
201 (Typ : Entity_Id;
202 Subp : Entity_Id)
204 Formal : Entity_Id;
205 Ctrl_Type : Entity_Id;
207 begin
208 Formal := First_Formal (Subp);
209 while Present (Formal) loop
210 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
212 if Present (Ctrl_Type) then
214 -- When controlling type is concurrent and declared within a
215 -- generic or inside an instance use corresponding record type.
217 if Is_Concurrent_Type (Ctrl_Type)
218 and then Present (Corresponding_Record_Type (Ctrl_Type))
219 then
220 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
221 end if;
223 if Ctrl_Type = Typ then
224 Set_Is_Controlling_Formal (Formal);
226 -- Ada 2005 (AI-231): Anonymous access types that are used in
227 -- controlling parameters exclude null because it is necessary
228 -- to read the tag to dispatch, and null has no tag.
230 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
231 Set_Can_Never_Be_Null (Etype (Formal));
232 Set_Is_Known_Non_Null (Etype (Formal));
233 end if;
235 -- Check that the parameter's nominal subtype statically
236 -- matches the first subtype.
238 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
239 if not Subtypes_Statically_Match
240 (Typ, Designated_Type (Etype (Formal)))
241 then
242 Error_Msg_N
243 ("parameter subtype does not match controlling type",
244 Formal);
245 end if;
247 -- Within a predicate function, the formal may be a subtype
248 -- of a tagged type, given that the predicate is expressed
249 -- in terms of the subtype.
251 elsif not Subtypes_Statically_Match (Typ, Etype (Formal))
252 and then not Is_Predicate_Function (Subp)
253 then
254 Error_Msg_N
255 ("parameter subtype does not match controlling type",
256 Formal);
257 end if;
259 if Present (Default_Value (Formal)) then
261 -- In Ada 2005, access parameters can have defaults
263 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
264 and then Ada_Version < Ada_2005
265 then
266 Error_Msg_N
267 ("default not allowed for controlling access parameter",
268 Default_Value (Formal));
270 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
271 Error_Msg_N
272 ("default expression must be a tag indeterminate" &
273 " function call", Default_Value (Formal));
274 end if;
275 end if;
277 elsif Comes_From_Source (Subp) then
278 Error_Msg_N
279 ("operation can be dispatching in only one type", Subp);
280 end if;
281 end if;
283 Next_Formal (Formal);
284 end loop;
286 if Ekind_In (Subp, E_Function, E_Generic_Function) then
287 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
289 if Present (Ctrl_Type) then
290 if Ctrl_Type = Typ then
291 Set_Has_Controlling_Result (Subp);
293 -- Check that result subtype statically matches first subtype
294 -- (Ada 2005): Subp may have a controlling access result.
296 if Subtypes_Statically_Match (Typ, Etype (Subp))
297 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
298 and then
299 Subtypes_Statically_Match
300 (Typ, Designated_Type (Etype (Subp))))
301 then
302 null;
304 else
305 Error_Msg_N
306 ("result subtype does not match controlling type", Subp);
307 end if;
309 elsif Comes_From_Source (Subp) then
310 Error_Msg_N
311 ("operation can be dispatching in only one type", Subp);
312 end if;
313 end if;
314 end if;
315 end Check_Controlling_Formals;
317 ----------------------------
318 -- Check_Controlling_Type --
319 ----------------------------
321 function Check_Controlling_Type
322 (T : Entity_Id;
323 Subp : Entity_Id) return Entity_Id
325 Tagged_Type : Entity_Id := Empty;
327 begin
328 if Is_Tagged_Type (T) then
329 if Is_First_Subtype (T) then
330 Tagged_Type := T;
331 else
332 Tagged_Type := Base_Type (T);
333 end if;
335 -- If the type is incomplete, it may have been declared without a
336 -- Tagged indication, but the full view may be tagged, in which case
337 -- that is the controlling type of the subprogram. This is one of the
338 -- approx. 579 places in the language where a lookahead would help.
340 elsif Ekind (T) = E_Incomplete_Type
341 and then Present (Full_View (T))
342 and then Is_Tagged_Type (Full_View (T))
343 then
344 Set_Is_Tagged_Type (T);
345 Tagged_Type := Full_View (T);
347 elsif Ekind (T) = E_Anonymous_Access_Type
348 and then Is_Tagged_Type (Designated_Type (T))
349 then
350 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
351 if Is_First_Subtype (Designated_Type (T)) then
352 Tagged_Type := Designated_Type (T);
353 else
354 Tagged_Type := Base_Type (Designated_Type (T));
355 end if;
357 -- Ada 2005: an incomplete type can be tagged. An operation with an
358 -- access parameter of the type is dispatching.
360 elsif Scope (Designated_Type (T)) = Current_Scope then
361 Tagged_Type := Designated_Type (T);
363 -- Ada 2005 (AI-50217)
365 elsif From_Limited_With (Designated_Type (T))
366 and then Has_Non_Limited_View (Designated_Type (T))
367 and then Scope (Designated_Type (T)) = Scope (Subp)
368 then
369 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
370 Tagged_Type := Non_Limited_View (Designated_Type (T));
371 else
372 Tagged_Type := Base_Type (Non_Limited_View
373 (Designated_Type (T)));
374 end if;
375 end if;
376 end if;
378 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
379 return Empty;
381 -- The dispatching type and the primitive operation must be defined in
382 -- the same scope, except in the case of internal operations and formal
383 -- abstract subprograms.
385 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
386 and then (not Is_Generic_Type (Tagged_Type)
387 or else not Comes_From_Source (Subp)))
388 or else
389 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
390 or else
391 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
392 and then
393 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
394 and then
395 Is_Abstract_Subprogram (Subp))
396 then
397 return Tagged_Type;
399 else
400 return Empty;
401 end if;
402 end Check_Controlling_Type;
404 ----------------------------
405 -- Check_Dispatching_Call --
406 ----------------------------
408 procedure Check_Dispatching_Call (N : Node_Id) is
409 Loc : constant Source_Ptr := Sloc (N);
410 Actual : Node_Id;
411 Formal : Entity_Id;
412 Control : Node_Id := Empty;
413 Func : Entity_Id;
414 Subp_Entity : Entity_Id;
415 Indeterm_Ancestor_Call : Boolean := False;
416 Indeterm_Ctrl_Type : Entity_Id := Empty; -- init to avoid warning
418 Static_Tag : Node_Id := Empty;
419 -- If a controlling formal has a statically tagged actual, the tag of
420 -- this actual is to be used for any tag-indeterminate actual.
422 procedure Check_Direct_Call;
423 -- In the case when the controlling actual is a class-wide type whose
424 -- root type's completion is a task or protected type, the call is in
425 -- fact direct. This routine detects the above case and modifies the
426 -- call accordingly.
428 procedure Check_Dispatching_Context (Call : Node_Id);
429 -- If the call is tag-indeterminate and the entity being called is
430 -- abstract, verify that the context is a call that will eventually
431 -- provide a tag for dispatching, or has provided one already.
433 -----------------------
434 -- Check_Direct_Call --
435 -----------------------
437 procedure Check_Direct_Call is
438 Typ : Entity_Id := Etype (Control);
439 begin
440 -- Predefined primitives do not receive wrappers since they are built
441 -- from scratch for the corresponding record of synchronized types.
442 -- Equality is in general predefined, but is excluded from the check
443 -- when it is user-defined.
445 if Is_Predefined_Dispatching_Operation (Subp_Entity)
446 and then not Is_User_Defined_Equality (Subp_Entity)
447 then
448 return;
449 end if;
451 if Is_Class_Wide_Type (Typ) then
452 Typ := Root_Type (Typ);
453 end if;
455 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
456 Typ := Full_View (Typ);
457 end if;
459 if Is_Concurrent_Type (Typ)
460 and then
461 Present (Corresponding_Record_Type (Typ))
462 then
463 Typ := Corresponding_Record_Type (Typ);
465 -- The concurrent record's list of primitives should contain a
466 -- wrapper for the entity of the call, retrieve it.
468 declare
469 Prim : Entity_Id;
470 Prim_Elmt : Elmt_Id;
471 Wrapper_Found : Boolean := False;
473 begin
474 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
475 while Present (Prim_Elmt) loop
476 Prim := Node (Prim_Elmt);
478 if Is_Primitive_Wrapper (Prim)
479 and then Wrapped_Entity (Prim) = Subp_Entity
480 then
481 Wrapper_Found := True;
482 exit;
483 end if;
485 Next_Elmt (Prim_Elmt);
486 end loop;
488 -- A primitive declared between two views should have a
489 -- corresponding wrapper.
491 pragma Assert (Wrapper_Found);
493 -- Modify the call by setting the proper entity
495 Set_Entity (Name (N), Prim);
496 end;
497 end if;
498 end Check_Direct_Call;
500 -------------------------------
501 -- Check_Dispatching_Context --
502 -------------------------------
504 procedure Check_Dispatching_Context (Call : Node_Id) is
505 Subp : constant Entity_Id := Entity (Name (Call));
507 procedure Abstract_Context_Error;
508 -- Error for abstract call dispatching on result is not dispatching
510 ----------------------------
511 -- Abstract_Context_Error --
512 ----------------------------
514 procedure Abstract_Context_Error is
515 begin
516 if Ekind (Subp) = E_Function then
517 Error_Msg_N
518 ("call to abstract function must be dispatching", N);
520 -- This error can occur for a procedure in the case of a call to
521 -- an abstract formal procedure with a statically tagged operand.
523 else
524 Error_Msg_N
525 ("call to abstract procedure must be dispatching", N);
526 end if;
527 end Abstract_Context_Error;
529 -- Local variables
531 Scop : constant Entity_Id := Current_Scope_No_Loops;
532 Typ : constant Entity_Id := Etype (Subp);
533 Par : Node_Id;
535 -- Start of processing for Check_Dispatching_Context
537 begin
538 -- If the called subprogram is a private overriding, replace it
539 -- with its alias, which has the correct body. Verify that the
540 -- two subprograms have the same controlling type (this is not the
541 -- case for an inherited subprogram that has become abstract).
543 if Is_Abstract_Subprogram (Subp)
544 and then No (Controlling_Argument (Call))
545 then
546 if Present (Alias (Subp))
547 and then not Is_Abstract_Subprogram (Alias (Subp))
548 and then No (DTC_Entity (Subp))
549 and then Find_Dispatching_Type (Subp) =
550 Find_Dispatching_Type (Alias (Subp))
551 then
552 -- Private overriding of inherited abstract operation, call is
553 -- legal.
555 Set_Entity (Name (N), Alias (Subp));
556 return;
558 -- An obscure special case: a null procedure may have a class-
559 -- wide pre/postcondition that includes a call to an abstract
560 -- subp. Calls within the expression may not have been rewritten
561 -- as dispatching calls yet, because the null body appears in
562 -- the current declarative part. The expression will be properly
563 -- rewritten/reanalyzed when the postcondition procedure is built.
565 -- Similarly, if this is a pre/postcondition for an abstract
566 -- subprogram, it may call another abstract function which is
567 -- a primitive of an abstract type. The call is non-dispatching
568 -- but will be legal in overridings of the operation.
570 elsif (Is_Subprogram (Scop)
571 or else Chars (Scop) = Name_Postcondition)
572 and then
573 (Is_Abstract_Subprogram (Scop)
574 or else
575 (Nkind (Parent (Scop)) = N_Procedure_Specification
576 and then Null_Present (Parent (Scop))))
577 then
578 null;
580 elsif Ekind (Current_Scope) = E_Function
581 and then Nkind (Unit_Declaration_Node (Scop)) =
582 N_Generic_Subprogram_Declaration
583 then
584 null;
586 else
587 -- We need to determine whether the context of the call
588 -- provides a tag to make the call dispatching. This requires
589 -- the call to be the actual in an enclosing call, and that
590 -- actual must be controlling. If the call is an operand of
591 -- equality, the other operand must not ve abstract.
593 if not Is_Tagged_Type (Typ)
594 and then not
595 (Ekind (Typ) = E_Anonymous_Access_Type
596 and then Is_Tagged_Type (Designated_Type (Typ)))
597 then
598 Abstract_Context_Error;
599 return;
600 end if;
602 Par := Parent (Call);
604 if Nkind (Par) = N_Parameter_Association then
605 Par := Parent (Par);
606 end if;
608 if Nkind (Par) = N_Qualified_Expression
609 or else Nkind (Par) = N_Unchecked_Type_Conversion
610 then
611 Par := Parent (Par);
612 end if;
614 if Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
615 and then Is_Entity_Name (Name (Par))
616 then
617 declare
618 Enc_Subp : constant Entity_Id := Entity (Name (Par));
619 A : Node_Id;
620 F : Entity_Id;
621 Control : Entity_Id;
622 Ret_Type : Entity_Id;
624 begin
625 -- Find controlling formal that can provide tag for the
626 -- tag-indeterminate actual. The corresponding actual
627 -- must be the corresponding class-wide type.
629 F := First_Formal (Enc_Subp);
630 A := First_Actual (Par);
632 -- Find controlling type of call. Dereference if function
633 -- returns an access type.
635 Ret_Type := Etype (Call);
636 if Is_Access_Type (Etype (Call)) then
637 Ret_Type := Designated_Type (Ret_Type);
638 end if;
640 while Present (F) loop
641 Control := Etype (A);
643 if Is_Access_Type (Control) then
644 Control := Designated_Type (Control);
645 end if;
647 if Is_Controlling_Formal (F)
648 and then not (Call = A or else Parent (Call) = A)
649 and then Control = Class_Wide_Type (Ret_Type)
650 then
651 return;
652 end if;
654 Next_Formal (F);
655 Next_Actual (A);
656 end loop;
658 if Nkind (Par) = N_Function_Call
659 and then Is_Tag_Indeterminate (Par)
660 then
661 -- The parent may be an actual of an enclosing call
663 Check_Dispatching_Context (Par);
664 return;
666 else
667 Error_Msg_N
668 ("call to abstract function must be dispatching",
669 Call);
670 return;
671 end if;
672 end;
674 -- For equality operators, one of the operands must be
675 -- statically or dynamically tagged.
677 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
678 if N = Right_Opnd (Par)
679 and then Is_Tag_Indeterminate (Left_Opnd (Par))
680 then
681 Abstract_Context_Error;
683 elsif N = Left_Opnd (Par)
684 and then Is_Tag_Indeterminate (Right_Opnd (Par))
685 then
686 Abstract_Context_Error;
687 end if;
689 return;
691 -- The left-hand side of an assignment provides the tag
693 elsif Nkind (Par) = N_Assignment_Statement then
694 return;
696 else
697 Abstract_Context_Error;
698 end if;
699 end if;
700 end if;
701 end Check_Dispatching_Context;
703 -- Start of processing for Check_Dispatching_Call
705 begin
706 -- Find a controlling argument, if any
708 if Present (Parameter_Associations (N)) then
709 Subp_Entity := Entity (Name (N));
711 Actual := First_Actual (N);
712 Formal := First_Formal (Subp_Entity);
713 while Present (Actual) loop
714 Control := Find_Controlling_Arg (Actual);
715 exit when Present (Control);
717 -- Check for the case where the actual is a tag-indeterminate call
718 -- whose result type is different than the tagged type associated
719 -- with the containing call, but is an ancestor of the type.
721 if Is_Controlling_Formal (Formal)
722 and then Is_Tag_Indeterminate (Actual)
723 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
724 and then Is_Ancestor (Etype (Actual), Etype (Formal))
725 then
726 Indeterm_Ancestor_Call := True;
727 Indeterm_Ctrl_Type := Etype (Formal);
729 -- If the formal is controlling but the actual is not, the type
730 -- of the actual is statically known, and may be used as the
731 -- controlling tag for some other tag-indeterminate actual.
733 elsif Is_Controlling_Formal (Formal)
734 and then Is_Entity_Name (Actual)
735 and then Is_Tagged_Type (Etype (Actual))
736 then
737 Static_Tag := Actual;
738 end if;
740 Next_Actual (Actual);
741 Next_Formal (Formal);
742 end loop;
744 -- If the call doesn't have a controlling actual but does have an
745 -- indeterminate actual that requires dispatching treatment, then an
746 -- object is needed that will serve as the controlling argument for
747 -- a dispatching call on the indeterminate actual. This can occur
748 -- in the unusual situation of a default actual given by a tag-
749 -- indeterminate call and where the type of the call is an ancestor
750 -- of the type associated with a containing call to an inherited
751 -- operation (see AI-239).
753 -- Rather than create an object of the tagged type, which would
754 -- be problematic for various reasons (default initialization,
755 -- discriminants), the tag of the containing call's associated
756 -- tagged type is directly used to control the dispatching.
758 if No (Control)
759 and then Indeterm_Ancestor_Call
760 and then No (Static_Tag)
761 then
762 Control :=
763 Make_Attribute_Reference (Loc,
764 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
765 Attribute_Name => Name_Tag);
767 Analyze (Control);
768 end if;
770 if Present (Control) then
772 -- Verify that no controlling arguments are statically tagged
774 if Debug_Flag_E then
775 Write_Str ("Found Dispatching call");
776 Write_Int (Int (N));
777 Write_Eol;
778 end if;
780 Actual := First_Actual (N);
781 while Present (Actual) loop
782 if Actual /= Control then
784 if not Is_Controlling_Actual (Actual) then
785 null; -- Can be anything
787 elsif Is_Dynamically_Tagged (Actual) then
788 null; -- Valid parameter
790 elsif Is_Tag_Indeterminate (Actual) then
792 -- The tag is inherited from the enclosing call (the node
793 -- we are currently analyzing). Explicitly expand the
794 -- actual, since the previous call to Expand (from
795 -- Resolve_Call) had no way of knowing about the
796 -- required dispatching.
798 Propagate_Tag (Control, Actual);
800 else
801 Error_Msg_N
802 ("controlling argument is not dynamically tagged",
803 Actual);
804 return;
805 end if;
806 end if;
808 Next_Actual (Actual);
809 end loop;
811 -- Mark call as a dispatching call
813 Set_Controlling_Argument (N, Control);
814 Check_Restriction (No_Dispatching_Calls, N);
816 -- The dispatching call may need to be converted into a direct
817 -- call in certain cases.
819 Check_Direct_Call;
821 -- If there is a statically tagged actual and a tag-indeterminate
822 -- call to a function of the ancestor (such as that provided by a
823 -- default), then treat this as a dispatching call and propagate
824 -- the tag to the tag-indeterminate call(s).
826 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
827 Control :=
828 Make_Attribute_Reference (Loc,
829 Prefix =>
830 New_Occurrence_Of (Etype (Static_Tag), Loc),
831 Attribute_Name => Name_Tag);
833 Analyze (Control);
835 Actual := First_Actual (N);
836 Formal := First_Formal (Subp_Entity);
837 while Present (Actual) loop
838 if Is_Tag_Indeterminate (Actual)
839 and then Is_Controlling_Formal (Formal)
840 then
841 Propagate_Tag (Control, Actual);
842 end if;
844 Next_Actual (Actual);
845 Next_Formal (Formal);
846 end loop;
848 Check_Dispatching_Context (N);
850 elsif Nkind (N) /= N_Function_Call then
852 -- The call is not dispatching, so check that there aren't any
853 -- tag-indeterminate abstract calls left among its actuals.
855 Actual := First_Actual (N);
856 while Present (Actual) loop
857 if Is_Tag_Indeterminate (Actual) then
859 -- Function call case
861 if Nkind (Original_Node (Actual)) = N_Function_Call then
862 Func := Entity (Name (Original_Node (Actual)));
864 -- If the actual is an attribute then it can't be abstract
865 -- (the only current case of a tag-indeterminate attribute
866 -- is the stream Input attribute).
868 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
869 then
870 Func := Empty;
872 -- Ditto if it is an explicit dereference
874 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
875 then
876 Func := Empty;
878 -- Only other possibility is a qualified expression whose
879 -- constituent expression is itself a call.
881 else
882 Func :=
883 Entity (Name (Original_Node
884 (Expression (Original_Node (Actual)))));
885 end if;
887 if Present (Func) and then Is_Abstract_Subprogram (Func) then
888 Error_Msg_N
889 ("call to abstract function must be dispatching",
890 Actual);
891 end if;
892 end if;
894 Next_Actual (Actual);
895 end loop;
897 Check_Dispatching_Context (N);
898 return;
900 elsif Nkind (Parent (N)) in N_Subexpr then
901 Check_Dispatching_Context (N);
903 elsif Nkind (Parent (N)) = N_Assignment_Statement
904 and then Is_Class_Wide_Type (Etype (Name (Parent (N))))
905 then
906 return;
908 elsif Is_Abstract_Subprogram (Subp_Entity) then
909 Check_Dispatching_Context (N);
910 return;
911 end if;
913 else
914 -- If dispatching on result, the enclosing call, if any, will
915 -- determine the controlling argument. Otherwise this is the
916 -- primitive operation of the root type.
918 Check_Dispatching_Context (N);
919 end if;
920 end Check_Dispatching_Call;
922 ---------------------------------
923 -- Check_Dispatching_Operation --
924 ---------------------------------
926 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
927 procedure Warn_On_Late_Primitive_After_Private_Extension
928 (Typ : Entity_Id;
929 Prim : Entity_Id);
930 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
931 -- if it is a public primitive defined after some private extension of
932 -- the tagged type.
934 ----------------------------------------------------
935 -- Warn_On_Late_Primitive_After_Private_Extension --
936 ----------------------------------------------------
938 procedure Warn_On_Late_Primitive_After_Private_Extension
939 (Typ : Entity_Id;
940 Prim : Entity_Id)
942 E : Entity_Id;
944 begin
945 if Warn_On_Late_Primitives
946 and then Comes_From_Source (Prim)
947 and then Has_Private_Extension (Typ)
948 and then Is_Package_Or_Generic_Package (Current_Scope)
949 and then not In_Private_Part (Current_Scope)
950 then
951 E := Next_Entity (Typ);
953 while E /= Prim loop
954 if Ekind (E) = E_Record_Type_With_Private
955 and then Etype (E) = Typ
956 then
957 Error_Msg_Name_1 := Chars (Typ);
958 Error_Msg_Name_2 := Chars (E);
959 Error_Msg_Sloc := Sloc (E);
960 Error_Msg_N
961 ("?j?primitive of type % defined after private extension "
962 & "% #?", Prim);
963 Error_Msg_Name_1 := Chars (Prim);
964 Error_Msg_Name_2 := Chars (E);
965 Error_Msg_N
966 ("\spec of % should appear before declaration of type %!",
967 Prim);
968 exit;
969 end if;
971 Next_Entity (E);
972 end loop;
973 end if;
974 end Warn_On_Late_Primitive_After_Private_Extension;
976 -- Local variables
978 Body_Is_Last_Primitive : Boolean := False;
979 Has_Dispatching_Parent : Boolean := False;
980 Ovr_Subp : Entity_Id := Empty;
981 Tagged_Type : Entity_Id;
983 -- Start of processing for Check_Dispatching_Operation
985 begin
986 if not Ekind_In (Subp, E_Function, E_Procedure) then
987 return;
989 -- The Default_Initial_Condition procedure is not a primitive subprogram
990 -- even if it relates to a tagged type. This routine is not meant to be
991 -- inherited or overridden.
993 elsif Is_DIC_Procedure (Subp) then
994 return;
996 -- The "partial" and "full" type invariant procedures are not primitive
997 -- subprograms even if they relate to a tagged type. These routines are
998 -- not meant to be inherited or overridden.
1000 elsif Is_Invariant_Procedure (Subp)
1001 or else Is_Partial_Invariant_Procedure (Subp)
1002 then
1003 return;
1004 end if;
1006 Set_Is_Dispatching_Operation (Subp, False);
1007 Tagged_Type := Find_Dispatching_Type (Subp);
1009 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1010 -- Required because primitives of concurrent types are attached
1011 -- to the corresponding record (not to the concurrent type).
1013 if Ada_Version >= Ada_2005
1014 and then Present (Tagged_Type)
1015 and then Is_Concurrent_Type (Tagged_Type)
1016 and then Present (Corresponding_Record_Type (Tagged_Type))
1017 then
1018 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
1019 end if;
1021 -- (AI-345): The task body procedure is not a primitive of the tagged
1022 -- type
1024 if Present (Tagged_Type)
1025 and then Is_Concurrent_Record_Type (Tagged_Type)
1026 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
1027 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
1028 and then Subp = Get_Task_Body_Procedure
1029 (Corresponding_Concurrent_Type (Tagged_Type))
1030 then
1031 return;
1032 end if;
1034 -- If Subp is derived from a dispatching operation then it should
1035 -- always be treated as dispatching. In this case various checks
1036 -- below will be bypassed. Makes sure that late declarations for
1037 -- inherited private subprograms are treated as dispatching, even
1038 -- if the associated tagged type is already frozen.
1040 Has_Dispatching_Parent :=
1041 Present (Alias (Subp))
1042 and then Is_Dispatching_Operation (Alias (Subp));
1044 if No (Tagged_Type) then
1046 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1047 -- with an abstract interface type unless the interface acts as a
1048 -- parent type in a derivation. If the interface type is a formal
1049 -- type then the operation is not primitive and therefore legal.
1051 declare
1052 E : Entity_Id;
1053 Typ : Entity_Id;
1055 begin
1056 E := First_Entity (Subp);
1057 while Present (E) loop
1059 -- For an access parameter, check designated type
1061 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
1062 Typ := Designated_Type (Etype (E));
1063 else
1064 Typ := Etype (E);
1065 end if;
1067 if Comes_From_Source (Subp)
1068 and then Is_Interface (Typ)
1069 and then not Is_Class_Wide_Type (Typ)
1070 and then not Is_Derived_Type (Typ)
1071 and then not Is_Generic_Type (Typ)
1072 and then not In_Instance
1073 then
1074 Error_Msg_N ("??declaration of& is too late!", Subp);
1075 Error_Msg_NE -- CODEFIX??
1076 ("\??spec should appear immediately after declaration of "
1077 & "& !", Subp, Typ);
1078 exit;
1079 end if;
1081 Next_Entity (E);
1082 end loop;
1084 -- In case of functions check also the result type
1086 if Ekind (Subp) = E_Function then
1087 if Is_Access_Type (Etype (Subp)) then
1088 Typ := Designated_Type (Etype (Subp));
1089 else
1090 Typ := Etype (Subp);
1091 end if;
1093 -- The following should be better commented, especially since
1094 -- we just added several new conditions here ???
1096 if Comes_From_Source (Subp)
1097 and then Is_Interface (Typ)
1098 and then not Is_Class_Wide_Type (Typ)
1099 and then not Is_Derived_Type (Typ)
1100 and then not Is_Generic_Type (Typ)
1101 and then not In_Instance
1102 then
1103 Error_Msg_N ("??declaration of& is too late!", Subp);
1104 Error_Msg_NE
1105 ("\??spec should appear immediately after declaration of "
1106 & "& !", Subp, Typ);
1107 end if;
1108 end if;
1109 end;
1111 return;
1113 -- The subprograms build internally after the freezing point (such as
1114 -- init procs, interface thunks, type support subprograms, and Offset
1115 -- to top functions for accessing interface components in variable
1116 -- size tagged types) are not primitives.
1118 elsif Is_Frozen (Tagged_Type)
1119 and then not Comes_From_Source (Subp)
1120 and then not Has_Dispatching_Parent
1121 then
1122 -- Complete decoration of internally built subprograms that override
1123 -- a dispatching primitive. These entities correspond with the
1124 -- following cases:
1126 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1127 -- to override functions of nonabstract null extensions. These
1128 -- primitives were added to the list of primitives of the tagged
1129 -- type by Make_Controlling_Function_Wrappers. However, attribute
1130 -- Is_Dispatching_Operation must be set to true.
1132 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1133 -- primitives.
1135 -- 3. Subprograms associated with stream attributes (built by
1136 -- New_Stream_Subprogram)
1138 -- 4. Wrapper built for inherited operations with inherited class-
1139 -- wide conditions, where the conditions include calls to other
1140 -- overridden primitives. The wrappers include checks on these
1141 -- modified conditions. (AI12-113).
1143 if Present (Old_Subp)
1144 and then Present (Overridden_Operation (Subp))
1145 and then Is_Dispatching_Operation (Old_Subp)
1146 then
1147 pragma Assert
1148 ((Ekind (Subp) = E_Function
1149 and then Is_Dispatching_Operation (Old_Subp)
1150 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1152 or else
1153 (Ekind (Subp) = E_Procedure
1154 and then Is_Dispatching_Operation (Old_Subp)
1155 and then Present (Alias (Old_Subp))
1156 and then Is_Null_Interface_Primitive
1157 (Ultimate_Alias (Old_Subp)))
1159 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1160 or else Get_TSS_Name (Subp) = TSS_Stream_Write
1162 or else Present (Contract (Overridden_Operation (Subp))));
1164 Check_Controlling_Formals (Tagged_Type, Subp);
1165 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1166 Set_Is_Dispatching_Operation (Subp);
1167 end if;
1169 return;
1171 -- The operation may be a child unit, whose scope is the defining
1172 -- package, but which is not a primitive operation of the type.
1174 elsif Is_Child_Unit (Subp) then
1175 return;
1177 -- If the subprogram is not defined in a package spec, the only case
1178 -- where it can be a dispatching op is when it overrides an operation
1179 -- before the freezing point of the type.
1181 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1182 or else In_Package_Body (Scope (Subp)))
1183 and then not Has_Dispatching_Parent
1184 then
1185 if not Comes_From_Source (Subp)
1186 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1187 then
1188 null;
1190 -- If the type is already frozen, the overriding is not allowed
1191 -- except when Old_Subp is not a dispatching operation (which can
1192 -- occur when Old_Subp was inherited by an untagged type). However,
1193 -- a body with no previous spec freezes the type *after* its
1194 -- declaration, and therefore is a legal overriding (unless the type
1195 -- has already been frozen). Only the first such body is legal.
1197 elsif Present (Old_Subp)
1198 and then Is_Dispatching_Operation (Old_Subp)
1199 then
1200 if Comes_From_Source (Subp)
1201 and then
1202 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1203 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1204 then
1205 declare
1206 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1207 Decl_Item : Node_Id;
1209 begin
1210 -- ??? The checks here for whether the type has been frozen
1211 -- prior to the new body are not complete. It's not simple
1212 -- to check frozenness at this point since the body has
1213 -- already caused the type to be prematurely frozen in
1214 -- Analyze_Declarations, but we're forced to recheck this
1215 -- here because of the odd rule interpretation that allows
1216 -- the overriding if the type wasn't frozen prior to the
1217 -- body. The freezing action should probably be delayed
1218 -- until after the spec is seen, but that's a tricky
1219 -- change to the delicate freezing code.
1221 -- Look at each declaration following the type up until the
1222 -- new subprogram body. If any of the declarations is a body
1223 -- then the type has been frozen already so the overriding
1224 -- primitive is illegal.
1226 Decl_Item := Next (Parent (Tagged_Type));
1227 while Present (Decl_Item)
1228 and then (Decl_Item /= Subp_Body)
1229 loop
1230 if Comes_From_Source (Decl_Item)
1231 and then (Nkind (Decl_Item) in N_Proper_Body
1232 or else Nkind (Decl_Item) in N_Body_Stub)
1233 then
1234 Error_Msg_N ("overriding of& is too late!", Subp);
1235 Error_Msg_N
1236 ("\spec should appear immediately after the type!",
1237 Subp);
1238 exit;
1239 end if;
1241 Next (Decl_Item);
1242 end loop;
1244 -- If the subprogram doesn't follow in the list of
1245 -- declarations including the type then the type has
1246 -- definitely been frozen already and the body is illegal.
1248 if No (Decl_Item) then
1249 Error_Msg_N ("overriding of& is too late!", Subp);
1250 Error_Msg_N
1251 ("\spec should appear immediately after the type!",
1252 Subp);
1254 elsif Is_Frozen (Subp) then
1256 -- The subprogram body declares a primitive operation.
1257 -- If the subprogram is already frozen, we must update
1258 -- its dispatching information explicitly here. The
1259 -- information is taken from the overridden subprogram.
1260 -- We must also generate a cross-reference entry because
1261 -- references to other primitives were already created
1262 -- when type was frozen.
1264 Body_Is_Last_Primitive := True;
1266 if Present (DTC_Entity (Old_Subp)) then
1267 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1268 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1270 if not Restriction_Active (No_Dispatching_Calls) then
1271 if Building_Static_DT (Tagged_Type) then
1273 -- If the static dispatch table has not been
1274 -- built then there is nothing else to do now;
1275 -- otherwise we notify that we cannot build the
1276 -- static dispatch table.
1278 if Has_Dispatch_Table (Tagged_Type) then
1279 Error_Msg_N
1280 ("overriding of& is too late for building "
1281 & " static dispatch tables!", Subp);
1282 Error_Msg_N
1283 ("\spec should appear immediately after "
1284 & "the type!", Subp);
1285 end if;
1287 -- No code required to register primitives in VM
1288 -- targets
1290 elsif not Tagged_Type_Expansion then
1291 null;
1293 else
1294 Insert_Actions_After (Subp_Body,
1295 Register_Primitive (Sloc (Subp_Body),
1296 Prim => Subp));
1297 end if;
1299 -- Indicate that this is an overriding operation,
1300 -- and replace the overridden entry in the list of
1301 -- primitive operations, which is used for xref
1302 -- generation subsequently.
1304 Generate_Reference (Tagged_Type, Subp, 'P', False);
1305 Override_Dispatching_Operation
1306 (Tagged_Type, Old_Subp, Subp);
1307 end if;
1308 end if;
1309 end if;
1310 end;
1312 else
1313 Error_Msg_N ("overriding of& is too late!", Subp);
1314 Error_Msg_N
1315 ("\subprogram spec should appear immediately after the type!",
1316 Subp);
1317 end if;
1319 -- If the type is not frozen yet and we are not in the overriding
1320 -- case it looks suspiciously like an attempt to define a primitive
1321 -- operation, which requires the declaration to be in a package spec
1322 -- (3.2.3(6)). Only report cases where the type and subprogram are
1323 -- in the same declaration list (by checking the enclosing parent
1324 -- declarations), to avoid spurious warnings on subprograms in
1325 -- instance bodies when the type is declared in the instance spec
1326 -- but hasn't been frozen by the instance body.
1328 elsif not Is_Frozen (Tagged_Type)
1329 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1330 then
1331 Error_Msg_N
1332 ("??not dispatching (must be defined in a package spec)", Subp);
1333 return;
1335 -- When the type is frozen, it is legitimate to define a new
1336 -- non-primitive operation.
1338 else
1339 return;
1340 end if;
1342 -- Now, we are sure that the scope is a package spec. If the subprogram
1343 -- is declared after the freezing point of the type that's an error
1345 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1346 Error_Msg_N ("this primitive operation is declared too late", Subp);
1347 Error_Msg_NE
1348 ("??no primitive operations for& after this line",
1349 Freeze_Node (Tagged_Type),
1350 Tagged_Type);
1351 return;
1352 end if;
1354 Check_Controlling_Formals (Tagged_Type, Subp);
1356 Ovr_Subp := Old_Subp;
1358 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1359 -- overridden by Subp. This only applies to source subprograms, and
1360 -- their declaration must carry an explicit overriding indicator.
1362 if No (Ovr_Subp)
1363 and then Ada_Version >= Ada_2012
1364 and then Comes_From_Source (Subp)
1365 and then
1366 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1367 then
1368 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1370 -- Verify that the proper overriding indicator has been supplied.
1372 if Present (Ovr_Subp)
1373 and then
1374 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1375 then
1376 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1377 end if;
1378 end if;
1380 -- Now it should be a correct primitive operation, put it in the list
1382 if Present (Ovr_Subp) then
1384 -- If the type has interfaces we complete this check after we set
1385 -- attribute Is_Dispatching_Operation.
1387 Check_Subtype_Conformant (Subp, Ovr_Subp);
1389 -- A primitive operation with the name of a primitive controlled
1390 -- operation does not override a non-visible overriding controlled
1391 -- operation, i.e. one declared in a private part when the full
1392 -- view of a type is controlled. Conversely, it will override a
1393 -- visible operation that may be declared in a partial view when
1394 -- the full view is controlled.
1396 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1397 and then Is_Controlled (Tagged_Type)
1398 and then not Is_Visibly_Controlled (Tagged_Type)
1399 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1400 then
1401 Set_Overridden_Operation (Subp, Empty);
1403 -- If the subprogram specification carries an overriding
1404 -- indicator, no need for the warning: it is either redundant,
1405 -- or else an error will be reported.
1407 if Nkind (Parent (Subp)) = N_Procedure_Specification
1408 and then
1409 (Must_Override (Parent (Subp))
1410 or else Must_Not_Override (Parent (Subp)))
1411 then
1412 null;
1414 -- Here we need the warning
1416 else
1417 Error_Msg_NE
1418 ("operation does not override inherited&??", Subp, Subp);
1419 end if;
1421 else
1422 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1424 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1425 -- that covers abstract interface subprograms we must register it
1426 -- in all the secondary dispatch tables associated with abstract
1427 -- interfaces. We do this now only if not building static tables,
1428 -- nor when the expander is inactive (we avoid trying to register
1429 -- primitives in semantics-only mode, since the type may not have
1430 -- an associated dispatch table). Otherwise the patch code is
1431 -- emitted after those tables are built, to prevent access before
1432 -- elaboration in gigi.
1434 if Body_Is_Last_Primitive and then Expander_Active then
1435 declare
1436 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1437 Elmt : Elmt_Id;
1438 Prim : Node_Id;
1440 begin
1441 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1442 while Present (Elmt) loop
1443 Prim := Node (Elmt);
1445 -- No code required to register primitives in VM targets
1447 if Present (Alias (Prim))
1448 and then Present (Interface_Alias (Prim))
1449 and then Alias (Prim) = Subp
1450 and then not Building_Static_DT (Tagged_Type)
1451 and then Tagged_Type_Expansion
1452 then
1453 Insert_Actions_After (Subp_Body,
1454 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1455 end if;
1457 Next_Elmt (Elmt);
1458 end loop;
1460 -- Redisplay the contents of the updated dispatch table
1462 if Debug_Flag_ZZ then
1463 Write_Str ("Late overriding: ");
1464 Write_DT (Tagged_Type);
1465 end if;
1466 end;
1467 end if;
1468 end if;
1470 -- If the tagged type is a concurrent type then we must be compiling
1471 -- with no code generation (we are either compiling a generic unit or
1472 -- compiling under -gnatc mode) because we have previously tested that
1473 -- no serious errors has been reported. In this case we do not add the
1474 -- primitive to the list of primitives of Tagged_Type but we leave the
1475 -- primitive decorated as a dispatching operation to be able to analyze
1476 -- and report errors associated with the Object.Operation notation.
1478 elsif Is_Concurrent_Type (Tagged_Type) then
1479 pragma Assert (not Expander_Active);
1481 -- Attach operation to list of primitives of the synchronized type
1482 -- itself, for ASIS use.
1484 Add_Dispatching_Operation (Tagged_Type, Subp);
1486 -- If no old subprogram, then we add this as a dispatching operation,
1487 -- but we avoid doing this if an error was posted, to prevent annoying
1488 -- cascaded errors.
1490 elsif not Error_Posted (Subp) then
1491 Add_Dispatching_Operation (Tagged_Type, Subp);
1492 end if;
1494 Set_Is_Dispatching_Operation (Subp, True);
1496 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1497 -- subtype conformance against all the interfaces covered by this
1498 -- primitive.
1500 if Present (Ovr_Subp)
1501 and then Has_Interfaces (Tagged_Type)
1502 then
1503 declare
1504 Ifaces_List : Elist_Id;
1505 Iface_Elmt : Elmt_Id;
1506 Iface_Prim_Elmt : Elmt_Id;
1507 Iface_Prim : Entity_Id;
1508 Ret_Typ : Entity_Id;
1510 begin
1511 Collect_Interfaces (Tagged_Type, Ifaces_List);
1513 Iface_Elmt := First_Elmt (Ifaces_List);
1514 while Present (Iface_Elmt) loop
1515 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1516 Iface_Prim_Elmt :=
1517 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1518 while Present (Iface_Prim_Elmt) loop
1519 Iface_Prim := Node (Iface_Prim_Elmt);
1521 if Is_Interface_Conformant
1522 (Tagged_Type, Iface_Prim, Subp)
1523 then
1524 -- Handle procedures, functions whose return type
1525 -- matches, or functions not returning interfaces
1527 if Ekind (Subp) = E_Procedure
1528 or else Etype (Iface_Prim) = Etype (Subp)
1529 or else not Is_Interface (Etype (Iface_Prim))
1530 then
1531 Check_Subtype_Conformant
1532 (New_Id => Subp,
1533 Old_Id => Iface_Prim,
1534 Err_Loc => Subp,
1535 Skip_Controlling_Formals => True);
1537 -- Handle functions returning interfaces
1539 elsif Implements_Interface
1540 (Etype (Subp), Etype (Iface_Prim))
1541 then
1542 -- Temporarily force both entities to return the
1543 -- same type. Required because Subtype_Conformant
1544 -- does not handle this case.
1546 Ret_Typ := Etype (Iface_Prim);
1547 Set_Etype (Iface_Prim, Etype (Subp));
1549 Check_Subtype_Conformant
1550 (New_Id => Subp,
1551 Old_Id => Iface_Prim,
1552 Err_Loc => Subp,
1553 Skip_Controlling_Formals => True);
1555 Set_Etype (Iface_Prim, Ret_Typ);
1556 end if;
1557 end if;
1559 Next_Elmt (Iface_Prim_Elmt);
1560 end loop;
1561 end if;
1563 Next_Elmt (Iface_Elmt);
1564 end loop;
1565 end;
1566 end if;
1568 if not Body_Is_Last_Primitive then
1569 Set_DT_Position_Value (Subp, No_Uint);
1571 elsif Has_Controlled_Component (Tagged_Type)
1572 and then Nam_In (Chars (Subp), Name_Initialize,
1573 Name_Adjust,
1574 Name_Finalize,
1575 Name_Finalize_Address)
1576 then
1577 declare
1578 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1579 Decl : Node_Id;
1580 Old_P : Entity_Id;
1581 Old_Bod : Node_Id;
1582 Old_Spec : Entity_Id;
1584 C_Names : constant array (1 .. 4) of Name_Id :=
1585 (Name_Initialize,
1586 Name_Adjust,
1587 Name_Finalize,
1588 Name_Finalize_Address);
1590 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1591 (TSS_Deep_Initialize,
1592 TSS_Deep_Adjust,
1593 TSS_Deep_Finalize,
1594 TSS_Finalize_Address);
1596 begin
1597 -- Remove previous controlled function which was constructed and
1598 -- analyzed when the type was frozen. This requires removing the
1599 -- body of the redefined primitive, as well as its specification
1600 -- if needed (there is no spec created for Deep_Initialize, see
1601 -- exp_ch3.adb). We must also dismantle the exception information
1602 -- that may have been generated for it when front end zero-cost
1603 -- tables are enabled.
1605 for J in D_Names'Range loop
1606 Old_P := TSS (Tagged_Type, D_Names (J));
1608 if Present (Old_P)
1609 and then Chars (Subp) = C_Names (J)
1610 then
1611 Old_Bod := Unit_Declaration_Node (Old_P);
1612 Remove (Old_Bod);
1613 Set_Is_Eliminated (Old_P);
1614 Set_Scope (Old_P, Scope (Current_Scope));
1616 if Nkind (Old_Bod) = N_Subprogram_Body
1617 and then Present (Corresponding_Spec (Old_Bod))
1618 then
1619 Old_Spec := Corresponding_Spec (Old_Bod);
1620 Set_Has_Completion (Old_Spec, False);
1621 end if;
1622 end if;
1623 end loop;
1625 Build_Late_Proc (Tagged_Type, Chars (Subp));
1627 -- The new operation is added to the actions of the freeze node
1628 -- for the type, but this node has already been analyzed, so we
1629 -- must retrieve and analyze explicitly the new body.
1631 if Present (F_Node)
1632 and then Present (Actions (F_Node))
1633 then
1634 Decl := Last (Actions (F_Node));
1635 Analyze (Decl);
1636 end if;
1637 end;
1638 end if;
1640 -- For similarity with record extensions, in Ada 9X the language should
1641 -- have disallowed adding visible operations to a tagged type after
1642 -- deriving a private extension from it. Report a warning if this
1643 -- primitive is defined after a private extension of Tagged_Type.
1645 Warn_On_Late_Primitive_After_Private_Extension (Tagged_Type, Subp);
1646 end Check_Dispatching_Operation;
1648 ------------------------------------------
1649 -- Check_Operation_From_Incomplete_Type --
1650 ------------------------------------------
1652 procedure Check_Operation_From_Incomplete_Type
1653 (Subp : Entity_Id;
1654 Typ : Entity_Id)
1656 Full : constant Entity_Id := Full_View (Typ);
1657 Parent_Typ : constant Entity_Id := Etype (Full);
1658 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1659 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1660 Op1, Op2 : Elmt_Id;
1661 Prev : Elmt_Id := No_Elmt;
1663 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1664 -- Check that Subp has profile of an operation derived from Parent_Subp.
1665 -- Subp must have a parameter or result type that is Typ or an access
1666 -- parameter or access result type that designates Typ.
1668 ------------------
1669 -- Derives_From --
1670 ------------------
1672 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1673 F1, F2 : Entity_Id;
1675 begin
1676 if Chars (Parent_Subp) /= Chars (Subp) then
1677 return False;
1678 end if;
1680 -- Check that the type of controlling formals is derived from the
1681 -- parent subprogram's controlling formal type (or designated type
1682 -- if the formal type is an anonymous access type).
1684 F1 := First_Formal (Parent_Subp);
1685 F2 := First_Formal (Subp);
1686 while Present (F1) and then Present (F2) loop
1687 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1688 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1689 return False;
1690 elsif Designated_Type (Etype (F1)) = Parent_Typ
1691 and then Designated_Type (Etype (F2)) /= Full
1692 then
1693 return False;
1694 end if;
1696 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1697 return False;
1699 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1700 return False;
1701 end if;
1703 Next_Formal (F1);
1704 Next_Formal (F2);
1705 end loop;
1707 -- Check that a controlling result type is derived from the parent
1708 -- subprogram's result type (or designated type if the result type
1709 -- is an anonymous access type).
1711 if Ekind (Parent_Subp) = E_Function then
1712 if Ekind (Subp) /= E_Function then
1713 return False;
1715 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1716 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1717 return False;
1719 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1720 and then Designated_Type (Etype (Subp)) /= Full
1721 then
1722 return False;
1723 end if;
1725 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1726 return False;
1728 elsif Etype (Parent_Subp) = Parent_Typ
1729 and then Etype (Subp) /= Full
1730 then
1731 return False;
1732 end if;
1734 elsif Ekind (Subp) = E_Function then
1735 return False;
1736 end if;
1738 return No (F1) and then No (F2);
1739 end Derives_From;
1741 -- Start of processing for Check_Operation_From_Incomplete_Type
1743 begin
1744 -- The operation may override an inherited one, or may be a new one
1745 -- altogether. The inherited operation will have been hidden by the
1746 -- current one at the point of the type derivation, so it does not
1747 -- appear in the list of primitive operations of the type. We have to
1748 -- find the proper place of insertion in the list of primitive opera-
1749 -- tions by iterating over the list for the parent type.
1751 Op1 := First_Elmt (Old_Prim);
1752 Op2 := First_Elmt (New_Prim);
1753 while Present (Op1) and then Present (Op2) loop
1754 if Derives_From (Node (Op1)) then
1755 if No (Prev) then
1757 -- Avoid adding it to the list of primitives if already there
1759 if Node (Op2) /= Subp then
1760 Prepend_Elmt (Subp, New_Prim);
1761 end if;
1763 else
1764 Insert_Elmt_After (Subp, Prev);
1765 end if;
1767 return;
1768 end if;
1770 Prev := Op2;
1771 Next_Elmt (Op1);
1772 Next_Elmt (Op2);
1773 end loop;
1775 -- Operation is a new primitive
1777 Append_Elmt (Subp, New_Prim);
1778 end Check_Operation_From_Incomplete_Type;
1780 ---------------------------------------
1781 -- Check_Operation_From_Private_View --
1782 ---------------------------------------
1784 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1785 Tagged_Type : Entity_Id;
1787 begin
1788 if Is_Dispatching_Operation (Alias (Subp)) then
1789 Set_Scope (Subp, Current_Scope);
1790 Tagged_Type := Find_Dispatching_Type (Subp);
1792 -- Add Old_Subp to primitive operations if not already present
1794 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1795 Add_Dispatching_Operation (Tagged_Type, Old_Subp);
1797 -- If Old_Subp isn't already marked as dispatching then this is
1798 -- the case of an operation of an untagged private type fulfilled
1799 -- by a tagged type that overrides an inherited dispatching
1800 -- operation, so we set the necessary dispatching attributes here.
1802 if not Is_Dispatching_Operation (Old_Subp) then
1804 -- If the untagged type has no discriminants, and the full
1805 -- view is constrained, there will be a spurious mismatch of
1806 -- subtypes on the controlling arguments, because the tagged
1807 -- type is the internal base type introduced in the derivation.
1808 -- Use the original type to verify conformance, rather than the
1809 -- base type.
1811 if not Comes_From_Source (Tagged_Type)
1812 and then Has_Discriminants (Tagged_Type)
1813 then
1814 declare
1815 Formal : Entity_Id;
1817 begin
1818 Formal := First_Formal (Old_Subp);
1819 while Present (Formal) loop
1820 if Tagged_Type = Base_Type (Etype (Formal)) then
1821 Tagged_Type := Etype (Formal);
1822 end if;
1824 Next_Formal (Formal);
1825 end loop;
1826 end;
1828 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1829 Tagged_Type := Etype (Old_Subp);
1830 end if;
1831 end if;
1833 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1834 Set_Is_Dispatching_Operation (Old_Subp, True);
1835 Set_DT_Position_Value (Old_Subp, No_Uint);
1836 end if;
1838 -- If the old subprogram is an explicit renaming of some other
1839 -- entity, it is not overridden by the inherited subprogram.
1840 -- Otherwise, update its alias and other attributes.
1842 if Present (Alias (Old_Subp))
1843 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1844 N_Subprogram_Renaming_Declaration
1845 then
1846 Set_Alias (Old_Subp, Alias (Subp));
1848 -- The derived subprogram should inherit the abstractness of
1849 -- the parent subprogram (except in the case of a function
1850 -- returning the type). This sets the abstractness properly
1851 -- for cases where a private extension may have inherited an
1852 -- abstract operation, but the full type is derived from a
1853 -- descendant type and inherits a nonabstract version.
1855 if Etype (Subp) /= Tagged_Type then
1856 Set_Is_Abstract_Subprogram
1857 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1858 end if;
1859 end if;
1860 end if;
1861 end if;
1862 end Check_Operation_From_Private_View;
1864 --------------------------
1865 -- Find_Controlling_Arg --
1866 --------------------------
1868 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1869 Orig_Node : constant Node_Id := Original_Node (N);
1870 Typ : Entity_Id;
1872 begin
1873 if Nkind (Orig_Node) = N_Qualified_Expression then
1874 return Find_Controlling_Arg (Expression (Orig_Node));
1875 end if;
1877 -- Dispatching on result case. If expansion is disabled, the node still
1878 -- has the structure of a function call. However, if the function name
1879 -- is an operator and the call was given in infix form, the original
1880 -- node has no controlling result and we must examine the current node.
1882 if Nkind (N) = N_Function_Call
1883 and then Present (Controlling_Argument (N))
1884 and then Has_Controlling_Result (Entity (Name (N)))
1885 then
1886 return Controlling_Argument (N);
1888 -- If expansion is enabled, the call may have been transformed into
1889 -- an indirect call, and we need to recover the original node.
1891 elsif Nkind (Orig_Node) = N_Function_Call
1892 and then Present (Controlling_Argument (Orig_Node))
1893 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1894 then
1895 return Controlling_Argument (Orig_Node);
1897 -- Type conversions are dynamically tagged if the target type, or its
1898 -- designated type, are classwide. An interface conversion expands into
1899 -- a dereference, so test must be performed on the original node.
1901 elsif Nkind (Orig_Node) = N_Type_Conversion
1902 and then Nkind (N) = N_Explicit_Dereference
1903 and then Is_Controlling_Actual (N)
1904 then
1905 declare
1906 Target_Type : constant Entity_Id :=
1907 Entity (Subtype_Mark (Orig_Node));
1909 begin
1910 if Is_Class_Wide_Type (Target_Type) then
1911 return N;
1913 elsif Is_Access_Type (Target_Type)
1914 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1915 then
1916 return N;
1918 else
1919 return Empty;
1920 end if;
1921 end;
1923 -- Normal case
1925 elsif Is_Controlling_Actual (N)
1926 or else
1927 (Nkind (Parent (N)) = N_Qualified_Expression
1928 and then Is_Controlling_Actual (Parent (N)))
1929 then
1930 Typ := Etype (N);
1932 if Is_Access_Type (Typ) then
1934 -- In the case of an Access attribute, use the type of the prefix,
1935 -- since in the case of an actual for an access parameter, the
1936 -- attribute's type may be of a specific designated type, even
1937 -- though the prefix type is class-wide.
1939 if Nkind (N) = N_Attribute_Reference then
1940 Typ := Etype (Prefix (N));
1942 -- An allocator is dispatching if the type of qualified expression
1943 -- is class_wide, in which case this is the controlling type.
1945 elsif Nkind (Orig_Node) = N_Allocator
1946 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1947 then
1948 Typ := Etype (Expression (Orig_Node));
1949 else
1950 Typ := Designated_Type (Typ);
1951 end if;
1952 end if;
1954 if Is_Class_Wide_Type (Typ)
1955 or else
1956 (Nkind (Parent (N)) = N_Qualified_Expression
1957 and then Is_Access_Type (Etype (N))
1958 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1959 then
1960 return N;
1961 end if;
1962 end if;
1964 return Empty;
1965 end Find_Controlling_Arg;
1967 ---------------------------
1968 -- Find_Dispatching_Type --
1969 ---------------------------
1971 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1972 A_Formal : Entity_Id;
1973 Formal : Entity_Id;
1974 Ctrl_Type : Entity_Id;
1976 begin
1977 if Ekind_In (Subp, E_Function, E_Procedure)
1978 and then Present (DTC_Entity (Subp))
1979 then
1980 return Scope (DTC_Entity (Subp));
1982 -- For subprograms internally generated by derivations of tagged types
1983 -- use the alias subprogram as a reference to locate the dispatching
1984 -- type of Subp.
1986 elsif not Comes_From_Source (Subp)
1987 and then Present (Alias (Subp))
1988 and then Is_Dispatching_Operation (Alias (Subp))
1989 then
1990 if Ekind (Alias (Subp)) = E_Function
1991 and then Has_Controlling_Result (Alias (Subp))
1992 then
1993 return Check_Controlling_Type (Etype (Subp), Subp);
1995 else
1996 Formal := First_Formal (Subp);
1997 A_Formal := First_Formal (Alias (Subp));
1998 while Present (A_Formal) loop
1999 if Is_Controlling_Formal (A_Formal) then
2000 return Check_Controlling_Type (Etype (Formal), Subp);
2001 end if;
2003 Next_Formal (Formal);
2004 Next_Formal (A_Formal);
2005 end loop;
2007 pragma Assert (False);
2008 return Empty;
2009 end if;
2011 -- General case
2013 else
2014 Formal := First_Formal (Subp);
2015 while Present (Formal) loop
2016 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
2018 if Present (Ctrl_Type) then
2019 return Ctrl_Type;
2020 end if;
2022 Next_Formal (Formal);
2023 end loop;
2025 -- The subprogram may also be dispatching on result
2027 if Present (Etype (Subp)) then
2028 return Check_Controlling_Type (Etype (Subp), Subp);
2029 end if;
2030 end if;
2032 pragma Assert (not Is_Dispatching_Operation (Subp));
2033 return Empty;
2034 end Find_Dispatching_Type;
2036 --------------------------------------
2037 -- Find_Hidden_Overridden_Primitive --
2038 --------------------------------------
2040 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
2042 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
2043 Elmt : Elmt_Id;
2044 Orig_Prim : Entity_Id;
2045 Prim : Entity_Id;
2046 Vis_List : Elist_Id;
2048 begin
2049 -- This Ada 2012 rule applies only for type extensions or private
2050 -- extensions, where the parent type is not in a parent unit, and
2051 -- where an operation is never declared but still inherited.
2053 if No (Tag_Typ)
2054 or else not Is_Record_Type (Tag_Typ)
2055 or else Etype (Tag_Typ) = Tag_Typ
2056 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
2057 then
2058 return Empty;
2059 end if;
2061 -- Collect the list of visible ancestor of the tagged type
2063 Vis_List := Visible_Ancestors (Tag_Typ);
2065 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2066 while Present (Elmt) loop
2067 Prim := Node (Elmt);
2069 -- Find an inherited hidden dispatching primitive with the name of S
2070 -- and a type-conformant profile.
2072 if Present (Alias (Prim))
2073 and then Is_Hidden (Alias (Prim))
2074 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
2075 and then Primitive_Names_Match (S, Prim)
2076 and then Type_Conformant (S, Prim)
2077 then
2078 declare
2079 Vis_Ancestor : Elmt_Id;
2080 Elmt : Elmt_Id;
2082 begin
2083 -- The original corresponding operation of Prim must be an
2084 -- operation of a visible ancestor of the dispatching type S,
2085 -- and the original corresponding operation of S2 must be
2086 -- visible.
2088 Orig_Prim := Original_Corresponding_Operation (Prim);
2090 if Orig_Prim /= Prim
2091 and then Is_Immediately_Visible (Orig_Prim)
2092 then
2093 Vis_Ancestor := First_Elmt (Vis_List);
2094 while Present (Vis_Ancestor) loop
2095 Elmt :=
2096 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
2097 while Present (Elmt) loop
2098 if Node (Elmt) = Orig_Prim then
2099 Set_Overridden_Operation (S, Prim);
2100 Set_Alias (Prim, Orig_Prim);
2101 return Prim;
2102 end if;
2104 Next_Elmt (Elmt);
2105 end loop;
2107 Next_Elmt (Vis_Ancestor);
2108 end loop;
2109 end if;
2110 end;
2111 end if;
2113 Next_Elmt (Elmt);
2114 end loop;
2116 return Empty;
2117 end Find_Hidden_Overridden_Primitive;
2119 ---------------------------------------
2120 -- Find_Primitive_Covering_Interface --
2121 ---------------------------------------
2123 function Find_Primitive_Covering_Interface
2124 (Tagged_Type : Entity_Id;
2125 Iface_Prim : Entity_Id) return Entity_Id
2127 E : Entity_Id;
2128 El : Elmt_Id;
2130 begin
2131 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
2132 or else (Present (Alias (Iface_Prim))
2133 and then
2134 Is_Interface
2135 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
2137 -- Search in the homonym chain. Done to speed up locating visible
2138 -- entities and required to catch primitives associated with the partial
2139 -- view of private types when processing the corresponding full view.
2141 E := Current_Entity (Iface_Prim);
2142 while Present (E) loop
2143 if Is_Subprogram (E)
2144 and then Is_Dispatching_Operation (E)
2145 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
2146 then
2147 return E;
2148 end if;
2150 E := Homonym (E);
2151 end loop;
2153 -- Search in the list of primitives of the type. Required to locate
2154 -- the covering primitive if the covering primitive is not visible
2155 -- (for example, non-visible inherited primitive of private type).
2157 El := First_Elmt (Primitive_Operations (Tagged_Type));
2158 while Present (El) loop
2159 E := Node (El);
2161 -- Keep separate the management of internal entities that link
2162 -- primitives with interface primitives from tagged type primitives.
2164 if No (Interface_Alias (E)) then
2165 if Present (Alias (E)) then
2167 -- This interface primitive has not been covered yet
2169 if Alias (E) = Iface_Prim then
2170 return E;
2172 -- The covering primitive was inherited
2174 elsif Overridden_Operation (Ultimate_Alias (E))
2175 = Iface_Prim
2176 then
2177 return E;
2178 end if;
2179 end if;
2181 -- Check if E covers the interface primitive (includes case in
2182 -- which E is an inherited private primitive).
2184 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2185 return E;
2186 end if;
2188 -- Use the internal entity that links the interface primitive with
2189 -- the covering primitive to locate the entity.
2191 elsif Interface_Alias (E) = Iface_Prim then
2192 return Alias (E);
2193 end if;
2195 Next_Elmt (El);
2196 end loop;
2198 -- Not found
2200 return Empty;
2201 end Find_Primitive_Covering_Interface;
2203 ---------------------------
2204 -- Inheritance_Utilities --
2205 ---------------------------
2207 package body Inheritance_Utilities is
2209 ---------------------------
2210 -- Inherited_Subprograms --
2211 ---------------------------
2213 function Inherited_Subprograms
2214 (S : Entity_Id;
2215 No_Interfaces : Boolean := False;
2216 Interfaces_Only : Boolean := False;
2217 One_Only : Boolean := False) return Subprogram_List
2219 Result : Subprogram_List (1 .. 6000);
2220 -- 6000 here is intended to be infinity. We could use an expandable
2221 -- table, but it would be awfully heavy, and there is no way that we
2222 -- could reasonably exceed this value.
2224 N : Nat := 0;
2225 -- Number of entries in Result
2227 Parent_Op : Entity_Id;
2228 -- Traverses the Overridden_Operation chain
2230 procedure Store_IS (E : Entity_Id);
2231 -- Stores E in Result if not already stored
2233 --------------
2234 -- Store_IS --
2235 --------------
2237 procedure Store_IS (E : Entity_Id) is
2238 begin
2239 for J in 1 .. N loop
2240 if E = Result (J) then
2241 return;
2242 end if;
2243 end loop;
2245 N := N + 1;
2246 Result (N) := E;
2247 end Store_IS;
2249 -- Start of processing for Inherited_Subprograms
2251 begin
2252 pragma Assert (not (No_Interfaces and Interfaces_Only));
2254 -- When used from backends, visibility can be handled differently
2255 -- resulting in no dispatching type being found.
2257 if Present (S)
2258 and then Is_Dispatching_Operation (S)
2259 and then Present (Find_DT (S))
2260 then
2261 -- Deal with direct inheritance
2263 if not Interfaces_Only then
2264 Parent_Op := S;
2265 loop
2266 Parent_Op := Overridden_Operation (Parent_Op);
2267 exit when No (Parent_Op)
2268 or else (No_Interfaces
2269 and then Is_Interface (Find_DT (Parent_Op)));
2271 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2272 Store_IS (Parent_Op);
2274 if One_Only then
2275 goto Done;
2276 end if;
2277 end if;
2278 end loop;
2279 end if;
2281 -- Now deal with interfaces
2283 if not No_Interfaces then
2284 declare
2285 Tag_Typ : Entity_Id;
2286 Prim : Entity_Id;
2287 Elmt : Elmt_Id;
2289 begin
2290 Tag_Typ := Find_DT (S);
2292 -- In the presence of limited views there may be no visible
2293 -- dispatching type. Primitives will be inherited when non-
2294 -- limited view is frozen.
2296 if No (Tag_Typ) then
2297 return Result (1 .. 0);
2298 end if;
2300 if Is_Concurrent_Type (Tag_Typ) then
2301 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2302 end if;
2304 -- Search primitive operations of dispatching type
2306 if Present (Tag_Typ)
2307 and then Present (Primitive_Operations (Tag_Typ))
2308 then
2309 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2310 while Present (Elmt) loop
2311 Prim := Node (Elmt);
2313 -- The following test eliminates some odd cases in
2314 -- which Ekind (Prim) is Void, to be investigated
2315 -- further ???
2317 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2318 null;
2320 -- For [generic] subprogram, look at interface
2321 -- alias.
2323 elsif Present (Interface_Alias (Prim))
2324 and then Alias (Prim) = S
2325 then
2326 -- We have found a primitive covered by S
2328 Store_IS (Interface_Alias (Prim));
2330 if One_Only then
2331 goto Done;
2332 end if;
2333 end if;
2335 Next_Elmt (Elmt);
2336 end loop;
2337 end if;
2338 end;
2339 end if;
2340 end if;
2342 <<Done>>
2344 return Result (1 .. N);
2345 end Inherited_Subprograms;
2347 ------------------------------
2348 -- Is_Overriding_Subprogram --
2349 ------------------------------
2351 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2352 Inherited : constant Subprogram_List :=
2353 Inherited_Subprograms (E, One_Only => True);
2354 begin
2355 return Inherited'Length > 0;
2356 end Is_Overriding_Subprogram;
2357 end Inheritance_Utilities;
2359 --------------------------------
2360 -- Inheritance_Utilities_Inst --
2361 --------------------------------
2363 package Inheritance_Utilities_Inst is new
2364 Inheritance_Utilities (Find_Dispatching_Type);
2366 ---------------------------
2367 -- Inherited_Subprograms --
2368 ---------------------------
2370 function Inherited_Subprograms
2371 (S : Entity_Id;
2372 No_Interfaces : Boolean := False;
2373 Interfaces_Only : Boolean := False;
2374 One_Only : Boolean := False) return Subprogram_List renames
2375 Inheritance_Utilities_Inst.Inherited_Subprograms;
2377 ---------------------------
2378 -- Is_Dynamically_Tagged --
2379 ---------------------------
2381 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2382 begin
2383 if Nkind (N) = N_Error then
2384 return False;
2386 elsif Present (Find_Controlling_Arg (N)) then
2387 return True;
2389 -- Special cases: entities, and calls that dispatch on result
2391 elsif Is_Entity_Name (N) then
2392 return Is_Class_Wide_Type (Etype (N));
2394 elsif Nkind (N) = N_Function_Call
2395 and then Is_Class_Wide_Type (Etype (N))
2396 then
2397 return True;
2399 -- Otherwise check whether call has controlling argument
2401 else
2402 return False;
2403 end if;
2404 end Is_Dynamically_Tagged;
2406 ---------------------------------
2407 -- Is_Null_Interface_Primitive --
2408 ---------------------------------
2410 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2411 begin
2412 return Comes_From_Source (E)
2413 and then Is_Dispatching_Operation (E)
2414 and then Ekind (E) = E_Procedure
2415 and then Null_Present (Parent (E))
2416 and then Is_Interface (Find_Dispatching_Type (E));
2417 end Is_Null_Interface_Primitive;
2419 -----------------------------------
2420 -- Is_Inherited_Public_Operation --
2421 -----------------------------------
2423 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
2424 Pack_Decl : Node_Id;
2425 Prim : Entity_Id := Op;
2426 Scop : Entity_Id := Prim;
2428 begin
2429 -- Locate the ultimate non-hidden alias entity
2431 while Present (Alias (Prim)) and then not Is_Hidden (Alias (Prim)) loop
2432 pragma Assert (Alias (Prim) /= Prim);
2433 Prim := Alias (Prim);
2434 Scop := Scope (Prim);
2435 end loop;
2437 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
2438 Pack_Decl := Unit_Declaration_Node (Scop);
2440 return
2441 Nkind (Pack_Decl) = N_Package_Declaration
2442 and then List_Containing (Unit_Declaration_Node (Prim)) =
2443 Visible_Declarations (Specification (Pack_Decl));
2445 else
2446 return False;
2447 end if;
2448 end Is_Inherited_Public_Operation;
2450 ------------------------------
2451 -- Is_Overriding_Subprogram --
2452 ------------------------------
2454 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean renames
2455 Inheritance_Utilities_Inst.Is_Overriding_Subprogram;
2457 --------------------------
2458 -- Is_Tag_Indeterminate --
2459 --------------------------
2461 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2462 Nam : Entity_Id;
2463 Actual : Node_Id;
2464 Orig_Node : constant Node_Id := Original_Node (N);
2466 begin
2467 if Nkind (Orig_Node) = N_Function_Call
2468 and then Is_Entity_Name (Name (Orig_Node))
2469 then
2470 Nam := Entity (Name (Orig_Node));
2472 if not Has_Controlling_Result (Nam) then
2473 return False;
2475 -- The function may have a controlling result, but if the return type
2476 -- is not visibly tagged, then this is not tag-indeterminate.
2478 elsif Is_Access_Type (Etype (Nam))
2479 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2480 then
2481 return False;
2483 -- An explicit dereference means that the call has already been
2484 -- expanded and there is no tag to propagate.
2486 elsif Nkind (N) = N_Explicit_Dereference then
2487 return False;
2489 -- If there are no actuals, the call is tag-indeterminate
2491 elsif No (Parameter_Associations (Orig_Node)) then
2492 return True;
2494 else
2495 Actual := First_Actual (Orig_Node);
2496 while Present (Actual) loop
2497 if Is_Controlling_Actual (Actual)
2498 and then not Is_Tag_Indeterminate (Actual)
2499 then
2500 -- One operand is dispatching
2502 return False;
2503 end if;
2505 Next_Actual (Actual);
2506 end loop;
2508 return True;
2509 end if;
2511 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2512 return Is_Tag_Indeterminate (Expression (Orig_Node));
2514 -- Case of a call to the Input attribute (possibly rewritten), which is
2515 -- always tag-indeterminate except when its prefix is a Class attribute.
2517 elsif Nkind (Orig_Node) = N_Attribute_Reference
2518 and then
2519 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2520 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2521 then
2522 return True;
2524 -- In Ada 2005, a function that returns an anonymous access type can be
2525 -- dispatching, and the dereference of a call to such a function can
2526 -- also be tag-indeterminate if the call itself is.
2528 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2529 and then Ada_Version >= Ada_2005
2530 then
2531 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2533 else
2534 return False;
2535 end if;
2536 end Is_Tag_Indeterminate;
2538 ------------------------------------
2539 -- Override_Dispatching_Operation --
2540 ------------------------------------
2542 procedure Override_Dispatching_Operation
2543 (Tagged_Type : Entity_Id;
2544 Prev_Op : Entity_Id;
2545 New_Op : Entity_Id;
2546 Is_Wrapper : Boolean := False)
2548 Elmt : Elmt_Id;
2549 Prim : Node_Id;
2551 begin
2552 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2553 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2555 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2556 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2557 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2558 end if;
2560 -- If there is no previous operation to override, the type declaration
2561 -- was malformed, and an error must have been emitted already.
2563 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2564 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
2565 Next_Elmt (Elmt);
2566 end loop;
2568 if No (Elmt) then
2569 return;
2570 end if;
2572 -- The location of entities that come from source in the list of
2573 -- primitives of the tagged type must follow their order of occurrence
2574 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2575 -- primitive of an interface that is not implemented by the parents of
2576 -- this tagged type (that is, it is an alias of an interface primitive
2577 -- generated by Derive_Interface_Progenitors), then we must append the
2578 -- new entity at the end of the list of primitives.
2580 if Present (Alias (Prev_Op))
2581 and then Etype (Tagged_Type) /= Tagged_Type
2582 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2583 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2584 Tagged_Type, Use_Full_View => True)
2585 and then not Implements_Interface
2586 (Etype (Tagged_Type),
2587 Find_Dispatching_Type (Alias (Prev_Op)))
2588 then
2589 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2590 Add_Dispatching_Operation (Tagged_Type, New_Op);
2592 -- The new primitive replaces the overridden entity. Required to ensure
2593 -- that overriding primitive is assigned the same dispatch table slot.
2595 else
2596 Replace_Elmt (Elmt, New_Op);
2597 end if;
2599 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
2601 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2602 -- entities of the overridden primitive to reference New_Op, and
2603 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2604 -- that the new operation is subtype conformant with the interface
2605 -- operations that it implements (for operations inherited from the
2606 -- parent itself, this check is made when building the derived type).
2608 -- Note: This code is executed with internally generated wrappers of
2609 -- functions with controlling result and late overridings.
2611 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2612 while Present (Elmt) loop
2613 Prim := Node (Elmt);
2615 if Prim = New_Op then
2616 null;
2618 -- Note: The check on Is_Subprogram protects the frontend against
2619 -- reading attributes in entities that are not yet fully decorated
2621 elsif Is_Subprogram (Prim)
2622 and then Present (Interface_Alias (Prim))
2623 and then Alias (Prim) = Prev_Op
2624 then
2625 Set_Alias (Prim, New_Op);
2627 -- No further decoration needed yet for internally generated
2628 -- wrappers of controlling functions since (at this stage)
2629 -- they are not yet decorated.
2631 if not Is_Wrapper then
2632 Check_Subtype_Conformant (New_Op, Prim);
2634 Set_Is_Abstract_Subprogram (Prim,
2635 Is_Abstract_Subprogram (New_Op));
2637 -- Ensure that this entity will be expanded to fill the
2638 -- corresponding entry in its dispatch table.
2640 if not Is_Abstract_Subprogram (Prim) then
2641 Set_Has_Delayed_Freeze (Prim);
2642 end if;
2643 end if;
2644 end if;
2646 Next_Elmt (Elmt);
2647 end loop;
2648 end if;
2650 if (not Is_Package_Or_Generic_Package (Current_Scope))
2651 or else not In_Private_Part (Current_Scope)
2652 then
2653 -- Not a private primitive
2655 null;
2657 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2659 -- Make the overriding operation into an alias of the implicit one.
2660 -- In this fashion a call from outside ends up calling the new body
2661 -- even if non-dispatching, and a call from inside calls the over-
2662 -- riding operation because it hides the implicit one. To indicate
2663 -- that the body of Prev_Op is never called, set its dispatch table
2664 -- entity to Empty. If the overridden operation has a dispatching
2665 -- result, so does the overriding one.
2667 Set_Alias (Prev_Op, New_Op);
2668 Set_DTC_Entity (Prev_Op, Empty);
2669 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2670 return;
2671 end if;
2672 end Override_Dispatching_Operation;
2674 -------------------
2675 -- Propagate_Tag --
2676 -------------------
2678 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2679 Call_Node : Node_Id;
2680 Arg : Node_Id;
2682 begin
2683 if Nkind (Actual) = N_Function_Call then
2684 Call_Node := Actual;
2686 elsif Nkind (Actual) = N_Identifier
2687 and then Nkind (Original_Node (Actual)) = N_Function_Call
2688 then
2689 -- Call rewritten as object declaration when stack-checking is
2690 -- enabled. Propagate tag to expression in declaration, which is
2691 -- original call.
2693 Call_Node := Expression (Parent (Entity (Actual)));
2695 -- Ada 2005: If this is a dereference of a call to a function with a
2696 -- dispatching access-result, the tag is propagated when the dereference
2697 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2699 elsif Nkind (Actual) = N_Explicit_Dereference
2700 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2701 then
2702 return;
2704 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2705 -- and in that case we can simply return.
2707 elsif Nkind (Actual) = N_Attribute_Reference then
2708 pragma Assert (Attribute_Name (Actual) = Name_Input);
2710 return;
2712 -- Only other possibilities are parenthesized or qualified expression,
2713 -- or an expander-generated unchecked conversion of a function call to
2714 -- a stream Input attribute.
2716 else
2717 Call_Node := Expression (Actual);
2718 end if;
2720 -- No action needed if the call has been already expanded
2722 if Is_Expanded_Dispatching_Call (Call_Node) then
2723 return;
2724 end if;
2726 -- Do not set the Controlling_Argument if already set. This happens in
2727 -- the special case of _Input (see Exp_Attr, case Input).
2729 if No (Controlling_Argument (Call_Node)) then
2730 Set_Controlling_Argument (Call_Node, Control);
2731 end if;
2733 Arg := First_Actual (Call_Node);
2734 while Present (Arg) loop
2735 if Is_Tag_Indeterminate (Arg) then
2736 Propagate_Tag (Control, Arg);
2737 end if;
2739 Next_Actual (Arg);
2740 end loop;
2742 -- Expansion of dispatching calls is suppressed on VM targets, because
2743 -- the VM back-ends directly handle the generation of dispatching calls
2744 -- and would have to undo any expansion to an indirect call.
2746 if Tagged_Type_Expansion then
2747 declare
2748 Call_Typ : constant Entity_Id := Etype (Call_Node);
2750 begin
2751 Expand_Dispatching_Call (Call_Node);
2753 -- If the controlling argument is an interface type and the type
2754 -- of Call_Node differs then we must add an implicit conversion to
2755 -- force displacement of the pointer to the object to reference
2756 -- the secondary dispatch table of the interface.
2758 if Is_Interface (Etype (Control))
2759 and then Etype (Control) /= Call_Typ
2760 then
2761 -- Cannot use Convert_To because the previous call to
2762 -- Expand_Dispatching_Call leaves decorated the Call_Node
2763 -- with the type of Control.
2765 Rewrite (Call_Node,
2766 Make_Type_Conversion (Sloc (Call_Node),
2767 Subtype_Mark =>
2768 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2769 Expression => Relocate_Node (Call_Node)));
2770 Set_Etype (Call_Node, Etype (Control));
2771 Set_Analyzed (Call_Node);
2773 Expand_Interface_Conversion (Call_Node);
2774 end if;
2775 end;
2777 -- Expansion of a dispatching call results in an indirect call, which in
2778 -- turn causes current values to be killed (see Resolve_Call), so on VM
2779 -- targets we do the call here to ensure consistent warnings between VM
2780 -- and non-VM targets.
2782 else
2783 Kill_Current_Values;
2784 end if;
2785 end Propagate_Tag;
2787 end Sem_Disp;