OSDN Git Service

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