1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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 if Is_Inlined
(Decl_Id
)
396 or else Has_Pragma_Inline
(Decl_Id
)
398 -- Inspect the statements of the subprogram body
399 -- to determine whether the body references other
403 and then not Contains_Subprograms_Refs
(Decl
)
405 Has_Non_Subprograms_Referencer
:= True;
411 -- Otherwise this is a stand alone subprogram body
414 Decl_Id
:= Defining_Entity
(Decl
);
416 -- An inlined body acts as a referencer. Note that an
417 -- inlined subprogram remains Is_Public as gigi requires
418 -- the flag to be set.
420 -- Note that we test Has_Pragma_Inline here rather than
421 -- Is_Inlined. We are compiling this for a client, and
422 -- it is the client who will decide if actual inlining
423 -- should occur, so we need to assume that the procedure
424 -- could be inlined for the purpose of accessing global
427 if Has_Pragma_Inline
(Decl_Id
) then
429 and then not Contains_Subprograms_Refs
(Decl
)
431 Has_Non_Subprograms_Referencer
:= True;
436 Set_Is_Public
(Decl_Id
, False);
440 -- Exceptions, objects and renamings do not need to be public
441 -- if they are not followed by a construct which can reference
442 -- and export them. The Is_Public flag is reset on top level
443 -- entities only as anything nested is local to its context.
444 -- Likewise for subprograms, but we work harder for them as
445 -- their visibility can have a significant impact on inlining
446 -- decisions in the back end.
448 elsif Nkind_In
(Decl
, N_Exception_Declaration
,
449 N_Object_Declaration
,
450 N_Object_Renaming_Declaration
,
451 N_Subprogram_Declaration
,
452 N_Subprogram_Renaming_Declaration
)
454 Decl_Id
:= Defining_Entity
(Decl
);
457 and then not Is_Imported
(Decl_Id
)
458 and then not Is_Exported
(Decl_Id
)
459 and then No
(Interface_Name
(Decl_Id
))
461 (not Has_Non_Subprograms_Referencer
462 or else Nkind
(Decl
) = N_Subprogram_Declaration
)
464 Set_Is_Public
(Decl_Id
, False);
471 return Has_Non_Subprograms_Referencer
;
476 Discard
: Boolean := True;
477 pragma Unreferenced
(Discard
);
479 -- Start of processing for Hide_Public_Entities
482 -- The algorithm examines the top level declarations of a package
483 -- body in reverse looking for a construct that may export entities
484 -- declared prior to it. If such a scenario is encountered, then all
485 -- entities in the range Last (Decls) .. construct are hidden from
486 -- external visibility. Consider:
494 -- package body Pack is
495 -- External_Obj : ...; -- (1)
497 -- package body Gen is -- (2)
498 -- ... External_Obj ... -- (3)
501 -- Local_Obj : ...; -- (4)
504 -- In this example Local_Obj (4) must not be externally visible as
505 -- it cannot be exported by anything in Pack. The body of generic
506 -- package Gen (2) on the other hand acts as a "referencer" and may
507 -- export anything declared before it. Since the compiler does not
508 -- perform flow analysis, it is not possible to determine precisely
509 -- which entities will be exported when Gen is instantiated. In the
510 -- example above External_Obj (1) is exported at (3), but this may
511 -- not always be the case. The algorithm takes a conservative stance
512 -- and leaves entity External_Obj public.
514 Discard
:= Has_Referencer
(Decls
, Top_Level
=> True);
515 end Hide_Public_Entities
;
517 ----------------------------------
518 -- Install_Composite_Operations --
519 ----------------------------------
521 procedure Install_Composite_Operations
(P
: Entity_Id
) is
525 Id
:= First_Entity
(P
);
526 while Present
(Id
) loop
528 and then (Is_Limited_Composite
(Id
)
529 or else Is_Private_Composite
(Id
))
530 and then No
(Private_Component
(Id
))
532 Set_Is_Limited_Composite
(Id
, False);
533 Set_Is_Private_Composite
(Id
, False);
538 end Install_Composite_Operations
;
544 Last_Spec_Entity
: Entity_Id
;
545 Mode
: Ghost_Mode_Type
;
550 -- Start of processing for Analyze_Package_Body_Helper
553 -- Find corresponding package specification, and establish the current
554 -- scope. The visible defining entity for the package is the defining
555 -- occurrence in the spec. On exit from the package body, all body
556 -- declarations are attached to the defining entity for the body, but
557 -- the later is never used for name resolution. In this fashion there
558 -- is only one visible entity that denotes the package.
560 -- Set Body_Id. Note that this will be reset to point to the generic
561 -- copy later on in the generic case.
563 Body_Id
:= Defining_Entity
(N
);
565 -- Body is body of package instantiation. Corresponding spec has already
568 if Present
(Corresponding_Spec
(N
)) then
569 Spec_Id
:= Corresponding_Spec
(N
);
570 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
573 Spec_Id
:= Current_Entity_In_Scope
(Defining_Entity
(N
));
576 and then Is_Package_Or_Generic_Package
(Spec_Id
)
578 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
580 if Nkind
(Pack_Decl
) = N_Package_Renaming_Declaration
then
581 Error_Msg_N
("cannot supply body for package renaming", N
);
584 elsif Present
(Corresponding_Body
(Pack_Decl
)) then
585 Error_Msg_N
("redefinition of package body", N
);
590 Error_Msg_N
("missing specification for package body", N
);
594 if Is_Package_Or_Generic_Package
(Spec_Id
)
595 and then (Scope
(Spec_Id
) = Standard_Standard
596 or else Is_Child_Unit
(Spec_Id
))
597 and then not Unit_Requires_Body
(Spec_Id
)
599 if Ada_Version
= Ada_83
then
601 ("optional package body (not allowed in Ada 95)??", N
);
603 Error_Msg_N
("spec of this package does not allow a body", N
);
608 -- A [generic] package body "freezes" the contract of the nearest
609 -- enclosing package body and all other contracts encountered in the
610 -- same declarative part up to and excluding the package body:
612 -- package body Nearest_Enclosing_Package
613 -- with Refined_State => (State => Constit)
617 -- package body Freezes_Enclosing_Package_Body
618 -- with Refined_State => (State_2 => Constit_2)
623 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
625 -- This ensures that any annotations referenced by the contract of a
626 -- [generic] subprogram body declared within the current package body
627 -- are available. This form of "freezing" is decoupled from the usual
628 -- Freeze_xxx mechanism because it must also work in the context of
629 -- generics where normal freezing is disabled.
631 -- Only bodies coming from source should cause this type of "freezing".
632 -- Instantiated generic bodies are excluded because their processing is
633 -- performed in a separate compilation pass which lacks enough semantic
634 -- information with respect to contract analysis. It is safe to suppress
635 -- the "freezing" of contracts in this case because this action already
636 -- took place at the end of the enclosing declarative part.
638 if Comes_From_Source
(N
)
639 and then not Is_Generic_Instance
(Spec_Id
)
641 Analyze_Previous_Contracts
(N
);
644 -- A package body is Ghost when the corresponding spec is Ghost. Set
645 -- the mode now to ensure that any nodes generated during analysis and
646 -- expansion are properly flagged as ignored Ghost.
648 Mark_And_Set_Ghost_Body
(N
, Spec_Id
, Mode
);
650 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
651 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
653 if Is_Child_Unit
(Spec_Id
) then
654 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
656 ("body of child unit& cannot be an inner package", N
, Spec_Id
);
659 Set_Is_Child_Unit
(Body_Id
);
662 -- Generic package case
664 if Ekind
(Spec_Id
) = E_Generic_Package
then
666 -- Disable expansion and perform semantic analysis on copy. The
667 -- unannotated body will be used in all instantiations.
669 Body_Id
:= Defining_Entity
(N
);
670 Set_Ekind
(Body_Id
, E_Package_Body
);
671 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
672 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
673 Set_Body_Entity
(Spec_Id
, Body_Id
);
674 Set_Spec_Entity
(Body_Id
, Spec_Id
);
676 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
679 -- Once the contents of the generic copy and the template are
680 -- swapped, do the same for their respective aspect specifications.
682 Exchange_Aspects
(N
, New_N
);
684 -- Collect all contract-related source pragmas found within the
685 -- template and attach them to the contract of the package body.
686 -- This contract is used in the capture of global references within
689 Create_Generic_Contract
(N
);
691 -- Update Body_Id to point to the copied node for the remainder of
694 Body_Id
:= Defining_Entity
(N
);
698 -- The Body_Id is that of the copied node in the generic case, the
699 -- current node otherwise. Note that N was rewritten above, so we must
700 -- be sure to get the latest Body_Id value.
702 Set_Ekind
(Body_Id
, E_Package_Body
);
703 Set_Body_Entity
(Spec_Id
, Body_Id
);
704 Set_Spec_Entity
(Body_Id
, Spec_Id
);
706 -- Defining name for the package body is not a visible entity: Only the
707 -- defining name for the declaration is visible.
709 Set_Etype
(Body_Id
, Standard_Void_Type
);
710 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
711 Set_Corresponding_Spec
(N
, Spec_Id
);
712 Set_Corresponding_Body
(Pack_Decl
, Body_Id
);
714 -- The body entity is not used for semantics or code generation, but
715 -- it is attached to the entity list of the enclosing scope to simplify
716 -- the listing of back-annotations for the types it main contain.
718 if Scope
(Spec_Id
) /= Standard_Standard
then
719 Append_Entity
(Body_Id
, Scope
(Spec_Id
));
722 -- Indicate that we are currently compiling the body of the package
724 Set_In_Package_Body
(Spec_Id
);
725 Set_Has_Completion
(Spec_Id
);
726 Last_Spec_Entity
:= Last_Entity
(Spec_Id
);
728 if Has_Aspects
(N
) then
729 Analyze_Aspect_Specifications
(N
, Body_Id
);
732 Push_Scope
(Spec_Id
);
734 -- Set SPARK_Mode only for non-generic package
736 if Ekind
(Spec_Id
) = E_Package
then
737 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
738 Set_SPARK_Aux_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
739 Set_SPARK_Pragma_Inherited
(Body_Id
);
740 Set_SPARK_Aux_Pragma_Inherited
(Body_Id
);
743 Set_Categorization_From_Pragmas
(N
);
745 Install_Visible_Declarations
(Spec_Id
);
746 Install_Private_Declarations
(Spec_Id
);
747 Install_Private_With_Clauses
(Spec_Id
);
748 Install_Composite_Operations
(Spec_Id
);
750 Check_Anonymous_Access_Types
(Spec_Id
, N
);
752 if Ekind
(Spec_Id
) = E_Generic_Package
then
753 Set_Use
(Generic_Formal_Declarations
(Pack_Decl
));
756 Set_Use
(Visible_Declarations
(Specification
(Pack_Decl
)));
757 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
759 -- This is a nested package, so it may be necessary to declare certain
760 -- inherited subprograms that are not yet visible because the parent
761 -- type's subprograms are now visible.
763 if Ekind
(Scope
(Spec_Id
)) = E_Package
764 and then Scope
(Spec_Id
) /= Standard_Standard
766 Declare_Inherited_Private_Subprograms
(Spec_Id
);
769 -- A package body "freezes" the contract of its initial declaration.
770 -- This analysis depends on attribute Corresponding_Spec being set. Only
771 -- bodies coming from source shuld cause this type of "freezing".
773 if Present
(Declarations
(N
)) then
774 Analyze_Declarations
(Declarations
(N
));
775 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
778 -- Verify that the SPARK_Mode of the body agrees with that of its spec
780 if Present
(SPARK_Pragma
(Body_Id
)) then
781 if Present
(SPARK_Aux_Pragma
(Spec_Id
)) then
782 if Get_SPARK_Mode_From_Annotation
(SPARK_Aux_Pragma
(Spec_Id
)) =
785 Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = On
787 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
788 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
789 Error_Msg_Sloc
:= Sloc
(SPARK_Aux_Pragma
(Spec_Id
));
791 ("\value Off was set for SPARK_Mode on & #", N
, Spec_Id
);
795 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
796 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
797 Error_Msg_Sloc
:= Sloc
(Spec_Id
);
799 ("\no value was set for SPARK_Mode on & #", N
, Spec_Id
);
803 -- Analyze_Declarations has caused freezing of all types. Now generate
804 -- bodies for RACW primitives and stream attributes, if any.
806 if Ekind
(Spec_Id
) = E_Package
and then Has_RACW
(Spec_Id
) then
808 -- Attach subprogram bodies to support RACWs declared in spec
810 Append_RACW_Bodies
(Declarations
(N
), Spec_Id
);
811 Analyze_List
(Declarations
(N
));
814 HSS
:= Handled_Statement_Sequence
(N
);
816 if Present
(HSS
) then
817 Process_End_Label
(HSS
, 't', Spec_Id
);
820 -- Check that elaboration code in a preelaborable package body is
821 -- empty other than null statements and labels (RM 10.2.1(6)).
823 Validate_Null_Statement_Sequence
(N
);
826 Validate_Categorization_Dependency
(N
, Spec_Id
);
827 Check_Completion
(Body_Id
);
829 -- Generate start of body reference. Note that we do this fairly late,
830 -- because the call will use In_Extended_Main_Source_Unit as a check,
831 -- and we want to make sure that Corresponding_Stub links are set
833 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
835 -- For a generic package, collect global references and mark them on
836 -- the original body so that they are not resolved again at the point
839 if Ekind
(Spec_Id
) /= E_Package
then
840 Save_Global_References
(Original_Node
(N
));
844 -- The entities of the package body have so far been chained onto the
845 -- declaration chain for the spec. That's been fine while we were in the
846 -- body, since we wanted them to be visible, but now that we are leaving
847 -- the package body, they are no longer visible, so we remove them from
848 -- the entity chain of the package spec entity, and copy them to the
849 -- entity chain of the package body entity, where they will never again
852 if Present
(Last_Spec_Entity
) then
853 Set_First_Entity
(Body_Id
, Next_Entity
(Last_Spec_Entity
));
854 Set_Next_Entity
(Last_Spec_Entity
, Empty
);
855 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
856 Set_Last_Entity
(Spec_Id
, Last_Spec_Entity
);
859 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
860 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
861 Set_First_Entity
(Spec_Id
, Empty
);
862 Set_Last_Entity
(Spec_Id
, Empty
);
865 End_Package_Scope
(Spec_Id
);
867 -- All entities declared in body are not visible
873 E
:= First_Entity
(Body_Id
);
874 while Present
(E
) loop
875 Set_Is_Immediately_Visible
(E
, False);
876 Set_Is_Potentially_Use_Visible
(E
, False);
879 -- Child units may appear on the entity list (e.g. if they appear
880 -- in the context of a subunit) but they are not body entities.
882 if not Is_Child_Unit
(E
) then
883 Set_Is_Package_Body_Entity
(E
);
890 Check_References
(Body_Id
);
892 -- For a generic unit, check that the formal parameters are referenced,
893 -- and that local variables are used, as for regular packages.
895 if Ekind
(Spec_Id
) = E_Generic_Package
then
896 Check_References
(Spec_Id
);
899 -- At this point all entities of the package body are externally visible
900 -- to the linker as their Is_Public flag is set to True. This proactive
901 -- approach is necessary because an inlined or a generic body for which
902 -- code is generated in other units may need to see these entities. Cut
903 -- down the number of global symbols that do not neet public visibility
904 -- as this has two beneficial effects:
905 -- (1) It makes the compilation process more efficient.
906 -- (2) It gives the code generatormore freedom to optimize within each
907 -- unit, especially subprograms.
909 -- This is done only for top level library packages or child units as
910 -- the algorithm does a top down traversal of the package body.
912 if (Scope
(Spec_Id
) = Standard_Standard
or else Is_Child_Unit
(Spec_Id
))
913 and then not Is_Generic_Unit
(Spec_Id
)
915 Hide_Public_Entities
(Declarations
(N
));
918 -- If expander is not active, then here is where we turn off the
919 -- In_Package_Body flag, otherwise it is turned off at the end of the
920 -- corresponding expansion routine. If this is an instance body, we need
921 -- to qualify names of local entities, because the body may have been
922 -- compiled as a preliminary to another instantiation.
924 if not Expander_Active
then
925 Set_In_Package_Body
(Spec_Id
, False);
927 if Is_Generic_Instance
(Spec_Id
)
928 and then Operating_Mode
= Generate_Code
930 Qualify_Entity_Names
(N
);
934 Restore_Ghost_Mode
(Mode
);
935 end Analyze_Package_Body_Helper
;
937 ---------------------------------
938 -- Analyze_Package_Declaration --
939 ---------------------------------
941 procedure Analyze_Package_Declaration
(N
: Node_Id
) is
942 Id
: constant Node_Id
:= Defining_Entity
(N
);
944 Is_Comp_Unit
: constant Boolean :=
945 Nkind
(Parent
(N
)) = N_Compilation_Unit
;
947 Body_Required
: Boolean;
948 -- True when this package declaration requires a corresponding body
952 Write_Str
("==> package spec ");
953 Write_Name
(Chars
(Id
));
954 Write_Str
(" from ");
955 Write_Location
(Sloc
(N
));
960 Generate_Definition
(Id
);
962 Set_Ekind
(Id
, E_Package
);
963 Set_Etype
(Id
, Standard_Void_Type
);
965 -- Set SPARK_Mode from context only for non-generic package
967 if Ekind
(Id
) = E_Package
then
968 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
969 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
970 Set_SPARK_Pragma_Inherited
(Id
);
971 Set_SPARK_Aux_Pragma_Inherited
(Id
);
974 -- Analyze aspect specifications immediately, since we need to recognize
975 -- things like Pure early enough to diagnose violations during analysis.
977 if Has_Aspects
(N
) then
978 Analyze_Aspect_Specifications
(N
, Id
);
981 -- Ada 2005 (AI-217): Check if the package has been illegally named in
982 -- a limited-with clause of its own context. In this case the error has
983 -- been previously notified by Analyze_Context.
985 -- limited with Pkg; -- ERROR
986 -- package Pkg is ...
988 if From_Limited_With
(Id
) then
994 Set_Is_Pure
(Id
, Is_Pure
(Enclosing_Lib_Unit_Entity
));
995 Set_Categorization_From_Pragmas
(N
);
997 Analyze
(Specification
(N
));
998 Validate_Categorization_Dependency
(N
, Id
);
1000 -- Determine whether the package requires a body. Abstract states are
1001 -- intentionally ignored because they do require refinement which can
1002 -- only come in a body, but at the same time they do not force the need
1003 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1005 Body_Required
:= Unit_Requires_Body
(Id
);
1007 if not Body_Required
then
1009 -- If the package spec does not require an explicit body, then there
1010 -- are not entities requiring completion in the language sense. Call
1011 -- Check_Completion now to ensure that nested package declarations
1012 -- that require an implicit body get one. (In the case where a body
1013 -- is required, Check_Completion is called at the end of the body's
1014 -- declarative part.)
1018 -- If the package spec does not require an explicit body, then all
1019 -- abstract states declared in nested packages cannot possibly get
1020 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1021 -- only when the compilation unit is the main unit to allow for
1022 -- modular SPARK analysis where packages do not necessarily have
1025 if Is_Comp_Unit
then
1026 Check_State_Refinements
1028 Is_Main_Unit
=> Parent
(N
) = Cunit
(Main_Unit
));
1032 if Is_Comp_Unit
then
1034 -- Set Body_Required indication on the compilation unit node, and
1035 -- determine whether elaboration warnings may be meaningful on it.
1037 Set_Body_Required
(Parent
(N
), Body_Required
);
1039 if not Body_Required
then
1040 Set_Suppress_Elaboration_Warnings
(Id
);
1044 End_Package_Scope
(Id
);
1046 -- For the declaration of a library unit that is a remote types package,
1047 -- check legality rules regarding availability of stream attributes for
1048 -- types that contain non-remote access values. This subprogram performs
1049 -- visibility tests that rely on the fact that we have exited the scope
1052 if Is_Comp_Unit
then
1053 Validate_RT_RAT_Component
(N
);
1056 if Debug_Flag_C
then
1058 Write_Str
("<== package spec ");
1059 Write_Name
(Chars
(Id
));
1060 Write_Str
(" from ");
1061 Write_Location
(Sloc
(N
));
1064 end Analyze_Package_Declaration
;
1066 -----------------------------------
1067 -- Analyze_Package_Specification --
1068 -----------------------------------
1070 -- Note that this code is shared for the analysis of generic package specs
1071 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1073 procedure Analyze_Package_Specification
(N
: Node_Id
) is
1074 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1075 Orig_Decl
: constant Node_Id
:= Original_Node
(Parent
(N
));
1076 Vis_Decls
: constant List_Id
:= Visible_Declarations
(N
);
1077 Priv_Decls
: constant List_Id
:= Private_Declarations
(N
);
1080 Public_Child
: Boolean;
1082 Private_With_Clauses_Installed
: Boolean := False;
1083 -- In Ada 2005, private with_clauses are visible in the private part
1084 -- of a nested package, even if it appears in the public part of the
1085 -- enclosing package. This requires a separate step to install these
1086 -- private_with_clauses, and remove them at the end of the nested
1089 procedure Check_One_Tagged_Type_Or_Extension_At_Most
;
1090 -- Issue an error in SPARK mode if a package specification contains
1091 -- more than one tagged type or type extension.
1093 procedure Clear_Constants
(Id
: Entity_Id
; FE
: Entity_Id
);
1094 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1095 -- Is_True_Constant) on all variables that are entities of Id, and on
1096 -- the chain whose first element is FE. A recursive call is made for all
1097 -- packages and generic packages.
1099 procedure Generate_Parent_References
;
1100 -- For a child unit, generate references to parent units, for
1101 -- GPS navigation purposes.
1103 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean;
1104 -- Child and Unit are entities of compilation units. True if Child
1105 -- is a public child of Parent as defined in 10.1.1
1107 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
);
1108 -- Reject completion of an incomplete or private type declarations
1109 -- having a known discriminant part by an unchecked union.
1111 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
);
1112 -- Given the package entity of a generic package instantiation or
1113 -- formal package whose corresponding generic is a child unit, installs
1114 -- the private declarations of each of the child unit's parents.
1115 -- This has to be done at the point of entering the instance package's
1116 -- private part rather than being done in Sem_Ch12.Install_Parent
1117 -- (which is where the parents' visible declarations are installed).
1119 ------------------------------------------------
1120 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1121 ------------------------------------------------
1123 procedure Check_One_Tagged_Type_Or_Extension_At_Most
is
1126 procedure Check_Decls
(Decls
: List_Id
);
1127 -- Check that either Previous is Empty and Decls does not contain
1128 -- more than one tagged type or type extension, or Previous is
1129 -- already set and Decls contains no tagged type or type extension.
1135 procedure Check_Decls
(Decls
: List_Id
) is
1139 Decl
:= First
(Decls
);
1140 while Present
(Decl
) loop
1141 if Nkind
(Decl
) = N_Full_Type_Declaration
1142 and then Is_Tagged_Type
(Defining_Identifier
(Decl
))
1144 if No
(Previous
) then
1148 Error_Msg_Sloc
:= Sloc
(Previous
);
1149 Check_SPARK_05_Restriction
1150 ("at most one tagged type or type extension allowed",
1151 "\\ previous declaration#",
1160 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1164 Check_Decls
(Vis_Decls
);
1166 if Present
(Priv_Decls
) then
1167 Check_Decls
(Priv_Decls
);
1169 end Check_One_Tagged_Type_Or_Extension_At_Most
;
1171 ---------------------
1172 -- Clear_Constants --
1173 ---------------------
1175 procedure Clear_Constants
(Id
: Entity_Id
; FE
: Entity_Id
) is
1179 -- Ignore package renamings, not interesting and they can cause self
1180 -- referential loops in the code below.
1182 if Nkind
(Parent
(Id
)) = N_Package_Renaming_Declaration
then
1186 -- Note: in the loop below, the check for Next_Entity pointing back
1187 -- to the package entity may seem odd, but it is needed, because a
1188 -- package can contain a renaming declaration to itself, and such
1189 -- renamings are generated automatically within package instances.
1192 while Present
(E
) and then E
/= Id
loop
1193 if Is_Assignable
(E
) then
1194 Set_Never_Set_In_Source
(E
, False);
1195 Set_Is_True_Constant
(E
, False);
1196 Set_Current_Value
(E
, Empty
);
1197 Set_Is_Known_Null
(E
, False);
1198 Set_Last_Assignment
(E
, Empty
);
1200 if not Can_Never_Be_Null
(E
) then
1201 Set_Is_Known_Non_Null
(E
, False);
1204 elsif Is_Package_Or_Generic_Package
(E
) then
1205 Clear_Constants
(E
, First_Entity
(E
));
1206 Clear_Constants
(E
, First_Private_Entity
(E
));
1211 end Clear_Constants
;
1213 --------------------------------
1214 -- Generate_Parent_References --
1215 --------------------------------
1217 procedure Generate_Parent_References
is
1218 Decl
: constant Node_Id
:= Parent
(N
);
1221 if Id
= Cunit_Entity
(Main_Unit
)
1222 or else Parent
(Decl
) = Library_Unit
(Cunit
(Main_Unit
))
1224 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1226 elsif not Nkind_In
(Unit
(Cunit
(Main_Unit
)), N_Subprogram_Body
,
1229 -- If current unit is an ancestor of main unit, generate a
1230 -- reference to its own parent.
1234 Main_Spec
: Node_Id
:= Unit
(Cunit
(Main_Unit
));
1237 if Nkind
(Main_Spec
) = N_Package_Body
then
1238 Main_Spec
:= Unit
(Library_Unit
(Cunit
(Main_Unit
)));
1241 U
:= Parent_Spec
(Main_Spec
);
1242 while Present
(U
) loop
1243 if U
= Parent
(Decl
) then
1244 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1247 elsif Nkind
(Unit
(U
)) = N_Package_Body
then
1251 U
:= Parent_Spec
(Unit
(U
));
1256 end Generate_Parent_References
;
1258 ---------------------
1259 -- Is_Public_Child --
1260 ---------------------
1262 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean is
1264 if not Is_Private_Descendant
(Child
) then
1267 if Child
= Unit
then
1268 return not Private_Present
(
1269 Parent
(Unit_Declaration_Node
(Child
)));
1271 return Is_Public_Child
(Scope
(Child
), Unit
);
1274 end Is_Public_Child
;
1276 ----------------------------------------
1277 -- Inspect_Unchecked_Union_Completion --
1278 ----------------------------------------
1280 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
) is
1284 Decl
:= First
(Decls
);
1285 while Present
(Decl
) loop
1287 -- We are looking at an incomplete or private type declaration
1288 -- with a known_discriminant_part whose full view is an
1291 if Nkind_In
(Decl
, N_Incomplete_Type_Declaration
,
1292 N_Private_Type_Declaration
)
1293 and then Has_Discriminants
(Defining_Identifier
(Decl
))
1294 and then Present
(Full_View
(Defining_Identifier
(Decl
)))
1296 Is_Unchecked_Union
(Full_View
(Defining_Identifier
(Decl
)))
1299 ("completion of discriminated partial view "
1300 & "cannot be an unchecked union",
1301 Full_View
(Defining_Identifier
(Decl
)));
1306 end Inspect_Unchecked_Union_Completion
;
1308 -----------------------------------------
1309 -- Install_Parent_Private_Declarations --
1310 -----------------------------------------
1312 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
) is
1313 Inst_Par
: Entity_Id
;
1314 Gen_Par
: Entity_Id
;
1315 Inst_Node
: Node_Id
;
1318 Inst_Par
:= Inst_Id
;
1321 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
1322 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
1323 Inst_Node
:= Get_Package_Instantiation_Node
(Inst_Par
);
1325 if Nkind_In
(Inst_Node
, N_Package_Instantiation
,
1326 N_Formal_Package_Declaration
)
1327 and then Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
1329 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
1331 if Present
(Renamed_Entity
(Inst_Par
)) then
1332 Inst_Par
:= Renamed_Entity
(Inst_Par
);
1337 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
1339 -- Install the private declarations and private use clauses
1340 -- of a parent instance of the child instance, unless the
1341 -- parent instance private declarations have already been
1342 -- installed earlier in Analyze_Package_Specification, which
1343 -- happens when a generic child is instantiated, and the
1344 -- instance is a child of the parent instance.
1346 -- Installing the use clauses of the parent instance twice
1347 -- is both unnecessary and wrong, because it would cause the
1348 -- clauses to be chained to themselves in the use clauses
1349 -- list of the scope stack entry. That in turn would cause
1350 -- an endless loop from End_Use_Clauses upon scope exit.
1352 -- The parent is now fully visible. It may be a hidden open
1353 -- scope if we are currently compiling some child instance
1354 -- declared within it, but while the current instance is being
1355 -- compiled the parent is immediately visible. In particular
1356 -- its entities must remain visible if a stack save/restore
1357 -- takes place through a call to Rtsfind.
1359 if Present
(Gen_Par
) then
1360 if not In_Private_Part
(Inst_Par
) then
1361 Install_Private_Declarations
(Inst_Par
);
1362 Set_Use
(Private_Declarations
1364 (Unit_Declaration_Node
(Inst_Par
))));
1365 Set_Is_Hidden_Open_Scope
(Inst_Par
, False);
1368 -- If we've reached the end of the generic instance parents,
1369 -- then finish off by looping through the nongeneric parents
1370 -- and installing their private declarations.
1372 -- If one of the non-generic parents is itself on the scope
1373 -- stack, do not install its private declarations: they are
1374 -- installed in due time when the private part of that parent
1378 while Present
(Inst_Par
)
1379 and then Inst_Par
/= Standard_Standard
1380 and then (not In_Open_Scopes
(Inst_Par
)
1381 or else not In_Private_Part
(Inst_Par
))
1383 if Nkind
(Inst_Node
) = N_Formal_Package_Declaration
1385 not Is_Ancestor_Package
1386 (Inst_Par
, Cunit_Entity
(Current_Sem_Unit
))
1388 Install_Private_Declarations
(Inst_Par
);
1390 (Private_Declarations
1392 (Unit_Declaration_Node
(Inst_Par
))));
1393 Inst_Par
:= Scope
(Inst_Par
);
1406 end Install_Parent_Private_Declarations
;
1408 -- Start of processing for Analyze_Package_Specification
1411 if Present
(Vis_Decls
) then
1412 Analyze_Declarations
(Vis_Decls
);
1415 -- Inspect the entities defined in the package and ensure that all
1416 -- incomplete types have received full declarations. Build default
1417 -- initial condition and invariant procedures for all qualifying types.
1419 E
:= First_Entity
(Id
);
1420 while Present
(E
) loop
1422 -- Check on incomplete types
1424 -- AI05-0213: A formal incomplete type has no completion
1426 if Ekind
(E
) = E_Incomplete_Type
1427 and then No
(Full_View
(E
))
1428 and then not Is_Generic_Type
(E
)
1430 Error_Msg_N
("no declaration in visible part for incomplete}", E
);
1436 if Is_Remote_Call_Interface
(Id
)
1437 and then Nkind
(Parent
(Parent
(N
))) = N_Compilation_Unit
1439 Validate_RCI_Declarations
(Id
);
1442 -- Save global references in the visible declarations, before installing
1443 -- private declarations of parent unit if there is one, because the
1444 -- privacy status of types defined in the parent will change. This is
1445 -- only relevant for generic child units, but is done in all cases for
1448 if Ekind
(Id
) = E_Generic_Package
1449 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1452 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1453 Save_Priv
: constant List_Id
:= Private_Declarations
(Orig_Spec
);
1456 -- Insert the freezing nodes after the visible declarations to
1457 -- ensure that we analyze its aspects; needed to ensure that
1458 -- global entities referenced in the aspects are properly handled.
1460 if Ada_Version
>= Ada_2012
1461 and then Is_Non_Empty_List
(Vis_Decls
)
1462 and then Is_Empty_List
(Priv_Decls
)
1464 Insert_List_After_And_Analyze
1465 (Last
(Vis_Decls
), Freeze_Entity
(Id
, Last
(Vis_Decls
)));
1468 Set_Private_Declarations
(Orig_Spec
, Empty_List
);
1469 Save_Global_References
(Orig_Decl
);
1470 Set_Private_Declarations
(Orig_Spec
, Save_Priv
);
1474 -- If package is a public child unit, then make the private declarations
1475 -- of the parent visible.
1477 Public_Child
:= False;
1481 Pack_Decl
: Node_Id
;
1486 Par_Spec
:= Parent_Spec
(Parent
(N
));
1488 -- If the package is formal package of an enclosing generic, it is
1489 -- transformed into a local generic declaration, and compiled to make
1490 -- its spec available. We need to retrieve the original generic to
1491 -- determine whether it is a child unit, and install its parents.
1495 Nkind
(Original_Node
(Parent
(N
))) = N_Formal_Package_Declaration
1497 Par
:= Entity
(Name
(Original_Node
(Parent
(N
))));
1498 Par_Spec
:= Parent_Spec
(Unit_Declaration_Node
(Par
));
1501 if Present
(Par_Spec
) then
1502 Generate_Parent_References
;
1504 while Scope
(Par
) /= Standard_Standard
1505 and then Is_Public_Child
(Id
, Par
)
1506 and then In_Open_Scopes
(Par
)
1508 Public_Child
:= True;
1510 Install_Private_Declarations
(Par
);
1511 Install_Private_With_Clauses
(Par
);
1512 Pack_Decl
:= Unit_Declaration_Node
(Par
);
1513 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
1518 if Is_Compilation_Unit
(Id
) then
1519 Install_Private_With_Clauses
(Id
);
1521 -- The current compilation unit may include private with_clauses,
1522 -- which are visible in the private part of the current nested
1523 -- package, and have to be installed now. This is not done for
1524 -- nested instantiations, where the private with_clauses of the
1525 -- enclosing unit have no effect once the instantiation info is
1526 -- established and we start analyzing the package declaration.
1529 Comp_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
1531 if Is_Package_Or_Generic_Package
(Comp_Unit
)
1532 and then not In_Private_Part
(Comp_Unit
)
1533 and then not In_Instance
1535 Install_Private_With_Clauses
(Comp_Unit
);
1536 Private_With_Clauses_Installed
:= True;
1541 -- If this is a package associated with a generic instance or formal
1542 -- package, then the private declarations of each of the generic's
1543 -- parents must be installed at this point.
1545 if Is_Generic_Instance
(Id
) then
1546 Install_Parent_Private_Declarations
(Id
);
1549 -- Analyze private part if present. The flag In_Private_Part is reset
1550 -- in End_Package_Scope.
1552 L
:= Last_Entity
(Id
);
1554 if Present
(Priv_Decls
) then
1555 Set_In_Private_Part
(Id
);
1557 -- Upon entering a public child's private part, it may be necessary
1558 -- to declare subprograms that were derived in the package's visible
1559 -- part but not yet made visible.
1561 if Public_Child
then
1562 Declare_Inherited_Private_Subprograms
(Id
);
1565 Analyze_Declarations
(Priv_Decls
);
1567 -- Check the private declarations for incomplete deferred constants
1569 Inspect_Deferred_Constant_Completion
(Priv_Decls
);
1571 -- The first private entity is the immediate follower of the last
1572 -- visible entity, if there was one.
1575 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1577 Set_First_Private_Entity
(Id
, First_Entity
(Id
));
1580 -- There may be inherited private subprograms that need to be declared,
1581 -- even in the absence of an explicit private part. If there are any
1582 -- public declarations in the package and the package is a public child
1583 -- unit, then an implicit private part is assumed.
1585 elsif Present
(L
) and then Public_Child
then
1586 Set_In_Private_Part
(Id
);
1587 Declare_Inherited_Private_Subprograms
(Id
);
1588 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1591 E
:= First_Entity
(Id
);
1592 while Present
(E
) loop
1594 -- Check rule of 3.6(11), which in general requires waiting till all
1595 -- full types have been seen.
1597 if Ekind
(E
) = E_Record_Type
or else Ekind
(E
) = E_Array_Type
then
1598 Check_Aliased_Component_Types
(E
);
1601 -- Check preelaborable initialization for full type completing a
1602 -- private type for which pragma Preelaborable_Initialization given.
1605 and then Must_Have_Preelab_Init
(E
)
1606 and then not Has_Preelaborable_Initialization
(E
)
1609 ("full view of & does not have preelaborable initialization", E
);
1615 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1616 -- declaration having a known_discriminant_part shall not be an
1617 -- unchecked union type.
1619 if Present
(Vis_Decls
) then
1620 Inspect_Unchecked_Union_Completion
(Vis_Decls
);
1623 if Present
(Priv_Decls
) then
1624 Inspect_Unchecked_Union_Completion
(Priv_Decls
);
1627 if Ekind
(Id
) = E_Generic_Package
1628 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1629 and then Present
(Priv_Decls
)
1631 -- Save global references in private declarations, ignoring the
1632 -- visible declarations that were processed earlier.
1635 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1636 Save_Vis
: constant List_Id
:= Visible_Declarations
(Orig_Spec
);
1637 Save_Form
: constant List_Id
:=
1638 Generic_Formal_Declarations
(Orig_Decl
);
1641 -- Insert the freezing nodes after the private declarations to
1642 -- ensure that we analyze its aspects; needed to ensure that
1643 -- global entities referenced in the aspects are properly handled.
1645 if Ada_Version
>= Ada_2012
1646 and then Is_Non_Empty_List
(Priv_Decls
)
1648 Insert_List_After_And_Analyze
1649 (Last
(Priv_Decls
), Freeze_Entity
(Id
, Last
(Priv_Decls
)));
1652 Set_Visible_Declarations
(Orig_Spec
, Empty_List
);
1653 Set_Generic_Formal_Declarations
(Orig_Decl
, Empty_List
);
1654 Save_Global_References
(Orig_Decl
);
1655 Set_Generic_Formal_Declarations
(Orig_Decl
, Save_Form
);
1656 Set_Visible_Declarations
(Orig_Spec
, Save_Vis
);
1660 Process_End_Label
(N
, 'e', Id
);
1662 -- Remove private_with_clauses of enclosing compilation unit, if they
1665 if Private_With_Clauses_Installed
then
1666 Remove_Private_With_Clauses
(Cunit
(Current_Sem_Unit
));
1669 -- For the case of a library level package, we must go through all the
1670 -- entities clearing the indications that the value may be constant and
1671 -- not modified. Why? Because any client of this package may modify
1672 -- these values freely from anywhere. This also applies to any nested
1673 -- packages or generic packages.
1675 -- For now we unconditionally clear constants for packages that are
1676 -- instances of generic packages. The reason is that we do not have the
1677 -- body yet, and we otherwise think things are unreferenced when they
1678 -- are not. This should be fixed sometime (the effect is not terrible,
1679 -- we just lose some warnings, and also some cases of value propagation)
1682 if Is_Library_Level_Entity
(Id
)
1683 or else Is_Generic_Instance
(Id
)
1685 Clear_Constants
(Id
, First_Entity
(Id
));
1686 Clear_Constants
(Id
, First_Private_Entity
(Id
));
1689 -- Issue an error in SPARK mode if a package specification contains
1690 -- more than one tagged type or type extension.
1692 Check_One_Tagged_Type_Or_Extension_At_Most
;
1694 -- If switch set, output information on why body required
1696 if List_Body_Required_Info
1697 and then In_Extended_Main_Source_Unit
(Id
)
1698 and then Unit_Requires_Body
(Id
)
1700 Unit_Requires_Body_Info
(Id
);
1702 end Analyze_Package_Specification
;
1704 --------------------------------------
1705 -- Analyze_Private_Type_Declaration --
1706 --------------------------------------
1708 procedure Analyze_Private_Type_Declaration
(N
: Node_Id
) is
1709 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1710 PF
: constant Boolean := Is_Pure
(Enclosing_Lib_Unit_Entity
);
1713 Generate_Definition
(Id
);
1714 Set_Is_Pure
(Id
, PF
);
1715 Init_Size_Align
(Id
);
1717 if not Is_Package_Or_Generic_Package
(Current_Scope
)
1718 or else In_Private_Part
(Current_Scope
)
1720 Error_Msg_N
("invalid context for private declaration", N
);
1723 New_Private_Type
(N
, Id
, N
);
1724 Set_Depends_On_Private
(Id
);
1726 if Has_Aspects
(N
) then
1727 Analyze_Aspect_Specifications
(N
, Id
);
1729 end Analyze_Private_Type_Declaration
;
1731 ----------------------------------
1732 -- Check_Anonymous_Access_Types --
1733 ----------------------------------
1735 procedure Check_Anonymous_Access_Types
1736 (Spec_Id
: Entity_Id
;
1743 -- Itype references are only needed by gigi, to force elaboration of
1744 -- itypes. In the absence of code generation, they are not needed.
1746 if not Expander_Active
then
1750 E
:= First_Entity
(Spec_Id
);
1751 while Present
(E
) loop
1752 if Ekind
(E
) = E_Anonymous_Access_Type
1753 and then From_Limited_With
(E
)
1755 IR
:= Make_Itype_Reference
(Sloc
(P_Body
));
1758 if No
(Declarations
(P_Body
)) then
1759 Set_Declarations
(P_Body
, New_List
(IR
));
1761 Prepend
(IR
, Declarations
(P_Body
));
1767 end Check_Anonymous_Access_Types
;
1769 -------------------------------------------
1770 -- Declare_Inherited_Private_Subprograms --
1771 -------------------------------------------
1773 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
) is
1775 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean;
1776 -- Check whether an inherited subprogram S is an operation of an
1777 -- untagged derived type T.
1779 ---------------------
1780 -- Is_Primitive_Of --
1781 ---------------------
1783 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean is
1787 -- If the full view is a scalar type, the type is the anonymous base
1788 -- type, but the operation mentions the first subtype, so check the
1789 -- signature against the base type.
1791 if Base_Type
(Etype
(S
)) = Base_Type
(T
) then
1795 Formal
:= First_Formal
(S
);
1796 while Present
(Formal
) loop
1797 if Base_Type
(Etype
(Formal
)) = Base_Type
(T
) then
1801 Next_Formal
(Formal
);
1806 end Is_Primitive_Of
;
1813 Op_Elmt_2
: Elmt_Id
;
1814 Prim_Op
: Entity_Id
;
1815 New_Op
: Entity_Id
:= Empty
;
1816 Parent_Subp
: Entity_Id
;
1819 -- Start of processing for Declare_Inherited_Private_Subprograms
1822 E
:= First_Entity
(Id
);
1823 while Present
(E
) loop
1825 -- If the entity is a nonprivate type extension whose parent type
1826 -- is declared in an open scope, then the type may have inherited
1827 -- operations that now need to be made visible. Ditto if the entity
1828 -- is a formal derived type in a child unit.
1830 if ((Is_Derived_Type
(E
) and then not Is_Private_Type
(E
))
1832 (Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
1833 and then Is_Generic_Type
(E
)))
1834 and then In_Open_Scopes
(Scope
(Etype
(E
)))
1835 and then Is_Base_Type
(E
)
1837 if Is_Tagged_Type
(E
) then
1838 Op_List
:= Primitive_Operations
(E
);
1840 Tag
:= First_Tag_Component
(E
);
1842 Op_Elmt
:= First_Elmt
(Op_List
);
1843 while Present
(Op_Elmt
) loop
1844 Prim_Op
:= Node
(Op_Elmt
);
1846 -- Search primitives that are implicit operations with an
1847 -- internal name whose parent operation has a normal name.
1849 if Present
(Alias
(Prim_Op
))
1850 and then Find_Dispatching_Type
(Alias
(Prim_Op
)) /= E
1851 and then not Comes_From_Source
(Prim_Op
)
1852 and then Is_Internal_Name
(Chars
(Prim_Op
))
1853 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
1855 Parent_Subp
:= Alias
(Prim_Op
);
1857 -- Case 1: Check if the type has also an explicit
1858 -- overriding for this primitive.
1860 Op_Elmt_2
:= Next_Elmt
(Op_Elmt
);
1861 while Present
(Op_Elmt_2
) loop
1863 -- Skip entities with attribute Interface_Alias since
1864 -- they are not overriding primitives (these entities
1865 -- link an interface primitive with their covering
1868 if Chars
(Node
(Op_Elmt_2
)) = Chars
(Parent_Subp
)
1869 and then Type_Conformant
(Prim_Op
, Node
(Op_Elmt_2
))
1870 and then No
(Interface_Alias
(Node
(Op_Elmt_2
)))
1872 -- The private inherited operation has been
1873 -- overridden by an explicit subprogram:
1874 -- replace the former by the latter.
1876 New_Op
:= Node
(Op_Elmt_2
);
1877 Replace_Elmt
(Op_Elmt
, New_Op
);
1878 Remove_Elmt
(Op_List
, Op_Elmt_2
);
1879 Set_Overridden_Operation
(New_Op
, Parent_Subp
);
1881 -- We don't need to inherit its dispatching slot.
1882 -- Set_All_DT_Position has previously ensured that
1883 -- the same slot was assigned to the two primitives
1886 and then Present
(DTC_Entity
(New_Op
))
1887 and then Present
(DTC_Entity
(Prim_Op
))
1890 (DT_Position
(New_Op
) = DT_Position
(Prim_Op
));
1894 goto Next_Primitive
;
1897 Next_Elmt
(Op_Elmt_2
);
1900 -- Case 2: We have not found any explicit overriding and
1901 -- hence we need to declare the operation (i.e., make it
1904 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
1906 -- Inherit the dispatching slot if E is already frozen
1909 and then Present
(DTC_Entity
(Alias
(Prim_Op
)))
1911 Set_DTC_Entity_Value
(E
, New_Op
);
1912 Set_DT_Position_Value
(New_Op
,
1913 DT_Position
(Alias
(Prim_Op
)));
1917 (Is_Dispatching_Operation
(New_Op
)
1918 and then Node
(Last_Elmt
(Op_List
)) = New_Op
);
1920 -- Substitute the new operation for the old one in the
1921 -- type's primitive operations list. Since the new
1922 -- operation was also just added to the end of list,
1923 -- the last element must be removed.
1925 -- (Question: is there a simpler way of declaring the
1926 -- operation, say by just replacing the name of the
1927 -- earlier operation, reentering it in the in the symbol
1928 -- table (how?), and marking it as private???)
1930 Replace_Elmt
(Op_Elmt
, New_Op
);
1931 Remove_Last_Elmt
(Op_List
);
1935 Next_Elmt
(Op_Elmt
);
1938 -- Generate listing showing the contents of the dispatch table
1940 if Debug_Flag_ZZ
then
1945 -- For untagged type, scan forward to locate inherited hidden
1948 Prim_Op
:= Next_Entity
(E
);
1949 while Present
(Prim_Op
) loop
1950 if Is_Subprogram
(Prim_Op
)
1951 and then Present
(Alias
(Prim_Op
))
1952 and then not Comes_From_Source
(Prim_Op
)
1953 and then Is_Internal_Name
(Chars
(Prim_Op
))
1954 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
1955 and then Is_Primitive_Of
(E
, Prim_Op
)
1957 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
1960 Next_Entity
(Prim_Op
);
1962 -- Derived operations appear immediately after the type
1963 -- declaration (or the following subtype indication for
1964 -- a derived scalar type). Further declarations cannot
1965 -- include inherited operations of the type.
1967 if Present
(Prim_Op
) then
1968 exit when Ekind
(Prim_Op
) not in Overloadable_Kind
;
1976 end Declare_Inherited_Private_Subprograms
;
1978 -----------------------
1979 -- End_Package_Scope --
1980 -----------------------
1982 procedure End_Package_Scope
(P
: Entity_Id
) is
1984 Uninstall_Declarations
(P
);
1986 end End_Package_Scope
;
1988 ---------------------------
1989 -- Exchange_Declarations --
1990 ---------------------------
1992 procedure Exchange_Declarations
(Id
: Entity_Id
) is
1993 Full_Id
: constant Entity_Id
:= Full_View
(Id
);
1994 H1
: constant Entity_Id
:= Homonym
(Id
);
1995 Next1
: constant Entity_Id
:= Next_Entity
(Id
);
2000 -- If missing full declaration for type, nothing to exchange
2002 if No
(Full_Id
) then
2006 -- Otherwise complete the exchange, and preserve semantic links
2008 Next2
:= Next_Entity
(Full_Id
);
2009 H2
:= Homonym
(Full_Id
);
2011 -- Reset full declaration pointer to reflect the switched entities and
2012 -- readjust the next entity chains.
2014 Exchange_Entities
(Id
, Full_Id
);
2016 Set_Next_Entity
(Id
, Next1
);
2017 Set_Homonym
(Id
, H1
);
2019 Set_Full_View
(Full_Id
, Id
);
2020 Set_Next_Entity
(Full_Id
, Next2
);
2021 Set_Homonym
(Full_Id
, H2
);
2022 end Exchange_Declarations
;
2024 ----------------------------
2025 -- Install_Package_Entity --
2026 ----------------------------
2028 procedure Install_Package_Entity
(Id
: Entity_Id
) is
2030 if not Is_Internal
(Id
) then
2031 if Debug_Flag_E
then
2032 Write_Str
("Install: ");
2033 Write_Name
(Chars
(Id
));
2037 if Is_Child_Unit
(Id
) then
2040 -- Do not enter implicitly inherited non-overridden subprograms of
2041 -- a tagged type back into visibility if they have non-conformant
2042 -- homographs (Ada RM 8.3 12.3/2).
2044 elsif Is_Hidden_Non_Overridden_Subpgm
(Id
) then
2048 Set_Is_Immediately_Visible
(Id
);
2051 end Install_Package_Entity
;
2053 ----------------------------------
2054 -- Install_Private_Declarations --
2055 ----------------------------------
2057 procedure Install_Private_Declarations
(P
: Entity_Id
) is
2060 Priv_Deps
: Elist_Id
;
2062 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2063 -- When the full view of a private type is made available, we do the
2064 -- same for its private dependents under proper visibility conditions.
2065 -- When compiling a grand-chid unit this needs to be done recursively.
2067 -----------------------------
2068 -- Swap_Private_Dependents --
2069 -----------------------------
2071 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
2074 Priv_Elmt
: Elmt_Id
;
2078 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
2079 while Present
(Priv_Elmt
) loop
2080 Priv
:= Node
(Priv_Elmt
);
2082 -- Before the exchange, verify that the presence of the Full_View
2083 -- field. This field will be empty if the entity has already been
2084 -- installed due to a previous call.
2086 if Present
(Full_View
(Priv
)) and then Is_Visible_Dependent
(Priv
)
2088 if Is_Private_Type
(Priv
) then
2089 Deps
:= Private_Dependents
(Priv
);
2095 -- For each subtype that is swapped, we also swap the reference
2096 -- to it in Private_Dependents, to allow access to it when we
2097 -- swap them out in End_Package_Scope.
2099 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
2101 -- Ensure that both views of the dependent private subtype are
2102 -- immediately visible if within some open scope. Check full
2103 -- view before exchanging views.
2105 if In_Open_Scopes
(Scope
(Full_View
(Priv
))) then
2106 Set_Is_Immediately_Visible
(Priv
);
2109 Exchange_Declarations
(Priv
);
2110 Set_Is_Immediately_Visible
2111 (Priv
, In_Open_Scopes
(Scope
(Priv
)));
2113 Set_Is_Potentially_Use_Visible
2114 (Priv
, Is_Potentially_Use_Visible
(Node
(Priv_Elmt
)));
2116 -- Within a child unit, recurse, except in generic child unit,
2117 -- which (unfortunately) handle private_dependents separately.
2120 and then Is_Child_Unit
(Cunit_Entity
(Current_Sem_Unit
))
2121 and then not Is_Empty_Elmt_List
(Deps
)
2122 and then not Inside_A_Generic
2124 Swap_Private_Dependents
(Deps
);
2128 Next_Elmt
(Priv_Elmt
);
2130 end Swap_Private_Dependents
;
2132 -- Start of processing for Install_Private_Declarations
2135 -- First exchange declarations for private types, so that the full
2136 -- declaration is visible. For each private type, we check its
2137 -- Private_Dependents list and also exchange any subtypes of or derived
2138 -- types from it. Finally, if this is a Taft amendment type, the
2139 -- incomplete declaration is irrelevant, and we want to link the
2140 -- eventual full declaration with the original private one so we
2141 -- also skip the exchange.
2143 Id
:= First_Entity
(P
);
2144 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2145 if Is_Private_Base_Type
(Id
)
2146 and then Present
(Full_View
(Id
))
2147 and then Comes_From_Source
(Full_View
(Id
))
2148 and then Scope
(Full_View
(Id
)) = Scope
(Id
)
2149 and then Ekind
(Full_View
(Id
)) /= E_Incomplete_Type
2151 -- If there is a use-type clause on the private type, set the full
2152 -- view accordingly.
2154 Set_In_Use
(Full_View
(Id
), In_Use
(Id
));
2155 Full
:= Full_View
(Id
);
2157 if Is_Private_Base_Type
(Full
)
2158 and then Has_Private_Declaration
(Full
)
2159 and then Nkind
(Parent
(Full
)) = N_Full_Type_Declaration
2160 and then In_Open_Scopes
(Scope
(Etype
(Full
)))
2161 and then In_Package_Body
(Current_Scope
)
2162 and then not Is_Private_Type
(Etype
(Full
))
2164 -- This is the completion of a private type by a derivation
2165 -- from another private type which is not private anymore. This
2166 -- can only happen in a package nested within a child package,
2167 -- when the parent type is defined in the parent unit. At this
2168 -- point the current type is not private either, and we have
2169 -- to install the underlying full view, which is now visible.
2170 -- Save the current full view as well, so that all views can be
2171 -- restored on exit. It may seem that after compiling the child
2172 -- body there are not environments to restore, but the back-end
2173 -- expects those links to be valid, and freeze nodes depend on
2176 if No
(Full_View
(Full
))
2177 and then Present
(Underlying_Full_View
(Full
))
2179 Set_Full_View
(Id
, Underlying_Full_View
(Full
));
2180 Set_Underlying_Full_View
(Id
, Full
);
2181 Set_Is_Underlying_Full_View
(Full
);
2183 Set_Underlying_Full_View
(Full
, Empty
);
2184 Set_Is_Frozen
(Full_View
(Id
));
2188 Priv_Deps
:= Private_Dependents
(Id
);
2189 Exchange_Declarations
(Id
);
2190 Set_Is_Immediately_Visible
(Id
);
2191 Swap_Private_Dependents
(Priv_Deps
);
2197 -- Next make other declarations in the private part visible as well
2199 Id
:= First_Private_Entity
(P
);
2200 while Present
(Id
) loop
2201 Install_Package_Entity
(Id
);
2202 Set_Is_Hidden
(Id
, False);
2206 -- An abstract state is partially refined when it has at least one
2207 -- Part_Of constituent. Since these constituents are being installed
2208 -- into visibility, update the partial refinement status of any state
2209 -- defined in the associated package, subject to at least one Part_Of
2212 if Ekind_In
(P
, E_Generic_Package
, E_Package
) then
2214 States
: constant Elist_Id
:= Abstract_States
(P
);
2215 State_Elmt
: Elmt_Id
;
2216 State_Id
: Entity_Id
;
2219 if Present
(States
) then
2220 State_Elmt
:= First_Elmt
(States
);
2221 while Present
(State_Elmt
) loop
2222 State_Id
:= Node
(State_Elmt
);
2224 if Present
(Part_Of_Constituents
(State_Id
)) then
2225 Set_Has_Partial_Visible_Refinement
(State_Id
);
2228 Next_Elmt
(State_Elmt
);
2234 -- Indicate that the private part is currently visible, so it can be
2235 -- properly reset on exit.
2237 Set_In_Private_Part
(P
);
2238 end Install_Private_Declarations
;
2240 ----------------------------------
2241 -- Install_Visible_Declarations --
2242 ----------------------------------
2244 procedure Install_Visible_Declarations
(P
: Entity_Id
) is
2246 Last_Entity
: Entity_Id
;
2250 (Is_Package_Or_Generic_Package
(P
) or else Is_Record_Type
(P
));
2252 if Is_Package_Or_Generic_Package
(P
) then
2253 Last_Entity
:= First_Private_Entity
(P
);
2255 Last_Entity
:= Empty
;
2258 Id
:= First_Entity
(P
);
2259 while Present
(Id
) and then Id
/= Last_Entity
loop
2260 Install_Package_Entity
(Id
);
2263 end Install_Visible_Declarations
;
2265 --------------------------
2266 -- Is_Private_Base_Type --
2267 --------------------------
2269 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean is
2271 return Ekind
(E
) = E_Private_Type
2272 or else Ekind
(E
) = E_Limited_Private_Type
2273 or else Ekind
(E
) = E_Record_Type_With_Private
;
2274 end Is_Private_Base_Type
;
2276 --------------------------
2277 -- Is_Visible_Dependent --
2278 --------------------------
2280 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean
2282 S
: constant Entity_Id
:= Scope
(Dep
);
2285 -- Renamings created for actual types have the visibility of the actual
2287 if Ekind
(S
) = E_Package
2288 and then Is_Generic_Instance
(S
)
2289 and then (Is_Generic_Actual_Type
(Dep
)
2290 or else Is_Generic_Actual_Type
(Full_View
(Dep
)))
2294 elsif not (Is_Derived_Type
(Dep
))
2295 and then Is_Derived_Type
(Full_View
(Dep
))
2297 -- When instantiating a package body, the scope stack is empty, so
2298 -- check instead whether the dependent type is defined in the same
2299 -- scope as the instance itself.
2301 return In_Open_Scopes
(S
)
2302 or else (Is_Generic_Instance
(Current_Scope
)
2303 and then Scope
(Dep
) = Scope
(Current_Scope
));
2307 end Is_Visible_Dependent
;
2309 ----------------------------
2310 -- May_Need_Implicit_Body --
2311 ----------------------------
2313 procedure May_Need_Implicit_Body
(E
: Entity_Id
) is
2314 P
: constant Node_Id
:= Unit_Declaration_Node
(E
);
2315 S
: constant Node_Id
:= Parent
(P
);
2320 if not Has_Completion
(E
)
2321 and then Nkind
(P
) = N_Package_Declaration
2322 and then (Present
(Activation_Chain_Entity
(P
)) or else Has_RACW
(E
))
2325 Make_Package_Body
(Sloc
(E
),
2326 Defining_Unit_Name
=> Make_Defining_Identifier
(Sloc
(E
),
2327 Chars
=> Chars
(E
)),
2328 Declarations
=> New_List
);
2330 if Nkind
(S
) = N_Package_Specification
then
2331 if Present
(Private_Declarations
(S
)) then
2332 Decls
:= Private_Declarations
(S
);
2334 Decls
:= Visible_Declarations
(S
);
2337 Decls
:= Declarations
(S
);
2343 end May_Need_Implicit_Body
;
2345 ----------------------
2346 -- New_Private_Type --
2347 ----------------------
2349 procedure New_Private_Type
(N
: Node_Id
; Id
: Entity_Id
; Def
: Node_Id
) is
2351 -- For other than Ada 2012, enter the name in the current scope
2353 if Ada_Version
< Ada_2012
then
2356 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2357 -- there may be an incomplete previous view.
2363 Prev
:= Find_Type_Name
(N
);
2364 pragma Assert
(Prev
= Id
2365 or else (Ekind
(Prev
) = E_Incomplete_Type
2366 and then Present
(Full_View
(Prev
))
2367 and then Full_View
(Prev
) = Id
));
2371 if Limited_Present
(Def
) then
2372 Set_Ekind
(Id
, E_Limited_Private_Type
);
2374 Set_Ekind
(Id
, E_Private_Type
);
2378 Set_Has_Delayed_Freeze
(Id
);
2379 Set_Is_First_Subtype
(Id
);
2380 Init_Size_Align
(Id
);
2382 Set_Is_Constrained
(Id
,
2383 No
(Discriminant_Specifications
(N
))
2384 and then not Unknown_Discriminants_Present
(N
));
2386 -- Set tagged flag before processing discriminants, to catch illegal
2389 Set_Is_Tagged_Type
(Id
, Tagged_Present
(Def
));
2391 Set_Discriminant_Constraint
(Id
, No_Elist
);
2392 Set_Stored_Constraint
(Id
, No_Elist
);
2394 if Present
(Discriminant_Specifications
(N
)) then
2396 Process_Discriminants
(N
);
2399 elsif Unknown_Discriminants_Present
(N
) then
2400 Set_Has_Unknown_Discriminants
(Id
);
2403 Set_Private_Dependents
(Id
, New_Elmt_List
);
2405 if Tagged_Present
(Def
) then
2406 Set_Ekind
(Id
, E_Record_Type_With_Private
);
2407 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2408 Set_Is_Abstract_Type
(Id
, Abstract_Present
(Def
));
2409 Set_Is_Limited_Record
(Id
, Limited_Present
(Def
));
2410 Set_Has_Delayed_Freeze
(Id
, True);
2412 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2414 if Is_RTE
(Id
, RE_Timing_Event
) then
2415 Set_Has_Timing_Event
(Id
);
2418 -- Create a class-wide type with the same attributes
2420 Make_Class_Wide_Type
(Id
);
2422 elsif Abstract_Present
(Def
) then
2423 Error_Msg_N
("only a tagged type can be abstract", N
);
2425 end New_Private_Type
;
2427 ---------------------------------
2428 -- Requires_Completion_In_Body --
2429 ---------------------------------
2431 function Requires_Completion_In_Body
2433 Pack_Id
: Entity_Id
;
2434 Do_Abstract_States
: Boolean := False) return Boolean
2437 -- Always ignore child units. Child units get added to the entity list
2438 -- of a parent unit, but are not original entities of the parent, and
2439 -- so do not affect whether the parent needs a body.
2441 if Is_Child_Unit
(Id
) then
2444 -- Ignore formal packages and their renamings
2446 elsif Ekind
(Id
) = E_Package
2447 and then Nkind
(Original_Node
(Unit_Declaration_Node
(Id
))) =
2448 N_Formal_Package_Declaration
2452 -- Otherwise test to see if entity requires a completion. Note that
2453 -- subprogram entities whose declaration does not come from source are
2454 -- ignored here on the basis that we assume the expander will provide an
2455 -- implicit completion at some point.
2457 elsif (Is_Overloadable
(Id
)
2458 and then not Ekind_In
(Id
, E_Enumeration_Literal
, E_Operator
)
2459 and then not Is_Abstract_Subprogram
(Id
)
2460 and then not Has_Completion
(Id
)
2461 and then Comes_From_Source
(Parent
(Id
)))
2464 (Ekind
(Id
) = E_Package
2465 and then Id
/= Pack_Id
2466 and then not Has_Completion
(Id
)
2467 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2470 (Ekind
(Id
) = E_Incomplete_Type
2471 and then No
(Full_View
(Id
))
2472 and then not Is_Generic_Type
(Id
))
2475 (Ekind_In
(Id
, E_Task_Type
, E_Protected_Type
)
2476 and then not Has_Completion
(Id
))
2479 (Ekind
(Id
) = E_Generic_Package
2480 and then Id
/= Pack_Id
2481 and then not Has_Completion
(Id
)
2482 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2485 (Is_Generic_Subprogram
(Id
)
2486 and then not Has_Completion
(Id
))
2490 -- Otherwise the entity does not require completion in a package body
2495 end Requires_Completion_In_Body
;
2497 ----------------------------
2498 -- Uninstall_Declarations --
2499 ----------------------------
2501 procedure Uninstall_Declarations
(P
: Entity_Id
) is
2502 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
2505 Priv_Elmt
: Elmt_Id
;
2506 Priv_Sub
: Entity_Id
;
2508 procedure Preserve_Full_Attributes
(Priv
: Entity_Id
; Full
: Entity_Id
);
2509 -- Copy to the private declaration the attributes of the full view that
2510 -- need to be available for the partial view also.
2512 function Type_In_Use
(T
: Entity_Id
) return Boolean;
2513 -- Check whether type or base type appear in an active use_type clause
2515 ------------------------------
2516 -- Preserve_Full_Attributes --
2517 ------------------------------
2519 procedure Preserve_Full_Attributes
2523 Full_Base
: constant Entity_Id
:= Base_Type
(Full
);
2524 Priv_Is_Base_Type
: constant Boolean := Is_Base_Type
(Priv
);
2527 Set_Size_Info
(Priv
, Full
);
2528 Set_RM_Size
(Priv
, RM_Size
(Full
));
2529 Set_Size_Known_At_Compile_Time
2530 (Priv
, Size_Known_At_Compile_Time
(Full
));
2531 Set_Is_Volatile
(Priv
, Is_Volatile
(Full
));
2532 Set_Treat_As_Volatile
(Priv
, Treat_As_Volatile
(Full
));
2533 Set_Is_Ada_2005_Only
(Priv
, Is_Ada_2005_Only
(Full
));
2534 Set_Is_Ada_2012_Only
(Priv
, Is_Ada_2012_Only
(Full
));
2535 Set_Has_Pragma_Unmodified
(Priv
, Has_Pragma_Unmodified
(Full
));
2536 Set_Has_Pragma_Unreferenced
(Priv
, Has_Pragma_Unreferenced
(Full
));
2537 Set_Has_Pragma_Unreferenced_Objects
2538 (Priv
, Has_Pragma_Unreferenced_Objects
2540 if Is_Unchecked_Union
(Full
) then
2541 Set_Is_Unchecked_Union
(Base_Type
(Priv
));
2543 -- Why is atomic not copied here ???
2545 if Referenced
(Full
) then
2546 Set_Referenced
(Priv
);
2549 if Priv_Is_Base_Type
then
2550 Set_Is_Controlled
(Priv
, Is_Controlled
(Full_Base
));
2551 Set_Finalize_Storage_Only
2552 (Priv
, Finalize_Storage_Only
(Full_Base
));
2553 Set_Has_Controlled_Component
2554 (Priv
, Has_Controlled_Component
(Full_Base
));
2556 Propagate_Concurrent_Flags
(Priv
, Base_Type
(Full
));
2559 Set_Freeze_Node
(Priv
, Freeze_Node
(Full
));
2561 -- Propagate Default_Initial_Condition-related attributes from the
2562 -- base type of the full view to the full view and vice versa. This
2563 -- may seem strange, but is necessary depending on which type
2564 -- triggered the generation of the DIC procedure body. As a result,
2565 -- both the full view and its base type carry the same DIC-related
2568 Propagate_DIC_Attributes
(Full
, From_Typ
=> Full_Base
);
2569 Propagate_DIC_Attributes
(Full_Base
, From_Typ
=> Full
);
2571 -- Propagate invariant-related attributes from the base type of the
2572 -- full view to the full view and vice versa. This may seem strange,
2573 -- but is necessary depending on which type triggered the generation
2574 -- of the invariant procedure body. As a result, both the full view
2575 -- and its base type carry the same invariant-related information.
2577 Propagate_Invariant_Attributes
(Full
, From_Typ
=> Full_Base
);
2578 Propagate_Invariant_Attributes
(Full_Base
, From_Typ
=> Full
);
2580 -- Propagate invariant-related attributes from the full view to the
2583 Propagate_Invariant_Attributes
(Priv
, From_Typ
=> Full
);
2585 if Is_Tagged_Type
(Priv
)
2586 and then Is_Tagged_Type
(Full
)
2587 and then not Error_Posted
(Full
)
2589 if Is_Tagged_Type
(Priv
) then
2591 -- If the type is tagged, the tag itself must be available on
2592 -- the partial view, for expansion purposes.
2594 Set_First_Entity
(Priv
, First_Entity
(Full
));
2596 -- If there are discriminants in the partial view, these remain
2597 -- visible. Otherwise only the tag itself is visible, and there
2598 -- are no nameable components in the partial view.
2600 if No
(Last_Entity
(Priv
)) then
2601 Set_Last_Entity
(Priv
, First_Entity
(Priv
));
2605 Set_Has_Discriminants
(Priv
, Has_Discriminants
(Full
));
2607 if Has_Discriminants
(Full
) then
2608 Set_Discriminant_Constraint
(Priv
,
2609 Discriminant_Constraint
(Full
));
2612 end Preserve_Full_Attributes
;
2618 function Type_In_Use
(T
: Entity_Id
) return Boolean is
2620 return Scope
(Base_Type
(T
)) = P
2621 and then (In_Use
(T
) or else In_Use
(Base_Type
(T
)));
2624 -- Start of processing for Uninstall_Declarations
2627 Id
:= First_Entity
(P
);
2628 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2629 if Debug_Flag_E
then
2630 Write_Str
("unlinking visible entity ");
2631 Write_Int
(Int
(Id
));
2635 -- On exit from the package scope, we must preserve the visibility
2636 -- established by use clauses in the current scope. Two cases:
2638 -- a) If the entity is an operator, it may be a primitive operator of
2639 -- a type for which there is a visible use-type clause.
2641 -- b) for other entities, their use-visibility is determined by a
2642 -- visible use clause for the package itself. For a generic instance,
2643 -- the instantiation of the formals appears in the visible part,
2644 -- but the formals are private and remain so.
2646 if Ekind
(Id
) = E_Function
2647 and then Is_Operator_Symbol_Name
(Chars
(Id
))
2648 and then not Is_Hidden
(Id
)
2649 and then not Error_Posted
(Id
)
2651 Set_Is_Potentially_Use_Visible
(Id
,
2653 or else Type_In_Use
(Etype
(Id
))
2654 or else Type_In_Use
(Etype
(First_Formal
(Id
)))
2655 or else (Present
(Next_Formal
(First_Formal
(Id
)))
2658 (Etype
(Next_Formal
(First_Formal
(Id
))))));
2660 if In_Use
(P
) and then not Is_Hidden
(Id
) then
2662 -- A child unit of a use-visible package remains use-visible
2663 -- only if it is itself a visible child unit. Otherwise it
2664 -- would remain visible in other contexts where P is use-
2665 -- visible, because once compiled it stays in the entity list
2666 -- of its parent unit.
2668 if Is_Child_Unit
(Id
) then
2669 Set_Is_Potentially_Use_Visible
2670 (Id
, Is_Visible_Lib_Unit
(Id
));
2672 Set_Is_Potentially_Use_Visible
(Id
);
2676 Set_Is_Potentially_Use_Visible
(Id
, False);
2680 -- Local entities are not immediately visible outside of the package
2682 Set_Is_Immediately_Visible
(Id
, False);
2684 -- If this is a private type with a full view (for example a local
2685 -- subtype of a private type declared elsewhere), ensure that the
2686 -- full view is also removed from visibility: it may be exposed when
2687 -- swapping views in an instantiation. Similarly, ensure that the
2688 -- use-visibility is properly set on both views.
2690 if Is_Type
(Id
) and then Present
(Full_View
(Id
)) then
2691 Set_Is_Immediately_Visible
(Full_View
(Id
), False);
2692 Set_Is_Potentially_Use_Visible
(Full_View
(Id
),
2693 Is_Potentially_Use_Visible
(Id
));
2696 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
2697 Check_Abstract_Overriding
(Id
);
2698 Check_Conventions
(Id
);
2701 if Ekind_In
(Id
, E_Private_Type
, E_Limited_Private_Type
)
2702 and then No
(Full_View
(Id
))
2703 and then not Is_Generic_Type
(Id
)
2704 and then not Is_Derived_Type
(Id
)
2706 Error_Msg_N
("missing full declaration for private type&", Id
);
2708 elsif Ekind
(Id
) = E_Record_Type_With_Private
2709 and then not Is_Generic_Type
(Id
)
2710 and then No
(Full_View
(Id
))
2712 if Nkind
(Parent
(Id
)) = N_Private_Type_Declaration
then
2713 Error_Msg_N
("missing full declaration for private type&", Id
);
2716 ("missing full declaration for private extension", Id
);
2719 -- Case of constant, check for deferred constant declaration with
2720 -- no full view. Likely just a matter of a missing expression, or
2721 -- accidental use of the keyword constant.
2723 elsif Ekind
(Id
) = E_Constant
2725 -- OK if constant value present
2727 and then No
(Constant_Value
(Id
))
2729 -- OK if full view present
2731 and then No
(Full_View
(Id
))
2733 -- OK if imported, since that provides the completion
2735 and then not Is_Imported
(Id
)
2737 -- OK if object declaration replaced by renaming declaration as
2738 -- a result of OK_To_Rename processing (e.g. for concatenation)
2740 and then Nkind
(Parent
(Id
)) /= N_Object_Renaming_Declaration
2742 -- OK if object declaration with the No_Initialization flag set
2744 and then not (Nkind
(Parent
(Id
)) = N_Object_Declaration
2745 and then No_Initialization
(Parent
(Id
)))
2747 -- If no private declaration is present, we assume the user did
2748 -- not intend a deferred constant declaration and the problem
2749 -- is simply that the initializing expression is missing.
2751 if not Has_Private_Declaration
(Etype
(Id
)) then
2753 -- We assume that the user did not intend a deferred constant
2754 -- declaration, and the expression is just missing.
2757 ("constant declaration requires initialization expression",
2760 if Is_Limited_Type
(Etype
(Id
)) then
2762 ("\if variable intended, remove CONSTANT from declaration",
2766 -- Otherwise if a private declaration is present, then we are
2767 -- missing the full declaration for the deferred constant.
2771 ("missing full declaration for deferred constant (RM 7.4)",
2774 if Is_Limited_Type
(Etype
(Id
)) then
2776 ("\if variable intended, remove CONSTANT from declaration",
2785 -- If the specification was installed as the parent of a public child
2786 -- unit, the private declarations were not installed, and there is
2789 if not In_Private_Part
(P
) then
2792 Set_In_Private_Part
(P
, False);
2795 -- Make private entities invisible and exchange full and private
2796 -- declarations for private types. Id is now the first private entity
2799 while Present
(Id
) loop
2800 if Debug_Flag_E
then
2801 Write_Str
("unlinking private entity ");
2802 Write_Int
(Int
(Id
));
2806 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
2807 Check_Abstract_Overriding
(Id
);
2808 Check_Conventions
(Id
);
2811 Set_Is_Immediately_Visible
(Id
, False);
2813 if Is_Private_Base_Type
(Id
) and then Present
(Full_View
(Id
)) then
2814 Full
:= Full_View
(Id
);
2816 -- If the partial view is not declared in the visible part of the
2817 -- package (as is the case when it is a type derived from some
2818 -- other private type in the private part of the current package),
2819 -- no exchange takes place.
2822 or else List_Containing
(Parent
(Id
)) /=
2823 Visible_Declarations
(Specification
(Decl
))
2828 -- The entry in the private part points to the full declaration,
2829 -- which is currently visible. Exchange them so only the private
2830 -- type declaration remains accessible, and link private and full
2831 -- declaration in the opposite direction. Before the actual
2832 -- exchange, we copy back attributes of the full view that must
2833 -- be available to the partial view too.
2835 Preserve_Full_Attributes
(Id
, Full
);
2837 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(P
));
2839 -- The following test may be redundant, as this is already
2840 -- diagnosed in sem_ch3. ???
2842 if not Is_Definite_Subtype
(Full
)
2843 and then Is_Definite_Subtype
(Id
)
2845 Error_Msg_Sloc
:= Sloc
(Parent
(Id
));
2847 ("full view of& not compatible with declaration#", Full
, Id
);
2850 -- Swap out the subtypes and derived types of Id that
2851 -- were compiled in this scope, or installed previously
2852 -- by Install_Private_Declarations.
2854 -- Before we do the swap, we verify the presence of the Full_View
2855 -- field which may be empty due to a swap by a previous call to
2856 -- End_Package_Scope (e.g. from the freezing mechanism).
2858 Priv_Elmt
:= First_Elmt
(Private_Dependents
(Id
));
2859 while Present
(Priv_Elmt
) loop
2860 Priv_Sub
:= Node
(Priv_Elmt
);
2862 if Present
(Full_View
(Priv_Sub
)) then
2863 if Scope
(Priv_Sub
) = P
2864 or else not In_Open_Scopes
(Scope
(Priv_Sub
))
2866 Set_Is_Immediately_Visible
(Priv_Sub
, False);
2869 if Is_Visible_Dependent
(Priv_Sub
) then
2870 Preserve_Full_Attributes
2871 (Priv_Sub
, Full_View
(Priv_Sub
));
2872 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv_Sub
));
2873 Exchange_Declarations
(Priv_Sub
);
2877 Next_Elmt
(Priv_Elmt
);
2880 -- Now restore the type itself to its private view
2882 Exchange_Declarations
(Id
);
2884 -- If we have installed an underlying full view for a type derived
2885 -- from a private type in a child unit, restore the proper views
2886 -- of private and full view. See corresponding code in
2887 -- Install_Private_Declarations.
2889 -- After the exchange, Full denotes the private type in the
2890 -- visible part of the package.
2892 if Is_Private_Base_Type
(Full
)
2893 and then Present
(Full_View
(Full
))
2894 and then Present
(Underlying_Full_View
(Full
))
2895 and then In_Package_Body
(Current_Scope
)
2897 Set_Full_View
(Full
, Underlying_Full_View
(Full
));
2898 Set_Underlying_Full_View
(Full
, Empty
);
2901 elsif Ekind
(Id
) = E_Incomplete_Type
2902 and then Comes_From_Source
(Id
)
2903 and then No
(Full_View
(Id
))
2905 -- Mark Taft amendment types. Verify that there are no primitive
2906 -- operations declared for the type (3.10.1(9)).
2908 Set_Has_Completion_In_Body
(Id
);
2915 Elmt
:= First_Elmt
(Private_Dependents
(Id
));
2916 while Present
(Elmt
) loop
2917 Subp
:= Node
(Elmt
);
2919 -- Is_Primitive is tested because there can be cases where
2920 -- nonprimitive subprograms (in nested packages) are added
2921 -- to the Private_Dependents list.
2923 if Is_Overloadable
(Subp
) and then Is_Primitive
(Subp
) then
2925 ("type& must be completed in the private part",
2928 -- The result type of an access-to-function type cannot be a
2929 -- Taft-amendment type, unless the version is Ada 2012 or
2930 -- later (see AI05-151).
2932 elsif Ada_Version
< Ada_2012
2933 and then Ekind
(Subp
) = E_Subprogram_Type
2935 if Etype
(Subp
) = Id
2937 (Is_Class_Wide_Type
(Etype
(Subp
))
2938 and then Etype
(Etype
(Subp
)) = Id
)
2941 ("type& must be completed in the private part",
2942 Associated_Node_For_Itype
(Subp
), Id
);
2950 elsif not Is_Child_Unit
(Id
)
2951 and then (not Is_Private_Type
(Id
) or else No
(Full_View
(Id
)))
2954 Set_Is_Potentially_Use_Visible
(Id
, False);
2960 end Uninstall_Declarations
;
2962 ------------------------
2963 -- Unit_Requires_Body --
2964 ------------------------
2966 function Unit_Requires_Body
2967 (Pack_Id
: Entity_Id
;
2968 Do_Abstract_States
: Boolean := False) return Boolean
2972 Requires_Body
: Boolean := False;
2973 -- Flag set when the unit has at least one construct that requries
2974 -- completion in a body.
2977 -- Imported entity never requires body. Right now, only subprograms can
2978 -- be imported, but perhaps in the future we will allow import of
2981 if Is_Imported
(Pack_Id
) then
2984 -- Body required if library package with pragma Elaborate_Body
2986 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
2989 -- Body required if subprogram
2991 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
2994 -- Treat a block as requiring a body
2996 elsif Ekind
(Pack_Id
) = E_Block
then
2999 elsif Ekind
(Pack_Id
) = E_Package
3000 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3001 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3004 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3006 if Has_Pragma_Elaborate_Body
(G_P
) then
3012 -- Traverse the entity chain of the package and look for constructs that
3013 -- require a completion in a body.
3015 E
:= First_Entity
(Pack_Id
);
3016 while Present
(E
) loop
3018 -- Skip abstract states because their completion depends on several
3019 -- criteria (see below).
3021 if Ekind
(E
) = E_Abstract_State
then
3024 elsif Requires_Completion_In_Body
3025 (E
, Pack_Id
, Do_Abstract_States
)
3027 Requires_Body
:= True;
3034 -- A [generic] package that defines at least one non-null abstract state
3035 -- requires a completion only when at least one other construct requires
3036 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3037 -- performed if the caller requests this behavior.
3039 if Do_Abstract_States
3040 and then Ekind_In
(Pack_Id
, E_Generic_Package
, E_Package
)
3041 and then Has_Non_Null_Abstract_State
(Pack_Id
)
3042 and then Requires_Body
3047 return Requires_Body
;
3048 end Unit_Requires_Body
;
3050 -----------------------------
3051 -- Unit_Requires_Body_Info --
3052 -----------------------------
3054 procedure Unit_Requires_Body_Info
(Pack_Id
: Entity_Id
) is
3058 -- An imported entity never requires body. Right now, only subprograms
3059 -- can be imported, but perhaps in the future we will allow import of
3062 if Is_Imported
(Pack_Id
) then
3065 -- Body required if library package with pragma Elaborate_Body
3067 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
3068 Error_Msg_N
("info: & requires body (Elaborate_Body)?Y?", Pack_Id
);
3070 -- Body required if subprogram
3072 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
3073 Error_Msg_N
("info: & requires body (subprogram case)?Y?", Pack_Id
);
3075 -- Body required if generic parent has Elaborate_Body
3077 elsif Ekind
(Pack_Id
) = E_Package
3078 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3079 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3082 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3084 if Has_Pragma_Elaborate_Body
(G_P
) then
3086 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3091 -- A [generic] package that introduces at least one non-null abstract
3092 -- state requires completion. However, there is a separate rule that
3093 -- requires that such a package have a reason other than this for a
3094 -- body being required (if necessary a pragma Elaborate_Body must be
3095 -- provided). If Ignore_Abstract_State is True, we don't do this check
3096 -- (so we can use Unit_Requires_Body to check for some other reason).
3098 elsif Ekind_In
(Pack_Id
, E_Generic_Package
, E_Package
)
3099 and then Present
(Abstract_States
(Pack_Id
))
3100 and then not Is_Null_State
3101 (Node
(First_Elmt
(Abstract_States
(Pack_Id
))))
3104 ("info: & requires body (non-null abstract state aspect)?Y?",
3108 -- Otherwise search entity chain for entity requiring completion
3110 E
:= First_Entity
(Pack_Id
);
3111 while Present
(E
) loop
3112 if Requires_Completion_In_Body
(E
, Pack_Id
) then
3113 Error_Msg_Node_2
:= E
;
3115 ("info: & requires body (& requires completion)?Y?", E
, Pack_Id
);
3120 end Unit_Requires_Body_Info
;