2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ada / sem_elab.adb
blob01fd0cd969e0fe1d4d3cbf1e875267db54ceb5cc
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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Namet; use Namet;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Output; use Output;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch7; use Sem_Ch7;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinput; use Sinput;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Table;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Uname; use Uname;
60 package body Sem_Elab is
62 -- The following table records the recursive call chain for output in the
63 -- Output routine. Each entry records the call node and the entity of the
64 -- called routine. The number of entries in the table (i.e. the value of
65 -- Elab_Call.Last) indicates the current depth of recursion and is used to
66 -- identify the outer level.
68 type Elab_Call_Entry is record
69 Cloc : Source_Ptr;
70 Ent : Entity_Id;
71 end record;
73 package Elab_Call is new Table.Table (
74 Table_Component_Type => Elab_Call_Entry,
75 Table_Index_Type => Int,
76 Table_Low_Bound => 1,
77 Table_Initial => 50,
78 Table_Increment => 100,
79 Table_Name => "Elab_Call");
81 -- This table is initialized at the start of each outer level call. It
82 -- holds the entities for all subprograms that have been examined for this
83 -- particular outer level call, and is used to prevent both infinite
84 -- recursion, and useless reanalysis of bodies already seen
86 package Elab_Visited is new Table.Table (
87 Table_Component_Type => Entity_Id,
88 Table_Index_Type => Int,
89 Table_Low_Bound => 1,
90 Table_Initial => 200,
91 Table_Increment => 100,
92 Table_Name => "Elab_Visited");
94 -- This table stores calls to Check_Internal_Call that are delayed
95 -- until all generics are instantiated, and in particular that all
96 -- generic bodies have been inserted. We need to delay, because we
97 -- need to be able to look through the inserted bodies.
99 type Delay_Element is record
100 N : Node_Id;
101 -- The parameter N from the call to Check_Internal_Call. Note that
102 -- this node may get rewritten over the delay period by expansion
103 -- in the call case (but not in the instantiation case).
105 E : Entity_Id;
106 -- The parameter E from the call to Check_Internal_Call
108 Orig_Ent : Entity_Id;
109 -- The parameter Orig_Ent from the call to Check_Internal_Call
111 Curscop : Entity_Id;
112 -- The current scope of the call. This is restored when we complete
113 -- the delayed call, so that we do this in the right scope.
115 From_Elab_Code : Boolean;
116 -- Save indication of whether this call is from elaboration code
118 Outer_Scope : Entity_Id;
119 -- Save scope of outer level call
120 end record;
122 package Delay_Check is new Table.Table (
123 Table_Component_Type => Delay_Element,
124 Table_Index_Type => Int,
125 Table_Low_Bound => 1,
126 Table_Initial => 1000,
127 Table_Increment => 100,
128 Table_Name => "Delay_Check");
130 C_Scope : Entity_Id;
131 -- Top level scope of current scope. Compute this only once at the outer
132 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
134 Outer_Level_Sloc : Source_Ptr;
135 -- Save Sloc value for outer level call node for comparisons of source
136 -- locations. A body is too late if it appears after the *outer* level
137 -- call, not the particular call that is being analyzed.
139 From_Elab_Code : Boolean;
140 -- This flag shows whether the outer level call currently being examined
141 -- is or is not in elaboration code. We are only interested in calls to
142 -- routines in other units if this flag is True.
144 In_Task_Activation : Boolean := False;
145 -- This flag indicates whether we are performing elaboration checks on
146 -- task procedures, at the point of activation. If true, we do not trace
147 -- internal calls in these procedures, because all local bodies are known
148 -- to be elaborated.
150 Delaying_Elab_Checks : Boolean := True;
151 -- This is set True till the compilation is complete, including the
152 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
153 -- the delay table is used to make the delayed calls and this flag is reset
154 -- to False, so that the calls are processed.
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 -- Note: Outer_Scope in all following specs represents the scope of
161 -- interest of the outer level call. If it is set to Standard_Standard,
162 -- then it means the outer level call was at elaboration level, and that
163 -- thus all calls are of interest. If it was set to some other scope,
164 -- then the original call was an inner call, and we are not interested
165 -- in calls that go outside this scope.
167 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
168 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
169 -- for the WITH clause for unit U (which will always be present). A special
170 -- case is when N is a function or procedure instantiation, in which case
171 -- it is sufficient to set Elaborate_Desirable, since in this case there is
172 -- no possibility of transitive elaboration issues.
174 procedure Check_A_Call
175 (N : Node_Id;
176 E : Entity_Id;
177 Outer_Scope : Entity_Id;
178 Inter_Unit_Only : Boolean;
179 Generate_Warnings : Boolean := True;
180 In_Init_Proc : Boolean := False);
181 -- This is the internal recursive routine that is called to check for
182 -- possible elaboration error. The argument N is a subprogram call or
183 -- generic instantiation, or 'Access attribute reference to be checked, and
184 -- E is the entity of the called subprogram, or instantiated generic unit,
185 -- or subprogram referenced by 'Access.
187 -- In SPARK mode, N can also be a variable reference, since in SPARK this
188 -- also triggers a requirement for Elaborate_All, and in this case E is the
189 -- entity being referenced.
191 -- Outer_Scope is the outer level scope for the original reference.
192 -- Inter_Unit_Only is set if the call is only to be checked in the
193 -- case where it is to another unit (and skipped if within a unit).
194 -- Generate_Warnings is set to False to suppress warning messages about
195 -- missing pragma Elaborate_All's. These messages are not wanted for
196 -- inner calls in the dynamic model. Note that an instance of the Access
197 -- attribute applied to a subprogram also generates a call to this
198 -- procedure (since the referenced subprogram may be called later
199 -- indirectly). Flag In_Init_Proc should be set whenever the current
200 -- context is a type init proc.
202 -- Note: this might better be called Check_A_Reference to recognize the
203 -- variable case for SPARK, but we prefer to retain the historical name
204 -- since in practice this is mostly about checking calls for the possible
205 -- occurrence of an access-before-elaboration exception.
207 procedure Check_Bad_Instantiation (N : Node_Id);
208 -- N is a node for an instantiation (if called with any other node kind,
209 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
210 -- the special case of a generic instantiation of a generic spec in the
211 -- same declarative part as the instantiation where a body is present and
212 -- has not yet been seen. This is an obvious error, but needs to be checked
213 -- specially at the time of the instantiation, since it is a case where we
214 -- cannot insert the body anywhere. If this case is detected, warnings are
215 -- generated, and a raise of Program_Error is inserted. In addition any
216 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
217 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
218 -- flag as an indication that no attempt should be made to insert an
219 -- instance body.
221 procedure Check_Internal_Call
222 (N : Node_Id;
223 E : Entity_Id;
224 Outer_Scope : Entity_Id;
225 Orig_Ent : Entity_Id);
226 -- N is a function call or procedure statement call node and E is the
227 -- entity of the called function, which is within the current compilation
228 -- unit (where subunits count as part of the parent). This call checks if
229 -- this call, or any call within any accessed body could cause an ABE, and
230 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
231 -- renamings, and points to the original name of the entity. This is used
232 -- for error messages. Outer_Scope is the outer level scope for the
233 -- original call.
235 procedure Check_Internal_Call_Continue
236 (N : Node_Id;
237 E : Entity_Id;
238 Outer_Scope : Entity_Id;
239 Orig_Ent : Entity_Id);
240 -- The processing for Check_Internal_Call is divided up into two phases,
241 -- and this represents the second phase. The second phase is delayed if
242 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
243 -- phase makes an entry in the Delay_Check table, which is processed when
244 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
245 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
246 -- original call.
248 function Has_Generic_Body (N : Node_Id) return Boolean;
249 -- N is a generic package instantiation node, and this routine determines
250 -- if this package spec does in fact have a generic body. If so, then
251 -- True is returned, otherwise False. Note that this is not at all the
252 -- same as checking if the unit requires a body, since it deals with
253 -- the case of optional bodies accurately (i.e. if a body is optional,
254 -- then it looks to see if a body is actually present). Note: this
255 -- function can only do a fully correct job if in generating code mode
256 -- where all bodies have to be present. If we are operating in semantics
257 -- check only mode, then in some cases of optional bodies, a result of
258 -- False may incorrectly be given. In practice this simply means that
259 -- some cases of warnings for incorrect order of elaboration will only
260 -- be given when generating code, which is not a big problem (and is
261 -- inevitable, given the optional body semantics of Ada).
263 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
264 -- Given code for an elaboration check (or unconditional raise if the check
265 -- is not needed), inserts the code in the appropriate place. N is the call
266 -- or instantiation node for which the check code is required. C is the
267 -- test whose failure triggers the raise.
269 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean;
270 -- Returns True if node N is a call to a generic formal subprogram
272 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean;
273 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
275 procedure Output_Calls
276 (N : Node_Id;
277 Check_Elab_Flag : Boolean);
278 -- Outputs chain of calls stored in the Elab_Call table. The caller has
279 -- already generated the main warning message, so the warnings generated
280 -- are all continuation messages. The argument is the call node at which
281 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
282 -- enumerated only when flag Elab_Warning is set for the dynamic case or
283 -- when flag Elab_Info_Messages is set for the static case.
285 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
286 -- Given two scopes, determine whether they are the same scope from an
287 -- elaboration point of view, i.e. packages and blocks are ignored.
289 procedure Set_C_Scope;
290 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
291 -- to be the enclosing compilation unit of this scope.
293 function Get_Referenced_Ent (N : Node_Id) return Entity_Id;
294 -- N is either a function or procedure call or an access attribute that
295 -- references a subprogram. This call retrieves the relevant entity. If
296 -- this is a call to a protected subprogram, the entity is a selected
297 -- component. The callable entity may be absent, in which case Empty is
298 -- returned. This happens with non-analyzed calls in nested generics.
300 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
301 -- entity, in which case, the value returned is simply this entity.
303 procedure Set_Elaboration_Constraint
304 (Call : Node_Id;
305 Subp : Entity_Id;
306 Scop : Entity_Id);
307 -- The current unit U may depend semantically on some unit P which is not
308 -- in the current context. If there is an elaboration call that reaches P,
309 -- we need to indicate that P requires an Elaborate_All, but this is not
310 -- effective in U's ali file, if there is no with_clause for P. In this
311 -- case we add the Elaborate_All on the unit Q that directly or indirectly
312 -- makes P available. This can happen in two cases:
314 -- a) Q declares a subtype of a type declared in P, and the call is an
315 -- initialization call for an object of that subtype.
317 -- b) Q declares an object of some tagged type whose root type is
318 -- declared in P, and the initialization call uses object notation on
319 -- that object to reach a primitive operation or a classwide operation
320 -- declared in P.
322 -- If P appears in the context of U, the current processing is correct.
323 -- Otherwise we must identify these two cases to retrieve Q and place the
324 -- Elaborate_All_Desirable on it.
326 function Spec_Entity (E : Entity_Id) return Entity_Id;
327 -- Given a compilation unit entity, if it is a spec entity, it is returned
328 -- unchanged. If it is a body entity, then the spec for the corresponding
329 -- spec is returned
331 procedure Supply_Bodies (N : Node_Id);
332 -- Given a node, N, that is either a subprogram declaration or a package
333 -- declaration, this procedure supplies dummy bodies for the subprogram
334 -- or for all subprograms in the package. If the given node is not one of
335 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
336 -- contains a single Raise statement.
338 procedure Supply_Bodies (L : List_Id);
339 -- Calls Supply_Bodies for all elements of the given list L
341 function Within (E1, E2 : Entity_Id) return Boolean;
342 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
343 -- of its contained scopes, False otherwise.
345 function Within_Elaborate_All
346 (Unit : Unit_Number_Type;
347 E : Entity_Id) return Boolean;
348 -- Return True if we are within the scope of an Elaborate_All for E, or if
349 -- we are within the scope of an Elaborate_All for some other unit U, and U
350 -- with's E. This prevents spurious warnings when the called entity is
351 -- renamed within U, or in case of generic instances.
353 --------------------------------------
354 -- Activate_Elaborate_All_Desirable --
355 --------------------------------------
357 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
358 UN : constant Unit_Number_Type := Get_Code_Unit (N);
359 CU : constant Node_Id := Cunit (UN);
360 UE : constant Entity_Id := Cunit_Entity (UN);
361 Unm : constant Unit_Name_Type := Unit_Name (UN);
362 CI : constant List_Id := Context_Items (CU);
363 Itm : Node_Id;
364 Ent : Entity_Id;
366 procedure Add_To_Context_And_Mark (Itm : Node_Id);
367 -- This procedure is called when the elaborate indication must be
368 -- applied to a unit not in the context of the referencing unit. The
369 -- unit gets added to the context as an implicit with.
371 function In_Withs_Of (UEs : Entity_Id) return Boolean;
372 -- UEs is the spec entity of a unit. If the unit to be marked is
373 -- in the context item list of this unit spec, then the call returns
374 -- True and Itm is left set to point to the relevant N_With_Clause node.
376 procedure Set_Elab_Flag (Itm : Node_Id);
377 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
379 -----------------------------
380 -- Add_To_Context_And_Mark --
381 -----------------------------
383 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
384 CW : constant Node_Id :=
385 Make_With_Clause (Sloc (Itm),
386 Name => Name (Itm));
388 begin
389 Set_Library_Unit (CW, Library_Unit (Itm));
390 Set_Implicit_With (CW, True);
392 -- Set elaborate all desirable on copy and then append the copy to
393 -- the list of body with's and we are done.
395 Set_Elab_Flag (CW);
396 Append_To (CI, CW);
397 end Add_To_Context_And_Mark;
399 -----------------
400 -- In_Withs_Of --
401 -----------------
403 function In_Withs_Of (UEs : Entity_Id) return Boolean is
404 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
405 CUs : constant Node_Id := Cunit (UNs);
406 CIs : constant List_Id := Context_Items (CUs);
408 begin
409 Itm := First (CIs);
410 while Present (Itm) loop
411 if Nkind (Itm) = N_With_Clause then
412 Ent :=
413 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
415 if U = Ent then
416 return True;
417 end if;
418 end if;
420 Next (Itm);
421 end loop;
423 return False;
424 end In_Withs_Of;
426 -------------------
427 -- Set_Elab_Flag --
428 -------------------
430 procedure Set_Elab_Flag (Itm : Node_Id) is
431 begin
432 if Nkind (N) in N_Subprogram_Instantiation then
433 Set_Elaborate_Desirable (Itm);
434 else
435 Set_Elaborate_All_Desirable (Itm);
436 end if;
437 end Set_Elab_Flag;
439 -- Start of processing for Activate_Elaborate_All_Desirable
441 begin
442 -- Do not set binder indication if expansion is disabled, as when
443 -- compiling a generic unit.
445 if not Expander_Active then
446 return;
447 end if;
449 Itm := First (CI);
450 while Present (Itm) loop
451 if Nkind (Itm) = N_With_Clause then
452 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
454 -- If we find it, then mark elaborate all desirable and return
456 if U = Ent then
457 Set_Elab_Flag (Itm);
458 return;
459 end if;
460 end if;
462 Next (Itm);
463 end loop;
465 -- If we fall through then the with clause is not present in the
466 -- current unit. One legitimate possibility is that the with clause
467 -- is present in the spec when we are a body.
469 if Is_Body_Name (Unm)
470 and then In_Withs_Of (Spec_Entity (UE))
471 then
472 Add_To_Context_And_Mark (Itm);
473 return;
474 end if;
476 -- Similarly, we may be in the spec or body of a child unit, where
477 -- the unit in question is with'ed by some ancestor of the child unit.
479 if Is_Child_Name (Unm) then
480 declare
481 Pkg : Entity_Id;
483 begin
484 Pkg := UE;
485 loop
486 Pkg := Scope (Pkg);
487 exit when Pkg = Standard_Standard;
489 if In_Withs_Of (Pkg) then
490 Add_To_Context_And_Mark (Itm);
491 return;
492 end if;
493 end loop;
494 end;
495 end if;
497 -- Here if we do not find with clause on spec or body. We just ignore
498 -- this case, it means that the elaboration involves some other unit
499 -- than the unit being compiled, and will be caught elsewhere.
501 null;
502 end Activate_Elaborate_All_Desirable;
504 ------------------
505 -- Check_A_Call --
506 ------------------
508 procedure Check_A_Call
509 (N : Node_Id;
510 E : Entity_Id;
511 Outer_Scope : Entity_Id;
512 Inter_Unit_Only : Boolean;
513 Generate_Warnings : Boolean := True;
514 In_Init_Proc : Boolean := False)
516 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference;
517 -- Indicates if we have Access attribute case
519 Variable_Case : constant Boolean :=
520 Nkind (N) in N_Has_Entity
521 and then Present (Entity (N))
522 and then Ekind (Entity (N)) = E_Variable;
523 -- Indicates if we have variable reference case
525 procedure Elab_Warning
526 (Msg_D : String;
527 Msg_S : String;
528 Ent : Node_Or_Entity_Id);
529 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
530 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
531 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
532 -- Msg_S is an info message (output if Elab_Info_Messages is set.
534 ------------------
535 -- Elab_Warning --
536 ------------------
538 procedure Elab_Warning
539 (Msg_D : String;
540 Msg_S : String;
541 Ent : Node_Or_Entity_Id)
543 begin
544 -- Dynamic elaboration checks, real warning
546 if Dynamic_Elaboration_Checks then
547 if not Access_Case then
548 if Msg_D /= "" and then Elab_Warnings then
549 Error_Msg_NE (Msg_D, N, Ent);
550 end if;
551 end if;
553 -- Static elaboration checks, info message
555 else
556 if Elab_Info_Messages then
557 Error_Msg_NE (Msg_S, N, Ent);
558 end if;
559 end if;
560 end Elab_Warning;
562 -- Local variables
564 Loc : constant Source_Ptr := Sloc (N);
565 Ent : Entity_Id;
566 Decl : Node_Id;
568 E_Scope : Entity_Id;
569 -- Top level scope of entity for called subprogram. This value includes
570 -- following renamings and derivations, so this scope can be in a
571 -- non-visible unit. This is the scope that is to be investigated to
572 -- see whether an elaboration check is required.
574 W_Scope : Entity_Id;
575 -- Top level scope of directly called entity for subprogram. This
576 -- differs from E_Scope in the case where renamings or derivations
577 -- are involved, since it does not follow these links. W_Scope is
578 -- generally in a visible unit, and it is this scope that may require
579 -- an Elaborate_All. However, there are some cases (initialization
580 -- calls and calls involving object notation) where W_Scope might not
581 -- be in the context of the current unit, and there is an intermediate
582 -- package that is, in which case the Elaborate_All has to be placed
583 -- on this intermediate package. These special cases are handled in
584 -- Set_Elaboration_Constraint.
586 Body_Acts_As_Spec : Boolean;
587 -- Set to true if call is to body acting as spec (no separate spec)
589 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
590 -- Indicates if we have instantiation case
592 Caller_Unit_Internal : Boolean;
593 Callee_Unit_Internal : Boolean;
595 Inst_Caller : Source_Ptr;
596 Inst_Callee : Source_Ptr;
598 Unit_Caller : Unit_Number_Type;
599 Unit_Callee : Unit_Number_Type;
601 Cunit_SC : Boolean := False;
602 -- Set to suppress dynamic elaboration checks where one of the
603 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
604 -- if a pragma Elaborate[_All] applies to that scope, in which case
605 -- warnings on the scope are also suppressed. For the internal case,
606 -- we ignore this flag.
608 -- Start of processing for Check_A_Call
610 begin
611 -- If the call is known to be within a local Suppress Elaboration
612 -- pragma, nothing to check. This can happen in task bodies. But
613 -- we ignore this for a call to a generic formal.
615 if Nkind (N) in N_Subprogram_Call
616 and then No_Elaboration_Check (N)
617 and then not Is_Call_Of_Generic_Formal (N)
618 then
619 return;
620 end if;
622 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
623 -- check, we don't mind in this case if the call occurs before the body
624 -- since this is all generated code.
626 if Nkind (Original_Node (N)) = N_Attribute_Reference
627 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
628 then
629 return;
630 end if;
632 -- Proceed with check
634 Ent := E;
636 -- For a variable reference, just set Body_Acts_As_Spec to False
638 if Variable_Case then
639 Body_Acts_As_Spec := False;
641 -- Additional checks for all other cases
643 else
644 -- Go to parent for derived subprogram, or to original subprogram in
645 -- the case of a renaming (Alias covers both these cases).
647 loop
648 if (Suppress_Elaboration_Warnings (Ent)
649 or else Elaboration_Checks_Suppressed (Ent))
650 and then (Inst_Case or else No (Alias (Ent)))
651 then
652 return;
653 end if;
655 -- Nothing to do for imported entities
657 if Is_Imported (Ent) then
658 return;
659 end if;
661 exit when Inst_Case or else No (Alias (Ent));
662 Ent := Alias (Ent);
663 end loop;
665 Decl := Unit_Declaration_Node (Ent);
667 if Nkind (Decl) = N_Subprogram_Body then
668 Body_Acts_As_Spec := True;
670 elsif Nkind_In (Decl, N_Subprogram_Declaration,
671 N_Subprogram_Body_Stub)
672 or else Inst_Case
673 then
674 Body_Acts_As_Spec := False;
676 -- If we have none of an instantiation, subprogram body or subprogram
677 -- declaration, or in the SPARK case, a variable reference, then
678 -- it is not a case that we want to check. (One case is a call to a
679 -- generic formal subprogram, where we do not want the check in the
680 -- template).
682 else
683 return;
684 end if;
685 end if;
687 E_Scope := Ent;
688 loop
689 if Elaboration_Checks_Suppressed (E_Scope)
690 or else Suppress_Elaboration_Warnings (E_Scope)
691 then
692 Cunit_SC := True;
693 end if;
695 -- Exit when we get to compilation unit, not counting subunits
697 exit when Is_Compilation_Unit (E_Scope)
698 and then (Is_Child_Unit (E_Scope)
699 or else Scope (E_Scope) = Standard_Standard);
701 -- If we did not find a compilation unit, other than standard,
702 -- then nothing to check (happens in some instantiation cases)
704 if E_Scope = Standard_Standard then
705 return;
707 -- Otherwise move up a scope looking for compilation unit
709 else
710 E_Scope := Scope (E_Scope);
711 end if;
712 end loop;
714 -- No checks needed for pure or preelaborated compilation units
716 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
717 return;
718 end if;
720 -- If the generic entity is within a deeper instance than we are, then
721 -- either the instantiation to which we refer itself caused an ABE, in
722 -- which case that will be handled separately, or else we know that the
723 -- body we need appears as needed at the point of the instantiation.
724 -- However, this assumption is only valid if we are in static mode.
726 if not Dynamic_Elaboration_Checks
727 and then
728 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
729 then
730 return;
731 end if;
733 -- Do not give a warning for a package with no body
735 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
736 return;
737 end if;
739 -- Find top level scope for called entity (not following renamings
740 -- or derivations). This is where the Elaborate_All will go if it is
741 -- needed. We start with the called entity, except in the case of an
742 -- initialization procedure outside the current package, where the init
743 -- proc is in the root package, and we start from the entity of the name
744 -- in the call.
746 declare
747 Ent : constant Entity_Id := Get_Referenced_Ent (N);
748 begin
749 if Is_Init_Proc (Ent)
750 and then not In_Same_Extended_Unit (N, Ent)
751 then
752 W_Scope := Scope (Ent);
753 else
754 W_Scope := E;
755 end if;
756 end;
758 -- Now loop through scopes to get to the enclosing compilation unit
760 while not Is_Compilation_Unit (W_Scope) loop
761 W_Scope := Scope (W_Scope);
762 end loop;
764 -- Case of entity is in same unit as call or instantiation. In the
765 -- instantiation case, W_Scope may be different from E_Scope; we want
766 -- the unit in which the instantiation occurs, since we're analyzing
767 -- based on the expansion.
769 if W_Scope = C_Scope then
770 if not Inter_Unit_Only then
771 Check_Internal_Call (N, Ent, Outer_Scope, E);
772 end if;
774 return;
775 end if;
777 -- Case of entity is not in current unit (i.e. with'ed unit case)
779 -- We are only interested in such calls if the outer call was from
780 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
782 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
783 return;
784 end if;
786 -- Nothing to do if some scope said that no checks were required
788 if Cunit_SC then
789 return;
790 end if;
792 -- Nothing to do for a generic instance, because in this case the
793 -- checking was at the point of instantiation of the generic However,
794 -- this shortcut is only applicable in static mode.
796 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
797 return;
798 end if;
800 -- Nothing to do if subprogram with no separate spec. However, a call
801 -- to Deep_Initialize may result in a call to a user-defined Initialize
802 -- procedure, which imposes a body dependency. This happens only if the
803 -- type is controlled and the Initialize procedure is not inherited.
805 if Body_Acts_As_Spec then
806 if Is_TSS (Ent, TSS_Deep_Initialize) then
807 declare
808 Typ : constant Entity_Id := Etype (First_Formal (Ent));
809 Init : Entity_Id;
811 begin
812 if not Is_Controlled (Typ) then
813 return;
814 else
815 Init := Find_Prim_Op (Typ, Name_Initialize);
817 if Comes_From_Source (Init) then
818 Ent := Init;
819 else
820 return;
821 end if;
822 end if;
823 end;
825 else
826 return;
827 end if;
828 end if;
830 -- Check cases of internal units
832 Callee_Unit_Internal :=
833 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E_Scope)));
835 -- Do not give a warning if the with'ed unit is internal and this is
836 -- the generic instantiation case (this saves a lot of hassle dealing
837 -- with the Text_IO special child units)
839 if Callee_Unit_Internal and Inst_Case then
840 return;
841 end if;
843 if C_Scope = Standard_Standard then
844 Caller_Unit_Internal := False;
845 else
846 Caller_Unit_Internal :=
847 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (C_Scope)));
848 end if;
850 -- Do not give a warning if the with'ed unit is internal and the
851 -- caller is not internal (since the binder always elaborates
852 -- internal units first).
854 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
855 return;
856 end if;
858 -- For now, if debug flag -gnatdE is not set, do no checking for
859 -- one internal unit withing another. This fixes the problem with
860 -- the sgi build and storage errors. To be resolved later ???
862 if (Callee_Unit_Internal and Caller_Unit_Internal)
863 and not Debug_Flag_EE
864 then
865 return;
866 end if;
868 if Is_TSS (E, TSS_Deep_Initialize) then
869 Ent := E;
870 end if;
872 -- If the call is in an instance, and the called entity is not
873 -- defined in the same instance, then the elaboration issue focuses
874 -- around the unit containing the template, it is this unit which
875 -- requires an Elaborate_All.
877 -- However, if we are doing dynamic elaboration, we need to chase the
878 -- call in the usual manner.
880 -- We also need to chase the call in the usual manner if it is a call
881 -- to a generic formal parameter, since that case was not handled as
882 -- part of the processing of the template.
884 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
885 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
887 if Inst_Caller = No_Location then
888 Unit_Caller := No_Unit;
889 else
890 Unit_Caller := Get_Source_Unit (N);
891 end if;
893 if Inst_Callee = No_Location then
894 Unit_Callee := No_Unit;
895 else
896 Unit_Callee := Get_Source_Unit (Ent);
897 end if;
899 if Unit_Caller /= No_Unit
900 and then Unit_Callee /= Unit_Caller
901 and then not Dynamic_Elaboration_Checks
902 and then not Is_Call_Of_Generic_Formal (N)
903 then
904 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
906 -- If we don't get a spec entity, just ignore call. Not quite
907 -- clear why this check is necessary. ???
909 if No (E_Scope) then
910 return;
911 end if;
913 -- Otherwise step to enclosing compilation unit
915 while not Is_Compilation_Unit (E_Scope) loop
916 E_Scope := Scope (E_Scope);
917 end loop;
919 -- For the case where N is not an instance, and is not a call within
920 -- instance to other than a generic formal, we recompute E_Scope
921 -- for the error message, since we do NOT want to go to the unit
922 -- which has the ultimate declaration in the case of renaming and
923 -- derivation and we also want to go to the generic unit in the
924 -- case of an instance, and no further.
926 else
927 -- Loop to carefully follow renamings and derivations one step
928 -- outside the current unit, but not further.
930 if not (Inst_Case or Variable_Case)
931 and then Present (Alias (Ent))
932 then
933 E_Scope := Alias (Ent);
934 else
935 E_Scope := Ent;
936 end if;
938 loop
939 while not Is_Compilation_Unit (E_Scope) loop
940 E_Scope := Scope (E_Scope);
941 end loop;
943 -- If E_Scope is the same as C_Scope, it means that there
944 -- definitely was a local renaming or derivation, and we
945 -- are not yet out of the current unit.
947 exit when E_Scope /= C_Scope;
948 Ent := Alias (Ent);
949 E_Scope := Ent;
951 -- If no alias, there is a previous error
953 if No (Ent) then
954 Check_Error_Detected;
955 return;
956 end if;
957 end loop;
958 end if;
960 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
961 return;
962 end if;
964 -- Now check if an Elaborate_All (or dynamic check) is needed
966 if not Suppress_Elaboration_Warnings (Ent)
967 and then not Elaboration_Checks_Suppressed (Ent)
968 and then not Suppress_Elaboration_Warnings (E_Scope)
969 and then not Elaboration_Checks_Suppressed (E_Scope)
970 and then ((Elab_Warnings or Elab_Info_Messages)
971 or else SPARK_Mode = On)
972 and then Generate_Warnings
973 then
974 -- Instantiation case
976 if Inst_Case then
977 if SPARK_Mode = On then
978 Error_Msg_NE
979 ("instantiation of & during elaboration in SPARK", N, Ent);
981 else
982 Elab_Warning
983 ("instantiation of & may raise Program_Error?l?",
984 "info: instantiation of & during elaboration?$?", Ent);
985 end if;
987 -- Indirect call case, info message only in static elaboration
988 -- case, because the attribute reference itself cannot raise an
989 -- exception. Note that SPARK does not permit indirect calls.
991 elsif Access_Case then
992 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
994 -- Variable reference in SPARK mode
996 elsif Variable_Case then
997 Error_Msg_NE
998 ("reference to & during elaboration in SPARK", N, Ent);
1000 -- Subprogram call case
1002 else
1003 if Nkind (Name (N)) in N_Has_Entity
1004 and then Is_Init_Proc (Entity (Name (N)))
1005 and then Comes_From_Source (Ent)
1006 then
1007 Elab_Warning
1008 ("implicit call to & may raise Program_Error?l?",
1009 "info: implicit call to & during elaboration?$?",
1010 Ent);
1012 elsif SPARK_Mode = On then
1013 Error_Msg_NE ("call to & during elaboration in SPARK", N, Ent);
1015 else
1016 Elab_Warning
1017 ("call to & may raise Program_Error?l?",
1018 "info: call to & during elaboration?$?",
1019 Ent);
1020 end if;
1021 end if;
1023 Error_Msg_Qual_Level := Nat'Last;
1025 -- Case of Elaborate_All not present and required, for SPARK this
1026 -- is an error, so give an error message.
1028 if SPARK_Mode = On then
1029 Error_Msg_NE ("\Elaborate_All pragma required for&", N, W_Scope);
1031 -- Otherwise we generate an implicit pragma. For a subprogram
1032 -- instantiation, Elaborate is good enough, since no transitive
1033 -- call is possible at elaboration time in this case.
1035 elsif Nkind (N) in N_Subprogram_Instantiation then
1036 Elab_Warning
1037 ("\missing pragma Elaborate for&?l?",
1038 "\implicit pragma Elaborate for& generated?$?",
1039 W_Scope);
1041 -- For all other cases, we need an implicit Elaborate_All
1043 else
1044 Elab_Warning
1045 ("\missing pragma Elaborate_All for&?l?",
1046 "\implicit pragma Elaborate_All for & generated?$?",
1047 W_Scope);
1048 end if;
1050 Error_Msg_Qual_Level := 0;
1052 -- Take into account the flags related to elaboration warning
1053 -- messages when enumerating the various calls involved. This
1054 -- ensures the proper pairing of the main warning and the
1055 -- clarification messages generated by Output_Calls.
1057 Output_Calls (N, Check_Elab_Flag => True);
1059 -- Set flag to prevent further warnings for same unit unless in
1060 -- All_Errors_Mode.
1062 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
1063 Set_Suppress_Elaboration_Warnings (W_Scope, True);
1064 end if;
1065 end if;
1067 -- Check for runtime elaboration check required
1069 if Dynamic_Elaboration_Checks then
1070 if not Elaboration_Checks_Suppressed (Ent)
1071 and then not Elaboration_Checks_Suppressed (W_Scope)
1072 and then not Elaboration_Checks_Suppressed (E_Scope)
1073 and then not Cunit_SC
1074 then
1075 -- Runtime elaboration check required. Generate check of the
1076 -- elaboration Boolean for the unit containing the entity.
1078 -- Note that for this case, we do check the real unit (the one
1079 -- from following renamings, since that is the issue).
1081 -- Could this possibly miss a useless but required PE???
1083 Insert_Elab_Check (N,
1084 Make_Attribute_Reference (Loc,
1085 Attribute_Name => Name_Elaborated,
1086 Prefix =>
1087 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1089 -- Prevent duplicate elaboration checks on the same call,
1090 -- which can happen if the body enclosing the call appears
1091 -- itself in a call whose elaboration check is delayed.
1093 if Nkind (N) in N_Subprogram_Call then
1094 Set_No_Elaboration_Check (N);
1095 end if;
1096 end if;
1098 -- Case of static elaboration model
1100 else
1101 -- Do not do anything if elaboration checks suppressed. Note that
1102 -- we check Ent here, not E, since we want the real entity for the
1103 -- body to see if checks are suppressed for it, not the dummy
1104 -- entry for renamings or derivations.
1106 if Elaboration_Checks_Suppressed (Ent)
1107 or else Elaboration_Checks_Suppressed (E_Scope)
1108 or else Elaboration_Checks_Suppressed (W_Scope)
1109 then
1110 null;
1112 -- Do not generate an Elaborate_All for finalization routines
1113 -- which perform partial clean up as part of initialization.
1115 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1116 null;
1118 -- Here we need to generate an implicit elaborate all
1120 else
1121 -- Generate Elaborate_All warning unless suppressed
1123 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1124 and then not Suppress_Elaboration_Warnings (Ent)
1125 and then not Suppress_Elaboration_Warnings (E_Scope)
1126 and then not Suppress_Elaboration_Warnings (W_Scope)
1127 then
1128 Error_Msg_Node_2 := W_Scope;
1129 Error_Msg_NE
1130 ("info: call to& in elaboration code " &
1131 "requires pragma Elaborate_All on&?$?", N, E);
1132 end if;
1134 -- Set indication for binder to generate Elaborate_All
1136 Set_Elaboration_Constraint (N, E, W_Scope);
1137 end if;
1138 end if;
1139 end Check_A_Call;
1141 -----------------------------
1142 -- Check_Bad_Instantiation --
1143 -----------------------------
1145 procedure Check_Bad_Instantiation (N : Node_Id) is
1146 Ent : Entity_Id;
1148 begin
1149 -- Nothing to do if we do not have an instantiation (happens in some
1150 -- error cases, and also in the formal package declaration case)
1152 if Nkind (N) not in N_Generic_Instantiation then
1153 return;
1155 -- Nothing to do if serious errors detected (avoid cascaded errors)
1157 elsif Serious_Errors_Detected /= 0 then
1158 return;
1160 -- Nothing to do if not in full analysis mode
1162 elsif not Full_Analysis then
1163 return;
1165 -- Nothing to do if inside a generic template
1167 elsif Inside_A_Generic then
1168 return;
1170 -- Nothing to do if a library level instantiation
1172 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1173 return;
1175 -- Nothing to do if we are compiling a proper body for semantic
1176 -- purposes only. The generic body may be in another proper body.
1178 elsif
1179 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1180 then
1181 return;
1182 end if;
1184 Ent := Get_Generic_Entity (N);
1186 -- The case we are interested in is when the generic spec is in the
1187 -- current declarative part
1189 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1190 or else not In_Same_Extended_Unit (N, Ent)
1191 then
1192 return;
1193 end if;
1195 -- If the generic entity is within a deeper instance than we are, then
1196 -- either the instantiation to which we refer itself caused an ABE, in
1197 -- which case that will be handled separately. Otherwise, we know that
1198 -- the body we need appears as needed at the point of the instantiation.
1199 -- If they are both at the same level but not within the same instance
1200 -- then the body of the generic will be in the earlier instance.
1202 declare
1203 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1204 D2 : constant Int := Instantiation_Depth (Sloc (N));
1206 begin
1207 if D1 > D2 then
1208 return;
1210 elsif D1 = D2
1211 and then Is_Generic_Instance (Scope (Ent))
1212 and then not In_Open_Scopes (Scope (Ent))
1213 then
1214 return;
1215 end if;
1216 end;
1218 -- Now we can proceed, if the entity being called has a completion,
1219 -- then we are definitely OK, since we have already seen the body.
1221 if Has_Completion (Ent) then
1222 return;
1223 end if;
1225 -- If there is no body, then nothing to do
1227 if not Has_Generic_Body (N) then
1228 return;
1229 end if;
1231 -- Here we definitely have a bad instantiation
1233 Error_Msg_Warn := SPARK_Mode /= On;
1234 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1236 if Present (Instance_Spec (N)) then
1237 Supply_Bodies (Instance_Spec (N));
1238 end if;
1240 Error_Msg_N ("\Program_Error [<<", N);
1241 Insert_Elab_Check (N);
1242 Set_ABE_Is_Certain (N);
1243 end Check_Bad_Instantiation;
1245 ---------------------
1246 -- Check_Elab_Call --
1247 ---------------------
1249 procedure Check_Elab_Call
1250 (N : Node_Id;
1251 Outer_Scope : Entity_Id := Empty;
1252 In_Init_Proc : Boolean := False)
1254 Ent : Entity_Id;
1255 P : Node_Id;
1257 begin
1258 -- If the reference is not in the main unit, there is nothing to check.
1259 -- Elaboration call from units in the context of the main unit will lead
1260 -- to semantic dependencies when those units are compiled.
1262 if not In_Extended_Main_Code_Unit (N) then
1263 return;
1264 end if;
1266 -- For an entry call, check relevant restriction
1268 if Nkind (N) = N_Entry_Call_Statement
1269 and then not In_Subprogram_Or_Concurrent_Unit
1270 then
1271 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1273 -- Nothing to do if this is not an expected type of reference (happens
1274 -- in some error conditions, and in some cases where rewriting occurs).
1276 elsif Nkind (N) not in N_Subprogram_Call
1277 and then Nkind (N) /= N_Attribute_Reference
1278 and then (SPARK_Mode /= On
1279 or else Nkind (N) not in N_Has_Entity
1280 or else No (Entity (N))
1281 or else Ekind (Entity (N)) /= E_Variable)
1282 then
1283 return;
1285 -- Nothing to do if this is a call already rewritten for elab checking.
1286 -- Such calls appear as the targets of If_Expressions.
1288 -- This check MUST be wrong, it catches far too much
1290 elsif Nkind (Parent (N)) = N_If_Expression then
1291 return;
1293 -- Nothing to do if inside a generic template
1295 elsif Inside_A_Generic
1296 and then No (Enclosing_Generic_Body (N))
1297 then
1298 return;
1300 -- Nothing to do if call is being pre-analyzed, as when within a
1301 -- pre/postcondition, a predicate, or an invariant.
1303 elsif In_Spec_Expression then
1304 return;
1305 end if;
1307 -- Nothing to do if this is a call to a postcondition, which is always
1308 -- within a subprogram body, even though the current scope may be the
1309 -- enclosing scope of the subprogram.
1311 if Nkind (N) = N_Procedure_Call_Statement
1312 and then Is_Entity_Name (Name (N))
1313 and then Chars (Entity (Name (N))) = Name_uPostconditions
1314 then
1315 return;
1316 end if;
1318 -- Here we have a reference at elaboration time which must be checked
1320 if Debug_Flag_LL then
1321 Write_Str (" Check_Elab_Ref: ");
1323 if Nkind (N) = N_Attribute_Reference then
1324 if not Is_Entity_Name (Prefix (N)) then
1325 Write_Str ("<<not entity name>>");
1326 else
1327 Write_Name (Chars (Entity (Prefix (N))));
1328 end if;
1330 Write_Str ("'Access");
1332 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1333 Write_Str ("<<not entity name>> ");
1335 else
1336 Write_Name (Chars (Entity (Name (N))));
1337 end if;
1339 Write_Str (" reference at ");
1340 Write_Location (Sloc (N));
1341 Write_Eol;
1342 end if;
1344 -- Climb up the tree to make sure we are not inside default expression
1345 -- of a parameter specification or a record component, since in both
1346 -- these cases, we will be doing the actual reference later, not now,
1347 -- and it is at the time of the actual reference (statically speaking)
1348 -- that we must do our static check, not at the time of its initial
1349 -- analysis).
1351 -- However, we have to check references within component definitions
1352 -- (e.g. a function call that determines an array component bound),
1353 -- so we terminate the loop in that case.
1355 P := Parent (N);
1356 while Present (P) loop
1357 if Nkind_In (P, N_Parameter_Specification,
1358 N_Component_Declaration)
1359 then
1360 return;
1362 -- The reference occurs within the constraint of a component,
1363 -- so it must be checked.
1365 elsif Nkind (P) = N_Component_Definition then
1366 exit;
1368 else
1369 P := Parent (P);
1370 end if;
1371 end loop;
1373 -- Stuff that happens only at the outer level
1375 if No (Outer_Scope) then
1376 Elab_Visited.Set_Last (0);
1378 -- Nothing to do if current scope is Standard (this is a bit odd, but
1379 -- it happens in the case of generic instantiations).
1381 C_Scope := Current_Scope;
1383 if C_Scope = Standard_Standard then
1384 return;
1385 end if;
1387 -- First case, we are in elaboration code
1389 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1391 if From_Elab_Code then
1393 -- Complain if ref that comes from source in preelaborated unit
1394 -- and we are not inside a subprogram (i.e. we are in elab code).
1396 if Comes_From_Source (N)
1397 and then In_Preelaborated_Unit
1398 and then not In_Inlined_Body
1399 and then Nkind (N) /= N_Attribute_Reference
1400 then
1401 -- This is a warning in GNAT mode allowing such calls to be
1402 -- used in the predefined library with appropriate care.
1404 Error_Msg_Warn := GNAT_Mode;
1405 Error_Msg_N
1406 ("<<non-static call not allowed in preelaborated unit", N);
1407 return;
1408 end if;
1410 -- Second case, we are inside a subprogram or concurrent unit, which
1411 -- means we are not in elaboration code.
1413 else
1414 -- In this case, the issue is whether we are inside the
1415 -- declarative part of the unit in which we live, or inside its
1416 -- statements. In the latter case, there is no issue of ABE calls
1417 -- at this level (a call from outside to the unit in which we live
1418 -- might cause an ABE, but that will be detected when we analyze
1419 -- that outer level call, as it recurses into the called unit).
1421 -- Climb up the tree, doing this test, and also testing for being
1422 -- inside a default expression, which, as discussed above, is not
1423 -- checked at this stage.
1425 declare
1426 P : Node_Id;
1427 L : List_Id;
1429 begin
1430 P := N;
1431 loop
1432 -- If we find a parentless subtree, it seems safe to assume
1433 -- that we are not in a declarative part and that no
1434 -- checking is required.
1436 if No (P) then
1437 return;
1438 end if;
1440 if Is_List_Member (P) then
1441 L := List_Containing (P);
1442 P := Parent (L);
1443 else
1444 L := No_List;
1445 P := Parent (P);
1446 end if;
1448 exit when Nkind (P) = N_Subunit;
1450 -- Filter out case of default expressions, where we do not
1451 -- do the check at this stage.
1453 if Nkind_In (P, N_Parameter_Specification,
1454 N_Component_Declaration)
1455 then
1456 return;
1457 end if;
1459 -- A protected body has no elaboration code and contains
1460 -- only other bodies.
1462 if Nkind (P) = N_Protected_Body then
1463 return;
1465 elsif Nkind_In (P, N_Subprogram_Body,
1466 N_Task_Body,
1467 N_Block_Statement,
1468 N_Entry_Body)
1469 then
1470 if L = Declarations (P) then
1471 exit;
1473 -- We are not in elaboration code, but we are doing
1474 -- dynamic elaboration checks, in this case, we still
1475 -- need to do the reference, since the subprogram we are
1476 -- in could be called from another unit, also in dynamic
1477 -- elaboration check mode, at elaboration time.
1479 elsif Dynamic_Elaboration_Checks then
1481 -- We provide a debug flag to disable this check. That
1482 -- way we have an easy work around for regressions
1483 -- that are caused by this new check. This debug flag
1484 -- can be removed later.
1486 if Debug_Flag_DD then
1487 return;
1488 end if;
1490 -- Do the check in this case
1492 exit;
1494 elsif Nkind (P) = N_Task_Body then
1496 -- The check is deferred until Check_Task_Activation
1497 -- but we need to capture local suppress pragmas
1498 -- that may inhibit checks on this call.
1500 Ent := Get_Referenced_Ent (N);
1502 if No (Ent) then
1503 return;
1505 elsif Elaboration_Checks_Suppressed (Current_Scope)
1506 or else Elaboration_Checks_Suppressed (Ent)
1507 or else Elaboration_Checks_Suppressed (Scope (Ent))
1508 then
1509 Set_No_Elaboration_Check (N);
1510 end if;
1512 return;
1514 -- Static model, call is not in elaboration code, we
1515 -- never need to worry, because in the static model the
1516 -- top level caller always takes care of things.
1518 else
1519 return;
1520 end if;
1521 end if;
1522 end loop;
1523 end;
1524 end if;
1525 end if;
1527 Ent := Get_Referenced_Ent (N);
1529 if No (Ent) then
1530 return;
1531 end if;
1533 -- Nothing to do if this is a recursive call (i.e. a call to
1534 -- an entity that is already in the Elab_Call stack)
1536 for J in 1 .. Elab_Visited.Last loop
1537 if Ent = Elab_Visited.Table (J) then
1538 return;
1539 end if;
1540 end loop;
1542 -- See if we need to analyze this reference. We analyze it if either of
1543 -- the following conditions is met:
1545 -- It is an inner level call (since in this case it was triggered
1546 -- by an outer level call from elaboration code), but only if the
1547 -- call is within the scope of the original outer level call.
1549 -- It is an outer level reference from elaboration code, or a call to
1550 -- an entity is in the same elaboration scope.
1552 -- And in these cases, we will check both inter-unit calls and
1553 -- intra-unit (within a single unit) calls.
1555 C_Scope := Current_Scope;
1557 -- If not outer level reference, then we follow it if it is within the
1558 -- original scope of the outer reference.
1560 if Present (Outer_Scope)
1561 and then Within (Scope (Ent), Outer_Scope)
1562 then
1563 Set_C_Scope;
1564 Check_A_Call
1565 (N => N,
1566 E => Ent,
1567 Outer_Scope => Outer_Scope,
1568 Inter_Unit_Only => False,
1569 In_Init_Proc => In_Init_Proc);
1571 -- Nothing to do if elaboration checks suppressed for this scope.
1572 -- However, an interesting exception, the fact that elaboration checks
1573 -- are suppressed within an instance (because we can trace the body when
1574 -- we process the template) does not extend to calls to generic formal
1575 -- subprograms.
1577 elsif Elaboration_Checks_Suppressed (Current_Scope)
1578 and then not Is_Call_Of_Generic_Formal (N)
1579 then
1580 null;
1582 elsif From_Elab_Code then
1583 Set_C_Scope;
1584 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1586 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1587 Set_C_Scope;
1588 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1590 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1591 -- is set, then we will do the check, but only in the inter-unit case
1592 -- (this is to accommodate unguarded elaboration calls from other units
1593 -- in which this same mode is set). We don't want warnings in this case,
1594 -- it would generate warnings having nothing to do with elaboration.
1596 elsif Dynamic_Elaboration_Checks then
1597 Set_C_Scope;
1598 Check_A_Call
1600 Ent,
1601 Standard_Standard,
1602 Inter_Unit_Only => True,
1603 Generate_Warnings => False);
1605 -- Otherwise nothing to do
1607 else
1608 return;
1609 end if;
1611 -- A call to an Init_Proc in elaboration code may bring additional
1612 -- dependencies, if some of the record components thereof have
1613 -- initializations that are function calls that come from source. We
1614 -- treat the current node as a call to each of these functions, to check
1615 -- their elaboration impact.
1617 if Is_Init_Proc (Ent) and then From_Elab_Code then
1618 Process_Init_Proc : declare
1619 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1621 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1622 -- Find subprogram calls within body of Init_Proc for Traverse
1623 -- instantiation below.
1625 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1626 -- Traversal procedure to find all calls with body of Init_Proc
1628 ---------------------
1629 -- Check_Init_Call --
1630 ---------------------
1632 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1633 Func : Entity_Id;
1635 begin
1636 if Nkind (Nod) in N_Subprogram_Call
1637 and then Is_Entity_Name (Name (Nod))
1638 then
1639 Func := Entity (Name (Nod));
1641 if Comes_From_Source (Func) then
1642 Check_A_Call
1643 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1644 end if;
1646 return OK;
1648 else
1649 return OK;
1650 end if;
1651 end Check_Init_Call;
1653 -- Start of processing for Process_Init_Proc
1655 begin
1656 if Nkind (Unit_Decl) = N_Subprogram_Body then
1657 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1658 end if;
1659 end Process_Init_Proc;
1660 end if;
1661 end Check_Elab_Call;
1663 -----------------------
1664 -- Check_Elab_Assign --
1665 -----------------------
1667 procedure Check_Elab_Assign (N : Node_Id) is
1668 Ent : Entity_Id;
1669 Scop : Entity_Id;
1671 Pkg_Spec : Entity_Id;
1672 Pkg_Body : Entity_Id;
1674 begin
1675 -- For record or array component, check prefix. If it is an access type,
1676 -- then there is nothing to do (we do not know what is being assigned),
1677 -- but otherwise this is an assignment to the prefix.
1679 if Nkind_In (N, N_Indexed_Component,
1680 N_Selected_Component,
1681 N_Slice)
1682 then
1683 if not Is_Access_Type (Etype (Prefix (N))) then
1684 Check_Elab_Assign (Prefix (N));
1685 end if;
1687 return;
1688 end if;
1690 -- For type conversion, check expression
1692 if Nkind (N) = N_Type_Conversion then
1693 Check_Elab_Assign (Expression (N));
1694 return;
1695 end if;
1697 -- Nothing to do if this is not an entity reference otherwise get entity
1699 if Is_Entity_Name (N) then
1700 Ent := Entity (N);
1701 else
1702 return;
1703 end if;
1705 -- What we are looking for is a reference in the body of a package that
1706 -- modifies a variable declared in the visible part of the package spec.
1708 if Present (Ent)
1709 and then Comes_From_Source (N)
1710 and then not Suppress_Elaboration_Warnings (Ent)
1711 and then Ekind (Ent) = E_Variable
1712 and then not In_Private_Part (Ent)
1713 and then Is_Library_Level_Entity (Ent)
1714 then
1715 Scop := Current_Scope;
1716 loop
1717 if No (Scop) or else Scop = Standard_Standard then
1718 return;
1719 elsif Ekind (Scop) = E_Package
1720 and then Is_Compilation_Unit (Scop)
1721 then
1722 exit;
1723 else
1724 Scop := Scope (Scop);
1725 end if;
1726 end loop;
1728 -- Here Scop points to the containing library package
1730 Pkg_Spec := Scop;
1731 Pkg_Body := Body_Entity (Pkg_Spec);
1733 -- All OK if the package has an Elaborate_Body pragma
1735 if Has_Pragma_Elaborate_Body (Scop) then
1736 return;
1737 end if;
1739 -- OK if entity being modified is not in containing package spec
1741 if not In_Same_Source_Unit (Scop, Ent) then
1742 return;
1743 end if;
1745 -- All OK if entity appears in generic package or generic instance.
1746 -- We just get too messed up trying to give proper warnings in the
1747 -- presence of generics. Better no message than a junk one.
1749 Scop := Scope (Ent);
1750 while Present (Scop) and then Scop /= Pkg_Spec loop
1751 if Ekind (Scop) = E_Generic_Package then
1752 return;
1753 elsif Ekind (Scop) = E_Package
1754 and then Is_Generic_Instance (Scop)
1755 then
1756 return;
1757 end if;
1759 Scop := Scope (Scop);
1760 end loop;
1762 -- All OK if in task, don't issue warnings there
1764 if In_Task_Activation then
1765 return;
1766 end if;
1768 -- OK if no package body
1770 if No (Pkg_Body) then
1771 return;
1772 end if;
1774 -- OK if reference is not in package body
1776 if not In_Same_Source_Unit (Pkg_Body, N) then
1777 return;
1778 end if;
1780 -- OK if package body has no handled statement sequence
1782 declare
1783 HSS : constant Node_Id :=
1784 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1785 begin
1786 if No (HSS) or else not Comes_From_Source (HSS) then
1787 return;
1788 end if;
1789 end;
1791 -- We definitely have a case of a modification of an entity in
1792 -- the package spec from the elaboration code of the package body.
1793 -- We may not give the warning (because there are some additional
1794 -- checks to avoid too many false positives), but it would be a good
1795 -- idea for the binder to try to keep the body elaboration close to
1796 -- the spec elaboration.
1798 Set_Elaborate_Body_Desirable (Pkg_Spec);
1800 -- All OK in gnat mode (we know what we are doing)
1802 if GNAT_Mode then
1803 return;
1804 end if;
1806 -- All OK if all warnings suppressed
1808 if Warning_Mode = Suppress then
1809 return;
1810 end if;
1812 -- All OK if elaboration checks suppressed for entity
1814 if Checks_May_Be_Suppressed (Ent)
1815 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1816 then
1817 return;
1818 end if;
1820 -- OK if the entity is initialized. Note that the No_Initialization
1821 -- flag usually means that the initialization has been rewritten into
1822 -- assignments, but that still counts for us.
1824 declare
1825 Decl : constant Node_Id := Declaration_Node (Ent);
1826 begin
1827 if Nkind (Decl) = N_Object_Declaration
1828 and then (Present (Expression (Decl))
1829 or else No_Initialization (Decl))
1830 then
1831 return;
1832 end if;
1833 end;
1835 -- Here is where we give the warning
1837 -- All OK if warnings suppressed on the entity
1839 if not Has_Warnings_Off (Ent) then
1840 Error_Msg_Sloc := Sloc (Ent);
1842 Error_Msg_NE
1843 ("??& can be accessed by clients before this initialization",
1844 N, Ent);
1845 Error_Msg_NE
1846 ("\??add Elaborate_Body to spec to ensure & is initialized",
1847 N, Ent);
1848 end if;
1850 if not All_Errors_Mode then
1851 Set_Suppress_Elaboration_Warnings (Ent);
1852 end if;
1853 end if;
1854 end Check_Elab_Assign;
1856 ----------------------
1857 -- Check_Elab_Calls --
1858 ----------------------
1860 procedure Check_Elab_Calls is
1861 begin
1862 -- If expansion is disabled, do not generate any checks. Also skip
1863 -- checks if any subunits are missing because in either case we lack the
1864 -- full information that we need, and no object file will be created in
1865 -- any case.
1867 if not Expander_Active
1868 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1869 or else Subunits_Missing
1870 then
1871 return;
1872 end if;
1874 -- Skip delayed calls if we had any errors
1876 if Serious_Errors_Detected = 0 then
1877 Delaying_Elab_Checks := False;
1878 Expander_Mode_Save_And_Set (True);
1880 for J in Delay_Check.First .. Delay_Check.Last loop
1881 Push_Scope (Delay_Check.Table (J).Curscop);
1882 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1884 Check_Internal_Call_Continue (
1885 N => Delay_Check.Table (J).N,
1886 E => Delay_Check.Table (J).E,
1887 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1888 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1890 Pop_Scope;
1891 end loop;
1893 -- Set Delaying_Elab_Checks back on for next main compilation
1895 Expander_Mode_Restore;
1896 Delaying_Elab_Checks := True;
1897 end if;
1898 end Check_Elab_Calls;
1900 ------------------------------
1901 -- Check_Elab_Instantiation --
1902 ------------------------------
1904 procedure Check_Elab_Instantiation
1905 (N : Node_Id;
1906 Outer_Scope : Entity_Id := Empty)
1908 Ent : Entity_Id;
1910 begin
1911 -- Check for and deal with bad instantiation case. There is some
1912 -- duplicated code here, but we will worry about this later ???
1914 Check_Bad_Instantiation (N);
1916 if ABE_Is_Certain (N) then
1917 return;
1918 end if;
1920 -- Nothing to do if we do not have an instantiation (happens in some
1921 -- error cases, and also in the formal package declaration case)
1923 if Nkind (N) not in N_Generic_Instantiation then
1924 return;
1925 end if;
1927 -- Nothing to do if inside a generic template
1929 if Inside_A_Generic then
1930 return;
1931 end if;
1933 -- Nothing to do if the instantiation is not in the main unit
1935 if not In_Extended_Main_Code_Unit (N) then
1936 return;
1937 end if;
1939 Ent := Get_Generic_Entity (N);
1940 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1942 -- See if we need to analyze this instantiation. We analyze it if
1943 -- either of the following conditions is met:
1945 -- It is an inner level instantiation (since in this case it was
1946 -- triggered by an outer level call from elaboration code), but
1947 -- only if the instantiation is within the scope of the original
1948 -- outer level call.
1950 -- It is an outer level instantiation from elaboration code, or the
1951 -- instantiated entity is in the same elaboration scope.
1953 -- And in these cases, we will check both the inter-unit case and
1954 -- the intra-unit (within a single unit) case.
1956 C_Scope := Current_Scope;
1958 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
1959 Set_C_Scope;
1960 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1962 elsif From_Elab_Code then
1963 Set_C_Scope;
1964 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1966 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1967 Set_C_Scope;
1968 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1970 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
1971 -- set, then we will do the check, but only in the inter-unit case (this
1972 -- is to accommodate unguarded elaboration calls from other units in
1973 -- which this same mode is set). We inhibit warnings in this case, since
1974 -- this instantiation is not occurring in elaboration code.
1976 elsif Dynamic_Elaboration_Checks then
1977 Set_C_Scope;
1978 Check_A_Call
1980 Ent,
1981 Standard_Standard,
1982 Inter_Unit_Only => True,
1983 Generate_Warnings => False);
1985 else
1986 return;
1987 end if;
1988 end Check_Elab_Instantiation;
1990 -------------------------
1991 -- Check_Internal_Call --
1992 -------------------------
1994 procedure Check_Internal_Call
1995 (N : Node_Id;
1996 E : Entity_Id;
1997 Outer_Scope : Entity_Id;
1998 Orig_Ent : Entity_Id)
2000 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
2002 begin
2003 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2004 -- node comes from source.
2006 if Nkind (N) = N_Attribute_Reference and then
2007 (not Warn_On_Elab_Access or else not Comes_From_Source (N))
2008 then
2009 return;
2011 -- If not function or procedure call, instantiation, or 'Access, then
2012 -- ignore call (this happens in some error cases and rewriting cases).
2014 elsif not Nkind_In
2015 (N, N_Function_Call,
2016 N_Procedure_Call_Statement,
2017 N_Attribute_Reference)
2018 and then not Inst_Case
2019 then
2020 return;
2022 -- Nothing to do if this is a call or instantiation that has already
2023 -- been found to be a sure ABE.
2025 elsif Nkind (N) /= N_Attribute_Reference and then ABE_Is_Certain (N) then
2026 return;
2028 -- Nothing to do if errors already detected (avoid cascaded errors)
2030 elsif Serious_Errors_Detected /= 0 then
2031 return;
2033 -- Nothing to do if not in full analysis mode
2035 elsif not Full_Analysis then
2036 return;
2038 -- Nothing to do if analyzing in special spec-expression mode, since the
2039 -- call is not actually being made at this time.
2041 elsif In_Spec_Expression then
2042 return;
2044 -- Nothing to do for call to intrinsic subprogram
2046 elsif Is_Intrinsic_Subprogram (E) then
2047 return;
2049 -- No need to trace local calls if checking task activation, because
2050 -- other local bodies are elaborated already.
2052 elsif In_Task_Activation then
2053 return;
2055 -- Nothing to do if call is within a generic unit
2057 elsif Inside_A_Generic then
2058 return;
2059 end if;
2061 -- Delay this call if we are still delaying calls
2063 if Delaying_Elab_Checks then
2064 Delay_Check.Append (
2065 (N => N,
2066 E => E,
2067 Orig_Ent => Orig_Ent,
2068 Curscop => Current_Scope,
2069 Outer_Scope => Outer_Scope,
2070 From_Elab_Code => From_Elab_Code));
2071 return;
2073 -- Otherwise, call phase 2 continuation right now
2075 else
2076 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
2077 end if;
2078 end Check_Internal_Call;
2080 ----------------------------------
2081 -- Check_Internal_Call_Continue --
2082 ----------------------------------
2084 procedure Check_Internal_Call_Continue
2085 (N : Node_Id;
2086 E : Entity_Id;
2087 Outer_Scope : Entity_Id;
2088 Orig_Ent : Entity_Id)
2090 Loc : constant Source_Ptr := Sloc (N);
2091 Inst_Case : constant Boolean := Is_Generic_Unit (E);
2093 Sbody : Node_Id;
2094 Ebody : Entity_Id;
2096 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
2097 -- Function applied to each node as we traverse the body. Checks for
2098 -- call or entity reference that needs checking, and if so checks it.
2099 -- Always returns OK, so entire tree is traversed, except that as
2100 -- described below subprogram bodies are skipped for now.
2102 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2103 -- Traverse procedure using above Find_Elab_Reference function
2105 -------------------------
2106 -- Find_Elab_Reference --
2107 -------------------------
2109 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2110 Actual : Node_Id;
2112 begin
2113 -- If user has specified that there are no entry calls in elaboration
2114 -- code, do not trace past an accept statement, because the rendez-
2115 -- vous will happen after elaboration.
2117 if Nkind_In (Original_Node (N), N_Accept_Statement,
2118 N_Selective_Accept)
2119 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2120 then
2121 return Abandon;
2123 -- If we have a function call, check it
2125 elsif Nkind (N) = N_Function_Call then
2126 Check_Elab_Call (N, Outer_Scope);
2127 return OK;
2129 -- If we have a procedure call, check the call, and also check
2130 -- arguments that are assignments (OUT or IN OUT mode formals).
2132 elsif Nkind (N) = N_Procedure_Call_Statement then
2133 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2135 Actual := First_Actual (N);
2136 while Present (Actual) loop
2137 if Known_To_Be_Assigned (Actual) then
2138 Check_Elab_Assign (Actual);
2139 end if;
2141 Next_Actual (Actual);
2142 end loop;
2144 return OK;
2146 -- If we have an access attribute for a subprogram, check it.
2147 -- Suppress this behavior under debug flag.
2149 elsif not Debug_Flag_Dot_UU
2150 and then Nkind (N) = N_Attribute_Reference
2151 and then Nam_In (Attribute_Name (N), Name_Access,
2152 Name_Unrestricted_Access)
2153 and then Is_Entity_Name (Prefix (N))
2154 and then Is_Subprogram (Entity (Prefix (N)))
2155 then
2156 Check_Elab_Call (N, Outer_Scope);
2157 return OK;
2159 -- In SPARK mode, if we have an entity reference to a variable, then
2160 -- check it. For now we consider any reference.
2162 elsif SPARK_Mode = On
2163 and then Nkind (N) in N_Has_Entity
2164 and then Present (Entity (N))
2165 and then Ekind (Entity (N)) = E_Variable
2166 then
2167 Check_Elab_Call (N, Outer_Scope);
2168 return OK;
2170 -- If we have a generic instantiation, check it
2172 elsif Nkind (N) in N_Generic_Instantiation then
2173 Check_Elab_Instantiation (N, Outer_Scope);
2174 return OK;
2176 -- Skip subprogram bodies that come from source (wait for call to
2177 -- analyze these). The reason for the come from source test is to
2178 -- avoid catching task bodies.
2180 -- For task bodies, we should really avoid these too, waiting for the
2181 -- task activation, but that's too much trouble to catch for now, so
2182 -- we go in unconditionally. This is not so terrible, it means the
2183 -- error backtrace is not quite complete, and we are too eager to
2184 -- scan bodies of tasks that are unused, but this is hardly very
2185 -- significant.
2187 elsif Nkind (N) = N_Subprogram_Body
2188 and then Comes_From_Source (N)
2189 then
2190 return Skip;
2192 elsif Nkind (N) = N_Assignment_Statement
2193 and then Comes_From_Source (N)
2194 then
2195 Check_Elab_Assign (Name (N));
2196 return OK;
2198 else
2199 return OK;
2200 end if;
2201 end Find_Elab_Reference;
2203 -- Start of processing for Check_Internal_Call_Continue
2205 begin
2206 -- Save outer level call if at outer level
2208 if Elab_Call.Last = 0 then
2209 Outer_Level_Sloc := Loc;
2210 end if;
2212 Elab_Visited.Append (E);
2214 -- If the call is to a function that renames a literal, no check needed
2216 if Ekind (E) = E_Enumeration_Literal then
2217 return;
2218 end if;
2220 Sbody := Unit_Declaration_Node (E);
2222 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2223 Ebody := Corresponding_Body (Sbody);
2225 if No (Ebody) then
2226 return;
2227 else
2228 Sbody := Unit_Declaration_Node (Ebody);
2229 end if;
2230 end if;
2232 -- If the body appears after the outer level call or instantiation then
2233 -- we have an error case handled below.
2235 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2236 and then not In_Task_Activation
2237 then
2238 null;
2240 -- If we have the instantiation case we are done, since we now
2241 -- know that the body of the generic appeared earlier.
2243 elsif Inst_Case then
2244 return;
2246 -- Otherwise we have a call, so we trace through the called body to see
2247 -- if it has any problems.
2249 else
2250 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2252 Elab_Call.Append ((Cloc => Loc, Ent => E));
2254 if Debug_Flag_LL then
2255 Write_Str ("Elab_Call.Last = ");
2256 Write_Int (Int (Elab_Call.Last));
2257 Write_Str (" Ent = ");
2258 Write_Name (Chars (E));
2259 Write_Str (" at ");
2260 Write_Location (Sloc (N));
2261 Write_Eol;
2262 end if;
2264 -- Now traverse declarations and statements of subprogram body. Note
2265 -- that we cannot simply Traverse (Sbody), since traverse does not
2266 -- normally visit subprogram bodies.
2268 declare
2269 Decl : Node_Id;
2270 begin
2271 Decl := First (Declarations (Sbody));
2272 while Present (Decl) loop
2273 Traverse (Decl);
2274 Next (Decl);
2275 end loop;
2276 end;
2278 Traverse (Handled_Statement_Sequence (Sbody));
2280 Elab_Call.Decrement_Last;
2281 return;
2282 end if;
2284 -- Here is the case of calling a subprogram where the body has not yet
2285 -- been encountered. A warning message is needed, except if this is the
2286 -- case of appearing within an aspect specification that results in
2287 -- a check call, we do not really have such a situation, so no warning
2288 -- is needed (e.g. the case of a precondition, where the call appears
2289 -- textually before the body, but in actual fact is moved to the
2290 -- appropriate subprogram body and so does not need a check).
2292 declare
2293 P : Node_Id;
2294 O : Node_Id;
2296 begin
2297 P := Parent (N);
2298 loop
2299 -- Keep looking at parents if we are still in the subexpression
2301 if Nkind (P) in N_Subexpr then
2302 P := Parent (P);
2304 -- Here P is the parent of the expression, check for special case
2306 else
2307 O := Original_Node (P);
2309 -- Definitely not the special case if orig node is not a pragma
2311 exit when Nkind (O) /= N_Pragma;
2313 -- Check we have an If statement or a null statement (happens
2314 -- when the If has been expanded to be True).
2316 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2318 -- Our special case will be indicated either by the pragma
2319 -- coming from an aspect ...
2321 if Present (Corresponding_Aspect (O)) then
2322 return;
2324 -- Or, in the case of an initial condition, specifically by a
2325 -- Check pragma specifying an Initial_Condition check.
2327 elsif Pragma_Name (O) = Name_Check
2328 and then
2329 Chars
2330 (Expression (First (Pragma_Argument_Associations (O)))) =
2331 Name_Initial_Condition
2332 then
2333 return;
2335 -- For anything else, we have an error
2337 else
2338 exit;
2339 end if;
2340 end if;
2341 end loop;
2342 end;
2344 -- Not that special case, warning and dynamic check is required
2346 -- If we have nothing in the call stack, then this is at the outer
2347 -- level, and the ABE is bound to occur, unless it's a 'Access.
2349 if Elab_Call.Last = 0 then
2350 Error_Msg_Warn := SPARK_Mode /= On;
2352 if Inst_Case then
2353 Error_Msg_NE
2354 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2355 elsif Nkind (N) /= N_Attribute_Reference then
2356 Error_Msg_NE
2357 ("cannot call& before body seen<<", N, Orig_Ent);
2358 else
2359 Error_Msg_NE
2360 ("Access attribute of & before body seen<<", N, Orig_Ent);
2361 Error_Msg_N ("\possible Program_Error on later references<", N);
2362 end if;
2364 if Nkind (N) /= N_Attribute_Reference then
2365 Error_Msg_N ("\Program_Error [<<", N);
2366 Insert_Elab_Check (N);
2367 end if;
2369 -- Call is not at outer level
2371 else
2372 -- Deal with dynamic elaboration check
2374 if not Elaboration_Checks_Suppressed (E) then
2375 Set_Elaboration_Entity_Required (E);
2377 -- Case of no elaboration entity allocated yet
2379 if No (Elaboration_Entity (E)) then
2381 -- Create object declaration for elaboration entity, and put it
2382 -- just in front of the spec of the subprogram or generic unit,
2383 -- in the same scope as this unit. The subprogram may be over-
2384 -- loaded, so make the name of elaboration entity unique by
2385 -- means of a numeric suffix.
2387 declare
2388 Loce : constant Source_Ptr := Sloc (E);
2389 Ent : constant Entity_Id :=
2390 Make_Defining_Identifier (Loc,
2391 Chars => New_External_Name (Chars (E), 'E', -1));
2393 begin
2394 Set_Elaboration_Entity (E, Ent);
2395 Push_Scope (Scope (E));
2397 Insert_Action (Declaration_Node (E),
2398 Make_Object_Declaration (Loce,
2399 Defining_Identifier => Ent,
2400 Object_Definition =>
2401 New_Occurrence_Of (Standard_Short_Integer, Loce),
2402 Expression =>
2403 Make_Integer_Literal (Loc, Uint_0)));
2405 -- Set elaboration flag at the point of the body
2407 Set_Elaboration_Flag (Sbody, E);
2409 -- Kill current value indication. This is necessary because
2410 -- the tests of this flag are inserted out of sequence and
2411 -- must not pick up bogus indications of the wrong constant
2412 -- value. Also, this is never a true constant, since one way
2413 -- or another, it gets reset.
2415 Set_Current_Value (Ent, Empty);
2416 Set_Last_Assignment (Ent, Empty);
2417 Set_Is_True_Constant (Ent, False);
2418 Pop_Scope;
2419 end;
2420 end if;
2422 -- Generate check of the elaboration counter
2424 Insert_Elab_Check (N,
2425 Make_Attribute_Reference (Loc,
2426 Attribute_Name => Name_Elaborated,
2427 Prefix => New_Occurrence_Of (E, Loc)));
2428 end if;
2430 -- Generate the warning
2432 if not Suppress_Elaboration_Warnings (E)
2433 and then not Elaboration_Checks_Suppressed (E)
2435 -- Suppress this warning if we have a function call that occurred
2436 -- within an assertion expression, since we can get false warnings
2437 -- in this case, due to the out of order handling in this case.
2439 and then
2440 (Nkind (Original_Node (N)) /= N_Function_Call
2441 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2442 then
2443 Error_Msg_Warn := SPARK_Mode /= On;
2445 if Inst_Case then
2446 Error_Msg_NE
2447 ("instantiation of& may occur before body is seen<l<",
2448 N, Orig_Ent);
2449 else
2450 -- A rather specific check. For Finalize/Adjust/Initialize,
2451 -- if the type has Warnings_Off set, suppress the warning.
2453 if Nam_In (Chars (E), Name_Adjust,
2454 Name_Finalize,
2455 Name_Initialize)
2456 and then Present (First_Formal (E))
2457 then
2458 declare
2459 T : constant Entity_Id := Etype (First_Formal (E));
2460 begin
2461 if Is_Controlled (T) then
2462 if Warnings_Off (T)
2463 or else (Ekind (T) = E_Private_Type
2464 and then Warnings_Off (Full_View (T)))
2465 then
2466 goto Output;
2467 end if;
2468 end if;
2469 end;
2470 end if;
2472 -- Go ahead and give warning if not this special case
2474 Error_Msg_NE
2475 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2476 end if;
2478 Error_Msg_N ("\Program_Error ]<l<", N);
2480 -- There is no need to query the elaboration warning message flags
2481 -- because the main message is an error, not a warning, therefore
2482 -- all the clarification messages produces by Output_Calls must be
2483 -- emitted unconditionally.
2485 <<Output>>
2487 Output_Calls (N, Check_Elab_Flag => False);
2488 end if;
2489 end if;
2491 -- Set flag to suppress further warnings on same subprogram
2492 -- unless in all errors mode
2494 if not All_Errors_Mode then
2495 Set_Suppress_Elaboration_Warnings (E);
2496 end if;
2497 end Check_Internal_Call_Continue;
2499 ---------------------------
2500 -- Check_Task_Activation --
2501 ---------------------------
2503 procedure Check_Task_Activation (N : Node_Id) is
2504 Loc : constant Source_Ptr := Sloc (N);
2505 Inter_Procs : constant Elist_Id := New_Elmt_List;
2506 Intra_Procs : constant Elist_Id := New_Elmt_List;
2507 Ent : Entity_Id;
2508 P : Entity_Id;
2509 Task_Scope : Entity_Id;
2510 Cunit_SC : Boolean := False;
2511 Decl : Node_Id;
2512 Elmt : Elmt_Id;
2513 Enclosing : Entity_Id;
2515 procedure Add_Task_Proc (Typ : Entity_Id);
2516 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2517 -- For record types, this procedure recurses over component types.
2519 procedure Collect_Tasks (Decls : List_Id);
2520 -- Collect the types of the tasks that are to be activated in the given
2521 -- list of declarations, in order to perform elaboration checks on the
2522 -- corresponding task procedures which are called implicitly here.
2524 function Outer_Unit (E : Entity_Id) return Entity_Id;
2525 -- find enclosing compilation unit of Entity, ignoring subunits, or
2526 -- else enclosing subprogram. If E is not a package, there is no need
2527 -- for inter-unit elaboration checks.
2529 -------------------
2530 -- Add_Task_Proc --
2531 -------------------
2533 procedure Add_Task_Proc (Typ : Entity_Id) is
2534 Comp : Entity_Id;
2535 Proc : Entity_Id := Empty;
2537 begin
2538 if Is_Task_Type (Typ) then
2539 Proc := Get_Task_Body_Procedure (Typ);
2541 elsif Is_Array_Type (Typ)
2542 and then Has_Task (Base_Type (Typ))
2543 then
2544 Add_Task_Proc (Component_Type (Typ));
2546 elsif Is_Record_Type (Typ)
2547 and then Has_Task (Base_Type (Typ))
2548 then
2549 Comp := First_Component (Typ);
2550 while Present (Comp) loop
2551 Add_Task_Proc (Etype (Comp));
2552 Comp := Next_Component (Comp);
2553 end loop;
2554 end if;
2556 -- If the task type is another unit, we will perform the usual
2557 -- elaboration check on its enclosing unit. If the type is in the
2558 -- same unit, we can trace the task body as for an internal call,
2559 -- but we only need to examine other external calls, because at
2560 -- the point the task is activated, internal subprogram bodies
2561 -- will have been elaborated already. We keep separate lists for
2562 -- each kind of task.
2564 -- Skip this test if errors have occurred, since in this case
2565 -- we can get false indications.
2567 if Serious_Errors_Detected /= 0 then
2568 return;
2569 end if;
2571 if Present (Proc) then
2572 if Outer_Unit (Scope (Proc)) = Enclosing then
2574 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2575 and then
2576 (not Is_Generic_Instance (Scope (Proc))
2577 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2578 then
2579 Error_Msg_Warn := SPARK_Mode /= On;
2580 Error_Msg_N
2581 ("task will be activated before elaboration of its body<<",
2582 Decl);
2583 Error_Msg_N ("\Program_Error [<<", Decl);
2585 elsif Present
2586 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2587 then
2588 Append_Elmt (Proc, Intra_Procs);
2589 end if;
2591 else
2592 -- No need for multiple entries of the same type
2594 Elmt := First_Elmt (Inter_Procs);
2595 while Present (Elmt) loop
2596 if Node (Elmt) = Proc then
2597 return;
2598 end if;
2600 Next_Elmt (Elmt);
2601 end loop;
2603 Append_Elmt (Proc, Inter_Procs);
2604 end if;
2605 end if;
2606 end Add_Task_Proc;
2608 -------------------
2609 -- Collect_Tasks --
2610 -------------------
2612 procedure Collect_Tasks (Decls : List_Id) is
2613 begin
2614 if Present (Decls) then
2615 Decl := First (Decls);
2616 while Present (Decl) loop
2617 if Nkind (Decl) = N_Object_Declaration
2618 and then Has_Task (Etype (Defining_Identifier (Decl)))
2619 then
2620 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2621 end if;
2623 Next (Decl);
2624 end loop;
2625 end if;
2626 end Collect_Tasks;
2628 ----------------
2629 -- Outer_Unit --
2630 ----------------
2632 function Outer_Unit (E : Entity_Id) return Entity_Id is
2633 Outer : Entity_Id;
2635 begin
2636 Outer := E;
2637 while Present (Outer) loop
2638 if Elaboration_Checks_Suppressed (Outer) then
2639 Cunit_SC := True;
2640 end if;
2642 exit when Is_Child_Unit (Outer)
2643 or else Scope (Outer) = Standard_Standard
2644 or else Ekind (Outer) /= E_Package;
2645 Outer := Scope (Outer);
2646 end loop;
2648 return Outer;
2649 end Outer_Unit;
2651 -- Start of processing for Check_Task_Activation
2653 begin
2654 Enclosing := Outer_Unit (Current_Scope);
2656 -- Find all tasks declared in the current unit
2658 if Nkind (N) = N_Package_Body then
2659 P := Unit_Declaration_Node (Corresponding_Spec (N));
2661 Collect_Tasks (Declarations (N));
2662 Collect_Tasks (Visible_Declarations (Specification (P)));
2663 Collect_Tasks (Private_Declarations (Specification (P)));
2665 elsif Nkind (N) = N_Package_Declaration then
2666 Collect_Tasks (Visible_Declarations (Specification (N)));
2667 Collect_Tasks (Private_Declarations (Specification (N)));
2669 else
2670 Collect_Tasks (Declarations (N));
2671 end if;
2673 -- We only perform detailed checks in all tasks that are library level
2674 -- entities. If the master is a subprogram or task, activation will
2675 -- depend on the activation of the master itself.
2677 -- Should dynamic checks be added in the more general case???
2679 if Ekind (Enclosing) /= E_Package then
2680 return;
2681 end if;
2683 -- For task types defined in other units, we want the unit containing
2684 -- the task body to be elaborated before the current one.
2686 Elmt := First_Elmt (Inter_Procs);
2687 while Present (Elmt) loop
2688 Ent := Node (Elmt);
2689 Task_Scope := Outer_Unit (Scope (Ent));
2691 if not Is_Compilation_Unit (Task_Scope) then
2692 null;
2694 elsif Suppress_Elaboration_Warnings (Task_Scope)
2695 or else Elaboration_Checks_Suppressed (Task_Scope)
2696 then
2697 null;
2699 elsif Dynamic_Elaboration_Checks then
2700 if not Elaboration_Checks_Suppressed (Ent)
2701 and then not Cunit_SC
2702 and then
2703 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2704 then
2705 -- Runtime elaboration check required. Generate check of the
2706 -- elaboration counter for the unit containing the entity.
2708 Insert_Elab_Check (N,
2709 Make_Attribute_Reference (Loc,
2710 Attribute_Name => Name_Elaborated,
2711 Prefix =>
2712 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2713 end if;
2715 else
2716 -- Force the binder to elaborate other unit first
2718 if not Suppress_Elaboration_Warnings (Ent)
2719 and then not Elaboration_Checks_Suppressed (Ent)
2720 and then Elab_Info_Messages
2721 and then not Suppress_Elaboration_Warnings (Task_Scope)
2722 and then not Elaboration_Checks_Suppressed (Task_Scope)
2723 then
2724 Error_Msg_Node_2 := Task_Scope;
2725 Error_Msg_NE
2726 ("info: activation of an instance of task type&" &
2727 " requires pragma Elaborate_All on &?$?", N, Ent);
2728 end if;
2730 Activate_Elaborate_All_Desirable (N, Task_Scope);
2731 Set_Suppress_Elaboration_Warnings (Task_Scope);
2732 end if;
2734 Next_Elmt (Elmt);
2735 end loop;
2737 -- For tasks declared in the current unit, trace other calls within
2738 -- the task procedure bodies, which are available.
2740 In_Task_Activation := True;
2742 Elmt := First_Elmt (Intra_Procs);
2743 while Present (Elmt) loop
2744 Ent := Node (Elmt);
2745 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2746 Next_Elmt (Elmt);
2747 end loop;
2749 In_Task_Activation := False;
2750 end Check_Task_Activation;
2752 -------------------------------
2753 -- Is_Call_Of_Generic_Formal --
2754 -------------------------------
2756 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2757 begin
2758 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2760 -- Always return False if debug flag -gnatd.G is set
2762 and then not Debug_Flag_Dot_GG
2764 -- For now, we detect this by looking for the strange identifier
2765 -- node, whose Chars reflect the name of the generic formal, but
2766 -- the Chars of the Entity references the generic actual.
2768 and then Nkind (Name (N)) = N_Identifier
2769 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
2770 end Is_Call_Of_Generic_Formal;
2772 --------------------------------
2773 -- Set_Elaboration_Constraint --
2774 --------------------------------
2776 procedure Set_Elaboration_Constraint
2777 (Call : Node_Id;
2778 Subp : Entity_Id;
2779 Scop : Entity_Id)
2781 Elab_Unit : Entity_Id;
2783 -- Check whether this is a call to an Initialize subprogram for a
2784 -- controlled type. Note that Call can also be a 'Access attribute
2785 -- reference, which now generates an elaboration check.
2787 Init_Call : constant Boolean :=
2788 Nkind (Call) = N_Procedure_Call_Statement
2789 and then Chars (Subp) = Name_Initialize
2790 and then Comes_From_Source (Subp)
2791 and then Present (Parameter_Associations (Call))
2792 and then Is_Controlled (Etype (First_Actual (Call)));
2793 begin
2794 -- If the unit is mentioned in a with_clause of the current unit, it is
2795 -- visible, and we can set the elaboration flag.
2797 if Is_Immediately_Visible (Scop)
2798 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
2799 then
2800 Activate_Elaborate_All_Desirable (Call, Scop);
2801 Set_Suppress_Elaboration_Warnings (Scop, True);
2802 return;
2803 end if;
2805 -- If this is not an initialization call or a call using object notation
2806 -- we know that the unit of the called entity is in the context, and
2807 -- we can set the flag as well. The unit need not be visible if the call
2808 -- occurs within an instantiation.
2810 if Is_Init_Proc (Subp)
2811 or else Init_Call
2812 or else Nkind (Original_Node (Call)) = N_Selected_Component
2813 then
2814 null; -- detailed processing follows.
2816 else
2817 Activate_Elaborate_All_Desirable (Call, Scop);
2818 Set_Suppress_Elaboration_Warnings (Scop, True);
2819 return;
2820 end if;
2822 -- If the unit is not in the context, there must be an intermediate unit
2823 -- that is, on which we need to place to elaboration flag. This happens
2824 -- with init proc calls.
2826 if Is_Init_Proc (Subp) or else Init_Call then
2828 -- The initialization call is on an object whose type is not declared
2829 -- in the same scope as the subprogram. The type of the object must
2830 -- be a subtype of the type of operation. This object is the first
2831 -- actual in the call.
2833 declare
2834 Typ : constant Entity_Id :=
2835 Etype (First (Parameter_Associations (Call)));
2836 begin
2837 Elab_Unit := Scope (Typ);
2838 while (Present (Elab_Unit))
2839 and then not Is_Compilation_Unit (Elab_Unit)
2840 loop
2841 Elab_Unit := Scope (Elab_Unit);
2842 end loop;
2843 end;
2845 -- If original node uses selected component notation, the prefix is
2846 -- visible and determines the scope that must be elaborated. After
2847 -- rewriting, the prefix is the first actual in the call.
2849 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2850 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2852 -- Not one of special cases above
2854 else
2855 -- Using previously computed scope. If the elaboration check is
2856 -- done after analysis, the scope is not visible any longer, but
2857 -- must still be in the context.
2859 Elab_Unit := Scop;
2860 end if;
2862 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2863 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2864 end Set_Elaboration_Constraint;
2866 ------------------------
2867 -- Get_Referenced_Ent --
2868 ------------------------
2870 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
2871 Nam : Node_Id;
2873 begin
2874 if Nkind (N) in N_Has_Entity
2875 and then Present (Entity (N))
2876 and then Ekind (Entity (N)) = E_Variable
2877 then
2878 return Entity (N);
2879 end if;
2881 if Nkind (N) = N_Attribute_Reference then
2882 Nam := Prefix (N);
2883 else
2884 Nam := Name (N);
2885 end if;
2887 if No (Nam) then
2888 return Empty;
2889 elsif Nkind (Nam) = N_Selected_Component then
2890 return Entity (Selector_Name (Nam));
2891 elsif not Is_Entity_Name (Nam) then
2892 return Empty;
2893 else
2894 return Entity (Nam);
2895 end if;
2896 end Get_Referenced_Ent;
2898 ----------------------
2899 -- Has_Generic_Body --
2900 ----------------------
2902 function Has_Generic_Body (N : Node_Id) return Boolean is
2903 Ent : constant Entity_Id := Get_Generic_Entity (N);
2904 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2905 Scop : Entity_Id;
2907 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2908 -- Determine if the list of nodes headed by N and linked by Next
2909 -- contains a package body for the package spec entity E, and if so
2910 -- return the package body. If not, then returns Empty.
2912 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2913 -- This procedure is called load the unit whose name is given by Nam.
2914 -- This unit is being loaded to see whether it contains an optional
2915 -- generic body. The returned value is the loaded unit, which is always
2916 -- a package body (only package bodies can contain other entities in the
2917 -- sense in which Has_Generic_Body is interested). We only attempt to
2918 -- load bodies if we are generating code. If we are in semantics check
2919 -- only mode, then it would be wrong to load bodies that are not
2920 -- required from a semantic point of view, so in this case we return
2921 -- Empty. The result is that the caller may incorrectly decide that a
2922 -- generic spec does not have a body when in fact it does, but the only
2923 -- harm in this is that some warnings on elaboration problems may be
2924 -- lost in semantic checks only mode, which is not big loss. We also
2925 -- return Empty if we go for a body and it is not there.
2927 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2928 -- PE is the entity for a package spec. This function locates the
2929 -- corresponding package body, returning Empty if none is found. The
2930 -- package body returned is fully parsed but may not yet be analyzed,
2931 -- so only syntactic fields should be referenced.
2933 ------------------
2934 -- Find_Body_In --
2935 ------------------
2937 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2938 Nod : Node_Id;
2940 begin
2941 Nod := N;
2942 while Present (Nod) loop
2944 -- If we found the package body we are looking for, return it
2946 if Nkind (Nod) = N_Package_Body
2947 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2948 then
2949 return Nod;
2951 -- If we found the stub for the body, go after the subunit,
2952 -- loading it if necessary.
2954 elsif Nkind (Nod) = N_Package_Body_Stub
2955 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2956 then
2957 if Present (Library_Unit (Nod)) then
2958 return Unit (Library_Unit (Nod));
2960 else
2961 return Load_Package_Body (Get_Unit_Name (Nod));
2962 end if;
2964 -- If neither package body nor stub, keep looking on chain
2966 else
2967 Next (Nod);
2968 end if;
2969 end loop;
2971 return Empty;
2972 end Find_Body_In;
2974 -----------------------
2975 -- Load_Package_Body --
2976 -----------------------
2978 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2979 U : Unit_Number_Type;
2981 begin
2982 if Operating_Mode /= Generate_Code then
2983 return Empty;
2984 else
2985 U :=
2986 Load_Unit
2987 (Load_Name => Nam,
2988 Required => False,
2989 Subunit => False,
2990 Error_Node => N);
2992 if U = No_Unit then
2993 return Empty;
2994 else
2995 return Unit (Cunit (U));
2996 end if;
2997 end if;
2998 end Load_Package_Body;
3000 -------------------------------
3001 -- Locate_Corresponding_Body --
3002 -------------------------------
3004 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
3005 Spec : constant Node_Id := Declaration_Node (PE);
3006 Decl : constant Node_Id := Parent (Spec);
3007 Scop : constant Entity_Id := Scope (PE);
3008 PBody : Node_Id;
3010 begin
3011 if Is_Library_Level_Entity (PE) then
3013 -- If package is a library unit that requires a body, we have no
3014 -- choice but to go after that body because it might contain an
3015 -- optional body for the original generic package.
3017 if Unit_Requires_Body (PE) then
3019 -- Load the body. Note that we are a little careful here to use
3020 -- Spec to get the unit number, rather than PE or Decl, since
3021 -- in the case where the package is itself a library level
3022 -- instantiation, Spec will properly reference the generic
3023 -- template, which is what we really want.
3025 return
3026 Load_Package_Body
3027 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
3029 -- But if the package is a library unit that does NOT require
3030 -- a body, then no body is permitted, so we are sure that there
3031 -- is no body for the original generic package.
3033 else
3034 return Empty;
3035 end if;
3037 -- Otherwise look and see if we are embedded in a further package
3039 elsif Is_Package_Or_Generic_Package (Scop) then
3041 -- If so, get the body of the enclosing package, and look in
3042 -- its package body for the package body we are looking for.
3044 PBody := Locate_Corresponding_Body (Scop);
3046 if No (PBody) then
3047 return Empty;
3048 else
3049 return Find_Body_In (PE, First (Declarations (PBody)));
3050 end if;
3052 -- If we are not embedded in a further package, then the body
3053 -- must be in the same declarative part as we are.
3055 else
3056 return Find_Body_In (PE, Next (Decl));
3057 end if;
3058 end Locate_Corresponding_Body;
3060 -- Start of processing for Has_Generic_Body
3062 begin
3063 if Present (Corresponding_Body (Decl)) then
3064 return True;
3066 elsif Unit_Requires_Body (Ent) then
3067 return True;
3069 -- Compilation units cannot have optional bodies
3071 elsif Is_Compilation_Unit (Ent) then
3072 return False;
3074 -- Otherwise look at what scope we are in
3076 else
3077 Scop := Scope (Ent);
3079 -- Case of entity is in other than a package spec, in this case
3080 -- the body, if present, must be in the same declarative part.
3082 if not Is_Package_Or_Generic_Package (Scop) then
3083 declare
3084 P : Node_Id;
3086 begin
3087 -- Declaration node may get us a spec, so if so, go to
3088 -- the parent declaration.
3090 P := Declaration_Node (Ent);
3091 while not Is_List_Member (P) loop
3092 P := Parent (P);
3093 end loop;
3095 return Present (Find_Body_In (Ent, Next (P)));
3096 end;
3098 -- If the entity is in a package spec, then we have to locate
3099 -- the corresponding package body, and look there.
3101 else
3102 declare
3103 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
3105 begin
3106 if No (PBody) then
3107 return False;
3108 else
3109 return
3110 Present
3111 (Find_Body_In (Ent, (First (Declarations (PBody)))));
3112 end if;
3113 end;
3114 end if;
3115 end if;
3116 end Has_Generic_Body;
3118 -----------------------
3119 -- Insert_Elab_Check --
3120 -----------------------
3122 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
3123 Nod : Node_Id;
3124 Loc : constant Source_Ptr := Sloc (N);
3126 Chk : Node_Id;
3127 -- The check (N_Raise_Program_Error) node to be inserted
3129 begin
3130 -- If expansion is disabled, do not generate any checks. Also
3131 -- skip checks if any subunits are missing because in either
3132 -- case we lack the full information that we need, and no object
3133 -- file will be created in any case.
3135 if not Expander_Active or else Subunits_Missing then
3136 return;
3137 end if;
3139 -- If we have a generic instantiation, where Instance_Spec is set,
3140 -- then this field points to a generic instance spec that has
3141 -- been inserted before the instantiation node itself, so that
3142 -- is where we want to insert a check.
3144 if Nkind (N) in N_Generic_Instantiation
3145 and then Present (Instance_Spec (N))
3146 then
3147 Nod := Instance_Spec (N);
3148 else
3149 Nod := N;
3150 end if;
3152 -- Build check node, possibly with condition
3154 Chk :=
3155 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3157 if Present (C) then
3158 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3159 end if;
3161 -- If we are inserting at the top level, insert in Aux_Decls
3163 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3164 declare
3165 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3167 begin
3168 if No (Declarations (ADN)) then
3169 Set_Declarations (ADN, New_List (Chk));
3170 else
3171 Append_To (Declarations (ADN), Chk);
3172 end if;
3174 Analyze (Chk);
3175 end;
3177 -- Otherwise just insert as an action on the node in question
3179 else
3180 Insert_Action (Nod, Chk);
3181 end if;
3182 end Insert_Elab_Check;
3184 -------------------------------
3185 -- Is_Finalization_Procedure --
3186 -------------------------------
3188 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3189 begin
3190 -- Check whether Id is a procedure with at least one parameter
3192 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3193 declare
3194 Typ : constant Entity_Id := Etype (First_Formal (Id));
3195 Deep_Fin : Entity_Id := Empty;
3196 Fin : Entity_Id := Empty;
3198 begin
3199 -- If the type of the first formal does not require finalization
3200 -- actions, then this is definitely not [Deep_]Finalize.
3202 if not Needs_Finalization (Typ) then
3203 return False;
3204 end if;
3206 -- At this point we have the following scenario:
3208 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3210 -- Recover the two possible versions of [Deep_]Finalize using the
3211 -- type of the first parameter and compare with the input.
3213 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3215 if Is_Controlled (Typ) then
3216 Fin := Find_Prim_Op (Typ, Name_Finalize);
3217 end if;
3219 return (Present (Deep_Fin) and then Id = Deep_Fin)
3220 or else (Present (Fin) and then Id = Fin);
3221 end;
3222 end if;
3224 return False;
3225 end Is_Finalization_Procedure;
3227 ------------------
3228 -- Output_Calls --
3229 ------------------
3231 procedure Output_Calls
3232 (N : Node_Id;
3233 Check_Elab_Flag : Boolean)
3235 function Emit (Flag : Boolean) return Boolean;
3236 -- Determine whether to emit an error message based on the combination
3237 -- of flags Check_Elab_Flag and Flag.
3239 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
3240 -- An internal function, used to determine if a name, Nm, is either
3241 -- a non-internal name, or is an internal name that is printable
3242 -- by the error message circuits (i.e. it has a single upper
3243 -- case letter at the end).
3245 ----------
3246 -- Emit --
3247 ----------
3249 function Emit (Flag : Boolean) return Boolean is
3250 begin
3251 if Check_Elab_Flag then
3252 return Flag;
3253 else
3254 return True;
3255 end if;
3256 end Emit;
3258 -----------------------------
3259 -- Is_Printable_Error_Name --
3260 -----------------------------
3262 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
3263 begin
3264 if not Is_Internal_Name (Nm) then
3265 return True;
3267 elsif Name_Len = 1 then
3268 return False;
3270 else
3271 Name_Len := Name_Len - 1;
3272 return not Is_Internal_Name;
3273 end if;
3274 end Is_Printable_Error_Name;
3276 -- Local variables
3278 Ent : Entity_Id;
3280 -- Start of processing for Output_Calls
3282 begin
3283 for J in reverse 1 .. Elab_Call.Last loop
3284 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3286 Ent := Elab_Call.Table (J).Ent;
3288 -- Dynamic elaboration model, warnings controlled by -gnatwl
3290 if Dynamic_Elaboration_Checks then
3291 if Emit (Elab_Warnings) then
3292 if Is_Generic_Unit (Ent) then
3293 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3294 elsif Is_Init_Proc (Ent) then
3295 Error_Msg_N ("\\?l?initialization procedure called #", N);
3296 elsif Is_Printable_Error_Name (Chars (Ent)) then
3297 Error_Msg_NE ("\\?l?& called #", N, Ent);
3298 else
3299 Error_Msg_N ("\\?l?called #", N);
3300 end if;
3301 end if;
3303 -- Static elaboration model, info messages controlled by -gnatel
3305 else
3306 if Emit (Elab_Info_Messages) then
3307 if Is_Generic_Unit (Ent) then
3308 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3309 elsif Is_Init_Proc (Ent) then
3310 Error_Msg_N ("\\?$?initialization procedure called #", N);
3311 elsif Is_Printable_Error_Name (Chars (Ent)) then
3312 Error_Msg_NE ("\\?$?& called #", N, Ent);
3313 else
3314 Error_Msg_N ("\\?$?called #", N);
3315 end if;
3316 end if;
3317 end if;
3318 end loop;
3319 end Output_Calls;
3321 ----------------------------
3322 -- Same_Elaboration_Scope --
3323 ----------------------------
3325 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3326 S1 : Entity_Id;
3327 S2 : Entity_Id;
3329 begin
3330 -- Find elaboration scope for Scop1
3331 -- This is either a subprogram or a compilation unit.
3333 S1 := Scop1;
3334 while S1 /= Standard_Standard
3335 and then not Is_Compilation_Unit (S1)
3336 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3337 loop
3338 S1 := Scope (S1);
3339 end loop;
3341 -- Find elaboration scope for Scop2
3343 S2 := Scop2;
3344 while S2 /= Standard_Standard
3345 and then not Is_Compilation_Unit (S2)
3346 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3347 loop
3348 S2 := Scope (S2);
3349 end loop;
3351 return S1 = S2;
3352 end Same_Elaboration_Scope;
3354 -----------------
3355 -- Set_C_Scope --
3356 -----------------
3358 procedure Set_C_Scope is
3359 begin
3360 while not Is_Compilation_Unit (C_Scope) loop
3361 C_Scope := Scope (C_Scope);
3362 end loop;
3363 end Set_C_Scope;
3365 -----------------
3366 -- Spec_Entity --
3367 -----------------
3369 function Spec_Entity (E : Entity_Id) return Entity_Id is
3370 Decl : Node_Id;
3372 begin
3373 -- Check for case of body entity
3374 -- Why is the check for E_Void needed???
3376 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3377 Decl := E;
3379 loop
3380 Decl := Parent (Decl);
3381 exit when Nkind (Decl) in N_Proper_Body;
3382 end loop;
3384 return Corresponding_Spec (Decl);
3386 else
3387 return E;
3388 end if;
3389 end Spec_Entity;
3391 -------------------
3392 -- Supply_Bodies --
3393 -------------------
3395 procedure Supply_Bodies (N : Node_Id) is
3396 begin
3397 if Nkind (N) = N_Subprogram_Declaration then
3398 declare
3399 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3401 begin
3402 -- Internal subprograms will already have a generated body, so
3403 -- there is no need to provide a stub for them.
3405 if No (Corresponding_Body (N)) then
3406 declare
3407 Loc : constant Source_Ptr := Sloc (N);
3408 B : Node_Id;
3409 Formals : constant List_Id := Copy_Parameter_List (Ent);
3410 Nam : constant Entity_Id :=
3411 Make_Defining_Identifier (Loc, Chars (Ent));
3412 Spec : Node_Id;
3413 Stats : constant List_Id :=
3414 New_List
3415 (Make_Raise_Program_Error (Loc,
3416 Reason => PE_Access_Before_Elaboration));
3418 begin
3419 if Ekind (Ent) = E_Function then
3420 Spec :=
3421 Make_Function_Specification (Loc,
3422 Defining_Unit_Name => Nam,
3423 Parameter_Specifications => Formals,
3424 Result_Definition =>
3425 New_Copy_Tree
3426 (Result_Definition (Specification (N))));
3428 -- We cannot reliably make a return statement for this
3429 -- body, but none is needed because the call raises
3430 -- program error.
3432 Set_Return_Present (Ent);
3434 else
3435 Spec :=
3436 Make_Procedure_Specification (Loc,
3437 Defining_Unit_Name => Nam,
3438 Parameter_Specifications => Formals);
3439 end if;
3441 B := Make_Subprogram_Body (Loc,
3442 Specification => Spec,
3443 Declarations => New_List,
3444 Handled_Statement_Sequence =>
3445 Make_Handled_Sequence_Of_Statements (Loc, Stats));
3446 Insert_After (N, B);
3447 Analyze (B);
3448 end;
3449 end if;
3450 end;
3452 elsif Nkind (N) = N_Package_Declaration then
3453 declare
3454 Spec : constant Node_Id := Specification (N);
3455 begin
3456 Push_Scope (Defining_Unit_Name (Spec));
3457 Supply_Bodies (Visible_Declarations (Spec));
3458 Supply_Bodies (Private_Declarations (Spec));
3459 Pop_Scope;
3460 end;
3461 end if;
3462 end Supply_Bodies;
3464 procedure Supply_Bodies (L : List_Id) is
3465 Elmt : Node_Id;
3466 begin
3467 if Present (L) then
3468 Elmt := First (L);
3469 while Present (Elmt) loop
3470 Supply_Bodies (Elmt);
3471 Next (Elmt);
3472 end loop;
3473 end if;
3474 end Supply_Bodies;
3476 ------------
3477 -- Within --
3478 ------------
3480 function Within (E1, E2 : Entity_Id) return Boolean is
3481 Scop : Entity_Id;
3482 begin
3483 Scop := E1;
3484 loop
3485 if Scop = E2 then
3486 return True;
3487 elsif Scop = Standard_Standard then
3488 return False;
3489 else
3490 Scop := Scope (Scop);
3491 end if;
3492 end loop;
3493 end Within;
3495 --------------------------
3496 -- Within_Elaborate_All --
3497 --------------------------
3499 function Within_Elaborate_All
3500 (Unit : Unit_Number_Type;
3501 E : Entity_Id) return Boolean
3503 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3504 pragma Pack (Unit_Number_Set);
3506 Seen : Unit_Number_Set := (others => False);
3507 -- Seen (X) is True after we have seen unit X in the walk. This is used
3508 -- to prevent processing the same unit more than once.
3510 Result : Boolean := False;
3512 procedure Helper (Unit : Unit_Number_Type);
3513 -- This helper procedure does all the work for Within_Elaborate_All. It
3514 -- walks the dependency graph, and sets Result to True if it finds an
3515 -- appropriate Elaborate_All.
3517 ------------
3518 -- Helper --
3519 ------------
3521 procedure Helper (Unit : Unit_Number_Type) is
3522 CU : constant Node_Id := Cunit (Unit);
3524 Item : Node_Id;
3525 Item2 : Node_Id;
3526 Elab_Id : Entity_Id;
3527 Par : Node_Id;
3529 begin
3530 if Seen (Unit) then
3531 return;
3532 else
3533 Seen (Unit) := True;
3534 end if;
3536 -- First, check for Elaborate_Alls on this unit
3538 Item := First (Context_Items (CU));
3539 while Present (Item) loop
3540 if Nkind (Item) = N_Pragma
3541 and then Pragma_Name (Item) = Name_Elaborate_All
3542 then
3543 -- Return if some previous error on the pragma itself. The
3544 -- pragma may be unanalyzed, because of a previous error, or
3545 -- if it is the context of a subunit, inherited by its parent.
3547 if Error_Posted (Item) or else not Analyzed (Item) then
3548 return;
3549 end if;
3551 Elab_Id :=
3552 Entity
3553 (Expression (First (Pragma_Argument_Associations (Item))));
3555 if E = Elab_Id then
3556 Result := True;
3557 return;
3558 end if;
3560 Par := Parent (Unit_Declaration_Node (Elab_Id));
3562 Item2 := First (Context_Items (Par));
3563 while Present (Item2) loop
3564 if Nkind (Item2) = N_With_Clause
3565 and then Entity (Name (Item2)) = E
3566 and then not Limited_Present (Item2)
3567 then
3568 Result := True;
3569 return;
3570 end if;
3572 Next (Item2);
3573 end loop;
3574 end if;
3576 Next (Item);
3577 end loop;
3579 -- Second, recurse on with's. We could do this as part of the above
3580 -- loop, but it's probably more efficient to have two loops, because
3581 -- the relevant Elaborate_All is likely to be on the initial unit. In
3582 -- other words, we're walking the with's breadth-first. This part is
3583 -- only necessary in the dynamic elaboration model.
3585 if Dynamic_Elaboration_Checks then
3586 Item := First (Context_Items (CU));
3587 while Present (Item) loop
3588 if Nkind (Item) = N_With_Clause
3589 and then not Limited_Present (Item)
3590 then
3591 -- Note: the following call to Get_Cunit_Unit_Number does a
3592 -- linear search, which could be slow, but it's OK because
3593 -- we're about to give a warning anyway. Also, there might
3594 -- be hundreds of units, but not millions. If it turns out
3595 -- to be a problem, we could store the Get_Cunit_Unit_Number
3596 -- in each N_Compilation_Unit node, but that would involve
3597 -- rearranging N_Compilation_Unit_Aux to make room.
3599 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3601 if Result then
3602 return;
3603 end if;
3604 end if;
3606 Next (Item);
3607 end loop;
3608 end if;
3609 end Helper;
3611 -- Start of processing for Within_Elaborate_All
3613 begin
3614 Helper (Unit);
3615 return Result;
3616 end Within_Elaborate_All;
3618 end Sem_Elab;