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