2005-12-26 Anthony Green <green@redhat.com>
[official-gcc.git] / gcc / ada / sem_elab.adb
blob1eae58685b4145227ac2044e97ad08a3190fd703
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ E L A B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1997-2005, Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
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;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Namet; use Namet;
40 with Nlists; use Nlists;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Output; use Output;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
46 with Sem; use Sem;
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;
56 with Table;
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
69 Cloc : Source_Ptr;
70 Ent : Entity_Id;
71 end record;
73 package Elab_Call is new Table.Table (
74 Table_Component_Type => Elab_Call_Entry,
75 Table_Index_Type => Int,
76 Table_Low_Bound => 1,
77 Table_Initial => 50,
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,
89 Table_Low_Bound => 1,
90 Table_Initial => 200,
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
100 N : Node_Id;
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).
105 E : Entity_Id;
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
111 Curscop : Entity_Id;
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
120 end record;
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");
130 C_Scope : Entity_Id;
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
149 -- to be elaborated.
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
176 (N : Node_Id;
177 E : Entity_Id;
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
204 -- instance body.
206 procedure Check_Internal_Call
207 (N : Node_Id;
208 E : Entity_Id;
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
221 (N : Node_Id;
222 E : Entity_Id;
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
234 (Call : Node_Id;
235 Subp : Entity_Id;
236 Scop : Entity_Id);
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
250 -- declared in P.
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);
327 Itm : Node_Id;
328 Ent : Entity_Id;
330 procedure Set_Elab_Flag (Itm : Node_Id);
331 -- Sets Elaborate_[All_]Desirable as appropriate on Itm
333 -------------------
334 -- Set_Elab_Flag --
335 -------------------
337 procedure Set_Elab_Flag (Itm : Node_Id) is
338 begin
339 if Nkind (N) in N_Subprogram_Instantiation then
340 Set_Elaborate_Desirable (Itm);
341 else
342 Set_Elaborate_All_Desirable (Itm);
343 end if;
344 end Set_Elab_Flag;
346 -- Start of processing for Activate_Elaborate_All_Desirable
348 begin
349 Itm := First (CI);
350 while Present (Itm) loop
351 if Nkind (Itm) = N_With_Clause then
352 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
354 -- If we find it, then mark elaborate all desirable and return
356 if U = Ent then
357 Set_Elab_Flag (Itm);
358 return;
359 end if;
360 end if;
362 Next (Itm);
363 end loop;
365 -- If we fall through then the with clause is not present in the
366 -- current unit. One legitimate possibility is that the with clause
367 -- is present in the spec when we are a body.
369 if Is_Body_Name (Unm) then
370 declare
371 UEs : constant Entity_Id := Spec_Entity (UE);
372 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs);
373 CUs : constant Node_Id := Cunit (UNs);
374 CIs : constant List_Id := Context_Items (CUs);
376 begin
377 Itm := First (CIs);
378 while Present (Itm) loop
379 if Nkind (Itm) = N_With_Clause then
380 Ent :=
381 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm)));
383 if U = Ent then
385 -- If we find it, we have to create an implicit copy
386 -- of the with clause for the body, just so that it
387 -- can be marked as elaborate desirable (it would be
388 -- wrong to put it on the spec item, since it is the
389 -- body that has possible elaboration problems, not
390 -- the spec.
392 declare
393 CW : constant Node_Id :=
394 Make_With_Clause (Sloc (Itm),
395 Name => Name (Itm));
397 begin
398 Set_Library_Unit (CW, Library_Unit (Itm));
399 Set_Implicit_With (CW, True);
401 -- Set elaborate all desirable on copy and then
402 -- append the copy to the list of body with's
403 -- and we are done.
405 Set_Elab_Flag (CW);
406 Append_To (CI, CW);
407 return;
408 end;
409 end if;
410 end if;
412 Next (Itm);
413 end loop;
414 end;
415 end if;
417 -- Here if we do not find with clause on spec or body. We just ignore
418 -- this case, it means that the elaboration involves some other unit
419 -- than the unit being compiled, and will be caught elsewhere.
421 null;
422 end Activate_Elaborate_All_Desirable;
424 ------------------
425 -- Check_A_Call --
426 ------------------
428 procedure Check_A_Call
429 (N : Node_Id;
430 E : Entity_Id;
431 Outer_Scope : Entity_Id;
432 Inter_Unit_Only : Boolean;
433 Generate_Warnings : Boolean := True)
435 Loc : constant Source_Ptr := Sloc (N);
436 Ent : Entity_Id;
437 Decl : Node_Id;
439 E_Scope : Entity_Id;
440 -- Top level scope of entity for called subprogram. This
441 -- value includes following renamings and derivations, so
442 -- this scope can be in a non-visible unit. This is the
443 -- scope that is to be investigated to see whether an
444 -- elaboration check is required.
446 W_Scope : Entity_Id;
447 -- Top level scope of directly called entity for subprogram. This
448 -- differs from E_Scope in the case where renamings or derivations
449 -- are involved, since it does not follow these links. W_Scope is
450 -- generally in a visible unit, and it is this scope that may require
451 -- an Elaborate_All. However, there are some cases (initialization
452 -- calls and calls involving object notation) where W_Scope might not
453 -- be in the context of the current unit, and there is an intermediate
454 -- package that is, in which case the Elaborate_All has to be placed
455 -- on this intedermediate package. These special cases are handled in
456 -- Set_Elaboration_Constraint.
458 Body_Acts_As_Spec : Boolean;
459 -- Set to true if call is to body acting as spec (no separate spec)
461 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
462 -- Indicates if we have instantiation case
464 Caller_Unit_Internal : Boolean;
465 Callee_Unit_Internal : Boolean;
467 Inst_Caller : Source_Ptr;
468 Inst_Callee : Source_Ptr;
470 Unit_Caller : Unit_Number_Type;
471 Unit_Callee : Unit_Number_Type;
473 Cunit_SC : Boolean := False;
474 -- Set to suppress dynamic elaboration checks where one of the
475 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else
476 -- if a pragma Elaborate (_All) applies to that scope, in which case
477 -- warnings on the scope are also suppressed. For the internal case,
478 -- we ignore this flag.
480 begin
481 -- If the call is known to be within a local Suppress Elaboration
482 -- pragma, nothing to check. This can happen in task bodies.
484 if (Nkind (N) = N_Function_Call
485 or else Nkind (N) = N_Procedure_Call_Statement)
486 and then No_Elaboration_Check (N)
487 then
488 return;
489 end if;
491 -- Go to parent for derived subprogram, or to original subprogram
492 -- in the case of a renaming (Alias covers both these cases)
494 Ent := E;
495 loop
496 if (Suppress_Elaboration_Warnings (Ent)
497 or else Elaboration_Checks_Suppressed (Ent))
498 and then (Inst_Case or else No (Alias (Ent)))
499 then
500 return;
501 end if;
503 -- Nothing to do for imported entities
505 if Is_Imported (Ent) then
506 return;
507 end if;
509 exit when Inst_Case or else No (Alias (Ent));
510 Ent := Alias (Ent);
511 end loop;
513 Decl := Unit_Declaration_Node (Ent);
515 if Nkind (Decl) = N_Subprogram_Body then
516 Body_Acts_As_Spec := True;
518 elsif Nkind (Decl) = N_Subprogram_Declaration
519 or else Nkind (Decl) = N_Subprogram_Body_Stub
520 or else Inst_Case
521 then
522 Body_Acts_As_Spec := False;
524 -- If we have none of an instantiation, subprogram body or
525 -- subprogram declaration, then it is not a case that we want
526 -- to check. (One case is a call to a generic formal subprogram,
527 -- where we do not want the check in the template).
529 else
530 return;
531 end if;
533 E_Scope := Ent;
534 loop
535 if Elaboration_Checks_Suppressed (E_Scope)
536 or else Suppress_Elaboration_Warnings (E_Scope)
537 then
538 Cunit_SC := True;
539 end if;
541 -- Exit when we get to compilation unit, not counting subunits
543 exit when Is_Compilation_Unit (E_Scope)
544 and then (Is_Child_Unit (E_Scope)
545 or else Scope (E_Scope) = Standard_Standard);
547 -- If we did not find a compilation unit, other than standard,
548 -- then nothing to check (happens in some instantiation cases)
550 if E_Scope = Standard_Standard then
551 return;
553 -- Otherwise move up a scope looking for compilation unit
555 else
556 E_Scope := Scope (E_Scope);
557 end if;
558 end loop;
560 -- No checks needed for pure or preelaborated compilation units
562 if Is_Pure (E_Scope)
563 or else Is_Preelaborated (E_Scope)
564 then
565 return;
566 end if;
568 -- If the generic entity is within a deeper instance than we are, then
569 -- either the instantiation to which we refer itself caused an ABE, in
570 -- which case that will be handled separately, or else we know that the
571 -- body we need appears as needed at the point of the instantiation.
572 -- However, this assumption is only valid if we are in static mode.
574 if not Dynamic_Elaboration_Checks
575 and then Instantiation_Depth (Sloc (Ent)) >
576 Instantiation_Depth (Sloc (N))
577 then
578 return;
579 end if;
581 -- Do not give a warning for a package with no body
583 if Ekind (Ent) = E_Generic_Package
584 and then not Has_Generic_Body (N)
585 then
586 return;
587 end if;
589 -- Case of entity is not in current unit (i.e. with'ed unit case)
591 if E_Scope /= C_Scope then
593 -- We are only interested in such calls if the outer call was from
594 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
596 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
597 return;
598 end if;
600 -- Nothing to do if some scope said that no checks were required
602 if Cunit_SC then
603 return;
604 end if;
606 -- Nothing to do for a generic instance, because in this case
607 -- the checking was at the point of instantiation of the generic
608 -- However, this shortcut is only applicable in static mode.
610 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
611 return;
612 end if;
614 -- Nothing to do if subprogram with no separate spec. However,
615 -- a call to Deep_Initialize may result in a call to a user-defined
616 -- Initialize procedure, which imposes a body dependency. This
617 -- happens only if the type is controlled and the Initialize
618 -- procedure is not inherited.
620 if Body_Acts_As_Spec then
621 if Is_TSS (Ent, TSS_Deep_Initialize) then
622 declare
623 Typ : Entity_Id;
624 Init : Entity_Id;
625 begin
626 Typ := Etype (Next_Formal (First_Formal (Ent)));
628 if not Is_Controlled (Typ) then
629 return;
630 else
631 Init := Find_Prim_Op (Typ, Name_Initialize);
633 if Comes_From_Source (Init) then
634 Ent := Init;
635 else
636 return;
637 end if;
638 end if;
639 end;
641 else
642 return;
643 end if;
644 end if;
646 -- Check cases of internal units
648 Callee_Unit_Internal :=
649 Is_Internal_File_Name
650 (Unit_File_Name (Get_Source_Unit (E_Scope)));
652 -- Do not give a warning if the with'ed unit is internal
653 -- and this is the generic instantiation case (this saves a
654 -- lot of hassle dealing with the Text_IO special child units)
656 if Callee_Unit_Internal and Inst_Case then
657 return;
658 end if;
660 if C_Scope = Standard_Standard then
661 Caller_Unit_Internal := False;
662 else
663 Caller_Unit_Internal :=
664 Is_Internal_File_Name
665 (Unit_File_Name (Get_Source_Unit (C_Scope)));
666 end if;
668 -- Do not give a warning if the with'ed unit is internal
669 -- and the caller is not internal (since the binder always
670 -- elaborates internal units first).
672 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
673 return;
674 end if;
676 -- For now, if debug flag -gnatdE is not set, do no checking for
677 -- one internal unit withing another. This fixes the problem with
678 -- the sgi build and storage errors. To be resolved later ???
680 if (Callee_Unit_Internal and Caller_Unit_Internal)
681 and then not Debug_Flag_EE
682 then
683 return;
684 end if;
686 if Is_TSS (E, TSS_Deep_Initialize) then
687 Ent := E;
688 end if;
690 -- If the call is in an instance, and the called entity is not
691 -- defined in the same instance, then the elaboration issue
692 -- focuses around the unit containing the template, it is
693 -- this unit which requires an Elaborate_All.
695 -- However, if we are doing dynamic elaboration, we need to
696 -- chase the call in the usual manner.
698 -- We do not handle the case of calling a generic formal correctly
699 -- in the static case. See test 4703-004 to explore this gap ???
701 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
702 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
704 if Inst_Caller = No_Location then
705 Unit_Caller := No_Unit;
706 else
707 Unit_Caller := Get_Source_Unit (N);
708 end if;
710 if Inst_Callee = No_Location then
711 Unit_Callee := No_Unit;
712 else
713 Unit_Callee := Get_Source_Unit (Ent);
714 end if;
716 if Unit_Caller /= No_Unit
717 and then Unit_Callee /= Unit_Caller
718 and then not Dynamic_Elaboration_Checks
719 then
720 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
722 -- If we don't get a spec entity, just ignore call. Not
723 -- quite clear why this check is necessary.
725 if No (E_Scope) then
726 return;
727 end if;
729 -- Otherwise step to enclosing compilation unit
731 while not Is_Compilation_Unit (E_Scope) loop
732 E_Scope := Scope (E_Scope);
733 end loop;
735 -- For the case N is not an instance, or a call within instance
736 -- We recompute E_Scope for the error message, since we
737 -- do NOT want to go to the unit which has the ultimate
738 -- declaration in the case of renaming and derivation and
739 -- we also want to go to the generic unit in the case of
740 -- an instance, and no further.
742 else
743 -- Loop to carefully follow renamings and derivations
744 -- one step outside the current unit, but not further.
746 if not Inst_Case
747 and then Present (Alias (Ent))
748 then
749 E_Scope := Alias (Ent);
750 else
751 E_Scope := Ent;
752 end if;
754 loop
755 while not Is_Compilation_Unit (E_Scope) loop
756 E_Scope := Scope (E_Scope);
757 end loop;
759 -- If E_Scope is the same as C_Scope, it means that there
760 -- definitely was a local renaming or derivation, and we
761 -- are not yet out of the current unit.
763 exit when E_Scope /= C_Scope;
764 Ent := Alias (Ent);
765 E_Scope := Ent;
767 -- If no alias, there is a previous error
769 if No (Ent) then
770 return;
771 end if;
772 end loop;
773 end if;
775 if Within_Elaborate_All (E_Scope) then
776 return;
777 end if;
779 -- Find top level scope for called entity (not following renamings
780 -- or derivations). This is where the Elaborate_All will go if it
781 -- is needed. We start with the called entity, except in the case
782 -- of an initialization procedure outside the current package, where
783 -- the init proc is in the root package, and we start from the entity
784 -- of the name in the call.
786 if Is_Entity_Name (Name (N))
787 and then Is_Init_Proc (Entity (Name (N)))
788 and then not In_Same_Extended_Unit (N, Entity (Name (N)))
789 then
790 W_Scope := Scope (Entity (Name (N)));
791 else
792 W_Scope := E;
793 end if;
795 while not Is_Compilation_Unit (W_Scope) loop
796 W_Scope := Scope (W_Scope);
797 end loop;
799 -- Now check if an elaborate_all (or dynamic check) is needed
801 if not Suppress_Elaboration_Warnings (Ent)
802 and then not Elaboration_Checks_Suppressed (Ent)
803 and then not Suppress_Elaboration_Warnings (E_Scope)
804 and then not Elaboration_Checks_Suppressed (E_Scope)
805 and then Elab_Warnings
806 and then Generate_Warnings
807 then
808 if Inst_Case then
809 Error_Msg_NE
810 ("instantiation of& may raise Program_Error?", N, Ent);
812 else
813 if Is_Init_Proc (Entity (Name (N)))
814 and then Comes_From_Source (Ent)
815 then
816 Error_Msg_NE
817 ("implicit call to & may raise Program_Error?", N, Ent);
819 else
820 Error_Msg_NE
821 ("call to & may raise Program_Error?", N, Ent);
822 end if;
823 end if;
825 Error_Msg_Qual_Level := Nat'Last;
827 if Nkind (N) in N_Subprogram_Instantiation then
828 Error_Msg_NE
829 ("\missing pragma Elaborate for&?", N, W_Scope);
830 else
831 Error_Msg_NE
832 ("\missing pragma Elaborate_All for&?", N, W_Scope);
833 end if;
835 Error_Msg_Qual_Level := 0;
836 Output_Calls (N);
838 -- Set flag to prevent further warnings for same unit
839 -- unless in All_Errors_Mode.
841 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
842 Set_Suppress_Elaboration_Warnings (W_Scope, True);
843 end if;
844 end if;
846 -- Check for runtime elaboration check required
848 if Dynamic_Elaboration_Checks then
849 if not Elaboration_Checks_Suppressed (Ent)
850 and then not Elaboration_Checks_Suppressed (W_Scope)
851 and then not Elaboration_Checks_Suppressed (E_Scope)
852 and then not Cunit_SC
853 then
854 -- Runtime elaboration check required. Generate check of the
855 -- elaboration Boolean for the unit containing the entity.
857 -- Note that for this case, we do check the real unit (the
858 -- one from following renamings, since that is the issue!)
860 -- Could this possibly miss a useless but required PE???
862 Insert_Elab_Check (N,
863 Make_Attribute_Reference (Loc,
864 Attribute_Name => Name_Elaborated,
865 Prefix =>
866 New_Occurrence_Of
867 (Spec_Entity (E_Scope), Loc)));
868 end if;
870 -- Case of static elaboration model
872 else
873 -- Do not do anything if elaboration checks suppressed. Note
874 -- that we check Ent here, not E, since we want the real entity
875 -- for the body to see if checks are suppressed for it, not the
876 -- dummy entry for renamings or derivations.
878 if Elaboration_Checks_Suppressed (Ent)
879 or else Elaboration_Checks_Suppressed (E_Scope)
880 or else Elaboration_Checks_Suppressed (W_Scope)
881 then
882 null;
884 -- Here we need to generate an implicit elaborate all
886 else
887 -- Generate elaborate_all warning unless suppressed
889 if (Elab_Warnings and Generate_Warnings and not Inst_Case)
890 and then not Suppress_Elaboration_Warnings (Ent)
891 and then not Suppress_Elaboration_Warnings (E_Scope)
892 and then not Suppress_Elaboration_Warnings (W_Scope)
893 then
894 Error_Msg_Node_2 := W_Scope;
895 Error_Msg_NE
896 ("call to& in elaboration code " &
897 "requires pragma Elaborate_All on&?", N, E);
898 end if;
900 -- Set indication for binder to generate Elaborate_All
902 Set_Elaboration_Constraint (N, E, W_Scope);
903 end if;
904 end if;
906 -- Case of entity is in same unit as call or instantiation
908 elsif not Inter_Unit_Only then
909 Check_Internal_Call (N, Ent, Outer_Scope, E);
910 end if;
911 end Check_A_Call;
913 -----------------------------
914 -- Check_Bad_Instantiation --
915 -----------------------------
917 procedure Check_Bad_Instantiation (N : Node_Id) is
918 Ent : Entity_Id;
920 begin
921 -- Nothing to do if we do not have an instantiation (happens in some
922 -- error cases, and also in the formal package declaration case)
924 if Nkind (N) not in N_Generic_Instantiation then
925 return;
927 -- Nothing to do if serious errors detected (avoid cascaded errors)
929 elsif Serious_Errors_Detected /= 0 then
930 return;
932 -- Nothing to do if not in full analysis mode
934 elsif not Full_Analysis then
935 return;
937 -- Nothing to do if inside a generic template
939 elsif Inside_A_Generic then
940 return;
942 -- Nothing to do if a library level instantiation
944 elsif Nkind (Parent (N)) = N_Compilation_Unit then
945 return;
947 -- Nothing to do if we are compiling a proper body for semantic
948 -- purposes only. The generic body may be in another proper body.
950 elsif
951 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
952 then
953 return;
954 end if;
956 Ent := Get_Generic_Entity (N);
958 -- The case we are interested in is when the generic spec is in the
959 -- current declarative part
961 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
962 or else not In_Same_Extended_Unit (N, Ent)
963 then
964 return;
965 end if;
967 -- If the generic entity is within a deeper instance than we are, then
968 -- either the instantiation to which we refer itself caused an ABE, in
969 -- which case that will be handled separately. Otherwise, we know that
970 -- the body we need appears as needed at the point of the instantiation.
971 -- If they are both at the same level but not within the same instance
972 -- then the body of the generic will be in the earlier instance.
974 declare
975 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
976 D2 : constant Int := Instantiation_Depth (Sloc (N));
978 begin
979 if D1 > D2 then
980 return;
982 elsif D1 = D2
983 and then Is_Generic_Instance (Scope (Ent))
984 and then not In_Open_Scopes (Scope (Ent))
985 then
986 return;
987 end if;
988 end;
990 -- Now we can proceed, if the entity being called has a completion,
991 -- then we are definitely OK, since we have already seen the body.
993 if Has_Completion (Ent) then
994 return;
995 end if;
997 -- If there is no body, then nothing to do
999 if not Has_Generic_Body (N) then
1000 return;
1001 end if;
1003 -- Here we definitely have a bad instantiation
1005 Error_Msg_NE
1006 ("?cannot instantiate& before body seen", N, Ent);
1008 if Present (Instance_Spec (N)) then
1009 Supply_Bodies (Instance_Spec (N));
1010 end if;
1012 Error_Msg_N
1013 ("\?Program_Error will be raised at run time", N);
1014 Insert_Elab_Check (N);
1015 Set_ABE_Is_Certain (N);
1016 end Check_Bad_Instantiation;
1018 ---------------------
1019 -- Check_Elab_Call --
1020 ---------------------
1022 procedure Check_Elab_Call
1023 (N : Node_Id;
1024 Outer_Scope : Entity_Id := Empty)
1026 Ent : Entity_Id;
1027 P : Node_Id;
1029 function Get_Called_Ent return Entity_Id;
1030 -- Retrieve called entity. If this is a call to a protected subprogram,
1031 -- entity is a selected component. The callable entity may be absent,
1032 -- in which case there is no check to perform. This happens with
1033 -- non-analyzed calls in nested generics.
1035 --------------------
1036 -- Get_Called_Ent --
1037 --------------------
1039 function Get_Called_Ent return Entity_Id is
1040 Nam : Node_Id;
1042 begin
1043 Nam := Name (N);
1045 if No (Nam) then
1046 return Empty;
1048 elsif Nkind (Nam) = N_Selected_Component then
1049 return Entity (Selector_Name (Nam));
1051 elsif not Is_Entity_Name (Nam) then
1052 return Empty;
1054 else
1055 return Entity (Nam);
1056 end if;
1057 end Get_Called_Ent;
1059 -- Start of processing for Check_Elab_Call
1061 begin
1062 -- If the call does not come from the main unit, there is nothing to
1063 -- check. Elaboration call from units in the context of the main unit
1064 -- will lead to semantic dependencies when those units are compiled.
1066 if not In_Extended_Main_Code_Unit (N) then
1067 return;
1068 end if;
1070 -- For an entry call, check relevant restriction
1072 if Nkind (N) = N_Entry_Call_Statement
1073 and then not In_Subprogram_Or_Concurrent_Unit
1074 then
1075 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
1077 -- Nothing to do if this is not a call (happens in some error
1078 -- conditions, and in some cases where rewriting occurs).
1080 elsif Nkind (N) /= N_Function_Call
1081 and then Nkind (N) /= N_Procedure_Call_Statement
1082 then
1083 return;
1085 -- Nothing to do if this is a call already rewritten for elab checking
1087 elsif Nkind (Parent (N)) = N_Conditional_Expression then
1088 return;
1090 -- Nothing to do if inside a generic template
1092 elsif Inside_A_Generic
1093 and then not Present (Enclosing_Generic_Body (N))
1094 then
1095 return;
1096 end if;
1098 -- Here we have a call at elaboration time which must be checked
1100 if Debug_Flag_LL then
1101 Write_Str (" Check_Elab_Call: ");
1103 if No (Name (N))
1104 or else not Is_Entity_Name (Name (N))
1105 then
1106 Write_Str ("<<not entity name>> ");
1107 else
1108 Write_Name (Chars (Entity (Name (N))));
1109 end if;
1111 Write_Str (" call at ");
1112 Write_Location (Sloc (N));
1113 Write_Eol;
1114 end if;
1116 -- Climb up the tree to make sure we are not inside a
1117 -- default expression of a parameter specification or
1118 -- a record component, since in both these cases, we
1119 -- will be doing the actual call later, not now, and it
1120 -- is at the time of the actual call (statically speaking)
1121 -- that we must do our static check, not at the time of
1122 -- its initial analysis). However, we have to check calls
1123 -- within component definitions (e.g., a function call
1124 -- that determines an array component bound), so we
1125 -- terminate the loop in that case.
1127 P := Parent (N);
1128 while Present (P) loop
1129 if Nkind (P) = N_Parameter_Specification
1130 or else
1131 Nkind (P) = N_Component_Declaration
1132 then
1133 return;
1135 -- The call occurs within the constraint of a component,
1136 -- so it must be checked.
1138 elsif Nkind (P) = N_Component_Definition then
1139 exit;
1141 else
1142 P := Parent (P);
1143 end if;
1144 end loop;
1146 -- Stuff that happens only at the outer level
1148 if No (Outer_Scope) then
1149 Elab_Visited.Set_Last (0);
1151 -- Nothing to do if current scope is Standard (this is a bit
1152 -- odd, but it happens in the case of generic instantiations).
1154 C_Scope := Current_Scope;
1156 if C_Scope = Standard_Standard then
1157 return;
1158 end if;
1160 -- First case, we are in elaboration code
1162 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1163 if From_Elab_Code then
1165 -- Complain if call that comes from source in preelaborated
1166 -- unit and we are not inside a subprogram (i.e. we are in
1167 -- elab code)
1169 if Comes_From_Source (N)
1170 and then In_Preelaborated_Unit
1171 and then not In_Inlined_Body
1172 then
1173 -- This is a warning in GNAT mode allowing such calls to be
1174 -- used in the predefined library with appropriate care.
1176 Error_Msg_Warn := GNAT_Mode;
1177 Error_Msg_N
1178 ("<non-static call not allowed in preelaborated unit", N);
1179 return;
1180 end if;
1182 -- Second case, we are inside a subprogram or concurrent unit, which
1183 -- means we are not in elaboration code.
1185 else
1186 -- In this case, the issue is whether we are inside the
1187 -- declarative part of the unit in which we live, or inside its
1188 -- statements. In the latter case, there is no issue of ABE calls
1189 -- at this level (a call from outside to the unit in which we live
1190 -- might cause an ABE, but that will be detected when we analyze
1191 -- that outer level call, as it recurses into the called unit).
1193 -- Climb up the tree, doing this test, and also testing for being
1194 -- inside a default expression, which, as discussed above, is not
1195 -- checked at this stage.
1197 declare
1198 P : Node_Id;
1199 L : List_Id;
1201 begin
1202 P := N;
1203 loop
1204 -- If we find a parentless subtree, it seems safe to assume
1205 -- that we are not in a declarative part and that no
1206 -- checking is required.
1208 if No (P) then
1209 return;
1210 end if;
1212 if Is_List_Member (P) then
1213 L := List_Containing (P);
1214 P := Parent (L);
1215 else
1216 L := No_List;
1217 P := Parent (P);
1218 end if;
1220 exit when Nkind (P) = N_Subunit;
1222 -- Filter out case of default expressions, where we do not
1223 -- do the check at this stage.
1225 if Nkind (P) = N_Parameter_Specification
1226 or else
1227 Nkind (P) = N_Component_Declaration
1228 then
1229 return;
1230 end if;
1232 -- A protected body has no elaboration code and contains
1233 -- only other bodies.
1235 if Nkind (P) = N_Protected_Body then
1236 return;
1238 elsif Nkind (P) = N_Subprogram_Body
1239 or else
1240 Nkind (P) = N_Task_Body
1241 or else
1242 Nkind (P) = N_Block_Statement
1243 or else
1244 Nkind (P) = N_Entry_Body
1245 then
1246 if L = Declarations (P) then
1247 exit;
1249 -- We are not in elaboration code, but we are doing
1250 -- dynamic elaboration checks, in this case, we still
1251 -- need to do the call, since the subprogram we are in
1252 -- could be called from another unit, also in dynamic
1253 -- elaboration check mode, at elaboration time.
1255 elsif Dynamic_Elaboration_Checks then
1257 -- This is a rather new check, going into version
1258 -- 3.14a1 for the first time (V1.80 of this unit), so
1259 -- we provide a debug flag to enable it. That way we
1260 -- have an easy work around for regressions that are
1261 -- caused by this new check. This debug flag can be
1262 -- removed later.
1264 if Debug_Flag_DD then
1265 return;
1266 end if;
1268 -- Do the check in this case
1270 exit;
1272 elsif Nkind (P) = N_Task_Body then
1274 -- The check is deferred until Check_Task_Activation
1275 -- but we need to capture local suppress pragmas
1276 -- that may inhibit checks on this call.
1278 Ent := Get_Called_Ent;
1280 if No (Ent) then
1281 return;
1283 elsif Elaboration_Checks_Suppressed (Current_Scope)
1284 or else Elaboration_Checks_Suppressed (Ent)
1285 or else Elaboration_Checks_Suppressed (Scope (Ent))
1286 then
1287 Set_No_Elaboration_Check (N);
1288 end if;
1290 return;
1292 -- Static model, call is not in elaboration code, we
1293 -- never need to worry, because in the static model
1294 -- the top level caller always takes care of things.
1296 else
1297 return;
1298 end if;
1299 end if;
1300 end loop;
1301 end;
1302 end if;
1303 end if;
1305 Ent := Get_Called_Ent;
1307 if No (Ent) then
1308 return;
1309 end if;
1311 -- Nothing to do if this is a recursive call (i.e. a call to
1312 -- an entity that is already in the Elab_Call stack)
1314 for J in 1 .. Elab_Visited.Last loop
1315 if Ent = Elab_Visited.Table (J) then
1316 return;
1317 end if;
1318 end loop;
1320 -- See if we need to analyze this call. We analyze it if either of
1321 -- the following conditions is met:
1323 -- It is an inner level call (since in this case it was triggered
1324 -- by an outer level call from elaboration code), but only if the
1325 -- call is within the scope of the original outer level call.
1327 -- It is an outer level call from elaboration code, or the called
1328 -- entity is in the same elaboration scope.
1330 -- And in these cases, we will check both inter-unit calls and
1331 -- intra-unit (within a single unit) calls.
1333 C_Scope := Current_Scope;
1335 -- If not outer level call, then we follow it if it is within
1336 -- the original scope of the outer call.
1338 if Present (Outer_Scope)
1339 and then Within (Scope (Ent), Outer_Scope)
1340 then
1341 Set_C_Scope;
1342 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1344 elsif Elaboration_Checks_Suppressed (Current_Scope) then
1345 null;
1347 elsif From_Elab_Code then
1348 Set_C_Scope;
1349 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1351 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1352 Set_C_Scope;
1353 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1355 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1356 -- is set, then we will do the check, but only in the inter-unit case
1357 -- (this is to accommodate unguarded elaboration calls from other units
1358 -- in which this same mode is set). We don't want warnings in this case,
1359 -- it would generate warnings having nothing to do with elaboration.
1361 elsif Dynamic_Elaboration_Checks then
1362 Set_C_Scope;
1363 Check_A_Call
1365 Ent,
1366 Standard_Standard,
1367 Inter_Unit_Only => True,
1368 Generate_Warnings => False);
1370 -- Otherwise nothing to do
1372 else
1373 return;
1374 end if;
1376 -- A call to an Init_Proc in elaboration code may bring additional
1377 -- dependencies, if some of the record components thereof have
1378 -- initializations that are function calls that come from source.
1379 -- We treat the current node as a call to each of these functions,
1380 -- to check their elaboration impact.
1382 if Is_Init_Proc (Ent)
1383 and then From_Elab_Code
1384 then
1385 Process_Init_Proc : declare
1386 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1388 function Process (Nod : Node_Id) return Traverse_Result;
1389 -- Find subprogram calls within body of init_proc for
1390 -- Traverse instantiation below.
1392 function Process (Nod : Node_Id) return Traverse_Result is
1393 Func : Entity_Id;
1395 begin
1396 if (Nkind (Nod) = N_Function_Call
1397 or else Nkind (Nod) = N_Procedure_Call_Statement)
1398 and then Is_Entity_Name (Name (Nod))
1399 then
1400 Func := Entity (Name (Nod));
1402 if Comes_From_Source (Func) then
1403 Check_A_Call
1404 (N, Func, Standard_Standard, Inter_Unit_Only => True);
1405 end if;
1407 return OK;
1409 else
1410 return OK;
1411 end if;
1412 end Process;
1414 procedure Traverse_Body is new Traverse_Proc (Process);
1416 -- Start of processing for Process_Init_Proc
1418 begin
1419 if Nkind (Unit_Decl) = N_Subprogram_Body then
1420 Traverse_Body (Handled_Statement_Sequence (Unit_Decl));
1421 end if;
1422 end Process_Init_Proc;
1423 end if;
1424 end Check_Elab_Call;
1426 ----------------------
1427 -- Check_Elab_Calls --
1428 ----------------------
1430 procedure Check_Elab_Calls is
1431 begin
1432 -- If expansion is disabled, do not generate any checks. Also
1433 -- skip checks if any subunits are missing because in either
1434 -- case we lack the full information that we need, and no object
1435 -- file will be created in any case.
1437 if not Expander_Active
1438 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
1439 or else Subunits_Missing
1440 then
1441 return;
1442 end if;
1444 -- Skip delayed calls if we had any errors
1446 if Serious_Errors_Detected = 0 then
1447 Delaying_Elab_Checks := False;
1448 Expander_Mode_Save_And_Set (True);
1450 for J in Delay_Check.First .. Delay_Check.Last loop
1451 New_Scope (Delay_Check.Table (J).Curscop);
1452 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1454 Check_Internal_Call_Continue (
1455 N => Delay_Check.Table (J).N,
1456 E => Delay_Check.Table (J).E,
1457 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1458 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1460 Pop_Scope;
1461 end loop;
1463 -- Set Delaying_Elab_Checks back on for next main compilation
1465 Expander_Mode_Restore;
1466 Delaying_Elab_Checks := True;
1467 end if;
1468 end Check_Elab_Calls;
1470 ------------------------------
1471 -- Check_Elab_Instantiation --
1472 ------------------------------
1474 procedure Check_Elab_Instantiation
1475 (N : Node_Id;
1476 Outer_Scope : Entity_Id := Empty)
1478 Ent : Entity_Id;
1480 begin
1481 -- Check for and deal with bad instantiation case. There is some
1482 -- duplicated code here, but we will worry about this later ???
1484 Check_Bad_Instantiation (N);
1486 if ABE_Is_Certain (N) then
1487 return;
1488 end if;
1490 -- Nothing to do if we do not have an instantiation (happens in some
1491 -- error cases, and also in the formal package declaration case)
1493 if Nkind (N) not in N_Generic_Instantiation then
1494 return;
1495 end if;
1497 -- Nothing to do if inside a generic template
1499 if Inside_A_Generic then
1500 return;
1501 end if;
1503 -- Nothing to do if the instantiation is not in the main unit
1505 if not In_Extended_Main_Code_Unit (N) then
1506 return;
1507 end if;
1509 Ent := Get_Generic_Entity (N);
1510 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1512 -- See if we need to analyze this instantiation. We analyze it if
1513 -- either of the following conditions is met:
1515 -- It is an inner level instantiation (since in this case it was
1516 -- triggered by an outer level call from elaboration code), but
1517 -- only if the instantiation is within the scope of the original
1518 -- outer level call.
1520 -- It is an outer level instantiation from elaboration code, or the
1521 -- instantiated entity is in the same elaboratoin scope.
1523 -- And in these cases, we will check both the inter-unit case and
1524 -- the intra-unit (within a single unit) case.
1526 C_Scope := Current_Scope;
1528 if Present (Outer_Scope)
1529 and then Within (Scope (Ent), Outer_Scope)
1530 then
1531 Set_C_Scope;
1532 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1534 elsif From_Elab_Code then
1535 Set_C_Scope;
1536 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1538 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1539 Set_C_Scope;
1540 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1542 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1543 -- is set, then we will do the check, but only in the inter-unit case
1544 -- (this is to accommodate unguarded elaboration calls from other units
1545 -- in which this same mode is set). We inhibit warnings in this case,
1546 -- since this instantiation is not occurring in elaboration code.
1548 elsif Dynamic_Elaboration_Checks then
1549 Set_C_Scope;
1550 Check_A_Call
1552 Ent,
1553 Standard_Standard,
1554 Inter_Unit_Only => True,
1555 Generate_Warnings => False);
1557 else
1558 return;
1559 end if;
1560 end Check_Elab_Instantiation;
1562 -------------------------
1563 -- Check_Internal_Call --
1564 -------------------------
1566 procedure Check_Internal_Call
1567 (N : Node_Id;
1568 E : Entity_Id;
1569 Outer_Scope : Entity_Id;
1570 Orig_Ent : Entity_Id)
1572 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1574 begin
1575 -- If not function or procedure call or instantiation, then ignore
1576 -- call (this happens in some error case and rewriting cases)
1578 if Nkind (N) /= N_Function_Call
1579 and then
1580 Nkind (N) /= N_Procedure_Call_Statement
1581 and then
1582 not Inst_Case
1583 then
1584 return;
1586 -- Nothing to do if this is a call or instantiation that has
1587 -- already been found to be a sure ABE
1589 elsif ABE_Is_Certain (N) then
1590 return;
1592 -- Nothing to do if errors already detected (avoid cascaded errors)
1594 elsif Serious_Errors_Detected /= 0 then
1595 return;
1597 -- Nothing to do if not in full analysis mode
1599 elsif not Full_Analysis then
1600 return;
1602 -- Nothing to do if within a default expression, since the call
1603 -- is not actualy being made at this time.
1605 elsif In_Default_Expression then
1606 return;
1608 -- Nothing to do for call to intrinsic subprogram
1610 elsif Is_Intrinsic_Subprogram (E) then
1611 return;
1613 -- No need to trace local calls if checking task activation, because
1614 -- other local bodies are elaborated already.
1616 elsif In_Task_Activation then
1617 return;
1618 end if;
1620 -- Delay this call if we are still delaying calls
1622 if Delaying_Elab_Checks then
1623 Delay_Check.Increment_Last;
1624 Delay_Check.Table (Delay_Check.Last) :=
1625 (N => N,
1626 E => E,
1627 Orig_Ent => Orig_Ent,
1628 Curscop => Current_Scope,
1629 Outer_Scope => Outer_Scope,
1630 From_Elab_Code => From_Elab_Code);
1631 return;
1633 -- Otherwise, call phase 2 continuation right now
1635 else
1636 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1637 end if;
1638 end Check_Internal_Call;
1640 ----------------------------------
1641 -- Check_Internal_Call_Continue --
1642 ----------------------------------
1644 procedure Check_Internal_Call_Continue
1645 (N : Node_Id;
1646 E : Entity_Id;
1647 Outer_Scope : Entity_Id;
1648 Orig_Ent : Entity_Id)
1650 Loc : constant Source_Ptr := Sloc (N);
1651 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1653 Sbody : Node_Id;
1654 Ebody : Entity_Id;
1656 function Process (N : Node_Id) return Traverse_Result;
1657 -- Function applied to each node as we traverse the body.
1658 -- Checks for call that needs checking, and if so checks
1659 -- it. Always returns OK, so entire tree is traversed.
1661 -------------
1662 -- Process --
1663 -------------
1665 function Process (N : Node_Id) return Traverse_Result is
1666 begin
1667 -- If user has specified that there are no entry calls in elaboration
1668 -- code, do not trace past an accept statement, because the rendez-
1669 -- vous will happen after elaboration.
1671 if (Nkind (Original_Node (N)) = N_Accept_Statement
1672 or else Nkind (Original_Node (N)) = N_Selective_Accept)
1673 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
1674 then
1675 return Abandon;
1677 -- If we have a subprogram call, check it
1679 elsif Nkind (N) = N_Function_Call
1680 or else Nkind (N) = N_Procedure_Call_Statement
1681 then
1682 Check_Elab_Call (N, Outer_Scope);
1683 return OK;
1685 -- If we have a generic instantiation, check it
1687 elsif Nkind (N) in N_Generic_Instantiation then
1688 Check_Elab_Instantiation (N, Outer_Scope);
1689 return OK;
1691 -- Skip subprogram bodies that come from source (wait for
1692 -- call to analyze these). The reason for the come from
1693 -- source test is to avoid catching task bodies.
1695 -- For task bodies, we should really avoid these too, waiting
1696 -- for the task activation, but that's too much trouble to
1697 -- catch for now, so we go in unconditionally. This is not
1698 -- so terrible, it means the error backtrace is not quite
1699 -- complete, and we are too eager to scan bodies of tasks
1700 -- that are unused, but this is hardly very significant!
1702 elsif Nkind (N) = N_Subprogram_Body
1703 and then Comes_From_Source (N)
1704 then
1705 return Skip;
1707 else
1708 return OK;
1709 end if;
1710 end Process;
1712 procedure Traverse is new Atree.Traverse_Proc;
1713 -- Traverse procedure using above Process function
1715 -- Start of processing for Check_Internal_Call_Continue
1717 begin
1718 -- Save outer level call if at outer level
1720 if Elab_Call.Last = 0 then
1721 Outer_Level_Sloc := Loc;
1722 end if;
1724 Elab_Visited.Increment_Last;
1725 Elab_Visited.Table (Elab_Visited.Last) := E;
1727 -- If the call is to a function that renames a literal, no check
1728 -- is needed.
1730 if Ekind (E) = E_Enumeration_Literal then
1731 return;
1732 end if;
1734 Sbody := Unit_Declaration_Node (E);
1736 if Nkind (Sbody) /= N_Subprogram_Body
1737 and then
1738 Nkind (Sbody) /= N_Package_Body
1739 then
1740 Ebody := Corresponding_Body (Sbody);
1742 if No (Ebody) then
1743 return;
1744 else
1745 Sbody := Unit_Declaration_Node (Ebody);
1746 end if;
1747 end if;
1749 -- If the body appears after the outer level call or
1750 -- instantiation then we have an error case handled below.
1752 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
1753 and then not In_Task_Activation
1754 then
1755 null;
1757 -- If we have the instantiation case we are done, since we now
1758 -- know that the body of the generic appeared earlier.
1760 elsif Inst_Case then
1761 return;
1763 -- Otherwise we have a call, so we trace through the called
1764 -- body to see if it has any problems ..
1766 else
1767 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
1769 Elab_Call.Increment_Last;
1770 Elab_Call.Table (Elab_Call.Last).Cloc := Loc;
1771 Elab_Call.Table (Elab_Call.Last).Ent := E;
1773 if Debug_Flag_LL then
1774 Write_Str ("Elab_Call.Last = ");
1775 Write_Int (Int (Elab_Call.Last));
1776 Write_Str (" Ent = ");
1777 Write_Name (Chars (E));
1778 Write_Str (" at ");
1779 Write_Location (Sloc (N));
1780 Write_Eol;
1781 end if;
1783 -- Now traverse declarations and statements of subprogram body.
1784 -- Note that we cannot simply Traverse (Sbody), since traverse
1785 -- does not normally visit subprogram bodies.
1787 declare
1788 Decl : Node_Id;
1789 begin
1790 Decl := First (Declarations (Sbody));
1791 while Present (Decl) loop
1792 Traverse (Decl);
1793 Next (Decl);
1794 end loop;
1795 end;
1797 Traverse (Handled_Statement_Sequence (Sbody));
1799 Elab_Call.Decrement_Last;
1800 return;
1801 end if;
1803 -- Here is the case of calling a subprogram where the body has
1804 -- not yet been encountered, a warning message is needed.
1806 -- If we have nothing in the call stack, then this is at the
1807 -- outer level, and the ABE is bound to occur.
1809 if Elab_Call.Last = 0 then
1810 if Inst_Case then
1811 Error_Msg_NE
1812 ("?cannot instantiate& before body seen", N, Orig_Ent);
1813 else
1814 Error_Msg_NE
1815 ("?cannot call& before body seen", N, Orig_Ent);
1816 end if;
1818 Error_Msg_N
1819 ("\?Program_Error will be raised at run time", N);
1820 Insert_Elab_Check (N);
1822 -- Call is not at outer level
1824 else
1825 -- Deal with dynamic elaboration check
1827 if not Elaboration_Checks_Suppressed (E) then
1828 Set_Elaboration_Entity_Required (E);
1830 -- Case of no elaboration entity allocated yet
1832 if No (Elaboration_Entity (E)) then
1834 -- Create object declaration for elaboration entity, and put it
1835 -- just in front of the spec of the subprogram or generic unit,
1836 -- in the same scope as this unit.
1838 declare
1839 Loce : constant Source_Ptr := Sloc (E);
1840 Ent : constant Entity_Id :=
1841 Make_Defining_Identifier (Loc,
1842 Chars => New_External_Name (Chars (E), 'E'));
1844 begin
1845 Set_Elaboration_Entity (E, Ent);
1846 New_Scope (Scope (E));
1848 Insert_Action (Declaration_Node (E),
1849 Make_Object_Declaration (Loce,
1850 Defining_Identifier => Ent,
1851 Object_Definition =>
1852 New_Occurrence_Of (Standard_Boolean, Loce),
1853 Expression => New_Occurrence_Of (Standard_False, Loce)));
1855 -- Set elaboration flag at the point of the body
1857 Set_Elaboration_Flag (Sbody, E);
1859 -- Kill current value indication. This is necessary
1860 -- because the tests of this flag are inserted out of
1861 -- sequence and must not pick up bogus indications of
1862 -- the wrong constant value. Also, this is never a true
1863 -- constant, since one way or another, it gets reset.
1865 Set_Current_Value (Ent, Empty);
1866 Set_Is_True_Constant (Ent, False);
1867 Pop_Scope;
1868 end;
1869 end if;
1871 -- Generate check of the elaboration Boolean
1873 Insert_Elab_Check (N,
1874 New_Occurrence_Of (Elaboration_Entity (E), Loc));
1875 end if;
1877 -- Generate the warning
1879 if not Suppress_Elaboration_Warnings (E)
1880 and then not Elaboration_Checks_Suppressed (E)
1881 then
1882 if Inst_Case then
1883 Error_Msg_NE
1884 ("instantiation of& may occur before body is seen?",
1885 N, Orig_Ent);
1886 else
1887 Error_Msg_NE
1888 ("call to& may occur before body is seen?", N, Orig_Ent);
1889 end if;
1891 Error_Msg_N
1892 ("\Program_Error may be raised at run time?", N);
1894 Output_Calls (N);
1895 end if;
1896 end if;
1898 -- Set flag to suppress further warnings on same subprogram
1899 -- unless in all errors mode
1901 if not All_Errors_Mode then
1902 Set_Suppress_Elaboration_Warnings (E);
1903 end if;
1904 end Check_Internal_Call_Continue;
1906 ---------------------------
1907 -- Check_Task_Activation --
1908 ---------------------------
1910 procedure Check_Task_Activation (N : Node_Id) is
1911 Loc : constant Source_Ptr := Sloc (N);
1912 Inter_Procs : constant Elist_Id := New_Elmt_List;
1913 Intra_Procs : constant Elist_Id := New_Elmt_List;
1914 Ent : Entity_Id;
1915 P : Entity_Id;
1916 Task_Scope : Entity_Id;
1917 Cunit_SC : Boolean := False;
1918 Decl : Node_Id;
1919 Elmt : Elmt_Id;
1920 Enclosing : Entity_Id;
1922 procedure Add_Task_Proc (Typ : Entity_Id);
1923 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
1924 -- For record types, this procedure recurses over component types.
1926 procedure Collect_Tasks (Decls : List_Id);
1927 -- Collect the types of the tasks that are to be activated in the given
1928 -- list of declarations, in order to perform elaboration checks on the
1929 -- corresponding task procedures which are called implicitly here.
1931 function Outer_Unit (E : Entity_Id) return Entity_Id;
1932 -- find enclosing compilation unit of Entity, ignoring subunits, or
1933 -- else enclosing subprogram. If E is not a package, there is no need
1934 -- for inter-unit elaboration checks.
1936 -------------------
1937 -- Add_Task_Proc --
1938 -------------------
1940 procedure Add_Task_Proc (Typ : Entity_Id) is
1941 Comp : Entity_Id;
1942 Proc : Entity_Id := Empty;
1944 begin
1945 if Is_Task_Type (Typ) then
1946 Proc := Get_Task_Body_Procedure (Typ);
1948 elsif Is_Array_Type (Typ)
1949 and then Has_Task (Base_Type (Typ))
1950 then
1951 Add_Task_Proc (Component_Type (Typ));
1953 elsif Is_Record_Type (Typ)
1954 and then Has_Task (Base_Type (Typ))
1955 then
1956 Comp := First_Component (Typ);
1957 while Present (Comp) loop
1958 Add_Task_Proc (Etype (Comp));
1959 Comp := Next_Component (Comp);
1960 end loop;
1961 end if;
1963 -- If the task type is another unit, we will perform the usual
1964 -- elaboration check on its enclosing unit. If the type is in the
1965 -- same unit, we can trace the task body as for an internal call,
1966 -- but we only need to examine other external calls, because at
1967 -- the point the task is activated, internal subprogram bodies
1968 -- will have been elaborated already. We keep separate lists for
1969 -- each kind of task.
1971 -- Skip this test if errors have occurred, since in this case
1972 -- we can get false indications.
1974 if Serious_Errors_Detected /= 0 then
1975 return;
1976 end if;
1978 if Present (Proc) then
1979 if Outer_Unit (Scope (Proc)) = Enclosing then
1981 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
1982 and then
1983 (not Is_Generic_Instance (Scope (Proc))
1984 or else
1985 Scope (Proc) = Scope (Defining_Identifier (Decl)))
1986 then
1987 Error_Msg_N
1988 ("task will be activated before elaboration of its body?",
1989 Decl);
1990 Error_Msg_N
1991 ("Program_Error will be raised at run-time?", Decl);
1993 elsif
1994 Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
1995 then
1996 Append_Elmt (Proc, Intra_Procs);
1997 end if;
1999 else
2000 -- No need for multiple entries of the same type
2002 Elmt := First_Elmt (Inter_Procs);
2003 while Present (Elmt) loop
2004 if Node (Elmt) = Proc then
2005 return;
2006 end if;
2008 Next_Elmt (Elmt);
2009 end loop;
2011 Append_Elmt (Proc, Inter_Procs);
2012 end if;
2013 end if;
2014 end Add_Task_Proc;
2016 -------------------
2017 -- Collect_Tasks --
2018 -------------------
2020 procedure Collect_Tasks (Decls : List_Id) is
2021 begin
2022 if Present (Decls) then
2023 Decl := First (Decls);
2024 while Present (Decl) loop
2025 if Nkind (Decl) = N_Object_Declaration
2026 and then Has_Task (Etype (Defining_Identifier (Decl)))
2027 then
2028 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
2029 end if;
2031 Next (Decl);
2032 end loop;
2033 end if;
2034 end Collect_Tasks;
2036 ----------------
2037 -- Outer_Unit --
2038 ----------------
2040 function Outer_Unit (E : Entity_Id) return Entity_Id is
2041 Outer : Entity_Id;
2043 begin
2044 Outer := E;
2045 while Present (Outer) loop
2046 if Elaboration_Checks_Suppressed (Outer) then
2047 Cunit_SC := True;
2048 end if;
2050 exit when Is_Child_Unit (Outer)
2051 or else Scope (Outer) = Standard_Standard
2052 or else Ekind (Outer) /= E_Package;
2053 Outer := Scope (Outer);
2054 end loop;
2056 return Outer;
2057 end Outer_Unit;
2059 -- Start of processing for Check_Task_Activation
2061 begin
2062 Enclosing := Outer_Unit (Current_Scope);
2064 -- Find all tasks declared in the current unit
2066 if Nkind (N) = N_Package_Body then
2067 P := Unit_Declaration_Node (Corresponding_Spec (N));
2069 Collect_Tasks (Declarations (N));
2070 Collect_Tasks (Visible_Declarations (Specification (P)));
2071 Collect_Tasks (Private_Declarations (Specification (P)));
2073 elsif Nkind (N) = N_Package_Declaration then
2074 Collect_Tasks (Visible_Declarations (Specification (N)));
2075 Collect_Tasks (Private_Declarations (Specification (N)));
2077 else
2078 Collect_Tasks (Declarations (N));
2079 end if;
2081 -- We only perform detailed checks in all tasks are library level
2082 -- entities. If the master is a subprogram or task, activation will
2083 -- depend on the activation of the master itself.
2084 -- Should dynamic checks be added in the more general case???
2086 if Ekind (Enclosing) /= E_Package then
2087 return;
2088 end if;
2090 -- For task types defined in other units, we want the unit containing
2091 -- the task body to be elaborated before the current one.
2093 Elmt := First_Elmt (Inter_Procs);
2094 while Present (Elmt) loop
2095 Ent := Node (Elmt);
2096 Task_Scope := Outer_Unit (Scope (Ent));
2098 if not Is_Compilation_Unit (Task_Scope) then
2099 null;
2101 elsif Suppress_Elaboration_Warnings (Task_Scope)
2102 or else Elaboration_Checks_Suppressed (Task_Scope)
2103 then
2104 null;
2106 elsif Dynamic_Elaboration_Checks then
2107 if not Elaboration_Checks_Suppressed (Ent)
2108 and then not Cunit_SC
2109 and then
2110 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code)
2111 then
2112 -- Runtime elaboration check required. generate check of the
2113 -- elaboration Boolean for the unit containing the entity.
2115 Insert_Elab_Check (N,
2116 Make_Attribute_Reference (Loc,
2117 Attribute_Name => Name_Elaborated,
2118 Prefix =>
2119 New_Occurrence_Of
2120 (Spec_Entity (Task_Scope), Loc)));
2121 end if;
2123 else
2124 -- Force the binder to elaborate other unit first
2126 if not Suppress_Elaboration_Warnings (Ent)
2127 and then not Elaboration_Checks_Suppressed (Ent)
2128 and then Elab_Warnings
2129 and then not Suppress_Elaboration_Warnings (Task_Scope)
2130 and then not Elaboration_Checks_Suppressed (Task_Scope)
2131 then
2132 Error_Msg_Node_2 := Task_Scope;
2133 Error_Msg_NE ("activation of an instance of task type&" &
2134 " requires pragma Elaborate_All on &?", N, Ent);
2135 end if;
2137 Activate_Elaborate_All_Desirable (N, Task_Scope);
2138 Set_Suppress_Elaboration_Warnings (Task_Scope);
2139 end if;
2141 Next_Elmt (Elmt);
2142 end loop;
2144 -- For tasks declared in the current unit, trace other calls within
2145 -- the task procedure bodies, which are available.
2147 In_Task_Activation := True;
2149 Elmt := First_Elmt (Intra_Procs);
2150 while Present (Elmt) loop
2151 Ent := Node (Elmt);
2152 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
2153 Next_Elmt (Elmt);
2154 end loop;
2156 In_Task_Activation := False;
2157 end Check_Task_Activation;
2159 --------------------------------
2160 -- Set_Elaboration_Constraint --
2161 --------------------------------
2163 procedure Set_Elaboration_Constraint
2164 (Call : Node_Id;
2165 Subp : Entity_Id;
2166 Scop : Entity_Id)
2168 Elab_Unit : Entity_Id;
2169 Init_Call : constant Boolean :=
2170 Chars (Subp) = Name_Initialize
2171 and then Comes_From_Source (Subp)
2172 and then Present (Parameter_Associations (Call))
2173 and then Is_Controlled
2174 (Etype (First (Parameter_Associations (Call))));
2175 begin
2176 -- If the unit is mentioned in a with_clause of the current
2177 -- unit, it is visible, and we can set the elaboration flag.
2179 if Is_Immediately_Visible (Scop)
2180 or else
2181 (Is_Child_Unit (Scop) and then Is_Visible_Child_Unit (Scop))
2182 then
2183 Activate_Elaborate_All_Desirable (Call, Scop);
2184 Set_Suppress_Elaboration_Warnings (Scop, True);
2185 return;
2186 end if;
2188 -- If this is not an initialization call or a call using object notation
2189 -- we know that the unit of the called entity is in the context, and
2190 -- we can set the flag as well. The unit need not be visible if the call
2191 -- occurs within an instantiation.
2193 if Is_Init_Proc (Subp)
2194 or else Init_Call
2195 or else Nkind (Original_Node (Call)) = N_Selected_Component
2196 then
2197 null; -- detailed processing follows.
2199 else
2200 Activate_Elaborate_All_Desirable (Call, Scop);
2201 Set_Suppress_Elaboration_Warnings (Scop, True);
2202 return;
2203 end if;
2205 -- If the unit is not in the context, there must be an intermediate
2206 -- unit that is, on which we need to place to elaboration flag. This
2207 -- happens with init proc calls.
2209 if Is_Init_Proc (Subp)
2210 or else Init_Call
2211 then
2212 -- The initialization call is on an object whose type is not
2213 -- declared in the same scope as the subprogram. The type of
2214 -- the object must be a subtype of the type of operation. This
2215 -- object is the first actual in the call.
2217 declare
2218 Typ : constant Entity_Id :=
2219 Etype (First (Parameter_Associations (Call)));
2220 begin
2221 Elab_Unit := Scope (Typ);
2222 while (Present (Elab_Unit))
2223 and then not Is_Compilation_Unit (Elab_Unit)
2224 loop
2225 Elab_Unit := Scope (Elab_Unit);
2226 end loop;
2227 end;
2229 -- If original node uses selected component notation, the prefix is
2230 -- visible and determines the scope that must be elaborated. After
2231 -- rewriting, the prefix is the first actual in the call.
2233 elsif Nkind (Original_Node (Call)) = N_Selected_Component then
2234 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call))));
2236 -- Not one of special cases above
2238 else
2239 -- Using previously computed scope. If the elaboration check is
2240 -- done after analysis, the scope is not visible any longer, but
2241 -- must still be in the context.
2243 Elab_Unit := Scop;
2244 end if;
2246 Activate_Elaborate_All_Desirable (Call, Elab_Unit);
2247 Set_Suppress_Elaboration_Warnings (Elab_Unit, True);
2248 end Set_Elaboration_Constraint;
2250 ----------------------
2251 -- Has_Generic_Body --
2252 ----------------------
2254 function Has_Generic_Body (N : Node_Id) return Boolean is
2255 Ent : constant Entity_Id := Get_Generic_Entity (N);
2256 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
2257 Scop : Entity_Id;
2259 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
2260 -- Determine if the list of nodes headed by N and linked by Next
2261 -- contains a package body for the package spec entity E, and if
2262 -- so return the package body. If not, then returns Empty.
2264 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
2265 -- This procedure is called load the unit whose name is given by Nam.
2266 -- This unit is being loaded to see whether it contains an optional
2267 -- generic body. The returned value is the loaded unit, which is
2268 -- always a package body (only package bodies can contain other
2269 -- entities in the sense in which Has_Generic_Body is interested).
2270 -- We only attempt to load bodies if we are generating code. If we
2271 -- are in semantics check only mode, then it would be wrong to load
2272 -- bodies that are not required from a semantic point of view, so
2273 -- in this case we return Empty. The result is that the caller may
2274 -- incorrectly decide that a generic spec does not have a body when
2275 -- in fact it does, but the only harm in this is that some warnings
2276 -- on elaboration problems may be lost in semantic checks only mode,
2277 -- which is not big loss. We also return Empty if we go for a body
2278 -- and it is not there.
2280 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
2281 -- PE is the entity for a package spec. This function locates the
2282 -- corresponding package body, returning Empty if none is found.
2283 -- The package body returned is fully parsed but may not yet be
2284 -- analyzed, so only syntactic fields should be referenced.
2286 ------------------
2287 -- Find_Body_In --
2288 ------------------
2290 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
2291 Nod : Node_Id;
2293 begin
2294 Nod := N;
2295 while Present (Nod) loop
2297 -- If we found the package body we are looking for, return it
2299 if Nkind (Nod) = N_Package_Body
2300 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
2301 then
2302 return Nod;
2304 -- If we found the stub for the body, go after the subunit,
2305 -- loading it if necessary.
2307 elsif Nkind (Nod) = N_Package_Body_Stub
2308 and then Chars (Defining_Identifier (Nod)) = Chars (E)
2309 then
2310 if Present (Library_Unit (Nod)) then
2311 return Unit (Library_Unit (Nod));
2313 else
2314 return Load_Package_Body (Get_Unit_Name (Nod));
2315 end if;
2317 -- If neither package body nor stub, keep looking on chain
2319 else
2320 Next (Nod);
2321 end if;
2322 end loop;
2324 return Empty;
2325 end Find_Body_In;
2327 -----------------------
2328 -- Load_Package_Body --
2329 -----------------------
2331 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
2332 U : Unit_Number_Type;
2334 begin
2335 if Operating_Mode /= Generate_Code then
2336 return Empty;
2337 else
2338 U :=
2339 Load_Unit
2340 (Load_Name => Nam,
2341 Required => False,
2342 Subunit => False,
2343 Error_Node => N);
2345 if U = No_Unit then
2346 return Empty;
2347 else
2348 return Unit (Cunit (U));
2349 end if;
2350 end if;
2351 end Load_Package_Body;
2353 -------------------------------
2354 -- Locate_Corresponding_Body --
2355 -------------------------------
2357 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
2358 Spec : constant Node_Id := Declaration_Node (PE);
2359 Decl : constant Node_Id := Parent (Spec);
2360 Scop : constant Entity_Id := Scope (PE);
2361 PBody : Node_Id;
2363 begin
2364 if Is_Library_Level_Entity (PE) then
2366 -- If package is a library unit that requires a body, we have
2367 -- no choice but to go after that body because it might contain
2368 -- an optional body for the original generic package.
2370 if Unit_Requires_Body (PE) then
2372 -- Load the body. Note that we are a little careful here to
2373 -- use Spec to get the unit number, rather than PE or Decl,
2374 -- since in the case where the package is itself a library
2375 -- level instantiation, Spec will properly reference the
2376 -- generic template, which is what we really want.
2378 return
2379 Load_Package_Body
2380 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
2382 -- But if the package is a library unit that does NOT require
2383 -- a body, then no body is permitted, so we are sure that there
2384 -- is no body for the original generic package.
2386 else
2387 return Empty;
2388 end if;
2390 -- Otherwise look and see if we are embedded in a further package
2392 elsif Is_Package_Or_Generic_Package (Scop) then
2394 -- If so, get the body of the enclosing package, and look in
2395 -- its package body for the package body we are looking for.
2397 PBody := Locate_Corresponding_Body (Scop);
2399 if No (PBody) then
2400 return Empty;
2401 else
2402 return Find_Body_In (PE, First (Declarations (PBody)));
2403 end if;
2405 -- If we are not embedded in a further package, then the body
2406 -- must be in the same declarative part as we are.
2408 else
2409 return Find_Body_In (PE, Next (Decl));
2410 end if;
2411 end Locate_Corresponding_Body;
2413 -- Start of processing for Has_Generic_Body
2415 begin
2416 if Present (Corresponding_Body (Decl)) then
2417 return True;
2419 elsif Unit_Requires_Body (Ent) then
2420 return True;
2422 -- Compilation units cannot have optional bodies
2424 elsif Is_Compilation_Unit (Ent) then
2425 return False;
2427 -- Otherwise look at what scope we are in
2429 else
2430 Scop := Scope (Ent);
2432 -- Case of entity is in other than a package spec, in this case
2433 -- the body, if present, must be in the same declarative part.
2435 if not Is_Package_Or_Generic_Package (Scop) then
2436 declare
2437 P : Node_Id;
2439 begin
2440 -- Declaration node may get us a spec, so if so, go to
2441 -- the parent declaration.
2443 P := Declaration_Node (Ent);
2444 while not Is_List_Member (P) loop
2445 P := Parent (P);
2446 end loop;
2448 return Present (Find_Body_In (Ent, Next (P)));
2449 end;
2451 -- If the entity is in a package spec, then we have to locate
2452 -- the corresponding package body, and look there.
2454 else
2455 declare
2456 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
2458 begin
2459 if No (PBody) then
2460 return False;
2461 else
2462 return
2463 Present
2464 (Find_Body_In (Ent, (First (Declarations (PBody)))));
2465 end if;
2466 end;
2467 end if;
2468 end if;
2469 end Has_Generic_Body;
2471 -----------------------
2472 -- Insert_Elab_Check --
2473 -----------------------
2475 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
2476 Nod : Node_Id;
2477 Loc : constant Source_Ptr := Sloc (N);
2479 begin
2480 -- If expansion is disabled, do not generate any checks. Also
2481 -- skip checks if any subunits are missing because in either
2482 -- case we lack the full information that we need, and no object
2483 -- file will be created in any case.
2485 if not Expander_Active or else Subunits_Missing then
2486 return;
2487 end if;
2489 -- If we have a generic instantiation, where Instance_Spec is set,
2490 -- then this field points to a generic instance spec that has
2491 -- been inserted before the instantiation node itself, so that
2492 -- is where we want to insert a check.
2494 if Nkind (N) in N_Generic_Instantiation
2495 and then Present (Instance_Spec (N))
2496 then
2497 Nod := Instance_Spec (N);
2498 else
2499 Nod := N;
2500 end if;
2502 -- If we are inserting at the top level, insert in Aux_Decls
2504 if Nkind (Parent (Nod)) = N_Compilation_Unit then
2505 declare
2506 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2507 R : Node_Id;
2509 begin
2510 if No (C) then
2511 R :=
2512 Make_Raise_Program_Error (Loc,
2513 Reason => PE_Access_Before_Elaboration);
2514 else
2515 R :=
2516 Make_Raise_Program_Error (Loc,
2517 Condition => Make_Op_Not (Loc, C),
2518 Reason => PE_Access_Before_Elaboration);
2519 end if;
2521 if No (Declarations (ADN)) then
2522 Set_Declarations (ADN, New_List (R));
2523 else
2524 Append_To (Declarations (ADN), R);
2525 end if;
2527 Analyze (R);
2528 end;
2530 -- Otherwise just insert before the node in question. However, if
2531 -- the context of the call has already been analyzed, an insertion
2532 -- will not work if it depends on subsequent expansion (e.g. a call in
2533 -- a branch of a short-circuit). In that case we replace the call with
2534 -- a conditional expression, or with a Raise if it is unconditional.
2535 -- Unfortunately this does not work if the call has a dynamic size,
2536 -- because gigi regards it as a dynamic-sized temporary. If such a call
2537 -- appears in a short-circuit expression, the elaboration check will be
2538 -- missed (rare enough ???). Otherwise, the code below inserts the check
2539 -- at the appropriate place before the call. Same applies in the even
2540 -- rarer case the return type has a known size but is unconstrained.
2542 else
2543 if Nkind (N) = N_Function_Call
2544 and then Analyzed (Parent (N))
2545 and then Size_Known_At_Compile_Time (Etype (N))
2546 and then
2547 (not Has_Discriminants (Etype (N))
2548 or else Is_Constrained (Etype (N)))
2550 then
2551 declare
2552 Typ : constant Entity_Id := Etype (N);
2553 Chk : constant Boolean := Do_Range_Check (N);
2555 R : constant Node_Id :=
2556 Make_Raise_Program_Error (Loc,
2557 Reason => PE_Access_Before_Elaboration);
2559 begin
2560 Set_Etype (R, Typ);
2562 if No (C) then
2563 Rewrite (N, R);
2565 else
2566 Rewrite (N,
2567 Make_Conditional_Expression (Loc,
2568 Expressions => New_List (C, Relocate_Node (N), R)));
2569 end if;
2571 Analyze_And_Resolve (N, Typ);
2573 -- If the original call requires a range check, so does the
2574 -- conditional expression.
2576 if Chk then
2577 Enable_Range_Check (N);
2578 else
2579 Set_Do_Range_Check (N, False);
2580 end if;
2581 end;
2583 else
2584 if No (C) then
2585 Insert_Action (Nod,
2586 Make_Raise_Program_Error (Loc,
2587 Reason => PE_Access_Before_Elaboration));
2588 else
2589 Insert_Action (Nod,
2590 Make_Raise_Program_Error (Loc,
2591 Condition =>
2592 Make_Op_Not (Loc,
2593 Right_Opnd => C),
2594 Reason => PE_Access_Before_Elaboration));
2595 end if;
2596 end if;
2597 end if;
2598 end Insert_Elab_Check;
2600 ------------------
2601 -- Output_Calls --
2602 ------------------
2604 procedure Output_Calls (N : Node_Id) is
2605 Ent : Entity_Id;
2607 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
2608 -- An internal function, used to determine if a name, Nm, is either
2609 -- a non-internal name, or is an internal name that is printable
2610 -- by the error message circuits (i.e. it has a single upper
2611 -- case letter at the end).
2613 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
2614 begin
2615 if not Is_Internal_Name (Nm) then
2616 return True;
2618 elsif Name_Len = 1 then
2619 return False;
2621 else
2622 Name_Len := Name_Len - 1;
2623 return not Is_Internal_Name;
2624 end if;
2625 end Is_Printable_Error_Name;
2627 -- Start of processing for Output_Calls
2629 begin
2630 for J in reverse 1 .. Elab_Call.Last loop
2631 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
2633 Ent := Elab_Call.Table (J).Ent;
2635 if Is_Generic_Unit (Ent) then
2636 Error_Msg_NE ("\?& instantiated #", N, Ent);
2638 elsif Is_Init_Proc (Ent) then
2639 Error_Msg_N ("\?initialization procedure called #", N);
2641 elsif Is_Printable_Error_Name (Chars (Ent)) then
2642 Error_Msg_NE ("\?& called #", N, Ent);
2644 else
2645 Error_Msg_N ("\? called #", N);
2646 end if;
2647 end loop;
2648 end Output_Calls;
2650 ----------------------------
2651 -- Same_Elaboration_Scope --
2652 ----------------------------
2654 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
2655 S1 : Entity_Id;
2656 S2 : Entity_Id;
2658 begin
2659 -- Find elaboration scope for Scop1
2661 S1 := Scop1;
2662 while S1 /= Standard_Standard
2663 and then (Ekind (S1) = E_Package
2664 or else
2665 Ekind (S1) = E_Protected_Type
2666 or else
2667 Ekind (S1) = E_Block)
2668 loop
2669 S1 := Scope (S1);
2670 end loop;
2672 -- Find elaboration scope for Scop2
2674 S2 := Scop2;
2675 while S2 /= Standard_Standard
2676 and then (Ekind (S2) = E_Package
2677 or else
2678 Ekind (S2) = E_Protected_Type
2679 or else
2680 Ekind (S2) = E_Block)
2681 loop
2682 S2 := Scope (S2);
2683 end loop;
2685 return S1 = S2;
2686 end Same_Elaboration_Scope;
2688 -----------------
2689 -- Set_C_Scope --
2690 -----------------
2692 procedure Set_C_Scope is
2693 begin
2694 while not Is_Compilation_Unit (C_Scope) loop
2695 C_Scope := Scope (C_Scope);
2696 end loop;
2697 end Set_C_Scope;
2699 -----------------
2700 -- Spec_Entity --
2701 -----------------
2703 function Spec_Entity (E : Entity_Id) return Entity_Id is
2704 Decl : Node_Id;
2706 begin
2707 -- Check for case of body entity
2708 -- Why is the check for E_Void needed???
2710 if Ekind (E) = E_Void
2711 or else Ekind (E) = E_Subprogram_Body
2712 or else Ekind (E) = E_Package_Body
2713 then
2714 Decl := E;
2716 loop
2717 Decl := Parent (Decl);
2718 exit when Nkind (Decl) in N_Proper_Body;
2719 end loop;
2721 return Corresponding_Spec (Decl);
2723 else
2724 return E;
2725 end if;
2726 end Spec_Entity;
2728 -------------------
2729 -- Supply_Bodies --
2730 -------------------
2732 procedure Supply_Bodies (N : Node_Id) is
2733 begin
2734 if Nkind (N) = N_Subprogram_Declaration then
2735 declare
2736 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
2737 begin
2738 Set_Is_Imported (Ent);
2739 Set_Convention (Ent, Convention_Stubbed);
2740 end;
2742 elsif Nkind (N) = N_Package_Declaration then
2743 declare
2744 Spec : constant Node_Id := Specification (N);
2745 begin
2746 New_Scope (Defining_Unit_Name (Spec));
2747 Supply_Bodies (Visible_Declarations (Spec));
2748 Supply_Bodies (Private_Declarations (Spec));
2749 Pop_Scope;
2750 end;
2751 end if;
2752 end Supply_Bodies;
2754 procedure Supply_Bodies (L : List_Id) is
2755 Elmt : Node_Id;
2756 begin
2757 if Present (L) then
2758 Elmt := First (L);
2759 while Present (Elmt) loop
2760 Supply_Bodies (Elmt);
2761 Next (Elmt);
2762 end loop;
2763 end if;
2764 end Supply_Bodies;
2766 ------------
2767 -- Within --
2768 ------------
2770 function Within (E1, E2 : Entity_Id) return Boolean is
2771 Scop : Entity_Id;
2773 begin
2774 Scop := E1;
2775 loop
2776 if Scop = E2 then
2777 return True;
2779 elsif Scop = Standard_Standard then
2780 return False;
2782 else
2783 Scop := Scope (Scop);
2784 end if;
2785 end loop;
2787 raise Program_Error;
2788 end Within;
2790 --------------------------
2791 -- Within_Elaborate_All --
2792 --------------------------
2794 function Within_Elaborate_All (E : Entity_Id) return Boolean is
2795 Item : Node_Id;
2796 Item2 : Node_Id;
2797 Elab_Id : Entity_Id;
2798 Par : Node_Id;
2800 begin
2801 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
2802 while Present (Item) loop
2803 if Nkind (Item) = N_Pragma
2804 and then Get_Pragma_Id (Chars (Item)) = Pragma_Elaborate_All
2805 then
2806 -- Return if some previous error on the pragma itself
2808 if Error_Posted (Item) then
2809 return False;
2810 end if;
2812 Elab_Id :=
2813 Entity
2814 (Expression (First (Pragma_Argument_Associations (Item))));
2816 Par := Parent (Unit_Declaration_Node (Elab_Id));
2818 Item2 := First (Context_Items (Par));
2819 while Present (Item2) loop
2820 if Nkind (Item2) = N_With_Clause
2821 and then Entity (Name (Item2)) = E
2822 then
2823 return True;
2824 end if;
2826 Next (Item2);
2827 end loop;
2828 end if;
2830 Next (Item);
2831 end loop;
2833 return False;
2834 end Within_Elaborate_All;
2836 end Sem_Elab;