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