OSDN Git Service

2011-08-05 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_elab.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ E L A B                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1997-2011, 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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Einfo;    use Einfo;
30 with Elists;   use Elists;
31 with Errout;   use Errout;
32 with Exp_Tss;  use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Expander; use Expander;
35 with Fname;    use Fname;
36 with Lib;      use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet;    use Namet;
39 with Nlists;   use Nlists;
40 with Nmake;    use Nmake;
41 with Opt;      use Opt;
42 with Output;   use Output;
43 with Restrict; use Restrict;
44 with Rident;   use Rident;
45 with Sem;      use Sem;
46 with Sem_Cat;  use Sem_Cat;
47 with Sem_Ch7;  use Sem_Ch7;
48 with Sem_Ch8;  use Sem_Ch8;
49 with Sem_Res;  use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Sinfo;    use Sinfo;
53 with Sinput;   use Sinput;
54 with Snames;   use Snames;
55 with Stand;    use Stand;
56 with Table;
57 with Tbuild;   use Tbuild;
58 with Uintp;    use Uintp;
59 with Uname;    use Uname;
60
61 package body Sem_Elab is
62
63    --  The following table records the recursive call chain for output in the
64    --  Output routine. Each entry records the call node and the entity of the
65    --  called routine. The number of entries in the table (i.e. the value of
66    --  Elab_Call.Last) indicates the current depth of recursion and is used to
67    --  identify the outer level.
68
69    type Elab_Call_Entry is record
70       Cloc : Source_Ptr;
71       Ent  : Entity_Id;
72    end record;
73
74    package Elab_Call is new Table.Table (
75      Table_Component_Type => Elab_Call_Entry,
76      Table_Index_Type     => Int,
77      Table_Low_Bound      => 1,
78      Table_Initial        => 50,
79      Table_Increment      => 100,
80      Table_Name           => "Elab_Call");
81
82    --  This table is initialized at the start of each outer level call. It
83    --  holds the entities for all subprograms that have been examined for this
84    --  particular outer level call, and is used to prevent both infinite
85    --  recursion, and useless reanalysis of bodies already seen
86
87    package Elab_Visited is new Table.Table (
88      Table_Component_Type => Entity_Id,
89      Table_Index_Type     => Int,
90      Table_Low_Bound      => 1,
91      Table_Initial        => 200,
92      Table_Increment      => 100,
93      Table_Name           => "Elab_Visited");
94
95    --  This table stores calls to Check_Internal_Call that are delayed
96    --  until all generics are instantiated, and in particular that all
97    --  generic bodies have been inserted. We need to delay, because we
98    --  need to be able to look through the inserted bodies.
99
100    type Delay_Element is record
101       N : Node_Id;
102       --  The parameter N from the call to Check_Internal_Call. Note that
103       --  this node may get rewritten over the delay period by expansion
104       --  in the call case (but not in the instantiation case).
105
106       E : Entity_Id;
107       --  The parameter E from the call to Check_Internal_Call
108
109       Orig_Ent : Entity_Id;
110       --  The parameter Orig_Ent from the call to Check_Internal_Call
111
112       Curscop : Entity_Id;
113       --  The current scope of the call. This is restored when we complete
114       --  the delayed call, so that we do this in the right scope.
115
116       From_Elab_Code : Boolean;
117       --  Save indication of whether this call is from elaboration code
118
119       Outer_Scope : Entity_Id;
120       --  Save scope of outer level call
121    end record;
122
123    package Delay_Check is new Table.Table (
124      Table_Component_Type => Delay_Element,
125      Table_Index_Type     => Int,
126      Table_Low_Bound      => 1,
127      Table_Initial        => 1000,
128      Table_Increment      => 100,
129      Table_Name           => "Delay_Check");
130
131    C_Scope : Entity_Id;
132    --  Top level scope of current scope. Compute this only once at the outer
133    --  level, i.e. for a call to Check_Elab_Call from outside this unit.
134
135    Outer_Level_Sloc : Source_Ptr;
136    --  Save Sloc value for outer level call node for comparisons of source
137    --  locations. A body is too late if it appears after the *outer* level
138    --  call, not the particular call that is being analyzed.
139
140    From_Elab_Code : Boolean;
141    --  This flag shows whether the outer level call currently being examined
142    --  is or is not in elaboration code. We are only interested in calls to
143    --  routines in other units if this flag is True.
144
145    In_Task_Activation : Boolean := False;
146    --  This flag indicates whether we are performing elaboration checks on
147    --  task procedures, at the point of activation. If true, we do not trace
148    --  internal calls in these procedures, because all local bodies are known
149    --  to be elaborated.
150
151    Delaying_Elab_Checks : Boolean := True;
152    --  This is set True till the compilation is complete, including the
153    --  insertion of all instance bodies. Then when Check_Elab_Calls is called,
154    --  the delay table is used to make the delayed calls and this flag is reset
155    --  to False, so that the calls are processed
156
157    -----------------------
158    -- Local Subprograms --
159    -----------------------
160
161    --  Note: Outer_Scope in all following specs represents the scope of
162    --  interest of the outer level call. If it is set to Standard_Standard,
163    --  then it means the outer level call was at elaboration level, and that
164    --  thus all calls are of interest. If it was set to some other scope,
165    --  then the original call was an inner call, and we are not interested
166    --  in calls that go outside this scope.
167
168    procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
169    --  Analysis of construct N shows that we should set Elaborate_All_Desirable
170    --  for the WITH clause for unit U (which will always be present). A special
171    --  case is when N is a function or procedure instantiation, in which case
172    --  it is sufficient to set Elaborate_Desirable, since in this case there is
173    --  no possibility of transitive elaboration issues.
174
175    procedure Check_A_Call
176      (N                 : Node_Id;
177       E                 : Entity_Id;
178       Outer_Scope       : Entity_Id;
179       Inter_Unit_Only   : Boolean;
180       Generate_Warnings : Boolean := True;
181       In_Init_Proc      : Boolean := False);
182    --  This is the internal recursive routine that is called to check for a
183    --  possible elaboration error. The argument N is a subprogram call or
184    --  generic instantiation to be checked, and E is the entity of the called
185    --  subprogram, or instantiated generic unit. The flag Outer_Scope is the
186    --  outer level scope for the original call. Inter_Unit_Only is set if the
187    --  call is only to be checked in the case where it is to another unit (and
188    --  skipped if within a unit). Generate_Warnings is set to False to suppress
189    --  warning messages about missing pragma Elaborate_All's. These messages
190    --  are not wanted for inner calls in the dynamic model. Flag In_Init_Proc
191    --  should be set whenever the current context is a type init proc.
192
193    procedure Check_Bad_Instantiation (N : Node_Id);
194    --  N is a node for an instantiation (if called with any other node kind,
195    --  Check_Bad_Instantiation ignores the call). This subprogram checks for
196    --  the special case of a generic instantiation of a generic spec in the
197    --  same declarative part as the instantiation where a body is present and
198    --  has not yet been seen. This is an obvious error, but needs to be checked
199    --  specially at the time of the instantiation, since it is a case where we
200    --  cannot insert the body anywhere. If this case is detected, warnings are
201    --  generated, and a raise of Program_Error is inserted. In addition any
202    --  subprograms in the generic spec are stubbed, and the Bad_Instantiation
203    --  flag is set on the instantiation node. The caller in Sem_Ch12 uses this
204    --  flag as an indication that no attempt should be made to insert an
205    --  instance body.
206
207    procedure Check_Internal_Call
208      (N           : Node_Id;
209       E           : Entity_Id;
210       Outer_Scope : Entity_Id;
211       Orig_Ent    : Entity_Id);
212    --  N is a function call or procedure statement call node and E is the
213    --  entity of the called function, which is within the current compilation
214    --  unit (where subunits count as part of the parent). This call checks if
215    --  this call, or any call within any accessed body could cause an ABE, and
216    --  if so, outputs a warning. Orig_Ent differs from E only in the case of
217    --  renamings, and points to the original name of the entity. This is used
218    --  for error messages. Outer_Scope is the outer level scope for the
219    --  original call.
220
221    procedure Check_Internal_Call_Continue
222      (N           : Node_Id;
223       E           : Entity_Id;
224       Outer_Scope : Entity_Id;
225       Orig_Ent    : Entity_Id);
226    --  The processing for Check_Internal_Call is divided up into two phases,
227    --  and this represents the second phase. The second phase is delayed if
228    --  Delaying_Elab_Calls is set to True. In this delayed case, the first
229    --  phase makes an entry in the Delay_Check table, which is processed when
230    --  Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
231    --  Check_Internal_Call. Outer_Scope is the outer level scope for the
232    --  original call.
233
234    function Has_Generic_Body (N : Node_Id) return Boolean;
235    --  N is a generic package instantiation node, and this routine determines
236    --  if this package spec does in fact have a generic body. If so, then
237    --  True is returned, otherwise False. Note that this is not at all the
238    --  same as checking if the unit requires a body, since it deals with
239    --  the case of optional bodies accurately (i.e. if a body is optional,
240    --  then it looks to see if a body is actually present). Note: this
241    --  function can only do a fully correct job if in generating code mode
242    --  where all bodies have to be present. If we are operating in semantics
243    --  check only mode, then in some cases of optional bodies, a result of
244    --  False may incorrectly be given. In practice this simply means that
245    --  some cases of warnings for incorrect order of elaboration will only
246    --  be given when generating code, which is not a big problem (and is
247    --  inevitable, given the optional body semantics of Ada).
248
249    procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
250    --  Given code for an elaboration check (or unconditional raise if the check
251    --  is not needed), inserts the code in the appropriate place. N is the call
252    --  or instantiation node for which the check code is required. C is the
253    --  test whose failure triggers the raise.
254
255    function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
256    --  Determine whether entity Id denotes a [Deep_]Finalize procedure
257
258    procedure Output_Calls (N : Node_Id);
259    --  Outputs chain of calls stored in the Elab_Call table. The caller has
260    --  already generated the main warning message, so the warnings generated
261    --  are all continuation messages. The argument is the call node at which
262    --  the messages are to be placed.
263
264    function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
265    --  Given two scopes, determine whether they are the same scope from an
266    --  elaboration point of view, i.e. packages and blocks are ignored.
267
268    procedure Set_C_Scope;
269    --  On entry C_Scope is set to some scope. On return, C_Scope is reset
270    --  to be the enclosing compilation unit of this scope.
271
272    procedure Set_Elaboration_Constraint
273     (Call : Node_Id;
274      Subp : Entity_Id;
275      Scop : Entity_Id);
276    --  The current unit U may depend semantically on some unit P which is not
277    --  in the current context. If there is an elaboration call that reaches P,
278    --  we need to indicate that P requires an Elaborate_All, but this is not
279    --  effective in U's ali file, if there is no with_clause for P. In this
280    --  case we add the Elaborate_All on the unit Q that directly or indirectly
281    --  makes P available. This can happen in two cases:
282    --
283    --    a) Q declares a subtype of a type declared in P, and the call is an
284    --    initialization call for an object of that subtype.
285    --
286    --    b) Q declares an object of some tagged type whose root type is
287    --    declared in P, and the initialization call uses object notation on
288    --    that object to reach a primitive operation or a classwide operation
289    --    declared in P.
290    --
291    --  If P appears in the context of U, the current processing is correct.
292    --  Otherwise we must identify these two cases to retrieve Q and place the
293    --  Elaborate_All_Desirable on it.
294
295    function Spec_Entity (E : Entity_Id) return Entity_Id;
296    --  Given a compilation unit entity, if it is a spec entity, it is returned
297    --  unchanged. If it is a body entity, then the spec for the corresponding
298    --  spec is returned
299
300    procedure Supply_Bodies (N : Node_Id);
301    --  Given a node, N, that is either a subprogram declaration or a package
302    --  declaration, this procedure supplies dummy bodies for the subprogram
303    --  or for all subprograms in the package. If the given node is not one
304    --  of these two possibilities, then Supply_Bodies does nothing. The
305    --  dummy body contains a single Raise statement.
306
307    procedure Supply_Bodies (L : List_Id);
308    --  Calls Supply_Bodies for all elements of the given list L
309
310    function Within (E1, E2 : Entity_Id) return Boolean;
311    --  Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
312    --  of its contained scopes, False otherwise.
313
314    function Within_Elaborate_All (E : Entity_Id) return Boolean;
315    --  Before emitting a warning on a scope E for a missing elaborate_all,
316    --  check whether E may be in the context of a directly visible unit U to
317    --  which the pragma applies. This prevents spurious warnings when the
318    --  called entity is renamed within U.
319
320    --------------------------------------
321    -- Activate_Elaborate_All_Desirable --
322    --------------------------------------
323
324    procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
325       UN  : constant Unit_Number_Type := Get_Code_Unit (N);
326       CU  : constant Node_Id          := Cunit (UN);
327       UE  : constant Entity_Id        := Cunit_Entity (UN);
328       Unm : constant Unit_Name_Type   := Unit_Name (UN);
329       CI  : constant List_Id          := Context_Items (CU);
330       Itm : Node_Id;
331       Ent : Entity_Id;
332
333       procedure Add_To_Context_And_Mark (Itm : Node_Id);
334       --  This procedure is called when the elaborate indication must be
335       --  applied to a unit not in the context of the referencing unit. The
336       --  unit gets added to the context as an implicit with.
337
338       function In_Withs_Of (UEs : Entity_Id) return Boolean;
339       --  UEs is the spec entity of a unit. If the unit to be marked is
340       --  in the context item list of this unit spec, then the call returns
341       --  True and Itm is left set to point to the relevant N_With_Clause node.
342
343       procedure Set_Elab_Flag (Itm : Node_Id);
344       --  Sets Elaborate_[All_]Desirable as appropriate on Itm
345
346       -----------------------------
347       -- Add_To_Context_And_Mark --
348       -----------------------------
349
350       procedure Add_To_Context_And_Mark (Itm : Node_Id) is
351          CW : constant Node_Id :=
352                 Make_With_Clause (Sloc (Itm),
353                   Name => Name (Itm));
354
355       begin
356          Set_Library_Unit  (CW, Library_Unit (Itm));
357          Set_Implicit_With (CW, True);
358
359          --  Set elaborate all desirable on copy and then append the copy to
360          --  the list of body with's and we are done.
361
362          Set_Elab_Flag (CW);
363          Append_To (CI, CW);
364       end Add_To_Context_And_Mark;
365
366       -----------------
367       -- In_Withs_Of --
368       -----------------
369
370       function In_Withs_Of (UEs : Entity_Id) return Boolean is
371          UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
372          CUs : constant Node_Id          := Cunit (UNs);
373          CIs : constant List_Id          := Context_Items (CUs);
374
375       begin
376          Itm := First (CIs);
377          while Present (Itm) loop
378             if Nkind (Itm) = N_With_Clause then
379                Ent :=
380                  Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
381
382                if U = Ent then
383                   return True;
384                end if;
385             end if;
386
387             Next (Itm);
388          end loop;
389
390          return False;
391       end In_Withs_Of;
392
393       -------------------
394       -- Set_Elab_Flag --
395       -------------------
396
397       procedure Set_Elab_Flag (Itm : Node_Id) is
398       begin
399          if Nkind (N) in N_Subprogram_Instantiation then
400             Set_Elaborate_Desirable (Itm);
401          else
402             Set_Elaborate_All_Desirable (Itm);
403          end if;
404       end Set_Elab_Flag;
405
406    --  Start of processing for Activate_Elaborate_All_Desirable
407
408    begin
409       --  Do not set binder indication if expansion is disabled, as when
410       --  compiling a generic unit.
411
412       if not Expander_Active then
413          return;
414       end if;
415
416       Itm := First (CI);
417       while Present (Itm) loop
418          if Nkind (Itm) = N_With_Clause then
419             Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
420
421             --  If we find it, then mark elaborate all desirable and return
422
423             if U = Ent then
424                Set_Elab_Flag (Itm);
425                return;
426             end if;
427          end if;
428
429          Next (Itm);
430       end loop;
431
432       --  If we fall through then the with clause is not present in the
433       --  current unit. One legitimate possibility is that the with clause
434       --  is present in the spec when we are a body.
435
436       if Is_Body_Name (Unm)
437         and then In_Withs_Of (Spec_Entity (UE))
438       then
439          Add_To_Context_And_Mark (Itm);
440          return;
441       end if;
442
443       --  Similarly, we may be in the spec or body of a child unit, where
444       --  the unit in question is with'ed by some ancestor of the child unit.
445
446       if Is_Child_Name (Unm) then
447          declare
448             Pkg : Entity_Id;
449
450          begin
451             Pkg := UE;
452             loop
453                Pkg := Scope (Pkg);
454                exit when Pkg = Standard_Standard;
455
456                if In_Withs_Of (Pkg) then
457                   Add_To_Context_And_Mark (Itm);
458                   return;
459                end if;
460             end loop;
461          end;
462       end if;
463
464       --  Here if we do not find with clause on spec or body. We just ignore
465       --  this case, it means that the elaboration involves some other unit
466       --  than the unit being compiled, and will be caught elsewhere.
467
468       null;
469    end Activate_Elaborate_All_Desirable;
470
471    ------------------
472    -- Check_A_Call --
473    ------------------
474
475    procedure Check_A_Call
476      (N                 : Node_Id;
477       E                 : Entity_Id;
478       Outer_Scope       : Entity_Id;
479       Inter_Unit_Only   : Boolean;
480       Generate_Warnings : Boolean := True;
481       In_Init_Proc      : Boolean := False)
482    is
483       Loc  : constant Source_Ptr := Sloc (N);
484       Ent  : Entity_Id;
485       Decl : Node_Id;
486
487       E_Scope : Entity_Id;
488       --  Top level scope of entity for called subprogram. This value includes
489       --  following renamings and derivations, so this scope can be in a
490       --  non-visible unit. This is the scope that is to be investigated to
491       --  see whether an elaboration check is required.
492
493       W_Scope : Entity_Id;
494       --  Top level scope of directly called entity for subprogram. This
495       --  differs from E_Scope in the case where renamings or derivations
496       --  are involved, since it does not follow these links. W_Scope is
497       --  generally in a visible unit, and it is this scope that may require
498       --  an Elaborate_All. However, there are some cases (initialization
499       --  calls and calls involving object notation) where W_Scope might not
500       --  be in the context of the current unit, and there is an intermediate
501       --  package that is, in which case the Elaborate_All has to be placed
502       --  on this intermediate package. These special cases are handled in
503       --  Set_Elaboration_Constraint.
504
505       Body_Acts_As_Spec : Boolean;
506       --  Set to true if call is to body acting as spec (no separate spec)
507
508       Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
509       --  Indicates if we have instantiation case
510
511       Caller_Unit_Internal : Boolean;
512       Callee_Unit_Internal : Boolean;
513
514       Inst_Caller : Source_Ptr;
515       Inst_Callee : Source_Ptr;
516
517       Unit_Caller : Unit_Number_Type;
518       Unit_Callee : Unit_Number_Type;
519
520       Cunit_SC : Boolean := False;
521       --  Set to suppress dynamic elaboration checks where one of the
522       --  enclosing scopes has Elaboration_Checks_Suppressed set, or else
523       --  if a pragma Elaborate (_All) applies to that scope, in which case
524       --  warnings on the scope are also suppressed. For the internal case,
525       --  we ignore this flag.
526
527    begin
528       --  If the call is known to be within a local Suppress Elaboration
529       --  pragma, nothing to check. This can happen in task bodies.
530
531       if (Nkind (N) = N_Function_Call
532            or else Nkind (N) = N_Procedure_Call_Statement)
533         and then No_Elaboration_Check (N)
534       then
535          return;
536       end if;
537
538       --  Go to parent for derived subprogram, or to original subprogram in the
539       --  case of a renaming (Alias covers both these cases).
540
541       Ent := E;
542       loop
543          if (Suppress_Elaboration_Warnings (Ent)
544               or else Elaboration_Checks_Suppressed (Ent))
545            and then (Inst_Case or else No (Alias (Ent)))
546          then
547             return;
548          end if;
549
550          --  Nothing to do for imported entities
551
552          if Is_Imported (Ent) then
553             return;
554          end if;
555
556          exit when Inst_Case or else No (Alias (Ent));
557          Ent := Alias (Ent);
558       end loop;
559
560       Decl := Unit_Declaration_Node (Ent);
561
562       if Nkind (Decl) = N_Subprogram_Body then
563          Body_Acts_As_Spec := True;
564
565       elsif Nkind (Decl) = N_Subprogram_Declaration
566         or else Nkind (Decl) = N_Subprogram_Body_Stub
567         or else Inst_Case
568       then
569          Body_Acts_As_Spec := False;
570
571       --  If we have none of an instantiation, subprogram body or
572       --  subprogram declaration, then it is not a case that we want
573       --  to check. (One case is a call to a generic formal subprogram,
574       --  where we do not want the check in the template).
575
576       else
577          return;
578       end if;
579
580       E_Scope := Ent;
581       loop
582          if Elaboration_Checks_Suppressed (E_Scope)
583            or else Suppress_Elaboration_Warnings (E_Scope)
584          then
585             Cunit_SC := True;
586          end if;
587
588          --  Exit when we get to compilation unit, not counting subunits
589
590          exit when Is_Compilation_Unit (E_Scope)
591            and then (Is_Child_Unit (E_Scope)
592                        or else Scope (E_Scope) = Standard_Standard);
593
594          --  If we did not find a compilation unit, other than standard,
595          --  then nothing to check (happens in some instantiation cases)
596
597          if E_Scope = Standard_Standard then
598             return;
599
600          --  Otherwise move up a scope looking for compilation unit
601
602          else
603             E_Scope := Scope (E_Scope);
604          end if;
605       end loop;
606
607       --  No checks needed for pure or preelaborated compilation units
608
609       if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
610          return;
611       end if;
612
613       --  If the generic entity is within a deeper instance than we are, then
614       --  either the instantiation to which we refer itself caused an ABE, in
615       --  which case that will be handled separately, or else we know that the
616       --  body we need appears as needed at the point of the instantiation.
617       --  However, this assumption is only valid if we are in static mode.
618
619       if not Dynamic_Elaboration_Checks
620         and then Instantiation_Depth (Sloc (Ent)) >
621                  Instantiation_Depth (Sloc (N))
622       then
623          return;
624       end if;
625
626       --  Do not give a warning for a package with no body
627
628       if Ekind (Ent) = E_Generic_Package
629         and then not Has_Generic_Body (N)
630       then
631          return;
632       end if;
633
634       --  Case of entity is not in current unit (i.e. with'ed unit case)
635
636       if E_Scope /= C_Scope then
637
638          --  We are only interested in such calls if the outer call was from
639          --  elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
640
641          if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
642             return;
643          end if;
644
645          --  Nothing to do if some scope said that no checks were required
646
647          if Cunit_SC then
648             return;
649          end if;
650
651          --  Nothing to do for a generic instance, because in this case the
652          --  checking was at the point of instantiation of the generic However,
653          --  this shortcut is only applicable in static mode.
654
655          if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
656             return;
657          end if;
658
659          --  Nothing to do if subprogram with no separate spec. However, a
660          --  call to Deep_Initialize may result in a call to a user-defined
661          --  Initialize procedure, which imposes a body dependency. This
662          --  happens only if the type is controlled and the Initialize
663          --  procedure is not inherited.
664
665          if Body_Acts_As_Spec then
666             if Is_TSS (Ent, TSS_Deep_Initialize) then
667                declare
668                   Typ  : constant Entity_Id := Etype (First_Formal (Ent));
669                   Init : Entity_Id;
670
671                begin
672                   if not Is_Controlled (Typ) then
673                      return;
674                   else
675                      Init := Find_Prim_Op (Typ, Name_Initialize);
676
677                      if Comes_From_Source (Init) then
678                         Ent := Init;
679                      else
680                         return;
681                      end if;
682                   end if;
683                end;
684
685             else
686                return;
687             end if;
688          end if;
689
690          --  Check cases of internal units
691
692          Callee_Unit_Internal :=
693            Is_Internal_File_Name
694              (Unit_File_Name (Get_Source_Unit (E_Scope)));
695
696          --  Do not give a warning if the with'ed unit is internal
697          --  and this is the generic instantiation case (this saves a
698          --  lot of hassle dealing with the Text_IO special child units)
699
700          if Callee_Unit_Internal and Inst_Case then
701             return;
702          end if;
703
704          if C_Scope = Standard_Standard then
705             Caller_Unit_Internal := False;
706          else
707             Caller_Unit_Internal :=
708               Is_Internal_File_Name
709                 (Unit_File_Name (Get_Source_Unit (C_Scope)));
710          end if;
711
712          --  Do not give a warning if the with'ed unit is internal
713          --  and the caller is not internal (since the binder always
714          --  elaborates internal units first).
715
716          if Callee_Unit_Internal and (not Caller_Unit_Internal) then
717             return;
718          end if;
719
720          --  For now, if debug flag -gnatdE is not set, do no checking for
721          --  one internal unit withing another. This fixes the problem with
722          --  the sgi build and storage errors. To be resolved later ???
723
724          if (Callee_Unit_Internal and Caller_Unit_Internal)
725             and then not Debug_Flag_EE
726          then
727             return;
728          end if;
729
730          if Is_TSS (E, TSS_Deep_Initialize) then
731             Ent := E;
732          end if;
733
734          --  If the call is in an instance, and the called entity is not
735          --  defined in the same instance, then the elaboration issue
736          --  focuses around the unit containing the template, it is
737          --  this unit which requires an Elaborate_All.
738
739          --  However, if we are doing dynamic elaboration, we need to
740          --  chase the call in the usual manner.
741
742          --  We do not handle the case of calling a generic formal correctly
743          --  in the static case. See test 4703-004 to explore this gap ???
744
745          Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
746          Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
747
748          if Inst_Caller = No_Location then
749             Unit_Caller := No_Unit;
750          else
751             Unit_Caller := Get_Source_Unit (N);
752          end if;
753
754          if Inst_Callee = No_Location then
755             Unit_Callee := No_Unit;
756          else
757             Unit_Callee := Get_Source_Unit (Ent);
758          end if;
759
760          if Unit_Caller /= No_Unit
761            and then Unit_Callee /= Unit_Caller
762            and then not Dynamic_Elaboration_Checks
763          then
764             E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
765
766             --  If we don't get a spec entity, just ignore call. Not quite
767             --  clear why this check is necessary. ???
768
769             if No (E_Scope) then
770                return;
771             end if;
772
773             --  Otherwise step to enclosing compilation unit
774
775             while not Is_Compilation_Unit (E_Scope) loop
776                E_Scope := Scope (E_Scope);
777             end loop;
778
779          --  For the case N is not an instance, or a call within instance, we
780          --  recompute E_Scope for the error message, since we do NOT want to
781          --  go to the unit which has the ultimate declaration in the case of
782          --  renaming and derivation and we also want to go to the generic unit
783          --  in the case of an instance, and no further.
784
785          else
786             --  Loop to carefully follow renamings and derivations one step
787             --  outside the current unit, but not further.
788
789             if not Inst_Case
790               and then Present (Alias (Ent))
791             then
792                E_Scope := Alias (Ent);
793             else
794                E_Scope := Ent;
795             end if;
796
797             loop
798                while not Is_Compilation_Unit (E_Scope) loop
799                   E_Scope := Scope (E_Scope);
800                end loop;
801
802                --  If E_Scope is the same as C_Scope, it means that there
803                --  definitely was a local renaming or derivation, and we
804                --  are not yet out of the current unit.
805
806                exit when E_Scope /= C_Scope;
807                Ent := Alias (Ent);
808                E_Scope := Ent;
809
810                --  If no alias, there is a previous error
811
812                if No (Ent) then
813                   return;
814                end if;
815             end loop;
816          end if;
817
818          if Within_Elaborate_All (E_Scope) then
819             return;
820          end if;
821
822          --  Find top level scope for called entity (not following renamings
823          --  or derivations). This is where the Elaborate_All will go if it
824          --  is needed. We start with the called entity, except in the case
825          --  of an initialization procedure outside the current package, where
826          --  the init proc is in the root package, and we start from the entity
827          --  of the name in the call.
828
829          if Is_Entity_Name (Name (N))
830            and then Is_Init_Proc (Entity (Name (N)))
831            and then not In_Same_Extended_Unit (N, Entity (Name (N)))
832          then
833             W_Scope := Scope (Entity (Name (N)));
834          else
835             W_Scope := E;
836          end if;
837
838          while not Is_Compilation_Unit (W_Scope) loop
839             W_Scope := Scope (W_Scope);
840          end loop;
841
842          --  Now check if an elaborate_all (or dynamic check) is needed
843
844          if not Suppress_Elaboration_Warnings (Ent)
845            and then not Elaboration_Checks_Suppressed (Ent)
846            and then not Suppress_Elaboration_Warnings (E_Scope)
847            and then not Elaboration_Checks_Suppressed (E_Scope)
848            and then Elab_Warnings
849            and then Generate_Warnings
850          then
851             Generate_Elab_Warnings : declare
852                procedure Elab_Warning
853                  (Msg_D : String;
854                   Msg_S : String;
855                   Ent   : Node_Or_Entity_Id);
856                --  Generate a call to Error_Msg_NE with parameters Msg_D or
857                --  Msg_S (for dynamic or static elaboration model), N and Ent.
858
859                ------------------
860                -- Elab_Warning --
861                ------------------
862
863                procedure Elab_Warning
864                  (Msg_D : String;
865                   Msg_S : String;
866                   Ent   : Node_Or_Entity_Id)
867                is
868                begin
869                   if Dynamic_Elaboration_Checks then
870                      Error_Msg_NE (Msg_D, N, Ent);
871                   else
872                      Error_Msg_NE (Msg_S, N, Ent);
873                   end if;
874                end Elab_Warning;
875
876             --  Start of processing for Generate_Elab_Warnings
877
878             begin
879                if Inst_Case then
880                   Elab_Warning
881                     ("instantiation of& may raise Program_Error?",
882                      "info: instantiation of& during elaboration?", Ent);
883
884                else
885                   if Nkind (Name (N)) in N_Has_Entity
886                     and then Is_Init_Proc (Entity (Name (N)))
887                     and then Comes_From_Source (Ent)
888                   then
889                      Elab_Warning
890                        ("implicit call to & may raise Program_Error?",
891                         "info: implicit call to & during elaboration?",
892                         Ent);
893
894                   else
895                      Elab_Warning
896                        ("call to & may raise Program_Error?",
897                         "info: call to & during elaboration?",
898                         Ent);
899                   end if;
900                end if;
901
902                Error_Msg_Qual_Level := Nat'Last;
903
904                if Nkind (N) in N_Subprogram_Instantiation then
905                   Elab_Warning
906                     ("\missing pragma Elaborate for&?",
907                      "\info: implicit pragma Elaborate for& generated?",
908                      W_Scope);
909                else
910                   Elab_Warning
911                     ("\missing pragma Elaborate_All for&?",
912                      "\info: implicit pragma Elaborate_All for & generated?",
913                      W_Scope);
914                end if;
915             end Generate_Elab_Warnings;
916
917             Error_Msg_Qual_Level := 0;
918             Output_Calls (N);
919
920             --  Set flag to prevent further warnings for same unit unless in
921             --  All_Errors_Mode.
922
923             if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
924                Set_Suppress_Elaboration_Warnings (W_Scope, True);
925             end if;
926          end if;
927
928          --  Check for runtime elaboration check required
929
930          if Dynamic_Elaboration_Checks then
931             if not Elaboration_Checks_Suppressed (Ent)
932               and then not Elaboration_Checks_Suppressed (W_Scope)
933               and then not Elaboration_Checks_Suppressed (E_Scope)
934               and then not Cunit_SC
935             then
936                --  Runtime elaboration check required. Generate check of the
937                --  elaboration Boolean for the unit containing the entity.
938
939                --  Note that for this case, we do check the real unit (the one
940                --  from following renamings, since that is the issue!)
941
942                --  Could this possibly miss a useless but required PE???
943
944                Insert_Elab_Check (N,
945                  Make_Attribute_Reference (Loc,
946                    Attribute_Name => Name_Elaborated,
947                    Prefix => New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
948
949                --  Prevent duplicate elaboration checks on the same call,
950                --  which can happen if the body enclosing the call appears
951                --  itself in a call whose elaboration check is delayed.
952
953                if Nkind_In (N, N_Function_Call,
954                                N_Procedure_Call_Statement)
955                then
956                   Set_No_Elaboration_Check (N);
957                end if;
958             end if;
959
960          --  Case of static elaboration model
961
962          else
963             --  Do not do anything if elaboration checks suppressed. Note that
964             --  we check Ent here, not E, since we want the real entity for the
965             --  body to see if checks are suppressed for it, not the dummy
966             --  entry for renamings or derivations.
967
968             if Elaboration_Checks_Suppressed (Ent)
969               or else Elaboration_Checks_Suppressed (E_Scope)
970               or else Elaboration_Checks_Suppressed (W_Scope)
971             then
972                null;
973
974             --  Do not generate an Elaborate_All for finalization routines
975             --  which perform partial clean up as part of initialization.
976
977             elsif In_Init_Proc
978               and then Is_Finalization_Procedure (Ent)
979             then
980                null;
981
982             --  Here we need to generate an implicit elaborate all
983
984             else
985                --  Generate elaborate_all warning unless suppressed
986
987                if (Elab_Warnings and Generate_Warnings and not Inst_Case)
988                  and then not Suppress_Elaboration_Warnings (Ent)
989                  and then not Suppress_Elaboration_Warnings (E_Scope)
990                  and then not Suppress_Elaboration_Warnings (W_Scope)
991                then
992                   Error_Msg_Node_2 := W_Scope;
993                   Error_Msg_NE
994                     ("call to& in elaboration code " &
995                      "requires pragma Elaborate_All on&?", N, E);
996                end if;
997
998                --  Set indication for binder to generate Elaborate_All
999
1000                Set_Elaboration_Constraint (N, E, W_Scope);
1001             end if;
1002          end if;
1003
1004       --  Case of entity is in same unit as call or instantiation
1005
1006       elsif not Inter_Unit_Only then
1007          Check_Internal_Call (N, Ent, Outer_Scope, E);
1008       end if;
1009    end Check_A_Call;
1010
1011    -----------------------------
1012    -- Check_Bad_Instantiation --
1013    -----------------------------
1014
1015    procedure Check_Bad_Instantiation (N : Node_Id) is
1016       Ent : Entity_Id;
1017
1018    begin
1019       --  Nothing to do if we do not have an instantiation (happens in some
1020       --  error cases, and also in the formal package declaration case)
1021
1022       if Nkind (N) not in N_Generic_Instantiation then
1023          return;
1024
1025       --  Nothing to do if serious errors detected (avoid cascaded errors)
1026
1027       elsif Serious_Errors_Detected /= 0 then
1028          return;
1029
1030       --  Nothing to do if not in full analysis mode
1031
1032       elsif not Full_Analysis then
1033          return;
1034
1035       --  Nothing to do if inside a generic template
1036
1037       elsif Inside_A_Generic then
1038          return;
1039
1040       --  Nothing to do if a library level instantiation
1041
1042       elsif Nkind (Parent (N)) = N_Compilation_Unit then
1043          return;
1044
1045       --  Nothing to do if we are compiling a proper body for semantic
1046       --  purposes only. The generic body may be in another proper body.
1047
1048       elsif
1049         Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1050       then
1051          return;
1052       end if;
1053
1054       Ent := Get_Generic_Entity (N);
1055
1056       --  The case we are interested in is when the generic spec is in the
1057       --  current declarative part
1058
1059       if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1060         or else not In_Same_Extended_Unit (N, Ent)
1061       then
1062          return;
1063       end if;
1064
1065       --  If the generic entity is within a deeper instance than we are, then
1066       --  either the instantiation to which we refer itself caused an ABE, in
1067       --  which case that will be handled separately. Otherwise, we know that
1068       --  the body we need appears as needed at the point of the instantiation.
1069       --  If they are both at the same level but not within the same instance
1070       --  then the body of the generic will be in the earlier instance.
1071
1072       declare
1073          D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1074          D2 : constant Int := Instantiation_Depth (Sloc (N));
1075
1076       begin
1077          if D1 > D2 then
1078             return;
1079
1080          elsif D1 = D2
1081            and then Is_Generic_Instance (Scope (Ent))
1082            and then not In_Open_Scopes (Scope (Ent))
1083          then
1084             return;
1085          end if;
1086       end;
1087
1088       --  Now we can proceed, if the entity being called has a completion,
1089       --  then we are definitely OK, since we have already seen the body.
1090
1091       if Has_Completion (Ent) then
1092          return;
1093       end if;
1094
1095       --  If there is no body, then nothing to do
1096
1097       if not Has_Generic_Body (N) then
1098          return;
1099       end if;
1100
1101       --  Here we definitely have a bad instantiation
1102
1103       Error_Msg_NE
1104         ("?cannot instantiate& before body seen", N, Ent);
1105
1106       if Present (Instance_Spec (N)) then
1107          Supply_Bodies (Instance_Spec (N));
1108       end if;
1109
1110       Error_Msg_N
1111         ("\?Program_Error will be raised at run time", N);
1112       Insert_Elab_Check (N);
1113       Set_ABE_Is_Certain (N);
1114    end Check_Bad_Instantiation;
1115
1116    ---------------------
1117    -- Check_Elab_Call --
1118    ---------------------
1119
1120    procedure Check_Elab_Call
1121      (N            : Node_Id;
1122       Outer_Scope  : Entity_Id := Empty;
1123       In_Init_Proc : Boolean := False)
1124    is
1125       Ent : Entity_Id;
1126       P   : Node_Id;
1127
1128       function Get_Called_Ent return Entity_Id;
1129       --  Retrieve called entity. If this is a call to a protected subprogram,
1130       --  entity is a selected component. The callable entity may be absent,
1131       --  in which case there is no check to perform. This happens with
1132       --  non-analyzed calls in nested generics.
1133
1134       --------------------
1135       -- Get_Called_Ent --
1136       --------------------
1137
1138       function Get_Called_Ent return Entity_Id is
1139          Nam : Node_Id;
1140
1141       begin
1142          Nam := Name (N);
1143
1144          if No (Nam) then
1145             return Empty;
1146
1147          elsif Nkind (Nam) = N_Selected_Component then
1148             return Entity (Selector_Name (Nam));
1149
1150          elsif not Is_Entity_Name (Nam) then
1151             return Empty;
1152
1153          else
1154             return Entity (Nam);
1155          end if;
1156       end Get_Called_Ent;
1157
1158    --  Start of processing for Check_Elab_Call
1159
1160    begin
1161       --  If the call does not come from the main unit, there is nothing to
1162       --  check. Elaboration call from units in the context of the main unit
1163       --  will lead to semantic dependencies when those units are compiled.
1164
1165       if not In_Extended_Main_Code_Unit (N) then
1166          return;
1167       end if;
1168
1169       --  For an entry call, check relevant restriction
1170
1171       if Nkind (N) = N_Entry_Call_Statement
1172          and then not In_Subprogram_Or_Concurrent_Unit
1173       then
1174          Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1175
1176       --  Nothing to do if this is not a call (happens in some error
1177       --  conditions, and in some cases where rewriting occurs).
1178
1179       elsif Nkind (N) /= N_Function_Call
1180         and then Nkind (N) /= N_Procedure_Call_Statement
1181       then
1182          return;
1183
1184       --  Nothing to do if this is a call already rewritten for elab checking
1185
1186       elsif Nkind (Parent (N)) = N_Conditional_Expression then
1187          return;
1188
1189       --  Nothing to do if inside a generic template
1190
1191       elsif Inside_A_Generic
1192         and then No (Enclosing_Generic_Body (N))
1193       then
1194          return;
1195       end if;
1196
1197       --  Here we have a call at elaboration time which must be checked
1198
1199       if Debug_Flag_LL then
1200          Write_Str ("  Check_Elab_Call: ");
1201
1202          if No (Name (N))
1203            or else not Is_Entity_Name (Name (N))
1204          then
1205             Write_Str ("<<not entity name>> ");
1206          else
1207             Write_Name (Chars (Entity (Name (N))));
1208          end if;
1209
1210          Write_Str ("  call at ");
1211          Write_Location (Sloc (N));
1212          Write_Eol;
1213       end if;
1214
1215       --  Climb up the tree to make sure we are not inside default expression
1216       --  of a parameter specification or a record component, since in both
1217       --  these cases, we will be doing the actual call later, not now, and it
1218       --  is at the time of the actual call (statically speaking) that we must
1219       --  do our static check, not at the time of its initial analysis).
1220
1221       --  However, we have to check calls within component definitions (e.g.
1222       --  a function call that determines an array component bound), so we
1223       --  terminate the loop in that case.
1224
1225       P := Parent (N);
1226       while Present (P) loop
1227          if Nkind (P) = N_Parameter_Specification
1228               or else
1229             Nkind (P) = N_Component_Declaration
1230          then
1231             return;
1232
1233          --  The call occurs within the constraint of a component,
1234          --  so it must be checked.
1235
1236          elsif Nkind (P) = N_Component_Definition then
1237             exit;
1238
1239          else
1240             P := Parent (P);
1241          end if;
1242       end loop;
1243
1244       --  Stuff that happens only at the outer level
1245
1246       if No (Outer_Scope) then
1247          Elab_Visited.Set_Last (0);
1248
1249          --  Nothing to do if current scope is Standard (this is a bit odd, but
1250          --  it happens in the case of generic instantiations).
1251
1252          C_Scope := Current_Scope;
1253
1254          if C_Scope = Standard_Standard then
1255             return;
1256          end if;
1257
1258          --  First case, we are in elaboration code
1259
1260          From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1261          if From_Elab_Code then
1262
1263             --  Complain if call that comes from source in preelaborated unit
1264             --  and we are not inside a subprogram (i.e. we are in elab code).
1265
1266             if Comes_From_Source (N)
1267               and then In_Preelaborated_Unit
1268               and then not In_Inlined_Body
1269             then
1270                --  This is a warning in GNAT mode allowing such calls to be
1271                --  used in the predefined library with appropriate care.
1272
1273                Error_Msg_Warn := GNAT_Mode;
1274                Error_Msg_N
1275                  ("<non-static call not allowed in preelaborated unit", N);
1276                return;
1277             end if;
1278
1279          --  Second case, we are inside a subprogram or concurrent unit, which
1280          --  means we are not in elaboration code.
1281
1282          else
1283             --  In this case, the issue is whether we are inside the
1284             --  declarative part of the unit in which we live, or inside its
1285             --  statements. In the latter case, there is no issue of ABE calls
1286             --  at this level (a call from outside to the unit in which we live
1287             --  might cause an ABE, but that will be detected when we analyze
1288             --  that outer level call, as it recurses into the called unit).
1289
1290             --  Climb up the tree, doing this test, and also testing for being
1291             --  inside a default expression, which, as discussed above, is not
1292             --  checked at this stage.
1293
1294             declare
1295                P : Node_Id;
1296                L : List_Id;
1297
1298             begin
1299                P := N;
1300                loop
1301                   --  If we find a parentless subtree, it seems safe to assume
1302                   --  that we are not in a declarative part and that no
1303                   --  checking is required.
1304
1305                   if No (P) then
1306                      return;
1307                   end if;
1308
1309                   if Is_List_Member (P) then
1310                      L := List_Containing (P);
1311                      P := Parent (L);
1312                   else
1313                      L := No_List;
1314                      P := Parent (P);
1315                   end if;
1316
1317                   exit when Nkind (P) = N_Subunit;
1318
1319                   --  Filter out case of default expressions, where we do not
1320                   --  do the check at this stage.
1321
1322                   if Nkind (P) = N_Parameter_Specification
1323                        or else
1324                      Nkind (P) = N_Component_Declaration
1325                   then
1326                      return;
1327                   end if;
1328
1329                   --  A protected body has no elaboration code and contains
1330                   --  only other bodies.
1331
1332                   if Nkind (P) = N_Protected_Body then
1333                      return;
1334
1335                   elsif Nkind (P) = N_Subprogram_Body
1336                        or else
1337                      Nkind (P) = N_Task_Body
1338                        or else
1339                      Nkind (P) = N_Block_Statement
1340                        or else
1341                      Nkind (P) = N_Entry_Body
1342                   then
1343                      if L = Declarations (P) then
1344                         exit;
1345
1346                      --  We are not in elaboration code, but we are doing
1347                      --  dynamic elaboration checks, in this case, we still
1348                      --  need to do the call, since the subprogram we are in
1349                      --  could be called from another unit, also in dynamic
1350                      --  elaboration check mode, at elaboration time.
1351
1352                      elsif Dynamic_Elaboration_Checks then
1353
1354                         --  This is a rather new check, going into version
1355                         --  3.14a1 for the first time (V1.80 of this unit), so
1356                         --  we provide a debug flag to enable it. That way we
1357                         --  have an easy work around for regressions that are
1358                         --  caused by this new check. This debug flag can be
1359                         --  removed later.
1360
1361                         if Debug_Flag_DD then
1362                            return;
1363                         end if;
1364
1365                         --  Do the check in this case
1366
1367                         exit;
1368
1369                      elsif Nkind (P) = N_Task_Body then
1370
1371                         --  The check is deferred until Check_Task_Activation
1372                         --  but we need to capture local suppress pragmas
1373                         --  that may inhibit checks on this call.
1374
1375                         Ent := Get_Called_Ent;
1376
1377                         if No (Ent) then
1378                            return;
1379
1380                         elsif Elaboration_Checks_Suppressed (Current_Scope)
1381                           or else Elaboration_Checks_Suppressed (Ent)
1382                           or else Elaboration_Checks_Suppressed (Scope (Ent))
1383                         then
1384                            Set_No_Elaboration_Check (N);
1385                         end if;
1386
1387                         return;
1388
1389                      --  Static model, call is not in elaboration code, we
1390                      --  never need to worry, because in the static model the
1391                      --  top level caller always takes care of things.
1392
1393                      else
1394                         return;
1395                      end if;
1396                   end if;
1397                end loop;
1398             end;
1399          end if;
1400       end if;
1401
1402       Ent := Get_Called_Ent;
1403
1404       if No (Ent) then
1405          return;
1406       end if;
1407
1408       --  Nothing to do if this is a recursive call (i.e. a call to
1409       --  an entity that is already in the Elab_Call stack)
1410
1411       for J in 1 .. Elab_Visited.Last loop
1412          if Ent = Elab_Visited.Table (J) then
1413             return;
1414          end if;
1415       end loop;
1416
1417       --  See if we need to analyze this call. We analyze it if either of
1418       --  the following conditions is met:
1419
1420       --    It is an inner level call (since in this case it was triggered
1421       --    by an outer level call from elaboration code), but only if the
1422       --    call is within the scope of the original outer level call.
1423
1424       --    It is an outer level call from elaboration code, or the called
1425       --    entity is in the same elaboration scope.
1426
1427       --  And in these cases, we will check both inter-unit calls and
1428       --  intra-unit (within a single unit) calls.
1429
1430       C_Scope := Current_Scope;
1431
1432       --  If not outer level call, then we follow it if it is within the
1433       --  original scope of the outer call.
1434
1435       if Present (Outer_Scope)
1436         and then Within (Scope (Ent), Outer_Scope)
1437       then
1438          Set_C_Scope;
1439          Check_A_Call
1440            (N               => N,
1441             E               => Ent,
1442             Outer_Scope     => Outer_Scope,
1443             Inter_Unit_Only => False,
1444             In_Init_Proc    => In_Init_Proc);
1445
1446       elsif Elaboration_Checks_Suppressed (Current_Scope) then
1447          null;
1448
1449       elsif From_Elab_Code then
1450          Set_C_Scope;
1451          Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1452
1453       elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1454          Set_C_Scope;
1455          Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1456
1457       --  If none of those cases holds, but Dynamic_Elaboration_Checks mode
1458       --  is set, then we will do the check, but only in the inter-unit case
1459       --  (this is to accommodate unguarded elaboration calls from other units
1460       --  in which this same mode is set). We don't want warnings in this case,
1461       --  it would generate warnings having nothing to do with elaboration.
1462
1463       elsif Dynamic_Elaboration_Checks then
1464          Set_C_Scope;
1465          Check_A_Call
1466            (N,
1467             Ent,
1468             Standard_Standard,
1469             Inter_Unit_Only   => True,
1470             Generate_Warnings => False);
1471
1472       --  Otherwise nothing to do
1473
1474       else
1475          return;
1476       end if;
1477
1478       --  A call to an Init_Proc in elaboration code may bring additional
1479       --  dependencies, if some of the record components thereof have
1480       --  initializations that are function calls that come from source. We
1481       --  treat the current node as a call to each of these functions, to check
1482       --  their elaboration impact.
1483
1484       if Is_Init_Proc (Ent)
1485         and then From_Elab_Code
1486       then
1487          Process_Init_Proc : declare
1488             Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1489
1490             function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1491             --  Find subprogram calls within body of Init_Proc for Traverse
1492             --  instantiation below.
1493
1494             procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1495             --  Traversal procedure to find all calls with body of Init_Proc
1496
1497             ---------------------
1498             -- Check_Init_Call --
1499             ---------------------
1500
1501             function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1502                Func : Entity_Id;
1503
1504             begin
1505                if (Nkind (Nod) = N_Function_Call
1506                     or else Nkind (Nod) = N_Procedure_Call_Statement)
1507                  and then Is_Entity_Name (Name (Nod))
1508                then
1509                   Func := Entity (Name (Nod));
1510
1511                   if Comes_From_Source (Func) then
1512                      Check_A_Call
1513                        (N, Func, Standard_Standard, Inter_Unit_Only => True);
1514                   end if;
1515
1516                   return OK;
1517
1518                else
1519                   return OK;
1520                end if;
1521             end Check_Init_Call;
1522
1523          --  Start of processing for Process_Init_Proc
1524
1525          begin
1526             if Nkind (Unit_Decl) = N_Subprogram_Body then
1527                Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1528             end if;
1529          end Process_Init_Proc;
1530       end if;
1531    end Check_Elab_Call;
1532
1533    -----------------------
1534    -- Check_Elab_Assign --
1535    -----------------------
1536
1537    procedure Check_Elab_Assign (N : Node_Id) is
1538       Ent  : Entity_Id;
1539       Scop : Entity_Id;
1540
1541       Pkg_Spec : Entity_Id;
1542       Pkg_Body : Entity_Id;
1543
1544    begin
1545       --  For record or array component, check prefix. If it is an access type,
1546       --  then there is nothing to do (we do not know what is being assigned),
1547       --  but otherwise this is an assignment to the prefix.
1548
1549       if Nkind (N) = N_Indexed_Component
1550            or else
1551          Nkind (N) = N_Selected_Component
1552            or else
1553          Nkind (N) = N_Slice
1554       then
1555          if not Is_Access_Type (Etype (Prefix (N))) then
1556             Check_Elab_Assign (Prefix (N));
1557          end if;
1558
1559          return;
1560       end if;
1561
1562       --  For type conversion, check expression
1563
1564       if Nkind (N) = N_Type_Conversion then
1565          Check_Elab_Assign (Expression (N));
1566          return;
1567       end if;
1568
1569       --  Nothing to do if this is not an entity reference otherwise get entity
1570
1571       if Is_Entity_Name (N) then
1572          Ent := Entity (N);
1573       else
1574          return;
1575       end if;
1576
1577       --  What we are looking for is a reference in the body of a package that
1578       --  modifies a variable declared in the visible part of the package spec.
1579
1580       if Present (Ent)
1581         and then Comes_From_Source (N)
1582         and then not Suppress_Elaboration_Warnings (Ent)
1583         and then Ekind (Ent) = E_Variable
1584         and then not In_Private_Part (Ent)
1585         and then Is_Library_Level_Entity (Ent)
1586       then
1587          Scop := Current_Scope;
1588          loop
1589             if No (Scop) or else Scop = Standard_Standard then
1590                return;
1591             elsif Ekind (Scop) = E_Package
1592               and then Is_Compilation_Unit (Scop)
1593             then
1594                exit;
1595             else
1596                Scop := Scope (Scop);
1597             end if;
1598          end loop;
1599
1600          --  Here Scop points to the containing library package
1601
1602          Pkg_Spec := Scop;
1603          Pkg_Body := Body_Entity (Pkg_Spec);
1604
1605          --  All OK if the package has an Elaborate_Body pragma
1606
1607          if Has_Pragma_Elaborate_Body (Scop) then
1608             return;
1609          end if;
1610
1611          --  OK if entity being modified is not in containing package spec
1612
1613          if not In_Same_Source_Unit (Scop, Ent) then
1614             return;
1615          end if;
1616
1617          --  All OK if entity appears in generic package or generic instance.
1618          --  We just get too messed up trying to give proper warnings in the
1619          --  presence of generics. Better no message than a junk one.
1620
1621          Scop := Scope (Ent);
1622          while Present (Scop) and then Scop /= Pkg_Spec loop
1623             if Ekind (Scop) = E_Generic_Package then
1624                return;
1625             elsif Ekind (Scop) = E_Package
1626               and then Is_Generic_Instance (Scop)
1627             then
1628                return;
1629             end if;
1630
1631             Scop := Scope (Scop);
1632          end loop;
1633
1634          --  All OK if in task, don't issue warnings there
1635
1636          if In_Task_Activation then
1637             return;
1638          end if;
1639
1640          --  OK if no package body
1641
1642          if No (Pkg_Body) then
1643             return;
1644          end if;
1645
1646          --  OK if reference is not in package body
1647
1648          if not In_Same_Source_Unit (Pkg_Body, N) then
1649             return;
1650          end if;
1651
1652          --  OK if package body has no handled statement sequence
1653
1654          declare
1655             HSS : constant Node_Id :=
1656                     Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1657          begin
1658             if No (HSS) or else not Comes_From_Source (HSS) then
1659                return;
1660             end if;
1661          end;
1662
1663          --  We definitely have a case of a modification of an entity in
1664          --  the package spec from the elaboration code of the package body.
1665          --  We may not give the warning (because there are some additional
1666          --  checks to avoid too many false positives), but it would be a good
1667          --  idea for the binder to try to keep the body elaboration close to
1668          --  the spec elaboration.
1669
1670          Set_Elaborate_Body_Desirable (Pkg_Spec);
1671
1672          --  All OK in gnat mode (we know what we are doing)
1673
1674          if GNAT_Mode then
1675             return;
1676          end if;
1677
1678          --  All OK if all warnings suppressed
1679
1680          if Warning_Mode = Suppress then
1681             return;
1682          end if;
1683
1684          --  All OK if elaboration checks suppressed for entity
1685
1686          if Checks_May_Be_Suppressed (Ent)
1687            and then Is_Check_Suppressed (Ent, Elaboration_Check)
1688          then
1689             return;
1690          end if;
1691
1692          --  OK if the entity is initialized. Note that the No_Initialization
1693          --  flag usually means that the initialization has been rewritten into
1694          --  assignments, but that still counts for us.
1695
1696          declare
1697             Decl : constant Node_Id := Declaration_Node (Ent);
1698          begin
1699             if Nkind (Decl) = N_Object_Declaration
1700               and then (Present (Expression (Decl))
1701                           or else No_Initialization (Decl))
1702             then
1703                return;
1704             end if;
1705          end;
1706
1707          --  Here is where we give the warning
1708
1709          --  All OK if warnings suppressed on the entity
1710
1711          if not Has_Warnings_Off (Ent) then
1712             Error_Msg_Sloc := Sloc (Ent);
1713
1714             Error_Msg_NE
1715               ("?elaboration code may access& before it is initialized",
1716                N, Ent);
1717             Error_Msg_NE
1718               ("\?suggest adding pragma Elaborate_Body to spec of &",
1719                N, Scop);
1720             Error_Msg_N
1721               ("\?or an explicit initialization could be added #", N);
1722          end if;
1723
1724          if not All_Errors_Mode then
1725             Set_Suppress_Elaboration_Warnings (Ent);
1726          end if;
1727       end if;
1728    end Check_Elab_Assign;
1729
1730    ----------------------
1731    -- Check_Elab_Calls --
1732    ----------------------
1733
1734    procedure Check_Elab_Calls is
1735    begin
1736       --  If expansion is disabled, do not generate any checks. Also skip
1737       --  checks if any subunits are missing because in either case we lack the
1738       --  full information that we need, and no object file will be created in
1739       --  any case.
1740
1741       if not Expander_Active
1742         or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1743         or else Subunits_Missing
1744       then
1745          return;
1746       end if;
1747
1748       --  Skip delayed calls if we had any errors
1749
1750       if Serious_Errors_Detected = 0 then
1751          Delaying_Elab_Checks := False;
1752          Expander_Mode_Save_And_Set (True);
1753
1754          for J in Delay_Check.First .. Delay_Check.Last loop
1755             Push_Scope (Delay_Check.Table (J).Curscop);
1756             From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1757
1758             Check_Internal_Call_Continue (
1759               N           => Delay_Check.Table (J).N,
1760               E           => Delay_Check.Table (J).E,
1761               Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1762               Orig_Ent    => Delay_Check.Table (J).Orig_Ent);
1763
1764             Pop_Scope;
1765          end loop;
1766
1767          --  Set Delaying_Elab_Checks back on for next main compilation
1768
1769          Expander_Mode_Restore;
1770          Delaying_Elab_Checks := True;
1771       end if;
1772    end Check_Elab_Calls;
1773
1774    ------------------------------
1775    -- Check_Elab_Instantiation --
1776    ------------------------------
1777
1778    procedure Check_Elab_Instantiation
1779      (N           : Node_Id;
1780       Outer_Scope : Entity_Id := Empty)
1781    is
1782       Ent : Entity_Id;
1783
1784    begin
1785       --  Check for and deal with bad instantiation case. There is some
1786       --  duplicated code here, but we will worry about this later ???
1787
1788       Check_Bad_Instantiation (N);
1789
1790       if ABE_Is_Certain (N) then
1791          return;
1792       end if;
1793
1794       --  Nothing to do if we do not have an instantiation (happens in some
1795       --  error cases, and also in the formal package declaration case)
1796
1797       if Nkind (N) not in N_Generic_Instantiation then
1798          return;
1799       end if;
1800
1801       --  Nothing to do if inside a generic template
1802
1803       if Inside_A_Generic then
1804          return;
1805       end if;
1806
1807       --  Nothing to do if the instantiation is not in the main unit
1808
1809       if not In_Extended_Main_Code_Unit (N) then
1810          return;
1811       end if;
1812
1813       Ent := Get_Generic_Entity (N);
1814       From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1815
1816       --  See if we need to analyze this instantiation. We analyze it if
1817       --  either of the following conditions is met:
1818
1819       --    It is an inner level instantiation (since in this case it was
1820       --    triggered by an outer level call from elaboration code), but
1821       --    only if the instantiation is within the scope of the original
1822       --    outer level call.
1823
1824       --    It is an outer level instantiation from elaboration code, or the
1825       --    instantiated entity is in the same elaboration scope.
1826
1827       --  And in these cases, we will check both the inter-unit case and
1828       --  the intra-unit (within a single unit) case.
1829
1830       C_Scope := Current_Scope;
1831
1832       if Present (Outer_Scope)
1833         and then Within (Scope (Ent), Outer_Scope)
1834       then
1835          Set_C_Scope;
1836          Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1837
1838       elsif From_Elab_Code then
1839          Set_C_Scope;
1840          Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1841
1842       elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1843          Set_C_Scope;
1844          Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1845
1846       --  If none of those cases holds, but Dynamic_Elaboration_Checks mode is
1847       --  set, then we will do the check, but only in the inter-unit case (this
1848       --  is to accommodate unguarded elaboration calls from other units in
1849       --  which this same mode is set). We inhibit warnings in this case, since
1850       --  this instantiation is not occurring in elaboration code.
1851
1852       elsif Dynamic_Elaboration_Checks then
1853          Set_C_Scope;
1854          Check_A_Call
1855            (N,
1856             Ent,
1857             Standard_Standard,
1858             Inter_Unit_Only => True,
1859             Generate_Warnings => False);
1860
1861       else
1862          return;
1863       end if;
1864    end Check_Elab_Instantiation;
1865
1866    -------------------------
1867    -- Check_Internal_Call --
1868    -------------------------
1869
1870    procedure Check_Internal_Call
1871      (N           : Node_Id;
1872       E           : Entity_Id;
1873       Outer_Scope : Entity_Id;
1874       Orig_Ent    : Entity_Id)
1875    is
1876       Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1877
1878    begin
1879       --  If not function or procedure call or instantiation, then ignore
1880       --  call (this happens in some error case and rewriting cases)
1881
1882       if Nkind (N) /= N_Function_Call
1883            and then
1884          Nkind (N) /= N_Procedure_Call_Statement
1885            and then
1886          not Inst_Case
1887       then
1888          return;
1889
1890       --  Nothing to do if this is a call or instantiation that has
1891       --  already been found to be a sure ABE
1892
1893       elsif ABE_Is_Certain (N) then
1894          return;
1895
1896       --  Nothing to do if errors already detected (avoid cascaded errors)
1897
1898       elsif Serious_Errors_Detected /= 0 then
1899          return;
1900
1901       --  Nothing to do if not in full analysis mode
1902
1903       elsif not Full_Analysis then
1904          return;
1905
1906       --  Nothing to do if analyzing in special spec-expression mode, since the
1907       --  call is not actually being made at this time.
1908
1909       elsif In_Spec_Expression then
1910          return;
1911
1912       --  Nothing to do for call to intrinsic subprogram
1913
1914       elsif Is_Intrinsic_Subprogram (E) then
1915          return;
1916
1917       --  No need to trace local calls if checking task activation, because
1918       --  other local bodies are elaborated already.
1919
1920       elsif In_Task_Activation then
1921          return;
1922
1923       --  Nothing to do if call is within a generic unit
1924
1925       elsif Inside_A_Generic then
1926          return;
1927       end if;
1928
1929       --  Delay this call if we are still delaying calls
1930
1931       if Delaying_Elab_Checks then
1932          Delay_Check.Append (
1933            (N              => N,
1934             E              => E,
1935             Orig_Ent       => Orig_Ent,
1936             Curscop        => Current_Scope,
1937             Outer_Scope    => Outer_Scope,
1938             From_Elab_Code => From_Elab_Code));
1939          return;
1940
1941       --  Otherwise, call phase 2 continuation right now
1942
1943       else
1944          Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1945       end if;
1946    end Check_Internal_Call;
1947
1948    ----------------------------------
1949    -- Check_Internal_Call_Continue --
1950    ----------------------------------
1951
1952    procedure Check_Internal_Call_Continue
1953      (N           : Node_Id;
1954       E           : Entity_Id;
1955       Outer_Scope : Entity_Id;
1956       Orig_Ent    : Entity_Id)
1957    is
1958       Loc       : constant Source_Ptr := Sloc (N);
1959       Inst_Case : constant Boolean := Is_Generic_Unit (E);
1960
1961       Sbody : Node_Id;
1962       Ebody : Entity_Id;
1963
1964       function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
1965       --  Function applied to each node as we traverse the body. Checks for
1966       --  call or entity reference that needs checking, and if so checks it.
1967       --  Always returns OK, so entire tree is traversed, except that as
1968       --  described below subprogram bodies are skipped for now.
1969
1970       procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
1971       --  Traverse procedure using above Find_Elab_Reference function
1972
1973       -------------------------
1974       -- Find_Elab_Reference --
1975       -------------------------
1976
1977       function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
1978          Actual : Node_Id;
1979
1980       begin
1981          --  If user has specified that there are no entry calls in elaboration
1982          --  code, do not trace past an accept statement, because the rendez-
1983          --  vous will happen after elaboration.
1984
1985          if (Nkind (Original_Node (N)) = N_Accept_Statement
1986               or else Nkind (Original_Node (N)) = N_Selective_Accept)
1987            and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1988          then
1989             return Abandon;
1990
1991             --  If we have a function call, check it
1992
1993          elsif Nkind (N) = N_Function_Call then
1994             Check_Elab_Call (N, Outer_Scope);
1995             return OK;
1996
1997          --  If we have a procedure call, check the call, and also check
1998          --  arguments that are assignments (OUT or IN OUT mode formals).
1999
2000          elsif Nkind (N) = N_Procedure_Call_Statement then
2001             Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2002
2003             Actual := First_Actual (N);
2004             while Present (Actual) loop
2005                if Known_To_Be_Assigned (Actual) then
2006                   Check_Elab_Assign (Actual);
2007                end if;
2008
2009                Next_Actual (Actual);
2010             end loop;
2011
2012             return OK;
2013
2014          --  If we have a generic instantiation, check it
2015
2016          elsif Nkind (N) in N_Generic_Instantiation then
2017             Check_Elab_Instantiation (N, Outer_Scope);
2018             return OK;
2019
2020          --  Skip subprogram bodies that come from source (wait for call to
2021          --  analyze these). The reason for the come from source test is to
2022          --  avoid catching task bodies.
2023
2024          --  For task bodies, we should really avoid these too, waiting for the
2025          --  task activation, but that's too much trouble to catch for now, so
2026          --  we go in unconditionally. This is not so terrible, it means the
2027          --  error backtrace is not quite complete, and we are too eager to
2028          --  scan bodies of tasks that are unused, but this is hardly very
2029          --  significant!
2030
2031          elsif Nkind (N) = N_Subprogram_Body
2032            and then Comes_From_Source (N)
2033          then
2034             return Skip;
2035
2036          elsif Nkind (N) = N_Assignment_Statement
2037            and then Comes_From_Source (N)
2038          then
2039             Check_Elab_Assign (Name (N));
2040             return OK;
2041
2042          else
2043             return OK;
2044          end if;
2045       end Find_Elab_Reference;
2046
2047    --  Start of processing for Check_Internal_Call_Continue
2048
2049    begin
2050       --  Save outer level call if at outer level
2051
2052       if Elab_Call.Last = 0 then
2053          Outer_Level_Sloc := Loc;
2054       end if;
2055
2056       Elab_Visited.Append (E);
2057
2058       --  If the call is to a function that renames a literal, no check
2059       --  is needed.
2060
2061       if Ekind (E) = E_Enumeration_Literal then
2062          return;
2063       end if;
2064
2065       Sbody := Unit_Declaration_Node (E);
2066
2067       if Nkind (Sbody) /= N_Subprogram_Body
2068            and then
2069          Nkind (Sbody) /= N_Package_Body
2070       then
2071          Ebody := Corresponding_Body (Sbody);
2072
2073          if No (Ebody) then
2074             return;
2075          else
2076             Sbody := Unit_Declaration_Node (Ebody);
2077          end if;
2078       end if;
2079
2080       --  If the body appears after the outer level call or instantiation then
2081       --  we have an error case handled below.
2082
2083       if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2084         and then not In_Task_Activation
2085       then
2086          null;
2087
2088       --  If we have the instantiation case we are done, since we now
2089       --  know that the body of the generic appeared earlier.
2090
2091       elsif Inst_Case then
2092          return;
2093
2094       --  Otherwise we have a call, so we trace through the called body to see
2095       --  if it has any problems.
2096
2097       else
2098          pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2099
2100          Elab_Call.Append ((Cloc => Loc, Ent => E));
2101
2102          if Debug_Flag_LL then
2103             Write_Str ("Elab_Call.Last = ");
2104             Write_Int (Int (Elab_Call.Last));
2105             Write_Str ("   Ent = ");
2106             Write_Name (Chars (E));
2107             Write_Str ("   at ");
2108             Write_Location (Sloc (N));
2109             Write_Eol;
2110          end if;
2111
2112          --  Now traverse declarations and statements of subprogram body. Note
2113          --  that we cannot simply Traverse (Sbody), since traverse does not
2114          --  normally visit subprogram bodies.
2115
2116          declare
2117             Decl : Node_Id;
2118          begin
2119             Decl := First (Declarations (Sbody));
2120             while Present (Decl) loop
2121                Traverse (Decl);
2122                Next (Decl);
2123             end loop;
2124          end;
2125
2126          Traverse (Handled_Statement_Sequence (Sbody));
2127
2128          Elab_Call.Decrement_Last;
2129          return;
2130       end if;
2131
2132       --  Here is the case of calling a subprogram where the body has not yet
2133       --  been encountered, a warning message is needed.
2134
2135       --  If we have nothing in the call stack, then this is at the outer
2136       --  level, and the ABE is bound to occur.
2137
2138       if Elab_Call.Last = 0 then
2139          if Inst_Case then
2140             Error_Msg_NE
2141               ("?cannot instantiate& before body seen", N, Orig_Ent);
2142          else
2143             Error_Msg_NE
2144               ("?cannot call& before body seen", N, Orig_Ent);
2145          end if;
2146
2147          Error_Msg_N
2148            ("\?Program_Error will be raised at run time", N);
2149          Insert_Elab_Check (N);
2150
2151       --  Call is not at outer level
2152
2153       else
2154          --  Deal with dynamic elaboration check
2155
2156          if not Elaboration_Checks_Suppressed (E) then
2157             Set_Elaboration_Entity_Required (E);
2158
2159             --  Case of no elaboration entity allocated yet
2160
2161             if No (Elaboration_Entity (E)) then
2162
2163                --  Create object declaration for elaboration entity, and put it
2164                --  just in front of the spec of the subprogram or generic unit,
2165                --  in the same scope as this unit.
2166
2167                declare
2168                   Loce : constant Source_Ptr := Sloc (E);
2169                   Ent  : constant Entity_Id  :=
2170                            Make_Defining_Identifier (Loc,
2171                              Chars => New_External_Name (Chars (E), 'E'));
2172
2173                begin
2174                   Set_Elaboration_Entity (E, Ent);
2175                   Push_Scope (Scope (E));
2176
2177                   Insert_Action (Declaration_Node (E),
2178                     Make_Object_Declaration (Loce,
2179                       Defining_Identifier => Ent,
2180                       Object_Definition   =>
2181                         New_Occurrence_Of (Standard_Short_Integer, Loce),
2182                       Expression          =>
2183                         Make_Integer_Literal (Loc, Uint_0)));
2184
2185                   --  Set elaboration flag at the point of the body
2186
2187                   Set_Elaboration_Flag (Sbody, E);
2188
2189                   --  Kill current value indication. This is necessary because
2190                   --  the tests of this flag are inserted out of sequence and
2191                   --  must not pick up bogus indications of the wrong constant
2192                   --  value. Also, this is never a true constant, since one way
2193                   --  or another, it gets reset.
2194
2195                   Set_Current_Value    (Ent, Empty);
2196                   Set_Last_Assignment  (Ent, Empty);
2197                   Set_Is_True_Constant (Ent, False);
2198                   Pop_Scope;
2199                end;
2200             end if;
2201
2202             --  Generate check of the elaboration counter
2203
2204             Insert_Elab_Check (N,
2205                Make_Attribute_Reference (Loc,
2206                  Attribute_Name => Name_Elaborated,
2207                  Prefix         => New_Occurrence_Of (E, Loc)));
2208          end if;
2209
2210          --  Generate the warning
2211
2212          if not Suppress_Elaboration_Warnings (E)
2213            and then not Elaboration_Checks_Suppressed (E)
2214          then
2215             if Inst_Case then
2216                Error_Msg_NE
2217                  ("instantiation of& may occur before body is seen?",
2218                   N, Orig_Ent);
2219             else
2220                Error_Msg_NE
2221                  ("call to& may occur before body is seen?", N, Orig_Ent);
2222             end if;
2223
2224             Error_Msg_N
2225               ("\Program_Error may be raised at run time?", N);
2226
2227             Output_Calls (N);
2228          end if;
2229       end if;
2230
2231       --  Set flag to suppress further warnings on same subprogram
2232       --  unless in all errors mode
2233
2234       if not All_Errors_Mode then
2235          Set_Suppress_Elaboration_Warnings (E);
2236       end if;
2237    end Check_Internal_Call_Continue;
2238
2239    ---------------------------
2240    -- Check_Task_Activation --
2241    ---------------------------
2242
2243    procedure Check_Task_Activation (N : Node_Id) is
2244       Loc         : constant Source_Ptr := Sloc (N);
2245       Inter_Procs : constant Elist_Id   := New_Elmt_List;
2246       Intra_Procs : constant Elist_Id   := New_Elmt_List;
2247       Ent         : Entity_Id;
2248       P           : Entity_Id;
2249       Task_Scope  : Entity_Id;
2250       Cunit_SC    : Boolean := False;
2251       Decl        : Node_Id;
2252       Elmt        : Elmt_Id;
2253       Enclosing   : Entity_Id;
2254
2255       procedure Add_Task_Proc (Typ : Entity_Id);
2256       --  Add to Task_Procs the task body procedure(s) of task types in Typ.
2257       --  For record types, this procedure recurses over component types.
2258
2259       procedure Collect_Tasks (Decls : List_Id);
2260       --  Collect the types of the tasks that are to be activated in the given
2261       --  list of declarations, in order to perform elaboration checks on the
2262       --  corresponding task procedures which are called implicitly here.
2263
2264       function Outer_Unit (E : Entity_Id) return Entity_Id;
2265       --  find enclosing compilation unit of Entity, ignoring subunits, or
2266       --  else enclosing subprogram. If E is not a package, there is no need
2267       --  for inter-unit elaboration checks.
2268
2269       -------------------
2270       -- Add_Task_Proc --
2271       -------------------
2272
2273       procedure Add_Task_Proc (Typ : Entity_Id) is
2274          Comp : Entity_Id;
2275          Proc : Entity_Id := Empty;
2276
2277       begin
2278          if Is_Task_Type (Typ) then
2279             Proc := Get_Task_Body_Procedure (Typ);
2280
2281          elsif Is_Array_Type (Typ)
2282            and then Has_Task (Base_Type (Typ))
2283          then
2284             Add_Task_Proc (Component_Type (Typ));
2285
2286          elsif Is_Record_Type (Typ)
2287            and then Has_Task (Base_Type (Typ))
2288          then
2289             Comp := First_Component (Typ);
2290             while Present (Comp) loop
2291                Add_Task_Proc (Etype (Comp));
2292                Comp := Next_Component (Comp);
2293             end loop;
2294          end if;
2295
2296          --  If the task type is another unit, we will perform the usual
2297          --  elaboration check on its enclosing unit. If the type is in the
2298          --  same unit, we can trace the task body as for an internal call,
2299          --  but we only need to examine other external calls, because at
2300          --  the point the task is activated, internal subprogram bodies
2301          --  will have been elaborated already. We keep separate lists for
2302          --  each kind of task.
2303
2304          --  Skip this test if errors have occurred, since in this case
2305          --  we can get false indications.
2306
2307          if Serious_Errors_Detected /= 0 then
2308             return;
2309          end if;
2310
2311          if Present (Proc) then
2312             if Outer_Unit (Scope (Proc)) = Enclosing then
2313
2314                if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2315                  and then
2316                    (not Is_Generic_Instance (Scope (Proc))
2317                       or else
2318                     Scope (Proc) = Scope (Defining_Identifier (Decl)))
2319                then
2320                   Error_Msg_N
2321                     ("task will be activated before elaboration of its body?",
2322                       Decl);
2323                   Error_Msg_N
2324                     ("\Program_Error will be raised at run time?", Decl);
2325
2326                elsif
2327                  Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
2328                then
2329                   Append_Elmt (Proc, Intra_Procs);
2330                end if;
2331
2332             else
2333                --  No need for multiple entries of the same type
2334
2335                Elmt := First_Elmt (Inter_Procs);
2336                while Present (Elmt) loop
2337                   if Node (Elmt) = Proc then
2338                      return;
2339                   end if;
2340
2341                   Next_Elmt (Elmt);
2342                end loop;
2343
2344                Append_Elmt (Proc, Inter_Procs);
2345             end if;
2346          end if;
2347       end Add_Task_Proc;
2348
2349       -------------------
2350       -- Collect_Tasks --
2351       -------------------
2352
2353       procedure Collect_Tasks (Decls : List_Id) is
2354       begin
2355          if Present (Decls) then
2356             Decl := First (Decls);
2357             while Present (Decl) loop
2358                if Nkind (Decl) = N_Object_Declaration
2359                  and then Has_Task (Etype (Defining_Identifier (Decl)))
2360                then
2361                   Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2362                end if;
2363
2364                Next (Decl);
2365             end loop;
2366          end if;
2367       end Collect_Tasks;
2368
2369       ----------------
2370       -- Outer_Unit --
2371       ----------------
2372
2373       function Outer_Unit (E : Entity_Id) return Entity_Id is
2374          Outer : Entity_Id;
2375
2376       begin
2377          Outer := E;
2378          while Present (Outer) loop
2379             if Elaboration_Checks_Suppressed (Outer) then
2380                Cunit_SC := True;
2381             end if;
2382
2383             exit when Is_Child_Unit (Outer)
2384               or else Scope (Outer) = Standard_Standard
2385               or else Ekind (Outer) /= E_Package;
2386             Outer := Scope (Outer);
2387          end loop;
2388
2389          return Outer;
2390       end Outer_Unit;
2391
2392    --  Start of processing for Check_Task_Activation
2393
2394    begin
2395       Enclosing := Outer_Unit (Current_Scope);
2396
2397       --  Find all tasks declared in the current unit
2398
2399       if Nkind (N) = N_Package_Body then
2400          P := Unit_Declaration_Node (Corresponding_Spec (N));
2401
2402          Collect_Tasks (Declarations (N));
2403          Collect_Tasks (Visible_Declarations (Specification (P)));
2404          Collect_Tasks (Private_Declarations (Specification (P)));
2405
2406       elsif Nkind (N) = N_Package_Declaration then
2407          Collect_Tasks (Visible_Declarations (Specification (N)));
2408          Collect_Tasks (Private_Declarations (Specification (N)));
2409
2410       else
2411          Collect_Tasks (Declarations (N));
2412       end if;
2413
2414       --  We only perform detailed checks in all tasks are library level
2415       --  entities. If the master is a subprogram or task, activation will
2416       --  depend on the activation of the master itself.
2417
2418       --  Should dynamic checks be added in the more general case???
2419
2420       if Ekind (Enclosing) /= E_Package then
2421          return;
2422       end if;
2423
2424       --  For task types defined in other units, we want the unit containing
2425       --  the task body to be elaborated before the current one.
2426
2427       Elmt := First_Elmt (Inter_Procs);
2428       while Present (Elmt) loop
2429          Ent := Node (Elmt);
2430          Task_Scope := Outer_Unit (Scope (Ent));
2431
2432          if not Is_Compilation_Unit (Task_Scope) then
2433             null;
2434
2435          elsif Suppress_Elaboration_Warnings (Task_Scope)
2436            or else Elaboration_Checks_Suppressed (Task_Scope)
2437          then
2438             null;
2439
2440          elsif Dynamic_Elaboration_Checks then
2441             if not Elaboration_Checks_Suppressed (Ent)
2442               and then not Cunit_SC
2443               and then
2444                 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2445             then
2446                --  Runtime elaboration check required. Generate check of the
2447                --  elaboration counter for the unit containing the entity.
2448
2449                Insert_Elab_Check (N,
2450                  Make_Attribute_Reference (Loc,
2451                    Attribute_Name => Name_Elaborated,
2452                    Prefix =>
2453                      New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2454             end if;
2455
2456          else
2457             --  Force the binder to elaborate other unit first
2458
2459             if not Suppress_Elaboration_Warnings (Ent)
2460               and then not Elaboration_Checks_Suppressed (Ent)
2461               and then Elab_Warnings
2462               and then not Suppress_Elaboration_Warnings (Task_Scope)
2463               and then not Elaboration_Checks_Suppressed (Task_Scope)
2464             then
2465                Error_Msg_Node_2 := Task_Scope;
2466                Error_Msg_NE
2467                  ("activation of an instance of task type&" &
2468                   " requires pragma Elaborate_All on &?", N, Ent);
2469             end if;
2470
2471             Activate_Elaborate_All_Desirable (N, Task_Scope);
2472             Set_Suppress_Elaboration_Warnings (Task_Scope);
2473          end if;
2474
2475          Next_Elmt (Elmt);
2476       end loop;
2477
2478       --  For tasks declared in the current unit, trace other calls within
2479       --  the task procedure bodies, which are available.
2480
2481       In_Task_Activation := True;
2482
2483       Elmt := First_Elmt (Intra_Procs);
2484       while Present (Elmt) loop
2485          Ent := Node (Elmt);
2486          Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2487          Next_Elmt (Elmt);
2488       end loop;
2489
2490       In_Task_Activation := False;
2491    end Check_Task_Activation;
2492
2493    --------------------------------
2494    -- Set_Elaboration_Constraint --
2495    --------------------------------
2496
2497    procedure Set_Elaboration_Constraint
2498     (Call : Node_Id;
2499      Subp : Entity_Id;
2500      Scop : Entity_Id)
2501    is
2502       Elab_Unit  : Entity_Id;
2503       Init_Call  : constant Boolean :=
2504                      Chars (Subp) = Name_Initialize
2505                        and then Comes_From_Source (Subp)
2506                        and then Present (Parameter_Associations (Call))
2507                        and then Is_Controlled (Etype (First_Actual (Call)));
2508    begin
2509       --  If the unit is mentioned in a with_clause of the current unit, it is
2510       --  visible, and we can set the elaboration flag.
2511
2512       if Is_Immediately_Visible (Scop)
2513         or else (Is_Child_Unit (Scop) and then Is_Visible_Child_Unit (Scop))
2514       then
2515          Activate_Elaborate_All_Desirable (Call, Scop);
2516          Set_Suppress_Elaboration_Warnings (Scop, True);
2517          return;
2518       end if;
2519
2520       --  If this is not an initialization call or a call using object notation
2521       --  we know that the unit of the called entity is in the context, and
2522       --  we can set the flag as well. The unit need not be visible if the call
2523       --  occurs within an instantiation.
2524
2525       if Is_Init_Proc (Subp)
2526         or else Init_Call
2527         or else Nkind (Original_Node (Call)) = N_Selected_Component
2528       then
2529          null;  --  detailed processing follows.
2530
2531       else
2532          Activate_Elaborate_All_Desirable (Call, Scop);
2533          Set_Suppress_Elaboration_Warnings (Scop, True);
2534          return;
2535       end if;
2536
2537       --  If the unit is not in the context, there must be an intermediate unit
2538       --  that is, on which we need to place to elaboration flag. This happens
2539       --  with init proc calls.
2540
2541       if Is_Init_Proc (Subp)
2542         or else Init_Call
2543       then
2544          --  The initialization call is on an object whose type is not declared
2545          --  in the same scope as the subprogram. The type of the object must
2546          --  be a subtype of the type of operation. This object is the first
2547          --  actual in the call.
2548
2549          declare
2550             Typ : constant Entity_Id :=
2551                     Etype (First (Parameter_Associations (Call)));
2552          begin
2553             Elab_Unit := Scope (Typ);
2554             while (Present (Elab_Unit))
2555               and then not Is_Compilation_Unit (Elab_Unit)
2556             loop
2557                Elab_Unit := Scope (Elab_Unit);
2558             end loop;
2559          end;
2560
2561       --  If original node uses selected component notation, the prefix is
2562       --  visible and determines the scope that must be elaborated. After
2563       --  rewriting, the prefix is the first actual in the call.
2564
2565       elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2566          Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2567
2568       --  Not one of special cases above
2569
2570       else
2571          --  Using previously computed scope. If the elaboration check is
2572          --  done after analysis, the scope is not visible any longer, but
2573          --  must still be in the context.
2574
2575          Elab_Unit := Scop;
2576       end if;
2577
2578       Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2579       Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2580    end Set_Elaboration_Constraint;
2581
2582    ----------------------
2583    -- Has_Generic_Body --
2584    ----------------------
2585
2586    function Has_Generic_Body (N : Node_Id) return Boolean is
2587       Ent  : constant Entity_Id := Get_Generic_Entity (N);
2588       Decl : constant Node_Id   := Unit_Declaration_Node (Ent);
2589       Scop : Entity_Id;
2590
2591       function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2592       --  Determine if the list of nodes headed by N and linked by Next
2593       --  contains a package body for the package spec entity E, and if so
2594       --  return the package body. If not, then returns Empty.
2595
2596       function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2597       --  This procedure is called load the unit whose name is given by Nam.
2598       --  This unit is being loaded to see whether it contains an optional
2599       --  generic body. The returned value is the loaded unit, which is always
2600       --  a package body (only package bodies can contain other entities in the
2601       --  sense in which Has_Generic_Body is interested). We only attempt to
2602       --  load bodies if we are generating code. If we are in semantics check
2603       --  only mode, then it would be wrong to load bodies that are not
2604       --  required from a semantic point of view, so in this case we return
2605       --  Empty. The result is that the caller may incorrectly decide that a
2606       --  generic spec does not have a body when in fact it does, but the only
2607       --  harm in this is that some warnings on elaboration problems may be
2608       --  lost in semantic checks only mode, which is not big loss. We also
2609       --  return Empty if we go for a body and it is not there.
2610
2611       function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2612       --  PE is the entity for a package spec. This function locates the
2613       --  corresponding package body, returning Empty if none is found. The
2614       --  package body returned is fully parsed but may not yet be analyzed,
2615       --  so only syntactic fields should be referenced.
2616
2617       ------------------
2618       -- Find_Body_In --
2619       ------------------
2620
2621       function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2622          Nod : Node_Id;
2623
2624       begin
2625          Nod := N;
2626          while Present (Nod) loop
2627
2628             --  If we found the package body we are looking for, return it
2629
2630             if Nkind (Nod) = N_Package_Body
2631               and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2632             then
2633                return Nod;
2634
2635             --  If we found the stub for the body, go after the subunit,
2636             --  loading it if necessary.
2637
2638             elsif Nkind (Nod) = N_Package_Body_Stub
2639               and then Chars (Defining_Identifier (Nod)) = Chars (E)
2640             then
2641                if Present (Library_Unit (Nod)) then
2642                   return Unit (Library_Unit (Nod));
2643
2644                else
2645                   return Load_Package_Body (Get_Unit_Name (Nod));
2646                end if;
2647
2648             --  If neither package body nor stub, keep looking on chain
2649
2650             else
2651                Next (Nod);
2652             end if;
2653          end loop;
2654
2655          return Empty;
2656       end Find_Body_In;
2657
2658       -----------------------
2659       -- Load_Package_Body --
2660       -----------------------
2661
2662       function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2663          U : Unit_Number_Type;
2664
2665       begin
2666          if Operating_Mode /= Generate_Code then
2667             return Empty;
2668          else
2669             U :=
2670               Load_Unit
2671                 (Load_Name  => Nam,
2672                  Required   => False,
2673                  Subunit    => False,
2674                  Error_Node => N);
2675
2676             if U = No_Unit then
2677                return Empty;
2678             else
2679                return Unit (Cunit (U));
2680             end if;
2681          end if;
2682       end Load_Package_Body;
2683
2684       -------------------------------
2685       -- Locate_Corresponding_Body --
2686       -------------------------------
2687
2688       function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2689          Spec  : constant Node_Id   := Declaration_Node (PE);
2690          Decl  : constant Node_Id   := Parent (Spec);
2691          Scop  : constant Entity_Id := Scope (PE);
2692          PBody : Node_Id;
2693
2694       begin
2695          if Is_Library_Level_Entity (PE) then
2696
2697             --  If package is a library unit that requires a body, we have no
2698             --  choice but to go after that body because it might contain an
2699             --  optional body for the original generic package.
2700
2701             if Unit_Requires_Body (PE) then
2702
2703                --  Load the body. Note that we are a little careful here to use
2704                --  Spec to get the unit number, rather than PE or Decl, since
2705                --  in the case where the package is itself a library level
2706                --  instantiation, Spec will properly reference the generic
2707                --  template, which is what we really want.
2708
2709                return
2710                  Load_Package_Body
2711                    (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2712
2713             --  But if the package is a library unit that does NOT require
2714             --  a body, then no body is permitted, so we are sure that there
2715             --  is no body for the original generic package.
2716
2717             else
2718                return Empty;
2719             end if;
2720
2721          --  Otherwise look and see if we are embedded in a further package
2722
2723          elsif Is_Package_Or_Generic_Package (Scop) then
2724
2725             --  If so, get the body of the enclosing package, and look in
2726             --  its package body for the package body we are looking for.
2727
2728             PBody := Locate_Corresponding_Body (Scop);
2729
2730             if No (PBody) then
2731                return Empty;
2732             else
2733                return Find_Body_In (PE, First (Declarations (PBody)));
2734             end if;
2735
2736          --  If we are not embedded in a further package, then the body
2737          --  must be in the same declarative part as we are.
2738
2739          else
2740             return Find_Body_In (PE, Next (Decl));
2741          end if;
2742       end Locate_Corresponding_Body;
2743
2744    --  Start of processing for Has_Generic_Body
2745
2746    begin
2747       if Present (Corresponding_Body (Decl)) then
2748          return True;
2749
2750       elsif Unit_Requires_Body (Ent) then
2751          return True;
2752
2753       --  Compilation units cannot have optional bodies
2754
2755       elsif Is_Compilation_Unit (Ent) then
2756          return False;
2757
2758       --  Otherwise look at what scope we are in
2759
2760       else
2761          Scop := Scope (Ent);
2762
2763          --  Case of entity is in other than a package spec, in this case
2764          --  the body, if present, must be in the same declarative part.
2765
2766          if not Is_Package_Or_Generic_Package (Scop) then
2767             declare
2768                P : Node_Id;
2769
2770             begin
2771                --  Declaration node may get us a spec, so if so, go to
2772                --  the parent declaration.
2773
2774                P := Declaration_Node (Ent);
2775                while not Is_List_Member (P) loop
2776                   P := Parent (P);
2777                end loop;
2778
2779                return Present (Find_Body_In (Ent, Next (P)));
2780             end;
2781
2782          --  If the entity is in a package spec, then we have to locate
2783          --  the corresponding package body, and look there.
2784
2785          else
2786             declare
2787                PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2788
2789             begin
2790                if No (PBody) then
2791                   return False;
2792                else
2793                   return
2794                     Present
2795                       (Find_Body_In (Ent, (First (Declarations (PBody)))));
2796                end if;
2797             end;
2798          end if;
2799       end if;
2800    end Has_Generic_Body;
2801
2802    -----------------------
2803    -- Insert_Elab_Check --
2804    -----------------------
2805
2806    procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2807       Nod : Node_Id;
2808       Loc : constant Source_Ptr := Sloc (N);
2809
2810    begin
2811       --  If expansion is disabled, do not generate any checks. Also
2812       --  skip checks if any subunits are missing because in either
2813       --  case we lack the full information that we need, and no object
2814       --  file will be created in any case.
2815
2816       if not Expander_Active or else Subunits_Missing then
2817          return;
2818       end if;
2819
2820       --  If we have a generic instantiation, where Instance_Spec is set,
2821       --  then this field points to a generic instance spec that has
2822       --  been inserted before the instantiation node itself, so that
2823       --  is where we want to insert a check.
2824
2825       if Nkind (N) in N_Generic_Instantiation
2826         and then Present (Instance_Spec (N))
2827       then
2828          Nod := Instance_Spec (N);
2829       else
2830          Nod := N;
2831       end if;
2832
2833       --  If we are inserting at the top level, insert in Aux_Decls
2834
2835       if Nkind (Parent (Nod)) = N_Compilation_Unit then
2836          declare
2837             ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2838             R   : Node_Id;
2839
2840          begin
2841             if No (C) then
2842                R :=
2843                  Make_Raise_Program_Error (Loc,
2844                    Reason => PE_Access_Before_Elaboration);
2845             else
2846                R :=
2847                  Make_Raise_Program_Error (Loc,
2848                    Condition => Make_Op_Not (Loc, C),
2849                    Reason    => PE_Access_Before_Elaboration);
2850             end if;
2851
2852             if No (Declarations (ADN)) then
2853                Set_Declarations (ADN, New_List (R));
2854             else
2855                Append_To (Declarations (ADN), R);
2856             end if;
2857
2858             Analyze (R);
2859          end;
2860
2861       --  Otherwise just insert before the node in question. However, if
2862       --  the context of the call has already been analyzed, an insertion
2863       --  will not work if it depends on subsequent expansion (e.g. a call in
2864       --  a branch of a short-circuit). In that case we replace the call with
2865       --  a conditional expression, or with a Raise if it is unconditional.
2866       --  Unfortunately this does not work if the call has a dynamic size,
2867       --  because gigi regards it as a dynamic-sized temporary. If such a call
2868       --  appears in a short-circuit expression, the elaboration check will be
2869       --  missed (rare enough ???). Otherwise, the code below inserts the check
2870       --  at the appropriate place before the call. Same applies in the even
2871       --  rarer case the return type has a known size but is unconstrained.
2872
2873       else
2874          if Nkind (N) = N_Function_Call
2875            and then Analyzed (Parent (N))
2876            and then Size_Known_At_Compile_Time (Etype (N))
2877            and then
2878             (not Has_Discriminants (Etype (N))
2879               or else Is_Constrained (Etype (N)))
2880
2881          then
2882             declare
2883                Typ : constant Entity_Id := Etype (N);
2884                Chk : constant Boolean   := Do_Range_Check (N);
2885
2886                R  : constant Node_Id :=
2887                       Make_Raise_Program_Error (Loc,
2888                          Reason => PE_Access_Before_Elaboration);
2889
2890                Reloc_N : Node_Id;
2891
2892             begin
2893                Set_Etype (R, Typ);
2894
2895                if No (C) then
2896                   Rewrite (N, R);
2897
2898                else
2899                   Reloc_N := Relocate_Node (N);
2900                   Save_Interps (N, Reloc_N);
2901                   Rewrite (N,
2902                     Make_Conditional_Expression (Loc,
2903                       Expressions => New_List (C, Reloc_N, R)));
2904                end if;
2905
2906                Analyze_And_Resolve (N, Typ);
2907
2908                --  If the original call requires a range check, so does the
2909                --  conditional expression.
2910
2911                if Chk then
2912                   Enable_Range_Check (N);
2913                else
2914                   Set_Do_Range_Check (N, False);
2915                end if;
2916             end;
2917
2918          else
2919             if No (C) then
2920                Insert_Action (Nod,
2921                   Make_Raise_Program_Error (Loc,
2922                     Reason => PE_Access_Before_Elaboration));
2923             else
2924                Insert_Action (Nod,
2925                   Make_Raise_Program_Error (Loc,
2926                     Condition =>
2927                       Make_Op_Not (Loc,
2928                         Right_Opnd => C),
2929                     Reason => PE_Access_Before_Elaboration));
2930             end if;
2931          end if;
2932       end if;
2933    end Insert_Elab_Check;
2934
2935    -------------------------------
2936    -- Is_Finalization_Procedure --
2937    -------------------------------
2938
2939    function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
2940    begin
2941       --  Check whether Id is a procedure with at least one parameter
2942
2943       if Ekind (Id) = E_Procedure
2944         and then Present (First_Formal (Id))
2945       then
2946          declare
2947             Typ      : constant Entity_Id := Etype (First_Formal (Id));
2948             Deep_Fin : Entity_Id := Empty;
2949             Fin      : Entity_Id := Empty;
2950
2951          begin
2952             --  If the type of the first formal does not require finalization
2953             --  actions, then this is definitely not [Deep_]Finalize.
2954
2955             if not Needs_Finalization (Typ) then
2956                return False;
2957             end if;
2958
2959             --  At this point we have the following scenario:
2960
2961             --    procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
2962
2963             --  Recover the two possible versions of [Deep_]Finalize using the
2964             --  type of the first parameter and compare with the input.
2965
2966             Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
2967
2968             if Is_Controlled (Typ) then
2969                Fin := Find_Prim_Op (Typ, Name_Finalize);
2970             end if;
2971
2972             return
2973                 (Present (Deep_Fin) and then Id = Deep_Fin)
2974               or else
2975                 (Present (Fin) and then Id = Fin);
2976          end;
2977       end if;
2978
2979       return False;
2980    end Is_Finalization_Procedure;
2981
2982    ------------------
2983    -- Output_Calls --
2984    ------------------
2985
2986    procedure Output_Calls (N : Node_Id) is
2987       Ent : Entity_Id;
2988
2989       function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
2990       --  An internal function, used to determine if a name, Nm, is either
2991       --  a non-internal name, or is an internal name that is printable
2992       --  by the error message circuits (i.e. it has a single upper
2993       --  case letter at the end).
2994
2995       function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
2996       begin
2997          if not Is_Internal_Name (Nm) then
2998             return True;
2999
3000          elsif Name_Len = 1 then
3001             return False;
3002
3003          else
3004             Name_Len := Name_Len - 1;
3005             return not Is_Internal_Name;
3006          end if;
3007       end Is_Printable_Error_Name;
3008
3009    --  Start of processing for Output_Calls
3010
3011    begin
3012       for J in reverse 1 .. Elab_Call.Last loop
3013          Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3014
3015          Ent := Elab_Call.Table (J).Ent;
3016
3017          if Is_Generic_Unit (Ent) then
3018             Error_Msg_NE ("\?& instantiated #", N, Ent);
3019
3020          elsif Is_Init_Proc (Ent) then
3021             Error_Msg_N ("\?initialization procedure called #", N);
3022
3023          elsif Is_Printable_Error_Name (Chars (Ent)) then
3024             Error_Msg_NE ("\?& called #", N, Ent);
3025
3026          else
3027             Error_Msg_N ("\? called #", N);
3028          end if;
3029       end loop;
3030    end Output_Calls;
3031
3032    ----------------------------
3033    -- Same_Elaboration_Scope --
3034    ----------------------------
3035
3036    function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3037       S1 : Entity_Id;
3038       S2 : Entity_Id;
3039
3040    begin
3041       --  Find elaboration scope for Scop1
3042       --  This is either a subprogram or a compilation unit.
3043
3044       S1 := Scop1;
3045       while S1 /= Standard_Standard
3046         and then not Is_Compilation_Unit (S1)
3047         and then (Ekind (S1) = E_Package
3048                     or else
3049                   Ekind (S1) = E_Protected_Type
3050                     or else
3051                   Ekind (S1) = E_Block)
3052       loop
3053          S1 := Scope (S1);
3054       end loop;
3055
3056       --  Find elaboration scope for Scop2
3057
3058       S2 := Scop2;
3059       while S2 /= Standard_Standard
3060         and then not Is_Compilation_Unit (S2)
3061         and then (Ekind (S2) = E_Package
3062                     or else
3063                   Ekind (S2) = E_Protected_Type
3064                     or else
3065                   Ekind (S2) = E_Block)
3066       loop
3067          S2 := Scope (S2);
3068       end loop;
3069
3070       return S1 = S2;
3071    end Same_Elaboration_Scope;
3072
3073    -----------------
3074    -- Set_C_Scope --
3075    -----------------
3076
3077    procedure Set_C_Scope is
3078    begin
3079       while not Is_Compilation_Unit (C_Scope) loop
3080          C_Scope := Scope (C_Scope);
3081       end loop;
3082    end Set_C_Scope;
3083
3084    -----------------
3085    -- Spec_Entity --
3086    -----------------
3087
3088    function Spec_Entity (E : Entity_Id) return Entity_Id is
3089       Decl : Node_Id;
3090
3091    begin
3092       --  Check for case of body entity
3093       --  Why is the check for E_Void needed???
3094
3095       if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3096          Decl := E;
3097
3098          loop
3099             Decl := Parent (Decl);
3100             exit when Nkind (Decl) in N_Proper_Body;
3101          end loop;
3102
3103          return Corresponding_Spec (Decl);
3104
3105       else
3106          return E;
3107       end if;
3108    end Spec_Entity;
3109
3110    -------------------
3111    -- Supply_Bodies --
3112    -------------------
3113
3114    procedure Supply_Bodies (N : Node_Id) is
3115    begin
3116       if Nkind (N) = N_Subprogram_Declaration then
3117          declare
3118             Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3119          begin
3120
3121             --  Internal subprograms will already have a generated body, so
3122             --  there is no need to provide a stub for them.
3123
3124             if No (Corresponding_Body (N)) then
3125                declare
3126                   Loc     : constant Source_Ptr := Sloc (N);
3127                   B       : Node_Id;
3128                   Formals : constant List_Id := Copy_Parameter_List (Ent);
3129                   Nam     : constant Entity_Id :=
3130                               Make_Defining_Identifier (Loc, Chars (Ent));
3131                   Spec    : Node_Id;
3132                   Stats   : constant List_Id :=
3133                               New_List
3134                                (Make_Raise_Program_Error (Loc,
3135                                   Reason => PE_Access_Before_Elaboration));
3136
3137                begin
3138                   if Ekind (Ent) = E_Function then
3139                      Spec :=
3140                         Make_Function_Specification (Loc,
3141                           Defining_Unit_Name => Nam,
3142                           Parameter_Specifications => Formals,
3143                           Result_Definition =>
3144                             New_Copy_Tree
3145                               (Result_Definition (Specification (N))));
3146
3147                      --  We cannot reliably make a return statement for this
3148                      --  body, but none is needed because the call raises
3149                      --  program error.
3150
3151                      Set_Return_Present (Ent);
3152
3153                   else
3154                      Spec :=
3155                         Make_Procedure_Specification (Loc,
3156                           Defining_Unit_Name => Nam,
3157                           Parameter_Specifications => Formals);
3158                   end if;
3159
3160                   B := Make_Subprogram_Body (Loc,
3161                           Specification => Spec,
3162                           Declarations => New_List,
3163                           Handled_Statement_Sequence =>
3164                             Make_Handled_Sequence_Of_Statements (Loc,  Stats));
3165                   Insert_After (N, B);
3166                   Analyze (B);
3167                end;
3168             end if;
3169          end;
3170
3171       elsif Nkind (N) = N_Package_Declaration then
3172          declare
3173             Spec : constant Node_Id := Specification (N);
3174          begin
3175             Push_Scope (Defining_Unit_Name (Spec));
3176             Supply_Bodies (Visible_Declarations (Spec));
3177             Supply_Bodies (Private_Declarations (Spec));
3178             Pop_Scope;
3179          end;
3180       end if;
3181    end Supply_Bodies;
3182
3183    procedure Supply_Bodies (L : List_Id) is
3184       Elmt : Node_Id;
3185    begin
3186       if Present (L) then
3187          Elmt := First (L);
3188          while Present (Elmt) loop
3189             Supply_Bodies (Elmt);
3190             Next (Elmt);
3191          end loop;
3192       end if;
3193    end Supply_Bodies;
3194
3195    ------------
3196    -- Within --
3197    ------------
3198
3199    function Within (E1, E2 : Entity_Id) return Boolean is
3200       Scop : Entity_Id;
3201    begin
3202       Scop := E1;
3203       loop
3204          if Scop = E2 then
3205             return True;
3206          elsif Scop = Standard_Standard then
3207             return False;
3208          else
3209             Scop := Scope (Scop);
3210          end if;
3211       end loop;
3212    end Within;
3213
3214    --------------------------
3215    -- Within_Elaborate_All --
3216    --------------------------
3217
3218    function Within_Elaborate_All (E : Entity_Id) return Boolean is
3219       Item    : Node_Id;
3220       Item2   : Node_Id;
3221       Elab_Id : Entity_Id;
3222       Par     : Node_Id;
3223
3224    begin
3225       Item := First (Context_Items (Cunit (Current_Sem_Unit)));
3226       while Present (Item) loop
3227          if Nkind (Item) = N_Pragma
3228            and then Pragma_Name (Item) = Name_Elaborate_All
3229          then
3230             --  Return if some previous error on the pragma itself
3231
3232             if Error_Posted (Item) then
3233                return False;
3234             end if;
3235
3236             Elab_Id :=
3237               Entity
3238                 (Expression (First (Pragma_Argument_Associations (Item))));
3239
3240             Par := Parent (Unit_Declaration_Node (Elab_Id));
3241
3242             Item2 := First (Context_Items (Par));
3243             while Present (Item2) loop
3244                if Nkind (Item2) = N_With_Clause
3245                  and then Entity (Name (Item2)) = E
3246                then
3247                   return True;
3248                end if;
3249
3250                Next (Item2);
3251             end loop;
3252          end if;
3253
3254          Next (Item);
3255       end loop;
3256
3257       return False;
3258    end Within_Elaborate_All;
3259
3260 end Sem_Elab;