9a0f878aa8a433c7833f38a116adb5876345740b
[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-2009, 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 Stand; use Stand;
52 with Sinfo; use Sinfo;
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 -------------------------------
77 -- Add_Dispatching_Operation --
78 -------------------------------
79
80 procedure Add_Dispatching_Operation
81 (Tagged_Type : Entity_Id;
82 New_Op : Entity_Id)
83 is
84 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
85
86 begin
87 -- The dispatching operation may already be on the list, if it is the
88 -- wrapper for an inherited function of a null extension (see Exp_Ch3
89 -- for the construction of function wrappers). The list of primitive
90 -- operations must not contain duplicates.
91
92 Append_Unique_Elmt (New_Op, List);
93 end Add_Dispatching_Operation;
94
95 -------------------------------
96 -- Check_Controlling_Formals --
97 -------------------------------
98
99 procedure Check_Controlling_Formals
100 (Typ : Entity_Id;
101 Subp : Entity_Id)
102 is
103 Formal : Entity_Id;
104 Ctrl_Type : Entity_Id;
105
106 begin
107 Formal := First_Formal (Subp);
108
109 while Present (Formal) loop
110 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
111
112 if Present (Ctrl_Type) then
113
114 -- When the controlling type is concurrent and declared within a
115 -- generic or inside an instance, use its corresponding record
116 -- type.
117
118 if Is_Concurrent_Type (Ctrl_Type)
119 and then Present (Corresponding_Record_Type (Ctrl_Type))
120 then
121 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
122 end if;
123
124 if Ctrl_Type = Typ then
125 Set_Is_Controlling_Formal (Formal);
126
127 -- Ada 2005 (AI-231): Anonymous access types used in
128 -- controlling parameters exclude null because it is necessary
129 -- to read the tag to dispatch, and null has no tag.
130
131 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
132 Set_Can_Never_Be_Null (Etype (Formal));
133 Set_Is_Known_Non_Null (Etype (Formal));
134 end if;
135
136 -- Check that the parameter's nominal subtype statically
137 -- matches the first subtype.
138
139 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
140 if not Subtypes_Statically_Match
141 (Typ, Designated_Type (Etype (Formal)))
142 then
143 Error_Msg_N
144 ("parameter subtype does not match controlling type",
145 Formal);
146 end if;
147
148 elsif not Subtypes_Statically_Match (Typ, Etype (Formal)) then
149 Error_Msg_N
150 ("parameter subtype does not match controlling type",
151 Formal);
152 end if;
153
154 if Present (Default_Value (Formal)) then
155
156 -- In Ada 2005, access parameters can have defaults
157
158 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
159 and then Ada_Version < Ada_05
160 then
161 Error_Msg_N
162 ("default not allowed for controlling access parameter",
163 Default_Value (Formal));
164
165 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
166 Error_Msg_N
167 ("default expression must be a tag indeterminate" &
168 " function call", Default_Value (Formal));
169 end if;
170 end if;
171
172 elsif Comes_From_Source (Subp) then
173 Error_Msg_N
174 ("operation can be dispatching in only one type", Subp);
175 end if;
176 end if;
177
178 Next_Formal (Formal);
179 end loop;
180
181 if Present (Etype (Subp)) then
182 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
183
184 if Present (Ctrl_Type) then
185 if Ctrl_Type = Typ then
186 Set_Has_Controlling_Result (Subp);
187
188 -- Check that result subtype statically matches first subtype
189 -- (Ada 2005): Subp may have a controlling access result.
190
191 if Subtypes_Statically_Match (Typ, Etype (Subp))
192 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
193 and then
194 Subtypes_Statically_Match
195 (Typ, Designated_Type (Etype (Subp))))
196 then
197 null;
198
199 else
200 Error_Msg_N
201 ("result subtype does not match controlling type", Subp);
202 end if;
203
204 elsif Comes_From_Source (Subp) then
205 Error_Msg_N
206 ("operation can be dispatching in only one type", Subp);
207 end if;
208 end if;
209 end if;
210 end Check_Controlling_Formals;
211
212 ----------------------------
213 -- Check_Controlling_Type --
214 ----------------------------
215
216 function Check_Controlling_Type
217 (T : Entity_Id;
218 Subp : Entity_Id) return Entity_Id
219 is
220 Tagged_Type : Entity_Id := Empty;
221
222 begin
223 if Is_Tagged_Type (T) then
224 if Is_First_Subtype (T) then
225 Tagged_Type := T;
226 else
227 Tagged_Type := Base_Type (T);
228 end if;
229
230 elsif Ekind (T) = E_Anonymous_Access_Type
231 and then Is_Tagged_Type (Designated_Type (T))
232 then
233 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
234 if Is_First_Subtype (Designated_Type (T)) then
235 Tagged_Type := Designated_Type (T);
236 else
237 Tagged_Type := Base_Type (Designated_Type (T));
238 end if;
239
240 -- Ada 2005: an incomplete type can be tagged. An operation with an
241 -- access parameter of the type is dispatching.
242
243 elsif Scope (Designated_Type (T)) = Current_Scope then
244 Tagged_Type := Designated_Type (T);
245
246 -- Ada 2005 (AI-50217)
247
248 elsif From_With_Type (Designated_Type (T))
249 and then Present (Non_Limited_View (Designated_Type (T)))
250 then
251 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
252 Tagged_Type := Non_Limited_View (Designated_Type (T));
253 else
254 Tagged_Type := Base_Type (Non_Limited_View
255 (Designated_Type (T)));
256 end if;
257 end if;
258 end if;
259
260 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
261 return Empty;
262
263 -- The dispatching type and the primitive operation must be defined in
264 -- the same scope, except in the case of internal operations and formal
265 -- abstract subprograms.
266
267 elsif ((Scope (Subp) = Scope (Tagged_Type) or else Is_Internal (Subp))
268 and then (not Is_Generic_Type (Tagged_Type)
269 or else not Comes_From_Source (Subp)))
270 or else
271 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
272 or else
273 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
274 and then
275 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
276 and then
277 Is_Abstract_Subprogram (Subp))
278 then
279 return Tagged_Type;
280
281 else
282 return Empty;
283 end if;
284 end Check_Controlling_Type;
285
286 ----------------------------
287 -- Check_Dispatching_Call --
288 ----------------------------
289
290 procedure Check_Dispatching_Call (N : Node_Id) is
291 Loc : constant Source_Ptr := Sloc (N);
292 Actual : Node_Id;
293 Formal : Entity_Id;
294 Control : Node_Id := Empty;
295 Func : Entity_Id;
296 Subp_Entity : Entity_Id;
297 Indeterm_Ancestor_Call : Boolean := False;
298 Indeterm_Ctrl_Type : Entity_Id;
299
300 Static_Tag : Node_Id := Empty;
301 -- If a controlling formal has a statically tagged actual, the tag of
302 -- this actual is to be used for any tag-indeterminate actual.
303
304 procedure Check_Direct_Call;
305 -- In the case when the controlling actual is a class-wide type whose
306 -- root type's completion is a task or protected type, the call is in
307 -- fact direct. This routine detects the above case and modifies the
308 -- call accordingly.
309
310 procedure Check_Dispatching_Context;
311 -- If the call is tag-indeterminate and the entity being called is
312 -- abstract, verify that the context is a call that will eventually
313 -- provide a tag for dispatching, or has provided one already.
314
315 -----------------------
316 -- Check_Direct_Call --
317 -----------------------
318
319 procedure Check_Direct_Call is
320 Typ : Entity_Id := Etype (Control);
321
322 begin
323 if Is_Class_Wide_Type (Typ) then
324 Typ := Root_Type (Typ);
325 end if;
326
327 -- Detect whether the controlling type is a private type completed
328 -- by a task or protected type.
329
330 if Is_Private_Type (Typ)
331 and then Present (Full_View (Typ))
332 and then Is_Concurrent_Type (Full_View (Typ))
333 and then Present (Corresponding_Record_Type (Full_View (Typ)))
334 then
335 Typ := Corresponding_Record_Type (Full_View (Typ));
336
337 -- The concurrent record's list of primitives should contain a
338 -- wrapper for the entity of the call, retrieve it.
339
340 declare
341 Prim : Entity_Id;
342 Prim_Elmt : Elmt_Id;
343 Wrapper_Found : Boolean := False;
344
345 begin
346 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
347 while Present (Prim_Elmt) loop
348 Prim := Node (Prim_Elmt);
349
350 if Is_Primitive_Wrapper (Prim)
351 and then Wrapped_Entity (Prim) = Subp_Entity
352 then
353 Wrapper_Found := True;
354 exit;
355 end if;
356
357 Next_Elmt (Prim_Elmt);
358 end loop;
359
360 -- A primitive declared between two views should have a
361 -- corresponding wrapper.
362
363 pragma Assert (Wrapper_Found);
364
365 -- Modify the call by setting the proper entity
366
367 Set_Entity (Name (N), Prim);
368 end;
369 end if;
370 end Check_Direct_Call;
371
372 -------------------------------
373 -- Check_Dispatching_Context --
374 -------------------------------
375
376 procedure Check_Dispatching_Context is
377 Subp : constant Entity_Id := Entity (Name (N));
378 Par : Node_Id;
379
380 begin
381 if Is_Abstract_Subprogram (Subp)
382 and then No (Controlling_Argument (N))
383 then
384 if Present (Alias (Subp))
385 and then not Is_Abstract_Subprogram (Alias (Subp))
386 and then No (DTC_Entity (Subp))
387 then
388 -- Private overriding of inherited abstract operation, call is
389 -- legal.
390
391 Set_Entity (Name (N), Alias (Subp));
392 return;
393
394 else
395 Par := Parent (N);
396
397 while Present (Par) loop
398
399 if (Nkind (Par) = N_Function_Call or else
400 Nkind (Par) = N_Procedure_Call_Statement or else
401 Nkind (Par) = N_Assignment_Statement or else
402 Nkind (Par) = N_Op_Eq or else
403 Nkind (Par) = N_Op_Ne)
404 and then Is_Tagged_Type (Etype (Subp))
405 then
406 return;
407
408 elsif Nkind (Par) = N_Qualified_Expression
409 or else Nkind (Par) = N_Unchecked_Type_Conversion
410 then
411 Par := Parent (Par);
412
413 else
414 if Ekind (Subp) = E_Function then
415 Error_Msg_N
416 ("call to abstract function must be dispatching", N);
417
418 -- This error can occur for a procedure in the case of a
419 -- call to an abstract formal procedure with a statically
420 -- tagged operand.
421
422 else
423 Error_Msg_N
424 ("call to abstract procedure must be dispatching",
425 N);
426 end if;
427
428 return;
429 end if;
430 end loop;
431 end if;
432 end if;
433 end Check_Dispatching_Context;
434
435 -- Start of processing for Check_Dispatching_Call
436
437 begin
438 -- Find a controlling argument, if any
439
440 if Present (Parameter_Associations (N)) then
441 Actual := First_Actual (N);
442
443 Subp_Entity := Entity (Name (N));
444 Formal := First_Formal (Subp_Entity);
445
446 while Present (Actual) loop
447 Control := Find_Controlling_Arg (Actual);
448 exit when Present (Control);
449
450 -- Check for the case where the actual is a tag-indeterminate call
451 -- whose result type is different than the tagged type associated
452 -- with the containing call, but is an ancestor of the type.
453
454 if Is_Controlling_Formal (Formal)
455 and then Is_Tag_Indeterminate (Actual)
456 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
457 and then Is_Ancestor (Etype (Actual), Etype (Formal))
458 then
459 Indeterm_Ancestor_Call := True;
460 Indeterm_Ctrl_Type := Etype (Formal);
461
462 -- If the formal is controlling but the actual is not, the type
463 -- of the actual is statically known, and may be used as the
464 -- controlling tag for some other tag-indeterminate actual.
465
466 elsif Is_Controlling_Formal (Formal)
467 and then Is_Entity_Name (Actual)
468 and then Is_Tagged_Type (Etype (Actual))
469 then
470 Static_Tag := Actual;
471 end if;
472
473 Next_Actual (Actual);
474 Next_Formal (Formal);
475 end loop;
476
477 -- If the call doesn't have a controlling actual but does have an
478 -- indeterminate actual that requires dispatching treatment, then an
479 -- object is needed that will serve as the controlling argument for a
480 -- dispatching call on the indeterminate actual. This can only occur
481 -- in the unusual situation of a default actual given by a
482 -- tag-indeterminate call and where the type of the call is an
483 -- ancestor of the type associated with a containing call to an
484 -- inherited operation (see AI-239).
485
486 -- Rather than create an object of the tagged type, which would be
487 -- problematic for various reasons (default initialization,
488 -- discriminants), the tag of the containing call's associated tagged
489 -- type is directly used to control the dispatching.
490
491 if No (Control)
492 and then Indeterm_Ancestor_Call
493 and then No (Static_Tag)
494 then
495 Control :=
496 Make_Attribute_Reference (Loc,
497 Prefix => New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
498 Attribute_Name => Name_Tag);
499
500 Analyze (Control);
501 end if;
502
503 if Present (Control) then
504
505 -- Verify that no controlling arguments are statically tagged
506
507 if Debug_Flag_E then
508 Write_Str ("Found Dispatching call");
509 Write_Int (Int (N));
510 Write_Eol;
511 end if;
512
513 Actual := First_Actual (N);
514
515 while Present (Actual) loop
516 if Actual /= Control then
517
518 if not Is_Controlling_Actual (Actual) then
519 null; -- Can be anything
520
521 elsif Is_Dynamically_Tagged (Actual) then
522 null; -- Valid parameter
523
524 elsif Is_Tag_Indeterminate (Actual) then
525
526 -- The tag is inherited from the enclosing call (the node
527 -- we are currently analyzing). Explicitly expand the
528 -- actual, since the previous call to Expand (from
529 -- Resolve_Call) had no way of knowing about the required
530 -- dispatching.
531
532 Propagate_Tag (Control, Actual);
533
534 else
535 Error_Msg_N
536 ("controlling argument is not dynamically tagged",
537 Actual);
538 return;
539 end if;
540 end if;
541
542 Next_Actual (Actual);
543 end loop;
544
545 -- Mark call as a dispatching call
546
547 Set_Controlling_Argument (N, Control);
548 Check_Restriction (No_Dispatching_Calls, N);
549
550 -- The dispatching call may need to be converted into a direct
551 -- call in certain cases.
552
553 Check_Direct_Call;
554
555 -- If there is a statically tagged actual and a tag-indeterminate
556 -- call to a function of the ancestor (such as that provided by a
557 -- default), then treat this as a dispatching call and propagate
558 -- the tag to the tag-indeterminate call(s).
559
560 elsif Present (Static_Tag) and then Indeterm_Ancestor_Call then
561 Control :=
562 Make_Attribute_Reference (Loc,
563 Prefix =>
564 New_Occurrence_Of (Etype (Static_Tag), Loc),
565 Attribute_Name => Name_Tag);
566
567 Analyze (Control);
568
569 Actual := First_Actual (N);
570 Formal := First_Formal (Subp_Entity);
571 while Present (Actual) loop
572 if Is_Tag_Indeterminate (Actual)
573 and then Is_Controlling_Formal (Formal)
574 then
575 Propagate_Tag (Control, Actual);
576 end if;
577
578 Next_Actual (Actual);
579 Next_Formal (Formal);
580 end loop;
581
582 Check_Dispatching_Context;
583
584 else
585 -- The call is not dispatching, so check that there aren't any
586 -- tag-indeterminate abstract calls left.
587
588 Actual := First_Actual (N);
589 while Present (Actual) loop
590 if Is_Tag_Indeterminate (Actual) then
591
592 -- Function call case
593
594 if Nkind (Original_Node (Actual)) = N_Function_Call then
595 Func := Entity (Name (Original_Node (Actual)));
596
597 -- If the actual is an attribute then it can't be abstract
598 -- (the only current case of a tag-indeterminate attribute
599 -- is the stream Input attribute).
600
601 elsif
602 Nkind (Original_Node (Actual)) = N_Attribute_Reference
603 then
604 Func := Empty;
605
606 -- Only other possibility is a qualified expression whose
607 -- constituent expression is itself a call.
608
609 else
610 Func :=
611 Entity (Name
612 (Original_Node
613 (Expression (Original_Node (Actual)))));
614 end if;
615
616 if Present (Func) and then Is_Abstract_Subprogram (Func) then
617 Error_Msg_N (
618 "call to abstract function must be dispatching", N);
619 end if;
620 end if;
621
622 Next_Actual (Actual);
623 end loop;
624
625 Check_Dispatching_Context;
626 end if;
627
628 else
629 -- If dispatching on result, the enclosing call, if any, will
630 -- determine the controlling argument. Otherwise this is the
631 -- primitive operation of the root type.
632
633 Check_Dispatching_Context;
634 end if;
635 end Check_Dispatching_Call;
636
637 ---------------------------------
638 -- Check_Dispatching_Operation --
639 ---------------------------------
640
641 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
642 Tagged_Type : Entity_Id;
643 Has_Dispatching_Parent : Boolean := False;
644 Body_Is_Last_Primitive : Boolean := False;
645
646 function Is_Visibly_Controlled (T : Entity_Id) return Boolean;
647 -- Check whether T is derived from a visibly controlled type.
648 -- This is true if the root type is declared in Ada.Finalization.
649 -- If T is derived instead from a private type whose full view
650 -- is controlled, an explicit Initialize/Adjust/Finalize subprogram
651 -- does not override the inherited one.
652
653 ---------------------------
654 -- Is_Visibly_Controlled --
655 ---------------------------
656
657 function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
658 Root : constant Entity_Id := Root_Type (T);
659 begin
660 return Chars (Scope (Root)) = Name_Finalization
661 and then Chars (Scope (Scope (Root))) = Name_Ada
662 and then Scope (Scope (Scope (Root))) = Standard_Standard;
663 end Is_Visibly_Controlled;
664
665 -- Start of processing for Check_Dispatching_Operation
666
667 begin
668 if Ekind (Subp) /= E_Procedure and then Ekind (Subp) /= E_Function then
669 return;
670 end if;
671
672 Set_Is_Dispatching_Operation (Subp, False);
673 Tagged_Type := Find_Dispatching_Type (Subp);
674
675 -- Ada 2005 (AI-345)
676
677 if Ada_Version = Ada_05
678 and then Present (Tagged_Type)
679 and then Is_Concurrent_Type (Tagged_Type)
680 then
681 -- Protect the frontend against previously detected errors
682
683 if No (Corresponding_Record_Type (Tagged_Type)) then
684 return;
685 end if;
686
687 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
688 end if;
689
690 -- (AI-345): The task body procedure is not a primitive of the tagged
691 -- type
692
693 if Present (Tagged_Type)
694 and then Is_Concurrent_Record_Type (Tagged_Type)
695 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
696 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
697 and then Subp = Get_Task_Body_Procedure
698 (Corresponding_Concurrent_Type (Tagged_Type))
699 then
700 return;
701 end if;
702
703 -- If Subp is derived from a dispatching operation then it should
704 -- always be treated as dispatching. In this case various checks
705 -- below will be bypassed. Makes sure that late declarations for
706 -- inherited private subprograms are treated as dispatching, even
707 -- if the associated tagged type is already frozen.
708
709 Has_Dispatching_Parent :=
710 Present (Alias (Subp))
711 and then Is_Dispatching_Operation (Alias (Subp));
712
713 if No (Tagged_Type) then
714
715 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
716 -- with an abstract interface type unless the interface acts as a
717 -- parent type in a derivation. If the interface type is a formal
718 -- type then the operation is not primitive and therefore legal.
719
720 declare
721 E : Entity_Id;
722 Typ : Entity_Id;
723
724 begin
725 E := First_Entity (Subp);
726 while Present (E) loop
727
728 -- For an access parameter, check designated type.
729
730 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
731 Typ := Designated_Type (Etype (E));
732 else
733 Typ := Etype (E);
734 end if;
735
736 if Comes_From_Source (Subp)
737 and then Is_Interface (Typ)
738 and then not Is_Class_Wide_Type (Typ)
739 and then not Is_Derived_Type (Typ)
740 and then not Is_Generic_Type (Typ)
741 and then not In_Instance
742 then
743 Error_Msg_N ("?declaration of& is too late!", Subp);
744 Error_Msg_NE
745 ("\spec should appear immediately after declaration of &!",
746 Subp, Typ);
747 exit;
748 end if;
749
750 Next_Entity (E);
751 end loop;
752
753 -- In case of functions check also the result type
754
755 if Ekind (Subp) = E_Function then
756 if Is_Access_Type (Etype (Subp)) then
757 Typ := Designated_Type (Etype (Subp));
758 else
759 Typ := Etype (Subp);
760 end if;
761
762 if not Is_Class_Wide_Type (Typ)
763 and then Is_Interface (Typ)
764 and then not Is_Derived_Type (Typ)
765 then
766 Error_Msg_N ("?declaration of& is too late!", Subp);
767 Error_Msg_NE
768 ("\spec should appear immediately after declaration of &!",
769 Subp, Typ);
770 end if;
771 end if;
772 end;
773
774 return;
775
776 -- The subprograms build internally after the freezing point (such as
777 -- init procs, interface thunks, type support subprograms, and Offset
778 -- to top functions for accessing interface components in variable
779 -- size tagged types) are not primitives.
780
781 elsif Is_Frozen (Tagged_Type)
782 and then not Comes_From_Source (Subp)
783 and then not Has_Dispatching_Parent
784 then
785 -- Complete decoration if internally built subprograms that override
786 -- a dispatching primitive. These entities correspond with the
787 -- following cases:
788
789 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
790 -- to override functions of nonabstract null extensions. These
791 -- primitives were added to the list of primitives of the tagged
792 -- type by Make_Controlling_Function_Wrappers. However, attribute
793 -- Is_Dispatching_Operation must be set to true.
794
795 -- 2. Subprograms associated with stream attributes (built by
796 -- New_Stream_Subprogram)
797
798 if Present (Old_Subp)
799 and then Is_Overriding_Operation (Subp)
800 and then Is_Dispatching_Operation (Old_Subp)
801 then
802 pragma Assert
803 ((Ekind (Subp) = E_Function
804 and then Is_Dispatching_Operation (Old_Subp)
805 and then Is_Null_Extension (Base_Type (Etype (Subp))))
806 or else Get_TSS_Name (Subp) = TSS_Stream_Read
807 or else Get_TSS_Name (Subp) = TSS_Stream_Write);
808
809 Set_Is_Dispatching_Operation (Subp);
810 end if;
811
812 return;
813
814 -- The operation may be a child unit, whose scope is the defining
815 -- package, but which is not a primitive operation of the type.
816
817 elsif Is_Child_Unit (Subp) then
818 return;
819
820 -- If the subprogram is not defined in a package spec, the only case
821 -- where it can be a dispatching op is when it overrides an operation
822 -- before the freezing point of the type.
823
824 elsif ((not Is_Package_Or_Generic_Package (Scope (Subp)))
825 or else In_Package_Body (Scope (Subp)))
826 and then not Has_Dispatching_Parent
827 then
828 if not Comes_From_Source (Subp)
829 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
830 then
831 null;
832
833 -- If the type is already frozen, the overriding is not allowed
834 -- except when Old_Subp is not a dispatching operation (which can
835 -- occur when Old_Subp was inherited by an untagged type). However,
836 -- a body with no previous spec freezes the type "after" its
837 -- declaration, and therefore is a legal overriding (unless the type
838 -- has already been frozen). Only the first such body is legal.
839
840 elsif Present (Old_Subp)
841 and then Is_Dispatching_Operation (Old_Subp)
842 then
843 if Comes_From_Source (Subp)
844 and then
845 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
846 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
847 then
848 declare
849 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
850 Decl_Item : Node_Id := Next (Parent (Tagged_Type));
851
852 begin
853 -- ??? The checks here for whether the type has been
854 -- frozen prior to the new body are not complete. It's
855 -- not simple to check frozenness at this point since
856 -- the body has already caused the type to be prematurely
857 -- frozen in Analyze_Declarations, but we're forced to
858 -- recheck this here because of the odd rule interpretation
859 -- that allows the overriding if the type wasn't frozen
860 -- prior to the body. The freezing action should probably
861 -- be delayed until after the spec is seen, but that's
862 -- a tricky change to the delicate freezing code.
863
864 -- Look at each declaration following the type up until the
865 -- new subprogram body. If any of the declarations is a body
866 -- then the type has been frozen already so the overriding
867 -- primitive is illegal.
868
869 while Present (Decl_Item)
870 and then (Decl_Item /= Subp_Body)
871 loop
872 if Comes_From_Source (Decl_Item)
873 and then (Nkind (Decl_Item) in N_Proper_Body
874 or else Nkind (Decl_Item) in N_Body_Stub)
875 then
876 Error_Msg_N ("overriding of& is too late!", Subp);
877 Error_Msg_N
878 ("\spec should appear immediately after the type!",
879 Subp);
880 exit;
881 end if;
882
883 Next (Decl_Item);
884 end loop;
885
886 -- If the subprogram doesn't follow in the list of
887 -- declarations including the type then the type has
888 -- definitely been frozen already and the body is illegal.
889
890 if No (Decl_Item) then
891 Error_Msg_N ("overriding of& is too late!", Subp);
892 Error_Msg_N
893 ("\spec should appear immediately after the type!",
894 Subp);
895
896 elsif Is_Frozen (Subp) then
897
898 -- The subprogram body declares a primitive operation.
899 -- if the subprogram is already frozen, we must update
900 -- its dispatching information explicitly here. The
901 -- information is taken from the overridden subprogram.
902 -- We must also generate a cross-reference entry because
903 -- references to other primitives were already created
904 -- when type was frozen.
905
906 Body_Is_Last_Primitive := True;
907
908 if Present (DTC_Entity (Old_Subp)) then
909 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
910 Set_DT_Position (Subp, DT_Position (Old_Subp));
911
912 if not Restriction_Active (No_Dispatching_Calls) then
913 if Building_Static_DT (Tagged_Type) then
914
915 -- If the static dispatch table has not been
916 -- built then there is nothing else to do now;
917 -- otherwise we notify that we cannot build the
918 -- static dispatch table.
919
920 if Has_Dispatch_Table (Tagged_Type) then
921 Error_Msg_N
922 ("overriding of& is too late for building" &
923 " static dispatch tables!", Subp);
924 Error_Msg_N
925 ("\spec should appear immediately after" &
926 " the type!", Subp);
927 end if;
928
929 else
930 Insert_Actions_After (Subp_Body,
931 Register_Primitive (Sloc (Subp_Body),
932 Prim => Subp));
933 end if;
934
935 -- Indicate that this is an overriding operation,
936 -- and replace the overriden entry in the list of
937 -- primitive operations, which is used for xref
938 -- generation subsequently.
939
940 Generate_Reference (Tagged_Type, Subp, 'P', False);
941 Override_Dispatching_Operation
942 (Tagged_Type, Old_Subp, Subp);
943 end if;
944 end if;
945 end if;
946 end;
947
948 else
949 Error_Msg_N ("overriding of& is too late!", Subp);
950 Error_Msg_N
951 ("\subprogram spec should appear immediately after the type!",
952 Subp);
953 end if;
954
955 -- If the type is not frozen yet and we are not in the overriding
956 -- case it looks suspiciously like an attempt to define a primitive
957 -- operation, which requires the declaration to be in a package spec
958 -- (3.2.3(6)).
959
960 elsif not Is_Frozen (Tagged_Type) then
961 Error_Msg_N
962 ("?not dispatching (must be defined in a package spec)", Subp);
963 return;
964
965 -- When the type is frozen, it is legitimate to define a new
966 -- non-primitive operation.
967
968 else
969 return;
970 end if;
971
972 -- Now, we are sure that the scope is a package spec. If the subprogram
973 -- is declared after the freezing point of the type that's an error
974
975 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
976 Error_Msg_N ("this primitive operation is declared too late", Subp);
977 Error_Msg_NE
978 ("?no primitive operations for& after this line",
979 Freeze_Node (Tagged_Type),
980 Tagged_Type);
981 return;
982 end if;
983
984 Check_Controlling_Formals (Tagged_Type, Subp);
985
986 -- Now it should be a correct primitive operation, put it in the list
987
988 if Present (Old_Subp) then
989
990 -- If the type has interfaces we complete this check after we set
991 -- attribute Is_Dispatching_Operation.
992
993 Check_Subtype_Conformant (Subp, Old_Subp);
994
995 if (Chars (Subp) = Name_Initialize
996 or else Chars (Subp) = Name_Adjust
997 or else Chars (Subp) = Name_Finalize)
998 and then Is_Controlled (Tagged_Type)
999 and then not Is_Visibly_Controlled (Tagged_Type)
1000 then
1001 Set_Is_Overriding_Operation (Subp, False);
1002 Error_Msg_NE
1003 ("operation does not override inherited&?", Subp, Subp);
1004 else
1005 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1006 Set_Is_Overriding_Operation (Subp);
1007
1008 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1009 -- that covers abstract interface subprograms we must register it
1010 -- in all the secondary dispatch tables associated with abstract
1011 -- interfaces. We do this now only if not building static tables.
1012 -- Otherwise the patch code is emitted after those tables are
1013 -- built, to prevent access_before_elaboration in gigi.
1014
1015 if Body_Is_Last_Primitive then
1016 declare
1017 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1018 Elmt : Elmt_Id;
1019 Prim : Node_Id;
1020
1021 begin
1022 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1023 while Present (Elmt) loop
1024 Prim := Node (Elmt);
1025
1026 if Present (Alias (Prim))
1027 and then Present (Interface_Alias (Prim))
1028 and then Alias (Prim) = Subp
1029 and then not Building_Static_DT (Tagged_Type)
1030 then
1031 Insert_Actions_After (Subp_Body,
1032 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1033 end if;
1034
1035 Next_Elmt (Elmt);
1036 end loop;
1037
1038 -- Redisplay the contents of the updated dispatch table
1039
1040 if Debug_Flag_ZZ then
1041 Write_Str ("Late overriding: ");
1042 Write_DT (Tagged_Type);
1043 end if;
1044 end;
1045 end if;
1046 end if;
1047
1048 -- If no old subprogram, then we add this as a dispatching operation,
1049 -- but we avoid doing this if an error was posted, to prevent annoying
1050 -- cascaded errors.
1051
1052 elsif not Error_Posted (Subp) then
1053 Add_Dispatching_Operation (Tagged_Type, Subp);
1054 end if;
1055
1056 Set_Is_Dispatching_Operation (Subp, True);
1057
1058 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1059 -- subtype conformance against all the interfaces covered by this
1060 -- primitive.
1061
1062 if Present (Old_Subp)
1063 and then Has_Interfaces (Tagged_Type)
1064 then
1065 declare
1066 Ifaces_List : Elist_Id;
1067 Iface_Elmt : Elmt_Id;
1068 Iface_Prim_Elmt : Elmt_Id;
1069 Iface_Prim : Entity_Id;
1070 Ret_Typ : Entity_Id;
1071
1072 begin
1073 Collect_Interfaces (Tagged_Type, Ifaces_List);
1074
1075 Iface_Elmt := First_Elmt (Ifaces_List);
1076 while Present (Iface_Elmt) loop
1077 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1078 Iface_Prim_Elmt :=
1079 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1080 while Present (Iface_Prim_Elmt) loop
1081 Iface_Prim := Node (Iface_Prim_Elmt);
1082
1083 if Is_Interface_Conformant
1084 (Tagged_Type, Iface_Prim, Subp)
1085 then
1086 -- Handle procedures, functions whose return type
1087 -- matches, or functions not returning interfaces
1088
1089 if Ekind (Subp) = E_Procedure
1090 or else Etype (Iface_Prim) = Etype (Subp)
1091 or else not Is_Interface (Etype (Iface_Prim))
1092 then
1093 Check_Subtype_Conformant
1094 (New_Id => Subp,
1095 Old_Id => Iface_Prim,
1096 Err_Loc => Subp,
1097 Skip_Controlling_Formals => True);
1098
1099 -- Handle functions returning interfaces
1100
1101 elsif Implements_Interface
1102 (Etype (Subp), Etype (Iface_Prim))
1103 then
1104 -- Temporarily force both entities to return the
1105 -- same type. Required because Subtype_Conformant
1106 -- does not handle this case.
1107
1108 Ret_Typ := Etype (Iface_Prim);
1109 Set_Etype (Iface_Prim, Etype (Subp));
1110
1111 Check_Subtype_Conformant
1112 (New_Id => Subp,
1113 Old_Id => Iface_Prim,
1114 Err_Loc => Subp,
1115 Skip_Controlling_Formals => True);
1116
1117 Set_Etype (Iface_Prim, Ret_Typ);
1118 end if;
1119 end if;
1120
1121 Next_Elmt (Iface_Prim_Elmt);
1122 end loop;
1123 end if;
1124
1125 Next_Elmt (Iface_Elmt);
1126 end loop;
1127 end;
1128 end if;
1129
1130 if not Body_Is_Last_Primitive then
1131 Set_DT_Position (Subp, No_Uint);
1132
1133 elsif Has_Controlled_Component (Tagged_Type)
1134 and then
1135 (Chars (Subp) = Name_Initialize
1136 or else Chars (Subp) = Name_Adjust
1137 or else Chars (Subp) = Name_Finalize)
1138 then
1139 declare
1140 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1141 Decl : Node_Id;
1142 Old_P : Entity_Id;
1143 Old_Bod : Node_Id;
1144 Old_Spec : Entity_Id;
1145
1146 C_Names : constant array (1 .. 3) of Name_Id :=
1147 (Name_Initialize,
1148 Name_Adjust,
1149 Name_Finalize);
1150
1151 D_Names : constant array (1 .. 3) of TSS_Name_Type :=
1152 (TSS_Deep_Initialize,
1153 TSS_Deep_Adjust,
1154 TSS_Deep_Finalize);
1155
1156 begin
1157 -- Remove previous controlled function, which was constructed
1158 -- and analyzed when the type was frozen. This requires
1159 -- removing the body of the redefined primitive, as well as
1160 -- its specification if needed (there is no spec created for
1161 -- Deep_Initialize, see exp_ch3.adb). We must also dismantle
1162 -- the exception information that may have been generated for
1163 -- it when front end zero-cost tables are enabled.
1164
1165 for J in D_Names'Range loop
1166 Old_P := TSS (Tagged_Type, D_Names (J));
1167
1168 if Present (Old_P)
1169 and then Chars (Subp) = C_Names (J)
1170 then
1171 Old_Bod := Unit_Declaration_Node (Old_P);
1172 Remove (Old_Bod);
1173 Set_Is_Eliminated (Old_P);
1174 Set_Scope (Old_P, Scope (Current_Scope));
1175
1176 if Nkind (Old_Bod) = N_Subprogram_Body
1177 and then Present (Corresponding_Spec (Old_Bod))
1178 then
1179 Old_Spec := Corresponding_Spec (Old_Bod);
1180 Set_Has_Completion (Old_Spec, False);
1181 end if;
1182 end if;
1183 end loop;
1184
1185 Build_Late_Proc (Tagged_Type, Chars (Subp));
1186
1187 -- The new operation is added to the actions of the freeze
1188 -- node for the type, but this node has already been analyzed,
1189 -- so we must retrieve and analyze explicitly the new body.
1190
1191 if Present (F_Node)
1192 and then Present (Actions (F_Node))
1193 then
1194 Decl := Last (Actions (F_Node));
1195 Analyze (Decl);
1196 end if;
1197 end;
1198 end if;
1199 end Check_Dispatching_Operation;
1200
1201 ------------------------------------------
1202 -- Check_Operation_From_Incomplete_Type --
1203 ------------------------------------------
1204
1205 procedure Check_Operation_From_Incomplete_Type
1206 (Subp : Entity_Id;
1207 Typ : Entity_Id)
1208 is
1209 Full : constant Entity_Id := Full_View (Typ);
1210 Parent_Typ : constant Entity_Id := Etype (Full);
1211 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
1212 New_Prim : constant Elist_Id := Primitive_Operations (Full);
1213 Op1, Op2 : Elmt_Id;
1214 Prev : Elmt_Id := No_Elmt;
1215
1216 function Derives_From (Proc : Entity_Id) return Boolean;
1217 -- Check that Subp has the signature of an operation derived from Proc.
1218 -- Subp has an access parameter that designates Typ.
1219
1220 ------------------
1221 -- Derives_From --
1222 ------------------
1223
1224 function Derives_From (Proc : Entity_Id) return Boolean is
1225 F1, F2 : Entity_Id;
1226
1227 begin
1228 if Chars (Proc) /= Chars (Subp) then
1229 return False;
1230 end if;
1231
1232 F1 := First_Formal (Proc);
1233 F2 := First_Formal (Subp);
1234
1235 while Present (F1) and then Present (F2) loop
1236
1237 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
1238
1239 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
1240 return False;
1241
1242 elsif Designated_Type (Etype (F1)) = Parent_Typ
1243 and then Designated_Type (Etype (F2)) /= Full
1244 then
1245 return False;
1246 end if;
1247
1248 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
1249 return False;
1250
1251 elsif Etype (F1) /= Etype (F2) then
1252 return False;
1253 end if;
1254
1255 Next_Formal (F1);
1256 Next_Formal (F2);
1257 end loop;
1258
1259 return No (F1) and then No (F2);
1260 end Derives_From;
1261
1262 -- Start of processing for Check_Operation_From_Incomplete_Type
1263
1264 begin
1265 -- The operation may override an inherited one, or may be a new one
1266 -- altogether. The inherited operation will have been hidden by the
1267 -- current one at the point of the type derivation, so it does not
1268 -- appear in the list of primitive operations of the type. We have to
1269 -- find the proper place of insertion in the list of primitive opera-
1270 -- tions by iterating over the list for the parent type.
1271
1272 Op1 := First_Elmt (Old_Prim);
1273 Op2 := First_Elmt (New_Prim);
1274
1275 while Present (Op1) and then Present (Op2) loop
1276
1277 if Derives_From (Node (Op1)) then
1278
1279 if No (Prev) then
1280
1281 -- Avoid adding it to the list of primitives if already there!
1282
1283 if Node (Op2) /= Subp then
1284 Prepend_Elmt (Subp, New_Prim);
1285 end if;
1286
1287 else
1288 Insert_Elmt_After (Subp, Prev);
1289 end if;
1290
1291 return;
1292 end if;
1293
1294 Prev := Op2;
1295 Next_Elmt (Op1);
1296 Next_Elmt (Op2);
1297 end loop;
1298
1299 -- Operation is a new primitive
1300
1301 Append_Elmt (Subp, New_Prim);
1302 end Check_Operation_From_Incomplete_Type;
1303
1304 ---------------------------------------
1305 -- Check_Operation_From_Private_View --
1306 ---------------------------------------
1307
1308 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
1309 Tagged_Type : Entity_Id;
1310
1311 begin
1312 if Is_Dispatching_Operation (Alias (Subp)) then
1313 Set_Scope (Subp, Current_Scope);
1314 Tagged_Type := Find_Dispatching_Type (Subp);
1315
1316 -- Add Old_Subp to primitive operations if not already present.
1317
1318 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
1319 Append_Unique_Elmt (Old_Subp, Primitive_Operations (Tagged_Type));
1320
1321 -- If Old_Subp isn't already marked as dispatching then
1322 -- this is the case of an operation of an untagged private
1323 -- type fulfilled by a tagged type that overrides an
1324 -- inherited dispatching operation, so we set the necessary
1325 -- dispatching attributes here.
1326
1327 if not Is_Dispatching_Operation (Old_Subp) then
1328
1329 -- If the untagged type has no discriminants, and the full
1330 -- view is constrained, there will be a spurious mismatch
1331 -- of subtypes on the controlling arguments, because the tagged
1332 -- type is the internal base type introduced in the derivation.
1333 -- Use the original type to verify conformance, rather than the
1334 -- base type.
1335
1336 if not Comes_From_Source (Tagged_Type)
1337 and then Has_Discriminants (Tagged_Type)
1338 then
1339 declare
1340 Formal : Entity_Id;
1341 begin
1342 Formal := First_Formal (Old_Subp);
1343 while Present (Formal) loop
1344 if Tagged_Type = Base_Type (Etype (Formal)) then
1345 Tagged_Type := Etype (Formal);
1346 end if;
1347
1348 Next_Formal (Formal);
1349 end loop;
1350 end;
1351
1352 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
1353 Tagged_Type := Etype (Old_Subp);
1354 end if;
1355 end if;
1356
1357 Check_Controlling_Formals (Tagged_Type, Old_Subp);
1358 Set_Is_Dispatching_Operation (Old_Subp, True);
1359 Set_DT_Position (Old_Subp, No_Uint);
1360 end if;
1361
1362 -- If the old subprogram is an explicit renaming of some other
1363 -- entity, it is not overridden by the inherited subprogram.
1364 -- Otherwise, update its alias and other attributes.
1365
1366 if Present (Alias (Old_Subp))
1367 and then Nkind (Unit_Declaration_Node (Old_Subp))
1368 /= N_Subprogram_Renaming_Declaration
1369 then
1370 Set_Alias (Old_Subp, Alias (Subp));
1371
1372 -- The derived subprogram should inherit the abstractness
1373 -- of the parent subprogram (except in the case of a function
1374 -- returning the type). This sets the abstractness properly
1375 -- for cases where a private extension may have inherited
1376 -- an abstract operation, but the full type is derived from
1377 -- a descendant type and inherits a nonabstract version.
1378
1379 if Etype (Subp) /= Tagged_Type then
1380 Set_Is_Abstract_Subprogram
1381 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
1382 end if;
1383 end if;
1384 end if;
1385 end if;
1386 end Check_Operation_From_Private_View;
1387
1388 --------------------------
1389 -- Find_Controlling_Arg --
1390 --------------------------
1391
1392 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
1393 Orig_Node : constant Node_Id := Original_Node (N);
1394 Typ : Entity_Id;
1395
1396 begin
1397 if Nkind (Orig_Node) = N_Qualified_Expression then
1398 return Find_Controlling_Arg (Expression (Orig_Node));
1399 end if;
1400
1401 -- Dispatching on result case. If expansion is disabled, the node still
1402 -- has the structure of a function call. However, if the function name
1403 -- is an operator and the call was given in infix form, the original
1404 -- node has no controlling result and we must examine the current node.
1405
1406 if Nkind (N) = N_Function_Call
1407 and then Present (Controlling_Argument (N))
1408 and then Has_Controlling_Result (Entity (Name (N)))
1409 then
1410 return Controlling_Argument (N);
1411
1412 -- If expansion is enabled, the call may have been transformed into
1413 -- an indirect call, and we need to recover the original node.
1414
1415 elsif Nkind (Orig_Node) = N_Function_Call
1416 and then Present (Controlling_Argument (Orig_Node))
1417 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
1418 then
1419 return Controlling_Argument (Orig_Node);
1420
1421 -- Normal case
1422
1423 elsif Is_Controlling_Actual (N)
1424 or else
1425 (Nkind (Parent (N)) = N_Qualified_Expression
1426 and then Is_Controlling_Actual (Parent (N)))
1427 then
1428 Typ := Etype (N);
1429
1430 if Is_Access_Type (Typ) then
1431 -- In the case of an Access attribute, use the type of
1432 -- the prefix, since in the case of an actual for an
1433 -- access parameter, the attribute's type may be of a
1434 -- specific designated type, even though the prefix
1435 -- type is class-wide.
1436
1437 if Nkind (N) = N_Attribute_Reference then
1438 Typ := Etype (Prefix (N));
1439
1440 -- An allocator is dispatching if the type of qualified
1441 -- expression is class_wide, in which case this is the
1442 -- controlling type.
1443
1444 elsif Nkind (Orig_Node) = N_Allocator
1445 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
1446 then
1447 Typ := Etype (Expression (Orig_Node));
1448
1449 else
1450 Typ := Designated_Type (Typ);
1451 end if;
1452 end if;
1453
1454 if Is_Class_Wide_Type (Typ)
1455 or else
1456 (Nkind (Parent (N)) = N_Qualified_Expression
1457 and then Is_Access_Type (Etype (N))
1458 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
1459 then
1460 return N;
1461 end if;
1462 end if;
1463
1464 return Empty;
1465 end Find_Controlling_Arg;
1466
1467 ---------------------------
1468 -- Find_Dispatching_Type --
1469 ---------------------------
1470
1471 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
1472 A_Formal : Entity_Id;
1473 Formal : Entity_Id;
1474 Ctrl_Type : Entity_Id;
1475
1476 begin
1477 if Present (DTC_Entity (Subp)) then
1478 return Scope (DTC_Entity (Subp));
1479
1480 -- For subprograms internally generated by derivations of tagged types
1481 -- use the alias subprogram as a reference to locate the dispatching
1482 -- type of Subp
1483
1484 elsif not Comes_From_Source (Subp)
1485 and then Present (Alias (Subp))
1486 and then Is_Dispatching_Operation (Alias (Subp))
1487 then
1488 if Ekind (Alias (Subp)) = E_Function
1489 and then Has_Controlling_Result (Alias (Subp))
1490 then
1491 return Check_Controlling_Type (Etype (Subp), Subp);
1492
1493 else
1494 Formal := First_Formal (Subp);
1495 A_Formal := First_Formal (Alias (Subp));
1496 while Present (A_Formal) loop
1497 if Is_Controlling_Formal (A_Formal) then
1498 return Check_Controlling_Type (Etype (Formal), Subp);
1499 end if;
1500
1501 Next_Formal (Formal);
1502 Next_Formal (A_Formal);
1503 end loop;
1504
1505 pragma Assert (False);
1506 return Empty;
1507 end if;
1508
1509 -- General case
1510
1511 else
1512 Formal := First_Formal (Subp);
1513 while Present (Formal) loop
1514 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
1515
1516 if Present (Ctrl_Type) then
1517 return Ctrl_Type;
1518 end if;
1519
1520 Next_Formal (Formal);
1521 end loop;
1522
1523 -- The subprogram may also be dispatching on result
1524
1525 if Present (Etype (Subp)) then
1526 return Check_Controlling_Type (Etype (Subp), Subp);
1527 end if;
1528 end if;
1529
1530 return Empty;
1531 end Find_Dispatching_Type;
1532
1533 ---------------------------------------
1534 -- Find_Primitive_Covering_Interface --
1535 ---------------------------------------
1536
1537 function Find_Primitive_Covering_Interface
1538 (Tagged_Type : Entity_Id;
1539 Iface_Prim : Entity_Id) return Entity_Id
1540 is
1541 E : Entity_Id;
1542
1543 begin
1544 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
1545 or else (Present (Alias (Iface_Prim))
1546 and then
1547 Is_Interface
1548 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
1549
1550 E := Current_Entity (Iface_Prim);
1551 while Present (E) loop
1552 if Is_Subprogram (E)
1553 and then Is_Dispatching_Operation (E)
1554 and then Is_Interface_Conformant (Tagged_Type, Iface_Prim, E)
1555 then
1556 return E;
1557 end if;
1558
1559 E := Homonym (E);
1560 end loop;
1561
1562 return Empty;
1563 end Find_Primitive_Covering_Interface;
1564
1565 ---------------------------
1566 -- Is_Dynamically_Tagged --
1567 ---------------------------
1568
1569 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
1570 begin
1571 if Nkind (N) = N_Error then
1572 return False;
1573 else
1574 return Find_Controlling_Arg (N) /= Empty;
1575 end if;
1576 end Is_Dynamically_Tagged;
1577
1578 --------------------------
1579 -- Is_Tag_Indeterminate --
1580 --------------------------
1581
1582 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
1583 Nam : Entity_Id;
1584 Actual : Node_Id;
1585 Orig_Node : constant Node_Id := Original_Node (N);
1586
1587 begin
1588 if Nkind (Orig_Node) = N_Function_Call
1589 and then Is_Entity_Name (Name (Orig_Node))
1590 then
1591 Nam := Entity (Name (Orig_Node));
1592
1593 if not Has_Controlling_Result (Nam) then
1594 return False;
1595
1596 -- An explicit dereference means that the call has already been
1597 -- expanded and there is no tag to propagate.
1598
1599 elsif Nkind (N) = N_Explicit_Dereference then
1600 return False;
1601
1602 -- If there are no actuals, the call is tag-indeterminate
1603
1604 elsif No (Parameter_Associations (Orig_Node)) then
1605 return True;
1606
1607 else
1608 Actual := First_Actual (Orig_Node);
1609 while Present (Actual) loop
1610 if Is_Controlling_Actual (Actual)
1611 and then not Is_Tag_Indeterminate (Actual)
1612 then
1613 return False; -- one operand is dispatching
1614 end if;
1615
1616 Next_Actual (Actual);
1617 end loop;
1618
1619 return True;
1620 end if;
1621
1622 elsif Nkind (Orig_Node) = N_Qualified_Expression then
1623 return Is_Tag_Indeterminate (Expression (Orig_Node));
1624
1625 -- Case of a call to the Input attribute (possibly rewritten), which is
1626 -- always tag-indeterminate except when its prefix is a Class attribute.
1627
1628 elsif Nkind (Orig_Node) = N_Attribute_Reference
1629 and then
1630 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
1631 and then
1632 Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
1633 then
1634 return True;
1635
1636 -- In Ada 2005 a function that returns an anonymous access type can
1637 -- dispatching, and the dereference of a call to such a function
1638 -- is also tag-indeterminate.
1639
1640 elsif Nkind (Orig_Node) = N_Explicit_Dereference
1641 and then Ada_Version >= Ada_05
1642 then
1643 return Is_Tag_Indeterminate (Prefix (Orig_Node));
1644
1645 else
1646 return False;
1647 end if;
1648 end Is_Tag_Indeterminate;
1649
1650 ------------------------------------
1651 -- Override_Dispatching_Operation --
1652 ------------------------------------
1653
1654 procedure Override_Dispatching_Operation
1655 (Tagged_Type : Entity_Id;
1656 Prev_Op : Entity_Id;
1657 New_Op : Entity_Id)
1658 is
1659 Elmt : Elmt_Id;
1660 Prim : Node_Id;
1661
1662 begin
1663 -- Diagnose failure to match No_Return in parent (Ada-2005, AI-414, but
1664 -- we do it unconditionally in Ada 95 now, since this is our pragma!)
1665
1666 if No_Return (Prev_Op) and then not No_Return (New_Op) then
1667 Error_Msg_N ("procedure & must have No_Return pragma", New_Op);
1668 Error_Msg_N ("\since overridden procedure has No_Return", New_Op);
1669 end if;
1670
1671 -- If there is no previous operation to override, the type declaration
1672 -- was malformed, and an error must have been emitted already.
1673
1674 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1675 while Present (Elmt)
1676 and then Node (Elmt) /= Prev_Op
1677 loop
1678 Next_Elmt (Elmt);
1679 end loop;
1680
1681 if No (Elmt) then
1682 return;
1683 end if;
1684
1685 Replace_Elmt (Elmt, New_Op);
1686
1687 if Ada_Version >= Ada_05
1688 and then Has_Interfaces (Tagged_Type)
1689 then
1690 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
1691 -- entities of the overridden primitive to reference New_Op, and also
1692 -- propagate the proper value of Is_Abstract_Subprogram. Verify
1693 -- that the new operation is subtype conformant with the interface
1694 -- operations that it implements (for operations inherited from the
1695 -- parent itself, this check is made when building the derived type).
1696
1697 -- Note: This code is only executed in case of late overriding
1698
1699 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1700 while Present (Elmt) loop
1701 Prim := Node (Elmt);
1702
1703 if Prim = New_Op then
1704 null;
1705
1706 -- Note: The check on Is_Subprogram protects the frontend against
1707 -- reading attributes in entities that are not yet fully decorated
1708
1709 elsif Is_Subprogram (Prim)
1710 and then Present (Interface_Alias (Prim))
1711 and then Alias (Prim) = Prev_Op
1712 and then Present (Etype (New_Op))
1713 then
1714 Set_Alias (Prim, New_Op);
1715 Check_Subtype_Conformant (New_Op, Prim);
1716 Set_Is_Abstract_Subprogram (Prim,
1717 Is_Abstract_Subprogram (New_Op));
1718
1719 -- Ensure that this entity will be expanded to fill the
1720 -- corresponding entry in its dispatch table.
1721
1722 if not Is_Abstract_Subprogram (Prim) then
1723 Set_Has_Delayed_Freeze (Prim);
1724 end if;
1725 end if;
1726
1727 Next_Elmt (Elmt);
1728 end loop;
1729 end if;
1730
1731 if (not Is_Package_Or_Generic_Package (Current_Scope))
1732 or else not In_Private_Part (Current_Scope)
1733 then
1734 -- Not a private primitive
1735
1736 null;
1737
1738 else pragma Assert (Is_Inherited_Operation (Prev_Op));
1739
1740 -- Make the overriding operation into an alias of the implicit one.
1741 -- In this fashion a call from outside ends up calling the new body
1742 -- even if non-dispatching, and a call from inside calls the
1743 -- overriding operation because it hides the implicit one. To
1744 -- indicate that the body of Prev_Op is never called, set its
1745 -- dispatch table entity to Empty.
1746
1747 Set_Alias (Prev_Op, New_Op);
1748 Set_DTC_Entity (Prev_Op, Empty);
1749 return;
1750 end if;
1751 end Override_Dispatching_Operation;
1752
1753 -------------------
1754 -- Propagate_Tag --
1755 -------------------
1756
1757 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
1758 Call_Node : Node_Id;
1759 Arg : Node_Id;
1760
1761 begin
1762 if Nkind (Actual) = N_Function_Call then
1763 Call_Node := Actual;
1764
1765 elsif Nkind (Actual) = N_Identifier
1766 and then Nkind (Original_Node (Actual)) = N_Function_Call
1767 then
1768 -- Call rewritten as object declaration when stack-checking
1769 -- is enabled. Propagate tag to expression in declaration, which
1770 -- is original call.
1771
1772 Call_Node := Expression (Parent (Entity (Actual)));
1773
1774 -- Ada 2005: If this is a dereference of a call to a function with a
1775 -- dispatching access-result, the tag is propagated when the dereference
1776 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
1777
1778 elsif Nkind (Actual) = N_Explicit_Dereference
1779 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
1780 then
1781 return;
1782
1783 -- Only other possibilities are parenthesized or qualified expression,
1784 -- or an expander-generated unchecked conversion of a function call to
1785 -- a stream Input attribute.
1786
1787 else
1788 Call_Node := Expression (Actual);
1789 end if;
1790
1791 -- Do not set the Controlling_Argument if already set. This happens
1792 -- in the special case of _Input (see Exp_Attr, case Input).
1793
1794 if No (Controlling_Argument (Call_Node)) then
1795 Set_Controlling_Argument (Call_Node, Control);
1796 end if;
1797
1798 Arg := First_Actual (Call_Node);
1799
1800 while Present (Arg) loop
1801 if Is_Tag_Indeterminate (Arg) then
1802 Propagate_Tag (Control, Arg);
1803 end if;
1804
1805 Next_Actual (Arg);
1806 end loop;
1807
1808 -- Expansion of dispatching calls is suppressed when VM_Target, because
1809 -- the VM back-ends directly handle the generation of dispatching
1810 -- calls and would have to undo any expansion to an indirect call.
1811
1812 if Tagged_Type_Expansion then
1813 Expand_Dispatching_Call (Call_Node);
1814
1815 -- Expansion of a dispatching call results in an indirect call, which in
1816 -- turn causes current values to be killed (see Resolve_Call), so on VM
1817 -- targets we do the call here to ensure consistent warnings between VM
1818 -- and non-VM targets.
1819
1820 else
1821 Kill_Current_Values;
1822 end if;
1823 end Propagate_Tag;
1824
1825 end Sem_Disp;