PR rtl-optimization/79386
[official-gcc.git] / gcc / ada / sem_elab.adb
blob89b21a0ef6a1528f90505d05d079a2966abfa90c
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-2016, 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 Fname; use Fname;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch7; use Sem_Ch7;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinput; use Sinput;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Table;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Uname; use Uname;
60 package body Sem_Elab is
62 -- The following table records the recursive call chain for output in the
63 -- Output routine. Each entry records the call node and the entity of the
64 -- called routine. The number of entries in the table (i.e. the value of
65 -- Elab_Call.Last) indicates the current depth of recursion and is used to
66 -- identify the outer level.
68 type Elab_Call_Entry is record
69 Cloc : Source_Ptr;
70 Ent : Entity_Id;
71 end record;
73 package Elab_Call is new Table.Table (
74 Table_Component_Type => Elab_Call_Entry,
75 Table_Index_Type => Int,
76 Table_Low_Bound => 1,
77 Table_Initial => 50,
78 Table_Increment => 100,
79 Table_Name => "Elab_Call");
81 -- This table is initialized at the start of each outer level call. It
82 -- holds the entities for all subprograms that have been examined for this
83 -- particular outer level call, and is used to prevent both infinite
84 -- recursion, and useless reanalysis of bodies already seen
86 package Elab_Visited is new Table.Table (
87 Table_Component_Type => Entity_Id,
88 Table_Index_Type => Int,
89 Table_Low_Bound => 1,
90 Table_Initial => 200,
91 Table_Increment => 100,
92 Table_Name => "Elab_Visited");
94 -- This table stores calls to Check_Internal_Call that are delayed until
95 -- all generics are instantiated and in particular until after all generic
96 -- bodies have been inserted. We need to delay, because we need to be able
97 -- to look through the inserted bodies.
99 type Delay_Element is record
100 N : Node_Id;
101 -- The parameter N from the call to Check_Internal_Call. Note that this
102 -- node may get rewritten over the delay period by expansion in the call
103 -- case (but not in the instantiation case).
105 E : Entity_Id;
106 -- The parameter E from the call to Check_Internal_Call
108 Orig_Ent : Entity_Id;
109 -- The parameter Orig_Ent from the call to Check_Internal_Call
111 Curscop : Entity_Id;
112 -- The current scope of the call. This is restored when we complete the
113 -- delayed call, so that we do this in the right scope.
115 From_SPARK_Code : Boolean;
116 -- Save indication of whether this call is under SPARK_Mode => On
118 From_Elab_Code : Boolean;
119 -- Save indication of whether this call is from elaboration code
121 Outer_Scope : Entity_Id;
122 -- Save scope of outer level call
123 end record;
125 package Delay_Check is new Table.Table (
126 Table_Component_Type => Delay_Element,
127 Table_Index_Type => Int,
128 Table_Low_Bound => 1,
129 Table_Initial => 1000,
130 Table_Increment => 100,
131 Table_Name => "Delay_Check");
133 C_Scope : Entity_Id;
134 -- Top-level scope of current scope. Compute this only once at the outer
135 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
137 Outer_Level_Sloc : Source_Ptr;
138 -- Save Sloc value for outer level call node for comparisons of source
139 -- locations. A body is too late if it appears after the *outer* level
140 -- call, not the particular call that is being analyzed.
142 From_Elab_Code : Boolean;
143 -- This flag shows whether the outer level call currently being examined
144 -- is or is not in elaboration code. We are only interested in calls to
145 -- routines in other units if this flag is True.
147 In_Task_Activation : Boolean := False;
148 -- This flag indicates whether we are performing elaboration checks on
149 -- task procedures, at the point of activation. If true, we do not trace
150 -- internal calls in these procedures, because all local bodies are known
151 -- to be elaborated.
153 Delaying_Elab_Checks : Boolean := True;
154 -- This is set True till the compilation is complete, including the
155 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
156 -- the delay table is used to make the delayed calls and this flag is reset
157 -- to False, so that the calls are processed.
159 -----------------------
160 -- Local Subprograms --
161 -----------------------
163 -- Note: Outer_Scope in all following specs represents the scope of
164 -- interest of the outer level call. If it is set to Standard_Standard,
165 -- then it means the outer level call was at elaboration level, and that
166 -- thus all calls are of interest. If it was set to some other scope,
167 -- then the original call was an inner call, and we are not interested
168 -- in calls that go outside this scope.
170 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
171 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
172 -- for the WITH clause for unit U (which will always be present). A special
173 -- case is when N is a function or procedure instantiation, in which case
174 -- it is sufficient to set Elaborate_Desirable, since in this case there is
175 -- no possibility of transitive elaboration issues.
177 procedure Check_A_Call
178 (N : Node_Id;
179 E : Entity_Id;
180 Outer_Scope : Entity_Id;
181 Inter_Unit_Only : Boolean;
182 Generate_Warnings : Boolean := True;
183 In_Init_Proc : Boolean := False);
184 -- This is the internal recursive routine that is called to check for
185 -- possible elaboration error. The argument N is a subprogram call or
186 -- generic instantiation, or 'Access attribute reference to be checked, and
187 -- E is the entity of the called subprogram, or instantiated generic unit,
188 -- or subprogram referenced by 'Access.
190 -- In SPARK mode, N can also be a variable reference, since in SPARK this
191 -- also triggers a requirement for Elaborate_All, and in this case E is the
192 -- entity being referenced.
194 -- Outer_Scope is the outer level scope for the original reference.
195 -- Inter_Unit_Only is set if the call is only to be checked in the
196 -- case where it is to another unit (and skipped if within a unit).
197 -- Generate_Warnings is set to False to suppress warning messages about
198 -- missing pragma Elaborate_All's. These messages are not wanted for
199 -- inner calls in the dynamic model. Note that an instance of the Access
200 -- attribute applied to a subprogram also generates a call to this
201 -- procedure (since the referenced subprogram may be called later
202 -- indirectly). Flag In_Init_Proc should be set whenever the current
203 -- context is a type init proc.
205 -- Note: this might better be called Check_A_Reference to recognize the
206 -- variable case for SPARK, but we prefer to retain the historical name
207 -- since in practice this is mostly about checking calls for the possible
208 -- occurrence of an access-before-elaboration exception.
210 procedure Check_Bad_Instantiation (N : Node_Id);
211 -- N is a node for an instantiation (if called with any other node kind,
212 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
213 -- the special case of a generic instantiation of a generic spec in the
214 -- same declarative part as the instantiation where a body is present and
215 -- has not yet been seen. This is an obvious error, but needs to be checked
216 -- specially at the time of the instantiation, since it is a case where we
217 -- cannot insert the body anywhere. If this case is detected, warnings are
218 -- generated, and a raise of Program_Error is inserted. In addition any
219 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
220 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
221 -- flag as an indication that no attempt should be made to insert an
222 -- instance body.
224 procedure Check_Internal_Call
225 (N : Node_Id;
226 E : Entity_Id;
227 Outer_Scope : Entity_Id;
228 Orig_Ent : Entity_Id);
229 -- N is a function call or procedure statement call node and E is the
230 -- entity of the called function, which is within the current compilation
231 -- unit (where subunits count as part of the parent). This call checks if
232 -- this call, or any call within any accessed body could cause an ABE, and
233 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
234 -- renamings, and points to the original name of the entity. This is used
235 -- for error messages. Outer_Scope is the outer level scope for the
236 -- original call.
238 procedure Check_Internal_Call_Continue
239 (N : Node_Id;
240 E : Entity_Id;
241 Outer_Scope : Entity_Id;
242 Orig_Ent : Entity_Id);
243 -- The processing for Check_Internal_Call is divided up into two phases,
244 -- and this represents the second phase. The second phase is delayed if
245 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
246 -- phase makes an entry in the Delay_Check table, which is processed when
247 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
248 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
249 -- original call.
251 function Has_Generic_Body (N : Node_Id) return Boolean;
252 -- N is a generic package instantiation node, and this routine determines
253 -- if this package spec does in fact have a generic body. If so, then
254 -- True is returned, otherwise False. Note that this is not at all the
255 -- same as checking if the unit requires a body, since it deals with
256 -- the case of optional bodies accurately (i.e. if a body is optional,
257 -- then it looks to see if a body is actually present). Note: this
258 -- function can only do a fully correct job if in generating code mode
259 -- where all bodies have to be present. If we are operating in semantics
260 -- check only mode, then in some cases of optional bodies, a result of
261 -- False may incorrectly be given. In practice this simply means that
262 -- some cases of warnings for incorrect order of elaboration will only
263 -- be given when generating code, which is not a big problem (and is
264 -- inevitable, given the optional body semantics of Ada).
266 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
267 -- Given code for an elaboration check (or unconditional raise if the check
268 -- is not needed), inserts the code in the appropriate place. N is the call
269 -- or instantiation node for which the check code is required. C is the
270 -- test whose failure triggers the raise.
272 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean;
273 -- Returns True if node N is a call to a generic formal subprogram
275 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
276 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
278 procedure Output_Calls
279 (N : Node_Id;
280 Check_Elab_Flag : Boolean);
281 -- Outputs chain of calls stored in the Elab_Call table. The caller has
282 -- already generated the main warning message, so the warnings generated
283 -- are all continuation messages. The argument is the call node at which
284 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
285 -- enumerated only when flag Elab_Warning is set for the dynamic case or
286 -- when flag Elab_Info_Messages is set for the static case.
288 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
289 -- Given two scopes, determine whether they are the same scope from an
290 -- elaboration point of view, i.e. packages and blocks are ignored.
292 procedure Set_C_Scope;
293 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
294 -- to be the enclosing compilation unit of this scope.
296 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
297 -- N is either a function or procedure call or an access attribute that
298 -- references a subprogram. This call retrieves the relevant entity. If
299 -- this is a call to a protected subprogram, the entity is a selected
300 -- component. The callable entity may be absent, in which case Empty is
301 -- returned. This happens with non-analyzed calls in nested generics.
303 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
304 -- entity, in which case, the value returned is simply this entity.
306 procedure Set_Elaboration_Constraint
307 (Call : Node_Id;
308 Subp : Entity_Id;
309 Scop : Entity_Id);
310 -- The current unit U may depend semantically on some unit P that is not
311 -- in the current context. If there is an elaboration call that reaches P,
312 -- we need to indicate that P requires an Elaborate_All, but this is not
313 -- effective in U's ali file, if there is no with_clause for P. In this
314 -- case we add the Elaborate_All on the unit Q that directly or indirectly
315 -- makes P available. This can happen in two cases:
317 -- a) Q declares a subtype of a type declared in P, and the call is an
318 -- initialization call for an object of that subtype.
320 -- b) Q declares an object of some tagged type whose root type is
321 -- declared in P, and the initialization call uses object notation on
322 -- that object to reach a primitive operation or a classwide operation
323 -- declared in P.
325 -- If P appears in the context of U, the current processing is correct.
326 -- Otherwise we must identify these two cases to retrieve Q and place the
327 -- Elaborate_All_Desirable on it.
329 function Spec_Entity (E : Entity_Id) return Entity_Id;
330 -- Given a compilation unit entity, if it is a spec entity, it is returned
331 -- unchanged. If it is a body entity, then the spec for the corresponding
332 -- spec is returned
334 procedure Supply_Bodies (N : Node_Id);
335 -- Given a node, N, that is either a subprogram declaration or a package
336 -- declaration, this procedure supplies dummy bodies for the subprogram
337 -- or for all subprograms in the package. If the given node is not one of
338 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
339 -- contains a single Raise statement.
341 procedure Supply_Bodies (L : List_Id);
342 -- Calls Supply_Bodies for all elements of the given list L
344 function Within (E1, E2 : Entity_Id) return Boolean;
345 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
346 -- of its contained scopes, False otherwise.
348 function Within_Elaborate_All
349 (Unit : Unit_Number_Type;
350 E : Entity_Id) return Boolean;
351 -- Return True if we are within the scope of an Elaborate_All for E, or if
352 -- we are within the scope of an Elaborate_All for some other unit U, and U
353 -- with's E. This prevents spurious warnings when the called entity is
354 -- renamed within U, or in case of generic instances.
356 --------------------------------------
357 -- Activate_Elaborate_All_Desirable --
358 --------------------------------------
360 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
361 UN : constant Unit_Number_Type := Get_Code_Unit (N);
362 CU : constant Node_Id := Cunit (UN);
363 UE : constant Entity_Id := Cunit_Entity (UN);
364 Unm : constant Unit_Name_Type := Unit_Name (UN);
365 CI : constant List_Id := Context_Items (CU);
366 Itm : Node_Id;
367 Ent : Entity_Id;
369 procedure Add_To_Context_And_Mark (Itm : Node_Id);
370 -- This procedure is called when the elaborate indication must be
371 -- applied to a unit not in the context of the referencing unit. The
372 -- unit gets added to the context as an implicit with.
374 function In_Withs_Of (UEs : Entity_Id) return Boolean;
375 -- UEs is the spec entity of a unit. If the unit to be marked is
376 -- in the context item list of this unit spec, then the call returns
377 -- True and Itm is left set to point to the relevant N_With_Clause node.
379 procedure Set_Elab_Flag (Itm : Node_Id);
380 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
382 -----------------------------
383 -- Add_To_Context_And_Mark --
384 -----------------------------
386 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
387 CW : constant Node_Id :=
388 Make_With_Clause (Sloc (Itm),
389 Name => Name (Itm));
391 begin
392 Set_Library_Unit (CW, Library_Unit (Itm));
393 Set_Implicit_With (CW, True);
395 -- Set elaborate all desirable on copy and then append the copy to
396 -- the list of body with's and we are done.
398 Set_Elab_Flag (CW);
399 Append_To (CI, CW);
400 end Add_To_Context_And_Mark;
402 -----------------
403 -- In_Withs_Of --
404 -----------------
406 function In_Withs_Of (UEs : Entity_Id) return Boolean is
407 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
408 CUs : constant Node_Id := Cunit (UNs);
409 CIs : constant List_Id := Context_Items (CUs);
411 begin
412 Itm := First (CIs);
413 while Present (Itm) loop
414 if Nkind (Itm) = N_With_Clause then
415 Ent :=
416 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
418 if U = Ent then
419 return True;
420 end if;
421 end if;
423 Next (Itm);
424 end loop;
426 return False;
427 end In_Withs_Of;
429 -------------------
430 -- Set_Elab_Flag --
431 -------------------
433 procedure Set_Elab_Flag (Itm : Node_Id) is
434 begin
435 if Nkind (N) in N_Subprogram_Instantiation then
436 Set_Elaborate_Desirable (Itm);
437 else
438 Set_Elaborate_All_Desirable (Itm);
439 end if;
440 end Set_Elab_Flag;
442 -- Start of processing for Activate_Elaborate_All_Desirable
444 begin
445 -- Do not set binder indication if expansion is disabled, as when
446 -- compiling a generic unit.
448 if not Expander_Active then
449 return;
450 end if;
452 -- If an instance of a generic package contains a controlled object (so
453 -- we're calling Initialize at elaboration time), and the instance is in
454 -- a package body P that says "with P;", then we need to return without
455 -- adding "pragma Elaborate_All (P);" to P.
457 if U = Main_Unit_Entity then
458 return;
459 end if;
461 Itm := First (CI);
462 while Present (Itm) loop
463 if Nkind (Itm) = N_With_Clause then
464 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
466 -- If we find it, then mark elaborate all desirable and return
468 if U = Ent then
469 Set_Elab_Flag (Itm);
470 return;
471 end if;
472 end if;
474 Next (Itm);
475 end loop;
477 -- If we fall through then the with clause is not present in the
478 -- current unit. One legitimate possibility is that the with clause
479 -- is present in the spec when we are a body.
481 if Is_Body_Name (Unm)
482 and then In_Withs_Of (Spec_Entity (UE))
483 then
484 Add_To_Context_And_Mark (Itm);
485 return;
486 end if;
488 -- Similarly, we may be in the spec or body of a child unit, where
489 -- the unit in question is with'ed by some ancestor of the child unit.
491 if Is_Child_Name (Unm) then
492 declare
493 Pkg : Entity_Id;
495 begin
496 Pkg := UE;
497 loop
498 Pkg := Scope (Pkg);
499 exit when Pkg = Standard_Standard;
501 if In_Withs_Of (Pkg) then
502 Add_To_Context_And_Mark (Itm);
503 return;
504 end if;
505 end loop;
506 end;
507 end if;
509 -- Here if we do not find with clause on spec or body. We just ignore
510 -- this case; it means that the elaboration involves some other unit
511 -- than the unit being compiled, and will be caught elsewhere.
512 end Activate_Elaborate_All_Desirable;
514 ------------------
515 -- Check_A_Call --
516 ------------------
518 procedure Check_A_Call
519 (N : Node_Id;
520 E : Entity_Id;
521 Outer_Scope : Entity_Id;
522 Inter_Unit_Only : Boolean;
523 Generate_Warnings : Boolean := True;
524 In_Init_Proc : Boolean := False)
526 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
527 -- Indicates if we have Access attribute case
529 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean;
530 -- True if we're calling an instance of a generic subprogram, or a
531 -- subprogram in an instance of a generic package, and the call is
532 -- outside that instance.
534 procedure Elab_Warning
535 (Msg_D : String;
536 Msg_S : String;
537 Ent : Node_Or_Entity_Id);
538 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
539 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
540 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
541 -- Msg_S is an info message (output if Elab_Info_Messages is set).
543 function Find_W_Scope return Entity_Id;
544 -- Find top-level scope for called entity (not following renamings
545 -- or derivations). This is where the Elaborate_All will go if it is
546 -- needed. We start with the called entity, except in the case of an
547 -- initialization procedure outside the current package, where the init
548 -- proc is in the root package, and we start from the entity of the name
549 -- in the call.
551 -----------------------------------
552 -- Call_To_Instance_From_Outside --
553 -----------------------------------
555 function Call_To_Instance_From_Outside (Id : Entity_Id) return Boolean is
556 Scop : Entity_Id := Id;
558 begin
559 loop
560 if Scop = Standard_Standard then
561 return False;
562 end if;
564 if Is_Generic_Instance (Scop) then
565 return not In_Open_Scopes (Scop);
566 end if;
568 Scop := Scope (Scop);
569 end loop;
570 end Call_To_Instance_From_Outside;
572 ------------------
573 -- Elab_Warning --
574 ------------------
576 procedure Elab_Warning
577 (Msg_D : String;
578 Msg_S : String;
579 Ent : Node_Or_Entity_Id)
581 begin
582 -- Dynamic elaboration checks, real warning
584 if Dynamic_Elaboration_Checks then
585 if not Access_Case then
586 if Msg_D /= "" and then Elab_Warnings then
587 Error_Msg_NE (Msg_D, N, Ent);
588 end if;
590 -- In the access case emit first warning message as well,
591 -- otherwise list of calls will appear as errors.
593 elsif Elab_Warnings then
594 Error_Msg_NE (Msg_S, N, Ent);
595 end if;
597 -- Static elaboration checks, info message
599 else
600 if Elab_Info_Messages then
601 Error_Msg_NE (Msg_S, N, Ent);
602 end if;
603 end if;
604 end Elab_Warning;
606 ------------------
607 -- Find_W_Scope --
608 ------------------
610 function Find_W_Scope return Entity_Id is
611 Refed_Ent : constant Entity_Id := Get_Referenced_Ent (N);
612 W_Scope : Entity_Id;
614 begin
615 if Is_Init_Proc (Refed_Ent)
616 and then not In_Same_Extended_Unit (N, Refed_Ent)
617 then
618 W_Scope := Scope (Refed_Ent);
619 else
620 W_Scope := E;
621 end if;
623 -- Now loop through scopes to get to the enclosing compilation unit
625 while not Is_Compilation_Unit (W_Scope) loop
626 W_Scope := Scope (W_Scope);
627 end loop;
629 return W_Scope;
630 end Find_W_Scope;
632 -- Locals
634 Variable_Case : constant Boolean :=
635 Nkind (N) in N_Has_Entity
636 and then Present (Entity (N))
637 and then Ekind (Entity (N)) = E_Variable;
638 -- Indicates if we have variable reference case
640 Loc : constant Source_Ptr := Sloc (N);
642 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
643 -- Indicates if we have instantiation case
645 Ent : Entity_Id;
646 Callee_Unit_Internal : Boolean;
647 Caller_Unit_Internal : Boolean;
648 Decl : Node_Id;
649 Inst_Callee : Source_Ptr;
650 Inst_Caller : Source_Ptr;
651 Unit_Callee : Unit_Number_Type;
652 Unit_Caller : Unit_Number_Type;
654 Body_Acts_As_Spec : Boolean;
655 -- Set to true if call is to body acting as spec (no separate spec)
657 Cunit_SC : Boolean := False;
658 -- Set to suppress dynamic elaboration checks where one of the
659 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
660 -- if a pragma Elaborate[_All] applies to that scope, in which case
661 -- warnings on the scope are also suppressed. For the internal case,
662 -- we ignore this flag.
664 E_Scope : Entity_Id;
665 -- Top-level scope of entity for called subprogram. This value includes
666 -- following renamings and derivations, so this scope can be in a
667 -- non-visible unit. This is the scope that is to be investigated to
668 -- see whether an elaboration check is required.
670 Is_DIC_Proc : Boolean := False;
671 -- Flag set when the call denotes the Default_Initial_Condition
672 -- procedure of a private type that wraps a nontrivial assertion
673 -- expression.
675 Issue_In_SPARK : Boolean;
676 -- Flag set when a source entity is called during elaboration in SPARK
678 W_Scope : constant Entity_Id := Find_W_Scope;
679 -- Top-level scope of directly called entity for subprogram. This
680 -- differs from E_Scope in the case where renamings or derivations
681 -- are involved, since it does not follow these links. W_Scope is
682 -- generally in a visible unit, and it is this scope that may require
683 -- an Elaborate_All. However, there are some cases (initialization
684 -- calls and calls involving object notation) where W_Scope might not
685 -- be in the context of the current unit, and there is an intermediate
686 -- package that is, in which case the Elaborate_All has to be placed
687 -- on this intermediate package. These special cases are handled in
688 -- Set_Elaboration_Constraint.
690 -- Start of processing for Check_A_Call
692 begin
693 -- If the call is known to be within a local Suppress Elaboration
694 -- pragma, nothing to check. This can happen in task bodies. But
695 -- we ignore this for a call to a generic formal.
697 if Nkind (N) in N_Subprogram_Call
698 and then No_Elaboration_Check (N)
699 and then not Is_Call_Of_Generic_Formal (N)
700 then
701 return;
702 end if;
704 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
705 -- check, we don't mind in this case if the call occurs before the body
706 -- since this is all generated code.
708 if Nkind (Original_Node (N)) = N_Attribute_Reference
709 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
710 then
711 return;
712 end if;
714 -- Intrinsics such as instances of Unchecked_Deallocation do not have
715 -- any body, so elaboration checking is not needed, and would be wrong.
717 if Is_Intrinsic_Subprogram (E) then
718 return;
719 end if;
721 -- Proceed with check
723 Ent := E;
725 -- For a variable reference, just set Body_Acts_As_Spec to False
727 if Variable_Case then
728 Body_Acts_As_Spec := False;
730 -- Additional checks for all other cases
732 else
733 -- Go to parent for derived subprogram, or to original subprogram in
734 -- the case of a renaming (Alias covers both these cases).
736 loop
737 if (Suppress_Elaboration_Warnings (Ent)
738 or else Elaboration_Checks_Suppressed (Ent))
739 and then (Inst_Case or else No (Alias (Ent)))
740 then
741 return;
742 end if;
744 -- Nothing to do for imported entities
746 if Is_Imported (Ent) then
747 return;
748 end if;
750 exit when Inst_Case or else No (Alias (Ent));
751 Ent := Alias (Ent);
752 end loop;
754 Decl := Unit_Declaration_Node (Ent);
756 if Nkind (Decl) = N_Subprogram_Body then
757 Body_Acts_As_Spec := True;
759 elsif Nkind_In (Decl, N_Subprogram_Declaration,
760 N_Subprogram_Body_Stub)
761 or else Inst_Case
762 then
763 Body_Acts_As_Spec := False;
765 -- If we have none of an instantiation, subprogram body or subprogram
766 -- declaration, or in the SPARK case, a variable reference, then
767 -- it is not a case that we want to check. (One case is a call to a
768 -- generic formal subprogram, where we do not want the check in the
769 -- template).
771 else
772 return;
773 end if;
774 end if;
776 E_Scope := Ent;
777 loop
778 if Elaboration_Checks_Suppressed (E_Scope)
779 or else Suppress_Elaboration_Warnings (E_Scope)
780 then
781 Cunit_SC := True;
782 end if;
784 -- Exit when we get to compilation unit, not counting subunits
786 exit when Is_Compilation_Unit (E_Scope)
787 and then (Is_Child_Unit (E_Scope)
788 or else Scope (E_Scope) = Standard_Standard);
790 pragma Assert (E_Scope /= Standard_Standard);
792 -- Move up a scope looking for compilation unit
794 E_Scope := Scope (E_Scope);
795 end loop;
797 -- No checks needed for pure or preelaborated compilation units
799 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
800 return;
801 end if;
803 -- If the generic entity is within a deeper instance than we are, then
804 -- either the instantiation to which we refer itself caused an ABE, in
805 -- which case that will be handled separately, or else we know that the
806 -- body we need appears as needed at the point of the instantiation.
807 -- However, this assumption is only valid if we are in static mode.
809 if not Dynamic_Elaboration_Checks
810 and then
811 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
812 then
813 return;
814 end if;
816 -- Do not give a warning for a package with no body
818 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
819 return;
820 end if;
822 -- Case of entity is in same unit as call or instantiation. In the
823 -- instantiation case, W_Scope may be different from E_Scope; we want
824 -- the unit in which the instantiation occurs, since we're analyzing
825 -- based on the expansion.
827 if W_Scope = C_Scope then
828 if not Inter_Unit_Only then
829 Check_Internal_Call (N, Ent, Outer_Scope, E);
830 end if;
832 return;
833 end if;
835 -- Case of entity is not in current unit (i.e. with'ed unit case)
837 -- We are only interested in such calls if the outer call was from
838 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
840 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
841 return;
842 end if;
844 -- Nothing to do if some scope said that no checks were required
846 if Cunit_SC then
847 return;
848 end if;
850 -- Nothing to do for a generic instance, because a call to an instance
851 -- cannot fail the elaboration check, because the body of the instance
852 -- is always elaborated immediately after the spec.
854 if Call_To_Instance_From_Outside (Ent) then
855 return;
856 end if;
858 -- Nothing to do if subprogram with no separate spec. However, a call
859 -- to Deep_Initialize may result in a call to a user-defined Initialize
860 -- procedure, which imposes a body dependency. This happens only if the
861 -- type is controlled and the Initialize procedure is not inherited.
863 if Body_Acts_As_Spec then
864 if Is_TSS (Ent, TSS_Deep_Initialize) then
865 declare
866 Typ : constant Entity_Id := Etype (First_Formal (Ent));
867 Init : Entity_Id;
869 begin
870 if not Is_Controlled (Typ) then
871 return;
872 else
873 Init := Find_Prim_Op (Typ, Name_Initialize);
875 if Comes_From_Source (Init) then
876 Ent := Init;
877 else
878 return;
879 end if;
880 end if;
881 end;
883 else
884 return;
885 end if;
886 end if;
888 -- Check cases of internal units
890 Callee_Unit_Internal :=
891 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E_Scope)));
893 -- Do not give a warning if the with'ed unit is internal and this is
894 -- the generic instantiation case (this saves a lot of hassle dealing
895 -- with the Text_IO special child units)
897 if Callee_Unit_Internal and Inst_Case then
898 return;
899 end if;
901 if C_Scope = Standard_Standard then
902 Caller_Unit_Internal := False;
903 else
904 Caller_Unit_Internal :=
905 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (C_Scope)));
906 end if;
908 -- Do not give a warning if the with'ed unit is internal and the
909 -- caller is not internal (since the binder always elaborates
910 -- internal units first).
912 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
913 return;
914 end if;
916 -- For now, if debug flag -gnatdE is not set, do no checking for
917 -- one internal unit withing another. This fixes the problem with
918 -- the sgi build and storage errors. To be resolved later ???
920 if (Callee_Unit_Internal and Caller_Unit_Internal)
921 and not Debug_Flag_EE
922 then
923 return;
924 end if;
926 if Is_TSS (E, TSS_Deep_Initialize) then
927 Ent := E;
928 end if;
930 -- If the call is in an instance, and the called entity is not
931 -- defined in the same instance, then the elaboration issue focuses
932 -- around the unit containing the template, it is this unit that
933 -- requires an Elaborate_All.
935 -- However, if we are doing dynamic elaboration, we need to chase the
936 -- call in the usual manner.
938 -- We also need to chase the call in the usual manner if it is a call
939 -- to a generic formal parameter, since that case was not handled as
940 -- part of the processing of the template.
942 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
943 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
945 if Inst_Caller = No_Location then
946 Unit_Caller := No_Unit;
947 else
948 Unit_Caller := Get_Source_Unit (N);
949 end if;
951 if Inst_Callee = No_Location then
952 Unit_Callee := No_Unit;
953 else
954 Unit_Callee := Get_Source_Unit (Ent);
955 end if;
957 if Unit_Caller /= No_Unit
958 and then Unit_Callee /= Unit_Caller
959 and then not Dynamic_Elaboration_Checks
960 and then not Is_Call_Of_Generic_Formal (N)
961 then
962 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
964 -- If we don't get a spec entity, just ignore call. Not quite
965 -- clear why this check is necessary. ???
967 if No (E_Scope) then
968 return;
969 end if;
971 -- Otherwise step to enclosing compilation unit
973 while not Is_Compilation_Unit (E_Scope) loop
974 E_Scope := Scope (E_Scope);
975 end loop;
977 -- For the case where N is not an instance, and is not a call within
978 -- instance to other than a generic formal, we recompute E_Scope
979 -- for the error message, since we do NOT want to go to the unit
980 -- that has the ultimate declaration in the case of renaming and
981 -- derivation and we also want to go to the generic unit in the
982 -- case of an instance, and no further.
984 else
985 -- Loop to carefully follow renamings and derivations one step
986 -- outside the current unit, but not further.
988 if not (Inst_Case or Variable_Case)
989 and then Present (Alias (Ent))
990 then
991 E_Scope := Alias (Ent);
992 else
993 E_Scope := Ent;
994 end if;
996 loop
997 while not Is_Compilation_Unit (E_Scope) loop
998 E_Scope := Scope (E_Scope);
999 end loop;
1001 -- If E_Scope is the same as C_Scope, it means that there
1002 -- definitely was a local renaming or derivation, and we
1003 -- are not yet out of the current unit.
1005 exit when E_Scope /= C_Scope;
1006 Ent := Alias (Ent);
1007 E_Scope := Ent;
1009 -- If no alias, there could be a previous error, but not if we've
1010 -- already reached the outermost level (Standard).
1012 if No (Ent) then
1013 return;
1014 end if;
1015 end loop;
1016 end if;
1018 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
1019 return;
1020 end if;
1022 Is_DIC_Proc := Is_Nontrivial_DIC_Procedure (Ent);
1024 -- Elaboration issues in SPARK are reported only for source constructs
1025 -- and for nontrivial Default_Initial_Condition procedures. The latter
1026 -- must be checked because the default initialization of an object of a
1027 -- private type triggers the evaluation of the Default_Initial_Condition
1028 -- expression, which in turn may have side effects.
1030 Issue_In_SPARK :=
1031 SPARK_Mode = On
1032 and then Dynamic_Elaboration_Checks
1033 and then (Comes_From_Source (Ent) or Is_DIC_Proc);
1035 -- Now check if an Elaborate_All (or dynamic check) is needed
1037 if not Suppress_Elaboration_Warnings (Ent)
1038 and then not Elaboration_Checks_Suppressed (Ent)
1039 and then not Suppress_Elaboration_Warnings (E_Scope)
1040 and then not Elaboration_Checks_Suppressed (E_Scope)
1041 and then ((Elab_Warnings or Elab_Info_Messages)
1042 or else SPARK_Mode = On)
1043 and then Generate_Warnings
1044 then
1045 -- Instantiation case
1047 if Inst_Case then
1048 if Issue_In_SPARK then
1049 Error_Msg_NE
1050 ("instantiation of & during elaboration in SPARK", N, Ent);
1051 else
1052 Elab_Warning
1053 ("instantiation of & may raise Program_Error?l?",
1054 "info: instantiation of & during elaboration?$?", Ent);
1055 end if;
1057 -- Indirect call case, info message only in static elaboration
1058 -- case, because the attribute reference itself cannot raise an
1059 -- exception. Note that SPARK does not permit indirect calls.
1061 elsif Access_Case then
1062 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
1064 -- Variable reference in SPARK mode
1066 elsif Variable_Case and Issue_In_SPARK then
1067 Error_Msg_NE
1068 ("reference to & during elaboration in SPARK", N, Ent);
1070 -- Subprogram call case
1072 else
1073 if Nkind (Name (N)) in N_Has_Entity
1074 and then Is_Init_Proc (Entity (Name (N)))
1075 and then Comes_From_Source (Ent)
1076 then
1077 Elab_Warning
1078 ("implicit call to & may raise Program_Error?l?",
1079 "info: implicit call to & during elaboration?$?",
1080 Ent);
1082 elsif Issue_In_SPARK then
1084 -- Emit a specialized error message when the elaboration of an
1085 -- object of a private type evaluates the expression of pragma
1086 -- Default_Initial_Condition. This prevents the internal name
1087 -- of the procedure from appearing in the error message.
1089 if Is_DIC_Proc then
1090 Error_Msg_N
1091 ("call to Default_Initial_Condition during elaboration in "
1092 & "SPARK", N);
1093 else
1094 Error_Msg_NE
1095 ("call to & during elaboration in SPARK", N, Ent);
1096 end if;
1098 else
1099 Elab_Warning
1100 ("call to & may raise Program_Error?l?",
1101 "info: call to & during elaboration?$?",
1102 Ent);
1103 end if;
1104 end if;
1106 Error_Msg_Qual_Level := Nat'Last;
1108 -- Case of Elaborate_All not present and required, for SPARK this
1109 -- is an error, so give an error message.
1111 if Issue_In_SPARK then
1112 Error_Msg_NE -- CODEFIX
1113 ("\Elaborate_All pragma required for&", N, W_Scope);
1115 -- Otherwise we generate an implicit pragma. For a subprogram
1116 -- instantiation, Elaborate is good enough, since no transitive
1117 -- call is possible at elaboration time in this case.
1119 elsif Nkind (N) in N_Subprogram_Instantiation then
1120 Elab_Warning
1121 ("\missing pragma Elaborate for&?l?",
1122 "\implicit pragma Elaborate for& generated?$?",
1123 W_Scope);
1125 -- For all other cases, we need an implicit Elaborate_All
1127 else
1128 Elab_Warning
1129 ("\missing pragma Elaborate_All for&?l?",
1130 "\implicit pragma Elaborate_All for & generated?$?",
1131 W_Scope);
1132 end if;
1134 Error_Msg_Qual_Level := 0;
1136 -- Take into account the flags related to elaboration warning
1137 -- messages when enumerating the various calls involved. This
1138 -- ensures the proper pairing of the main warning and the
1139 -- clarification messages generated by Output_Calls.
1141 Output_Calls (N, Check_Elab_Flag => True);
1143 -- Set flag to prevent further warnings for same unit unless in
1144 -- All_Errors_Mode.
1146 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
1147 Set_Suppress_Elaboration_Warnings (W_Scope, True);
1148 end if;
1149 end if;
1151 -- Check for runtime elaboration check required
1153 if Dynamic_Elaboration_Checks then
1154 if not Elaboration_Checks_Suppressed (Ent)
1155 and then not Elaboration_Checks_Suppressed (W_Scope)
1156 and then not Elaboration_Checks_Suppressed (E_Scope)
1157 and then not Cunit_SC
1158 then
1159 -- Runtime elaboration check required. Generate check of the
1160 -- elaboration Boolean for the unit containing the entity.
1162 -- Note that for this case, we do check the real unit (the one
1163 -- from following renamings, since that is the issue).
1165 -- Could this possibly miss a useless but required PE???
1167 Insert_Elab_Check (N,
1168 Make_Attribute_Reference (Loc,
1169 Attribute_Name => Name_Elaborated,
1170 Prefix =>
1171 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1173 -- Prevent duplicate elaboration checks on the same call,
1174 -- which can happen if the body enclosing the call appears
1175 -- itself in a call whose elaboration check is delayed.
1177 if Nkind (N) in N_Subprogram_Call then
1178 Set_No_Elaboration_Check (N);
1179 end if;
1180 end if;
1182 -- Case of static elaboration model
1184 else
1185 -- Do not do anything if elaboration checks suppressed. Note that
1186 -- we check Ent here, not E, since we want the real entity for the
1187 -- body to see if checks are suppressed for it, not the dummy
1188 -- entry for renamings or derivations.
1190 if Elaboration_Checks_Suppressed (Ent)
1191 or else Elaboration_Checks_Suppressed (E_Scope)
1192 or else Elaboration_Checks_Suppressed (W_Scope)
1193 then
1194 null;
1196 -- Do not generate an Elaborate_All for finalization routines
1197 -- that perform partial clean up as part of initialization.
1199 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1200 null;
1202 -- Here we need to generate an implicit elaborate all
1204 else
1205 -- Generate Elaborate_All warning unless suppressed
1207 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1208 and then not Suppress_Elaboration_Warnings (Ent)
1209 and then not Suppress_Elaboration_Warnings (E_Scope)
1210 and then not Suppress_Elaboration_Warnings (W_Scope)
1211 then
1212 Error_Msg_Node_2 := W_Scope;
1213 Error_Msg_NE
1214 ("info: call to& in elaboration code " &
1215 "requires pragma Elaborate_All on&?$?", N, E);
1216 end if;
1218 -- Set indication for binder to generate Elaborate_All
1220 Set_Elaboration_Constraint (N, E, W_Scope);
1221 end if;
1222 end if;
1223 end Check_A_Call;
1225 -----------------------------
1226 -- Check_Bad_Instantiation --
1227 -----------------------------
1229 procedure Check_Bad_Instantiation (N : Node_Id) is
1230 Ent : Entity_Id;
1232 begin
1233 -- Nothing to do if we do not have an instantiation (happens in some
1234 -- error cases, and also in the formal package declaration case)
1236 if Nkind (N) not in N_Generic_Instantiation then
1237 return;
1239 -- Nothing to do if serious errors detected (avoid cascaded errors)
1241 elsif Serious_Errors_Detected /= 0 then
1242 return;
1244 -- Nothing to do if not in full analysis mode
1246 elsif not Full_Analysis then
1247 return;
1249 -- Nothing to do if inside a generic template
1251 elsif Inside_A_Generic then
1252 return;
1254 -- Nothing to do if a library level instantiation
1256 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1257 return;
1259 -- Nothing to do if we are compiling a proper body for semantic
1260 -- purposes only. The generic body may be in another proper body.
1262 elsif
1263 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1264 then
1265 return;
1266 end if;
1268 Ent := Get_Generic_Entity (N);
1270 -- The case we are interested in is when the generic spec is in the
1271 -- current declarative part
1273 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1274 or else not In_Same_Extended_Unit (N, Ent)
1275 then
1276 return;
1277 end if;
1279 -- If the generic entity is within a deeper instance than we are, then
1280 -- either the instantiation to which we refer itself caused an ABE, in
1281 -- which case that will be handled separately. Otherwise, we know that
1282 -- the body we need appears as needed at the point of the instantiation.
1283 -- If they are both at the same level but not within the same instance
1284 -- then the body of the generic will be in the earlier instance.
1286 declare
1287 D1 : constant Nat := Instantiation_Depth (Sloc (Ent));
1288 D2 : constant Nat := Instantiation_Depth (Sloc (N));
1290 begin
1291 if D1 > D2 then
1292 return;
1294 elsif D1 = D2
1295 and then Is_Generic_Instance (Scope (Ent))
1296 and then not In_Open_Scopes (Scope (Ent))
1297 then
1298 return;
1299 end if;
1300 end;
1302 -- Now we can proceed, if the entity being called has a completion,
1303 -- then we are definitely OK, since we have already seen the body.
1305 if Has_Completion (Ent) then
1306 return;
1307 end if;
1309 -- If there is no body, then nothing to do
1311 if not Has_Generic_Body (N) then
1312 return;
1313 end if;
1315 -- Here we definitely have a bad instantiation
1317 Error_Msg_Warn := SPARK_Mode /= On;
1318 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1320 if Present (Instance_Spec (N)) then
1321 Supply_Bodies (Instance_Spec (N));
1322 end if;
1324 Error_Msg_N ("\Program_Error [<<", N);
1325 Insert_Elab_Check (N);
1326 Set_ABE_Is_Certain (N);
1327 end Check_Bad_Instantiation;
1329 ---------------------
1330 -- Check_Elab_Call --
1331 ---------------------
1333 procedure Check_Elab_Call
1334 (N : Node_Id;
1335 Outer_Scope : Entity_Id := Empty;
1336 In_Init_Proc : Boolean := False)
1338 Ent : Entity_Id;
1339 P : Node_Id;
1341 begin
1342 -- If the reference is not in the main unit, there is nothing to check.
1343 -- Elaboration call from units in the context of the main unit will lead
1344 -- to semantic dependencies when those units are compiled.
1346 if not In_Extended_Main_Code_Unit (N) then
1347 return;
1348 end if;
1350 -- For an entry call, check relevant restriction
1352 if Nkind (N) = N_Entry_Call_Statement
1353 and then not In_Subprogram_Or_Concurrent_Unit
1354 then
1355 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1357 -- Nothing to do if this is not an expected type of reference (happens
1358 -- in some error conditions, and in some cases where rewriting occurs).
1360 elsif Nkind (N) not in N_Subprogram_Call
1361 and then Nkind (N) /= N_Attribute_Reference
1362 and then (SPARK_Mode /= On
1363 or else Nkind (N) not in N_Has_Entity
1364 or else No (Entity (N))
1365 or else Ekind (Entity (N)) /= E_Variable)
1366 then
1367 return;
1369 -- Nothing to do if this is a call already rewritten for elab checking.
1370 -- Such calls appear as the targets of If_Expressions.
1372 -- This check MUST be wrong, it catches far too much
1374 elsif Nkind (Parent (N)) = N_If_Expression then
1375 return;
1377 -- Nothing to do if inside a generic template
1379 elsif Inside_A_Generic
1380 and then No (Enclosing_Generic_Body (N))
1381 then
1382 return;
1384 -- Nothing to do if call is being pre-analyzed, as when within a
1385 -- pre/postcondition, a predicate, or an invariant.
1387 elsif In_Spec_Expression then
1388 return;
1389 end if;
1391 -- Nothing to do if this is a call to a postcondition, which is always
1392 -- within a subprogram body, even though the current scope may be the
1393 -- enclosing scope of the subprogram.
1395 if Nkind (N) = N_Procedure_Call_Statement
1396 and then Is_Entity_Name (Name (N))
1397 and then Chars (Entity (Name (N))) = Name_uPostconditions
1398 then
1399 return;
1400 end if;
1402 -- Here we have a reference at elaboration time that must be checked
1404 if Debug_Flag_LL then
1405 Write_Str (" Check_Elab_Ref: ");
1407 if Nkind (N) = N_Attribute_Reference then
1408 if not Is_Entity_Name (Prefix (N)) then
1409 Write_Str ("<<not entity name>>");
1410 else
1411 Write_Name (Chars (Entity (Prefix (N))));
1412 end if;
1414 Write_Str ("'Access");
1416 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1417 Write_Str ("<<not entity name>> ");
1419 else
1420 Write_Name (Chars (Entity (Name (N))));
1421 end if;
1423 Write_Str (" reference at ");
1424 Write_Location (Sloc (N));
1425 Write_Eol;
1426 end if;
1428 -- Climb up the tree to make sure we are not inside default expression
1429 -- of a parameter specification or a record component, since in both
1430 -- these cases, we will be doing the actual reference later, not now,
1431 -- and it is at the time of the actual reference (statically speaking)
1432 -- that we must do our static check, not at the time of its initial
1433 -- analysis).
1435 -- However, we have to check references within component definitions
1436 -- (e.g. a function call that determines an array component bound),
1437 -- so we terminate the loop in that case.
1439 P := Parent (N);
1440 while Present (P) loop
1441 if Nkind_In (P, N_Parameter_Specification,
1442 N_Component_Declaration)
1443 then
1444 return;
1446 -- The reference occurs within the constraint of a component,
1447 -- so it must be checked.
1449 elsif Nkind (P) = N_Component_Definition then
1450 exit;
1452 else
1453 P := Parent (P);
1454 end if;
1455 end loop;
1457 -- Stuff that happens only at the outer level
1459 if No (Outer_Scope) then
1460 Elab_Visited.Set_Last (0);
1462 -- Nothing to do if current scope is Standard (this is a bit odd, but
1463 -- it happens in the case of generic instantiations).
1465 C_Scope := Current_Scope;
1467 if C_Scope = Standard_Standard then
1468 return;
1469 end if;
1471 -- First case, we are in elaboration code
1473 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1475 if From_Elab_Code then
1477 -- Complain if ref that comes from source in preelaborated unit
1478 -- and we are not inside a subprogram (i.e. we are in elab code).
1480 if Comes_From_Source (N)
1481 and then In_Preelaborated_Unit
1482 and then not In_Inlined_Body
1483 and then Nkind (N) /= N_Attribute_Reference
1484 then
1485 -- This is a warning in GNAT mode allowing such calls to be
1486 -- used in the predefined library with appropriate care.
1488 Error_Msg_Warn := GNAT_Mode;
1489 Error_Msg_N
1490 ("<<non-static call not allowed in preelaborated unit", N);
1491 return;
1492 end if;
1494 -- Second case, we are inside a subprogram or concurrent unit, which
1495 -- means we are not in elaboration code.
1497 else
1498 -- In this case, the issue is whether we are inside the
1499 -- declarative part of the unit in which we live, or inside its
1500 -- statements. In the latter case, there is no issue of ABE calls
1501 -- at this level (a call from outside to the unit in which we live
1502 -- might cause an ABE, but that will be detected when we analyze
1503 -- that outer level call, as it recurses into the called unit).
1505 -- Climb up the tree, doing this test, and also testing for being
1506 -- inside a default expression, which, as discussed above, is not
1507 -- checked at this stage.
1509 declare
1510 P : Node_Id;
1511 L : List_Id;
1513 begin
1514 P := N;
1515 loop
1516 -- If we find a parentless subtree, it seems safe to assume
1517 -- that we are not in a declarative part and that no
1518 -- checking is required.
1520 if No (P) then
1521 return;
1522 end if;
1524 if Is_List_Member (P) then
1525 L := List_Containing (P);
1526 P := Parent (L);
1527 else
1528 L := No_List;
1529 P := Parent (P);
1530 end if;
1532 exit when Nkind (P) = N_Subunit;
1534 -- Filter out case of default expressions, where we do not
1535 -- do the check at this stage.
1537 if Nkind_In (P, N_Parameter_Specification,
1538 N_Component_Declaration)
1539 then
1540 return;
1541 end if;
1543 -- A protected body has no elaboration code and contains
1544 -- only other bodies.
1546 if Nkind (P) = N_Protected_Body then
1547 return;
1549 elsif Nkind_In (P, N_Subprogram_Body,
1550 N_Task_Body,
1551 N_Block_Statement,
1552 N_Entry_Body)
1553 then
1554 if L = Declarations (P) then
1555 exit;
1557 -- We are not in elaboration code, but we are doing
1558 -- dynamic elaboration checks, in this case, we still
1559 -- need to do the reference, since the subprogram we are
1560 -- in could be called from another unit, also in dynamic
1561 -- elaboration check mode, at elaboration time.
1563 elsif Dynamic_Elaboration_Checks then
1565 -- We provide a debug flag to disable this check. That
1566 -- way we have an easy work around for regressions
1567 -- that are caused by this new check. This debug flag
1568 -- can be removed later.
1570 if Debug_Flag_DD then
1571 return;
1572 end if;
1574 -- Do the check in this case
1576 exit;
1578 elsif Nkind (P) = N_Task_Body then
1580 -- The check is deferred until Check_Task_Activation
1581 -- but we need to capture local suppress pragmas
1582 -- that may inhibit checks on this call.
1584 Ent := Get_Referenced_Ent (N);
1586 if No (Ent) then
1587 return;
1589 elsif Elaboration_Checks_Suppressed (Current_Scope)
1590 or else Elaboration_Checks_Suppressed (Ent)
1591 or else Elaboration_Checks_Suppressed (Scope (Ent))
1592 then
1593 if Nkind (N) in N_Subprogram_Call then
1594 Set_No_Elaboration_Check (N);
1595 end if;
1596 end if;
1598 return;
1600 -- Static model, call is not in elaboration code, we
1601 -- never need to worry, because in the static model the
1602 -- top-level caller always takes care of things.
1604 else
1605 return;
1606 end if;
1607 end if;
1608 end loop;
1609 end;
1610 end if;
1611 end if;
1613 Ent := Get_Referenced_Ent (N);
1615 if No (Ent) then
1616 return;
1617 end if;
1619 -- Nothing to do if this is a recursive call (i.e. a call to
1620 -- an entity that is already in the Elab_Call stack)
1622 for J in 1 .. Elab_Visited.Last loop
1623 if Ent = Elab_Visited.Table (J) then
1624 return;
1625 end if;
1626 end loop;
1628 -- See if we need to analyze this reference. We analyze it if either of
1629 -- the following conditions is met:
1631 -- It is an inner level call (since in this case it was triggered
1632 -- by an outer level call from elaboration code), but only if the
1633 -- call is within the scope of the original outer level call.
1635 -- It is an outer level reference from elaboration code, or a call to
1636 -- an entity is in the same elaboration scope.
1638 -- And in these cases, we will check both inter-unit calls and
1639 -- intra-unit (within a single unit) calls.
1641 C_Scope := Current_Scope;
1643 -- If not outer level reference, then we follow it if it is within the
1644 -- original scope of the outer reference.
1646 if Present (Outer_Scope)
1647 and then Within (Scope (Ent), Outer_Scope)
1648 then
1649 Set_C_Scope;
1650 Check_A_Call
1651 (N => N,
1652 E => Ent,
1653 Outer_Scope => Outer_Scope,
1654 Inter_Unit_Only => False,
1655 In_Init_Proc => In_Init_Proc);
1657 -- Nothing to do if elaboration checks suppressed for this scope.
1658 -- However, an interesting exception, the fact that elaboration checks
1659 -- are suppressed within an instance (because we can trace the body when
1660 -- we process the template) does not extend to calls to generic formal
1661 -- subprograms.
1663 elsif Elaboration_Checks_Suppressed (Current_Scope)
1664 and then not Is_Call_Of_Generic_Formal (N)
1665 then
1666 null;
1668 elsif From_Elab_Code then
1669 Set_C_Scope;
1670 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1672 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1673 Set_C_Scope;
1674 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1676 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1677 -- is set, then we will do the check, but only in the inter-unit case
1678 -- (this is to accommodate unguarded elaboration calls from other units
1679 -- in which this same mode is set). We don't want warnings in this case,
1680 -- it would generate warnings having nothing to do with elaboration.
1682 elsif Dynamic_Elaboration_Checks then
1683 Set_C_Scope;
1684 Check_A_Call
1686 Ent,
1687 Standard_Standard,
1688 Inter_Unit_Only => True,
1689 Generate_Warnings => False);
1691 -- Otherwise nothing to do
1693 else
1694 return;
1695 end if;
1697 -- A call to an Init_Proc in elaboration code may bring additional
1698 -- dependencies, if some of the record components thereof have
1699 -- initializations that are function calls that come from source. We
1700 -- treat the current node as a call to each of these functions, to check
1701 -- their elaboration impact.
1703 if Is_Init_Proc (Ent) and then From_Elab_Code then
1704 Process_Init_Proc : declare
1705 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1707 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1708 -- Find subprogram calls within body of Init_Proc for Traverse
1709 -- instantiation below.
1711 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1712 -- Traversal procedure to find all calls with body of Init_Proc
1714 ---------------------
1715 -- Check_Init_Call --
1716 ---------------------
1718 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1719 Func : Entity_Id;
1721 begin
1722 if Nkind (Nod) in N_Subprogram_Call
1723 and then Is_Entity_Name (Name (Nod))
1724 then
1725 Func := Entity (Name (Nod));
1727 if Comes_From_Source (Func) then
1728 Check_A_Call
1729 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1730 end if;
1732 return OK;
1734 else
1735 return OK;
1736 end if;
1737 end Check_Init_Call;
1739 -- Start of processing for Process_Init_Proc
1741 begin
1742 if Nkind (Unit_Decl) = N_Subprogram_Body then
1743 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1744 end if;
1745 end Process_Init_Proc;
1746 end if;
1747 end Check_Elab_Call;
1749 -----------------------
1750 -- Check_Elab_Assign --
1751 -----------------------
1753 procedure Check_Elab_Assign (N : Node_Id) is
1754 Ent : Entity_Id;
1755 Scop : Entity_Id;
1757 Pkg_Spec : Entity_Id;
1758 Pkg_Body : Entity_Id;
1760 begin
1761 -- For record or array component, check prefix. If it is an access type,
1762 -- then there is nothing to do (we do not know what is being assigned),
1763 -- but otherwise this is an assignment to the prefix.
1765 if Nkind_In (N, N_Indexed_Component,
1766 N_Selected_Component,
1767 N_Slice)
1768 then
1769 if not Is_Access_Type (Etype (Prefix (N))) then
1770 Check_Elab_Assign (Prefix (N));
1771 end if;
1773 return;
1774 end if;
1776 -- For type conversion, check expression
1778 if Nkind (N) = N_Type_Conversion then
1779 Check_Elab_Assign (Expression (N));
1780 return;
1781 end if;
1783 -- Nothing to do if this is not an entity reference otherwise get entity
1785 if Is_Entity_Name (N) then
1786 Ent := Entity (N);
1787 else
1788 return;
1789 end if;
1791 -- What we are looking for is a reference in the body of a package that
1792 -- modifies a variable declared in the visible part of the package spec.
1794 if Present (Ent)
1795 and then Comes_From_Source (N)
1796 and then not Suppress_Elaboration_Warnings (Ent)
1797 and then Ekind (Ent) = E_Variable
1798 and then not In_Private_Part (Ent)
1799 and then Is_Library_Level_Entity (Ent)
1800 then
1801 Scop := Current_Scope;
1802 loop
1803 if No (Scop) or else Scop = Standard_Standard then
1804 return;
1805 elsif Ekind (Scop) = E_Package
1806 and then Is_Compilation_Unit (Scop)
1807 then
1808 exit;
1809 else
1810 Scop := Scope (Scop);
1811 end if;
1812 end loop;
1814 -- Here Scop points to the containing library package
1816 Pkg_Spec := Scop;
1817 Pkg_Body := Body_Entity (Pkg_Spec);
1819 -- All OK if the package has an Elaborate_Body pragma
1821 if Has_Pragma_Elaborate_Body (Scop) then
1822 return;
1823 end if;
1825 -- OK if entity being modified is not in containing package spec
1827 if not In_Same_Source_Unit (Scop, Ent) then
1828 return;
1829 end if;
1831 -- All OK if entity appears in generic package or generic instance.
1832 -- We just get too messed up trying to give proper warnings in the
1833 -- presence of generics. Better no message than a junk one.
1835 Scop := Scope (Ent);
1836 while Present (Scop) and then Scop /= Pkg_Spec loop
1837 if Ekind (Scop) = E_Generic_Package then
1838 return;
1839 elsif Ekind (Scop) = E_Package
1840 and then Is_Generic_Instance (Scop)
1841 then
1842 return;
1843 end if;
1845 Scop := Scope (Scop);
1846 end loop;
1848 -- All OK if in task, don't issue warnings there
1850 if In_Task_Activation then
1851 return;
1852 end if;
1854 -- OK if no package body
1856 if No (Pkg_Body) then
1857 return;
1858 end if;
1860 -- OK if reference is not in package body
1862 if not In_Same_Source_Unit (Pkg_Body, N) then
1863 return;
1864 end if;
1866 -- OK if package body has no handled statement sequence
1868 declare
1869 HSS : constant Node_Id :=
1870 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1871 begin
1872 if No (HSS) or else not Comes_From_Source (HSS) then
1873 return;
1874 end if;
1875 end;
1877 -- We definitely have a case of a modification of an entity in
1878 -- the package spec from the elaboration code of the package body.
1879 -- We may not give the warning (because there are some additional
1880 -- checks to avoid too many false positives), but it would be a good
1881 -- idea for the binder to try to keep the body elaboration close to
1882 -- the spec elaboration.
1884 Set_Elaborate_Body_Desirable (Pkg_Spec);
1886 -- All OK in gnat mode (we know what we are doing)
1888 if GNAT_Mode then
1889 return;
1890 end if;
1892 -- All OK if all warnings suppressed
1894 if Warning_Mode = Suppress then
1895 return;
1896 end if;
1898 -- All OK if elaboration checks suppressed for entity
1900 if Checks_May_Be_Suppressed (Ent)
1901 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1902 then
1903 return;
1904 end if;
1906 -- OK if the entity is initialized. Note that the No_Initialization
1907 -- flag usually means that the initialization has been rewritten into
1908 -- assignments, but that still counts for us.
1910 declare
1911 Decl : constant Node_Id := Declaration_Node (Ent);
1912 begin
1913 if Nkind (Decl) = N_Object_Declaration
1914 and then (Present (Expression (Decl))
1915 or else No_Initialization (Decl))
1916 then
1917 return;
1918 end if;
1919 end;
1921 -- Here is where we give the warning
1923 -- All OK if warnings suppressed on the entity
1925 if not Has_Warnings_Off (Ent) then
1926 Error_Msg_Sloc := Sloc (Ent);
1928 Error_Msg_NE
1929 ("??& can be accessed by clients before this initialization",
1930 N, Ent);
1931 Error_Msg_NE
1932 ("\??add Elaborate_Body to spec to ensure & is initialized",
1933 N, Ent);
1934 end if;
1936 if not All_Errors_Mode then
1937 Set_Suppress_Elaboration_Warnings (Ent);
1938 end if;
1939 end if;
1940 end Check_Elab_Assign;
1942 ----------------------
1943 -- Check_Elab_Calls --
1944 ----------------------
1946 procedure Check_Elab_Calls is
1947 Save_SPARK_Mode : SPARK_Mode_Type;
1949 begin
1950 -- If expansion is disabled, do not generate any checks, unless we
1951 -- are in GNATprove mode, so that errors are issued in GNATprove for
1952 -- violations of static elaboration rules in SPARK code. Also skip
1953 -- checks if any subunits are missing because in either case we lack the
1954 -- full information that we need, and no object file will be created in
1955 -- any case.
1957 if (not Expander_Active and not GNATprove_Mode)
1958 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1959 or else Subunits_Missing
1960 then
1961 return;
1962 end if;
1964 -- Skip delayed calls if we had any errors
1966 if Serious_Errors_Detected = 0 then
1967 Delaying_Elab_Checks := False;
1968 Expander_Mode_Save_And_Set (True);
1970 for J in Delay_Check.First .. Delay_Check.Last loop
1971 Push_Scope (Delay_Check.Table (J).Curscop);
1972 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1974 -- Set appropriate value of SPARK_Mode
1976 Save_SPARK_Mode := SPARK_Mode;
1978 if Delay_Check.Table (J).From_SPARK_Code then
1979 SPARK_Mode := On;
1980 end if;
1982 Check_Internal_Call_Continue (
1983 N => Delay_Check.Table (J).N,
1984 E => Delay_Check.Table (J).E,
1985 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1986 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1988 SPARK_Mode := Save_SPARK_Mode;
1989 Pop_Scope;
1990 end loop;
1992 -- Set Delaying_Elab_Checks back on for next main compilation
1994 Expander_Mode_Restore;
1995 Delaying_Elab_Checks := True;
1996 end if;
1997 end Check_Elab_Calls;
1999 ------------------------------
2000 -- Check_Elab_Instantiation --
2001 ------------------------------
2003 procedure Check_Elab_Instantiation
2004 (N : Node_Id;
2005 Outer_Scope : Entity_Id := Empty)
2007 Ent : Entity_Id;
2009 begin
2010 -- Check for and deal with bad instantiation case. There is some
2011 -- duplicated code here, but we will worry about this later ???
2013 Check_Bad_Instantiation (N);
2015 if ABE_Is_Certain (N) then
2016 return;
2017 end if;
2019 -- Nothing to do if we do not have an instantiation (happens in some
2020 -- error cases, and also in the formal package declaration case)
2022 if Nkind (N) not in N_Generic_Instantiation then
2023 return;
2024 end if;
2026 -- Nothing to do if inside a generic template
2028 if Inside_A_Generic then
2029 return;
2030 end if;
2032 -- Nothing to do if the instantiation is not in the main unit
2034 if not In_Extended_Main_Code_Unit (N) then
2035 return;
2036 end if;
2038 Ent := Get_Generic_Entity (N);
2039 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
2041 -- See if we need to analyze this instantiation. We analyze it if
2042 -- either of the following conditions is met:
2044 -- It is an inner level instantiation (since in this case it was
2045 -- triggered by an outer level call from elaboration code), but
2046 -- only if the instantiation is within the scope of the original
2047 -- outer level call.
2049 -- It is an outer level instantiation from elaboration code, or the
2050 -- instantiated entity is in the same elaboration scope.
2052 -- And in these cases, we will check both the inter-unit case and
2053 -- the intra-unit (within a single unit) case.
2055 C_Scope := Current_Scope;
2057 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
2058 Set_C_Scope;
2059 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
2061 elsif From_Elab_Code then
2062 Set_C_Scope;
2063 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
2065 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
2066 Set_C_Scope;
2067 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
2069 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2070 -- set, then we will do the check, but only in the inter-unit case (this
2071 -- is to accommodate unguarded elaboration calls from other units in
2072 -- which this same mode is set). We inhibit warnings in this case, since
2073 -- this instantiation is not occurring in elaboration code.
2075 elsif Dynamic_Elaboration_Checks then
2076 Set_C_Scope;
2077 Check_A_Call
2079 Ent,
2080 Standard_Standard,
2081 Inter_Unit_Only => True,
2082 Generate_Warnings => False);
2084 else
2085 return;
2086 end if;
2087 end Check_Elab_Instantiation;
2089 -------------------------
2090 -- Check_Internal_Call --
2091 -------------------------
2093 procedure Check_Internal_Call
2094 (N : Node_Id;
2095 E : Entity_Id;
2096 Outer_Scope : Entity_Id;
2097 Orig_Ent : Entity_Id)
2099 function Within_Initial_Condition (Call : Node_Id) return Boolean;
2100 -- Determine whether call Call occurs within pragma Initial_Condition or
2101 -- pragma Check with check_kind set to Initial_Condition.
2103 ------------------------------
2104 -- Within_Initial_Condition --
2105 ------------------------------
2107 function Within_Initial_Condition (Call : Node_Id) return Boolean is
2108 Args : List_Id;
2109 Nam : Name_Id;
2110 Par : Node_Id;
2112 begin
2113 -- Traverse the parent chain looking for an enclosing pragma
2115 Par := Call;
2116 while Present (Par) loop
2117 if Nkind (Par) = N_Pragma then
2118 Nam := Pragma_Name (Par);
2120 -- Pragma Initial_Condition appears in its alternative from as
2121 -- Check (Initial_Condition, ...).
2123 if Nam = Name_Check then
2124 Args := Pragma_Argument_Associations (Par);
2126 -- Pragma Check should have at least two arguments
2128 pragma Assert (Present (Args));
2130 return
2131 Chars (Expression (First (Args))) = Name_Initial_Condition;
2133 -- Direct match
2135 elsif Nam = Name_Initial_Condition then
2136 return True;
2138 -- Since pragmas are never nested within other pragmas, stop
2139 -- the traversal.
2141 else
2142 return False;
2143 end if;
2145 -- Prevent the search from going too far
2147 elsif Is_Body_Or_Package_Declaration (Par) then
2148 exit;
2149 end if;
2151 Par := Parent (Par);
2153 -- If assertions are not enabled, the check pragma is rewritten
2154 -- as an if_statement in sem_prag, to generate various warnings
2155 -- on boolean expressions. Retrieve the original pragma.
2157 if Nkind (Original_Node (Par)) = N_Pragma then
2158 Par := Original_Node (Par);
2159 end if;
2160 end loop;
2162 return False;
2163 end Within_Initial_Condition;
2165 -- Local variables
2167 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
2169 -- Start of processing for Check_Internal_Call
2171 begin
2172 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2173 -- node comes from source.
2175 if Nkind (N) = N_Attribute_Reference
2176 and then ((not Warn_On_Elab_Access and then not Debug_Flag_Dot_O)
2177 or else not Comes_From_Source (N))
2178 then
2179 return;
2181 -- If not function or procedure call, instantiation, or 'Access, then
2182 -- ignore call (this happens in some error cases and rewriting cases).
2184 elsif not Nkind_In (N, N_Attribute_Reference,
2185 N_Function_Call,
2186 N_Procedure_Call_Statement)
2187 and then not Inst_Case
2188 then
2189 return;
2191 -- Nothing to do if this is a call or instantiation that has already
2192 -- been found to be a sure ABE.
2194 elsif Nkind (N) /= N_Attribute_Reference and then ABE_Is_Certain (N) then
2195 return;
2197 -- Nothing to do if errors already detected (avoid cascaded errors)
2199 elsif Serious_Errors_Detected /= 0 then
2200 return;
2202 -- Nothing to do if not in full analysis mode
2204 elsif not Full_Analysis then
2205 return;
2207 -- Nothing to do if analyzing in special spec-expression mode, since the
2208 -- call is not actually being made at this time.
2210 elsif In_Spec_Expression then
2211 return;
2213 -- Nothing to do for call to intrinsic subprogram
2215 elsif Is_Intrinsic_Subprogram (E) then
2216 return;
2218 -- No need to trace local calls if checking task activation, because
2219 -- other local bodies are elaborated already.
2221 elsif In_Task_Activation then
2222 return;
2224 -- Nothing to do if call is within a generic unit
2226 elsif Inside_A_Generic then
2227 return;
2229 -- Nothing to do when the call appears within pragma Initial_Condition.
2230 -- The pragma is part of the elaboration statements of a package body
2231 -- and may only call external subprograms or subprograms whose body is
2232 -- already available.
2234 elsif Within_Initial_Condition (N) then
2235 return;
2236 end if;
2238 -- Delay this call if we are still delaying calls
2240 if Delaying_Elab_Checks then
2241 Delay_Check.Append (
2242 (N => N,
2243 E => E,
2244 Orig_Ent => Orig_Ent,
2245 Curscop => Current_Scope,
2246 Outer_Scope => Outer_Scope,
2247 From_Elab_Code => From_Elab_Code,
2248 From_SPARK_Code => SPARK_Mode = On));
2249 return;
2251 -- Otherwise, call phase 2 continuation right now
2253 else
2254 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
2255 end if;
2256 end Check_Internal_Call;
2258 ----------------------------------
2259 -- Check_Internal_Call_Continue --
2260 ----------------------------------
2262 procedure Check_Internal_Call_Continue
2263 (N : Node_Id;
2264 E : Entity_Id;
2265 Outer_Scope : Entity_Id;
2266 Orig_Ent : Entity_Id)
2268 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
2269 -- Function applied to each node as we traverse the body. Checks for
2270 -- call or entity reference that needs checking, and if so checks it.
2271 -- Always returns OK, so entire tree is traversed, except that as
2272 -- described below subprogram bodies are skipped for now.
2274 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2275 -- Traverse procedure using above Find_Elab_Reference function
2277 -------------------------
2278 -- Find_Elab_Reference --
2279 -------------------------
2281 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2282 Actual : Node_Id;
2284 begin
2285 -- If user has specified that there are no entry calls in elaboration
2286 -- code, do not trace past an accept statement, because the rendez-
2287 -- vous will happen after elaboration.
2289 if Nkind_In (Original_Node (N), N_Accept_Statement,
2290 N_Selective_Accept)
2291 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2292 then
2293 return Abandon;
2295 -- If we have a function call, check it
2297 elsif Nkind (N) = N_Function_Call then
2298 Check_Elab_Call (N, Outer_Scope);
2299 return OK;
2301 -- If we have a procedure call, check the call, and also check
2302 -- arguments that are assignments (OUT or IN OUT mode formals).
2304 elsif Nkind (N) = N_Procedure_Call_Statement then
2305 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2307 Actual := First_Actual (N);
2308 while Present (Actual) loop
2309 if Known_To_Be_Assigned (Actual) then
2310 Check_Elab_Assign (Actual);
2311 end if;
2313 Next_Actual (Actual);
2314 end loop;
2316 return OK;
2318 -- If we have an access attribute for a subprogram, check it.
2319 -- Suppress this behavior under debug flag.
2321 elsif not Debug_Flag_Dot_UU
2322 and then Nkind (N) = N_Attribute_Reference
2323 and then Nam_In (Attribute_Name (N), Name_Access,
2324 Name_Unrestricted_Access)
2325 and then Is_Entity_Name (Prefix (N))
2326 and then Is_Subprogram (Entity (Prefix (N)))
2327 then
2328 Check_Elab_Call (N, Outer_Scope);
2329 return OK;
2331 -- In SPARK mode, if we have an entity reference to a variable, then
2332 -- check it. For now we consider any reference.
2334 elsif SPARK_Mode = On
2335 and then Nkind (N) in N_Has_Entity
2336 and then Present (Entity (N))
2337 and then Ekind (Entity (N)) = E_Variable
2338 then
2339 Check_Elab_Call (N, Outer_Scope);
2340 return OK;
2342 -- If we have a generic instantiation, check it
2344 elsif Nkind (N) in N_Generic_Instantiation then
2345 Check_Elab_Instantiation (N, Outer_Scope);
2346 return OK;
2348 -- Skip subprogram bodies that come from source (wait for call to
2349 -- analyze these). The reason for the come from source test is to
2350 -- avoid catching task bodies.
2352 -- For task bodies, we should really avoid these too, waiting for the
2353 -- task activation, but that's too much trouble to catch for now, so
2354 -- we go in unconditionally. This is not so terrible, it means the
2355 -- error backtrace is not quite complete, and we are too eager to
2356 -- scan bodies of tasks that are unused, but this is hardly very
2357 -- significant.
2359 elsif Nkind (N) = N_Subprogram_Body
2360 and then Comes_From_Source (N)
2361 then
2362 return Skip;
2364 elsif Nkind (N) = N_Assignment_Statement
2365 and then Comes_From_Source (N)
2366 then
2367 Check_Elab_Assign (Name (N));
2368 return OK;
2370 else
2371 return OK;
2372 end if;
2373 end Find_Elab_Reference;
2375 Inst_Case : constant Boolean := Is_Generic_Unit (E);
2376 Loc : constant Source_Ptr := Sloc (N);
2378 Ebody : Entity_Id;
2379 Sbody : Node_Id;
2381 -- Start of processing for Check_Internal_Call_Continue
2383 begin
2384 -- Save outer level call if at outer level
2386 if Elab_Call.Last = 0 then
2387 Outer_Level_Sloc := Loc;
2388 end if;
2390 Elab_Visited.Append (E);
2392 -- If the call is to a function that renames a literal, no check needed
2394 if Ekind (E) = E_Enumeration_Literal then
2395 return;
2396 end if;
2398 Sbody := Unit_Declaration_Node (E);
2400 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2401 Ebody := Corresponding_Body (Sbody);
2403 if No (Ebody) then
2404 return;
2405 else
2406 Sbody := Unit_Declaration_Node (Ebody);
2407 end if;
2408 end if;
2410 -- If the body appears after the outer level call or instantiation then
2411 -- we have an error case handled below.
2413 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2414 and then not In_Task_Activation
2415 then
2416 null;
2418 -- If we have the instantiation case we are done, since we now
2419 -- know that the body of the generic appeared earlier.
2421 elsif Inst_Case then
2422 return;
2424 -- Otherwise we have a call, so we trace through the called body to see
2425 -- if it has any problems.
2427 else
2428 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2430 Elab_Call.Append ((Cloc => Loc, Ent => E));
2432 if Debug_Flag_LL then
2433 Write_Str ("Elab_Call.Last = ");
2434 Write_Int (Int (Elab_Call.Last));
2435 Write_Str (" Ent = ");
2436 Write_Name (Chars (E));
2437 Write_Str (" at ");
2438 Write_Location (Sloc (N));
2439 Write_Eol;
2440 end if;
2442 -- Now traverse declarations and statements of subprogram body. Note
2443 -- that we cannot simply Traverse (Sbody), since traverse does not
2444 -- normally visit subprogram bodies.
2446 declare
2447 Decl : Node_Id;
2448 begin
2449 Decl := First (Declarations (Sbody));
2450 while Present (Decl) loop
2451 Traverse (Decl);
2452 Next (Decl);
2453 end loop;
2454 end;
2456 Traverse (Handled_Statement_Sequence (Sbody));
2458 Elab_Call.Decrement_Last;
2459 return;
2460 end if;
2462 -- Here is the case of calling a subprogram where the body has not yet
2463 -- been encountered. A warning message is needed, except if this is the
2464 -- case of appearing within an aspect specification that results in
2465 -- a check call, we do not really have such a situation, so no warning
2466 -- is needed (e.g. the case of a precondition, where the call appears
2467 -- textually before the body, but in actual fact is moved to the
2468 -- appropriate subprogram body and so does not need a check).
2470 declare
2471 P : Node_Id;
2472 O : Node_Id;
2474 begin
2475 P := Parent (N);
2476 loop
2477 -- Keep looking at parents if we are still in the subexpression
2479 if Nkind (P) in N_Subexpr then
2480 P := Parent (P);
2482 -- Here P is the parent of the expression, check for special case
2484 else
2485 O := Original_Node (P);
2487 -- Definitely not the special case if orig node is not a pragma
2489 exit when Nkind (O) /= N_Pragma;
2491 -- Check we have an If statement or a null statement (happens
2492 -- when the If has been expanded to be True).
2494 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2496 -- Our special case will be indicated either by the pragma
2497 -- coming from an aspect ...
2499 if Present (Corresponding_Aspect (O)) then
2500 return;
2502 -- Or, in the case of an initial condition, specifically by a
2503 -- Check pragma specifying an Initial_Condition check.
2505 elsif Pragma_Name (O) = Name_Check
2506 and then
2507 Chars
2508 (Expression (First (Pragma_Argument_Associations (O)))) =
2509 Name_Initial_Condition
2510 then
2511 return;
2513 -- For anything else, we have an error
2515 else
2516 exit;
2517 end if;
2518 end if;
2519 end loop;
2520 end;
2522 -- Not that special case, warning and dynamic check is required
2524 -- If we have nothing in the call stack, then this is at the outer
2525 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2526 -- it's a renaming.
2528 if Elab_Call.Last = 0 then
2529 Error_Msg_Warn := SPARK_Mode /= On;
2531 declare
2532 Insert_Check : Boolean := True;
2533 -- This flag is set to True if an elaboration check should be
2534 -- inserted.
2536 begin
2537 if Inst_Case then
2538 Error_Msg_NE
2539 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2541 elsif Nkind (N) = N_Attribute_Reference then
2542 Error_Msg_NE
2543 ("Access attribute of & before body seen<<", N, Orig_Ent);
2544 Error_Msg_N ("\possible Program_Error on later references<", N);
2545 Insert_Check := False;
2547 elsif Nkind (Unit_Declaration_Node (Orig_Ent)) /=
2548 N_Subprogram_Renaming_Declaration
2549 then
2550 Error_Msg_NE
2551 ("cannot call& before body seen<<", N, Orig_Ent);
2553 elsif not Is_Generic_Actual_Subprogram (Orig_Ent) then
2554 Insert_Check := False;
2555 end if;
2557 if Insert_Check then
2558 Error_Msg_N ("\Program_Error [<<", N);
2559 Insert_Elab_Check (N);
2560 end if;
2561 end;
2563 -- Call is not at outer level
2565 else
2566 -- Deal with dynamic elaboration check
2568 if not Elaboration_Checks_Suppressed (E) then
2569 Set_Elaboration_Entity_Required (E);
2571 -- Case of no elaboration entity allocated yet
2573 if No (Elaboration_Entity (E)) then
2575 -- Create object declaration for elaboration entity, and put it
2576 -- just in front of the spec of the subprogram or generic unit,
2577 -- in the same scope as this unit. The subprogram may be over-
2578 -- loaded, so make the name of elaboration entity unique by
2579 -- means of a numeric suffix.
2581 declare
2582 Loce : constant Source_Ptr := Sloc (E);
2583 Ent : constant Entity_Id :=
2584 Make_Defining_Identifier (Loc,
2585 Chars => New_External_Name (Chars (E), 'E', -1));
2587 begin
2588 Set_Elaboration_Entity (E, Ent);
2589 Push_Scope (Scope (E));
2591 Insert_Action (Declaration_Node (E),
2592 Make_Object_Declaration (Loce,
2593 Defining_Identifier => Ent,
2594 Object_Definition =>
2595 New_Occurrence_Of (Standard_Short_Integer, Loce),
2596 Expression =>
2597 Make_Integer_Literal (Loc, Uint_0)));
2599 -- Set elaboration flag at the point of the body
2601 Set_Elaboration_Flag (Sbody, E);
2603 -- Kill current value indication. This is necessary because
2604 -- the tests of this flag are inserted out of sequence and
2605 -- must not pick up bogus indications of the wrong constant
2606 -- value. Also, this is never a true constant, since one way
2607 -- or another, it gets reset.
2609 Set_Current_Value (Ent, Empty);
2610 Set_Last_Assignment (Ent, Empty);
2611 Set_Is_True_Constant (Ent, False);
2612 Pop_Scope;
2613 end;
2614 end if;
2616 -- Generate check of the elaboration counter
2618 Insert_Elab_Check (N,
2619 Make_Attribute_Reference (Loc,
2620 Attribute_Name => Name_Elaborated,
2621 Prefix => New_Occurrence_Of (E, Loc)));
2622 end if;
2624 -- Generate the warning
2626 if not Suppress_Elaboration_Warnings (E)
2627 and then not Elaboration_Checks_Suppressed (E)
2629 -- Suppress this warning if we have a function call that occurred
2630 -- within an assertion expression, since we can get false warnings
2631 -- in this case, due to the out of order handling in this case.
2633 and then
2634 (Nkind (Original_Node (N)) /= N_Function_Call
2635 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2636 then
2637 Error_Msg_Warn := SPARK_Mode /= On;
2639 if Inst_Case then
2640 Error_Msg_NE
2641 ("instantiation of& may occur before body is seen<l<",
2642 N, Orig_Ent);
2643 else
2644 -- A rather specific check. For Finalize/Adjust/Initialize,
2645 -- if the type has Warnings_Off set, suppress the warning.
2647 if Nam_In (Chars (E), Name_Adjust,
2648 Name_Finalize,
2649 Name_Initialize)
2650 and then Present (First_Formal (E))
2651 then
2652 declare
2653 T : constant Entity_Id := Etype (First_Formal (E));
2654 begin
2655 if Is_Controlled (T) then
2656 if Warnings_Off (T)
2657 or else (Ekind (T) = E_Private_Type
2658 and then Warnings_Off (Full_View (T)))
2659 then
2660 goto Output;
2661 end if;
2662 end if;
2663 end;
2664 end if;
2666 -- Go ahead and give warning if not this special case
2668 Error_Msg_NE
2669 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2670 end if;
2672 Error_Msg_N ("\Program_Error ]<l<", N);
2674 -- There is no need to query the elaboration warning message flags
2675 -- because the main message is an error, not a warning, therefore
2676 -- all the clarification messages produces by Output_Calls must be
2677 -- emitted unconditionally.
2679 <<Output>>
2681 Output_Calls (N, Check_Elab_Flag => False);
2682 end if;
2683 end if;
2685 -- Set flag to suppress further warnings on same subprogram
2686 -- unless in all errors mode
2688 if not All_Errors_Mode then
2689 Set_Suppress_Elaboration_Warnings (E);
2690 end if;
2691 end Check_Internal_Call_Continue;
2693 ---------------------------
2694 -- Check_Task_Activation --
2695 ---------------------------
2697 procedure Check_Task_Activation (N : Node_Id) is
2698 Loc : constant Source_Ptr := Sloc (N);
2699 Inter_Procs : constant Elist_Id := New_Elmt_List;
2700 Intra_Procs : constant Elist_Id := New_Elmt_List;
2701 Ent : Entity_Id;
2702 P : Entity_Id;
2703 Task_Scope : Entity_Id;
2704 Cunit_SC : Boolean := False;
2705 Decl : Node_Id;
2706 Elmt : Elmt_Id;
2707 Enclosing : Entity_Id;
2709 procedure Add_Task_Proc (Typ : Entity_Id);
2710 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2711 -- For record types, this procedure recurses over component types.
2713 procedure Collect_Tasks (Decls : List_Id);
2714 -- Collect the types of the tasks that are to be activated in the given
2715 -- list of declarations, in order to perform elaboration checks on the
2716 -- corresponding task procedures that are called implicitly here.
2718 function Outer_Unit (E : Entity_Id) return Entity_Id;
2719 -- find enclosing compilation unit of Entity, ignoring subunits, or
2720 -- else enclosing subprogram. If E is not a package, there is no need
2721 -- for inter-unit elaboration checks.
2723 -------------------
2724 -- Add_Task_Proc --
2725 -------------------
2727 procedure Add_Task_Proc (Typ : Entity_Id) is
2728 Comp : Entity_Id;
2729 Proc : Entity_Id := Empty;
2731 begin
2732 if Is_Task_Type (Typ) then
2733 Proc := Get_Task_Body_Procedure (Typ);
2735 elsif Is_Array_Type (Typ)
2736 and then Has_Task (Base_Type (Typ))
2737 then
2738 Add_Task_Proc (Component_Type (Typ));
2740 elsif Is_Record_Type (Typ)
2741 and then Has_Task (Base_Type (Typ))
2742 then
2743 Comp := First_Component (Typ);
2744 while Present (Comp) loop
2745 Add_Task_Proc (Etype (Comp));
2746 Comp := Next_Component (Comp);
2747 end loop;
2748 end if;
2750 -- If the task type is another unit, we will perform the usual
2751 -- elaboration check on its enclosing unit. If the type is in the
2752 -- same unit, we can trace the task body as for an internal call,
2753 -- but we only need to examine other external calls, because at
2754 -- the point the task is activated, internal subprogram bodies
2755 -- will have been elaborated already. We keep separate lists for
2756 -- each kind of task.
2758 -- Skip this test if errors have occurred, since in this case
2759 -- we can get false indications.
2761 if Serious_Errors_Detected /= 0 then
2762 return;
2763 end if;
2765 if Present (Proc) then
2766 if Outer_Unit (Scope (Proc)) = Enclosing then
2768 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2769 and then
2770 (not Is_Generic_Instance (Scope (Proc))
2771 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2772 then
2773 Error_Msg_Warn := SPARK_Mode /= On;
2774 Error_Msg_N
2775 ("task will be activated before elaboration of its body<<",
2776 Decl);
2777 Error_Msg_N ("\Program_Error [<<", Decl);
2779 elsif Present
2780 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2781 then
2782 Append_Elmt (Proc, Intra_Procs);
2783 end if;
2785 else
2786 -- No need for multiple entries of the same type
2788 Elmt := First_Elmt (Inter_Procs);
2789 while Present (Elmt) loop
2790 if Node (Elmt) = Proc then
2791 return;
2792 end if;
2794 Next_Elmt (Elmt);
2795 end loop;
2797 Append_Elmt (Proc, Inter_Procs);
2798 end if;
2799 end if;
2800 end Add_Task_Proc;
2802 -------------------
2803 -- Collect_Tasks --
2804 -------------------
2806 procedure Collect_Tasks (Decls : List_Id) is
2807 begin
2808 if Present (Decls) then
2809 Decl := First (Decls);
2810 while Present (Decl) loop
2811 if Nkind (Decl) = N_Object_Declaration
2812 and then Has_Task (Etype (Defining_Identifier (Decl)))
2813 then
2814 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2815 end if;
2817 Next (Decl);
2818 end loop;
2819 end if;
2820 end Collect_Tasks;
2822 ----------------
2823 -- Outer_Unit --
2824 ----------------
2826 function Outer_Unit (E : Entity_Id) return Entity_Id is
2827 Outer : Entity_Id;
2829 begin
2830 Outer := E;
2831 while Present (Outer) loop
2832 if Elaboration_Checks_Suppressed (Outer) then
2833 Cunit_SC := True;
2834 end if;
2836 exit when Is_Child_Unit (Outer)
2837 or else Scope (Outer) = Standard_Standard
2838 or else Ekind (Outer) /= E_Package;
2839 Outer := Scope (Outer);
2840 end loop;
2842 return Outer;
2843 end Outer_Unit;
2845 -- Start of processing for Check_Task_Activation
2847 begin
2848 Enclosing := Outer_Unit (Current_Scope);
2850 -- Find all tasks declared in the current unit
2852 if Nkind (N) = N_Package_Body then
2853 P := Unit_Declaration_Node (Corresponding_Spec (N));
2855 Collect_Tasks (Declarations (N));
2856 Collect_Tasks (Visible_Declarations (Specification (P)));
2857 Collect_Tasks (Private_Declarations (Specification (P)));
2859 elsif Nkind (N) = N_Package_Declaration then
2860 Collect_Tasks (Visible_Declarations (Specification (N)));
2861 Collect_Tasks (Private_Declarations (Specification (N)));
2863 else
2864 Collect_Tasks (Declarations (N));
2865 end if;
2867 -- We only perform detailed checks in all tasks that are library level
2868 -- entities. If the master is a subprogram or task, activation will
2869 -- depend on the activation of the master itself.
2871 -- Should dynamic checks be added in the more general case???
2873 if Ekind (Enclosing) /= E_Package then
2874 return;
2875 end if;
2877 -- For task types defined in other units, we want the unit containing
2878 -- the task body to be elaborated before the current one.
2880 Elmt := First_Elmt (Inter_Procs);
2881 while Present (Elmt) loop
2882 Ent := Node (Elmt);
2883 Task_Scope := Outer_Unit (Scope (Ent));
2885 if not Is_Compilation_Unit (Task_Scope) then
2886 null;
2888 elsif Suppress_Elaboration_Warnings (Task_Scope)
2889 or else Elaboration_Checks_Suppressed (Task_Scope)
2890 then
2891 null;
2893 elsif Dynamic_Elaboration_Checks then
2894 if not Elaboration_Checks_Suppressed (Ent)
2895 and then not Cunit_SC
2896 and then
2897 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2898 then
2899 -- Runtime elaboration check required. Generate check of the
2900 -- elaboration counter for the unit containing the entity.
2902 Insert_Elab_Check (N,
2903 Make_Attribute_Reference (Loc,
2904 Attribute_Name => Name_Elaborated,
2905 Prefix =>
2906 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2907 end if;
2909 else
2910 -- Force the binder to elaborate other unit first
2912 if not Suppress_Elaboration_Warnings (Ent)
2913 and then not Elaboration_Checks_Suppressed (Ent)
2914 and then Elab_Info_Messages
2915 and then not Suppress_Elaboration_Warnings (Task_Scope)
2916 and then not Elaboration_Checks_Suppressed (Task_Scope)
2917 then
2918 Error_Msg_Node_2 := Task_Scope;
2919 Error_Msg_NE
2920 ("info: activation of an instance of task type&" &
2921 " requires pragma Elaborate_All on &?$?", N, Ent);
2922 end if;
2924 Activate_Elaborate_All_Desirable (N, Task_Scope);
2925 Set_Suppress_Elaboration_Warnings (Task_Scope);
2926 end if;
2928 Next_Elmt (Elmt);
2929 end loop;
2931 -- For tasks declared in the current unit, trace other calls within
2932 -- the task procedure bodies, which are available.
2934 In_Task_Activation := True;
2936 Elmt := First_Elmt (Intra_Procs);
2937 while Present (Elmt) loop
2938 Ent := Node (Elmt);
2939 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2940 Next_Elmt (Elmt);
2941 end loop;
2943 In_Task_Activation := False;
2944 end Check_Task_Activation;
2946 -------------------------------
2947 -- Is_Call_Of_Generic_Formal --
2948 -------------------------------
2950 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2951 begin
2952 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2954 -- Always return False if debug flag -gnatd.G is set
2956 and then not Debug_Flag_Dot_GG
2958 -- For now, we detect this by looking for the strange identifier
2959 -- node, whose Chars reflect the name of the generic formal, but
2960 -- the Chars of the Entity references the generic actual.
2962 and then Nkind (Name (N)) = N_Identifier
2963 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
2964 end Is_Call_Of_Generic_Formal;
2966 --------------------------------
2967 -- Set_Elaboration_Constraint --
2968 --------------------------------
2970 procedure Set_Elaboration_Constraint
2971 (Call : Node_Id;
2972 Subp : Entity_Id;
2973 Scop : Entity_Id)
2975 Elab_Unit : Entity_Id;
2977 -- Check whether this is a call to an Initialize subprogram for a
2978 -- controlled type. Note that Call can also be a 'Access attribute
2979 -- reference, which now generates an elaboration check.
2981 Init_Call : constant Boolean :=
2982 Nkind (Call) = N_Procedure_Call_Statement
2983 and then Chars (Subp) = Name_Initialize
2984 and then Comes_From_Source (Subp)
2985 and then Present (Parameter_Associations (Call))
2986 and then Is_Controlled (Etype (First_Actual (Call)));
2987 begin
2988 -- If the unit is mentioned in a with_clause of the current unit, it is
2989 -- visible, and we can set the elaboration flag.
2991 if Is_Immediately_Visible (Scop)
2992 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
2993 then
2994 Activate_Elaborate_All_Desirable (Call, Scop);
2995 Set_Suppress_Elaboration_Warnings (Scop, True);
2996 return;
2997 end if;
2999 -- If this is not an initialization call or a call using object notation
3000 -- we know that the unit of the called entity is in the context, and
3001 -- we can set the flag as well. The unit need not be visible if the call
3002 -- occurs within an instantiation.
3004 if Is_Init_Proc (Subp)
3005 or else Init_Call
3006 or else Nkind (Original_Node (Call)) = N_Selected_Component
3007 then
3008 null; -- detailed processing follows.
3010 else
3011 Activate_Elaborate_All_Desirable (Call, Scop);
3012 Set_Suppress_Elaboration_Warnings (Scop, True);
3013 return;
3014 end if;
3016 -- If the unit is not in the context, there must be an intermediate unit
3017 -- that is, on which we need to place to elaboration flag. This happens
3018 -- with init proc calls.
3020 if Is_Init_Proc (Subp) or else Init_Call then
3022 -- The initialization call is on an object whose type is not declared
3023 -- in the same scope as the subprogram. The type of the object must
3024 -- be a subtype of the type of operation. This object is the first
3025 -- actual in the call.
3027 declare
3028 Typ : constant Entity_Id :=
3029 Etype (First (Parameter_Associations (Call)));
3030 begin
3031 Elab_Unit := Scope (Typ);
3032 while (Present (Elab_Unit))
3033 and then not Is_Compilation_Unit (Elab_Unit)
3034 loop
3035 Elab_Unit := Scope (Elab_Unit);
3036 end loop;
3037 end;
3039 -- If original node uses selected component notation, the prefix is
3040 -- visible and determines the scope that must be elaborated. After
3041 -- rewriting, the prefix is the first actual in the call.
3043 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
3044 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
3046 -- Not one of special cases above
3048 else
3049 -- Using previously computed scope. If the elaboration check is
3050 -- done after analysis, the scope is not visible any longer, but
3051 -- must still be in the context.
3053 Elab_Unit := Scop;
3054 end if;
3056 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
3057 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
3058 end Set_Elaboration_Constraint;
3060 ------------------------
3061 -- Get_Referenced_Ent --
3062 ------------------------
3064 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
3065 Nam : Node_Id;
3067 begin
3068 if Nkind (N) in N_Has_Entity
3069 and then Present (Entity (N))
3070 and then Ekind (Entity (N)) = E_Variable
3071 then
3072 return Entity (N);
3073 end if;
3075 if Nkind (N) = N_Attribute_Reference then
3076 Nam := Prefix (N);
3077 else
3078 Nam := Name (N);
3079 end if;
3081 if No (Nam) then
3082 return Empty;
3083 elsif Nkind (Nam) = N_Selected_Component then
3084 return Entity (Selector_Name (Nam));
3085 elsif not Is_Entity_Name (Nam) then
3086 return Empty;
3087 else
3088 return Entity (Nam);
3089 end if;
3090 end Get_Referenced_Ent;
3092 ----------------------
3093 -- Has_Generic_Body --
3094 ----------------------
3096 function Has_Generic_Body (N : Node_Id) return Boolean is
3097 Ent : constant Entity_Id := Get_Generic_Entity (N);
3098 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
3099 Scop : Entity_Id;
3101 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
3102 -- Determine if the list of nodes headed by N and linked by Next
3103 -- contains a package body for the package spec entity E, and if so
3104 -- return the package body. If not, then returns Empty.
3106 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
3107 -- This procedure is called load the unit whose name is given by Nam.
3108 -- This unit is being loaded to see whether it contains an optional
3109 -- generic body. The returned value is the loaded unit, which is always
3110 -- a package body (only package bodies can contain other entities in the
3111 -- sense in which Has_Generic_Body is interested). We only attempt to
3112 -- load bodies if we are generating code. If we are in semantics check
3113 -- only mode, then it would be wrong to load bodies that are not
3114 -- required from a semantic point of view, so in this case we return
3115 -- Empty. The result is that the caller may incorrectly decide that a
3116 -- generic spec does not have a body when in fact it does, but the only
3117 -- harm in this is that some warnings on elaboration problems may be
3118 -- lost in semantic checks only mode, which is not big loss. We also
3119 -- return Empty if we go for a body and it is not there.
3121 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
3122 -- PE is the entity for a package spec. This function locates the
3123 -- corresponding package body, returning Empty if none is found. The
3124 -- package body returned is fully parsed but may not yet be analyzed,
3125 -- so only syntactic fields should be referenced.
3127 ------------------
3128 -- Find_Body_In --
3129 ------------------
3131 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
3132 Nod : Node_Id;
3134 begin
3135 Nod := N;
3136 while Present (Nod) loop
3138 -- If we found the package body we are looking for, return it
3140 if Nkind (Nod) = N_Package_Body
3141 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
3142 then
3143 return Nod;
3145 -- If we found the stub for the body, go after the subunit,
3146 -- loading it if necessary.
3148 elsif Nkind (Nod) = N_Package_Body_Stub
3149 and then Chars (Defining_Identifier (Nod)) = Chars (E)
3150 then
3151 if Present (Library_Unit (Nod)) then
3152 return Unit (Library_Unit (Nod));
3154 else
3155 return Load_Package_Body (Get_Unit_Name (Nod));
3156 end if;
3158 -- If neither package body nor stub, keep looking on chain
3160 else
3161 Next (Nod);
3162 end if;
3163 end loop;
3165 return Empty;
3166 end Find_Body_In;
3168 -----------------------
3169 -- Load_Package_Body --
3170 -----------------------
3172 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
3173 U : Unit_Number_Type;
3175 begin
3176 if Operating_Mode /= Generate_Code then
3177 return Empty;
3178 else
3179 U :=
3180 Load_Unit
3181 (Load_Name => Nam,
3182 Required => False,
3183 Subunit => False,
3184 Error_Node => N);
3186 if U = No_Unit then
3187 return Empty;
3188 else
3189 return Unit (Cunit (U));
3190 end if;
3191 end if;
3192 end Load_Package_Body;
3194 -------------------------------
3195 -- Locate_Corresponding_Body --
3196 -------------------------------
3198 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
3199 Spec : constant Node_Id := Declaration_Node (PE);
3200 Decl : constant Node_Id := Parent (Spec);
3201 Scop : constant Entity_Id := Scope (PE);
3202 PBody : Node_Id;
3204 begin
3205 if Is_Library_Level_Entity (PE) then
3207 -- If package is a library unit that requires a body, we have no
3208 -- choice but to go after that body because it might contain an
3209 -- optional body for the original generic package.
3211 if Unit_Requires_Body (PE) then
3213 -- Load the body. Note that we are a little careful here to use
3214 -- Spec to get the unit number, rather than PE or Decl, since
3215 -- in the case where the package is itself a library level
3216 -- instantiation, Spec will properly reference the generic
3217 -- template, which is what we really want.
3219 return
3220 Load_Package_Body
3221 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
3223 -- But if the package is a library unit that does NOT require
3224 -- a body, then no body is permitted, so we are sure that there
3225 -- is no body for the original generic package.
3227 else
3228 return Empty;
3229 end if;
3231 -- Otherwise look and see if we are embedded in a further package
3233 elsif Is_Package_Or_Generic_Package (Scop) then
3235 -- If so, get the body of the enclosing package, and look in
3236 -- its package body for the package body we are looking for.
3238 PBody := Locate_Corresponding_Body (Scop);
3240 if No (PBody) then
3241 return Empty;
3242 else
3243 return Find_Body_In (PE, First (Declarations (PBody)));
3244 end if;
3246 -- If we are not embedded in a further package, then the body
3247 -- must be in the same declarative part as we are.
3249 else
3250 return Find_Body_In (PE, Next (Decl));
3251 end if;
3252 end Locate_Corresponding_Body;
3254 -- Start of processing for Has_Generic_Body
3256 begin
3257 if Present (Corresponding_Body (Decl)) then
3258 return True;
3260 elsif Unit_Requires_Body (Ent) then
3261 return True;
3263 -- Compilation units cannot have optional bodies
3265 elsif Is_Compilation_Unit (Ent) then
3266 return False;
3268 -- Otherwise look at what scope we are in
3270 else
3271 Scop := Scope (Ent);
3273 -- Case of entity is in other than a package spec, in this case
3274 -- the body, if present, must be in the same declarative part.
3276 if not Is_Package_Or_Generic_Package (Scop) then
3277 declare
3278 P : Node_Id;
3280 begin
3281 -- Declaration node may get us a spec, so if so, go to
3282 -- the parent declaration.
3284 P := Declaration_Node (Ent);
3285 while not Is_List_Member (P) loop
3286 P := Parent (P);
3287 end loop;
3289 return Present (Find_Body_In (Ent, Next (P)));
3290 end;
3292 -- If the entity is in a package spec, then we have to locate
3293 -- the corresponding package body, and look there.
3295 else
3296 declare
3297 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
3299 begin
3300 if No (PBody) then
3301 return False;
3302 else
3303 return
3304 Present
3305 (Find_Body_In (Ent, (First (Declarations (PBody)))));
3306 end if;
3307 end;
3308 end if;
3309 end if;
3310 end Has_Generic_Body;
3312 -----------------------
3313 -- Insert_Elab_Check --
3314 -----------------------
3316 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
3317 Nod : Node_Id;
3318 Loc : constant Source_Ptr := Sloc (N);
3320 Chk : Node_Id;
3321 -- The check (N_Raise_Program_Error) node to be inserted
3323 begin
3324 -- If expansion is disabled, do not generate any checks. Also
3325 -- skip checks if any subunits are missing because in either
3326 -- case we lack the full information that we need, and no object
3327 -- file will be created in any case.
3329 if not Expander_Active or else Subunits_Missing then
3330 return;
3331 end if;
3333 -- If we have a generic instantiation, where Instance_Spec is set,
3334 -- then this field points to a generic instance spec that has
3335 -- been inserted before the instantiation node itself, so that
3336 -- is where we want to insert a check.
3338 if Nkind (N) in N_Generic_Instantiation
3339 and then Present (Instance_Spec (N))
3340 then
3341 Nod := Instance_Spec (N);
3342 else
3343 Nod := N;
3344 end if;
3346 -- Build check node, possibly with condition
3348 Chk :=
3349 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3351 if Present (C) then
3352 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3353 end if;
3355 -- If we are inserting at the top level, insert in Aux_Decls
3357 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3358 declare
3359 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3361 begin
3362 if No (Declarations (ADN)) then
3363 Set_Declarations (ADN, New_List (Chk));
3364 else
3365 Append_To (Declarations (ADN), Chk);
3366 end if;
3368 Analyze (Chk);
3369 end;
3371 -- Otherwise just insert as an action on the node in question
3373 else
3374 Insert_Action (Nod, Chk);
3375 end if;
3376 end Insert_Elab_Check;
3378 -------------------------------
3379 -- Is_Finalization_Procedure --
3380 -------------------------------
3382 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3383 begin
3384 -- Check whether Id is a procedure with at least one parameter
3386 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3387 declare
3388 Typ : constant Entity_Id := Etype (First_Formal (Id));
3389 Deep_Fin : Entity_Id := Empty;
3390 Fin : Entity_Id := Empty;
3392 begin
3393 -- If the type of the first formal does not require finalization
3394 -- actions, then this is definitely not [Deep_]Finalize.
3396 if not Needs_Finalization (Typ) then
3397 return False;
3398 end if;
3400 -- At this point we have the following scenario:
3402 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3404 -- Recover the two possible versions of [Deep_]Finalize using the
3405 -- type of the first parameter and compare with the input.
3407 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3409 if Is_Controlled (Typ) then
3410 Fin := Find_Prim_Op (Typ, Name_Finalize);
3411 end if;
3413 return (Present (Deep_Fin) and then Id = Deep_Fin)
3414 or else (Present (Fin) and then Id = Fin);
3415 end;
3416 end if;
3418 return False;
3419 end Is_Finalization_Procedure;
3421 ------------------
3422 -- Output_Calls --
3423 ------------------
3425 procedure Output_Calls
3426 (N : Node_Id;
3427 Check_Elab_Flag : Boolean)
3429 function Emit (Flag : Boolean) return Boolean;
3430 -- Determine whether to emit an error message based on the combination
3431 -- of flags Check_Elab_Flag and Flag.
3433 function Is_Printable_Error_Name return Boolean;
3434 -- An internal function, used to determine if a name, stored in the
3435 -- Name_Buffer, is either a non-internal name, or is an internal name
3436 -- that is printable by the error message circuits (i.e. it has a single
3437 -- upper case letter at the end).
3439 ----------
3440 -- Emit --
3441 ----------
3443 function Emit (Flag : Boolean) return Boolean is
3444 begin
3445 if Check_Elab_Flag then
3446 return Flag;
3447 else
3448 return True;
3449 end if;
3450 end Emit;
3452 -----------------------------
3453 -- Is_Printable_Error_Name --
3454 -----------------------------
3456 function Is_Printable_Error_Name return Boolean is
3457 begin
3458 if not Is_Internal_Name then
3459 return True;
3461 elsif Name_Len = 1 then
3462 return False;
3464 else
3465 Name_Len := Name_Len - 1;
3466 return not Is_Internal_Name;
3467 end if;
3468 end Is_Printable_Error_Name;
3470 -- Local variables
3472 Ent : Entity_Id;
3474 -- Start of processing for Output_Calls
3476 begin
3477 for J in reverse 1 .. Elab_Call.Last loop
3478 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3480 Ent := Elab_Call.Table (J).Ent;
3481 Get_Name_String (Chars (Ent));
3483 -- Dynamic elaboration model, warnings controlled by -gnatwl
3485 if Dynamic_Elaboration_Checks then
3486 if Emit (Elab_Warnings) then
3487 if Is_Generic_Unit (Ent) then
3488 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3489 elsif Is_Init_Proc (Ent) then
3490 Error_Msg_N ("\\?l?initialization procedure called #", N);
3491 elsif Is_Printable_Error_Name then
3492 Error_Msg_NE ("\\?l?& called #", N, Ent);
3493 else
3494 Error_Msg_N ("\\?l?called #", N);
3495 end if;
3496 end if;
3498 -- Static elaboration model, info messages controlled by -gnatel
3500 else
3501 if Emit (Elab_Info_Messages) then
3502 if Is_Generic_Unit (Ent) then
3503 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3504 elsif Is_Init_Proc (Ent) then
3505 Error_Msg_N ("\\?$?initialization procedure called #", N);
3506 elsif Is_Printable_Error_Name then
3507 Error_Msg_NE ("\\?$?& called #", N, Ent);
3508 else
3509 Error_Msg_N ("\\?$?called #", N);
3510 end if;
3511 end if;
3512 end if;
3513 end loop;
3514 end Output_Calls;
3516 ----------------------------
3517 -- Same_Elaboration_Scope --
3518 ----------------------------
3520 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3521 S1 : Entity_Id;
3522 S2 : Entity_Id;
3524 begin
3525 -- Find elaboration scope for Scop1
3526 -- This is either a subprogram or a compilation unit.
3528 S1 := Scop1;
3529 while S1 /= Standard_Standard
3530 and then not Is_Compilation_Unit (S1)
3531 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3532 loop
3533 S1 := Scope (S1);
3534 end loop;
3536 -- Find elaboration scope for Scop2
3538 S2 := Scop2;
3539 while S2 /= Standard_Standard
3540 and then not Is_Compilation_Unit (S2)
3541 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3542 loop
3543 S2 := Scope (S2);
3544 end loop;
3546 return S1 = S2;
3547 end Same_Elaboration_Scope;
3549 -----------------
3550 -- Set_C_Scope --
3551 -----------------
3553 procedure Set_C_Scope is
3554 begin
3555 while not Is_Compilation_Unit (C_Scope) loop
3556 C_Scope := Scope (C_Scope);
3557 end loop;
3558 end Set_C_Scope;
3560 -----------------
3561 -- Spec_Entity --
3562 -----------------
3564 function Spec_Entity (E : Entity_Id) return Entity_Id is
3565 Decl : Node_Id;
3567 begin
3568 -- Check for case of body entity
3569 -- Why is the check for E_Void needed???
3571 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3572 Decl := E;
3574 loop
3575 Decl := Parent (Decl);
3576 exit when Nkind (Decl) in N_Proper_Body;
3577 end loop;
3579 return Corresponding_Spec (Decl);
3581 else
3582 return E;
3583 end if;
3584 end Spec_Entity;
3586 -------------------
3587 -- Supply_Bodies --
3588 -------------------
3590 procedure Supply_Bodies (N : Node_Id) is
3591 begin
3592 if Nkind (N) = N_Subprogram_Declaration then
3593 declare
3594 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3596 begin
3597 -- Internal subprograms will already have a generated body, so
3598 -- there is no need to provide a stub for them.
3600 if No (Corresponding_Body (N)) then
3601 declare
3602 Loc : constant Source_Ptr := Sloc (N);
3603 B : Node_Id;
3604 Formals : constant List_Id := Copy_Parameter_List (Ent);
3605 Nam : constant Entity_Id :=
3606 Make_Defining_Identifier (Loc, Chars (Ent));
3607 Spec : Node_Id;
3608 Stats : constant List_Id :=
3609 New_List
3610 (Make_Raise_Program_Error (Loc,
3611 Reason => PE_Access_Before_Elaboration));
3613 begin
3614 if Ekind (Ent) = E_Function then
3615 Spec :=
3616 Make_Function_Specification (Loc,
3617 Defining_Unit_Name => Nam,
3618 Parameter_Specifications => Formals,
3619 Result_Definition =>
3620 New_Copy_Tree
3621 (Result_Definition (Specification (N))));
3623 -- We cannot reliably make a return statement for this
3624 -- body, but none is needed because the call raises
3625 -- program error.
3627 Set_Return_Present (Ent);
3629 else
3630 Spec :=
3631 Make_Procedure_Specification (Loc,
3632 Defining_Unit_Name => Nam,
3633 Parameter_Specifications => Formals);
3634 end if;
3636 B := Make_Subprogram_Body (Loc,
3637 Specification => Spec,
3638 Declarations => New_List,
3639 Handled_Statement_Sequence =>
3640 Make_Handled_Sequence_Of_Statements (Loc, Stats));
3641 Insert_After (N, B);
3642 Analyze (B);
3643 end;
3644 end if;
3645 end;
3647 elsif Nkind (N) = N_Package_Declaration then
3648 declare
3649 Spec : constant Node_Id := Specification (N);
3650 begin
3651 Push_Scope (Defining_Unit_Name (Spec));
3652 Supply_Bodies (Visible_Declarations (Spec));
3653 Supply_Bodies (Private_Declarations (Spec));
3654 Pop_Scope;
3655 end;
3656 end if;
3657 end Supply_Bodies;
3659 procedure Supply_Bodies (L : List_Id) is
3660 Elmt : Node_Id;
3661 begin
3662 if Present (L) then
3663 Elmt := First (L);
3664 while Present (Elmt) loop
3665 Supply_Bodies (Elmt);
3666 Next (Elmt);
3667 end loop;
3668 end if;
3669 end Supply_Bodies;
3671 ------------
3672 -- Within --
3673 ------------
3675 function Within (E1, E2 : Entity_Id) return Boolean is
3676 Scop : Entity_Id;
3677 begin
3678 Scop := E1;
3679 loop
3680 if Scop = E2 then
3681 return True;
3682 elsif Scop = Standard_Standard then
3683 return False;
3684 else
3685 Scop := Scope (Scop);
3686 end if;
3687 end loop;
3688 end Within;
3690 --------------------------
3691 -- Within_Elaborate_All --
3692 --------------------------
3694 function Within_Elaborate_All
3695 (Unit : Unit_Number_Type;
3696 E : Entity_Id) return Boolean
3698 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3699 pragma Pack (Unit_Number_Set);
3701 Seen : Unit_Number_Set := (others => False);
3702 -- Seen (X) is True after we have seen unit X in the walk. This is used
3703 -- to prevent processing the same unit more than once.
3705 Result : Boolean := False;
3707 procedure Helper (Unit : Unit_Number_Type);
3708 -- This helper procedure does all the work for Within_Elaborate_All. It
3709 -- walks the dependency graph, and sets Result to True if it finds an
3710 -- appropriate Elaborate_All.
3712 ------------
3713 -- Helper --
3714 ------------
3716 procedure Helper (Unit : Unit_Number_Type) is
3717 CU : constant Node_Id := Cunit (Unit);
3719 Item : Node_Id;
3720 Item2 : Node_Id;
3721 Elab_Id : Entity_Id;
3722 Par : Node_Id;
3724 begin
3725 if Seen (Unit) then
3726 return;
3727 else
3728 Seen (Unit) := True;
3729 end if;
3731 -- First, check for Elaborate_Alls on this unit
3733 Item := First (Context_Items (CU));
3734 while Present (Item) loop
3735 if Nkind (Item) = N_Pragma
3736 and then Pragma_Name (Item) = Name_Elaborate_All
3737 then
3738 -- Return if some previous error on the pragma itself. The
3739 -- pragma may be unanalyzed, because of a previous error, or
3740 -- if it is the context of a subunit, inherited by its parent.
3742 if Error_Posted (Item) or else not Analyzed (Item) then
3743 return;
3744 end if;
3746 Elab_Id :=
3747 Entity
3748 (Expression (First (Pragma_Argument_Associations (Item))));
3750 if E = Elab_Id then
3751 Result := True;
3752 return;
3753 end if;
3755 Par := Parent (Unit_Declaration_Node (Elab_Id));
3757 Item2 := First (Context_Items (Par));
3758 while Present (Item2) loop
3759 if Nkind (Item2) = N_With_Clause
3760 and then Entity (Name (Item2)) = E
3761 and then not Limited_Present (Item2)
3762 then
3763 Result := True;
3764 return;
3765 end if;
3767 Next (Item2);
3768 end loop;
3769 end if;
3771 Next (Item);
3772 end loop;
3774 -- Second, recurse on with's. We could do this as part of the above
3775 -- loop, but it's probably more efficient to have two loops, because
3776 -- the relevant Elaborate_All is likely to be on the initial unit. In
3777 -- other words, we're walking the with's breadth-first. This part is
3778 -- only necessary in the dynamic elaboration model.
3780 if Dynamic_Elaboration_Checks then
3781 Item := First (Context_Items (CU));
3782 while Present (Item) loop
3783 if Nkind (Item) = N_With_Clause
3784 and then not Limited_Present (Item)
3785 then
3786 -- Note: the following call to Get_Cunit_Unit_Number does a
3787 -- linear search, which could be slow, but it's OK because
3788 -- we're about to give a warning anyway. Also, there might
3789 -- be hundreds of units, but not millions. If it turns out
3790 -- to be a problem, we could store the Get_Cunit_Unit_Number
3791 -- in each N_Compilation_Unit node, but that would involve
3792 -- rearranging N_Compilation_Unit_Aux to make room.
3794 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3796 if Result then
3797 return;
3798 end if;
3799 end if;
3801 Next (Item);
3802 end loop;
3803 end if;
3804 end Helper;
3806 -- Start of processing for Within_Elaborate_All
3808 begin
3809 Helper (Unit);
3810 return Result;
3811 end Within_Elaborate_All;
3813 end Sem_Elab;