OSDN Git Service

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