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