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