Daily bump.
[official-gcc.git] / gcc / ada / sem_elab.adb
blob25c3d4433ff1121d5504d392c98c78ac0e32f064
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-2017, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Expander; use Expander;
35 with Lib; use Lib;
36 with Lib.Load; use Lib.Load;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Output; use Output;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Sem; use Sem;
45 with Sem_Aux; use Sem_Aux;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch7; use Sem_Ch7;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Util; use Sem_Util;
50 with Sinfo; use Sinfo;
51 with Sinput; use Sinput;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Table;
55 with Tbuild; use Tbuild;
56 with Uintp; use Uintp;
57 with Uname; use Uname;
59 package body Sem_Elab is
61 -- The following table records the recursive call chain for output in the
62 -- Output routine. Each entry records the call node and the entity of the
63 -- called routine. The number of entries in the table (i.e. the value of
64 -- Elab_Call.Last) indicates the current depth of recursion and is used to
65 -- identify the outer level.
67 type Elab_Call_Element is record
68 Cloc : Source_Ptr;
69 Ent : Entity_Id;
70 end record;
72 package Elab_Call is new Table.Table
73 (Table_Component_Type => Elab_Call_Element,
74 Table_Index_Type => Int,
75 Table_Low_Bound => 1,
76 Table_Initial => 50,
77 Table_Increment => 100,
78 Table_Name => "Elab_Call");
80 -- The following table records all calls that have been processed starting
81 -- from an outer level call. The table prevents both infinite recursion and
82 -- useless reanalysis of calls within the same context. The use of context
83 -- is important because it allows for proper checks in more complex code:
85 -- if ... then
86 -- Call; -- requires a check
87 -- Call; -- does not need a check thanks to the table
88 -- elsif ... then
89 -- Call; -- requires a check, different context
90 -- end if;
92 -- Call; -- requires a check, different context
94 type Visited_Element is record
95 Subp_Id : Entity_Id;
96 -- The entity of the subprogram being called
98 Context : Node_Id;
99 -- The context where the call to the subprogram occurs
100 end record;
102 package Elab_Visited is new Table.Table
103 (Table_Component_Type => Visited_Element,
104 Table_Index_Type => Int,
105 Table_Low_Bound => 1,
106 Table_Initial => 200,
107 Table_Increment => 100,
108 Table_Name => "Elab_Visited");
110 -- The following table records delayed calls which must be examined after
111 -- all generic bodies have been instantiated.
113 type Delay_Element is record
114 N : Node_Id;
115 -- The parameter N from the call to Check_Internal_Call. Note that this
116 -- node may get rewritten over the delay period by expansion in the call
117 -- case (but not in the instantiation case).
119 E : Entity_Id;
120 -- The parameter E from the call to Check_Internal_Call
122 Orig_Ent : Entity_Id;
123 -- The parameter Orig_Ent from the call to Check_Internal_Call
125 Curscop : Entity_Id;
126 -- The current scope of the call. This is restored when we complete the
127 -- delayed call, so that we do this in the right scope.
129 Outer_Scope : Entity_Id;
130 -- Save scope of outer level call
132 From_Elab_Code : Boolean;
133 -- Save indication of whether this call is from elaboration code
135 In_Task_Activation : Boolean;
136 -- Save indication of whether this call is from a task body. Tasks are
137 -- activated at the "begin", which is after all local procedure bodies,
138 -- so calls to those procedures can't fail, even if they occur after the
139 -- task body.
141 From_SPARK_Code : Boolean;
142 -- Save indication of whether this call is under SPARK_Mode => On
143 end record;
145 package Delay_Check is new Table.Table
146 (Table_Component_Type => Delay_Element,
147 Table_Index_Type => Int,
148 Table_Low_Bound => 1,
149 Table_Initial => 1000,
150 Table_Increment => 100,
151 Table_Name => "Delay_Check");
153 C_Scope : Entity_Id;
154 -- Top-level scope of current scope. Compute this only once at the outer
155 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
157 Outer_Level_Sloc : Source_Ptr;
158 -- Save Sloc value for outer level call node for comparisons of source
159 -- locations. A body is too late if it appears after the *outer* level
160 -- call, not the particular call that is being analyzed.
162 From_Elab_Code : Boolean;
163 -- This flag shows whether the outer level call currently being examined
164 -- is or is not in elaboration code. We are only interested in calls to
165 -- routines in other units if this flag is True.
167 In_Task_Activation : Boolean := False;
168 -- This flag indicates whether we are performing elaboration checks on task
169 -- bodies, at the point of activation. If true, we do not raise
170 -- Program_Error for calls to local procedures, because all local bodies
171 -- are known to be elaborated. However, we still need to trace such calls,
172 -- because a local procedure could call a procedure in another package,
173 -- so we might need an implicit Elaborate_All.
175 Delaying_Elab_Checks : Boolean := True;
176 -- This is set True till the compilation is complete, including the
177 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
178 -- the delay table is used to make the delayed calls and this flag is reset
179 -- to False, so that the calls are processed.
181 -----------------------
182 -- Local Subprograms --
183 -----------------------
185 -- Note: Outer_Scope in all following specs represents the scope of
186 -- interest of the outer level call. If it is set to Standard_Standard,
187 -- then it means the outer level call was at elaboration level, and that
188 -- thus all calls are of interest. If it was set to some other scope,
189 -- then the original call was an inner call, and we are not interested
190 -- in calls that go outside this scope.
192 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
193 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
194 -- for the WITH clause for unit U (which will always be present). A special
195 -- case is when N is a function or procedure instantiation, in which case
196 -- it is sufficient to set Elaborate_Desirable, since in this case there is
197 -- no possibility of transitive elaboration issues.
199 procedure Check_A_Call
200 (N : Node_Id;
201 E : Entity_Id;
202 Outer_Scope : Entity_Id;
203 Inter_Unit_Only : Boolean;
204 Generate_Warnings : Boolean := True;
205 In_Init_Proc : Boolean := False);
206 -- This is the internal recursive routine that is called to check for
207 -- possible elaboration error. The argument N is a subprogram call or
208 -- generic instantiation, or 'Access attribute reference to be checked, and
209 -- E is the entity of the called subprogram, or instantiated generic unit,
210 -- or subprogram referenced by 'Access.
212 -- In SPARK mode, N can also be a variable reference, since in SPARK this
213 -- also triggers a requirement for Elaborate_All, and in this case E is the
214 -- entity being referenced.
216 -- Outer_Scope is the outer level scope for the original reference.
217 -- Inter_Unit_Only is set if the call is only to be checked in the
218 -- case where it is to another unit (and skipped if within a unit).
219 -- Generate_Warnings is set to False to suppress warning messages about
220 -- missing pragma Elaborate_All's. These messages are not wanted for
221 -- inner calls in the dynamic model. Note that an instance of the Access
222 -- attribute applied to a subprogram also generates a call to this
223 -- procedure (since the referenced subprogram may be called later
224 -- indirectly). Flag In_Init_Proc should be set whenever the current
225 -- context is a type init proc.
227 -- Note: this might better be called Check_A_Reference to recognize the
228 -- variable case for SPARK, but we prefer to retain the historical name
229 -- since in practice this is mostly about checking calls for the possible
230 -- occurrence of an access-before-elaboration exception.
232 procedure Check_Bad_Instantiation (N : Node_Id);
233 -- N is a node for an instantiation (if called with any other node kind,
234 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
235 -- the special case of a generic instantiation of a generic spec in the
236 -- same declarative part as the instantiation where a body is present and
237 -- has not yet been seen. This is an obvious error, but needs to be checked
238 -- specially at the time of the instantiation, since it is a case where we
239 -- cannot insert the body anywhere. If this case is detected, warnings are
240 -- generated, and a raise of Program_Error is inserted. In addition any
241 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
242 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
243 -- flag as an indication that no attempt should be made to insert an
244 -- instance body.
246 procedure Check_Internal_Call
247 (N : Node_Id;
248 E : Entity_Id;
249 Outer_Scope : Entity_Id;
250 Orig_Ent : Entity_Id);
251 -- N is a function call or procedure statement call node and E is the
252 -- entity of the called function, which is within the current compilation
253 -- unit (where subunits count as part of the parent). This call checks if
254 -- this call, or any call within any accessed body could cause an ABE, and
255 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
256 -- renamings, and points to the original name of the entity. This is used
257 -- for error messages. Outer_Scope is the outer level scope for the
258 -- original call.
260 procedure Check_Internal_Call_Continue
261 (N : Node_Id;
262 E : Entity_Id;
263 Outer_Scope : Entity_Id;
264 Orig_Ent : Entity_Id);
265 -- The processing for Check_Internal_Call is divided up into two phases,
266 -- and this represents the second phase. The second phase is delayed if
267 -- Delaying_Elab_Checks is set to True. In this delayed case, the first
268 -- phase makes an entry in the Delay_Check table, which is processed when
269 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
270 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
271 -- original call.
273 function Has_Generic_Body (N : Node_Id) return Boolean;
274 -- N is a generic package instantiation node, and this routine determines
275 -- if this package spec does in fact have a generic body. If so, then
276 -- True is returned, otherwise False. Note that this is not at all the
277 -- same as checking if the unit requires a body, since it deals with
278 -- the case of optional bodies accurately (i.e. if a body is optional,
279 -- then it looks to see if a body is actually present). Note: this
280 -- function can only do a fully correct job if in generating code mode
281 -- where all bodies have to be present. If we are operating in semantics
282 -- check only mode, then in some cases of optional bodies, a result of
283 -- False may incorrectly be given. In practice this simply means that
284 -- some cases of warnings for incorrect order of elaboration will only
285 -- be given when generating code, which is not a big problem (and is
286 -- inevitable, given the optional body semantics of Ada).
288 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
289 -- Given code for an elaboration check (or unconditional raise if the check
290 -- is not needed), inserts the code in the appropriate place. N is the call
291 -- or instantiation node for which the check code is required. C is the
292 -- test whose failure triggers the raise.
294 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean;
295 -- Returns True if node N is a call to a generic formal subprogram
297 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
298 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
300 procedure Output_Calls
301 (N : Node_Id;
302 Check_Elab_Flag : Boolean);
303 -- Outputs chain of calls stored in the Elab_Call table. The caller has
304 -- already generated the main warning message, so the warnings generated
305 -- are all continuation messages. The argument is the call node at which
306 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
307 -- enumerated only when flag Elab_Warning is set for the dynamic case or
308 -- when flag Elab_Info_Messages is set for the static case.
310 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
311 -- Given two scopes, determine whether they are the same scope from an
312 -- elaboration point of view, i.e. packages and blocks are ignored.
314 procedure Set_C_Scope;
315 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
316 -- to be the enclosing compilation unit of this scope.
318 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
319 -- N is either a function or procedure call or an access attribute that
320 -- references a subprogram. This call retrieves the relevant entity. If
321 -- this is a call to a protected subprogram, the entity is a selected
322 -- component. The callable entity may be absent, in which case Empty is
323 -- returned. This happens with non-analyzed calls in nested generics.
325 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
326 -- entity, in which case, the value returned is simply this entity.
328 procedure Set_Elaboration_Constraint
329 (Call : Node_Id;
330 Subp : Entity_Id;
331 Scop : Entity_Id);
332 -- The current unit U may depend semantically on some unit P that is not
333 -- in the current context. If there is an elaboration call that reaches P,
334 -- we need to indicate that P requires an Elaborate_All, but this is not
335 -- effective in U's ali file, if there is no with_clause for P. In this
336 -- case we add the Elaborate_All on the unit Q that directly or indirectly
337 -- makes P available. This can happen in two cases:
339 -- a) Q declares a subtype of a type declared in P, and the call is an
340 -- initialization call for an object of that subtype.
342 -- b) Q declares an object of some tagged type whose root type is
343 -- declared in P, and the initialization call uses object notation on
344 -- that object to reach a primitive operation or a classwide operation
345 -- declared in P.
347 -- If P appears in the context of U, the current processing is correct.
348 -- Otherwise we must identify these two cases to retrieve Q and place the
349 -- Elaborate_All_Desirable on it.
351 function Spec_Entity (E : Entity_Id) return Entity_Id;
352 -- Given a compilation unit entity, if it is a spec entity, it is returned
353 -- unchanged. If it is a body entity, then the spec for the corresponding
354 -- spec is returned
356 procedure Supply_Bodies (N : Node_Id);
357 -- Given a node, N, that is either a subprogram declaration or a package
358 -- declaration, this procedure supplies dummy bodies for the subprogram
359 -- or for all subprograms in the package. If the given node is not one of
360 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
361 -- contains a single Raise statement.
363 procedure Supply_Bodies (L : List_Id);
364 -- Calls Supply_Bodies for all elements of the given list L
366 function Within (E1, E2 : Entity_Id) return Boolean;
367 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
368 -- of its contained scopes, False otherwise.
370 function Within_Elaborate_All
371 (Unit : Unit_Number_Type;
372 E : Entity_Id) return Boolean;
373 -- Return True if we are within the scope of an Elaborate_All for E, or if
374 -- we are within the scope of an Elaborate_All for some other unit U, and U
375 -- with's E. This prevents spurious warnings when the called entity is
376 -- renamed within U, or in case of generic instances.
378 --------------------------------------
379 -- Activate_Elaborate_All_Desirable --
380 --------------------------------------
382 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
383 UN : constant Unit_Number_Type := Get_Code_Unit (N);
384 CU : constant Node_Id := Cunit (UN);
385 UE : constant Entity_Id := Cunit_Entity (UN);
386 Unm : constant Unit_Name_Type := Unit_Name (UN);
387 CI : constant List_Id := Context_Items (CU);
388 Itm : Node_Id;
389 Ent : Entity_Id;
391 procedure Add_To_Context_And_Mark (Itm : Node_Id);
392 -- This procedure is called when the elaborate indication must be
393 -- applied to a unit not in the context of the referencing unit. The
394 -- unit gets added to the context as an implicit with.
396 function In_Withs_Of (UEs : Entity_Id) return Boolean;
397 -- UEs is the spec entity of a unit. If the unit to be marked is
398 -- in the context item list of this unit spec, then the call returns
399 -- True and Itm is left set to point to the relevant N_With_Clause node.
401 procedure Set_Elab_Flag (Itm : Node_Id);
402 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
404 -----------------------------
405 -- Add_To_Context_And_Mark --
406 -----------------------------
408 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
409 CW : constant Node_Id :=
410 Make_With_Clause (Sloc (Itm),
411 Name => Name (Itm));
413 begin
414 Set_Library_Unit (CW, Library_Unit (Itm));
415 Set_Implicit_With (CW, True);
417 -- Set elaborate all desirable on copy and then append the copy to
418 -- the list of body with's and we are done.
420 Set_Elab_Flag (CW);
421 Append_To (CI, CW);
422 end Add_To_Context_And_Mark;
424 -----------------
425 -- In_Withs_Of --
426 -----------------
428 function In_Withs_Of (UEs : Entity_Id) return Boolean is
429 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
430 CUs : constant Node_Id := Cunit (UNs);
431 CIs : constant List_Id := Context_Items (CUs);
433 begin
434 Itm := First (CIs);
435 while Present (Itm) loop
436 if Nkind (Itm) = N_With_Clause then
437 Ent :=
438 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
440 if U = Ent then
441 return True;
442 end if;
443 end if;
445 Next (Itm);
446 end loop;
448 return False;
449 end In_Withs_Of;
451 -------------------
452 -- Set_Elab_Flag --
453 -------------------
455 procedure Set_Elab_Flag (Itm : Node_Id) is
456 begin
457 if Nkind (N) in N_Subprogram_Instantiation then
458 Set_Elaborate_Desirable (Itm);
459 else
460 Set_Elaborate_All_Desirable (Itm);
461 end if;
462 end Set_Elab_Flag;
464 -- Start of processing for Activate_Elaborate_All_Desirable
466 begin
467 -- Do not set binder indication if expansion is disabled, as when
468 -- compiling a generic unit.
470 if not Expander_Active then
471 return;
472 end if;
474 -- If an instance of a generic package contains a controlled object (so
475 -- we're calling Initialize at elaboration time), and the instance is in
476 -- a package body P that says "with P;", then we need to return without
477 -- adding "pragma Elaborate_All (P);" to P.
479 if U = Main_Unit_Entity then
480 return;
481 end if;
483 Itm := First (CI);
484 while Present (Itm) loop
485 if Nkind (Itm) = N_With_Clause then
486 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
488 -- If we find it, then mark elaborate all desirable and return
490 if U = Ent then
491 Set_Elab_Flag (Itm);
492 return;
493 end if;
494 end if;
496 Next (Itm);
497 end loop;
499 -- If we fall through then the with clause is not present in the
500 -- current unit. One legitimate possibility is that the with clause
501 -- is present in the spec when we are a body.
503 if Is_Body_Name (Unm)
504 and then In_Withs_Of (Spec_Entity (UE))
505 then
506 Add_To_Context_And_Mark (Itm);
507 return;
508 end if;
510 -- Similarly, we may be in the spec or body of a child unit, where
511 -- the unit in question is with'ed by some ancestor of the child unit.
513 if Is_Child_Name (Unm) then
514 declare
515 Pkg : Entity_Id;
517 begin
518 Pkg := UE;
519 loop
520 Pkg := Scope (Pkg);
521 exit when Pkg = Standard_Standard;
523 if In_Withs_Of (Pkg) then
524 Add_To_Context_And_Mark (Itm);
525 return;
526 end if;
527 end loop;
528 end;
529 end if;
531 -- Here if we do not find with clause on spec or body. We just ignore
532 -- this case; it means that the elaboration involves some other unit
533 -- than the unit being compiled, and will be caught elsewhere.
534 end Activate_Elaborate_All_Desirable;
536 ------------------
537 -- Check_A_Call --
538 ------------------
540 procedure Check_A_Call
541 (N : Node_Id;
542 E : Entity_Id;
543 Outer_Scope : Entity_Id;
544 Inter_Unit_Only : Boolean;
545 Generate_Warnings : Boolean := True;
546 In_Init_Proc : Boolean := False)
548 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
549 -- Indicates if we have Access attribute case
551 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean;
552 -- True if we're calling an instance of a generic subprogram, or a
553 -- subprogram in an instance of a generic package, and the call is
554 -- outside that instance.
556 procedure Elab_Warning
557 (Msg_D : String;
558 Msg_S : String;
559 Ent : Node_Or_Entity_Id);
560 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
561 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
562 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
563 -- Msg_S is an info message (output if Elab_Info_Messages is set).
565 function Find_W_Scope return Entity_Id;
566 -- Find top-level scope for called entity (not following renamings
567 -- or derivations). This is where the Elaborate_All will go if it is
568 -- needed. We start with the called entity, except in the case of an
569 -- initialization procedure outside the current package, where the init
570 -- proc is in the root package, and we start from the entity of the name
571 -- in the call.
573 -----------------------------------
574 -- Call_To_Instance_From_Outside --
575 -----------------------------------
577 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean is
578 Scop : Entity_Id := Id;
580 begin
581 loop
582 if Scop = Standard_Standard then
583 return False;
584 end if;
586 if Is_Generic_Instance (Scop) then
587 return not In_Open_Scopes (Scop);
588 end if;
590 Scop := Scope (Scop);
591 end loop;
592 end Call_To_Instance_From_Outside;
594 ------------------
595 -- Elab_Warning --
596 ------------------
598 procedure Elab_Warning
599 (Msg_D : String;
600 Msg_S : String;
601 Ent : Node_Or_Entity_Id)
603 begin
604 -- Dynamic elaboration checks, real warning
606 if Dynamic_Elaboration_Checks then
607 if not Access_Case then
608 if Msg_D /= "" and then Elab_Warnings then
609 Error_Msg_NE (Msg_D, N, Ent);
610 end if;
612 -- In the access case emit first warning message as well,
613 -- otherwise list of calls will appear as errors.
615 elsif Elab_Warnings then
616 Error_Msg_NE (Msg_S, N, Ent);
617 end if;
619 -- Static elaboration checks, info message
621 else
622 if Elab_Info_Messages then
623 Error_Msg_NE (Msg_S, N, Ent);
624 end if;
625 end if;
626 end Elab_Warning;
628 ------------------
629 -- Find_W_Scope --
630 ------------------
632 function Find_W_Scope return Entity_Id is
633 Refed_Ent : constant Entity_Id := Get_Referenced_Ent (N);
634 W_Scope : Entity_Id;
636 begin
637 if Is_Init_Proc (Refed_Ent)
638 and then not In_Same_Extended_Unit (N, Refed_Ent)
639 then
640 W_Scope := Scope (Refed_Ent);
641 else
642 W_Scope := E;
643 end if;
645 -- Now loop through scopes to get to the enclosing compilation unit
647 while not Is_Compilation_Unit (W_Scope) loop
648 W_Scope := Scope (W_Scope);
649 end loop;
651 return W_Scope;
652 end Find_W_Scope;
654 -- Local variables
656 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
657 -- Indicates if we have instantiation case
659 Loc : constant Source_Ptr := Sloc (N);
661 Variable_Case : constant Boolean :=
662 Nkind (N) in N_Has_Entity
663 and then Present (Entity (N))
664 and then Ekind (Entity (N)) = E_Variable;
665 -- Indicates if we have variable reference case
667 W_Scope : constant Entity_Id := Find_W_Scope;
668 -- Top-level scope of directly called entity for subprogram. This
669 -- differs from E_Scope in the case where renamings or derivations
670 -- are involved, since it does not follow these links. W_Scope is
671 -- generally in a visible unit, and it is this scope that may require
672 -- an Elaborate_All. However, there are some cases (initialization
673 -- calls and calls involving object notation) where W_Scope might not
674 -- be in the context of the current unit, and there is an intermediate
675 -- package that is, in which case the Elaborate_All has to be placed
676 -- on this intermediate package. These special cases are handled in
677 -- Set_Elaboration_Constraint.
679 Ent : Entity_Id;
680 Callee_Unit_Internal : Boolean;
681 Caller_Unit_Internal : Boolean;
682 Decl : Node_Id;
683 Inst_Callee : Source_Ptr;
684 Inst_Caller : Source_Ptr;
685 Unit_Callee : Unit_Number_Type;
686 Unit_Caller : Unit_Number_Type;
688 Body_Acts_As_Spec : Boolean;
689 -- Set to true if call is to body acting as spec (no separate spec)
691 Cunit_SC : Boolean := False;
692 -- Set to suppress dynamic elaboration checks where one of the
693 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
694 -- if a pragma Elaborate[_All] applies to that scope, in which case
695 -- warnings on the scope are also suppressed. For the internal case,
696 -- we ignore this flag.
698 E_Scope : Entity_Id;
699 -- Top-level scope of entity for called subprogram. This value includes
700 -- following renamings and derivations, so this scope can be in a
701 -- non-visible unit. This is the scope that is to be investigated to
702 -- see whether an elaboration check is required.
704 Is_DIC : Boolean;
705 -- Flag set when the subprogram being invoked is the procedure generated
706 -- for pragma Default_Initial_Condition.
708 SPARK_Elab_Errors : Boolean;
709 -- Flag set when an entity is called or a variable is read during SPARK
710 -- dynamic elaboration.
712 -- Start of processing for Check_A_Call
714 begin
715 -- If the call is known to be within a local Suppress Elaboration
716 -- pragma, nothing to check. This can happen in task bodies. But
717 -- we ignore this for a call to a generic formal.
719 if Nkind (N) in N_Subprogram_Call
720 and then No_Elaboration_Check (N)
721 and then not Is_Call_Of_Generic_Formal (N)
722 then
723 return;
724 end if;
726 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
727 -- check, we don't mind in this case if the call occurs before the body
728 -- since this is all generated code.
730 if Nkind (Original_Node (N)) = N_Attribute_Reference
731 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
732 then
733 return;
734 end if;
736 -- Intrinsics such as instances of Unchecked_Deallocation do not have
737 -- any body, so elaboration checking is not needed, and would be wrong.
739 if Is_Intrinsic_Subprogram (E) then
740 return;
741 end if;
743 -- Proceed with check
745 Ent := E;
747 -- For a variable reference, just set Body_Acts_As_Spec to False
749 if Variable_Case then
750 Body_Acts_As_Spec := False;
752 -- Additional checks for all other cases
754 else
755 -- Go to parent for derived subprogram, or to original subprogram in
756 -- the case of a renaming (Alias covers both these cases).
758 loop
759 if (Suppress_Elaboration_Warnings (Ent)
760 or else Elaboration_Checks_Suppressed (Ent))
761 and then (Inst_Case or else No (Alias (Ent)))
762 then
763 return;
764 end if;
766 -- Nothing to do for imported entities
768 if Is_Imported (Ent) then
769 return;
770 end if;
772 exit when Inst_Case or else No (Alias (Ent));
773 Ent := Alias (Ent);
774 end loop;
776 Decl := Unit_Declaration_Node (Ent);
778 if Nkind (Decl) = N_Subprogram_Body then
779 Body_Acts_As_Spec := True;
781 elsif Nkind_In (Decl, N_Subprogram_Declaration,
782 N_Subprogram_Body_Stub)
783 or else Inst_Case
784 then
785 Body_Acts_As_Spec := False;
787 -- If we have none of an instantiation, subprogram body or subprogram
788 -- declaration, or in the SPARK case, a variable reference, then
789 -- it is not a case that we want to check. (One case is a call to a
790 -- generic formal subprogram, where we do not want the check in the
791 -- template).
793 else
794 return;
795 end if;
796 end if;
798 E_Scope := Ent;
799 loop
800 if Elaboration_Checks_Suppressed (E_Scope)
801 or else Suppress_Elaboration_Warnings (E_Scope)
802 then
803 Cunit_SC := True;
804 end if;
806 -- Exit when we get to compilation unit, not counting subunits
808 exit when Is_Compilation_Unit (E_Scope)
809 and then (Is_Child_Unit (E_Scope)
810 or else Scope (E_Scope) = Standard_Standard);
812 pragma Assert (E_Scope /= Standard_Standard);
814 -- Move up a scope looking for compilation unit
816 E_Scope := Scope (E_Scope);
817 end loop;
819 -- No checks needed for pure or preelaborated compilation units
821 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
822 return;
823 end if;
825 -- If the generic entity is within a deeper instance than we are, then
826 -- either the instantiation to which we refer itself caused an ABE, in
827 -- which case that will be handled separately, or else we know that the
828 -- body we need appears as needed at the point of the instantiation.
829 -- However, this assumption is only valid if we are in static mode.
831 if not Dynamic_Elaboration_Checks
832 and then
833 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
834 then
835 return;
836 end if;
838 -- Do not give a warning for a package with no body
840 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
841 return;
842 end if;
844 -- Case of entity is in same unit as call or instantiation. In the
845 -- instantiation case, W_Scope may be different from E_Scope; we want
846 -- the unit in which the instantiation occurs, since we're analyzing
847 -- based on the expansion.
849 if W_Scope = C_Scope then
850 if not Inter_Unit_Only then
851 Check_Internal_Call (N, Ent, Outer_Scope, E);
852 end if;
854 return;
855 end if;
857 -- Case of entity is not in current unit (i.e. with'ed unit case)
859 -- We are only interested in such calls if the outer call was from
860 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
862 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
863 return;
864 end if;
866 -- Nothing to do if some scope said that no checks were required
868 if Cunit_SC then
869 return;
870 end if;
872 -- Nothing to do for a generic instance, because a call to an instance
873 -- cannot fail the elaboration check, because the body of the instance
874 -- is always elaborated immediately after the spec.
876 if Call_To_Instance_From_Outside (Ent) then
877 return;
878 end if;
880 -- Nothing to do if subprogram with no separate spec. However, a call
881 -- to Deep_Initialize may result in a call to a user-defined Initialize
882 -- procedure, which imposes a body dependency. This happens only if the
883 -- type is controlled and the Initialize procedure is not inherited.
885 if Body_Acts_As_Spec then
886 if Is_TSS (Ent, TSS_Deep_Initialize) then
887 declare
888 Typ : constant Entity_Id := Etype (First_Formal (Ent));
889 Init : Entity_Id;
891 begin
892 if not Is_Controlled (Typ) then
893 return;
894 else
895 Init := Find_Prim_Op (Typ, Name_Initialize);
897 if Comes_From_Source (Init) then
898 Ent := Init;
899 else
900 return;
901 end if;
902 end if;
903 end;
905 else
906 return;
907 end if;
908 end if;
910 -- Check cases of internal units
912 Callee_Unit_Internal := In_Internal_Unit (E_Scope);
914 -- Do not give a warning if the with'ed unit is internal and this is
915 -- the generic instantiation case (this saves a lot of hassle dealing
916 -- with the Text_IO special child units)
918 if Callee_Unit_Internal and Inst_Case then
919 return;
920 end if;
922 if C_Scope = Standard_Standard then
923 Caller_Unit_Internal := False;
924 else
925 Caller_Unit_Internal := In_Internal_Unit (C_Scope);
926 end if;
928 -- Do not give a warning if the with'ed unit is internal and the caller
929 -- is not internal (since the binder always elaborates internal units
930 -- first).
932 if Callee_Unit_Internal and not Caller_Unit_Internal then
933 return;
934 end if;
936 -- For now, if debug flag -gnatdE is not set, do no checking for one
937 -- internal unit withing another. This fixes the problem with the sgi
938 -- build and storage errors. To be resolved later ???
940 if (Callee_Unit_Internal and Caller_Unit_Internal)
941 and not Debug_Flag_EE
942 then
943 return;
944 end if;
946 if Is_TSS (E, TSS_Deep_Initialize) then
947 Ent := E;
948 end if;
950 -- If the call is in an instance, and the called entity is not
951 -- defined in the same instance, then the elaboration issue focuses
952 -- around the unit containing the template, it is this unit that
953 -- requires an Elaborate_All.
955 -- However, if we are doing dynamic elaboration, we need to chase the
956 -- call in the usual manner.
958 -- We also need to chase the call in the usual manner if it is a call
959 -- to a generic formal parameter, since that case was not handled as
960 -- part of the processing of the template.
962 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
963 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
965 if Inst_Caller = No_Location then
966 Unit_Caller := No_Unit;
967 else
968 Unit_Caller := Get_Source_Unit (N);
969 end if;
971 if Inst_Callee = No_Location then
972 Unit_Callee := No_Unit;
973 else
974 Unit_Callee := Get_Source_Unit (Ent);
975 end if;
977 if Unit_Caller /= No_Unit
978 and then Unit_Callee /= Unit_Caller
979 and then not Dynamic_Elaboration_Checks
980 and then not Is_Call_Of_Generic_Formal (N)
981 then
982 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
984 -- If we don't get a spec entity, just ignore call. Not quite
985 -- clear why this check is necessary. ???
987 if No (E_Scope) then
988 return;
989 end if;
991 -- Otherwise step to enclosing compilation unit
993 while not Is_Compilation_Unit (E_Scope) loop
994 E_Scope := Scope (E_Scope);
995 end loop;
997 -- For the case where N is not an instance, and is not a call within
998 -- instance to other than a generic formal, we recompute E_Scope
999 -- for the error message, since we do NOT want to go to the unit
1000 -- that has the ultimate declaration in the case of renaming and
1001 -- derivation and we also want to go to the generic unit in the
1002 -- case of an instance, and no further.
1004 else
1005 -- Loop to carefully follow renamings and derivations one step
1006 -- outside the current unit, but not further.
1008 if not (Inst_Case or Variable_Case)
1009 and then Present (Alias (Ent))
1010 then
1011 E_Scope := Alias (Ent);
1012 else
1013 E_Scope := Ent;
1014 end if;
1016 loop
1017 while not Is_Compilation_Unit (E_Scope) loop
1018 E_Scope := Scope (E_Scope);
1019 end loop;
1021 -- If E_Scope is the same as C_Scope, it means that there
1022 -- definitely was a local renaming or derivation, and we
1023 -- are not yet out of the current unit.
1025 exit when E_Scope /= C_Scope;
1026 Ent := Alias (Ent);
1027 E_Scope := Ent;
1029 -- If no alias, there could be a previous error, but not if we've
1030 -- already reached the outermost level (Standard).
1032 if No (Ent) then
1033 return;
1034 end if;
1035 end loop;
1036 end if;
1038 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
1039 return;
1040 end if;
1042 -- Determine whether the Default_Initial_Condition procedure of some
1043 -- type is being invoked.
1045 Is_DIC := Ekind (Ent) = E_Procedure and then Is_DIC_Procedure (Ent);
1047 -- Checks related to Default_Initial_Condition fall under the SPARK
1048 -- umbrella because this is a SPARK-specific annotation.
1050 SPARK_Elab_Errors :=
1051 SPARK_Mode = On and (Is_DIC or Dynamic_Elaboration_Checks);
1053 -- Now check if an Elaborate_All (or dynamic check) is needed
1055 if (Elab_Info_Messages or Elab_Warnings or SPARK_Elab_Errors)
1056 and then Generate_Warnings
1057 and then not Suppress_Elaboration_Warnings (Ent)
1058 and then not Elaboration_Checks_Suppressed (Ent)
1059 and then not Suppress_Elaboration_Warnings (E_Scope)
1060 and then not Elaboration_Checks_Suppressed (E_Scope)
1061 then
1062 -- Instantiation case
1064 if Inst_Case then
1065 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
1066 Error_Msg_NE
1067 ("instantiation of & during elaboration in SPARK", N, Ent);
1068 else
1069 Elab_Warning
1070 ("instantiation of & may raise Program_Error?l?",
1071 "info: instantiation of & during elaboration?$?", Ent);
1072 end if;
1074 -- Indirect call case, info message only in static elaboration
1075 -- case, because the attribute reference itself cannot raise an
1076 -- exception. Note that SPARK does not permit indirect calls.
1078 elsif Access_Case then
1079 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
1081 -- Variable reference in SPARK mode
1083 elsif Variable_Case then
1084 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
1085 Error_Msg_NE
1086 ("reference to & during elaboration in SPARK", N, Ent);
1087 end if;
1089 -- Subprogram call case
1091 else
1092 if Nkind (Name (N)) in N_Has_Entity
1093 and then Is_Init_Proc (Entity (Name (N)))
1094 and then Comes_From_Source (Ent)
1095 then
1096 Elab_Warning
1097 ("implicit call to & may raise Program_Error?l?",
1098 "info: implicit call to & during elaboration?$?",
1099 Ent);
1101 elsif SPARK_Elab_Errors then
1103 -- Emit a specialized error message when the elaboration of an
1104 -- object of a private type evaluates the expression of pragma
1105 -- Default_Initial_Condition. This prevents the internal name
1106 -- of the procedure from appearing in the error message.
1108 if Is_DIC then
1109 Error_Msg_N
1110 ("call to Default_Initial_Condition during elaboration in "
1111 & "SPARK", N);
1112 else
1113 Error_Msg_NE
1114 ("call to & during elaboration in SPARK", N, Ent);
1115 end if;
1117 else
1118 Elab_Warning
1119 ("call to & may raise Program_Error?l?",
1120 "info: call to & during elaboration?$?",
1121 Ent);
1122 end if;
1123 end if;
1125 Error_Msg_Qual_Level := Nat'Last;
1127 -- Case of Elaborate_All not present and required, for SPARK this
1128 -- is an error, so give an error message.
1130 if SPARK_Elab_Errors then
1131 Error_Msg_NE -- CODEFIX
1132 ("\Elaborate_All pragma required for&", N, W_Scope);
1134 -- Otherwise we generate an implicit pragma. For a subprogram
1135 -- instantiation, Elaborate is good enough, since no transitive
1136 -- call is possible at elaboration time in this case.
1138 elsif Nkind (N) in N_Subprogram_Instantiation then
1139 Elab_Warning
1140 ("\missing pragma Elaborate for&?l?",
1141 "\implicit pragma Elaborate for& generated?$?",
1142 W_Scope);
1144 -- For all other cases, we need an implicit Elaborate_All
1146 else
1147 Elab_Warning
1148 ("\missing pragma Elaborate_All for&?l?",
1149 "\implicit pragma Elaborate_All for & generated?$?",
1150 W_Scope);
1151 end if;
1153 Error_Msg_Qual_Level := 0;
1155 -- Take into account the flags related to elaboration warning
1156 -- messages when enumerating the various calls involved. This
1157 -- ensures the proper pairing of the main warning and the
1158 -- clarification messages generated by Output_Calls.
1160 Output_Calls (N, Check_Elab_Flag => True);
1162 -- Set flag to prevent further warnings for same unit unless in
1163 -- All_Errors_Mode.
1165 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
1166 Set_Suppress_Elaboration_Warnings (W_Scope);
1167 end if;
1168 end if;
1170 -- Check for runtime elaboration check required
1172 if Dynamic_Elaboration_Checks then
1173 if not Elaboration_Checks_Suppressed (Ent)
1174 and then not Elaboration_Checks_Suppressed (W_Scope)
1175 and then not Elaboration_Checks_Suppressed (E_Scope)
1176 and then not Cunit_SC
1177 then
1178 -- Runtime elaboration check required. Generate check of the
1179 -- elaboration Boolean for the unit containing the entity.
1181 -- Note that for this case, we do check the real unit (the one
1182 -- from following renamings, since that is the issue).
1184 -- Could this possibly miss a useless but required PE???
1186 Insert_Elab_Check (N,
1187 Make_Attribute_Reference (Loc,
1188 Attribute_Name => Name_Elaborated,
1189 Prefix =>
1190 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1192 -- Prevent duplicate elaboration checks on the same call,
1193 -- which can happen if the body enclosing the call appears
1194 -- itself in a call whose elaboration check is delayed.
1196 if Nkind (N) in N_Subprogram_Call then
1197 Set_No_Elaboration_Check (N);
1198 end if;
1199 end if;
1201 -- Case of static elaboration model
1203 else
1204 -- Do not do anything if elaboration checks suppressed. Note that
1205 -- we check Ent here, not E, since we want the real entity for the
1206 -- body to see if checks are suppressed for it, not the dummy
1207 -- entry for renamings or derivations.
1209 if Elaboration_Checks_Suppressed (Ent)
1210 or else Elaboration_Checks_Suppressed (E_Scope)
1211 or else Elaboration_Checks_Suppressed (W_Scope)
1212 then
1213 null;
1215 -- Do not generate an Elaborate_All for finalization routines
1216 -- that perform partial clean up as part of initialization.
1218 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1219 null;
1221 -- Here we need to generate an implicit elaborate all
1223 else
1224 -- Generate Elaborate_All warning unless suppressed
1226 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1227 and then not Suppress_Elaboration_Warnings (Ent)
1228 and then not Suppress_Elaboration_Warnings (E_Scope)
1229 and then not Suppress_Elaboration_Warnings (W_Scope)
1230 then
1231 Error_Msg_Node_2 := W_Scope;
1232 Error_Msg_NE
1233 ("info: call to& in elaboration code requires pragma "
1234 & "Elaborate_All on&?$?", N, E);
1235 end if;
1237 -- Set indication for binder to generate Elaborate_All
1239 Set_Elaboration_Constraint (N, E, W_Scope);
1240 end if;
1241 end if;
1242 end Check_A_Call;
1244 -----------------------------
1245 -- Check_Bad_Instantiation --
1246 -----------------------------
1248 procedure Check_Bad_Instantiation (N : Node_Id) is
1249 Ent : Entity_Id;
1251 begin
1252 -- Nothing to do if we do not have an instantiation (happens in some
1253 -- error cases, and also in the formal package declaration case)
1255 if Nkind (N) not in N_Generic_Instantiation then
1256 return;
1258 -- Nothing to do if serious errors detected (avoid cascaded errors)
1260 elsif Serious_Errors_Detected /= 0 then
1261 return;
1263 -- Nothing to do if not in full analysis mode
1265 elsif not Full_Analysis then
1266 return;
1268 -- Nothing to do if inside a generic template
1270 elsif Inside_A_Generic then
1271 return;
1273 -- Nothing to do if a library level instantiation
1275 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1276 return;
1278 -- Nothing to do if we are compiling a proper body for semantic
1279 -- purposes only. The generic body may be in another proper body.
1281 elsif
1282 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1283 then
1284 return;
1285 end if;
1287 Ent := Get_Generic_Entity (N);
1289 -- The case we are interested in is when the generic spec is in the
1290 -- current declarative part
1292 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1293 or else not In_Same_Extended_Unit (N, Ent)
1294 then
1295 return;
1296 end if;
1298 -- If the generic entity is within a deeper instance than we are, then
1299 -- either the instantiation to which we refer itself caused an ABE, in
1300 -- which case that will be handled separately. Otherwise, we know that
1301 -- the body we need appears as needed at the point of the instantiation.
1302 -- If they are both at the same level but not within the same instance
1303 -- then the body of the generic will be in the earlier instance.
1305 declare
1306 D1 : constant Nat := Instantiation_Depth (Sloc (Ent));
1307 D2 : constant Nat := Instantiation_Depth (Sloc (N));
1309 begin
1310 if D1 > D2 then
1311 return;
1313 elsif D1 = D2
1314 and then Is_Generic_Instance (Scope (Ent))
1315 and then not In_Open_Scopes (Scope (Ent))
1316 then
1317 return;
1318 end if;
1319 end;
1321 -- Now we can proceed, if the entity being called has a completion,
1322 -- then we are definitely OK, since we have already seen the body.
1324 if Has_Completion (Ent) then
1325 return;
1326 end if;
1328 -- If there is no body, then nothing to do
1330 if not Has_Generic_Body (N) then
1331 return;
1332 end if;
1334 -- Here we definitely have a bad instantiation
1336 Error_Msg_Warn := SPARK_Mode /= On;
1337 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1339 if Present (Instance_Spec (N)) then
1340 Supply_Bodies (Instance_Spec (N));
1341 end if;
1343 Error_Msg_N ("\Program_Error [<<", N);
1344 Insert_Elab_Check (N);
1345 Set_ABE_Is_Certain (N);
1346 end Check_Bad_Instantiation;
1348 ---------------------
1349 -- Check_Elab_Call --
1350 ---------------------
1352 procedure Check_Elab_Call
1353 (N : Node_Id;
1354 Outer_Scope : Entity_Id := Empty;
1355 In_Init_Proc : Boolean := False)
1357 Ent : Entity_Id;
1358 P : Node_Id;
1360 begin
1361 -- If the reference is not in the main unit, there is nothing to check.
1362 -- Elaboration call from units in the context of the main unit will lead
1363 -- to semantic dependencies when those units are compiled.
1365 if not In_Extended_Main_Code_Unit (N) then
1366 return;
1367 end if;
1369 -- For an entry call, check relevant restriction
1371 if Nkind (N) = N_Entry_Call_Statement
1372 and then not In_Subprogram_Or_Concurrent_Unit
1373 then
1374 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1376 -- Nothing to do if this is not an expected type of reference (happens
1377 -- in some error conditions, and in some cases where rewriting occurs).
1379 elsif Nkind (N) not in N_Subprogram_Call
1380 and then Nkind (N) /= N_Attribute_Reference
1381 and then (SPARK_Mode /= On
1382 or else Nkind (N) not in N_Has_Entity
1383 or else No (Entity (N))
1384 or else Ekind (Entity (N)) /= E_Variable)
1385 then
1386 return;
1388 -- Nothing to do if this is a call already rewritten for elab checking.
1389 -- Such calls appear as the targets of If_Expressions.
1391 -- This check MUST be wrong, it catches far too much
1393 elsif Nkind (Parent (N)) = N_If_Expression then
1394 return;
1396 -- Nothing to do if inside a generic template
1398 elsif Inside_A_Generic
1399 and then No (Enclosing_Generic_Body (N))
1400 then
1401 return;
1403 -- Nothing to do if call is being pre-analyzed, as when within a
1404 -- pre/postcondition, a predicate, or an invariant.
1406 elsif In_Spec_Expression then
1407 return;
1408 end if;
1410 -- Nothing to do if this is a call to a postcondition, which is always
1411 -- within a subprogram body, even though the current scope may be the
1412 -- enclosing scope of the subprogram.
1414 if Nkind (N) = N_Procedure_Call_Statement
1415 and then Is_Entity_Name (Name (N))
1416 and then Chars (Entity (Name (N))) = Name_uPostconditions
1417 then
1418 return;
1419 end if;
1421 -- Here we have a reference at elaboration time that must be checked
1423 if Debug_Flag_LL then
1424 Write_Str (" Check_Elab_Ref: ");
1426 if Nkind (N) = N_Attribute_Reference then
1427 if not Is_Entity_Name (Prefix (N)) then
1428 Write_Str ("<<not entity name>>");
1429 else
1430 Write_Name (Chars (Entity (Prefix (N))));
1431 end if;
1433 Write_Str ("'Access");
1435 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1436 Write_Str ("<<not entity name>> ");
1438 else
1439 Write_Name (Chars (Entity (Name (N))));
1440 end if;
1442 Write_Str (" reference at ");
1443 Write_Location (Sloc (N));
1444 Write_Eol;
1445 end if;
1447 -- Climb up the tree to make sure we are not inside default expression
1448 -- of a parameter specification or a record component, since in both
1449 -- these cases, we will be doing the actual reference later, not now,
1450 -- and it is at the time of the actual reference (statically speaking)
1451 -- that we must do our static check, not at the time of its initial
1452 -- analysis).
1454 -- However, we have to check references within component definitions
1455 -- (e.g. a function call that determines an array component bound),
1456 -- so we terminate the loop in that case.
1458 P := Parent (N);
1459 while Present (P) loop
1460 if Nkind_In (P, N_Parameter_Specification,
1461 N_Component_Declaration)
1462 then
1463 return;
1465 -- The reference occurs within the constraint of a component,
1466 -- so it must be checked.
1468 elsif Nkind (P) = N_Component_Definition then
1469 exit;
1471 else
1472 P := Parent (P);
1473 end if;
1474 end loop;
1476 -- Stuff that happens only at the outer level
1478 if No (Outer_Scope) then
1479 Elab_Visited.Set_Last (0);
1481 -- Nothing to do if current scope is Standard (this is a bit odd, but
1482 -- it happens in the case of generic instantiations).
1484 C_Scope := Current_Scope;
1486 if C_Scope = Standard_Standard then
1487 return;
1488 end if;
1490 -- First case, we are in elaboration code
1492 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1494 if From_Elab_Code then
1496 -- Complain if ref that comes from source in preelaborated unit
1497 -- and we are not inside a subprogram (i.e. we are in elab code).
1499 if Comes_From_Source (N)
1500 and then In_Preelaborated_Unit
1501 and then not In_Inlined_Body
1502 and then Nkind (N) /= N_Attribute_Reference
1503 then
1504 -- This is a warning in GNAT mode allowing such calls to be
1505 -- used in the predefined library with appropriate care.
1507 Error_Msg_Warn := GNAT_Mode;
1508 Error_Msg_N
1509 ("<<non-static call not allowed in preelaborated unit", N);
1510 return;
1511 end if;
1513 -- Second case, we are inside a subprogram or concurrent unit, which
1514 -- means we are not in elaboration code.
1516 else
1517 -- In this case, the issue is whether we are inside the
1518 -- declarative part of the unit in which we live, or inside its
1519 -- statements. In the latter case, there is no issue of ABE calls
1520 -- at this level (a call from outside to the unit in which we live
1521 -- might cause an ABE, but that will be detected when we analyze
1522 -- that outer level call, as it recurses into the called unit).
1524 -- Climb up the tree, doing this test, and also testing for being
1525 -- inside a default expression, which, as discussed above, is not
1526 -- checked at this stage.
1528 declare
1529 P : Node_Id;
1530 L : List_Id;
1532 begin
1533 P := N;
1534 loop
1535 -- If we find a parentless subtree, it seems safe to assume
1536 -- that we are not in a declarative part and that no
1537 -- checking is required.
1539 if No (P) then
1540 return;
1541 end if;
1543 if Is_List_Member (P) then
1544 L := List_Containing (P);
1545 P := Parent (L);
1546 else
1547 L := No_List;
1548 P := Parent (P);
1549 end if;
1551 exit when Nkind (P) = N_Subunit;
1553 -- Filter out case of default expressions, where we do not
1554 -- do the check at this stage.
1556 if Nkind_In (P, N_Parameter_Specification,
1557 N_Component_Declaration)
1558 then
1559 return;
1560 end if;
1562 -- A protected body has no elaboration code and contains
1563 -- only other bodies.
1565 if Nkind (P) = N_Protected_Body then
1566 return;
1568 elsif Nkind_In (P, N_Subprogram_Body,
1569 N_Task_Body,
1570 N_Block_Statement,
1571 N_Entry_Body)
1572 then
1573 if L = Declarations (P) then
1574 exit;
1576 -- We are not in elaboration code, but we are doing
1577 -- dynamic elaboration checks, in this case, we still
1578 -- need to do the reference, since the subprogram we are
1579 -- in could be called from another unit, also in dynamic
1580 -- elaboration check mode, at elaboration time.
1582 elsif Dynamic_Elaboration_Checks then
1584 -- We provide a debug flag to disable this check. That
1585 -- way we have an easy work around for regressions
1586 -- that are caused by this new check. This debug flag
1587 -- can be removed later.
1589 if Debug_Flag_DD then
1590 return;
1591 end if;
1593 -- Do the check in this case
1595 exit;
1597 elsif Nkind (P) = N_Task_Body then
1599 -- The check is deferred until Check_Task_Activation
1600 -- but we need to capture local suppress pragmas
1601 -- that may inhibit checks on this call.
1603 Ent := Get_Referenced_Ent (N);
1605 if No (Ent) then
1606 return;
1608 elsif Elaboration_Checks_Suppressed (Current_Scope)
1609 or else Elaboration_Checks_Suppressed (Ent)
1610 or else Elaboration_Checks_Suppressed (Scope (Ent))
1611 then
1612 if Nkind (N) in N_Subprogram_Call then
1613 Set_No_Elaboration_Check (N);
1614 end if;
1615 end if;
1617 return;
1619 -- Static model, call is not in elaboration code, we
1620 -- never need to worry, because in the static model the
1621 -- top-level caller always takes care of things.
1623 else
1624 return;
1625 end if;
1626 end if;
1627 end loop;
1628 end;
1629 end if;
1630 end if;
1632 Ent := Get_Referenced_Ent (N);
1634 if No (Ent) then
1635 return;
1636 end if;
1638 -- Determine whether a prior call to the same subprogram was already
1639 -- examined within the same context. If this is the case, then there is
1640 -- no need to proceed with the various warnings and checks because the
1641 -- work was already done for the previous call.
1643 declare
1644 Self : constant Visited_Element :=
1645 (Subp_Id => Ent, Context => Parent (N));
1647 begin
1648 for Index in 1 .. Elab_Visited.Last loop
1649 if Self = Elab_Visited.Table (Index) then
1650 return;
1651 end if;
1652 end loop;
1653 end;
1655 -- See if we need to analyze this reference. We analyze it if either of
1656 -- the following conditions is met:
1658 -- It is an inner level call (since in this case it was triggered
1659 -- by an outer level call from elaboration code), but only if the
1660 -- call is within the scope of the original outer level call.
1662 -- It is an outer level reference from elaboration code, or a call to
1663 -- an entity is in the same elaboration scope.
1665 -- And in these cases, we will check both inter-unit calls and
1666 -- intra-unit (within a single unit) calls.
1668 C_Scope := Current_Scope;
1670 -- If not outer level reference, then we follow it if it is within the
1671 -- original scope of the outer reference.
1673 if Present (Outer_Scope)
1674 and then Within (Scope (Ent), Outer_Scope)
1675 then
1676 Set_C_Scope;
1677 Check_A_Call
1678 (N => N,
1679 E => Ent,
1680 Outer_Scope => Outer_Scope,
1681 Inter_Unit_Only => False,
1682 In_Init_Proc => In_Init_Proc);
1684 -- Nothing to do if elaboration checks suppressed for this scope.
1685 -- However, an interesting exception, the fact that elaboration checks
1686 -- are suppressed within an instance (because we can trace the body when
1687 -- we process the template) does not extend to calls to generic formal
1688 -- subprograms.
1690 elsif Elaboration_Checks_Suppressed (Current_Scope)
1691 and then not Is_Call_Of_Generic_Formal (N)
1692 then
1693 null;
1695 elsif From_Elab_Code then
1696 Set_C_Scope;
1697 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1699 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1700 Set_C_Scope;
1701 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1703 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1704 -- is set, then we will do the check, but only in the inter-unit case
1705 -- (this is to accommodate unguarded elaboration calls from other units
1706 -- in which this same mode is set). We don't want warnings in this case,
1707 -- it would generate warnings having nothing to do with elaboration.
1709 elsif Dynamic_Elaboration_Checks then
1710 Set_C_Scope;
1711 Check_A_Call
1713 Ent,
1714 Standard_Standard,
1715 Inter_Unit_Only => True,
1716 Generate_Warnings => False);
1718 -- Otherwise nothing to do
1720 else
1721 return;
1722 end if;
1724 -- A call to an Init_Proc in elaboration code may bring additional
1725 -- dependencies, if some of the record components thereof have
1726 -- initializations that are function calls that come from source. We
1727 -- treat the current node as a call to each of these functions, to check
1728 -- their elaboration impact.
1730 if Is_Init_Proc (Ent) and then From_Elab_Code then
1731 Process_Init_Proc : declare
1732 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1734 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1735 -- Find subprogram calls within body of Init_Proc for Traverse
1736 -- instantiation below.
1738 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1739 -- Traversal procedure to find all calls with body of Init_Proc
1741 ---------------------
1742 -- Check_Init_Call --
1743 ---------------------
1745 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1746 Func : Entity_Id;
1748 begin
1749 if Nkind (Nod) in N_Subprogram_Call
1750 and then Is_Entity_Name (Name (Nod))
1751 then
1752 Func := Entity (Name (Nod));
1754 if Comes_From_Source (Func) then
1755 Check_A_Call
1756 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1757 end if;
1759 return OK;
1761 else
1762 return OK;
1763 end if;
1764 end Check_Init_Call;
1766 -- Start of processing for Process_Init_Proc
1768 begin
1769 if Nkind (Unit_Decl) = N_Subprogram_Body then
1770 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1771 end if;
1772 end Process_Init_Proc;
1773 end if;
1774 end Check_Elab_Call;
1776 -----------------------
1777 -- Check_Elab_Assign --
1778 -----------------------
1780 procedure Check_Elab_Assign (N : Node_Id) is
1781 Ent : Entity_Id;
1782 Scop : Entity_Id;
1784 Pkg_Spec : Entity_Id;
1785 Pkg_Body : Entity_Id;
1787 begin
1788 -- For record or array component, check prefix. If it is an access type,
1789 -- then there is nothing to do (we do not know what is being assigned),
1790 -- but otherwise this is an assignment to the prefix.
1792 if Nkind_In (N, N_Indexed_Component,
1793 N_Selected_Component,
1794 N_Slice)
1795 then
1796 if not Is_Access_Type (Etype (Prefix (N))) then
1797 Check_Elab_Assign (Prefix (N));
1798 end if;
1800 return;
1801 end if;
1803 -- For type conversion, check expression
1805 if Nkind (N) = N_Type_Conversion then
1806 Check_Elab_Assign (Expression (N));
1807 return;
1808 end if;
1810 -- Nothing to do if this is not an entity reference otherwise get entity
1812 if Is_Entity_Name (N) then
1813 Ent := Entity (N);
1814 else
1815 return;
1816 end if;
1818 -- What we are looking for is a reference in the body of a package that
1819 -- modifies a variable declared in the visible part of the package spec.
1821 if Present (Ent)
1822 and then Comes_From_Source (N)
1823 and then not Suppress_Elaboration_Warnings (Ent)
1824 and then Ekind (Ent) = E_Variable
1825 and then not In_Private_Part (Ent)
1826 and then Is_Library_Level_Entity (Ent)
1827 then
1828 Scop := Current_Scope;
1829 loop
1830 if No (Scop) or else Scop = Standard_Standard then
1831 return;
1832 elsif Ekind (Scop) = E_Package
1833 and then Is_Compilation_Unit (Scop)
1834 then
1835 exit;
1836 else
1837 Scop := Scope (Scop);
1838 end if;
1839 end loop;
1841 -- Here Scop points to the containing library package
1843 Pkg_Spec := Scop;
1844 Pkg_Body := Body_Entity (Pkg_Spec);
1846 -- All OK if the package has an Elaborate_Body pragma
1848 if Has_Pragma_Elaborate_Body (Scop) then
1849 return;
1850 end if;
1852 -- OK if entity being modified is not in containing package spec
1854 if not In_Same_Source_Unit (Scop, Ent) then
1855 return;
1856 end if;
1858 -- All OK if entity appears in generic package or generic instance.
1859 -- We just get too messed up trying to give proper warnings in the
1860 -- presence of generics. Better no message than a junk one.
1862 Scop := Scope (Ent);
1863 while Present (Scop) and then Scop /= Pkg_Spec loop
1864 if Ekind (Scop) = E_Generic_Package then
1865 return;
1866 elsif Ekind (Scop) = E_Package
1867 and then Is_Generic_Instance (Scop)
1868 then
1869 return;
1870 end if;
1872 Scop := Scope (Scop);
1873 end loop;
1875 -- All OK if in task, don't issue warnings there
1877 if In_Task_Activation then
1878 return;
1879 end if;
1881 -- OK if no package body
1883 if No (Pkg_Body) then
1884 return;
1885 end if;
1887 -- OK if reference is not in package body
1889 if not In_Same_Source_Unit (Pkg_Body, N) then
1890 return;
1891 end if;
1893 -- OK if package body has no handled statement sequence
1895 declare
1896 HSS : constant Node_Id :=
1897 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1898 begin
1899 if No (HSS) or else not Comes_From_Source (HSS) then
1900 return;
1901 end if;
1902 end;
1904 -- We definitely have a case of a modification of an entity in
1905 -- the package spec from the elaboration code of the package body.
1906 -- We may not give the warning (because there are some additional
1907 -- checks to avoid too many false positives), but it would be a good
1908 -- idea for the binder to try to keep the body elaboration close to
1909 -- the spec elaboration.
1911 Set_Elaborate_Body_Desirable (Pkg_Spec);
1913 -- All OK in gnat mode (we know what we are doing)
1915 if GNAT_Mode then
1916 return;
1917 end if;
1919 -- All OK if all warnings suppressed
1921 if Warning_Mode = Suppress then
1922 return;
1923 end if;
1925 -- All OK if elaboration checks suppressed for entity
1927 if Checks_May_Be_Suppressed (Ent)
1928 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1929 then
1930 return;
1931 end if;
1933 -- OK if the entity is initialized. Note that the No_Initialization
1934 -- flag usually means that the initialization has been rewritten into
1935 -- assignments, but that still counts for us.
1937 declare
1938 Decl : constant Node_Id := Declaration_Node (Ent);
1939 begin
1940 if Nkind (Decl) = N_Object_Declaration
1941 and then (Present (Expression (Decl))
1942 or else No_Initialization (Decl))
1943 then
1944 return;
1945 end if;
1946 end;
1948 -- Here is where we give the warning
1950 -- All OK if warnings suppressed on the entity
1952 if not Has_Warnings_Off (Ent) then
1953 Error_Msg_Sloc := Sloc (Ent);
1955 Error_Msg_NE
1956 ("??& can be accessed by clients before this initialization",
1957 N, Ent);
1958 Error_Msg_NE
1959 ("\??add Elaborate_Body to spec to ensure & is initialized",
1960 N, Ent);
1961 end if;
1963 if not All_Errors_Mode then
1964 Set_Suppress_Elaboration_Warnings (Ent);
1965 end if;
1966 end if;
1967 end Check_Elab_Assign;
1969 ----------------------
1970 -- Check_Elab_Calls --
1971 ----------------------
1973 -- WARNING: This routine manages SPARK regions
1975 procedure Check_Elab_Calls is
1976 Saved_SM : SPARK_Mode_Type;
1977 Saved_SMP : Node_Id;
1979 begin
1980 -- If expansion is disabled, do not generate any checks, unless we
1981 -- are in GNATprove mode, so that errors are issued in GNATprove for
1982 -- violations of static elaboration rules in SPARK code. Also skip
1983 -- checks if any subunits are missing because in either case we lack the
1984 -- full information that we need, and no object file will be created in
1985 -- any case.
1987 if (not Expander_Active and not GNATprove_Mode)
1988 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1989 or else Subunits_Missing
1990 then
1991 return;
1992 end if;
1994 -- Skip delayed calls if we had any errors
1996 if Serious_Errors_Detected = 0 then
1997 Delaying_Elab_Checks := False;
1998 Expander_Mode_Save_And_Set (True);
2000 for J in Delay_Check.First .. Delay_Check.Last loop
2001 Push_Scope (Delay_Check.Table (J).Curscop);
2002 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
2003 In_Task_Activation := Delay_Check.Table (J).In_Task_Activation;
2005 Saved_SM := SPARK_Mode;
2006 Saved_SMP := SPARK_Mode_Pragma;
2008 -- Set appropriate value of SPARK_Mode
2010 if Delay_Check.Table (J).From_SPARK_Code then
2011 SPARK_Mode := On;
2012 end if;
2014 Check_Internal_Call_Continue
2015 (N => Delay_Check.Table (J).N,
2016 E => Delay_Check.Table (J).E,
2017 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
2018 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
2020 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
2021 Pop_Scope;
2022 end loop;
2024 -- Set Delaying_Elab_Checks back on for next main compilation
2026 Expander_Mode_Restore;
2027 Delaying_Elab_Checks := True;
2028 end if;
2029 end Check_Elab_Calls;
2031 ------------------------------
2032 -- Check_Elab_Instantiation --
2033 ------------------------------
2035 procedure Check_Elab_Instantiation
2036 (N : Node_Id;
2037 Outer_Scope : Entity_Id := Empty)
2039 Ent : Entity_Id;
2041 begin
2042 -- Check for and deal with bad instantiation case. There is some
2043 -- duplicated code here, but we will worry about this later ???
2045 Check_Bad_Instantiation (N);
2047 if ABE_Is_Certain (N) then
2048 return;
2049 end if;
2051 -- Nothing to do if we do not have an instantiation (happens in some
2052 -- error cases, and also in the formal package declaration case)
2054 if Nkind (N) not in N_Generic_Instantiation then
2055 return;
2056 end if;
2058 -- Nothing to do if inside a generic template
2060 if Inside_A_Generic then
2061 return;
2062 end if;
2064 -- Nothing to do if the instantiation is not in the main unit
2066 if not In_Extended_Main_Code_Unit (N) then
2067 return;
2068 end if;
2070 Ent := Get_Generic_Entity (N);
2071 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
2073 -- See if we need to analyze this instantiation. We analyze it if
2074 -- either of the following conditions is met:
2076 -- It is an inner level instantiation (since in this case it was
2077 -- triggered by an outer level call from elaboration code), but
2078 -- only if the instantiation is within the scope of the original
2079 -- outer level call.
2081 -- It is an outer level instantiation from elaboration code, or the
2082 -- instantiated entity is in the same elaboration scope.
2084 -- And in these cases, we will check both the inter-unit case and
2085 -- the intra-unit (within a single unit) case.
2087 C_Scope := Current_Scope;
2089 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
2090 Set_C_Scope;
2091 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
2093 elsif From_Elab_Code then
2094 Set_C_Scope;
2095 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
2097 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
2098 Set_C_Scope;
2099 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
2101 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2102 -- set, then we will do the check, but only in the inter-unit case (this
2103 -- is to accommodate unguarded elaboration calls from other units in
2104 -- which this same mode is set). We inhibit warnings in this case, since
2105 -- this instantiation is not occurring in elaboration code.
2107 elsif Dynamic_Elaboration_Checks then
2108 Set_C_Scope;
2109 Check_A_Call
2111 Ent,
2112 Standard_Standard,
2113 Inter_Unit_Only => True,
2114 Generate_Warnings => False);
2116 else
2117 return;
2118 end if;
2119 end Check_Elab_Instantiation;
2121 -------------------------
2122 -- Check_Internal_Call --
2123 -------------------------
2125 procedure Check_Internal_Call
2126 (N : Node_Id;
2127 E : Entity_Id;
2128 Outer_Scope : Entity_Id;
2129 Orig_Ent : Entity_Id)
2131 function Within_Initial_Condition (Call : Node_Id) return Boolean;
2132 -- Determine whether call Call occurs within pragma Initial_Condition or
2133 -- pragma Check with check_kind set to Initial_Condition.
2135 ------------------------------
2136 -- Within_Initial_Condition --
2137 ------------------------------
2139 function Within_Initial_Condition (Call : Node_Id) return Boolean is
2140 Args : List_Id;
2141 Nam : Name_Id;
2142 Par : Node_Id;
2144 begin
2145 -- Traverse the parent chain looking for an enclosing pragma
2147 Par := Call;
2148 while Present (Par) loop
2149 if Nkind (Par) = N_Pragma then
2150 Nam := Pragma_Name (Par);
2152 -- Pragma Initial_Condition appears in its alternative from as
2153 -- Check (Initial_Condition, ...).
2155 if Nam = Name_Check then
2156 Args := Pragma_Argument_Associations (Par);
2158 -- Pragma Check should have at least two arguments
2160 pragma Assert (Present (Args));
2162 return
2163 Chars (Expression (First (Args))) = Name_Initial_Condition;
2165 -- Direct match
2167 elsif Nam = Name_Initial_Condition then
2168 return True;
2170 -- Since pragmas are never nested within other pragmas, stop
2171 -- the traversal.
2173 else
2174 return False;
2175 end if;
2177 -- Prevent the search from going too far
2179 elsif Is_Body_Or_Package_Declaration (Par) then
2180 exit;
2181 end if;
2183 Par := Parent (Par);
2185 -- If assertions are not enabled, the check pragma is rewritten
2186 -- as an if_statement in sem_prag, to generate various warnings
2187 -- on boolean expressions. Retrieve the original pragma.
2189 if Nkind (Original_Node (Par)) = N_Pragma then
2190 Par := Original_Node (Par);
2191 end if;
2192 end loop;
2194 return False;
2195 end Within_Initial_Condition;
2197 -- Local variables
2199 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
2201 -- Start of processing for Check_Internal_Call
2203 begin
2204 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2205 -- node comes from source.
2207 if Nkind (N) = N_Attribute_Reference
2208 and then ((not Warn_On_Elab_Access and then not Debug_Flag_Dot_O)
2209 or else not Comes_From_Source (N))
2210 then
2211 return;
2213 -- If not function or procedure call, instantiation, or 'Access, then
2214 -- ignore call (this happens in some error cases and rewriting cases).
2216 elsif not Nkind_In (N, N_Attribute_Reference,
2217 N_Function_Call,
2218 N_Procedure_Call_Statement)
2219 and then not Inst_Case
2220 then
2221 return;
2223 -- Nothing to do if this is a call or instantiation that has already
2224 -- been found to be a sure ABE.
2226 elsif Nkind (N) /= N_Attribute_Reference and then ABE_Is_Certain (N) then
2227 return;
2229 -- Nothing to do if errors already detected (avoid cascaded errors)
2231 elsif Serious_Errors_Detected /= 0 then
2232 return;
2234 -- Nothing to do if not in full analysis mode
2236 elsif not Full_Analysis then
2237 return;
2239 -- Nothing to do if analyzing in special spec-expression mode, since the
2240 -- call is not actually being made at this time.
2242 elsif In_Spec_Expression then
2243 return;
2245 -- Nothing to do for call to intrinsic subprogram
2247 elsif Is_Intrinsic_Subprogram (E) then
2248 return;
2250 -- Nothing to do if call is within a generic unit
2252 elsif Inside_A_Generic then
2253 return;
2255 -- Nothing to do when the call appears within pragma Initial_Condition.
2256 -- The pragma is part of the elaboration statements of a package body
2257 -- and may only call external subprograms or subprograms whose body is
2258 -- already available.
2260 elsif Within_Initial_Condition (N) then
2261 return;
2262 end if;
2264 -- Delay this call if we are still delaying calls
2266 if Delaying_Elab_Checks then
2267 Delay_Check.Append
2268 ((N => N,
2269 E => E,
2270 Orig_Ent => Orig_Ent,
2271 Curscop => Current_Scope,
2272 Outer_Scope => Outer_Scope,
2273 From_Elab_Code => From_Elab_Code,
2274 In_Task_Activation => In_Task_Activation,
2275 From_SPARK_Code => SPARK_Mode = On));
2276 return;
2278 -- Otherwise, call phase 2 continuation right now
2280 else
2281 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
2282 end if;
2283 end Check_Internal_Call;
2285 ----------------------------------
2286 -- Check_Internal_Call_Continue --
2287 ----------------------------------
2289 procedure Check_Internal_Call_Continue
2290 (N : Node_Id;
2291 E : Entity_Id;
2292 Outer_Scope : Entity_Id;
2293 Orig_Ent : Entity_Id)
2295 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
2296 -- Function applied to each node as we traverse the body. Checks for
2297 -- call or entity reference that needs checking, and if so checks it.
2298 -- Always returns OK, so entire tree is traversed, except that as
2299 -- described below subprogram bodies are skipped for now.
2301 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2302 -- Traverse procedure using above Find_Elab_Reference function
2304 -------------------------
2305 -- Find_Elab_Reference --
2306 -------------------------
2308 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2309 Actual : Node_Id;
2311 begin
2312 -- If user has specified that there are no entry calls in elaboration
2313 -- code, do not trace past an accept statement, because the rendez-
2314 -- vous will happen after elaboration.
2316 if Nkind_In (Original_Node (N), N_Accept_Statement,
2317 N_Selective_Accept)
2318 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2319 then
2320 return Abandon;
2322 -- If we have a function call, check it
2324 elsif Nkind (N) = N_Function_Call then
2325 Check_Elab_Call (N, Outer_Scope);
2326 return OK;
2328 -- If we have a procedure call, check the call, and also check
2329 -- arguments that are assignments (OUT or IN OUT mode formals).
2331 elsif Nkind (N) = N_Procedure_Call_Statement then
2332 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2334 Actual := First_Actual (N);
2335 while Present (Actual) loop
2336 if Known_To_Be_Assigned (Actual) then
2337 Check_Elab_Assign (Actual);
2338 end if;
2340 Next_Actual (Actual);
2341 end loop;
2343 return OK;
2345 -- If we have an access attribute for a subprogram, check it.
2346 -- Suppress this behavior under debug flag.
2348 elsif not Debug_Flag_Dot_UU
2349 and then Nkind (N) = N_Attribute_Reference
2350 and then Nam_In (Attribute_Name (N), Name_Access,
2351 Name_Unrestricted_Access)
2352 and then Is_Entity_Name (Prefix (N))
2353 and then Is_Subprogram (Entity (Prefix (N)))
2354 then
2355 Check_Elab_Call (N, Outer_Scope);
2356 return OK;
2358 -- In SPARK mode, if we have an entity reference to a variable, then
2359 -- check it. For now we consider any reference.
2361 elsif SPARK_Mode = On
2362 and then Nkind (N) in N_Has_Entity
2363 and then Present (Entity (N))
2364 and then Ekind (Entity (N)) = E_Variable
2365 then
2366 Check_Elab_Call (N, Outer_Scope);
2367 return OK;
2369 -- If we have a generic instantiation, check it
2371 elsif Nkind (N) in N_Generic_Instantiation then
2372 Check_Elab_Instantiation (N, Outer_Scope);
2373 return OK;
2375 -- Skip subprogram bodies that come from source (wait for call to
2376 -- analyze these). The reason for the come from source test is to
2377 -- avoid catching task bodies.
2379 -- For task bodies, we should really avoid these too, waiting for the
2380 -- task activation, but that's too much trouble to catch for now, so
2381 -- we go in unconditionally. This is not so terrible, it means the
2382 -- error backtrace is not quite complete, and we are too eager to
2383 -- scan bodies of tasks that are unused, but this is hardly very
2384 -- significant.
2386 elsif Nkind (N) = N_Subprogram_Body
2387 and then Comes_From_Source (N)
2388 then
2389 return Skip;
2391 elsif Nkind (N) = N_Assignment_Statement
2392 and then Comes_From_Source (N)
2393 then
2394 Check_Elab_Assign (Name (N));
2395 return OK;
2397 else
2398 return OK;
2399 end if;
2400 end Find_Elab_Reference;
2402 Inst_Case : constant Boolean := Is_Generic_Unit (E);
2403 Loc : constant Source_Ptr := Sloc (N);
2405 Ebody : Entity_Id;
2406 Sbody : Node_Id;
2408 -- Start of processing for Check_Internal_Call_Continue
2410 begin
2411 -- Save outer level call if at outer level
2413 if Elab_Call.Last = 0 then
2414 Outer_Level_Sloc := Loc;
2415 end if;
2417 -- If the call is to a function that renames a literal, no check needed
2419 if Ekind (E) = E_Enumeration_Literal then
2420 return;
2421 end if;
2423 -- Register the subprogram as examined within this particular context.
2424 -- This ensures that calls to the same subprogram but in different
2425 -- contexts receive warnings and checks of their own since the calls
2426 -- may be reached through different flow paths.
2428 Elab_Visited.Append ((Subp_Id => E, Context => Parent (N)));
2430 Sbody := Unit_Declaration_Node (E);
2432 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2433 Ebody := Corresponding_Body (Sbody);
2435 if No (Ebody) then
2436 return;
2437 else
2438 Sbody := Unit_Declaration_Node (Ebody);
2439 end if;
2440 end if;
2442 -- If the body appears after the outer level call or instantiation then
2443 -- we have an error case handled below.
2445 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2446 and then not In_Task_Activation
2447 then
2448 null;
2450 -- If we have the instantiation case we are done, since we now know that
2451 -- the body of the generic appeared earlier.
2453 elsif Inst_Case then
2454 return;
2456 -- Otherwise we have a call, so we trace through the called body to see
2457 -- if it has any problems.
2459 else
2460 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2462 Elab_Call.Append ((Cloc => Loc, Ent => E));
2464 if Debug_Flag_LL then
2465 Write_Str ("Elab_Call.Last = ");
2466 Write_Int (Int (Elab_Call.Last));
2467 Write_Str (" Ent = ");
2468 Write_Name (Chars (E));
2469 Write_Str (" at ");
2470 Write_Location (Sloc (N));
2471 Write_Eol;
2472 end if;
2474 -- Now traverse declarations and statements of subprogram body. Note
2475 -- that we cannot simply Traverse (Sbody), since traverse does not
2476 -- normally visit subprogram bodies.
2478 declare
2479 Decl : Node_Id;
2480 begin
2481 Decl := First (Declarations (Sbody));
2482 while Present (Decl) loop
2483 Traverse (Decl);
2484 Next (Decl);
2485 end loop;
2486 end;
2488 Traverse (Handled_Statement_Sequence (Sbody));
2490 Elab_Call.Decrement_Last;
2491 return;
2492 end if;
2494 -- Here is the case of calling a subprogram where the body has not yet
2495 -- been encountered. A warning message is needed, except if this is the
2496 -- case of appearing within an aspect specification that results in
2497 -- a check call, we do not really have such a situation, so no warning
2498 -- is needed (e.g. the case of a precondition, where the call appears
2499 -- textually before the body, but in actual fact is moved to the
2500 -- appropriate subprogram body and so does not need a check).
2502 declare
2503 P : Node_Id;
2504 O : Node_Id;
2506 begin
2507 P := Parent (N);
2508 loop
2509 -- Keep looking at parents if we are still in the subexpression
2511 if Nkind (P) in N_Subexpr then
2512 P := Parent (P);
2514 -- Here P is the parent of the expression, check for special case
2516 else
2517 O := Original_Node (P);
2519 -- Definitely not the special case if orig node is not a pragma
2521 exit when Nkind (O) /= N_Pragma;
2523 -- Check we have an If statement or a null statement (happens
2524 -- when the If has been expanded to be True).
2526 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2528 -- Our special case will be indicated either by the pragma
2529 -- coming from an aspect ...
2531 if Present (Corresponding_Aspect (O)) then
2532 return;
2534 -- Or, in the case of an initial condition, specifically by a
2535 -- Check pragma specifying an Initial_Condition check.
2537 elsif Pragma_Name (O) = Name_Check
2538 and then
2539 Chars
2540 (Expression (First (Pragma_Argument_Associations (O)))) =
2541 Name_Initial_Condition
2542 then
2543 return;
2545 -- For anything else, we have an error
2547 else
2548 exit;
2549 end if;
2550 end if;
2551 end loop;
2552 end;
2554 -- Not that special case, warning and dynamic check is required
2556 -- If we have nothing in the call stack, then this is at the outer
2557 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2558 -- it's a renaming.
2560 if Elab_Call.Last = 0 then
2561 Error_Msg_Warn := SPARK_Mode /= On;
2563 declare
2564 Insert_Check : Boolean := True;
2565 -- This flag is set to True if an elaboration check should be
2566 -- inserted.
2568 begin
2569 if In_Task_Activation then
2570 Insert_Check := False;
2572 elsif Inst_Case then
2573 Error_Msg_NE
2574 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2576 elsif Nkind (N) = N_Attribute_Reference then
2577 Error_Msg_NE
2578 ("Access attribute of & before body seen<<", N, Orig_Ent);
2579 Error_Msg_N ("\possible Program_Error on later references<", N);
2580 Insert_Check := False;
2582 elsif Nkind (Unit_Declaration_Node (Orig_Ent)) /=
2583 N_Subprogram_Renaming_Declaration
2584 then
2585 Error_Msg_NE
2586 ("cannot call& before body seen<<", N, Orig_Ent);
2588 elsif not Is_Generic_Actual_Subprogram (Orig_Ent) then
2589 Insert_Check := False;
2590 end if;
2592 if Insert_Check then
2593 Error_Msg_N ("\Program_Error [<<", N);
2594 Insert_Elab_Check (N);
2595 end if;
2596 end;
2598 -- Call is not at outer level
2600 else
2601 -- Do not generate elaboration checks in GNATprove mode because the
2602 -- elaboration counter and the check are both forms of expansion.
2604 if GNATprove_Mode then
2605 null;
2607 -- Generate an elaboration check
2609 elsif not Elaboration_Checks_Suppressed (E) then
2610 Set_Elaboration_Entity_Required (E);
2612 -- Create a declaration of the elaboration entity, and insert it
2613 -- prior to the subprogram or the generic unit, within the same
2614 -- scope. Since the subprogram may be overloaded, create a unique
2615 -- entity.
2617 if No (Elaboration_Entity (E)) then
2618 declare
2619 Loce : constant Source_Ptr := Sloc (E);
2620 Ent : constant Entity_Id :=
2621 Make_Defining_Identifier (Loc,
2622 New_External_Name (Chars (E), 'E', -1));
2624 begin
2625 Set_Elaboration_Entity (E, Ent);
2626 Push_Scope (Scope (E));
2628 Insert_Action (Declaration_Node (E),
2629 Make_Object_Declaration (Loce,
2630 Defining_Identifier => Ent,
2631 Object_Definition =>
2632 New_Occurrence_Of (Standard_Short_Integer, Loce),
2633 Expression =>
2634 Make_Integer_Literal (Loc, Uint_0)));
2636 -- Set elaboration flag at the point of the body
2638 Set_Elaboration_Flag (Sbody, E);
2640 -- Kill current value indication. This is necessary because
2641 -- the tests of this flag are inserted out of sequence and
2642 -- must not pick up bogus indications of the wrong constant
2643 -- value. Also, this is never a true constant, since one way
2644 -- or another, it gets reset.
2646 Set_Current_Value (Ent, Empty);
2647 Set_Last_Assignment (Ent, Empty);
2648 Set_Is_True_Constant (Ent, False);
2649 Pop_Scope;
2650 end;
2651 end if;
2653 -- Generate:
2654 -- if Enn = 0 then
2655 -- raise Program_Error with "access before elaboration";
2656 -- end if;
2658 Insert_Elab_Check (N,
2659 Make_Attribute_Reference (Loc,
2660 Attribute_Name => Name_Elaborated,
2661 Prefix => New_Occurrence_Of (E, Loc)));
2662 end if;
2664 -- Generate the warning
2666 if not Suppress_Elaboration_Warnings (E)
2667 and then not Elaboration_Checks_Suppressed (E)
2669 -- Suppress this warning if we have a function call that occurred
2670 -- within an assertion expression, since we can get false warnings
2671 -- in this case, due to the out of order handling in this case.
2673 and then
2674 (Nkind (Original_Node (N)) /= N_Function_Call
2675 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2676 then
2677 Error_Msg_Warn := SPARK_Mode /= On;
2679 if Inst_Case then
2680 Error_Msg_NE
2681 ("instantiation of& may occur before body is seen<l<",
2682 N, Orig_Ent);
2683 else
2684 -- A rather specific check. For Finalize/Adjust/Initialize, if
2685 -- the type has Warnings_Off set, suppress the warning.
2687 if Nam_In (Chars (E), Name_Adjust,
2688 Name_Finalize,
2689 Name_Initialize)
2690 and then Present (First_Formal (E))
2691 then
2692 declare
2693 T : constant Entity_Id := Etype (First_Formal (E));
2694 begin
2695 if Is_Controlled (T) then
2696 if Warnings_Off (T)
2697 or else (Ekind (T) = E_Private_Type
2698 and then Warnings_Off (Full_View (T)))
2699 then
2700 goto Output;
2701 end if;
2702 end if;
2703 end;
2704 end if;
2706 -- Go ahead and give warning if not this special case
2708 Error_Msg_NE
2709 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2710 end if;
2712 Error_Msg_N ("\Program_Error ]<l<", N);
2714 -- There is no need to query the elaboration warning message flags
2715 -- because the main message is an error, not a warning, therefore
2716 -- all the clarification messages produces by Output_Calls must be
2717 -- emitted unconditionally.
2719 <<Output>>
2721 Output_Calls (N, Check_Elab_Flag => False);
2722 end if;
2723 end if;
2724 end Check_Internal_Call_Continue;
2726 ---------------------------
2727 -- Check_Task_Activation --
2728 ---------------------------
2730 procedure Check_Task_Activation (N : Node_Id) is
2731 Loc : constant Source_Ptr := Sloc (N);
2732 Inter_Procs : constant Elist_Id := New_Elmt_List;
2733 Intra_Procs : constant Elist_Id := New_Elmt_List;
2734 Ent : Entity_Id;
2735 P : Entity_Id;
2736 Task_Scope : Entity_Id;
2737 Cunit_SC : Boolean := False;
2738 Decl : Node_Id;
2739 Elmt : Elmt_Id;
2740 Enclosing : Entity_Id;
2742 procedure Add_Task_Proc (Typ : Entity_Id);
2743 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2744 -- For record types, this procedure recurses over component types.
2746 procedure Collect_Tasks (Decls : List_Id);
2747 -- Collect the types of the tasks that are to be activated in the given
2748 -- list of declarations, in order to perform elaboration checks on the
2749 -- corresponding task procedures that are called implicitly here.
2751 function Outer_Unit (E : Entity_Id) return Entity_Id;
2752 -- find enclosing compilation unit of Entity, ignoring subunits, or
2753 -- else enclosing subprogram. If E is not a package, there is no need
2754 -- for inter-unit elaboration checks.
2756 -------------------
2757 -- Add_Task_Proc --
2758 -------------------
2760 procedure Add_Task_Proc (Typ : Entity_Id) is
2761 Comp : Entity_Id;
2762 Proc : Entity_Id := Empty;
2764 begin
2765 if Is_Task_Type (Typ) then
2766 Proc := Get_Task_Body_Procedure (Typ);
2768 elsif Is_Array_Type (Typ)
2769 and then Has_Task (Base_Type (Typ))
2770 then
2771 Add_Task_Proc (Component_Type (Typ));
2773 elsif Is_Record_Type (Typ)
2774 and then Has_Task (Base_Type (Typ))
2775 then
2776 Comp := First_Component (Typ);
2777 while Present (Comp) loop
2778 Add_Task_Proc (Etype (Comp));
2779 Comp := Next_Component (Comp);
2780 end loop;
2781 end if;
2783 -- If the task type is another unit, we will perform the usual
2784 -- elaboration check on its enclosing unit. If the type is in the
2785 -- same unit, we can trace the task body as for an internal call,
2786 -- but we only need to examine other external calls, because at
2787 -- the point the task is activated, internal subprogram bodies
2788 -- will have been elaborated already. We keep separate lists for
2789 -- each kind of task.
2791 -- Skip this test if errors have occurred, since in this case
2792 -- we can get false indications.
2794 if Serious_Errors_Detected /= 0 then
2795 return;
2796 end if;
2798 if Present (Proc) then
2799 if Outer_Unit (Scope (Proc)) = Enclosing then
2801 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2802 and then
2803 (not Is_Generic_Instance (Scope (Proc))
2804 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2805 then
2806 Error_Msg_Warn := SPARK_Mode /= On;
2807 Error_Msg_N
2808 ("task will be activated before elaboration of its body<<",
2809 Decl);
2810 Error_Msg_N ("\Program_Error [<<", Decl);
2812 elsif Present
2813 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2814 then
2815 Append_Elmt (Proc, Intra_Procs);
2816 end if;
2818 else
2819 -- No need for multiple entries of the same type
2821 Elmt := First_Elmt (Inter_Procs);
2822 while Present (Elmt) loop
2823 if Node (Elmt) = Proc then
2824 return;
2825 end if;
2827 Next_Elmt (Elmt);
2828 end loop;
2830 Append_Elmt (Proc, Inter_Procs);
2831 end if;
2832 end if;
2833 end Add_Task_Proc;
2835 -------------------
2836 -- Collect_Tasks --
2837 -------------------
2839 procedure Collect_Tasks (Decls : List_Id) is
2840 begin
2841 if Present (Decls) then
2842 Decl := First (Decls);
2843 while Present (Decl) loop
2844 if Nkind (Decl) = N_Object_Declaration
2845 and then Has_Task (Etype (Defining_Identifier (Decl)))
2846 then
2847 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2848 end if;
2850 Next (Decl);
2851 end loop;
2852 end if;
2853 end Collect_Tasks;
2855 ----------------
2856 -- Outer_Unit --
2857 ----------------
2859 function Outer_Unit (E : Entity_Id) return Entity_Id is
2860 Outer : Entity_Id;
2862 begin
2863 Outer := E;
2864 while Present (Outer) loop
2865 if Elaboration_Checks_Suppressed (Outer) then
2866 Cunit_SC := True;
2867 end if;
2869 exit when Is_Child_Unit (Outer)
2870 or else Scope (Outer) = Standard_Standard
2871 or else Ekind (Outer) /= E_Package;
2872 Outer := Scope (Outer);
2873 end loop;
2875 return Outer;
2876 end Outer_Unit;
2878 -- Start of processing for Check_Task_Activation
2880 begin
2881 Enclosing := Outer_Unit (Current_Scope);
2883 -- Find all tasks declared in the current unit
2885 if Nkind (N) = N_Package_Body then
2886 P := Unit_Declaration_Node (Corresponding_Spec (N));
2888 Collect_Tasks (Declarations (N));
2889 Collect_Tasks (Visible_Declarations (Specification (P)));
2890 Collect_Tasks (Private_Declarations (Specification (P)));
2892 elsif Nkind (N) = N_Package_Declaration then
2893 Collect_Tasks (Visible_Declarations (Specification (N)));
2894 Collect_Tasks (Private_Declarations (Specification (N)));
2896 else
2897 Collect_Tasks (Declarations (N));
2898 end if;
2900 -- We only perform detailed checks in all tasks that are library level
2901 -- entities. If the master is a subprogram or task, activation will
2902 -- depend on the activation of the master itself.
2904 -- Should dynamic checks be added in the more general case???
2906 if Ekind (Enclosing) /= E_Package then
2907 return;
2908 end if;
2910 -- For task types defined in other units, we want the unit containing
2911 -- the task body to be elaborated before the current one.
2913 Elmt := First_Elmt (Inter_Procs);
2914 while Present (Elmt) loop
2915 Ent := Node (Elmt);
2916 Task_Scope := Outer_Unit (Scope (Ent));
2918 if not Is_Compilation_Unit (Task_Scope) then
2919 null;
2921 elsif Suppress_Elaboration_Warnings (Task_Scope)
2922 or else Elaboration_Checks_Suppressed (Task_Scope)
2923 then
2924 null;
2926 elsif Dynamic_Elaboration_Checks then
2927 if not Elaboration_Checks_Suppressed (Ent)
2928 and then not Cunit_SC
2929 and then not Restriction_Active
2930 (No_Entry_Calls_In_Elaboration_Code)
2931 then
2932 -- Runtime elaboration check required. Generate check of the
2933 -- elaboration counter for the unit containing the entity.
2935 Insert_Elab_Check (N,
2936 Make_Attribute_Reference (Loc,
2937 Prefix =>
2938 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc),
2939 Attribute_Name => Name_Elaborated));
2940 end if;
2942 else
2943 -- Force the binder to elaborate other unit first
2945 if Elab_Info_Messages
2946 and then not Suppress_Elaboration_Warnings (Ent)
2947 and then not Elaboration_Checks_Suppressed (Ent)
2948 and then not Suppress_Elaboration_Warnings (Task_Scope)
2949 and then not Elaboration_Checks_Suppressed (Task_Scope)
2950 then
2951 Error_Msg_Node_2 := Task_Scope;
2952 Error_Msg_NE
2953 ("info: activation of an instance of task type & requires "
2954 & "pragma Elaborate_All on &?$?", N, Ent);
2955 end if;
2957 Activate_Elaborate_All_Desirable (N, Task_Scope);
2958 Set_Suppress_Elaboration_Warnings (Task_Scope);
2959 end if;
2961 Next_Elmt (Elmt);
2962 end loop;
2964 -- For tasks declared in the current unit, trace other calls within
2965 -- the task procedure bodies, which are available.
2967 In_Task_Activation := True;
2969 Elmt := First_Elmt (Intra_Procs);
2970 while Present (Elmt) loop
2971 Ent := Node (Elmt);
2972 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2973 Next_Elmt (Elmt);
2974 end loop;
2976 In_Task_Activation := False;
2977 end Check_Task_Activation;
2979 -------------------------------
2980 -- Is_Call_Of_Generic_Formal --
2981 -------------------------------
2983 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2984 begin
2985 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2987 -- Always return False if debug flag -gnatd.G is set
2989 and then not Debug_Flag_Dot_GG
2991 -- For now, we detect this by looking for the strange identifier
2992 -- node, whose Chars reflect the name of the generic formal, but
2993 -- the Chars of the Entity references the generic actual.
2995 and then Nkind (Name (N)) = N_Identifier
2996 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
2997 end Is_Call_Of_Generic_Formal;
2999 --------------------------------
3000 -- Set_Elaboration_Constraint --
3001 --------------------------------
3003 procedure Set_Elaboration_Constraint
3004 (Call : Node_Id;
3005 Subp : Entity_Id;
3006 Scop : Entity_Id)
3008 Elab_Unit : Entity_Id;
3010 -- Check whether this is a call to an Initialize subprogram for a
3011 -- controlled type. Note that Call can also be a 'Access attribute
3012 -- reference, which now generates an elaboration check.
3014 Init_Call : constant Boolean :=
3015 Nkind (Call) = N_Procedure_Call_Statement
3016 and then Chars (Subp) = Name_Initialize
3017 and then Comes_From_Source (Subp)
3018 and then Present (Parameter_Associations (Call))
3019 and then Is_Controlled (Etype (First_Actual (Call)));
3021 begin
3022 -- If the unit is mentioned in a with_clause of the current unit, it is
3023 -- visible, and we can set the elaboration flag.
3025 if Is_Immediately_Visible (Scop)
3026 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
3027 then
3028 Activate_Elaborate_All_Desirable (Call, Scop);
3029 Set_Suppress_Elaboration_Warnings (Scop);
3030 return;
3031 end if;
3033 -- If this is not an initialization call or a call using object notation
3034 -- we know that the unit of the called entity is in the context, and we
3035 -- can set the flag as well. The unit need not be visible if the call
3036 -- occurs within an instantiation.
3038 if Is_Init_Proc (Subp)
3039 or else Init_Call
3040 or else Nkind (Original_Node (Call)) = N_Selected_Component
3041 then
3042 null; -- detailed processing follows.
3044 else
3045 Activate_Elaborate_All_Desirable (Call, Scop);
3046 Set_Suppress_Elaboration_Warnings (Scop);
3047 return;
3048 end if;
3050 -- If the unit is not in the context, there must be an intermediate unit
3051 -- that is, on which we need to place to elaboration flag. This happens
3052 -- with init proc calls.
3054 if Is_Init_Proc (Subp) or else Init_Call then
3056 -- The initialization call is on an object whose type is not declared
3057 -- in the same scope as the subprogram. The type of the object must
3058 -- be a subtype of the type of operation. This object is the first
3059 -- actual in the call.
3061 declare
3062 Typ : constant Entity_Id :=
3063 Etype (First (Parameter_Associations (Call)));
3064 begin
3065 Elab_Unit := Scope (Typ);
3066 while (Present (Elab_Unit))
3067 and then not Is_Compilation_Unit (Elab_Unit)
3068 loop
3069 Elab_Unit := Scope (Elab_Unit);
3070 end loop;
3071 end;
3073 -- If original node uses selected component notation, the prefix is
3074 -- visible and determines the scope that must be elaborated. After
3075 -- rewriting, the prefix is the first actual in the call.
3077 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
3078 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
3080 -- Not one of special cases above
3082 else
3083 -- Using previously computed scope. If the elaboration check is
3084 -- done after analysis, the scope is not visible any longer, but
3085 -- must still be in the context.
3087 Elab_Unit := Scop;
3088 end if;
3090 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
3091 Set_Suppress_Elaboration_Warnings (Elab_Unit);
3092 end Set_Elaboration_Constraint;
3094 ------------------------
3095 -- Get_Referenced_Ent --
3096 ------------------------
3098 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
3099 Nam : Node_Id;
3101 begin
3102 if Nkind (N) in N_Has_Entity
3103 and then Present (Entity (N))
3104 and then Ekind (Entity (N)) = E_Variable
3105 then
3106 return Entity (N);
3107 end if;
3109 if Nkind (N) = N_Attribute_Reference then
3110 Nam := Prefix (N);
3111 else
3112 Nam := Name (N);
3113 end if;
3115 if No (Nam) then
3116 return Empty;
3117 elsif Nkind (Nam) = N_Selected_Component then
3118 return Entity (Selector_Name (Nam));
3119 elsif not Is_Entity_Name (Nam) then
3120 return Empty;
3121 else
3122 return Entity (Nam);
3123 end if;
3124 end Get_Referenced_Ent;
3126 ----------------------
3127 -- Has_Generic_Body --
3128 ----------------------
3130 function Has_Generic_Body (N : Node_Id) return Boolean is
3131 Ent : constant Entity_Id := Get_Generic_Entity (N);
3132 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
3133 Scop : Entity_Id;
3135 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
3136 -- Determine if the list of nodes headed by N and linked by Next
3137 -- contains a package body for the package spec entity E, and if so
3138 -- return the package body. If not, then returns Empty.
3140 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
3141 -- This procedure is called load the unit whose name is given by Nam.
3142 -- This unit is being loaded to see whether it contains an optional
3143 -- generic body. The returned value is the loaded unit, which is always
3144 -- a package body (only package bodies can contain other entities in the
3145 -- sense in which Has_Generic_Body is interested). We only attempt to
3146 -- load bodies if we are generating code. If we are in semantics check
3147 -- only mode, then it would be wrong to load bodies that are not
3148 -- required from a semantic point of view, so in this case we return
3149 -- Empty. The result is that the caller may incorrectly decide that a
3150 -- generic spec does not have a body when in fact it does, but the only
3151 -- harm in this is that some warnings on elaboration problems may be
3152 -- lost in semantic checks only mode, which is not big loss. We also
3153 -- return Empty if we go for a body and it is not there.
3155 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
3156 -- PE is the entity for a package spec. This function locates the
3157 -- corresponding package body, returning Empty if none is found. The
3158 -- package body returned is fully parsed but may not yet be analyzed,
3159 -- so only syntactic fields should be referenced.
3161 ------------------
3162 -- Find_Body_In --
3163 ------------------
3165 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
3166 Nod : Node_Id;
3168 begin
3169 Nod := N;
3170 while Present (Nod) loop
3172 -- If we found the package body we are looking for, return it
3174 if Nkind (Nod) = N_Package_Body
3175 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
3176 then
3177 return Nod;
3179 -- If we found the stub for the body, go after the subunit,
3180 -- loading it if necessary.
3182 elsif Nkind (Nod) = N_Package_Body_Stub
3183 and then Chars (Defining_Identifier (Nod)) = Chars (E)
3184 then
3185 if Present (Library_Unit (Nod)) then
3186 return Unit (Library_Unit (Nod));
3188 else
3189 return Load_Package_Body (Get_Unit_Name (Nod));
3190 end if;
3192 -- If neither package body nor stub, keep looking on chain
3194 else
3195 Next (Nod);
3196 end if;
3197 end loop;
3199 return Empty;
3200 end Find_Body_In;
3202 -----------------------
3203 -- Load_Package_Body --
3204 -----------------------
3206 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
3207 U : Unit_Number_Type;
3209 begin
3210 if Operating_Mode /= Generate_Code then
3211 return Empty;
3212 else
3213 U :=
3214 Load_Unit
3215 (Load_Name => Nam,
3216 Required => False,
3217 Subunit => False,
3218 Error_Node => N);
3220 if U = No_Unit then
3221 return Empty;
3222 else
3223 return Unit (Cunit (U));
3224 end if;
3225 end if;
3226 end Load_Package_Body;
3228 -------------------------------
3229 -- Locate_Corresponding_Body --
3230 -------------------------------
3232 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
3233 Spec : constant Node_Id := Declaration_Node (PE);
3234 Decl : constant Node_Id := Parent (Spec);
3235 Scop : constant Entity_Id := Scope (PE);
3236 PBody : Node_Id;
3238 begin
3239 if Is_Library_Level_Entity (PE) then
3241 -- If package is a library unit that requires a body, we have no
3242 -- choice but to go after that body because it might contain an
3243 -- optional body for the original generic package.
3245 if Unit_Requires_Body (PE) then
3247 -- Load the body. Note that we are a little careful here to use
3248 -- Spec to get the unit number, rather than PE or Decl, since
3249 -- in the case where the package is itself a library level
3250 -- instantiation, Spec will properly reference the generic
3251 -- template, which is what we really want.
3253 return
3254 Load_Package_Body
3255 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
3257 -- But if the package is a library unit that does NOT require
3258 -- a body, then no body is permitted, so we are sure that there
3259 -- is no body for the original generic package.
3261 else
3262 return Empty;
3263 end if;
3265 -- Otherwise look and see if we are embedded in a further package
3267 elsif Is_Package_Or_Generic_Package (Scop) then
3269 -- If so, get the body of the enclosing package, and look in
3270 -- its package body for the package body we are looking for.
3272 PBody := Locate_Corresponding_Body (Scop);
3274 if No (PBody) then
3275 return Empty;
3276 else
3277 return Find_Body_In (PE, First (Declarations (PBody)));
3278 end if;
3280 -- If we are not embedded in a further package, then the body
3281 -- must be in the same declarative part as we are.
3283 else
3284 return Find_Body_In (PE, Next (Decl));
3285 end if;
3286 end Locate_Corresponding_Body;
3288 -- Start of processing for Has_Generic_Body
3290 begin
3291 if Present (Corresponding_Body (Decl)) then
3292 return True;
3294 elsif Unit_Requires_Body (Ent) then
3295 return True;
3297 -- Compilation units cannot have optional bodies
3299 elsif Is_Compilation_Unit (Ent) then
3300 return False;
3302 -- Otherwise look at what scope we are in
3304 else
3305 Scop := Scope (Ent);
3307 -- Case of entity is in other than a package spec, in this case
3308 -- the body, if present, must be in the same declarative part.
3310 if not Is_Package_Or_Generic_Package (Scop) then
3311 declare
3312 P : Node_Id;
3314 begin
3315 -- Declaration node may get us a spec, so if so, go to
3316 -- the parent declaration.
3318 P := Declaration_Node (Ent);
3319 while not Is_List_Member (P) loop
3320 P := Parent (P);
3321 end loop;
3323 return Present (Find_Body_In (Ent, Next (P)));
3324 end;
3326 -- If the entity is in a package spec, then we have to locate
3327 -- the corresponding package body, and look there.
3329 else
3330 declare
3331 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
3333 begin
3334 if No (PBody) then
3335 return False;
3336 else
3337 return
3338 Present
3339 (Find_Body_In (Ent, (First (Declarations (PBody)))));
3340 end if;
3341 end;
3342 end if;
3343 end if;
3344 end Has_Generic_Body;
3346 -----------------------
3347 -- Insert_Elab_Check --
3348 -----------------------
3350 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
3351 Nod : Node_Id;
3352 Loc : constant Source_Ptr := Sloc (N);
3354 Chk : Node_Id;
3355 -- The check (N_Raise_Program_Error) node to be inserted
3357 begin
3358 -- If expansion is disabled, do not generate any checks. Also
3359 -- skip checks if any subunits are missing because in either
3360 -- case we lack the full information that we need, and no object
3361 -- file will be created in any case.
3363 if not Expander_Active or else Subunits_Missing then
3364 return;
3365 end if;
3367 -- If we have a generic instantiation, where Instance_Spec is set,
3368 -- then this field points to a generic instance spec that has
3369 -- been inserted before the instantiation node itself, so that
3370 -- is where we want to insert a check.
3372 if Nkind (N) in N_Generic_Instantiation
3373 and then Present (Instance_Spec (N))
3374 then
3375 Nod := Instance_Spec (N);
3376 else
3377 Nod := N;
3378 end if;
3380 -- Build check node, possibly with condition
3382 Chk :=
3383 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3385 if Present (C) then
3386 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3387 end if;
3389 -- If we are inserting at the top level, insert in Aux_Decls
3391 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3392 declare
3393 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3395 begin
3396 if No (Declarations (ADN)) then
3397 Set_Declarations (ADN, New_List (Chk));
3398 else
3399 Append_To (Declarations (ADN), Chk);
3400 end if;
3402 Analyze (Chk);
3403 end;
3405 -- Otherwise just insert as an action on the node in question
3407 else
3408 Insert_Action (Nod, Chk);
3409 end if;
3410 end Insert_Elab_Check;
3412 -------------------------------
3413 -- Is_Finalization_Procedure --
3414 -------------------------------
3416 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3417 begin
3418 -- Check whether Id is a procedure with at least one parameter
3420 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3421 declare
3422 Typ : constant Entity_Id := Etype (First_Formal (Id));
3423 Deep_Fin : Entity_Id := Empty;
3424 Fin : Entity_Id := Empty;
3426 begin
3427 -- If the type of the first formal does not require finalization
3428 -- actions, then this is definitely not [Deep_]Finalize.
3430 if not Needs_Finalization (Typ) then
3431 return False;
3432 end if;
3434 -- At this point we have the following scenario:
3436 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3438 -- Recover the two possible versions of [Deep_]Finalize using the
3439 -- type of the first parameter and compare with the input.
3441 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3443 if Is_Controlled (Typ) then
3444 Fin := Find_Prim_Op (Typ, Name_Finalize);
3445 end if;
3447 return (Present (Deep_Fin) and then Id = Deep_Fin)
3448 or else (Present (Fin) and then Id = Fin);
3449 end;
3450 end if;
3452 return False;
3453 end Is_Finalization_Procedure;
3455 ------------------
3456 -- Output_Calls --
3457 ------------------
3459 procedure Output_Calls
3460 (N : Node_Id;
3461 Check_Elab_Flag : Boolean)
3463 function Emit (Flag : Boolean) return Boolean;
3464 -- Determine whether to emit an error message based on the combination
3465 -- of flags Check_Elab_Flag and Flag.
3467 function Is_Printable_Error_Name return Boolean;
3468 -- An internal function, used to determine if a name, stored in the
3469 -- Name_Buffer, is either a non-internal name, or is an internal name
3470 -- that is printable by the error message circuits (i.e. it has a single
3471 -- upper case letter at the end).
3473 ----------
3474 -- Emit --
3475 ----------
3477 function Emit (Flag : Boolean) return Boolean is
3478 begin
3479 if Check_Elab_Flag then
3480 return Flag;
3481 else
3482 return True;
3483 end if;
3484 end Emit;
3486 -----------------------------
3487 -- Is_Printable_Error_Name --
3488 -----------------------------
3490 function Is_Printable_Error_Name return Boolean is
3491 begin
3492 if not Is_Internal_Name then
3493 return True;
3495 elsif Name_Len = 1 then
3496 return False;
3498 else
3499 Name_Len := Name_Len - 1;
3500 return not Is_Internal_Name;
3501 end if;
3502 end Is_Printable_Error_Name;
3504 -- Local variables
3506 Ent : Entity_Id;
3508 -- Start of processing for Output_Calls
3510 begin
3511 for J in reverse 1 .. Elab_Call.Last loop
3512 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3514 Ent := Elab_Call.Table (J).Ent;
3515 Get_Name_String (Chars (Ent));
3517 -- Dynamic elaboration model, warnings controlled by -gnatwl
3519 if Dynamic_Elaboration_Checks then
3520 if Emit (Elab_Warnings) then
3521 if Is_Generic_Unit (Ent) then
3522 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3523 elsif Is_Init_Proc (Ent) then
3524 Error_Msg_N ("\\?l?initialization procedure called #", N);
3525 elsif Is_Printable_Error_Name then
3526 Error_Msg_NE ("\\?l?& called #", N, Ent);
3527 else
3528 Error_Msg_N ("\\?l?called #", N);
3529 end if;
3530 end if;
3532 -- Static elaboration model, info messages controlled by -gnatel
3534 else
3535 if Emit (Elab_Info_Messages) then
3536 if Is_Generic_Unit (Ent) then
3537 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3538 elsif Is_Init_Proc (Ent) then
3539 Error_Msg_N ("\\?$?initialization procedure called #", N);
3540 elsif Is_Printable_Error_Name then
3541 Error_Msg_NE ("\\?$?& called #", N, Ent);
3542 else
3543 Error_Msg_N ("\\?$?called #", N);
3544 end if;
3545 end if;
3546 end if;
3547 end loop;
3548 end Output_Calls;
3550 ----------------------------
3551 -- Same_Elaboration_Scope --
3552 ----------------------------
3554 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3555 S1 : Entity_Id;
3556 S2 : Entity_Id;
3558 begin
3559 -- Find elaboration scope for Scop1
3560 -- This is either a subprogram or a compilation unit.
3562 S1 := Scop1;
3563 while S1 /= Standard_Standard
3564 and then not Is_Compilation_Unit (S1)
3565 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3566 loop
3567 S1 := Scope (S1);
3568 end loop;
3570 -- Find elaboration scope for Scop2
3572 S2 := Scop2;
3573 while S2 /= Standard_Standard
3574 and then not Is_Compilation_Unit (S2)
3575 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3576 loop
3577 S2 := Scope (S2);
3578 end loop;
3580 return S1 = S2;
3581 end Same_Elaboration_Scope;
3583 -----------------
3584 -- Set_C_Scope --
3585 -----------------
3587 procedure Set_C_Scope is
3588 begin
3589 while not Is_Compilation_Unit (C_Scope) loop
3590 C_Scope := Scope (C_Scope);
3591 end loop;
3592 end Set_C_Scope;
3594 -----------------
3595 -- Spec_Entity --
3596 -----------------
3598 function Spec_Entity (E : Entity_Id) return Entity_Id is
3599 Decl : Node_Id;
3601 begin
3602 -- Check for case of body entity
3603 -- Why is the check for E_Void needed???
3605 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3606 Decl := E;
3608 loop
3609 Decl := Parent (Decl);
3610 exit when Nkind (Decl) in N_Proper_Body;
3611 end loop;
3613 return Corresponding_Spec (Decl);
3615 else
3616 return E;
3617 end if;
3618 end Spec_Entity;
3620 -------------------
3621 -- Supply_Bodies --
3622 -------------------
3624 procedure Supply_Bodies (N : Node_Id) is
3625 begin
3626 if Nkind (N) = N_Subprogram_Declaration then
3627 declare
3628 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3630 begin
3631 -- Internal subprograms will already have a generated body, so
3632 -- there is no need to provide a stub for them.
3634 if No (Corresponding_Body (N)) then
3635 declare
3636 Loc : constant Source_Ptr := Sloc (N);
3637 Formals : constant List_Id := Copy_Parameter_List (Ent);
3638 Nam : constant Entity_Id :=
3639 Make_Defining_Identifier (Loc, Chars (Ent));
3640 Stats : constant List_Id :=
3641 New_List (
3642 Make_Raise_Program_Error (Loc,
3643 Reason => PE_Access_Before_Elaboration));
3644 Spec : Node_Id;
3646 begin
3647 if Ekind (Ent) = E_Function then
3648 Spec :=
3649 Make_Function_Specification (Loc,
3650 Defining_Unit_Name => Nam,
3651 Parameter_Specifications => Formals,
3652 Result_Definition =>
3653 New_Copy_Tree
3654 (Result_Definition (Specification (N))));
3656 -- We cannot reliably make a return statement for this
3657 -- body, but none is needed because the call raises
3658 -- program error.
3660 Set_Return_Present (Ent);
3662 else
3663 Spec :=
3664 Make_Procedure_Specification (Loc,
3665 Defining_Unit_Name => Nam,
3666 Parameter_Specifications => Formals);
3667 end if;
3669 Insert_After_And_Analyze (N,
3670 Make_Subprogram_Body (Loc,
3671 Specification => Spec,
3672 Declarations => New_List,
3673 Handled_Statement_Sequence =>
3674 Make_Handled_Sequence_Of_Statements (Loc, Stats)));
3675 end;
3676 end if;
3677 end;
3679 elsif Nkind (N) = N_Package_Declaration then
3680 declare
3681 Spec : constant Node_Id := Specification (N);
3682 begin
3683 Push_Scope (Defining_Unit_Name (Spec));
3684 Supply_Bodies (Visible_Declarations (Spec));
3685 Supply_Bodies (Private_Declarations (Spec));
3686 Pop_Scope;
3687 end;
3688 end if;
3689 end Supply_Bodies;
3691 procedure Supply_Bodies (L : List_Id) is
3692 Elmt : Node_Id;
3693 begin
3694 if Present (L) then
3695 Elmt := First (L);
3696 while Present (Elmt) loop
3697 Supply_Bodies (Elmt);
3698 Next (Elmt);
3699 end loop;
3700 end if;
3701 end Supply_Bodies;
3703 ------------
3704 -- Within --
3705 ------------
3707 function Within (E1, E2 : Entity_Id) return Boolean is
3708 Scop : Entity_Id;
3709 begin
3710 Scop := E1;
3711 loop
3712 if Scop = E2 then
3713 return True;
3714 elsif Scop = Standard_Standard then
3715 return False;
3716 else
3717 Scop := Scope (Scop);
3718 end if;
3719 end loop;
3720 end Within;
3722 --------------------------
3723 -- Within_Elaborate_All --
3724 --------------------------
3726 function Within_Elaborate_All
3727 (Unit : Unit_Number_Type;
3728 E : Entity_Id) return Boolean
3730 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3731 pragma Pack (Unit_Number_Set);
3733 Seen : Unit_Number_Set := (others => False);
3734 -- Seen (X) is True after we have seen unit X in the walk. This is used
3735 -- to prevent processing the same unit more than once.
3737 Result : Boolean := False;
3739 procedure Helper (Unit : Unit_Number_Type);
3740 -- This helper procedure does all the work for Within_Elaborate_All. It
3741 -- walks the dependency graph, and sets Result to True if it finds an
3742 -- appropriate Elaborate_All.
3744 ------------
3745 -- Helper --
3746 ------------
3748 procedure Helper (Unit : Unit_Number_Type) is
3749 CU : constant Node_Id := Cunit (Unit);
3751 Item : Node_Id;
3752 Item2 : Node_Id;
3753 Elab_Id : Entity_Id;
3754 Par : Node_Id;
3756 begin
3757 if Seen (Unit) then
3758 return;
3759 else
3760 Seen (Unit) := True;
3761 end if;
3763 -- First, check for Elaborate_Alls on this unit
3765 Item := First (Context_Items (CU));
3766 while Present (Item) loop
3767 if Nkind (Item) = N_Pragma
3768 and then Pragma_Name (Item) = Name_Elaborate_All
3769 then
3770 -- Return if some previous error on the pragma itself. The
3771 -- pragma may be unanalyzed, because of a previous error, or
3772 -- if it is the context of a subunit, inherited by its parent.
3774 if Error_Posted (Item) or else not Analyzed (Item) then
3775 return;
3776 end if;
3778 Elab_Id :=
3779 Entity
3780 (Expression (First (Pragma_Argument_Associations (Item))));
3782 if E = Elab_Id then
3783 Result := True;
3784 return;
3785 end if;
3787 Par := Parent (Unit_Declaration_Node (Elab_Id));
3789 Item2 := First (Context_Items (Par));
3790 while Present (Item2) loop
3791 if Nkind (Item2) = N_With_Clause
3792 and then Entity (Name (Item2)) = E
3793 and then not Limited_Present (Item2)
3794 then
3795 Result := True;
3796 return;
3797 end if;
3799 Next (Item2);
3800 end loop;
3801 end if;
3803 Next (Item);
3804 end loop;
3806 -- Second, recurse on with's. We could do this as part of the above
3807 -- loop, but it's probably more efficient to have two loops, because
3808 -- the relevant Elaborate_All is likely to be on the initial unit. In
3809 -- other words, we're walking the with's breadth-first. This part is
3810 -- only necessary in the dynamic elaboration model.
3812 if Dynamic_Elaboration_Checks then
3813 Item := First (Context_Items (CU));
3814 while Present (Item) loop
3815 if Nkind (Item) = N_With_Clause
3816 and then not Limited_Present (Item)
3817 then
3818 -- Note: the following call to Get_Cunit_Unit_Number does a
3819 -- linear search, which could be slow, but it's OK because
3820 -- we're about to give a warning anyway. Also, there might
3821 -- be hundreds of units, but not millions. If it turns out
3822 -- to be a problem, we could store the Get_Cunit_Unit_Number
3823 -- in each N_Compilation_Unit node, but that would involve
3824 -- rearranging N_Compilation_Unit_Aux to make room.
3826 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3828 if Result then
3829 return;
3830 end if;
3831 end if;
3833 Next (Item);
3834 end loop;
3835 end if;
3836 end Helper;
3838 -- Start of processing for Within_Elaborate_All
3840 begin
3841 Helper (Unit);
3842 return Result;
3843 end Within_Elaborate_All;
3845 end Sem_Elab;