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