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