OSDN Git Service

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