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