1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Tss; use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Expander; use Expander;
36 with Fname; use Fname;
38 with Lib.Load; use Lib.Load;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
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
122 package Delay_Check is new Table.Table (
123 Table_Component_Type => Delay_Element,
124 Table_Index_Type => Int,
125 Table_Low_Bound => 1,
126 Table_Initial => 1000,
127 Table_Increment => 100,
128 Table_Name => "Delay_Check");
131 -- Top level scope of current scope. We need to compute this only
132 -- once at the outer level, i.e. for a call to Check_Elab_Call from
133 -- outside this unit.
135 Outer_Level_Sloc : Source_Ptr;
136 -- Save Sloc value for outer level call node for comparisons of source
137 -- locations. A body is too late if it appears after the *outer* level
138 -- call, not the particular call that is being analyzed.
140 From_Elab_Code : Boolean;
141 -- This flag shows whether the outer level call currently being examined
142 -- is or is not in elaboration code. We are only interested in calls to
143 -- routines in other units if this flag is True.
145 In_Task_Activation : Boolean := False;
146 -- This flag indicates whether we are performing elaboration checks on
147 -- task procedures, at the point of activation. If true, we do not trace
148 -- internal calls in these procedures, because all local bodies are known
151 Delaying_Elab_Checks : Boolean := True;
152 -- This is set True till the compilation is complete, including the
153 -- insertion of all instance bodies. Then when Check_Elab_Calls is
154 -- called, the delay table is used to make the delayed calls and
155 -- this flag is reset to False, so that the calls are processed
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 -- Note: Outer_Scope in all following specs represents the scope of
162 -- interest of the outer level call. If it is set to Standard_Standard,
163 -- then it means the outer level call was at elaboration level, and that
164 -- thus all calls are of interest. If it was set to some other scope,
165 -- then the original call was an inner call, and we are not interested
166 -- in calls that go outside this scope.
168 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id);
169 -- Analysis of construct N shows that we should set Elaborate_All_Desirable
170 -- for the WITH clause for unit U (which will always be present). A special
171 -- case is when N is a function or procedure instantiation, in which case
172 -- it is sufficient to set Elaborate_Desirable, since in this case there is
173 -- no possibility of transitive elaboration issues.
175 procedure Check_A_Call
178 Outer_Scope : Entity_Id;
179 Inter_Unit_Only : Boolean;
180 Generate_Warnings : Boolean := True);
181 -- This is the internal recursive routine that is called to check for
182 -- a possible elaboration error. The argument N is a subprogram call
183 -- or generic instantiation to be checked, and E is the entity of
184 -- the called subprogram, or instantiated generic unit. The flag
185 -- Outer_Scope is the outer level scope for the original call.
186 -- Inter_Unit_Only is set if the call is only to be checked in the
187 -- case where it is to another unit (and skipped if within a unit).
188 -- Generate_Warnings is set to False to suppress warning messages
189 -- about missing pragma Elaborate_All's. These messages are not
190 -- wanted for inner calls in the dynamic model.
192 procedure Check_Bad_Instantiation (N : Node_Id);
193 -- N is a node for an instantiation (if called with any other node kind,
194 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
195 -- the special case of a generic instantiation of a generic spec in the
196 -- same declarative part as the instantiation where a body is present and
197 -- has not yet been seen. This is an obvious error, but needs to be checked
198 -- specially at the time of the instantiation, since it is a case where we
199 -- cannot insert the body anywhere. If this case is detected, warnings are
200 -- generated, and a raise of Program_Error is inserted. In addition any
201 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
202 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
203 -- flag as an indication that no attempt should be made to insert an
206 procedure Check_Internal_Call
209 Outer_Scope : Entity_Id;
210 Orig_Ent : Entity_Id);
211 -- N is a function call or procedure statement call node and E is
212 -- the entity of the called function, which is within the current
213 -- compilation unit (where subunits count as part of the parent).
214 -- This call checks if this call, or any call within any accessed
215 -- body could cause an ABE, and if so, outputs a warning. Orig_Ent
216 -- differs from E only in the case of renamings, and points to the
217 -- original name of the entity. This is used for error messages.
218 -- Outer_Scope is the outer level scope for the original call.
220 procedure Check_Internal_Call_Continue
223 Outer_Scope : Entity_Id;
224 Orig_Ent : Entity_Id);
225 -- The processing for Check_Internal_Call is divided up into two phases,
226 -- and this represents the second phase. The second phase is delayed if
227 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
228 -- phase makes an entry in the Delay_Check table, which is processed
229 -- when Check_Elab_Calls is called. N, E and Orig_Ent are as for the call
230 -- to Check_Internal_Call. Outer_Scope is the outer level scope for
231 -- the original call.
233 procedure Set_Elaboration_Constraint
237 -- The current unit U may depend semantically on some unit P which is not
238 -- in the current context. If there is an elaboration call that reaches P,
239 -- we need to indicate that P requires an Elaborate_All, but this is not
240 -- effective in U's ali file, if there is no with_clause for P. In this
241 -- case we add the Elaborate_All on the unit Q that directly or indirectly
242 -- makes P available. This can happen in two cases:
244 -- a) Q declares a subtype of a type declared in P, and the call is an
245 -- initialization call for an object of that subtype.
247 -- b) Q declares an object of some tagged type whose root type is
248 -- declared in P, and the initialization call uses object notation on
249 -- that object to reach a primitive operation or a classwide operation
252 -- If P appears in the context of U, the current processing is correct.
253 -- Otherwise we must identify these two cases to retrieve Q and place the
254 -- Elaborate_All_Desirable on it.
256 function Has_Generic_Body (N : Node_Id) return Boolean;
257 -- N is a generic package instantiation node, and this routine determines
258 -- if this package spec does in fact have a generic body. If so, then
259 -- True is returned, otherwise False. Note that this is not at all the
260 -- same as checking if the unit requires a body, since it deals with
261 -- the case of optional bodies accurately (i.e. if a body is optional,
262 -- then it looks to see if a body is actually present). Note: this
263 -- function can only do a fully correct job if in generating code mode
264 -- where all bodies have to be present. If we are operating in semantics
265 -- check only mode, then in some cases of optional bodies, a result of
266 -- False may incorrectly be given. In practice this simply means that
267 -- some cases of warnings for incorrect order of elaboration will only
268 -- be given when generating code, which is not a big problem (and is
269 -- inevitable, given the optional body semantics of Ada).
271 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
272 -- Given code for an elaboration check (or unconditional raise if
273 -- the check is not needed), inserts the code in the appropriate
274 -- place. N is the call or instantiation node for which the check
275 -- code is required. C is the test whose failure triggers the raise.
277 procedure Output_Calls (N : Node_Id);
278 -- Outputs chain of calls stored in the Elab_Call table. The caller
279 -- has already generated the main warning message, so the warnings
280 -- generated are all continuation messages. The argument is the
281 -- call node at which the messages are to be placed.
283 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
284 -- Given two scopes, determine whether they are the same scope from an
285 -- elaboration point of view, i.e. packages and blocks are ignored.
287 procedure Set_C_Scope;
288 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
289 -- to be the enclosing compilation unit of this scope.
291 function Spec_Entity (E : Entity_Id) return Entity_Id;
292 -- Given a compilation unit entity, if it is a spec entity, it is
293 -- returned unchanged. If it is a body entity, then the spec for
294 -- the corresponding spec is returned
296 procedure Supply_Bodies (N : Node_Id);
297 -- Given a node, N, that is either a subprogram declaration or a package
298 -- declaration, this procedure supplies dummy bodies for the subprogram
299 -- or for all subprograms in the package. If the given node is not one
300 -- of these two possibilities, then Supply_Bodies does nothing. The
301 -- dummy body is supplied by setting the subprogram to be Imported with
302 -- convention Stubbed.
304 procedure Supply_Bodies (L : List_Id);
305 -- Calls Supply_Bodies for all elements of the given list L
307 function Within (E1, E2 : Entity_Id) return Boolean;
308 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one
309 -- of its contained scopes, False otherwise.
311 function Within_Elaborate_All (E : Entity_Id) return Boolean;
312 -- Before emitting a warning on a scope E for a missing elaborate_all,
313 -- check whether E may be in the context of a directly visible unit U to
314 -- which the pragma applies. This prevents spurious warnings when the
315 -- called entity is renamed within U.
317 --------------------------------------
318 -- Activate_Elaborate_All_Desirable --
319 --------------------------------------
321 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is
322 UN : constant Unit_Number_Type := Get_Code_Unit (N);
323 CU : constant Node_Id := Cunit (UN);
324 UE : constant Entity_Id := Cunit_Entity (UN);
325 Unm : constant Unit_Name_Type := Unit_Name (UN);
326 CI : constant List_Id := Context_Items (CU);
330 procedure Add_To_Context_And_Mark (Itm : Node_Id);
331 -- This procedure is called when the elaborate indication must be
332 -- applied to a unit not in the context of the referencing unit. The
333 -- unit gets added to the context as an implicit with.
335 function In_Withs_Of (UEs : Entity_Id) return Boolean;
336 -- UEs is the spec entity of a unit. If the unit to be marked is
337 -- in the context item list of this unit spec, then the call returns
338 -- True and Itm is left set to point to the relevant N_With_Clause node.
340 procedure Set_Elab_Flag (Itm : Node_Id);
341 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
343 -----------------------------
344 -- Add_To_Context_And_Mark --
345 -----------------------------
347 procedure Add_To_Context_And_Mark (Itm : Node_Id) is
348 CW : constant Node_Id :=
349 Make_With_Clause (Sloc (Itm),
353 Set_Library_Unit (CW, Library_Unit (Itm));
354 Set_Implicit_With (CW, True);
356 -- Set elaborate all desirable on copy and then append the copy to
357 -- the list of body with's and we are done.
361 end Add_To_Context_And_Mark;
367 function In_Withs_Of (UEs : Entity_Id) return Boolean is
368 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
369 CUs : constant Node_Id := Cunit (UNs);
370 CIs : constant List_Id := Context_Items (CUs);
374 while Present (Itm) loop
375 if Nkind (Itm) = N_With_Clause then
377 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
394 procedure Set_Elab_Flag (Itm : Node_Id) is
396 if Nkind (N) in N_Subprogram_Instantiation then
397 Set_Elaborate_Desirable (Itm);
399 Set_Elaborate_All_Desirable (Itm);
403 -- Start of processing for Activate_Elaborate_All_Desirable
406 -- Do not set binder indication if expansion is disabled, as when
407 -- compiling a generic unit.
409 if not Expander_Active then
414 while Present (Itm) loop
415 if Nkind (Itm) = N_With_Clause then
416 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
418 -- If we find it, then mark elaborate all desirable and return
429 -- If we fall through then the with clause is not present in the
430 -- current unit. One legitimate possibility is that the with clause
431 -- is present in the spec when we are a body.
433 if Is_Body_Name (Unm)
434 and then In_Withs_Of (Spec_Entity (UE))
436 Add_To_Context_And_Mark (Itm);
440 -- Similarly, we may be in the spec or body of a child unit, where
441 -- the unit in question is with'ed by some ancestor of the child unit.
443 if Is_Child_Name (Unm) then
451 exit when Pkg = Standard_Standard;
453 if In_Withs_Of (Pkg) then
454 Add_To_Context_And_Mark (Itm);
461 -- Here if we do not find with clause on spec or body. We just ignore
462 -- this case, it means that the elaboration involves some other unit
463 -- than the unit being compiled, and will be caught elsewhere.
466 end Activate_Elaborate_All_Desirable;
472 procedure Check_A_Call
475 Outer_Scope : Entity_Id;
476 Inter_Unit_Only : Boolean;
477 Generate_Warnings : Boolean := True)
479 Loc : constant Source_Ptr := Sloc (N);
484 -- Top level scope of entity for called subprogram. This
485 -- value includes following renamings and derivations, so
486 -- this scope can be in a non-visible unit. This is the
487 -- scope that is to be investigated to see whether an
488 -- elaboration check is required.
491 -- Top level scope of directly called entity for subprogram. This
492 -- differs from E_Scope in the case where renamings or derivations
493 -- are involved, since it does not follow these links. W_Scope is
494 -- generally in a visible unit, and it is this scope that may require
495 -- an Elaborate_All. However, there are some cases (initialization
496 -- calls and calls involving object notation) where W_Scope might not
497 -- be in the context of the current unit, and there is an intermediate
498 -- package that is, in which case the Elaborate_All has to be placed
499 -- on this intedermediate package. These special cases are handled in
500 -- Set_Elaboration_Constraint.
502 Body_Acts_As_Spec : Boolean;
503 -- Set to true if call is to body acting as spec (no separate spec)
505 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
506 -- Indicates if we have instantiation case
508 Caller_Unit_Internal : Boolean;
509 Callee_Unit_Internal : Boolean;
511 Inst_Caller : Source_Ptr;
512 Inst_Callee : Source_Ptr;
514 Unit_Caller : Unit_Number_Type;
515 Unit_Callee : Unit_Number_Type;
517 Cunit_SC : Boolean := False;
518 -- Set to suppress dynamic elaboration checks where one of the
519 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
520 -- if a pragma Elaborate (_All) applies to that scope, in which case
521 -- warnings on the scope are also suppressed. For the internal case,
522 -- we ignore this flag.
525 -- If the call is known to be within a local Suppress Elaboration
526 -- pragma, nothing to check. This can happen in task bodies.
528 if (Nkind (N) = N_Function_Call
529 or else Nkind (N) = N_Procedure_Call_Statement)
530 and then No_Elaboration_Check (N)
535 -- Go to parent for derived subprogram, or to original subprogram
536 -- in the case of a renaming (Alias covers both these cases)
540 if (Suppress_Elaboration_Warnings (Ent)
541 or else Elaboration_Checks_Suppressed (Ent))
542 and then (Inst_Case or else No (Alias (Ent)))
547 -- Nothing to do for imported entities
549 if Is_Imported (Ent) then
553 exit when Inst_Case or else No (Alias (Ent));
557 Decl := Unit_Declaration_Node (Ent);
559 if Nkind (Decl) = N_Subprogram_Body then
560 Body_Acts_As_Spec := True;
562 elsif Nkind (Decl) = N_Subprogram_Declaration
563 or else Nkind (Decl) = N_Subprogram_Body_Stub
566 Body_Acts_As_Spec := False;
568 -- If we have none of an instantiation, subprogram body or
569 -- subprogram declaration, then it is not a case that we want
570 -- to check. (One case is a call to a generic formal subprogram,
571 -- where we do not want the check in the template).
579 if Elaboration_Checks_Suppressed (E_Scope)
580 or else Suppress_Elaboration_Warnings (E_Scope)
585 -- Exit when we get to compilation unit, not counting subunits
587 exit when Is_Compilation_Unit (E_Scope)
588 and then (Is_Child_Unit (E_Scope)
589 or else Scope (E_Scope) = Standard_Standard);
591 -- If we did not find a compilation unit, other than standard,
592 -- then nothing to check (happens in some instantiation cases)
594 if E_Scope = Standard_Standard then
597 -- Otherwise move up a scope looking for compilation unit
600 E_Scope := Scope (E_Scope);
604 -- No checks needed for pure or preelaborated compilation units
607 or else Is_Preelaborated (E_Scope)
612 -- If the generic entity is within a deeper instance than we are, then
613 -- either the instantiation to which we refer itself caused an ABE, in
614 -- which case that will be handled separately, or else we know that the
615 -- body we need appears as needed at the point of the instantiation.
616 -- However, this assumption is only valid if we are in static mode.
618 if not Dynamic_Elaboration_Checks
619 and then Instantiation_Depth (Sloc (Ent)) >
620 Instantiation_Depth (Sloc (N))
625 -- Do not give a warning for a package with no body
627 if Ekind (Ent) = E_Generic_Package
628 and then not Has_Generic_Body (N)
633 -- Case of entity is not in current unit (i.e. with'ed unit case)
635 if E_Scope /= C_Scope then
637 -- We are only interested in such calls if the outer call was from
638 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
640 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
644 -- Nothing to do if some scope said that no checks were required
650 -- Nothing to do for a generic instance, because in this case
651 -- the checking was at the point of instantiation of the generic
652 -- However, this shortcut is only applicable in static mode.
654 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
658 -- Nothing to do if subprogram with no separate spec. However,
659 -- a call to Deep_Initialize may result in a call to a user-defined
660 -- Initialize procedure, which imposes a body dependency. This
661 -- happens only if the type is controlled and the Initialize
662 -- procedure is not inherited.
664 if Body_Acts_As_Spec then
665 if Is_TSS (Ent, TSS_Deep_Initialize) then
670 Typ := Etype (Next_Formal (First_Formal (Ent)));
672 if not Is_Controlled (Typ) then
675 Init := Find_Prim_Op (Typ, Name_Initialize);
677 if Comes_From_Source (Init) then
690 -- Check cases of internal units
692 Callee_Unit_Internal :=
693 Is_Internal_File_Name
694 (Unit_File_Name (Get_Source_Unit (E_Scope)));
696 -- Do not give a warning if the with'ed unit is internal
697 -- and this is the generic instantiation case (this saves a
698 -- lot of hassle dealing with the Text_IO special child units)
700 if Callee_Unit_Internal and Inst_Case then
704 if C_Scope = Standard_Standard then
705 Caller_Unit_Internal := False;
707 Caller_Unit_Internal :=
708 Is_Internal_File_Name
709 (Unit_File_Name (Get_Source_Unit (C_Scope)));
712 -- Do not give a warning if the with'ed unit is internal
713 -- and the caller is not internal (since the binder always
714 -- elaborates internal units first).
716 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
720 -- For now, if debug flag -gnatdE is not set, do no checking for
721 -- one internal unit withing another. This fixes the problem with
722 -- the sgi build and storage errors. To be resolved later ???
724 if (Callee_Unit_Internal and Caller_Unit_Internal)
725 and then not Debug_Flag_EE
730 if Is_TSS (E, TSS_Deep_Initialize) then
734 -- If the call is in an instance, and the called entity is not
735 -- defined in the same instance, then the elaboration issue
736 -- focuses around the unit containing the template, it is
737 -- this unit which requires an Elaborate_All.
739 -- However, if we are doing dynamic elaboration, we need to
740 -- chase the call in the usual manner.
742 -- We do not handle the case of calling a generic formal correctly
743 -- in the static case. See test 4703-004 to explore this gap ???
745 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
746 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
748 if Inst_Caller = No_Location then
749 Unit_Caller := No_Unit;
751 Unit_Caller := Get_Source_Unit (N);
754 if Inst_Callee = No_Location then
755 Unit_Callee := No_Unit;
757 Unit_Callee := Get_Source_Unit (Ent);
760 if Unit_Caller /= No_Unit
761 and then Unit_Callee /= Unit_Caller
762 and then not Dynamic_Elaboration_Checks
764 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
766 -- If we don't get a spec entity, just ignore call. Not
767 -- quite clear why this check is necessary.
773 -- Otherwise step to enclosing compilation unit
775 while not Is_Compilation_Unit (E_Scope) loop
776 E_Scope := Scope (E_Scope);
779 -- For the case N is not an instance, or a call within instance
780 -- We recompute E_Scope for the error message, since we
781 -- do NOT want to go to the unit which has the ultimate
782 -- declaration in the case of renaming and derivation and
783 -- we also want to go to the generic unit in the case of
784 -- an instance, and no further.
787 -- Loop to carefully follow renamings and derivations
788 -- one step outside the current unit, but not further.
791 and then Present (Alias (Ent))
793 E_Scope := Alias (Ent);
799 while not Is_Compilation_Unit (E_Scope) loop
800 E_Scope := Scope (E_Scope);
803 -- If E_Scope is the same as C_Scope, it means that there
804 -- definitely was a local renaming or derivation, and we
805 -- are not yet out of the current unit.
807 exit when E_Scope /= C_Scope;
811 -- If no alias, there is a previous error
819 if Within_Elaborate_All (E_Scope) then
823 -- Find top level scope for called entity (not following renamings
824 -- or derivations). This is where the Elaborate_All will go if it
825 -- is needed. We start with the called entity, except in the case
826 -- of an initialization procedure outside the current package, where
827 -- the init proc is in the root package, and we start from the entity
828 -- of the name in the call.
830 if Is_Entity_Name (Name (N))
831 and then Is_Init_Proc (Entity (Name (N)))
832 and then not In_Same_Extended_Unit (N, Entity (Name (N)))
834 W_Scope := Scope (Entity (Name (N)));
839 while not Is_Compilation_Unit (W_Scope) loop
840 W_Scope := Scope (W_Scope);
843 -- Now check if an elaborate_all (or dynamic check) is needed
845 if not Suppress_Elaboration_Warnings (Ent)
846 and then not Elaboration_Checks_Suppressed (Ent)
847 and then not Suppress_Elaboration_Warnings (E_Scope)
848 and then not Elaboration_Checks_Suppressed (E_Scope)
849 and then Elab_Warnings
850 and then Generate_Warnings
854 ("instantiation of& may raise Program_Error?", N, Ent);
857 if Is_Init_Proc (Entity (Name (N)))
858 and then Comes_From_Source (Ent)
861 ("implicit call to & may raise Program_Error?", N, Ent);
865 ("call to & may raise Program_Error?", N, Ent);
869 Error_Msg_Qual_Level := Nat'Last;
871 if Nkind (N) in N_Subprogram_Instantiation then
873 ("\missing pragma Elaborate for&?", N, W_Scope);
876 ("\missing pragma Elaborate_All for&?", N, W_Scope);
879 Error_Msg_Qual_Level := 0;
882 -- Set flag to prevent further warnings for same unit
883 -- unless in All_Errors_Mode.
885 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
886 Set_Suppress_Elaboration_Warnings (W_Scope, True);
890 -- Check for runtime elaboration check required
892 if Dynamic_Elaboration_Checks then
893 if not Elaboration_Checks_Suppressed (Ent)
894 and then not Elaboration_Checks_Suppressed (W_Scope)
895 and then not Elaboration_Checks_Suppressed (E_Scope)
896 and then not Cunit_SC
898 -- Runtime elaboration check required. Generate check of the
899 -- elaboration Boolean for the unit containing the entity.
901 -- Note that for this case, we do check the real unit (the
902 -- one from following renamings, since that is the issue!)
904 -- Could this possibly miss a useless but required PE???
906 Insert_Elab_Check (N,
907 Make_Attribute_Reference (Loc,
908 Attribute_Name => Name_Elaborated,
911 (Spec_Entity (E_Scope), Loc)));
914 -- Case of static elaboration model
917 -- Do not do anything if elaboration checks suppressed. Note
918 -- that we check Ent here, not E, since we want the real entity
919 -- for the body to see if checks are suppressed for it, not the
920 -- dummy entry for renamings or derivations.
922 if Elaboration_Checks_Suppressed (Ent)
923 or else Elaboration_Checks_Suppressed (E_Scope)
924 or else Elaboration_Checks_Suppressed (W_Scope)
928 -- Here we need to generate an implicit elaborate all
931 -- Generate elaborate_all warning unless suppressed
933 if (Elab_Warnings and Generate_Warnings and not Inst_Case)
934 and then not Suppress_Elaboration_Warnings (Ent)
935 and then not Suppress_Elaboration_Warnings (E_Scope)
936 and then not Suppress_Elaboration_Warnings (W_Scope)
938 Error_Msg_Node_2 := W_Scope;
940 ("call to& in elaboration code " &
941 "requires pragma Elaborate_All on&?", N, E);
944 -- Set indication for binder to generate Elaborate_All
946 Set_Elaboration_Constraint (N, E, W_Scope);
950 -- Case of entity is in same unit as call or instantiation
952 elsif not Inter_Unit_Only then
953 Check_Internal_Call (N, Ent, Outer_Scope, E);
957 -----------------------------
958 -- Check_Bad_Instantiation --
959 -----------------------------
961 procedure Check_Bad_Instantiation (N : Node_Id) is
965 -- Nothing to do if we do not have an instantiation (happens in some
966 -- error cases, and also in the formal package declaration case)
968 if Nkind (N) not in N_Generic_Instantiation then
971 -- Nothing to do if serious errors detected (avoid cascaded errors)
973 elsif Serious_Errors_Detected /= 0 then
976 -- Nothing to do if not in full analysis mode
978 elsif not Full_Analysis then
981 -- Nothing to do if inside a generic template
983 elsif Inside_A_Generic then
986 -- Nothing to do if a library level instantiation
988 elsif Nkind (Parent (N)) = N_Compilation_Unit then
991 -- Nothing to do if we are compiling a proper body for semantic
992 -- purposes only. The generic body may be in another proper body.
995 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
1000 Ent := Get_Generic_Entity (N);
1002 -- The case we are interested in is when the generic spec is in the
1003 -- current declarative part
1005 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
1006 or else not In_Same_Extended_Unit (N, Ent)
1011 -- If the generic entity is within a deeper instance than we are, then
1012 -- either the instantiation to which we refer itself caused an ABE, in
1013 -- which case that will be handled separately. Otherwise, we know that
1014 -- the body we need appears as needed at the point of the instantiation.
1015 -- If they are both at the same level but not within the same instance
1016 -- then the body of the generic will be in the earlier instance.
1019 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
1020 D2 : constant Int := Instantiation_Depth (Sloc (N));
1027 and then Is_Generic_Instance (Scope (Ent))
1028 and then not In_Open_Scopes (Scope (Ent))
1034 -- Now we can proceed, if the entity being called has a completion,
1035 -- then we are definitely OK, since we have already seen the body.
1037 if Has_Completion (Ent) then
1041 -- If there is no body, then nothing to do
1043 if not Has_Generic_Body (N) then
1047 -- Here we definitely have a bad instantiation
1050 ("?cannot instantiate& before body seen", N, Ent);
1052 if Present (Instance_Spec (N)) then
1053 Supply_Bodies (Instance_Spec (N));
1057 ("\?Program_Error will be raised at run time", N);
1058 Insert_Elab_Check (N);
1059 Set_ABE_Is_Certain (N);
1060 end Check_Bad_Instantiation;
1062 ---------------------
1063 -- Check_Elab_Call --
1064 ---------------------
1066 procedure Check_Elab_Call
1068 Outer_Scope : Entity_Id := Empty)
1073 function Get_Called_Ent return Entity_Id;
1074 -- Retrieve called entity. If this is a call to a protected subprogram,
1075 -- entity is a selected component. The callable entity may be absent,
1076 -- in which case there is no check to perform. This happens with
1077 -- non-analyzed calls in nested generics.
1079 --------------------
1080 -- Get_Called_Ent --
1081 --------------------
1083 function Get_Called_Ent return Entity_Id is
1092 elsif Nkind (Nam) = N_Selected_Component then
1093 return Entity (Selector_Name (Nam));
1095 elsif not Is_Entity_Name (Nam) then
1099 return Entity (Nam);
1103 -- Start of processing for Check_Elab_Call
1106 -- If the call does not come from the main unit, there is nothing to
1107 -- check. Elaboration call from units in the context of the main unit
1108 -- will lead to semantic dependencies when those units are compiled.
1110 if not In_Extended_Main_Code_Unit (N) then
1114 -- For an entry call, check relevant restriction
1116 if Nkind (N) = N_Entry_Call_Statement
1117 and then not In_Subprogram_Or_Concurrent_Unit
1119 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1121 -- Nothing to do if this is not a call (happens in some error
1122 -- conditions, and in some cases where rewriting occurs).
1124 elsif Nkind (N) /= N_Function_Call
1125 and then Nkind (N) /= N_Procedure_Call_Statement
1129 -- Nothing to do if this is a call already rewritten for elab checking
1131 elsif Nkind (Parent (N)) = N_Conditional_Expression then
1134 -- Nothing to do if inside a generic template
1136 elsif Inside_A_Generic
1137 and then No (Enclosing_Generic_Body (N))
1142 -- Here we have a call at elaboration time which must be checked
1144 if Debug_Flag_LL then
1145 Write_Str (" Check_Elab_Call: ");
1148 or else not Is_Entity_Name (Name (N))
1150 Write_Str ("<<not entity name>> ");
1152 Write_Name (Chars (Entity (Name (N))));
1155 Write_Str (" call at ");
1156 Write_Location (Sloc (N));
1160 -- Climb up the tree to make sure we are not inside default expression
1161 -- of a parameter specification or a record component, since in both
1162 -- these cases, we will be doing the actual call later, not now, and it
1163 -- is at the time of the actual call (statically speaking) that we must
1164 -- do our static check, not at the time of its initial analysis).
1166 -- However, we have to check calls within component definitions (e.g., a
1167 -- function call that determines an array component bound), so we
1168 -- terminate the loop in that case.
1171 while Present (P) loop
1172 if Nkind (P) = N_Parameter_Specification
1174 Nkind (P) = N_Component_Declaration
1178 -- The call occurs within the constraint of a component,
1179 -- so it must be checked.
1181 elsif Nkind (P) = N_Component_Definition then
1189 -- Stuff that happens only at the outer level
1191 if No (Outer_Scope) then
1192 Elab_Visited.Set_Last (0);
1194 -- Nothing to do if current scope is Standard (this is a bit
1195 -- odd, but it happens in the case of generic instantiations).
1197 C_Scope := Current_Scope;
1199 if C_Scope = Standard_Standard then
1203 -- First case, we are in elaboration code
1205 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1206 if From_Elab_Code then
1208 -- Complain if call that comes from source in preelaborated
1209 -- unit and we are not inside a subprogram (i.e. we are in
1212 if Comes_From_Source (N)
1213 and then In_Preelaborated_Unit
1214 and then not In_Inlined_Body
1216 -- This is a warning in GNAT mode allowing such calls to be
1217 -- used in the predefined library with appropriate care.
1219 Error_Msg_Warn := GNAT_Mode;
1221 ("<non-static call not allowed in preelaborated unit", N);
1225 -- Second case, we are inside a subprogram or concurrent unit, which
1226 -- means we are not in elaboration code.
1229 -- In this case, the issue is whether we are inside the
1230 -- declarative part of the unit in which we live, or inside its
1231 -- statements. In the latter case, there is no issue of ABE calls
1232 -- at this level (a call from outside to the unit in which we live
1233 -- might cause an ABE, but that will be detected when we analyze
1234 -- that outer level call, as it recurses into the called unit).
1236 -- Climb up the tree, doing this test, and also testing for being
1237 -- inside a default expression, which, as discussed above, is not
1238 -- checked at this stage.
1247 -- If we find a parentless subtree, it seems safe to assume
1248 -- that we are not in a declarative part and that no
1249 -- checking is required.
1255 if Is_List_Member (P) then
1256 L := List_Containing (P);
1263 exit when Nkind (P) = N_Subunit;
1265 -- Filter out case of default expressions, where we do not
1266 -- do the check at this stage.
1268 if Nkind (P) = N_Parameter_Specification
1270 Nkind (P) = N_Component_Declaration
1275 -- A protected body has no elaboration code and contains
1276 -- only other bodies.
1278 if Nkind (P) = N_Protected_Body then
1281 elsif Nkind (P) = N_Subprogram_Body
1283 Nkind (P) = N_Task_Body
1285 Nkind (P) = N_Block_Statement
1287 Nkind (P) = N_Entry_Body
1289 if L = Declarations (P) then
1292 -- We are not in elaboration code, but we are doing
1293 -- dynamic elaboration checks, in this case, we still
1294 -- need to do the call, since the subprogram we are in
1295 -- could be called from another unit, also in dynamic
1296 -- elaboration check mode, at elaboration time.
1298 elsif Dynamic_Elaboration_Checks then
1300 -- This is a rather new check, going into version
1301 -- 3.14a1 for the first time (V1.80 of this unit), so
1302 -- we provide a debug flag to enable it. That way we
1303 -- have an easy work around for regressions that are
1304 -- caused by this new check. This debug flag can be
1307 if Debug_Flag_DD then
1311 -- Do the check in this case
1315 elsif Nkind (P) = N_Task_Body then
1317 -- The check is deferred until Check_Task_Activation
1318 -- but we need to capture local suppress pragmas
1319 -- that may inhibit checks on this call.
1321 Ent := Get_Called_Ent;
1326 elsif Elaboration_Checks_Suppressed (Current_Scope)
1327 or else Elaboration_Checks_Suppressed (Ent)
1328 or else Elaboration_Checks_Suppressed (Scope (Ent))
1330 Set_No_Elaboration_Check (N);
1335 -- Static model, call is not in elaboration code, we
1336 -- never need to worry, because in the static model the
1337 -- top level caller always takes care of things.
1348 Ent := Get_Called_Ent;
1354 -- Nothing to do if this is a recursive call (i.e. a call to
1355 -- an entity that is already in the Elab_Call stack)
1357 for J in 1 .. Elab_Visited.Last loop
1358 if Ent = Elab_Visited.Table (J) then
1363 -- See if we need to analyze this call. We analyze it if either of
1364 -- the following conditions is met:
1366 -- It is an inner level call (since in this case it was triggered
1367 -- by an outer level call from elaboration code), but only if the
1368 -- call is within the scope of the original outer level call.
1370 -- It is an outer level call from elaboration code, or the called
1371 -- entity is in the same elaboration scope.
1373 -- And in these cases, we will check both inter-unit calls and
1374 -- intra-unit (within a single unit) calls.
1376 C_Scope := Current_Scope;
1378 -- If not outer level call, then we follow it if it is within
1379 -- the original scope of the outer call.
1381 if Present (Outer_Scope)
1382 and then Within (Scope (Ent), Outer_Scope)
1385 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1387 elsif Elaboration_Checks_Suppressed (Current_Scope) then
1390 elsif From_Elab_Code then
1392 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1394 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1396 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1398 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1399 -- is set, then we will do the check, but only in the inter-unit case
1400 -- (this is to accommodate unguarded elaboration calls from other units
1401 -- in which this same mode is set). We don't want warnings in this case,
1402 -- it would generate warnings having nothing to do with elaboration.
1404 elsif Dynamic_Elaboration_Checks then
1410 Inter_Unit_Only => True,
1411 Generate_Warnings => False);
1413 -- Otherwise nothing to do
1419 -- A call to an Init_Proc in elaboration code may bring additional
1420 -- dependencies, if some of the record components thereof have
1421 -- initializations that are function calls that come from source.
1422 -- We treat the current node as a call to each of these functions,
1423 -- to check their elaboration impact.
1425 if Is_Init_Proc (Ent)
1426 and then From_Elab_Code
1428 Process_Init_Proc : declare
1429 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1431 function Find_Init_Call (Nod : Node_Id) return Traverse_Result;
1432 -- Find subprogram calls within body of Init_Proc for Traverse
1433 -- instantiation below.
1435 procedure Traverse_Body is new Traverse_Proc (Find_Init_Call);
1436 -- Traversal procedure to find all calls with body of Init_Proc
1438 --------------------
1439 -- Find_Init_Call --
1440 --------------------
1442 function Find_Init_Call (Nod : Node_Id) return Traverse_Result is
1446 if (Nkind (Nod) = N_Function_Call
1447 or else Nkind (Nod) = N_Procedure_Call_Statement)
1448 and then Is_Entity_Name (Name (Nod))
1450 Func := Entity (Name (Nod));
1452 if Comes_From_Source (Func) then
1454 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1464 -- Start of processing for Process_Init_Proc
1467 if Nkind (Unit_Decl) = N_Subprogram_Body then
1468 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1470 end Process_Init_Proc;
1472 end Check_Elab_Call;
1474 -----------------------
1475 -- Check_Elab_Assign --
1476 -----------------------
1478 procedure Check_Elab_Assign (N : Node_Id) is
1482 Pkg_Spec : Entity_Id;
1483 Pkg_Body : Entity_Id;
1486 -- For record or array component, check prefix. If it is an access
1487 -- type, then there is nothing to do (we do not know what is being
1488 -- assigned), but otherwise this is an assignment to the prefix.
1490 if Nkind (N) = N_Indexed_Component
1492 Nkind (N) = N_Selected_Component
1496 if not Is_Access_Type (Etype (Prefix (N))) then
1497 Check_Elab_Assign (Prefix (N));
1503 -- For type conversion, check expression
1505 if Nkind (N) = N_Type_Conversion then
1506 Check_Elab_Assign (Expression (N));
1510 -- Nothing to do if this is not an entity reference otherwise get entity
1512 if Is_Entity_Name (N) then
1518 -- What we are looking for is a reference in the body of a package that
1519 -- modifies a variable declared in the visible part of the package spec.
1522 and then Comes_From_Source (N)
1523 and then not Suppress_Elaboration_Warnings (Ent)
1524 and then Ekind (Ent) = E_Variable
1525 and then not In_Private_Part (Ent)
1526 and then Is_Library_Level_Entity (Ent)
1528 Scop := Current_Scope;
1530 if No (Scop) or else Scop = Standard_Standard then
1532 elsif Ekind (Scop) = E_Package
1533 and then Is_Compilation_Unit (Scop)
1537 Scop := Scope (Scop);
1541 -- Here Scop points to the containing library package
1544 Pkg_Body := Body_Entity (Pkg_Spec);
1546 -- All OK if the package has an Elaborate_Body pragma
1548 if Has_Pragma_Elaborate_Body (Scop) then
1552 -- OK if entity being modified is not in containing package spec
1554 if not In_Same_Source_Unit (Scop, Ent) then
1558 -- All OK if entity appears in generic package or generic instance.
1559 -- We just get too messed up trying to give proper warnings in the
1560 -- presence of generics. Better no message than a junk one.
1562 Scop := Scope (Ent);
1563 while Present (Scop) and then Scop /= Pkg_Spec loop
1564 if Ekind (Scop) = E_Generic_Package then
1566 elsif Ekind (Scop) = E_Package
1567 and then Is_Generic_Instance (Scop)
1572 Scop := Scope (Scop);
1575 -- All OK if in task, don't issue warnings there
1577 if In_Task_Activation then
1581 -- OK if no package body
1583 if No (Pkg_Body) then
1587 -- OK if reference is not in package body
1589 if not In_Same_Source_Unit (Pkg_Body, N) then
1593 -- OK if package body has no handled statement sequence
1596 HSS : constant Node_Id :=
1597 Handled_Statement_Sequence (Declaration_Node (Pkg_Body));
1599 if No (HSS) or else not Comes_From_Source (HSS) then
1604 -- We definitely have a case of a modification of an entity in
1605 -- the package spec from the elaboration code of the package body.
1606 -- We may not give the warning (because there are some additional
1607 -- checks to avoid too many false positives), but it would be a good
1608 -- idea for the binder to try to keep the body elaboration close to
1609 -- the spec elaboration.
1611 Set_Elaborate_Body_Desirable (Pkg_Spec);
1613 -- All OK in gnat mode (we know what we are doing)
1619 -- All OK if warnings suppressed on the entity
1621 if Warnings_Off (Ent) then
1625 -- All OK if all warnings suppressed
1627 if Warning_Mode = Suppress then
1631 -- All OK if elaboration checks suppressed for entity
1633 if Checks_May_Be_Suppressed (Ent)
1634 and then Is_Check_Suppressed (Ent, Elaboration_Check)
1639 -- OK if the entity is initialized. Note that the No_Initialization
1640 -- flag usually means that the initialization has been rewritten into
1641 -- assignments, but that still counts for us.
1644 Decl : constant Node_Id := Declaration_Node (Ent);
1646 if Nkind (Decl) = N_Object_Declaration
1647 and then (Present (Expression (Decl))
1648 or else No_Initialization (Decl))
1654 -- Here is where we give the warning
1656 Error_Msg_Sloc := Sloc (Ent);
1659 ("?elaboration code may access& before it is initialized",
1662 ("\?suggest adding pragma Elaborate_Body to spec of &",
1665 ("\?or an explicit initialization could be added #", N);
1667 if not All_Errors_Mode then
1668 Set_Suppress_Elaboration_Warnings (Ent);
1671 end Check_Elab_Assign;
1673 ----------------------
1674 -- Check_Elab_Calls --
1675 ----------------------
1677 procedure Check_Elab_Calls is
1679 -- If expansion is disabled, do not generate any checks. Also
1680 -- skip checks if any subunits are missing because in either
1681 -- case we lack the full information that we need, and no object
1682 -- file will be created in any case.
1684 if not Expander_Active
1685 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1686 or else Subunits_Missing
1691 -- Skip delayed calls if we had any errors
1693 if Serious_Errors_Detected = 0 then
1694 Delaying_Elab_Checks := False;
1695 Expander_Mode_Save_And_Set (True);
1697 for J in Delay_Check.First .. Delay_Check.Last loop
1698 New_Scope (Delay_Check.Table (J).Curscop);
1699 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1701 Check_Internal_Call_Continue (
1702 N => Delay_Check.Table (J).N,
1703 E => Delay_Check.Table (J).E,
1704 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1705 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1710 -- Set Delaying_Elab_Checks back on for next main compilation
1712 Expander_Mode_Restore;
1713 Delaying_Elab_Checks := True;
1715 end Check_Elab_Calls;
1717 ------------------------------
1718 -- Check_Elab_Instantiation --
1719 ------------------------------
1721 procedure Check_Elab_Instantiation
1723 Outer_Scope : Entity_Id := Empty)
1728 -- Check for and deal with bad instantiation case. There is some
1729 -- duplicated code here, but we will worry about this later ???
1731 Check_Bad_Instantiation (N);
1733 if ABE_Is_Certain (N) then
1737 -- Nothing to do if we do not have an instantiation (happens in some
1738 -- error cases, and also in the formal package declaration case)
1740 if Nkind (N) not in N_Generic_Instantiation then
1744 -- Nothing to do if inside a generic template
1746 if Inside_A_Generic then
1750 -- Nothing to do if the instantiation is not in the main unit
1752 if not In_Extended_Main_Code_Unit (N) then
1756 Ent := Get_Generic_Entity (N);
1757 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1759 -- See if we need to analyze this instantiation. We analyze it if
1760 -- either of the following conditions is met:
1762 -- It is an inner level instantiation (since in this case it was
1763 -- triggered by an outer level call from elaboration code), but
1764 -- only if the instantiation is within the scope of the original
1765 -- outer level call.
1767 -- It is an outer level instantiation from elaboration code, or the
1768 -- instantiated entity is in the same elaboratoin scope.
1770 -- And in these cases, we will check both the inter-unit case and
1771 -- the intra-unit (within a single unit) case.
1773 C_Scope := Current_Scope;
1775 if Present (Outer_Scope)
1776 and then Within (Scope (Ent), Outer_Scope)
1779 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1781 elsif From_Elab_Code then
1783 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1785 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1787 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1789 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1790 -- is set, then we will do the check, but only in the inter-unit case
1791 -- (this is to accommodate unguarded elaboration calls from other units
1792 -- in which this same mode is set). We inhibit warnings in this case,
1793 -- since this instantiation is not occurring in elaboration code.
1795 elsif Dynamic_Elaboration_Checks then
1801 Inter_Unit_Only => True,
1802 Generate_Warnings => False);
1807 end Check_Elab_Instantiation;
1809 -------------------------
1810 -- Check_Internal_Call --
1811 -------------------------
1813 procedure Check_Internal_Call
1816 Outer_Scope : Entity_Id;
1817 Orig_Ent : Entity_Id)
1819 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1822 -- If not function or procedure call or instantiation, then ignore
1823 -- call (this happens in some error case and rewriting cases)
1825 if Nkind (N) /= N_Function_Call
1827 Nkind (N) /= N_Procedure_Call_Statement
1833 -- Nothing to do if this is a call or instantiation that has
1834 -- already been found to be a sure ABE
1836 elsif ABE_Is_Certain (N) then
1839 -- Nothing to do if errors already detected (avoid cascaded errors)
1841 elsif Serious_Errors_Detected /= 0 then
1844 -- Nothing to do if not in full analysis mode
1846 elsif not Full_Analysis then
1849 -- Nothing to do if within a default expression, since the call
1850 -- is not actualy being made at this time.
1852 elsif In_Default_Expression then
1855 -- Nothing to do for call to intrinsic subprogram
1857 elsif Is_Intrinsic_Subprogram (E) then
1860 -- No need to trace local calls if checking task activation, because
1861 -- other local bodies are elaborated already.
1863 elsif In_Task_Activation then
1867 -- Delay this call if we are still delaying calls
1869 if Delaying_Elab_Checks then
1870 Delay_Check.Increment_Last;
1871 Delay_Check.Table (Delay_Check.Last) :=
1874 Orig_Ent => Orig_Ent,
1875 Curscop => Current_Scope,
1876 Outer_Scope => Outer_Scope,
1877 From_Elab_Code => From_Elab_Code);
1880 -- Otherwise, call phase 2 continuation right now
1883 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1885 end Check_Internal_Call;
1887 ----------------------------------
1888 -- Check_Internal_Call_Continue --
1889 ----------------------------------
1891 procedure Check_Internal_Call_Continue
1894 Outer_Scope : Entity_Id;
1895 Orig_Ent : Entity_Id)
1897 Loc : constant Source_Ptr := Sloc (N);
1898 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1903 function Find_Elab_Reference (N : Node_Id) return Traverse_Result;
1904 -- Function applied to each node as we traverse the body. Checks for
1905 -- call or entity reference that needs checking, and if so checks it.
1906 -- Always returns OK, so entire tree is traversed, except that as
1907 -- described below subprogram bodies are skipped for now.
1909 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference);
1910 -- Traverse procedure using above Find_Elab_Reference function
1912 -------------------------
1913 -- Find_Elab_Reference --
1914 -------------------------
1916 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is
1920 -- If user has specified that there are no entry calls in elaboration
1921 -- code, do not trace past an accept statement, because the rendez-
1922 -- vous will happen after elaboration.
1924 if (Nkind (Original_Node (N)) = N_Accept_Statement
1925 or else Nkind (Original_Node (N)) = N_Selective_Accept)
1926 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1930 -- If we have a function call, check it
1932 elsif Nkind (N) = N_Function_Call then
1933 Check_Elab_Call (N, Outer_Scope);
1936 -- If we have a procedure call, check the call, and also check
1937 -- arguments that are assignments (OUT or IN OUT mode formals).
1939 elsif Nkind (N) = N_Procedure_Call_Statement then
1940 Check_Elab_Call (N, Outer_Scope);
1942 Actual := First_Actual (N);
1943 while Present (Actual) loop
1944 if Known_To_Be_Assigned (Actual) then
1945 Check_Elab_Assign (Actual);
1948 Next_Actual (Actual);
1953 -- If we have a generic instantiation, check it
1955 elsif Nkind (N) in N_Generic_Instantiation then
1956 Check_Elab_Instantiation (N, Outer_Scope);
1959 -- Skip subprogram bodies that come from source (wait for
1960 -- call to analyze these). The reason for the come from
1961 -- source test is to avoid catching task bodies.
1963 -- For task bodies, we should really avoid these too, waiting
1964 -- for the task activation, but that's too much trouble to
1965 -- catch for now, so we go in unconditionally. This is not
1966 -- so terrible, it means the error backtrace is not quite
1967 -- complete, and we are too eager to scan bodies of tasks
1968 -- that are unused, but this is hardly very significant!
1970 elsif Nkind (N) = N_Subprogram_Body
1971 and then Comes_From_Source (N)
1975 elsif Nkind (N) = N_Assignment_Statement
1976 and then Comes_From_Source (N)
1978 Check_Elab_Assign (Name (N));
1984 end Find_Elab_Reference;
1986 -- Start of processing for Check_Internal_Call_Continue
1989 -- Save outer level call if at outer level
1991 if Elab_Call.Last = 0 then
1992 Outer_Level_Sloc := Loc;
1995 Elab_Visited.Increment_Last;
1996 Elab_Visited.Table (Elab_Visited.Last) := E;
1998 -- If the call is to a function that renames a literal, no check
2001 if Ekind (E) = E_Enumeration_Literal then
2005 Sbody := Unit_Declaration_Node (E);
2007 if Nkind (Sbody) /= N_Subprogram_Body
2009 Nkind (Sbody) /= N_Package_Body
2011 Ebody := Corresponding_Body (Sbody);
2016 Sbody := Unit_Declaration_Node (Ebody);
2020 -- If the body appears after the outer level call or
2021 -- instantiation then we have an error case handled below.
2023 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
2024 and then not In_Task_Activation
2028 -- If we have the instantiation case we are done, since we now
2029 -- know that the body of the generic appeared earlier.
2031 elsif Inst_Case then
2034 -- Otherwise we have a call, so we trace through the called
2035 -- body to see if it has any problems ..
2038 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
2040 Elab_Call.Increment_Last;
2041 Elab_Call.Table (Elab_Call.Last).Cloc := Loc;
2042 Elab_Call.Table (Elab_Call.Last).Ent := E;
2044 if Debug_Flag_LL then
2045 Write_Str ("Elab_Call.Last = ");
2046 Write_Int (Int (Elab_Call.Last));
2047 Write_Str (" Ent = ");
2048 Write_Name (Chars (E));
2050 Write_Location (Sloc (N));
2054 -- Now traverse declarations and statements of subprogram body.
2055 -- Note that we cannot simply Traverse (Sbody), since traverse
2056 -- does not normally visit subprogram bodies.
2061 Decl := First (Declarations (Sbody));
2062 while Present (Decl) loop
2068 Traverse (Handled_Statement_Sequence (Sbody));
2070 Elab_Call.Decrement_Last;
2074 -- Here is the case of calling a subprogram where the body has
2075 -- not yet been encountered, a warning message is needed.
2077 -- If we have nothing in the call stack, then this is at the
2078 -- outer level, and the ABE is bound to occur.
2080 if Elab_Call.Last = 0 then
2083 ("?cannot instantiate& before body seen", N, Orig_Ent);
2086 ("?cannot call& before body seen", N, Orig_Ent);
2090 ("\?Program_Error will be raised at run time", N);
2091 Insert_Elab_Check (N);
2093 -- Call is not at outer level
2096 -- Deal with dynamic elaboration check
2098 if not Elaboration_Checks_Suppressed (E) then
2099 Set_Elaboration_Entity_Required (E);
2101 -- Case of no elaboration entity allocated yet
2103 if No (Elaboration_Entity (E)) then
2105 -- Create object declaration for elaboration entity, and put it
2106 -- just in front of the spec of the subprogram or generic unit,
2107 -- in the same scope as this unit.
2110 Loce : constant Source_Ptr := Sloc (E);
2111 Ent : constant Entity_Id :=
2112 Make_Defining_Identifier (Loc,
2113 Chars => New_External_Name (Chars (E), 'E'));
2116 Set_Elaboration_Entity (E, Ent);
2117 New_Scope (Scope (E));
2119 Insert_Action (Declaration_Node (E),
2120 Make_Object_Declaration (Loce,
2121 Defining_Identifier => Ent,
2122 Object_Definition =>
2123 New_Occurrence_Of (Standard_Boolean, Loce),
2124 Expression => New_Occurrence_Of (Standard_False, Loce)));
2126 -- Set elaboration flag at the point of the body
2128 Set_Elaboration_Flag (Sbody, E);
2130 -- Kill current value indication. This is necessary because
2131 -- the tests of this flag are inserted out of sequence and
2132 -- must not pick up bogus indications of the wrong constant
2133 -- value. Also, this is never a true constant, since one way
2134 -- or another, it gets reset.
2136 Set_Current_Value (Ent, Empty);
2137 Set_Last_Assignment (Ent, Empty);
2138 Set_Is_True_Constant (Ent, False);
2143 -- Generate check of the elaboration Boolean
2145 Insert_Elab_Check (N,
2146 New_Occurrence_Of (Elaboration_Entity (E), Loc));
2149 -- Generate the warning
2151 if not Suppress_Elaboration_Warnings (E)
2152 and then not Elaboration_Checks_Suppressed (E)
2156 ("instantiation of& may occur before body is seen?",
2160 ("call to& may occur before body is seen?", N, Orig_Ent);
2164 ("\Program_Error may be raised at run time?", N);
2170 -- Set flag to suppress further warnings on same subprogram
2171 -- unless in all errors mode
2173 if not All_Errors_Mode then
2174 Set_Suppress_Elaboration_Warnings (E);
2176 end Check_Internal_Call_Continue;
2178 ---------------------------
2179 -- Check_Task_Activation --
2180 ---------------------------
2182 procedure Check_Task_Activation (N : Node_Id) is
2183 Loc : constant Source_Ptr := Sloc (N);
2184 Inter_Procs : constant Elist_Id := New_Elmt_List;
2185 Intra_Procs : constant Elist_Id := New_Elmt_List;
2188 Task_Scope : Entity_Id;
2189 Cunit_SC : Boolean := False;
2192 Enclosing : Entity_Id;
2194 procedure Add_Task_Proc (Typ : Entity_Id);
2195 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
2196 -- For record types, this procedure recurses over component types.
2198 procedure Collect_Tasks (Decls : List_Id);
2199 -- Collect the types of the tasks that are to be activated in the given
2200 -- list of declarations, in order to perform elaboration checks on the
2201 -- corresponding task procedures which are called implicitly here.
2203 function Outer_Unit (E : Entity_Id) return Entity_Id;
2204 -- find enclosing compilation unit of Entity, ignoring subunits, or
2205 -- else enclosing subprogram. If E is not a package, there is no need
2206 -- for inter-unit elaboration checks.
2212 procedure Add_Task_Proc (Typ : Entity_Id) is
2214 Proc : Entity_Id := Empty;
2217 if Is_Task_Type (Typ) then
2218 Proc := Get_Task_Body_Procedure (Typ);
2220 elsif Is_Array_Type (Typ)
2221 and then Has_Task (Base_Type (Typ))
2223 Add_Task_Proc (Component_Type (Typ));
2225 elsif Is_Record_Type (Typ)
2226 and then Has_Task (Base_Type (Typ))
2228 Comp := First_Component (Typ);
2229 while Present (Comp) loop
2230 Add_Task_Proc (Etype (Comp));
2231 Comp := Next_Component (Comp);
2235 -- If the task type is another unit, we will perform the usual
2236 -- elaboration check on its enclosing unit. If the type is in the
2237 -- same unit, we can trace the task body as for an internal call,
2238 -- but we only need to examine other external calls, because at
2239 -- the point the task is activated, internal subprogram bodies
2240 -- will have been elaborated already. We keep separate lists for
2241 -- each kind of task.
2243 -- Skip this test if errors have occurred, since in this case
2244 -- we can get false indications.
2246 if Serious_Errors_Detected /= 0 then
2250 if Present (Proc) then
2251 if Outer_Unit (Scope (Proc)) = Enclosing then
2253 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
2255 (not Is_Generic_Instance (Scope (Proc))
2257 Scope (Proc) = Scope (Defining_Identifier (Decl)))
2260 ("task will be activated before elaboration of its body?",
2263 ("\Program_Error will be raised at run-time?", Decl);
2266 Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
2268 Append_Elmt (Proc, Intra_Procs);
2272 -- No need for multiple entries of the same type
2274 Elmt := First_Elmt (Inter_Procs);
2275 while Present (Elmt) loop
2276 if Node (Elmt) = Proc then
2283 Append_Elmt (Proc, Inter_Procs);
2292 procedure Collect_Tasks (Decls : List_Id) is
2294 if Present (Decls) then
2295 Decl := First (Decls);
2296 while Present (Decl) loop
2297 if Nkind (Decl) = N_Object_Declaration
2298 and then Has_Task (Etype (Defining_Identifier (Decl)))
2300 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2312 function Outer_Unit (E : Entity_Id) return Entity_Id is
2317 while Present (Outer) loop
2318 if Elaboration_Checks_Suppressed (Outer) then
2322 exit when Is_Child_Unit (Outer)
2323 or else Scope (Outer) = Standard_Standard
2324 or else Ekind (Outer) /= E_Package;
2325 Outer := Scope (Outer);
2331 -- Start of processing for Check_Task_Activation
2334 Enclosing := Outer_Unit (Current_Scope);
2336 -- Find all tasks declared in the current unit
2338 if Nkind (N) = N_Package_Body then
2339 P := Unit_Declaration_Node (Corresponding_Spec (N));
2341 Collect_Tasks (Declarations (N));
2342 Collect_Tasks (Visible_Declarations (Specification (P)));
2343 Collect_Tasks (Private_Declarations (Specification (P)));
2345 elsif Nkind (N) = N_Package_Declaration then
2346 Collect_Tasks (Visible_Declarations (Specification (N)));
2347 Collect_Tasks (Private_Declarations (Specification (N)));
2350 Collect_Tasks (Declarations (N));
2353 -- We only perform detailed checks in all tasks are library level
2354 -- entities. If the master is a subprogram or task, activation will
2355 -- depend on the activation of the master itself.
2357 -- Should dynamic checks be added in the more general case???
2359 if Ekind (Enclosing) /= E_Package then
2363 -- For task types defined in other units, we want the unit containing
2364 -- the task body to be elaborated before the current one.
2366 Elmt := First_Elmt (Inter_Procs);
2367 while Present (Elmt) loop
2369 Task_Scope := Outer_Unit (Scope (Ent));
2371 if not Is_Compilation_Unit (Task_Scope) then
2374 elsif Suppress_Elaboration_Warnings (Task_Scope)
2375 or else Elaboration_Checks_Suppressed (Task_Scope)
2379 elsif Dynamic_Elaboration_Checks then
2380 if not Elaboration_Checks_Suppressed (Ent)
2381 and then not Cunit_SC
2383 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2385 -- Runtime elaboration check required. generate check of the
2386 -- elaboration Boolean for the unit containing the entity.
2388 Insert_Elab_Check (N,
2389 Make_Attribute_Reference (Loc,
2390 Attribute_Name => Name_Elaborated,
2393 (Spec_Entity (Task_Scope), Loc)));
2397 -- Force the binder to elaborate other unit first
2399 if not Suppress_Elaboration_Warnings (Ent)
2400 and then not Elaboration_Checks_Suppressed (Ent)
2401 and then Elab_Warnings
2402 and then not Suppress_Elaboration_Warnings (Task_Scope)
2403 and then not Elaboration_Checks_Suppressed (Task_Scope)
2405 Error_Msg_Node_2 := Task_Scope;
2406 Error_Msg_NE ("activation of an instance of task type&" &
2407 " requires pragma Elaborate_All on &?", N, Ent);
2410 Activate_Elaborate_All_Desirable (N, Task_Scope);
2411 Set_Suppress_Elaboration_Warnings (Task_Scope);
2417 -- For tasks declared in the current unit, trace other calls within
2418 -- the task procedure bodies, which are available.
2420 In_Task_Activation := True;
2422 Elmt := First_Elmt (Intra_Procs);
2423 while Present (Elmt) loop
2425 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2429 In_Task_Activation := False;
2430 end Check_Task_Activation;
2432 --------------------------------
2433 -- Set_Elaboration_Constraint --
2434 --------------------------------
2436 procedure Set_Elaboration_Constraint
2441 Elab_Unit : Entity_Id;
2442 Init_Call : constant Boolean :=
2443 Chars (Subp) = Name_Initialize
2444 and then Comes_From_Source (Subp)
2445 and then Present (Parameter_Associations (Call))
2446 and then Is_Controlled (Etype (First_Actual (Call)));
2448 -- If the unit is mentioned in a with_clause of the current
2449 -- unit, it is visible, and we can set the elaboration flag.
2451 if Is_Immediately_Visible (Scop)
2452 or else (Is_Child_Unit (Scop) and then Is_Visible_Child_Unit (Scop))
2454 Activate_Elaborate_All_Desirable (Call, Scop);
2455 Set_Suppress_Elaboration_Warnings (Scop, True);
2459 -- If this is not an initialization call or a call using object notation
2460 -- we know that the unit of the called entity is in the context, and
2461 -- we can set the flag as well. The unit need not be visible if the call
2462 -- occurs within an instantiation.
2464 if Is_Init_Proc (Subp)
2466 or else Nkind (Original_Node (Call)) = N_Selected_Component
2468 null; -- detailed processing follows.
2471 Activate_Elaborate_All_Desirable (Call, Scop);
2472 Set_Suppress_Elaboration_Warnings (Scop, True);
2476 -- If the unit is not in the context, there must be an intermediate
2477 -- unit that is, on which we need to place to elaboration flag. This
2478 -- happens with init proc calls.
2480 if Is_Init_Proc (Subp)
2483 -- The initialization call is on an object whose type is not declared
2484 -- in the same scope as the subprogram. The type of the object must
2485 -- be a subtype of the type of operation. This object is the first
2486 -- actual in the call.
2489 Typ : constant Entity_Id :=
2490 Etype (First (Parameter_Associations (Call)));
2492 Elab_Unit := Scope (Typ);
2493 while (Present (Elab_Unit))
2494 and then not Is_Compilation_Unit (Elab_Unit)
2496 Elab_Unit := Scope (Elab_Unit);
2500 -- If original node uses selected component notation, the prefix is
2501 -- visible and determines the scope that must be elaborated. After
2502 -- rewriting, the prefix is the first actual in the call.
2504 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2505 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2507 -- Not one of special cases above
2510 -- Using previously computed scope. If the elaboration check is
2511 -- done after analysis, the scope is not visible any longer, but
2512 -- must still be in the context.
2517 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2518 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2519 end Set_Elaboration_Constraint;
2521 ----------------------
2522 -- Has_Generic_Body --
2523 ----------------------
2525 function Has_Generic_Body (N : Node_Id) return Boolean is
2526 Ent : constant Entity_Id := Get_Generic_Entity (N);
2527 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2530 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2531 -- Determine if the list of nodes headed by N and linked by Next
2532 -- contains a package body for the package spec entity E, and if
2533 -- so return the package body. If not, then returns Empty.
2535 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2536 -- This procedure is called load the unit whose name is given by Nam.
2537 -- This unit is being loaded to see whether it contains an optional
2538 -- generic body. The returned value is the loaded unit, which is
2539 -- always a package body (only package bodies can contain other
2540 -- entities in the sense in which Has_Generic_Body is interested).
2541 -- We only attempt to load bodies if we are generating code. If we
2542 -- are in semantics check only mode, then it would be wrong to load
2543 -- bodies that are not required from a semantic point of view, so
2544 -- in this case we return Empty. The result is that the caller may
2545 -- incorrectly decide that a generic spec does not have a body when
2546 -- in fact it does, but the only harm in this is that some warnings
2547 -- on elaboration problems may be lost in semantic checks only mode,
2548 -- which is not big loss. We also return Empty if we go for a body
2549 -- and it is not there.
2551 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2552 -- PE is the entity for a package spec. This function locates the
2553 -- corresponding package body, returning Empty if none is found.
2554 -- The package body returned is fully parsed but may not yet be
2555 -- analyzed, so only syntactic fields should be referenced.
2561 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2566 while Present (Nod) loop
2568 -- If we found the package body we are looking for, return it
2570 if Nkind (Nod) = N_Package_Body
2571 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2575 -- If we found the stub for the body, go after the subunit,
2576 -- loading it if necessary.
2578 elsif Nkind (Nod) = N_Package_Body_Stub
2579 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2581 if Present (Library_Unit (Nod)) then
2582 return Unit (Library_Unit (Nod));
2585 return Load_Package_Body (Get_Unit_Name (Nod));
2588 -- If neither package body nor stub, keep looking on chain
2598 -----------------------
2599 -- Load_Package_Body --
2600 -----------------------
2602 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2603 U : Unit_Number_Type;
2606 if Operating_Mode /= Generate_Code then
2619 return Unit (Cunit (U));
2622 end Load_Package_Body;
2624 -------------------------------
2625 -- Locate_Corresponding_Body --
2626 -------------------------------
2628 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2629 Spec : constant Node_Id := Declaration_Node (PE);
2630 Decl : constant Node_Id := Parent (Spec);
2631 Scop : constant Entity_Id := Scope (PE);
2635 if Is_Library_Level_Entity (PE) then
2637 -- If package is a library unit that requires a body, we have
2638 -- no choice but to go after that body because it might contain
2639 -- an optional body for the original generic package.
2641 if Unit_Requires_Body (PE) then
2643 -- Load the body. Note that we are a little careful here to
2644 -- use Spec to get the unit number, rather than PE or Decl,
2645 -- since in the case where the package is itself a library
2646 -- level instantiation, Spec will properly reference the
2647 -- generic template, which is what we really want.
2651 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2653 -- But if the package is a library unit that does NOT require
2654 -- a body, then no body is permitted, so we are sure that there
2655 -- is no body for the original generic package.
2661 -- Otherwise look and see if we are embedded in a further package
2663 elsif Is_Package_Or_Generic_Package (Scop) then
2665 -- If so, get the body of the enclosing package, and look in
2666 -- its package body for the package body we are looking for.
2668 PBody := Locate_Corresponding_Body (Scop);
2673 return Find_Body_In (PE, First (Declarations (PBody)));
2676 -- If we are not embedded in a further package, then the body
2677 -- must be in the same declarative part as we are.
2680 return Find_Body_In (PE, Next (Decl));
2682 end Locate_Corresponding_Body;
2684 -- Start of processing for Has_Generic_Body
2687 if Present (Corresponding_Body (Decl)) then
2690 elsif Unit_Requires_Body (Ent) then
2693 -- Compilation units cannot have optional bodies
2695 elsif Is_Compilation_Unit (Ent) then
2698 -- Otherwise look at what scope we are in
2701 Scop := Scope (Ent);
2703 -- Case of entity is in other than a package spec, in this case
2704 -- the body, if present, must be in the same declarative part.
2706 if not Is_Package_Or_Generic_Package (Scop) then
2711 -- Declaration node may get us a spec, so if so, go to
2712 -- the parent declaration.
2714 P := Declaration_Node (Ent);
2715 while not Is_List_Member (P) loop
2719 return Present (Find_Body_In (Ent, Next (P)));
2722 -- If the entity is in a package spec, then we have to locate
2723 -- the corresponding package body, and look there.
2727 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2735 (Find_Body_In (Ent, (First (Declarations (PBody)))));
2740 end Has_Generic_Body;
2742 -----------------------
2743 -- Insert_Elab_Check --
2744 -----------------------
2746 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2748 Loc : constant Source_Ptr := Sloc (N);
2751 -- If expansion is disabled, do not generate any checks. Also
2752 -- skip checks if any subunits are missing because in either
2753 -- case we lack the full information that we need, and no object
2754 -- file will be created in any case.
2756 if not Expander_Active or else Subunits_Missing then
2760 -- If we have a generic instantiation, where Instance_Spec is set,
2761 -- then this field points to a generic instance spec that has
2762 -- been inserted before the instantiation node itself, so that
2763 -- is where we want to insert a check.
2765 if Nkind (N) in N_Generic_Instantiation
2766 and then Present (Instance_Spec (N))
2768 Nod := Instance_Spec (N);
2773 -- If we are inserting at the top level, insert in Aux_Decls
2775 if Nkind (Parent (Nod)) = N_Compilation_Unit then
2777 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2783 Make_Raise_Program_Error (Loc,
2784 Reason => PE_Access_Before_Elaboration);
2787 Make_Raise_Program_Error (Loc,
2788 Condition => Make_Op_Not (Loc, C),
2789 Reason => PE_Access_Before_Elaboration);
2792 if No (Declarations (ADN)) then
2793 Set_Declarations (ADN, New_List (R));
2795 Append_To (Declarations (ADN), R);
2801 -- Otherwise just insert before the node in question. However, if
2802 -- the context of the call has already been analyzed, an insertion
2803 -- will not work if it depends on subsequent expansion (e.g. a call in
2804 -- a branch of a short-circuit). In that case we replace the call with
2805 -- a conditional expression, or with a Raise if it is unconditional.
2806 -- Unfortunately this does not work if the call has a dynamic size,
2807 -- because gigi regards it as a dynamic-sized temporary. If such a call
2808 -- appears in a short-circuit expression, the elaboration check will be
2809 -- missed (rare enough ???). Otherwise, the code below inserts the check
2810 -- at the appropriate place before the call. Same applies in the even
2811 -- rarer case the return type has a known size but is unconstrained.
2814 if Nkind (N) = N_Function_Call
2815 and then Analyzed (Parent (N))
2816 and then Size_Known_At_Compile_Time (Etype (N))
2818 (not Has_Discriminants (Etype (N))
2819 or else Is_Constrained (Etype (N)))
2823 Typ : constant Entity_Id := Etype (N);
2824 Chk : constant Boolean := Do_Range_Check (N);
2826 R : constant Node_Id :=
2827 Make_Raise_Program_Error (Loc,
2828 Reason => PE_Access_Before_Elaboration);
2838 Make_Conditional_Expression (Loc,
2839 Expressions => New_List (C, Relocate_Node (N), R)));
2842 Analyze_And_Resolve (N, Typ);
2844 -- If the original call requires a range check, so does the
2845 -- conditional expression.
2848 Enable_Range_Check (N);
2850 Set_Do_Range_Check (N, False);
2857 Make_Raise_Program_Error (Loc,
2858 Reason => PE_Access_Before_Elaboration));
2861 Make_Raise_Program_Error (Loc,
2865 Reason => PE_Access_Before_Elaboration));
2869 end Insert_Elab_Check;
2875 procedure Output_Calls (N : Node_Id) is
2878 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
2879 -- An internal function, used to determine if a name, Nm, is either
2880 -- a non-internal name, or is an internal name that is printable
2881 -- by the error message circuits (i.e. it has a single upper
2882 -- case letter at the end).
2884 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
2886 if not Is_Internal_Name (Nm) then
2889 elsif Name_Len = 1 then
2893 Name_Len := Name_Len - 1;
2894 return not Is_Internal_Name;
2896 end Is_Printable_Error_Name;
2898 -- Start of processing for Output_Calls
2901 for J in reverse 1 .. Elab_Call.Last loop
2902 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
2904 Ent := Elab_Call.Table (J).Ent;
2906 if Is_Generic_Unit (Ent) then
2907 Error_Msg_NE ("\?& instantiated #", N, Ent);
2909 elsif Is_Init_Proc (Ent) then
2910 Error_Msg_N ("\?initialization procedure called #", N);
2912 elsif Is_Printable_Error_Name (Chars (Ent)) then
2913 Error_Msg_NE ("\?& called #", N, Ent);
2916 Error_Msg_N ("\? called #", N);
2921 ----------------------------
2922 -- Same_Elaboration_Scope --
2923 ----------------------------
2925 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
2930 -- Find elaboration scope for Scop1
2931 -- This is either a subprogram or a compilation unit.
2934 while S1 /= Standard_Standard
2935 and then not Is_Compilation_Unit (S1)
2936 and then (Ekind (S1) = E_Package
2938 Ekind (S1) = E_Protected_Type
2940 Ekind (S1) = E_Block)
2945 -- Find elaboration scope for Scop2
2948 while S2 /= Standard_Standard
2949 and then not Is_Compilation_Unit (S2)
2950 and then (Ekind (S2) = E_Package
2952 Ekind (S2) = E_Protected_Type
2954 Ekind (S2) = E_Block)
2960 end Same_Elaboration_Scope;
2966 procedure Set_C_Scope is
2968 while not Is_Compilation_Unit (C_Scope) loop
2969 C_Scope := Scope (C_Scope);
2977 function Spec_Entity (E : Entity_Id) return Entity_Id is
2981 -- Check for case of body entity
2982 -- Why is the check for E_Void needed???
2984 if Ekind (E) = E_Void
2985 or else Ekind (E) = E_Subprogram_Body
2986 or else Ekind (E) = E_Package_Body
2991 Decl := Parent (Decl);
2992 exit when Nkind (Decl) in N_Proper_Body;
2995 return Corresponding_Spec (Decl);
3006 procedure Supply_Bodies (N : Node_Id) is
3008 if Nkind (N) = N_Subprogram_Declaration then
3010 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
3012 Set_Is_Imported (Ent);
3013 Set_Convention (Ent, Convention_Stubbed);
3016 elsif Nkind (N) = N_Package_Declaration then
3018 Spec : constant Node_Id := Specification (N);
3020 New_Scope (Defining_Unit_Name (Spec));
3021 Supply_Bodies (Visible_Declarations (Spec));
3022 Supply_Bodies (Private_Declarations (Spec));
3028 procedure Supply_Bodies (L : List_Id) is
3033 while Present (Elmt) loop
3034 Supply_Bodies (Elmt);
3044 function Within (E1, E2 : Entity_Id) return Boolean is
3053 elsif Scop = Standard_Standard then
3057 Scop := Scope (Scop);
3061 raise Program_Error;
3064 --------------------------
3065 -- Within_Elaborate_All --
3066 --------------------------
3068 function Within_Elaborate_All (E : Entity_Id) return Boolean is
3071 Elab_Id : Entity_Id;
3075 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
3076 while Present (Item) loop
3077 if Nkind (Item) = N_Pragma
3078 and then Get_Pragma_Id (Chars (Item)) = Pragma_Elaborate_All
3080 -- Return if some previous error on the pragma itself
3082 if Error_Posted (Item) then
3088 (Expression (First (Pragma_Argument_Associations (Item))));
3090 Par := Parent (Unit_Declaration_Node (Elab_Id));
3092 Item2 := First (Context_Items (Par));
3093 while Present (Item2) loop
3094 if Nkind (Item2) = N_With_Clause
3095 and then Entity (Name (Item2)) = E
3108 end Within_Elaborate_All;