1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1997-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Checks
; use Checks
;
31 with Debug
; use Debug
;
32 with Einfo
; use Einfo
;
33 with Elists
; use Elists
;
34 with Errout
; use Errout
;
35 with Exp_Util
; use Exp_Util
;
36 with Expander
; use Expander
;
37 with Fname
; use Fname
;
39 with Lib
.Load
; use Lib
.Load
;
40 with Namet
; use Namet
;
41 with Nlists
; use Nlists
;
42 with Nmake
; use Nmake
;
44 with Output
; use Output
;
45 with Restrict
; use Restrict
;
47 with Sem_Cat
; use Sem_Cat
;
48 with Sem_Ch7
; use Sem_Ch7
;
49 with Sem_Ch8
; use Sem_Ch8
;
50 with Sem_Res
; use Sem_Res
;
51 with Sem_Util
; use Sem_Util
;
52 with Sinfo
; use Sinfo
;
53 with Sinput
; use Sinput
;
54 with Snames
; use Snames
;
55 with Stand
; use Stand
;
57 with Tbuild
; use Tbuild
;
58 with Uname
; use Uname
;
60 package body Sem_Elab
is
62 -- The following table records the recursive call chain for output
63 -- in the Output routine. Each entry records the call node and the
64 -- entity of the called routine. The number of entries in the table
65 -- (i.e. the value of Elab_Call.Last) indicates the current depth
66 -- of recursion and is used to 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.
82 -- It holds the entities for all subprograms that have been examined
83 -- for this particular outer level call, and is used to prevent both
84 -- infinite 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
95 -- until all generics are instantiated, and in particular that all
96 -- generic bodies have been inserted. We need to delay, because we
97 -- need to be able to look through the inserted bodies.
99 type Delay_Element
is record
101 -- The parameter N from the call to Check_Internal_Call. Note that
102 -- this node may get rewritten over the delay period by expansion
103 -- in the call case (but not in the instantiation case).
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
113 -- the delayed call, so that we do this in the right scope.
115 From_Elab_Code
: Boolean;
116 -- Save indication of whether this call is from elaboration code
118 Outer_Scope
: Entity_Id
;
119 -- Save scope of outer level call
123 package Delay_Check
is new Table
.Table
(
124 Table_Component_Type
=> Delay_Element
,
125 Table_Index_Type
=> Int
,
126 Table_Low_Bound
=> 1,
127 Table_Initial
=> 1000,
128 Table_Increment
=> 100,
129 Table_Name
=> "Delay_Check");
132 -- Top level scope of current scope. We need to compute this only
133 -- once at the outer level, i.e. for a call to Check_Elab_Call from
134 -- outside this unit.
136 Outer_Level_Sloc
: Source_Ptr
;
137 -- Save Sloc value for outer level call node for comparisons of source
138 -- locations. A body is too late if it appears after the *outer* level
139 -- call, not the particular call that is being analyzed.
141 From_Elab_Code
: Boolean;
142 -- This flag shows whether the outer level call currently being examined
143 -- is or is not in elaboration code. We are only interested in calls to
144 -- routines in other units if this flag is True.
146 In_Task_Activation
: Boolean := False;
147 -- This flag indicates whether we are performing elaboration checks on
148 -- task procedures, at the point of activation. If true, we do not trace
149 -- internal calls in these procedures, because all local bodies are known
152 Delaying_Elab_Checks
: Boolean := True;
153 -- This is set True till the compilation is complete, including the
154 -- insertion of all instance bodies. Then when Check_Elab_Calls is
155 -- called, the delay table is used to make the delayed calls and
156 -- this flag is reset to False, so that the calls are processed
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 -- Note: Outer_Scope in all these calls represents the scope of
163 -- interest of the outer level call. If it is set to Standard_Standard,
164 -- then it means the outer level call was at elaboration level, and that
165 -- thus all calls are of interest. If it was set to some other scope,
166 -- then the original call was an inner call, and we are not interested
167 -- in calls that go outside this scope.
169 procedure Check_A_Call
172 Outer_Scope
: Entity_Id
;
173 Inter_Unit_Only
: Boolean;
174 Generate_Warnings
: Boolean := True);
175 -- This is the internal recursive routine that is called to check for
176 -- a possible elaboration error. The argument N is a subprogram call
177 -- or generic instantiation to be checked, and E is the entity of
178 -- the called subprogram, or instantiated generic unit. The flag
179 -- Outer_Scope is the outer level scope for the original call.
180 -- Inter_Unit_Only is set if the call is only to be checked in the
181 -- case where it is to another unit (and skipped if within a unit).
182 -- Generate_Warnings is set to True to suppress warning messages
183 -- about missing pragma Elaborate_All's. These messages are not
184 -- wanted for inner calls in the dynamic model.
186 procedure Check_Bad_Instantiation
(N
: Node_Id
);
187 -- N is a node for an instantiation (if called with any other node kind,
188 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
189 -- the special case of a generic instantiation of a generic spec in the
190 -- same declarative part as the instantiation where a body is present and
191 -- has not yet been seen. This is an obvious error, but needs to be checked
192 -- specially at the time of the instantiation, since it is a case where we
193 -- cannot insert the body anywhere. If this case is detected, warnings are
194 -- generated, and a raise of Program_Error is inserted. In addition any
195 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
196 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
197 -- flag as an indication that no attempt should be made to insert an
200 procedure Check_Internal_Call
203 Outer_Scope
: Entity_Id
;
204 Orig_Ent
: Entity_Id
);
205 -- N is a function call or procedure statement call node and E is
206 -- the entity of the called function, which is within the current
207 -- compilation unit (where subunits count as part of the parent).
208 -- This call checks if this call, or any call within any accessed
209 -- body could cause an ABE, and if so, outputs a warning. Orig_Ent
210 -- differs from E only in the case of renamings, and points to the
211 -- original name of the entity. This is used for error messages.
212 -- Outer_Scope is the outer level scope for the original call.
214 procedure Check_Internal_Call_Continue
217 Outer_Scope
: Entity_Id
;
218 Orig_Ent
: Entity_Id
);
219 -- The processing for Check_Internal_Call is divided up into two phases,
220 -- and this represents the second phase. The second phase is delayed if
221 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
222 -- phase makes an entry in the Delay_Check table, which is processed
223 -- when Check_Elab_Calls is called. N, E and Orig_Ent are as for the call
224 -- to Check_Internal_Call. Outer_Scope is the outer level scope for
225 -- the original call.
227 function Has_Generic_Body
(N
: Node_Id
) return Boolean;
228 -- N is a generic package instantiation node, and this routine determines
229 -- if this package spec does in fact have a generic body. If so, then
230 -- True is returned, otherwise False. Note that this is not at all the
231 -- same as checking if the unit requires a body, since it deals with
232 -- the case of optional bodies accurately (i.e. if a body is optional,
233 -- then it looks to see if a body is actually present). Note: this
234 -- function can only do a fully correct job if in generating code mode
235 -- where all bodies have to be present. If we are operating in semantics
236 -- check only mode, then in some cases of optional bodies, a result of
237 -- False may incorrectly be given. In practice this simply means that
238 -- some cases of warnings for incorrect order of elaboration will only
239 -- be given when generating code, which is not a big problem (and is
240 -- inevitable, given the optional body semantics of Ada).
242 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
);
243 -- Given code for an elaboration check (or unconditional raise if
244 -- the check is not needed), inserts the code in the appropriate
245 -- place. N is the call or instantiation node for which the check
246 -- code is required. C is the test whose failure triggers the raise.
248 procedure Output_Calls
(N
: Node_Id
);
249 -- Outputs chain of calls stored in the Elab_Call table. The caller
250 -- has already generated the main warning message, so the warnings
251 -- generated are all continuation messages. The argument is the
252 -- call node at which the messages are to be placed.
254 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean;
255 -- Given two scopes, determine whether they are the same scope from an
256 -- elaboration point of view, i.e. packages and blocks are ignored.
258 procedure Set_C_Scope
;
259 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
260 -- to be the enclosing compilation unit of this scope.
262 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
;
263 -- Given a compilation unit entity, if it is a spec entity, it is
264 -- returned unchanged. If it is a body entity, then the spec for
265 -- the corresponding spec is returned
267 procedure Supply_Bodies
(N
: Node_Id
);
268 -- Given a node, N, that is either a subprogram declaration or a package
269 -- declaration, this procedure supplies dummy bodies for the subprogram
270 -- or for all subprograms in the package. If the given node is not one
271 -- of these two possibilities, then Supply_Bodies does nothing. The
272 -- dummy body is supplied by setting the subprogram to be Imported with
273 -- convention Stubbed.
275 procedure Supply_Bodies
(L
: List_Id
);
276 -- Calls Supply_Bodies for all elements of the given list L.
278 function Within
(E1
, E2
: Entity_Id
) return Boolean;
279 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or
280 -- is one of its contained scopes, False otherwise.
286 procedure Check_A_Call
289 Outer_Scope
: Entity_Id
;
290 Inter_Unit_Only
: Boolean;
291 Generate_Warnings
: Boolean := True)
293 Loc
: constant Source_Ptr
:= Sloc
(N
);
298 -- Top level scope of entity for called subprogram
300 Body_Acts_As_Spec
: Boolean;
301 -- Set to true if call is to body acting as spec (no separate spec)
303 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
304 -- Indicates if we have instantiation case
306 Caller_Unit_Internal
: Boolean;
307 Callee_Unit_Internal
: Boolean;
309 Inst_Caller
: Source_Ptr
;
310 Inst_Callee
: Source_Ptr
;
312 Unit_Caller
: Unit_Number_Type
;
313 Unit_Callee
: Unit_Number_Type
;
315 Cunit_SW
: Boolean := False;
316 -- Set to suppress warnings for case of external reference where
317 -- one of the enclosing scopes has the Suppress_Elaboration_Warnings
318 -- flag set. For the internal case, we ignore this flag.
320 Cunit_SC
: Boolean := False;
321 -- Set to suppress dynamic elaboration checks where one of the
322 -- enclosing scopes has Suppress_Elaboration_Checks set. For
323 -- the internal case, we ignore this flag.
326 -- Go to parent for derived subprogram, or to original subprogram
327 -- in the case of a renaming (Alias covers both these cases)
331 if Suppress_Elaboration_Warnings
(Ent
) then
335 -- Nothing to do for imported entities,
337 if Is_Imported
(Ent
) then
341 exit when Inst_Case
or else No
(Alias
(Ent
));
345 Decl
:= Unit_Declaration_Node
(Ent
);
347 if Nkind
(Decl
) = N_Subprogram_Body
then
348 Body_Acts_As_Spec
:= True;
350 elsif Nkind
(Decl
) = N_Subprogram_Declaration
351 or else Nkind
(Decl
) = N_Subprogram_Body_Stub
354 Body_Acts_As_Spec
:= False;
356 -- If we have none of an instantiation, subprogram body or
357 -- subprogram declaration, then it is not a case that we want
358 -- to check. (One case is a call to a generic formal subprogram,
359 -- where we do not want the check in the template).
367 if Suppress_Elaboration_Warnings
(E_Scope
) then
371 if Suppress_Elaboration_Checks
(E_Scope
) then
375 -- Exit when we get to compilation unit, not counting subunits
377 exit when Is_Compilation_Unit
(E_Scope
)
378 and then (Is_Child_Unit
(E_Scope
)
379 or else Scope
(E_Scope
) = Standard_Standard
);
381 -- If we did not find a compilation unit, other than standard,
382 -- then nothing to check (happens in some instantiation cases)
384 if E_Scope
= Standard_Standard
then
387 -- Otherwise move up a scope looking for compilation unit
390 E_Scope
:= Scope
(E_Scope
);
394 -- No checks needed for pure or preelaborated compilation units
397 or else Is_Preelaborated
(E_Scope
)
402 -- If the generic entity is within a deeper instance than we are, then
403 -- either the instantiation to which we refer itself caused an ABE, in
404 -- which case that will be handled separately. Otherwise, we know that
405 -- the body we need appears as needed at the point of the instantiation.
406 -- However, this assumption is only valid if we are in static mode.
408 if not Dynamic_Elaboration_Checks
409 and then Instantiation_Depth
(Sloc
(Ent
)) >
410 Instantiation_Depth
(Sloc
(N
))
415 -- Do not give a warning for a package with no body
417 if Ekind
(Ent
) = E_Generic_Package
418 and then not Has_Generic_Body
(N
)
423 -- Case of entity is not in current unit (i.e. with'ed unit case)
425 if E_Scope
/= C_Scope
then
427 -- We are only interested in such calls if the outer call was from
428 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
430 if not From_Elab_Code
and then not Dynamic_Elaboration_Checks
then
434 -- Nothing to do if some scope said to ignore warnings
440 -- Nothing to do for a generic instance, because in this case
441 -- the checking was at the point of instantiation of the generic
442 -- However, this shortcut is only applicable in static mode.
444 if Is_Generic_Instance
(Ent
) and not Dynamic_Elaboration_Checks
then
448 -- Nothing to do if subprogram with no separate spec
450 if Body_Acts_As_Spec
then
454 -- Check cases of internal units
456 Callee_Unit_Internal
:=
457 Is_Internal_File_Name
458 (Unit_File_Name
(Get_Source_Unit
(E_Scope
)));
460 -- Do not give a warning if the with'ed unit is internal
461 -- and this is the generic instantiation case (this saves a
462 -- lot of hassle dealing with the Text_IO special child units)
464 if Callee_Unit_Internal
and Inst_Case
then
468 if C_Scope
= Standard_Standard
then
469 Caller_Unit_Internal
:= False;
471 Caller_Unit_Internal
:=
472 Is_Internal_File_Name
473 (Unit_File_Name
(Get_Source_Unit
(C_Scope
)));
476 -- Do not give a warning if the with'ed unit is internal
477 -- and the caller is not internal (since the binder always
478 -- elaborates internal units first).
480 if Callee_Unit_Internal
and (not Caller_Unit_Internal
) then
484 -- For now, if debug flag -gnatdE is not set, do no checking for
485 -- one internal unit withing another. This fixes the problem with
486 -- the sgi build and storage errors. To be resolved later ???
488 if (Callee_Unit_Internal
and Caller_Unit_Internal
)
489 and then not Debug_Flag_EE
496 -- If the call is in an instance, and the called entity is not
497 -- defined in the same instance, then the elaboration issue
498 -- focuses around the unit containing the template, it is
499 -- this unit which requires an Elaborate_All.
501 -- However, if we are doing dynamic elaboration, we need to
502 -- chase the call in the usual manner.
504 -- We do not handle the case of calling a generic formal correctly
505 -- in the static case. See test 4703-004 to explore this gap ???
507 Inst_Caller
:= Instantiation
(Get_Source_File_Index
(Sloc
(N
)));
508 Inst_Callee
:= Instantiation
(Get_Source_File_Index
(Sloc
(Ent
)));
510 if Inst_Caller
= No_Location
then
511 Unit_Caller
:= No_Unit
;
513 Unit_Caller
:= Get_Source_Unit
(N
);
516 if Inst_Callee
= No_Location
then
517 Unit_Callee
:= No_Unit
;
519 Unit_Callee
:= Get_Source_Unit
(Ent
);
522 if Unit_Caller
/= No_Unit
523 and then Unit_Callee
/= Unit_Caller
524 and then Unit_Callee
/= No_Unit
525 and then not Dynamic_Elaboration_Checks
527 E_Scope
:= Spec_Entity
(Cunit_Entity
(Unit_Caller
));
529 -- If we don't get a spec entity, just ignore call. Not
530 -- quite clear why this check is necessary.
536 -- Otherwise step to enclosing compilation unit
538 while not Is_Compilation_Unit
(E_Scope
) loop
539 E_Scope
:= Scope
(E_Scope
);
542 -- For the case of not in an instance, or call within instance
543 -- We recompute E_Scope for the error message, since we
544 -- do NOT want to go to the unit which has the ultimate
545 -- declaration in the case of renaming and derivation and
546 -- we also want to go to the generic unit in the case of
547 -- an instance, and no further.
550 -- Loop to carefully follow renamings and derivations
551 -- one step outside the current unit, but not further.
554 and then Present
(Alias
(Ent
))
556 E_Scope
:= Alias
(Ent
);
562 while not Is_Compilation_Unit
(E_Scope
) loop
563 E_Scope
:= Scope
(E_Scope
);
566 -- If E_Scope is the same as C_Scope, it means that there
567 -- definitely was a local renaming or derivation, and we
568 -- are not yet out of the current unit.
570 exit when E_Scope
/= C_Scope
;
576 if not Suppress_Elaboration_Warnings
(Ent
)
577 and then not Suppress_Elaboration_Warnings
(E_Scope
)
578 and then Elab_Warnings
579 and then Generate_Warnings
581 Warn_On_Instance
:= True;
585 ("instantiation of& may raise Program_Error?", N
, Ent
);
588 ("call to & may raise Program_Error?", N
, Ent
);
591 Error_Msg_Qual_Level
:= Nat
'Last;
593 ("\missing pragma Elaborate_All for&?", N
, E_Scope
);
594 Error_Msg_Qual_Level
:= 0;
596 Warn_On_Instance
:= False;
598 -- Set flag to prevent further warnings for same unit
599 -- unless in All_Errors_Mode.
601 if not All_Errors_Mode
and not Dynamic_Elaboration_Checks
then
602 Set_Suppress_Elaboration_Warnings
(E_Scope
);
606 -- Check for runtime elaboration check required
608 if Dynamic_Elaboration_Checks
then
609 if not Elaboration_Checks_Suppressed
(Ent
)
610 and then not Suppress_Elaboration_Checks
(E_Scope
)
611 and then not Cunit_SC
613 -- Runtime elaboration check required. generate check of the
614 -- elaboration Boolean for the unit containing the entity.
616 Insert_Elab_Check
(N
,
617 Make_Attribute_Reference
(Loc
,
618 Attribute_Name
=> Name_Elaborated
,
621 (Spec_Entity
(E_Scope
), Loc
)));
624 -- If no dynamic check required, then ask binder to guarantee
625 -- that the necessary elaborations will be done properly!
628 if not Suppress_Elaboration_Warnings
(E
)
629 and then not Suppress_Elaboration_Warnings
(E_Scope
)
630 and then Elab_Warnings
631 and then Generate_Warnings
632 and then not Inst_Case
634 Error_Msg_Node_2
:= E_Scope
;
635 Error_Msg_NE
("call to& in elaboration code " &
636 "requires pragma Elaborate_All on&?", N
, E
);
639 Set_Elaborate_All_Desirable
(E_Scope
);
640 Set_Suppress_Elaboration_Warnings
(E_Scope
);
643 -- Case of entity is in same unit as call or instantiation
645 elsif not Inter_Unit_Only
then
646 Check_Internal_Call
(N
, Ent
, Outer_Scope
, E
);
651 -----------------------------
652 -- Check_Bad_Instantiation --
653 -----------------------------
655 procedure Check_Bad_Instantiation
(N
: Node_Id
) is
660 -- Nothing to do if we do not have an instantiation (happens in some
661 -- error cases, and also in the formal package declaration case)
663 if Nkind
(N
) not in N_Generic_Instantiation
then
666 -- Nothing to do if errors already detected (avoid cascaded errors)
668 elsif Errors_Detected
/= 0 then
671 -- Nothing to do if not in full analysis mode
673 elsif not Full_Analysis
then
676 -- Nothing to do if inside a generic template
678 elsif Inside_A_Generic
then
681 -- Nothing to do if a library level instantiation
683 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
686 -- Nothing to do if we are compiling a proper body for semantic
687 -- purposes only. The generic body may be in another proper body.
690 Nkind
(Parent
(Unit_Declaration_Node
(Main_Unit_Entity
))) = N_Subunit
698 -- The case we are interested in is when the generic spec is in the
699 -- current declarative part
701 if not Same_Elaboration_Scope
(Current_Scope
, Scope
(Ent
))
702 or else not In_Same_Extended_Unit
(Sloc
(N
), Sloc
(Ent
))
707 -- If the generic entity is within a deeper instance than we are, then
708 -- either the instantiation to which we refer itself caused an ABE, in
709 -- which case that will be handled separately. Otherwise, we know that
710 -- the body we need appears as needed at the point of the instantiation.
711 -- If they are both at the same level but not within the same instance
712 -- then the body of the generic will be in the earlier instance.
715 D1
: constant Int
:= Instantiation_Depth
(Sloc
(Ent
));
716 D2
: constant Int
:= Instantiation_Depth
(Sloc
(N
));
723 and then Is_Generic_Instance
(Scope
(Ent
))
724 and then not In_Open_Scopes
(Scope
(Ent
))
730 -- Now we can proceed, if the entity being called has a completion,
731 -- then we are definitely OK, since we have already seen the body.
733 if Has_Completion
(Ent
) then
737 -- If there is no body, then nothing to do
739 if not Has_Generic_Body
(N
) then
743 -- Here we definitely have a bad instantiation
746 ("?cannot instantiate& before body seen", N
, Ent
);
748 if Present
(Instance_Spec
(N
)) then
749 Supply_Bodies
(Instance_Spec
(N
));
753 ("\?Program_Error will be raised at run time", N
);
754 Insert_Elab_Check
(N
);
755 Set_ABE_Is_Certain
(N
);
757 end Check_Bad_Instantiation
;
759 ---------------------
760 -- Check_Elab_Call --
761 ---------------------
763 procedure Check_Elab_Call
765 Outer_Scope
: Entity_Id
:= Empty
)
772 -- For an entry call, check relevant restriction
774 if Nkind
(N
) = N_Entry_Call_Statement
775 and then not In_Subprogram_Or_Concurrent_Unit
777 Check_Restriction
(No_Entry_Calls_In_Elaboration_Code
, N
);
779 -- Nothing to do if this is not a call (happens in some error
780 -- conditions, and in some cases where rewriting occurs).
782 elsif Nkind
(N
) /= N_Function_Call
783 and then Nkind
(N
) /= N_Procedure_Call_Statement
787 -- Nothing to do if this is a call already rewritten for elab checking.
789 elsif Nkind
(Parent
(N
)) = N_Conditional_Expression
then
792 -- Nothing to do if inside a generic template
794 elsif Inside_A_Generic
795 and then not Present
(Enclosing_Generic_Body
(N
))
800 -- Here we have a call at elaboration time which must be checked
802 if Debug_Flag_LL
then
803 Write_Str
(" Check_Elab_Call: ");
806 or else not Is_Entity_Name
(Name
(N
))
808 Write_Str
("<<not entity name>> ");
810 Write_Name
(Chars
(Entity
(Name
(N
))));
813 Write_Str
(" call at ");
814 Write_Location
(Sloc
(N
));
818 -- Climb up the tree to make sure we are not inside a
819 -- default expression of a parameter specification or
820 -- a record component, since in both these cases, we
821 -- will be doing the actual call later, not now, and it
822 -- is at the time of the actual call (statically speaking)
823 -- that we must do our static check, not at the time of
824 -- its initial analysis).
827 while Present
(P
) loop
828 if Nkind
(P
) = N_Parameter_Specification
830 Nkind
(P
) = N_Component_Declaration
838 -- Stuff that happens only at the outer level
840 if No
(Outer_Scope
) then
841 Elab_Visited
.Set_Last
(0);
843 -- Nothing to do if current scope is Standard (this is a bit
844 -- odd, but it happens in the case of generic instantiations).
846 C_Scope
:= Current_Scope
;
848 if C_Scope
= Standard_Standard
then
852 -- First case, we are in elaboration code
854 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
856 if From_Elab_Code
then
858 -- Complain if call that comes from source in preelaborated
859 -- unit and we are not inside a subprogram (i.e. we are in
862 if Comes_From_Source
(N
)
863 and then In_Preelaborated_Unit
866 ("non-static call not allowed in preelaborated unit", N
);
870 -- Second case, we are inside a subprogram or concurrent unit
871 -- i.e, we are not in elaboration code.
874 -- In this case, the issue is whether we are inside the
875 -- declarative part of the unit in which we live, or inside
876 -- its statements. In the latter case, there is no issue of
877 -- ABE calls at this level (a call from outside to the unit
878 -- in which we live might cause an ABE, but that will be
879 -- detected when we analyze that outer level call, as it
880 -- recurses into the called unit).
882 -- Climb up the tree, doing this test, and also testing
883 -- for being inside a default expression, which, as
884 -- discussed above, is not checked at this stage.
893 -- If we find a parentless subtree, it seems safe to
894 -- assume that we are not in a declarative part and
895 -- that no checking is required.
901 if Is_List_Member
(P
) then
902 L
:= List_Containing
(P
);
909 exit when Nkind
(P
) = N_Subunit
;
911 -- Filter out case of default expressions, where
912 -- we do not do the check at this stage.
914 if Nkind
(P
) = N_Parameter_Specification
916 Nkind
(P
) = N_Component_Declaration
921 if Nkind
(P
) = N_Subprogram_Body
923 Nkind
(P
) = N_Protected_Body
925 Nkind
(P
) = N_Task_Body
927 Nkind
(P
) = N_Block_Statement
929 if L
= Declarations
(P
) then
932 -- We are not in elaboration code, but we are doing
933 -- dynamic elaboration checks, in this case, we still
934 -- need to do the call, since the subprogram we are in
935 -- could be called from another unit, also in dynamic
936 -- elaboration check mode, at elaboration time.
938 elsif Dynamic_Elaboration_Checks
then
940 -- This is a rather new check, going into version
941 -- 3.14a1 for the first time (V1.80 of this unit),
942 -- so we provide a debug flag to enable it. That
943 -- way we have an easy work around for regressions
944 -- that are caused by this new check. This debug
945 -- flag can be removed later.
947 if Debug_Flag_DD
then
951 -- Do the check in this case
955 -- Static model, call is not in elaboration code, we
956 -- never need to worry, because in the static model
957 -- the top level caller always takes care of things.
968 -- Retrieve called entity. If this is a call to a protected subprogram,
969 -- the entity is a selected component.
970 -- The callable entity may be absent, in which case there is nothing
971 -- to do. This happens with non-analyzed calls in nested generics.
978 elsif Nkind
(Nam
) = N_Selected_Component
then
979 Ent
:= Entity
(Selector_Name
(Nam
));
981 elsif not Is_Entity_Name
(Nam
) then
992 -- Nothing to do if this is a recursive call (i.e. a call to
993 -- an entity that is already in the Elab_Call stack)
995 for J
in 1 .. Elab_Visited
.Last
loop
996 if Ent
= Elab_Visited
.Table
(J
) then
1001 -- See if we need to analyze this call. We analyze it if either of
1002 -- the following conditions is met:
1004 -- It is an inner level call (since in this case it was triggered
1005 -- by an outer level call from elaboration code), but only if the
1006 -- call is within the scope of the original outer level call.
1008 -- It is an outer level call from elaboration code, or the called
1009 -- entity is in the same elaboration scope.
1011 -- And in these cases, we will check both inter-unit calls and
1012 -- intra-unit (within a single unit) calls.
1014 C_Scope
:= Current_Scope
;
1016 -- If not outer level call, then we follow it if it is within
1017 -- the original scope of the outer call.
1019 if Present
(Outer_Scope
)
1020 and then Within
(Scope
(Ent
), Outer_Scope
)
1023 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
1025 elsif Elaboration_Checks_Suppressed
(Current_Scope
) then
1028 elsif From_Elab_Code
then
1030 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1032 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1034 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1036 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1037 -- is set, then we will do the check, but only in the inter-unit case
1038 -- (this is to accommodate unguarded elaboration calls from other units
1039 -- in which this same mode is set). We don't want warnings in this case,
1040 -- it would generate warnings having nothing to do with elaboration.
1042 elsif Dynamic_Elaboration_Checks
then
1048 Inter_Unit_Only
=> True,
1049 Generate_Warnings
=> False);
1054 end Check_Elab_Call
;
1056 ----------------------
1057 -- Check_Elab_Calls --
1058 ----------------------
1060 procedure Check_Elab_Calls
is
1062 -- If expansion is disabled, do not generate any checks. Also
1063 -- skip checks if any subunits are missing because in either
1064 -- case we lack the full information that we need, and no object
1065 -- file will be created in any case.
1067 if not Expander_Active
or else Subunits_Missing
then
1071 -- Skip delayed calls if we had any errors
1073 if Errors_Detected
= 0 then
1074 Delaying_Elab_Checks
:= False;
1075 Expander_Mode_Save_And_Set
(True);
1077 for J
in Delay_Check
.First
.. Delay_Check
.Last
loop
1078 New_Scope
(Delay_Check
.Table
(J
).Curscop
);
1079 From_Elab_Code
:= Delay_Check
.Table
(J
).From_Elab_Code
;
1081 Check_Internal_Call_Continue
(
1082 N
=> Delay_Check
.Table
(J
).N
,
1083 E
=> Delay_Check
.Table
(J
).E
,
1084 Outer_Scope
=> Delay_Check
.Table
(J
).Outer_Scope
,
1085 Orig_Ent
=> Delay_Check
.Table
(J
).Orig_Ent
);
1090 -- Set Delaying_Elab_Checks back on for next main compilation
1092 Expander_Mode_Restore
;
1093 Delaying_Elab_Checks
:= True;
1095 end Check_Elab_Calls
;
1097 ------------------------------
1098 -- Check_Elab_Instantiation --
1099 ------------------------------
1101 procedure Check_Elab_Instantiation
1103 Outer_Scope
: Entity_Id
:= Empty
)
1109 -- Check for and deal with bad instantiation case. There is some
1110 -- duplicated code here, but we will worry about this later ???
1112 Check_Bad_Instantiation
(N
);
1114 if ABE_Is_Certain
(N
) then
1118 -- Nothing to do if we do not have an instantiation (happens in some
1119 -- error cases, and also in the formal package declaration case)
1121 if Nkind
(N
) not in N_Generic_Instantiation
then
1125 -- Nothing to do if inside a generic template
1127 if Inside_A_Generic
then
1132 Ent
:= Entity
(Nam
);
1133 From_Elab_Code
:= not In_Subprogram_Or_Concurrent_Unit
;
1135 -- See if we need to analyze this instantiation. We analyze it if
1136 -- either of the following conditions is met:
1138 -- It is an inner level instantiation (since in this case it was
1139 -- triggered by an outer level call from elaboration code), but
1140 -- only if the instantiation is within the scope of the original
1141 -- outer level call.
1143 -- It is an outer level instantiation from elaboration code, or the
1144 -- instantiated entity is in the same elaboratoin scope.
1146 -- And in these cases, we will check both the inter-unit case and
1147 -- the intra-unit (within a single unit) case.
1149 C_Scope
:= Current_Scope
;
1151 if Present
(Outer_Scope
)
1152 and then Within
(Scope
(Ent
), Outer_Scope
)
1155 Check_A_Call
(N
, Ent
, Outer_Scope
, Inter_Unit_Only
=> False);
1157 elsif From_Elab_Code
then
1159 Check_A_Call
(N
, Ent
, Standard_Standard
, Inter_Unit_Only
=> False);
1161 elsif Same_Elaboration_Scope
(C_Scope
, Scope
(Ent
)) then
1163 Check_A_Call
(N
, Ent
, Scope
(Ent
), Inter_Unit_Only
=> False);
1165 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1166 -- is set, then we will do the check, but only in the inter-unit case
1167 -- (this is to accommodate unguarded elaboration calls from other units
1168 -- in which this same mode is set). We inhibit warnings in this case,
1169 -- since this instantiation is not occurring in elaboration code.
1171 elsif Dynamic_Elaboration_Checks
then
1177 Inter_Unit_Only
=> True,
1178 Generate_Warnings
=> False);
1183 end Check_Elab_Instantiation
;
1185 -------------------------
1186 -- Check_Internal_Call --
1187 -------------------------
1189 procedure Check_Internal_Call
1192 Outer_Scope
: Entity_Id
;
1193 Orig_Ent
: Entity_Id
)
1195 Inst_Case
: constant Boolean := Nkind
(N
) in N_Generic_Instantiation
;
1198 -- If not function or procedure call or instantiation, then ignore
1199 -- call (this happens in some error case and rewriting cases)
1201 if Nkind
(N
) /= N_Function_Call
1203 Nkind
(N
) /= N_Procedure_Call_Statement
1209 -- Nothing to do if this is a call or instantiation that has
1210 -- already been found to be a sure ABE
1212 elsif ABE_Is_Certain
(N
) then
1215 -- Nothing to do if errors already detected (avoid cascaded errors)
1217 elsif Errors_Detected
/= 0 then
1220 -- Nothing to do if not in full analysis mode
1222 elsif not Full_Analysis
then
1225 -- Nothing to do if within a default expression, since the call
1226 -- is not actualy being made at this time.
1228 elsif In_Default_Expression
then
1231 -- Nothing to do for call to intrinsic subprogram
1233 elsif Is_Intrinsic_Subprogram
(E
) then
1236 -- No need to trace local calls if checking task activation, because
1237 -- other local bodies are elaborated already.
1239 elsif In_Task_Activation
then
1243 -- Delay this call if we are still delaying calls
1245 if Delaying_Elab_Checks
then
1246 Delay_Check
.Increment_Last
;
1247 Delay_Check
.Table
(Delay_Check
.Last
) :=
1250 Orig_Ent
=> Orig_Ent
,
1251 Curscop
=> Current_Scope
,
1252 Outer_Scope
=> Outer_Scope
,
1253 From_Elab_Code
=> From_Elab_Code
);
1256 -- Otherwise, call phase 2 continuation right now
1259 Check_Internal_Call_Continue
(N
, E
, Outer_Scope
, Orig_Ent
);
1262 end Check_Internal_Call
;
1264 ----------------------------------
1265 -- Check_Internal_Call_Continue --
1266 ----------------------------------
1268 procedure Check_Internal_Call_Continue
1271 Outer_Scope
: Entity_Id
;
1272 Orig_Ent
: Entity_Id
)
1274 Loc
: constant Source_Ptr
:= Sloc
(N
);
1275 Inst_Case
: constant Boolean := Is_Generic_Unit
(E
);
1280 function Process
(N
: Node_Id
) return Traverse_Result
;
1281 -- Function applied to each node as we traverse the body.
1282 -- Checks for call that needs checking, and if so checks
1283 -- it. Always returns OK, so entire tree is traversed.
1285 function Process
(N
: Node_Id
) return Traverse_Result
is
1287 -- If user has specified that there are no entry calls in elaboration
1288 -- code, do not trace past an accept statement, because the rendez-
1289 -- vous will happen after elaboration.
1291 if (Nkind
(Original_Node
(N
)) = N_Accept_Statement
1292 or else Nkind
(Original_Node
(N
)) = N_Selective_Accept
)
1293 and then Restrictions
(No_Entry_Calls_In_Elaboration_Code
)
1297 -- If we have a subprogram call, check it
1299 elsif Nkind
(N
) = N_Function_Call
1300 or else Nkind
(N
) = N_Procedure_Call_Statement
1302 Check_Elab_Call
(N
, Outer_Scope
);
1305 -- If we have a generic instantiation, check it
1307 elsif Nkind
(N
) in N_Generic_Instantiation
then
1308 Check_Elab_Instantiation
(N
, Outer_Scope
);
1311 -- Skip subprogram bodies that come from source (wait for
1312 -- call to analyze these). The reason for the come from
1313 -- source test is to avoid catching task bodies.
1315 -- For task bodies, we should really avoid these too, waiting
1316 -- for the task activation, but that's too much trouble to
1317 -- catch for now, so we go in unconditionally. This is not
1318 -- so terrible, it means the error backtrace is not quite
1319 -- complete, and we are too eager to scan bodies of tasks
1320 -- that are unused, but this is hardly very significant!
1322 elsif Nkind
(N
) = N_Subprogram_Body
1323 and then Comes_From_Source
(N
)
1332 procedure Traverse
is new Atree
.Traverse_Proc
;
1333 -- Traverse procedure using above Process function
1335 -- Start of processing for Check_Internal_Call_Continue
1338 -- Save outer level call if at outer level
1340 if Elab_Call
.Last
= 0 then
1341 Outer_Level_Sloc
:= Loc
;
1344 Elab_Visited
.Increment_Last
;
1345 Elab_Visited
.Table
(Elab_Visited
.Last
) := E
;
1347 -- If the call is to a function that renames a literal, no check
1350 if Ekind
(E
) = E_Enumeration_Literal
then
1354 Sbody
:= Unit_Declaration_Node
(E
);
1356 if Nkind
(Sbody
) /= N_Subprogram_Body
1358 Nkind
(Sbody
) /= N_Package_Body
1360 Ebody
:= Corresponding_Body
(Sbody
);
1365 Sbody
:= Unit_Declaration_Node
(Ebody
);
1369 -- If the body appears after the outer level call or
1370 -- instantiation then we have an error case handled below.
1372 if Earlier_In_Extended_Unit
(Outer_Level_Sloc
, Sloc
(Sbody
))
1373 and then not In_Task_Activation
1377 -- If we have the instantiation case we are done, since we now
1378 -- know that the body of the generic appeared earlier.
1380 elsif Inst_Case
then
1383 -- Otherwise we have a call, so we trace through the called
1384 -- body to see if it has any problems ..
1387 pragma Assert
(Nkind
(Sbody
) = N_Subprogram_Body
);
1389 Elab_Call
.Increment_Last
;
1390 Elab_Call
.Table
(Elab_Call
.Last
).Cloc
:= Loc
;
1391 Elab_Call
.Table
(Elab_Call
.Last
).Ent
:= E
;
1393 if Debug_Flag_LL
then
1394 Write_Str
("Elab_Call.Last = ");
1395 Write_Int
(Int
(Elab_Call
.Last
));
1396 Write_Str
(" Ent = ");
1397 Write_Name
(Chars
(E
));
1399 Write_Location
(Sloc
(N
));
1403 -- Now traverse declarations and statements of subprogram body.
1404 -- Note that we cannot simply Traverse (Sbody), since traverse
1405 -- does not normally visit subprogram bodies.
1408 Decl
: Node_Id
:= First
(Declarations
(Sbody
));
1411 while Present
(Decl
) loop
1417 Traverse
(Handled_Statement_Sequence
(Sbody
));
1419 Elab_Call
.Decrement_Last
;
1423 -- Here is the case of calling a subprogram where the body has
1424 -- not yet been encountered, a warning message is needed.
1426 Warn_On_Instance
:= True;
1428 -- If we have nothing in the call stack, then this is at the
1429 -- outer level, and the ABE is bound to occur.
1431 if Elab_Call
.Last
= 0 then
1435 ("?cannot instantiate& before body seen", N
, Orig_Ent
);
1438 ("?cannot call& before body seen", N
, Orig_Ent
);
1442 ("\?Program_Error will be raised at run time", N
);
1443 Insert_Elab_Check
(N
);
1445 -- Call is not at outer level
1448 -- Deal with dynamic elaboration check
1450 if not Elaboration_Checks_Suppressed
(E
) then
1451 Set_Elaboration_Entity_Required
(E
);
1453 -- Case of no elaboration entity allocated yet
1455 if No
(Elaboration_Entity
(E
)) then
1457 -- Create object declaration for elaboration entity, and put it
1458 -- just in front of the spec of the subprogram or generic unit,
1459 -- in the same scope as this unit.
1462 Loce
: constant Source_Ptr
:= Sloc
(E
);
1463 Ent
: constant Entity_Id
:=
1464 Make_Defining_Identifier
(Loc
,
1465 Chars
=> New_External_Name
(Chars
(E
), 'E'));
1468 Set_Elaboration_Entity
(E
, Ent
);
1469 New_Scope
(Scope
(E
));
1471 Insert_Action
(Declaration_Node
(E
),
1472 Make_Object_Declaration
(Loce
,
1473 Defining_Identifier
=> Ent
,
1474 Object_Definition
=>
1475 New_Occurrence_Of
(Standard_Boolean
, Loce
),
1476 Expression
=> New_Occurrence_Of
(Standard_False
, Loce
)));
1478 -- Set elaboration flag at the point of the body
1480 Set_Elaboration_Flag
(Sbody
, E
);
1486 -- Generate check of the elaboration Boolean
1488 Insert_Elab_Check
(N
,
1489 New_Occurrence_Of
(Elaboration_Entity
(E
), Loc
));
1492 -- Generate the warning
1494 if not Suppress_Elaboration_Warnings
(E
) then
1497 ("instantiation of& may occur before body is seen?",
1501 ("call to& may occur before body is seen?", N
, Orig_Ent
);
1505 ("\Program_Error may be raised at run time?", N
);
1511 Warn_On_Instance
:= False;
1513 -- Set flag to suppress further warnings on same subprogram
1514 -- unless in all errors mode
1516 if not All_Errors_Mode
then
1517 Set_Suppress_Elaboration_Warnings
(E
);
1519 end Check_Internal_Call_Continue
;
1521 ----------------------------
1522 -- Check_Task_Activation --
1523 ----------------------------
1525 procedure Check_Task_Activation
(N
: Node_Id
) is
1526 Loc
: constant Source_Ptr
:= Sloc
(N
);
1529 Task_Scope
: Entity_Id
;
1530 Cunit_SC
: Boolean := False;
1533 Inter_Procs
: Elist_Id
:= New_Elmt_List
;
1534 Intra_Procs
: Elist_Id
:= New_Elmt_List
;
1535 Enclosing
: Entity_Id
;
1537 procedure Add_Task_Proc
(Typ
: Entity_Id
);
1538 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
1539 -- For record types, this procedure recurses over component types.
1541 procedure Collect_Tasks
(Decls
: List_Id
);
1542 -- Collect the types of the tasks that are to be activated in the given
1543 -- list of declarations, in order to perform elaboration checks on the
1544 -- corresponding task procedures which are called implicitly here.
1546 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
;
1547 -- find enclosing compilation unit of Entity, ignoring subunits, or
1548 -- else enclosing subprogram. If E is not a package, there is no need
1549 -- for inter-unit elaboration checks.
1555 procedure Add_Task_Proc
(Typ
: Entity_Id
) is
1557 Proc
: Entity_Id
:= Empty
;
1560 if Is_Task_Type
(Typ
) then
1561 Proc
:= Get_Task_Body_Procedure
(Typ
);
1563 elsif Is_Array_Type
(Typ
)
1564 and then Has_Task
(Base_Type
(Typ
))
1566 Add_Task_Proc
(Component_Type
(Typ
));
1568 elsif Is_Record_Type
(Typ
)
1569 and then Has_Task
(Base_Type
(Typ
))
1571 Comp
:= First_Component
(Typ
);
1573 while Present
(Comp
) loop
1574 Add_Task_Proc
(Etype
(Comp
));
1575 Comp
:= Next_Component
(Comp
);
1579 -- If the task type is another unit, we will perform the usual
1580 -- elaboration check on its enclosing unit. If the type is in the
1581 -- same unit, we can trace the task body as for an internal call,
1582 -- but we only need to examine other external calls, because at
1583 -- the point the task is activated, internal subprogram bodies
1584 -- will have been elaborated already. We keep separate lists for
1585 -- each kind of task.
1587 if Present
(Proc
) then
1588 if Outer_Unit
(Scope
(Proc
)) = Enclosing
then
1590 if No
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
1592 (not Is_Generic_Instance
(Scope
(Proc
))
1594 Scope
(Proc
) = Scope
(Defining_Identifier
(Decl
)))
1597 ("task will be activated before elaboration of its body?",
1600 ("Program_Error will be raised at run-time?", Decl
);
1603 Present
(Corresponding_Body
(Unit_Declaration_Node
(Proc
)))
1605 Append_Elmt
(Proc
, Intra_Procs
);
1609 Elmt
:= First_Elmt
(Inter_Procs
);
1611 -- No need for multiple entries of the same type.
1613 while Present
(Elmt
) loop
1614 if Node
(Elmt
) = Proc
then
1621 Append_Elmt
(Proc
, Inter_Procs
);
1630 procedure Collect_Tasks
(Decls
: List_Id
) is
1632 if Present
(Decls
) then
1633 Decl
:= First
(Decls
);
1635 while Present
(Decl
) loop
1637 if Nkind
(Decl
) = N_Object_Declaration
1638 and then Has_Task
(Etype
(Defining_Identifier
(Decl
)))
1640 Add_Task_Proc
(Etype
(Defining_Identifier
(Decl
)));
1652 function Outer_Unit
(E
: Entity_Id
) return Entity_Id
is
1653 Outer
: Entity_Id
:= E
;
1656 while Present
(Outer
) loop
1657 if Suppress_Elaboration_Checks
(Outer
) then
1661 exit when Is_Child_Unit
(Outer
)
1662 or else Scope
(Outer
) = Standard_Standard
1663 or else Ekind
(Outer
) /= E_Package
;
1664 Outer
:= Scope
(Outer
);
1670 -- Start of processing for Check_Task_Activation
1673 Enclosing
:= Outer_Unit
(Current_Scope
);
1675 -- Find all tasks declared in the current unit.
1677 if Nkind
(N
) = N_Package_Body
then
1678 P
:= Unit_Declaration_Node
(Corresponding_Spec
(N
));
1680 Collect_Tasks
(Declarations
(N
));
1681 Collect_Tasks
(Visible_Declarations
(Specification
(P
)));
1682 Collect_Tasks
(Private_Declarations
(Specification
(P
)));
1684 elsif Nkind
(N
) = N_Package_Declaration
then
1685 Collect_Tasks
(Visible_Declarations
(Specification
(N
)));
1686 Collect_Tasks
(Private_Declarations
(Specification
(N
)));
1689 Collect_Tasks
(Declarations
(N
));
1692 -- We only perform detailed checks in all tasks are library level
1693 -- entities. If the master is a subprogram or task, activation will
1694 -- depend on the activation of the master itself.
1695 -- Should dynamic checks be added in the more general case???
1697 if Ekind
(Enclosing
) /= E_Package
then
1701 -- For task types defined in other units, we want the unit containing
1702 -- the task body to be elaborated before the current one.
1704 Elmt
:= First_Elmt
(Inter_Procs
);
1706 while Present
(Elmt
) loop
1708 Task_Scope
:= Outer_Unit
(Scope
(Ent
));
1710 if not Is_Compilation_Unit
(Task_Scope
) then
1713 elsif Suppress_Elaboration_Warnings
(Task_Scope
) then
1716 elsif Dynamic_Elaboration_Checks
then
1717 if not Elaboration_Checks_Suppressed
(Ent
)
1718 and then not Cunit_SC
1719 and then not Restrictions
(No_Entry_Calls_In_Elaboration_Code
)
1721 -- Runtime elaboration check required. generate check of the
1722 -- elaboration Boolean for the unit containing the entity.
1724 Insert_Elab_Check
(N
,
1725 Make_Attribute_Reference
(Loc
,
1726 Attribute_Name
=> Name_Elaborated
,
1729 (Spec_Entity
(Task_Scope
), Loc
)));
1733 -- Force the binder to elaborate other unit first.
1735 if not Suppress_Elaboration_Warnings
(Ent
)
1736 and then Elab_Warnings
1737 and then not Suppress_Elaboration_Warnings
(Task_Scope
)
1739 Error_Msg_Node_2
:= Task_Scope
;
1740 Error_Msg_NE
("activation of an instance of task type&" &
1741 " requires pragma Elaborate_All on &?", N
, Ent
);
1744 Set_Elaborate_All_Desirable
(Task_Scope
);
1745 Set_Suppress_Elaboration_Warnings
(Task_Scope
);
1751 -- For tasks declared in the current unit, trace other calls within
1752 -- the task procedure bodies, which are available.
1754 In_Task_Activation
:= True;
1755 Elmt
:= First_Elmt
(Intra_Procs
);
1757 while Present
(Elmt
) loop
1759 Check_Internal_Call_Continue
(N
, Ent
, Enclosing
, Ent
);
1763 In_Task_Activation
:= False;
1764 end Check_Task_Activation
;
1766 ----------------------
1767 -- Has_Generic_Body --
1768 ----------------------
1770 function Has_Generic_Body
(N
: Node_Id
) return Boolean is
1771 Ent
: constant Entity_Id
:= Entity
(Name
(N
));
1772 Decl
: constant Node_Id
:= Unit_Declaration_Node
(Ent
);
1775 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
;
1776 -- Determine if the list of nodes headed by N and linked by Next
1777 -- contains a package body for the package spec entity E, and if
1778 -- so return the package body. If not, then returns Empty.
1780 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
;
1781 -- This procedure is called load the unit whose name is given by Nam.
1782 -- This unit is being loaded to see whether it contains an optional
1783 -- generic body. The returned value is the loaded unit, which is
1784 -- always a package body (only package bodies can contain other
1785 -- entities in the sense in which Has_Generic_Body is interested).
1786 -- We only attempt to load bodies if we are generating code. If we
1787 -- are in semantics check only mode, then it would be wrong to load
1788 -- bodies that are not required from a semantic point of view, so
1789 -- in this case we return Empty. The result is that the caller may
1790 -- incorrectly decide that a generic spec does not have a body when
1791 -- in fact it does, but the only harm in this is that some warnings
1792 -- on elaboration problems may be lost in semantic checks only mode,
1793 -- which is not big loss. We also return Empty if we go for a body
1794 -- and it is not there.
1796 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
;
1797 -- PE is the entity for a package spec. This function locates the
1798 -- corresponding package body, returning Empty if none is found.
1799 -- The package body returned is fully parsed but may not yet be
1800 -- analyzed, so only syntactic fields should be referenced.
1806 function Find_Body_In
(E
: Entity_Id
; N
: Node_Id
) return Node_Id
is
1811 while Present
(Nod
) loop
1813 -- If we found the package body we are looking for, return it
1815 if Nkind
(Nod
) = N_Package_Body
1816 and then Chars
(Defining_Unit_Name
(Nod
)) = Chars
(E
)
1820 -- If we found the stub for the body, go after the subunit,
1821 -- loading it if necessary.
1823 elsif Nkind
(Nod
) = N_Package_Body_Stub
1824 and then Chars
(Defining_Identifier
(Nod
)) = Chars
(E
)
1826 if Present
(Library_Unit
(Nod
)) then
1827 return Unit
(Library_Unit
(Nod
));
1830 return Load_Package_Body
(Get_Unit_Name
(Nod
));
1833 -- If neither package body nor stub, keep looking on chain
1843 -----------------------
1844 -- Load_Package_Body --
1845 -----------------------
1847 function Load_Package_Body
(Nam
: Unit_Name_Type
) return Node_Id
is
1848 U
: Unit_Number_Type
;
1851 if Operating_Mode
/= Generate_Code
then
1864 return Unit
(Cunit
(U
));
1867 end Load_Package_Body
;
1869 -------------------------------
1870 -- Locate_Corresponding_Body --
1871 -------------------------------
1873 function Locate_Corresponding_Body
(PE
: Entity_Id
) return Node_Id
is
1874 Spec
: constant Node_Id
:= Declaration_Node
(PE
);
1875 Decl
: constant Node_Id
:= Parent
(Spec
);
1876 Scop
: constant Entity_Id
:= Scope
(PE
);
1880 if Is_Library_Level_Entity
(PE
) then
1882 -- If package is a library unit that requires a body, we have
1883 -- no choice but to go after that body because it might contain
1884 -- an optional body for the original generic package.
1886 if Unit_Requires_Body
(PE
) then
1888 -- Load the body. Note that we are a little careful here to
1889 -- use Spec to get the unit number, rather than PE or Decl,
1890 -- since in the case where the package is itself a library
1891 -- level instantiation, Spec will properly reference the
1892 -- generic template, which is what we really want.
1896 (Get_Body_Name
(Unit_Name
(Get_Source_Unit
(Spec
))));
1898 -- But if the package is a library unit that does NOT require
1899 -- a body, then no body is permitted, so we are sure that there
1900 -- is no body for the original generic package.
1906 -- Otherwise look and see if we are embedded in a further package
1908 elsif Is_Package
(Scop
) then
1910 -- If so, get the body of the enclosing package, and look in
1911 -- its package body for the package body we are looking for.
1913 PBody
:= Locate_Corresponding_Body
(Scop
);
1918 return Find_Body_In
(PE
, First
(Declarations
(PBody
)));
1921 -- If we are not embedded in a further package, then the body
1922 -- must be in the same declarative part as we are.
1925 return Find_Body_In
(PE
, Next
(Decl
));
1927 end Locate_Corresponding_Body
;
1929 -- Start of processing for Has_Generic_Body
1932 if Present
(Corresponding_Body
(Decl
)) then
1935 elsif Unit_Requires_Body
(Ent
) then
1938 -- Compilation units cannot have optional bodies
1940 elsif Is_Compilation_Unit
(Ent
) then
1943 -- Otherwise look at what scope we are in
1946 Scop
:= Scope
(Ent
);
1948 -- Case of entity is in other than a package spec, in this case
1949 -- the body, if present, must be in the same declarative part.
1951 if not Is_Package
(Scop
) then
1956 P
:= Declaration_Node
(Ent
);
1958 -- Declaration node may get us a spec, so if so, go to
1959 -- the parent declaration.
1961 while not Is_List_Member
(P
) loop
1965 return Present
(Find_Body_In
(Ent
, Next
(P
)));
1968 -- If the entity is in a package spec, then we have to locate
1969 -- the corresponding package body, and look there.
1973 PBody
: constant Node_Id
:= Locate_Corresponding_Body
(Scop
);
1981 (Find_Body_In
(Ent
, (First
(Declarations
(PBody
)))));
1986 end Has_Generic_Body
;
1988 -----------------------
1989 -- Insert_Elab_Check --
1990 -----------------------
1992 procedure Insert_Elab_Check
(N
: Node_Id
; C
: Node_Id
:= Empty
) is
1994 Loc
: constant Source_Ptr
:= Sloc
(N
);
1997 -- If expansion is disabled, do not generate any checks. Also
1998 -- skip checks if any subunits are missing because in either
1999 -- case we lack the full information that we need, and no object
2000 -- file will be created in any case.
2002 if not Expander_Active
or else Subunits_Missing
then
2006 -- If we have a generic instantiation, where Instance_Spec is set,
2007 -- then this field points to a generic instance spec that has
2008 -- been inserted before the instantiation node itself, so that
2009 -- is where we want to insert a check.
2011 if Nkind
(N
) in N_Generic_Instantiation
2012 and then Present
(Instance_Spec
(N
))
2014 Nod
:= Instance_Spec
(N
);
2019 -- If we are inserting at the top level, insert in Aux_Decls
2021 if Nkind
(Parent
(Nod
)) = N_Compilation_Unit
then
2023 ADN
: constant Node_Id
:= Aux_Decls_Node
(Parent
(Nod
));
2028 R
:= Make_Raise_Program_Error
(Loc
);
2030 R
:= Make_Raise_Program_Error
(Loc
, Make_Op_Not
(Loc
, C
));
2033 if No
(Declarations
(ADN
)) then
2034 Set_Declarations
(ADN
, New_List
(R
));
2036 Append_To
(Declarations
(ADN
), R
);
2042 -- Otherwise just insert before the node in question. However, if
2043 -- the context of the call has already been analyzed, an insertion
2044 -- will not work if it depends on subsequent expansion (e.g. a call in
2045 -- a branch of a short-circuit). In that case we replace the call with
2046 -- a conditional expression, or with a Raise if it is unconditional.
2047 -- Unfortunately this does not work if the call has a dynamic size,
2048 -- because gigi regards it as a dynamic-sized temporary. If such a call
2049 -- appears in a short-circuit expression, the elaboration check will be
2050 -- missed (rare enough ???).
2053 if Nkind
(N
) = N_Function_Call
2054 and then Analyzed
(Parent
(N
))
2055 and then Size_Known_At_Compile_Time
(Etype
(N
))
2058 Typ
: constant Entity_Id
:= Etype
(N
);
2059 R
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
);
2060 Chk
: constant Boolean := Do_Range_Check
(N
);
2070 Make_Conditional_Expression
(Loc
,
2071 Expressions
=> New_List
(C
, Relocate_Node
(N
), R
)));
2074 Analyze_And_Resolve
(N
, Typ
);
2076 -- If the original call requires a range check, so does the
2077 -- conditional expression.
2080 Enable_Range_Check
(N
);
2082 Set_Do_Range_Check
(N
, False);
2089 Make_Raise_Program_Error
(Loc
));
2092 Make_Raise_Program_Error
(Loc
,
2099 end Insert_Elab_Check
;
2105 procedure Output_Calls
(N
: Node_Id
) is
2108 function Is_Printable_Error_Name
(Nm
: Name_Id
) return Boolean;
2109 -- An internal function, used to determine if a name, Nm, is either
2110 -- a non-internal name, or is an internal name that is printable
2111 -- by the error message circuits (i.e. it has a single upper
2112 -- case letter at the end).
2114 function Is_Printable_Error_Name
(Nm
: Name_Id
) return Boolean is
2116 if not Is_Internal_Name
(Nm
) then
2119 elsif Name_Len
= 1 then
2123 Name_Len
:= Name_Len
- 1;
2124 return not Is_Internal_Name
;
2126 end Is_Printable_Error_Name
;
2128 -- Start of processing for Output_Calls
2131 for J
in reverse 1 .. Elab_Call
.Last
loop
2132 Error_Msg_Sloc
:= Elab_Call
.Table
(J
).Cloc
;
2134 Ent
:= Elab_Call
.Table
(J
).Ent
;
2136 if Is_Generic_Unit
(Ent
) then
2137 Error_Msg_NE
("\?& instantiated #", N
, Ent
);
2139 elsif Chars
(Ent
) = Name_uInit_Proc
then
2140 Error_Msg_N
("\?initialization procedure called #", N
);
2142 elsif Is_Printable_Error_Name
(Chars
(Ent
)) then
2143 Error_Msg_NE
("\?& called #", N
, Ent
);
2146 Error_Msg_N
("\? called #", N
);
2151 ----------------------------
2152 -- Same_Elaboration_Scope --
2153 ----------------------------
2155 function Same_Elaboration_Scope
(Scop1
, Scop2
: Entity_Id
) return Boolean is
2156 S1
: Entity_Id
:= Scop1
;
2157 S2
: Entity_Id
:= Scop2
;
2160 while S1
/= Standard_Standard
2161 and then (Ekind
(S1
) = E_Package
2163 Ekind
(S1
) = E_Block
)
2168 while S2
/= Standard_Standard
2169 and then (Ekind
(S2
) = E_Package
2171 Ekind
(S2
) = E_Protected_Type
2173 Ekind
(S2
) = E_Block
)
2179 end Same_Elaboration_Scope
;
2185 procedure Set_C_Scope
is
2187 while not Is_Compilation_Unit
(C_Scope
) loop
2188 C_Scope
:= Scope
(C_Scope
);
2196 function Spec_Entity
(E
: Entity_Id
) return Entity_Id
is
2200 -- Check for case of body entity
2201 -- Why is the check for E_Void needed???
2203 if Ekind
(E
) = E_Void
2204 or else Ekind
(E
) = E_Subprogram_Body
2205 or else Ekind
(E
) = E_Package_Body
2210 Decl
:= Parent
(Decl
);
2211 exit when Nkind
(Decl
) in N_Proper_Body
;
2214 return Corresponding_Spec
(Decl
);
2225 procedure Supply_Bodies
(N
: Node_Id
) is
2227 if Nkind
(N
) = N_Subprogram_Declaration
then
2229 Ent
: constant Entity_Id
:= Defining_Unit_Name
(Specification
(N
));
2232 Set_Is_Imported
(Ent
);
2233 Set_Convention
(Ent
, Convention_Stubbed
);
2236 elsif Nkind
(N
) = N_Package_Declaration
then
2238 Spec
: constant Node_Id
:= Specification
(N
);
2241 New_Scope
(Defining_Unit_Name
(Spec
));
2242 Supply_Bodies
(Visible_Declarations
(Spec
));
2243 Supply_Bodies
(Private_Declarations
(Spec
));
2249 procedure Supply_Bodies
(L
: List_Id
) is
2255 while Present
(Elmt
) loop
2256 Supply_Bodies
(Elmt
);
2266 function Within
(E1
, E2
: Entity_Id
) return Boolean is
2276 elsif Scop
= Standard_Standard
then
2280 Scop
:= Scope
(Scop
);
2284 raise Program_Error
;