1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2017, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
36 with Lib
.Load
; use Lib
.Load
;
37 with Namet
; use Namet
;
38 with Nlists
; use Nlists
;
39 with Nmake
; use Nmake
;
41 with Output
; use Output
;
42 with Restrict
; use Restrict
;
43 with Rident
; use Rident
;
45 with Sem_Aux
; use Sem_Aux
;
46 with Sem_Cat
; use Sem_Cat
;
47 with Sem_Ch7
; use Sem_Ch7
;
48 with Sem_Ch8
; use Sem_Ch8
;
49 with Sem_Util
; use Sem_Util
;
50 with Sinfo
; use Sinfo
;
51 with Sinput
; use Sinput
;
52 with Snames
; use Snames
;
53 with Stand
; use Stand
;
55 with Tbuild
; use Tbuild
;
56 with Uintp
; use Uintp
;
57 with Uname
; use Uname
;
59 package body Sem_Elab
is
61 -- The following table records the recursive call chain for output in the
62 -- Output routine. Each entry records the call node and the entity of the
63 -- called routine. The number of entries in the table (i.e. the value of
64 -- Elab_Call.Last) indicates the current depth of recursion and is used to
65 -- identify the outer level.
67 type Elab_Call_Element
is record
72 package Elab_Call
is new Table
.Table
73 (Table_Component_Type
=> Elab_Call_Element
,
74 Table_Index_Type
=> Int
,
77 Table_Increment
=> 100,
78 Table_Name
=> "Elab_Call");
80 -- The following table records all calls that have been processed starting
81 -- from an outer level call. The table prevents both infinite recursion and
82 -- useless reanalysis of calls within the same context. The use of context
83 -- is important because it allows for proper checks in more complex code:
86 -- Call; -- requires a check
87 -- Call; -- does not need a check thanks to the table
89 -- Call; -- requires a check, different context
92 -- Call; -- requires a check, different context
94 type Visited_Element
is record
96 -- The entity of the subprogram being called
99 -- The context where the call to the subprogram occurs
102 package Elab_Visited
is new Table
.Table
103 (Table_Component_Type
=> Visited_Element
,
104 Table_Index_Type
=> Int
,
105 Table_Low_Bound
=> 1,
106 Table_Initial
=> 200,
107 Table_Increment
=> 100,
108 Table_Name
=> "Elab_Visited");
110 -- The following table records delayed calls which must be examined after
111 -- all generic bodies have been instantiated.
113 type Delay_Element
is record
115 -- The parameter N from the call to Check_Internal_Call. Note that this
116 -- node may get rewritten over the delay period by expansion in the call
117 -- case (but not in the instantiation case).
120 -- The parameter E from the call to Check_Internal_Call
122 Orig_Ent
: Entity_Id
;
123 -- The parameter Orig_Ent from the call to Check_Internal_Call
126 -- The current scope of the call. This is restored when we complete the
127 -- delayed call, so that we do this in the right scope.
129 Outer_Scope
: Entity_Id
;
130 -- Save scope of outer level call
132 From_Elab_Code
: Boolean;
133 -- Save indication of whether this call is from elaboration code
135 In_Task_Activation
: Boolean;
136 -- Save indication of whether this call is from a task body. Tasks are
137 -- activated at the "begin", which is after all local procedure bodies,
138 -- so calls to those procedures can't fail, even if they occur after the
141 From_SPARK_Code
: Boolean;
142 -- Save indication of whether this call is under SPARK_Mode => On
145 package Delay_Check
is new Table
.Table
146 (Table_Component_Type
=> Delay_Element
,
147 Table_Index_Type
=> Int
,
148 Table_Low_Bound
=> 1,
149 Table_Initial
=> 1000,
150 Table_Increment
=> 100,
151 Table_Name
=> "Delay_Check");
154 -- Top-level scope of current scope. Compute this only once at the outer
155 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
157 Outer_Level_Sloc
: Source_Ptr
;
158 -- Save Sloc value for outer level call node for comparisons of source
159 -- locations. A body is too late if it appears after the *outer* level
160 -- call, not the particular call that is being analyzed.
162 From_Elab_Code
: Boolean;
163 -- This flag shows whether the outer level call currently being examined
164 -- is or is not in elaboration code. We are only interested in calls to
165 -- routines in other units if this flag is True.
167 In_Task_Activation
: Boolean := False;
168 -- This flag indicates whether we are performing elaboration checks on task
169 -- bodies, at the point of activation. If true, we do not raise
170 -- Program_Error for calls to local procedures, because all local bodies
171 -- are known to be elaborated. However, we still need to trace such calls,
172 -- because a local procedure could call a procedure in another package,
173 -- so we might need an implicit Elaborate_All.
175 Delaying_Elab_Checks
: Boolean := True;
176 -- This is set True till the compilation is complete, including the
177 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
178 -- the delay table is used to make the delayed calls and this flag is reset
179 -- to False, so that the calls are processed.
181 -----------------------
182 -- Local Subprograms --
183 -----------------------
185 -- Note: Outer_Scope in all following specs represents the scope of
186 -- interest of the outer level call. If it is set to Standard_Standard,
187 -- then it means the outer level call was at elaboration level, and that
188 -- thus all calls are of interest. If it was set to some other scope,
189 -- then the original call was an inner call, and we are not interested
190 -- in calls that go outside this scope.
192 procedure Activate_Elaborate_All_Desirable
(N
: Node_Id
; U
: Entity_Id
);
193 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
194 -- for the WITH clause for unit U (which will always be present). A special
195 -- case is when N is a function or procedure instantiation, in which case
196 -- it is sufficient to set Elaborate_Desirable, since in this case there is
197 -- no possibility of transitive elaboration issues.
199 procedure Check_A_Call
202 Outer_Scope
: Entity_Id
;
203 Inter_Unit_Only
: Boolean;
204 Generate_Warnings
: Boolean := True;
205 In_Init_Proc
: Boolean := False);
206 -- This is the internal recursive routine that is called to check for
207 -- possible elaboration error. The argument N is a subprogram call or
208 -- generic instantiation, or 'Access attribute reference to be checked, and
209 -- E is the entity of the called subprogram, or instantiated generic unit,
210 -- or subprogram referenced by 'Access.
212 -- In SPARK mode, N can also be a variable reference, since in SPARK this
213 -- also triggers a requirement for Elaborate_All, and in this case E is the
214 -- entity being referenced.
216 -- Outer_Scope is the outer level scope for the original reference.
217 -- Inter_Unit_Only is set if the call is only to be checked in the
218 -- case where it is to another unit (and skipped if within a unit).
219 -- Generate_Warnings is set to False to suppress warning messages about
220 -- missing pragma Elaborate_All's. These messages are not wanted for
221 -- inner calls in the dynamic model. Note that an instance of the Access
222 -- attribute applied to a subprogram also generates a call to this
223 -- procedure (since the referenced subprogram may be called later
224 -- indirectly). Flag In_Init_Proc should be set whenever the current
225 -- context is a type init proc.
227 -- Note: this might better be called Check_A_Reference to recognize the
228 -- variable case for SPARK, but we prefer to retain the historical name
229 -- since in practice this is mostly about checking calls for the possible
230 -- occurrence of an access-before-elaboration exception.
232 procedure Check_Bad_Instantiation
(N
: Node_Id
);
233 -- N is a node for an instantiation (if called with any other node kind,
234 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
235 -- the special case of a generic instantiation of a generic spec in the
236 -- same declarative part as the instantiation where a body is present and
237 -- has not yet been seen. This is an obvious error, but needs to be checked
238 -- specially at the time of the instantiation, since it is a case where we
239 -- cannot insert the body anywhere. If this case is detected, warnings are
240 -- generated, and a raise of Program_Error is inserted. In addition any
241 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
242 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
243 -- flag as an indication that no attempt should be made to insert an
246 procedure Check_Internal_Call
249 Outer_Scope
: Entity_Id
;
250 Orig_Ent
: Entity_Id
);
251 -- N is a function call or procedure statement call node and E is the
252 -- entity of the called function, which is within the current compilation
253 -- unit (where subunits count as part of the parent). This call checks if
254 -- this call, or any call within any accessed body could cause an ABE, and
255 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
256 -- renamings, and points to the original name of the entity. This is used
257 -- for error messages. Outer_Scope is the outer level scope for the
260 procedure Check_Internal_Call_Continue
263 Outer_Scope
: Entity_Id
;
264 Orig_Ent
: Entity_Id
);
265 -- The processing for Check_Internal_Call is divided up into two phases,
266 -- and this represents the second phase. The second phase is delayed if
267 -- Delaying_Elab_Checks is set to True. In this delayed case, the first
268 -- phase makes an entry in the Delay_Check table, which is processed when
269 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
270 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
273 function Has_Generic_Body
(N
: Node_Id
) return Boolean;
274 -- N is a generic package instantiation node, and this routine determines
275 -- if this package spec does in fact have a generic body. If so, then
276 -- True is returned, otherwise False. Note that this is not at all the
277 -- same as checking if the unit requires a body, since it deals with
278 -- the case of optional bodies accurately (i.e. if a body is optional,
279 -- then it looks to see if a body is actually present). Note: this
280 -- function can only do a fully correct job if in generating code mode
281 -- where all bodies have to be present. If we are operating in semantics
282 -- check only mode, then in some cases of optional bodies, a result of
283 -- False may incorrectly be given. In practice this simply means that
284 -- some cases of warnings for incorrect order of elaboration will only
285 -- be given when generating code, which is not a big problem (and is
286 -- inevitable, given the optional body semantics of Ada).
288 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
);
289 -- Given code for an elaboration check (or unconditional raise if the check
290 -- is not needed), inserts the code in the appropriate place. N is the call
291 -- or instantiation node for which the check code is required. C is the
292 -- test whose failure triggers the raise.
294 function Is_Call_Of_Generic_Formal
(N
: Node_Id
) return Boolean;
295 -- Returns True if node N is a call to a generic formal subprogram
297 function Is_Finalization_Procedure
(Id
: Entity_Id
) return Boolean;
298 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
300 procedure Output_Calls
302 Check_Elab_Flag
: Boolean);
303 -- Outputs chain of calls stored in the Elab_Call table. The caller has
304 -- already generated the main warning message, so the warnings generated
305 -- are all continuation messages. The argument is the call node at which
306 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
307 -- enumerated only when flag Elab_Warning is set for the dynamic case or
308 -- when flag Elab_Info_Messages is set for the static case.
310 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean;
311 -- Given two scopes, determine whether they are the same scope from an
312 -- elaboration point of view, i.e. packages and blocks are ignored.
314 procedure Set_C_Scope
;
315 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
316 -- to be the enclosing compilation unit of this scope.
318 function Get_Referenced_Ent
(N
: Node_Id
) return Entity_Id
;
319 -- N is either a function or procedure call or an access attribute that
320 -- references a subprogram. This call retrieves the relevant entity. If
321 -- this is a call to a protected subprogram, the entity is a selected
322 -- component. The callable entity may be absent, in which case Empty is
323 -- returned. This happens with non-analyzed calls in nested generics.
325 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
326 -- entity, in which case, the value returned is simply this entity.
328 procedure Set_Elaboration_Constraint
332 -- The current unit U may depend semantically on some unit P that is not
333 -- in the current context. If there is an elaboration call that reaches P,
334 -- we need to indicate that P requires an Elaborate_All, but this is not
335 -- effective in U's ali file, if there is no with_clause for P. In this
336 -- case we add the Elaborate_All on the unit Q that directly or indirectly
337 -- makes P available. This can happen in two cases:
339 -- a) Q declares a subtype of a type declared in P, and the call is an
340 -- initialization call for an object of that subtype.
342 -- b) Q declares an object of some tagged type whose root type is
343 -- declared in P, and the initialization call uses object notation on
344 -- that object to reach a primitive operation or a classwide operation
347 -- If P appears in the context of U, the current processing is correct.
348 -- Otherwise we must identify these two cases to retrieve Q and place the
349 -- Elaborate_All_Desirable on it.
351 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
;
352 -- Given a compilation unit entity, if it is a spec entity, it is returned
353 -- unchanged. If it is a body entity, then the spec for the corresponding
356 procedure Supply_Bodies
(N
: Node_Id
);
357 -- Given a node, N, that is either a subprogram declaration or a package
358 -- declaration, this procedure supplies dummy bodies for the subprogram
359 -- or for all subprograms in the package. If the given node is not one of
360 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
361 -- contains a single Raise statement.
363 procedure Supply_Bodies
(L
: List_Id
);
364 -- Calls Supply_Bodies for all elements of the given list L
366 function Within
(E1
, E2
: Entity_Id
) return Boolean;
367 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
368 -- of its contained scopes, False otherwise.
370 function Within_Elaborate_All
371 (Unit
: Unit_Number_Type
;
372 E
: Entity_Id
) return Boolean;
373 -- Return True if we are within the scope of an Elaborate_All for E, or if
374 -- we are within the scope of an Elaborate_All for some other unit U, and U
375 -- with's E. This prevents spurious warnings when the called entity is
376 -- renamed within U, or in case of generic instances.
378 --------------------------------------
379 -- Activate_Elaborate_All_Desirable --
380 --------------------------------------
382 procedure Activate_Elaborate_All_Desirable
(N
: Node_Id
; U
: Entity_Id
) is
383 UN
: constant Unit_Number_Type
:= Get_Code_Unit
(N
);
384 CU
: constant Node_Id
:= Cunit
(UN
);
385 UE
: constant Entity_Id
:= Cunit_Entity
(UN
);
386 Unm
: constant Unit_Name_Type
:= Unit_Name
(UN
);
387 CI
: constant List_Id
:= Context_Items
(CU
);
391 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
);
392 -- This procedure is called when the elaborate indication must be
393 -- applied to a unit not in the context of the referencing unit. The
394 -- unit gets added to the context as an implicit with.
396 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean;
397 -- UEs is the spec entity of a unit. If the unit to be marked is
398 -- in the context item list of this unit spec, then the call returns
399 -- True and Itm is left set to point to the relevant N_With_Clause node.
401 procedure Set_Elab_Flag
(Itm
: Node_Id
);
402 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
404 -----------------------------
405 -- Add_To_Context_And_Mark --
406 -----------------------------
408 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
) is
409 CW
: constant Node_Id
:=
410 Make_With_Clause
(Sloc
(Itm
),
414 Set_Library_Unit
(CW
, Library_Unit
(Itm
));
415 Set_Implicit_With
(CW
, True);
417 -- Set elaborate all desirable on copy and then append the copy to
418 -- the list of body with's and we are done.
422 end Add_To_Context_And_Mark
;
428 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean is
429 UNs
: constant Unit_Number_Type
:= Get_Source_Unit
(UEs
);
430 CUs
: constant Node_Id
:= Cunit
(UNs
);
431 CIs
: constant List_Id
:= Context_Items
(CUs
);
435 while Present
(Itm
) loop
436 if Nkind
(Itm
) = N_With_Clause
then
438 Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
455 procedure Set_Elab_Flag
(Itm
: Node_Id
) is
457 if Nkind
(N
) in N_Subprogram_Instantiation
then
458 Set_Elaborate_Desirable
(Itm
);
460 Set_Elaborate_All_Desirable
(Itm
);
464 -- Start of processing for Activate_Elaborate_All_Desirable
467 -- Do not set binder indication if expansion is disabled, as when
468 -- compiling a generic unit.
470 if not Expander_Active
then
474 -- If an instance of a generic package contains a controlled object (so
475 -- we're calling Initialize at elaboration time), and the instance is in
476 -- a package body P that says "with P;", then we need to return without
477 -- adding "pragma Elaborate_All (P);" to P.
479 if U
= Main_Unit_Entity
then
484 while Present
(Itm
) loop
485 if Nkind
(Itm
) = N_With_Clause
then
486 Ent
:= Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
488 -- If we find it, then mark elaborate all desirable and return
499 -- If we fall through then the with clause is not present in the
500 -- current unit. One legitimate possibility is that the with clause
501 -- is present in the spec when we are a body.
503 if Is_Body_Name
(Unm
)
504 and then In_Withs_Of
(Spec_Entity
(UE
))
506 Add_To_Context_And_Mark
(Itm
);
510 -- Similarly, we may be in the spec or body of a child unit, where
511 -- the unit in question is with'ed by some ancestor of the child unit.
513 if Is_Child_Name
(Unm
) then
521 exit when Pkg
= Standard_Standard
;
523 if In_Withs_Of
(Pkg
) then
524 Add_To_Context_And_Mark
(Itm
);
531 -- Here if we do not find with clause on spec or body. We just ignore
532 -- this case; it means that the elaboration involves some other unit
533 -- than the unit being compiled, and will be caught elsewhere.
534 end Activate_Elaborate_All_Desirable
;
540 procedure Check_A_Call
543 Outer_Scope
: Entity_Id
;
544 Inter_Unit_Only
: Boolean;
545 Generate_Warnings
: Boolean := True;
546 In_Init_Proc
: Boolean := False)
548 Access_Case
: constant Boolean := Nkind
(N
) = N_Attribute_Reference
;
549 -- Indicates if we have Access attribute case
551 function Call_To_Instance_From_Outside
(Id
: Entity_Id
) return Boolean;
552 -- True if we're calling an instance of a generic subprogram, or a
553 -- subprogram in an instance of a generic package, and the call is
554 -- outside that instance.
556 procedure Elab_Warning
559 Ent
: Node_Or_Entity_Id
);
560 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
561 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
562 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
563 -- Msg_S is an info message (output if Elab_Info_Messages is set).
565 function Find_W_Scope
return Entity_Id
;
566 -- Find top-level scope for called entity (not following renamings
567 -- or derivations). This is where the Elaborate_All will go if it is
568 -- needed. We start with the called entity, except in the case of an
569 -- initialization procedure outside the current package, where the init
570 -- proc is in the root package, and we start from the entity of the name
573 -----------------------------------
574 -- Call_To_Instance_From_Outside --
575 -----------------------------------
577 function Call_To_Instance_From_Outside
(Id
: Entity_Id
) return Boolean is
578 Scop
: Entity_Id
:= Id
;
582 if Scop
= Standard_Standard
then
586 if Is_Generic_Instance
(Scop
) then
587 return not In_Open_Scopes
(Scop
);
590 Scop
:= Scope
(Scop
);
592 end Call_To_Instance_From_Outside
;
598 procedure Elab_Warning
601 Ent
: Node_Or_Entity_Id
)
604 -- Dynamic elaboration checks, real warning
606 if Dynamic_Elaboration_Checks
then
607 if not Access_Case
then
608 if Msg_D
/= "" and then Elab_Warnings
then
609 Error_Msg_NE
(Msg_D
, N
, Ent
);
612 -- In the access case emit first warning message as well,
613 -- otherwise list of calls will appear as errors.
615 elsif Elab_Warnings
then
616 Error_Msg_NE
(Msg_S
, N
, Ent
);
619 -- Static elaboration checks, info message
622 if Elab_Info_Messages
then
623 Error_Msg_NE
(Msg_S
, N
, Ent
);
632 function Find_W_Scope
return Entity_Id
is
633 Refed_Ent
: constant Entity_Id
:= Get_Referenced_Ent
(N
);
637 if Is_Init_Proc
(Refed_Ent
)
638 and then not In_Same_Extended_Unit
(N
, Refed_Ent
)
640 W_Scope
:= Scope
(Refed_Ent
);
645 -- Now loop through scopes to get to the enclosing compilation unit
647 while not Is_Compilation_Unit
(W_Scope
) loop
648 W_Scope
:= Scope
(W_Scope
);
656 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
657 -- Indicates if we have instantiation case
659 Loc
: constant Source_Ptr
:= Sloc
(N
);
661 Variable_Case
: constant Boolean :=
662 Nkind
(N
) in N_Has_Entity
663 and then Present
(Entity
(N
))
664 and then Ekind
(Entity
(N
)) = E_Variable
;
665 -- Indicates if we have variable reference case
667 W_Scope
: constant Entity_Id
:= Find_W_Scope
;
668 -- Top-level scope of directly called entity for subprogram. This
669 -- differs from E_Scope in the case where renamings or derivations
670 -- are involved, since it does not follow these links. W_Scope is
671 -- generally in a visible unit, and it is this scope that may require
672 -- an Elaborate_All. However, there are some cases (initialization
673 -- calls and calls involving object notation) where W_Scope might not
674 -- be in the context of the current unit, and there is an intermediate
675 -- package that is, in which case the Elaborate_All has to be placed
676 -- on this intermediate package. These special cases are handled in
677 -- Set_Elaboration_Constraint.
680 Callee_Unit_Internal
: Boolean;
681 Caller_Unit_Internal
: Boolean;
683 Inst_Callee
: Source_Ptr
;
684 Inst_Caller
: Source_Ptr
;
685 Unit_Callee
: Unit_Number_Type
;
686 Unit_Caller
: Unit_Number_Type
;
688 Body_Acts_As_Spec
: Boolean;
689 -- Set to true if call is to body acting as spec (no separate spec)
691 Cunit_SC
: Boolean := False;
692 -- Set to suppress dynamic elaboration checks where one of the
693 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
694 -- if a pragma Elaborate[_All] applies to that scope, in which case
695 -- warnings on the scope are also suppressed. For the internal case,
696 -- we ignore this flag.
699 -- Top-level scope of entity for called subprogram. This value includes
700 -- following renamings and derivations, so this scope can be in a
701 -- non-visible unit. This is the scope that is to be investigated to
702 -- see whether an elaboration check is required.
705 -- Flag set when the subprogram being invoked is the procedure generated
706 -- for pragma Default_Initial_Condition.
708 SPARK_Elab_Errors
: Boolean;
709 -- Flag set when an entity is called or a variable is read during SPARK
710 -- dynamic elaboration.
712 -- Start of processing for Check_A_Call
715 -- If the call is known to be within a local Suppress Elaboration
716 -- pragma, nothing to check. This can happen in task bodies. But
717 -- we ignore this for a call to a generic formal.
719 if Nkind
(N
) in N_Subprogram_Call
720 and then No_Elaboration_Check
(N
)
721 and then not Is_Call_Of_Generic_Formal
(N
)
725 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
726 -- check, we don't mind in this case if the call occurs before the body
727 -- since this is all generated code.
729 elsif Nkind
(Original_Node
(N
)) = N_Attribute_Reference
730 and then Attribute_Name
(Original_Node
(N
)) = Name_Valid_Scalars
734 -- Intrinsics such as instances of Unchecked_Deallocation do not have
735 -- any body, so elaboration checking is not needed, and would be wrong.
737 elsif Is_Intrinsic_Subprogram
(E
) then
740 -- Do not consider references to internal variables for SPARK semantics
742 elsif Variable_Case
and then not Comes_From_Source
(E
) then
746 -- Proceed with check
750 -- For a variable reference, just set Body_Acts_As_Spec to False
752 if Variable_Case
then
753 Body_Acts_As_Spec
:= False;
755 -- Additional checks for all other cases
758 -- Go to parent for derived subprogram, or to original subprogram in
759 -- the case of a renaming (Alias covers both these cases).
762 if (Suppress_Elaboration_Warnings
(Ent
)
763 or else Elaboration_Checks_Suppressed
(Ent
))
764 and then (Inst_Case
or else No
(Alias
(Ent
)))
769 -- Nothing to do for imported entities
771 if Is_Imported
(Ent
) then
775 exit when Inst_Case
or else No
(Alias
(Ent
));
779 Decl
:= Unit_Declaration_Node
(Ent
);
781 if Nkind
(Decl
) = N_Subprogram_Body
then
782 Body_Acts_As_Spec
:= True;
784 elsif Nkind_In
(Decl
, N_Subprogram_Declaration
,
785 N_Subprogram_Body_Stub
)
788 Body_Acts_As_Spec
:= False;
790 -- If we have none of an instantiation, subprogram body or subprogram
791 -- declaration, or in the SPARK case, a variable reference, then
792 -- it is not a case that we want to check. (One case is a call to a
793 -- generic formal subprogram, where we do not want the check in the
803 if Elaboration_Checks_Suppressed
(E_Scope
)
804 or else Suppress_Elaboration_Warnings
(E_Scope
)
809 -- Exit when we get to compilation unit, not counting subunits
811 exit when Is_Compilation_Unit
(E_Scope
)
812 and then (Is_Child_Unit
(E_Scope
)
813 or else Scope
(E_Scope
) = Standard_Standard
);
815 pragma Assert
(E_Scope
/= Standard_Standard
);
817 -- Move up a scope looking for compilation unit
819 E_Scope
:= Scope
(E_Scope
);
822 -- No checks needed for pure or preelaborated compilation units
824 if Is_Pure
(E_Scope
) or else Is_Preelaborated
(E_Scope
) then
828 -- If the generic entity is within a deeper instance than we are, then
829 -- either the instantiation to which we refer itself caused an ABE, in
830 -- which case that will be handled separately, or else we know that the
831 -- body we need appears as needed at the point of the instantiation.
832 -- However, this assumption is only valid if we are in static mode.
834 if not Dynamic_Elaboration_Checks
836 Instantiation_Depth
(Sloc
(Ent
)) > Instantiation_Depth
(Sloc
(N
))
841 -- Do not give a warning for a package with no body
843 if Ekind
(Ent
) = E_Generic_Package
and then not Has_Generic_Body
(N
) then
847 -- Case of entity is in same unit as call or instantiation. In the
848 -- instantiation case, W_Scope may be different from E_Scope; we want
849 -- the unit in which the instantiation occurs, since we're analyzing
850 -- based on the expansion.
852 if W_Scope
= C_Scope
then
853 if not Inter_Unit_Only
then
854 Check_Internal_Call
(N
, Ent
, Outer_Scope
, E
);
860 -- Case of entity is not in current unit (i.e. with'ed unit case)
862 -- We are only interested in such calls if the outer call was from
863 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
865 if not From_Elab_Code
and then not Dynamic_Elaboration_Checks
then
869 -- Nothing to do if some scope said that no checks were required
875 -- Nothing to do for a generic instance, because a call to an instance
876 -- cannot fail the elaboration check, because the body of the instance
877 -- is always elaborated immediately after the spec.
879 if Call_To_Instance_From_Outside
(Ent
) then
883 -- Nothing to do if subprogram with no separate spec. However, a call
884 -- to Deep_Initialize may result in a call to a user-defined Initialize
885 -- procedure, which imposes a body dependency. This happens only if the
886 -- type is controlled and the Initialize procedure is not inherited.
888 if Body_Acts_As_Spec
then
889 if Is_TSS
(Ent
, TSS_Deep_Initialize
) then
891 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Ent
));
895 if not Is_Controlled
(Typ
) then
898 Init
:= Find_Prim_Op
(Typ
, Name_Initialize
);
900 if Comes_From_Source
(Init
) then
913 -- Check cases of internal units
915 Callee_Unit_Internal
:= In_Internal_Unit
(E_Scope
);
917 -- Do not give a warning if the with'ed unit is internal and this is
918 -- the generic instantiation case (this saves a lot of hassle dealing
919 -- with the Text_IO special child units)
921 if Callee_Unit_Internal
and Inst_Case
then
925 if C_Scope
= Standard_Standard
then
926 Caller_Unit_Internal
:= False;
928 Caller_Unit_Internal
:= In_Internal_Unit
(C_Scope
);
931 -- Do not give a warning if the with'ed unit is internal and the caller
932 -- is not internal (since the binder always elaborates internal units
935 if Callee_Unit_Internal
and not Caller_Unit_Internal
then
939 -- For now, if debug flag -gnatdE is not set, do no checking for one
940 -- internal unit withing another. This fixes the problem with the sgi
941 -- build and storage errors. To be resolved later ???
943 if (Callee_Unit_Internal
and Caller_Unit_Internal
)
944 and not Debug_Flag_EE
949 if Is_TSS
(E
, TSS_Deep_Initialize
) then
953 -- If the call is in an instance, and the called entity is not
954 -- defined in the same instance, then the elaboration issue focuses
955 -- around the unit containing the template, it is this unit that
956 -- requires an Elaborate_All.
958 -- However, if we are doing dynamic elaboration, we need to chase the
959 -- call in the usual manner.
961 -- We also need to chase the call in the usual manner if it is a call
962 -- to a generic formal parameter, since that case was not handled as
963 -- part of the processing of the template.
965 Inst_Caller
:= Instantiation
(Get_Source_File_Index
(Sloc
(N
)));
966 Inst_Callee
:= Instantiation
(Get_Source_File_Index
(Sloc
(Ent
)));
968 if Inst_Caller
= No_Location
then
969 Unit_Caller
:= No_Unit
;
971 Unit_Caller
:= Get_Source_Unit
(N
);
974 if Inst_Callee
= No_Location
then
975 Unit_Callee
:= No_Unit
;
977 Unit_Callee
:= Get_Source_Unit
(Ent
);
980 if Unit_Caller
/= No_Unit
981 and then Unit_Callee
/= Unit_Caller
982 and then not Dynamic_Elaboration_Checks
983 and then not Is_Call_Of_Generic_Formal
(N
)
985 E_Scope
:= Spec_Entity
(Cunit_Entity
(Unit_Caller
));
987 -- If we don't get a spec entity, just ignore call. Not quite
988 -- clear why this check is necessary. ???
994 -- Otherwise step to enclosing compilation unit
996 while not Is_Compilation_Unit
(E_Scope
) loop
997 E_Scope
:= Scope
(E_Scope
);
1000 -- For the case where N is not an instance, and is not a call within
1001 -- instance to other than a generic formal, we recompute E_Scope
1002 -- for the error message, since we do NOT want to go to the unit
1003 -- that has the ultimate declaration in the case of renaming and
1004 -- derivation and we also want to go to the generic unit in the
1005 -- case of an instance, and no further.
1008 -- Loop to carefully follow renamings and derivations one step
1009 -- outside the current unit, but not further.
1011 if not (Inst_Case
or Variable_Case
)
1012 and then Present
(Alias
(Ent
))
1014 E_Scope
:= Alias
(Ent
);
1020 while not Is_Compilation_Unit
(E_Scope
) loop
1021 E_Scope
:= Scope
(E_Scope
);
1024 -- If E_Scope is the same as C_Scope, it means that there
1025 -- definitely was a local renaming or derivation, and we
1026 -- are not yet out of the current unit.
1028 exit when E_Scope
/= C_Scope
;
1032 -- If no alias, there could be a previous error, but not if we've
1033 -- already reached the outermost level (Standard).
1041 if Within_Elaborate_All
(Current_Sem_Unit
, E_Scope
) then
1045 -- Determine whether the Default_Initial_Condition procedure of some
1046 -- type is being invoked.
1048 Is_DIC
:= Ekind
(Ent
) = E_Procedure
and then Is_DIC_Procedure
(Ent
);
1050 -- Checks related to Default_Initial_Condition fall under the SPARK
1051 -- umbrella because this is a SPARK-specific annotation.
1053 SPARK_Elab_Errors
:=
1054 SPARK_Mode
= On
and (Is_DIC
or Dynamic_Elaboration_Checks
);
1056 -- Now check if an Elaborate_All (or dynamic check) is needed
1058 if (Elab_Info_Messages
or Elab_Warnings
or SPARK_Elab_Errors
)
1059 and then Generate_Warnings
1060 and then not Suppress_Elaboration_Warnings
(Ent
)
1061 and then not Elaboration_Checks_Suppressed
(Ent
)
1062 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1063 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1065 -- Instantiation case
1068 if Comes_From_Source
(Ent
) and then SPARK_Elab_Errors
then
1070 ("instantiation of & during elaboration in SPARK", N
, Ent
);
1073 ("instantiation of & may raise Program_Error?l?",
1074 "info: instantiation of & during elaboration?$?", Ent
);
1077 -- Indirect call case, info message only in static elaboration
1078 -- case, because the attribute reference itself cannot raise an
1079 -- exception. Note that SPARK does not permit indirect calls.
1081 elsif Access_Case
then
1082 Elab_Warning
("", "info: access to & during elaboration?$?", Ent
);
1084 -- Variable reference in SPARK mode
1086 elsif Variable_Case
then
1087 if Comes_From_Source
(Ent
) and then SPARK_Elab_Errors
then
1089 ("reference to & during elaboration in SPARK", N
, Ent
);
1092 -- Subprogram call case
1095 if Nkind
(Name
(N
)) in N_Has_Entity
1096 and then Is_Init_Proc
(Entity
(Name
(N
)))
1097 and then Comes_From_Source
(Ent
)
1100 ("implicit call to & may raise Program_Error?l?",
1101 "info: implicit call to & during elaboration?$?",
1104 elsif SPARK_Elab_Errors
then
1106 -- Emit a specialized error message when the elaboration of an
1107 -- object of a private type evaluates the expression of pragma
1108 -- Default_Initial_Condition. This prevents the internal name
1109 -- of the procedure from appearing in the error message.
1113 ("call to Default_Initial_Condition during elaboration in "
1117 ("call to & during elaboration in SPARK", N
, Ent
);
1122 ("call to & may raise Program_Error?l?",
1123 "info: call to & during elaboration?$?",
1128 Error_Msg_Qual_Level
:= Nat
'Last;
1130 -- Case of Elaborate_All not present and required, for SPARK this
1131 -- is an error, so give an error message.
1133 if SPARK_Elab_Errors
then
1134 Error_Msg_NE
-- CODEFIX
1135 ("\Elaborate_All pragma required for&", N
, W_Scope
);
1137 -- Otherwise we generate an implicit pragma. For a subprogram
1138 -- instantiation, Elaborate is good enough, since no transitive
1139 -- call is possible at elaboration time in this case.
1141 elsif Nkind
(N
) in N_Subprogram_Instantiation
then
1143 ("\missing pragma Elaborate for&?l?",
1144 "\implicit pragma Elaborate for& generated?$?",
1147 -- For all other cases, we need an implicit Elaborate_All
1151 ("\missing pragma Elaborate_All for&?l?",
1152 "\implicit pragma Elaborate_All for & generated?$?",
1156 Error_Msg_Qual_Level
:= 0;
1158 -- Take into account the flags related to elaboration warning
1159 -- messages when enumerating the various calls involved. This
1160 -- ensures the proper pairing of the main warning and the
1161 -- clarification messages generated by Output_Calls.
1163 Output_Calls
(N
, Check_Elab_Flag
=> True);
1165 -- Set flag to prevent further warnings for same unit unless in
1168 if not All_Errors_Mode
and not Dynamic_Elaboration_Checks
then
1169 Set_Suppress_Elaboration_Warnings
(W_Scope
);
1173 -- Check for runtime elaboration check required
1175 if Dynamic_Elaboration_Checks
then
1176 if not Elaboration_Checks_Suppressed
(Ent
)
1177 and then not Elaboration_Checks_Suppressed
(W_Scope
)
1178 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1179 and then not Cunit_SC
1181 -- Runtime elaboration check required. Generate check of the
1182 -- elaboration Boolean for the unit containing the entity.
1184 -- Note that for this case, we do check the real unit (the one
1185 -- from following renamings, since that is the issue).
1187 -- Could this possibly miss a useless but required PE???
1189 Insert_Elab_Check
(N
,
1190 Make_Attribute_Reference
(Loc
,
1191 Attribute_Name
=> Name_Elaborated
,
1193 New_Occurrence_Of
(Spec_Entity
(E_Scope
), Loc
)));
1195 -- Prevent duplicate elaboration checks on the same call,
1196 -- which can happen if the body enclosing the call appears
1197 -- itself in a call whose elaboration check is delayed.
1199 if Nkind
(N
) in N_Subprogram_Call
then
1200 Set_No_Elaboration_Check
(N
);
1204 -- Case of static elaboration model
1207 -- Do not do anything if elaboration checks suppressed. Note that
1208 -- we check Ent here, not E, since we want the real entity for the
1209 -- body to see if checks are suppressed for it, not the dummy
1210 -- entry for renamings or derivations.
1212 if Elaboration_Checks_Suppressed
(Ent
)
1213 or else Elaboration_Checks_Suppressed
(E_Scope
)
1214 or else Elaboration_Checks_Suppressed
(W_Scope
)
1218 -- Do not generate an Elaborate_All for finalization routines
1219 -- that perform partial clean up as part of initialization.
1221 elsif In_Init_Proc
and then Is_Finalization_Procedure
(Ent
) then
1224 -- Here we need to generate an implicit elaborate all
1227 -- Generate Elaborate_All warning unless suppressed
1229 if (Elab_Info_Messages
and Generate_Warnings
and not Inst_Case
)
1230 and then not Suppress_Elaboration_Warnings
(Ent
)
1231 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1232 and then not Suppress_Elaboration_Warnings
(W_Scope
)
1234 Error_Msg_Node_2
:= W_Scope
;
1236 ("info: call to& in elaboration code requires pragma "
1237 & "Elaborate_All on&?$?", N
, E
);
1240 -- Set indication for binder to generate Elaborate_All
1242 Set_Elaboration_Constraint
(N
, E
, W_Scope
);
1247 -----------------------------
1248 -- Check_Bad_Instantiation --
1249 -----------------------------
1251 procedure Check_Bad_Instantiation
(N
: Node_Id
) is
1255 -- Nothing to do if we do not have an instantiation (happens in some
1256 -- error cases, and also in the formal package declaration case)
1258 if Nkind
(N
) not in N_Generic_Instantiation
then
1261 -- Nothing to do if serious errors detected (avoid cascaded errors)
1263 elsif Serious_Errors_Detected
/= 0 then
1266 -- Nothing to do if not in full analysis mode
1268 elsif not Full_Analysis
then
1271 -- Nothing to do if inside a generic template
1273 elsif Inside_A_Generic
then
1276 -- Nothing to do if a library level instantiation
1278 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1281 -- Nothing to do if we are compiling a proper body for semantic
1282 -- purposes only. The generic body may be in another proper body.
1285 Nkind
(Parent
(Unit_Declaration_Node
(Main_Unit_Entity
))) = N_Subunit
1290 Ent
:= Get_Generic_Entity
(N
);
1292 -- The case we are interested in is when the generic spec is in the
1293 -- current declarative part
1295 if not Same_Elaboration_Scope
(Current_Scope
, Scope
(Ent
))
1296 or else not In_Same_Extended_Unit
(N
, Ent
)
1301 -- If the generic entity is within a deeper instance than we are, then
1302 -- either the instantiation to which we refer itself caused an ABE, in
1303 -- which case that will be handled separately. Otherwise, we know that
1304 -- the body we need appears as needed at the point of the instantiation.
1305 -- If they are both at the same level but not within the same instance
1306 -- then the body of the generic will be in the earlier instance.
1309 D1
: constant Nat
:= Instantiation_Depth
(Sloc
(Ent
));
1310 D2
: constant Nat
:= Instantiation_Depth
(Sloc
(N
));
1317 and then Is_Generic_Instance
(Scope
(Ent
))
1318 and then not In_Open_Scopes
(Scope
(Ent
))
1324 -- Now we can proceed, if the entity being called has a completion,
1325 -- then we are definitely OK, since we have already seen the body.
1327 if Has_Completion
(Ent
) then
1331 -- If there is no body, then nothing to do
1333 if not Has_Generic_Body
(N
) then
1337 -- Here we definitely have a bad instantiation
1339 Error_Msg_Warn
:= SPARK_Mode
/= On
;
1340 Error_Msg_NE
("cannot instantiate& before body seen<<", N
, Ent
);
1342 if Present
(Instance_Spec
(N
)) then
1343 Supply_Bodies
(Instance_Spec
(N
));
1346 Error_Msg_N
("\Program_Error [<<", N
);
1347 Insert_Elab_Check
(N
);
1348 Set_ABE_Is_Certain
(N
);
1349 end Check_Bad_Instantiation
;
1351 ---------------------
1352 -- Check_Elab_Call --
1353 ---------------------
1355 procedure Check_Elab_Call
1357 Outer_Scope
: Entity_Id
:= Empty
;
1358 In_Init_Proc
: Boolean := False)
1364 -- If the reference is not in the main unit, there is nothing to check.
1365 -- Elaboration call from units in the context of the main unit will lead
1366 -- to semantic dependencies when those units are compiled.
1368 if not In_Extended_Main_Code_Unit
(N
) then
1372 -- For an entry call, check relevant restriction
1374 if Nkind
(N
) = N_Entry_Call_Statement
1375 and then not In_Subprogram_Or_Concurrent_Unit
1377 Check_Restriction
(No_Entry_Calls_In_Elaboration_Code
, N
);
1379 -- Nothing to do if this is not an expected type of reference (happens
1380 -- in some error conditions, and in some cases where rewriting occurs).
1382 elsif Nkind
(N
) not in N_Subprogram_Call
1383 and then Nkind
(N
) /= N_Attribute_Reference
1384 and then (SPARK_Mode
/= On
1385 or else Nkind
(N
) not in N_Has_Entity
1386 or else No
(Entity
(N
))
1387 or else Ekind
(Entity
(N
)) /= E_Variable
)
1391 -- Nothing to do if this is a call already rewritten for elab checking.
1392 -- Such calls appear as the targets of If_Expressions.
1394 -- This check MUST be wrong, it catches far too much
1396 elsif Nkind
(Parent
(N
)) = N_If_Expression
then
1399 -- Nothing to do if inside a generic template
1401 elsif Inside_A_Generic
1402 and then No
(Enclosing_Generic_Body
(N
))
1406 -- Nothing to do if call is being pre-analyzed, as when within a
1407 -- pre/postcondition, a predicate, or an invariant.
1409 elsif In_Spec_Expression
then
1413 -- Nothing to do if this is a call to a postcondition, which is always
1414 -- within a subprogram body, even though the current scope may be the
1415 -- enclosing scope of the subprogram.
1417 if Nkind
(N
) = N_Procedure_Call_Statement
1418 and then Is_Entity_Name
(Name
(N
))
1419 and then Chars
(Entity
(Name
(N
))) = Name_uPostconditions
1424 -- Here we have a reference at elaboration time that must be checked
1426 if Debug_Flag_LL
then
1427 Write_Str
(" Check_Elab_Ref: ");
1429 if Nkind
(N
) = N_Attribute_Reference
then
1430 if not Is_Entity_Name
(Prefix
(N
)) then
1431 Write_Str
("<<not entity name>>");
1433 Write_Name
(Chars
(Entity
(Prefix
(N
))));
1436 Write_Str
("'Access");
1438 elsif No
(Name
(N
)) or else not Is_Entity_Name
(Name
(N
)) then
1439 Write_Str
("<<not entity name>> ");
1442 Write_Name
(Chars
(Entity
(Name
(N
))));
1445 Write_Str
(" reference at ");
1446 Write_Location
(Sloc
(N
));
1450 -- Climb up the tree to make sure we are not inside default expression
1451 -- of a parameter specification or a record component, since in both
1452 -- these cases, we will be doing the actual reference later, not now,
1453 -- and it is at the time of the actual reference (statically speaking)
1454 -- that we must do our static check, not at the time of its initial
1457 -- However, we have to check references within component definitions
1458 -- (e.g. a function call that determines an array component bound),
1459 -- so we terminate the loop in that case.
1462 while Present
(P
) loop
1463 if Nkind_In
(P
, N_Parameter_Specification
,
1464 N_Component_Declaration
)
1468 -- The reference occurs within the constraint of a component,
1469 -- so it must be checked.
1471 elsif Nkind
(P
) = N_Component_Definition
then
1479 -- Stuff that happens only at the outer level
1481 if No
(Outer_Scope
) then
1482 Elab_Visited
.Set_Last
(0);
1484 -- Nothing to do if current scope is Standard (this is a bit odd, but
1485 -- it happens in the case of generic instantiations).
1487 C_Scope
:= Current_Scope
;
1489 if C_Scope
= Standard_Standard
then
1493 -- First case, we are in elaboration code
1495 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1497 if From_Elab_Code
then
1499 -- Complain if ref that comes from source in preelaborated unit
1500 -- and we are not inside a subprogram (i.e. we are in elab code).
1502 if Comes_From_Source
(N
)
1503 and then In_Preelaborated_Unit
1504 and then not In_Inlined_Body
1505 and then Nkind
(N
) /= N_Attribute_Reference
1507 -- This is a warning in GNAT mode allowing such calls to be
1508 -- used in the predefined library with appropriate care.
1510 Error_Msg_Warn
:= GNAT_Mode
;
1512 ("<<non-static call not allowed in preelaborated unit", N
);
1516 -- Second case, we are inside a subprogram or concurrent unit, which
1517 -- means we are not in elaboration code.
1520 -- In this case, the issue is whether we are inside the
1521 -- declarative part of the unit in which we live, or inside its
1522 -- statements. In the latter case, there is no issue of ABE calls
1523 -- at this level (a call from outside to the unit in which we live
1524 -- might cause an ABE, but that will be detected when we analyze
1525 -- that outer level call, as it recurses into the called unit).
1527 -- Climb up the tree, doing this test, and also testing for being
1528 -- inside a default expression, which, as discussed above, is not
1529 -- checked at this stage.
1538 -- If we find a parentless subtree, it seems safe to assume
1539 -- that we are not in a declarative part and that no
1540 -- checking is required.
1546 if Is_List_Member
(P
) then
1547 L
:= List_Containing
(P
);
1554 exit when Nkind
(P
) = N_Subunit
;
1556 -- Filter out case of default expressions, where we do not
1557 -- do the check at this stage.
1559 if Nkind_In
(P
, N_Parameter_Specification
,
1560 N_Component_Declaration
)
1565 -- A protected body has no elaboration code and contains
1566 -- only other bodies.
1568 if Nkind
(P
) = N_Protected_Body
then
1571 elsif Nkind_In
(P
, N_Subprogram_Body
,
1576 if L
= Declarations
(P
) then
1579 -- We are not in elaboration code, but we are doing
1580 -- dynamic elaboration checks, in this case, we still
1581 -- need to do the reference, since the subprogram we are
1582 -- in could be called from another unit, also in dynamic
1583 -- elaboration check mode, at elaboration time.
1585 elsif Dynamic_Elaboration_Checks
then
1587 -- We provide a debug flag to disable this check. That
1588 -- way we have an easy work around for regressions
1589 -- that are caused by this new check. This debug flag
1590 -- can be removed later.
1592 if Debug_Flag_DD
then
1596 -- Do the check in this case
1600 elsif Nkind
(P
) = N_Task_Body
then
1602 -- The check is deferred until Check_Task_Activation
1603 -- but we need to capture local suppress pragmas
1604 -- that may inhibit checks on this call.
1606 Ent
:= Get_Referenced_Ent
(N
);
1611 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1612 or else Elaboration_Checks_Suppressed
(Ent
)
1613 or else Elaboration_Checks_Suppressed
(Scope
(Ent
))
1615 if Nkind
(N
) in N_Subprogram_Call
then
1616 Set_No_Elaboration_Check
(N
);
1622 -- Static model, call is not in elaboration code, we
1623 -- never need to worry, because in the static model the
1624 -- top-level caller always takes care of things.
1635 Ent
:= Get_Referenced_Ent
(N
);
1641 -- Determine whether a prior call to the same subprogram was already
1642 -- examined within the same context. If this is the case, then there is
1643 -- no need to proceed with the various warnings and checks because the
1644 -- work was already done for the previous call.
1647 Self
: constant Visited_Element
:=
1648 (Subp_Id
=> Ent
, Context
=> Parent
(N
));
1651 for Index
in 1 .. Elab_Visited
.Last
loop
1652 if Self
= Elab_Visited
.Table
(Index
) then
1658 -- See if we need to analyze this reference. We analyze it if either of
1659 -- the following conditions is met:
1661 -- It is an inner level call (since in this case it was triggered
1662 -- by an outer level call from elaboration code), but only if the
1663 -- call is within the scope of the original outer level call.
1665 -- It is an outer level reference from elaboration code, or a call to
1666 -- an entity is in the same elaboration scope.
1668 -- And in these cases, we will check both inter-unit calls and
1669 -- intra-unit (within a single unit) calls.
1671 C_Scope
:= Current_Scope
;
1673 -- If not outer level reference, then we follow it if it is within the
1674 -- original scope of the outer reference.
1676 if Present
(Outer_Scope
)
1677 and then Within
(Scope
(Ent
), Outer_Scope
)
1683 Outer_Scope
=> Outer_Scope
,
1684 Inter_Unit_Only
=> False,
1685 In_Init_Proc
=> In_Init_Proc
);
1687 -- Nothing to do if elaboration checks suppressed for this scope.
1688 -- However, an interesting exception, the fact that elaboration checks
1689 -- are suppressed within an instance (because we can trace the body when
1690 -- we process the template) does not extend to calls to generic formal
1693 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1694 and then not Is_Call_Of_Generic_Formal
(N
)
1698 elsif From_Elab_Code
then
1700 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1702 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1704 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1706 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1707 -- is set, then we will do the check, but only in the inter-unit case
1708 -- (this is to accommodate unguarded elaboration calls from other units
1709 -- in which this same mode is set). We don't want warnings in this case,
1710 -- it would generate warnings having nothing to do with elaboration.
1712 elsif Dynamic_Elaboration_Checks
then
1718 Inter_Unit_Only
=> True,
1719 Generate_Warnings
=> False);
1721 -- Otherwise nothing to do
1727 -- A call to an Init_Proc in elaboration code may bring additional
1728 -- dependencies, if some of the record components thereof have
1729 -- initializations that are function calls that come from source. We
1730 -- treat the current node as a call to each of these functions, to check
1731 -- their elaboration impact.
1733 if Is_Init_Proc
(Ent
) and then From_Elab_Code
then
1734 Process_Init_Proc
: declare
1735 Unit_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
1737 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
;
1738 -- Find subprogram calls within body of Init_Proc for Traverse
1739 -- instantiation below.
1741 procedure Traverse_Body
is new Traverse_Proc
(Check_Init_Call
);
1742 -- Traversal procedure to find all calls with body of Init_Proc
1744 ---------------------
1745 -- Check_Init_Call --
1746 ---------------------
1748 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
is
1752 if Nkind
(Nod
) in N_Subprogram_Call
1753 and then Is_Entity_Name
(Name
(Nod
))
1755 Func
:= Entity
(Name
(Nod
));
1757 if Comes_From_Source
(Func
) then
1759 (N
, Func
, Standard_Standard
, Inter_Unit_Only
=> True);
1767 end Check_Init_Call
;
1769 -- Start of processing for Process_Init_Proc
1772 if Nkind
(Unit_Decl
) = N_Subprogram_Body
then
1773 Traverse_Body
(Handled_Statement_Sequence
(Unit_Decl
));
1775 end Process_Init_Proc
;
1777 end Check_Elab_Call
;
1779 -----------------------
1780 -- Check_Elab_Assign --
1781 -----------------------
1783 procedure Check_Elab_Assign
(N
: Node_Id
) is
1787 Pkg_Spec
: Entity_Id
;
1788 Pkg_Body
: Entity_Id
;
1791 -- For record or array component, check prefix. If it is an access type,
1792 -- then there is nothing to do (we do not know what is being assigned),
1793 -- but otherwise this is an assignment to the prefix.
1795 if Nkind_In
(N
, N_Indexed_Component
,
1796 N_Selected_Component
,
1799 if not Is_Access_Type
(Etype
(Prefix
(N
))) then
1800 Check_Elab_Assign
(Prefix
(N
));
1806 -- For type conversion, check expression
1808 if Nkind
(N
) = N_Type_Conversion
then
1809 Check_Elab_Assign
(Expression
(N
));
1813 -- Nothing to do if this is not an entity reference otherwise get entity
1815 if Is_Entity_Name
(N
) then
1821 -- What we are looking for is a reference in the body of a package that
1822 -- modifies a variable declared in the visible part of the package spec.
1825 and then Comes_From_Source
(N
)
1826 and then not Suppress_Elaboration_Warnings
(Ent
)
1827 and then Ekind
(Ent
) = E_Variable
1828 and then not In_Private_Part
(Ent
)
1829 and then Is_Library_Level_Entity
(Ent
)
1831 Scop
:= Current_Scope
;
1833 if No
(Scop
) or else Scop
= Standard_Standard
then
1835 elsif Ekind
(Scop
) = E_Package
1836 and then Is_Compilation_Unit
(Scop
)
1840 Scop
:= Scope
(Scop
);
1844 -- Here Scop points to the containing library package
1847 Pkg_Body
:= Body_Entity
(Pkg_Spec
);
1849 -- All OK if the package has an Elaborate_Body pragma
1851 if Has_Pragma_Elaborate_Body
(Scop
) then
1855 -- OK if entity being modified is not in containing package spec
1857 if not In_Same_Source_Unit
(Scop
, Ent
) then
1861 -- All OK if entity appears in generic package or generic instance.
1862 -- We just get too messed up trying to give proper warnings in the
1863 -- presence of generics. Better no message than a junk one.
1865 Scop
:= Scope
(Ent
);
1866 while Present
(Scop
) and then Scop
/= Pkg_Spec
loop
1867 if Ekind
(Scop
) = E_Generic_Package
then
1869 elsif Ekind
(Scop
) = E_Package
1870 and then Is_Generic_Instance
(Scop
)
1875 Scop
:= Scope
(Scop
);
1878 -- All OK if in task, don't issue warnings there
1880 if In_Task_Activation
then
1884 -- OK if no package body
1886 if No
(Pkg_Body
) then
1890 -- OK if reference is not in package body
1892 if not In_Same_Source_Unit
(Pkg_Body
, N
) then
1896 -- OK if package body has no handled statement sequence
1899 HSS
: constant Node_Id
:=
1900 Handled_Statement_Sequence
(Declaration_Node
(Pkg_Body
));
1902 if No
(HSS
) or else not Comes_From_Source
(HSS
) then
1907 -- We definitely have a case of a modification of an entity in
1908 -- the package spec from the elaboration code of the package body.
1909 -- We may not give the warning (because there are some additional
1910 -- checks to avoid too many false positives), but it would be a good
1911 -- idea for the binder to try to keep the body elaboration close to
1912 -- the spec elaboration.
1914 Set_Elaborate_Body_Desirable
(Pkg_Spec
);
1916 -- All OK in gnat mode (we know what we are doing)
1922 -- All OK if all warnings suppressed
1924 if Warning_Mode
= Suppress
then
1928 -- All OK if elaboration checks suppressed for entity
1930 if Checks_May_Be_Suppressed
(Ent
)
1931 and then Is_Check_Suppressed
(Ent
, Elaboration_Check
)
1936 -- OK if the entity is initialized. Note that the No_Initialization
1937 -- flag usually means that the initialization has been rewritten into
1938 -- assignments, but that still counts for us.
1941 Decl
: constant Node_Id
:= Declaration_Node
(Ent
);
1943 if Nkind
(Decl
) = N_Object_Declaration
1944 and then (Present
(Expression
(Decl
))
1945 or else No_Initialization
(Decl
))
1951 -- Here is where we give the warning
1953 -- All OK if warnings suppressed on the entity
1955 if not Has_Warnings_Off
(Ent
) then
1956 Error_Msg_Sloc
:= Sloc
(Ent
);
1959 ("??& can be accessed by clients before this initialization",
1962 ("\??add Elaborate_Body to spec to ensure & is initialized",
1966 if not All_Errors_Mode
then
1967 Set_Suppress_Elaboration_Warnings
(Ent
);
1970 end Check_Elab_Assign
;
1972 ----------------------
1973 -- Check_Elab_Calls --
1974 ----------------------
1976 -- WARNING: This routine manages SPARK regions
1978 procedure Check_Elab_Calls
is
1979 Saved_SM
: SPARK_Mode_Type
;
1980 Saved_SMP
: Node_Id
;
1983 -- If expansion is disabled, do not generate any checks, unless we
1984 -- are in GNATprove mode, so that errors are issued in GNATprove for
1985 -- violations of static elaboration rules in SPARK code. Also skip
1986 -- checks if any subunits are missing because in either case we lack the
1987 -- full information that we need, and no object file will be created in
1990 if (not Expander_Active
and not GNATprove_Mode
)
1991 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
1992 or else Subunits_Missing
1997 -- Skip delayed calls if we had any errors
1999 if Serious_Errors_Detected
= 0 then
2000 Delaying_Elab_Checks
:= False;
2001 Expander_Mode_Save_And_Set
(True);
2003 for J
in Delay_Check
.First
.. Delay_Check
.Last
loop
2004 Push_Scope
(Delay_Check
.Table
(J
).Curscop
);
2005 From_Elab_Code
:= Delay_Check
.Table
(J
).From_Elab_Code
;
2006 In_Task_Activation
:= Delay_Check
.Table
(J
).In_Task_Activation
;
2008 Saved_SM
:= SPARK_Mode
;
2009 Saved_SMP
:= SPARK_Mode_Pragma
;
2011 -- Set appropriate value of SPARK_Mode
2013 if Delay_Check
.Table
(J
).From_SPARK_Code
then
2017 Check_Internal_Call_Continue
2018 (N
=> Delay_Check
.Table
(J
).N
,
2019 E
=> Delay_Check
.Table
(J
).E
,
2020 Outer_Scope
=> Delay_Check
.Table
(J
).Outer_Scope
,
2021 Orig_Ent
=> Delay_Check
.Table
(J
).Orig_Ent
);
2023 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
2027 -- Set Delaying_Elab_Checks back on for next main compilation
2029 Expander_Mode_Restore
;
2030 Delaying_Elab_Checks
:= True;
2032 end Check_Elab_Calls
;
2034 ------------------------------
2035 -- Check_Elab_Instantiation --
2036 ------------------------------
2038 procedure Check_Elab_Instantiation
2040 Outer_Scope
: Entity_Id
:= Empty
)
2045 -- Check for and deal with bad instantiation case. There is some
2046 -- duplicated code here, but we will worry about this later ???
2048 Check_Bad_Instantiation
(N
);
2050 if ABE_Is_Certain
(N
) then
2054 -- Nothing to do if we do not have an instantiation (happens in some
2055 -- error cases, and also in the formal package declaration case)
2057 if Nkind
(N
) not in N_Generic_Instantiation
then
2061 -- Nothing to do if inside a generic template
2063 if Inside_A_Generic
then
2067 -- Nothing to do if the instantiation is not in the main unit
2069 if not In_Extended_Main_Code_Unit
(N
) then
2073 Ent
:= Get_Generic_Entity
(N
);
2074 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
2076 -- See if we need to analyze this instantiation. We analyze it if
2077 -- either of the following conditions is met:
2079 -- It is an inner level instantiation (since in this case it was
2080 -- triggered by an outer level call from elaboration code), but
2081 -- only if the instantiation is within the scope of the original
2082 -- outer level call.
2084 -- It is an outer level instantiation from elaboration code, or the
2085 -- instantiated entity is in the same elaboration scope.
2087 -- And in these cases, we will check both the inter-unit case and
2088 -- the intra-unit (within a single unit) case.
2090 C_Scope
:= Current_Scope
;
2092 if Present
(Outer_Scope
) and then Within
(Scope
(Ent
), Outer_Scope
) then
2094 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
2096 elsif From_Elab_Code
then
2098 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
2100 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
2102 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
2104 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2105 -- set, then we will do the check, but only in the inter-unit case (this
2106 -- is to accommodate unguarded elaboration calls from other units in
2107 -- which this same mode is set). We inhibit warnings in this case, since
2108 -- this instantiation is not occurring in elaboration code.
2110 elsif Dynamic_Elaboration_Checks
then
2116 Inter_Unit_Only
=> True,
2117 Generate_Warnings
=> False);
2122 end Check_Elab_Instantiation
;
2124 -------------------------
2125 -- Check_Internal_Call --
2126 -------------------------
2128 procedure Check_Internal_Call
2131 Outer_Scope
: Entity_Id
;
2132 Orig_Ent
: Entity_Id
)
2134 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean;
2135 -- Determine whether call Call occurs within pragma Initial_Condition or
2136 -- pragma Check with check_kind set to Initial_Condition.
2138 ------------------------------
2139 -- Within_Initial_Condition --
2140 ------------------------------
2142 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean is
2148 -- Traverse the parent chain looking for an enclosing pragma
2151 while Present
(Par
) loop
2152 if Nkind
(Par
) = N_Pragma
then
2153 Nam
:= Pragma_Name
(Par
);
2155 -- Pragma Initial_Condition appears in its alternative from as
2156 -- Check (Initial_Condition, ...).
2158 if Nam
= Name_Check
then
2159 Args
:= Pragma_Argument_Associations
(Par
);
2161 -- Pragma Check should have at least two arguments
2163 pragma Assert
(Present
(Args
));
2166 Chars
(Expression
(First
(Args
))) = Name_Initial_Condition
;
2170 elsif Nam
= Name_Initial_Condition
then
2173 -- Since pragmas are never nested within other pragmas, stop
2180 -- Prevent the search from going too far
2182 elsif Is_Body_Or_Package_Declaration
(Par
) then
2186 Par
:= Parent
(Par
);
2188 -- If assertions are not enabled, the check pragma is rewritten
2189 -- as an if_statement in sem_prag, to generate various warnings
2190 -- on boolean expressions. Retrieve the original pragma.
2192 if Nkind
(Original_Node
(Par
)) = N_Pragma
then
2193 Par
:= Original_Node
(Par
);
2198 end Within_Initial_Condition
;
2202 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
2204 -- Start of processing for Check_Internal_Call
2207 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2208 -- node comes from source.
2210 if Nkind
(N
) = N_Attribute_Reference
2211 and then ((not Warn_On_Elab_Access
and then not Debug_Flag_Dot_O
)
2212 or else not Comes_From_Source
(N
))
2216 -- If not function or procedure call, instantiation, or 'Access, then
2217 -- ignore call (this happens in some error cases and rewriting cases).
2219 elsif not Nkind_In
(N
, N_Attribute_Reference
,
2221 N_Procedure_Call_Statement
)
2222 and then not Inst_Case
2226 -- Nothing to do if this is a call or instantiation that has already
2227 -- been found to be a sure ABE.
2229 elsif Nkind
(N
) /= N_Attribute_Reference
and then ABE_Is_Certain
(N
) then
2232 -- Nothing to do if errors already detected (avoid cascaded errors)
2234 elsif Serious_Errors_Detected
/= 0 then
2237 -- Nothing to do if not in full analysis mode
2239 elsif not Full_Analysis
then
2242 -- Nothing to do if analyzing in special spec-expression mode, since the
2243 -- call is not actually being made at this time.
2245 elsif In_Spec_Expression
then
2248 -- Nothing to do for call to intrinsic subprogram
2250 elsif Is_Intrinsic_Subprogram
(E
) then
2253 -- Nothing to do if call is within a generic unit
2255 elsif Inside_A_Generic
then
2258 -- Nothing to do when the call appears within pragma Initial_Condition.
2259 -- The pragma is part of the elaboration statements of a package body
2260 -- and may only call external subprograms or subprograms whose body is
2261 -- already available.
2263 elsif Within_Initial_Condition
(N
) then
2267 -- Delay this call if we are still delaying calls
2269 if Delaying_Elab_Checks
then
2273 Orig_Ent
=> Orig_Ent
,
2274 Curscop
=> Current_Scope
,
2275 Outer_Scope
=> Outer_Scope
,
2276 From_Elab_Code
=> From_Elab_Code
,
2277 In_Task_Activation
=> In_Task_Activation
,
2278 From_SPARK_Code
=> SPARK_Mode
= On
));
2281 -- Otherwise, call phase 2 continuation right now
2284 Check_Internal_Call_Continue
(N
, E
, Outer_Scope
, Orig_Ent
);
2286 end Check_Internal_Call
;
2288 ----------------------------------
2289 -- Check_Internal_Call_Continue --
2290 ----------------------------------
2292 procedure Check_Internal_Call_Continue
2295 Outer_Scope
: Entity_Id
;
2296 Orig_Ent
: Entity_Id
)
2298 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
;
2299 -- Function applied to each node as we traverse the body. Checks for
2300 -- call or entity reference that needs checking, and if so checks it.
2301 -- Always returns OK, so entire tree is traversed, except that as
2302 -- described below subprogram bodies are skipped for now.
2304 procedure Traverse
is new Atree
.Traverse_Proc
(Find_Elab_Reference
);
2305 -- Traverse procedure using above Find_Elab_Reference function
2307 -------------------------
2308 -- Find_Elab_Reference --
2309 -------------------------
2311 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
is
2315 -- If user has specified that there are no entry calls in elaboration
2316 -- code, do not trace past an accept statement, because the rendez-
2317 -- vous will happen after elaboration.
2319 if Nkind_In
(Original_Node
(N
), N_Accept_Statement
,
2321 and then Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
2325 -- If we have a function call, check it
2327 elsif Nkind
(N
) = N_Function_Call
then
2328 Check_Elab_Call
(N
, Outer_Scope
);
2331 -- If we have a procedure call, check the call, and also check
2332 -- arguments that are assignments (OUT or IN OUT mode formals).
2334 elsif Nkind
(N
) = N_Procedure_Call_Statement
then
2335 Check_Elab_Call
(N
, Outer_Scope
, In_Init_Proc
=> Is_Init_Proc
(E
));
2337 Actual
:= First_Actual
(N
);
2338 while Present
(Actual
) loop
2339 if Known_To_Be_Assigned
(Actual
) then
2340 Check_Elab_Assign
(Actual
);
2343 Next_Actual
(Actual
);
2348 -- If we have an access attribute for a subprogram, check it.
2349 -- Suppress this behavior under debug flag.
2351 elsif not Debug_Flag_Dot_UU
2352 and then Nkind
(N
) = N_Attribute_Reference
2353 and then Nam_In
(Attribute_Name
(N
), Name_Access
,
2354 Name_Unrestricted_Access
)
2355 and then Is_Entity_Name
(Prefix
(N
))
2356 and then Is_Subprogram
(Entity
(Prefix
(N
)))
2358 Check_Elab_Call
(N
, Outer_Scope
);
2361 -- In SPARK mode, if we have an entity reference to a variable, then
2362 -- check it. For now we consider any reference.
2364 elsif SPARK_Mode
= On
2365 and then Nkind
(N
) in N_Has_Entity
2366 and then Present
(Entity
(N
))
2367 and then Ekind
(Entity
(N
)) = E_Variable
2369 Check_Elab_Call
(N
, Outer_Scope
);
2372 -- If we have a generic instantiation, check it
2374 elsif Nkind
(N
) in N_Generic_Instantiation
then
2375 Check_Elab_Instantiation
(N
, Outer_Scope
);
2378 -- Skip subprogram bodies that come from source (wait for call to
2379 -- analyze these). The reason for the come from source test is to
2380 -- avoid catching task bodies.
2382 -- For task bodies, we should really avoid these too, waiting for the
2383 -- task activation, but that's too much trouble to catch for now, so
2384 -- we go in unconditionally. This is not so terrible, it means the
2385 -- error backtrace is not quite complete, and we are too eager to
2386 -- scan bodies of tasks that are unused, but this is hardly very
2389 elsif Nkind
(N
) = N_Subprogram_Body
2390 and then Comes_From_Source
(N
)
2394 elsif Nkind
(N
) = N_Assignment_Statement
2395 and then Comes_From_Source
(N
)
2397 Check_Elab_Assign
(Name
(N
));
2403 end Find_Elab_Reference
;
2405 Inst_Case
: constant Boolean := Is_Generic_Unit
(E
);
2406 Loc
: constant Source_Ptr
:= Sloc
(N
);
2411 -- Start of processing for Check_Internal_Call_Continue
2414 -- Save outer level call if at outer level
2416 if Elab_Call
.Last
= 0 then
2417 Outer_Level_Sloc
:= Loc
;
2420 -- If the call is to a function that renames a literal, no check needed
2422 if Ekind
(E
) = E_Enumeration_Literal
then
2426 -- Register the subprogram as examined within this particular context.
2427 -- This ensures that calls to the same subprogram but in different
2428 -- contexts receive warnings and checks of their own since the calls
2429 -- may be reached through different flow paths.
2431 Elab_Visited
.Append
((Subp_Id
=> E
, Context
=> Parent
(N
)));
2433 Sbody
:= Unit_Declaration_Node
(E
);
2435 if not Nkind_In
(Sbody
, N_Subprogram_Body
, N_Package_Body
) then
2436 Ebody
:= Corresponding_Body
(Sbody
);
2441 Sbody
:= Unit_Declaration_Node
(Ebody
);
2445 -- If the body appears after the outer level call or instantiation then
2446 -- we have an error case handled below.
2448 if Earlier_In_Extended_Unit
(Outer_Level_Sloc
, Sloc
(Sbody
))
2449 and then not In_Task_Activation
2453 -- If we have the instantiation case we are done, since we now know that
2454 -- the body of the generic appeared earlier.
2456 elsif Inst_Case
then
2459 -- Otherwise we have a call, so we trace through the called body to see
2460 -- if it has any problems.
2463 pragma Assert
(Nkind
(Sbody
) = N_Subprogram_Body
);
2465 Elab_Call
.Append
((Cloc
=> Loc
, Ent
=> E
));
2467 if Debug_Flag_LL
then
2468 Write_Str
("Elab_Call.Last = ");
2469 Write_Int
(Int
(Elab_Call
.Last
));
2470 Write_Str
(" Ent = ");
2471 Write_Name
(Chars
(E
));
2473 Write_Location
(Sloc
(N
));
2477 -- Now traverse declarations and statements of subprogram body. Note
2478 -- that we cannot simply Traverse (Sbody), since traverse does not
2479 -- normally visit subprogram bodies.
2484 Decl
:= First
(Declarations
(Sbody
));
2485 while Present
(Decl
) loop
2491 Traverse
(Handled_Statement_Sequence
(Sbody
));
2493 Elab_Call
.Decrement_Last
;
2497 -- Here is the case of calling a subprogram where the body has not yet
2498 -- been encountered. A warning message is needed, except if this is the
2499 -- case of appearing within an aspect specification that results in
2500 -- a check call, we do not really have such a situation, so no warning
2501 -- is needed (e.g. the case of a precondition, where the call appears
2502 -- textually before the body, but in actual fact is moved to the
2503 -- appropriate subprogram body and so does not need a check).
2512 -- Keep looking at parents if we are still in the subexpression
2514 if Nkind
(P
) in N_Subexpr
then
2517 -- Here P is the parent of the expression, check for special case
2520 O
:= Original_Node
(P
);
2522 -- Definitely not the special case if orig node is not a pragma
2524 exit when Nkind
(O
) /= N_Pragma
;
2526 -- Check we have an If statement or a null statement (happens
2527 -- when the If has been expanded to be True).
2529 exit when not Nkind_In
(P
, N_If_Statement
, N_Null_Statement
);
2531 -- Our special case will be indicated either by the pragma
2532 -- coming from an aspect ...
2534 if Present
(Corresponding_Aspect
(O
)) then
2537 -- Or, in the case of an initial condition, specifically by a
2538 -- Check pragma specifying an Initial_Condition check.
2540 elsif Pragma_Name
(O
) = Name_Check
2543 (Expression
(First
(Pragma_Argument_Associations
(O
)))) =
2544 Name_Initial_Condition
2548 -- For anything else, we have an error
2557 -- Not that special case, warning and dynamic check is required
2559 -- If we have nothing in the call stack, then this is at the outer
2560 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2563 if Elab_Call
.Last
= 0 then
2564 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2567 Insert_Check
: Boolean := True;
2568 -- This flag is set to True if an elaboration check should be
2572 if In_Task_Activation
then
2573 Insert_Check
:= False;
2575 elsif Inst_Case
then
2577 ("cannot instantiate& before body seen<<", N
, Orig_Ent
);
2579 elsif Nkind
(N
) = N_Attribute_Reference
then
2581 ("Access attribute of & before body seen<<", N
, Orig_Ent
);
2582 Error_Msg_N
("\possible Program_Error on later references<", N
);
2583 Insert_Check
:= False;
2585 elsif Nkind
(Unit_Declaration_Node
(Orig_Ent
)) /=
2586 N_Subprogram_Renaming_Declaration
2589 ("cannot call& before body seen<<", N
, Orig_Ent
);
2591 elsif not Is_Generic_Actual_Subprogram
(Orig_Ent
) then
2592 Insert_Check
:= False;
2595 if Insert_Check
then
2596 Error_Msg_N
("\Program_Error [<<", N
);
2597 Insert_Elab_Check
(N
);
2601 -- Call is not at outer level
2604 -- Do not generate elaboration checks in GNATprove mode because the
2605 -- elaboration counter and the check are both forms of expansion.
2607 if GNATprove_Mode
then
2610 -- Generate an elaboration check
2612 elsif not Elaboration_Checks_Suppressed
(E
) then
2613 Set_Elaboration_Entity_Required
(E
);
2615 -- Create a declaration of the elaboration entity, and insert it
2616 -- prior to the subprogram or the generic unit, within the same
2617 -- scope. Since the subprogram may be overloaded, create a unique
2620 if No
(Elaboration_Entity
(E
)) then
2622 Loce
: constant Source_Ptr
:= Sloc
(E
);
2623 Ent
: constant Entity_Id
:=
2624 Make_Defining_Identifier
(Loc
,
2625 New_External_Name
(Chars
(E
), 'E', -1));
2628 Set_Elaboration_Entity
(E
, Ent
);
2629 Push_Scope
(Scope
(E
));
2631 Insert_Action
(Declaration_Node
(E
),
2632 Make_Object_Declaration
(Loce
,
2633 Defining_Identifier
=> Ent
,
2634 Object_Definition
=>
2635 New_Occurrence_Of
(Standard_Short_Integer
, Loce
),
2637 Make_Integer_Literal
(Loc
, Uint_0
)));
2639 -- Set elaboration flag at the point of the body
2641 Set_Elaboration_Flag
(Sbody
, E
);
2643 -- Kill current value indication. This is necessary because
2644 -- the tests of this flag are inserted out of sequence and
2645 -- must not pick up bogus indications of the wrong constant
2646 -- value. Also, this is never a true constant, since one way
2647 -- or another, it gets reset.
2649 Set_Current_Value
(Ent
, Empty
);
2650 Set_Last_Assignment
(Ent
, Empty
);
2651 Set_Is_True_Constant
(Ent
, False);
2658 -- raise Program_Error with "access before elaboration";
2661 Insert_Elab_Check
(N
,
2662 Make_Attribute_Reference
(Loc
,
2663 Attribute_Name
=> Name_Elaborated
,
2664 Prefix
=> New_Occurrence_Of
(E
, Loc
)));
2667 -- Generate the warning
2669 if not Suppress_Elaboration_Warnings
(E
)
2670 and then not Elaboration_Checks_Suppressed
(E
)
2672 -- Suppress this warning if we have a function call that occurred
2673 -- within an assertion expression, since we can get false warnings
2674 -- in this case, due to the out of order handling in this case.
2677 (Nkind
(Original_Node
(N
)) /= N_Function_Call
2678 or else not In_Assertion_Expression_Pragma
(Original_Node
(N
)))
2680 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2684 ("instantiation of& may occur before body is seen<l<",
2687 -- A rather specific check. For Finalize/Adjust/Initialize, if
2688 -- the type has Warnings_Off set, suppress the warning.
2690 if Nam_In
(Chars
(E
), Name_Adjust
,
2693 and then Present
(First_Formal
(E
))
2696 T
: constant Entity_Id
:= Etype
(First_Formal
(E
));
2698 if Is_Controlled
(T
) then
2700 or else (Ekind
(T
) = E_Private_Type
2701 and then Warnings_Off
(Full_View
(T
)))
2709 -- Go ahead and give warning if not this special case
2712 ("call to& may occur before body is seen<l<", N
, Orig_Ent
);
2715 Error_Msg_N
("\Program_Error ]<l<", N
);
2717 -- There is no need to query the elaboration warning message flags
2718 -- because the main message is an error, not a warning, therefore
2719 -- all the clarification messages produces by Output_Calls must be
2720 -- emitted unconditionally.
2724 Output_Calls
(N
, Check_Elab_Flag
=> False);
2727 end Check_Internal_Call_Continue
;
2729 ---------------------------
2730 -- Check_Task_Activation --
2731 ---------------------------
2733 procedure Check_Task_Activation
(N
: Node_Id
) is
2734 Loc
: constant Source_Ptr
:= Sloc
(N
);
2735 Inter_Procs
: constant Elist_Id
:= New_Elmt_List
;
2736 Intra_Procs
: constant Elist_Id
:= New_Elmt_List
;
2739 Task_Scope
: Entity_Id
;
2740 Cunit_SC
: Boolean := False;
2743 Enclosing
: Entity_Id
;
2745 procedure Add_Task_Proc
(Typ
: Entity_Id
);
2746 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2747 -- For record types, this procedure recurses over component types.
2749 procedure Collect_Tasks
(Decls
: List_Id
);
2750 -- Collect the types of the tasks that are to be activated in the given
2751 -- list of declarations, in order to perform elaboration checks on the
2752 -- corresponding task procedures that are called implicitly here.
2754 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
;
2755 -- find enclosing compilation unit of Entity, ignoring subunits, or
2756 -- else enclosing subprogram. If E is not a package, there is no need
2757 -- for inter-unit elaboration checks.
2763 procedure Add_Task_Proc
(Typ
: Entity_Id
) is
2765 Proc
: Entity_Id
:= Empty
;
2768 if Is_Task_Type
(Typ
) then
2769 Proc
:= Get_Task_Body_Procedure
(Typ
);
2771 elsif Is_Array_Type
(Typ
)
2772 and then Has_Task
(Base_Type
(Typ
))
2774 Add_Task_Proc
(Component_Type
(Typ
));
2776 elsif Is_Record_Type
(Typ
)
2777 and then Has_Task
(Base_Type
(Typ
))
2779 Comp
:= First_Component
(Typ
);
2780 while Present
(Comp
) loop
2781 Add_Task_Proc
(Etype
(Comp
));
2782 Comp
:= Next_Component
(Comp
);
2786 -- If the task type is another unit, we will perform the usual
2787 -- elaboration check on its enclosing unit. If the type is in the
2788 -- same unit, we can trace the task body as for an internal call,
2789 -- but we only need to examine other external calls, because at
2790 -- the point the task is activated, internal subprogram bodies
2791 -- will have been elaborated already. We keep separate lists for
2792 -- each kind of task.
2794 -- Skip this test if errors have occurred, since in this case
2795 -- we can get false indications.
2797 if Serious_Errors_Detected
/= 0 then
2801 if Present
(Proc
) then
2802 if Outer_Unit
(Scope
(Proc
)) = Enclosing
then
2804 if No
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2806 (not Is_Generic_Instance
(Scope
(Proc
))
2807 or else Scope
(Proc
) = Scope
(Defining_Identifier
(Decl
)))
2809 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2811 ("task will be activated before elaboration of its body<<",
2813 Error_Msg_N
("\Program_Error [<<", Decl
);
2816 (Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2818 Append_Elmt
(Proc
, Intra_Procs
);
2822 -- No need for multiple entries of the same type
2824 Elmt
:= First_Elmt
(Inter_Procs
);
2825 while Present
(Elmt
) loop
2826 if Node
(Elmt
) = Proc
then
2833 Append_Elmt
(Proc
, Inter_Procs
);
2842 procedure Collect_Tasks
(Decls
: List_Id
) is
2844 if Present
(Decls
) then
2845 Decl
:= First
(Decls
);
2846 while Present
(Decl
) loop
2847 if Nkind
(Decl
) = N_Object_Declaration
2848 and then Has_Task
(Etype
(Defining_Identifier
(Decl
)))
2850 Add_Task_Proc
(Etype
(Defining_Identifier
(Decl
)));
2862 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
is
2867 while Present
(Outer
) loop
2868 if Elaboration_Checks_Suppressed
(Outer
) then
2872 exit when Is_Child_Unit
(Outer
)
2873 or else Scope
(Outer
) = Standard_Standard
2874 or else Ekind
(Outer
) /= E_Package
;
2875 Outer
:= Scope
(Outer
);
2881 -- Start of processing for Check_Task_Activation
2884 Enclosing
:= Outer_Unit
(Current_Scope
);
2886 -- Find all tasks declared in the current unit
2888 if Nkind
(N
) = N_Package_Body
then
2889 P
:= Unit_Declaration_Node
(Corresponding_Spec
(N
));
2891 Collect_Tasks
(Declarations
(N
));
2892 Collect_Tasks
(Visible_Declarations
(Specification
(P
)));
2893 Collect_Tasks
(Private_Declarations
(Specification
(P
)));
2895 elsif Nkind
(N
) = N_Package_Declaration
then
2896 Collect_Tasks
(Visible_Declarations
(Specification
(N
)));
2897 Collect_Tasks
(Private_Declarations
(Specification
(N
)));
2900 Collect_Tasks
(Declarations
(N
));
2903 -- We only perform detailed checks in all tasks that are library level
2904 -- entities. If the master is a subprogram or task, activation will
2905 -- depend on the activation of the master itself.
2907 -- Should dynamic checks be added in the more general case???
2909 if Ekind
(Enclosing
) /= E_Package
then
2913 -- For task types defined in other units, we want the unit containing
2914 -- the task body to be elaborated before the current one.
2916 Elmt
:= First_Elmt
(Inter_Procs
);
2917 while Present
(Elmt
) loop
2919 Task_Scope
:= Outer_Unit
(Scope
(Ent
));
2921 if not Is_Compilation_Unit
(Task_Scope
) then
2924 elsif Suppress_Elaboration_Warnings
(Task_Scope
)
2925 or else Elaboration_Checks_Suppressed
(Task_Scope
)
2929 elsif Dynamic_Elaboration_Checks
then
2930 if not Elaboration_Checks_Suppressed
(Ent
)
2931 and then not Cunit_SC
2932 and then not Restriction_Active
2933 (No_Entry_Calls_In_Elaboration_Code
)
2935 -- Runtime elaboration check required. Generate check of the
2936 -- elaboration counter for the unit containing the entity.
2938 Insert_Elab_Check
(N
,
2939 Make_Attribute_Reference
(Loc
,
2941 New_Occurrence_Of
(Spec_Entity
(Task_Scope
), Loc
),
2942 Attribute_Name
=> Name_Elaborated
));
2946 -- Force the binder to elaborate other unit first
2948 if Elab_Info_Messages
2949 and then not Suppress_Elaboration_Warnings
(Ent
)
2950 and then not Elaboration_Checks_Suppressed
(Ent
)
2951 and then not Suppress_Elaboration_Warnings
(Task_Scope
)
2952 and then not Elaboration_Checks_Suppressed
(Task_Scope
)
2954 Error_Msg_Node_2
:= Task_Scope
;
2956 ("info: activation of an instance of task type & requires "
2957 & "pragma Elaborate_All on &?$?", N
, Ent
);
2960 Activate_Elaborate_All_Desirable
(N
, Task_Scope
);
2961 Set_Suppress_Elaboration_Warnings
(Task_Scope
);
2967 -- For tasks declared in the current unit, trace other calls within the
2968 -- task procedure bodies, which are available.
2970 if not Debug_Flag_Dot_Y
then
2971 In_Task_Activation
:= True;
2973 Elmt
:= First_Elmt
(Intra_Procs
);
2974 while Present
(Elmt
) loop
2976 Check_Internal_Call_Continue
(N
, Ent
, Enclosing
, Ent
);
2980 In_Task_Activation
:= False;
2982 end Check_Task_Activation
;
2984 -------------------------------
2985 -- Is_Call_Of_Generic_Formal --
2986 -------------------------------
2988 function Is_Call_Of_Generic_Formal
(N
: Node_Id
) return Boolean is
2990 return Nkind_In
(N
, N_Function_Call
, N_Procedure_Call_Statement
)
2992 -- Always return False if debug flag -gnatd.G is set
2994 and then not Debug_Flag_Dot_GG
2996 -- For now, we detect this by looking for the strange identifier
2997 -- node, whose Chars reflect the name of the generic formal, but
2998 -- the Chars of the Entity references the generic actual.
3000 and then Nkind
(Name
(N
)) = N_Identifier
3001 and then Chars
(Name
(N
)) /= Chars
(Entity
(Name
(N
)));
3002 end Is_Call_Of_Generic_Formal
;
3004 --------------------------------
3005 -- Set_Elaboration_Constraint --
3006 --------------------------------
3008 procedure Set_Elaboration_Constraint
3013 Elab_Unit
: Entity_Id
;
3015 -- Check whether this is a call to an Initialize subprogram for a
3016 -- controlled type. Note that Call can also be a 'Access attribute
3017 -- reference, which now generates an elaboration check.
3019 Init_Call
: constant Boolean :=
3020 Nkind
(Call
) = N_Procedure_Call_Statement
3021 and then Chars
(Subp
) = Name_Initialize
3022 and then Comes_From_Source
(Subp
)
3023 and then Present
(Parameter_Associations
(Call
))
3024 and then Is_Controlled
(Etype
(First_Actual
(Call
)));
3027 -- If the unit is mentioned in a with_clause of the current unit, it is
3028 -- visible, and we can set the elaboration flag.
3030 if Is_Immediately_Visible
(Scop
)
3031 or else (Is_Child_Unit
(Scop
) and then Is_Visible_Lib_Unit
(Scop
))
3033 Activate_Elaborate_All_Desirable
(Call
, Scop
);
3034 Set_Suppress_Elaboration_Warnings
(Scop
);
3038 -- If this is not an initialization call or a call using object notation
3039 -- we know that the unit of the called entity is in the context, and we
3040 -- can set the flag as well. The unit need not be visible if the call
3041 -- occurs within an instantiation.
3043 if Is_Init_Proc
(Subp
)
3045 or else Nkind
(Original_Node
(Call
)) = N_Selected_Component
3047 null; -- detailed processing follows.
3050 Activate_Elaborate_All_Desirable
(Call
, Scop
);
3051 Set_Suppress_Elaboration_Warnings
(Scop
);
3055 -- If the unit is not in the context, there must be an intermediate unit
3056 -- that is, on which we need to place to elaboration flag. This happens
3057 -- with init proc calls.
3059 if Is_Init_Proc
(Subp
) or else Init_Call
then
3061 -- The initialization call is on an object whose type is not declared
3062 -- in the same scope as the subprogram. The type of the object must
3063 -- be a subtype of the type of operation. This object is the first
3064 -- actual in the call.
3067 Typ
: constant Entity_Id
:=
3068 Etype
(First
(Parameter_Associations
(Call
)));
3070 Elab_Unit
:= Scope
(Typ
);
3071 while (Present
(Elab_Unit
))
3072 and then not Is_Compilation_Unit
(Elab_Unit
)
3074 Elab_Unit
:= Scope
(Elab_Unit
);
3078 -- If original node uses selected component notation, the prefix is
3079 -- visible and determines the scope that must be elaborated. After
3080 -- rewriting, the prefix is the first actual in the call.
3082 elsif Nkind
(Original_Node
(Call
)) = N_Selected_Component
then
3083 Elab_Unit
:= Scope
(Etype
(First
(Parameter_Associations
(Call
))));
3085 -- Not one of special cases above
3088 -- Using previously computed scope. If the elaboration check is
3089 -- done after analysis, the scope is not visible any longer, but
3090 -- must still be in the context.
3095 Activate_Elaborate_All_Desirable
(Call
, Elab_Unit
);
3096 Set_Suppress_Elaboration_Warnings
(Elab_Unit
);
3097 end Set_Elaboration_Constraint
;
3099 ------------------------
3100 -- Get_Referenced_Ent --
3101 ------------------------
3103 function Get_Referenced_Ent
(N
: Node_Id
) return Entity_Id
is
3107 if Nkind
(N
) in N_Has_Entity
3108 and then Present
(Entity
(N
))
3109 and then Ekind
(Entity
(N
)) = E_Variable
3114 if Nkind
(N
) = N_Attribute_Reference
then
3122 elsif Nkind
(Nam
) = N_Selected_Component
then
3123 return Entity
(Selector_Name
(Nam
));
3124 elsif not Is_Entity_Name
(Nam
) then
3127 return Entity
(Nam
);
3129 end Get_Referenced_Ent
;
3131 ----------------------
3132 -- Has_Generic_Body --
3133 ----------------------
3135 function Has_Generic_Body
(N
: Node_Id
) return Boolean is
3136 Ent
: constant Entity_Id
:= Get_Generic_Entity
(N
);
3137 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
3140 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
;
3141 -- Determine if the list of nodes headed by N and linked by Next
3142 -- contains a package body for the package spec entity E, and if so
3143 -- return the package body. If not, then returns Empty.
3145 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
;
3146 -- This procedure is called load the unit whose name is given by Nam.
3147 -- This unit is being loaded to see whether it contains an optional
3148 -- generic body. The returned value is the loaded unit, which is always
3149 -- a package body (only package bodies can contain other entities in the
3150 -- sense in which Has_Generic_Body is interested). We only attempt to
3151 -- load bodies if we are generating code. If we are in semantics check
3152 -- only mode, then it would be wrong to load bodies that are not
3153 -- required from a semantic point of view, so in this case we return
3154 -- Empty. The result is that the caller may incorrectly decide that a
3155 -- generic spec does not have a body when in fact it does, but the only
3156 -- harm in this is that some warnings on elaboration problems may be
3157 -- lost in semantic checks only mode, which is not big loss. We also
3158 -- return Empty if we go for a body and it is not there.
3160 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
;
3161 -- PE is the entity for a package spec. This function locates the
3162 -- corresponding package body, returning Empty if none is found. The
3163 -- package body returned is fully parsed but may not yet be analyzed,
3164 -- so only syntactic fields should be referenced.
3170 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
is
3175 while Present
(Nod
) loop
3177 -- If we found the package body we are looking for, return it
3179 if Nkind
(Nod
) = N_Package_Body
3180 and then Chars
(Defining_Unit_Name
(Nod
)) = Chars
(E
)
3184 -- If we found the stub for the body, go after the subunit,
3185 -- loading it if necessary.
3187 elsif Nkind
(Nod
) = N_Package_Body_Stub
3188 and then Chars
(Defining_Identifier
(Nod
)) = Chars
(E
)
3190 if Present
(Library_Unit
(Nod
)) then
3191 return Unit
(Library_Unit
(Nod
));
3194 return Load_Package_Body
(Get_Unit_Name
(Nod
));
3197 -- If neither package body nor stub, keep looking on chain
3207 -----------------------
3208 -- Load_Package_Body --
3209 -----------------------
3211 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
is
3212 U
: Unit_Number_Type
;
3215 if Operating_Mode
/= Generate_Code
then
3228 return Unit
(Cunit
(U
));
3231 end Load_Package_Body
;
3233 -------------------------------
3234 -- Locate_Corresponding_Body --
3235 -------------------------------
3237 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
is
3238 Spec
: constant Node_Id
:= Declaration_Node
(PE
);
3239 Decl
: constant Node_Id
:= Parent
(Spec
);
3240 Scop
: constant Entity_Id
:= Scope
(PE
);
3244 if Is_Library_Level_Entity
(PE
) then
3246 -- If package is a library unit that requires a body, we have no
3247 -- choice but to go after that body because it might contain an
3248 -- optional body for the original generic package.
3250 if Unit_Requires_Body
(PE
) then
3252 -- Load the body. Note that we are a little careful here to use
3253 -- Spec to get the unit number, rather than PE or Decl, since
3254 -- in the case where the package is itself a library level
3255 -- instantiation, Spec will properly reference the generic
3256 -- template, which is what we really want.
3260 (Get_Body_Name
(Unit_Name
(Get_Source_Unit
(Spec
))));
3262 -- But if the package is a library unit that does NOT require
3263 -- a body, then no body is permitted, so we are sure that there
3264 -- is no body for the original generic package.
3270 -- Otherwise look and see if we are embedded in a further package
3272 elsif Is_Package_Or_Generic_Package
(Scop
) then
3274 -- If so, get the body of the enclosing package, and look in
3275 -- its package body for the package body we are looking for.
3277 PBody
:= Locate_Corresponding_Body
(Scop
);
3282 return Find_Body_In
(PE
, First
(Declarations
(PBody
)));
3285 -- If we are not embedded in a further package, then the body
3286 -- must be in the same declarative part as we are.
3289 return Find_Body_In
(PE
, Next
(Decl
));
3291 end Locate_Corresponding_Body
;
3293 -- Start of processing for Has_Generic_Body
3296 if Present
(Corresponding_Body
(Decl
)) then
3299 elsif Unit_Requires_Body
(Ent
) then
3302 -- Compilation units cannot have optional bodies
3304 elsif Is_Compilation_Unit
(Ent
) then
3307 -- Otherwise look at what scope we are in
3310 Scop
:= Scope
(Ent
);
3312 -- Case of entity is in other than a package spec, in this case
3313 -- the body, if present, must be in the same declarative part.
3315 if not Is_Package_Or_Generic_Package
(Scop
) then
3320 -- Declaration node may get us a spec, so if so, go to
3321 -- the parent declaration.
3323 P
:= Declaration_Node
(Ent
);
3324 while not Is_List_Member
(P
) loop
3328 return Present
(Find_Body_In
(Ent
, Next
(P
)));
3331 -- If the entity is in a package spec, then we have to locate
3332 -- the corresponding package body, and look there.
3336 PBody
: constant Node_Id
:= Locate_Corresponding_Body
(Scop
);
3344 (Find_Body_In
(Ent
, (First
(Declarations
(PBody
)))));
3349 end Has_Generic_Body
;
3351 -----------------------
3352 -- Insert_Elab_Check --
3353 -----------------------
3355 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
) is
3357 Loc
: constant Source_Ptr
:= Sloc
(N
);
3360 -- The check (N_Raise_Program_Error) node to be inserted
3363 -- If expansion is disabled, do not generate any checks. Also
3364 -- skip checks if any subunits are missing because in either
3365 -- case we lack the full information that we need, and no object
3366 -- file will be created in any case.
3368 if not Expander_Active
or else Subunits_Missing
then
3372 -- If we have a generic instantiation, where Instance_Spec is set,
3373 -- then this field points to a generic instance spec that has
3374 -- been inserted before the instantiation node itself, so that
3375 -- is where we want to insert a check.
3377 if Nkind
(N
) in N_Generic_Instantiation
3378 and then Present
(Instance_Spec
(N
))
3380 Nod
:= Instance_Spec
(N
);
3385 -- Build check node, possibly with condition
3388 Make_Raise_Program_Error
(Loc
, Reason
=> PE_Access_Before_Elaboration
);
3391 Set_Condition
(Chk
, Make_Op_Not
(Loc
, Right_Opnd
=> C
));
3394 -- If we are inserting at the top level, insert in Aux_Decls
3396 if Nkind
(Parent
(Nod
)) = N_Compilation_Unit
then
3398 ADN
: constant Node_Id
:= Aux_Decls_Node
(Parent
(Nod
));
3401 if No
(Declarations
(ADN
)) then
3402 Set_Declarations
(ADN
, New_List
(Chk
));
3404 Append_To
(Declarations
(ADN
), Chk
);
3410 -- Otherwise just insert as an action on the node in question
3413 Insert_Action
(Nod
, Chk
);
3415 end Insert_Elab_Check
;
3417 -------------------------------
3418 -- Is_Finalization_Procedure --
3419 -------------------------------
3421 function Is_Finalization_Procedure
(Id
: Entity_Id
) return Boolean is
3423 -- Check whether Id is a procedure with at least one parameter
3425 if Ekind
(Id
) = E_Procedure
and then Present
(First_Formal
(Id
)) then
3427 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Id
));
3428 Deep_Fin
: Entity_Id
:= Empty
;
3429 Fin
: Entity_Id
:= Empty
;
3432 -- If the type of the first formal does not require finalization
3433 -- actions, then this is definitely not [Deep_]Finalize.
3435 if not Needs_Finalization
(Typ
) then
3439 -- At this point we have the following scenario:
3441 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3443 -- Recover the two possible versions of [Deep_]Finalize using the
3444 -- type of the first parameter and compare with the input.
3446 Deep_Fin
:= TSS
(Typ
, TSS_Deep_Finalize
);
3448 if Is_Controlled
(Typ
) then
3449 Fin
:= Find_Prim_Op
(Typ
, Name_Finalize
);
3452 return (Present
(Deep_Fin
) and then Id
= Deep_Fin
)
3453 or else (Present
(Fin
) and then Id
= Fin
);
3458 end Is_Finalization_Procedure
;
3464 procedure Output_Calls
3466 Check_Elab_Flag
: Boolean)
3468 function Emit
(Flag
: Boolean) return Boolean;
3469 -- Determine whether to emit an error message based on the combination
3470 -- of flags Check_Elab_Flag and Flag.
3472 function Is_Printable_Error_Name
return Boolean;
3473 -- An internal function, used to determine if a name, stored in the
3474 -- Name_Buffer, is either a non-internal name, or is an internal name
3475 -- that is printable by the error message circuits (i.e. it has a single
3476 -- upper case letter at the end).
3482 function Emit
(Flag
: Boolean) return Boolean is
3484 if Check_Elab_Flag
then
3491 -----------------------------
3492 -- Is_Printable_Error_Name --
3493 -----------------------------
3495 function Is_Printable_Error_Name
return Boolean is
3497 if not Is_Internal_Name
then
3500 elsif Name_Len
= 1 then
3504 Name_Len
:= Name_Len
- 1;
3505 return not Is_Internal_Name
;
3507 end Is_Printable_Error_Name
;
3513 -- Start of processing for Output_Calls
3516 for J
in reverse 1 .. Elab_Call
.Last
loop
3517 Error_Msg_Sloc
:= Elab_Call
.Table
(J
).Cloc
;
3519 Ent
:= Elab_Call
.Table
(J
).Ent
;
3520 Get_Name_String
(Chars
(Ent
));
3522 -- Dynamic elaboration model, warnings controlled by -gnatwl
3524 if Dynamic_Elaboration_Checks
then
3525 if Emit
(Elab_Warnings
) then
3526 if Is_Generic_Unit
(Ent
) then
3527 Error_Msg_NE
("\\?l?& instantiated #", N
, Ent
);
3528 elsif Is_Init_Proc
(Ent
) then
3529 Error_Msg_N
("\\?l?initialization procedure called #", N
);
3530 elsif Is_Printable_Error_Name
then
3531 Error_Msg_NE
("\\?l?& called #", N
, Ent
);
3533 Error_Msg_N
("\\?l?called #", N
);
3537 -- Static elaboration model, info messages controlled by -gnatel
3540 if Emit
(Elab_Info_Messages
) then
3541 if Is_Generic_Unit
(Ent
) then
3542 Error_Msg_NE
("\\?$?& instantiated #", N
, Ent
);
3543 elsif Is_Init_Proc
(Ent
) then
3544 Error_Msg_N
("\\?$?initialization procedure called #", N
);
3545 elsif Is_Printable_Error_Name
then
3546 Error_Msg_NE
("\\?$?& called #", N
, Ent
);
3548 Error_Msg_N
("\\?$?called #", N
);
3555 ----------------------------
3556 -- Same_Elaboration_Scope --
3557 ----------------------------
3559 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean is
3564 -- Find elaboration scope for Scop1
3565 -- This is either a subprogram or a compilation unit.
3568 while S1
/= Standard_Standard
3569 and then not Is_Compilation_Unit
(S1
)
3570 and then Ekind_In
(S1
, E_Package
, E_Protected_Type
, E_Block
)
3575 -- Find elaboration scope for Scop2
3578 while S2
/= Standard_Standard
3579 and then not Is_Compilation_Unit
(S2
)
3580 and then Ekind_In
(S2
, E_Package
, E_Protected_Type
, E_Block
)
3586 end Same_Elaboration_Scope
;
3592 procedure Set_C_Scope
is
3594 while not Is_Compilation_Unit
(C_Scope
) loop
3595 C_Scope
:= Scope
(C_Scope
);
3603 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
is
3607 -- Check for case of body entity
3608 -- Why is the check for E_Void needed???
3610 if Ekind_In
(E
, E_Void
, E_Subprogram_Body
, E_Package_Body
) then
3614 Decl
:= Parent
(Decl
);
3615 exit when Nkind
(Decl
) in N_Proper_Body
;
3618 return Corresponding_Spec
(Decl
);
3629 procedure Supply_Bodies
(N
: Node_Id
) is
3631 if Nkind
(N
) = N_Subprogram_Declaration
then
3633 Ent
: constant Entity_Id
:= Defining_Unit_Name
(Specification
(N
));
3636 -- Internal subprograms will already have a generated body, so
3637 -- there is no need to provide a stub for them.
3639 if No
(Corresponding_Body
(N
)) then
3641 Loc
: constant Source_Ptr
:= Sloc
(N
);
3642 Formals
: constant List_Id
:= Copy_Parameter_List
(Ent
);
3643 Nam
: constant Entity_Id
:=
3644 Make_Defining_Identifier
(Loc
, Chars
(Ent
));
3645 Stats
: constant List_Id
:=
3647 Make_Raise_Program_Error
(Loc
,
3648 Reason
=> PE_Access_Before_Elaboration
));
3652 if Ekind
(Ent
) = E_Function
then
3654 Make_Function_Specification
(Loc
,
3655 Defining_Unit_Name
=> Nam
,
3656 Parameter_Specifications
=> Formals
,
3657 Result_Definition
=>
3659 (Result_Definition
(Specification
(N
))));
3661 -- We cannot reliably make a return statement for this
3662 -- body, but none is needed because the call raises
3665 Set_Return_Present
(Ent
);
3669 Make_Procedure_Specification
(Loc
,
3670 Defining_Unit_Name
=> Nam
,
3671 Parameter_Specifications
=> Formals
);
3674 Insert_After_And_Analyze
(N
,
3675 Make_Subprogram_Body
(Loc
,
3676 Specification
=> Spec
,
3677 Declarations
=> New_List
,
3678 Handled_Statement_Sequence
=>
3679 Make_Handled_Sequence_Of_Statements
(Loc
, Stats
)));
3684 elsif Nkind
(N
) = N_Package_Declaration
then
3686 Spec
: constant Node_Id
:= Specification
(N
);
3688 Push_Scope
(Defining_Unit_Name
(Spec
));
3689 Supply_Bodies
(Visible_Declarations
(Spec
));
3690 Supply_Bodies
(Private_Declarations
(Spec
));
3696 procedure Supply_Bodies
(L
: List_Id
) is
3701 while Present
(Elmt
) loop
3702 Supply_Bodies
(Elmt
);
3712 function Within
(E1
, E2
: Entity_Id
) return Boolean is
3719 elsif Scop
= Standard_Standard
then
3722 Scop
:= Scope
(Scop
);
3727 --------------------------
3728 -- Within_Elaborate_All --
3729 --------------------------
3731 function Within_Elaborate_All
3732 (Unit
: Unit_Number_Type
;
3733 E
: Entity_Id
) return Boolean
3735 type Unit_Number_Set
is array (Main_Unit
.. Last_Unit
) of Boolean;
3736 pragma Pack
(Unit_Number_Set
);
3738 Seen
: Unit_Number_Set
:= (others => False);
3739 -- Seen (X) is True after we have seen unit X in the walk. This is used
3740 -- to prevent processing the same unit more than once.
3742 Result
: Boolean := False;
3744 procedure Helper
(Unit
: Unit_Number_Type
);
3745 -- This helper procedure does all the work for Within_Elaborate_All. It
3746 -- walks the dependency graph, and sets Result to True if it finds an
3747 -- appropriate Elaborate_All.
3753 procedure Helper
(Unit
: Unit_Number_Type
) is
3754 CU
: constant Node_Id
:= Cunit
(Unit
);
3758 Elab_Id
: Entity_Id
;
3765 Seen
(Unit
) := True;
3768 -- First, check for Elaborate_Alls on this unit
3770 Item
:= First
(Context_Items
(CU
));
3771 while Present
(Item
) loop
3772 if Nkind
(Item
) = N_Pragma
3773 and then Pragma_Name
(Item
) = Name_Elaborate_All
3775 -- Return if some previous error on the pragma itself. The
3776 -- pragma may be unanalyzed, because of a previous error, or
3777 -- if it is the context of a subunit, inherited by its parent.
3779 if Error_Posted
(Item
) or else not Analyzed
(Item
) then
3785 (Expression
(First
(Pragma_Argument_Associations
(Item
))));
3792 Par
:= Parent
(Unit_Declaration_Node
(Elab_Id
));
3794 Item2
:= First
(Context_Items
(Par
));
3795 while Present
(Item2
) loop
3796 if Nkind
(Item2
) = N_With_Clause
3797 and then Entity
(Name
(Item2
)) = E
3798 and then not Limited_Present
(Item2
)
3811 -- Second, recurse on with's. We could do this as part of the above
3812 -- loop, but it's probably more efficient to have two loops, because
3813 -- the relevant Elaborate_All is likely to be on the initial unit. In
3814 -- other words, we're walking the with's breadth-first. This part is
3815 -- only necessary in the dynamic elaboration model.
3817 if Dynamic_Elaboration_Checks
then
3818 Item
:= First
(Context_Items
(CU
));
3819 while Present
(Item
) loop
3820 if Nkind
(Item
) = N_With_Clause
3821 and then not Limited_Present
(Item
)
3823 -- Note: the following call to Get_Cunit_Unit_Number does a
3824 -- linear search, which could be slow, but it's OK because
3825 -- we're about to give a warning anyway. Also, there might
3826 -- be hundreds of units, but not millions. If it turns out
3827 -- to be a problem, we could store the Get_Cunit_Unit_Number
3828 -- in each N_Compilation_Unit node, but that would involve
3829 -- rearranging N_Compilation_Unit_Aux to make room.
3831 Helper
(Get_Cunit_Unit_Number
(Library_Unit
(Item
)));
3843 -- Start of processing for Within_Elaborate_All
3848 end Within_Elaborate_All
;