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