1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1997-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Exp_Util; use Exp_Util;
36 with Expander; use Expander;
37 with Fname; use Fname;
39 with Lib.Load; use Lib.Load;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Output; use Output;
45 with Restrict; use Restrict;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch7; use Sem_Ch7;
49 with Sem_Ch8; use Sem_Ch8;
50 with Sem_Res; use Sem_Res;
51 with Sem_Util; use Sem_Util;
52 with Sinfo; use Sinfo;
53 with Sinput; use Sinput;
54 with Snames; use Snames;
55 with Stand; use Stand;
57 with Tbuild; use Tbuild;
58 with Uname; use Uname;
60 package body Sem_Elab is
62 -- The following table records the recursive call chain for output
63 -- in the Output routine. Each entry records the call node and the
64 -- entity of the called routine. The number of entries in the table
65 -- (i.e. the value of Elab_Call.Last) indicates the current depth
66 -- of recursion and is used to identify the outer level.
68 type Elab_Call_Entry is record
73 package Elab_Call is new Table.Table (
74 Table_Component_Type => Elab_Call_Entry,
75 Table_Index_Type => Int,
78 Table_Increment => 100,
79 Table_Name => "Elab_Call");
81 -- This table is initialized at the start of each outer level call.
82 -- It holds the entities for all subprograms that have been examined
83 -- for this particular outer level call, and is used to prevent both
84 -- infinite recursion, and useless reanalysis of bodies already seen
86 package Elab_Visited is new Table.Table (
87 Table_Component_Type => Entity_Id,
88 Table_Index_Type => Int,
91 Table_Increment => 100,
92 Table_Name => "Elab_Visited");
94 -- This table stores calls to Check_Internal_Call that are delayed
95 -- until all generics are instantiated, and in particular that all
96 -- generic bodies have been inserted. We need to delay, because we
97 -- need to be able to look through the inserted bodies.
99 type Delay_Element is record
101 -- The parameter N from the call to Check_Internal_Call. Note that
102 -- this node may get rewritten over the delay period by expansion
103 -- in the call case (but not in the instantiation case).
106 -- The parameter E from the call to Check_Internal_Call
108 Orig_Ent : Entity_Id;
109 -- The parameter Orig_Ent from the call to Check_Internal_Call
112 -- The current scope of the call. This is restored when we complete
113 -- the delayed call, so that we do this in the right scope.
115 From_Elab_Code : Boolean;
116 -- Save indication of whether this call is from elaboration code
118 Outer_Scope : Entity_Id;
119 -- Save scope of outer level call
123 package Delay_Check is new Table.Table (
124 Table_Component_Type => Delay_Element,
125 Table_Index_Type => Int,
126 Table_Low_Bound => 1,
127 Table_Initial => 1000,
128 Table_Increment => 100,
129 Table_Name => "Delay_Check");
132 -- Top level scope of current scope. We need to compute this only
133 -- once at the outer level, i.e. for a call to Check_Elab_Call from
134 -- outside this unit.
136 Outer_Level_Sloc : Source_Ptr;
137 -- Save Sloc value for outer level call node for comparisons of source
138 -- locations. A body is too late if it appears after the *outer* level
139 -- call, not the particular call that is being analyzed.
141 From_Elab_Code : Boolean;
142 -- This flag shows whether the outer level call currently being examined
143 -- is or is not in elaboration code. We are only interested in calls to
144 -- routines in other units if this flag is True.
146 In_Task_Activation : Boolean := False;
147 -- This flag indicates whether we are performing elaboration checks on
148 -- task procedures, at the point of activation. If true, we do not trace
149 -- internal calls in these procedures, because all local bodies are known
152 Delaying_Elab_Checks : Boolean := True;
153 -- This is set True till the compilation is complete, including the
154 -- insertion of all instance bodies. Then when Check_Elab_Calls is
155 -- called, the delay table is used to make the delayed calls and
156 -- this flag is reset to False, so that the calls are processed
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 -- Note: Outer_Scope in all these calls represents the scope of
163 -- interest of the outer level call. If it is set to Standard_Standard,
164 -- then it means the outer level call was at elaboration level, and that
165 -- thus all calls are of interest. If it was set to some other scope,
166 -- then the original call was an inner call, and we are not interested
167 -- in calls that go outside this scope.
169 procedure Check_A_Call
172 Outer_Scope : Entity_Id;
173 Inter_Unit_Only : Boolean;
174 Generate_Warnings : Boolean := True);
175 -- This is the internal recursive routine that is called to check for
176 -- a possible elaboration error. The argument N is a subprogram call
177 -- or generic instantiation to be checked, and E is the entity of
178 -- the called subprogram, or instantiated generic unit. The flag
179 -- Outer_Scope is the outer level scope for the original call.
180 -- Inter_Unit_Only is set if the call is only to be checked in the
181 -- case where it is to another unit (and skipped if within a unit).
182 -- Generate_Warnings is set to True to suppress warning messages
183 -- about missing pragma Elaborate_All's. These messages are not
184 -- wanted for inner calls in the dynamic model.
186 procedure Check_Bad_Instantiation (N : Node_Id);
187 -- N is a node for an instantiation (if called with any other node kind,
188 -- Check_Bad_Instantiation ignores the call). This subprogram checks for
189 -- the special case of a generic instantiation of a generic spec in the
190 -- same declarative part as the instantiation where a body is present and
191 -- has not yet been seen. This is an obvious error, but needs to be checked
192 -- specially at the time of the instantiation, since it is a case where we
193 -- cannot insert the body anywhere. If this case is detected, warnings are
194 -- generated, and a raise of Program_Error is inserted. In addition any
195 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation
196 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this
197 -- flag as an indication that no attempt should be made to insert an
200 procedure Check_Internal_Call
203 Outer_Scope : Entity_Id;
204 Orig_Ent : Entity_Id);
205 -- N is a function call or procedure statement call node and E is
206 -- the entity of the called function, which is within the current
207 -- compilation unit (where subunits count as part of the parent).
208 -- This call checks if this call, or any call within any accessed
209 -- body could cause an ABE, and if so, outputs a warning. Orig_Ent
210 -- differs from E only in the case of renamings, and points to the
211 -- original name of the entity. This is used for error messages.
212 -- Outer_Scope is the outer level scope for the original call.
214 procedure Check_Internal_Call_Continue
217 Outer_Scope : Entity_Id;
218 Orig_Ent : Entity_Id);
219 -- The processing for Check_Internal_Call is divided up into two phases,
220 -- and this represents the second phase. The second phase is delayed if
221 -- Delaying_Elab_Calls is set to True. In this delayed case, the first
222 -- phase makes an entry in the Delay_Check table, which is processed
223 -- when Check_Elab_Calls is called. N, E and Orig_Ent are as for the call
224 -- to Check_Internal_Call. Outer_Scope is the outer level scope for
225 -- the original call.
227 function Has_Generic_Body (N : Node_Id) return Boolean;
228 -- N is a generic package instantiation node, and this routine determines
229 -- if this package spec does in fact have a generic body. If so, then
230 -- True is returned, otherwise False. Note that this is not at all the
231 -- same as checking if the unit requires a body, since it deals with
232 -- the case of optional bodies accurately (i.e. if a body is optional,
233 -- then it looks to see if a body is actually present). Note: this
234 -- function can only do a fully correct job if in generating code mode
235 -- where all bodies have to be present. If we are operating in semantics
236 -- check only mode, then in some cases of optional bodies, a result of
237 -- False may incorrectly be given. In practice this simply means that
238 -- some cases of warnings for incorrect order of elaboration will only
239 -- be given when generating code, which is not a big problem (and is
240 -- inevitable, given the optional body semantics of Ada).
242 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty);
243 -- Given code for an elaboration check (or unconditional raise if
244 -- the check is not needed), inserts the code in the appropriate
245 -- place. N is the call or instantiation node for which the check
246 -- code is required. C is the test whose failure triggers the raise.
248 procedure Output_Calls (N : Node_Id);
249 -- Outputs chain of calls stored in the Elab_Call table. The caller
250 -- has already generated the main warning message, so the warnings
251 -- generated are all continuation messages. The argument is the
252 -- call node at which the messages are to be placed.
254 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean;
255 -- Given two scopes, determine whether they are the same scope from an
256 -- elaboration point of view, i.e. packages and blocks are ignored.
258 procedure Set_C_Scope;
259 -- On entry C_Scope is set to some scope. On return, C_Scope is reset
260 -- to be the enclosing compilation unit of this scope.
262 function Spec_Entity (E : Entity_Id) return Entity_Id;
263 -- Given a compilation unit entity, if it is a spec entity, it is
264 -- returned unchanged. If it is a body entity, then the spec for
265 -- the corresponding spec is returned
267 procedure Supply_Bodies (N : Node_Id);
268 -- Given a node, N, that is either a subprogram declaration or a package
269 -- declaration, this procedure supplies dummy bodies for the subprogram
270 -- or for all subprograms in the package. If the given node is not one
271 -- of these two possibilities, then Supply_Bodies does nothing. The
272 -- dummy body is supplied by setting the subprogram to be Imported with
273 -- convention Stubbed.
275 procedure Supply_Bodies (L : List_Id);
276 -- Calls Supply_Bodies for all elements of the given list L.
278 function Within (E1, E2 : Entity_Id) return Boolean;
279 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or
280 -- is one of its contained scopes, False otherwise.
286 procedure Check_A_Call
289 Outer_Scope : Entity_Id;
290 Inter_Unit_Only : Boolean;
291 Generate_Warnings : Boolean := True)
293 Loc : constant Source_Ptr := Sloc (N);
298 -- Top level scope of entity for called subprogram
300 Body_Acts_As_Spec : Boolean;
301 -- Set to true if call is to body acting as spec (no separate spec)
303 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
304 -- Indicates if we have instantiation case
306 Caller_Unit_Internal : Boolean;
307 Callee_Unit_Internal : Boolean;
309 Inst_Caller : Source_Ptr;
310 Inst_Callee : Source_Ptr;
312 Unit_Caller : Unit_Number_Type;
313 Unit_Callee : Unit_Number_Type;
315 Cunit_SW : Boolean := False;
316 -- Set to suppress warnings for case of external reference where
317 -- one of the enclosing scopes has the Suppress_Elaboration_Warnings
318 -- flag set. For the internal case, we ignore this flag.
320 Cunit_SC : Boolean := False;
321 -- Set to suppress dynamic elaboration checks where one of the
322 -- enclosing scopes has Suppress_Elaboration_Checks set. For
323 -- the internal case, we ignore this flag.
326 -- Go to parent for derived subprogram, or to original subprogram
327 -- in the case of a renaming (Alias covers both these cases)
331 if Suppress_Elaboration_Warnings (Ent) then
335 -- Nothing to do for imported entities,
337 if Is_Imported (Ent) then
341 exit when Inst_Case or else No (Alias (Ent));
345 Decl := Unit_Declaration_Node (Ent);
347 if Nkind (Decl) = N_Subprogram_Body then
348 Body_Acts_As_Spec := True;
350 elsif Nkind (Decl) = N_Subprogram_Declaration
351 or else Nkind (Decl) = N_Subprogram_Body_Stub
354 Body_Acts_As_Spec := False;
356 -- If we have none of an instantiation, subprogram body or
357 -- subprogram declaration, then it is not a case that we want
358 -- to check. (One case is a call to a generic formal subprogram,
359 -- where we do not want the check in the template).
367 if Suppress_Elaboration_Warnings (E_Scope) then
371 if Suppress_Elaboration_Checks (E_Scope) then
375 -- Exit when we get to compilation unit, not counting subunits
377 exit when Is_Compilation_Unit (E_Scope)
378 and then (Is_Child_Unit (E_Scope)
379 or else Scope (E_Scope) = Standard_Standard);
381 -- If we did not find a compilation unit, other than standard,
382 -- then nothing to check (happens in some instantiation cases)
384 if E_Scope = Standard_Standard then
387 -- Otherwise move up a scope looking for compilation unit
390 E_Scope := Scope (E_Scope);
394 -- No checks needed for pure or preelaborated compilation units
397 or else Is_Preelaborated (E_Scope)
402 -- If the generic entity is within a deeper instance than we are, then
403 -- either the instantiation to which we refer itself caused an ABE, in
404 -- which case that will be handled separately. Otherwise, we know that
405 -- the body we need appears as needed at the point of the instantiation.
406 -- However, this assumption is only valid if we are in static mode.
408 if not Dynamic_Elaboration_Checks
409 and then Instantiation_Depth (Sloc (Ent)) >
410 Instantiation_Depth (Sloc (N))
415 -- Do not give a warning for a package with no body
417 if Ekind (Ent) = E_Generic_Package
418 and then not Has_Generic_Body (N)
423 -- Case of entity is not in current unit (i.e. with'ed unit case)
425 if E_Scope /= C_Scope then
427 -- We are only interested in such calls if the outer call was from
428 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode.
430 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then
434 -- Nothing to do if some scope said to ignore warnings
440 -- Nothing to do for a generic instance, because in this case
441 -- the checking was at the point of instantiation of the generic
442 -- However, this shortcut is only applicable in static mode.
444 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then
448 -- Nothing to do if subprogram with no separate spec
450 if Body_Acts_As_Spec then
454 -- Check cases of internal units
456 Callee_Unit_Internal :=
457 Is_Internal_File_Name
458 (Unit_File_Name (Get_Source_Unit (E_Scope)));
460 -- Do not give a warning if the with'ed unit is internal
461 -- and this is the generic instantiation case (this saves a
462 -- lot of hassle dealing with the Text_IO special child units)
464 if Callee_Unit_Internal and Inst_Case then
468 if C_Scope = Standard_Standard then
469 Caller_Unit_Internal := False;
471 Caller_Unit_Internal :=
472 Is_Internal_File_Name
473 (Unit_File_Name (Get_Source_Unit (C_Scope)));
476 -- Do not give a warning if the with'ed unit is internal
477 -- and the caller is not internal (since the binder always
478 -- elaborates internal units first).
480 if Callee_Unit_Internal and (not Caller_Unit_Internal) then
484 -- For now, if debug flag -gnatdE is not set, do no checking for
485 -- one internal unit withing another. This fixes the problem with
486 -- the sgi build and storage errors. To be resolved later ???
488 if (Callee_Unit_Internal and Caller_Unit_Internal)
489 and then not Debug_Flag_EE
496 -- If the call is in an instance, and the called entity is not
497 -- defined in the same instance, then the elaboration issue
498 -- focuses around the unit containing the template, it is
499 -- this unit which requires an Elaborate_All.
501 -- However, if we are doing dynamic elaboration, we need to
502 -- chase the call in the usual manner.
504 -- We do not handle the case of calling a generic formal correctly
505 -- in the static case. See test 4703-004 to explore this gap ???
507 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N)));
508 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent)));
510 if Inst_Caller = No_Location then
511 Unit_Caller := No_Unit;
513 Unit_Caller := Get_Source_Unit (N);
516 if Inst_Callee = No_Location then
517 Unit_Callee := No_Unit;
519 Unit_Callee := Get_Source_Unit (Ent);
522 if Unit_Caller /= No_Unit
523 and then Unit_Callee /= Unit_Caller
524 and then not Dynamic_Elaboration_Checks
526 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller));
528 -- If we don't get a spec entity, just ignore call. Not
529 -- quite clear why this check is necessary.
535 -- Otherwise step to enclosing compilation unit
537 while not Is_Compilation_Unit (E_Scope) loop
538 E_Scope := Scope (E_Scope);
541 -- For the case of not in an instance, or call within instance
542 -- We recompute E_Scope for the error message, since we
543 -- do NOT want to go to the unit which has the ultimate
544 -- declaration in the case of renaming and derivation and
545 -- we also want to go to the generic unit in the case of
546 -- an instance, and no further.
549 -- Loop to carefully follow renamings and derivations
550 -- one step outside the current unit, but not further.
554 while not Is_Compilation_Unit (E_Scope) loop
555 E_Scope := Scope (E_Scope);
558 -- If E_Scope is the same as C_Scope, it means that there
559 -- definitely was a renaming or derivation, and we are
560 -- not yet out of the current unit.
562 exit when E_Scope /= C_Scope;
567 if not Suppress_Elaboration_Warnings (Ent)
568 and then not Suppress_Elaboration_Warnings (E_Scope)
569 and then Elab_Warnings
570 and then Generate_Warnings
572 Warn_On_Instance := True;
576 ("instantiation of& may raise Program_Error?", N, Ent);
579 ("call to & may raise Program_Error?", N, Ent);
582 Error_Msg_Qual_Level := Nat'Last;
584 ("\missing pragma Elaborate_All for&?", N, E_Scope);
585 Error_Msg_Qual_Level := 0;
587 Warn_On_Instance := False;
589 -- Set flag to prevent further warnings for same unit
590 -- unless in All_Errors_Mode.
592 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then
593 Set_Suppress_Elaboration_Warnings (E_Scope);
597 -- Check for runtime elaboration check required
599 if Dynamic_Elaboration_Checks then
600 if not Elaboration_Checks_Suppressed (Ent)
601 and then not Suppress_Elaboration_Checks (E_Scope)
602 and then not Cunit_SC
604 -- Runtime elaboration check required. generate check of the
605 -- elaboration Boolean for the unit containing the entity.
607 Insert_Elab_Check (N,
608 Make_Attribute_Reference (Loc,
609 Attribute_Name => Name_Elaborated,
612 (Spec_Entity (E_Scope), Loc)));
615 -- If no dynamic check required, then ask binder to guarantee
616 -- that the necessary elaborations will be done properly!
619 if not Suppress_Elaboration_Warnings (E)
620 and then not Suppress_Elaboration_Warnings (E_Scope)
621 and then Elab_Warnings
622 and then Generate_Warnings
623 and then not Inst_Case
625 Error_Msg_Node_2 := E_Scope;
626 Error_Msg_NE ("call to& in elaboration code " &
627 "requires pragma Elaborate_All on&?", N, E);
630 Set_Elaborate_All_Desirable (E_Scope);
631 Set_Suppress_Elaboration_Warnings (E_Scope);
634 -- Case of entity is in same unit as call or instantiation
636 elsif not Inter_Unit_Only then
637 Check_Internal_Call (N, Ent, Outer_Scope, E);
642 -----------------------------
643 -- Check_Bad_Instantiation --
644 -----------------------------
646 procedure Check_Bad_Instantiation (N : Node_Id) is
651 -- Nothing to do if we do not have an instantiation (happens in some
652 -- error cases, and also in the formal package declaration case)
654 if Nkind (N) not in N_Generic_Instantiation then
657 -- Nothing to do if errors already detected (avoid cascaded errors)
659 elsif Errors_Detected /= 0 then
662 -- Nothing to do if not in full analysis mode
664 elsif not Full_Analysis then
667 -- Nothing to do if inside a generic template
669 elsif Inside_A_Generic then
672 -- Nothing to do if a library level instantiation
674 elsif Nkind (Parent (N)) = N_Compilation_Unit then
677 -- Nothing to do if we are compiling a proper body for semantic
678 -- purposes only. The generic body may be in another proper body.
681 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit
689 -- The case we are interested in is when the generic spec is in the
690 -- current declarative part
692 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent))
693 or else not In_Same_Extended_Unit (Sloc (N), Sloc (Ent))
698 -- If the generic entity is within a deeper instance than we are, then
699 -- either the instantiation to which we refer itself caused an ABE, in
700 -- which case that will be handled separately. Otherwise, we know that
701 -- the body we need appears as needed at the point of the instantiation.
702 -- If they are both at the same level but not within the same instance
703 -- then the body of the generic will be in the earlier instance.
706 D1 : constant Int := Instantiation_Depth (Sloc (Ent));
707 D2 : constant Int := Instantiation_Depth (Sloc (N));
714 and then Is_Generic_Instance (Scope (Ent))
715 and then not In_Open_Scopes (Scope (Ent))
721 -- Now we can proceed, if the entity being called has a completion,
722 -- then we are definitely OK, since we have already seen the body.
724 if Has_Completion (Ent) then
728 -- If there is no body, then nothing to do
730 if not Has_Generic_Body (N) then
734 -- Here we definitely have a bad instantiation
737 ("?cannot instantiate& before body seen", N, Ent);
739 if Present (Instance_Spec (N)) then
740 Supply_Bodies (Instance_Spec (N));
744 ("\?Program_Error will be raised at run time", N);
745 Insert_Elab_Check (N);
746 Set_ABE_Is_Certain (N);
748 end Check_Bad_Instantiation;
750 ---------------------
751 -- Check_Elab_Call --
752 ---------------------
754 procedure Check_Elab_Call
756 Outer_Scope : Entity_Id := Empty)
763 -- For an entry call, check relevant restriction
765 if Nkind (N) = N_Entry_Call_Statement
766 and then not In_Subprogram_Or_Concurrent_Unit
768 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N);
770 -- Nothing to do if this is not a call (happens in some error
771 -- conditions, and in some cases where rewriting occurs).
773 elsif Nkind (N) /= N_Function_Call
774 and then Nkind (N) /= N_Procedure_Call_Statement
778 -- Nothing to do if this is a call already rewritten for elab checking.
780 elsif Nkind (Parent (N)) = N_Conditional_Expression then
783 -- Nothing to do if inside a generic template
785 elsif Inside_A_Generic
786 and then not Present (Enclosing_Generic_Body (N))
791 -- Here we have a call at elaboration time which must be checked
793 if Debug_Flag_LL then
794 Write_Str (" Check_Elab_Call: ");
797 or else not Is_Entity_Name (Name (N))
799 Write_Str ("<<not entity name>> ");
801 Write_Name (Chars (Entity (Name (N))));
804 Write_Str (" call at ");
805 Write_Location (Sloc (N));
809 -- Climb up the tree to make sure we are not inside a
810 -- default expression of a parameter specification or
811 -- a record component, since in both these cases, we
812 -- will be doing the actual call later, not now, and it
813 -- is at the time of the actual call (statically speaking)
814 -- that we must do our static check, not at the time of
815 -- its initial analysis).
818 while Present (P) loop
819 if Nkind (P) = N_Parameter_Specification
821 Nkind (P) = N_Component_Declaration
829 -- Stuff that happens only at the outer level
831 if No (Outer_Scope) then
832 Elab_Visited.Set_Last (0);
834 -- Nothing to do if current scope is Standard (this is a bit
835 -- odd, but it happens in the case of generic instantiations).
837 C_Scope := Current_Scope;
839 if C_Scope = Standard_Standard then
843 -- First case, we are in elaboration code
845 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
847 if From_Elab_Code then
849 -- Complain if call that comes from source in preelaborated
850 -- unit and we are not inside a subprogram (i.e. we are in
853 if Comes_From_Source (N)
854 and then In_Preelaborated_Unit
857 ("non-static call not allowed in preelaborated unit", N);
861 -- Second case, we are inside a subprogram or concurrent unit
862 -- i.e, we are not in elaboration code.
865 -- In this case, the issue is whether we are inside the
866 -- declarative part of the unit in which we live, or inside
867 -- its statements. In the latter case, there is no issue of
868 -- ABE calls at this level (a call from outside to the unit
869 -- in which we live might cause an ABE, but that will be
870 -- detected when we analyze that outer level call, as it
871 -- recurses into the called unit).
873 -- Climb up the tree, doing this test, and also testing
874 -- for being inside a default expression, which, as
875 -- discussed above, is not checked at this stage.
884 -- If we find a parentless subtree, it seems safe to
885 -- assume that we are not in a declarative part and
886 -- that no checking is required.
892 if Is_List_Member (P) then
893 L := List_Containing (P);
900 exit when Nkind (P) = N_Subunit;
902 -- Filter out case of default expressions, where
903 -- we do not do the check at this stage.
905 if Nkind (P) = N_Parameter_Specification
907 Nkind (P) = N_Component_Declaration
912 if Nkind (P) = N_Subprogram_Body
914 Nkind (P) = N_Protected_Body
916 Nkind (P) = N_Task_Body
918 Nkind (P) = N_Block_Statement
920 if L = Declarations (P) then
923 -- We are not in elaboration code, but we are doing
924 -- dynamic elaboration checks, in this case, we still
925 -- need to do the call, since the subprogram we are in
926 -- could be called from another unit, also in dynamic
927 -- elaboration check mode, at elaboration time.
929 elsif Dynamic_Elaboration_Checks then
931 -- This is a rather new check, going into version
932 -- 3.14a1 for the first time (V1.80 of this unit),
933 -- so we provide a debug flag to enable it. That
934 -- way we have an easy work around for regressions
935 -- that are caused by this new check. This debug
936 -- flag can be removed later.
938 if Debug_Flag_DD then
942 -- Do the check in this case
946 -- Static model, call is not in elaboration code, we
947 -- never need to worry, because in the static model
948 -- the top level caller always takes care of things.
959 -- Retrieve called entity. If this is a call to a protected subprogram,
960 -- the entity is a selected component.
961 -- The callable entity may be absent, in which case there is nothing
962 -- to do. This happens with non-analyzed calls in nested generics.
969 elsif Nkind (Nam) = N_Selected_Component then
970 Ent := Entity (Selector_Name (Nam));
972 elsif not Is_Entity_Name (Nam) then
983 -- Nothing to do if this is a recursive call (i.e. a call to
984 -- an entity that is already in the Elab_Call stack)
986 for J in 1 .. Elab_Visited.Last loop
987 if Ent = Elab_Visited.Table (J) then
992 -- See if we need to analyze this call. We analyze it if either of
993 -- the following conditions is met:
995 -- It is an inner level call (since in this case it was triggered
996 -- by an outer level call from elaboration code), but only if the
997 -- call is within the scope of the original outer level call.
999 -- It is an outer level call from elaboration code, or the called
1000 -- entity is in the same elaboration scope.
1002 -- And in these cases, we will check both inter-unit calls and
1003 -- intra-unit (within a single unit) calls.
1005 C_Scope := Current_Scope;
1007 -- If not outer level call, then we follow it if it is within
1008 -- the original scope of the outer call.
1010 if Present (Outer_Scope)
1011 and then Within (Scope (Ent), Outer_Scope)
1014 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1016 elsif Elaboration_Checks_Suppressed (Current_Scope) then
1019 elsif From_Elab_Code then
1021 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1023 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1025 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1027 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1028 -- is set, then we will do the check, but only in the inter-unit case
1029 -- (this is to accomodate unguarded elaboration calls from other units
1030 -- in which this same mode is set). We don't want warnings in this case,
1031 -- it would generate warnings having nothing to do with elaboration.
1033 elsif Dynamic_Elaboration_Checks then
1039 Inter_Unit_Only => True,
1040 Generate_Warnings => False);
1045 end Check_Elab_Call;
1047 ----------------------
1048 -- Check_Elab_Calls --
1049 ----------------------
1051 procedure Check_Elab_Calls is
1053 -- If expansion is disabled, do not generate any checks. Also
1054 -- skip checks if any subunits are missing because in either
1055 -- case we lack the full information that we need, and no object
1056 -- file will be created in any case.
1058 if not Expander_Active or else Subunits_Missing then
1062 -- Skip delayed calls if we had any errors
1064 if Errors_Detected = 0 then
1065 Delaying_Elab_Checks := False;
1066 Expander_Mode_Save_And_Set (True);
1068 for J in Delay_Check.First .. Delay_Check.Last loop
1069 New_Scope (Delay_Check.Table (J).Curscop);
1070 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code;
1072 Check_Internal_Call_Continue (
1073 N => Delay_Check.Table (J).N,
1074 E => Delay_Check.Table (J).E,
1075 Outer_Scope => Delay_Check.Table (J).Outer_Scope,
1076 Orig_Ent => Delay_Check.Table (J).Orig_Ent);
1081 -- Set Delaying_Elab_Checks back on for next main compilation
1083 Expander_Mode_Restore;
1084 Delaying_Elab_Checks := True;
1086 end Check_Elab_Calls;
1088 ------------------------------
1089 -- Check_Elab_Instantiation --
1090 ------------------------------
1092 procedure Check_Elab_Instantiation
1094 Outer_Scope : Entity_Id := Empty)
1100 -- Check for and deal with bad instantiation case. There is some
1101 -- duplicated code here, but we will worry about this later ???
1103 Check_Bad_Instantiation (N);
1105 if ABE_Is_Certain (N) then
1109 -- Nothing to do if we do not have an instantiation (happens in some
1110 -- error cases, and also in the formal package declaration case)
1112 if Nkind (N) not in N_Generic_Instantiation then
1116 -- Nothing to do if inside a generic template
1118 if Inside_A_Generic then
1123 Ent := Entity (Nam);
1124 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit;
1126 -- See if we need to analyze this instantiation. We analyze it if
1127 -- either of the following conditions is met:
1129 -- It is an inner level instantiation (since in this case it was
1130 -- triggered by an outer level call from elaboration code), but
1131 -- only if the instantiation is within the scope of the original
1132 -- outer level call.
1134 -- It is an outer level instantiation from elaboration code, or the
1135 -- instantiated entity is in the same elaboratoin scope.
1137 -- And in these cases, we will check both the inter-unit case and
1138 -- the intra-unit (within a single unit) case.
1140 C_Scope := Current_Scope;
1142 if Present (Outer_Scope)
1143 and then Within (Scope (Ent), Outer_Scope)
1146 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False);
1148 elsif From_Elab_Code then
1150 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False);
1152 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then
1154 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False);
1156 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode
1157 -- is set, then we will do the check, but only in the inter-unit case
1158 -- (this is to accomodate unguarded elaboration calls from other units
1159 -- in which this same mode is set). We inhibit warnings in this case,
1160 -- since this instantiation is not occurring in elaboration code.
1162 elsif Dynamic_Elaboration_Checks then
1168 Inter_Unit_Only => True,
1169 Generate_Warnings => False);
1174 end Check_Elab_Instantiation;
1176 -------------------------
1177 -- Check_Internal_Call --
1178 -------------------------
1180 procedure Check_Internal_Call
1183 Outer_Scope : Entity_Id;
1184 Orig_Ent : Entity_Id)
1186 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation;
1189 -- If not function or procedure call or instantiation, then ignore
1190 -- call (this happens in some error case and rewriting cases)
1192 if Nkind (N) /= N_Function_Call
1194 Nkind (N) /= N_Procedure_Call_Statement
1200 -- Nothing to do if this is a call or instantiation that has
1201 -- already been found to be a sure ABE
1203 elsif ABE_Is_Certain (N) then
1206 -- Nothing to do if errors already detected (avoid cascaded errors)
1208 elsif Errors_Detected /= 0 then
1211 -- Nothing to do if not in full analysis mode
1213 elsif not Full_Analysis then
1216 -- Nothing to do if within a default expression, since the call
1217 -- is not actualy being made at this time.
1219 elsif In_Default_Expression then
1222 -- Nothing to do for call to intrinsic subprogram
1224 elsif Is_Intrinsic_Subprogram (E) then
1227 -- No need to trace local calls if checking task activation, because
1228 -- other local bodies are elaborated already.
1230 elsif In_Task_Activation then
1234 -- Delay this call if we are still delaying calls
1236 if Delaying_Elab_Checks then
1237 Delay_Check.Increment_Last;
1238 Delay_Check.Table (Delay_Check.Last) :=
1241 Orig_Ent => Orig_Ent,
1242 Curscop => Current_Scope,
1243 Outer_Scope => Outer_Scope,
1244 From_Elab_Code => From_Elab_Code);
1247 -- Otherwise, call phase 2 continuation right now
1250 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent);
1253 end Check_Internal_Call;
1255 ----------------------------------
1256 -- Check_Internal_Call_Continue --
1257 ----------------------------------
1259 procedure Check_Internal_Call_Continue
1262 Outer_Scope : Entity_Id;
1263 Orig_Ent : Entity_Id)
1265 Loc : constant Source_Ptr := Sloc (N);
1266 Inst_Case : constant Boolean := Is_Generic_Unit (E);
1271 function Process (N : Node_Id) return Traverse_Result;
1272 -- Function applied to each node as we traverse the body.
1273 -- Checks for call that needs checking, and if so checks
1274 -- it. Always returns OK, so entire tree is traversed.
1276 function Process (N : Node_Id) return Traverse_Result is
1278 -- If user has specified that there are no entry calls in elaboration
1279 -- code, do not trace past an accept statement, because the rendez-
1280 -- vous will happen after elaboration.
1282 if (Nkind (Original_Node (N)) = N_Accept_Statement
1283 or else Nkind (Original_Node (N)) = N_Selective_Accept)
1284 and then Restrictions (No_Entry_Calls_In_Elaboration_Code)
1288 -- If we have a subprogram call, check it
1290 elsif Nkind (N) = N_Function_Call
1291 or else Nkind (N) = N_Procedure_Call_Statement
1293 Check_Elab_Call (N, Outer_Scope);
1296 -- If we have a generic instantiation, check it
1298 elsif Nkind (N) in N_Generic_Instantiation then
1299 Check_Elab_Instantiation (N, Outer_Scope);
1302 -- Skip subprogram bodies that come from source (wait for
1303 -- call to analyze these). The reason for the come from
1304 -- source test is to avoid catching task bodies.
1306 -- For task bodies, we should really avoid these too, waiting
1307 -- for the task activation, but that's too much trouble to
1308 -- catch for now, so we go in unconditionally. This is not
1309 -- so terrible, it means the error backtrace is not quite
1310 -- complete, and we are too eager to scan bodies of tasks
1311 -- that are unused, but this is hardly very significant!
1313 elsif Nkind (N) = N_Subprogram_Body
1314 and then Comes_From_Source (N)
1323 procedure Traverse is new Atree.Traverse_Proc;
1324 -- Traverse procedure using above Process function
1326 -- Start of processing for Check_Internal_Call_Continue
1329 -- Save outer level call if at outer level
1331 if Elab_Call.Last = 0 then
1332 Outer_Level_Sloc := Loc;
1335 Elab_Visited.Increment_Last;
1336 Elab_Visited.Table (Elab_Visited.Last) := E;
1338 -- If the call is to a function that renames a literal, no check
1341 if Ekind (E) = E_Enumeration_Literal then
1345 Sbody := Unit_Declaration_Node (E);
1347 if Nkind (Sbody) /= N_Subprogram_Body
1349 Nkind (Sbody) /= N_Package_Body
1351 Ebody := Corresponding_Body (Sbody);
1356 Sbody := Unit_Declaration_Node (Ebody);
1360 -- If the body appears after the outer level call or
1361 -- instantiation then we have an error case handled below.
1363 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody))
1364 and then not In_Task_Activation
1368 -- If we have the instantiation case we are done, since we now
1369 -- know that the body of the generic appeared earlier.
1371 elsif Inst_Case then
1374 -- Otherwise we have a call, so we trace through the called
1375 -- body to see if it has any problems ..
1378 pragma Assert (Nkind (Sbody) = N_Subprogram_Body);
1380 Elab_Call.Increment_Last;
1381 Elab_Call.Table (Elab_Call.Last).Cloc := Loc;
1382 Elab_Call.Table (Elab_Call.Last).Ent := E;
1384 if Debug_Flag_LL then
1385 Write_Str ("Elab_Call.Last = ");
1386 Write_Int (Int (Elab_Call.Last));
1387 Write_Str (" Ent = ");
1388 Write_Name (Chars (E));
1390 Write_Location (Sloc (N));
1394 -- Now traverse declarations and statements of subprogram body.
1395 -- Note that we cannot simply Traverse (Sbody), since traverse
1396 -- does not normally visit subprogram bodies.
1399 Decl : Node_Id := First (Declarations (Sbody));
1402 while Present (Decl) loop
1408 Traverse (Handled_Statement_Sequence (Sbody));
1410 Elab_Call.Decrement_Last;
1414 -- Here is the case of calling a subprogram where the body has
1415 -- not yet been encountered, a warning message is needed.
1417 Warn_On_Instance := True;
1419 -- If we have nothing in the call stack, then this is at the
1420 -- outer level, and the ABE is bound to occur.
1422 if Elab_Call.Last = 0 then
1426 ("?cannot instantiate& before body seen", N, Orig_Ent);
1429 ("?cannot call& before body seen", N, Orig_Ent);
1433 ("\?Program_Error will be raised at run time", N);
1434 Insert_Elab_Check (N);
1436 -- Call is not at outer level
1439 -- Deal with dynamic elaboration check
1441 if not Elaboration_Checks_Suppressed (E) then
1442 Set_Elaboration_Entity_Required (E);
1444 -- Case of no elaboration entity allocated yet
1446 if No (Elaboration_Entity (E)) then
1448 -- Create object declaration for elaboration entity, and put it
1449 -- just in front of the spec of the subprogram or generic unit,
1450 -- in the same scope as this unit.
1453 Loce : constant Source_Ptr := Sloc (E);
1454 Ent : constant Entity_Id :=
1455 Make_Defining_Identifier (Loc,
1456 Chars => New_External_Name (Chars (E), 'E'));
1459 Set_Elaboration_Entity (E, Ent);
1460 New_Scope (Scope (E));
1462 Insert_Action (Declaration_Node (E),
1463 Make_Object_Declaration (Loce,
1464 Defining_Identifier => Ent,
1465 Object_Definition =>
1466 New_Occurrence_Of (Standard_Boolean, Loce),
1467 Expression => New_Occurrence_Of (Standard_False, Loce)));
1469 -- Set elaboration flag at the point of the body
1471 Set_Elaboration_Flag (Sbody, E);
1477 -- Generate check of the elaboration Boolean
1479 Insert_Elab_Check (N,
1480 New_Occurrence_Of (Elaboration_Entity (E), Loc));
1483 -- Generate the warning
1485 if not Suppress_Elaboration_Warnings (E) then
1488 ("instantiation of& may occur before body is seen?",
1492 ("call to& may occur before body is seen?", N, Orig_Ent);
1496 ("\Program_Error may be raised at run time?", N);
1502 Warn_On_Instance := False;
1504 -- Set flag to suppress further warnings on same subprogram
1505 -- unless in all errors mode
1507 if not All_Errors_Mode then
1508 Set_Suppress_Elaboration_Warnings (E);
1510 end Check_Internal_Call_Continue;
1512 ----------------------------
1513 -- Check_Task_Activation --
1514 ----------------------------
1516 procedure Check_Task_Activation (N : Node_Id) is
1517 Loc : constant Source_Ptr := Sloc (N);
1520 Task_Scope : Entity_Id;
1521 Cunit_SC : Boolean := False;
1524 Inter_Procs : Elist_Id := New_Elmt_List;
1525 Intra_Procs : Elist_Id := New_Elmt_List;
1526 Enclosing : Entity_Id;
1528 procedure Add_Task_Proc (Typ : Entity_Id);
1529 -- Add to Task_Procs the task body procedure(s) of task types in Typ.
1530 -- For record types, this procedure recurses over component types.
1532 procedure Collect_Tasks (Decls : List_Id);
1533 -- Collect the types of the tasks that are to be activated in the given
1534 -- list of declarations, in order to perform elaboration checks on the
1535 -- corresponding task procedures which are called implicitly here.
1537 function Outer_Unit (E : Entity_Id) return Entity_Id;
1538 -- find enclosing compilation unit of Entity, ignoring subunits, or
1539 -- else enclosing subprogram. If E is not a package, there is no need
1540 -- for inter-unit elaboration checks.
1546 procedure Add_Task_Proc (Typ : Entity_Id) is
1548 Proc : Entity_Id := Empty;
1551 if Is_Task_Type (Typ) then
1552 Proc := Get_Task_Body_Procedure (Typ);
1554 elsif Is_Array_Type (Typ)
1555 and then Has_Task (Base_Type (Typ))
1557 Add_Task_Proc (Component_Type (Typ));
1559 elsif Is_Record_Type (Typ)
1560 and then Has_Task (Base_Type (Typ))
1562 Comp := First_Component (Typ);
1564 while Present (Comp) loop
1565 Add_Task_Proc (Etype (Comp));
1566 Comp := Next_Component (Comp);
1570 -- If the task type is another unit, we will perform the usual
1571 -- elaboration check on its enclosing unit. If the type is in the
1572 -- same unit, we can trace the task body as for an internal call,
1573 -- but we only need to examine other external calls, because at
1574 -- the point the task is activated, internal subprogram bodies
1575 -- will have been elaborated already. We keep separate lists for
1576 -- each kind of task.
1578 if Present (Proc) then
1579 if Outer_Unit (Scope (Proc)) = Enclosing then
1581 if No (Corresponding_Body (Unit_Declaration_Node (Proc)))
1583 (not Is_Generic_Instance (Scope (Proc))
1585 Scope (Proc) = Scope (Defining_Identifier (Decl)))
1588 ("task will be activated before elaboration of its body?",
1591 ("Program_Error will be raised at run-time?", Decl);
1594 Present (Corresponding_Body (Unit_Declaration_Node (Proc)))
1596 Append_Elmt (Proc, Intra_Procs);
1600 Elmt := First_Elmt (Inter_Procs);
1602 -- No need for multiple entries of the same type.
1604 while Present (Elmt) loop
1605 if Node (Elmt) = Proc then
1612 Append_Elmt (Proc, Inter_Procs);
1621 procedure Collect_Tasks (Decls : List_Id) is
1623 if Present (Decls) then
1624 Decl := First (Decls);
1626 while Present (Decl) loop
1628 if Nkind (Decl) = N_Object_Declaration
1629 and then Has_Task (Etype (Defining_Identifier (Decl)))
1631 Add_Task_Proc (Etype (Defining_Identifier (Decl)));
1643 function Outer_Unit (E : Entity_Id) return Entity_Id is
1644 Outer : Entity_Id := E;
1647 while Present (Outer) loop
1648 if Suppress_Elaboration_Checks (Outer) then
1652 exit when Is_Child_Unit (Outer)
1653 or else Scope (Outer) = Standard_Standard
1654 or else Ekind (Outer) /= E_Package;
1655 Outer := Scope (Outer);
1661 -- Start of processing for Check_Task_Activation
1664 Enclosing := Outer_Unit (Current_Scope);
1666 -- Find all tasks declared in the current unit.
1668 if Nkind (N) = N_Package_Body then
1669 P := Unit_Declaration_Node (Corresponding_Spec (N));
1671 Collect_Tasks (Declarations (N));
1672 Collect_Tasks (Visible_Declarations (Specification (P)));
1673 Collect_Tasks (Private_Declarations (Specification (P)));
1675 elsif Nkind (N) = N_Package_Declaration then
1676 Collect_Tasks (Visible_Declarations (Specification (N)));
1677 Collect_Tasks (Private_Declarations (Specification (N)));
1680 Collect_Tasks (Declarations (N));
1683 -- We only perform detailed checks in all tasks are library level
1684 -- entities. If the master is a subprogram or task, activation will
1685 -- depend on the activation of the master itself.
1686 -- Should dynamic checks be added in the more general case???
1688 if Ekind (Enclosing) /= E_Package then
1692 -- For task types defined in other units, we want the unit containing
1693 -- the task body to be elaborated before the current one.
1695 Elmt := First_Elmt (Inter_Procs);
1697 while Present (Elmt) loop
1699 Task_Scope := Outer_Unit (Scope (Ent));
1701 if not Is_Compilation_Unit (Task_Scope) then
1704 elsif Suppress_Elaboration_Warnings (Task_Scope) then
1707 elsif Dynamic_Elaboration_Checks then
1708 if not Elaboration_Checks_Suppressed (Ent)
1709 and then not Cunit_SC
1710 and then not Restrictions (No_Entry_Calls_In_Elaboration_Code)
1712 -- Runtime elaboration check required. generate check of the
1713 -- elaboration Boolean for the unit containing the entity.
1715 Insert_Elab_Check (N,
1716 Make_Attribute_Reference (Loc,
1717 Attribute_Name => Name_Elaborated,
1720 (Spec_Entity (Task_Scope), Loc)));
1724 -- Force the binder to elaborate other unit first.
1726 if not Suppress_Elaboration_Warnings (Ent)
1727 and then Elab_Warnings
1728 and then not Suppress_Elaboration_Warnings (Task_Scope)
1730 Error_Msg_Node_2 := Task_Scope;
1731 Error_Msg_NE ("activation of an instance of task type&" &
1732 " requires pragma Elaborate_All on &?", N, Ent);
1735 Set_Elaborate_All_Desirable (Task_Scope);
1736 Set_Suppress_Elaboration_Warnings (Task_Scope);
1742 -- For tasks declared in the current unit, trace other calls within
1743 -- the task procedure bodies, which are available.
1745 In_Task_Activation := True;
1746 Elmt := First_Elmt (Intra_Procs);
1748 while Present (Elmt) loop
1750 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent);
1754 In_Task_Activation := False;
1755 end Check_Task_Activation;
1757 ----------------------
1758 -- Has_Generic_Body --
1759 ----------------------
1761 function Has_Generic_Body (N : Node_Id) return Boolean is
1762 Ent : constant Entity_Id := Entity (Name (N));
1763 Decl : constant Node_Id := Unit_Declaration_Node (Ent);
1766 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id;
1767 -- Determine if the list of nodes headed by N and linked by Next
1768 -- contains a package body for the package spec entity E, and if
1769 -- so return the package body. If not, then returns Empty.
1771 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id;
1772 -- This procedure is called load the unit whose name is given by Nam.
1773 -- This unit is being loaded to see whether it contains an optional
1774 -- generic body. The returned value is the loaded unit, which is
1775 -- always a package body (only package bodies can contain other
1776 -- entities in the sense in which Has_Generic_Body is interested).
1777 -- We only attempt to load bodies if we are generating code. If we
1778 -- are in semantics check only mode, then it would be wrong to load
1779 -- bodies that are not required from a semantic point of view, so
1780 -- in this case we return Empty. The result is that the caller may
1781 -- incorrectly decide that a generic spec does not have a body when
1782 -- in fact it does, but the only harm in this is that some warnings
1783 -- on elaboration problems may be lost in semantic checks only mode,
1784 -- which is not big loss. We also return Empty if we go for a body
1785 -- and it is not there.
1787 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id;
1788 -- PE is the entity for a package spec. This function locates the
1789 -- corresponding package body, returning Empty if none is found.
1790 -- The package body returned is fully parsed but may not yet be
1791 -- analyzed, so only syntactic fields should be referenced.
1797 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is
1802 while Present (Nod) loop
1804 -- If we found the package body we are looking for, return it
1806 if Nkind (Nod) = N_Package_Body
1807 and then Chars (Defining_Unit_Name (Nod)) = Chars (E)
1811 -- If we found the stub for the body, go after the subunit,
1812 -- loading it if necessary.
1814 elsif Nkind (Nod) = N_Package_Body_Stub
1815 and then Chars (Defining_Identifier (Nod)) = Chars (E)
1817 if Present (Library_Unit (Nod)) then
1818 return Unit (Library_Unit (Nod));
1821 return Load_Package_Body (Get_Unit_Name (Nod));
1824 -- If neither package body nor stub, keep looking on chain
1834 -----------------------
1835 -- Load_Package_Body --
1836 -----------------------
1838 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is
1839 U : Unit_Number_Type;
1842 if Operating_Mode /= Generate_Code then
1855 return Unit (Cunit (U));
1858 end Load_Package_Body;
1860 -------------------------------
1861 -- Locate_Corresponding_Body --
1862 -------------------------------
1864 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is
1865 Spec : constant Node_Id := Declaration_Node (PE);
1866 Decl : constant Node_Id := Parent (Spec);
1867 Scop : constant Entity_Id := Scope (PE);
1871 if Is_Library_Level_Entity (PE) then
1873 -- If package is a library unit that requires a body, we have
1874 -- no choice but to go after that body because it might contain
1875 -- an optional body for the original generic package.
1877 if Unit_Requires_Body (PE) then
1879 -- Load the body. Note that we are a little careful here to
1880 -- use Spec to get the unit number, rather than PE or Decl,
1881 -- since in the case where the package is itself a library
1882 -- level instantiation, Spec will properly reference the
1883 -- generic template, which is what we really want.
1887 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec))));
1889 -- But if the package is a library unit that does NOT require
1890 -- a body, then no body is permitted, so we are sure that there
1891 -- is no body for the original generic package.
1897 -- Otherwise look and see if we are embedded in a further package
1899 elsif Is_Package (Scop) then
1901 -- If so, get the body of the enclosing package, and look in
1902 -- its package body for the package body we are looking for.
1904 PBody := Locate_Corresponding_Body (Scop);
1909 return Find_Body_In (PE, First (Declarations (PBody)));
1912 -- If we are not embedded in a further package, then the body
1913 -- must be in the same declarative part as we are.
1916 return Find_Body_In (PE, Next (Decl));
1918 end Locate_Corresponding_Body;
1920 -- Start of processing for Has_Generic_Body
1923 if Present (Corresponding_Body (Decl)) then
1926 elsif Unit_Requires_Body (Ent) then
1929 -- Compilation units cannot have optional bodies
1931 elsif Is_Compilation_Unit (Ent) then
1934 -- Otherwise look at what scope we are in
1937 Scop := Scope (Ent);
1939 -- Case of entity is in other than a package spec, in this case
1940 -- the body, if present, must be in the same declarative part.
1942 if not Is_Package (Scop) then
1947 P := Declaration_Node (Ent);
1949 -- Declaration node may get us a spec, so if so, go to
1950 -- the parent declaration.
1952 while not Is_List_Member (P) loop
1956 return Present (Find_Body_In (Ent, Next (P)));
1959 -- If the entity is in a package spec, then we have to locate
1960 -- the corresponding package body, and look there.
1964 PBody : constant Node_Id := Locate_Corresponding_Body (Scop);
1972 (Find_Body_In (Ent, (First (Declarations (PBody)))));
1977 end Has_Generic_Body;
1979 -----------------------
1980 -- Insert_Elab_Check --
1981 -----------------------
1983 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is
1985 Loc : constant Source_Ptr := Sloc (N);
1988 -- If expansion is disabled, do not generate any checks. Also
1989 -- skip checks if any subunits are missing because in either
1990 -- case we lack the full information that we need, and no object
1991 -- file will be created in any case.
1993 if not Expander_Active or else Subunits_Missing then
1997 -- If we have a generic instantiation, where Instance_Spec is set,
1998 -- then this field points to a generic instance spec that has
1999 -- been inserted before the instantiation node itself, so that
2000 -- is where we want to insert a check.
2002 if Nkind (N) in N_Generic_Instantiation
2003 and then Present (Instance_Spec (N))
2005 Nod := Instance_Spec (N);
2010 -- If we are inserting at the top level, insert in Aux_Decls
2012 if Nkind (Parent (Nod)) = N_Compilation_Unit then
2014 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod));
2019 R := Make_Raise_Program_Error (Loc);
2021 R := Make_Raise_Program_Error (Loc, Make_Op_Not (Loc, C));
2024 if No (Declarations (ADN)) then
2025 Set_Declarations (ADN, New_List (R));
2027 Append_To (Declarations (ADN), R);
2033 -- Otherwise just insert before the node in question. However, if
2034 -- the context of the call has already been analyzed, an insertion
2035 -- will not work if it depends on subsequent expansion (e.g. a call in
2036 -- a branch of a short-circuit). In that case we replace the call with
2037 -- a conditional expression, or with a Raise if it is unconditional.
2038 -- Unfortunately this does not work if the call has a dynamic size,
2039 -- because gigi regards it as a dynamic-sized temporary. If such a call
2040 -- appears in a short-circuit expression, the elaboration check will be
2041 -- missed (rare enough ???).
2044 if Nkind (N) = N_Function_Call
2045 and then Analyzed (Parent (N))
2046 and then Size_Known_At_Compile_Time (Etype (N))
2049 Typ : constant Entity_Id := Etype (N);
2050 R : constant Node_Id := Make_Raise_Program_Error (Loc);
2051 Chk : constant Boolean := Do_Range_Check (N);
2061 Make_Conditional_Expression (Loc,
2062 Expressions => New_List (C, Relocate_Node (N), R)));
2065 Analyze_And_Resolve (N, Typ);
2067 -- If the original call requires a range check, so does the
2068 -- conditional expression.
2071 Enable_Range_Check (N);
2073 Set_Do_Range_Check (N, False);
2080 Make_Raise_Program_Error (Loc));
2083 Make_Raise_Program_Error (Loc,
2090 end Insert_Elab_Check;
2096 procedure Output_Calls (N : Node_Id) is
2099 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean;
2100 -- An internal function, used to determine if a name, Nm, is either
2101 -- a non-internal name, or is an internal name that is printable
2102 -- by the error message circuits (i.e. it has a single upper
2103 -- case letter at the end).
2105 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is
2107 if not Is_Internal_Name (Nm) then
2110 elsif Name_Len = 1 then
2114 Name_Len := Name_Len - 1;
2115 return not Is_Internal_Name;
2117 end Is_Printable_Error_Name;
2119 -- Start of processing for Output_Calls
2122 for J in reverse 1 .. Elab_Call.Last loop
2123 Error_Msg_Sloc := Elab_Call.Table (J).Cloc;
2125 Ent := Elab_Call.Table (J).Ent;
2127 if Is_Generic_Unit (Ent) then
2128 Error_Msg_NE ("\?& instantiated #", N, Ent);
2130 elsif Chars (Ent) = Name_uInit_Proc then
2131 Error_Msg_N ("\?initialization procedure called #", N);
2133 elsif Is_Printable_Error_Name (Chars (Ent)) then
2134 Error_Msg_NE ("\?& called #", N, Ent);
2137 Error_Msg_N ("\? called #", N);
2142 ----------------------------
2143 -- Same_Elaboration_Scope --
2144 ----------------------------
2146 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is
2147 S1 : Entity_Id := Scop1;
2148 S2 : Entity_Id := Scop2;
2151 while S1 /= Standard_Standard
2152 and then (Ekind (S1) = E_Package
2154 Ekind (S1) = E_Block)
2159 while S2 /= Standard_Standard
2160 and then (Ekind (S2) = E_Package
2162 Ekind (S2) = E_Protected_Type
2164 Ekind (S2) = E_Block)
2170 end Same_Elaboration_Scope;
2176 procedure Set_C_Scope is
2178 while not Is_Compilation_Unit (C_Scope) loop
2179 C_Scope := Scope (C_Scope);
2187 function Spec_Entity (E : Entity_Id) return Entity_Id is
2191 -- Check for case of body entity
2192 -- Why is the check for E_Void needed???
2194 if Ekind (E) = E_Void
2195 or else Ekind (E) = E_Subprogram_Body
2196 or else Ekind (E) = E_Package_Body
2201 Decl := Parent (Decl);
2202 exit when Nkind (Decl) in N_Proper_Body;
2205 return Corresponding_Spec (Decl);
2216 procedure Supply_Bodies (N : Node_Id) is
2218 if Nkind (N) = N_Subprogram_Declaration then
2220 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N));
2223 Set_Is_Imported (Ent);
2224 Set_Convention (Ent, Convention_Stubbed);
2227 elsif Nkind (N) = N_Package_Declaration then
2229 Spec : constant Node_Id := Specification (N);
2232 New_Scope (Defining_Unit_Name (Spec));
2233 Supply_Bodies (Visible_Declarations (Spec));
2234 Supply_Bodies (Private_Declarations (Spec));
2240 procedure Supply_Bodies (L : List_Id) is
2246 while Present (Elmt) loop
2247 Supply_Bodies (Elmt);
2257 function Within (E1, E2 : Entity_Id) return Boolean is
2267 elsif Scop = Standard_Standard then
2271 Scop := Scope (Scop);
2275 raise Program_Error;