OSDN Git Service

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