OSDN Git Service

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