Debian Codegraph @

gcc/ada/sem_disp.adb

raw
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-2025, 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 Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Util; use Exp_Util;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Tss; use Exp_Tss;
37 with Errout; use Errout;
38 with Freeze; use Freeze;
39 with Lib.Xref; use Lib.Xref;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
43 with Opt; use Opt;
44 with Output; use Output;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Eval; use Sem_Eval;
52 with Sem_Type; use Sem_Type;
53 with Sem_Util; use Sem_Util;
54 with Snames; use Snames;
55 with Sinfo; use Sinfo;
56 with Sinfo.Nodes; use Sinfo.Nodes;
57 with Sinfo.Utils; use Sinfo.Utils;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
60 with Warnsw; use Warnsw;
61
62 package body Sem_Disp is
63
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
67
68 procedure Add_Dispatching_Operation
69 (Tagged_Type : Entity_Id;
70 New_Op : Entity_Id);
71 -- Add New_Op in the list of primitive operations of Tagged_Type
72
73 function Check_Controlling_Type
74 (T : Entity_Id;
75 Subp : Entity_Id) return Entity_Id;
76 -- T is the tagged type of a formal parameter or the result of Subp.
77 -- If the subprogram has a controlling parameter or result that matches
78 -- the type, then returns the tagged type of that parameter or result
79 -- (returning the designated tagged type in the case of an access
80 -- parameter); otherwise returns empty.
81
82 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id;
83 -- [Ada 2012:AI-0125] Find an inherited hidden primitive of the dispatching
84 -- type of S that has the same name of S, a type-conformant profile, an
85 -- original corresponding operation O that is a primitive of a visible
86 -- ancestor of the dispatching type of S and O is visible at the point of
87 -- of declaration of S. If the entity is found the Alias of S is set to the
88 -- original corresponding operation S and its Overridden_Operation is set
89 -- to the found entity; otherwise return Empty.
90 --
91 -- This routine does not search for non-hidden primitives since they are
92 -- covered by the normal Ada 2005 rules. Its name was motivated by an
93 -- intermediate version of AI05-0125 where this term was proposed to
94 -- name these entities in the RM.
95
96 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean;
97 -- Check whether a primitive operation is inherited from an operation
98 -- declared in the visible part of its package.
99
100 -------------------------------
101 -- Add_Dispatching_Operation --
102 -------------------------------
103
104 procedure Add_Dispatching_Operation
105 (Tagged_Type : Entity_Id;
106 New_Op : Entity_Id)
107 is
108 List : constant Elist_Id := Primitive_Operations (Tagged_Type);
109
110 begin
111 -- The dispatching operation may already be on the list, if it is the
112 -- wrapper for an inherited function of a null extension (see Exp_Ch3
113 -- for the construction of function wrappers). The list of primitive
114 -- operations must not contain duplicates.
115
116 -- The Default_Initial_Condition and invariant procedures are not added
117 -- to the list of primitives even when they are generated for a tagged
118 -- type. These routines must not be targets of dispatching calls and
119 -- therefore must not appear in the dispatch table because they already
120 -- utilize class-wide-precondition semantics to handle inheritance and
121 -- overriding.
122
123 if Is_Suitable_Primitive (New_Op) then
124 Append_Unique_Elmt (New_Op, List);
125 end if;
126 end Add_Dispatching_Operation;
127
128 --------------------------
129 -- Covered_Interface_Op --
130 --------------------------
131
132 function Covered_Interface_Op (Prim : Entity_Id) return Entity_Id is
133 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
134 Elmt : Elmt_Id;
135 E : Entity_Id;
136
137 begin
138 pragma Assert (Is_Dispatching_Operation (Prim));
139
140 -- Although this is a dispatching primitive we must check if its
141 -- dispatching type is available because it may be the primitive
142 -- of a private type not defined as tagged in its partial view.
143
144 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
145
146 -- If the tagged type is frozen then the internal entities associated
147 -- with interfaces are available in the list of primitives of the
148 -- tagged type and can be used to speed up this search.
149
150 if Is_Frozen (Tagged_Type) then
151 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
152 while Present (Elmt) loop
153 E := Node (Elmt);
154
155 if Present (Interface_Alias (E))
156 and then Alias (E) = Prim
157 then
158 return Interface_Alias (E);
159 end if;
160
161 Next_Elmt (Elmt);
162 end loop;
163
164 -- Otherwise we must collect all the interface primitives and check
165 -- if the Prim overrides (implements) some interface primitive.
166
167 else
168 declare
169 Ifaces_List : Elist_Id;
170 Iface_Elmt : Elmt_Id;
171 Iface : Entity_Id;
172 Iface_Prim : Entity_Id;
173
174 begin
175 Collect_Interfaces (Tagged_Type, Ifaces_List);
176 Iface_Elmt := First_Elmt (Ifaces_List);
177 while Present (Iface_Elmt) loop
178 Iface := Node (Iface_Elmt);
179
180 Elmt := First_Elmt (Primitive_Operations (Iface));
181 while Present (Elmt) loop
182 Iface_Prim := Node (Elmt);
183
184 if Chars (Iface_Prim) = Chars (Prim)
185 and then Is_Interface_Conformant
186 (Tagged_Type, Iface_Prim, Prim)
187 then
188 return Iface_Prim;
189 end if;
190
191 Next_Elmt (Elmt);
192 end loop;
193
194 Next_Elmt (Iface_Elmt);
195 end loop;
196 end;
197 end if;
198 end if;
199
200 return Empty;
201 end Covered_Interface_Op;
202
203 ----------------------------------
204 -- Covered_Interface_Primitives --
205 ----------------------------------
206
207 function Covered_Interface_Primitives (Prim : Entity_Id) return Elist_Id is
208 Tagged_Type : constant Entity_Id := Find_Dispatching_Type (Prim);
209 Elmt : Elmt_Id;
210 E : Entity_Id;
211 Result : Elist_Id := No_Elist;
212
213 begin
214 pragma Assert (Is_Dispatching_Operation (Prim));
215
216 -- Although this is a dispatching primitive we must check if its
217 -- dispatching type is available because it may be the primitive
218 -- of a private type not defined as tagged in its partial view.
219
220 if Present (Tagged_Type) and then Has_Interfaces (Tagged_Type) then
221
222 -- If the tagged type is frozen then the internal entities associated
223 -- with interfaces are available in the list of primitives of the
224 -- tagged type and can be used to speed up this search.
225
226 if Is_Frozen (Tagged_Type) then
227 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
228 while Present (Elmt) loop
229 E := Node (Elmt);
230
231 if Present (Interface_Alias (E))
232 and then Alias (E) = Prim
233 then
234 if No (Result) then
235 Result := New_Elmt_List;
236 end if;
237
238 Append_Elmt (Interface_Alias (E), Result);
239 end if;
240
241 Next_Elmt (Elmt);
242 end loop;
243
244 -- Otherwise we must collect all the interface primitives and check
245 -- whether the Prim overrides (implements) some interface primitive.
246
247 else
248 declare
249 Ifaces_List : Elist_Id;
250 Iface_Elmt : Elmt_Id;
251 Iface : Entity_Id;
252 Iface_Prim : Entity_Id;
253
254 begin
255 Collect_Interfaces (Tagged_Type, Ifaces_List);
256
257 Iface_Elmt := First_Elmt (Ifaces_List);
258 while Present (Iface_Elmt) loop
259 Iface := Node (Iface_Elmt);
260
261 Elmt := First_Elmt (Primitive_Operations (Iface));
262 while Present (Elmt) loop
263 Iface_Prim := Node (Elmt);
264
265 if Chars (Iface_Prim) = Chars (Prim)
266 and then Is_Interface_Conformant
267 (Tagged_Type, Iface_Prim, Prim)
268 then
269 if No (Result) then
270 Result := New_Elmt_List;
271 end if;
272
273 Append_Elmt (Iface_Prim, Result);
274 end if;
275
276 Next_Elmt (Elmt);
277 end loop;
278
279 Next_Elmt (Iface_Elmt);
280 end loop;
281 end;
282 end if;
283 end if;
284
285 return Result;
286 end Covered_Interface_Primitives;
287
288 -------------------------------
289 -- Check_Controlling_Formals --
290 -------------------------------
291
292 procedure Check_Controlling_Formals
293 (Typ : Entity_Id;
294 Subp : Entity_Id)
295 is
296 Formal : Entity_Id;
297 Ctrl_Type : Entity_Id;
298
299 begin
300 -- We skip the check for thunks
301
302 if Is_Thunk (Subp) then
303 return;
304 end if;
305
306 Formal := First_Formal (Subp);
307 while Present (Formal) loop
308 Ctrl_Type := Empty;
309
310 -- Common Ada case
311
312 if not Has_First_Controlling_Parameter_Aspect (Typ) then
313 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
314
315 -- Type with the First_Controlling_Parameter aspect: for overriding
316 -- primitives of a parent type that lacks this aspect, we cannot be
317 -- more restrictive than the overridden primitive. This also applies
318 -- to renamings of dispatching primitives. Dispatching operators can
319 -- have one or two controlling parameters, as long as one of them is
320 -- the first one, and none of the parameters have the same type as
321 -- the operator's result type.
322
323 -- Internal subprograms added by the frontend bypass the restrictions
324 -- of First_Controlling_Parameter aspect.
325
326 elsif Formal = First_Formal (Subp)
327 or else Is_Internal (Subp)
328 or else Present (Overridden_Operation (Subp))
329 or else
330 (Present (Alias (Subp))
331 and then Is_Dispatching_Operation (Ultimate_Alias (Subp)))
332 or else
333 (Ekind (Subp) = E_Function
334 and then Is_Operator_Name (Chars (Subp)))
335 then
336 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
337 end if;
338
339 if Present (Ctrl_Type) then
340 -- Use the full view for an incomplete type
341
342 if Ekind (Ctrl_Type) = E_Incomplete_Type
343 and then Present (Full_View (Ctrl_Type))
344 then
345 Ctrl_Type := Full_View (Ctrl_Type);
346 end if;
347
348 -- Use the corresponding record type for a concurrent type
349
350 if Is_Concurrent_Type (Ctrl_Type)
351 and then Present (Corresponding_Record_Type (Ctrl_Type))
352 then
353 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
354 end if;
355
356 if Ctrl_Type = Typ then
357 Set_Is_Controlling_Formal (Formal);
358
359 -- Ada 2005 (AI-231): Anonymous access types that are used in
360 -- controlling parameters exclude null because it is necessary
361 -- to read the tag to dispatch, and null has no tag.
362
363 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
364 Set_Can_Never_Be_Null (Etype (Formal));
365 Set_Is_Known_Non_Null (Etype (Formal));
366 end if;
367
368 -- Check that the parameter's nominal subtype statically
369 -- matches the first subtype.
370
371 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
372 if not Subtypes_Statically_Match
373 (Typ, Designated_Type (Etype (Formal)))
374 then
375 Error_Msg_N
376 ("parameter subtype does not match controlling type",
377 Formal);
378 end if;
379
380 -- Within a predicate function, the formal may be a subtype
381 -- of a tagged type, given that the predicate is expressed
382 -- in terms of the subtype.
383
384 elsif not Subtypes_Statically_Match (Typ, Etype (Formal))
385 and then not Is_Predicate_Function (Subp)
386 then
387 Error_Msg_N
388 ("parameter subtype does not match controlling type",
389 Formal);
390 end if;
391
392 if Present (Default_Value (Formal)) then
393
394 -- In Ada 2005, access parameters can have defaults
395
396 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
397 and then Ada_Version < Ada_2005
398 then
399 Error_Msg_N
400 ("default not allowed for controlling access parameter",
401 Default_Value (Formal));
402
403 elsif not Is_Tag_Indeterminate (Default_Value (Formal)) then
404 Error_Msg_N
405 ("default expression must be a tag indeterminate" &
406 " function call", Default_Value (Formal));
407 end if;
408 end if;
409
410 elsif Comes_From_Source (Subp) then
411 Error_Msg_N
412 ("operation can be dispatching in only one type", Subp);
413 end if;
414 end if;
415
416 Next_Formal (Formal);
417 end loop;
418
419 -- Functions overriding parent type primitives that lack the aspect
420 -- First_Controlling_Param cannot be more restrictive than the
421 -- overridden function. This also applies to renamings of dispatching
422 -- primitives. Internal subprograms added by the frontend bypass these
423 -- restrictions.
424
425 if Ekind (Subp) in E_Function | E_Generic_Function
426 and then (not Has_First_Controlling_Parameter_Aspect (Typ)
427 or else Is_Internal (Subp)
428 or else
429 (Present (Overridden_Operation (Subp))
430 and then
431 Has_Controlling_Result (Overridden_Operation (Subp)))
432 or else
433 (Present (Alias (Subp))
434 and then
435 Has_Controlling_Result (Ultimate_Alias (Subp))))
436 then
437 Ctrl_Type := Check_Controlling_Type (Etype (Subp), Subp);
438
439 if Present (Ctrl_Type) then
440 -- Use the full view for an incomplete type
441
442 if Ekind (Ctrl_Type) = E_Incomplete_Type
443 and then Present (Full_View (Ctrl_Type))
444 then
445 Ctrl_Type := Full_View (Ctrl_Type);
446 end if;
447
448 -- Use the corresponding record type for a concurrent type
449
450 if Is_Concurrent_Type (Ctrl_Type)
451 and then Present (Corresponding_Record_Type (Ctrl_Type))
452 then
453 Ctrl_Type := Corresponding_Record_Type (Ctrl_Type);
454 end if;
455
456 if Ctrl_Type = Typ then
457 Set_Has_Controlling_Result (Subp);
458
459 -- Check that result subtype statically matches first subtype
460 -- (Ada 2005): Subp may have a controlling access result.
461
462 if Subtypes_Statically_Match (Typ, Etype (Subp))
463 or else (Ekind (Etype (Subp)) = E_Anonymous_Access_Type
464 and then
465 Subtypes_Statically_Match
466 (Typ, Designated_Type (Etype (Subp))))
467 then
468 null;
469
470 else
471 Error_Msg_N
472 ("result subtype does not match controlling type", Subp);
473 end if;
474
475 elsif Comes_From_Source (Subp) then
476 Error_Msg_N
477 ("operation can be dispatching in only one type", Subp);
478 end if;
479 end if;
480 end if;
481 end Check_Controlling_Formals;
482
483 ----------------------------
484 -- Check_Controlling_Type --
485 ----------------------------
486
487 function Check_Controlling_Type
488 (T : Entity_Id;
489 Subp : Entity_Id) return Entity_Id
490 is
491 Tagged_Type : Entity_Id := Empty;
492
493 begin
494 if Is_Tagged_Type (T) then
495 if Is_First_Subtype (T) then
496 Tagged_Type := T;
497 else
498 Tagged_Type := Base_Type (T);
499 end if;
500
501 -- If the type is incomplete, it may have been declared without a
502 -- Tagged indication, but the full view may be tagged, in which case
503 -- that is the controlling type of the subprogram. This is one of the
504 -- approx. 579 places in the language where a lookahead would help.
505
506 elsif Ekind (T) = E_Incomplete_Type
507 and then Present (Full_View (T))
508 and then Is_Tagged_Type (Full_View (T))
509 then
510 Set_Is_Tagged_Type (T);
511 Tagged_Type := Full_View (T);
512
513 elsif Ekind (T) = E_Anonymous_Access_Type
514 and then Is_Tagged_Type (Designated_Type (T))
515 then
516 if Ekind (Designated_Type (T)) /= E_Incomplete_Type then
517 if Is_First_Subtype (Designated_Type (T)) then
518 Tagged_Type := Designated_Type (T);
519 else
520 Tagged_Type := Base_Type (Designated_Type (T));
521 end if;
522
523 -- Ada 2005: an incomplete type can be tagged. An operation with an
524 -- access parameter of the type is dispatching.
525
526 elsif Scope (Designated_Type (T)) = Current_Scope then
527 Tagged_Type := Designated_Type (T);
528
529 -- Ada 2005 (AI-50217)
530
531 elsif From_Limited_With (Designated_Type (T))
532 and then Has_Non_Limited_View (Designated_Type (T))
533 and then Scope (Designated_Type (T)) = Scope (Subp)
534 then
535 if Is_First_Subtype (Non_Limited_View (Designated_Type (T))) then
536 Tagged_Type := Non_Limited_View (Designated_Type (T));
537 else
538 Tagged_Type := Base_Type (Non_Limited_View
539 (Designated_Type (T)));
540 end if;
541 end if;
542 end if;
543
544 if No (Tagged_Type) or else Is_Class_Wide_Type (Tagged_Type) then
545 return Empty;
546
547 -- In the special case of a protected subprogram of a tagged protected
548 -- type that has a formal of a tagged type (or access formal whose type
549 -- designates a tagged type), such a formal is not controlling unless
550 -- it's of the protected type's corresponding record type. The latter
551 -- can occur for the special wrapper subprograms created for protected
552 -- subprograms. Such subprograms may occur in the same scope where some
553 -- formal's tagged type is declared, and we don't want formals of that
554 -- tagged type being marked as controlling, for one thing because they
555 -- aren't controlling from the language point of view, but also because
556 -- this can cause errors for access formals when conformance is checked
557 -- between the spec and body of the protected subprogram (null-exclusion
558 -- status of the formals may be set differently, which is the case that
559 -- led to adding this check).
560
561 elsif Is_Subprogram (Subp)
562 and then Present (Protected_Subprogram (Subp))
563 and then Ekind (Scope (Protected_Subprogram (Subp))) = E_Protected_Type
564 and then
565 Base_Type (Tagged_Type)
566 /= Corresponding_Record_Type (Scope (Protected_Subprogram (Subp)))
567 then
568 return Empty;
569
570 -- The dispatching type and the primitive operation must be defined in
571 -- the same scope, except in the case of abstract formal subprograms.
572
573 elsif (Scope (Subp) = Scope (Tagged_Type)
574 and then (not Is_Generic_Type (Tagged_Type)
575 or else not Comes_From_Source (Subp)))
576 or else
577 (Is_Formal_Subprogram (Subp) and then Is_Abstract_Subprogram (Subp))
578 or else
579 (Nkind (Parent (Parent (Subp))) = N_Subprogram_Renaming_Declaration
580 and then
581 Present (Corresponding_Formal_Spec (Parent (Parent (Subp))))
582 and then
583 Is_Abstract_Subprogram (Subp))
584 then
585 return Tagged_Type;
586
587 else
588 return Empty;
589 end if;
590 end Check_Controlling_Type;
591
592 ----------------------------
593 -- Check_Dispatching_Call --
594 ----------------------------
595
596 procedure Check_Dispatching_Call (N : Node_Id) is
597 Loc : constant Source_Ptr := Sloc (N);
598 Actual : Node_Id;
599 Formal : Entity_Id;
600 Control : Node_Id := Empty;
601 Func : Entity_Id;
602 Subp_Entity : Entity_Id;
603
604 Indeterm_Ctrl_Type : Entity_Id := Empty;
605 -- Type of a controlling formal whose actual is a tag-indeterminate call
606 -- whose result type is different from, but is an ancestor of, the type.
607
608 Static_Tag : Node_Id := Empty;
609 -- If a controlling formal has a statically tagged actual, the tag of
610 -- this actual is to be used for any tag-indeterminate actual.
611
612 procedure Check_Direct_Call;
613 -- In the case when the controlling actual is a class-wide type whose
614 -- root type's completion is a task or protected type, the call is in
615 -- fact direct. This routine detects the above case and modifies the
616 -- call accordingly.
617
618 procedure Check_Dispatching_Context (Call : Node_Id);
619 -- If the call is tag-indeterminate and the entity being called is
620 -- abstract, verify that the context is a call that will eventually
621 -- provide a tag for dispatching, or has provided one already.
622
623 -----------------------
624 -- Check_Direct_Call --
625 -----------------------
626
627 procedure Check_Direct_Call is
628 Typ : Entity_Id := Etype (Control);
629 begin
630 -- Predefined primitives do not receive wrappers since they are built
631 -- from scratch for the corresponding record of synchronized types.
632 -- Equality is in general predefined, but is excluded from the check
633 -- when it is user-defined.
634
635 if Is_Predefined_Dispatching_Operation (Subp_Entity)
636 and then not (Is_User_Defined_Equality (Subp_Entity)
637 and then Comes_From_Source (Subp_Entity)
638 and then Nkind (Parent (Subp_Entity)) =
639 N_Function_Specification)
640 then
641 return;
642 end if;
643
644 if Is_Class_Wide_Type (Typ) then
645 Typ := Root_Type (Typ);
646 end if;
647
648 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
649 Typ := Full_View (Typ);
650 end if;
651
652 if Is_Concurrent_Type (Typ)
653 and then
654 Present (Corresponding_Record_Type (Typ))
655 then
656 Typ := Corresponding_Record_Type (Typ);
657
658 -- The concurrent record's list of primitives should contain a
659 -- wrapper for the entity of the call, retrieve it.
660
661 declare
662 Prim : Entity_Id;
663 Prim_Elmt : Elmt_Id;
664 Wrapper_Found : Boolean := False;
665
666 begin
667 Prim_Elmt := First_Elmt (Primitive_Operations (Typ));
668 while Present (Prim_Elmt) loop
669 Prim := Node (Prim_Elmt);
670
671 if Is_Primitive_Wrapper (Prim)
672 and then Wrapped_Entity (Prim) = Subp_Entity
673 then
674 Wrapper_Found := True;
675 exit;
676 end if;
677
678 Next_Elmt (Prim_Elmt);
679 end loop;
680
681 -- A primitive declared between two views should have a
682 -- corresponding wrapper.
683
684 pragma Assert (Wrapper_Found);
685
686 -- Modify the call by setting the proper entity
687
688 Set_Entity (Name (N), Prim);
689 end;
690 end if;
691 end Check_Direct_Call;
692
693 -------------------------------
694 -- Check_Dispatching_Context --
695 -------------------------------
696
697 procedure Check_Dispatching_Context (Call : Node_Id) is
698 Subp : constant Entity_Id := Entity (Name (Call));
699
700 procedure Abstract_Context_Error;
701 -- Error for abstract call dispatching on result is not dispatching
702
703 function Has_Controlling_Current_Instance_Actual_In_DIC
704 (Call : Node_Id) return Boolean;
705 -- Return True if the subprogram call Call has a controlling actual
706 -- given directly by a current instance referenced within a DIC
707 -- aspect.
708
709 ----------------------------
710 -- Abstract_Context_Error --
711 ----------------------------
712
713 procedure Abstract_Context_Error is
714 begin
715 if Ekind (Subp) = E_Function then
716 Error_Msg_N
717 ("call to abstract function must be dispatching", N);
718
719 -- This error can occur for a procedure in the case of a call to
720 -- an abstract formal procedure with a statically tagged operand.
721
722 else
723 Error_Msg_N
724 ("call to abstract procedure must be dispatching", N);
725 end if;
726 end Abstract_Context_Error;
727
728 ----------------------------------------
729 -- Has_Current_Instance_Actual_In_DIC --
730 ----------------------------------------
731
732 function Has_Controlling_Current_Instance_Actual_In_DIC
733 (Call : Node_Id) return Boolean
734 is
735 A : Node_Id;
736 F : Entity_Id;
737 begin
738 F := First_Formal (Subp_Entity);
739 A := First_Actual (Call);
740
741 while Present (F) loop
742
743 -- Return True if the actual denotes a current instance (which
744 -- will be represented by an in-mode formal of the enclosing
745 -- DIC_Procedure) passed to a controlling formal. We don't have
746 -- to worry about controlling access formals here, because its
747 -- illegal to apply Access (etc.) attributes to a current
748 -- instance within an aspect (by AI12-0068).
749
750 if Is_Controlling_Formal (F)
751 and then Nkind (A) = N_Identifier
752 and then Ekind (Entity (A)) = E_In_Parameter
753 and then Is_Subprogram (Scope (Entity (A)))
754 and then Is_DIC_Procedure (Scope (Entity (A)))
755 then
756 return True;
757 end if;
758
759 Next_Formal (F);
760 Next_Actual (A);
761 end loop;
762
763 return False;
764 end Has_Controlling_Current_Instance_Actual_In_DIC;
765
766 -- Local variables
767
768 Scop : constant Entity_Id := Current_Scope_No_Loops;
769 Typ : constant Entity_Id := Etype (Subp);
770 Par : Node_Id;
771
772 -- Start of processing for Check_Dispatching_Context
773
774 begin
775 -- Skip checking context of dispatching calls during preanalysis of
776 -- class-wide conditions since at that stage the expression is not
777 -- installed yet on its definite context.
778
779 if Inside_Class_Condition_Preanalysis then
780 return;
781 end if;
782
783 -- If the called subprogram is a private overriding, replace it
784 -- with its alias, which has the correct body. Verify that the
785 -- two subprograms have the same controlling type (this is not the
786 -- case for an inherited subprogram that has become abstract).
787
788 if Is_Abstract_Subprogram (Subp)
789 and then No (Controlling_Argument (Call))
790 then
791 if Present (Alias (Subp))
792 and then not Is_Abstract_Subprogram (Alias (Subp))
793 and then No (DTC_Entity (Subp))
794 and then Find_Dispatching_Type (Subp) =
795 Find_Dispatching_Type (Alias (Subp))
796 then
797 -- Private overriding of inherited abstract operation, call is
798 -- legal.
799
800 Set_Entity (Name (N), Alias (Subp));
801 return;
802
803 -- If this is a pre/postcondition for an abstract subprogram,
804 -- it may call another abstract function that is a primitive
805 -- of an abstract type. The call is nondispatching but will be
806 -- legal in overridings of the operation. However, if the call
807 -- is tag-indeterminate we want to continue with with the error
808 -- checking below, as this case is illegal even for abstract
809 -- subprograms (see AI12-0170).
810
811 -- Similarly, as per AI12-0412, a nonabstract subprogram may
812 -- have a class-wide pre/postcondition that includes a call to
813 -- an abstract primitive of the subprogram's controlling type.
814 -- Certain operations (nondispatching calls, 'Access, use as
815 -- a generic actual) applied to such a nonabstract subprogram
816 -- are illegal in the case where the type is abstract (see
817 -- RM 6.1.1(18.2/5)).
818
819 elsif Is_Subprogram (Scop)
820 and then not Is_Tag_Indeterminate (N)
821 and then
822 -- The context is an internally built helper or an indirect
823 -- call wrapper that handles class-wide preconditions
824 (Present (Class_Preconditions_Subprogram (Scop))
825
826 -- ... or the context is a class-wide pre/postcondition.
827 or else
828 (In_Pre_Post_Condition (Call, Class_Wide_Only => True)
829
830 -- The tagged type associated with the called
831 -- subprogram must be the same as that of the
832 -- subprogram with a class-wide aspect.
833
834 and then Is_Dispatching_Operation (Scop)
835 and then Find_Dispatching_Type (Subp)
836 = Find_Dispatching_Type (Scop)))
837 then
838 null;
839
840 -- Similarly to the dispensation for postconditions, a call to
841 -- an abstract function within a Default_Initial_Condition aspect
842 -- can be legal when passed a current instance of the type. Such
843 -- a call will be effectively mapped to a call to a primitive of
844 -- a descendant type (see AI12-0397, as well as AI12-0170), so
845 -- doesn't need to be dispatching. We test for being within a DIC
846 -- procedure, since that's where the call will be analyzed.
847
848 elsif Is_Subprogram (Scop)
849 and then Is_DIC_Procedure (Scop)
850 and then Has_Controlling_Current_Instance_Actual_In_DIC (Call)
851 then
852 null;
853
854 elsif Ekind (Current_Scope) = E_Function
855 and then Nkind (Unit_Declaration_Node (Scop)) =
856 N_Generic_Subprogram_Declaration
857 then
858 null;
859
860 else
861 -- We need to determine whether the context of the call
862 -- provides a tag to make the call dispatching. This requires
863 -- the call to be the actual in an enclosing call, and that
864 -- actual must be controlling. If the call is an operand of
865 -- equality, the other operand must not be abstract.
866
867 if not Is_Tagged_Type (Typ)
868 and then not
869 (Ekind (Typ) = E_Anonymous_Access_Type
870 and then Is_Tagged_Type (Designated_Type (Typ)))
871 then
872 Abstract_Context_Error;
873 return;
874 end if;
875
876 Par := Parent (Call);
877
878 if Nkind (Par) = N_Parameter_Association then
879 Par := Parent (Par);
880 end if;
881
882 if Nkind (Par) = N_Qualified_Expression
883 or else Nkind (Par) = N_Unchecked_Type_Conversion
884 then
885 Par := Parent (Par);
886 end if;
887
888 if Nkind (Par) in N_Subprogram_Call
889 and then Is_Entity_Name (Name (Par))
890 then
891 declare
892 Enc_Subp : constant Entity_Id := Entity (Name (Par));
893 A : Node_Id;
894 F : Entity_Id;
895 Control : Entity_Id;
896 Ret_Type : Entity_Id;
897
898 begin
899 -- Find controlling formal that can provide tag for the
900 -- tag-indeterminate actual. The corresponding actual
901 -- must be the corresponding class-wide type.
902
903 F := First_Formal (Enc_Subp);
904 A := First_Actual (Par);
905
906 -- Find controlling type of call. Dereference if function
907 -- returns an access type.
908
909 Ret_Type := Etype (Call);
910 if Is_Access_Type (Etype (Call)) then
911 Ret_Type := Designated_Type (Ret_Type);
912 end if;
913
914 while Present (F) loop
915 Control := Etype (A);
916
917 if Is_Access_Type (Control) then
918 Control := Designated_Type (Control);
919 end if;
920
921 if Is_Controlling_Formal (F)
922 and then not (Call = A or else Parent (Call) = A)
923 and then Control = Class_Wide_Type (Ret_Type)
924 then
925 return;
926 end if;
927
928 Next_Formal (F);
929 Next_Actual (A);
930 end loop;
931
932 if Nkind (Par) = N_Function_Call
933 and then Is_Tag_Indeterminate (Par)
934 then
935 -- The parent may be an actual of an enclosing call
936
937 Check_Dispatching_Context (Par);
938 return;
939
940 else
941 Error_Msg_N
942 ("call to abstract function must be dispatching",
943 Call);
944 return;
945 end if;
946 end;
947
948 -- For equality operators, one of the operands must be
949 -- statically or dynamically tagged.
950
951 elsif Nkind (Par) in N_Op_Eq | N_Op_Ne then
952 if N = Right_Opnd (Par)
953 and then Is_Tag_Indeterminate (Left_Opnd (Par))
954 then
955 Abstract_Context_Error;
956
957 elsif N = Left_Opnd (Par)
958 and then Is_Tag_Indeterminate (Right_Opnd (Par))
959 then
960 Abstract_Context_Error;
961 end if;
962
963 return;
964
965 -- The left-hand side of an assignment provides the tag
966
967 elsif Nkind (Par) = N_Assignment_Statement then
968 return;
969
970 else
971 Abstract_Context_Error;
972 end if;
973 end if;
974 end if;
975 end Check_Dispatching_Context;
976
977 -- Start of processing for Check_Dispatching_Call
978
979 begin
980 -- Find a controlling argument, if any
981
982 if Present (Parameter_Associations (N)) then
983 Subp_Entity := Entity (Name (N));
984
985 Actual := First_Actual (N);
986 Formal := First_Formal (Subp_Entity);
987 while Present (Actual) loop
988 Control := Find_Controlling_Arg (Actual);
989 exit when Present (Control);
990
991 -- Check for the case where the actual is a tag-indeterminate call
992 -- whose result type is different than the tagged type associated
993 -- with the containing call, but is an ancestor of the type.
994
995 if Is_Controlling_Formal (Formal)
996 and then Is_Tag_Indeterminate (Actual)
997 and then Base_Type (Etype (Actual)) /= Base_Type (Etype (Formal))
998 and then Is_Ancestor (Etype (Actual), Etype (Formal))
999 then
1000 Indeterm_Ctrl_Type := Etype (Formal);
1001
1002 -- If the formal is controlling but the actual is not, the type
1003 -- of the actual is statically known, and may be used as the
1004 -- controlling tag for some other tag-indeterminate actual.
1005
1006 elsif Is_Controlling_Formal (Formal)
1007 and then Is_Entity_Name (Actual)
1008 and then Is_Tagged_Type (Etype (Actual))
1009 then
1010 Static_Tag := Etype (Actual);
1011 end if;
1012
1013 Next_Actual (Actual);
1014 Next_Formal (Formal);
1015 end loop;
1016
1017 if Present (Control) then
1018
1019 -- Verify that no controlling arguments are statically tagged
1020
1021 if Debug_Flag_E then
1022 Write_Str ("Found Dispatching call");
1023 Write_Int (Int (N));
1024 Write_Eol;
1025 end if;
1026
1027 Actual := First_Actual (N);
1028 while Present (Actual) loop
1029 if Actual /= Control then
1030
1031 if not Is_Controlling_Actual (Actual) then
1032 null; -- Can be anything
1033
1034 elsif Is_Dynamically_Tagged (Actual) then
1035 null; -- Valid parameter
1036
1037 elsif Is_Tag_Indeterminate (Actual) then
1038
1039 -- The tag is inherited from the enclosing call (the node
1040 -- we are currently analyzing). Explicitly expand the
1041 -- actual, since the previous call to Expand (from
1042 -- Resolve_Call) had no way of knowing about the
1043 -- required dispatching.
1044
1045 Propagate_Tag (Control, Actual);
1046
1047 else
1048 Error_Msg_N
1049 ("controlling argument is not dynamically tagged",
1050 Actual);
1051 return;
1052 end if;
1053 end if;
1054
1055 Next_Actual (Actual);
1056 end loop;
1057
1058 -- Mark call as a dispatching call
1059
1060 Set_Controlling_Argument (N, Control);
1061 Check_Restriction (No_Dispatching_Calls, N);
1062
1063 -- The dispatching call may need to be converted into a direct
1064 -- call in certain cases.
1065
1066 Check_Direct_Call;
1067
1068 -- If the call doesn't have a controlling actual but does have an
1069 -- indeterminate actual that requires dispatching treatment, then an
1070 -- object is needed that will serve as the controlling argument for
1071 -- a dispatching call on the indeterminate actual. This can occur
1072 -- in the unusual situation of a default actual given by a tag-
1073 -- indeterminate call and where the type of the call is an ancestor
1074 -- of the type associated with a containing call to an inherited
1075 -- operation (see AI-239).
1076
1077 -- Rather than create an object of the tagged type, which would
1078 -- be problematic for various reasons (default initialization,
1079 -- discriminants), the tag of the containing call's associated
1080 -- tagged type is directly used to control the dispatching.
1081
1082 elsif Present (Indeterm_Ctrl_Type) then
1083 if Present (Static_Tag) then
1084 Control :=
1085 Make_Attribute_Reference (Loc,
1086 Prefix =>
1087 New_Occurrence_Of (Static_Tag, Loc),
1088 Attribute_Name => Name_Tag);
1089
1090 else
1091 Control :=
1092 Make_Attribute_Reference (Loc,
1093 Prefix =>
1094 New_Occurrence_Of (Indeterm_Ctrl_Type, Loc),
1095 Attribute_Name => Name_Tag);
1096 end if;
1097
1098 Analyze (Control);
1099
1100 Actual := First_Actual (N);
1101 Formal := First_Formal (Subp_Entity);
1102 while Present (Actual) loop
1103 if Is_Tag_Indeterminate (Actual)
1104 and then Is_Controlling_Formal (Formal)
1105 then
1106 Propagate_Tag (Control, Actual);
1107 end if;
1108
1109 Next_Actual (Actual);
1110 Next_Formal (Formal);
1111 end loop;
1112
1113 Check_Dispatching_Context (N);
1114
1115 elsif Nkind (N) /= N_Function_Call then
1116
1117 -- The call is not dispatching, so check that there aren't any
1118 -- tag-indeterminate abstract calls left among its actuals.
1119
1120 Actual := First_Actual (N);
1121 while Present (Actual) loop
1122 if Is_Tag_Indeterminate (Actual) then
1123
1124 -- Function call case
1125
1126 if Nkind (Original_Node (Actual)) = N_Function_Call then
1127 Func := Entity (Name (Original_Node (Actual)));
1128
1129 -- If the actual is an attribute then it can't be abstract
1130 -- (the only current case of a tag-indeterminate attribute
1131 -- is the stream Input attribute).
1132
1133 elsif Nkind (Original_Node (Actual)) = N_Attribute_Reference
1134 then
1135 Func := Empty;
1136
1137 -- Ditto if it is an explicit dereference
1138
1139 elsif Nkind (Original_Node (Actual)) = N_Explicit_Dereference
1140 then
1141 Func := Empty;
1142
1143 -- Only other possibility is a qualified expression whose
1144 -- constituent expression is itself a call.
1145
1146 else
1147 Func :=
1148 Entity (Name (Original_Node
1149 (Expression (Original_Node (Actual)))));
1150 end if;
1151
1152 if Present (Func) and then Is_Abstract_Subprogram (Func) then
1153 Error_Msg_N
1154 ("call to abstract function must be dispatching",
1155 Actual);
1156 end if;
1157 end if;
1158
1159 Next_Actual (Actual);
1160 end loop;
1161
1162 Check_Dispatching_Context (N);
1163
1164 elsif Nkind (Parent (N)) in N_Subexpr then
1165 Check_Dispatching_Context (N);
1166
1167 elsif Nkind (Parent (N)) = N_Assignment_Statement
1168 and then Is_Class_Wide_Type (Etype (Name (Parent (N))))
1169 then
1170 return;
1171
1172 elsif Is_Abstract_Subprogram (Subp_Entity) then
1173 Check_Dispatching_Context (N);
1174 return;
1175 end if;
1176
1177 -- If this is a nondispatching call to a nonabstract subprogram
1178 -- and the subprogram has any Pre'Class or Post'Class aspects with
1179 -- nonstatic values, then report an error. This is specified by
1180 -- RM 6.1.1(18.2/5) (by AI12-0412).
1181
1182 -- Skip reporting this error on helpers and indirect-call wrappers
1183 -- built to support class-wide preconditions.
1184
1185 if No (Control)
1186 and then not Is_Abstract_Subprogram (Subp_Entity)
1187 and then
1188 Is_Prim_Of_Abst_Type_With_Nonstatic_CW_Pre_Post (Subp_Entity)
1189 and then not
1190 (Is_Subprogram (Current_Scope)
1191 and then
1192 Present (Class_Preconditions_Subprogram (Current_Scope)))
1193 then
1194 Error_Msg_N
1195 ("nondispatching call to nonabstract subprogram of "
1196 & "abstract type with nonstatic class-wide "
1197 & "pre/postconditions",
1198 N);
1199 end if;
1200
1201 else
1202 -- If dispatching on result, the enclosing call, if any, will
1203 -- determine the controlling argument. Otherwise this is the
1204 -- primitive operation of the root type.
1205
1206 Check_Dispatching_Context (N);
1207 end if;
1208 end Check_Dispatching_Call;
1209
1210 ---------------------------------
1211 -- Check_Dispatching_Operation --
1212 ---------------------------------
1213
1214 procedure Check_Dispatching_Operation (Subp, Old_Subp : Entity_Id) is
1215 function Is_Access_To_Subprogram_Wrapper (E : Entity_Id) return Boolean;
1216 -- Return True if E is an access to subprogram wrapper
1217
1218 procedure Warn_On_Late_Primitive_After_Private_Extension
1219 (Typ : Entity_Id;
1220 Prim : Entity_Id);
1221 -- Prim is a dispatching primitive of the tagged type Typ. Warn on Prim
1222 -- if it is a public primitive defined after some private extension of
1223 -- the tagged type.
1224
1225 -------------------------------------
1226 -- Is_Access_To_Subprogram_Wrapper --
1227 -------------------------------------
1228
1229 function Is_Access_To_Subprogram_Wrapper (E : Entity_Id) return Boolean
1230 is
1231 Decl_N : constant Node_Id := Unit_Declaration_Node (E);
1232 Par_N : constant Node_Id := Parent (List_Containing (Decl_N));
1233
1234 begin
1235 -- Access to subprogram wrappers are declared in the freezing actions
1236
1237 return Nkind (Par_N) = N_Freeze_Entity
1238 and then Ekind (Entity (Par_N)) = E_Access_Subprogram_Type;
1239 end Is_Access_To_Subprogram_Wrapper;
1240
1241 ----------------------------------------------------
1242 -- Warn_On_Late_Primitive_After_Private_Extension --
1243 ----------------------------------------------------
1244
1245 procedure Warn_On_Late_Primitive_After_Private_Extension
1246 (Typ : Entity_Id;
1247 Prim : Entity_Id)
1248 is
1249 E : Entity_Id;
1250
1251 begin
1252 if Warn_On_Late_Primitives
1253 and then Comes_From_Source (Prim)
1254 and then Has_Private_Extension (Typ)
1255 and then Is_Package_Or_Generic_Package (Current_Scope)
1256 and then not In_Private_Part (Current_Scope)
1257 then
1258 E := Next_Entity (Typ);
1259
1260 while E /= Prim loop
1261 if Ekind (E) = E_Record_Type_With_Private
1262 and then Etype (E) = Typ
1263 then
1264 Error_Msg_Name_1 := Chars (Typ);
1265 Error_Msg_Name_2 := Chars (E);
1266 Error_Msg_Sloc := Sloc (E);
1267 Error_Msg_N
1268 ("?.j?primitive of type % defined after private extension "
1269 & "% #?", Prim);
1270 Error_Msg_Name_1 := Chars (Prim);
1271 Error_Msg_Name_2 := Chars (E);
1272 Error_Msg_N
1273 ("\spec of % should appear before declaration of type %!",
1274 Prim);
1275 exit;
1276 end if;
1277
1278 Next_Entity (E);
1279 end loop;
1280 end if;
1281 end Warn_On_Late_Primitive_After_Private_Extension;
1282
1283 -- Local variables
1284
1285 Body_Is_Last_Primitive : Boolean := False;
1286 Has_Dispatching_Parent : Boolean := False;
1287 Ovr_Subp : Entity_Id := Empty;
1288 Tagged_Type : Entity_Id;
1289
1290 -- Start of processing for Check_Dispatching_Operation
1291
1292 begin
1293 if Ekind (Subp) not in E_Function | E_Procedure then
1294 return;
1295
1296 -- The Default_Initial_Condition procedure is not a primitive subprogram
1297 -- even if it relates to a tagged type. This routine is not meant to be
1298 -- inherited or overridden.
1299
1300 elsif Is_DIC_Procedure (Subp) then
1301 return;
1302
1303 -- The "partial" and "full" type invariant procedures are not primitive
1304 -- subprograms even if they relate to a tagged type. These routines are
1305 -- not meant to be inherited or overridden.
1306
1307 elsif Is_Invariant_Procedure (Subp)
1308 or else Is_Partial_Invariant_Procedure (Subp)
1309 then
1310 return;
1311
1312 -- Wrappers of access to subprograms are not primitive subprograms.
1313
1314 elsif Is_Wrapper (Subp)
1315 and then Is_Access_To_Subprogram_Wrapper (Subp)
1316 then
1317 return;
1318 end if;
1319
1320 Set_Is_Dispatching_Operation (Subp, False);
1321 Tagged_Type := Find_Dispatching_Type (Subp);
1322
1323 -- Ada 2005 (AI-345): Use the corresponding record (if available).
1324 -- Required because primitives of concurrent types are attached
1325 -- to the corresponding record (not to the concurrent type).
1326
1327 if Ada_Version >= Ada_2005
1328 and then Present (Tagged_Type)
1329 and then Is_Concurrent_Type (Tagged_Type)
1330 and then Present (Corresponding_Record_Type (Tagged_Type))
1331 then
1332 Tagged_Type := Corresponding_Record_Type (Tagged_Type);
1333 end if;
1334
1335 -- (AI-345): The task body procedure is not a primitive of the tagged
1336 -- type
1337
1338 if Present (Tagged_Type)
1339 and then Is_Concurrent_Record_Type (Tagged_Type)
1340 and then Present (Corresponding_Concurrent_Type (Tagged_Type))
1341 and then Is_Task_Type (Corresponding_Concurrent_Type (Tagged_Type))
1342 and then Subp = Get_Task_Body_Procedure
1343 (Corresponding_Concurrent_Type (Tagged_Type))
1344 then
1345 return;
1346 end if;
1347
1348 -- If Subp is derived from a dispatching operation then it should
1349 -- always be treated as dispatching. In this case various checks
1350 -- below will be bypassed. Makes sure that late declarations for
1351 -- inherited private subprograms are treated as dispatching, even
1352 -- if the associated tagged type is already frozen.
1353
1354 Has_Dispatching_Parent :=
1355 Present (Alias (Subp))
1356 and then Is_Dispatching_Operation (Alias (Subp));
1357
1358 if No (Tagged_Type) then
1359
1360 -- Ada 2005 (AI-251): Check that Subp is not a primitive associated
1361 -- with an abstract interface type unless the interface acts as a
1362 -- parent type in a derivation. If the interface type is a formal
1363 -- type then the operation is not primitive and therefore legal.
1364
1365 declare
1366 E : Entity_Id;
1367 Typ : Entity_Id;
1368
1369 begin
1370 E := First_Entity (Subp);
1371 while Present (E) loop
1372
1373 -- For an access parameter, check designated type
1374
1375 if Ekind (Etype (E)) = E_Anonymous_Access_Type then
1376 Typ := Designated_Type (Etype (E));
1377 else
1378 Typ := Etype (E);
1379 end if;
1380
1381 if Comes_From_Source (Subp)
1382 and then Is_Interface (Typ)
1383 and then not Is_Class_Wide_Type (Typ)
1384 and then not Is_Derived_Type (Typ)
1385 and then not Is_Generic_Type (Typ)
1386 and then not In_Instance
1387 then
1388 Error_Msg_N ("??declaration of& is too late!", Subp);
1389 Error_Msg_NE -- CODEFIX??
1390 ("\??spec should appear immediately after declaration of "
1391 & "& !", Subp, Typ);
1392 exit;
1393 end if;
1394
1395 Next_Entity (E);
1396 end loop;
1397
1398 -- In case of functions check also the result type
1399
1400 if Ekind (Subp) = E_Function then
1401 if Is_Access_Type (Etype (Subp)) then
1402 Typ := Designated_Type (Etype (Subp));
1403 else
1404 Typ := Etype (Subp);
1405 end if;
1406
1407 -- Report warning on non dispatching primitives of interface
1408 -- type Typ; this warning is disabled when the type has the
1409 -- aspect First_Controlling_Parameter because we will report
1410 -- an error when the interface type is frozen.
1411
1412 if Comes_From_Source (Subp)
1413 and then Is_Interface (Typ)
1414 and then not Is_Class_Wide_Type (Typ)
1415 and then not Is_Derived_Type (Typ)
1416 and then not Is_Generic_Type (Typ)
1417 and then not In_Instance
1418 and then not Has_First_Controlling_Parameter_Aspect (Typ)
1419 then
1420 Error_Msg_N ("??declaration of& is too late!", Subp);
1421 Error_Msg_NE
1422 ("\??spec should appear immediately after declaration of "
1423 & "& !", Subp, Typ);
1424 end if;
1425 end if;
1426 end;
1427
1428 return;
1429
1430 -- The subprograms build internally after the freezing point (such as
1431 -- init procs, interface thunks, type support subprograms, and Offset
1432 -- to top functions for accessing interface components in variable
1433 -- size tagged types) are not primitives.
1434
1435 elsif Is_Frozen (Tagged_Type)
1436 and then not Comes_From_Source (Subp)
1437 and then not Has_Dispatching_Parent
1438 then
1439 -- Complete decoration of internally built subprograms that override
1440 -- a dispatching primitive. These entities correspond with the
1441 -- following cases:
1442
1443 -- 1. Ada 2005 (AI-391): Wrapper functions built by the expander
1444 -- to override functions of nonabstract null extensions. These
1445 -- primitives were added to the list of primitives of the tagged
1446 -- type by Make_Controlling_Function_Wrappers. However, attribute
1447 -- Is_Dispatching_Operation must be set to true.
1448
1449 -- 2. Ada 2005 (AI-251): Wrapper procedures of null interface
1450 -- primitives.
1451
1452 -- 3. Subprograms associated with stream attributes (built by
1453 -- New_Stream_Subprogram) or with the Put_Image attribute.
1454
1455 -- 4. Wrappers built for inherited operations. We have two kinds:
1456 -- * Wrappers built for inherited operations with inherited class-
1457 -- wide conditions, where the conditions include calls to other
1458 -- overridden primitives. The wrappers include checks on these
1459 -- modified conditions (AI12-195).
1460 -- * Wrappers built for inherited operations that implement
1461 -- interface primitives that have class-wide postconditions.
1462
1463 -- 5. Declarations built for subprograms without separate specs that
1464 -- are eligible for inlining in GNATprove (inside
1465 -- Sem_Ch6.Analyze_Subprogram_Body_Helper).
1466
1467 if Present (Old_Subp)
1468 and then Present (Overridden_Operation (Subp))
1469 and then Is_Dispatching_Operation (Old_Subp)
1470 then
1471 pragma Assert
1472 ((Ekind (Subp) = E_Function
1473 and then Is_Dispatching_Operation (Old_Subp)
1474 and then not Is_Class_Wide_Type (Etype (Subp))
1475 and then Is_Null_Extension (Base_Type (Etype (Subp))))
1476
1477 or else
1478 (Ekind (Subp) = E_Function
1479 and then Is_Dispatching_Operation (Old_Subp)
1480 and then Is_Class_Wide_Type (Etype (Subp))
1481 and then Is_Null_Extension (Root_Type (Etype (Subp))))
1482
1483 or else
1484 (Ekind (Subp) = E_Procedure
1485 and then Is_Dispatching_Operation (Old_Subp)
1486 and then Present (Alias (Old_Subp))
1487 and then Is_Null_Interface_Primitive
1488 (Ultimate_Alias (Old_Subp)))
1489
1490 or else Get_TSS_Name (Subp) in TSS_Stream_Read
1491 | TSS_Stream_Write
1492 | TSS_Put_Image
1493
1494 or else
1495 (Is_Wrapper (Subp)
1496 and then Is_Dispatch_Table_Wrapper (Subp))
1497
1498 or else GNATprove_Mode);
1499
1500 Check_Controlling_Formals (Tagged_Type, Subp);
1501 Override_Dispatching_Operation (Tagged_Type, Old_Subp, Subp);
1502 Set_Is_Dispatching_Operation (Subp);
1503 end if;
1504
1505 return;
1506
1507 -- The operation may be a child unit, whose scope is the defining
1508 -- package, but which is not a primitive operation of the type.
1509
1510 elsif Is_Child_Unit (Subp) then
1511 return;
1512
1513 -- If the subprogram is not defined in a package spec, the only case
1514 -- where it can be a dispatching op is when it overrides an operation
1515 -- before the freezing point of the type.
1516
1517 elsif (not Is_Package_Or_Generic_Package (Scope (Subp))
1518 or else In_Package_Body (Scope (Subp)))
1519 and then not Has_Dispatching_Parent
1520 then
1521 if not Comes_From_Source (Subp)
1522 or else (Present (Old_Subp) and then not Is_Frozen (Tagged_Type))
1523 then
1524 null;
1525
1526 -- If the type is already frozen, the overriding is not allowed
1527 -- except when Old_Subp is not a dispatching operation (which can
1528 -- occur when Old_Subp was inherited by an untagged type). However,
1529 -- a body with no previous spec freezes the type *after* its
1530 -- declaration, and therefore is a legal overriding (unless the type
1531 -- has already been frozen). Only the first such body is legal.
1532
1533 elsif Present (Old_Subp)
1534 and then Is_Dispatching_Operation (Old_Subp)
1535 then
1536 if Comes_From_Source (Subp)
1537 and then
1538 (Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Body
1539 or else Nkind (Unit_Declaration_Node (Subp)) in N_Body_Stub)
1540 then
1541 declare
1542 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1543 Decl_Item : Node_Id;
1544
1545 begin
1546 -- ??? The checks here for whether the type has been frozen
1547 -- prior to the new body are not complete. It's not simple
1548 -- to check frozenness at this point since the body has
1549 -- already caused the type to be prematurely frozen in
1550 -- Analyze_Declarations, but we're forced to recheck this
1551 -- here because of the odd rule interpretation that allows
1552 -- the overriding if the type wasn't frozen prior to the
1553 -- body. The freezing action should probably be delayed
1554 -- until after the spec is seen, but that's a tricky
1555 -- change to the delicate freezing code.
1556
1557 -- Look at each declaration following the type up until the
1558 -- new subprogram body. If any of the declarations is a body
1559 -- then the type has been frozen already so the overriding
1560 -- primitive is illegal.
1561
1562 Decl_Item := Next (Parent (Tagged_Type));
1563 while Present (Decl_Item)
1564 and then Decl_Item /= Subp_Body
1565 loop
1566 if Comes_From_Source (Decl_Item)
1567 and then (Nkind (Decl_Item) in N_Proper_Body
1568 or else Nkind (Decl_Item) in N_Body_Stub)
1569 then
1570 Error_Msg_N ("overriding of& is too late!", Subp);
1571 Error_Msg_N
1572 ("\spec should appear immediately after the type!",
1573 Subp);
1574 exit;
1575 end if;
1576
1577 Next (Decl_Item);
1578 end loop;
1579
1580 -- If the subprogram doesn't follow in the list of
1581 -- declarations including the type then the type has
1582 -- definitely been frozen already and the body is illegal.
1583
1584 if No (Decl_Item) then
1585 Error_Msg_N ("overriding of& is too late!", Subp);
1586 Error_Msg_N
1587 ("\spec should appear immediately after the type!",
1588 Subp);
1589
1590 else
1591 -- The subprogram body declares a primitive operation.
1592 -- We must update its dispatching information here. The
1593 -- information is taken from the overridden subprogram.
1594 -- Such a late-overriding body also needs extra formals.
1595 -- We must also generate a cross-reference entry because
1596 -- references to other primitives were already created
1597 -- when type was frozen.
1598
1599 Body_Is_Last_Primitive := True;
1600
1601 if Present (DTC_Entity (Old_Subp)) then
1602 Set_DTC_Entity (Subp, DTC_Entity (Old_Subp));
1603 Set_DT_Position_Value (Subp, DT_Position (Old_Subp));
1604 Create_Extra_Formals (Subp);
1605
1606 if not Restriction_Active (No_Dispatching_Calls) then
1607 if Building_Static_DT (Tagged_Type) then
1608
1609 -- If the static dispatch table has not been
1610 -- built then there is nothing else to do now;
1611 -- otherwise we notify that we cannot build the
1612 -- static dispatch table.
1613
1614 if Has_Dispatch_Table (Tagged_Type) then
1615 Error_Msg_N
1616 ("overriding of& is too late for building "
1617 & " static dispatch tables!", Subp);
1618 Error_Msg_N
1619 ("\spec should appear immediately after "
1620 & "the type!", Subp);
1621 end if;
1622
1623 -- No code required to register primitives in VM
1624 -- targets
1625
1626 elsif not Tagged_Type_Expansion then
1627 null;
1628
1629 else
1630 Insert_Actions_After (Subp_Body,
1631 Register_Primitive (Sloc (Subp_Body),
1632 Prim => Subp));
1633 end if;
1634
1635 -- Indicate that this is an overriding operation,
1636 -- and replace the overridden entry in the list of
1637 -- primitive operations, which is used for xref
1638 -- generation subsequently.
1639
1640 Generate_Reference (Tagged_Type, Subp, 'P', False);
1641 Override_Dispatching_Operation
1642 (Tagged_Type, Old_Subp, Subp);
1643 Set_Is_Dispatching_Operation (Subp);
1644
1645 -- Inherit decoration of controlling formals and
1646 -- controlling result.
1647
1648 if Ekind (Old_Subp) = E_Function
1649 and then Has_Controlling_Result (Old_Subp)
1650 then
1651 Set_Has_Controlling_Result (Subp);
1652 end if;
1653
1654 if Present (First_Formal (Old_Subp)) then
1655 declare
1656 Old_Formal : Entity_Id;
1657 Formal : Entity_Id;
1658
1659 begin
1660 Formal := First_Formal (Subp);
1661 Old_Formal := First_Formal (Old_Subp);
1662
1663 while Present (Old_Formal) loop
1664 Set_Is_Controlling_Formal (Formal,
1665 Is_Controlling_Formal (Old_Formal));
1666
1667 Next_Formal (Formal);
1668 Next_Formal (Old_Formal);
1669 end loop;
1670 end;
1671 end if;
1672 end if;
1673
1674 Check_Inherited_Conditions (Tagged_Type,
1675 Late_Overriding => True);
1676 end if;
1677 end if;
1678 end;
1679
1680 else
1681 Error_Msg_N ("overriding of& is too late!", Subp);
1682 Error_Msg_N
1683 ("\subprogram spec should appear immediately after the type!",
1684 Subp);
1685 end if;
1686
1687 -- If the type is not frozen yet and we are not in the overriding
1688 -- case it looks suspiciously like an attempt to define a primitive
1689 -- operation, which requires the declaration to be in a package spec
1690 -- (3.2.3(6)). Only report cases where the type and subprogram are
1691 -- in the same declaration list (by checking the enclosing parent
1692 -- declarations), to avoid spurious warnings on subprograms in
1693 -- instance bodies when the type is declared in the instance spec
1694 -- but hasn't been frozen by the instance body.
1695
1696 elsif not Is_Frozen (Tagged_Type)
1697 and then In_Same_List (Parent (Tagged_Type), Parent (Parent (Subp)))
1698 then
1699 Error_Msg_N
1700 ("??not dispatching (must be defined in a package spec)", Subp);
1701 return;
1702
1703 -- When the type is frozen, it is legitimate to define a new
1704 -- non-primitive operation.
1705
1706 else
1707 return;
1708 end if;
1709
1710 -- Now, we are sure that the scope is a package spec. If the subprogram
1711 -- is declared after the freezing point of the type that's an error
1712
1713 elsif Is_Frozen (Tagged_Type) and then not Has_Dispatching_Parent then
1714 Error_Msg_N ("this primitive operation is declared too late", Subp);
1715 Error_Msg_NE
1716 ("??no primitive operations for& after this line",
1717 Freeze_Node (Tagged_Type),
1718 Tagged_Type);
1719 return;
1720 end if;
1721
1722 Check_Controlling_Formals (Tagged_Type, Subp);
1723
1724 Ovr_Subp := Old_Subp;
1725
1726 -- [Ada 2012:AI-0125]: Search for inherited hidden primitive that may be
1727 -- overridden by Subp. This only applies to source subprograms, and
1728 -- their declaration must carry an explicit overriding indicator.
1729
1730 if No (Ovr_Subp)
1731 and then Ada_Version >= Ada_2012
1732 and then Comes_From_Source (Subp)
1733 and then
1734 Nkind (Unit_Declaration_Node (Subp)) = N_Subprogram_Declaration
1735 then
1736 Ovr_Subp := Find_Hidden_Overridden_Primitive (Subp);
1737
1738 -- Warn if the proper overriding indicator has not been supplied.
1739
1740 if Present (Ovr_Subp)
1741 and then
1742 not Must_Override (Specification (Unit_Declaration_Node (Subp)))
1743 and then not In_Instance
1744 then
1745 Error_Msg_NE ("missing overriding indicator for&??", Subp, Subp);
1746 end if;
1747 end if;
1748
1749 -- Now it should be a correct primitive operation, put it in the list
1750
1751 if Present (Ovr_Subp) then
1752
1753 -- If the type has interfaces we complete this check after we set
1754 -- attribute Is_Dispatching_Operation.
1755
1756 Check_Subtype_Conformant (Subp, Ovr_Subp);
1757
1758 -- A primitive operation with the name of a primitive controlled
1759 -- operation does not override a non-visible overriding controlled
1760 -- operation, i.e. one declared in a private part when the full
1761 -- view of a type is controlled. Conversely, it will override a
1762 -- visible operation that may be declared in a partial view when
1763 -- the full view is controlled.
1764
1765 if Chars (Subp) in Name_Initialize | Name_Adjust | Name_Finalize
1766 and then Is_Controlled (Tagged_Type)
1767 and then not Is_Visibly_Controlled (Tagged_Type)
1768 and then not Is_Inherited_Public_Operation (Ovr_Subp)
1769 then
1770 Set_Overridden_Operation (Subp, Empty);
1771
1772 -- If the subprogram specification carries an overriding
1773 -- indicator, no need for the warning: it is either redundant,
1774 -- or else an error will be reported.
1775
1776 if Nkind (Parent (Subp)) = N_Procedure_Specification
1777 and then
1778 (Must_Override (Parent (Subp))
1779 or else Must_Not_Override (Parent (Subp)))
1780 then
1781 null;
1782
1783 -- Here we need the warning
1784
1785 else
1786 Error_Msg_NE
1787 ("operation does not override inherited&??", Subp, Subp);
1788 end if;
1789
1790 else
1791 Override_Dispatching_Operation (Tagged_Type, Ovr_Subp, Subp);
1792
1793 -- Ada 2005 (AI-251): In case of late overriding of a primitive
1794 -- that covers abstract interface subprograms we must register it
1795 -- in all the secondary dispatch tables associated with abstract
1796 -- interfaces. We do this now only if not building static tables,
1797 -- nor when the expander is inactive (we avoid trying to register
1798 -- primitives in semantics-only mode, since the type may not have
1799 -- an associated dispatch table). Otherwise the patch code is
1800 -- emitted after those tables are built, to prevent access before
1801 -- elaboration in gigi.
1802
1803 if Body_Is_Last_Primitive
1804 and then not Building_Static_DT (Tagged_Type)
1805 and then Expander_Active
1806 and then Tagged_Type_Expansion
1807 then
1808 declare
1809 Subp_Body : constant Node_Id := Unit_Declaration_Node (Subp);
1810 Elmt : Elmt_Id;
1811 Prim : Node_Id;
1812
1813 begin
1814 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
1815 while Present (Elmt) loop
1816 Prim := Node (Elmt);
1817
1818 if Present (Alias (Prim))
1819 and then Present (Interface_Alias (Prim))
1820 and then Alias (Prim) = Subp
1821 then
1822 Insert_Actions_After (Subp_Body,
1823 Register_Primitive (Sloc (Subp_Body), Prim => Prim));
1824 end if;
1825
1826 Next_Elmt (Elmt);
1827 end loop;
1828
1829 -- Redisplay the contents of the updated dispatch table
1830
1831 if Debug_Flag_ZZ then
1832 Write_Str ("Late overriding: ");
1833 Write_DT (Tagged_Type);
1834 end if;
1835 end;
1836 end if;
1837 end if;
1838
1839 -- If no old subprogram, then we add this as a dispatching operation,
1840 -- but we avoid doing this if an error was posted, to prevent annoying
1841 -- cascaded errors.
1842
1843 elsif not Error_Posted (Subp) then
1844
1845 -- When aspect First_Controlling_Parameter applies, check if the
1846 -- subprogram is a primitive. Internal subprograms added by the
1847 -- frontend bypass its restrictions.
1848
1849 if Has_First_Controlling_Parameter_Aspect (Tagged_Type)
1850 and then not Is_Internal (Subp)
1851 and then not
1852 (Present (Overridden_Operation (Subp))
1853 and then
1854 Is_Dispatching_Operation (Overridden_Operation (Subp)))
1855 and then not
1856 (Present (Alias (Subp))
1857 and then
1858 Is_Dispatching_Operation (Ultimate_Alias (Subp)))
1859 and then (No (First_Formal (Subp))
1860 or else not
1861 Is_Controlling_Formal (First_Formal (Subp)))
1862 then
1863 if Warn_On_Non_Dispatching_Primitives then
1864 Error_Msg_NE
1865 ("?_j?not a dispatching primitive of tagged type&",
1866 Subp, Tagged_Type);
1867 Error_Msg_NE
1868 ("\?_j?disallowed by 'First_'Controlling_'Parameter on &",
1869 Subp, Tagged_Type);
1870 end if;
1871
1872 return;
1873 end if;
1874
1875 Add_Dispatching_Operation (Tagged_Type, Subp);
1876 end if;
1877
1878 Set_Is_Dispatching_Operation (Subp, True);
1879
1880 -- Ada 2005 (AI-251): If the type implements interfaces we must check
1881 -- subtype conformance against all the interfaces covered by this
1882 -- primitive.
1883
1884 if Present (Ovr_Subp)
1885 and then Has_Interfaces (Tagged_Type)
1886 then
1887 declare
1888 Ifaces_List : Elist_Id;
1889 Iface_Elmt : Elmt_Id;
1890 Iface_Prim_Elmt : Elmt_Id;
1891 Iface_Prim : Entity_Id;
1892 Ret_Typ : Entity_Id;
1893
1894 begin
1895 Collect_Interfaces (Tagged_Type, Ifaces_List);
1896
1897 Iface_Elmt := First_Elmt (Ifaces_List);
1898 while Present (Iface_Elmt) loop
1899 if not Is_Ancestor (Node (Iface_Elmt), Tagged_Type) then
1900 Iface_Prim_Elmt :=
1901 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
1902 while Present (Iface_Prim_Elmt) loop
1903 Iface_Prim := Node (Iface_Prim_Elmt);
1904
1905 if Is_Interface_Conformant
1906 (Tagged_Type, Iface_Prim, Subp)
1907 then
1908 -- Handle procedures, functions whose return type
1909 -- matches, or functions not returning interfaces
1910
1911 if Ekind (Subp) = E_Procedure
1912 or else Etype (Iface_Prim) = Etype (Subp)
1913 or else not Is_Interface (Etype (Iface_Prim))
1914 then
1915 Check_Subtype_Conformant
1916 (New_Id => Subp,
1917 Old_Id => Iface_Prim,
1918 Err_Loc => Subp,
1919 Skip_Controlling_Formals => True);
1920
1921 -- Handle functions returning interfaces
1922
1923 elsif Implements_Interface
1924 (Etype (Subp), Etype (Iface_Prim))
1925 then
1926 -- Temporarily force both entities to return the
1927 -- same type. Required because Subtype_Conformant
1928 -- does not handle this case.
1929
1930 Ret_Typ := Etype (Iface_Prim);
1931 Set_Etype (Iface_Prim, Etype (Subp));
1932
1933 Check_Subtype_Conformant
1934 (New_Id => Subp,
1935 Old_Id => Iface_Prim,
1936 Err_Loc => Subp,
1937 Skip_Controlling_Formals => True);
1938
1939 Set_Etype (Iface_Prim, Ret_Typ);
1940 end if;
1941 end if;
1942
1943 Next_Elmt (Iface_Prim_Elmt);
1944 end loop;
1945 end if;
1946
1947 Next_Elmt (Iface_Elmt);
1948 end loop;
1949 end;
1950 end if;
1951
1952 if not Body_Is_Last_Primitive then
1953 Set_DT_Position_Value (Subp, No_Uint);
1954
1955 elsif Has_Controlled_Component (Tagged_Type)
1956 and then Chars (Subp) in Name_Initialize
1957 | Name_Adjust
1958 | Name_Finalize
1959 | Name_Finalize_Address
1960 then
1961 declare
1962 F_Node : constant Node_Id := Freeze_Node (Tagged_Type);
1963 Decl : Node_Id;
1964 Old_P : Entity_Id;
1965 Old_Bod : Node_Id;
1966 Old_Spec : Entity_Id;
1967
1968 C_Names : constant array (1 .. 4) of Name_Id :=
1969 (Name_Initialize,
1970 Name_Adjust,
1971 Name_Finalize,
1972 Name_Finalize_Address);
1973
1974 D_Names : constant array (1 .. 4) of TSS_Name_Type :=
1975 (TSS_Deep_Initialize,
1976 TSS_Deep_Adjust,
1977 TSS_Deep_Finalize,
1978 TSS_Finalize_Address);
1979
1980 begin
1981 -- Remove previous controlled function which was constructed and
1982 -- analyzed when the type was frozen. This requires removing the
1983 -- body of the redefined primitive, as well as its specification
1984 -- if needed (there is no spec created for Deep_Initialize, see
1985 -- exp_ch3.adb). We must also dismantle the exception information
1986 -- that may have been generated for it when front end zero-cost
1987 -- tables are enabled.
1988
1989 for J in D_Names'Range loop
1990 Old_P := TSS (Tagged_Type, D_Names (J));
1991
1992 if Present (Old_P)
1993 and then Chars (Subp) = C_Names (J)
1994 then
1995 Old_Bod := Unit_Declaration_Node (Old_P);
1996 Remove (Old_Bod);
1997 Set_Is_Eliminated (Old_P);
1998 Set_Scope (Old_P, Scope (Current_Scope));
1999
2000 if Nkind (Old_Bod) = N_Subprogram_Body
2001 and then Present (Corresponding_Spec (Old_Bod))
2002 then
2003 Old_Spec := Corresponding_Spec (Old_Bod);
2004 Set_Has_Completion (Old_Spec, False);
2005 end if;
2006 end if;
2007 end loop;
2008
2009 Build_Late_Proc (Tagged_Type, Chars (Subp));
2010
2011 -- The new operation is added to the actions of the freeze node
2012 -- for the type, but this node has already been analyzed, so we
2013 -- must retrieve and analyze explicitly the new body.
2014
2015 if Present (F_Node)
2016 and then Present (Actions (F_Node))
2017 then
2018 Decl := Last (Actions (F_Node));
2019 Analyze (Decl);
2020 end if;
2021 end;
2022 end if;
2023
2024 -- AI12-0279: If the Yield aspect is specified for a dispatching
2025 -- subprogram that inherits the aspect, the specified value shall
2026 -- be confirming.
2027
2028 if Is_Dispatching_Operation (Subp)
2029 and then Is_Primitive_Wrapper (Subp)
2030 and then Present (Wrapped_Entity (Subp))
2031 and then Comes_From_Source (Wrapped_Entity (Subp))
2032 and then Present (Overridden_Operation (Subp))
2033 and then Has_Yield_Aspect (Overridden_Operation (Subp))
2034 /= Has_Yield_Aspect (Wrapped_Entity (Subp))
2035 then
2036 declare
2037 W_Ent : constant Entity_Id := Wrapped_Entity (Subp);
2038 W_Decl : constant Node_Id := Parent (W_Ent);
2039 Asp : Node_Id;
2040
2041 begin
2042 Asp := First (Aspect_Specifications (W_Decl));
2043 while Present (Asp) loop
2044 if Chars (Identifier (Asp)) = Name_Yield then
2045 Error_Msg_Name_1 := Name_Yield;
2046 Error_Msg_N
2047 ("specification of inherited aspect% can only confirm "
2048 & "parent value", Asp);
2049 end if;
2050
2051 Next (Asp);
2052 end loop;
2053
2054 Set_Has_Yield_Aspect (Wrapped_Entity (Subp));
2055 end;
2056 end if;
2057
2058 -- For similarity with record extensions, in Ada 9X the language should
2059 -- have disallowed adding visible operations to a tagged type after
2060 -- deriving a private extension from it. Report a warning if this
2061 -- primitive is defined after a private extension of Tagged_Type.
2062
2063 Warn_On_Late_Primitive_After_Private_Extension (Tagged_Type, Subp);
2064 end Check_Dispatching_Operation;
2065
2066 ------------------------------------------
2067 -- Check_Operation_From_Incomplete_Type --
2068 ------------------------------------------
2069
2070 procedure Check_Operation_From_Incomplete_Type
2071 (Subp : Entity_Id;
2072 Typ : Entity_Id)
2073 is
2074 Full : constant Entity_Id := Full_View (Typ);
2075 Parent_Typ : constant Entity_Id := Etype (Full);
2076 Old_Prim : constant Elist_Id := Primitive_Operations (Parent_Typ);
2077 New_Prim : constant Elist_Id := Primitive_Operations (Full);
2078 Op1, Op2 : Elmt_Id;
2079 Prev : Elmt_Id := No_Elmt;
2080
2081 function Derives_From (Parent_Subp : Entity_Id) return Boolean;
2082 -- Check that Subp has profile of an operation derived from Parent_Subp.
2083 -- Subp must have a parameter or result type that is Typ or an access
2084 -- parameter or access result type that designates Typ.
2085
2086 ------------------
2087 -- Derives_From --
2088 ------------------
2089
2090 function Derives_From (Parent_Subp : Entity_Id) return Boolean is
2091 F1, F2 : Entity_Id;
2092
2093 begin
2094 if Chars (Parent_Subp) /= Chars (Subp) then
2095 return False;
2096 end if;
2097
2098 -- Check that the type of controlling formals is derived from the
2099 -- parent subprogram's controlling formal type (or designated type
2100 -- if the formal type is an anonymous access type).
2101
2102 F1 := First_Formal (Parent_Subp);
2103 F2 := First_Formal (Subp);
2104 while Present (F1) and then Present (F2) loop
2105 if Ekind (Etype (F1)) = E_Anonymous_Access_Type then
2106 if Ekind (Etype (F2)) /= E_Anonymous_Access_Type then
2107 return False;
2108 elsif Designated_Type (Etype (F1)) = Parent_Typ
2109 and then Designated_Type (Etype (F2)) /= Full
2110 then
2111 return False;
2112 end if;
2113
2114 elsif Ekind (Etype (F2)) = E_Anonymous_Access_Type then
2115 return False;
2116
2117 elsif Etype (F1) = Parent_Typ and then Etype (F2) /= Full then
2118 return False;
2119 end if;
2120
2121 Next_Formal (F1);
2122 Next_Formal (F2);
2123 end loop;
2124
2125 -- Check that a controlling result type is derived from the parent
2126 -- subprogram's result type (or designated type if the result type
2127 -- is an anonymous access type).
2128
2129 if Ekind (Parent_Subp) = E_Function then
2130 if Ekind (Subp) /= E_Function then
2131 return False;
2132
2133 elsif Ekind (Etype (Parent_Subp)) = E_Anonymous_Access_Type then
2134 if Ekind (Etype (Subp)) /= E_Anonymous_Access_Type then
2135 return False;
2136
2137 elsif Designated_Type (Etype (Parent_Subp)) = Parent_Typ
2138 and then Designated_Type (Etype (Subp)) /= Full
2139 then
2140 return False;
2141 end if;
2142
2143 elsif Ekind (Etype (Subp)) = E_Anonymous_Access_Type then
2144 return False;
2145
2146 elsif Etype (Parent_Subp) = Parent_Typ
2147 and then Etype (Subp) /= Full
2148 then
2149 return False;
2150 end if;
2151
2152 elsif Ekind (Subp) = E_Function then
2153 return False;
2154 end if;
2155
2156 return No (F1) and then No (F2);
2157 end Derives_From;
2158
2159 -- Start of processing for Check_Operation_From_Incomplete_Type
2160
2161 begin
2162 -- The operation may override an inherited one, or may be a new one
2163 -- altogether. The inherited operation will have been hidden by the
2164 -- current one at the point of the type derivation, so it does not
2165 -- appear in the list of primitive operations of the type. We have to
2166 -- find the proper place of insertion in the list of primitive opera-
2167 -- tions by iterating over the list for the parent type.
2168
2169 Op1 := First_Elmt (Old_Prim);
2170 Op2 := First_Elmt (New_Prim);
2171 while Present (Op1) and then Present (Op2) loop
2172 if Derives_From (Node (Op1)) then
2173 if No (Prev) then
2174
2175 -- Avoid adding it to the list of primitives if already there
2176
2177 if Node (Op2) /= Subp then
2178 Prepend_Elmt (Subp, New_Prim);
2179 end if;
2180
2181 else
2182 Insert_Elmt_After (Subp, Prev);
2183 end if;
2184
2185 return;
2186 end if;
2187
2188 Prev := Op2;
2189 Next_Elmt (Op1);
2190 Next_Elmt (Op2);
2191 end loop;
2192
2193 -- Operation is a new primitive
2194
2195 Append_Elmt (Subp, New_Prim);
2196 end Check_Operation_From_Incomplete_Type;
2197
2198 ---------------------------------------
2199 -- Check_Operation_From_Private_View --
2200 ---------------------------------------
2201
2202 procedure Check_Operation_From_Private_View (Subp, Old_Subp : Entity_Id) is
2203 Tagged_Type : Entity_Id;
2204
2205 begin
2206 if Is_Dispatching_Operation (Alias (Subp)) then
2207 Set_Scope (Subp, Current_Scope);
2208 Tagged_Type := Find_Dispatching_Type (Subp);
2209
2210 -- Add Old_Subp to primitive operations if not already present
2211
2212 if Present (Tagged_Type) and then Is_Tagged_Type (Tagged_Type) then
2213 Add_Dispatching_Operation (Tagged_Type, Old_Subp);
2214
2215 -- If Old_Subp isn't already marked as dispatching then this is
2216 -- the case of an operation of an untagged private type fulfilled
2217 -- by a tagged type that overrides an inherited dispatching
2218 -- operation, so we set the necessary dispatching attributes here.
2219
2220 if not Is_Dispatching_Operation (Old_Subp) then
2221
2222 -- If the untagged type has no discriminants, and the full
2223 -- view is constrained, there will be a spurious mismatch of
2224 -- subtypes on the controlling arguments, because the tagged
2225 -- type is the internal base type introduced in the derivation.
2226 -- Use the original type to verify conformance, rather than the
2227 -- base type.
2228
2229 if not Comes_From_Source (Tagged_Type)
2230 and then Has_Discriminants (Tagged_Type)
2231 then
2232 declare
2233 Formal : Entity_Id;
2234
2235 begin
2236 Formal := First_Formal (Old_Subp);
2237 while Present (Formal) loop
2238 if Tagged_Type = Base_Type (Etype (Formal)) then
2239 Tagged_Type := Etype (Formal);
2240 end if;
2241
2242 Next_Formal (Formal);
2243 end loop;
2244 end;
2245
2246 if Tagged_Type = Base_Type (Etype (Old_Subp)) then
2247 Tagged_Type := Etype (Old_Subp);
2248 end if;
2249 end if;
2250
2251 Check_Controlling_Formals (Tagged_Type, Old_Subp);
2252 Set_Is_Dispatching_Operation (Old_Subp, True);
2253 Set_DT_Position_Value (Old_Subp, No_Uint);
2254 end if;
2255
2256 -- If the old subprogram is an explicit renaming of some other
2257 -- entity, it is not overridden by the inherited subprogram.
2258 -- Otherwise, update its alias and other attributes.
2259
2260 if Present (Alias (Old_Subp))
2261 and then Nkind (Unit_Declaration_Node (Old_Subp)) /=
2262 N_Subprogram_Renaming_Declaration
2263 then
2264 Set_Alias (Old_Subp, Alias (Subp));
2265
2266 -- The derived subprogram should inherit the abstractness of
2267 -- the parent subprogram (except in the case of a function
2268 -- returning the type). This sets the abstractness properly
2269 -- for cases where a private extension may have inherited an
2270 -- abstract operation, but the full type is derived from a
2271 -- descendant type and inherits a nonabstract version.
2272
2273 if Etype (Subp) /= Tagged_Type then
2274 Set_Is_Abstract_Subprogram
2275 (Old_Subp, Is_Abstract_Subprogram (Alias (Subp)));
2276 end if;
2277 end if;
2278 end if;
2279 end if;
2280 end Check_Operation_From_Private_View;
2281
2282 --------------------------
2283 -- Find_Controlling_Arg --
2284 --------------------------
2285
2286 function Find_Controlling_Arg (N : Node_Id) return Node_Id is
2287 Orig_Node : constant Node_Id := Original_Node (N);
2288 Typ : Entity_Id;
2289
2290 begin
2291 if Nkind (Orig_Node) = N_Qualified_Expression then
2292 return Find_Controlling_Arg (Expression (Orig_Node));
2293 end if;
2294
2295 -- Dispatching on result case. If expansion is disabled, the node still
2296 -- has the structure of a function call. However, if the function name
2297 -- is an operator and the call was given in infix form, the original
2298 -- node has no controlling result and we must examine the current node.
2299
2300 if Nkind (N) = N_Function_Call
2301 and then Present (Controlling_Argument (N))
2302 and then Has_Controlling_Result (Entity (Name (N)))
2303 then
2304 return Controlling_Argument (N);
2305
2306 -- If expansion is enabled, the call may have been transformed into
2307 -- an indirect call, and we need to recover the original node.
2308
2309 elsif Nkind (Orig_Node) = N_Function_Call
2310 and then Present (Controlling_Argument (Orig_Node))
2311 and then Has_Controlling_Result (Entity (Name (Orig_Node)))
2312 then
2313 return Controlling_Argument (Orig_Node);
2314
2315 -- Type conversions are dynamically tagged if the target type, or its
2316 -- designated type, are classwide. An interface conversion expands into
2317 -- a dereference, so test must be performed on the original node.
2318
2319 elsif Nkind (Orig_Node) = N_Type_Conversion
2320 and then Nkind (N) = N_Explicit_Dereference
2321 and then Is_Controlling_Actual (N)
2322 then
2323 declare
2324 Target_Type : constant Entity_Id :=
2325 Entity (Subtype_Mark (Orig_Node));
2326
2327 begin
2328 if Is_Class_Wide_Type (Target_Type) then
2329 return N;
2330
2331 elsif Is_Access_Type (Target_Type)
2332 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
2333 then
2334 return N;
2335
2336 else
2337 return Empty;
2338 end if;
2339 end;
2340
2341 -- Normal case
2342
2343 elsif Is_Controlling_Actual (N)
2344 or else
2345 (Nkind (Parent (N)) = N_Qualified_Expression
2346 and then Is_Controlling_Actual (Parent (N)))
2347 then
2348 Typ := Etype (N);
2349
2350 if Is_Access_Type (Typ) then
2351
2352 -- In the case of an Access attribute, use the type of the prefix,
2353 -- since in the case of an actual for an access parameter, the
2354 -- attribute's type may be of a specific designated type, even
2355 -- though the prefix type is class-wide.
2356
2357 if Nkind (N) = N_Attribute_Reference then
2358 Typ := Etype (Prefix (N));
2359
2360 -- An allocator is dispatching if the type of qualified expression
2361 -- is class_wide, in which case this is the controlling type.
2362
2363 elsif Nkind (Orig_Node) = N_Allocator
2364 and then Nkind (Expression (Orig_Node)) = N_Qualified_Expression
2365 then
2366 Typ := Etype (Expression (Orig_Node));
2367 else
2368 Typ := Designated_Type (Typ);
2369 end if;
2370 end if;
2371
2372 if Is_Class_Wide_Type (Typ)
2373 or else
2374 (Nkind (Parent (N)) = N_Qualified_Expression
2375 and then Is_Access_Type (Etype (N))
2376 and then Is_Class_Wide_Type (Designated_Type (Etype (N))))
2377 then
2378 return N;
2379 end if;
2380 end if;
2381
2382 return Empty;
2383 end Find_Controlling_Arg;
2384
2385 ---------------------------
2386 -- Find_Dispatching_Type --
2387 ---------------------------
2388
2389 function Find_Dispatching_Type (Subp : Entity_Id) return Entity_Id is
2390
2391 function Has_Predefined_Dispatching_Operation_Name return Boolean;
2392 -- Determines if Subp has the name of a predefined dispatching
2393 -- operation.
2394
2395 -----------------------------------------------
2396 -- Has_Predefined_Dispatching_Operation_Name --
2397 -----------------------------------------------
2398
2399 function Has_Predefined_Dispatching_Operation_Name return Boolean is
2400 TSS_Name : TSS_Name_Type;
2401
2402 begin
2403 Get_Name_String (Chars (Subp));
2404
2405 if Name_Len > TSS_Name_Type'Last then
2406 TSS_Name :=
2407 TSS_Name_Type
2408 (Name_Buffer (Name_Len - TSS_Name'Length + 1 .. Name_Len));
2409
2410 if Chars (Subp) in Name_uAssign
2411 | Name_uSize
2412 | Name_Op_Eq
2413 or else TSS_Name = TSS_Deep_Adjust
2414 or else TSS_Name = TSS_Deep_Finalize
2415 or else TSS_Name = TSS_Stream_Input
2416 or else TSS_Name = TSS_Stream_Output
2417 or else TSS_Name = TSS_Stream_Read
2418 or else TSS_Name = TSS_Stream_Write
2419 or else TSS_Name = TSS_Put_Image
2420
2421 -- Name of predefined interface type primitives
2422
2423 or else Chars (Subp) in Name_uDisp_Asynchronous_Select
2424 | Name_uDisp_Conditional_Select
2425 | Name_uDisp_Get_Prim_Op_Kind
2426 | Name_uDisp_Get_Task_Id
2427 | Name_uDisp_Requeue
2428 | Name_uDisp_Timed_Select
2429 then
2430 return True;
2431 end if;
2432 end if;
2433
2434 return False;
2435 end Has_Predefined_Dispatching_Operation_Name;
2436
2437 -- Local variables
2438
2439 A_Formal : Entity_Id;
2440 Formal : Entity_Id;
2441 Ctrl_Type : Entity_Id;
2442
2443 begin
2444 if Ekind (Subp) in E_Function | E_Procedure
2445 and then Present (DTC_Entity (Subp))
2446 then
2447 return Scope (DTC_Entity (Subp));
2448
2449 -- For subprograms internally generated by derivations of tagged types
2450 -- use the alias subprogram as a reference to locate the dispatching
2451 -- type of Subp.
2452
2453 elsif not Comes_From_Source (Subp)
2454 and then Present (Alias (Subp))
2455 and then Is_Dispatching_Operation (Alias (Subp))
2456 then
2457 if Ekind (Alias (Subp)) = E_Function
2458 and then Has_Controlling_Result (Alias (Subp))
2459 then
2460 return Check_Controlling_Type (Etype (Subp), Subp);
2461
2462 else
2463 Formal := First_Formal (Subp);
2464 A_Formal := First_Formal (Alias (Subp));
2465 while Present (A_Formal) loop
2466 if Is_Controlling_Formal (A_Formal) then
2467 return Check_Controlling_Type (Etype (Formal), Subp);
2468 end if;
2469
2470 Next_Formal (Formal);
2471 Next_Formal (A_Formal);
2472 end loop;
2473
2474 pragma Assert (False);
2475 return Empty;
2476 end if;
2477
2478 -- General case
2479
2480 else
2481 Formal := First_Formal (Subp);
2482 while Present (Formal) loop
2483 Ctrl_Type := Check_Controlling_Type (Etype (Formal), Subp);
2484
2485 if Present (Ctrl_Type) then
2486 return Ctrl_Type;
2487 end if;
2488
2489 Next_Formal (Formal);
2490 end loop;
2491
2492 -- The subprogram may also be dispatching on result
2493
2494 if Present (Etype (Subp)) then
2495 if Is_Tagged_Type (Etype (Subp))
2496 and then Has_First_Controlling_Parameter_Aspect (Etype (Subp))
2497 then
2498 if Present (Overridden_Operation (Subp))
2499 and then Has_Controlling_Result (Overridden_Operation (Subp))
2500 then
2501 return Check_Controlling_Type (Etype (Subp), Subp);
2502
2503 -- Internal subprograms added by the frontend bypass the
2504 -- restrictions of First_Controlling_Parameter aspect.
2505
2506 elsif Is_Internal (Subp)
2507 and then Has_Predefined_Dispatching_Operation_Name
2508 then
2509 return Check_Controlling_Type (Etype (Subp), Subp);
2510 end if;
2511 else
2512 return Check_Controlling_Type (Etype (Subp), Subp);
2513 end if;
2514 end if;
2515 end if;
2516
2517 pragma Assert (not Is_Dispatching_Operation (Subp));
2518 return Empty;
2519 end Find_Dispatching_Type;
2520
2521 --------------------------------------
2522 -- Find_Hidden_Overridden_Primitive --
2523 --------------------------------------
2524
2525 function Find_Hidden_Overridden_Primitive (S : Entity_Id) return Entity_Id
2526 is
2527 Tag_Typ : constant Entity_Id := Find_Dispatching_Type (S);
2528 Elmt : Elmt_Id;
2529 Orig_Prim : Entity_Id;
2530 Prim : Entity_Id;
2531 Vis_List : Elist_Id;
2532
2533 begin
2534 -- This Ada 2012 rule applies only for type extensions or private
2535 -- extensions, where the parent type is not in a parent unit, and
2536 -- where an operation is never declared but still inherited.
2537
2538 if No (Tag_Typ)
2539 or else not Is_Record_Type (Tag_Typ)
2540 or else Etype (Tag_Typ) = Tag_Typ
2541 or else In_Open_Scopes (Scope (Etype (Tag_Typ)))
2542 then
2543 return Empty;
2544 end if;
2545
2546 -- Collect the list of visible ancestor of the tagged type
2547
2548 Vis_List := Visible_Ancestors (Tag_Typ);
2549
2550 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2551 while Present (Elmt) loop
2552 Prim := Node (Elmt);
2553
2554 -- Find an inherited hidden dispatching primitive with the name of S
2555 -- and a type-conformant profile.
2556
2557 if Present (Alias (Prim))
2558 and then Is_Hidden (Alias (Prim))
2559 and then Find_Dispatching_Type (Alias (Prim)) /= Tag_Typ
2560 and then Primitive_Names_Match (S, Prim)
2561 and then Type_Conformant (S, Prim)
2562 then
2563 declare
2564 Vis_Ancestor : Elmt_Id;
2565 Elmt : Elmt_Id;
2566
2567 begin
2568 -- The original corresponding operation of Prim must be an
2569 -- operation of a visible ancestor of the dispatching type S,
2570 -- and the original corresponding operation of S2 must be
2571 -- visible.
2572
2573 Orig_Prim := Original_Corresponding_Operation (Prim);
2574
2575 if Orig_Prim /= Prim
2576 and then not Is_Hidden (Orig_Prim)
2577 then
2578 Vis_Ancestor := First_Elmt (Vis_List);
2579 while Present (Vis_Ancestor) loop
2580 Elmt :=
2581 First_Elmt (Primitive_Operations (Node (Vis_Ancestor)));
2582 while Present (Elmt) loop
2583 if Node (Elmt) = Orig_Prim then
2584 Set_Overridden_Operation (S, Prim);
2585 Set_Is_Ada_2022_Only (S,
2586 Is_Ada_2022_Only (Prim));
2587 Set_Alias (Prim, Orig_Prim);
2588 return Prim;
2589 end if;
2590
2591 Next_Elmt (Elmt);
2592 end loop;
2593
2594 Next_Elmt (Vis_Ancestor);
2595 end loop;
2596 end if;
2597 end;
2598 end if;
2599
2600 Next_Elmt (Elmt);
2601 end loop;
2602
2603 return Empty;
2604 end Find_Hidden_Overridden_Primitive;
2605
2606 ---------------------------------------
2607 -- Find_Primitive_Covering_Interface --
2608 ---------------------------------------
2609
2610 function Find_Primitive_Covering_Interface
2611 (Tagged_Type : Entity_Id;
2612 Iface_Prim : Entity_Id) return Entity_Id
2613 is
2614 Is_FCP_Type : constant Boolean :=
2615 Has_First_Controlling_Parameter_Aspect (Tagged_Type);
2616 E : Entity_Id;
2617 El : Elmt_Id;
2618
2619 begin
2620 pragma Assert (Is_Interface (Find_Dispatching_Type (Iface_Prim))
2621 or else (Present (Alias (Iface_Prim))
2622 and then
2623 Is_Interface
2624 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
2625
2626 -- Search in the homonym chain. Done to speed up locating visible
2627 -- entities and required to catch primitives associated with the partial
2628 -- view of private types when processing the corresponding full view.
2629
2630 E := Current_Entity (Iface_Prim);
2631 while Present (E) loop
2632 if Is_Subprogram (E)
2633 and then Is_Dispatching_Operation (E)
2634 then
2635 -- For overriding primitives of parent or interface types that
2636 -- do not have the aspect First_Controlling_Parameter, we must
2637 -- temporarily unset this attribute to check conformance.
2638
2639 if Ekind (E) = E_Function
2640 and then Is_FCP_Type
2641 and then Present (Overridden_Operation (E))
2642 and then Has_Controlling_Result (Overridden_Operation (E))
2643 then
2644 Set_Has_First_Controlling_Parameter_Aspect (Tagged_Type, False);
2645
2646 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2647 Set_Has_First_Controlling_Parameter_Aspect
2648 (Tagged_Type, Is_FCP_Type);
2649 return E;
2650 end if;
2651
2652 Set_Has_First_Controlling_Parameter_Aspect
2653 (Tagged_Type, Is_FCP_Type);
2654
2655 elsif Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2656 return E;
2657 end if;
2658 end if;
2659
2660 E := Homonym (E);
2661 end loop;
2662
2663 -- Search in the list of primitives of the type. Required to locate
2664 -- the covering primitive if the covering primitive is not visible
2665 -- (for example, non-visible inherited primitive of private type).
2666
2667 El := First_Elmt (Primitive_Operations (Tagged_Type));
2668 while Present (El) loop
2669 E := Node (El);
2670
2671 -- Keep separate the management of internal entities that link
2672 -- primitives with interface primitives from tagged type primitives.
2673
2674 if No (Interface_Alias (E)) then
2675 if Present (Alias (E)) then
2676
2677 -- This interface primitive has not been covered yet
2678
2679 if Alias (E) = Iface_Prim then
2680 return E;
2681
2682 -- The covering primitive was inherited
2683
2684 elsif Overridden_Operation (Ultimate_Alias (E))
2685 = Iface_Prim
2686 then
2687 return E;
2688 end if;
2689 end if;
2690
2691 -- Check if E covers the interface primitive (includes case in
2692 -- which E is an inherited private primitive).
2693
2694 -- For overriding primitives of parent or interface types that
2695 -- do not have the aspect First_Controlling_Parameter, we must
2696 -- temporarily unset this attribute to check conformance.
2697
2698 if Present (Overridden_Operation (E))
2699 and then Is_FCP_Type
2700 and then not
2701 Has_First_Controlling_Parameter_Aspect
2702 (Find_Dispatching_Type (Overridden_Operation (E)))
2703 then
2704 Set_Has_First_Controlling_Parameter_Aspect (Tagged_Type, False);
2705
2706 if Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2707 Set_Has_First_Controlling_Parameter_Aspect
2708 (Tagged_Type, Is_FCP_Type);
2709 return E;
2710 end if;
2711
2712 Set_Has_First_Controlling_Parameter_Aspect
2713 (Tagged_Type, Is_FCP_Type);
2714
2715 elsif Is_Interface_Conformant (Tagged_Type, Iface_Prim, E) then
2716 return E;
2717 end if;
2718
2719 -- Use the internal entity that links the interface primitive with
2720 -- the covering primitive to locate the entity.
2721
2722 elsif Interface_Alias (E) = Iface_Prim then
2723 return Alias (E);
2724 end if;
2725
2726 Next_Elmt (El);
2727 end loop;
2728
2729 -- Not found
2730
2731 return Empty;
2732 end Find_Primitive_Covering_Interface;
2733
2734 ---------------------------
2735 -- Inheritance_Utilities --
2736 ---------------------------
2737
2738 package body Inheritance_Utilities is
2739
2740 ---------------------------
2741 -- Inherited_Subprograms --
2742 ---------------------------
2743
2744 function Inherited_Subprograms
2745 (S : Entity_Id;
2746 No_Interfaces : Boolean := False;
2747 Interfaces_Only : Boolean := False;
2748 Skip_Overridden : Boolean := False;
2749 One_Only : Boolean := False) return Subprogram_List
2750 is
2751 Result : Subprogram_List (1 .. 6000);
2752 -- 6000 here is intended to be infinity. We could use an expandable
2753 -- table, but it would be awfully heavy, and there is no way that we
2754 -- could reasonably exceed this value.
2755
2756 N : Nat := 0;
2757 -- Number of entries in Result
2758
2759 Parent_Op : Entity_Id;
2760 -- Traverses the Overridden_Operation chain
2761
2762 procedure Store_IS (E : Entity_Id);
2763 -- Stores E in Result if not already stored
2764
2765 --------------
2766 -- Store_IS --
2767 --------------
2768
2769 procedure Store_IS (E : Entity_Id) is
2770 begin
2771 for J in 1 .. N loop
2772 if E = Result (J) then
2773 return;
2774 end if;
2775 end loop;
2776
2777 N := N + 1;
2778 Result (N) := E;
2779 end Store_IS;
2780
2781 -- Start of processing for Inherited_Subprograms
2782
2783 begin
2784 pragma Assert (not (No_Interfaces and Interfaces_Only));
2785
2786 -- When used from backends, visibility can be handled differently
2787 -- resulting in no dispatching type being found.
2788
2789 if Present (S)
2790 and then Is_Dispatching_Operation (S)
2791 and then Present (Find_DT (S))
2792 then
2793 -- Deal with direct inheritance
2794
2795 if not Interfaces_Only then
2796 Parent_Op := S;
2797 loop
2798 Parent_Op := Overridden_Operation (Parent_Op);
2799 exit when No (Parent_Op)
2800 or else No (Find_DT (Parent_Op))
2801 or else (No_Interfaces
2802 and then Is_Interface (Find_DT (Parent_Op)));
2803
2804 if Is_Subprogram_Or_Generic_Subprogram (Parent_Op) then
2805 Store_IS (Parent_Op);
2806
2807 if One_Only then
2808 goto Done;
2809 end if;
2810 end if;
2811 end loop;
2812 end if;
2813
2814 -- Now deal with interfaces
2815
2816 if not No_Interfaces then
2817 declare
2818 Tag_Typ : Entity_Id;
2819 Prim : Entity_Id;
2820 Elmt : Elmt_Id;
2821
2822 begin
2823 Tag_Typ := Find_DT (S);
2824
2825 -- In the presence of limited views there may be no visible
2826 -- dispatching type. Primitives will be inherited when non-
2827 -- limited view is frozen.
2828
2829 if No (Tag_Typ) then
2830 return Result (1 .. 0);
2831
2832 -- Prevent cascaded errors
2833
2834 elsif Is_Concurrent_Type (Tag_Typ)
2835 and then No (Corresponding_Record_Type (Tag_Typ))
2836 and then Serious_Errors_Detected > 0
2837 then
2838 return Result (1 .. 0);
2839 end if;
2840
2841 if Is_Concurrent_Type (Tag_Typ) then
2842 Tag_Typ := Corresponding_Record_Type (Tag_Typ);
2843 end if;
2844
2845 if Present (Tag_Typ)
2846 and then Is_Private_Type (Tag_Typ)
2847 and then Present (Full_View (Tag_Typ))
2848 then
2849 Tag_Typ := Full_View (Tag_Typ);
2850 end if;
2851
2852 -- Search primitive operations of dispatching type
2853
2854 if Present (Tag_Typ)
2855 and then Present (Primitive_Operations (Tag_Typ))
2856 then
2857 Elmt := First_Elmt (Primitive_Operations (Tag_Typ));
2858 while Present (Elmt) loop
2859 Prim := Node (Elmt);
2860
2861 -- The following test eliminates some odd cases in
2862 -- which Ekind (Prim) is Void, to be investigated
2863 -- further ???
2864
2865 if not Is_Subprogram_Or_Generic_Subprogram (Prim) then
2866 null;
2867
2868 -- For [generic] subprogram, look at interface
2869 -- alias.
2870
2871 elsif Present (Interface_Alias (Prim))
2872 and then Alias (Prim) = S
2873 then
2874 -- We have found a primitive covered by S
2875
2876 Store_IS (Interface_Alias (Prim));
2877
2878 if One_Only then
2879 goto Done;
2880 end if;
2881 end if;
2882
2883 Next_Elmt (Elmt);
2884 end loop;
2885 end if;
2886 end;
2887 end if;
2888 end if;
2889
2890 -- Do not keep an overridden operation if its overridding operation
2891 -- is in the results too, and it is not S. This can happen for
2892 -- inheritance between interfaces.
2893
2894 if Skip_Overridden then
2895 declare
2896 Res : constant Subprogram_List (1 .. N) := Result (1 .. N);
2897 M : Nat := 0;
2898 begin
2899 for J in 1 .. N loop
2900 for K in 1 .. N loop
2901 if Res (K) /= S
2902 and then Res (J) = Overridden_Operation (Res (K))
2903 then
2904 goto Skip;
2905 end if;
2906 end loop;
2907
2908 M := M + 1;
2909 Result (M) := Res (J);
2910
2911 <<Skip>>
2912 end loop;
2913
2914 N := M;
2915 end;
2916 end if;
2917
2918 <<Done>>
2919
2920 return Result (1 .. N);
2921 end Inherited_Subprograms;
2922
2923 ------------------------------
2924 -- Is_Overriding_Subprogram --
2925 ------------------------------
2926
2927 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean is
2928 Inherited : constant Subprogram_List :=
2929 Inherited_Subprograms (E, One_Only => True);
2930 begin
2931 return Inherited'Length > 0;
2932 end Is_Overriding_Subprogram;
2933 end Inheritance_Utilities;
2934
2935 --------------------------------
2936 -- Inheritance_Utilities_Inst --
2937 --------------------------------
2938
2939 package Inheritance_Utilities_Inst is new
2940 Inheritance_Utilities (Find_Dispatching_Type);
2941
2942 ---------------------------
2943 -- Inherited_Subprograms --
2944 ---------------------------
2945
2946 function Inherited_Subprograms
2947 (S : Entity_Id;
2948 No_Interfaces : Boolean := False;
2949 Interfaces_Only : Boolean := False;
2950 Skip_Overridden : Boolean := False;
2951 One_Only : Boolean := False) return Subprogram_List renames
2952 Inheritance_Utilities_Inst.Inherited_Subprograms;
2953
2954 ---------------------------
2955 -- Is_Dynamically_Tagged --
2956 ---------------------------
2957
2958 function Is_Dynamically_Tagged (N : Node_Id) return Boolean is
2959 begin
2960 if Nkind (N) = N_Error then
2961 return False;
2962
2963 elsif Present (Find_Controlling_Arg (N)) then
2964 return True;
2965
2966 -- Special cases: entities, and calls that dispatch on result
2967
2968 elsif Is_Entity_Name (N) then
2969 return Is_Class_Wide_Type (Etype (N));
2970
2971 elsif Nkind (N) = N_Function_Call
2972 and then Is_Class_Wide_Type (Etype (N))
2973 then
2974 return True;
2975
2976 -- Otherwise check whether call has controlling argument
2977
2978 else
2979 return False;
2980 end if;
2981 end Is_Dynamically_Tagged;
2982
2983 ---------------------------------
2984 -- Is_Null_Interface_Primitive --
2985 ---------------------------------
2986
2987 function Is_Null_Interface_Primitive (E : Entity_Id) return Boolean is
2988 begin
2989 return Comes_From_Source (E)
2990 and then Is_Dispatching_Operation (E)
2991 and then Ekind (E) = E_Procedure
2992 and then Null_Present (Parent (E))
2993 and then Is_Interface (Find_Dispatching_Type (E));
2994 end Is_Null_Interface_Primitive;
2995
2996 -----------------------------------
2997 -- Is_Inherited_Public_Operation --
2998 -----------------------------------
2999
3000 function Is_Inherited_Public_Operation (Op : Entity_Id) return Boolean is
3001 Pack_Decl : Node_Id;
3002 Prim : Entity_Id := Op;
3003 Scop : Entity_Id := Prim;
3004
3005 begin
3006 -- Locate the ultimate non-hidden alias entity
3007
3008 while Present (Alias (Prim)) and then not Is_Hidden (Alias (Prim)) loop
3009 pragma Assert (Alias (Prim) /= Prim);
3010 Prim := Alias (Prim);
3011 Scop := Scope (Prim);
3012 end loop;
3013
3014 if Comes_From_Source (Prim) and then Ekind (Scop) = E_Package then
3015 Pack_Decl := Unit_Declaration_Node (Scop);
3016
3017 return
3018 Nkind (Pack_Decl) = N_Package_Declaration
3019 and then List_Containing (Unit_Declaration_Node (Prim)) =
3020 Visible_Declarations (Specification (Pack_Decl));
3021
3022 else
3023 return False;
3024 end if;
3025 end Is_Inherited_Public_Operation;
3026
3027 ------------------------------
3028 -- Is_Overriding_Subprogram --
3029 ------------------------------
3030
3031 function Is_Overriding_Subprogram (E : Entity_Id) return Boolean renames
3032 Inheritance_Utilities_Inst.Is_Overriding_Subprogram;
3033
3034 --------------------------
3035 -- Is_Tag_Indeterminate --
3036 --------------------------
3037
3038 function Is_Tag_Indeterminate (N : Node_Id) return Boolean is
3039 Nam : Entity_Id;
3040 Actual : Node_Id;
3041 Orig_Node : constant Node_Id := Original_Node (N);
3042
3043 begin
3044 if Nkind (Orig_Node) = N_Function_Call
3045 and then Is_Entity_Name (Name (Orig_Node))
3046 then
3047 Nam := Entity (Name (Orig_Node));
3048
3049 if not Has_Controlling_Result (Nam) then
3050 return False;
3051
3052 -- The function may have a controlling result, but if the return type
3053 -- is not visibly tagged, then this is not tag-indeterminate.
3054
3055 elsif Is_Access_Type (Etype (Nam))
3056 and then not Is_Tagged_Type (Designated_Type (Etype (Nam)))
3057 then
3058 return False;
3059
3060 -- An explicit dereference means that the call has already been
3061 -- expanded and there is no tag to propagate.
3062
3063 elsif Nkind (N) = N_Explicit_Dereference then
3064 return False;
3065
3066 -- If there are no actuals, the call is tag-indeterminate
3067
3068 elsif No (Parameter_Associations (Orig_Node)) then
3069 return True;
3070
3071 else
3072 Actual := First_Actual (Orig_Node);
3073 while Present (Actual) loop
3074 if Is_Controlling_Actual (Actual)
3075 and then not Is_Tag_Indeterminate (Actual)
3076 then
3077 -- One operand is dispatching
3078
3079 return False;
3080 end if;
3081
3082 Next_Actual (Actual);
3083 end loop;
3084
3085 return True;
3086 end if;
3087
3088 elsif Nkind (Orig_Node) = N_Qualified_Expression then
3089 return Is_Tag_Indeterminate (Expression (Orig_Node));
3090
3091 -- Case of a call to the Input attribute (possibly rewritten), which is
3092 -- always tag-indeterminate except when its prefix is a Class attribute.
3093
3094 elsif Nkind (Orig_Node) = N_Attribute_Reference
3095 and then
3096 Get_Attribute_Id (Attribute_Name (Orig_Node)) = Attribute_Input
3097 and then Nkind (Prefix (Orig_Node)) /= N_Attribute_Reference
3098 then
3099 return True;
3100
3101 -- In Ada 2005, a function that returns an anonymous access type can be
3102 -- dispatching, and the dereference of a call to such a function can
3103 -- also be tag-indeterminate if the call itself is.
3104
3105 elsif Nkind (Orig_Node) = N_Explicit_Dereference
3106 and then Ada_Version >= Ada_2005
3107 then
3108 return Is_Tag_Indeterminate (Prefix (Orig_Node));
3109
3110 else
3111 return False;
3112 end if;
3113 end Is_Tag_Indeterminate;
3114
3115 ------------------------------------
3116 -- Override_Dispatching_Operation --
3117 ------------------------------------
3118
3119 procedure Override_Dispatching_Operation
3120 (Tagged_Type : Entity_Id;
3121 Prev_Op : Entity_Id;
3122 New_Op : Entity_Id)
3123 is
3124 Elmt : Elmt_Id;
3125 Prim : Node_Id;
3126
3127 begin
3128 -- If there is no previous operation to override, the type declaration
3129 -- was malformed, and an error must have been emitted already.
3130
3131 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
3132 while Present (Elmt) and then Node (Elmt) /= Prev_Op loop
3133 Next_Elmt (Elmt);
3134 end loop;
3135
3136 if No (Elmt) then
3137 return;
3138 end if;
3139
3140 -- The location of entities that come from source in the list of
3141 -- primitives of the tagged type must follow their order of occurrence
3142 -- in the sources to fulfill the C++ ABI. If the overridden entity is a
3143 -- primitive of an interface that is not implemented by the parents of
3144 -- this tagged type (that is, it is an alias of an interface primitive
3145 -- generated by Derive_Interface_Progenitors), then we must append the
3146 -- new entity at the end of the list of primitives.
3147
3148 if Present (Alias (Prev_Op))
3149 and then Etype (Tagged_Type) /= Tagged_Type
3150 and then Is_Interface (Find_Dispatching_Type (Alias (Prev_Op)))
3151 and then not Is_Ancestor (Find_Dispatching_Type (Alias (Prev_Op)),
3152 Tagged_Type, Use_Full_View => True)
3153 and then not Implements_Interface
3154 (Etype (Tagged_Type),
3155 Find_Dispatching_Type (Alias (Prev_Op)))
3156 then
3157 Remove_Elmt (Primitive_Operations (Tagged_Type), Elmt);
3158 Add_Dispatching_Operation (Tagged_Type, New_Op);
3159
3160 -- The new primitive replaces the overridden entity. Required to ensure
3161 -- that overriding primitive is assigned the same dispatch table slot.
3162
3163 else
3164 Replace_Elmt (Elmt, New_Op);
3165 end if;
3166
3167 if Ada_Version >= Ada_2005 and then Has_Interfaces (Tagged_Type) then
3168
3169 -- Ada 2005 (AI-251): Update the attribute alias of all the aliased
3170 -- entities of the overridden primitive to reference New_Op, and
3171 -- also propagate the proper value of Is_Abstract_Subprogram. Verify
3172 -- that the new operation is subtype conformant with the interface
3173 -- operations that it implements (for operations inherited from the
3174 -- parent itself, this check is made when building the derived type).
3175
3176 -- Note: This code is executed with internally generated wrappers of
3177 -- functions with controlling result and late overridings.
3178
3179 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
3180 while Present (Elmt) loop
3181 Prim := Node (Elmt);
3182
3183 if Prim = New_Op then
3184 null;
3185
3186 -- Note: The check on Is_Subprogram protects the frontend against
3187 -- reading attributes in entities that are not yet fully decorated
3188
3189 elsif Is_Subprogram (Prim)
3190 and then Present (Interface_Alias (Prim))
3191 and then Alias (Prim) = Prev_Op
3192 then
3193 Set_Alias (Prim, New_Op);
3194
3195 -- No further decoration needed yet for internally generated
3196 -- wrappers of controlling functions since (at this stage)
3197 -- they are not yet decorated.
3198
3199 if not Is_Wrapper (New_Op) then
3200 Check_Subtype_Conformant (New_Op, Prim);
3201
3202 Set_Is_Abstract_Subprogram (Prim,
3203 Is_Abstract_Subprogram (New_Op));
3204
3205 -- Ensure that this entity will be expanded to fill the
3206 -- corresponding entry in its dispatch table.
3207
3208 if not Is_Abstract_Subprogram (Prim) then
3209 Set_Has_Delayed_Freeze (Prim);
3210 end if;
3211 end if;
3212 end if;
3213
3214 Next_Elmt (Elmt);
3215 end loop;
3216 end if;
3217
3218 if not Is_Package_Or_Generic_Package (Current_Scope)
3219 or else not In_Private_Part (Current_Scope)
3220 then
3221 -- Not a private primitive
3222
3223 null;
3224
3225 else pragma Assert (Is_Inherited_Operation (Prev_Op));
3226
3227 -- Make the overriding operation into an alias of the implicit one.
3228 -- In this fashion a call from outside ends up calling the new body
3229 -- even if non-dispatching, and a call from inside calls the over-
3230 -- riding operation because it hides the implicit one. To indicate
3231 -- that the body of Prev_Op is never called, set its dispatch table
3232 -- entity to Empty. If the overridden operation has a dispatching
3233 -- result, so does the overriding one.
3234
3235 Set_Alias (Prev_Op, New_Op);
3236 Set_DTC_Entity (Prev_Op, Empty);
3237 Set_Has_Controlling_Result (New_Op, Has_Controlling_Result (Prev_Op));
3238 Set_Is_Ada_2022_Only (New_Op, Is_Ada_2022_Only (Prev_Op));
3239 end if;
3240 end Override_Dispatching_Operation;
3241
3242 -------------------
3243 -- Propagate_Tag --
3244 -------------------
3245
3246 procedure Propagate_Tag (Control : Node_Id; Actual : Node_Id) is
3247 Call_Node : Node_Id;
3248 Arg : Node_Id;
3249
3250 begin
3251 if Nkind (Actual) = N_Function_Call then
3252 Call_Node := Actual;
3253
3254 elsif Nkind (Actual) = N_Identifier
3255 and then Nkind (Original_Node (Actual)) = N_Function_Call
3256 then
3257 -- Call rewritten as object declaration when stack-checking is
3258 -- enabled. Propagate tag to expression in declaration, which is
3259 -- original call.
3260
3261 Call_Node := Expression (Parent (Entity (Actual)));
3262
3263 -- Ada 2005: If this is a dereference of a call to a function with a
3264 -- dispatching access-result, the tag is propagated when the dereference
3265 -- itself is expanded (see exp_ch6.adb) and there is nothing else to do.
3266
3267 elsif Nkind (Actual) = N_Explicit_Dereference
3268 and then Nkind (Original_Node (Prefix (Actual))) = N_Function_Call
3269 then
3270 return;
3271
3272 -- When expansion is suppressed, an unexpanded call to 'Input can occur,
3273 -- and in that case we can simply return.
3274
3275 elsif Nkind (Actual) = N_Attribute_Reference then
3276 pragma Assert (Attribute_Name (Actual) = Name_Input);
3277
3278 return;
3279
3280 -- Only other possibilities are parenthesized or qualified expression,
3281 -- or an expander-generated unchecked conversion of a function call to
3282 -- a stream Input attribute.
3283
3284 else
3285 Call_Node := Expression (Actual);
3286 end if;
3287
3288 -- No action needed if the call has been already expanded
3289
3290 if Is_Expanded_Dispatching_Call (Call_Node) then
3291 return;
3292 end if;
3293
3294 -- Do not set the Controlling_Argument if already set. This happens in
3295 -- the special case of _Input (see Exp_Attr, case Input).
3296
3297 if No (Controlling_Argument (Call_Node)) then
3298 Set_Controlling_Argument (Call_Node, Control);
3299 end if;
3300
3301 Arg := First_Actual (Call_Node);
3302 while Present (Arg) loop
3303 if Is_Tag_Indeterminate (Arg) then
3304 Propagate_Tag (Control, Arg);
3305 end if;
3306
3307 Next_Actual (Arg);
3308 end loop;
3309
3310 -- Add class-wide precondition check if the target of this dispatching
3311 -- call has or inherits class-wide preconditions.
3312
3313 Install_Class_Preconditions_Check (Call_Node);
3314
3315 -- Expansion of dispatching calls is suppressed on VM targets, because
3316 -- the VM back-ends directly handle the generation of dispatching calls
3317 -- and would have to undo any expansion to an indirect call.
3318
3319 if Tagged_Type_Expansion then
3320 declare
3321 Call_Typ : Entity_Id := Etype (Call_Node);
3322 Ctrl_Typ : Entity_Id := Etype (Control);
3323
3324 begin
3325 Expand_Dispatching_Call (Call_Node);
3326
3327 if Is_Class_Wide_Type (Call_Typ) then
3328 Call_Typ := Root_Type (Call_Typ);
3329 end if;
3330
3331 if Is_Class_Wide_Type (Ctrl_Typ) then
3332 Ctrl_Typ := Root_Type (Ctrl_Typ);
3333 end if;
3334
3335 -- If the controlling argument is an interface type and the type
3336 -- of Call_Node differs then we must add an implicit conversion to
3337 -- force displacement of the pointer to the object to reference
3338 -- the secondary dispatch table of the interface.
3339
3340 if Is_Interface (Ctrl_Typ)
3341 and then Ctrl_Typ /= Call_Typ
3342 then
3343 -- Cannot use Convert_To because the previous call to
3344 -- Expand_Dispatching_Call leaves decorated the Call_Node
3345 -- with the type of Control.
3346
3347 Rewrite (Call_Node,
3348 Make_Type_Conversion (Sloc (Call_Node),
3349 Subtype_Mark =>
3350 New_Occurrence_Of (Etype (Control), Sloc (Call_Node)),
3351 Expression => Relocate_Node (Call_Node)));
3352 Set_Etype (Call_Node, Etype (Control));
3353 Set_Analyzed (Call_Node);
3354
3355 Expand_Interface_Conversion (Call_Node);
3356 end if;
3357 end;
3358
3359 -- Expansion of a dispatching call results in an indirect call, which in
3360 -- turn causes current values to be killed (see Resolve_Call), so on VM
3361 -- targets we do the call here to ensure consistent warnings between VM
3362 -- and non-VM targets.
3363
3364 else
3365 Kill_Current_Values;
3366 end if;
3367 end Propagate_Tag;
3368
3369 end Sem_Disp;