OSDN Git Service

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