OSDN Git Service

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