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