1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1997-2004 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
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 False 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.
282 function Within_Elaborate_All (E : Entity_Id) return Boolean;
283 -- Before emitting a warning on a scope E for a missing elaborate_all,
284 -- check whether E may be in the context of a directly visible unit
285 -- U to which the pragma applies. This prevents spurious warnings when
286 -- the called entity is renamed within U.
292 procedure Check_A_Call
295 Outer_Scope : Entity_Id;
296 Inter_Unit_Only : Boolean;
297 Generate_Warnings : Boolean := True)
299 Loc : constant Source_Ptr := Sloc (N);
304 -- Top level scope of entity for called subprogram. This
305 -- value includes following renamings and derivations, so
306 -- this scope can be in a non-visible unit. This is the
307 -- scope that is to be investigated to see whether an
308 -- elaboration check is required.
311 -- Top level scope of directly called entity for subprogram.
312 -- This differs from E_Scope in the case where renamings or
313 -- derivations are involved, since it does not follow these
314 -- links, thus W_Scope is always in a visible unit. This is
315 -- the scope for the Elaborate_All if one is needed.
317 Body_Acts_As_Spec : Boolean;
318 -- Set to true if call is to body acting as spec (no separate spec)
320 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
321 -- Indicates if we have instantiation case
323 Caller_Unit_Internal : Boolean;
324 Callee_Unit_Internal : Boolean;
326 Inst_Caller : Source_Ptr;
327 Inst_Callee : Source_Ptr;
329 Unit_Caller : Unit_Number_Type;
330 Unit_Callee : Unit_Number_Type;
332 Cunit_SC : Boolean := False;
333 -- Set to suppress dynamic elaboration checks where one of the
334 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
335 -- if a pragma Elaborate (_All) applies to that scope, in which case
336 -- warnings on the scope are also suppressed. For the internal case,
337 -- we ignore this flag.
340 -- If the call is known to be within a local Suppress Elaboration
341 -- pragma, nothing to check. This can happen in task bodies.
343 if (Nkind (N) = N_Function_Call
344 or else Nkind (N) = N_Procedure_Call_Statement)
345 and then No_Elaboration_Check (N)
350 -- Go to parent for derived subprogram, or to original subprogram
351 -- in the case of a renaming (Alias covers both these cases)
355 if (Suppress_Elaboration_Warnings (Ent)
356 or else Elaboration_Checks_Suppressed (Ent))
357 and then (Inst_Case or else No (Alias (Ent)))
362 -- Nothing to do for imported entities
364 if Is_Imported (Ent) then
368 exit when Inst_Case or else No (Alias (Ent));
372 Decl := Unit_Declaration_Node (Ent);
374 if Nkind (Decl) = N_Subprogram_Body then
375 Body_Acts_As_Spec := True;
377 elsif Nkind (Decl) = N_Subprogram_Declaration
378 or else Nkind (Decl) = N_Subprogram_Body_Stub
381 Body_Acts_As_Spec := False;
383 -- If we have none of an instantiation, subprogram body or
384 -- subprogram declaration, then it is not a case that we want
385 -- to check. (One case is a call to a generic formal subprogram,
386 -- where we do not want the check in the template).
394 if Elaboration_Checks_Suppressed (E_Scope)
395 or else Suppress_Elaboration_Warnings (E_Scope)
400 -- Exit when we get to compilation unit, not counting subunits
402 exit when Is_Compilation_Unit (E_Scope)
403 and then (Is_Child_Unit (E_Scope)
404 or else Scope (E_Scope) = Standard_Standard);
406 -- If we did not find a compilation unit, other than standard,
407 -- then nothing to check (happens in some instantiation cases)
409 if E_Scope = Standard_Standard then
412 -- Otherwise move up a scope looking for compilation unit
415 E_Scope := Scope (E_Scope);
419 -- No checks needed for pure or preelaborated compilation units
422 or else Is_Preelaborated (E_Scope)
427 -- If the generic entity is within a deeper instance than we are, then
428 -- either the instantiation to which we refer itself caused an ABE, in
429 -- which case that will be handled separately, or else we know that the
430 -- body we need appears as needed at the point of the instantiation.
431 -- However, this assumption is only valid if we are in static mode.
433 if not Dynamic_Elaboration_Checks
434 and then Instantiation_Depth (Sloc (Ent)) >
435 Instantiation_Depth (Sloc (N))
440 -- Do not give a warning for a package with no body
442 if Ekind (Ent) = E_Generic_Package
443 and then not Has_Generic_Body (N)
448 -- Case of entity is not in current unit (i.e. with'ed unit case)
450 if E_Scope /= C_Scope then
452 -- We are only interested in such calls if the outer call was from
453 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
455 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
459 -- Nothing to do if some scope said that no checks were required
465 -- Nothing to do for a generic instance, because in this case
466 -- the checking was at the point of instantiation of the generic
467 -- However, this shortcut is only applicable in static mode.
469 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
473 -- Nothing to do if subprogram with no separate spec. However,
474 -- a call to Deep_Initialize may result in a call to a user-defined
475 -- Initialize procedure, which imposes a body dependency. This
476 -- happens only if the type is controlled and the Initialize
477 -- procedure is not inherited.
479 if Body_Acts_As_Spec then
480 if Is_TSS (Ent, TSS_Deep_Initialize) then
485 Typ := Etype (Next_Formal (First_Formal (Ent)));
487 if not Is_Controlled (Typ) then
490 Init := Find_Prim_Op (Typ, Name_Initialize);
492 if Comes_From_Source (Init) then
505 -- Check cases of internal units
507 Callee_Unit_Internal :=
508 Is_Internal_File_Name
509 (Unit_File_Name (Get_Source_Unit (E_Scope)));
511 -- Do not give a warning if the with'ed unit is internal
512 -- and this is the generic instantiation case (this saves a
513 -- lot of hassle dealing with the Text_IO special child units)
515 if Callee_Unit_Internal and Inst_Case then
519 if C_Scope = Standard_Standard then
520 Caller_Unit_Internal := False;
522 Caller_Unit_Internal :=
523 Is_Internal_File_Name
524 (Unit_File_Name (Get_Source_Unit (C_Scope)));
527 -- Do not give a warning if the with'ed unit is internal
528 -- and the caller is not internal (since the binder always
529 -- elaborates internal units first).
531 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
535 -- For now, if debug flag -gnatdE is not set, do no checking for
536 -- one internal unit withing another. This fixes the problem with
537 -- the sgi build and storage errors. To be resolved later ???
539 if (Callee_Unit_Internal and Caller_Unit_Internal)
540 and then not Debug_Flag_EE
545 if Is_TSS (E, TSS_Deep_Initialize) then
549 -- If the call is in an instance, and the called entity is not
550 -- defined in the same instance, then the elaboration issue
551 -- focuses around the unit containing the template, it is
552 -- this unit which requires an Elaborate_All.
554 -- However, if we are doing dynamic elaboration, we need to
555 -- chase the call in the usual manner.
557 -- We do not handle the case of calling a generic formal correctly
558 -- in the static case. See test 4703-004 to explore this gap ???
560 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
561 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
563 if Inst_Caller = No_Location then
564 Unit_Caller := No_Unit;
566 Unit_Caller := Get_Source_Unit (N);
569 if Inst_Callee = No_Location then
570 Unit_Callee := No_Unit;
572 Unit_Callee := Get_Source_Unit (Ent);
575 if Unit_Caller /= No_Unit
576 and then Unit_Callee /= Unit_Caller
577 and then not Dynamic_Elaboration_Checks
579 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
581 -- If we don't get a spec entity, just ignore call. Not
582 -- quite clear why this check is necessary.
588 -- Otherwise step to enclosing compilation unit
590 while not Is_Compilation_Unit (E_Scope) loop
591 E_Scope := Scope (E_Scope);
594 -- For the case N is not an instance, or a call within instance
595 -- We recompute E_Scope for the error message, since we
596 -- do NOT want to go to the unit which has the ultimate
597 -- declaration in the case of renaming and derivation and
598 -- we also want to go to the generic unit in the case of
599 -- an instance, and no further.
602 -- Loop to carefully follow renamings and derivations
603 -- one step outside the current unit, but not further.
606 and then Present (Alias (Ent))
608 E_Scope := Alias (Ent);
614 while not Is_Compilation_Unit (E_Scope) loop
615 E_Scope := Scope (E_Scope);
618 -- If E_Scope is the same as C_Scope, it means that there
619 -- definitely was a local renaming or derivation, and we
620 -- are not yet out of the current unit.
622 exit when E_Scope /= C_Scope;
626 -- If no alias, there is a previous error
634 if Within_Elaborate_All (E_Scope) then
638 -- Find top level scope for called entity (not following renamings
639 -- or derivations). This is where the Elaborate_All will go if it
640 -- is needed. We start with the called entity, except in the case
641 -- of an initialization procedure outside the current package, where
642 -- the init proc is in the root package, and we start from the entity
643 -- of the name in the call.
645 if Is_Entity_Name (Name (N))
646 and then Is_Init_Proc (Entity (Name (N)))
647 and then not In_Same_Extended_Unit (N, Entity (Name (N)))
649 W_Scope := Scope (Entity (Name (N)));
654 while not Is_Compilation_Unit (W_Scope) loop
655 W_Scope := Scope (W_Scope);
658 -- Now check if an elaborate_all (or dynamic check) is needed
660 if not Suppress_Elaboration_Warnings (Ent)
661 and then not Elaboration_Checks_Suppressed (Ent)
662 and then not Suppress_Elaboration_Warnings (E_Scope)
663 and then not Elaboration_Checks_Suppressed (E_Scope)
664 and then Elab_Warnings
665 and then Generate_Warnings
669 ("instantiation of& may raise Program_Error?", N, Ent);
672 if Is_Init_Proc (Entity (Name (N)))
673 and then Comes_From_Source (Ent)
676 ("implicit call to & may raise Program_Error?", N, Ent);
680 ("call to & may raise Program_Error?", N, Ent);
684 Error_Msg_Qual_Level := Nat'Last;
686 ("\missing pragma Elaborate_All for&?", N, W_Scope);
687 Error_Msg_Qual_Level := 0;
690 -- Set flag to prevent further warnings for same unit
691 -- unless in All_Errors_Mode.
693 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
694 Set_Suppress_Elaboration_Warnings (W_Scope, True);
698 -- Check for runtime elaboration check required
700 if Dynamic_Elaboration_Checks then
701 if not Elaboration_Checks_Suppressed (Ent)
702 and then not Elaboration_Checks_Suppressed (W_Scope)
703 and then not Elaboration_Checks_Suppressed (E_Scope)
704 and then not Cunit_SC
706 -- Runtime elaboration check required. Generate check of the
707 -- elaboration Boolean for the unit containing the entity.
709 -- Note that for this case, we do check the real unit (the
710 -- one from following renamings, since that is the issue!)
712 -- Could this possibly miss a useless but required PE???
714 Insert_Elab_Check (N,
715 Make_Attribute_Reference (Loc,
716 Attribute_Name => Name_Elaborated,
719 (Spec_Entity (E_Scope), Loc)));
722 -- Case of static elaboration model
725 -- Do not do anything if elaboration checks suppressed. Note
726 -- that we check Ent here, not E, since we want the real entity
727 -- for the body to see if checks are suppressed for it, not the
728 -- dummy entry for renamings or derivations.
730 if Elaboration_Checks_Suppressed (Ent)
731 or else Elaboration_Checks_Suppressed (E_Scope)
732 or else Elaboration_Checks_Suppressed (W_Scope)
736 -- Here we need to generate an implicit elaborate all
739 -- Generate elaborate_all warning unless suppressed
741 if (Elab_Warnings and Generate_Warnings and not Inst_Case)
742 and then not Suppress_Elaboration_Warnings (Ent)
743 and then not Suppress_Elaboration_Warnings (E_Scope)
744 and then not Suppress_Elaboration_Warnings (W_Scope)
746 Error_Msg_Node_2 := W_Scope;
748 ("call to& in elaboration code " &
749 "requires pragma Elaborate_All on&?", N, E);
752 -- Set indication for binder to generate Elaborate_All
754 Set_Elaborate_All_Desirable (W_Scope);
755 Set_Suppress_Elaboration_Warnings (W_Scope, True);
759 -- Case of entity is in same unit as call or instantiation
761 elsif not Inter_Unit_Only then
762 Check_Internal_Call (N, Ent, Outer_Scope, E);
766 -----------------------------
767 -- Check_Bad_Instantiation --
768 -----------------------------
770 procedure Check_Bad_Instantiation (N : Node_Id) is
774 -- Nothing to do if we do not have an instantiation (happens in some
775 -- error cases, and also in the formal package declaration case)
777 if Nkind (N) not in N_Generic_Instantiation then
780 -- Nothing to do if serious errors detected (avoid cascaded errors)
782 elsif Serious_Errors_Detected /= 0 then
785 -- Nothing to do if not in full analysis mode
787 elsif not Full_Analysis then
790 -- Nothing to do if inside a generic template
792 elsif Inside_A_Generic then
795 -- Nothing to do if a library level instantiation
797 elsif Nkind (Parent (N)) = N_Compilation_Unit then
800 -- Nothing to do if we are compiling a proper body for semantic
801 -- purposes only. The generic body may be in another proper body.
804 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
809 Ent := Get_Generic_Entity (N);
811 -- The case we are interested in is when the generic spec is in the
812 -- current declarative part
814 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
815 or else not In_Same_Extended_Unit (N, Ent)
820 -- If the generic entity is within a deeper instance than we are, then
821 -- either the instantiation to which we refer itself caused an ABE, in
822 -- which case that will be handled separately. Otherwise, we know that
823 -- the body we need appears as needed at the point of the instantiation.
824 -- If they are both at the same level but not within the same instance
825 -- then the body of the generic will be in the earlier instance.
828 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
829 D2 : constant Int := Instantiation_Depth (Sloc (N));
836 and then Is_Generic_Instance (Scope (Ent))
837 and then not In_Open_Scopes (Scope (Ent))
843 -- Now we can proceed, if the entity being called has a completion,
844 -- then we are definitely OK, since we have already seen the body.
846 if Has_Completion (Ent) then
850 -- If there is no body, then nothing to do
852 if not Has_Generic_Body (N) then
856 -- Here we definitely have a bad instantiation
859 ("?cannot instantiate& before body seen", N, Ent);
861 if Present (Instance_Spec (N)) then
862 Supply_Bodies (Instance_Spec (N));
866 ("\?Program_Error will be raised at run time", N);
867 Insert_Elab_Check (N);
868 Set_ABE_Is_Certain (N);
870 end Check_Bad_Instantiation;
872 ---------------------
873 -- Check_Elab_Call --
874 ---------------------
876 procedure Check_Elab_Call
878 Outer_Scope : Entity_Id := Empty)
883 function Get_Called_Ent return Entity_Id;
884 -- Retrieve called entity. If this is a call to a protected subprogram,
885 -- entity is a selected component. The callable entity may be absent,
886 -- in which case there is no check to perform. This happens with
887 -- non-analyzed calls in nested generics.
893 function Get_Called_Ent return Entity_Id is
902 elsif Nkind (Nam) = N_Selected_Component then
903 return Entity (Selector_Name (Nam));
905 elsif not Is_Entity_Name (Nam) then
913 -- Start of processing for Check_Elab_Call
916 -- For an entry call, check relevant restriction
918 if Nkind (N) = N_Entry_Call_Statement
919 and then not In_Subprogram_Or_Concurrent_Unit
921 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
923 -- Nothing to do if this is not a call (happens in some error
924 -- conditions, and in some cases where rewriting occurs).
926 elsif Nkind (N) /= N_Function_Call
927 and then Nkind (N) /= N_Procedure_Call_Statement
931 -- Nothing to do if this is a call already rewritten for elab checking.
933 elsif Nkind (Parent (N)) = N_Conditional_Expression then
936 -- Nothing to do if inside a generic template
938 elsif Inside_A_Generic
939 and then not Present (Enclosing_Generic_Body (N))
944 -- Here we have a call at elaboration time which must be checked
946 if Debug_Flag_LL then
947 Write_Str (" Check_Elab_Call: ");
950 or else not Is_Entity_Name (Name (N))
952 Write_Str ("<<not entity name>> ");
954 Write_Name (Chars (Entity (Name (N))));
957 Write_Str (" call at ");
958 Write_Location (Sloc (N));
962 -- Climb up the tree to make sure we are not inside a
963 -- default expression of a parameter specification or
964 -- a record component, since in both these cases, we
965 -- will be doing the actual call later, not now, and it
966 -- is at the time of the actual call (statically speaking)
967 -- that we must do our static check, not at the time of
968 -- its initial analysis). However, we have to check calls
969 -- within component definitions (e.g., a function call
970 -- that determines an array component bound), so we
971 -- terminate the loop in that case.
974 while Present (P) loop
975 if Nkind (P) = N_Parameter_Specification
977 Nkind (P) = N_Component_Declaration
981 -- The call occurs within the constraint of a component,
982 -- so it must be checked.
984 elsif Nkind (P) = N_Component_Definition then
992 -- Stuff that happens only at the outer level
994 if No (Outer_Scope) then
995 Elab_Visited.Set_Last (0);
997 -- Nothing to do if current scope is Standard (this is a bit
998 -- odd, but it happens in the case of generic instantiations).
1000 C_Scope := Current_Scope;
1002 if C_Scope = Standard_Standard then
1006 -- First case, we are in elaboration code
1008 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1009 if From_Elab_Code then
1011 -- Complain if call that comes from source in preelaborated
1012 -- unit and we are not inside a subprogram (i.e. we are in
1015 if Comes_From_Source (N)
1016 and then In_Preelaborated_Unit
1017 and then not In_Inlined_Body
1020 ("non-static call not allowed in preelaborated unit", N);
1024 -- Second case, we are inside a subprogram or concurrent unit
1025 -- i.e, we are not in elaboration code.
1028 -- In this case, the issue is whether we are inside the
1029 -- declarative part of the unit in which we live, or inside
1030 -- its statements. In the latter case, there is no issue of
1031 -- ABE calls at this level (a call from outside to the unit
1032 -- in which we live might cause an ABE, but that will be
1033 -- detected when we analyze that outer level call, as it
1034 -- recurses into the called unit).
1036 -- Climb up the tree, doing this test, and also testing
1037 -- for being inside a default expression, which, as
1038 -- discussed above, is not checked at this stage.
1047 -- If we find a parentless subtree, it seems safe to
1048 -- assume that we are not in a declarative part and
1049 -- that no checking is required.
1055 if Is_List_Member (P) then
1056 L := List_Containing (P);
1063 exit when Nkind (P) = N_Subunit;
1065 -- Filter out case of default expressions, where
1066 -- we do not do the check at this stage.
1068 if Nkind (P) = N_Parameter_Specification
1070 Nkind (P) = N_Component_Declaration
1075 if Nkind (P) = N_Subprogram_Body
1077 Nkind (P) = N_Protected_Body
1079 Nkind (P) = N_Task_Body
1081 Nkind (P) = N_Block_Statement
1083 if L = Declarations (P) then
1086 -- We are not in elaboration code, but we are doing
1087 -- dynamic elaboration checks, in this case, we still
1088 -- need to do the call, since the subprogram we are in
1089 -- could be called from another unit, also in dynamic
1090 -- elaboration check mode, at elaboration time.
1092 elsif Dynamic_Elaboration_Checks then
1094 -- This is a rather new check, going into version
1095 -- 3.14a1 for the first time (V1.80 of this unit),
1096 -- so we provide a debug flag to enable it. That
1097 -- way we have an easy work around for regressions
1098 -- that are caused by this new check. This debug
1099 -- flag can be removed later.
1101 if Debug_Flag_DD then
1105 -- Do the check in this case
1109 elsif Nkind (P) = N_Task_Body then
1111 -- The check is deferred until Check_Task_Activation
1112 -- but we need to capture local suppress pragmas
1113 -- that may inhibit checks on this call.
1115 Ent := Get_Called_Ent;
1120 elsif Elaboration_Checks_Suppressed (Current_Scope)
1121 or else Elaboration_Checks_Suppressed (Ent)
1122 or else Elaboration_Checks_Suppressed (Scope (Ent))
1124 Set_No_Elaboration_Check (N);
1129 -- Static model, call is not in elaboration code, we
1130 -- never need to worry, because in the static model
1131 -- the top level caller always takes care of things.
1142 Ent := Get_Called_Ent;
1148 -- Nothing to do if this is a recursive call (i.e. a call to
1149 -- an entity that is already in the Elab_Call stack)
1151 for J in 1 .. Elab_Visited.Last loop
1152 if Ent = Elab_Visited.Table (J) then
1157 -- See if we need to analyze this call. We analyze it if either of
1158 -- the following conditions is met:
1160 -- It is an inner level call (since in this case it was triggered
1161 -- by an outer level call from elaboration code), but only if the
1162 -- call is within the scope of the original outer level call.
1164 -- It is an outer level call from elaboration code, or the called
1165 -- entity is in the same elaboration scope.
1167 -- And in these cases, we will check both inter-unit calls and
1168 -- intra-unit (within a single unit) calls.
1170 C_Scope := Current_Scope;
1172 -- If not outer level call, then we follow it if it is within
1173 -- the original scope of the outer call.
1175 if Present (Outer_Scope)
1176 and then Within (Scope (Ent), Outer_Scope)
1179 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1181 elsif Elaboration_Checks_Suppressed (Current_Scope) then
1184 elsif From_Elab_Code then
1186 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1188 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1190 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1192 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1193 -- is set, then we will do the check, but only in the inter-unit case
1194 -- (this is to accommodate unguarded elaboration calls from other units
1195 -- in which this same mode is set). We don't want warnings in this case,
1196 -- it would generate warnings having nothing to do with elaboration.
1198 elsif Dynamic_Elaboration_Checks then
1204 Inter_Unit_Only => True,
1205 Generate_Warnings => False);
1207 -- Otherwise nothing to do
1213 -- A call to an Init_Proc in elaboration code may bring additional
1214 -- dependencies, if some of the record components thereof have
1215 -- initializations that are function calls that come from source.
1216 -- We treat the current node as a call to each of these functions,
1217 -- to check their elaboration impact.
1219 if Is_Init_Proc (Ent)
1220 and then From_Elab_Code
1222 Process_Init_Proc : declare
1223 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1225 function Process (Nod : Node_Id) return Traverse_Result;
1226 -- Find subprogram calls within body of init_proc for
1227 -- Traverse instantiation below.
1229 function Process (Nod : Node_Id) return Traverse_Result is
1233 if (Nkind (Nod) = N_Function_Call
1234 or else Nkind (Nod) = N_Procedure_Call_Statement)
1235 and then Is_Entity_Name (Name (Nod))
1237 Func := Entity (Name (Nod));
1239 if Comes_From_Source (Func) then
1241 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1251 procedure Traverse_Body is new Traverse_Proc (Process);
1253 -- Start of processing for Process_Init_Proc
1256 if Nkind (Unit_Decl) = N_Subprogram_Body then
1257 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1259 end Process_Init_Proc;
1261 end Check_Elab_Call;
1263 ----------------------
1264 -- Check_Elab_Calls --
1265 ----------------------
1267 procedure Check_Elab_Calls is
1269 -- If expansion is disabled, do not generate any checks. Also
1270 -- skip checks if any subunits are missing because in either
1271 -- case we lack the full information that we need, and no object
1272 -- file will be created in any case.
1274 if not Expander_Active
1275 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1276 or else Subunits_Missing
1281 -- Skip delayed calls if we had any errors
1283 if Serious_Errors_Detected = 0 then
1284 Delaying_Elab_Checks := False;
1285 Expander_Mode_Save_And_Set (True);
1287 for J in Delay_Check.First .. Delay_Check.Last loop
1288 New_Scope (Delay_Check.Table (J).Curscop);
1289 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1291 Check_Internal_Call_Continue (
1292 N => Delay_Check.Table (J).N,
1293 E => Delay_Check.Table (J).E,
1294 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1295 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1300 -- Set Delaying_Elab_Checks back on for next main compilation
1302 Expander_Mode_Restore;
1303 Delaying_Elab_Checks := True;
1305 end Check_Elab_Calls;
1307 ------------------------------
1308 -- Check_Elab_Instantiation --
1309 ------------------------------
1311 procedure Check_Elab_Instantiation
1313 Outer_Scope : Entity_Id := Empty)
1318 -- Check for and deal with bad instantiation case. There is some
1319 -- duplicated code here, but we will worry about this later ???
1321 Check_Bad_Instantiation (N);
1323 if ABE_Is_Certain (N) then
1327 -- Nothing to do if we do not have an instantiation (happens in some
1328 -- error cases, and also in the formal package declaration case)
1330 if Nkind (N) not in N_Generic_Instantiation then
1334 -- Nothing to do if inside a generic template
1336 if Inside_A_Generic then
1340 Ent := Get_Generic_Entity (N);
1341 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1343 -- See if we need to analyze this instantiation. We analyze it if
1344 -- either of the following conditions is met:
1346 -- It is an inner level instantiation (since in this case it was
1347 -- triggered by an outer level call from elaboration code), but
1348 -- only if the instantiation is within the scope of the original
1349 -- outer level call.
1351 -- It is an outer level instantiation from elaboration code, or the
1352 -- instantiated entity is in the same elaboratoin scope.
1354 -- And in these cases, we will check both the inter-unit case and
1355 -- the intra-unit (within a single unit) case.
1357 C_Scope := Current_Scope;
1359 if Present (Outer_Scope)
1360 and then Within (Scope (Ent), Outer_Scope)
1363 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1365 elsif From_Elab_Code then
1367 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1369 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1371 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1373 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1374 -- is set, then we will do the check, but only in the inter-unit case
1375 -- (this is to accommodate unguarded elaboration calls from other units
1376 -- in which this same mode is set). We inhibit warnings in this case,
1377 -- since this instantiation is not occurring in elaboration code.
1379 elsif Dynamic_Elaboration_Checks then
1385 Inter_Unit_Only => True,
1386 Generate_Warnings => False);
1391 end Check_Elab_Instantiation;
1393 -------------------------
1394 -- Check_Internal_Call --
1395 -------------------------
1397 procedure Check_Internal_Call
1400 Outer_Scope : Entity_Id;
1401 Orig_Ent : Entity_Id)
1403 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1406 -- If not function or procedure call or instantiation, then ignore
1407 -- call (this happens in some error case and rewriting cases)
1409 if Nkind (N) /= N_Function_Call
1411 Nkind (N) /= N_Procedure_Call_Statement
1417 -- Nothing to do if this is a call or instantiation that has
1418 -- already been found to be a sure ABE
1420 elsif ABE_Is_Certain (N) then
1423 -- Nothing to do if errors already detected (avoid cascaded errors)
1425 elsif Serious_Errors_Detected /= 0 then
1428 -- Nothing to do if not in full analysis mode
1430 elsif not Full_Analysis then
1433 -- Nothing to do if within a default expression, since the call
1434 -- is not actualy being made at this time.
1436 elsif In_Default_Expression then
1439 -- Nothing to do for call to intrinsic subprogram
1441 elsif Is_Intrinsic_Subprogram (E) then
1444 -- No need to trace local calls if checking task activation, because
1445 -- other local bodies are elaborated already.
1447 elsif In_Task_Activation then
1451 -- Delay this call if we are still delaying calls
1453 if Delaying_Elab_Checks then
1454 Delay_Check.Increment_Last;
1455 Delay_Check.Table (Delay_Check.Last) :=
1458 Orig_Ent => Orig_Ent,
1459 Curscop => Current_Scope,
1460 Outer_Scope => Outer_Scope,
1461 From_Elab_Code => From_Elab_Code);
1464 -- Otherwise, call phase 2 continuation right now
1467 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1470 end Check_Internal_Call;
1472 ----------------------------------
1473 -- Check_Internal_Call_Continue --
1474 ----------------------------------
1476 procedure Check_Internal_Call_Continue
1479 Outer_Scope : Entity_Id;
1480 Orig_Ent : Entity_Id)
1482 Loc : constant Source_Ptr := Sloc (N);
1483 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1488 function Process (N : Node_Id) return Traverse_Result;
1489 -- Function applied to each node as we traverse the body.
1490 -- Checks for call that needs checking, and if so checks
1491 -- it. Always returns OK, so entire tree is traversed.
1497 function Process (N : Node_Id) return Traverse_Result is
1499 -- If user has specified that there are no entry calls in elaboration
1500 -- code, do not trace past an accept statement, because the rendez-
1501 -- vous will happen after elaboration.
1503 if (Nkind (Original_Node (N)) = N_Accept_Statement
1504 or else Nkind (Original_Node (N)) = N_Selective_Accept)
1505 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1509 -- If we have a subprogram call, check it
1511 elsif Nkind (N) = N_Function_Call
1512 or else Nkind (N) = N_Procedure_Call_Statement
1514 Check_Elab_Call (N, Outer_Scope);
1517 -- If we have a generic instantiation, check it
1519 elsif Nkind (N) in N_Generic_Instantiation then
1520 Check_Elab_Instantiation (N, Outer_Scope);
1523 -- Skip subprogram bodies that come from source (wait for
1524 -- call to analyze these). The reason for the come from
1525 -- source test is to avoid catching task bodies.
1527 -- For task bodies, we should really avoid these too, waiting
1528 -- for the task activation, but that's too much trouble to
1529 -- catch for now, so we go in unconditionally. This is not
1530 -- so terrible, it means the error backtrace is not quite
1531 -- complete, and we are too eager to scan bodies of tasks
1532 -- that are unused, but this is hardly very significant!
1534 elsif Nkind (N) = N_Subprogram_Body
1535 and then Comes_From_Source (N)
1544 procedure Traverse is new Atree.Traverse_Proc;
1545 -- Traverse procedure using above Process function
1547 -- Start of processing for Check_Internal_Call_Continue
1550 -- Save outer level call if at outer level
1552 if Elab_Call.Last = 0 then
1553 Outer_Level_Sloc := Loc;
1556 Elab_Visited.Increment_Last;
1557 Elab_Visited.Table (Elab_Visited.Last) := E;
1559 -- If the call is to a function that renames a literal, no check
1562 if Ekind (E) = E_Enumeration_Literal then
1566 Sbody := Unit_Declaration_Node (E);
1568 if Nkind (Sbody) /= N_Subprogram_Body
1570 Nkind (Sbody) /= N_Package_Body
1572 Ebody := Corresponding_Body (Sbody);
1577 Sbody := Unit_Declaration_Node (Ebody);
1581 -- If the body appears after the outer level call or
1582 -- instantiation then we have an error case handled below.
1584 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
1585 and then not In_Task_Activation
1589 -- If we have the instantiation case we are done, since we now
1590 -- know that the body of the generic appeared earlier.
1592 elsif Inst_Case then
1595 -- Otherwise we have a call, so we trace through the called
1596 -- body to see if it has any problems ..
1599 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
1601 Elab_Call.Increment_Last;
1602 Elab_Call.Table (Elab_Call.Last).Cloc := Loc;
1603 Elab_Call.Table (Elab_Call.Last).Ent := E;
1605 if Debug_Flag_LL then
1606 Write_Str ("Elab_Call.Last = ");
1607 Write_Int (Int (Elab_Call.Last));
1608 Write_Str (" Ent = ");
1609 Write_Name (Chars (E));
1611 Write_Location (Sloc (N));
1615 -- Now traverse declarations and statements of subprogram body.
1616 -- Note that we cannot simply Traverse (Sbody), since traverse
1617 -- does not normally visit subprogram bodies.
1620 Decl : Node_Id := First (Declarations (Sbody));
1623 while Present (Decl) loop
1629 Traverse (Handled_Statement_Sequence (Sbody));
1631 Elab_Call.Decrement_Last;
1635 -- Here is the case of calling a subprogram where the body has
1636 -- not yet been encountered, a warning message is needed.
1638 -- If we have nothing in the call stack, then this is at the
1639 -- outer level, and the ABE is bound to occur.
1641 if Elab_Call.Last = 0 then
1644 ("?cannot instantiate& before body seen", N, Orig_Ent);
1647 ("?cannot call& before body seen", N, Orig_Ent);
1651 ("\?Program_Error will be raised at run time", N);
1652 Insert_Elab_Check (N);
1654 -- Call is not at outer level
1657 -- Deal with dynamic elaboration check
1659 if not Elaboration_Checks_Suppressed (E) then
1660 Set_Elaboration_Entity_Required (E);
1662 -- Case of no elaboration entity allocated yet
1664 if No (Elaboration_Entity (E)) then
1666 -- Create object declaration for elaboration entity, and put it
1667 -- just in front of the spec of the subprogram or generic unit,
1668 -- in the same scope as this unit.
1671 Loce : constant Source_Ptr := Sloc (E);
1672 Ent : constant Entity_Id :=
1673 Make_Defining_Identifier (Loc,
1674 Chars => New_External_Name (Chars (E), 'E'));
1677 Set_Elaboration_Entity (E, Ent);
1678 New_Scope (Scope (E));
1680 Insert_Action (Declaration_Node (E),
1681 Make_Object_Declaration (Loce,
1682 Defining_Identifier => Ent,
1683 Object_Definition =>
1684 New_Occurrence_Of (Standard_Boolean, Loce),
1685 Expression => New_Occurrence_Of (Standard_False, Loce)));
1687 -- Set elaboration flag at the point of the body
1689 Set_Elaboration_Flag (Sbody, E);
1691 -- Kill current value indication. This is necessary
1692 -- because the tests of this flag are inserted out of
1693 -- sequence and must not pick up bogus indications of
1694 -- the wrong constant value. Also, this is never a true
1695 -- constant, since one way or another, it gets reset.
1697 Set_Current_Value (Ent, Empty);
1698 Set_Is_True_Constant (Ent, False);
1703 -- Generate check of the elaboration Boolean
1705 Insert_Elab_Check (N,
1706 New_Occurrence_Of (Elaboration_Entity (E), Loc));
1709 -- Generate the warning
1711 if not Suppress_Elaboration_Warnings (E)
1712 and then not Elaboration_Checks_Suppressed (E)
1716 ("instantiation of& may occur before body is seen?",
1720 ("call to& may occur before body is seen?", N, Orig_Ent);
1724 ("\Program_Error may be raised at run time?", N);
1730 -- Set flag to suppress further warnings on same subprogram
1731 -- unless in all errors mode
1733 if not All_Errors_Mode then
1734 Set_Suppress_Elaboration_Warnings (E);
1736 end Check_Internal_Call_Continue;
1738 ---------------------------
1739 -- Check_Task_Activation --
1740 ---------------------------
1742 procedure Check_Task_Activation (N : Node_Id) is
1743 Loc : constant Source_Ptr := Sloc (N);
1744 Inter_Procs : constant Elist_Id := New_Elmt_List;
1745 Intra_Procs : constant Elist_Id := New_Elmt_List;
1748 Task_Scope : Entity_Id;
1749 Cunit_SC : Boolean := False;
1752 Enclosing : Entity_Id;
1754 procedure Add_Task_Proc (Typ : Entity_Id);
1755 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
1756 -- For record types, this procedure recurses over component types.
1758 procedure Collect_Tasks (Decls : List_Id);
1759 -- Collect the types of the tasks that are to be activated in the given
1760 -- list of declarations, in order to perform elaboration checks on the
1761 -- corresponding task procedures which are called implicitly here.
1763 function Outer_Unit (E : Entity_Id) return Entity_Id;
1764 -- find enclosing compilation unit of Entity, ignoring subunits, or
1765 -- else enclosing subprogram. If E is not a package, there is no need
1766 -- for inter-unit elaboration checks.
1772 procedure Add_Task_Proc (Typ : Entity_Id) is
1774 Proc : Entity_Id := Empty;
1777 if Is_Task_Type (Typ) then
1778 Proc := Get_Task_Body_Procedure (Typ);
1780 elsif Is_Array_Type (Typ)
1781 and then Has_Task (Base_Type (Typ))
1783 Add_Task_Proc (Component_Type (Typ));
1785 elsif Is_Record_Type (Typ)
1786 and then Has_Task (Base_Type (Typ))
1788 Comp := First_Component (Typ);
1790 while Present (Comp) loop
1791 Add_Task_Proc (Etype (Comp));
1792 Comp := Next_Component (Comp);
1796 -- If the task type is another unit, we will perform the usual
1797 -- elaboration check on its enclosing unit. If the type is in the
1798 -- same unit, we can trace the task body as for an internal call,
1799 -- but we only need to examine other external calls, because at
1800 -- the point the task is activated, internal subprogram bodies
1801 -- will have been elaborated already. We keep separate lists for
1802 -- each kind of task.
1804 -- Skip this test if errors have occurred, since in this case
1805 -- we can get false indications.
1807 if Serious_Errors_Detected /= 0 then
1811 if Present (Proc) then
1812 if Outer_Unit (Scope (Proc)) = Enclosing then
1814 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
1816 (not Is_Generic_Instance (Scope (Proc))
1818 Scope (Proc) = Scope (Defining_Identifier (Decl)))
1821 ("task will be activated before elaboration of its body?",
1824 ("Program_Error will be raised at run-time?", Decl);
1827 Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
1829 Append_Elmt (Proc, Intra_Procs);
1833 Elmt := First_Elmt (Inter_Procs);
1835 -- No need for multiple entries of the same type.
1837 while Present (Elmt) loop
1838 if Node (Elmt) = Proc then
1845 Append_Elmt (Proc, Inter_Procs);
1854 procedure Collect_Tasks (Decls : List_Id) is
1856 if Present (Decls) then
1857 Decl := First (Decls);
1859 while Present (Decl) loop
1861 if Nkind (Decl) = N_Object_Declaration
1862 and then Has_Task (Etype (Defining_Identifier (Decl)))
1864 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
1876 function Outer_Unit (E : Entity_Id) return Entity_Id is
1877 Outer : Entity_Id := E;
1880 while Present (Outer) loop
1881 if Elaboration_Checks_Suppressed (Outer) then
1885 exit when Is_Child_Unit (Outer)
1886 or else Scope (Outer) = Standard_Standard
1887 or else Ekind (Outer) /= E_Package;
1888 Outer := Scope (Outer);
1894 -- Start of processing for Check_Task_Activation
1897 Enclosing := Outer_Unit (Current_Scope);
1899 -- Find all tasks declared in the current unit.
1901 if Nkind (N) = N_Package_Body then
1902 P := Unit_Declaration_Node (Corresponding_Spec (N));
1904 Collect_Tasks (Declarations (N));
1905 Collect_Tasks (Visible_Declarations (Specification (P)));
1906 Collect_Tasks (Private_Declarations (Specification (P)));
1908 elsif Nkind (N) = N_Package_Declaration then
1909 Collect_Tasks (Visible_Declarations (Specification (N)));
1910 Collect_Tasks (Private_Declarations (Specification (N)));
1913 Collect_Tasks (Declarations (N));
1916 -- We only perform detailed checks in all tasks are library level
1917 -- entities. If the master is a subprogram or task, activation will
1918 -- depend on the activation of the master itself.
1919 -- Should dynamic checks be added in the more general case???
1921 if Ekind (Enclosing) /= E_Package then
1925 -- For task types defined in other units, we want the unit containing
1926 -- the task body to be elaborated before the current one.
1928 Elmt := First_Elmt (Inter_Procs);
1930 while Present (Elmt) loop
1932 Task_Scope := Outer_Unit (Scope (Ent));
1934 if not Is_Compilation_Unit (Task_Scope) then
1937 elsif Suppress_Elaboration_Warnings (Task_Scope)
1938 or else Elaboration_Checks_Suppressed (Task_Scope)
1942 elsif Dynamic_Elaboration_Checks then
1943 if not Elaboration_Checks_Suppressed (Ent)
1944 and then not Cunit_SC
1946 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1948 -- Runtime elaboration check required. generate check of the
1949 -- elaboration Boolean for the unit containing the entity.
1951 Insert_Elab_Check (N,
1952 Make_Attribute_Reference (Loc,
1953 Attribute_Name => Name_Elaborated,
1956 (Spec_Entity (Task_Scope), Loc)));
1960 -- Force the binder to elaborate other unit first
1962 if not Suppress_Elaboration_Warnings (Ent)
1963 and then not Elaboration_Checks_Suppressed (Ent)
1964 and then Elab_Warnings
1965 and then not Suppress_Elaboration_Warnings (Task_Scope)
1966 and then not Elaboration_Checks_Suppressed (Task_Scope)
1968 Error_Msg_Node_2 := Task_Scope;
1969 Error_Msg_NE ("activation of an instance of task type&" &
1970 " requires pragma Elaborate_All on &?", N, Ent);
1973 Set_Elaborate_All_Desirable (Task_Scope);
1974 Set_Suppress_Elaboration_Warnings (Task_Scope);
1980 -- For tasks declared in the current unit, trace other calls within
1981 -- the task procedure bodies, which are available.
1983 In_Task_Activation := True;
1984 Elmt := First_Elmt (Intra_Procs);
1986 while Present (Elmt) loop
1988 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
1992 In_Task_Activation := False;
1993 end Check_Task_Activation;
1995 ----------------------
1996 -- Has_Generic_Body --
1997 ----------------------
1999 function Has_Generic_Body (N : Node_Id) return Boolean is
2000 Ent : constant Entity_Id := Get_Generic_Entity (N);
2001 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2004 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2005 -- Determine if the list of nodes headed by N and linked by Next
2006 -- contains a package body for the package spec entity E, and if
2007 -- so return the package body. If not, then returns Empty.
2009 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2010 -- This procedure is called load the unit whose name is given by Nam.
2011 -- This unit is being loaded to see whether it contains an optional
2012 -- generic body. The returned value is the loaded unit, which is
2013 -- always a package body (only package bodies can contain other
2014 -- entities in the sense in which Has_Generic_Body is interested).
2015 -- We only attempt to load bodies if we are generating code. If we
2016 -- are in semantics check only mode, then it would be wrong to load
2017 -- bodies that are not required from a semantic point of view, so
2018 -- in this case we return Empty. The result is that the caller may
2019 -- incorrectly decide that a generic spec does not have a body when
2020 -- in fact it does, but the only harm in this is that some warnings
2021 -- on elaboration problems may be lost in semantic checks only mode,
2022 -- which is not big loss. We also return Empty if we go for a body
2023 -- and it is not there.
2025 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2026 -- PE is the entity for a package spec. This function locates the
2027 -- corresponding package body, returning Empty if none is found.
2028 -- The package body returned is fully parsed but may not yet be
2029 -- analyzed, so only syntactic fields should be referenced.
2035 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2040 while Present (Nod) loop
2042 -- If we found the package body we are looking for, return it
2044 if Nkind (Nod) = N_Package_Body
2045 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2049 -- If we found the stub for the body, go after the subunit,
2050 -- loading it if necessary.
2052 elsif Nkind (Nod) = N_Package_Body_Stub
2053 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2055 if Present (Library_Unit (Nod)) then
2056 return Unit (Library_Unit (Nod));
2059 return Load_Package_Body (Get_Unit_Name (Nod));
2062 -- If neither package body nor stub, keep looking on chain
2072 -----------------------
2073 -- Load_Package_Body --
2074 -----------------------
2076 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2077 U : Unit_Number_Type;
2080 if Operating_Mode /= Generate_Code then
2093 return Unit (Cunit (U));
2096 end Load_Package_Body;
2098 -------------------------------
2099 -- Locate_Corresponding_Body --
2100 -------------------------------
2102 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2103 Spec : constant Node_Id := Declaration_Node (PE);
2104 Decl : constant Node_Id := Parent (Spec);
2105 Scop : constant Entity_Id := Scope (PE);
2109 if Is_Library_Level_Entity (PE) then
2111 -- If package is a library unit that requires a body, we have
2112 -- no choice but to go after that body because it might contain
2113 -- an optional body for the original generic package.
2115 if Unit_Requires_Body (PE) then
2117 -- Load the body. Note that we are a little careful here to
2118 -- use Spec to get the unit number, rather than PE or Decl,
2119 -- since in the case where the package is itself a library
2120 -- level instantiation, Spec will properly reference the
2121 -- generic template, which is what we really want.
2125 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2127 -- But if the package is a library unit that does NOT require
2128 -- a body, then no body is permitted, so we are sure that there
2129 -- is no body for the original generic package.
2135 -- Otherwise look and see if we are embedded in a further package
2137 elsif Is_Package (Scop) then
2139 -- If so, get the body of the enclosing package, and look in
2140 -- its package body for the package body we are looking for.
2142 PBody := Locate_Corresponding_Body (Scop);
2147 return Find_Body_In (PE, First (Declarations (PBody)));
2150 -- If we are not embedded in a further package, then the body
2151 -- must be in the same declarative part as we are.
2154 return Find_Body_In (PE, Next (Decl));
2156 end Locate_Corresponding_Body;
2158 -- Start of processing for Has_Generic_Body
2161 if Present (Corresponding_Body (Decl)) then
2164 elsif Unit_Requires_Body (Ent) then
2167 -- Compilation units cannot have optional bodies
2169 elsif Is_Compilation_Unit (Ent) then
2172 -- Otherwise look at what scope we are in
2175 Scop := Scope (Ent);
2177 -- Case of entity is in other than a package spec, in this case
2178 -- the body, if present, must be in the same declarative part.
2180 if not Is_Package (Scop) then
2185 P := Declaration_Node (Ent);
2187 -- Declaration node may get us a spec, so if so, go to
2188 -- the parent declaration.
2190 while not Is_List_Member (P) loop
2194 return Present (Find_Body_In (Ent, Next (P)));
2197 -- If the entity is in a package spec, then we have to locate
2198 -- the corresponding package body, and look there.
2202 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2210 (Find_Body_In (Ent, (First (Declarations (PBody)))));
2215 end Has_Generic_Body;
2217 -----------------------
2218 -- Insert_Elab_Check --
2219 -----------------------
2221 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2223 Loc : constant Source_Ptr := Sloc (N);
2226 -- If expansion is disabled, do not generate any checks. Also
2227 -- skip checks if any subunits are missing because in either
2228 -- case we lack the full information that we need, and no object
2229 -- file will be created in any case.
2231 if not Expander_Active or else Subunits_Missing then
2235 -- If we have a generic instantiation, where Instance_Spec is set,
2236 -- then this field points to a generic instance spec that has
2237 -- been inserted before the instantiation node itself, so that
2238 -- is where we want to insert a check.
2240 if Nkind (N) in N_Generic_Instantiation
2241 and then Present (Instance_Spec (N))
2243 Nod := Instance_Spec (N);
2248 -- If we are inserting at the top level, insert in Aux_Decls
2250 if Nkind (Parent (Nod)) = N_Compilation_Unit then
2252 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2258 Make_Raise_Program_Error (Loc,
2259 Reason => PE_Access_Before_Elaboration);
2262 Make_Raise_Program_Error (Loc,
2263 Condition => Make_Op_Not (Loc, C),
2264 Reason => PE_Access_Before_Elaboration);
2267 if No (Declarations (ADN)) then
2268 Set_Declarations (ADN, New_List (R));
2270 Append_To (Declarations (ADN), R);
2276 -- Otherwise just insert before the node in question. However, if
2277 -- the context of the call has already been analyzed, an insertion
2278 -- will not work if it depends on subsequent expansion (e.g. a call in
2279 -- a branch of a short-circuit). In that case we replace the call with
2280 -- a conditional expression, or with a Raise if it is unconditional.
2281 -- Unfortunately this does not work if the call has a dynamic size,
2282 -- because gigi regards it as a dynamic-sized temporary. If such a call
2283 -- appears in a short-circuit expression, the elaboration check will be
2284 -- missed (rare enough ???). Otherwise, the code below inserts the check
2285 -- at the appropriate place before the call. Same applies in the even
2286 -- rarer case the return type has a known size but is unconstrained.
2289 if Nkind (N) = N_Function_Call
2290 and then Analyzed (Parent (N))
2291 and then Size_Known_At_Compile_Time (Etype (N))
2293 (not Has_Discriminants (Etype (N))
2294 or else Is_Constrained (Etype (N)))
2298 Typ : constant Entity_Id := Etype (N);
2299 Chk : constant Boolean := Do_Range_Check (N);
2301 R : constant Node_Id :=
2302 Make_Raise_Program_Error (Loc,
2303 Reason => PE_Access_Before_Elaboration);
2313 Make_Conditional_Expression (Loc,
2314 Expressions => New_List (C, Relocate_Node (N), R)));
2317 Analyze_And_Resolve (N, Typ);
2319 -- If the original call requires a range check, so does the
2320 -- conditional expression.
2323 Enable_Range_Check (N);
2325 Set_Do_Range_Check (N, False);
2332 Make_Raise_Program_Error (Loc,
2333 Reason => PE_Access_Before_Elaboration));
2336 Make_Raise_Program_Error (Loc,
2340 Reason => PE_Access_Before_Elaboration));
2344 end Insert_Elab_Check;
2350 procedure Output_Calls (N : Node_Id) is
2353 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
2354 -- An internal function, used to determine if a name, Nm, is either
2355 -- a non-internal name, or is an internal name that is printable
2356 -- by the error message circuits (i.e. it has a single upper
2357 -- case letter at the end).
2359 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
2361 if not Is_Internal_Name (Nm) then
2364 elsif Name_Len = 1 then
2368 Name_Len := Name_Len - 1;
2369 return not Is_Internal_Name;
2371 end Is_Printable_Error_Name;
2373 -- Start of processing for Output_Calls
2376 for J in reverse 1 .. Elab_Call.Last loop
2377 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
2379 Ent := Elab_Call.Table (J).Ent;
2381 if Is_Generic_Unit (Ent) then
2382 Error_Msg_NE ("\?& instantiated #", N, Ent);
2384 elsif Is_Init_Proc (Ent) then
2385 Error_Msg_N ("\?initialization procedure called #", N);
2387 elsif Is_Printable_Error_Name (Chars (Ent)) then
2388 Error_Msg_NE ("\?& called #", N, Ent);
2391 Error_Msg_N ("\? called #", N);
2396 ----------------------------
2397 -- Same_Elaboration_Scope --
2398 ----------------------------
2400 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
2401 S1 : Entity_Id := Scop1;
2402 S2 : Entity_Id := Scop2;
2405 while S1 /= Standard_Standard
2406 and then (Ekind (S1) = E_Package
2408 Ekind (S1) = E_Block)
2413 while S2 /= Standard_Standard
2414 and then (Ekind (S2) = E_Package
2416 Ekind (S2) = E_Protected_Type
2418 Ekind (S2) = E_Block)
2424 end Same_Elaboration_Scope;
2430 procedure Set_C_Scope is
2432 while not Is_Compilation_Unit (C_Scope) loop
2433 C_Scope := Scope (C_Scope);
2441 function Spec_Entity (E : Entity_Id) return Entity_Id is
2445 -- Check for case of body entity
2446 -- Why is the check for E_Void needed???
2448 if Ekind (E) = E_Void
2449 or else Ekind (E) = E_Subprogram_Body
2450 or else Ekind (E) = E_Package_Body
2455 Decl := Parent (Decl);
2456 exit when Nkind (Decl) in N_Proper_Body;
2459 return Corresponding_Spec (Decl);
2470 procedure Supply_Bodies (N : Node_Id) is
2472 if Nkind (N) = N_Subprogram_Declaration then
2474 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
2477 Set_Is_Imported (Ent);
2478 Set_Convention (Ent, Convention_Stubbed);
2481 elsif Nkind (N) = N_Package_Declaration then
2483 Spec : constant Node_Id := Specification (N);
2486 New_Scope (Defining_Unit_Name (Spec));
2487 Supply_Bodies (Visible_Declarations (Spec));
2488 Supply_Bodies (Private_Declarations (Spec));
2494 procedure Supply_Bodies (L : List_Id) is
2500 while Present (Elmt) loop
2501 Supply_Bodies (Elmt);
2511 function Within (E1, E2 : Entity_Id) return Boolean is
2521 elsif Scop = Standard_Standard then
2525 Scop := Scope (Scop);
2529 raise Program_Error;
2532 --------------------------
2533 -- Within_Elaborate_All --
2534 --------------------------
2536 function Within_Elaborate_All (E : Entity_Id) return Boolean is
2539 Elab_Id : Entity_Id;
2543 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
2545 while Present (Item) loop
2546 if Nkind (Item) = N_Pragma
2547 and then Get_Pragma_Id (Chars (Item)) = Pragma_Elaborate_All
2549 if Error_Posted (Item) then
2551 -- Some previous error on the pragma itself
2558 Expression (First (Pragma_Argument_Associations (Item))));
2560 Par := Parent (Unit_Declaration_Node (Elab_Id));
2561 Item2 := First (Context_Items (Par));
2563 while Present (Item2) loop
2564 if Nkind (Item2) = N_With_Clause
2565 and then Entity (Name (Item2)) = E
2578 end Within_Elaborate_All;