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
465 -- and export them. Likewise for subprograms but we work harder
466 -- for them to see whether they are referenced on an individual
467 -- basis by looking into the table of referenced subprograms.
468 -- But we cannot say anything for entities declared in nested
469 -- instances because instantiations are not done yet so the
470 -- bodies are not visible and could contain references to them.
471 elsif Nkind_In (Decl, N_Exception_Declaration,
472 N_Object_Declaration,
473 N_Object_Renaming_Declaration,
474 N_Subprogram_Declaration,
475 N_Subprogram_Renaming_Declaration)
477 Decl_Id := Defining_Entity (Decl);
479 if not In_Nested_Instance
480 and then not Is_Imported (Decl_Id)
481 and then not Is_Exported (Decl_Id)
482 and then No (Interface_Name (Decl_Id))
484 ((Nkind (Decl) /= N_Subprogram_Declaration
485 and then not Has_Referencer_Of_Non_Subprograms)
486 or else (Nkind (Decl) = N_Subprogram_Declaration
487 and then not Subprogram_Table.Get (Decl_Id)))
489 Set_Is_Public (Decl_Id, False);
492 -- For a subprogram renaming, if the entity is referenced,
493 -- then so is the renamed subprogram. But there is an issue
494 -- with generic bodies because instantiations are not done
495 -- yet and, therefore, cannot be scanned for referencers.
496 -- That's why we use an approximation and test that we have
497 -- at least one subprogram referenced by an inlined body
498 -- instead of precisely the entity of this renaming.
500 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
501 and then Subprogram_Table.Get_First
502 and then Is_Entity_Name (Name (Decl))
503 and then Present (Entity (Name (Decl)))
504 and then Is_Subprogram (Entity (Name (Decl)))
506 Subprogram_Table.Set (Entity (Name (Decl)), True);
513 return Has_Referencer_Of_Non_Subprograms;
516 -------------------------
517 -- Scan_Subprogram_Ref --
518 -------------------------
520 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
522 -- Detect a reference of the form
525 if Nkind (N) in N_Subprogram_Call
526 and then Is_Entity_Name (Name (N))
527 and then Present (Entity (Name (N)))
528 and then Is_Subprogram (Entity (Name (N)))
530 Subprogram_Table.Set (Entity (Name (N)), True);
532 -- Detect a reference of the form
533 -- Subp'Some_Attribute
535 elsif Nkind (N) = N_Attribute_Reference
536 and then Is_Entity_Name (Prefix (N))
537 and then Present (Entity (Prefix (N)))
538 and then Is_Subprogram (Entity (Prefix (N)))
540 Subprogram_Table.Set (Entity (Prefix (N)), True);
542 -- Constants can be substituted by their value in gigi, which may
543 -- contain a reference, so scan the value recursively.
545 elsif Is_Entity_Name (N)
546 and then Present (Entity (N))
547 and then Ekind (Entity (N)) = E_Constant
550 Val : constant Node_Id := Constant_Value (Entity (N));
553 and then not Compile_Time_Known_Value (Val)
555 Scan_Subprogram_Refs (Val);
561 end Scan_Subprogram_Ref;
563 --------------------------
564 -- Scan_Subprogram_Refs --
565 --------------------------
567 procedure Scan_Subprogram_Refs (Node : Node_Id) is
569 if not Traversed_Table.Get (Node) then
570 Traversed_Table.Set (Node, True);
571 Traverse_And_Scan_Subprogram_Refs (Node);
573 end Scan_Subprogram_Refs;
578 pragma Unreferenced (Discard);
580 -- Start of processing for Hide_Public_Entities
583 -- The algorithm examines the top level declarations of a package
584 -- body in reverse looking for a construct that may export entities
585 -- declared prior to it. If such a scenario is encountered, then all
586 -- entities in the range Last (Decls) .. construct are hidden from
587 -- external visibility. Consider:
595 -- package body Pack is
596 -- External_Obj : ...; -- (1)
598 -- package body Gen is -- (2)
599 -- ... External_Obj ... -- (3)
602 -- Local_Obj : ...; -- (4)
605 -- In this example Local_Obj (4) must not be externally visible as
606 -- it cannot be exported by anything in Pack. The body of generic
607 -- package Gen (2) on the other hand acts as a "referencer" and may
608 -- export anything declared before it. Since the compiler does not
609 -- perform flow analysis, it is not possible to determine precisely
610 -- which entities will be exported when Gen is instantiated. In the
611 -- example above External_Obj (1) is exported at (3), but this may
612 -- not always be the case. The algorithm takes a conservative stance
613 -- and leaves entity External_Obj public.
615 -- This very conservative algorithm is supplemented by a more precise
616 -- processing for inlined bodies. For them, we traverse the syntactic
617 -- tree and record which subprograms are actually referenced from it.
618 -- This makes it possible to compute a much smaller set of externally
619 -- visible subprograms in the absence of generic bodies, which can
620 -- have a significant impact on the inlining decisions made in the
621 -- back end and the removal of out-of-line bodies from the object
622 -- code. We do it only for inlined bodies because they are supposed
623 -- to be reasonably small and tree traversal is very expensive.
625 -- Note that even this special processing is not optimal for inlined
626 -- bodies, because we treat all inlined subprograms alike. An optimal
627 -- algorithm would require computing the transitive closure of the
628 -- inlined subprograms that can really be referenced from other units
629 -- in the source code.
631 -- We could extend this processing for inlined bodies and record all
632 -- entities, not just subprograms, referenced from them, which would
633 -- make it possible to compute a much smaller set of all externally
634 -- visible entities in the absence of generic bodies. But this would
635 -- mean implementing a more thorough tree traversal of the bodies,
636 -- i.e. not just syntactic, and the gain would very likely be worth
637 -- neither the hassle nor the slowdown of the compiler.
639 -- Finally, an important thing to be aware of is that, at this point,
640 -- instantiations are not done yet so we cannot directly see inlined
641 -- bodies coming from them. That's not catastrophic because only the
642 -- actual parameters of the instantiations matter here, and they are
643 -- present in the declarations list of the instantiated packages.
645 Traversed_Table.Reset;
646 Subprogram_Table.Reset;
647 Discard := Has_Referencer (Decls, False, False);
648 end Hide_Public_Entities;
650 ----------------------------------
651 -- Install_Composite_Operations --
652 ----------------------------------
654 procedure Install_Composite_Operations (P : Entity_Id) is
658 Id := First_Entity (P);
659 while Present (Id) loop
661 and then (Is_Limited_Composite (Id)
662 or else Is_Private_Composite (Id))
663 and then No (Private_Component (Id))
665 Set_Is_Limited_Composite (Id, False);
666 Set_Is_Private_Composite (Id, False);
671 end Install_Composite_Operations;
675 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
676 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
677 Saved_EA : constant Boolean := Expander_Active;
678 Saved_ISMP : constant Boolean :=
679 Ignore_SPARK_Mode_Pragmas_In_Instance;
680 -- Save the Ghost and SPARK mode-related data to restore on exit
684 Last_Spec_Entity : Entity_Id;
689 -- Start of processing for Analyze_Package_Body_Helper
692 -- Find corresponding package specification, and establish the current
693 -- scope. The visible defining entity for the package is the defining
694 -- occurrence in the spec. On exit from the package body, all body
695 -- declarations are attached to the defining entity for the body, but
696 -- the later is never used for name resolution. In this fashion there
697 -- is only one visible entity that denotes the package.
699 -- Set Body_Id. Note that this will be reset to point to the generic
700 -- copy later on in the generic case.
702 Body_Id := Defining_Entity (N);
704 -- Body is body of package instantiation. Corresponding spec has already
707 if Present (Corresponding_Spec (N)) then
708 Spec_Id := Corresponding_Spec (N);
709 Pack_Decl := Unit_Declaration_Node (Spec_Id);
712 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
715 and then Is_Package_Or_Generic_Package (Spec_Id)
717 Pack_Decl := Unit_Declaration_Node (Spec_Id);
719 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
720 Error_Msg_N ("cannot supply body for package renaming", N);
723 elsif Present (Corresponding_Body (Pack_Decl)) then
724 Error_Msg_N ("redefinition of package body", N);
729 Error_Msg_N ("missing specification for package body", N);
733 if Is_Package_Or_Generic_Package (Spec_Id)
734 and then (Scope (Spec_Id) = Standard_Standard
735 or else Is_Child_Unit (Spec_Id))
736 and then not Unit_Requires_Body (Spec_Id)
738 if Ada_Version = Ada_83 then
740 ("optional package body (not allowed in Ada 95)??", N);
742 Error_Msg_N ("spec of this package does not allow a body", N);
747 -- A [generic] package body freezes the contract of the nearest
748 -- enclosing package body and all other contracts encountered in
749 -- the same declarative part up to and excluding the package body:
751 -- package body Nearest_Enclosing_Package
752 -- with Refined_State => (State => Constit)
756 -- package body Freezes_Enclosing_Package_Body
757 -- with Refined_State => (State_2 => Constit_2)
762 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
764 -- This ensures that any annotations referenced by the contract of a
765 -- [generic] subprogram body declared within the current package body
766 -- are available. This form of freezing is decoupled from the usual
767 -- Freeze_xxx mechanism because it must also work in the context of
768 -- generics where normal freezing is disabled.
770 -- Only bodies coming from source should cause this type of freezing.
771 -- Instantiated generic bodies are excluded because their processing is
772 -- performed in a separate compilation pass which lacks enough semantic
773 -- information with respect to contract analysis. It is safe to suppress
774 -- the freezing of contracts in this case because this action already
775 -- took place at the end of the enclosing declarative part.
777 if Comes_From_Source (N)
778 and then not Is_Generic_Instance (Spec_Id)
780 Freeze_Previous_Contracts (N);
783 -- A package body is Ghost when the corresponding spec is Ghost. Set
784 -- the mode now to ensure that any nodes generated during analysis and
785 -- expansion are properly flagged as ignored Ghost.
787 Mark_And_Set_Ghost_Body (N, Spec_Id);
789 -- Deactivate expansion inside the body of ignored Ghost entities,
790 -- as this code will ultimately be ignored. This avoids requiring the
791 -- presence of run-time units which are not needed. Only do this for
792 -- user entities, as internally generated entities might still need
793 -- to be expanded (e.g. those generated for types).
795 if Present (Ignored_Ghost_Region)
796 and then Comes_From_Source (Body_Id)
798 Expander_Active := False;
801 -- If the body completes the initial declaration of a compilation unit
802 -- which is subject to pragma Elaboration_Checks, set the model of the
803 -- pragma because it applies to all parts of the unit.
805 Install_Elaboration_Model (Spec_Id);
807 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
808 Style.Check_Identifier (Body_Id, Spec_Id);
810 if Is_Child_Unit (Spec_Id) then
811 if Nkind (Parent (N)) /= N_Compilation_Unit then
813 ("body of child unit& cannot be an inner package", N, Spec_Id);
816 Set_Is_Child_Unit (Body_Id);
819 -- Generic package case
821 if Ekind (Spec_Id) = E_Generic_Package then
823 -- Disable expansion and perform semantic analysis on copy. The
824 -- unannotated body will be used in all instantiations.
826 Body_Id := Defining_Entity (N);
827 Set_Ekind (Body_Id, E_Package_Body);
828 Set_Scope (Body_Id, Scope (Spec_Id));
829 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
830 Set_Body_Entity (Spec_Id, Body_Id);
831 Set_Spec_Entity (Body_Id, Spec_Id);
833 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
836 -- Once the contents of the generic copy and the template are
837 -- swapped, do the same for their respective aspect specifications.
839 Exchange_Aspects (N, New_N);
841 -- Collect all contract-related source pragmas found within the
842 -- template and attach them to the contract of the package body.
843 -- This contract is used in the capture of global references within
846 Create_Generic_Contract (N);
848 -- Update Body_Id to point to the copied node for the remainder of
851 Body_Id := Defining_Entity (N);
855 -- The Body_Id is that of the copied node in the generic case, the
856 -- current node otherwise. Note that N was rewritten above, so we must
857 -- be sure to get the latest Body_Id value.
859 Set_Ekind (Body_Id, E_Package_Body);
860 Set_Body_Entity (Spec_Id, Body_Id);
861 Set_Spec_Entity (Body_Id, Spec_Id);
863 -- Defining name for the package body is not a visible entity: Only the
864 -- defining name for the declaration is visible.
866 Set_Etype (Body_Id, Standard_Void_Type);
867 Set_Scope (Body_Id, Scope (Spec_Id));
868 Set_Corresponding_Spec (N, Spec_Id);
869 Set_Corresponding_Body (Pack_Decl, Body_Id);
871 -- The body entity is not used for semantics or code generation, but
872 -- it is attached to the entity list of the enclosing scope to simplify
873 -- the listing of back-annotations for the types it main contain.
875 if Scope (Spec_Id) /= Standard_Standard then
876 Append_Entity (Body_Id, Scope (Spec_Id));
879 -- Indicate that we are currently compiling the body of the package
881 Set_In_Package_Body (Spec_Id);
882 Set_Has_Completion (Spec_Id);
883 Last_Spec_Entity := Last_Entity (Spec_Id);
885 if Has_Aspects (N) then
886 Analyze_Aspect_Specifications (N, Body_Id);
889 Push_Scope (Spec_Id);
891 -- Set SPARK_Mode only for non-generic package
893 if Ekind (Spec_Id) = E_Package then
894 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
895 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
896 Set_SPARK_Pragma_Inherited (Body_Id);
897 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
899 -- A package body may be instantiated or inlined at a later pass.
900 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
901 -- it applied to the package spec.
903 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
904 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
908 Set_Categorization_From_Pragmas (N);
910 Install_Visible_Declarations (Spec_Id);
911 Install_Private_Declarations (Spec_Id);
912 Install_Private_With_Clauses (Spec_Id);
913 Install_Composite_Operations (Spec_Id);
915 Check_Anonymous_Access_Types (Spec_Id, N);
917 if Ekind (Spec_Id) = E_Generic_Package then
918 Set_Use (Generic_Formal_Declarations (Pack_Decl));
921 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
922 Set_Use (Private_Declarations (Specification (Pack_Decl)));
924 -- This is a nested package, so it may be necessary to declare certain
925 -- inherited subprograms that are not yet visible because the parent
926 -- type's subprograms are now visible.
927 -- Note that for child units these operations were generated when
928 -- analyzing the package specification.
930 if Ekind (Scope (Spec_Id)) = E_Package
931 and then Scope (Spec_Id) /= Standard_Standard
932 and then not Is_Child_Unit (Spec_Id)
934 Declare_Inherited_Private_Subprograms (Spec_Id);
937 if Present (Declarations (N)) then
938 Analyze_Declarations (Declarations (N));
939 Inspect_Deferred_Constant_Completion (Declarations (N));
942 -- Verify that the SPARK_Mode of the body agrees with that of its spec
944 if Present (SPARK_Pragma (Body_Id)) then
945 if Present (SPARK_Aux_Pragma (Spec_Id)) then
946 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
949 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
951 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
952 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
953 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
955 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
958 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
959 -- as specified in source code, or because SPARK_Mode On is ignored
960 -- in an instance where the context is SPARK_Mode Off/Auto.
962 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
963 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
968 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
969 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
970 Error_Msg_Sloc := Sloc (Spec_Id);
972 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
976 -- Analyze_Declarations has caused freezing of all types. Now generate
977 -- bodies for RACW primitives and stream attributes, if any.
979 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
981 -- Attach subprogram bodies to support RACWs declared in spec
983 Append_RACW_Bodies (Declarations (N), Spec_Id);
984 Analyze_List (Declarations (N));
987 HSS := Handled_Statement_Sequence (N);
989 if Present (HSS) then
990 Process_End_Label (HSS, 't', Spec_Id);
993 -- Check that elaboration code in a preelaborable package body is
994 -- empty other than null statements and labels (RM 10.2.1(6)).
996 Validate_Null_Statement_Sequence (N);
999 Validate_Categorization_Dependency (N, Spec_Id);
1000 Check_Completion (Body_Id);
1002 -- Generate start of body reference. Note that we do this fairly late,
1003 -- because the call will use In_Extended_Main_Source_Unit as a check,
1004 -- and we want to make sure that Corresponding_Stub links are set
1006 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1008 -- For a generic package, collect global references and mark them on
1009 -- the original body so that they are not resolved again at the point
1010 -- of instantiation.
1012 if Ekind (Spec_Id) /= E_Package then
1013 Save_Global_References (Original_Node (N));
1017 -- The entities of the package body have so far been chained onto the
1018 -- declaration chain for the spec. That's been fine while we were in the
1019 -- body, since we wanted them to be visible, but now that we are leaving
1020 -- the package body, they are no longer visible, so we remove them from
1021 -- the entity chain of the package spec entity, and copy them to the
1022 -- entity chain of the package body entity, where they will never again
1025 if Present (Last_Spec_Entity) then
1026 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1027 Set_Next_Entity (Last_Spec_Entity, Empty);
1028 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1029 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1032 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1033 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1034 Set_First_Entity (Spec_Id, Empty);
1035 Set_Last_Entity (Spec_Id, Empty);
1038 Update_Use_Clause_Chain;
1039 End_Package_Scope (Spec_Id);
1041 -- All entities declared in body are not visible
1047 E := First_Entity (Body_Id);
1048 while Present (E) loop
1049 Set_Is_Immediately_Visible (E, False);
1050 Set_Is_Potentially_Use_Visible (E, False);
1053 -- Child units may appear on the entity list (e.g. if they appear
1054 -- in the context of a subunit) but they are not body entities.
1056 if not Is_Child_Unit (E) then
1057 Set_Is_Package_Body_Entity (E);
1064 Check_References (Body_Id);
1066 -- For a generic unit, check that the formal parameters are referenced,
1067 -- and that local variables are used, as for regular packages.
1069 if Ekind (Spec_Id) = E_Generic_Package then
1070 Check_References (Spec_Id);
1073 -- At this point all entities of the package body are externally visible
1074 -- to the linker as their Is_Public flag is set to True. This proactive
1075 -- approach is necessary because an inlined or a generic body for which
1076 -- code is generated in other units may need to see these entities. Cut
1077 -- down the number of global symbols that do not need public visibility
1078 -- as this has two beneficial effects:
1079 -- (1) It makes the compilation process more efficient.
1080 -- (2) It gives the code generator more leeway to optimize within each
1081 -- unit, especially subprograms.
1083 -- This is done only for top-level library packages or child units as
1084 -- the algorithm does a top-down traversal of the package body.
1086 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
1087 and then not Is_Generic_Unit (Spec_Id)
1089 Hide_Public_Entities (Declarations (N));
1092 -- If expander is not active, then here is where we turn off the
1093 -- In_Package_Body flag, otherwise it is turned off at the end of the
1094 -- corresponding expansion routine. If this is an instance body, we need
1095 -- to qualify names of local entities, because the body may have been
1096 -- compiled as a preliminary to another instantiation.
1098 if not Expander_Active then
1099 Set_In_Package_Body (Spec_Id, False);
1101 if Is_Generic_Instance (Spec_Id)
1102 and then Operating_Mode = Generate_Code
1104 Qualify_Entity_Names (N);
1108 if Present (Ignored_Ghost_Region) then
1109 Expander_Active := Saved_EA;
1112 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1113 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1114 end Analyze_Package_Body_Helper;
1116 ---------------------------------
1117 -- Analyze_Package_Declaration --
1118 ---------------------------------
1120 procedure Analyze_Package_Declaration (N : Node_Id) is
1121 Id : constant Node_Id := Defining_Entity (N);
1123 Is_Comp_Unit : constant Boolean :=
1124 Nkind (Parent (N)) = N_Compilation_Unit;
1126 Body_Required : Boolean;
1127 -- True when this package declaration requires a corresponding body
1130 if Debug_Flag_C then
1131 Write_Str ("==> package spec ");
1132 Write_Name (Chars (Id));
1133 Write_Str (" from ");
1134 Write_Location (Sloc (N));
1139 Generate_Definition (Id);
1141 Set_Ekind (Id, E_Package);
1142 Set_Etype (Id, Standard_Void_Type);
1144 -- Set SPARK_Mode from context
1146 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1147 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1148 Set_SPARK_Pragma_Inherited (Id);
1149 Set_SPARK_Aux_Pragma_Inherited (Id);
1151 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1152 -- the body of this package is instantiated or inlined later and out of
1153 -- context. The body uses this attribute to restore the value of the
1156 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1157 Set_Ignore_SPARK_Mode_Pragmas (Id);
1160 -- Analyze aspect specifications immediately, since we need to recognize
1161 -- things like Pure early enough to diagnose violations during analysis.
1163 if Has_Aspects (N) then
1164 Analyze_Aspect_Specifications (N, Id);
1167 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1168 -- a limited-with clause of its own context. In this case the error has
1169 -- been previously notified by Analyze_Context.
1171 -- limited with Pkg; -- ERROR
1172 -- package Pkg is ...
1174 if From_Limited_With (Id) then
1180 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1181 Set_Categorization_From_Pragmas (N);
1183 Analyze (Specification (N));
1184 Validate_Categorization_Dependency (N, Id);
1186 -- Determine whether the package requires a body. Abstract states are
1187 -- intentionally ignored because they do require refinement which can
1188 -- only come in a body, but at the same time they do not force the need
1189 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1191 Body_Required := Unit_Requires_Body (Id);
1193 if not Body_Required then
1195 -- If the package spec does not require an explicit body, then there
1196 -- are not entities requiring completion in the language sense. Call
1197 -- Check_Completion now to ensure that nested package declarations
1198 -- that require an implicit body get one. (In the case where a body
1199 -- is required, Check_Completion is called at the end of the body's
1200 -- declarative part.)
1204 -- If the package spec does not require an explicit body, then all
1205 -- abstract states declared in nested packages cannot possibly get
1206 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1207 -- only when the compilation unit is the main unit to allow for
1208 -- modular SPARK analysis where packages do not necessarily have
1211 if Is_Comp_Unit then
1212 Check_State_Refinements
1214 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1218 -- Set Body_Required indication on the compilation unit node
1220 if Is_Comp_Unit then
1221 Set_Body_Required (Parent (N), Body_Required);
1223 if Legacy_Elaboration_Checks and not Body_Required then
1224 Set_Suppress_Elaboration_Warnings (Id);
1228 End_Package_Scope (Id);
1230 -- For the declaration of a library unit that is a remote types package,
1231 -- check legality rules regarding availability of stream attributes for
1232 -- types that contain non-remote access values. This subprogram performs
1233 -- visibility tests that rely on the fact that we have exited the scope
1236 if Is_Comp_Unit then
1237 Validate_RT_RAT_Component (N);
1240 if Debug_Flag_C then
1242 Write_Str ("<== package spec ");
1243 Write_Name (Chars (Id));
1244 Write_Str (" from ");
1245 Write_Location (Sloc (N));
1248 end Analyze_Package_Declaration;
1250 -----------------------------------
1251 -- Analyze_Package_Specification --
1252 -----------------------------------
1254 -- Note that this code is shared for the analysis of generic package specs
1255 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1257 procedure Analyze_Package_Specification (N : Node_Id) is
1258 Id : constant Entity_Id := Defining_Entity (N);
1259 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1260 Vis_Decls : constant List_Id := Visible_Declarations (N);
1261 Priv_Decls : constant List_Id := Private_Declarations (N);
1264 Public_Child : Boolean;
1266 Private_With_Clauses_Installed : Boolean := False;
1267 -- In Ada 2005, private with_clauses are visible in the private part
1268 -- of a nested package, even if it appears in the public part of the
1269 -- enclosing package. This requires a separate step to install these
1270 -- private_with_clauses, and remove them at the end of the nested
1273 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1274 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1275 -- Is_True_Constant) on all variables that are entities of Id, and on
1276 -- the chain whose first element is FE. A recursive call is made for all
1277 -- packages and generic packages.
1279 procedure Generate_Parent_References;
1280 -- For a child unit, generate references to parent units, for
1281 -- GNAT Studio navigation purposes.
1283 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1284 -- Child and Unit are entities of compilation units. True if Child
1285 -- is a public child of Parent as defined in 10.1.1
1287 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1288 -- Reject completion of an incomplete or private type declarations
1289 -- having a known discriminant part by an unchecked union.
1291 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1292 -- Given the package entity of a generic package instantiation or
1293 -- formal package whose corresponding generic is a child unit, installs
1294 -- the private declarations of each of the child unit's parents.
1295 -- This has to be done at the point of entering the instance package's
1296 -- private part rather than being done in Sem_Ch12.Install_Parent
1297 -- (which is where the parents' visible declarations are installed).
1299 ---------------------
1300 -- Clear_Constants --
1301 ---------------------
1303 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1307 -- Ignore package renamings, not interesting and they can cause self
1308 -- referential loops in the code below.
1310 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1314 -- Note: in the loop below, the check for Next_Entity pointing back
1315 -- to the package entity may seem odd, but it is needed, because a
1316 -- package can contain a renaming declaration to itself, and such
1317 -- renamings are generated automatically within package instances.
1320 while Present (E) and then E /= Id loop
1321 if Is_Assignable (E) then
1322 Set_Never_Set_In_Source (E, False);
1323 Set_Is_True_Constant (E, False);
1324 Set_Current_Value (E, Empty);
1325 Set_Is_Known_Null (E, False);
1326 Set_Last_Assignment (E, Empty);
1328 if not Can_Never_Be_Null (E) then
1329 Set_Is_Known_Non_Null (E, False);
1332 elsif Is_Package_Or_Generic_Package (E) then
1333 Clear_Constants (E, First_Entity (E));
1334 Clear_Constants (E, First_Private_Entity (E));
1339 end Clear_Constants;
1341 --------------------------------
1342 -- Generate_Parent_References --
1343 --------------------------------
1345 procedure Generate_Parent_References is
1346 Decl : constant Node_Id := Parent (N);
1349 if Id = Cunit_Entity (Main_Unit)
1350 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1352 Generate_Reference (Id, Scope (Id), 'k', False);
1354 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1357 -- If current unit is an ancestor of main unit, generate a
1358 -- reference to its own parent.
1362 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1365 if Nkind (Main_Spec) = N_Package_Body then
1366 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1369 U := Parent_Spec (Main_Spec);
1370 while Present (U) loop
1371 if U = Parent (Decl) then
1372 Generate_Reference (Id, Scope (Id), 'k', False);
1375 elsif Nkind (Unit (U)) = N_Package_Body then
1379 U := Parent_Spec (Unit (U));
1384 end Generate_Parent_References;
1386 ---------------------
1387 -- Is_Public_Child --
1388 ---------------------
1390 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1392 if not Is_Private_Descendant (Child) then
1395 if Child = Unit then
1396 return not Private_Present (
1397 Parent (Unit_Declaration_Node (Child)));
1399 return Is_Public_Child (Scope (Child), Unit);
1402 end Is_Public_Child;
1404 ----------------------------------------
1405 -- Inspect_Unchecked_Union_Completion --
1406 ----------------------------------------
1408 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1412 Decl := First (Decls);
1413 while Present (Decl) loop
1415 -- We are looking at an incomplete or private type declaration
1416 -- with a known_discriminant_part whose full view is an
1417 -- Unchecked_Union. The seemingly useless check with Is_Type
1418 -- prevents cascaded errors when routines defined only for type
1419 -- entities are called with non-type entities.
1421 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1422 N_Private_Type_Declaration)
1423 and then Is_Type (Defining_Identifier (Decl))
1424 and then Has_Discriminants (Defining_Identifier (Decl))
1425 and then Present (Full_View (Defining_Identifier (Decl)))
1427 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1430 ("completion of discriminated partial view "
1431 & "cannot be an unchecked union",
1432 Full_View (Defining_Identifier (Decl)));
1437 end Inspect_Unchecked_Union_Completion;
1439 -----------------------------------------
1440 -- Install_Parent_Private_Declarations --
1441 -----------------------------------------
1443 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1444 Inst_Par : Entity_Id;
1445 Gen_Par : Entity_Id;
1446 Inst_Node : Node_Id;
1449 Inst_Par := Inst_Id;
1452 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1453 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1454 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1456 if Nkind_In (Inst_Node, N_Package_Instantiation,
1457 N_Formal_Package_Declaration)
1458 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1460 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1462 if Present (Renamed_Entity (Inst_Par)) then
1463 Inst_Par := Renamed_Entity (Inst_Par);
1466 -- The instance may appear in a sibling generic unit, in
1467 -- which case the prefix must include the common (generic)
1468 -- ancestor, which is treated as a current instance.
1471 and then Ekind (Inst_Par) = E_Generic_Package
1473 Gen_Par := Inst_Par;
1474 pragma Assert (In_Open_Scopes (Gen_Par));
1479 (Specification (Unit_Declaration_Node (Inst_Par)));
1482 -- Install the private declarations and private use clauses
1483 -- of a parent instance of the child instance, unless the
1484 -- parent instance private declarations have already been
1485 -- installed earlier in Analyze_Package_Specification, which
1486 -- happens when a generic child is instantiated, and the
1487 -- instance is a child of the parent instance.
1489 -- Installing the use clauses of the parent instance twice
1490 -- is both unnecessary and wrong, because it would cause the
1491 -- clauses to be chained to themselves in the use clauses
1492 -- list of the scope stack entry. That in turn would cause
1493 -- an endless loop from End_Use_Clauses upon scope exit.
1495 -- The parent is now fully visible. It may be a hidden open
1496 -- scope if we are currently compiling some child instance
1497 -- declared within it, but while the current instance is being
1498 -- compiled the parent is immediately visible. In particular
1499 -- its entities must remain visible if a stack save/restore
1500 -- takes place through a call to Rtsfind.
1502 if Present (Gen_Par) then
1503 if not In_Private_Part (Inst_Par) then
1504 Install_Private_Declarations (Inst_Par);
1505 Set_Use (Private_Declarations
1507 (Unit_Declaration_Node (Inst_Par))));
1508 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1511 -- If we've reached the end of the generic instance parents,
1512 -- then finish off by looping through the nongeneric parents
1513 -- and installing their private declarations.
1515 -- If one of the non-generic parents is itself on the scope
1516 -- stack, do not install its private declarations: they are
1517 -- installed in due time when the private part of that parent
1521 while Present (Inst_Par)
1522 and then Inst_Par /= Standard_Standard
1523 and then (not In_Open_Scopes (Inst_Par)
1524 or else not In_Private_Part (Inst_Par))
1526 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1528 not Is_Ancestor_Package
1529 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1531 Install_Private_Declarations (Inst_Par);
1533 (Private_Declarations
1535 (Unit_Declaration_Node (Inst_Par))));
1536 Inst_Par := Scope (Inst_Par);
1549 end Install_Parent_Private_Declarations;
1551 -- Start of processing for Analyze_Package_Specification
1554 if Present (Vis_Decls) then
1555 Analyze_Declarations (Vis_Decls);
1558 -- Inspect the entities defined in the package and ensure that all
1559 -- incomplete types have received full declarations. Build default
1560 -- initial condition and invariant procedures for all qualifying types.
1562 E := First_Entity (Id);
1563 while Present (E) loop
1565 -- Check on incomplete types
1567 -- AI05-0213: A formal incomplete type has no completion, and neither
1568 -- does the corresponding subtype in an instance.
1570 if Is_Incomplete_Type (E)
1571 and then No (Full_View (E))
1572 and then not Is_Generic_Type (E)
1573 and then not From_Limited_With (E)
1574 and then not Is_Generic_Actual_Type (E)
1576 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1582 if Is_Remote_Call_Interface (Id)
1583 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1585 Validate_RCI_Declarations (Id);
1588 -- Save global references in the visible declarations, before installing
1589 -- private declarations of parent unit if there is one, because the
1590 -- privacy status of types defined in the parent will change. This is
1591 -- only relevant for generic child units, but is done in all cases for
1594 if Ekind (Id) = E_Generic_Package
1595 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1598 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1599 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1602 -- Insert the freezing nodes after the visible declarations to
1603 -- ensure that we analyze its aspects; needed to ensure that
1604 -- global entities referenced in the aspects are properly handled.
1606 if Ada_Version >= Ada_2012
1607 and then Is_Non_Empty_List (Vis_Decls)
1608 and then Is_Empty_List (Priv_Decls)
1610 Insert_List_After_And_Analyze
1611 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1614 Set_Private_Declarations (Orig_Spec, Empty_List);
1615 Save_Global_References (Orig_Decl);
1616 Set_Private_Declarations (Orig_Spec, Save_Priv);
1620 -- If package is a public child unit, then make the private declarations
1621 -- of the parent visible.
1623 Public_Child := False;
1627 Pack_Decl : Node_Id;
1632 Par_Spec := Parent_Spec (Parent (N));
1634 -- If the package is formal package of an enclosing generic, it is
1635 -- transformed into a local generic declaration, and compiled to make
1636 -- its spec available. We need to retrieve the original generic to
1637 -- determine whether it is a child unit, and install its parents.
1641 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1643 Par := Entity (Name (Original_Node (Parent (N))));
1644 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1647 if Present (Par_Spec) then
1648 Generate_Parent_References;
1650 while Scope (Par) /= Standard_Standard
1651 and then Is_Public_Child (Id, Par)
1652 and then In_Open_Scopes (Par)
1654 Public_Child := True;
1656 Install_Private_Declarations (Par);
1657 Install_Private_With_Clauses (Par);
1658 Pack_Decl := Unit_Declaration_Node (Par);
1659 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1664 if Is_Compilation_Unit (Id) then
1665 Install_Private_With_Clauses (Id);
1667 -- The current compilation unit may include private with_clauses,
1668 -- which are visible in the private part of the current nested
1669 -- package, and have to be installed now. This is not done for
1670 -- nested instantiations, where the private with_clauses of the
1671 -- enclosing unit have no effect once the instantiation info is
1672 -- established and we start analyzing the package declaration.
1675 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1677 if Is_Package_Or_Generic_Package (Comp_Unit)
1678 and then not In_Private_Part (Comp_Unit)
1679 and then not In_Instance
1681 Install_Private_With_Clauses (Comp_Unit);
1682 Private_With_Clauses_Installed := True;
1687 -- If this is a package associated with a generic instance or formal
1688 -- package, then the private declarations of each of the generic's
1689 -- parents must be installed at this point.
1691 if Is_Generic_Instance (Id) then
1692 Install_Parent_Private_Declarations (Id);
1695 -- Analyze private part if present. The flag In_Private_Part is reset
1696 -- in End_Package_Scope.
1698 L := Last_Entity (Id);
1700 if Present (Priv_Decls) then
1701 Set_In_Private_Part (Id);
1703 -- Upon entering a public child's private part, it may be necessary
1704 -- to declare subprograms that were derived in the package's visible
1705 -- part but not yet made visible.
1707 if Public_Child then
1708 Declare_Inherited_Private_Subprograms (Id);
1711 Analyze_Declarations (Priv_Decls);
1713 -- Check the private declarations for incomplete deferred constants
1715 Inspect_Deferred_Constant_Completion (Priv_Decls);
1717 -- The first private entity is the immediate follower of the last
1718 -- visible entity, if there was one.
1721 Set_First_Private_Entity (Id, Next_Entity (L));
1723 Set_First_Private_Entity (Id, First_Entity (Id));
1726 -- There may be inherited private subprograms that need to be declared,
1727 -- even in the absence of an explicit private part. If there are any
1728 -- public declarations in the package and the package is a public child
1729 -- unit, then an implicit private part is assumed.
1731 elsif Present (L) and then Public_Child then
1732 Set_In_Private_Part (Id);
1733 Declare_Inherited_Private_Subprograms (Id);
1734 Set_First_Private_Entity (Id, Next_Entity (L));
1737 E := First_Entity (Id);
1738 while Present (E) loop
1740 -- Check rule of 3.6(11), which in general requires waiting till all
1741 -- full types have been seen.
1743 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1744 Check_Aliased_Component_Types (E);
1747 -- Check preelaborable initialization for full type completing a
1748 -- private type for which pragma Preelaborable_Initialization given.
1751 and then Must_Have_Preelab_Init (E)
1752 and then not Has_Preelaborable_Initialization (E)
1755 ("full view of & does not have preelaborable initialization", E);
1761 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1762 -- declaration having a known_discriminant_part shall not be an
1763 -- unchecked union type.
1765 if Present (Vis_Decls) then
1766 Inspect_Unchecked_Union_Completion (Vis_Decls);
1769 if Present (Priv_Decls) then
1770 Inspect_Unchecked_Union_Completion (Priv_Decls);
1773 if Ekind (Id) = E_Generic_Package
1774 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1775 and then Present (Priv_Decls)
1777 -- Save global references in private declarations, ignoring the
1778 -- visible declarations that were processed earlier.
1781 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1782 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1783 Save_Form : constant List_Id :=
1784 Generic_Formal_Declarations (Orig_Decl);
1787 -- Insert the freezing nodes after the private declarations to
1788 -- ensure that we analyze its aspects; needed to ensure that
1789 -- global entities referenced in the aspects are properly handled.
1791 if Ada_Version >= Ada_2012
1792 and then Is_Non_Empty_List (Priv_Decls)
1794 Insert_List_After_And_Analyze
1795 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1798 Set_Visible_Declarations (Orig_Spec, Empty_List);
1799 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1800 Save_Global_References (Orig_Decl);
1801 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1802 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1806 Process_End_Label (N, 'e', Id);
1808 -- Remove private_with_clauses of enclosing compilation unit, if they
1811 if Private_With_Clauses_Installed then
1812 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1815 -- For the case of a library level package, we must go through all the
1816 -- entities clearing the indications that the value may be constant and
1817 -- not modified. Why? Because any client of this package may modify
1818 -- these values freely from anywhere. This also applies to any nested
1819 -- packages or generic packages.
1821 -- For now we unconditionally clear constants for packages that are
1822 -- instances of generic packages. The reason is that we do not have the
1823 -- body yet, and we otherwise think things are unreferenced when they
1824 -- are not. This should be fixed sometime (the effect is not terrible,
1825 -- we just lose some warnings, and also some cases of value propagation)
1828 if Is_Library_Level_Entity (Id)
1829 or else Is_Generic_Instance (Id)
1831 Clear_Constants (Id, First_Entity (Id));
1832 Clear_Constants (Id, First_Private_Entity (Id));
1835 -- Output relevant information as to why the package requires a body.
1836 -- Do not consider generated packages as this exposes internal symbols
1837 -- and leads to confusing messages.
1839 if List_Body_Required_Info
1840 and then In_Extended_Main_Source_Unit (Id)
1841 and then Unit_Requires_Body (Id)
1842 and then Comes_From_Source (Id)
1844 Unit_Requires_Body_Info (Id);
1847 -- Nested package specs that do not require bodies are not checked for
1848 -- ineffective use clauses due to the possibility of subunits. This is
1849 -- because at this stage it is impossible to tell whether there will be
1852 if not Unit_Requires_Body (Id)
1853 and then Is_Compilation_Unit (Id)
1854 and then not Is_Private_Descendant (Id)
1856 Update_Use_Clause_Chain;
1858 end Analyze_Package_Specification;
1860 --------------------------------------
1861 -- Analyze_Private_Type_Declaration --
1862 --------------------------------------
1864 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1865 Id : constant Entity_Id := Defining_Identifier (N);
1866 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1869 Generate_Definition (Id);
1870 Set_Is_Pure (Id, PF);
1871 Init_Size_Align (Id);
1873 if not Is_Package_Or_Generic_Package (Current_Scope)
1874 or else In_Private_Part (Current_Scope)
1876 Error_Msg_N ("invalid context for private declaration", N);
1879 New_Private_Type (N, Id, N);
1880 Set_Depends_On_Private (Id);
1882 -- Set the SPARK mode from the current context
1884 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1885 Set_SPARK_Pragma_Inherited (Id);
1887 if Has_Aspects (N) then
1888 Analyze_Aspect_Specifications (N, Id);
1890 end Analyze_Private_Type_Declaration;
1892 ----------------------------------
1893 -- Check_Anonymous_Access_Types --
1894 ----------------------------------
1896 procedure Check_Anonymous_Access_Types
1897 (Spec_Id : Entity_Id;
1904 -- Itype references are only needed by gigi, to force elaboration of
1905 -- itypes. In the absence of code generation, they are not needed.
1907 if not Expander_Active then
1911 E := First_Entity (Spec_Id);
1912 while Present (E) loop
1913 if Ekind (E) = E_Anonymous_Access_Type
1914 and then From_Limited_With (E)
1916 IR := Make_Itype_Reference (Sloc (P_Body));
1919 if No (Declarations (P_Body)) then
1920 Set_Declarations (P_Body, New_List (IR));
1922 Prepend (IR, Declarations (P_Body));
1928 end Check_Anonymous_Access_Types;
1930 -------------------------------------------
1931 -- Declare_Inherited_Private_Subprograms --
1932 -------------------------------------------
1934 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1936 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1937 -- Check whether an inherited subprogram S is an operation of an
1938 -- untagged derived type T.
1940 ---------------------
1941 -- Is_Primitive_Of --
1942 ---------------------
1944 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1948 -- If the full view is a scalar type, the type is the anonymous base
1949 -- type, but the operation mentions the first subtype, so check the
1950 -- signature against the base type.
1952 if Base_Type (Etype (S)) = Base_Type (T) then
1956 Formal := First_Formal (S);
1957 while Present (Formal) loop
1958 if Base_Type (Etype (Formal)) = Base_Type (T) then
1962 Next_Formal (Formal);
1967 end Is_Primitive_Of;
1974 Op_Elmt_2 : Elmt_Id;
1975 Prim_Op : Entity_Id;
1976 New_Op : Entity_Id := Empty;
1977 Parent_Subp : Entity_Id;
1980 -- Start of processing for Declare_Inherited_Private_Subprograms
1983 E := First_Entity (Id);
1984 while Present (E) loop
1986 -- If the entity is a nonprivate type extension whose parent type
1987 -- is declared in an open scope, then the type may have inherited
1988 -- operations that now need to be made visible. Ditto if the entity
1989 -- is a formal derived type in a child unit.
1991 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1993 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1994 and then Is_Generic_Type (E)))
1995 and then In_Open_Scopes (Scope (Etype (E)))
1996 and then Is_Base_Type (E)
1998 if Is_Tagged_Type (E) then
1999 Op_List := Primitive_Operations (E);
2001 Tag := First_Tag_Component (E);
2003 Op_Elmt := First_Elmt (Op_List);
2004 while Present (Op_Elmt) loop
2005 Prim_Op := Node (Op_Elmt);
2007 -- Search primitives that are implicit operations with an
2008 -- internal name whose parent operation has a normal name.
2010 if Present (Alias (Prim_Op))
2011 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2012 and then not Comes_From_Source (Prim_Op)
2013 and then Is_Internal_Name (Chars (Prim_Op))
2014 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2016 Parent_Subp := Alias (Prim_Op);
2018 -- Case 1: Check if the type has also an explicit
2019 -- overriding for this primitive.
2021 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2022 while Present (Op_Elmt_2) loop
2024 -- Skip entities with attribute Interface_Alias since
2025 -- they are not overriding primitives (these entities
2026 -- link an interface primitive with their covering
2029 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2030 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2031 and then No (Interface_Alias (Node (Op_Elmt_2)))
2033 -- The private inherited operation has been
2034 -- overridden by an explicit subprogram:
2035 -- replace the former by the latter.
2037 New_Op := Node (Op_Elmt_2);
2038 Replace_Elmt (Op_Elmt, New_Op);
2039 Remove_Elmt (Op_List, Op_Elmt_2);
2040 Set_Overridden_Operation (New_Op, Parent_Subp);
2042 -- We don't need to inherit its dispatching slot.
2043 -- Set_All_DT_Position has previously ensured that
2044 -- the same slot was assigned to the two primitives
2047 and then Present (DTC_Entity (New_Op))
2048 and then Present (DTC_Entity (Prim_Op))
2051 (DT_Position (New_Op) = DT_Position (Prim_Op));
2055 goto Next_Primitive;
2058 Next_Elmt (Op_Elmt_2);
2061 -- Case 2: We have not found any explicit overriding and
2062 -- hence we need to declare the operation (i.e., make it
2065 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2067 -- Inherit the dispatching slot if E is already frozen
2070 and then Present (DTC_Entity (Alias (Prim_Op)))
2072 Set_DTC_Entity_Value (E, New_Op);
2073 Set_DT_Position_Value (New_Op,
2074 DT_Position (Alias (Prim_Op)));
2078 (Is_Dispatching_Operation (New_Op)
2079 and then Node (Last_Elmt (Op_List)) = New_Op);
2081 -- Substitute the new operation for the old one in the
2082 -- type's primitive operations list. Since the new
2083 -- operation was also just added to the end of list,
2084 -- the last element must be removed.
2086 -- (Question: is there a simpler way of declaring the
2087 -- operation, say by just replacing the name of the
2088 -- earlier operation, reentering it in the in the symbol
2089 -- table (how?), and marking it as private???)
2091 Replace_Elmt (Op_Elmt, New_Op);
2092 Remove_Last_Elmt (Op_List);
2096 Next_Elmt (Op_Elmt);
2099 -- Generate listing showing the contents of the dispatch table
2101 if Debug_Flag_ZZ then
2106 -- For untagged type, scan forward to locate inherited hidden
2109 Prim_Op := Next_Entity (E);
2110 while Present (Prim_Op) loop
2111 if Is_Subprogram (Prim_Op)
2112 and then Present (Alias (Prim_Op))
2113 and then not Comes_From_Source (Prim_Op)
2114 and then Is_Internal_Name (Chars (Prim_Op))
2115 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2116 and then Is_Primitive_Of (E, Prim_Op)
2118 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2121 Next_Entity (Prim_Op);
2123 -- Derived operations appear immediately after the type
2124 -- declaration (or the following subtype indication for
2125 -- a derived scalar type). Further declarations cannot
2126 -- include inherited operations of the type.
2128 if Present (Prim_Op) then
2129 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2137 end Declare_Inherited_Private_Subprograms;
2139 -----------------------
2140 -- End_Package_Scope --
2141 -----------------------
2143 procedure End_Package_Scope (P : Entity_Id) is
2145 Uninstall_Declarations (P);
2147 end End_Package_Scope;
2149 ---------------------------
2150 -- Exchange_Declarations --
2151 ---------------------------
2153 procedure Exchange_Declarations (Id : Entity_Id) is
2154 Full_Id : constant Entity_Id := Full_View (Id);
2155 H1 : constant Entity_Id := Homonym (Id);
2156 Next1 : constant Entity_Id := Next_Entity (Id);
2161 -- If missing full declaration for type, nothing to exchange
2163 if No (Full_Id) then
2167 -- Otherwise complete the exchange, and preserve semantic links
2169 Next2 := Next_Entity (Full_Id);
2170 H2 := Homonym (Full_Id);
2172 -- Reset full declaration pointer to reflect the switched entities and
2173 -- readjust the next entity chains.
2175 Exchange_Entities (Id, Full_Id);
2177 Link_Entities (Id, Next1);
2178 Set_Homonym (Id, H1);
2180 Set_Full_View (Full_Id, Id);
2181 Link_Entities (Full_Id, Next2);
2182 Set_Homonym (Full_Id, H2);
2183 end Exchange_Declarations;
2185 ----------------------------
2186 -- Install_Package_Entity --
2187 ----------------------------
2189 procedure Install_Package_Entity (Id : Entity_Id) is
2191 if not Is_Internal (Id) then
2192 if Debug_Flag_E then
2193 Write_Str ("Install: ");
2194 Write_Name (Chars (Id));
2198 if Is_Child_Unit (Id) then
2201 -- Do not enter implicitly inherited non-overridden subprograms of
2202 -- a tagged type back into visibility if they have non-conformant
2203 -- homographs (Ada RM 8.3 12.3/2).
2205 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2209 Set_Is_Immediately_Visible (Id);
2212 end Install_Package_Entity;
2214 ----------------------------------
2215 -- Install_Private_Declarations --
2216 ----------------------------------
2218 procedure Install_Private_Declarations (P : Entity_Id) is
2221 Priv_Deps : Elist_Id;
2223 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2224 -- When the full view of a private type is made available, we do the
2225 -- same for its private dependents under proper visibility conditions.
2226 -- When compiling a child unit this needs to be done recursively.
2228 -----------------------------
2229 -- Swap_Private_Dependents --
2230 -----------------------------
2232 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2236 Priv_Elmt : Elmt_Id;
2240 Priv_Elmt := First_Elmt (Priv_Deps);
2241 while Present (Priv_Elmt) loop
2242 Priv := Node (Priv_Elmt);
2244 -- Before the exchange, verify that the presence of the Full_View
2245 -- field. This field will be empty if the entity has already been
2246 -- installed due to a previous call.
2248 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2250 if Is_Private_Type (Priv) then
2251 Cunit := Cunit_Entity (Current_Sem_Unit);
2252 Deps := Private_Dependents (Priv);
2258 -- For each subtype that is swapped, we also swap the reference
2259 -- to it in Private_Dependents, to allow access to it when we
2260 -- swap them out in End_Package_Scope.
2262 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2264 -- Ensure that both views of the dependent private subtype are
2265 -- immediately visible if within some open scope. Check full
2266 -- view before exchanging views.
2268 if In_Open_Scopes (Scope (Full_View (Priv))) then
2269 Set_Is_Immediately_Visible (Priv);
2272 Exchange_Declarations (Priv);
2273 Set_Is_Immediately_Visible
2274 (Priv, In_Open_Scopes (Scope (Priv)));
2276 Set_Is_Potentially_Use_Visible
2277 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2279 -- Recurse for child units, except in generic child units,
2280 -- which unfortunately handle private_dependents separately.
2281 -- Note that the current unit may not have been analyzed,
2282 -- for example a package body, so we cannot rely solely on
2283 -- the Is_Child_Unit flag, but that's only an optimization.
2286 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2287 and then not Is_Empty_Elmt_List (Deps)
2288 and then not Inside_A_Generic
2290 Swap_Private_Dependents (Deps);
2294 Next_Elmt (Priv_Elmt);
2296 end Swap_Private_Dependents;
2298 -- Start of processing for Install_Private_Declarations
2301 -- First exchange declarations for private types, so that the full
2302 -- declaration is visible. For each private type, we check its
2303 -- Private_Dependents list and also exchange any subtypes of or derived
2304 -- types from it. Finally, if this is a Taft amendment type, the
2305 -- incomplete declaration is irrelevant, and we want to link the
2306 -- eventual full declaration with the original private one so we
2307 -- also skip the exchange.
2309 Id := First_Entity (P);
2310 while Present (Id) and then Id /= First_Private_Entity (P) loop
2311 if Is_Private_Base_Type (Id)
2312 and then Present (Full_View (Id))
2313 and then Comes_From_Source (Full_View (Id))
2314 and then Scope (Full_View (Id)) = Scope (Id)
2315 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2317 -- If there is a use-type clause on the private type, set the full
2318 -- view accordingly.
2320 Set_In_Use (Full_View (Id), In_Use (Id));
2321 Full := Full_View (Id);
2323 if Is_Private_Base_Type (Full)
2324 and then Has_Private_Declaration (Full)
2325 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2326 and then In_Open_Scopes (Scope (Etype (Full)))
2327 and then In_Package_Body (Current_Scope)
2328 and then not Is_Private_Type (Etype (Full))
2330 -- This is the completion of a private type by a derivation
2331 -- from another private type which is not private anymore. This
2332 -- can only happen in a package nested within a child package,
2333 -- when the parent type is defined in the parent unit. At this
2334 -- point the current type is not private either, and we have
2335 -- to install the underlying full view, which is now visible.
2336 -- Save the current full view as well, so that all views can be
2337 -- restored on exit. It may seem that after compiling the child
2338 -- body there are not environments to restore, but the back-end
2339 -- expects those links to be valid, and freeze nodes depend on
2342 if No (Full_View (Full))
2343 and then Present (Underlying_Full_View (Full))
2345 Set_Full_View (Id, Underlying_Full_View (Full));
2346 Set_Underlying_Full_View (Id, Full);
2347 Set_Is_Underlying_Full_View (Full);
2349 Set_Underlying_Full_View (Full, Empty);
2350 Set_Is_Frozen (Full_View (Id));
2354 Priv_Deps := Private_Dependents (Id);
2355 Exchange_Declarations (Id);
2356 Set_Is_Immediately_Visible (Id);
2357 Swap_Private_Dependents (Priv_Deps);
2363 -- Next make other declarations in the private part visible as well
2365 Id := First_Private_Entity (P);
2366 while Present (Id) loop
2367 Install_Package_Entity (Id);
2368 Set_Is_Hidden (Id, False);
2372 -- An abstract state is partially refined when it has at least one
2373 -- Part_Of constituent. Since these constituents are being installed
2374 -- into visibility, update the partial refinement status of any state
2375 -- defined in the associated package, subject to at least one Part_Of
2378 if Is_Package_Or_Generic_Package (P) then
2380 States : constant Elist_Id := Abstract_States (P);
2381 State_Elmt : Elmt_Id;
2382 State_Id : Entity_Id;
2385 if Present (States) then
2386 State_Elmt := First_Elmt (States);
2387 while Present (State_Elmt) loop
2388 State_Id := Node (State_Elmt);
2390 if Present (Part_Of_Constituents (State_Id)) then
2391 Set_Has_Partial_Visible_Refinement (State_Id);
2394 Next_Elmt (State_Elmt);
2400 -- Indicate that the private part is currently visible, so it can be
2401 -- properly reset on exit.
2403 Set_In_Private_Part (P);
2404 end Install_Private_Declarations;
2406 ----------------------------------
2407 -- Install_Visible_Declarations --
2408 ----------------------------------
2410 procedure Install_Visible_Declarations (P : Entity_Id) is
2412 Last_Entity : Entity_Id;
2416 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2418 if Is_Package_Or_Generic_Package (P) then
2419 Last_Entity := First_Private_Entity (P);
2421 Last_Entity := Empty;
2424 Id := First_Entity (P);
2425 while Present (Id) and then Id /= Last_Entity loop
2426 Install_Package_Entity (Id);
2429 end Install_Visible_Declarations;
2431 --------------------------
2432 -- Is_Private_Base_Type --
2433 --------------------------
2435 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2437 return Ekind (E) = E_Private_Type
2438 or else Ekind (E) = E_Limited_Private_Type
2439 or else Ekind (E) = E_Record_Type_With_Private;
2440 end Is_Private_Base_Type;
2442 --------------------------
2443 -- Is_Visible_Dependent --
2444 --------------------------
2446 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2448 S : constant Entity_Id := Scope (Dep);
2451 -- Renamings created for actual types have the visibility of the actual
2453 if Ekind (S) = E_Package
2454 and then Is_Generic_Instance (S)
2455 and then (Is_Generic_Actual_Type (Dep)
2456 or else Is_Generic_Actual_Type (Full_View (Dep)))
2460 elsif not (Is_Derived_Type (Dep))
2461 and then Is_Derived_Type (Full_View (Dep))
2463 -- When instantiating a package body, the scope stack is empty, so
2464 -- check instead whether the dependent type is defined in the same
2465 -- scope as the instance itself.
2467 return In_Open_Scopes (S)
2468 or else (Is_Generic_Instance (Current_Scope)
2469 and then Scope (Dep) = Scope (Current_Scope));
2473 end Is_Visible_Dependent;
2475 ----------------------------
2476 -- May_Need_Implicit_Body --
2477 ----------------------------
2479 procedure May_Need_Implicit_Body (E : Entity_Id) is
2480 P : constant Node_Id := Unit_Declaration_Node (E);
2481 S : constant Node_Id := Parent (P);
2486 if not Has_Completion (E)
2487 and then Nkind (P) = N_Package_Declaration
2488 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2491 Make_Package_Body (Sloc (E),
2492 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2493 Chars => Chars (E)),
2494 Declarations => New_List);
2496 if Nkind (S) = N_Package_Specification then
2497 if Present (Private_Declarations (S)) then
2498 Decls := Private_Declarations (S);
2500 Decls := Visible_Declarations (S);
2503 Decls := Declarations (S);
2509 end May_Need_Implicit_Body;
2511 ----------------------
2512 -- New_Private_Type --
2513 ----------------------
2515 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2517 -- For other than Ada 2012, enter the name in the current scope
2519 if Ada_Version < Ada_2012 then
2522 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2523 -- there may be an incomplete previous view.
2529 Prev := Find_Type_Name (N);
2530 pragma Assert (Prev = Id
2531 or else (Ekind (Prev) = E_Incomplete_Type
2532 and then Present (Full_View (Prev))
2533 and then Full_View (Prev) = Id));
2537 if Limited_Present (Def) then
2538 Set_Ekind (Id, E_Limited_Private_Type);
2540 Set_Ekind (Id, E_Private_Type);
2544 Set_Has_Delayed_Freeze (Id);
2545 Set_Is_First_Subtype (Id);
2546 Init_Size_Align (Id);
2548 Set_Is_Constrained (Id,
2549 No (Discriminant_Specifications (N))
2550 and then not Unknown_Discriminants_Present (N));
2552 -- Set tagged flag before processing discriminants, to catch illegal
2555 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2557 Set_Discriminant_Constraint (Id, No_Elist);
2558 Set_Stored_Constraint (Id, No_Elist);
2560 if Present (Discriminant_Specifications (N)) then
2562 Process_Discriminants (N);
2565 elsif Unknown_Discriminants_Present (N) then
2566 Set_Has_Unknown_Discriminants (Id);
2569 Set_Private_Dependents (Id, New_Elmt_List);
2571 if Tagged_Present (Def) then
2572 Set_Ekind (Id, E_Record_Type_With_Private);
2573 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2574 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2575 Set_Is_Limited_Record (Id, Limited_Present (Def));
2576 Set_Has_Delayed_Freeze (Id, True);
2578 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2580 if Is_RTE (Id, RE_Timing_Event) then
2581 Set_Has_Timing_Event (Id);
2584 -- Create a class-wide type with the same attributes
2586 Make_Class_Wide_Type (Id);
2588 elsif Abstract_Present (Def) then
2589 Error_Msg_N ("only a tagged type can be abstract", N);
2591 end New_Private_Type;
2593 ---------------------------------
2594 -- Requires_Completion_In_Body --
2595 ---------------------------------
2597 function Requires_Completion_In_Body
2599 Pack_Id : Entity_Id;
2600 Do_Abstract_States : Boolean := False) return Boolean
2603 -- Always ignore child units. Child units get added to the entity list
2604 -- of a parent unit, but are not original entities of the parent, and
2605 -- so do not affect whether the parent needs a body.
2607 if Is_Child_Unit (Id) then
2610 -- Ignore formal packages and their renamings
2612 elsif Ekind (Id) = E_Package
2613 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2614 N_Formal_Package_Declaration
2618 -- Otherwise test to see if entity requires a completion. Note that
2619 -- subprogram entities whose declaration does not come from source are
2620 -- ignored here on the basis that we assume the expander will provide an
2621 -- implicit completion at some point.
2623 elsif (Is_Overloadable (Id)
2624 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2625 and then not Is_Abstract_Subprogram (Id)
2626 and then not Has_Completion (Id)
2627 and then Comes_From_Source (Parent (Id)))
2630 (Ekind (Id) = E_Package
2631 and then Id /= Pack_Id
2632 and then not Has_Completion (Id)
2633 and then Unit_Requires_Body (Id, Do_Abstract_States))
2636 (Ekind (Id) = E_Incomplete_Type
2637 and then No (Full_View (Id))
2638 and then not Is_Generic_Type (Id))
2641 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2642 and then not Has_Completion (Id))
2645 (Ekind (Id) = E_Generic_Package
2646 and then Id /= Pack_Id
2647 and then not Has_Completion (Id)
2648 and then Unit_Requires_Body (Id, Do_Abstract_States))
2651 (Is_Generic_Subprogram (Id)
2652 and then not Has_Completion (Id))
2656 -- Otherwise the entity does not require completion in a package body
2661 end Requires_Completion_In_Body;
2663 ----------------------------
2664 -- Uninstall_Declarations --
2665 ----------------------------
2667 procedure Uninstall_Declarations (P : Entity_Id) is
2668 Decl : constant Node_Id := Unit_Declaration_Node (P);
2672 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2673 -- Copy to the private declaration the attributes of the full view that
2674 -- need to be available for the partial view also.
2676 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2677 -- When the full view of a private type is made unavailable, we do the
2678 -- same for its private dependents under proper visibility conditions.
2679 -- When compiling a child unit this needs to be done recursively.
2681 function Type_In_Use (T : Entity_Id) return Boolean;
2682 -- Check whether type or base type appear in an active use_type clause
2684 ------------------------------
2685 -- Preserve_Full_Attributes --
2686 ------------------------------
2688 procedure Preserve_Full_Attributes
2692 Full_Base : constant Entity_Id := Base_Type (Full);
2693 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2696 Set_Size_Info (Priv, Full);
2697 Set_RM_Size (Priv, RM_Size (Full));
2698 Set_Size_Known_At_Compile_Time
2699 (Priv, Size_Known_At_Compile_Time (Full));
2700 Set_Is_Volatile (Priv, Is_Volatile (Full));
2701 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2702 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2703 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2704 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2705 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2706 Set_Has_Pragma_Unreferenced_Objects
2707 (Priv, Has_Pragma_Unreferenced_Objects
2709 if Is_Unchecked_Union (Full) then
2710 Set_Is_Unchecked_Union (Base_Type (Priv));
2712 -- Why is atomic not copied here ???
2714 if Referenced (Full) then
2715 Set_Referenced (Priv);
2718 if Priv_Is_Base_Type then
2719 Set_Is_Controlled_Active
2720 (Priv, Is_Controlled_Active (Full_Base));
2721 Set_Finalize_Storage_Only
2722 (Priv, Finalize_Storage_Only (Full_Base));
2723 Set_Has_Controlled_Component
2724 (Priv, Has_Controlled_Component (Full_Base));
2726 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2729 -- As explained in Freeze_Entity, private types are required to point
2730 -- to the same freeze node as their corresponding full view, if any.
2731 -- But we ought not to overwrite a node already inserted in the tree.
2734 (Serious_Errors_Detected /= 0
2735 or else No (Freeze_Node (Priv))
2736 or else No (Parent (Freeze_Node (Priv)))
2737 or else Freeze_Node (Priv) = Freeze_Node (Full));
2739 Set_Freeze_Node (Priv, Freeze_Node (Full));
2741 -- Propagate Default_Initial_Condition-related attributes from the
2742 -- base type of the full view to the full view and vice versa. This
2743 -- may seem strange, but is necessary depending on which type
2744 -- triggered the generation of the DIC procedure body. As a result,
2745 -- both the full view and its base type carry the same DIC-related
2748 Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2749 Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2751 -- Propagate Default_Initial_Condition-related attributes from the
2752 -- full view to the private view.
2754 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2756 -- Propagate invariant-related attributes from the base type of the
2757 -- full view to the full view and vice versa. This may seem strange,
2758 -- but is necessary depending on which type triggered the generation
2759 -- of the invariant procedure body. As a result, both the full view
2760 -- and its base type carry the same invariant-related information.
2762 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2763 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2765 -- Propagate invariant-related attributes from the full view to the
2768 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2770 if Is_Tagged_Type (Priv)
2771 and then Is_Tagged_Type (Full)
2772 and then not Error_Posted (Full)
2774 if Is_Tagged_Type (Priv) then
2776 -- If the type is tagged, the tag itself must be available on
2777 -- the partial view, for expansion purposes.
2779 Set_First_Entity (Priv, First_Entity (Full));
2781 -- If there are discriminants in the partial view, these remain
2782 -- visible. Otherwise only the tag itself is visible, and there
2783 -- are no nameable components in the partial view.
2785 if No (Last_Entity (Priv)) then
2786 Set_Last_Entity (Priv, First_Entity (Priv));
2790 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2792 if Has_Discriminants (Full) then
2793 Set_Discriminant_Constraint (Priv,
2794 Discriminant_Constraint (Full));
2797 end Preserve_Full_Attributes;
2799 -----------------------------
2800 -- Swap_Private_Dependents --
2801 -----------------------------
2803 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2807 Priv_Elmt : Elmt_Id;
2811 Priv_Elmt := First_Elmt (Priv_Deps);
2812 while Present (Priv_Elmt) loop
2813 Priv := Node (Priv_Elmt);
2815 -- Before we do the swap, we verify the presence of the Full_View
2816 -- field, which may be empty due to a swap by a previous call to
2817 -- End_Package_Scope (e.g. from the freezing mechanism).
2819 if Present (Full_View (Priv)) then
2820 if Is_Private_Type (Priv) then
2821 Cunit := Cunit_Entity (Current_Sem_Unit);
2822 Deps := Private_Dependents (Priv);
2829 or else not In_Open_Scopes (Scope (Priv))
2831 Set_Is_Immediately_Visible (Priv, False);
2834 if Is_Visible_Dependent (Priv) then
2835 Preserve_Full_Attributes (Priv, Full_View (Priv));
2836 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2837 Exchange_Declarations (Priv);
2839 -- Recurse for child units, except in generic child units,
2840 -- which unfortunately handle private_dependents separately.
2841 -- Note that the current unit may not have been analyzed,
2842 -- for example a package body, so we cannot rely solely on
2843 -- the Is_Child_Unit flag, but that's only an optimization.
2846 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2847 and then not Is_Empty_Elmt_List (Deps)
2848 and then not Inside_A_Generic
2850 Swap_Private_Dependents (Deps);
2855 Next_Elmt (Priv_Elmt);
2857 end Swap_Private_Dependents;
2863 function Type_In_Use (T : Entity_Id) return Boolean is
2865 return Scope (Base_Type (T)) = P
2866 and then (In_Use (T) or else In_Use (Base_Type (T)));
2869 -- Start of processing for Uninstall_Declarations
2872 Id := First_Entity (P);
2873 while Present (Id) and then Id /= First_Private_Entity (P) loop
2874 if Debug_Flag_E then
2875 Write_Str ("unlinking visible entity ");
2876 Write_Int (Int (Id));
2880 -- On exit from the package scope, we must preserve the visibility
2881 -- established by use clauses in the current scope. Two cases:
2883 -- a) If the entity is an operator, it may be a primitive operator of
2884 -- a type for which there is a visible use-type clause.
2886 -- b) For other entities, their use-visibility is determined by a
2887 -- visible use clause for the package itself or a use-all-type clause
2888 -- applied directly to the entity's type. For a generic instance,
2889 -- the instantiation of the formals appears in the visible part,
2890 -- but the formals are private and remain so.
2892 if Ekind (Id) = E_Function
2893 and then Is_Operator_Symbol_Name (Chars (Id))
2894 and then not Is_Hidden (Id)
2895 and then not Error_Posted (Id)
2897 Set_Is_Potentially_Use_Visible (Id,
2899 or else Type_In_Use (Etype (Id))
2900 or else Type_In_Use (Etype (First_Formal (Id)))
2901 or else (Present (Next_Formal (First_Formal (Id)))
2904 (Etype (Next_Formal (First_Formal (Id))))));
2906 if In_Use (P) and then not Is_Hidden (Id) then
2908 -- A child unit of a use-visible package remains use-visible
2909 -- only if it is itself a visible child unit. Otherwise it
2910 -- would remain visible in other contexts where P is use-
2911 -- visible, because once compiled it stays in the entity list
2912 -- of its parent unit.
2914 if Is_Child_Unit (Id) then
2915 Set_Is_Potentially_Use_Visible
2916 (Id, Is_Visible_Lib_Unit (Id));
2918 Set_Is_Potentially_Use_Visible (Id);
2921 -- We need to avoid incorrectly marking enumeration literals as
2922 -- non-visible when a visible use-all-type clause is in effect.
2924 elsif Type_In_Use (Etype (Id))
2925 and then Nkind (Current_Use_Clause (Etype (Id))) =
2927 and then All_Present (Current_Use_Clause (Etype (Id)))
2932 Set_Is_Potentially_Use_Visible (Id, False);
2936 -- Local entities are not immediately visible outside of the package
2938 Set_Is_Immediately_Visible (Id, False);
2940 -- If this is a private type with a full view (for example a local
2941 -- subtype of a private type declared elsewhere), ensure that the
2942 -- full view is also removed from visibility: it may be exposed when
2943 -- swapping views in an instantiation. Similarly, ensure that the
2944 -- use-visibility is properly set on both views.
2946 if Is_Type (Id) and then Present (Full_View (Id)) then
2947 Set_Is_Immediately_Visible (Full_View (Id), False);
2948 Set_Is_Potentially_Use_Visible (Full_View (Id),
2949 Is_Potentially_Use_Visible (Id));
2952 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2953 Check_Abstract_Overriding (Id);
2954 Check_Conventions (Id);
2957 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2958 and then No (Full_View (Id))
2959 and then not Is_Generic_Type (Id)
2960 and then not Is_Derived_Type (Id)
2962 Error_Msg_N ("missing full declaration for private type&", Id);
2964 elsif Ekind (Id) = E_Record_Type_With_Private
2965 and then not Is_Generic_Type (Id)
2966 and then No (Full_View (Id))
2968 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2969 Error_Msg_N ("missing full declaration for private type&", Id);
2972 ("missing full declaration for private extension", Id);
2975 -- Case of constant, check for deferred constant declaration with
2976 -- no full view. Likely just a matter of a missing expression, or
2977 -- accidental use of the keyword constant.
2979 elsif Ekind (Id) = E_Constant
2981 -- OK if constant value present
2983 and then No (Constant_Value (Id))
2985 -- OK if full view present
2987 and then No (Full_View (Id))
2989 -- OK if imported, since that provides the completion
2991 and then not Is_Imported (Id)
2993 -- OK if object declaration replaced by renaming declaration as
2994 -- a result of OK_To_Rename processing (e.g. for concatenation)
2996 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2998 -- OK if object declaration with the No_Initialization flag set
3000 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3001 and then No_Initialization (Parent (Id)))
3003 -- If no private declaration is present, we assume the user did
3004 -- not intend a deferred constant declaration and the problem
3005 -- is simply that the initializing expression is missing.
3007 if not Has_Private_Declaration (Etype (Id)) then
3009 -- We assume that the user did not intend a deferred constant
3010 -- declaration, and the expression is just missing.
3013 ("constant declaration requires initialization expression",
3016 if Is_Limited_Type (Etype (Id)) then
3018 ("\if variable intended, remove CONSTANT from declaration",
3022 -- Otherwise if a private declaration is present, then we are
3023 -- missing the full declaration for the deferred constant.
3027 ("missing full declaration for deferred constant (RM 7.4)",
3030 if Is_Limited_Type (Etype (Id)) then
3032 ("\if variable intended, remove CONSTANT from declaration",
3041 -- If the specification was installed as the parent of a public child
3042 -- unit, the private declarations were not installed, and there is
3045 if not In_Private_Part (P) then
3048 Set_In_Private_Part (P, False);
3051 -- Make private entities invisible and exchange full and private
3052 -- declarations for private types. Id is now the first private entity
3055 while Present (Id) loop
3056 if Debug_Flag_E then
3057 Write_Str ("unlinking private entity ");
3058 Write_Int (Int (Id));
3062 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3063 Check_Abstract_Overriding (Id);
3064 Check_Conventions (Id);
3067 Set_Is_Immediately_Visible (Id, False);
3069 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3070 Full := Full_View (Id);
3072 -- If the partial view is not declared in the visible part of the
3073 -- package (as is the case when it is a type derived from some
3074 -- other private type in the private part of the current package),
3075 -- no exchange takes place.
3078 or else List_Containing (Parent (Id)) /=
3079 Visible_Declarations (Specification (Decl))
3084 -- The entry in the private part points to the full declaration,
3085 -- which is currently visible. Exchange them so only the private
3086 -- type declaration remains accessible, and link private and full
3087 -- declaration in the opposite direction. Before the actual
3088 -- exchange, we copy back attributes of the full view that must
3089 -- be available to the partial view too.
3091 Preserve_Full_Attributes (Id, Full);
3093 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3095 -- The following test may be redundant, as this is already
3096 -- diagnosed in sem_ch3. ???
3098 if not Is_Definite_Subtype (Full)
3099 and then Is_Definite_Subtype (Id)
3101 Error_Msg_Sloc := Sloc (Parent (Id));
3103 ("full view of& not compatible with declaration#", Full, Id);
3106 -- Swap out the subtypes and derived types of Id that
3107 -- were compiled in this scope, or installed previously
3108 -- by Install_Private_Declarations.
3110 Swap_Private_Dependents (Private_Dependents (Id));
3112 -- Now restore the type itself to its private view
3114 Exchange_Declarations (Id);
3116 -- If we have installed an underlying full view for a type derived
3117 -- from a private type in a child unit, restore the proper views
3118 -- of private and full view. See corresponding code in
3119 -- Install_Private_Declarations.
3121 -- After the exchange, Full denotes the private type in the
3122 -- visible part of the package.
3124 if Is_Private_Base_Type (Full)
3125 and then Present (Full_View (Full))
3126 and then Present (Underlying_Full_View (Full))
3127 and then In_Package_Body (Current_Scope)
3129 Set_Full_View (Full, Underlying_Full_View (Full));
3130 Set_Underlying_Full_View (Full, Empty);
3133 elsif Ekind (Id) = E_Incomplete_Type
3134 and then Comes_From_Source (Id)
3135 and then No (Full_View (Id))
3137 -- Mark Taft amendment types. Verify that there are no primitive
3138 -- operations declared for the type (3.10.1(9)).
3140 Set_Has_Completion_In_Body (Id);
3147 Elmt := First_Elmt (Private_Dependents (Id));
3148 while Present (Elmt) loop
3149 Subp := Node (Elmt);
3151 -- Is_Primitive is tested because there can be cases where
3152 -- nonprimitive subprograms (in nested packages) are added
3153 -- to the Private_Dependents list.
3155 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3157 ("type& must be completed in the private part",
3160 -- The result type of an access-to-function type cannot be a
3161 -- Taft-amendment type, unless the version is Ada 2012 or
3162 -- later (see AI05-151).
3164 elsif Ada_Version < Ada_2012
3165 and then Ekind (Subp) = E_Subprogram_Type
3167 if Etype (Subp) = Id
3169 (Is_Class_Wide_Type (Etype (Subp))
3170 and then Etype (Etype (Subp)) = Id)
3173 ("type& must be completed in the private part",
3174 Associated_Node_For_Itype (Subp), Id);
3182 elsif not Is_Child_Unit (Id)
3183 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3186 Set_Is_Potentially_Use_Visible (Id, False);
3192 end Uninstall_Declarations;
3194 ------------------------
3195 -- Unit_Requires_Body --
3196 ------------------------
3198 function Unit_Requires_Body
3199 (Pack_Id : Entity_Id;
3200 Do_Abstract_States : Boolean := False) return Boolean
3204 Requires_Body : Boolean := False;
3205 -- Flag set when the unit has at least one construct that requires
3206 -- completion in a body.
3209 -- Imported entity never requires body. Right now, only subprograms can
3210 -- be imported, but perhaps in the future we will allow import of
3213 if Is_Imported (Pack_Id) then
3216 -- Body required if library package with pragma Elaborate_Body
3218 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3221 -- Body required if subprogram
3223 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3226 -- Treat a block as requiring a body
3228 elsif Ekind (Pack_Id) = E_Block then
3231 elsif Ekind (Pack_Id) = E_Package
3232 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3233 and then Present (Generic_Parent (Parent (Pack_Id)))
3236 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3238 if Has_Pragma_Elaborate_Body (G_P) then
3244 -- Traverse the entity chain of the package and look for constructs that
3245 -- require a completion in a body.
3247 E := First_Entity (Pack_Id);
3248 while Present (E) loop
3250 -- Skip abstract states because their completion depends on several
3251 -- criteria (see below).
3253 if Ekind (E) = E_Abstract_State then
3256 elsif Requires_Completion_In_Body
3257 (E, Pack_Id, Do_Abstract_States)
3259 Requires_Body := True;
3266 -- A [generic] package that defines at least one non-null abstract state
3267 -- requires a completion only when at least one other construct requires
3268 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3269 -- performed if the caller requests this behavior.
3271 if Do_Abstract_States
3272 and then Is_Package_Or_Generic_Package (Pack_Id)
3273 and then Has_Non_Null_Abstract_State (Pack_Id)
3274 and then Requires_Body
3279 return Requires_Body;
3280 end Unit_Requires_Body;
3282 -----------------------------
3283 -- Unit_Requires_Body_Info --
3284 -----------------------------
3286 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3290 -- An imported entity never requires body. Right now, only subprograms
3291 -- can be imported, but perhaps in the future we will allow import of
3294 if Is_Imported (Pack_Id) then
3297 -- Body required if library package with pragma Elaborate_Body
3299 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3300 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3302 -- Body required if subprogram
3304 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3305 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3307 -- Body required if generic parent has Elaborate_Body
3309 elsif Ekind (Pack_Id) = E_Package
3310 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3311 and then Present (Generic_Parent (Parent (Pack_Id)))
3314 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3316 if Has_Pragma_Elaborate_Body (G_P) then
3318 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3323 -- A [generic] package that introduces at least one non-null abstract
3324 -- state requires completion. However, there is a separate rule that
3325 -- requires that such a package have a reason other than this for a
3326 -- body being required (if necessary a pragma Elaborate_Body must be
3327 -- provided). If Ignore_Abstract_State is True, we don't do this check
3328 -- (so we can use Unit_Requires_Body to check for some other reason).
3330 elsif Is_Package_Or_Generic_Package (Pack_Id)
3331 and then Present (Abstract_States (Pack_Id))
3332 and then not Is_Null_State
3333 (Node (First_Elmt (Abstract_States (Pack_Id))))
3336 ("info: & requires body (non-null abstract state aspect)?Y?",
3340 -- Otherwise search entity chain for entity requiring completion
3342 E := First_Entity (Pack_Id);
3343 while Present (E) loop
3344 if Requires_Completion_In_Body (E, Pack_Id) then
3345 Error_Msg_Node_2 := E;
3347 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3352 end Unit_Requires_Body_Info;