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