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