* config/rs6000/aix61.h (TARGET_DEFAULT): Add MASK_PPC_GPOPT,
[official-gcc.git] / gcc / ada / sem_elab.adb
blobd1b5f7c6b55a4c9f7e336c77e5f7d2f2c0ba828a
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-2012, 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_Res; use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sem_Util; use Sem_Util;
53 with Sinfo; use Sinfo;
54 with Sinput; use Sinput;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Table;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
60 with Uname; use Uname;
62 package body Sem_Elab is
64 -- The following table records the recursive call chain for output in the
65 -- Output routine. Each entry records the call node and the entity of the
66 -- called routine. The number of entries in the table (i.e. the value of
67 -- Elab_Call.Last) indicates the current depth of recursion and is used to
68 -- identify the outer level.
70 type Elab_Call_Entry is record
71 Cloc : Source_Ptr;
72 Ent : Entity_Id;
73 end record;
75 package Elab_Call is new Table.Table (
76 Table_Component_Type => Elab_Call_Entry,
77 Table_Index_Type => Int,
78 Table_Low_Bound => 1,
79 Table_Initial => 50,
80 Table_Increment => 100,
81 Table_Name => "Elab_Call");
83 -- This table is initialized at the start of each outer level call. It
84 -- holds the entities for all subprograms that have been examined for this
85 -- particular outer level call, and is used to prevent both infinite
86 -- recursion, and useless reanalysis of bodies already seen
88 package Elab_Visited is new Table.Table (
89 Table_Component_Type => Entity_Id,
90 Table_Index_Type => Int,
91 Table_Low_Bound => 1,
92 Table_Initial => 200,
93 Table_Increment => 100,
94 Table_Name => "Elab_Visited");
96 -- This table stores calls to Check_Internal_Call that are delayed
97 -- until all generics are instantiated, and in particular that all
98 -- generic bodies have been inserted. We need to delay, because we
99 -- need to be able to look through the inserted bodies.
101 type Delay_Element is record
102 N : Node_Id;
103 -- The parameter N from the call to Check_Internal_Call. Note that
104 -- this node may get rewritten over the delay period by expansion
105 -- in the call case (but not in the instantiation case).
107 E : Entity_Id;
108 -- The parameter E from the call to Check_Internal_Call
110 Orig_Ent : Entity_Id;
111 -- The parameter Orig_Ent from the call to Check_Internal_Call
113 Curscop : Entity_Id;
114 -- The current scope of the call. This is restored when we complete
115 -- the delayed call, so that we do this in the right scope.
117 From_Elab_Code : Boolean;
118 -- Save indication of whether this call is from elaboration code
120 Outer_Scope : Entity_Id;
121 -- Save scope of outer level call
122 end record;
124 package Delay_Check is new Table.Table (
125 Table_Component_Type => Delay_Element,
126 Table_Index_Type => Int,
127 Table_Low_Bound => 1,
128 Table_Initial => 1000,
129 Table_Increment => 100,
130 Table_Name => "Delay_Check");
132 C_Scope : Entity_Id;
133 -- Top level scope of current scope. Compute this only once at the outer
134 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
136 Outer_Level_Sloc : Source_Ptr;
137 -- Save Sloc value for outer level call node for comparisons of source
138 -- locations. A body is too late if it appears after the *outer* level
139 -- call, not the particular call that is being analyzed.
141 From_Elab_Code : Boolean;
142 -- This flag shows whether the outer level call currently being examined
143 -- is or is not in elaboration code. We are only interested in calls to
144 -- routines in other units if this flag is True.
146 In_Task_Activation : Boolean := False;
147 -- This flag indicates whether we are performing elaboration checks on
148 -- task procedures, at the point of activation. If true, we do not trace
149 -- internal calls in these procedures, because all local bodies are known
150 -- to be elaborated.
152 Delaying_Elab_Checks : Boolean := True;
153 -- This is set True till the compilation is complete, including the
154 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
155 -- the delay table is used to make the delayed calls and this flag is reset
156 -- to False, so that the calls are processed
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 -- Note: Outer_Scope in all following specs represents the scope of
163 -- interest of the outer level call. If it is set to Standard_Standard,
164 -- then it means the outer level call was at elaboration level, and that
165 -- thus all calls are of interest. If it was set to some other scope,
166 -- then the original call was an inner call, and we are not interested
167 -- in calls that go outside this scope.
169 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
170 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
171 -- for the WITH clause for unit U (which will always be present). A special
172 -- case is when N is a function or procedure instantiation, in which case
173 -- it is sufficient to set Elaborate_Desirable, since in this case there is
174 -- no possibility of transitive elaboration issues.
176 procedure Check_A_Call
177 (N : Node_Id;
178 E : Entity_Id;
179 Outer_Scope : Entity_Id;
180 Inter_Unit_Only : Boolean;
181 Generate_Warnings : Boolean := True;
182 In_Init_Proc : Boolean := False);
183 -- This is the internal recursive routine that is called to check for
184 -- possible elaboration error. The argument N is a subprogram call or
185 -- generic instantiation, or 'Access attribute reference to be checked, and
186 -- E is the entity of the called subprogram, or instantiated generic unit,
187 -- or subprogram referenced by 'Access.
189 -- The flag Outer_Scope is the outer level scope for the original call.
190 -- Inter_Unit_Only is set if the call is only to be checked in the
191 -- case where it is to another unit (and skipped if within a unit).
192 -- Generate_Warnings is set to False to suppress warning messages about
193 -- missing pragma Elaborate_All's. These messages are not wanted for
194 -- inner calls in the dynamic model. Note that an instance of the Access
195 -- attribute applied to a subprogram also generates a call to this
196 -- procedure (since the referenced subprogram may be called later
197 -- indirectly). Flag In_Init_Proc should be set whenever the current
198 -- context is a type init proc.
200 procedure Check_Bad_Instantiation (N : Node_Id);
201 -- N is a node for an instantiation (if called with any other node kind,
202 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
203 -- the special case of a generic instantiation of a generic spec in the
204 -- same declarative part as the instantiation where a body is present and
205 -- has not yet been seen. This is an obvious error, but needs to be checked
206 -- specially at the time of the instantiation, since it is a case where we
207 -- cannot insert the body anywhere. If this case is detected, warnings are
208 -- generated, and a raise of Program_Error is inserted. In addition any
209 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
210 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
211 -- flag as an indication that no attempt should be made to insert an
212 -- instance body.
214 procedure Check_Internal_Call
215 (N : Node_Id;
216 E : Entity_Id;
217 Outer_Scope : Entity_Id;
218 Orig_Ent : Entity_Id);
219 -- N is a function call or procedure statement call node and E is the
220 -- entity of the called function, which is within the current compilation
221 -- unit (where subunits count as part of the parent). This call checks if
222 -- this call, or any call within any accessed body could cause an ABE, and
223 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
224 -- renamings, and points to the original name of the entity. This is used
225 -- for error messages. Outer_Scope is the outer level scope for the
226 -- original call.
228 procedure Check_Internal_Call_Continue
229 (N : Node_Id;
230 E : Entity_Id;
231 Outer_Scope : Entity_Id;
232 Orig_Ent : Entity_Id);
233 -- The processing for Check_Internal_Call is divided up into two phases,
234 -- and this represents the second phase. The second phase is delayed if
235 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
236 -- phase makes an entry in the Delay_Check table, which is processed when
237 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
238 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
239 -- original call.
241 function Has_Generic_Body (N : Node_Id) return Boolean;
242 -- N is a generic package instantiation node, and this routine determines
243 -- if this package spec does in fact have a generic body. If so, then
244 -- True is returned, otherwise False. Note that this is not at all the
245 -- same as checking if the unit requires a body, since it deals with
246 -- the case of optional bodies accurately (i.e. if a body is optional,
247 -- then it looks to see if a body is actually present). Note: this
248 -- function can only do a fully correct job if in generating code mode
249 -- where all bodies have to be present. If we are operating in semantics
250 -- check only mode, then in some cases of optional bodies, a result of
251 -- False may incorrectly be given. In practice this simply means that
252 -- some cases of warnings for incorrect order of elaboration will only
253 -- be given when generating code, which is not a big problem (and is
254 -- inevitable, given the optional body semantics of Ada).
256 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
257 -- Given code for an elaboration check (or unconditional raise if the check
258 -- is not needed), inserts the code in the appropriate place. N is the call
259 -- or instantiation node for which the check code is required. C is the
260 -- test whose failure triggers the raise.
262 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
263 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
265 procedure Output_Calls (N : Node_Id);
266 -- Outputs chain of calls stored in the Elab_Call table. The caller has
267 -- already generated the main warning message, so the warnings generated
268 -- are all continuation messages. The argument is the call node at which
269 -- the messages are to be placed.
271 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
272 -- Given two scopes, determine whether they are the same scope from an
273 -- elaboration point of view, i.e. packages and blocks are ignored.
275 procedure Set_C_Scope;
276 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
277 -- to be the enclosing compilation unit of this scope.
279 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
280 -- N is either a function or procedure call or an access attribute that
281 -- references a subprogram. This call retrieves the relevant entity. If
282 -- this is a call to a protected subprogram, the entity is a selected
283 -- component. The callable entity may be absent, in which case Empty is
284 -- returned. This happens with non-analyzed calls in nested generics.
286 procedure Set_Elaboration_Constraint
287 (Call : Node_Id;
288 Subp : Entity_Id;
289 Scop : Entity_Id);
290 -- The current unit U may depend semantically on some unit P which is not
291 -- in the current context. If there is an elaboration call that reaches P,
292 -- we need to indicate that P requires an Elaborate_All, but this is not
293 -- effective in U's ali file, if there is no with_clause for P. In this
294 -- case we add the Elaborate_All on the unit Q that directly or indirectly
295 -- makes P available. This can happen in two cases:
297 -- a) Q declares a subtype of a type declared in P, and the call is an
298 -- initialization call for an object of that subtype.
300 -- b) Q declares an object of some tagged type whose root type is
301 -- declared in P, and the initialization call uses object notation on
302 -- that object to reach a primitive operation or a classwide operation
303 -- declared in P.
305 -- If P appears in the context of U, the current processing is correct.
306 -- Otherwise we must identify these two cases to retrieve Q and place the
307 -- Elaborate_All_Desirable on it.
309 function Spec_Entity (E : Entity_Id) return Entity_Id;
310 -- Given a compilation unit entity, if it is a spec entity, it is returned
311 -- unchanged. If it is a body entity, then the spec for the corresponding
312 -- spec is returned
314 procedure Supply_Bodies (N : Node_Id);
315 -- Given a node, N, that is either a subprogram declaration or a package
316 -- declaration, this procedure supplies dummy bodies for the subprogram
317 -- or for all subprograms in the package. If the given node is not one
318 -- of these two possibilities, then Supply_Bodies does nothing. The
319 -- dummy body contains a single Raise statement.
321 procedure Supply_Bodies (L : List_Id);
322 -- Calls Supply_Bodies for all elements of the given list L
324 function Within (E1, E2 : Entity_Id) return Boolean;
325 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
326 -- of its contained scopes, False otherwise.
328 function Within_Elaborate_All
329 (Unit : Unit_Number_Type;
330 E : Entity_Id) return Boolean;
331 -- Return True if we are within the scope of an Elaborate_All for E, or if
332 -- we are within the scope of an Elaborate_All for some other unit U, and U
333 -- with's E. This prevents spurious warnings when the called entity is
334 -- renamed within U, or in case of generic instances.
336 --------------------------------------
337 -- Activate_Elaborate_All_Desirable --
338 --------------------------------------
340 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
341 UN : constant Unit_Number_Type := Get_Code_Unit (N);
342 CU : constant Node_Id := Cunit (UN);
343 UE : constant Entity_Id := Cunit_Entity (UN);
344 Unm : constant Unit_Name_Type := Unit_Name (UN);
345 CI : constant List_Id := Context_Items (CU);
346 Itm : Node_Id;
347 Ent : Entity_Id;
349 procedure Add_To_Context_And_Mark (Itm : Node_Id);
350 -- This procedure is called when the elaborate indication must be
351 -- applied to a unit not in the context of the referencing unit. The
352 -- unit gets added to the context as an implicit with.
354 function In_Withs_Of (UEs : Entity_Id) return Boolean;
355 -- UEs is the spec entity of a unit. If the unit to be marked is
356 -- in the context item list of this unit spec, then the call returns
357 -- True and Itm is left set to point to the relevant N_With_Clause node.
359 procedure Set_Elab_Flag (Itm : Node_Id);
360 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
362 -----------------------------
363 -- Add_To_Context_And_Mark --
364 -----------------------------
366 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
367 CW : constant Node_Id :=
368 Make_With_Clause (Sloc (Itm),
369 Name => Name (Itm));
371 begin
372 Set_Library_Unit (CW, Library_Unit (Itm));
373 Set_Implicit_With (CW, True);
375 -- Set elaborate all desirable on copy and then append the copy to
376 -- the list of body with's and we are done.
378 Set_Elab_Flag (CW);
379 Append_To (CI, CW);
380 end Add_To_Context_And_Mark;
382 -----------------
383 -- In_Withs_Of --
384 -----------------
386 function In_Withs_Of (UEs : Entity_Id) return Boolean is
387 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
388 CUs : constant Node_Id := Cunit (UNs);
389 CIs : constant List_Id := Context_Items (CUs);
391 begin
392 Itm := First (CIs);
393 while Present (Itm) loop
394 if Nkind (Itm) = N_With_Clause then
395 Ent :=
396 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
398 if U = Ent then
399 return True;
400 end if;
401 end if;
403 Next (Itm);
404 end loop;
406 return False;
407 end In_Withs_Of;
409 -------------------
410 -- Set_Elab_Flag --
411 -------------------
413 procedure Set_Elab_Flag (Itm : Node_Id) is
414 begin
415 if Nkind (N) in N_Subprogram_Instantiation then
416 Set_Elaborate_Desirable (Itm);
417 else
418 Set_Elaborate_All_Desirable (Itm);
419 end if;
420 end Set_Elab_Flag;
422 -- Start of processing for Activate_Elaborate_All_Desirable
424 begin
425 -- Do not set binder indication if expansion is disabled, as when
426 -- compiling a generic unit.
428 if not Expander_Active then
429 return;
430 end if;
432 Itm := First (CI);
433 while Present (Itm) loop
434 if Nkind (Itm) = N_With_Clause then
435 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
437 -- If we find it, then mark elaborate all desirable and return
439 if U = Ent then
440 Set_Elab_Flag (Itm);
441 return;
442 end if;
443 end if;
445 Next (Itm);
446 end loop;
448 -- If we fall through then the with clause is not present in the
449 -- current unit. One legitimate possibility is that the with clause
450 -- is present in the spec when we are a body.
452 if Is_Body_Name (Unm)
453 and then In_Withs_Of (Spec_Entity (UE))
454 then
455 Add_To_Context_And_Mark (Itm);
456 return;
457 end if;
459 -- Similarly, we may be in the spec or body of a child unit, where
460 -- the unit in question is with'ed by some ancestor of the child unit.
462 if Is_Child_Name (Unm) then
463 declare
464 Pkg : Entity_Id;
466 begin
467 Pkg := UE;
468 loop
469 Pkg := Scope (Pkg);
470 exit when Pkg = Standard_Standard;
472 if In_Withs_Of (Pkg) then
473 Add_To_Context_And_Mark (Itm);
474 return;
475 end if;
476 end loop;
477 end;
478 end if;
480 -- Here if we do not find with clause on spec or body. We just ignore
481 -- this case, it means that the elaboration involves some other unit
482 -- than the unit being compiled, and will be caught elsewhere.
484 null;
485 end Activate_Elaborate_All_Desirable;
487 ------------------
488 -- Check_A_Call --
489 ------------------
491 procedure Check_A_Call
492 (N : Node_Id;
493 E : Entity_Id;
494 Outer_Scope : Entity_Id;
495 Inter_Unit_Only : Boolean;
496 Generate_Warnings : Boolean := True;
497 In_Init_Proc : Boolean := False)
499 Loc : constant Source_Ptr := Sloc (N);
500 Ent : Entity_Id;
501 Decl : Node_Id;
503 E_Scope : Entity_Id;
504 -- Top level scope of entity for called subprogram. This value includes
505 -- following renamings and derivations, so this scope can be in a
506 -- non-visible unit. This is the scope that is to be investigated to
507 -- see whether an elaboration check is required.
509 W_Scope : Entity_Id;
510 -- Top level scope of directly called entity for subprogram. This
511 -- differs from E_Scope in the case where renamings or derivations
512 -- are involved, since it does not follow these links. W_Scope is
513 -- generally in a visible unit, and it is this scope that may require
514 -- an Elaborate_All. However, there are some cases (initialization
515 -- calls and calls involving object notation) where W_Scope might not
516 -- be in the context of the current unit, and there is an intermediate
517 -- package that is, in which case the Elaborate_All has to be placed
518 -- on this intermediate package. These special cases are handled in
519 -- Set_Elaboration_Constraint.
521 Body_Acts_As_Spec : Boolean;
522 -- Set to true if call is to body acting as spec (no separate spec)
524 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
525 -- Indicates if we have instantiation case
527 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
528 -- Indicates if we have Access attribute case
530 Caller_Unit_Internal : Boolean;
531 Callee_Unit_Internal : Boolean;
533 Inst_Caller : Source_Ptr;
534 Inst_Callee : Source_Ptr;
536 Unit_Caller : Unit_Number_Type;
537 Unit_Callee : Unit_Number_Type;
539 Cunit_SC : Boolean := False;
540 -- Set to suppress dynamic elaboration checks where one of the
541 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
542 -- if a pragma Elaborate (_All) applies to that scope, in which case
543 -- warnings on the scope are also suppressed. For the internal case,
544 -- we ignore this flag.
546 begin
547 -- If the call is known to be within a local Suppress Elaboration
548 -- pragma, nothing to check. This can happen in task bodies.
550 if Nkind (N) in N_Subprogram_Call
551 and then No_Elaboration_Check (N)
552 then
553 return;
554 end if;
556 -- Go to parent for derived subprogram, or to original subprogram in the
557 -- case of a renaming (Alias covers both these cases).
559 Ent := E;
560 loop
561 if (Suppress_Elaboration_Warnings (Ent)
562 or else Elaboration_Checks_Suppressed (Ent))
563 and then (Inst_Case or else No (Alias (Ent)))
564 then
565 return;
566 end if;
568 -- Nothing to do for imported entities
570 if Is_Imported (Ent) then
571 return;
572 end if;
574 exit when Inst_Case or else No (Alias (Ent));
575 Ent := Alias (Ent);
576 end loop;
578 Decl := Unit_Declaration_Node (Ent);
580 if Nkind (Decl) = N_Subprogram_Body then
581 Body_Acts_As_Spec := True;
583 elsif Nkind (Decl) = N_Subprogram_Declaration
584 or else Nkind (Decl) = N_Subprogram_Body_Stub
585 or else Inst_Case
586 then
587 Body_Acts_As_Spec := False;
589 -- If we have none of an instantiation, subprogram body or
590 -- subprogram declaration, then it is not a case that we want
591 -- to check. (One case is a call to a generic formal subprogram,
592 -- where we do not want the check in the template).
594 else
595 return;
596 end if;
598 E_Scope := Ent;
599 loop
600 if Elaboration_Checks_Suppressed (E_Scope)
601 or else Suppress_Elaboration_Warnings (E_Scope)
602 then
603 Cunit_SC := True;
604 end if;
606 -- Exit when we get to compilation unit, not counting subunits
608 exit when Is_Compilation_Unit (E_Scope)
609 and then (Is_Child_Unit (E_Scope)
610 or else Scope (E_Scope) = Standard_Standard);
612 -- If we did not find a compilation unit, other than standard,
613 -- then nothing to check (happens in some instantiation cases)
615 if E_Scope = Standard_Standard then
616 return;
618 -- Otherwise move up a scope looking for compilation unit
620 else
621 E_Scope := Scope (E_Scope);
622 end if;
623 end loop;
625 -- No checks needed for pure or preelaborated compilation units
627 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
628 return;
629 end if;
631 -- If the generic entity is within a deeper instance than we are, then
632 -- either the instantiation to which we refer itself caused an ABE, in
633 -- which case that will be handled separately, or else we know that the
634 -- body we need appears as needed at the point of the instantiation.
635 -- However, this assumption is only valid if we are in static mode.
637 if not Dynamic_Elaboration_Checks
638 and then Instantiation_Depth (Sloc (Ent)) >
639 Instantiation_Depth (Sloc (N))
640 then
641 return;
642 end if;
644 -- Do not give a warning for a package with no body
646 if Ekind (Ent) = E_Generic_Package
647 and then not Has_Generic_Body (N)
648 then
649 return;
650 end if;
652 -- Case of entity is not in current unit (i.e. with'ed unit case)
654 if E_Scope /= C_Scope then
656 -- We are only interested in such calls if the outer call was from
657 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
659 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
660 return;
661 end if;
663 -- Nothing to do if some scope said that no checks were required
665 if Cunit_SC then
666 return;
667 end if;
669 -- Nothing to do for a generic instance, because in this case the
670 -- checking was at the point of instantiation of the generic However,
671 -- this shortcut is only applicable in static mode.
673 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
674 return;
675 end if;
677 -- Nothing to do if subprogram with no separate spec. However, a
678 -- call to Deep_Initialize may result in a call to a user-defined
679 -- Initialize procedure, which imposes a body dependency. This
680 -- happens only if the type is controlled and the Initialize
681 -- procedure is not inherited.
683 if Body_Acts_As_Spec then
684 if Is_TSS (Ent, TSS_Deep_Initialize) then
685 declare
686 Typ : constant Entity_Id := Etype (First_Formal (Ent));
687 Init : Entity_Id;
689 begin
690 if not Is_Controlled (Typ) then
691 return;
692 else
693 Init := Find_Prim_Op (Typ, Name_Initialize);
695 if Comes_From_Source (Init) then
696 Ent := Init;
697 else
698 return;
699 end if;
700 end if;
701 end;
703 else
704 return;
705 end if;
706 end if;
708 -- Check cases of internal units
710 Callee_Unit_Internal :=
711 Is_Internal_File_Name
712 (Unit_File_Name (Get_Source_Unit (E_Scope)));
714 -- Do not give a warning if the with'ed unit is internal and this is
715 -- the generic instantiation case (this saves a lot of hassle dealing
716 -- with the Text_IO special child units)
718 if Callee_Unit_Internal and Inst_Case then
719 return;
720 end if;
722 if C_Scope = Standard_Standard then
723 Caller_Unit_Internal := False;
724 else
725 Caller_Unit_Internal :=
726 Is_Internal_File_Name
727 (Unit_File_Name (Get_Source_Unit (C_Scope)));
728 end if;
730 -- Do not give a warning if the with'ed unit is internal and the
731 -- caller is not internal (since the binder always elaborates
732 -- internal units first).
734 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
735 return;
736 end if;
738 -- For now, if debug flag -gnatdE is not set, do no checking for
739 -- one internal unit withing another. This fixes the problem with
740 -- the sgi build and storage errors. To be resolved later ???
742 if (Callee_Unit_Internal and Caller_Unit_Internal)
743 and then not Debug_Flag_EE
744 then
745 return;
746 end if;
748 if Is_TSS (E, TSS_Deep_Initialize) then
749 Ent := E;
750 end if;
752 -- If the call is in an instance, and the called entity is not
753 -- defined in the same instance, then the elaboration issue focuses
754 -- around the unit containing the template, it is this unit which
755 -- requires an Elaborate_All.
757 -- However, if we are doing dynamic elaboration, we need to chase the
758 -- call in the usual manner.
760 -- We do not handle the case of calling a generic formal correctly in
761 -- the static case.???
763 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
764 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
766 if Inst_Caller = No_Location then
767 Unit_Caller := No_Unit;
768 else
769 Unit_Caller := Get_Source_Unit (N);
770 end if;
772 if Inst_Callee = No_Location then
773 Unit_Callee := No_Unit;
774 else
775 Unit_Callee := Get_Source_Unit (Ent);
776 end if;
778 if Unit_Caller /= No_Unit
779 and then Unit_Callee /= Unit_Caller
780 and then not Dynamic_Elaboration_Checks
781 then
782 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
784 -- If we don't get a spec entity, just ignore call. Not quite
785 -- clear why this check is necessary. ???
787 if No (E_Scope) then
788 return;
789 end if;
791 -- Otherwise step to enclosing compilation unit
793 while not Is_Compilation_Unit (E_Scope) loop
794 E_Scope := Scope (E_Scope);
795 end loop;
797 -- For the case N is not an instance, or a call within instance, we
798 -- recompute E_Scope for the error message, since we do NOT want to
799 -- go to the unit which has the ultimate declaration in the case of
800 -- renaming and derivation and we also want to go to the generic unit
801 -- in the case of an instance, and no further.
803 else
804 -- Loop to carefully follow renamings and derivations one step
805 -- outside the current unit, but not further.
807 if not Inst_Case
808 and then Present (Alias (Ent))
809 then
810 E_Scope := Alias (Ent);
811 else
812 E_Scope := Ent;
813 end if;
815 loop
816 while not Is_Compilation_Unit (E_Scope) loop
817 E_Scope := Scope (E_Scope);
818 end loop;
820 -- If E_Scope is the same as C_Scope, it means that there
821 -- definitely was a local renaming or derivation, and we
822 -- are not yet out of the current unit.
824 exit when E_Scope /= C_Scope;
825 Ent := Alias (Ent);
826 E_Scope := Ent;
828 -- If no alias, there is a previous error
830 if No (Ent) then
831 return;
832 end if;
833 end loop;
834 end if;
836 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
837 return;
838 end if;
840 -- Find top level scope for called entity (not following renamings
841 -- or derivations). This is where the Elaborate_All will go if it
842 -- is needed. We start with the called entity, except in the case
843 -- of an initialization procedure outside the current package, where
844 -- the init proc is in the root package, and we start from the entity
845 -- of the name in the call.
847 declare
848 Ent : constant Entity_Id := Get_Referenced_Ent (N);
849 begin
850 if Is_Init_Proc (Ent)
851 and then not In_Same_Extended_Unit (N, Ent)
852 then
853 W_Scope := Scope (Ent);
854 else
855 W_Scope := E;
856 end if;
857 end;
859 -- Now loop through scopes to get to the enclosing compilation unit
861 while not Is_Compilation_Unit (W_Scope) loop
862 W_Scope := Scope (W_Scope);
863 end loop;
865 -- Now check if an elaborate_all (or dynamic check) is needed
867 if not Suppress_Elaboration_Warnings (Ent)
868 and then not Elaboration_Checks_Suppressed (Ent)
869 and then not Suppress_Elaboration_Warnings (E_Scope)
870 and then not Elaboration_Checks_Suppressed (E_Scope)
871 and then Elab_Warnings
872 and then Generate_Warnings
873 then
874 Generate_Elab_Warnings : declare
875 procedure Elab_Warning
876 (Msg_D : String;
877 Msg_S : String;
878 Ent : Node_Or_Entity_Id);
879 -- Generate a call to Error_Msg_NE with parameters Msg_D or
880 -- Msg_S (for dynamic or static elaboration model), N and Ent.
881 -- Msg_D is suppressed for the attribute reference case, since
882 -- we never raise Program_Error for an attribute reference.
884 ------------------
885 -- Elab_Warning --
886 ------------------
888 procedure Elab_Warning
889 (Msg_D : String;
890 Msg_S : String;
891 Ent : Node_Or_Entity_Id)
893 begin
894 if Dynamic_Elaboration_Checks then
895 if not Access_Case then
896 Error_Msg_NE (Msg_D, N, Ent);
897 end if;
898 else
899 Error_Msg_NE (Msg_S, N, Ent);
900 end if;
901 end Elab_Warning;
903 -- Start of processing for Generate_Elab_Warnings
905 begin
906 -- Instantiation case
908 if Inst_Case then
909 Elab_Warning
910 ("instantiation of& may raise Program_Error?",
911 "info: instantiation of& during elaboration?", Ent);
913 -- Indirect call case, warning only in static elaboration
914 -- case, because the attribute reference itself cannot raise
915 -- an exception.
917 elsif Access_Case then
918 Elab_Warning
919 ("", "info: access to& during elaboration?", Ent);
921 -- Subprogram call case
923 else
924 if Nkind (Name (N)) in N_Has_Entity
925 and then Is_Init_Proc (Entity (Name (N)))
926 and then Comes_From_Source (Ent)
927 then
928 Elab_Warning
929 ("implicit call to & may raise Program_Error?",
930 "info: implicit call to & during elaboration?",
931 Ent);
933 else
934 Elab_Warning
935 ("call to & may raise Program_Error?",
936 "info: call to & during elaboration?",
937 Ent);
938 end if;
939 end if;
941 Error_Msg_Qual_Level := Nat'Last;
943 if Nkind (N) in N_Subprogram_Instantiation then
944 Elab_Warning
945 ("\missing pragma Elaborate for&?",
946 "\info: implicit pragma Elaborate for& generated?",
947 W_Scope);
949 else
950 Elab_Warning
951 ("\missing pragma Elaborate_All for&?",
952 "\info: implicit pragma Elaborate_All for & generated?",
953 W_Scope);
954 end if;
955 end Generate_Elab_Warnings;
957 Error_Msg_Qual_Level := 0;
958 Output_Calls (N);
960 -- Set flag to prevent further warnings for same unit unless in
961 -- All_Errors_Mode.
963 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
964 Set_Suppress_Elaboration_Warnings (W_Scope, True);
965 end if;
966 end if;
968 -- Check for runtime elaboration check required
970 if Dynamic_Elaboration_Checks then
971 if not Elaboration_Checks_Suppressed (Ent)
972 and then not Elaboration_Checks_Suppressed (W_Scope)
973 and then not Elaboration_Checks_Suppressed (E_Scope)
974 and then not Cunit_SC
975 then
976 -- Runtime elaboration check required. Generate check of the
977 -- elaboration Boolean for the unit containing the entity.
979 -- Note that for this case, we do check the real unit (the one
980 -- from following renamings, since that is the issue!)
982 -- Could this possibly miss a useless but required PE???
984 Insert_Elab_Check (N,
985 Make_Attribute_Reference (Loc,
986 Attribute_Name => Name_Elaborated,
987 Prefix =>
988 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
990 -- Prevent duplicate elaboration checks on the same call,
991 -- which can happen if the body enclosing the call appears
992 -- itself in a call whose elaboration check is delayed.
994 if Nkind (N) in N_Subprogram_Call then
995 Set_No_Elaboration_Check (N);
996 end if;
997 end if;
999 -- Case of static elaboration model
1001 else
1002 -- Do not do anything if elaboration checks suppressed. Note that
1003 -- we check Ent here, not E, since we want the real entity for the
1004 -- body to see if checks are suppressed for it, not the dummy
1005 -- entry for renamings or derivations.
1007 if Elaboration_Checks_Suppressed (Ent)
1008 or else Elaboration_Checks_Suppressed (E_Scope)
1009 or else Elaboration_Checks_Suppressed (W_Scope)
1010 then
1011 null;
1013 -- Do not generate an Elaborate_All for finalization routines
1014 -- which perform partial clean up as part of initialization.
1016 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1017 null;
1019 -- Here we need to generate an implicit elaborate all
1021 else
1022 -- Generate elaborate_all warning unless suppressed
1024 if (Elab_Warnings and Generate_Warnings and not Inst_Case)
1025 and then not Suppress_Elaboration_Warnings (Ent)
1026 and then not Suppress_Elaboration_Warnings (E_Scope)
1027 and then not Suppress_Elaboration_Warnings (W_Scope)
1028 then
1029 Error_Msg_Node_2 := W_Scope;
1030 Error_Msg_NE
1031 ("call to& in elaboration code " &
1032 "requires pragma Elaborate_All on&?", N, E);
1033 end if;
1035 -- Set indication for binder to generate Elaborate_All
1037 Set_Elaboration_Constraint (N, E, W_Scope);
1038 end if;
1039 end if;
1041 -- Case of entity is in same unit as call or instantiation
1043 elsif not Inter_Unit_Only then
1044 Check_Internal_Call (N, Ent, Outer_Scope, E);
1045 end if;
1046 end Check_A_Call;
1048 -----------------------------
1049 -- Check_Bad_Instantiation --
1050 -----------------------------
1052 procedure Check_Bad_Instantiation (N : Node_Id) is
1053 Ent : Entity_Id;
1055 begin
1056 -- Nothing to do if we do not have an instantiation (happens in some
1057 -- error cases, and also in the formal package declaration case)
1059 if Nkind (N) not in N_Generic_Instantiation then
1060 return;
1062 -- Nothing to do if serious errors detected (avoid cascaded errors)
1064 elsif Serious_Errors_Detected /= 0 then
1065 return;
1067 -- Nothing to do if not in full analysis mode
1069 elsif not Full_Analysis then
1070 return;
1072 -- Nothing to do if inside a generic template
1074 elsif Inside_A_Generic then
1075 return;
1077 -- Nothing to do if a library level instantiation
1079 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1080 return;
1082 -- Nothing to do if we are compiling a proper body for semantic
1083 -- purposes only. The generic body may be in another proper body.
1085 elsif
1086 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1087 then
1088 return;
1089 end if;
1091 Ent := Get_Generic_Entity (N);
1093 -- The case we are interested in is when the generic spec is in the
1094 -- current declarative part
1096 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1097 or else not In_Same_Extended_Unit (N, Ent)
1098 then
1099 return;
1100 end if;
1102 -- If the generic entity is within a deeper instance than we are, then
1103 -- either the instantiation to which we refer itself caused an ABE, in
1104 -- which case that will be handled separately. Otherwise, we know that
1105 -- the body we need appears as needed at the point of the instantiation.
1106 -- If they are both at the same level but not within the same instance
1107 -- then the body of the generic will be in the earlier instance.
1109 declare
1110 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1111 D2 : constant Int := Instantiation_Depth (Sloc (N));
1113 begin
1114 if D1 > D2 then
1115 return;
1117 elsif D1 = D2
1118 and then Is_Generic_Instance (Scope (Ent))
1119 and then not In_Open_Scopes (Scope (Ent))
1120 then
1121 return;
1122 end if;
1123 end;
1125 -- Now we can proceed, if the entity being called has a completion,
1126 -- then we are definitely OK, since we have already seen the body.
1128 if Has_Completion (Ent) then
1129 return;
1130 end if;
1132 -- If there is no body, then nothing to do
1134 if not Has_Generic_Body (N) then
1135 return;
1136 end if;
1138 -- Here we definitely have a bad instantiation
1140 Error_Msg_NE
1141 ("?cannot instantiate& before body seen", N, Ent);
1143 if Present (Instance_Spec (N)) then
1144 Supply_Bodies (Instance_Spec (N));
1145 end if;
1147 Error_Msg_N
1148 ("\?Program_Error will be raised at run time", N);
1149 Insert_Elab_Check (N);
1150 Set_ABE_Is_Certain (N);
1151 end Check_Bad_Instantiation;
1153 ---------------------
1154 -- Check_Elab_Call --
1155 ---------------------
1157 procedure Check_Elab_Call
1158 (N : Node_Id;
1159 Outer_Scope : Entity_Id := Empty;
1160 In_Init_Proc : Boolean := False)
1162 Ent : Entity_Id;
1163 P : Node_Id;
1165 -- Start of processing for Check_Elab_Call
1167 begin
1168 -- If the call does not come from the main unit, there is nothing to
1169 -- check. Elaboration call from units in the context of the main unit
1170 -- will lead to semantic dependencies when those units are compiled.
1172 if not In_Extended_Main_Code_Unit (N) then
1173 return;
1174 end if;
1176 -- For an entry call, check relevant restriction
1178 if Nkind (N) = N_Entry_Call_Statement
1179 and then not In_Subprogram_Or_Concurrent_Unit
1180 then
1181 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1183 -- Nothing to do if this is not a call or attribute reference (happens
1184 -- in some error conditions, and in some cases where rewriting occurs).
1186 elsif Nkind (N) not in N_Subprogram_Call
1187 and then Nkind (N) /= N_Attribute_Reference
1188 then
1189 return;
1191 -- Nothing to do if this is a call already rewritten for elab checking
1193 elsif Nkind (Parent (N)) = N_Conditional_Expression then
1194 return;
1196 -- Nothing to do if inside a generic template
1198 elsif Inside_A_Generic
1199 and then No (Enclosing_Generic_Body (N))
1200 then
1201 return;
1202 end if;
1204 -- Here we have a call at elaboration time which must be checked
1206 if Debug_Flag_LL then
1207 Write_Str (" Check_Elab_Call: ");
1209 if No (Name (N))
1210 or else not Is_Entity_Name (Name (N))
1211 then
1212 Write_Str ("<<not entity name>> ");
1213 else
1214 Write_Name (Chars (Entity (Name (N))));
1215 end if;
1217 Write_Str (" call at ");
1218 Write_Location (Sloc (N));
1219 Write_Eol;
1220 end if;
1222 -- Climb up the tree to make sure we are not inside default expression
1223 -- of a parameter specification or a record component, since in both
1224 -- these cases, we will be doing the actual call later, not now, and it
1225 -- is at the time of the actual call (statically speaking) that we must
1226 -- do our static check, not at the time of its initial analysis).
1228 -- However, we have to check calls within component definitions (e.g.
1229 -- a function call that determines an array component bound), so we
1230 -- terminate the loop in that case.
1232 P := Parent (N);
1233 while Present (P) loop
1234 if Nkind_In (P, N_Parameter_Specification,
1235 N_Component_Declaration)
1236 then
1237 return;
1239 -- The call occurs within the constraint of a component,
1240 -- so it must be checked.
1242 elsif Nkind (P) = N_Component_Definition then
1243 exit;
1245 else
1246 P := Parent (P);
1247 end if;
1248 end loop;
1250 -- Stuff that happens only at the outer level
1252 if No (Outer_Scope) then
1253 Elab_Visited.Set_Last (0);
1255 -- Nothing to do if current scope is Standard (this is a bit odd, but
1256 -- it happens in the case of generic instantiations).
1258 C_Scope := Current_Scope;
1260 if C_Scope = Standard_Standard then
1261 return;
1262 end if;
1264 -- First case, we are in elaboration code
1266 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1267 if From_Elab_Code then
1269 -- Complain if call that comes from source in preelaborated unit
1270 -- and we are not inside a subprogram (i.e. we are in elab code).
1272 if Comes_From_Source (N)
1273 and then In_Preelaborated_Unit
1274 and then not In_Inlined_Body
1275 and then Nkind (N) /= N_Attribute_Reference
1276 then
1277 -- This is a warning in GNAT mode allowing such calls to be
1278 -- used in the predefined library with appropriate care.
1280 Error_Msg_Warn := GNAT_Mode;
1281 Error_Msg_N
1282 ("<non-static call not allowed in preelaborated unit", N);
1283 return;
1284 end if;
1286 -- Second case, we are inside a subprogram or concurrent unit, which
1287 -- means we are not in elaboration code.
1289 else
1290 -- In this case, the issue is whether we are inside the
1291 -- declarative part of the unit in which we live, or inside its
1292 -- statements. In the latter case, there is no issue of ABE calls
1293 -- at this level (a call from outside to the unit in which we live
1294 -- might cause an ABE, but that will be detected when we analyze
1295 -- that outer level call, as it recurses into the called unit).
1297 -- Climb up the tree, doing this test, and also testing for being
1298 -- inside a default expression, which, as discussed above, is not
1299 -- checked at this stage.
1301 declare
1302 P : Node_Id;
1303 L : List_Id;
1305 begin
1306 P := N;
1307 loop
1308 -- If we find a parentless subtree, it seems safe to assume
1309 -- that we are not in a declarative part and that no
1310 -- checking is required.
1312 if No (P) then
1313 return;
1314 end if;
1316 if Is_List_Member (P) then
1317 L := List_Containing (P);
1318 P := Parent (L);
1319 else
1320 L := No_List;
1321 P := Parent (P);
1322 end if;
1324 exit when Nkind (P) = N_Subunit;
1326 -- Filter out case of default expressions, where we do not
1327 -- do the check at this stage.
1329 if Nkind (P) = N_Parameter_Specification
1330 or else
1331 Nkind (P) = N_Component_Declaration
1332 then
1333 return;
1334 end if;
1336 -- A protected body has no elaboration code and contains
1337 -- only other bodies.
1339 if Nkind (P) = N_Protected_Body then
1340 return;
1342 elsif Nkind (P) = N_Subprogram_Body
1343 or else
1344 Nkind (P) = N_Task_Body
1345 or else
1346 Nkind (P) = N_Block_Statement
1347 or else
1348 Nkind (P) = N_Entry_Body
1349 then
1350 if L = Declarations (P) then
1351 exit;
1353 -- We are not in elaboration code, but we are doing
1354 -- dynamic elaboration checks, in this case, we still
1355 -- need to do the call, since the subprogram we are in
1356 -- could be called from another unit, also in dynamic
1357 -- elaboration check mode, at elaboration time.
1359 elsif Dynamic_Elaboration_Checks then
1361 -- We provide a debug flag to disable this check. That
1362 -- way we have an easy work around for regressions
1363 -- that are caused by this new check. This debug flag
1364 -- can be removed later.
1366 if Debug_Flag_DD then
1367 return;
1368 end if;
1370 -- Do the check in this case
1372 exit;
1374 elsif Nkind (P) = N_Task_Body then
1376 -- The check is deferred until Check_Task_Activation
1377 -- but we need to capture local suppress pragmas
1378 -- that may inhibit checks on this call.
1380 Ent := Get_Referenced_Ent (N);
1382 if No (Ent) then
1383 return;
1385 elsif Elaboration_Checks_Suppressed (Current_Scope)
1386 or else Elaboration_Checks_Suppressed (Ent)
1387 or else Elaboration_Checks_Suppressed (Scope (Ent))
1388 then
1389 Set_No_Elaboration_Check (N);
1390 end if;
1392 return;
1394 -- Static model, call is not in elaboration code, we
1395 -- never need to worry, because in the static model the
1396 -- top level caller always takes care of things.
1398 else
1399 return;
1400 end if;
1401 end if;
1402 end loop;
1403 end;
1404 end if;
1405 end if;
1407 Ent := Get_Referenced_Ent (N);
1409 if No (Ent) then
1410 return;
1411 end if;
1413 -- Nothing to do if this is a recursive call (i.e. a call to
1414 -- an entity that is already in the Elab_Call stack)
1416 for J in 1 .. Elab_Visited.Last loop
1417 if Ent = Elab_Visited.Table (J) then
1418 return;
1419 end if;
1420 end loop;
1422 -- See if we need to analyze this call. We analyze it if either of
1423 -- the following conditions is met:
1425 -- It is an inner level call (since in this case it was triggered
1426 -- by an outer level call from elaboration code), but only if the
1427 -- call is within the scope of the original outer level call.
1429 -- It is an outer level call from elaboration code, or the called
1430 -- entity is in the same elaboration scope.
1432 -- And in these cases, we will check both inter-unit calls and
1433 -- intra-unit (within a single unit) calls.
1435 C_Scope := Current_Scope;
1437 -- If not outer level call, then we follow it if it is within the
1438 -- original scope of the outer call.
1440 if Present (Outer_Scope)
1441 and then Within (Scope (Ent), Outer_Scope)
1442 then
1443 Set_C_Scope;
1444 Check_A_Call
1445 (N => N,
1446 E => Ent,
1447 Outer_Scope => Outer_Scope,
1448 Inter_Unit_Only => False,
1449 In_Init_Proc => In_Init_Proc);
1451 elsif Elaboration_Checks_Suppressed (Current_Scope) then
1452 null;
1454 elsif From_Elab_Code then
1455 Set_C_Scope;
1456 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1458 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1459 Set_C_Scope;
1460 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1462 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1463 -- is set, then we will do the check, but only in the inter-unit case
1464 -- (this is to accommodate unguarded elaboration calls from other units
1465 -- in which this same mode is set). We don't want warnings in this case,
1466 -- it would generate warnings having nothing to do with elaboration.
1468 elsif Dynamic_Elaboration_Checks then
1469 Set_C_Scope;
1470 Check_A_Call
1472 Ent,
1473 Standard_Standard,
1474 Inter_Unit_Only => True,
1475 Generate_Warnings => False);
1477 -- Otherwise nothing to do
1479 else
1480 return;
1481 end if;
1483 -- A call to an Init_Proc in elaboration code may bring additional
1484 -- dependencies, if some of the record components thereof have
1485 -- initializations that are function calls that come from source. We
1486 -- treat the current node as a call to each of these functions, to check
1487 -- their elaboration impact.
1489 if Is_Init_Proc (Ent)
1490 and then From_Elab_Code
1491 then
1492 Process_Init_Proc : declare
1493 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1495 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1496 -- Find subprogram calls within body of Init_Proc for Traverse
1497 -- instantiation below.
1499 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1500 -- Traversal procedure to find all calls with body of Init_Proc
1502 ---------------------
1503 -- Check_Init_Call --
1504 ---------------------
1506 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1507 Func : Entity_Id;
1509 begin
1510 if Nkind (Nod) in N_Subprogram_Call
1511 and then Is_Entity_Name (Name (Nod))
1512 then
1513 Func := Entity (Name (Nod));
1515 if Comes_From_Source (Func) then
1516 Check_A_Call
1517 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1518 end if;
1520 return OK;
1522 else
1523 return OK;
1524 end if;
1525 end Check_Init_Call;
1527 -- Start of processing for Process_Init_Proc
1529 begin
1530 if Nkind (Unit_Decl) = N_Subprogram_Body then
1531 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1532 end if;
1533 end Process_Init_Proc;
1534 end if;
1535 end Check_Elab_Call;
1537 -----------------------
1538 -- Check_Elab_Assign --
1539 -----------------------
1541 procedure Check_Elab_Assign (N : Node_Id) is
1542 Ent : Entity_Id;
1543 Scop : Entity_Id;
1545 Pkg_Spec : Entity_Id;
1546 Pkg_Body : Entity_Id;
1548 begin
1549 -- For record or array component, check prefix. If it is an access type,
1550 -- then there is nothing to do (we do not know what is being assigned),
1551 -- but otherwise this is an assignment to the prefix.
1553 if Nkind (N) = N_Indexed_Component
1554 or else
1555 Nkind (N) = N_Selected_Component
1556 or else
1557 Nkind (N) = N_Slice
1558 then
1559 if not Is_Access_Type (Etype (Prefix (N))) then
1560 Check_Elab_Assign (Prefix (N));
1561 end if;
1563 return;
1564 end if;
1566 -- For type conversion, check expression
1568 if Nkind (N) = N_Type_Conversion then
1569 Check_Elab_Assign (Expression (N));
1570 return;
1571 end if;
1573 -- Nothing to do if this is not an entity reference otherwise get entity
1575 if Is_Entity_Name (N) then
1576 Ent := Entity (N);
1577 else
1578 return;
1579 end if;
1581 -- What we are looking for is a reference in the body of a package that
1582 -- modifies a variable declared in the visible part of the package spec.
1584 if Present (Ent)
1585 and then Comes_From_Source (N)
1586 and then not Suppress_Elaboration_Warnings (Ent)
1587 and then Ekind (Ent) = E_Variable
1588 and then not In_Private_Part (Ent)
1589 and then Is_Library_Level_Entity (Ent)
1590 then
1591 Scop := Current_Scope;
1592 loop
1593 if No (Scop) or else Scop = Standard_Standard then
1594 return;
1595 elsif Ekind (Scop) = E_Package
1596 and then Is_Compilation_Unit (Scop)
1597 then
1598 exit;
1599 else
1600 Scop := Scope (Scop);
1601 end if;
1602 end loop;
1604 -- Here Scop points to the containing library package
1606 Pkg_Spec := Scop;
1607 Pkg_Body := Body_Entity (Pkg_Spec);
1609 -- All OK if the package has an Elaborate_Body pragma
1611 if Has_Pragma_Elaborate_Body (Scop) then
1612 return;
1613 end if;
1615 -- OK if entity being modified is not in containing package spec
1617 if not In_Same_Source_Unit (Scop, Ent) then
1618 return;
1619 end if;
1621 -- All OK if entity appears in generic package or generic instance.
1622 -- We just get too messed up trying to give proper warnings in the
1623 -- presence of generics. Better no message than a junk one.
1625 Scop := Scope (Ent);
1626 while Present (Scop) and then Scop /= Pkg_Spec loop
1627 if Ekind (Scop) = E_Generic_Package then
1628 return;
1629 elsif Ekind (Scop) = E_Package
1630 and then Is_Generic_Instance (Scop)
1631 then
1632 return;
1633 end if;
1635 Scop := Scope (Scop);
1636 end loop;
1638 -- All OK if in task, don't issue warnings there
1640 if In_Task_Activation then
1641 return;
1642 end if;
1644 -- OK if no package body
1646 if No (Pkg_Body) then
1647 return;
1648 end if;
1650 -- OK if reference is not in package body
1652 if not In_Same_Source_Unit (Pkg_Body, N) then
1653 return;
1654 end if;
1656 -- OK if package body has no handled statement sequence
1658 declare
1659 HSS : constant Node_Id :=
1660 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1661 begin
1662 if No (HSS) or else not Comes_From_Source (HSS) then
1663 return;
1664 end if;
1665 end;
1667 -- We definitely have a case of a modification of an entity in
1668 -- the package spec from the elaboration code of the package body.
1669 -- We may not give the warning (because there are some additional
1670 -- checks to avoid too many false positives), but it would be a good
1671 -- idea for the binder to try to keep the body elaboration close to
1672 -- the spec elaboration.
1674 Set_Elaborate_Body_Desirable (Pkg_Spec);
1676 -- All OK in gnat mode (we know what we are doing)
1678 if GNAT_Mode then
1679 return;
1680 end if;
1682 -- All OK if all warnings suppressed
1684 if Warning_Mode = Suppress then
1685 return;
1686 end if;
1688 -- All OK if elaboration checks suppressed for entity
1690 if Checks_May_Be_Suppressed (Ent)
1691 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1692 then
1693 return;
1694 end if;
1696 -- OK if the entity is initialized. Note that the No_Initialization
1697 -- flag usually means that the initialization has been rewritten into
1698 -- assignments, but that still counts for us.
1700 declare
1701 Decl : constant Node_Id := Declaration_Node (Ent);
1702 begin
1703 if Nkind (Decl) = N_Object_Declaration
1704 and then (Present (Expression (Decl))
1705 or else No_Initialization (Decl))
1706 then
1707 return;
1708 end if;
1709 end;
1711 -- Here is where we give the warning
1713 -- All OK if warnings suppressed on the entity
1715 if not Has_Warnings_Off (Ent) then
1716 Error_Msg_Sloc := Sloc (Ent);
1718 Error_Msg_NE
1719 ("?elaboration code may access& before it is initialized",
1720 N, Ent);
1721 Error_Msg_NE
1722 ("\?suggest adding pragma Elaborate_Body to spec of &",
1723 N, Scop);
1724 Error_Msg_N
1725 ("\?or an explicit initialization could be added #", N);
1726 end if;
1728 if not All_Errors_Mode then
1729 Set_Suppress_Elaboration_Warnings (Ent);
1730 end if;
1731 end if;
1732 end Check_Elab_Assign;
1734 ----------------------
1735 -- Check_Elab_Calls --
1736 ----------------------
1738 procedure Check_Elab_Calls is
1739 begin
1740 -- If expansion is disabled, do not generate any checks. Also skip
1741 -- checks if any subunits are missing because in either case we lack the
1742 -- full information that we need, and no object file will be created in
1743 -- any case.
1745 if not Expander_Active
1746 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1747 or else Subunits_Missing
1748 then
1749 return;
1750 end if;
1752 -- Skip delayed calls if we had any errors
1754 if Serious_Errors_Detected = 0 then
1755 Delaying_Elab_Checks := False;
1756 Expander_Mode_Save_And_Set (True);
1758 for J in Delay_Check.First .. Delay_Check.Last loop
1759 Push_Scope (Delay_Check.Table (J).Curscop);
1760 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1762 Check_Internal_Call_Continue (
1763 N => Delay_Check.Table (J).N,
1764 E => Delay_Check.Table (J).E,
1765 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1766 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1768 Pop_Scope;
1769 end loop;
1771 -- Set Delaying_Elab_Checks back on for next main compilation
1773 Expander_Mode_Restore;
1774 Delaying_Elab_Checks := True;
1775 end if;
1776 end Check_Elab_Calls;
1778 ------------------------------
1779 -- Check_Elab_Instantiation --
1780 ------------------------------
1782 procedure Check_Elab_Instantiation
1783 (N : Node_Id;
1784 Outer_Scope : Entity_Id := Empty)
1786 Ent : Entity_Id;
1788 begin
1789 -- Check for and deal with bad instantiation case. There is some
1790 -- duplicated code here, but we will worry about this later ???
1792 Check_Bad_Instantiation (N);
1794 if ABE_Is_Certain (N) then
1795 return;
1796 end if;
1798 -- Nothing to do if we do not have an instantiation (happens in some
1799 -- error cases, and also in the formal package declaration case)
1801 if Nkind (N) not in N_Generic_Instantiation then
1802 return;
1803 end if;
1805 -- Nothing to do if inside a generic template
1807 if Inside_A_Generic then
1808 return;
1809 end if;
1811 -- Nothing to do if the instantiation is not in the main unit
1813 if not In_Extended_Main_Code_Unit (N) then
1814 return;
1815 end if;
1817 Ent := Get_Generic_Entity (N);
1818 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1820 -- See if we need to analyze this instantiation. We analyze it if
1821 -- either of the following conditions is met:
1823 -- It is an inner level instantiation (since in this case it was
1824 -- triggered by an outer level call from elaboration code), but
1825 -- only if the instantiation is within the scope of the original
1826 -- outer level call.
1828 -- It is an outer level instantiation from elaboration code, or the
1829 -- instantiated entity is in the same elaboration scope.
1831 -- And in these cases, we will check both the inter-unit case and
1832 -- the intra-unit (within a single unit) case.
1834 C_Scope := Current_Scope;
1836 if Present (Outer_Scope)
1837 and then Within (Scope (Ent), Outer_Scope)
1838 then
1839 Set_C_Scope;
1840 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1842 elsif From_Elab_Code then
1843 Set_C_Scope;
1844 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1846 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1847 Set_C_Scope;
1848 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1850 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
1851 -- set, then we will do the check, but only in the inter-unit case (this
1852 -- is to accommodate unguarded elaboration calls from other units in
1853 -- which this same mode is set). We inhibit warnings in this case, since
1854 -- this instantiation is not occurring in elaboration code.
1856 elsif Dynamic_Elaboration_Checks then
1857 Set_C_Scope;
1858 Check_A_Call
1860 Ent,
1861 Standard_Standard,
1862 Inter_Unit_Only => True,
1863 Generate_Warnings => False);
1865 else
1866 return;
1867 end if;
1868 end Check_Elab_Instantiation;
1870 -------------------------
1871 -- Check_Internal_Call --
1872 -------------------------
1874 procedure Check_Internal_Call
1875 (N : Node_Id;
1876 E : Entity_Id;
1877 Outer_Scope : Entity_Id;
1878 Orig_Ent : Entity_Id)
1880 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1882 begin
1883 -- If not function or procedure call or instantiation, then ignore
1884 -- call (this happens in some error case and rewriting cases)
1886 if Nkind (N) /= N_Function_Call
1887 and then
1888 Nkind (N) /= N_Procedure_Call_Statement
1889 and then
1890 not Inst_Case
1891 then
1892 return;
1894 -- Nothing to do if this is a call or instantiation that has
1895 -- already been found to be a sure ABE
1897 elsif ABE_Is_Certain (N) then
1898 return;
1900 -- Nothing to do if errors already detected (avoid cascaded errors)
1902 elsif Serious_Errors_Detected /= 0 then
1903 return;
1905 -- Nothing to do if not in full analysis mode
1907 elsif not Full_Analysis then
1908 return;
1910 -- Nothing to do if analyzing in special spec-expression mode, since the
1911 -- call is not actually being made at this time.
1913 elsif In_Spec_Expression then
1914 return;
1916 -- Nothing to do for call to intrinsic subprogram
1918 elsif Is_Intrinsic_Subprogram (E) then
1919 return;
1921 -- No need to trace local calls if checking task activation, because
1922 -- other local bodies are elaborated already.
1924 elsif In_Task_Activation then
1925 return;
1927 -- Nothing to do if call is within a generic unit
1929 elsif Inside_A_Generic then
1930 return;
1931 end if;
1933 -- Delay this call if we are still delaying calls
1935 if Delaying_Elab_Checks then
1936 Delay_Check.Append (
1937 (N => N,
1938 E => E,
1939 Orig_Ent => Orig_Ent,
1940 Curscop => Current_Scope,
1941 Outer_Scope => Outer_Scope,
1942 From_Elab_Code => From_Elab_Code));
1943 return;
1945 -- Otherwise, call phase 2 continuation right now
1947 else
1948 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1949 end if;
1950 end Check_Internal_Call;
1952 ----------------------------------
1953 -- Check_Internal_Call_Continue --
1954 ----------------------------------
1956 procedure Check_Internal_Call_Continue
1957 (N : Node_Id;
1958 E : Entity_Id;
1959 Outer_Scope : Entity_Id;
1960 Orig_Ent : Entity_Id)
1962 Loc : constant Source_Ptr := Sloc (N);
1963 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1965 Sbody : Node_Id;
1966 Ebody : Entity_Id;
1968 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
1969 -- Function applied to each node as we traverse the body. Checks for
1970 -- call or entity reference that needs checking, and if so checks it.
1971 -- Always returns OK, so entire tree is traversed, except that as
1972 -- described below subprogram bodies are skipped for now.
1974 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
1975 -- Traverse procedure using above Find_Elab_Reference function
1977 -------------------------
1978 -- Find_Elab_Reference --
1979 -------------------------
1981 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
1982 Actual : Node_Id;
1984 begin
1985 -- If user has specified that there are no entry calls in elaboration
1986 -- code, do not trace past an accept statement, because the rendez-
1987 -- vous will happen after elaboration.
1989 if (Nkind (Original_Node (N)) = N_Accept_Statement
1990 or else Nkind (Original_Node (N)) = N_Selective_Accept)
1991 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1992 then
1993 return Abandon;
1995 -- If we have a function call, check it
1997 elsif Nkind (N) = N_Function_Call then
1998 Check_Elab_Call (N, Outer_Scope);
1999 return OK;
2001 -- If we have a procedure call, check the call, and also check
2002 -- arguments that are assignments (OUT or IN OUT mode formals).
2004 elsif Nkind (N) = N_Procedure_Call_Statement then
2005 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2007 Actual := First_Actual (N);
2008 while Present (Actual) loop
2009 if Known_To_Be_Assigned (Actual) then
2010 Check_Elab_Assign (Actual);
2011 end if;
2013 Next_Actual (Actual);
2014 end loop;
2016 return OK;
2018 -- If we have an access attribute for a subprogram, check
2019 -- it. Suppress this behavior under debug flag.
2021 elsif not Debug_Flag_Dot_UU
2022 and then Nkind (N) = N_Attribute_Reference
2023 and then (Attribute_Name (N) = Name_Access
2024 or else
2025 Attribute_Name (N) = Name_Unrestricted_Access)
2026 and then Is_Entity_Name (Prefix (N))
2027 and then Is_Subprogram (Entity (Prefix (N)))
2028 then
2029 Check_Elab_Call (N, Outer_Scope);
2030 return OK;
2032 -- If we have a generic instantiation, check it
2034 elsif Nkind (N) in N_Generic_Instantiation then
2035 Check_Elab_Instantiation (N, Outer_Scope);
2036 return OK;
2038 -- Skip subprogram bodies that come from source (wait for call to
2039 -- analyze these). The reason for the come from source test is to
2040 -- avoid catching task bodies.
2042 -- For task bodies, we should really avoid these too, waiting for the
2043 -- task activation, but that's too much trouble to catch for now, so
2044 -- we go in unconditionally. This is not so terrible, it means the
2045 -- error backtrace is not quite complete, and we are too eager to
2046 -- scan bodies of tasks that are unused, but this is hardly very
2047 -- significant!
2049 elsif Nkind (N) = N_Subprogram_Body
2050 and then Comes_From_Source (N)
2051 then
2052 return Skip;
2054 elsif Nkind (N) = N_Assignment_Statement
2055 and then Comes_From_Source (N)
2056 then
2057 Check_Elab_Assign (Name (N));
2058 return OK;
2060 else
2061 return OK;
2062 end if;
2063 end Find_Elab_Reference;
2065 -- Start of processing for Check_Internal_Call_Continue
2067 begin
2068 -- Save outer level call if at outer level
2070 if Elab_Call.Last = 0 then
2071 Outer_Level_Sloc := Loc;
2072 end if;
2074 Elab_Visited.Append (E);
2076 -- If the call is to a function that renames a literal, no check
2077 -- is needed.
2079 if Ekind (E) = E_Enumeration_Literal then
2080 return;
2081 end if;
2083 Sbody := Unit_Declaration_Node (E);
2085 if Nkind (Sbody) /= N_Subprogram_Body
2086 and then
2087 Nkind (Sbody) /= N_Package_Body
2088 then
2089 Ebody := Corresponding_Body (Sbody);
2091 if No (Ebody) then
2092 return;
2093 else
2094 Sbody := Unit_Declaration_Node (Ebody);
2095 end if;
2096 end if;
2098 -- If the body appears after the outer level call or instantiation then
2099 -- we have an error case handled below.
2101 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2102 and then not In_Task_Activation
2103 then
2104 null;
2106 -- If we have the instantiation case we are done, since we now
2107 -- know that the body of the generic appeared earlier.
2109 elsif Inst_Case then
2110 return;
2112 -- Otherwise we have a call, so we trace through the called body to see
2113 -- if it has any problems.
2115 else
2116 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2118 Elab_Call.Append ((Cloc => Loc, Ent => E));
2120 if Debug_Flag_LL then
2121 Write_Str ("Elab_Call.Last = ");
2122 Write_Int (Int (Elab_Call.Last));
2123 Write_Str (" Ent = ");
2124 Write_Name (Chars (E));
2125 Write_Str (" at ");
2126 Write_Location (Sloc (N));
2127 Write_Eol;
2128 end if;
2130 -- Now traverse declarations and statements of subprogram body. Note
2131 -- that we cannot simply Traverse (Sbody), since traverse does not
2132 -- normally visit subprogram bodies.
2134 declare
2135 Decl : Node_Id;
2136 begin
2137 Decl := First (Declarations (Sbody));
2138 while Present (Decl) loop
2139 Traverse (Decl);
2140 Next (Decl);
2141 end loop;
2142 end;
2144 Traverse (Handled_Statement_Sequence (Sbody));
2146 Elab_Call.Decrement_Last;
2147 return;
2148 end if;
2150 -- Here is the case of calling a subprogram where the body has not yet
2151 -- been encountered. A warning message is needed, except if this is the
2152 -- case of appearing within an aspect specification that results in
2153 -- a check call, we do not really have such a situation, so no warning
2154 -- is needed (e.g. the case of a precondition, where the call appears
2155 -- textually before the body, but in actual fact is moved to the
2156 -- appropriate subprogram body and so does not need a check).
2158 declare
2159 P : Node_Id;
2160 begin
2161 P := Parent (N);
2162 loop
2163 if Nkind (P) in N_Subexpr then
2164 P := Parent (P);
2165 elsif Nkind (P) = N_If_Statement
2166 and then Nkind (Original_Node (P)) = N_Pragma
2167 and then Present (Corresponding_Aspect (Original_Node (P)))
2168 then
2169 return;
2170 else
2171 exit;
2172 end if;
2173 end loop;
2174 end;
2176 -- Not that special case, warning and dynamic check is required
2178 -- If we have nothing in the call stack, then this is at the outer
2179 -- level, and the ABE is bound to occur.
2181 if Elab_Call.Last = 0 then
2182 if Inst_Case then
2183 Error_Msg_NE
2184 ("?cannot instantiate& before body seen", N, Orig_Ent);
2185 else
2186 Error_Msg_NE
2187 ("?cannot call& before body seen", N, Orig_Ent);
2188 end if;
2190 Error_Msg_N
2191 ("\?Program_Error will be raised at run time", N);
2192 Insert_Elab_Check (N);
2194 -- Call is not at outer level
2196 else
2197 -- Deal with dynamic elaboration check
2199 if not Elaboration_Checks_Suppressed (E) then
2200 Set_Elaboration_Entity_Required (E);
2202 -- Case of no elaboration entity allocated yet
2204 if No (Elaboration_Entity (E)) then
2206 -- Create object declaration for elaboration entity, and put it
2207 -- just in front of the spec of the subprogram or generic unit,
2208 -- in the same scope as this unit.
2210 declare
2211 Loce : constant Source_Ptr := Sloc (E);
2212 Ent : constant Entity_Id :=
2213 Make_Defining_Identifier (Loc,
2214 Chars => New_External_Name (Chars (E), 'E'));
2216 begin
2217 Set_Elaboration_Entity (E, Ent);
2218 Push_Scope (Scope (E));
2220 Insert_Action (Declaration_Node (E),
2221 Make_Object_Declaration (Loce,
2222 Defining_Identifier => Ent,
2223 Object_Definition =>
2224 New_Occurrence_Of (Standard_Short_Integer, Loce),
2225 Expression =>
2226 Make_Integer_Literal (Loc, Uint_0)));
2228 -- Set elaboration flag at the point of the body
2230 Set_Elaboration_Flag (Sbody, E);
2232 -- Kill current value indication. This is necessary because
2233 -- the tests of this flag are inserted out of sequence and
2234 -- must not pick up bogus indications of the wrong constant
2235 -- value. Also, this is never a true constant, since one way
2236 -- or another, it gets reset.
2238 Set_Current_Value (Ent, Empty);
2239 Set_Last_Assignment (Ent, Empty);
2240 Set_Is_True_Constant (Ent, False);
2241 Pop_Scope;
2242 end;
2243 end if;
2245 -- Generate check of the elaboration counter
2247 Insert_Elab_Check (N,
2248 Make_Attribute_Reference (Loc,
2249 Attribute_Name => Name_Elaborated,
2250 Prefix => New_Occurrence_Of (E, Loc)));
2251 end if;
2253 -- Generate the warning
2255 if not Suppress_Elaboration_Warnings (E)
2256 and then not Elaboration_Checks_Suppressed (E)
2257 then
2258 if Inst_Case then
2259 Error_Msg_NE
2260 ("instantiation of& may occur before body is seen?",
2261 N, Orig_Ent);
2262 else
2263 Error_Msg_NE
2264 ("call to& may occur before body is seen?", N, Orig_Ent);
2265 end if;
2267 Error_Msg_N
2268 ("\Program_Error may be raised at run time?", N);
2270 Output_Calls (N);
2271 end if;
2272 end if;
2274 -- Set flag to suppress further warnings on same subprogram
2275 -- unless in all errors mode
2277 if not All_Errors_Mode then
2278 Set_Suppress_Elaboration_Warnings (E);
2279 end if;
2280 end Check_Internal_Call_Continue;
2282 ---------------------------
2283 -- Check_Task_Activation --
2284 ---------------------------
2286 procedure Check_Task_Activation (N : Node_Id) is
2287 Loc : constant Source_Ptr := Sloc (N);
2288 Inter_Procs : constant Elist_Id := New_Elmt_List;
2289 Intra_Procs : constant Elist_Id := New_Elmt_List;
2290 Ent : Entity_Id;
2291 P : Entity_Id;
2292 Task_Scope : Entity_Id;
2293 Cunit_SC : Boolean := False;
2294 Decl : Node_Id;
2295 Elmt : Elmt_Id;
2296 Enclosing : Entity_Id;
2298 procedure Add_Task_Proc (Typ : Entity_Id);
2299 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2300 -- For record types, this procedure recurses over component types.
2302 procedure Collect_Tasks (Decls : List_Id);
2303 -- Collect the types of the tasks that are to be activated in the given
2304 -- list of declarations, in order to perform elaboration checks on the
2305 -- corresponding task procedures which are called implicitly here.
2307 function Outer_Unit (E : Entity_Id) return Entity_Id;
2308 -- find enclosing compilation unit of Entity, ignoring subunits, or
2309 -- else enclosing subprogram. If E is not a package, there is no need
2310 -- for inter-unit elaboration checks.
2312 -------------------
2313 -- Add_Task_Proc --
2314 -------------------
2316 procedure Add_Task_Proc (Typ : Entity_Id) is
2317 Comp : Entity_Id;
2318 Proc : Entity_Id := Empty;
2320 begin
2321 if Is_Task_Type (Typ) then
2322 Proc := Get_Task_Body_Procedure (Typ);
2324 elsif Is_Array_Type (Typ)
2325 and then Has_Task (Base_Type (Typ))
2326 then
2327 Add_Task_Proc (Component_Type (Typ));
2329 elsif Is_Record_Type (Typ)
2330 and then Has_Task (Base_Type (Typ))
2331 then
2332 Comp := First_Component (Typ);
2333 while Present (Comp) loop
2334 Add_Task_Proc (Etype (Comp));
2335 Comp := Next_Component (Comp);
2336 end loop;
2337 end if;
2339 -- If the task type is another unit, we will perform the usual
2340 -- elaboration check on its enclosing unit. If the type is in the
2341 -- same unit, we can trace the task body as for an internal call,
2342 -- but we only need to examine other external calls, because at
2343 -- the point the task is activated, internal subprogram bodies
2344 -- will have been elaborated already. We keep separate lists for
2345 -- each kind of task.
2347 -- Skip this test if errors have occurred, since in this case
2348 -- we can get false indications.
2350 if Serious_Errors_Detected /= 0 then
2351 return;
2352 end if;
2354 if Present (Proc) then
2355 if Outer_Unit (Scope (Proc)) = Enclosing then
2357 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2358 and then
2359 (not Is_Generic_Instance (Scope (Proc))
2360 or else
2361 Scope (Proc) = Scope (Defining_Identifier (Decl)))
2362 then
2363 Error_Msg_N
2364 ("task will be activated before elaboration of its body?",
2365 Decl);
2366 Error_Msg_N
2367 ("\Program_Error will be raised at run time?", Decl);
2369 elsif
2370 Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
2371 then
2372 Append_Elmt (Proc, Intra_Procs);
2373 end if;
2375 else
2376 -- No need for multiple entries of the same type
2378 Elmt := First_Elmt (Inter_Procs);
2379 while Present (Elmt) loop
2380 if Node (Elmt) = Proc then
2381 return;
2382 end if;
2384 Next_Elmt (Elmt);
2385 end loop;
2387 Append_Elmt (Proc, Inter_Procs);
2388 end if;
2389 end if;
2390 end Add_Task_Proc;
2392 -------------------
2393 -- Collect_Tasks --
2394 -------------------
2396 procedure Collect_Tasks (Decls : List_Id) is
2397 begin
2398 if Present (Decls) then
2399 Decl := First (Decls);
2400 while Present (Decl) loop
2401 if Nkind (Decl) = N_Object_Declaration
2402 and then Has_Task (Etype (Defining_Identifier (Decl)))
2403 then
2404 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2405 end if;
2407 Next (Decl);
2408 end loop;
2409 end if;
2410 end Collect_Tasks;
2412 ----------------
2413 -- Outer_Unit --
2414 ----------------
2416 function Outer_Unit (E : Entity_Id) return Entity_Id is
2417 Outer : Entity_Id;
2419 begin
2420 Outer := E;
2421 while Present (Outer) loop
2422 if Elaboration_Checks_Suppressed (Outer) then
2423 Cunit_SC := True;
2424 end if;
2426 exit when Is_Child_Unit (Outer)
2427 or else Scope (Outer) = Standard_Standard
2428 or else Ekind (Outer) /= E_Package;
2429 Outer := Scope (Outer);
2430 end loop;
2432 return Outer;
2433 end Outer_Unit;
2435 -- Start of processing for Check_Task_Activation
2437 begin
2438 Enclosing := Outer_Unit (Current_Scope);
2440 -- Find all tasks declared in the current unit
2442 if Nkind (N) = N_Package_Body then
2443 P := Unit_Declaration_Node (Corresponding_Spec (N));
2445 Collect_Tasks (Declarations (N));
2446 Collect_Tasks (Visible_Declarations (Specification (P)));
2447 Collect_Tasks (Private_Declarations (Specification (P)));
2449 elsif Nkind (N) = N_Package_Declaration then
2450 Collect_Tasks (Visible_Declarations (Specification (N)));
2451 Collect_Tasks (Private_Declarations (Specification (N)));
2453 else
2454 Collect_Tasks (Declarations (N));
2455 end if;
2457 -- We only perform detailed checks in all tasks are library level
2458 -- entities. If the master is a subprogram or task, activation will
2459 -- depend on the activation of the master itself.
2461 -- Should dynamic checks be added in the more general case???
2463 if Ekind (Enclosing) /= E_Package then
2464 return;
2465 end if;
2467 -- For task types defined in other units, we want the unit containing
2468 -- the task body to be elaborated before the current one.
2470 Elmt := First_Elmt (Inter_Procs);
2471 while Present (Elmt) loop
2472 Ent := Node (Elmt);
2473 Task_Scope := Outer_Unit (Scope (Ent));
2475 if not Is_Compilation_Unit (Task_Scope) then
2476 null;
2478 elsif Suppress_Elaboration_Warnings (Task_Scope)
2479 or else Elaboration_Checks_Suppressed (Task_Scope)
2480 then
2481 null;
2483 elsif Dynamic_Elaboration_Checks then
2484 if not Elaboration_Checks_Suppressed (Ent)
2485 and then not Cunit_SC
2486 and then
2487 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2488 then
2489 -- Runtime elaboration check required. Generate check of the
2490 -- elaboration counter for the unit containing the entity.
2492 Insert_Elab_Check (N,
2493 Make_Attribute_Reference (Loc,
2494 Attribute_Name => Name_Elaborated,
2495 Prefix =>
2496 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2497 end if;
2499 else
2500 -- Force the binder to elaborate other unit first
2502 if not Suppress_Elaboration_Warnings (Ent)
2503 and then not Elaboration_Checks_Suppressed (Ent)
2504 and then Elab_Warnings
2505 and then not Suppress_Elaboration_Warnings (Task_Scope)
2506 and then not Elaboration_Checks_Suppressed (Task_Scope)
2507 then
2508 Error_Msg_Node_2 := Task_Scope;
2509 Error_Msg_NE
2510 ("activation of an instance of task type&" &
2511 " requires pragma Elaborate_All on &?", N, Ent);
2512 end if;
2514 Activate_Elaborate_All_Desirable (N, Task_Scope);
2515 Set_Suppress_Elaboration_Warnings (Task_Scope);
2516 end if;
2518 Next_Elmt (Elmt);
2519 end loop;
2521 -- For tasks declared in the current unit, trace other calls within
2522 -- the task procedure bodies, which are available.
2524 In_Task_Activation := True;
2526 Elmt := First_Elmt (Intra_Procs);
2527 while Present (Elmt) loop
2528 Ent := Node (Elmt);
2529 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2530 Next_Elmt (Elmt);
2531 end loop;
2533 In_Task_Activation := False;
2534 end Check_Task_Activation;
2536 --------------------------------
2537 -- Set_Elaboration_Constraint --
2538 --------------------------------
2540 procedure Set_Elaboration_Constraint
2541 (Call : Node_Id;
2542 Subp : Entity_Id;
2543 Scop : Entity_Id)
2545 Elab_Unit : Entity_Id;
2546 Init_Call : constant Boolean :=
2547 Chars (Subp) = Name_Initialize
2548 and then Comes_From_Source (Subp)
2549 and then Present (Parameter_Associations (Call))
2550 and then Is_Controlled (Etype (First_Actual (Call)));
2551 begin
2552 -- If the unit is mentioned in a with_clause of the current unit, it is
2553 -- visible, and we can set the elaboration flag.
2555 if Is_Immediately_Visible (Scop)
2556 or else (Is_Child_Unit (Scop) and then Is_Visible_Child_Unit (Scop))
2557 then
2558 Activate_Elaborate_All_Desirable (Call, Scop);
2559 Set_Suppress_Elaboration_Warnings (Scop, True);
2560 return;
2561 end if;
2563 -- If this is not an initialization call or a call using object notation
2564 -- we know that the unit of the called entity is in the context, and
2565 -- we can set the flag as well. The unit need not be visible if the call
2566 -- occurs within an instantiation.
2568 if Is_Init_Proc (Subp)
2569 or else Init_Call
2570 or else Nkind (Original_Node (Call)) = N_Selected_Component
2571 then
2572 null; -- detailed processing follows.
2574 else
2575 Activate_Elaborate_All_Desirable (Call, Scop);
2576 Set_Suppress_Elaboration_Warnings (Scop, True);
2577 return;
2578 end if;
2580 -- If the unit is not in the context, there must be an intermediate unit
2581 -- that is, on which we need to place to elaboration flag. This happens
2582 -- with init proc calls.
2584 if Is_Init_Proc (Subp)
2585 or else Init_Call
2586 then
2587 -- The initialization call is on an object whose type is not declared
2588 -- in the same scope as the subprogram. The type of the object must
2589 -- be a subtype of the type of operation. This object is the first
2590 -- actual in the call.
2592 declare
2593 Typ : constant Entity_Id :=
2594 Etype (First (Parameter_Associations (Call)));
2595 begin
2596 Elab_Unit := Scope (Typ);
2597 while (Present (Elab_Unit))
2598 and then not Is_Compilation_Unit (Elab_Unit)
2599 loop
2600 Elab_Unit := Scope (Elab_Unit);
2601 end loop;
2602 end;
2604 -- If original node uses selected component notation, the prefix is
2605 -- visible and determines the scope that must be elaborated. After
2606 -- rewriting, the prefix is the first actual in the call.
2608 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2609 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2611 -- Not one of special cases above
2613 else
2614 -- Using previously computed scope. If the elaboration check is
2615 -- done after analysis, the scope is not visible any longer, but
2616 -- must still be in the context.
2618 Elab_Unit := Scop;
2619 end if;
2621 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2622 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2623 end Set_Elaboration_Constraint;
2625 ------------------------
2626 -- Get_Referenced_Ent --
2627 ------------------------
2629 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
2630 Nam : Node_Id;
2632 begin
2633 if Nkind (N) = N_Attribute_Reference then
2634 Nam := Prefix (N);
2635 else
2636 Nam := Name (N);
2637 end if;
2639 if No (Nam) then
2640 return Empty;
2641 elsif Nkind (Nam) = N_Selected_Component then
2642 return Entity (Selector_Name (Nam));
2643 elsif not Is_Entity_Name (Nam) then
2644 return Empty;
2645 else
2646 return Entity (Nam);
2647 end if;
2648 end Get_Referenced_Ent;
2650 ----------------------
2651 -- Has_Generic_Body --
2652 ----------------------
2654 function Has_Generic_Body (N : Node_Id) return Boolean is
2655 Ent : constant Entity_Id := Get_Generic_Entity (N);
2656 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2657 Scop : Entity_Id;
2659 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2660 -- Determine if the list of nodes headed by N and linked by Next
2661 -- contains a package body for the package spec entity E, and if so
2662 -- return the package body. If not, then returns Empty.
2664 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2665 -- This procedure is called load the unit whose name is given by Nam.
2666 -- This unit is being loaded to see whether it contains an optional
2667 -- generic body. The returned value is the loaded unit, which is always
2668 -- a package body (only package bodies can contain other entities in the
2669 -- sense in which Has_Generic_Body is interested). We only attempt to
2670 -- load bodies if we are generating code. If we are in semantics check
2671 -- only mode, then it would be wrong to load bodies that are not
2672 -- required from a semantic point of view, so in this case we return
2673 -- Empty. The result is that the caller may incorrectly decide that a
2674 -- generic spec does not have a body when in fact it does, but the only
2675 -- harm in this is that some warnings on elaboration problems may be
2676 -- lost in semantic checks only mode, which is not big loss. We also
2677 -- return Empty if we go for a body and it is not there.
2679 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2680 -- PE is the entity for a package spec. This function locates the
2681 -- corresponding package body, returning Empty if none is found. The
2682 -- package body returned is fully parsed but may not yet be analyzed,
2683 -- so only syntactic fields should be referenced.
2685 ------------------
2686 -- Find_Body_In --
2687 ------------------
2689 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2690 Nod : Node_Id;
2692 begin
2693 Nod := N;
2694 while Present (Nod) loop
2696 -- If we found the package body we are looking for, return it
2698 if Nkind (Nod) = N_Package_Body
2699 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2700 then
2701 return Nod;
2703 -- If we found the stub for the body, go after the subunit,
2704 -- loading it if necessary.
2706 elsif Nkind (Nod) = N_Package_Body_Stub
2707 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2708 then
2709 if Present (Library_Unit (Nod)) then
2710 return Unit (Library_Unit (Nod));
2712 else
2713 return Load_Package_Body (Get_Unit_Name (Nod));
2714 end if;
2716 -- If neither package body nor stub, keep looking on chain
2718 else
2719 Next (Nod);
2720 end if;
2721 end loop;
2723 return Empty;
2724 end Find_Body_In;
2726 -----------------------
2727 -- Load_Package_Body --
2728 -----------------------
2730 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2731 U : Unit_Number_Type;
2733 begin
2734 if Operating_Mode /= Generate_Code then
2735 return Empty;
2736 else
2737 U :=
2738 Load_Unit
2739 (Load_Name => Nam,
2740 Required => False,
2741 Subunit => False,
2742 Error_Node => N);
2744 if U = No_Unit then
2745 return Empty;
2746 else
2747 return Unit (Cunit (U));
2748 end if;
2749 end if;
2750 end Load_Package_Body;
2752 -------------------------------
2753 -- Locate_Corresponding_Body --
2754 -------------------------------
2756 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2757 Spec : constant Node_Id := Declaration_Node (PE);
2758 Decl : constant Node_Id := Parent (Spec);
2759 Scop : constant Entity_Id := Scope (PE);
2760 PBody : Node_Id;
2762 begin
2763 if Is_Library_Level_Entity (PE) then
2765 -- If package is a library unit that requires a body, we have no
2766 -- choice but to go after that body because it might contain an
2767 -- optional body for the original generic package.
2769 if Unit_Requires_Body (PE) then
2771 -- Load the body. Note that we are a little careful here to use
2772 -- Spec to get the unit number, rather than PE or Decl, since
2773 -- in the case where the package is itself a library level
2774 -- instantiation, Spec will properly reference the generic
2775 -- template, which is what we really want.
2777 return
2778 Load_Package_Body
2779 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2781 -- But if the package is a library unit that does NOT require
2782 -- a body, then no body is permitted, so we are sure that there
2783 -- is no body for the original generic package.
2785 else
2786 return Empty;
2787 end if;
2789 -- Otherwise look and see if we are embedded in a further package
2791 elsif Is_Package_Or_Generic_Package (Scop) then
2793 -- If so, get the body of the enclosing package, and look in
2794 -- its package body for the package body we are looking for.
2796 PBody := Locate_Corresponding_Body (Scop);
2798 if No (PBody) then
2799 return Empty;
2800 else
2801 return Find_Body_In (PE, First (Declarations (PBody)));
2802 end if;
2804 -- If we are not embedded in a further package, then the body
2805 -- must be in the same declarative part as we are.
2807 else
2808 return Find_Body_In (PE, Next (Decl));
2809 end if;
2810 end Locate_Corresponding_Body;
2812 -- Start of processing for Has_Generic_Body
2814 begin
2815 if Present (Corresponding_Body (Decl)) then
2816 return True;
2818 elsif Unit_Requires_Body (Ent) then
2819 return True;
2821 -- Compilation units cannot have optional bodies
2823 elsif Is_Compilation_Unit (Ent) then
2824 return False;
2826 -- Otherwise look at what scope we are in
2828 else
2829 Scop := Scope (Ent);
2831 -- Case of entity is in other than a package spec, in this case
2832 -- the body, if present, must be in the same declarative part.
2834 if not Is_Package_Or_Generic_Package (Scop) then
2835 declare
2836 P : Node_Id;
2838 begin
2839 -- Declaration node may get us a spec, so if so, go to
2840 -- the parent declaration.
2842 P := Declaration_Node (Ent);
2843 while not Is_List_Member (P) loop
2844 P := Parent (P);
2845 end loop;
2847 return Present (Find_Body_In (Ent, Next (P)));
2848 end;
2850 -- If the entity is in a package spec, then we have to locate
2851 -- the corresponding package body, and look there.
2853 else
2854 declare
2855 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2857 begin
2858 if No (PBody) then
2859 return False;
2860 else
2861 return
2862 Present
2863 (Find_Body_In (Ent, (First (Declarations (PBody)))));
2864 end if;
2865 end;
2866 end if;
2867 end if;
2868 end Has_Generic_Body;
2870 -----------------------
2871 -- Insert_Elab_Check --
2872 -----------------------
2874 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2875 Nod : Node_Id;
2876 Loc : constant Source_Ptr := Sloc (N);
2878 begin
2879 -- If expansion is disabled, do not generate any checks. Also
2880 -- skip checks if any subunits are missing because in either
2881 -- case we lack the full information that we need, and no object
2882 -- file will be created in any case.
2884 if not Expander_Active or else Subunits_Missing then
2885 return;
2886 end if;
2888 -- If we have a generic instantiation, where Instance_Spec is set,
2889 -- then this field points to a generic instance spec that has
2890 -- been inserted before the instantiation node itself, so that
2891 -- is where we want to insert a check.
2893 if Nkind (N) in N_Generic_Instantiation
2894 and then Present (Instance_Spec (N))
2895 then
2896 Nod := Instance_Spec (N);
2897 else
2898 Nod := N;
2899 end if;
2901 -- If we are inserting at the top level, insert in Aux_Decls
2903 if Nkind (Parent (Nod)) = N_Compilation_Unit then
2904 declare
2905 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2906 R : Node_Id;
2908 begin
2909 if No (C) then
2910 R :=
2911 Make_Raise_Program_Error (Loc,
2912 Reason => PE_Access_Before_Elaboration);
2913 else
2914 R :=
2915 Make_Raise_Program_Error (Loc,
2916 Condition => Make_Op_Not (Loc, C),
2917 Reason => PE_Access_Before_Elaboration);
2918 end if;
2920 if No (Declarations (ADN)) then
2921 Set_Declarations (ADN, New_List (R));
2922 else
2923 Append_To (Declarations (ADN), R);
2924 end if;
2926 Analyze (R);
2927 end;
2929 -- Otherwise just insert before the node in question. However, if
2930 -- the context of the call has already been analyzed, an insertion
2931 -- will not work if it depends on subsequent expansion (e.g. a call in
2932 -- a branch of a short-circuit). In that case we replace the call with
2933 -- a conditional expression, or with a Raise if it is unconditional.
2934 -- Unfortunately this does not work if the call has a dynamic size,
2935 -- because gigi regards it as a dynamic-sized temporary. If such a call
2936 -- appears in a short-circuit expression, the elaboration check will be
2937 -- missed (rare enough ???). Otherwise, the code below inserts the check
2938 -- at the appropriate place before the call. Same applies in the even
2939 -- rarer case the return type has a known size but is unconstrained.
2941 else
2942 if Nkind (N) = N_Function_Call
2943 and then Analyzed (Parent (N))
2944 and then Size_Known_At_Compile_Time (Etype (N))
2945 and then
2946 (not Has_Discriminants (Etype (N))
2947 or else Is_Constrained (Etype (N)))
2949 then
2950 declare
2951 Typ : constant Entity_Id := Etype (N);
2952 Chk : constant Boolean := Do_Range_Check (N);
2954 R : constant Node_Id :=
2955 Make_Raise_Program_Error (Loc,
2956 Reason => PE_Access_Before_Elaboration);
2958 Reloc_N : Node_Id;
2960 begin
2961 Set_Etype (R, Typ);
2963 if No (C) then
2964 Rewrite (N, R);
2966 else
2967 Reloc_N := Relocate_Node (N);
2968 Save_Interps (N, Reloc_N);
2969 Rewrite (N,
2970 Make_Conditional_Expression (Loc,
2971 Expressions => New_List (C, Reloc_N, R)));
2972 end if;
2974 Analyze_And_Resolve (N, Typ);
2976 -- If the original call requires a range check, so does the
2977 -- conditional expression.
2979 if Chk then
2980 Enable_Range_Check (N);
2981 else
2982 Set_Do_Range_Check (N, False);
2983 end if;
2984 end;
2986 else
2987 if No (C) then
2988 Insert_Action (Nod,
2989 Make_Raise_Program_Error (Loc,
2990 Reason => PE_Access_Before_Elaboration));
2991 else
2992 Insert_Action (Nod,
2993 Make_Raise_Program_Error (Loc,
2994 Condition =>
2995 Make_Op_Not (Loc,
2996 Right_Opnd => C),
2997 Reason => PE_Access_Before_Elaboration));
2998 end if;
2999 end if;
3000 end if;
3001 end Insert_Elab_Check;
3003 -------------------------------
3004 -- Is_Finalization_Procedure --
3005 -------------------------------
3007 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3008 begin
3009 -- Check whether Id is a procedure with at least one parameter
3011 if Ekind (Id) = E_Procedure
3012 and then Present (First_Formal (Id))
3013 then
3014 declare
3015 Typ : constant Entity_Id := Etype (First_Formal (Id));
3016 Deep_Fin : Entity_Id := Empty;
3017 Fin : Entity_Id := Empty;
3019 begin
3020 -- If the type of the first formal does not require finalization
3021 -- actions, then this is definitely not [Deep_]Finalize.
3023 if not Needs_Finalization (Typ) then
3024 return False;
3025 end if;
3027 -- At this point we have the following scenario:
3029 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3031 -- Recover the two possible versions of [Deep_]Finalize using the
3032 -- type of the first parameter and compare with the input.
3034 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3036 if Is_Controlled (Typ) then
3037 Fin := Find_Prim_Op (Typ, Name_Finalize);
3038 end if;
3040 return
3041 (Present (Deep_Fin) and then Id = Deep_Fin)
3042 or else
3043 (Present (Fin) and then Id = Fin);
3044 end;
3045 end if;
3047 return False;
3048 end Is_Finalization_Procedure;
3050 ------------------
3051 -- Output_Calls --
3052 ------------------
3054 procedure Output_Calls (N : Node_Id) is
3055 Ent : Entity_Id;
3057 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
3058 -- An internal function, used to determine if a name, Nm, is either
3059 -- a non-internal name, or is an internal name that is printable
3060 -- by the error message circuits (i.e. it has a single upper
3061 -- case letter at the end).
3063 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
3064 begin
3065 if not Is_Internal_Name (Nm) then
3066 return True;
3068 elsif Name_Len = 1 then
3069 return False;
3071 else
3072 Name_Len := Name_Len - 1;
3073 return not Is_Internal_Name;
3074 end if;
3075 end Is_Printable_Error_Name;
3077 -- Start of processing for Output_Calls
3079 begin
3080 for J in reverse 1 .. Elab_Call.Last loop
3081 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3083 Ent := Elab_Call.Table (J).Ent;
3085 if Is_Generic_Unit (Ent) then
3086 Error_Msg_NE ("\?& instantiated #", N, Ent);
3088 elsif Is_Init_Proc (Ent) then
3089 Error_Msg_N ("\?initialization procedure called #", N);
3091 elsif Is_Printable_Error_Name (Chars (Ent)) then
3092 Error_Msg_NE ("\?& called #", N, Ent);
3094 else
3095 Error_Msg_N ("\? called #", N);
3096 end if;
3097 end loop;
3098 end Output_Calls;
3100 ----------------------------
3101 -- Same_Elaboration_Scope --
3102 ----------------------------
3104 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3105 S1 : Entity_Id;
3106 S2 : Entity_Id;
3108 begin
3109 -- Find elaboration scope for Scop1
3110 -- This is either a subprogram or a compilation unit.
3112 S1 := Scop1;
3113 while S1 /= Standard_Standard
3114 and then not Is_Compilation_Unit (S1)
3115 and then (Ekind (S1) = E_Package
3116 or else
3117 Ekind (S1) = E_Protected_Type
3118 or else
3119 Ekind (S1) = E_Block)
3120 loop
3121 S1 := Scope (S1);
3122 end loop;
3124 -- Find elaboration scope for Scop2
3126 S2 := Scop2;
3127 while S2 /= Standard_Standard
3128 and then not Is_Compilation_Unit (S2)
3129 and then (Ekind (S2) = E_Package
3130 or else
3131 Ekind (S2) = E_Protected_Type
3132 or else
3133 Ekind (S2) = E_Block)
3134 loop
3135 S2 := Scope (S2);
3136 end loop;
3138 return S1 = S2;
3139 end Same_Elaboration_Scope;
3141 -----------------
3142 -- Set_C_Scope --
3143 -----------------
3145 procedure Set_C_Scope is
3146 begin
3147 while not Is_Compilation_Unit (C_Scope) loop
3148 C_Scope := Scope (C_Scope);
3149 end loop;
3150 end Set_C_Scope;
3152 -----------------
3153 -- Spec_Entity --
3154 -----------------
3156 function Spec_Entity (E : Entity_Id) return Entity_Id is
3157 Decl : Node_Id;
3159 begin
3160 -- Check for case of body entity
3161 -- Why is the check for E_Void needed???
3163 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3164 Decl := E;
3166 loop
3167 Decl := Parent (Decl);
3168 exit when Nkind (Decl) in N_Proper_Body;
3169 end loop;
3171 return Corresponding_Spec (Decl);
3173 else
3174 return E;
3175 end if;
3176 end Spec_Entity;
3178 -------------------
3179 -- Supply_Bodies --
3180 -------------------
3182 procedure Supply_Bodies (N : Node_Id) is
3183 begin
3184 if Nkind (N) = N_Subprogram_Declaration then
3185 declare
3186 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3187 begin
3189 -- Internal subprograms will already have a generated body, so
3190 -- there is no need to provide a stub for them.
3192 if No (Corresponding_Body (N)) then
3193 declare
3194 Loc : constant Source_Ptr := Sloc (N);
3195 B : Node_Id;
3196 Formals : constant List_Id := Copy_Parameter_List (Ent);
3197 Nam : constant Entity_Id :=
3198 Make_Defining_Identifier (Loc, Chars (Ent));
3199 Spec : Node_Id;
3200 Stats : constant List_Id :=
3201 New_List
3202 (Make_Raise_Program_Error (Loc,
3203 Reason => PE_Access_Before_Elaboration));
3205 begin
3206 if Ekind (Ent) = E_Function then
3207 Spec :=
3208 Make_Function_Specification (Loc,
3209 Defining_Unit_Name => Nam,
3210 Parameter_Specifications => Formals,
3211 Result_Definition =>
3212 New_Copy_Tree
3213 (Result_Definition (Specification (N))));
3215 -- We cannot reliably make a return statement for this
3216 -- body, but none is needed because the call raises
3217 -- program error.
3219 Set_Return_Present (Ent);
3221 else
3222 Spec :=
3223 Make_Procedure_Specification (Loc,
3224 Defining_Unit_Name => Nam,
3225 Parameter_Specifications => Formals);
3226 end if;
3228 B := Make_Subprogram_Body (Loc,
3229 Specification => Spec,
3230 Declarations => New_List,
3231 Handled_Statement_Sequence =>
3232 Make_Handled_Sequence_Of_Statements (Loc, Stats));
3233 Insert_After (N, B);
3234 Analyze (B);
3235 end;
3236 end if;
3237 end;
3239 elsif Nkind (N) = N_Package_Declaration then
3240 declare
3241 Spec : constant Node_Id := Specification (N);
3242 begin
3243 Push_Scope (Defining_Unit_Name (Spec));
3244 Supply_Bodies (Visible_Declarations (Spec));
3245 Supply_Bodies (Private_Declarations (Spec));
3246 Pop_Scope;
3247 end;
3248 end if;
3249 end Supply_Bodies;
3251 procedure Supply_Bodies (L : List_Id) is
3252 Elmt : Node_Id;
3253 begin
3254 if Present (L) then
3255 Elmt := First (L);
3256 while Present (Elmt) loop
3257 Supply_Bodies (Elmt);
3258 Next (Elmt);
3259 end loop;
3260 end if;
3261 end Supply_Bodies;
3263 ------------
3264 -- Within --
3265 ------------
3267 function Within (E1, E2 : Entity_Id) return Boolean is
3268 Scop : Entity_Id;
3269 begin
3270 Scop := E1;
3271 loop
3272 if Scop = E2 then
3273 return True;
3274 elsif Scop = Standard_Standard then
3275 return False;
3276 else
3277 Scop := Scope (Scop);
3278 end if;
3279 end loop;
3280 end Within;
3282 --------------------------
3283 -- Within_Elaborate_All --
3284 --------------------------
3286 function Within_Elaborate_All
3287 (Unit : Unit_Number_Type;
3288 E : Entity_Id) return Boolean
3290 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3291 pragma Pack (Unit_Number_Set);
3293 Seen : Unit_Number_Set := (others => False);
3294 -- Seen (X) is True after we have seen unit X in the walk. This is used
3295 -- to prevent processing the same unit more than once.
3297 Result : Boolean := False;
3299 procedure Helper (Unit : Unit_Number_Type);
3300 -- This helper procedure does all the work for Within_Elaborate_All. It
3301 -- walks the dependency graph, and sets Result to True if it finds an
3302 -- appropriate Elaborate_All.
3304 ------------
3305 -- Helper --
3306 ------------
3308 procedure Helper (Unit : Unit_Number_Type) is
3309 CU : constant Node_Id := Cunit (Unit);
3311 Item : Node_Id;
3312 Item2 : Node_Id;
3313 Elab_Id : Entity_Id;
3314 Par : Node_Id;
3316 begin
3317 if Seen (Unit) then
3318 return;
3319 else
3320 Seen (Unit) := True;
3321 end if;
3323 -- First, check for Elaborate_Alls on this unit
3325 Item := First (Context_Items (CU));
3326 while Present (Item) loop
3327 if Nkind (Item) = N_Pragma
3328 and then Pragma_Name (Item) = Name_Elaborate_All
3329 then
3330 -- Return if some previous error on the pragma itself
3332 if Error_Posted (Item) then
3333 return;
3334 end if;
3336 Elab_Id :=
3337 Entity
3338 (Expression (First (Pragma_Argument_Associations (Item))));
3340 if E = Elab_Id then
3341 Result := True;
3342 return;
3343 end if;
3345 Par := Parent (Unit_Declaration_Node (Elab_Id));
3347 Item2 := First (Context_Items (Par));
3348 while Present (Item2) loop
3349 if Nkind (Item2) = N_With_Clause
3350 and then Entity (Name (Item2)) = E
3351 and then not Limited_Present (Item2)
3352 then
3353 Result := True;
3354 return;
3355 end if;
3357 Next (Item2);
3358 end loop;
3359 end if;
3361 Next (Item);
3362 end loop;
3364 -- Second, recurse on with's. We could do this as part of the above
3365 -- loop, but it's probably more efficient to have two loops, because
3366 -- the relevant Elaborate_All is likely to be on the initial unit. In
3367 -- other words, we're walking the with's breadth-first. This part is
3368 -- only necessary in the dynamic elaboration model.
3370 if Dynamic_Elaboration_Checks then
3371 Item := First (Context_Items (CU));
3372 while Present (Item) loop
3373 if Nkind (Item) = N_With_Clause
3374 and then not Limited_Present (Item)
3375 then
3376 -- Note: the following call to Get_Cunit_Unit_Number does a
3377 -- linear search, which could be slow, but it's OK because
3378 -- we're about to give a warning anyway. Also, there might
3379 -- be hundreds of units, but not millions. If it turns out
3380 -- to be a problem, we could store the Get_Cunit_Unit_Number
3381 -- in each N_Compilation_Unit node, but that would involve
3382 -- rearranging N_Compilation_Unit_Aux to make room.
3384 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3386 if Result then
3387 return;
3388 end if;
3389 end if;
3391 Next (Item);
3392 end loop;
3393 end if;
3394 end Helper;
3396 -- Start of processing for Within_Elaborate_All
3398 begin
3399 Helper (Unit);
3400 return Result;
3401 end Within_Elaborate_All;
3403 end Sem_Elab;