OSDN Git Service

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