2014-02-20 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_disp.adb
blob53aefc9ecbfec4b3d222735f7ef21b9932cb3561
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-2013, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Elists; use Elists;
29 with Einfo; use Einfo;
30 with Exp_Disp; use Exp_Disp;
31 with Exp_Util; use Exp_Util;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Tss; use Exp_Tss;
34 with Errout; use Errout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Output; use Output;
41 with Restrict; use Restrict;
42 with Rident; use Rident;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Ch3; use Sem_Ch3;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Snames; use Snames;
52 with Sinfo; use Sinfo;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
57 package body Sem_Disp is
59 -----------------------
60 -- Local Subprograms --
61 -----------------------
63 procedure Add_Dispatching_Operation
64 (Tagged_Type : Entity_Id;
65 New_Op : Entity_Id);
66 -- Add New_Op in the list of primitive operations of Tagged_Type
68 function Check_Controlling_Type
69 (T : Entity_Id;
70 Subp : Entity_Id) return Entity_Id;
71 -- T is the tagged type of a formal parameter or the result of Subp.
72 -- If the subprogram has a controlling parameter or result that matches
73 -- the type, then returns the tagged type of that parameter or result
74 -- (returning the designated tagged type in the case of an access
75 -- parameter); otherwise returns empty.
77 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
78 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
79 -- type of S that has the same name of S, a type-conformant profile, an
80 -- original corresponding operation O that is a primitive of a visible
81 -- ancestor of the dispatching type of S and O is visible at the point of
82 -- of declaration of S. If the entity is found the Alias of S is set to the
83 -- original corresponding operation S and its Overridden_Operation is set
84 -- to the found entity; otherwise return Empty.
86 -- This routine does not search for non-hidden primitives since they are
87 -- covered by the normal Ada 2005 rules.
89 -------------------------------
90 -- Add_Dispatching_Operation --
91 -------------------------------
93 procedure Add_Dispatching_Operation
94 (Tagged_Type : Entity_Id;
95 New_Op : Entity_Id)
97 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
99 begin
100 -- The dispatching operation may already be on the list, if it is the
101 -- wrapper for an inherited function of a null extension (see Exp_Ch3
102 -- for the construction of function wrappers). The list of primitive
103 -- operations must not contain duplicates.
105 Append_Unique_Elmt (New_Op, List);
106 end Add_Dispatching_Operation;
108 ---------------------------
109 -- Covers_Some_Interface --
110 ---------------------------
112 function Covers_Some_Interface (Prim : Entity_Id) return Boolean is
113 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
114 Elmt : Elmt_Id;
115 E : Entity_Id;
117 begin
118 pragma Assert (Is_Dispatching_Operation (Prim));
120 -- Although this is a dispatching primitive we must check if its
121 -- dispatching type is available because it may be the primitive
122 -- of a private type not defined as tagged in its partial view.
124 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
126 -- If the tagged type is frozen then the internal entities associated
127 -- with interfaces are available in the list of primitives of the
128 -- tagged type and can be used to speed up this search.
130 if Is_Frozen (Tagged_Type) then
131 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
132 while Present (Elmt) loop
133 E := Node (Elmt);
135 if Present (Interface_Alias (E))
136 and then Alias (E) = Prim
137 then
138 return True;
139 end if;
141 Next_Elmt (Elmt);
142 end loop;
144 -- Otherwise we must collect all the interface primitives and check
145 -- if the Prim will override some interface primitive.
147 else
148 declare
149 Ifaces_List : Elist_Id;
150 Iface_Elmt : Elmt_Id;
151 Iface : Entity_Id;
152 Iface_Prim : Entity_Id;
154 begin
155 Collect_Interfaces (Tagged_Type, Ifaces_List);
156 Iface_Elmt := First_Elmt (Ifaces_List);
157 while Present (Iface_Elmt) loop
158 Iface := Node (Iface_Elmt);
160 Elmt := First_Elmt (Primitive_Operations (Iface));
161 while Present (Elmt) loop
162 Iface_Prim := Node (Elmt);
164 if Chars (Iface) = Chars (Prim)
165 and then Is_Interface_Conformant
166 (Tagged_Type, Iface_Prim, Prim)
167 then
168 return True;
169 end if;
171 Next_Elmt (Elmt);
172 end loop;
174 Next_Elmt (Iface_Elmt);
175 end loop;
176 end;
177 end if;
178 end if;
180 return False;
181 end Covers_Some_Interface;
183 -------------------------------
184 -- Check_Controlling_Formals --
185 -------------------------------
187 procedure Check_Controlling_Formals
188 (Typ : Entity_Id;
189 Subp : Entity_Id)
191 Formal : Entity_Id;
192 Ctrl_Type : Entity_Id;
194 begin
195 Formal := First_Formal (Subp);
196 while Present (Formal) loop
197 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
199 if Present (Ctrl_Type) then
201 -- When controlling type is concurrent and declared within a
202 -- generic or inside an instance use corresponding record type.
204 if Is_Concurrent_Type (Ctrl_Type)
205 and then Present (Corresponding_Record_Type (Ctrl_Type))
206 then
207 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
208 end if;
210 if Ctrl_Type = Typ then
211 Set_Is_Controlling_Formal (Formal);
213 -- Ada 2005 (AI-231): Anonymous access types that are used in
214 -- controlling parameters exclude null because it is necessary
215 -- to read the tag to dispatch, and null has no tag.
217 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
218 Set_Can_Never_Be_Null (Etype (Formal));
219 Set_Is_Known_Non_Null (Etype (Formal));
220 end if;
222 -- Check that the parameter's nominal subtype statically
223 -- matches the first subtype.
225 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
226 if not Subtypes_Statically_Match
227 (Typ, Designated_Type (Etype (Formal)))
228 then
229 Error_Msg_N
230 ("parameter subtype does not match controlling type",
231 Formal);
232 end if;
234 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
235 Error_Msg_N
236 ("parameter subtype does not match controlling type",
237 Formal);
238 end if;
240 if Present (Default_Value (Formal)) then
242 -- In Ada 2005, access parameters can have defaults
244 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
245 and then Ada_Version < Ada_2005
246 then
247 Error_Msg_N
248 ("default not allowed for controlling access parameter",
249 Default_Value (Formal));
251 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
252 Error_Msg_N
253 ("default expression must be a tag indeterminate" &
254 " function call", Default_Value (Formal));
255 end if;
256 end if;
258 elsif Comes_From_Source (Subp) then
259 Error_Msg_N
260 ("operation can be dispatching in only one type", Subp);
261 end if;
262 end if;
264 Next_Formal (Formal);
265 end loop;
267 if Ekind_In (Subp, E_Function, E_Generic_Function) then
268 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
270 if Present (Ctrl_Type) then
271 if Ctrl_Type = Typ then
272 Set_Has_Controlling_Result (Subp);
274 -- Check that result subtype statically matches first subtype
275 -- (Ada 2005): Subp may have a controlling access result.
277 if Subtypes_Statically_Match (Typ, Etype (Subp))
278 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
279 and then
280 Subtypes_Statically_Match
281 (Typ, Designated_Type (Etype (Subp))))
282 then
283 null;
285 else
286 Error_Msg_N
287 ("result subtype does not match controlling type", Subp);
288 end if;
290 elsif Comes_From_Source (Subp) then
291 Error_Msg_N
292 ("operation can be dispatching in only one type", Subp);
293 end if;
294 end if;
295 end if;
296 end Check_Controlling_Formals;
298 ----------------------------
299 -- Check_Controlling_Type --
300 ----------------------------
302 function Check_Controlling_Type
303 (T : Entity_Id;
304 Subp : Entity_Id) return Entity_Id
306 Tagged_Type : Entity_Id := Empty;
308 begin
309 if Is_Tagged_Type (T) then
310 if Is_First_Subtype (T) then
311 Tagged_Type := T;
312 else
313 Tagged_Type := Base_Type (T);
314 end if;
316 elsif Ekind (T) = E_Anonymous_Access_Type
317 and then Is_Tagged_Type (Designated_Type (T))
318 then
319 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
320 if Is_First_Subtype (Designated_Type (T)) then
321 Tagged_Type := Designated_Type (T);
322 else
323 Tagged_Type := Base_Type (Designated_Type (T));
324 end if;
326 -- Ada 2005: an incomplete type can be tagged. An operation with an
327 -- access parameter of the type is dispatching.
329 elsif Scope (Designated_Type (T)) = Current_Scope then
330 Tagged_Type := Designated_Type (T);
332 -- Ada 2005 (AI-50217)
334 elsif From_Limited_With (Designated_Type (T))
335 and then Present (Non_Limited_View (Designated_Type (T)))
336 and then Scope (Designated_Type (T)) = Scope (Subp)
337 then
338 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
339 Tagged_Type := Non_Limited_View (Designated_Type (T));
340 else
341 Tagged_Type := Base_Type (Non_Limited_View
342 (Designated_Type (T)));
343 end if;
344 end if;
345 end if;
347 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
348 return Empty;
350 -- The dispatching type and the primitive operation must be defined in
351 -- the same scope, except in the case of internal operations and formal
352 -- abstract subprograms.
354 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
355 and then (not Is_Generic_Type (Tagged_Type)
356 or else not Comes_From_Source (Subp)))
357 or else
358 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
359 or else
360 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
361 and then
362 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
363 and then
364 Is_Abstract_Subprogram (Subp))
365 then
366 return Tagged_Type;
368 else
369 return Empty;
370 end if;
371 end Check_Controlling_Type;
373 ----------------------------
374 -- Check_Dispatching_Call --
375 ----------------------------
377 procedure Check_Dispatching_Call (N : Node_Id) is
378 Loc : constant Source_Ptr := Sloc (N);
379 Actual : Node_Id;
380 Formal : Entity_Id;
381 Control : Node_Id := Empty;
382 Func : Entity_Id;
383 Subp_Entity : Entity_Id;
384 Indeterm_Ancestor_Call : Boolean := False;
385 Indeterm_Ctrl_Type : Entity_Id;
387 Static_Tag : Node_Id := Empty;
388 -- If a controlling formal has a statically tagged actual, the tag of
389 -- this actual is to be used for any tag-indeterminate actual.
391 procedure Check_Direct_Call;
392 -- In the case when the controlling actual is a class-wide type whose
393 -- root type's completion is a task or protected type, the call is in
394 -- fact direct. This routine detects the above case and modifies the
395 -- call accordingly.
397 procedure Check_Dispatching_Context;
398 -- If the call is tag-indeterminate and the entity being called is
399 -- abstract, verify that the context is a call that will eventually
400 -- provide a tag for dispatching, or has provided one already.
402 -----------------------
403 -- Check_Direct_Call --
404 -----------------------
406 procedure Check_Direct_Call is
407 Typ : Entity_Id := Etype (Control);
409 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean;
410 -- Determine whether an entity denotes a user-defined equality
412 ------------------------------
413 -- Is_User_Defined_Equality --
414 ------------------------------
416 function Is_User_Defined_Equality (Id : Entity_Id) return Boolean is
417 begin
418 return
419 Ekind (Id) = E_Function
420 and then Chars (Id) = Name_Op_Eq
421 and then Comes_From_Source (Id)
423 -- Internally generated equalities have a full type declaration
424 -- as their parent.
426 and then Nkind (Parent (Id)) = N_Function_Specification;
427 end Is_User_Defined_Equality;
429 -- Start of processing for Check_Direct_Call
431 begin
432 -- Predefined primitives do not receive wrappers since they are built
433 -- from scratch for the corresponding record of synchronized types.
434 -- Equality is in general predefined, but is excluded from the check
435 -- when it is user-defined.
437 if Is_Predefined_Dispatching_Operation (Subp_Entity)
438 and then not Is_User_Defined_Equality (Subp_Entity)
439 then
440 return;
441 end if;
443 if Is_Class_Wide_Type (Typ) then
444 Typ := Root_Type (Typ);
445 end if;
447 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
448 Typ := Full_View (Typ);
449 end if;
451 if Is_Concurrent_Type (Typ)
452 and then
453 Present (Corresponding_Record_Type (Typ))
454 then
455 Typ := Corresponding_Record_Type (Typ);
457 -- The concurrent record's list of primitives should contain a
458 -- wrapper for the entity of the call, retrieve it.
460 declare
461 Prim : Entity_Id;
462 Prim_Elmt : Elmt_Id;
463 Wrapper_Found : Boolean := False;
465 begin
466 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
467 while Present (Prim_Elmt) loop
468 Prim := Node (Prim_Elmt);
470 if Is_Primitive_Wrapper (Prim)
471 and then Wrapped_Entity (Prim) = Subp_Entity
472 then
473 Wrapper_Found := True;
474 exit;
475 end if;
477 Next_Elmt (Prim_Elmt);
478 end loop;
480 -- A primitive declared between two views should have a
481 -- corresponding wrapper.
483 pragma Assert (Wrapper_Found);
485 -- Modify the call by setting the proper entity
487 Set_Entity (Name (N), Prim);
488 end;
489 end if;
490 end Check_Direct_Call;
492 -------------------------------
493 -- Check_Dispatching_Context --
494 -------------------------------
496 procedure Check_Dispatching_Context is
497 Subp : constant Entity_Id := Entity (Name (N));
498 Typ : constant Entity_Id := Etype (Subp);
499 Par : Node_Id;
501 procedure Abstract_Context_Error;
502 -- Error for abstract call dispatching on result is not dispatching
504 ----------------------------
505 -- Abstract_Context_Error --
506 ----------------------------
508 procedure Abstract_Context_Error is
509 begin
510 if Ekind (Subp) = E_Function then
511 Error_Msg_N
512 ("call to abstract function must be dispatching", N);
514 -- This error can occur for a procedure in the case of a call to
515 -- an abstract formal procedure with a statically tagged operand.
517 else
518 Error_Msg_N
519 ("call to abstract procedure must be dispatching",
521 end if;
522 end Abstract_Context_Error;
524 -- Start of processing for Check_Dispatching_Context
526 begin
527 if Is_Abstract_Subprogram (Subp)
528 and then No (Controlling_Argument (N))
529 then
530 if Present (Alias (Subp))
531 and then not Is_Abstract_Subprogram (Alias (Subp))
532 and then No (DTC_Entity (Subp))
533 then
534 -- Private overriding of inherited abstract operation, call is
535 -- legal.
537 Set_Entity (Name (N), Alias (Subp));
538 return;
540 -- An obscure special case: a null procedure may have a class-
541 -- wide pre/postcondition that includes a call to an abstract
542 -- subp. Calls within the expression may not have been rewritten
543 -- as dispatching calls yet, because the null body appears in
544 -- the current declarative part. The expression will be properly
545 -- rewritten/reanalyzed when the postcondition procedure is built.
547 elsif In_Spec_Expression
548 and then Is_Subprogram (Current_Scope)
549 and then
550 Nkind (Parent (Current_Scope)) = N_Procedure_Specification
551 and then Null_Present (Parent (Current_Scope))
552 then
553 null;
555 else
556 -- We need to determine whether the context of the call
557 -- provides a tag to make the call dispatching. This requires
558 -- the call to be the actual in an enclosing call, and that
559 -- actual must be controlling. If the call is an operand of
560 -- equality, the other operand must not ve abstract.
562 if not Is_Tagged_Type (Typ)
563 and then not
564 (Ekind (Typ) = E_Anonymous_Access_Type
565 and then Is_Tagged_Type (Designated_Type (Typ)))
566 then
567 Abstract_Context_Error;
568 return;
569 end if;
571 Par := Parent (N);
573 if Nkind (Par) = N_Parameter_Association then
574 Par := Parent (Par);
575 end if;
577 while Present (Par) loop
578 if Nkind_In (Par, N_Function_Call,
579 N_Procedure_Call_Statement)
580 and then Is_Entity_Name (Name (Par))
581 then
582 declare
583 A : Node_Id;
584 F : Entity_Id;
586 begin
587 -- Find formal for which call is the actual.
589 F := First_Formal (Entity (Name (Par)));
590 A := First_Actual (Par);
591 while Present (F) loop
592 if Is_Controlling_Formal (F)
593 and then (N = A or else Parent (N) = A)
594 then
595 return;
596 end if;
598 Next_Formal (F);
599 Next_Actual (A);
600 end loop;
602 Error_Msg_N
603 ("call to abstract function must be dispatching", N);
604 return;
605 end;
607 -- For equalitiy operators, one of the operands must be
608 -- statically or dynamically tagged.
610 elsif Nkind_In (Par, N_Op_Eq, N_Op_Ne) then
611 if N = Right_Opnd (Par)
612 and then Is_Tag_Indeterminate (Left_Opnd (Par))
613 then
614 Abstract_Context_Error;
616 elsif N = Left_Opnd (Par)
617 and then Is_Tag_Indeterminate (Right_Opnd (Par))
618 then
619 Abstract_Context_Error;
620 end if;
622 return;
624 elsif Nkind (Par) = N_Assignment_Statement then
625 return;
627 elsif Nkind (Par) = N_Qualified_Expression
628 or else Nkind (Par) = N_Unchecked_Type_Conversion
629 then
630 Par := Parent (Par);
632 else
633 Abstract_Context_Error;
634 return;
635 end if;
636 end loop;
637 end if;
638 end if;
639 end Check_Dispatching_Context;
641 -- Start of processing for Check_Dispatching_Call
643 begin
644 -- Find a controlling argument, if any
646 if Present (Parameter_Associations (N)) then
647 Subp_Entity := Entity (Name (N));
649 Actual := First_Actual (N);
650 Formal := First_Formal (Subp_Entity);
651 while Present (Actual) loop
652 Control := Find_Controlling_Arg (Actual);
653 exit when Present (Control);
655 -- Check for the case where the actual is a tag-indeterminate call
656 -- whose result type is different than the tagged type associated
657 -- with the containing call, but is an ancestor of the type.
659 if Is_Controlling_Formal (Formal)
660 and then Is_Tag_Indeterminate (Actual)
661 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
662 and then Is_Ancestor (Etype (Actual), Etype (Formal))
663 then
664 Indeterm_Ancestor_Call := True;
665 Indeterm_Ctrl_Type := Etype (Formal);
667 -- If the formal is controlling but the actual is not, the type
668 -- of the actual is statically known, and may be used as the
669 -- controlling tag for some other tag-indeterminate actual.
671 elsif Is_Controlling_Formal (Formal)
672 and then Is_Entity_Name (Actual)
673 and then Is_Tagged_Type (Etype (Actual))
674 then
675 Static_Tag := Actual;
676 end if;
678 Next_Actual (Actual);
679 Next_Formal (Formal);
680 end loop;
682 -- If the call doesn't have a controlling actual but does have an
683 -- indeterminate actual that requires dispatching treatment, then an
684 -- object is needed that will serve as the controlling argument for
685 -- a dispatching call on the indeterminate actual. This can only
686 -- occur in the unusual situation of a default actual given by
687 -- a tag-indeterminate call and where the type of the call is an
688 -- ancestor of the type associated with a containing call to an
689 -- inherited operation (see AI-239).
691 -- Rather than create an object of the tagged type, which would
692 -- be problematic for various reasons (default initialization,
693 -- discriminants), the tag of the containing call's associated
694 -- tagged type is directly used to control the dispatching.
696 if No (Control)
697 and then Indeterm_Ancestor_Call
698 and then No (Static_Tag)
699 then
700 Control :=
701 Make_Attribute_Reference (Loc,
702 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
703 Attribute_Name => Name_Tag);
705 Analyze (Control);
706 end if;
708 if Present (Control) then
710 -- Verify that no controlling arguments are statically tagged
712 if Debug_Flag_E then
713 Write_Str ("Found Dispatching call");
714 Write_Int (Int (N));
715 Write_Eol;
716 end if;
718 Actual := First_Actual (N);
719 while Present (Actual) loop
720 if Actual /= Control then
722 if not Is_Controlling_Actual (Actual) then
723 null; -- Can be anything
725 elsif Is_Dynamically_Tagged (Actual) then
726 null; -- Valid parameter
728 elsif Is_Tag_Indeterminate (Actual) then
730 -- The tag is inherited from the enclosing call (the node
731 -- we are currently analyzing). Explicitly expand the
732 -- actual, since the previous call to Expand (from
733 -- Resolve_Call) had no way of knowing about the
734 -- required dispatching.
736 Propagate_Tag (Control, Actual);
738 else
739 Error_Msg_N
740 ("controlling argument is not dynamically tagged",
741 Actual);
742 return;
743 end if;
744 end if;
746 Next_Actual (Actual);
747 end loop;
749 -- Mark call as a dispatching call
751 Set_Controlling_Argument (N, Control);
752 Check_Restriction (No_Dispatching_Calls, N);
754 -- The dispatching call may need to be converted into a direct
755 -- call in certain cases.
757 Check_Direct_Call;
759 -- If there is a statically tagged actual and a tag-indeterminate
760 -- call to a function of the ancestor (such as that provided by a
761 -- default), then treat this as a dispatching call and propagate
762 -- the tag to the tag-indeterminate call(s).
764 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
765 Control :=
766 Make_Attribute_Reference (Loc,
767 Prefix =>
768 New_Occurrence_Of (Etype (Static_Tag), Loc),
769 Attribute_Name => Name_Tag);
771 Analyze (Control);
773 Actual := First_Actual (N);
774 Formal := First_Formal (Subp_Entity);
775 while Present (Actual) loop
776 if Is_Tag_Indeterminate (Actual)
777 and then Is_Controlling_Formal (Formal)
778 then
779 Propagate_Tag (Control, Actual);
780 end if;
782 Next_Actual (Actual);
783 Next_Formal (Formal);
784 end loop;
786 Check_Dispatching_Context;
788 else
789 -- The call is not dispatching, so check that there aren't any
790 -- tag-indeterminate abstract calls left.
792 Actual := First_Actual (N);
793 while Present (Actual) loop
794 if Is_Tag_Indeterminate (Actual) then
796 -- Function call case
798 if Nkind (Original_Node (Actual)) = N_Function_Call then
799 Func := Entity (Name (Original_Node (Actual)));
801 -- If the actual is an attribute then it can't be abstract
802 -- (the only current case of a tag-indeterminate attribute
803 -- is the stream Input attribute).
805 elsif
806 Nkind (Original_Node (Actual)) = N_Attribute_Reference
807 then
808 Func := Empty;
810 -- Only other possibility is a qualified expression whose
811 -- constituent expression is itself a call.
813 else
814 Func :=
815 Entity (Name
816 (Original_Node
817 (Expression (Original_Node (Actual)))));
818 end if;
820 if Present (Func) and then Is_Abstract_Subprogram (Func) then
821 Error_Msg_N
822 ("call to abstract function must be dispatching", N);
823 end if;
824 end if;
826 Next_Actual (Actual);
827 end loop;
829 Check_Dispatching_Context;
830 end if;
832 else
833 -- If dispatching on result, the enclosing call, if any, will
834 -- determine the controlling argument. Otherwise this is the
835 -- primitive operation of the root type.
837 Check_Dispatching_Context;
838 end if;
839 end Check_Dispatching_Call;
841 ---------------------------------
842 -- Check_Dispatching_Operation --
843 ---------------------------------
845 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
846 Tagged_Type : Entity_Id;
847 Has_Dispatching_Parent : Boolean := False;
848 Body_Is_Last_Primitive : Boolean := False;
849 Ovr_Subp : Entity_Id := Empty;
851 begin
852 if not Ekind_In (Subp, E_Procedure, E_Function) then
853 return;
854 end if;
856 Set_Is_Dispatching_Operation (Subp, False);
857 Tagged_Type := Find_Dispatching_Type (Subp);
859 -- Ada 2005 (AI-345): Use the corresponding record (if available).
860 -- Required because primitives of concurrent types are attached
861 -- to the corresponding record (not to the concurrent type).
863 if Ada_Version >= Ada_2005
864 and then Present (Tagged_Type)
865 and then Is_Concurrent_Type (Tagged_Type)
866 and then Present (Corresponding_Record_Type (Tagged_Type))
867 then
868 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
869 end if;
871 -- (AI-345): The task body procedure is not a primitive of the tagged
872 -- type
874 if Present (Tagged_Type)
875 and then Is_Concurrent_Record_Type (Tagged_Type)
876 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
877 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
878 and then Subp = Get_Task_Body_Procedure
879 (Corresponding_Concurrent_Type (Tagged_Type))
880 then
881 return;
882 end if;
884 -- If Subp is derived from a dispatching operation then it should
885 -- always be treated as dispatching. In this case various checks
886 -- below will be bypassed. Makes sure that late declarations for
887 -- inherited private subprograms are treated as dispatching, even
888 -- if the associated tagged type is already frozen.
890 Has_Dispatching_Parent :=
891 Present (Alias (Subp))
892 and then Is_Dispatching_Operation (Alias (Subp));
894 if No (Tagged_Type) then
896 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
897 -- with an abstract interface type unless the interface acts as a
898 -- parent type in a derivation. If the interface type is a formal
899 -- type then the operation is not primitive and therefore legal.
901 declare
902 E : Entity_Id;
903 Typ : Entity_Id;
905 begin
906 E := First_Entity (Subp);
907 while Present (E) loop
909 -- For an access parameter, check designated type
911 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
912 Typ := Designated_Type (Etype (E));
913 else
914 Typ := Etype (E);
915 end if;
917 if Comes_From_Source (Subp)
918 and then Is_Interface (Typ)
919 and then not Is_Class_Wide_Type (Typ)
920 and then not Is_Derived_Type (Typ)
921 and then not Is_Generic_Type (Typ)
922 and then not In_Instance
923 then
924 Error_Msg_N ("??declaration of& is too late!", Subp);
925 Error_Msg_NE -- CODEFIX??
926 ("\??spec should appear immediately after declaration "
927 & "of & !", Subp, Typ);
928 exit;
929 end if;
931 Next_Entity (E);
932 end loop;
934 -- In case of functions check also the result type
936 if Ekind (Subp) = E_Function then
937 if Is_Access_Type (Etype (Subp)) then
938 Typ := Designated_Type (Etype (Subp));
939 else
940 Typ := Etype (Subp);
941 end if;
943 -- The following should be better commented, especially since
944 -- we just added several new conditions here ???
946 if Comes_From_Source (Subp)
947 and then Is_Interface (Typ)
948 and then not Is_Class_Wide_Type (Typ)
949 and then not Is_Derived_Type (Typ)
950 and then not Is_Generic_Type (Typ)
951 and then not In_Instance
952 then
953 Error_Msg_N ("??declaration of& is too late!", Subp);
954 Error_Msg_NE
955 ("\??spec should appear immediately after declaration "
956 & "of & !", Subp, Typ);
957 end if;
958 end if;
959 end;
961 return;
963 -- The subprograms build internally after the freezing point (such as
964 -- init procs, interface thunks, type support subprograms, and Offset
965 -- to top functions for accessing interface components in variable
966 -- size tagged types) are not primitives.
968 elsif Is_Frozen (Tagged_Type)
969 and then not Comes_From_Source (Subp)
970 and then not Has_Dispatching_Parent
971 then
972 -- Complete decoration of internally built subprograms that override
973 -- a dispatching primitive. These entities correspond with the
974 -- following cases:
976 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
977 -- to override functions of nonabstract null extensions. These
978 -- primitives were added to the list of primitives of the tagged
979 -- type by Make_Controlling_Function_Wrappers. However, attribute
980 -- Is_Dispatching_Operation must be set to true.
982 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
983 -- primitives.
985 -- 3. Subprograms associated with stream attributes (built by
986 -- New_Stream_Subprogram)
988 if Present (Old_Subp)
989 and then Present (Overridden_Operation (Subp))
990 and then Is_Dispatching_Operation (Old_Subp)
991 then
992 pragma Assert
993 ((Ekind (Subp) = E_Function
994 and then Is_Dispatching_Operation (Old_Subp)
995 and then Is_Null_Extension (Base_Type (Etype (Subp))))
996 or else
997 (Ekind (Subp) = E_Procedure
998 and then Is_Dispatching_Operation (Old_Subp)
999 and then Present (Alias (Old_Subp))
1000 and then Is_Null_Interface_Primitive
1001 (Ultimate_Alias (Old_Subp)))
1002 or else Get_TSS_Name (Subp) = TSS_Stream_Read
1003 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
1005 Check_Controlling_Formals (Tagged_Type, Subp);
1006 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1007 Set_Is_Dispatching_Operation (Subp);
1008 end if;
1010 return;
1012 -- The operation may be a child unit, whose scope is the defining
1013 -- package, but which is not a primitive operation of the type.
1015 elsif Is_Child_Unit (Subp) then
1016 return;
1018 -- If the subprogram is not defined in a package spec, the only case
1019 -- where it can be a dispatching op is when it overrides an operation
1020 -- before the freezing point of the type.
1022 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
1023 or else In_Package_Body (Scope (Subp)))
1024 and then not Has_Dispatching_Parent
1025 then
1026 if not Comes_From_Source (Subp)
1027 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1028 then
1029 null;
1031 -- If the type is already frozen, the overriding is not allowed
1032 -- except when Old_Subp is not a dispatching operation (which can
1033 -- occur when Old_Subp was inherited by an untagged type). However,
1034 -- a body with no previous spec freezes the type *after* its
1035 -- declaration, and therefore is a legal overriding (unless the type
1036 -- has already been frozen). Only the first such body is legal.
1038 elsif Present (Old_Subp)
1039 and then Is_Dispatching_Operation (Old_Subp)
1040 then
1041 if Comes_From_Source (Subp)
1042 and then
1043 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1044 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1045 then
1046 declare
1047 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1048 Decl_Item : Node_Id;
1050 begin
1051 -- ??? The checks here for whether the type has been frozen
1052 -- prior to the new body are not complete. It's not simple
1053 -- to check frozenness at this point since the body has
1054 -- already caused the type to be prematurely frozen in
1055 -- Analyze_Declarations, but we're forced to recheck this
1056 -- here because of the odd rule interpretation that allows
1057 -- the overriding if the type wasn't frozen prior to the
1058 -- body. The freezing action should probably be delayed
1059 -- until after the spec is seen, but that's a tricky
1060 -- change to the delicate freezing code.
1062 -- Look at each declaration following the type up until the
1063 -- new subprogram body. If any of the declarations is a body
1064 -- then the type has been frozen already so the overriding
1065 -- primitive is illegal.
1067 Decl_Item := Next (Parent (Tagged_Type));
1068 while Present (Decl_Item)
1069 and then (Decl_Item /= Subp_Body)
1070 loop
1071 if Comes_From_Source (Decl_Item)
1072 and then (Nkind (Decl_Item) in N_Proper_Body
1073 or else Nkind (Decl_Item) in N_Body_Stub)
1074 then
1075 Error_Msg_N ("overriding of& is too late!", Subp);
1076 Error_Msg_N
1077 ("\spec should appear immediately after the type!",
1078 Subp);
1079 exit;
1080 end if;
1082 Next (Decl_Item);
1083 end loop;
1085 -- If the subprogram doesn't follow in the list of
1086 -- declarations including the type then the type has
1087 -- definitely been frozen already and the body is illegal.
1089 if No (Decl_Item) then
1090 Error_Msg_N ("overriding of& is too late!", Subp);
1091 Error_Msg_N
1092 ("\spec should appear immediately after the type!",
1093 Subp);
1095 elsif Is_Frozen (Subp) then
1097 -- The subprogram body declares a primitive operation.
1098 -- If the subprogram is already frozen, we must update
1099 -- its dispatching information explicitly here. The
1100 -- information is taken from the overridden subprogram.
1101 -- We must also generate a cross-reference entry because
1102 -- references to other primitives were already created
1103 -- when type was frozen.
1105 Body_Is_Last_Primitive := True;
1107 if Present (DTC_Entity (Old_Subp)) then
1108 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1109 Set_DT_Position (Subp, DT_Position (Old_Subp));
1111 if not Restriction_Active (No_Dispatching_Calls) then
1112 if Building_Static_DT (Tagged_Type) then
1114 -- If the static dispatch table has not been
1115 -- built then there is nothing else to do now;
1116 -- otherwise we notify that we cannot build the
1117 -- static dispatch table.
1119 if Has_Dispatch_Table (Tagged_Type) then
1120 Error_Msg_N
1121 ("overriding of& is too late for building "
1122 & " static dispatch tables!", Subp);
1123 Error_Msg_N
1124 ("\spec should appear immediately after "
1125 & "the type!", Subp);
1126 end if;
1128 -- No code required to register primitives in VM
1129 -- targets
1131 elsif VM_Target /= No_VM then
1132 null;
1134 else
1135 Insert_Actions_After (Subp_Body,
1136 Register_Primitive (Sloc (Subp_Body),
1137 Prim => Subp));
1138 end if;
1140 -- Indicate that this is an overriding operation,
1141 -- and replace the overridden entry in the list of
1142 -- primitive operations, which is used for xref
1143 -- generation subsequently.
1145 Generate_Reference (Tagged_Type, Subp, 'P', False);
1146 Override_Dispatching_Operation
1147 (Tagged_Type, Old_Subp, Subp);
1148 end if;
1149 end if;
1150 end if;
1151 end;
1153 else
1154 Error_Msg_N ("overriding of& is too late!", Subp);
1155 Error_Msg_N
1156 ("\subprogram spec should appear immediately after the type!",
1157 Subp);
1158 end if;
1160 -- If the type is not frozen yet and we are not in the overriding
1161 -- case it looks suspiciously like an attempt to define a primitive
1162 -- operation, which requires the declaration to be in a package spec
1163 -- (3.2.3(6)). Only report cases where the type and subprogram are
1164 -- in the same declaration list (by checking the enclosing parent
1165 -- declarations), to avoid spurious warnings on subprograms in
1166 -- instance bodies when the type is declared in the instance spec
1167 -- but hasn't been frozen by the instance body.
1169 elsif not Is_Frozen (Tagged_Type)
1170 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1171 then
1172 Error_Msg_N
1173 ("??not dispatching (must be defined in a package spec)", Subp);
1174 return;
1176 -- When the type is frozen, it is legitimate to define a new
1177 -- non-primitive operation.
1179 else
1180 return;
1181 end if;
1183 -- Now, we are sure that the scope is a package spec. If the subprogram
1184 -- is declared after the freezing point of the type that's an error
1186 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1187 Error_Msg_N ("this primitive operation is declared too late", Subp);
1188 Error_Msg_NE
1189 ("??no primitive operations for& after this line",
1190 Freeze_Node (Tagged_Type),
1191 Tagged_Type);
1192 return;
1193 end if;
1195 Check_Controlling_Formals (Tagged_Type, Subp);
1197 Ovr_Subp := Old_Subp;
1199 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1200 -- overridden by Subp. This only applies to source subprograms, and
1201 -- their declaration must carry an explicit overriding indicator.
1203 if No (Ovr_Subp)
1204 and then Ada_Version >= Ada_2012
1205 and then Comes_From_Source (Subp)
1206 and then
1207 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1208 then
1209 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1211 -- Verify that the proper overriding indicator has been supplied.
1213 if Present (Ovr_Subp)
1214 and then
1215 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1216 then
1217 Error_Msg_NE ("missing overriding indicator for&", Subp, Subp);
1218 end if;
1219 end if;
1221 -- Now it should be a correct primitive operation, put it in the list
1223 if Present (Ovr_Subp) then
1225 -- If the type has interfaces we complete this check after we set
1226 -- attribute Is_Dispatching_Operation.
1228 Check_Subtype_Conformant (Subp, Ovr_Subp);
1230 if Nam_In (Chars (Subp), Name_Initialize, Name_Adjust, Name_Finalize)
1231 and then Is_Controlled (Tagged_Type)
1232 and then not Is_Visibly_Controlled (Tagged_Type)
1233 then
1234 Set_Overridden_Operation (Subp, Empty);
1236 -- If the subprogram specification carries an overriding
1237 -- indicator, no need for the warning: it is either redundant,
1238 -- or else an error will be reported.
1240 if Nkind (Parent (Subp)) = N_Procedure_Specification
1241 and then
1242 (Must_Override (Parent (Subp))
1243 or else Must_Not_Override (Parent (Subp)))
1244 then
1245 null;
1247 -- Here we need the warning
1249 else
1250 Error_Msg_NE
1251 ("operation does not override inherited&??", Subp, Subp);
1252 end if;
1254 else
1255 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1257 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1258 -- that covers abstract interface subprograms we must register it
1259 -- in all the secondary dispatch tables associated with abstract
1260 -- interfaces. We do this now only if not building static tables,
1261 -- nor when the expander is inactive (we avoid trying to register
1262 -- primitives in semantics-only mode, since the type may not have
1263 -- an associated dispatch table). Otherwise the patch code is
1264 -- emitted after those tables are built, to prevent access before
1265 -- elaboration in gigi.
1267 if Body_Is_Last_Primitive and then Expander_Active then
1268 declare
1269 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1270 Elmt : Elmt_Id;
1271 Prim : Node_Id;
1273 begin
1274 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1275 while Present (Elmt) loop
1276 Prim := Node (Elmt);
1278 -- No code required to register primitives in VM targets
1280 if Present (Alias (Prim))
1281 and then Present (Interface_Alias (Prim))
1282 and then Alias (Prim) = Subp
1283 and then not Building_Static_DT (Tagged_Type)
1284 and then VM_Target = No_VM
1285 then
1286 Insert_Actions_After (Subp_Body,
1287 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1288 end if;
1290 Next_Elmt (Elmt);
1291 end loop;
1293 -- Redisplay the contents of the updated dispatch table
1295 if Debug_Flag_ZZ then
1296 Write_Str ("Late overriding: ");
1297 Write_DT (Tagged_Type);
1298 end if;
1299 end;
1300 end if;
1301 end if;
1303 -- If the tagged type is a concurrent type then we must be compiling
1304 -- with no code generation (we are either compiling a generic unit or
1305 -- compiling under -gnatc mode) because we have previously tested that
1306 -- no serious errors has been reported. In this case we do not add the
1307 -- primitive to the list of primitives of Tagged_Type but we leave the
1308 -- primitive decorated as a dispatching operation to be able to analyze
1309 -- and report errors associated with the Object.Operation notation.
1311 elsif Is_Concurrent_Type (Tagged_Type) then
1312 pragma Assert (not Expander_Active);
1313 null;
1315 -- If no old subprogram, then we add this as a dispatching operation,
1316 -- but we avoid doing this if an error was posted, to prevent annoying
1317 -- cascaded errors.
1319 elsif not Error_Posted (Subp) then
1320 Add_Dispatching_Operation (Tagged_Type, Subp);
1321 end if;
1323 Set_Is_Dispatching_Operation (Subp, True);
1325 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1326 -- subtype conformance against all the interfaces covered by this
1327 -- primitive.
1329 if Present (Ovr_Subp)
1330 and then Has_Interfaces (Tagged_Type)
1331 then
1332 declare
1333 Ifaces_List : Elist_Id;
1334 Iface_Elmt : Elmt_Id;
1335 Iface_Prim_Elmt : Elmt_Id;
1336 Iface_Prim : Entity_Id;
1337 Ret_Typ : Entity_Id;
1339 begin
1340 Collect_Interfaces (Tagged_Type, Ifaces_List);
1342 Iface_Elmt := First_Elmt (Ifaces_List);
1343 while Present (Iface_Elmt) loop
1344 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1345 Iface_Prim_Elmt :=
1346 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1347 while Present (Iface_Prim_Elmt) loop
1348 Iface_Prim := Node (Iface_Prim_Elmt);
1350 if Is_Interface_Conformant
1351 (Tagged_Type, Iface_Prim, Subp)
1352 then
1353 -- Handle procedures, functions whose return type
1354 -- matches, or functions not returning interfaces
1356 if Ekind (Subp) = E_Procedure
1357 or else Etype (Iface_Prim) = Etype (Subp)
1358 or else not Is_Interface (Etype (Iface_Prim))
1359 then
1360 Check_Subtype_Conformant
1361 (New_Id => Subp,
1362 Old_Id => Iface_Prim,
1363 Err_Loc => Subp,
1364 Skip_Controlling_Formals => True);
1366 -- Handle functions returning interfaces
1368 elsif Implements_Interface
1369 (Etype (Subp), Etype (Iface_Prim))
1370 then
1371 -- Temporarily force both entities to return the
1372 -- same type. Required because Subtype_Conformant
1373 -- does not handle this case.
1375 Ret_Typ := Etype (Iface_Prim);
1376 Set_Etype (Iface_Prim, Etype (Subp));
1378 Check_Subtype_Conformant
1379 (New_Id => Subp,
1380 Old_Id => Iface_Prim,
1381 Err_Loc => Subp,
1382 Skip_Controlling_Formals => True);
1384 Set_Etype (Iface_Prim, Ret_Typ);
1385 end if;
1386 end if;
1388 Next_Elmt (Iface_Prim_Elmt);
1389 end loop;
1390 end if;
1392 Next_Elmt (Iface_Elmt);
1393 end loop;
1394 end;
1395 end if;
1397 if not Body_Is_Last_Primitive then
1398 Set_DT_Position (Subp, No_Uint);
1400 elsif Has_Controlled_Component (Tagged_Type)
1401 and then Nam_In (Chars (Subp), Name_Initialize,
1402 Name_Adjust,
1403 Name_Finalize,
1404 Name_Finalize_Address)
1405 then
1406 declare
1407 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1408 Decl : Node_Id;
1409 Old_P : Entity_Id;
1410 Old_Bod : Node_Id;
1411 Old_Spec : Entity_Id;
1413 C_Names : constant array (1 .. 4) of Name_Id :=
1414 (Name_Initialize,
1415 Name_Adjust,
1416 Name_Finalize,
1417 Name_Finalize_Address);
1419 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1420 (TSS_Deep_Initialize,
1421 TSS_Deep_Adjust,
1422 TSS_Deep_Finalize,
1423 TSS_Finalize_Address);
1425 begin
1426 -- Remove previous controlled function which was constructed and
1427 -- analyzed when the type was frozen. This requires removing the
1428 -- body of the redefined primitive, as well as its specification
1429 -- if needed (there is no spec created for Deep_Initialize, see
1430 -- exp_ch3.adb). We must also dismantle the exception information
1431 -- that may have been generated for it when front end zero-cost
1432 -- tables are enabled.
1434 for J in D_Names'Range loop
1435 Old_P := TSS (Tagged_Type, D_Names (J));
1437 if Present (Old_P)
1438 and then Chars (Subp) = C_Names (J)
1439 then
1440 Old_Bod := Unit_Declaration_Node (Old_P);
1441 Remove (Old_Bod);
1442 Set_Is_Eliminated (Old_P);
1443 Set_Scope (Old_P, Scope (Current_Scope));
1445 if Nkind (Old_Bod) = N_Subprogram_Body
1446 and then Present (Corresponding_Spec (Old_Bod))
1447 then
1448 Old_Spec := Corresponding_Spec (Old_Bod);
1449 Set_Has_Completion (Old_Spec, False);
1450 end if;
1451 end if;
1452 end loop;
1454 Build_Late_Proc (Tagged_Type, Chars (Subp));
1456 -- The new operation is added to the actions of the freeze node
1457 -- for the type, but this node has already been analyzed, so we
1458 -- must retrieve and analyze explicitly the new body.
1460 if Present (F_Node)
1461 and then Present (Actions (F_Node))
1462 then
1463 Decl := Last (Actions (F_Node));
1464 Analyze (Decl);
1465 end if;
1466 end;
1467 end if;
1468 end Check_Dispatching_Operation;
1470 ------------------------------------------
1471 -- Check_Operation_From_Incomplete_Type --
1472 ------------------------------------------
1474 procedure Check_Operation_From_Incomplete_Type
1475 (Subp : Entity_Id;
1476 Typ : Entity_Id)
1478 Full : constant Entity_Id := Full_View (Typ);
1479 Parent_Typ : constant Entity_Id := Etype (Full);
1480 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1481 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1482 Op1, Op2 : Elmt_Id;
1483 Prev : Elmt_Id := No_Elmt;
1485 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
1486 -- Check that Subp has profile of an operation derived from Parent_Subp.
1487 -- Subp must have a parameter or result type that is Typ or an access
1488 -- parameter or access result type that designates Typ.
1490 ------------------
1491 -- Derives_From --
1492 ------------------
1494 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
1495 F1, F2 : Entity_Id;
1497 begin
1498 if Chars (Parent_Subp) /= Chars (Subp) then
1499 return False;
1500 end if;
1502 -- Check that the type of controlling formals is derived from the
1503 -- parent subprogram's controlling formal type (or designated type
1504 -- if the formal type is an anonymous access type).
1506 F1 := First_Formal (Parent_Subp);
1507 F2 := First_Formal (Subp);
1508 while Present (F1) and then Present (F2) loop
1509 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1510 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1511 return False;
1512 elsif Designated_Type (Etype (F1)) = Parent_Typ
1513 and then Designated_Type (Etype (F2)) /= Full
1514 then
1515 return False;
1516 end if;
1518 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1519 return False;
1521 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
1522 return False;
1523 end if;
1525 Next_Formal (F1);
1526 Next_Formal (F2);
1527 end loop;
1529 -- Check that a controlling result type is derived from the parent
1530 -- subprogram's result type (or designated type if the result type
1531 -- is an anonymous access type).
1533 if Ekind (Parent_Subp) = E_Function then
1534 if Ekind (Subp) /= E_Function then
1535 return False;
1537 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
1538 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
1539 return False;
1541 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
1542 and then Designated_Type (Etype (Subp)) /= Full
1543 then
1544 return False;
1545 end if;
1547 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
1548 return False;
1550 elsif Etype (Parent_Subp) = Parent_Typ
1551 and then Etype (Subp) /= Full
1552 then
1553 return False;
1554 end if;
1556 elsif Ekind (Subp) = E_Function then
1557 return False;
1558 end if;
1560 return No (F1) and then No (F2);
1561 end Derives_From;
1563 -- Start of processing for Check_Operation_From_Incomplete_Type
1565 begin
1566 -- The operation may override an inherited one, or may be a new one
1567 -- altogether. The inherited operation will have been hidden by the
1568 -- current one at the point of the type derivation, so it does not
1569 -- appear in the list of primitive operations of the type. We have to
1570 -- find the proper place of insertion in the list of primitive opera-
1571 -- tions by iterating over the list for the parent type.
1573 Op1 := First_Elmt (Old_Prim);
1574 Op2 := First_Elmt (New_Prim);
1575 while Present (Op1) and then Present (Op2) loop
1576 if Derives_From (Node (Op1)) then
1577 if No (Prev) then
1579 -- Avoid adding it to the list of primitives if already there
1581 if Node (Op2) /= Subp then
1582 Prepend_Elmt (Subp, New_Prim);
1583 end if;
1585 else
1586 Insert_Elmt_After (Subp, Prev);
1587 end if;
1589 return;
1590 end if;
1592 Prev := Op2;
1593 Next_Elmt (Op1);
1594 Next_Elmt (Op2);
1595 end loop;
1597 -- Operation is a new primitive
1599 Append_Elmt (Subp, New_Prim);
1600 end Check_Operation_From_Incomplete_Type;
1602 ---------------------------------------
1603 -- Check_Operation_From_Private_View --
1604 ---------------------------------------
1606 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1607 Tagged_Type : Entity_Id;
1609 begin
1610 if Is_Dispatching_Operation (Alias (Subp)) then
1611 Set_Scope (Subp, Current_Scope);
1612 Tagged_Type := Find_Dispatching_Type (Subp);
1614 -- Add Old_Subp to primitive operations if not already present
1616 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1617 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1619 -- If Old_Subp isn't already marked as dispatching then this is
1620 -- the case of an operation of an untagged private type fulfilled
1621 -- by a tagged type that overrides an inherited dispatching
1622 -- operation, so we set the necessary dispatching attributes here.
1624 if not Is_Dispatching_Operation (Old_Subp) then
1626 -- If the untagged type has no discriminants, and the full
1627 -- view is constrained, there will be a spurious mismatch of
1628 -- subtypes on the controlling arguments, because the tagged
1629 -- type is the internal base type introduced in the derivation.
1630 -- Use the original type to verify conformance, rather than the
1631 -- base type.
1633 if not Comes_From_Source (Tagged_Type)
1634 and then Has_Discriminants (Tagged_Type)
1635 then
1636 declare
1637 Formal : Entity_Id;
1639 begin
1640 Formal := First_Formal (Old_Subp);
1641 while Present (Formal) loop
1642 if Tagged_Type = Base_Type (Etype (Formal)) then
1643 Tagged_Type := Etype (Formal);
1644 end if;
1646 Next_Formal (Formal);
1647 end loop;
1648 end;
1650 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1651 Tagged_Type := Etype (Old_Subp);
1652 end if;
1653 end if;
1655 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1656 Set_Is_Dispatching_Operation (Old_Subp, True);
1657 Set_DT_Position (Old_Subp, No_Uint);
1658 end if;
1660 -- If the old subprogram is an explicit renaming of some other
1661 -- entity, it is not overridden by the inherited subprogram.
1662 -- Otherwise, update its alias and other attributes.
1664 if Present (Alias (Old_Subp))
1665 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
1666 N_Subprogram_Renaming_Declaration
1667 then
1668 Set_Alias (Old_Subp, Alias (Subp));
1670 -- The derived subprogram should inherit the abstractness of
1671 -- the parent subprogram (except in the case of a function
1672 -- returning the type). This sets the abstractness properly
1673 -- for cases where a private extension may have inherited an
1674 -- abstract operation, but the full type is derived from a
1675 -- descendant type and inherits a nonabstract version.
1677 if Etype (Subp) /= Tagged_Type then
1678 Set_Is_Abstract_Subprogram
1679 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1680 end if;
1681 end if;
1682 end if;
1683 end if;
1684 end Check_Operation_From_Private_View;
1686 --------------------------
1687 -- Find_Controlling_Arg --
1688 --------------------------
1690 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1691 Orig_Node : constant Node_Id := Original_Node (N);
1692 Typ : Entity_Id;
1694 begin
1695 if Nkind (Orig_Node) = N_Qualified_Expression then
1696 return Find_Controlling_Arg (Expression (Orig_Node));
1697 end if;
1699 -- Dispatching on result case. If expansion is disabled, the node still
1700 -- has the structure of a function call. However, if the function name
1701 -- is an operator and the call was given in infix form, the original
1702 -- node has no controlling result and we must examine the current node.
1704 if Nkind (N) = N_Function_Call
1705 and then Present (Controlling_Argument (N))
1706 and then Has_Controlling_Result (Entity (Name (N)))
1707 then
1708 return Controlling_Argument (N);
1710 -- If expansion is enabled, the call may have been transformed into
1711 -- an indirect call, and we need to recover the original node.
1713 elsif Nkind (Orig_Node) = N_Function_Call
1714 and then Present (Controlling_Argument (Orig_Node))
1715 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1716 then
1717 return Controlling_Argument (Orig_Node);
1719 -- Type conversions are dynamically tagged if the target type, or its
1720 -- designated type, are classwide. An interface conversion expands into
1721 -- a dereference, so test must be performed on the original node.
1723 elsif Nkind (Orig_Node) = N_Type_Conversion
1724 and then Nkind (N) = N_Explicit_Dereference
1725 and then Is_Controlling_Actual (N)
1726 then
1727 declare
1728 Target_Type : constant Entity_Id :=
1729 Entity (Subtype_Mark (Orig_Node));
1731 begin
1732 if Is_Class_Wide_Type (Target_Type) then
1733 return N;
1735 elsif Is_Access_Type (Target_Type)
1736 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
1737 then
1738 return N;
1740 else
1741 return Empty;
1742 end if;
1743 end;
1745 -- Normal case
1747 elsif Is_Controlling_Actual (N)
1748 or else
1749 (Nkind (Parent (N)) = N_Qualified_Expression
1750 and then Is_Controlling_Actual (Parent (N)))
1751 then
1752 Typ := Etype (N);
1754 if Is_Access_Type (Typ) then
1756 -- In the case of an Access attribute, use the type of the prefix,
1757 -- since in the case of an actual for an access parameter, the
1758 -- attribute's type may be of a specific designated type, even
1759 -- though the prefix type is class-wide.
1761 if Nkind (N) = N_Attribute_Reference then
1762 Typ := Etype (Prefix (N));
1764 -- An allocator is dispatching if the type of qualified expression
1765 -- is class_wide, in which case this is the controlling type.
1767 elsif Nkind (Orig_Node) = N_Allocator
1768 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1769 then
1770 Typ := Etype (Expression (Orig_Node));
1771 else
1772 Typ := Designated_Type (Typ);
1773 end if;
1774 end if;
1776 if Is_Class_Wide_Type (Typ)
1777 or else
1778 (Nkind (Parent (N)) = N_Qualified_Expression
1779 and then Is_Access_Type (Etype (N))
1780 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1781 then
1782 return N;
1783 end if;
1784 end if;
1786 return Empty;
1787 end Find_Controlling_Arg;
1789 ---------------------------
1790 -- Find_Dispatching_Type --
1791 ---------------------------
1793 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1794 A_Formal : Entity_Id;
1795 Formal : Entity_Id;
1796 Ctrl_Type : Entity_Id;
1798 begin
1799 if Ekind_In (Subp, E_Function, E_Procedure)
1800 and then Present (DTC_Entity (Subp))
1801 then
1802 return Scope (DTC_Entity (Subp));
1804 -- For subprograms internally generated by derivations of tagged types
1805 -- use the alias subprogram as a reference to locate the dispatching
1806 -- type of Subp.
1808 elsif not Comes_From_Source (Subp)
1809 and then Present (Alias (Subp))
1810 and then Is_Dispatching_Operation (Alias (Subp))
1811 then
1812 if Ekind (Alias (Subp)) = E_Function
1813 and then Has_Controlling_Result (Alias (Subp))
1814 then
1815 return Check_Controlling_Type (Etype (Subp), Subp);
1817 else
1818 Formal := First_Formal (Subp);
1819 A_Formal := First_Formal (Alias (Subp));
1820 while Present (A_Formal) loop
1821 if Is_Controlling_Formal (A_Formal) then
1822 return Check_Controlling_Type (Etype (Formal), Subp);
1823 end if;
1825 Next_Formal (Formal);
1826 Next_Formal (A_Formal);
1827 end loop;
1829 pragma Assert (False);
1830 return Empty;
1831 end if;
1833 -- General case
1835 else
1836 Formal := First_Formal (Subp);
1837 while Present (Formal) loop
1838 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1840 if Present (Ctrl_Type) then
1841 return Ctrl_Type;
1842 end if;
1844 Next_Formal (Formal);
1845 end loop;
1847 -- The subprogram may also be dispatching on result
1849 if Present (Etype (Subp)) then
1850 return Check_Controlling_Type (Etype (Subp), Subp);
1851 end if;
1852 end if;
1854 pragma Assert (not Is_Dispatching_Operation (Subp));
1855 return Empty;
1856 end Find_Dispatching_Type;
1858 --------------------------------------
1859 -- Find_Hidden_Overridden_Primitive --
1860 --------------------------------------
1862 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
1864 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
1865 Elmt : Elmt_Id;
1866 Orig_Prim : Entity_Id;
1867 Prim : Entity_Id;
1868 Vis_List : Elist_Id;
1870 begin
1871 -- This Ada 2012 rule applies only for type extensions or private
1872 -- extensions, where the parent type is not in a parent unit, and
1873 -- where an operation is never declared but still inherited.
1875 if No (Tag_Typ)
1876 or else not Is_Record_Type (Tag_Typ)
1877 or else Etype (Tag_Typ) = Tag_Typ
1878 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
1879 then
1880 return Empty;
1881 end if;
1883 -- Collect the list of visible ancestor of the tagged type
1885 Vis_List := Visible_Ancestors (Tag_Typ);
1887 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
1888 while Present (Elmt) loop
1889 Prim := Node (Elmt);
1891 -- Find an inherited hidden dispatching primitive with the name of S
1892 -- and a type-conformant profile.
1894 if Present (Alias (Prim))
1895 and then Is_Hidden (Alias (Prim))
1896 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
1897 and then Primitive_Names_Match (S, Prim)
1898 and then Type_Conformant (S, Prim)
1899 then
1900 declare
1901 Vis_Ancestor : Elmt_Id;
1902 Elmt : Elmt_Id;
1904 begin
1905 -- The original corresponding operation of Prim must be an
1906 -- operation of a visible ancestor of the dispatching type S,
1907 -- and the original corresponding operation of S2 must be
1908 -- visible.
1910 Orig_Prim := Original_Corresponding_Operation (Prim);
1912 if Orig_Prim /= Prim
1913 and then Is_Immediately_Visible (Orig_Prim)
1914 then
1915 Vis_Ancestor := First_Elmt (Vis_List);
1916 while Present (Vis_Ancestor) loop
1917 Elmt :=
1918 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
1919 while Present (Elmt) loop
1920 if Node (Elmt) = Orig_Prim then
1921 Set_Overridden_Operation (S, Prim);
1922 Set_Alias (Prim, Orig_Prim);
1923 return Prim;
1924 end if;
1926 Next_Elmt (Elmt);
1927 end loop;
1929 Next_Elmt (Vis_Ancestor);
1930 end loop;
1931 end if;
1932 end;
1933 end if;
1935 Next_Elmt (Elmt);
1936 end loop;
1938 return Empty;
1939 end Find_Hidden_Overridden_Primitive;
1941 ---------------------------------------
1942 -- Find_Primitive_Covering_Interface --
1943 ---------------------------------------
1945 function Find_Primitive_Covering_Interface
1946 (Tagged_Type : Entity_Id;
1947 Iface_Prim : Entity_Id) return Entity_Id
1949 E : Entity_Id;
1950 El : Elmt_Id;
1952 begin
1953 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1954 or else (Present (Alias (Iface_Prim))
1955 and then
1956 Is_Interface
1957 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1959 -- Search in the homonym chain. Done to speed up locating visible
1960 -- entities and required to catch primitives associated with the partial
1961 -- view of private types when processing the corresponding full view.
1963 E := Current_Entity (Iface_Prim);
1964 while Present (E) loop
1965 if Is_Subprogram (E)
1966 and then Is_Dispatching_Operation (E)
1967 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1968 then
1969 return E;
1970 end if;
1972 E := Homonym (E);
1973 end loop;
1975 -- Search in the list of primitives of the type. Required to locate
1976 -- the covering primitive if the covering primitive is not visible
1977 -- (for example, non-visible inherited primitive of private type).
1979 El := First_Elmt (Primitive_Operations (Tagged_Type));
1980 while Present (El) loop
1981 E := Node (El);
1983 -- Keep separate the management of internal entities that link
1984 -- primitives with interface primitives from tagged type primitives.
1986 if No (Interface_Alias (E)) then
1987 if Present (Alias (E)) then
1989 -- This interface primitive has not been covered yet
1991 if Alias (E) = Iface_Prim then
1992 return E;
1994 -- The covering primitive was inherited
1996 elsif Overridden_Operation (Ultimate_Alias (E))
1997 = Iface_Prim
1998 then
1999 return E;
2000 end if;
2001 end if;
2003 -- Check if E covers the interface primitive (includes case in
2004 -- which E is an inherited private primitive).
2006 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2007 return E;
2008 end if;
2010 -- Use the internal entity that links the interface primitive with
2011 -- the covering primitive to locate the entity.
2013 elsif Interface_Alias (E) = Iface_Prim then
2014 return Alias (E);
2015 end if;
2017 Next_Elmt (El);
2018 end loop;
2020 -- Not found
2022 return Empty;
2023 end Find_Primitive_Covering_Interface;
2025 ---------------------------
2026 -- Inherited_Subprograms --
2027 ---------------------------
2029 function Inherited_Subprograms (S : Entity_Id) return Subprogram_List is
2030 Result : Subprogram_List (1 .. 6000);
2031 -- 6000 here is intended to be infinity. We could use an expandable
2032 -- table, but it would be awfully heavy, and there is no way that we
2033 -- could reasonably exceed this value.
2035 N : Int := 0;
2036 -- Number of entries in Result
2038 Parent_Op : Entity_Id;
2039 -- Traverses the Overridden_Operation chain
2041 procedure Store_IS (E : Entity_Id);
2042 -- Stores E in Result if not already stored
2044 --------------
2045 -- Store_IS --
2046 --------------
2048 procedure Store_IS (E : Entity_Id) is
2049 begin
2050 for J in 1 .. N loop
2051 if E = Result (J) then
2052 return;
2053 end if;
2054 end loop;
2056 N := N + 1;
2057 Result (N) := E;
2058 end Store_IS;
2060 -- Start of processing for Inherited_Subprograms
2062 begin
2063 if Present (S) and then Is_Dispatching_Operation (S) then
2065 -- Deal with direct inheritance
2067 Parent_Op := S;
2068 loop
2069 Parent_Op := Overridden_Operation (Parent_Op);
2070 exit when No (Parent_Op);
2072 if Is_Subprogram (Parent_Op)
2073 or else Is_Generic_Subprogram (Parent_Op)
2074 then
2075 Store_IS (Parent_Op);
2076 end if;
2077 end loop;
2079 -- Now deal with interfaces
2081 declare
2082 Tag_Typ : Entity_Id;
2083 Prim : Entity_Id;
2084 Elmt : Elmt_Id;
2086 begin
2087 Tag_Typ := Find_Dispatching_Type (S);
2089 if Is_Concurrent_Type (Tag_Typ) then
2090 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2091 end if;
2093 -- Search primitive operations of dispatching type
2095 if Present (Tag_Typ)
2096 and then Present (Primitive_Operations (Tag_Typ))
2097 then
2098 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2099 while Present (Elmt) loop
2100 Prim := Node (Elmt);
2102 -- The following test eliminates some odd cases in which
2103 -- Ekind (Prim) is Void, to be investigated further ???
2105 if not (Is_Subprogram (Prim)
2106 or else
2107 Is_Generic_Subprogram (Prim))
2108 then
2109 null;
2111 -- For [generic] subprogram, look at interface alias
2113 elsif Present (Interface_Alias (Prim))
2114 and then Alias (Prim) = S
2115 then
2116 -- We have found a primitive covered by S
2118 Store_IS (Interface_Alias (Prim));
2119 end if;
2121 Next_Elmt (Elmt);
2122 end loop;
2123 end if;
2124 end;
2125 end if;
2127 return Result (1 .. N);
2128 end Inherited_Subprograms;
2130 ---------------------------
2131 -- Is_Dynamically_Tagged --
2132 ---------------------------
2134 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2135 begin
2136 if Nkind (N) = N_Error then
2137 return False;
2138 else
2139 return Find_Controlling_Arg (N) /= Empty;
2140 end if;
2141 end Is_Dynamically_Tagged;
2143 ---------------------------------
2144 -- Is_Null_Interface_Primitive --
2145 ---------------------------------
2147 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2148 begin
2149 return Comes_From_Source (E)
2150 and then Is_Dispatching_Operation (E)
2151 and then Ekind (E) = E_Procedure
2152 and then Null_Present (Parent (E))
2153 and then Is_Interface (Find_Dispatching_Type (E));
2154 end Is_Null_Interface_Primitive;
2156 --------------------------
2157 -- Is_Tag_Indeterminate --
2158 --------------------------
2160 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
2161 Nam : Entity_Id;
2162 Actual : Node_Id;
2163 Orig_Node : constant Node_Id := Original_Node (N);
2165 begin
2166 if Nkind (Orig_Node) = N_Function_Call
2167 and then Is_Entity_Name (Name (Orig_Node))
2168 then
2169 Nam := Entity (Name (Orig_Node));
2171 if not Has_Controlling_Result (Nam) then
2172 return False;
2174 -- The function may have a controlling result, but if the return type
2175 -- is not visibly tagged, then this is not tag-indeterminate.
2177 elsif Is_Access_Type (Etype (Nam))
2178 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
2179 then
2180 return False;
2182 -- An explicit dereference means that the call has already been
2183 -- expanded and there is no tag to propagate.
2185 elsif Nkind (N) = N_Explicit_Dereference then
2186 return False;
2188 -- If there are no actuals, the call is tag-indeterminate
2190 elsif No (Parameter_Associations (Orig_Node)) then
2191 return True;
2193 else
2194 Actual := First_Actual (Orig_Node);
2195 while Present (Actual) loop
2196 if Is_Controlling_Actual (Actual)
2197 and then not Is_Tag_Indeterminate (Actual)
2198 then
2199 -- One operand is dispatching
2201 return False;
2202 end if;
2204 Next_Actual (Actual);
2205 end loop;
2207 return True;
2208 end if;
2210 elsif Nkind (Orig_Node) = N_Qualified_Expression then
2211 return Is_Tag_Indeterminate (Expression (Orig_Node));
2213 -- Case of a call to the Input attribute (possibly rewritten), which is
2214 -- always tag-indeterminate except when its prefix is a Class attribute.
2216 elsif Nkind (Orig_Node) = N_Attribute_Reference
2217 and then
2218 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
2219 and then
2220 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
2221 then
2222 return True;
2224 -- In Ada 2005, a function that returns an anonymous access type can be
2225 -- dispatching, and the dereference of a call to such a function can
2226 -- also be tag-indeterminate if the call itself is.
2228 elsif Nkind (Orig_Node) = N_Explicit_Dereference
2229 and then Ada_Version >= Ada_2005
2230 then
2231 return Is_Tag_Indeterminate (Prefix (Orig_Node));
2233 else
2234 return False;
2235 end if;
2236 end Is_Tag_Indeterminate;
2238 ------------------------------------
2239 -- Override_Dispatching_Operation --
2240 ------------------------------------
2242 procedure Override_Dispatching_Operation
2243 (Tagged_Type : Entity_Id;
2244 Prev_Op : Entity_Id;
2245 New_Op : Entity_Id;
2246 Is_Wrapper : Boolean := False)
2248 Elmt : Elmt_Id;
2249 Prim : Node_Id;
2251 begin
2252 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
2253 -- we do it unconditionally in Ada 95 now, since this is our pragma).
2255 if No_Return (Prev_Op) and then not No_Return (New_Op) then
2256 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
2257 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
2258 end if;
2260 -- If there is no previous operation to override, the type declaration
2261 -- was malformed, and an error must have been emitted already.
2263 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2264 while Present (Elmt)
2265 and then Node (Elmt) /= Prev_Op
2266 loop
2267 Next_Elmt (Elmt);
2268 end loop;
2270 if No (Elmt) then
2271 return;
2272 end if;
2274 -- The location of entities that come from source in the list of
2275 -- primitives of the tagged type must follow their order of occurrence
2276 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
2277 -- primitive of an interface that is not implemented by the parents of
2278 -- this tagged type (that is, it is an alias of an interface primitive
2279 -- generated by Derive_Interface_Progenitors), then we must append the
2280 -- new entity at the end of the list of primitives.
2282 if Present (Alias (Prev_Op))
2283 and then Etype (Tagged_Type) /= Tagged_Type
2284 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
2285 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
2286 Tagged_Type, Use_Full_View => True)
2287 and then not Implements_Interface
2288 (Etype (Tagged_Type),
2289 Find_Dispatching_Type (Alias (Prev_Op)))
2290 then
2291 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
2292 Append_Elmt (New_Op, Primitive_Operations (Tagged_Type));
2294 -- The new primitive replaces the overridden entity. Required to ensure
2295 -- that overriding primitive is assigned the same dispatch table slot.
2297 else
2298 Replace_Elmt (Elmt, New_Op);
2299 end if;
2301 if Ada_Version >= Ada_2005
2302 and then Has_Interfaces (Tagged_Type)
2303 then
2304 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
2305 -- entities of the overridden primitive to reference New_Op, and
2306 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
2307 -- that the new operation is subtype conformant with the interface
2308 -- operations that it implements (for operations inherited from the
2309 -- parent itself, this check is made when building the derived type).
2311 -- Note: This code is executed with internally generated wrappers of
2312 -- functions with controlling result and late overridings.
2314 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
2315 while Present (Elmt) loop
2316 Prim := Node (Elmt);
2318 if Prim = New_Op then
2319 null;
2321 -- Note: The check on Is_Subprogram protects the frontend against
2322 -- reading attributes in entities that are not yet fully decorated
2324 elsif Is_Subprogram (Prim)
2325 and then Present (Interface_Alias (Prim))
2326 and then Alias (Prim) = Prev_Op
2327 then
2328 Set_Alias (Prim, New_Op);
2330 -- No further decoration needed yet for internally generated
2331 -- wrappers of controlling functions since (at this stage)
2332 -- they are not yet decorated.
2334 if not Is_Wrapper then
2335 Check_Subtype_Conformant (New_Op, Prim);
2337 Set_Is_Abstract_Subprogram (Prim,
2338 Is_Abstract_Subprogram (New_Op));
2340 -- Ensure that this entity will be expanded to fill the
2341 -- corresponding entry in its dispatch table.
2343 if not Is_Abstract_Subprogram (Prim) then
2344 Set_Has_Delayed_Freeze (Prim);
2345 end if;
2346 end if;
2347 end if;
2349 Next_Elmt (Elmt);
2350 end loop;
2351 end if;
2353 if (not Is_Package_Or_Generic_Package (Current_Scope))
2354 or else not In_Private_Part (Current_Scope)
2355 then
2356 -- Not a private primitive
2358 null;
2360 else pragma Assert (Is_Inherited_Operation (Prev_Op));
2362 -- Make the overriding operation into an alias of the implicit one.
2363 -- In this fashion a call from outside ends up calling the new body
2364 -- even if non-dispatching, and a call from inside calls the over-
2365 -- riding operation because it hides the implicit one. To indicate
2366 -- that the body of Prev_Op is never called, set its dispatch table
2367 -- entity to Empty. If the overridden operation has a dispatching
2368 -- result, so does the overriding one.
2370 Set_Alias (Prev_Op, New_Op);
2371 Set_DTC_Entity (Prev_Op, Empty);
2372 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
2373 return;
2374 end if;
2375 end Override_Dispatching_Operation;
2377 -------------------
2378 -- Propagate_Tag --
2379 -------------------
2381 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
2382 Call_Node : Node_Id;
2383 Arg : Node_Id;
2385 begin
2386 if Nkind (Actual) = N_Function_Call then
2387 Call_Node := Actual;
2389 elsif Nkind (Actual) = N_Identifier
2390 and then Nkind (Original_Node (Actual)) = N_Function_Call
2391 then
2392 -- Call rewritten as object declaration when stack-checking is
2393 -- enabled. Propagate tag to expression in declaration, which is
2394 -- original call.
2396 Call_Node := Expression (Parent (Entity (Actual)));
2398 -- Ada 2005: If this is a dereference of a call to a function with a
2399 -- dispatching access-result, the tag is propagated when the dereference
2400 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
2402 elsif Nkind (Actual) = N_Explicit_Dereference
2403 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
2404 then
2405 return;
2407 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
2408 -- and in that case we can simply return.
2410 elsif Nkind (Actual) = N_Attribute_Reference then
2411 pragma Assert (Attribute_Name (Actual) = Name_Input);
2413 return;
2415 -- Only other possibilities are parenthesized or qualified expression,
2416 -- or an expander-generated unchecked conversion of a function call to
2417 -- a stream Input attribute.
2419 else
2420 Call_Node := Expression (Actual);
2421 end if;
2423 -- No action needed if the call has been already expanded
2425 if Is_Expanded_Dispatching_Call (Call_Node) then
2426 return;
2427 end if;
2429 -- Do not set the Controlling_Argument if already set. This happens in
2430 -- the special case of _Input (see Exp_Attr, case Input).
2432 if No (Controlling_Argument (Call_Node)) then
2433 Set_Controlling_Argument (Call_Node, Control);
2434 end if;
2436 Arg := First_Actual (Call_Node);
2437 while Present (Arg) loop
2438 if Is_Tag_Indeterminate (Arg) then
2439 Propagate_Tag (Control, Arg);
2440 end if;
2442 Next_Actual (Arg);
2443 end loop;
2445 -- Expansion of dispatching calls is suppressed when VM_Target, because
2446 -- the VM back-ends directly handle the generation of dispatching calls
2447 -- and would have to undo any expansion to an indirect call.
2449 if Tagged_Type_Expansion then
2450 declare
2451 Call_Typ : constant Entity_Id := Etype (Call_Node);
2453 begin
2454 Expand_Dispatching_Call (Call_Node);
2456 -- If the controlling argument is an interface type and the type
2457 -- of Call_Node differs then we must add an implicit conversion to
2458 -- force displacement of the pointer to the object to reference
2459 -- the secondary dispatch table of the interface.
2461 if Is_Interface (Etype (Control))
2462 and then Etype (Control) /= Call_Typ
2463 then
2464 -- Cannot use Convert_To because the previous call to
2465 -- Expand_Dispatching_Call leaves decorated the Call_Node
2466 -- with the type of Control.
2468 Rewrite (Call_Node,
2469 Make_Type_Conversion (Sloc (Call_Node),
2470 Subtype_Mark =>
2471 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
2472 Expression => Relocate_Node (Call_Node)));
2473 Set_Etype (Call_Node, Etype (Control));
2474 Set_Analyzed (Call_Node);
2476 Expand_Interface_Conversion (Call_Node);
2477 end if;
2478 end;
2480 -- Expansion of a dispatching call results in an indirect call, which in
2481 -- turn causes current values to be killed (see Resolve_Call), so on VM
2482 -- targets we do the call here to ensure consistent warnings between VM
2483 -- and non-VM targets.
2485 else
2486 Kill_Current_Values;
2487 end if;
2488 end Propagate_Tag;
2490 end Sem_Disp;