1------------------------------------------------------------------------------ 2-- -- 3-- GNAT COMPILER COMPONENTS -- 4-- -- 5-- S E M . C H 7 -- 6-- -- 7-- B o d y -- 8-- -- 9-- Copyright (C) 1992-2012, 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 26-- This package contains the routines to process package specifications and 27-- bodies. The most important semantic aspects of package processing are the 28-- handling of private and full declarations, and the construction of dispatch 29-- tables for tagged types. 30 31with Aspects; use Aspects; 32with Atree; use Atree; 33with Debug; use Debug; 34with Einfo; use Einfo; 35with Elists; use Elists; 36with Errout; use Errout; 37with Exp_Disp; use Exp_Disp; 38with Exp_Dist; use Exp_Dist; 39with Exp_Dbug; use Exp_Dbug; 40with Lib; use Lib; 41with Lib.Xref; use Lib.Xref; 42with Namet; use Namet; 43with Nmake; use Nmake; 44with Nlists; use Nlists; 45with Opt; use Opt; 46with Output; use Output; 47with Restrict; use Restrict; 48with Sem; use Sem; 49with Sem_Aux; use Sem_Aux; 50with Sem_Cat; use Sem_Cat; 51with Sem_Ch3; use Sem_Ch3; 52with Sem_Ch6; use Sem_Ch6; 53with Sem_Ch8; use Sem_Ch8; 54with Sem_Ch10; use Sem_Ch10; 55with Sem_Ch12; use Sem_Ch12; 56with Sem_Ch13; use Sem_Ch13; 57with Sem_Disp; use Sem_Disp; 58with Sem_Eval; use Sem_Eval; 59with Sem_Util; use Sem_Util; 60with Sem_Warn; use Sem_Warn; 61with Snames; use Snames; 62with Stand; use Stand; 63with Sinfo; use Sinfo; 64with Sinput; use Sinput; 65with Style; 66with Uintp; use Uintp; 67 68package body Sem_Ch7 is 69 70 ----------------------------------- 71 -- Handling private declarations -- 72 ----------------------------------- 73 74 -- The principle that each entity has a single defining occurrence clashes 75 -- with the presence of two separate definitions for private types: the 76 -- first is the private type declaration, and the second is the full type 77 -- declaration. It is important that all references to the type point to 78 -- the same defining occurrence, namely the first one. To enforce the two 79 -- separate views of the entity, the corresponding information is swapped 80 -- between the two declarations. Outside of the package, the defining 81 -- occurrence only contains the private declaration information, while in 82 -- the private part and the body of the package the defining occurrence 83 -- contains the full declaration. To simplify the swap, the defining 84 -- occurrence that currently holds the private declaration points to the 85 -- full declaration. During semantic processing the defining occurrence 86 -- also points to a list of private dependents, that is to say access types 87 -- or composite types whose designated types or component types are 88 -- subtypes or derived types of the private type in question. After the 89 -- full declaration has been seen, the private dependents are updated to 90 -- indicate that they have full definitions. 91 92 ----------------------- 93 -- Local Subprograms -- 94 ----------------------- 95 96 procedure Analyze_Package_Body_Helper (N : Node_Id); 97 -- Does all the real work of Analyze_Package_Body 98 99 procedure Check_Anonymous_Access_Types 100 (Spec_Id : Entity_Id; 101 P_Body : Node_Id); 102 -- If the spec of a package has a limited_with_clause, it may declare 103 -- anonymous access types whose designated type is a limited view, such an 104 -- anonymous access return type for a function. This access type cannot be 105 -- elaborated in the spec itself, but it may need an itype reference if it 106 -- is used within a nested scope. In that case the itype reference is 107 -- created at the beginning of the corresponding package body and inserted 108 -- before other body declarations. 109 110 procedure Install_Package_Entity (Id : Entity_Id); 111 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places 112 -- one entity on its visibility chain, and recurses on the visible part if 113 -- the entity is an inner package. 114 115 function Is_Private_Base_Type (E : Entity_Id) return Boolean; 116 -- True for a private type that is not a subtype 117 118 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean; 119 -- If the private dependent is a private type whose full view is derived 120 -- from the parent type, its full properties are revealed only if we are in 121 -- the immediate scope of the private dependent. Should this predicate be 122 -- tightened further??? 123 124 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id); 125 -- Called upon entering the private part of a public child package and the 126 -- body of a nested package, to potentially declare certain inherited 127 -- subprograms that were inherited by types in the visible part, but whose 128 -- declaration was deferred because the parent operation was private and 129 -- not visible at that point. These subprograms are located by traversing 130 -- the visible part declarations looking for non-private type extensions 131 -- and then examining each of the primitive operations of such types to 132 -- find those that were inherited but declared with a special internal 133 -- name. Each such operation is now declared as an operation with a normal 134 -- name (using the name of the parent operation) and replaces the previous 135 -- implicit operation in the primitive operations list of the type. If the 136 -- inherited private operation has been overridden, then it's replaced by 137 -- the overriding operation. 138 139 -------------------------- 140 -- Analyze_Package_Body -- 141 -------------------------- 142 143 procedure Analyze_Package_Body (N : Node_Id) is 144 Loc : constant Source_Ptr := Sloc (N); 145 146 begin 147 if Debug_Flag_C then 148 Write_Str ("==> package body "); 149 Write_Name (Chars (Defining_Entity (N))); 150 Write_Str (" from "); 151 Write_Location (Loc); 152 Write_Eol; 153 Indent; 154 end if; 155 156 -- The real work is split out into the helper, so it can do "return;" 157 -- without skipping the debug output. 158 159 Analyze_Package_Body_Helper (N); 160 161 if Debug_Flag_C then 162 Outdent; 163 Write_Str ("<== package body "); 164 Write_Name (Chars (Defining_Entity (N))); 165 Write_Str (" from "); 166 Write_Location (Loc); 167 Write_Eol; 168 end if; 169 end Analyze_Package_Body; 170 171 --------------------------------- 172 -- Analyze_Package_Body_Helper -- 173 --------------------------------- 174 175 procedure Analyze_Package_Body_Helper (N : Node_Id) is 176 HSS : Node_Id; 177 Body_Id : Entity_Id; 178 Spec_Id : Entity_Id; 179 Last_Spec_Entity : Entity_Id; 180 New_N : Node_Id; 181 Pack_Decl : Node_Id; 182 183 procedure Install_Composite_Operations (P : Entity_Id); 184 -- Composite types declared in the current scope may depend on types 185 -- that were private at the point of declaration, and whose full view 186 -- is now in scope. Indicate that the corresponding operations on the 187 -- composite type are available. 188 189 ---------------------------------- 190 -- Install_Composite_Operations -- 191 ---------------------------------- 192 193 procedure Install_Composite_Operations (P : Entity_Id) is 194 Id : Entity_Id; 195 196 begin 197 Id := First_Entity (P); 198 while Present (Id) loop 199 if Is_Type (Id) 200 and then (Is_Limited_Composite (Id) 201 or else Is_Private_Composite (Id)) 202 and then No (Private_Component (Id)) 203 then 204 Set_Is_Limited_Composite (Id, False); 205 Set_Is_Private_Composite (Id, False); 206 end if; 207 208 Next_Entity (Id); 209 end loop; 210 end Install_Composite_Operations; 211 212 -- Start of processing for Analyze_Package_Body_Helper 213 214 begin 215 -- Find corresponding package specification, and establish the current 216 -- scope. The visible defining entity for the package is the defining 217 -- occurrence in the spec. On exit from the package body, all body 218 -- declarations are attached to the defining entity for the body, but 219 -- the later is never used for name resolution. In this fashion there 220 -- is only one visible entity that denotes the package. 221 222 -- Set Body_Id. Note that this Will be reset to point to the generic 223 -- copy later on in the generic case. 224 225 Body_Id := Defining_Entity (N); 226 227 if Present (Corresponding_Spec (N)) then 228 229 -- Body is body of package instantiation. Corresponding spec has 230 -- already been set. 231 232 Spec_Id := Corresponding_Spec (N); 233 Pack_Decl := Unit_Declaration_Node (Spec_Id); 234 235 else 236 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N)); 237 238 if Present (Spec_Id) 239 and then Is_Package_Or_Generic_Package (Spec_Id) 240 then 241 Pack_Decl := Unit_Declaration_Node (Spec_Id); 242 243 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then 244 Error_Msg_N ("cannot supply body for package renaming", N); 245 return; 246 247 elsif Present (Corresponding_Body (Pack_Decl)) then 248 Error_Msg_N ("redefinition of package body", N); 249 return; 250 end if; 251 252 else 253 Error_Msg_N ("missing specification for package body", N); 254 return; 255 end if; 256 257 if Is_Package_Or_Generic_Package (Spec_Id) 258 and then (Scope (Spec_Id) = Standard_Standard 259 or else Is_Child_Unit (Spec_Id)) 260 and then not Unit_Requires_Body (Spec_Id) 261 then 262 if Ada_Version = Ada_83 then 263 Error_Msg_N 264 ("optional package body (not allowed in Ada 95)??", N); 265 else 266 Error_Msg_N ("spec of this package does not allow a body", N); 267 end if; 268 end if; 269 end if; 270 271 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id)); 272 Style.Check_Identifier (Body_Id, Spec_Id); 273 274 if Is_Child_Unit (Spec_Id) then 275 if Nkind (Parent (N)) /= N_Compilation_Unit then 276 Error_Msg_NE 277 ("body of child unit& cannot be an inner package", N, Spec_Id); 278 end if; 279 280 Set_Is_Child_Unit (Body_Id); 281 end if; 282 283 -- Generic package case 284 285 if Ekind (Spec_Id) = E_Generic_Package then 286 287 -- Disable expansion and perform semantic analysis on copy. The 288 -- unannotated body will be used in all instantiations. 289 290 Body_Id := Defining_Entity (N); 291 Set_Ekind (Body_Id, E_Package_Body); 292 Set_Scope (Body_Id, Scope (Spec_Id)); 293 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id)); 294 Set_Body_Entity (Spec_Id, Body_Id); 295 Set_Spec_Entity (Body_Id, Spec_Id); 296 297 New_N := Copy_Generic_Node (N, Empty, Instantiating => False); 298 Rewrite (N, New_N); 299 300 -- Update Body_Id to point to the copied node for the remainder of 301 -- the processing. 302 303 Body_Id := Defining_Entity (N); 304 Start_Generic; 305 end if; 306 307 -- The Body_Id is that of the copied node in the generic case, the 308 -- current node otherwise. Note that N was rewritten above, so we must 309 -- be sure to get the latest Body_Id value. 310 311 Set_Ekind (Body_Id, E_Package_Body); 312 Set_Body_Entity (Spec_Id, Body_Id); 313 Set_Spec_Entity (Body_Id, Spec_Id); 314 315 -- Defining name for the package body is not a visible entity: Only the 316 -- defining name for the declaration is visible. 317 318 Set_Etype (Body_Id, Standard_Void_Type); 319 Set_Scope (Body_Id, Scope (Spec_Id)); 320 Set_Corresponding_Spec (N, Spec_Id); 321 Set_Corresponding_Body (Pack_Decl, Body_Id); 322 323 -- The body entity is not used for semantics or code generation, but 324 -- it is attached to the entity list of the enclosing scope to simplify 325 -- the listing of back-annotations for the types it main contain. 326 327 if Scope (Spec_Id) /= Standard_Standard then 328 Append_Entity (Body_Id, Scope (Spec_Id)); 329 end if; 330 331 -- Indicate that we are currently compiling the body of the package 332 333 Set_In_Package_Body (Spec_Id); 334 Set_Has_Completion (Spec_Id); 335 Last_Spec_Entity := Last_Entity (Spec_Id); 336 337 Push_Scope (Spec_Id); 338 339 Set_Categorization_From_Pragmas (N); 340 341 Install_Visible_Declarations (Spec_Id); 342 Install_Private_Declarations (Spec_Id); 343 Install_Private_With_Clauses (Spec_Id); 344 Install_Composite_Operations (Spec_Id); 345 346 Check_Anonymous_Access_Types (Spec_Id, N); 347 348 if Ekind (Spec_Id) = E_Generic_Package then 349 Set_Use (Generic_Formal_Declarations (Pack_Decl)); 350 end if; 351 352 Set_Use (Visible_Declarations (Specification (Pack_Decl))); 353 Set_Use (Private_Declarations (Specification (Pack_Decl))); 354 355 -- This is a nested package, so it may be necessary to declare certain 356 -- inherited subprograms that are not yet visible because the parent 357 -- type's subprograms are now visible. 358 359 if Ekind (Scope (Spec_Id)) = E_Package 360 and then Scope (Spec_Id) /= Standard_Standard 361 then 362 Declare_Inherited_Private_Subprograms (Spec_Id); 363 end if; 364 365 if Present (Declarations (N)) then 366 Analyze_Declarations (Declarations (N)); 367 Inspect_Deferred_Constant_Completion (Declarations (N)); 368 end if; 369 370 -- Analyze_Declarations has caused freezing of all types. Now generate 371 -- bodies for RACW primitives and stream attributes, if any. 372 373 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then 374 375 -- Attach subprogram bodies to support RACWs declared in spec 376 377 Append_RACW_Bodies (Declarations (N), Spec_Id); 378 Analyze_List (Declarations (N)); 379 end if; 380 381 HSS := Handled_Statement_Sequence (N); 382 383 if Present (HSS) then 384 Process_End_Label (HSS, 't', Spec_Id); 385 Analyze (HSS); 386 387 -- Check that elaboration code in a preelaborable package body is 388 -- empty other than null statements and labels (RM 10.2.1(6)). 389 390 Validate_Null_Statement_Sequence (N); 391 end if; 392 393 Validate_Categorization_Dependency (N, Spec_Id); 394 Check_Completion (Body_Id); 395 396 -- Generate start of body reference. Note that we do this fairly late, 397 -- because the call will use In_Extended_Main_Source_Unit as a check, 398 -- and we want to make sure that Corresponding_Stub links are set 399 400 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False); 401 402 -- For a generic package, collect global references and mark them on 403 -- the original body so that they are not resolved again at the point 404 -- of instantiation. 405 406 if Ekind (Spec_Id) /= E_Package then 407 Save_Global_References (Original_Node (N)); 408 End_Generic; 409 end if; 410 411 -- The entities of the package body have so far been chained onto the 412 -- declaration chain for the spec. That's been fine while we were in the 413 -- body, since we wanted them to be visible, but now that we are leaving 414 -- the package body, they are no longer visible, so we remove them from 415 -- the entity chain of the package spec entity, and copy them to the 416 -- entity chain of the package body entity, where they will never again 417 -- be visible. 418 419 if Present (Last_Spec_Entity) then 420 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity)); 421 Set_Next_Entity (Last_Spec_Entity, Empty); 422 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); 423 Set_Last_Entity (Spec_Id, Last_Spec_Entity); 424 425 else 426 Set_First_Entity (Body_Id, First_Entity (Spec_Id)); 427 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id)); 428 Set_First_Entity (Spec_Id, Empty); 429 Set_Last_Entity (Spec_Id, Empty); 430 end if; 431 432 End_Package_Scope (Spec_Id); 433 434 -- All entities declared in body are not visible 435 436 declare 437 E : Entity_Id; 438 439 begin 440 E := First_Entity (Body_Id); 441 while Present (E) loop 442 Set_Is_Immediately_Visible (E, False); 443 Set_Is_Potentially_Use_Visible (E, False); 444 Set_Is_Hidden (E); 445 446 -- Child units may appear on the entity list (e.g. if they appear 447 -- in the context of a subunit) but they are not body entities. 448 449 if not Is_Child_Unit (E) then 450 Set_Is_Package_Body_Entity (E); 451 end if; 452 453 Next_Entity (E); 454 end loop; 455 end; 456 457 Check_References (Body_Id); 458 459 -- For a generic unit, check that the formal parameters are referenced, 460 -- and that local variables are used, as for regular packages. 461 462 if Ekind (Spec_Id) = E_Generic_Package then 463 Check_References (Spec_Id); 464 end if; 465 466 -- The processing so far has made all entities of the package body 467 -- public (i.e. externally visible to the linker). This is in general 468 -- necessary, since inlined or generic bodies, for which code is 469 -- generated in other units, may need to see these entities. The 470 -- following loop runs backwards from the end of the entities of the 471 -- package body making these entities invisible until we reach a 472 -- referencer, i.e. a declaration that could reference a previous 473 -- declaration, a generic body or an inlined body, or a stub (which may 474 -- contain either of these). This is of course an approximation, but it 475 -- is conservative and definitely correct. 476 477 -- We only do this at the outer (library) level non-generic packages. 478 -- The reason is simply to cut down on the number of global symbols 479 -- generated, which has a double effect: (1) to make the compilation 480 -- process more efficient and (2) to give the code generator more 481 -- freedom to optimize within each unit, especially subprograms. 482 483 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id)) 484 and then not Is_Generic_Unit (Spec_Id) 485 and then Present (Declarations (N)) 486 then 487 Make_Non_Public_Where_Possible : declare 488 489 function Has_Referencer 490 (L : List_Id; 491 Outer : Boolean) return Boolean; 492 -- Traverse the given list of declarations in reverse order. 493 -- Return True if a referencer is present. Return False if none is 494 -- found. The Outer parameter is True for the outer level call and 495 -- False for inner level calls for nested packages. If Outer is 496 -- True, then any entities up to the point of hitting a referencer 497 -- get their Is_Public flag cleared, so that the entities will be 498 -- treated as static entities in the C sense, and need not have 499 -- fully qualified names. Furthermore, if the referencer is an 500 -- inlined subprogram that doesn't reference other subprograms, 501 -- we keep clearing the Is_Public flag on subprograms. For inner 502 -- levels, we need all names to be fully qualified to deal with 503 -- the same name appearing in parallel packages (right now this 504 -- is tied to their being external). 505 506 -------------------- 507 -- Has_Referencer -- 508 -------------------- 509 510 function Has_Referencer 511 (L : List_Id; 512 Outer : Boolean) return Boolean 513 is 514 Has_Referencer_Except_For_Subprograms : Boolean := False; 515 516 D : Node_Id; 517 E : Entity_Id; 518 K : Node_Kind; 519 S : Entity_Id; 520 521 function Check_Subprogram_Ref (N : Node_Id) 522 return Traverse_Result; 523 -- Look for references to subprograms 524 525 -------------------------- 526 -- Check_Subprogram_Ref -- 527 -------------------------- 528 529 function Check_Subprogram_Ref (N : Node_Id) 530 return Traverse_Result 531 is 532 V : Node_Id; 533 534 begin 535 -- Check name of procedure or function calls 536 537 if Nkind (N) in N_Subprogram_Call 538 and then Is_Entity_Name (Name (N)) 539 then 540 return Abandon; 541 end if; 542 543 -- Check prefix of attribute references 544 545 if Nkind (N) = N_Attribute_Reference 546 and then Is_Entity_Name (Prefix (N)) 547 and then Present (Entity (Prefix (N))) 548 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind 549 then 550 return Abandon; 551 end if; 552 553 -- Check value of constants 554 555 if Nkind (N) = N_Identifier 556 and then Present (Entity (N)) 557 and then Ekind (Entity (N)) = E_Constant 558 then 559 V := Constant_Value (Entity (N)); 560 if Present (V) 561 and then not Compile_Time_Known_Value_Or_Aggr (V) 562 then 563 return Abandon; 564 end if; 565 end if; 566 567 return OK; 568 end Check_Subprogram_Ref; 569 570 function Check_Subprogram_Refs is 571 new Traverse_Func (Check_Subprogram_Ref); 572 573 -- Start of processing for Has_Referencer 574 575 begin 576 if No (L) then 577 return False; 578 end if; 579 580 D := Last (L); 581 while Present (D) loop 582 K := Nkind (D); 583 584 if K in N_Body_Stub then 585 return True; 586 587 -- Processing for subprogram bodies 588 589 elsif K = N_Subprogram_Body then 590 if Acts_As_Spec (D) then 591 E := Defining_Entity (D); 592 593 -- An inlined body acts as a referencer. Note also 594 -- that we never reset Is_Public for an inlined 595 -- subprogram. Gigi requires Is_Public to be set. 596 597 -- Note that we test Has_Pragma_Inline here rather 598 -- than Is_Inlined. We are compiling this for a 599 -- client, and it is the client who will decide if 600 -- actual inlining should occur, so we need to assume 601 -- that the procedure could be inlined for the purpose 602 -- of accessing global entities. 603 604 if Has_Pragma_Inline (E) then 605 if Outer 606 and then Check_Subprogram_Refs (D) = OK 607 then 608 Has_Referencer_Except_For_Subprograms := True; 609 else 610 return True; 611 end if; 612 else 613 Set_Is_Public (E, False); 614 end if; 615 616 else 617 E := Corresponding_Spec (D); 618 619 if Present (E) then 620 621 -- A generic subprogram body acts as a referencer 622 623 if Is_Generic_Unit (E) then 624 return True; 625 end if; 626 627 if Has_Pragma_Inline (E) or else Is_Inlined (E) then 628 if Outer 629 and then Check_Subprogram_Refs (D) = OK 630 then 631 Has_Referencer_Except_For_Subprograms := True; 632 else 633 return True; 634 end if; 635 end if; 636 end if; 637 end if; 638 639 -- Processing for package bodies 640 641 elsif K = N_Package_Body 642 and then Present (Corresponding_Spec (D)) 643 then 644 E := Corresponding_Spec (D); 645 646 -- Generic package body is a referencer. It would seem 647 -- that we only have to consider generics that can be 648 -- exported, i.e. where the corresponding spec is the 649 -- spec of the current package, but because of nested 650 -- instantiations, a fully private generic body may 651 -- export other private body entities. Furthermore, 652 -- regardless of whether there was a previous inlined 653 -- subprogram, (an instantiation of) the generic package 654 -- may reference any entity declared before it. 655 656 if Is_Generic_Unit (E) then 657 return True; 658 659 -- For non-generic package body, recurse into body unless 660 -- this is an instance, we ignore instances since they 661 -- cannot have references that affect outer entities. 662 663 elsif not Is_Generic_Instance (E) 664 and then not Has_Referencer_Except_For_Subprograms 665 then 666 if Has_Referencer 667 (Declarations (D), Outer => False) 668 then 669 return True; 670 end if; 671 end if; 672 673 -- Processing for package specs, recurse into declarations. 674 -- Again we skip this for the case of generic instances. 675 676 elsif K = N_Package_Declaration 677 and then not Has_Referencer_Except_For_Subprograms 678 then 679 S := Specification (D); 680 681 if not Is_Generic_Unit (Defining_Entity (S)) then 682 if Has_Referencer 683 (Private_Declarations (S), Outer => False) 684 then 685 return True; 686 elsif Has_Referencer 687 (Visible_Declarations (S), Outer => False) 688 then 689 return True; 690 end if; 691 end if; 692 693 -- Objects and exceptions need not be public if we have not 694 -- encountered a referencer so far. We only reset the flag 695 -- for outer level entities that are not imported/exported, 696 -- and which have no interface name. 697 698 elsif Nkind_In (K, N_Object_Declaration, 699 N_Exception_Declaration, 700 N_Subprogram_Declaration) 701 then 702 E := Defining_Entity (D); 703 704 if Outer 705 and then (not Has_Referencer_Except_For_Subprograms 706 or else K = N_Subprogram_Declaration) 707 and then not Is_Imported (E) 708 and then not Is_Exported (E) 709 and then No (Interface_Name (E)) 710 then 711 Set_Is_Public (E, False); 712 end if; 713 end if; 714 715 Prev (D); 716 end loop; 717 718 return Has_Referencer_Except_For_Subprograms; 719 end Has_Referencer; 720 721 -- Start of processing for Make_Non_Public_Where_Possible 722 723 begin 724 declare 725 Discard : Boolean; 726 pragma Warnings (Off, Discard); 727 728 begin 729 Discard := Has_Referencer (Declarations (N), Outer => True); 730 end; 731 end Make_Non_Public_Where_Possible; 732 end if; 733 734 -- If expander is not active, then here is where we turn off the 735 -- In_Package_Body flag, otherwise it is turned off at the end of the 736 -- corresponding expansion routine. If this is an instance body, we need 737 -- to qualify names of local entities, because the body may have been 738 -- compiled as a preliminary to another instantiation. 739 740 if not Expander_Active then 741 Set_In_Package_Body (Spec_Id, False); 742 743 if Is_Generic_Instance (Spec_Id) 744 and then Operating_Mode = Generate_Code 745 then 746 Qualify_Entity_Names (N); 747 end if; 748 end if; 749 end Analyze_Package_Body_Helper; 750 751 --------------------------------- 752 -- Analyze_Package_Declaration -- 753 --------------------------------- 754 755 procedure Analyze_Package_Declaration (N : Node_Id) is 756 Id : constant Node_Id := Defining_Entity (N); 757 758 PF : Boolean; 759 -- True when in the context of a declared pure library unit 760 761 Body_Required : Boolean; 762 -- True when this package declaration requires a corresponding body 763 764 Comp_Unit : Boolean; 765 -- True when this package declaration is not a nested declaration 766 767 begin 768 -- Analye aspect specifications immediately, since we need to recognize 769 -- things like Pure early enough to diagnose violations during analysis. 770 771 if Has_Aspects (N) then 772 Analyze_Aspect_Specifications (N, Id); 773 end if; 774 775 -- Ada 2005 (AI-217): Check if the package has been erroneously named 776 -- in a limited-with clause of its own context. In this case the error 777 -- has been previously notified by Analyze_Context. 778 779 -- limited with Pkg; -- ERROR 780 -- package Pkg is ... 781 782 if From_With_Type (Id) then 783 return; 784 end if; 785 786 if Debug_Flag_C then 787 Write_Str ("==> package spec "); 788 Write_Name (Chars (Id)); 789 Write_Str (" from "); 790 Write_Location (Sloc (N)); 791 Write_Eol; 792 Indent; 793 end if; 794 795 Generate_Definition (Id); 796 Enter_Name (Id); 797 Set_Ekind (Id, E_Package); 798 Set_Etype (Id, Standard_Void_Type); 799 800 Push_Scope (Id); 801 802 PF := Is_Pure (Enclosing_Lib_Unit_Entity); 803 Set_Is_Pure (Id, PF); 804 805 Set_Categorization_From_Pragmas (N); 806 807 Analyze (Specification (N)); 808 Validate_Categorization_Dependency (N, Id); 809 810 Body_Required := Unit_Requires_Body (Id); 811 812 -- When this spec does not require an explicit body, we know that there 813 -- are no entities requiring completion in the language sense; we call 814 -- Check_Completion here only to ensure that any nested package 815 -- declaration that requires an implicit body gets one. (In the case 816 -- where a body is required, Check_Completion is called at the end of 817 -- the body's declarative part.) 818 819 if not Body_Required then 820 Check_Completion; 821 end if; 822 823 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit; 824 if Comp_Unit then 825 826 -- Set Body_Required indication on the compilation unit node, and 827 -- determine whether elaboration warnings may be meaningful on it. 828 829 Set_Body_Required (Parent (N), Body_Required); 830 831 if not Body_Required then 832 Set_Suppress_Elaboration_Warnings (Id); 833 end if; 834 835 end if; 836 837 End_Package_Scope (Id); 838 839 -- For the declaration of a library unit that is a remote types package, 840 -- check legality rules regarding availability of stream attributes for 841 -- types that contain non-remote access values. This subprogram performs 842 -- visibility tests that rely on the fact that we have exited the scope 843 -- of Id. 844 845 if Comp_Unit then 846 Validate_RT_RAT_Component (N); 847 end if; 848 849 if Debug_Flag_C then 850 Outdent; 851 Write_Str ("<== package spec "); 852 Write_Name (Chars (Id)); 853 Write_Str (" from "); 854 Write_Location (Sloc (N)); 855 Write_Eol; 856 end if; 857 end Analyze_Package_Declaration; 858 859 ----------------------------------- 860 -- Analyze_Package_Specification -- 861 ----------------------------------- 862 863 -- Note that this code is shared for the analysis of generic package specs 864 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details). 865 866 procedure Analyze_Package_Specification (N : Node_Id) is 867 Id : constant Entity_Id := Defining_Entity (N); 868 Orig_Decl : constant Node_Id := Original_Node (Parent (N)); 869 Vis_Decls : constant List_Id := Visible_Declarations (N); 870 Priv_Decls : constant List_Id := Private_Declarations (N); 871 E : Entity_Id; 872 L : Entity_Id; 873 Public_Child : Boolean; 874 875 Private_With_Clauses_Installed : Boolean := False; 876 -- In Ada 2005, private with_clauses are visible in the private part 877 -- of a nested package, even if it appears in the public part of the 878 -- enclosing package. This requires a separate step to install these 879 -- private_with_clauses, and remove them at the end of the nested 880 -- package. 881 882 procedure Check_One_Tagged_Type_Or_Extension_At_Most; 883 -- Issue an error in SPARK mode if a package specification contains 884 -- more than one tagged type or type extension. 885 886 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id); 887 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and 888 -- Is_True_Constant) on all variables that are entities of Id, and on 889 -- the chain whose first element is FE. A recursive call is made for all 890 -- packages and generic packages. 891 892 procedure Generate_Parent_References; 893 -- For a child unit, generate references to parent units, for 894 -- GPS navigation purposes. 895 896 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean; 897 -- Child and Unit are entities of compilation units. True if Child 898 -- is a public child of Parent as defined in 10.1.1 899 900 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id); 901 -- Reject completion of an incomplete or private type declarations 902 -- having a known discriminant part by an unchecked union. 903 904 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id); 905 -- Given the package entity of a generic package instantiation or 906 -- formal package whose corresponding generic is a child unit, installs 907 -- the private declarations of each of the child unit's parents. 908 -- This has to be done at the point of entering the instance package's 909 -- private part rather than being done in Sem_Ch12.Install_Parent 910 -- (which is where the parents' visible declarations are installed). 911 912 ------------------------------------------------ 913 -- Check_One_Tagged_Type_Or_Extension_At_Most -- 914 ------------------------------------------------ 915 916 procedure Check_One_Tagged_Type_Or_Extension_At_Most is 917 Previous : Node_Id; 918 919 procedure Check_Decls (Decls : List_Id); 920 -- Check that either Previous is Empty and Decls does not contain 921 -- more than one tagged type or type extension, or Previous is 922 -- already set and Decls contains no tagged type or type extension. 923 924 ----------------- 925 -- Check_Decls -- 926 ----------------- 927 928 procedure Check_Decls (Decls : List_Id) is 929 Decl : Node_Id; 930 931 begin 932 Decl := First (Decls); 933 while Present (Decl) loop 934 if Nkind (Decl) = N_Full_Type_Declaration 935 and then Is_Tagged_Type (Defining_Identifier (Decl)) 936 then 937 if No (Previous) then 938 Previous := Decl; 939 940 else 941 Error_Msg_Sloc := Sloc (Previous); 942 Check_SPARK_Restriction 943 ("at most one tagged type or type extension allowed", 944 "\\ previous declaration#", 945 Decl); 946 end if; 947 end if; 948 949 Next (Decl); 950 end loop; 951 end Check_Decls; 952 953 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most 954 955 begin 956 Previous := Empty; 957 Check_Decls (Vis_Decls); 958 959 if Present (Priv_Decls) then 960 Check_Decls (Priv_Decls); 961 end if; 962 end Check_One_Tagged_Type_Or_Extension_At_Most; 963 964 --------------------- 965 -- Clear_Constants -- 966 --------------------- 967 968 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is 969 E : Entity_Id; 970 971 begin 972 -- Ignore package renamings, not interesting and they can cause self 973 -- referential loops in the code below. 974 975 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then 976 return; 977 end if; 978 979 -- Note: in the loop below, the check for Next_Entity pointing back 980 -- to the package entity may seem odd, but it is needed, because a 981 -- package can contain a renaming declaration to itself, and such 982 -- renamings are generated automatically within package instances. 983 984 E := FE; 985 while Present (E) and then E /= Id loop 986 if Is_Assignable (E) then 987 Set_Never_Set_In_Source (E, False); 988 Set_Is_True_Constant (E, False); 989 Set_Current_Value (E, Empty); 990 Set_Is_Known_Null (E, False); 991 Set_Last_Assignment (E, Empty); 992 993 if not Can_Never_Be_Null (E) then 994 Set_Is_Known_Non_Null (E, False); 995 end if; 996 997 elsif Is_Package_Or_Generic_Package (E) then 998 Clear_Constants (E, First_Entity (E)); 999 Clear_Constants (E, First_Private_Entity (E)); 1000 end if; 1001 1002 Next_Entity (E); 1003 end loop; 1004 end Clear_Constants; 1005 1006 -------------------------------- 1007 -- Generate_Parent_References -- 1008 -------------------------------- 1009 1010 procedure Generate_Parent_References is 1011 Decl : constant Node_Id := Parent (N); 1012 1013 begin 1014 if Id = Cunit_Entity (Main_Unit) 1015 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit)) 1016 then 1017 Generate_Reference (Id, Scope (Id), 'k', False); 1018 1019 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body, 1020 N_Subunit) 1021 then 1022 -- If current unit is an ancestor of main unit, generate a 1023 -- reference to its own parent. 1024 1025 declare 1026 U : Node_Id; 1027 Main_Spec : Node_Id := Unit (Cunit (Main_Unit)); 1028 1029 begin 1030 if Nkind (Main_Spec) = N_Package_Body then 1031 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit))); 1032 end if; 1033 1034 U := Parent_Spec (Main_Spec); 1035 while Present (U) loop 1036 if U = Parent (Decl) then 1037 Generate_Reference (Id, Scope (Id), 'k', False); 1038 exit; 1039 1040 elsif Nkind (Unit (U)) = N_Package_Body then 1041 exit; 1042 1043 else 1044 U := Parent_Spec (Unit (U)); 1045 end if; 1046 end loop; 1047 end; 1048 end if; 1049 end Generate_Parent_References; 1050 1051 --------------------- 1052 -- Is_Public_Child -- 1053 --------------------- 1054 1055 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is 1056 begin 1057 if not Is_Private_Descendant (Child) then 1058 return True; 1059 else 1060 if Child = Unit then 1061 return not Private_Present ( 1062 Parent (Unit_Declaration_Node (Child))); 1063 else 1064 return Is_Public_Child (Scope (Child), Unit); 1065 end if; 1066 end if; 1067 end Is_Public_Child; 1068 1069 ---------------------------------------- 1070 -- Inspect_Unchecked_Union_Completion -- 1071 ---------------------------------------- 1072 1073 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is 1074 Decl : Node_Id; 1075 1076 begin 1077 Decl := First (Decls); 1078 while Present (Decl) loop 1079 1080 -- We are looking at an incomplete or private type declaration 1081 -- with a known_discriminant_part whose full view is an 1082 -- Unchecked_Union. 1083 1084 if Nkind_In (Decl, N_Incomplete_Type_Declaration, 1085 N_Private_Type_Declaration) 1086 and then Has_Discriminants (Defining_Identifier (Decl)) 1087 and then Present (Full_View (Defining_Identifier (Decl))) 1088 and then 1089 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl))) 1090 then 1091 Error_Msg_N 1092 ("completion of discriminated partial view " 1093 & "cannot be an unchecked union", 1094 Full_View (Defining_Identifier (Decl))); 1095 end if; 1096 1097 Next (Decl); 1098 end loop; 1099 end Inspect_Unchecked_Union_Completion; 1100 1101 ----------------------------------------- 1102 -- Install_Parent_Private_Declarations -- 1103 ----------------------------------------- 1104 1105 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is 1106 Inst_Par : Entity_Id; 1107 Gen_Par : Entity_Id; 1108 Inst_Node : Node_Id; 1109 1110 begin 1111 Inst_Par := Inst_Id; 1112 1113 Gen_Par := 1114 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par))); 1115 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop 1116 Inst_Node := Get_Package_Instantiation_Node (Inst_Par); 1117 1118 if Nkind_In (Inst_Node, N_Package_Instantiation, 1119 N_Formal_Package_Declaration) 1120 and then Nkind (Name (Inst_Node)) = N_Expanded_Name 1121 then 1122 Inst_Par := Entity (Prefix (Name (Inst_Node))); 1123 1124 if Present (Renamed_Entity (Inst_Par)) then 1125 Inst_Par := Renamed_Entity (Inst_Par); 1126 end if; 1127 1128 Gen_Par := 1129 Generic_Parent 1130 (Specification (Unit_Declaration_Node (Inst_Par))); 1131 1132 -- Install the private declarations and private use clauses 1133 -- of a parent instance of the child instance, unless the 1134 -- parent instance private declarations have already been 1135 -- installed earlier in Analyze_Package_Specification, which 1136 -- happens when a generic child is instantiated, and the 1137 -- instance is a child of the parent instance. 1138 1139 -- Installing the use clauses of the parent instance twice 1140 -- is both unnecessary and wrong, because it would cause the 1141 -- clauses to be chained to themselves in the use clauses 1142 -- list of the scope stack entry. That in turn would cause 1143 -- an endless loop from End_Use_Clauses upon scope exit. 1144 1145 -- The parent is now fully visible. It may be a hidden open 1146 -- scope if we are currently compiling some child instance 1147 -- declared within it, but while the current instance is being 1148 -- compiled the parent is immediately visible. In particular 1149 -- its entities must remain visible if a stack save/restore 1150 -- takes place through a call to Rtsfind. 1151 1152 if Present (Gen_Par) then 1153 if not In_Private_Part (Inst_Par) then 1154 Install_Private_Declarations (Inst_Par); 1155 Set_Use (Private_Declarations 1156 (Specification 1157 (Unit_Declaration_Node (Inst_Par)))); 1158 Set_Is_Hidden_Open_Scope (Inst_Par, False); 1159 end if; 1160 1161 -- If we've reached the end of the generic instance parents, 1162 -- then finish off by looping through the nongeneric parents 1163 -- and installing their private declarations. 1164 1165 else 1166 while Present (Inst_Par) 1167 and then Inst_Par /= Standard_Standard 1168 and then (not In_Open_Scopes (Inst_Par) 1169 or else not In_Private_Part (Inst_Par)) 1170 loop 1171 Install_Private_Declarations (Inst_Par); 1172 Set_Use (Private_Declarations 1173 (Specification 1174 (Unit_Declaration_Node (Inst_Par)))); 1175 Inst_Par := Scope (Inst_Par); 1176 end loop; 1177 1178 exit; 1179 end if; 1180 1181 else 1182 exit; 1183 end if; 1184 end loop; 1185 end Install_Parent_Private_Declarations; 1186 1187 -- Start of processing for Analyze_Package_Specification 1188 1189 begin 1190 if Present (Vis_Decls) then 1191 Analyze_Declarations (Vis_Decls); 1192 end if; 1193 1194 -- Verify that incomplete types have received full declarations and 1195 -- also build invariant procedures for any types with invariants. 1196 1197 E := First_Entity (Id); 1198 while Present (E) loop 1199 1200 -- Check on incomplete types 1201 1202 -- AI05-0213: A formal incomplete type has no completion 1203 1204 if Ekind (E) = E_Incomplete_Type 1205 and then No (Full_View (E)) 1206 and then not Is_Generic_Type (E) 1207 then 1208 Error_Msg_N ("no declaration in visible part for incomplete}", E); 1209 end if; 1210 1211 -- Build invariant procedures 1212 1213 if Is_Type (E) and then Has_Invariants (E) then 1214 Build_Invariant_Procedure (E, N); 1215 end if; 1216 1217 Next_Entity (E); 1218 end loop; 1219 1220 if Is_Remote_Call_Interface (Id) 1221 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit 1222 then 1223 Validate_RCI_Declarations (Id); 1224 end if; 1225 1226 -- Save global references in the visible declarations, before installing 1227 -- private declarations of parent unit if there is one, because the 1228 -- privacy status of types defined in the parent will change. This is 1229 -- only relevant for generic child units, but is done in all cases for 1230 -- uniformity. 1231 1232 if Ekind (Id) = E_Generic_Package 1233 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration 1234 then 1235 declare 1236 Orig_Spec : constant Node_Id := Specification (Orig_Decl); 1237 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec); 1238 begin 1239 Set_Private_Declarations (Orig_Spec, Empty_List); 1240 Save_Global_References (Orig_Decl); 1241 Set_Private_Declarations (Orig_Spec, Save_Priv); 1242 end; 1243 end if; 1244 1245 -- If package is a public child unit, then make the private declarations 1246 -- of the parent visible. 1247 1248 Public_Child := False; 1249 1250 declare 1251 Par : Entity_Id; 1252 Pack_Decl : Node_Id; 1253 Par_Spec : Node_Id; 1254 1255 begin 1256 Par := Id; 1257 Par_Spec := Parent_Spec (Parent (N)); 1258 1259 -- If the package is formal package of an enclosing generic, it is 1260 -- transformed into a local generic declaration, and compiled to make 1261 -- its spec available. We need to retrieve the original generic to 1262 -- determine whether it is a child unit, and install its parents. 1263 1264 if No (Par_Spec) 1265 and then 1266 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration 1267 then 1268 Par := Entity (Name (Original_Node (Parent (N)))); 1269 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par)); 1270 end if; 1271 1272 if Present (Par_Spec) then 1273 Generate_Parent_References; 1274 1275 while Scope (Par) /= Standard_Standard 1276 and then Is_Public_Child (Id, Par) 1277 and then In_Open_Scopes (Par) 1278 loop 1279 Public_Child := True; 1280 Par := Scope (Par); 1281 Install_Private_Declarations (Par); 1282 Install_Private_With_Clauses (Par); 1283 Pack_Decl := Unit_Declaration_Node (Par); 1284 Set_Use (Private_Declarations (Specification (Pack_Decl))); 1285 end loop; 1286 end if; 1287 end; 1288 1289 if Is_Compilation_Unit (Id) then 1290 Install_Private_With_Clauses (Id); 1291 else 1292 1293 -- The current compilation unit may include private with_clauses, 1294 -- which are visible in the private part of the current nested 1295 -- package, and have to be installed now. This is not done for 1296 -- nested instantiations, where the private with_clauses of the 1297 -- enclosing unit have no effect once the instantiation info is 1298 -- established and we start analyzing the package declaration. 1299 1300 declare 1301 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit); 1302 begin 1303 if Is_Package_Or_Generic_Package (Comp_Unit) 1304 and then not In_Private_Part (Comp_Unit) 1305 and then not In_Instance 1306 then 1307 Install_Private_With_Clauses (Comp_Unit); 1308 Private_With_Clauses_Installed := True; 1309 end if; 1310 end; 1311 end if; 1312 1313 -- If this is a package associated with a generic instance or formal 1314 -- package, then the private declarations of each of the generic's 1315 -- parents must be installed at this point. 1316 1317 if Is_Generic_Instance (Id) then 1318 Install_Parent_Private_Declarations (Id); 1319 end if; 1320 1321 -- Analyze private part if present. The flag In_Private_Part is reset 1322 -- in End_Package_Scope. 1323 1324 L := Last_Entity (Id); 1325 1326 if Present (Priv_Decls) then 1327 Set_In_Private_Part (Id); 1328 1329 -- Upon entering a public child's private part, it may be necessary 1330 -- to declare subprograms that were derived in the package's visible 1331 -- part but not yet made visible. 1332 1333 if Public_Child then 1334 Declare_Inherited_Private_Subprograms (Id); 1335 end if; 1336 1337 Analyze_Declarations (Priv_Decls); 1338 1339 -- Check the private declarations for incomplete deferred constants 1340 1341 Inspect_Deferred_Constant_Completion (Priv_Decls); 1342 1343 -- The first private entity is the immediate follower of the last 1344 -- visible entity, if there was one. 1345 1346 if Present (L) then 1347 Set_First_Private_Entity (Id, Next_Entity (L)); 1348 else 1349 Set_First_Private_Entity (Id, First_Entity (Id)); 1350 end if; 1351 1352 -- There may be inherited private subprograms that need to be declared, 1353 -- even in the absence of an explicit private part. If there are any 1354 -- public declarations in the package and the package is a public child 1355 -- unit, then an implicit private part is assumed. 1356 1357 elsif Present (L) and then Public_Child then 1358 Set_In_Private_Part (Id); 1359 Declare_Inherited_Private_Subprograms (Id); 1360 Set_First_Private_Entity (Id, Next_Entity (L)); 1361 end if; 1362 1363 E := First_Entity (Id); 1364 while Present (E) loop 1365 1366 -- Check rule of 3.6(11), which in general requires waiting till all 1367 -- full types have been seen. 1368 1369 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then 1370 Check_Aliased_Component_Types (E); 1371 end if; 1372 1373 -- Check preelaborable initialization for full type completing a 1374 -- private type for which pragma Preelaborable_Initialization given. 1375 1376 if Is_Type (E) 1377 and then Must_Have_Preelab_Init (E) 1378 and then not Has_Preelaborable_Initialization (E) 1379 then 1380 Error_Msg_N 1381 ("full view of & does not have preelaborable initialization", E); 1382 end if; 1383 1384 -- An invariant may appear on a full view of a type 1385 1386 if Is_Type (E) 1387 and then Has_Private_Declaration (E) 1388 and then Nkind (Parent (E)) = N_Full_Type_Declaration 1389 and then Has_Aspects (Parent (E)) 1390 then 1391 declare 1392 ASN : Node_Id; 1393 1394 begin 1395 ASN := First (Aspect_Specifications (Parent (E))); 1396 while Present (ASN) loop 1397 if Chars (Identifier (ASN)) = Name_Invariant 1398 or else 1399 Chars (Identifier (ASN)) = Name_Type_Invariant 1400 then 1401 Build_Invariant_Procedure (E, N); 1402 exit; 1403 end if; 1404 1405 Next (ASN); 1406 end loop; 1407 end; 1408 end if; 1409 1410 Next_Entity (E); 1411 end loop; 1412 1413 -- Ada 2005 (AI-216): The completion of an incomplete or private type 1414 -- declaration having a known_discriminant_part shall not be an 1415 -- unchecked union type. 1416 1417 if Present (Vis_Decls) then 1418 Inspect_Unchecked_Union_Completion (Vis_Decls); 1419 end if; 1420 1421 if Present (Priv_Decls) then 1422 Inspect_Unchecked_Union_Completion (Priv_Decls); 1423 end if; 1424 1425 if Ekind (Id) = E_Generic_Package 1426 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration 1427 and then Present (Priv_Decls) 1428 then 1429 -- Save global references in private declarations, ignoring the 1430 -- visible declarations that were processed earlier. 1431 1432 declare 1433 Orig_Spec : constant Node_Id := Specification (Orig_Decl); 1434 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec); 1435 Save_Form : constant List_Id := 1436 Generic_Formal_Declarations (Orig_Decl); 1437 1438 begin 1439 Set_Visible_Declarations (Orig_Spec, Empty_List); 1440 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List); 1441 Save_Global_References (Orig_Decl); 1442 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form); 1443 Set_Visible_Declarations (Orig_Spec, Save_Vis); 1444 end; 1445 end if; 1446 1447 Process_End_Label (N, 'e', Id); 1448 1449 -- Remove private_with_clauses of enclosing compilation unit, if they 1450 -- were installed. 1451 1452 if Private_With_Clauses_Installed then 1453 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit)); 1454 end if; 1455 1456 -- For the case of a library level package, we must go through all the 1457 -- entities clearing the indications that the value may be constant and 1458 -- not modified. Why? Because any client of this package may modify 1459 -- these values freely from anywhere. This also applies to any nested 1460 -- packages or generic packages. 1461 1462 -- For now we unconditionally clear constants for packages that are 1463 -- instances of generic packages. The reason is that we do not have the 1464 -- body yet, and we otherwise think things are unreferenced when they 1465 -- are not. This should be fixed sometime (the effect is not terrible, 1466 -- we just lose some warnings, and also some cases of value propagation) 1467 -- ??? 1468 1469 if Is_Library_Level_Entity (Id) 1470 or else Is_Generic_Instance (Id) 1471 then 1472 Clear_Constants (Id, First_Entity (Id)); 1473 Clear_Constants (Id, First_Private_Entity (Id)); 1474 end if; 1475 1476 Check_One_Tagged_Type_Or_Extension_At_Most; 1477 end Analyze_Package_Specification; 1478 1479 -------------------------------------- 1480 -- Analyze_Private_Type_Declaration -- 1481 -------------------------------------- 1482 1483 procedure Analyze_Private_Type_Declaration (N : Node_Id) is 1484 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity); 1485 Id : constant Entity_Id := Defining_Identifier (N); 1486 1487 begin 1488 Generate_Definition (Id); 1489 Set_Is_Pure (Id, PF); 1490 Init_Size_Align (Id); 1491 1492 if not Is_Package_Or_Generic_Package (Current_Scope) 1493 or else In_Private_Part (Current_Scope) 1494 then 1495 Error_Msg_N ("invalid context for private declaration", N); 1496 end if; 1497 1498 New_Private_Type (N, Id, N); 1499 Set_Depends_On_Private (Id); 1500 1501 if Has_Aspects (N) then 1502 Analyze_Aspect_Specifications (N, Id); 1503 end if; 1504 end Analyze_Private_Type_Declaration; 1505 1506 ---------------------------------- 1507 -- Check_Anonymous_Access_Types -- 1508 ---------------------------------- 1509 1510 procedure Check_Anonymous_Access_Types 1511 (Spec_Id : Entity_Id; 1512 P_Body : Node_Id) 1513 is 1514 E : Entity_Id; 1515 IR : Node_Id; 1516 1517 begin 1518 -- Itype references are only needed by gigi, to force elaboration of 1519 -- itypes. In the absence of code generation, they are not needed. 1520 1521 if not Expander_Active then 1522 return; 1523 end if; 1524 1525 E := First_Entity (Spec_Id); 1526 while Present (E) loop 1527 if Ekind (E) = E_Anonymous_Access_Type 1528 and then From_With_Type (E) 1529 then 1530 IR := Make_Itype_Reference (Sloc (P_Body)); 1531 Set_Itype (IR, E); 1532 1533 if No (Declarations (P_Body)) then 1534 Set_Declarations (P_Body, New_List (IR)); 1535 else 1536 Prepend (IR, Declarations (P_Body)); 1537 end if; 1538 end if; 1539 1540 Next_Entity (E); 1541 end loop; 1542 end Check_Anonymous_Access_Types; 1543 1544 ------------------------------------------- 1545 -- Declare_Inherited_Private_Subprograms -- 1546 ------------------------------------------- 1547 1548 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is 1549 1550 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean; 1551 -- Check whether an inherited subprogram S is an operation of an 1552 -- untagged derived type T. 1553 1554 --------------------- 1555 -- Is_Primitive_Of -- 1556 --------------------- 1557 1558 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is 1559 Formal : Entity_Id; 1560 1561 begin 1562 -- If the full view is a scalar type, the type is the anonymous base 1563 -- type, but the operation mentions the first subtype, so check the 1564 -- signature against the base type. 1565 1566 if Base_Type (Etype (S)) = Base_Type (T) then 1567 return True; 1568 1569 else 1570 Formal := First_Formal (S); 1571 while Present (Formal) loop 1572 if Base_Type (Etype (Formal)) = Base_Type (T) then 1573 return True; 1574 end if; 1575 1576 Next_Formal (Formal); 1577 end loop; 1578 1579 return False; 1580 end if; 1581 end Is_Primitive_Of; 1582 1583 -- Local variables 1584 1585 E : Entity_Id; 1586 Op_List : Elist_Id; 1587 Op_Elmt : Elmt_Id; 1588 Op_Elmt_2 : Elmt_Id; 1589 Prim_Op : Entity_Id; 1590 New_Op : Entity_Id := Empty; 1591 Parent_Subp : Entity_Id; 1592 Tag : Entity_Id; 1593 1594 -- Start of processing for Declare_Inherited_Private_Subprograms 1595 1596 begin 1597 E := First_Entity (Id); 1598 while Present (E) loop 1599 1600 -- If the entity is a nonprivate type extension whose parent type 1601 -- is declared in an open scope, then the type may have inherited 1602 -- operations that now need to be made visible. Ditto if the entity 1603 -- is a formal derived type in a child unit. 1604 1605 if ((Is_Derived_Type (E) and then not Is_Private_Type (E)) 1606 or else 1607 (Nkind (Parent (E)) = N_Private_Extension_Declaration 1608 and then Is_Generic_Type (E))) 1609 and then In_Open_Scopes (Scope (Etype (E))) 1610 and then Is_Base_Type (E) 1611 then 1612 if Is_Tagged_Type (E) then 1613 Op_List := Primitive_Operations (E); 1614 New_Op := Empty; 1615 Tag := First_Tag_Component (E); 1616 1617 Op_Elmt := First_Elmt (Op_List); 1618 while Present (Op_Elmt) loop 1619 Prim_Op := Node (Op_Elmt); 1620 1621 -- Search primitives that are implicit operations with an 1622 -- internal name whose parent operation has a normal name. 1623 1624 if Present (Alias (Prim_Op)) 1625 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E 1626 and then not Comes_From_Source (Prim_Op) 1627 and then Is_Internal_Name (Chars (Prim_Op)) 1628 and then not Is_Internal_Name (Chars (Alias (Prim_Op))) 1629 then 1630 Parent_Subp := Alias (Prim_Op); 1631 1632 -- Case 1: Check if the type has also an explicit 1633 -- overriding for this primitive. 1634 1635 Op_Elmt_2 := Next_Elmt (Op_Elmt); 1636 while Present (Op_Elmt_2) loop 1637 1638 -- Skip entities with attribute Interface_Alias since 1639 -- they are not overriding primitives (these entities 1640 -- link an interface primitive with their covering 1641 -- primitive) 1642 1643 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp) 1644 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2)) 1645 and then No (Interface_Alias (Node (Op_Elmt_2))) 1646 then 1647 -- The private inherited operation has been 1648 -- overridden by an explicit subprogram: replace 1649 -- the former by the latter. 1650 1651 New_Op := Node (Op_Elmt_2); 1652 Replace_Elmt (Op_Elmt, New_Op); 1653 Remove_Elmt (Op_List, Op_Elmt_2); 1654 Set_Overridden_Operation (New_Op, Parent_Subp); 1655 1656 -- We don't need to inherit its dispatching slot. 1657 -- Set_All_DT_Position has previously ensured that 1658 -- the same slot was assigned to the two primitives 1659 1660 if Present (Tag) 1661 and then Present (DTC_Entity (New_Op)) 1662 and then Present (DTC_Entity (Prim_Op)) 1663 then 1664 pragma Assert (DT_Position (New_Op) 1665 = DT_Position (Prim_Op)); 1666 null; 1667 end if; 1668 1669 goto Next_Primitive; 1670 end if; 1671 1672 Next_Elmt (Op_Elmt_2); 1673 end loop; 1674 1675 -- Case 2: We have not found any explicit overriding and 1676 -- hence we need to declare the operation (i.e., make it 1677 -- visible). 1678 1679 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E)); 1680 1681 -- Inherit the dispatching slot if E is already frozen 1682 1683 if Is_Frozen (E) 1684 and then Present (DTC_Entity (Alias (Prim_Op))) 1685 then 1686 Set_DTC_Entity_Value (E, New_Op); 1687 Set_DT_Position (New_Op, 1688 DT_Position (Alias (Prim_Op))); 1689 end if; 1690 1691 pragma Assert 1692 (Is_Dispatching_Operation (New_Op) 1693 and then Node (Last_Elmt (Op_List)) = New_Op); 1694 1695 -- Substitute the new operation for the old one in the 1696 -- type's primitive operations list. Since the new 1697 -- operation was also just added to the end of list, 1698 -- the last element must be removed. 1699 1700 -- (Question: is there a simpler way of declaring the 1701 -- operation, say by just replacing the name of the 1702 -- earlier operation, reentering it in the in the symbol 1703 -- table (how?), and marking it as private???) 1704 1705 Replace_Elmt (Op_Elmt, New_Op); 1706 Remove_Last_Elmt (Op_List); 1707 end if; 1708 1709 <<Next_Primitive>> 1710 Next_Elmt (Op_Elmt); 1711 end loop; 1712 1713 -- Generate listing showing the contents of the dispatch table 1714 1715 if Debug_Flag_ZZ then 1716 Write_DT (E); 1717 end if; 1718 1719 else 1720 -- Non-tagged type, scan forward to locate inherited hidden 1721 -- operations. 1722 1723 Prim_Op := Next_Entity (E); 1724 while Present (Prim_Op) loop 1725 if Is_Subprogram (Prim_Op) 1726 and then Present (Alias (Prim_Op)) 1727 and then not Comes_From_Source (Prim_Op) 1728 and then Is_Internal_Name (Chars (Prim_Op)) 1729 and then not Is_Internal_Name (Chars (Alias (Prim_Op))) 1730 and then Is_Primitive_Of (E, Prim_Op) 1731 then 1732 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E)); 1733 end if; 1734 1735 Next_Entity (Prim_Op); 1736 end loop; 1737 end if; 1738 end if; 1739 1740 Next_Entity (E); 1741 end loop; 1742 end Declare_Inherited_Private_Subprograms; 1743 1744 ----------------------- 1745 -- End_Package_Scope -- 1746 ----------------------- 1747 1748 procedure End_Package_Scope (P : Entity_Id) is 1749 begin 1750 Uninstall_Declarations (P); 1751 Pop_Scope; 1752 end End_Package_Scope; 1753 1754 --------------------------- 1755 -- Exchange_Declarations -- 1756 --------------------------- 1757 1758 procedure Exchange_Declarations (Id : Entity_Id) is 1759 Full_Id : constant Entity_Id := Full_View (Id); 1760 H1 : constant Entity_Id := Homonym (Id); 1761 Next1 : constant Entity_Id := Next_Entity (Id); 1762 H2 : Entity_Id; 1763 Next2 : Entity_Id; 1764 1765 begin 1766 -- If missing full declaration for type, nothing to exchange 1767 1768 if No (Full_Id) then 1769 return; 1770 end if; 1771 1772 -- Otherwise complete the exchange, and preserve semantic links 1773 1774 Next2 := Next_Entity (Full_Id); 1775 H2 := Homonym (Full_Id); 1776 1777 -- Reset full declaration pointer to reflect the switched entities and 1778 -- readjust the next entity chains. 1779 1780 Exchange_Entities (Id, Full_Id); 1781 1782 Set_Next_Entity (Id, Next1); 1783 Set_Homonym (Id, H1); 1784 1785 Set_Full_View (Full_Id, Id); 1786 Set_Next_Entity (Full_Id, Next2); 1787 Set_Homonym (Full_Id, H2); 1788 end Exchange_Declarations; 1789 1790 ---------------------------- 1791 -- Install_Package_Entity -- 1792 ---------------------------- 1793 1794 procedure Install_Package_Entity (Id : Entity_Id) is 1795 begin 1796 if not Is_Internal (Id) then 1797 if Debug_Flag_E then 1798 Write_Str ("Install: "); 1799 Write_Name (Chars (Id)); 1800 Write_Eol; 1801 end if; 1802 1803 if not Is_Child_Unit (Id) then 1804 Set_Is_Immediately_Visible (Id); 1805 end if; 1806 1807 end if; 1808 end Install_Package_Entity; 1809 1810 ---------------------------------- 1811 -- Install_Private_Declarations -- 1812 ---------------------------------- 1813 1814 procedure Install_Private_Declarations (P : Entity_Id) is 1815 Id : Entity_Id; 1816 Priv_Elmt : Elmt_Id; 1817 Priv : Entity_Id; 1818 Full : Entity_Id; 1819 1820 begin 1821 -- First exchange declarations for private types, so that the full 1822 -- declaration is visible. For each private type, we check its 1823 -- Private_Dependents list and also exchange any subtypes of or derived 1824 -- types from it. Finally, if this is a Taft amendment type, the 1825 -- incomplete declaration is irrelevant, and we want to link the 1826 -- eventual full declaration with the original private one so we also 1827 -- skip the exchange. 1828 1829 Id := First_Entity (P); 1830 while Present (Id) and then Id /= First_Private_Entity (P) loop 1831 if Is_Private_Base_Type (Id) 1832 and then Comes_From_Source (Full_View (Id)) 1833 and then Present (Full_View (Id)) 1834 and then Scope (Full_View (Id)) = Scope (Id) 1835 and then Ekind (Full_View (Id)) /= E_Incomplete_Type 1836 then 1837 -- If there is a use-type clause on the private type, set the 1838 -- full view accordingly. 1839 1840 Set_In_Use (Full_View (Id), In_Use (Id)); 1841 Full := Full_View (Id); 1842 1843 if Is_Private_Base_Type (Full) 1844 and then Has_Private_Declaration (Full) 1845 and then Nkind (Parent (Full)) = N_Full_Type_Declaration 1846 and then In_Open_Scopes (Scope (Etype (Full))) 1847 and then In_Package_Body (Current_Scope) 1848 and then not Is_Private_Type (Etype (Full)) 1849 then 1850 -- This is the completion of a private type by a derivation 1851 -- from another private type which is not private anymore. This 1852 -- can only happen in a package nested within a child package, 1853 -- when the parent type is defined in the parent unit. At this 1854 -- point the current type is not private either, and we have to 1855 -- install the underlying full view, which is now visible. Save 1856 -- the current full view as well, so that all views can be 1857 -- restored on exit. It may seem that after compiling the child 1858 -- body there are not environments to restore, but the back-end 1859 -- expects those links to be valid, and freeze nodes depend on 1860 -- them. 1861 1862 if No (Full_View (Full)) 1863 and then Present (Underlying_Full_View (Full)) 1864 then 1865 Set_Full_View (Id, Underlying_Full_View (Full)); 1866 Set_Underlying_Full_View (Id, Full); 1867 1868 Set_Underlying_Full_View (Full, Empty); 1869 Set_Is_Frozen (Full_View (Id)); 1870 end if; 1871 end if; 1872 1873 Priv_Elmt := First_Elmt (Private_Dependents (Id)); 1874 1875 Exchange_Declarations (Id); 1876 Set_Is_Immediately_Visible (Id); 1877 1878 while Present (Priv_Elmt) loop 1879 Priv := Node (Priv_Elmt); 1880 1881 -- Before the exchange, verify that the presence of the 1882 -- Full_View field. It will be empty if the entity has already 1883 -- been installed due to a previous call. 1884 1885 if Present (Full_View (Priv)) 1886 and then Is_Visible_Dependent (Priv) 1887 then 1888 1889 -- For each subtype that is swapped, we also swap the 1890 -- reference to it in Private_Dependents, to allow access 1891 -- to it when we swap them out in End_Package_Scope. 1892 1893 Replace_Elmt (Priv_Elmt, Full_View (Priv)); 1894 Exchange_Declarations (Priv); 1895 Set_Is_Immediately_Visible 1896 (Priv, In_Open_Scopes (Scope (Priv))); 1897 Set_Is_Potentially_Use_Visible 1898 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt))); 1899 end if; 1900 1901 Next_Elmt (Priv_Elmt); 1902 end loop; 1903 end if; 1904 1905 Next_Entity (Id); 1906 end loop; 1907 1908 -- Next make other declarations in the private part visible as well 1909 1910 Id := First_Private_Entity (P); 1911 while Present (Id) loop 1912 Install_Package_Entity (Id); 1913 Set_Is_Hidden (Id, False); 1914 Next_Entity (Id); 1915 end loop; 1916 1917 -- Indicate that the private part is currently visible, so it can be 1918 -- properly reset on exit. 1919 1920 Set_In_Private_Part (P); 1921 end Install_Private_Declarations; 1922 1923 ---------------------------------- 1924 -- Install_Visible_Declarations -- 1925 ---------------------------------- 1926 1927 procedure Install_Visible_Declarations (P : Entity_Id) is 1928 Id : Entity_Id; 1929 Last_Entity : Entity_Id; 1930 1931 begin 1932 pragma Assert 1933 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P)); 1934 1935 if Is_Package_Or_Generic_Package (P) then 1936 Last_Entity := First_Private_Entity (P); 1937 else 1938 Last_Entity := Empty; 1939 end if; 1940 1941 Id := First_Entity (P); 1942 while Present (Id) and then Id /= Last_Entity loop 1943 Install_Package_Entity (Id); 1944 Next_Entity (Id); 1945 end loop; 1946 end Install_Visible_Declarations; 1947 1948 -------------------------- 1949 -- Is_Private_Base_Type -- 1950 -------------------------- 1951 1952 function Is_Private_Base_Type (E : Entity_Id) return Boolean is 1953 begin 1954 return Ekind (E) = E_Private_Type 1955 or else Ekind (E) = E_Limited_Private_Type 1956 or else Ekind (E) = E_Record_Type_With_Private; 1957 end Is_Private_Base_Type; 1958 1959 -------------------------- 1960 -- Is_Visible_Dependent -- 1961 -------------------------- 1962 1963 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean 1964 is 1965 S : constant Entity_Id := Scope (Dep); 1966 1967 begin 1968 -- Renamings created for actual types have the visibility of the actual 1969 1970 if Ekind (S) = E_Package 1971 and then Is_Generic_Instance (S) 1972 and then (Is_Generic_Actual_Type (Dep) 1973 or else Is_Generic_Actual_Type (Full_View (Dep))) 1974 then 1975 return True; 1976 1977 elsif not (Is_Derived_Type (Dep)) 1978 and then Is_Derived_Type (Full_View (Dep)) 1979 then 1980 -- When instantiating a package body, the scope stack is empty, so 1981 -- check instead whether the dependent type is defined in the same 1982 -- scope as the instance itself. 1983 1984 return In_Open_Scopes (S) 1985 or else (Is_Generic_Instance (Current_Scope) 1986 and then Scope (Dep) = Scope (Current_Scope)); 1987 else 1988 return True; 1989 end if; 1990 end Is_Visible_Dependent; 1991 1992 ---------------------------- 1993 -- May_Need_Implicit_Body -- 1994 ---------------------------- 1995 1996 procedure May_Need_Implicit_Body (E : Entity_Id) is 1997 P : constant Node_Id := Unit_Declaration_Node (E); 1998 S : constant Node_Id := Parent (P); 1999 B : Node_Id; 2000 Decls : List_Id; 2001 2002 begin 2003 if not Has_Completion (E) 2004 and then Nkind (P) = N_Package_Declaration 2005 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E)) 2006 then 2007 B := 2008 Make_Package_Body (Sloc (E), 2009 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E), 2010 Chars => Chars (E)), 2011 Declarations => New_List); 2012 2013 if Nkind (S) = N_Package_Specification then 2014 if Present (Private_Declarations (S)) then 2015 Decls := Private_Declarations (S); 2016 else 2017 Decls := Visible_Declarations (S); 2018 end if; 2019 else 2020 Decls := Declarations (S); 2021 end if; 2022 2023 Append (B, Decls); 2024 Analyze (B); 2025 end if; 2026 end May_Need_Implicit_Body; 2027 2028 ---------------------- 2029 -- New_Private_Type -- 2030 ---------------------- 2031 2032 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is 2033 begin 2034 -- For other than Ada 2012, enter the name in the current scope 2035 2036 if Ada_Version < Ada_2012 then 2037 Enter_Name (Id); 2038 2039 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling 2040 -- private type that completes an incomplete type. 2041 2042 else 2043 declare 2044 Prev : Entity_Id; 2045 begin 2046 Prev := Find_Type_Name (N); 2047 pragma Assert (Prev = Id 2048 or else (Ekind (Prev) = E_Incomplete_Type 2049 and then Present (Full_View (Prev)) 2050 and then Full_View (Prev) = Id)); 2051 end; 2052 end if; 2053 2054 if Limited_Present (Def) then 2055 Set_Ekind (Id, E_Limited_Private_Type); 2056 else 2057 Set_Ekind (Id, E_Private_Type); 2058 end if; 2059 2060 Set_Etype (Id, Id); 2061 Set_Has_Delayed_Freeze (Id); 2062 Set_Is_First_Subtype (Id); 2063 Init_Size_Align (Id); 2064 2065 Set_Is_Constrained (Id, 2066 No (Discriminant_Specifications (N)) 2067 and then not Unknown_Discriminants_Present (N)); 2068 2069 -- Set tagged flag before processing discriminants, to catch illegal 2070 -- usage. 2071 2072 Set_Is_Tagged_Type (Id, Tagged_Present (Def)); 2073 2074 Set_Discriminant_Constraint (Id, No_Elist); 2075 Set_Stored_Constraint (Id, No_Elist); 2076 2077 if Present (Discriminant_Specifications (N)) then 2078 Push_Scope (Id); 2079 Process_Discriminants (N); 2080 End_Scope; 2081 2082 elsif Unknown_Discriminants_Present (N) then 2083 Set_Has_Unknown_Discriminants (Id); 2084 end if; 2085 2086 Set_Private_Dependents (Id, New_Elmt_List); 2087 2088 if Tagged_Present (Def) then 2089 Set_Ekind (Id, E_Record_Type_With_Private); 2090 Set_Direct_Primitive_Operations (Id, New_Elmt_List); 2091 Set_Is_Abstract_Type (Id, Abstract_Present (Def)); 2092 Set_Is_Limited_Record (Id, Limited_Present (Def)); 2093 Set_Has_Delayed_Freeze (Id, True); 2094 2095 -- Create a class-wide type with the same attributes 2096 2097 Make_Class_Wide_Type (Id); 2098 2099 elsif Abstract_Present (Def) then 2100 Error_Msg_N ("only a tagged type can be abstract", N); 2101 end if; 2102 end New_Private_Type; 2103 2104 ---------------------------- 2105 -- Uninstall_Declarations -- 2106 ---------------------------- 2107 2108 procedure Uninstall_Declarations (P : Entity_Id) is 2109 Decl : constant Node_Id := Unit_Declaration_Node (P); 2110 Id : Entity_Id; 2111 Full : Entity_Id; 2112 Priv_Elmt : Elmt_Id; 2113 Priv_Sub : Entity_Id; 2114 2115 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id); 2116 -- Copy to the private declaration the attributes of the full view that 2117 -- need to be available for the partial view also. 2118 2119 function Type_In_Use (T : Entity_Id) return Boolean; 2120 -- Check whether type or base type appear in an active use_type clause 2121 2122 ------------------------------ 2123 -- Preserve_Full_Attributes -- 2124 ------------------------------ 2125 2126 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is 2127 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv); 2128 2129 begin 2130 Set_Size_Info (Priv, (Full)); 2131 Set_RM_Size (Priv, RM_Size (Full)); 2132 Set_Size_Known_At_Compile_Time 2133 (Priv, Size_Known_At_Compile_Time (Full)); 2134 Set_Is_Volatile (Priv, Is_Volatile (Full)); 2135 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full)); 2136 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full)); 2137 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full)); 2138 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full)); 2139 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full)); 2140 Set_Has_Pragma_Unreferenced_Objects 2141 (Priv, Has_Pragma_Unreferenced_Objects 2142 (Full)); 2143 if Is_Unchecked_Union (Full) then 2144 Set_Is_Unchecked_Union (Base_Type (Priv)); 2145 end if; 2146 -- Why is atomic not copied here ??? 2147 2148 if Referenced (Full) then 2149 Set_Referenced (Priv); 2150 end if; 2151 2152 if Priv_Is_Base_Type then 2153 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full))); 2154 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only 2155 (Base_Type (Full))); 2156 Set_Has_Task (Priv, Has_Task (Base_Type (Full))); 2157 Set_Has_Controlled_Component (Priv, Has_Controlled_Component 2158 (Base_Type (Full))); 2159 end if; 2160 2161 Set_Freeze_Node (Priv, Freeze_Node (Full)); 2162 2163 -- Propagate information of type invariants, which may be specified 2164 -- for the full view. 2165 2166 if Has_Invariants (Full) and not Has_Invariants (Priv) then 2167 Set_Has_Invariants (Priv); 2168 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full)); 2169 end if; 2170 2171 if Is_Tagged_Type (Priv) 2172 and then Is_Tagged_Type (Full) 2173 and then not Error_Posted (Full) 2174 then 2175 if Is_Tagged_Type (Priv) then 2176 2177 -- If the type is tagged, the tag itself must be available on 2178 -- the partial view, for expansion purposes. 2179 2180 Set_First_Entity (Priv, First_Entity (Full)); 2181 2182 -- If there are discriminants in the partial view, these remain 2183 -- visible. Otherwise only the tag itself is visible, and there 2184 -- are no nameable components in the partial view. 2185 2186 if No (Last_Entity (Priv)) then 2187 Set_Last_Entity (Priv, First_Entity (Priv)); 2188 end if; 2189 end if; 2190 2191 Set_Has_Discriminants (Priv, Has_Discriminants (Full)); 2192 2193 if Has_Discriminants (Full) then 2194 Set_Discriminant_Constraint (Priv, 2195 Discriminant_Constraint (Full)); 2196 end if; 2197 end if; 2198 end Preserve_Full_Attributes; 2199 2200 ----------------- 2201 -- Type_In_Use -- 2202 ----------------- 2203 2204 function Type_In_Use (T : Entity_Id) return Boolean is 2205 begin 2206 return Scope (Base_Type (T)) = P 2207 and then (In_Use (T) or else In_Use (Base_Type (T))); 2208 end Type_In_Use; 2209 2210 -- Start of processing for Uninstall_Declarations 2211 2212 begin 2213 Id := First_Entity (P); 2214 while Present (Id) and then Id /= First_Private_Entity (P) loop 2215 if Debug_Flag_E then 2216 Write_Str ("unlinking visible entity "); 2217 Write_Int (Int (Id)); 2218 Write_Eol; 2219 end if; 2220 2221 -- On exit from the package scope, we must preserve the visibility 2222 -- established by use clauses in the current scope. Two cases: 2223 2224 -- a) If the entity is an operator, it may be a primitive operator of 2225 -- a type for which there is a visible use-type clause. 2226 2227 -- b) for other entities, their use-visibility is determined by a 2228 -- visible use clause for the package itself. For a generic instance, 2229 -- the instantiation of the formals appears in the visible part, 2230 -- but the formals are private and remain so. 2231 2232 if Ekind (Id) = E_Function 2233 and then Is_Operator_Symbol_Name (Chars (Id)) 2234 and then not Is_Hidden (Id) 2235 and then not Error_Posted (Id) 2236 then 2237 Set_Is_Potentially_Use_Visible (Id, 2238 In_Use (P) 2239 or else Type_In_Use (Etype (Id)) 2240 or else Type_In_Use (Etype (First_Formal (Id))) 2241 or else (Present (Next_Formal (First_Formal (Id))) 2242 and then 2243 Type_In_Use 2244 (Etype (Next_Formal (First_Formal (Id)))))); 2245 else 2246 if In_Use (P) and then not Is_Hidden (Id) then 2247 2248 -- A child unit of a use-visible package remains use-visible 2249 -- only if it is itself a visible child unit. Otherwise it 2250 -- would remain visible in other contexts where P is use- 2251 -- visible, because once compiled it stays in the entity list 2252 -- of its parent unit. 2253 2254 if Is_Child_Unit (Id) then 2255 Set_Is_Potentially_Use_Visible 2256 (Id, Is_Visible_Lib_Unit (Id)); 2257 else 2258 Set_Is_Potentially_Use_Visible (Id); 2259 end if; 2260 2261 else 2262 Set_Is_Potentially_Use_Visible (Id, False); 2263 end if; 2264 end if; 2265 2266 -- Local entities are not immediately visible outside of the package 2267 2268 Set_Is_Immediately_Visible (Id, False); 2269 2270 -- If this is a private type with a full view (for example a local 2271 -- subtype of a private type declared elsewhere), ensure that the 2272 -- full view is also removed from visibility: it may be exposed when 2273 -- swapping views in an instantiation. 2274 2275 if Is_Type (Id) and then Present (Full_View (Id)) then 2276 Set_Is_Immediately_Visible (Full_View (Id), False); 2277 end if; 2278 2279 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then 2280 Check_Abstract_Overriding (Id); 2281 Check_Conventions (Id); 2282 end if; 2283 2284 if (Ekind (Id) = E_Private_Type 2285 or else Ekind (Id) = E_Limited_Private_Type) 2286 and then No (Full_View (Id)) 2287 and then not Is_Generic_Type (Id) 2288 and then not Is_Derived_Type (Id) 2289 then 2290 Error_Msg_N ("missing full declaration for private type&", Id); 2291 2292 elsif Ekind (Id) = E_Record_Type_With_Private 2293 and then not Is_Generic_Type (Id) 2294 and then No (Full_View (Id)) 2295 then 2296 if Nkind (Parent (Id)) = N_Private_Type_Declaration then 2297 Error_Msg_N ("missing full declaration for private type&", Id); 2298 else 2299 Error_Msg_N 2300 ("missing full declaration for private extension", Id); 2301 end if; 2302 2303 -- Case of constant, check for deferred constant declaration with 2304 -- no full view. Likely just a matter of a missing expression, or 2305 -- accidental use of the keyword constant. 2306 2307 elsif Ekind (Id) = E_Constant 2308 2309 -- OK if constant value present 2310 2311 and then No (Constant_Value (Id)) 2312 2313 -- OK if full view present 2314 2315 and then No (Full_View (Id)) 2316 2317 -- OK if imported, since that provides the completion 2318 2319 and then not Is_Imported (Id) 2320 2321 -- OK if object declaration replaced by renaming declaration as 2322 -- a result of OK_To_Rename processing (e.g. for concatenation) 2323 2324 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration 2325 2326 -- OK if object declaration with the No_Initialization flag set 2327 2328 and then not (Nkind (Parent (Id)) = N_Object_Declaration 2329 and then No_Initialization (Parent (Id))) 2330 then 2331 -- If no private declaration is present, we assume the user did 2332 -- not intend a deferred constant declaration and the problem 2333 -- is simply that the initializing expression is missing. 2334 2335 if not Has_Private_Declaration (Etype (Id)) then 2336 2337 -- We assume that the user did not intend a deferred constant 2338 -- declaration, and the expression is just missing. 2339 2340 Error_Msg_N 2341 ("constant declaration requires initialization expression", 2342 Parent (Id)); 2343 2344 if Is_Limited_Type (Etype (Id)) then 2345 Error_Msg_N 2346 ("\if variable intended, remove CONSTANT from declaration", 2347 Parent (Id)); 2348 end if; 2349 2350 -- Otherwise if a private declaration is present, then we are 2351 -- missing the full declaration for the deferred constant. 2352 2353 else 2354 Error_Msg_N 2355 ("missing full declaration for deferred constant (RM 7.4)", 2356 Id); 2357 2358 if Is_Limited_Type (Etype (Id)) then 2359 Error_Msg_N 2360 ("\if variable intended, remove CONSTANT from declaration", 2361 Parent (Id)); 2362 end if; 2363 end if; 2364 end if; 2365 2366 Next_Entity (Id); 2367 end loop; 2368 2369 -- If the specification was installed as the parent of a public child 2370 -- unit, the private declarations were not installed, and there is 2371 -- nothing to do. 2372 2373 if not In_Private_Part (P) then 2374 return; 2375 else 2376 Set_In_Private_Part (P, False); 2377 end if; 2378 2379 -- Make private entities invisible and exchange full and private 2380 -- declarations for private types. Id is now the first private entity 2381 -- in the package. 2382 2383 while Present (Id) loop 2384 if Debug_Flag_E then 2385 Write_Str ("unlinking private entity "); 2386 Write_Int (Int (Id)); 2387 Write_Eol; 2388 end if; 2389 2390 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then 2391 Check_Abstract_Overriding (Id); 2392 Check_Conventions (Id); 2393 end if; 2394 2395 Set_Is_Immediately_Visible (Id, False); 2396 2397 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then 2398 Full := Full_View (Id); 2399 2400 -- If the partial view is not declared in the visible part of the 2401 -- package (as is the case when it is a type derived from some 2402 -- other private type in the private part of the current package), 2403 -- no exchange takes place. 2404 2405 if No (Parent (Id)) 2406 or else List_Containing (Parent (Id)) /= 2407 Visible_Declarations (Specification (Decl)) 2408 then 2409 goto Next_Id; 2410 end if; 2411 2412 -- The entry in the private part points to the full declaration, 2413 -- which is currently visible. Exchange them so only the private 2414 -- type declaration remains accessible, and link private and full 2415 -- declaration in the opposite direction. Before the actual 2416 -- exchange, we copy back attributes of the full view that must 2417 -- be available to the partial view too. 2418 2419 Preserve_Full_Attributes (Id, Full); 2420 2421 Set_Is_Potentially_Use_Visible (Id, In_Use (P)); 2422 2423 if Is_Indefinite_Subtype (Full) 2424 and then not Is_Indefinite_Subtype (Id) 2425 then 2426 Error_Msg_N 2427 ("full view of type must be definite subtype", Full); 2428 end if; 2429 2430 Priv_Elmt := First_Elmt (Private_Dependents (Id)); 2431 2432 -- Swap out the subtypes and derived types of Id that 2433 -- were compiled in this scope, or installed previously 2434 -- by Install_Private_Declarations. 2435 2436 -- Before we do the swap, we verify the presence of the Full_View 2437 -- field which may be empty due to a swap by a previous call to 2438 -- End_Package_Scope (e.g. from the freezing mechanism). 2439 2440 while Present (Priv_Elmt) loop 2441 Priv_Sub := Node (Priv_Elmt); 2442 2443 if Present (Full_View (Priv_Sub)) then 2444 if Scope (Priv_Sub) = P 2445 or else not In_Open_Scopes (Scope (Priv_Sub)) 2446 then 2447 Set_Is_Immediately_Visible (Priv_Sub, False); 2448 end if; 2449 2450 if Is_Visible_Dependent (Priv_Sub) then 2451 Preserve_Full_Attributes 2452 (Priv_Sub, Full_View (Priv_Sub)); 2453 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub)); 2454 Exchange_Declarations (Priv_Sub); 2455 end if; 2456 end if; 2457 2458 Next_Elmt (Priv_Elmt); 2459 end loop; 2460 2461 -- Now restore the type itself to its private view 2462 2463 Exchange_Declarations (Id); 2464 2465 -- If we have installed an underlying full view for a type derived 2466 -- from a private type in a child unit, restore the proper views 2467 -- of private and full view. See corresponding code in 2468 -- Install_Private_Declarations. 2469 2470 -- After the exchange, Full denotes the private type in the 2471 -- visible part of the package. 2472 2473 if Is_Private_Base_Type (Full) 2474 and then Present (Full_View (Full)) 2475 and then Present (Underlying_Full_View (Full)) 2476 and then In_Package_Body (Current_Scope) 2477 then 2478 Set_Full_View (Full, Underlying_Full_View (Full)); 2479 Set_Underlying_Full_View (Full, Empty); 2480 end if; 2481 2482 elsif Ekind (Id) = E_Incomplete_Type 2483 and then Comes_From_Source (Id) 2484 and then No (Full_View (Id)) 2485 then 2486 -- Mark Taft amendment types. Verify that there are no primitive 2487 -- operations declared for the type (3.10.1(9)). 2488 2489 Set_Has_Completion_In_Body (Id); 2490 2491 declare 2492 Elmt : Elmt_Id; 2493 Subp : Entity_Id; 2494 2495 begin 2496 Elmt := First_Elmt (Private_Dependents (Id)); 2497 while Present (Elmt) loop 2498 Subp := Node (Elmt); 2499 2500 -- Is_Primitive is tested because there can be cases where 2501 -- nonprimitive subprograms (in nested packages) are added 2502 -- to the Private_Dependents list. 2503 2504 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then 2505 Error_Msg_NE 2506 ("type& must be completed in the private part", 2507 Parent (Subp), Id); 2508 2509 -- The result type of an access-to-function type cannot be a 2510 -- Taft-amendment type, unless the version is Ada 2012 or 2511 -- later (see AI05-151). 2512 2513 elsif Ada_Version < Ada_2012 2514 and then Ekind (Subp) = E_Subprogram_Type 2515 then 2516 if Etype (Subp) = Id 2517 or else 2518 (Is_Class_Wide_Type (Etype (Subp)) 2519 and then Etype (Etype (Subp)) = Id) 2520 then 2521 Error_Msg_NE 2522 ("type& must be completed in the private part", 2523 Associated_Node_For_Itype (Subp), Id); 2524 end if; 2525 end if; 2526 2527 Next_Elmt (Elmt); 2528 end loop; 2529 end; 2530 2531 elsif not Is_Child_Unit (Id) 2532 and then (not Is_Private_Type (Id) 2533 or else No (Full_View (Id))) 2534 then 2535 Set_Is_Hidden (Id); 2536 Set_Is_Potentially_Use_Visible (Id, False); 2537 end if; 2538 2539 <<Next_Id>> 2540 Next_Entity (Id); 2541 end loop; 2542 end Uninstall_Declarations; 2543 2544 ------------------------ 2545 -- Unit_Requires_Body -- 2546 ------------------------ 2547 2548 function Unit_Requires_Body (P : Entity_Id) return Boolean is 2549 E : Entity_Id; 2550 2551 begin 2552 -- Imported entity never requires body. Right now, only subprograms can 2553 -- be imported, but perhaps in the future we will allow import of 2554 -- packages. 2555 2556 if Is_Imported (P) then 2557 return False; 2558 2559 -- Body required if library package with pragma Elaborate_Body 2560 2561 elsif Has_Pragma_Elaborate_Body (P) then 2562 return True; 2563 2564 -- Body required if subprogram 2565 2566 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then 2567 return True; 2568 2569 -- Treat a block as requiring a body 2570 2571 elsif Ekind (P) = E_Block then 2572 return True; 2573 2574 elsif Ekind (P) = E_Package 2575 and then Nkind (Parent (P)) = N_Package_Specification 2576 and then Present (Generic_Parent (Parent (P))) 2577 then 2578 declare 2579 G_P : constant Entity_Id := Generic_Parent (Parent (P)); 2580 begin 2581 if Has_Pragma_Elaborate_Body (G_P) then 2582 return True; 2583 end if; 2584 end; 2585 end if; 2586 2587 -- Otherwise search entity chain for entity requiring completion 2588 2589 E := First_Entity (P); 2590 while Present (E) loop 2591 2592 -- Always ignore child units. Child units get added to the entity 2593 -- list of a parent unit, but are not original entities of the 2594 -- parent, and so do not affect whether the parent needs a body. 2595 2596 if Is_Child_Unit (E) then 2597 null; 2598 2599 -- Ignore formal packages and their renamings 2600 2601 elsif Ekind (E) = E_Package 2602 and then Nkind (Original_Node (Unit_Declaration_Node (E))) = 2603 N_Formal_Package_Declaration 2604 then 2605 null; 2606 2607 -- Otherwise test to see if entity requires a completion. 2608 -- Note that subprogram entities whose declaration does not come 2609 -- from source are ignored here on the basis that we assume the 2610 -- expander will provide an implicit completion at some point. 2611 2612 elsif (Is_Overloadable (E) 2613 and then Ekind (E) /= E_Enumeration_Literal 2614 and then Ekind (E) /= E_Operator 2615 and then not Is_Abstract_Subprogram (E) 2616 and then not Has_Completion (E) 2617 and then Comes_From_Source (Parent (E))) 2618 2619 or else 2620 (Ekind (E) = E_Package 2621 and then E /= P 2622 and then not Has_Completion (E) 2623 and then Unit_Requires_Body (E)) 2624 2625 or else 2626 (Ekind (E) = E_Incomplete_Type 2627 and then No (Full_View (E)) 2628 and then not Is_Generic_Type (E)) 2629 2630 or else 2631 (Ekind_In (E, E_Task_Type, E_Protected_Type) 2632 and then not Has_Completion (E)) 2633 2634 or else 2635 (Ekind (E) = E_Generic_Package 2636 and then E /= P 2637 and then not Has_Completion (E) 2638 and then Unit_Requires_Body (E)) 2639 2640 or else 2641 (Is_Generic_Subprogram (E) 2642 and then not Has_Completion (E)) 2643 2644 then 2645 return True; 2646 2647 -- Entity that does not require completion 2648 2649 else 2650 null; 2651 end if; 2652 2653 Next_Entity (E); 2654 end loop; 2655 2656 return False; 2657 end Unit_Requires_Body; 2658 2659end Sem_Ch7; 2660