* MAINTAINERS: Add a note that maintainership also includes web
[official-gcc.git] / gcc / ada / sem_elab.adb
blob7be57cfce97901d60292ddc830fbf5b85177b190
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;
725 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
726 -- check, we don't mind in this case if the call occurs before the body
727 -- since this is all generated code.
729 elsif Nkind (Original_Node (N)) = N_Attribute_Reference
730 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
731 then
732 return;
734 -- Intrinsics such as instances of Unchecked_Deallocation do not have
735 -- any body, so elaboration checking is not needed, and would be wrong.
737 elsif Is_Intrinsic_Subprogram (E) then
738 return;
740 -- Do not consider references to internal variables for SPARK semantics
742 elsif Variable_Case and then not Comes_From_Source (E) then
743 return;
744 end if;
746 -- Proceed with check
748 Ent := E;
750 -- For a variable reference, just set Body_Acts_As_Spec to False
752 if Variable_Case then
753 Body_Acts_As_Spec := False;
755 -- Additional checks for all other cases
757 else
758 -- Go to parent for derived subprogram, or to original subprogram in
759 -- the case of a renaming (Alias covers both these cases).
761 loop
762 if (Suppress_Elaboration_Warnings (Ent)
763 or else Elaboration_Checks_Suppressed (Ent))
764 and then (Inst_Case or else No (Alias (Ent)))
765 then
766 return;
767 end if;
769 -- Nothing to do for imported entities
771 if Is_Imported (Ent) then
772 return;
773 end if;
775 exit when Inst_Case or else No (Alias (Ent));
776 Ent := Alias (Ent);
777 end loop;
779 Decl := Unit_Declaration_Node (Ent);
781 if Nkind (Decl) = N_Subprogram_Body then
782 Body_Acts_As_Spec := True;
784 elsif Nkind_In (Decl, N_Subprogram_Declaration,
785 N_Subprogram_Body_Stub)
786 or else Inst_Case
787 then
788 Body_Acts_As_Spec := False;
790 -- If we have none of an instantiation, subprogram body or subprogram
791 -- declaration, or in the SPARK case, a variable reference, then
792 -- it is not a case that we want to check. (One case is a call to a
793 -- generic formal subprogram, where we do not want the check in the
794 -- template).
796 else
797 return;
798 end if;
799 end if;
801 E_Scope := Ent;
802 loop
803 if Elaboration_Checks_Suppressed (E_Scope)
804 or else Suppress_Elaboration_Warnings (E_Scope)
805 then
806 Cunit_SC := True;
807 end if;
809 -- Exit when we get to compilation unit, not counting subunits
811 exit when Is_Compilation_Unit (E_Scope)
812 and then (Is_Child_Unit (E_Scope)
813 or else Scope (E_Scope) = Standard_Standard);
815 pragma Assert (E_Scope /= Standard_Standard);
817 -- Move up a scope looking for compilation unit
819 E_Scope := Scope (E_Scope);
820 end loop;
822 -- No checks needed for pure or preelaborated compilation units
824 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
825 return;
826 end if;
828 -- If the generic entity is within a deeper instance than we are, then
829 -- either the instantiation to which we refer itself caused an ABE, in
830 -- which case that will be handled separately, or else we know that the
831 -- body we need appears as needed at the point of the instantiation.
832 -- However, this assumption is only valid if we are in static mode.
834 if not Dynamic_Elaboration_Checks
835 and then
836 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
837 then
838 return;
839 end if;
841 -- Do not give a warning for a package with no body
843 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
844 return;
845 end if;
847 -- Case of entity is in same unit as call or instantiation. In the
848 -- instantiation case, W_Scope may be different from E_Scope; we want
849 -- the unit in which the instantiation occurs, since we're analyzing
850 -- based on the expansion.
852 if W_Scope = C_Scope then
853 if not Inter_Unit_Only then
854 Check_Internal_Call (N, Ent, Outer_Scope, E);
855 end if;
857 return;
858 end if;
860 -- Case of entity is not in current unit (i.e. with'ed unit case)
862 -- We are only interested in such calls if the outer call was from
863 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
865 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
866 return;
867 end if;
869 -- Nothing to do if some scope said that no checks were required
871 if Cunit_SC then
872 return;
873 end if;
875 -- Nothing to do for a generic instance, because a call to an instance
876 -- cannot fail the elaboration check, because the body of the instance
877 -- is always elaborated immediately after the spec.
879 if Call_To_Instance_From_Outside (Ent) then
880 return;
881 end if;
883 -- Nothing to do if subprogram with no separate spec. However, a call
884 -- to Deep_Initialize may result in a call to a user-defined Initialize
885 -- procedure, which imposes a body dependency. This happens only if the
886 -- type is controlled and the Initialize procedure is not inherited.
888 if Body_Acts_As_Spec then
889 if Is_TSS (Ent, TSS_Deep_Initialize) then
890 declare
891 Typ : constant Entity_Id := Etype (First_Formal (Ent));
892 Init : Entity_Id;
894 begin
895 if not Is_Controlled (Typ) then
896 return;
897 else
898 Init := Find_Prim_Op (Typ, Name_Initialize);
900 if Comes_From_Source (Init) then
901 Ent := Init;
902 else
903 return;
904 end if;
905 end if;
906 end;
908 else
909 return;
910 end if;
911 end if;
913 -- Check cases of internal units
915 Callee_Unit_Internal := In_Internal_Unit (E_Scope);
917 -- Do not give a warning if the with'ed unit is internal and this is
918 -- the generic instantiation case (this saves a lot of hassle dealing
919 -- with the Text_IO special child units)
921 if Callee_Unit_Internal and Inst_Case then
922 return;
923 end if;
925 if C_Scope = Standard_Standard then
926 Caller_Unit_Internal := False;
927 else
928 Caller_Unit_Internal := In_Internal_Unit (C_Scope);
929 end if;
931 -- Do not give a warning if the with'ed unit is internal and the caller
932 -- is not internal (since the binder always elaborates internal units
933 -- first).
935 if Callee_Unit_Internal and not Caller_Unit_Internal then
936 return;
937 end if;
939 -- For now, if debug flag -gnatdE is not set, do no checking for one
940 -- internal unit withing another. This fixes the problem with the sgi
941 -- build and storage errors. To be resolved later ???
943 if (Callee_Unit_Internal and Caller_Unit_Internal)
944 and not Debug_Flag_EE
945 then
946 return;
947 end if;
949 if Is_TSS (E, TSS_Deep_Initialize) then
950 Ent := E;
951 end if;
953 -- If the call is in an instance, and the called entity is not
954 -- defined in the same instance, then the elaboration issue focuses
955 -- around the unit containing the template, it is this unit that
956 -- requires an Elaborate_All.
958 -- However, if we are doing dynamic elaboration, we need to chase the
959 -- call in the usual manner.
961 -- We also need to chase the call in the usual manner if it is a call
962 -- to a generic formal parameter, since that case was not handled as
963 -- part of the processing of the template.
965 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
966 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
968 if Inst_Caller = No_Location then
969 Unit_Caller := No_Unit;
970 else
971 Unit_Caller := Get_Source_Unit (N);
972 end if;
974 if Inst_Callee = No_Location then
975 Unit_Callee := No_Unit;
976 else
977 Unit_Callee := Get_Source_Unit (Ent);
978 end if;
980 if Unit_Caller /= No_Unit
981 and then Unit_Callee /= Unit_Caller
982 and then not Dynamic_Elaboration_Checks
983 and then not Is_Call_Of_Generic_Formal (N)
984 then
985 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
987 -- If we don't get a spec entity, just ignore call. Not quite
988 -- clear why this check is necessary. ???
990 if No (E_Scope) then
991 return;
992 end if;
994 -- Otherwise step to enclosing compilation unit
996 while not Is_Compilation_Unit (E_Scope) loop
997 E_Scope := Scope (E_Scope);
998 end loop;
1000 -- For the case where N is not an instance, and is not a call within
1001 -- instance to other than a generic formal, we recompute E_Scope
1002 -- for the error message, since we do NOT want to go to the unit
1003 -- that has the ultimate declaration in the case of renaming and
1004 -- derivation and we also want to go to the generic unit in the
1005 -- case of an instance, and no further.
1007 else
1008 -- Loop to carefully follow renamings and derivations one step
1009 -- outside the current unit, but not further.
1011 if not (Inst_Case or Variable_Case)
1012 and then Present (Alias (Ent))
1013 then
1014 E_Scope := Alias (Ent);
1015 else
1016 E_Scope := Ent;
1017 end if;
1019 loop
1020 while not Is_Compilation_Unit (E_Scope) loop
1021 E_Scope := Scope (E_Scope);
1022 end loop;
1024 -- If E_Scope is the same as C_Scope, it means that there
1025 -- definitely was a local renaming or derivation, and we
1026 -- are not yet out of the current unit.
1028 exit when E_Scope /= C_Scope;
1029 Ent := Alias (Ent);
1030 E_Scope := Ent;
1032 -- If no alias, there could be a previous error, but not if we've
1033 -- already reached the outermost level (Standard).
1035 if No (Ent) then
1036 return;
1037 end if;
1038 end loop;
1039 end if;
1041 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
1042 return;
1043 end if;
1045 -- Determine whether the Default_Initial_Condition procedure of some
1046 -- type is being invoked.
1048 Is_DIC := Ekind (Ent) = E_Procedure and then Is_DIC_Procedure (Ent);
1050 -- Checks related to Default_Initial_Condition fall under the SPARK
1051 -- umbrella because this is a SPARK-specific annotation.
1053 SPARK_Elab_Errors :=
1054 SPARK_Mode = On and (Is_DIC or Dynamic_Elaboration_Checks);
1056 -- Now check if an Elaborate_All (or dynamic check) is needed
1058 if (Elab_Info_Messages or Elab_Warnings or SPARK_Elab_Errors)
1059 and then Generate_Warnings
1060 and then not Suppress_Elaboration_Warnings (Ent)
1061 and then not Elaboration_Checks_Suppressed (Ent)
1062 and then not Suppress_Elaboration_Warnings (E_Scope)
1063 and then not Elaboration_Checks_Suppressed (E_Scope)
1064 then
1065 -- Instantiation case
1067 if Inst_Case then
1068 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
1069 Error_Msg_NE
1070 ("instantiation of & during elaboration in SPARK", N, Ent);
1071 else
1072 Elab_Warning
1073 ("instantiation of & may raise Program_Error?l?",
1074 "info: instantiation of & during elaboration?$?", Ent);
1075 end if;
1077 -- Indirect call case, info message only in static elaboration
1078 -- case, because the attribute reference itself cannot raise an
1079 -- exception. Note that SPARK does not permit indirect calls.
1081 elsif Access_Case then
1082 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
1084 -- Variable reference in SPARK mode
1086 elsif Variable_Case then
1087 if Comes_From_Source (Ent) and then SPARK_Elab_Errors then
1088 Error_Msg_NE
1089 ("reference to & during elaboration in SPARK", N, Ent);
1090 end if;
1092 -- Subprogram call case
1094 else
1095 if Nkind (Name (N)) in N_Has_Entity
1096 and then Is_Init_Proc (Entity (Name (N)))
1097 and then Comes_From_Source (Ent)
1098 then
1099 Elab_Warning
1100 ("implicit call to & may raise Program_Error?l?",
1101 "info: implicit call to & during elaboration?$?",
1102 Ent);
1104 elsif SPARK_Elab_Errors then
1106 -- Emit a specialized error message when the elaboration of an
1107 -- object of a private type evaluates the expression of pragma
1108 -- Default_Initial_Condition. This prevents the internal name
1109 -- of the procedure from appearing in the error message.
1111 if Is_DIC then
1112 Error_Msg_N
1113 ("call to Default_Initial_Condition during elaboration in "
1114 & "SPARK", N);
1115 else
1116 Error_Msg_NE
1117 ("call to & during elaboration in SPARK", N, Ent);
1118 end if;
1120 else
1121 Elab_Warning
1122 ("call to & may raise Program_Error?l?",
1123 "info: call to & during elaboration?$?",
1124 Ent);
1125 end if;
1126 end if;
1128 Error_Msg_Qual_Level := Nat'Last;
1130 -- Case of Elaborate_All not present and required, for SPARK this
1131 -- is an error, so give an error message.
1133 if SPARK_Elab_Errors then
1134 Error_Msg_NE -- CODEFIX
1135 ("\Elaborate_All pragma required for&", N, W_Scope);
1137 -- Otherwise we generate an implicit pragma. For a subprogram
1138 -- instantiation, Elaborate is good enough, since no transitive
1139 -- call is possible at elaboration time in this case.
1141 elsif Nkind (N) in N_Subprogram_Instantiation then
1142 Elab_Warning
1143 ("\missing pragma Elaborate for&?l?",
1144 "\implicit pragma Elaborate for& generated?$?",
1145 W_Scope);
1147 -- For all other cases, we need an implicit Elaborate_All
1149 else
1150 Elab_Warning
1151 ("\missing pragma Elaborate_All for&?l?",
1152 "\implicit pragma Elaborate_All for & generated?$?",
1153 W_Scope);
1154 end if;
1156 Error_Msg_Qual_Level := 0;
1158 -- Take into account the flags related to elaboration warning
1159 -- messages when enumerating the various calls involved. This
1160 -- ensures the proper pairing of the main warning and the
1161 -- clarification messages generated by Output_Calls.
1163 Output_Calls (N, Check_Elab_Flag => True);
1165 -- Set flag to prevent further warnings for same unit unless in
1166 -- All_Errors_Mode.
1168 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
1169 Set_Suppress_Elaboration_Warnings (W_Scope);
1170 end if;
1171 end if;
1173 -- Check for runtime elaboration check required
1175 if Dynamic_Elaboration_Checks then
1176 if not Elaboration_Checks_Suppressed (Ent)
1177 and then not Elaboration_Checks_Suppressed (W_Scope)
1178 and then not Elaboration_Checks_Suppressed (E_Scope)
1179 and then not Cunit_SC
1180 then
1181 -- Runtime elaboration check required. Generate check of the
1182 -- elaboration Boolean for the unit containing the entity.
1184 -- Note that for this case, we do check the real unit (the one
1185 -- from following renamings, since that is the issue).
1187 -- Could this possibly miss a useless but required PE???
1189 Insert_Elab_Check (N,
1190 Make_Attribute_Reference (Loc,
1191 Attribute_Name => Name_Elaborated,
1192 Prefix =>
1193 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1195 -- Prevent duplicate elaboration checks on the same call,
1196 -- which can happen if the body enclosing the call appears
1197 -- itself in a call whose elaboration check is delayed.
1199 if Nkind (N) in N_Subprogram_Call then
1200 Set_No_Elaboration_Check (N);
1201 end if;
1202 end if;
1204 -- Case of static elaboration model
1206 else
1207 -- Do not do anything if elaboration checks suppressed. Note that
1208 -- we check Ent here, not E, since we want the real entity for the
1209 -- body to see if checks are suppressed for it, not the dummy
1210 -- entry for renamings or derivations.
1212 if Elaboration_Checks_Suppressed (Ent)
1213 or else Elaboration_Checks_Suppressed (E_Scope)
1214 or else Elaboration_Checks_Suppressed (W_Scope)
1215 then
1216 null;
1218 -- Do not generate an Elaborate_All for finalization routines
1219 -- that perform partial clean up as part of initialization.
1221 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1222 null;
1224 -- Here we need to generate an implicit elaborate all
1226 else
1227 -- Generate Elaborate_All warning unless suppressed
1229 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1230 and then not Suppress_Elaboration_Warnings (Ent)
1231 and then not Suppress_Elaboration_Warnings (E_Scope)
1232 and then not Suppress_Elaboration_Warnings (W_Scope)
1233 then
1234 Error_Msg_Node_2 := W_Scope;
1235 Error_Msg_NE
1236 ("info: call to& in elaboration code requires pragma "
1237 & "Elaborate_All on&?$?", N, E);
1238 end if;
1240 -- Set indication for binder to generate Elaborate_All
1242 Set_Elaboration_Constraint (N, E, W_Scope);
1243 end if;
1244 end if;
1245 end Check_A_Call;
1247 -----------------------------
1248 -- Check_Bad_Instantiation --
1249 -----------------------------
1251 procedure Check_Bad_Instantiation (N : Node_Id) is
1252 Ent : Entity_Id;
1254 begin
1255 -- Nothing to do if we do not have an instantiation (happens in some
1256 -- error cases, and also in the formal package declaration case)
1258 if Nkind (N) not in N_Generic_Instantiation then
1259 return;
1261 -- Nothing to do if serious errors detected (avoid cascaded errors)
1263 elsif Serious_Errors_Detected /= 0 then
1264 return;
1266 -- Nothing to do if not in full analysis mode
1268 elsif not Full_Analysis then
1269 return;
1271 -- Nothing to do if inside a generic template
1273 elsif Inside_A_Generic then
1274 return;
1276 -- Nothing to do if a library level instantiation
1278 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1279 return;
1281 -- Nothing to do if we are compiling a proper body for semantic
1282 -- purposes only. The generic body may be in another proper body.
1284 elsif
1285 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1286 then
1287 return;
1288 end if;
1290 Ent := Get_Generic_Entity (N);
1292 -- The case we are interested in is when the generic spec is in the
1293 -- current declarative part
1295 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1296 or else not In_Same_Extended_Unit (N, Ent)
1297 then
1298 return;
1299 end if;
1301 -- If the generic entity is within a deeper instance than we are, then
1302 -- either the instantiation to which we refer itself caused an ABE, in
1303 -- which case that will be handled separately. Otherwise, we know that
1304 -- the body we need appears as needed at the point of the instantiation.
1305 -- If they are both at the same level but not within the same instance
1306 -- then the body of the generic will be in the earlier instance.
1308 declare
1309 D1 : constant Nat := Instantiation_Depth (Sloc (Ent));
1310 D2 : constant Nat := Instantiation_Depth (Sloc (N));
1312 begin
1313 if D1 > D2 then
1314 return;
1316 elsif D1 = D2
1317 and then Is_Generic_Instance (Scope (Ent))
1318 and then not In_Open_Scopes (Scope (Ent))
1319 then
1320 return;
1321 end if;
1322 end;
1324 -- Now we can proceed, if the entity being called has a completion,
1325 -- then we are definitely OK, since we have already seen the body.
1327 if Has_Completion (Ent) then
1328 return;
1329 end if;
1331 -- If there is no body, then nothing to do
1333 if not Has_Generic_Body (N) then
1334 return;
1335 end if;
1337 -- Here we definitely have a bad instantiation
1339 Error_Msg_Warn := SPARK_Mode /= On;
1340 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1342 if Present (Instance_Spec (N)) then
1343 Supply_Bodies (Instance_Spec (N));
1344 end if;
1346 Error_Msg_N ("\Program_Error [<<", N);
1347 Insert_Elab_Check (N);
1348 Set_ABE_Is_Certain (N);
1349 end Check_Bad_Instantiation;
1351 ---------------------
1352 -- Check_Elab_Call --
1353 ---------------------
1355 procedure Check_Elab_Call
1356 (N : Node_Id;
1357 Outer_Scope : Entity_Id := Empty;
1358 In_Init_Proc : Boolean := False)
1360 Ent : Entity_Id;
1361 P : Node_Id;
1363 begin
1364 -- If the reference is not in the main unit, there is nothing to check.
1365 -- Elaboration call from units in the context of the main unit will lead
1366 -- to semantic dependencies when those units are compiled.
1368 if not In_Extended_Main_Code_Unit (N) then
1369 return;
1370 end if;
1372 -- For an entry call, check relevant restriction
1374 if Nkind (N) = N_Entry_Call_Statement
1375 and then not In_Subprogram_Or_Concurrent_Unit
1376 then
1377 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1379 -- Nothing to do if this is not an expected type of reference (happens
1380 -- in some error conditions, and in some cases where rewriting occurs).
1382 elsif Nkind (N) not in N_Subprogram_Call
1383 and then Nkind (N) /= N_Attribute_Reference
1384 and then (SPARK_Mode /= On
1385 or else Nkind (N) not in N_Has_Entity
1386 or else No (Entity (N))
1387 or else Ekind (Entity (N)) /= E_Variable)
1388 then
1389 return;
1391 -- Nothing to do if this is a call already rewritten for elab checking.
1392 -- Such calls appear as the targets of If_Expressions.
1394 -- This check MUST be wrong, it catches far too much
1396 elsif Nkind (Parent (N)) = N_If_Expression then
1397 return;
1399 -- Nothing to do if inside a generic template
1401 elsif Inside_A_Generic
1402 and then No (Enclosing_Generic_Body (N))
1403 then
1404 return;
1406 -- Nothing to do if call is being pre-analyzed, as when within a
1407 -- pre/postcondition, a predicate, or an invariant.
1409 elsif In_Spec_Expression then
1410 return;
1411 end if;
1413 -- Nothing to do if this is a call to a postcondition, which is always
1414 -- within a subprogram body, even though the current scope may be the
1415 -- enclosing scope of the subprogram.
1417 if Nkind (N) = N_Procedure_Call_Statement
1418 and then Is_Entity_Name (Name (N))
1419 and then Chars (Entity (Name (N))) = Name_uPostconditions
1420 then
1421 return;
1422 end if;
1424 -- Here we have a reference at elaboration time that must be checked
1426 if Debug_Flag_LL then
1427 Write_Str (" Check_Elab_Ref: ");
1429 if Nkind (N) = N_Attribute_Reference then
1430 if not Is_Entity_Name (Prefix (N)) then
1431 Write_Str ("<<not entity name>>");
1432 else
1433 Write_Name (Chars (Entity (Prefix (N))));
1434 end if;
1436 Write_Str ("'Access");
1438 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1439 Write_Str ("<<not entity name>> ");
1441 else
1442 Write_Name (Chars (Entity (Name (N))));
1443 end if;
1445 Write_Str (" reference at ");
1446 Write_Location (Sloc (N));
1447 Write_Eol;
1448 end if;
1450 -- Climb up the tree to make sure we are not inside default expression
1451 -- of a parameter specification or a record component, since in both
1452 -- these cases, we will be doing the actual reference later, not now,
1453 -- and it is at the time of the actual reference (statically speaking)
1454 -- that we must do our static check, not at the time of its initial
1455 -- analysis).
1457 -- However, we have to check references within component definitions
1458 -- (e.g. a function call that determines an array component bound),
1459 -- so we terminate the loop in that case.
1461 P := Parent (N);
1462 while Present (P) loop
1463 if Nkind_In (P, N_Parameter_Specification,
1464 N_Component_Declaration)
1465 then
1466 return;
1468 -- The reference occurs within the constraint of a component,
1469 -- so it must be checked.
1471 elsif Nkind (P) = N_Component_Definition then
1472 exit;
1474 else
1475 P := Parent (P);
1476 end if;
1477 end loop;
1479 -- Stuff that happens only at the outer level
1481 if No (Outer_Scope) then
1482 Elab_Visited.Set_Last (0);
1484 -- Nothing to do if current scope is Standard (this is a bit odd, but
1485 -- it happens in the case of generic instantiations).
1487 C_Scope := Current_Scope;
1489 if C_Scope = Standard_Standard then
1490 return;
1491 end if;
1493 -- First case, we are in elaboration code
1495 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1497 if From_Elab_Code then
1499 -- Complain if ref that comes from source in preelaborated unit
1500 -- and we are not inside a subprogram (i.e. we are in elab code).
1502 if Comes_From_Source (N)
1503 and then In_Preelaborated_Unit
1504 and then not In_Inlined_Body
1505 and then Nkind (N) /= N_Attribute_Reference
1506 then
1507 -- This is a warning in GNAT mode allowing such calls to be
1508 -- used in the predefined library with appropriate care.
1510 Error_Msg_Warn := GNAT_Mode;
1511 Error_Msg_N
1512 ("<<non-static call not allowed in preelaborated unit", N);
1513 return;
1514 end if;
1516 -- Second case, we are inside a subprogram or concurrent unit, which
1517 -- means we are not in elaboration code.
1519 else
1520 -- In this case, the issue is whether we are inside the
1521 -- declarative part of the unit in which we live, or inside its
1522 -- statements. In the latter case, there is no issue of ABE calls
1523 -- at this level (a call from outside to the unit in which we live
1524 -- might cause an ABE, but that will be detected when we analyze
1525 -- that outer level call, as it recurses into the called unit).
1527 -- Climb up the tree, doing this test, and also testing for being
1528 -- inside a default expression, which, as discussed above, is not
1529 -- checked at this stage.
1531 declare
1532 P : Node_Id;
1533 L : List_Id;
1535 begin
1536 P := N;
1537 loop
1538 -- If we find a parentless subtree, it seems safe to assume
1539 -- that we are not in a declarative part and that no
1540 -- checking is required.
1542 if No (P) then
1543 return;
1544 end if;
1546 if Is_List_Member (P) then
1547 L := List_Containing (P);
1548 P := Parent (L);
1549 else
1550 L := No_List;
1551 P := Parent (P);
1552 end if;
1554 exit when Nkind (P) = N_Subunit;
1556 -- Filter out case of default expressions, where we do not
1557 -- do the check at this stage.
1559 if Nkind_In (P, N_Parameter_Specification,
1560 N_Component_Declaration)
1561 then
1562 return;
1563 end if;
1565 -- A protected body has no elaboration code and contains
1566 -- only other bodies.
1568 if Nkind (P) = N_Protected_Body then
1569 return;
1571 elsif Nkind_In (P, N_Subprogram_Body,
1572 N_Task_Body,
1573 N_Block_Statement,
1574 N_Entry_Body)
1575 then
1576 if L = Declarations (P) then
1577 exit;
1579 -- We are not in elaboration code, but we are doing
1580 -- dynamic elaboration checks, in this case, we still
1581 -- need to do the reference, since the subprogram we are
1582 -- in could be called from another unit, also in dynamic
1583 -- elaboration check mode, at elaboration time.
1585 elsif Dynamic_Elaboration_Checks then
1587 -- We provide a debug flag to disable this check. That
1588 -- way we have an easy work around for regressions
1589 -- that are caused by this new check. This debug flag
1590 -- can be removed later.
1592 if Debug_Flag_DD then
1593 return;
1594 end if;
1596 -- Do the check in this case
1598 exit;
1600 elsif Nkind (P) = N_Task_Body then
1602 -- The check is deferred until Check_Task_Activation
1603 -- but we need to capture local suppress pragmas
1604 -- that may inhibit checks on this call.
1606 Ent := Get_Referenced_Ent (N);
1608 if No (Ent) then
1609 return;
1611 elsif Elaboration_Checks_Suppressed (Current_Scope)
1612 or else Elaboration_Checks_Suppressed (Ent)
1613 or else Elaboration_Checks_Suppressed (Scope (Ent))
1614 then
1615 if Nkind (N) in N_Subprogram_Call then
1616 Set_No_Elaboration_Check (N);
1617 end if;
1618 end if;
1620 return;
1622 -- Static model, call is not in elaboration code, we
1623 -- never need to worry, because in the static model the
1624 -- top-level caller always takes care of things.
1626 else
1627 return;
1628 end if;
1629 end if;
1630 end loop;
1631 end;
1632 end if;
1633 end if;
1635 Ent := Get_Referenced_Ent (N);
1637 if No (Ent) then
1638 return;
1639 end if;
1641 -- Determine whether a prior call to the same subprogram was already
1642 -- examined within the same context. If this is the case, then there is
1643 -- no need to proceed with the various warnings and checks because the
1644 -- work was already done for the previous call.
1646 declare
1647 Self : constant Visited_Element :=
1648 (Subp_Id => Ent, Context => Parent (N));
1650 begin
1651 for Index in 1 .. Elab_Visited.Last loop
1652 if Self = Elab_Visited.Table (Index) then
1653 return;
1654 end if;
1655 end loop;
1656 end;
1658 -- See if we need to analyze this reference. We analyze it if either of
1659 -- the following conditions is met:
1661 -- It is an inner level call (since in this case it was triggered
1662 -- by an outer level call from elaboration code), but only if the
1663 -- call is within the scope of the original outer level call.
1665 -- It is an outer level reference from elaboration code, or a call to
1666 -- an entity is in the same elaboration scope.
1668 -- And in these cases, we will check both inter-unit calls and
1669 -- intra-unit (within a single unit) calls.
1671 C_Scope := Current_Scope;
1673 -- If not outer level reference, then we follow it if it is within the
1674 -- original scope of the outer reference.
1676 if Present (Outer_Scope)
1677 and then Within (Scope (Ent), Outer_Scope)
1678 then
1679 Set_C_Scope;
1680 Check_A_Call
1681 (N => N,
1682 E => Ent,
1683 Outer_Scope => Outer_Scope,
1684 Inter_Unit_Only => False,
1685 In_Init_Proc => In_Init_Proc);
1687 -- Nothing to do if elaboration checks suppressed for this scope.
1688 -- However, an interesting exception, the fact that elaboration checks
1689 -- are suppressed within an instance (because we can trace the body when
1690 -- we process the template) does not extend to calls to generic formal
1691 -- subprograms.
1693 elsif Elaboration_Checks_Suppressed (Current_Scope)
1694 and then not Is_Call_Of_Generic_Formal (N)
1695 then
1696 null;
1698 elsif From_Elab_Code then
1699 Set_C_Scope;
1700 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1702 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1703 Set_C_Scope;
1704 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1706 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1707 -- is set, then we will do the check, but only in the inter-unit case
1708 -- (this is to accommodate unguarded elaboration calls from other units
1709 -- in which this same mode is set). We don't want warnings in this case,
1710 -- it would generate warnings having nothing to do with elaboration.
1712 elsif Dynamic_Elaboration_Checks then
1713 Set_C_Scope;
1714 Check_A_Call
1716 Ent,
1717 Standard_Standard,
1718 Inter_Unit_Only => True,
1719 Generate_Warnings => False);
1721 -- Otherwise nothing to do
1723 else
1724 return;
1725 end if;
1727 -- A call to an Init_Proc in elaboration code may bring additional
1728 -- dependencies, if some of the record components thereof have
1729 -- initializations that are function calls that come from source. We
1730 -- treat the current node as a call to each of these functions, to check
1731 -- their elaboration impact.
1733 if Is_Init_Proc (Ent) and then From_Elab_Code then
1734 Process_Init_Proc : declare
1735 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1737 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1738 -- Find subprogram calls within body of Init_Proc for Traverse
1739 -- instantiation below.
1741 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1742 -- Traversal procedure to find all calls with body of Init_Proc
1744 ---------------------
1745 -- Check_Init_Call --
1746 ---------------------
1748 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1749 Func : Entity_Id;
1751 begin
1752 if Nkind (Nod) in N_Subprogram_Call
1753 and then Is_Entity_Name (Name (Nod))
1754 then
1755 Func := Entity (Name (Nod));
1757 if Comes_From_Source (Func) then
1758 Check_A_Call
1759 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1760 end if;
1762 return OK;
1764 else
1765 return OK;
1766 end if;
1767 end Check_Init_Call;
1769 -- Start of processing for Process_Init_Proc
1771 begin
1772 if Nkind (Unit_Decl) = N_Subprogram_Body then
1773 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1774 end if;
1775 end Process_Init_Proc;
1776 end if;
1777 end Check_Elab_Call;
1779 -----------------------
1780 -- Check_Elab_Assign --
1781 -----------------------
1783 procedure Check_Elab_Assign (N : Node_Id) is
1784 Ent : Entity_Id;
1785 Scop : Entity_Id;
1787 Pkg_Spec : Entity_Id;
1788 Pkg_Body : Entity_Id;
1790 begin
1791 -- For record or array component, check prefix. If it is an access type,
1792 -- then there is nothing to do (we do not know what is being assigned),
1793 -- but otherwise this is an assignment to the prefix.
1795 if Nkind_In (N, N_Indexed_Component,
1796 N_Selected_Component,
1797 N_Slice)
1798 then
1799 if not Is_Access_Type (Etype (Prefix (N))) then
1800 Check_Elab_Assign (Prefix (N));
1801 end if;
1803 return;
1804 end if;
1806 -- For type conversion, check expression
1808 if Nkind (N) = N_Type_Conversion then
1809 Check_Elab_Assign (Expression (N));
1810 return;
1811 end if;
1813 -- Nothing to do if this is not an entity reference otherwise get entity
1815 if Is_Entity_Name (N) then
1816 Ent := Entity (N);
1817 else
1818 return;
1819 end if;
1821 -- What we are looking for is a reference in the body of a package that
1822 -- modifies a variable declared in the visible part of the package spec.
1824 if Present (Ent)
1825 and then Comes_From_Source (N)
1826 and then not Suppress_Elaboration_Warnings (Ent)
1827 and then Ekind (Ent) = E_Variable
1828 and then not In_Private_Part (Ent)
1829 and then Is_Library_Level_Entity (Ent)
1830 then
1831 Scop := Current_Scope;
1832 loop
1833 if No (Scop) or else Scop = Standard_Standard then
1834 return;
1835 elsif Ekind (Scop) = E_Package
1836 and then Is_Compilation_Unit (Scop)
1837 then
1838 exit;
1839 else
1840 Scop := Scope (Scop);
1841 end if;
1842 end loop;
1844 -- Here Scop points to the containing library package
1846 Pkg_Spec := Scop;
1847 Pkg_Body := Body_Entity (Pkg_Spec);
1849 -- All OK if the package has an Elaborate_Body pragma
1851 if Has_Pragma_Elaborate_Body (Scop) then
1852 return;
1853 end if;
1855 -- OK if entity being modified is not in containing package spec
1857 if not In_Same_Source_Unit (Scop, Ent) then
1858 return;
1859 end if;
1861 -- All OK if entity appears in generic package or generic instance.
1862 -- We just get too messed up trying to give proper warnings in the
1863 -- presence of generics. Better no message than a junk one.
1865 Scop := Scope (Ent);
1866 while Present (Scop) and then Scop /= Pkg_Spec loop
1867 if Ekind (Scop) = E_Generic_Package then
1868 return;
1869 elsif Ekind (Scop) = E_Package
1870 and then Is_Generic_Instance (Scop)
1871 then
1872 return;
1873 end if;
1875 Scop := Scope (Scop);
1876 end loop;
1878 -- All OK if in task, don't issue warnings there
1880 if In_Task_Activation then
1881 return;
1882 end if;
1884 -- OK if no package body
1886 if No (Pkg_Body) then
1887 return;
1888 end if;
1890 -- OK if reference is not in package body
1892 if not In_Same_Source_Unit (Pkg_Body, N) then
1893 return;
1894 end if;
1896 -- OK if package body has no handled statement sequence
1898 declare
1899 HSS : constant Node_Id :=
1900 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1901 begin
1902 if No (HSS) or else not Comes_From_Source (HSS) then
1903 return;
1904 end if;
1905 end;
1907 -- We definitely have a case of a modification of an entity in
1908 -- the package spec from the elaboration code of the package body.
1909 -- We may not give the warning (because there are some additional
1910 -- checks to avoid too many false positives), but it would be a good
1911 -- idea for the binder to try to keep the body elaboration close to
1912 -- the spec elaboration.
1914 Set_Elaborate_Body_Desirable (Pkg_Spec);
1916 -- All OK in gnat mode (we know what we are doing)
1918 if GNAT_Mode then
1919 return;
1920 end if;
1922 -- All OK if all warnings suppressed
1924 if Warning_Mode = Suppress then
1925 return;
1926 end if;
1928 -- All OK if elaboration checks suppressed for entity
1930 if Checks_May_Be_Suppressed (Ent)
1931 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1932 then
1933 return;
1934 end if;
1936 -- OK if the entity is initialized. Note that the No_Initialization
1937 -- flag usually means that the initialization has been rewritten into
1938 -- assignments, but that still counts for us.
1940 declare
1941 Decl : constant Node_Id := Declaration_Node (Ent);
1942 begin
1943 if Nkind (Decl) = N_Object_Declaration
1944 and then (Present (Expression (Decl))
1945 or else No_Initialization (Decl))
1946 then
1947 return;
1948 end if;
1949 end;
1951 -- Here is where we give the warning
1953 -- All OK if warnings suppressed on the entity
1955 if not Has_Warnings_Off (Ent) then
1956 Error_Msg_Sloc := Sloc (Ent);
1958 Error_Msg_NE
1959 ("??& can be accessed by clients before this initialization",
1960 N, Ent);
1961 Error_Msg_NE
1962 ("\??add Elaborate_Body to spec to ensure & is initialized",
1963 N, Ent);
1964 end if;
1966 if not All_Errors_Mode then
1967 Set_Suppress_Elaboration_Warnings (Ent);
1968 end if;
1969 end if;
1970 end Check_Elab_Assign;
1972 ----------------------
1973 -- Check_Elab_Calls --
1974 ----------------------
1976 -- WARNING: This routine manages SPARK regions
1978 procedure Check_Elab_Calls is
1979 Saved_SM : SPARK_Mode_Type;
1980 Saved_SMP : Node_Id;
1982 begin
1983 -- If expansion is disabled, do not generate any checks, unless we
1984 -- are in GNATprove mode, so that errors are issued in GNATprove for
1985 -- violations of static elaboration rules in SPARK code. Also skip
1986 -- checks if any subunits are missing because in either case we lack the
1987 -- full information that we need, and no object file will be created in
1988 -- any case.
1990 if (not Expander_Active and not GNATprove_Mode)
1991 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1992 or else Subunits_Missing
1993 then
1994 return;
1995 end if;
1997 -- Skip delayed calls if we had any errors
1999 if Serious_Errors_Detected = 0 then
2000 Delaying_Elab_Checks := False;
2001 Expander_Mode_Save_And_Set (True);
2003 for J in Delay_Check.First .. Delay_Check.Last loop
2004 Push_Scope (Delay_Check.Table (J).Curscop);
2005 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
2006 In_Task_Activation := Delay_Check.Table (J).In_Task_Activation;
2008 Saved_SM := SPARK_Mode;
2009 Saved_SMP := SPARK_Mode_Pragma;
2011 -- Set appropriate value of SPARK_Mode
2013 if Delay_Check.Table (J).From_SPARK_Code then
2014 SPARK_Mode := On;
2015 end if;
2017 Check_Internal_Call_Continue
2018 (N => Delay_Check.Table (J).N,
2019 E => Delay_Check.Table (J).E,
2020 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
2021 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
2023 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
2024 Pop_Scope;
2025 end loop;
2027 -- Set Delaying_Elab_Checks back on for next main compilation
2029 Expander_Mode_Restore;
2030 Delaying_Elab_Checks := True;
2031 end if;
2032 end Check_Elab_Calls;
2034 ------------------------------
2035 -- Check_Elab_Instantiation --
2036 ------------------------------
2038 procedure Check_Elab_Instantiation
2039 (N : Node_Id;
2040 Outer_Scope : Entity_Id := Empty)
2042 Ent : Entity_Id;
2044 begin
2045 -- Check for and deal with bad instantiation case. There is some
2046 -- duplicated code here, but we will worry about this later ???
2048 Check_Bad_Instantiation (N);
2050 if ABE_Is_Certain (N) then
2051 return;
2052 end if;
2054 -- Nothing to do if we do not have an instantiation (happens in some
2055 -- error cases, and also in the formal package declaration case)
2057 if Nkind (N) not in N_Generic_Instantiation then
2058 return;
2059 end if;
2061 -- Nothing to do if inside a generic template
2063 if Inside_A_Generic then
2064 return;
2065 end if;
2067 -- Nothing to do if the instantiation is not in the main unit
2069 if not In_Extended_Main_Code_Unit (N) then
2070 return;
2071 end if;
2073 Ent := Get_Generic_Entity (N);
2074 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
2076 -- See if we need to analyze this instantiation. We analyze it if
2077 -- either of the following conditions is met:
2079 -- It is an inner level instantiation (since in this case it was
2080 -- triggered by an outer level call from elaboration code), but
2081 -- only if the instantiation is within the scope of the original
2082 -- outer level call.
2084 -- It is an outer level instantiation from elaboration code, or the
2085 -- instantiated entity is in the same elaboration scope.
2087 -- And in these cases, we will check both the inter-unit case and
2088 -- the intra-unit (within a single unit) case.
2090 C_Scope := Current_Scope;
2092 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
2093 Set_C_Scope;
2094 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
2096 elsif From_Elab_Code then
2097 Set_C_Scope;
2098 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
2100 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
2101 Set_C_Scope;
2102 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
2104 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2105 -- set, then we will do the check, but only in the inter-unit case (this
2106 -- is to accommodate unguarded elaboration calls from other units in
2107 -- which this same mode is set). We inhibit warnings in this case, since
2108 -- this instantiation is not occurring in elaboration code.
2110 elsif Dynamic_Elaboration_Checks then
2111 Set_C_Scope;
2112 Check_A_Call
2114 Ent,
2115 Standard_Standard,
2116 Inter_Unit_Only => True,
2117 Generate_Warnings => False);
2119 else
2120 return;
2121 end if;
2122 end Check_Elab_Instantiation;
2124 -------------------------
2125 -- Check_Internal_Call --
2126 -------------------------
2128 procedure Check_Internal_Call
2129 (N : Node_Id;
2130 E : Entity_Id;
2131 Outer_Scope : Entity_Id;
2132 Orig_Ent : Entity_Id)
2134 function Within_Initial_Condition (Call : Node_Id) return Boolean;
2135 -- Determine whether call Call occurs within pragma Initial_Condition or
2136 -- pragma Check with check_kind set to Initial_Condition.
2138 ------------------------------
2139 -- Within_Initial_Condition --
2140 ------------------------------
2142 function Within_Initial_Condition (Call : Node_Id) return Boolean is
2143 Args : List_Id;
2144 Nam : Name_Id;
2145 Par : Node_Id;
2147 begin
2148 -- Traverse the parent chain looking for an enclosing pragma
2150 Par := Call;
2151 while Present (Par) loop
2152 if Nkind (Par) = N_Pragma then
2153 Nam := Pragma_Name (Par);
2155 -- Pragma Initial_Condition appears in its alternative from as
2156 -- Check (Initial_Condition, ...).
2158 if Nam = Name_Check then
2159 Args := Pragma_Argument_Associations (Par);
2161 -- Pragma Check should have at least two arguments
2163 pragma Assert (Present (Args));
2165 return
2166 Chars (Expression (First (Args))) = Name_Initial_Condition;
2168 -- Direct match
2170 elsif Nam = Name_Initial_Condition then
2171 return True;
2173 -- Since pragmas are never nested within other pragmas, stop
2174 -- the traversal.
2176 else
2177 return False;
2178 end if;
2180 -- Prevent the search from going too far
2182 elsif Is_Body_Or_Package_Declaration (Par) then
2183 exit;
2184 end if;
2186 Par := Parent (Par);
2188 -- If assertions are not enabled, the check pragma is rewritten
2189 -- as an if_statement in sem_prag, to generate various warnings
2190 -- on boolean expressions. Retrieve the original pragma.
2192 if Nkind (Original_Node (Par)) = N_Pragma then
2193 Par := Original_Node (Par);
2194 end if;
2195 end loop;
2197 return False;
2198 end Within_Initial_Condition;
2200 -- Local variables
2202 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
2204 -- Start of processing for Check_Internal_Call
2206 begin
2207 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2208 -- node comes from source.
2210 if Nkind (N) = N_Attribute_Reference
2211 and then ((not Warn_On_Elab_Access and then not Debug_Flag_Dot_O)
2212 or else not Comes_From_Source (N))
2213 then
2214 return;
2216 -- If not function or procedure call, instantiation, or 'Access, then
2217 -- ignore call (this happens in some error cases and rewriting cases).
2219 elsif not Nkind_In (N, N_Attribute_Reference,
2220 N_Function_Call,
2221 N_Procedure_Call_Statement)
2222 and then not Inst_Case
2223 then
2224 return;
2226 -- Nothing to do if this is a call or instantiation that has already
2227 -- been found to be a sure ABE.
2229 elsif Nkind (N) /= N_Attribute_Reference and then ABE_Is_Certain (N) then
2230 return;
2232 -- Nothing to do if errors already detected (avoid cascaded errors)
2234 elsif Serious_Errors_Detected /= 0 then
2235 return;
2237 -- Nothing to do if not in full analysis mode
2239 elsif not Full_Analysis then
2240 return;
2242 -- Nothing to do if analyzing in special spec-expression mode, since the
2243 -- call is not actually being made at this time.
2245 elsif In_Spec_Expression then
2246 return;
2248 -- Nothing to do for call to intrinsic subprogram
2250 elsif Is_Intrinsic_Subprogram (E) then
2251 return;
2253 -- Nothing to do if call is within a generic unit
2255 elsif Inside_A_Generic then
2256 return;
2258 -- Nothing to do when the call appears within pragma Initial_Condition.
2259 -- The pragma is part of the elaboration statements of a package body
2260 -- and may only call external subprograms or subprograms whose body is
2261 -- already available.
2263 elsif Within_Initial_Condition (N) then
2264 return;
2265 end if;
2267 -- Delay this call if we are still delaying calls
2269 if Delaying_Elab_Checks then
2270 Delay_Check.Append
2271 ((N => N,
2272 E => E,
2273 Orig_Ent => Orig_Ent,
2274 Curscop => Current_Scope,
2275 Outer_Scope => Outer_Scope,
2276 From_Elab_Code => From_Elab_Code,
2277 In_Task_Activation => In_Task_Activation,
2278 From_SPARK_Code => SPARK_Mode = On));
2279 return;
2281 -- Otherwise, call phase 2 continuation right now
2283 else
2284 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
2285 end if;
2286 end Check_Internal_Call;
2288 ----------------------------------
2289 -- Check_Internal_Call_Continue --
2290 ----------------------------------
2292 procedure Check_Internal_Call_Continue
2293 (N : Node_Id;
2294 E : Entity_Id;
2295 Outer_Scope : Entity_Id;
2296 Orig_Ent : Entity_Id)
2298 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
2299 -- Function applied to each node as we traverse the body. Checks for
2300 -- call or entity reference that needs checking, and if so checks it.
2301 -- Always returns OK, so entire tree is traversed, except that as
2302 -- described below subprogram bodies are skipped for now.
2304 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2305 -- Traverse procedure using above Find_Elab_Reference function
2307 -------------------------
2308 -- Find_Elab_Reference --
2309 -------------------------
2311 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2312 Actual : Node_Id;
2314 begin
2315 -- If user has specified that there are no entry calls in elaboration
2316 -- code, do not trace past an accept statement, because the rendez-
2317 -- vous will happen after elaboration.
2319 if Nkind_In (Original_Node (N), N_Accept_Statement,
2320 N_Selective_Accept)
2321 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2322 then
2323 return Abandon;
2325 -- If we have a function call, check it
2327 elsif Nkind (N) = N_Function_Call then
2328 Check_Elab_Call (N, Outer_Scope);
2329 return OK;
2331 -- If we have a procedure call, check the call, and also check
2332 -- arguments that are assignments (OUT or IN OUT mode formals).
2334 elsif Nkind (N) = N_Procedure_Call_Statement then
2335 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2337 Actual := First_Actual (N);
2338 while Present (Actual) loop
2339 if Known_To_Be_Assigned (Actual) then
2340 Check_Elab_Assign (Actual);
2341 end if;
2343 Next_Actual (Actual);
2344 end loop;
2346 return OK;
2348 -- If we have an access attribute for a subprogram, check it.
2349 -- Suppress this behavior under debug flag.
2351 elsif not Debug_Flag_Dot_UU
2352 and then Nkind (N) = N_Attribute_Reference
2353 and then Nam_In (Attribute_Name (N), Name_Access,
2354 Name_Unrestricted_Access)
2355 and then Is_Entity_Name (Prefix (N))
2356 and then Is_Subprogram (Entity (Prefix (N)))
2357 then
2358 Check_Elab_Call (N, Outer_Scope);
2359 return OK;
2361 -- In SPARK mode, if we have an entity reference to a variable, then
2362 -- check it. For now we consider any reference.
2364 elsif SPARK_Mode = On
2365 and then Nkind (N) in N_Has_Entity
2366 and then Present (Entity (N))
2367 and then Ekind (Entity (N)) = E_Variable
2368 then
2369 Check_Elab_Call (N, Outer_Scope);
2370 return OK;
2372 -- If we have a generic instantiation, check it
2374 elsif Nkind (N) in N_Generic_Instantiation then
2375 Check_Elab_Instantiation (N, Outer_Scope);
2376 return OK;
2378 -- Skip subprogram bodies that come from source (wait for call to
2379 -- analyze these). The reason for the come from source test is to
2380 -- avoid catching task bodies.
2382 -- For task bodies, we should really avoid these too, waiting for the
2383 -- task activation, but that's too much trouble to catch for now, so
2384 -- we go in unconditionally. This is not so terrible, it means the
2385 -- error backtrace is not quite complete, and we are too eager to
2386 -- scan bodies of tasks that are unused, but this is hardly very
2387 -- significant.
2389 elsif Nkind (N) = N_Subprogram_Body
2390 and then Comes_From_Source (N)
2391 then
2392 return Skip;
2394 elsif Nkind (N) = N_Assignment_Statement
2395 and then Comes_From_Source (N)
2396 then
2397 Check_Elab_Assign (Name (N));
2398 return OK;
2400 else
2401 return OK;
2402 end if;
2403 end Find_Elab_Reference;
2405 Inst_Case : constant Boolean := Is_Generic_Unit (E);
2406 Loc : constant Source_Ptr := Sloc (N);
2408 Ebody : Entity_Id;
2409 Sbody : Node_Id;
2411 -- Start of processing for Check_Internal_Call_Continue
2413 begin
2414 -- Save outer level call if at outer level
2416 if Elab_Call.Last = 0 then
2417 Outer_Level_Sloc := Loc;
2418 end if;
2420 -- If the call is to a function that renames a literal, no check needed
2422 if Ekind (E) = E_Enumeration_Literal then
2423 return;
2424 end if;
2426 -- Register the subprogram as examined within this particular context.
2427 -- This ensures that calls to the same subprogram but in different
2428 -- contexts receive warnings and checks of their own since the calls
2429 -- may be reached through different flow paths.
2431 Elab_Visited.Append ((Subp_Id => E, Context => Parent (N)));
2433 Sbody := Unit_Declaration_Node (E);
2435 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2436 Ebody := Corresponding_Body (Sbody);
2438 if No (Ebody) then
2439 return;
2440 else
2441 Sbody := Unit_Declaration_Node (Ebody);
2442 end if;
2443 end if;
2445 -- If the body appears after the outer level call or instantiation then
2446 -- we have an error case handled below.
2448 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2449 and then not In_Task_Activation
2450 then
2451 null;
2453 -- If we have the instantiation case we are done, since we now know that
2454 -- the body of the generic appeared earlier.
2456 elsif Inst_Case then
2457 return;
2459 -- Otherwise we have a call, so we trace through the called body to see
2460 -- if it has any problems.
2462 else
2463 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2465 Elab_Call.Append ((Cloc => Loc, Ent => E));
2467 if Debug_Flag_LL then
2468 Write_Str ("Elab_Call.Last = ");
2469 Write_Int (Int (Elab_Call.Last));
2470 Write_Str (" Ent = ");
2471 Write_Name (Chars (E));
2472 Write_Str (" at ");
2473 Write_Location (Sloc (N));
2474 Write_Eol;
2475 end if;
2477 -- Now traverse declarations and statements of subprogram body. Note
2478 -- that we cannot simply Traverse (Sbody), since traverse does not
2479 -- normally visit subprogram bodies.
2481 declare
2482 Decl : Node_Id;
2483 begin
2484 Decl := First (Declarations (Sbody));
2485 while Present (Decl) loop
2486 Traverse (Decl);
2487 Next (Decl);
2488 end loop;
2489 end;
2491 Traverse (Handled_Statement_Sequence (Sbody));
2493 Elab_Call.Decrement_Last;
2494 return;
2495 end if;
2497 -- Here is the case of calling a subprogram where the body has not yet
2498 -- been encountered. A warning message is needed, except if this is the
2499 -- case of appearing within an aspect specification that results in
2500 -- a check call, we do not really have such a situation, so no warning
2501 -- is needed (e.g. the case of a precondition, where the call appears
2502 -- textually before the body, but in actual fact is moved to the
2503 -- appropriate subprogram body and so does not need a check).
2505 declare
2506 P : Node_Id;
2507 O : Node_Id;
2509 begin
2510 P := Parent (N);
2511 loop
2512 -- Keep looking at parents if we are still in the subexpression
2514 if Nkind (P) in N_Subexpr then
2515 P := Parent (P);
2517 -- Here P is the parent of the expression, check for special case
2519 else
2520 O := Original_Node (P);
2522 -- Definitely not the special case if orig node is not a pragma
2524 exit when Nkind (O) /= N_Pragma;
2526 -- Check we have an If statement or a null statement (happens
2527 -- when the If has been expanded to be True).
2529 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2531 -- Our special case will be indicated either by the pragma
2532 -- coming from an aspect ...
2534 if Present (Corresponding_Aspect (O)) then
2535 return;
2537 -- Or, in the case of an initial condition, specifically by a
2538 -- Check pragma specifying an Initial_Condition check.
2540 elsif Pragma_Name (O) = Name_Check
2541 and then
2542 Chars
2543 (Expression (First (Pragma_Argument_Associations (O)))) =
2544 Name_Initial_Condition
2545 then
2546 return;
2548 -- For anything else, we have an error
2550 else
2551 exit;
2552 end if;
2553 end if;
2554 end loop;
2555 end;
2557 -- Not that special case, warning and dynamic check is required
2559 -- If we have nothing in the call stack, then this is at the outer
2560 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2561 -- it's a renaming.
2563 if Elab_Call.Last = 0 then
2564 Error_Msg_Warn := SPARK_Mode /= On;
2566 declare
2567 Insert_Check : Boolean := True;
2568 -- This flag is set to True if an elaboration check should be
2569 -- inserted.
2571 begin
2572 if In_Task_Activation then
2573 Insert_Check := False;
2575 elsif Inst_Case then
2576 Error_Msg_NE
2577 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2579 elsif Nkind (N) = N_Attribute_Reference then
2580 Error_Msg_NE
2581 ("Access attribute of & before body seen<<", N, Orig_Ent);
2582 Error_Msg_N ("\possible Program_Error on later references<", N);
2583 Insert_Check := False;
2585 elsif Nkind (Unit_Declaration_Node (Orig_Ent)) /=
2586 N_Subprogram_Renaming_Declaration
2587 then
2588 Error_Msg_NE
2589 ("cannot call& before body seen<<", N, Orig_Ent);
2591 elsif not Is_Generic_Actual_Subprogram (Orig_Ent) then
2592 Insert_Check := False;
2593 end if;
2595 if Insert_Check then
2596 Error_Msg_N ("\Program_Error [<<", N);
2597 Insert_Elab_Check (N);
2598 end if;
2599 end;
2601 -- Call is not at outer level
2603 else
2604 -- Do not generate elaboration checks in GNATprove mode because the
2605 -- elaboration counter and the check are both forms of expansion.
2607 if GNATprove_Mode then
2608 null;
2610 -- Generate an elaboration check
2612 elsif not Elaboration_Checks_Suppressed (E) then
2613 Set_Elaboration_Entity_Required (E);
2615 -- Create a declaration of the elaboration entity, and insert it
2616 -- prior to the subprogram or the generic unit, within the same
2617 -- scope. Since the subprogram may be overloaded, create a unique
2618 -- entity.
2620 if No (Elaboration_Entity (E)) then
2621 declare
2622 Loce : constant Source_Ptr := Sloc (E);
2623 Ent : constant Entity_Id :=
2624 Make_Defining_Identifier (Loc,
2625 New_External_Name (Chars (E), 'E', -1));
2627 begin
2628 Set_Elaboration_Entity (E, Ent);
2629 Push_Scope (Scope (E));
2631 Insert_Action (Declaration_Node (E),
2632 Make_Object_Declaration (Loce,
2633 Defining_Identifier => Ent,
2634 Object_Definition =>
2635 New_Occurrence_Of (Standard_Short_Integer, Loce),
2636 Expression =>
2637 Make_Integer_Literal (Loc, Uint_0)));
2639 -- Set elaboration flag at the point of the body
2641 Set_Elaboration_Flag (Sbody, E);
2643 -- Kill current value indication. This is necessary because
2644 -- the tests of this flag are inserted out of sequence and
2645 -- must not pick up bogus indications of the wrong constant
2646 -- value. Also, this is never a true constant, since one way
2647 -- or another, it gets reset.
2649 Set_Current_Value (Ent, Empty);
2650 Set_Last_Assignment (Ent, Empty);
2651 Set_Is_True_Constant (Ent, False);
2652 Pop_Scope;
2653 end;
2654 end if;
2656 -- Generate:
2657 -- if Enn = 0 then
2658 -- raise Program_Error with "access before elaboration";
2659 -- end if;
2661 Insert_Elab_Check (N,
2662 Make_Attribute_Reference (Loc,
2663 Attribute_Name => Name_Elaborated,
2664 Prefix => New_Occurrence_Of (E, Loc)));
2665 end if;
2667 -- Generate the warning
2669 if not Suppress_Elaboration_Warnings (E)
2670 and then not Elaboration_Checks_Suppressed (E)
2672 -- Suppress this warning if we have a function call that occurred
2673 -- within an assertion expression, since we can get false warnings
2674 -- in this case, due to the out of order handling in this case.
2676 and then
2677 (Nkind (Original_Node (N)) /= N_Function_Call
2678 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2679 then
2680 Error_Msg_Warn := SPARK_Mode /= On;
2682 if Inst_Case then
2683 Error_Msg_NE
2684 ("instantiation of& may occur before body is seen<l<",
2685 N, Orig_Ent);
2686 else
2687 -- A rather specific check. For Finalize/Adjust/Initialize, if
2688 -- the type has Warnings_Off set, suppress the warning.
2690 if Nam_In (Chars (E), Name_Adjust,
2691 Name_Finalize,
2692 Name_Initialize)
2693 and then Present (First_Formal (E))
2694 then
2695 declare
2696 T : constant Entity_Id := Etype (First_Formal (E));
2697 begin
2698 if Is_Controlled (T) then
2699 if Warnings_Off (T)
2700 or else (Ekind (T) = E_Private_Type
2701 and then Warnings_Off (Full_View (T)))
2702 then
2703 goto Output;
2704 end if;
2705 end if;
2706 end;
2707 end if;
2709 -- Go ahead and give warning if not this special case
2711 Error_Msg_NE
2712 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2713 end if;
2715 Error_Msg_N ("\Program_Error ]<l<", N);
2717 -- There is no need to query the elaboration warning message flags
2718 -- because the main message is an error, not a warning, therefore
2719 -- all the clarification messages produces by Output_Calls must be
2720 -- emitted unconditionally.
2722 <<Output>>
2724 Output_Calls (N, Check_Elab_Flag => False);
2725 end if;
2726 end if;
2727 end Check_Internal_Call_Continue;
2729 ---------------------------
2730 -- Check_Task_Activation --
2731 ---------------------------
2733 procedure Check_Task_Activation (N : Node_Id) is
2734 Loc : constant Source_Ptr := Sloc (N);
2735 Inter_Procs : constant Elist_Id := New_Elmt_List;
2736 Intra_Procs : constant Elist_Id := New_Elmt_List;
2737 Ent : Entity_Id;
2738 P : Entity_Id;
2739 Task_Scope : Entity_Id;
2740 Cunit_SC : Boolean := False;
2741 Decl : Node_Id;
2742 Elmt : Elmt_Id;
2743 Enclosing : Entity_Id;
2745 procedure Add_Task_Proc (Typ : Entity_Id);
2746 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2747 -- For record types, this procedure recurses over component types.
2749 procedure Collect_Tasks (Decls : List_Id);
2750 -- Collect the types of the tasks that are to be activated in the given
2751 -- list of declarations, in order to perform elaboration checks on the
2752 -- corresponding task procedures that are called implicitly here.
2754 function Outer_Unit (E : Entity_Id) return Entity_Id;
2755 -- find enclosing compilation unit of Entity, ignoring subunits, or
2756 -- else enclosing subprogram. If E is not a package, there is no need
2757 -- for inter-unit elaboration checks.
2759 -------------------
2760 -- Add_Task_Proc --
2761 -------------------
2763 procedure Add_Task_Proc (Typ : Entity_Id) is
2764 Comp : Entity_Id;
2765 Proc : Entity_Id := Empty;
2767 begin
2768 if Is_Task_Type (Typ) then
2769 Proc := Get_Task_Body_Procedure (Typ);
2771 elsif Is_Array_Type (Typ)
2772 and then Has_Task (Base_Type (Typ))
2773 then
2774 Add_Task_Proc (Component_Type (Typ));
2776 elsif Is_Record_Type (Typ)
2777 and then Has_Task (Base_Type (Typ))
2778 then
2779 Comp := First_Component (Typ);
2780 while Present (Comp) loop
2781 Add_Task_Proc (Etype (Comp));
2782 Comp := Next_Component (Comp);
2783 end loop;
2784 end if;
2786 -- If the task type is another unit, we will perform the usual
2787 -- elaboration check on its enclosing unit. If the type is in the
2788 -- same unit, we can trace the task body as for an internal call,
2789 -- but we only need to examine other external calls, because at
2790 -- the point the task is activated, internal subprogram bodies
2791 -- will have been elaborated already. We keep separate lists for
2792 -- each kind of task.
2794 -- Skip this test if errors have occurred, since in this case
2795 -- we can get false indications.
2797 if Serious_Errors_Detected /= 0 then
2798 return;
2799 end if;
2801 if Present (Proc) then
2802 if Outer_Unit (Scope (Proc)) = Enclosing then
2804 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2805 and then
2806 (not Is_Generic_Instance (Scope (Proc))
2807 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2808 then
2809 Error_Msg_Warn := SPARK_Mode /= On;
2810 Error_Msg_N
2811 ("task will be activated before elaboration of its body<<",
2812 Decl);
2813 Error_Msg_N ("\Program_Error [<<", Decl);
2815 elsif Present
2816 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2817 then
2818 Append_Elmt (Proc, Intra_Procs);
2819 end if;
2821 else
2822 -- No need for multiple entries of the same type
2824 Elmt := First_Elmt (Inter_Procs);
2825 while Present (Elmt) loop
2826 if Node (Elmt) = Proc then
2827 return;
2828 end if;
2830 Next_Elmt (Elmt);
2831 end loop;
2833 Append_Elmt (Proc, Inter_Procs);
2834 end if;
2835 end if;
2836 end Add_Task_Proc;
2838 -------------------
2839 -- Collect_Tasks --
2840 -------------------
2842 procedure Collect_Tasks (Decls : List_Id) is
2843 begin
2844 if Present (Decls) then
2845 Decl := First (Decls);
2846 while Present (Decl) loop
2847 if Nkind (Decl) = N_Object_Declaration
2848 and then Has_Task (Etype (Defining_Identifier (Decl)))
2849 then
2850 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2851 end if;
2853 Next (Decl);
2854 end loop;
2855 end if;
2856 end Collect_Tasks;
2858 ----------------
2859 -- Outer_Unit --
2860 ----------------
2862 function Outer_Unit (E : Entity_Id) return Entity_Id is
2863 Outer : Entity_Id;
2865 begin
2866 Outer := E;
2867 while Present (Outer) loop
2868 if Elaboration_Checks_Suppressed (Outer) then
2869 Cunit_SC := True;
2870 end if;
2872 exit when Is_Child_Unit (Outer)
2873 or else Scope (Outer) = Standard_Standard
2874 or else Ekind (Outer) /= E_Package;
2875 Outer := Scope (Outer);
2876 end loop;
2878 return Outer;
2879 end Outer_Unit;
2881 -- Start of processing for Check_Task_Activation
2883 begin
2884 Enclosing := Outer_Unit (Current_Scope);
2886 -- Find all tasks declared in the current unit
2888 if Nkind (N) = N_Package_Body then
2889 P := Unit_Declaration_Node (Corresponding_Spec (N));
2891 Collect_Tasks (Declarations (N));
2892 Collect_Tasks (Visible_Declarations (Specification (P)));
2893 Collect_Tasks (Private_Declarations (Specification (P)));
2895 elsif Nkind (N) = N_Package_Declaration then
2896 Collect_Tasks (Visible_Declarations (Specification (N)));
2897 Collect_Tasks (Private_Declarations (Specification (N)));
2899 else
2900 Collect_Tasks (Declarations (N));
2901 end if;
2903 -- We only perform detailed checks in all tasks that are library level
2904 -- entities. If the master is a subprogram or task, activation will
2905 -- depend on the activation of the master itself.
2907 -- Should dynamic checks be added in the more general case???
2909 if Ekind (Enclosing) /= E_Package then
2910 return;
2911 end if;
2913 -- For task types defined in other units, we want the unit containing
2914 -- the task body to be elaborated before the current one.
2916 Elmt := First_Elmt (Inter_Procs);
2917 while Present (Elmt) loop
2918 Ent := Node (Elmt);
2919 Task_Scope := Outer_Unit (Scope (Ent));
2921 if not Is_Compilation_Unit (Task_Scope) then
2922 null;
2924 elsif Suppress_Elaboration_Warnings (Task_Scope)
2925 or else Elaboration_Checks_Suppressed (Task_Scope)
2926 then
2927 null;
2929 elsif Dynamic_Elaboration_Checks then
2930 if not Elaboration_Checks_Suppressed (Ent)
2931 and then not Cunit_SC
2932 and then not Restriction_Active
2933 (No_Entry_Calls_In_Elaboration_Code)
2934 then
2935 -- Runtime elaboration check required. Generate check of the
2936 -- elaboration counter for the unit containing the entity.
2938 Insert_Elab_Check (N,
2939 Make_Attribute_Reference (Loc,
2940 Prefix =>
2941 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc),
2942 Attribute_Name => Name_Elaborated));
2943 end if;
2945 else
2946 -- Force the binder to elaborate other unit first
2948 if Elab_Info_Messages
2949 and then not Suppress_Elaboration_Warnings (Ent)
2950 and then not Elaboration_Checks_Suppressed (Ent)
2951 and then not Suppress_Elaboration_Warnings (Task_Scope)
2952 and then not Elaboration_Checks_Suppressed (Task_Scope)
2953 then
2954 Error_Msg_Node_2 := Task_Scope;
2955 Error_Msg_NE
2956 ("info: activation of an instance of task type & requires "
2957 & "pragma Elaborate_All on &?$?", N, Ent);
2958 end if;
2960 Activate_Elaborate_All_Desirable (N, Task_Scope);
2961 Set_Suppress_Elaboration_Warnings (Task_Scope);
2962 end if;
2964 Next_Elmt (Elmt);
2965 end loop;
2967 -- For tasks declared in the current unit, trace other calls within the
2968 -- task procedure bodies, which are available.
2970 if not Debug_Flag_Dot_Y then
2971 In_Task_Activation := True;
2973 Elmt := First_Elmt (Intra_Procs);
2974 while Present (Elmt) loop
2975 Ent := Node (Elmt);
2976 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2977 Next_Elmt (Elmt);
2978 end loop;
2980 In_Task_Activation := False;
2981 end if;
2982 end Check_Task_Activation;
2984 -------------------------------
2985 -- Is_Call_Of_Generic_Formal --
2986 -------------------------------
2988 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2989 begin
2990 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2992 -- Always return False if debug flag -gnatd.G is set
2994 and then not Debug_Flag_Dot_GG
2996 -- For now, we detect this by looking for the strange identifier
2997 -- node, whose Chars reflect the name of the generic formal, but
2998 -- the Chars of the Entity references the generic actual.
3000 and then Nkind (Name (N)) = N_Identifier
3001 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
3002 end Is_Call_Of_Generic_Formal;
3004 --------------------------------
3005 -- Set_Elaboration_Constraint --
3006 --------------------------------
3008 procedure Set_Elaboration_Constraint
3009 (Call : Node_Id;
3010 Subp : Entity_Id;
3011 Scop : Entity_Id)
3013 Elab_Unit : Entity_Id;
3015 -- Check whether this is a call to an Initialize subprogram for a
3016 -- controlled type. Note that Call can also be a 'Access attribute
3017 -- reference, which now generates an elaboration check.
3019 Init_Call : constant Boolean :=
3020 Nkind (Call) = N_Procedure_Call_Statement
3021 and then Chars (Subp) = Name_Initialize
3022 and then Comes_From_Source (Subp)
3023 and then Present (Parameter_Associations (Call))
3024 and then Is_Controlled (Etype (First_Actual (Call)));
3026 begin
3027 -- If the unit is mentioned in a with_clause of the current unit, it is
3028 -- visible, and we can set the elaboration flag.
3030 if Is_Immediately_Visible (Scop)
3031 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
3032 then
3033 Activate_Elaborate_All_Desirable (Call, Scop);
3034 Set_Suppress_Elaboration_Warnings (Scop);
3035 return;
3036 end if;
3038 -- If this is not an initialization call or a call using object notation
3039 -- we know that the unit of the called entity is in the context, and we
3040 -- can set the flag as well. The unit need not be visible if the call
3041 -- occurs within an instantiation.
3043 if Is_Init_Proc (Subp)
3044 or else Init_Call
3045 or else Nkind (Original_Node (Call)) = N_Selected_Component
3046 then
3047 null; -- detailed processing follows.
3049 else
3050 Activate_Elaborate_All_Desirable (Call, Scop);
3051 Set_Suppress_Elaboration_Warnings (Scop);
3052 return;
3053 end if;
3055 -- If the unit is not in the context, there must be an intermediate unit
3056 -- that is, on which we need to place to elaboration flag. This happens
3057 -- with init proc calls.
3059 if Is_Init_Proc (Subp) or else Init_Call then
3061 -- The initialization call is on an object whose type is not declared
3062 -- in the same scope as the subprogram. The type of the object must
3063 -- be a subtype of the type of operation. This object is the first
3064 -- actual in the call.
3066 declare
3067 Typ : constant Entity_Id :=
3068 Etype (First (Parameter_Associations (Call)));
3069 begin
3070 Elab_Unit := Scope (Typ);
3071 while (Present (Elab_Unit))
3072 and then not Is_Compilation_Unit (Elab_Unit)
3073 loop
3074 Elab_Unit := Scope (Elab_Unit);
3075 end loop;
3076 end;
3078 -- If original node uses selected component notation, the prefix is
3079 -- visible and determines the scope that must be elaborated. After
3080 -- rewriting, the prefix is the first actual in the call.
3082 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
3083 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
3085 -- Not one of special cases above
3087 else
3088 -- Using previously computed scope. If the elaboration check is
3089 -- done after analysis, the scope is not visible any longer, but
3090 -- must still be in the context.
3092 Elab_Unit := Scop;
3093 end if;
3095 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
3096 Set_Suppress_Elaboration_Warnings (Elab_Unit);
3097 end Set_Elaboration_Constraint;
3099 ------------------------
3100 -- Get_Referenced_Ent --
3101 ------------------------
3103 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
3104 Nam : Node_Id;
3106 begin
3107 if Nkind (N) in N_Has_Entity
3108 and then Present (Entity (N))
3109 and then Ekind (Entity (N)) = E_Variable
3110 then
3111 return Entity (N);
3112 end if;
3114 if Nkind (N) = N_Attribute_Reference then
3115 Nam := Prefix (N);
3116 else
3117 Nam := Name (N);
3118 end if;
3120 if No (Nam) then
3121 return Empty;
3122 elsif Nkind (Nam) = N_Selected_Component then
3123 return Entity (Selector_Name (Nam));
3124 elsif not Is_Entity_Name (Nam) then
3125 return Empty;
3126 else
3127 return Entity (Nam);
3128 end if;
3129 end Get_Referenced_Ent;
3131 ----------------------
3132 -- Has_Generic_Body --
3133 ----------------------
3135 function Has_Generic_Body (N : Node_Id) return Boolean is
3136 Ent : constant Entity_Id := Get_Generic_Entity (N);
3137 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
3138 Scop : Entity_Id;
3140 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
3141 -- Determine if the list of nodes headed by N and linked by Next
3142 -- contains a package body for the package spec entity E, and if so
3143 -- return the package body. If not, then returns Empty.
3145 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
3146 -- This procedure is called load the unit whose name is given by Nam.
3147 -- This unit is being loaded to see whether it contains an optional
3148 -- generic body. The returned value is the loaded unit, which is always
3149 -- a package body (only package bodies can contain other entities in the
3150 -- sense in which Has_Generic_Body is interested). We only attempt to
3151 -- load bodies if we are generating code. If we are in semantics check
3152 -- only mode, then it would be wrong to load bodies that are not
3153 -- required from a semantic point of view, so in this case we return
3154 -- Empty. The result is that the caller may incorrectly decide that a
3155 -- generic spec does not have a body when in fact it does, but the only
3156 -- harm in this is that some warnings on elaboration problems may be
3157 -- lost in semantic checks only mode, which is not big loss. We also
3158 -- return Empty if we go for a body and it is not there.
3160 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
3161 -- PE is the entity for a package spec. This function locates the
3162 -- corresponding package body, returning Empty if none is found. The
3163 -- package body returned is fully parsed but may not yet be analyzed,
3164 -- so only syntactic fields should be referenced.
3166 ------------------
3167 -- Find_Body_In --
3168 ------------------
3170 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
3171 Nod : Node_Id;
3173 begin
3174 Nod := N;
3175 while Present (Nod) loop
3177 -- If we found the package body we are looking for, return it
3179 if Nkind (Nod) = N_Package_Body
3180 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
3181 then
3182 return Nod;
3184 -- If we found the stub for the body, go after the subunit,
3185 -- loading it if necessary.
3187 elsif Nkind (Nod) = N_Package_Body_Stub
3188 and then Chars (Defining_Identifier (Nod)) = Chars (E)
3189 then
3190 if Present (Library_Unit (Nod)) then
3191 return Unit (Library_Unit (Nod));
3193 else
3194 return Load_Package_Body (Get_Unit_Name (Nod));
3195 end if;
3197 -- If neither package body nor stub, keep looking on chain
3199 else
3200 Next (Nod);
3201 end if;
3202 end loop;
3204 return Empty;
3205 end Find_Body_In;
3207 -----------------------
3208 -- Load_Package_Body --
3209 -----------------------
3211 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
3212 U : Unit_Number_Type;
3214 begin
3215 if Operating_Mode /= Generate_Code then
3216 return Empty;
3217 else
3218 U :=
3219 Load_Unit
3220 (Load_Name => Nam,
3221 Required => False,
3222 Subunit => False,
3223 Error_Node => N);
3225 if U = No_Unit then
3226 return Empty;
3227 else
3228 return Unit (Cunit (U));
3229 end if;
3230 end if;
3231 end Load_Package_Body;
3233 -------------------------------
3234 -- Locate_Corresponding_Body --
3235 -------------------------------
3237 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
3238 Spec : constant Node_Id := Declaration_Node (PE);
3239 Decl : constant Node_Id := Parent (Spec);
3240 Scop : constant Entity_Id := Scope (PE);
3241 PBody : Node_Id;
3243 begin
3244 if Is_Library_Level_Entity (PE) then
3246 -- If package is a library unit that requires a body, we have no
3247 -- choice but to go after that body because it might contain an
3248 -- optional body for the original generic package.
3250 if Unit_Requires_Body (PE) then
3252 -- Load the body. Note that we are a little careful here to use
3253 -- Spec to get the unit number, rather than PE or Decl, since
3254 -- in the case where the package is itself a library level
3255 -- instantiation, Spec will properly reference the generic
3256 -- template, which is what we really want.
3258 return
3259 Load_Package_Body
3260 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
3262 -- But if the package is a library unit that does NOT require
3263 -- a body, then no body is permitted, so we are sure that there
3264 -- is no body for the original generic package.
3266 else
3267 return Empty;
3268 end if;
3270 -- Otherwise look and see if we are embedded in a further package
3272 elsif Is_Package_Or_Generic_Package (Scop) then
3274 -- If so, get the body of the enclosing package, and look in
3275 -- its package body for the package body we are looking for.
3277 PBody := Locate_Corresponding_Body (Scop);
3279 if No (PBody) then
3280 return Empty;
3281 else
3282 return Find_Body_In (PE, First (Declarations (PBody)));
3283 end if;
3285 -- If we are not embedded in a further package, then the body
3286 -- must be in the same declarative part as we are.
3288 else
3289 return Find_Body_In (PE, Next (Decl));
3290 end if;
3291 end Locate_Corresponding_Body;
3293 -- Start of processing for Has_Generic_Body
3295 begin
3296 if Present (Corresponding_Body (Decl)) then
3297 return True;
3299 elsif Unit_Requires_Body (Ent) then
3300 return True;
3302 -- Compilation units cannot have optional bodies
3304 elsif Is_Compilation_Unit (Ent) then
3305 return False;
3307 -- Otherwise look at what scope we are in
3309 else
3310 Scop := Scope (Ent);
3312 -- Case of entity is in other than a package spec, in this case
3313 -- the body, if present, must be in the same declarative part.
3315 if not Is_Package_Or_Generic_Package (Scop) then
3316 declare
3317 P : Node_Id;
3319 begin
3320 -- Declaration node may get us a spec, so if so, go to
3321 -- the parent declaration.
3323 P := Declaration_Node (Ent);
3324 while not Is_List_Member (P) loop
3325 P := Parent (P);
3326 end loop;
3328 return Present (Find_Body_In (Ent, Next (P)));
3329 end;
3331 -- If the entity is in a package spec, then we have to locate
3332 -- the corresponding package body, and look there.
3334 else
3335 declare
3336 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
3338 begin
3339 if No (PBody) then
3340 return False;
3341 else
3342 return
3343 Present
3344 (Find_Body_In (Ent, (First (Declarations (PBody)))));
3345 end if;
3346 end;
3347 end if;
3348 end if;
3349 end Has_Generic_Body;
3351 -----------------------
3352 -- Insert_Elab_Check --
3353 -----------------------
3355 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
3356 Nod : Node_Id;
3357 Loc : constant Source_Ptr := Sloc (N);
3359 Chk : Node_Id;
3360 -- The check (N_Raise_Program_Error) node to be inserted
3362 begin
3363 -- If expansion is disabled, do not generate any checks. Also
3364 -- skip checks if any subunits are missing because in either
3365 -- case we lack the full information that we need, and no object
3366 -- file will be created in any case.
3368 if not Expander_Active or else Subunits_Missing then
3369 return;
3370 end if;
3372 -- If we have a generic instantiation, where Instance_Spec is set,
3373 -- then this field points to a generic instance spec that has
3374 -- been inserted before the instantiation node itself, so that
3375 -- is where we want to insert a check.
3377 if Nkind (N) in N_Generic_Instantiation
3378 and then Present (Instance_Spec (N))
3379 then
3380 Nod := Instance_Spec (N);
3381 else
3382 Nod := N;
3383 end if;
3385 -- Build check node, possibly with condition
3387 Chk :=
3388 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3390 if Present (C) then
3391 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3392 end if;
3394 -- If we are inserting at the top level, insert in Aux_Decls
3396 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3397 declare
3398 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3400 begin
3401 if No (Declarations (ADN)) then
3402 Set_Declarations (ADN, New_List (Chk));
3403 else
3404 Append_To (Declarations (ADN), Chk);
3405 end if;
3407 Analyze (Chk);
3408 end;
3410 -- Otherwise just insert as an action on the node in question
3412 else
3413 Insert_Action (Nod, Chk);
3414 end if;
3415 end Insert_Elab_Check;
3417 -------------------------------
3418 -- Is_Finalization_Procedure --
3419 -------------------------------
3421 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3422 begin
3423 -- Check whether Id is a procedure with at least one parameter
3425 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3426 declare
3427 Typ : constant Entity_Id := Etype (First_Formal (Id));
3428 Deep_Fin : Entity_Id := Empty;
3429 Fin : Entity_Id := Empty;
3431 begin
3432 -- If the type of the first formal does not require finalization
3433 -- actions, then this is definitely not [Deep_]Finalize.
3435 if not Needs_Finalization (Typ) then
3436 return False;
3437 end if;
3439 -- At this point we have the following scenario:
3441 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3443 -- Recover the two possible versions of [Deep_]Finalize using the
3444 -- type of the first parameter and compare with the input.
3446 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3448 if Is_Controlled (Typ) then
3449 Fin := Find_Prim_Op (Typ, Name_Finalize);
3450 end if;
3452 return (Present (Deep_Fin) and then Id = Deep_Fin)
3453 or else (Present (Fin) and then Id = Fin);
3454 end;
3455 end if;
3457 return False;
3458 end Is_Finalization_Procedure;
3460 ------------------
3461 -- Output_Calls --
3462 ------------------
3464 procedure Output_Calls
3465 (N : Node_Id;
3466 Check_Elab_Flag : Boolean)
3468 function Emit (Flag : Boolean) return Boolean;
3469 -- Determine whether to emit an error message based on the combination
3470 -- of flags Check_Elab_Flag and Flag.
3472 function Is_Printable_Error_Name return Boolean;
3473 -- An internal function, used to determine if a name, stored in the
3474 -- Name_Buffer, is either a non-internal name, or is an internal name
3475 -- that is printable by the error message circuits (i.e. it has a single
3476 -- upper case letter at the end).
3478 ----------
3479 -- Emit --
3480 ----------
3482 function Emit (Flag : Boolean) return Boolean is
3483 begin
3484 if Check_Elab_Flag then
3485 return Flag;
3486 else
3487 return True;
3488 end if;
3489 end Emit;
3491 -----------------------------
3492 -- Is_Printable_Error_Name --
3493 -----------------------------
3495 function Is_Printable_Error_Name return Boolean is
3496 begin
3497 if not Is_Internal_Name then
3498 return True;
3500 elsif Name_Len = 1 then
3501 return False;
3503 else
3504 Name_Len := Name_Len - 1;
3505 return not Is_Internal_Name;
3506 end if;
3507 end Is_Printable_Error_Name;
3509 -- Local variables
3511 Ent : Entity_Id;
3513 -- Start of processing for Output_Calls
3515 begin
3516 for J in reverse 1 .. Elab_Call.Last loop
3517 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3519 Ent := Elab_Call.Table (J).Ent;
3520 Get_Name_String (Chars (Ent));
3522 -- Dynamic elaboration model, warnings controlled by -gnatwl
3524 if Dynamic_Elaboration_Checks then
3525 if Emit (Elab_Warnings) then
3526 if Is_Generic_Unit (Ent) then
3527 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3528 elsif Is_Init_Proc (Ent) then
3529 Error_Msg_N ("\\?l?initialization procedure called #", N);
3530 elsif Is_Printable_Error_Name then
3531 Error_Msg_NE ("\\?l?& called #", N, Ent);
3532 else
3533 Error_Msg_N ("\\?l?called #", N);
3534 end if;
3535 end if;
3537 -- Static elaboration model, info messages controlled by -gnatel
3539 else
3540 if Emit (Elab_Info_Messages) then
3541 if Is_Generic_Unit (Ent) then
3542 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3543 elsif Is_Init_Proc (Ent) then
3544 Error_Msg_N ("\\?$?initialization procedure called #", N);
3545 elsif Is_Printable_Error_Name then
3546 Error_Msg_NE ("\\?$?& called #", N, Ent);
3547 else
3548 Error_Msg_N ("\\?$?called #", N);
3549 end if;
3550 end if;
3551 end if;
3552 end loop;
3553 end Output_Calls;
3555 ----------------------------
3556 -- Same_Elaboration_Scope --
3557 ----------------------------
3559 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3560 S1 : Entity_Id;
3561 S2 : Entity_Id;
3563 begin
3564 -- Find elaboration scope for Scop1
3565 -- This is either a subprogram or a compilation unit.
3567 S1 := Scop1;
3568 while S1 /= Standard_Standard
3569 and then not Is_Compilation_Unit (S1)
3570 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3571 loop
3572 S1 := Scope (S1);
3573 end loop;
3575 -- Find elaboration scope for Scop2
3577 S2 := Scop2;
3578 while S2 /= Standard_Standard
3579 and then not Is_Compilation_Unit (S2)
3580 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3581 loop
3582 S2 := Scope (S2);
3583 end loop;
3585 return S1 = S2;
3586 end Same_Elaboration_Scope;
3588 -----------------
3589 -- Set_C_Scope --
3590 -----------------
3592 procedure Set_C_Scope is
3593 begin
3594 while not Is_Compilation_Unit (C_Scope) loop
3595 C_Scope := Scope (C_Scope);
3596 end loop;
3597 end Set_C_Scope;
3599 -----------------
3600 -- Spec_Entity --
3601 -----------------
3603 function Spec_Entity (E : Entity_Id) return Entity_Id is
3604 Decl : Node_Id;
3606 begin
3607 -- Check for case of body entity
3608 -- Why is the check for E_Void needed???
3610 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3611 Decl := E;
3613 loop
3614 Decl := Parent (Decl);
3615 exit when Nkind (Decl) in N_Proper_Body;
3616 end loop;
3618 return Corresponding_Spec (Decl);
3620 else
3621 return E;
3622 end if;
3623 end Spec_Entity;
3625 -------------------
3626 -- Supply_Bodies --
3627 -------------------
3629 procedure Supply_Bodies (N : Node_Id) is
3630 begin
3631 if Nkind (N) = N_Subprogram_Declaration then
3632 declare
3633 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3635 begin
3636 -- Internal subprograms will already have a generated body, so
3637 -- there is no need to provide a stub for them.
3639 if No (Corresponding_Body (N)) then
3640 declare
3641 Loc : constant Source_Ptr := Sloc (N);
3642 Formals : constant List_Id := Copy_Parameter_List (Ent);
3643 Nam : constant Entity_Id :=
3644 Make_Defining_Identifier (Loc, Chars (Ent));
3645 Stats : constant List_Id :=
3646 New_List (
3647 Make_Raise_Program_Error (Loc,
3648 Reason => PE_Access_Before_Elaboration));
3649 Spec : Node_Id;
3651 begin
3652 if Ekind (Ent) = E_Function then
3653 Spec :=
3654 Make_Function_Specification (Loc,
3655 Defining_Unit_Name => Nam,
3656 Parameter_Specifications => Formals,
3657 Result_Definition =>
3658 New_Copy_Tree
3659 (Result_Definition (Specification (N))));
3661 -- We cannot reliably make a return statement for this
3662 -- body, but none is needed because the call raises
3663 -- program error.
3665 Set_Return_Present (Ent);
3667 else
3668 Spec :=
3669 Make_Procedure_Specification (Loc,
3670 Defining_Unit_Name => Nam,
3671 Parameter_Specifications => Formals);
3672 end if;
3674 Insert_After_And_Analyze (N,
3675 Make_Subprogram_Body (Loc,
3676 Specification => Spec,
3677 Declarations => New_List,
3678 Handled_Statement_Sequence =>
3679 Make_Handled_Sequence_Of_Statements (Loc, Stats)));
3680 end;
3681 end if;
3682 end;
3684 elsif Nkind (N) = N_Package_Declaration then
3685 declare
3686 Spec : constant Node_Id := Specification (N);
3687 begin
3688 Push_Scope (Defining_Unit_Name (Spec));
3689 Supply_Bodies (Visible_Declarations (Spec));
3690 Supply_Bodies (Private_Declarations (Spec));
3691 Pop_Scope;
3692 end;
3693 end if;
3694 end Supply_Bodies;
3696 procedure Supply_Bodies (L : List_Id) is
3697 Elmt : Node_Id;
3698 begin
3699 if Present (L) then
3700 Elmt := First (L);
3701 while Present (Elmt) loop
3702 Supply_Bodies (Elmt);
3703 Next (Elmt);
3704 end loop;
3705 end if;
3706 end Supply_Bodies;
3708 ------------
3709 -- Within --
3710 ------------
3712 function Within (E1, E2 : Entity_Id) return Boolean is
3713 Scop : Entity_Id;
3714 begin
3715 Scop := E1;
3716 loop
3717 if Scop = E2 then
3718 return True;
3719 elsif Scop = Standard_Standard then
3720 return False;
3721 else
3722 Scop := Scope (Scop);
3723 end if;
3724 end loop;
3725 end Within;
3727 --------------------------
3728 -- Within_Elaborate_All --
3729 --------------------------
3731 function Within_Elaborate_All
3732 (Unit : Unit_Number_Type;
3733 E : Entity_Id) return Boolean
3735 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3736 pragma Pack (Unit_Number_Set);
3738 Seen : Unit_Number_Set := (others => False);
3739 -- Seen (X) is True after we have seen unit X in the walk. This is used
3740 -- to prevent processing the same unit more than once.
3742 Result : Boolean := False;
3744 procedure Helper (Unit : Unit_Number_Type);
3745 -- This helper procedure does all the work for Within_Elaborate_All. It
3746 -- walks the dependency graph, and sets Result to True if it finds an
3747 -- appropriate Elaborate_All.
3749 ------------
3750 -- Helper --
3751 ------------
3753 procedure Helper (Unit : Unit_Number_Type) is
3754 CU : constant Node_Id := Cunit (Unit);
3756 Item : Node_Id;
3757 Item2 : Node_Id;
3758 Elab_Id : Entity_Id;
3759 Par : Node_Id;
3761 begin
3762 if Seen (Unit) then
3763 return;
3764 else
3765 Seen (Unit) := True;
3766 end if;
3768 -- First, check for Elaborate_Alls on this unit
3770 Item := First (Context_Items (CU));
3771 while Present (Item) loop
3772 if Nkind (Item) = N_Pragma
3773 and then Pragma_Name (Item) = Name_Elaborate_All
3774 then
3775 -- Return if some previous error on the pragma itself. The
3776 -- pragma may be unanalyzed, because of a previous error, or
3777 -- if it is the context of a subunit, inherited by its parent.
3779 if Error_Posted (Item) or else not Analyzed (Item) then
3780 return;
3781 end if;
3783 Elab_Id :=
3784 Entity
3785 (Expression (First (Pragma_Argument_Associations (Item))));
3787 if E = Elab_Id then
3788 Result := True;
3789 return;
3790 end if;
3792 Par := Parent (Unit_Declaration_Node (Elab_Id));
3794 Item2 := First (Context_Items (Par));
3795 while Present (Item2) loop
3796 if Nkind (Item2) = N_With_Clause
3797 and then Entity (Name (Item2)) = E
3798 and then not Limited_Present (Item2)
3799 then
3800 Result := True;
3801 return;
3802 end if;
3804 Next (Item2);
3805 end loop;
3806 end if;
3808 Next (Item);
3809 end loop;
3811 -- Second, recurse on with's. We could do this as part of the above
3812 -- loop, but it's probably more efficient to have two loops, because
3813 -- the relevant Elaborate_All is likely to be on the initial unit. In
3814 -- other words, we're walking the with's breadth-first. This part is
3815 -- only necessary in the dynamic elaboration model.
3817 if Dynamic_Elaboration_Checks then
3818 Item := First (Context_Items (CU));
3819 while Present (Item) loop
3820 if Nkind (Item) = N_With_Clause
3821 and then not Limited_Present (Item)
3822 then
3823 -- Note: the following call to Get_Cunit_Unit_Number does a
3824 -- linear search, which could be slow, but it's OK because
3825 -- we're about to give a warning anyway. Also, there might
3826 -- be hundreds of units, but not millions. If it turns out
3827 -- to be a problem, we could store the Get_Cunit_Unit_Number
3828 -- in each N_Compilation_Unit node, but that would involve
3829 -- rearranging N_Compilation_Unit_Aux to make room.
3831 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3833 if Result then
3834 return;
3835 end if;
3836 end if;
3838 Next (Item);
3839 end loop;
3840 end if;
3841 end Helper;
3843 -- Start of processing for Within_Elaborate_All
3845 begin
3846 Helper (Unit);
3847 return Result;
3848 end Within_Elaborate_All;
3850 end Sem_Elab;