OSDN Git Service

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