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