1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2007, 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_Cat
; use Sem_Cat
;
47 with Sem_Ch7
; use Sem_Ch7
;
48 with Sem_Ch8
; use Sem_Ch8
;
49 with Sem_Res
; use Sem_Res
;
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 Uname
; use Uname
;
59 package body Sem_Elab
is
61 -- The following table records the recursive call chain for output
62 -- in the Output routine. Each entry records the call node and the
63 -- entity of the called routine. The number of entries in the table
64 -- (i.e. the value of Elab_Call.Last) indicates the current depth
65 -- of recursion and is used to identify the outer level.
67 type Elab_Call_Entry
is record
72 package Elab_Call
is new Table
.Table
(
73 Table_Component_Type
=> Elab_Call_Entry
,
74 Table_Index_Type
=> Int
,
77 Table_Increment
=> 100,
78 Table_Name
=> "Elab_Call");
80 -- This table is initialized at the start of each outer level call.
81 -- It holds the entities for all subprograms that have been examined
82 -- for this particular outer level call, and is used to prevent both
83 -- infinite recursion, and useless reanalysis of bodies already seen
85 package Elab_Visited
is new Table
.Table
(
86 Table_Component_Type
=> Entity_Id
,
87 Table_Index_Type
=> Int
,
90 Table_Increment
=> 100,
91 Table_Name
=> "Elab_Visited");
93 -- This table stores calls to Check_Internal_Call that are delayed
94 -- until all generics are instantiated, and in particular that all
95 -- generic bodies have been inserted. We need to delay, because we
96 -- need to be able to look through the inserted bodies.
98 type Delay_Element
is record
100 -- The parameter N from the call to Check_Internal_Call. Note that
101 -- this node may get rewritten over the delay period by expansion
102 -- in the call case (but not in the instantiation case).
105 -- The parameter E from the call to Check_Internal_Call
107 Orig_Ent
: Entity_Id
;
108 -- The parameter Orig_Ent from the call to Check_Internal_Call
111 -- The current scope of the call. This is restored when we complete
112 -- the delayed call, so that we do this in the right scope.
114 From_Elab_Code
: Boolean;
115 -- Save indication of whether this call is from elaboration code
117 Outer_Scope
: Entity_Id
;
118 -- Save scope of outer level call
121 package Delay_Check
is new Table
.Table
(
122 Table_Component_Type
=> Delay_Element
,
123 Table_Index_Type
=> Int
,
124 Table_Low_Bound
=> 1,
125 Table_Initial
=> 1000,
126 Table_Increment
=> 100,
127 Table_Name
=> "Delay_Check");
130 -- Top level scope of current scope. We need to compute this only
131 -- once at the outer level, i.e. for a call to Check_Elab_Call from
132 -- 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
153 -- called, the delay table is used to make the delayed calls and
154 -- this flag is reset 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 -- This is the internal recursive routine that is called to check for
181 -- a possible elaboration error. The argument N is a subprogram call
182 -- or generic instantiation to be checked, and E is the entity of
183 -- the called subprogram, or instantiated generic unit. The flag
184 -- Outer_Scope is the outer level scope for the original call.
185 -- Inter_Unit_Only is set if the call is only to be checked in the
186 -- case where it is to another unit (and skipped if within a unit).
187 -- Generate_Warnings is set to False to suppress warning messages
188 -- about missing pragma Elaborate_All's. These messages are not
189 -- wanted for inner calls in the dynamic model.
191 procedure Check_Bad_Instantiation
(N
: Node_Id
);
192 -- N is a node for an instantiation (if called with any other node kind,
193 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
194 -- the special case of a generic instantiation of a generic spec in the
195 -- same declarative part as the instantiation where a body is present and
196 -- has not yet been seen. This is an obvious error, but needs to be checked
197 -- specially at the time of the instantiation, since it is a case where we
198 -- cannot insert the body anywhere. If this case is detected, warnings are
199 -- generated, and a raise of Program_Error is inserted. In addition any
200 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
201 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
202 -- flag as an indication that no attempt should be made to insert an
205 procedure Check_Internal_Call
208 Outer_Scope
: Entity_Id
;
209 Orig_Ent
: Entity_Id
);
210 -- N is a function call or procedure statement call node and E is
211 -- the entity of the called function, which is within the current
212 -- compilation unit (where subunits count as part of the parent).
213 -- This call checks if this call, or any call within any accessed
214 -- body could cause an ABE, and if so, outputs a warning. Orig_Ent
215 -- differs from E only in the case of renamings, and points to the
216 -- original name of the entity. This is used for error messages.
217 -- Outer_Scope is the outer level scope for the original call.
219 procedure Check_Internal_Call_Continue
222 Outer_Scope
: Entity_Id
;
223 Orig_Ent
: Entity_Id
);
224 -- The processing for Check_Internal_Call is divided up into two phases,
225 -- and this represents the second phase. The second phase is delayed if
226 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
227 -- phase makes an entry in the Delay_Check table, which is processed
228 -- when Check_Elab_Calls is called. N, E and Orig_Ent are as for the call
229 -- to Check_Internal_Call. Outer_Scope is the outer level scope for
230 -- the original call.
232 procedure Set_Elaboration_Constraint
236 -- The current unit U may depend semantically on some unit P which is not
237 -- in the current context. If there is an elaboration call that reaches P,
238 -- we need to indicate that P requires an Elaborate_All, but this is not
239 -- effective in U's ali file, if there is no with_clause for P. In this
240 -- case we add the Elaborate_All on the unit Q that directly or indirectly
241 -- makes P available. This can happen in two cases:
243 -- a) Q declares a subtype of a type declared in P, and the call is an
244 -- initialization call for an object of that subtype.
246 -- b) Q declares an object of some tagged type whose root type is
247 -- declared in P, and the initialization call uses object notation on
248 -- that object to reach a primitive operation or a classwide operation
251 -- If P appears in the context of U, the current processing is correct.
252 -- Otherwise we must identify these two cases to retrieve Q and place the
253 -- Elaborate_All_Desirable on it.
255 function Has_Generic_Body
(N
: Node_Id
) return Boolean;
256 -- N is a generic package instantiation node, and this routine determines
257 -- if this package spec does in fact have a generic body. If so, then
258 -- True is returned, otherwise False. Note that this is not at all the
259 -- same as checking if the unit requires a body, since it deals with
260 -- the case of optional bodies accurately (i.e. if a body is optional,
261 -- then it looks to see if a body is actually present). Note: this
262 -- function can only do a fully correct job if in generating code mode
263 -- where all bodies have to be present. If we are operating in semantics
264 -- check only mode, then in some cases of optional bodies, a result of
265 -- False may incorrectly be given. In practice this simply means that
266 -- some cases of warnings for incorrect order of elaboration will only
267 -- be given when generating code, which is not a big problem (and is
268 -- inevitable, given the optional body semantics of Ada).
270 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
);
271 -- Given code for an elaboration check (or unconditional raise if
272 -- the check is not needed), inserts the code in the appropriate
273 -- place. N is the call or instantiation node for which the check
274 -- code is required. C is the test whose failure triggers the raise.
276 procedure Output_Calls
(N
: Node_Id
);
277 -- Outputs chain of calls stored in the Elab_Call table. The caller
278 -- has already generated the main warning message, so the warnings
279 -- generated are all continuation messages. The argument is the
280 -- call node at which the messages are to be placed.
282 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean;
283 -- Given two scopes, determine whether they are the same scope from an
284 -- elaboration point of view, i.e. packages and blocks are ignored.
286 procedure Set_C_Scope
;
287 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
288 -- to be the enclosing compilation unit of this scope.
290 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
;
291 -- Given a compilation unit entity, if it is a spec entity, it is
292 -- returned unchanged. If it is a body entity, then the spec for
293 -- the corresponding spec is returned
295 procedure Supply_Bodies
(N
: Node_Id
);
296 -- Given a node, N, that is either a subprogram declaration or a package
297 -- declaration, this procedure supplies dummy bodies for the subprogram
298 -- or for all subprograms in the package. If the given node is not one
299 -- of these two possibilities, then Supply_Bodies does nothing. The
300 -- dummy body is supplied by setting the subprogram to be Imported with
301 -- convention Stubbed.
303 procedure Supply_Bodies
(L
: List_Id
);
304 -- Calls Supply_Bodies for all elements of the given list L
306 function Within
(E1
, E2
: Entity_Id
) return Boolean;
307 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
308 -- of its contained scopes, False otherwise.
310 function Within_Elaborate_All
(E
: Entity_Id
) return Boolean;
311 -- Before emitting a warning on a scope E for a missing elaborate_all,
312 -- check whether E may be in the context of a directly visible unit U to
313 -- which the pragma applies. This prevents spurious warnings when the
314 -- called entity is renamed within U.
316 --------------------------------------
317 -- Activate_Elaborate_All_Desirable --
318 --------------------------------------
320 procedure Activate_Elaborate_All_Desirable
(N
: Node_Id
; U
: Entity_Id
) is
321 UN
: constant Unit_Number_Type
:= Get_Code_Unit
(N
);
322 CU
: constant Node_Id
:= Cunit
(UN
);
323 UE
: constant Entity_Id
:= Cunit_Entity
(UN
);
324 Unm
: constant Unit_Name_Type
:= Unit_Name
(UN
);
325 CI
: constant List_Id
:= Context_Items
(CU
);
329 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
);
330 -- This procedure is called when the elaborate indication must be
331 -- applied to a unit not in the context of the referencing unit. The
332 -- unit gets added to the context as an implicit with.
334 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean;
335 -- UEs is the spec entity of a unit. If the unit to be marked is
336 -- in the context item list of this unit spec, then the call returns
337 -- True and Itm is left set to point to the relevant N_With_Clause node.
339 procedure Set_Elab_Flag
(Itm
: Node_Id
);
340 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
342 -----------------------------
343 -- Add_To_Context_And_Mark --
344 -----------------------------
346 procedure Add_To_Context_And_Mark
(Itm
: Node_Id
) is
347 CW
: constant Node_Id
:=
348 Make_With_Clause
(Sloc
(Itm
),
352 Set_Library_Unit
(CW
, Library_Unit
(Itm
));
353 Set_Implicit_With
(CW
, True);
355 -- Set elaborate all desirable on copy and then append the copy to
356 -- the list of body with's and we are done.
360 end Add_To_Context_And_Mark
;
366 function In_Withs_Of
(UEs
: Entity_Id
) return Boolean is
367 UNs
: constant Unit_Number_Type
:= Get_Source_Unit
(UEs
);
368 CUs
: constant Node_Id
:= Cunit
(UNs
);
369 CIs
: constant List_Id
:= Context_Items
(CUs
);
373 while Present
(Itm
) loop
374 if Nkind
(Itm
) = N_With_Clause
then
376 Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
393 procedure Set_Elab_Flag
(Itm
: Node_Id
) is
395 if Nkind
(N
) in N_Subprogram_Instantiation
then
396 Set_Elaborate_Desirable
(Itm
);
398 Set_Elaborate_All_Desirable
(Itm
);
402 -- Start of processing for Activate_Elaborate_All_Desirable
405 -- Do not set binder indication if expansion is disabled, as when
406 -- compiling a generic unit.
408 if not Expander_Active
then
413 while Present
(Itm
) loop
414 if Nkind
(Itm
) = N_With_Clause
then
415 Ent
:= Cunit_Entity
(Get_Cunit_Unit_Number
(Library_Unit
(Itm
)));
417 -- If we find it, then mark elaborate all desirable and return
428 -- If we fall through then the with clause is not present in the
429 -- current unit. One legitimate possibility is that the with clause
430 -- is present in the spec when we are a body.
432 if Is_Body_Name
(Unm
)
433 and then In_Withs_Of
(Spec_Entity
(UE
))
435 Add_To_Context_And_Mark
(Itm
);
439 -- Similarly, we may be in the spec or body of a child unit, where
440 -- the unit in question is with'ed by some ancestor of the child unit.
442 if Is_Child_Name
(Unm
) then
450 exit when Pkg
= Standard_Standard
;
452 if In_Withs_Of
(Pkg
) then
453 Add_To_Context_And_Mark
(Itm
);
460 -- Here if we do not find with clause on spec or body. We just ignore
461 -- this case, it means that the elaboration involves some other unit
462 -- than the unit being compiled, and will be caught elsewhere.
465 end Activate_Elaborate_All_Desirable
;
471 procedure Check_A_Call
474 Outer_Scope
: Entity_Id
;
475 Inter_Unit_Only
: Boolean;
476 Generate_Warnings
: Boolean := True)
478 Loc
: constant Source_Ptr
:= Sloc
(N
);
483 -- Top level scope of entity for called subprogram. This
484 -- value includes following renamings and derivations, so
485 -- this scope can be in a non-visible unit. This is the
486 -- scope that is to be investigated to see whether an
487 -- elaboration check is required.
490 -- Top level scope of directly called entity for subprogram. This
491 -- differs from E_Scope in the case where renamings or derivations
492 -- are involved, since it does not follow these links. W_Scope is
493 -- generally in a visible unit, and it is this scope that may require
494 -- an Elaborate_All. However, there are some cases (initialization
495 -- calls and calls involving object notation) where W_Scope might not
496 -- be in the context of the current unit, and there is an intermediate
497 -- package that is, in which case the Elaborate_All has to be placed
498 -- on this intedermediate package. These special cases are handled in
499 -- Set_Elaboration_Constraint.
501 Body_Acts_As_Spec
: Boolean;
502 -- Set to true if call is to body acting as spec (no separate spec)
504 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
505 -- Indicates if we have instantiation case
507 Caller_Unit_Internal
: Boolean;
508 Callee_Unit_Internal
: Boolean;
510 Inst_Caller
: Source_Ptr
;
511 Inst_Callee
: Source_Ptr
;
513 Unit_Caller
: Unit_Number_Type
;
514 Unit_Callee
: Unit_Number_Type
;
516 Cunit_SC
: Boolean := False;
517 -- Set to suppress dynamic elaboration checks where one of the
518 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
519 -- if a pragma Elaborate (_All) applies to that scope, in which case
520 -- warnings on the scope are also suppressed. For the internal case,
521 -- we ignore this flag.
524 -- If the call is known to be within a local Suppress Elaboration
525 -- pragma, nothing to check. This can happen in task bodies.
527 if (Nkind
(N
) = N_Function_Call
528 or else Nkind
(N
) = N_Procedure_Call_Statement
)
529 and then No_Elaboration_Check
(N
)
534 -- Go to parent for derived subprogram, or to original subprogram
535 -- in the case of a renaming (Alias covers both these cases)
539 if (Suppress_Elaboration_Warnings
(Ent
)
540 or else Elaboration_Checks_Suppressed
(Ent
))
541 and then (Inst_Case
or else No
(Alias
(Ent
)))
546 -- Nothing to do for imported entities
548 if Is_Imported
(Ent
) then
552 exit when Inst_Case
or else No
(Alias
(Ent
));
556 Decl
:= Unit_Declaration_Node
(Ent
);
558 if Nkind
(Decl
) = N_Subprogram_Body
then
559 Body_Acts_As_Spec
:= True;
561 elsif Nkind
(Decl
) = N_Subprogram_Declaration
562 or else Nkind
(Decl
) = N_Subprogram_Body_Stub
565 Body_Acts_As_Spec
:= False;
567 -- If we have none of an instantiation, subprogram body or
568 -- subprogram declaration, then it is not a case that we want
569 -- to check. (One case is a call to a generic formal subprogram,
570 -- where we do not want the check in the template).
578 if Elaboration_Checks_Suppressed
(E_Scope
)
579 or else Suppress_Elaboration_Warnings
(E_Scope
)
584 -- Exit when we get to compilation unit, not counting subunits
586 exit when Is_Compilation_Unit
(E_Scope
)
587 and then (Is_Child_Unit
(E_Scope
)
588 or else Scope
(E_Scope
) = Standard_Standard
);
590 -- If we did not find a compilation unit, other than standard,
591 -- then nothing to check (happens in some instantiation cases)
593 if E_Scope
= Standard_Standard
then
596 -- Otherwise move up a scope looking for compilation unit
599 E_Scope
:= Scope
(E_Scope
);
603 -- No checks needed for pure or preelaborated compilation units
606 or else Is_Preelaborated
(E_Scope
)
611 -- If the generic entity is within a deeper instance than we are, then
612 -- either the instantiation to which we refer itself caused an ABE, in
613 -- which case that will be handled separately, or else we know that the
614 -- body we need appears as needed at the point of the instantiation.
615 -- However, this assumption is only valid if we are in static mode.
617 if not Dynamic_Elaboration_Checks
618 and then Instantiation_Depth
(Sloc
(Ent
)) >
619 Instantiation_Depth
(Sloc
(N
))
624 -- Do not give a warning for a package with no body
626 if Ekind
(Ent
) = E_Generic_Package
627 and then not Has_Generic_Body
(N
)
632 -- Case of entity is not in current unit (i.e. with'ed unit case)
634 if E_Scope
/= C_Scope
then
636 -- We are only interested in such calls if the outer call was from
637 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
639 if not From_Elab_Code
and then not Dynamic_Elaboration_Checks
then
643 -- Nothing to do if some scope said that no checks were required
649 -- Nothing to do for a generic instance, because in this case
650 -- the checking was at the point of instantiation of the generic
651 -- However, this shortcut is only applicable in static mode.
653 if Is_Generic_Instance
(Ent
) and not Dynamic_Elaboration_Checks
then
657 -- Nothing to do if subprogram with no separate spec. However,
658 -- a call to Deep_Initialize may result in a call to a user-defined
659 -- Initialize procedure, which imposes a body dependency. This
660 -- happens only if the type is controlled and the Initialize
661 -- procedure is not inherited.
663 if Body_Acts_As_Spec
then
664 if Is_TSS
(Ent
, TSS_Deep_Initialize
) then
669 Typ
:= Etype
(Next_Formal
(First_Formal
(Ent
)));
671 if not Is_Controlled
(Typ
) then
674 Init
:= Find_Prim_Op
(Typ
, Name_Initialize
);
676 if Comes_From_Source
(Init
) then
689 -- Check cases of internal units
691 Callee_Unit_Internal
:=
692 Is_Internal_File_Name
693 (Unit_File_Name
(Get_Source_Unit
(E_Scope
)));
695 -- Do not give a warning if the with'ed unit is internal
696 -- and this is the generic instantiation case (this saves a
697 -- lot of hassle dealing with the Text_IO special child units)
699 if Callee_Unit_Internal
and Inst_Case
then
703 if C_Scope
= Standard_Standard
then
704 Caller_Unit_Internal
:= False;
706 Caller_Unit_Internal
:=
707 Is_Internal_File_Name
708 (Unit_File_Name
(Get_Source_Unit
(C_Scope
)));
711 -- Do not give a warning if the with'ed unit is internal
712 -- and the caller is not internal (since the binder always
713 -- elaborates internal units first).
715 if Callee_Unit_Internal
and (not Caller_Unit_Internal
) then
719 -- For now, if debug flag -gnatdE is not set, do no checking for
720 -- one internal unit withing another. This fixes the problem with
721 -- the sgi build and storage errors. To be resolved later ???
723 if (Callee_Unit_Internal
and Caller_Unit_Internal
)
724 and then not Debug_Flag_EE
729 if Is_TSS
(E
, TSS_Deep_Initialize
) then
733 -- If the call is in an instance, and the called entity is not
734 -- defined in the same instance, then the elaboration issue
735 -- focuses around the unit containing the template, it is
736 -- this unit which requires an Elaborate_All.
738 -- However, if we are doing dynamic elaboration, we need to
739 -- chase the call in the usual manner.
741 -- We do not handle the case of calling a generic formal correctly
742 -- in the static case. See test 4703-004 to explore this gap ???
744 Inst_Caller
:= Instantiation
(Get_Source_File_Index
(Sloc
(N
)));
745 Inst_Callee
:= Instantiation
(Get_Source_File_Index
(Sloc
(Ent
)));
747 if Inst_Caller
= No_Location
then
748 Unit_Caller
:= No_Unit
;
750 Unit_Caller
:= Get_Source_Unit
(N
);
753 if Inst_Callee
= No_Location
then
754 Unit_Callee
:= No_Unit
;
756 Unit_Callee
:= Get_Source_Unit
(Ent
);
759 if Unit_Caller
/= No_Unit
760 and then Unit_Callee
/= Unit_Caller
761 and then not Dynamic_Elaboration_Checks
763 E_Scope
:= Spec_Entity
(Cunit_Entity
(Unit_Caller
));
765 -- If we don't get a spec entity, just ignore call. Not
766 -- quite clear why this check is necessary.
772 -- Otherwise step to enclosing compilation unit
774 while not Is_Compilation_Unit
(E_Scope
) loop
775 E_Scope
:= Scope
(E_Scope
);
778 -- For the case N is not an instance, or a call within instance
779 -- We recompute E_Scope for the error message, since we
780 -- do NOT want to go to the unit which has the ultimate
781 -- declaration in the case of renaming and derivation and
782 -- we also want to go to the generic unit in the case of
783 -- an instance, and no further.
786 -- Loop to carefully follow renamings and derivations
787 -- one step outside the current unit, but not further.
790 and then Present
(Alias
(Ent
))
792 E_Scope
:= Alias
(Ent
);
798 while not Is_Compilation_Unit
(E_Scope
) loop
799 E_Scope
:= Scope
(E_Scope
);
802 -- If E_Scope is the same as C_Scope, it means that there
803 -- definitely was a local renaming or derivation, and we
804 -- are not yet out of the current unit.
806 exit when E_Scope
/= C_Scope
;
810 -- If no alias, there is a previous error
818 if Within_Elaborate_All
(E_Scope
) then
822 -- Find top level scope for called entity (not following renamings
823 -- or derivations). This is where the Elaborate_All will go if it
824 -- is needed. We start with the called entity, except in the case
825 -- of an initialization procedure outside the current package, where
826 -- the init proc is in the root package, and we start from the entity
827 -- of the name in the call.
829 if Is_Entity_Name
(Name
(N
))
830 and then Is_Init_Proc
(Entity
(Name
(N
)))
831 and then not In_Same_Extended_Unit
(N
, Entity
(Name
(N
)))
833 W_Scope
:= Scope
(Entity
(Name
(N
)));
838 while not Is_Compilation_Unit
(W_Scope
) loop
839 W_Scope
:= Scope
(W_Scope
);
842 -- Now check if an elaborate_all (or dynamic check) is needed
844 if not Suppress_Elaboration_Warnings
(Ent
)
845 and then not Elaboration_Checks_Suppressed
(Ent
)
846 and then not Suppress_Elaboration_Warnings
(E_Scope
)
847 and then not Elaboration_Checks_Suppressed
(E_Scope
)
848 and then Elab_Warnings
849 and then Generate_Warnings
851 Generate_Elab_Warnings
: declare
852 procedure Elab_Warning
855 Ent
: Node_Or_Entity_Id
);
856 -- Generate a call to Error_Msg_NE with parameters Msg_D or
857 -- Msg_S (for dynamic or static elaboration model), N and Ent.
863 procedure Elab_Warning
866 Ent
: Node_Or_Entity_Id
)
869 if Dynamic_Elaboration_Checks
then
870 Error_Msg_NE
(Msg_D
, N
, Ent
);
872 Error_Msg_NE
(Msg_S
, N
, Ent
);
876 -- Start of processing for Generate_Elab_Warnings
881 ("instantiation of& may raise Program_Error?",
882 "instantiation of& during elaboration?", Ent
);
885 if Nkind
(Name
(N
)) in N_Has_Entity
886 and then Is_Init_Proc
(Entity
(Name
(N
)))
887 and then Comes_From_Source
(Ent
)
890 ("implicit call to & may raise Program_Error?",
891 "implicit call to & during elaboration?",
896 ("call to & may raise Program_Error?",
897 "call to & during elaboration?",
902 Error_Msg_Qual_Level
:= Nat
'Last;
904 if Nkind
(N
) in N_Subprogram_Instantiation
then
906 ("\missing pragma Elaborate for&?",
907 "\implicit pragma Elaborate for& generated?",
911 ("\missing pragma Elaborate_All for&?",
912 "\implicit pragma Elaborate_All for & generated?",
915 end Generate_Elab_Warnings
;
917 Error_Msg_Qual_Level
:= 0;
920 -- Set flag to prevent further warnings for same unit unless in
923 if not All_Errors_Mode
and not Dynamic_Elaboration_Checks
then
924 Set_Suppress_Elaboration_Warnings
(W_Scope
, True);
928 -- Check for runtime elaboration check required
930 if Dynamic_Elaboration_Checks
then
931 if not Elaboration_Checks_Suppressed
(Ent
)
932 and then not Elaboration_Checks_Suppressed
(W_Scope
)
933 and then not Elaboration_Checks_Suppressed
(E_Scope
)
934 and then not Cunit_SC
936 -- Runtime elaboration check required. Generate check of the
937 -- elaboration Boolean for the unit containing the entity.
939 -- Note that for this case, we do check the real unit (the
940 -- one from following renamings, since that is the issue!)
942 -- Could this possibly miss a useless but required PE???
944 Insert_Elab_Check
(N
,
945 Make_Attribute_Reference
(Loc
,
946 Attribute_Name
=> Name_Elaborated
,
949 (Spec_Entity
(E_Scope
), Loc
)));
952 -- Case of static elaboration model
955 -- Do not do anything if elaboration checks suppressed. Note
956 -- that we check Ent here, not E, since we want the real entity
957 -- for the body to see if checks are suppressed for it, not the
958 -- dummy entry for renamings or derivations.
960 if Elaboration_Checks_Suppressed
(Ent
)
961 or else Elaboration_Checks_Suppressed
(E_Scope
)
962 or else Elaboration_Checks_Suppressed
(W_Scope
)
966 -- Here we need to generate an implicit elaborate all
969 -- Generate elaborate_all warning unless suppressed
971 if (Elab_Warnings
and Generate_Warnings
and not Inst_Case
)
972 and then not Suppress_Elaboration_Warnings
(Ent
)
973 and then not Suppress_Elaboration_Warnings
(E_Scope
)
974 and then not Suppress_Elaboration_Warnings
(W_Scope
)
976 Error_Msg_Node_2
:= W_Scope
;
978 ("call to& in elaboration code " &
979 "requires pragma Elaborate_All on&?", N
, E
);
982 -- Set indication for binder to generate Elaborate_All
984 Set_Elaboration_Constraint
(N
, E
, W_Scope
);
988 -- Case of entity is in same unit as call or instantiation
990 elsif not Inter_Unit_Only
then
991 Check_Internal_Call
(N
, Ent
, Outer_Scope
, E
);
995 -----------------------------
996 -- Check_Bad_Instantiation --
997 -----------------------------
999 procedure Check_Bad_Instantiation
(N
: Node_Id
) is
1003 -- Nothing to do if we do not have an instantiation (happens in some
1004 -- error cases, and also in the formal package declaration case)
1006 if Nkind
(N
) not in N_Generic_Instantiation
then
1009 -- Nothing to do if serious errors detected (avoid cascaded errors)
1011 elsif Serious_Errors_Detected
/= 0 then
1014 -- Nothing to do if not in full analysis mode
1016 elsif not Full_Analysis
then
1019 -- Nothing to do if inside a generic template
1021 elsif Inside_A_Generic
then
1024 -- Nothing to do if a library level instantiation
1026 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
1029 -- Nothing to do if we are compiling a proper body for semantic
1030 -- purposes only. The generic body may be in another proper body.
1033 Nkind
(Parent
(Unit_Declaration_Node
(Main_Unit_Entity
))) = N_Subunit
1038 Ent
:= Get_Generic_Entity
(N
);
1040 -- The case we are interested in is when the generic spec is in the
1041 -- current declarative part
1043 if not Same_Elaboration_Scope
(Current_Scope
, Scope
(Ent
))
1044 or else not In_Same_Extended_Unit
(N
, Ent
)
1049 -- If the generic entity is within a deeper instance than we are, then
1050 -- either the instantiation to which we refer itself caused an ABE, in
1051 -- which case that will be handled separately. Otherwise, we know that
1052 -- the body we need appears as needed at the point of the instantiation.
1053 -- If they are both at the same level but not within the same instance
1054 -- then the body of the generic will be in the earlier instance.
1057 D1
: constant Int
:= Instantiation_Depth
(Sloc
(Ent
));
1058 D2
: constant Int
:= Instantiation_Depth
(Sloc
(N
));
1065 and then Is_Generic_Instance
(Scope
(Ent
))
1066 and then not In_Open_Scopes
(Scope
(Ent
))
1072 -- Now we can proceed, if the entity being called has a completion,
1073 -- then we are definitely OK, since we have already seen the body.
1075 if Has_Completion
(Ent
) then
1079 -- If there is no body, then nothing to do
1081 if not Has_Generic_Body
(N
) then
1085 -- Here we definitely have a bad instantiation
1088 ("?cannot instantiate& before body seen", N
, Ent
);
1090 if Present
(Instance_Spec
(N
)) then
1091 Supply_Bodies
(Instance_Spec
(N
));
1095 ("\?Program_Error will be raised at run time", N
);
1096 Insert_Elab_Check
(N
);
1097 Set_ABE_Is_Certain
(N
);
1098 end Check_Bad_Instantiation
;
1100 ---------------------
1101 -- Check_Elab_Call --
1102 ---------------------
1104 procedure Check_Elab_Call
1106 Outer_Scope
: Entity_Id
:= Empty
)
1111 function Get_Called_Ent
return Entity_Id
;
1112 -- Retrieve called entity. If this is a call to a protected subprogram,
1113 -- entity is a selected component. The callable entity may be absent,
1114 -- in which case there is no check to perform. This happens with
1115 -- non-analyzed calls in nested generics.
1117 --------------------
1118 -- Get_Called_Ent --
1119 --------------------
1121 function Get_Called_Ent
return Entity_Id
is
1130 elsif Nkind
(Nam
) = N_Selected_Component
then
1131 return Entity
(Selector_Name
(Nam
));
1133 elsif not Is_Entity_Name
(Nam
) then
1137 return Entity
(Nam
);
1141 -- Start of processing for Check_Elab_Call
1144 -- If the call does not come from the main unit, there is nothing to
1145 -- check. Elaboration call from units in the context of the main unit
1146 -- will lead to semantic dependencies when those units are compiled.
1148 if not In_Extended_Main_Code_Unit
(N
) then
1152 -- For an entry call, check relevant restriction
1154 if Nkind
(N
) = N_Entry_Call_Statement
1155 and then not In_Subprogram_Or_Concurrent_Unit
1157 Check_Restriction
(No_Entry_Calls_In_Elaboration_Code
, N
);
1159 -- Nothing to do if this is not a call (happens in some error
1160 -- conditions, and in some cases where rewriting occurs).
1162 elsif Nkind
(N
) /= N_Function_Call
1163 and then Nkind
(N
) /= N_Procedure_Call_Statement
1167 -- Nothing to do if this is a call already rewritten for elab checking
1169 elsif Nkind
(Parent
(N
)) = N_Conditional_Expression
then
1172 -- Nothing to do if inside a generic template
1174 elsif Inside_A_Generic
1175 and then No
(Enclosing_Generic_Body
(N
))
1180 -- Here we have a call at elaboration time which must be checked
1182 if Debug_Flag_LL
then
1183 Write_Str
(" Check_Elab_Call: ");
1186 or else not Is_Entity_Name
(Name
(N
))
1188 Write_Str
("<<not entity name>> ");
1190 Write_Name
(Chars
(Entity
(Name
(N
))));
1193 Write_Str
(" call at ");
1194 Write_Location
(Sloc
(N
));
1198 -- Climb up the tree to make sure we are not inside default expression
1199 -- of a parameter specification or a record component, since in both
1200 -- these cases, we will be doing the actual call later, not now, and it
1201 -- is at the time of the actual call (statically speaking) that we must
1202 -- do our static check, not at the time of its initial analysis).
1204 -- However, we have to check calls within component definitions (e.g., a
1205 -- function call that determines an array component bound), so we
1206 -- terminate the loop in that case.
1209 while Present
(P
) loop
1210 if Nkind
(P
) = N_Parameter_Specification
1212 Nkind
(P
) = N_Component_Declaration
1216 -- The call occurs within the constraint of a component,
1217 -- so it must be checked.
1219 elsif Nkind
(P
) = N_Component_Definition
then
1227 -- Stuff that happens only at the outer level
1229 if No
(Outer_Scope
) then
1230 Elab_Visited
.Set_Last
(0);
1232 -- Nothing to do if current scope is Standard (this is a bit
1233 -- odd, but it happens in the case of generic instantiations).
1235 C_Scope
:= Current_Scope
;
1237 if C_Scope
= Standard_Standard
then
1241 -- First case, we are in elaboration code
1243 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1244 if From_Elab_Code
then
1246 -- Complain if call that comes from source in preelaborated
1247 -- unit and we are not inside a subprogram (i.e. we are in
1250 if Comes_From_Source
(N
)
1251 and then In_Preelaborated_Unit
1252 and then not In_Inlined_Body
1254 -- This is a warning in GNAT mode allowing such calls to be
1255 -- used in the predefined library with appropriate care.
1257 Error_Msg_Warn
:= GNAT_Mode
;
1259 ("<non-static call not allowed in preelaborated unit", N
);
1263 -- Second case, we are inside a subprogram or concurrent unit, which
1264 -- means we are not in elaboration code.
1267 -- In this case, the issue is whether we are inside the
1268 -- declarative part of the unit in which we live, or inside its
1269 -- statements. In the latter case, there is no issue of ABE calls
1270 -- at this level (a call from outside to the unit in which we live
1271 -- might cause an ABE, but that will be detected when we analyze
1272 -- that outer level call, as it recurses into the called unit).
1274 -- Climb up the tree, doing this test, and also testing for being
1275 -- inside a default expression, which, as discussed above, is not
1276 -- checked at this stage.
1285 -- If we find a parentless subtree, it seems safe to assume
1286 -- that we are not in a declarative part and that no
1287 -- checking is required.
1293 if Is_List_Member
(P
) then
1294 L
:= List_Containing
(P
);
1301 exit when Nkind
(P
) = N_Subunit
;
1303 -- Filter out case of default expressions, where we do not
1304 -- do the check at this stage.
1306 if Nkind
(P
) = N_Parameter_Specification
1308 Nkind
(P
) = N_Component_Declaration
1313 -- A protected body has no elaboration code and contains
1314 -- only other bodies.
1316 if Nkind
(P
) = N_Protected_Body
then
1319 elsif Nkind
(P
) = N_Subprogram_Body
1321 Nkind
(P
) = N_Task_Body
1323 Nkind
(P
) = N_Block_Statement
1325 Nkind
(P
) = N_Entry_Body
1327 if L
= Declarations
(P
) then
1330 -- We are not in elaboration code, but we are doing
1331 -- dynamic elaboration checks, in this case, we still
1332 -- need to do the call, since the subprogram we are in
1333 -- could be called from another unit, also in dynamic
1334 -- elaboration check mode, at elaboration time.
1336 elsif Dynamic_Elaboration_Checks
then
1338 -- This is a rather new check, going into version
1339 -- 3.14a1 for the first time (V1.80 of this unit), so
1340 -- we provide a debug flag to enable it. That way we
1341 -- have an easy work around for regressions that are
1342 -- caused by this new check. This debug flag can be
1345 if Debug_Flag_DD
then
1349 -- Do the check in this case
1353 elsif Nkind
(P
) = N_Task_Body
then
1355 -- The check is deferred until Check_Task_Activation
1356 -- but we need to capture local suppress pragmas
1357 -- that may inhibit checks on this call.
1359 Ent
:= Get_Called_Ent
;
1364 elsif Elaboration_Checks_Suppressed
(Current_Scope
)
1365 or else Elaboration_Checks_Suppressed
(Ent
)
1366 or else Elaboration_Checks_Suppressed
(Scope
(Ent
))
1368 Set_No_Elaboration_Check
(N
);
1373 -- Static model, call is not in elaboration code, we
1374 -- never need to worry, because in the static model the
1375 -- top level caller always takes care of things.
1386 Ent
:= Get_Called_Ent
;
1392 -- Nothing to do if this is a recursive call (i.e. a call to
1393 -- an entity that is already in the Elab_Call stack)
1395 for J
in 1 .. Elab_Visited
.Last
loop
1396 if Ent
= Elab_Visited
.Table
(J
) then
1401 -- See if we need to analyze this call. We analyze it if either of
1402 -- the following conditions is met:
1404 -- It is an inner level call (since in this case it was triggered
1405 -- by an outer level call from elaboration code), but only if the
1406 -- call is within the scope of the original outer level call.
1408 -- It is an outer level call from elaboration code, or the called
1409 -- entity is in the same elaboration scope.
1411 -- And in these cases, we will check both inter-unit calls and
1412 -- intra-unit (within a single unit) calls.
1414 C_Scope
:= Current_Scope
;
1416 -- If not outer level call, then we follow it if it is within
1417 -- the original scope of the outer call.
1419 if Present
(Outer_Scope
)
1420 and then Within
(Scope
(Ent
), Outer_Scope
)
1423 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
1425 elsif Elaboration_Checks_Suppressed
(Current_Scope
) then
1428 elsif From_Elab_Code
then
1430 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1432 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1434 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1436 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1437 -- is set, then we will do the check, but only in the inter-unit case
1438 -- (this is to accommodate unguarded elaboration calls from other units
1439 -- in which this same mode is set). We don't want warnings in this case,
1440 -- it would generate warnings having nothing to do with elaboration.
1442 elsif Dynamic_Elaboration_Checks
then
1448 Inter_Unit_Only
=> True,
1449 Generate_Warnings
=> False);
1451 -- Otherwise nothing to do
1457 -- A call to an Init_Proc in elaboration code may bring additional
1458 -- dependencies, if some of the record components thereof have
1459 -- initializations that are function calls that come from source.
1460 -- We treat the current node as a call to each of these functions,
1461 -- to check their elaboration impact.
1463 if Is_Init_Proc
(Ent
)
1464 and then From_Elab_Code
1466 Process_Init_Proc
: declare
1467 Unit_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
1469 function Find_Init_Call
(Nod
: Node_Id
) return Traverse_Result
;
1470 -- Find subprogram calls within body of Init_Proc for Traverse
1471 -- instantiation below.
1473 procedure Traverse_Body
is new Traverse_Proc
(Find_Init_Call
);
1474 -- Traversal procedure to find all calls with body of Init_Proc
1476 --------------------
1477 -- Find_Init_Call --
1478 --------------------
1480 function Find_Init_Call
(Nod
: Node_Id
) return Traverse_Result
is
1484 if (Nkind
(Nod
) = N_Function_Call
1485 or else Nkind
(Nod
) = N_Procedure_Call_Statement
)
1486 and then Is_Entity_Name
(Name
(Nod
))
1488 Func
:= Entity
(Name
(Nod
));
1490 if Comes_From_Source
(Func
) then
1492 (N
, Func
, Standard_Standard
, Inter_Unit_Only
=> True);
1502 -- Start of processing for Process_Init_Proc
1505 if Nkind
(Unit_Decl
) = N_Subprogram_Body
then
1506 Traverse_Body
(Handled_Statement_Sequence
(Unit_Decl
));
1508 end Process_Init_Proc
;
1510 end Check_Elab_Call
;
1512 -----------------------
1513 -- Check_Elab_Assign --
1514 -----------------------
1516 procedure Check_Elab_Assign
(N
: Node_Id
) is
1520 Pkg_Spec
: Entity_Id
;
1521 Pkg_Body
: Entity_Id
;
1524 -- For record or array component, check prefix. If it is an access
1525 -- type, then there is nothing to do (we do not know what is being
1526 -- assigned), but otherwise this is an assignment to the prefix.
1528 if Nkind
(N
) = N_Indexed_Component
1530 Nkind
(N
) = N_Selected_Component
1534 if not Is_Access_Type
(Etype
(Prefix
(N
))) then
1535 Check_Elab_Assign
(Prefix
(N
));
1541 -- For type conversion, check expression
1543 if Nkind
(N
) = N_Type_Conversion
then
1544 Check_Elab_Assign
(Expression
(N
));
1548 -- Nothing to do if this is not an entity reference otherwise get entity
1550 if Is_Entity_Name
(N
) then
1556 -- What we are looking for is a reference in the body of a package that
1557 -- modifies a variable declared in the visible part of the package spec.
1560 and then Comes_From_Source
(N
)
1561 and then not Suppress_Elaboration_Warnings
(Ent
)
1562 and then Ekind
(Ent
) = E_Variable
1563 and then not In_Private_Part
(Ent
)
1564 and then Is_Library_Level_Entity
(Ent
)
1566 Scop
:= Current_Scope
;
1568 if No
(Scop
) or else Scop
= Standard_Standard
then
1570 elsif Ekind
(Scop
) = E_Package
1571 and then Is_Compilation_Unit
(Scop
)
1575 Scop
:= Scope
(Scop
);
1579 -- Here Scop points to the containing library package
1582 Pkg_Body
:= Body_Entity
(Pkg_Spec
);
1584 -- All OK if the package has an Elaborate_Body pragma
1586 if Has_Pragma_Elaborate_Body
(Scop
) then
1590 -- OK if entity being modified is not in containing package spec
1592 if not In_Same_Source_Unit
(Scop
, Ent
) then
1596 -- All OK if entity appears in generic package or generic instance.
1597 -- We just get too messed up trying to give proper warnings in the
1598 -- presence of generics. Better no message than a junk one.
1600 Scop
:= Scope
(Ent
);
1601 while Present
(Scop
) and then Scop
/= Pkg_Spec
loop
1602 if Ekind
(Scop
) = E_Generic_Package
then
1604 elsif Ekind
(Scop
) = E_Package
1605 and then Is_Generic_Instance
(Scop
)
1610 Scop
:= Scope
(Scop
);
1613 -- All OK if in task, don't issue warnings there
1615 if In_Task_Activation
then
1619 -- OK if no package body
1621 if No
(Pkg_Body
) then
1625 -- OK if reference is not in package body
1627 if not In_Same_Source_Unit
(Pkg_Body
, N
) then
1631 -- OK if package body has no handled statement sequence
1634 HSS
: constant Node_Id
:=
1635 Handled_Statement_Sequence
(Declaration_Node
(Pkg_Body
));
1637 if No
(HSS
) or else not Comes_From_Source
(HSS
) then
1642 -- We definitely have a case of a modification of an entity in
1643 -- the package spec from the elaboration code of the package body.
1644 -- We may not give the warning (because there are some additional
1645 -- checks to avoid too many false positives), but it would be a good
1646 -- idea for the binder to try to keep the body elaboration close to
1647 -- the spec elaboration.
1649 Set_Elaborate_Body_Desirable
(Pkg_Spec
);
1651 -- All OK in gnat mode (we know what we are doing)
1657 -- All OK if warnings suppressed on the entity
1659 if Warnings_Off
(Ent
) then
1663 -- All OK if all warnings suppressed
1665 if Warning_Mode
= Suppress
then
1669 -- All OK if elaboration checks suppressed for entity
1671 if Checks_May_Be_Suppressed
(Ent
)
1672 and then Is_Check_Suppressed
(Ent
, Elaboration_Check
)
1677 -- OK if the entity is initialized. Note that the No_Initialization
1678 -- flag usually means that the initialization has been rewritten into
1679 -- assignments, but that still counts for us.
1682 Decl
: constant Node_Id
:= Declaration_Node
(Ent
);
1684 if Nkind
(Decl
) = N_Object_Declaration
1685 and then (Present
(Expression
(Decl
))
1686 or else No_Initialization
(Decl
))
1692 -- Here is where we give the warning
1694 Error_Msg_Sloc
:= Sloc
(Ent
);
1697 ("?elaboration code may access& before it is initialized",
1700 ("\?suggest adding pragma Elaborate_Body to spec of &",
1703 ("\?or an explicit initialization could be added #", N
);
1705 if not All_Errors_Mode
then
1706 Set_Suppress_Elaboration_Warnings
(Ent
);
1709 end Check_Elab_Assign
;
1711 ----------------------
1712 -- Check_Elab_Calls --
1713 ----------------------
1715 procedure Check_Elab_Calls
is
1717 -- If expansion is disabled, do not generate any checks. Also
1718 -- skip checks if any subunits are missing because in either
1719 -- case we lack the full information that we need, and no object
1720 -- file will be created in any case.
1722 if not Expander_Active
1723 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
1724 or else Subunits_Missing
1729 -- Skip delayed calls if we had any errors
1731 if Serious_Errors_Detected
= 0 then
1732 Delaying_Elab_Checks
:= False;
1733 Expander_Mode_Save_And_Set
(True);
1735 for J
in Delay_Check
.First
.. Delay_Check
.Last
loop
1736 Push_Scope
(Delay_Check
.Table
(J
).Curscop
);
1737 From_Elab_Code
:= Delay_Check
.Table
(J
).From_Elab_Code
;
1739 Check_Internal_Call_Continue
(
1740 N
=> Delay_Check
.Table
(J
).N
,
1741 E
=> Delay_Check
.Table
(J
).E
,
1742 Outer_Scope
=> Delay_Check
.Table
(J
).Outer_Scope
,
1743 Orig_Ent
=> Delay_Check
.Table
(J
).Orig_Ent
);
1748 -- Set Delaying_Elab_Checks back on for next main compilation
1750 Expander_Mode_Restore
;
1751 Delaying_Elab_Checks
:= True;
1753 end Check_Elab_Calls
;
1755 ------------------------------
1756 -- Check_Elab_Instantiation --
1757 ------------------------------
1759 procedure Check_Elab_Instantiation
1761 Outer_Scope
: Entity_Id
:= Empty
)
1766 -- Check for and deal with bad instantiation case. There is some
1767 -- duplicated code here, but we will worry about this later ???
1769 Check_Bad_Instantiation
(N
);
1771 if ABE_Is_Certain
(N
) then
1775 -- Nothing to do if we do not have an instantiation (happens in some
1776 -- error cases, and also in the formal package declaration case)
1778 if Nkind
(N
) not in N_Generic_Instantiation
then
1782 -- Nothing to do if inside a generic template
1784 if Inside_A_Generic
then
1788 -- Nothing to do if the instantiation is not in the main unit
1790 if not In_Extended_Main_Code_Unit
(N
) then
1794 Ent
:= Get_Generic_Entity
(N
);
1795 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1797 -- See if we need to analyze this instantiation. We analyze it if
1798 -- either of the following conditions is met:
1800 -- It is an inner level instantiation (since in this case it was
1801 -- triggered by an outer level call from elaboration code), but
1802 -- only if the instantiation is within the scope of the original
1803 -- outer level call.
1805 -- It is an outer level instantiation from elaboration code, or the
1806 -- instantiated entity is in the same elaboratoin scope.
1808 -- And in these cases, we will check both the inter-unit case and
1809 -- the intra-unit (within a single unit) case.
1811 C_Scope
:= Current_Scope
;
1813 if Present
(Outer_Scope
)
1814 and then Within
(Scope
(Ent
), Outer_Scope
)
1817 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
1819 elsif From_Elab_Code
then
1821 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1823 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1825 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1827 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1828 -- is set, then we will do the check, but only in the inter-unit case
1829 -- (this is to accommodate unguarded elaboration calls from other units
1830 -- in which this same mode is set). We inhibit warnings in this case,
1831 -- since this instantiation is not occurring in elaboration code.
1833 elsif Dynamic_Elaboration_Checks
then
1839 Inter_Unit_Only
=> True,
1840 Generate_Warnings
=> False);
1845 end Check_Elab_Instantiation
;
1847 -------------------------
1848 -- Check_Internal_Call --
1849 -------------------------
1851 procedure Check_Internal_Call
1854 Outer_Scope
: Entity_Id
;
1855 Orig_Ent
: Entity_Id
)
1857 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
1860 -- If not function or procedure call or instantiation, then ignore
1861 -- call (this happens in some error case and rewriting cases)
1863 if Nkind
(N
) /= N_Function_Call
1865 Nkind
(N
) /= N_Procedure_Call_Statement
1871 -- Nothing to do if this is a call or instantiation that has
1872 -- already been found to be a sure ABE
1874 elsif ABE_Is_Certain
(N
) then
1877 -- Nothing to do if errors already detected (avoid cascaded errors)
1879 elsif Serious_Errors_Detected
/= 0 then
1882 -- Nothing to do if not in full analysis mode
1884 elsif not Full_Analysis
then
1887 -- Nothing to do if within a default expression, since the call
1888 -- is not actualy being made at this time.
1890 elsif In_Default_Expression
then
1893 -- Nothing to do for call to intrinsic subprogram
1895 elsif Is_Intrinsic_Subprogram
(E
) then
1898 -- No need to trace local calls if checking task activation, because
1899 -- other local bodies are elaborated already.
1901 elsif In_Task_Activation
then
1905 -- Delay this call if we are still delaying calls
1907 if Delaying_Elab_Checks
then
1908 Delay_Check
.Append
(
1911 Orig_Ent
=> Orig_Ent
,
1912 Curscop
=> Current_Scope
,
1913 Outer_Scope
=> Outer_Scope
,
1914 From_Elab_Code
=> From_Elab_Code
));
1917 -- Otherwise, call phase 2 continuation right now
1920 Check_Internal_Call_Continue
(N
, E
, Outer_Scope
, Orig_Ent
);
1922 end Check_Internal_Call
;
1924 ----------------------------------
1925 -- Check_Internal_Call_Continue --
1926 ----------------------------------
1928 procedure Check_Internal_Call_Continue
1931 Outer_Scope
: Entity_Id
;
1932 Orig_Ent
: Entity_Id
)
1934 Loc
: constant Source_Ptr
:= Sloc
(N
);
1935 Inst_Case
: constant Boolean := Is_Generic_Unit
(E
);
1940 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
;
1941 -- Function applied to each node as we traverse the body. Checks for
1942 -- call or entity reference that needs checking, and if so checks it.
1943 -- Always returns OK, so entire tree is traversed, except that as
1944 -- described below subprogram bodies are skipped for now.
1946 procedure Traverse
is new Atree
.Traverse_Proc
(Find_Elab_Reference
);
1947 -- Traverse procedure using above Find_Elab_Reference function
1949 -------------------------
1950 -- Find_Elab_Reference --
1951 -------------------------
1953 function Find_Elab_Reference
(N
: Node_Id
) return Traverse_Result
is
1957 -- If user has specified that there are no entry calls in elaboration
1958 -- code, do not trace past an accept statement, because the rendez-
1959 -- vous will happen after elaboration.
1961 if (Nkind
(Original_Node
(N
)) = N_Accept_Statement
1962 or else Nkind
(Original_Node
(N
)) = N_Selective_Accept
)
1963 and then Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
1967 -- If we have a function call, check it
1969 elsif Nkind
(N
) = N_Function_Call
then
1970 Check_Elab_Call
(N
, Outer_Scope
);
1973 -- If we have a procedure call, check the call, and also check
1974 -- arguments that are assignments (OUT or IN OUT mode formals).
1976 elsif Nkind
(N
) = N_Procedure_Call_Statement
then
1977 Check_Elab_Call
(N
, Outer_Scope
);
1979 Actual
:= First_Actual
(N
);
1980 while Present
(Actual
) loop
1981 if Known_To_Be_Assigned
(Actual
) then
1982 Check_Elab_Assign
(Actual
);
1985 Next_Actual
(Actual
);
1990 -- If we have a generic instantiation, check it
1992 elsif Nkind
(N
) in N_Generic_Instantiation
then
1993 Check_Elab_Instantiation
(N
, Outer_Scope
);
1996 -- Skip subprogram bodies that come from source (wait for
1997 -- call to analyze these). The reason for the come from
1998 -- source test is to avoid catching task bodies.
2000 -- For task bodies, we should really avoid these too, waiting
2001 -- for the task activation, but that's too much trouble to
2002 -- catch for now, so we go in unconditionally. This is not
2003 -- so terrible, it means the error backtrace is not quite
2004 -- complete, and we are too eager to scan bodies of tasks
2005 -- that are unused, but this is hardly very significant!
2007 elsif Nkind
(N
) = N_Subprogram_Body
2008 and then Comes_From_Source
(N
)
2012 elsif Nkind
(N
) = N_Assignment_Statement
2013 and then Comes_From_Source
(N
)
2015 Check_Elab_Assign
(Name
(N
));
2021 end Find_Elab_Reference
;
2023 -- Start of processing for Check_Internal_Call_Continue
2026 -- Save outer level call if at outer level
2028 if Elab_Call
.Last
= 0 then
2029 Outer_Level_Sloc
:= Loc
;
2032 Elab_Visited
.Append
(E
);
2034 -- If the call is to a function that renames a literal, no check
2037 if Ekind
(E
) = E_Enumeration_Literal
then
2041 Sbody
:= Unit_Declaration_Node
(E
);
2043 if Nkind
(Sbody
) /= N_Subprogram_Body
2045 Nkind
(Sbody
) /= N_Package_Body
2047 Ebody
:= Corresponding_Body
(Sbody
);
2052 Sbody
:= Unit_Declaration_Node
(Ebody
);
2056 -- If the body appears after the outer level call or
2057 -- instantiation then we have an error case handled below.
2059 if Earlier_In_Extended_Unit
(Outer_Level_Sloc
, Sloc
(Sbody
))
2060 and then not In_Task_Activation
2064 -- If we have the instantiation case we are done, since we now
2065 -- know that the body of the generic appeared earlier.
2067 elsif Inst_Case
then
2070 -- Otherwise we have a call, so we trace through the called
2071 -- body to see if it has any problems ..
2074 pragma Assert
(Nkind
(Sbody
) = N_Subprogram_Body
);
2076 Elab_Call
.Append
((Cloc
=> Loc
, Ent
=> E
));
2078 if Debug_Flag_LL
then
2079 Write_Str
("Elab_Call.Last = ");
2080 Write_Int
(Int
(Elab_Call
.Last
));
2081 Write_Str
(" Ent = ");
2082 Write_Name
(Chars
(E
));
2084 Write_Location
(Sloc
(N
));
2088 -- Now traverse declarations and statements of subprogram body.
2089 -- Note that we cannot simply Traverse (Sbody), since traverse
2090 -- does not normally visit subprogram bodies.
2095 Decl
:= First
(Declarations
(Sbody
));
2096 while Present
(Decl
) loop
2102 Traverse
(Handled_Statement_Sequence
(Sbody
));
2104 Elab_Call
.Decrement_Last
;
2108 -- Here is the case of calling a subprogram where the body has
2109 -- not yet been encountered, a warning message is needed.
2111 -- If we have nothing in the call stack, then this is at the
2112 -- outer level, and the ABE is bound to occur.
2114 if Elab_Call
.Last
= 0 then
2117 ("?cannot instantiate& before body seen", N
, Orig_Ent
);
2120 ("?cannot call& before body seen", N
, Orig_Ent
);
2124 ("\?Program_Error will be raised at run time", N
);
2125 Insert_Elab_Check
(N
);
2127 -- Call is not at outer level
2130 -- Deal with dynamic elaboration check
2132 if not Elaboration_Checks_Suppressed
(E
) then
2133 Set_Elaboration_Entity_Required
(E
);
2135 -- Case of no elaboration entity allocated yet
2137 if No
(Elaboration_Entity
(E
)) then
2139 -- Create object declaration for elaboration entity, and put it
2140 -- just in front of the spec of the subprogram or generic unit,
2141 -- in the same scope as this unit.
2144 Loce
: constant Source_Ptr
:= Sloc
(E
);
2145 Ent
: constant Entity_Id
:=
2146 Make_Defining_Identifier
(Loc
,
2147 Chars
=> New_External_Name
(Chars
(E
), 'E'));
2150 Set_Elaboration_Entity
(E
, Ent
);
2151 Push_Scope
(Scope
(E
));
2153 Insert_Action
(Declaration_Node
(E
),
2154 Make_Object_Declaration
(Loce
,
2155 Defining_Identifier
=> Ent
,
2156 Object_Definition
=>
2157 New_Occurrence_Of
(Standard_Boolean
, Loce
),
2158 Expression
=> New_Occurrence_Of
(Standard_False
, Loce
)));
2160 -- Set elaboration flag at the point of the body
2162 Set_Elaboration_Flag
(Sbody
, E
);
2164 -- Kill current value indication. This is necessary because
2165 -- the tests of this flag are inserted out of sequence and
2166 -- must not pick up bogus indications of the wrong constant
2167 -- value. Also, this is never a true constant, since one way
2168 -- or another, it gets reset.
2170 Set_Current_Value
(Ent
, Empty
);
2171 Set_Last_Assignment
(Ent
, Empty
);
2172 Set_Is_True_Constant
(Ent
, False);
2177 -- Generate check of the elaboration Boolean
2179 Insert_Elab_Check
(N
,
2180 New_Occurrence_Of
(Elaboration_Entity
(E
), Loc
));
2183 -- Generate the warning
2185 if not Suppress_Elaboration_Warnings
(E
)
2186 and then not Elaboration_Checks_Suppressed
(E
)
2190 ("instantiation of& may occur before body is seen?",
2194 ("call to& may occur before body is seen?", N
, Orig_Ent
);
2198 ("\Program_Error may be raised at run time?", N
);
2204 -- Set flag to suppress further warnings on same subprogram
2205 -- unless in all errors mode
2207 if not All_Errors_Mode
then
2208 Set_Suppress_Elaboration_Warnings
(E
);
2210 end Check_Internal_Call_Continue
;
2212 ---------------------------
2213 -- Check_Task_Activation --
2214 ---------------------------
2216 procedure Check_Task_Activation
(N
: Node_Id
) is
2217 Loc
: constant Source_Ptr
:= Sloc
(N
);
2218 Inter_Procs
: constant Elist_Id
:= New_Elmt_List
;
2219 Intra_Procs
: constant Elist_Id
:= New_Elmt_List
;
2222 Task_Scope
: Entity_Id
;
2223 Cunit_SC
: Boolean := False;
2226 Enclosing
: Entity_Id
;
2228 procedure Add_Task_Proc
(Typ
: Entity_Id
);
2229 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2230 -- For record types, this procedure recurses over component types.
2232 procedure Collect_Tasks
(Decls
: List_Id
);
2233 -- Collect the types of the tasks that are to be activated in the given
2234 -- list of declarations, in order to perform elaboration checks on the
2235 -- corresponding task procedures which are called implicitly here.
2237 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
;
2238 -- find enclosing compilation unit of Entity, ignoring subunits, or
2239 -- else enclosing subprogram. If E is not a package, there is no need
2240 -- for inter-unit elaboration checks.
2246 procedure Add_Task_Proc
(Typ
: Entity_Id
) is
2248 Proc
: Entity_Id
:= Empty
;
2251 if Is_Task_Type
(Typ
) then
2252 Proc
:= Get_Task_Body_Procedure
(Typ
);
2254 elsif Is_Array_Type
(Typ
)
2255 and then Has_Task
(Base_Type
(Typ
))
2257 Add_Task_Proc
(Component_Type
(Typ
));
2259 elsif Is_Record_Type
(Typ
)
2260 and then Has_Task
(Base_Type
(Typ
))
2262 Comp
:= First_Component
(Typ
);
2263 while Present
(Comp
) loop
2264 Add_Task_Proc
(Etype
(Comp
));
2265 Comp
:= Next_Component
(Comp
);
2269 -- If the task type is another unit, we will perform the usual
2270 -- elaboration check on its enclosing unit. If the type is in the
2271 -- same unit, we can trace the task body as for an internal call,
2272 -- but we only need to examine other external calls, because at
2273 -- the point the task is activated, internal subprogram bodies
2274 -- will have been elaborated already. We keep separate lists for
2275 -- each kind of task.
2277 -- Skip this test if errors have occurred, since in this case
2278 -- we can get false indications.
2280 if Serious_Errors_Detected
/= 0 then
2284 if Present
(Proc
) then
2285 if Outer_Unit
(Scope
(Proc
)) = Enclosing
then
2287 if No
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2289 (not Is_Generic_Instance
(Scope
(Proc
))
2291 Scope
(Proc
) = Scope
(Defining_Identifier
(Decl
)))
2294 ("task will be activated before elaboration of its body?",
2297 ("\Program_Error will be raised at run-time?", Decl
);
2300 Present
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
2302 Append_Elmt
(Proc
, Intra_Procs
);
2306 -- No need for multiple entries of the same type
2308 Elmt
:= First_Elmt
(Inter_Procs
);
2309 while Present
(Elmt
) loop
2310 if Node
(Elmt
) = Proc
then
2317 Append_Elmt
(Proc
, Inter_Procs
);
2326 procedure Collect_Tasks
(Decls
: List_Id
) is
2328 if Present
(Decls
) then
2329 Decl
:= First
(Decls
);
2330 while Present
(Decl
) loop
2331 if Nkind
(Decl
) = N_Object_Declaration
2332 and then Has_Task
(Etype
(Defining_Identifier
(Decl
)))
2334 Add_Task_Proc
(Etype
(Defining_Identifier
(Decl
)));
2346 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
is
2351 while Present
(Outer
) loop
2352 if Elaboration_Checks_Suppressed
(Outer
) then
2356 exit when Is_Child_Unit
(Outer
)
2357 or else Scope
(Outer
) = Standard_Standard
2358 or else Ekind
(Outer
) /= E_Package
;
2359 Outer
:= Scope
(Outer
);
2365 -- Start of processing for Check_Task_Activation
2368 Enclosing
:= Outer_Unit
(Current_Scope
);
2370 -- Find all tasks declared in the current unit
2372 if Nkind
(N
) = N_Package_Body
then
2373 P
:= Unit_Declaration_Node
(Corresponding_Spec
(N
));
2375 Collect_Tasks
(Declarations
(N
));
2376 Collect_Tasks
(Visible_Declarations
(Specification
(P
)));
2377 Collect_Tasks
(Private_Declarations
(Specification
(P
)));
2379 elsif Nkind
(N
) = N_Package_Declaration
then
2380 Collect_Tasks
(Visible_Declarations
(Specification
(N
)));
2381 Collect_Tasks
(Private_Declarations
(Specification
(N
)));
2384 Collect_Tasks
(Declarations
(N
));
2387 -- We only perform detailed checks in all tasks are library level
2388 -- entities. If the master is a subprogram or task, activation will
2389 -- depend on the activation of the master itself.
2391 -- Should dynamic checks be added in the more general case???
2393 if Ekind
(Enclosing
) /= E_Package
then
2397 -- For task types defined in other units, we want the unit containing
2398 -- the task body to be elaborated before the current one.
2400 Elmt
:= First_Elmt
(Inter_Procs
);
2401 while Present
(Elmt
) loop
2403 Task_Scope
:= Outer_Unit
(Scope
(Ent
));
2405 if not Is_Compilation_Unit
(Task_Scope
) then
2408 elsif Suppress_Elaboration_Warnings
(Task_Scope
)
2409 or else Elaboration_Checks_Suppressed
(Task_Scope
)
2413 elsif Dynamic_Elaboration_Checks
then
2414 if not Elaboration_Checks_Suppressed
(Ent
)
2415 and then not Cunit_SC
2417 not Restriction_Active
(No_Entry_Calls_In_Elaboration_Code
)
2419 -- Runtime elaboration check required. generate check of the
2420 -- elaboration Boolean for the unit containing the entity.
2422 Insert_Elab_Check
(N
,
2423 Make_Attribute_Reference
(Loc
,
2424 Attribute_Name
=> Name_Elaborated
,
2427 (Spec_Entity
(Task_Scope
), Loc
)));
2431 -- Force the binder to elaborate other unit first
2433 if not Suppress_Elaboration_Warnings
(Ent
)
2434 and then not Elaboration_Checks_Suppressed
(Ent
)
2435 and then Elab_Warnings
2436 and then not Suppress_Elaboration_Warnings
(Task_Scope
)
2437 and then not Elaboration_Checks_Suppressed
(Task_Scope
)
2439 Error_Msg_Node_2
:= Task_Scope
;
2440 Error_Msg_NE
("activation of an instance of task type&" &
2441 " requires pragma Elaborate_All on &?", N
, Ent
);
2444 Activate_Elaborate_All_Desirable
(N
, Task_Scope
);
2445 Set_Suppress_Elaboration_Warnings
(Task_Scope
);
2451 -- For tasks declared in the current unit, trace other calls within
2452 -- the task procedure bodies, which are available.
2454 In_Task_Activation
:= True;
2456 Elmt
:= First_Elmt
(Intra_Procs
);
2457 while Present
(Elmt
) loop
2459 Check_Internal_Call_Continue
(N
, Ent
, Enclosing
, Ent
);
2463 In_Task_Activation
:= False;
2464 end Check_Task_Activation
;
2466 --------------------------------
2467 -- Set_Elaboration_Constraint --
2468 --------------------------------
2470 procedure Set_Elaboration_Constraint
2475 Elab_Unit
: Entity_Id
;
2476 Init_Call
: constant Boolean :=
2477 Chars
(Subp
) = Name_Initialize
2478 and then Comes_From_Source
(Subp
)
2479 and then Present
(Parameter_Associations
(Call
))
2480 and then Is_Controlled
(Etype
(First_Actual
(Call
)));
2482 -- If the unit is mentioned in a with_clause of the current
2483 -- unit, it is visible, and we can set the elaboration flag.
2485 if Is_Immediately_Visible
(Scop
)
2486 or else (Is_Child_Unit
(Scop
) and then Is_Visible_Child_Unit
(Scop
))
2488 Activate_Elaborate_All_Desirable
(Call
, Scop
);
2489 Set_Suppress_Elaboration_Warnings
(Scop
, True);
2493 -- If this is not an initialization call or a call using object notation
2494 -- we know that the unit of the called entity is in the context, and
2495 -- we can set the flag as well. The unit need not be visible if the call
2496 -- occurs within an instantiation.
2498 if Is_Init_Proc
(Subp
)
2500 or else Nkind
(Original_Node
(Call
)) = N_Selected_Component
2502 null; -- detailed processing follows.
2505 Activate_Elaborate_All_Desirable
(Call
, Scop
);
2506 Set_Suppress_Elaboration_Warnings
(Scop
, True);
2510 -- If the unit is not in the context, there must be an intermediate
2511 -- unit that is, on which we need to place to elaboration flag. This
2512 -- happens with init proc calls.
2514 if Is_Init_Proc
(Subp
)
2517 -- The initialization call is on an object whose type is not declared
2518 -- in the same scope as the subprogram. The type of the object must
2519 -- be a subtype of the type of operation. This object is the first
2520 -- actual in the call.
2523 Typ
: constant Entity_Id
:=
2524 Etype
(First
(Parameter_Associations
(Call
)));
2526 Elab_Unit
:= Scope
(Typ
);
2527 while (Present
(Elab_Unit
))
2528 and then not Is_Compilation_Unit
(Elab_Unit
)
2530 Elab_Unit
:= Scope
(Elab_Unit
);
2534 -- If original node uses selected component notation, the prefix is
2535 -- visible and determines the scope that must be elaborated. After
2536 -- rewriting, the prefix is the first actual in the call.
2538 elsif Nkind
(Original_Node
(Call
)) = N_Selected_Component
then
2539 Elab_Unit
:= Scope
(Etype
(First
(Parameter_Associations
(Call
))));
2541 -- Not one of special cases above
2544 -- Using previously computed scope. If the elaboration check is
2545 -- done after analysis, the scope is not visible any longer, but
2546 -- must still be in the context.
2551 Activate_Elaborate_All_Desirable
(Call
, Elab_Unit
);
2552 Set_Suppress_Elaboration_Warnings
(Elab_Unit
, True);
2553 end Set_Elaboration_Constraint
;
2555 ----------------------
2556 -- Has_Generic_Body --
2557 ----------------------
2559 function Has_Generic_Body
(N
: Node_Id
) return Boolean is
2560 Ent
: constant Entity_Id
:= Get_Generic_Entity
(N
);
2561 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
2564 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
;
2565 -- Determine if the list of nodes headed by N and linked by Next
2566 -- contains a package body for the package spec entity E, and if
2567 -- so return the package body. If not, then returns Empty.
2569 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
;
2570 -- This procedure is called load the unit whose name is given by Nam.
2571 -- This unit is being loaded to see whether it contains an optional
2572 -- generic body. The returned value is the loaded unit, which is
2573 -- always a package body (only package bodies can contain other
2574 -- entities in the sense in which Has_Generic_Body is interested).
2575 -- We only attempt to load bodies if we are generating code. If we
2576 -- are in semantics check only mode, then it would be wrong to load
2577 -- bodies that are not required from a semantic point of view, so
2578 -- in this case we return Empty. The result is that the caller may
2579 -- incorrectly decide that a generic spec does not have a body when
2580 -- in fact it does, but the only harm in this is that some warnings
2581 -- on elaboration problems may be lost in semantic checks only mode,
2582 -- which is not big loss. We also return Empty if we go for a body
2583 -- and it is not there.
2585 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
;
2586 -- PE is the entity for a package spec. This function locates the
2587 -- corresponding package body, returning Empty if none is found.
2588 -- The package body returned is fully parsed but may not yet be
2589 -- analyzed, so only syntactic fields should be referenced.
2595 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
is
2600 while Present
(Nod
) loop
2602 -- If we found the package body we are looking for, return it
2604 if Nkind
(Nod
) = N_Package_Body
2605 and then Chars
(Defining_Unit_Name
(Nod
)) = Chars
(E
)
2609 -- If we found the stub for the body, go after the subunit,
2610 -- loading it if necessary.
2612 elsif Nkind
(Nod
) = N_Package_Body_Stub
2613 and then Chars
(Defining_Identifier
(Nod
)) = Chars
(E
)
2615 if Present
(Library_Unit
(Nod
)) then
2616 return Unit
(Library_Unit
(Nod
));
2619 return Load_Package_Body
(Get_Unit_Name
(Nod
));
2622 -- If neither package body nor stub, keep looking on chain
2632 -----------------------
2633 -- Load_Package_Body --
2634 -----------------------
2636 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
is
2637 U
: Unit_Number_Type
;
2640 if Operating_Mode
/= Generate_Code
then
2653 return Unit
(Cunit
(U
));
2656 end Load_Package_Body
;
2658 -------------------------------
2659 -- Locate_Corresponding_Body --
2660 -------------------------------
2662 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
is
2663 Spec
: constant Node_Id
:= Declaration_Node
(PE
);
2664 Decl
: constant Node_Id
:= Parent
(Spec
);
2665 Scop
: constant Entity_Id
:= Scope
(PE
);
2669 if Is_Library_Level_Entity
(PE
) then
2671 -- If package is a library unit that requires a body, we have
2672 -- no choice but to go after that body because it might contain
2673 -- an optional body for the original generic package.
2675 if Unit_Requires_Body
(PE
) then
2677 -- Load the body. Note that we are a little careful here to
2678 -- use Spec to get the unit number, rather than PE or Decl,
2679 -- since in the case where the package is itself a library
2680 -- level instantiation, Spec will properly reference the
2681 -- generic template, which is what we really want.
2685 (Get_Body_Name
(Unit_Name
(Get_Source_Unit
(Spec
))));
2687 -- But if the package is a library unit that does NOT require
2688 -- a body, then no body is permitted, so we are sure that there
2689 -- is no body for the original generic package.
2695 -- Otherwise look and see if we are embedded in a further package
2697 elsif Is_Package_Or_Generic_Package
(Scop
) then
2699 -- If so, get the body of the enclosing package, and look in
2700 -- its package body for the package body we are looking for.
2702 PBody
:= Locate_Corresponding_Body
(Scop
);
2707 return Find_Body_In
(PE
, First
(Declarations
(PBody
)));
2710 -- If we are not embedded in a further package, then the body
2711 -- must be in the same declarative part as we are.
2714 return Find_Body_In
(PE
, Next
(Decl
));
2716 end Locate_Corresponding_Body
;
2718 -- Start of processing for Has_Generic_Body
2721 if Present
(Corresponding_Body
(Decl
)) then
2724 elsif Unit_Requires_Body
(Ent
) then
2727 -- Compilation units cannot have optional bodies
2729 elsif Is_Compilation_Unit
(Ent
) then
2732 -- Otherwise look at what scope we are in
2735 Scop
:= Scope
(Ent
);
2737 -- Case of entity is in other than a package spec, in this case
2738 -- the body, if present, must be in the same declarative part.
2740 if not Is_Package_Or_Generic_Package
(Scop
) then
2745 -- Declaration node may get us a spec, so if so, go to
2746 -- the parent declaration.
2748 P
:= Declaration_Node
(Ent
);
2749 while not Is_List_Member
(P
) loop
2753 return Present
(Find_Body_In
(Ent
, Next
(P
)));
2756 -- If the entity is in a package spec, then we have to locate
2757 -- the corresponding package body, and look there.
2761 PBody
: constant Node_Id
:= Locate_Corresponding_Body
(Scop
);
2769 (Find_Body_In
(Ent
, (First
(Declarations
(PBody
)))));
2774 end Has_Generic_Body
;
2776 -----------------------
2777 -- Insert_Elab_Check --
2778 -----------------------
2780 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
) is
2782 Loc
: constant Source_Ptr
:= Sloc
(N
);
2785 -- If expansion is disabled, do not generate any checks. Also
2786 -- skip checks if any subunits are missing because in either
2787 -- case we lack the full information that we need, and no object
2788 -- file will be created in any case.
2790 if not Expander_Active
or else Subunits_Missing
then
2794 -- If we have a generic instantiation, where Instance_Spec is set,
2795 -- then this field points to a generic instance spec that has
2796 -- been inserted before the instantiation node itself, so that
2797 -- is where we want to insert a check.
2799 if Nkind
(N
) in N_Generic_Instantiation
2800 and then Present
(Instance_Spec
(N
))
2802 Nod
:= Instance_Spec
(N
);
2807 -- If we are inserting at the top level, insert in Aux_Decls
2809 if Nkind
(Parent
(Nod
)) = N_Compilation_Unit
then
2811 ADN
: constant Node_Id
:= Aux_Decls_Node
(Parent
(Nod
));
2817 Make_Raise_Program_Error
(Loc
,
2818 Reason
=> PE_Access_Before_Elaboration
);
2821 Make_Raise_Program_Error
(Loc
,
2822 Condition
=> Make_Op_Not
(Loc
, C
),
2823 Reason
=> PE_Access_Before_Elaboration
);
2826 if No
(Declarations
(ADN
)) then
2827 Set_Declarations
(ADN
, New_List
(R
));
2829 Append_To
(Declarations
(ADN
), R
);
2835 -- Otherwise just insert before the node in question. However, if
2836 -- the context of the call has already been analyzed, an insertion
2837 -- will not work if it depends on subsequent expansion (e.g. a call in
2838 -- a branch of a short-circuit). In that case we replace the call with
2839 -- a conditional expression, or with a Raise if it is unconditional.
2840 -- Unfortunately this does not work if the call has a dynamic size,
2841 -- because gigi regards it as a dynamic-sized temporary. If such a call
2842 -- appears in a short-circuit expression, the elaboration check will be
2843 -- missed (rare enough ???). Otherwise, the code below inserts the check
2844 -- at the appropriate place before the call. Same applies in the even
2845 -- rarer case the return type has a known size but is unconstrained.
2848 if Nkind
(N
) = N_Function_Call
2849 and then Analyzed
(Parent
(N
))
2850 and then Size_Known_At_Compile_Time
(Etype
(N
))
2852 (not Has_Discriminants
(Etype
(N
))
2853 or else Is_Constrained
(Etype
(N
)))
2857 Typ
: constant Entity_Id
:= Etype
(N
);
2858 Chk
: constant Boolean := Do_Range_Check
(N
);
2860 R
: constant Node_Id
:=
2861 Make_Raise_Program_Error
(Loc
,
2862 Reason
=> PE_Access_Before_Elaboration
);
2872 Make_Conditional_Expression
(Loc
,
2873 Expressions
=> New_List
(C
, Relocate_Node
(N
), R
)));
2876 Analyze_And_Resolve
(N
, Typ
);
2878 -- If the original call requires a range check, so does the
2879 -- conditional expression.
2882 Enable_Range_Check
(N
);
2884 Set_Do_Range_Check
(N
, False);
2891 Make_Raise_Program_Error
(Loc
,
2892 Reason
=> PE_Access_Before_Elaboration
));
2895 Make_Raise_Program_Error
(Loc
,
2899 Reason
=> PE_Access_Before_Elaboration
));
2903 end Insert_Elab_Check
;
2909 procedure Output_Calls
(N
: Node_Id
) is
2912 function Is_Printable_Error_Name
(Nm
: Name_Id
) return Boolean;
2913 -- An internal function, used to determine if a name, Nm, is either
2914 -- a non-internal name, or is an internal name that is printable
2915 -- by the error message circuits (i.e. it has a single upper
2916 -- case letter at the end).
2918 function Is_Printable_Error_Name
(Nm
: Name_Id
) return Boolean is
2920 if not Is_Internal_Name
(Nm
) then
2923 elsif Name_Len
= 1 then
2927 Name_Len
:= Name_Len
- 1;
2928 return not Is_Internal_Name
;
2930 end Is_Printable_Error_Name
;
2932 -- Start of processing for Output_Calls
2935 for J
in reverse 1 .. Elab_Call
.Last
loop
2936 Error_Msg_Sloc
:= Elab_Call
.Table
(J
).Cloc
;
2938 Ent
:= Elab_Call
.Table
(J
).Ent
;
2940 if Is_Generic_Unit
(Ent
) then
2941 Error_Msg_NE
("\?& instantiated #", N
, Ent
);
2943 elsif Is_Init_Proc
(Ent
) then
2944 Error_Msg_N
("\?initialization procedure called #", N
);
2946 elsif Is_Printable_Error_Name
(Chars
(Ent
)) then
2947 Error_Msg_NE
("\?& called #", N
, Ent
);
2950 Error_Msg_N
("\? called #", N
);
2955 ----------------------------
2956 -- Same_Elaboration_Scope --
2957 ----------------------------
2959 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean is
2964 -- Find elaboration scope for Scop1
2965 -- This is either a subprogram or a compilation unit.
2968 while S1
/= Standard_Standard
2969 and then not Is_Compilation_Unit
(S1
)
2970 and then (Ekind
(S1
) = E_Package
2972 Ekind
(S1
) = E_Protected_Type
2974 Ekind
(S1
) = E_Block
)
2979 -- Find elaboration scope for Scop2
2982 while S2
/= Standard_Standard
2983 and then not Is_Compilation_Unit
(S2
)
2984 and then (Ekind
(S2
) = E_Package
2986 Ekind
(S2
) = E_Protected_Type
2988 Ekind
(S2
) = E_Block
)
2994 end Same_Elaboration_Scope
;
3000 procedure Set_C_Scope
is
3002 while not Is_Compilation_Unit
(C_Scope
) loop
3003 C_Scope
:= Scope
(C_Scope
);
3011 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
is
3015 -- Check for case of body entity
3016 -- Why is the check for E_Void needed???
3018 if Ekind
(E
) = E_Void
3019 or else Ekind
(E
) = E_Subprogram_Body
3020 or else Ekind
(E
) = E_Package_Body
3025 Decl
:= Parent
(Decl
);
3026 exit when Nkind
(Decl
) in N_Proper_Body
;
3029 return Corresponding_Spec
(Decl
);
3040 procedure Supply_Bodies
(N
: Node_Id
) is
3042 if Nkind
(N
) = N_Subprogram_Declaration
then
3044 Ent
: constant Entity_Id
:= Defining_Unit_Name
(Specification
(N
));
3046 Set_Is_Imported
(Ent
);
3047 Set_Convention
(Ent
, Convention_Stubbed
);
3050 elsif Nkind
(N
) = N_Package_Declaration
then
3052 Spec
: constant Node_Id
:= Specification
(N
);
3054 Push_Scope
(Defining_Unit_Name
(Spec
));
3055 Supply_Bodies
(Visible_Declarations
(Spec
));
3056 Supply_Bodies
(Private_Declarations
(Spec
));
3062 procedure Supply_Bodies
(L
: List_Id
) is
3067 while Present
(Elmt
) loop
3068 Supply_Bodies
(Elmt
);
3078 function Within
(E1
, E2
: Entity_Id
) return Boolean is
3087 elsif Scop
= Standard_Standard
then
3091 Scop
:= Scope
(Scop
);
3095 raise Program_Error
;
3098 --------------------------
3099 -- Within_Elaborate_All --
3100 --------------------------
3102 function Within_Elaborate_All
(E
: Entity_Id
) return Boolean is
3105 Elab_Id
: Entity_Id
;
3109 Item
:= First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
3110 while Present
(Item
) loop
3111 if Nkind
(Item
) = N_Pragma
3112 and then Get_Pragma_Id
(Chars
(Item
)) = Pragma_Elaborate_All
3114 -- Return if some previous error on the pragma itself
3116 if Error_Posted
(Item
) then
3122 (Expression
(First
(Pragma_Argument_Associations
(Item
))));
3124 Par
:= Parent
(Unit_Declaration_Node
(Elab_Id
));
3126 Item2
:= First
(Context_Items
(Par
));
3127 while Present
(Item2
) loop
3128 if Nkind
(Item2
) = N_With_Clause
3129 and then Entity
(Name
(Item2
)) = E
3142 end Within_Elaborate_All
;