1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2016, 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
;
35 with Fname
; use Fname
;
37 with Lib
.Load
; use Lib
.Load
;
38 with Namet
; use Namet
;
39 with Nlists
; use Nlists
;
40 with Nmake
; use Nmake
;
42 with Output
; use Output
;
43 with Restrict
; use Restrict
;
44 with Rident
; use Rident
;
46 with Sem_Aux
; use Sem_Aux
;
47 with Sem_Cat
; use Sem_Cat
;
48 with Sem_Ch7
; use Sem_Ch7
;
49 with Sem_Ch8
; use Sem_Ch8
;
50 with Sem_Util
; use Sem_Util
;
51 with Sinfo
; use Sinfo
;
52 with Sinput
; use Sinput
;
53 with Snames
; use Snames
;
54 with Stand
; use Stand
;
56 with Tbuild
; use Tbuild
;
57 with Uintp
; use Uintp
;
58 with Uname
; use Uname
;
60 package body Sem_Elab
is
62 -- The following table records the recursive call chain for output in the
63 -- Output routine. Each entry records the call node and the entity of the
64 -- called routine. The number of entries in the table (i.e. the value of
65 -- Elab_Call.Last) indicates the current depth of recursion and is used to
66 -- identify the outer level.
68 type Elab_Call_Entry
is record
73 package Elab_Call
is new Table
.Table
(
74 Table_Component_Type
=> Elab_Call_Entry
,
75 Table_Index_Type
=> Int
,
78 Table_Increment
=> 100,
79 Table_Name
=> "Elab_Call");
81 -- This table is initialized at the start of each outer level call. It
82 -- holds the entities for all subprograms that have been examined for this
83 -- particular outer level call, and is used to prevent both infinite
84 -- recursion, and useless reanalysis of bodies already seen
86 package Elab_Visited
is new Table
.Table
(
87 Table_Component_Type
=> Entity_Id
,
88 Table_Index_Type
=> Int
,
91 Table_Increment
=> 100,
92 Table_Name
=> "Elab_Visited");
94 -- This table stores calls to Check_Internal_Call that are delayed until
95 -- all generics are instantiated and in particular until after all generic
96 -- bodies have been inserted. We need to delay, because we need to be able
97 -- to look through the inserted bodies.
99 type Delay_Element
is record
101 -- The parameter N from the call to Check_Internal_Call. Note that this
102 -- node may get rewritten over the delay period by expansion in the call
103 -- case (but not in the instantiation case).
106 -- The parameter E from the call to Check_Internal_Call
108 Orig_Ent
: Entity_Id
;
109 -- The parameter Orig_Ent from the call to Check_Internal_Call
112 -- The current scope of the call. This is restored when we complete the
113 -- delayed call, so that we do this in the right scope.
115 From_Elab_Code
: Boolean;
116 -- Save indication of whether this call is from elaboration code
118 Outer_Scope
: Entity_Id
;
119 -- Save scope of outer level call
122 package Delay_Check
is new Table
.Table
(
123 Table_Component_Type
=> Delay_Element
,
124 Table_Index_Type
=> Int
,
125 Table_Low_Bound
=> 1,
126 Table_Initial
=> 1000,
127 Table_Increment
=> 100,
128 Table_Name
=> "Delay_Check");
131 -- Top level scope of current scope. Compute this only once at the outer
132 -- level, i.e. for a call to Check_Elab_Call from outside this unit.
134 Outer_Level_Sloc
: Source_Ptr
;
135 -- Save Sloc value for outer level call node for comparisons of source
136 -- locations. A body is too late if it appears after the *outer* level
137 -- call, not the particular call that is being analyzed.
139 From_Elab_Code
: Boolean;
140 -- This flag shows whether the outer level call currently being examined
141 -- is or is not in elaboration code. We are only interested in calls to
142 -- routines in other units if this flag is True.
144 In_Task_Activation
: Boolean := False;
145 -- This flag indicates whether we are performing elaboration checks on
146 -- task procedures, at the point of activation. If true, we do not trace
147 -- internal calls in these procedures, because all local bodies are known
150 Delaying_Elab_Checks
: Boolean := True;
151 -- This is set True till the compilation is complete, including the
152 -- insertion of all instance bodies. Then when Check_Elab_Calls is called,
153 -- the delay table is used to make the delayed calls and this flag is reset
154 -- to False, so that the calls are processed.
156 -----------------------
157 -- Local Subprograms --
158 -----------------------
160 -- Note: Outer_Scope in all following specs represents the scope of
161 -- interest of the outer level call. If it is set to Standard_Standard,
162 -- then it means the outer level call was at elaboration level, and that
163 -- thus all calls are of interest. If it was set to some other scope,
164 -- then the original call was an inner call, and we are not interested
165 -- in calls that go outside this scope.
167 procedure Activate_Elaborate_All_Desirable
(N
: Node_Id
; U
: Entity_Id
);
168 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
169 -- for the WITH clause for unit U (which will always be present). A special
170 -- case is when N is a function or procedure instantiation, in which case
171 -- it is sufficient to set Elaborate_Desirable, since in this case there is
172 -- no possibility of transitive elaboration issues.
174 procedure Check_A_Call
177 Outer_Scope
: Entity_Id
;
178 Inter_Unit_Only
: Boolean;
179 Generate_Warnings
: Boolean := True;
180 In_Init_Proc
: Boolean := False);
181 -- This is the internal recursive routine that is called to check for
182 -- possible elaboration error. The argument N is a subprogram call or
183 -- generic instantiation, or 'Access attribute reference to be checked, and
184 -- E is the entity of the called subprogram, or instantiated generic unit,
185 -- or subprogram referenced by 'Access.
187 -- In SPARK mode, N can also be a variable reference, since in SPARK this
188 -- also triggers a requirement for Elaborate_All, and in this case E is the
189 -- entity being referenced.
191 -- Outer_Scope is the outer level scope for the original reference.
192 -- Inter_Unit_Only is set if the call is only to be checked in the
193 -- case where it is to another unit (and skipped if within a unit).
194 -- Generate_Warnings is set to False to suppress warning messages about
195 -- missing pragma Elaborate_All's. These messages are not wanted for
196 -- inner calls in the dynamic model. Note that an instance of the Access
197 -- attribute applied to a subprogram also generates a call to this
198 -- procedure (since the referenced subprogram may be called later
199 -- indirectly). Flag In_Init_Proc should be set whenever the current
200 -- context is a type init proc.
202 -- Note: this might better be called Check_A_Reference to recognize the
203 -- variable case for SPARK, but we prefer to retain the historical name
204 -- since in practice this is mostly about checking calls for the possible
205 -- occurrence of an access-before-elaboration exception.
207 procedure Check_Bad_Instantiation
(N
: Node_Id
);
208 -- N is a node for an instantiation (if called with any other node kind,
209 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
210 -- the special case of a generic instantiation of a generic spec in the
211 -- same declarative part as the instantiation where a body is present and
212 -- has not yet been seen. This is an obvious error, but needs to be checked
213 -- specially at the time of the instantiation, since it is a case where we
214 -- cannot insert the body anywhere. If this case is detected, warnings are
215 -- generated, and a raise of Program_Error is inserted. In addition any
216 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
217 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
218 -- flag as an indication that no attempt should be made to insert an
221 procedure Check_Internal_Call
224 Outer_Scope
: Entity_Id
;
225 Orig_Ent
: Entity_Id
);
226 -- N is a function call or procedure statement call node and E is the
227 -- entity of the called function, which is within the current compilation
228 -- unit (where subunits count as part of the parent). This call checks if
229 -- this call, or any call within any accessed body could cause an ABE, and
230 -- if so, outputs a warning. Orig_Ent differs from E only in the case of
231 -- renamings, and points to the original name of the entity. This is used
232 -- for error messages. Outer_Scope is the outer level scope for the
235 procedure Check_Internal_Call_Continue
238 Outer_Scope
: Entity_Id
;
239 Orig_Ent
: Entity_Id
);
240 -- The processing for Check_Internal_Call is divided up into two phases,
241 -- and this represents the second phase. The second phase is delayed if
242 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
243 -- phase makes an entry in the Delay_Check table, which is processed when
244 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to
245 -- Check_Internal_Call. Outer_Scope is the outer level scope for the
248 function Has_Generic_Body
(N
: Node_Id
) return Boolean;
249 -- N is a generic package instantiation node, and this routine determines
250 -- if this package spec does in fact have a generic body. If so, then
251 -- True is returned, otherwise False. Note that this is not at all the
252 -- same as checking if the unit requires a body, since it deals with
253 -- the case of optional bodies accurately (i.e. if a body is optional,
254 -- then it looks to see if a body is actually present). Note: this
255 -- function can only do a fully correct job if in generating code mode
256 -- where all bodies have to be present. If we are operating in semantics
257 -- check only mode, then in some cases of optional bodies, a result of
258 -- False may incorrectly be given. In practice this simply means that
259 -- some cases of warnings for incorrect order of elaboration will only
260 -- be given when generating code, which is not a big problem (and is
261 -- inevitable, given the optional body semantics of Ada).
263 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
);
264 -- Given code for an elaboration check (or unconditional raise if the check
265 -- is not needed), inserts the code in the appropriate place. N is the call
266 -- or instantiation node for which the check code is required. C is the
267 -- test whose failure triggers the raise.
269 function Is_Call_Of_Generic_Formal
(N
: Node_Id
) return Boolean;
270 -- Returns True if node N is a call to a generic formal subprogram
272 function Is_Finalization_Procedure
(Id
: Entity_Id
) return Boolean;
273 -- Determine whether entity Id denotes a [Deep_]Finalize procedure
275 procedure Output_Calls
277 Check_Elab_Flag
: Boolean);
278 -- Outputs chain of calls stored in the Elab_Call table. The caller has
279 -- already generated the main warning message, so the warnings generated
280 -- are all continuation messages. The argument is the call node at which
281 -- the messages are to be placed. When Check_Elab_Flag is set, calls are
282 -- enumerated only when flag Elab_Warning is set for the dynamic case or
283 -- when flag Elab_Info_Messages is set for the static case.
285 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean;
286 -- Given two scopes, determine whether they are the same scope from an
287 -- elaboration point of view, i.e. packages and blocks are ignored.
289 procedure Set_C_Scope
;
290 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
291 -- to be the enclosing compilation unit of this scope.
293 function Get_Referenced_Ent
(N
: Node_Id
) return Entity_Id
;
294 -- N is either a function or procedure call or an access attribute that
295 -- references a subprogram. This call retrieves the relevant entity. If
296 -- this is a call to a protected subprogram, the entity is a selected
297 -- component. The callable entity may be absent, in which case Empty is
298 -- returned. This happens with non-analyzed calls in nested generics.
300 -- If SPARK_Mode is On, then N can also be a reference to an E_Variable
301 -- entity, in which case, the value returned is simply this entity.
303 procedure Set_Elaboration_Constraint
307 -- The current unit U may depend semantically on some unit P which is not
308 -- in the current context. If there is an elaboration call that reaches P,
309 -- we need to indicate that P requires an Elaborate_All, but this is not
310 -- effective in U's ali file, if there is no with_clause for P. In this
311 -- case we add the Elaborate_All on the unit Q that directly or indirectly
312 -- makes P available. This can happen in two cases:
314 -- a) Q declares a subtype of a type declared in P, and the call is an
315 -- initialization call for an object of that subtype.
317 -- b) Q declares an object of some tagged type whose root type is
318 -- declared in P, and the initialization call uses object notation on
319 -- that object to reach a primitive operation or a classwide operation
322 -- If P appears in the context of U, the current processing is correct.
323 -- Otherwise we must identify these two cases to retrieve Q and place the
324 -- Elaborate_All_Desirable on it.
326 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
;
327 -- Given a compilation unit entity, if it is a spec entity, it is returned
328 -- unchanged. If it is a body entity, then the spec for the corresponding
331 procedure Supply_Bodies
(N
: Node_Id
);
332 -- Given a node, N, that is either a subprogram declaration or a package
333 -- declaration, this procedure supplies dummy bodies for the subprogram
334 -- or for all subprograms in the package. If the given node is not one of
335 -- these two possibilities, then Supply_Bodies does nothing. The dummy body
336 -- contains a single Raise statement.
338 procedure Supply_Bodies
(L
: List_Id
);
339 -- Calls Supply_Bodies for all elements of the given list L
341 function Within
(E1
, E2
: Entity_Id
) return Boolean;
342 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
343 -- of its contained scopes, False otherwise.
345 function Within_Elaborate_All
346 (Unit
: Unit_Number_Type
;
347 E
: Entity_Id
) return Boolean;
348 -- Return True if we are within the scope of an Elaborate_All for E, or if
349 -- we are within the scope of an Elaborate_All for some other unit U, and U
350 -- with's E. This prevents spurious warnings when the called entity is
351 -- renamed within U, or in case of generic instances.
353 --------------------------------------
354 -- Activate_Elaborate_All_Desirable --
355 --------------------------------------
357 procedure Activate_Elaborate_All_Desirable
(N
: Node_Id
; U
: Entity_Id
) is
358 UN
: constant Unit_Number_Type
:= Get_Code_Unit
(N
);
359 CU
: constant Node_Id
:= Cunit
(UN
);
360 UE
: constant Entity_Id
:= Cunit_Entity
(UN
);
361 Unm
: constant Unit_Name_Type
:= Unit_Name
(UN
);
362 CI
: constant List_Id
:= Context_Items
(CU
);
366 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
);
367 -- This procedure is called when the elaborate indication must be
368 -- applied to a unit not in the context of the referencing unit. The
369 -- unit gets added to the context as an implicit with.
371 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean;
372 -- UEs is the spec entity of a unit. If the unit to be marked is
373 -- in the context item list of this unit spec, then the call returns
374 -- True and Itm is left set to point to the relevant N_With_Clause node.
376 procedure Set_Elab_Flag
(Itm
: Node_Id
);
377 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
379 -----------------------------
380 -- Add_To_Context_And_Mark --
381 -----------------------------
383 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
) is
384 CW
: constant Node_Id
:=
385 Make_With_Clause
(Sloc
(Itm
),
389 Set_Library_Unit
(CW
, Library_Unit
(Itm
));
390 Set_Implicit_With
(CW
, True);
392 -- Set elaborate all desirable on copy and then append the copy to
393 -- the list of body with's and we are done.
397 end Add_To_Context_And_Mark
;
403 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean is
404 UNs
: constant Unit_Number_Type
:= Get_Source_Unit
(UEs
);
405 CUs
: constant Node_Id
:= Cunit
(UNs
);
406 CIs
: constant List_Id
:= Context_Items
(CUs
);
410 while Present
(Itm
) loop
411 if Nkind
(Itm
) = N_With_Clause
then
413 Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
430 procedure Set_Elab_Flag
(Itm
: Node_Id
) is
432 if Nkind
(N
) in N_Subprogram_Instantiation
then
433 Set_Elaborate_Desirable
(Itm
);
435 Set_Elaborate_All_Desirable
(Itm
);
439 -- Start of processing for Activate_Elaborate_All_Desirable
442 -- Do not set binder indication if expansion is disabled, as when
443 -- compiling a generic unit.
445 if not Expander_Active
then
450 while Present
(Itm
) loop
451 if Nkind
(Itm
) = N_With_Clause
then
452 Ent
:= Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
454 -- If we find it, then mark elaborate all desirable and return
465 -- If we fall through then the with clause is not present in the
466 -- current unit. One legitimate possibility is that the with clause
467 -- is present in the spec when we are a body.
469 if Is_Body_Name
(Unm
)
470 and then In_Withs_Of
(Spec_Entity
(UE
))
472 Add_To_Context_And_Mark
(Itm
);
476 -- Similarly, we may be in the spec or body of a child unit, where
477 -- the unit in question is with'ed by some ancestor of the child unit.
479 if Is_Child_Name
(Unm
) then
487 exit when Pkg
= Standard_Standard
;
489 if In_Withs_Of
(Pkg
) then
490 Add_To_Context_And_Mark
(Itm
);
497 -- Here if we do not find with clause on spec or body. We just ignore
498 -- this case, it means that the elaboration involves some other unit
499 -- than the unit being compiled, and will be caught elsewhere.
502 end Activate_Elaborate_All_Desirable
;
508 procedure Check_A_Call
511 Outer_Scope
: Entity_Id
;
512 Inter_Unit_Only
: Boolean;
513 Generate_Warnings
: Boolean := True;
514 In_Init_Proc
: Boolean := False)
516 Access_Case
: constant Boolean := Nkind
(N
) = N_Attribute_Reference
;
517 -- Indicates if we have Access attribute case
519 function Call_To_Instance_From_Outside
520 (Ent
: Entity_Id
) return Boolean;
521 -- True if we're calling an instance of a generic subprogram, or a
522 -- subprogram in an instance of a generic package, and the call is
523 -- outside that instance.
525 procedure Elab_Warning
528 Ent
: Node_Or_Entity_Id
);
529 -- Generate a call to Error_Msg_NE with parameters Msg_D or Msg_S (for
530 -- dynamic or static elaboration model), N and Ent. Msg_D is a real
531 -- warning (output if Msg_D is non-null and Elab_Warnings is set),
532 -- Msg_S is an info message (output if Elab_Info_Messages is set.
534 function Find_W_Scope
return Entity_Id
;
535 -- Find top level scope for called entity (not following renamings
536 -- or derivations). This is where the Elaborate_All will go if it is
537 -- needed. We start with the called entity, except in the case of an
538 -- initialization procedure outside the current package, where the init
539 -- proc is in the root package, and we start from the entity of the name
542 -----------------------------------
543 -- Call_To_Instance_From_Outside --
544 -----------------------------------
546 function Call_To_Instance_From_Outside
547 (Ent
: Entity_Id
) return Boolean is
549 X
: Entity_Id
:= Ent
;
552 if X
= Standard_Standard
then
556 if Is_Generic_Instance
(X
) then
557 return not In_Open_Scopes
(X
);
562 end Call_To_Instance_From_Outside
;
568 procedure Elab_Warning
571 Ent
: Node_Or_Entity_Id
)
574 -- Dynamic elaboration checks, real warning
576 if Dynamic_Elaboration_Checks
then
577 if not Access_Case
then
578 if Msg_D
/= "" and then Elab_Warnings
then
579 Error_Msg_NE
(Msg_D
, N
, Ent
);
582 -- In the access case emit first warning message as well,
583 -- otherwise list of calls will appear as errors.
585 elsif Elab_Warnings
then
586 Error_Msg_NE
(Msg_S
, N
, Ent
);
589 -- Static elaboration checks, info message
592 if Elab_Info_Messages
then
593 Error_Msg_NE
(Msg_S
, N
, Ent
);
602 function Find_W_Scope
return Entity_Id
is
603 Refed_Ent
: constant Entity_Id
:= Get_Referenced_Ent
(N
);
606 if Is_Init_Proc
(Refed_Ent
)
607 and then not In_Same_Extended_Unit
(N
, Refed_Ent
)
609 W_Scope
:= Scope
(Refed_Ent
);
614 -- Now loop through scopes to get to the enclosing compilation unit
616 while not Is_Compilation_Unit
(W_Scope
) loop
617 W_Scope
:= Scope
(W_Scope
);
625 Variable_Case
: constant Boolean :=
626 Nkind
(N
) in N_Has_Entity
627 and then Present
(Entity
(N
))
628 and then Ekind
(Entity
(N
)) = E_Variable
;
629 -- Indicates if we have variable reference case
631 Loc
: constant Source_Ptr
:= Sloc
(N
);
633 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
634 -- Indicates if we have instantiation case
637 Callee_Unit_Internal
: Boolean;
638 Caller_Unit_Internal
: Boolean;
640 Inst_Callee
: Source_Ptr
;
641 Inst_Caller
: Source_Ptr
;
642 Unit_Callee
: Unit_Number_Type
;
643 Unit_Caller
: Unit_Number_Type
;
645 Body_Acts_As_Spec
: Boolean;
646 -- Set to true if call is to body acting as spec (no separate spec)
648 Cunit_SC
: Boolean := False;
649 -- Set to suppress dynamic elaboration checks where one of the
650 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
651 -- if a pragma Elaborate[_All] applies to that scope, in which case
652 -- warnings on the scope are also suppressed. For the internal case,
653 -- we ignore this flag.
656 -- Top level scope of entity for called subprogram. This value includes
657 -- following renamings and derivations, so this scope can be in a
658 -- non-visible unit. This is the scope that is to be investigated to
659 -- see whether an elaboration check is required.
661 Is_DIC_Proc
: Boolean := False;
662 -- Flag set when the call denotes the Default_Initial_Condition
663 -- procedure of a private type that wraps a nontrivial assertion
666 Issue_In_SPARK
: Boolean;
667 -- Flag set when a source entity is called during elaboration in SPARK
669 W_Scope
: constant Entity_Id
:= Find_W_Scope
;
670 -- Top level scope of directly called entity for subprogram. This
671 -- differs from E_Scope in the case where renamings or derivations
672 -- are involved, since it does not follow these links. W_Scope is
673 -- generally in a visible unit, and it is this scope that may require
674 -- an Elaborate_All. However, there are some cases (initialization
675 -- calls and calls involving object notation) where W_Scope might not
676 -- be in the context of the current unit, and there is an intermediate
677 -- package that is, in which case the Elaborate_All has to be placed
678 -- on this intermediate package. These special cases are handled in
679 -- Set_Elaboration_Constraint.
681 -- Start of processing for Check_A_Call
684 -- If the call is known to be within a local Suppress Elaboration
685 -- pragma, nothing to check. This can happen in task bodies. But
686 -- we ignore this for a call to a generic formal.
688 if Nkind
(N
) in N_Subprogram_Call
689 and then No_Elaboration_Check
(N
)
690 and then not Is_Call_Of_Generic_Formal
(N
)
695 -- If this is a rewrite of a Valid_Scalars attribute, then nothing to
696 -- check, we don't mind in this case if the call occurs before the body
697 -- since this is all generated code.
699 if Nkind
(Original_Node
(N
)) = N_Attribute_Reference
700 and then Attribute_Name
(Original_Node
(N
)) = Name_Valid_Scalars
705 -- Intrinsics such as instances of Unchecked_Deallocation do not have
706 -- any body, so elaboration checking is not needed, and would be wrong.
708 if Is_Intrinsic_Subprogram
(E
) then
712 -- Proceed with check
716 -- For a variable reference, just set Body_Acts_As_Spec to False
718 if Variable_Case
then
719 Body_Acts_As_Spec
:= False;
721 -- Additional checks for all other cases
724 -- Go to parent for derived subprogram, or to original subprogram in
725 -- the case of a renaming (Alias covers both these cases).
728 if (Suppress_Elaboration_Warnings
(Ent
)
729 or else Elaboration_Checks_Suppressed
(Ent
))
730 and then (Inst_Case
or else No
(Alias
(Ent
)))
735 -- Nothing to do for imported entities
737 if Is_Imported
(Ent
) then
741 exit when Inst_Case
or else No
(Alias
(Ent
));
745 Decl
:= Unit_Declaration_Node
(Ent
);
747 if Nkind
(Decl
) = N_Subprogram_Body
then
748 Body_Acts_As_Spec
:= True;
750 elsif Nkind_In
(Decl
, N_Subprogram_Declaration
,
751 N_Subprogram_Body_Stub
)
754 Body_Acts_As_Spec
:= False;
756 -- If we have none of an instantiation, subprogram body or subprogram
757 -- declaration, or in the SPARK case, a variable reference, then
758 -- it is not a case that we want to check. (One case is a call to a
759 -- generic formal subprogram, where we do not want the check in the
769 if Elaboration_Checks_Suppressed
(E_Scope
)
770 or else Suppress_Elaboration_Warnings
(E_Scope
)
775 -- Exit when we get to compilation unit, not counting subunits
777 exit when Is_Compilation_Unit
(E_Scope
)
778 and then (Is_Child_Unit
(E_Scope
)
779 or else Scope
(E_Scope
) = Standard_Standard
);
781 pragma Assert
(E_Scope
/= Standard_Standard
);
783 -- Move up a scope looking for compilation unit
785 E_Scope
:= Scope
(E_Scope
);
788 -- No checks needed for pure or preelaborated compilation units
790 if Is_Pure
(E_Scope
) or else Is_Preelaborated
(E_Scope
) then
794 -- If the generic entity is within a deeper instance than we are, then
795 -- either the instantiation to which we refer itself caused an ABE, in
796 -- which case that will be handled separately, or else we know that the
797 -- body we need appears as needed at the point of the instantiation.
798 -- However, this assumption is only valid if we are in static mode.
800 if not Dynamic_Elaboration_Checks
802 Instantiation_Depth
(Sloc
(Ent
)) > Instantiation_Depth
(Sloc
(N
))
807 -- Do not give a warning for a package with no body
809 if Ekind
(Ent
) = E_Generic_Package
and then not Has_Generic_Body
(N
) then
813 -- Case of entity is in same unit as call or instantiation. In the
814 -- instantiation case, W_Scope may be different from E_Scope; we want
815 -- the unit in which the instantiation occurs, since we're analyzing
816 -- based on the expansion.
818 if W_Scope
= C_Scope
then
819 if not Inter_Unit_Only
then
820 Check_Internal_Call
(N
, Ent
, Outer_Scope
, E
);
826 -- Case of entity is not in current unit (i.e. with'ed unit case)
828 -- We are only interested in such calls if the outer call was from
829 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
831 if not From_Elab_Code
and then not Dynamic_Elaboration_Checks
then
835 -- Nothing to do if some scope said that no checks were required
841 -- Nothing to do for a generic instance, because a call to an instance
842 -- cannot fail the elaboration check, because the body of the instance
843 -- is always elaborated immediately after the spec.
845 if Call_To_Instance_From_Outside
(Ent
) then
849 -- Nothing to do if subprogram with no separate spec. However, a call
850 -- to Deep_Initialize may result in a call to a user-defined Initialize
851 -- procedure, which imposes a body dependency. This happens only if the
852 -- type is controlled and the Initialize procedure is not inherited.
854 if Body_Acts_As_Spec
then
855 if Is_TSS
(Ent
, TSS_Deep_Initialize
) then
857 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Ent
));
861 if not Is_Controlled
(Typ
) then
864 Init
:= Find_Prim_Op
(Typ
, Name_Initialize
);
866 if Comes_From_Source
(Init
) then
879 -- Check cases of internal units
881 Callee_Unit_Internal
:=
882 Is_Internal_File_Name
(Unit_File_Name
(Get_Source_Unit
(E_Scope
)));
884 -- Do not give a warning if the with'ed unit is internal and this is
885 -- the generic instantiation case (this saves a lot of hassle dealing
886 -- with the Text_IO special child units)
888 if Callee_Unit_Internal
and Inst_Case
then
892 if C_Scope
= Standard_Standard
then
893 Caller_Unit_Internal
:= False;
895 Caller_Unit_Internal
:=
896 Is_Internal_File_Name
(Unit_File_Name
(Get_Source_Unit
(C_Scope
)));
899 -- Do not give a warning if the with'ed unit is internal and the
900 -- caller is not internal (since the binder always elaborates
901 -- internal units first).
903 if Callee_Unit_Internal
and (not Caller_Unit_Internal
) then
907 -- For now, if debug flag -gnatdE is not set, do no checking for
908 -- one internal unit withing another. This fixes the problem with
909 -- the sgi build and storage errors. To be resolved later ???
911 if (Callee_Unit_Internal
and Caller_Unit_Internal
)
912 and not Debug_Flag_EE
917 if Is_TSS
(E
, TSS_Deep_Initialize
) then
921 -- If the call is in an instance, and the called entity is not
922 -- defined in the same instance, then the elaboration issue focuses
923 -- around the unit containing the template, it is this unit which
924 -- requires an Elaborate_All.
926 -- However, if we are doing dynamic elaboration, we need to chase the
927 -- call in the usual manner.
929 -- We also need to chase the call in the usual manner if it is a call
930 -- to a generic formal parameter, since that case was not handled as
931 -- part of the processing of the template.
933 Inst_Caller
:= Instantiation
(Get_Source_File_Index
(Sloc
(N
)));
934 Inst_Callee
:= Instantiation
(Get_Source_File_Index
(Sloc
(Ent
)));
936 if Inst_Caller
= No_Location
then
937 Unit_Caller
:= No_Unit
;
939 Unit_Caller
:= Get_Source_Unit
(N
);
942 if Inst_Callee
= No_Location
then
943 Unit_Callee
:= No_Unit
;
945 Unit_Callee
:= Get_Source_Unit
(Ent
);
948 if Unit_Caller
/= No_Unit
949 and then Unit_Callee
/= Unit_Caller
950 and then not Dynamic_Elaboration_Checks
951 and then not Is_Call_Of_Generic_Formal
(N
)
953 E_Scope
:= Spec_Entity
(Cunit_Entity
(Unit_Caller
));
955 -- If we don't get a spec entity, just ignore call. Not quite
956 -- clear why this check is necessary. ???
962 -- Otherwise step to enclosing compilation unit
964 while not Is_Compilation_Unit
(E_Scope
) loop
965 E_Scope
:= Scope
(E_Scope
);
968 -- For the case where N is not an instance, and is not a call within
969 -- instance to other than a generic formal, we recompute E_Scope
970 -- for the error message, since we do NOT want to go to the unit
971 -- which has the ultimate declaration in the case of renaming and
972 -- derivation and we also want to go to the generic unit in the
973 -- case of an instance, and no further.
976 -- Loop to carefully follow renamings and derivations one step
977 -- outside the current unit, but not further.
979 if not (Inst_Case
or Variable_Case
)
980 and then Present
(Alias
(Ent
))
982 E_Scope
:= Alias
(Ent
);
988 while not Is_Compilation_Unit
(E_Scope
) loop
989 E_Scope
:= Scope
(E_Scope
);
992 -- If E_Scope is the same as C_Scope, it means that there
993 -- definitely was a local renaming or derivation, and we
994 -- are not yet out of the current unit.
996 exit when E_Scope
/= C_Scope
;
1000 -- If no alias, there could be a previous error, but not if we've
1001 -- already reached the outermost level (Standard).
1009 if Within_Elaborate_All
(Current_Sem_Unit
, E_Scope
) then
1013 Is_DIC_Proc
:= Is_Nontrivial_Default_Init_Cond_Procedure
(Ent
);
1015 -- Elaboration issues in SPARK are reported only for source constructs
1016 -- and for nontrivial Default_Initial_Condition procedures. The latter
1017 -- must be checked because the default initialization of an object of a
1018 -- private type triggers the evaluation of the Default_Initial_Condition
1019 -- expression, which in turn may have side effects.
1022 SPARK_Mode
= On
and (Comes_From_Source
(Ent
) or Is_DIC_Proc
);
1024 -- Now check if an Elaborate_All (or dynamic check) is needed
1026 if not Suppress_Elaboration_Warnings
(Ent
)
1027 and then not Elaboration_Checks_Suppressed
(Ent
)
1028 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1029 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1030 and then ((Elab_Warnings
or Elab_Info_Messages
)
1031 or else SPARK_Mode
= On
)
1032 and then Generate_Warnings
1034 -- Instantiation case
1037 if Issue_In_SPARK
then
1039 ("instantiation of & during elaboration in SPARK", N
, Ent
);
1042 ("instantiation of & may raise Program_Error?l?",
1043 "info: instantiation of & during elaboration?$?", Ent
);
1046 -- Indirect call case, info message only in static elaboration
1047 -- case, because the attribute reference itself cannot raise an
1048 -- exception. Note that SPARK does not permit indirect calls.
1050 elsif Access_Case
then
1051 Elab_Warning
("", "info: access to & during elaboration?$?", Ent
);
1053 -- Variable reference in SPARK mode
1055 elsif Variable_Case
and Issue_In_SPARK
then
1057 ("reference to & during elaboration in SPARK", N
, Ent
);
1059 -- Subprogram call case
1062 if Nkind
(Name
(N
)) in N_Has_Entity
1063 and then Is_Init_Proc
(Entity
(Name
(N
)))
1064 and then Comes_From_Source
(Ent
)
1067 ("implicit call to & may raise Program_Error?l?",
1068 "info: implicit call to & during elaboration?$?",
1071 elsif Issue_In_SPARK
then
1073 -- Emit a specialized error message when the elaboration of an
1074 -- object of a private type evaluates the expression of pragma
1075 -- Default_Initial_Condition. This prevents the internal name
1076 -- of the procedure from appearing in the error message.
1080 ("call to Default_Initial_Condition during elaboration in "
1084 ("call to & during elaboration in SPARK", N
, Ent
);
1089 ("call to & may raise Program_Error?l?",
1090 "info: call to & during elaboration?$?",
1095 Error_Msg_Qual_Level
:= Nat
'Last;
1097 -- Case of Elaborate_All not present and required, for SPARK this
1098 -- is an error, so give an error message.
1100 if Issue_In_SPARK
then
1101 Error_Msg_NE
("\Elaborate_All pragma required for&", N
, W_Scope
);
1103 -- Otherwise we generate an implicit pragma. For a subprogram
1104 -- instantiation, Elaborate is good enough, since no transitive
1105 -- call is possible at elaboration time in this case.
1107 elsif Nkind
(N
) in N_Subprogram_Instantiation
then
1109 ("\missing pragma Elaborate for&?l?",
1110 "\implicit pragma Elaborate for& generated?$?",
1113 -- For all other cases, we need an implicit Elaborate_All
1117 ("\missing pragma Elaborate_All for&?l?",
1118 "\implicit pragma Elaborate_All for & generated?$?",
1122 Error_Msg_Qual_Level
:= 0;
1124 -- Take into account the flags related to elaboration warning
1125 -- messages when enumerating the various calls involved. This
1126 -- ensures the proper pairing of the main warning and the
1127 -- clarification messages generated by Output_Calls.
1129 Output_Calls
(N
, Check_Elab_Flag
=> True);
1131 -- Set flag to prevent further warnings for same unit unless in
1134 if not All_Errors_Mode
and not Dynamic_Elaboration_Checks
then
1135 Set_Suppress_Elaboration_Warnings
(W_Scope
, True);
1139 -- Check for runtime elaboration check required
1141 if Dynamic_Elaboration_Checks
then
1142 if not Elaboration_Checks_Suppressed
(Ent
)
1143 and then not Elaboration_Checks_Suppressed
(W_Scope
)
1144 and then not Elaboration_Checks_Suppressed
(E_Scope
)
1145 and then not Cunit_SC
1147 -- Runtime elaboration check required. Generate check of the
1148 -- elaboration Boolean for the unit containing the entity.
1150 -- Note that for this case, we do check the real unit (the one
1151 -- from following renamings, since that is the issue).
1153 -- Could this possibly miss a useless but required PE???
1155 Insert_Elab_Check
(N
,
1156 Make_Attribute_Reference
(Loc
,
1157 Attribute_Name
=> Name_Elaborated
,
1159 New_Occurrence_Of
(Spec_Entity
(E_Scope
), Loc
)));
1161 -- Prevent duplicate elaboration checks on the same call,
1162 -- which can happen if the body enclosing the call appears
1163 -- itself in a call whose elaboration check is delayed.
1165 if Nkind
(N
) in N_Subprogram_Call
then
1166 Set_No_Elaboration_Check
(N
);
1170 -- Case of static elaboration model
1173 -- Do not do anything if elaboration checks suppressed. Note that
1174 -- we check Ent here, not E, since we want the real entity for the
1175 -- body to see if checks are suppressed for it, not the dummy
1176 -- entry for renamings or derivations.
1178 if Elaboration_Checks_Suppressed
(Ent
)
1179 or else Elaboration_Checks_Suppressed
(E_Scope
)
1180 or else Elaboration_Checks_Suppressed
(W_Scope
)
1184 -- Do not generate an Elaborate_All for finalization routines
1185 -- which perform partial clean up as part of initialization.
1187 elsif In_Init_Proc
and then Is_Finalization_Procedure
(Ent
) then
1190 -- Here we need to generate an implicit elaborate all
1193 -- Generate Elaborate_All warning unless suppressed
1195 if (Elab_Info_Messages
and Generate_Warnings
and not Inst_Case
)
1196 and then not Suppress_Elaboration_Warnings
(Ent
)
1197 and then not Suppress_Elaboration_Warnings
(E_Scope
)
1198 and then not Suppress_Elaboration_Warnings
(W_Scope
)
1200 Error_Msg_Node_2
:= W_Scope
;
1202 ("info: call to& in elaboration code " &
1203 "requires pragma Elaborate_All on&?$?", N
, E
);
1206 -- Set indication for binder to generate Elaborate_All
1208 Set_Elaboration_Constraint
(N
, E
, W_Scope
);
1213 -----------------------------
1214 -- Check_Bad_Instantiation --
1215 -----------------------------
1217 procedure Check_Bad_Instantiation
(N
: Node_Id
) is
1221 -- Nothing to do if we do not have an instantiation (happens in some
1222 -- error cases, and also in the formal package declaration case)
1224 if Nkind
(N
) not in N_Generic_Instantiation
then
1227 -- Nothing to do if serious errors detected (avoid cascaded errors)
1229 elsif Serious_Errors_Detected
/= 0 then
1232 -- Nothing to do if not in full analysis mode
1234 elsif not Full_Analysis
then
1237 -- Nothing to do if inside a generic template
1239 elsif Inside_A_Generic
then
1242 -- Nothing to do if a library level instantiation
1244 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1247 -- Nothing to do if we are compiling a proper body for semantic
1248 -- purposes only. The generic body may be in another proper body.
1251 Nkind
(Parent
(Unit_Declaration_Node
(Main_Unit_Entity
))) = N_Subunit
1256 Ent
:= Get_Generic_Entity
(N
);
1258 -- The case we are interested in is when the generic spec is in the
1259 -- current declarative part
1261 if not Same_Elaboration_Scope
(Current_Scope
, Scope
(Ent
))
1262 or else not In_Same_Extended_Unit
(N
, Ent
)
1267 -- If the generic entity is within a deeper instance than we are, then
1268 -- either the instantiation to which we refer itself caused an ABE, in
1269 -- which case that will be handled separately. Otherwise, we know that
1270 -- the body we need appears as needed at the point of the instantiation.
1271 -- If they are both at the same level but not within the same instance
1272 -- then the body of the generic will be in the earlier instance.
1275 D1
: constant Nat
:= Instantiation_Depth
(Sloc
(Ent
));
1276 D2
: constant Nat
:= Instantiation_Depth
(Sloc
(N
));
1283 and then Is_Generic_Instance
(Scope
(Ent
))
1284 and then not In_Open_Scopes
(Scope
(Ent
))
1290 -- Now we can proceed, if the entity being called has a completion,
1291 -- then we are definitely OK, since we have already seen the body.
1293 if Has_Completion
(Ent
) then
1297 -- If there is no body, then nothing to do
1299 if not Has_Generic_Body
(N
) then
1303 -- Here we definitely have a bad instantiation
1305 Error_Msg_Warn
:= SPARK_Mode
/= On
;
1306 Error_Msg_NE
("cannot instantiate& before body seen<<", N
, Ent
);
1308 if Present
(Instance_Spec
(N
)) then
1309 Supply_Bodies
(Instance_Spec
(N
));
1312 Error_Msg_N
("\Program_Error [<<", N
);
1313 Insert_Elab_Check
(N
);
1314 Set_ABE_Is_Certain
(N
);
1315 end Check_Bad_Instantiation
;
1317 ---------------------
1318 -- Check_Elab_Call --
1319 ---------------------
1321 procedure Check_Elab_Call
1323 Outer_Scope
: Entity_Id
:= Empty
;
1324 In_Init_Proc
: Boolean := False)
1330 -- If the reference is not in the main unit, there is nothing to check.
1331 -- Elaboration call from units in the context of the main unit will lead
1332 -- to semantic dependencies when those units are compiled.
1334 if not In_Extended_Main_Code_Unit
(N
) then
1338 -- For an entry call, check relevant restriction
1340 if Nkind
(N
) = N_Entry_Call_Statement
1341 and then not In_Subprogram_Or_Concurrent_Unit
1343 Check_Restriction
(No_Entry_Calls_In_Elaboration_Code
, N
);
1345 -- Nothing to do if this is not an expected type of reference (happens
1346 -- in some error conditions, and in some cases where rewriting occurs).
1348 elsif Nkind
(N
) not in N_Subprogram_Call
1349 and then Nkind
(N
) /= N_Attribute_Reference
1350 and then (SPARK_Mode
/= On
1351 or else Nkind
(N
) not in N_Has_Entity
1352 or else No
(Entity
(N
))
1353 or else Ekind
(Entity
(N
)) /= E_Variable
)
1357 -- Nothing to do if this is a call already rewritten for elab checking.
1358 -- Such calls appear as the targets of If_Expressions.
1360 -- This check MUST be wrong, it catches far too much
1362 elsif Nkind
(Parent
(N
)) = N_If_Expression
then
1365 -- Nothing to do if inside a generic template
1367 elsif Inside_A_Generic
1368 and then No
(Enclosing_Generic_Body
(N
))
1372 -- Nothing to do if call is being pre-analyzed, as when within a
1373 -- pre/postcondition, a predicate, or an invariant.
1375 elsif In_Spec_Expression
then
1379 -- Nothing to do if this is a call to a postcondition, which is always
1380 -- within a subprogram body, even though the current scope may be the
1381 -- enclosing scope of the subprogram.
1383 if Nkind
(N
) = N_Procedure_Call_Statement
1384 and then Is_Entity_Name
(Name
(N
))
1385 and then Chars
(Entity
(Name
(N
))) = Name_uPostconditions
1390 -- Here we have a reference at elaboration time which must be checked
1392 if Debug_Flag_LL
then
1393 Write_Str
(" Check_Elab_Ref: ");
1395 if Nkind
(N
) = N_Attribute_Reference
then
1396 if not Is_Entity_Name
(Prefix
(N
)) then
1397 Write_Str
("<<not entity name>>");
1399 Write_Name
(Chars
(Entity
(Prefix
(N
))));
1402 Write_Str
("'Access");
1404 elsif No
(Name
(N
)) or else not Is_Entity_Name
(Name
(N
)) then
1405 Write_Str
("<<not entity name>> ");
1408 Write_Name
(Chars
(Entity
(Name
(N
))));
1411 Write_Str
(" reference at ");
1412 Write_Location
(Sloc
(N
));
1416 -- Climb up the tree to make sure we are not inside default expression
1417 -- of a parameter specification or a record component, since in both
1418 -- these cases, we will be doing the actual reference later, not now,
1419 -- and it is at the time of the actual reference (statically speaking)
1420 -- that we must do our static check, not at the time of its initial
1423 -- However, we have to check references within component definitions
1424 -- (e.g. a function call that determines an array component bound),
1425 -- so we terminate the loop in that case.
1428 while Present
(P
) loop
1429 if Nkind_In
(P
, N_Parameter_Specification
,
1430 N_Component_Declaration
)
1434 -- The reference occurs within the constraint of a component,
1435 -- so it must be checked.
1437 elsif Nkind
(P
) = N_Component_Definition
then
1445 -- Stuff that happens only at the outer level
1447 if No
(Outer_Scope
) then
1448 Elab_Visited
.Set_Last
(0);
1450 -- Nothing to do if current scope is Standard (this is a bit odd, but
1451 -- it happens in the case of generic instantiations).
1453 C_Scope
:= Current_Scope
;
1455 if C_Scope
= Standard_Standard
then
1459 -- First case, we are in elaboration code
1461 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1463 if From_Elab_Code
then
1465 -- Complain if ref that comes from source in preelaborated unit
1466 -- and we are not inside a subprogram (i.e. we are in elab code).
1468 if Comes_From_Source
(N
)
1469 and then In_Preelaborated_Unit
1470 and then not In_Inlined_Body
1471 and then Nkind
(N
) /= N_Attribute_Reference
1473 -- This is a warning in GNAT mode allowing such calls to be
1474 -- used in the predefined library with appropriate care.
1476 Error_Msg_Warn
:= GNAT_Mode
;
1478 ("<<non-static call not allowed in preelaborated unit", N
);
1482 -- Second case, we are inside a subprogram or concurrent unit, which
1483 -- means we are not in elaboration code.
1486 -- In this case, the issue is whether we are inside the
1487 -- declarative part of the unit in which we live, or inside its
1488 -- statements. In the latter case, there is no issue of ABE calls
1489 -- at this level (a call from outside to the unit in which we live
1490 -- might cause an ABE, but that will be detected when we analyze
1491 -- that outer level call, as it recurses into the called unit).
1493 -- Climb up the tree, doing this test, and also testing for being
1494 -- inside a default expression, which, as discussed above, is not
1495 -- checked at this stage.
1504 -- If we find a parentless subtree, it seems safe to assume
1505 -- that we are not in a declarative part and that no
1506 -- checking is required.
1512 if Is_List_Member
(P
) then
1513 L
:= List_Containing
(P
);
1520 exit when Nkind
(P
) = N_Subunit
;
1522 -- Filter out case of default expressions, where we do not
1523 -- do the check at this stage.
1525 if Nkind_In
(P
, N_Parameter_Specification
,
1526 N_Component_Declaration
)
1531 -- A protected body has no elaboration code and contains
1532 -- only other bodies.
1534 if Nkind
(P
) = N_Protected_Body
then
1537 elsif Nkind_In
(P
, N_Subprogram_Body
,
1542 if L
= Declarations
(P
) then
1545 -- We are not in elaboration code, but we are doing
1546 -- dynamic elaboration checks, in this case, we still
1547 -- need to do the reference, since the subprogram we are
1548 -- in could be called from another unit, also in dynamic
1549 -- elaboration check mode, at elaboration time.
1551 elsif Dynamic_Elaboration_Checks
then
1553 -- We provide a debug flag to disable this check. That
1554 -- way we have an easy work around for regressions
1555 -- that are caused by this new check. This debug flag
1556 -- can be removed later.
1558 if Debug_Flag_DD
then
1562 -- Do the check in this case
1566 elsif Nkind
(P
) = N_Task_Body
then
1568 -- The check is deferred until Check_Task_Activation
1569 -- but we need to capture local suppress pragmas
1570 -- that may inhibit checks on this call.
1572 Ent
:= Get_Referenced_Ent
(N
);
1577 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1578 or else Elaboration_Checks_Suppressed
(Ent
)
1579 or else Elaboration_Checks_Suppressed
(Scope
(Ent
))
1581 if Nkind
(N
) in N_Subprogram_Call
then
1582 Set_No_Elaboration_Check
(N
);
1588 -- Static model, call is not in elaboration code, we
1589 -- never need to worry, because in the static model the
1590 -- top level caller always takes care of things.
1601 Ent
:= Get_Referenced_Ent
(N
);
1607 -- Nothing to do if this is a recursive call (i.e. a call to
1608 -- an entity that is already in the Elab_Call stack)
1610 for J
in 1 .. Elab_Visited
.Last
loop
1611 if Ent
= Elab_Visited
.Table
(J
) then
1616 -- See if we need to analyze this reference. We analyze it if either of
1617 -- the following conditions is met:
1619 -- It is an inner level call (since in this case it was triggered
1620 -- by an outer level call from elaboration code), but only if the
1621 -- call is within the scope of the original outer level call.
1623 -- It is an outer level reference from elaboration code, or a call to
1624 -- an entity is in the same elaboration scope.
1626 -- And in these cases, we will check both inter-unit calls and
1627 -- intra-unit (within a single unit) calls.
1629 C_Scope
:= Current_Scope
;
1631 -- If not outer level reference, then we follow it if it is within the
1632 -- original scope of the outer reference.
1634 if Present
(Outer_Scope
)
1635 and then Within
(Scope
(Ent
), Outer_Scope
)
1641 Outer_Scope
=> Outer_Scope
,
1642 Inter_Unit_Only
=> False,
1643 In_Init_Proc
=> In_Init_Proc
);
1645 -- Nothing to do if elaboration checks suppressed for this scope.
1646 -- However, an interesting exception, the fact that elaboration checks
1647 -- are suppressed within an instance (because we can trace the body when
1648 -- we process the template) does not extend to calls to generic formal
1651 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1652 and then not Is_Call_Of_Generic_Formal
(N
)
1656 elsif From_Elab_Code
then
1658 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1660 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1662 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1664 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1665 -- is set, then we will do the check, but only in the inter-unit case
1666 -- (this is to accommodate unguarded elaboration calls from other units
1667 -- in which this same mode is set). We don't want warnings in this case,
1668 -- it would generate warnings having nothing to do with elaboration.
1670 elsif Dynamic_Elaboration_Checks
then
1676 Inter_Unit_Only
=> True,
1677 Generate_Warnings
=> False);
1679 -- Otherwise nothing to do
1685 -- A call to an Init_Proc in elaboration code may bring additional
1686 -- dependencies, if some of the record components thereof have
1687 -- initializations that are function calls that come from source. We
1688 -- treat the current node as a call to each of these functions, to check
1689 -- their elaboration impact.
1691 if Is_Init_Proc
(Ent
) and then From_Elab_Code
then
1692 Process_Init_Proc
: declare
1693 Unit_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
1695 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
;
1696 -- Find subprogram calls within body of Init_Proc for Traverse
1697 -- instantiation below.
1699 procedure Traverse_Body
is new Traverse_Proc
(Check_Init_Call
);
1700 -- Traversal procedure to find all calls with body of Init_Proc
1702 ---------------------
1703 -- Check_Init_Call --
1704 ---------------------
1706 function Check_Init_Call
(Nod
: Node_Id
) return Traverse_Result
is
1710 if Nkind
(Nod
) in N_Subprogram_Call
1711 and then Is_Entity_Name
(Name
(Nod
))
1713 Func
:= Entity
(Name
(Nod
));
1715 if Comes_From_Source
(Func
) then
1717 (N
, Func
, Standard_Standard
, Inter_Unit_Only
=> True);
1725 end Check_Init_Call
;
1727 -- Start of processing for Process_Init_Proc
1730 if Nkind
(Unit_Decl
) = N_Subprogram_Body
then
1731 Traverse_Body
(Handled_Statement_Sequence
(Unit_Decl
));
1733 end Process_Init_Proc
;
1735 end Check_Elab_Call
;
1737 -----------------------
1738 -- Check_Elab_Assign --
1739 -----------------------
1741 procedure Check_Elab_Assign
(N
: Node_Id
) is
1745 Pkg_Spec
: Entity_Id
;
1746 Pkg_Body
: Entity_Id
;
1749 -- For record or array component, check prefix. If it is an access type,
1750 -- then there is nothing to do (we do not know what is being assigned),
1751 -- but otherwise this is an assignment to the prefix.
1753 if Nkind_In
(N
, N_Indexed_Component
,
1754 N_Selected_Component
,
1757 if not Is_Access_Type
(Etype
(Prefix
(N
))) then
1758 Check_Elab_Assign
(Prefix
(N
));
1764 -- For type conversion, check expression
1766 if Nkind
(N
) = N_Type_Conversion
then
1767 Check_Elab_Assign
(Expression
(N
));
1771 -- Nothing to do if this is not an entity reference otherwise get entity
1773 if Is_Entity_Name
(N
) then
1779 -- What we are looking for is a reference in the body of a package that
1780 -- modifies a variable declared in the visible part of the package spec.
1783 and then Comes_From_Source
(N
)
1784 and then not Suppress_Elaboration_Warnings
(Ent
)
1785 and then Ekind
(Ent
) = E_Variable
1786 and then not In_Private_Part
(Ent
)
1787 and then Is_Library_Level_Entity
(Ent
)
1789 Scop
:= Current_Scope
;
1791 if No
(Scop
) or else Scop
= Standard_Standard
then
1793 elsif Ekind
(Scop
) = E_Package
1794 and then Is_Compilation_Unit
(Scop
)
1798 Scop
:= Scope
(Scop
);
1802 -- Here Scop points to the containing library package
1805 Pkg_Body
:= Body_Entity
(Pkg_Spec
);
1807 -- All OK if the package has an Elaborate_Body pragma
1809 if Has_Pragma_Elaborate_Body
(Scop
) then
1813 -- OK if entity being modified is not in containing package spec
1815 if not In_Same_Source_Unit
(Scop
, Ent
) then
1819 -- All OK if entity appears in generic package or generic instance.
1820 -- We just get too messed up trying to give proper warnings in the
1821 -- presence of generics. Better no message than a junk one.
1823 Scop
:= Scope
(Ent
);
1824 while Present
(Scop
) and then Scop
/= Pkg_Spec
loop
1825 if Ekind
(Scop
) = E_Generic_Package
then
1827 elsif Ekind
(Scop
) = E_Package
1828 and then Is_Generic_Instance
(Scop
)
1833 Scop
:= Scope
(Scop
);
1836 -- All OK if in task, don't issue warnings there
1838 if In_Task_Activation
then
1842 -- OK if no package body
1844 if No
(Pkg_Body
) then
1848 -- OK if reference is not in package body
1850 if not In_Same_Source_Unit
(Pkg_Body
, N
) then
1854 -- OK if package body has no handled statement sequence
1857 HSS
: constant Node_Id
:=
1858 Handled_Statement_Sequence
(Declaration_Node
(Pkg_Body
));
1860 if No
(HSS
) or else not Comes_From_Source
(HSS
) then
1865 -- We definitely have a case of a modification of an entity in
1866 -- the package spec from the elaboration code of the package body.
1867 -- We may not give the warning (because there are some additional
1868 -- checks to avoid too many false positives), but it would be a good
1869 -- idea for the binder to try to keep the body elaboration close to
1870 -- the spec elaboration.
1872 Set_Elaborate_Body_Desirable
(Pkg_Spec
);
1874 -- All OK in gnat mode (we know what we are doing)
1880 -- All OK if all warnings suppressed
1882 if Warning_Mode
= Suppress
then
1886 -- All OK if elaboration checks suppressed for entity
1888 if Checks_May_Be_Suppressed
(Ent
)
1889 and then Is_Check_Suppressed
(Ent
, Elaboration_Check
)
1894 -- OK if the entity is initialized. Note that the No_Initialization
1895 -- flag usually means that the initialization has been rewritten into
1896 -- assignments, but that still counts for us.
1899 Decl
: constant Node_Id
:= Declaration_Node
(Ent
);
1901 if Nkind
(Decl
) = N_Object_Declaration
1902 and then (Present
(Expression
(Decl
))
1903 or else No_Initialization
(Decl
))
1909 -- Here is where we give the warning
1911 -- All OK if warnings suppressed on the entity
1913 if not Has_Warnings_Off
(Ent
) then
1914 Error_Msg_Sloc
:= Sloc
(Ent
);
1917 ("??& can be accessed by clients before this initialization",
1920 ("\??add Elaborate_Body to spec to ensure & is initialized",
1924 if not All_Errors_Mode
then
1925 Set_Suppress_Elaboration_Warnings
(Ent
);
1928 end Check_Elab_Assign
;
1930 ----------------------
1931 -- Check_Elab_Calls --
1932 ----------------------
1934 procedure Check_Elab_Calls
is
1936 -- If expansion is disabled, do not generate any checks. Also skip
1937 -- checks if any subunits are missing because in either case we lack the
1938 -- full information that we need, and no object file will be created in
1941 if not Expander_Active
1942 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
1943 or else Subunits_Missing
1948 -- Skip delayed calls if we had any errors
1950 if Serious_Errors_Detected
= 0 then
1951 Delaying_Elab_Checks
:= False;
1952 Expander_Mode_Save_And_Set
(True);
1954 for J
in Delay_Check
.First
.. Delay_Check
.Last
loop
1955 Push_Scope
(Delay_Check
.Table
(J
).Curscop
);
1956 From_Elab_Code
:= Delay_Check
.Table
(J
).From_Elab_Code
;
1958 Check_Internal_Call_Continue
(
1959 N
=> Delay_Check
.Table
(J
).N
,
1960 E
=> Delay_Check
.Table
(J
).E
,
1961 Outer_Scope
=> Delay_Check
.Table
(J
).Outer_Scope
,
1962 Orig_Ent
=> Delay_Check
.Table
(J
).Orig_Ent
);
1967 -- Set Delaying_Elab_Checks back on for next main compilation
1969 Expander_Mode_Restore
;
1970 Delaying_Elab_Checks
:= True;
1972 end Check_Elab_Calls
;
1974 ------------------------------
1975 -- Check_Elab_Instantiation --
1976 ------------------------------
1978 procedure Check_Elab_Instantiation
1980 Outer_Scope
: Entity_Id
:= Empty
)
1985 -- Check for and deal with bad instantiation case. There is some
1986 -- duplicated code here, but we will worry about this later ???
1988 Check_Bad_Instantiation
(N
);
1990 if ABE_Is_Certain
(N
) then
1994 -- Nothing to do if we do not have an instantiation (happens in some
1995 -- error cases, and also in the formal package declaration case)
1997 if Nkind
(N
) not in N_Generic_Instantiation
then
2001 -- Nothing to do if inside a generic template
2003 if Inside_A_Generic
then
2007 -- Nothing to do if the instantiation is not in the main unit
2009 if not In_Extended_Main_Code_Unit
(N
) then
2013 Ent
:= Get_Generic_Entity
(N
);
2014 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
2016 -- See if we need to analyze this instantiation. We analyze it if
2017 -- either of the following conditions is met:
2019 -- It is an inner level instantiation (since in this case it was
2020 -- triggered by an outer level call from elaboration code), but
2021 -- only if the instantiation is within the scope of the original
2022 -- outer level call.
2024 -- It is an outer level instantiation from elaboration code, or the
2025 -- instantiated entity is in the same elaboration scope.
2027 -- And in these cases, we will check both the inter-unit case and
2028 -- the intra-unit (within a single unit) case.
2030 C_Scope
:= Current_Scope
;
2032 if Present
(Outer_Scope
) and then Within
(Scope
(Ent
), Outer_Scope
) then
2034 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
2036 elsif From_Elab_Code
then
2038 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
2040 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
2042 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
2044 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is
2045 -- set, then we will do the check, but only in the inter-unit case (this
2046 -- is to accommodate unguarded elaboration calls from other units in
2047 -- which this same mode is set). We inhibit warnings in this case, since
2048 -- this instantiation is not occurring in elaboration code.
2050 elsif Dynamic_Elaboration_Checks
then
2056 Inter_Unit_Only
=> True,
2057 Generate_Warnings
=> False);
2062 end Check_Elab_Instantiation
;
2064 -------------------------
2065 -- Check_Internal_Call --
2066 -------------------------
2068 procedure Check_Internal_Call
2071 Outer_Scope
: Entity_Id
;
2072 Orig_Ent
: Entity_Id
)
2074 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean;
2075 -- Determine whether call Call occurs within pragma Initial_Condition or
2076 -- pragma Check with check_kind set to Initial_Condition.
2078 ------------------------------
2079 -- Within_Initial_Condition --
2080 ------------------------------
2082 function Within_Initial_Condition
(Call
: Node_Id
) return Boolean is
2088 -- Traverse the parent chain looking for an enclosing pragma
2091 while Present
(Par
) loop
2092 if Nkind
(Par
) = N_Pragma
then
2093 Nam
:= Pragma_Name
(Par
);
2095 -- Pragma Initial_Condition appears in its alternative from as
2096 -- Check (Initial_Condition, ...).
2098 if Nam
= Name_Check
then
2099 Args
:= Pragma_Argument_Associations
(Par
);
2101 -- Pragma Check should have at least two arguments
2103 pragma Assert
(Present
(Args
));
2106 Chars
(Expression
(First
(Args
))) = Name_Initial_Condition
;
2110 elsif Nam
= Name_Initial_Condition
then
2113 -- Since pragmas are never nested within other pragmas, stop
2120 -- Prevent the search from going too far
2122 elsif Is_Body_Or_Package_Declaration
(Par
) then
2126 Par
:= Parent
(Par
);
2130 end Within_Initial_Condition
;
2134 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
2136 -- Start of processing for Check_Internal_Call
2139 -- For P'Access, we want to warn if the -gnatw.f switch is set, and the
2140 -- node comes from source.
2142 if Nkind
(N
) = N_Attribute_Reference
2143 and then (not Warn_On_Elab_Access
or else not Comes_From_Source
(N
))
2147 -- If not function or procedure call, instantiation, or 'Access, then
2148 -- ignore call (this happens in some error cases and rewriting cases).
2150 elsif not Nkind_In
(N
, N_Attribute_Reference
,
2152 N_Procedure_Call_Statement
)
2153 and then not Inst_Case
2157 -- Nothing to do if this is a call or instantiation that has already
2158 -- been found to be a sure ABE.
2160 elsif Nkind
(N
) /= N_Attribute_Reference
and then ABE_Is_Certain
(N
) then
2163 -- Nothing to do if errors already detected (avoid cascaded errors)
2165 elsif Serious_Errors_Detected
/= 0 then
2168 -- Nothing to do if not in full analysis mode
2170 elsif not Full_Analysis
then
2173 -- Nothing to do if analyzing in special spec-expression mode, since the
2174 -- call is not actually being made at this time.
2176 elsif In_Spec_Expression
then
2179 -- Nothing to do for call to intrinsic subprogram
2181 elsif Is_Intrinsic_Subprogram
(E
) then
2184 -- No need to trace local calls if checking task activation, because
2185 -- other local bodies are elaborated already.
2187 elsif In_Task_Activation
then
2190 -- Nothing to do if call is within a generic unit
2192 elsif Inside_A_Generic
then
2195 -- Nothing to do when the call appears within pragma Initial_Condition.
2196 -- The pragma is part of the elaboration statements of a package body
2197 -- and may only call external subprograms or subprograms whose body is
2198 -- already available.
2200 elsif Within_Initial_Condition
(N
) then
2204 -- Delay this call if we are still delaying calls
2206 if Delaying_Elab_Checks
then
2207 Delay_Check
.Append
(
2210 Orig_Ent
=> Orig_Ent
,
2211 Curscop
=> Current_Scope
,
2212 Outer_Scope
=> Outer_Scope
,
2213 From_Elab_Code
=> From_Elab_Code
));
2216 -- Otherwise, call phase 2 continuation right now
2219 Check_Internal_Call_Continue
(N
, E
, Outer_Scope
, Orig_Ent
);
2221 end Check_Internal_Call
;
2223 ----------------------------------
2224 -- Check_Internal_Call_Continue --
2225 ----------------------------------
2227 procedure Check_Internal_Call_Continue
2230 Outer_Scope
: Entity_Id
;
2231 Orig_Ent
: Entity_Id
)
2233 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
;
2234 -- Function applied to each node as we traverse the body. Checks for
2235 -- call or entity reference that needs checking, and if so checks it.
2236 -- Always returns OK, so entire tree is traversed, except that as
2237 -- described below subprogram bodies are skipped for now.
2239 procedure Traverse
is new Atree
.Traverse_Proc
(Find_Elab_Reference
);
2240 -- Traverse procedure using above Find_Elab_Reference function
2242 -------------------------
2243 -- Find_Elab_Reference --
2244 -------------------------
2246 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
is
2250 -- If user has specified that there are no entry calls in elaboration
2251 -- code, do not trace past an accept statement, because the rendez-
2252 -- vous will happen after elaboration.
2254 if Nkind_In
(Original_Node
(N
), N_Accept_Statement
,
2256 and then Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
2260 -- If we have a function call, check it
2262 elsif Nkind
(N
) = N_Function_Call
then
2263 Check_Elab_Call
(N
, Outer_Scope
);
2266 -- If we have a procedure call, check the call, and also check
2267 -- arguments that are assignments (OUT or IN OUT mode formals).
2269 elsif Nkind
(N
) = N_Procedure_Call_Statement
then
2270 Check_Elab_Call
(N
, Outer_Scope
, In_Init_Proc
=> Is_Init_Proc
(E
));
2272 Actual
:= First_Actual
(N
);
2273 while Present
(Actual
) loop
2274 if Known_To_Be_Assigned
(Actual
) then
2275 Check_Elab_Assign
(Actual
);
2278 Next_Actual
(Actual
);
2283 -- If we have an access attribute for a subprogram, check it.
2284 -- Suppress this behavior under debug flag.
2286 elsif not Debug_Flag_Dot_UU
2287 and then Nkind
(N
) = N_Attribute_Reference
2288 and then Nam_In
(Attribute_Name
(N
), Name_Access
,
2289 Name_Unrestricted_Access
)
2290 and then Is_Entity_Name
(Prefix
(N
))
2291 and then Is_Subprogram
(Entity
(Prefix
(N
)))
2293 Check_Elab_Call
(N
, Outer_Scope
);
2296 -- In SPARK mode, if we have an entity reference to a variable, then
2297 -- check it. For now we consider any reference.
2299 elsif SPARK_Mode
= On
2300 and then Nkind
(N
) in N_Has_Entity
2301 and then Present
(Entity
(N
))
2302 and then Ekind
(Entity
(N
)) = E_Variable
2304 Check_Elab_Call
(N
, Outer_Scope
);
2307 -- If we have a generic instantiation, check it
2309 elsif Nkind
(N
) in N_Generic_Instantiation
then
2310 Check_Elab_Instantiation
(N
, Outer_Scope
);
2313 -- Skip subprogram bodies that come from source (wait for call to
2314 -- analyze these). The reason for the come from source test is to
2315 -- avoid catching task bodies.
2317 -- For task bodies, we should really avoid these too, waiting for the
2318 -- task activation, but that's too much trouble to catch for now, so
2319 -- we go in unconditionally. This is not so terrible, it means the
2320 -- error backtrace is not quite complete, and we are too eager to
2321 -- scan bodies of tasks that are unused, but this is hardly very
2324 elsif Nkind
(N
) = N_Subprogram_Body
2325 and then Comes_From_Source
(N
)
2329 elsif Nkind
(N
) = N_Assignment_Statement
2330 and then Comes_From_Source
(N
)
2332 Check_Elab_Assign
(Name
(N
));
2338 end Find_Elab_Reference
;
2340 Inst_Case
: constant Boolean := Is_Generic_Unit
(E
);
2341 Loc
: constant Source_Ptr
:= Sloc
(N
);
2346 -- Start of processing for Check_Internal_Call_Continue
2349 -- Save outer level call if at outer level
2351 if Elab_Call
.Last
= 0 then
2352 Outer_Level_Sloc
:= Loc
;
2355 Elab_Visited
.Append
(E
);
2357 -- If the call is to a function that renames a literal, no check needed
2359 if Ekind
(E
) = E_Enumeration_Literal
then
2363 Sbody
:= Unit_Declaration_Node
(E
);
2365 if not Nkind_In
(Sbody
, N_Subprogram_Body
, N_Package_Body
) then
2366 Ebody
:= Corresponding_Body
(Sbody
);
2371 Sbody
:= Unit_Declaration_Node
(Ebody
);
2375 -- If the body appears after the outer level call or instantiation then
2376 -- we have an error case handled below.
2378 if Earlier_In_Extended_Unit
(Outer_Level_Sloc
, Sloc
(Sbody
))
2379 and then not In_Task_Activation
2383 -- If we have the instantiation case we are done, since we now
2384 -- know that the body of the generic appeared earlier.
2386 elsif Inst_Case
then
2389 -- Otherwise we have a call, so we trace through the called body to see
2390 -- if it has any problems.
2393 pragma Assert
(Nkind
(Sbody
) = N_Subprogram_Body
);
2395 Elab_Call
.Append
((Cloc
=> Loc
, Ent
=> E
));
2397 if Debug_Flag_LL
then
2398 Write_Str
("Elab_Call.Last = ");
2399 Write_Int
(Int
(Elab_Call
.Last
));
2400 Write_Str
(" Ent = ");
2401 Write_Name
(Chars
(E
));
2403 Write_Location
(Sloc
(N
));
2407 -- Now traverse declarations and statements of subprogram body. Note
2408 -- that we cannot simply Traverse (Sbody), since traverse does not
2409 -- normally visit subprogram bodies.
2414 Decl
:= First
(Declarations
(Sbody
));
2415 while Present
(Decl
) loop
2421 Traverse
(Handled_Statement_Sequence
(Sbody
));
2423 Elab_Call
.Decrement_Last
;
2427 -- Here is the case of calling a subprogram where the body has not yet
2428 -- been encountered. A warning message is needed, except if this is the
2429 -- case of appearing within an aspect specification that results in
2430 -- a check call, we do not really have such a situation, so no warning
2431 -- is needed (e.g. the case of a precondition, where the call appears
2432 -- textually before the body, but in actual fact is moved to the
2433 -- appropriate subprogram body and so does not need a check).
2442 -- Keep looking at parents if we are still in the subexpression
2444 if Nkind
(P
) in N_Subexpr
then
2447 -- Here P is the parent of the expression, check for special case
2450 O
:= Original_Node
(P
);
2452 -- Definitely not the special case if orig node is not a pragma
2454 exit when Nkind
(O
) /= N_Pragma
;
2456 -- Check we have an If statement or a null statement (happens
2457 -- when the If has been expanded to be True).
2459 exit when not Nkind_In
(P
, N_If_Statement
, N_Null_Statement
);
2461 -- Our special case will be indicated either by the pragma
2462 -- coming from an aspect ...
2464 if Present
(Corresponding_Aspect
(O
)) then
2467 -- Or, in the case of an initial condition, specifically by a
2468 -- Check pragma specifying an Initial_Condition check.
2470 elsif Pragma_Name
(O
) = Name_Check
2473 (Expression
(First
(Pragma_Argument_Associations
(O
)))) =
2474 Name_Initial_Condition
2478 -- For anything else, we have an error
2487 -- Not that special case, warning and dynamic check is required
2489 -- If we have nothing in the call stack, then this is at the outer
2490 -- level, and the ABE is bound to occur, unless it's a 'Access, or
2493 if Elab_Call
.Last
= 0 then
2494 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2497 Insert_Check
: Boolean := True;
2498 -- This flag is set to True if an elaboration check should be
2504 ("cannot instantiate& before body seen<<", N
, Orig_Ent
);
2506 elsif Nkind
(N
) = N_Attribute_Reference
then
2508 ("Access attribute of & before body seen<<", N
, Orig_Ent
);
2509 Error_Msg_N
("\possible Program_Error on later references<", N
);
2510 Insert_Check
:= False;
2512 elsif Nkind
(Unit_Declaration_Node
(Orig_Ent
)) /=
2513 N_Subprogram_Renaming_Declaration
2516 ("cannot call& before body seen<<", N
, Orig_Ent
);
2518 elsif not Is_Generic_Actual_Subprogram
(Orig_Ent
) then
2519 Insert_Check
:= False;
2522 if Insert_Check
then
2523 Error_Msg_N
("\Program_Error [<<", N
);
2524 Insert_Elab_Check
(N
);
2528 -- Call is not at outer level
2531 -- Deal with dynamic elaboration check
2533 if not Elaboration_Checks_Suppressed
(E
) then
2534 Set_Elaboration_Entity_Required
(E
);
2536 -- Case of no elaboration entity allocated yet
2538 if No
(Elaboration_Entity
(E
)) then
2540 -- Create object declaration for elaboration entity, and put it
2541 -- just in front of the spec of the subprogram or generic unit,
2542 -- in the same scope as this unit. The subprogram may be over-
2543 -- loaded, so make the name of elaboration entity unique by
2544 -- means of a numeric suffix.
2547 Loce
: constant Source_Ptr
:= Sloc
(E
);
2548 Ent
: constant Entity_Id
:=
2549 Make_Defining_Identifier
(Loc
,
2550 Chars
=> New_External_Name
(Chars
(E
), 'E', -1));
2553 Set_Elaboration_Entity
(E
, Ent
);
2554 Push_Scope
(Scope
(E
));
2556 Insert_Action
(Declaration_Node
(E
),
2557 Make_Object_Declaration
(Loce
,
2558 Defining_Identifier
=> Ent
,
2559 Object_Definition
=>
2560 New_Occurrence_Of
(Standard_Short_Integer
, Loce
),
2562 Make_Integer_Literal
(Loc
, Uint_0
)));
2564 -- Set elaboration flag at the point of the body
2566 Set_Elaboration_Flag
(Sbody
, E
);
2568 -- Kill current value indication. This is necessary because
2569 -- the tests of this flag are inserted out of sequence and
2570 -- must not pick up bogus indications of the wrong constant
2571 -- value. Also, this is never a true constant, since one way
2572 -- or another, it gets reset.
2574 Set_Current_Value
(Ent
, Empty
);
2575 Set_Last_Assignment
(Ent
, Empty
);
2576 Set_Is_True_Constant
(Ent
, False);
2581 -- Generate check of the elaboration counter
2583 Insert_Elab_Check
(N
,
2584 Make_Attribute_Reference
(Loc
,
2585 Attribute_Name
=> Name_Elaborated
,
2586 Prefix
=> New_Occurrence_Of
(E
, Loc
)));
2589 -- Generate the warning
2591 if not Suppress_Elaboration_Warnings
(E
)
2592 and then not Elaboration_Checks_Suppressed
(E
)
2594 -- Suppress this warning if we have a function call that occurred
2595 -- within an assertion expression, since we can get false warnings
2596 -- in this case, due to the out of order handling in this case.
2599 (Nkind
(Original_Node
(N
)) /= N_Function_Call
2600 or else not In_Assertion_Expression_Pragma
(Original_Node
(N
)))
2602 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2606 ("instantiation of& may occur before body is seen<l<",
2609 -- A rather specific check. For Finalize/Adjust/Initialize,
2610 -- if the type has Warnings_Off set, suppress the warning.
2612 if Nam_In
(Chars
(E
), Name_Adjust
,
2615 and then Present
(First_Formal
(E
))
2618 T
: constant Entity_Id
:= Etype
(First_Formal
(E
));
2620 if Is_Controlled
(T
) then
2622 or else (Ekind
(T
) = E_Private_Type
2623 and then Warnings_Off
(Full_View
(T
)))
2631 -- Go ahead and give warning if not this special case
2634 ("call to& may occur before body is seen<l<", N
, Orig_Ent
);
2637 Error_Msg_N
("\Program_Error ]<l<", N
);
2639 -- There is no need to query the elaboration warning message flags
2640 -- because the main message is an error, not a warning, therefore
2641 -- all the clarification messages produces by Output_Calls must be
2642 -- emitted unconditionally.
2646 Output_Calls
(N
, Check_Elab_Flag
=> False);
2650 -- Set flag to suppress further warnings on same subprogram
2651 -- unless in all errors mode
2653 if not All_Errors_Mode
then
2654 Set_Suppress_Elaboration_Warnings
(E
);
2656 end Check_Internal_Call_Continue
;
2658 ---------------------------
2659 -- Check_Task_Activation --
2660 ---------------------------
2662 procedure Check_Task_Activation
(N
: Node_Id
) is
2663 Loc
: constant Source_Ptr
:= Sloc
(N
);
2664 Inter_Procs
: constant Elist_Id
:= New_Elmt_List
;
2665 Intra_Procs
: constant Elist_Id
:= New_Elmt_List
;
2668 Task_Scope
: Entity_Id
;
2669 Cunit_SC
: Boolean := False;
2672 Enclosing
: Entity_Id
;
2674 procedure Add_Task_Proc
(Typ
: Entity_Id
);
2675 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2676 -- For record types, this procedure recurses over component types.
2678 procedure Collect_Tasks
(Decls
: List_Id
);
2679 -- Collect the types of the tasks that are to be activated in the given
2680 -- list of declarations, in order to perform elaboration checks on the
2681 -- corresponding task procedures which are called implicitly here.
2683 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
;
2684 -- find enclosing compilation unit of Entity, ignoring subunits, or
2685 -- else enclosing subprogram. If E is not a package, there is no need
2686 -- for inter-unit elaboration checks.
2692 procedure Add_Task_Proc
(Typ
: Entity_Id
) is
2694 Proc
: Entity_Id
:= Empty
;
2697 if Is_Task_Type
(Typ
) then
2698 Proc
:= Get_Task_Body_Procedure
(Typ
);
2700 elsif Is_Array_Type
(Typ
)
2701 and then Has_Task
(Base_Type
(Typ
))
2703 Add_Task_Proc
(Component_Type
(Typ
));
2705 elsif Is_Record_Type
(Typ
)
2706 and then Has_Task
(Base_Type
(Typ
))
2708 Comp
:= First_Component
(Typ
);
2709 while Present
(Comp
) loop
2710 Add_Task_Proc
(Etype
(Comp
));
2711 Comp
:= Next_Component
(Comp
);
2715 -- If the task type is another unit, we will perform the usual
2716 -- elaboration check on its enclosing unit. If the type is in the
2717 -- same unit, we can trace the task body as for an internal call,
2718 -- but we only need to examine other external calls, because at
2719 -- the point the task is activated, internal subprogram bodies
2720 -- will have been elaborated already. We keep separate lists for
2721 -- each kind of task.
2723 -- Skip this test if errors have occurred, since in this case
2724 -- we can get false indications.
2726 if Serious_Errors_Detected
/= 0 then
2730 if Present
(Proc
) then
2731 if Outer_Unit
(Scope
(Proc
)) = Enclosing
then
2733 if No
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2735 (not Is_Generic_Instance
(Scope
(Proc
))
2736 or else Scope
(Proc
) = Scope
(Defining_Identifier
(Decl
)))
2738 Error_Msg_Warn
:= SPARK_Mode
/= On
;
2740 ("task will be activated before elaboration of its body<<",
2742 Error_Msg_N
("\Program_Error [<<", Decl
);
2745 (Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2747 Append_Elmt
(Proc
, Intra_Procs
);
2751 -- No need for multiple entries of the same type
2753 Elmt
:= First_Elmt
(Inter_Procs
);
2754 while Present
(Elmt
) loop
2755 if Node
(Elmt
) = Proc
then
2762 Append_Elmt
(Proc
, Inter_Procs
);
2771 procedure Collect_Tasks
(Decls
: List_Id
) is
2773 if Present
(Decls
) then
2774 Decl
:= First
(Decls
);
2775 while Present
(Decl
) loop
2776 if Nkind
(Decl
) = N_Object_Declaration
2777 and then Has_Task
(Etype
(Defining_Identifier
(Decl
)))
2779 Add_Task_Proc
(Etype
(Defining_Identifier
(Decl
)));
2791 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
is
2796 while Present
(Outer
) loop
2797 if Elaboration_Checks_Suppressed
(Outer
) then
2801 exit when Is_Child_Unit
(Outer
)
2802 or else Scope
(Outer
) = Standard_Standard
2803 or else Ekind
(Outer
) /= E_Package
;
2804 Outer
:= Scope
(Outer
);
2810 -- Start of processing for Check_Task_Activation
2813 Enclosing
:= Outer_Unit
(Current_Scope
);
2815 -- Find all tasks declared in the current unit
2817 if Nkind
(N
) = N_Package_Body
then
2818 P
:= Unit_Declaration_Node
(Corresponding_Spec
(N
));
2820 Collect_Tasks
(Declarations
(N
));
2821 Collect_Tasks
(Visible_Declarations
(Specification
(P
)));
2822 Collect_Tasks
(Private_Declarations
(Specification
(P
)));
2824 elsif Nkind
(N
) = N_Package_Declaration
then
2825 Collect_Tasks
(Visible_Declarations
(Specification
(N
)));
2826 Collect_Tasks
(Private_Declarations
(Specification
(N
)));
2829 Collect_Tasks
(Declarations
(N
));
2832 -- We only perform detailed checks in all tasks that are library level
2833 -- entities. If the master is a subprogram or task, activation will
2834 -- depend on the activation of the master itself.
2836 -- Should dynamic checks be added in the more general case???
2838 if Ekind
(Enclosing
) /= E_Package
then
2842 -- For task types defined in other units, we want the unit containing
2843 -- the task body to be elaborated before the current one.
2845 Elmt
:= First_Elmt
(Inter_Procs
);
2846 while Present
(Elmt
) loop
2848 Task_Scope
:= Outer_Unit
(Scope
(Ent
));
2850 if not Is_Compilation_Unit
(Task_Scope
) then
2853 elsif Suppress_Elaboration_Warnings
(Task_Scope
)
2854 or else Elaboration_Checks_Suppressed
(Task_Scope
)
2858 elsif Dynamic_Elaboration_Checks
then
2859 if not Elaboration_Checks_Suppressed
(Ent
)
2860 and then not Cunit_SC
2862 not Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
2864 -- Runtime elaboration check required. Generate check of the
2865 -- elaboration counter for the unit containing the entity.
2867 Insert_Elab_Check
(N
,
2868 Make_Attribute_Reference
(Loc
,
2869 Attribute_Name
=> Name_Elaborated
,
2871 New_Occurrence_Of
(Spec_Entity
(Task_Scope
), Loc
)));
2875 -- Force the binder to elaborate other unit first
2877 if not Suppress_Elaboration_Warnings
(Ent
)
2878 and then not Elaboration_Checks_Suppressed
(Ent
)
2879 and then Elab_Info_Messages
2880 and then not Suppress_Elaboration_Warnings
(Task_Scope
)
2881 and then not Elaboration_Checks_Suppressed
(Task_Scope
)
2883 Error_Msg_Node_2
:= Task_Scope
;
2885 ("info: activation of an instance of task type&" &
2886 " requires pragma Elaborate_All on &?$?", N
, Ent
);
2889 Activate_Elaborate_All_Desirable
(N
, Task_Scope
);
2890 Set_Suppress_Elaboration_Warnings
(Task_Scope
);
2896 -- For tasks declared in the current unit, trace other calls within
2897 -- the task procedure bodies, which are available.
2899 In_Task_Activation
:= True;
2901 Elmt
:= First_Elmt
(Intra_Procs
);
2902 while Present
(Elmt
) loop
2904 Check_Internal_Call_Continue
(N
, Ent
, Enclosing
, Ent
);
2908 In_Task_Activation
:= False;
2909 end Check_Task_Activation
;
2911 -------------------------------
2912 -- Is_Call_Of_Generic_Formal --
2913 -------------------------------
2915 function Is_Call_Of_Generic_Formal
(N
: Node_Id
) return Boolean is
2917 return Nkind_In
(N
, N_Function_Call
, N_Procedure_Call_Statement
)
2919 -- Always return False if debug flag -gnatd.G is set
2921 and then not Debug_Flag_Dot_GG
2923 -- For now, we detect this by looking for the strange identifier
2924 -- node, whose Chars reflect the name of the generic formal, but
2925 -- the Chars of the Entity references the generic actual.
2927 and then Nkind
(Name
(N
)) = N_Identifier
2928 and then Chars
(Name
(N
)) /= Chars
(Entity
(Name
(N
)));
2929 end Is_Call_Of_Generic_Formal
;
2931 --------------------------------
2932 -- Set_Elaboration_Constraint --
2933 --------------------------------
2935 procedure Set_Elaboration_Constraint
2940 Elab_Unit
: Entity_Id
;
2942 -- Check whether this is a call to an Initialize subprogram for a
2943 -- controlled type. Note that Call can also be a 'Access attribute
2944 -- reference, which now generates an elaboration check.
2946 Init_Call
: constant Boolean :=
2947 Nkind
(Call
) = N_Procedure_Call_Statement
2948 and then Chars
(Subp
) = Name_Initialize
2949 and then Comes_From_Source
(Subp
)
2950 and then Present
(Parameter_Associations
(Call
))
2951 and then Is_Controlled
(Etype
(First_Actual
(Call
)));
2953 -- If the unit is mentioned in a with_clause of the current unit, it is
2954 -- visible, and we can set the elaboration flag.
2956 if Is_Immediately_Visible
(Scop
)
2957 or else (Is_Child_Unit
(Scop
) and then Is_Visible_Lib_Unit
(Scop
))
2959 Activate_Elaborate_All_Desirable
(Call
, Scop
);
2960 Set_Suppress_Elaboration_Warnings
(Scop
, True);
2964 -- If this is not an initialization call or a call using object notation
2965 -- we know that the unit of the called entity is in the context, and
2966 -- we can set the flag as well. The unit need not be visible if the call
2967 -- occurs within an instantiation.
2969 if Is_Init_Proc
(Subp
)
2971 or else Nkind
(Original_Node
(Call
)) = N_Selected_Component
2973 null; -- detailed processing follows.
2976 Activate_Elaborate_All_Desirable
(Call
, Scop
);
2977 Set_Suppress_Elaboration_Warnings
(Scop
, True);
2981 -- If the unit is not in the context, there must be an intermediate unit
2982 -- that is, on which we need to place to elaboration flag. This happens
2983 -- with init proc calls.
2985 if Is_Init_Proc
(Subp
) or else Init_Call
then
2987 -- The initialization call is on an object whose type is not declared
2988 -- in the same scope as the subprogram. The type of the object must
2989 -- be a subtype of the type of operation. This object is the first
2990 -- actual in the call.
2993 Typ
: constant Entity_Id
:=
2994 Etype
(First
(Parameter_Associations
(Call
)));
2996 Elab_Unit
:= Scope
(Typ
);
2997 while (Present
(Elab_Unit
))
2998 and then not Is_Compilation_Unit
(Elab_Unit
)
3000 Elab_Unit
:= Scope
(Elab_Unit
);
3004 -- If original node uses selected component notation, the prefix is
3005 -- visible and determines the scope that must be elaborated. After
3006 -- rewriting, the prefix is the first actual in the call.
3008 elsif Nkind
(Original_Node
(Call
)) = N_Selected_Component
then
3009 Elab_Unit
:= Scope
(Etype
(First
(Parameter_Associations
(Call
))));
3011 -- Not one of special cases above
3014 -- Using previously computed scope. If the elaboration check is
3015 -- done after analysis, the scope is not visible any longer, but
3016 -- must still be in the context.
3021 Activate_Elaborate_All_Desirable
(Call
, Elab_Unit
);
3022 Set_Suppress_Elaboration_Warnings
(Elab_Unit
, True);
3023 end Set_Elaboration_Constraint
;
3025 ------------------------
3026 -- Get_Referenced_Ent --
3027 ------------------------
3029 function Get_Referenced_Ent
(N
: Node_Id
) return Entity_Id
is
3033 if Nkind
(N
) in N_Has_Entity
3034 and then Present
(Entity
(N
))
3035 and then Ekind
(Entity
(N
)) = E_Variable
3040 if Nkind
(N
) = N_Attribute_Reference
then
3048 elsif Nkind
(Nam
) = N_Selected_Component
then
3049 return Entity
(Selector_Name
(Nam
));
3050 elsif not Is_Entity_Name
(Nam
) then
3053 return Entity
(Nam
);
3055 end Get_Referenced_Ent
;
3057 ----------------------
3058 -- Has_Generic_Body --
3059 ----------------------
3061 function Has_Generic_Body
(N
: Node_Id
) return Boolean is
3062 Ent
: constant Entity_Id
:= Get_Generic_Entity
(N
);
3063 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
3066 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
;
3067 -- Determine if the list of nodes headed by N and linked by Next
3068 -- contains a package body for the package spec entity E, and if so
3069 -- return the package body. If not, then returns Empty.
3071 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
;
3072 -- This procedure is called load the unit whose name is given by Nam.
3073 -- This unit is being loaded to see whether it contains an optional
3074 -- generic body. The returned value is the loaded unit, which is always
3075 -- a package body (only package bodies can contain other entities in the
3076 -- sense in which Has_Generic_Body is interested). We only attempt to
3077 -- load bodies if we are generating code. If we are in semantics check
3078 -- only mode, then it would be wrong to load bodies that are not
3079 -- required from a semantic point of view, so in this case we return
3080 -- Empty. The result is that the caller may incorrectly decide that a
3081 -- generic spec does not have a body when in fact it does, but the only
3082 -- harm in this is that some warnings on elaboration problems may be
3083 -- lost in semantic checks only mode, which is not big loss. We also
3084 -- return Empty if we go for a body and it is not there.
3086 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
;
3087 -- PE is the entity for a package spec. This function locates the
3088 -- corresponding package body, returning Empty if none is found. The
3089 -- package body returned is fully parsed but may not yet be analyzed,
3090 -- so only syntactic fields should be referenced.
3096 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
is
3101 while Present
(Nod
) loop
3103 -- If we found the package body we are looking for, return it
3105 if Nkind
(Nod
) = N_Package_Body
3106 and then Chars
(Defining_Unit_Name
(Nod
)) = Chars
(E
)
3110 -- If we found the stub for the body, go after the subunit,
3111 -- loading it if necessary.
3113 elsif Nkind
(Nod
) = N_Package_Body_Stub
3114 and then Chars
(Defining_Identifier
(Nod
)) = Chars
(E
)
3116 if Present
(Library_Unit
(Nod
)) then
3117 return Unit
(Library_Unit
(Nod
));
3120 return Load_Package_Body
(Get_Unit_Name
(Nod
));
3123 -- If neither package body nor stub, keep looking on chain
3133 -----------------------
3134 -- Load_Package_Body --
3135 -----------------------
3137 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
is
3138 U
: Unit_Number_Type
;
3141 if Operating_Mode
/= Generate_Code
then
3154 return Unit
(Cunit
(U
));
3157 end Load_Package_Body
;
3159 -------------------------------
3160 -- Locate_Corresponding_Body --
3161 -------------------------------
3163 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
is
3164 Spec
: constant Node_Id
:= Declaration_Node
(PE
);
3165 Decl
: constant Node_Id
:= Parent
(Spec
);
3166 Scop
: constant Entity_Id
:= Scope
(PE
);
3170 if Is_Library_Level_Entity
(PE
) then
3172 -- If package is a library unit that requires a body, we have no
3173 -- choice but to go after that body because it might contain an
3174 -- optional body for the original generic package.
3176 if Unit_Requires_Body
(PE
) then
3178 -- Load the body. Note that we are a little careful here to use
3179 -- Spec to get the unit number, rather than PE or Decl, since
3180 -- in the case where the package is itself a library level
3181 -- instantiation, Spec will properly reference the generic
3182 -- template, which is what we really want.
3186 (Get_Body_Name
(Unit_Name
(Get_Source_Unit
(Spec
))));
3188 -- But if the package is a library unit that does NOT require
3189 -- a body, then no body is permitted, so we are sure that there
3190 -- is no body for the original generic package.
3196 -- Otherwise look and see if we are embedded in a further package
3198 elsif Is_Package_Or_Generic_Package
(Scop
) then
3200 -- If so, get the body of the enclosing package, and look in
3201 -- its package body for the package body we are looking for.
3203 PBody
:= Locate_Corresponding_Body
(Scop
);
3208 return Find_Body_In
(PE
, First
(Declarations
(PBody
)));
3211 -- If we are not embedded in a further package, then the body
3212 -- must be in the same declarative part as we are.
3215 return Find_Body_In
(PE
, Next
(Decl
));
3217 end Locate_Corresponding_Body
;
3219 -- Start of processing for Has_Generic_Body
3222 if Present
(Corresponding_Body
(Decl
)) then
3225 elsif Unit_Requires_Body
(Ent
) then
3228 -- Compilation units cannot have optional bodies
3230 elsif Is_Compilation_Unit
(Ent
) then
3233 -- Otherwise look at what scope we are in
3236 Scop
:= Scope
(Ent
);
3238 -- Case of entity is in other than a package spec, in this case
3239 -- the body, if present, must be in the same declarative part.
3241 if not Is_Package_Or_Generic_Package
(Scop
) then
3246 -- Declaration node may get us a spec, so if so, go to
3247 -- the parent declaration.
3249 P
:= Declaration_Node
(Ent
);
3250 while not Is_List_Member
(P
) loop
3254 return Present
(Find_Body_In
(Ent
, Next
(P
)));
3257 -- If the entity is in a package spec, then we have to locate
3258 -- the corresponding package body, and look there.
3262 PBody
: constant Node_Id
:= Locate_Corresponding_Body
(Scop
);
3270 (Find_Body_In
(Ent
, (First
(Declarations
(PBody
)))));
3275 end Has_Generic_Body
;
3277 -----------------------
3278 -- Insert_Elab_Check --
3279 -----------------------
3281 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
) is
3283 Loc
: constant Source_Ptr
:= Sloc
(N
);
3286 -- The check (N_Raise_Program_Error) node to be inserted
3289 -- If expansion is disabled, do not generate any checks. Also
3290 -- skip checks if any subunits are missing because in either
3291 -- case we lack the full information that we need, and no object
3292 -- file will be created in any case.
3294 if not Expander_Active
or else Subunits_Missing
then
3298 -- If we have a generic instantiation, where Instance_Spec is set,
3299 -- then this field points to a generic instance spec that has
3300 -- been inserted before the instantiation node itself, so that
3301 -- is where we want to insert a check.
3303 if Nkind
(N
) in N_Generic_Instantiation
3304 and then Present
(Instance_Spec
(N
))
3306 Nod
:= Instance_Spec
(N
);
3311 -- Build check node, possibly with condition
3314 Make_Raise_Program_Error
(Loc
, Reason
=> PE_Access_Before_Elaboration
);
3317 Set_Condition
(Chk
, Make_Op_Not
(Loc
, Right_Opnd
=> C
));
3320 -- If we are inserting at the top level, insert in Aux_Decls
3322 if Nkind
(Parent
(Nod
)) = N_Compilation_Unit
then
3324 ADN
: constant Node_Id
:= Aux_Decls_Node
(Parent
(Nod
));
3327 if No
(Declarations
(ADN
)) then
3328 Set_Declarations
(ADN
, New_List
(Chk
));
3330 Append_To
(Declarations
(ADN
), Chk
);
3336 -- Otherwise just insert as an action on the node in question
3339 Insert_Action
(Nod
, Chk
);
3341 end Insert_Elab_Check
;
3343 -------------------------------
3344 -- Is_Finalization_Procedure --
3345 -------------------------------
3347 function Is_Finalization_Procedure
(Id
: Entity_Id
) return Boolean is
3349 -- Check whether Id is a procedure with at least one parameter
3351 if Ekind
(Id
) = E_Procedure
and then Present
(First_Formal
(Id
)) then
3353 Typ
: constant Entity_Id
:= Etype
(First_Formal
(Id
));
3354 Deep_Fin
: Entity_Id
:= Empty
;
3355 Fin
: Entity_Id
:= Empty
;
3358 -- If the type of the first formal does not require finalization
3359 -- actions, then this is definitely not [Deep_]Finalize.
3361 if not Needs_Finalization
(Typ
) then
3365 -- At this point we have the following scenario:
3367 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]);
3369 -- Recover the two possible versions of [Deep_]Finalize using the
3370 -- type of the first parameter and compare with the input.
3372 Deep_Fin
:= TSS
(Typ
, TSS_Deep_Finalize
);
3374 if Is_Controlled
(Typ
) then
3375 Fin
:= Find_Prim_Op
(Typ
, Name_Finalize
);
3378 return (Present
(Deep_Fin
) and then Id
= Deep_Fin
)
3379 or else (Present
(Fin
) and then Id
= Fin
);
3384 end Is_Finalization_Procedure
;
3390 procedure Output_Calls
3392 Check_Elab_Flag
: Boolean)
3394 function Emit
(Flag
: Boolean) return Boolean;
3395 -- Determine whether to emit an error message based on the combination
3396 -- of flags Check_Elab_Flag and Flag.
3398 function Is_Printable_Error_Name
return Boolean;
3399 -- An internal function, used to determine if a name, stored in the
3400 -- Name_Buffer, is either a non-internal name, or is an internal name
3401 -- that is printable by the error message circuits (i.e. it has a single
3402 -- upper case letter at the end).
3408 function Emit
(Flag
: Boolean) return Boolean is
3410 if Check_Elab_Flag
then
3417 -----------------------------
3418 -- Is_Printable_Error_Name --
3419 -----------------------------
3421 function Is_Printable_Error_Name
return Boolean is
3423 if not Is_Internal_Name
then
3426 elsif Name_Len
= 1 then
3430 Name_Len
:= Name_Len
- 1;
3431 return not Is_Internal_Name
;
3433 end Is_Printable_Error_Name
;
3439 -- Start of processing for Output_Calls
3442 for J
in reverse 1 .. Elab_Call
.Last
loop
3443 Error_Msg_Sloc
:= Elab_Call
.Table
(J
).Cloc
;
3445 Ent
:= Elab_Call
.Table
(J
).Ent
;
3446 Get_Name_String
(Chars
(Ent
));
3448 -- Dynamic elaboration model, warnings controlled by -gnatwl
3450 if Dynamic_Elaboration_Checks
then
3451 if Emit
(Elab_Warnings
) then
3452 if Is_Generic_Unit
(Ent
) then
3453 Error_Msg_NE
("\\?l?& instantiated #", N
, Ent
);
3454 elsif Is_Init_Proc
(Ent
) then
3455 Error_Msg_N
("\\?l?initialization procedure called #", N
);
3456 elsif Is_Printable_Error_Name
then
3457 Error_Msg_NE
("\\?l?& called #", N
, Ent
);
3459 Error_Msg_N
("\\?l?called #", N
);
3463 -- Static elaboration model, info messages controlled by -gnatel
3466 if Emit
(Elab_Info_Messages
) then
3467 if Is_Generic_Unit
(Ent
) then
3468 Error_Msg_NE
("\\?$?& instantiated #", N
, Ent
);
3469 elsif Is_Init_Proc
(Ent
) then
3470 Error_Msg_N
("\\?$?initialization procedure called #", N
);
3471 elsif Is_Printable_Error_Name
then
3472 Error_Msg_NE
("\\?$?& called #", N
, Ent
);
3474 Error_Msg_N
("\\?$?called #", N
);
3481 ----------------------------
3482 -- Same_Elaboration_Scope --
3483 ----------------------------
3485 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean is
3490 -- Find elaboration scope for Scop1
3491 -- This is either a subprogram or a compilation unit.
3494 while S1
/= Standard_Standard
3495 and then not Is_Compilation_Unit
(S1
)
3496 and then Ekind_In
(S1
, E_Package
, E_Protected_Type
, E_Block
)
3501 -- Find elaboration scope for Scop2
3504 while S2
/= Standard_Standard
3505 and then not Is_Compilation_Unit
(S2
)
3506 and then Ekind_In
(S2
, E_Package
, E_Protected_Type
, E_Block
)
3512 end Same_Elaboration_Scope
;
3518 procedure Set_C_Scope
is
3520 while not Is_Compilation_Unit
(C_Scope
) loop
3521 C_Scope
:= Scope
(C_Scope
);
3529 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
is
3533 -- Check for case of body entity
3534 -- Why is the check for E_Void needed???
3536 if Ekind_In
(E
, E_Void
, E_Subprogram_Body
, E_Package_Body
) then
3540 Decl
:= Parent
(Decl
);
3541 exit when Nkind
(Decl
) in N_Proper_Body
;
3544 return Corresponding_Spec
(Decl
);
3555 procedure Supply_Bodies
(N
: Node_Id
) is
3557 if Nkind
(N
) = N_Subprogram_Declaration
then
3559 Ent
: constant Entity_Id
:= Defining_Unit_Name
(Specification
(N
));
3562 -- Internal subprograms will already have a generated body, so
3563 -- there is no need to provide a stub for them.
3565 if No
(Corresponding_Body
(N
)) then
3567 Loc
: constant Source_Ptr
:= Sloc
(N
);
3569 Formals
: constant List_Id
:= Copy_Parameter_List
(Ent
);
3570 Nam
: constant Entity_Id
:=
3571 Make_Defining_Identifier
(Loc
, Chars
(Ent
));
3573 Stats
: constant List_Id
:=
3575 (Make_Raise_Program_Error
(Loc
,
3576 Reason
=> PE_Access_Before_Elaboration
));
3579 if Ekind
(Ent
) = E_Function
then
3581 Make_Function_Specification
(Loc
,
3582 Defining_Unit_Name
=> Nam
,
3583 Parameter_Specifications
=> Formals
,
3584 Result_Definition
=>
3586 (Result_Definition
(Specification
(N
))));
3588 -- We cannot reliably make a return statement for this
3589 -- body, but none is needed because the call raises
3592 Set_Return_Present
(Ent
);
3596 Make_Procedure_Specification
(Loc
,
3597 Defining_Unit_Name
=> Nam
,
3598 Parameter_Specifications
=> Formals
);
3601 B
:= Make_Subprogram_Body
(Loc
,
3602 Specification
=> Spec
,
3603 Declarations
=> New_List
,
3604 Handled_Statement_Sequence
=>
3605 Make_Handled_Sequence_Of_Statements
(Loc
, Stats
));
3606 Insert_After
(N
, B
);
3612 elsif Nkind
(N
) = N_Package_Declaration
then
3614 Spec
: constant Node_Id
:= Specification
(N
);
3616 Push_Scope
(Defining_Unit_Name
(Spec
));
3617 Supply_Bodies
(Visible_Declarations
(Spec
));
3618 Supply_Bodies
(Private_Declarations
(Spec
));
3624 procedure Supply_Bodies
(L
: List_Id
) is
3629 while Present
(Elmt
) loop
3630 Supply_Bodies
(Elmt
);
3640 function Within
(E1
, E2
: Entity_Id
) return Boolean is
3647 elsif Scop
= Standard_Standard
then
3650 Scop
:= Scope
(Scop
);
3655 --------------------------
3656 -- Within_Elaborate_All --
3657 --------------------------
3659 function Within_Elaborate_All
3660 (Unit
: Unit_Number_Type
;
3661 E
: Entity_Id
) return Boolean
3663 type Unit_Number_Set
is array (Main_Unit
.. Last_Unit
) of Boolean;
3664 pragma Pack
(Unit_Number_Set
);
3666 Seen
: Unit_Number_Set
:= (others => False);
3667 -- Seen (X) is True after we have seen unit X in the walk. This is used
3668 -- to prevent processing the same unit more than once.
3670 Result
: Boolean := False;
3672 procedure Helper
(Unit
: Unit_Number_Type
);
3673 -- This helper procedure does all the work for Within_Elaborate_All. It
3674 -- walks the dependency graph, and sets Result to True if it finds an
3675 -- appropriate Elaborate_All.
3681 procedure Helper
(Unit
: Unit_Number_Type
) is
3682 CU
: constant Node_Id
:= Cunit
(Unit
);
3686 Elab_Id
: Entity_Id
;
3693 Seen
(Unit
) := True;
3696 -- First, check for Elaborate_Alls on this unit
3698 Item
:= First
(Context_Items
(CU
));
3699 while Present
(Item
) loop
3700 if Nkind
(Item
) = N_Pragma
3701 and then Pragma_Name
(Item
) = Name_Elaborate_All
3703 -- Return if some previous error on the pragma itself. The
3704 -- pragma may be unanalyzed, because of a previous error, or
3705 -- if it is the context of a subunit, inherited by its parent.
3707 if Error_Posted
(Item
) or else not Analyzed
(Item
) then
3713 (Expression
(First
(Pragma_Argument_Associations
(Item
))));
3720 Par
:= Parent
(Unit_Declaration_Node
(Elab_Id
));
3722 Item2
:= First
(Context_Items
(Par
));
3723 while Present
(Item2
) loop
3724 if Nkind
(Item2
) = N_With_Clause
3725 and then Entity
(Name
(Item2
)) = E
3726 and then not Limited_Present
(Item2
)
3739 -- Second, recurse on with's. We could do this as part of the above
3740 -- loop, but it's probably more efficient to have two loops, because
3741 -- the relevant Elaborate_All is likely to be on the initial unit. In
3742 -- other words, we're walking the with's breadth-first. This part is
3743 -- only necessary in the dynamic elaboration model.
3745 if Dynamic_Elaboration_Checks
then
3746 Item
:= First
(Context_Items
(CU
));
3747 while Present
(Item
) loop
3748 if Nkind
(Item
) = N_With_Clause
3749 and then not Limited_Present
(Item
)
3751 -- Note: the following call to Get_Cunit_Unit_Number does a
3752 -- linear search, which could be slow, but it's OK because
3753 -- we're about to give a warning anyway. Also, there might
3754 -- be hundreds of units, but not millions. If it turns out
3755 -- to be a problem, we could store the Get_Cunit_Unit_Number
3756 -- in each N_Compilation_Unit node, but that would involve
3757 -- rearranging N_Compilation_Unit_Aux to make room.
3759 Helper
(Get_Cunit_Unit_Number
(Library_Unit
(Item
)));
3771 -- Start of processing for Within_Elaborate_All
3776 end Within_Elaborate_All
;