1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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.
31 with Aspects; use Aspects;
32 with Atree; use Atree;
33 with Contracts; use Contracts;
34 with Debug; use Debug;
35 with Einfo; use Einfo;
36 with Elists; use Elists;
37 with Errout; use Errout;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Dbug; use Exp_Dbug;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Nlists; use Nlists;
49 with Output; use Output;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Snames; use Snames;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinput; use Sinput;
70 with Uintp; use Uintp;
74 package body Sem_Ch7 is
76 -----------------------------------
77 -- Handling private declarations --
78 -----------------------------------
80 -- The principle that each entity has a single defining occurrence clashes
81 -- with the presence of two separate definitions for private types: the
82 -- first is the private type declaration, and the second is the full type
83 -- declaration. It is important that all references to the type point to
84 -- the same defining occurrence, namely the first one. To enforce the two
85 -- separate views of the entity, the corresponding information is swapped
86 -- between the two declarations. Outside of the package, the defining
87 -- occurrence only contains the private declaration information, while in
88 -- the private part and the body of the package the defining occurrence
89 -- contains the full declaration. To simplify the swap, the defining
90 -- occurrence that currently holds the private declaration points to the
91 -- full declaration. During semantic processing the defining occurrence
92 -- also points to a list of private dependents, that is to say access types
93 -- or composite types whose designated types or component types are
94 -- subtypes or derived types of the private type in question. After the
95 -- full declaration has been seen, the private dependents are updated to
96 -- indicate that they have full definitions.
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Analyze_Package_Body_Helper (N : Node_Id);
103 -- Does all the real work of Analyze_Package_Body
105 procedure Check_Anonymous_Access_Types
106 (Spec_Id : Entity_Id;
108 -- If the spec of a package has a limited_with_clause, it may declare
109 -- anonymous access types whose designated type is a limited view, such an
110 -- anonymous access return type for a function. This access type cannot be
111 -- elaborated in the spec itself, but it may need an itype reference if it
112 -- is used within a nested scope. In that case the itype reference is
113 -- created at the beginning of the corresponding package body and inserted
114 -- before other body declarations.
116 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
117 -- Called upon entering the private part of a public child package and the
118 -- body of a nested package, to potentially declare certain inherited
119 -- subprograms that were inherited by types in the visible part, but whose
120 -- declaration was deferred because the parent operation was private and
121 -- not visible at that point. These subprograms are located by traversing
122 -- the visible part declarations looking for non-private type extensions
123 -- and then examining each of the primitive operations of such types to
124 -- find those that were inherited but declared with a special internal
125 -- name. Each such operation is now declared as an operation with a normal
126 -- name (using the name of the parent operation) and replaces the previous
127 -- implicit operation in the primitive operations list of the type. If the
128 -- inherited private operation has been overridden, then it's replaced by
129 -- the overriding operation.
131 procedure Install_Package_Entity (Id : Entity_Id);
132 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
133 -- one entity on its visibility chain, and recurses on the visible part if
134 -- the entity is an inner package.
136 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
137 -- True for a private type that is not a subtype
139 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
140 -- If the private dependent is a private type whose full view is derived
141 -- from the parent type, its full properties are revealed only if we are in
142 -- the immediate scope of the private dependent. Should this predicate be
143 -- tightened further???
145 function Requires_Completion_In_Body
148 Do_Abstract_States : Boolean := False) return Boolean;
149 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
150 -- Determine whether entity Id declared in package spec Pack_Id requires
151 -- completion in a package body. Flag Do_Abstract_Stats should be set when
152 -- abstract states are to be considered in the completion test.
154 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
155 -- Outputs info messages showing why package Pack_Id requires a body. The
156 -- caller has checked that the switch requesting this information is set,
157 -- and that the package does indeed require a body.
159 --------------------------
160 -- Analyze_Package_Body --
161 --------------------------
163 procedure Analyze_Package_Body (N : Node_Id) is
164 Loc : constant Source_Ptr := Sloc (N);
168 Write_Str ("==> package body ");
169 Write_Name (Chars (Defining_Entity (N)));
170 Write_Str (" from ");
171 Write_Location (Loc);
176 -- The real work is split out into the helper, so it can do "return;"
177 -- without skipping the debug output.
179 Analyze_Package_Body_Helper (N);
183 Write_Str ("<== package body ");
184 Write_Name (Chars (Defining_Entity (N)));
185 Write_Str (" from ");
186 Write_Location (Loc);
189 end Analyze_Package_Body;
191 ------------------------------------------------------
192 -- Analyze_Package_Body_Helper Data and Subprograms --
193 ------------------------------------------------------
195 Entity_Table_Size : constant := 4093;
196 -- Number of headers in hash table
198 subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
199 -- Range of headers in hash table
201 function Node_Hash (Id : Entity_Id) return Entity_Header_Num;
202 -- Simple hash function for Entity_Ids
204 package Subprogram_Table is new GNAT.Htable.Simple_HTable
205 (Header_Num => Entity_Header_Num,
211 -- Hash table to record which subprograms are referenced. It is declared
212 -- at library level to avoid elaborating it for every call to Analyze.
214 package Traversed_Table is new GNAT.Htable.Simple_HTable
215 (Header_Num => Entity_Header_Num,
221 -- Hash table to record which nodes we have traversed, so we can avoid
222 -- traversing the same nodes repeatedly.
228 function Node_Hash (Id : Entity_Id) return Entity_Header_Num is
230 return Entity_Header_Num (Id mod Entity_Table_Size);
233 ---------------------------------
234 -- Analyze_Package_Body_Helper --
235 ---------------------------------
237 -- WARNING: This routine manages Ghost regions. Return statements must be
238 -- replaced by gotos which jump to the end of the routine and restore the
241 procedure Analyze_Package_Body_Helper (N : Node_Id) is
242 procedure Hide_Public_Entities (Decls : List_Id);
243 -- Attempt to hide all public entities found in declarative list Decls
244 -- by resetting their Is_Public flag to False depending on whether the
245 -- entities are not referenced by inlined or generic bodies. This kind
246 -- of processing is a conservative approximation and will still leave
247 -- entities externally visible if the package is not simple enough.
249 procedure Install_Composite_Operations (P : Entity_Id);
250 -- Composite types declared in the current scope may depend on types
251 -- that were private at the point of declaration, and whose full view
252 -- is now in scope. Indicate that the corresponding operations on the
253 -- composite type are available.
255 --------------------------
256 -- Hide_Public_Entities --
257 --------------------------
259 procedure Hide_Public_Entities (Decls : List_Id) is
260 function Has_Referencer
262 In_Nested_Instance : Boolean;
263 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean;
264 -- A "referencer" is a construct which may reference a previous
265 -- declaration. Examine all declarations in list Decls in reverse
266 -- and determine whether one such referencer exists. All entities
267 -- in the range Last (Decls) .. Referencer are hidden from external
270 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
271 -- Determine whether a node denotes a reference to a subprogram
273 procedure Traverse_And_Scan_Subprogram_Refs is
274 new Traverse_Proc (Scan_Subprogram_Ref);
275 -- Subsidiary to routine Has_Referencer. Determine whether a node
276 -- contains references to a subprogram and record them.
277 -- WARNING: this is a very expensive routine as it performs a full
280 procedure Scan_Subprogram_Refs (Node : Node_Id);
281 -- If we haven't already traversed Node, then mark it and traverse
288 function Has_Referencer
290 In_Nested_Instance : Boolean;
291 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean
297 Has_Referencer_Of_Non_Subprograms : Boolean :=
298 Has_Outer_Referencer_Of_Non_Subprograms;
299 -- Set if an inlined subprogram body was detected as a referencer.
300 -- In this case, we do not return True immediately but keep hiding
301 -- subprograms from external visibility.
308 -- Examine all declarations in reverse order, hiding all entities
309 -- from external visibility until a referencer has been found. The
310 -- algorithm recurses into nested packages.
312 Decl := Last (Decls);
313 while Present (Decl) loop
315 -- A stub is always considered a referencer
317 if Nkind (Decl) in N_Body_Stub then
320 -- Package declaration
322 elsif Nkind (Decl) = N_Package_Declaration then
323 Spec := Specification (Decl);
324 Decl_Id := Defining_Entity (Spec);
326 -- Inspect the declarations of a non-generic package to try
327 -- and hide more entities from external visibility.
329 if not Is_Generic_Unit (Decl_Id) then
330 if Has_Referencer (Private_Declarations (Spec),
333 Is_Generic_Instance (Decl_Id),
334 Has_Referencer_Of_Non_Subprograms)
336 Has_Referencer (Visible_Declarations (Spec),
339 Is_Generic_Instance (Decl_Id),
340 Has_Referencer_Of_Non_Subprograms)
348 elsif Nkind (Decl) = N_Package_Body
349 and then Present (Corresponding_Spec (Decl))
351 Decl_Id := Corresponding_Spec (Decl);
353 -- A generic package body is a referencer. It would seem
354 -- that we only have to consider generics that can be
355 -- exported, i.e. where the corresponding spec is the
356 -- spec of the current package, but because of nested
357 -- instantiations, a fully private generic body may export
358 -- other private body entities. Furthermore, regardless of
359 -- whether there was a previous inlined subprogram, (an
360 -- instantiation of) the generic package may reference any
361 -- entity declared before it.
363 if Is_Generic_Unit (Decl_Id) then
366 -- Inspect the declarations of a non-generic package body to
367 -- try and hide more entities from external visibility.
369 elsif Has_Referencer (Declarations (Decl),
372 Is_Generic_Instance (Decl_Id),
373 Has_Referencer_Of_Non_Subprograms)
380 elsif Nkind (Decl) = N_Subprogram_Body then
381 if Present (Corresponding_Spec (Decl)) then
382 Decl_Id := Corresponding_Spec (Decl);
384 -- A generic subprogram body acts as a referencer
386 if Is_Generic_Unit (Decl_Id) then
390 -- An inlined subprogram body acts as a referencer
391 -- unless we generate C code since inlining is then
392 -- handled by the C compiler.
394 -- Note that we test Has_Pragma_Inline here in addition
395 -- to Is_Inlined. We are doing this for a client, since
396 -- we are computing which entities should be public, and
397 -- it is the client who will decide if actual inlining
398 -- should occur, so we need to catch all cases where the
399 -- subprogram may be inlined by the client.
401 if not Generate_C_Code
402 and then (Is_Inlined (Decl_Id)
403 or else Has_Pragma_Inline (Decl_Id))
405 Has_Referencer_Of_Non_Subprograms := True;
407 -- Inspect the statements of the subprogram body
408 -- to determine whether the body references other
411 Scan_Subprogram_Refs (Decl);
414 -- Otherwise this is a stand alone subprogram body
417 Decl_Id := Defining_Entity (Decl);
419 -- An inlined subprogram body acts as a referencer
420 -- unless we generate C code since inlining is then
421 -- handled by the C compiler.
423 if not Generate_C_Code
424 and then (Is_Inlined (Decl_Id)
425 or else Has_Pragma_Inline (Decl_Id))
427 Has_Referencer_Of_Non_Subprograms := True;
429 -- Inspect the statements of the subprogram body
430 -- to determine whether the body references other
433 Scan_Subprogram_Refs (Decl);
435 -- Otherwise we can reset Is_Public right away
437 elsif not Subprogram_Table.Get (Decl_Id) then
438 Set_Is_Public (Decl_Id, False);
444 elsif Nkind (Decl) = N_Freeze_Entity then
447 pragma Unreferenced (Discard);
449 -- Inspect the actions to find references to subprograms.
450 -- We assume that the actions do not contain other kinds
451 -- of references and, therefore, we do not stop the scan
452 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
453 -- it would pessimize common cases for which the actions
454 -- contain the declaration of an init procedure, since
455 -- such a procedure is automatically marked inline.
458 Has_Referencer (Actions (Decl),
460 Has_Referencer_Of_Non_Subprograms);
463 -- Exceptions, objects and renamings do not need to be public
464 -- if they are not followed by a construct which can reference
467 elsif Nkind_In (Decl, N_Exception_Declaration,
468 N_Object_Declaration,
469 N_Object_Renaming_Declaration)
471 Decl_Id := Defining_Entity (Decl);
473 if not In_Nested_Instance
474 and then not Is_Imported (Decl_Id)
475 and then not Is_Exported (Decl_Id)
476 and then No (Interface_Name (Decl_Id))
477 and then not Has_Referencer_Of_Non_Subprograms
479 Set_Is_Public (Decl_Id, False);
482 -- Likewise for subprograms and renamings, but we work harder
483 -- for them to see whether they are referenced on an individual
484 -- basis by looking into the table of referenced subprograms.
486 elsif Nkind_In (Decl, N_Subprogram_Declaration,
487 N_Subprogram_Renaming_Declaration)
489 Decl_Id := Defining_Entity (Decl);
491 -- We cannot say anything for subprograms declared in nested
492 -- instances because instantiations are not done yet so the
493 -- bodies are not visible and could contain references to
494 -- them, except if we still have no subprograms at all which
495 -- are referenced by an inlined body.
497 if (not In_Nested_Instance
498 or else not Subprogram_Table.Get_First)
499 and then not Is_Imported (Decl_Id)
500 and then not Is_Exported (Decl_Id)
501 and then No (Interface_Name (Decl_Id))
502 and then not Subprogram_Table.Get (Decl_Id)
504 Set_Is_Public (Decl_Id, False);
507 -- For a subprogram renaming, if the entity is referenced,
508 -- then so is the renamed subprogram. But there is an issue
509 -- with generic bodies because instantiations are not done
510 -- yet and, therefore, cannot be scanned for referencers.
511 -- That's why we use an approximation and test that we have
512 -- at least one subprogram referenced by an inlined body
513 -- instead of precisely the entity of this renaming.
515 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
516 and then Subprogram_Table.Get_First
517 and then Is_Entity_Name (Name (Decl))
518 and then Present (Entity (Name (Decl)))
519 and then Is_Subprogram (Entity (Name (Decl)))
521 Subprogram_Table.Set (Entity (Name (Decl)), True);
528 return Has_Referencer_Of_Non_Subprograms;
531 -------------------------
532 -- Scan_Subprogram_Ref --
533 -------------------------
535 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
537 -- Detect a reference of the form
540 if Nkind (N) in N_Subprogram_Call
541 and then Is_Entity_Name (Name (N))
542 and then Present (Entity (Name (N)))
543 and then Is_Subprogram (Entity (Name (N)))
545 Subprogram_Table.Set (Entity (Name (N)), True);
547 -- Detect a reference of the form
548 -- Subp'Some_Attribute
550 elsif Nkind (N) = N_Attribute_Reference
551 and then Is_Entity_Name (Prefix (N))
552 and then Present (Entity (Prefix (N)))
553 and then Is_Subprogram (Entity (Prefix (N)))
555 Subprogram_Table.Set (Entity (Prefix (N)), True);
557 -- Constants can be substituted by their value in gigi, which may
558 -- contain a reference, so scan the value recursively.
560 elsif Is_Entity_Name (N)
561 and then Present (Entity (N))
562 and then Ekind (Entity (N)) = E_Constant
565 Val : constant Node_Id := Constant_Value (Entity (N));
568 and then not Compile_Time_Known_Value (Val)
570 Scan_Subprogram_Refs (Val);
576 end Scan_Subprogram_Ref;
578 --------------------------
579 -- Scan_Subprogram_Refs --
580 --------------------------
582 procedure Scan_Subprogram_Refs (Node : Node_Id) is
584 if not Traversed_Table.Get (Node) then
585 Traversed_Table.Set (Node, True);
586 Traverse_And_Scan_Subprogram_Refs (Node);
588 end Scan_Subprogram_Refs;
593 pragma Unreferenced (Discard);
595 -- Start of processing for Hide_Public_Entities
598 -- The algorithm examines the top level declarations of a package
599 -- body in reverse looking for a construct that may export entities
600 -- declared prior to it. If such a scenario is encountered, then all
601 -- entities in the range Last (Decls) .. construct are hidden from
602 -- external visibility. Consider:
610 -- package body Pack is
611 -- External_Obj : ...; -- (1)
613 -- package body Gen is -- (2)
614 -- ... External_Obj ... -- (3)
617 -- Local_Obj : ...; -- (4)
620 -- In this example Local_Obj (4) must not be externally visible as
621 -- it cannot be exported by anything in Pack. The body of generic
622 -- package Gen (2) on the other hand acts as a "referencer" and may
623 -- export anything declared before it. Since the compiler does not
624 -- perform flow analysis, it is not possible to determine precisely
625 -- which entities will be exported when Gen is instantiated. In the
626 -- example above External_Obj (1) is exported at (3), but this may
627 -- not always be the case. The algorithm takes a conservative stance
628 -- and leaves entity External_Obj public.
630 -- This very conservative algorithm is supplemented by a more precise
631 -- processing for inlined bodies. For them, we traverse the syntactic
632 -- tree and record which subprograms are actually referenced from it.
633 -- This makes it possible to compute a much smaller set of externally
634 -- visible subprograms in the absence of generic bodies, which can
635 -- have a significant impact on the inlining decisions made in the
636 -- back end and the removal of out-of-line bodies from the object
637 -- code. We do it only for inlined bodies because they are supposed
638 -- to be reasonably small and tree traversal is very expensive.
640 -- Note that even this special processing is not optimal for inlined
641 -- bodies, because we treat all inlined subprograms alike. An optimal
642 -- algorithm would require computing the transitive closure of the
643 -- inlined subprograms that can really be referenced from other units
644 -- in the source code.
646 -- We could extend this processing for inlined bodies and record all
647 -- entities, not just subprograms, referenced from them, which would
648 -- make it possible to compute a much smaller set of all externally
649 -- visible entities in the absence of generic bodies. But this would
650 -- mean implementing a more thorough tree traversal of the bodies,
651 -- i.e. not just syntactic, and the gain would very likely be worth
652 -- neither the hassle nor the slowdown of the compiler.
654 -- Finally, an important thing to be aware of is that, at this point,
655 -- instantiations are not done yet so we cannot directly see inlined
656 -- bodies coming from them. That's not catastrophic because only the
657 -- actual parameters of the instantiations matter here, and they are
658 -- present in the declarations list of the instantiated packages.
660 Traversed_Table.Reset;
661 Subprogram_Table.Reset;
662 Discard := Has_Referencer (Decls, False, False);
663 end Hide_Public_Entities;
665 ----------------------------------
666 -- Install_Composite_Operations --
667 ----------------------------------
669 procedure Install_Composite_Operations (P : Entity_Id) is
673 Id := First_Entity (P);
674 while Present (Id) loop
676 and then (Is_Limited_Composite (Id)
677 or else Is_Private_Composite (Id))
678 and then No (Private_Component (Id))
680 Set_Is_Limited_Composite (Id, False);
681 Set_Is_Private_Composite (Id, False);
686 end Install_Composite_Operations;
690 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
691 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
692 Saved_EA : constant Boolean := Expander_Active;
693 Saved_ISMP : constant Boolean :=
694 Ignore_SPARK_Mode_Pragmas_In_Instance;
695 -- Save the Ghost and SPARK mode-related data to restore on exit
699 Last_Spec_Entity : Entity_Id;
704 -- Start of processing for Analyze_Package_Body_Helper
707 -- Find corresponding package specification, and establish the current
708 -- scope. The visible defining entity for the package is the defining
709 -- occurrence in the spec. On exit from the package body, all body
710 -- declarations are attached to the defining entity for the body, but
711 -- the later is never used for name resolution. In this fashion there
712 -- is only one visible entity that denotes the package.
714 -- Set Body_Id. Note that this will be reset to point to the generic
715 -- copy later on in the generic case.
717 Body_Id := Defining_Entity (N);
719 -- Body is body of package instantiation. Corresponding spec has already
722 if Present (Corresponding_Spec (N)) then
723 Spec_Id := Corresponding_Spec (N);
724 Pack_Decl := Unit_Declaration_Node (Spec_Id);
727 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
730 and then Is_Package_Or_Generic_Package (Spec_Id)
732 Pack_Decl := Unit_Declaration_Node (Spec_Id);
734 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
735 Error_Msg_N ("cannot supply body for package renaming", N);
738 elsif Present (Corresponding_Body (Pack_Decl)) then
739 Error_Msg_N ("redefinition of package body", N);
744 Error_Msg_N ("missing specification for package body", N);
748 if Is_Package_Or_Generic_Package (Spec_Id)
749 and then (Scope (Spec_Id) = Standard_Standard
750 or else Is_Child_Unit (Spec_Id))
751 and then not Unit_Requires_Body (Spec_Id)
753 if Ada_Version = Ada_83 then
755 ("optional package body (not allowed in Ada 95)??", N);
757 Error_Msg_N ("spec of this package does not allow a body", N);
762 -- A [generic] package body freezes the contract of the nearest
763 -- enclosing package body and all other contracts encountered in
764 -- the same declarative part up to and excluding the package body:
766 -- package body Nearest_Enclosing_Package
767 -- with Refined_State => (State => Constit)
771 -- package body Freezes_Enclosing_Package_Body
772 -- with Refined_State => (State_2 => Constit_2)
777 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
779 -- This ensures that any annotations referenced by the contract of a
780 -- [generic] subprogram body declared within the current package body
781 -- are available. This form of freezing is decoupled from the usual
782 -- Freeze_xxx mechanism because it must also work in the context of
783 -- generics where normal freezing is disabled.
785 -- Only bodies coming from source should cause this type of freezing.
786 -- Instantiated generic bodies are excluded because their processing is
787 -- performed in a separate compilation pass which lacks enough semantic
788 -- information with respect to contract analysis. It is safe to suppress
789 -- the freezing of contracts in this case because this action already
790 -- took place at the end of the enclosing declarative part.
792 if Comes_From_Source (N)
793 and then not Is_Generic_Instance (Spec_Id)
795 Freeze_Previous_Contracts (N);
798 -- A package body is Ghost when the corresponding spec is Ghost. Set
799 -- the mode now to ensure that any nodes generated during analysis and
800 -- expansion are properly flagged as ignored Ghost.
802 Mark_And_Set_Ghost_Body (N, Spec_Id);
804 -- Deactivate expansion inside the body of ignored Ghost entities,
805 -- as this code will ultimately be ignored. This avoids requiring the
806 -- presence of run-time units which are not needed. Only do this for
807 -- user entities, as internally generated entities might still need
808 -- to be expanded (e.g. those generated for types).
810 if Present (Ignored_Ghost_Region)
811 and then Comes_From_Source (Body_Id)
813 Expander_Active := False;
816 -- If the body completes the initial declaration of a compilation unit
817 -- which is subject to pragma Elaboration_Checks, set the model of the
818 -- pragma because it applies to all parts of the unit.
820 Install_Elaboration_Model (Spec_Id);
822 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
823 Style.Check_Identifier (Body_Id, Spec_Id);
825 if Is_Child_Unit (Spec_Id) then
826 if Nkind (Parent (N)) /= N_Compilation_Unit then
828 ("body of child unit& cannot be an inner package", N, Spec_Id);
831 Set_Is_Child_Unit (Body_Id);
834 -- Generic package case
836 if Ekind (Spec_Id) = E_Generic_Package then
838 -- Disable expansion and perform semantic analysis on copy. The
839 -- unannotated body will be used in all instantiations.
841 Body_Id := Defining_Entity (N);
842 Set_Ekind (Body_Id, E_Package_Body);
843 Set_Scope (Body_Id, Scope (Spec_Id));
844 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
845 Set_Body_Entity (Spec_Id, Body_Id);
846 Set_Spec_Entity (Body_Id, Spec_Id);
848 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
851 -- Once the contents of the generic copy and the template are
852 -- swapped, do the same for their respective aspect specifications.
854 Exchange_Aspects (N, New_N);
856 -- Collect all contract-related source pragmas found within the
857 -- template and attach them to the contract of the package body.
858 -- This contract is used in the capture of global references within
861 Create_Generic_Contract (N);
863 -- Update Body_Id to point to the copied node for the remainder of
866 Body_Id := Defining_Entity (N);
870 -- The Body_Id is that of the copied node in the generic case, the
871 -- current node otherwise. Note that N was rewritten above, so we must
872 -- be sure to get the latest Body_Id value.
874 Set_Ekind (Body_Id, E_Package_Body);
875 Set_Body_Entity (Spec_Id, Body_Id);
876 Set_Spec_Entity (Body_Id, Spec_Id);
878 -- Defining name for the package body is not a visible entity: Only the
879 -- defining name for the declaration is visible.
881 Set_Etype (Body_Id, Standard_Void_Type);
882 Set_Scope (Body_Id, Scope (Spec_Id));
883 Set_Corresponding_Spec (N, Spec_Id);
884 Set_Corresponding_Body (Pack_Decl, Body_Id);
886 -- The body entity is not used for semantics or code generation, but
887 -- it is attached to the entity list of the enclosing scope to simplify
888 -- the listing of back-annotations for the types it main contain.
890 if Scope (Spec_Id) /= Standard_Standard then
891 Append_Entity (Body_Id, Scope (Spec_Id));
894 -- Indicate that we are currently compiling the body of the package
896 Set_In_Package_Body (Spec_Id);
897 Set_Has_Completion (Spec_Id);
898 Last_Spec_Entity := Last_Entity (Spec_Id);
900 if Has_Aspects (N) then
901 Analyze_Aspect_Specifications (N, Body_Id);
904 Push_Scope (Spec_Id);
906 -- Set SPARK_Mode only for non-generic package
908 if Ekind (Spec_Id) = E_Package then
909 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
910 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
911 Set_SPARK_Pragma_Inherited (Body_Id);
912 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
914 -- A package body may be instantiated or inlined at a later pass.
915 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
916 -- it applied to the package spec.
918 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
919 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
923 Set_Categorization_From_Pragmas (N);
925 Install_Visible_Declarations (Spec_Id);
926 Install_Private_Declarations (Spec_Id);
927 Install_Private_With_Clauses (Spec_Id);
928 Install_Composite_Operations (Spec_Id);
930 Check_Anonymous_Access_Types (Spec_Id, N);
932 if Ekind (Spec_Id) = E_Generic_Package then
933 Set_Use (Generic_Formal_Declarations (Pack_Decl));
936 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
937 Set_Use (Private_Declarations (Specification (Pack_Decl)));
939 -- This is a nested package, so it may be necessary to declare certain
940 -- inherited subprograms that are not yet visible because the parent
941 -- type's subprograms are now visible.
942 -- Note that for child units these operations were generated when
943 -- analyzing the package specification.
945 if Ekind (Scope (Spec_Id)) = E_Package
946 and then Scope (Spec_Id) /= Standard_Standard
947 and then not Is_Child_Unit (Spec_Id)
949 Declare_Inherited_Private_Subprograms (Spec_Id);
952 if Present (Declarations (N)) then
953 Analyze_Declarations (Declarations (N));
954 Inspect_Deferred_Constant_Completion (Declarations (N));
957 -- Verify that the SPARK_Mode of the body agrees with that of its spec
959 if Present (SPARK_Pragma (Body_Id)) then
960 if Present (SPARK_Aux_Pragma (Spec_Id)) then
961 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
964 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
966 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
967 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
968 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
970 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
973 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
974 -- as specified in source code, or because SPARK_Mode On is ignored
975 -- in an instance where the context is SPARK_Mode Off/Auto.
977 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
978 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
983 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
984 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
985 Error_Msg_Sloc := Sloc (Spec_Id);
987 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
991 -- Analyze_Declarations has caused freezing of all types. Now generate
992 -- bodies for RACW primitives and stream attributes, if any.
994 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
996 -- Attach subprogram bodies to support RACWs declared in spec
998 Append_RACW_Bodies (Declarations (N), Spec_Id);
999 Analyze_List (Declarations (N));
1002 HSS := Handled_Statement_Sequence (N);
1004 if Present (HSS) then
1005 Process_End_Label (HSS, 't', Spec_Id);
1008 -- Check that elaboration code in a preelaborable package body is
1009 -- empty other than null statements and labels (RM 10.2.1(6)).
1011 Validate_Null_Statement_Sequence (N);
1014 Validate_Categorization_Dependency (N, Spec_Id);
1015 Check_Completion (Body_Id);
1017 -- Generate start of body reference. Note that we do this fairly late,
1018 -- because the call will use In_Extended_Main_Source_Unit as a check,
1019 -- and we want to make sure that Corresponding_Stub links are set
1021 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1023 -- For a generic package, collect global references and mark them on
1024 -- the original body so that they are not resolved again at the point
1025 -- of instantiation.
1027 if Ekind (Spec_Id) /= E_Package then
1028 Save_Global_References (Original_Node (N));
1032 -- The entities of the package body have so far been chained onto the
1033 -- declaration chain for the spec. That's been fine while we were in the
1034 -- body, since we wanted them to be visible, but now that we are leaving
1035 -- the package body, they are no longer visible, so we remove them from
1036 -- the entity chain of the package spec entity, and copy them to the
1037 -- entity chain of the package body entity, where they will never again
1040 if Present (Last_Spec_Entity) then
1041 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1042 Set_Next_Entity (Last_Spec_Entity, Empty);
1043 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1044 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1047 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1048 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1049 Set_First_Entity (Spec_Id, Empty);
1050 Set_Last_Entity (Spec_Id, Empty);
1053 Update_Use_Clause_Chain;
1054 End_Package_Scope (Spec_Id);
1056 -- All entities declared in body are not visible
1062 E := First_Entity (Body_Id);
1063 while Present (E) loop
1064 Set_Is_Immediately_Visible (E, False);
1065 Set_Is_Potentially_Use_Visible (E, False);
1068 -- Child units may appear on the entity list (e.g. if they appear
1069 -- in the context of a subunit) but they are not body entities.
1071 if not Is_Child_Unit (E) then
1072 Set_Is_Package_Body_Entity (E);
1079 Check_References (Body_Id);
1081 -- For a generic unit, check that the formal parameters are referenced,
1082 -- and that local variables are used, as for regular packages.
1084 if Ekind (Spec_Id) = E_Generic_Package then
1085 Check_References (Spec_Id);
1088 -- At this point all entities of the package body are externally visible
1089 -- to the linker as their Is_Public flag is set to True. This proactive
1090 -- approach is necessary because an inlined or a generic body for which
1091 -- code is generated in other units may need to see these entities. Cut
1092 -- down the number of global symbols that do not need public visibility
1093 -- as this has two beneficial effects:
1094 -- (1) It makes the compilation process more efficient.
1095 -- (2) It gives the code generator more leeway to optimize within each
1096 -- unit, especially subprograms.
1098 -- This is done only for top-level library packages or child units as
1099 -- the algorithm does a top-down traversal of the package body. This is
1100 -- also done for instances because instantiations are still pending by
1101 -- the time the enclosing package body is analyzed.
1103 if (Scope (Spec_Id) = Standard_Standard
1104 or else Is_Child_Unit (Spec_Id)
1105 or else Is_Generic_Instance (Spec_Id))
1106 and then not Is_Generic_Unit (Spec_Id)
1108 Hide_Public_Entities (Declarations (N));
1111 -- If expander is not active, then here is where we turn off the
1112 -- In_Package_Body flag, otherwise it is turned off at the end of the
1113 -- corresponding expansion routine. If this is an instance body, we need
1114 -- to qualify names of local entities, because the body may have been
1115 -- compiled as a preliminary to another instantiation.
1117 if not Expander_Active then
1118 Set_In_Package_Body (Spec_Id, False);
1120 if Is_Generic_Instance (Spec_Id)
1121 and then Operating_Mode = Generate_Code
1123 Qualify_Entity_Names (N);
1127 if Present (Ignored_Ghost_Region) then
1128 Expander_Active := Saved_EA;
1131 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1132 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1133 end Analyze_Package_Body_Helper;
1135 ---------------------------------
1136 -- Analyze_Package_Declaration --
1137 ---------------------------------
1139 procedure Analyze_Package_Declaration (N : Node_Id) is
1140 Id : constant Node_Id := Defining_Entity (N);
1142 Is_Comp_Unit : constant Boolean :=
1143 Nkind (Parent (N)) = N_Compilation_Unit;
1145 Body_Required : Boolean;
1146 -- True when this package declaration requires a corresponding body
1149 if Debug_Flag_C then
1150 Write_Str ("==> package spec ");
1151 Write_Name (Chars (Id));
1152 Write_Str (" from ");
1153 Write_Location (Sloc (N));
1158 Generate_Definition (Id);
1160 Set_Ekind (Id, E_Package);
1161 Set_Etype (Id, Standard_Void_Type);
1163 -- Set SPARK_Mode from context
1165 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1166 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1167 Set_SPARK_Pragma_Inherited (Id);
1168 Set_SPARK_Aux_Pragma_Inherited (Id);
1170 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1171 -- the body of this package is instantiated or inlined later and out of
1172 -- context. The body uses this attribute to restore the value of the
1175 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1176 Set_Ignore_SPARK_Mode_Pragmas (Id);
1179 -- Analyze aspect specifications immediately, since we need to recognize
1180 -- things like Pure early enough to diagnose violations during analysis.
1182 if Has_Aspects (N) then
1183 Analyze_Aspect_Specifications (N, Id);
1186 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1187 -- a limited-with clause of its own context. In this case the error has
1188 -- been previously notified by Analyze_Context.
1190 -- limited with Pkg; -- ERROR
1191 -- package Pkg is ...
1193 if From_Limited_With (Id) then
1199 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1200 Set_Categorization_From_Pragmas (N);
1202 Analyze (Specification (N));
1203 Validate_Categorization_Dependency (N, Id);
1205 -- Determine whether the package requires a body. Abstract states are
1206 -- intentionally ignored because they do require refinement which can
1207 -- only come in a body, but at the same time they do not force the need
1208 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1210 Body_Required := Unit_Requires_Body (Id);
1212 if not Body_Required then
1214 -- If the package spec does not require an explicit body, then there
1215 -- are not entities requiring completion in the language sense. Call
1216 -- Check_Completion now to ensure that nested package declarations
1217 -- that require an implicit body get one. (In the case where a body
1218 -- is required, Check_Completion is called at the end of the body's
1219 -- declarative part.)
1223 -- If the package spec does not require an explicit body, then all
1224 -- abstract states declared in nested packages cannot possibly get
1225 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1226 -- only when the compilation unit is the main unit to allow for
1227 -- modular SPARK analysis where packages do not necessarily have
1230 if Is_Comp_Unit then
1231 Check_State_Refinements
1233 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1237 -- Set Body_Required indication on the compilation unit node
1239 if Is_Comp_Unit then
1240 Set_Body_Required (Parent (N), Body_Required);
1242 if Legacy_Elaboration_Checks and not Body_Required then
1243 Set_Suppress_Elaboration_Warnings (Id);
1247 End_Package_Scope (Id);
1249 -- For the declaration of a library unit that is a remote types package,
1250 -- check legality rules regarding availability of stream attributes for
1251 -- types that contain non-remote access values. This subprogram performs
1252 -- visibility tests that rely on the fact that we have exited the scope
1255 if Is_Comp_Unit then
1256 Validate_RT_RAT_Component (N);
1259 if Debug_Flag_C then
1261 Write_Str ("<== package spec ");
1262 Write_Name (Chars (Id));
1263 Write_Str (" from ");
1264 Write_Location (Sloc (N));
1267 end Analyze_Package_Declaration;
1269 -----------------------------------
1270 -- Analyze_Package_Specification --
1271 -----------------------------------
1273 -- Note that this code is shared for the analysis of generic package specs
1274 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1276 procedure Analyze_Package_Specification (N : Node_Id) is
1277 Id : constant Entity_Id := Defining_Entity (N);
1278 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1279 Vis_Decls : constant List_Id := Visible_Declarations (N);
1280 Priv_Decls : constant List_Id := Private_Declarations (N);
1283 Public_Child : Boolean;
1285 Private_With_Clauses_Installed : Boolean := False;
1286 -- In Ada 2005, private with_clauses are visible in the private part
1287 -- of a nested package, even if it appears in the public part of the
1288 -- enclosing package. This requires a separate step to install these
1289 -- private_with_clauses, and remove them at the end of the nested
1292 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1293 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1294 -- Is_True_Constant) on all variables that are entities of Id, and on
1295 -- the chain whose first element is FE. A recursive call is made for all
1296 -- packages and generic packages.
1298 procedure Generate_Parent_References;
1299 -- For a child unit, generate references to parent units, for
1300 -- GNAT Studio navigation purposes.
1302 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1303 -- Child and Unit are entities of compilation units. True if Child
1304 -- is a public child of Parent as defined in 10.1.1
1306 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1307 -- Reject completion of an incomplete or private type declarations
1308 -- having a known discriminant part by an unchecked union.
1310 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1311 -- Given the package entity of a generic package instantiation or
1312 -- formal package whose corresponding generic is a child unit, installs
1313 -- the private declarations of each of the child unit's parents.
1314 -- This has to be done at the point of entering the instance package's
1315 -- private part rather than being done in Sem_Ch12.Install_Parent
1316 -- (which is where the parents' visible declarations are installed).
1318 ---------------------
1319 -- Clear_Constants --
1320 ---------------------
1322 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1326 -- Ignore package renamings, not interesting and they can cause self
1327 -- referential loops in the code below.
1329 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1333 -- Note: in the loop below, the check for Next_Entity pointing back
1334 -- to the package entity may seem odd, but it is needed, because a
1335 -- package can contain a renaming declaration to itself, and such
1336 -- renamings are generated automatically within package instances.
1339 while Present (E) and then E /= Id loop
1340 if Is_Assignable (E) then
1341 Set_Never_Set_In_Source (E, False);
1342 Set_Is_True_Constant (E, False);
1343 Set_Current_Value (E, Empty);
1344 Set_Is_Known_Null (E, False);
1345 Set_Last_Assignment (E, Empty);
1347 if not Can_Never_Be_Null (E) then
1348 Set_Is_Known_Non_Null (E, False);
1351 elsif Is_Package_Or_Generic_Package (E) then
1352 Clear_Constants (E, First_Entity (E));
1353 Clear_Constants (E, First_Private_Entity (E));
1358 end Clear_Constants;
1360 --------------------------------
1361 -- Generate_Parent_References --
1362 --------------------------------
1364 procedure Generate_Parent_References is
1365 Decl : constant Node_Id := Parent (N);
1368 if Id = Cunit_Entity (Main_Unit)
1369 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1371 Generate_Reference (Id, Scope (Id), 'k', False);
1373 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1376 -- If current unit is an ancestor of main unit, generate a
1377 -- reference to its own parent.
1381 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1384 if Nkind (Main_Spec) = N_Package_Body then
1385 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1388 U := Parent_Spec (Main_Spec);
1389 while Present (U) loop
1390 if U = Parent (Decl) then
1391 Generate_Reference (Id, Scope (Id), 'k', False);
1394 elsif Nkind (Unit (U)) = N_Package_Body then
1398 U := Parent_Spec (Unit (U));
1403 end Generate_Parent_References;
1405 ---------------------
1406 -- Is_Public_Child --
1407 ---------------------
1409 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1411 if not Is_Private_Descendant (Child) then
1414 if Child = Unit then
1415 return not Private_Present (
1416 Parent (Unit_Declaration_Node (Child)));
1418 return Is_Public_Child (Scope (Child), Unit);
1421 end Is_Public_Child;
1423 ----------------------------------------
1424 -- Inspect_Unchecked_Union_Completion --
1425 ----------------------------------------
1427 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1431 Decl := First (Decls);
1432 while Present (Decl) loop
1434 -- We are looking at an incomplete or private type declaration
1435 -- with a known_discriminant_part whose full view is an
1436 -- Unchecked_Union. The seemingly useless check with Is_Type
1437 -- prevents cascaded errors when routines defined only for type
1438 -- entities are called with non-type entities.
1440 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1441 N_Private_Type_Declaration)
1442 and then Is_Type (Defining_Identifier (Decl))
1443 and then Has_Discriminants (Defining_Identifier (Decl))
1444 and then Present (Full_View (Defining_Identifier (Decl)))
1446 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1449 ("completion of discriminated partial view "
1450 & "cannot be an unchecked union",
1451 Full_View (Defining_Identifier (Decl)));
1456 end Inspect_Unchecked_Union_Completion;
1458 -----------------------------------------
1459 -- Install_Parent_Private_Declarations --
1460 -----------------------------------------
1462 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1463 Inst_Par : Entity_Id;
1464 Gen_Par : Entity_Id;
1465 Inst_Node : Node_Id;
1468 Inst_Par := Inst_Id;
1471 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1472 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1473 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1475 if Nkind_In (Inst_Node, N_Package_Instantiation,
1476 N_Formal_Package_Declaration)
1477 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1479 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1481 if Present (Renamed_Entity (Inst_Par)) then
1482 Inst_Par := Renamed_Entity (Inst_Par);
1485 -- The instance may appear in a sibling generic unit, in
1486 -- which case the prefix must include the common (generic)
1487 -- ancestor, which is treated as a current instance.
1490 and then Ekind (Inst_Par) = E_Generic_Package
1492 Gen_Par := Inst_Par;
1493 pragma Assert (In_Open_Scopes (Gen_Par));
1498 (Specification (Unit_Declaration_Node (Inst_Par)));
1501 -- Install the private declarations and private use clauses
1502 -- of a parent instance of the child instance, unless the
1503 -- parent instance private declarations have already been
1504 -- installed earlier in Analyze_Package_Specification, which
1505 -- happens when a generic child is instantiated, and the
1506 -- instance is a child of the parent instance.
1508 -- Installing the use clauses of the parent instance twice
1509 -- is both unnecessary and wrong, because it would cause the
1510 -- clauses to be chained to themselves in the use clauses
1511 -- list of the scope stack entry. That in turn would cause
1512 -- an endless loop from End_Use_Clauses upon scope exit.
1514 -- The parent is now fully visible. It may be a hidden open
1515 -- scope if we are currently compiling some child instance
1516 -- declared within it, but while the current instance is being
1517 -- compiled the parent is immediately visible. In particular
1518 -- its entities must remain visible if a stack save/restore
1519 -- takes place through a call to Rtsfind.
1521 if Present (Gen_Par) then
1522 if not In_Private_Part (Inst_Par) then
1523 Install_Private_Declarations (Inst_Par);
1524 Set_Use (Private_Declarations
1526 (Unit_Declaration_Node (Inst_Par))));
1527 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1530 -- If we've reached the end of the generic instance parents,
1531 -- then finish off by looping through the nongeneric parents
1532 -- and installing their private declarations.
1534 -- If one of the non-generic parents is itself on the scope
1535 -- stack, do not install its private declarations: they are
1536 -- installed in due time when the private part of that parent
1540 while Present (Inst_Par)
1541 and then Inst_Par /= Standard_Standard
1542 and then (not In_Open_Scopes (Inst_Par)
1543 or else not In_Private_Part (Inst_Par))
1545 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1547 not Is_Ancestor_Package
1548 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1550 Install_Private_Declarations (Inst_Par);
1552 (Private_Declarations
1554 (Unit_Declaration_Node (Inst_Par))));
1555 Inst_Par := Scope (Inst_Par);
1568 end Install_Parent_Private_Declarations;
1570 -- Start of processing for Analyze_Package_Specification
1573 if Present (Vis_Decls) then
1574 Analyze_Declarations (Vis_Decls);
1577 -- Inspect the entities defined in the package and ensure that all
1578 -- incomplete types have received full declarations. Build default
1579 -- initial condition and invariant procedures for all qualifying types.
1581 E := First_Entity (Id);
1582 while Present (E) loop
1584 -- Check on incomplete types
1586 -- AI05-0213: A formal incomplete type has no completion, and neither
1587 -- does the corresponding subtype in an instance.
1589 if Is_Incomplete_Type (E)
1590 and then No (Full_View (E))
1591 and then not Is_Generic_Type (E)
1592 and then not From_Limited_With (E)
1593 and then not Is_Generic_Actual_Type (E)
1595 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1601 if Is_Remote_Call_Interface (Id)
1602 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1604 Validate_RCI_Declarations (Id);
1607 -- Save global references in the visible declarations, before installing
1608 -- private declarations of parent unit if there is one, because the
1609 -- privacy status of types defined in the parent will change. This is
1610 -- only relevant for generic child units, but is done in all cases for
1613 if Ekind (Id) = E_Generic_Package
1614 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1617 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1618 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1621 -- Insert the freezing nodes after the visible declarations to
1622 -- ensure that we analyze its aspects; needed to ensure that
1623 -- global entities referenced in the aspects are properly handled.
1625 if Ada_Version >= Ada_2012
1626 and then Is_Non_Empty_List (Vis_Decls)
1627 and then Is_Empty_List (Priv_Decls)
1629 Insert_List_After_And_Analyze
1630 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1633 Set_Private_Declarations (Orig_Spec, Empty_List);
1634 Save_Global_References (Orig_Decl);
1635 Set_Private_Declarations (Orig_Spec, Save_Priv);
1639 -- If package is a public child unit, then make the private declarations
1640 -- of the parent visible.
1642 Public_Child := False;
1646 Pack_Decl : Node_Id;
1651 Par_Spec := Parent_Spec (Parent (N));
1653 -- If the package is formal package of an enclosing generic, it is
1654 -- transformed into a local generic declaration, and compiled to make
1655 -- its spec available. We need to retrieve the original generic to
1656 -- determine whether it is a child unit, and install its parents.
1660 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1662 Par := Entity (Name (Original_Node (Parent (N))));
1663 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1666 if Present (Par_Spec) then
1667 Generate_Parent_References;
1669 while Scope (Par) /= Standard_Standard
1670 and then Is_Public_Child (Id, Par)
1671 and then In_Open_Scopes (Par)
1673 Public_Child := True;
1675 Install_Private_Declarations (Par);
1676 Install_Private_With_Clauses (Par);
1677 Pack_Decl := Unit_Declaration_Node (Par);
1678 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1683 if Is_Compilation_Unit (Id) then
1684 Install_Private_With_Clauses (Id);
1686 -- The current compilation unit may include private with_clauses,
1687 -- which are visible in the private part of the current nested
1688 -- package, and have to be installed now. This is not done for
1689 -- nested instantiations, where the private with_clauses of the
1690 -- enclosing unit have no effect once the instantiation info is
1691 -- established and we start analyzing the package declaration.
1694 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1696 if Is_Package_Or_Generic_Package (Comp_Unit)
1697 and then not In_Private_Part (Comp_Unit)
1698 and then not In_Instance
1700 Install_Private_With_Clauses (Comp_Unit);
1701 Private_With_Clauses_Installed := True;
1706 -- If this is a package associated with a generic instance or formal
1707 -- package, then the private declarations of each of the generic's
1708 -- parents must be installed at this point.
1710 if Is_Generic_Instance (Id) then
1711 Install_Parent_Private_Declarations (Id);
1714 -- Analyze private part if present. The flag In_Private_Part is reset
1715 -- in End_Package_Scope.
1717 L := Last_Entity (Id);
1719 if Present (Priv_Decls) then
1720 Set_In_Private_Part (Id);
1722 -- Upon entering a public child's private part, it may be necessary
1723 -- to declare subprograms that were derived in the package's visible
1724 -- part but not yet made visible.
1726 if Public_Child then
1727 Declare_Inherited_Private_Subprograms (Id);
1730 Analyze_Declarations (Priv_Decls);
1732 -- Check the private declarations for incomplete deferred constants
1734 Inspect_Deferred_Constant_Completion (Priv_Decls);
1736 -- The first private entity is the immediate follower of the last
1737 -- visible entity, if there was one.
1740 Set_First_Private_Entity (Id, Next_Entity (L));
1742 Set_First_Private_Entity (Id, First_Entity (Id));
1745 -- There may be inherited private subprograms that need to be declared,
1746 -- even in the absence of an explicit private part. If there are any
1747 -- public declarations in the package and the package is a public child
1748 -- unit, then an implicit private part is assumed.
1750 elsif Present (L) and then Public_Child then
1751 Set_In_Private_Part (Id);
1752 Declare_Inherited_Private_Subprograms (Id);
1753 Set_First_Private_Entity (Id, Next_Entity (L));
1756 E := First_Entity (Id);
1757 while Present (E) loop
1759 -- Check rule of 3.6(11), which in general requires waiting till all
1760 -- full types have been seen.
1762 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1763 Check_Aliased_Component_Types (E);
1766 -- Check preelaborable initialization for full type completing a
1767 -- private type for which pragma Preelaborable_Initialization given.
1770 and then Must_Have_Preelab_Init (E)
1771 and then not Has_Preelaborable_Initialization (E)
1774 ("full view of & does not have preelaborable initialization", E);
1780 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1781 -- declaration having a known_discriminant_part shall not be an
1782 -- unchecked union type.
1784 if Present (Vis_Decls) then
1785 Inspect_Unchecked_Union_Completion (Vis_Decls);
1788 if Present (Priv_Decls) then
1789 Inspect_Unchecked_Union_Completion (Priv_Decls);
1792 if Ekind (Id) = E_Generic_Package
1793 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1794 and then Present (Priv_Decls)
1796 -- Save global references in private declarations, ignoring the
1797 -- visible declarations that were processed earlier.
1800 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1801 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1802 Save_Form : constant List_Id :=
1803 Generic_Formal_Declarations (Orig_Decl);
1806 -- Insert the freezing nodes after the private declarations to
1807 -- ensure that we analyze its aspects; needed to ensure that
1808 -- global entities referenced in the aspects are properly handled.
1810 if Ada_Version >= Ada_2012
1811 and then Is_Non_Empty_List (Priv_Decls)
1813 Insert_List_After_And_Analyze
1814 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1817 Set_Visible_Declarations (Orig_Spec, Empty_List);
1818 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1819 Save_Global_References (Orig_Decl);
1820 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1821 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1825 Process_End_Label (N, 'e', Id);
1827 -- Remove private_with_clauses of enclosing compilation unit, if they
1830 if Private_With_Clauses_Installed then
1831 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1834 -- For the case of a library level package, we must go through all the
1835 -- entities clearing the indications that the value may be constant and
1836 -- not modified. Why? Because any client of this package may modify
1837 -- these values freely from anywhere. This also applies to any nested
1838 -- packages or generic packages.
1840 -- For now we unconditionally clear constants for packages that are
1841 -- instances of generic packages. The reason is that we do not have the
1842 -- body yet, and we otherwise think things are unreferenced when they
1843 -- are not. This should be fixed sometime (the effect is not terrible,
1844 -- we just lose some warnings, and also some cases of value propagation)
1847 if Is_Library_Level_Entity (Id)
1848 or else Is_Generic_Instance (Id)
1850 Clear_Constants (Id, First_Entity (Id));
1851 Clear_Constants (Id, First_Private_Entity (Id));
1854 -- Output relevant information as to why the package requires a body.
1855 -- Do not consider generated packages as this exposes internal symbols
1856 -- and leads to confusing messages.
1858 if List_Body_Required_Info
1859 and then In_Extended_Main_Source_Unit (Id)
1860 and then Unit_Requires_Body (Id)
1861 and then Comes_From_Source (Id)
1863 Unit_Requires_Body_Info (Id);
1866 -- Nested package specs that do not require bodies are not checked for
1867 -- ineffective use clauses due to the possibility of subunits. This is
1868 -- because at this stage it is impossible to tell whether there will be
1871 if not Unit_Requires_Body (Id)
1872 and then Is_Compilation_Unit (Id)
1873 and then not Is_Private_Descendant (Id)
1875 Update_Use_Clause_Chain;
1877 end Analyze_Package_Specification;
1879 --------------------------------------
1880 -- Analyze_Private_Type_Declaration --
1881 --------------------------------------
1883 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1884 Id : constant Entity_Id := Defining_Identifier (N);
1885 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1888 Generate_Definition (Id);
1889 Set_Is_Pure (Id, PF);
1890 Init_Size_Align (Id);
1892 if not Is_Package_Or_Generic_Package (Current_Scope)
1893 or else In_Private_Part (Current_Scope)
1895 Error_Msg_N ("invalid context for private declaration", N);
1898 New_Private_Type (N, Id, N);
1899 Set_Depends_On_Private (Id);
1901 -- Set the SPARK mode from the current context
1903 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1904 Set_SPARK_Pragma_Inherited (Id);
1906 if Has_Aspects (N) then
1907 Analyze_Aspect_Specifications (N, Id);
1909 end Analyze_Private_Type_Declaration;
1911 ----------------------------------
1912 -- Check_Anonymous_Access_Types --
1913 ----------------------------------
1915 procedure Check_Anonymous_Access_Types
1916 (Spec_Id : Entity_Id;
1923 -- Itype references are only needed by gigi, to force elaboration of
1924 -- itypes. In the absence of code generation, they are not needed.
1926 if not Expander_Active then
1930 E := First_Entity (Spec_Id);
1931 while Present (E) loop
1932 if Ekind (E) = E_Anonymous_Access_Type
1933 and then From_Limited_With (E)
1935 IR := Make_Itype_Reference (Sloc (P_Body));
1938 if No (Declarations (P_Body)) then
1939 Set_Declarations (P_Body, New_List (IR));
1941 Prepend (IR, Declarations (P_Body));
1947 end Check_Anonymous_Access_Types;
1949 -------------------------------------------
1950 -- Declare_Inherited_Private_Subprograms --
1951 -------------------------------------------
1953 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1955 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1956 -- Check whether an inherited subprogram S is an operation of an
1957 -- untagged derived type T.
1959 ---------------------
1960 -- Is_Primitive_Of --
1961 ---------------------
1963 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1967 -- If the full view is a scalar type, the type is the anonymous base
1968 -- type, but the operation mentions the first subtype, so check the
1969 -- signature against the base type.
1971 if Base_Type (Etype (S)) = Base_Type (T) then
1975 Formal := First_Formal (S);
1976 while Present (Formal) loop
1977 if Base_Type (Etype (Formal)) = Base_Type (T) then
1981 Next_Formal (Formal);
1986 end Is_Primitive_Of;
1993 Op_Elmt_2 : Elmt_Id;
1994 Prim_Op : Entity_Id;
1995 New_Op : Entity_Id := Empty;
1996 Parent_Subp : Entity_Id;
1999 -- Start of processing for Declare_Inherited_Private_Subprograms
2002 E := First_Entity (Id);
2003 while Present (E) loop
2005 -- If the entity is a nonprivate type extension whose parent type
2006 -- is declared in an open scope, then the type may have inherited
2007 -- operations that now need to be made visible. Ditto if the entity
2008 -- is a formal derived type in a child unit.
2010 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2012 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2013 and then Is_Generic_Type (E)))
2014 and then In_Open_Scopes (Scope (Etype (E)))
2015 and then Is_Base_Type (E)
2017 if Is_Tagged_Type (E) then
2018 Op_List := Primitive_Operations (E);
2020 Tag := First_Tag_Component (E);
2022 Op_Elmt := First_Elmt (Op_List);
2023 while Present (Op_Elmt) loop
2024 Prim_Op := Node (Op_Elmt);
2026 -- Search primitives that are implicit operations with an
2027 -- internal name whose parent operation has a normal name.
2029 if Present (Alias (Prim_Op))
2030 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2031 and then not Comes_From_Source (Prim_Op)
2032 and then Is_Internal_Name (Chars (Prim_Op))
2033 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2035 Parent_Subp := Alias (Prim_Op);
2037 -- Case 1: Check if the type has also an explicit
2038 -- overriding for this primitive.
2040 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2041 while Present (Op_Elmt_2) loop
2043 -- Skip entities with attribute Interface_Alias since
2044 -- they are not overriding primitives (these entities
2045 -- link an interface primitive with their covering
2048 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2049 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2050 and then No (Interface_Alias (Node (Op_Elmt_2)))
2052 -- The private inherited operation has been
2053 -- overridden by an explicit subprogram:
2054 -- replace the former by the latter.
2056 New_Op := Node (Op_Elmt_2);
2057 Replace_Elmt (Op_Elmt, New_Op);
2058 Remove_Elmt (Op_List, Op_Elmt_2);
2059 Set_Overridden_Operation (New_Op, Parent_Subp);
2061 -- We don't need to inherit its dispatching slot.
2062 -- Set_All_DT_Position has previously ensured that
2063 -- the same slot was assigned to the two primitives
2066 and then Present (DTC_Entity (New_Op))
2067 and then Present (DTC_Entity (Prim_Op))
2070 (DT_Position (New_Op) = DT_Position (Prim_Op));
2074 goto Next_Primitive;
2077 Next_Elmt (Op_Elmt_2);
2080 -- Case 2: We have not found any explicit overriding and
2081 -- hence we need to declare the operation (i.e., make it
2084 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2086 -- Inherit the dispatching slot if E is already frozen
2089 and then Present (DTC_Entity (Alias (Prim_Op)))
2091 Set_DTC_Entity_Value (E, New_Op);
2092 Set_DT_Position_Value (New_Op,
2093 DT_Position (Alias (Prim_Op)));
2097 (Is_Dispatching_Operation (New_Op)
2098 and then Node (Last_Elmt (Op_List)) = New_Op);
2100 -- Substitute the new operation for the old one in the
2101 -- type's primitive operations list. Since the new
2102 -- operation was also just added to the end of list,
2103 -- the last element must be removed.
2105 -- (Question: is there a simpler way of declaring the
2106 -- operation, say by just replacing the name of the
2107 -- earlier operation, reentering it in the in the symbol
2108 -- table (how?), and marking it as private???)
2110 Replace_Elmt (Op_Elmt, New_Op);
2111 Remove_Last_Elmt (Op_List);
2115 Next_Elmt (Op_Elmt);
2118 -- Generate listing showing the contents of the dispatch table
2120 if Debug_Flag_ZZ then
2125 -- For untagged type, scan forward to locate inherited hidden
2128 Prim_Op := Next_Entity (E);
2129 while Present (Prim_Op) loop
2130 if Is_Subprogram (Prim_Op)
2131 and then Present (Alias (Prim_Op))
2132 and then not Comes_From_Source (Prim_Op)
2133 and then Is_Internal_Name (Chars (Prim_Op))
2134 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2135 and then Is_Primitive_Of (E, Prim_Op)
2137 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2140 Next_Entity (Prim_Op);
2142 -- Derived operations appear immediately after the type
2143 -- declaration (or the following subtype indication for
2144 -- a derived scalar type). Further declarations cannot
2145 -- include inherited operations of the type.
2147 if Present (Prim_Op) then
2148 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2156 end Declare_Inherited_Private_Subprograms;
2158 -----------------------
2159 -- End_Package_Scope --
2160 -----------------------
2162 procedure End_Package_Scope (P : Entity_Id) is
2164 Uninstall_Declarations (P);
2166 end End_Package_Scope;
2168 ---------------------------
2169 -- Exchange_Declarations --
2170 ---------------------------
2172 procedure Exchange_Declarations (Id : Entity_Id) is
2173 Full_Id : constant Entity_Id := Full_View (Id);
2174 H1 : constant Entity_Id := Homonym (Id);
2175 Next1 : constant Entity_Id := Next_Entity (Id);
2180 -- If missing full declaration for type, nothing to exchange
2182 if No (Full_Id) then
2186 -- Otherwise complete the exchange, and preserve semantic links
2188 Next2 := Next_Entity (Full_Id);
2189 H2 := Homonym (Full_Id);
2191 -- Reset full declaration pointer to reflect the switched entities and
2192 -- readjust the next entity chains.
2194 Exchange_Entities (Id, Full_Id);
2196 Link_Entities (Id, Next1);
2197 Set_Homonym (Id, H1);
2199 Set_Full_View (Full_Id, Id);
2200 Link_Entities (Full_Id, Next2);
2201 Set_Homonym (Full_Id, H2);
2202 end Exchange_Declarations;
2204 ----------------------------
2205 -- Install_Package_Entity --
2206 ----------------------------
2208 procedure Install_Package_Entity (Id : Entity_Id) is
2210 if not Is_Internal (Id) then
2211 if Debug_Flag_E then
2212 Write_Str ("Install: ");
2213 Write_Name (Chars (Id));
2217 if Is_Child_Unit (Id) then
2220 -- Do not enter implicitly inherited non-overridden subprograms of
2221 -- a tagged type back into visibility if they have non-conformant
2222 -- homographs (Ada RM 8.3 12.3/2).
2224 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2228 Set_Is_Immediately_Visible (Id);
2231 end Install_Package_Entity;
2233 ----------------------------------
2234 -- Install_Private_Declarations --
2235 ----------------------------------
2237 procedure Install_Private_Declarations (P : Entity_Id) is
2240 Priv_Deps : Elist_Id;
2242 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2243 -- When the full view of a private type is made available, we do the
2244 -- same for its private dependents under proper visibility conditions.
2245 -- When compiling a child unit this needs to be done recursively.
2247 -----------------------------
2248 -- Swap_Private_Dependents --
2249 -----------------------------
2251 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2255 Priv_Elmt : Elmt_Id;
2259 Priv_Elmt := First_Elmt (Priv_Deps);
2260 while Present (Priv_Elmt) loop
2261 Priv := Node (Priv_Elmt);
2263 -- Before the exchange, verify that the presence of the Full_View
2264 -- field. This field will be empty if the entity has already been
2265 -- installed due to a previous call.
2267 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2269 if Is_Private_Type (Priv) then
2270 Cunit := Cunit_Entity (Current_Sem_Unit);
2271 Deps := Private_Dependents (Priv);
2277 -- For each subtype that is swapped, we also swap the reference
2278 -- to it in Private_Dependents, to allow access to it when we
2279 -- swap them out in End_Package_Scope.
2281 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2283 -- Ensure that both views of the dependent private subtype are
2284 -- immediately visible if within some open scope. Check full
2285 -- view before exchanging views.
2287 if In_Open_Scopes (Scope (Full_View (Priv))) then
2288 Set_Is_Immediately_Visible (Priv);
2291 Exchange_Declarations (Priv);
2292 Set_Is_Immediately_Visible
2293 (Priv, In_Open_Scopes (Scope (Priv)));
2295 Set_Is_Potentially_Use_Visible
2296 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2298 -- Recurse for child units, except in generic child units,
2299 -- which unfortunately handle private_dependents separately.
2300 -- Note that the current unit may not have been analyzed,
2301 -- for example a package body, so we cannot rely solely on
2302 -- the Is_Child_Unit flag, but that's only an optimization.
2305 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2306 and then not Is_Empty_Elmt_List (Deps)
2307 and then not Inside_A_Generic
2309 Swap_Private_Dependents (Deps);
2313 Next_Elmt (Priv_Elmt);
2315 end Swap_Private_Dependents;
2317 -- Start of processing for Install_Private_Declarations
2320 -- First exchange declarations for private types, so that the full
2321 -- declaration is visible. For each private type, we check its
2322 -- Private_Dependents list and also exchange any subtypes of or derived
2323 -- types from it. Finally, if this is a Taft amendment type, the
2324 -- incomplete declaration is irrelevant, and we want to link the
2325 -- eventual full declaration with the original private one so we
2326 -- also skip the exchange.
2328 Id := First_Entity (P);
2329 while Present (Id) and then Id /= First_Private_Entity (P) loop
2330 if Is_Private_Base_Type (Id)
2331 and then Present (Full_View (Id))
2332 and then Comes_From_Source (Full_View (Id))
2333 and then Scope (Full_View (Id)) = Scope (Id)
2334 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2336 -- If there is a use-type clause on the private type, set the full
2337 -- view accordingly.
2339 Set_In_Use (Full_View (Id), In_Use (Id));
2340 Full := Full_View (Id);
2342 if Is_Private_Base_Type (Full)
2343 and then Has_Private_Declaration (Full)
2344 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2345 and then In_Open_Scopes (Scope (Etype (Full)))
2346 and then In_Package_Body (Current_Scope)
2347 and then not Is_Private_Type (Etype (Full))
2349 -- This is the completion of a private type by a derivation
2350 -- from another private type which is not private anymore. This
2351 -- can only happen in a package nested within a child package,
2352 -- when the parent type is defined in the parent unit. At this
2353 -- point the current type is not private either, and we have
2354 -- to install the underlying full view, which is now visible.
2355 -- Save the current full view as well, so that all views can be
2356 -- restored on exit. It may seem that after compiling the child
2357 -- body there are not environments to restore, but the back-end
2358 -- expects those links to be valid, and freeze nodes depend on
2361 if No (Full_View (Full))
2362 and then Present (Underlying_Full_View (Full))
2364 Set_Full_View (Id, Underlying_Full_View (Full));
2365 Set_Underlying_Full_View (Id, Full);
2366 Set_Is_Underlying_Full_View (Full);
2368 Set_Underlying_Full_View (Full, Empty);
2369 Set_Is_Frozen (Full_View (Id));
2373 Priv_Deps := Private_Dependents (Id);
2374 Exchange_Declarations (Id);
2375 Set_Is_Immediately_Visible (Id);
2376 Swap_Private_Dependents (Priv_Deps);
2382 -- Next make other declarations in the private part visible as well
2384 Id := First_Private_Entity (P);
2385 while Present (Id) loop
2386 Install_Package_Entity (Id);
2387 Set_Is_Hidden (Id, False);
2391 -- An abstract state is partially refined when it has at least one
2392 -- Part_Of constituent. Since these constituents are being installed
2393 -- into visibility, update the partial refinement status of any state
2394 -- defined in the associated package, subject to at least one Part_Of
2397 if Is_Package_Or_Generic_Package (P) then
2399 States : constant Elist_Id := Abstract_States (P);
2400 State_Elmt : Elmt_Id;
2401 State_Id : Entity_Id;
2404 if Present (States) then
2405 State_Elmt := First_Elmt (States);
2406 while Present (State_Elmt) loop
2407 State_Id := Node (State_Elmt);
2409 if Present (Part_Of_Constituents (State_Id)) then
2410 Set_Has_Partial_Visible_Refinement (State_Id);
2413 Next_Elmt (State_Elmt);
2419 -- Indicate that the private part is currently visible, so it can be
2420 -- properly reset on exit.
2422 Set_In_Private_Part (P);
2423 end Install_Private_Declarations;
2425 ----------------------------------
2426 -- Install_Visible_Declarations --
2427 ----------------------------------
2429 procedure Install_Visible_Declarations (P : Entity_Id) is
2431 Last_Entity : Entity_Id;
2435 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2437 if Is_Package_Or_Generic_Package (P) then
2438 Last_Entity := First_Private_Entity (P);
2440 Last_Entity := Empty;
2443 Id := First_Entity (P);
2444 while Present (Id) and then Id /= Last_Entity loop
2445 Install_Package_Entity (Id);
2448 end Install_Visible_Declarations;
2450 --------------------------
2451 -- Is_Private_Base_Type --
2452 --------------------------
2454 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2456 return Ekind (E) = E_Private_Type
2457 or else Ekind (E) = E_Limited_Private_Type
2458 or else Ekind (E) = E_Record_Type_With_Private;
2459 end Is_Private_Base_Type;
2461 --------------------------
2462 -- Is_Visible_Dependent --
2463 --------------------------
2465 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2467 S : constant Entity_Id := Scope (Dep);
2470 -- Renamings created for actual types have the visibility of the actual
2472 if Ekind (S) = E_Package
2473 and then Is_Generic_Instance (S)
2474 and then (Is_Generic_Actual_Type (Dep)
2475 or else Is_Generic_Actual_Type (Full_View (Dep)))
2479 elsif not (Is_Derived_Type (Dep))
2480 and then Is_Derived_Type (Full_View (Dep))
2482 -- When instantiating a package body, the scope stack is empty, so
2483 -- check instead whether the dependent type is defined in the same
2484 -- scope as the instance itself.
2486 return In_Open_Scopes (S)
2487 or else (Is_Generic_Instance (Current_Scope)
2488 and then Scope (Dep) = Scope (Current_Scope));
2492 end Is_Visible_Dependent;
2494 ----------------------------
2495 -- May_Need_Implicit_Body --
2496 ----------------------------
2498 procedure May_Need_Implicit_Body (E : Entity_Id) is
2499 P : constant Node_Id := Unit_Declaration_Node (E);
2500 S : constant Node_Id := Parent (P);
2505 if not Has_Completion (E)
2506 and then Nkind (P) = N_Package_Declaration
2507 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2510 Make_Package_Body (Sloc (E),
2511 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2512 Chars => Chars (E)),
2513 Declarations => New_List);
2515 if Nkind (S) = N_Package_Specification then
2516 if Present (Private_Declarations (S)) then
2517 Decls := Private_Declarations (S);
2519 Decls := Visible_Declarations (S);
2522 Decls := Declarations (S);
2528 end May_Need_Implicit_Body;
2530 ----------------------
2531 -- New_Private_Type --
2532 ----------------------
2534 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2536 -- For other than Ada 2012, enter the name in the current scope
2538 if Ada_Version < Ada_2012 then
2541 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2542 -- there may be an incomplete previous view.
2548 Prev := Find_Type_Name (N);
2549 pragma Assert (Prev = Id
2550 or else (Ekind (Prev) = E_Incomplete_Type
2551 and then Present (Full_View (Prev))
2552 and then Full_View (Prev) = Id));
2556 if Limited_Present (Def) then
2557 Set_Ekind (Id, E_Limited_Private_Type);
2559 Set_Ekind (Id, E_Private_Type);
2563 Set_Has_Delayed_Freeze (Id);
2564 Set_Is_First_Subtype (Id);
2565 Init_Size_Align (Id);
2567 Set_Is_Constrained (Id,
2568 No (Discriminant_Specifications (N))
2569 and then not Unknown_Discriminants_Present (N));
2571 -- Set tagged flag before processing discriminants, to catch illegal
2574 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2576 Set_Discriminant_Constraint (Id, No_Elist);
2577 Set_Stored_Constraint (Id, No_Elist);
2579 if Present (Discriminant_Specifications (N)) then
2581 Process_Discriminants (N);
2584 elsif Unknown_Discriminants_Present (N) then
2585 Set_Has_Unknown_Discriminants (Id);
2588 Set_Private_Dependents (Id, New_Elmt_List);
2590 if Tagged_Present (Def) then
2591 Set_Ekind (Id, E_Record_Type_With_Private);
2592 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2593 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2594 Set_Is_Limited_Record (Id, Limited_Present (Def));
2595 Set_Has_Delayed_Freeze (Id, True);
2597 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2599 if Is_RTE (Id, RE_Timing_Event) then
2600 Set_Has_Timing_Event (Id);
2603 -- Create a class-wide type with the same attributes
2605 Make_Class_Wide_Type (Id);
2607 elsif Abstract_Present (Def) then
2608 Error_Msg_N ("only a tagged type can be abstract", N);
2610 end New_Private_Type;
2612 ---------------------------------
2613 -- Requires_Completion_In_Body --
2614 ---------------------------------
2616 function Requires_Completion_In_Body
2618 Pack_Id : Entity_Id;
2619 Do_Abstract_States : Boolean := False) return Boolean
2622 -- Always ignore child units. Child units get added to the entity list
2623 -- of a parent unit, but are not original entities of the parent, and
2624 -- so do not affect whether the parent needs a body.
2626 if Is_Child_Unit (Id) then
2629 -- Ignore formal packages and their renamings
2631 elsif Ekind (Id) = E_Package
2632 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2633 N_Formal_Package_Declaration
2637 -- Otherwise test to see if entity requires a completion. Note that
2638 -- subprogram entities whose declaration does not come from source are
2639 -- ignored here on the basis that we assume the expander will provide an
2640 -- implicit completion at some point.
2642 elsif (Is_Overloadable (Id)
2643 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2644 and then not Is_Abstract_Subprogram (Id)
2645 and then not Has_Completion (Id)
2646 and then Comes_From_Source (Parent (Id)))
2649 (Ekind (Id) = E_Package
2650 and then Id /= Pack_Id
2651 and then not Has_Completion (Id)
2652 and then Unit_Requires_Body (Id, Do_Abstract_States))
2655 (Ekind (Id) = E_Incomplete_Type
2656 and then No (Full_View (Id))
2657 and then not Is_Generic_Type (Id))
2660 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2661 and then not Has_Completion (Id))
2664 (Ekind (Id) = E_Generic_Package
2665 and then Id /= Pack_Id
2666 and then not Has_Completion (Id)
2667 and then Unit_Requires_Body (Id, Do_Abstract_States))
2670 (Is_Generic_Subprogram (Id)
2671 and then not Has_Completion (Id))
2675 -- Otherwise the entity does not require completion in a package body
2680 end Requires_Completion_In_Body;
2682 ----------------------------
2683 -- Uninstall_Declarations --
2684 ----------------------------
2686 procedure Uninstall_Declarations (P : Entity_Id) is
2687 Decl : constant Node_Id := Unit_Declaration_Node (P);
2691 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2692 -- Copy to the private declaration the attributes of the full view that
2693 -- need to be available for the partial view also.
2695 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2696 -- When the full view of a private type is made unavailable, we do the
2697 -- same for its private dependents under proper visibility conditions.
2698 -- When compiling a child unit this needs to be done recursively.
2700 function Type_In_Use (T : Entity_Id) return Boolean;
2701 -- Check whether type or base type appear in an active use_type clause
2703 ------------------------------
2704 -- Preserve_Full_Attributes --
2705 ------------------------------
2707 procedure Preserve_Full_Attributes
2711 Full_Base : constant Entity_Id := Base_Type (Full);
2712 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2715 Set_Size_Info (Priv, Full);
2716 Set_RM_Size (Priv, RM_Size (Full));
2717 Set_Size_Known_At_Compile_Time
2718 (Priv, Size_Known_At_Compile_Time (Full));
2719 Set_Is_Volatile (Priv, Is_Volatile (Full));
2720 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2721 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2722 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2723 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2724 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2725 Set_Has_Pragma_Unreferenced_Objects
2726 (Priv, Has_Pragma_Unreferenced_Objects
2728 if Is_Unchecked_Union (Full) then
2729 Set_Is_Unchecked_Union (Base_Type (Priv));
2731 -- Why is atomic not copied here ???
2733 if Referenced (Full) then
2734 Set_Referenced (Priv);
2737 if Priv_Is_Base_Type then
2738 Set_Is_Controlled_Active
2739 (Priv, Is_Controlled_Active (Full_Base));
2740 Set_Finalize_Storage_Only
2741 (Priv, Finalize_Storage_Only (Full_Base));
2742 Set_Has_Controlled_Component
2743 (Priv, Has_Controlled_Component (Full_Base));
2745 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2748 -- As explained in Freeze_Entity, private types are required to point
2749 -- to the same freeze node as their corresponding full view, if any.
2750 -- But we ought not to overwrite a node already inserted in the tree.
2753 (Serious_Errors_Detected /= 0
2754 or else No (Freeze_Node (Priv))
2755 or else No (Parent (Freeze_Node (Priv)))
2756 or else Freeze_Node (Priv) = Freeze_Node (Full));
2758 Set_Freeze_Node (Priv, Freeze_Node (Full));
2760 -- Propagate Default_Initial_Condition-related attributes from the
2761 -- full view to the private view.
2763 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2765 -- Propagate invariant-related attributes from the full view to the
2768 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2770 -- Propagate predicate-related attributes from the full view to the
2773 Propagate_Predicate_Attributes (Priv, From_Typ => Full);
2775 if Is_Tagged_Type (Priv)
2776 and then Is_Tagged_Type (Full)
2777 and then not Error_Posted (Full)
2779 if Is_Tagged_Type (Priv) then
2781 -- If the type is tagged, the tag itself must be available on
2782 -- the partial view, for expansion purposes.
2784 Set_First_Entity (Priv, First_Entity (Full));
2786 -- If there are discriminants in the partial view, these remain
2787 -- visible. Otherwise only the tag itself is visible, and there
2788 -- are no nameable components in the partial view.
2790 if No (Last_Entity (Priv)) then
2791 Set_Last_Entity (Priv, First_Entity (Priv));
2795 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2797 if Has_Discriminants (Full) then
2798 Set_Discriminant_Constraint (Priv,
2799 Discriminant_Constraint (Full));
2802 end Preserve_Full_Attributes;
2804 -----------------------------
2805 -- Swap_Private_Dependents --
2806 -----------------------------
2808 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2812 Priv_Elmt : Elmt_Id;
2816 Priv_Elmt := First_Elmt (Priv_Deps);
2817 while Present (Priv_Elmt) loop
2818 Priv := Node (Priv_Elmt);
2820 -- Before we do the swap, we verify the presence of the Full_View
2821 -- field, which may be empty due to a swap by a previous call to
2822 -- End_Package_Scope (e.g. from the freezing mechanism).
2824 if Present (Full_View (Priv)) then
2825 if Is_Private_Type (Priv) then
2826 Cunit := Cunit_Entity (Current_Sem_Unit);
2827 Deps := Private_Dependents (Priv);
2834 or else not In_Open_Scopes (Scope (Priv))
2836 Set_Is_Immediately_Visible (Priv, False);
2839 if Is_Visible_Dependent (Priv) then
2840 Preserve_Full_Attributes (Priv, Full_View (Priv));
2841 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2842 Exchange_Declarations (Priv);
2844 -- Recurse for child units, except in generic child units,
2845 -- which unfortunately handle private_dependents separately.
2846 -- Note that the current unit may not have been analyzed,
2847 -- for example a package body, so we cannot rely solely on
2848 -- the Is_Child_Unit flag, but that's only an optimization.
2851 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2852 and then not Is_Empty_Elmt_List (Deps)
2853 and then not Inside_A_Generic
2855 Swap_Private_Dependents (Deps);
2860 Next_Elmt (Priv_Elmt);
2862 end Swap_Private_Dependents;
2868 function Type_In_Use (T : Entity_Id) return Boolean is
2870 return Scope (Base_Type (T)) = P
2871 and then (In_Use (T) or else In_Use (Base_Type (T)));
2874 -- Start of processing for Uninstall_Declarations
2877 Id := First_Entity (P);
2878 while Present (Id) and then Id /= First_Private_Entity (P) loop
2879 if Debug_Flag_E then
2880 Write_Str ("unlinking visible entity ");
2881 Write_Int (Int (Id));
2885 -- On exit from the package scope, we must preserve the visibility
2886 -- established by use clauses in the current scope. Two cases:
2888 -- a) If the entity is an operator, it may be a primitive operator of
2889 -- a type for which there is a visible use-type clause.
2891 -- b) For other entities, their use-visibility is determined by a
2892 -- visible use clause for the package itself or a use-all-type clause
2893 -- applied directly to the entity's type. For a generic instance,
2894 -- the instantiation of the formals appears in the visible part,
2895 -- but the formals are private and remain so.
2897 if Ekind (Id) = E_Function
2898 and then Is_Operator_Symbol_Name (Chars (Id))
2899 and then not Is_Hidden (Id)
2900 and then not Error_Posted (Id)
2902 Set_Is_Potentially_Use_Visible (Id,
2904 or else Type_In_Use (Etype (Id))
2905 or else Type_In_Use (Etype (First_Formal (Id)))
2906 or else (Present (Next_Formal (First_Formal (Id)))
2909 (Etype (Next_Formal (First_Formal (Id))))));
2911 if In_Use (P) and then not Is_Hidden (Id) then
2913 -- A child unit of a use-visible package remains use-visible
2914 -- only if it is itself a visible child unit. Otherwise it
2915 -- would remain visible in other contexts where P is use-
2916 -- visible, because once compiled it stays in the entity list
2917 -- of its parent unit.
2919 if Is_Child_Unit (Id) then
2920 Set_Is_Potentially_Use_Visible
2921 (Id, Is_Visible_Lib_Unit (Id));
2923 Set_Is_Potentially_Use_Visible (Id);
2926 -- We need to avoid incorrectly marking enumeration literals as
2927 -- non-visible when a visible use-all-type clause is in effect.
2929 elsif Type_In_Use (Etype (Id))
2930 and then Nkind (Current_Use_Clause (Etype (Id))) =
2932 and then All_Present (Current_Use_Clause (Etype (Id)))
2937 Set_Is_Potentially_Use_Visible (Id, False);
2941 -- Local entities are not immediately visible outside of the package
2943 Set_Is_Immediately_Visible (Id, False);
2945 -- If this is a private type with a full view (for example a local
2946 -- subtype of a private type declared elsewhere), ensure that the
2947 -- full view is also removed from visibility: it may be exposed when
2948 -- swapping views in an instantiation. Similarly, ensure that the
2949 -- use-visibility is properly set on both views.
2951 if Is_Type (Id) and then Present (Full_View (Id)) then
2952 Set_Is_Immediately_Visible (Full_View (Id), False);
2953 Set_Is_Potentially_Use_Visible (Full_View (Id),
2954 Is_Potentially_Use_Visible (Id));
2957 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2958 Check_Abstract_Overriding (Id);
2959 Check_Conventions (Id);
2962 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2963 and then No (Full_View (Id))
2964 and then not Is_Generic_Type (Id)
2965 and then not Is_Derived_Type (Id)
2967 Error_Msg_N ("missing full declaration for private type&", Id);
2969 elsif Ekind (Id) = E_Record_Type_With_Private
2970 and then not Is_Generic_Type (Id)
2971 and then No (Full_View (Id))
2973 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2974 Error_Msg_N ("missing full declaration for private type&", Id);
2977 ("missing full declaration for private extension", Id);
2980 -- Case of constant, check for deferred constant declaration with
2981 -- no full view. Likely just a matter of a missing expression, or
2982 -- accidental use of the keyword constant.
2984 elsif Ekind (Id) = E_Constant
2986 -- OK if constant value present
2988 and then No (Constant_Value (Id))
2990 -- OK if full view present
2992 and then No (Full_View (Id))
2994 -- OK if imported, since that provides the completion
2996 and then not Is_Imported (Id)
2998 -- OK if object declaration replaced by renaming declaration as
2999 -- a result of OK_To_Rename processing (e.g. for concatenation)
3001 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
3003 -- OK if object declaration with the No_Initialization flag set
3005 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3006 and then No_Initialization (Parent (Id)))
3008 -- If no private declaration is present, we assume the user did
3009 -- not intend a deferred constant declaration and the problem
3010 -- is simply that the initializing expression is missing.
3012 if not Has_Private_Declaration (Etype (Id)) then
3014 -- We assume that the user did not intend a deferred constant
3015 -- declaration, and the expression is just missing.
3018 ("constant declaration requires initialization expression",
3021 if Is_Limited_Type (Etype (Id)) then
3023 ("\if variable intended, remove CONSTANT from declaration",
3027 -- Otherwise if a private declaration is present, then we are
3028 -- missing the full declaration for the deferred constant.
3032 ("missing full declaration for deferred constant (RM 7.4)",
3035 if Is_Limited_Type (Etype (Id)) then
3037 ("\if variable intended, remove CONSTANT from declaration",
3046 -- If the specification was installed as the parent of a public child
3047 -- unit, the private declarations were not installed, and there is
3050 if not In_Private_Part (P) then
3053 Set_In_Private_Part (P, False);
3056 -- Make private entities invisible and exchange full and private
3057 -- declarations for private types. Id is now the first private entity
3060 while Present (Id) loop
3061 if Debug_Flag_E then
3062 Write_Str ("unlinking private entity ");
3063 Write_Int (Int (Id));
3067 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3068 Check_Abstract_Overriding (Id);
3069 Check_Conventions (Id);
3072 Set_Is_Immediately_Visible (Id, False);
3074 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3075 Full := Full_View (Id);
3077 -- If the partial view is not declared in the visible part of the
3078 -- package (as is the case when it is a type derived from some
3079 -- other private type in the private part of the current package),
3080 -- no exchange takes place.
3083 or else List_Containing (Parent (Id)) /=
3084 Visible_Declarations (Specification (Decl))
3089 -- The entry in the private part points to the full declaration,
3090 -- which is currently visible. Exchange them so only the private
3091 -- type declaration remains accessible, and link private and full
3092 -- declaration in the opposite direction. Before the actual
3093 -- exchange, we copy back attributes of the full view that must
3094 -- be available to the partial view too.
3096 Preserve_Full_Attributes (Id, Full);
3098 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3100 -- The following test may be redundant, as this is already
3101 -- diagnosed in sem_ch3. ???
3103 if not Is_Definite_Subtype (Full)
3104 and then Is_Definite_Subtype (Id)
3106 Error_Msg_Sloc := Sloc (Parent (Id));
3108 ("full view of& not compatible with declaration#", Full, Id);
3111 -- Swap out the subtypes and derived types of Id that
3112 -- were compiled in this scope, or installed previously
3113 -- by Install_Private_Declarations.
3115 Swap_Private_Dependents (Private_Dependents (Id));
3117 -- Now restore the type itself to its private view
3119 Exchange_Declarations (Id);
3121 -- If we have installed an underlying full view for a type derived
3122 -- from a private type in a child unit, restore the proper views
3123 -- of private and full view. See corresponding code in
3124 -- Install_Private_Declarations.
3126 -- After the exchange, Full denotes the private type in the
3127 -- visible part of the package.
3129 if Is_Private_Base_Type (Full)
3130 and then Present (Full_View (Full))
3131 and then Present (Underlying_Full_View (Full))
3132 and then In_Package_Body (Current_Scope)
3134 Set_Full_View (Full, Underlying_Full_View (Full));
3135 Set_Underlying_Full_View (Full, Empty);
3138 elsif Ekind (Id) = E_Incomplete_Type
3139 and then Comes_From_Source (Id)
3140 and then No (Full_View (Id))
3142 -- Mark Taft amendment types. Verify that there are no primitive
3143 -- operations declared for the type (3.10.1(9)).
3145 Set_Has_Completion_In_Body (Id);
3152 Elmt := First_Elmt (Private_Dependents (Id));
3153 while Present (Elmt) loop
3154 Subp := Node (Elmt);
3156 -- Is_Primitive is tested because there can be cases where
3157 -- nonprimitive subprograms (in nested packages) are added
3158 -- to the Private_Dependents list.
3160 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3162 ("type& must be completed in the private part",
3165 -- The result type of an access-to-function type cannot be a
3166 -- Taft-amendment type, unless the version is Ada 2012 or
3167 -- later (see AI05-151).
3169 elsif Ada_Version < Ada_2012
3170 and then Ekind (Subp) = E_Subprogram_Type
3172 if Etype (Subp) = Id
3174 (Is_Class_Wide_Type (Etype (Subp))
3175 and then Etype (Etype (Subp)) = Id)
3178 ("type& must be completed in the private part",
3179 Associated_Node_For_Itype (Subp), Id);
3187 elsif not Is_Child_Unit (Id)
3188 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3191 Set_Is_Potentially_Use_Visible (Id, False);
3197 end Uninstall_Declarations;
3199 ------------------------
3200 -- Unit_Requires_Body --
3201 ------------------------
3203 function Unit_Requires_Body
3204 (Pack_Id : Entity_Id;
3205 Do_Abstract_States : Boolean := False) return Boolean
3209 Requires_Body : Boolean := False;
3210 -- Flag set when the unit has at least one construct that requires
3211 -- completion in a body.
3214 -- Imported entity never requires body. Right now, only subprograms can
3215 -- be imported, but perhaps in the future we will allow import of
3218 if Is_Imported (Pack_Id) then
3221 -- Body required if library package with pragma Elaborate_Body
3223 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3226 -- Body required if subprogram
3228 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3231 -- Treat a block as requiring a body
3233 elsif Ekind (Pack_Id) = E_Block then
3236 elsif Ekind (Pack_Id) = E_Package
3237 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3238 and then Present (Generic_Parent (Parent (Pack_Id)))
3241 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3243 if Has_Pragma_Elaborate_Body (G_P) then
3249 -- Traverse the entity chain of the package and look for constructs that
3250 -- require a completion in a body.
3252 E := First_Entity (Pack_Id);
3253 while Present (E) loop
3255 -- Skip abstract states because their completion depends on several
3256 -- criteria (see below).
3258 if Ekind (E) = E_Abstract_State then
3261 elsif Requires_Completion_In_Body
3262 (E, Pack_Id, Do_Abstract_States)
3264 Requires_Body := True;
3271 -- A [generic] package that defines at least one non-null abstract state
3272 -- requires a completion only when at least one other construct requires
3273 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3274 -- performed if the caller requests this behavior.
3276 if Do_Abstract_States
3277 and then Is_Package_Or_Generic_Package (Pack_Id)
3278 and then Has_Non_Null_Abstract_State (Pack_Id)
3279 and then Requires_Body
3284 return Requires_Body;
3285 end Unit_Requires_Body;
3287 -----------------------------
3288 -- Unit_Requires_Body_Info --
3289 -----------------------------
3291 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3295 -- An imported entity never requires body. Right now, only subprograms
3296 -- can be imported, but perhaps in the future we will allow import of
3299 if Is_Imported (Pack_Id) then
3302 -- Body required if library package with pragma Elaborate_Body
3304 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3305 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3307 -- Body required if subprogram
3309 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3310 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3312 -- Body required if generic parent has Elaborate_Body
3314 elsif Ekind (Pack_Id) = E_Package
3315 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3316 and then Present (Generic_Parent (Parent (Pack_Id)))
3319 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3321 if Has_Pragma_Elaborate_Body (G_P) then
3323 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3328 -- A [generic] package that introduces at least one non-null abstract
3329 -- state requires completion. However, there is a separate rule that
3330 -- requires that such a package have a reason other than this for a
3331 -- body being required (if necessary a pragma Elaborate_Body must be
3332 -- provided). If Ignore_Abstract_State is True, we don't do this check
3333 -- (so we can use Unit_Requires_Body to check for some other reason).
3335 elsif Is_Package_Or_Generic_Package (Pack_Id)
3336 and then Present (Abstract_States (Pack_Id))
3337 and then not Is_Null_State
3338 (Node (First_Elmt (Abstract_States (Pack_Id))))
3341 ("info: & requires body (non-null abstract state aspect)?Y?",
3345 -- Otherwise search entity chain for entity requiring completion
3347 E := First_Entity (Pack_Id);
3348 while Present (E) loop
3349 if Requires_Completion_In_Body (E, Pack_Id) then
3350 Error_Msg_Node_2 := E;
3352 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3357 end Unit_Requires_Body_Info;