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-2013, 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 3, 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 COPYING3. If not, go to -- 19-- http://www.gnu.org/licenses for a complete copy of the license. -- 20-- -- 21-- GNAT was originally developed by the GNAT team at New York University. -- 22-- Extensive contributions were provided by Ada Core Technologies Inc. -- 23-- -- 24------------------------------------------------------------------------------ 25 26with Atree; use Atree; 27with Checks; use Checks; 28with Debug; use Debug; 29with Einfo; use Einfo; 30with Elists; use Elists; 31with Errout; use Errout; 32with Exp_Tss; use Exp_Tss; 33with Exp_Util; use Exp_Util; 34with Expander; use Expander; 35with Fname; use Fname; 36with Lib; use Lib; 37with Lib.Load; use Lib.Load; 38with Namet; use Namet; 39with Nlists; use Nlists; 40with Nmake; use Nmake; 41with Opt; use Opt; 42with Output; use Output; 43with Restrict; use Restrict; 44with Rident; use Rident; 45with Sem; use Sem; 46with Sem_Aux; use Sem_Aux; 47with Sem_Cat; use Sem_Cat; 48with Sem_Ch7; use Sem_Ch7; 49with Sem_Ch8; use Sem_Ch8; 50with Sem_Util; use Sem_Util; 51with Sinfo; use Sinfo; 52with Sinput; use Sinput; 53with Snames; use Snames; 54with Stand; use Stand; 55with Table; 56with Tbuild; use Tbuild; 57with Uintp; use Uintp; 58with Uname; use Uname; 59 60package body Sem_Elab is 61 62 -- The following table records the recursive call chain for output in the 63 -- Output routine. Each entry records the call node and the entity of the 64 -- called routine. The number of entries in the table (i.e. the value of 65 -- Elab_Call.Last) indicates the current depth of recursion and is used to 66 -- identify the outer level. 67 68 type Elab_Call_Entry is record 69 Cloc : Source_Ptr; 70 Ent : Entity_Id; 71 end record; 72 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"); 80 81 -- This table is initialized at the start of each outer level call. It 82 -- holds the entities for all subprograms that have been examined for this 83 -- particular outer level call, and is used to prevent both infinite 84 -- recursion, and useless reanalysis of bodies already seen 85 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"); 93 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. 98 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). 104 105 E : Entity_Id; 106 -- The parameter E from the call to Check_Internal_Call 107 108 Orig_Ent : Entity_Id; 109 -- The parameter Orig_Ent from the call to Check_Internal_Call 110 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. 114 115 From_Elab_Code : Boolean; 116 -- Save indication of whether this call is from elaboration code 117 118 Outer_Scope : Entity_Id; 119 -- Save scope of outer level call 120 end record; 121 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"); 129 130 C_Scope : Entity_Id; 131 -- Top level scope of current scope. Compute this only once at the outer 132 -- level, i.e. for a call to Check_Elab_Call from outside this unit. 133 134 Outer_Level_Sloc : Source_Ptr; 135 -- Save Sloc value for outer level call node for comparisons of source 136 -- locations. A body is too late if it appears after the *outer* level 137 -- call, not the particular call that is being analyzed. 138 139 From_Elab_Code : Boolean; 140 -- This flag shows whether the outer level call currently being examined 141 -- is or is not in elaboration code. We are only interested in calls to 142 -- routines in other units if this flag is True. 143 144 In_Task_Activation : Boolean := False; 145 -- This flag indicates whether we are performing elaboration checks on 146 -- task procedures, at the point of activation. If true, we do not trace 147 -- internal calls in these procedures, because all local bodies are known 148 -- to be elaborated. 149 150 Delaying_Elab_Checks : Boolean := True; 151 -- This is set True till the compilation is complete, including the 152 -- insertion of all instance bodies. Then when Check_Elab_Calls is called, 153 -- the delay table is used to make the delayed calls and this flag is reset 154 -- to False, so that the calls are processed. 155 156 ----------------------- 157 -- Local Subprograms -- 158 ----------------------- 159 160 -- Note: Outer_Scope in all following specs represents the scope of 161 -- interest of the outer level call. If it is set to Standard_Standard, 162 -- then it means the outer level call was at elaboration level, and that 163 -- thus all calls are of interest. If it was set to some other scope, 164 -- then the original call was an inner call, and we are not interested 165 -- in calls that go outside this scope. 166 167 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id); 168 -- Analysis of construct N shows that we should set Elaborate_All_Desirable 169 -- for the WITH clause for unit U (which will always be present). A special 170 -- case is when N is a function or procedure instantiation, in which case 171 -- it is sufficient to set Elaborate_Desirable, since in this case there is 172 -- no possibility of transitive elaboration issues. 173 174 procedure Check_A_Call 175 (N : Node_Id; 176 E : Entity_Id; 177 Outer_Scope : Entity_Id; 178 Inter_Unit_Only : Boolean; 179 Generate_Warnings : Boolean := True; 180 In_Init_Proc : Boolean := False); 181 -- This is the internal recursive routine that is called to check for 182 -- possible elaboration error. The argument N is a subprogram call or 183 -- generic instantiation, or 'Access attribute reference to be checked, and 184 -- E is the entity of the called subprogram, or instantiated generic unit, 185 -- or subprogram referenced by 'Access. 186 -- 187 -- The flag Outer_Scope is the outer level scope for the original call. 188 -- Inter_Unit_Only is set if the call is only to be checked in the 189 -- case where it is to another unit (and skipped if within a unit). 190 -- Generate_Warnings is set to False to suppress warning messages about 191 -- missing pragma Elaborate_All's. These messages are not wanted for 192 -- inner calls in the dynamic model. Note that an instance of the Access 193 -- attribute applied to a subprogram also generates a call to this 194 -- procedure (since the referenced subprogram may be called later 195 -- indirectly). Flag In_Init_Proc should be set whenever the current 196 -- context is a type init proc. 197 198 procedure Check_Bad_Instantiation (N : Node_Id); 199 -- N is a node for an instantiation (if called with any other node kind, 200 -- Check_Bad_Instantiation ignores the call). This subprogram checks for 201 -- the special case of a generic instantiation of a generic spec in the 202 -- same declarative part as the instantiation where a body is present and 203 -- has not yet been seen. This is an obvious error, but needs to be checked 204 -- specially at the time of the instantiation, since it is a case where we 205 -- cannot insert the body anywhere. If this case is detected, warnings are 206 -- generated, and a raise of Program_Error is inserted. In addition any 207 -- subprograms in the generic spec are stubbed, and the Bad_Instantiation 208 -- flag is set on the instantiation node. The caller in Sem_Ch12 uses this 209 -- flag as an indication that no attempt should be made to insert an 210 -- instance body. 211 212 procedure Check_Internal_Call 213 (N : Node_Id; 214 E : Entity_Id; 215 Outer_Scope : Entity_Id; 216 Orig_Ent : Entity_Id); 217 -- N is a function call or procedure statement call node and E is the 218 -- entity of the called function, which is within the current compilation 219 -- unit (where subunits count as part of the parent). This call checks if 220 -- this call, or any call within any accessed body could cause an ABE, and 221 -- if so, outputs a warning. Orig_Ent differs from E only in the case of 222 -- renamings, and points to the original name of the entity. This is used 223 -- for error messages. Outer_Scope is the outer level scope for the 224 -- original call. 225 226 procedure Check_Internal_Call_Continue 227 (N : Node_Id; 228 E : Entity_Id; 229 Outer_Scope : Entity_Id; 230 Orig_Ent : Entity_Id); 231 -- The processing for Check_Internal_Call is divided up into two phases, 232 -- and this represents the second phase. The second phase is delayed if 233 -- Delaying_Elab_Calls is set to True. In this delayed case, the first 234 -- phase makes an entry in the Delay_Check table, which is processed when 235 -- Check_Elab_Calls is called. N, E and Orig_Ent are as for the call to 236 -- Check_Internal_Call. Outer_Scope is the outer level scope for the 237 -- original call. 238 239 function Has_Generic_Body (N : Node_Id) return Boolean; 240 -- N is a generic package instantiation node, and this routine determines 241 -- if this package spec does in fact have a generic body. If so, then 242 -- True is returned, otherwise False. Note that this is not at all the 243 -- same as checking if the unit requires a body, since it deals with 244 -- the case of optional bodies accurately (i.e. if a body is optional, 245 -- then it looks to see if a body is actually present). Note: this 246 -- function can only do a fully correct job if in generating code mode 247 -- where all bodies have to be present. If we are operating in semantics 248 -- check only mode, then in some cases of optional bodies, a result of 249 -- False may incorrectly be given. In practice this simply means that 250 -- some cases of warnings for incorrect order of elaboration will only 251 -- be given when generating code, which is not a big problem (and is 252 -- inevitable, given the optional body semantics of Ada). 253 254 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty); 255 -- Given code for an elaboration check (or unconditional raise if the check 256 -- is not needed), inserts the code in the appropriate place. N is the call 257 -- or instantiation node for which the check code is required. C is the 258 -- test whose failure triggers the raise. 259 260 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean; 261 -- Determine whether entity Id denotes a [Deep_]Finalize procedure 262 263 procedure Output_Calls (N : Node_Id); 264 -- Outputs chain of calls stored in the Elab_Call table. The caller has 265 -- already generated the main warning message, so the warnings generated 266 -- are all continuation messages. The argument is the call node at which 267 -- the messages are to be placed. 268 269 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean; 270 -- Given two scopes, determine whether they are the same scope from an 271 -- elaboration point of view, i.e. packages and blocks are ignored. 272 273 procedure Set_C_Scope; 274 -- On entry C_Scope is set to some scope. On return, C_Scope is reset 275 -- to be the enclosing compilation unit of this scope. 276 277 function Get_Referenced_Ent (N : Node_Id) return Entity_Id; 278 -- N is either a function or procedure call or an access attribute that 279 -- references a subprogram. This call retrieves the relevant entity. If 280 -- this is a call to a protected subprogram, the entity is a selected 281 -- component. The callable entity may be absent, in which case Empty is 282 -- returned. This happens with non-analyzed calls in nested generics. 283 284 procedure Set_Elaboration_Constraint 285 (Call : Node_Id; 286 Subp : Entity_Id; 287 Scop : Entity_Id); 288 -- The current unit U may depend semantically on some unit P which is not 289 -- in the current context. If there is an elaboration call that reaches P, 290 -- we need to indicate that P requires an Elaborate_All, but this is not 291 -- effective in U's ali file, if there is no with_clause for P. In this 292 -- case we add the Elaborate_All on the unit Q that directly or indirectly 293 -- makes P available. This can happen in two cases: 294 -- 295 -- a) Q declares a subtype of a type declared in P, and the call is an 296 -- initialization call for an object of that subtype. 297 -- 298 -- b) Q declares an object of some tagged type whose root type is 299 -- declared in P, and the initialization call uses object notation on 300 -- that object to reach a primitive operation or a classwide operation 301 -- declared in P. 302 -- 303 -- If P appears in the context of U, the current processing is correct. 304 -- Otherwise we must identify these two cases to retrieve Q and place the 305 -- Elaborate_All_Desirable on it. 306 307 function Spec_Entity (E : Entity_Id) return Entity_Id; 308 -- Given a compilation unit entity, if it is a spec entity, it is returned 309 -- unchanged. If it is a body entity, then the spec for the corresponding 310 -- spec is returned 311 312 procedure Supply_Bodies (N : Node_Id); 313 -- Given a node, N, that is either a subprogram declaration or a package 314 -- declaration, this procedure supplies dummy bodies for the subprogram 315 -- or for all subprograms in the package. If the given node is not one 316 -- of these two possibilities, then Supply_Bodies does nothing. The 317 -- dummy body contains a single Raise statement. 318 319 procedure Supply_Bodies (L : List_Id); 320 -- Calls Supply_Bodies for all elements of the given list L 321 322 function Within (E1, E2 : Entity_Id) return Boolean; 323 -- Given two scopes E1 and E2, returns True if E1 is equal to E2, or is one 324 -- of its contained scopes, False otherwise. 325 326 function Within_Elaborate_All 327 (Unit : Unit_Number_Type; 328 E : Entity_Id) return Boolean; 329 -- Return True if we are within the scope of an Elaborate_All for E, or if 330 -- we are within the scope of an Elaborate_All for some other unit U, and U 331 -- with's E. This prevents spurious warnings when the called entity is 332 -- renamed within U, or in case of generic instances. 333 334 -------------------------------------- 335 -- Activate_Elaborate_All_Desirable -- 336 -------------------------------------- 337 338 procedure Activate_Elaborate_All_Desirable (N : Node_Id; U : Entity_Id) is 339 UN : constant Unit_Number_Type := Get_Code_Unit (N); 340 CU : constant Node_Id := Cunit (UN); 341 UE : constant Entity_Id := Cunit_Entity (UN); 342 Unm : constant Unit_Name_Type := Unit_Name (UN); 343 CI : constant List_Id := Context_Items (CU); 344 Itm : Node_Id; 345 Ent : Entity_Id; 346 347 procedure Add_To_Context_And_Mark (Itm : Node_Id); 348 -- This procedure is called when the elaborate indication must be 349 -- applied to a unit not in the context of the referencing unit. The 350 -- unit gets added to the context as an implicit with. 351 352 function In_Withs_Of (UEs : Entity_Id) return Boolean; 353 -- UEs is the spec entity of a unit. If the unit to be marked is 354 -- in the context item list of this unit spec, then the call returns 355 -- True and Itm is left set to point to the relevant N_With_Clause node. 356 357 procedure Set_Elab_Flag (Itm : Node_Id); 358 -- Sets Elaborate_[All_]Desirable as appropriate on Itm 359 360 ----------------------------- 361 -- Add_To_Context_And_Mark -- 362 ----------------------------- 363 364 procedure Add_To_Context_And_Mark (Itm : Node_Id) is 365 CW : constant Node_Id := 366 Make_With_Clause (Sloc (Itm), 367 Name => Name (Itm)); 368 369 begin 370 Set_Library_Unit (CW, Library_Unit (Itm)); 371 Set_Implicit_With (CW, True); 372 373 -- Set elaborate all desirable on copy and then append the copy to 374 -- the list of body with's and we are done. 375 376 Set_Elab_Flag (CW); 377 Append_To (CI, CW); 378 end Add_To_Context_And_Mark; 379 380 ----------------- 381 -- In_Withs_Of -- 382 ----------------- 383 384 function In_Withs_Of (UEs : Entity_Id) return Boolean is 385 UNs : constant Unit_Number_Type := Get_Source_Unit (UEs); 386 CUs : constant Node_Id := Cunit (UNs); 387 CIs : constant List_Id := Context_Items (CUs); 388 389 begin 390 Itm := First (CIs); 391 while Present (Itm) loop 392 if Nkind (Itm) = N_With_Clause then 393 Ent := 394 Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm))); 395 396 if U = Ent then 397 return True; 398 end if; 399 end if; 400 401 Next (Itm); 402 end loop; 403 404 return False; 405 end In_Withs_Of; 406 407 ------------------- 408 -- Set_Elab_Flag -- 409 ------------------- 410 411 procedure Set_Elab_Flag (Itm : Node_Id) is 412 begin 413 if Nkind (N) in N_Subprogram_Instantiation then 414 Set_Elaborate_Desirable (Itm); 415 else 416 Set_Elaborate_All_Desirable (Itm); 417 end if; 418 end Set_Elab_Flag; 419 420 -- Start of processing for Activate_Elaborate_All_Desirable 421 422 begin 423 -- Do not set binder indication if expansion is disabled, as when 424 -- compiling a generic unit. 425 426 if not Expander_Active then 427 return; 428 end if; 429 430 Itm := First (CI); 431 while Present (Itm) loop 432 if Nkind (Itm) = N_With_Clause then 433 Ent := Cunit_Entity (Get_Cunit_Unit_Number (Library_Unit (Itm))); 434 435 -- If we find it, then mark elaborate all desirable and return 436 437 if U = Ent then 438 Set_Elab_Flag (Itm); 439 return; 440 end if; 441 end if; 442 443 Next (Itm); 444 end loop; 445 446 -- If we fall through then the with clause is not present in the 447 -- current unit. One legitimate possibility is that the with clause 448 -- is present in the spec when we are a body. 449 450 if Is_Body_Name (Unm) 451 and then In_Withs_Of (Spec_Entity (UE)) 452 then 453 Add_To_Context_And_Mark (Itm); 454 return; 455 end if; 456 457 -- Similarly, we may be in the spec or body of a child unit, where 458 -- the unit in question is with'ed by some ancestor of the child unit. 459 460 if Is_Child_Name (Unm) then 461 declare 462 Pkg : Entity_Id; 463 464 begin 465 Pkg := UE; 466 loop 467 Pkg := Scope (Pkg); 468 exit when Pkg = Standard_Standard; 469 470 if In_Withs_Of (Pkg) then 471 Add_To_Context_And_Mark (Itm); 472 return; 473 end if; 474 end loop; 475 end; 476 end if; 477 478 -- Here if we do not find with clause on spec or body. We just ignore 479 -- this case, it means that the elaboration involves some other unit 480 -- than the unit being compiled, and will be caught elsewhere. 481 482 null; 483 end Activate_Elaborate_All_Desirable; 484 485 ------------------ 486 -- Check_A_Call -- 487 ------------------ 488 489 procedure Check_A_Call 490 (N : Node_Id; 491 E : Entity_Id; 492 Outer_Scope : Entity_Id; 493 Inter_Unit_Only : Boolean; 494 Generate_Warnings : Boolean := True; 495 In_Init_Proc : Boolean := False) 496 is 497 Loc : constant Source_Ptr := Sloc (N); 498 Ent : Entity_Id; 499 Decl : Node_Id; 500 501 E_Scope : Entity_Id; 502 -- Top level scope of entity for called subprogram. This value includes 503 -- following renamings and derivations, so this scope can be in a 504 -- non-visible unit. This is the scope that is to be investigated to 505 -- see whether an elaboration check is required. 506 507 W_Scope : Entity_Id; 508 -- Top level scope of directly called entity for subprogram. This 509 -- differs from E_Scope in the case where renamings or derivations 510 -- are involved, since it does not follow these links. W_Scope is 511 -- generally in a visible unit, and it is this scope that may require 512 -- an Elaborate_All. However, there are some cases (initialization 513 -- calls and calls involving object notation) where W_Scope might not 514 -- be in the context of the current unit, and there is an intermediate 515 -- package that is, in which case the Elaborate_All has to be placed 516 -- on this intermediate package. These special cases are handled in 517 -- Set_Elaboration_Constraint. 518 519 Body_Acts_As_Spec : Boolean; 520 -- Set to true if call is to body acting as spec (no separate spec) 521 522 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation; 523 -- Indicates if we have instantiation case 524 525 Access_Case : constant Boolean := Nkind (N) = N_Attribute_Reference; 526 -- Indicates if we have Access attribute case 527 528 Caller_Unit_Internal : Boolean; 529 Callee_Unit_Internal : Boolean; 530 531 Inst_Caller : Source_Ptr; 532 Inst_Callee : Source_Ptr; 533 534 Unit_Caller : Unit_Number_Type; 535 Unit_Callee : Unit_Number_Type; 536 537 Cunit_SC : Boolean := False; 538 -- Set to suppress dynamic elaboration checks where one of the 539 -- enclosing scopes has Elaboration_Checks_Suppressed set, or else 540 -- if a pragma Elaborate (_All) applies to that scope, in which case 541 -- warnings on the scope are also suppressed. For the internal case, 542 -- we ignore this flag. 543 544 begin 545 -- If the call is known to be within a local Suppress Elaboration 546 -- pragma, nothing to check. This can happen in task bodies. 547 548 if Nkind (N) in N_Subprogram_Call 549 and then No_Elaboration_Check (N) 550 then 551 return; 552 end if; 553 554 -- Go to parent for derived subprogram, or to original subprogram in the 555 -- case of a renaming (Alias covers both these cases). 556 557 Ent := E; 558 loop 559 if (Suppress_Elaboration_Warnings (Ent) 560 or else Elaboration_Checks_Suppressed (Ent)) 561 and then (Inst_Case or else No (Alias (Ent))) 562 then 563 return; 564 end if; 565 566 -- Nothing to do for imported entities 567 568 if Is_Imported (Ent) then 569 return; 570 end if; 571 572 exit when Inst_Case or else No (Alias (Ent)); 573 Ent := Alias (Ent); 574 end loop; 575 576 Decl := Unit_Declaration_Node (Ent); 577 578 if Nkind (Decl) = N_Subprogram_Body then 579 Body_Acts_As_Spec := True; 580 581 elsif Nkind (Decl) = N_Subprogram_Declaration 582 or else Nkind (Decl) = N_Subprogram_Body_Stub 583 or else Inst_Case 584 then 585 Body_Acts_As_Spec := False; 586 587 -- If we have none of an instantiation, subprogram body or 588 -- subprogram declaration, then it is not a case that we want 589 -- to check. (One case is a call to a generic formal subprogram, 590 -- where we do not want the check in the template). 591 592 else 593 return; 594 end if; 595 596 E_Scope := Ent; 597 loop 598 if Elaboration_Checks_Suppressed (E_Scope) 599 or else Suppress_Elaboration_Warnings (E_Scope) 600 then 601 Cunit_SC := True; 602 end if; 603 604 -- Exit when we get to compilation unit, not counting subunits 605 606 exit when Is_Compilation_Unit (E_Scope) 607 and then (Is_Child_Unit (E_Scope) 608 or else Scope (E_Scope) = Standard_Standard); 609 610 -- If we did not find a compilation unit, other than standard, 611 -- then nothing to check (happens in some instantiation cases) 612 613 if E_Scope = Standard_Standard then 614 return; 615 616 -- Otherwise move up a scope looking for compilation unit 617 618 else 619 E_Scope := Scope (E_Scope); 620 end if; 621 end loop; 622 623 -- No checks needed for pure or preelaborated compilation units 624 625 if Is_Pure (E_Scope) or else Is_Preelaborated (E_Scope) then 626 return; 627 end if; 628 629 -- If the generic entity is within a deeper instance than we are, then 630 -- either the instantiation to which we refer itself caused an ABE, in 631 -- which case that will be handled separately, or else we know that the 632 -- body we need appears as needed at the point of the instantiation. 633 -- However, this assumption is only valid if we are in static mode. 634 635 if not Dynamic_Elaboration_Checks 636 and then Instantiation_Depth (Sloc (Ent)) > 637 Instantiation_Depth (Sloc (N)) 638 then 639 return; 640 end if; 641 642 -- Do not give a warning for a package with no body 643 644 if Ekind (Ent) = E_Generic_Package 645 and then not Has_Generic_Body (N) 646 then 647 return; 648 end if; 649 650 -- Case of entity is not in current unit (i.e. with'ed unit case) 651 652 if E_Scope /= C_Scope then 653 654 -- We are only interested in such calls if the outer call was from 655 -- elaboration code, or if we are in Dynamic_Elaboration_Checks mode. 656 657 if not From_Elab_Code and then not Dynamic_Elaboration_Checks then 658 return; 659 end if; 660 661 -- Nothing to do if some scope said that no checks were required 662 663 if Cunit_SC then 664 return; 665 end if; 666 667 -- Nothing to do for a generic instance, because in this case the 668 -- checking was at the point of instantiation of the generic However, 669 -- this shortcut is only applicable in static mode. 670 671 if Is_Generic_Instance (Ent) and not Dynamic_Elaboration_Checks then 672 return; 673 end if; 674 675 -- Nothing to do if subprogram with no separate spec. However, a 676 -- call to Deep_Initialize may result in a call to a user-defined 677 -- Initialize procedure, which imposes a body dependency. This 678 -- happens only if the type is controlled and the Initialize 679 -- procedure is not inherited. 680 681 if Body_Acts_As_Spec then 682 if Is_TSS (Ent, TSS_Deep_Initialize) then 683 declare 684 Typ : constant Entity_Id := Etype (First_Formal (Ent)); 685 Init : Entity_Id; 686 687 begin 688 if not Is_Controlled (Typ) then 689 return; 690 else 691 Init := Find_Prim_Op (Typ, Name_Initialize); 692 693 if Comes_From_Source (Init) then 694 Ent := Init; 695 else 696 return; 697 end if; 698 end if; 699 end; 700 701 else 702 return; 703 end if; 704 end if; 705 706 -- Check cases of internal units 707 708 Callee_Unit_Internal := 709 Is_Internal_File_Name 710 (Unit_File_Name (Get_Source_Unit (E_Scope))); 711 712 -- Do not give a warning if the with'ed unit is internal and this is 713 -- the generic instantiation case (this saves a lot of hassle dealing 714 -- with the Text_IO special child units) 715 716 if Callee_Unit_Internal and Inst_Case then 717 return; 718 end if; 719 720 if C_Scope = Standard_Standard then 721 Caller_Unit_Internal := False; 722 else 723 Caller_Unit_Internal := 724 Is_Internal_File_Name 725 (Unit_File_Name (Get_Source_Unit (C_Scope))); 726 end if; 727 728 -- Do not give a warning if the with'ed unit is internal and the 729 -- caller is not internal (since the binder always elaborates 730 -- internal units first). 731 732 if Callee_Unit_Internal and (not Caller_Unit_Internal) then 733 return; 734 end if; 735 736 -- For now, if debug flag -gnatdE is not set, do no checking for 737 -- one internal unit withing another. This fixes the problem with 738 -- the sgi build and storage errors. To be resolved later ??? 739 740 if (Callee_Unit_Internal and Caller_Unit_Internal) 741 and then not Debug_Flag_EE 742 then 743 return; 744 end if; 745 746 if Is_TSS (E, TSS_Deep_Initialize) then 747 Ent := E; 748 end if; 749 750 -- If the call is in an instance, and the called entity is not 751 -- defined in the same instance, then the elaboration issue focuses 752 -- around the unit containing the template, it is this unit which 753 -- requires an Elaborate_All. 754 755 -- However, if we are doing dynamic elaboration, we need to chase the 756 -- call in the usual manner. 757 758 -- We do not handle the case of calling a generic formal correctly in 759 -- the static case.??? 760 761 Inst_Caller := Instantiation (Get_Source_File_Index (Sloc (N))); 762 Inst_Callee := Instantiation (Get_Source_File_Index (Sloc (Ent))); 763 764 if Inst_Caller = No_Location then 765 Unit_Caller := No_Unit; 766 else 767 Unit_Caller := Get_Source_Unit (N); 768 end if; 769 770 if Inst_Callee = No_Location then 771 Unit_Callee := No_Unit; 772 else 773 Unit_Callee := Get_Source_Unit (Ent); 774 end if; 775 776 if Unit_Caller /= No_Unit 777 and then Unit_Callee /= Unit_Caller 778 and then not Dynamic_Elaboration_Checks 779 then 780 E_Scope := Spec_Entity (Cunit_Entity (Unit_Caller)); 781 782 -- If we don't get a spec entity, just ignore call. Not quite 783 -- clear why this check is necessary. ??? 784 785 if No (E_Scope) then 786 return; 787 end if; 788 789 -- Otherwise step to enclosing compilation unit 790 791 while not Is_Compilation_Unit (E_Scope) loop 792 E_Scope := Scope (E_Scope); 793 end loop; 794 795 -- For the case N is not an instance, or a call within instance, we 796 -- recompute E_Scope for the error message, since we do NOT want to 797 -- go to the unit which has the ultimate declaration in the case of 798 -- renaming and derivation and we also want to go to the generic unit 799 -- in the case of an instance, and no further. 800 801 else 802 -- Loop to carefully follow renamings and derivations one step 803 -- outside the current unit, but not further. 804 805 if not Inst_Case 806 and then Present (Alias (Ent)) 807 then 808 E_Scope := Alias (Ent); 809 else 810 E_Scope := Ent; 811 end if; 812 813 loop 814 while not Is_Compilation_Unit (E_Scope) loop 815 E_Scope := Scope (E_Scope); 816 end loop; 817 818 -- If E_Scope is the same as C_Scope, it means that there 819 -- definitely was a local renaming or derivation, and we 820 -- are not yet out of the current unit. 821 822 exit when E_Scope /= C_Scope; 823 Ent := Alias (Ent); 824 E_Scope := Ent; 825 826 -- If no alias, there is a previous error 827 828 if No (Ent) then 829 Check_Error_Detected; 830 return; 831 end if; 832 end loop; 833 end if; 834 835 if Within_Elaborate_All (Current_Sem_Unit, E_Scope) then 836 return; 837 end if; 838 839 -- Find top level scope for called entity (not following renamings 840 -- or derivations). This is where the Elaborate_All will go if it 841 -- is needed. We start with the called entity, except in the case 842 -- of an initialization procedure outside the current package, where 843 -- the init proc is in the root package, and we start from the entity 844 -- of the name in the call. 845 846 declare 847 Ent : constant Entity_Id := Get_Referenced_Ent (N); 848 begin 849 if Is_Init_Proc (Ent) 850 and then not In_Same_Extended_Unit (N, Ent) 851 then 852 W_Scope := Scope (Ent); 853 else 854 W_Scope := E; 855 end if; 856 end; 857 858 -- Now loop through scopes to get to the enclosing compilation unit 859 860 while not Is_Compilation_Unit (W_Scope) loop 861 W_Scope := Scope (W_Scope); 862 end loop; 863 864 -- Now check if an elaborate_all (or dynamic check) is needed 865 866 if not Suppress_Elaboration_Warnings (Ent) 867 and then not Elaboration_Checks_Suppressed (Ent) 868 and then not Suppress_Elaboration_Warnings (E_Scope) 869 and then not Elaboration_Checks_Suppressed (E_Scope) 870 and then (Elab_Warnings or Elab_Info_Messages) 871 and then Generate_Warnings 872 then 873 Generate_Elab_Warnings : declare 874 procedure Elab_Warning 875 (Msg_D : String; 876 Msg_S : String; 877 Ent : Node_Or_Entity_Id); 878 -- Generate a call to Error_Msg_NE with parameters Msg_D or 879 -- Msg_S (for dynamic or static elaboration model), N and Ent. 880 -- Msg_D is a real warning (output if Msg_D is non-null and 881 -- Elab_Warnings is set), Msg_S is an info message (output if 882 -- Elab_Info_Messages is set. 883 884 ------------------ 885 -- Elab_Warning -- 886 ------------------ 887 888 procedure Elab_Warning 889 (Msg_D : String; 890 Msg_S : String; 891 Ent : Node_Or_Entity_Id) 892 is 893 begin 894 -- Dynamic elaboration checks, real warning 895 896 if Dynamic_Elaboration_Checks then 897 if not Access_Case then 898 if Msg_D /= "" and then Elab_Warnings then 899 Error_Msg_NE (Msg_D, N, Ent); 900 end if; 901 end if; 902 903 -- Static elaboration checks, info message 904 905 else 906 if Elab_Info_Messages then 907 Error_Msg_NE (Msg_S, N, Ent); 908 end if; 909 end if; 910 end Elab_Warning; 911 912 -- Start of processing for Generate_Elab_Warnings 913 914 begin 915 -- Instantiation case 916 917 if Inst_Case then 918 Elab_Warning 919 ("instantiation of& may raise Program_Error?l?", 920 "info: instantiation of& during elaboration?", Ent); 921 922 -- Indirect call case, info message only in static elaboration 923 -- case, because the attribute reference itself cannot raise 924 -- an exception. 925 926 elsif Access_Case then 927 Elab_Warning 928 ("", "info: access to& during elaboration?", Ent); 929 930 -- Subprogram call case 931 932 else 933 if Nkind (Name (N)) in N_Has_Entity 934 and then Is_Init_Proc (Entity (Name (N))) 935 and then Comes_From_Source (Ent) 936 then 937 Elab_Warning 938 ("implicit call to & may raise Program_Error?l?", 939 "info: implicit call to & during elaboration?", 940 Ent); 941 942 else 943 Elab_Warning 944 ("call to & may raise Program_Error?l?", 945 "info: call to & during elaboration?", 946 Ent); 947 end if; 948 end if; 949 950 Error_Msg_Qual_Level := Nat'Last; 951 952 if Nkind (N) in N_Subprogram_Instantiation then 953 Elab_Warning 954 ("\missing pragma Elaborate for&?l?", 955 "\info: implicit pragma Elaborate for& generated?", 956 W_Scope); 957 958 else 959 Elab_Warning 960 ("\missing pragma Elaborate_All for&?l?", 961 "\info: implicit pragma Elaborate_All for & generated?", 962 W_Scope); 963 end if; 964 end Generate_Elab_Warnings; 965 966 Error_Msg_Qual_Level := 0; 967 Output_Calls (N); 968 969 -- Set flag to prevent further warnings for same unit unless in 970 -- All_Errors_Mode. 971 972 if not All_Errors_Mode and not Dynamic_Elaboration_Checks then 973 Set_Suppress_Elaboration_Warnings (W_Scope, True); 974 end if; 975 end if; 976 977 -- Check for runtime elaboration check required 978 979 if Dynamic_Elaboration_Checks then 980 if not Elaboration_Checks_Suppressed (Ent) 981 and then not Elaboration_Checks_Suppressed (W_Scope) 982 and then not Elaboration_Checks_Suppressed (E_Scope) 983 and then not Cunit_SC 984 then 985 -- Runtime elaboration check required. Generate check of the 986 -- elaboration Boolean for the unit containing the entity. 987 988 -- Note that for this case, we do check the real unit (the one 989 -- from following renamings, since that is the issue). 990 991 -- Could this possibly miss a useless but required PE??? 992 993 Insert_Elab_Check (N, 994 Make_Attribute_Reference (Loc, 995 Attribute_Name => Name_Elaborated, 996 Prefix => 997 New_Occurrence_Of (Spec_Entity (E_Scope), Loc))); 998 999 -- Prevent duplicate elaboration checks on the same call, 1000 -- which can happen if the body enclosing the call appears 1001 -- itself in a call whose elaboration check is delayed. 1002 1003 if Nkind (N) in N_Subprogram_Call then 1004 Set_No_Elaboration_Check (N); 1005 end if; 1006 end if; 1007 1008 -- Case of static elaboration model 1009 1010 else 1011 -- Do not do anything if elaboration checks suppressed. Note that 1012 -- we check Ent here, not E, since we want the real entity for the 1013 -- body to see if checks are suppressed for it, not the dummy 1014 -- entry for renamings or derivations. 1015 1016 if Elaboration_Checks_Suppressed (Ent) 1017 or else Elaboration_Checks_Suppressed (E_Scope) 1018 or else Elaboration_Checks_Suppressed (W_Scope) 1019 then 1020 null; 1021 1022 -- Do not generate an Elaborate_All for finalization routines 1023 -- which perform partial clean up as part of initialization. 1024 1025 elsif In_Init_Proc and then Is_Finalization_Procedure (Ent) then 1026 null; 1027 1028 -- Here we need to generate an implicit elaborate all 1029 1030 else 1031 -- Generate Elaborate_all warning unless suppressed 1032 1033 if (Elab_Info_Messages and Generate_Warnings and not Inst_Case) 1034 and then not Suppress_Elaboration_Warnings (Ent) 1035 and then not Suppress_Elaboration_Warnings (E_Scope) 1036 and then not Suppress_Elaboration_Warnings (W_Scope) 1037 then 1038 Error_Msg_Node_2 := W_Scope; 1039 Error_Msg_NE 1040 ("info: call to& in elaboration code " & 1041 "requires pragma Elaborate_All on&?", N, E); 1042 end if; 1043 1044 -- Set indication for binder to generate Elaborate_All 1045 1046 Set_Elaboration_Constraint (N, E, W_Scope); 1047 end if; 1048 end if; 1049 1050 -- Case of entity is in same unit as call or instantiation 1051 1052 elsif not Inter_Unit_Only then 1053 Check_Internal_Call (N, Ent, Outer_Scope, E); 1054 end if; 1055 end Check_A_Call; 1056 1057 ----------------------------- 1058 -- Check_Bad_Instantiation -- 1059 ----------------------------- 1060 1061 procedure Check_Bad_Instantiation (N : Node_Id) is 1062 Ent : Entity_Id; 1063 1064 begin 1065 -- Nothing to do if we do not have an instantiation (happens in some 1066 -- error cases, and also in the formal package declaration case) 1067 1068 if Nkind (N) not in N_Generic_Instantiation then 1069 return; 1070 1071 -- Nothing to do if serious errors detected (avoid cascaded errors) 1072 1073 elsif Serious_Errors_Detected /= 0 then 1074 return; 1075 1076 -- Nothing to do if not in full analysis mode 1077 1078 elsif not Full_Analysis then 1079 return; 1080 1081 -- Nothing to do if inside a generic template 1082 1083 elsif Inside_A_Generic then 1084 return; 1085 1086 -- Nothing to do if a library level instantiation 1087 1088 elsif Nkind (Parent (N)) = N_Compilation_Unit then 1089 return; 1090 1091 -- Nothing to do if we are compiling a proper body for semantic 1092 -- purposes only. The generic body may be in another proper body. 1093 1094 elsif 1095 Nkind (Parent (Unit_Declaration_Node (Main_Unit_Entity))) = N_Subunit 1096 then 1097 return; 1098 end if; 1099 1100 Ent := Get_Generic_Entity (N); 1101 1102 -- The case we are interested in is when the generic spec is in the 1103 -- current declarative part 1104 1105 if not Same_Elaboration_Scope (Current_Scope, Scope (Ent)) 1106 or else not In_Same_Extended_Unit (N, Ent) 1107 then 1108 return; 1109 end if; 1110 1111 -- If the generic entity is within a deeper instance than we are, then 1112 -- either the instantiation to which we refer itself caused an ABE, in 1113 -- which case that will be handled separately. Otherwise, we know that 1114 -- the body we need appears as needed at the point of the instantiation. 1115 -- If they are both at the same level but not within the same instance 1116 -- then the body of the generic will be in the earlier instance. 1117 1118 declare 1119 D1 : constant Int := Instantiation_Depth (Sloc (Ent)); 1120 D2 : constant Int := Instantiation_Depth (Sloc (N)); 1121 1122 begin 1123 if D1 > D2 then 1124 return; 1125 1126 elsif D1 = D2 1127 and then Is_Generic_Instance (Scope (Ent)) 1128 and then not In_Open_Scopes (Scope (Ent)) 1129 then 1130 return; 1131 end if; 1132 end; 1133 1134 -- Now we can proceed, if the entity being called has a completion, 1135 -- then we are definitely OK, since we have already seen the body. 1136 1137 if Has_Completion (Ent) then 1138 return; 1139 end if; 1140 1141 -- If there is no body, then nothing to do 1142 1143 if not Has_Generic_Body (N) then 1144 return; 1145 end if; 1146 1147 -- Here we definitely have a bad instantiation 1148 1149 Error_Msg_Warn := SPARK_Mode /= On; 1150 Error_Msg_NE ("cannot instantiate& before body seen<<", N, Ent); 1151 1152 if Present (Instance_Spec (N)) then 1153 Supply_Bodies (Instance_Spec (N)); 1154 end if; 1155 1156 Error_Msg_N ("\Program_Error [<<", N); 1157 Insert_Elab_Check (N); 1158 Set_ABE_Is_Certain (N); 1159 end Check_Bad_Instantiation; 1160 1161 --------------------- 1162 -- Check_Elab_Call -- 1163 --------------------- 1164 1165 procedure Check_Elab_Call 1166 (N : Node_Id; 1167 Outer_Scope : Entity_Id := Empty; 1168 In_Init_Proc : Boolean := False) 1169 is 1170 Ent : Entity_Id; 1171 P : Node_Id; 1172 1173 begin 1174 -- If the call does not come from the main unit, there is nothing to 1175 -- check. Elaboration call from units in the context of the main unit 1176 -- will lead to semantic dependencies when those units are compiled. 1177 1178 if not In_Extended_Main_Code_Unit (N) then 1179 return; 1180 end if; 1181 1182 -- For an entry call, check relevant restriction 1183 1184 if Nkind (N) = N_Entry_Call_Statement 1185 and then not In_Subprogram_Or_Concurrent_Unit 1186 then 1187 Check_Restriction (No_Entry_Calls_In_Elaboration_Code, N); 1188 1189 -- Nothing to do if this is not a call or attribute reference (happens 1190 -- in some error conditions, and in some cases where rewriting occurs). 1191 1192 elsif Nkind (N) not in N_Subprogram_Call 1193 and then Nkind (N) /= N_Attribute_Reference 1194 then 1195 return; 1196 1197 -- Nothing to do if this is a call already rewritten for elab checking 1198 1199 elsif Nkind (Parent (N)) = N_If_Expression then 1200 return; 1201 1202 -- Nothing to do if inside a generic template 1203 1204 elsif Inside_A_Generic 1205 and then No (Enclosing_Generic_Body (N)) 1206 then 1207 return; 1208 end if; 1209 1210 -- Here we have a call at elaboration time which must be checked 1211 1212 if Debug_Flag_LL then 1213 Write_Str (" Check_Elab_Call: "); 1214 1215 if Nkind (N) = N_Attribute_Reference then 1216 if not Is_Entity_Name (Prefix (N)) then 1217 Write_Str ("<<not entity name>>"); 1218 else 1219 Write_Name (Chars (Entity (Prefix (N)))); 1220 end if; 1221 Write_Str ("'Access"); 1222 1223 elsif No (Name (N)) or else not Is_Entity_Name (Name (N)) then 1224 Write_Str ("<<not entity name>> "); 1225 1226 else 1227 Write_Name (Chars (Entity (Name (N)))); 1228 end if; 1229 1230 Write_Str (" call at "); 1231 Write_Location (Sloc (N)); 1232 Write_Eol; 1233 end if; 1234 1235 -- Climb up the tree to make sure we are not inside default expression 1236 -- of a parameter specification or a record component, since in both 1237 -- these cases, we will be doing the actual call later, not now, and it 1238 -- is at the time of the actual call (statically speaking) that we must 1239 -- do our static check, not at the time of its initial analysis). 1240 1241 -- However, we have to check calls within component definitions (e.g. 1242 -- a function call that determines an array component bound), so we 1243 -- terminate the loop in that case. 1244 1245 P := Parent (N); 1246 while Present (P) loop 1247 if Nkind_In (P, N_Parameter_Specification, 1248 N_Component_Declaration) 1249 then 1250 return; 1251 1252 -- The call occurs within the constraint of a component, 1253 -- so it must be checked. 1254 1255 elsif Nkind (P) = N_Component_Definition then 1256 exit; 1257 1258 else 1259 P := Parent (P); 1260 end if; 1261 end loop; 1262 1263 -- Stuff that happens only at the outer level 1264 1265 if No (Outer_Scope) then 1266 Elab_Visited.Set_Last (0); 1267 1268 -- Nothing to do if current scope is Standard (this is a bit odd, but 1269 -- it happens in the case of generic instantiations). 1270 1271 C_Scope := Current_Scope; 1272 1273 if C_Scope = Standard_Standard then 1274 return; 1275 end if; 1276 1277 -- First case, we are in elaboration code 1278 1279 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit; 1280 if From_Elab_Code then 1281 1282 -- Complain if call that comes from source in preelaborated unit 1283 -- and we are not inside a subprogram (i.e. we are in elab code). 1284 1285 if Comes_From_Source (N) 1286 and then In_Preelaborated_Unit 1287 and then not In_Inlined_Body 1288 and then Nkind (N) /= N_Attribute_Reference 1289 then 1290 -- This is a warning in GNAT mode allowing such calls to be 1291 -- used in the predefined library with appropriate care. 1292 1293 Error_Msg_Warn := GNAT_Mode; 1294 Error_Msg_N 1295 ("<non-static call not allowed in preelaborated unit", N); 1296 return; 1297 end if; 1298 1299 -- Second case, we are inside a subprogram or concurrent unit, which 1300 -- means we are not in elaboration code. 1301 1302 else 1303 -- In this case, the issue is whether we are inside the 1304 -- declarative part of the unit in which we live, or inside its 1305 -- statements. In the latter case, there is no issue of ABE calls 1306 -- at this level (a call from outside to the unit in which we live 1307 -- might cause an ABE, but that will be detected when we analyze 1308 -- that outer level call, as it recurses into the called unit). 1309 1310 -- Climb up the tree, doing this test, and also testing for being 1311 -- inside a default expression, which, as discussed above, is not 1312 -- checked at this stage. 1313 1314 declare 1315 P : Node_Id; 1316 L : List_Id; 1317 1318 begin 1319 P := N; 1320 loop 1321 -- If we find a parentless subtree, it seems safe to assume 1322 -- that we are not in a declarative part and that no 1323 -- checking is required. 1324 1325 if No (P) then 1326 return; 1327 end if; 1328 1329 if Is_List_Member (P) then 1330 L := List_Containing (P); 1331 P := Parent (L); 1332 else 1333 L := No_List; 1334 P := Parent (P); 1335 end if; 1336 1337 exit when Nkind (P) = N_Subunit; 1338 1339 -- Filter out case of default expressions, where we do not 1340 -- do the check at this stage. 1341 1342 if Nkind (P) = N_Parameter_Specification 1343 or else 1344 Nkind (P) = N_Component_Declaration 1345 then 1346 return; 1347 end if; 1348 1349 -- A protected body has no elaboration code and contains 1350 -- only other bodies. 1351 1352 if Nkind (P) = N_Protected_Body then 1353 return; 1354 1355 elsif Nkind (P) = N_Subprogram_Body 1356 or else 1357 Nkind (P) = N_Task_Body 1358 or else 1359 Nkind (P) = N_Block_Statement 1360 or else 1361 Nkind (P) = N_Entry_Body 1362 then 1363 if L = Declarations (P) then 1364 exit; 1365 1366 -- We are not in elaboration code, but we are doing 1367 -- dynamic elaboration checks, in this case, we still 1368 -- need to do the call, since the subprogram we are in 1369 -- could be called from another unit, also in dynamic 1370 -- elaboration check mode, at elaboration time. 1371 1372 elsif Dynamic_Elaboration_Checks then 1373 1374 -- We provide a debug flag to disable this check. That 1375 -- way we have an easy work around for regressions 1376 -- that are caused by this new check. This debug flag 1377 -- can be removed later. 1378 1379 if Debug_Flag_DD then 1380 return; 1381 end if; 1382 1383 -- Do the check in this case 1384 1385 exit; 1386 1387 elsif Nkind (P) = N_Task_Body then 1388 1389 -- The check is deferred until Check_Task_Activation 1390 -- but we need to capture local suppress pragmas 1391 -- that may inhibit checks on this call. 1392 1393 Ent := Get_Referenced_Ent (N); 1394 1395 if No (Ent) then 1396 return; 1397 1398 elsif Elaboration_Checks_Suppressed (Current_Scope) 1399 or else Elaboration_Checks_Suppressed (Ent) 1400 or else Elaboration_Checks_Suppressed (Scope (Ent)) 1401 then 1402 Set_No_Elaboration_Check (N); 1403 end if; 1404 1405 return; 1406 1407 -- Static model, call is not in elaboration code, we 1408 -- never need to worry, because in the static model the 1409 -- top level caller always takes care of things. 1410 1411 else 1412 return; 1413 end if; 1414 end if; 1415 end loop; 1416 end; 1417 end if; 1418 end if; 1419 1420 Ent := Get_Referenced_Ent (N); 1421 1422 if No (Ent) then 1423 return; 1424 end if; 1425 1426 -- Nothing to do if this is a recursive call (i.e. a call to 1427 -- an entity that is already in the Elab_Call stack) 1428 1429 for J in 1 .. Elab_Visited.Last loop 1430 if Ent = Elab_Visited.Table (J) then 1431 return; 1432 end if; 1433 end loop; 1434 1435 -- See if we need to analyze this call. We analyze it if either of 1436 -- the following conditions is met: 1437 1438 -- It is an inner level call (since in this case it was triggered 1439 -- by an outer level call from elaboration code), but only if the 1440 -- call is within the scope of the original outer level call. 1441 1442 -- It is an outer level call from elaboration code, or the called 1443 -- entity is in the same elaboration scope. 1444 1445 -- And in these cases, we will check both inter-unit calls and 1446 -- intra-unit (within a single unit) calls. 1447 1448 C_Scope := Current_Scope; 1449 1450 -- If not outer level call, then we follow it if it is within the 1451 -- original scope of the outer call. 1452 1453 if Present (Outer_Scope) 1454 and then Within (Scope (Ent), Outer_Scope) 1455 then 1456 Set_C_Scope; 1457 Check_A_Call 1458 (N => N, 1459 E => Ent, 1460 Outer_Scope => Outer_Scope, 1461 Inter_Unit_Only => False, 1462 In_Init_Proc => In_Init_Proc); 1463 1464 elsif Elaboration_Checks_Suppressed (Current_Scope) then 1465 null; 1466 1467 elsif From_Elab_Code then 1468 Set_C_Scope; 1469 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False); 1470 1471 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then 1472 Set_C_Scope; 1473 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False); 1474 1475 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode 1476 -- is set, then we will do the check, but only in the inter-unit case 1477 -- (this is to accommodate unguarded elaboration calls from other units 1478 -- in which this same mode is set). We don't want warnings in this case, 1479 -- it would generate warnings having nothing to do with elaboration. 1480 1481 elsif Dynamic_Elaboration_Checks then 1482 Set_C_Scope; 1483 Check_A_Call 1484 (N, 1485 Ent, 1486 Standard_Standard, 1487 Inter_Unit_Only => True, 1488 Generate_Warnings => False); 1489 1490 -- Otherwise nothing to do 1491 1492 else 1493 return; 1494 end if; 1495 1496 -- A call to an Init_Proc in elaboration code may bring additional 1497 -- dependencies, if some of the record components thereof have 1498 -- initializations that are function calls that come from source. We 1499 -- treat the current node as a call to each of these functions, to check 1500 -- their elaboration impact. 1501 1502 if Is_Init_Proc (Ent) 1503 and then From_Elab_Code 1504 then 1505 Process_Init_Proc : declare 1506 Unit_Decl : constant Node_Id := Unit_Declaration_Node (Ent); 1507 1508 function Check_Init_Call (Nod : Node_Id) return Traverse_Result; 1509 -- Find subprogram calls within body of Init_Proc for Traverse 1510 -- instantiation below. 1511 1512 procedure Traverse_Body is new Traverse_Proc (Check_Init_Call); 1513 -- Traversal procedure to find all calls with body of Init_Proc 1514 1515 --------------------- 1516 -- Check_Init_Call -- 1517 --------------------- 1518 1519 function Check_Init_Call (Nod : Node_Id) return Traverse_Result is 1520 Func : Entity_Id; 1521 1522 begin 1523 if Nkind (Nod) in N_Subprogram_Call 1524 and then Is_Entity_Name (Name (Nod)) 1525 then 1526 Func := Entity (Name (Nod)); 1527 1528 if Comes_From_Source (Func) then 1529 Check_A_Call 1530 (N, Func, Standard_Standard, Inter_Unit_Only => True); 1531 end if; 1532 1533 return OK; 1534 1535 else 1536 return OK; 1537 end if; 1538 end Check_Init_Call; 1539 1540 -- Start of processing for Process_Init_Proc 1541 1542 begin 1543 if Nkind (Unit_Decl) = N_Subprogram_Body then 1544 Traverse_Body (Handled_Statement_Sequence (Unit_Decl)); 1545 end if; 1546 end Process_Init_Proc; 1547 end if; 1548 end Check_Elab_Call; 1549 1550 ----------------------- 1551 -- Check_Elab_Assign -- 1552 ----------------------- 1553 1554 procedure Check_Elab_Assign (N : Node_Id) is 1555 Ent : Entity_Id; 1556 Scop : Entity_Id; 1557 1558 Pkg_Spec : Entity_Id; 1559 Pkg_Body : Entity_Id; 1560 1561 begin 1562 -- For record or array component, check prefix. If it is an access type, 1563 -- then there is nothing to do (we do not know what is being assigned), 1564 -- but otherwise this is an assignment to the prefix. 1565 1566 if Nkind_In (N, N_Indexed_Component, 1567 N_Selected_Component, 1568 N_Slice) 1569 then 1570 if not Is_Access_Type (Etype (Prefix (N))) then 1571 Check_Elab_Assign (Prefix (N)); 1572 end if; 1573 1574 return; 1575 end if; 1576 1577 -- For type conversion, check expression 1578 1579 if Nkind (N) = N_Type_Conversion then 1580 Check_Elab_Assign (Expression (N)); 1581 return; 1582 end if; 1583 1584 -- Nothing to do if this is not an entity reference otherwise get entity 1585 1586 if Is_Entity_Name (N) then 1587 Ent := Entity (N); 1588 else 1589 return; 1590 end if; 1591 1592 -- What we are looking for is a reference in the body of a package that 1593 -- modifies a variable declared in the visible part of the package spec. 1594 1595 if Present (Ent) 1596 and then Comes_From_Source (N) 1597 and then not Suppress_Elaboration_Warnings (Ent) 1598 and then Ekind (Ent) = E_Variable 1599 and then not In_Private_Part (Ent) 1600 and then Is_Library_Level_Entity (Ent) 1601 then 1602 Scop := Current_Scope; 1603 loop 1604 if No (Scop) or else Scop = Standard_Standard then 1605 return; 1606 elsif Ekind (Scop) = E_Package 1607 and then Is_Compilation_Unit (Scop) 1608 then 1609 exit; 1610 else 1611 Scop := Scope (Scop); 1612 end if; 1613 end loop; 1614 1615 -- Here Scop points to the containing library package 1616 1617 Pkg_Spec := Scop; 1618 Pkg_Body := Body_Entity (Pkg_Spec); 1619 1620 -- All OK if the package has an Elaborate_Body pragma 1621 1622 if Has_Pragma_Elaborate_Body (Scop) then 1623 return; 1624 end if; 1625 1626 -- OK if entity being modified is not in containing package spec 1627 1628 if not In_Same_Source_Unit (Scop, Ent) then 1629 return; 1630 end if; 1631 1632 -- All OK if entity appears in generic package or generic instance. 1633 -- We just get too messed up trying to give proper warnings in the 1634 -- presence of generics. Better no message than a junk one. 1635 1636 Scop := Scope (Ent); 1637 while Present (Scop) and then Scop /= Pkg_Spec loop 1638 if Ekind (Scop) = E_Generic_Package then 1639 return; 1640 elsif Ekind (Scop) = E_Package 1641 and then Is_Generic_Instance (Scop) 1642 then 1643 return; 1644 end if; 1645 1646 Scop := Scope (Scop); 1647 end loop; 1648 1649 -- All OK if in task, don't issue warnings there 1650 1651 if In_Task_Activation then 1652 return; 1653 end if; 1654 1655 -- OK if no package body 1656 1657 if No (Pkg_Body) then 1658 return; 1659 end if; 1660 1661 -- OK if reference is not in package body 1662 1663 if not In_Same_Source_Unit (Pkg_Body, N) then 1664 return; 1665 end if; 1666 1667 -- OK if package body has no handled statement sequence 1668 1669 declare 1670 HSS : constant Node_Id := 1671 Handled_Statement_Sequence (Declaration_Node (Pkg_Body)); 1672 begin 1673 if No (HSS) or else not Comes_From_Source (HSS) then 1674 return; 1675 end if; 1676 end; 1677 1678 -- We definitely have a case of a modification of an entity in 1679 -- the package spec from the elaboration code of the package body. 1680 -- We may not give the warning (because there are some additional 1681 -- checks to avoid too many false positives), but it would be a good 1682 -- idea for the binder to try to keep the body elaboration close to 1683 -- the spec elaboration. 1684 1685 Set_Elaborate_Body_Desirable (Pkg_Spec); 1686 1687 -- All OK in gnat mode (we know what we are doing) 1688 1689 if GNAT_Mode then 1690 return; 1691 end if; 1692 1693 -- All OK if all warnings suppressed 1694 1695 if Warning_Mode = Suppress then 1696 return; 1697 end if; 1698 1699 -- All OK if elaboration checks suppressed for entity 1700 1701 if Checks_May_Be_Suppressed (Ent) 1702 and then Is_Check_Suppressed (Ent, Elaboration_Check) 1703 then 1704 return; 1705 end if; 1706 1707 -- OK if the entity is initialized. Note that the No_Initialization 1708 -- flag usually means that the initialization has been rewritten into 1709 -- assignments, but that still counts for us. 1710 1711 declare 1712 Decl : constant Node_Id := Declaration_Node (Ent); 1713 begin 1714 if Nkind (Decl) = N_Object_Declaration 1715 and then (Present (Expression (Decl)) 1716 or else No_Initialization (Decl)) 1717 then 1718 return; 1719 end if; 1720 end; 1721 1722 -- Here is where we give the warning 1723 1724 -- All OK if warnings suppressed on the entity 1725 1726 if not Has_Warnings_Off (Ent) then 1727 Error_Msg_Sloc := Sloc (Ent); 1728 1729 Error_Msg_NE 1730 ("??& can be accessed by clients before this initialization", 1731 N, Ent); 1732 Error_Msg_NE 1733 ("\??add Elaborate_Body to spec to ensure & is initialized", 1734 N, Ent); 1735 end if; 1736 1737 if not All_Errors_Mode then 1738 Set_Suppress_Elaboration_Warnings (Ent); 1739 end if; 1740 end if; 1741 end Check_Elab_Assign; 1742 1743 ---------------------- 1744 -- Check_Elab_Calls -- 1745 ---------------------- 1746 1747 procedure Check_Elab_Calls is 1748 begin 1749 -- If expansion is disabled, do not generate any checks. Also skip 1750 -- checks if any subunits are missing because in either case we lack the 1751 -- full information that we need, and no object file will be created in 1752 -- any case. 1753 1754 if not Expander_Active 1755 or else Is_Generic_Unit (Cunit_Entity (Main_Unit)) 1756 or else Subunits_Missing 1757 then 1758 return; 1759 end if; 1760 1761 -- Skip delayed calls if we had any errors 1762 1763 if Serious_Errors_Detected = 0 then 1764 Delaying_Elab_Checks := False; 1765 Expander_Mode_Save_And_Set (True); 1766 1767 for J in Delay_Check.First .. Delay_Check.Last loop 1768 Push_Scope (Delay_Check.Table (J).Curscop); 1769 From_Elab_Code := Delay_Check.Table (J).From_Elab_Code; 1770 1771 Check_Internal_Call_Continue ( 1772 N => Delay_Check.Table (J).N, 1773 E => Delay_Check.Table (J).E, 1774 Outer_Scope => Delay_Check.Table (J).Outer_Scope, 1775 Orig_Ent => Delay_Check.Table (J).Orig_Ent); 1776 1777 Pop_Scope; 1778 end loop; 1779 1780 -- Set Delaying_Elab_Checks back on for next main compilation 1781 1782 Expander_Mode_Restore; 1783 Delaying_Elab_Checks := True; 1784 end if; 1785 end Check_Elab_Calls; 1786 1787 ------------------------------ 1788 -- Check_Elab_Instantiation -- 1789 ------------------------------ 1790 1791 procedure Check_Elab_Instantiation 1792 (N : Node_Id; 1793 Outer_Scope : Entity_Id := Empty) 1794 is 1795 Ent : Entity_Id; 1796 1797 begin 1798 -- Check for and deal with bad instantiation case. There is some 1799 -- duplicated code here, but we will worry about this later ??? 1800 1801 Check_Bad_Instantiation (N); 1802 1803 if ABE_Is_Certain (N) then 1804 return; 1805 end if; 1806 1807 -- Nothing to do if we do not have an instantiation (happens in some 1808 -- error cases, and also in the formal package declaration case) 1809 1810 if Nkind (N) not in N_Generic_Instantiation then 1811 return; 1812 end if; 1813 1814 -- Nothing to do if inside a generic template 1815 1816 if Inside_A_Generic then 1817 return; 1818 end if; 1819 1820 -- Nothing to do if the instantiation is not in the main unit 1821 1822 if not In_Extended_Main_Code_Unit (N) then 1823 return; 1824 end if; 1825 1826 Ent := Get_Generic_Entity (N); 1827 From_Elab_Code := not In_Subprogram_Or_Concurrent_Unit; 1828 1829 -- See if we need to analyze this instantiation. We analyze it if 1830 -- either of the following conditions is met: 1831 1832 -- It is an inner level instantiation (since in this case it was 1833 -- triggered by an outer level call from elaboration code), but 1834 -- only if the instantiation is within the scope of the original 1835 -- outer level call. 1836 1837 -- It is an outer level instantiation from elaboration code, or the 1838 -- instantiated entity is in the same elaboration scope. 1839 1840 -- And in these cases, we will check both the inter-unit case and 1841 -- the intra-unit (within a single unit) case. 1842 1843 C_Scope := Current_Scope; 1844 1845 if Present (Outer_Scope) 1846 and then Within (Scope (Ent), Outer_Scope) 1847 then 1848 Set_C_Scope; 1849 Check_A_Call (N, Ent, Outer_Scope, Inter_Unit_Only => False); 1850 1851 elsif From_Elab_Code then 1852 Set_C_Scope; 1853 Check_A_Call (N, Ent, Standard_Standard, Inter_Unit_Only => False); 1854 1855 elsif Same_Elaboration_Scope (C_Scope, Scope (Ent)) then 1856 Set_C_Scope; 1857 Check_A_Call (N, Ent, Scope (Ent), Inter_Unit_Only => False); 1858 1859 -- If none of those cases holds, but Dynamic_Elaboration_Checks mode is 1860 -- set, then we will do the check, but only in the inter-unit case (this 1861 -- is to accommodate unguarded elaboration calls from other units in 1862 -- which this same mode is set). We inhibit warnings in this case, since 1863 -- this instantiation is not occurring in elaboration code. 1864 1865 elsif Dynamic_Elaboration_Checks then 1866 Set_C_Scope; 1867 Check_A_Call 1868 (N, 1869 Ent, 1870 Standard_Standard, 1871 Inter_Unit_Only => True, 1872 Generate_Warnings => False); 1873 1874 else 1875 return; 1876 end if; 1877 end Check_Elab_Instantiation; 1878 1879 ------------------------- 1880 -- Check_Internal_Call -- 1881 ------------------------- 1882 1883 procedure Check_Internal_Call 1884 (N : Node_Id; 1885 E : Entity_Id; 1886 Outer_Scope : Entity_Id; 1887 Orig_Ent : Entity_Id) 1888 is 1889 Inst_Case : constant Boolean := Nkind (N) in N_Generic_Instantiation; 1890 1891 begin 1892 -- If not function or procedure call or instantiation, then ignore 1893 -- call (this happens in some error cases and rewriting cases). 1894 1895 if not Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) 1896 and then not Inst_Case 1897 then 1898 return; 1899 1900 -- Nothing to do if this is a call or instantiation that has already 1901 -- been found to be a sure ABE. 1902 1903 elsif ABE_Is_Certain (N) then 1904 return; 1905 1906 -- Nothing to do if errors already detected (avoid cascaded errors) 1907 1908 elsif Serious_Errors_Detected /= 0 then 1909 return; 1910 1911 -- Nothing to do if not in full analysis mode 1912 1913 elsif not Full_Analysis then 1914 return; 1915 1916 -- Nothing to do if analyzing in special spec-expression mode, since the 1917 -- call is not actually being made at this time. 1918 1919 elsif In_Spec_Expression then 1920 return; 1921 1922 -- Nothing to do for call to intrinsic subprogram 1923 1924 elsif Is_Intrinsic_Subprogram (E) then 1925 return; 1926 1927 -- No need to trace local calls if checking task activation, because 1928 -- other local bodies are elaborated already. 1929 1930 elsif In_Task_Activation then 1931 return; 1932 1933 -- Nothing to do if call is within a generic unit 1934 1935 elsif Inside_A_Generic then 1936 return; 1937 end if; 1938 1939 -- Delay this call if we are still delaying calls 1940 1941 if Delaying_Elab_Checks then 1942 Delay_Check.Append ( 1943 (N => N, 1944 E => E, 1945 Orig_Ent => Orig_Ent, 1946 Curscop => Current_Scope, 1947 Outer_Scope => Outer_Scope, 1948 From_Elab_Code => From_Elab_Code)); 1949 return; 1950 1951 -- Otherwise, call phase 2 continuation right now 1952 1953 else 1954 Check_Internal_Call_Continue (N, E, Outer_Scope, Orig_Ent); 1955 end if; 1956 end Check_Internal_Call; 1957 1958 ---------------------------------- 1959 -- Check_Internal_Call_Continue -- 1960 ---------------------------------- 1961 1962 procedure Check_Internal_Call_Continue 1963 (N : Node_Id; 1964 E : Entity_Id; 1965 Outer_Scope : Entity_Id; 1966 Orig_Ent : Entity_Id) 1967 is 1968 Loc : constant Source_Ptr := Sloc (N); 1969 Inst_Case : constant Boolean := Is_Generic_Unit (E); 1970 1971 Sbody : Node_Id; 1972 Ebody : Entity_Id; 1973 1974 function Find_Elab_Reference (N : Node_Id) return Traverse_Result; 1975 -- Function applied to each node as we traverse the body. Checks for 1976 -- call or entity reference that needs checking, and if so checks it. 1977 -- Always returns OK, so entire tree is traversed, except that as 1978 -- described below subprogram bodies are skipped for now. 1979 1980 procedure Traverse is new Atree.Traverse_Proc (Find_Elab_Reference); 1981 -- Traverse procedure using above Find_Elab_Reference function 1982 1983 ------------------------- 1984 -- Find_Elab_Reference -- 1985 ------------------------- 1986 1987 function Find_Elab_Reference (N : Node_Id) return Traverse_Result is 1988 Actual : Node_Id; 1989 1990 begin 1991 -- If user has specified that there are no entry calls in elaboration 1992 -- code, do not trace past an accept statement, because the rendez- 1993 -- vous will happen after elaboration. 1994 1995 if (Nkind (Original_Node (N)) = N_Accept_Statement 1996 or else Nkind (Original_Node (N)) = N_Selective_Accept) 1997 and then Restriction_Active (No_Entry_Calls_In_Elaboration_Code) 1998 then 1999 return Abandon; 2000 2001 -- If we have a function call, check it 2002 2003 elsif Nkind (N) = N_Function_Call then 2004 Check_Elab_Call (N, Outer_Scope); 2005 return OK; 2006 2007 -- If we have a procedure call, check the call, and also check 2008 -- arguments that are assignments (OUT or IN OUT mode formals). 2009 2010 elsif Nkind (N) = N_Procedure_Call_Statement then 2011 Check_Elab_Call (N, Outer_Scope, In_Init_Proc => Is_Init_Proc (E)); 2012 2013 Actual := First_Actual (N); 2014 while Present (Actual) loop 2015 if Known_To_Be_Assigned (Actual) then 2016 Check_Elab_Assign (Actual); 2017 end if; 2018 2019 Next_Actual (Actual); 2020 end loop; 2021 2022 return OK; 2023 2024 -- If we have an access attribute for a subprogram, check 2025 -- it. Suppress this behavior under debug flag. 2026 2027 elsif not Debug_Flag_Dot_UU 2028 and then Nkind (N) = N_Attribute_Reference 2029 and then Nam_In (Attribute_Name (N), Name_Access, 2030 Name_Unrestricted_Access) 2031 and then Is_Entity_Name (Prefix (N)) 2032 and then Is_Subprogram (Entity (Prefix (N))) 2033 then 2034 Check_Elab_Call (N, Outer_Scope); 2035 return OK; 2036 2037 -- If we have a generic instantiation, check it 2038 2039 elsif Nkind (N) in N_Generic_Instantiation then 2040 Check_Elab_Instantiation (N, Outer_Scope); 2041 return OK; 2042 2043 -- Skip subprogram bodies that come from source (wait for call to 2044 -- analyze these). The reason for the come from source test is to 2045 -- avoid catching task bodies. 2046 2047 -- For task bodies, we should really avoid these too, waiting for the 2048 -- task activation, but that's too much trouble to catch for now, so 2049 -- we go in unconditionally. This is not so terrible, it means the 2050 -- error backtrace is not quite complete, and we are too eager to 2051 -- scan bodies of tasks that are unused, but this is hardly very 2052 -- significant. 2053 2054 elsif Nkind (N) = N_Subprogram_Body 2055 and then Comes_From_Source (N) 2056 then 2057 return Skip; 2058 2059 elsif Nkind (N) = N_Assignment_Statement 2060 and then Comes_From_Source (N) 2061 then 2062 Check_Elab_Assign (Name (N)); 2063 return OK; 2064 2065 else 2066 return OK; 2067 end if; 2068 end Find_Elab_Reference; 2069 2070 -- Start of processing for Check_Internal_Call_Continue 2071 2072 begin 2073 -- Save outer level call if at outer level 2074 2075 if Elab_Call.Last = 0 then 2076 Outer_Level_Sloc := Loc; 2077 end if; 2078 2079 Elab_Visited.Append (E); 2080 2081 -- If the call is to a function that renames a literal, no check needed 2082 2083 if Ekind (E) = E_Enumeration_Literal then 2084 return; 2085 end if; 2086 2087 Sbody := Unit_Declaration_Node (E); 2088 2089 if Nkind (Sbody) /= N_Subprogram_Body 2090 and then 2091 Nkind (Sbody) /= N_Package_Body 2092 then 2093 Ebody := Corresponding_Body (Sbody); 2094 2095 if No (Ebody) then 2096 return; 2097 else 2098 Sbody := Unit_Declaration_Node (Ebody); 2099 end if; 2100 end if; 2101 2102 -- If the body appears after the outer level call or instantiation then 2103 -- we have an error case handled below. 2104 2105 if Earlier_In_Extended_Unit (Outer_Level_Sloc, Sloc (Sbody)) 2106 and then not In_Task_Activation 2107 then 2108 null; 2109 2110 -- If we have the instantiation case we are done, since we now 2111 -- know that the body of the generic appeared earlier. 2112 2113 elsif Inst_Case then 2114 return; 2115 2116 -- Otherwise we have a call, so we trace through the called body to see 2117 -- if it has any problems. 2118 2119 else 2120 pragma Assert (Nkind (Sbody) = N_Subprogram_Body); 2121 2122 Elab_Call.Append ((Cloc => Loc, Ent => E)); 2123 2124 if Debug_Flag_LL then 2125 Write_Str ("Elab_Call.Last = "); 2126 Write_Int (Int (Elab_Call.Last)); 2127 Write_Str (" Ent = "); 2128 Write_Name (Chars (E)); 2129 Write_Str (" at "); 2130 Write_Location (Sloc (N)); 2131 Write_Eol; 2132 end if; 2133 2134 -- Now traverse declarations and statements of subprogram body. Note 2135 -- that we cannot simply Traverse (Sbody), since traverse does not 2136 -- normally visit subprogram bodies. 2137 2138 declare 2139 Decl : Node_Id; 2140 begin 2141 Decl := First (Declarations (Sbody)); 2142 while Present (Decl) loop 2143 Traverse (Decl); 2144 Next (Decl); 2145 end loop; 2146 end; 2147 2148 Traverse (Handled_Statement_Sequence (Sbody)); 2149 2150 Elab_Call.Decrement_Last; 2151 return; 2152 end if; 2153 2154 -- Here is the case of calling a subprogram where the body has not yet 2155 -- been encountered. A warning message is needed, except if this is the 2156 -- case of appearing within an aspect specification that results in 2157 -- a check call, we do not really have such a situation, so no warning 2158 -- is needed (e.g. the case of a precondition, where the call appears 2159 -- textually before the body, but in actual fact is moved to the 2160 -- appropriate subprogram body and so does not need a check). 2161 2162 declare 2163 P : Node_Id; 2164 O : Node_Id; 2165 2166 begin 2167 P := Parent (N); 2168 loop 2169 -- Keep looking at parents if we are still in the subexpression 2170 2171 if Nkind (P) in N_Subexpr then 2172 P := Parent (P); 2173 2174 -- Here P is the parent of the expression, check for special case 2175 2176 else 2177 O := Original_Node (P); 2178 2179 -- Definitely not the special case if orig node is not a pragma 2180 2181 exit when Nkind (O) /= N_Pragma; 2182 2183 -- Check we have an If statement or a null statement (happens 2184 -- when the If has been expanded to be True). 2185 2186 exit when not Nkind_In (P, N_If_Statement, N_Null_Statement); 2187 2188 -- Our special case will be indicated either by the pragma 2189 -- coming from an aspect ... 2190 2191 if Present (Corresponding_Aspect (O)) then 2192 return; 2193 2194 -- Or, in the case of an initial condition, specifically by a 2195 -- Check pragma specifying an Initial_Condition check. 2196 2197 elsif Pragma_Name (O) = Name_Check 2198 and then 2199 Chars 2200 (Expression (First (Pragma_Argument_Associations (O)))) = 2201 Name_Initial_Condition 2202 then 2203 return; 2204 2205 -- For anything else, we have an error 2206 2207 else 2208 exit; 2209 end if; 2210 end if; 2211 end loop; 2212 end; 2213 2214 -- Not that special case, warning and dynamic check is required 2215 2216 -- If we have nothing in the call stack, then this is at the outer 2217 -- level, and the ABE is bound to occur. 2218 2219 if Elab_Call.Last = 0 then 2220 Error_Msg_Warn := SPARK_Mode /= On; 2221 2222 if Inst_Case then 2223 Error_Msg_NE 2224 ("cannot instantiate& before body seen<<", N, Orig_Ent); 2225 else 2226 Error_Msg_NE 2227 ("cannot call& before body seen<<", N, Orig_Ent); 2228 end if; 2229 2230 Error_Msg_N ("\Program_Error [<<", N); 2231 Insert_Elab_Check (N); 2232 2233 -- Call is not at outer level 2234 2235 else 2236 -- Deal with dynamic elaboration check 2237 2238 if not Elaboration_Checks_Suppressed (E) then 2239 Set_Elaboration_Entity_Required (E); 2240 2241 -- Case of no elaboration entity allocated yet 2242 2243 if No (Elaboration_Entity (E)) then 2244 2245 -- Create object declaration for elaboration entity, and put it 2246 -- just in front of the spec of the subprogram or generic unit, 2247 -- in the same scope as this unit. 2248 2249 declare 2250 Loce : constant Source_Ptr := Sloc (E); 2251 Ent : constant Entity_Id := 2252 Make_Defining_Identifier (Loc, 2253 Chars => New_External_Name (Chars (E), 'E')); 2254 2255 begin 2256 Set_Elaboration_Entity (E, Ent); 2257 Push_Scope (Scope (E)); 2258 2259 Insert_Action (Declaration_Node (E), 2260 Make_Object_Declaration (Loce, 2261 Defining_Identifier => Ent, 2262 Object_Definition => 2263 New_Occurrence_Of (Standard_Short_Integer, Loce), 2264 Expression => 2265 Make_Integer_Literal (Loc, Uint_0))); 2266 2267 -- Set elaboration flag at the point of the body 2268 2269 Set_Elaboration_Flag (Sbody, E); 2270 2271 -- Kill current value indication. This is necessary because 2272 -- the tests of this flag are inserted out of sequence and 2273 -- must not pick up bogus indications of the wrong constant 2274 -- value. Also, this is never a true constant, since one way 2275 -- or another, it gets reset. 2276 2277 Set_Current_Value (Ent, Empty); 2278 Set_Last_Assignment (Ent, Empty); 2279 Set_Is_True_Constant (Ent, False); 2280 Pop_Scope; 2281 end; 2282 end if; 2283 2284 -- Generate check of the elaboration counter 2285 2286 Insert_Elab_Check (N, 2287 Make_Attribute_Reference (Loc, 2288 Attribute_Name => Name_Elaborated, 2289 Prefix => New_Occurrence_Of (E, Loc))); 2290 end if; 2291 2292 -- Generate the warning 2293 2294 if not Suppress_Elaboration_Warnings (E) 2295 and then not Elaboration_Checks_Suppressed (E) 2296 2297 -- Suppress this warning if we have a function call that occurred 2298 -- within an assertion expression, since we can get false warnings 2299 -- in this case, due to the out of order handling in this case. 2300 2301 and then 2302 (Nkind (Original_Node (N)) /= N_Function_Call 2303 or else not In_Assertion_Expression_Pragma (Original_Node (N))) 2304 then 2305 Error_Msg_Warn := SPARK_Mode /= On; 2306 2307 if Inst_Case then 2308 Error_Msg_NE 2309 ("instantiation of& may occur before body is seen<<", 2310 N, Orig_Ent); 2311 else 2312 Error_Msg_NE 2313 ("call to& may occur before body is seen<<", N, Orig_Ent); 2314 end if; 2315 2316 Error_Msg_N 2317 ("\Program_Error ]<<", N); 2318 2319 Output_Calls (N); 2320 end if; 2321 end if; 2322 2323 -- Set flag to suppress further warnings on same subprogram 2324 -- unless in all errors mode 2325 2326 if not All_Errors_Mode then 2327 Set_Suppress_Elaboration_Warnings (E); 2328 end if; 2329 end Check_Internal_Call_Continue; 2330 2331 --------------------------- 2332 -- Check_Task_Activation -- 2333 --------------------------- 2334 2335 procedure Check_Task_Activation (N : Node_Id) is 2336 Loc : constant Source_Ptr := Sloc (N); 2337 Inter_Procs : constant Elist_Id := New_Elmt_List; 2338 Intra_Procs : constant Elist_Id := New_Elmt_List; 2339 Ent : Entity_Id; 2340 P : Entity_Id; 2341 Task_Scope : Entity_Id; 2342 Cunit_SC : Boolean := False; 2343 Decl : Node_Id; 2344 Elmt : Elmt_Id; 2345 Enclosing : Entity_Id; 2346 2347 procedure Add_Task_Proc (Typ : Entity_Id); 2348 -- Add to Task_Procs the task body procedure(s) of task types in Typ. 2349 -- For record types, this procedure recurses over component types. 2350 2351 procedure Collect_Tasks (Decls : List_Id); 2352 -- Collect the types of the tasks that are to be activated in the given 2353 -- list of declarations, in order to perform elaboration checks on the 2354 -- corresponding task procedures which are called implicitly here. 2355 2356 function Outer_Unit (E : Entity_Id) return Entity_Id; 2357 -- find enclosing compilation unit of Entity, ignoring subunits, or 2358 -- else enclosing subprogram. If E is not a package, there is no need 2359 -- for inter-unit elaboration checks. 2360 2361 ------------------- 2362 -- Add_Task_Proc -- 2363 ------------------- 2364 2365 procedure Add_Task_Proc (Typ : Entity_Id) is 2366 Comp : Entity_Id; 2367 Proc : Entity_Id := Empty; 2368 2369 begin 2370 if Is_Task_Type (Typ) then 2371 Proc := Get_Task_Body_Procedure (Typ); 2372 2373 elsif Is_Array_Type (Typ) 2374 and then Has_Task (Base_Type (Typ)) 2375 then 2376 Add_Task_Proc (Component_Type (Typ)); 2377 2378 elsif Is_Record_Type (Typ) 2379 and then Has_Task (Base_Type (Typ)) 2380 then 2381 Comp := First_Component (Typ); 2382 while Present (Comp) loop 2383 Add_Task_Proc (Etype (Comp)); 2384 Comp := Next_Component (Comp); 2385 end loop; 2386 end if; 2387 2388 -- If the task type is another unit, we will perform the usual 2389 -- elaboration check on its enclosing unit. If the type is in the 2390 -- same unit, we can trace the task body as for an internal call, 2391 -- but we only need to examine other external calls, because at 2392 -- the point the task is activated, internal subprogram bodies 2393 -- will have been elaborated already. We keep separate lists for 2394 -- each kind of task. 2395 2396 -- Skip this test if errors have occurred, since in this case 2397 -- we can get false indications. 2398 2399 if Serious_Errors_Detected /= 0 then 2400 return; 2401 end if; 2402 2403 if Present (Proc) then 2404 if Outer_Unit (Scope (Proc)) = Enclosing then 2405 2406 if No (Corresponding_Body (Unit_Declaration_Node (Proc))) 2407 and then 2408 (not Is_Generic_Instance (Scope (Proc)) 2409 or else 2410 Scope (Proc) = Scope (Defining_Identifier (Decl))) 2411 then 2412 Error_Msg_Warn := SPARK_Mode /= On; 2413 Error_Msg_N 2414 ("task will be activated before elaboration of its body<<", 2415 Decl); 2416 Error_Msg_N ("\Program_Error [<<", Decl); 2417 2418 elsif 2419 Present (Corresponding_Body (Unit_Declaration_Node (Proc))) 2420 then 2421 Append_Elmt (Proc, Intra_Procs); 2422 end if; 2423 2424 else 2425 -- No need for multiple entries of the same type 2426 2427 Elmt := First_Elmt (Inter_Procs); 2428 while Present (Elmt) loop 2429 if Node (Elmt) = Proc then 2430 return; 2431 end if; 2432 2433 Next_Elmt (Elmt); 2434 end loop; 2435 2436 Append_Elmt (Proc, Inter_Procs); 2437 end if; 2438 end if; 2439 end Add_Task_Proc; 2440 2441 ------------------- 2442 -- Collect_Tasks -- 2443 ------------------- 2444 2445 procedure Collect_Tasks (Decls : List_Id) is 2446 begin 2447 if Present (Decls) then 2448 Decl := First (Decls); 2449 while Present (Decl) loop 2450 if Nkind (Decl) = N_Object_Declaration 2451 and then Has_Task (Etype (Defining_Identifier (Decl))) 2452 then 2453 Add_Task_Proc (Etype (Defining_Identifier (Decl))); 2454 end if; 2455 2456 Next (Decl); 2457 end loop; 2458 end if; 2459 end Collect_Tasks; 2460 2461 ---------------- 2462 -- Outer_Unit -- 2463 ---------------- 2464 2465 function Outer_Unit (E : Entity_Id) return Entity_Id is 2466 Outer : Entity_Id; 2467 2468 begin 2469 Outer := E; 2470 while Present (Outer) loop 2471 if Elaboration_Checks_Suppressed (Outer) then 2472 Cunit_SC := True; 2473 end if; 2474 2475 exit when Is_Child_Unit (Outer) 2476 or else Scope (Outer) = Standard_Standard 2477 or else Ekind (Outer) /= E_Package; 2478 Outer := Scope (Outer); 2479 end loop; 2480 2481 return Outer; 2482 end Outer_Unit; 2483 2484 -- Start of processing for Check_Task_Activation 2485 2486 begin 2487 Enclosing := Outer_Unit (Current_Scope); 2488 2489 -- Find all tasks declared in the current unit 2490 2491 if Nkind (N) = N_Package_Body then 2492 P := Unit_Declaration_Node (Corresponding_Spec (N)); 2493 2494 Collect_Tasks (Declarations (N)); 2495 Collect_Tasks (Visible_Declarations (Specification (P))); 2496 Collect_Tasks (Private_Declarations (Specification (P))); 2497 2498 elsif Nkind (N) = N_Package_Declaration then 2499 Collect_Tasks (Visible_Declarations (Specification (N))); 2500 Collect_Tasks (Private_Declarations (Specification (N))); 2501 2502 else 2503 Collect_Tasks (Declarations (N)); 2504 end if; 2505 2506 -- We only perform detailed checks in all tasks are library level 2507 -- entities. If the master is a subprogram or task, activation will 2508 -- depend on the activation of the master itself. 2509 2510 -- Should dynamic checks be added in the more general case??? 2511 2512 if Ekind (Enclosing) /= E_Package then 2513 return; 2514 end if; 2515 2516 -- For task types defined in other units, we want the unit containing 2517 -- the task body to be elaborated before the current one. 2518 2519 Elmt := First_Elmt (Inter_Procs); 2520 while Present (Elmt) loop 2521 Ent := Node (Elmt); 2522 Task_Scope := Outer_Unit (Scope (Ent)); 2523 2524 if not Is_Compilation_Unit (Task_Scope) then 2525 null; 2526 2527 elsif Suppress_Elaboration_Warnings (Task_Scope) 2528 or else Elaboration_Checks_Suppressed (Task_Scope) 2529 then 2530 null; 2531 2532 elsif Dynamic_Elaboration_Checks then 2533 if not Elaboration_Checks_Suppressed (Ent) 2534 and then not Cunit_SC 2535 and then 2536 not Restriction_Active (No_Entry_Calls_In_Elaboration_Code) 2537 then 2538 -- Runtime elaboration check required. Generate check of the 2539 -- elaboration counter for the unit containing the entity. 2540 2541 Insert_Elab_Check (N, 2542 Make_Attribute_Reference (Loc, 2543 Attribute_Name => Name_Elaborated, 2544 Prefix => 2545 New_Occurrence_Of (Spec_Entity (Task_Scope), Loc))); 2546 end if; 2547 2548 else 2549 -- Force the binder to elaborate other unit first 2550 2551 if not Suppress_Elaboration_Warnings (Ent) 2552 and then not Elaboration_Checks_Suppressed (Ent) 2553 and then Elab_Info_Messages 2554 and then not Suppress_Elaboration_Warnings (Task_Scope) 2555 and then not Elaboration_Checks_Suppressed (Task_Scope) 2556 then 2557 Error_Msg_Node_2 := Task_Scope; 2558 Error_Msg_NE 2559 ("info: activation of an instance of task type&" & 2560 " requires pragma Elaborate_All on &?", N, Ent); 2561 end if; 2562 2563 Activate_Elaborate_All_Desirable (N, Task_Scope); 2564 Set_Suppress_Elaboration_Warnings (Task_Scope); 2565 end if; 2566 2567 Next_Elmt (Elmt); 2568 end loop; 2569 2570 -- For tasks declared in the current unit, trace other calls within 2571 -- the task procedure bodies, which are available. 2572 2573 In_Task_Activation := True; 2574 2575 Elmt := First_Elmt (Intra_Procs); 2576 while Present (Elmt) loop 2577 Ent := Node (Elmt); 2578 Check_Internal_Call_Continue (N, Ent, Enclosing, Ent); 2579 Next_Elmt (Elmt); 2580 end loop; 2581 2582 In_Task_Activation := False; 2583 end Check_Task_Activation; 2584 2585 -------------------------------- 2586 -- Set_Elaboration_Constraint -- 2587 -------------------------------- 2588 2589 procedure Set_Elaboration_Constraint 2590 (Call : Node_Id; 2591 Subp : Entity_Id; 2592 Scop : Entity_Id) 2593 is 2594 Elab_Unit : Entity_Id; 2595 2596 -- Check whether this is a call to an Initialize subprogram for a 2597 -- controlled type. Note that Call can also be a 'Access attribute 2598 -- reference, which now generates an elaboration check. 2599 2600 Init_Call : constant Boolean := 2601 Nkind (Call) = N_Procedure_Call_Statement 2602 and then Chars (Subp) = Name_Initialize 2603 and then Comes_From_Source (Subp) 2604 and then Present (Parameter_Associations (Call)) 2605 and then Is_Controlled (Etype (First_Actual (Call))); 2606 begin 2607 -- If the unit is mentioned in a with_clause of the current unit, it is 2608 -- visible, and we can set the elaboration flag. 2609 2610 if Is_Immediately_Visible (Scop) 2611 or else (Is_Child_Unit (Scop) and then Is_Visible_Lib_Unit (Scop)) 2612 then 2613 Activate_Elaborate_All_Desirable (Call, Scop); 2614 Set_Suppress_Elaboration_Warnings (Scop, True); 2615 return; 2616 end if; 2617 2618 -- If this is not an initialization call or a call using object notation 2619 -- we know that the unit of the called entity is in the context, and 2620 -- we can set the flag as well. The unit need not be visible if the call 2621 -- occurs within an instantiation. 2622 2623 if Is_Init_Proc (Subp) 2624 or else Init_Call 2625 or else Nkind (Original_Node (Call)) = N_Selected_Component 2626 then 2627 null; -- detailed processing follows. 2628 2629 else 2630 Activate_Elaborate_All_Desirable (Call, Scop); 2631 Set_Suppress_Elaboration_Warnings (Scop, True); 2632 return; 2633 end if; 2634 2635 -- If the unit is not in the context, there must be an intermediate unit 2636 -- that is, on which we need to place to elaboration flag. This happens 2637 -- with init proc calls. 2638 2639 if Is_Init_Proc (Subp) 2640 or else Init_Call 2641 then 2642 -- The initialization call is on an object whose type is not declared 2643 -- in the same scope as the subprogram. The type of the object must 2644 -- be a subtype of the type of operation. This object is the first 2645 -- actual in the call. 2646 2647 declare 2648 Typ : constant Entity_Id := 2649 Etype (First (Parameter_Associations (Call))); 2650 begin 2651 Elab_Unit := Scope (Typ); 2652 while (Present (Elab_Unit)) 2653 and then not Is_Compilation_Unit (Elab_Unit) 2654 loop 2655 Elab_Unit := Scope (Elab_Unit); 2656 end loop; 2657 end; 2658 2659 -- If original node uses selected component notation, the prefix is 2660 -- visible and determines the scope that must be elaborated. After 2661 -- rewriting, the prefix is the first actual in the call. 2662 2663 elsif Nkind (Original_Node (Call)) = N_Selected_Component then 2664 Elab_Unit := Scope (Etype (First (Parameter_Associations (Call)))); 2665 2666 -- Not one of special cases above 2667 2668 else 2669 -- Using previously computed scope. If the elaboration check is 2670 -- done after analysis, the scope is not visible any longer, but 2671 -- must still be in the context. 2672 2673 Elab_Unit := Scop; 2674 end if; 2675 2676 Activate_Elaborate_All_Desirable (Call, Elab_Unit); 2677 Set_Suppress_Elaboration_Warnings (Elab_Unit, True); 2678 end Set_Elaboration_Constraint; 2679 2680 ------------------------ 2681 -- Get_Referenced_Ent -- 2682 ------------------------ 2683 2684 function Get_Referenced_Ent (N : Node_Id) return Entity_Id is 2685 Nam : Node_Id; 2686 2687 begin 2688 if Nkind (N) = N_Attribute_Reference then 2689 Nam := Prefix (N); 2690 else 2691 Nam := Name (N); 2692 end if; 2693 2694 if No (Nam) then 2695 return Empty; 2696 elsif Nkind (Nam) = N_Selected_Component then 2697 return Entity (Selector_Name (Nam)); 2698 elsif not Is_Entity_Name (Nam) then 2699 return Empty; 2700 else 2701 return Entity (Nam); 2702 end if; 2703 end Get_Referenced_Ent; 2704 2705 ---------------------- 2706 -- Has_Generic_Body -- 2707 ---------------------- 2708 2709 function Has_Generic_Body (N : Node_Id) return Boolean is 2710 Ent : constant Entity_Id := Get_Generic_Entity (N); 2711 Decl : constant Node_Id := Unit_Declaration_Node (Ent); 2712 Scop : Entity_Id; 2713 2714 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id; 2715 -- Determine if the list of nodes headed by N and linked by Next 2716 -- contains a package body for the package spec entity E, and if so 2717 -- return the package body. If not, then returns Empty. 2718 2719 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id; 2720 -- This procedure is called load the unit whose name is given by Nam. 2721 -- This unit is being loaded to see whether it contains an optional 2722 -- generic body. The returned value is the loaded unit, which is always 2723 -- a package body (only package bodies can contain other entities in the 2724 -- sense in which Has_Generic_Body is interested). We only attempt to 2725 -- load bodies if we are generating code. If we are in semantics check 2726 -- only mode, then it would be wrong to load bodies that are not 2727 -- required from a semantic point of view, so in this case we return 2728 -- Empty. The result is that the caller may incorrectly decide that a 2729 -- generic spec does not have a body when in fact it does, but the only 2730 -- harm in this is that some warnings on elaboration problems may be 2731 -- lost in semantic checks only mode, which is not big loss. We also 2732 -- return Empty if we go for a body and it is not there. 2733 2734 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id; 2735 -- PE is the entity for a package spec. This function locates the 2736 -- corresponding package body, returning Empty if none is found. The 2737 -- package body returned is fully parsed but may not yet be analyzed, 2738 -- so only syntactic fields should be referenced. 2739 2740 ------------------ 2741 -- Find_Body_In -- 2742 ------------------ 2743 2744 function Find_Body_In (E : Entity_Id; N : Node_Id) return Node_Id is 2745 Nod : Node_Id; 2746 2747 begin 2748 Nod := N; 2749 while Present (Nod) loop 2750 2751 -- If we found the package body we are looking for, return it 2752 2753 if Nkind (Nod) = N_Package_Body 2754 and then Chars (Defining_Unit_Name (Nod)) = Chars (E) 2755 then 2756 return Nod; 2757 2758 -- If we found the stub for the body, go after the subunit, 2759 -- loading it if necessary. 2760 2761 elsif Nkind (Nod) = N_Package_Body_Stub 2762 and then Chars (Defining_Identifier (Nod)) = Chars (E) 2763 then 2764 if Present (Library_Unit (Nod)) then 2765 return Unit (Library_Unit (Nod)); 2766 2767 else 2768 return Load_Package_Body (Get_Unit_Name (Nod)); 2769 end if; 2770 2771 -- If neither package body nor stub, keep looking on chain 2772 2773 else 2774 Next (Nod); 2775 end if; 2776 end loop; 2777 2778 return Empty; 2779 end Find_Body_In; 2780 2781 ----------------------- 2782 -- Load_Package_Body -- 2783 ----------------------- 2784 2785 function Load_Package_Body (Nam : Unit_Name_Type) return Node_Id is 2786 U : Unit_Number_Type; 2787 2788 begin 2789 if Operating_Mode /= Generate_Code then 2790 return Empty; 2791 else 2792 U := 2793 Load_Unit 2794 (Load_Name => Nam, 2795 Required => False, 2796 Subunit => False, 2797 Error_Node => N); 2798 2799 if U = No_Unit then 2800 return Empty; 2801 else 2802 return Unit (Cunit (U)); 2803 end if; 2804 end if; 2805 end Load_Package_Body; 2806 2807 ------------------------------- 2808 -- Locate_Corresponding_Body -- 2809 ------------------------------- 2810 2811 function Locate_Corresponding_Body (PE : Entity_Id) return Node_Id is 2812 Spec : constant Node_Id := Declaration_Node (PE); 2813 Decl : constant Node_Id := Parent (Spec); 2814 Scop : constant Entity_Id := Scope (PE); 2815 PBody : Node_Id; 2816 2817 begin 2818 if Is_Library_Level_Entity (PE) then 2819 2820 -- If package is a library unit that requires a body, we have no 2821 -- choice but to go after that body because it might contain an 2822 -- optional body for the original generic package. 2823 2824 if Unit_Requires_Body (PE) then 2825 2826 -- Load the body. Note that we are a little careful here to use 2827 -- Spec to get the unit number, rather than PE or Decl, since 2828 -- in the case where the package is itself a library level 2829 -- instantiation, Spec will properly reference the generic 2830 -- template, which is what we really want. 2831 2832 return 2833 Load_Package_Body 2834 (Get_Body_Name (Unit_Name (Get_Source_Unit (Spec)))); 2835 2836 -- But if the package is a library unit that does NOT require 2837 -- a body, then no body is permitted, so we are sure that there 2838 -- is no body for the original generic package. 2839 2840 else 2841 return Empty; 2842 end if; 2843 2844 -- Otherwise look and see if we are embedded in a further package 2845 2846 elsif Is_Package_Or_Generic_Package (Scop) then 2847 2848 -- If so, get the body of the enclosing package, and look in 2849 -- its package body for the package body we are looking for. 2850 2851 PBody := Locate_Corresponding_Body (Scop); 2852 2853 if No (PBody) then 2854 return Empty; 2855 else 2856 return Find_Body_In (PE, First (Declarations (PBody))); 2857 end if; 2858 2859 -- If we are not embedded in a further package, then the body 2860 -- must be in the same declarative part as we are. 2861 2862 else 2863 return Find_Body_In (PE, Next (Decl)); 2864 end if; 2865 end Locate_Corresponding_Body; 2866 2867 -- Start of processing for Has_Generic_Body 2868 2869 begin 2870 if Present (Corresponding_Body (Decl)) then 2871 return True; 2872 2873 elsif Unit_Requires_Body (Ent) then 2874 return True; 2875 2876 -- Compilation units cannot have optional bodies 2877 2878 elsif Is_Compilation_Unit (Ent) then 2879 return False; 2880 2881 -- Otherwise look at what scope we are in 2882 2883 else 2884 Scop := Scope (Ent); 2885 2886 -- Case of entity is in other than a package spec, in this case 2887 -- the body, if present, must be in the same declarative part. 2888 2889 if not Is_Package_Or_Generic_Package (Scop) then 2890 declare 2891 P : Node_Id; 2892 2893 begin 2894 -- Declaration node may get us a spec, so if so, go to 2895 -- the parent declaration. 2896 2897 P := Declaration_Node (Ent); 2898 while not Is_List_Member (P) loop 2899 P := Parent (P); 2900 end loop; 2901 2902 return Present (Find_Body_In (Ent, Next (P))); 2903 end; 2904 2905 -- If the entity is in a package spec, then we have to locate 2906 -- the corresponding package body, and look there. 2907 2908 else 2909 declare 2910 PBody : constant Node_Id := Locate_Corresponding_Body (Scop); 2911 2912 begin 2913 if No (PBody) then 2914 return False; 2915 else 2916 return 2917 Present 2918 (Find_Body_In (Ent, (First (Declarations (PBody))))); 2919 end if; 2920 end; 2921 end if; 2922 end if; 2923 end Has_Generic_Body; 2924 2925 ----------------------- 2926 -- Insert_Elab_Check -- 2927 ----------------------- 2928 2929 procedure Insert_Elab_Check (N : Node_Id; C : Node_Id := Empty) is 2930 Nod : Node_Id; 2931 Loc : constant Source_Ptr := Sloc (N); 2932 2933 Chk : Node_Id; 2934 -- The check (N_Raise_Program_Error) node to be inserted 2935 2936 begin 2937 -- If expansion is disabled, do not generate any checks. Also 2938 -- skip checks if any subunits are missing because in either 2939 -- case we lack the full information that we need, and no object 2940 -- file will be created in any case. 2941 2942 if not Expander_Active or else Subunits_Missing then 2943 return; 2944 end if; 2945 2946 -- If we have a generic instantiation, where Instance_Spec is set, 2947 -- then this field points to a generic instance spec that has 2948 -- been inserted before the instantiation node itself, so that 2949 -- is where we want to insert a check. 2950 2951 if Nkind (N) in N_Generic_Instantiation 2952 and then Present (Instance_Spec (N)) 2953 then 2954 Nod := Instance_Spec (N); 2955 else 2956 Nod := N; 2957 end if; 2958 2959 -- Build check node, possibly with condition 2960 2961 Chk := 2962 Make_Raise_Program_Error (Loc, Reason => PE_Access_Before_Elaboration); 2963 2964 if Present (C) then 2965 Set_Condition (Chk, Make_Op_Not (Loc, Right_Opnd => C)); 2966 end if; 2967 2968 -- If we are inserting at the top level, insert in Aux_Decls 2969 2970 if Nkind (Parent (Nod)) = N_Compilation_Unit then 2971 declare 2972 ADN : constant Node_Id := Aux_Decls_Node (Parent (Nod)); 2973 2974 begin 2975 if No (Declarations (ADN)) then 2976 Set_Declarations (ADN, New_List (Chk)); 2977 else 2978 Append_To (Declarations (ADN), Chk); 2979 end if; 2980 2981 Analyze (Chk); 2982 end; 2983 2984 -- Otherwise just insert as an action on the node in question 2985 2986 else 2987 Insert_Action (Nod, Chk); 2988 end if; 2989 end Insert_Elab_Check; 2990 2991 ------------------------------- 2992 -- Is_Finalization_Procedure -- 2993 ------------------------------- 2994 2995 function Is_Finalization_Procedure (Id : Entity_Id) return Boolean is 2996 begin 2997 -- Check whether Id is a procedure with at least one parameter 2998 2999 if Ekind (Id) = E_Procedure 3000 and then Present (First_Formal (Id)) 3001 then 3002 declare 3003 Typ : constant Entity_Id := Etype (First_Formal (Id)); 3004 Deep_Fin : Entity_Id := Empty; 3005 Fin : Entity_Id := Empty; 3006 3007 begin 3008 -- If the type of the first formal does not require finalization 3009 -- actions, then this is definitely not [Deep_]Finalize. 3010 3011 if not Needs_Finalization (Typ) then 3012 return False; 3013 end if; 3014 3015 -- At this point we have the following scenario: 3016 3017 -- procedure Name (Param1 : [in] [out] Ctrl[; Param2 : ...]); 3018 3019 -- Recover the two possible versions of [Deep_]Finalize using the 3020 -- type of the first parameter and compare with the input. 3021 3022 Deep_Fin := TSS (Typ, TSS_Deep_Finalize); 3023 3024 if Is_Controlled (Typ) then 3025 Fin := Find_Prim_Op (Typ, Name_Finalize); 3026 end if; 3027 3028 return 3029 (Present (Deep_Fin) and then Id = Deep_Fin) 3030 or else 3031 (Present (Fin) and then Id = Fin); 3032 end; 3033 end if; 3034 3035 return False; 3036 end Is_Finalization_Procedure; 3037 3038 ------------------ 3039 -- Output_Calls -- 3040 ------------------ 3041 3042 procedure Output_Calls (N : Node_Id) is 3043 Ent : Entity_Id; 3044 3045 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean; 3046 -- An internal function, used to determine if a name, Nm, is either 3047 -- a non-internal name, or is an internal name that is printable 3048 -- by the error message circuits (i.e. it has a single upper 3049 -- case letter at the end). 3050 3051 function Is_Printable_Error_Name (Nm : Name_Id) return Boolean is 3052 begin 3053 if not Is_Internal_Name (Nm) then 3054 return True; 3055 3056 elsif Name_Len = 1 then 3057 return False; 3058 3059 else 3060 Name_Len := Name_Len - 1; 3061 return not Is_Internal_Name; 3062 end if; 3063 end Is_Printable_Error_Name; 3064 3065 -- Start of processing for Output_Calls 3066 3067 begin 3068 for J in reverse 1 .. Elab_Call.Last loop 3069 Error_Msg_Sloc := Elab_Call.Table (J).Cloc; 3070 3071 Ent := Elab_Call.Table (J).Ent; 3072 3073 if Is_Generic_Unit (Ent) then 3074 Error_Msg_NE ("\??& instantiated #", N, Ent); 3075 3076 elsif Is_Init_Proc (Ent) then 3077 Error_Msg_N ("\??initialization procedure called #", N); 3078 3079 elsif Is_Printable_Error_Name (Chars (Ent)) then 3080 Error_Msg_NE ("\??& called #", N, Ent); 3081 3082 else 3083 Error_Msg_N ("\?? called #", N); 3084 end if; 3085 end loop; 3086 end Output_Calls; 3087 3088 ---------------------------- 3089 -- Same_Elaboration_Scope -- 3090 ---------------------------- 3091 3092 function Same_Elaboration_Scope (Scop1, Scop2 : Entity_Id) return Boolean is 3093 S1 : Entity_Id; 3094 S2 : Entity_Id; 3095 3096 begin 3097 -- Find elaboration scope for Scop1 3098 -- This is either a subprogram or a compilation unit. 3099 3100 S1 := Scop1; 3101 while S1 /= Standard_Standard 3102 and then not Is_Compilation_Unit (S1) 3103 and then (Ekind (S1) = E_Package 3104 or else 3105 Ekind (S1) = E_Protected_Type 3106 or else 3107 Ekind (S1) = E_Block) 3108 loop 3109 S1 := Scope (S1); 3110 end loop; 3111 3112 -- Find elaboration scope for Scop2 3113 3114 S2 := Scop2; 3115 while S2 /= Standard_Standard 3116 and then not Is_Compilation_Unit (S2) 3117 and then (Ekind (S2) = E_Package 3118 or else 3119 Ekind (S2) = E_Protected_Type 3120 or else 3121 Ekind (S2) = E_Block) 3122 loop 3123 S2 := Scope (S2); 3124 end loop; 3125 3126 return S1 = S2; 3127 end Same_Elaboration_Scope; 3128 3129 ----------------- 3130 -- Set_C_Scope -- 3131 ----------------- 3132 3133 procedure Set_C_Scope is 3134 begin 3135 while not Is_Compilation_Unit (C_Scope) loop 3136 C_Scope := Scope (C_Scope); 3137 end loop; 3138 end Set_C_Scope; 3139 3140 ----------------- 3141 -- Spec_Entity -- 3142 ----------------- 3143 3144 function Spec_Entity (E : Entity_Id) return Entity_Id is 3145 Decl : Node_Id; 3146 3147 begin 3148 -- Check for case of body entity 3149 -- Why is the check for E_Void needed??? 3150 3151 if Ekind_In (E, E_Void, E_Subprogram_Body, E_Package_Body) then 3152 Decl := E; 3153 3154 loop 3155 Decl := Parent (Decl); 3156 exit when Nkind (Decl) in N_Proper_Body; 3157 end loop; 3158 3159 return Corresponding_Spec (Decl); 3160 3161 else 3162 return E; 3163 end if; 3164 end Spec_Entity; 3165 3166 ------------------- 3167 -- Supply_Bodies -- 3168 ------------------- 3169 3170 procedure Supply_Bodies (N : Node_Id) is 3171 begin 3172 if Nkind (N) = N_Subprogram_Declaration then 3173 declare 3174 Ent : constant Entity_Id := Defining_Unit_Name (Specification (N)); 3175 begin 3176 3177 -- Internal subprograms will already have a generated body, so 3178 -- there is no need to provide a stub for them. 3179 3180 if No (Corresponding_Body (N)) then 3181 declare 3182 Loc : constant Source_Ptr := Sloc (N); 3183 B : Node_Id; 3184 Formals : constant List_Id := Copy_Parameter_List (Ent); 3185 Nam : constant Entity_Id := 3186 Make_Defining_Identifier (Loc, Chars (Ent)); 3187 Spec : Node_Id; 3188 Stats : constant List_Id := 3189 New_List 3190 (Make_Raise_Program_Error (Loc, 3191 Reason => PE_Access_Before_Elaboration)); 3192 3193 begin 3194 if Ekind (Ent) = E_Function then 3195 Spec := 3196 Make_Function_Specification (Loc, 3197 Defining_Unit_Name => Nam, 3198 Parameter_Specifications => Formals, 3199 Result_Definition => 3200 New_Copy_Tree 3201 (Result_Definition (Specification (N)))); 3202 3203 -- We cannot reliably make a return statement for this 3204 -- body, but none is needed because the call raises 3205 -- program error. 3206 3207 Set_Return_Present (Ent); 3208 3209 else 3210 Spec := 3211 Make_Procedure_Specification (Loc, 3212 Defining_Unit_Name => Nam, 3213 Parameter_Specifications => Formals); 3214 end if; 3215 3216 B := Make_Subprogram_Body (Loc, 3217 Specification => Spec, 3218 Declarations => New_List, 3219 Handled_Statement_Sequence => 3220 Make_Handled_Sequence_Of_Statements (Loc, Stats)); 3221 Insert_After (N, B); 3222 Analyze (B); 3223 end; 3224 end if; 3225 end; 3226 3227 elsif Nkind (N) = N_Package_Declaration then 3228 declare 3229 Spec : constant Node_Id := Specification (N); 3230 begin 3231 Push_Scope (Defining_Unit_Name (Spec)); 3232 Supply_Bodies (Visible_Declarations (Spec)); 3233 Supply_Bodies (Private_Declarations (Spec)); 3234 Pop_Scope; 3235 end; 3236 end if; 3237 end Supply_Bodies; 3238 3239 procedure Supply_Bodies (L : List_Id) is 3240 Elmt : Node_Id; 3241 begin 3242 if Present (L) then 3243 Elmt := First (L); 3244 while Present (Elmt) loop 3245 Supply_Bodies (Elmt); 3246 Next (Elmt); 3247 end loop; 3248 end if; 3249 end Supply_Bodies; 3250 3251 ------------ 3252 -- Within -- 3253 ------------ 3254 3255 function Within (E1, E2 : Entity_Id) return Boolean is 3256 Scop : Entity_Id; 3257 begin 3258 Scop := E1; 3259 loop 3260 if Scop = E2 then 3261 return True; 3262 elsif Scop = Standard_Standard then 3263 return False; 3264 else 3265 Scop := Scope (Scop); 3266 end if; 3267 end loop; 3268 end Within; 3269 3270 -------------------------- 3271 -- Within_Elaborate_All -- 3272 -------------------------- 3273 3274 function Within_Elaborate_All 3275 (Unit : Unit_Number_Type; 3276 E : Entity_Id) return Boolean 3277 is 3278 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean; 3279 pragma Pack (Unit_Number_Set); 3280 3281 Seen : Unit_Number_Set := (others => False); 3282 -- Seen (X) is True after we have seen unit X in the walk. This is used 3283 -- to prevent processing the same unit more than once. 3284 3285 Result : Boolean := False; 3286 3287 procedure Helper (Unit : Unit_Number_Type); 3288 -- This helper procedure does all the work for Within_Elaborate_All. It 3289 -- walks the dependency graph, and sets Result to True if it finds an 3290 -- appropriate Elaborate_All. 3291 3292 ------------ 3293 -- Helper -- 3294 ------------ 3295 3296 procedure Helper (Unit : Unit_Number_Type) is 3297 CU : constant Node_Id := Cunit (Unit); 3298 3299 Item : Node_Id; 3300 Item2 : Node_Id; 3301 Elab_Id : Entity_Id; 3302 Par : Node_Id; 3303 3304 begin 3305 if Seen (Unit) then 3306 return; 3307 else 3308 Seen (Unit) := True; 3309 end if; 3310 3311 -- First, check for Elaborate_Alls on this unit 3312 3313 Item := First (Context_Items (CU)); 3314 while Present (Item) loop 3315 if Nkind (Item) = N_Pragma 3316 and then Pragma_Name (Item) = Name_Elaborate_All 3317 then 3318 -- Return if some previous error on the pragma itself. The 3319 -- pragma may be unanalyzed, because of a previous error, or 3320 -- if it is the context of a subunit, inherited by its parent. 3321 3322 if Error_Posted (Item) or else not Analyzed (Item) then 3323 return; 3324 end if; 3325 3326 Elab_Id := 3327 Entity 3328 (Expression (First (Pragma_Argument_Associations (Item)))); 3329 3330 if E = Elab_Id then 3331 Result := True; 3332 return; 3333 end if; 3334 3335 Par := Parent (Unit_Declaration_Node (Elab_Id)); 3336 3337 Item2 := First (Context_Items (Par)); 3338 while Present (Item2) loop 3339 if Nkind (Item2) = N_With_Clause 3340 and then Entity (Name (Item2)) = E 3341 and then not Limited_Present (Item2) 3342 then 3343 Result := True; 3344 return; 3345 end if; 3346 3347 Next (Item2); 3348 end loop; 3349 end if; 3350 3351 Next (Item); 3352 end loop; 3353 3354 -- Second, recurse on with's. We could do this as part of the above 3355 -- loop, but it's probably more efficient to have two loops, because 3356 -- the relevant Elaborate_All is likely to be on the initial unit. In 3357 -- other words, we're walking the with's breadth-first. This part is 3358 -- only necessary in the dynamic elaboration model. 3359 3360 if Dynamic_Elaboration_Checks then 3361 Item := First (Context_Items (CU)); 3362 while Present (Item) loop 3363 if Nkind (Item) = N_With_Clause 3364 and then not Limited_Present (Item) 3365 then 3366 -- Note: the following call to Get_Cunit_Unit_Number does a 3367 -- linear search, which could be slow, but it's OK because 3368 -- we're about to give a warning anyway. Also, there might 3369 -- be hundreds of units, but not millions. If it turns out 3370 -- to be a problem, we could store the Get_Cunit_Unit_Number 3371 -- in each N_Compilation_Unit node, but that would involve 3372 -- rearranging N_Compilation_Unit_Aux to make room. 3373 3374 Helper (Get_Cunit_Unit_Number (Library_Unit (Item))); 3375 3376 if Result then 3377 return; 3378 end if; 3379 end if; 3380 3381 Next (Item); 3382 end loop; 3383 end if; 3384 end Helper; 3385 3386 -- Start of processing for Within_Elaborate_All 3387 3388 begin 3389 Helper (Unit); 3390 return Result; 3391 end Within_Elaborate_All; 3392 3393end Sem_Elab; 3394