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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [ada/] [sem_ch6.adb] - Blame information for rev 20

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

Line No. Rev Author Line
1 12 jlechner
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                              S E M _ C H 6                               --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2005, 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 2,  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 COPYING.  If not, write --
19
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20
-- Boston, MA 02110-1301, USA.                                              --
21
--                                                                          --
22
-- GNAT was originally developed  by the GNAT team at  New York University. --
23
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24
--                                                                          --
25
------------------------------------------------------------------------------
26
 
27
with Atree;    use Atree;
28
with Checks;   use Checks;
29
with Debug;    use Debug;
30
with Einfo;    use Einfo;
31
with Elists;   use Elists;
32
with Errout;   use Errout;
33
with Expander; use Expander;
34
with Exp_Ch7;  use Exp_Ch7;
35
with Exp_Tss;  use Exp_Tss;
36
with Fname;    use Fname;
37
with Freeze;   use Freeze;
38
with Itypes;   use Itypes;
39
with Lib.Xref; use Lib.Xref;
40
with Namet;    use Namet;
41
with Lib;      use Lib;
42
with Nlists;   use Nlists;
43
with Nmake;    use Nmake;
44
with Opt;      use Opt;
45
with Output;   use Output;
46
with Rtsfind;  use Rtsfind;
47
with Sem;      use Sem;
48
with Sem_Cat;  use Sem_Cat;
49
with Sem_Ch3;  use Sem_Ch3;
50
with Sem_Ch4;  use Sem_Ch4;
51
with Sem_Ch5;  use Sem_Ch5;
52
with Sem_Ch8;  use Sem_Ch8;
53
with Sem_Ch10; use Sem_Ch10;
54
with Sem_Ch12; use Sem_Ch12;
55
with Sem_Disp; use Sem_Disp;
56
with Sem_Dist; use Sem_Dist;
57
with Sem_Elim; use Sem_Elim;
58
with Sem_Eval; use Sem_Eval;
59
with Sem_Mech; use Sem_Mech;
60
with Sem_Prag; use Sem_Prag;
61
with Sem_Res;  use Sem_Res;
62
with Sem_Util; use Sem_Util;
63
with Sem_Type; use Sem_Type;
64
with Sem_Warn; use Sem_Warn;
65
with Sinput;   use Sinput;
66
with Stand;    use Stand;
67
with Sinfo;    use Sinfo;
68
with Sinfo.CN; use Sinfo.CN;
69
with Snames;   use Snames;
70
with Stringt;  use Stringt;
71
with Style;
72
with Stylesw;  use Stylesw;
73
with Tbuild;   use Tbuild;
74
with Uintp;    use Uintp;
75
with Urealp;   use Urealp;
76
with Validsw;  use Validsw;
77
 
78
package body Sem_Ch6 is
79
 
80
   -----------------------
81
   -- Local Subprograms --
82
   -----------------------
83
 
84
   procedure Analyze_Return_Type (N : Node_Id);
85
   --  Subsidiary to Process_Formals: analyze subtype mark in function
86
   --  specification, in a context where the formals are visible and hide
87
   --  outer homographs.
88
 
89
   procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
90
   --  Analyze a generic subprogram body. N is the body to be analyzed, and
91
   --  Gen_Id is the defining entity Id for the corresponding spec.
92
 
93
   procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
94
   --  If a subprogram has pragma Inline and inlining is active, use generic
95
   --  machinery to build an unexpanded body for the subprogram. This body is
96
   --  subsequenty used for inline expansions at call sites. If subprogram can
97
   --  be inlined (depending on size and nature of local declarations) this
98
   --  function returns true. Otherwise subprogram body is treated normally.
99
   --  If proper warnings are enabled and the subprogram contains a construct
100
   --  that cannot be inlined, the offending construct is flagged accordingly.
101
 
102
   type Conformance_Type is
103
     (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
104
   --  Conformance type used for following call, meaning matches the
105
   --  RM definitions of the corresponding terms.
106
 
107
   procedure Check_Conformance
108
     (New_Id                   : Entity_Id;
109
      Old_Id                   : Entity_Id;
110
      Ctype                    : Conformance_Type;
111
      Errmsg                   : Boolean;
112
      Conforms                 : out Boolean;
113
      Err_Loc                  : Node_Id := Empty;
114
      Get_Inst                 : Boolean := False;
115
      Skip_Controlling_Formals : Boolean := False);
116
   --  Given two entities, this procedure checks that the profiles associated
117
   --  with these entities meet the conformance criterion given by the third
118
   --  parameter. If they conform, Conforms is set True and control returns
119
   --  to the caller. If they do not conform, Conforms is set to False, and
120
   --  in addition, if Errmsg is True on the call, proper messages are output
121
   --  to complain about the conformance failure. If Err_Loc is non_Empty
122
   --  the error messages are placed on Err_Loc, if Err_Loc is empty, then
123
   --  error messages are placed on the appropriate part of the construct
124
   --  denoted by New_Id. If Get_Inst is true, then this is a mode conformance
125
   --  against a formal access-to-subprogram type so Get_Instance_Of must
126
   --  be called.
127
 
128
   procedure Check_Overriding_Indicator
129
     (Subp          : Entity_Id;
130
      Does_Override : Boolean);
131
   --  Verify the consistency of an overriding_indicator given for subprogram
132
   --  declaration, body, renaming, or instantiation. The flag Does_Override
133
   --  is set if the scope into which we are introducing the subprogram
134
   --  contains a type-conformant subprogram that becomes hidden by the new
135
   --  subprogram.
136
 
137
   procedure Check_Subprogram_Order (N : Node_Id);
138
   --  N is the N_Subprogram_Body node for a subprogram. This routine applies
139
   --  the alpha ordering rule for N if this ordering requirement applicable.
140
 
141
   procedure Check_Returns
142
     (HSS  : Node_Id;
143
      Mode : Character;
144
      Err  : out Boolean);
145
   --  Called to check for missing return statements in a function body, or
146
   --  for returns present in a procedure body which has No_Return set. L is
147
   --  the handled statement sequence for the subprogram body. This procedure
148
   --  checks all flow paths to make sure they either have return (Mode = 'F')
149
   --  or do not have a return (Mode = 'P'). The flag Err is set if there are
150
   --  any control paths not explicitly terminated by a return in the function
151
   --  case, and is True otherwise.
152
 
153
   function Conforming_Types
154
     (T1       : Entity_Id;
155
      T2       : Entity_Id;
156
      Ctype    : Conformance_Type;
157
      Get_Inst : Boolean := False) return Boolean;
158
   --  Check that two formal parameter types conform, checking both for
159
   --  equality of base types, and where required statically matching
160
   --  subtypes, depending on the setting of Ctype.
161
 
162
   procedure Enter_Overloaded_Entity (S : Entity_Id);
163
   --  This procedure makes S, a new overloaded entity, into the first visible
164
   --  entity with that name.
165
 
166
   procedure Install_Entity (E : Entity_Id);
167
   --  Make single entity visible. Used for generic formals as well
168
 
169
   procedure Install_Formals (Id : Entity_Id);
170
   --  On entry to a subprogram body, make the formals visible. Note that
171
   --  simply placing the subprogram on the scope stack is not sufficient:
172
   --  the formals must become the current entities for their names.
173
 
174
   function Is_Non_Overriding_Operation
175
     (Prev_E : Entity_Id;
176
      New_E  : Entity_Id) return Boolean;
177
   --  Enforce the rule given in 12.3(18): a private operation in an instance
178
   --  overrides an inherited operation only if the corresponding operation
179
   --  was overriding in the generic. This can happen for primitive operations
180
   --  of types derived (in the generic unit) from formal private or formal
181
   --  derived types.
182
 
183
   procedure Make_Inequality_Operator (S : Entity_Id);
184
   --  Create the declaration for an inequality operator that is implicitly
185
   --  created by a user-defined equality operator that yields a boolean.
186
 
187
   procedure May_Need_Actuals (Fun : Entity_Id);
188
   --  Flag functions that can be called without parameters, i.e. those that
189
   --  have no parameters, or those for which defaults exist for all parameters
190
 
191
   procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
192
   --  If there is a separate spec for a subprogram or generic subprogram, the
193
   --  formals of the body are treated as references to the corresponding
194
   --  formals of the spec. This reference does not count as an actual use of
195
   --  the formal, in order to diagnose formals that are unused in the body.
196
 
197
   procedure Set_Formal_Validity (Formal_Id : Entity_Id);
198
   --  Formal_Id is an formal parameter entity. This procedure deals with
199
   --  setting the proper validity status for this entity, which depends
200
   --  on the kind of parameter and the validity checking mode.
201
 
202
   ---------------------------------------------
203
   -- Analyze_Abstract_Subprogram_Declaration --
204
   ---------------------------------------------
205
 
206
   procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
207
      Designator : constant Entity_Id :=
208
                     Analyze_Subprogram_Specification (Specification (N));
209
      Scop       : constant Entity_Id := Current_Scope;
210
 
211
   begin
212
      Generate_Definition (Designator);
213
      Set_Is_Abstract (Designator);
214
      New_Overloaded_Entity (Designator);
215
      Check_Delayed_Subprogram (Designator);
216
 
217
      Set_Categorization_From_Scope (Designator, Scop);
218
 
219
      if Ekind (Scope (Designator)) = E_Protected_Type then
220
         Error_Msg_N
221
           ("abstract subprogram not allowed in protected type", N);
222
      end if;
223
 
224
      Generate_Reference_To_Formals (Designator);
225
   end Analyze_Abstract_Subprogram_Declaration;
226
 
227
   ----------------------------
228
   -- Analyze_Function_Call  --
229
   ----------------------------
230
 
231
   procedure Analyze_Function_Call (N : Node_Id) is
232
      P      : constant Node_Id := Name (N);
233
      L      : constant List_Id := Parameter_Associations (N);
234
      Actual : Node_Id;
235
 
236
   begin
237
      Analyze (P);
238
 
239
      --  A call of the form A.B (X) may be an Ada05 call, which is rewritten
240
      --  as B (A, X). If the rewriting is successful, the call has been
241
      --  analyzed and we just return.
242
 
243
      if Nkind (P) = N_Selected_Component
244
        and then Name (N) /= P
245
        and then Is_Rewrite_Substitution (N)
246
        and then Present (Etype (N))
247
      then
248
         return;
249
      end if;
250
 
251
      --  If error analyzing name, then set Any_Type as result type and return
252
 
253
      if Etype (P) = Any_Type then
254
         Set_Etype (N, Any_Type);
255
         return;
256
      end if;
257
 
258
      --  Otherwise analyze the parameters
259
 
260
      if Present (L) then
261
         Actual := First (L);
262
         while Present (Actual) loop
263
            Analyze (Actual);
264
            Check_Parameterless_Call (Actual);
265
            Next (Actual);
266
         end loop;
267
      end if;
268
 
269
      Analyze_Call (N);
270
   end Analyze_Function_Call;
271
 
272
   -------------------------------------
273
   -- Analyze_Generic_Subprogram_Body --
274
   -------------------------------------
275
 
276
   procedure Analyze_Generic_Subprogram_Body
277
     (N      : Node_Id;
278
      Gen_Id : Entity_Id)
279
   is
280
      Gen_Decl : constant Node_Id     := Unit_Declaration_Node (Gen_Id);
281
      Kind     : constant Entity_Kind := Ekind (Gen_Id);
282
      Body_Id  : Entity_Id;
283
      New_N    : Node_Id;
284
      Spec     : Node_Id;
285
 
286
   begin
287
      --  Copy body and disable expansion while analyzing the generic For a
288
      --  stub, do not copy the stub (which would load the proper body), this
289
      --  will be done when the proper body is analyzed.
290
 
291
      if Nkind (N) /= N_Subprogram_Body_Stub then
292
         New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
293
         Rewrite (N, New_N);
294
         Start_Generic;
295
      end if;
296
 
297
      Spec := Specification (N);
298
 
299
      --  Within the body of the generic, the subprogram is callable, and
300
      --  behaves like the corresponding non-generic unit.
301
 
302
      Body_Id := Defining_Entity (Spec);
303
 
304
      if Kind = E_Generic_Procedure
305
        and then Nkind (Spec) /= N_Procedure_Specification
306
      then
307
         Error_Msg_N ("invalid body for generic procedure ", Body_Id);
308
         return;
309
 
310
      elsif Kind = E_Generic_Function
311
        and then Nkind (Spec) /= N_Function_Specification
312
      then
313
         Error_Msg_N ("invalid body for generic function ", Body_Id);
314
         return;
315
      end if;
316
 
317
      Set_Corresponding_Body (Gen_Decl, Body_Id);
318
 
319
      if Has_Completion (Gen_Id)
320
        and then Nkind (Parent (N)) /= N_Subunit
321
      then
322
         Error_Msg_N ("duplicate generic body", N);
323
         return;
324
      else
325
         Set_Has_Completion (Gen_Id);
326
      end if;
327
 
328
      if Nkind (N) = N_Subprogram_Body_Stub then
329
         Set_Ekind (Defining_Entity (Specification (N)), Kind);
330
      else
331
         Set_Corresponding_Spec (N, Gen_Id);
332
      end if;
333
 
334
      if Nkind (Parent (N)) = N_Compilation_Unit then
335
         Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
336
      end if;
337
 
338
      --  Make generic parameters immediately visible in the body. They are
339
      --  needed to process the formals declarations. Then make the formals
340
      --  visible in a separate step.
341
 
342
      New_Scope (Gen_Id);
343
 
344
      declare
345
         E         : Entity_Id;
346
         First_Ent : Entity_Id;
347
 
348
      begin
349
         First_Ent := First_Entity (Gen_Id);
350
 
351
         E := First_Ent;
352
         while Present (E) and then not Is_Formal (E) loop
353
            Install_Entity (E);
354
            Next_Entity (E);
355
         end loop;
356
 
357
         Set_Use (Generic_Formal_Declarations (Gen_Decl));
358
 
359
         --  Now generic formals are visible, and the specification can be
360
         --  analyzed, for subsequent conformance check.
361
 
362
         Body_Id := Analyze_Subprogram_Specification (Spec);
363
 
364
         --  Make formal parameters visible
365
 
366
         if Present (E) then
367
 
368
            --  E is the first formal parameter, we loop through the formals
369
            --  installing them so that they will be visible.
370
 
371
            Set_First_Entity (Gen_Id, E);
372
            while Present (E) loop
373
               Install_Entity (E);
374
               Next_Formal (E);
375
            end loop;
376
         end if;
377
 
378
         --  Visible generic entity is callable within its own body
379
 
380
         Set_Ekind (Gen_Id, Ekind (Body_Id));
381
         Set_Ekind (Body_Id, E_Subprogram_Body);
382
         Set_Convention (Body_Id, Convention (Gen_Id));
383
         Set_Scope (Body_Id, Scope (Gen_Id));
384
         Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
385
 
386
         if Nkind (N) = N_Subprogram_Body_Stub then
387
 
388
            --  No body to analyze, so restore state of generic unit
389
 
390
            Set_Ekind (Gen_Id, Kind);
391
            Set_Ekind (Body_Id, Kind);
392
 
393
            if Present (First_Ent) then
394
               Set_First_Entity (Gen_Id, First_Ent);
395
            end if;
396
 
397
            End_Scope;
398
            return;
399
         end if;
400
 
401
         --  If this is a compilation unit, it must be made visible explicitly,
402
         --  because the compilation of the declaration, unlike other library
403
         --  unit declarations, does not. If it is not a unit, the following
404
         --  is redundant but harmless.
405
 
406
         Set_Is_Immediately_Visible (Gen_Id);
407
         Reference_Body_Formals (Gen_Id, Body_Id);
408
 
409
         Set_Actual_Subtypes (N, Current_Scope);
410
         Analyze_Declarations (Declarations (N));
411
         Check_Completion;
412
         Analyze (Handled_Statement_Sequence (N));
413
 
414
         Save_Global_References (Original_Node (N));
415
 
416
         --  Prior to exiting the scope, include generic formals again (if any
417
         --  are present) in the set of local entities.
418
 
419
         if Present (First_Ent) then
420
            Set_First_Entity (Gen_Id, First_Ent);
421
         end if;
422
 
423
         Check_References (Gen_Id);
424
      end;
425
 
426
      Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
427
      End_Scope;
428
      Check_Subprogram_Order (N);
429
 
430
      --  Outside of its body, unit is generic again
431
 
432
      Set_Ekind (Gen_Id, Kind);
433
      Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
434
      Style.Check_Identifier (Body_Id, Gen_Id);
435
      End_Generic;
436
   end Analyze_Generic_Subprogram_Body;
437
 
438
   -----------------------------
439
   -- Analyze_Operator_Symbol --
440
   -----------------------------
441
 
442
   --  An operator symbol such as "+" or "and" may appear in context where the
443
   --  literal denotes an entity name, such as "+"(x, y) or in context when it
444
   --  is just a string, as in (conjunction = "or"). In these cases the parser
445
   --  generates this node, and the semantics does the disambiguation. Other
446
   --  such case are actuals in an instantiation, the generic unit in an
447
   --  instantiation, and pragma arguments.
448
 
449
   procedure Analyze_Operator_Symbol (N : Node_Id) is
450
      Par : constant Node_Id := Parent (N);
451
 
452
   begin
453
      if        (Nkind (Par) = N_Function_Call and then N = Name (Par))
454
        or else  Nkind (Par) = N_Function_Instantiation
455
        or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
456
        or else (Nkind (Par) = N_Pragma_Argument_Association
457
                   and then not Is_Pragma_String_Literal (Par))
458
        or else  Nkind (Par) = N_Subprogram_Renaming_Declaration
459
        or else  (Nkind (Par) = N_Attribute_Reference
460
                   and then Attribute_Name (Par) /= Name_Value)
461
      then
462
         Find_Direct_Name (N);
463
 
464
      else
465
         Change_Operator_Symbol_To_String_Literal (N);
466
         Analyze (N);
467
      end if;
468
   end Analyze_Operator_Symbol;
469
 
470
   -----------------------------------
471
   -- Analyze_Parameter_Association --
472
   -----------------------------------
473
 
474
   procedure Analyze_Parameter_Association (N : Node_Id) is
475
   begin
476
      Analyze (Explicit_Actual_Parameter (N));
477
   end Analyze_Parameter_Association;
478
 
479
   ----------------------------
480
   -- Analyze_Procedure_Call --
481
   ----------------------------
482
 
483
   procedure Analyze_Procedure_Call (N : Node_Id) is
484
      Loc     : constant Source_Ptr := Sloc (N);
485
      P       : constant Node_Id    := Name (N);
486
      Actuals : constant List_Id    := Parameter_Associations (N);
487
      Actual  : Node_Id;
488
      New_N   : Node_Id;
489
 
490
      procedure Analyze_Call_And_Resolve;
491
      --  Do Analyze and Resolve calls for procedure call
492
 
493
      ------------------------------
494
      -- Analyze_Call_And_Resolve --
495
      ------------------------------
496
 
497
      procedure Analyze_Call_And_Resolve is
498
      begin
499
         if Nkind (N) = N_Procedure_Call_Statement then
500
            Analyze_Call (N);
501
            Resolve (N, Standard_Void_Type);
502
         else
503
            Analyze (N);
504
         end if;
505
      end Analyze_Call_And_Resolve;
506
 
507
   --  Start of processing for Analyze_Procedure_Call
508
 
509
   begin
510
      --  The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
511
      --  a procedure call or an entry call. The prefix may denote an access
512
      --  to subprogram type, in which case an implicit dereference applies.
513
      --  If the prefix is an indexed component (without implicit defererence)
514
      --  then the construct denotes a call to a member of an entire family.
515
      --  If the prefix is a simple name, it may still denote a call to a
516
      --  parameterless member of an entry family. Resolution of these various
517
      --  interpretations is delicate.
518
 
519
      Analyze (P);
520
 
521
      --  If this is a call of the form Obj.Op, the call may have been
522
      --  analyzed and possibly rewritten into a block, in which case
523
      --  we are done.
524
 
525
      if Analyzed (N) then
526
         return;
527
      end if;
528
 
529
      --  If error analyzing prefix, then set Any_Type as result and return
530
 
531
      if Etype (P) = Any_Type then
532
         Set_Etype (N, Any_Type);
533
         return;
534
      end if;
535
 
536
      --  Otherwise analyze the parameters
537
 
538
      if Present (Actuals) then
539
         Actual := First (Actuals);
540
 
541
         while Present (Actual) loop
542
            Analyze (Actual);
543
            Check_Parameterless_Call (Actual);
544
            Next (Actual);
545
         end loop;
546
      end if;
547
 
548
      --  Special processing for Elab_Spec and Elab_Body calls
549
 
550
      if Nkind (P) = N_Attribute_Reference
551
        and then (Attribute_Name (P) = Name_Elab_Spec
552
                   or else Attribute_Name (P) = Name_Elab_Body)
553
      then
554
         if Present (Actuals) then
555
            Error_Msg_N
556
              ("no parameters allowed for this call", First (Actuals));
557
            return;
558
         end if;
559
 
560
         Set_Etype (N, Standard_Void_Type);
561
         Set_Analyzed (N);
562
 
563
      elsif Is_Entity_Name (P)
564
        and then Is_Record_Type (Etype (Entity (P)))
565
        and then Remote_AST_I_Dereference (P)
566
      then
567
         return;
568
 
569
      elsif Is_Entity_Name (P)
570
        and then Ekind (Entity (P)) /= E_Entry_Family
571
      then
572
         if Is_Access_Type (Etype (P))
573
           and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
574
           and then No (Actuals)
575
           and then Comes_From_Source (N)
576
         then
577
            Error_Msg_N ("missing explicit dereference in call", N);
578
         end if;
579
 
580
         Analyze_Call_And_Resolve;
581
 
582
      --  If the prefix is the simple name of an entry family, this is
583
      --  a parameterless call from within the task body itself.
584
 
585
      elsif Is_Entity_Name (P)
586
        and then Nkind (P) = N_Identifier
587
        and then Ekind (Entity (P)) = E_Entry_Family
588
        and then Present (Actuals)
589
        and then No (Next (First (Actuals)))
590
      then
591
         --  Can be call to parameterless entry family. What appears to be the
592
         --  sole argument is in fact the entry index. Rewrite prefix of node
593
         --  accordingly. Source representation is unchanged by this
594
         --  transformation.
595
 
596
         New_N :=
597
           Make_Indexed_Component (Loc,
598
             Prefix =>
599
               Make_Selected_Component (Loc,
600
                 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
601
                 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
602
             Expressions => Actuals);
603
         Set_Name (N, New_N);
604
         Set_Etype (New_N, Standard_Void_Type);
605
         Set_Parameter_Associations (N, No_List);
606
         Analyze_Call_And_Resolve;
607
 
608
      elsif Nkind (P) = N_Explicit_Dereference then
609
         if Ekind (Etype (P)) = E_Subprogram_Type then
610
            Analyze_Call_And_Resolve;
611
         else
612
            Error_Msg_N ("expect access to procedure in call", P);
613
         end if;
614
 
615
      --  The name can be a selected component or an indexed component that
616
      --  yields an access to subprogram. Such a prefix is legal if the call
617
      --  has parameter associations.
618
 
619
      elsif Is_Access_Type (Etype (P))
620
        and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
621
      then
622
         if Present (Actuals) then
623
            Analyze_Call_And_Resolve;
624
         else
625
            Error_Msg_N ("missing explicit dereference in call ", N);
626
         end if;
627
 
628
      --  If not an access to subprogram, then the prefix must resolve to the
629
      --  name of an entry, entry family, or protected operation.
630
 
631
      --  For the case of a simple entry call, P is a selected component where
632
      --  the prefix is the task and the selector name is the entry. A call to
633
      --  a protected procedure will have the same syntax. If the protected
634
      --  object contains overloaded operations, the entity may appear as a
635
      --  function, the context will select the operation whose type is Void.
636
 
637
      elsif Nkind (P) = N_Selected_Component
638
        and then (Ekind (Entity (Selector_Name (P))) = E_Entry
639
                    or else
640
                  Ekind (Entity (Selector_Name (P))) = E_Procedure
641
                    or else
642
                  Ekind (Entity (Selector_Name (P))) = E_Function)
643
      then
644
         Analyze_Call_And_Resolve;
645
 
646
      elsif Nkind (P) = N_Selected_Component
647
        and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
648
        and then Present (Actuals)
649
        and then No (Next (First (Actuals)))
650
      then
651
         --  Can be call to parameterless entry family. What appears to be the
652
         --  sole argument is in fact the entry index. Rewrite prefix of node
653
         --  accordingly. Source representation is unchanged by this
654
         --  transformation.
655
 
656
         New_N :=
657
           Make_Indexed_Component (Loc,
658
             Prefix => New_Copy (P),
659
             Expressions => Actuals);
660
         Set_Name (N, New_N);
661
         Set_Etype (New_N, Standard_Void_Type);
662
         Set_Parameter_Associations (N, No_List);
663
         Analyze_Call_And_Resolve;
664
 
665
      --  For the case of a reference to an element of an entry family, P is
666
      --  an indexed component whose prefix is a selected component (task and
667
      --  entry family), and whose index is the entry family index.
668
 
669
      elsif Nkind (P) = N_Indexed_Component
670
        and then Nkind (Prefix (P)) = N_Selected_Component
671
        and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
672
      then
673
         Analyze_Call_And_Resolve;
674
 
675
      --  If the prefix is the name of an entry family, it is a call from
676
      --  within the task body itself.
677
 
678
      elsif Nkind (P) = N_Indexed_Component
679
        and then Nkind (Prefix (P)) = N_Identifier
680
        and then Ekind (Entity (Prefix (P))) = E_Entry_Family
681
      then
682
         New_N :=
683
           Make_Selected_Component (Loc,
684
             Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
685
             Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
686
         Rewrite (Prefix (P), New_N);
687
         Analyze (P);
688
         Analyze_Call_And_Resolve;
689
 
690
      --  Anything else is an error
691
 
692
      else
693
         Error_Msg_N ("invalid procedure or entry call", N);
694
      end if;
695
   end Analyze_Procedure_Call;
696
 
697
   ------------------------------
698
   -- Analyze_Return_Statement --
699
   ------------------------------
700
 
701
   procedure Analyze_Return_Statement (N : Node_Id) is
702
      Loc      : constant Source_Ptr := Sloc (N);
703
      Expr     : Node_Id;
704
      Scope_Id : Entity_Id;
705
      Kind     : Entity_Kind;
706
      R_Type   : Entity_Id;
707
 
708
   begin
709
      --  Find subprogram or accept statement enclosing the return statement
710
 
711
      Scope_Id := Empty;
712
      for J in reverse 0 .. Scope_Stack.Last loop
713
         Scope_Id := Scope_Stack.Table (J).Entity;
714
         exit when Ekind (Scope_Id) /= E_Block and then
715
                   Ekind (Scope_Id) /= E_Loop;
716
      end loop;
717
 
718
      pragma Assert (Present (Scope_Id));
719
 
720
      Kind := Ekind (Scope_Id);
721
      Expr := Expression (N);
722
 
723
      if Kind /= E_Function
724
        and then Kind /= E_Generic_Function
725
        and then Kind /= E_Procedure
726
        and then Kind /= E_Generic_Procedure
727
        and then Kind /= E_Entry
728
        and then Kind /= E_Entry_Family
729
      then
730
         Error_Msg_N ("illegal context for return statement", N);
731
 
732
      elsif Present (Expr) then
733
         if Kind = E_Function or else Kind = E_Generic_Function then
734
            Set_Return_Present (Scope_Id);
735
            R_Type := Etype (Scope_Id);
736
            Set_Return_Type (N, R_Type);
737
            Analyze_And_Resolve (Expr, R_Type);
738
 
739
            --  Ada 2005 (AI-318-02): When the result type is an anonymous
740
            --  access type, apply an implicit conversion of the expression
741
            --  to that type to force appropriate static and run-time
742
            --  accessibility checks.
743
 
744
            if Ada_Version >= Ada_05
745
              and then Ekind (R_Type) = E_Anonymous_Access_Type
746
            then
747
               Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
748
               Analyze_And_Resolve (Expr, R_Type);
749
            end if;
750
 
751
            if (Is_Class_Wide_Type (Etype (Expr))
752
                 or else Is_Dynamically_Tagged (Expr))
753
              and then not Is_Class_Wide_Type (R_Type)
754
            then
755
               Error_Msg_N
756
                 ("dynamically tagged expression not allowed!", Expr);
757
            end if;
758
 
759
            Apply_Constraint_Check (Expr, R_Type);
760
 
761
            --  Ada 2005 (AI-318-02): Return-by-reference types have been
762
            --  removed and replaced by anonymous access results. This is
763
            --  an incompatibility with Ada 95. Not clear whether this
764
            --  should be enforced yet or perhaps controllable with a
765
            --  special switch. ???
766
 
767
            --  if Ada_Version >= Ada_05
768
            --    and then Is_Limited_Type (R_Type)
769
            --    and then Nkind (Expr) /= N_Aggregate
770
            --    and then Nkind (Expr) /= N_Extension_Aggregate
771
            --    and then Nkind (Expr) /= N_Function_Call
772
            --  then
773
            --     Error_Msg_N
774
            --       ("(Ada 2005) illegal operand for limited return", N);
775
            --  end if;
776
 
777
            --  ??? A real run-time accessibility check is needed in cases
778
            --  involving dereferences of access parameters. For now we just
779
            --  check the static cases.
780
 
781
            if Is_Return_By_Reference_Type (Etype (Scope_Id))
782
              and then Object_Access_Level (Expr)
783
                > Subprogram_Access_Level (Scope_Id)
784
            then
785
               Rewrite (N,
786
                 Make_Raise_Program_Error (Loc,
787
                   Reason => PE_Accessibility_Check_Failed));
788
               Analyze (N);
789
 
790
               Error_Msg_N
791
                 ("cannot return a local value by reference?", N);
792
               Error_Msg_NE
793
                 ("& will be raised at run time?!",
794
                  N, Standard_Program_Error);
795
            end if;
796
 
797
         elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
798
            Error_Msg_N ("procedure cannot return value (use function)", N);
799
 
800
         else
801
            Error_Msg_N ("accept statement cannot return value", N);
802
         end if;
803
 
804
      --  No expression present
805
 
806
      else
807
         if Kind = E_Function or Kind = E_Generic_Function then
808
            Error_Msg_N ("missing expression in return from function", N);
809
         end if;
810
 
811
         if (Ekind (Scope_Id) = E_Procedure
812
              or else Ekind (Scope_Id) = E_Generic_Procedure)
813
           and then No_Return (Scope_Id)
814
         then
815
            Error_Msg_N
816
              ("RETURN statement not allowed (No_Return)", N);
817
         end if;
818
      end if;
819
 
820
      Check_Unreachable_Code (N);
821
   end Analyze_Return_Statement;
822
 
823
   -------------------------
824
   -- Analyze_Return_Type --
825
   -------------------------
826
 
827
   procedure Analyze_Return_Type (N : Node_Id) is
828
      Designator : constant Entity_Id := Defining_Entity (N);
829
      Typ        : Entity_Id := Empty;
830
 
831
   begin
832
      if Result_Definition (N) /= Error then
833
         if Nkind (Result_Definition (N)) = N_Access_Definition then
834
            Typ := Access_Definition (N, Result_Definition (N));
835
            Set_Parent (Typ, Result_Definition (N));
836
            Set_Is_Local_Anonymous_Access (Typ);
837
            Set_Etype (Designator, Typ);
838
 
839
            --  Ada 2005 (AI-231): Static checks
840
 
841
            --  Null_Exclusion_Static_Checks needs to be extended to handle
842
            --  null exclusion checks for function specifications. ???
843
 
844
            --  if Null_Exclusion_Present (N) then
845
            --     Null_Exclusion_Static_Checks (Param_Spec);
846
            --  end if;
847
 
848
         --  Subtype_Mark case
849
 
850
         else
851
            Find_Type (Result_Definition (N));
852
            Typ := Entity (Result_Definition (N));
853
            Set_Etype (Designator, Typ);
854
 
855
            if Ekind (Typ) = E_Incomplete_Type
856
              or else (Is_Class_Wide_Type (Typ)
857
                         and then
858
                           Ekind (Root_Type (Typ)) = E_Incomplete_Type)
859
            then
860
               Error_Msg_N
861
                 ("invalid use of incomplete type", Result_Definition (N));
862
            end if;
863
         end if;
864
 
865
      else
866
         Set_Etype (Designator, Any_Type);
867
      end if;
868
   end Analyze_Return_Type;
869
 
870
   -----------------------------
871
   -- Analyze_Subprogram_Body --
872
   -----------------------------
873
 
874
   --  This procedure is called for regular subprogram bodies, generic bodies,
875
   --  and for subprogram stubs of both kinds. In the case of stubs, only the
876
   --  specification matters, and is used to create a proper declaration for
877
   --  the subprogram, or to perform conformance checks.
878
 
879
   procedure Analyze_Subprogram_Body (N : Node_Id) is
880
      Loc          : constant Source_Ptr := Sloc (N);
881
      Body_Spec    : constant Node_Id    := Specification (N);
882
      Body_Id      : Entity_Id           := Defining_Entity (Body_Spec);
883
      Prev_Id      : constant Entity_Id  := Current_Entity_In_Scope (Body_Id);
884
      Body_Deleted : constant Boolean    := False;
885
 
886
      HSS          : Node_Id;
887
      Spec_Id      : Entity_Id;
888
      Spec_Decl    : Node_Id   := Empty;
889
      Last_Formal  : Entity_Id := Empty;
890
      Conformant   : Boolean;
891
      Missing_Ret  : Boolean;
892
      P_Ent        : Entity_Id;
893
 
894
      procedure Check_Inline_Pragma (Spec : in out Node_Id);
895
      --  Look ahead to recognize a pragma that may appear after the body.
896
      --  If there is a previous spec, check that it appears in the same
897
      --  declarative part. If the pragma is Inline_Always, perform inlining
898
      --  unconditionally, otherwise only if Front_End_Inlining is requested.
899
      --  If the body acts as a spec, and inlining is required, we create a
900
      --  subprogram declaration for it, in order to attach the body to inline.
901
 
902
      procedure Copy_Parameter_List (Plist : List_Id);
903
      --  Comment required ???
904
 
905
      procedure Verify_Overriding_Indicator;
906
      --  If there was a previous spec, the entity has been entered in the
907
      --  current scope previously. If the body itself carries an overriding
908
      --  indicator, check that it is consistent with the known status of the
909
      --  entity.
910
 
911
      -------------------------
912
      -- Check_Inline_Pragma --
913
      -------------------------
914
 
915
      procedure Check_Inline_Pragma (Spec : in out Node_Id) is
916
         Prag  : Node_Id;
917
         Plist : List_Id;
918
 
919
      begin
920
         if not Expander_Active then
921
            return;
922
         end if;
923
 
924
         if Is_List_Member (N)
925
           and then Present (Next (N))
926
           and then Nkind (Next (N)) = N_Pragma
927
         then
928
            Prag := Next (N);
929
 
930
            if Nkind (Prag) = N_Pragma
931
              and then
932
                 (Get_Pragma_Id (Chars (Prag)) = Pragma_Inline_Always
933
                  or else
934
                    (Front_End_Inlining
935
                     and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline))
936
              and then
937
                 Chars
938
                   (Expression (First (Pragma_Argument_Associations (Prag))))
939
                      = Chars (Body_Id)
940
            then
941
               Prag := Next (N);
942
            else
943
               Prag := Empty;
944
            end if;
945
         else
946
            Prag := Empty;
947
         end if;
948
 
949
         if Present (Prag) then
950
            if Present (Spec_Id) then
951
               if List_Containing (N) =
952
                 List_Containing (Unit_Declaration_Node (Spec_Id))
953
               then
954
                  Analyze (Prag);
955
               end if;
956
 
957
            else
958
               --  Create a subprogram declaration, to make treatment uniform.
959
 
960
               declare
961
                  Subp : constant Entity_Id :=
962
                    Make_Defining_Identifier (Loc, Chars (Body_Id));
963
                  Decl : constant Node_Id :=
964
                    Make_Subprogram_Declaration (Loc,
965
                      Specification =>  New_Copy_Tree (Specification (N)));
966
               begin
967
                  Set_Defining_Unit_Name (Specification (Decl), Subp);
968
 
969
                  if Present (First_Formal (Body_Id)) then
970
                     Plist := New_List;
971
                     Copy_Parameter_List (Plist);
972
                     Set_Parameter_Specifications
973
                       (Specification (Decl), Plist);
974
                  end if;
975
 
976
                  Insert_Before (N, Decl);
977
                  Analyze (Decl);
978
                  Analyze (Prag);
979
                  Set_Has_Pragma_Inline (Subp);
980
 
981
                  if Get_Pragma_Id (Chars (Prag)) = Pragma_Inline_Always then
982
                     Set_Is_Inlined (Subp);
983
                     Set_Next_Rep_Item (Prag, First_Rep_Item (Subp));
984
                     Set_First_Rep_Item (Subp, Prag);
985
                  end if;
986
 
987
                  Spec := Subp;
988
               end;
989
            end if;
990
         end if;
991
      end Check_Inline_Pragma;
992
 
993
      -------------------------
994
      -- Copy_Parameter_List --
995
      -------------------------
996
 
997
      procedure Copy_Parameter_List (Plist : List_Id) is
998
         Formal : Entity_Id;
999
 
1000
      begin
1001
         Formal := First_Formal (Body_Id);
1002
 
1003
         while Present (Formal) loop
1004
            Append
1005
              (Make_Parameter_Specification (Loc,
1006
                Defining_Identifier =>
1007
                  Make_Defining_Identifier (Sloc (Formal),
1008
                    Chars => Chars (Formal)),
1009
                In_Present  => In_Present (Parent (Formal)),
1010
                Out_Present => Out_Present (Parent (Formal)),
1011
             Parameter_Type =>
1012
                  New_Reference_To (Etype (Formal), Loc),
1013
                Expression =>
1014
                  New_Copy_Tree (Expression (Parent (Formal)))),
1015
              Plist);
1016
 
1017
            Next_Formal (Formal);
1018
         end loop;
1019
      end Copy_Parameter_List;
1020
 
1021
      ---------------------------------
1022
      -- Verify_Overriding_Indicator --
1023
      ---------------------------------
1024
 
1025
      procedure Verify_Overriding_Indicator is
1026
      begin
1027
         if Must_Override (Body_Spec)
1028
           and then not Is_Overriding_Operation (Spec_Id)
1029
         then
1030
            Error_Msg_NE
1031
              ("subprogram& is not overriding", Body_Spec, Spec_Id);
1032
 
1033
         elsif Must_Not_Override (Body_Spec)
1034
              and then Is_Overriding_Operation (Spec_Id)
1035
         then
1036
            Error_Msg_NE
1037
              ("subprogram& overrides inherited operation",
1038
                 Body_Spec, Spec_Id);
1039
         end if;
1040
      end Verify_Overriding_Indicator;
1041
 
1042
   --  Start of processing for Analyze_Subprogram_Body
1043
 
1044
   begin
1045
      if Debug_Flag_C then
1046
         Write_Str ("====  Compiling subprogram body ");
1047
         Write_Name (Chars (Body_Id));
1048
         Write_Str (" from ");
1049
         Write_Location (Loc);
1050
         Write_Eol;
1051
      end if;
1052
 
1053
      Trace_Scope (N, Body_Id, " Analyze subprogram");
1054
 
1055
      --  Generic subprograms are handled separately. They always have a
1056
      --  generic specification. Determine whether current scope has a
1057
      --  previous declaration.
1058
 
1059
      --  If the subprogram body is defined within an instance of the same
1060
      --  name, the instance appears as a package renaming, and will be hidden
1061
      --  within the subprogram.
1062
 
1063
      if Present (Prev_Id)
1064
        and then not Is_Overloadable (Prev_Id)
1065
        and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1066
                   or else Comes_From_Source (Prev_Id))
1067
      then
1068
         if Is_Generic_Subprogram (Prev_Id) then
1069
            Spec_Id := Prev_Id;
1070
            Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1071
            Set_Is_Child_Unit       (Body_Id, Is_Child_Unit       (Spec_Id));
1072
 
1073
            Analyze_Generic_Subprogram_Body (N, Spec_Id);
1074
            return;
1075
 
1076
         else
1077
            --  Previous entity conflicts with subprogram name. Attempting to
1078
            --  enter name will post error.
1079
 
1080
            Enter_Name (Body_Id);
1081
            return;
1082
         end if;
1083
 
1084
      --  Non-generic case, find the subprogram declaration, if one was seen,
1085
      --  or enter new overloaded entity in the current scope. If the
1086
      --  Current_Entity is the Body_Id itself, the unit is being analyzed as
1087
      --  part of the context of one of its subunits. No need to redo the
1088
      --  analysis.
1089
 
1090
      elsif Prev_Id = Body_Id
1091
        and then Has_Completion (Body_Id)
1092
      then
1093
         return;
1094
 
1095
      else
1096
         Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1097
 
1098
         if Nkind (N) = N_Subprogram_Body_Stub
1099
           or else No (Corresponding_Spec (N))
1100
         then
1101
            Spec_Id := Find_Corresponding_Spec (N);
1102
 
1103
            --  If this is a duplicate body, no point in analyzing it
1104
 
1105
            if Error_Posted (N) then
1106
               return;
1107
            end if;
1108
 
1109
            --  A subprogram body should cause freezing of its own declaration,
1110
            --  but if there was no previous explicit declaration, then the
1111
            --  subprogram will get frozen too late (there may be code within
1112
            --  the body that depends on the subprogram having been frozen,
1113
            --  such as uses of extra formals), so we force it to be frozen
1114
            --  here. Same holds if the body and the spec are compilation
1115
            --  units.
1116
 
1117
            if No (Spec_Id) then
1118
               Freeze_Before (N, Body_Id);
1119
 
1120
            elsif Nkind (Parent (N)) = N_Compilation_Unit then
1121
               Freeze_Before (N, Spec_Id);
1122
            end if;
1123
         else
1124
            Spec_Id := Corresponding_Spec (N);
1125
         end if;
1126
      end if;
1127
 
1128
      --  Do not inline any subprogram that contains nested subprograms, since
1129
      --  the backend inlining circuit seems to generate uninitialized
1130
      --  references in this case. We know this happens in the case of front
1131
      --  end ZCX support, but it also appears it can happen in other cases as
1132
      --  well. The backend often rejects attempts to inline in the case of
1133
      --  nested procedures anyway, so little if anything is lost by this.
1134
      --  Note that this is test is for the benefit of the back-end. There is
1135
      --  a separate test for front-end inlining that also rejects nested
1136
      --  subprograms.
1137
 
1138
      --  Do not do this test if errors have been detected, because in some
1139
      --  error cases, this code blows up, and we don't need it anyway if
1140
      --  there have been errors, since we won't get to the linker anyway.
1141
 
1142
      if Comes_From_Source (Body_Id)
1143
        and then Serious_Errors_Detected = 0
1144
      then
1145
         P_Ent := Body_Id;
1146
         loop
1147
            P_Ent := Scope (P_Ent);
1148
            exit when No (P_Ent) or else P_Ent = Standard_Standard;
1149
 
1150
            if Is_Subprogram (P_Ent) then
1151
               Set_Is_Inlined (P_Ent, False);
1152
 
1153
               if Comes_From_Source (P_Ent)
1154
                 and then Has_Pragma_Inline (P_Ent)
1155
               then
1156
                  Cannot_Inline
1157
                    ("cannot inline& (nested subprogram)?",
1158
                     N, P_Ent);
1159
               end if;
1160
            end if;
1161
         end loop;
1162
      end if;
1163
 
1164
      Check_Inline_Pragma (Spec_Id);
1165
 
1166
      --  Case of fully private operation in the body of the protected type.
1167
      --  We must create a declaration for the subprogram, in order to attach
1168
      --  the protected subprogram that will be used in internal calls.
1169
 
1170
      if No (Spec_Id)
1171
        and then Comes_From_Source (N)
1172
        and then Is_Protected_Type (Current_Scope)
1173
      then
1174
         declare
1175
            Decl     : Node_Id;
1176
            Plist    : List_Id;
1177
            Formal   : Entity_Id;
1178
            New_Spec : Node_Id;
1179
 
1180
         begin
1181
            Formal := First_Formal (Body_Id);
1182
 
1183
            --  The protected operation always has at least one formal, namely
1184
            --  the object itself, but it is only placed in the parameter list
1185
            --  if expansion is enabled.
1186
 
1187
            if Present (Formal)
1188
              or else Expander_Active
1189
            then
1190
               Plist := New_List;
1191
 
1192
            else
1193
               Plist := No_List;
1194
            end if;
1195
 
1196
            Copy_Parameter_List (Plist);
1197
 
1198
            if Nkind (Body_Spec) = N_Procedure_Specification then
1199
               New_Spec :=
1200
                 Make_Procedure_Specification (Loc,
1201
                    Defining_Unit_Name =>
1202
                      Make_Defining_Identifier (Sloc (Body_Id),
1203
                        Chars => Chars (Body_Id)),
1204
                    Parameter_Specifications => Plist);
1205
            else
1206
               New_Spec :=
1207
                 Make_Function_Specification (Loc,
1208
                    Defining_Unit_Name =>
1209
                      Make_Defining_Identifier (Sloc (Body_Id),
1210
                        Chars => Chars (Body_Id)),
1211
                    Parameter_Specifications => Plist,
1212
                    Result_Definition =>
1213
                      New_Occurrence_Of (Etype (Body_Id), Loc));
1214
            end if;
1215
 
1216
            Decl :=
1217
              Make_Subprogram_Declaration (Loc,
1218
                Specification => New_Spec);
1219
            Insert_Before (N, Decl);
1220
            Spec_Id := Defining_Unit_Name (New_Spec);
1221
 
1222
            --  Indicate that the entity comes from source, to ensure that
1223
            --  cross-reference information is properly generated. The body
1224
            --  itself is rewritten during expansion, and the body entity will
1225
            --  not appear in calls to the operation.
1226
 
1227
            Set_Comes_From_Source (Spec_Id, True);
1228
            Analyze (Decl);
1229
            Set_Has_Completion (Spec_Id);
1230
            Set_Convention (Spec_Id, Convention_Protected);
1231
         end;
1232
 
1233
      elsif Present (Spec_Id) then
1234
         Spec_Decl := Unit_Declaration_Node (Spec_Id);
1235
         Verify_Overriding_Indicator;
1236
      end if;
1237
 
1238
      --  Place subprogram on scope stack, and make formals visible. If there
1239
      --  is a spec, the visible entity remains that of the spec.
1240
 
1241
      if Present (Spec_Id) then
1242
         Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1243
 
1244
         if Is_Child_Unit (Spec_Id) then
1245
            Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1246
         end if;
1247
 
1248
         if Style_Check then
1249
            Style.Check_Identifier (Body_Id, Spec_Id);
1250
         end if;
1251
 
1252
         Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1253
         Set_Is_Child_Unit       (Body_Id, Is_Child_Unit       (Spec_Id));
1254
 
1255
         if Is_Abstract (Spec_Id) then
1256
            Error_Msg_N ("an abstract subprogram cannot have a body", N);
1257
            return;
1258
         else
1259
            Set_Convention (Body_Id, Convention (Spec_Id));
1260
            Set_Has_Completion (Spec_Id);
1261
 
1262
            if Is_Protected_Type (Scope (Spec_Id)) then
1263
               Set_Privals_Chain (Spec_Id, New_Elmt_List);
1264
            end if;
1265
 
1266
            --  If this is a body generated for a renaming, do not check for
1267
            --  full conformance. The check is redundant, because the spec of
1268
            --  the body is a copy of the spec in the renaming declaration,
1269
            --  and the test can lead to spurious errors on nested defaults.
1270
 
1271
            if Present (Spec_Decl)
1272
              and then not Comes_From_Source (N)
1273
              and then
1274
                (Nkind (Original_Node (Spec_Decl)) =
1275
                                        N_Subprogram_Renaming_Declaration
1276
                   or else (Present (Corresponding_Body (Spec_Decl))
1277
                              and then
1278
                                Nkind (Unit_Declaration_Node
1279
                                        (Corresponding_Body (Spec_Decl))) =
1280
                                           N_Subprogram_Renaming_Declaration))
1281
            then
1282
               Conformant := True;
1283
            else
1284
               Check_Conformance
1285
                 (Body_Id, Spec_Id,
1286
                   Fully_Conformant, True, Conformant, Body_Id);
1287
            end if;
1288
 
1289
            --  If the body is not fully conformant, we have to decide if we
1290
            --  should analyze it or not. If it has a really messed up profile
1291
            --  then we probably should not analyze it, since we will get too
1292
            --  many bogus messages.
1293
 
1294
            --  Our decision is to go ahead in the non-fully conformant case
1295
            --  only if it is at least mode conformant with the spec. Note
1296
            --  that the call to Check_Fully_Conformant has issued the proper
1297
            --  error messages to complain about the lack of conformance.
1298
 
1299
            if not Conformant
1300
              and then not Mode_Conformant (Body_Id, Spec_Id)
1301
            then
1302
               return;
1303
            end if;
1304
         end if;
1305
 
1306
         if Spec_Id /= Body_Id then
1307
            Reference_Body_Formals (Spec_Id, Body_Id);
1308
         end if;
1309
 
1310
         if Nkind (N) /= N_Subprogram_Body_Stub then
1311
            Set_Corresponding_Spec (N, Spec_Id);
1312
 
1313
            --  Ada 2005 (AI-345): Restore the correct Etype: here we undo the
1314
            --  work done by Analyze_Subprogram_Specification to allow the
1315
            --  overriding of task, protected and interface primitives.
1316
 
1317
            if Comes_From_Source (Spec_Id)
1318
              and then Present (First_Entity (Spec_Id))
1319
              and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
1320
              and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
1321
              and then Present (Abstract_Interfaces
1322
                                (Etype (First_Entity (Spec_Id))))
1323
              and then Present (Corresponding_Concurrent_Type
1324
                                (Etype (First_Entity (Spec_Id))))
1325
            then
1326
               Set_Etype (First_Entity (Spec_Id),
1327
                 Corresponding_Concurrent_Type
1328
                   (Etype (First_Entity (Spec_Id))));
1329
            end if;
1330
 
1331
            --  Comment needed here, since this is not Ada 2005 stuff! ???
1332
 
1333
            Install_Formals (Spec_Id);
1334
            Last_Formal := Last_Entity (Spec_Id);
1335
            New_Scope (Spec_Id);
1336
 
1337
            --  Make sure that the subprogram is immediately visible. For
1338
            --  child units that have no separate spec this is indispensable.
1339
            --  Otherwise it is safe albeit redundant.
1340
 
1341
            Set_Is_Immediately_Visible (Spec_Id);
1342
         end if;
1343
 
1344
         Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1345
         Set_Ekind (Body_Id, E_Subprogram_Body);
1346
         Set_Scope (Body_Id, Scope (Spec_Id));
1347
 
1348
      --  Case of subprogram body with no previous spec
1349
 
1350
      else
1351
         if Style_Check
1352
           and then Comes_From_Source (Body_Id)
1353
           and then not Suppress_Style_Checks (Body_Id)
1354
           and then not In_Instance
1355
         then
1356
            Style.Body_With_No_Spec (N);
1357
         end if;
1358
 
1359
         New_Overloaded_Entity (Body_Id);
1360
 
1361
         if Nkind (N) /= N_Subprogram_Body_Stub then
1362
            Set_Acts_As_Spec (N);
1363
            Generate_Definition (Body_Id);
1364
            Generate_Reference
1365
              (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1366
            Generate_Reference_To_Formals (Body_Id);
1367
            Install_Formals (Body_Id);
1368
            New_Scope (Body_Id);
1369
         end if;
1370
      end if;
1371
 
1372
      --  If this is the proper body of a stub, we must verify that the stub
1373
      --  conforms to the body, and to the previous spec if one was present.
1374
      --  we know already that the body conforms to that spec. This test is
1375
      --  only required for subprograms that come from source.
1376
 
1377
      if Nkind (Parent (N)) = N_Subunit
1378
        and then Comes_From_Source (N)
1379
        and then not Error_Posted (Body_Id)
1380
        and then Nkind (Corresponding_Stub (Parent (N))) =
1381
                                                N_Subprogram_Body_Stub
1382
      then
1383
         declare
1384
            Old_Id : constant Entity_Id :=
1385
                       Defining_Entity
1386
                         (Specification (Corresponding_Stub (Parent (N))));
1387
 
1388
            Conformant : Boolean := False;
1389
 
1390
         begin
1391
            if No (Spec_Id) then
1392
               Check_Fully_Conformant (Body_Id, Old_Id);
1393
 
1394
            else
1395
               Check_Conformance
1396
                 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1397
 
1398
               if not Conformant then
1399
 
1400
                  --  The stub was taken to be a new declaration. Indicate
1401
                  --  that it lacks a body.
1402
 
1403
                  Set_Has_Completion (Old_Id, False);
1404
               end if;
1405
            end if;
1406
         end;
1407
      end if;
1408
 
1409
      Set_Has_Completion (Body_Id);
1410
      Check_Eliminated (Body_Id);
1411
 
1412
      if Nkind (N) = N_Subprogram_Body_Stub then
1413
         return;
1414
 
1415
      elsif  Present (Spec_Id)
1416
        and then Expander_Active
1417
        and then
1418
          (Is_Always_Inlined (Spec_Id)
1419
             or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
1420
      then
1421
         Build_Body_To_Inline (N, Spec_Id);
1422
      end if;
1423
 
1424
      --  Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1425
      --  if its specification we have to install the private withed units.
1426
 
1427
      if Is_Compilation_Unit (Body_Id)
1428
        and then Scope (Body_Id) = Standard_Standard
1429
      then
1430
         Install_Private_With_Clauses (Body_Id);
1431
      end if;
1432
 
1433
      --  Now we can go on to analyze the body
1434
 
1435
      HSS := Handled_Statement_Sequence (N);
1436
      Set_Actual_Subtypes (N, Current_Scope);
1437
      Analyze_Declarations (Declarations (N));
1438
      Check_Completion;
1439
      Analyze (HSS);
1440
      Process_End_Label (HSS, 't', Current_Scope);
1441
      End_Scope;
1442
      Check_Subprogram_Order (N);
1443
      Set_Analyzed (Body_Id);
1444
 
1445
      --  If we have a separate spec, then the analysis of the declarations
1446
      --  caused the entities in the body to be chained to the spec id, but
1447
      --  we want them chained to the body id. Only the formal parameters
1448
      --  end up chained to the spec id in this case.
1449
 
1450
      if Present (Spec_Id) then
1451
 
1452
         --  If a parent unit is categorized, the context of a subunit must
1453
         --  conform to the categorization. Conversely, if a child unit is
1454
         --  categorized, the parents themselves must conform.
1455
 
1456
         if Nkind (Parent (N)) = N_Subunit then
1457
            Validate_Categorization_Dependency (N, Spec_Id);
1458
 
1459
         elsif Is_Child_Unit (Spec_Id) then
1460
            Validate_Categorization_Dependency
1461
              (Unit_Declaration_Node (Spec_Id), Spec_Id);
1462
         end if;
1463
 
1464
         if Present (Last_Formal) then
1465
            Set_Next_Entity
1466
              (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1467
            Set_Next_Entity (Last_Formal, Empty);
1468
            Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1469
            Set_Last_Entity (Spec_Id, Last_Formal);
1470
 
1471
         else
1472
            Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1473
            Set_Last_Entity  (Body_Id, Last_Entity (Spec_Id));
1474
            Set_First_Entity (Spec_Id, Empty);
1475
            Set_Last_Entity  (Spec_Id, Empty);
1476
         end if;
1477
      end if;
1478
 
1479
      --  If function, check return statements
1480
 
1481
      if Nkind (Body_Spec) = N_Function_Specification then
1482
         declare
1483
            Id : Entity_Id;
1484
 
1485
         begin
1486
            if Present (Spec_Id) then
1487
               Id := Spec_Id;
1488
            else
1489
               Id := Body_Id;
1490
            end if;
1491
 
1492
            if Return_Present (Id) then
1493
               Check_Returns (HSS, 'F', Missing_Ret);
1494
 
1495
               if Missing_Ret then
1496
                  Set_Has_Missing_Return (Id);
1497
               end if;
1498
 
1499
            elsif not Is_Machine_Code_Subprogram (Id)
1500
              and then not Body_Deleted
1501
            then
1502
               Error_Msg_N ("missing RETURN statement in function body", N);
1503
            end if;
1504
         end;
1505
 
1506
      --  If procedure with No_Return, check returns
1507
 
1508
      elsif Nkind (Body_Spec) = N_Procedure_Specification
1509
        and then Present (Spec_Id)
1510
        and then No_Return (Spec_Id)
1511
      then
1512
         Check_Returns (HSS, 'P', Missing_Ret);
1513
      end if;
1514
 
1515
      --  Now we are going to check for variables that are never modified in
1516
      --  the body of the procedure. We omit these checks if the first
1517
      --  statement of the procedure raises an exception. In particular this
1518
      --  deals with the common idiom of a stubbed function, which might
1519
      --  appear as something like
1520
 
1521
      --     function F (A : Integer) return Some_Type;
1522
      --        X : Some_Type;
1523
      --     begin
1524
      --        raise Program_Error;
1525
      --        return X;
1526
      --     end F;
1527
 
1528
      --  Here the purpose of X is simply to satisfy the (annoying)
1529
      --  requirement in Ada that there be at least one return, and we
1530
      --  certainly do not want to go posting warnings on X that it is not
1531
      --  initialized!
1532
 
1533
      declare
1534
         Stm : Node_Id := First (Statements (HSS));
1535
 
1536
      begin
1537
         --  Skip an initial label (for one thing this occurs when we are in
1538
         --  front end ZCX mode, but in any case it is irrelevant).
1539
 
1540
         if Nkind (Stm) = N_Label then
1541
            Next (Stm);
1542
         end if;
1543
 
1544
         --  Do the test on the original statement before expansion
1545
 
1546
         declare
1547
            Ostm : constant Node_Id := Original_Node (Stm);
1548
 
1549
         begin
1550
            --  If explicit raise statement, return with no checks
1551
 
1552
            if Nkind (Ostm) = N_Raise_Statement then
1553
               return;
1554
 
1555
            --  Check for explicit call cases which likely raise an exception
1556
 
1557
            elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1558
               if Is_Entity_Name (Name (Ostm)) then
1559
                  declare
1560
                     Ent : constant Entity_Id := Entity (Name (Ostm));
1561
 
1562
                  begin
1563
                     --  If the procedure is marked No_Return, then likely it
1564
                     --  raises an exception, but in any case it is not coming
1565
                     --  back here, so no need to check beyond the call.
1566
 
1567
                     if Ekind (Ent) = E_Procedure
1568
                       and then No_Return (Ent)
1569
                     then
1570
                        return;
1571
 
1572
                     --  If the procedure name is Raise_Exception, then also
1573
                     --  assume that it raises an exception. The main target
1574
                     --  here is Ada.Exceptions.Raise_Exception, but this name
1575
                     --  is pretty evocative in any context! Note that the
1576
                     --  procedure in Ada.Exceptions is not marked No_Return
1577
                     --  because of the annoying case of the null exception Id.
1578
 
1579
                     elsif Chars (Ent) = Name_Raise_Exception then
1580
                        return;
1581
                     end if;
1582
                  end;
1583
               end if;
1584
            end if;
1585
         end;
1586
      end;
1587
 
1588
      --  Check for variables that are never modified
1589
 
1590
      declare
1591
         E1, E2 : Entity_Id;
1592
 
1593
      begin
1594
         --  If there is a separate spec, then transfer Never_Set_In_Source
1595
         --  flags from out parameters to the corresponding entities in the
1596
         --  body. The reason we do that is we want to post error flags on
1597
         --  the body entities, not the spec entities.
1598
 
1599
         if Present (Spec_Id) then
1600
            E1 := First_Entity (Spec_Id);
1601
 
1602
            while Present (E1) loop
1603
               if Ekind (E1) = E_Out_Parameter then
1604
                  E2 := First_Entity (Body_Id);
1605
                  while Present (E2) loop
1606
                     exit when Chars (E1) = Chars (E2);
1607
                     Next_Entity (E2);
1608
                  end loop;
1609
 
1610
                  if Present (E2) then
1611
                     Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1612
                  end if;
1613
               end if;
1614
 
1615
               Next_Entity (E1);
1616
            end loop;
1617
         end if;
1618
 
1619
         --  Check references in body unless it was deleted. Note that the
1620
         --  check of Body_Deleted here is not just for efficiency, it is
1621
         --  necessary to avoid junk warnings on formal parameters.
1622
 
1623
         if not Body_Deleted then
1624
            Check_References (Body_Id);
1625
         end if;
1626
      end;
1627
   end Analyze_Subprogram_Body;
1628
 
1629
   ------------------------------------
1630
   -- Analyze_Subprogram_Declaration --
1631
   ------------------------------------
1632
 
1633
   procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1634
      Designator : constant Entity_Id :=
1635
                     Analyze_Subprogram_Specification (Specification (N));
1636
      Scop       : constant Entity_Id := Current_Scope;
1637
 
1638
   --  Start of processing for Analyze_Subprogram_Declaration
1639
 
1640
   begin
1641
      Generate_Definition (Designator);
1642
 
1643
      --  Check for RCI unit subprogram declarations against in-lined
1644
      --  subprograms and subprograms having access parameter or limited
1645
      --  parameter without Read and Write (RM E.2.3(12-13)).
1646
 
1647
      Validate_RCI_Subprogram_Declaration (N);
1648
 
1649
      Trace_Scope
1650
        (N,
1651
         Defining_Entity (N),
1652
         " Analyze subprogram spec. ");
1653
 
1654
      if Debug_Flag_C then
1655
         Write_Str ("====  Compiling subprogram spec ");
1656
         Write_Name (Chars (Designator));
1657
         Write_Str (" from ");
1658
         Write_Location (Sloc (N));
1659
         Write_Eol;
1660
      end if;
1661
 
1662
      New_Overloaded_Entity (Designator);
1663
      Check_Delayed_Subprogram (Designator);
1664
 
1665
      --  What is the following code for, it used to be
1666
 
1667
      --  ???   Set_Suppress_Elaboration_Checks
1668
      --  ???     (Designator, Elaboration_Checks_Suppressed (Designator));
1669
 
1670
      --  The following seems equivalent, but a bit dubious
1671
 
1672
      if Elaboration_Checks_Suppressed (Designator) then
1673
         Set_Kill_Elaboration_Checks (Designator);
1674
      end if;
1675
 
1676
      if Scop /= Standard_Standard
1677
        and then not Is_Child_Unit (Designator)
1678
      then
1679
         Set_Categorization_From_Scope (Designator, Scop);
1680
      else
1681
         --  For a compilation unit, check for library-unit pragmas
1682
 
1683
         New_Scope (Designator);
1684
         Set_Categorization_From_Pragmas (N);
1685
         Validate_Categorization_Dependency (N, Designator);
1686
         Pop_Scope;
1687
      end if;
1688
 
1689
      --  For a compilation unit, set body required. This flag will only be
1690
      --  reset if a valid Import or Interface pragma is processed later on.
1691
 
1692
      if Nkind (Parent (N)) = N_Compilation_Unit then
1693
         Set_Body_Required (Parent (N), True);
1694
 
1695
         if Ada_Version >= Ada_05
1696
           and then Nkind (Specification (N)) = N_Procedure_Specification
1697
           and then Null_Present (Specification (N))
1698
         then
1699
            Error_Msg_N
1700
              ("null procedure cannot be declared at library level", N);
1701
         end if;
1702
      end if;
1703
 
1704
      Generate_Reference_To_Formals (Designator);
1705
      Check_Eliminated (Designator);
1706
 
1707
      --  Ada 2005: if procedure is declared with "is null" qualifier,
1708
      --  it requires no body.
1709
 
1710
      if Nkind (Specification (N)) = N_Procedure_Specification
1711
        and then Null_Present (Specification (N))
1712
      then
1713
         Set_Has_Completion (Designator);
1714
         Set_Is_Inlined (Designator);
1715
      end if;
1716
   end Analyze_Subprogram_Declaration;
1717
 
1718
   --------------------------------------
1719
   -- Analyze_Subprogram_Specification --
1720
   --------------------------------------
1721
 
1722
   --  Reminder: N here really is a subprogram specification (not a subprogram
1723
   --  declaration). This procedure is called to analyze the specification in
1724
   --  both subprogram bodies and subprogram declarations (specs).
1725
 
1726
   function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1727
      Designator : constant Entity_Id := Defining_Entity (N);
1728
      Formals    : constant List_Id   := Parameter_Specifications (N);
1729
 
1730
      function Has_Interface_Formals (T : List_Id) return Boolean;
1731
      --  Ada 2005 (AI-251): Returns true if some non class-wide interface
1732
      --  formal is found.
1733
 
1734
      ---------------------------
1735
      -- Has_Interface_Formals --
1736
      ---------------------------
1737
 
1738
      function Has_Interface_Formals (T : List_Id) return Boolean is
1739
         Param_Spec : Node_Id;
1740
         Formal     : Entity_Id;
1741
 
1742
      begin
1743
         Param_Spec := First (T);
1744
 
1745
         while Present (Param_Spec) loop
1746
            Formal := Defining_Identifier (Param_Spec);
1747
 
1748
            if Is_Class_Wide_Type (Etype (Formal)) then
1749
               null;
1750
 
1751
            elsif Is_Interface (Etype (Formal)) then
1752
               return True;
1753
            end if;
1754
 
1755
            Next (Param_Spec);
1756
         end loop;
1757
 
1758
         return False;
1759
      end Has_Interface_Formals;
1760
 
1761
   --  Start of processing for Analyze_Subprogram_Specification
1762
 
1763
   begin
1764
      Generate_Definition (Designator);
1765
 
1766
      if Nkind (N) = N_Function_Specification then
1767
         Set_Ekind (Designator, E_Function);
1768
         Set_Mechanism (Designator, Default_Mechanism);
1769
 
1770
      else
1771
         Set_Ekind (Designator, E_Procedure);
1772
         Set_Etype (Designator, Standard_Void_Type);
1773
      end if;
1774
 
1775
      --  Introduce new scope for analysis of the formals and of the
1776
      --  return type.
1777
 
1778
      Set_Scope (Designator, Current_Scope);
1779
 
1780
      if Present (Formals) then
1781
         New_Scope (Designator);
1782
         Process_Formals (Formals, N);
1783
 
1784
         --  Ada 2005 (AI-345): Allow overriding primitives of protected
1785
         --  interfaces by means of normal subprograms. For this purpose
1786
         --  temporarily use the corresponding record type as the etype
1787
         --  of the first formal.
1788
 
1789
         if Ada_Version >= Ada_05
1790
           and then Comes_From_Source (Designator)
1791
           and then Present (First_Entity (Designator))
1792
           and then (Ekind (Etype (First_Entity (Designator)))
1793
                             = E_Protected_Type
1794
                       or else
1795
                     Ekind (Etype (First_Entity (Designator)))
1796
                             = E_Task_Type)
1797
           and then Present (Corresponding_Record_Type
1798
                             (Etype (First_Entity (Designator))))
1799
           and then Present (Abstract_Interfaces
1800
                             (Corresponding_Record_Type
1801
                             (Etype (First_Entity (Designator)))))
1802
         then
1803
            Set_Etype (First_Entity (Designator),
1804
              Corresponding_Record_Type (Etype (First_Entity (Designator))));
1805
         end if;
1806
 
1807
         End_Scope;
1808
 
1809
      elsif Nkind (N) = N_Function_Specification then
1810
         Analyze_Return_Type (N);
1811
      end if;
1812
 
1813
      if Nkind (N) = N_Function_Specification then
1814
         if Nkind (Designator) = N_Defining_Operator_Symbol then
1815
            Valid_Operator_Definition (Designator);
1816
         end if;
1817
 
1818
         May_Need_Actuals (Designator);
1819
 
1820
         if Is_Abstract (Etype (Designator))
1821
           and then Nkind (Parent (N))
1822
                      /= N_Abstract_Subprogram_Declaration
1823
           and then (Nkind (Parent (N)))
1824
                      /= N_Formal_Abstract_Subprogram_Declaration
1825
           and then (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1826
                      or else not Is_Entity_Name (Name (Parent (N)))
1827
                      or else not Is_Abstract (Entity (Name (Parent (N)))))
1828
         then
1829
            Error_Msg_N
1830
              ("function that returns abstract type must be abstract", N);
1831
         end if;
1832
      end if;
1833
 
1834
      if Ada_Version >= Ada_05
1835
        and then Comes_From_Source (N)
1836
        and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1837
        and then (Nkind (N) /= N_Procedure_Specification
1838
                    or else
1839
                  not Null_Present (N))
1840
        and then Has_Interface_Formals (Formals)
1841
      then
1842
         Error_Msg_Name_1 := Chars (Defining_Unit_Name
1843
                                    (Specification (Parent (N))));
1844
         Error_Msg_N
1845
           ("(Ada 2005) interface subprogram % must be abstract or null", N);
1846
      end if;
1847
 
1848
      return Designator;
1849
   end Analyze_Subprogram_Specification;
1850
 
1851
   --------------------------
1852
   -- Build_Body_To_Inline --
1853
   --------------------------
1854
 
1855
   procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1856
      Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1857
      Original_Body   : Node_Id;
1858
      Body_To_Analyze : Node_Id;
1859
      Max_Size        : constant := 10;
1860
      Stat_Count      : Integer := 0;
1861
 
1862
      function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1863
      --  Check for declarations that make inlining not worthwhile
1864
 
1865
      function Has_Excluded_Statement   (Stats : List_Id) return Boolean;
1866
      --  Check for statements that make inlining not worthwhile: any tasking
1867
      --  statement, nested at any level. Keep track of total number of
1868
      --  elementary statements, as a measure of acceptable size.
1869
 
1870
      function Has_Pending_Instantiation return Boolean;
1871
      --  If some enclosing body contains instantiations that appear before
1872
      --  the corresponding generic body, the enclosing body has a freeze node
1873
      --  so that it can be elaborated after the generic itself. This might
1874
      --  conflict with subsequent inlinings, so that it is unsafe to try to
1875
      --  inline in such a case.
1876
 
1877
      procedure Remove_Pragmas;
1878
      --  A pragma Unreferenced that mentions a formal parameter has no
1879
      --  meaning when the body is inlined and the formals are rewritten.
1880
      --  Remove it from body to inline. The analysis of the non-inlined body
1881
      --  will handle the pragma properly.
1882
 
1883
      function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
1884
      --  If the body of the subprogram includes a call that returns an
1885
      --  unconstrained type, the secondary stack is involved, and it
1886
      --  is not worth inlining.
1887
 
1888
      ------------------------------
1889
      -- Has_Excluded_Declaration --
1890
      ------------------------------
1891
 
1892
      function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1893
         D : Node_Id;
1894
 
1895
         function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1896
         --  Nested subprograms make a given body ineligible for inlining, but
1897
         --  we make an exception for instantiations of unchecked conversion.
1898
         --  The body has not been analyzed yet, so check the name, and verify
1899
         --  that the visible entity with that name is the predefined unit.
1900
 
1901
         -----------------------------
1902
         -- Is_Unchecked_Conversion --
1903
         -----------------------------
1904
 
1905
         function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1906
            Id   : constant Node_Id := Name (D);
1907
            Conv : Entity_Id;
1908
 
1909
         begin
1910
            if Nkind (Id) = N_Identifier
1911
              and then Chars (Id) = Name_Unchecked_Conversion
1912
            then
1913
               Conv := Current_Entity (Id);
1914
 
1915
            elsif (Nkind (Id) = N_Selected_Component
1916
                    or else Nkind (Id) = N_Expanded_Name)
1917
              and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1918
            then
1919
               Conv := Current_Entity (Selector_Name (Id));
1920
 
1921
            else
1922
               return False;
1923
            end if;
1924
 
1925
            return Present (Conv)
1926
              and then Is_Predefined_File_Name
1927
                         (Unit_File_Name (Get_Source_Unit (Conv)))
1928
              and then Is_Intrinsic_Subprogram (Conv);
1929
         end Is_Unchecked_Conversion;
1930
 
1931
      --  Start of processing for Has_Excluded_Declaration
1932
 
1933
      begin
1934
         D := First (Decls);
1935
 
1936
         while Present (D) loop
1937
            if       (Nkind (D) = N_Function_Instantiation
1938
                        and then not Is_Unchecked_Conversion (D))
1939
              or else Nkind (D) = N_Protected_Type_Declaration
1940
              or else Nkind (D) = N_Package_Declaration
1941
              or else Nkind (D) = N_Package_Instantiation
1942
              or else Nkind (D) = N_Subprogram_Body
1943
              or else Nkind (D) = N_Procedure_Instantiation
1944
              or else Nkind (D) = N_Task_Type_Declaration
1945
            then
1946
               Cannot_Inline
1947
                 ("cannot inline & (non-allowed declaration)?", D, Subp);
1948
               return True;
1949
            end if;
1950
 
1951
            Next (D);
1952
         end loop;
1953
 
1954
         return False;
1955
      end Has_Excluded_Declaration;
1956
 
1957
      ----------------------------
1958
      -- Has_Excluded_Statement --
1959
      ----------------------------
1960
 
1961
      function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1962
         S : Node_Id;
1963
         E : Node_Id;
1964
 
1965
      begin
1966
         S := First (Stats);
1967
 
1968
         while Present (S) loop
1969
            Stat_Count := Stat_Count + 1;
1970
 
1971
            if Nkind (S) = N_Abort_Statement
1972
              or else Nkind (S) = N_Asynchronous_Select
1973
              or else Nkind (S) = N_Conditional_Entry_Call
1974
              or else Nkind (S) = N_Delay_Relative_Statement
1975
              or else Nkind (S) = N_Delay_Until_Statement
1976
              or else Nkind (S) = N_Selective_Accept
1977
              or else Nkind (S) = N_Timed_Entry_Call
1978
            then
1979
               Cannot_Inline
1980
                 ("cannot inline & (non-allowed statement)?", S, Subp);
1981
               return True;
1982
 
1983
            elsif Nkind (S) = N_Block_Statement then
1984
               if Present (Declarations (S))
1985
                 and then Has_Excluded_Declaration (Declarations (S))
1986
               then
1987
                  return True;
1988
 
1989
               elsif Present (Handled_Statement_Sequence (S))
1990
                  and then
1991
                    (Present
1992
                      (Exception_Handlers (Handled_Statement_Sequence (S)))
1993
                     or else
1994
                       Has_Excluded_Statement
1995
                         (Statements (Handled_Statement_Sequence (S))))
1996
               then
1997
                  return True;
1998
               end if;
1999
 
2000
            elsif Nkind (S) = N_Case_Statement then
2001
               E := First (Alternatives (S));
2002
               while Present (E) loop
2003
                  if Has_Excluded_Statement (Statements (E)) then
2004
                     return True;
2005
                  end if;
2006
 
2007
                  Next (E);
2008
               end loop;
2009
 
2010
            elsif Nkind (S) = N_If_Statement then
2011
               if Has_Excluded_Statement (Then_Statements (S)) then
2012
                  return True;
2013
               end if;
2014
 
2015
               if Present (Elsif_Parts (S)) then
2016
                  E := First (Elsif_Parts (S));
2017
                  while Present (E) loop
2018
                     if Has_Excluded_Statement (Then_Statements (E)) then
2019
                        return True;
2020
                     end if;
2021
                     Next (E);
2022
                  end loop;
2023
               end if;
2024
 
2025
               if Present (Else_Statements (S))
2026
                 and then Has_Excluded_Statement (Else_Statements (S))
2027
               then
2028
                  return True;
2029
               end if;
2030
 
2031
            elsif Nkind (S) = N_Loop_Statement
2032
              and then Has_Excluded_Statement (Statements (S))
2033
            then
2034
               return True;
2035
            end if;
2036
 
2037
            Next (S);
2038
         end loop;
2039
 
2040
         return False;
2041
      end Has_Excluded_Statement;
2042
 
2043
      -------------------------------
2044
      -- Has_Pending_Instantiation --
2045
      -------------------------------
2046
 
2047
      function Has_Pending_Instantiation return Boolean is
2048
         S : Entity_Id := Current_Scope;
2049
 
2050
      begin
2051
         while Present (S) loop
2052
            if Is_Compilation_Unit (S)
2053
              or else Is_Child_Unit (S)
2054
            then
2055
               return False;
2056
            elsif Ekind (S) = E_Package
2057
              and then Has_Forward_Instantiation (S)
2058
            then
2059
               return True;
2060
            end if;
2061
 
2062
            S := Scope (S);
2063
         end loop;
2064
 
2065
         return False;
2066
      end Has_Pending_Instantiation;
2067
 
2068
      --------------------
2069
      -- Remove_Pragmas --
2070
      --------------------
2071
 
2072
      procedure Remove_Pragmas is
2073
         Decl : Node_Id;
2074
         Nxt  : Node_Id;
2075
 
2076
      begin
2077
         Decl := First (Declarations (Body_To_Analyze));
2078
         while Present (Decl) loop
2079
            Nxt := Next (Decl);
2080
 
2081
            if Nkind (Decl) = N_Pragma
2082
              and then Chars (Decl) = Name_Unreferenced
2083
            then
2084
               Remove (Decl);
2085
            end if;
2086
 
2087
            Decl := Nxt;
2088
         end loop;
2089
      end Remove_Pragmas;
2090
 
2091
      --------------------------
2092
      -- Uses_Secondary_Stack --
2093
      --------------------------
2094
 
2095
      function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
2096
         function Check_Call (N : Node_Id) return Traverse_Result;
2097
         --  Look for function calls that return an unconstrained type
2098
 
2099
         ----------------
2100
         -- Check_Call --
2101
         ----------------
2102
 
2103
         function Check_Call (N : Node_Id) return Traverse_Result is
2104
         begin
2105
            if Nkind (N) = N_Function_Call
2106
              and then Is_Entity_Name (Name (N))
2107
              and then Is_Composite_Type (Etype (Entity (Name (N))))
2108
              and then not Is_Constrained (Etype (Entity (Name (N))))
2109
            then
2110
               Cannot_Inline
2111
                 ("cannot inline & (call returns unconstrained type)?",
2112
                    N, Subp);
2113
               return Abandon;
2114
            else
2115
               return OK;
2116
            end if;
2117
         end Check_Call;
2118
 
2119
         function Check_Calls is new Traverse_Func (Check_Call);
2120
 
2121
      begin
2122
         return Check_Calls (Bod) = Abandon;
2123
      end Uses_Secondary_Stack;
2124
 
2125
   --  Start of processing for Build_Body_To_Inline
2126
 
2127
   begin
2128
      if Nkind (Decl) = N_Subprogram_Declaration
2129
        and then Present (Body_To_Inline (Decl))
2130
      then
2131
         return;    --  Done already.
2132
 
2133
      --  Functions that return unconstrained composite types will require
2134
      --  secondary stack handling, and cannot currently be inlined.
2135
      --  Ditto for functions that return controlled types, where controlled
2136
      --  actions interfere in complex ways with inlining.
2137
 
2138
      elsif Ekind (Subp) = E_Function
2139
        and then not Is_Scalar_Type (Etype (Subp))
2140
        and then not Is_Access_Type (Etype (Subp))
2141
        and then not Is_Constrained (Etype (Subp))
2142
      then
2143
         Cannot_Inline
2144
           ("cannot inline & (unconstrained return type)?", N, Subp);
2145
         return;
2146
 
2147
      elsif Ekind (Subp) = E_Function
2148
        and then Controlled_Type (Etype (Subp))
2149
      then
2150
         Cannot_Inline
2151
           ("cannot inline & (controlled return type)?", N, Subp);
2152
         return;
2153
      end if;
2154
 
2155
      if Present (Declarations (N))
2156
        and then Has_Excluded_Declaration (Declarations (N))
2157
      then
2158
         return;
2159
      end if;
2160
 
2161
      if Present (Handled_Statement_Sequence (N)) then
2162
         if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
2163
            Cannot_Inline
2164
              ("cannot inline& (exception handler)?",
2165
               First (Exception_Handlers (Handled_Statement_Sequence (N))),
2166
               Subp);
2167
            return;
2168
         elsif
2169
           Has_Excluded_Statement
2170
             (Statements (Handled_Statement_Sequence (N)))
2171
         then
2172
            return;
2173
         end if;
2174
      end if;
2175
 
2176
      --  We do not inline a subprogram  that is too large, unless it is
2177
      --  marked Inline_Always. This pragma does not suppress the other
2178
      --  checks on inlining (forbidden declarations, handlers, etc).
2179
 
2180
      if Stat_Count > Max_Size
2181
        and then not Is_Always_Inlined (Subp)
2182
      then
2183
         Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
2184
         return;
2185
      end if;
2186
 
2187
      if Has_Pending_Instantiation then
2188
         Cannot_Inline
2189
           ("cannot inline& (forward instance within enclosing body)?",
2190
             N, Subp);
2191
         return;
2192
      end if;
2193
 
2194
      --  Within an instance, the body to inline must be treated as a nested
2195
      --  generic, so that the proper global references are preserved.
2196
 
2197
      if In_Instance then
2198
         Save_Env (Scope (Current_Scope), Scope (Current_Scope));
2199
         Original_Body := Copy_Generic_Node (N, Empty, True);
2200
      else
2201
         Original_Body := Copy_Separate_Tree (N);
2202
      end if;
2203
 
2204
      --  We need to capture references to the formals in order to substitute
2205
      --  the actuals at the point of inlining, i.e. instantiation. To treat
2206
      --  the formals as globals to the body to inline, we nest it within
2207
      --  a dummy parameterless subprogram, declared within the real one.
2208
      --  To avoid generating an internal name (which is never public, and
2209
      --  which affects serial numbers of other generated names), we use
2210
      --  an internal symbol that cannot conflict with user declarations.
2211
 
2212
      Set_Parameter_Specifications (Specification (Original_Body), No_List);
2213
      Set_Defining_Unit_Name
2214
        (Specification (Original_Body),
2215
          Make_Defining_Identifier (Sloc (N), Name_uParent));
2216
      Set_Corresponding_Spec (Original_Body, Empty);
2217
 
2218
      Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
2219
 
2220
      --  Set return type of function, which is also global and does not need
2221
      --  to be resolved.
2222
 
2223
      if Ekind (Subp) = E_Function then
2224
         Set_Result_Definition (Specification (Body_To_Analyze),
2225
           New_Occurrence_Of (Etype (Subp), Sloc (N)));
2226
      end if;
2227
 
2228
      if No (Declarations (N)) then
2229
         Set_Declarations (N, New_List (Body_To_Analyze));
2230
      else
2231
         Append (Body_To_Analyze, Declarations (N));
2232
      end if;
2233
 
2234
      Expander_Mode_Save_And_Set (False);
2235
      Remove_Pragmas;
2236
 
2237
      Analyze (Body_To_Analyze);
2238
      New_Scope (Defining_Entity (Body_To_Analyze));
2239
      Save_Global_References (Original_Body);
2240
      End_Scope;
2241
      Remove (Body_To_Analyze);
2242
 
2243
      Expander_Mode_Restore;
2244
 
2245
      if In_Instance then
2246
         Restore_Env;
2247
      end if;
2248
 
2249
      --  If secondary stk used there is no point in inlining. We have
2250
      --  already issued the warning in this case, so nothing to do.
2251
 
2252
      if Uses_Secondary_Stack (Body_To_Analyze) then
2253
         return;
2254
      end if;
2255
 
2256
      Set_Body_To_Inline (Decl, Original_Body);
2257
      Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
2258
      Set_Is_Inlined (Subp);
2259
   end Build_Body_To_Inline;
2260
 
2261
   -------------------
2262
   -- Cannot_Inline --
2263
   -------------------
2264
 
2265
   procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
2266
   begin
2267
      --  Do not emit warning if this is a predefined unit which is not
2268
      --  the main unit. With validity checks enabled, some predefined
2269
      --  subprograms may contain nested subprograms and become ineligible
2270
      --  for inlining.
2271
 
2272
      if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
2273
        and then not In_Extended_Main_Source_Unit (Subp)
2274
      then
2275
         null;
2276
 
2277
      elsif Is_Always_Inlined (Subp) then
2278
 
2279
         --  Remove last character (question mark) to make this into an error,
2280
         --  because the Inline_Always pragma cannot be obeyed.
2281
 
2282
         Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
2283
 
2284
      elsif Ineffective_Inline_Warnings then
2285
         Error_Msg_NE (Msg, N, Subp);
2286
      end if;
2287
   end Cannot_Inline;
2288
 
2289
   -----------------------
2290
   -- Check_Conformance --
2291
   -----------------------
2292
 
2293
   procedure Check_Conformance
2294
     (New_Id                   : Entity_Id;
2295
      Old_Id                   : Entity_Id;
2296
      Ctype                    : Conformance_Type;
2297
      Errmsg                   : Boolean;
2298
      Conforms                 : out Boolean;
2299
      Err_Loc                  : Node_Id := Empty;
2300
      Get_Inst                 : Boolean := False;
2301
      Skip_Controlling_Formals : Boolean := False)
2302
   is
2303
      Old_Type   : constant Entity_Id := Etype (Old_Id);
2304
      New_Type   : constant Entity_Id := Etype (New_Id);
2305
      Old_Formal : Entity_Id;
2306
      New_Formal : Entity_Id;
2307
 
2308
      procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
2309
      --  Post error message for conformance error on given node. Two messages
2310
      --  are output. The first points to the previous declaration with a
2311
      --  general "no conformance" message. The second is the detailed reason,
2312
      --  supplied as Msg. The parameter N provide information for a possible
2313
      --  & insertion in the message, and also provides the location for
2314
      --  posting the message in the absence of a specified Err_Loc location.
2315
 
2316
      -----------------------
2317
      -- Conformance_Error --
2318
      -----------------------
2319
 
2320
      procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
2321
         Enode : Node_Id;
2322
 
2323
      begin
2324
         Conforms := False;
2325
 
2326
         if Errmsg then
2327
            if No (Err_Loc) then
2328
               Enode := N;
2329
            else
2330
               Enode := Err_Loc;
2331
            end if;
2332
 
2333
            Error_Msg_Sloc := Sloc (Old_Id);
2334
 
2335
            case Ctype is
2336
               when Type_Conformant =>
2337
                  Error_Msg_N
2338
                    ("not type conformant with declaration#!", Enode);
2339
 
2340
               when Mode_Conformant =>
2341
                  Error_Msg_N
2342
                    ("not mode conformant with declaration#!", Enode);
2343
 
2344
               when Subtype_Conformant =>
2345
                  Error_Msg_N
2346
                    ("not subtype conformant with declaration#!", Enode);
2347
 
2348
               when Fully_Conformant =>
2349
                  Error_Msg_N
2350
                    ("not fully conformant with declaration#!", Enode);
2351
            end case;
2352
 
2353
            Error_Msg_NE (Msg, Enode, N);
2354
         end if;
2355
      end Conformance_Error;
2356
 
2357
   --  Start of processing for Check_Conformance
2358
 
2359
   begin
2360
      Conforms := True;
2361
 
2362
      --  We need a special case for operators, since they don't appear
2363
      --  explicitly.
2364
 
2365
      if Ctype = Type_Conformant then
2366
         if Ekind (New_Id) = E_Operator
2367
           and then Operator_Matches_Spec (New_Id, Old_Id)
2368
         then
2369
            return;
2370
         end if;
2371
      end if;
2372
 
2373
      --  If both are functions/operators, check return types conform
2374
 
2375
      if Old_Type /= Standard_Void_Type
2376
        and then New_Type /= Standard_Void_Type
2377
      then
2378
         if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2379
            Conformance_Error ("return type does not match!", New_Id);
2380
            return;
2381
         end if;
2382
 
2383
         --  Ada 2005 (AI-231): In case of anonymous access types check the
2384
         --  null-exclusion and access-to-constant attributes must match.
2385
 
2386
         if Ada_Version >= Ada_05
2387
           and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
2388
           and then
2389
             (Can_Never_Be_Null (Old_Type)
2390
                /= Can_Never_Be_Null (New_Type)
2391
              or else Is_Access_Constant (Etype (Old_Type))
2392
                        /= Is_Access_Constant (Etype (New_Type)))
2393
         then
2394
            Conformance_Error ("return type does not match!", New_Id);
2395
            return;
2396
         end if;
2397
 
2398
      --  If either is a function/operator and the other isn't, error
2399
 
2400
      elsif Old_Type /= Standard_Void_Type
2401
        or else New_Type /= Standard_Void_Type
2402
      then
2403
         Conformance_Error ("functions can only match functions!", New_Id);
2404
         return;
2405
      end if;
2406
 
2407
      --  In subtype conformant case, conventions must match (RM 6.3.1(16))
2408
      --  If this is a renaming as body, refine error message to indicate that
2409
      --  the conflict is with the original declaration. If the entity is not
2410
      --  frozen, the conventions don't have to match, the one of the renamed
2411
      --  entity is inherited.
2412
 
2413
      if Ctype >= Subtype_Conformant then
2414
         if Convention (Old_Id) /= Convention (New_Id) then
2415
 
2416
            if not Is_Frozen (New_Id) then
2417
               null;
2418
 
2419
            elsif Present (Err_Loc)
2420
              and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2421
              and then Present (Corresponding_Spec (Err_Loc))
2422
            then
2423
               Error_Msg_Name_1 := Chars (New_Id);
2424
               Error_Msg_Name_2 :=
2425
                 Name_Ada + Convention_Id'Pos (Convention (New_Id));
2426
 
2427
               Conformance_Error ("prior declaration for% has convention %!");
2428
 
2429
            else
2430
               Conformance_Error ("calling conventions do not match!");
2431
            end if;
2432
 
2433
            return;
2434
 
2435
         elsif Is_Formal_Subprogram (Old_Id)
2436
           or else Is_Formal_Subprogram (New_Id)
2437
         then
2438
            Conformance_Error ("formal subprograms not allowed!");
2439
            return;
2440
         end if;
2441
      end if;
2442
 
2443
      --  Deal with parameters
2444
 
2445
      --  Note: we use the entity information, rather than going directly
2446
      --  to the specification in the tree. This is not only simpler, but
2447
      --  absolutely necessary for some cases of conformance tests between
2448
      --  operators, where the declaration tree simply does not exist!
2449
 
2450
      Old_Formal := First_Formal (Old_Id);
2451
      New_Formal := First_Formal (New_Id);
2452
 
2453
      while Present (Old_Formal) and then Present (New_Formal) loop
2454
         if Is_Controlling_Formal (Old_Formal)
2455
           and then Is_Controlling_Formal (New_Formal)
2456
           and then Skip_Controlling_Formals
2457
         then
2458
            goto Skip_Controlling_Formal;
2459
         end if;
2460
 
2461
         if Ctype = Fully_Conformant then
2462
 
2463
            --  Names must match. Error message is more accurate if we do
2464
            --  this before checking that the types of the formals match.
2465
 
2466
            if Chars (Old_Formal) /= Chars (New_Formal) then
2467
               Conformance_Error ("name & does not match!", New_Formal);
2468
 
2469
               --  Set error posted flag on new formal as well to stop
2470
               --  junk cascaded messages in some cases.
2471
 
2472
               Set_Error_Posted (New_Formal);
2473
               return;
2474
            end if;
2475
         end if;
2476
 
2477
         --  Types must always match. In the visible part of an instance,
2478
         --  usual overloading rules for dispatching operations apply, and
2479
         --  we check base types (not the actual subtypes).
2480
 
2481
         if In_Instance_Visible_Part
2482
           and then Is_Dispatching_Operation (New_Id)
2483
         then
2484
            if not Conforming_Types
2485
              (Base_Type (Etype (Old_Formal)),
2486
                 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2487
            then
2488
               Conformance_Error ("type of & does not match!", New_Formal);
2489
               return;
2490
            end if;
2491
 
2492
         elsif not Conforming_Types
2493
           (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2494
         then
2495
            Conformance_Error ("type of & does not match!", New_Formal);
2496
            return;
2497
         end if;
2498
 
2499
         --  For mode conformance, mode must match
2500
 
2501
         if Ctype >= Mode_Conformant
2502
           and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2503
         then
2504
            Conformance_Error ("mode of & does not match!", New_Formal);
2505
            return;
2506
         end if;
2507
 
2508
         --  Full conformance checks
2509
 
2510
         if Ctype = Fully_Conformant then
2511
 
2512
            --  We have checked already that names match
2513
 
2514
            if Parameter_Mode (Old_Formal) = E_In_Parameter then
2515
 
2516
               --  Ada 2005 (AI-231): In case of anonymous access types check
2517
               --  the null-exclusion and access-to-constant attributes must
2518
               --  match.
2519
 
2520
               if Ada_Version >= Ada_05
2521
                 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
2522
                 and then
2523
                   (Can_Never_Be_Null (Old_Formal)
2524
                      /= Can_Never_Be_Null (New_Formal)
2525
                    or else Is_Access_Constant (Etype (Old_Formal))
2526
                              /= Is_Access_Constant (Etype (New_Formal)))
2527
               then
2528
                  --  It is allowed to omit the null-exclusion in case of
2529
                  --  stream attribute subprograms
2530
 
2531
                  declare
2532
                     TSS_Name : TSS_Name_Type;
2533
 
2534
                  begin
2535
                     Get_Name_String (Chars (New_Id));
2536
                     TSS_Name :=
2537
                       TSS_Name_Type
2538
                         (Name_Buffer
2539
                            (Name_Len - TSS_Name'Length + 1 .. Name_Len));
2540
 
2541
                     if TSS_Name /= TSS_Stream_Read
2542
                       and then TSS_Name /= TSS_Stream_Write
2543
                       and then TSS_Name /= TSS_Stream_Input
2544
                       and then TSS_Name /= TSS_Stream_Output
2545
                     then
2546
                        Conformance_Error
2547
                          ("type of & does not match!", New_Formal);
2548
                        return;
2549
                     end if;
2550
                  end;
2551
               end if;
2552
 
2553
               --  Check default expressions for in parameters
2554
 
2555
               declare
2556
                  NewD : constant Boolean :=
2557
                           Present (Default_Value (New_Formal));
2558
                  OldD : constant Boolean :=
2559
                           Present (Default_Value (Old_Formal));
2560
               begin
2561
                  if NewD or OldD then
2562
 
2563
                     --  The old default value has been analyzed because the
2564
                     --  current full declaration will have frozen everything
2565
                     --  before. The new default values have not been
2566
                     --  analyzed, so analyze them now before we check for
2567
                     --  conformance.
2568
 
2569
                     if NewD then
2570
                        New_Scope (New_Id);
2571
                        Analyze_Per_Use_Expression
2572
                          (Default_Value (New_Formal), Etype (New_Formal));
2573
                        End_Scope;
2574
                     end if;
2575
 
2576
                     if not (NewD and OldD)
2577
                       or else not Fully_Conformant_Expressions
2578
                                    (Default_Value (Old_Formal),
2579
                                     Default_Value (New_Formal))
2580
                     then
2581
                        Conformance_Error
2582
                          ("default expression for & does not match!",
2583
                           New_Formal);
2584
                        return;
2585
                     end if;
2586
                  end if;
2587
               end;
2588
            end if;
2589
         end if;
2590
 
2591
         --  A couple of special checks for Ada 83 mode. These checks are
2592
         --  skipped if either entity is an operator in package Standard.
2593
         --  or if either old or new instance is not from the source program.
2594
 
2595
         if Ada_Version = Ada_83
2596
           and then Sloc (Old_Id) > Standard_Location
2597
           and then Sloc (New_Id) > Standard_Location
2598
           and then Comes_From_Source (Old_Id)
2599
           and then Comes_From_Source (New_Id)
2600
         then
2601
            declare
2602
               Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2603
               New_Param : constant Node_Id := Declaration_Node (New_Formal);
2604
 
2605
            begin
2606
               --  Explicit IN must be present or absent in both cases. This
2607
               --  test is required only in the full conformance case.
2608
 
2609
               if In_Present (Old_Param) /= In_Present (New_Param)
2610
                 and then Ctype = Fully_Conformant
2611
               then
2612
                  Conformance_Error
2613
                    ("(Ada 83) IN must appear in both declarations",
2614
                     New_Formal);
2615
                  return;
2616
               end if;
2617
 
2618
               --  Grouping (use of comma in param lists) must be the same
2619
               --  This is where we catch a misconformance like:
2620
 
2621
               --    A,B : Integer
2622
               --    A : Integer; B : Integer
2623
 
2624
               --  which are represented identically in the tree except
2625
               --  for the setting of the flags More_Ids and Prev_Ids.
2626
 
2627
               if More_Ids (Old_Param) /= More_Ids (New_Param)
2628
                 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2629
               then
2630
                  Conformance_Error
2631
                    ("grouping of & does not match!", New_Formal);
2632
                  return;
2633
               end if;
2634
            end;
2635
         end if;
2636
 
2637
         --  This label is required when skipping controlling formals
2638
 
2639
         <<Skip_Controlling_Formal>>
2640
 
2641
         Next_Formal (Old_Formal);
2642
         Next_Formal (New_Formal);
2643
      end loop;
2644
 
2645
      if Present (Old_Formal) then
2646
         Conformance_Error ("too few parameters!");
2647
         return;
2648
 
2649
      elsif Present (New_Formal) then
2650
         Conformance_Error ("too many parameters!", New_Formal);
2651
         return;
2652
      end if;
2653
   end Check_Conformance;
2654
 
2655
   ------------------------------
2656
   -- Check_Delayed_Subprogram --
2657
   ------------------------------
2658
 
2659
   procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2660
      F : Entity_Id;
2661
 
2662
      procedure Possible_Freeze (T : Entity_Id);
2663
      --  T is the type of either a formal parameter or of the return type.
2664
      --  If T is not yet frozen and needs a delayed freeze, then the
2665
      --  subprogram itself must be delayed.
2666
 
2667
      ---------------------
2668
      -- Possible_Freeze --
2669
      ---------------------
2670
 
2671
      procedure Possible_Freeze (T : Entity_Id) is
2672
      begin
2673
         if Has_Delayed_Freeze (T)
2674
           and then not Is_Frozen (T)
2675
         then
2676
            Set_Has_Delayed_Freeze (Designator);
2677
 
2678
         elsif Is_Access_Type (T)
2679
           and then Has_Delayed_Freeze (Designated_Type (T))
2680
           and then not Is_Frozen (Designated_Type (T))
2681
         then
2682
            Set_Has_Delayed_Freeze (Designator);
2683
         end if;
2684
      end Possible_Freeze;
2685
 
2686
   --  Start of processing for Check_Delayed_Subprogram
2687
 
2688
   begin
2689
      --  Never need to freeze abstract subprogram
2690
 
2691
      if Is_Abstract (Designator) then
2692
         null;
2693
      else
2694
         --  Need delayed freeze if return type itself needs a delayed
2695
         --  freeze and is not yet frozen.
2696
 
2697
         Possible_Freeze (Etype (Designator));
2698
         Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2699
 
2700
         --  Need delayed freeze if any of the formal types themselves need
2701
         --  a delayed freeze and are not yet frozen.
2702
 
2703
         F := First_Formal (Designator);
2704
         while Present (F) loop
2705
            Possible_Freeze (Etype (F));
2706
            Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2707
            Next_Formal (F);
2708
         end loop;
2709
      end if;
2710
 
2711
      --  Mark functions that return by reference. Note that it cannot be
2712
      --  done for delayed_freeze subprograms because the underlying
2713
      --  returned type may not be known yet (for private types)
2714
 
2715
      if not Has_Delayed_Freeze (Designator)
2716
        and then Expander_Active
2717
      then
2718
         declare
2719
            Typ  : constant Entity_Id := Etype (Designator);
2720
            Utyp : constant Entity_Id := Underlying_Type (Typ);
2721
 
2722
         begin
2723
            if Is_Return_By_Reference_Type (Typ) then
2724
               Set_Returns_By_Ref (Designator);
2725
 
2726
            elsif Present (Utyp) and then Controlled_Type (Utyp) then
2727
               Set_Returns_By_Ref (Designator);
2728
            end if;
2729
         end;
2730
      end if;
2731
   end Check_Delayed_Subprogram;
2732
 
2733
   ------------------------------------
2734
   -- Check_Discriminant_Conformance --
2735
   ------------------------------------
2736
 
2737
   procedure Check_Discriminant_Conformance
2738
     (N        : Node_Id;
2739
      Prev     : Entity_Id;
2740
      Prev_Loc : Node_Id)
2741
   is
2742
      Old_Discr      : Entity_Id := First_Discriminant (Prev);
2743
      New_Discr      : Node_Id   := First (Discriminant_Specifications (N));
2744
      New_Discr_Id   : Entity_Id;
2745
      New_Discr_Type : Entity_Id;
2746
 
2747
      procedure Conformance_Error (Msg : String; N : Node_Id);
2748
      --  Post error message for conformance error on given node. Two messages
2749
      --  are output. The first points to the previous declaration with a
2750
      --  general "no conformance" message. The second is the detailed reason,
2751
      --  supplied as Msg. The parameter N provide information for a possible
2752
      --  & insertion in the message.
2753
 
2754
      -----------------------
2755
      -- Conformance_Error --
2756
      -----------------------
2757
 
2758
      procedure Conformance_Error (Msg : String; N : Node_Id) is
2759
      begin
2760
         Error_Msg_Sloc := Sloc (Prev_Loc);
2761
         Error_Msg_N ("not fully conformant with declaration#!", N);
2762
         Error_Msg_NE (Msg, N, N);
2763
      end Conformance_Error;
2764
 
2765
   --  Start of processing for Check_Discriminant_Conformance
2766
 
2767
   begin
2768
      while Present (Old_Discr) and then Present (New_Discr) loop
2769
 
2770
         New_Discr_Id := Defining_Identifier (New_Discr);
2771
 
2772
         --  The subtype mark of the discriminant on the full type has not
2773
         --  been analyzed so we do it here. For an access discriminant a new
2774
         --  type is created.
2775
 
2776
         if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2777
            New_Discr_Type :=
2778
              Access_Definition (N, Discriminant_Type (New_Discr));
2779
 
2780
         else
2781
            Analyze (Discriminant_Type (New_Discr));
2782
            New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2783
         end if;
2784
 
2785
         if not Conforming_Types
2786
                  (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2787
         then
2788
            Conformance_Error ("type of & does not match!", New_Discr_Id);
2789
            return;
2790
         else
2791
            --  Treat the new discriminant as an occurrence of the old one,
2792
            --  for navigation purposes, and fill in some semantic
2793
            --  information, for completeness.
2794
 
2795
            Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2796
            Set_Etype (New_Discr_Id, Etype (Old_Discr));
2797
            Set_Scope (New_Discr_Id, Scope (Old_Discr));
2798
         end if;
2799
 
2800
         --  Names must match
2801
 
2802
         if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2803
            Conformance_Error ("name & does not match!", New_Discr_Id);
2804
            return;
2805
         end if;
2806
 
2807
         --  Default expressions must match
2808
 
2809
         declare
2810
            NewD : constant Boolean :=
2811
                     Present (Expression (New_Discr));
2812
            OldD : constant Boolean :=
2813
                     Present (Expression (Parent (Old_Discr)));
2814
 
2815
         begin
2816
            if NewD or OldD then
2817
 
2818
               --  The old default value has been analyzed and expanded,
2819
               --  because the current full declaration will have frozen
2820
               --  everything before. The new default values have not been
2821
               --  expanded, so expand now to check conformance.
2822
 
2823
               if NewD then
2824
                  Analyze_Per_Use_Expression
2825
                    (Expression (New_Discr), New_Discr_Type);
2826
               end if;
2827
 
2828
               if not (NewD and OldD)
2829
                 or else not Fully_Conformant_Expressions
2830
                              (Expression (Parent (Old_Discr)),
2831
                               Expression (New_Discr))
2832
 
2833
               then
2834
                  Conformance_Error
2835
                    ("default expression for & does not match!",
2836
                     New_Discr_Id);
2837
                  return;
2838
               end if;
2839
            end if;
2840
         end;
2841
 
2842
         --  In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2843
 
2844
         if Ada_Version = Ada_83 then
2845
            declare
2846
               Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2847
 
2848
            begin
2849
               --  Grouping (use of comma in param lists) must be the same
2850
               --  This is where we catch a misconformance like:
2851
 
2852
               --    A,B : Integer
2853
               --    A : Integer; B : Integer
2854
 
2855
               --  which are represented identically in the tree except
2856
               --  for the setting of the flags More_Ids and Prev_Ids.
2857
 
2858
               if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2859
                 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2860
               then
2861
                  Conformance_Error
2862
                    ("grouping of & does not match!", New_Discr_Id);
2863
                  return;
2864
               end if;
2865
            end;
2866
         end if;
2867
 
2868
         Next_Discriminant (Old_Discr);
2869
         Next (New_Discr);
2870
      end loop;
2871
 
2872
      if Present (Old_Discr) then
2873
         Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2874
         return;
2875
 
2876
      elsif Present (New_Discr) then
2877
         Conformance_Error
2878
           ("too many discriminants!", Defining_Identifier (New_Discr));
2879
         return;
2880
      end if;
2881
   end Check_Discriminant_Conformance;
2882
 
2883
   ----------------------------
2884
   -- Check_Fully_Conformant --
2885
   ----------------------------
2886
 
2887
   procedure Check_Fully_Conformant
2888
     (New_Id  : Entity_Id;
2889
      Old_Id  : Entity_Id;
2890
      Err_Loc : Node_Id := Empty)
2891
   is
2892
      Result : Boolean;
2893
   begin
2894
      Check_Conformance
2895
        (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2896
   end Check_Fully_Conformant;
2897
 
2898
   ---------------------------
2899
   -- Check_Mode_Conformant --
2900
   ---------------------------
2901
 
2902
   procedure Check_Mode_Conformant
2903
     (New_Id   : Entity_Id;
2904
      Old_Id   : Entity_Id;
2905
      Err_Loc  : Node_Id := Empty;
2906
      Get_Inst : Boolean := False)
2907
   is
2908
      Result : Boolean;
2909
 
2910
   begin
2911
      Check_Conformance
2912
        (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2913
   end Check_Mode_Conformant;
2914
 
2915
   --------------------------------
2916
   -- Check_Overriding_Indicator --
2917
   --------------------------------
2918
 
2919
   procedure Check_Overriding_Indicator
2920
     (Subp          : Entity_Id;
2921
      Does_Override : Boolean)
2922
   is
2923
      Decl : Node_Id;
2924
      Spec : Node_Id;
2925
 
2926
   begin
2927
      if Ekind (Subp) = E_Enumeration_Literal then
2928
 
2929
         --  No overriding indicator for literals
2930
 
2931
         return;
2932
 
2933
      else
2934
         Decl := Unit_Declaration_Node (Subp);
2935
      end if;
2936
 
2937
      if Nkind (Decl) = N_Subprogram_Declaration
2938
        or else Nkind (Decl) = N_Subprogram_Body
2939
        or else Nkind (Decl) = N_Subprogram_Renaming_Declaration
2940
        or else Nkind (Decl) = N_Subprogram_Body_Stub
2941
      then
2942
         Spec := Specification (Decl);
2943
      else
2944
         return;
2945
      end if;
2946
 
2947
      if not Does_Override then
2948
         if Must_Override (Spec) then
2949
            Error_Msg_NE ("subprogram& is not overriding", Spec, Subp);
2950
         end if;
2951
 
2952
      else
2953
         if Must_Not_Override (Spec) then
2954
            Error_Msg_NE
2955
              ("subprogram& overrides inherited operation", Spec, Subp);
2956
         end if;
2957
      end if;
2958
   end Check_Overriding_Indicator;
2959
 
2960
   -------------------
2961
   -- Check_Returns --
2962
   -------------------
2963
 
2964
   procedure Check_Returns
2965
     (HSS  : Node_Id;
2966
      Mode : Character;
2967
      Err  : out Boolean)
2968
   is
2969
      Handler : Node_Id;
2970
 
2971
      procedure Check_Statement_Sequence (L : List_Id);
2972
      --  Internal recursive procedure to check a list of statements for proper
2973
      --  termination by a return statement (or a transfer of control or a
2974
      --  compound statement that is itself internally properly terminated).
2975
 
2976
      ------------------------------
2977
      -- Check_Statement_Sequence --
2978
      ------------------------------
2979
 
2980
      procedure Check_Statement_Sequence (L : List_Id) is
2981
         Last_Stm : Node_Id;
2982
         Kind     : Node_Kind;
2983
 
2984
         Raise_Exception_Call : Boolean;
2985
         --  Set True if statement sequence terminated by Raise_Exception call
2986
         --  or a Reraise_Occurrence call.
2987
 
2988
      begin
2989
         Raise_Exception_Call := False;
2990
 
2991
         --  Get last real statement
2992
 
2993
         Last_Stm := Last (L);
2994
 
2995
         --  Don't count pragmas
2996
 
2997
         while Nkind (Last_Stm) = N_Pragma
2998
 
2999
         --  Don't count call to SS_Release (can happen after Raise_Exception)
3000
 
3001
           or else
3002
             (Nkind (Last_Stm) = N_Procedure_Call_Statement
3003
                and then
3004
              Nkind (Name (Last_Stm)) = N_Identifier
3005
                and then
3006
              Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
3007
 
3008
         --  Don't count exception junk
3009
 
3010
           or else
3011
             ((Nkind (Last_Stm) = N_Goto_Statement
3012
                 or else Nkind (Last_Stm) = N_Label
3013
                 or else Nkind (Last_Stm) = N_Object_Declaration)
3014
               and then Exception_Junk (Last_Stm))
3015
         loop
3016
            Prev (Last_Stm);
3017
         end loop;
3018
 
3019
         --  Here we have the "real" last statement
3020
 
3021
         Kind := Nkind (Last_Stm);
3022
 
3023
         --  Transfer of control, OK. Note that in the No_Return procedure
3024
         --  case, we already diagnosed any explicit return statements, so
3025
         --  we can treat them as OK in this context.
3026
 
3027
         if Is_Transfer (Last_Stm) then
3028
            return;
3029
 
3030
         --  Check cases of explicit non-indirect procedure calls
3031
 
3032
         elsif Kind = N_Procedure_Call_Statement
3033
           and then Is_Entity_Name (Name (Last_Stm))
3034
         then
3035
            --  Check call to Raise_Exception procedure which is treated
3036
            --  specially, as is a call to Reraise_Occurrence.
3037
 
3038
            --  We suppress the warning in these cases since it is likely that
3039
            --  the programmer really does not expect to deal with the case
3040
            --  of Null_Occurrence, and thus would find a warning about a
3041
            --  missing return curious, and raising Program_Error does not
3042
            --  seem such a bad behavior if this does occur.
3043
 
3044
            if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
3045
                 or else
3046
               Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
3047
            then
3048
               Raise_Exception_Call := True;
3049
 
3050
               --  For Raise_Exception call, test first argument, if it is
3051
               --  an attribute reference for a 'Identity call, then we know
3052
               --  that the call cannot possibly return.
3053
 
3054
               declare
3055
                  Arg : constant Node_Id :=
3056
                          Original_Node (First_Actual (Last_Stm));
3057
 
3058
               begin
3059
                  if Nkind (Arg) = N_Attribute_Reference
3060
                    and then Attribute_Name (Arg) = Name_Identity
3061
                  then
3062
                     return;
3063
                  end if;
3064
               end;
3065
            end if;
3066
 
3067
         --  If statement, need to look inside if there is an else and check
3068
         --  each constituent statement sequence for proper termination.
3069
 
3070
         elsif Kind = N_If_Statement
3071
           and then Present (Else_Statements (Last_Stm))
3072
         then
3073
            Check_Statement_Sequence (Then_Statements (Last_Stm));
3074
            Check_Statement_Sequence (Else_Statements (Last_Stm));
3075
 
3076
            if Present (Elsif_Parts (Last_Stm)) then
3077
               declare
3078
                  Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
3079
 
3080
               begin
3081
                  while Present (Elsif_Part) loop
3082
                     Check_Statement_Sequence (Then_Statements (Elsif_Part));
3083
                     Next (Elsif_Part);
3084
                  end loop;
3085
               end;
3086
            end if;
3087
 
3088
            return;
3089
 
3090
         --  Case statement, check each case for proper termination
3091
 
3092
         elsif Kind = N_Case_Statement then
3093
            declare
3094
               Case_Alt : Node_Id;
3095
 
3096
            begin
3097
               Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
3098
               while Present (Case_Alt) loop
3099
                  Check_Statement_Sequence (Statements (Case_Alt));
3100
                  Next_Non_Pragma (Case_Alt);
3101
               end loop;
3102
            end;
3103
 
3104
            return;
3105
 
3106
         --  Block statement, check its handled sequence of statements
3107
 
3108
         elsif Kind = N_Block_Statement then
3109
            declare
3110
               Err1 : Boolean;
3111
 
3112
            begin
3113
               Check_Returns
3114
                 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
3115
 
3116
               if Err1 then
3117
                  Err := True;
3118
               end if;
3119
 
3120
               return;
3121
            end;
3122
 
3123
         --  Loop statement. If there is an iteration scheme, we can definitely
3124
         --  fall out of the loop. Similarly if there is an exit statement, we
3125
         --  can fall out. In either case we need a following return.
3126
 
3127
         elsif Kind = N_Loop_Statement then
3128
            if Present (Iteration_Scheme (Last_Stm))
3129
              or else Has_Exit (Entity (Identifier (Last_Stm)))
3130
            then
3131
               null;
3132
 
3133
            --  A loop with no exit statement or iteration scheme if either
3134
            --  an inifite loop, or it has some other exit (raise/return).
3135
            --  In either case, no warning is required.
3136
 
3137
            else
3138
               return;
3139
            end if;
3140
 
3141
         --  Timed entry call, check entry call and delay alternatives
3142
 
3143
         --  Note: in expanded code, the timed entry call has been converted
3144
         --  to a set of expanded statements on which the check will work
3145
         --  correctly in any case.
3146
 
3147
         elsif Kind = N_Timed_Entry_Call then
3148
            declare
3149
               ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3150
               DCA : constant Node_Id := Delay_Alternative      (Last_Stm);
3151
 
3152
            begin
3153
               --  If statement sequence of entry call alternative is missing,
3154
               --  then we can definitely fall through, and we post the error
3155
               --  message on the entry call alternative itself.
3156
 
3157
               if No (Statements (ECA)) then
3158
                  Last_Stm := ECA;
3159
 
3160
               --  If statement sequence of delay alternative is missing, then
3161
               --  we can definitely fall through, and we post the error
3162
               --  message on the delay alternative itself.
3163
 
3164
               --  Note: if both ECA and DCA are missing the return, then we
3165
               --  post only one message, should be enough to fix the bugs.
3166
               --  If not we will get a message next time on the DCA when the
3167
               --  ECA is fixed!
3168
 
3169
               elsif No (Statements (DCA)) then
3170
                  Last_Stm := DCA;
3171
 
3172
               --  Else check both statement sequences
3173
 
3174
               else
3175
                  Check_Statement_Sequence (Statements (ECA));
3176
                  Check_Statement_Sequence (Statements (DCA));
3177
                  return;
3178
               end if;
3179
            end;
3180
 
3181
         --  Conditional entry call, check entry call and else part
3182
 
3183
         --  Note: in expanded code, the conditional entry call has been
3184
         --  converted to a set of expanded statements on which the check
3185
         --  will work correctly in any case.
3186
 
3187
         elsif Kind = N_Conditional_Entry_Call then
3188
            declare
3189
               ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
3190
 
3191
            begin
3192
               --  If statement sequence of entry call alternative is missing,
3193
               --  then we can definitely fall through, and we post the error
3194
               --  message on the entry call alternative itself.
3195
 
3196
               if No (Statements (ECA)) then
3197
                  Last_Stm := ECA;
3198
 
3199
               --  Else check statement sequence and else part
3200
 
3201
               else
3202
                  Check_Statement_Sequence (Statements (ECA));
3203
                  Check_Statement_Sequence (Else_Statements (Last_Stm));
3204
                  return;
3205
               end if;
3206
            end;
3207
         end if;
3208
 
3209
         --  If we fall through, issue appropriate message
3210
 
3211
         if Mode = 'F' then
3212
 
3213
            if not Raise_Exception_Call then
3214
               Error_Msg_N
3215
                 ("?RETURN statement missing following this statement!",
3216
                  Last_Stm);
3217
               Error_Msg_N
3218
                 ("\?Program_Error may be raised at run time",
3219
                  Last_Stm);
3220
            end if;
3221
 
3222
            --  Note: we set Err even though we have not issued a warning
3223
            --  because we still have a case of a missing return. This is
3224
            --  an extremely marginal case, probably will never be noticed
3225
            --  but we might as well get it right.
3226
 
3227
            Err := True;
3228
 
3229
         else
3230
            Error_Msg_N
3231
              ("implied return after this statement not allowed (No_Return)",
3232
               Last_Stm);
3233
         end if;
3234
      end Check_Statement_Sequence;
3235
 
3236
   --  Start of processing for Check_Returns
3237
 
3238
   begin
3239
      Err := False;
3240
      Check_Statement_Sequence (Statements (HSS));
3241
 
3242
      if Present (Exception_Handlers (HSS)) then
3243
         Handler := First_Non_Pragma (Exception_Handlers (HSS));
3244
         while Present (Handler) loop
3245
            Check_Statement_Sequence (Statements (Handler));
3246
            Next_Non_Pragma (Handler);
3247
         end loop;
3248
      end if;
3249
   end Check_Returns;
3250
 
3251
   ----------------------------
3252
   -- Check_Subprogram_Order --
3253
   ----------------------------
3254
 
3255
   procedure Check_Subprogram_Order (N : Node_Id) is
3256
 
3257
      function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
3258
      --  This is used to check if S1 > S2 in the sense required by this
3259
      --  test, for example nameab < namec, but name2 < name10.
3260
 
3261
      -----------------------------
3262
      -- Subprogram_Name_Greater --
3263
      -----------------------------
3264
 
3265
      function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
3266
         L1, L2 : Positive;
3267
         N1, N2 : Natural;
3268
 
3269
      begin
3270
         --  Remove trailing numeric parts
3271
 
3272
         L1 := S1'Last;
3273
         while S1 (L1) in '0' .. '9' loop
3274
            L1 := L1 - 1;
3275
         end loop;
3276
 
3277
         L2 := S2'Last;
3278
         while S2 (L2) in '0' .. '9' loop
3279
            L2 := L2 - 1;
3280
         end loop;
3281
 
3282
         --  If non-numeric parts non-equal, that's decisive
3283
 
3284
         if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
3285
            return False;
3286
 
3287
         elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
3288
            return True;
3289
 
3290
         --  If non-numeric parts equal, compare suffixed numeric parts. Note
3291
         --  that a missing suffix is treated as numeric zero in this test.
3292
 
3293
         else
3294
            N1 := 0;
3295
            while L1 < S1'Last loop
3296
               L1 := L1 + 1;
3297
               N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
3298
            end loop;
3299
 
3300
            N2 := 0;
3301
            while L2 < S2'Last loop
3302
               L2 := L2 + 1;
3303
               N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
3304
            end loop;
3305
 
3306
            return N1 > N2;
3307
         end if;
3308
      end Subprogram_Name_Greater;
3309
 
3310
   --  Start of processing for Check_Subprogram_Order
3311
 
3312
   begin
3313
      --  Check body in alpha order if this is option
3314
 
3315
      if Style_Check
3316
        and then Style_Check_Order_Subprograms
3317
        and then Nkind (N) = N_Subprogram_Body
3318
        and then Comes_From_Source (N)
3319
        and then In_Extended_Main_Source_Unit (N)
3320
      then
3321
         declare
3322
            LSN : String_Ptr
3323
                    renames Scope_Stack.Table
3324
                              (Scope_Stack.Last).Last_Subprogram_Name;
3325
 
3326
            Body_Id : constant Entity_Id :=
3327
                        Defining_Entity (Specification (N));
3328
 
3329
         begin
3330
            Get_Decoded_Name_String (Chars (Body_Id));
3331
 
3332
            if LSN /= null then
3333
               if Subprogram_Name_Greater
3334
                    (LSN.all, Name_Buffer (1 .. Name_Len))
3335
               then
3336
                  Style.Subprogram_Not_In_Alpha_Order (Body_Id);
3337
               end if;
3338
 
3339
               Free (LSN);
3340
            end if;
3341
 
3342
            LSN := new String'(Name_Buffer (1 .. Name_Len));
3343
         end;
3344
      end if;
3345
   end Check_Subprogram_Order;
3346
 
3347
   ------------------------------
3348
   -- Check_Subtype_Conformant --
3349
   ------------------------------
3350
 
3351
   procedure Check_Subtype_Conformant
3352
     (New_Id  : Entity_Id;
3353
      Old_Id  : Entity_Id;
3354
      Err_Loc : Node_Id := Empty)
3355
   is
3356
      Result : Boolean;
3357
   begin
3358
      Check_Conformance
3359
        (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
3360
   end Check_Subtype_Conformant;
3361
 
3362
   ---------------------------
3363
   -- Check_Type_Conformant --
3364
   ---------------------------
3365
 
3366
   procedure Check_Type_Conformant
3367
     (New_Id  : Entity_Id;
3368
      Old_Id  : Entity_Id;
3369
      Err_Loc : Node_Id := Empty)
3370
   is
3371
      Result : Boolean;
3372
   begin
3373
      Check_Conformance
3374
        (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
3375
   end Check_Type_Conformant;
3376
 
3377
   ----------------------
3378
   -- Conforming_Types --
3379
   ----------------------
3380
 
3381
   function Conforming_Types
3382
     (T1       : Entity_Id;
3383
      T2       : Entity_Id;
3384
      Ctype    : Conformance_Type;
3385
      Get_Inst : Boolean := False) return Boolean
3386
   is
3387
      Type_1 : Entity_Id := T1;
3388
      Type_2 : Entity_Id := T2;
3389
      Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3390
 
3391
      function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3392
      --  If neither T1 nor T2 are generic actual types, or if they are
3393
      --  in different scopes (e.g. parent and child instances), then verify
3394
      --  that the base types are equal. Otherwise T1 and T2 must be
3395
      --  on the same subtype chain. The whole purpose of this procedure
3396
      --  is to prevent spurious ambiguities in an instantiation that may
3397
      --  arise if two distinct generic types are instantiated with the
3398
      --  same actual.
3399
 
3400
      ----------------------
3401
      -- Base_Types_Match --
3402
      ----------------------
3403
 
3404
      function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3405
      begin
3406
         if T1 = T2 then
3407
            return True;
3408
 
3409
         elsif Base_Type (T1) = Base_Type (T2) then
3410
 
3411
            --  The following is too permissive. A more precise test must
3412
            --  check that the generic actual is an ancestor subtype of the
3413
            --  other ???.
3414
 
3415
            return not Is_Generic_Actual_Type (T1)
3416
              or else not Is_Generic_Actual_Type (T2)
3417
              or else Scope (T1) /= Scope (T2);
3418
 
3419
         --  In some cases a type imported through a limited_with clause,
3420
         --  and its non-limited view are both visible, for example in an
3421
         --  anonymous access_to_classwide type in a formal. Both entities
3422
         --  designate the same type.
3423
 
3424
         elsif From_With_Type (T1)
3425
           and then Ekind (T1) = E_Incomplete_Type
3426
           and then T2 = Non_Limited_View (T1)
3427
         then
3428
            return True;
3429
 
3430
         elsif From_With_Type (T2)
3431
           and then Ekind (T2) = E_Incomplete_Type
3432
           and then T1 = Non_Limited_View (T2)
3433
         then
3434
            return True;
3435
 
3436
         else
3437
            return False;
3438
         end if;
3439
      end Base_Types_Match;
3440
 
3441
      --  Start of processing for Conforming_Types
3442
 
3443
   begin
3444
      --  The context is an instance association for a formal
3445
      --  access-to-subprogram type; the formal parameter types require
3446
      --  mapping because they may denote other formal parameters of the
3447
      --  generic unit.
3448
 
3449
      if Get_Inst then
3450
         Type_1 := Get_Instance_Of (T1);
3451
         Type_2 := Get_Instance_Of (T2);
3452
      end if;
3453
 
3454
      --  First see if base types match
3455
 
3456
      if Base_Types_Match (Type_1, Type_2) then
3457
         return Ctype <= Mode_Conformant
3458
           or else Subtypes_Statically_Match (Type_1, Type_2);
3459
 
3460
      elsif Is_Incomplete_Or_Private_Type (Type_1)
3461
        and then Present (Full_View (Type_1))
3462
        and then Base_Types_Match (Full_View (Type_1), Type_2)
3463
      then
3464
         return Ctype <= Mode_Conformant
3465
           or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3466
 
3467
      elsif Ekind (Type_2) = E_Incomplete_Type
3468
        and then Present (Full_View (Type_2))
3469
        and then Base_Types_Match (Type_1, Full_View (Type_2))
3470
      then
3471
         return Ctype <= Mode_Conformant
3472
           or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3473
 
3474
      elsif Is_Private_Type (Type_2)
3475
        and then In_Instance
3476
        and then Present (Full_View (Type_2))
3477
        and then Base_Types_Match (Type_1, Full_View (Type_2))
3478
      then
3479
         return Ctype <= Mode_Conformant
3480
           or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3481
      end if;
3482
 
3483
      --  Ada 2005 (AI-254): Anonymous access to subprogram types must be
3484
      --  treated recursively because they carry a signature.
3485
 
3486
      Are_Anonymous_Access_To_Subprogram_Types :=
3487
 
3488
         --  Case 1: Anonymous access to subprogram types
3489
 
3490
        (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3491
           and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3492
 
3493
         --  Case 2: Anonymous access to PROTECTED subprogram types. In this
3494
         --  case the anonymous type_declaration has been replaced by an
3495
         --  occurrence of an internal access to subprogram type declaration
3496
         --  available through the Original_Access_Type attribute
3497
 
3498
        or else
3499
          (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3500
            and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3501
            and then not Comes_From_Source (Type_1)
3502
            and then not Comes_From_Source (Type_2)
3503
            and then Present (Original_Access_Type (Type_1))
3504
            and then Present (Original_Access_Type (Type_2))
3505
            and then Ekind (Original_Access_Type (Type_1)) =
3506
                       E_Anonymous_Access_Protected_Subprogram_Type
3507
            and then Ekind (Original_Access_Type (Type_2)) =
3508
                       E_Anonymous_Access_Protected_Subprogram_Type);
3509
 
3510
      --  Test anonymous access type case. For this case, static subtype
3511
      --  matching is required for mode conformance (RM 6.3.1(15))
3512
 
3513
      if (Ekind (Type_1) = E_Anonymous_Access_Type
3514
            and then Ekind (Type_2) = E_Anonymous_Access_Type)
3515
        or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3516
      then
3517
         declare
3518
            Desig_1 : Entity_Id;
3519
            Desig_2 : Entity_Id;
3520
 
3521
         begin
3522
            Desig_1 := Directly_Designated_Type (Type_1);
3523
 
3524
            --  An access parameter can designate an incomplete type
3525
            --  If the incomplete type is the limited view of a type
3526
            --  from a limited_with_clause, check whether the non-limited
3527
            --  view is available.
3528
 
3529
            if Ekind (Desig_1) = E_Incomplete_Type then
3530
               if Present (Full_View (Desig_1)) then
3531
                  Desig_1 := Full_View (Desig_1);
3532
 
3533
               elsif Present (Non_Limited_View (Desig_1)) then
3534
                  Desig_1 := Non_Limited_View (Desig_1);
3535
               end if;
3536
            end if;
3537
 
3538
            Desig_2 := Directly_Designated_Type (Type_2);
3539
 
3540
            if Ekind (Desig_2) = E_Incomplete_Type then
3541
               if Present (Full_View (Desig_2)) then
3542
                  Desig_2 := Full_View (Desig_2);
3543
               elsif Present (Non_Limited_View (Desig_2)) then
3544
                  Desig_2 := Non_Limited_View (Desig_2);
3545
               end if;
3546
            end if;
3547
 
3548
            --  The context is an instance association for a formal
3549
            --  access-to-subprogram type; formal access parameter designated
3550
            --  types require mapping because they may denote other formal
3551
            --  parameters of the generic unit.
3552
 
3553
            if Get_Inst then
3554
               Desig_1 := Get_Instance_Of (Desig_1);
3555
               Desig_2 := Get_Instance_Of (Desig_2);
3556
            end if;
3557
 
3558
            --  It is possible for a Class_Wide_Type to be introduced for an
3559
            --  incomplete type, in which case there is a separate class_ wide
3560
            --  type for the full view. The types conform if their Etypes
3561
            --  conform, i.e. one may be the full view of the other. This can
3562
            --  only happen in the context of an access parameter, other uses
3563
            --  of an incomplete Class_Wide_Type are illegal.
3564
 
3565
            if Is_Class_Wide_Type (Desig_1)
3566
              and then Is_Class_Wide_Type (Desig_2)
3567
            then
3568
               return
3569
                 Conforming_Types
3570
                   (Etype (Base_Type (Desig_1)),
3571
                    Etype (Base_Type (Desig_2)), Ctype);
3572
 
3573
            elsif Are_Anonymous_Access_To_Subprogram_Types then
3574
               if Ada_Version < Ada_05 then
3575
                  return Ctype = Type_Conformant
3576
                    or else
3577
                      Subtypes_Statically_Match (Desig_1, Desig_2);
3578
 
3579
               --  We must check the conformance of the signatures themselves
3580
 
3581
               else
3582
                  declare
3583
                     Conformant : Boolean;
3584
                  begin
3585
                     Check_Conformance
3586
                       (Desig_1, Desig_2, Ctype, False, Conformant);
3587
                     return Conformant;
3588
                  end;
3589
               end if;
3590
 
3591
            else
3592
               return Base_Type (Desig_1) = Base_Type (Desig_2)
3593
                and then (Ctype = Type_Conformant
3594
                            or else
3595
                          Subtypes_Statically_Match (Desig_1, Desig_2));
3596
            end if;
3597
         end;
3598
 
3599
      --  Otherwise definitely no match
3600
 
3601
      else
3602
         return False;
3603
      end if;
3604
   end Conforming_Types;
3605
 
3606
   --------------------------
3607
   -- Create_Extra_Formals --
3608
   --------------------------
3609
 
3610
   procedure Create_Extra_Formals (E : Entity_Id) is
3611
      Formal      : Entity_Id;
3612
      Last_Extra  : Entity_Id;
3613
      Formal_Type : Entity_Id;
3614
      P_Formal    : Entity_Id := Empty;
3615
 
3616
      function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3617
      --  Add an extra formal, associated with the current Formal. The extra
3618
      --  formal is added to the list of extra formals, and also returned as
3619
      --  the result. These formals are always of mode IN.
3620
 
3621
      ----------------------
3622
      -- Add_Extra_Formal --
3623
      ----------------------
3624
 
3625
      function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3626
         EF : constant Entity_Id :=
3627
                Make_Defining_Identifier (Sloc (Formal),
3628
                  Chars => New_External_Name (Chars (Formal), 'F'));
3629
 
3630
      begin
3631
         --  We never generate extra formals if expansion is not active
3632
         --  because we don't need them unless we are generating code.
3633
 
3634
         if not Expander_Active then
3635
            return Empty;
3636
         end if;
3637
 
3638
         --  A little optimization. Never generate an extra formal for the
3639
         --  _init operand of an initialization procedure, since it could
3640
         --  never be used.
3641
 
3642
         if Chars (Formal) = Name_uInit then
3643
            return Empty;
3644
         end if;
3645
 
3646
         Set_Ekind           (EF, E_In_Parameter);
3647
         Set_Actual_Subtype  (EF, Typ);
3648
         Set_Etype           (EF, Typ);
3649
         Set_Scope           (EF, Scope (Formal));
3650
         Set_Mechanism       (EF, Default_Mechanism);
3651
         Set_Formal_Validity (EF);
3652
 
3653
         Set_Extra_Formal (Last_Extra, EF);
3654
         Last_Extra := EF;
3655
         return EF;
3656
      end Add_Extra_Formal;
3657
 
3658
   --  Start of processing for Create_Extra_Formals
3659
 
3660
   begin
3661
      --  If this is a derived subprogram then the subtypes of the parent
3662
      --  subprogram's formal parameters will be used to to determine the need
3663
      --  for extra formals.
3664
 
3665
      if Is_Overloadable (E) and then Present (Alias (E)) then
3666
         P_Formal := First_Formal (Alias (E));
3667
      end if;
3668
 
3669
      Last_Extra := Empty;
3670
      Formal := First_Formal (E);
3671
      while Present (Formal) loop
3672
         Last_Extra := Formal;
3673
         Next_Formal (Formal);
3674
      end loop;
3675
 
3676
      --  If Extra_formals where already created, don't do it again. This
3677
      --  situation may arise for subprogram types created as part of
3678
      --  dispatching calls (see Expand_Dispatching_Call)
3679
 
3680
      if Present (Last_Extra) and then
3681
        Present (Extra_Formal (Last_Extra))
3682
      then
3683
         return;
3684
      end if;
3685
 
3686
      Formal := First_Formal (E);
3687
 
3688
      while Present (Formal) loop
3689
 
3690
         --  Create extra formal for supporting the attribute 'Constrained.
3691
         --  The case of a private type view without discriminants also
3692
         --  requires the extra formal if the underlying type has defaulted
3693
         --  discriminants.
3694
 
3695
         if Ekind (Formal) /= E_In_Parameter then
3696
            if Present (P_Formal) then
3697
               Formal_Type := Etype (P_Formal);
3698
            else
3699
               Formal_Type := Etype (Formal);
3700
            end if;
3701
 
3702
            --  Do not produce extra formals for Unchecked_Union parameters.
3703
            --  Jump directly to the end of the loop.
3704
 
3705
            if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3706
               goto Skip_Extra_Formal_Generation;
3707
            end if;
3708
 
3709
            if not Has_Discriminants (Formal_Type)
3710
              and then Ekind (Formal_Type) in Private_Kind
3711
              and then Present (Underlying_Type (Formal_Type))
3712
            then
3713
               Formal_Type := Underlying_Type (Formal_Type);
3714
            end if;
3715
 
3716
            if Has_Discriminants (Formal_Type)
3717
              and then
3718
                ((not Is_Constrained (Formal_Type)
3719
                    and then not Is_Indefinite_Subtype (Formal_Type))
3720
                  or else Present (Extra_Formal (Formal)))
3721
            then
3722
               Set_Extra_Constrained
3723
                 (Formal, Add_Extra_Formal (Standard_Boolean));
3724
            end if;
3725
         end if;
3726
 
3727
         --  Create extra formal for supporting accessibility checking
3728
 
3729
         --  This is suppressed if we specifically suppress accessibility
3730
         --  checks at the pacage level for either the subprogram, or the
3731
         --  package in which it resides. However, we do not suppress it
3732
         --  simply if the scope has accessibility checks suppressed, since
3733
         --  this could cause trouble when clients are compiled with a
3734
         --  different suppression setting. The explicit checks at the
3735
         --  package level are safe from this point of view.
3736
 
3737
         if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3738
           and then not
3739
             (Explicit_Suppress (E, Accessibility_Check)
3740
               or else
3741
              Explicit_Suppress (Scope (E), Accessibility_Check))
3742
           and then
3743
             (not Present (P_Formal)
3744
               or else Present (Extra_Accessibility (P_Formal)))
3745
         then
3746
            --  Temporary kludge: for now we avoid creating the extra formal
3747
            --  for access parameters of protected operations because of
3748
            --  problem with the case of internal protected calls. ???
3749
 
3750
            if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3751
              and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3752
            then
3753
               Set_Extra_Accessibility
3754
                 (Formal, Add_Extra_Formal (Standard_Natural));
3755
            end if;
3756
         end if;
3757
 
3758
         if Present (P_Formal) then
3759
            Next_Formal (P_Formal);
3760
         end if;
3761
 
3762
         --  This label is required when skipping extra formal generation for
3763
         --  Unchecked_Union parameters.
3764
 
3765
         <<Skip_Extra_Formal_Generation>>
3766
 
3767
         Next_Formal (Formal);
3768
      end loop;
3769
   end Create_Extra_Formals;
3770
 
3771
   -----------------------------
3772
   -- Enter_Overloaded_Entity --
3773
   -----------------------------
3774
 
3775
   procedure Enter_Overloaded_Entity (S : Entity_Id) is
3776
      E   : Entity_Id := Current_Entity_In_Scope (S);
3777
      C_E : Entity_Id := Current_Entity (S);
3778
 
3779
   begin
3780
      if Present (E) then
3781
         Set_Has_Homonym (E);
3782
         Set_Has_Homonym (S);
3783
      end if;
3784
 
3785
      Set_Is_Immediately_Visible (S);
3786
      Set_Scope (S, Current_Scope);
3787
 
3788
      --  Chain new entity if front of homonym in current scope, so that
3789
      --  homonyms are contiguous.
3790
 
3791
      if Present (E)
3792
        and then E /= C_E
3793
      then
3794
         while Homonym (C_E) /= E loop
3795
            C_E := Homonym (C_E);
3796
         end loop;
3797
 
3798
         Set_Homonym (C_E, S);
3799
 
3800
      else
3801
         E := C_E;
3802
         Set_Current_Entity (S);
3803
      end if;
3804
 
3805
      Set_Homonym (S, E);
3806
 
3807
      Append_Entity (S, Current_Scope);
3808
      Set_Public_Status (S);
3809
 
3810
      if Debug_Flag_E then
3811
         Write_Str ("New overloaded entity chain: ");
3812
         Write_Name (Chars (S));
3813
 
3814
         E := S;
3815
         while Present (E) loop
3816
            Write_Str (" "); Write_Int (Int (E));
3817
            E := Homonym (E);
3818
         end loop;
3819
 
3820
         Write_Eol;
3821
      end if;
3822
 
3823
      --  Generate warning for hiding
3824
 
3825
      if Warn_On_Hiding
3826
        and then Comes_From_Source (S)
3827
        and then In_Extended_Main_Source_Unit (S)
3828
      then
3829
         E := S;
3830
         loop
3831
            E := Homonym (E);
3832
            exit when No (E);
3833
 
3834
            --  Warn unless genuine overloading
3835
 
3836
            if (not Is_Overloadable (E))
3837
              or else Subtype_Conformant (E, S)
3838
            then
3839
               Error_Msg_Sloc := Sloc (E);
3840
               Error_Msg_N ("declaration of & hides one#?", S);
3841
            end if;
3842
         end loop;
3843
      end if;
3844
   end Enter_Overloaded_Entity;
3845
 
3846
   -----------------------------
3847
   -- Find_Corresponding_Spec --
3848
   -----------------------------
3849
 
3850
   function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3851
      Spec       : constant Node_Id   := Specification (N);
3852
      Designator : constant Entity_Id := Defining_Entity (Spec);
3853
 
3854
      E : Entity_Id;
3855
 
3856
   begin
3857
      E := Current_Entity (Designator);
3858
 
3859
      while Present (E) loop
3860
 
3861
         --  We are looking for a matching spec. It must have the same scope,
3862
         --  and the same name, and either be type conformant, or be the case
3863
         --  of a library procedure spec and its body (which belong to one
3864
         --  another regardless of whether they are type conformant or not).
3865
 
3866
         if Scope (E) = Current_Scope then
3867
            if Current_Scope = Standard_Standard
3868
              or else (Ekind (E) = Ekind (Designator)
3869
                         and then Type_Conformant (E, Designator))
3870
            then
3871
               --  Within an instantiation, we know that spec and body are
3872
               --  subtype conformant, because they were subtype conformant
3873
               --  in the generic. We choose the subtype-conformant entity
3874
               --  here as well, to resolve spurious ambiguities in the
3875
               --  instance that were not present in the generic (i.e. when
3876
               --  two different types are given the same actual). If we are
3877
               --  looking for a spec to match a body, full conformance is
3878
               --  expected.
3879
 
3880
               if In_Instance then
3881
                  Set_Convention (Designator, Convention (E));
3882
 
3883
                  if Nkind (N) = N_Subprogram_Body
3884
                    and then Present (Homonym (E))
3885
                    and then not Fully_Conformant (E, Designator)
3886
                  then
3887
                     goto Next_Entity;
3888
 
3889
                  elsif not Subtype_Conformant (E, Designator) then
3890
                     goto Next_Entity;
3891
                  end if;
3892
               end if;
3893
 
3894
               if not Has_Completion (E) then
3895
 
3896
                  if Nkind (N) /= N_Subprogram_Body_Stub then
3897
                     Set_Corresponding_Spec (N, E);
3898
                  end if;
3899
 
3900
                  Set_Has_Completion (E);
3901
                  return E;
3902
 
3903
               elsif Nkind (Parent (N)) = N_Subunit then
3904
 
3905
                  --  If this is the proper body of a subunit, the completion
3906
                  --  flag is set when analyzing the stub.
3907
 
3908
                  return E;
3909
 
3910
               --  If body already exists, this is an error unless the
3911
               --  previous declaration is the implicit declaration of
3912
               --  a derived subprogram, or this is a spurious overloading
3913
               --  in an instance.
3914
 
3915
               elsif No (Alias (E))
3916
                 and then not Is_Intrinsic_Subprogram (E)
3917
                 and then not In_Instance
3918
               then
3919
                  Error_Msg_Sloc := Sloc (E);
3920
                  if Is_Imported (E) then
3921
                     Error_Msg_NE
3922
                      ("body not allowed for imported subprogram & declared#",
3923
                        N, E);
3924
                  else
3925
                     Error_Msg_NE ("duplicate body for & declared#", N, E);
3926
                  end if;
3927
               end if;
3928
 
3929
            elsif Is_Child_Unit (E)
3930
              and then
3931
                Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3932
              and then
3933
                Nkind (Parent (Unit_Declaration_Node (Designator)))
3934
                  = N_Compilation_Unit
3935
            then
3936
 
3937
               --  Child units cannot be overloaded, so a conformance mismatch
3938
               --  between body and a previous spec is an error.
3939
 
3940
               Error_Msg_N
3941
                 ("body of child unit does not match previous declaration", N);
3942
            end if;
3943
         end if;
3944
 
3945
         <<Next_Entity>>
3946
            E := Homonym (E);
3947
      end loop;
3948
 
3949
      --  On exit, we know that no previous declaration of subprogram exists
3950
 
3951
      return Empty;
3952
   end Find_Corresponding_Spec;
3953
 
3954
   ----------------------
3955
   -- Fully_Conformant --
3956
   ----------------------
3957
 
3958
   function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3959
      Result : Boolean;
3960
   begin
3961
      Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3962
      return Result;
3963
   end Fully_Conformant;
3964
 
3965
   ----------------------------------
3966
   -- Fully_Conformant_Expressions --
3967
   ----------------------------------
3968
 
3969
   function Fully_Conformant_Expressions
3970
     (Given_E1 : Node_Id;
3971
      Given_E2 : Node_Id) return Boolean
3972
   is
3973
      E1 : constant Node_Id := Original_Node (Given_E1);
3974
      E2 : constant Node_Id := Original_Node (Given_E2);
3975
      --  We always test conformance on original nodes, since it is possible
3976
      --  for analysis and/or expansion to make things look as though they
3977
      --  conform when they do not, e.g. by converting 1+2 into 3.
3978
 
3979
      function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3980
        renames Fully_Conformant_Expressions;
3981
 
3982
      function FCL (L1, L2 : List_Id) return Boolean;
3983
      --  Compare elements of two lists for conformance. Elements have to
3984
      --  be conformant, and actuals inserted as default parameters do not
3985
      --  match explicit actuals with the same value.
3986
 
3987
      function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3988
      --  Compare an operator node with a function call
3989
 
3990
      ---------
3991
      -- FCL --
3992
      ---------
3993
 
3994
      function FCL (L1, L2 : List_Id) return Boolean is
3995
         N1, N2 : Node_Id;
3996
 
3997
      begin
3998
         if L1 = No_List then
3999
            N1 := Empty;
4000
         else
4001
            N1 := First (L1);
4002
         end if;
4003
 
4004
         if L2 = No_List then
4005
            N2 := Empty;
4006
         else
4007
            N2 := First (L2);
4008
         end if;
4009
 
4010
         --  Compare two lists, skipping rewrite insertions (we want to
4011
         --  compare the original trees, not the expanded versions!)
4012
 
4013
         loop
4014
            if Is_Rewrite_Insertion (N1) then
4015
               Next (N1);
4016
            elsif Is_Rewrite_Insertion (N2) then
4017
               Next (N2);
4018
            elsif No (N1) then
4019
               return No (N2);
4020
            elsif No (N2) then
4021
               return False;
4022
            elsif not FCE (N1, N2) then
4023
               return False;
4024
            else
4025
               Next (N1);
4026
               Next (N2);
4027
            end if;
4028
         end loop;
4029
      end FCL;
4030
 
4031
      ---------
4032
      -- FCO --
4033
      ---------
4034
 
4035
      function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
4036
         Actuals : constant List_Id := Parameter_Associations (Call_Node);
4037
         Act     : Node_Id;
4038
 
4039
      begin
4040
         if No (Actuals)
4041
            or else Entity (Op_Node) /= Entity (Name (Call_Node))
4042
         then
4043
            return False;
4044
 
4045
         else
4046
            Act := First (Actuals);
4047
 
4048
            if Nkind (Op_Node) in N_Binary_Op then
4049
 
4050
               if not FCE (Left_Opnd (Op_Node), Act) then
4051
                  return False;
4052
               end if;
4053
 
4054
               Next (Act);
4055
            end if;
4056
 
4057
            return Present (Act)
4058
              and then FCE (Right_Opnd (Op_Node), Act)
4059
              and then No (Next (Act));
4060
         end if;
4061
      end FCO;
4062
 
4063
   --  Start of processing for Fully_Conformant_Expressions
4064
 
4065
   begin
4066
      --  Non-conformant if paren count does not match. Note: if some idiot
4067
      --  complains that we don't do this right for more than 3 levels of
4068
      --  parentheses, they will be treated with the respect they deserve :-)
4069
 
4070
      if Paren_Count (E1) /= Paren_Count (E2) then
4071
         return False;
4072
 
4073
      --  If same entities are referenced, then they are conformant even if
4074
      --  they have different forms (RM 8.3.1(19-20)).
4075
 
4076
      elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
4077
         if Present (Entity (E1)) then
4078
            return Entity (E1) = Entity (E2)
4079
              or else (Chars (Entity (E1)) = Chars (Entity (E2))
4080
                        and then Ekind (Entity (E1)) = E_Discriminant
4081
                        and then Ekind (Entity (E2)) = E_In_Parameter);
4082
 
4083
         elsif Nkind (E1) = N_Expanded_Name
4084
           and then Nkind (E2) = N_Expanded_Name
4085
           and then Nkind (Selector_Name (E1)) = N_Character_Literal
4086
           and then Nkind (Selector_Name (E2)) = N_Character_Literal
4087
         then
4088
            return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
4089
 
4090
         else
4091
            --  Identifiers in component associations don't always have
4092
            --  entities, but their names must conform.
4093
 
4094
            return Nkind  (E1) = N_Identifier
4095
              and then Nkind (E2) = N_Identifier
4096
              and then Chars (E1) = Chars (E2);
4097
         end if;
4098
 
4099
      elsif Nkind (E1) = N_Character_Literal
4100
        and then Nkind (E2) = N_Expanded_Name
4101
      then
4102
         return Nkind (Selector_Name (E2)) = N_Character_Literal
4103
           and then Chars (E1) = Chars (Selector_Name (E2));
4104
 
4105
      elsif Nkind (E2) = N_Character_Literal
4106
        and then Nkind (E1) = N_Expanded_Name
4107
      then
4108
         return Nkind (Selector_Name (E1)) = N_Character_Literal
4109
           and then Chars (E2) = Chars (Selector_Name (E1));
4110
 
4111
      elsif Nkind (E1) in N_Op
4112
        and then Nkind (E2) = N_Function_Call
4113
      then
4114
         return FCO (E1, E2);
4115
 
4116
      elsif Nkind (E2) in N_Op
4117
        and then Nkind (E1) = N_Function_Call
4118
      then
4119
         return FCO (E2, E1);
4120
 
4121
      --  Otherwise we must have the same syntactic entity
4122
 
4123
      elsif Nkind (E1) /= Nkind (E2) then
4124
         return False;
4125
 
4126
      --  At this point, we specialize by node type
4127
 
4128
      else
4129
         case Nkind (E1) is
4130
 
4131
            when N_Aggregate =>
4132
               return
4133
                 FCL (Expressions (E1), Expressions (E2))
4134
                   and then FCL (Component_Associations (E1),
4135
                                 Component_Associations (E2));
4136
 
4137
            when N_Allocator =>
4138
               if Nkind (Expression (E1)) = N_Qualified_Expression
4139
                    or else
4140
                  Nkind (Expression (E2)) = N_Qualified_Expression
4141
               then
4142
                  return FCE (Expression (E1), Expression (E2));
4143
 
4144
               --  Check that the subtype marks and any constraints
4145
               --  are conformant
4146
 
4147
               else
4148
                  declare
4149
                     Indic1 : constant Node_Id := Expression (E1);
4150
                     Indic2 : constant Node_Id := Expression (E2);
4151
                     Elt1   : Node_Id;
4152
                     Elt2   : Node_Id;
4153
 
4154
                  begin
4155
                     if Nkind (Indic1) /= N_Subtype_Indication then
4156
                        return
4157
                          Nkind (Indic2) /= N_Subtype_Indication
4158
                            and then Entity (Indic1) = Entity (Indic2);
4159
 
4160
                     elsif Nkind (Indic2) /= N_Subtype_Indication then
4161
                        return
4162
                          Nkind (Indic1) /= N_Subtype_Indication
4163
                            and then Entity (Indic1) = Entity (Indic2);
4164
 
4165
                     else
4166
                        if Entity (Subtype_Mark (Indic1)) /=
4167
                          Entity (Subtype_Mark (Indic2))
4168
                        then
4169
                           return False;
4170
                        end if;
4171
 
4172
                        Elt1 := First (Constraints (Constraint (Indic1)));
4173
                        Elt2 := First (Constraints (Constraint (Indic2)));
4174
 
4175
                        while Present (Elt1) and then Present (Elt2) loop
4176
                           if not FCE (Elt1, Elt2) then
4177
                              return False;
4178
                           end if;
4179
 
4180
                           Next (Elt1);
4181
                           Next (Elt2);
4182
                        end loop;
4183
 
4184
                        return True;
4185
                     end if;
4186
                  end;
4187
               end if;
4188
 
4189
            when N_Attribute_Reference =>
4190
               return
4191
                 Attribute_Name (E1) = Attribute_Name (E2)
4192
                   and then FCL (Expressions (E1), Expressions (E2));
4193
 
4194
            when N_Binary_Op =>
4195
               return
4196
                 Entity (E1) = Entity (E2)
4197
                   and then FCE (Left_Opnd  (E1), Left_Opnd  (E2))
4198
                   and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4199
 
4200
            when N_And_Then | N_Or_Else | N_In | N_Not_In =>
4201
               return
4202
                 FCE (Left_Opnd  (E1), Left_Opnd  (E2))
4203
                   and then
4204
                 FCE (Right_Opnd (E1), Right_Opnd (E2));
4205
 
4206
            when N_Character_Literal =>
4207
               return
4208
                 Char_Literal_Value (E1) = Char_Literal_Value (E2);
4209
 
4210
            when N_Component_Association =>
4211
               return
4212
                 FCL (Choices (E1), Choices (E2))
4213
                   and then FCE (Expression (E1), Expression (E2));
4214
 
4215
            when N_Conditional_Expression =>
4216
               return
4217
                 FCL (Expressions (E1), Expressions (E2));
4218
 
4219
            when N_Explicit_Dereference =>
4220
               return
4221
                 FCE (Prefix (E1), Prefix (E2));
4222
 
4223
            when N_Extension_Aggregate =>
4224
               return
4225
                 FCL (Expressions (E1), Expressions (E2))
4226
                   and then Null_Record_Present (E1) =
4227
                            Null_Record_Present (E2)
4228
                   and then FCL (Component_Associations (E1),
4229
                               Component_Associations (E2));
4230
 
4231
            when N_Function_Call =>
4232
               return
4233
                 FCE (Name (E1), Name (E2))
4234
                   and then FCL (Parameter_Associations (E1),
4235
                                 Parameter_Associations (E2));
4236
 
4237
            when N_Indexed_Component =>
4238
               return
4239
                 FCE (Prefix (E1), Prefix (E2))
4240
                   and then FCL (Expressions (E1), Expressions (E2));
4241
 
4242
            when N_Integer_Literal =>
4243
               return (Intval (E1) = Intval (E2));
4244
 
4245
            when N_Null =>
4246
               return True;
4247
 
4248
            when N_Operator_Symbol =>
4249
               return
4250
                 Chars (E1) = Chars (E2);
4251
 
4252
            when N_Others_Choice =>
4253
               return True;
4254
 
4255
            when N_Parameter_Association =>
4256
               return
4257
                 Chars (Selector_Name (E1))  = Chars (Selector_Name (E2))
4258
                   and then FCE (Explicit_Actual_Parameter (E1),
4259
                                 Explicit_Actual_Parameter (E2));
4260
 
4261
            when N_Qualified_Expression =>
4262
               return
4263
                 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4264
                   and then FCE (Expression (E1), Expression (E2));
4265
 
4266
            when N_Range =>
4267
               return
4268
                 FCE (Low_Bound (E1), Low_Bound (E2))
4269
                   and then FCE (High_Bound (E1), High_Bound (E2));
4270
 
4271
            when N_Real_Literal =>
4272
               return (Realval (E1) = Realval (E2));
4273
 
4274
            when N_Selected_Component =>
4275
               return
4276
                 FCE (Prefix (E1), Prefix (E2))
4277
                   and then FCE (Selector_Name (E1), Selector_Name (E2));
4278
 
4279
            when N_Slice =>
4280
               return
4281
                 FCE (Prefix (E1), Prefix (E2))
4282
                   and then FCE (Discrete_Range (E1), Discrete_Range (E2));
4283
 
4284
            when N_String_Literal =>
4285
               declare
4286
                  S1 : constant String_Id := Strval (E1);
4287
                  S2 : constant String_Id := Strval (E2);
4288
                  L1 : constant Nat       := String_Length (S1);
4289
                  L2 : constant Nat       := String_Length (S2);
4290
 
4291
               begin
4292
                  if L1 /= L2 then
4293
                     return False;
4294
 
4295
                  else
4296
                     for J in 1 .. L1 loop
4297
                        if Get_String_Char (S1, J) /=
4298
                           Get_String_Char (S2, J)
4299
                        then
4300
                           return False;
4301
                        end if;
4302
                     end loop;
4303
 
4304
                     return True;
4305
                  end if;
4306
               end;
4307
 
4308
            when N_Type_Conversion =>
4309
               return
4310
                 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4311
                   and then FCE (Expression (E1), Expression (E2));
4312
 
4313
            when N_Unary_Op =>
4314
               return
4315
                 Entity (E1) = Entity (E2)
4316
                   and then FCE (Right_Opnd (E1), Right_Opnd (E2));
4317
 
4318
            when N_Unchecked_Type_Conversion =>
4319
               return
4320
                 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
4321
                   and then FCE (Expression (E1), Expression (E2));
4322
 
4323
            --  All other node types cannot appear in this context. Strictly
4324
            --  we should raise a fatal internal error. Instead we just ignore
4325
            --  the nodes. This means that if anyone makes a mistake in the
4326
            --  expander and mucks an expression tree irretrievably, the
4327
            --  result will be a failure to detect a (probably very obscure)
4328
            --  case of non-conformance, which is better than bombing on some
4329
            --  case where two expressions do in fact conform.
4330
 
4331
            when others =>
4332
               return True;
4333
 
4334
         end case;
4335
      end if;
4336
   end Fully_Conformant_Expressions;
4337
 
4338
   ----------------------------------------
4339
   -- Fully_Conformant_Discrete_Subtypes --
4340
   ----------------------------------------
4341
 
4342
   function Fully_Conformant_Discrete_Subtypes
4343
     (Given_S1 : Node_Id;
4344
      Given_S2 : Node_Id) return Boolean
4345
   is
4346
      S1 : constant Node_Id := Original_Node (Given_S1);
4347
      S2 : constant Node_Id := Original_Node (Given_S2);
4348
 
4349
      function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
4350
      --  Special-case for a bound given by a discriminant, which in the body
4351
      --  is replaced with the discriminal of the enclosing type.
4352
 
4353
      function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
4354
      --  Check both bounds
4355
 
4356
      function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
4357
      begin
4358
         if Is_Entity_Name (B1)
4359
           and then Is_Entity_Name (B2)
4360
           and then Ekind (Entity (B1)) = E_Discriminant
4361
         then
4362
            return Chars (B1) = Chars (B2);
4363
 
4364
         else
4365
            return Fully_Conformant_Expressions (B1, B2);
4366
         end if;
4367
      end Conforming_Bounds;
4368
 
4369
      function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
4370
      begin
4371
         return
4372
           Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
4373
             and then
4374
           Conforming_Bounds (High_Bound (R1), High_Bound (R2));
4375
      end Conforming_Ranges;
4376
 
4377
   --  Start of processing for Fully_Conformant_Discrete_Subtypes
4378
 
4379
   begin
4380
      if Nkind (S1) /= Nkind (S2) then
4381
         return False;
4382
 
4383
      elsif Is_Entity_Name (S1) then
4384
         return Entity (S1) = Entity (S2);
4385
 
4386
      elsif Nkind (S1) = N_Range then
4387
         return Conforming_Ranges (S1, S2);
4388
 
4389
      elsif Nkind (S1) = N_Subtype_Indication then
4390
         return
4391
            Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
4392
              and then
4393
            Conforming_Ranges
4394
              (Range_Expression (Constraint (S1)),
4395
               Range_Expression (Constraint (S2)));
4396
      else
4397
         return True;
4398
      end if;
4399
   end Fully_Conformant_Discrete_Subtypes;
4400
 
4401
   --------------------
4402
   -- Install_Entity --
4403
   --------------------
4404
 
4405
   procedure Install_Entity (E : Entity_Id) is
4406
      Prev : constant Entity_Id := Current_Entity (E);
4407
 
4408
   begin
4409
      Set_Is_Immediately_Visible (E);
4410
      Set_Current_Entity (E);
4411
      Set_Homonym (E, Prev);
4412
   end Install_Entity;
4413
 
4414
   ---------------------
4415
   -- Install_Formals --
4416
   ---------------------
4417
 
4418
   procedure Install_Formals (Id : Entity_Id) is
4419
      F : Entity_Id;
4420
 
4421
   begin
4422
      F := First_Formal (Id);
4423
 
4424
      while Present (F) loop
4425
         Install_Entity (F);
4426
         Next_Formal (F);
4427
      end loop;
4428
   end Install_Formals;
4429
 
4430
   ---------------------------------
4431
   -- Is_Non_Overriding_Operation --
4432
   ---------------------------------
4433
 
4434
   function Is_Non_Overriding_Operation
4435
     (Prev_E : Entity_Id;
4436
      New_E  : Entity_Id) return Boolean
4437
   is
4438
      Formal : Entity_Id;
4439
      F_Typ  : Entity_Id;
4440
      G_Typ  : Entity_Id := Empty;
4441
 
4442
      function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4443
      --  If F_Type is a derived type associated with a generic actual
4444
      --  subtype, then return its Generic_Parent_Type attribute, else return
4445
      --  Empty.
4446
 
4447
      function Types_Correspond
4448
        (P_Type : Entity_Id;
4449
         N_Type : Entity_Id) return Boolean;
4450
      --  Returns true if and only if the types (or designated types in the
4451
      --  case of anonymous access types) are the same or N_Type is derived
4452
      --  directly or indirectly from P_Type.
4453
 
4454
      -----------------------------
4455
      -- Get_Generic_Parent_Type --
4456
      -----------------------------
4457
 
4458
      function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4459
         G_Typ : Entity_Id;
4460
         Indic : Node_Id;
4461
 
4462
      begin
4463
         if Is_Derived_Type (F_Typ)
4464
           and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4465
         then
4466
            --  The tree must be traversed to determine the parent subtype in
4467
            --  the generic unit, which unfortunately isn't always available
4468
            --  via semantic attributes. ??? (Note: The use of Original_Node
4469
            --  is needed for cases where a full derived type has been
4470
            --  rewritten.)
4471
 
4472
            Indic := Subtype_Indication
4473
                       (Type_Definition (Original_Node (Parent (F_Typ))));
4474
 
4475
            if Nkind (Indic) = N_Subtype_Indication then
4476
               G_Typ := Entity (Subtype_Mark (Indic));
4477
            else
4478
               G_Typ := Entity (Indic);
4479
            end if;
4480
 
4481
            if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4482
              and then Present (Generic_Parent_Type (Parent (G_Typ)))
4483
            then
4484
               return Generic_Parent_Type (Parent (G_Typ));
4485
            end if;
4486
         end if;
4487
 
4488
         return Empty;
4489
      end Get_Generic_Parent_Type;
4490
 
4491
      ----------------------
4492
      -- Types_Correspond --
4493
      ----------------------
4494
 
4495
      function Types_Correspond
4496
        (P_Type : Entity_Id;
4497
         N_Type : Entity_Id) return Boolean
4498
      is
4499
         Prev_Type : Entity_Id := Base_Type (P_Type);
4500
         New_Type  : Entity_Id := Base_Type (N_Type);
4501
 
4502
      begin
4503
         if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4504
            Prev_Type := Designated_Type (Prev_Type);
4505
         end if;
4506
 
4507
         if Ekind (New_Type) = E_Anonymous_Access_Type then
4508
            New_Type := Designated_Type (New_Type);
4509
         end if;
4510
 
4511
         if Prev_Type = New_Type then
4512
            return True;
4513
 
4514
         elsif not Is_Class_Wide_Type (New_Type) then
4515
            while Etype (New_Type) /= New_Type loop
4516
               New_Type := Etype (New_Type);
4517
               if New_Type = Prev_Type then
4518
                  return True;
4519
               end if;
4520
            end loop;
4521
         end if;
4522
         return False;
4523
      end Types_Correspond;
4524
 
4525
   --  Start of processing for Is_Non_Overriding_Operation
4526
 
4527
   begin
4528
      --  In the case where both operations are implicit derived subprograms
4529
      --  then neither overrides the other. This can only occur in certain
4530
      --  obscure cases (e.g., derivation from homographs created in a generic
4531
      --  instantiation).
4532
 
4533
      if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4534
         return True;
4535
 
4536
      elsif Ekind (Current_Scope) = E_Package
4537
        and then Is_Generic_Instance (Current_Scope)
4538
        and then In_Private_Part (Current_Scope)
4539
        and then Comes_From_Source (New_E)
4540
      then
4541
         --  We examine the formals and result subtype of the inherited
4542
         --  operation, to determine whether their type is derived from (the
4543
         --  instance of) a generic type.
4544
 
4545
         Formal := First_Formal (Prev_E);
4546
 
4547
         while Present (Formal) loop
4548
            F_Typ := Base_Type (Etype (Formal));
4549
 
4550
            if Ekind (F_Typ) = E_Anonymous_Access_Type then
4551
               F_Typ := Designated_Type (F_Typ);
4552
            end if;
4553
 
4554
            G_Typ := Get_Generic_Parent_Type (F_Typ);
4555
 
4556
            Next_Formal (Formal);
4557
         end loop;
4558
 
4559
         if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4560
            G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4561
         end if;
4562
 
4563
         if No (G_Typ) then
4564
            return False;
4565
         end if;
4566
 
4567
         --  If the generic type is a private type, then the original
4568
         --  operation was not overriding in the generic, because there was
4569
         --  no primitive operation to override.
4570
 
4571
         if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4572
           and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4573
             N_Formal_Private_Type_Definition
4574
         then
4575
            return True;
4576
 
4577
         --  The generic parent type is the ancestor of a formal derived
4578
         --  type declaration. We need to check whether it has a primitive
4579
         --  operation that should be overridden by New_E in the generic.
4580
 
4581
         else
4582
            declare
4583
               P_Formal : Entity_Id;
4584
               N_Formal : Entity_Id;
4585
               P_Typ    : Entity_Id;
4586
               N_Typ    : Entity_Id;
4587
               P_Prim   : Entity_Id;
4588
               Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4589
 
4590
            begin
4591
               while Present (Prim_Elt) loop
4592
                  P_Prim := Node (Prim_Elt);
4593
 
4594
                  if Chars (P_Prim) = Chars (New_E)
4595
                    and then Ekind (P_Prim) = Ekind (New_E)
4596
                  then
4597
                     P_Formal := First_Formal (P_Prim);
4598
                     N_Formal := First_Formal (New_E);
4599
                     while Present (P_Formal) and then Present (N_Formal) loop
4600
                        P_Typ := Etype (P_Formal);
4601
                        N_Typ := Etype (N_Formal);
4602
 
4603
                        if not Types_Correspond (P_Typ, N_Typ) then
4604
                           exit;
4605
                        end if;
4606
 
4607
                        Next_Entity (P_Formal);
4608
                        Next_Entity (N_Formal);
4609
                     end loop;
4610
 
4611
                     --  Found a matching primitive operation belonging to the
4612
                     --  formal ancestor type, so the new subprogram is
4613
                     --  overriding.
4614
 
4615
                     if not Present (P_Formal)
4616
                       and then not Present (N_Formal)
4617
                       and then (Ekind (New_E) /= E_Function
4618
                                  or else
4619
                                 Types_Correspond
4620
                                   (Etype (P_Prim), Etype (New_E)))
4621
                     then
4622
                        return False;
4623
                     end if;
4624
                  end if;
4625
 
4626
                  Next_Elmt (Prim_Elt);
4627
               end loop;
4628
 
4629
               --  If no match found, then the new subprogram does not
4630
               --  override in the generic (nor in the instance).
4631
 
4632
               return True;
4633
            end;
4634
         end if;
4635
      else
4636
         return False;
4637
      end if;
4638
   end Is_Non_Overriding_Operation;
4639
 
4640
   ------------------------------
4641
   -- Make_Inequality_Operator --
4642
   ------------------------------
4643
 
4644
   --  S is the defining identifier of an equality operator. We build a
4645
   --  subprogram declaration with the right signature. This operation is
4646
   --  intrinsic, because it is always expanded as the negation of the
4647
   --  call to the equality function.
4648
 
4649
   procedure Make_Inequality_Operator (S : Entity_Id) is
4650
      Loc     : constant Source_Ptr := Sloc (S);
4651
      Decl    : Node_Id;
4652
      Formals : List_Id;
4653
      Op_Name : Entity_Id;
4654
 
4655
      A : Entity_Id;
4656
      B : Entity_Id;
4657
 
4658
   begin
4659
      --  Check that equality was properly defined
4660
 
4661
      if  No (Next_Formal (First_Formal (S))) then
4662
         return;
4663
      end if;
4664
 
4665
      A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4666
      B := Make_Defining_Identifier (Loc,
4667
             Chars (Next_Formal (First_Formal (S))));
4668
 
4669
      Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4670
 
4671
      Formals := New_List (
4672
        Make_Parameter_Specification (Loc,
4673
          Defining_Identifier => A,
4674
          Parameter_Type =>
4675
            New_Reference_To (Etype (First_Formal (S)), Loc)),
4676
 
4677
        Make_Parameter_Specification (Loc,
4678
          Defining_Identifier => B,
4679
          Parameter_Type =>
4680
            New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4681
 
4682
      Decl :=
4683
        Make_Subprogram_Declaration (Loc,
4684
          Specification =>
4685
            Make_Function_Specification (Loc,
4686
              Defining_Unit_Name => Op_Name,
4687
              Parameter_Specifications => Formals,
4688
              Result_Definition => New_Reference_To (Standard_Boolean, Loc)));
4689
 
4690
      --  Insert inequality right after equality if it is explicit or after
4691
      --  the derived type when implicit. These entities are created only for
4692
      --  visibility purposes, and eventually replaced in the course of
4693
      --  expansion, so they do not need to be attached to the tree and seen
4694
      --  by the back-end. Keeping them internal also avoids spurious freezing
4695
      --  problems. The declaration is inserted in the tree for analysis, and
4696
      --  removed afterwards. If the equality operator comes from an explicit
4697
      --  declaration, attach the inequality immediately after. Else the
4698
      --  equality is inherited from a derived type declaration, so insert
4699
      --  inequality after that declaration.
4700
 
4701
      if No (Alias (S)) then
4702
         Insert_After (Unit_Declaration_Node (S), Decl);
4703
      elsif Is_List_Member (Parent (S)) then
4704
         Insert_After (Parent (S), Decl);
4705
      else
4706
         Insert_After (Parent (Etype (First_Formal (S))), Decl);
4707
      end if;
4708
 
4709
      Mark_Rewrite_Insertion (Decl);
4710
      Set_Is_Intrinsic_Subprogram (Op_Name);
4711
      Analyze (Decl);
4712
      Remove (Decl);
4713
      Set_Has_Completion (Op_Name);
4714
      Set_Corresponding_Equality (Op_Name, S);
4715
      Set_Is_Abstract (Op_Name, Is_Abstract (S));
4716
   end Make_Inequality_Operator;
4717
 
4718
   ----------------------
4719
   -- May_Need_Actuals --
4720
   ----------------------
4721
 
4722
   procedure May_Need_Actuals (Fun : Entity_Id) is
4723
      F : Entity_Id;
4724
      B : Boolean;
4725
 
4726
   begin
4727
      F := First_Formal (Fun);
4728
      B := True;
4729
 
4730
      while Present (F) loop
4731
         if No (Default_Value (F)) then
4732
            B := False;
4733
            exit;
4734
         end if;
4735
 
4736
         Next_Formal (F);
4737
      end loop;
4738
 
4739
      Set_Needs_No_Actuals (Fun, B);
4740
   end May_Need_Actuals;
4741
 
4742
   ---------------------
4743
   -- Mode_Conformant --
4744
   ---------------------
4745
 
4746
   function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4747
      Result : Boolean;
4748
   begin
4749
      Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4750
      return Result;
4751
   end Mode_Conformant;
4752
 
4753
   ---------------------------
4754
   -- New_Overloaded_Entity --
4755
   ---------------------------
4756
 
4757
   procedure New_Overloaded_Entity
4758
     (S            : Entity_Id;
4759
      Derived_Type : Entity_Id := Empty)
4760
   is
4761
      Does_Override : Boolean := False;
4762
      --  Set if the current scope has an operation that is type-conformant
4763
      --  with S, and becomes hidden by S.
4764
 
4765
      E : Entity_Id;
4766
      --  Entity that S overrides
4767
 
4768
      Prev_Vis : Entity_Id := Empty;
4769
      --  Needs comment ???
4770
 
4771
      Is_Alias_Interface : Boolean := False;
4772
 
4773
      function Is_Private_Declaration (E : Entity_Id) return Boolean;
4774
      --  Check that E is declared in the private part of the current package,
4775
      --  or in the package body, where it may hide a previous declaration.
4776
      --  We can't use In_Private_Part by itself because this flag is also
4777
      --  set when freezing entities, so we must examine the place of the
4778
      --  declaration in the tree, and recognize wrapper packages as well.
4779
 
4780
      procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False);
4781
      --  If the subprogram being analyzed is a primitive operation of
4782
      --  the type of one of its formals, set the corresponding flag.
4783
 
4784
      ----------------------------
4785
      -- Is_Private_Declaration --
4786
      ----------------------------
4787
 
4788
      function Is_Private_Declaration (E : Entity_Id) return Boolean is
4789
         Priv_Decls : List_Id;
4790
         Decl       : constant Node_Id := Unit_Declaration_Node (E);
4791
 
4792
      begin
4793
         if Is_Package_Or_Generic_Package (Current_Scope)
4794
           and then In_Private_Part (Current_Scope)
4795
         then
4796
            Priv_Decls :=
4797
              Private_Declarations (
4798
                Specification (Unit_Declaration_Node (Current_Scope)));
4799
 
4800
            return In_Package_Body (Current_Scope)
4801
              or else
4802
                (Is_List_Member (Decl)
4803
                   and then List_Containing (Decl) = Priv_Decls)
4804
              or else (Nkind (Parent (Decl)) = N_Package_Specification
4805
                         and then not Is_Compilation_Unit (
4806
                           Defining_Entity (Parent (Decl)))
4807
                         and then List_Containing (Parent (Parent (Decl)))
4808
                           = Priv_Decls);
4809
         else
4810
            return False;
4811
         end if;
4812
      end Is_Private_Declaration;
4813
 
4814
      -------------------------------
4815
      -- Maybe_Primitive_Operation --
4816
      -------------------------------
4817
 
4818
      procedure Maybe_Primitive_Operation (Is_Overriding : Boolean := False) is
4819
         Formal : Entity_Id;
4820
         F_Typ  : Entity_Id;
4821
         B_Typ  : Entity_Id;
4822
 
4823
         function Visible_Part_Type (T : Entity_Id) return Boolean;
4824
         --  Returns true if T is declared in the visible part of
4825
         --  the current package scope; otherwise returns false.
4826
         --  Assumes that T is declared in a package.
4827
 
4828
         procedure Check_Private_Overriding (T : Entity_Id);
4829
         --  Checks that if a primitive abstract subprogram of a visible
4830
         --  abstract type is declared in a private part, then it must
4831
         --  override an abstract subprogram declared in the visible part.
4832
         --  Also checks that if a primitive function with a controlling
4833
         --  result is declared in a private part, then it must override
4834
         --  a function declared in the visible part.
4835
 
4836
         ------------------------------
4837
         -- Check_Private_Overriding --
4838
         ------------------------------
4839
 
4840
         procedure Check_Private_Overriding (T : Entity_Id) is
4841
         begin
4842
            if Ekind (Current_Scope) = E_Package
4843
              and then In_Private_Part (Current_Scope)
4844
              and then Visible_Part_Type (T)
4845
              and then not In_Instance
4846
            then
4847
               if Is_Abstract (T)
4848
                 and then Is_Abstract (S)
4849
                 and then (not Is_Overriding or else not Is_Abstract (E))
4850
               then
4851
                  if not Is_Interface (T) then
4852
                     Error_Msg_N ("abstract subprograms must be visible "
4853
                                   & "('R'M 3.9.3(10))!", S);
4854
 
4855
                  --  Ada 2005 (AI-251)
4856
 
4857
                  else
4858
                     Error_Msg_N ("primitive subprograms of interface types "
4859
                       & "declared in a visible part, must be declared in "
4860
                       & "the visible part ('R'M 3.9.4)!", S);
4861
                  end if;
4862
 
4863
               elsif Ekind (S) = E_Function
4864
                 and then Is_Tagged_Type (T)
4865
                 and then T = Base_Type (Etype (S))
4866
                 and then not Is_Overriding
4867
               then
4868
                  Error_Msg_N
4869
                    ("private function with tagged result must"
4870
                     & " override visible-part function", S);
4871
                  Error_Msg_N
4872
                    ("\move subprogram to the visible part"
4873
                     & " ('R'M 3.9.3(10))", S);
4874
               end if;
4875
            end if;
4876
         end Check_Private_Overriding;
4877
 
4878
         -----------------------
4879
         -- Visible_Part_Type --
4880
         -----------------------
4881
 
4882
         function Visible_Part_Type (T : Entity_Id) return Boolean is
4883
            P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4884
            N : Node_Id;
4885
 
4886
         begin
4887
            --  If the entity is a private type, then it must be
4888
            --  declared in a visible part.
4889
 
4890
            if Ekind (T) in Private_Kind then
4891
               return True;
4892
            end if;
4893
 
4894
            --  Otherwise, we traverse the visible part looking for its
4895
            --  corresponding declaration. We cannot use the declaration
4896
            --  node directly because in the private part the entity of a
4897
            --  private type is the one in the full view, which does not
4898
            --  indicate that it is the completion of something visible.
4899
 
4900
            N := First (Visible_Declarations (Specification (P)));
4901
            while Present (N) loop
4902
               if Nkind (N) = N_Full_Type_Declaration
4903
                 and then Present (Defining_Identifier (N))
4904
                 and then T = Defining_Identifier (N)
4905
               then
4906
                  return True;
4907
 
4908
               elsif (Nkind (N) = N_Private_Type_Declaration
4909
                       or else
4910
                      Nkind (N) = N_Private_Extension_Declaration)
4911
                 and then Present (Defining_Identifier (N))
4912
                 and then T = Full_View (Defining_Identifier (N))
4913
               then
4914
                  return True;
4915
               end if;
4916
 
4917
               Next (N);
4918
            end loop;
4919
 
4920
            return False;
4921
         end Visible_Part_Type;
4922
 
4923
      --  Start of processing for Maybe_Primitive_Operation
4924
 
4925
      begin
4926
         if not Comes_From_Source (S) then
4927
            null;
4928
 
4929
         --  If the subprogram is at library level, it is not primitive
4930
         --  operation.
4931
 
4932
         elsif Current_Scope = Standard_Standard then
4933
            null;
4934
 
4935
         elsif (Ekind (Current_Scope) = E_Package
4936
                 and then not In_Package_Body (Current_Scope))
4937
           or else Is_Overriding
4938
         then
4939
            --  For function, check return type
4940
 
4941
            if Ekind (S) = E_Function then
4942
               B_Typ := Base_Type (Etype (S));
4943
 
4944
               if Scope (B_Typ) = Current_Scope then
4945
                  Set_Has_Primitive_Operations (B_Typ);
4946
                  Check_Private_Overriding (B_Typ);
4947
               end if;
4948
            end if;
4949
 
4950
            --  For all subprograms, check formals
4951
 
4952
            Formal := First_Formal (S);
4953
            while Present (Formal) loop
4954
               if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4955
                  F_Typ := Designated_Type (Etype (Formal));
4956
               else
4957
                  F_Typ := Etype (Formal);
4958
               end if;
4959
 
4960
               B_Typ := Base_Type (F_Typ);
4961
 
4962
               if Scope (B_Typ) = Current_Scope then
4963
                  Set_Has_Primitive_Operations (B_Typ);
4964
                  Check_Private_Overriding (B_Typ);
4965
               end if;
4966
 
4967
               Next_Formal (Formal);
4968
            end loop;
4969
         end if;
4970
      end Maybe_Primitive_Operation;
4971
 
4972
   --  Start of processing for New_Overloaded_Entity
4973
 
4974
   begin
4975
      --  We need to look for an entity that S may override. This must be a
4976
      --  homonym in the current scope, so we look for the first homonym of
4977
      --  S in the current scope as the starting point for the search.
4978
 
4979
      E := Current_Entity_In_Scope (S);
4980
 
4981
      --  If there is no homonym then this is definitely not overriding
4982
 
4983
      if No (E) then
4984
         Enter_Overloaded_Entity (S);
4985
         Check_Dispatching_Operation (S, Empty);
4986
         Maybe_Primitive_Operation;
4987
 
4988
         --  Ada 2005 (AI-397): Subprograms in the context of protected
4989
         --  types have their overriding indicators checked in Sem_Ch9.
4990
 
4991
         if Ekind (S) not in Subprogram_Kind
4992
           or else Ekind (Scope (S)) /= E_Protected_Type
4993
         then
4994
            Check_Overriding_Indicator (S, False);
4995
         end if;
4996
 
4997
      --  If there is a homonym that is not overloadable, then we have an
4998
      --  error, except for the special cases checked explicitly below.
4999
 
5000
      elsif not Is_Overloadable (E) then
5001
 
5002
         --  Check for spurious conflict produced by a subprogram that has the
5003
         --  same name as that of the enclosing generic package. The conflict
5004
         --  occurs within an instance, between the subprogram and the renaming
5005
         --  declaration for the package. After the subprogram, the package
5006
         --  renaming declaration becomes hidden.
5007
 
5008
         if Ekind (E) = E_Package
5009
           and then Present (Renamed_Object (E))
5010
           and then Renamed_Object (E) = Current_Scope
5011
           and then Nkind (Parent (Renamed_Object (E))) =
5012
                                                     N_Package_Specification
5013
           and then Present (Generic_Parent (Parent (Renamed_Object (E))))
5014
         then
5015
            Set_Is_Hidden (E);
5016
            Set_Is_Immediately_Visible (E, False);
5017
            Enter_Overloaded_Entity (S);
5018
            Set_Homonym (S, Homonym (E));
5019
            Check_Dispatching_Operation (S, Empty);
5020
            Check_Overriding_Indicator (S, False);
5021
 
5022
         --  If the subprogram is implicit it is hidden by the previous
5023
         --  declaration. However if it is dispatching, it must appear in the
5024
         --  dispatch table anyway, because it can be dispatched to even if it
5025
         --  cannot be called directly.
5026
 
5027
         elsif Present (Alias (S))
5028
           and then not Comes_From_Source (S)
5029
         then
5030
            Set_Scope (S, Current_Scope);
5031
 
5032
            if Is_Dispatching_Operation (Alias (S)) then
5033
               Check_Dispatching_Operation (S, Empty);
5034
            end if;
5035
 
5036
            return;
5037
 
5038
         else
5039
            Error_Msg_Sloc := Sloc (E);
5040
            Error_Msg_N ("& conflicts with declaration#", S);
5041
 
5042
            --  Useful additional warning
5043
 
5044
            if Is_Generic_Unit (E) then
5045
               Error_Msg_N ("\previous generic unit cannot be overloaded", S);
5046
            end if;
5047
 
5048
            return;
5049
         end if;
5050
 
5051
      --  E exists and is overloadable
5052
 
5053
      else
5054
         Is_Alias_Interface :=
5055
            Present (Alias (S))
5056
            and then Is_Dispatching_Operation (Alias (S))
5057
            and then Present (DTC_Entity (Alias (S)))
5058
            and then Is_Interface (Scope (DTC_Entity (Alias (S))));
5059
 
5060
         --  Loop through E and its homonyms to determine if any of them is
5061
         --  the candidate for overriding by S.
5062
 
5063
         while Present (E) loop
5064
 
5065
            --  Definitely not interesting if not in the current scope
5066
 
5067
            if Scope (E) /= Current_Scope then
5068
               null;
5069
 
5070
            --  Check if we have type conformance
5071
 
5072
            --  Ada 2005 (AI-251): In case of overriding an interface
5073
            --  subprogram it is not an error that the old and new entities
5074
            --  have the same profile, and hence we skip this code.
5075
 
5076
            elsif not Is_Alias_Interface
5077
              and then Type_Conformant (E, S)
5078
            then
5079
               --  If the old and new entities have the same profile and one
5080
               --  is not the body of the other, then this is an error, unless
5081
               --  one of them is implicitly declared.
5082
 
5083
               --  There are some cases when both can be implicit, for example
5084
               --  when both a literal and a function that overrides it are
5085
               --  inherited in a derivation, or when an inhertited operation
5086
               --  of a tagged full type overrides the ineherited operation of
5087
               --  a private extension. Ada 83 had a special rule for the the
5088
               --  literal case. In Ada95, the later implicit operation hides
5089
               --  the former, and the literal is always the former. In the
5090
               --  odd case where both are derived operations declared at the
5091
               --  same point, both operations should be declared, and in that
5092
               --  case we bypass the following test and proceed to the next
5093
               --  part (this can only occur for certain obscure cases
5094
               --  involving homographs in instances and can't occur for
5095
               --  dispatching operations ???). Note that the following
5096
               --  condition is less than clear. For example, it's not at all
5097
               --  clear why there's a test for E_Entry here. ???
5098
 
5099
               if Present (Alias (S))
5100
                 and then (No (Alias (E))
5101
                            or else Comes_From_Source (E)
5102
                            or else Is_Dispatching_Operation (E))
5103
                 and then
5104
                   (Ekind (E) = E_Entry
5105
                     or else Ekind (E) /= E_Enumeration_Literal)
5106
               then
5107
                  --  When an derived operation is overloaded it may be due to
5108
                  --  the fact that the full view of a private extension
5109
                  --  re-inherits. It has to be dealt with.
5110
 
5111
                  if Is_Package_Or_Generic_Package (Current_Scope)
5112
                    and then In_Private_Part (Current_Scope)
5113
                  then
5114
                     Check_Operation_From_Private_View (S, E);
5115
                  end if;
5116
 
5117
                  --  In any case the implicit operation remains hidden by
5118
                  --  the existing declaration, which is overriding.
5119
 
5120
                  Set_Is_Overriding_Operation (E);
5121
 
5122
                  if Comes_From_Source (E) then
5123
                     Check_Overriding_Indicator (E, True);
5124
 
5125
                     --  Indicate that E overrides the operation from which
5126
                     --  S is inherited.
5127
 
5128
                     if  Present (Alias (S)) then
5129
                        Set_Overridden_Operation (E, Alias (S));
5130
                     else
5131
                        Set_Overridden_Operation (E, S);
5132
                     end if;
5133
                  end if;
5134
 
5135
                  return;
5136
 
5137
                  --  Within an instance, the renaming declarations for
5138
                  --  actual subprograms may become ambiguous, but they do
5139
                  --  not hide each other.
5140
 
5141
               elsif Ekind (E) /= E_Entry
5142
                 and then not Comes_From_Source (E)
5143
                 and then not Is_Generic_Instance (E)
5144
                 and then (Present (Alias (E))
5145
                            or else Is_Intrinsic_Subprogram (E))
5146
                 and then (not In_Instance
5147
                            or else No (Parent (E))
5148
                            or else Nkind (Unit_Declaration_Node (E)) /=
5149
                               N_Subprogram_Renaming_Declaration)
5150
               then
5151
                  --  A subprogram child unit is not allowed to override
5152
                  --  an inherited subprogram (10.1.1(20)).
5153
 
5154
                  if Is_Child_Unit (S) then
5155
                     Error_Msg_N
5156
                       ("child unit overrides inherited subprogram in parent",
5157
                        S);
5158
                     return;
5159
                  end if;
5160
 
5161
                  if Is_Non_Overriding_Operation (E, S) then
5162
                     Enter_Overloaded_Entity (S);
5163
                     if not Present (Derived_Type)
5164
                       or else Is_Tagged_Type (Derived_Type)
5165
                     then
5166
                        Check_Dispatching_Operation (S, Empty);
5167
                     end if;
5168
 
5169
                     return;
5170
                  end if;
5171
 
5172
                  --  E is a derived operation or an internal operator which
5173
                  --  is being overridden. Remove E from further visibility.
5174
                  --  Furthermore, if E is a dispatching operation, it must be
5175
                  --  replaced in the list of primitive operations of its type
5176
                  --  (see Override_Dispatching_Operation).
5177
 
5178
                  Does_Override := True;
5179
 
5180
                  declare
5181
                     Prev : Entity_Id;
5182
 
5183
                  begin
5184
                     Prev := First_Entity (Current_Scope);
5185
 
5186
                     while Present (Prev)
5187
                       and then Next_Entity (Prev) /= E
5188
                     loop
5189
                        Next_Entity (Prev);
5190
                     end loop;
5191
 
5192
                     --  It is possible for E to be in the current scope and
5193
                     --  yet not in the entity chain. This can only occur in a
5194
                     --  generic context where E is an implicit concatenation
5195
                     --  in the formal part, because in a generic body the
5196
                     --  entity chain starts with the formals.
5197
 
5198
                     pragma Assert
5199
                       (Present (Prev) or else Chars (E) = Name_Op_Concat);
5200
 
5201
                     --  E must be removed both from the entity_list of the
5202
                     --  current scope, and from the visibility chain
5203
 
5204
                     if Debug_Flag_E then
5205
                        Write_Str ("Override implicit operation ");
5206
                        Write_Int (Int (E));
5207
                        Write_Eol;
5208
                     end if;
5209
 
5210
                     --  If E is a predefined concatenation, it stands for four
5211
                     --  different operations. As a result, a single explicit
5212
                     --  declaration does not hide it. In a possible ambiguous
5213
                     --  situation, Disambiguate chooses the user-defined op,
5214
                     --  so it is correct to retain the previous internal one.
5215
 
5216
                     if Chars (E) /= Name_Op_Concat
5217
                       or else Ekind (E) /= E_Operator
5218
                     then
5219
                        --  For nondispatching derived operations that are
5220
                        --  overridden by a subprogram declared in the private
5221
                        --  part of a package, we retain the derived
5222
                        --  subprogram but mark it as not immediately visible.
5223
                        --  If the derived operation was declared in the
5224
                        --  visible part then this ensures that it will still
5225
                        --  be visible outside the package with the proper
5226
                        --  signature (calls from outside must also be
5227
                        --  directed to this version rather than the
5228
                        --  overriding one, unlike the dispatching case).
5229
                        --  Calls from inside the package will still resolve
5230
                        --  to the overriding subprogram since the derived one
5231
                        --  is marked as not visible within the package.
5232
 
5233
                        --  If the private operation is dispatching, we achieve
5234
                        --  the overriding by keeping the implicit operation
5235
                        --  but setting its alias to be the overriding one. In
5236
                        --  this fashion the proper body is executed in all
5237
                        --  cases, but the original signature is used outside
5238
                        --  of the package.
5239
 
5240
                        --  If the overriding is not in the private part, we
5241
                        --  remove the implicit operation altogether.
5242
 
5243
                        if Is_Private_Declaration (S) then
5244
 
5245
                           if not Is_Dispatching_Operation (E) then
5246
                              Set_Is_Immediately_Visible (E, False);
5247
                           else
5248
                              --  Work done in Override_Dispatching_Operation,
5249
                              --  so nothing else need to be done here.
5250
 
5251
                              null;
5252
                           end if;
5253
 
5254
                        else
5255
                           --  Find predecessor of E in Homonym chain
5256
 
5257
                           if E = Current_Entity (E) then
5258
                              Prev_Vis := Empty;
5259
                           else
5260
                              Prev_Vis := Current_Entity (E);
5261
                              while Homonym (Prev_Vis) /= E loop
5262
                                 Prev_Vis := Homonym (Prev_Vis);
5263
                              end loop;
5264
                           end if;
5265
 
5266
                           if Prev_Vis /= Empty then
5267
 
5268
                              --  Skip E in the visibility chain
5269
 
5270
                              Set_Homonym (Prev_Vis, Homonym (E));
5271
 
5272
                           else
5273
                              Set_Name_Entity_Id (Chars (E), Homonym (E));
5274
                           end if;
5275
 
5276
                           Set_Next_Entity (Prev, Next_Entity (E));
5277
 
5278
                           if No (Next_Entity (Prev)) then
5279
                              Set_Last_Entity (Current_Scope, Prev);
5280
                           end if;
5281
 
5282
                        end if;
5283
                     end if;
5284
 
5285
                     Enter_Overloaded_Entity (S);
5286
                     Set_Is_Overriding_Operation (S);
5287
                     Check_Overriding_Indicator (S, True);
5288
 
5289
                     --  Indicate that S overrides the operation from which
5290
                     --  E is inherited.
5291
 
5292
                     if Comes_From_Source (S) then
5293
                        if  Present (Alias (E)) then
5294
                           Set_Overridden_Operation (S, Alias (E));
5295
                        else
5296
                           Set_Overridden_Operation (S, E);
5297
                        end if;
5298
                     end if;
5299
 
5300
                     if Is_Dispatching_Operation (E) then
5301
 
5302
                        --  An overriding dispatching subprogram inherits the
5303
                        --  convention of the overridden subprogram (by
5304
                        --  AI-117).
5305
 
5306
                        Set_Convention (S, Convention (E));
5307
 
5308
                        --  AI-251: For an entity overriding an interface
5309
                        --  primitive check if the entity also covers other
5310
                        --  abstract subprograms in the same scope. This is
5311
                        --  required to handle the general case, that is,
5312
                        --  1) overriding other interface primitives, and
5313
                        --  2) overriding abstract subprograms inherited from
5314
                        --  some abstract ancestor type.
5315
 
5316
                        if Has_Homonym (E)
5317
                          and then Present (Alias (E))
5318
                          and then Ekind (Alias (E)) /= E_Operator
5319
                          and then Present (DTC_Entity (Alias (E)))
5320
                          and then Is_Interface (Scope (DTC_Entity
5321
                                                        (Alias (E))))
5322
                        then
5323
                           declare
5324
                              E1 : Entity_Id;
5325
 
5326
                           begin
5327
                              E1 := Homonym (E);
5328
                              while Present (E1) loop
5329
                                 if (Is_Overloadable (E1)
5330
                                       or else Ekind (E1) = E_Subprogram_Type)
5331
                                   and then Present (Alias (E1))
5332
                                   and then Ekind (Alias (E1)) /= E_Operator
5333
                                   and then Present (DTC_Entity (Alias (E1)))
5334
                                   and then Is_Abstract
5335
                                              (Scope (DTC_Entity (Alias (E1))))
5336
                                   and then Type_Conformant (E1, S)
5337
                                 then
5338
                                    Check_Dispatching_Operation (S, E1);
5339
                                 end if;
5340
 
5341
                                 E1 := Homonym (E1);
5342
                              end loop;
5343
                           end;
5344
                        end if;
5345
 
5346
                        Check_Dispatching_Operation (S, E);
5347
 
5348
                     else
5349
                        Check_Dispatching_Operation (S, Empty);
5350
                     end if;
5351
 
5352
                     Maybe_Primitive_Operation (Is_Overriding => True);
5353
                     goto Check_Inequality;
5354
                  end;
5355
 
5356
               --  Apparent redeclarations in instances can occur when two
5357
               --  formal types get the same actual type. The subprograms in
5358
               --  in the instance are legal,  even if not callable from the
5359
               --  outside. Calls from within are disambiguated elsewhere.
5360
               --  For dispatching operations in the visible part, the usual
5361
               --  rules apply, and operations with the same profile are not
5362
               --  legal (B830001).
5363
 
5364
               elsif (In_Instance_Visible_Part
5365
                       and then not Is_Dispatching_Operation (E))
5366
                 or else In_Instance_Not_Visible
5367
               then
5368
                  null;
5369
 
5370
               --  Here we have a real error (identical profile)
5371
 
5372
               else
5373
                  Error_Msg_Sloc := Sloc (E);
5374
 
5375
                  --  Avoid cascaded errors if the entity appears in
5376
                  --  subsequent calls.
5377
 
5378
                  Set_Scope (S, Current_Scope);
5379
 
5380
                  Error_Msg_N ("& conflicts with declaration#", S);
5381
 
5382
                  if Is_Generic_Instance (S)
5383
                    and then not Has_Completion (E)
5384
                  then
5385
                     Error_Msg_N
5386
                       ("\instantiation cannot provide body for it", S);
5387
                  end if;
5388
 
5389
                  return;
5390
               end if;
5391
 
5392
            else
5393
               null;
5394
            end if;
5395
 
5396
            Prev_Vis := E;
5397
            E := Homonym (E);
5398
         end loop;
5399
 
5400
         --  On exit, we know that S is a new entity
5401
 
5402
         Enter_Overloaded_Entity (S);
5403
         Maybe_Primitive_Operation;
5404
         Check_Overriding_Indicator (S, Does_Override);
5405
 
5406
         --  If S is a derived operation for an untagged type then by
5407
         --  definition it's not a dispatching operation (even if the parent
5408
         --  operation was dispatching), so we don't call
5409
         --  Check_Dispatching_Operation in that case.
5410
 
5411
         if not Present (Derived_Type)
5412
           or else Is_Tagged_Type (Derived_Type)
5413
         then
5414
            Check_Dispatching_Operation (S, Empty);
5415
         end if;
5416
      end if;
5417
 
5418
      --  If this is a user-defined equality operator that is not a derived
5419
      --  subprogram, create the corresponding inequality. If the operation is
5420
      --  dispatching, the expansion is done elsewhere, and we do not create
5421
      --  an explicit inequality operation.
5422
 
5423
      <<Check_Inequality>>
5424
         if Chars (S) = Name_Op_Eq
5425
           and then Etype (S) = Standard_Boolean
5426
           and then Present (Parent (S))
5427
           and then not Is_Dispatching_Operation (S)
5428
         then
5429
            Make_Inequality_Operator (S);
5430
         end if;
5431
   end New_Overloaded_Entity;
5432
 
5433
   ---------------------
5434
   -- Process_Formals --
5435
   ---------------------
5436
 
5437
   procedure Process_Formals
5438
     (T           : List_Id;
5439
      Related_Nod : Node_Id)
5440
   is
5441
      Param_Spec  : Node_Id;
5442
      Formal      : Entity_Id;
5443
      Formal_Type : Entity_Id;
5444
      Default     : Node_Id;
5445
      Ptype       : Entity_Id;
5446
 
5447
      function Is_Class_Wide_Default (D : Node_Id) return Boolean;
5448
      --  Check whether the default has a class-wide type. After analysis the
5449
      --  default has the type of the formal, so we must also check explicitly
5450
      --  for an access attribute.
5451
 
5452
      ---------------------------
5453
      -- Is_Class_Wide_Default --
5454
      ---------------------------
5455
 
5456
      function Is_Class_Wide_Default (D : Node_Id) return Boolean is
5457
      begin
5458
         return Is_Class_Wide_Type (Designated_Type (Etype (D)))
5459
           or else (Nkind (D) =  N_Attribute_Reference
5460
                      and then Attribute_Name (D) = Name_Access
5461
                      and then Is_Class_Wide_Type (Etype (Prefix (D))));
5462
      end Is_Class_Wide_Default;
5463
 
5464
   --  Start of processing for Process_Formals
5465
 
5466
   begin
5467
      --  In order to prevent premature use of the formals in the same formal
5468
      --  part, the Ekind is left undefined until all default expressions are
5469
      --  analyzed. The Ekind is established in a separate loop at the end.
5470
 
5471
      Param_Spec := First (T);
5472
 
5473
      while Present (Param_Spec) loop
5474
 
5475
         Formal := Defining_Identifier (Param_Spec);
5476
         Enter_Name (Formal);
5477
 
5478
         --  Case of ordinary parameters
5479
 
5480
         if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
5481
            Find_Type (Parameter_Type (Param_Spec));
5482
            Ptype := Parameter_Type (Param_Spec);
5483
 
5484
            if Ptype = Error then
5485
               goto Continue;
5486
            end if;
5487
 
5488
            Formal_Type := Entity (Ptype);
5489
 
5490
            if Ekind (Formal_Type) = E_Incomplete_Type
5491
              or else (Is_Class_Wide_Type (Formal_Type)
5492
                        and then Ekind (Root_Type (Formal_Type)) =
5493
                                                         E_Incomplete_Type)
5494
            then
5495
               --  Ada 2005 (AI-326): Tagged incomplete types allowed
5496
 
5497
               if Is_Tagged_Type (Formal_Type) then
5498
                  null;
5499
 
5500
               elsif Nkind (Parent (T)) /= N_Access_Function_Definition
5501
                 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
5502
               then
5503
                  Error_Msg_N ("invalid use of incomplete type", Param_Spec);
5504
               end if;
5505
 
5506
            elsif Ekind (Formal_Type) = E_Void then
5507
               Error_Msg_NE ("premature use of&",
5508
                 Parameter_Type (Param_Spec), Formal_Type);
5509
            end if;
5510
 
5511
            --  Ada 2005 (AI-231): Create and decorate an internal subtype
5512
            --  declaration corresponding to the null-excluding type of the
5513
            --  formal in the enclosing scope. Finally, replace the parameter
5514
            --  type of the formal with the internal subtype.
5515
 
5516
            if Ada_Version >= Ada_05
5517
              and then Is_Access_Type (Formal_Type)
5518
              and then Null_Exclusion_Present (Param_Spec)
5519
            then
5520
               if Can_Never_Be_Null (Formal_Type)
5521
                 and then Comes_From_Source (Related_Nod)
5522
               then
5523
                  Error_Msg_N
5524
                    ("null exclusion must apply to a type that does not "
5525
                       & "exclude null ('R'M 3.10 (14)", Related_Nod);
5526
               end if;
5527
 
5528
               Formal_Type :=
5529
                 Create_Null_Excluding_Itype
5530
                   (T           => Formal_Type,
5531
                    Related_Nod => Related_Nod,
5532
                    Scope_Id    => Scope (Current_Scope));
5533
            end if;
5534
 
5535
         --  An access formal type
5536
 
5537
         else
5538
            Formal_Type :=
5539
              Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5540
 
5541
            --  Ada 2005 (AI-254)
5542
 
5543
            declare
5544
               AD : constant Node_Id :=
5545
                      Access_To_Subprogram_Definition
5546
                        (Parameter_Type (Param_Spec));
5547
            begin
5548
               if Present (AD) and then Protected_Present (AD) then
5549
                  Formal_Type :=
5550
                    Replace_Anonymous_Access_To_Protected_Subprogram
5551
                      (Param_Spec, Formal_Type);
5552
               end if;
5553
            end;
5554
         end if;
5555
 
5556
         Set_Etype (Formal, Formal_Type);
5557
         Default := Expression (Param_Spec);
5558
 
5559
         if Present (Default) then
5560
            if Out_Present (Param_Spec) then
5561
               Error_Msg_N
5562
                 ("default initialization only allowed for IN parameters",
5563
                  Param_Spec);
5564
            end if;
5565
 
5566
            --  Do the special preanalysis of the expression (see section on
5567
            --  "Handling of Default Expressions" in the spec of package Sem).
5568
 
5569
            Analyze_Per_Use_Expression (Default, Formal_Type);
5570
 
5571
            --  Check that the designated type of an access parameter's default
5572
            --  is not a class-wide type unless the parameter's designated type
5573
            --  is also class-wide.
5574
 
5575
            if Ekind (Formal_Type) = E_Anonymous_Access_Type
5576
              and then not From_With_Type (Formal_Type)
5577
              and then Is_Class_Wide_Default (Default)
5578
              and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5579
            then
5580
               Error_Msg_N
5581
                 ("access to class-wide expression not allowed here", Default);
5582
            end if;
5583
         end if;
5584
 
5585
         --  Ada 2005 (AI-231): Static checks
5586
 
5587
         if Ada_Version >= Ada_05
5588
           and then Is_Access_Type (Etype (Formal))
5589
           and then Can_Never_Be_Null (Etype (Formal))
5590
         then
5591
            Null_Exclusion_Static_Checks (Param_Spec);
5592
         end if;
5593
 
5594
      <<Continue>>
5595
         Next (Param_Spec);
5596
      end loop;
5597
 
5598
      --  If this is the formal part of a function specification, analyze the
5599
      --  subtype mark in the context where the formals are visible but not
5600
      --  yet usable, and may hide outer homographs.
5601
 
5602
      if Nkind (Related_Nod) = N_Function_Specification then
5603
         Analyze_Return_Type (Related_Nod);
5604
      end if;
5605
 
5606
      --  Now set the kind (mode) of each formal
5607
 
5608
      Param_Spec := First (T);
5609
 
5610
      while Present (Param_Spec) loop
5611
         Formal := Defining_Identifier (Param_Spec);
5612
         Set_Formal_Mode (Formal);
5613
 
5614
         if Ekind (Formal) = E_In_Parameter then
5615
            Set_Default_Value (Formal, Expression (Param_Spec));
5616
 
5617
            if Present (Expression (Param_Spec)) then
5618
               Default :=  Expression (Param_Spec);
5619
 
5620
               if Is_Scalar_Type (Etype (Default)) then
5621
                  if Nkind
5622
                       (Parameter_Type (Param_Spec)) /= N_Access_Definition
5623
                  then
5624
                     Formal_Type := Entity (Parameter_Type (Param_Spec));
5625
 
5626
                  else
5627
                     Formal_Type := Access_Definition
5628
                       (Related_Nod, Parameter_Type (Param_Spec));
5629
                  end if;
5630
 
5631
                  Apply_Scalar_Range_Check (Default, Formal_Type);
5632
               end if;
5633
            end if;
5634
         end if;
5635
 
5636
         Next (Param_Spec);
5637
      end loop;
5638
 
5639
   end Process_Formals;
5640
 
5641
   ----------------------------
5642
   -- Reference_Body_Formals --
5643
   ----------------------------
5644
 
5645
   procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5646
      Fs : Entity_Id;
5647
      Fb : Entity_Id;
5648
 
5649
   begin
5650
      if Error_Posted (Spec) then
5651
         return;
5652
      end if;
5653
 
5654
      Fs := First_Formal (Spec);
5655
      Fb := First_Formal (Bod);
5656
 
5657
      while Present (Fs) loop
5658
         Generate_Reference (Fs, Fb, 'b');
5659
 
5660
         if Style_Check then
5661
            Style.Check_Identifier (Fb, Fs);
5662
         end if;
5663
 
5664
         Set_Spec_Entity (Fb, Fs);
5665
         Set_Referenced (Fs, False);
5666
         Next_Formal (Fs);
5667
         Next_Formal (Fb);
5668
      end loop;
5669
   end Reference_Body_Formals;
5670
 
5671
   -------------------------
5672
   -- Set_Actual_Subtypes --
5673
   -------------------------
5674
 
5675
   procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5676
      Loc            : constant Source_Ptr := Sloc (N);
5677
      Decl           : Node_Id;
5678
      Formal         : Entity_Id;
5679
      T              : Entity_Id;
5680
      First_Stmt     : Node_Id := Empty;
5681
      AS_Needed      : Boolean;
5682
 
5683
   begin
5684
      --  If this is an emtpy initialization procedure, no need to create
5685
      --  actual subtypes (small optimization).
5686
 
5687
      if Ekind (Subp) = E_Procedure
5688
        and then Is_Null_Init_Proc (Subp)
5689
      then
5690
         return;
5691
      end if;
5692
 
5693
      Formal := First_Formal (Subp);
5694
      while Present (Formal) loop
5695
         T := Etype (Formal);
5696
 
5697
         --  We never need an actual subtype for a constrained formal
5698
 
5699
         if Is_Constrained (T) then
5700
            AS_Needed := False;
5701
 
5702
         --  If we have unknown discriminants, then we do not need an actual
5703
         --  subtype, or more accurately we cannot figure it out! Note that
5704
         --  all class-wide types have unknown discriminants.
5705
 
5706
         elsif Has_Unknown_Discriminants (T) then
5707
            AS_Needed := False;
5708
 
5709
         --  At this stage we have an unconstrained type that may need an
5710
         --  actual subtype. For sure the actual subtype is needed if we have
5711
         --  an unconstrained array type.
5712
 
5713
         elsif Is_Array_Type (T) then
5714
            AS_Needed := True;
5715
 
5716
         --  The only other case needing an actual subtype is an unconstrained
5717
         --  record type which is an IN parameter (we cannot generate actual
5718
         --  subtypes for the OUT or IN OUT case, since an assignment can
5719
         --  change the discriminant values. However we exclude the case of
5720
         --  initialization procedures, since discriminants are handled very
5721
         --  specially in this context, see the section entitled "Handling of
5722
         --  Discriminants" in Einfo.
5723
 
5724
         --  We also exclude the case of Discrim_SO_Functions (functions used
5725
         --  in front end layout mode for size/offset values), since in such
5726
         --  functions only discriminants are referenced, and not only are such
5727
         --  subtypes not needed, but they cannot always be generated, because
5728
         --  of order of elaboration issues.
5729
 
5730
         elsif Is_Record_Type (T)
5731
           and then Ekind (Formal) = E_In_Parameter
5732
           and then Chars (Formal) /= Name_uInit
5733
           and then not Is_Unchecked_Union (T)
5734
           and then not Is_Discrim_SO_Function (Subp)
5735
         then
5736
            AS_Needed := True;
5737
 
5738
         --  All other cases do not need an actual subtype
5739
 
5740
         else
5741
            AS_Needed := False;
5742
         end if;
5743
 
5744
         --  Generate actual subtypes for unconstrained arrays and
5745
         --  unconstrained discriminated records.
5746
 
5747
         if AS_Needed then
5748
            if Nkind (N) = N_Accept_Statement then
5749
 
5750
               --  If expansion is active, The formal is replaced by a local
5751
               --  variable that renames the corresponding entry of the
5752
               --  parameter block, and it is this local variable that may
5753
               --  require an actual subtype.
5754
 
5755
               if Expander_Active then
5756
                  Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5757
               else
5758
                  Decl := Build_Actual_Subtype (T, Formal);
5759
               end if;
5760
 
5761
               if Present (Handled_Statement_Sequence (N)) then
5762
                  First_Stmt :=
5763
                    First (Statements (Handled_Statement_Sequence (N)));
5764
                  Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5765
                  Mark_Rewrite_Insertion (Decl);
5766
               else
5767
                  --  If the accept statement has no body, there will be no
5768
                  --  reference to the actuals, so no need to compute actual
5769
                  --  subtypes.
5770
 
5771
                  return;
5772
               end if;
5773
 
5774
            else
5775
               Decl := Build_Actual_Subtype (T, Formal);
5776
               Prepend (Decl, Declarations (N));
5777
               Mark_Rewrite_Insertion (Decl);
5778
            end if;
5779
 
5780
            --  The declaration uses the bounds of an existing object, and
5781
            --  therefore needs no constraint checks.
5782
 
5783
            Analyze (Decl, Suppress => All_Checks);
5784
 
5785
            --  We need to freeze manually the generated type when it is
5786
            --  inserted anywhere else than in a declarative part.
5787
 
5788
            if Present (First_Stmt) then
5789
               Insert_List_Before_And_Analyze (First_Stmt,
5790
                 Freeze_Entity (Defining_Identifier (Decl), Loc));
5791
            end if;
5792
 
5793
            if Nkind (N) = N_Accept_Statement
5794
              and then Expander_Active
5795
            then
5796
               Set_Actual_Subtype (Renamed_Object (Formal),
5797
                 Defining_Identifier (Decl));
5798
            else
5799
               Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5800
            end if;
5801
         end if;
5802
 
5803
         Next_Formal (Formal);
5804
      end loop;
5805
   end Set_Actual_Subtypes;
5806
 
5807
   ---------------------
5808
   -- Set_Formal_Mode --
5809
   ---------------------
5810
 
5811
   procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5812
      Spec : constant Node_Id := Parent (Formal_Id);
5813
 
5814
   begin
5815
      --  Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5816
      --  since we ensure that corresponding actuals are always valid at the
5817
      --  point of the call.
5818
 
5819
      if Out_Present (Spec) then
5820
         if Ekind (Scope (Formal_Id)) = E_Function
5821
           or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5822
         then
5823
            Error_Msg_N ("functions can only have IN parameters", Spec);
5824
            Set_Ekind (Formal_Id, E_In_Parameter);
5825
 
5826
         elsif In_Present (Spec) then
5827
            Set_Ekind (Formal_Id, E_In_Out_Parameter);
5828
 
5829
         else
5830
            Set_Ekind               (Formal_Id, E_Out_Parameter);
5831
            Set_Never_Set_In_Source (Formal_Id, True);
5832
            Set_Is_True_Constant    (Formal_Id, False);
5833
            Set_Current_Value       (Formal_Id, Empty);
5834
         end if;
5835
 
5836
      else
5837
         Set_Ekind (Formal_Id, E_In_Parameter);
5838
      end if;
5839
 
5840
      --  Set Is_Known_Non_Null for access parameters since the language
5841
      --  guarantees that access parameters are always non-null. We also set
5842
      --  Can_Never_Be_Null, since there is no way to change the value.
5843
 
5844
      if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5845
 
5846
         --  Ada 2005 (AI-231): In Ada95, access parameters are always non-
5847
         --  null; In Ada 2005, only if then null_exclusion is explicit.
5848
 
5849
         if Ada_Version < Ada_05
5850
           or else Can_Never_Be_Null (Etype (Formal_Id))
5851
         then
5852
            Set_Is_Known_Non_Null (Formal_Id);
5853
            Set_Can_Never_Be_Null (Formal_Id);
5854
         end if;
5855
 
5856
      --  Ada 2005 (AI-231): Null-exclusion access subtype
5857
 
5858
      elsif Is_Access_Type (Etype (Formal_Id))
5859
        and then Can_Never_Be_Null (Etype (Formal_Id))
5860
      then
5861
         Set_Is_Known_Non_Null (Formal_Id);
5862
      end if;
5863
 
5864
      Set_Mechanism (Formal_Id, Default_Mechanism);
5865
      Set_Formal_Validity (Formal_Id);
5866
   end Set_Formal_Mode;
5867
 
5868
   -------------------------
5869
   -- Set_Formal_Validity --
5870
   -------------------------
5871
 
5872
   procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5873
   begin
5874
      --  If no validity checking, then we cannot assume anything about the
5875
      --  validity of parameters, since we do not know there is any checking
5876
      --  of the validity on the call side.
5877
 
5878
      if not Validity_Checks_On then
5879
         return;
5880
 
5881
      --  If validity checking for parameters is enabled, this means we are
5882
      --  not supposed to make any assumptions about argument values.
5883
 
5884
      elsif Validity_Check_Parameters then
5885
         return;
5886
 
5887
      --  If we are checking in parameters, we will assume that the caller is
5888
      --  also checking parameters, so we can assume the parameter is valid.
5889
 
5890
      elsif Ekind (Formal_Id) = E_In_Parameter
5891
        and then Validity_Check_In_Params
5892
      then
5893
         Set_Is_Known_Valid (Formal_Id, True);
5894
 
5895
      --  Similar treatment for IN OUT parameters
5896
 
5897
      elsif Ekind (Formal_Id) = E_In_Out_Parameter
5898
        and then Validity_Check_In_Out_Params
5899
      then
5900
         Set_Is_Known_Valid (Formal_Id, True);
5901
      end if;
5902
   end Set_Formal_Validity;
5903
 
5904
   ------------------------
5905
   -- Subtype_Conformant --
5906
   ------------------------
5907
 
5908
   function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5909
      Result : Boolean;
5910
   begin
5911
      Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5912
      return Result;
5913
   end Subtype_Conformant;
5914
 
5915
   ---------------------
5916
   -- Type_Conformant --
5917
   ---------------------
5918
 
5919
   function Type_Conformant
5920
     (New_Id                   : Entity_Id;
5921
      Old_Id                   : Entity_Id;
5922
      Skip_Controlling_Formals : Boolean := False) return Boolean
5923
   is
5924
      Result : Boolean;
5925
   begin
5926
      Check_Conformance
5927
        (New_Id, Old_Id, Type_Conformant, False, Result,
5928
         Skip_Controlling_Formals => Skip_Controlling_Formals);
5929
      return Result;
5930
   end Type_Conformant;
5931
 
5932
   -------------------------------
5933
   -- Valid_Operator_Definition --
5934
   -------------------------------
5935
 
5936
   procedure Valid_Operator_Definition (Designator : Entity_Id) is
5937
      N    : Integer := 0;
5938
      F    : Entity_Id;
5939
      Id   : constant Name_Id := Chars (Designator);
5940
      N_OK : Boolean;
5941
 
5942
   begin
5943
      F := First_Formal (Designator);
5944
      while Present (F) loop
5945
         N := N + 1;
5946
 
5947
         if Present (Default_Value (F)) then
5948
            Error_Msg_N
5949
              ("default values not allowed for operator parameters",
5950
               Parent (F));
5951
         end if;
5952
 
5953
         Next_Formal (F);
5954
      end loop;
5955
 
5956
      --  Verify that user-defined operators have proper number of arguments
5957
      --  First case of operators which can only be unary
5958
 
5959
      if Id = Name_Op_Not
5960
        or else Id = Name_Op_Abs
5961
      then
5962
         N_OK := (N = 1);
5963
 
5964
      --  Case of operators which can be unary or binary
5965
 
5966
      elsif Id = Name_Op_Add
5967
        or Id = Name_Op_Subtract
5968
      then
5969
         N_OK := (N in 1 .. 2);
5970
 
5971
      --  All other operators can only be binary
5972
 
5973
      else
5974
         N_OK := (N = 2);
5975
      end if;
5976
 
5977
      if not N_OK then
5978
         Error_Msg_N
5979
           ("incorrect number of arguments for operator", Designator);
5980
      end if;
5981
 
5982
      if Id = Name_Op_Ne
5983
        and then Base_Type (Etype (Designator)) = Standard_Boolean
5984
        and then not Is_Intrinsic_Subprogram (Designator)
5985
      then
5986
         Error_Msg_N
5987
            ("explicit definition of inequality not allowed", Designator);
5988
      end if;
5989
   end Valid_Operator_Definition;
5990
 
5991
end Sem_Ch6;

powered by: WebSVN 2.1.0

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