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