OSDN Git Service

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