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
)
726 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
727 -- check, we don't mind in this case if the call occurs before the body
728 -- since this is all generated code.
730 if Nkind
(Original_Node
(N
)) = N_Attribute_Reference
731 and then Attribute_Name
(Original_Node
(N
)) = Name_Valid_Scalars
736 -- Intrinsics such as instances of Unchecked_Deallocation do not have
737 -- any body, so elaboration checking is not needed, and would be wrong.
739 if Is_Intrinsic_Subprogram
(E
) then
743 -- Proceed with check
747 -- For a variable reference, just set Body_Acts_As_Spec to False
749 if Variable_Case
then
750 Body_Acts_As_Spec
:= False;
752 -- Additional checks for all other cases
755 -- Go to parent for derived subprogram, or to original subprogram in
756 -- the case of a renaming (Alias covers both these cases).
759 if (Suppress_Elaboration_Warnings
(Ent
)
760 or else Elaboration_Checks_Suppressed
(Ent
))
761 and then (Inst_Case
or else No
(Alias
(Ent
)))
766 -- Nothing to do for imported entities
768 if Is_Imported
(Ent
) then
772 exit when Inst_Case
or else No
(Alias
(Ent
));
776 Decl
:= Unit_Declaration_Node
(Ent
);
778 if Nkind
(Decl
) = N_Subprogram_Body
then
779 Body_Acts_As_Spec
:= True;
781 elsif Nkind_In
(Decl
, N_Subprogram_Declaration
,
782 N_Subprogram_Body_Stub
)
785 Body_Acts_As_Spec
:= False;
787 -- If we have none of an instantiation, subprogram body or subprogram
788 -- declaration, or in the SPARK case, a variable reference, then
789 -- it is not a case that we want to check. (One case is a call to a
790 -- generic formal subprogram, where we do not want the check in the
800 if Elaboration_Checks_Suppressed
(E_Scope
)
801 or else Suppress_Elaboration_Warnings
(E_Scope
)
806 -- Exit when we get to compilation unit, not counting subunits
808 exit when Is_Compilation_Unit
(E_Scope
)
809 and then (Is_Child_Unit
(E_Scope
)
810 or else Scope
(E_Scope
) = Standard_Standard
);
812 pragma Assert
(E_Scope
/= Standard_Standard
);
814 -- Move up a scope looking for compilation unit
816 E_Scope
:= Scope
(E_Scope
);
819 -- No checks needed for pure or preelaborated compilation units
821 if Is_Pure
(E_Scope
) or else Is_Preelaborated
(E_Scope
) then
825 -- If the generic entity is within a deeper instance than we are, then
826 -- either the instantiation to which we refer itself caused an ABE, in
827 -- which case that will be handled separately, or else we know that the
828 -- body we need appears as needed at the point of the instantiation.
829 -- However, this assumption is only valid if we are in static mode.
831 if not Dynamic_Elaboration_Checks
833 Instantiation_Depth
(Sloc
(Ent
)) > Instantiation_Depth
(Sloc
(N
))
838 -- Do not give a warning for a package with no body
840 if Ekind
(Ent
) = E_Generic_Package
and then not Has_Generic_Body
(N
) then
844 -- Case of entity is in same unit as call or instantiation. In the
845 -- instantiation case, W_Scope may be different from E_Scope; we want
846 -- the unit in which the instantiation occurs, since we're analyzing
847 -- based on the expansion.
849 if W_Scope
= C_Scope
then
850 if not Inter_Unit_Only
then
851 Check_Internal_Call
(N
, Ent
, Outer_Scope
, E
);
857 -- Case of entity is not in current unit (i.e. with'ed unit case)
859 -- We are only interested in such calls if the outer call was from
860 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
862 if not From_Elab_Code
and then not Dynamic_Elaboration_Checks
then
866 -- Nothing to do if some scope said that no checks were required
872 -- Nothing to do for a generic instance, because a call to an instance
873 -- cannot fail the elaboration check, because the body of the instance
874 -- is always elaborated immediately after the spec.
876 if Call_To_Instance_From_Outside
(Ent
) then
880 -- Nothing to do if subprogram with no separate spec. However, a call
881 -- to Deep_Initialize may result in a call to a user-defined Initialize
882 -- procedure, which imposes a body dependency. This happens only if the
883 -- type is controlled and the Initialize procedure is not inherited.
885 if Body_Acts_As_Spec
then
886 if Is_TSS
(Ent
, TSS_Deep_Initialize
) then
888 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Ent
));
892 if not Is_Controlled
(Typ
) then
895 Init
:= Find_Prim_Op
(Typ
, Name_Initialize
);
897 if Comes_From_Source
(Init
) then
910 -- Check cases of internal units
912 Callee_Unit_Internal
:= In_Internal_Unit
(E_Scope
);
914 -- Do not give a warning if the with'ed unit is internal and this is
915 -- the generic instantiation case (this saves a lot of hassle dealing
916 -- with the Text_IO special child units)
918 if Callee_Unit_Internal
and Inst_Case
then
922 if C_Scope
= Standard_Standard
then
923 Caller_Unit_Internal
:= False;
925 Caller_Unit_Internal
:= In_Internal_Unit
(C_Scope
);
928 -- Do not give a warning if the with'ed unit is internal and the caller
929 -- is not internal (since the binder always elaborates internal units
932 if Callee_Unit_Internal
and not Caller_Unit_Internal
then
936 -- For now, if debug flag -gnatdE is not set, do no checking for one
937 -- internal unit withing another. This fixes the problem with the sgi
938 -- build and storage errors. To be resolved later ???
940 if (Callee_Unit_Internal
and Caller_Unit_Internal
)
941 and not Debug_Flag_EE
946 if Is_TSS
(E
, TSS_Deep_Initialize
) then
950 -- If the call is in an instance, and the called entity is not
951 -- defined in the same instance, then the elaboration issue focuses
952 -- around the unit containing the template, it is this unit that
953 -- requires an Elaborate_All.
955 -- However, if we are doing dynamic elaboration, we need to chase the
956 -- call in the usual manner.
958 -- We also need to chase the call in the usual manner if it is a call
959 -- to a generic formal parameter, since that case was not handled as
960 -- part of the processing of the template.
962 Inst_Caller
:= Instantiation
(Get_Source_File_Index
(Sloc
(N
)));
963 Inst_Callee
:= Instantiation
(Get_Source_File_Index
(Sloc
(Ent
)));
965 if Inst_Caller
= No_Location
then
966 Unit_Caller
:= No_Unit
;
968 Unit_Caller
:= Get_Source_Unit
(N
);
971 if Inst_Callee
= No_Location
then
972 Unit_Callee
:= No_Unit
;
974 Unit_Callee
:= Get_Source_Unit
(Ent
);
977 if Unit_Caller
/= No_Unit
978 and then Unit_Callee
/= Unit_Caller
979 and then not Dynamic_Elaboration_Checks
980 and then not Is_Call_Of_Generic_Formal
(N
)
982 E_Scope
:= Spec_Entity
(Cunit_Entity
(Unit_Caller
));
984 -- If we don't get a spec entity, just ignore call. Not quite
985 -- clear why this check is necessary. ???
991 -- Otherwise step to enclosing compilation unit
993 while not Is_Compilation_Unit
(E_Scope
) loop
994 E_Scope
:= Scope
(E_Scope
);
997 -- For the case where N is not an instance, and is not a call within
998 -- instance to other than a generic formal, we recompute E_Scope
999 -- for the error message, since we do NOT want to go to the unit
1000 -- that has the ultimate declaration in the case of renaming and
1001 -- derivation and we also want to go to the generic unit in the
1002 -- case of an instance, and no further.
1005 -- Loop to carefully follow renamings and derivations one step
1006 -- outside the current unit, but not further.
1008 if not (Inst_Case
or Variable_Case
)
1009 and then Present
(Alias
(Ent
))
1011 E_Scope
:= Alias
(Ent
);
1017 while not Is_Compilation_Unit
(E_Scope
) loop
1018 E_Scope
:= Scope
(E_Scope
);
1021 -- If E_Scope is the same as C_Scope, it means that there
1022 -- definitely was a local renaming or derivation, and we
1023 -- are not yet out of the current unit.
1025 exit when E_Scope
/= C_Scope
;
1029 -- If no alias, there could be a previous error, but not if we've
1030 -- already reached the outermost level (Standard).
1038 if Within_Elaborate_All
(Current_Sem_Unit
, E_Scope
) then
1042 -- Determine whether the Default_Initial_Condition procedure of some
1043 -- type is being invoked.
1045 Is_DIC
:= Ekind
(Ent
) = E_Procedure
and then Is_DIC_Procedure
(Ent
);
1047 -- Checks related to Default_Initial_Condition fall under the SPARK
1048 -- umbrella because this is a SPARK-specific annotation.
1050 SPARK_Elab_Errors
:=
1051 SPARK_Mode
= On
and (Is_DIC
or Dynamic_Elaboration_Checks
);
1053 -- Now check if an Elaborate_All (or dynamic check) is needed
1055 if (Elab_Info_Messages
or Elab_Warnings
or SPARK_Elab_Errors
)
1056 and then Generate_Warnings
1057 and then not Suppress_Elaboration_Warnings
(Ent
)
1058 and then not Elaboration_Checks_Suppressed
(Ent
)
1059 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1060 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1062 -- Instantiation case
1065 if Comes_From_Source
(Ent
) and then SPARK_Elab_Errors
then
1067 ("instantiation of & during elaboration in SPARK", N
, Ent
);
1070 ("instantiation of & may raise Program_Error?l?",
1071 "info: instantiation of & during elaboration?$?", Ent
);
1074 -- Indirect call case, info message only in static elaboration
1075 -- case, because the attribute reference itself cannot raise an
1076 -- exception. Note that SPARK does not permit indirect calls.
1078 elsif Access_Case
then
1079 Elab_Warning
("", "info: access to & during elaboration?$?", Ent
);
1081 -- Variable reference in SPARK mode
1083 elsif Variable_Case
then
1084 if Comes_From_Source
(Ent
) and then SPARK_Elab_Errors
then
1086 ("reference to & during elaboration in SPARK", N
, Ent
);
1089 -- Subprogram call case
1092 if Nkind
(Name
(N
)) in N_Has_Entity
1093 and then Is_Init_Proc
(Entity
(Name
(N
)))
1094 and then Comes_From_Source
(Ent
)
1097 ("implicit call to & may raise Program_Error?l?",
1098 "info: implicit call to & during elaboration?$?",
1101 elsif SPARK_Elab_Errors
then
1103 -- Emit a specialized error message when the elaboration of an
1104 -- object of a private type evaluates the expression of pragma
1105 -- Default_Initial_Condition. This prevents the internal name
1106 -- of the procedure from appearing in the error message.
1110 ("call to Default_Initial_Condition during elaboration in "
1114 ("call to & during elaboration in SPARK", N
, Ent
);
1119 ("call to & may raise Program_Error?l?",
1120 "info: call to & during elaboration?$?",
1125 Error_Msg_Qual_Level
:= Nat
'Last;
1127 -- Case of Elaborate_All not present and required, for SPARK this
1128 -- is an error, so give an error message.
1130 if SPARK_Elab_Errors
then
1131 Error_Msg_NE
-- CODEFIX
1132 ("\Elaborate_All pragma required for&", N
, W_Scope
);
1134 -- Otherwise we generate an implicit pragma. For a subprogram
1135 -- instantiation, Elaborate is good enough, since no transitive
1136 -- call is possible at elaboration time in this case.
1138 elsif Nkind
(N
) in N_Subprogram_Instantiation
then
1140 ("\missing pragma Elaborate for&?l?",
1141 "\implicit pragma Elaborate for& generated?$?",
1144 -- For all other cases, we need an implicit Elaborate_All
1148 ("\missing pragma Elaborate_All for&?l?",
1149 "\implicit pragma Elaborate_All for & generated?$?",
1153 Error_Msg_Qual_Level
:= 0;
1155 -- Take into account the flags related to elaboration warning
1156 -- messages when enumerating the various calls involved. This
1157 -- ensures the proper pairing of the main warning and the
1158 -- clarification messages generated by Output_Calls.
1160 Output_Calls
(N
, Check_Elab_Flag
=> True);
1162 -- Set flag to prevent further warnings for same unit unless in
1165 if not All_Errors_Mode
and not Dynamic_Elaboration_Checks
then
1166 Set_Suppress_Elaboration_Warnings
(W_Scope
);
1170 -- Check for runtime elaboration check required
1172 if Dynamic_Elaboration_Checks
then
1173 if not Elaboration_Checks_Suppressed
(Ent
)
1174 and then not Elaboration_Checks_Suppressed
(W_Scope
)
1175 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1176 and then not Cunit_SC
1178 -- Runtime elaboration check required. Generate check of the
1179 -- elaboration Boolean for the unit containing the entity.
1181 -- Note that for this case, we do check the real unit (the one
1182 -- from following renamings, since that is the issue).
1184 -- Could this possibly miss a useless but required PE???
1186 Insert_Elab_Check
(N
,
1187 Make_Attribute_Reference
(Loc
,
1188 Attribute_Name
=> Name_Elaborated
,
1190 New_Occurrence_Of
(Spec_Entity
(E_Scope
), Loc
)));
1192 -- Prevent duplicate elaboration checks on the same call,
1193 -- which can happen if the body enclosing the call appears
1194 -- itself in a call whose elaboration check is delayed.
1196 if Nkind
(N
) in N_Subprogram_Call
then
1197 Set_No_Elaboration_Check
(N
);
1201 -- Case of static elaboration model
1204 -- Do not do anything if elaboration checks suppressed. Note that
1205 -- we check Ent here, not E, since we want the real entity for the
1206 -- body to see if checks are suppressed for it, not the dummy
1207 -- entry for renamings or derivations.
1209 if Elaboration_Checks_Suppressed
(Ent
)
1210 or else Elaboration_Checks_Suppressed
(E_Scope
)
1211 or else Elaboration_Checks_Suppressed
(W_Scope
)
1215 -- Do not generate an Elaborate_All for finalization routines
1216 -- that perform partial clean up as part of initialization.
1218 elsif In_Init_Proc
and then Is_Finalization_Procedure
(Ent
) then
1221 -- Here we need to generate an implicit elaborate all
1224 -- Generate Elaborate_All warning unless suppressed
1226 if (Elab_Info_Messages
and Generate_Warnings
and not Inst_Case
)
1227 and then not Suppress_Elaboration_Warnings
(Ent
)
1228 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1229 and then not Suppress_Elaboration_Warnings
(W_Scope
)
1231 Error_Msg_Node_2
:= W_Scope
;
1233 ("info: call to& in elaboration code requires pragma "
1234 & "Elaborate_All on&?$?", N
, E
);
1237 -- Set indication for binder to generate Elaborate_All
1239 Set_Elaboration_Constraint
(N
, E
, W_Scope
);
1244 -----------------------------
1245 -- Check_Bad_Instantiation --
1246 -----------------------------
1248 procedure Check_Bad_Instantiation
(N
: Node_Id
) is
1252 -- Nothing to do if we do not have an instantiation (happens in some
1253 -- error cases, and also in the formal package declaration case)
1255 if Nkind
(N
) not in N_Generic_Instantiation
then
1258 -- Nothing to do if serious errors detected (avoid cascaded errors)
1260 elsif Serious_Errors_Detected
/= 0 then
1263 -- Nothing to do if not in full analysis mode
1265 elsif not Full_Analysis
then
1268 -- Nothing to do if inside a generic template
1270 elsif Inside_A_Generic
then
1273 -- Nothing to do if a library level instantiation
1275 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1278 -- Nothing to do if we are compiling a proper body for semantic
1279 -- purposes only. The generic body may be in another proper body.
1282 Nkind
(Parent
(Unit_Declaration_Node
(Main_Unit_Entity
))) = N_Subunit
1287 Ent
:= Get_Generic_Entity
(N
);
1289 -- The case we are interested in is when the generic spec is in the
1290 -- current declarative part
1292 if not Same_Elaboration_Scope
(Current_Scope
, Scope
(Ent
))
1293 or else not In_Same_Extended_Unit
(N
, Ent
)
1298 -- If the generic entity is within a deeper instance than we are, then
1299 -- either the instantiation to which we refer itself caused an ABE, in
1300 -- which case that will be handled separately. Otherwise, we know that
1301 -- the body we need appears as needed at the point of the instantiation.
1302 -- If they are both at the same level but not within the same instance
1303 -- then the body of the generic will be in the earlier instance.
1306 D1
: constant Nat
:= Instantiation_Depth
(Sloc
(Ent
));
1307 D2
: constant Nat
:= Instantiation_Depth
(Sloc
(N
));
1314 and then Is_Generic_Instance
(Scope
(Ent
))
1315 and then not In_Open_Scopes
(Scope
(Ent
))
1321 -- Now we can proceed, if the entity being called has a completion,
1322 -- then we are definitely OK, since we have already seen the body.
1324 if Has_Completion
(Ent
) then
1328 -- If there is no body, then nothing to do
1330 if not Has_Generic_Body
(N
) then
1334 -- Here we definitely have a bad instantiation
1336 Error_Msg_Warn
:= SPARK_Mode
/= On
;
1337 Error_Msg_NE
("cannot instantiate& before body seen<<", N
, Ent
);
1339 if Present
(Instance_Spec
(N
)) then
1340 Supply_Bodies
(Instance_Spec
(N
));
1343 Error_Msg_N
("\Program_Error [<<", N
);
1344 Insert_Elab_Check
(N
);
1345 Set_ABE_Is_Certain
(N
);
1346 end Check_Bad_Instantiation
;
1348 ---------------------
1349 -- Check_Elab_Call --
1350 ---------------------
1352 procedure Check_Elab_Call
1354 Outer_Scope
: Entity_Id
:= Empty
;
1355 In_Init_Proc
: Boolean := False)
1361 -- If the reference is not in the main unit, there is nothing to check.
1362 -- Elaboration call from units in the context of the main unit will lead
1363 -- to semantic dependencies when those units are compiled.
1365 if not In_Extended_Main_Code_Unit
(N
) then
1369 -- For an entry call, check relevant restriction
1371 if Nkind
(N
) = N_Entry_Call_Statement
1372 and then not In_Subprogram_Or_Concurrent_Unit
1374 Check_Restriction
(No_Entry_Calls_In_Elaboration_Code
, N
);
1376 -- Nothing to do if this is not an expected type of reference (happens
1377 -- in some error conditions, and in some cases where rewriting occurs).
1379 elsif Nkind
(N
) not in N_Subprogram_Call
1380 and then Nkind
(N
) /= N_Attribute_Reference
1381 and then (SPARK_Mode
/= On
1382 or else Nkind
(N
) not in N_Has_Entity
1383 or else No
(Entity
(N
))
1384 or else Ekind
(Entity
(N
)) /= E_Variable
)
1388 -- Nothing to do if this is a call already rewritten for elab checking.
1389 -- Such calls appear as the targets of If_Expressions.
1391 -- This check MUST be wrong, it catches far too much
1393 elsif Nkind
(Parent
(N
)) = N_If_Expression
then
1396 -- Nothing to do if inside a generic template
1398 elsif Inside_A_Generic
1399 and then No
(Enclosing_Generic_Body
(N
))
1403 -- Nothing to do if call is being pre-analyzed, as when within a
1404 -- pre/postcondition, a predicate, or an invariant.
1406 elsif In_Spec_Expression
then
1410 -- Nothing to do if this is a call to a postcondition, which is always
1411 -- within a subprogram body, even though the current scope may be the
1412 -- enclosing scope of the subprogram.
1414 if Nkind
(N
) = N_Procedure_Call_Statement
1415 and then Is_Entity_Name
(Name
(N
))
1416 and then Chars
(Entity
(Name
(N
))) = Name_uPostconditions
1421 -- Here we have a reference at elaboration time that must be checked
1423 if Debug_Flag_LL
then
1424 Write_Str
(" Check_Elab_Ref: ");
1426 if Nkind
(N
) = N_Attribute_Reference
then
1427 if not Is_Entity_Name
(Prefix
(N
)) then
1428 Write_Str
("<<not entity name>>");
1430 Write_Name
(Chars
(Entity
(Prefix
(N
))));
1433 Write_Str
("'Access");
1435 elsif No
(Name
(N
)) or else not Is_Entity_Name
(Name
(N
)) then
1436 Write_Str
("<<not entity name>> ");
1439 Write_Name
(Chars
(Entity
(Name
(N
))));
1442 Write_Str
(" reference at ");
1443 Write_Location
(Sloc
(N
));
1447 -- Climb up the tree to make sure we are not inside default expression
1448 -- of a parameter specification or a record component, since in both
1449 -- these cases, we will be doing the actual reference later, not now,
1450 -- and it is at the time of the actual reference (statically speaking)
1451 -- that we must do our static check, not at the time of its initial
1454 -- However, we have to check references within component definitions
1455 -- (e.g. a function call that determines an array component bound),
1456 -- so we terminate the loop in that case.
1459 while Present
(P
) loop
1460 if Nkind_In
(P
, N_Parameter_Specification
,
1461 N_Component_Declaration
)
1465 -- The reference occurs within the constraint of a component,
1466 -- so it must be checked.
1468 elsif Nkind
(P
) = N_Component_Definition
then
1476 -- Stuff that happens only at the outer level
1478 if No
(Outer_Scope
) then
1479 Elab_Visited
.Set_Last
(0);
1481 -- Nothing to do if current scope is Standard (this is a bit odd, but
1482 -- it happens in the case of generic instantiations).
1484 C_Scope
:= Current_Scope
;
1486 if C_Scope
= Standard_Standard
then
1490 -- First case, we are in elaboration code
1492 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1494 if From_Elab_Code
then
1496 -- Complain if ref that comes from source in preelaborated unit
1497 -- and we are not inside a subprogram (i.e. we are in elab code).
1499 if Comes_From_Source
(N
)
1500 and then In_Preelaborated_Unit
1501 and then not In_Inlined_Body
1502 and then Nkind
(N
) /= N_Attribute_Reference
1504 -- This is a warning in GNAT mode allowing such calls to be
1505 -- used in the predefined library with appropriate care.
1507 Error_Msg_Warn
:= GNAT_Mode
;
1509 ("<<non-static call not allowed in preelaborated unit", N
);
1513 -- Second case, we are inside a subprogram or concurrent unit, which
1514 -- means we are not in elaboration code.
1517 -- In this case, the issue is whether we are inside the
1518 -- declarative part of the unit in which we live, or inside its
1519 -- statements. In the latter case, there is no issue of ABE calls
1520 -- at this level (a call from outside to the unit in which we live
1521 -- might cause an ABE, but that will be detected when we analyze
1522 -- that outer level call, as it recurses into the called unit).
1524 -- Climb up the tree, doing this test, and also testing for being
1525 -- inside a default expression, which, as discussed above, is not
1526 -- checked at this stage.
1535 -- If we find a parentless subtree, it seems safe to assume
1536 -- that we are not in a declarative part and that no
1537 -- checking is required.
1543 if Is_List_Member
(P
) then
1544 L
:= List_Containing
(P
);
1551 exit when Nkind
(P
) = N_Subunit
;
1553 -- Filter out case of default expressions, where we do not
1554 -- do the check at this stage.
1556 if Nkind_In
(P
, N_Parameter_Specification
,
1557 N_Component_Declaration
)
1562 -- A protected body has no elaboration code and contains
1563 -- only other bodies.
1565 if Nkind
(P
) = N_Protected_Body
then
1568 elsif Nkind_In
(P
, N_Subprogram_Body
,
1573 if L
= Declarations
(P
) then
1576 -- We are not in elaboration code, but we are doing
1577 -- dynamic elaboration checks, in this case, we still
1578 -- need to do the reference, since the subprogram we are
1579 -- in could be called from another unit, also in dynamic
1580 -- elaboration check mode, at elaboration time.
1582 elsif Dynamic_Elaboration_Checks
then
1584 -- We provide a debug flag to disable this check. That
1585 -- way we have an easy work around for regressions
1586 -- that are caused by this new check. This debug flag
1587 -- can be removed later.
1589 if Debug_Flag_DD
then
1593 -- Do the check in this case
1597 elsif Nkind
(P
) = N_Task_Body
then
1599 -- The check is deferred until Check_Task_Activation
1600 -- but we need to capture local suppress pragmas
1601 -- that may inhibit checks on this call.
1603 Ent
:= Get_Referenced_Ent
(N
);
1608 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1609 or else Elaboration_Checks_Suppressed
(Ent
)
1610 or else Elaboration_Checks_Suppressed
(Scope
(Ent
))
1612 if Nkind
(N
) in N_Subprogram_Call
then
1613 Set_No_Elaboration_Check
(N
);
1619 -- Static model, call is not in elaboration code, we
1620 -- never need to worry, because in the static model the
1621 -- top-level caller always takes care of things.
1632 Ent
:= Get_Referenced_Ent
(N
);
1638 -- Determine whether a prior call to the same subprogram was already
1639 -- examined within the same context. If this is the case, then there is
1640 -- no need to proceed with the various warnings and checks because the
1641 -- work was already done for the previous call.
1644 Self
: constant Visited_Element
:=
1645 (Subp_Id
=> Ent
, Context
=> Parent
(N
));
1648 for Index
in 1 .. Elab_Visited
.Last
loop
1649 if Self
= Elab_Visited
.Table
(Index
) then
1655 -- See if we need to analyze this reference. We analyze it if either of
1656 -- the following conditions is met:
1658 -- It is an inner level call (since in this case it was triggered
1659 -- by an outer level call from elaboration code), but only if the
1660 -- call is within the scope of the original outer level call.
1662 -- It is an outer level reference from elaboration code, or a call to
1663 -- an entity is in the same elaboration scope.
1665 -- And in these cases, we will check both inter-unit calls and
1666 -- intra-unit (within a single unit) calls.
1668 C_Scope
:= Current_Scope
;
1670 -- If not outer level reference, then we follow it if it is within the
1671 -- original scope of the outer reference.
1673 if Present
(Outer_Scope
)
1674 and then Within
(Scope
(Ent
), Outer_Scope
)
1680 Outer_Scope
=> Outer_Scope
,
1681 Inter_Unit_Only
=> False,
1682 In_Init_Proc
=> In_Init_Proc
);
1684 -- Nothing to do if elaboration checks suppressed for this scope.
1685 -- However, an interesting exception, the fact that elaboration checks
1686 -- are suppressed within an instance (because we can trace the body when
1687 -- we process the template) does not extend to calls to generic formal
1690 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1691 and then not Is_Call_Of_Generic_Formal
(N
)
1695 elsif From_Elab_Code
then
1697 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1699 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1701 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1703 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1704 -- is set, then we will do the check, but only in the inter-unit case
1705 -- (this is to accommodate unguarded elaboration calls from other units
1706 -- in which this same mode is set). We don't want warnings in this case,
1707 -- it would generate warnings having nothing to do with elaboration.
1709 elsif Dynamic_Elaboration_Checks
then
1715 Inter_Unit_Only
=> True,
1716 Generate_Warnings
=> False);
1718 -- Otherwise nothing to do
1724 -- A call to an Init_Proc in elaboration code may bring additional
1725 -- dependencies, if some of the record components thereof have
1726 -- initializations that are function calls that come from source. We
1727 -- treat the current node as a call to each of these functions, to check
1728 -- their elaboration impact.
1730 if Is_Init_Proc
(Ent
) and then From_Elab_Code
then
1731 Process_Init_Proc
: declare
1732 Unit_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
1734 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
;
1735 -- Find subprogram calls within body of Init_Proc for Traverse
1736 -- instantiation below.
1738 procedure Traverse_Body
is new Traverse_Proc
(Check_Init_Call
);
1739 -- Traversal procedure to find all calls with body of Init_Proc
1741 ---------------------
1742 -- Check_Init_Call --
1743 ---------------------
1745 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
is
1749 if Nkind
(Nod
) in N_Subprogram_Call
1750 and then Is_Entity_Name
(Name
(Nod
))
1752 Func
:= Entity
(Name
(Nod
));
1754 if Comes_From_Source
(Func
) then
1756 (N
, Func
, Standard_Standard
, Inter_Unit_Only
=> True);
1764 end Check_Init_Call
;
1766 -- Start of processing for Process_Init_Proc
1769 if Nkind
(Unit_Decl
) = N_Subprogram_Body
then
1770 Traverse_Body
(Handled_Statement_Sequence
(Unit_Decl
));
1772 end Process_Init_Proc
;
1774 end Check_Elab_Call
;
1776 -----------------------
1777 -- Check_Elab_Assign --
1778 -----------------------
1780 procedure Check_Elab_Assign
(N
: Node_Id
) is
1784 Pkg_Spec
: Entity_Id
;
1785 Pkg_Body
: Entity_Id
;
1788 -- For record or array component, check prefix. If it is an access type,
1789 -- then there is nothing to do (we do not know what is being assigned),
1790 -- but otherwise this is an assignment to the prefix.
1792 if Nkind_In
(N
, N_Indexed_Component
,
1793 N_Selected_Component
,
1796 if not Is_Access_Type
(Etype
(Prefix
(N
))) then
1797 Check_Elab_Assign
(Prefix
(N
));
1803 -- For type conversion, check expression
1805 if Nkind
(N
) = N_Type_Conversion
then
1806 Check_Elab_Assign
(Expression
(N
));
1810 -- Nothing to do if this is not an entity reference otherwise get entity
1812 if Is_Entity_Name
(N
) then
1818 -- What we are looking for is a reference in the body of a package that
1819 -- modifies a variable declared in the visible part of the package spec.
1822 and then Comes_From_Source
(N
)
1823 and then not Suppress_Elaboration_Warnings
(Ent
)
1824 and then Ekind
(Ent
) = E_Variable
1825 and then not In_Private_Part
(Ent
)
1826 and then Is_Library_Level_Entity
(Ent
)
1828 Scop
:= Current_Scope
;
1830 if No
(Scop
) or else Scop
= Standard_Standard
then
1832 elsif Ekind
(Scop
) = E_Package
1833 and then Is_Compilation_Unit
(Scop
)
1837 Scop
:= Scope
(Scop
);
1841 -- Here Scop points to the containing library package
1844 Pkg_Body
:= Body_Entity
(Pkg_Spec
);
1846 -- All OK if the package has an Elaborate_Body pragma
1848 if Has_Pragma_Elaborate_Body
(Scop
) then
1852 -- OK if entity being modified is not in containing package spec
1854 if not In_Same_Source_Unit
(Scop
, Ent
) then
1858 -- All OK if entity appears in generic package or generic instance.
1859 -- We just get too messed up trying to give proper warnings in the
1860 -- presence of generics. Better no message than a junk one.
1862 Scop
:= Scope
(Ent
);
1863 while Present
(Scop
) and then Scop
/= Pkg_Spec
loop
1864 if Ekind
(Scop
) = E_Generic_Package
then
1866 elsif Ekind
(Scop
) = E_Package
1867 and then Is_Generic_Instance
(Scop
)
1872 Scop
:= Scope
(Scop
);
1875 -- All OK if in task, don't issue warnings there
1877 if In_Task_Activation
then
1881 -- OK if no package body
1883 if No
(Pkg_Body
) then
1887 -- OK if reference is not in package body
1889 if not In_Same_Source_Unit
(Pkg_Body
, N
) then
1893 -- OK if package body has no handled statement sequence
1896 HSS
: constant Node_Id
:=
1897 Handled_Statement_Sequence
(Declaration_Node
(Pkg_Body
));
1899 if No
(HSS
) or else not Comes_From_Source
(HSS
) then
1904 -- We definitely have a case of a modification of an entity in
1905 -- the package spec from the elaboration code of the package body.
1906 -- We may not give the warning (because there are some additional
1907 -- checks to avoid too many false positives), but it would be a good
1908 -- idea for the binder to try to keep the body elaboration close to
1909 -- the spec elaboration.
1911 Set_Elaborate_Body_Desirable
(Pkg_Spec
);
1913 -- All OK in gnat mode (we know what we are doing)
1919 -- All OK if all warnings suppressed
1921 if Warning_Mode
= Suppress
then
1925 -- All OK if elaboration checks suppressed for entity
1927 if Checks_May_Be_Suppressed
(Ent
)
1928 and then Is_Check_Suppressed
(Ent
, Elaboration_Check
)
1933 -- OK if the entity is initialized. Note that the No_Initialization
1934 -- flag usually means that the initialization has been rewritten into
1935 -- assignments, but that still counts for us.
1938 Decl
: constant Node_Id
:= Declaration_Node
(Ent
);
1940 if Nkind
(Decl
) = N_Object_Declaration
1941 and then (Present
(Expression
(Decl
))
1942 or else No_Initialization
(Decl
))
1948 -- Here is where we give the warning
1950 -- All OK if warnings suppressed on the entity
1952 if not Has_Warnings_Off
(Ent
) then
1953 Error_Msg_Sloc
:= Sloc
(Ent
);
1956 ("??& can be accessed by clients before this initialization",
1959 ("\??add Elaborate_Body to spec to ensure & is initialized",
1963 if not All_Errors_Mode
then
1964 Set_Suppress_Elaboration_Warnings
(Ent
);
1967 end Check_Elab_Assign
;
1969 ----------------------
1970 -- Check_Elab_Calls --
1971 ----------------------
1973 -- WARNING: This routine manages SPARK regions
1975 procedure Check_Elab_Calls
is
1976 Saved_SM
: SPARK_Mode_Type
;
1977 Saved_SMP
: Node_Id
;
1980 -- If expansion is disabled, do not generate any checks, unless we
1981 -- are in GNATprove mode, so that errors are issued in GNATprove for
1982 -- violations of static elaboration rules in SPARK code. Also skip
1983 -- checks if any subunits are missing because in either case we lack the
1984 -- full information that we need, and no object file will be created in
1987 if (not Expander_Active
and not GNATprove_Mode
)
1988 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
1989 or else Subunits_Missing
1994 -- Skip delayed calls if we had any errors
1996 if Serious_Errors_Detected
= 0 then
1997 Delaying_Elab_Checks
:= False;
1998 Expander_Mode_Save_And_Set
(True);
2000 for J
in Delay_Check
.First
.. Delay_Check
.Last
loop
2001 Push_Scope
(Delay_Check
.Table
(J
).Curscop
);
2002 From_Elab_Code
:= Delay_Check
.Table
(J
).From_Elab_Code
;
2003 In_Task_Activation
:= Delay_Check
.Table
(J
).In_Task_Activation
;
2005 Saved_SM
:= SPARK_Mode
;
2006 Saved_SMP
:= SPARK_Mode_Pragma
;
2008 -- Set appropriate value of SPARK_Mode
2010 if Delay_Check
.Table
(J
).From_SPARK_Code
then
2014 Check_Internal_Call_Continue
2015 (N
=> Delay_Check
.Table
(J
).N
,
2016 E
=> Delay_Check
.Table
(J
).E
,
2017 Outer_Scope
=> Delay_Check
.Table
(J
).Outer_Scope
,
2018 Orig_Ent
=> Delay_Check
.Table
(J
).Orig_Ent
);
2020 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
2024 -- Set Delaying_Elab_Checks back on for next main compilation
2026 Expander_Mode_Restore
;
2027 Delaying_Elab_Checks
:= True;
2029 end Check_Elab_Calls
;
2031 ------------------------------
2032 -- Check_Elab_Instantiation --
2033 ------------------------------
2035 procedure Check_Elab_Instantiation
2037 Outer_Scope
: Entity_Id
:= Empty
)
2042 -- Check for and deal with bad instantiation case. There is some
2043 -- duplicated code here, but we will worry about this later ???
2045 Check_Bad_Instantiation
(N
);
2047 if ABE_Is_Certain
(N
) then
2051 -- Nothing to do if we do not have an instantiation (happens in some
2052 -- error cases, and also in the formal package declaration case)
2054 if Nkind
(N
) not in N_Generic_Instantiation
then
2058 -- Nothing to do if inside a generic template
2060 if Inside_A_Generic
then
2064 -- Nothing to do if the instantiation is not in the main unit
2066 if not In_Extended_Main_Code_Unit
(N
) then
2070 Ent
:= Get_Generic_Entity
(N
);
2071 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
2073 -- See if we need to analyze this instantiation. We analyze it if
2074 -- either of the following conditions is met:
2076 -- It is an inner level instantiation (since in this case it was
2077 -- triggered by an outer level call from elaboration code), but
2078 -- only if the instantiation is within the scope of the original
2079 -- outer level call.
2081 -- It is an outer level instantiation from elaboration code, or the
2082 -- instantiated entity is in the same elaboration scope.
2084 -- And in these cases, we will check both the inter-unit case and
2085 -- the intra-unit (within a single unit) case.
2087 C_Scope
:= Current_Scope
;
2089 if Present
(Outer_Scope
) and then Within
(Scope
(Ent
), Outer_Scope
) then
2091 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
2093 elsif From_Elab_Code
then
2095 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
2097 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
2099 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
2101 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2102 -- set, then we will do the check, but only in the inter-unit case (this
2103 -- is to accommodate unguarded elaboration calls from other units in
2104 -- which this same mode is set). We inhibit warnings in this case, since
2105 -- this instantiation is not occurring in elaboration code.
2107 elsif Dynamic_Elaboration_Checks
then
2113 Inter_Unit_Only
=> True,
2114 Generate_Warnings
=> False);
2119 end Check_Elab_Instantiation
;
2121 -------------------------
2122 -- Check_Internal_Call --
2123 -------------------------
2125 procedure Check_Internal_Call
2128 Outer_Scope
: Entity_Id
;
2129 Orig_Ent
: Entity_Id
)
2131 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean;
2132 -- Determine whether call Call occurs within pragma Initial_Condition or
2133 -- pragma Check with check_kind set to Initial_Condition.
2135 ------------------------------
2136 -- Within_Initial_Condition --
2137 ------------------------------
2139 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean is
2145 -- Traverse the parent chain looking for an enclosing pragma
2148 while Present
(Par
) loop
2149 if Nkind
(Par
) = N_Pragma
then
2150 Nam
:= Pragma_Name
(Par
);
2152 -- Pragma Initial_Condition appears in its alternative from as
2153 -- Check (Initial_Condition, ...).
2155 if Nam
= Name_Check
then
2156 Args
:= Pragma_Argument_Associations
(Par
);
2158 -- Pragma Check should have at least two arguments
2160 pragma Assert
(Present
(Args
));
2163 Chars
(Expression
(First
(Args
))) = Name_Initial_Condition
;
2167 elsif Nam
= Name_Initial_Condition
then
2170 -- Since pragmas are never nested within other pragmas, stop
2177 -- Prevent the search from going too far
2179 elsif Is_Body_Or_Package_Declaration
(Par
) then
2183 Par
:= Parent
(Par
);
2185 -- If assertions are not enabled, the check pragma is rewritten
2186 -- as an if_statement in sem_prag, to generate various warnings
2187 -- on boolean expressions. Retrieve the original pragma.
2189 if Nkind
(Original_Node
(Par
)) = N_Pragma
then
2190 Par
:= Original_Node
(Par
);
2195 end Within_Initial_Condition
;
2199 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
2201 -- Start of processing for Check_Internal_Call
2204 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2205 -- node comes from source.
2207 if Nkind
(N
) = N_Attribute_Reference
2208 and then ((not Warn_On_Elab_Access
and then not Debug_Flag_Dot_O
)
2209 or else not Comes_From_Source
(N
))
2213 -- If not function or procedure call, instantiation, or 'Access, then
2214 -- ignore call (this happens in some error cases and rewriting cases).
2216 elsif not Nkind_In
(N
, N_Attribute_Reference
,
2218 N_Procedure_Call_Statement
)
2219 and then not Inst_Case
2223 -- Nothing to do if this is a call or instantiation that has already
2224 -- been found to be a sure ABE.
2226 elsif Nkind
(N
) /= N_Attribute_Reference
and then ABE_Is_Certain
(N
) then
2229 -- Nothing to do if errors already detected (avoid cascaded errors)
2231 elsif Serious_Errors_Detected
/= 0 then
2234 -- Nothing to do if not in full analysis mode
2236 elsif not Full_Analysis
then
2239 -- Nothing to do if analyzing in special spec-expression mode, since the
2240 -- call is not actually being made at this time.
2242 elsif In_Spec_Expression
then
2245 -- Nothing to do for call to intrinsic subprogram
2247 elsif Is_Intrinsic_Subprogram
(E
) then
2250 -- Nothing to do if call is within a generic unit
2252 elsif Inside_A_Generic
then
2255 -- Nothing to do when the call appears within pragma Initial_Condition.
2256 -- The pragma is part of the elaboration statements of a package body
2257 -- and may only call external subprograms or subprograms whose body is
2258 -- already available.
2260 elsif Within_Initial_Condition
(N
) then
2264 -- Delay this call if we are still delaying calls
2266 if Delaying_Elab_Checks
then
2270 Orig_Ent
=> Orig_Ent
,
2271 Curscop
=> Current_Scope
,
2272 Outer_Scope
=> Outer_Scope
,
2273 From_Elab_Code
=> From_Elab_Code
,
2274 In_Task_Activation
=> In_Task_Activation
,
2275 From_SPARK_Code
=> SPARK_Mode
= On
));
2278 -- Otherwise, call phase 2 continuation right now
2281 Check_Internal_Call_Continue
(N
, E
, Outer_Scope
, Orig_Ent
);
2283 end Check_Internal_Call
;
2285 ----------------------------------
2286 -- Check_Internal_Call_Continue --
2287 ----------------------------------
2289 procedure Check_Internal_Call_Continue
2292 Outer_Scope
: Entity_Id
;
2293 Orig_Ent
: Entity_Id
)
2295 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
;
2296 -- Function applied to each node as we traverse the body. Checks for
2297 -- call or entity reference that needs checking, and if so checks it.
2298 -- Always returns OK, so entire tree is traversed, except that as
2299 -- described below subprogram bodies are skipped for now.
2301 procedure Traverse
is new Atree
.Traverse_Proc
(Find_Elab_Reference
);
2302 -- Traverse procedure using above Find_Elab_Reference function
2304 -------------------------
2305 -- Find_Elab_Reference --
2306 -------------------------
2308 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
is
2312 -- If user has specified that there are no entry calls in elaboration
2313 -- code, do not trace past an accept statement, because the rendez-
2314 -- vous will happen after elaboration.
2316 if Nkind_In
(Original_Node
(N
), N_Accept_Statement
,
2318 and then Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
2322 -- If we have a function call, check it
2324 elsif Nkind
(N
) = N_Function_Call
then
2325 Check_Elab_Call
(N
, Outer_Scope
);
2328 -- If we have a procedure call, check the call, and also check
2329 -- arguments that are assignments (OUT or IN OUT mode formals).
2331 elsif Nkind
(N
) = N_Procedure_Call_Statement
then
2332 Check_Elab_Call
(N
, Outer_Scope
, In_Init_Proc
=> Is_Init_Proc
(E
));
2334 Actual
:= First_Actual
(N
);
2335 while Present
(Actual
) loop
2336 if Known_To_Be_Assigned
(Actual
) then
2337 Check_Elab_Assign
(Actual
);
2340 Next_Actual
(Actual
);
2345 -- If we have an access attribute for a subprogram, check it.
2346 -- Suppress this behavior under debug flag.
2348 elsif not Debug_Flag_Dot_UU
2349 and then Nkind
(N
) = N_Attribute_Reference
2350 and then Nam_In
(Attribute_Name
(N
), Name_Access
,
2351 Name_Unrestricted_Access
)
2352 and then Is_Entity_Name
(Prefix
(N
))
2353 and then Is_Subprogram
(Entity
(Prefix
(N
)))
2355 Check_Elab_Call
(N
, Outer_Scope
);
2358 -- In SPARK mode, if we have an entity reference to a variable, then
2359 -- check it. For now we consider any reference.
2361 elsif SPARK_Mode
= On
2362 and then Nkind
(N
) in N_Has_Entity
2363 and then Present
(Entity
(N
))
2364 and then Ekind
(Entity
(N
)) = E_Variable
2366 Check_Elab_Call
(N
, Outer_Scope
);
2369 -- If we have a generic instantiation, check it
2371 elsif Nkind
(N
) in N_Generic_Instantiation
then
2372 Check_Elab_Instantiation
(N
, Outer_Scope
);
2375 -- Skip subprogram bodies that come from source (wait for call to
2376 -- analyze these). The reason for the come from source test is to
2377 -- avoid catching task bodies.
2379 -- For task bodies, we should really avoid these too, waiting for the
2380 -- task activation, but that's too much trouble to catch for now, so
2381 -- we go in unconditionally. This is not so terrible, it means the
2382 -- error backtrace is not quite complete, and we are too eager to
2383 -- scan bodies of tasks that are unused, but this is hardly very
2386 elsif Nkind
(N
) = N_Subprogram_Body
2387 and then Comes_From_Source
(N
)
2391 elsif Nkind
(N
) = N_Assignment_Statement
2392 and then Comes_From_Source
(N
)
2394 Check_Elab_Assign
(Name
(N
));
2400 end Find_Elab_Reference
;
2402 Inst_Case
: constant Boolean := Is_Generic_Unit
(E
);
2403 Loc
: constant Source_Ptr
:= Sloc
(N
);
2408 -- Start of processing for Check_Internal_Call_Continue
2411 -- Save outer level call if at outer level
2413 if Elab_Call
.Last
= 0 then
2414 Outer_Level_Sloc
:= Loc
;
2417 -- If the call is to a function that renames a literal, no check needed
2419 if Ekind
(E
) = E_Enumeration_Literal
then
2423 -- Register the subprogram as examined within this particular context.
2424 -- This ensures that calls to the same subprogram but in different
2425 -- contexts receive warnings and checks of their own since the calls
2426 -- may be reached through different flow paths.
2428 Elab_Visited
.Append
((Subp_Id
=> E
, Context
=> Parent
(N
)));
2430 Sbody
:= Unit_Declaration_Node
(E
);
2432 if not Nkind_In
(Sbody
, N_Subprogram_Body
, N_Package_Body
) then
2433 Ebody
:= Corresponding_Body
(Sbody
);
2438 Sbody
:= Unit_Declaration_Node
(Ebody
);
2442 -- If the body appears after the outer level call or instantiation then
2443 -- we have an error case handled below.
2445 if Earlier_In_Extended_Unit
(Outer_Level_Sloc
, Sloc
(Sbody
))
2446 and then not In_Task_Activation
2450 -- If we have the instantiation case we are done, since we now know that
2451 -- the body of the generic appeared earlier.
2453 elsif Inst_Case
then
2456 -- Otherwise we have a call, so we trace through the called body to see
2457 -- if it has any problems.
2460 pragma Assert
(Nkind
(Sbody
) = N_Subprogram_Body
);
2462 Elab_Call
.Append
((Cloc
=> Loc
, Ent
=> E
));
2464 if Debug_Flag_LL
then
2465 Write_Str
("Elab_Call.Last = ");
2466 Write_Int
(Int
(Elab_Call
.Last
));
2467 Write_Str
(" Ent = ");
2468 Write_Name
(Chars
(E
));
2470 Write_Location
(Sloc
(N
));
2474 -- Now traverse declarations and statements of subprogram body. Note
2475 -- that we cannot simply Traverse (Sbody), since traverse does not
2476 -- normally visit subprogram bodies.
2481 Decl
:= First
(Declarations
(Sbody
));
2482 while Present
(Decl
) loop
2488 Traverse
(Handled_Statement_Sequence
(Sbody
));
2490 Elab_Call
.Decrement_Last
;
2494 -- Here is the case of calling a subprogram where the body has not yet
2495 -- been encountered. A warning message is needed, except if this is the
2496 -- case of appearing within an aspect specification that results in
2497 -- a check call, we do not really have such a situation, so no warning
2498 -- is needed (e.g. the case of a precondition, where the call appears
2499 -- textually before the body, but in actual fact is moved to the
2500 -- appropriate subprogram body and so does not need a check).
2509 -- Keep looking at parents if we are still in the subexpression
2511 if Nkind
(P
) in N_Subexpr
then
2514 -- Here P is the parent of the expression, check for special case
2517 O
:= Original_Node
(P
);
2519 -- Definitely not the special case if orig node is not a pragma
2521 exit when Nkind
(O
) /= N_Pragma
;
2523 -- Check we have an If statement or a null statement (happens
2524 -- when the If has been expanded to be True).
2526 exit when not Nkind_In
(P
, N_If_Statement
, N_Null_Statement
);
2528 -- Our special case will be indicated either by the pragma
2529 -- coming from an aspect ...
2531 if Present
(Corresponding_Aspect
(O
)) then
2534 -- Or, in the case of an initial condition, specifically by a
2535 -- Check pragma specifying an Initial_Condition check.
2537 elsif Pragma_Name
(O
) = Name_Check
2540 (Expression
(First
(Pragma_Argument_Associations
(O
)))) =
2541 Name_Initial_Condition
2545 -- For anything else, we have an error
2554 -- Not that special case, warning and dynamic check is required
2556 -- If we have nothing in the call stack, then this is at the outer
2557 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2560 if Elab_Call
.Last
= 0 then
2561 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2564 Insert_Check
: Boolean := True;
2565 -- This flag is set to True if an elaboration check should be
2569 if In_Task_Activation
then
2570 Insert_Check
:= False;
2572 elsif Inst_Case
then
2574 ("cannot instantiate& before body seen<<", N
, Orig_Ent
);
2576 elsif Nkind
(N
) = N_Attribute_Reference
then
2578 ("Access attribute of & before body seen<<", N
, Orig_Ent
);
2579 Error_Msg_N
("\possible Program_Error on later references<", N
);
2580 Insert_Check
:= False;
2582 elsif Nkind
(Unit_Declaration_Node
(Orig_Ent
)) /=
2583 N_Subprogram_Renaming_Declaration
2586 ("cannot call& before body seen<<", N
, Orig_Ent
);
2588 elsif not Is_Generic_Actual_Subprogram
(Orig_Ent
) then
2589 Insert_Check
:= False;
2592 if Insert_Check
then
2593 Error_Msg_N
("\Program_Error [<<", N
);
2594 Insert_Elab_Check
(N
);
2598 -- Call is not at outer level
2601 -- Do not generate elaboration checks in GNATprove mode because the
2602 -- elaboration counter and the check are both forms of expansion.
2604 if GNATprove_Mode
then
2607 -- Generate an elaboration check
2609 elsif not Elaboration_Checks_Suppressed
(E
) then
2610 Set_Elaboration_Entity_Required
(E
);
2612 -- Create a declaration of the elaboration entity, and insert it
2613 -- prior to the subprogram or the generic unit, within the same
2614 -- scope. Since the subprogram may be overloaded, create a unique
2617 if No
(Elaboration_Entity
(E
)) then
2619 Loce
: constant Source_Ptr
:= Sloc
(E
);
2620 Ent
: constant Entity_Id
:=
2621 Make_Defining_Identifier
(Loc
,
2622 New_External_Name
(Chars
(E
), 'E', -1));
2625 Set_Elaboration_Entity
(E
, Ent
);
2626 Push_Scope
(Scope
(E
));
2628 Insert_Action
(Declaration_Node
(E
),
2629 Make_Object_Declaration
(Loce
,
2630 Defining_Identifier
=> Ent
,
2631 Object_Definition
=>
2632 New_Occurrence_Of
(Standard_Short_Integer
, Loce
),
2634 Make_Integer_Literal
(Loc
, Uint_0
)));
2636 -- Set elaboration flag at the point of the body
2638 Set_Elaboration_Flag
(Sbody
, E
);
2640 -- Kill current value indication. This is necessary because
2641 -- the tests of this flag are inserted out of sequence and
2642 -- must not pick up bogus indications of the wrong constant
2643 -- value. Also, this is never a true constant, since one way
2644 -- or another, it gets reset.
2646 Set_Current_Value
(Ent
, Empty
);
2647 Set_Last_Assignment
(Ent
, Empty
);
2648 Set_Is_True_Constant
(Ent
, False);
2655 -- raise Program_Error with "access before elaboration";
2658 Insert_Elab_Check
(N
,
2659 Make_Attribute_Reference
(Loc
,
2660 Attribute_Name
=> Name_Elaborated
,
2661 Prefix
=> New_Occurrence_Of
(E
, Loc
)));
2664 -- Generate the warning
2666 if not Suppress_Elaboration_Warnings
(E
)
2667 and then not Elaboration_Checks_Suppressed
(E
)
2669 -- Suppress this warning if we have a function call that occurred
2670 -- within an assertion expression, since we can get false warnings
2671 -- in this case, due to the out of order handling in this case.
2674 (Nkind
(Original_Node
(N
)) /= N_Function_Call
2675 or else not In_Assertion_Expression_Pragma
(Original_Node
(N
)))
2677 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2681 ("instantiation of& may occur before body is seen<l<",
2684 -- A rather specific check. For Finalize/Adjust/Initialize, if
2685 -- the type has Warnings_Off set, suppress the warning.
2687 if Nam_In
(Chars
(E
), Name_Adjust
,
2690 and then Present
(First_Formal
(E
))
2693 T
: constant Entity_Id
:= Etype
(First_Formal
(E
));
2695 if Is_Controlled
(T
) then
2697 or else (Ekind
(T
) = E_Private_Type
2698 and then Warnings_Off
(Full_View
(T
)))
2706 -- Go ahead and give warning if not this special case
2709 ("call to& may occur before body is seen<l<", N
, Orig_Ent
);
2712 Error_Msg_N
("\Program_Error ]<l<", N
);
2714 -- There is no need to query the elaboration warning message flags
2715 -- because the main message is an error, not a warning, therefore
2716 -- all the clarification messages produces by Output_Calls must be
2717 -- emitted unconditionally.
2721 Output_Calls
(N
, Check_Elab_Flag
=> False);
2724 end Check_Internal_Call_Continue
;
2726 ---------------------------
2727 -- Check_Task_Activation --
2728 ---------------------------
2730 procedure Check_Task_Activation
(N
: Node_Id
) is
2731 Loc
: constant Source_Ptr
:= Sloc
(N
);
2732 Inter_Procs
: constant Elist_Id
:= New_Elmt_List
;
2733 Intra_Procs
: constant Elist_Id
:= New_Elmt_List
;
2736 Task_Scope
: Entity_Id
;
2737 Cunit_SC
: Boolean := False;
2740 Enclosing
: Entity_Id
;
2742 procedure Add_Task_Proc
(Typ
: Entity_Id
);
2743 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2744 -- For record types, this procedure recurses over component types.
2746 procedure Collect_Tasks
(Decls
: List_Id
);
2747 -- Collect the types of the tasks that are to be activated in the given
2748 -- list of declarations, in order to perform elaboration checks on the
2749 -- corresponding task procedures that are called implicitly here.
2751 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
;
2752 -- find enclosing compilation unit of Entity, ignoring subunits, or
2753 -- else enclosing subprogram. If E is not a package, there is no need
2754 -- for inter-unit elaboration checks.
2760 procedure Add_Task_Proc
(Typ
: Entity_Id
) is
2762 Proc
: Entity_Id
:= Empty
;
2765 if Is_Task_Type
(Typ
) then
2766 Proc
:= Get_Task_Body_Procedure
(Typ
);
2768 elsif Is_Array_Type
(Typ
)
2769 and then Has_Task
(Base_Type
(Typ
))
2771 Add_Task_Proc
(Component_Type
(Typ
));
2773 elsif Is_Record_Type
(Typ
)
2774 and then Has_Task
(Base_Type
(Typ
))
2776 Comp
:= First_Component
(Typ
);
2777 while Present
(Comp
) loop
2778 Add_Task_Proc
(Etype
(Comp
));
2779 Comp
:= Next_Component
(Comp
);
2783 -- If the task type is another unit, we will perform the usual
2784 -- elaboration check on its enclosing unit. If the type is in the
2785 -- same unit, we can trace the task body as for an internal call,
2786 -- but we only need to examine other external calls, because at
2787 -- the point the task is activated, internal subprogram bodies
2788 -- will have been elaborated already. We keep separate lists for
2789 -- each kind of task.
2791 -- Skip this test if errors have occurred, since in this case
2792 -- we can get false indications.
2794 if Serious_Errors_Detected
/= 0 then
2798 if Present
(Proc
) then
2799 if Outer_Unit
(Scope
(Proc
)) = Enclosing
then
2801 if No
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2803 (not Is_Generic_Instance
(Scope
(Proc
))
2804 or else Scope
(Proc
) = Scope
(Defining_Identifier
(Decl
)))
2806 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2808 ("task will be activated before elaboration of its body<<",
2810 Error_Msg_N
("\Program_Error [<<", Decl
);
2813 (Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2815 Append_Elmt
(Proc
, Intra_Procs
);
2819 -- No need for multiple entries of the same type
2821 Elmt
:= First_Elmt
(Inter_Procs
);
2822 while Present
(Elmt
) loop
2823 if Node
(Elmt
) = Proc
then
2830 Append_Elmt
(Proc
, Inter_Procs
);
2839 procedure Collect_Tasks
(Decls
: List_Id
) is
2841 if Present
(Decls
) then
2842 Decl
:= First
(Decls
);
2843 while Present
(Decl
) loop
2844 if Nkind
(Decl
) = N_Object_Declaration
2845 and then Has_Task
(Etype
(Defining_Identifier
(Decl
)))
2847 Add_Task_Proc
(Etype
(Defining_Identifier
(Decl
)));
2859 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
is
2864 while Present
(Outer
) loop
2865 if Elaboration_Checks_Suppressed
(Outer
) then
2869 exit when Is_Child_Unit
(Outer
)
2870 or else Scope
(Outer
) = Standard_Standard
2871 or else Ekind
(Outer
) /= E_Package
;
2872 Outer
:= Scope
(Outer
);
2878 -- Start of processing for Check_Task_Activation
2881 Enclosing
:= Outer_Unit
(Current_Scope
);
2883 -- Find all tasks declared in the current unit
2885 if Nkind
(N
) = N_Package_Body
then
2886 P
:= Unit_Declaration_Node
(Corresponding_Spec
(N
));
2888 Collect_Tasks
(Declarations
(N
));
2889 Collect_Tasks
(Visible_Declarations
(Specification
(P
)));
2890 Collect_Tasks
(Private_Declarations
(Specification
(P
)));
2892 elsif Nkind
(N
) = N_Package_Declaration
then
2893 Collect_Tasks
(Visible_Declarations
(Specification
(N
)));
2894 Collect_Tasks
(Private_Declarations
(Specification
(N
)));
2897 Collect_Tasks
(Declarations
(N
));
2900 -- We only perform detailed checks in all tasks that are library level
2901 -- entities. If the master is a subprogram or task, activation will
2902 -- depend on the activation of the master itself.
2904 -- Should dynamic checks be added in the more general case???
2906 if Ekind
(Enclosing
) /= E_Package
then
2910 -- For task types defined in other units, we want the unit containing
2911 -- the task body to be elaborated before the current one.
2913 Elmt
:= First_Elmt
(Inter_Procs
);
2914 while Present
(Elmt
) loop
2916 Task_Scope
:= Outer_Unit
(Scope
(Ent
));
2918 if not Is_Compilation_Unit
(Task_Scope
) then
2921 elsif Suppress_Elaboration_Warnings
(Task_Scope
)
2922 or else Elaboration_Checks_Suppressed
(Task_Scope
)
2926 elsif Dynamic_Elaboration_Checks
then
2927 if not Elaboration_Checks_Suppressed
(Ent
)
2928 and then not Cunit_SC
2929 and then not Restriction_Active
2930 (No_Entry_Calls_In_Elaboration_Code
)
2932 -- Runtime elaboration check required. Generate check of the
2933 -- elaboration counter for the unit containing the entity.
2935 Insert_Elab_Check
(N
,
2936 Make_Attribute_Reference
(Loc
,
2938 New_Occurrence_Of
(Spec_Entity
(Task_Scope
), Loc
),
2939 Attribute_Name
=> Name_Elaborated
));
2943 -- Force the binder to elaborate other unit first
2945 if Elab_Info_Messages
2946 and then not Suppress_Elaboration_Warnings
(Ent
)
2947 and then not Elaboration_Checks_Suppressed
(Ent
)
2948 and then not Suppress_Elaboration_Warnings
(Task_Scope
)
2949 and then not Elaboration_Checks_Suppressed
(Task_Scope
)
2951 Error_Msg_Node_2
:= Task_Scope
;
2953 ("info: activation of an instance of task type & requires "
2954 & "pragma Elaborate_All on &?$?", N
, Ent
);
2957 Activate_Elaborate_All_Desirable
(N
, Task_Scope
);
2958 Set_Suppress_Elaboration_Warnings
(Task_Scope
);
2964 -- For tasks declared in the current unit, trace other calls within
2965 -- the task procedure bodies, which are available.
2967 In_Task_Activation
:= True;
2969 Elmt
:= First_Elmt
(Intra_Procs
);
2970 while Present
(Elmt
) loop
2972 Check_Internal_Call_Continue
(N
, Ent
, Enclosing
, Ent
);
2976 In_Task_Activation
:= False;
2977 end Check_Task_Activation
;
2979 -------------------------------
2980 -- Is_Call_Of_Generic_Formal --
2981 -------------------------------
2983 function Is_Call_Of_Generic_Formal
(N
: Node_Id
) return Boolean is
2985 return Nkind_In
(N
, N_Function_Call
, N_Procedure_Call_Statement
)
2987 -- Always return False if debug flag -gnatd.G is set
2989 and then not Debug_Flag_Dot_GG
2991 -- For now, we detect this by looking for the strange identifier
2992 -- node, whose Chars reflect the name of the generic formal, but
2993 -- the Chars of the Entity references the generic actual.
2995 and then Nkind
(Name
(N
)) = N_Identifier
2996 and then Chars
(Name
(N
)) /= Chars
(Entity
(Name
(N
)));
2997 end Is_Call_Of_Generic_Formal
;
2999 --------------------------------
3000 -- Set_Elaboration_Constraint --
3001 --------------------------------
3003 procedure Set_Elaboration_Constraint
3008 Elab_Unit
: Entity_Id
;
3010 -- Check whether this is a call to an Initialize subprogram for a
3011 -- controlled type. Note that Call can also be a 'Access attribute
3012 -- reference, which now generates an elaboration check.
3014 Init_Call
: constant Boolean :=
3015 Nkind
(Call
) = N_Procedure_Call_Statement
3016 and then Chars
(Subp
) = Name_Initialize
3017 and then Comes_From_Source
(Subp
)
3018 and then Present
(Parameter_Associations
(Call
))
3019 and then Is_Controlled
(Etype
(First_Actual
(Call
)));
3022 -- If the unit is mentioned in a with_clause of the current unit, it is
3023 -- visible, and we can set the elaboration flag.
3025 if Is_Immediately_Visible
(Scop
)
3026 or else (Is_Child_Unit
(Scop
) and then Is_Visible_Lib_Unit
(Scop
))
3028 Activate_Elaborate_All_Desirable
(Call
, Scop
);
3029 Set_Suppress_Elaboration_Warnings
(Scop
);
3033 -- If this is not an initialization call or a call using object notation
3034 -- we know that the unit of the called entity is in the context, and we
3035 -- can set the flag as well. The unit need not be visible if the call
3036 -- occurs within an instantiation.
3038 if Is_Init_Proc
(Subp
)
3040 or else Nkind
(Original_Node
(Call
)) = N_Selected_Component
3042 null; -- detailed processing follows.
3045 Activate_Elaborate_All_Desirable
(Call
, Scop
);
3046 Set_Suppress_Elaboration_Warnings
(Scop
);
3050 -- If the unit is not in the context, there must be an intermediate unit
3051 -- that is, on which we need to place to elaboration flag. This happens
3052 -- with init proc calls.
3054 if Is_Init_Proc
(Subp
) or else Init_Call
then
3056 -- The initialization call is on an object whose type is not declared
3057 -- in the same scope as the subprogram. The type of the object must
3058 -- be a subtype of the type of operation. This object is the first
3059 -- actual in the call.
3062 Typ
: constant Entity_Id
:=
3063 Etype
(First
(Parameter_Associations
(Call
)));
3065 Elab_Unit
:= Scope
(Typ
);
3066 while (Present
(Elab_Unit
))
3067 and then not Is_Compilation_Unit
(Elab_Unit
)
3069 Elab_Unit
:= Scope
(Elab_Unit
);
3073 -- If original node uses selected component notation, the prefix is
3074 -- visible and determines the scope that must be elaborated. After
3075 -- rewriting, the prefix is the first actual in the call.
3077 elsif Nkind
(Original_Node
(Call
)) = N_Selected_Component
then
3078 Elab_Unit
:= Scope
(Etype
(First
(Parameter_Associations
(Call
))));
3080 -- Not one of special cases above
3083 -- Using previously computed scope. If the elaboration check is
3084 -- done after analysis, the scope is not visible any longer, but
3085 -- must still be in the context.
3090 Activate_Elaborate_All_Desirable
(Call
, Elab_Unit
);
3091 Set_Suppress_Elaboration_Warnings
(Elab_Unit
);
3092 end Set_Elaboration_Constraint
;
3094 ------------------------
3095 -- Get_Referenced_Ent --
3096 ------------------------
3098 function Get_Referenced_Ent
(N
: Node_Id
) return Entity_Id
is
3102 if Nkind
(N
) in N_Has_Entity
3103 and then Present
(Entity
(N
))
3104 and then Ekind
(Entity
(N
)) = E_Variable
3109 if Nkind
(N
) = N_Attribute_Reference
then
3117 elsif Nkind
(Nam
) = N_Selected_Component
then
3118 return Entity
(Selector_Name
(Nam
));
3119 elsif not Is_Entity_Name
(Nam
) then
3122 return Entity
(Nam
);
3124 end Get_Referenced_Ent
;
3126 ----------------------
3127 -- Has_Generic_Body --
3128 ----------------------
3130 function Has_Generic_Body
(N
: Node_Id
) return Boolean is
3131 Ent
: constant Entity_Id
:= Get_Generic_Entity
(N
);
3132 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
3135 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
;
3136 -- Determine if the list of nodes headed by N and linked by Next
3137 -- contains a package body for the package spec entity E, and if so
3138 -- return the package body. If not, then returns Empty.
3140 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
;
3141 -- This procedure is called load the unit whose name is given by Nam.
3142 -- This unit is being loaded to see whether it contains an optional
3143 -- generic body. The returned value is the loaded unit, which is always
3144 -- a package body (only package bodies can contain other entities in the
3145 -- sense in which Has_Generic_Body is interested). We only attempt to
3146 -- load bodies if we are generating code. If we are in semantics check
3147 -- only mode, then it would be wrong to load bodies that are not
3148 -- required from a semantic point of view, so in this case we return
3149 -- Empty. The result is that the caller may incorrectly decide that a
3150 -- generic spec does not have a body when in fact it does, but the only
3151 -- harm in this is that some warnings on elaboration problems may be
3152 -- lost in semantic checks only mode, which is not big loss. We also
3153 -- return Empty if we go for a body and it is not there.
3155 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
;
3156 -- PE is the entity for a package spec. This function locates the
3157 -- corresponding package body, returning Empty if none is found. The
3158 -- package body returned is fully parsed but may not yet be analyzed,
3159 -- so only syntactic fields should be referenced.
3165 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
is
3170 while Present
(Nod
) loop
3172 -- If we found the package body we are looking for, return it
3174 if Nkind
(Nod
) = N_Package_Body
3175 and then Chars
(Defining_Unit_Name
(Nod
)) = Chars
(E
)
3179 -- If we found the stub for the body, go after the subunit,
3180 -- loading it if necessary.
3182 elsif Nkind
(Nod
) = N_Package_Body_Stub
3183 and then Chars
(Defining_Identifier
(Nod
)) = Chars
(E
)
3185 if Present
(Library_Unit
(Nod
)) then
3186 return Unit
(Library_Unit
(Nod
));
3189 return Load_Package_Body
(Get_Unit_Name
(Nod
));
3192 -- If neither package body nor stub, keep looking on chain
3202 -----------------------
3203 -- Load_Package_Body --
3204 -----------------------
3206 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
is
3207 U
: Unit_Number_Type
;
3210 if Operating_Mode
/= Generate_Code
then
3223 return Unit
(Cunit
(U
));
3226 end Load_Package_Body
;
3228 -------------------------------
3229 -- Locate_Corresponding_Body --
3230 -------------------------------
3232 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
is
3233 Spec
: constant Node_Id
:= Declaration_Node
(PE
);
3234 Decl
: constant Node_Id
:= Parent
(Spec
);
3235 Scop
: constant Entity_Id
:= Scope
(PE
);
3239 if Is_Library_Level_Entity
(PE
) then
3241 -- If package is a library unit that requires a body, we have no
3242 -- choice but to go after that body because it might contain an
3243 -- optional body for the original generic package.
3245 if Unit_Requires_Body
(PE
) then
3247 -- Load the body. Note that we are a little careful here to use
3248 -- Spec to get the unit number, rather than PE or Decl, since
3249 -- in the case where the package is itself a library level
3250 -- instantiation, Spec will properly reference the generic
3251 -- template, which is what we really want.
3255 (Get_Body_Name
(Unit_Name
(Get_Source_Unit
(Spec
))));
3257 -- But if the package is a library unit that does NOT require
3258 -- a body, then no body is permitted, so we are sure that there
3259 -- is no body for the original generic package.
3265 -- Otherwise look and see if we are embedded in a further package
3267 elsif Is_Package_Or_Generic_Package
(Scop
) then
3269 -- If so, get the body of the enclosing package, and look in
3270 -- its package body for the package body we are looking for.
3272 PBody
:= Locate_Corresponding_Body
(Scop
);
3277 return Find_Body_In
(PE
, First
(Declarations
(PBody
)));
3280 -- If we are not embedded in a further package, then the body
3281 -- must be in the same declarative part as we are.
3284 return Find_Body_In
(PE
, Next
(Decl
));
3286 end Locate_Corresponding_Body
;
3288 -- Start of processing for Has_Generic_Body
3291 if Present
(Corresponding_Body
(Decl
)) then
3294 elsif Unit_Requires_Body
(Ent
) then
3297 -- Compilation units cannot have optional bodies
3299 elsif Is_Compilation_Unit
(Ent
) then
3302 -- Otherwise look at what scope we are in
3305 Scop
:= Scope
(Ent
);
3307 -- Case of entity is in other than a package spec, in this case
3308 -- the body, if present, must be in the same declarative part.
3310 if not Is_Package_Or_Generic_Package
(Scop
) then
3315 -- Declaration node may get us a spec, so if so, go to
3316 -- the parent declaration.
3318 P
:= Declaration_Node
(Ent
);
3319 while not Is_List_Member
(P
) loop
3323 return Present
(Find_Body_In
(Ent
, Next
(P
)));
3326 -- If the entity is in a package spec, then we have to locate
3327 -- the corresponding package body, and look there.
3331 PBody
: constant Node_Id
:= Locate_Corresponding_Body
(Scop
);
3339 (Find_Body_In
(Ent
, (First
(Declarations
(PBody
)))));
3344 end Has_Generic_Body
;
3346 -----------------------
3347 -- Insert_Elab_Check --
3348 -----------------------
3350 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
) is
3352 Loc
: constant Source_Ptr
:= Sloc
(N
);
3355 -- The check (N_Raise_Program_Error) node to be inserted
3358 -- If expansion is disabled, do not generate any checks. Also
3359 -- skip checks if any subunits are missing because in either
3360 -- case we lack the full information that we need, and no object
3361 -- file will be created in any case.
3363 if not Expander_Active
or else Subunits_Missing
then
3367 -- If we have a generic instantiation, where Instance_Spec is set,
3368 -- then this field points to a generic instance spec that has
3369 -- been inserted before the instantiation node itself, so that
3370 -- is where we want to insert a check.
3372 if Nkind
(N
) in N_Generic_Instantiation
3373 and then Present
(Instance_Spec
(N
))
3375 Nod
:= Instance_Spec
(N
);
3380 -- Build check node, possibly with condition
3383 Make_Raise_Program_Error
(Loc
, Reason
=> PE_Access_Before_Elaboration
);
3386 Set_Condition
(Chk
, Make_Op_Not
(Loc
, Right_Opnd
=> C
));
3389 -- If we are inserting at the top level, insert in Aux_Decls
3391 if Nkind
(Parent
(Nod
)) = N_Compilation_Unit
then
3393 ADN
: constant Node_Id
:= Aux_Decls_Node
(Parent
(Nod
));
3396 if No
(Declarations
(ADN
)) then
3397 Set_Declarations
(ADN
, New_List
(Chk
));
3399 Append_To
(Declarations
(ADN
), Chk
);
3405 -- Otherwise just insert as an action on the node in question
3408 Insert_Action
(Nod
, Chk
);
3410 end Insert_Elab_Check
;
3412 -------------------------------
3413 -- Is_Finalization_Procedure --
3414 -------------------------------
3416 function Is_Finalization_Procedure
(Id
: Entity_Id
) return Boolean is
3418 -- Check whether Id is a procedure with at least one parameter
3420 if Ekind
(Id
) = E_Procedure
and then Present
(First_Formal
(Id
)) then
3422 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Id
));
3423 Deep_Fin
: Entity_Id
:= Empty
;
3424 Fin
: Entity_Id
:= Empty
;
3427 -- If the type of the first formal does not require finalization
3428 -- actions, then this is definitely not [Deep_]Finalize.
3430 if not Needs_Finalization
(Typ
) then
3434 -- At this point we have the following scenario:
3436 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3438 -- Recover the two possible versions of [Deep_]Finalize using the
3439 -- type of the first parameter and compare with the input.
3441 Deep_Fin
:= TSS
(Typ
, TSS_Deep_Finalize
);
3443 if Is_Controlled
(Typ
) then
3444 Fin
:= Find_Prim_Op
(Typ
, Name_Finalize
);
3447 return (Present
(Deep_Fin
) and then Id
= Deep_Fin
)
3448 or else (Present
(Fin
) and then Id
= Fin
);
3453 end Is_Finalization_Procedure
;
3459 procedure Output_Calls
3461 Check_Elab_Flag
: Boolean)
3463 function Emit
(Flag
: Boolean) return Boolean;
3464 -- Determine whether to emit an error message based on the combination
3465 -- of flags Check_Elab_Flag and Flag.
3467 function Is_Printable_Error_Name
return Boolean;
3468 -- An internal function, used to determine if a name, stored in the
3469 -- Name_Buffer, is either a non-internal name, or is an internal name
3470 -- that is printable by the error message circuits (i.e. it has a single
3471 -- upper case letter at the end).
3477 function Emit
(Flag
: Boolean) return Boolean is
3479 if Check_Elab_Flag
then
3486 -----------------------------
3487 -- Is_Printable_Error_Name --
3488 -----------------------------
3490 function Is_Printable_Error_Name
return Boolean is
3492 if not Is_Internal_Name
then
3495 elsif Name_Len
= 1 then
3499 Name_Len
:= Name_Len
- 1;
3500 return not Is_Internal_Name
;
3502 end Is_Printable_Error_Name
;
3508 -- Start of processing for Output_Calls
3511 for J
in reverse 1 .. Elab_Call
.Last
loop
3512 Error_Msg_Sloc
:= Elab_Call
.Table
(J
).Cloc
;
3514 Ent
:= Elab_Call
.Table
(J
).Ent
;
3515 Get_Name_String
(Chars
(Ent
));
3517 -- Dynamic elaboration model, warnings controlled by -gnatwl
3519 if Dynamic_Elaboration_Checks
then
3520 if Emit
(Elab_Warnings
) then
3521 if Is_Generic_Unit
(Ent
) then
3522 Error_Msg_NE
("\\?l?& instantiated #", N
, Ent
);
3523 elsif Is_Init_Proc
(Ent
) then
3524 Error_Msg_N
("\\?l?initialization procedure called #", N
);
3525 elsif Is_Printable_Error_Name
then
3526 Error_Msg_NE
("\\?l?& called #", N
, Ent
);
3528 Error_Msg_N
("\\?l?called #", N
);
3532 -- Static elaboration model, info messages controlled by -gnatel
3535 if Emit
(Elab_Info_Messages
) then
3536 if Is_Generic_Unit
(Ent
) then
3537 Error_Msg_NE
("\\?$?& instantiated #", N
, Ent
);
3538 elsif Is_Init_Proc
(Ent
) then
3539 Error_Msg_N
("\\?$?initialization procedure called #", N
);
3540 elsif Is_Printable_Error_Name
then
3541 Error_Msg_NE
("\\?$?& called #", N
, Ent
);
3543 Error_Msg_N
("\\?$?called #", N
);
3550 ----------------------------
3551 -- Same_Elaboration_Scope --
3552 ----------------------------
3554 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean is
3559 -- Find elaboration scope for Scop1
3560 -- This is either a subprogram or a compilation unit.
3563 while S1
/= Standard_Standard
3564 and then not Is_Compilation_Unit
(S1
)
3565 and then Ekind_In
(S1
, E_Package
, E_Protected_Type
, E_Block
)
3570 -- Find elaboration scope for Scop2
3573 while S2
/= Standard_Standard
3574 and then not Is_Compilation_Unit
(S2
)
3575 and then Ekind_In
(S2
, E_Package
, E_Protected_Type
, E_Block
)
3581 end Same_Elaboration_Scope
;
3587 procedure Set_C_Scope
is
3589 while not Is_Compilation_Unit
(C_Scope
) loop
3590 C_Scope
:= Scope
(C_Scope
);
3598 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
is
3602 -- Check for case of body entity
3603 -- Why is the check for E_Void needed???
3605 if Ekind_In
(E
, E_Void
, E_Subprogram_Body
, E_Package_Body
) then
3609 Decl
:= Parent
(Decl
);
3610 exit when Nkind
(Decl
) in N_Proper_Body
;
3613 return Corresponding_Spec
(Decl
);
3624 procedure Supply_Bodies
(N
: Node_Id
) is
3626 if Nkind
(N
) = N_Subprogram_Declaration
then
3628 Ent
: constant Entity_Id
:= Defining_Unit_Name
(Specification
(N
));
3631 -- Internal subprograms will already have a generated body, so
3632 -- there is no need to provide a stub for them.
3634 if No
(Corresponding_Body
(N
)) then
3636 Loc
: constant Source_Ptr
:= Sloc
(N
);
3637 Formals
: constant List_Id
:= Copy_Parameter_List
(Ent
);
3638 Nam
: constant Entity_Id
:=
3639 Make_Defining_Identifier
(Loc
, Chars
(Ent
));
3640 Stats
: constant List_Id
:=
3642 Make_Raise_Program_Error
(Loc
,
3643 Reason
=> PE_Access_Before_Elaboration
));
3647 if Ekind
(Ent
) = E_Function
then
3649 Make_Function_Specification
(Loc
,
3650 Defining_Unit_Name
=> Nam
,
3651 Parameter_Specifications
=> Formals
,
3652 Result_Definition
=>
3654 (Result_Definition
(Specification
(N
))));
3656 -- We cannot reliably make a return statement for this
3657 -- body, but none is needed because the call raises
3660 Set_Return_Present
(Ent
);
3664 Make_Procedure_Specification
(Loc
,
3665 Defining_Unit_Name
=> Nam
,
3666 Parameter_Specifications
=> Formals
);
3669 Insert_After_And_Analyze
(N
,
3670 Make_Subprogram_Body
(Loc
,
3671 Specification
=> Spec
,
3672 Declarations
=> New_List
,
3673 Handled_Statement_Sequence
=>
3674 Make_Handled_Sequence_Of_Statements
(Loc
, Stats
)));
3679 elsif Nkind
(N
) = N_Package_Declaration
then
3681 Spec
: constant Node_Id
:= Specification
(N
);
3683 Push_Scope
(Defining_Unit_Name
(Spec
));
3684 Supply_Bodies
(Visible_Declarations
(Spec
));
3685 Supply_Bodies
(Private_Declarations
(Spec
));
3691 procedure Supply_Bodies
(L
: List_Id
) is
3696 while Present
(Elmt
) loop
3697 Supply_Bodies
(Elmt
);
3707 function Within
(E1
, E2
: Entity_Id
) return Boolean is
3714 elsif Scop
= Standard_Standard
then
3717 Scop
:= Scope
(Scop
);
3722 --------------------------
3723 -- Within_Elaborate_All --
3724 --------------------------
3726 function Within_Elaborate_All
3727 (Unit
: Unit_Number_Type
;
3728 E
: Entity_Id
) return Boolean
3730 type Unit_Number_Set
is array (Main_Unit
.. Last_Unit
) of Boolean;
3731 pragma Pack
(Unit_Number_Set
);
3733 Seen
: Unit_Number_Set
:= (others => False);
3734 -- Seen (X) is True after we have seen unit X in the walk. This is used
3735 -- to prevent processing the same unit more than once.
3737 Result
: Boolean := False;
3739 procedure Helper
(Unit
: Unit_Number_Type
);
3740 -- This helper procedure does all the work for Within_Elaborate_All. It
3741 -- walks the dependency graph, and sets Result to True if it finds an
3742 -- appropriate Elaborate_All.
3748 procedure Helper
(Unit
: Unit_Number_Type
) is
3749 CU
: constant Node_Id
:= Cunit
(Unit
);
3753 Elab_Id
: Entity_Id
;
3760 Seen
(Unit
) := True;
3763 -- First, check for Elaborate_Alls on this unit
3765 Item
:= First
(Context_Items
(CU
));
3766 while Present
(Item
) loop
3767 if Nkind
(Item
) = N_Pragma
3768 and then Pragma_Name
(Item
) = Name_Elaborate_All
3770 -- Return if some previous error on the pragma itself. The
3771 -- pragma may be unanalyzed, because of a previous error, or
3772 -- if it is the context of a subunit, inherited by its parent.
3774 if Error_Posted
(Item
) or else not Analyzed
(Item
) then
3780 (Expression
(First
(Pragma_Argument_Associations
(Item
))));
3787 Par
:= Parent
(Unit_Declaration_Node
(Elab_Id
));
3789 Item2
:= First
(Context_Items
(Par
));
3790 while Present
(Item2
) loop
3791 if Nkind
(Item2
) = N_With_Clause
3792 and then Entity
(Name
(Item2
)) = E
3793 and then not Limited_Present
(Item2
)
3806 -- Second, recurse on with's. We could do this as part of the above
3807 -- loop, but it's probably more efficient to have two loops, because
3808 -- the relevant Elaborate_All is likely to be on the initial unit. In
3809 -- other words, we're walking the with's breadth-first. This part is
3810 -- only necessary in the dynamic elaboration model.
3812 if Dynamic_Elaboration_Checks
then
3813 Item
:= First
(Context_Items
(CU
));
3814 while Present
(Item
) loop
3815 if Nkind
(Item
) = N_With_Clause
3816 and then not Limited_Present
(Item
)
3818 -- Note: the following call to Get_Cunit_Unit_Number does a
3819 -- linear search, which could be slow, but it's OK because
3820 -- we're about to give a warning anyway. Also, there might
3821 -- be hundreds of units, but not millions. If it turns out
3822 -- to be a problem, we could store the Get_Cunit_Unit_Number
3823 -- in each N_Compilation_Unit node, but that would involve
3824 -- rearranging N_Compilation_Unit_Aux to make room.
3826 Helper
(Get_Cunit_Unit_Number
(Library_Unit
(Item
)));
3838 -- Start of processing for Within_Elaborate_All
3843 end Within_Elaborate_All
;