Wno-frame-address.c: Skip on hppa*-*-*.
[official-gcc.git] / gcc / ada / sem_elab.adb
blob7f3b42a8530f678da2d79122fd2671a6b0c6b7bb
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;
552 -- In the access case emit first warning message as well,
553 -- otherwise list of calls will appear as errors.
555 elsif Elab_Warnings then
556 Error_Msg_NE (Msg_S, N, Ent);
557 end if;
559 -- Static elaboration checks, info message
561 else
562 if Elab_Info_Messages then
563 Error_Msg_NE (Msg_S, N, Ent);
564 end if;
565 end if;
566 end Elab_Warning;
568 -- Local variables
570 Loc : constant Source_Ptr := Sloc (N);
572 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
573 -- Indicates if we have instantiation case
575 Ent : Entity_Id;
576 Callee_Unit_Internal : Boolean;
577 Caller_Unit_Internal : Boolean;
578 Decl : Node_Id;
579 Inst_Callee : Source_Ptr;
580 Inst_Caller : Source_Ptr;
581 Unit_Callee : Unit_Number_Type;
582 Unit_Caller : Unit_Number_Type;
584 Body_Acts_As_Spec : Boolean;
585 -- Set to true if call is to body acting as spec (no separate spec)
587 Cunit_SC : Boolean := False;
588 -- Set to suppress dynamic elaboration checks where one of the
589 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
590 -- if a pragma Elaborate[_All] applies to that scope, in which case
591 -- warnings on the scope are also suppressed. For the internal case,
592 -- we ignore this flag.
594 E_Scope : Entity_Id;
595 -- Top level scope of entity for called subprogram. This value includes
596 -- following renamings and derivations, so this scope can be in a
597 -- non-visible unit. This is the scope that is to be investigated to
598 -- see whether an elaboration check is required.
600 Issue_In_SPARK : Boolean;
601 -- Flag set when a source entity is called during elaboration in SPARK
603 W_Scope : Entity_Id;
604 -- Top level scope of directly called entity for subprogram. This
605 -- differs from E_Scope in the case where renamings or derivations
606 -- are involved, since it does not follow these links. W_Scope is
607 -- generally in a visible unit, and it is this scope that may require
608 -- an Elaborate_All. However, there are some cases (initialization
609 -- calls and calls involving object notation) where W_Scope might not
610 -- be in the context of the current unit, and there is an intermediate
611 -- package that is, in which case the Elaborate_All has to be placed
612 -- on this intermediate package. These special cases are handled in
613 -- Set_Elaboration_Constraint.
615 -- Start of processing for Check_A_Call
617 begin
618 -- If the call is known to be within a local Suppress Elaboration
619 -- pragma, nothing to check. This can happen in task bodies. But
620 -- we ignore this for a call to a generic formal.
622 if Nkind (N) in N_Subprogram_Call
623 and then No_Elaboration_Check (N)
624 and then not Is_Call_Of_Generic_Formal (N)
625 then
626 return;
627 end if;
629 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
630 -- check, we don't mind in this case if the call occurs before the body
631 -- since this is all generated code.
633 if Nkind (Original_Node (N)) = N_Attribute_Reference
634 and then Attribute_Name (Original_Node (N)) = Name_Valid_Scalars
635 then
636 return;
637 end if;
639 -- Proceed with check
641 Ent := E;
643 -- For a variable reference, just set Body_Acts_As_Spec to False
645 if Variable_Case then
646 Body_Acts_As_Spec := False;
648 -- Additional checks for all other cases
650 else
651 -- Go to parent for derived subprogram, or to original subprogram in
652 -- the case of a renaming (Alias covers both these cases).
654 loop
655 if (Suppress_Elaboration_Warnings (Ent)
656 or else Elaboration_Checks_Suppressed (Ent))
657 and then (Inst_Case or else No (Alias (Ent)))
658 then
659 return;
660 end if;
662 -- Nothing to do for imported entities
664 if Is_Imported (Ent) then
665 return;
666 end if;
668 exit when Inst_Case or else No (Alias (Ent));
669 Ent := Alias (Ent);
670 end loop;
672 Decl := Unit_Declaration_Node (Ent);
674 if Nkind (Decl) = N_Subprogram_Body then
675 Body_Acts_As_Spec := True;
677 elsif Nkind_In (Decl, N_Subprogram_Declaration,
678 N_Subprogram_Body_Stub)
679 or else Inst_Case
680 then
681 Body_Acts_As_Spec := False;
683 -- If we have none of an instantiation, subprogram body or subprogram
684 -- declaration, or in the SPARK case, a variable reference, then
685 -- it is not a case that we want to check. (One case is a call to a
686 -- generic formal subprogram, where we do not want the check in the
687 -- template).
689 else
690 return;
691 end if;
692 end if;
694 E_Scope := Ent;
695 loop
696 if Elaboration_Checks_Suppressed (E_Scope)
697 or else Suppress_Elaboration_Warnings (E_Scope)
698 then
699 Cunit_SC := True;
700 end if;
702 -- Exit when we get to compilation unit, not counting subunits
704 exit when Is_Compilation_Unit (E_Scope)
705 and then (Is_Child_Unit (E_Scope)
706 or else Scope (E_Scope) = Standard_Standard);
708 -- If we did not find a compilation unit, other than standard,
709 -- then nothing to check (happens in some instantiation cases)
711 if E_Scope = Standard_Standard then
712 return;
714 -- Otherwise move up a scope looking for compilation unit
716 else
717 E_Scope := Scope (E_Scope);
718 end if;
719 end loop;
721 -- No checks needed for pure or preelaborated compilation units
723 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then
724 return;
725 end if;
727 -- If the generic entity is within a deeper instance than we are, then
728 -- either the instantiation to which we refer itself caused an ABE, in
729 -- which case that will be handled separately, or else we know that the
730 -- body we need appears as needed at the point of the instantiation.
731 -- However, this assumption is only valid if we are in static mode.
733 if not Dynamic_Elaboration_Checks
734 and then
735 Instantiation_Depth (Sloc (Ent)) > Instantiation_Depth (Sloc (N))
736 then
737 return;
738 end if;
740 -- Do not give a warning for a package with no body
742 if Ekind (Ent) = E_Generic_Package and then not Has_Generic_Body (N) then
743 return;
744 end if;
746 -- Find top level scope for called entity (not following renamings
747 -- or derivations). This is where the Elaborate_All will go if it is
748 -- needed. We start with the called entity, except in the case of an
749 -- initialization procedure outside the current package, where the init
750 -- proc is in the root package, and we start from the entity of the name
751 -- in the call.
753 declare
754 Ent : constant Entity_Id := Get_Referenced_Ent (N);
755 begin
756 if Is_Init_Proc (Ent) and then not In_Same_Extended_Unit (N, Ent) then
757 W_Scope := Scope (Ent);
758 else
759 W_Scope := E;
760 end if;
761 end;
763 -- Now loop through scopes to get to the enclosing compilation unit
765 while not Is_Compilation_Unit (W_Scope) loop
766 W_Scope := Scope (W_Scope);
767 end loop;
769 -- Case of entity is in same unit as call or instantiation. In the
770 -- instantiation case, W_Scope may be different from E_Scope; we want
771 -- the unit in which the instantiation occurs, since we're analyzing
772 -- based on the expansion.
774 if W_Scope = C_Scope then
775 if not Inter_Unit_Only then
776 Check_Internal_Call (N, Ent, Outer_Scope, E);
777 end if;
779 return;
780 end if;
782 -- Case of entity is not in current unit (i.e. with'ed unit case)
784 -- We are only interested in such calls if the outer call was from
785 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
787 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
788 return;
789 end if;
791 -- Nothing to do if some scope said that no checks were required
793 if Cunit_SC then
794 return;
795 end if;
797 -- Nothing to do for a generic instance, because in this case the
798 -- checking was at the point of instantiation of the generic However,
799 -- this shortcut is only applicable in static mode.
801 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
802 return;
803 end if;
805 -- Nothing to do if subprogram with no separate spec. However, a call
806 -- to Deep_Initialize may result in a call to a user-defined Initialize
807 -- procedure, which imposes a body dependency. This happens only if the
808 -- type is controlled and the Initialize procedure is not inherited.
810 if Body_Acts_As_Spec then
811 if Is_TSS (Ent, TSS_Deep_Initialize) then
812 declare
813 Typ : constant Entity_Id := Etype (First_Formal (Ent));
814 Init : Entity_Id;
816 begin
817 if not Is_Controlled (Typ) then
818 return;
819 else
820 Init := Find_Prim_Op (Typ, Name_Initialize);
822 if Comes_From_Source (Init) then
823 Ent := Init;
824 else
825 return;
826 end if;
827 end if;
828 end;
830 else
831 return;
832 end if;
833 end if;
835 -- Check cases of internal units
837 Callee_Unit_Internal :=
838 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (E_Scope)));
840 -- Do not give a warning if the with'ed unit is internal and this is
841 -- the generic instantiation case (this saves a lot of hassle dealing
842 -- with the Text_IO special child units)
844 if Callee_Unit_Internal and Inst_Case then
845 return;
846 end if;
848 if C_Scope = Standard_Standard then
849 Caller_Unit_Internal := False;
850 else
851 Caller_Unit_Internal :=
852 Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (C_Scope)));
853 end if;
855 -- Do not give a warning if the with'ed unit is internal and the
856 -- caller is not internal (since the binder always elaborates
857 -- internal units first).
859 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
860 return;
861 end if;
863 -- For now, if debug flag -gnatdE is not set, do no checking for
864 -- one internal unit withing another. This fixes the problem with
865 -- the sgi build and storage errors. To be resolved later ???
867 if (Callee_Unit_Internal and Caller_Unit_Internal)
868 and not Debug_Flag_EE
869 then
870 return;
871 end if;
873 if Is_TSS (E, TSS_Deep_Initialize) then
874 Ent := E;
875 end if;
877 -- If the call is in an instance, and the called entity is not
878 -- defined in the same instance, then the elaboration issue focuses
879 -- around the unit containing the template, it is this unit which
880 -- requires an Elaborate_All.
882 -- However, if we are doing dynamic elaboration, we need to chase the
883 -- call in the usual manner.
885 -- We also need to chase the call in the usual manner if it is a call
886 -- to a generic formal parameter, since that case was not handled as
887 -- part of the processing of the template.
889 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
890 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
892 if Inst_Caller = No_Location then
893 Unit_Caller := No_Unit;
894 else
895 Unit_Caller := Get_Source_Unit (N);
896 end if;
898 if Inst_Callee = No_Location then
899 Unit_Callee := No_Unit;
900 else
901 Unit_Callee := Get_Source_Unit (Ent);
902 end if;
904 if Unit_Caller /= No_Unit
905 and then Unit_Callee /= Unit_Caller
906 and then not Dynamic_Elaboration_Checks
907 and then not Is_Call_Of_Generic_Formal (N)
908 then
909 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
911 -- If we don't get a spec entity, just ignore call. Not quite
912 -- clear why this check is necessary. ???
914 if No (E_Scope) then
915 return;
916 end if;
918 -- Otherwise step to enclosing compilation unit
920 while not Is_Compilation_Unit (E_Scope) loop
921 E_Scope := Scope (E_Scope);
922 end loop;
924 -- For the case where N is not an instance, and is not a call within
925 -- instance to other than a generic formal, we recompute E_Scope
926 -- for the error message, since we do NOT want to go to the unit
927 -- which has the ultimate declaration in the case of renaming and
928 -- derivation and we also want to go to the generic unit in the
929 -- case of an instance, and no further.
931 else
932 -- Loop to carefully follow renamings and derivations one step
933 -- outside the current unit, but not further.
935 if not (Inst_Case or Variable_Case)
936 and then Present (Alias (Ent))
937 then
938 E_Scope := Alias (Ent);
939 else
940 E_Scope := Ent;
941 end if;
943 loop
944 while not Is_Compilation_Unit (E_Scope) loop
945 E_Scope := Scope (E_Scope);
946 end loop;
948 -- If E_Scope is the same as C_Scope, it means that there
949 -- definitely was a local renaming or derivation, and we
950 -- are not yet out of the current unit.
952 exit when E_Scope /= C_Scope;
953 Ent := Alias (Ent);
954 E_Scope := Ent;
956 -- If no alias, there is a previous error
958 if No (Ent) then
959 Check_Error_Detected;
960 return;
961 end if;
962 end loop;
963 end if;
965 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then
966 return;
967 end if;
969 Issue_In_SPARK := SPARK_Mode = On and Comes_From_Source (Ent);
971 -- Now check if an Elaborate_All (or dynamic check) is needed
973 if not Suppress_Elaboration_Warnings (Ent)
974 and then not Elaboration_Checks_Suppressed (Ent)
975 and then not Suppress_Elaboration_Warnings (E_Scope)
976 and then not Elaboration_Checks_Suppressed (E_Scope)
977 and then ((Elab_Warnings or Elab_Info_Messages)
978 or else SPARK_Mode = On)
979 and then Generate_Warnings
980 then
981 -- Instantiation case
983 if Inst_Case then
984 if Issue_In_SPARK then
985 Error_Msg_NE
986 ("instantiation of & during elaboration in SPARK", N, Ent);
987 else
988 Elab_Warning
989 ("instantiation of & may raise Program_Error?l?",
990 "info: instantiation of & during elaboration?$?", Ent);
991 end if;
993 -- Indirect call case, info message only in static elaboration
994 -- case, because the attribute reference itself cannot raise an
995 -- exception. Note that SPARK does not permit indirect calls.
997 elsif Access_Case then
998 Elab_Warning ("", "info: access to & during elaboration?$?", Ent);
1000 -- Variable reference in SPARK mode
1002 elsif Variable_Case and Issue_In_SPARK then
1003 Error_Msg_NE
1004 ("reference to & during elaboration in SPARK", N, Ent);
1006 -- Subprogram call case
1008 else
1009 if Nkind (Name (N)) in N_Has_Entity
1010 and then Is_Init_Proc (Entity (Name (N)))
1011 and then Comes_From_Source (Ent)
1012 then
1013 Elab_Warning
1014 ("implicit call to & may raise Program_Error?l?",
1015 "info: implicit call to & during elaboration?$?",
1016 Ent);
1018 elsif Issue_In_SPARK then
1019 Error_Msg_NE ("call to & during elaboration in SPARK", N, Ent);
1021 else
1022 Elab_Warning
1023 ("call to & may raise Program_Error?l?",
1024 "info: call to & during elaboration?$?",
1025 Ent);
1026 end if;
1027 end if;
1029 Error_Msg_Qual_Level := Nat'Last;
1031 -- Case of Elaborate_All not present and required, for SPARK this
1032 -- is an error, so give an error message.
1034 if Issue_In_SPARK then
1035 Error_Msg_NE ("\Elaborate_All pragma required for&", N, W_Scope);
1037 -- Otherwise we generate an implicit pragma. For a subprogram
1038 -- instantiation, Elaborate is good enough, since no transitive
1039 -- call is possible at elaboration time in this case.
1041 elsif Nkind (N) in N_Subprogram_Instantiation then
1042 Elab_Warning
1043 ("\missing pragma Elaborate for&?l?",
1044 "\implicit pragma Elaborate for& generated?$?",
1045 W_Scope);
1047 -- For all other cases, we need an implicit Elaborate_All
1049 else
1050 Elab_Warning
1051 ("\missing pragma Elaborate_All for&?l?",
1052 "\implicit pragma Elaborate_All for & generated?$?",
1053 W_Scope);
1054 end if;
1056 Error_Msg_Qual_Level := 0;
1058 -- Take into account the flags related to elaboration warning
1059 -- messages when enumerating the various calls involved. This
1060 -- ensures the proper pairing of the main warning and the
1061 -- clarification messages generated by Output_Calls.
1063 Output_Calls (N, Check_Elab_Flag => True);
1065 -- Set flag to prevent further warnings for same unit unless in
1066 -- All_Errors_Mode.
1068 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
1069 Set_Suppress_Elaboration_Warnings (W_Scope, True);
1070 end if;
1071 end if;
1073 -- Check for runtime elaboration check required
1075 if Dynamic_Elaboration_Checks then
1076 if not Elaboration_Checks_Suppressed (Ent)
1077 and then not Elaboration_Checks_Suppressed (W_Scope)
1078 and then not Elaboration_Checks_Suppressed (E_Scope)
1079 and then not Cunit_SC
1080 then
1081 -- Runtime elaboration check required. Generate check of the
1082 -- elaboration Boolean for the unit containing the entity.
1084 -- Note that for this case, we do check the real unit (the one
1085 -- from following renamings, since that is the issue).
1087 -- Could this possibly miss a useless but required PE???
1089 Insert_Elab_Check (N,
1090 Make_Attribute_Reference (Loc,
1091 Attribute_Name => Name_Elaborated,
1092 Prefix =>
1093 New_Occurrence_Of (Spec_Entity (E_Scope), Loc)));
1095 -- Prevent duplicate elaboration checks on the same call,
1096 -- which can happen if the body enclosing the call appears
1097 -- itself in a call whose elaboration check is delayed.
1099 if Nkind (N) in N_Subprogram_Call then
1100 Set_No_Elaboration_Check (N);
1101 end if;
1102 end if;
1104 -- Case of static elaboration model
1106 else
1107 -- Do not do anything if elaboration checks suppressed. Note that
1108 -- we check Ent here, not E, since we want the real entity for the
1109 -- body to see if checks are suppressed for it, not the dummy
1110 -- entry for renamings or derivations.
1112 if Elaboration_Checks_Suppressed (Ent)
1113 or else Elaboration_Checks_Suppressed (E_Scope)
1114 or else Elaboration_Checks_Suppressed (W_Scope)
1115 then
1116 null;
1118 -- Do not generate an Elaborate_All for finalization routines
1119 -- which perform partial clean up as part of initialization.
1121 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then
1122 null;
1124 -- Here we need to generate an implicit elaborate all
1126 else
1127 -- Generate Elaborate_All warning unless suppressed
1129 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case)
1130 and then not Suppress_Elaboration_Warnings (Ent)
1131 and then not Suppress_Elaboration_Warnings (E_Scope)
1132 and then not Suppress_Elaboration_Warnings (W_Scope)
1133 then
1134 Error_Msg_Node_2 := W_Scope;
1135 Error_Msg_NE
1136 ("info: call to& in elaboration code " &
1137 "requires pragma Elaborate_All on&?$?", N, E);
1138 end if;
1140 -- Set indication for binder to generate Elaborate_All
1142 Set_Elaboration_Constraint (N, E, W_Scope);
1143 end if;
1144 end if;
1145 end Check_A_Call;
1147 -----------------------------
1148 -- Check_Bad_Instantiation --
1149 -----------------------------
1151 procedure Check_Bad_Instantiation (N : Node_Id) is
1152 Ent : Entity_Id;
1154 begin
1155 -- Nothing to do if we do not have an instantiation (happens in some
1156 -- error cases, and also in the formal package declaration case)
1158 if Nkind (N) not in N_Generic_Instantiation then
1159 return;
1161 -- Nothing to do if serious errors detected (avoid cascaded errors)
1163 elsif Serious_Errors_Detected /= 0 then
1164 return;
1166 -- Nothing to do if not in full analysis mode
1168 elsif not Full_Analysis then
1169 return;
1171 -- Nothing to do if inside a generic template
1173 elsif Inside_A_Generic then
1174 return;
1176 -- Nothing to do if a library level instantiation
1178 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1179 return;
1181 -- Nothing to do if we are compiling a proper body for semantic
1182 -- purposes only. The generic body may be in another proper body.
1184 elsif
1185 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1186 then
1187 return;
1188 end if;
1190 Ent := Get_Generic_Entity (N);
1192 -- The case we are interested in is when the generic spec is in the
1193 -- current declarative part
1195 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1196 or else not In_Same_Extended_Unit (N, Ent)
1197 then
1198 return;
1199 end if;
1201 -- If the generic entity is within a deeper instance than we are, then
1202 -- either the instantiation to which we refer itself caused an ABE, in
1203 -- which case that will be handled separately. Otherwise, we know that
1204 -- the body we need appears as needed at the point of the instantiation.
1205 -- If they are both at the same level but not within the same instance
1206 -- then the body of the generic will be in the earlier instance.
1208 declare
1209 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1210 D2 : constant Int := Instantiation_Depth (Sloc (N));
1212 begin
1213 if D1 > D2 then
1214 return;
1216 elsif D1 = D2
1217 and then Is_Generic_Instance (Scope (Ent))
1218 and then not In_Open_Scopes (Scope (Ent))
1219 then
1220 return;
1221 end if;
1222 end;
1224 -- Now we can proceed, if the entity being called has a completion,
1225 -- then we are definitely OK, since we have already seen the body.
1227 if Has_Completion (Ent) then
1228 return;
1229 end if;
1231 -- If there is no body, then nothing to do
1233 if not Has_Generic_Body (N) then
1234 return;
1235 end if;
1237 -- Here we definitely have a bad instantiation
1239 Error_Msg_Warn := SPARK_Mode /= On;
1240 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent);
1242 if Present (Instance_Spec (N)) then
1243 Supply_Bodies (Instance_Spec (N));
1244 end if;
1246 Error_Msg_N ("\Program_Error [<<", N);
1247 Insert_Elab_Check (N);
1248 Set_ABE_Is_Certain (N);
1249 end Check_Bad_Instantiation;
1251 ---------------------
1252 -- Check_Elab_Call --
1253 ---------------------
1255 procedure Check_Elab_Call
1256 (N : Node_Id;
1257 Outer_Scope : Entity_Id := Empty;
1258 In_Init_Proc : Boolean := False)
1260 Ent : Entity_Id;
1261 P : Node_Id;
1263 begin
1264 -- If the reference is not in the main unit, there is nothing to check.
1265 -- Elaboration call from units in the context of the main unit will lead
1266 -- to semantic dependencies when those units are compiled.
1268 if not In_Extended_Main_Code_Unit (N) then
1269 return;
1270 end if;
1272 -- For an entry call, check relevant restriction
1274 if Nkind (N) = N_Entry_Call_Statement
1275 and then not In_Subprogram_Or_Concurrent_Unit
1276 then
1277 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1279 -- Nothing to do if this is not an expected type of reference (happens
1280 -- in some error conditions, and in some cases where rewriting occurs).
1282 elsif Nkind (N) not in N_Subprogram_Call
1283 and then Nkind (N) /= N_Attribute_Reference
1284 and then (SPARK_Mode /= On
1285 or else Nkind (N) not in N_Has_Entity
1286 or else No (Entity (N))
1287 or else Ekind (Entity (N)) /= E_Variable)
1288 then
1289 return;
1291 -- Nothing to do if this is a call already rewritten for elab checking.
1292 -- Such calls appear as the targets of If_Expressions.
1294 -- This check MUST be wrong, it catches far too much
1296 elsif Nkind (Parent (N)) = N_If_Expression then
1297 return;
1299 -- Nothing to do if inside a generic template
1301 elsif Inside_A_Generic
1302 and then No (Enclosing_Generic_Body (N))
1303 then
1304 return;
1306 -- Nothing to do if call is being pre-analyzed, as when within a
1307 -- pre/postcondition, a predicate, or an invariant.
1309 elsif In_Spec_Expression then
1310 return;
1311 end if;
1313 -- Nothing to do if this is a call to a postcondition, which is always
1314 -- within a subprogram body, even though the current scope may be the
1315 -- enclosing scope of the subprogram.
1317 if Nkind (N) = N_Procedure_Call_Statement
1318 and then Is_Entity_Name (Name (N))
1319 and then Chars (Entity (Name (N))) = Name_uPostconditions
1320 then
1321 return;
1322 end if;
1324 -- Here we have a reference at elaboration time which must be checked
1326 if Debug_Flag_LL then
1327 Write_Str (" Check_Elab_Ref: ");
1329 if Nkind (N) = N_Attribute_Reference then
1330 if not Is_Entity_Name (Prefix (N)) then
1331 Write_Str ("<<not entity name>>");
1332 else
1333 Write_Name (Chars (Entity (Prefix (N))));
1334 end if;
1336 Write_Str ("'Access");
1338 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then
1339 Write_Str ("<<not entity name>> ");
1341 else
1342 Write_Name (Chars (Entity (Name (N))));
1343 end if;
1345 Write_Str (" reference at ");
1346 Write_Location (Sloc (N));
1347 Write_Eol;
1348 end if;
1350 -- Climb up the tree to make sure we are not inside default expression
1351 -- of a parameter specification or a record component, since in both
1352 -- these cases, we will be doing the actual reference later, not now,
1353 -- and it is at the time of the actual reference (statically speaking)
1354 -- that we must do our static check, not at the time of its initial
1355 -- analysis).
1357 -- However, we have to check references within component definitions
1358 -- (e.g. a function call that determines an array component bound),
1359 -- so we terminate the loop in that case.
1361 P := Parent (N);
1362 while Present (P) loop
1363 if Nkind_In (P, N_Parameter_Specification,
1364 N_Component_Declaration)
1365 then
1366 return;
1368 -- The reference occurs within the constraint of a component,
1369 -- so it must be checked.
1371 elsif Nkind (P) = N_Component_Definition then
1372 exit;
1374 else
1375 P := Parent (P);
1376 end if;
1377 end loop;
1379 -- Stuff that happens only at the outer level
1381 if No (Outer_Scope) then
1382 Elab_Visited.Set_Last (0);
1384 -- Nothing to do if current scope is Standard (this is a bit odd, but
1385 -- it happens in the case of generic instantiations).
1387 C_Scope := Current_Scope;
1389 if C_Scope = Standard_Standard then
1390 return;
1391 end if;
1393 -- First case, we are in elaboration code
1395 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1397 if From_Elab_Code then
1399 -- Complain if ref that comes from source in preelaborated unit
1400 -- and we are not inside a subprogram (i.e. we are in elab code).
1402 if Comes_From_Source (N)
1403 and then In_Preelaborated_Unit
1404 and then not In_Inlined_Body
1405 and then Nkind (N) /= N_Attribute_Reference
1406 then
1407 -- This is a warning in GNAT mode allowing such calls to be
1408 -- used in the predefined library with appropriate care.
1410 Error_Msg_Warn := GNAT_Mode;
1411 Error_Msg_N
1412 ("<<non-static call not allowed in preelaborated unit", N);
1413 return;
1414 end if;
1416 -- Second case, we are inside a subprogram or concurrent unit, which
1417 -- means we are not in elaboration code.
1419 else
1420 -- In this case, the issue is whether we are inside the
1421 -- declarative part of the unit in which we live, or inside its
1422 -- statements. In the latter case, there is no issue of ABE calls
1423 -- at this level (a call from outside to the unit in which we live
1424 -- might cause an ABE, but that will be detected when we analyze
1425 -- that outer level call, as it recurses into the called unit).
1427 -- Climb up the tree, doing this test, and also testing for being
1428 -- inside a default expression, which, as discussed above, is not
1429 -- checked at this stage.
1431 declare
1432 P : Node_Id;
1433 L : List_Id;
1435 begin
1436 P := N;
1437 loop
1438 -- If we find a parentless subtree, it seems safe to assume
1439 -- that we are not in a declarative part and that no
1440 -- checking is required.
1442 if No (P) then
1443 return;
1444 end if;
1446 if Is_List_Member (P) then
1447 L := List_Containing (P);
1448 P := Parent (L);
1449 else
1450 L := No_List;
1451 P := Parent (P);
1452 end if;
1454 exit when Nkind (P) = N_Subunit;
1456 -- Filter out case of default expressions, where we do not
1457 -- do the check at this stage.
1459 if Nkind_In (P, N_Parameter_Specification,
1460 N_Component_Declaration)
1461 then
1462 return;
1463 end if;
1465 -- A protected body has no elaboration code and contains
1466 -- only other bodies.
1468 if Nkind (P) = N_Protected_Body then
1469 return;
1471 elsif Nkind_In (P, N_Subprogram_Body,
1472 N_Task_Body,
1473 N_Block_Statement,
1474 N_Entry_Body)
1475 then
1476 if L = Declarations (P) then
1477 exit;
1479 -- We are not in elaboration code, but we are doing
1480 -- dynamic elaboration checks, in this case, we still
1481 -- need to do the reference, since the subprogram we are
1482 -- in could be called from another unit, also in dynamic
1483 -- elaboration check mode, at elaboration time.
1485 elsif Dynamic_Elaboration_Checks then
1487 -- We provide a debug flag to disable this check. That
1488 -- way we have an easy work around for regressions
1489 -- that are caused by this new check. This debug flag
1490 -- can be removed later.
1492 if Debug_Flag_DD then
1493 return;
1494 end if;
1496 -- Do the check in this case
1498 exit;
1500 elsif Nkind (P) = N_Task_Body then
1502 -- The check is deferred until Check_Task_Activation
1503 -- but we need to capture local suppress pragmas
1504 -- that may inhibit checks on this call.
1506 Ent := Get_Referenced_Ent (N);
1508 if No (Ent) then
1509 return;
1511 elsif Elaboration_Checks_Suppressed (Current_Scope)
1512 or else Elaboration_Checks_Suppressed (Ent)
1513 or else Elaboration_Checks_Suppressed (Scope (Ent))
1514 then
1515 if Nkind (N) in N_Subprogram_Call then
1516 Set_No_Elaboration_Check (N);
1517 end if;
1518 end if;
1520 return;
1522 -- Static model, call is not in elaboration code, we
1523 -- never need to worry, because in the static model the
1524 -- top level caller always takes care of things.
1526 else
1527 return;
1528 end if;
1529 end if;
1530 end loop;
1531 end;
1532 end if;
1533 end if;
1535 Ent := Get_Referenced_Ent (N);
1537 if No (Ent) then
1538 return;
1539 end if;
1541 -- Nothing to do if this is a recursive call (i.e. a call to
1542 -- an entity that is already in the Elab_Call stack)
1544 for J in 1 .. Elab_Visited.Last loop
1545 if Ent = Elab_Visited.Table (J) then
1546 return;
1547 end if;
1548 end loop;
1550 -- See if we need to analyze this reference. We analyze it if either of
1551 -- the following conditions is met:
1553 -- It is an inner level call (since in this case it was triggered
1554 -- by an outer level call from elaboration code), but only if the
1555 -- call is within the scope of the original outer level call.
1557 -- It is an outer level reference from elaboration code, or a call to
1558 -- an entity is in the same elaboration scope.
1560 -- And in these cases, we will check both inter-unit calls and
1561 -- intra-unit (within a single unit) calls.
1563 C_Scope := Current_Scope;
1565 -- If not outer level reference, then we follow it if it is within the
1566 -- original scope of the outer reference.
1568 if Present (Outer_Scope)
1569 and then Within (Scope (Ent), Outer_Scope)
1570 then
1571 Set_C_Scope;
1572 Check_A_Call
1573 (N => N,
1574 E => Ent,
1575 Outer_Scope => Outer_Scope,
1576 Inter_Unit_Only => False,
1577 In_Init_Proc => In_Init_Proc);
1579 -- Nothing to do if elaboration checks suppressed for this scope.
1580 -- However, an interesting exception, the fact that elaboration checks
1581 -- are suppressed within an instance (because we can trace the body when
1582 -- we process the template) does not extend to calls to generic formal
1583 -- subprograms.
1585 elsif Elaboration_Checks_Suppressed (Current_Scope)
1586 and then not Is_Call_Of_Generic_Formal (N)
1587 then
1588 null;
1590 elsif From_Elab_Code then
1591 Set_C_Scope;
1592 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1594 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1595 Set_C_Scope;
1596 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1598 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1599 -- is set, then we will do the check, but only in the inter-unit case
1600 -- (this is to accommodate unguarded elaboration calls from other units
1601 -- in which this same mode is set). We don't want warnings in this case,
1602 -- it would generate warnings having nothing to do with elaboration.
1604 elsif Dynamic_Elaboration_Checks then
1605 Set_C_Scope;
1606 Check_A_Call
1608 Ent,
1609 Standard_Standard,
1610 Inter_Unit_Only => True,
1611 Generate_Warnings => False);
1613 -- Otherwise nothing to do
1615 else
1616 return;
1617 end if;
1619 -- A call to an Init_Proc in elaboration code may bring additional
1620 -- dependencies, if some of the record components thereof have
1621 -- initializations that are function calls that come from source. We
1622 -- treat the current node as a call to each of these functions, to check
1623 -- their elaboration impact.
1625 if Is_Init_Proc (Ent) and then From_Elab_Code then
1626 Process_Init_Proc : declare
1627 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1629 function Check_Init_Call (Nod : Node_Id) return Traverse_Result;
1630 -- Find subprogram calls within body of Init_Proc for Traverse
1631 -- instantiation below.
1633 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call);
1634 -- Traversal procedure to find all calls with body of Init_Proc
1636 ---------------------
1637 -- Check_Init_Call --
1638 ---------------------
1640 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is
1641 Func : Entity_Id;
1643 begin
1644 if Nkind (Nod) in N_Subprogram_Call
1645 and then Is_Entity_Name (Name (Nod))
1646 then
1647 Func := Entity (Name (Nod));
1649 if Comes_From_Source (Func) then
1650 Check_A_Call
1651 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1652 end if;
1654 return OK;
1656 else
1657 return OK;
1658 end if;
1659 end Check_Init_Call;
1661 -- Start of processing for Process_Init_Proc
1663 begin
1664 if Nkind (Unit_Decl) = N_Subprogram_Body then
1665 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1666 end if;
1667 end Process_Init_Proc;
1668 end if;
1669 end Check_Elab_Call;
1671 -----------------------
1672 -- Check_Elab_Assign --
1673 -----------------------
1675 procedure Check_Elab_Assign (N : Node_Id) is
1676 Ent : Entity_Id;
1677 Scop : Entity_Id;
1679 Pkg_Spec : Entity_Id;
1680 Pkg_Body : Entity_Id;
1682 begin
1683 -- For record or array component, check prefix. If it is an access type,
1684 -- then there is nothing to do (we do not know what is being assigned),
1685 -- but otherwise this is an assignment to the prefix.
1687 if Nkind_In (N, N_Indexed_Component,
1688 N_Selected_Component,
1689 N_Slice)
1690 then
1691 if not Is_Access_Type (Etype (Prefix (N))) then
1692 Check_Elab_Assign (Prefix (N));
1693 end if;
1695 return;
1696 end if;
1698 -- For type conversion, check expression
1700 if Nkind (N) = N_Type_Conversion then
1701 Check_Elab_Assign (Expression (N));
1702 return;
1703 end if;
1705 -- Nothing to do if this is not an entity reference otherwise get entity
1707 if Is_Entity_Name (N) then
1708 Ent := Entity (N);
1709 else
1710 return;
1711 end if;
1713 -- What we are looking for is a reference in the body of a package that
1714 -- modifies a variable declared in the visible part of the package spec.
1716 if Present (Ent)
1717 and then Comes_From_Source (N)
1718 and then not Suppress_Elaboration_Warnings (Ent)
1719 and then Ekind (Ent) = E_Variable
1720 and then not In_Private_Part (Ent)
1721 and then Is_Library_Level_Entity (Ent)
1722 then
1723 Scop := Current_Scope;
1724 loop
1725 if No (Scop) or else Scop = Standard_Standard then
1726 return;
1727 elsif Ekind (Scop) = E_Package
1728 and then Is_Compilation_Unit (Scop)
1729 then
1730 exit;
1731 else
1732 Scop := Scope (Scop);
1733 end if;
1734 end loop;
1736 -- Here Scop points to the containing library package
1738 Pkg_Spec := Scop;
1739 Pkg_Body := Body_Entity (Pkg_Spec);
1741 -- All OK if the package has an Elaborate_Body pragma
1743 if Has_Pragma_Elaborate_Body (Scop) then
1744 return;
1745 end if;
1747 -- OK if entity being modified is not in containing package spec
1749 if not In_Same_Source_Unit (Scop, Ent) then
1750 return;
1751 end if;
1753 -- All OK if entity appears in generic package or generic instance.
1754 -- We just get too messed up trying to give proper warnings in the
1755 -- presence of generics. Better no message than a junk one.
1757 Scop := Scope (Ent);
1758 while Present (Scop) and then Scop /= Pkg_Spec loop
1759 if Ekind (Scop) = E_Generic_Package then
1760 return;
1761 elsif Ekind (Scop) = E_Package
1762 and then Is_Generic_Instance (Scop)
1763 then
1764 return;
1765 end if;
1767 Scop := Scope (Scop);
1768 end loop;
1770 -- All OK if in task, don't issue warnings there
1772 if In_Task_Activation then
1773 return;
1774 end if;
1776 -- OK if no package body
1778 if No (Pkg_Body) then
1779 return;
1780 end if;
1782 -- OK if reference is not in package body
1784 if not In_Same_Source_Unit (Pkg_Body, N) then
1785 return;
1786 end if;
1788 -- OK if package body has no handled statement sequence
1790 declare
1791 HSS : constant Node_Id :=
1792 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1793 begin
1794 if No (HSS) or else not Comes_From_Source (HSS) then
1795 return;
1796 end if;
1797 end;
1799 -- We definitely have a case of a modification of an entity in
1800 -- the package spec from the elaboration code of the package body.
1801 -- We may not give the warning (because there are some additional
1802 -- checks to avoid too many false positives), but it would be a good
1803 -- idea for the binder to try to keep the body elaboration close to
1804 -- the spec elaboration.
1806 Set_Elaborate_Body_Desirable (Pkg_Spec);
1808 -- All OK in gnat mode (we know what we are doing)
1810 if GNAT_Mode then
1811 return;
1812 end if;
1814 -- All OK if all warnings suppressed
1816 if Warning_Mode = Suppress then
1817 return;
1818 end if;
1820 -- All OK if elaboration checks suppressed for entity
1822 if Checks_May_Be_Suppressed (Ent)
1823 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1824 then
1825 return;
1826 end if;
1828 -- OK if the entity is initialized. Note that the No_Initialization
1829 -- flag usually means that the initialization has been rewritten into
1830 -- assignments, but that still counts for us.
1832 declare
1833 Decl : constant Node_Id := Declaration_Node (Ent);
1834 begin
1835 if Nkind (Decl) = N_Object_Declaration
1836 and then (Present (Expression (Decl))
1837 or else No_Initialization (Decl))
1838 then
1839 return;
1840 end if;
1841 end;
1843 -- Here is where we give the warning
1845 -- All OK if warnings suppressed on the entity
1847 if not Has_Warnings_Off (Ent) then
1848 Error_Msg_Sloc := Sloc (Ent);
1850 Error_Msg_NE
1851 ("??& can be accessed by clients before this initialization",
1852 N, Ent);
1853 Error_Msg_NE
1854 ("\??add Elaborate_Body to spec to ensure & is initialized",
1855 N, Ent);
1856 end if;
1858 if not All_Errors_Mode then
1859 Set_Suppress_Elaboration_Warnings (Ent);
1860 end if;
1861 end if;
1862 end Check_Elab_Assign;
1864 ----------------------
1865 -- Check_Elab_Calls --
1866 ----------------------
1868 procedure Check_Elab_Calls is
1869 begin
1870 -- If expansion is disabled, do not generate any checks. Also skip
1871 -- checks if any subunits are missing because in either case we lack the
1872 -- full information that we need, and no object file will be created in
1873 -- any case.
1875 if not Expander_Active
1876 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1877 or else Subunits_Missing
1878 then
1879 return;
1880 end if;
1882 -- Skip delayed calls if we had any errors
1884 if Serious_Errors_Detected = 0 then
1885 Delaying_Elab_Checks := False;
1886 Expander_Mode_Save_And_Set (True);
1888 for J in Delay_Check.First .. Delay_Check.Last loop
1889 Push_Scope (Delay_Check.Table (J).Curscop);
1890 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1892 Check_Internal_Call_Continue (
1893 N => Delay_Check.Table (J).N,
1894 E => Delay_Check.Table (J).E,
1895 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1896 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1898 Pop_Scope;
1899 end loop;
1901 -- Set Delaying_Elab_Checks back on for next main compilation
1903 Expander_Mode_Restore;
1904 Delaying_Elab_Checks := True;
1905 end if;
1906 end Check_Elab_Calls;
1908 ------------------------------
1909 -- Check_Elab_Instantiation --
1910 ------------------------------
1912 procedure Check_Elab_Instantiation
1913 (N : Node_Id;
1914 Outer_Scope : Entity_Id := Empty)
1916 Ent : Entity_Id;
1918 begin
1919 -- Check for and deal with bad instantiation case. There is some
1920 -- duplicated code here, but we will worry about this later ???
1922 Check_Bad_Instantiation (N);
1924 if ABE_Is_Certain (N) then
1925 return;
1926 end if;
1928 -- Nothing to do if we do not have an instantiation (happens in some
1929 -- error cases, and also in the formal package declaration case)
1931 if Nkind (N) not in N_Generic_Instantiation then
1932 return;
1933 end if;
1935 -- Nothing to do if inside a generic template
1937 if Inside_A_Generic then
1938 return;
1939 end if;
1941 -- Nothing to do if the instantiation is not in the main unit
1943 if not In_Extended_Main_Code_Unit (N) then
1944 return;
1945 end if;
1947 Ent := Get_Generic_Entity (N);
1948 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1950 -- See if we need to analyze this instantiation. We analyze it if
1951 -- either of the following conditions is met:
1953 -- It is an inner level instantiation (since in this case it was
1954 -- triggered by an outer level call from elaboration code), but
1955 -- only if the instantiation is within the scope of the original
1956 -- outer level call.
1958 -- It is an outer level instantiation from elaboration code, or the
1959 -- instantiated entity is in the same elaboration scope.
1961 -- And in these cases, we will check both the inter-unit case and
1962 -- the intra-unit (within a single unit) case.
1964 C_Scope := Current_Scope;
1966 if Present (Outer_Scope) and then Within (Scope (Ent), Outer_Scope) then
1967 Set_C_Scope;
1968 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1970 elsif From_Elab_Code then
1971 Set_C_Scope;
1972 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1974 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1975 Set_C_Scope;
1976 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1978 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
1979 -- set, then we will do the check, but only in the inter-unit case (this
1980 -- is to accommodate unguarded elaboration calls from other units in
1981 -- which this same mode is set). We inhibit warnings in this case, since
1982 -- this instantiation is not occurring in elaboration code.
1984 elsif Dynamic_Elaboration_Checks then
1985 Set_C_Scope;
1986 Check_A_Call
1988 Ent,
1989 Standard_Standard,
1990 Inter_Unit_Only => True,
1991 Generate_Warnings => False);
1993 else
1994 return;
1995 end if;
1996 end Check_Elab_Instantiation;
1998 -------------------------
1999 -- Check_Internal_Call --
2000 -------------------------
2002 procedure Check_Internal_Call
2003 (N : Node_Id;
2004 E : Entity_Id;
2005 Outer_Scope : Entity_Id;
2006 Orig_Ent : Entity_Id)
2008 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
2010 begin
2011 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2012 -- node comes from source.
2014 if Nkind (N) = N_Attribute_Reference and then
2015 (not Warn_On_Elab_Access or else not Comes_From_Source (N))
2016 then
2017 return;
2019 -- If not function or procedure call, instantiation, or 'Access, then
2020 -- ignore call (this happens in some error cases and rewriting cases).
2022 elsif not Nkind_In
2023 (N, N_Function_Call,
2024 N_Procedure_Call_Statement,
2025 N_Attribute_Reference)
2026 and then not Inst_Case
2027 then
2028 return;
2030 -- Nothing to do if this is a call or instantiation that has already
2031 -- been found to be a sure ABE.
2033 elsif Nkind (N) /= N_Attribute_Reference and then ABE_Is_Certain (N) then
2034 return;
2036 -- Nothing to do if errors already detected (avoid cascaded errors)
2038 elsif Serious_Errors_Detected /= 0 then
2039 return;
2041 -- Nothing to do if not in full analysis mode
2043 elsif not Full_Analysis then
2044 return;
2046 -- Nothing to do if analyzing in special spec-expression mode, since the
2047 -- call is not actually being made at this time.
2049 elsif In_Spec_Expression then
2050 return;
2052 -- Nothing to do for call to intrinsic subprogram
2054 elsif Is_Intrinsic_Subprogram (E) then
2055 return;
2057 -- No need to trace local calls if checking task activation, because
2058 -- other local bodies are elaborated already.
2060 elsif In_Task_Activation then
2061 return;
2063 -- Nothing to do if call is within a generic unit
2065 elsif Inside_A_Generic then
2066 return;
2067 end if;
2069 -- Delay this call if we are still delaying calls
2071 if Delaying_Elab_Checks then
2072 Delay_Check.Append (
2073 (N => N,
2074 E => E,
2075 Orig_Ent => Orig_Ent,
2076 Curscop => Current_Scope,
2077 Outer_Scope => Outer_Scope,
2078 From_Elab_Code => From_Elab_Code));
2079 return;
2081 -- Otherwise, call phase 2 continuation right now
2083 else
2084 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
2085 end if;
2086 end Check_Internal_Call;
2088 ----------------------------------
2089 -- Check_Internal_Call_Continue --
2090 ----------------------------------
2092 procedure Check_Internal_Call_Continue
2093 (N : Node_Id;
2094 E : Entity_Id;
2095 Outer_Scope : Entity_Id;
2096 Orig_Ent : Entity_Id)
2098 Loc : constant Source_Ptr := Sloc (N);
2099 Inst_Case : constant Boolean := Is_Generic_Unit (E);
2101 Sbody : Node_Id;
2102 Ebody : Entity_Id;
2104 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
2105 -- Function applied to each node as we traverse the body. Checks for
2106 -- call or entity reference that needs checking, and if so checks it.
2107 -- Always returns OK, so entire tree is traversed, except that as
2108 -- described below subprogram bodies are skipped for now.
2110 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
2111 -- Traverse procedure using above Find_Elab_Reference function
2113 -------------------------
2114 -- Find_Elab_Reference --
2115 -------------------------
2117 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
2118 Actual : Node_Id;
2120 begin
2121 -- If user has specified that there are no entry calls in elaboration
2122 -- code, do not trace past an accept statement, because the rendez-
2123 -- vous will happen after elaboration.
2125 if Nkind_In (Original_Node (N), N_Accept_Statement,
2126 N_Selective_Accept)
2127 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2128 then
2129 return Abandon;
2131 -- If we have a function call, check it
2133 elsif Nkind (N) = N_Function_Call then
2134 Check_Elab_Call (N, Outer_Scope);
2135 return OK;
2137 -- If we have a procedure call, check the call, and also check
2138 -- arguments that are assignments (OUT or IN OUT mode formals).
2140 elsif Nkind (N) = N_Procedure_Call_Statement then
2141 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E));
2143 Actual := First_Actual (N);
2144 while Present (Actual) loop
2145 if Known_To_Be_Assigned (Actual) then
2146 Check_Elab_Assign (Actual);
2147 end if;
2149 Next_Actual (Actual);
2150 end loop;
2152 return OK;
2154 -- If we have an access attribute for a subprogram, check it.
2155 -- Suppress this behavior under debug flag.
2157 elsif not Debug_Flag_Dot_UU
2158 and then Nkind (N) = N_Attribute_Reference
2159 and then Nam_In (Attribute_Name (N), Name_Access,
2160 Name_Unrestricted_Access)
2161 and then Is_Entity_Name (Prefix (N))
2162 and then Is_Subprogram (Entity (Prefix (N)))
2163 then
2164 Check_Elab_Call (N, Outer_Scope);
2165 return OK;
2167 -- In SPARK mode, if we have an entity reference to a variable, then
2168 -- check it. For now we consider any reference.
2170 elsif SPARK_Mode = On
2171 and then Nkind (N) in N_Has_Entity
2172 and then Present (Entity (N))
2173 and then Ekind (Entity (N)) = E_Variable
2174 then
2175 Check_Elab_Call (N, Outer_Scope);
2176 return OK;
2178 -- If we have a generic instantiation, check it
2180 elsif Nkind (N) in N_Generic_Instantiation then
2181 Check_Elab_Instantiation (N, Outer_Scope);
2182 return OK;
2184 -- Skip subprogram bodies that come from source (wait for call to
2185 -- analyze these). The reason for the come from source test is to
2186 -- avoid catching task bodies.
2188 -- For task bodies, we should really avoid these too, waiting for the
2189 -- task activation, but that's too much trouble to catch for now, so
2190 -- we go in unconditionally. This is not so terrible, it means the
2191 -- error backtrace is not quite complete, and we are too eager to
2192 -- scan bodies of tasks that are unused, but this is hardly very
2193 -- significant.
2195 elsif Nkind (N) = N_Subprogram_Body
2196 and then Comes_From_Source (N)
2197 then
2198 return Skip;
2200 elsif Nkind (N) = N_Assignment_Statement
2201 and then Comes_From_Source (N)
2202 then
2203 Check_Elab_Assign (Name (N));
2204 return OK;
2206 else
2207 return OK;
2208 end if;
2209 end Find_Elab_Reference;
2211 -- Start of processing for Check_Internal_Call_Continue
2213 begin
2214 -- Save outer level call if at outer level
2216 if Elab_Call.Last = 0 then
2217 Outer_Level_Sloc := Loc;
2218 end if;
2220 Elab_Visited.Append (E);
2222 -- If the call is to a function that renames a literal, no check needed
2224 if Ekind (E) = E_Enumeration_Literal then
2225 return;
2226 end if;
2228 Sbody := Unit_Declaration_Node (E);
2230 if not Nkind_In (Sbody, N_Subprogram_Body, N_Package_Body) then
2231 Ebody := Corresponding_Body (Sbody);
2233 if No (Ebody) then
2234 return;
2235 else
2236 Sbody := Unit_Declaration_Node (Ebody);
2237 end if;
2238 end if;
2240 -- If the body appears after the outer level call or instantiation then
2241 -- we have an error case handled below.
2243 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2244 and then not In_Task_Activation
2245 then
2246 null;
2248 -- If we have the instantiation case we are done, since we now
2249 -- know that the body of the generic appeared earlier.
2251 elsif Inst_Case then
2252 return;
2254 -- Otherwise we have a call, so we trace through the called body to see
2255 -- if it has any problems.
2257 else
2258 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2260 Elab_Call.Append ((Cloc => Loc, Ent => E));
2262 if Debug_Flag_LL then
2263 Write_Str ("Elab_Call.Last = ");
2264 Write_Int (Int (Elab_Call.Last));
2265 Write_Str (" Ent = ");
2266 Write_Name (Chars (E));
2267 Write_Str (" at ");
2268 Write_Location (Sloc (N));
2269 Write_Eol;
2270 end if;
2272 -- Now traverse declarations and statements of subprogram body. Note
2273 -- that we cannot simply Traverse (Sbody), since traverse does not
2274 -- normally visit subprogram bodies.
2276 declare
2277 Decl : Node_Id;
2278 begin
2279 Decl := First (Declarations (Sbody));
2280 while Present (Decl) loop
2281 Traverse (Decl);
2282 Next (Decl);
2283 end loop;
2284 end;
2286 Traverse (Handled_Statement_Sequence (Sbody));
2288 Elab_Call.Decrement_Last;
2289 return;
2290 end if;
2292 -- Here is the case of calling a subprogram where the body has not yet
2293 -- been encountered. A warning message is needed, except if this is the
2294 -- case of appearing within an aspect specification that results in
2295 -- a check call, we do not really have such a situation, so no warning
2296 -- is needed (e.g. the case of a precondition, where the call appears
2297 -- textually before the body, but in actual fact is moved to the
2298 -- appropriate subprogram body and so does not need a check).
2300 declare
2301 P : Node_Id;
2302 O : Node_Id;
2304 begin
2305 P := Parent (N);
2306 loop
2307 -- Keep looking at parents if we are still in the subexpression
2309 if Nkind (P) in N_Subexpr then
2310 P := Parent (P);
2312 -- Here P is the parent of the expression, check for special case
2314 else
2315 O := Original_Node (P);
2317 -- Definitely not the special case if orig node is not a pragma
2319 exit when Nkind (O) /= N_Pragma;
2321 -- Check we have an If statement or a null statement (happens
2322 -- when the If has been expanded to be True).
2324 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement);
2326 -- Our special case will be indicated either by the pragma
2327 -- coming from an aspect ...
2329 if Present (Corresponding_Aspect (O)) then
2330 return;
2332 -- Or, in the case of an initial condition, specifically by a
2333 -- Check pragma specifying an Initial_Condition check.
2335 elsif Pragma_Name (O) = Name_Check
2336 and then
2337 Chars
2338 (Expression (First (Pragma_Argument_Associations (O)))) =
2339 Name_Initial_Condition
2340 then
2341 return;
2343 -- For anything else, we have an error
2345 else
2346 exit;
2347 end if;
2348 end if;
2349 end loop;
2350 end;
2352 -- Not that special case, warning and dynamic check is required
2354 -- If we have nothing in the call stack, then this is at the outer
2355 -- level, and the ABE is bound to occur, unless it's a 'Access.
2357 if Elab_Call.Last = 0 then
2358 Error_Msg_Warn := SPARK_Mode /= On;
2360 if Inst_Case then
2361 Error_Msg_NE
2362 ("cannot instantiate& before body seen<<", N, Orig_Ent);
2363 elsif Nkind (N) /= N_Attribute_Reference then
2364 Error_Msg_NE
2365 ("cannot call& before body seen<<", N, Orig_Ent);
2366 else
2367 Error_Msg_NE
2368 ("Access attribute of & before body seen<<", N, Orig_Ent);
2369 Error_Msg_N ("\possible Program_Error on later references<", N);
2370 end if;
2372 if Nkind (N) /= N_Attribute_Reference then
2373 Error_Msg_N ("\Program_Error [<<", N);
2374 Insert_Elab_Check (N);
2375 end if;
2377 -- Call is not at outer level
2379 else
2380 -- Deal with dynamic elaboration check
2382 if not Elaboration_Checks_Suppressed (E) then
2383 Set_Elaboration_Entity_Required (E);
2385 -- Case of no elaboration entity allocated yet
2387 if No (Elaboration_Entity (E)) then
2389 -- Create object declaration for elaboration entity, and put it
2390 -- just in front of the spec of the subprogram or generic unit,
2391 -- in the same scope as this unit. The subprogram may be over-
2392 -- loaded, so make the name of elaboration entity unique by
2393 -- means of a numeric suffix.
2395 declare
2396 Loce : constant Source_Ptr := Sloc (E);
2397 Ent : constant Entity_Id :=
2398 Make_Defining_Identifier (Loc,
2399 Chars => New_External_Name (Chars (E), 'E', -1));
2401 begin
2402 Set_Elaboration_Entity (E, Ent);
2403 Push_Scope (Scope (E));
2405 Insert_Action (Declaration_Node (E),
2406 Make_Object_Declaration (Loce,
2407 Defining_Identifier => Ent,
2408 Object_Definition =>
2409 New_Occurrence_Of (Standard_Short_Integer, Loce),
2410 Expression =>
2411 Make_Integer_Literal (Loc, Uint_0)));
2413 -- Set elaboration flag at the point of the body
2415 Set_Elaboration_Flag (Sbody, E);
2417 -- Kill current value indication. This is necessary because
2418 -- the tests of this flag are inserted out of sequence and
2419 -- must not pick up bogus indications of the wrong constant
2420 -- value. Also, this is never a true constant, since one way
2421 -- or another, it gets reset.
2423 Set_Current_Value (Ent, Empty);
2424 Set_Last_Assignment (Ent, Empty);
2425 Set_Is_True_Constant (Ent, False);
2426 Pop_Scope;
2427 end;
2428 end if;
2430 -- Generate check of the elaboration counter
2432 Insert_Elab_Check (N,
2433 Make_Attribute_Reference (Loc,
2434 Attribute_Name => Name_Elaborated,
2435 Prefix => New_Occurrence_Of (E, Loc)));
2436 end if;
2438 -- Generate the warning
2440 if not Suppress_Elaboration_Warnings (E)
2441 and then not Elaboration_Checks_Suppressed (E)
2443 -- Suppress this warning if we have a function call that occurred
2444 -- within an assertion expression, since we can get false warnings
2445 -- in this case, due to the out of order handling in this case.
2447 and then
2448 (Nkind (Original_Node (N)) /= N_Function_Call
2449 or else not In_Assertion_Expression_Pragma (Original_Node (N)))
2450 then
2451 Error_Msg_Warn := SPARK_Mode /= On;
2453 if Inst_Case then
2454 Error_Msg_NE
2455 ("instantiation of& may occur before body is seen<l<",
2456 N, Orig_Ent);
2457 else
2458 -- A rather specific check. For Finalize/Adjust/Initialize,
2459 -- if the type has Warnings_Off set, suppress the warning.
2461 if Nam_In (Chars (E), Name_Adjust,
2462 Name_Finalize,
2463 Name_Initialize)
2464 and then Present (First_Formal (E))
2465 then
2466 declare
2467 T : constant Entity_Id := Etype (First_Formal (E));
2468 begin
2469 if Is_Controlled (T) then
2470 if Warnings_Off (T)
2471 or else (Ekind (T) = E_Private_Type
2472 and then Warnings_Off (Full_View (T)))
2473 then
2474 goto Output;
2475 end if;
2476 end if;
2477 end;
2478 end if;
2480 -- Go ahead and give warning if not this special case
2482 Error_Msg_NE
2483 ("call to& may occur before body is seen<l<", N, Orig_Ent);
2484 end if;
2486 Error_Msg_N ("\Program_Error ]<l<", N);
2488 -- There is no need to query the elaboration warning message flags
2489 -- because the main message is an error, not a warning, therefore
2490 -- all the clarification messages produces by Output_Calls must be
2491 -- emitted unconditionally.
2493 <<Output>>
2495 Output_Calls (N, Check_Elab_Flag => False);
2496 end if;
2497 end if;
2499 -- Set flag to suppress further warnings on same subprogram
2500 -- unless in all errors mode
2502 if not All_Errors_Mode then
2503 Set_Suppress_Elaboration_Warnings (E);
2504 end if;
2505 end Check_Internal_Call_Continue;
2507 ---------------------------
2508 -- Check_Task_Activation --
2509 ---------------------------
2511 procedure Check_Task_Activation (N : Node_Id) is
2512 Loc : constant Source_Ptr := Sloc (N);
2513 Inter_Procs : constant Elist_Id := New_Elmt_List;
2514 Intra_Procs : constant Elist_Id := New_Elmt_List;
2515 Ent : Entity_Id;
2516 P : Entity_Id;
2517 Task_Scope : Entity_Id;
2518 Cunit_SC : Boolean := False;
2519 Decl : Node_Id;
2520 Elmt : Elmt_Id;
2521 Enclosing : Entity_Id;
2523 procedure Add_Task_Proc (Typ : Entity_Id);
2524 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2525 -- For record types, this procedure recurses over component types.
2527 procedure Collect_Tasks (Decls : List_Id);
2528 -- Collect the types of the tasks that are to be activated in the given
2529 -- list of declarations, in order to perform elaboration checks on the
2530 -- corresponding task procedures which are called implicitly here.
2532 function Outer_Unit (E : Entity_Id) return Entity_Id;
2533 -- find enclosing compilation unit of Entity, ignoring subunits, or
2534 -- else enclosing subprogram. If E is not a package, there is no need
2535 -- for inter-unit elaboration checks.
2537 -------------------
2538 -- Add_Task_Proc --
2539 -------------------
2541 procedure Add_Task_Proc (Typ : Entity_Id) is
2542 Comp : Entity_Id;
2543 Proc : Entity_Id := Empty;
2545 begin
2546 if Is_Task_Type (Typ) then
2547 Proc := Get_Task_Body_Procedure (Typ);
2549 elsif Is_Array_Type (Typ)
2550 and then Has_Task (Base_Type (Typ))
2551 then
2552 Add_Task_Proc (Component_Type (Typ));
2554 elsif Is_Record_Type (Typ)
2555 and then Has_Task (Base_Type (Typ))
2556 then
2557 Comp := First_Component (Typ);
2558 while Present (Comp) loop
2559 Add_Task_Proc (Etype (Comp));
2560 Comp := Next_Component (Comp);
2561 end loop;
2562 end if;
2564 -- If the task type is another unit, we will perform the usual
2565 -- elaboration check on its enclosing unit. If the type is in the
2566 -- same unit, we can trace the task body as for an internal call,
2567 -- but we only need to examine other external calls, because at
2568 -- the point the task is activated, internal subprogram bodies
2569 -- will have been elaborated already. We keep separate lists for
2570 -- each kind of task.
2572 -- Skip this test if errors have occurred, since in this case
2573 -- we can get false indications.
2575 if Serious_Errors_Detected /= 0 then
2576 return;
2577 end if;
2579 if Present (Proc) then
2580 if Outer_Unit (Scope (Proc)) = Enclosing then
2582 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2583 and then
2584 (not Is_Generic_Instance (Scope (Proc))
2585 or else Scope (Proc) = Scope (Defining_Identifier (Decl)))
2586 then
2587 Error_Msg_Warn := SPARK_Mode /= On;
2588 Error_Msg_N
2589 ("task will be activated before elaboration of its body<<",
2590 Decl);
2591 Error_Msg_N ("\Program_Error [<<", Decl);
2593 elsif Present
2594 (Corresponding_Body (Unit_Declaration_Node (Proc)))
2595 then
2596 Append_Elmt (Proc, Intra_Procs);
2597 end if;
2599 else
2600 -- No need for multiple entries of the same type
2602 Elmt := First_Elmt (Inter_Procs);
2603 while Present (Elmt) loop
2604 if Node (Elmt) = Proc then
2605 return;
2606 end if;
2608 Next_Elmt (Elmt);
2609 end loop;
2611 Append_Elmt (Proc, Inter_Procs);
2612 end if;
2613 end if;
2614 end Add_Task_Proc;
2616 -------------------
2617 -- Collect_Tasks --
2618 -------------------
2620 procedure Collect_Tasks (Decls : List_Id) is
2621 begin
2622 if Present (Decls) then
2623 Decl := First (Decls);
2624 while Present (Decl) loop
2625 if Nkind (Decl) = N_Object_Declaration
2626 and then Has_Task (Etype (Defining_Identifier (Decl)))
2627 then
2628 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2629 end if;
2631 Next (Decl);
2632 end loop;
2633 end if;
2634 end Collect_Tasks;
2636 ----------------
2637 -- Outer_Unit --
2638 ----------------
2640 function Outer_Unit (E : Entity_Id) return Entity_Id is
2641 Outer : Entity_Id;
2643 begin
2644 Outer := E;
2645 while Present (Outer) loop
2646 if Elaboration_Checks_Suppressed (Outer) then
2647 Cunit_SC := True;
2648 end if;
2650 exit when Is_Child_Unit (Outer)
2651 or else Scope (Outer) = Standard_Standard
2652 or else Ekind (Outer) /= E_Package;
2653 Outer := Scope (Outer);
2654 end loop;
2656 return Outer;
2657 end Outer_Unit;
2659 -- Start of processing for Check_Task_Activation
2661 begin
2662 Enclosing := Outer_Unit (Current_Scope);
2664 -- Find all tasks declared in the current unit
2666 if Nkind (N) = N_Package_Body then
2667 P := Unit_Declaration_Node (Corresponding_Spec (N));
2669 Collect_Tasks (Declarations (N));
2670 Collect_Tasks (Visible_Declarations (Specification (P)));
2671 Collect_Tasks (Private_Declarations (Specification (P)));
2673 elsif Nkind (N) = N_Package_Declaration then
2674 Collect_Tasks (Visible_Declarations (Specification (N)));
2675 Collect_Tasks (Private_Declarations (Specification (N)));
2677 else
2678 Collect_Tasks (Declarations (N));
2679 end if;
2681 -- We only perform detailed checks in all tasks that are library level
2682 -- entities. If the master is a subprogram or task, activation will
2683 -- depend on the activation of the master itself.
2685 -- Should dynamic checks be added in the more general case???
2687 if Ekind (Enclosing) /= E_Package then
2688 return;
2689 end if;
2691 -- For task types defined in other units, we want the unit containing
2692 -- the task body to be elaborated before the current one.
2694 Elmt := First_Elmt (Inter_Procs);
2695 while Present (Elmt) loop
2696 Ent := Node (Elmt);
2697 Task_Scope := Outer_Unit (Scope (Ent));
2699 if not Is_Compilation_Unit (Task_Scope) then
2700 null;
2702 elsif Suppress_Elaboration_Warnings (Task_Scope)
2703 or else Elaboration_Checks_Suppressed (Task_Scope)
2704 then
2705 null;
2707 elsif Dynamic_Elaboration_Checks then
2708 if not Elaboration_Checks_Suppressed (Ent)
2709 and then not Cunit_SC
2710 and then
2711 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2712 then
2713 -- Runtime elaboration check required. Generate check of the
2714 -- elaboration counter for the unit containing the entity.
2716 Insert_Elab_Check (N,
2717 Make_Attribute_Reference (Loc,
2718 Attribute_Name => Name_Elaborated,
2719 Prefix =>
2720 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc)));
2721 end if;
2723 else
2724 -- Force the binder to elaborate other unit first
2726 if not Suppress_Elaboration_Warnings (Ent)
2727 and then not Elaboration_Checks_Suppressed (Ent)
2728 and then Elab_Info_Messages
2729 and then not Suppress_Elaboration_Warnings (Task_Scope)
2730 and then not Elaboration_Checks_Suppressed (Task_Scope)
2731 then
2732 Error_Msg_Node_2 := Task_Scope;
2733 Error_Msg_NE
2734 ("info: activation of an instance of task type&" &
2735 " requires pragma Elaborate_All on &?$?", N, Ent);
2736 end if;
2738 Activate_Elaborate_All_Desirable (N, Task_Scope);
2739 Set_Suppress_Elaboration_Warnings (Task_Scope);
2740 end if;
2742 Next_Elmt (Elmt);
2743 end loop;
2745 -- For tasks declared in the current unit, trace other calls within
2746 -- the task procedure bodies, which are available.
2748 In_Task_Activation := True;
2750 Elmt := First_Elmt (Intra_Procs);
2751 while Present (Elmt) loop
2752 Ent := Node (Elmt);
2753 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2754 Next_Elmt (Elmt);
2755 end loop;
2757 In_Task_Activation := False;
2758 end Check_Task_Activation;
2760 -------------------------------
2761 -- Is_Call_Of_Generic_Formal --
2762 -------------------------------
2764 function Is_Call_Of_Generic_Formal (N : Node_Id) return Boolean is
2765 begin
2766 return Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2768 -- Always return False if debug flag -gnatd.G is set
2770 and then not Debug_Flag_Dot_GG
2772 -- For now, we detect this by looking for the strange identifier
2773 -- node, whose Chars reflect the name of the generic formal, but
2774 -- the Chars of the Entity references the generic actual.
2776 and then Nkind (Name (N)) = N_Identifier
2777 and then Chars (Name (N)) /= Chars (Entity (Name (N)));
2778 end Is_Call_Of_Generic_Formal;
2780 --------------------------------
2781 -- Set_Elaboration_Constraint --
2782 --------------------------------
2784 procedure Set_Elaboration_Constraint
2785 (Call : Node_Id;
2786 Subp : Entity_Id;
2787 Scop : Entity_Id)
2789 Elab_Unit : Entity_Id;
2791 -- Check whether this is a call to an Initialize subprogram for a
2792 -- controlled type. Note that Call can also be a 'Access attribute
2793 -- reference, which now generates an elaboration check.
2795 Init_Call : constant Boolean :=
2796 Nkind (Call) = N_Procedure_Call_Statement
2797 and then Chars (Subp) = Name_Initialize
2798 and then Comes_From_Source (Subp)
2799 and then Present (Parameter_Associations (Call))
2800 and then Is_Controlled (Etype (First_Actual (Call)));
2801 begin
2802 -- If the unit is mentioned in a with_clause of the current unit, it is
2803 -- visible, and we can set the elaboration flag.
2805 if Is_Immediately_Visible (Scop)
2806 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop))
2807 then
2808 Activate_Elaborate_All_Desirable (Call, Scop);
2809 Set_Suppress_Elaboration_Warnings (Scop, True);
2810 return;
2811 end if;
2813 -- If this is not an initialization call or a call using object notation
2814 -- we know that the unit of the called entity is in the context, and
2815 -- we can set the flag as well. The unit need not be visible if the call
2816 -- occurs within an instantiation.
2818 if Is_Init_Proc (Subp)
2819 or else Init_Call
2820 or else Nkind (Original_Node (Call)) = N_Selected_Component
2821 then
2822 null; -- detailed processing follows.
2824 else
2825 Activate_Elaborate_All_Desirable (Call, Scop);
2826 Set_Suppress_Elaboration_Warnings (Scop, True);
2827 return;
2828 end if;
2830 -- If the unit is not in the context, there must be an intermediate unit
2831 -- that is, on which we need to place to elaboration flag. This happens
2832 -- with init proc calls.
2834 if Is_Init_Proc (Subp) or else Init_Call then
2836 -- The initialization call is on an object whose type is not declared
2837 -- in the same scope as the subprogram. The type of the object must
2838 -- be a subtype of the type of operation. This object is the first
2839 -- actual in the call.
2841 declare
2842 Typ : constant Entity_Id :=
2843 Etype (First (Parameter_Associations (Call)));
2844 begin
2845 Elab_Unit := Scope (Typ);
2846 while (Present (Elab_Unit))
2847 and then not Is_Compilation_Unit (Elab_Unit)
2848 loop
2849 Elab_Unit := Scope (Elab_Unit);
2850 end loop;
2851 end;
2853 -- If original node uses selected component notation, the prefix is
2854 -- visible and determines the scope that must be elaborated. After
2855 -- rewriting, the prefix is the first actual in the call.
2857 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2858 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2860 -- Not one of special cases above
2862 else
2863 -- Using previously computed scope. If the elaboration check is
2864 -- done after analysis, the scope is not visible any longer, but
2865 -- must still be in the context.
2867 Elab_Unit := Scop;
2868 end if;
2870 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2871 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2872 end Set_Elaboration_Constraint;
2874 ------------------------
2875 -- Get_Referenced_Ent --
2876 ------------------------
2878 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is
2879 Nam : Node_Id;
2881 begin
2882 if Nkind (N) in N_Has_Entity
2883 and then Present (Entity (N))
2884 and then Ekind (Entity (N)) = E_Variable
2885 then
2886 return Entity (N);
2887 end if;
2889 if Nkind (N) = N_Attribute_Reference then
2890 Nam := Prefix (N);
2891 else
2892 Nam := Name (N);
2893 end if;
2895 if No (Nam) then
2896 return Empty;
2897 elsif Nkind (Nam) = N_Selected_Component then
2898 return Entity (Selector_Name (Nam));
2899 elsif not Is_Entity_Name (Nam) then
2900 return Empty;
2901 else
2902 return Entity (Nam);
2903 end if;
2904 end Get_Referenced_Ent;
2906 ----------------------
2907 -- Has_Generic_Body --
2908 ----------------------
2910 function Has_Generic_Body (N : Node_Id) return Boolean is
2911 Ent : constant Entity_Id := Get_Generic_Entity (N);
2912 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2913 Scop : Entity_Id;
2915 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2916 -- Determine if the list of nodes headed by N and linked by Next
2917 -- contains a package body for the package spec entity E, and if so
2918 -- return the package body. If not, then returns Empty.
2920 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2921 -- This procedure is called load the unit whose name is given by Nam.
2922 -- This unit is being loaded to see whether it contains an optional
2923 -- generic body. The returned value is the loaded unit, which is always
2924 -- a package body (only package bodies can contain other entities in the
2925 -- sense in which Has_Generic_Body is interested). We only attempt to
2926 -- load bodies if we are generating code. If we are in semantics check
2927 -- only mode, then it would be wrong to load bodies that are not
2928 -- required from a semantic point of view, so in this case we return
2929 -- Empty. The result is that the caller may incorrectly decide that a
2930 -- generic spec does not have a body when in fact it does, but the only
2931 -- harm in this is that some warnings on elaboration problems may be
2932 -- lost in semantic checks only mode, which is not big loss. We also
2933 -- return Empty if we go for a body and it is not there.
2935 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2936 -- PE is the entity for a package spec. This function locates the
2937 -- corresponding package body, returning Empty if none is found. The
2938 -- package body returned is fully parsed but may not yet be analyzed,
2939 -- so only syntactic fields should be referenced.
2941 ------------------
2942 -- Find_Body_In --
2943 ------------------
2945 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2946 Nod : Node_Id;
2948 begin
2949 Nod := N;
2950 while Present (Nod) loop
2952 -- If we found the package body we are looking for, return it
2954 if Nkind (Nod) = N_Package_Body
2955 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2956 then
2957 return Nod;
2959 -- If we found the stub for the body, go after the subunit,
2960 -- loading it if necessary.
2962 elsif Nkind (Nod) = N_Package_Body_Stub
2963 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2964 then
2965 if Present (Library_Unit (Nod)) then
2966 return Unit (Library_Unit (Nod));
2968 else
2969 return Load_Package_Body (Get_Unit_Name (Nod));
2970 end if;
2972 -- If neither package body nor stub, keep looking on chain
2974 else
2975 Next (Nod);
2976 end if;
2977 end loop;
2979 return Empty;
2980 end Find_Body_In;
2982 -----------------------
2983 -- Load_Package_Body --
2984 -----------------------
2986 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2987 U : Unit_Number_Type;
2989 begin
2990 if Operating_Mode /= Generate_Code then
2991 return Empty;
2992 else
2993 U :=
2994 Load_Unit
2995 (Load_Name => Nam,
2996 Required => False,
2997 Subunit => False,
2998 Error_Node => N);
3000 if U = No_Unit then
3001 return Empty;
3002 else
3003 return Unit (Cunit (U));
3004 end if;
3005 end if;
3006 end Load_Package_Body;
3008 -------------------------------
3009 -- Locate_Corresponding_Body --
3010 -------------------------------
3012 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
3013 Spec : constant Node_Id := Declaration_Node (PE);
3014 Decl : constant Node_Id := Parent (Spec);
3015 Scop : constant Entity_Id := Scope (PE);
3016 PBody : Node_Id;
3018 begin
3019 if Is_Library_Level_Entity (PE) then
3021 -- If package is a library unit that requires a body, we have no
3022 -- choice but to go after that body because it might contain an
3023 -- optional body for the original generic package.
3025 if Unit_Requires_Body (PE) then
3027 -- Load the body. Note that we are a little careful here to use
3028 -- Spec to get the unit number, rather than PE or Decl, since
3029 -- in the case where the package is itself a library level
3030 -- instantiation, Spec will properly reference the generic
3031 -- template, which is what we really want.
3033 return
3034 Load_Package_Body
3035 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
3037 -- But if the package is a library unit that does NOT require
3038 -- a body, then no body is permitted, so we are sure that there
3039 -- is no body for the original generic package.
3041 else
3042 return Empty;
3043 end if;
3045 -- Otherwise look and see if we are embedded in a further package
3047 elsif Is_Package_Or_Generic_Package (Scop) then
3049 -- If so, get the body of the enclosing package, and look in
3050 -- its package body for the package body we are looking for.
3052 PBody := Locate_Corresponding_Body (Scop);
3054 if No (PBody) then
3055 return Empty;
3056 else
3057 return Find_Body_In (PE, First (Declarations (PBody)));
3058 end if;
3060 -- If we are not embedded in a further package, then the body
3061 -- must be in the same declarative part as we are.
3063 else
3064 return Find_Body_In (PE, Next (Decl));
3065 end if;
3066 end Locate_Corresponding_Body;
3068 -- Start of processing for Has_Generic_Body
3070 begin
3071 if Present (Corresponding_Body (Decl)) then
3072 return True;
3074 elsif Unit_Requires_Body (Ent) then
3075 return True;
3077 -- Compilation units cannot have optional bodies
3079 elsif Is_Compilation_Unit (Ent) then
3080 return False;
3082 -- Otherwise look at what scope we are in
3084 else
3085 Scop := Scope (Ent);
3087 -- Case of entity is in other than a package spec, in this case
3088 -- the body, if present, must be in the same declarative part.
3090 if not Is_Package_Or_Generic_Package (Scop) then
3091 declare
3092 P : Node_Id;
3094 begin
3095 -- Declaration node may get us a spec, so if so, go to
3096 -- the parent declaration.
3098 P := Declaration_Node (Ent);
3099 while not Is_List_Member (P) loop
3100 P := Parent (P);
3101 end loop;
3103 return Present (Find_Body_In (Ent, Next (P)));
3104 end;
3106 -- If the entity is in a package spec, then we have to locate
3107 -- the corresponding package body, and look there.
3109 else
3110 declare
3111 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
3113 begin
3114 if No (PBody) then
3115 return False;
3116 else
3117 return
3118 Present
3119 (Find_Body_In (Ent, (First (Declarations (PBody)))));
3120 end if;
3121 end;
3122 end if;
3123 end if;
3124 end Has_Generic_Body;
3126 -----------------------
3127 -- Insert_Elab_Check --
3128 -----------------------
3130 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
3131 Nod : Node_Id;
3132 Loc : constant Source_Ptr := Sloc (N);
3134 Chk : Node_Id;
3135 -- The check (N_Raise_Program_Error) node to be inserted
3137 begin
3138 -- If expansion is disabled, do not generate any checks. Also
3139 -- skip checks if any subunits are missing because in either
3140 -- case we lack the full information that we need, and no object
3141 -- file will be created in any case.
3143 if not Expander_Active or else Subunits_Missing then
3144 return;
3145 end if;
3147 -- If we have a generic instantiation, where Instance_Spec is set,
3148 -- then this field points to a generic instance spec that has
3149 -- been inserted before the instantiation node itself, so that
3150 -- is where we want to insert a check.
3152 if Nkind (N) in N_Generic_Instantiation
3153 and then Present (Instance_Spec (N))
3154 then
3155 Nod := Instance_Spec (N);
3156 else
3157 Nod := N;
3158 end if;
3160 -- Build check node, possibly with condition
3162 Chk :=
3163 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration);
3165 if Present (C) then
3166 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C));
3167 end if;
3169 -- If we are inserting at the top level, insert in Aux_Decls
3171 if Nkind (Parent (Nod)) = N_Compilation_Unit then
3172 declare
3173 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
3175 begin
3176 if No (Declarations (ADN)) then
3177 Set_Declarations (ADN, New_List (Chk));
3178 else
3179 Append_To (Declarations (ADN), Chk);
3180 end if;
3182 Analyze (Chk);
3183 end;
3185 -- Otherwise just insert as an action on the node in question
3187 else
3188 Insert_Action (Nod, Chk);
3189 end if;
3190 end Insert_Elab_Check;
3192 -------------------------------
3193 -- Is_Finalization_Procedure --
3194 -------------------------------
3196 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is
3197 begin
3198 -- Check whether Id is a procedure with at least one parameter
3200 if Ekind (Id) = E_Procedure and then Present (First_Formal (Id)) then
3201 declare
3202 Typ : constant Entity_Id := Etype (First_Formal (Id));
3203 Deep_Fin : Entity_Id := Empty;
3204 Fin : Entity_Id := Empty;
3206 begin
3207 -- If the type of the first formal does not require finalization
3208 -- actions, then this is definitely not [Deep_]Finalize.
3210 if not Needs_Finalization (Typ) then
3211 return False;
3212 end if;
3214 -- At this point we have the following scenario:
3216 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3218 -- Recover the two possible versions of [Deep_]Finalize using the
3219 -- type of the first parameter and compare with the input.
3221 Deep_Fin := TSS (Typ, TSS_Deep_Finalize);
3223 if Is_Controlled (Typ) then
3224 Fin := Find_Prim_Op (Typ, Name_Finalize);
3225 end if;
3227 return (Present (Deep_Fin) and then Id = Deep_Fin)
3228 or else (Present (Fin) and then Id = Fin);
3229 end;
3230 end if;
3232 return False;
3233 end Is_Finalization_Procedure;
3235 ------------------
3236 -- Output_Calls --
3237 ------------------
3239 procedure Output_Calls
3240 (N : Node_Id;
3241 Check_Elab_Flag : Boolean)
3243 function Emit (Flag : Boolean) return Boolean;
3244 -- Determine whether to emit an error message based on the combination
3245 -- of flags Check_Elab_Flag and Flag.
3247 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
3248 -- An internal function, used to determine if a name, Nm, is either
3249 -- a non-internal name, or is an internal name that is printable
3250 -- by the error message circuits (i.e. it has a single upper
3251 -- case letter at the end).
3253 ----------
3254 -- Emit --
3255 ----------
3257 function Emit (Flag : Boolean) return Boolean is
3258 begin
3259 if Check_Elab_Flag then
3260 return Flag;
3261 else
3262 return True;
3263 end if;
3264 end Emit;
3266 -----------------------------
3267 -- Is_Printable_Error_Name --
3268 -----------------------------
3270 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
3271 begin
3272 if not Is_Internal_Name (Nm) then
3273 return True;
3275 elsif Name_Len = 1 then
3276 return False;
3278 else
3279 Name_Len := Name_Len - 1;
3280 return not Is_Internal_Name;
3281 end if;
3282 end Is_Printable_Error_Name;
3284 -- Local variables
3286 Ent : Entity_Id;
3288 -- Start of processing for Output_Calls
3290 begin
3291 for J in reverse 1 .. Elab_Call.Last loop
3292 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
3294 Ent := Elab_Call.Table (J).Ent;
3296 -- Dynamic elaboration model, warnings controlled by -gnatwl
3298 if Dynamic_Elaboration_Checks then
3299 if Emit (Elab_Warnings) then
3300 if Is_Generic_Unit (Ent) then
3301 Error_Msg_NE ("\\?l?& instantiated #", N, Ent);
3302 elsif Is_Init_Proc (Ent) then
3303 Error_Msg_N ("\\?l?initialization procedure called #", N);
3304 elsif Is_Printable_Error_Name (Chars (Ent)) then
3305 Error_Msg_NE ("\\?l?& called #", N, Ent);
3306 else
3307 Error_Msg_N ("\\?l?called #", N);
3308 end if;
3309 end if;
3311 -- Static elaboration model, info messages controlled by -gnatel
3313 else
3314 if Emit (Elab_Info_Messages) then
3315 if Is_Generic_Unit (Ent) then
3316 Error_Msg_NE ("\\?$?& instantiated #", N, Ent);
3317 elsif Is_Init_Proc (Ent) then
3318 Error_Msg_N ("\\?$?initialization procedure called #", N);
3319 elsif Is_Printable_Error_Name (Chars (Ent)) then
3320 Error_Msg_NE ("\\?$?& called #", N, Ent);
3321 else
3322 Error_Msg_N ("\\?$?called #", N);
3323 end if;
3324 end if;
3325 end if;
3326 end loop;
3327 end Output_Calls;
3329 ----------------------------
3330 -- Same_Elaboration_Scope --
3331 ----------------------------
3333 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
3334 S1 : Entity_Id;
3335 S2 : Entity_Id;
3337 begin
3338 -- Find elaboration scope for Scop1
3339 -- This is either a subprogram or a compilation unit.
3341 S1 := Scop1;
3342 while S1 /= Standard_Standard
3343 and then not Is_Compilation_Unit (S1)
3344 and then Ekind_In (S1, E_Package, E_Protected_Type, E_Block)
3345 loop
3346 S1 := Scope (S1);
3347 end loop;
3349 -- Find elaboration scope for Scop2
3351 S2 := Scop2;
3352 while S2 /= Standard_Standard
3353 and then not Is_Compilation_Unit (S2)
3354 and then Ekind_In (S2, E_Package, E_Protected_Type, E_Block)
3355 loop
3356 S2 := Scope (S2);
3357 end loop;
3359 return S1 = S2;
3360 end Same_Elaboration_Scope;
3362 -----------------
3363 -- Set_C_Scope --
3364 -----------------
3366 procedure Set_C_Scope is
3367 begin
3368 while not Is_Compilation_Unit (C_Scope) loop
3369 C_Scope := Scope (C_Scope);
3370 end loop;
3371 end Set_C_Scope;
3373 -----------------
3374 -- Spec_Entity --
3375 -----------------
3377 function Spec_Entity (E : Entity_Id) return Entity_Id is
3378 Decl : Node_Id;
3380 begin
3381 -- Check for case of body entity
3382 -- Why is the check for E_Void needed???
3384 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then
3385 Decl := E;
3387 loop
3388 Decl := Parent (Decl);
3389 exit when Nkind (Decl) in N_Proper_Body;
3390 end loop;
3392 return Corresponding_Spec (Decl);
3394 else
3395 return E;
3396 end if;
3397 end Spec_Entity;
3399 -------------------
3400 -- Supply_Bodies --
3401 -------------------
3403 procedure Supply_Bodies (N : Node_Id) is
3404 begin
3405 if Nkind (N) = N_Subprogram_Declaration then
3406 declare
3407 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3409 begin
3410 -- Internal subprograms will already have a generated body, so
3411 -- there is no need to provide a stub for them.
3413 if No (Corresponding_Body (N)) then
3414 declare
3415 Loc : constant Source_Ptr := Sloc (N);
3416 B : Node_Id;
3417 Formals : constant List_Id := Copy_Parameter_List (Ent);
3418 Nam : constant Entity_Id :=
3419 Make_Defining_Identifier (Loc, Chars (Ent));
3420 Spec : Node_Id;
3421 Stats : constant List_Id :=
3422 New_List
3423 (Make_Raise_Program_Error (Loc,
3424 Reason => PE_Access_Before_Elaboration));
3426 begin
3427 if Ekind (Ent) = E_Function then
3428 Spec :=
3429 Make_Function_Specification (Loc,
3430 Defining_Unit_Name => Nam,
3431 Parameter_Specifications => Formals,
3432 Result_Definition =>
3433 New_Copy_Tree
3434 (Result_Definition (Specification (N))));
3436 -- We cannot reliably make a return statement for this
3437 -- body, but none is needed because the call raises
3438 -- program error.
3440 Set_Return_Present (Ent);
3442 else
3443 Spec :=
3444 Make_Procedure_Specification (Loc,
3445 Defining_Unit_Name => Nam,
3446 Parameter_Specifications => Formals);
3447 end if;
3449 B := Make_Subprogram_Body (Loc,
3450 Specification => Spec,
3451 Declarations => New_List,
3452 Handled_Statement_Sequence =>
3453 Make_Handled_Sequence_Of_Statements (Loc, Stats));
3454 Insert_After (N, B);
3455 Analyze (B);
3456 end;
3457 end if;
3458 end;
3460 elsif Nkind (N) = N_Package_Declaration then
3461 declare
3462 Spec : constant Node_Id := Specification (N);
3463 begin
3464 Push_Scope (Defining_Unit_Name (Spec));
3465 Supply_Bodies (Visible_Declarations (Spec));
3466 Supply_Bodies (Private_Declarations (Spec));
3467 Pop_Scope;
3468 end;
3469 end if;
3470 end Supply_Bodies;
3472 procedure Supply_Bodies (L : List_Id) is
3473 Elmt : Node_Id;
3474 begin
3475 if Present (L) then
3476 Elmt := First (L);
3477 while Present (Elmt) loop
3478 Supply_Bodies (Elmt);
3479 Next (Elmt);
3480 end loop;
3481 end if;
3482 end Supply_Bodies;
3484 ------------
3485 -- Within --
3486 ------------
3488 function Within (E1, E2 : Entity_Id) return Boolean is
3489 Scop : Entity_Id;
3490 begin
3491 Scop := E1;
3492 loop
3493 if Scop = E2 then
3494 return True;
3495 elsif Scop = Standard_Standard then
3496 return False;
3497 else
3498 Scop := Scope (Scop);
3499 end if;
3500 end loop;
3501 end Within;
3503 --------------------------
3504 -- Within_Elaborate_All --
3505 --------------------------
3507 function Within_Elaborate_All
3508 (Unit : Unit_Number_Type;
3509 E : Entity_Id) return Boolean
3511 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
3512 pragma Pack (Unit_Number_Set);
3514 Seen : Unit_Number_Set := (others => False);
3515 -- Seen (X) is True after we have seen unit X in the walk. This is used
3516 -- to prevent processing the same unit more than once.
3518 Result : Boolean := False;
3520 procedure Helper (Unit : Unit_Number_Type);
3521 -- This helper procedure does all the work for Within_Elaborate_All. It
3522 -- walks the dependency graph, and sets Result to True if it finds an
3523 -- appropriate Elaborate_All.
3525 ------------
3526 -- Helper --
3527 ------------
3529 procedure Helper (Unit : Unit_Number_Type) is
3530 CU : constant Node_Id := Cunit (Unit);
3532 Item : Node_Id;
3533 Item2 : Node_Id;
3534 Elab_Id : Entity_Id;
3535 Par : Node_Id;
3537 begin
3538 if Seen (Unit) then
3539 return;
3540 else
3541 Seen (Unit) := True;
3542 end if;
3544 -- First, check for Elaborate_Alls on this unit
3546 Item := First (Context_Items (CU));
3547 while Present (Item) loop
3548 if Nkind (Item) = N_Pragma
3549 and then Pragma_Name (Item) = Name_Elaborate_All
3550 then
3551 -- Return if some previous error on the pragma itself. The
3552 -- pragma may be unanalyzed, because of a previous error, or
3553 -- if it is the context of a subunit, inherited by its parent.
3555 if Error_Posted (Item) or else not Analyzed (Item) then
3556 return;
3557 end if;
3559 Elab_Id :=
3560 Entity
3561 (Expression (First (Pragma_Argument_Associations (Item))));
3563 if E = Elab_Id then
3564 Result := True;
3565 return;
3566 end if;
3568 Par := Parent (Unit_Declaration_Node (Elab_Id));
3570 Item2 := First (Context_Items (Par));
3571 while Present (Item2) loop
3572 if Nkind (Item2) = N_With_Clause
3573 and then Entity (Name (Item2)) = E
3574 and then not Limited_Present (Item2)
3575 then
3576 Result := True;
3577 return;
3578 end if;
3580 Next (Item2);
3581 end loop;
3582 end if;
3584 Next (Item);
3585 end loop;
3587 -- Second, recurse on with's. We could do this as part of the above
3588 -- loop, but it's probably more efficient to have two loops, because
3589 -- the relevant Elaborate_All is likely to be on the initial unit. In
3590 -- other words, we're walking the with's breadth-first. This part is
3591 -- only necessary in the dynamic elaboration model.
3593 if Dynamic_Elaboration_Checks then
3594 Item := First (Context_Items (CU));
3595 while Present (Item) loop
3596 if Nkind (Item) = N_With_Clause
3597 and then not Limited_Present (Item)
3598 then
3599 -- Note: the following call to Get_Cunit_Unit_Number does a
3600 -- linear search, which could be slow, but it's OK because
3601 -- we're about to give a warning anyway. Also, there might
3602 -- be hundreds of units, but not millions. If it turns out
3603 -- to be a problem, we could store the Get_Cunit_Unit_Number
3604 -- in each N_Compilation_Unit node, but that would involve
3605 -- rearranging N_Compilation_Unit_Aux to make room.
3607 Helper (Get_Cunit_Unit_Number (Library_Unit (Item)));
3609 if Result then
3610 return;
3611 end if;
3612 end if;
3614 Next (Item);
3615 end loop;
3616 end if;
3617 end Helper;
3619 -- Start of processing for Within_Elaborate_All
3621 begin
3622 Helper (Unit);
3623 return Result;
3624 end Within_Elaborate_All;
3626 end Sem_Elab;