OSDN Git Service

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