1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 Restrict; use Restrict;
51 with Rtsfind; use Rtsfind;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch10; use Sem_Ch10;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
71 with Uintp; use Uintp;
73 package body Sem_Ch7 is
75 -----------------------------------
76 -- Handling private declarations --
77 -----------------------------------
79 -- The principle that each entity has a single defining occurrence clashes
80 -- with the presence of two separate definitions for private types: the
81 -- first is the private type declaration, and the second is the full type
82 -- declaration. It is important that all references to the type point to
83 -- the same defining occurrence, namely the first one. To enforce the two
84 -- separate views of the entity, the corresponding information is swapped
85 -- between the two declarations. Outside of the package, the defining
86 -- occurrence only contains the private declaration information, while in
87 -- the private part and the body of the package the defining occurrence
88 -- contains the full declaration. To simplify the swap, the defining
89 -- occurrence that currently holds the private declaration points to the
90 -- full declaration. During semantic processing the defining occurrence
91 -- also points to a list of private dependents, that is to say access types
92 -- or composite types whose designated types or component types are
93 -- subtypes or derived types of the private type in question. After the
94 -- full declaration has been seen, the private dependents are updated to
95 -- indicate that they have full definitions.
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Analyze_Package_Body_Helper (N : Node_Id);
102 -- Does all the real work of Analyze_Package_Body
104 procedure Check_Anonymous_Access_Types
105 (Spec_Id : Entity_Id;
107 -- If the spec of a package has a limited_with_clause, it may declare
108 -- anonymous access types whose designated type is a limited view, such an
109 -- anonymous access return type for a function. This access type cannot be
110 -- elaborated in the spec itself, but it may need an itype reference if it
111 -- is used within a nested scope. In that case the itype reference is
112 -- created at the beginning of the corresponding package body and inserted
113 -- before other body declarations.
115 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
116 -- Called upon entering the private part of a public child package and the
117 -- body of a nested package, to potentially declare certain inherited
118 -- subprograms that were inherited by types in the visible part, but whose
119 -- declaration was deferred because the parent operation was private and
120 -- not visible at that point. These subprograms are located by traversing
121 -- the visible part declarations looking for non-private type extensions
122 -- and then examining each of the primitive operations of such types to
123 -- find those that were inherited but declared with a special internal
124 -- name. Each such operation is now declared as an operation with a normal
125 -- name (using the name of the parent operation) and replaces the previous
126 -- implicit operation in the primitive operations list of the type. If the
127 -- inherited private operation has been overridden, then it's replaced by
128 -- the overriding operation.
130 procedure Install_Package_Entity (Id : Entity_Id);
131 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
132 -- one entity on its visibility chain, and recurses on the visible part if
133 -- the entity is an inner package.
135 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
136 -- True for a private type that is not a subtype
138 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
139 -- If the private dependent is a private type whose full view is derived
140 -- from the parent type, its full properties are revealed only if we are in
141 -- the immediate scope of the private dependent. Should this predicate be
142 -- tightened further???
144 function Requires_Completion_In_Body
147 Do_Abstract_States : Boolean := False) return Boolean;
148 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
149 -- Determine whether entity Id declared in package spec Pack_Id requires
150 -- completion in a package body. Flag Do_Abstract_Stats should be set when
151 -- abstract states are to be considered in the completion test.
153 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
154 -- Outputs info messages showing why package Pack_Id requires a body. The
155 -- caller has checked that the switch requesting this information is set,
156 -- and that the package does indeed require a body.
158 --------------------------
159 -- Analyze_Package_Body --
160 --------------------------
162 procedure Analyze_Package_Body (N : Node_Id) is
163 Loc : constant Source_Ptr := Sloc (N);
167 Write_Str ("==> package body ");
168 Write_Name (Chars (Defining_Entity (N)));
169 Write_Str (" from ");
170 Write_Location (Loc);
175 -- The real work is split out into the helper, so it can do "return;"
176 -- without skipping the debug output.
178 Analyze_Package_Body_Helper (N);
182 Write_Str ("<== package body ");
183 Write_Name (Chars (Defining_Entity (N)));
184 Write_Str (" from ");
185 Write_Location (Loc);
188 end Analyze_Package_Body;
190 ---------------------------------
191 -- Analyze_Package_Body_Helper --
192 ---------------------------------
194 -- WARNING: This routine manages Ghost regions. Return statements must be
195 -- replaced by gotos which jump to the end of the routine and restore the
198 procedure Analyze_Package_Body_Helper (N : Node_Id) is
199 procedure Hide_Public_Entities (Decls : List_Id);
200 -- Attempt to hide all public entities found in declarative list Decls
201 -- by resetting their Is_Public flag to False depending on whether the
202 -- entities are not referenced by inlined or generic bodies. This kind
203 -- of processing is a conservative approximation and may still leave
204 -- certain entities externally visible.
206 procedure Install_Composite_Operations (P : Entity_Id);
207 -- Composite types declared in the current scope may depend on types
208 -- that were private at the point of declaration, and whose full view
209 -- is now in scope. Indicate that the corresponding operations on the
210 -- composite type are available.
212 --------------------------
213 -- Hide_Public_Entities --
214 --------------------------
216 procedure Hide_Public_Entities (Decls : List_Id) is
217 function Contains_Subprograms_Refs (N : Node_Id) return Boolean;
218 -- Subsidiary to routine Has_Referencer. Determine whether a node
219 -- contains a reference to a subprogram.
220 -- WARNING: this is a very expensive routine as it performs a full
223 function Has_Referencer
225 Top_Level : Boolean := False) return Boolean;
226 -- A "referencer" is a construct which may reference a previous
227 -- declaration. Examine all declarations in list Decls in reverse
228 -- and determine whether once such referencer exists. All entities
229 -- in the range Last (Decls) .. Referencer are hidden from external
232 -------------------------------
233 -- Contains_Subprograms_Refs --
234 -------------------------------
236 function Contains_Subprograms_Refs (N : Node_Id) return Boolean is
237 Reference_Seen : Boolean := False;
239 function Is_Subprogram_Ref (N : Node_Id) return Traverse_Result;
240 -- Determine whether a node denotes a reference to a subprogram
242 -----------------------
243 -- Is_Subprogram_Ref --
244 -----------------------
246 function Is_Subprogram_Ref
247 (N : Node_Id) return Traverse_Result
252 -- Detect a reference of the form
255 if Nkind (N) in N_Subprogram_Call
256 and then Is_Entity_Name (Name (N))
258 Reference_Seen := True;
261 -- Detect a reference of the form
262 -- Subp'Some_Attribute
264 elsif Nkind (N) = N_Attribute_Reference
265 and then Is_Entity_Name (Prefix (N))
266 and then Present (Entity (Prefix (N)))
267 and then Is_Subprogram (Entity (Prefix (N)))
269 Reference_Seen := True;
272 -- Constants can be substituted by their value in gigi, which
273 -- may contain a reference, so be conservative for them.
275 elsif Is_Entity_Name (N)
276 and then Present (Entity (N))
277 and then Ekind (Entity (N)) = E_Constant
279 Val := Constant_Value (Entity (N));
282 and then not Compile_Time_Known_Value (Val)
284 Reference_Seen := True;
290 end Is_Subprogram_Ref;
292 procedure Find_Subprograms_Ref is
293 new Traverse_Proc (Is_Subprogram_Ref);
295 -- Start of processing for Contains_Subprograms_Refs
298 Find_Subprograms_Ref (N);
300 return Reference_Seen;
301 end Contains_Subprograms_Refs;
307 function Has_Referencer
309 Top_Level : Boolean := False) return Boolean
315 Has_Non_Subprograms_Referencer : Boolean := False;
316 -- Flag set if a subprogram body was detected as a referencer but
317 -- does not contain references to other subprograms. In this case,
318 -- if we still are top level, we do not return True immediately,
319 -- but keep hiding subprograms from external visibility.
326 -- Examine all declarations in reverse order, hiding all entities
327 -- from external visibility until a referencer has been found. The
328 -- algorithm recurses into nested packages.
330 Decl := Last (Decls);
331 while Present (Decl) loop
333 -- A stub is always considered a referencer
335 if Nkind (Decl) in N_Body_Stub then
338 -- Package declaration
340 elsif Nkind (Decl) = N_Package_Declaration then
341 Spec := Specification (Decl);
343 -- Inspect the declarations of a non-generic package to try
344 -- and hide more entities from external visibility.
346 if not Is_Generic_Unit (Defining_Entity (Spec)) then
347 if Has_Referencer (Private_Declarations (Spec))
348 or else Has_Referencer (Visible_Declarations (Spec))
356 elsif Nkind (Decl) = N_Package_Body
357 and then Present (Corresponding_Spec (Decl))
359 Decl_Id := Corresponding_Spec (Decl);
361 -- A generic package body is a referencer. It would seem
362 -- that we only have to consider generics that can be
363 -- exported, i.e. where the corresponding spec is the
364 -- spec of the current package, but because of nested
365 -- instantiations, a fully private generic body may export
366 -- other private body entities. Furthermore, regardless of
367 -- whether there was a previous inlined subprogram, (an
368 -- instantiation of) the generic package may reference any
369 -- entity declared before it.
371 if Is_Generic_Unit (Decl_Id) then
374 -- Inspect the declarations of a non-generic package body to
375 -- try and hide more entities from external visibility.
377 elsif Has_Referencer (Declarations (Decl)) then
383 elsif Nkind (Decl) = N_Subprogram_Body then
384 if Present (Corresponding_Spec (Decl)) then
385 Decl_Id := Corresponding_Spec (Decl);
387 -- A generic subprogram body acts as a referencer
389 if Is_Generic_Unit (Decl_Id) then
393 -- An inlined subprogram body acts as a referencer
395 -- Note that we test Has_Pragma_Inline here in addition
396 -- to Is_Inlined. We are doing this for a client, since
397 -- we are computing which entities should be public, and
398 -- it is the client who will decide if actual inlining
399 -- should occur, so we need to catch all cases where the
400 -- subprogram may be inlined by the client.
402 if Is_Inlined (Decl_Id)
403 or else Has_Pragma_Inline (Decl_Id)
405 -- Inspect the statements of the subprogram body
406 -- to determine whether the body references other
410 and then not Contains_Subprograms_Refs (Decl)
412 Has_Non_Subprograms_Referencer := True;
418 -- Otherwise this is a stand alone subprogram body
421 Decl_Id := Defining_Entity (Decl);
423 -- An inlined body acts as a referencer, see above. Note
424 -- that an inlined subprogram remains Is_Public as gigi
425 -- requires the flag to be set.
427 if Is_Inlined (Decl_Id)
428 or else Has_Pragma_Inline (Decl_Id)
431 and then not Contains_Subprograms_Refs (Decl)
433 Has_Non_Subprograms_Referencer := True;
438 Set_Is_Public (Decl_Id, False);
442 -- Exceptions, objects and renamings do not need to be public
443 -- if they are not followed by a construct which can reference
444 -- and export them. The Is_Public flag is reset on top level
445 -- entities only as anything nested is local to its context.
446 -- Likewise for subprograms, but we work harder for them as
447 -- their visibility can have a significant impact on inlining
448 -- decisions in the back end.
450 elsif Nkind_In (Decl, N_Exception_Declaration,
451 N_Object_Declaration,
452 N_Object_Renaming_Declaration,
453 N_Subprogram_Declaration,
454 N_Subprogram_Renaming_Declaration)
456 Decl_Id := Defining_Entity (Decl);
459 and then not Is_Imported (Decl_Id)
460 and then not Is_Exported (Decl_Id)
461 and then No (Interface_Name (Decl_Id))
463 (not Has_Non_Subprograms_Referencer
464 or else Nkind (Decl) = N_Subprogram_Declaration)
466 Set_Is_Public (Decl_Id, False);
473 return Has_Non_Subprograms_Referencer;
478 Discard : Boolean := True;
479 pragma Unreferenced (Discard);
481 -- Start of processing for Hide_Public_Entities
484 -- The algorithm examines the top level declarations of a package
485 -- body in reverse looking for a construct that may export entities
486 -- declared prior to it. If such a scenario is encountered, then all
487 -- entities in the range Last (Decls) .. construct are hidden from
488 -- external visibility. Consider:
496 -- package body Pack is
497 -- External_Obj : ...; -- (1)
499 -- package body Gen is -- (2)
500 -- ... External_Obj ... -- (3)
503 -- Local_Obj : ...; -- (4)
506 -- In this example Local_Obj (4) must not be externally visible as
507 -- it cannot be exported by anything in Pack. The body of generic
508 -- package Gen (2) on the other hand acts as a "referencer" and may
509 -- export anything declared before it. Since the compiler does not
510 -- perform flow analysis, it is not possible to determine precisely
511 -- which entities will be exported when Gen is instantiated. In the
512 -- example above External_Obj (1) is exported at (3), but this may
513 -- not always be the case. The algorithm takes a conservative stance
514 -- and leaves entity External_Obj public.
516 Discard := Has_Referencer (Decls, Top_Level => True);
517 end Hide_Public_Entities;
519 ----------------------------------
520 -- Install_Composite_Operations --
521 ----------------------------------
523 procedure Install_Composite_Operations (P : Entity_Id) is
527 Id := First_Entity (P);
528 while Present (Id) loop
530 and then (Is_Limited_Composite (Id)
531 or else Is_Private_Composite (Id))
532 and then No (Private_Component (Id))
534 Set_Is_Limited_Composite (Id, False);
535 Set_Is_Private_Composite (Id, False);
540 end Install_Composite_Operations;
544 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
545 Saved_ISMP : constant Boolean :=
546 Ignore_SPARK_Mode_Pragmas_In_Instance;
547 -- Save the Ghost and SPARK mode-related data to restore on exit
551 Last_Spec_Entity : Entity_Id;
556 -- Start of processing for Analyze_Package_Body_Helper
559 -- Find corresponding package specification, and establish the current
560 -- scope. The visible defining entity for the package is the defining
561 -- occurrence in the spec. On exit from the package body, all body
562 -- declarations are attached to the defining entity for the body, but
563 -- the later is never used for name resolution. In this fashion there
564 -- is only one visible entity that denotes the package.
566 -- Set Body_Id. Note that this will be reset to point to the generic
567 -- copy later on in the generic case.
569 Body_Id := Defining_Entity (N);
571 -- Body is body of package instantiation. Corresponding spec has already
574 if Present (Corresponding_Spec (N)) then
575 Spec_Id := Corresponding_Spec (N);
576 Pack_Decl := Unit_Declaration_Node (Spec_Id);
579 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
582 and then Is_Package_Or_Generic_Package (Spec_Id)
584 Pack_Decl := Unit_Declaration_Node (Spec_Id);
586 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
587 Error_Msg_N ("cannot supply body for package renaming", N);
590 elsif Present (Corresponding_Body (Pack_Decl)) then
591 Error_Msg_N ("redefinition of package body", N);
596 Error_Msg_N ("missing specification for package body", N);
600 if Is_Package_Or_Generic_Package (Spec_Id)
601 and then (Scope (Spec_Id) = Standard_Standard
602 or else Is_Child_Unit (Spec_Id))
603 and then not Unit_Requires_Body (Spec_Id)
605 if Ada_Version = Ada_83 then
607 ("optional package body (not allowed in Ada 95)??", N);
609 Error_Msg_N ("spec of this package does not allow a body", N);
614 -- A [generic] package body "freezes" the contract of the nearest
615 -- enclosing package body and all other contracts encountered in the
616 -- same declarative part up to and excluding the package body:
618 -- package body Nearest_Enclosing_Package
619 -- with Refined_State => (State => Constit)
623 -- package body Freezes_Enclosing_Package_Body
624 -- with Refined_State => (State_2 => Constit_2)
629 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
631 -- This ensures that any annotations referenced by the contract of a
632 -- [generic] subprogram body declared within the current package body
633 -- are available. This form of "freezing" is decoupled from the usual
634 -- Freeze_xxx mechanism because it must also work in the context of
635 -- generics where normal freezing is disabled.
637 -- Only bodies coming from source should cause this type of "freezing".
638 -- Instantiated generic bodies are excluded because their processing is
639 -- performed in a separate compilation pass which lacks enough semantic
640 -- information with respect to contract analysis. It is safe to suppress
641 -- the "freezing" of contracts in this case because this action already
642 -- took place at the end of the enclosing declarative part.
644 if Comes_From_Source (N)
645 and then not Is_Generic_Instance (Spec_Id)
647 Analyze_Previous_Contracts (N);
650 -- A package body is Ghost when the corresponding spec is Ghost. Set
651 -- the mode now to ensure that any nodes generated during analysis and
652 -- expansion are properly flagged as ignored Ghost.
654 Mark_And_Set_Ghost_Body (N, Spec_Id);
656 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
657 Style.Check_Identifier (Body_Id, Spec_Id);
659 if Is_Child_Unit (Spec_Id) then
660 if Nkind (Parent (N)) /= N_Compilation_Unit then
662 ("body of child unit& cannot be an inner package", N, Spec_Id);
665 Set_Is_Child_Unit (Body_Id);
668 -- Generic package case
670 if Ekind (Spec_Id) = E_Generic_Package then
672 -- Disable expansion and perform semantic analysis on copy. The
673 -- unannotated body will be used in all instantiations.
675 Body_Id := Defining_Entity (N);
676 Set_Ekind (Body_Id, E_Package_Body);
677 Set_Scope (Body_Id, Scope (Spec_Id));
678 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
679 Set_Body_Entity (Spec_Id, Body_Id);
680 Set_Spec_Entity (Body_Id, Spec_Id);
682 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
685 -- Once the contents of the generic copy and the template are
686 -- swapped, do the same for their respective aspect specifications.
688 Exchange_Aspects (N, New_N);
690 -- Collect all contract-related source pragmas found within the
691 -- template and attach them to the contract of the package body.
692 -- This contract is used in the capture of global references within
695 Create_Generic_Contract (N);
697 -- Update Body_Id to point to the copied node for the remainder of
700 Body_Id := Defining_Entity (N);
704 -- The Body_Id is that of the copied node in the generic case, the
705 -- current node otherwise. Note that N was rewritten above, so we must
706 -- be sure to get the latest Body_Id value.
708 Set_Ekind (Body_Id, E_Package_Body);
709 Set_Body_Entity (Spec_Id, Body_Id);
710 Set_Spec_Entity (Body_Id, Spec_Id);
712 -- Defining name for the package body is not a visible entity: Only the
713 -- defining name for the declaration is visible.
715 Set_Etype (Body_Id, Standard_Void_Type);
716 Set_Scope (Body_Id, Scope (Spec_Id));
717 Set_Corresponding_Spec (N, Spec_Id);
718 Set_Corresponding_Body (Pack_Decl, Body_Id);
720 -- The body entity is not used for semantics or code generation, but
721 -- it is attached to the entity list of the enclosing scope to simplify
722 -- the listing of back-annotations for the types it main contain.
724 if Scope (Spec_Id) /= Standard_Standard then
725 Append_Entity (Body_Id, Scope (Spec_Id));
728 -- Indicate that we are currently compiling the body of the package
730 Set_In_Package_Body (Spec_Id);
731 Set_Has_Completion (Spec_Id);
732 Last_Spec_Entity := Last_Entity (Spec_Id);
734 if Has_Aspects (N) then
735 Analyze_Aspect_Specifications (N, Body_Id);
738 Push_Scope (Spec_Id);
740 -- Set SPARK_Mode only for non-generic package
742 if Ekind (Spec_Id) = E_Package then
743 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
744 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
745 Set_SPARK_Pragma_Inherited (Body_Id);
746 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
748 -- A package body may be instantiated or inlined at a later pass.
749 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
750 -- it applied to the package spec.
752 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
753 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
757 Set_Categorization_From_Pragmas (N);
759 Install_Visible_Declarations (Spec_Id);
760 Install_Private_Declarations (Spec_Id);
761 Install_Private_With_Clauses (Spec_Id);
762 Install_Composite_Operations (Spec_Id);
764 Check_Anonymous_Access_Types (Spec_Id, N);
766 if Ekind (Spec_Id) = E_Generic_Package then
767 Set_Use (Generic_Formal_Declarations (Pack_Decl));
770 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
771 Set_Use (Private_Declarations (Specification (Pack_Decl)));
773 -- This is a nested package, so it may be necessary to declare certain
774 -- inherited subprograms that are not yet visible because the parent
775 -- type's subprograms are now visible.
777 if Ekind (Scope (Spec_Id)) = E_Package
778 and then Scope (Spec_Id) /= Standard_Standard
780 Declare_Inherited_Private_Subprograms (Spec_Id);
783 -- A package body "freezes" the contract of its initial declaration.
784 -- This analysis depends on attribute Corresponding_Spec being set. Only
785 -- bodies coming from source shuld cause this type of "freezing".
787 if Present (Declarations (N)) then
788 Analyze_Declarations (Declarations (N));
789 Inspect_Deferred_Constant_Completion (Declarations (N));
792 -- Verify that the SPARK_Mode of the body agrees with that of its spec
794 if Present (SPARK_Pragma (Body_Id)) then
795 if Present (SPARK_Aux_Pragma (Spec_Id)) then
796 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
799 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
801 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
802 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
803 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
805 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
809 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
810 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
811 Error_Msg_Sloc := Sloc (Spec_Id);
813 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
817 -- Analyze_Declarations has caused freezing of all types. Now generate
818 -- bodies for RACW primitives and stream attributes, if any.
820 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
822 -- Attach subprogram bodies to support RACWs declared in spec
824 Append_RACW_Bodies (Declarations (N), Spec_Id);
825 Analyze_List (Declarations (N));
828 HSS := Handled_Statement_Sequence (N);
830 if Present (HSS) then
831 Process_End_Label (HSS, 't', Spec_Id);
834 -- Check that elaboration code in a preelaborable package body is
835 -- empty other than null statements and labels (RM 10.2.1(6)).
837 Validate_Null_Statement_Sequence (N);
840 Validate_Categorization_Dependency (N, Spec_Id);
841 Check_Completion (Body_Id);
843 -- Generate start of body reference. Note that we do this fairly late,
844 -- because the call will use In_Extended_Main_Source_Unit as a check,
845 -- and we want to make sure that Corresponding_Stub links are set
847 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
849 -- For a generic package, collect global references and mark them on
850 -- the original body so that they are not resolved again at the point
853 if Ekind (Spec_Id) /= E_Package then
854 Save_Global_References (Original_Node (N));
858 -- The entities of the package body have so far been chained onto the
859 -- declaration chain for the spec. That's been fine while we were in the
860 -- body, since we wanted them to be visible, but now that we are leaving
861 -- the package body, they are no longer visible, so we remove them from
862 -- the entity chain of the package spec entity, and copy them to the
863 -- entity chain of the package body entity, where they will never again
866 if Present (Last_Spec_Entity) then
867 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
868 Set_Next_Entity (Last_Spec_Entity, Empty);
869 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
870 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
873 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
874 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
875 Set_First_Entity (Spec_Id, Empty);
876 Set_Last_Entity (Spec_Id, Empty);
879 End_Package_Scope (Spec_Id);
881 -- All entities declared in body are not visible
887 E := First_Entity (Body_Id);
888 while Present (E) loop
889 Set_Is_Immediately_Visible (E, False);
890 Set_Is_Potentially_Use_Visible (E, False);
893 -- Child units may appear on the entity list (e.g. if they appear
894 -- in the context of a subunit) but they are not body entities.
896 if not Is_Child_Unit (E) then
897 Set_Is_Package_Body_Entity (E);
904 Check_References (Body_Id);
906 -- For a generic unit, check that the formal parameters are referenced,
907 -- and that local variables are used, as for regular packages.
909 if Ekind (Spec_Id) = E_Generic_Package then
910 Check_References (Spec_Id);
913 -- At this point all entities of the package body are externally visible
914 -- to the linker as their Is_Public flag is set to True. This proactive
915 -- approach is necessary because an inlined or a generic body for which
916 -- code is generated in other units may need to see these entities. Cut
917 -- down the number of global symbols that do not neet public visibility
918 -- as this has two beneficial effects:
919 -- (1) It makes the compilation process more efficient.
920 -- (2) It gives the code generator more leeway to optimize within each
921 -- unit, especially subprograms.
923 -- This is done only for top-level library packages or child units as
924 -- the algorithm does a top-down traversal of the package body.
926 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
927 and then not Is_Generic_Unit (Spec_Id)
929 Hide_Public_Entities (Declarations (N));
932 -- If expander is not active, then here is where we turn off the
933 -- In_Package_Body flag, otherwise it is turned off at the end of the
934 -- corresponding expansion routine. If this is an instance body, we need
935 -- to qualify names of local entities, because the body may have been
936 -- compiled as a preliminary to another instantiation.
938 if not Expander_Active then
939 Set_In_Package_Body (Spec_Id, False);
941 if Is_Generic_Instance (Spec_Id)
942 and then Operating_Mode = Generate_Code
944 Qualify_Entity_Names (N);
948 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
949 Restore_Ghost_Mode (Saved_GM);
950 end Analyze_Package_Body_Helper;
952 ---------------------------------
953 -- Analyze_Package_Declaration --
954 ---------------------------------
956 procedure Analyze_Package_Declaration (N : Node_Id) is
957 Id : constant Node_Id := Defining_Entity (N);
959 Is_Comp_Unit : constant Boolean :=
960 Nkind (Parent (N)) = N_Compilation_Unit;
962 Body_Required : Boolean;
963 -- True when this package declaration requires a corresponding body
967 Write_Str ("==> package spec ");
968 Write_Name (Chars (Id));
969 Write_Str (" from ");
970 Write_Location (Sloc (N));
975 Generate_Definition (Id);
977 Set_Ekind (Id, E_Package);
978 Set_Etype (Id, Standard_Void_Type);
980 -- Set SPARK_Mode from context
982 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
983 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
984 Set_SPARK_Pragma_Inherited (Id);
985 Set_SPARK_Aux_Pragma_Inherited (Id);
987 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
988 -- the body of this package is instantiated or inlined later and out of
989 -- context. The body uses this attribute to restore the value of the
992 if Ignore_SPARK_Mode_Pragmas_In_Instance then
993 Set_Ignore_SPARK_Mode_Pragmas (Id);
996 -- Analyze aspect specifications immediately, since we need to recognize
997 -- things like Pure early enough to diagnose violations during analysis.
999 if Has_Aspects (N) then
1000 Analyze_Aspect_Specifications (N, Id);
1003 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1004 -- a limited-with clause of its own context. In this case the error has
1005 -- been previously notified by Analyze_Context.
1007 -- limited with Pkg; -- ERROR
1008 -- package Pkg is ...
1010 if From_Limited_With (Id) then
1016 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1017 Set_Categorization_From_Pragmas (N);
1019 Analyze (Specification (N));
1020 Validate_Categorization_Dependency (N, Id);
1022 -- Determine whether the package requires a body. Abstract states are
1023 -- intentionally ignored because they do require refinement which can
1024 -- only come in a body, but at the same time they do not force the need
1025 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1027 Body_Required := Unit_Requires_Body (Id);
1029 if not Body_Required then
1031 -- If the package spec does not require an explicit body, then there
1032 -- are not entities requiring completion in the language sense. Call
1033 -- Check_Completion now to ensure that nested package declarations
1034 -- that require an implicit body get one. (In the case where a body
1035 -- is required, Check_Completion is called at the end of the body's
1036 -- declarative part.)
1040 -- If the package spec does not require an explicit body, then all
1041 -- abstract states declared in nested packages cannot possibly get
1042 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1043 -- only when the compilation unit is the main unit to allow for
1044 -- modular SPARK analysis where packages do not necessarily have
1047 if Is_Comp_Unit then
1048 Check_State_Refinements
1050 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1054 if Is_Comp_Unit then
1056 -- Set Body_Required indication on the compilation unit node, and
1057 -- determine whether elaboration warnings may be meaningful on it.
1059 Set_Body_Required (Parent (N), Body_Required);
1061 if not Body_Required then
1062 Set_Suppress_Elaboration_Warnings (Id);
1066 End_Package_Scope (Id);
1068 -- For the declaration of a library unit that is a remote types package,
1069 -- check legality rules regarding availability of stream attributes for
1070 -- types that contain non-remote access values. This subprogram performs
1071 -- visibility tests that rely on the fact that we have exited the scope
1074 if Is_Comp_Unit then
1075 Validate_RT_RAT_Component (N);
1078 if Debug_Flag_C then
1080 Write_Str ("<== package spec ");
1081 Write_Name (Chars (Id));
1082 Write_Str (" from ");
1083 Write_Location (Sloc (N));
1086 end Analyze_Package_Declaration;
1088 -----------------------------------
1089 -- Analyze_Package_Specification --
1090 -----------------------------------
1092 -- Note that this code is shared for the analysis of generic package specs
1093 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1095 procedure Analyze_Package_Specification (N : Node_Id) is
1096 Id : constant Entity_Id := Defining_Entity (N);
1097 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1098 Vis_Decls : constant List_Id := Visible_Declarations (N);
1099 Priv_Decls : constant List_Id := Private_Declarations (N);
1102 Public_Child : Boolean;
1104 Private_With_Clauses_Installed : Boolean := False;
1105 -- In Ada 2005, private with_clauses are visible in the private part
1106 -- of a nested package, even if it appears in the public part of the
1107 -- enclosing package. This requires a separate step to install these
1108 -- private_with_clauses, and remove them at the end of the nested
1111 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1112 -- Issue an error in SPARK mode if a package specification contains
1113 -- more than one tagged type or type extension.
1115 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1116 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1117 -- Is_True_Constant) on all variables that are entities of Id, and on
1118 -- the chain whose first element is FE. A recursive call is made for all
1119 -- packages and generic packages.
1121 procedure Generate_Parent_References;
1122 -- For a child unit, generate references to parent units, for
1123 -- GPS navigation purposes.
1125 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1126 -- Child and Unit are entities of compilation units. True if Child
1127 -- is a public child of Parent as defined in 10.1.1
1129 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1130 -- Reject completion of an incomplete or private type declarations
1131 -- having a known discriminant part by an unchecked union.
1133 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1134 -- Given the package entity of a generic package instantiation or
1135 -- formal package whose corresponding generic is a child unit, installs
1136 -- the private declarations of each of the child unit's parents.
1137 -- This has to be done at the point of entering the instance package's
1138 -- private part rather than being done in Sem_Ch12.Install_Parent
1139 -- (which is where the parents' visible declarations are installed).
1141 ------------------------------------------------
1142 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1143 ------------------------------------------------
1145 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1148 procedure Check_Decls (Decls : List_Id);
1149 -- Check that either Previous is Empty and Decls does not contain
1150 -- more than one tagged type or type extension, or Previous is
1151 -- already set and Decls contains no tagged type or type extension.
1157 procedure Check_Decls (Decls : List_Id) is
1161 Decl := First (Decls);
1162 while Present (Decl) loop
1163 if Nkind (Decl) = N_Full_Type_Declaration
1164 and then Is_Tagged_Type (Defining_Identifier (Decl))
1166 if No (Previous) then
1170 Error_Msg_Sloc := Sloc (Previous);
1171 Check_SPARK_05_Restriction
1172 ("at most one tagged type or type extension allowed",
1173 "\\ previous declaration#",
1182 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1186 Check_Decls (Vis_Decls);
1188 if Present (Priv_Decls) then
1189 Check_Decls (Priv_Decls);
1191 end Check_One_Tagged_Type_Or_Extension_At_Most;
1193 ---------------------
1194 -- Clear_Constants --
1195 ---------------------
1197 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1201 -- Ignore package renamings, not interesting and they can cause self
1202 -- referential loops in the code below.
1204 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1208 -- Note: in the loop below, the check for Next_Entity pointing back
1209 -- to the package entity may seem odd, but it is needed, because a
1210 -- package can contain a renaming declaration to itself, and such
1211 -- renamings are generated automatically within package instances.
1214 while Present (E) and then E /= Id loop
1215 if Is_Assignable (E) then
1216 Set_Never_Set_In_Source (E, False);
1217 Set_Is_True_Constant (E, False);
1218 Set_Current_Value (E, Empty);
1219 Set_Is_Known_Null (E, False);
1220 Set_Last_Assignment (E, Empty);
1222 if not Can_Never_Be_Null (E) then
1223 Set_Is_Known_Non_Null (E, False);
1226 elsif Is_Package_Or_Generic_Package (E) then
1227 Clear_Constants (E, First_Entity (E));
1228 Clear_Constants (E, First_Private_Entity (E));
1233 end Clear_Constants;
1235 --------------------------------
1236 -- Generate_Parent_References --
1237 --------------------------------
1239 procedure Generate_Parent_References is
1240 Decl : constant Node_Id := Parent (N);
1243 if Id = Cunit_Entity (Main_Unit)
1244 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1246 Generate_Reference (Id, Scope (Id), 'k', False);
1248 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1251 -- If current unit is an ancestor of main unit, generate a
1252 -- reference to its own parent.
1256 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1259 if Nkind (Main_Spec) = N_Package_Body then
1260 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1263 U := Parent_Spec (Main_Spec);
1264 while Present (U) loop
1265 if U = Parent (Decl) then
1266 Generate_Reference (Id, Scope (Id), 'k', False);
1269 elsif Nkind (Unit (U)) = N_Package_Body then
1273 U := Parent_Spec (Unit (U));
1278 end Generate_Parent_References;
1280 ---------------------
1281 -- Is_Public_Child --
1282 ---------------------
1284 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1286 if not Is_Private_Descendant (Child) then
1289 if Child = Unit then
1290 return not Private_Present (
1291 Parent (Unit_Declaration_Node (Child)));
1293 return Is_Public_Child (Scope (Child), Unit);
1296 end Is_Public_Child;
1298 ----------------------------------------
1299 -- Inspect_Unchecked_Union_Completion --
1300 ----------------------------------------
1302 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1306 Decl := First (Decls);
1307 while Present (Decl) loop
1309 -- We are looking at an incomplete or private type declaration
1310 -- with a known_discriminant_part whose full view is an
1313 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1314 N_Private_Type_Declaration)
1315 and then Has_Discriminants (Defining_Identifier (Decl))
1316 and then Present (Full_View (Defining_Identifier (Decl)))
1318 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1321 ("completion of discriminated partial view "
1322 & "cannot be an unchecked union",
1323 Full_View (Defining_Identifier (Decl)));
1328 end Inspect_Unchecked_Union_Completion;
1330 -----------------------------------------
1331 -- Install_Parent_Private_Declarations --
1332 -----------------------------------------
1334 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1335 Inst_Par : Entity_Id;
1336 Gen_Par : Entity_Id;
1337 Inst_Node : Node_Id;
1340 Inst_Par := Inst_Id;
1343 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1344 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1345 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1347 if Nkind_In (Inst_Node, N_Package_Instantiation,
1348 N_Formal_Package_Declaration)
1349 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1351 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1353 if Present (Renamed_Entity (Inst_Par)) then
1354 Inst_Par := Renamed_Entity (Inst_Par);
1359 (Specification (Unit_Declaration_Node (Inst_Par)));
1361 -- Install the private declarations and private use clauses
1362 -- of a parent instance of the child instance, unless the
1363 -- parent instance private declarations have already been
1364 -- installed earlier in Analyze_Package_Specification, which
1365 -- happens when a generic child is instantiated, and the
1366 -- instance is a child of the parent instance.
1368 -- Installing the use clauses of the parent instance twice
1369 -- is both unnecessary and wrong, because it would cause the
1370 -- clauses to be chained to themselves in the use clauses
1371 -- list of the scope stack entry. That in turn would cause
1372 -- an endless loop from End_Use_Clauses upon scope exit.
1374 -- The parent is now fully visible. It may be a hidden open
1375 -- scope if we are currently compiling some child instance
1376 -- declared within it, but while the current instance is being
1377 -- compiled the parent is immediately visible. In particular
1378 -- its entities must remain visible if a stack save/restore
1379 -- takes place through a call to Rtsfind.
1381 if Present (Gen_Par) then
1382 if not In_Private_Part (Inst_Par) then
1383 Install_Private_Declarations (Inst_Par);
1384 Set_Use (Private_Declarations
1386 (Unit_Declaration_Node (Inst_Par))));
1387 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1390 -- If we've reached the end of the generic instance parents,
1391 -- then finish off by looping through the nongeneric parents
1392 -- and installing their private declarations.
1394 -- If one of the non-generic parents is itself on the scope
1395 -- stack, do not install its private declarations: they are
1396 -- installed in due time when the private part of that parent
1400 while Present (Inst_Par)
1401 and then Inst_Par /= Standard_Standard
1402 and then (not In_Open_Scopes (Inst_Par)
1403 or else not In_Private_Part (Inst_Par))
1405 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1407 not Is_Ancestor_Package
1408 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1410 Install_Private_Declarations (Inst_Par);
1412 (Private_Declarations
1414 (Unit_Declaration_Node (Inst_Par))));
1415 Inst_Par := Scope (Inst_Par);
1428 end Install_Parent_Private_Declarations;
1430 -- Start of processing for Analyze_Package_Specification
1433 if Present (Vis_Decls) then
1434 Analyze_Declarations (Vis_Decls);
1437 -- Inspect the entities defined in the package and ensure that all
1438 -- incomplete types have received full declarations. Build default
1439 -- initial condition and invariant procedures for all qualifying types.
1441 E := First_Entity (Id);
1442 while Present (E) loop
1444 -- Check on incomplete types
1446 -- AI05-0213: A formal incomplete type has no completion, and neither
1447 -- does the corresponding subtype in an instance.
1449 if Is_Incomplete_Type (E)
1450 and then No (Full_View (E))
1451 and then not Is_Generic_Type (E)
1452 and then not From_Limited_With (E)
1453 and then not Is_Generic_Actual_Type (E)
1455 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1461 if Is_Remote_Call_Interface (Id)
1462 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1464 Validate_RCI_Declarations (Id);
1467 -- Save global references in the visible declarations, before installing
1468 -- private declarations of parent unit if there is one, because the
1469 -- privacy status of types defined in the parent will change. This is
1470 -- only relevant for generic child units, but is done in all cases for
1473 if Ekind (Id) = E_Generic_Package
1474 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1477 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1478 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1481 -- Insert the freezing nodes after the visible declarations to
1482 -- ensure that we analyze its aspects; needed to ensure that
1483 -- global entities referenced in the aspects are properly handled.
1485 if Ada_Version >= Ada_2012
1486 and then Is_Non_Empty_List (Vis_Decls)
1487 and then Is_Empty_List (Priv_Decls)
1489 Insert_List_After_And_Analyze
1490 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1493 Set_Private_Declarations (Orig_Spec, Empty_List);
1494 Save_Global_References (Orig_Decl);
1495 Set_Private_Declarations (Orig_Spec, Save_Priv);
1499 -- If package is a public child unit, then make the private declarations
1500 -- of the parent visible.
1502 Public_Child := False;
1506 Pack_Decl : Node_Id;
1511 Par_Spec := Parent_Spec (Parent (N));
1513 -- If the package is formal package of an enclosing generic, it is
1514 -- transformed into a local generic declaration, and compiled to make
1515 -- its spec available. We need to retrieve the original generic to
1516 -- determine whether it is a child unit, and install its parents.
1520 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1522 Par := Entity (Name (Original_Node (Parent (N))));
1523 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1526 if Present (Par_Spec) then
1527 Generate_Parent_References;
1529 while Scope (Par) /= Standard_Standard
1530 and then Is_Public_Child (Id, Par)
1531 and then In_Open_Scopes (Par)
1533 Public_Child := True;
1535 Install_Private_Declarations (Par);
1536 Install_Private_With_Clauses (Par);
1537 Pack_Decl := Unit_Declaration_Node (Par);
1538 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1543 if Is_Compilation_Unit (Id) then
1544 Install_Private_With_Clauses (Id);
1546 -- The current compilation unit may include private with_clauses,
1547 -- which are visible in the private part of the current nested
1548 -- package, and have to be installed now. This is not done for
1549 -- nested instantiations, where the private with_clauses of the
1550 -- enclosing unit have no effect once the instantiation info is
1551 -- established and we start analyzing the package declaration.
1554 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1556 if Is_Package_Or_Generic_Package (Comp_Unit)
1557 and then not In_Private_Part (Comp_Unit)
1558 and then not In_Instance
1560 Install_Private_With_Clauses (Comp_Unit);
1561 Private_With_Clauses_Installed := True;
1566 -- If this is a package associated with a generic instance or formal
1567 -- package, then the private declarations of each of the generic's
1568 -- parents must be installed at this point.
1570 if Is_Generic_Instance (Id) then
1571 Install_Parent_Private_Declarations (Id);
1574 -- Analyze private part if present. The flag In_Private_Part is reset
1575 -- in End_Package_Scope.
1577 L := Last_Entity (Id);
1579 if Present (Priv_Decls) then
1580 Set_In_Private_Part (Id);
1582 -- Upon entering a public child's private part, it may be necessary
1583 -- to declare subprograms that were derived in the package's visible
1584 -- part but not yet made visible.
1586 if Public_Child then
1587 Declare_Inherited_Private_Subprograms (Id);
1590 Analyze_Declarations (Priv_Decls);
1592 -- Check the private declarations for incomplete deferred constants
1594 Inspect_Deferred_Constant_Completion (Priv_Decls);
1596 -- The first private entity is the immediate follower of the last
1597 -- visible entity, if there was one.
1600 Set_First_Private_Entity (Id, Next_Entity (L));
1602 Set_First_Private_Entity (Id, First_Entity (Id));
1605 -- There may be inherited private subprograms that need to be declared,
1606 -- even in the absence of an explicit private part. If there are any
1607 -- public declarations in the package and the package is a public child
1608 -- unit, then an implicit private part is assumed.
1610 elsif Present (L) and then Public_Child then
1611 Set_In_Private_Part (Id);
1612 Declare_Inherited_Private_Subprograms (Id);
1613 Set_First_Private_Entity (Id, Next_Entity (L));
1616 E := First_Entity (Id);
1617 while Present (E) loop
1619 -- Check rule of 3.6(11), which in general requires waiting till all
1620 -- full types have been seen.
1622 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1623 Check_Aliased_Component_Types (E);
1626 -- Check preelaborable initialization for full type completing a
1627 -- private type for which pragma Preelaborable_Initialization given.
1630 and then Must_Have_Preelab_Init (E)
1631 and then not Has_Preelaborable_Initialization (E)
1634 ("full view of & does not have preelaborable initialization", E);
1640 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1641 -- declaration having a known_discriminant_part shall not be an
1642 -- unchecked union type.
1644 if Present (Vis_Decls) then
1645 Inspect_Unchecked_Union_Completion (Vis_Decls);
1648 if Present (Priv_Decls) then
1649 Inspect_Unchecked_Union_Completion (Priv_Decls);
1652 if Ekind (Id) = E_Generic_Package
1653 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1654 and then Present (Priv_Decls)
1656 -- Save global references in private declarations, ignoring the
1657 -- visible declarations that were processed earlier.
1660 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1661 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1662 Save_Form : constant List_Id :=
1663 Generic_Formal_Declarations (Orig_Decl);
1666 -- Insert the freezing nodes after the private declarations to
1667 -- ensure that we analyze its aspects; needed to ensure that
1668 -- global entities referenced in the aspects are properly handled.
1670 if Ada_Version >= Ada_2012
1671 and then Is_Non_Empty_List (Priv_Decls)
1673 Insert_List_After_And_Analyze
1674 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1677 Set_Visible_Declarations (Orig_Spec, Empty_List);
1678 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1679 Save_Global_References (Orig_Decl);
1680 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1681 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1685 Process_End_Label (N, 'e', Id);
1687 -- Remove private_with_clauses of enclosing compilation unit, if they
1690 if Private_With_Clauses_Installed then
1691 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1694 -- For the case of a library level package, we must go through all the
1695 -- entities clearing the indications that the value may be constant and
1696 -- not modified. Why? Because any client of this package may modify
1697 -- these values freely from anywhere. This also applies to any nested
1698 -- packages or generic packages.
1700 -- For now we unconditionally clear constants for packages that are
1701 -- instances of generic packages. The reason is that we do not have the
1702 -- body yet, and we otherwise think things are unreferenced when they
1703 -- are not. This should be fixed sometime (the effect is not terrible,
1704 -- we just lose some warnings, and also some cases of value propagation)
1707 if Is_Library_Level_Entity (Id)
1708 or else Is_Generic_Instance (Id)
1710 Clear_Constants (Id, First_Entity (Id));
1711 Clear_Constants (Id, First_Private_Entity (Id));
1714 -- Issue an error in SPARK mode if a package specification contains
1715 -- more than one tagged type or type extension.
1717 Check_One_Tagged_Type_Or_Extension_At_Most;
1719 -- Output relevant information as to why the package requires a body.
1720 -- Do not consider generated packages as this exposes internal symbols
1721 -- and leads to confusing messages.
1723 if List_Body_Required_Info
1724 and then In_Extended_Main_Source_Unit (Id)
1725 and then Unit_Requires_Body (Id)
1726 and then Comes_From_Source (Id)
1728 Unit_Requires_Body_Info (Id);
1730 end Analyze_Package_Specification;
1732 --------------------------------------
1733 -- Analyze_Private_Type_Declaration --
1734 --------------------------------------
1736 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1737 Id : constant Entity_Id := Defining_Identifier (N);
1738 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1741 Generate_Definition (Id);
1742 Set_Is_Pure (Id, PF);
1743 Init_Size_Align (Id);
1745 if not Is_Package_Or_Generic_Package (Current_Scope)
1746 or else In_Private_Part (Current_Scope)
1748 Error_Msg_N ("invalid context for private declaration", N);
1751 New_Private_Type (N, Id, N);
1752 Set_Depends_On_Private (Id);
1754 if Has_Aspects (N) then
1755 Analyze_Aspect_Specifications (N, Id);
1757 end Analyze_Private_Type_Declaration;
1759 ----------------------------------
1760 -- Check_Anonymous_Access_Types --
1761 ----------------------------------
1763 procedure Check_Anonymous_Access_Types
1764 (Spec_Id : Entity_Id;
1771 -- Itype references are only needed by gigi, to force elaboration of
1772 -- itypes. In the absence of code generation, they are not needed.
1774 if not Expander_Active then
1778 E := First_Entity (Spec_Id);
1779 while Present (E) loop
1780 if Ekind (E) = E_Anonymous_Access_Type
1781 and then From_Limited_With (E)
1783 IR := Make_Itype_Reference (Sloc (P_Body));
1786 if No (Declarations (P_Body)) then
1787 Set_Declarations (P_Body, New_List (IR));
1789 Prepend (IR, Declarations (P_Body));
1795 end Check_Anonymous_Access_Types;
1797 -------------------------------------------
1798 -- Declare_Inherited_Private_Subprograms --
1799 -------------------------------------------
1801 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1803 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1804 -- Check whether an inherited subprogram S is an operation of an
1805 -- untagged derived type T.
1807 ---------------------
1808 -- Is_Primitive_Of --
1809 ---------------------
1811 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1815 -- If the full view is a scalar type, the type is the anonymous base
1816 -- type, but the operation mentions the first subtype, so check the
1817 -- signature against the base type.
1819 if Base_Type (Etype (S)) = Base_Type (T) then
1823 Formal := First_Formal (S);
1824 while Present (Formal) loop
1825 if Base_Type (Etype (Formal)) = Base_Type (T) then
1829 Next_Formal (Formal);
1834 end Is_Primitive_Of;
1841 Op_Elmt_2 : Elmt_Id;
1842 Prim_Op : Entity_Id;
1843 New_Op : Entity_Id := Empty;
1844 Parent_Subp : Entity_Id;
1847 -- Start of processing for Declare_Inherited_Private_Subprograms
1850 E := First_Entity (Id);
1851 while Present (E) loop
1853 -- If the entity is a nonprivate type extension whose parent type
1854 -- is declared in an open scope, then the type may have inherited
1855 -- operations that now need to be made visible. Ditto if the entity
1856 -- is a formal derived type in a child unit.
1858 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1860 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1861 and then Is_Generic_Type (E)))
1862 and then In_Open_Scopes (Scope (Etype (E)))
1863 and then Is_Base_Type (E)
1865 if Is_Tagged_Type (E) then
1866 Op_List := Primitive_Operations (E);
1868 Tag := First_Tag_Component (E);
1870 Op_Elmt := First_Elmt (Op_List);
1871 while Present (Op_Elmt) loop
1872 Prim_Op := Node (Op_Elmt);
1874 -- Search primitives that are implicit operations with an
1875 -- internal name whose parent operation has a normal name.
1877 if Present (Alias (Prim_Op))
1878 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1879 and then not Comes_From_Source (Prim_Op)
1880 and then Is_Internal_Name (Chars (Prim_Op))
1881 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1883 Parent_Subp := Alias (Prim_Op);
1885 -- Case 1: Check if the type has also an explicit
1886 -- overriding for this primitive.
1888 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1889 while Present (Op_Elmt_2) loop
1891 -- Skip entities with attribute Interface_Alias since
1892 -- they are not overriding primitives (these entities
1893 -- link an interface primitive with their covering
1896 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1897 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1898 and then No (Interface_Alias (Node (Op_Elmt_2)))
1900 -- The private inherited operation has been
1901 -- overridden by an explicit subprogram:
1902 -- replace the former by the latter.
1904 New_Op := Node (Op_Elmt_2);
1905 Replace_Elmt (Op_Elmt, New_Op);
1906 Remove_Elmt (Op_List, Op_Elmt_2);
1907 Set_Overridden_Operation (New_Op, Parent_Subp);
1909 -- We don't need to inherit its dispatching slot.
1910 -- Set_All_DT_Position has previously ensured that
1911 -- the same slot was assigned to the two primitives
1914 and then Present (DTC_Entity (New_Op))
1915 and then Present (DTC_Entity (Prim_Op))
1918 (DT_Position (New_Op) = DT_Position (Prim_Op));
1922 goto Next_Primitive;
1925 Next_Elmt (Op_Elmt_2);
1928 -- Case 2: We have not found any explicit overriding and
1929 -- hence we need to declare the operation (i.e., make it
1932 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1934 -- Inherit the dispatching slot if E is already frozen
1937 and then Present (DTC_Entity (Alias (Prim_Op)))
1939 Set_DTC_Entity_Value (E, New_Op);
1940 Set_DT_Position_Value (New_Op,
1941 DT_Position (Alias (Prim_Op)));
1945 (Is_Dispatching_Operation (New_Op)
1946 and then Node (Last_Elmt (Op_List)) = New_Op);
1948 -- Substitute the new operation for the old one in the
1949 -- type's primitive operations list. Since the new
1950 -- operation was also just added to the end of list,
1951 -- the last element must be removed.
1953 -- (Question: is there a simpler way of declaring the
1954 -- operation, say by just replacing the name of the
1955 -- earlier operation, reentering it in the in the symbol
1956 -- table (how?), and marking it as private???)
1958 Replace_Elmt (Op_Elmt, New_Op);
1959 Remove_Last_Elmt (Op_List);
1963 Next_Elmt (Op_Elmt);
1966 -- Generate listing showing the contents of the dispatch table
1968 if Debug_Flag_ZZ then
1973 -- For untagged type, scan forward to locate inherited hidden
1976 Prim_Op := Next_Entity (E);
1977 while Present (Prim_Op) loop
1978 if Is_Subprogram (Prim_Op)
1979 and then Present (Alias (Prim_Op))
1980 and then not Comes_From_Source (Prim_Op)
1981 and then Is_Internal_Name (Chars (Prim_Op))
1982 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1983 and then Is_Primitive_Of (E, Prim_Op)
1985 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1988 Next_Entity (Prim_Op);
1990 -- Derived operations appear immediately after the type
1991 -- declaration (or the following subtype indication for
1992 -- a derived scalar type). Further declarations cannot
1993 -- include inherited operations of the type.
1995 if Present (Prim_Op) then
1996 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2004 end Declare_Inherited_Private_Subprograms;
2006 -----------------------
2007 -- End_Package_Scope --
2008 -----------------------
2010 procedure End_Package_Scope (P : Entity_Id) is
2012 Uninstall_Declarations (P);
2014 end End_Package_Scope;
2016 ---------------------------
2017 -- Exchange_Declarations --
2018 ---------------------------
2020 procedure Exchange_Declarations (Id : Entity_Id) is
2021 Full_Id : constant Entity_Id := Full_View (Id);
2022 H1 : constant Entity_Id := Homonym (Id);
2023 Next1 : constant Entity_Id := Next_Entity (Id);
2028 -- If missing full declaration for type, nothing to exchange
2030 if No (Full_Id) then
2034 -- Otherwise complete the exchange, and preserve semantic links
2036 Next2 := Next_Entity (Full_Id);
2037 H2 := Homonym (Full_Id);
2039 -- Reset full declaration pointer to reflect the switched entities and
2040 -- readjust the next entity chains.
2042 Exchange_Entities (Id, Full_Id);
2044 Set_Next_Entity (Id, Next1);
2045 Set_Homonym (Id, H1);
2047 Set_Full_View (Full_Id, Id);
2048 Set_Next_Entity (Full_Id, Next2);
2049 Set_Homonym (Full_Id, H2);
2050 end Exchange_Declarations;
2052 ----------------------------
2053 -- Install_Package_Entity --
2054 ----------------------------
2056 procedure Install_Package_Entity (Id : Entity_Id) is
2058 if not Is_Internal (Id) then
2059 if Debug_Flag_E then
2060 Write_Str ("Install: ");
2061 Write_Name (Chars (Id));
2065 if Is_Child_Unit (Id) then
2068 -- Do not enter implicitly inherited non-overridden subprograms of
2069 -- a tagged type back into visibility if they have non-conformant
2070 -- homographs (Ada RM 8.3 12.3/2).
2072 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2076 Set_Is_Immediately_Visible (Id);
2079 end Install_Package_Entity;
2081 ----------------------------------
2082 -- Install_Private_Declarations --
2083 ----------------------------------
2085 procedure Install_Private_Declarations (P : Entity_Id) is
2088 Priv_Deps : Elist_Id;
2090 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2091 -- When the full view of a private type is made available, we do the
2092 -- same for its private dependents under proper visibility conditions.
2093 -- When compiling a grand-chid unit this needs to be done recursively.
2095 -----------------------------
2096 -- Swap_Private_Dependents --
2097 -----------------------------
2099 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2102 Priv_Elmt : Elmt_Id;
2106 Priv_Elmt := First_Elmt (Priv_Deps);
2107 while Present (Priv_Elmt) loop
2108 Priv := Node (Priv_Elmt);
2110 -- Before the exchange, verify that the presence of the Full_View
2111 -- field. This field will be empty if the entity has already been
2112 -- installed due to a previous call.
2114 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2116 if Is_Private_Type (Priv) then
2117 Deps := Private_Dependents (Priv);
2123 -- For each subtype that is swapped, we also swap the reference
2124 -- to it in Private_Dependents, to allow access to it when we
2125 -- swap them out in End_Package_Scope.
2127 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2129 -- Ensure that both views of the dependent private subtype are
2130 -- immediately visible if within some open scope. Check full
2131 -- view before exchanging views.
2133 if In_Open_Scopes (Scope (Full_View (Priv))) then
2134 Set_Is_Immediately_Visible (Priv);
2137 Exchange_Declarations (Priv);
2138 Set_Is_Immediately_Visible
2139 (Priv, In_Open_Scopes (Scope (Priv)));
2141 Set_Is_Potentially_Use_Visible
2142 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2144 -- Within a child unit, recurse, except in generic child unit,
2145 -- which (unfortunately) handle private_dependents separately.
2148 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2149 and then not Is_Empty_Elmt_List (Deps)
2150 and then not Inside_A_Generic
2152 Swap_Private_Dependents (Deps);
2156 Next_Elmt (Priv_Elmt);
2158 end Swap_Private_Dependents;
2160 -- Start of processing for Install_Private_Declarations
2163 -- First exchange declarations for private types, so that the full
2164 -- declaration is visible. For each private type, we check its
2165 -- Private_Dependents list and also exchange any subtypes of or derived
2166 -- types from it. Finally, if this is a Taft amendment type, the
2167 -- incomplete declaration is irrelevant, and we want to link the
2168 -- eventual full declaration with the original private one so we
2169 -- also skip the exchange.
2171 Id := First_Entity (P);
2172 while Present (Id) and then Id /= First_Private_Entity (P) loop
2173 if Is_Private_Base_Type (Id)
2174 and then Present (Full_View (Id))
2175 and then Comes_From_Source (Full_View (Id))
2176 and then Scope (Full_View (Id)) = Scope (Id)
2177 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2179 -- If there is a use-type clause on the private type, set the full
2180 -- view accordingly.
2182 Set_In_Use (Full_View (Id), In_Use (Id));
2183 Full := Full_View (Id);
2185 if Is_Private_Base_Type (Full)
2186 and then Has_Private_Declaration (Full)
2187 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2188 and then In_Open_Scopes (Scope (Etype (Full)))
2189 and then In_Package_Body (Current_Scope)
2190 and then not Is_Private_Type (Etype (Full))
2192 -- This is the completion of a private type by a derivation
2193 -- from another private type which is not private anymore. This
2194 -- can only happen in a package nested within a child package,
2195 -- when the parent type is defined in the parent unit. At this
2196 -- point the current type is not private either, and we have
2197 -- to install the underlying full view, which is now visible.
2198 -- Save the current full view as well, so that all views can be
2199 -- restored on exit. It may seem that after compiling the child
2200 -- body there are not environments to restore, but the back-end
2201 -- expects those links to be valid, and freeze nodes depend on
2204 if No (Full_View (Full))
2205 and then Present (Underlying_Full_View (Full))
2207 Set_Full_View (Id, Underlying_Full_View (Full));
2208 Set_Underlying_Full_View (Id, Full);
2209 Set_Is_Underlying_Full_View (Full);
2211 Set_Underlying_Full_View (Full, Empty);
2212 Set_Is_Frozen (Full_View (Id));
2216 Priv_Deps := Private_Dependents (Id);
2217 Exchange_Declarations (Id);
2218 Set_Is_Immediately_Visible (Id);
2219 Swap_Private_Dependents (Priv_Deps);
2225 -- Next make other declarations in the private part visible as well
2227 Id := First_Private_Entity (P);
2228 while Present (Id) loop
2229 Install_Package_Entity (Id);
2230 Set_Is_Hidden (Id, False);
2234 -- An abstract state is partially refined when it has at least one
2235 -- Part_Of constituent. Since these constituents are being installed
2236 -- into visibility, update the partial refinement status of any state
2237 -- defined in the associated package, subject to at least one Part_Of
2240 if Ekind_In (P, E_Generic_Package, E_Package) then
2242 States : constant Elist_Id := Abstract_States (P);
2243 State_Elmt : Elmt_Id;
2244 State_Id : Entity_Id;
2247 if Present (States) then
2248 State_Elmt := First_Elmt (States);
2249 while Present (State_Elmt) loop
2250 State_Id := Node (State_Elmt);
2252 if Present (Part_Of_Constituents (State_Id)) then
2253 Set_Has_Partial_Visible_Refinement (State_Id);
2256 Next_Elmt (State_Elmt);
2262 -- Indicate that the private part is currently visible, so it can be
2263 -- properly reset on exit.
2265 Set_In_Private_Part (P);
2266 end Install_Private_Declarations;
2268 ----------------------------------
2269 -- Install_Visible_Declarations --
2270 ----------------------------------
2272 procedure Install_Visible_Declarations (P : Entity_Id) is
2274 Last_Entity : Entity_Id;
2278 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2280 if Is_Package_Or_Generic_Package (P) then
2281 Last_Entity := First_Private_Entity (P);
2283 Last_Entity := Empty;
2286 Id := First_Entity (P);
2287 while Present (Id) and then Id /= Last_Entity loop
2288 Install_Package_Entity (Id);
2291 end Install_Visible_Declarations;
2293 --------------------------
2294 -- Is_Private_Base_Type --
2295 --------------------------
2297 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2299 return Ekind (E) = E_Private_Type
2300 or else Ekind (E) = E_Limited_Private_Type
2301 or else Ekind (E) = E_Record_Type_With_Private;
2302 end Is_Private_Base_Type;
2304 --------------------------
2305 -- Is_Visible_Dependent --
2306 --------------------------
2308 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2310 S : constant Entity_Id := Scope (Dep);
2313 -- Renamings created for actual types have the visibility of the actual
2315 if Ekind (S) = E_Package
2316 and then Is_Generic_Instance (S)
2317 and then (Is_Generic_Actual_Type (Dep)
2318 or else Is_Generic_Actual_Type (Full_View (Dep)))
2322 elsif not (Is_Derived_Type (Dep))
2323 and then Is_Derived_Type (Full_View (Dep))
2325 -- When instantiating a package body, the scope stack is empty, so
2326 -- check instead whether the dependent type is defined in the same
2327 -- scope as the instance itself.
2329 return In_Open_Scopes (S)
2330 or else (Is_Generic_Instance (Current_Scope)
2331 and then Scope (Dep) = Scope (Current_Scope));
2335 end Is_Visible_Dependent;
2337 ----------------------------
2338 -- May_Need_Implicit_Body --
2339 ----------------------------
2341 procedure May_Need_Implicit_Body (E : Entity_Id) is
2342 P : constant Node_Id := Unit_Declaration_Node (E);
2343 S : constant Node_Id := Parent (P);
2348 if not Has_Completion (E)
2349 and then Nkind (P) = N_Package_Declaration
2350 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2353 Make_Package_Body (Sloc (E),
2354 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2355 Chars => Chars (E)),
2356 Declarations => New_List);
2358 if Nkind (S) = N_Package_Specification then
2359 if Present (Private_Declarations (S)) then
2360 Decls := Private_Declarations (S);
2362 Decls := Visible_Declarations (S);
2365 Decls := Declarations (S);
2371 end May_Need_Implicit_Body;
2373 ----------------------
2374 -- New_Private_Type --
2375 ----------------------
2377 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2379 -- For other than Ada 2012, enter the name in the current scope
2381 if Ada_Version < Ada_2012 then
2384 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2385 -- there may be an incomplete previous view.
2391 Prev := Find_Type_Name (N);
2392 pragma Assert (Prev = Id
2393 or else (Ekind (Prev) = E_Incomplete_Type
2394 and then Present (Full_View (Prev))
2395 and then Full_View (Prev) = Id));
2399 if Limited_Present (Def) then
2400 Set_Ekind (Id, E_Limited_Private_Type);
2402 Set_Ekind (Id, E_Private_Type);
2406 Set_Has_Delayed_Freeze (Id);
2407 Set_Is_First_Subtype (Id);
2408 Init_Size_Align (Id);
2410 Set_Is_Constrained (Id,
2411 No (Discriminant_Specifications (N))
2412 and then not Unknown_Discriminants_Present (N));
2414 -- Set tagged flag before processing discriminants, to catch illegal
2417 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2419 Set_Discriminant_Constraint (Id, No_Elist);
2420 Set_Stored_Constraint (Id, No_Elist);
2422 if Present (Discriminant_Specifications (N)) then
2424 Process_Discriminants (N);
2427 elsif Unknown_Discriminants_Present (N) then
2428 Set_Has_Unknown_Discriminants (Id);
2431 Set_Private_Dependents (Id, New_Elmt_List);
2433 if Tagged_Present (Def) then
2434 Set_Ekind (Id, E_Record_Type_With_Private);
2435 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2436 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2437 Set_Is_Limited_Record (Id, Limited_Present (Def));
2438 Set_Has_Delayed_Freeze (Id, True);
2440 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2442 if Is_RTE (Id, RE_Timing_Event) then
2443 Set_Has_Timing_Event (Id);
2446 -- Create a class-wide type with the same attributes
2448 Make_Class_Wide_Type (Id);
2450 elsif Abstract_Present (Def) then
2451 Error_Msg_N ("only a tagged type can be abstract", N);
2453 end New_Private_Type;
2455 ---------------------------------
2456 -- Requires_Completion_In_Body --
2457 ---------------------------------
2459 function Requires_Completion_In_Body
2461 Pack_Id : Entity_Id;
2462 Do_Abstract_States : Boolean := False) return Boolean
2465 -- Always ignore child units. Child units get added to the entity list
2466 -- of a parent unit, but are not original entities of the parent, and
2467 -- so do not affect whether the parent needs a body.
2469 if Is_Child_Unit (Id) then
2472 -- Ignore formal packages and their renamings
2474 elsif Ekind (Id) = E_Package
2475 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2476 N_Formal_Package_Declaration
2480 -- Otherwise test to see if entity requires a completion. Note that
2481 -- subprogram entities whose declaration does not come from source are
2482 -- ignored here on the basis that we assume the expander will provide an
2483 -- implicit completion at some point.
2485 elsif (Is_Overloadable (Id)
2486 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2487 and then not Is_Abstract_Subprogram (Id)
2488 and then not Has_Completion (Id)
2489 and then Comes_From_Source (Parent (Id)))
2492 (Ekind (Id) = E_Package
2493 and then Id /= Pack_Id
2494 and then not Has_Completion (Id)
2495 and then Unit_Requires_Body (Id, Do_Abstract_States))
2498 (Ekind (Id) = E_Incomplete_Type
2499 and then No (Full_View (Id))
2500 and then not Is_Generic_Type (Id))
2503 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2504 and then not Has_Completion (Id))
2507 (Ekind (Id) = E_Generic_Package
2508 and then Id /= Pack_Id
2509 and then not Has_Completion (Id)
2510 and then Unit_Requires_Body (Id, Do_Abstract_States))
2513 (Is_Generic_Subprogram (Id)
2514 and then not Has_Completion (Id))
2518 -- Otherwise the entity does not require completion in a package body
2523 end Requires_Completion_In_Body;
2525 ----------------------------
2526 -- Uninstall_Declarations --
2527 ----------------------------
2529 procedure Uninstall_Declarations (P : Entity_Id) is
2530 Decl : constant Node_Id := Unit_Declaration_Node (P);
2533 Priv_Elmt : Elmt_Id;
2534 Priv_Sub : Entity_Id;
2536 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2537 -- Copy to the private declaration the attributes of the full view that
2538 -- need to be available for the partial view also.
2540 function Type_In_Use (T : Entity_Id) return Boolean;
2541 -- Check whether type or base type appear in an active use_type clause
2543 ------------------------------
2544 -- Preserve_Full_Attributes --
2545 ------------------------------
2547 procedure Preserve_Full_Attributes
2551 Full_Base : constant Entity_Id := Base_Type (Full);
2552 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2555 Set_Size_Info (Priv, Full);
2556 Set_RM_Size (Priv, RM_Size (Full));
2557 Set_Size_Known_At_Compile_Time
2558 (Priv, Size_Known_At_Compile_Time (Full));
2559 Set_Is_Volatile (Priv, Is_Volatile (Full));
2560 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2561 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2562 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2563 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2564 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2565 Set_Has_Pragma_Unreferenced_Objects
2566 (Priv, Has_Pragma_Unreferenced_Objects
2568 if Is_Unchecked_Union (Full) then
2569 Set_Is_Unchecked_Union (Base_Type (Priv));
2571 -- Why is atomic not copied here ???
2573 if Referenced (Full) then
2574 Set_Referenced (Priv);
2577 if Priv_Is_Base_Type then
2578 Set_Is_Controlled (Priv, Is_Controlled (Full_Base));
2579 Set_Finalize_Storage_Only
2580 (Priv, Finalize_Storage_Only (Full_Base));
2581 Set_Has_Controlled_Component
2582 (Priv, Has_Controlled_Component (Full_Base));
2584 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2587 Set_Freeze_Node (Priv, Freeze_Node (Full));
2589 -- Propagate Default_Initial_Condition-related attributes from the
2590 -- base type of the full view to the full view and vice versa. This
2591 -- may seem strange, but is necessary depending on which type
2592 -- triggered the generation of the DIC procedure body. As a result,
2593 -- both the full view and its base type carry the same DIC-related
2596 Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2597 Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2599 -- Propagate Default_Initial_Condition-related attributes from the
2600 -- full view to the private view.
2602 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2604 -- Propagate invariant-related attributes from the base type of the
2605 -- full view to the full view and vice versa. This may seem strange,
2606 -- but is necessary depending on which type triggered the generation
2607 -- of the invariant procedure body. As a result, both the full view
2608 -- and its base type carry the same invariant-related information.
2610 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2611 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2613 -- Propagate invariant-related attributes from the full view to the
2616 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2618 if Is_Tagged_Type (Priv)
2619 and then Is_Tagged_Type (Full)
2620 and then not Error_Posted (Full)
2622 if Is_Tagged_Type (Priv) then
2624 -- If the type is tagged, the tag itself must be available on
2625 -- the partial view, for expansion purposes.
2627 Set_First_Entity (Priv, First_Entity (Full));
2629 -- If there are discriminants in the partial view, these remain
2630 -- visible. Otherwise only the tag itself is visible, and there
2631 -- are no nameable components in the partial view.
2633 if No (Last_Entity (Priv)) then
2634 Set_Last_Entity (Priv, First_Entity (Priv));
2638 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2640 if Has_Discriminants (Full) then
2641 Set_Discriminant_Constraint (Priv,
2642 Discriminant_Constraint (Full));
2645 end Preserve_Full_Attributes;
2651 function Type_In_Use (T : Entity_Id) return Boolean is
2653 return Scope (Base_Type (T)) = P
2654 and then (In_Use (T) or else In_Use (Base_Type (T)));
2657 -- Start of processing for Uninstall_Declarations
2660 Id := First_Entity (P);
2661 while Present (Id) and then Id /= First_Private_Entity (P) loop
2662 if Debug_Flag_E then
2663 Write_Str ("unlinking visible entity ");
2664 Write_Int (Int (Id));
2668 -- On exit from the package scope, we must preserve the visibility
2669 -- established by use clauses in the current scope. Two cases:
2671 -- a) If the entity is an operator, it may be a primitive operator of
2672 -- a type for which there is a visible use-type clause.
2674 -- b) for other entities, their use-visibility is determined by a
2675 -- visible use clause for the package itself. For a generic instance,
2676 -- the instantiation of the formals appears in the visible part,
2677 -- but the formals are private and remain so.
2679 if Ekind (Id) = E_Function
2680 and then Is_Operator_Symbol_Name (Chars (Id))
2681 and then not Is_Hidden (Id)
2682 and then not Error_Posted (Id)
2684 Set_Is_Potentially_Use_Visible (Id,
2686 or else Type_In_Use (Etype (Id))
2687 or else Type_In_Use (Etype (First_Formal (Id)))
2688 or else (Present (Next_Formal (First_Formal (Id)))
2691 (Etype (Next_Formal (First_Formal (Id))))));
2693 if In_Use (P) and then not Is_Hidden (Id) then
2695 -- A child unit of a use-visible package remains use-visible
2696 -- only if it is itself a visible child unit. Otherwise it
2697 -- would remain visible in other contexts where P is use-
2698 -- visible, because once compiled it stays in the entity list
2699 -- of its parent unit.
2701 if Is_Child_Unit (Id) then
2702 Set_Is_Potentially_Use_Visible
2703 (Id, Is_Visible_Lib_Unit (Id));
2705 Set_Is_Potentially_Use_Visible (Id);
2709 Set_Is_Potentially_Use_Visible (Id, False);
2713 -- Local entities are not immediately visible outside of the package
2715 Set_Is_Immediately_Visible (Id, False);
2717 -- If this is a private type with a full view (for example a local
2718 -- subtype of a private type declared elsewhere), ensure that the
2719 -- full view is also removed from visibility: it may be exposed when
2720 -- swapping views in an instantiation. Similarly, ensure that the
2721 -- use-visibility is properly set on both views.
2723 if Is_Type (Id) and then Present (Full_View (Id)) then
2724 Set_Is_Immediately_Visible (Full_View (Id), False);
2725 Set_Is_Potentially_Use_Visible (Full_View (Id),
2726 Is_Potentially_Use_Visible (Id));
2729 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2730 Check_Abstract_Overriding (Id);
2731 Check_Conventions (Id);
2734 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2735 and then No (Full_View (Id))
2736 and then not Is_Generic_Type (Id)
2737 and then not Is_Derived_Type (Id)
2739 Error_Msg_N ("missing full declaration for private type&", Id);
2741 elsif Ekind (Id) = E_Record_Type_With_Private
2742 and then not Is_Generic_Type (Id)
2743 and then No (Full_View (Id))
2745 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2746 Error_Msg_N ("missing full declaration for private type&", Id);
2749 ("missing full declaration for private extension", Id);
2752 -- Case of constant, check for deferred constant declaration with
2753 -- no full view. Likely just a matter of a missing expression, or
2754 -- accidental use of the keyword constant.
2756 elsif Ekind (Id) = E_Constant
2758 -- OK if constant value present
2760 and then No (Constant_Value (Id))
2762 -- OK if full view present
2764 and then No (Full_View (Id))
2766 -- OK if imported, since that provides the completion
2768 and then not Is_Imported (Id)
2770 -- OK if object declaration replaced by renaming declaration as
2771 -- a result of OK_To_Rename processing (e.g. for concatenation)
2773 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2775 -- OK if object declaration with the No_Initialization flag set
2777 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2778 and then No_Initialization (Parent (Id)))
2780 -- If no private declaration is present, we assume the user did
2781 -- not intend a deferred constant declaration and the problem
2782 -- is simply that the initializing expression is missing.
2784 if not Has_Private_Declaration (Etype (Id)) then
2786 -- We assume that the user did not intend a deferred constant
2787 -- declaration, and the expression is just missing.
2790 ("constant declaration requires initialization expression",
2793 if Is_Limited_Type (Etype (Id)) then
2795 ("\if variable intended, remove CONSTANT from declaration",
2799 -- Otherwise if a private declaration is present, then we are
2800 -- missing the full declaration for the deferred constant.
2804 ("missing full declaration for deferred constant (RM 7.4)",
2807 if Is_Limited_Type (Etype (Id)) then
2809 ("\if variable intended, remove CONSTANT from declaration",
2818 -- If the specification was installed as the parent of a public child
2819 -- unit, the private declarations were not installed, and there is
2822 if not In_Private_Part (P) then
2825 Set_In_Private_Part (P, False);
2828 -- Make private entities invisible and exchange full and private
2829 -- declarations for private types. Id is now the first private entity
2832 while Present (Id) loop
2833 if Debug_Flag_E then
2834 Write_Str ("unlinking private entity ");
2835 Write_Int (Int (Id));
2839 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2840 Check_Abstract_Overriding (Id);
2841 Check_Conventions (Id);
2844 Set_Is_Immediately_Visible (Id, False);
2846 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2847 Full := Full_View (Id);
2849 -- If the partial view is not declared in the visible part of the
2850 -- package (as is the case when it is a type derived from some
2851 -- other private type in the private part of the current package),
2852 -- no exchange takes place.
2855 or else List_Containing (Parent (Id)) /=
2856 Visible_Declarations (Specification (Decl))
2861 -- The entry in the private part points to the full declaration,
2862 -- which is currently visible. Exchange them so only the private
2863 -- type declaration remains accessible, and link private and full
2864 -- declaration in the opposite direction. Before the actual
2865 -- exchange, we copy back attributes of the full view that must
2866 -- be available to the partial view too.
2868 Preserve_Full_Attributes (Id, Full);
2870 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2872 -- The following test may be redundant, as this is already
2873 -- diagnosed in sem_ch3. ???
2875 if not Is_Definite_Subtype (Full)
2876 and then Is_Definite_Subtype (Id)
2878 Error_Msg_Sloc := Sloc (Parent (Id));
2880 ("full view of& not compatible with declaration#", Full, Id);
2883 -- Swap out the subtypes and derived types of Id that
2884 -- were compiled in this scope, or installed previously
2885 -- by Install_Private_Declarations.
2887 -- Before we do the swap, we verify the presence of the Full_View
2888 -- field which may be empty due to a swap by a previous call to
2889 -- End_Package_Scope (e.g. from the freezing mechanism).
2891 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2892 while Present (Priv_Elmt) loop
2893 Priv_Sub := Node (Priv_Elmt);
2895 if Present (Full_View (Priv_Sub)) then
2896 if Scope (Priv_Sub) = P
2897 or else not In_Open_Scopes (Scope (Priv_Sub))
2899 Set_Is_Immediately_Visible (Priv_Sub, False);
2902 if Is_Visible_Dependent (Priv_Sub) then
2903 Preserve_Full_Attributes
2904 (Priv_Sub, Full_View (Priv_Sub));
2905 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2906 Exchange_Declarations (Priv_Sub);
2910 Next_Elmt (Priv_Elmt);
2913 -- Now restore the type itself to its private view
2915 Exchange_Declarations (Id);
2917 -- If we have installed an underlying full view for a type derived
2918 -- from a private type in a child unit, restore the proper views
2919 -- of private and full view. See corresponding code in
2920 -- Install_Private_Declarations.
2922 -- After the exchange, Full denotes the private type in the
2923 -- visible part of the package.
2925 if Is_Private_Base_Type (Full)
2926 and then Present (Full_View (Full))
2927 and then Present (Underlying_Full_View (Full))
2928 and then In_Package_Body (Current_Scope)
2930 Set_Full_View (Full, Underlying_Full_View (Full));
2931 Set_Underlying_Full_View (Full, Empty);
2934 elsif Ekind (Id) = E_Incomplete_Type
2935 and then Comes_From_Source (Id)
2936 and then No (Full_View (Id))
2938 -- Mark Taft amendment types. Verify that there are no primitive
2939 -- operations declared for the type (3.10.1(9)).
2941 Set_Has_Completion_In_Body (Id);
2948 Elmt := First_Elmt (Private_Dependents (Id));
2949 while Present (Elmt) loop
2950 Subp := Node (Elmt);
2952 -- Is_Primitive is tested because there can be cases where
2953 -- nonprimitive subprograms (in nested packages) are added
2954 -- to the Private_Dependents list.
2956 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2958 ("type& must be completed in the private part",
2961 -- The result type of an access-to-function type cannot be a
2962 -- Taft-amendment type, unless the version is Ada 2012 or
2963 -- later (see AI05-151).
2965 elsif Ada_Version < Ada_2012
2966 and then Ekind (Subp) = E_Subprogram_Type
2968 if Etype (Subp) = Id
2970 (Is_Class_Wide_Type (Etype (Subp))
2971 and then Etype (Etype (Subp)) = Id)
2974 ("type& must be completed in the private part",
2975 Associated_Node_For_Itype (Subp), Id);
2983 elsif not Is_Child_Unit (Id)
2984 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2987 Set_Is_Potentially_Use_Visible (Id, False);
2993 end Uninstall_Declarations;
2995 ------------------------
2996 -- Unit_Requires_Body --
2997 ------------------------
2999 function Unit_Requires_Body
3000 (Pack_Id : Entity_Id;
3001 Do_Abstract_States : Boolean := False) return Boolean
3005 Requires_Body : Boolean := False;
3006 -- Flag set when the unit has at least one construct that requries
3007 -- completion in a body.
3010 -- Imported entity never requires body. Right now, only subprograms can
3011 -- be imported, but perhaps in the future we will allow import of
3014 if Is_Imported (Pack_Id) then
3017 -- Body required if library package with pragma Elaborate_Body
3019 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3022 -- Body required if subprogram
3024 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3027 -- Treat a block as requiring a body
3029 elsif Ekind (Pack_Id) = E_Block then
3032 elsif Ekind (Pack_Id) = E_Package
3033 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3034 and then Present (Generic_Parent (Parent (Pack_Id)))
3037 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3039 if Has_Pragma_Elaborate_Body (G_P) then
3045 -- Traverse the entity chain of the package and look for constructs that
3046 -- require a completion in a body.
3048 E := First_Entity (Pack_Id);
3049 while Present (E) loop
3051 -- Skip abstract states because their completion depends on several
3052 -- criteria (see below).
3054 if Ekind (E) = E_Abstract_State then
3057 elsif Requires_Completion_In_Body
3058 (E, Pack_Id, Do_Abstract_States)
3060 Requires_Body := True;
3067 -- A [generic] package that defines at least one non-null abstract state
3068 -- requires a completion only when at least one other construct requires
3069 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3070 -- performed if the caller requests this behavior.
3072 if Do_Abstract_States
3073 and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3074 and then Has_Non_Null_Abstract_State (Pack_Id)
3075 and then Requires_Body
3080 return Requires_Body;
3081 end Unit_Requires_Body;
3083 -----------------------------
3084 -- Unit_Requires_Body_Info --
3085 -----------------------------
3087 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3091 -- An imported entity never requires body. Right now, only subprograms
3092 -- can be imported, but perhaps in the future we will allow import of
3095 if Is_Imported (Pack_Id) then
3098 -- Body required if library package with pragma Elaborate_Body
3100 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3101 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3103 -- Body required if subprogram
3105 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3106 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3108 -- Body required if generic parent has Elaborate_Body
3110 elsif Ekind (Pack_Id) = E_Package
3111 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3112 and then Present (Generic_Parent (Parent (Pack_Id)))
3115 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3117 if Has_Pragma_Elaborate_Body (G_P) then
3119 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3124 -- A [generic] package that introduces at least one non-null abstract
3125 -- state requires completion. However, there is a separate rule that
3126 -- requires that such a package have a reason other than this for a
3127 -- body being required (if necessary a pragma Elaborate_Body must be
3128 -- provided). If Ignore_Abstract_State is True, we don't do this check
3129 -- (so we can use Unit_Requires_Body to check for some other reason).
3131 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3132 and then Present (Abstract_States (Pack_Id))
3133 and then not Is_Null_State
3134 (Node (First_Elmt (Abstract_States (Pack_Id))))
3137 ("info: & requires body (non-null abstract state aspect)?Y?",
3141 -- Otherwise search entity chain for entity requiring completion
3143 E := First_Entity (Pack_Id);
3144 while Present (E) loop
3145 if Requires_Completion_In_Body (E, Pack_Id) then
3146 Error_Msg_Node_2 := E;
3148 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3153 end Unit_Requires_Body_Info;