OSDN Git Service

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