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