OSDN Git Service

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