OSDN Git Service

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