OSDN Git Service

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