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