OSDN Git Service

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