2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / gcc / ada / sem_elab.adb
blobe5e29bcce21e04f819fa8248f5bbdf2052ca4605
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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch7; use Sem_Ch7;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinput; use Sinput;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Table;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Uname; use Uname;
60 package body Sem_Elab is
62 -- The following table records the recursive call chain for output in the
63 -- Output routine. Each entry records the call node and the entity of the
64 -- called routine. The number of entries in the table (i.e. the value of
65 -- Elab_Call.Last) indicates the current depth of recursion and is used to
66 -- identify the outer level.
68 type Elab_Call_Entry is record
69 Cloc : Source_Ptr;
70 Ent : Entity_Id;
71 end record;
73 package Elab_Call is new Table.Table (
74 Table_Component_Type => Elab_Call_Entry,
75 Table_Index_Type => Int,
76 Table_Low_Bound => 1,
77 Table_Initial => 50,
78 Table_Increment => 100,
79 Table_Name => "Elab_Call");
81 -- This table is initialized at the start of each outer level call. It
82 -- holds the entities for all subprograms that have been examined for this
83 -- particular outer level call, and is used to prevent both infinite
84 -- recursion, and useless reanalysis of bodies already seen
86 package Elab_Visited is new Table.Table (
87 Table_Component_Type => Entity_Id,
88 Table_Index_Type => Int,
89 Table_Low_Bound => 1,
90 Table_Initial => 200,
91 Table_Increment => 100,
92 Table_Name => "Elab_Visited");
94 -- This table stores calls to Check_Internal_Call that are delayed
95 -- until all generics are instantiated, and in particular that all
96 -- generic bodies have been inserted. We need to delay, because we
97 -- need to be able to look through the inserted bodies.
99 type Delay_Element is record
100 N : Node_Id;
101 -- The parameter N from the call to Check_Internal_Call. Note that
102 -- this node may get rewritten over the delay period by expansion
103 -- in the call case (but not in the instantiation case).
105 E : Entity_Id;
106 -- The parameter E from the call to Check_Internal_Call
108 Orig_Ent : Entity_Id;
109 -- The parameter Orig_Ent from the call to Check_Internal_Call
111 Curscop : Entity_Id;
112 -- The current scope of the call. This is restored when we complete
113 -- the delayed call, so that we do this in the right scope.
115 From_Elab_Code : Boolean;
116 -- Save indication of whether this call is from elaboration code
118 Outer_Scope : Entity_Id;
119 -- Save scope of outer level call
120 end record;
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");
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.
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.
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.
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.
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.
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
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.
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.
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.
187 -- The flag Outer_Scope is the outer level scope for the original call.
188 -- Inter_Unit_Only is set if the call is only to be checked in the
189 -- case where it is to another unit (and skipped if within a unit).
190 -- Generate_Warnings is set to False to suppress warning messages about
191 -- missing pragma Elaborate_All's. These messages are not wanted for
192 -- inner calls in the dynamic model. Note that an instance of the Access
193 -- attribute applied to a subprogram also generates a call to this
194 -- procedure (since the referenced subprogram may be called later
195 -- indirectly). Flag In_Init_Proc should be set whenever the current
196 -- context is a type init proc.
198 procedure Check_Bad_Instantiation (N : Node_Id);
199 -- N is a node for an instantiation (if called with any other node kind,
200 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
201 -- the special case of a generic instantiation of a generic spec in the
202 -- same declarative part as the instantiation where a body is present and
203 -- has not yet been seen. This is an obvious error, but needs to be checked
204 -- specially at the time of the instantiation, since it is a case where we
205 -- cannot insert the body anywhere. If this case is detected, warnings are
206 -- generated, and a raise of Program_Error is inserted. In addition any
207 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
208 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
209 -- flag as an indication that no attempt should be made to insert an
210 -- instance body.
212 procedure Check_Internal_Call
213 (N : Node_Id;
214 E : Entity_Id;
215 Outer_Scope : Entity_Id;
216 Orig_Ent : Entity_Id);
217 -- N is a function call or procedure statement call node and E is the
218 -- entity of the called function, which is within the current compilation
219 -- unit (where subunits count as part of the parent). This call checks if
220 -- this call, or any call within any accessed body could cause an ABE, and
221 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
222 -- renamings, and points to the original name of the entity. This is used
223 -- for error messages. Outer_Scope is the outer level scope for the
224 -- original call.
226 procedure Check_Internal_Call_Continue
227 (N : Node_Id;
228 E : Entity_Id;
229 Outer_Scope : Entity_Id;
230 Orig_Ent : Entity_Id);
231 -- The processing for Check_Internal_Call is divided up into two phases,
232 -- and this represents the second phase. The second phase is delayed if
233 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
234 -- phase makes an entry in the Delay_Check table, which is processed when
235 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
236 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
237 -- original call.
239 function Has_Generic_Body (N : Node_Id) return Boolean;
240 -- N is a generic package instantiation node, and this routine determines
241 -- if this package spec does in fact have a generic body. If so, then
242 -- True is returned, otherwise False. Note that this is not at all the
243 -- same as checking if the unit requires a body, since it deals with
244 -- the case of optional bodies accurately (i.e. if a body is optional,
245 -- then it looks to see if a body is actually present). Note: this
246 -- function can only do a fully correct job if in generating code mode
247 -- where all bodies have to be present. If we are operating in semantics
248 -- check only mode, then in some cases of optional bodies, a result of
249 -- False may incorrectly be given. In practice this simply means that
250 -- some cases of warnings for incorrect order of elaboration will only
251 -- be given when generating code, which is not a big problem (and is
252 -- inevitable, given the optional body semantics of Ada).
254 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
255 -- Given code for an elaboration check (or unconditional raise if the check
256 -- is not needed), inserts the code in the appropriate place. N is the call
257 -- or instantiation node for which the check code is required. C is the
258 -- test whose failure triggers the raise.
260 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean;
261 -- Returns True if node N is a call to a generic formal subprogram
263 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
264 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
266 procedure Output_Calls
267 (N : Node_Id;
268 Check_Elab_Flag : Boolean);
269 -- Outputs chain of calls stored in the Elab_Call table. The caller has
270 -- already generated the main warning message, so the warnings generated
271 -- are all continuation messages. The argument is the call node at which
272 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
273 -- enumerated only when flag Elab_Warning is set for the dynamic case or
274 -- when flag Elab_Info_Messages is set for the static case.
276 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
277 -- Given two scopes, determine whether they are the same scope from an
278 -- elaboration point of view, i.e. packages and blocks are ignored.
280 procedure Set_C_Scope;
281 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
282 -- to be the enclosing compilation unit of this scope.
284 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
285 -- N is either a function or procedure call or an access attribute that
286 -- references a subprogram. This call retrieves the relevant entity. If
287 -- this is a call to a protected subprogram, the entity is a selected
288 -- component. The callable entity may be absent, in which case Empty is
289 -- returned. This happens with non-analyzed calls in nested generics.
291 procedure Set_Elaboration_Constraint
292 (Call : Node_Id;
293 Subp : Entity_Id;
294 Scop : Entity_Id);
295 -- The current unit U may depend semantically on some unit P which is not
296 -- in the current context. If there is an elaboration call that reaches P,
297 -- we need to indicate that P requires an Elaborate_All, but this is not
298 -- effective in U's ali file, if there is no with_clause for P. In this
299 -- case we add the Elaborate_All on the unit Q that directly or indirectly
300 -- makes P available. This can happen in two cases:
302 -- a) Q declares a subtype of a type declared in P, and the call is an
303 -- initialization call for an object of that subtype.
305 -- b) Q declares an object of some tagged type whose root type is
306 -- declared in P, and the initialization call uses object notation on
307 -- that object to reach a primitive operation or a classwide operation
308 -- declared in P.
310 -- If P appears in the context of U, the current processing is correct.
311 -- Otherwise we must identify these two cases to retrieve Q and place the
312 -- Elaborate_All_Desirable on it.
314 function Spec_Entity (E : Entity_Id) return Entity_Id;
315 -- Given a compilation unit entity, if it is a spec entity, it is returned
316 -- unchanged. If it is a body entity, then the spec for the corresponding
317 -- spec is returned
319 procedure Supply_Bodies (N : Node_Id);
320 -- Given a node, N, that is either a subprogram declaration or a package
321 -- declaration, this procedure supplies dummy bodies for the subprogram
322 -- or for all subprograms in the package. If the given node is not one of
323 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
324 -- contains a single Raise statement.
326 procedure Supply_Bodies (L : List_Id);
327 -- Calls Supply_Bodies for all elements of the given list L
329 function Within (E1, E2 : Entity_Id) return Boolean;
330 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
331 -- of its contained scopes, False otherwise.
333 function Within_Elaborate_All
334 (Unit : Unit_Number_Type;
335 E : Entity_Id) return Boolean;
336 -- Return True if we are within the scope of an Elaborate_All for E, or if
337 -- we are within the scope of an Elaborate_All for some other unit U, and U
338 -- with's E. This prevents spurious warnings when the called entity is
339 -- renamed within U, or in case of generic instances.
341 --------------------------------------
342 -- Activate_Elaborate_All_Desirable --
343 --------------------------------------
345 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
346 UN : constant Unit_Number_Type := Get_Code_Unit (N);
347 CU : constant Node_Id := Cunit (UN);
348 UE : constant Entity_Id := Cunit_Entity (UN);
349 Unm : constant Unit_Name_Type := Unit_Name (UN);
350 CI : constant List_Id := Context_Items (CU);
351 Itm : Node_Id;
352 Ent : Entity_Id;
354 procedure Add_To_Context_And_Mark (Itm : Node_Id);
355 -- This procedure is called when the elaborate indication must be
356 -- applied to a unit not in the context of the referencing unit. The
357 -- unit gets added to the context as an implicit with.
359 function In_Withs_Of (UEs : Entity_Id) return Boolean;
360 -- UEs is the spec entity of a unit. If the unit to be marked is
361 -- in the context item list of this unit spec, then the call returns
362 -- True and Itm is left set to point to the relevant N_With_Clause node.
364 procedure Set_Elab_Flag (Itm : Node_Id);
365 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
367 -----------------------------
368 -- Add_To_Context_And_Mark --
369 -----------------------------
371 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
372 CW : constant Node_Id :=
373 Make_With_Clause (Sloc (Itm),
374 Name => Name (Itm));
376 begin
377 Set_Library_Unit (CW, Library_Unit (Itm));
378 Set_Implicit_With (CW, True);
380 -- Set elaborate all desirable on copy and then append the copy to
381 -- the list of body with's and we are done.
383 Set_Elab_Flag (CW);
384 Append_To (CI, CW);
385 end Add_To_Context_And_Mark;
387 -----------------
388 -- In_Withs_Of --
389 -----------------
391 function In_Withs_Of (UEs : Entity_Id) return Boolean is
392 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
393 CUs : constant Node_Id := Cunit (UNs);
394 CIs : constant List_Id := Context_Items (CUs);
396 begin
397 Itm := First (CIs);
398 while Present (Itm) loop
399 if Nkind (Itm) = N_With_Clause then
400 Ent :=
401 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
403 if U = Ent then
404 return True;
405 end if;
406 end if;
408 Next (Itm);
409 end loop;
411 return False;
412 end In_Withs_Of;
414 -------------------
415 -- Set_Elab_Flag --
416 -------------------
418 procedure Set_Elab_Flag (Itm : Node_Id) is
419 begin
420 if Nkind (N) in N_Subprogram_Instantiation then
421 Set_Elaborate_Desirable (Itm);
422 else
423 Set_Elaborate_All_Desirable (Itm);
424 end if;
425 end Set_Elab_Flag;
427 -- Start of processing for Activate_Elaborate_All_Desirable
429 begin
430 -- Do not set binder indication if expansion is disabled, as when
431 -- compiling a generic unit.
433 if not Expander_Active then
434 return;
435 end if;
437 Itm := First (CI);
438 while Present (Itm) loop
439 if Nkind (Itm) = N_With_Clause then
440 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
442 -- If we find it, then mark elaborate all desirable and return
444 if U = Ent then
445 Set_Elab_Flag (Itm);
446 return;
447 end if;
448 end if;
450 Next (Itm);
451 end loop;
453 -- If we fall through then the with clause is not present in the
454 -- current unit. One legitimate possibility is that the with clause
455 -- is present in the spec when we are a body.
457 if Is_Body_Name (Unm)
458 and then In_Withs_Of (Spec_Entity (UE))
459 then
460 Add_To_Context_And_Mark (Itm);
461 return;
462 end if;
464 -- Similarly, we may be in the spec or body of a child unit, where
465 -- the unit in question is with'ed by some ancestor of the child unit.
467 if Is_Child_Name (Unm) then
468 declare
469 Pkg : Entity_Id;
471 begin
472 Pkg := UE;
473 loop
474 Pkg := Scope (Pkg);
475 exit when Pkg = Standard_Standard;
477 if In_Withs_Of (Pkg) then
478 Add_To_Context_And_Mark (Itm);
479 return;
480 end if;
481 end loop;
482 end;
483 end if;
485 -- Here if we do not find with clause on spec or body. We just ignore
486 -- this case, it means that the elaboration involves some other unit
487 -- than the unit being compiled, and will be caught elsewhere.
489 null;
490 end Activate_Elaborate_All_Desirable;
492 ------------------
493 -- Check_A_Call --
494 ------------------
496 procedure Check_A_Call
497 (N : Node_Id;
498 E : Entity_Id;
499 Outer_Scope : Entity_Id;
500 Inter_Unit_Only : Boolean;
501 Generate_Warnings : Boolean := True;
502 In_Init_Proc : Boolean := False)
504 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
505 -- Indicates if we have Access attribute case
507 procedure Elab_Warning
508 (Msg_D : String;
509 Msg_S : String;
510 Ent : Node_Or_Entity_Id);
511 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
512 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
513 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
514 -- Msg_S is an info message (output if Elab_Info_Messages is set.
516 ------------------
517 -- Elab_Warning --
518 ------------------
520 procedure Elab_Warning
521 (Msg_D : String;
522 Msg_S : String;
523 Ent : Node_Or_Entity_Id)
525 begin
526 -- Dynamic elaboration checks, real warning
528 if Dynamic_Elaboration_Checks then
529 if not Access_Case then
530 if Msg_D /= "" and then Elab_Warnings then
531 Error_Msg_NE (Msg_D, N, Ent);
532 end if;
533 end if;
535 -- Static elaboration checks, info message
537 else
538 if Elab_Info_Messages then
539 Error_Msg_NE (Msg_S, N, Ent);
540 end if;
541 end if;
542 end Elab_Warning;
544 -- Local variables
546 Loc : constant Source_Ptr := Sloc (N);
547 Ent : Entity_Id;
548 Decl : Node_Id;
550 E_Scope : Entity_Id;
551 -- Top level scope of entity for called subprogram. This value includes
552 -- following renamings and derivations, so this scope can be in a
553 -- non-visible unit. This is the scope that is to be investigated to
554 -- see whether an elaboration check is required.
556 W_Scope : Entity_Id;
557 -- Top level scope of directly called entity for subprogram. This
558 -- differs from E_Scope in the case where renamings or derivations
559 -- are involved, since it does not follow these links. W_Scope is
560 -- generally in a visible unit, and it is this scope that may require
561 -- an Elaborate_All. However, there are some cases (initialization
562 -- calls and calls involving object notation) where W_Scope might not
563 -- be in the context of the current unit, and there is an intermediate
564 -- package that is, in which case the Elaborate_All has to be placed
565 -- on this intermediate package. These special cases are handled in
566 -- Set_Elaboration_Constraint.
568 Body_Acts_As_Spec : Boolean;
569 -- Set to true if call is to body acting as spec (no separate spec)
571 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
572 -- Indicates if we have instantiation case
574 Caller_Unit_Internal : Boolean;
575 Callee_Unit_Internal : Boolean;
577 Inst_Caller : Source_Ptr;
578 Inst_Callee : Source_Ptr;
580 Unit_Caller : Unit_Number_Type;
581 Unit_Callee : Unit_Number_Type;
583 Cunit_SC : Boolean := False;
584 -- Set to suppress dynamic elaboration checks where one of the
585 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
586 -- if a pragma Elaborate (_All) applies to that scope, in which case
587 -- warnings on the scope are also suppressed. For the internal case,
588 -- we ignore this flag.
590 -- Start of processing for Check_A_Call
592 begin
593 -- If the call is known to be within a local Suppress Elaboration
594 -- pragma, nothing to check. This can happen in task bodies. But
595 -- we ignore this for a call to a generic formal.
597 if Nkind (N) in N_Subprogram_Call
598 and then No_Elaboration_Check (N)
599 and then not Is_Call_Of_Generic_Formal (N)
600 then
601 return;
602 end if;
604 -- Go to parent for derived subprogram, or to original subprogram in the
605 -- case of a renaming (Alias covers both these cases).
607 Ent := E;
608 loop
609 if (Suppress_Elaboration_Warnings (Ent)
610 or else Elaboration_Checks_Suppressed (Ent))
611 and then (Inst_Case or else No (Alias (Ent)))
612 then
613 return;
614 end if;
616 -- Nothing to do for imported entities
618 if Is_Imported (Ent) then
619 return;
620 end if;
622 exit when Inst_Case or else No (Alias (Ent));
623 Ent := Alias (Ent);
624 end loop;
626 Decl := Unit_Declaration_Node (Ent);
628 if Nkind (Decl) = N_Subprogram_Body then
629 Body_Acts_As_Spec := True;
631 elsif Nkind_In (Decl, N_Subprogram_Declaration, N_Subprogram_Body_Stub)
632 or else Inst_Case
633 then
634 Body_Acts_As_Spec := False;
636 -- If we have none of an instantiation, subprogram body or subprogram
637 -- declaration, then it is not a case that we want to check. (One case
638 -- is a call to a generic formal subprogram, where we do not want the
639 -- check in the template).
641 else
642 return;
643 end if;
645 E_Scope := Ent;
646 loop
647 if Elaboration_Checks_Suppressed (E_Scope)
648 or else Suppress_Elaboration_Warnings (E_Scope)
649 then
650 Cunit_SC := True;
651 end if;
653 -- Exit when we get to compilation unit, not counting subunits
655 exit when Is_Compilation_Unit (E_Scope)
656 and then (Is_Child_Unit (E_Scope)
657 or else Scope (E_Scope) = Standard_Standard);
659 -- If we did not find a compilation unit, other than standard,
660 -- then nothing to check (happens in some instantiation cases)
662 if E_Scope = Standard_Standard then
663 return;
665 -- Otherwise move up a scope looking for compilation unit
667 else
668 E_Scope := Scope (E_Scope);
669 end if;
670 end loop;
672 -- No checks needed for pure or preelaborated compilation units
674 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
675 return;
676 end if;
678 -- If the generic entity is within a deeper instance than we are, then
679 -- either the instantiation to which we refer itself caused an ABE, in
680 -- which case that will be handled separately, or else we know that the
681 -- body we need appears as needed at the point of the instantiation.
682 -- However, this assumption is only valid if we are in static mode.
684 if not Dynamic_Elaboration_Checks
685 and then
686 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
687 then
688 return;
689 end if;
691 -- Do not give a warning for a package with no body
693 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
694 return;
695 end if;
697 -- Case of entity is not in current unit (i.e. with'ed unit case)
699 if E_Scope /= C_Scope then
701 -- We are only interested in such calls if the outer call was from
702 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
704 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
705 return;
706 end if;
708 -- Nothing to do if some scope said that no checks were required
710 if Cunit_SC then
711 return;
712 end if;
714 -- Nothing to do for a generic instance, because in this case the
715 -- checking was at the point of instantiation of the generic However,
716 -- this shortcut is only applicable in static mode.
718 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
719 return;
720 end if;
722 -- Nothing to do if subprogram with no separate spec. However, a
723 -- call to Deep_Initialize may result in a call to a user-defined
724 -- Initialize procedure, which imposes a body dependency. This
725 -- happens only if the type is controlled and the Initialize
726 -- procedure is not inherited.
728 if Body_Acts_As_Spec then
729 if Is_TSS (Ent, TSS_Deep_Initialize) then
730 declare
731 Typ : constant Entity_Id := Etype (First_Formal (Ent));
732 Init : Entity_Id;
734 begin
735 if not Is_Controlled (Typ) then
736 return;
737 else
738 Init := Find_Prim_Op (Typ, Name_Initialize);
740 if Comes_From_Source (Init) then
741 Ent := Init;
742 else
743 return;
744 end if;
745 end if;
746 end;
748 else
749 return;
750 end if;
751 end if;
753 -- Check cases of internal units
755 Callee_Unit_Internal :=
756 Is_Internal_File_Name
757 (Unit_File_Name (Get_Source_Unit (E_Scope)));
759 -- Do not give a warning if the with'ed unit is internal and this is
760 -- the generic instantiation case (this saves a lot of hassle dealing
761 -- with the Text_IO special child units)
763 if Callee_Unit_Internal and Inst_Case then
764 return;
765 end if;
767 if C_Scope = Standard_Standard then
768 Caller_Unit_Internal := False;
769 else
770 Caller_Unit_Internal :=
771 Is_Internal_File_Name
772 (Unit_File_Name (Get_Source_Unit (C_Scope)));
773 end if;
775 -- Do not give a warning if the with'ed unit is internal and the
776 -- caller is not internal (since the binder always elaborates
777 -- internal units first).
779 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
780 return;
781 end if;
783 -- For now, if debug flag -gnatdE is not set, do no checking for
784 -- one internal unit withing another. This fixes the problem with
785 -- the sgi build and storage errors. To be resolved later ???
787 if (Callee_Unit_Internal and Caller_Unit_Internal)
788 and then not Debug_Flag_EE
789 then
790 return;
791 end if;
793 if Is_TSS (E, TSS_Deep_Initialize) then
794 Ent := E;
795 end if;
797 -- If the call is in an instance, and the called entity is not
798 -- defined in the same instance, then the elaboration issue focuses
799 -- around the unit containing the template, it is this unit which
800 -- requires an Elaborate_All.
802 -- However, if we are doing dynamic elaboration, we need to chase the
803 -- call in the usual manner.
805 -- We also need to chase the call in the usual manner if it is a call
806 -- to a generic formal parameter, since that case was not handled as
807 -- part of the processing of the template.
809 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
810 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
812 if Inst_Caller = No_Location then
813 Unit_Caller := No_Unit;
814 else
815 Unit_Caller := Get_Source_Unit (N);
816 end if;
818 if Inst_Callee = No_Location then
819 Unit_Callee := No_Unit;
820 else
821 Unit_Callee := Get_Source_Unit (Ent);
822 end if;
824 if Unit_Caller /= No_Unit
825 and then Unit_Callee /= Unit_Caller
826 and then not Dynamic_Elaboration_Checks
827 and then not Is_Call_Of_Generic_Formal (N)
828 then
829 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
831 -- If we don't get a spec entity, just ignore call. Not quite
832 -- clear why this check is necessary. ???
834 if No (E_Scope) then
835 return;
836 end if;
838 -- Otherwise step to enclosing compilation unit
840 while not Is_Compilation_Unit (E_Scope) loop
841 E_Scope := Scope (E_Scope);
842 end loop;
844 -- For the case where N is not an instance, and is not a call within
845 -- instance to other than a generic formal, we recompute E_Scope
846 -- for the error message, since we do NOT want to go to the unit
847 -- which has the ultimate declaration in the case of renaming and
848 -- derivation and we also want to go to the generic unit in the
849 -- case of an instance, and no further.
851 else
852 -- Loop to carefully follow renamings and derivations one step
853 -- outside the current unit, but not further.
855 if not Inst_Case and then Present (Alias (Ent)) then
856 E_Scope := Alias (Ent);
857 else
858 E_Scope := Ent;
859 end if;
861 loop
862 while not Is_Compilation_Unit (E_Scope) loop
863 E_Scope := Scope (E_Scope);
864 end loop;
866 -- If E_Scope is the same as C_Scope, it means that there
867 -- definitely was a local renaming or derivation, and we
868 -- are not yet out of the current unit.
870 exit when E_Scope /= C_Scope;
871 Ent := Alias (Ent);
872 E_Scope := Ent;
874 -- If no alias, there is a previous error
876 if No (Ent) then
877 Check_Error_Detected;
878 return;
879 end if;
880 end loop;
881 end if;
883 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
884 return;
885 end if;
887 -- Find top level scope for called entity (not following renamings
888 -- or derivations). This is where the Elaborate_All will go if it
889 -- is needed. We start with the called entity, except in the case
890 -- of an initialization procedure outside the current package, where
891 -- the init proc is in the root package, and we start from the entity
892 -- of the name in the call.
894 declare
895 Ent : constant Entity_Id := Get_Referenced_Ent (N);
896 begin
897 if Is_Init_Proc (Ent)
898 and then not In_Same_Extended_Unit (N, Ent)
899 then
900 W_Scope := Scope (Ent);
901 else
902 W_Scope := E;
903 end if;
904 end;
906 -- Now loop through scopes to get to the enclosing compilation unit
908 while not Is_Compilation_Unit (W_Scope) loop
909 W_Scope := Scope (W_Scope);
910 end loop;
912 -- Now check if an elaborate_all (or dynamic check) is needed
914 if not Suppress_Elaboration_Warnings (Ent)
915 and then not Elaboration_Checks_Suppressed (Ent)
916 and then not Suppress_Elaboration_Warnings (E_Scope)
917 and then not Elaboration_Checks_Suppressed (E_Scope)
918 and then (Elab_Warnings or Elab_Info_Messages)
919 and then Generate_Warnings
920 then
921 -- Instantiation case
923 if Inst_Case then
924 Elab_Warning
925 ("instantiation of& may raise Program_Error?l?",
926 "info: instantiation of& during elaboration?$?", Ent);
928 -- Indirect call case, info message only in static elaboration
929 -- case, because the attribute reference itself cannot raise an
930 -- exception.
932 elsif Access_Case then
933 Elab_Warning
934 ("", "info: access to& during elaboration?$?", Ent);
936 -- Subprogram call case
938 else
939 if Nkind (Name (N)) in N_Has_Entity
940 and then Is_Init_Proc (Entity (Name (N)))
941 and then Comes_From_Source (Ent)
942 then
943 Elab_Warning
944 ("implicit call to & may raise Program_Error?l?",
945 "info: implicit call to & during elaboration?$?",
946 Ent);
948 else
949 Elab_Warning
950 ("call to & may raise Program_Error?l?",
951 "info: call to & during elaboration?$?",
952 Ent);
953 end if;
954 end if;
956 Error_Msg_Qual_Level := Nat'Last;
958 if Nkind (N) in N_Subprogram_Instantiation then
959 Elab_Warning
960 ("\missing pragma Elaborate for&?l?",
961 "\implicit pragma Elaborate for& generated?$?",
962 W_Scope);
964 else
965 Elab_Warning
966 ("\missing pragma Elaborate_All for&?l?",
967 "\implicit pragma Elaborate_All for & generated?$?",
968 W_Scope);
969 end if;
971 Error_Msg_Qual_Level := 0;
973 -- Take into account the flags related to elaboration warning
974 -- messages when enumerating the various calls involved. This
975 -- ensures the proper pairing of the main warning and the
976 -- clarification messages generated by Output_Calls.
978 Output_Calls (N, Check_Elab_Flag => True);
980 -- Set flag to prevent further warnings for same unit unless in
981 -- All_Errors_Mode.
983 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
984 Set_Suppress_Elaboration_Warnings (W_Scope, True);
985 end if;
986 end if;
988 -- Check for runtime elaboration check required
990 if Dynamic_Elaboration_Checks then
991 if not Elaboration_Checks_Suppressed (Ent)
992 and then not Elaboration_Checks_Suppressed (W_Scope)
993 and then not Elaboration_Checks_Suppressed (E_Scope)
994 and then not Cunit_SC
995 then
996 -- Runtime elaboration check required. Generate check of the
997 -- elaboration Boolean for the unit containing the entity.
999 -- Note that for this case, we do check the real unit (the one
1000 -- from following renamings, since that is the issue).
1002 -- Could this possibly miss a useless but required PE???
1004 Insert_Elab_Check (N,
1005 Make_Attribute_Reference (Loc,
1006 Attribute_Name => Name_Elaborated,
1007 Prefix =>
1008 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1010 -- Prevent duplicate elaboration checks on the same call,
1011 -- which can happen if the body enclosing the call appears
1012 -- itself in a call whose elaboration check is delayed.
1014 if Nkind (N) in N_Subprogram_Call then
1015 Set_No_Elaboration_Check (N);
1016 end if;
1017 end if;
1019 -- Case of static elaboration model
1021 else
1022 -- Do not do anything if elaboration checks suppressed. Note that
1023 -- we check Ent here, not E, since we want the real entity for the
1024 -- body to see if checks are suppressed for it, not the dummy
1025 -- entry for renamings or derivations.
1027 if Elaboration_Checks_Suppressed (Ent)
1028 or else Elaboration_Checks_Suppressed (E_Scope)
1029 or else Elaboration_Checks_Suppressed (W_Scope)
1030 then
1031 null;
1033 -- Do not generate an Elaborate_All for finalization routines
1034 -- which perform partial clean up as part of initialization.
1036 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1037 null;
1039 -- Here we need to generate an implicit elaborate all
1041 else
1042 -- Generate Elaborate_all warning unless suppressed
1044 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1045 and then not Suppress_Elaboration_Warnings (Ent)
1046 and then not Suppress_Elaboration_Warnings (E_Scope)
1047 and then not Suppress_Elaboration_Warnings (W_Scope)
1048 then
1049 Error_Msg_Node_2 := W_Scope;
1050 Error_Msg_NE
1051 ("info: call to& in elaboration code " &
1052 "requires pragma Elaborate_All on&?$?", N, E);
1053 end if;
1055 -- Set indication for binder to generate Elaborate_All
1057 Set_Elaboration_Constraint (N, E, W_Scope);
1058 end if;
1059 end if;
1061 -- Case of entity is in same unit as call or instantiation
1063 elsif not Inter_Unit_Only then
1064 Check_Internal_Call (N, Ent, Outer_Scope, E);
1065 end if;
1066 end Check_A_Call;
1068 -----------------------------
1069 -- Check_Bad_Instantiation --
1070 -----------------------------
1072 procedure Check_Bad_Instantiation (N : Node_Id) is
1073 Ent : Entity_Id;
1075 begin
1076 -- Nothing to do if we do not have an instantiation (happens in some
1077 -- error cases, and also in the formal package declaration case)
1079 if Nkind (N) not in N_Generic_Instantiation then
1080 return;
1082 -- Nothing to do if serious errors detected (avoid cascaded errors)
1084 elsif Serious_Errors_Detected /= 0 then
1085 return;
1087 -- Nothing to do if not in full analysis mode
1089 elsif not Full_Analysis then
1090 return;
1092 -- Nothing to do if inside a generic template
1094 elsif Inside_A_Generic then
1095 return;
1097 -- Nothing to do if a library level instantiation
1099 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1100 return;
1102 -- Nothing to do if we are compiling a proper body for semantic
1103 -- purposes only. The generic body may be in another proper body.
1105 elsif
1106 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1107 then
1108 return;
1109 end if;
1111 Ent := Get_Generic_Entity (N);
1113 -- The case we are interested in is when the generic spec is in the
1114 -- current declarative part
1116 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1117 or else not In_Same_Extended_Unit (N, Ent)
1118 then
1119 return;
1120 end if;
1122 -- If the generic entity is within a deeper instance than we are, then
1123 -- either the instantiation to which we refer itself caused an ABE, in
1124 -- which case that will be handled separately. Otherwise, we know that
1125 -- the body we need appears as needed at the point of the instantiation.
1126 -- If they are both at the same level but not within the same instance
1127 -- then the body of the generic will be in the earlier instance.
1129 declare
1130 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1131 D2 : constant Int := Instantiation_Depth (Sloc (N));
1133 begin
1134 if D1 > D2 then
1135 return;
1137 elsif D1 = D2
1138 and then Is_Generic_Instance (Scope (Ent))
1139 and then not In_Open_Scopes (Scope (Ent))
1140 then
1141 return;
1142 end if;
1143 end;
1145 -- Now we can proceed, if the entity being called has a completion,
1146 -- then we are definitely OK, since we have already seen the body.
1148 if Has_Completion (Ent) then
1149 return;
1150 end if;
1152 -- If there is no body, then nothing to do
1154 if not Has_Generic_Body (N) then
1155 return;
1156 end if;
1158 -- Here we definitely have a bad instantiation
1160 Error_Msg_Warn := SPARK_Mode /= On;
1161 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1163 if Present (Instance_Spec (N)) then
1164 Supply_Bodies (Instance_Spec (N));
1165 end if;
1167 Error_Msg_N ("\Program_Error [<<", N);
1168 Insert_Elab_Check (N);
1169 Set_ABE_Is_Certain (N);
1170 end Check_Bad_Instantiation;
1172 ---------------------
1173 -- Check_Elab_Call --
1174 ---------------------
1176 procedure Check_Elab_Call
1177 (N : Node_Id;
1178 Outer_Scope : Entity_Id := Empty;
1179 In_Init_Proc : Boolean := False)
1181 Ent : Entity_Id;
1182 P : Node_Id;
1184 begin
1185 -- If the call does not come from the main unit, there is nothing to
1186 -- check. Elaboration call from units in the context of the main unit
1187 -- will lead to semantic dependencies when those units are compiled.
1189 if not In_Extended_Main_Code_Unit (N) then
1190 return;
1191 end if;
1193 -- For an entry call, check relevant restriction
1195 if Nkind (N) = N_Entry_Call_Statement
1196 and then not In_Subprogram_Or_Concurrent_Unit
1197 then
1198 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1200 -- Nothing to do if this is not a call or attribute reference (happens
1201 -- in some error conditions, and in some cases where rewriting occurs).
1203 elsif Nkind (N) not in N_Subprogram_Call
1204 and then Nkind (N) /= N_Attribute_Reference
1205 then
1206 return;
1208 -- Nothing to do if this is a call already rewritten for elab checking
1210 elsif Nkind (Parent (N)) = N_If_Expression then
1211 return;
1213 -- Nothing to do if inside a generic template
1215 elsif Inside_A_Generic
1216 and then No (Enclosing_Generic_Body (N))
1217 then
1218 return;
1219 end if;
1221 -- Nothing to do if this is a call to a postcondition, which is always
1222 -- within a subprogram body, even though the current scope may be the
1223 -- enclosing scope of the subprogram.
1225 if Nkind (N) = N_Procedure_Call_Statement
1226 and then Is_Entity_Name (Name (N))
1227 and then Chars (Entity (Name (N))) = Name_uPostconditions
1228 then
1229 return;
1230 end if;
1232 -- Here we have a call at elaboration time which must be checked
1234 if Debug_Flag_LL then
1235 Write_Str (" Check_Elab_Call: ");
1237 if Nkind (N) = N_Attribute_Reference then
1238 if not Is_Entity_Name (Prefix (N)) then
1239 Write_Str ("<<not entity name>>");
1240 else
1241 Write_Name (Chars (Entity (Prefix (N))));
1242 end if;
1243 Write_Str ("'Access");
1245 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1246 Write_Str ("<<not entity name>> ");
1248 else
1249 Write_Name (Chars (Entity (Name (N))));
1250 end if;
1252 Write_Str (" call at ");
1253 Write_Location (Sloc (N));
1254 Write_Eol;
1255 end if;
1257 -- Climb up the tree to make sure we are not inside default expression
1258 -- of a parameter specification or a record component, since in both
1259 -- these cases, we will be doing the actual call later, not now, and it
1260 -- is at the time of the actual call (statically speaking) that we must
1261 -- do our static check, not at the time of its initial analysis).
1263 -- However, we have to check calls within component definitions (e.g.
1264 -- a function call that determines an array component bound), so we
1265 -- terminate the loop in that case.
1267 P := Parent (N);
1268 while Present (P) loop
1269 if Nkind_In (P, N_Parameter_Specification,
1270 N_Component_Declaration)
1271 then
1272 return;
1274 -- The call occurs within the constraint of a component,
1275 -- so it must be checked.
1277 elsif Nkind (P) = N_Component_Definition then
1278 exit;
1280 else
1281 P := Parent (P);
1282 end if;
1283 end loop;
1285 -- Stuff that happens only at the outer level
1287 if No (Outer_Scope) then
1288 Elab_Visited.Set_Last (0);
1290 -- Nothing to do if current scope is Standard (this is a bit odd, but
1291 -- it happens in the case of generic instantiations).
1293 C_Scope := Current_Scope;
1295 if C_Scope = Standard_Standard then
1296 return;
1297 end if;
1299 -- First case, we are in elaboration code
1301 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1303 if From_Elab_Code then
1305 -- Complain if call that comes from source in preelaborated unit
1306 -- and we are not inside a subprogram (i.e. we are in elab code).
1308 if Comes_From_Source (N)
1309 and then In_Preelaborated_Unit
1310 and then not In_Inlined_Body
1311 and then Nkind (N) /= N_Attribute_Reference
1312 then
1313 -- This is a warning in GNAT mode allowing such calls to be
1314 -- used in the predefined library with appropriate care.
1316 Error_Msg_Warn := GNAT_Mode;
1317 Error_Msg_N
1318 ("<<non-static call not allowed in preelaborated unit", N);
1319 return;
1320 end if;
1322 -- Second case, we are inside a subprogram or concurrent unit, which
1323 -- means we are not in elaboration code.
1325 else
1326 -- In this case, the issue is whether we are inside the
1327 -- declarative part of the unit in which we live, or inside its
1328 -- statements. In the latter case, there is no issue of ABE calls
1329 -- at this level (a call from outside to the unit in which we live
1330 -- might cause an ABE, but that will be detected when we analyze
1331 -- that outer level call, as it recurses into the called unit).
1333 -- Climb up the tree, doing this test, and also testing for being
1334 -- inside a default expression, which, as discussed above, is not
1335 -- checked at this stage.
1337 declare
1338 P : Node_Id;
1339 L : List_Id;
1341 begin
1342 P := N;
1343 loop
1344 -- If we find a parentless subtree, it seems safe to assume
1345 -- that we are not in a declarative part and that no
1346 -- checking is required.
1348 if No (P) then
1349 return;
1350 end if;
1352 if Is_List_Member (P) then
1353 L := List_Containing (P);
1354 P := Parent (L);
1355 else
1356 L := No_List;
1357 P := Parent (P);
1358 end if;
1360 exit when Nkind (P) = N_Subunit;
1362 -- Filter out case of default expressions, where we do not
1363 -- do the check at this stage.
1365 if Nkind_In (P, N_Parameter_Specification,
1366 N_Component_Declaration)
1367 then
1368 return;
1369 end if;
1371 -- A protected body has no elaboration code and contains
1372 -- only other bodies.
1374 if Nkind (P) = N_Protected_Body then
1375 return;
1377 elsif Nkind_In (P, N_Subprogram_Body,
1378 N_Task_Body,
1379 N_Block_Statement,
1380 N_Entry_Body)
1381 then
1382 if L = Declarations (P) then
1383 exit;
1385 -- We are not in elaboration code, but we are doing
1386 -- dynamic elaboration checks, in this case, we still
1387 -- need to do the call, since the subprogram we are in
1388 -- could be called from another unit, also in dynamic
1389 -- elaboration check mode, at elaboration time.
1391 elsif Dynamic_Elaboration_Checks then
1393 -- We provide a debug flag to disable this check. That
1394 -- way we have an easy work around for regressions
1395 -- that are caused by this new check. This debug flag
1396 -- can be removed later.
1398 if Debug_Flag_DD then
1399 return;
1400 end if;
1402 -- Do the check in this case
1404 exit;
1406 elsif Nkind (P) = N_Task_Body then
1408 -- The check is deferred until Check_Task_Activation
1409 -- but we need to capture local suppress pragmas
1410 -- that may inhibit checks on this call.
1412 Ent := Get_Referenced_Ent (N);
1414 if No (Ent) then
1415 return;
1417 elsif Elaboration_Checks_Suppressed (Current_Scope)
1418 or else Elaboration_Checks_Suppressed (Ent)
1419 or else Elaboration_Checks_Suppressed (Scope (Ent))
1420 then
1421 Set_No_Elaboration_Check (N);
1422 end if;
1424 return;
1426 -- Static model, call is not in elaboration code, we
1427 -- never need to worry, because in the static model the
1428 -- top level caller always takes care of things.
1430 else
1431 return;
1432 end if;
1433 end if;
1434 end loop;
1435 end;
1436 end if;
1437 end if;
1439 Ent := Get_Referenced_Ent (N);
1441 if No (Ent) then
1442 return;
1443 end if;
1445 -- Nothing to do if this is a recursive call (i.e. a call to
1446 -- an entity that is already in the Elab_Call stack)
1448 for J in 1 .. Elab_Visited.Last loop
1449 if Ent = Elab_Visited.Table (J) then
1450 return;
1451 end if;
1452 end loop;
1454 -- See if we need to analyze this call. We analyze it if either of
1455 -- the following conditions is met:
1457 -- It is an inner level call (since in this case it was triggered
1458 -- by an outer level call from elaboration code), but only if the
1459 -- call is within the scope of the original outer level call.
1461 -- It is an outer level call from elaboration code, or the called
1462 -- entity is in the same elaboration scope.
1464 -- And in these cases, we will check both inter-unit calls and
1465 -- intra-unit (within a single unit) calls.
1467 C_Scope := Current_Scope;
1469 -- If not outer level call, then we follow it if it is within the
1470 -- original scope of the outer call.
1472 if Present (Outer_Scope)
1473 and then Within (Scope (Ent), Outer_Scope)
1474 then
1475 Set_C_Scope;
1476 Check_A_Call
1477 (N => N,
1478 E => Ent,
1479 Outer_Scope => Outer_Scope,
1480 Inter_Unit_Only => False,
1481 In_Init_Proc => In_Init_Proc);
1483 -- Nothing to do if elaboration checks suppressed for this scope.
1484 -- However, an interesting exception, the fact that elaboration checks
1485 -- are suppressed within an instance (because we can trace the body when
1486 -- we process the template) does not extend to calls to generic formal
1487 -- subprograms.
1489 elsif Elaboration_Checks_Suppressed (Current_Scope)
1490 and then not Is_Call_Of_Generic_Formal (N)
1491 then
1492 null;
1494 elsif From_Elab_Code then
1495 Set_C_Scope;
1496 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1498 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1499 Set_C_Scope;
1500 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1502 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1503 -- is set, then we will do the check, but only in the inter-unit case
1504 -- (this is to accommodate unguarded elaboration calls from other units
1505 -- in which this same mode is set). We don't want warnings in this case,
1506 -- it would generate warnings having nothing to do with elaboration.
1508 elsif Dynamic_Elaboration_Checks then
1509 Set_C_Scope;
1510 Check_A_Call
1512 Ent,
1513 Standard_Standard,
1514 Inter_Unit_Only => True,
1515 Generate_Warnings => False);
1517 -- Otherwise nothing to do
1519 else
1520 return;
1521 end if;
1523 -- A call to an Init_Proc in elaboration code may bring additional
1524 -- dependencies, if some of the record components thereof have
1525 -- initializations that are function calls that come from source. We
1526 -- treat the current node as a call to each of these functions, to check
1527 -- their elaboration impact.
1529 if Is_Init_Proc (Ent) and then From_Elab_Code then
1530 Process_Init_Proc : declare
1531 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1533 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1534 -- Find subprogram calls within body of Init_Proc for Traverse
1535 -- instantiation below.
1537 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1538 -- Traversal procedure to find all calls with body of Init_Proc
1540 ---------------------
1541 -- Check_Init_Call --
1542 ---------------------
1544 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1545 Func : Entity_Id;
1547 begin
1548 if Nkind (Nod) in N_Subprogram_Call
1549 and then Is_Entity_Name (Name (Nod))
1550 then
1551 Func := Entity (Name (Nod));
1553 if Comes_From_Source (Func) then
1554 Check_A_Call
1555 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1556 end if;
1558 return OK;
1560 else
1561 return OK;
1562 end if;
1563 end Check_Init_Call;
1565 -- Start of processing for Process_Init_Proc
1567 begin
1568 if Nkind (Unit_Decl) = N_Subprogram_Body then
1569 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1570 end if;
1571 end Process_Init_Proc;
1572 end if;
1573 end Check_Elab_Call;
1575 -----------------------
1576 -- Check_Elab_Assign --
1577 -----------------------
1579 procedure Check_Elab_Assign (N : Node_Id) is
1580 Ent : Entity_Id;
1581 Scop : Entity_Id;
1583 Pkg_Spec : Entity_Id;
1584 Pkg_Body : Entity_Id;
1586 begin
1587 -- For record or array component, check prefix. If it is an access type,
1588 -- then there is nothing to do (we do not know what is being assigned),
1589 -- but otherwise this is an assignment to the prefix.
1591 if Nkind_In (N, N_Indexed_Component,
1592 N_Selected_Component,
1593 N_Slice)
1594 then
1595 if not Is_Access_Type (Etype (Prefix (N))) then
1596 Check_Elab_Assign (Prefix (N));
1597 end if;
1599 return;
1600 end if;
1602 -- For type conversion, check expression
1604 if Nkind (N) = N_Type_Conversion then
1605 Check_Elab_Assign (Expression (N));
1606 return;
1607 end if;
1609 -- Nothing to do if this is not an entity reference otherwise get entity
1611 if Is_Entity_Name (N) then
1612 Ent := Entity (N);
1613 else
1614 return;
1615 end if;
1617 -- What we are looking for is a reference in the body of a package that
1618 -- modifies a variable declared in the visible part of the package spec.
1620 if Present (Ent)
1621 and then Comes_From_Source (N)
1622 and then not Suppress_Elaboration_Warnings (Ent)
1623 and then Ekind (Ent) = E_Variable
1624 and then not In_Private_Part (Ent)
1625 and then Is_Library_Level_Entity (Ent)
1626 then
1627 Scop := Current_Scope;
1628 loop
1629 if No (Scop) or else Scop = Standard_Standard then
1630 return;
1631 elsif Ekind (Scop) = E_Package
1632 and then Is_Compilation_Unit (Scop)
1633 then
1634 exit;
1635 else
1636 Scop := Scope (Scop);
1637 end if;
1638 end loop;
1640 -- Here Scop points to the containing library package
1642 Pkg_Spec := Scop;
1643 Pkg_Body := Body_Entity (Pkg_Spec);
1645 -- All OK if the package has an Elaborate_Body pragma
1647 if Has_Pragma_Elaborate_Body (Scop) then
1648 return;
1649 end if;
1651 -- OK if entity being modified is not in containing package spec
1653 if not In_Same_Source_Unit (Scop, Ent) then
1654 return;
1655 end if;
1657 -- All OK if entity appears in generic package or generic instance.
1658 -- We just get too messed up trying to give proper warnings in the
1659 -- presence of generics. Better no message than a junk one.
1661 Scop := Scope (Ent);
1662 while Present (Scop) and then Scop /= Pkg_Spec loop
1663 if Ekind (Scop) = E_Generic_Package then
1664 return;
1665 elsif Ekind (Scop) = E_Package
1666 and then Is_Generic_Instance (Scop)
1667 then
1668 return;
1669 end if;
1671 Scop := Scope (Scop);
1672 end loop;
1674 -- All OK if in task, don't issue warnings there
1676 if In_Task_Activation then
1677 return;
1678 end if;
1680 -- OK if no package body
1682 if No (Pkg_Body) then
1683 return;
1684 end if;
1686 -- OK if reference is not in package body
1688 if not In_Same_Source_Unit (Pkg_Body, N) then
1689 return;
1690 end if;
1692 -- OK if package body has no handled statement sequence
1694 declare
1695 HSS : constant Node_Id :=
1696 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1697 begin
1698 if No (HSS) or else not Comes_From_Source (HSS) then
1699 return;
1700 end if;
1701 end;
1703 -- We definitely have a case of a modification of an entity in
1704 -- the package spec from the elaboration code of the package body.
1705 -- We may not give the warning (because there are some additional
1706 -- checks to avoid too many false positives), but it would be a good
1707 -- idea for the binder to try to keep the body elaboration close to
1708 -- the spec elaboration.
1710 Set_Elaborate_Body_Desirable (Pkg_Spec);
1712 -- All OK in gnat mode (we know what we are doing)
1714 if GNAT_Mode then
1715 return;
1716 end if;
1718 -- All OK if all warnings suppressed
1720 if Warning_Mode = Suppress then
1721 return;
1722 end if;
1724 -- All OK if elaboration checks suppressed for entity
1726 if Checks_May_Be_Suppressed (Ent)
1727 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1728 then
1729 return;
1730 end if;
1732 -- OK if the entity is initialized. Note that the No_Initialization
1733 -- flag usually means that the initialization has been rewritten into
1734 -- assignments, but that still counts for us.
1736 declare
1737 Decl : constant Node_Id := Declaration_Node (Ent);
1738 begin
1739 if Nkind (Decl) = N_Object_Declaration
1740 and then (Present (Expression (Decl))
1741 or else No_Initialization (Decl))
1742 then
1743 return;
1744 end if;
1745 end;
1747 -- Here is where we give the warning
1749 -- All OK if warnings suppressed on the entity
1751 if not Has_Warnings_Off (Ent) then
1752 Error_Msg_Sloc := Sloc (Ent);
1754 Error_Msg_NE
1755 ("??& can be accessed by clients before this initialization",
1756 N, Ent);
1757 Error_Msg_NE
1758 ("\??add Elaborate_Body to spec to ensure & is initialized",
1759 N, Ent);
1760 end if;
1762 if not All_Errors_Mode then
1763 Set_Suppress_Elaboration_Warnings (Ent);
1764 end if;
1765 end if;
1766 end Check_Elab_Assign;
1768 ----------------------
1769 -- Check_Elab_Calls --
1770 ----------------------
1772 procedure Check_Elab_Calls is
1773 begin
1774 -- If expansion is disabled, do not generate any checks. Also skip
1775 -- checks if any subunits are missing because in either case we lack the
1776 -- full information that we need, and no object file will be created in
1777 -- any case.
1779 if not Expander_Active
1780 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1781 or else Subunits_Missing
1782 then
1783 return;
1784 end if;
1786 -- Skip delayed calls if we had any errors
1788 if Serious_Errors_Detected = 0 then
1789 Delaying_Elab_Checks := False;
1790 Expander_Mode_Save_And_Set (True);
1792 for J in Delay_Check.First .. Delay_Check.Last loop
1793 Push_Scope (Delay_Check.Table (J).Curscop);
1794 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1796 Check_Internal_Call_Continue (
1797 N => Delay_Check.Table (J).N,
1798 E => Delay_Check.Table (J).E,
1799 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1800 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1802 Pop_Scope;
1803 end loop;
1805 -- Set Delaying_Elab_Checks back on for next main compilation
1807 Expander_Mode_Restore;
1808 Delaying_Elab_Checks := True;
1809 end if;
1810 end Check_Elab_Calls;
1812 ------------------------------
1813 -- Check_Elab_Instantiation --
1814 ------------------------------
1816 procedure Check_Elab_Instantiation
1817 (N : Node_Id;
1818 Outer_Scope : Entity_Id := Empty)
1820 Ent : Entity_Id;
1822 begin
1823 -- Check for and deal with bad instantiation case. There is some
1824 -- duplicated code here, but we will worry about this later ???
1826 Check_Bad_Instantiation (N);
1828 if ABE_Is_Certain (N) then
1829 return;
1830 end if;
1832 -- Nothing to do if we do not have an instantiation (happens in some
1833 -- error cases, and also in the formal package declaration case)
1835 if Nkind (N) not in N_Generic_Instantiation then
1836 return;
1837 end if;
1839 -- Nothing to do if inside a generic template
1841 if Inside_A_Generic then
1842 return;
1843 end if;
1845 -- Nothing to do if the instantiation is not in the main unit
1847 if not In_Extended_Main_Code_Unit (N) then
1848 return;
1849 end if;
1851 Ent := Get_Generic_Entity (N);
1852 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1854 -- See if we need to analyze this instantiation. We analyze it if
1855 -- either of the following conditions is met:
1857 -- It is an inner level instantiation (since in this case it was
1858 -- triggered by an outer level call from elaboration code), but
1859 -- only if the instantiation is within the scope of the original
1860 -- outer level call.
1862 -- It is an outer level instantiation from elaboration code, or the
1863 -- instantiated entity is in the same elaboration scope.
1865 -- And in these cases, we will check both the inter-unit case and
1866 -- the intra-unit (within a single unit) case.
1868 C_Scope := Current_Scope;
1870 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
1871 Set_C_Scope;
1872 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1874 elsif From_Elab_Code then
1875 Set_C_Scope;
1876 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1878 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1879 Set_C_Scope;
1880 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1882 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
1883 -- set, then we will do the check, but only in the inter-unit case (this
1884 -- is to accommodate unguarded elaboration calls from other units in
1885 -- which this same mode is set). We inhibit warnings in this case, since
1886 -- this instantiation is not occurring in elaboration code.
1888 elsif Dynamic_Elaboration_Checks then
1889 Set_C_Scope;
1890 Check_A_Call
1892 Ent,
1893 Standard_Standard,
1894 Inter_Unit_Only => True,
1895 Generate_Warnings => False);
1897 else
1898 return;
1899 end if;
1900 end Check_Elab_Instantiation;
1902 -------------------------
1903 -- Check_Internal_Call --
1904 -------------------------
1906 procedure Check_Internal_Call
1907 (N : Node_Id;
1908 E : Entity_Id;
1909 Outer_Scope : Entity_Id;
1910 Orig_Ent : Entity_Id)
1912 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1914 begin
1915 -- If not function or procedure call or instantiation, then ignore
1916 -- call (this happens in some error cases and rewriting cases).
1918 if not Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
1919 and then not Inst_Case
1920 then
1921 return;
1923 -- Nothing to do if this is a call or instantiation that has already
1924 -- been found to be a sure ABE.
1926 elsif ABE_Is_Certain (N) then
1927 return;
1929 -- Nothing to do if errors already detected (avoid cascaded errors)
1931 elsif Serious_Errors_Detected /= 0 then
1932 return;
1934 -- Nothing to do if not in full analysis mode
1936 elsif not Full_Analysis then
1937 return;
1939 -- Nothing to do if analyzing in special spec-expression mode, since the
1940 -- call is not actually being made at this time.
1942 elsif In_Spec_Expression then
1943 return;
1945 -- Nothing to do for call to intrinsic subprogram
1947 elsif Is_Intrinsic_Subprogram (E) then
1948 return;
1950 -- No need to trace local calls if checking task activation, because
1951 -- other local bodies are elaborated already.
1953 elsif In_Task_Activation then
1954 return;
1956 -- Nothing to do if call is within a generic unit
1958 elsif Inside_A_Generic then
1959 return;
1960 end if;
1962 -- Delay this call if we are still delaying calls
1964 if Delaying_Elab_Checks then
1965 Delay_Check.Append (
1966 (N => N,
1967 E => E,
1968 Orig_Ent => Orig_Ent,
1969 Curscop => Current_Scope,
1970 Outer_Scope => Outer_Scope,
1971 From_Elab_Code => From_Elab_Code));
1972 return;
1974 -- Otherwise, call phase 2 continuation right now
1976 else
1977 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1978 end if;
1979 end Check_Internal_Call;
1981 ----------------------------------
1982 -- Check_Internal_Call_Continue --
1983 ----------------------------------
1985 procedure Check_Internal_Call_Continue
1986 (N : Node_Id;
1987 E : Entity_Id;
1988 Outer_Scope : Entity_Id;
1989 Orig_Ent : Entity_Id)
1991 Loc : constant Source_Ptr := Sloc (N);
1992 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1994 Sbody : Node_Id;
1995 Ebody : Entity_Id;
1997 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
1998 -- Function applied to each node as we traverse the body. Checks for
1999 -- call or entity reference that needs checking, and if so checks it.
2000 -- Always returns OK, so entire tree is traversed, except that as
2001 -- described below subprogram bodies are skipped for now.
2003 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2004 -- Traverse procedure using above Find_Elab_Reference function
2006 -------------------------
2007 -- Find_Elab_Reference --
2008 -------------------------
2010 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2011 Actual : Node_Id;
2013 begin
2014 -- If user has specified that there are no entry calls in elaboration
2015 -- code, do not trace past an accept statement, because the rendez-
2016 -- vous will happen after elaboration.
2018 if Nkind_In (Original_Node (N), N_Accept_Statement,
2019 N_Selective_Accept)
2020 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2021 then
2022 return Abandon;
2024 -- If we have a function call, check it
2026 elsif Nkind (N) = N_Function_Call then
2027 Check_Elab_Call (N, Outer_Scope);
2028 return OK;
2030 -- If we have a procedure call, check the call, and also check
2031 -- arguments that are assignments (OUT or IN OUT mode formals).
2033 elsif Nkind (N) = N_Procedure_Call_Statement then
2034 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2036 Actual := First_Actual (N);
2037 while Present (Actual) loop
2038 if Known_To_Be_Assigned (Actual) then
2039 Check_Elab_Assign (Actual);
2040 end if;
2042 Next_Actual (Actual);
2043 end loop;
2045 return OK;
2047 -- If we have an access attribute for a subprogram, check it.
2048 -- Suppress this behavior under debug flag.
2050 elsif not Debug_Flag_Dot_UU
2051 and then Nkind (N) = N_Attribute_Reference
2052 and then Nam_In (Attribute_Name (N), Name_Access,
2053 Name_Unrestricted_Access)
2054 and then Is_Entity_Name (Prefix (N))
2055 and then Is_Subprogram (Entity (Prefix (N)))
2056 then
2057 Check_Elab_Call (N, Outer_Scope);
2058 return OK;
2060 -- If we have a generic instantiation, check it
2062 elsif Nkind (N) in N_Generic_Instantiation then
2063 Check_Elab_Instantiation (N, Outer_Scope);
2064 return OK;
2066 -- Skip subprogram bodies that come from source (wait for call to
2067 -- analyze these). The reason for the come from source test is to
2068 -- avoid catching task bodies.
2070 -- For task bodies, we should really avoid these too, waiting for the
2071 -- task activation, but that's too much trouble to catch for now, so
2072 -- we go in unconditionally. This is not so terrible, it means the
2073 -- error backtrace is not quite complete, and we are too eager to
2074 -- scan bodies of tasks that are unused, but this is hardly very
2075 -- significant.
2077 elsif Nkind (N) = N_Subprogram_Body
2078 and then Comes_From_Source (N)
2079 then
2080 return Skip;
2082 elsif Nkind (N) = N_Assignment_Statement
2083 and then Comes_From_Source (N)
2084 then
2085 Check_Elab_Assign (Name (N));
2086 return OK;
2088 else
2089 return OK;
2090 end if;
2091 end Find_Elab_Reference;
2093 -- Start of processing for Check_Internal_Call_Continue
2095 begin
2096 -- Save outer level call if at outer level
2098 if Elab_Call.Last = 0 then
2099 Outer_Level_Sloc := Loc;
2100 end if;
2102 Elab_Visited.Append (E);
2104 -- If the call is to a function that renames a literal, no check needed
2106 if Ekind (E) = E_Enumeration_Literal then
2107 return;
2108 end if;
2110 Sbody := Unit_Declaration_Node (E);
2112 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2113 Ebody := Corresponding_Body (Sbody);
2115 if No (Ebody) then
2116 return;
2117 else
2118 Sbody := Unit_Declaration_Node (Ebody);
2119 end if;
2120 end if;
2122 -- If the body appears after the outer level call or instantiation then
2123 -- we have an error case handled below.
2125 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2126 and then not In_Task_Activation
2127 then
2128 null;
2130 -- If we have the instantiation case we are done, since we now
2131 -- know that the body of the generic appeared earlier.
2133 elsif Inst_Case then
2134 return;
2136 -- Otherwise we have a call, so we trace through the called body to see
2137 -- if it has any problems.
2139 else
2140 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2142 Elab_Call.Append ((Cloc => Loc, Ent => E));
2144 if Debug_Flag_LL then
2145 Write_Str ("Elab_Call.Last = ");
2146 Write_Int (Int (Elab_Call.Last));
2147 Write_Str (" Ent = ");
2148 Write_Name (Chars (E));
2149 Write_Str (" at ");
2150 Write_Location (Sloc (N));
2151 Write_Eol;
2152 end if;
2154 -- Now traverse declarations and statements of subprogram body. Note
2155 -- that we cannot simply Traverse (Sbody), since traverse does not
2156 -- normally visit subprogram bodies.
2158 declare
2159 Decl : Node_Id;
2160 begin
2161 Decl := First (Declarations (Sbody));
2162 while Present (Decl) loop
2163 Traverse (Decl);
2164 Next (Decl);
2165 end loop;
2166 end;
2168 Traverse (Handled_Statement_Sequence (Sbody));
2170 Elab_Call.Decrement_Last;
2171 return;
2172 end if;
2174 -- Here is the case of calling a subprogram where the body has not yet
2175 -- been encountered. A warning message is needed, except if this is the
2176 -- case of appearing within an aspect specification that results in
2177 -- a check call, we do not really have such a situation, so no warning
2178 -- is needed (e.g. the case of a precondition, where the call appears
2179 -- textually before the body, but in actual fact is moved to the
2180 -- appropriate subprogram body and so does not need a check).
2182 declare
2183 P : Node_Id;
2184 O : Node_Id;
2186 begin
2187 P := Parent (N);
2188 loop
2189 -- Keep looking at parents if we are still in the subexpression
2191 if Nkind (P) in N_Subexpr then
2192 P := Parent (P);
2194 -- Here P is the parent of the expression, check for special case
2196 else
2197 O := Original_Node (P);
2199 -- Definitely not the special case if orig node is not a pragma
2201 exit when Nkind (O) /= N_Pragma;
2203 -- Check we have an If statement or a null statement (happens
2204 -- when the If has been expanded to be True).
2206 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2208 -- Our special case will be indicated either by the pragma
2209 -- coming from an aspect ...
2211 if Present (Corresponding_Aspect (O)) then
2212 return;
2214 -- Or, in the case of an initial condition, specifically by a
2215 -- Check pragma specifying an Initial_Condition check.
2217 elsif Pragma_Name (O) = Name_Check
2218 and then
2219 Chars
2220 (Expression (First (Pragma_Argument_Associations (O)))) =
2221 Name_Initial_Condition
2222 then
2223 return;
2225 -- For anything else, we have an error
2227 else
2228 exit;
2229 end if;
2230 end if;
2231 end loop;
2232 end;
2234 -- Not that special case, warning and dynamic check is required
2236 -- If we have nothing in the call stack, then this is at the outer
2237 -- level, and the ABE is bound to occur.
2239 if Elab_Call.Last = 0 then
2240 Error_Msg_Warn := SPARK_Mode /= On;
2242 if Inst_Case then
2243 Error_Msg_NE
2244 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2245 else
2246 Error_Msg_NE
2247 ("cannot call& before body seen<<", N, Orig_Ent);
2248 end if;
2250 Error_Msg_N ("\Program_Error [<<", N);
2251 Insert_Elab_Check (N);
2253 -- Call is not at outer level
2255 else
2256 -- Deal with dynamic elaboration check
2258 if not Elaboration_Checks_Suppressed (E) then
2259 Set_Elaboration_Entity_Required (E);
2261 -- Case of no elaboration entity allocated yet
2263 if No (Elaboration_Entity (E)) then
2265 -- Create object declaration for elaboration entity, and put it
2266 -- just in front of the spec of the subprogram or generic unit,
2267 -- in the same scope as this unit. The subprogram may be over-
2268 -- loaded, so make the name of elaboration entity unique by
2269 -- means of a numeric suffix.
2271 declare
2272 Loce : constant Source_Ptr := Sloc (E);
2273 Ent : constant Entity_Id :=
2274 Make_Defining_Identifier (Loc,
2275 Chars => New_External_Name (Chars (E), 'E', -1));
2277 begin
2278 Set_Elaboration_Entity (E, Ent);
2279 Push_Scope (Scope (E));
2281 Insert_Action (Declaration_Node (E),
2282 Make_Object_Declaration (Loce,
2283 Defining_Identifier => Ent,
2284 Object_Definition =>
2285 New_Occurrence_Of (Standard_Short_Integer, Loce),
2286 Expression =>
2287 Make_Integer_Literal (Loc, Uint_0)));
2289 -- Set elaboration flag at the point of the body
2291 Set_Elaboration_Flag (Sbody, E);
2293 -- Kill current value indication. This is necessary because
2294 -- the tests of this flag are inserted out of sequence and
2295 -- must not pick up bogus indications of the wrong constant
2296 -- value. Also, this is never a true constant, since one way
2297 -- or another, it gets reset.
2299 Set_Current_Value (Ent, Empty);
2300 Set_Last_Assignment (Ent, Empty);
2301 Set_Is_True_Constant (Ent, False);
2302 Pop_Scope;
2303 end;
2304 end if;
2306 -- Generate check of the elaboration counter
2308 Insert_Elab_Check (N,
2309 Make_Attribute_Reference (Loc,
2310 Attribute_Name => Name_Elaborated,
2311 Prefix => New_Occurrence_Of (E, Loc)));
2312 end if;
2314 -- Generate the warning
2316 if not Suppress_Elaboration_Warnings (E)
2317 and then not Elaboration_Checks_Suppressed (E)
2319 -- Suppress this warning if we have a function call that occurred
2320 -- within an assertion expression, since we can get false warnings
2321 -- in this case, due to the out of order handling in this case.
2323 and then
2324 (Nkind (Original_Node (N)) /= N_Function_Call
2325 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2326 then
2327 Error_Msg_Warn := SPARK_Mode /= On;
2329 if Inst_Case then
2330 Error_Msg_NE
2331 ("instantiation of& may occur before body is seen<l<",
2332 N, Orig_Ent);
2333 else
2334 Error_Msg_NE
2335 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2336 end if;
2338 Error_Msg_N ("\Program_Error ]<l<", N);
2340 -- There is no need to query the elaboration warning message flags
2341 -- because the main message is an error, not a warning, therefore
2342 -- all the clarification messages produces by Output_Calls must be
2343 -- emitted unconditionally.
2345 Output_Calls (N, Check_Elab_Flag => False);
2346 end if;
2347 end if;
2349 -- Set flag to suppress further warnings on same subprogram
2350 -- unless in all errors mode
2352 if not All_Errors_Mode then
2353 Set_Suppress_Elaboration_Warnings (E);
2354 end if;
2355 end Check_Internal_Call_Continue;
2357 ---------------------------
2358 -- Check_Task_Activation --
2359 ---------------------------
2361 procedure Check_Task_Activation (N : Node_Id) is
2362 Loc : constant Source_Ptr := Sloc (N);
2363 Inter_Procs : constant Elist_Id := New_Elmt_List;
2364 Intra_Procs : constant Elist_Id := New_Elmt_List;
2365 Ent : Entity_Id;
2366 P : Entity_Id;
2367 Task_Scope : Entity_Id;
2368 Cunit_SC : Boolean := False;
2369 Decl : Node_Id;
2370 Elmt : Elmt_Id;
2371 Enclosing : Entity_Id;
2373 procedure Add_Task_Proc (Typ : Entity_Id);
2374 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2375 -- For record types, this procedure recurses over component types.
2377 procedure Collect_Tasks (Decls : List_Id);
2378 -- Collect the types of the tasks that are to be activated in the given
2379 -- list of declarations, in order to perform elaboration checks on the
2380 -- corresponding task procedures which are called implicitly here.
2382 function Outer_Unit (E : Entity_Id) return Entity_Id;
2383 -- find enclosing compilation unit of Entity, ignoring subunits, or
2384 -- else enclosing subprogram. If E is not a package, there is no need
2385 -- for inter-unit elaboration checks.
2387 -------------------
2388 -- Add_Task_Proc --
2389 -------------------
2391 procedure Add_Task_Proc (Typ : Entity_Id) is
2392 Comp : Entity_Id;
2393 Proc : Entity_Id := Empty;
2395 begin
2396 if Is_Task_Type (Typ) then
2397 Proc := Get_Task_Body_Procedure (Typ);
2399 elsif Is_Array_Type (Typ)
2400 and then Has_Task (Base_Type (Typ))
2401 then
2402 Add_Task_Proc (Component_Type (Typ));
2404 elsif Is_Record_Type (Typ)
2405 and then Has_Task (Base_Type (Typ))
2406 then
2407 Comp := First_Component (Typ);
2408 while Present (Comp) loop
2409 Add_Task_Proc (Etype (Comp));
2410 Comp := Next_Component (Comp);
2411 end loop;
2412 end if;
2414 -- If the task type is another unit, we will perform the usual
2415 -- elaboration check on its enclosing unit. If the type is in the
2416 -- same unit, we can trace the task body as for an internal call,
2417 -- but we only need to examine other external calls, because at
2418 -- the point the task is activated, internal subprogram bodies
2419 -- will have been elaborated already. We keep separate lists for
2420 -- each kind of task.
2422 -- Skip this test if errors have occurred, since in this case
2423 -- we can get false indications.
2425 if Serious_Errors_Detected /= 0 then
2426 return;
2427 end if;
2429 if Present (Proc) then
2430 if Outer_Unit (Scope (Proc)) = Enclosing then
2432 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2433 and then
2434 (not Is_Generic_Instance (Scope (Proc))
2435 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2436 then
2437 Error_Msg_Warn := SPARK_Mode /= On;
2438 Error_Msg_N
2439 ("task will be activated before elaboration of its body<<",
2440 Decl);
2441 Error_Msg_N ("\Program_Error [<<", Decl);
2443 elsif Present
2444 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2445 then
2446 Append_Elmt (Proc, Intra_Procs);
2447 end if;
2449 else
2450 -- No need for multiple entries of the same type
2452 Elmt := First_Elmt (Inter_Procs);
2453 while Present (Elmt) loop
2454 if Node (Elmt) = Proc then
2455 return;
2456 end if;
2458 Next_Elmt (Elmt);
2459 end loop;
2461 Append_Elmt (Proc, Inter_Procs);
2462 end if;
2463 end if;
2464 end Add_Task_Proc;
2466 -------------------
2467 -- Collect_Tasks --
2468 -------------------
2470 procedure Collect_Tasks (Decls : List_Id) is
2471 begin
2472 if Present (Decls) then
2473 Decl := First (Decls);
2474 while Present (Decl) loop
2475 if Nkind (Decl) = N_Object_Declaration
2476 and then Has_Task (Etype (Defining_Identifier (Decl)))
2477 then
2478 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2479 end if;
2481 Next (Decl);
2482 end loop;
2483 end if;
2484 end Collect_Tasks;
2486 ----------------
2487 -- Outer_Unit --
2488 ----------------
2490 function Outer_Unit (E : Entity_Id) return Entity_Id is
2491 Outer : Entity_Id;
2493 begin
2494 Outer := E;
2495 while Present (Outer) loop
2496 if Elaboration_Checks_Suppressed (Outer) then
2497 Cunit_SC := True;
2498 end if;
2500 exit when Is_Child_Unit (Outer)
2501 or else Scope (Outer) = Standard_Standard
2502 or else Ekind (Outer) /= E_Package;
2503 Outer := Scope (Outer);
2504 end loop;
2506 return Outer;
2507 end Outer_Unit;
2509 -- Start of processing for Check_Task_Activation
2511 begin
2512 Enclosing := Outer_Unit (Current_Scope);
2514 -- Find all tasks declared in the current unit
2516 if Nkind (N) = N_Package_Body then
2517 P := Unit_Declaration_Node (Corresponding_Spec (N));
2519 Collect_Tasks (Declarations (N));
2520 Collect_Tasks (Visible_Declarations (Specification (P)));
2521 Collect_Tasks (Private_Declarations (Specification (P)));
2523 elsif Nkind (N) = N_Package_Declaration then
2524 Collect_Tasks (Visible_Declarations (Specification (N)));
2525 Collect_Tasks (Private_Declarations (Specification (N)));
2527 else
2528 Collect_Tasks (Declarations (N));
2529 end if;
2531 -- We only perform detailed checks in all tasks that are library level
2532 -- entities. If the master is a subprogram or task, activation will
2533 -- depend on the activation of the master itself.
2535 -- Should dynamic checks be added in the more general case???
2537 if Ekind (Enclosing) /= E_Package then
2538 return;
2539 end if;
2541 -- For task types defined in other units, we want the unit containing
2542 -- the task body to be elaborated before the current one.
2544 Elmt := First_Elmt (Inter_Procs);
2545 while Present (Elmt) loop
2546 Ent := Node (Elmt);
2547 Task_Scope := Outer_Unit (Scope (Ent));
2549 if not Is_Compilation_Unit (Task_Scope) then
2550 null;
2552 elsif Suppress_Elaboration_Warnings (Task_Scope)
2553 or else Elaboration_Checks_Suppressed (Task_Scope)
2554 then
2555 null;
2557 elsif Dynamic_Elaboration_Checks then
2558 if not Elaboration_Checks_Suppressed (Ent)
2559 and then not Cunit_SC
2560 and then
2561 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2562 then
2563 -- Runtime elaboration check required. Generate check of the
2564 -- elaboration counter for the unit containing the entity.
2566 Insert_Elab_Check (N,
2567 Make_Attribute_Reference (Loc,
2568 Attribute_Name => Name_Elaborated,
2569 Prefix =>
2570 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2571 end if;
2573 else
2574 -- Force the binder to elaborate other unit first
2576 if not Suppress_Elaboration_Warnings (Ent)
2577 and then not Elaboration_Checks_Suppressed (Ent)
2578 and then Elab_Info_Messages
2579 and then not Suppress_Elaboration_Warnings (Task_Scope)
2580 and then not Elaboration_Checks_Suppressed (Task_Scope)
2581 then
2582 Error_Msg_Node_2 := Task_Scope;
2583 Error_Msg_NE
2584 ("info: activation of an instance of task type&" &
2585 " requires pragma Elaborate_All on &?$?", N, Ent);
2586 end if;
2588 Activate_Elaborate_All_Desirable (N, Task_Scope);
2589 Set_Suppress_Elaboration_Warnings (Task_Scope);
2590 end if;
2592 Next_Elmt (Elmt);
2593 end loop;
2595 -- For tasks declared in the current unit, trace other calls within
2596 -- the task procedure bodies, which are available.
2598 In_Task_Activation := True;
2600 Elmt := First_Elmt (Intra_Procs);
2601 while Present (Elmt) loop
2602 Ent := Node (Elmt);
2603 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2604 Next_Elmt (Elmt);
2605 end loop;
2607 In_Task_Activation := False;
2608 end Check_Task_Activation;
2610 -------------------------------
2611 -- Is_Call_Of_Generic_Formal --
2612 -------------------------------
2614 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2615 begin
2616 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2618 -- Always return False if debug flag -gnatd.G is set
2620 and then not Debug_Flag_Dot_GG
2622 -- For now, we detect this by looking for the strange identifier
2623 -- node, whose Chars reflect the name of the generic formal, but
2624 -- the Chars of the Entity references the generic actual.
2626 and then Nkind (Name (N)) = N_Identifier
2627 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
2628 end Is_Call_Of_Generic_Formal;
2630 --------------------------------
2631 -- Set_Elaboration_Constraint --
2632 --------------------------------
2634 procedure Set_Elaboration_Constraint
2635 (Call : Node_Id;
2636 Subp : Entity_Id;
2637 Scop : Entity_Id)
2639 Elab_Unit : Entity_Id;
2641 -- Check whether this is a call to an Initialize subprogram for a
2642 -- controlled type. Note that Call can also be a 'Access attribute
2643 -- reference, which now generates an elaboration check.
2645 Init_Call : constant Boolean :=
2646 Nkind (Call) = N_Procedure_Call_Statement
2647 and then Chars (Subp) = Name_Initialize
2648 and then Comes_From_Source (Subp)
2649 and then Present (Parameter_Associations (Call))
2650 and then Is_Controlled (Etype (First_Actual (Call)));
2651 begin
2652 -- If the unit is mentioned in a with_clause of the current unit, it is
2653 -- visible, and we can set the elaboration flag.
2655 if Is_Immediately_Visible (Scop)
2656 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
2657 then
2658 Activate_Elaborate_All_Desirable (Call, Scop);
2659 Set_Suppress_Elaboration_Warnings (Scop, True);
2660 return;
2661 end if;
2663 -- If this is not an initialization call or a call using object notation
2664 -- we know that the unit of the called entity is in the context, and
2665 -- we can set the flag as well. The unit need not be visible if the call
2666 -- occurs within an instantiation.
2668 if Is_Init_Proc (Subp)
2669 or else Init_Call
2670 or else Nkind (Original_Node (Call)) = N_Selected_Component
2671 then
2672 null; -- detailed processing follows.
2674 else
2675 Activate_Elaborate_All_Desirable (Call, Scop);
2676 Set_Suppress_Elaboration_Warnings (Scop, True);
2677 return;
2678 end if;
2680 -- If the unit is not in the context, there must be an intermediate unit
2681 -- that is, on which we need to place to elaboration flag. This happens
2682 -- with init proc calls.
2684 if Is_Init_Proc (Subp) or else Init_Call then
2686 -- The initialization call is on an object whose type is not declared
2687 -- in the same scope as the subprogram. The type of the object must
2688 -- be a subtype of the type of operation. This object is the first
2689 -- actual in the call.
2691 declare
2692 Typ : constant Entity_Id :=
2693 Etype (First (Parameter_Associations (Call)));
2694 begin
2695 Elab_Unit := Scope (Typ);
2696 while (Present (Elab_Unit))
2697 and then not Is_Compilation_Unit (Elab_Unit)
2698 loop
2699 Elab_Unit := Scope (Elab_Unit);
2700 end loop;
2701 end;
2703 -- If original node uses selected component notation, the prefix is
2704 -- visible and determines the scope that must be elaborated. After
2705 -- rewriting, the prefix is the first actual in the call.
2707 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2708 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2710 -- Not one of special cases above
2712 else
2713 -- Using previously computed scope. If the elaboration check is
2714 -- done after analysis, the scope is not visible any longer, but
2715 -- must still be in the context.
2717 Elab_Unit := Scop;
2718 end if;
2720 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2721 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2722 end Set_Elaboration_Constraint;
2724 ------------------------
2725 -- Get_Referenced_Ent --
2726 ------------------------
2728 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
2729 Nam : Node_Id;
2731 begin
2732 if Nkind (N) = N_Attribute_Reference then
2733 Nam := Prefix (N);
2734 else
2735 Nam := Name (N);
2736 end if;
2738 if No (Nam) then
2739 return Empty;
2740 elsif Nkind (Nam) = N_Selected_Component then
2741 return Entity (Selector_Name (Nam));
2742 elsif not Is_Entity_Name (Nam) then
2743 return Empty;
2744 else
2745 return Entity (Nam);
2746 end if;
2747 end Get_Referenced_Ent;
2749 ----------------------
2750 -- Has_Generic_Body --
2751 ----------------------
2753 function Has_Generic_Body (N : Node_Id) return Boolean is
2754 Ent : constant Entity_Id := Get_Generic_Entity (N);
2755 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2756 Scop : Entity_Id;
2758 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2759 -- Determine if the list of nodes headed by N and linked by Next
2760 -- contains a package body for the package spec entity E, and if so
2761 -- return the package body. If not, then returns Empty.
2763 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2764 -- This procedure is called load the unit whose name is given by Nam.
2765 -- This unit is being loaded to see whether it contains an optional
2766 -- generic body. The returned value is the loaded unit, which is always
2767 -- a package body (only package bodies can contain other entities in the
2768 -- sense in which Has_Generic_Body is interested). We only attempt to
2769 -- load bodies if we are generating code. If we are in semantics check
2770 -- only mode, then it would be wrong to load bodies that are not
2771 -- required from a semantic point of view, so in this case we return
2772 -- Empty. The result is that the caller may incorrectly decide that a
2773 -- generic spec does not have a body when in fact it does, but the only
2774 -- harm in this is that some warnings on elaboration problems may be
2775 -- lost in semantic checks only mode, which is not big loss. We also
2776 -- return Empty if we go for a body and it is not there.
2778 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2779 -- PE is the entity for a package spec. This function locates the
2780 -- corresponding package body, returning Empty if none is found. The
2781 -- package body returned is fully parsed but may not yet be analyzed,
2782 -- so only syntactic fields should be referenced.
2784 ------------------
2785 -- Find_Body_In --
2786 ------------------
2788 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2789 Nod : Node_Id;
2791 begin
2792 Nod := N;
2793 while Present (Nod) loop
2795 -- If we found the package body we are looking for, return it
2797 if Nkind (Nod) = N_Package_Body
2798 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2799 then
2800 return Nod;
2802 -- If we found the stub for the body, go after the subunit,
2803 -- loading it if necessary.
2805 elsif Nkind (Nod) = N_Package_Body_Stub
2806 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2807 then
2808 if Present (Library_Unit (Nod)) then
2809 return Unit (Library_Unit (Nod));
2811 else
2812 return Load_Package_Body (Get_Unit_Name (Nod));
2813 end if;
2815 -- If neither package body nor stub, keep looking on chain
2817 else
2818 Next (Nod);
2819 end if;
2820 end loop;
2822 return Empty;
2823 end Find_Body_In;
2825 -----------------------
2826 -- Load_Package_Body --
2827 -----------------------
2829 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2830 U : Unit_Number_Type;
2832 begin
2833 if Operating_Mode /= Generate_Code then
2834 return Empty;
2835 else
2836 U :=
2837 Load_Unit
2838 (Load_Name => Nam,
2839 Required => False,
2840 Subunit => False,
2841 Error_Node => N);
2843 if U = No_Unit then
2844 return Empty;
2845 else
2846 return Unit (Cunit (U));
2847 end if;
2848 end if;
2849 end Load_Package_Body;
2851 -------------------------------
2852 -- Locate_Corresponding_Body --
2853 -------------------------------
2855 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2856 Spec : constant Node_Id := Declaration_Node (PE);
2857 Decl : constant Node_Id := Parent (Spec);
2858 Scop : constant Entity_Id := Scope (PE);
2859 PBody : Node_Id;
2861 begin
2862 if Is_Library_Level_Entity (PE) then
2864 -- If package is a library unit that requires a body, we have no
2865 -- choice but to go after that body because it might contain an
2866 -- optional body for the original generic package.
2868 if Unit_Requires_Body (PE) then
2870 -- Load the body. Note that we are a little careful here to use
2871 -- Spec to get the unit number, rather than PE or Decl, since
2872 -- in the case where the package is itself a library level
2873 -- instantiation, Spec will properly reference the generic
2874 -- template, which is what we really want.
2876 return
2877 Load_Package_Body
2878 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2880 -- But if the package is a library unit that does NOT require
2881 -- a body, then no body is permitted, so we are sure that there
2882 -- is no body for the original generic package.
2884 else
2885 return Empty;
2886 end if;
2888 -- Otherwise look and see if we are embedded in a further package
2890 elsif Is_Package_Or_Generic_Package (Scop) then
2892 -- If so, get the body of the enclosing package, and look in
2893 -- its package body for the package body we are looking for.
2895 PBody := Locate_Corresponding_Body (Scop);
2897 if No (PBody) then
2898 return Empty;
2899 else
2900 return Find_Body_In (PE, First (Declarations (PBody)));
2901 end if;
2903 -- If we are not embedded in a further package, then the body
2904 -- must be in the same declarative part as we are.
2906 else
2907 return Find_Body_In (PE, Next (Decl));
2908 end if;
2909 end Locate_Corresponding_Body;
2911 -- Start of processing for Has_Generic_Body
2913 begin
2914 if Present (Corresponding_Body (Decl)) then
2915 return True;
2917 elsif Unit_Requires_Body (Ent) then
2918 return True;
2920 -- Compilation units cannot have optional bodies
2922 elsif Is_Compilation_Unit (Ent) then
2923 return False;
2925 -- Otherwise look at what scope we are in
2927 else
2928 Scop := Scope (Ent);
2930 -- Case of entity is in other than a package spec, in this case
2931 -- the body, if present, must be in the same declarative part.
2933 if not Is_Package_Or_Generic_Package (Scop) then
2934 declare
2935 P : Node_Id;
2937 begin
2938 -- Declaration node may get us a spec, so if so, go to
2939 -- the parent declaration.
2941 P := Declaration_Node (Ent);
2942 while not Is_List_Member (P) loop
2943 P := Parent (P);
2944 end loop;
2946 return Present (Find_Body_In (Ent, Next (P)));
2947 end;
2949 -- If the entity is in a package spec, then we have to locate
2950 -- the corresponding package body, and look there.
2952 else
2953 declare
2954 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2956 begin
2957 if No (PBody) then
2958 return False;
2959 else
2960 return
2961 Present
2962 (Find_Body_In (Ent, (First (Declarations (PBody)))));
2963 end if;
2964 end;
2965 end if;
2966 end if;
2967 end Has_Generic_Body;
2969 -----------------------
2970 -- Insert_Elab_Check --
2971 -----------------------
2973 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2974 Nod : Node_Id;
2975 Loc : constant Source_Ptr := Sloc (N);
2977 Chk : Node_Id;
2978 -- The check (N_Raise_Program_Error) node to be inserted
2980 begin
2981 -- If expansion is disabled, do not generate any checks. Also
2982 -- skip checks if any subunits are missing because in either
2983 -- case we lack the full information that we need, and no object
2984 -- file will be created in any case.
2986 if not Expander_Active or else Subunits_Missing then
2987 return;
2988 end if;
2990 -- If we have a generic instantiation, where Instance_Spec is set,
2991 -- then this field points to a generic instance spec that has
2992 -- been inserted before the instantiation node itself, so that
2993 -- is where we want to insert a check.
2995 if Nkind (N) in N_Generic_Instantiation
2996 and then Present (Instance_Spec (N))
2997 then
2998 Nod := Instance_Spec (N);
2999 else
3000 Nod := N;
3001 end if;
3003 -- Build check node, possibly with condition
3005 Chk :=
3006 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3008 if Present (C) then
3009 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3010 end if;
3012 -- If we are inserting at the top level, insert in Aux_Decls
3014 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3015 declare
3016 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3018 begin
3019 if No (Declarations (ADN)) then
3020 Set_Declarations (ADN, New_List (Chk));
3021 else
3022 Append_To (Declarations (ADN), Chk);
3023 end if;
3025 Analyze (Chk);
3026 end;
3028 -- Otherwise just insert as an action on the node in question
3030 else
3031 Insert_Action (Nod, Chk);
3032 end if;
3033 end Insert_Elab_Check;
3035 -------------------------------
3036 -- Is_Finalization_Procedure --
3037 -------------------------------
3039 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3040 begin
3041 -- Check whether Id is a procedure with at least one parameter
3043 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3044 declare
3045 Typ : constant Entity_Id := Etype (First_Formal (Id));
3046 Deep_Fin : Entity_Id := Empty;
3047 Fin : Entity_Id := Empty;
3049 begin
3050 -- If the type of the first formal does not require finalization
3051 -- actions, then this is definitely not [Deep_]Finalize.
3053 if not Needs_Finalization (Typ) then
3054 return False;
3055 end if;
3057 -- At this point we have the following scenario:
3059 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3061 -- Recover the two possible versions of [Deep_]Finalize using the
3062 -- type of the first parameter and compare with the input.
3064 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3066 if Is_Controlled (Typ) then
3067 Fin := Find_Prim_Op (Typ, Name_Finalize);
3068 end if;
3070 return (Present (Deep_Fin) and then Id = Deep_Fin)
3071 or else (Present (Fin) and then Id = Fin);
3072 end;
3073 end if;
3075 return False;
3076 end Is_Finalization_Procedure;
3078 ------------------
3079 -- Output_Calls --
3080 ------------------
3082 procedure Output_Calls
3083 (N : Node_Id;
3084 Check_Elab_Flag : Boolean)
3086 function Emit (Flag : Boolean) return Boolean;
3087 -- Determine whether to emit an error message based on the combination
3088 -- of flags Check_Elab_Flag and Flag.
3090 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
3091 -- An internal function, used to determine if a name, Nm, is either
3092 -- a non-internal name, or is an internal name that is printable
3093 -- by the error message circuits (i.e. it has a single upper
3094 -- case letter at the end).
3096 ----------
3097 -- Emit --
3098 ----------
3100 function Emit (Flag : Boolean) return Boolean is
3101 begin
3102 if Check_Elab_Flag then
3103 return Flag;
3104 else
3105 return True;
3106 end if;
3107 end Emit;
3109 -----------------------------
3110 -- Is_Printable_Error_Name --
3111 -----------------------------
3113 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
3114 begin
3115 if not Is_Internal_Name (Nm) then
3116 return True;
3118 elsif Name_Len = 1 then
3119 return False;
3121 else
3122 Name_Len := Name_Len - 1;
3123 return not Is_Internal_Name;
3124 end if;
3125 end Is_Printable_Error_Name;
3127 -- Local variables
3129 Ent : Entity_Id;
3131 -- Start of processing for Output_Calls
3133 begin
3134 for J in reverse 1 .. Elab_Call.Last loop
3135 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3137 Ent := Elab_Call.Table (J).Ent;
3139 -- Dynamic elaboration model, warnings controlled by -gnatwl
3141 if Dynamic_Elaboration_Checks then
3142 if Emit (Elab_Warnings) then
3143 if Is_Generic_Unit (Ent) then
3144 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3145 elsif Is_Init_Proc (Ent) then
3146 Error_Msg_N ("\\?l?initialization procedure called #", N);
3147 elsif Is_Printable_Error_Name (Chars (Ent)) then
3148 Error_Msg_NE ("\\?l?& called #", N, Ent);
3149 else
3150 Error_Msg_N ("\\?l?called #", N);
3151 end if;
3152 end if;
3154 -- Static elaboration model, info messages controlled by -gnatel
3156 else
3157 if Emit (Elab_Info_Messages) then
3158 if Is_Generic_Unit (Ent) then
3159 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3160 elsif Is_Init_Proc (Ent) then
3161 Error_Msg_N ("\\?$?initialization procedure called #", N);
3162 elsif Is_Printable_Error_Name (Chars (Ent)) then
3163 Error_Msg_NE ("\\?$?& called #", N, Ent);
3164 else
3165 Error_Msg_N ("\\?$?called #", N);
3166 end if;
3167 end if;
3168 end if;
3169 end loop;
3170 end Output_Calls;
3172 ----------------------------
3173 -- Same_Elaboration_Scope --
3174 ----------------------------
3176 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3177 S1 : Entity_Id;
3178 S2 : Entity_Id;
3180 begin
3181 -- Find elaboration scope for Scop1
3182 -- This is either a subprogram or a compilation unit.
3184 S1 := Scop1;
3185 while S1 /= Standard_Standard
3186 and then not Is_Compilation_Unit (S1)
3187 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3188 loop
3189 S1 := Scope (S1);
3190 end loop;
3192 -- Find elaboration scope for Scop2
3194 S2 := Scop2;
3195 while S2 /= Standard_Standard
3196 and then not Is_Compilation_Unit (S2)
3197 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3198 loop
3199 S2 := Scope (S2);
3200 end loop;
3202 return S1 = S2;
3203 end Same_Elaboration_Scope;
3205 -----------------
3206 -- Set_C_Scope --
3207 -----------------
3209 procedure Set_C_Scope is
3210 begin
3211 while not Is_Compilation_Unit (C_Scope) loop
3212 C_Scope := Scope (C_Scope);
3213 end loop;
3214 end Set_C_Scope;
3216 -----------------
3217 -- Spec_Entity --
3218 -----------------
3220 function Spec_Entity (E : Entity_Id) return Entity_Id is
3221 Decl : Node_Id;
3223 begin
3224 -- Check for case of body entity
3225 -- Why is the check for E_Void needed???
3227 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3228 Decl := E;
3230 loop
3231 Decl := Parent (Decl);
3232 exit when Nkind (Decl) in N_Proper_Body;
3233 end loop;
3235 return Corresponding_Spec (Decl);
3237 else
3238 return E;
3239 end if;
3240 end Spec_Entity;
3242 -------------------
3243 -- Supply_Bodies --
3244 -------------------
3246 procedure Supply_Bodies (N : Node_Id) is
3247 begin
3248 if Nkind (N) = N_Subprogram_Declaration then
3249 declare
3250 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3252 begin
3253 -- Internal subprograms will already have a generated body, so
3254 -- there is no need to provide a stub for them.
3256 if No (Corresponding_Body (N)) then
3257 declare
3258 Loc : constant Source_Ptr := Sloc (N);
3259 B : Node_Id;
3260 Formals : constant List_Id := Copy_Parameter_List (Ent);
3261 Nam : constant Entity_Id :=
3262 Make_Defining_Identifier (Loc, Chars (Ent));
3263 Spec : Node_Id;
3264 Stats : constant List_Id :=
3265 New_List
3266 (Make_Raise_Program_Error (Loc,
3267 Reason => PE_Access_Before_Elaboration));
3269 begin
3270 if Ekind (Ent) = E_Function then
3271 Spec :=
3272 Make_Function_Specification (Loc,
3273 Defining_Unit_Name => Nam,
3274 Parameter_Specifications => Formals,
3275 Result_Definition =>
3276 New_Copy_Tree
3277 (Result_Definition (Specification (N))));
3279 -- We cannot reliably make a return statement for this
3280 -- body, but none is needed because the call raises
3281 -- program error.
3283 Set_Return_Present (Ent);
3285 else
3286 Spec :=
3287 Make_Procedure_Specification (Loc,
3288 Defining_Unit_Name => Nam,
3289 Parameter_Specifications => Formals);
3290 end if;
3292 B := Make_Subprogram_Body (Loc,
3293 Specification => Spec,
3294 Declarations => New_List,
3295 Handled_Statement_Sequence =>
3296 Make_Handled_Sequence_Of_Statements (Loc, Stats));
3297 Insert_After (N, B);
3298 Analyze (B);
3299 end;
3300 end if;
3301 end;
3303 elsif Nkind (N) = N_Package_Declaration then
3304 declare
3305 Spec : constant Node_Id := Specification (N);
3306 begin
3307 Push_Scope (Defining_Unit_Name (Spec));
3308 Supply_Bodies (Visible_Declarations (Spec));
3309 Supply_Bodies (Private_Declarations (Spec));
3310 Pop_Scope;
3311 end;
3312 end if;
3313 end Supply_Bodies;
3315 procedure Supply_Bodies (L : List_Id) is
3316 Elmt : Node_Id;
3317 begin
3318 if Present (L) then
3319 Elmt := First (L);
3320 while Present (Elmt) loop
3321 Supply_Bodies (Elmt);
3322 Next (Elmt);
3323 end loop;
3324 end if;
3325 end Supply_Bodies;
3327 ------------
3328 -- Within --
3329 ------------
3331 function Within (E1, E2 : Entity_Id) return Boolean is
3332 Scop : Entity_Id;
3333 begin
3334 Scop := E1;
3335 loop
3336 if Scop = E2 then
3337 return True;
3338 elsif Scop = Standard_Standard then
3339 return False;
3340 else
3341 Scop := Scope (Scop);
3342 end if;
3343 end loop;
3344 end Within;
3346 --------------------------
3347 -- Within_Elaborate_All --
3348 --------------------------
3350 function Within_Elaborate_All
3351 (Unit : Unit_Number_Type;
3352 E : Entity_Id) return Boolean
3354 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3355 pragma Pack (Unit_Number_Set);
3357 Seen : Unit_Number_Set := (others => False);
3358 -- Seen (X) is True after we have seen unit X in the walk. This is used
3359 -- to prevent processing the same unit more than once.
3361 Result : Boolean := False;
3363 procedure Helper (Unit : Unit_Number_Type);
3364 -- This helper procedure does all the work for Within_Elaborate_All. It
3365 -- walks the dependency graph, and sets Result to True if it finds an
3366 -- appropriate Elaborate_All.
3368 ------------
3369 -- Helper --
3370 ------------
3372 procedure Helper (Unit : Unit_Number_Type) is
3373 CU : constant Node_Id := Cunit (Unit);
3375 Item : Node_Id;
3376 Item2 : Node_Id;
3377 Elab_Id : Entity_Id;
3378 Par : Node_Id;
3380 begin
3381 if Seen (Unit) then
3382 return;
3383 else
3384 Seen (Unit) := True;
3385 end if;
3387 -- First, check for Elaborate_Alls on this unit
3389 Item := First (Context_Items (CU));
3390 while Present (Item) loop
3391 if Nkind (Item) = N_Pragma
3392 and then Pragma_Name (Item) = Name_Elaborate_All
3393 then
3394 -- Return if some previous error on the pragma itself. The
3395 -- pragma may be unanalyzed, because of a previous error, or
3396 -- if it is the context of a subunit, inherited by its parent.
3398 if Error_Posted (Item) or else not Analyzed (Item) then
3399 return;
3400 end if;
3402 Elab_Id :=
3403 Entity
3404 (Expression (First (Pragma_Argument_Associations (Item))));
3406 if E = Elab_Id then
3407 Result := True;
3408 return;
3409 end if;
3411 Par := Parent (Unit_Declaration_Node (Elab_Id));
3413 Item2 := First (Context_Items (Par));
3414 while Present (Item2) loop
3415 if Nkind (Item2) = N_With_Clause
3416 and then Entity (Name (Item2)) = E
3417 and then not Limited_Present (Item2)
3418 then
3419 Result := True;
3420 return;
3421 end if;
3423 Next (Item2);
3424 end loop;
3425 end if;
3427 Next (Item);
3428 end loop;
3430 -- Second, recurse on with's. We could do this as part of the above
3431 -- loop, but it's probably more efficient to have two loops, because
3432 -- the relevant Elaborate_All is likely to be on the initial unit. In
3433 -- other words, we're walking the with's breadth-first. This part is
3434 -- only necessary in the dynamic elaboration model.
3436 if Dynamic_Elaboration_Checks then
3437 Item := First (Context_Items (CU));
3438 while Present (Item) loop
3439 if Nkind (Item) = N_With_Clause
3440 and then not Limited_Present (Item)
3441 then
3442 -- Note: the following call to Get_Cunit_Unit_Number does a
3443 -- linear search, which could be slow, but it's OK because
3444 -- we're about to give a warning anyway. Also, there might
3445 -- be hundreds of units, but not millions. If it turns out
3446 -- to be a problem, we could store the Get_Cunit_Unit_Number
3447 -- in each N_Compilation_Unit node, but that would involve
3448 -- rearranging N_Compilation_Unit_Aux to make room.
3450 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3452 if Result then
3453 return;
3454 end if;
3455 end if;
3457 Next (Item);
3458 end loop;
3459 end if;
3460 end Helper;
3462 -- Start of processing for Within_Elaborate_All
3464 begin
3465 Helper (Unit);
3466 return Result;
3467 end Within_Elaborate_All;
3469 end Sem_Elab;