OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [ada/] [sem_disp.adb] - Blame information for rev 281

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.