OSDN Git Service

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