OSDN Git Service

2004-08-13 Olivier Hainque <hainque@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch6.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 6                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2004, 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Einfo;    use Einfo;
31 with Elists;   use Elists;
32 with Errout;   use Errout;
33 with Expander; use Expander;
34 with Exp_Ch7;  use Exp_Ch7;
35 with Fname;    use Fname;
36 with Freeze;   use Freeze;
37 with Lib.Xref; use Lib.Xref;
38 with Namet;    use Namet;
39 with Lib;      use Lib;
40 with Nlists;   use Nlists;
41 with Nmake;    use Nmake;
42 with Opt;      use Opt;
43 with Output;   use Output;
44 with Rtsfind;  use Rtsfind;
45 with Sem;      use Sem;
46 with Sem_Cat;  use Sem_Cat;
47 with Sem_Ch3;  use Sem_Ch3;
48 with Sem_Ch4;  use Sem_Ch4;
49 with Sem_Ch5;  use Sem_Ch5;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Dist; use Sem_Dist;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res;  use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sem_Type; use Sem_Type;
62 with Sem_Warn; use Sem_Warn;
63 with Sinput;   use Sinput;
64 with Stand;    use Stand;
65 with Sinfo;    use Sinfo;
66 with Sinfo.CN; use Sinfo.CN;
67 with Snames;   use Snames;
68 with Stringt;  use Stringt;
69 with Style;
70 with Stylesw;  use Stylesw;
71 with Tbuild;   use Tbuild;
72 with Uintp;    use Uintp;
73 with Urealp;   use Urealp;
74 with Validsw;  use Validsw;
75
76 package body Sem_Ch6 is
77
78    -----------------------
79    -- Local Subprograms --
80    -----------------------
81
82    procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
83    --  Analyze a generic subprogram body. N is the body to be analyzed,
84    --  and Gen_Id is the defining entity Id for the corresponding spec.
85
86    procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
87    --  If a subprogram has pragma Inline and inlining is active, use generic
88    --  machinery to build an unexpanded body for the subprogram. This body is
89    --  subsequenty used for inline expansions at call sites. If subprogram can
90    --  be inlined (depending on size and nature of local declarations) this
91    --  function returns true. Otherwise subprogram body is treated normally.
92    --  If proper warnings are enabled and the subprogram contains a construct
93    --  that cannot be inlined, the offending construct is flagged accordingly.
94
95    type Conformance_Type is
96      (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
97    --  Conformance type used for following call, meaning matches the
98    --  RM definitions of the corresponding terms.
99
100    procedure Check_Conformance
101      (New_Id   : Entity_Id;
102       Old_Id   : Entity_Id;
103       Ctype    : Conformance_Type;
104       Errmsg   : Boolean;
105       Conforms : out Boolean;
106       Err_Loc  : Node_Id := Empty;
107       Get_Inst : Boolean := False);
108    --  Given two entities, this procedure checks that the profiles associated
109    --  with these entities meet the conformance criterion given by the third
110    --  parameter. If they conform, Conforms is set True and control returns
111    --  to the caller. If they do not conform, Conforms is set to False, and
112    --  in addition, if Errmsg is True on the call, proper messages are output
113    --  to complain about the conformance failure. If Err_Loc is non_Empty
114    --  the error messages are placed on Err_Loc, if Err_Loc is empty, then
115    --  error messages are placed on the appropriate part of the construct
116    --  denoted by New_Id. If Get_Inst is true, then this is a mode conformance
117    --  against a formal access-to-subprogram type so Get_Instance_Of must
118    --  be called.
119
120    procedure Check_Overriding_Operation
121      (N    : Node_Id;
122       Subp : Entity_Id);
123    --  Check that a subprogram with a pragma Overriding or Optional_Overriding
124    --  is legal. This check is performed here rather than in Sem_Prag because
125    --  the pragma must follow immediately the declaration, and can be treated
126    --  as part of the declaration itself, as described in AI-218.
127
128    procedure Check_Subprogram_Order (N : Node_Id);
129    --  N is the N_Subprogram_Body node for a subprogram. This routine applies
130    --  the alpha ordering rule for N if this ordering requirement applicable.
131
132    function Is_Non_Overriding_Operation
133      (Prev_E : Entity_Id;
134       New_E  : Entity_Id) return Boolean;
135    --  Enforce the rule given in 12.3(18): a private operation in an instance
136    --  overrides an inherited operation only if the corresponding operation
137    --  was overriding in the generic. This can happen for primitive operations
138    --  of types derived (in the generic unit) from formal private or formal
139    --  derived types.
140
141    procedure Check_Returns
142      (HSS  : Node_Id;
143       Mode : Character;
144       Err  : out Boolean);
145    --  Called to check for missing return statements in a function body,
146    --  or for returns present in a procedure body which has No_Return set.
147    --  L is the handled statement sequence for the subprogram body. This
148    --  procedure checks all flow paths to make sure they either have a
149    --  return (Mode = 'F') or do not have a return (Mode = 'P'). The flag
150    --  Err is set if there are any control paths not explicitly terminated
151    --  by a return in the function case, and is True otherwise.
152
153    function Conforming_Types
154      (T1       : Entity_Id;
155       T2       : Entity_Id;
156       Ctype    : Conformance_Type;
157       Get_Inst : Boolean := False) return Boolean;
158    --  Check that two formal parameter types conform, checking both
159    --  for equality of base types, and where required statically
160    --  matching subtypes, depending on the setting of Ctype.
161
162    procedure Enter_Overloaded_Entity (S : Entity_Id);
163    --  This procedure makes S, a new overloaded entity, into the first
164    --  visible entity with that name.
165
166    procedure Install_Entity (E : Entity_Id);
167    --  Make single entity visible. Used for generic formals as well.
168
169    procedure Install_Formals (Id : Entity_Id);
170    --  On entry to a subprogram body, make the formals visible. Note
171    --  that simply placing the subprogram on the scope stack is not
172    --  sufficient: the formals must become the current entities for
173    --  their names.
174
175    procedure Make_Inequality_Operator (S : Entity_Id);
176    --  Create the declaration for an inequality operator that is implicitly
177    --  created by a user-defined equality operator that yields a boolean.
178
179    procedure May_Need_Actuals (Fun : Entity_Id);
180    --  Flag functions that can be called without parameters, i.e. those that
181    --  have no parameters, or those for which defaults exist for all parameters
182
183    procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
184    --  If there is a separate spec for a subprogram or generic subprogram,
185    --  the formals of the body are treated as references to the corresponding
186    --  formals of the spec. This reference does not count as an actual use of
187    --  the formal, in order to diagnose formals that are unused in the body.
188
189    procedure Set_Formal_Validity (Formal_Id : Entity_Id);
190    --  Formal_Id is an formal parameter entity. This procedure deals with
191    --  setting the proper validity status for this entity, which depends
192    --  on the kind of parameter and the validity checking mode.
193
194    ---------------------------------------------
195    -- Analyze_Abstract_Subprogram_Declaration --
196    ---------------------------------------------
197
198    procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
199       Designator : constant Entity_Id :=
200                      Analyze_Subprogram_Specification (Specification (N));
201       Scop       : constant Entity_Id := Current_Scope;
202
203    begin
204       Generate_Definition (Designator);
205       Set_Is_Abstract (Designator);
206       New_Overloaded_Entity (Designator);
207       Check_Delayed_Subprogram (Designator);
208
209       Set_Categorization_From_Scope (Designator, Scop);
210
211       if Ekind (Scope (Designator)) = E_Protected_Type then
212          Error_Msg_N
213            ("abstract subprogram not allowed in protected type", N);
214       end if;
215
216       Generate_Reference_To_Formals (Designator);
217    end Analyze_Abstract_Subprogram_Declaration;
218
219    ----------------------------
220    -- Analyze_Function_Call  --
221    ----------------------------
222
223    procedure Analyze_Function_Call (N : Node_Id) is
224       P      : constant Node_Id := Name (N);
225       L      : constant List_Id := Parameter_Associations (N);
226       Actual : Node_Id;
227
228    begin
229       Analyze (P);
230
231       --  If error analyzing name, then set Any_Type as result type and return
232
233       if Etype (P) = Any_Type then
234          Set_Etype (N, Any_Type);
235          return;
236       end if;
237
238       --  Otherwise analyze the parameters
239
240       if Present (L) then
241          Actual := First (L);
242
243          while Present (Actual) loop
244             Analyze (Actual);
245             Check_Parameterless_Call (Actual);
246             Next (Actual);
247          end loop;
248       end if;
249
250       Analyze_Call (N);
251    end Analyze_Function_Call;
252
253    -------------------------------------
254    -- Analyze_Generic_Subprogram_Body --
255    -------------------------------------
256
257    procedure Analyze_Generic_Subprogram_Body
258      (N      : Node_Id;
259       Gen_Id : Entity_Id)
260    is
261       Gen_Decl : constant Node_Id     := Unit_Declaration_Node (Gen_Id);
262       Kind     : constant Entity_Kind := Ekind (Gen_Id);
263       Body_Id  : Entity_Id;
264       New_N    : Node_Id;
265       Spec     : Node_Id;
266
267    begin
268       --  Copy body and disable expansion while analyzing the generic
269       --  For a stub, do not copy the stub (which would load the proper body),
270       --  this will be done when the proper body is analyzed.
271
272       if Nkind (N) /= N_Subprogram_Body_Stub then
273          New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
274          Rewrite (N, New_N);
275          Start_Generic;
276       end if;
277
278       Spec := Specification (N);
279
280       --  Within the body of the generic, the subprogram is callable, and
281       --  behaves like the corresponding non-generic unit.
282
283       Body_Id := Defining_Entity (Spec);
284
285       if Kind = E_Generic_Procedure
286         and then Nkind (Spec) /= N_Procedure_Specification
287       then
288          Error_Msg_N ("invalid body for generic procedure ", Body_Id);
289          return;
290
291       elsif Kind = E_Generic_Function
292         and then Nkind (Spec) /= N_Function_Specification
293       then
294          Error_Msg_N ("invalid body for generic function ", Body_Id);
295          return;
296       end if;
297
298       Set_Corresponding_Body (Gen_Decl, Body_Id);
299
300       if Has_Completion (Gen_Id)
301         and then Nkind (Parent (N)) /= N_Subunit
302       then
303          Error_Msg_N ("duplicate generic body", N);
304          return;
305       else
306          Set_Has_Completion (Gen_Id);
307       end if;
308
309       if Nkind (N) = N_Subprogram_Body_Stub then
310          Set_Ekind (Defining_Entity (Specification (N)), Kind);
311       else
312          Set_Corresponding_Spec (N, Gen_Id);
313       end if;
314
315       if Nkind (Parent (N)) = N_Compilation_Unit then
316          Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
317       end if;
318
319       --  Make generic parameters immediately visible in the body. They are
320       --  needed to process the formals declarations. Then make the formals
321       --  visible in a separate step.
322
323       New_Scope (Gen_Id);
324
325       declare
326          E         : Entity_Id;
327          First_Ent : Entity_Id;
328
329       begin
330          First_Ent := First_Entity (Gen_Id);
331
332          E := First_Ent;
333          while Present (E) and then not Is_Formal (E) loop
334             Install_Entity (E);
335             Next_Entity (E);
336          end loop;
337
338          Set_Use (Generic_Formal_Declarations (Gen_Decl));
339
340          --  Now generic formals are visible, and the specification can be
341          --  analyzed, for subsequent conformance check.
342
343          Body_Id := Analyze_Subprogram_Specification (Spec);
344
345          --  Make formal parameters visible
346
347          if Present (E) then
348
349             --  E is the first formal parameter, we loop through the formals
350             --  installing them so that they will be visible.
351
352             Set_First_Entity (Gen_Id, E);
353             while Present (E) loop
354                Install_Entity (E);
355                Next_Formal (E);
356             end loop;
357          end if;
358
359          --  Visible generic entity is callable within its own body.
360
361          Set_Ekind (Gen_Id, Ekind (Body_Id));
362          Set_Ekind (Body_Id, E_Subprogram_Body);
363          Set_Convention (Body_Id, Convention (Gen_Id));
364          Set_Scope (Body_Id, Scope (Gen_Id));
365          Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
366
367          if Nkind (N) = N_Subprogram_Body_Stub then
368
369             --  No body to analyze, so restore state of generic unit.
370
371             Set_Ekind (Gen_Id, Kind);
372             Set_Ekind (Body_Id, Kind);
373
374             if Present (First_Ent) then
375                Set_First_Entity (Gen_Id, First_Ent);
376             end if;
377
378             End_Scope;
379             return;
380          end if;
381
382          --  If this is a compilation unit, it must be made visible
383          --  explicitly, because the compilation of the declaration,
384          --  unlike other library unit declarations, does not. If it
385          --  is not a unit, the following is redundant but harmless.
386
387          Set_Is_Immediately_Visible (Gen_Id);
388          Reference_Body_Formals (Gen_Id, Body_Id);
389
390          Set_Actual_Subtypes (N, Current_Scope);
391          Analyze_Declarations (Declarations (N));
392          Check_Completion;
393          Analyze (Handled_Statement_Sequence (N));
394
395          Save_Global_References (Original_Node (N));
396
397          --  Prior to exiting the scope, include generic formals again
398          --  (if any are present) in the set of local entities.
399
400          if Present (First_Ent) then
401             Set_First_Entity (Gen_Id, First_Ent);
402          end if;
403
404          Check_References (Gen_Id);
405       end;
406
407       Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
408       End_Scope;
409       Check_Subprogram_Order (N);
410
411       --  Outside of its body, unit is generic again.
412
413       Set_Ekind (Gen_Id, Kind);
414       Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
415       Style.Check_Identifier (Body_Id, Gen_Id);
416       End_Generic;
417    end Analyze_Generic_Subprogram_Body;
418
419    -----------------------------
420    -- Analyze_Operator_Symbol --
421    -----------------------------
422
423    --  An operator symbol such as "+" or "and" may appear in context where
424    --  the literal denotes an entity name, such as  "+"(x, y) or in a
425    --  context when it is just a string, as in  (conjunction = "or"). In
426    --  these cases the parser generates this node, and the semantics does
427    --  the disambiguation. Other such case are actuals in an instantiation,
428    --  the generic unit in an instantiation, and pragma arguments.
429
430    procedure Analyze_Operator_Symbol (N : Node_Id) is
431       Par : constant Node_Id := Parent (N);
432
433    begin
434       if        (Nkind (Par) = N_Function_Call and then N = Name (Par))
435         or else  Nkind (Par) = N_Function_Instantiation
436         or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
437         or else (Nkind (Par) = N_Pragma_Argument_Association
438                    and then not Is_Pragma_String_Literal (Par))
439         or else  Nkind (Par) = N_Subprogram_Renaming_Declaration
440         or else  (Nkind (Par) = N_Attribute_Reference
441                    and then Attribute_Name (Par) /= Name_Value)
442       then
443          Find_Direct_Name (N);
444
445       else
446          Change_Operator_Symbol_To_String_Literal (N);
447          Analyze (N);
448       end if;
449    end Analyze_Operator_Symbol;
450
451    -----------------------------------
452    -- Analyze_Parameter_Association --
453    -----------------------------------
454
455    procedure Analyze_Parameter_Association (N : Node_Id) is
456    begin
457       Analyze (Explicit_Actual_Parameter (N));
458    end Analyze_Parameter_Association;
459
460    ----------------------------
461    -- Analyze_Procedure_Call --
462    ----------------------------
463
464    procedure Analyze_Procedure_Call (N : Node_Id) is
465       Loc     : constant Source_Ptr := Sloc (N);
466       P       : constant Node_Id    := Name (N);
467       Actuals : constant List_Id    := Parameter_Associations (N);
468       Actual  : Node_Id;
469       New_N   : Node_Id;
470
471       procedure Analyze_Call_And_Resolve;
472       --  Do Analyze and Resolve calls for procedure call
473
474       ------------------------------
475       -- Analyze_Call_And_Resolve --
476       ------------------------------
477
478       procedure Analyze_Call_And_Resolve is
479       begin
480          if Nkind (N) = N_Procedure_Call_Statement then
481             Analyze_Call (N);
482             Resolve (N, Standard_Void_Type);
483          else
484             Analyze (N);
485          end if;
486       end Analyze_Call_And_Resolve;
487
488    --  Start of processing for Analyze_Procedure_Call
489
490    begin
491       --  The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
492       --  a procedure call or an entry call. The prefix may denote an access
493       --  to subprogram type, in which case an implicit dereference applies.
494       --  If the prefix is an indexed component (without implicit defererence)
495       --  then the construct denotes a call to a member of an entire family.
496       --  If the prefix is a simple name, it may still denote a call to a
497       --  parameterless member of an entry family. Resolution of these various
498       --  interpretations is delicate.
499
500       Analyze (P);
501
502       --  If error analyzing prefix, then set Any_Type as result and return
503
504       if Etype (P) = Any_Type then
505          Set_Etype (N, Any_Type);
506          return;
507       end if;
508
509       --  Otherwise analyze the parameters
510
511       if Present (Actuals) then
512          Actual := First (Actuals);
513
514          while Present (Actual) loop
515             Analyze (Actual);
516             Check_Parameterless_Call (Actual);
517             Next (Actual);
518          end loop;
519       end if;
520
521       --  Special processing for Elab_Spec and Elab_Body calls
522
523       if Nkind (P) = N_Attribute_Reference
524         and then (Attribute_Name (P) = Name_Elab_Spec
525                    or else Attribute_Name (P) = Name_Elab_Body)
526       then
527          if Present (Actuals) then
528             Error_Msg_N
529               ("no parameters allowed for this call", First (Actuals));
530             return;
531          end if;
532
533          Set_Etype (N, Standard_Void_Type);
534          Set_Analyzed (N);
535
536       elsif Is_Entity_Name (P)
537         and then Is_Record_Type (Etype (Entity (P)))
538         and then Remote_AST_I_Dereference (P)
539       then
540          return;
541
542       elsif Is_Entity_Name (P)
543         and then Ekind (Entity (P)) /= E_Entry_Family
544       then
545          if Is_Access_Type (Etype (P))
546            and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
547            and then No (Actuals)
548            and then Comes_From_Source (N)
549          then
550             Error_Msg_N ("missing explicit dereference in call", N);
551          end if;
552
553          Analyze_Call_And_Resolve;
554
555       --  If the prefix is the simple name of an entry family, this is
556       --  a parameterless call from within the task body itself.
557
558       elsif Is_Entity_Name (P)
559         and then Nkind (P) = N_Identifier
560         and then Ekind (Entity (P)) = E_Entry_Family
561         and then Present (Actuals)
562         and then No (Next (First (Actuals)))
563       then
564          --  Can be call to parameterless entry family. What appears to be
565          --  the sole argument is in fact the entry index. Rewrite prefix
566          --  of node accordingly. Source representation is unchanged by this
567          --  transformation.
568
569          New_N :=
570            Make_Indexed_Component (Loc,
571              Prefix =>
572                Make_Selected_Component (Loc,
573                  Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
574                  Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
575              Expressions => Actuals);
576          Set_Name (N, New_N);
577          Set_Etype (New_N, Standard_Void_Type);
578          Set_Parameter_Associations (N, No_List);
579          Analyze_Call_And_Resolve;
580
581       elsif Nkind (P) = N_Explicit_Dereference then
582          if Ekind (Etype (P)) = E_Subprogram_Type then
583             Analyze_Call_And_Resolve;
584          else
585             Error_Msg_N ("expect access to procedure in call", P);
586          end if;
587
588       --  The name can be a selected component or an indexed component
589       --  that yields an access to subprogram. Such a prefix is legal if
590       --  the call has parameter associations.
591
592       elsif Is_Access_Type (Etype (P))
593         and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
594       then
595          if Present (Actuals) then
596             Analyze_Call_And_Resolve;
597          else
598             Error_Msg_N ("missing explicit dereference in call ", N);
599          end if;
600
601       --  If not an access to subprogram, then the prefix must resolve to
602       --  the name of an entry, entry family, or protected operation.
603
604       --  For the case of a simple entry call, P is a selected component
605       --  where the prefix is the task and the selector name is the entry.
606       --  A call to a protected procedure will have the same syntax. If
607       --  the protected object contains overloaded operations, the entity
608       --  may appear as a function, the context will select the operation
609       --  whose type is Void.
610
611       elsif Nkind (P) = N_Selected_Component
612         and then (Ekind (Entity (Selector_Name (P))) = E_Entry
613                     or else
614                   Ekind (Entity (Selector_Name (P))) = E_Procedure
615                     or else
616                   Ekind (Entity (Selector_Name (P))) = E_Function)
617       then
618          Analyze_Call_And_Resolve;
619
620       elsif Nkind (P) = N_Selected_Component
621         and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
622         and then Present (Actuals)
623         and then No (Next (First (Actuals)))
624       then
625          --  Can be call to parameterless entry family. What appears to be
626          --  the sole argument is in fact the entry index. Rewrite prefix
627          --  of node accordingly. Source representation is unchanged by this
628          --  transformation.
629
630          New_N :=
631            Make_Indexed_Component (Loc,
632              Prefix => New_Copy (P),
633              Expressions => Actuals);
634          Set_Name (N, New_N);
635          Set_Etype (New_N, Standard_Void_Type);
636          Set_Parameter_Associations (N, No_List);
637          Analyze_Call_And_Resolve;
638
639       --  For the case of a reference to an element of an entry family, P is
640       --  an indexed component whose prefix is a selected component (task and
641       --  entry family), and whose index is the entry family index.
642
643       elsif Nkind (P) = N_Indexed_Component
644         and then Nkind (Prefix (P)) = N_Selected_Component
645         and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
646       then
647          Analyze_Call_And_Resolve;
648
649       --  If the prefix is the name of an entry family, it is a call from
650       --  within the task body itself.
651
652       elsif Nkind (P) = N_Indexed_Component
653         and then Nkind (Prefix (P)) = N_Identifier
654         and then Ekind (Entity (Prefix (P))) = E_Entry_Family
655       then
656          New_N :=
657            Make_Selected_Component (Loc,
658              Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
659              Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
660          Rewrite (Prefix (P), New_N);
661          Analyze (P);
662          Analyze_Call_And_Resolve;
663
664       --  Anything else is an error.
665
666       else
667          Error_Msg_N ("Invalid procedure or entry call", N);
668       end if;
669    end Analyze_Procedure_Call;
670
671    ------------------------------
672    -- Analyze_Return_Statement --
673    ------------------------------
674
675    procedure Analyze_Return_Statement (N : Node_Id) is
676       Loc      : constant Source_Ptr := Sloc (N);
677       Expr     : Node_Id;
678       Scope_Id : Entity_Id;
679       Kind     : Entity_Kind;
680       R_Type   : Entity_Id;
681
682    begin
683       --  Find subprogram or accept statement enclosing the return statement
684
685       Scope_Id := Empty;
686       for J in reverse 0 .. Scope_Stack.Last loop
687          Scope_Id := Scope_Stack.Table (J).Entity;
688          exit when Ekind (Scope_Id) /= E_Block and then
689                    Ekind (Scope_Id) /= E_Loop;
690       end loop;
691
692       pragma Assert (Present (Scope_Id));
693
694       Kind := Ekind (Scope_Id);
695       Expr := Expression (N);
696
697       if Kind /= E_Function
698         and then Kind /= E_Generic_Function
699         and then Kind /= E_Procedure
700         and then Kind /= E_Generic_Procedure
701         and then Kind /= E_Entry
702         and then Kind /= E_Entry_Family
703       then
704          Error_Msg_N ("illegal context for return statement", N);
705
706       elsif Present (Expr) then
707          if Kind = E_Function or else Kind = E_Generic_Function then
708             Set_Return_Present (Scope_Id);
709             R_Type := Etype (Scope_Id);
710             Set_Return_Type (N, R_Type);
711             Analyze_And_Resolve (Expr, R_Type);
712
713             if (Is_Class_Wide_Type (Etype (Expr))
714                  or else Is_Dynamically_Tagged (Expr))
715               and then not Is_Class_Wide_Type (R_Type)
716             then
717                Error_Msg_N
718                  ("dynamically tagged expression not allowed!", Expr);
719             end if;
720
721             Apply_Constraint_Check (Expr, R_Type);
722
723             --  ??? A real run-time accessibility check is needed
724             --  in cases involving dereferences of access parameters.
725             --  For now we just check the static cases.
726
727             if Is_Return_By_Reference_Type (Etype (Scope_Id))
728               and then Object_Access_Level (Expr)
729                 > Subprogram_Access_Level (Scope_Id)
730             then
731                Rewrite (N,
732                  Make_Raise_Program_Error (Loc,
733                    Reason => PE_Accessibility_Check_Failed));
734                Analyze (N);
735
736                Error_Msg_N
737                  ("cannot return a local value by reference?", N);
738                Error_Msg_NE
739                  ("& will be raised at run time?!",
740                   N, Standard_Program_Error);
741             end if;
742
743          elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
744             Error_Msg_N ("procedure cannot return value (use function)", N);
745
746          else
747             Error_Msg_N ("accept statement cannot return value", N);
748          end if;
749
750       --  No expression present
751
752       else
753          if Kind = E_Function or Kind = E_Generic_Function then
754             Error_Msg_N ("missing expression in return from function", N);
755          end if;
756
757          if (Ekind (Scope_Id) = E_Procedure
758               or else Ekind (Scope_Id) = E_Generic_Procedure)
759            and then No_Return (Scope_Id)
760          then
761             Error_Msg_N
762               ("RETURN statement not allowed (No_Return)", N);
763          end if;
764       end if;
765
766       Check_Unreachable_Code (N);
767    end Analyze_Return_Statement;
768
769    -----------------------------
770    -- Analyze_Subprogram_Body --
771    -----------------------------
772
773    --  This procedure is called for regular subprogram bodies, generic bodies,
774    --  and for subprogram stubs of both kinds. In the case of stubs, only the
775    --  specification matters, and is used to create a proper declaration for
776    --  the subprogram, or to perform conformance checks.
777
778    procedure Analyze_Subprogram_Body (N : Node_Id) is
779       Loc          : constant Source_Ptr := Sloc (N);
780       Body_Spec    : constant Node_Id    := Specification (N);
781       Body_Id      : Entity_Id           := Defining_Entity (Body_Spec);
782       Prev_Id      : constant Entity_Id  := Current_Entity_In_Scope (Body_Id);
783       Body_Deleted : constant Boolean    := False;
784
785       HSS          : Node_Id;
786       Spec_Id      : Entity_Id;
787       Spec_Decl    : Node_Id   := Empty;
788       Last_Formal  : Entity_Id := Empty;
789       Conformant   : Boolean;
790       Missing_Ret  : Boolean;
791       P_Ent        : Entity_Id;
792
793       procedure Check_Following_Pragma;
794       --  If front-end inlining is enabled, look ahead to recognize a pragma
795       --  that may appear after the body.
796
797       procedure Check_Following_Pragma is
798          Prag : Node_Id;
799
800       begin
801          if Front_End_Inlining
802            and then Is_List_Member (N)
803            and then Present (Spec_Decl)
804            and then List_Containing (N) = List_Containing (Spec_Decl)
805          then
806             Prag := Next (N);
807
808             if Present (Prag)
809               and then Nkind (Prag) = N_Pragma
810               and then Get_Pragma_Id (Chars (Prag)) = Pragma_Inline
811               and then
812               Chars
813                 (Expression (First (Pragma_Argument_Associations (Prag))))
814                    = Chars (Body_Id)
815             then
816                Analyze (Prag);
817             end if;
818          end if;
819       end Check_Following_Pragma;
820
821    --  Start of processing for Analyze_Subprogram_Body
822
823    begin
824       if Debug_Flag_C then
825          Write_Str ("====  Compiling subprogram body ");
826          Write_Name (Chars (Body_Id));
827          Write_Str (" from ");
828          Write_Location (Loc);
829          Write_Eol;
830       end if;
831
832       Trace_Scope (N, Body_Id, " Analyze subprogram");
833
834       --  Generic subprograms are handled separately. They always have
835       --  a generic specification. Determine whether current scope has
836       --  a previous declaration.
837
838       --  If the subprogram body is defined within an instance of the
839       --  same name, the instance appears as a package renaming, and
840       --  will be hidden within the subprogram.
841
842       if Present (Prev_Id)
843         and then not Is_Overloadable (Prev_Id)
844         and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
845                    or else Comes_From_Source (Prev_Id))
846       then
847          if Is_Generic_Subprogram (Prev_Id) then
848             Spec_Id := Prev_Id;
849             Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
850             Set_Is_Child_Unit       (Body_Id, Is_Child_Unit       (Spec_Id));
851
852             Analyze_Generic_Subprogram_Body (N, Spec_Id);
853             return;
854
855          else
856             --  Previous entity conflicts with subprogram name.
857             --  Attempting to enter name will post error.
858
859             Enter_Name (Body_Id);
860             return;
861          end if;
862
863       --  Non-generic case, find the subprogram declaration, if one was
864       --  seen, or enter new overloaded entity in the current scope.
865       --  If the current_entity is the body_id itself, the unit is being
866       --  analyzed as part of the context of one of its subunits. No need
867       --  to redo the analysis.
868
869       elsif Prev_Id = Body_Id
870         and then Has_Completion (Body_Id)
871       then
872          return;
873
874       else
875          Body_Id := Analyze_Subprogram_Specification (Body_Spec);
876
877          if Nkind (N) = N_Subprogram_Body_Stub
878            or else No (Corresponding_Spec (N))
879          then
880             Spec_Id := Find_Corresponding_Spec (N);
881
882             --  If this is a duplicate body, no point in analyzing it
883
884             if Error_Posted (N) then
885                return;
886             end if;
887
888             --  A subprogram body should cause freezing of its own
889             --  declaration, but if there was no previous explicit
890             --  declaration, then the subprogram will get frozen too
891             --  late (there may be code within the body that depends
892             --  on the subprogram having been frozen, such as uses of
893             --  extra formals), so we force it to be frozen here.
894             --  Same holds if the body and the spec are compilation units.
895
896             if No (Spec_Id) then
897                Freeze_Before (N, Body_Id);
898
899             elsif Nkind (Parent (N)) = N_Compilation_Unit then
900                Freeze_Before (N, Spec_Id);
901             end if;
902          else
903             Spec_Id := Corresponding_Spec (N);
904          end if;
905       end if;
906
907       --  Do not inline any subprogram that contains nested subprograms,
908       --  since the backend inlining circuit seems to generate uninitialized
909       --  references in this case. We know this happens in the case of front
910       --  end ZCX support, but it also appears it can happen in other cases
911       --  as well. The backend often rejects attempts to inline in the case
912       --  of nested procedures anyway, so little if anything is lost by this.
913
914       --  Do not do this test if errors have been detected, because in some
915       --  error cases, this code blows up, and we don't need it anyway if
916       --  there have been errors, since we won't get to the linker anyway.
917
918       if Serious_Errors_Detected = 0 then
919          P_Ent := Body_Id;
920          loop
921             P_Ent := Scope (P_Ent);
922             exit when No (P_Ent) or else P_Ent = Standard_Standard;
923
924             if Is_Subprogram (P_Ent) then
925                Set_Is_Inlined (P_Ent, False);
926
927                if Comes_From_Source (P_Ent)
928                  and then Has_Pragma_Inline (P_Ent)
929                then
930                   Cannot_Inline
931                     ("cannot inline& (nested subprogram)?",
932                      N, P_Ent);
933                end if;
934             end if;
935          end loop;
936       end if;
937
938       --  Case of fully private operation in the body of the protected type.
939       --  We must create a declaration for the subprogram, in order to attach
940       --  the protected subprogram that will be used in internal calls.
941
942       if No (Spec_Id)
943         and then Comes_From_Source (N)
944         and then Is_Protected_Type (Current_Scope)
945       then
946          declare
947             Decl     : Node_Id;
948             Plist    : List_Id;
949             Formal   : Entity_Id;
950             New_Spec : Node_Id;
951
952          begin
953             Formal := First_Formal (Body_Id);
954
955             --  The protected operation always has at least one formal,
956             --  namely the object itself, but it is only placed in the
957             --  parameter list if expansion is enabled.
958
959             if Present (Formal)
960               or else Expander_Active
961             then
962                Plist := New_List;
963
964             else
965                Plist := No_List;
966             end if;
967
968             while Present (Formal) loop
969                Append
970                  (Make_Parameter_Specification (Loc,
971                    Defining_Identifier =>
972                      Make_Defining_Identifier (Sloc (Formal),
973                        Chars => Chars (Formal)),
974                    In_Present  => In_Present (Parent (Formal)),
975                    Out_Present => Out_Present (Parent (Formal)),
976                    Parameter_Type =>
977                      New_Reference_To (Etype (Formal), Loc),
978                    Expression =>
979                      New_Copy_Tree (Expression (Parent (Formal)))),
980                  Plist);
981
982                Next_Formal (Formal);
983             end loop;
984
985             if Nkind (Body_Spec) = N_Procedure_Specification then
986                New_Spec :=
987                  Make_Procedure_Specification (Loc,
988                     Defining_Unit_Name =>
989                       Make_Defining_Identifier (Sloc (Body_Id),
990                         Chars => Chars (Body_Id)),
991                     Parameter_Specifications => Plist);
992             else
993                New_Spec :=
994                  Make_Function_Specification (Loc,
995                     Defining_Unit_Name =>
996                       Make_Defining_Identifier (Sloc (Body_Id),
997                         Chars => Chars (Body_Id)),
998                     Parameter_Specifications => Plist,
999                     Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
1000             end if;
1001
1002             Decl :=
1003               Make_Subprogram_Declaration (Loc,
1004                 Specification => New_Spec);
1005             Insert_Before (N, Decl);
1006             Spec_Id := Defining_Unit_Name (New_Spec);
1007
1008             --  Indicate that the entity comes from source, to ensure that
1009             --  cross-reference information is properly generated.
1010             --  The body itself is rewritten during expansion, and the
1011             --  body entity will not appear in calls to the operation.
1012
1013             Set_Comes_From_Source (Spec_Id, True);
1014             Analyze (Decl);
1015             Set_Has_Completion (Spec_Id);
1016             Set_Convention (Spec_Id, Convention_Protected);
1017          end;
1018
1019       elsif Present (Spec_Id) then
1020          Spec_Decl := Unit_Declaration_Node (Spec_Id);
1021       end if;
1022
1023       --  Place subprogram on scope stack, and make formals visible. If there
1024       --  is a spec, the visible entity remains that of the spec.
1025
1026       if Present (Spec_Id) then
1027          Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1028          if Style_Check then
1029             Style.Check_Identifier (Body_Id, Spec_Id);
1030          end if;
1031
1032          Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1033          Set_Is_Child_Unit       (Body_Id, Is_Child_Unit       (Spec_Id));
1034
1035          if Is_Abstract (Spec_Id) then
1036             Error_Msg_N ("an abstract subprogram cannot have a body", N);
1037             return;
1038          else
1039             Set_Convention (Body_Id, Convention (Spec_Id));
1040             Set_Has_Completion (Spec_Id);
1041
1042             if Is_Protected_Type (Scope (Spec_Id)) then
1043                Set_Privals_Chain (Spec_Id, New_Elmt_List);
1044             end if;
1045
1046             --  If this is a body generated for a renaming, do not check for
1047             --  full conformance. The check is redundant, because the spec of
1048             --  the body is a copy of the spec in the renaming declaration,
1049             --  and the test can lead to spurious errors on nested defaults.
1050
1051             if Present (Spec_Decl)
1052               and then not Comes_From_Source (N)
1053               and then
1054                 (Nkind (Original_Node (Spec_Decl)) =
1055                                         N_Subprogram_Renaming_Declaration
1056                    or else (Present (Corresponding_Body (Spec_Decl))
1057                               and then
1058                                 Nkind (Unit_Declaration_Node
1059                                         (Corresponding_Body (Spec_Decl))) =
1060                                            N_Subprogram_Renaming_Declaration))
1061             then
1062                Conformant := True;
1063             else
1064                Check_Conformance
1065                  (Body_Id, Spec_Id,
1066                    Fully_Conformant, True, Conformant, Body_Id);
1067             end if;
1068
1069             --  If the body is not fully conformant, we have to decide if we
1070             --  should analyze it or not. If it has a really messed up profile
1071             --  then we probably should not analyze it, since we will get too
1072             --  many bogus messages.
1073
1074             --  Our decision is to go ahead in the non-fully conformant case
1075             --  only if it is at least mode conformant with the spec. Note
1076             --  that the call to Check_Fully_Conformant has issued the proper
1077             --  error messages to complain about the lack of conformance.
1078
1079             if not Conformant
1080               and then not Mode_Conformant (Body_Id, Spec_Id)
1081             then
1082                return;
1083             end if;
1084          end if;
1085
1086          if Spec_Id /= Body_Id then
1087             Reference_Body_Formals (Spec_Id, Body_Id);
1088          end if;
1089
1090          if Nkind (N) /= N_Subprogram_Body_Stub then
1091             Set_Corresponding_Spec (N, Spec_Id);
1092             Install_Formals (Spec_Id);
1093             Last_Formal := Last_Entity (Spec_Id);
1094             New_Scope (Spec_Id);
1095
1096             --  Make sure that the subprogram is immediately visible. For
1097             --  child units that have no separate spec this is indispensable.
1098             --  Otherwise it is safe albeit redundant.
1099
1100             Set_Is_Immediately_Visible (Spec_Id);
1101          end if;
1102
1103          Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
1104          Set_Ekind (Body_Id, E_Subprogram_Body);
1105          Set_Scope (Body_Id, Scope (Spec_Id));
1106
1107       --  Case of subprogram body with no previous spec
1108
1109       else
1110          if Style_Check
1111            and then Comes_From_Source (Body_Id)
1112            and then not Suppress_Style_Checks (Body_Id)
1113            and then not In_Instance
1114          then
1115             Style.Body_With_No_Spec (N);
1116          end if;
1117
1118          New_Overloaded_Entity (Body_Id);
1119
1120          if Nkind (N) /= N_Subprogram_Body_Stub then
1121             Set_Acts_As_Spec (N);
1122             Generate_Definition (Body_Id);
1123             Generate_Reference
1124               (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
1125             Generate_Reference_To_Formals (Body_Id);
1126             Install_Formals (Body_Id);
1127             New_Scope (Body_Id);
1128          end if;
1129       end if;
1130
1131       --  If this is the proper body of a stub, we must verify that the stub
1132       --  conforms to the body, and to the previous spec if one was present.
1133       --  we know already that the body conforms to that spec. This test is
1134       --  only required for subprograms that come from source.
1135
1136       if Nkind (Parent (N)) = N_Subunit
1137         and then Comes_From_Source (N)
1138         and then not Error_Posted (Body_Id)
1139       then
1140          declare
1141             Old_Id : constant Entity_Id :=
1142                        Defining_Entity
1143                          (Specification (Corresponding_Stub (Parent (N))));
1144
1145             Conformant : Boolean := False;
1146
1147          begin
1148             if No (Spec_Id) then
1149                Check_Fully_Conformant (Body_Id, Old_Id);
1150
1151             else
1152                Check_Conformance
1153                  (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
1154
1155                if not Conformant then
1156
1157                   --  The stub was taken to be a new declaration. Indicate
1158                   --  that it lacks a body.
1159
1160                   Set_Has_Completion (Old_Id, False);
1161                end if;
1162             end if;
1163          end;
1164       end if;
1165
1166       Set_Has_Completion (Body_Id);
1167       Check_Eliminated (Body_Id);
1168
1169       if Nkind (N) = N_Subprogram_Body_Stub then
1170          return;
1171
1172       elsif  Present (Spec_Id)
1173         and then Expander_Active
1174       then
1175          Check_Following_Pragma;
1176
1177          if Is_Always_Inlined (Spec_Id)
1178            or else (Has_Pragma_Inline (Spec_Id)
1179              and then (Front_End_Inlining or else Configurable_Run_Time_Mode))
1180          then
1181             Build_Body_To_Inline (N, Spec_Id);
1182          end if;
1183       end if;
1184
1185       --  Ada 2005 (AI-262): In library subprogram bodies, after the analysis
1186       --  if its specification we have to install the private withed units.
1187
1188       if Is_Compilation_Unit (Body_Id)
1189         and then Scope (Body_Id) = Standard_Standard
1190       then
1191          Install_Private_With_Clauses (Body_Id);
1192       end if;
1193
1194       --  Now we can go on to analyze the body
1195
1196       HSS := Handled_Statement_Sequence (N);
1197       Set_Actual_Subtypes (N, Current_Scope);
1198       Analyze_Declarations (Declarations (N));
1199       Check_Completion;
1200       Analyze (HSS);
1201       Process_End_Label (HSS, 't', Current_Scope);
1202       End_Scope;
1203       Check_Subprogram_Order (N);
1204       Set_Analyzed (Body_Id);
1205
1206       --  If we have a separate spec, then the analysis of the declarations
1207       --  caused the entities in the body to be chained to the spec id, but
1208       --  we want them chained to the body id. Only the formal parameters
1209       --  end up chained to the spec id in this case.
1210
1211       if Present (Spec_Id) then
1212
1213          --  If a parent unit is categorized, the context of a subunit
1214          --  must conform to the categorization. Conversely, if a child
1215          --  unit is categorized, the parents themselves must conform.
1216
1217          if Nkind (Parent (N)) = N_Subunit then
1218             Validate_Categorization_Dependency (N, Spec_Id);
1219
1220          elsif Is_Child_Unit (Spec_Id) then
1221             Validate_Categorization_Dependency
1222               (Unit_Declaration_Node (Spec_Id), Spec_Id);
1223          end if;
1224
1225          if Present (Last_Formal) then
1226             Set_Next_Entity
1227               (Last_Entity (Body_Id), Next_Entity (Last_Formal));
1228             Set_Next_Entity (Last_Formal, Empty);
1229             Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1230             Set_Last_Entity (Spec_Id, Last_Formal);
1231
1232          else
1233             Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1234             Set_Last_Entity  (Body_Id, Last_Entity (Spec_Id));
1235             Set_First_Entity (Spec_Id, Empty);
1236             Set_Last_Entity  (Spec_Id, Empty);
1237          end if;
1238       end if;
1239
1240       --  If function, check return statements
1241
1242       if Nkind (Body_Spec) = N_Function_Specification then
1243          declare
1244             Id : Entity_Id;
1245
1246          begin
1247             if Present (Spec_Id) then
1248                Id := Spec_Id;
1249             else
1250                Id := Body_Id;
1251             end if;
1252
1253             if Return_Present (Id) then
1254                Check_Returns (HSS, 'F', Missing_Ret);
1255
1256                if Missing_Ret then
1257                   Set_Has_Missing_Return (Id);
1258                end if;
1259
1260             elsif not Is_Machine_Code_Subprogram (Id)
1261               and then not Body_Deleted
1262             then
1263                Error_Msg_N ("missing RETURN statement in function body", N);
1264             end if;
1265          end;
1266
1267       --  If procedure with No_Return, check returns
1268
1269       elsif Nkind (Body_Spec) = N_Procedure_Specification
1270         and then Present (Spec_Id)
1271         and then No_Return (Spec_Id)
1272       then
1273          Check_Returns (HSS, 'P', Missing_Ret);
1274       end if;
1275
1276       --  Now we are going to check for variables that are never modified
1277       --  in the body of the procedure. We omit these checks if the first
1278       --  statement of the procedure raises an exception. In particular
1279       --  this deals with the common idiom of a stubbed function, which
1280       --  might appear as something like
1281
1282       --     function F (A : Integer) return Some_Type;
1283       --        X : Some_Type;
1284       --     begin
1285       --        raise Program_Error;
1286       --        return X;
1287       --     end F;
1288
1289       --  Here the purpose of X is simply to satisfy the (annoying)
1290       --  requirement in Ada that there be at least one return, and
1291       --  we certainly do not want to go posting warnings on X that
1292       --  it is not initialized!
1293
1294       declare
1295          Stm : Node_Id := First (Statements (HSS));
1296
1297       begin
1298          --  Skip an initial label (for one thing this occurs when we
1299          --  are in front end ZCX mode, but in any case it is irrelevant).
1300
1301          if Nkind (Stm) = N_Label then
1302             Next (Stm);
1303          end if;
1304
1305          --  Do the test on the original statement before expansion
1306
1307          declare
1308             Ostm : constant Node_Id := Original_Node (Stm);
1309
1310          begin
1311             --  If explicit raise statement, return with no checks
1312
1313             if Nkind (Ostm) = N_Raise_Statement then
1314                return;
1315
1316             --  Check for explicit call cases which likely raise an exception
1317
1318             elsif Nkind (Ostm) = N_Procedure_Call_Statement then
1319                if Is_Entity_Name (Name (Ostm)) then
1320                   declare
1321                      Ent : constant Entity_Id := Entity (Name (Ostm));
1322
1323                   begin
1324                      --  If the procedure is marked No_Return, then likely it
1325                      --  raises an exception, but in any case it is not coming
1326                      --  back here, so no need to check beyond the call.
1327
1328                      if Ekind (Ent) = E_Procedure
1329                        and then No_Return (Ent)
1330                      then
1331                         return;
1332
1333                      --  If the procedure name is Raise_Exception, then also
1334                      --  assume that it raises an exception. The main target
1335                      --  here is Ada.Exceptions.Raise_Exception, but this name
1336                      --  is pretty evocative in any context! Note that the
1337                      --  procedure in Ada.Exceptions is not marked No_Return
1338                      --  because of the annoying case of the null exception Id.
1339
1340                      elsif Chars (Ent) = Name_Raise_Exception then
1341                         return;
1342                      end if;
1343                   end;
1344                end if;
1345             end if;
1346          end;
1347       end;
1348
1349       --  Check for variables that are never modified
1350
1351       declare
1352          E1, E2 : Entity_Id;
1353
1354       begin
1355          --  If there is a separate spec, then transfer Never_Set_In_Source
1356          --  flags from out parameters to the corresponding entities in the
1357          --  body. The reason we do that is we want to post error flags on
1358          --  the body entities, not the spec entities.
1359
1360          if Present (Spec_Id) then
1361             E1 := First_Entity (Spec_Id);
1362
1363             while Present (E1) loop
1364                if Ekind (E1) = E_Out_Parameter then
1365                   E2 := First_Entity (Body_Id);
1366                   while Present (E2) loop
1367                      exit when Chars (E1) = Chars (E2);
1368                      Next_Entity (E2);
1369                   end loop;
1370
1371                   if Present (E2) then
1372                      Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
1373                   end if;
1374                end if;
1375
1376                Next_Entity (E1);
1377             end loop;
1378          end if;
1379
1380          --  Check references in body unless it was deleted. Note that the
1381          --  check of Body_Deleted here is not just for efficiency, it is
1382          --  necessary to avoid junk warnings on formal parameters.
1383
1384          if not Body_Deleted then
1385             Check_References (Body_Id);
1386          end if;
1387       end;
1388    end Analyze_Subprogram_Body;
1389
1390    ------------------------------------
1391    -- Analyze_Subprogram_Declaration --
1392    ------------------------------------
1393
1394    procedure Analyze_Subprogram_Declaration (N : Node_Id) is
1395       Designator : constant Entity_Id :=
1396                      Analyze_Subprogram_Specification (Specification (N));
1397       Scop       : constant Entity_Id := Current_Scope;
1398
1399    --  Start of processing for Analyze_Subprogram_Declaration
1400
1401    begin
1402       Generate_Definition (Designator);
1403
1404       --  Check for RCI unit subprogram declarations against in-lined
1405       --  subprograms and subprograms having access parameter or limited
1406       --  parameter without Read and Write (RM E.2.3(12-13)).
1407
1408       Validate_RCI_Subprogram_Declaration (N);
1409
1410       Trace_Scope
1411         (N,
1412          Defining_Entity (N),
1413          " Analyze subprogram spec. ");
1414
1415       if Debug_Flag_C then
1416          Write_Str ("====  Compiling subprogram spec ");
1417          Write_Name (Chars (Designator));
1418          Write_Str (" from ");
1419          Write_Location (Sloc (N));
1420          Write_Eol;
1421       end if;
1422
1423       New_Overloaded_Entity (Designator);
1424       Check_Delayed_Subprogram (Designator);
1425
1426       --  What is the following code for, it used to be
1427
1428       --  ???   Set_Suppress_Elaboration_Checks
1429       --  ???     (Designator, Elaboration_Checks_Suppressed (Designator));
1430
1431       --  The following seems equivalent, but a bit dubious
1432
1433       if Elaboration_Checks_Suppressed (Designator) then
1434          Set_Kill_Elaboration_Checks (Designator);
1435       end if;
1436
1437       if Scop /= Standard_Standard
1438         and then not Is_Child_Unit (Designator)
1439       then
1440          Set_Categorization_From_Scope (Designator, Scop);
1441       else
1442          --  For a compilation unit, check for library-unit pragmas.
1443
1444          New_Scope (Designator);
1445          Set_Categorization_From_Pragmas (N);
1446          Validate_Categorization_Dependency (N, Designator);
1447          Pop_Scope;
1448       end if;
1449
1450       --  For a compilation unit, set body required. This flag will only be
1451       --  reset if a valid Import or Interface pragma is processed later on.
1452
1453       if Nkind (Parent (N)) = N_Compilation_Unit then
1454          Set_Body_Required (Parent (N), True);
1455       end if;
1456
1457       Generate_Reference_To_Formals (Designator);
1458       Check_Eliminated (Designator);
1459
1460       if Comes_From_Source (N)
1461         and then Is_List_Member (N)
1462       then
1463          Check_Overriding_Operation (N, Designator);
1464       end if;
1465
1466    end Analyze_Subprogram_Declaration;
1467
1468    --------------------------------------
1469    -- Analyze_Subprogram_Specification --
1470    --------------------------------------
1471
1472    --  Reminder: N here really is a subprogram specification (not a subprogram
1473    --  declaration). This procedure is called to analyze the specification in
1474    --  both subprogram bodies and subprogram declarations (specs).
1475
1476    function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
1477       Designator : constant Entity_Id := Defining_Entity (N);
1478       Formals    : constant List_Id   := Parameter_Specifications (N);
1479       Typ        : Entity_Id;
1480
1481    begin
1482       Generate_Definition (Designator);
1483
1484       if Nkind (N) = N_Function_Specification then
1485          Set_Ekind (Designator, E_Function);
1486          Set_Mechanism (Designator, Default_Mechanism);
1487
1488          if Subtype_Mark (N) /= Error then
1489             Find_Type (Subtype_Mark (N));
1490             Typ := Entity (Subtype_Mark (N));
1491             Set_Etype (Designator, Typ);
1492
1493             if Ekind (Typ) = E_Incomplete_Type
1494               or else (Is_Class_Wide_Type (Typ)
1495                          and then
1496                            Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1497             then
1498                Error_Msg_N
1499                  ("invalid use of incomplete type", Subtype_Mark (N));
1500             end if;
1501
1502          else
1503             Set_Etype (Designator, Any_Type);
1504          end if;
1505
1506       else
1507          Set_Ekind (Designator, E_Procedure);
1508          Set_Etype (Designator, Standard_Void_Type);
1509       end if;
1510
1511       if Present (Formals) then
1512          Set_Scope (Designator, Current_Scope);
1513          New_Scope (Designator);
1514          Process_Formals (Formals, N);
1515          End_Scope;
1516       end if;
1517
1518       if Nkind (N) = N_Function_Specification then
1519          if Nkind (Designator) = N_Defining_Operator_Symbol then
1520             Valid_Operator_Definition (Designator);
1521          end if;
1522
1523          May_Need_Actuals (Designator);
1524
1525          if Is_Abstract (Etype (Designator))
1526            and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
1527          then
1528             Error_Msg_N
1529               ("function that returns abstract type must be abstract", N);
1530          end if;
1531       end if;
1532
1533       return Designator;
1534    end Analyze_Subprogram_Specification;
1535
1536    --------------------------
1537    -- Build_Body_To_Inline --
1538    --------------------------
1539
1540    procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
1541       Decl : constant Node_Id := Unit_Declaration_Node (Subp);
1542       Original_Body   : Node_Id;
1543       Body_To_Analyze : Node_Id;
1544       Max_Size        : constant := 10;
1545       Stat_Count      : Integer := 0;
1546
1547       function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
1548       --  Check for declarations that make inlining not worthwhile.
1549
1550       function Has_Excluded_Statement   (Stats : List_Id) return Boolean;
1551       --  Check for statements that make inlining not worthwhile: any
1552       --  tasking statement, nested at any level. Keep track of total
1553       --  number of elementary statements, as a measure of acceptable size.
1554
1555       function Has_Pending_Instantiation return Boolean;
1556       --  If some enclosing body contains instantiations that appear before
1557       --  the corresponding generic body, the enclosing body has a freeze node
1558       --  so that it can be elaborated after the generic itself. This might
1559       --  conflict with subsequent inlinings, so that it is unsafe to try to
1560       --  inline in such a case.
1561
1562       procedure Remove_Pragmas;
1563       --  A pragma Unreferenced that mentions a formal parameter has no
1564       --  meaning when the body is inlined and the formals are rewritten.
1565       --  Remove it from body to inline. The analysis of the non-inlined
1566       --  body will handle the pragma properly.
1567
1568       ------------------------------
1569       -- Has_Excluded_Declaration --
1570       ------------------------------
1571
1572       function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
1573          D : Node_Id;
1574
1575          function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
1576          --  Nested subprograms make a given body ineligible for inlining,
1577          --  but we make an exception for instantiations of unchecked
1578          --  conversion. The body has not been analyzed yet, so we check
1579          --  the name, and verify that the visible entity with that name is
1580          --  the predefined unit.
1581
1582          function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
1583             Id : constant Node_Id := Name (D);
1584             Conv : Entity_Id;
1585
1586          begin
1587             if Nkind (Id) = N_Identifier
1588               and then Chars (Id) = Name_Unchecked_Conversion
1589             then
1590                Conv := Current_Entity (Id);
1591
1592             elsif Nkind (Id) = N_Selected_Component
1593               and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
1594             then
1595                Conv := Current_Entity (Selector_Name (Id));
1596
1597             else
1598                return False;
1599             end if;
1600
1601             return
1602               Present (Conv)
1603               and then Scope (Conv) = Standard_Standard
1604               and then Is_Intrinsic_Subprogram (Conv);
1605          end Is_Unchecked_Conversion;
1606
1607       --  Start of processing for Has_Excluded_Declaration
1608
1609       begin
1610          D := First (Decls);
1611
1612          while Present (D) loop
1613             if       (Nkind (D) = N_Function_Instantiation
1614                         and then not Is_Unchecked_Conversion (D))
1615               or else Nkind (D) = N_Protected_Type_Declaration
1616               or else Nkind (D) = N_Package_Declaration
1617               or else Nkind (D) = N_Package_Instantiation
1618               or else Nkind (D) = N_Subprogram_Body
1619               or else Nkind (D) = N_Procedure_Instantiation
1620               or else Nkind (D) = N_Task_Type_Declaration
1621             then
1622                Cannot_Inline
1623                  ("cannot inline & (non-allowed declaration)?", D, Subp);
1624                return True;
1625             end if;
1626
1627             Next (D);
1628          end loop;
1629
1630          return False;
1631       end Has_Excluded_Declaration;
1632
1633       ----------------------------
1634       -- Has_Excluded_Statement --
1635       ----------------------------
1636
1637       function Has_Excluded_Statement (Stats : List_Id) return Boolean is
1638          S : Node_Id;
1639          E : Node_Id;
1640
1641       begin
1642          S := First (Stats);
1643
1644          while Present (S) loop
1645             Stat_Count := Stat_Count + 1;
1646
1647             if Nkind (S) = N_Abort_Statement
1648               or else Nkind (S) = N_Asynchronous_Select
1649               or else Nkind (S) = N_Conditional_Entry_Call
1650               or else Nkind (S) = N_Delay_Relative_Statement
1651               or else Nkind (S) = N_Delay_Until_Statement
1652               or else Nkind (S) = N_Selective_Accept
1653               or else Nkind (S) = N_Timed_Entry_Call
1654             then
1655                Cannot_Inline
1656                  ("cannot inline & (non-allowed statement)?", S, Subp);
1657                return True;
1658
1659             elsif Nkind (S) = N_Block_Statement then
1660                if Present (Declarations (S))
1661                  and then Has_Excluded_Declaration (Declarations (S))
1662                then
1663                   return True;
1664
1665                elsif Present (Handled_Statement_Sequence (S))
1666                   and then
1667                     (Present
1668                       (Exception_Handlers (Handled_Statement_Sequence (S)))
1669                      or else
1670                        Has_Excluded_Statement
1671                          (Statements (Handled_Statement_Sequence (S))))
1672                then
1673                   return True;
1674                end if;
1675
1676             elsif Nkind (S) = N_Case_Statement then
1677                E := First (Alternatives (S));
1678
1679                while Present (E) loop
1680                   if Has_Excluded_Statement (Statements (E)) then
1681                      return True;
1682                   end if;
1683
1684                   Next (E);
1685                end loop;
1686
1687             elsif Nkind (S) = N_If_Statement then
1688                if Has_Excluded_Statement (Then_Statements (S)) then
1689                   return True;
1690                end if;
1691
1692                if Present (Elsif_Parts (S)) then
1693                   E := First (Elsif_Parts (S));
1694
1695                   while Present (E) loop
1696                      if Has_Excluded_Statement (Then_Statements (E)) then
1697                         return True;
1698                      end if;
1699                      Next (E);
1700                   end loop;
1701                end if;
1702
1703                if Present (Else_Statements (S))
1704                  and then Has_Excluded_Statement (Else_Statements (S))
1705                then
1706                   return True;
1707                end if;
1708
1709             elsif Nkind (S) = N_Loop_Statement
1710               and then Has_Excluded_Statement (Statements (S))
1711             then
1712                return True;
1713             end if;
1714
1715             Next (S);
1716          end loop;
1717
1718          return False;
1719       end Has_Excluded_Statement;
1720
1721       -------------------------------
1722       -- Has_Pending_Instantiation --
1723       -------------------------------
1724
1725       function Has_Pending_Instantiation return Boolean is
1726          S : Entity_Id := Current_Scope;
1727
1728       begin
1729          while Present (S) loop
1730             if Is_Compilation_Unit (S)
1731               or else Is_Child_Unit (S)
1732             then
1733                return False;
1734             elsif Ekind (S) = E_Package
1735               and then Has_Forward_Instantiation (S)
1736             then
1737                return True;
1738             end if;
1739
1740             S := Scope (S);
1741          end loop;
1742
1743          return False;
1744       end Has_Pending_Instantiation;
1745
1746       --------------------
1747       -- Remove_Pragmas --
1748       --------------------
1749
1750       procedure Remove_Pragmas is
1751          Decl : Node_Id;
1752          Nxt  : Node_Id;
1753
1754       begin
1755          Decl := First (Declarations (Body_To_Analyze));
1756          while Present (Decl) loop
1757             Nxt := Next (Decl);
1758
1759             if Nkind (Decl) = N_Pragma
1760               and then Chars (Decl) = Name_Unreferenced
1761             then
1762                Remove (Decl);
1763             end if;
1764
1765             Decl := Nxt;
1766          end loop;
1767       end Remove_Pragmas;
1768
1769    --  Start of processing for Build_Body_To_Inline
1770
1771    begin
1772       if Nkind (Decl) = N_Subprogram_Declaration
1773         and then Present (Body_To_Inline (Decl))
1774       then
1775          return;    --  Done already.
1776
1777       --  Functions that return unconstrained composite types will require
1778       --  secondary stack handling, and cannot currently be inlined.
1779       --  Ditto for functions that return controlled types, where controlled
1780       --  actions interfere in complex ways with inlining.
1781
1782       elsif Ekind (Subp) = E_Function
1783         and then not Is_Scalar_Type (Etype (Subp))
1784         and then not Is_Access_Type (Etype (Subp))
1785         and then not Is_Constrained (Etype (Subp))
1786       then
1787          Cannot_Inline
1788            ("cannot inline & (unconstrained return type)?", N, Subp);
1789          return;
1790
1791       elsif Ekind (Subp) = E_Function
1792         and then Controlled_Type (Etype (Subp))
1793       then
1794          Cannot_Inline
1795            ("cannot inline & (controlled return type)?", N, Subp);
1796          return;
1797       end if;
1798
1799       if Present (Declarations (N))
1800         and then Has_Excluded_Declaration (Declarations (N))
1801       then
1802          return;
1803       end if;
1804
1805       if Present (Handled_Statement_Sequence (N)) then
1806          if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1807             Cannot_Inline
1808               ("cannot inline& (exception handler)?",
1809                First (Exception_Handlers (Handled_Statement_Sequence (N))),
1810                Subp);
1811             return;
1812          elsif
1813            Has_Excluded_Statement
1814              (Statements (Handled_Statement_Sequence (N)))
1815          then
1816             return;
1817          end if;
1818       end if;
1819
1820       --  We do not inline a subprogram  that is too large, unless it is
1821       --  marked Inline_Always. This pragma does not suppress the other
1822       --  checks on inlining (forbidden declarations, handlers, etc).
1823
1824       if Stat_Count > Max_Size
1825         and then not Is_Always_Inlined (Subp)
1826       then
1827          Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
1828          return;
1829       end if;
1830
1831       if Has_Pending_Instantiation then
1832          Cannot_Inline
1833            ("cannot inline& (forward instance within enclosing body)?",
1834              N, Subp);
1835          return;
1836       end if;
1837
1838       --  Within an instance, the body to inline must be treated as a nested
1839       --  generic, so that the proper global references are preserved.
1840
1841       if In_Instance then
1842          Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1843          Original_Body := Copy_Generic_Node (N, Empty, True);
1844       else
1845          Original_Body := Copy_Separate_Tree (N);
1846       end if;
1847
1848       --  We need to capture references to the formals in order to substitute
1849       --  the actuals at the point of inlining, i.e. instantiation. To treat
1850       --  the formals as globals to the body to inline, we nest it within
1851       --  a dummy parameterless subprogram, declared within the real one.
1852       --  To avoid generating an internal name (which is never public, and
1853       --  which affects serial numbers of other generated names), we use
1854       --  an internal symbol that cannot conflict with user declarations.
1855
1856       Set_Parameter_Specifications (Specification (Original_Body), No_List);
1857       Set_Defining_Unit_Name
1858         (Specification (Original_Body),
1859           Make_Defining_Identifier (Sloc (N), Name_uParent));
1860       Set_Corresponding_Spec (Original_Body, Empty);
1861
1862       Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
1863
1864       --  Set return type of function, which is also global and does not need
1865       --  to be resolved.
1866
1867       if Ekind (Subp) = E_Function then
1868          Set_Subtype_Mark (Specification (Body_To_Analyze),
1869            New_Occurrence_Of (Etype (Subp), Sloc (N)));
1870       end if;
1871
1872       if No (Declarations (N)) then
1873          Set_Declarations (N, New_List (Body_To_Analyze));
1874       else
1875          Append (Body_To_Analyze, Declarations (N));
1876       end if;
1877
1878       Expander_Mode_Save_And_Set (False);
1879       Remove_Pragmas;
1880
1881       Analyze (Body_To_Analyze);
1882       New_Scope (Defining_Entity (Body_To_Analyze));
1883       Save_Global_References (Original_Body);
1884       End_Scope;
1885       Remove (Body_To_Analyze);
1886
1887       Expander_Mode_Restore;
1888       Set_Body_To_Inline (Decl, Original_Body);
1889       Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
1890       Set_Is_Inlined (Subp);
1891
1892       if In_Instance then
1893          Restore_Env;
1894       end if;
1895    end Build_Body_To_Inline;
1896
1897    -------------------
1898    -- Cannot_Inline --
1899    -------------------
1900
1901    procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
1902    begin
1903       --  Do not emit warning if this is a predefined unit which is not
1904       --  the main unit. With validity checks enabled, some predefined
1905       --  subprograms may contain nested subprograms and become ineligible
1906       --  for inlining.
1907
1908       if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
1909         and then not In_Extended_Main_Source_Unit (Subp)
1910       then
1911          null;
1912
1913       elsif Is_Always_Inlined (Subp) then
1914          Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
1915
1916       elsif Ineffective_Inline_Warnings then
1917          Error_Msg_NE (Msg, N, Subp);
1918       end if;
1919    end Cannot_Inline;
1920
1921    -----------------------
1922    -- Check_Conformance --
1923    -----------------------
1924
1925    procedure Check_Conformance
1926      (New_Id   : Entity_Id;
1927       Old_Id   : Entity_Id;
1928       Ctype    : Conformance_Type;
1929       Errmsg   : Boolean;
1930       Conforms : out Boolean;
1931       Err_Loc  : Node_Id := Empty;
1932       Get_Inst : Boolean := False)
1933    is
1934       Old_Type   : constant Entity_Id := Etype (Old_Id);
1935       New_Type   : constant Entity_Id := Etype (New_Id);
1936       Old_Formal : Entity_Id;
1937       New_Formal : Entity_Id;
1938
1939       procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
1940       --  Post error message for conformance error on given node.
1941       --  Two messages are output. The first points to the previous
1942       --  declaration with a general "no conformance" message.
1943       --  The second is the detailed reason, supplied as Msg. The
1944       --  parameter N provide information for a possible & insertion
1945       --  in the message, and also provides the location for posting
1946       --  the message in the absence of a specified Err_Loc location.
1947
1948       -----------------------
1949       -- Conformance_Error --
1950       -----------------------
1951
1952       procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
1953          Enode : Node_Id;
1954
1955       begin
1956          Conforms := False;
1957
1958          if Errmsg then
1959             if No (Err_Loc) then
1960                Enode := N;
1961             else
1962                Enode := Err_Loc;
1963             end if;
1964
1965             Error_Msg_Sloc := Sloc (Old_Id);
1966
1967             case Ctype is
1968                when Type_Conformant =>
1969                   Error_Msg_N
1970                     ("not type conformant with declaration#!", Enode);
1971
1972                when Mode_Conformant =>
1973                   Error_Msg_N
1974                     ("not mode conformant with declaration#!", Enode);
1975
1976                when Subtype_Conformant =>
1977                   Error_Msg_N
1978                     ("not subtype conformant with declaration#!", Enode);
1979
1980                when Fully_Conformant =>
1981                   Error_Msg_N
1982                     ("not fully conformant with declaration#!", Enode);
1983             end case;
1984
1985             Error_Msg_NE (Msg, Enode, N);
1986          end if;
1987       end Conformance_Error;
1988
1989    --  Start of processing for Check_Conformance
1990
1991    begin
1992       Conforms := True;
1993
1994       --  We need a special case for operators, since they don't
1995       --  appear explicitly.
1996
1997       if Ctype = Type_Conformant then
1998          if Ekind (New_Id) = E_Operator
1999            and then Operator_Matches_Spec (New_Id, Old_Id)
2000          then
2001             return;
2002          end if;
2003       end if;
2004
2005       --  If both are functions/operators, check return types conform
2006
2007       if Old_Type /= Standard_Void_Type
2008         and then New_Type /= Standard_Void_Type
2009       then
2010          if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
2011             Conformance_Error ("return type does not match!", New_Id);
2012             return;
2013          end if;
2014
2015       --  If either is a function/operator and the other isn't, error
2016
2017       elsif Old_Type /= Standard_Void_Type
2018         or else New_Type /= Standard_Void_Type
2019       then
2020          Conformance_Error ("functions can only match functions!", New_Id);
2021          return;
2022       end if;
2023
2024       --  In subtype conformant case, conventions must match (RM 6.3.1(16))
2025       --  If this is a renaming as body, refine error message to indicate that
2026       --  the conflict is with the original declaration. If the entity is not
2027       --  frozen, the conventions don't have to match, the one of the renamed
2028       --  entity is inherited.
2029
2030       if Ctype >= Subtype_Conformant then
2031          if Convention (Old_Id) /= Convention (New_Id) then
2032
2033             if not Is_Frozen (New_Id) then
2034                null;
2035
2036             elsif Present (Err_Loc)
2037               and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
2038               and then Present (Corresponding_Spec (Err_Loc))
2039             then
2040                Error_Msg_Name_1 := Chars (New_Id);
2041                Error_Msg_Name_2 :=
2042                  Name_Ada + Convention_Id'Pos (Convention (New_Id));
2043
2044                Conformance_Error ("prior declaration for% has convention %!");
2045
2046             else
2047                Conformance_Error ("calling conventions do not match!");
2048             end if;
2049
2050             return;
2051
2052          elsif Is_Formal_Subprogram (Old_Id)
2053            or else Is_Formal_Subprogram (New_Id)
2054          then
2055             Conformance_Error ("formal subprograms not allowed!");
2056             return;
2057          end if;
2058       end if;
2059
2060       --  Deal with parameters
2061
2062       --  Note: we use the entity information, rather than going directly
2063       --  to the specification in the tree. This is not only simpler, but
2064       --  absolutely necessary for some cases of conformance tests between
2065       --  operators, where the declaration tree simply does not exist!
2066
2067       Old_Formal := First_Formal (Old_Id);
2068       New_Formal := First_Formal (New_Id);
2069
2070       while Present (Old_Formal) and then Present (New_Formal) loop
2071          if Ctype = Fully_Conformant then
2072
2073             --  Names must match. Error message is more accurate if we do
2074             --  this before checking that the types of the formals match.
2075
2076             if Chars (Old_Formal) /= Chars (New_Formal) then
2077                Conformance_Error ("name & does not match!", New_Formal);
2078
2079                --  Set error posted flag on new formal as well to stop
2080                --  junk cascaded messages in some cases.
2081
2082                Set_Error_Posted (New_Formal);
2083                return;
2084             end if;
2085          end if;
2086
2087          --  Types must always match. In the visible part of an instance,
2088          --  usual overloading rules for dispatching operations apply, and
2089          --  we check base types (not the actual subtypes).
2090
2091          if In_Instance_Visible_Part
2092            and then Is_Dispatching_Operation (New_Id)
2093          then
2094             if not Conforming_Types
2095               (Base_Type (Etype (Old_Formal)),
2096                  Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
2097             then
2098                Conformance_Error ("type of & does not match!", New_Formal);
2099                return;
2100             end if;
2101
2102          elsif not Conforming_Types
2103            (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
2104          then
2105             Conformance_Error ("type of & does not match!", New_Formal);
2106             return;
2107          end if;
2108
2109          --  For mode conformance, mode must match
2110
2111          if Ctype >= Mode_Conformant
2112            and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
2113          then
2114             Conformance_Error ("mode of & does not match!", New_Formal);
2115             return;
2116          end if;
2117
2118          --  Full conformance checks
2119
2120          if Ctype = Fully_Conformant then
2121
2122             --  We have checked already that names match.
2123             --  Check default expressions for in parameters
2124
2125             if Parameter_Mode (Old_Formal) = E_In_Parameter then
2126                declare
2127                   NewD : constant Boolean :=
2128                            Present (Default_Value (New_Formal));
2129                   OldD : constant Boolean :=
2130                            Present (Default_Value (Old_Formal));
2131                begin
2132                   if NewD or OldD then
2133
2134                      --  The old default value has been analyzed because
2135                      --  the current full declaration will have frozen
2136                      --  everything before. The new default values have not
2137                      --  been analyzed, so analyze them now before we check
2138                      --  for conformance.
2139
2140                      if NewD then
2141                         New_Scope (New_Id);
2142                         Analyze_Per_Use_Expression
2143                           (Default_Value (New_Formal), Etype (New_Formal));
2144                         End_Scope;
2145                      end if;
2146
2147                      if not (NewD and OldD)
2148                        or else not Fully_Conformant_Expressions
2149                                     (Default_Value (Old_Formal),
2150                                      Default_Value (New_Formal))
2151                      then
2152                         Conformance_Error
2153                           ("default expression for & does not match!",
2154                            New_Formal);
2155                         return;
2156                      end if;
2157                   end if;
2158                end;
2159             end if;
2160          end if;
2161
2162          --  A couple of special checks for Ada 83 mode. These checks are
2163          --  skipped if either entity is an operator in package Standard.
2164          --  or if either old or new instance is not from the source program.
2165
2166          if Ada_Version = Ada_83
2167            and then Sloc (Old_Id) > Standard_Location
2168            and then Sloc (New_Id) > Standard_Location
2169            and then Comes_From_Source (Old_Id)
2170            and then Comes_From_Source (New_Id)
2171          then
2172             declare
2173                Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
2174                New_Param : constant Node_Id := Declaration_Node (New_Formal);
2175
2176             begin
2177                --  Explicit IN must be present or absent in both cases. This
2178                --  test is required only in the full conformance case.
2179
2180                if In_Present (Old_Param) /= In_Present (New_Param)
2181                  and then Ctype = Fully_Conformant
2182                then
2183                   Conformance_Error
2184                     ("(Ada 83) IN must appear in both declarations",
2185                      New_Formal);
2186                   return;
2187                end if;
2188
2189                --  Grouping (use of comma in param lists) must be the same
2190                --  This is where we catch a misconformance like:
2191
2192                --    A,B : Integer
2193                --    A : Integer; B : Integer
2194
2195                --  which are represented identically in the tree except
2196                --  for the setting of the flags More_Ids and Prev_Ids.
2197
2198                if More_Ids (Old_Param) /= More_Ids (New_Param)
2199                  or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
2200                then
2201                   Conformance_Error
2202                     ("grouping of & does not match!", New_Formal);
2203                   return;
2204                end if;
2205             end;
2206          end if;
2207
2208          Next_Formal (Old_Formal);
2209          Next_Formal (New_Formal);
2210       end loop;
2211
2212       if Present (Old_Formal) then
2213          Conformance_Error ("too few parameters!");
2214          return;
2215
2216       elsif Present (New_Formal) then
2217          Conformance_Error ("too many parameters!", New_Formal);
2218          return;
2219       end if;
2220
2221    end Check_Conformance;
2222
2223    ------------------------------
2224    -- Check_Delayed_Subprogram --
2225    ------------------------------
2226
2227    procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
2228       F : Entity_Id;
2229
2230       procedure Possible_Freeze (T : Entity_Id);
2231       --  T is the type of either a formal parameter or of the return type.
2232       --  If T is not yet frozen and needs a delayed freeze, then the
2233       --  subprogram itself must be delayed.
2234
2235       procedure Possible_Freeze (T : Entity_Id) is
2236       begin
2237          if Has_Delayed_Freeze (T)
2238            and then not Is_Frozen (T)
2239          then
2240             Set_Has_Delayed_Freeze (Designator);
2241
2242          elsif Is_Access_Type (T)
2243            and then Has_Delayed_Freeze (Designated_Type (T))
2244            and then not Is_Frozen (Designated_Type (T))
2245          then
2246             Set_Has_Delayed_Freeze (Designator);
2247          end if;
2248       end Possible_Freeze;
2249
2250    --  Start of processing for Check_Delayed_Subprogram
2251
2252    begin
2253       --  Never need to freeze abstract subprogram
2254
2255       if Is_Abstract (Designator) then
2256          null;
2257       else
2258          --  Need delayed freeze if return type itself needs a delayed
2259          --  freeze and is not yet frozen.
2260
2261          Possible_Freeze (Etype (Designator));
2262          Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
2263
2264          --  Need delayed freeze if any of the formal types themselves need
2265          --  a delayed freeze and are not yet frozen.
2266
2267          F := First_Formal (Designator);
2268          while Present (F) loop
2269             Possible_Freeze (Etype (F));
2270             Possible_Freeze (Base_Type (Etype (F))); -- needed ???
2271             Next_Formal (F);
2272          end loop;
2273       end if;
2274
2275       --  Mark functions that return by reference. Note that it cannot be
2276       --  done for delayed_freeze subprograms because the underlying
2277       --  returned type may not be known yet (for private types)
2278
2279       if not Has_Delayed_Freeze (Designator)
2280         and then Expander_Active
2281       then
2282          declare
2283             Typ  : constant Entity_Id := Etype (Designator);
2284             Utyp : constant Entity_Id := Underlying_Type (Typ);
2285
2286          begin
2287             if Is_Return_By_Reference_Type (Typ) then
2288                Set_Returns_By_Ref (Designator);
2289
2290             elsif Present (Utyp) and then Controlled_Type (Utyp) then
2291                Set_Returns_By_Ref (Designator);
2292             end if;
2293          end;
2294       end if;
2295    end Check_Delayed_Subprogram;
2296
2297    ------------------------------------
2298    -- Check_Discriminant_Conformance --
2299    ------------------------------------
2300
2301    procedure Check_Discriminant_Conformance
2302      (N        : Node_Id;
2303       Prev     : Entity_Id;
2304       Prev_Loc : Node_Id)
2305    is
2306       Old_Discr      : Entity_Id := First_Discriminant (Prev);
2307       New_Discr      : Node_Id   := First (Discriminant_Specifications (N));
2308       New_Discr_Id   : Entity_Id;
2309       New_Discr_Type : Entity_Id;
2310
2311       procedure Conformance_Error (Msg : String; N : Node_Id);
2312       --  Post error message for conformance error on given node.
2313       --  Two messages are output. The first points to the previous
2314       --  declaration with a general "no conformance" message.
2315       --  The second is the detailed reason, supplied as Msg. The
2316       --  parameter N provide information for a possible & insertion
2317       --  in the message.
2318
2319       -----------------------
2320       -- Conformance_Error --
2321       -----------------------
2322
2323       procedure Conformance_Error (Msg : String; N : Node_Id) is
2324       begin
2325          Error_Msg_Sloc := Sloc (Prev_Loc);
2326          Error_Msg_N ("not fully conformant with declaration#!", N);
2327          Error_Msg_NE (Msg, N, N);
2328       end Conformance_Error;
2329
2330    --  Start of processing for Check_Discriminant_Conformance
2331
2332    begin
2333       while Present (Old_Discr) and then Present (New_Discr) loop
2334
2335          New_Discr_Id := Defining_Identifier (New_Discr);
2336
2337          --  The subtype mark of the discriminant on the full type
2338          --  has not been analyzed so we do it here. For an access
2339          --  discriminant a new type is created.
2340
2341          if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
2342             New_Discr_Type :=
2343               Access_Definition (N, Discriminant_Type (New_Discr));
2344
2345          else
2346             Analyze (Discriminant_Type (New_Discr));
2347             New_Discr_Type := Etype (Discriminant_Type (New_Discr));
2348          end if;
2349
2350          if not Conforming_Types
2351                   (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
2352          then
2353             Conformance_Error ("type of & does not match!", New_Discr_Id);
2354             return;
2355          else
2356             --  Treat the new discriminant as an occurrence of the old
2357             --  one, for navigation purposes, and fill in some semantic
2358             --  information, for completeness.
2359
2360             Generate_Reference (Old_Discr, New_Discr_Id, 'r');
2361             Set_Etype (New_Discr_Id, Etype (Old_Discr));
2362             Set_Scope (New_Discr_Id, Scope (Old_Discr));
2363          end if;
2364
2365          --  Names must match
2366
2367          if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
2368             Conformance_Error ("name & does not match!", New_Discr_Id);
2369             return;
2370          end if;
2371
2372          --  Default expressions must match
2373
2374          declare
2375             NewD : constant Boolean :=
2376                      Present (Expression (New_Discr));
2377             OldD : constant Boolean :=
2378                      Present (Expression (Parent (Old_Discr)));
2379
2380          begin
2381             if NewD or OldD then
2382
2383                --  The old default value has been analyzed and expanded,
2384                --  because the current full declaration will have frozen
2385                --  everything before. The new default values have not
2386                --  been expanded, so expand now to check conformance.
2387
2388                if NewD then
2389                   Analyze_Per_Use_Expression
2390                     (Expression (New_Discr), New_Discr_Type);
2391                end if;
2392
2393                if not (NewD and OldD)
2394                  or else not Fully_Conformant_Expressions
2395                               (Expression (Parent (Old_Discr)),
2396                                Expression (New_Discr))
2397
2398                then
2399                   Conformance_Error
2400                     ("default expression for & does not match!",
2401                      New_Discr_Id);
2402                   return;
2403                end if;
2404             end if;
2405          end;
2406
2407          --  In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
2408
2409          if Ada_Version = Ada_83 then
2410             declare
2411                Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
2412
2413             begin
2414                --  Grouping (use of comma in param lists) must be the same
2415                --  This is where we catch a misconformance like:
2416
2417                --    A,B : Integer
2418                --    A : Integer; B : Integer
2419
2420                --  which are represented identically in the tree except
2421                --  for the setting of the flags More_Ids and Prev_Ids.
2422
2423                if More_Ids (Old_Disc) /= More_Ids (New_Discr)
2424                  or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
2425                then
2426                   Conformance_Error
2427                     ("grouping of & does not match!", New_Discr_Id);
2428                   return;
2429                end if;
2430             end;
2431          end if;
2432
2433          Next_Discriminant (Old_Discr);
2434          Next (New_Discr);
2435       end loop;
2436
2437       if Present (Old_Discr) then
2438          Conformance_Error ("too few discriminants!", Defining_Identifier (N));
2439          return;
2440
2441       elsif Present (New_Discr) then
2442          Conformance_Error
2443            ("too many discriminants!", Defining_Identifier (New_Discr));
2444          return;
2445       end if;
2446    end Check_Discriminant_Conformance;
2447
2448    ----------------------------
2449    -- Check_Fully_Conformant --
2450    ----------------------------
2451
2452    procedure Check_Fully_Conformant
2453      (New_Id  : Entity_Id;
2454       Old_Id  : Entity_Id;
2455       Err_Loc : Node_Id := Empty)
2456    is
2457       Result : Boolean;
2458
2459    begin
2460       Check_Conformance
2461         (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
2462    end Check_Fully_Conformant;
2463
2464    ---------------------------
2465    -- Check_Mode_Conformant --
2466    ---------------------------
2467
2468    procedure Check_Mode_Conformant
2469      (New_Id   : Entity_Id;
2470       Old_Id   : Entity_Id;
2471       Err_Loc  : Node_Id := Empty;
2472       Get_Inst : Boolean := False)
2473    is
2474       Result : Boolean;
2475
2476    begin
2477       Check_Conformance
2478         (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
2479    end Check_Mode_Conformant;
2480
2481    --------------------------------
2482    -- Check_Overriding_Operation --
2483    --------------------------------
2484
2485    procedure Check_Overriding_Operation
2486      (N    : Node_Id;
2487       Subp : Entity_Id)
2488    is
2489       Arg1       : Node_Id;
2490       Decl       : Node_Id;
2491       Has_Pragma : Boolean := False;
2492
2493    begin
2494       --  See whether there is an overriding pragma immediately following
2495       --  the declaration. Intervening pragmas, such as Inline, are allowed.
2496
2497       Decl := Next (N);
2498       while Present (Decl)
2499         and then Nkind (Decl) = N_Pragma
2500       loop
2501          if Chars (Decl) = Name_Overriding
2502            or else Chars (Decl) = Name_Optional_Overriding
2503          then
2504             --  For now disable the use of these pragmas, until the ARG
2505             --  finalizes the design of this feature.
2506
2507             Error_Msg_N ("?unrecognized pragma", Decl);
2508
2509             if not Is_Overriding_Operation (Subp) then
2510
2511                --  Before emitting an error message, check whether this
2512                --  may override an operation that is not yet visible, as
2513                --  in the case of a derivation of a private operation in
2514                --  a child unit. Such an operation is introduced with a
2515                --  different name, but its alias is the parent operation.
2516
2517                declare
2518                   E : Entity_Id;
2519
2520                begin
2521                   E := First_Entity (Current_Scope);
2522
2523                   while Present (E) loop
2524                      if Ekind (E) = Ekind (Subp)
2525                        and then not Comes_From_Source (E)
2526                        and then Present (Alias (E))
2527                        and then Chars (Alias (E)) = Chars (Subp)
2528                        and then In_Open_Scopes (Scope (Alias (E)))
2529                      then
2530                         exit;
2531                      else
2532                         Next_Entity (E);
2533                      end if;
2534                   end loop;
2535
2536                   if No (E) then
2537                      Error_Msg_NE
2538                        ("& must override an inherited operation",
2539                          Decl, Subp);
2540                   end if;
2541                end;
2542             end if;
2543
2544             --  Verify syntax of pragma
2545
2546             Arg1 := First (Pragma_Argument_Associations (Decl));
2547
2548             if Present (Arg1) then
2549                if not Is_Entity_Name (Expression (Arg1)) then
2550                   Error_Msg_N ("pragma applies to local subprogram", Decl);
2551
2552                elsif Chars (Expression (Arg1)) /= Chars (Subp) then
2553                   Error_Msg_N
2554                     ("pragma must apply to preceding subprogram", Decl);
2555
2556                elsif Present (Next (Arg1)) then
2557                   Error_Msg_N ("illegal pragma format", Decl);
2558                end if;
2559             end if;
2560
2561             Set_Analyzed (Decl);
2562             Has_Pragma := True;
2563             exit;
2564          end if;
2565
2566          Next (Decl);
2567       end loop;
2568
2569       if not Has_Pragma
2570         and then Explicit_Overriding
2571         and then Is_Overriding_Operation (Subp)
2572       then
2573          Error_Msg_NE ("Missing overriding pragma for&", Subp, Subp);
2574       end if;
2575    end Check_Overriding_Operation;
2576
2577    -------------------
2578    -- Check_Returns --
2579    -------------------
2580
2581    procedure Check_Returns
2582      (HSS  : Node_Id;
2583       Mode : Character;
2584       Err  : out Boolean)
2585    is
2586       Handler : Node_Id;
2587
2588       procedure Check_Statement_Sequence (L : List_Id);
2589       --  Internal recursive procedure to check a list of statements for proper
2590       --  termination by a return statement (or a transfer of control or a
2591       --  compound statement that is itself internally properly terminated).
2592
2593       ------------------------------
2594       -- Check_Statement_Sequence --
2595       ------------------------------
2596
2597       procedure Check_Statement_Sequence (L : List_Id) is
2598          Last_Stm : Node_Id;
2599          Kind     : Node_Kind;
2600
2601          Raise_Exception_Call : Boolean;
2602          --  Set True if statement sequence terminated by Raise_Exception call
2603          --  or a Reraise_Occurrence call.
2604
2605       begin
2606          Raise_Exception_Call := False;
2607
2608          --  Get last real statement
2609
2610          Last_Stm := Last (L);
2611
2612          --  Don't count pragmas
2613
2614          while Nkind (Last_Stm) = N_Pragma
2615
2616          --  Don't count call to SS_Release (can happen after Raise_Exception)
2617
2618            or else
2619              (Nkind (Last_Stm) = N_Procedure_Call_Statement
2620                 and then
2621               Nkind (Name (Last_Stm)) = N_Identifier
2622                 and then
2623               Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
2624
2625          --  Don't count exception junk
2626
2627            or else
2628              ((Nkind (Last_Stm) = N_Goto_Statement
2629                  or else Nkind (Last_Stm) = N_Label
2630                  or else Nkind (Last_Stm) = N_Object_Declaration)
2631                and then Exception_Junk (Last_Stm))
2632          loop
2633             Prev (Last_Stm);
2634          end loop;
2635
2636          --  Here we have the "real" last statement
2637
2638          Kind := Nkind (Last_Stm);
2639
2640          --  Transfer of control, OK. Note that in the No_Return procedure
2641          --  case, we already diagnosed any explicit return statements, so
2642          --  we can treat them as OK in this context.
2643
2644          if Is_Transfer (Last_Stm) then
2645             return;
2646
2647          --  Check cases of explicit non-indirect procedure calls
2648
2649          elsif Kind = N_Procedure_Call_Statement
2650            and then Is_Entity_Name (Name (Last_Stm))
2651          then
2652             --  Check call to Raise_Exception procedure which is treated
2653             --  specially, as is a call to Reraise_Occurrence.
2654
2655             --  We suppress the warning in these cases since it is likely that
2656             --  the programmer really does not expect to deal with the case
2657             --  of Null_Occurrence, and thus would find a warning about a
2658             --  missing return curious, and raising Program_Error does not
2659             --  seem such a bad behavior if this does occur.
2660
2661             if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
2662                  or else
2663                Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
2664             then
2665                Raise_Exception_Call := True;
2666
2667                --  For Raise_Exception call, test first argument, if it is
2668                --  an attribute reference for a 'Identity call, then we know
2669                --  that the call cannot possibly return.
2670
2671                declare
2672                   Arg : constant Node_Id :=
2673                           Original_Node (First_Actual (Last_Stm));
2674
2675                begin
2676                   if Nkind (Arg) = N_Attribute_Reference
2677                     and then Attribute_Name (Arg) = Name_Identity
2678                   then
2679                      return;
2680                   end if;
2681                end;
2682             end if;
2683
2684          --  If statement, need to look inside if there is an else and check
2685          --  each constituent statement sequence for proper termination.
2686
2687          elsif Kind = N_If_Statement
2688            and then Present (Else_Statements (Last_Stm))
2689          then
2690             Check_Statement_Sequence (Then_Statements (Last_Stm));
2691             Check_Statement_Sequence (Else_Statements (Last_Stm));
2692
2693             if Present (Elsif_Parts (Last_Stm)) then
2694                declare
2695                   Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
2696
2697                begin
2698                   while Present (Elsif_Part) loop
2699                      Check_Statement_Sequence (Then_Statements (Elsif_Part));
2700                      Next (Elsif_Part);
2701                   end loop;
2702                end;
2703             end if;
2704
2705             return;
2706
2707          --  Case statement, check each case for proper termination
2708
2709          elsif Kind = N_Case_Statement then
2710             declare
2711                Case_Alt : Node_Id;
2712
2713             begin
2714                Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
2715                while Present (Case_Alt) loop
2716                   Check_Statement_Sequence (Statements (Case_Alt));
2717                   Next_Non_Pragma (Case_Alt);
2718                end loop;
2719             end;
2720
2721             return;
2722
2723          --  Block statement, check its handled sequence of statements
2724
2725          elsif Kind = N_Block_Statement then
2726             declare
2727                Err1 : Boolean;
2728
2729             begin
2730                Check_Returns
2731                  (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
2732
2733                if Err1 then
2734                   Err := True;
2735                end if;
2736
2737                return;
2738             end;
2739
2740          --  Loop statement. If there is an iteration scheme, we can definitely
2741          --  fall out of the loop. Similarly if there is an exit statement, we
2742          --  can fall out. In either case we need a following return.
2743
2744          elsif Kind = N_Loop_Statement then
2745             if Present (Iteration_Scheme (Last_Stm))
2746               or else Has_Exit (Entity (Identifier (Last_Stm)))
2747             then
2748                null;
2749
2750             --  A loop with no exit statement or iteration scheme if either
2751             --  an inifite loop, or it has some other exit (raise/return).
2752             --  In either case, no warning is required.
2753
2754             else
2755                return;
2756             end if;
2757
2758          --  Timed entry call, check entry call and delay alternatives
2759
2760          --  Note: in expanded code, the timed entry call has been converted
2761          --  to a set of expanded statements on which the check will work
2762          --  correctly in any case.
2763
2764          elsif Kind = N_Timed_Entry_Call then
2765             declare
2766                ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2767                DCA : constant Node_Id := Delay_Alternative      (Last_Stm);
2768
2769             begin
2770                --  If statement sequence of entry call alternative is missing,
2771                --  then we can definitely fall through, and we post the error
2772                --  message on the entry call alternative itself.
2773
2774                if No (Statements (ECA)) then
2775                   Last_Stm := ECA;
2776
2777                --  If statement sequence of delay alternative is missing, then
2778                --  we can definitely fall through, and we post the error
2779                --  message on the delay alternative itself.
2780
2781                --  Note: if both ECA and DCA are missing the return, then we
2782                --  post only one message, should be enough to fix the bugs.
2783                --  If not we will get a message next time on the DCA when the
2784                --  ECA is fixed!
2785
2786                elsif No (Statements (DCA)) then
2787                   Last_Stm := DCA;
2788
2789                --  Else check both statement sequences
2790
2791                else
2792                   Check_Statement_Sequence (Statements (ECA));
2793                   Check_Statement_Sequence (Statements (DCA));
2794                   return;
2795                end if;
2796             end;
2797
2798          --  Conditional entry call, check entry call and else part
2799
2800          --  Note: in expanded code, the conditional entry call has been
2801          --  converted to a set of expanded statements on which the check
2802          --  will work correctly in any case.
2803
2804          elsif Kind = N_Conditional_Entry_Call then
2805             declare
2806                ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
2807
2808             begin
2809                --  If statement sequence of entry call alternative is missing,
2810                --  then we can definitely fall through, and we post the error
2811                --  message on the entry call alternative itself.
2812
2813                if No (Statements (ECA)) then
2814                   Last_Stm := ECA;
2815
2816                --  Else check statement sequence and else part
2817
2818                else
2819                   Check_Statement_Sequence (Statements (ECA));
2820                   Check_Statement_Sequence (Else_Statements (Last_Stm));
2821                   return;
2822                end if;
2823             end;
2824          end if;
2825
2826          --  If we fall through, issue appropriate message
2827
2828          if Mode = 'F' then
2829
2830             if not Raise_Exception_Call then
2831                Error_Msg_N
2832                  ("?RETURN statement missing following this statement!",
2833                   Last_Stm);
2834                Error_Msg_N
2835                  ("\?Program_Error may be raised at run time",
2836                   Last_Stm);
2837             end if;
2838
2839             --  Note: we set Err even though we have not issued a warning
2840             --  because we still have a case of a missing return. This is
2841             --  an extremely marginal case, probably will never be noticed
2842             --  but we might as well get it right.
2843
2844             Err := True;
2845
2846          else
2847             Error_Msg_N
2848               ("implied return after this statement not allowed (No_Return)",
2849                Last_Stm);
2850          end if;
2851       end Check_Statement_Sequence;
2852
2853    --  Start of processing for Check_Returns
2854
2855    begin
2856       Err := False;
2857       Check_Statement_Sequence (Statements (HSS));
2858
2859       if Present (Exception_Handlers (HSS)) then
2860          Handler := First_Non_Pragma (Exception_Handlers (HSS));
2861          while Present (Handler) loop
2862             Check_Statement_Sequence (Statements (Handler));
2863             Next_Non_Pragma (Handler);
2864          end loop;
2865       end if;
2866    end Check_Returns;
2867
2868    ----------------------------
2869    -- Check_Subprogram_Order --
2870    ----------------------------
2871
2872    procedure Check_Subprogram_Order (N : Node_Id) is
2873
2874       function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
2875       --  This is used to check if S1 > S2 in the sense required by this
2876       --  test, for example nameab < namec, but name2 < name10.
2877
2878       function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
2879          L1, L2 : Positive;
2880          N1, N2 : Natural;
2881
2882       begin
2883          --  Remove trailing numeric parts
2884
2885          L1 := S1'Last;
2886          while S1 (L1) in '0' .. '9' loop
2887             L1 := L1 - 1;
2888          end loop;
2889
2890          L2 := S2'Last;
2891          while S2 (L2) in '0' .. '9' loop
2892             L2 := L2 - 1;
2893          end loop;
2894
2895          --  If non-numeric parts non-equal, that's decisive
2896
2897          if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
2898             return False;
2899
2900          elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
2901             return True;
2902
2903          --  If non-numeric parts equal, compare suffixed numeric parts. Note
2904          --  that a missing suffix is treated as numeric zero in this test.
2905
2906          else
2907             N1 := 0;
2908             while L1 < S1'Last loop
2909                L1 := L1 + 1;
2910                N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
2911             end loop;
2912
2913             N2 := 0;
2914             while L2 < S2'Last loop
2915                L2 := L2 + 1;
2916                N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
2917             end loop;
2918
2919             return N1 > N2;
2920          end if;
2921       end Subprogram_Name_Greater;
2922
2923    --  Start of processing for Check_Subprogram_Order
2924
2925    begin
2926       --  Check body in alpha order if this is option
2927
2928       if Style_Check
2929         and then Style_Check_Subprogram_Order
2930         and then Nkind (N) = N_Subprogram_Body
2931         and then Comes_From_Source (N)
2932         and then In_Extended_Main_Source_Unit (N)
2933       then
2934          declare
2935             LSN : String_Ptr
2936                     renames Scope_Stack.Table
2937                               (Scope_Stack.Last).Last_Subprogram_Name;
2938
2939             Body_Id : constant Entity_Id :=
2940                         Defining_Entity (Specification (N));
2941
2942          begin
2943             Get_Decoded_Name_String (Chars (Body_Id));
2944
2945             if LSN /= null then
2946                if Subprogram_Name_Greater
2947                     (LSN.all, Name_Buffer (1 .. Name_Len))
2948                then
2949                   Style.Subprogram_Not_In_Alpha_Order (Body_Id);
2950                end if;
2951
2952                Free (LSN);
2953             end if;
2954
2955             LSN := new String'(Name_Buffer (1 .. Name_Len));
2956          end;
2957       end if;
2958    end Check_Subprogram_Order;
2959
2960    ------------------------------
2961    -- Check_Subtype_Conformant --
2962    ------------------------------
2963
2964    procedure Check_Subtype_Conformant
2965      (New_Id  : Entity_Id;
2966       Old_Id  : Entity_Id;
2967       Err_Loc : Node_Id := Empty)
2968    is
2969       Result : Boolean;
2970
2971    begin
2972       Check_Conformance
2973         (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
2974    end Check_Subtype_Conformant;
2975
2976    ---------------------------
2977    -- Check_Type_Conformant --
2978    ---------------------------
2979
2980    procedure Check_Type_Conformant
2981      (New_Id  : Entity_Id;
2982       Old_Id  : Entity_Id;
2983       Err_Loc : Node_Id := Empty)
2984    is
2985       Result : Boolean;
2986
2987    begin
2988       Check_Conformance
2989         (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
2990    end Check_Type_Conformant;
2991
2992    ----------------------
2993    -- Conforming_Types --
2994    ----------------------
2995
2996    function Conforming_Types
2997      (T1       : Entity_Id;
2998       T2       : Entity_Id;
2999       Ctype    : Conformance_Type;
3000       Get_Inst : Boolean := False) return Boolean
3001    is
3002       Type_1 : Entity_Id := T1;
3003       Type_2 : Entity_Id := T2;
3004       Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
3005
3006       function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
3007       --  If neither T1 nor T2 are generic actual types, or if they are
3008       --  in different scopes (e.g. parent and child instances), then verify
3009       --  that the base types are equal. Otherwise T1 and T2 must be
3010       --  on the same subtype chain. The whole purpose of this procedure
3011       --  is to prevent spurious ambiguities in an instantiation that may
3012       --  arise if two distinct generic types are instantiated with the
3013       --  same actual.
3014
3015       ----------------------
3016       -- Base_Types_Match --
3017       ----------------------
3018
3019       function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
3020       begin
3021          if T1 = T2 then
3022             return True;
3023
3024          elsif Base_Type (T1) = Base_Type (T2) then
3025
3026             --  The following is too permissive. A more precise test must
3027             --  check that the generic actual is an ancestor subtype of the
3028             --  other ???.
3029
3030             return not Is_Generic_Actual_Type (T1)
3031               or else not Is_Generic_Actual_Type (T2)
3032               or else Scope (T1) /= Scope (T2);
3033
3034          --  In some cases a type imported through a limited_with clause,
3035          --  and its non-limited view are both visible, for example in an
3036          --  anonymous access_to_classwide type in a formal. Both entities
3037          --  designate the same type.
3038
3039          elsif From_With_Type (T1)
3040            and then Ekind (T1) = E_Incomplete_Type
3041            and then T2 = Non_Limited_View (T1)
3042          then
3043             return True;
3044
3045          else
3046             return False;
3047          end if;
3048       end Base_Types_Match;
3049
3050    begin
3051       --  The context is an instance association for a formal
3052       --  access-to-subprogram type; the formal parameter types
3053       --  require mapping because they may denote other formal
3054       --  parameters of the generic unit.
3055
3056       if Get_Inst then
3057          Type_1 := Get_Instance_Of (T1);
3058          Type_2 := Get_Instance_Of (T2);
3059       end if;
3060
3061       --  First see if base types match
3062
3063       if Base_Types_Match (Type_1, Type_2) then
3064          return Ctype <= Mode_Conformant
3065            or else Subtypes_Statically_Match (Type_1, Type_2);
3066
3067       elsif Is_Incomplete_Or_Private_Type (Type_1)
3068         and then Present (Full_View (Type_1))
3069         and then Base_Types_Match (Full_View (Type_1), Type_2)
3070       then
3071          return Ctype <= Mode_Conformant
3072            or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
3073
3074       elsif Ekind (Type_2) = E_Incomplete_Type
3075         and then Present (Full_View (Type_2))
3076         and then Base_Types_Match (Type_1, Full_View (Type_2))
3077       then
3078          return Ctype <= Mode_Conformant
3079            or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3080
3081       elsif Is_Private_Type (Type_2)
3082         and then In_Instance
3083         and then Present (Full_View (Type_2))
3084         and then Base_Types_Match (Type_1, Full_View (Type_2))
3085       then
3086          return Ctype <= Mode_Conformant
3087            or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
3088       end if;
3089
3090       --  Ada 2005 (AI-254): Detect anonymous access to subprogram types
3091
3092       Are_Anonymous_Access_To_Subprogram_Types :=
3093
3094          --  Case 1: Anonymous access to subprogram types
3095
3096         (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
3097            and then Ekind (Type_2) = E_Anonymous_Access_Subprogram_Type)
3098
3099          --  Case 2: Anonymous access to PROTECTED subprogram types. In this
3100          --  case the anonymous type_declaration has been replaced by an
3101          --  occurrence of an internal access to subprogram type declaration
3102          --  available through the Original_Access_Type attribute
3103
3104         or else
3105           (Ekind (Type_1) = E_Access_Protected_Subprogram_Type
3106             and then Ekind (Type_2) = E_Access_Protected_Subprogram_Type
3107             and then not Comes_From_Source (Type_1)
3108             and then not Comes_From_Source (Type_2)
3109             and then Present (Original_Access_Type (Type_1))
3110             and then Present (Original_Access_Type (Type_2))
3111             and then Ekind (Original_Access_Type (Type_1)) =
3112                        E_Anonymous_Access_Protected_Subprogram_Type
3113             and then Ekind (Original_Access_Type (Type_2)) =
3114                        E_Anonymous_Access_Protected_Subprogram_Type);
3115
3116       --  Test anonymous access type case. For this case, static subtype
3117       --  matching is required for mode conformance (RM 6.3.1(15))
3118
3119       if (Ekind (Type_1) = E_Anonymous_Access_Type
3120             and then Ekind (Type_2) = E_Anonymous_Access_Type)
3121         or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
3122       then
3123          declare
3124             Desig_1 : Entity_Id;
3125             Desig_2 : Entity_Id;
3126
3127          begin
3128             Desig_1 := Directly_Designated_Type (Type_1);
3129
3130             --  An access parameter can designate an incomplete type
3131
3132             if Ekind (Desig_1) = E_Incomplete_Type
3133               and then Present (Full_View (Desig_1))
3134             then
3135                Desig_1 := Full_View (Desig_1);
3136             end if;
3137
3138             Desig_2 := Directly_Designated_Type (Type_2);
3139
3140             if Ekind (Desig_2) = E_Incomplete_Type
3141               and then Present (Full_View (Desig_2))
3142             then
3143                Desig_2 := Full_View (Desig_2);
3144             end if;
3145
3146             --  The context is an instance association for a formal
3147             --  access-to-subprogram type; formal access parameter
3148             --  designated types require mapping because they may
3149             --  denote other formal parameters of the generic unit.
3150
3151             if Get_Inst then
3152                Desig_1 := Get_Instance_Of (Desig_1);
3153                Desig_2 := Get_Instance_Of (Desig_2);
3154             end if;
3155
3156             --  It is possible for a Class_Wide_Type to be introduced for
3157             --  an incomplete type, in which case there is a separate class_
3158             --  wide type for the full view. The types conform if their
3159             --  Etypes conform, i.e. one may be the full view of the other.
3160             --  This can only happen in the context of an access parameter,
3161             --  other uses of an incomplete Class_Wide_Type are illegal.
3162
3163             if Is_Class_Wide_Type (Desig_1)
3164               and then Is_Class_Wide_Type (Desig_2)
3165             then
3166                return
3167                  Conforming_Types
3168                    (Etype (Base_Type (Desig_1)),
3169                     Etype (Base_Type (Desig_2)), Ctype);
3170
3171             elsif Are_Anonymous_Access_To_Subprogram_Types then
3172                return Ctype = Type_Conformant
3173                         or else
3174                       Subtypes_Statically_Match (Desig_1, Desig_2);
3175
3176             else
3177                return Base_Type (Desig_1) = Base_Type (Desig_2)
3178                 and then (Ctype = Type_Conformant
3179                             or else
3180                           Subtypes_Statically_Match (Desig_1, Desig_2));
3181             end if;
3182          end;
3183
3184       --  Otherwise definitely no match
3185
3186       else
3187          return False;
3188       end if;
3189
3190    end Conforming_Types;
3191
3192    --------------------------
3193    -- Create_Extra_Formals --
3194    --------------------------
3195
3196    procedure Create_Extra_Formals (E : Entity_Id) is
3197       Formal      : Entity_Id;
3198       Last_Extra  : Entity_Id;
3199       Formal_Type : Entity_Id;
3200       P_Formal    : Entity_Id := Empty;
3201
3202       function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
3203       --  Add an extra formal, associated with the current Formal. The
3204       --  extra formal is added to the list of extra formals, and also
3205       --  returned as the result. These formals are always of mode IN.
3206
3207       ----------------------
3208       -- Add_Extra_Formal --
3209       ----------------------
3210
3211       function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
3212          EF : constant Entity_Id :=
3213                 Make_Defining_Identifier (Sloc (Formal),
3214                   Chars => New_External_Name (Chars (Formal), 'F'));
3215
3216       begin
3217          --  We never generate extra formals if expansion is not active
3218          --  because we don't need them unless we are generating code.
3219
3220          if not Expander_Active then
3221             return Empty;
3222          end if;
3223
3224          --  A little optimization. Never generate an extra formal for
3225          --  the _init operand of an initialization procedure, since it
3226          --  could never be used.
3227
3228          if Chars (Formal) = Name_uInit then
3229             return Empty;
3230          end if;
3231
3232          Set_Ekind           (EF, E_In_Parameter);
3233          Set_Actual_Subtype  (EF, Typ);
3234          Set_Etype           (EF, Typ);
3235          Set_Scope           (EF, Scope (Formal));
3236          Set_Mechanism       (EF, Default_Mechanism);
3237          Set_Formal_Validity (EF);
3238
3239          Set_Extra_Formal (Last_Extra, EF);
3240          Last_Extra := EF;
3241          return EF;
3242       end Add_Extra_Formal;
3243
3244    --  Start of processing for Create_Extra_Formals
3245
3246    begin
3247       --  If this is a derived subprogram then the subtypes of the
3248       --  parent subprogram's formal parameters will be used to
3249       --  to determine the need for extra formals.
3250
3251       if Is_Overloadable (E) and then Present (Alias (E)) then
3252          P_Formal := First_Formal (Alias (E));
3253       end if;
3254
3255       Last_Extra := Empty;
3256       Formal := First_Formal (E);
3257       while Present (Formal) loop
3258          Last_Extra := Formal;
3259          Next_Formal (Formal);
3260       end loop;
3261
3262       --  If Extra_formals where already created, don't do it again
3263       --  This situation may arise for subprogram types created as part
3264       --  of dispatching calls (see Expand_Dispatch_Call)
3265
3266       if Present (Last_Extra) and then
3267         Present (Extra_Formal (Last_Extra))
3268       then
3269          return;
3270       end if;
3271
3272       Formal := First_Formal (E);
3273
3274       while Present (Formal) loop
3275
3276          --  Create extra formal for supporting the attribute 'Constrained.
3277          --  The case of a private type view without discriminants also
3278          --  requires the extra formal if the underlying type has defaulted
3279          --  discriminants.
3280
3281          if Ekind (Formal) /= E_In_Parameter then
3282             if Present (P_Formal) then
3283                Formal_Type := Etype (P_Formal);
3284             else
3285                Formal_Type := Etype (Formal);
3286             end if;
3287
3288             --  Do not produce extra formals for Unchecked_Union parameters.
3289             --  Jump directly to the end of the loop.
3290
3291             if Is_Unchecked_Union (Base_Type (Formal_Type)) then
3292                goto Skip_Extra_Formal_Generation;
3293             end if;
3294
3295             if not Has_Discriminants (Formal_Type)
3296               and then Ekind (Formal_Type) in Private_Kind
3297               and then Present (Underlying_Type (Formal_Type))
3298             then
3299                Formal_Type := Underlying_Type (Formal_Type);
3300             end if;
3301
3302             if Has_Discriminants (Formal_Type)
3303               and then
3304                 ((not Is_Constrained (Formal_Type)
3305                     and then not Is_Indefinite_Subtype (Formal_Type))
3306                   or else Present (Extra_Formal (Formal)))
3307             then
3308                Set_Extra_Constrained
3309                  (Formal, Add_Extra_Formal (Standard_Boolean));
3310             end if;
3311          end if;
3312
3313          --  Create extra formal for supporting accessibility checking
3314
3315          --  This is suppressed if we specifically suppress accessibility
3316          --  checks at the pacage level for either the subprogram, or the
3317          --  package in which it resides. However, we do not suppress it
3318          --  simply if the scope has accessibility checks suppressed, since
3319          --  this could cause trouble when clients are compiled with a
3320          --  different suppression setting. The explicit checks at the
3321          --  package level are safe from this point of view.
3322
3323          if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3324            and then not
3325              (Explicit_Suppress (E, Accessibility_Check)
3326                or else
3327               Explicit_Suppress (Scope (E), Accessibility_Check))
3328            and then
3329              (not Present (P_Formal)
3330                or else Present (Extra_Accessibility (P_Formal)))
3331          then
3332             --  Temporary kludge: for now we avoid creating the extra
3333             --  formal for access parameters of protected operations
3334             --  because of problem with the case of internal protected
3335             --  calls. ???
3336
3337             if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
3338               and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
3339             then
3340                Set_Extra_Accessibility
3341                  (Formal, Add_Extra_Formal (Standard_Natural));
3342             end if;
3343          end if;
3344
3345          if Present (P_Formal) then
3346             Next_Formal (P_Formal);
3347          end if;
3348
3349          --  This label is required when skipping extra formal generation for
3350          --  Unchecked_Union parameters.
3351
3352          <<Skip_Extra_Formal_Generation>>
3353
3354          Next_Formal (Formal);
3355       end loop;
3356    end Create_Extra_Formals;
3357
3358    -----------------------------
3359    -- Enter_Overloaded_Entity --
3360    -----------------------------
3361
3362    procedure Enter_Overloaded_Entity (S : Entity_Id) is
3363       E   : Entity_Id := Current_Entity_In_Scope (S);
3364       C_E : Entity_Id := Current_Entity (S);
3365
3366    begin
3367       if Present (E) then
3368          Set_Has_Homonym (E);
3369          Set_Has_Homonym (S);
3370       end if;
3371
3372       Set_Is_Immediately_Visible (S);
3373       Set_Scope (S, Current_Scope);
3374
3375       --  Chain new entity if front of homonym in current scope, so that
3376       --  homonyms are contiguous.
3377
3378       if Present (E)
3379         and then E /= C_E
3380       then
3381          while Homonym (C_E) /= E loop
3382             C_E := Homonym (C_E);
3383          end loop;
3384
3385          Set_Homonym (C_E, S);
3386
3387       else
3388          E := C_E;
3389          Set_Current_Entity (S);
3390       end if;
3391
3392       Set_Homonym (S, E);
3393
3394       Append_Entity (S, Current_Scope);
3395       Set_Public_Status (S);
3396
3397       if Debug_Flag_E then
3398          Write_Str ("New overloaded entity chain: ");
3399          Write_Name (Chars (S));
3400          E := S;
3401
3402          while Present (E) loop
3403             Write_Str (" "); Write_Int (Int (E));
3404             E := Homonym (E);
3405          end loop;
3406
3407          Write_Eol;
3408       end if;
3409
3410       --  Generate warning for hiding
3411
3412       if Warn_On_Hiding
3413         and then Comes_From_Source (S)
3414         and then In_Extended_Main_Source_Unit (S)
3415       then
3416          E := S;
3417          loop
3418             E := Homonym (E);
3419             exit when No (E);
3420
3421             --  Warn unless genuine overloading
3422
3423             if (not Is_Overloadable (E))
3424               or else Subtype_Conformant (E, S)
3425             then
3426                Error_Msg_Sloc := Sloc (E);
3427                Error_Msg_N ("declaration of & hides one#?", S);
3428             end if;
3429          end loop;
3430       end if;
3431    end Enter_Overloaded_Entity;
3432
3433    -----------------------------
3434    -- Find_Corresponding_Spec --
3435    -----------------------------
3436
3437    function Find_Corresponding_Spec (N : Node_Id) return Entity_Id is
3438       Spec       : constant Node_Id   := Specification (N);
3439       Designator : constant Entity_Id := Defining_Entity (Spec);
3440
3441       E : Entity_Id;
3442
3443    begin
3444       E := Current_Entity (Designator);
3445
3446       while Present (E) loop
3447
3448          --  We are looking for a matching spec. It must have the same scope,
3449          --  and the same name, and either be type conformant, or be the case
3450          --  of a library procedure spec and its body (which belong to one
3451          --  another regardless of whether they are type conformant or not).
3452
3453          if Scope (E) = Current_Scope then
3454             if Current_Scope = Standard_Standard
3455               or else (Ekind (E) = Ekind (Designator)
3456                          and then Type_Conformant (E, Designator))
3457             then
3458                --  Within an instantiation, we know that spec and body are
3459                --  subtype conformant, because they were subtype conformant
3460                --  in the generic. We choose the subtype-conformant entity
3461                --  here as well, to resolve spurious ambiguities in the
3462                --  instance that were not present in the generic (i.e. when
3463                --  two different types are given the same actual). If we are
3464                --  looking for a spec to match a body, full conformance is
3465                --  expected.
3466
3467                if In_Instance then
3468                   Set_Convention (Designator, Convention (E));
3469
3470                   if Nkind (N) = N_Subprogram_Body
3471                     and then Present (Homonym (E))
3472                     and then not Fully_Conformant (E, Designator)
3473                   then
3474                      goto Next_Entity;
3475
3476                   elsif not Subtype_Conformant (E, Designator) then
3477                      goto Next_Entity;
3478                   end if;
3479                end if;
3480
3481                if not Has_Completion (E) then
3482
3483                   if Nkind (N) /= N_Subprogram_Body_Stub then
3484                      Set_Corresponding_Spec (N, E);
3485                   end if;
3486
3487                   Set_Has_Completion (E);
3488                   return E;
3489
3490                elsif Nkind (Parent (N)) = N_Subunit then
3491
3492                   --  If this is the proper body of a subunit, the completion
3493                   --  flag is set when analyzing the stub.
3494
3495                   return E;
3496
3497                --  If body already exists, this is an error unless the
3498                --  previous declaration is the implicit declaration of
3499                --  a derived subprogram, or this is a spurious overloading
3500                --  in an instance.
3501
3502                elsif No (Alias (E))
3503                  and then not Is_Intrinsic_Subprogram (E)
3504                  and then not In_Instance
3505                then
3506                   Error_Msg_Sloc := Sloc (E);
3507                   if Is_Imported (E) then
3508                      Error_Msg_NE
3509                       ("body not allowed for imported subprogram & declared#",
3510                         N, E);
3511                   else
3512                      Error_Msg_NE ("duplicate body for & declared#", N, E);
3513                   end if;
3514                end if;
3515
3516             elsif Is_Child_Unit (E)
3517               and then
3518                 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
3519               and then
3520                 Nkind (Parent (Unit_Declaration_Node (Designator)))
3521                   = N_Compilation_Unit
3522             then
3523
3524                --  Child units cannot be overloaded, so a conformance mismatch
3525                --  between body and a previous spec is an error.
3526
3527                Error_Msg_N
3528                  ("body of child unit does not match previous declaration", N);
3529             end if;
3530          end if;
3531
3532          <<Next_Entity>>
3533             E := Homonym (E);
3534       end loop;
3535
3536       --  On exit, we know that no previous declaration of subprogram exists
3537
3538       return Empty;
3539    end Find_Corresponding_Spec;
3540
3541    ----------------------
3542    -- Fully_Conformant --
3543    ----------------------
3544
3545    function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
3546       Result : Boolean;
3547
3548    begin
3549       Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
3550       return Result;
3551    end Fully_Conformant;
3552
3553    ----------------------------------
3554    -- Fully_Conformant_Expressions --
3555    ----------------------------------
3556
3557    function Fully_Conformant_Expressions
3558      (Given_E1 : Node_Id;
3559       Given_E2 : Node_Id) return Boolean
3560    is
3561       E1 : constant Node_Id := Original_Node (Given_E1);
3562       E2 : constant Node_Id := Original_Node (Given_E2);
3563       --  We always test conformance on original nodes, since it is possible
3564       --  for analysis and/or expansion to make things look as though they
3565       --  conform when they do not, e.g. by converting 1+2 into 3.
3566
3567       function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
3568         renames Fully_Conformant_Expressions;
3569
3570       function FCL (L1, L2 : List_Id) return Boolean;
3571       --  Compare elements of two lists for conformance. Elements have to
3572       --  be conformant, and actuals inserted as default parameters do not
3573       --  match explicit actuals with the same value.
3574
3575       function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
3576       --  Compare an operator node with a function call.
3577
3578       ---------
3579       -- FCL --
3580       ---------
3581
3582       function FCL (L1, L2 : List_Id) return Boolean is
3583          N1, N2 : Node_Id;
3584
3585       begin
3586          if L1 = No_List then
3587             N1 := Empty;
3588          else
3589             N1 := First (L1);
3590          end if;
3591
3592          if L2 = No_List then
3593             N2 := Empty;
3594          else
3595             N2 := First (L2);
3596          end if;
3597
3598          --  Compare two lists, skipping rewrite insertions (we want to
3599          --  compare the original trees, not the expanded versions!)
3600
3601          loop
3602             if Is_Rewrite_Insertion (N1) then
3603                Next (N1);
3604             elsif Is_Rewrite_Insertion (N2) then
3605                Next (N2);
3606             elsif No (N1) then
3607                return No (N2);
3608             elsif No (N2) then
3609                return False;
3610             elsif not FCE (N1, N2) then
3611                return False;
3612             else
3613                Next (N1);
3614                Next (N2);
3615             end if;
3616          end loop;
3617       end FCL;
3618
3619       ---------
3620       -- FCO --
3621       ---------
3622
3623       function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
3624          Actuals : constant List_Id := Parameter_Associations (Call_Node);
3625          Act     : Node_Id;
3626
3627       begin
3628          if No (Actuals)
3629             or else Entity (Op_Node) /= Entity (Name (Call_Node))
3630          then
3631             return False;
3632
3633          else
3634             Act := First (Actuals);
3635
3636             if Nkind (Op_Node) in N_Binary_Op then
3637
3638                if not FCE (Left_Opnd (Op_Node), Act) then
3639                   return False;
3640                end if;
3641
3642                Next (Act);
3643             end if;
3644
3645             return Present (Act)
3646               and then FCE (Right_Opnd (Op_Node), Act)
3647               and then No (Next (Act));
3648          end if;
3649       end FCO;
3650
3651    --  Start of processing for Fully_Conformant_Expressions
3652
3653    begin
3654       --  Non-conformant if paren count does not match. Note: if some idiot
3655       --  complains that we don't do this right for more than 3 levels of
3656       --  parentheses, they will be treated with the respect they deserve :-)
3657
3658       if Paren_Count (E1) /= Paren_Count (E2) then
3659          return False;
3660
3661       --  If same entities are referenced, then they are conformant
3662       --  even if they have different forms (RM 8.3.1(19-20)).
3663
3664       elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
3665          if Present (Entity (E1)) then
3666             return Entity (E1) = Entity (E2)
3667               or else (Chars (Entity (E1)) = Chars (Entity (E2))
3668                         and then Ekind (Entity (E1)) = E_Discriminant
3669                         and then Ekind (Entity (E2)) = E_In_Parameter);
3670
3671          elsif Nkind (E1) = N_Expanded_Name
3672            and then Nkind (E2) = N_Expanded_Name
3673            and then Nkind (Selector_Name (E1)) = N_Character_Literal
3674            and then Nkind (Selector_Name (E2)) = N_Character_Literal
3675          then
3676             return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
3677
3678          else
3679             --  Identifiers in component associations don't always have
3680             --  entities, but their names must conform.
3681
3682             return Nkind  (E1) = N_Identifier
3683               and then Nkind (E2) = N_Identifier
3684               and then Chars (E1) = Chars (E2);
3685          end if;
3686
3687       elsif Nkind (E1) = N_Character_Literal
3688         and then Nkind (E2) = N_Expanded_Name
3689       then
3690          return Nkind (Selector_Name (E2)) = N_Character_Literal
3691            and then Chars (E1) = Chars (Selector_Name (E2));
3692
3693       elsif Nkind (E2) = N_Character_Literal
3694         and then Nkind (E1) = N_Expanded_Name
3695       then
3696          return Nkind (Selector_Name (E1)) = N_Character_Literal
3697            and then Chars (E2) = Chars (Selector_Name (E1));
3698
3699       elsif Nkind (E1) in N_Op
3700         and then Nkind (E2) = N_Function_Call
3701       then
3702          return FCO (E1, E2);
3703
3704       elsif Nkind (E2) in N_Op
3705         and then Nkind (E1) = N_Function_Call
3706       then
3707          return FCO (E2, E1);
3708
3709       --  Otherwise we must have the same syntactic entity
3710
3711       elsif Nkind (E1) /= Nkind (E2) then
3712          return False;
3713
3714       --  At this point, we specialize by node type
3715
3716       else
3717          case Nkind (E1) is
3718
3719             when N_Aggregate =>
3720                return
3721                  FCL (Expressions (E1), Expressions (E2))
3722                    and then FCL (Component_Associations (E1),
3723                                  Component_Associations (E2));
3724
3725             when N_Allocator =>
3726                if Nkind (Expression (E1)) = N_Qualified_Expression
3727                     or else
3728                   Nkind (Expression (E2)) = N_Qualified_Expression
3729                then
3730                   return FCE (Expression (E1), Expression (E2));
3731
3732                --  Check that the subtype marks and any constraints
3733                --  are conformant
3734
3735                else
3736                   declare
3737                      Indic1 : constant Node_Id := Expression (E1);
3738                      Indic2 : constant Node_Id := Expression (E2);
3739                      Elt1   : Node_Id;
3740                      Elt2   : Node_Id;
3741
3742                   begin
3743                      if Nkind (Indic1) /= N_Subtype_Indication then
3744                         return
3745                           Nkind (Indic2) /= N_Subtype_Indication
3746                             and then Entity (Indic1) = Entity (Indic2);
3747
3748                      elsif Nkind (Indic2) /= N_Subtype_Indication then
3749                         return
3750                           Nkind (Indic1) /= N_Subtype_Indication
3751                             and then Entity (Indic1) = Entity (Indic2);
3752
3753                      else
3754                         if Entity (Subtype_Mark (Indic1)) /=
3755                           Entity (Subtype_Mark (Indic2))
3756                         then
3757                            return False;
3758                         end if;
3759
3760                         Elt1 := First (Constraints (Constraint (Indic1)));
3761                         Elt2 := First (Constraints (Constraint (Indic2)));
3762
3763                         while Present (Elt1) and then Present (Elt2) loop
3764                            if not FCE (Elt1, Elt2) then
3765                               return False;
3766                            end if;
3767
3768                            Next (Elt1);
3769                            Next (Elt2);
3770                         end loop;
3771
3772                         return True;
3773                      end if;
3774                   end;
3775                end if;
3776
3777             when N_Attribute_Reference =>
3778                return
3779                  Attribute_Name (E1) = Attribute_Name (E2)
3780                    and then FCL (Expressions (E1), Expressions (E2));
3781
3782             when N_Binary_Op =>
3783                return
3784                  Entity (E1) = Entity (E2)
3785                    and then FCE (Left_Opnd  (E1), Left_Opnd  (E2))
3786                    and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3787
3788             when N_And_Then | N_Or_Else | N_In | N_Not_In =>
3789                return
3790                  FCE (Left_Opnd  (E1), Left_Opnd  (E2))
3791                    and then
3792                  FCE (Right_Opnd (E1), Right_Opnd (E2));
3793
3794             when N_Character_Literal =>
3795                return
3796                  Char_Literal_Value (E1) = Char_Literal_Value (E2);
3797
3798             when N_Component_Association =>
3799                return
3800                  FCL (Choices (E1), Choices (E2))
3801                    and then FCE (Expression (E1), Expression (E2));
3802
3803             when N_Conditional_Expression =>
3804                return
3805                  FCL (Expressions (E1), Expressions (E2));
3806
3807             when N_Explicit_Dereference =>
3808                return
3809                  FCE (Prefix (E1), Prefix (E2));
3810
3811             when N_Extension_Aggregate =>
3812                return
3813                  FCL (Expressions (E1), Expressions (E2))
3814                    and then Null_Record_Present (E1) =
3815                             Null_Record_Present (E2)
3816                    and then FCL (Component_Associations (E1),
3817                                Component_Associations (E2));
3818
3819             when N_Function_Call =>
3820                return
3821                  FCE (Name (E1), Name (E2))
3822                    and then FCL (Parameter_Associations (E1),
3823                                  Parameter_Associations (E2));
3824
3825             when N_Indexed_Component =>
3826                return
3827                  FCE (Prefix (E1), Prefix (E2))
3828                    and then FCL (Expressions (E1), Expressions (E2));
3829
3830             when N_Integer_Literal =>
3831                return (Intval (E1) = Intval (E2));
3832
3833             when N_Null =>
3834                return True;
3835
3836             when N_Operator_Symbol =>
3837                return
3838                  Chars (E1) = Chars (E2);
3839
3840             when N_Others_Choice =>
3841                return True;
3842
3843             when N_Parameter_Association =>
3844                return
3845                  Chars (Selector_Name (E1))  = Chars (Selector_Name (E2))
3846                    and then FCE (Explicit_Actual_Parameter (E1),
3847                                  Explicit_Actual_Parameter (E2));
3848
3849             when N_Qualified_Expression =>
3850                return
3851                  FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3852                    and then FCE (Expression (E1), Expression (E2));
3853
3854             when N_Range =>
3855                return
3856                  FCE (Low_Bound (E1), Low_Bound (E2))
3857                    and then FCE (High_Bound (E1), High_Bound (E2));
3858
3859             when N_Real_Literal =>
3860                return (Realval (E1) = Realval (E2));
3861
3862             when N_Selected_Component =>
3863                return
3864                  FCE (Prefix (E1), Prefix (E2))
3865                    and then FCE (Selector_Name (E1), Selector_Name (E2));
3866
3867             when N_Slice =>
3868                return
3869                  FCE (Prefix (E1), Prefix (E2))
3870                    and then FCE (Discrete_Range (E1), Discrete_Range (E2));
3871
3872             when N_String_Literal =>
3873                declare
3874                   S1 : constant String_Id := Strval (E1);
3875                   S2 : constant String_Id := Strval (E2);
3876                   L1 : constant Nat       := String_Length (S1);
3877                   L2 : constant Nat       := String_Length (S2);
3878
3879                begin
3880                   if L1 /= L2 then
3881                      return False;
3882
3883                   else
3884                      for J in 1 .. L1 loop
3885                         if Get_String_Char (S1, J) /=
3886                            Get_String_Char (S2, J)
3887                         then
3888                            return False;
3889                         end if;
3890                      end loop;
3891
3892                      return True;
3893                   end if;
3894                end;
3895
3896             when N_Type_Conversion =>
3897                return
3898                  FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3899                    and then FCE (Expression (E1), Expression (E2));
3900
3901             when N_Unary_Op =>
3902                return
3903                  Entity (E1) = Entity (E2)
3904                    and then FCE (Right_Opnd (E1), Right_Opnd (E2));
3905
3906             when N_Unchecked_Type_Conversion =>
3907                return
3908                  FCE (Subtype_Mark (E1), Subtype_Mark (E2))
3909                    and then FCE (Expression (E1), Expression (E2));
3910
3911             --  All other node types cannot appear in this context. Strictly
3912             --  we should raise a fatal internal error. Instead we just ignore
3913             --  the nodes. This means that if anyone makes a mistake in the
3914             --  expander and mucks an expression tree irretrievably, the
3915             --  result will be a failure to detect a (probably very obscure)
3916             --  case of non-conformance, which is better than bombing on some
3917             --  case where two expressions do in fact conform.
3918
3919             when others =>
3920                return True;
3921
3922          end case;
3923       end if;
3924    end Fully_Conformant_Expressions;
3925
3926    ----------------------------------------
3927    -- Fully_Conformant_Discrete_Subtypes --
3928    ----------------------------------------
3929
3930    function Fully_Conformant_Discrete_Subtypes
3931      (Given_S1 : Node_Id;
3932       Given_S2 : Node_Id) return Boolean
3933    is
3934       S1 : constant Node_Id := Original_Node (Given_S1);
3935       S2 : constant Node_Id := Original_Node (Given_S2);
3936
3937       function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
3938       --  Special-case for a bound given by a discriminant, which in the
3939       --  body is replaced with the discriminal of the enclosing type.
3940
3941       function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
3942       --  Check both bounds.
3943
3944       function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
3945       begin
3946          if Is_Entity_Name (B1)
3947            and then Is_Entity_Name (B2)
3948            and then Ekind (Entity (B1)) = E_Discriminant
3949          then
3950             return Chars (B1) = Chars (B2);
3951
3952          else
3953             return Fully_Conformant_Expressions (B1, B2);
3954          end if;
3955       end Conforming_Bounds;
3956
3957       function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
3958       begin
3959          return
3960            Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
3961              and then
3962            Conforming_Bounds (High_Bound (R1), High_Bound (R2));
3963       end Conforming_Ranges;
3964
3965    --  Start of processing for Fully_Conformant_Discrete_Subtypes
3966
3967    begin
3968       if Nkind (S1) /= Nkind (S2) then
3969          return False;
3970
3971       elsif Is_Entity_Name (S1) then
3972          return Entity (S1) = Entity (S2);
3973
3974       elsif Nkind (S1) = N_Range then
3975          return Conforming_Ranges (S1, S2);
3976
3977       elsif Nkind (S1) = N_Subtype_Indication then
3978          return
3979             Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
3980               and then
3981             Conforming_Ranges
3982               (Range_Expression (Constraint (S1)),
3983                Range_Expression (Constraint (S2)));
3984       else
3985          return True;
3986       end if;
3987    end Fully_Conformant_Discrete_Subtypes;
3988
3989    --------------------
3990    -- Install_Entity --
3991    --------------------
3992
3993    procedure Install_Entity (E : Entity_Id) is
3994       Prev : constant Entity_Id := Current_Entity (E);
3995
3996    begin
3997       Set_Is_Immediately_Visible (E);
3998       Set_Current_Entity (E);
3999       Set_Homonym (E, Prev);
4000    end Install_Entity;
4001
4002    ---------------------
4003    -- Install_Formals --
4004    ---------------------
4005
4006    procedure Install_Formals (Id : Entity_Id) is
4007       F : Entity_Id;
4008
4009    begin
4010       F := First_Formal (Id);
4011
4012       while Present (F) loop
4013          Install_Entity (F);
4014          Next_Formal (F);
4015       end loop;
4016    end Install_Formals;
4017
4018    ---------------------------------
4019    -- Is_Non_Overriding_Operation --
4020    ---------------------------------
4021
4022    function Is_Non_Overriding_Operation
4023      (Prev_E : Entity_Id;
4024       New_E  : Entity_Id) return Boolean
4025    is
4026       Formal : Entity_Id;
4027       F_Typ  : Entity_Id;
4028       G_Typ  : Entity_Id := Empty;
4029
4030       function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
4031       --  If F_Type is a derived type associated with a generic actual
4032       --  subtype, then return its Generic_Parent_Type attribute, else
4033       --  return Empty.
4034
4035       function Types_Correspond
4036         (P_Type : Entity_Id;
4037          N_Type : Entity_Id) return Boolean;
4038       --  Returns true if and only if the types (or designated types
4039       --  in the case of anonymous access types) are the same or N_Type
4040       --  is derived directly or indirectly from P_Type.
4041
4042       -----------------------------
4043       -- Get_Generic_Parent_Type --
4044       -----------------------------
4045
4046       function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
4047          G_Typ : Entity_Id;
4048          Indic : Node_Id;
4049
4050       begin
4051          if Is_Derived_Type (F_Typ)
4052            and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
4053          then
4054             --  The tree must be traversed to determine the parent
4055             --  subtype in the generic unit, which unfortunately isn't
4056             --  always available via semantic attributes. ???
4057             --  (Note: The use of Original_Node is needed for cases
4058             --  where a full derived type has been rewritten.)
4059
4060             Indic := Subtype_Indication
4061                        (Type_Definition (Original_Node (Parent (F_Typ))));
4062
4063             if Nkind (Indic) = N_Subtype_Indication then
4064                G_Typ := Entity (Subtype_Mark (Indic));
4065             else
4066                G_Typ := Entity (Indic);
4067             end if;
4068
4069             if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
4070               and then Present (Generic_Parent_Type (Parent (G_Typ)))
4071             then
4072                return Generic_Parent_Type (Parent (G_Typ));
4073             end if;
4074          end if;
4075
4076          return Empty;
4077       end Get_Generic_Parent_Type;
4078
4079       ----------------------
4080       -- Types_Correspond --
4081       ----------------------
4082
4083       function Types_Correspond
4084         (P_Type : Entity_Id;
4085          N_Type : Entity_Id) return Boolean
4086       is
4087          Prev_Type : Entity_Id := Base_Type (P_Type);
4088          New_Type  : Entity_Id := Base_Type (N_Type);
4089
4090       begin
4091          if Ekind (Prev_Type) = E_Anonymous_Access_Type then
4092             Prev_Type := Designated_Type (Prev_Type);
4093          end if;
4094
4095          if Ekind (New_Type) = E_Anonymous_Access_Type then
4096             New_Type := Designated_Type (New_Type);
4097          end if;
4098
4099          if Prev_Type = New_Type then
4100             return True;
4101
4102          elsif not Is_Class_Wide_Type (New_Type) then
4103             while Etype (New_Type) /= New_Type loop
4104                New_Type := Etype (New_Type);
4105                if New_Type = Prev_Type then
4106                   return True;
4107                end if;
4108             end loop;
4109          end if;
4110          return False;
4111       end Types_Correspond;
4112
4113    --  Start of processing for Is_Non_Overriding_Operation
4114
4115    begin
4116       --  In the case where both operations are implicit derived
4117       --  subprograms then neither overrides the other. This can
4118       --  only occur in certain obscure cases (e.g., derivation
4119       --  from homographs created in a generic instantiation).
4120
4121       if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
4122          return True;
4123
4124       elsif Ekind (Current_Scope) = E_Package
4125         and then Is_Generic_Instance (Current_Scope)
4126         and then In_Private_Part (Current_Scope)
4127         and then Comes_From_Source (New_E)
4128       then
4129          --  We examine the formals and result subtype of the inherited
4130          --  operation, to determine whether their type is derived from
4131          --  (the instance of) a generic type.
4132
4133          Formal := First_Formal (Prev_E);
4134
4135          while Present (Formal) loop
4136             F_Typ := Base_Type (Etype (Formal));
4137
4138             if Ekind (F_Typ) = E_Anonymous_Access_Type then
4139                F_Typ := Designated_Type (F_Typ);
4140             end if;
4141
4142             G_Typ := Get_Generic_Parent_Type (F_Typ);
4143
4144             Next_Formal (Formal);
4145          end loop;
4146
4147          if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
4148             G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
4149          end if;
4150
4151          if No (G_Typ) then
4152             return False;
4153          end if;
4154
4155          --  If the generic type is a private type, then the original
4156          --  operation was not overriding in the generic, because there was
4157          --  no primitive operation to override.
4158
4159          if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
4160            and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
4161              N_Formal_Private_Type_Definition
4162          then
4163             return True;
4164
4165          --  The generic parent type is the ancestor of a formal derived
4166          --  type declaration. We need to check whether it has a primitive
4167          --  operation that should be overridden by New_E in the generic.
4168
4169          else
4170             declare
4171                P_Formal : Entity_Id;
4172                N_Formal : Entity_Id;
4173                P_Typ    : Entity_Id;
4174                N_Typ    : Entity_Id;
4175                P_Prim   : Entity_Id;
4176                Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
4177
4178             begin
4179                while Present (Prim_Elt) loop
4180                   P_Prim := Node (Prim_Elt);
4181
4182                   if Chars (P_Prim) = Chars (New_E)
4183                     and then Ekind (P_Prim) = Ekind (New_E)
4184                   then
4185                      P_Formal := First_Formal (P_Prim);
4186                      N_Formal := First_Formal (New_E);
4187                      while Present (P_Formal) and then Present (N_Formal) loop
4188                         P_Typ := Etype (P_Formal);
4189                         N_Typ := Etype (N_Formal);
4190
4191                         if not Types_Correspond (P_Typ, N_Typ) then
4192                            exit;
4193                         end if;
4194
4195                         Next_Entity (P_Formal);
4196                         Next_Entity (N_Formal);
4197                      end loop;
4198
4199                      --  Found a matching primitive operation belonging to
4200                      --  the formal ancestor type, so the new subprogram
4201                      --  is overriding.
4202
4203                      if not Present (P_Formal)
4204                        and then not Present (N_Formal)
4205                        and then (Ekind (New_E) /= E_Function
4206                                   or else
4207                                  Types_Correspond
4208                                    (Etype (P_Prim), Etype (New_E)))
4209                      then
4210                         return False;
4211                      end if;
4212                   end if;
4213
4214                   Next_Elmt (Prim_Elt);
4215                end loop;
4216
4217                --  If no match found, then the new subprogram does
4218                --  not override in the generic (nor in the instance).
4219
4220                return True;
4221             end;
4222          end if;
4223       else
4224          return False;
4225       end if;
4226    end Is_Non_Overriding_Operation;
4227
4228    ------------------------------
4229    -- Make_Inequality_Operator --
4230    ------------------------------
4231
4232    --  S is the defining identifier of an equality operator. We build a
4233    --  subprogram declaration with the right signature. This operation is
4234    --  intrinsic, because it is always expanded as the negation of the
4235    --  call to the equality function.
4236
4237    procedure Make_Inequality_Operator (S : Entity_Id) is
4238       Loc     : constant Source_Ptr := Sloc (S);
4239       Decl    : Node_Id;
4240       Formals : List_Id;
4241       Op_Name : Entity_Id;
4242
4243       A : Entity_Id;
4244       B : Entity_Id;
4245
4246    begin
4247       --  Check that equality was properly defined.
4248
4249       if  No (Next_Formal (First_Formal (S))) then
4250          return;
4251       end if;
4252
4253       A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
4254       B := Make_Defining_Identifier (Loc,
4255              Chars (Next_Formal (First_Formal (S))));
4256
4257       Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
4258
4259       Formals := New_List (
4260         Make_Parameter_Specification (Loc,
4261           Defining_Identifier => A,
4262           Parameter_Type =>
4263             New_Reference_To (Etype (First_Formal (S)), Loc)),
4264
4265         Make_Parameter_Specification (Loc,
4266           Defining_Identifier => B,
4267           Parameter_Type =>
4268             New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
4269
4270       Decl :=
4271         Make_Subprogram_Declaration (Loc,
4272           Specification =>
4273             Make_Function_Specification (Loc,
4274               Defining_Unit_Name => Op_Name,
4275               Parameter_Specifications => Formals,
4276               Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
4277
4278       --  Insert inequality right after equality if it is explicit or after
4279       --  the derived type when implicit. These entities are created only
4280       --  for visibility purposes, and eventually replaced in the course of
4281       --  expansion, so they do not need to be attached to the tree and seen
4282       --  by the back-end. Keeping them internal also avoids spurious freezing
4283       --  problems. The parent field is set simply to make analysis safe.
4284
4285       if No (Alias (S)) then
4286          Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
4287       else
4288          Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
4289       end if;
4290
4291       Mark_Rewrite_Insertion (Decl);
4292       Set_Is_Intrinsic_Subprogram (Op_Name);
4293       Analyze (Decl);
4294       Set_Has_Completion (Op_Name);
4295       Set_Corresponding_Equality (Op_Name, S);
4296       Set_Is_Abstract (Op_Name, Is_Abstract (S));
4297
4298    end Make_Inequality_Operator;
4299
4300    ----------------------
4301    -- May_Need_Actuals --
4302    ----------------------
4303
4304    procedure May_Need_Actuals (Fun : Entity_Id) is
4305       F : Entity_Id;
4306       B : Boolean;
4307
4308    begin
4309       F := First_Formal (Fun);
4310       B := True;
4311
4312       while Present (F) loop
4313          if No (Default_Value (F)) then
4314             B := False;
4315             exit;
4316          end if;
4317
4318          Next_Formal (F);
4319       end loop;
4320
4321       Set_Needs_No_Actuals (Fun, B);
4322    end May_Need_Actuals;
4323
4324    ---------------------
4325    -- Mode_Conformant --
4326    ---------------------
4327
4328    function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
4329       Result : Boolean;
4330
4331    begin
4332       Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
4333       return Result;
4334    end Mode_Conformant;
4335
4336    ---------------------------
4337    -- New_Overloaded_Entity --
4338    ---------------------------
4339
4340    procedure New_Overloaded_Entity
4341      (S            : Entity_Id;
4342       Derived_Type : Entity_Id := Empty)
4343    is
4344       E : Entity_Id;
4345       --  Entity that S overrides
4346
4347       Prev_Vis : Entity_Id := Empty;
4348       --  Needs comment ???
4349
4350       function Is_Private_Declaration (E : Entity_Id) return Boolean;
4351       --  Check that E is declared in the private part of the current package,
4352       --  or in the package body, where it may hide a previous declaration.
4353       --  We can't use In_Private_Part by itself because this flag is also
4354       --  set when freezing entities, so we must examine the place of the
4355       --  declaration in the tree, and recognize wrapper packages as well.
4356
4357       procedure Maybe_Primitive_Operation (Overriding : Boolean := False);
4358       --  If the subprogram being analyzed is a primitive operation of
4359       --  the type of one of its formals, set the corresponding flag.
4360
4361       ----------------------------
4362       -- Is_Private_Declaration --
4363       ----------------------------
4364
4365       function Is_Private_Declaration (E : Entity_Id) return Boolean is
4366          Priv_Decls : List_Id;
4367          Decl       : constant Node_Id := Unit_Declaration_Node (E);
4368
4369       begin
4370          if Is_Package (Current_Scope)
4371            and then In_Private_Part (Current_Scope)
4372          then
4373             Priv_Decls :=
4374               Private_Declarations (
4375                 Specification (Unit_Declaration_Node (Current_Scope)));
4376
4377             return In_Package_Body (Current_Scope)
4378               or else List_Containing (Decl) = Priv_Decls
4379               or else (Nkind (Parent (Decl)) = N_Package_Specification
4380                          and then not Is_Compilation_Unit (
4381                            Defining_Entity (Parent (Decl)))
4382                          and then List_Containing (Parent (Parent (Decl)))
4383                            = Priv_Decls);
4384          else
4385             return False;
4386          end if;
4387       end Is_Private_Declaration;
4388
4389       -------------------------------
4390       -- Maybe_Primitive_Operation --
4391       -------------------------------
4392
4393       procedure Maybe_Primitive_Operation (Overriding : Boolean := False) is
4394          Formal : Entity_Id;
4395          F_Typ  : Entity_Id;
4396          B_Typ  : Entity_Id;
4397
4398          function Visible_Part_Type (T : Entity_Id) return Boolean;
4399          --  Returns true if T is declared in the visible part of
4400          --  the current package scope; otherwise returns false.
4401          --  Assumes that T is declared in a package.
4402
4403          procedure Check_Private_Overriding (T : Entity_Id);
4404          --  Checks that if a primitive abstract subprogram of a visible
4405          --  abstract type is declared in a private part, then it must
4406          --  override an abstract subprogram declared in the visible part.
4407          --  Also checks that if a primitive function with a controlling
4408          --  result is declared in a private part, then it must override
4409          --  a function declared in the visible part.
4410
4411          ------------------------------
4412          -- Check_Private_Overriding --
4413          ------------------------------
4414
4415          procedure Check_Private_Overriding (T : Entity_Id) is
4416          begin
4417             if Ekind (Current_Scope) = E_Package
4418               and then In_Private_Part (Current_Scope)
4419               and then Visible_Part_Type (T)
4420               and then not In_Instance
4421             then
4422                if Is_Abstract (T)
4423                  and then Is_Abstract (S)
4424                  and then (not Overriding or else not Is_Abstract (E))
4425                then
4426                   Error_Msg_N ("abstract subprograms must be visible "
4427                                 & "('R'M 3.9.3(10))!", S);
4428
4429                elsif Ekind (S) = E_Function
4430                  and then Is_Tagged_Type (T)
4431                  and then T = Base_Type (Etype (S))
4432                  and then not Overriding
4433                then
4434                   Error_Msg_N
4435                     ("private function with tagged result must"
4436                      & " override visible-part function", S);
4437                   Error_Msg_N
4438                     ("\move subprogram to the visible part"
4439                      & " ('R'M 3.9.3(10))", S);
4440                end if;
4441             end if;
4442          end Check_Private_Overriding;
4443
4444          -----------------------
4445          -- Visible_Part_Type --
4446          -----------------------
4447
4448          function Visible_Part_Type (T : Entity_Id) return Boolean is
4449             P : constant Node_Id := Unit_Declaration_Node (Scope (T));
4450             N : Node_Id;
4451
4452          begin
4453             --  If the entity is a private type, then it must be
4454             --  declared in a visible part.
4455
4456             if Ekind (T) in Private_Kind then
4457                return True;
4458             end if;
4459
4460             --  Otherwise, we traverse the visible part looking for its
4461             --  corresponding declaration. We cannot use the declaration
4462             --  node directly because in the private part the entity of a
4463             --  private type is the one in the full view, which does not
4464             --  indicate that it is the completion of something visible.
4465
4466             N := First (Visible_Declarations (Specification (P)));
4467             while Present (N) loop
4468                if Nkind (N) = N_Full_Type_Declaration
4469                  and then Present (Defining_Identifier (N))
4470                  and then T = Defining_Identifier (N)
4471                then
4472                   return True;
4473
4474                elsif (Nkind (N) = N_Private_Type_Declaration
4475                        or else
4476                       Nkind (N) = N_Private_Extension_Declaration)
4477                  and then Present (Defining_Identifier (N))
4478                  and then T = Full_View (Defining_Identifier (N))
4479                then
4480                   return True;
4481                end if;
4482
4483                Next (N);
4484             end loop;
4485
4486             return False;
4487          end Visible_Part_Type;
4488
4489       --  Start of processing for Maybe_Primitive_Operation
4490
4491       begin
4492          if not Comes_From_Source (S) then
4493             null;
4494
4495          --  If the subprogram is at library level, it is not a
4496          --  primitive operation.
4497
4498          elsif Current_Scope = Standard_Standard then
4499             null;
4500
4501          elsif (Ekind (Current_Scope) = E_Package
4502                  and then not In_Package_Body (Current_Scope))
4503            or else Overriding
4504          then
4505             --  For function, check return type
4506
4507             if Ekind (S) = E_Function then
4508                B_Typ := Base_Type (Etype (S));
4509
4510                if Scope (B_Typ) = Current_Scope then
4511                   Set_Has_Primitive_Operations (B_Typ);
4512                   Check_Private_Overriding (B_Typ);
4513                end if;
4514             end if;
4515
4516             --  For all subprograms, check formals
4517
4518             Formal := First_Formal (S);
4519             while Present (Formal) loop
4520                if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
4521                   F_Typ := Designated_Type (Etype (Formal));
4522                else
4523                   F_Typ := Etype (Formal);
4524                end if;
4525
4526                B_Typ := Base_Type (F_Typ);
4527
4528                if Scope (B_Typ) = Current_Scope then
4529                   Set_Has_Primitive_Operations (B_Typ);
4530                   Check_Private_Overriding (B_Typ);
4531                end if;
4532
4533                Next_Formal (Formal);
4534             end loop;
4535          end if;
4536       end Maybe_Primitive_Operation;
4537
4538    --  Start of processing for New_Overloaded_Entity
4539
4540    begin
4541       --  We need to look for an entity that S may override. This must be a
4542       --  homonym in the current scope, so we look for the first homonym of
4543       --  S in the current scope as the starting point for the search.
4544
4545       E := Current_Entity_In_Scope (S);
4546
4547       --  If there is no homonym then this is definitely not overriding
4548
4549       if No (E) then
4550          Enter_Overloaded_Entity (S);
4551          Check_Dispatching_Operation (S, Empty);
4552          Maybe_Primitive_Operation;
4553
4554       --  If there is a homonym that is not overloadable, then we have an
4555       --  error, except for the special cases checked explicitly below.
4556
4557       elsif not Is_Overloadable (E) then
4558
4559          --  Check for spurious conflict produced by a subprogram that has the
4560          --  same name as that of the enclosing generic package. The conflict
4561          --  occurs within an instance, between the subprogram and the renaming
4562          --  declaration for the package. After the subprogram, the package
4563          --  renaming declaration becomes hidden.
4564
4565          if Ekind (E) = E_Package
4566            and then Present (Renamed_Object (E))
4567            and then Renamed_Object (E) = Current_Scope
4568            and then Nkind (Parent (Renamed_Object (E))) =
4569                                                      N_Package_Specification
4570            and then Present (Generic_Parent (Parent (Renamed_Object (E))))
4571          then
4572             Set_Is_Hidden (E);
4573             Set_Is_Immediately_Visible (E, False);
4574             Enter_Overloaded_Entity (S);
4575             Set_Homonym (S, Homonym (E));
4576             Check_Dispatching_Operation (S, Empty);
4577
4578          --  If the subprogram is implicit it is hidden by the previous
4579          --  declaration. However if it is dispatching, it must appear in
4580          --  the dispatch table anyway, because it can be dispatched to
4581          --  even if it cannot be called directly.
4582
4583          elsif Present (Alias (S))
4584            and then not Comes_From_Source (S)
4585          then
4586             Set_Scope (S, Current_Scope);
4587
4588             if Is_Dispatching_Operation (Alias (S)) then
4589                Check_Dispatching_Operation (S, Empty);
4590             end if;
4591
4592             return;
4593
4594          else
4595             Error_Msg_Sloc := Sloc (E);
4596             Error_Msg_N ("& conflicts with declaration#", S);
4597
4598             --  Useful additional warning
4599
4600             if Is_Generic_Unit (E) then
4601                Error_Msg_N ("\previous generic unit cannot be overloaded", S);
4602             end if;
4603
4604             return;
4605          end if;
4606
4607       --  E exists and is overloadable
4608
4609       else
4610          --  Loop through E and its homonyms to determine if any of them
4611          --  is the candidate for overriding by S.
4612
4613          while Present (E) loop
4614
4615             --  Definitely not interesting if not in the current scope
4616
4617             if Scope (E) /= Current_Scope then
4618                null;
4619
4620             --  Check if we have type conformance
4621
4622             elsif Type_Conformant (E, S) then
4623
4624                --  If the old and new entities have the same profile and
4625                --  one is not the body of the other, then this is an error,
4626                --  unless one of them is implicitly declared.
4627
4628                --  There are some cases when both can be implicit, for example
4629                --  when both a literal and a function that overrides it are
4630                --  inherited in a derivation, or when an inhertited operation
4631                --  of a tagged full type overrides the ineherited operation of
4632                --  a private extension. Ada 83 had a special rule for the
4633                --  the literal case. In Ada95, the later implicit operation
4634                --  hides the former, and the literal is always the former.
4635                --  In the odd case where both are derived operations declared
4636                --  at the same point, both operations should be declared,
4637                --  and in that case we bypass the following test and proceed
4638                --  to the next part (this can only occur for certain obscure
4639                --  cases involving homographs in instances and can't occur for
4640                --  dispatching operations ???). Note that the following
4641                --  condition is less than clear. For example, it's not at
4642                --  all clear why there's a test for E_Entry here. ???
4643
4644                if Present (Alias (S))
4645                  and then (No (Alias (E))
4646                             or else Comes_From_Source (E)
4647                             or else Is_Dispatching_Operation (E))
4648                  and then
4649                    (Ekind (E) = E_Entry
4650                      or else Ekind (E) /= E_Enumeration_Literal)
4651                then
4652                   --  When an derived operation is overloaded it may be due
4653                   --  to the fact that the full view of a private extension
4654                   --  re-inherits. It has to be dealt with.
4655
4656                   if Is_Package (Current_Scope)
4657                     and then In_Private_Part (Current_Scope)
4658                   then
4659                      Check_Operation_From_Private_View (S, E);
4660                   end if;
4661
4662                   --  In any case the implicit operation remains hidden by
4663                   --  the existing declaration, which is overriding.
4664
4665                   Set_Is_Overriding_Operation (E);
4666                   return;
4667
4668                   --  Within an instance, the renaming declarations for
4669                   --  actual subprograms may become ambiguous, but they do
4670                   --  not hide each other.
4671
4672                elsif Ekind (E) /= E_Entry
4673                  and then not Comes_From_Source (E)
4674                  and then not Is_Generic_Instance (E)
4675                  and then (Present (Alias (E))
4676                             or else Is_Intrinsic_Subprogram (E))
4677                  and then (not In_Instance
4678                             or else No (Parent (E))
4679                             or else Nkind (Unit_Declaration_Node (E)) /=
4680                                N_Subprogram_Renaming_Declaration)
4681                then
4682                   --  A subprogram child unit is not allowed to override
4683                   --  an inherited subprogram (10.1.1(20)).
4684
4685                   if Is_Child_Unit (S) then
4686                      Error_Msg_N
4687                        ("child unit overrides inherited subprogram in parent",
4688                         S);
4689                      return;
4690                   end if;
4691
4692                   if Is_Non_Overriding_Operation (E, S) then
4693                      Enter_Overloaded_Entity (S);
4694                      if not Present (Derived_Type)
4695                        or else Is_Tagged_Type (Derived_Type)
4696                      then
4697                         Check_Dispatching_Operation (S, Empty);
4698                      end if;
4699
4700                      return;
4701                   end if;
4702
4703                   --  E is a derived operation or an internal operator which
4704                   --  is being overridden. Remove E from further visibility.
4705                   --  Furthermore, if E is a dispatching operation, it must be
4706                   --  replaced in the list of primitive operations of its type
4707                   --  (see Override_Dispatching_Operation).
4708
4709                   declare
4710                      Prev : Entity_Id;
4711
4712                   begin
4713                      Prev := First_Entity (Current_Scope);
4714
4715                      while Present (Prev)
4716                        and then Next_Entity (Prev) /= E
4717                      loop
4718                         Next_Entity (Prev);
4719                      end loop;
4720
4721                      --  It is possible for E to be in the current scope and
4722                      --  yet not in the entity chain. This can only occur in a
4723                      --  generic context where E is an implicit concatenation
4724                      --  in the formal part, because in a generic body the
4725                      --  entity chain starts with the formals.
4726
4727                      pragma Assert
4728                        (Present (Prev) or else Chars (E) = Name_Op_Concat);
4729
4730                      --  E must be removed both from the entity_list of the
4731                      --  current scope, and from the visibility chain
4732
4733                      if Debug_Flag_E then
4734                         Write_Str ("Override implicit operation ");
4735                         Write_Int (Int (E));
4736                         Write_Eol;
4737                      end if;
4738
4739                      --  If E is a predefined concatenation, it stands for four
4740                      --  different operations. As a result, a single explicit
4741                      --  declaration does not hide it. In a possible ambiguous
4742                      --  situation, Disambiguate chooses the user-defined op,
4743                      --  so it is correct to retain the previous internal one.
4744
4745                      if Chars (E) /= Name_Op_Concat
4746                        or else Ekind (E) /= E_Operator
4747                      then
4748                         --  For nondispatching derived operations that are
4749                         --  overridden by a subprogram declared in the private
4750                         --  part of a package, we retain the derived subprogram
4751                         --  but mark it as not immediately visible. If the
4752                         --  derived operation was declared in the visible part
4753                         --  then this ensures that it will still be visible
4754                         --  outside the package with the proper signature
4755                         --  (calls from outside must also be directed to this
4756                         --  version rather than the overriding one, unlike the
4757                         --  dispatching case). Calls from inside the package
4758                         --  will still resolve to the overriding subprogram
4759                         --  since the derived one is marked as not visible
4760                         --  within the package.
4761
4762                         --  If the private operation is dispatching, we achieve
4763                         --  the overriding by keeping the implicit operation
4764                         --  but setting its alias to be the overring one. In
4765                         --  this fashion the proper body is executed in all
4766                         --  cases, but the original signature is used outside
4767                         --  of the package.
4768
4769                         --  If the overriding is not in the private part, we
4770                         --  remove the implicit operation altogether.
4771
4772                         if Is_Private_Declaration (S) then
4773
4774                            if not Is_Dispatching_Operation (E) then
4775                               Set_Is_Immediately_Visible (E, False);
4776                            else
4777
4778                               --  work done in Override_Dispatching_Operation.
4779
4780                               null;
4781                            end if;
4782
4783                         else
4784                            --  Find predecessor of E in Homonym chain
4785
4786                            if E = Current_Entity (E) then
4787                               Prev_Vis := Empty;
4788                            else
4789                               Prev_Vis := Current_Entity (E);
4790                               while Homonym (Prev_Vis) /= E loop
4791                                  Prev_Vis := Homonym (Prev_Vis);
4792                               end loop;
4793                            end if;
4794
4795                            if Prev_Vis /= Empty then
4796
4797                               --  Skip E in the visibility chain
4798
4799                               Set_Homonym (Prev_Vis, Homonym (E));
4800
4801                            else
4802                               Set_Name_Entity_Id (Chars (E), Homonym (E));
4803                            end if;
4804
4805                            Set_Next_Entity (Prev, Next_Entity (E));
4806
4807                            if No (Next_Entity (Prev)) then
4808                               Set_Last_Entity (Current_Scope, Prev);
4809                            end if;
4810
4811                         end if;
4812                      end if;
4813
4814                      Enter_Overloaded_Entity (S);
4815                      Set_Is_Overriding_Operation (S);
4816
4817                      if Is_Dispatching_Operation (E) then
4818
4819                         --  An overriding dispatching subprogram inherits
4820                         --  the convention of the overridden subprogram
4821                         --  (by AI-117).
4822
4823                         Set_Convention (S, Convention (E));
4824
4825                         Check_Dispatching_Operation (S, E);
4826                      else
4827                         Check_Dispatching_Operation (S, Empty);
4828                      end if;
4829
4830                      Maybe_Primitive_Operation (Overriding => True);
4831                      goto Check_Inequality;
4832                   end;
4833
4834                --  Apparent redeclarations in instances can occur when two
4835                --  formal types get the same actual type. The subprograms in
4836                --  in the instance are legal,  even if not callable from the
4837                --  outside. Calls from within are disambiguated elsewhere.
4838                --  For dispatching operations in the visible part, the usual
4839                --  rules apply, and operations with the same profile are not
4840                --  legal (B830001).
4841
4842                elsif (In_Instance_Visible_Part
4843                        and then not Is_Dispatching_Operation (E))
4844                  or else In_Instance_Not_Visible
4845                then
4846                   null;
4847
4848                --  Here we have a real error (identical profile)
4849
4850                else
4851                   Error_Msg_Sloc := Sloc (E);
4852
4853                   --  Avoid cascaded errors if the entity appears in
4854                   --  subsequent calls.
4855
4856                   Set_Scope (S, Current_Scope);
4857
4858                   Error_Msg_N ("& conflicts with declaration#", S);
4859
4860                   if Is_Generic_Instance (S)
4861                     and then not Has_Completion (E)
4862                   then
4863                      Error_Msg_N
4864                        ("\instantiation cannot provide body for it", S);
4865                   end if;
4866
4867                   return;
4868                end if;
4869
4870             else
4871                null;
4872             end if;
4873
4874             Prev_Vis := E;
4875             E := Homonym (E);
4876          end loop;
4877
4878          --  On exit, we know that S is a new entity
4879
4880          Enter_Overloaded_Entity (S);
4881          Maybe_Primitive_Operation;
4882
4883          --  If S is a derived operation for an untagged type then
4884          --  by definition it's not a dispatching operation (even
4885          --  if the parent operation was dispatching), so we don't
4886          --  call Check_Dispatching_Operation in that case.
4887
4888          if not Present (Derived_Type)
4889            or else Is_Tagged_Type (Derived_Type)
4890          then
4891             Check_Dispatching_Operation (S, Empty);
4892          end if;
4893       end if;
4894
4895       --  If this is a  user-defined equality operator that is not
4896       --  a derived subprogram, create the corresponding inequality.
4897       --  If the operation is dispatching, the expansion is done
4898       --  elsewhere, and we do not create an explicit inequality
4899       --  operation.
4900
4901       <<Check_Inequality>>
4902          if Chars (S) = Name_Op_Eq
4903            and then Etype (S) = Standard_Boolean
4904            and then Present (Parent (S))
4905            and then not Is_Dispatching_Operation (S)
4906          then
4907             Make_Inequality_Operator (S);
4908          end if;
4909    end New_Overloaded_Entity;
4910
4911    ---------------------
4912    -- Process_Formals --
4913    ---------------------
4914
4915    procedure Process_Formals
4916      (T           : List_Id;
4917       Related_Nod : Node_Id)
4918    is
4919       Param_Spec  : Node_Id;
4920       Formal      : Entity_Id;
4921       Formal_Type : Entity_Id;
4922       Default     : Node_Id;
4923       Ptype       : Entity_Id;
4924
4925       function Is_Class_Wide_Default (D : Node_Id) return Boolean;
4926       --  Check whether the default has a class-wide type. After analysis
4927       --  the default has the type of the formal, so we must also check
4928       --  explicitly for an access attribute.
4929
4930       ---------------------------
4931       -- Is_Class_Wide_Default --
4932       ---------------------------
4933
4934       function Is_Class_Wide_Default (D : Node_Id) return Boolean is
4935       begin
4936          return Is_Class_Wide_Type (Designated_Type (Etype (D)))
4937            or else (Nkind (D) =  N_Attribute_Reference
4938                       and then Attribute_Name (D) = Name_Access
4939                       and then Is_Class_Wide_Type (Etype (Prefix (D))));
4940       end Is_Class_Wide_Default;
4941
4942    --  Start of processing for Process_Formals
4943
4944    begin
4945       --  In order to prevent premature use of the formals in the same formal
4946       --  part, the Ekind is left undefined until all default expressions are
4947       --  analyzed. The Ekind is established in a separate loop at the end.
4948
4949       Param_Spec := First (T);
4950
4951       while Present (Param_Spec) loop
4952
4953          Formal := Defining_Identifier (Param_Spec);
4954          Enter_Name (Formal);
4955
4956          --  Case of ordinary parameters
4957
4958          if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
4959             Find_Type (Parameter_Type (Param_Spec));
4960             Ptype := Parameter_Type (Param_Spec);
4961
4962             if Ptype = Error then
4963                goto Continue;
4964             end if;
4965
4966             Formal_Type := Entity (Ptype);
4967
4968             if Ekind (Formal_Type) = E_Incomplete_Type
4969               or else (Is_Class_Wide_Type (Formal_Type)
4970                         and then Ekind (Root_Type (Formal_Type)) =
4971                                                          E_Incomplete_Type)
4972             then
4973                --  Ada 2005 (AI-50217): Incomplete tagged types that are made
4974                --  visible by a limited with_clause are valid formal types.
4975
4976                if From_With_Type (Formal_Type)
4977                  and then Is_Tagged_Type (Formal_Type)
4978                then
4979                   null;
4980
4981                elsif Nkind (Parent (T)) /= N_Access_Function_Definition
4982                  and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
4983                then
4984                   Error_Msg_N ("invalid use of incomplete type", Param_Spec);
4985                end if;
4986
4987             elsif Ekind (Formal_Type) = E_Void then
4988                Error_Msg_NE ("premature use of&",
4989                  Parameter_Type (Param_Spec), Formal_Type);
4990             end if;
4991
4992             --  Ada 2005 (AI-231): Create and decorate an internal subtype
4993             --  declaration corresponding to the null-excluding type of the
4994             --  formal in the enclosing scope. In addition, replace the
4995             --  parameter type of the formal to this internal subtype.
4996
4997             if Null_Exclusion_Present (Param_Spec) then
4998                declare
4999                   Loc   : constant Source_Ptr := Sloc (Param_Spec);
5000
5001                   Anon  : constant Entity_Id :=
5002                             Make_Defining_Identifier (Loc,
5003                               Chars => New_Internal_Name ('S'));
5004
5005                   Curr_Scope : constant Scope_Stack_Entry :=
5006                                  Scope_Stack.Table (Scope_Stack.Last);
5007
5008                   Ptype : constant Node_Id := Parameter_Type (Param_Spec);
5009                   Decl  : Node_Id;
5010                   P     : Node_Id := Parent (Parent (Related_Nod));
5011
5012                begin
5013                   Set_Is_Internal (Anon);
5014
5015                   Decl :=
5016                     Make_Subtype_Declaration (Loc,
5017                       Defining_Identifier      => Anon,
5018                         Null_Exclusion_Present => True,
5019                         Subtype_Indication     =>
5020                           New_Occurrence_Of (Etype (Ptype), Loc));
5021
5022                   --  Propagate the null-excluding attribute to the new entity
5023
5024                   if Null_Exclusion_Present (Param_Spec) then
5025                      Set_Null_Exclusion_Present (Param_Spec, False);
5026                      Set_Can_Never_Be_Null (Anon);
5027                   end if;
5028
5029                   Mark_Rewrite_Insertion (Decl);
5030
5031                   --  Insert the new declaration in the nearest enclosing scope
5032
5033                   while not Has_Declarations (P) loop
5034                      P := Parent (P);
5035                   end loop;
5036
5037                   Prepend (Decl, Declarations (P));
5038
5039                   Rewrite (Ptype, New_Occurrence_Of (Anon, Loc));
5040                   Mark_Rewrite_Insertion (Ptype);
5041
5042                   --  Analyze the new declaration in the context of the
5043                   --  enclosing scope
5044
5045                   Scope_Stack.Decrement_Last;
5046                   Analyze (Decl);
5047                   Scope_Stack.Append (Curr_Scope);
5048
5049                   Formal_Type := Anon;
5050                end;
5051             end if;
5052
5053             --  Ada 2005 (AI-231): Static checks
5054
5055             if Null_Exclusion_Present (Param_Spec)
5056               or else Can_Never_Be_Null (Entity (Ptype))
5057             then
5058                Null_Exclusion_Static_Checks (Param_Spec);
5059             end if;
5060
5061          --  An access formal type
5062
5063          else
5064             Formal_Type :=
5065               Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
5066
5067             --  Ada 2005 (AI-254)
5068
5069             declare
5070                AD : constant Node_Id :=
5071                       Access_To_Subprogram_Definition
5072                         (Parameter_Type (Param_Spec));
5073             begin
5074                if Present (AD) and then Protected_Present (AD) then
5075                   Formal_Type :=
5076                     Replace_Anonymous_Access_To_Protected_Subprogram
5077                       (Param_Spec, Formal_Type);
5078                end if;
5079             end;
5080          end if;
5081
5082          Set_Etype (Formal, Formal_Type);
5083          Default := Expression (Param_Spec);
5084
5085          if Present (Default) then
5086             if Out_Present (Param_Spec) then
5087                Error_Msg_N
5088                  ("default initialization only allowed for IN parameters",
5089                   Param_Spec);
5090             end if;
5091
5092             --  Do the special preanalysis of the expression (see section on
5093             --  "Handling of Default Expressions" in the spec of package Sem).
5094
5095             Analyze_Per_Use_Expression (Default, Formal_Type);
5096
5097             --  Check that the designated type of an access parameter's
5098             --  default is not a class-wide type unless the parameter's
5099             --  designated type is also class-wide.
5100
5101             if Ekind (Formal_Type) = E_Anonymous_Access_Type
5102               and then Is_Class_Wide_Default (Default)
5103               and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
5104             then
5105                Error_Msg_N
5106                  ("access to class-wide expression not allowed here", Default);
5107             end if;
5108          end if;
5109
5110       <<Continue>>
5111          Next (Param_Spec);
5112       end loop;
5113
5114       --  Now set the kind (mode) of each formal
5115
5116       Param_Spec := First (T);
5117
5118       while Present (Param_Spec) loop
5119          Formal := Defining_Identifier (Param_Spec);
5120          Set_Formal_Mode (Formal);
5121
5122          if Ekind (Formal) = E_In_Parameter then
5123             Set_Default_Value (Formal, Expression (Param_Spec));
5124
5125             if Present (Expression (Param_Spec)) then
5126                Default :=  Expression (Param_Spec);
5127
5128                if Is_Scalar_Type (Etype (Default)) then
5129                   if Nkind
5130                        (Parameter_Type (Param_Spec)) /= N_Access_Definition
5131                   then
5132                      Formal_Type := Entity (Parameter_Type (Param_Spec));
5133
5134                   else
5135                      Formal_Type := Access_Definition
5136                        (Related_Nod, Parameter_Type (Param_Spec));
5137                   end if;
5138
5139                   Apply_Scalar_Range_Check (Default, Formal_Type);
5140                end if;
5141             end if;
5142          end if;
5143
5144          Next (Param_Spec);
5145       end loop;
5146
5147    end Process_Formals;
5148
5149    ----------------------------
5150    -- Reference_Body_Formals --
5151    ----------------------------
5152
5153    procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
5154       Fs : Entity_Id;
5155       Fb : Entity_Id;
5156
5157    begin
5158       if Error_Posted (Spec) then
5159          return;
5160       end if;
5161
5162       Fs := First_Formal (Spec);
5163       Fb := First_Formal (Bod);
5164
5165       while Present (Fs) loop
5166          Generate_Reference (Fs, Fb, 'b');
5167
5168          if Style_Check then
5169             Style.Check_Identifier (Fb, Fs);
5170          end if;
5171
5172          Set_Spec_Entity (Fb, Fs);
5173          Set_Referenced (Fs, False);
5174          Next_Formal (Fs);
5175          Next_Formal (Fb);
5176       end loop;
5177    end Reference_Body_Formals;
5178
5179    -------------------------
5180    -- Set_Actual_Subtypes --
5181    -------------------------
5182
5183    procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
5184       Loc            : constant Source_Ptr := Sloc (N);
5185       Decl           : Node_Id;
5186       Formal         : Entity_Id;
5187       T              : Entity_Id;
5188       First_Stmt     : Node_Id := Empty;
5189       AS_Needed      : Boolean;
5190
5191    begin
5192       --  If this is an emtpy initialization procedure, no need to create
5193       --  actual subtypes (small optimization).
5194
5195       if Ekind (Subp) = E_Procedure
5196         and then Is_Null_Init_Proc (Subp)
5197       then
5198          return;
5199       end if;
5200
5201       Formal := First_Formal (Subp);
5202       while Present (Formal) loop
5203          T := Etype (Formal);
5204
5205          --  We never need an actual subtype for a constrained formal.
5206
5207          if Is_Constrained (T) then
5208             AS_Needed := False;
5209
5210          --  If we have unknown discriminants, then we do not need an
5211          --  actual subtype, or more accurately we cannot figure it out!
5212          --  Note that all class-wide types have unknown discriminants.
5213
5214          elsif Has_Unknown_Discriminants (T) then
5215             AS_Needed := False;
5216
5217          --  At this stage we have an unconstrained type that may need
5218          --  an actual subtype. For sure the actual subtype is needed
5219          --  if we have an unconstrained array type.
5220
5221          elsif Is_Array_Type (T) then
5222             AS_Needed := True;
5223
5224          --  The only other case which needs an actual subtype is an
5225          --  unconstrained record type which is an IN parameter (we
5226          --  cannot generate actual subtypes for the OUT or IN OUT case,
5227          --  since an assignment can change the discriminant values.
5228          --  However we exclude the case of initialization procedures,
5229          --  since discriminants are handled very specially in this context,
5230          --  see the section entitled "Handling of Discriminants" in Einfo.
5231          --  We also exclude the case of Discrim_SO_Functions (functions
5232          --  used in front end layout mode for size/offset values), since
5233          --  in such functions only discriminants are referenced, and not
5234          --  only are such subtypes not needed, but they cannot always
5235          --  be generated, because of order of elaboration issues.
5236
5237          elsif Is_Record_Type (T)
5238            and then Ekind (Formal) = E_In_Parameter
5239            and then Chars (Formal) /= Name_uInit
5240            and then not Is_Unchecked_Union (T)
5241            and then not Is_Discrim_SO_Function (Subp)
5242          then
5243             AS_Needed := True;
5244
5245          --  All other cases do not need an actual subtype
5246
5247          else
5248             AS_Needed := False;
5249          end if;
5250
5251          --  Generate actual subtypes for unconstrained arrays and
5252          --  unconstrained discriminated records.
5253
5254          if AS_Needed then
5255             if Nkind (N) = N_Accept_Statement then
5256
5257                --  If expansion is active, The formal is replaced by a local
5258                --  variable that renames the corresponding entry of the
5259                --  parameter block, and it is this local variable that may
5260                --  require an actual subtype.
5261
5262                if Expander_Active then
5263                   Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
5264                else
5265                   Decl := Build_Actual_Subtype (T, Formal);
5266                end if;
5267
5268                if Present (Handled_Statement_Sequence (N)) then
5269                   First_Stmt :=
5270                     First (Statements (Handled_Statement_Sequence (N)));
5271                   Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
5272                   Mark_Rewrite_Insertion (Decl);
5273                else
5274                   --  If the accept statement has no body, there will be
5275                   --  no reference to the actuals, so no need to compute
5276                   --  actual subtypes.
5277
5278                   return;
5279                end if;
5280
5281             else
5282                Decl := Build_Actual_Subtype (T, Formal);
5283                Prepend (Decl, Declarations (N));
5284                Mark_Rewrite_Insertion (Decl);
5285             end if;
5286
5287             --  The declaration uses the bounds of an existing object,
5288             --  and therefore needs no constraint checks.
5289
5290             Analyze (Decl, Suppress => All_Checks);
5291
5292             --  We need to freeze manually the generated type when it is
5293             --  inserted anywhere else than in a declarative part.
5294
5295             if Present (First_Stmt) then
5296                Insert_List_Before_And_Analyze (First_Stmt,
5297                  Freeze_Entity (Defining_Identifier (Decl), Loc));
5298             end if;
5299
5300             if Nkind (N) = N_Accept_Statement
5301               and then Expander_Active
5302             then
5303                Set_Actual_Subtype (Renamed_Object (Formal),
5304                  Defining_Identifier (Decl));
5305             else
5306                Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
5307             end if;
5308          end if;
5309
5310          Next_Formal (Formal);
5311       end loop;
5312    end Set_Actual_Subtypes;
5313
5314    ---------------------
5315    -- Set_Formal_Mode --
5316    ---------------------
5317
5318    procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
5319       Spec : constant Node_Id := Parent (Formal_Id);
5320
5321    begin
5322       --  Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
5323       --  since we ensure that corresponding actuals are always valid at the
5324       --  point of the call.
5325
5326       if Out_Present (Spec) then
5327          if Ekind (Scope (Formal_Id)) = E_Function
5328            or else Ekind (Scope (Formal_Id)) = E_Generic_Function
5329          then
5330             Error_Msg_N ("functions can only have IN parameters", Spec);
5331             Set_Ekind (Formal_Id, E_In_Parameter);
5332
5333          elsif In_Present (Spec) then
5334             Set_Ekind (Formal_Id, E_In_Out_Parameter);
5335
5336          else
5337             Set_Ekind               (Formal_Id, E_Out_Parameter);
5338             Set_Never_Set_In_Source (Formal_Id, True);
5339             Set_Is_True_Constant    (Formal_Id, False);
5340             Set_Current_Value       (Formal_Id, Empty);
5341          end if;
5342
5343       else
5344          Set_Ekind (Formal_Id, E_In_Parameter);
5345       end if;
5346
5347       --  Set Is_Known_Non_Null for access parameters since the language
5348       --  guarantees that access parameters are always non-null. We also
5349       --  set Can_Never_Be_Null, since there is no way to change the value.
5350
5351       if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
5352
5353          --  Ada 2005 (AI-231): This behaviour has been modified in Ada 2005.
5354          --  It is only forced if the null_exclusion appears.
5355
5356          if Ada_Version < Ada_05
5357            or else Null_Exclusion_Present (Spec)
5358          then
5359             Set_Is_Known_Non_Null (Formal_Id);
5360             Set_Can_Never_Be_Null (Formal_Id);
5361          end if;
5362       end if;
5363
5364       Set_Mechanism (Formal_Id, Default_Mechanism);
5365       Set_Formal_Validity (Formal_Id);
5366    end Set_Formal_Mode;
5367
5368    -------------------------
5369    -- Set_Formal_Validity --
5370    -------------------------
5371
5372    procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
5373    begin
5374       --  If no validity checking, then we cannot assume anything about
5375       --  the validity of parameters, since we do not know there is any
5376       --  checking of the validity on the call side.
5377
5378       if not Validity_Checks_On then
5379          return;
5380
5381       --  If validity checking for parameters is enabled, this means we are
5382       --  not supposed to make any assumptions about argument values.
5383
5384       elsif Validity_Check_Parameters then
5385          return;
5386
5387       --  If we are checking in parameters, we will assume that the caller is
5388       --  also checking parameters, so we can assume the parameter is valid.
5389
5390       elsif Ekind (Formal_Id) = E_In_Parameter
5391         and then Validity_Check_In_Params
5392       then
5393          Set_Is_Known_Valid (Formal_Id, True);
5394
5395       --  Similar treatment for IN OUT parameters
5396
5397       elsif Ekind (Formal_Id) = E_In_Out_Parameter
5398         and then Validity_Check_In_Out_Params
5399       then
5400          Set_Is_Known_Valid (Formal_Id, True);
5401       end if;
5402    end Set_Formal_Validity;
5403
5404    ------------------------
5405    -- Subtype_Conformant --
5406    ------------------------
5407
5408    function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5409       Result : Boolean;
5410
5411    begin
5412       Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
5413       return Result;
5414    end Subtype_Conformant;
5415
5416    ---------------------
5417    -- Type_Conformant --
5418    ---------------------
5419
5420    function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5421       Result : Boolean;
5422    begin
5423       Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
5424       return Result;
5425    end Type_Conformant;
5426
5427    -------------------------------
5428    -- Valid_Operator_Definition --
5429    -------------------------------
5430
5431    procedure Valid_Operator_Definition (Designator : Entity_Id) is
5432       N    : Integer := 0;
5433       F    : Entity_Id;
5434       Id   : constant Name_Id := Chars (Designator);
5435       N_OK : Boolean;
5436
5437    begin
5438       F := First_Formal (Designator);
5439
5440       while Present (F) loop
5441          N := N + 1;
5442
5443          if Present (Default_Value (F)) then
5444             Error_Msg_N
5445               ("default values not allowed for operator parameters",
5446                Parent (F));
5447          end if;
5448
5449          Next_Formal (F);
5450       end loop;
5451
5452       --  Verify that user-defined operators have proper number of arguments
5453       --  First case of operators which can only be unary
5454
5455       if Id = Name_Op_Not
5456         or else Id = Name_Op_Abs
5457       then
5458          N_OK := (N = 1);
5459
5460       --  Case of operators which can be unary or binary
5461
5462       elsif Id = Name_Op_Add
5463         or Id = Name_Op_Subtract
5464       then
5465          N_OK := (N in 1 .. 2);
5466
5467       --  All other operators can only be binary
5468
5469       else
5470          N_OK := (N = 2);
5471       end if;
5472
5473       if not N_OK then
5474          Error_Msg_N
5475            ("incorrect number of arguments for operator", Designator);
5476       end if;
5477
5478       if Id = Name_Op_Ne
5479         and then Base_Type (Etype (Designator)) = Standard_Boolean
5480         and then not Is_Intrinsic_Subprogram (Designator)
5481       then
5482          Error_Msg_N
5483             ("explicit definition of inequality not allowed", Designator);
5484       end if;
5485    end Valid_Operator_Definition;
5486
5487 end Sem_Ch6;