1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 Debug
; use Debug
;
34 with Einfo
; use Einfo
;
35 with Elists
; use Elists
;
36 with Errout
; use Errout
;
37 with Exp_Disp
; use Exp_Disp
;
38 with Exp_Dist
; use Exp_Dist
;
39 with Exp_Dbug
; use Exp_Dbug
;
41 with Lib
.Xref
; use Lib
.Xref
;
42 with Namet
; use Namet
;
43 with Nmake
; use Nmake
;
44 with Nlists
; use Nlists
;
46 with Output
; use Output
;
47 with Restrict
; use Restrict
;
49 with Sem_Aux
; use Sem_Aux
;
50 with Sem_Cat
; use Sem_Cat
;
51 with Sem_Ch3
; use Sem_Ch3
;
52 with Sem_Ch6
; use Sem_Ch6
;
53 with Sem_Ch8
; use Sem_Ch8
;
54 with Sem_Ch10
; use Sem_Ch10
;
55 with Sem_Ch12
; use Sem_Ch12
;
56 with Sem_Ch13
; use Sem_Ch13
;
57 with Sem_Disp
; use Sem_Disp
;
58 with Sem_Eval
; use Sem_Eval
;
59 with Sem_Prag
; use Sem_Prag
;
60 with Sem_Util
; use Sem_Util
;
61 with Sem_Warn
; use Sem_Warn
;
62 with Snames
; use Snames
;
63 with Stand
; use Stand
;
64 with Sinfo
; use Sinfo
;
65 with Sinput
; use Sinput
;
67 with Uintp
; use Uintp
;
69 package body Sem_Ch7
is
71 -----------------------------------
72 -- Handling private declarations --
73 -----------------------------------
75 -- The principle that each entity has a single defining occurrence clashes
76 -- with the presence of two separate definitions for private types: the
77 -- first is the private type declaration, and the second is the full type
78 -- declaration. It is important that all references to the type point to
79 -- the same defining occurrence, namely the first one. To enforce the two
80 -- separate views of the entity, the corresponding information is swapped
81 -- between the two declarations. Outside of the package, the defining
82 -- occurrence only contains the private declaration information, while in
83 -- the private part and the body of the package the defining occurrence
84 -- contains the full declaration. To simplify the swap, the defining
85 -- occurrence that currently holds the private declaration points to the
86 -- full declaration. During semantic processing the defining occurrence
87 -- also points to a list of private dependents, that is to say access types
88 -- or composite types whose designated types or component types are
89 -- subtypes or derived types of the private type in question. After the
90 -- full declaration has been seen, the private dependents are updated to
91 -- indicate that they have full definitions.
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Analyze_Package_Body_Helper
(N
: Node_Id
);
98 -- Does all the real work of Analyze_Package_Body
100 procedure Check_Anonymous_Access_Types
101 (Spec_Id
: Entity_Id
;
103 -- If the spec of a package has a limited_with_clause, it may declare
104 -- anonymous access types whose designated type is a limited view, such an
105 -- anonymous access return type for a function. This access type cannot be
106 -- elaborated in the spec itself, but it may need an itype reference if it
107 -- is used within a nested scope. In that case the itype reference is
108 -- created at the beginning of the corresponding package body and inserted
109 -- before other body declarations.
111 procedure Install_Package_Entity
(Id
: Entity_Id
);
112 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
113 -- one entity on its visibility chain, and recurses on the visible part if
114 -- the entity is an inner package.
116 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean;
117 -- True for a private type that is not a subtype
119 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean;
120 -- If the private dependent is a private type whose full view is derived
121 -- from the parent type, its full properties are revealed only if we are in
122 -- the immediate scope of the private dependent. Should this predicate be
123 -- tightened further???
125 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
);
126 -- Called upon entering the private part of a public child package and the
127 -- body of a nested package, to potentially declare certain inherited
128 -- subprograms that were inherited by types in the visible part, but whose
129 -- declaration was deferred because the parent operation was private and
130 -- not visible at that point. These subprograms are located by traversing
131 -- the visible part declarations looking for non-private type extensions
132 -- and then examining each of the primitive operations of such types to
133 -- find those that were inherited but declared with a special internal
134 -- name. Each such operation is now declared as an operation with a normal
135 -- name (using the name of the parent operation) and replaces the previous
136 -- implicit operation in the primitive operations list of the type. If the
137 -- inherited private operation has been overridden, then it's replaced by
138 -- the overriding operation.
140 procedure Unit_Requires_Body_Info
(P
: Entity_Id
);
141 -- Outputs info messages showing why package specification P requires a
142 -- body. Caller has checked that the switch requesting this information
143 -- is set, and that the package does indeed require a body.
145 --------------------------
146 -- Analyze_Package_Body --
147 --------------------------
149 procedure Analyze_Package_Body
(N
: Node_Id
) is
150 Loc
: constant Source_Ptr
:= Sloc
(N
);
154 Write_Str
("==> package body ");
155 Write_Name
(Chars
(Defining_Entity
(N
)));
156 Write_Str
(" from ");
157 Write_Location
(Loc
);
162 -- The real work is split out into the helper, so it can do "return;"
163 -- without skipping the debug output.
165 Analyze_Package_Body_Helper
(N
);
169 Write_Str
("<== package body ");
170 Write_Name
(Chars
(Defining_Entity
(N
)));
171 Write_Str
(" from ");
172 Write_Location
(Loc
);
175 end Analyze_Package_Body
;
177 -----------------------------------
178 -- Analyze_Package_Body_Contract --
179 -----------------------------------
181 procedure Analyze_Package_Body_Contract
(Body_Id
: Entity_Id
) is
182 Spec_Id
: constant Entity_Id
:= Spec_Entity
(Body_Id
);
183 Mode
: SPARK_Mode_Type
;
187 -- Due to the timing of contract analysis, delayed pragmas may be
188 -- subject to the wrong SPARK_Mode, usually that of the enclosing
189 -- context. To remedy this, restore the original SPARK_Mode of the
190 -- related package body.
192 Save_SPARK_Mode_And_Set
(Body_Id
, Mode
);
194 Prag
:= Get_Pragma
(Body_Id
, Pragma_Refined_State
);
196 -- The analysis of pragma Refined_State detects whether the spec has
197 -- abstract states available for refinement.
199 if Present
(Prag
) then
200 Analyze_Refined_State_In_Decl_Part
(Prag
);
202 -- State refinement is required when the package declaration defines at
203 -- least one abstract state. Null states are not considered. Refinement
204 -- is not envorced when SPARK checks are turned off.
206 elsif SPARK_Mode
/= Off
207 and then Requires_State_Refinement
(Spec_Id
, Body_Id
)
209 Error_Msg_N
("package & requires state refinement", Spec_Id
);
212 -- Restore the SPARK_Mode of the enclosing context after all delayed
213 -- pragmas have been analyzed.
215 Restore_SPARK_Mode
(Mode
);
216 end Analyze_Package_Body_Contract
;
218 ---------------------------------
219 -- Analyze_Package_Body_Helper --
220 ---------------------------------
222 procedure Analyze_Package_Body_Helper
(N
: Node_Id
) is
226 Last_Spec_Entity
: Entity_Id
;
230 procedure Install_Composite_Operations
(P
: Entity_Id
);
231 -- Composite types declared in the current scope may depend on types
232 -- that were private at the point of declaration, and whose full view
233 -- is now in scope. Indicate that the corresponding operations on the
234 -- composite type are available.
236 ----------------------------------
237 -- Install_Composite_Operations --
238 ----------------------------------
240 procedure Install_Composite_Operations
(P
: Entity_Id
) is
244 Id
:= First_Entity
(P
);
245 while Present
(Id
) loop
247 and then (Is_Limited_Composite
(Id
)
248 or else Is_Private_Composite
(Id
))
249 and then No
(Private_Component
(Id
))
251 Set_Is_Limited_Composite
(Id
, False);
252 Set_Is_Private_Composite
(Id
, False);
257 end Install_Composite_Operations
;
259 -- Start of processing for Analyze_Package_Body_Helper
262 -- Find corresponding package specification, and establish the current
263 -- scope. The visible defining entity for the package is the defining
264 -- occurrence in the spec. On exit from the package body, all body
265 -- declarations are attached to the defining entity for the body, but
266 -- the later is never used for name resolution. In this fashion there
267 -- is only one visible entity that denotes the package.
269 -- Set Body_Id. Note that this will be reset to point to the generic
270 -- copy later on in the generic case.
272 Body_Id
:= Defining_Entity
(N
);
274 -- Body is body of package instantiation. Corresponding spec has already
277 if Present
(Corresponding_Spec
(N
)) then
278 Spec_Id
:= Corresponding_Spec
(N
);
279 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
282 Spec_Id
:= Current_Entity_In_Scope
(Defining_Entity
(N
));
284 if Present
(Spec_Id
) and then Is_Package_Or_Generic_Package
(Spec_Id
)
286 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
288 if Nkind
(Pack_Decl
) = N_Package_Renaming_Declaration
then
289 Error_Msg_N
("cannot supply body for package renaming", N
);
292 elsif Present
(Corresponding_Body
(Pack_Decl
)) then
293 Error_Msg_N
("redefinition of package body", N
);
298 Error_Msg_N
("missing specification for package body", N
);
302 if Is_Package_Or_Generic_Package
(Spec_Id
)
303 and then (Scope
(Spec_Id
) = Standard_Standard
304 or else Is_Child_Unit
(Spec_Id
))
305 and then not Unit_Requires_Body
(Spec_Id
)
307 if Ada_Version
= Ada_83
then
309 ("optional package body (not allowed in Ada 95)??", N
);
311 Error_Msg_N
("spec of this package does not allow a body", N
);
316 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
317 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
319 if Is_Child_Unit
(Spec_Id
) then
320 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
322 ("body of child unit& cannot be an inner package", N
, Spec_Id
);
325 Set_Is_Child_Unit
(Body_Id
);
328 -- Generic package case
330 if Ekind
(Spec_Id
) = E_Generic_Package
then
332 -- Disable expansion and perform semantic analysis on copy. The
333 -- unannotated body will be used in all instantiations.
335 Body_Id
:= Defining_Entity
(N
);
336 Set_Ekind
(Body_Id
, E_Package_Body
);
337 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
338 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
339 Set_Body_Entity
(Spec_Id
, Body_Id
);
340 Set_Spec_Entity
(Body_Id
, Spec_Id
);
342 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
345 -- Once the contents of the generic copy and the template are
346 -- swapped, do the same for their respective aspect specifications.
348 Exchange_Aspects
(N
, New_N
);
350 -- Update Body_Id to point to the copied node for the remainder of
353 Body_Id
:= Defining_Entity
(N
);
357 -- The Body_Id is that of the copied node in the generic case, the
358 -- current node otherwise. Note that N was rewritten above, so we must
359 -- be sure to get the latest Body_Id value.
361 Set_Ekind
(Body_Id
, E_Package_Body
);
362 Set_Body_Entity
(Spec_Id
, Body_Id
);
363 Set_Spec_Entity
(Body_Id
, Spec_Id
);
364 Set_Contract
(Body_Id
, Make_Contract
(Sloc
(Body_Id
)));
366 -- Defining name for the package body is not a visible entity: Only the
367 -- defining name for the declaration is visible.
369 Set_Etype
(Body_Id
, Standard_Void_Type
);
370 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
371 Set_Corresponding_Spec
(N
, Spec_Id
);
372 Set_Corresponding_Body
(Pack_Decl
, Body_Id
);
374 -- The body entity is not used for semantics or code generation, but
375 -- it is attached to the entity list of the enclosing scope to simplify
376 -- the listing of back-annotations for the types it main contain.
378 if Scope
(Spec_Id
) /= Standard_Standard
then
379 Append_Entity
(Body_Id
, Scope
(Spec_Id
));
382 -- Indicate that we are currently compiling the body of the package
384 Set_In_Package_Body
(Spec_Id
);
385 Set_Has_Completion
(Spec_Id
);
386 Last_Spec_Entity
:= Last_Entity
(Spec_Id
);
388 if Has_Aspects
(N
) then
389 Analyze_Aspect_Specifications
(N
, Body_Id
);
392 Push_Scope
(Spec_Id
);
394 -- Set SPARK_Mode only for non-generic package
396 if Ekind
(Spec_Id
) = E_Package
then
398 -- Set SPARK_Mode from context
400 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
401 Set_SPARK_Pragma_Inherited
(Body_Id
, True);
403 -- Set elaboration code SPARK mode the same for now
405 Set_SPARK_Aux_Pragma
(Body_Id
, SPARK_Pragma
(Body_Id
));
406 Set_SPARK_Aux_Pragma_Inherited
(Body_Id
, True);
409 Set_Categorization_From_Pragmas
(N
);
411 Install_Visible_Declarations
(Spec_Id
);
412 Install_Private_Declarations
(Spec_Id
);
413 Install_Private_With_Clauses
(Spec_Id
);
414 Install_Composite_Operations
(Spec_Id
);
416 Check_Anonymous_Access_Types
(Spec_Id
, N
);
418 if Ekind
(Spec_Id
) = E_Generic_Package
then
419 Set_Use
(Generic_Formal_Declarations
(Pack_Decl
));
422 Set_Use
(Visible_Declarations
(Specification
(Pack_Decl
)));
423 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
425 -- This is a nested package, so it may be necessary to declare certain
426 -- inherited subprograms that are not yet visible because the parent
427 -- type's subprograms are now visible.
429 if Ekind
(Scope
(Spec_Id
)) = E_Package
430 and then Scope
(Spec_Id
) /= Standard_Standard
432 Declare_Inherited_Private_Subprograms
(Spec_Id
);
435 if Present
(Declarations
(N
)) then
436 Analyze_Declarations
(Declarations
(N
));
437 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
440 -- Verify that the SPARK_Mode of the body agrees with that of its spec
442 if Present
(SPARK_Pragma
(Body_Id
)) then
443 if Present
(SPARK_Aux_Pragma
(Spec_Id
)) then
444 if Get_SPARK_Mode_From_Pragma
(SPARK_Aux_Pragma
(Spec_Id
)) = Off
446 Get_SPARK_Mode_From_Pragma
(SPARK_Pragma
(Body_Id
)) = On
448 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
449 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
450 Error_Msg_Sloc
:= Sloc
(SPARK_Aux_Pragma
(Spec_Id
));
452 ("\value Off was set for SPARK_Mode on & #", N
, Spec_Id
);
456 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
457 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
458 Error_Msg_Sloc
:= Sloc
(Spec_Id
);
460 ("\no value was set for SPARK_Mode on & #", N
, Spec_Id
);
464 -- Analyze_Declarations has caused freezing of all types. Now generate
465 -- bodies for RACW primitives and stream attributes, if any.
467 if Ekind
(Spec_Id
) = E_Package
and then Has_RACW
(Spec_Id
) then
469 -- Attach subprogram bodies to support RACWs declared in spec
471 Append_RACW_Bodies
(Declarations
(N
), Spec_Id
);
472 Analyze_List
(Declarations
(N
));
475 HSS
:= Handled_Statement_Sequence
(N
);
477 if Present
(HSS
) then
478 Process_End_Label
(HSS
, 't', Spec_Id
);
481 -- Check that elaboration code in a preelaborable package body is
482 -- empty other than null statements and labels (RM 10.2.1(6)).
484 Validate_Null_Statement_Sequence
(N
);
487 Validate_Categorization_Dependency
(N
, Spec_Id
);
488 Check_Completion
(Body_Id
);
490 -- Generate start of body reference. Note that we do this fairly late,
491 -- because the call will use In_Extended_Main_Source_Unit as a check,
492 -- and we want to make sure that Corresponding_Stub links are set
494 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
496 -- For a generic package, collect global references and mark them on
497 -- the original body so that they are not resolved again at the point
500 if Ekind
(Spec_Id
) /= E_Package
then
501 Save_Global_References
(Original_Node
(N
));
505 -- The entities of the package body have so far been chained onto the
506 -- declaration chain for the spec. That's been fine while we were in the
507 -- body, since we wanted them to be visible, but now that we are leaving
508 -- the package body, they are no longer visible, so we remove them from
509 -- the entity chain of the package spec entity, and copy them to the
510 -- entity chain of the package body entity, where they will never again
513 if Present
(Last_Spec_Entity
) then
514 Set_First_Entity
(Body_Id
, Next_Entity
(Last_Spec_Entity
));
515 Set_Next_Entity
(Last_Spec_Entity
, Empty
);
516 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
517 Set_Last_Entity
(Spec_Id
, Last_Spec_Entity
);
520 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
521 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
522 Set_First_Entity
(Spec_Id
, Empty
);
523 Set_Last_Entity
(Spec_Id
, Empty
);
526 End_Package_Scope
(Spec_Id
);
528 -- All entities declared in body are not visible
534 E
:= First_Entity
(Body_Id
);
535 while Present
(E
) loop
536 Set_Is_Immediately_Visible
(E
, False);
537 Set_Is_Potentially_Use_Visible
(E
, False);
540 -- Child units may appear on the entity list (e.g. if they appear
541 -- in the context of a subunit) but they are not body entities.
543 if not Is_Child_Unit
(E
) then
544 Set_Is_Package_Body_Entity
(E
);
551 Check_References
(Body_Id
);
553 -- For a generic unit, check that the formal parameters are referenced,
554 -- and that local variables are used, as for regular packages.
556 if Ekind
(Spec_Id
) = E_Generic_Package
then
557 Check_References
(Spec_Id
);
560 -- The processing so far has made all entities of the package body
561 -- public (i.e. externally visible to the linker). This is in general
562 -- necessary, since inlined or generic bodies, for which code is
563 -- generated in other units, may need to see these entities. The
564 -- following loop runs backwards from the end of the entities of the
565 -- package body making these entities invisible until we reach a
566 -- referencer, i.e. a declaration that could reference a previous
567 -- declaration, a generic body or an inlined body, or a stub (which may
568 -- contain either of these). This is of course an approximation, but it
569 -- is conservative and definitely correct.
571 -- We only do this at the outer (library) level non-generic packages.
572 -- The reason is simply to cut down on the number of global symbols
573 -- generated, which has a double effect: (1) to make the compilation
574 -- process more efficient and (2) to give the code generator more
575 -- freedom to optimize within each unit, especially subprograms.
577 if (Scope
(Spec_Id
) = Standard_Standard
or else Is_Child_Unit
(Spec_Id
))
578 and then not Is_Generic_Unit
(Spec_Id
)
579 and then Present
(Declarations
(N
))
581 Make_Non_Public_Where_Possible
: declare
583 function Has_Referencer
585 Outer
: Boolean) return Boolean;
586 -- Traverse given list of declarations in reverse order. Return
587 -- True if a referencer is present. Return False if none is found.
589 -- The Outer parameter is True for the outer level call and False
590 -- for inner level calls for nested packages. If Outer is True,
591 -- then any entities up to the point of hitting a referencer get
592 -- their Is_Public flag cleared, so that the entities will be
593 -- treated as static entities in the C sense, and need not have
594 -- fully qualified names. Furthermore, if the referencer is an
595 -- inlined subprogram that doesn't reference other subprograms,
596 -- we keep clearing the Is_Public flag on subprograms. For inner
597 -- levels, we need all names to be fully qualified to deal with
598 -- the same name appearing in parallel packages (right now this
599 -- is tied to their being external).
605 function Has_Referencer
607 Outer
: Boolean) return Boolean
609 Has_Referencer_Except_For_Subprograms
: Boolean := False;
616 function Check_Subprogram_Ref
(N
: Node_Id
)
617 return Traverse_Result
;
618 -- Look for references to subprograms
620 --------------------------
621 -- Check_Subprogram_Ref --
622 --------------------------
624 function Check_Subprogram_Ref
(N
: Node_Id
)
625 return Traverse_Result
630 -- Check name of procedure or function calls
632 if Nkind
(N
) in N_Subprogram_Call
633 and then Is_Entity_Name
(Name
(N
))
638 -- Check prefix of attribute references
640 if Nkind
(N
) = N_Attribute_Reference
641 and then Is_Entity_Name
(Prefix
(N
))
642 and then Present
(Entity
(Prefix
(N
)))
643 and then Ekind
(Entity
(Prefix
(N
))) in Subprogram_Kind
648 -- Check value of constants
650 if Nkind
(N
) = N_Identifier
651 and then Present
(Entity
(N
))
652 and then Ekind
(Entity
(N
)) = E_Constant
654 V
:= Constant_Value
(Entity
(N
));
657 and then not Compile_Time_Known_Value_Or_Aggr
(V
)
664 end Check_Subprogram_Ref
;
666 function Check_Subprogram_Refs
is
667 new Traverse_Func
(Check_Subprogram_Ref
);
669 -- Start of processing for Has_Referencer
677 while Present
(D
) loop
680 if K
in N_Body_Stub
then
683 -- Processing for subprogram bodies
685 elsif K
= N_Subprogram_Body
then
686 if Acts_As_Spec
(D
) then
687 E
:= Defining_Entity
(D
);
689 -- An inlined body acts as a referencer. Note also
690 -- that we never reset Is_Public for an inlined
691 -- subprogram. Gigi requires Is_Public to be set.
693 -- Note that we test Has_Pragma_Inline here rather
694 -- than Is_Inlined. We are compiling this for a
695 -- client, and it is the client who will decide if
696 -- actual inlining should occur, so we need to assume
697 -- that the procedure could be inlined for the purpose
698 -- of accessing global entities.
700 if Has_Pragma_Inline
(E
) then
701 if Outer
and then Check_Subprogram_Refs
(D
) = OK
703 Has_Referencer_Except_For_Subprograms
:= True;
708 Set_Is_Public
(E
, False);
712 E
:= Corresponding_Spec
(D
);
716 -- A generic subprogram body acts as a referencer
718 if Is_Generic_Unit
(E
) then
722 if Has_Pragma_Inline
(E
) or else Is_Inlined
(E
) then
723 if Outer
and then Check_Subprogram_Refs
(D
) = OK
725 Has_Referencer_Except_For_Subprograms
:= True;
733 -- Processing for package bodies
735 elsif K
= N_Package_Body
736 and then Present
(Corresponding_Spec
(D
))
738 E
:= Corresponding_Spec
(D
);
740 -- Generic package body is a referencer. It would seem
741 -- that we only have to consider generics that can be
742 -- exported, i.e. where the corresponding spec is the
743 -- spec of the current package, but because of nested
744 -- instantiations, a fully private generic body may
745 -- export other private body entities. Furthermore,
746 -- regardless of whether there was a previous inlined
747 -- subprogram, (an instantiation of) the generic package
748 -- may reference any entity declared before it.
750 if Is_Generic_Unit
(E
) then
753 -- For non-generic package body, recurse into body unless
754 -- this is an instance, we ignore instances since they
755 -- cannot have references that affect outer entities.
757 elsif not Is_Generic_Instance
(E
)
758 and then not Has_Referencer_Except_For_Subprograms
761 (Declarations
(D
), Outer
=> False)
767 -- Processing for package specs, recurse into declarations.
768 -- Again we skip this for the case of generic instances.
770 elsif K
= N_Package_Declaration
771 and then not Has_Referencer_Except_For_Subprograms
773 S
:= Specification
(D
);
775 if not Is_Generic_Unit
(Defining_Entity
(S
)) then
777 (Private_Declarations
(S
), Outer
=> False)
781 (Visible_Declarations
(S
), Outer
=> False)
787 -- Objects and exceptions need not be public if we have not
788 -- encountered a referencer so far. We only reset the flag
789 -- for outer level entities that are not imported/exported,
790 -- and which have no interface name.
792 elsif Nkind_In
(K
, N_Object_Declaration
,
793 N_Exception_Declaration
,
794 N_Subprogram_Declaration
)
796 E
:= Defining_Entity
(D
);
799 and then (not Has_Referencer_Except_For_Subprograms
800 or else K
= N_Subprogram_Declaration
)
801 and then not Is_Imported
(E
)
802 and then not Is_Exported
(E
)
803 and then No
(Interface_Name
(E
))
805 Set_Is_Public
(E
, False);
812 return Has_Referencer_Except_For_Subprograms
;
815 -- Start of processing for Make_Non_Public_Where_Possible
820 pragma Warnings
(Off
, Discard
);
823 Discard
:= Has_Referencer
(Declarations
(N
), Outer
=> True);
825 end Make_Non_Public_Where_Possible
;
828 -- If expander is not active, then here is where we turn off the
829 -- In_Package_Body flag, otherwise it is turned off at the end of the
830 -- corresponding expansion routine. If this is an instance body, we need
831 -- to qualify names of local entities, because the body may have been
832 -- compiled as a preliminary to another instantiation.
834 if not Expander_Active
then
835 Set_In_Package_Body
(Spec_Id
, False);
837 if Is_Generic_Instance
(Spec_Id
)
838 and then Operating_Mode
= Generate_Code
840 Qualify_Entity_Names
(N
);
843 end Analyze_Package_Body_Helper
;
845 ------------------------------
846 -- Analyze_Package_Contract --
847 ------------------------------
849 procedure Analyze_Package_Contract
(Pack_Id
: Entity_Id
) is
850 Mode
: SPARK_Mode_Type
;
854 -- Due to the timing of contract analysis, delayed pragmas may be
855 -- subject to the wrong SPARK_Mode, usually that of the enclosing
856 -- context. To remedy this, restore the original SPARK_Mode of the
859 Save_SPARK_Mode_And_Set
(Pack_Id
, Mode
);
861 -- Analyze the initialization related pragmas. Initializes must come
862 -- before Initial_Condition due to item dependencies.
864 Prag
:= Get_Pragma
(Pack_Id
, Pragma_Initializes
);
866 if Present
(Prag
) then
867 Analyze_Initializes_In_Decl_Part
(Prag
);
870 Prag
:= Get_Pragma
(Pack_Id
, Pragma_Initial_Condition
);
872 if Present
(Prag
) then
873 Analyze_Initial_Condition_In_Decl_Part
(Prag
);
876 -- Check whether the lack of indicator Part_Of agrees with the placement
877 -- of the package instantiation with respect to the state space.
879 if Is_Generic_Instance
(Pack_Id
) then
880 Prag
:= Get_Pragma
(Pack_Id
, Pragma_Part_Of
);
883 Check_Missing_Part_Of
(Pack_Id
);
887 -- Restore the SPARK_Mode of the enclosing context after all delayed
888 -- pragmas have been analyzed.
890 Restore_SPARK_Mode
(Mode
);
891 end Analyze_Package_Contract
;
893 ---------------------------------
894 -- Analyze_Package_Declaration --
895 ---------------------------------
897 procedure Analyze_Package_Declaration
(N
: Node_Id
) is
898 Id
: constant Node_Id
:= Defining_Entity
(N
);
901 -- True when in the context of a declared pure library unit
903 Body_Required
: Boolean;
904 -- True when this package declaration requires a corresponding body
907 -- True when this package declaration is not a nested declaration
911 Write_Str
("==> package spec ");
912 Write_Name
(Chars
(Id
));
913 Write_Str
(" from ");
914 Write_Location
(Sloc
(N
));
919 Generate_Definition
(Id
);
921 Set_Ekind
(Id
, E_Package
);
922 Set_Etype
(Id
, Standard_Void_Type
);
923 Set_Contract
(Id
, Make_Contract
(Sloc
(Id
)));
925 -- Set SPARK_Mode from context only for non-generic package
927 if Ekind
(Id
) = E_Package
then
928 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
929 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
930 Set_SPARK_Pragma_Inherited
(Id
, True);
931 Set_SPARK_Aux_Pragma_Inherited
(Id
, True);
934 -- Analyze aspect specifications immediately, since we need to recognize
935 -- things like Pure early enough to diagnose violations during analysis.
937 if Has_Aspects
(N
) then
938 Analyze_Aspect_Specifications
(N
, Id
);
941 -- Ada 2005 (AI-217): Check if the package has been illegally named
942 -- in a limited-with clause of its own context. In this case the error
943 -- has been previously notified by Analyze_Context.
945 -- limited with Pkg; -- ERROR
946 -- package Pkg is ...
948 if From_Limited_With
(Id
) then
954 PF
:= Is_Pure
(Enclosing_Lib_Unit_Entity
);
955 Set_Is_Pure
(Id
, PF
);
957 Set_Categorization_From_Pragmas
(N
);
959 Analyze
(Specification
(N
));
960 Validate_Categorization_Dependency
(N
, Id
);
962 Body_Required
:= Unit_Requires_Body
(Id
);
964 -- When this spec does not require an explicit body, we know that there
965 -- are no entities requiring completion in the language sense; we call
966 -- Check_Completion here only to ensure that any nested package
967 -- declaration that requires an implicit body gets one. (In the case
968 -- where a body is required, Check_Completion is called at the end of
969 -- the body's declarative part.)
971 if not Body_Required
then
975 Comp_Unit
:= Nkind
(Parent
(N
)) = N_Compilation_Unit
;
978 -- Set Body_Required indication on the compilation unit node, and
979 -- determine whether elaboration warnings may be meaningful on it.
981 Set_Body_Required
(Parent
(N
), Body_Required
);
983 if not Body_Required
then
984 Set_Suppress_Elaboration_Warnings
(Id
);
989 End_Package_Scope
(Id
);
991 -- For the declaration of a library unit that is a remote types package,
992 -- check legality rules regarding availability of stream attributes for
993 -- types that contain non-remote access values. This subprogram performs
994 -- visibility tests that rely on the fact that we have exited the scope
998 Validate_RT_RAT_Component
(N
);
1001 if Debug_Flag_C
then
1003 Write_Str
("<== package spec ");
1004 Write_Name
(Chars
(Id
));
1005 Write_Str
(" from ");
1006 Write_Location
(Sloc
(N
));
1009 end Analyze_Package_Declaration
;
1011 -----------------------------------
1012 -- Analyze_Package_Specification --
1013 -----------------------------------
1015 -- Note that this code is shared for the analysis of generic package specs
1016 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1018 procedure Analyze_Package_Specification
(N
: Node_Id
) is
1019 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1020 Orig_Decl
: constant Node_Id
:= Original_Node
(Parent
(N
));
1021 Vis_Decls
: constant List_Id
:= Visible_Declarations
(N
);
1022 Priv_Decls
: constant List_Id
:= Private_Declarations
(N
);
1025 Public_Child
: Boolean;
1027 Private_With_Clauses_Installed
: Boolean := False;
1028 -- In Ada 2005, private with_clauses are visible in the private part
1029 -- of a nested package, even if it appears in the public part of the
1030 -- enclosing package. This requires a separate step to install these
1031 -- private_with_clauses, and remove them at the end of the nested
1034 procedure Check_One_Tagged_Type_Or_Extension_At_Most
;
1035 -- Issue an error in SPARK mode if a package specification contains
1036 -- more than one tagged type or type extension.
1038 procedure Clear_Constants
(Id
: Entity_Id
; FE
: Entity_Id
);
1039 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1040 -- Is_True_Constant) on all variables that are entities of Id, and on
1041 -- the chain whose first element is FE. A recursive call is made for all
1042 -- packages and generic packages.
1044 procedure Generate_Parent_References
;
1045 -- For a child unit, generate references to parent units, for
1046 -- GPS navigation purposes.
1048 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean;
1049 -- Child and Unit are entities of compilation units. True if Child
1050 -- is a public child of Parent as defined in 10.1.1
1052 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
);
1053 -- Reject completion of an incomplete or private type declarations
1054 -- having a known discriminant part by an unchecked union.
1056 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
);
1057 -- Given the package entity of a generic package instantiation or
1058 -- formal package whose corresponding generic is a child unit, installs
1059 -- the private declarations of each of the child unit's parents.
1060 -- This has to be done at the point of entering the instance package's
1061 -- private part rather than being done in Sem_Ch12.Install_Parent
1062 -- (which is where the parents' visible declarations are installed).
1064 ------------------------------------------------
1065 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1066 ------------------------------------------------
1068 procedure Check_One_Tagged_Type_Or_Extension_At_Most
is
1071 procedure Check_Decls
(Decls
: List_Id
);
1072 -- Check that either Previous is Empty and Decls does not contain
1073 -- more than one tagged type or type extension, or Previous is
1074 -- already set and Decls contains no tagged type or type extension.
1080 procedure Check_Decls
(Decls
: List_Id
) is
1084 Decl
:= First
(Decls
);
1085 while Present
(Decl
) loop
1086 if Nkind
(Decl
) = N_Full_Type_Declaration
1087 and then Is_Tagged_Type
(Defining_Identifier
(Decl
))
1089 if No
(Previous
) then
1093 Error_Msg_Sloc
:= Sloc
(Previous
);
1094 Check_SPARK_05_Restriction
1095 ("at most one tagged type or type extension allowed",
1096 "\\ previous declaration#",
1105 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1109 Check_Decls
(Vis_Decls
);
1111 if Present
(Priv_Decls
) then
1112 Check_Decls
(Priv_Decls
);
1114 end Check_One_Tagged_Type_Or_Extension_At_Most
;
1116 ---------------------
1117 -- Clear_Constants --
1118 ---------------------
1120 procedure Clear_Constants
(Id
: Entity_Id
; FE
: Entity_Id
) is
1124 -- Ignore package renamings, not interesting and they can cause self
1125 -- referential loops in the code below.
1127 if Nkind
(Parent
(Id
)) = N_Package_Renaming_Declaration
then
1131 -- Note: in the loop below, the check for Next_Entity pointing back
1132 -- to the package entity may seem odd, but it is needed, because a
1133 -- package can contain a renaming declaration to itself, and such
1134 -- renamings are generated automatically within package instances.
1137 while Present
(E
) and then E
/= Id
loop
1138 if Is_Assignable
(E
) then
1139 Set_Never_Set_In_Source
(E
, False);
1140 Set_Is_True_Constant
(E
, False);
1141 Set_Current_Value
(E
, Empty
);
1142 Set_Is_Known_Null
(E
, False);
1143 Set_Last_Assignment
(E
, Empty
);
1145 if not Can_Never_Be_Null
(E
) then
1146 Set_Is_Known_Non_Null
(E
, False);
1149 elsif Is_Package_Or_Generic_Package
(E
) then
1150 Clear_Constants
(E
, First_Entity
(E
));
1151 Clear_Constants
(E
, First_Private_Entity
(E
));
1156 end Clear_Constants
;
1158 --------------------------------
1159 -- Generate_Parent_References --
1160 --------------------------------
1162 procedure Generate_Parent_References
is
1163 Decl
: constant Node_Id
:= Parent
(N
);
1166 if Id
= Cunit_Entity
(Main_Unit
)
1167 or else Parent
(Decl
) = Library_Unit
(Cunit
(Main_Unit
))
1169 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1171 elsif not Nkind_In
(Unit
(Cunit
(Main_Unit
)), N_Subprogram_Body
,
1174 -- If current unit is an ancestor of main unit, generate a
1175 -- reference to its own parent.
1179 Main_Spec
: Node_Id
:= Unit
(Cunit
(Main_Unit
));
1182 if Nkind
(Main_Spec
) = N_Package_Body
then
1183 Main_Spec
:= Unit
(Library_Unit
(Cunit
(Main_Unit
)));
1186 U
:= Parent_Spec
(Main_Spec
);
1187 while Present
(U
) loop
1188 if U
= Parent
(Decl
) then
1189 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1192 elsif Nkind
(Unit
(U
)) = N_Package_Body
then
1196 U
:= Parent_Spec
(Unit
(U
));
1201 end Generate_Parent_References
;
1203 ---------------------
1204 -- Is_Public_Child --
1205 ---------------------
1207 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean is
1209 if not Is_Private_Descendant
(Child
) then
1212 if Child
= Unit
then
1213 return not Private_Present
(
1214 Parent
(Unit_Declaration_Node
(Child
)));
1216 return Is_Public_Child
(Scope
(Child
), Unit
);
1219 end Is_Public_Child
;
1221 ----------------------------------------
1222 -- Inspect_Unchecked_Union_Completion --
1223 ----------------------------------------
1225 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
) is
1229 Decl
:= First
(Decls
);
1230 while Present
(Decl
) loop
1232 -- We are looking at an incomplete or private type declaration
1233 -- with a known_discriminant_part whose full view is an
1236 if Nkind_In
(Decl
, N_Incomplete_Type_Declaration
,
1237 N_Private_Type_Declaration
)
1238 and then Has_Discriminants
(Defining_Identifier
(Decl
))
1239 and then Present
(Full_View
(Defining_Identifier
(Decl
)))
1241 Is_Unchecked_Union
(Full_View
(Defining_Identifier
(Decl
)))
1244 ("completion of discriminated partial view "
1245 & "cannot be an unchecked union",
1246 Full_View
(Defining_Identifier
(Decl
)));
1251 end Inspect_Unchecked_Union_Completion
;
1253 -----------------------------------------
1254 -- Install_Parent_Private_Declarations --
1255 -----------------------------------------
1257 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
) is
1258 Inst_Par
: Entity_Id
;
1259 Gen_Par
: Entity_Id
;
1260 Inst_Node
: Node_Id
;
1263 Inst_Par
:= Inst_Id
;
1266 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
1267 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
1268 Inst_Node
:= Get_Package_Instantiation_Node
(Inst_Par
);
1270 if Nkind_In
(Inst_Node
, N_Package_Instantiation
,
1271 N_Formal_Package_Declaration
)
1272 and then Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
1274 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
1276 if Present
(Renamed_Entity
(Inst_Par
)) then
1277 Inst_Par
:= Renamed_Entity
(Inst_Par
);
1282 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
1284 -- Install the private declarations and private use clauses
1285 -- of a parent instance of the child instance, unless the
1286 -- parent instance private declarations have already been
1287 -- installed earlier in Analyze_Package_Specification, which
1288 -- happens when a generic child is instantiated, and the
1289 -- instance is a child of the parent instance.
1291 -- Installing the use clauses of the parent instance twice
1292 -- is both unnecessary and wrong, because it would cause the
1293 -- clauses to be chained to themselves in the use clauses
1294 -- list of the scope stack entry. That in turn would cause
1295 -- an endless loop from End_Use_Clauses upon scope exit.
1297 -- The parent is now fully visible. It may be a hidden open
1298 -- scope if we are currently compiling some child instance
1299 -- declared within it, but while the current instance is being
1300 -- compiled the parent is immediately visible. In particular
1301 -- its entities must remain visible if a stack save/restore
1302 -- takes place through a call to Rtsfind.
1304 if Present
(Gen_Par
) then
1305 if not In_Private_Part
(Inst_Par
) then
1306 Install_Private_Declarations
(Inst_Par
);
1307 Set_Use
(Private_Declarations
1309 (Unit_Declaration_Node
(Inst_Par
))));
1310 Set_Is_Hidden_Open_Scope
(Inst_Par
, False);
1313 -- If we've reached the end of the generic instance parents,
1314 -- then finish off by looping through the nongeneric parents
1315 -- and installing their private declarations.
1317 -- If one of the non-generic parents is itself on the scope
1318 -- stack, do not install its private declarations: they are
1319 -- installed in due time when the private part of that parent
1320 -- is analyzed. This is delicate ???
1323 while Present
(Inst_Par
)
1324 and then Inst_Par
/= Standard_Standard
1325 and then (not In_Open_Scopes
(Inst_Par
)
1326 or else not In_Private_Part
(Inst_Par
))
1328 Install_Private_Declarations
(Inst_Par
);
1329 Set_Use
(Private_Declarations
1331 (Unit_Declaration_Node
(Inst_Par
))));
1332 Inst_Par
:= Scope
(Inst_Par
);
1342 end Install_Parent_Private_Declarations
;
1344 -- Start of processing for Analyze_Package_Specification
1347 if Present
(Vis_Decls
) then
1348 Analyze_Declarations
(Vis_Decls
);
1351 -- Inspect the entities defined in the package and ensure that all
1352 -- incomplete types have received full declarations. Build default
1353 -- initial condition and invariant procedures for all qualifying types.
1355 E
:= First_Entity
(Id
);
1356 while Present
(E
) loop
1358 -- Check on incomplete types
1360 -- AI05-0213: A formal incomplete type has no completion
1362 if Ekind
(E
) = E_Incomplete_Type
1363 and then No
(Full_View
(E
))
1364 and then not Is_Generic_Type
(E
)
1366 Error_Msg_N
("no declaration in visible part for incomplete}", E
);
1371 -- Each private type subject to pragma Default_Initial_Condition
1372 -- declares a specialized procedure which verifies the assumption
1373 -- of the pragma. The declaration appears in the visible part of
1374 -- the package to allow for being called from the outside.
1376 if Has_Default_Init_Cond
(E
) then
1377 Build_Default_Init_Cond_Procedure_Declaration
(E
);
1379 -- A private extension inherits the default initial condition
1380 -- procedure from its parent type.
1382 elsif Has_Inherited_Default_Init_Cond
(E
) then
1383 Inherit_Default_Init_Cond_Procedure
(E
);
1386 -- If invariants are present, build the invariant procedure for a
1387 -- private type, but not any of its subtypes.
1389 if Has_Invariants
(E
) then
1390 if Ekind
(E
) = E_Private_Subtype
then
1393 Build_Invariant_Procedure
(E
, N
);
1401 if Is_Remote_Call_Interface
(Id
)
1402 and then Nkind
(Parent
(Parent
(N
))) = N_Compilation_Unit
1404 Validate_RCI_Declarations
(Id
);
1407 -- Save global references in the visible declarations, before installing
1408 -- private declarations of parent unit if there is one, because the
1409 -- privacy status of types defined in the parent will change. This is
1410 -- only relevant for generic child units, but is done in all cases for
1413 if Ekind
(Id
) = E_Generic_Package
1414 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1417 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1418 Save_Priv
: constant List_Id
:= Private_Declarations
(Orig_Spec
);
1420 Set_Private_Declarations
(Orig_Spec
, Empty_List
);
1421 Save_Global_References
(Orig_Decl
);
1422 Set_Private_Declarations
(Orig_Spec
, Save_Priv
);
1426 -- If package is a public child unit, then make the private declarations
1427 -- of the parent visible.
1429 Public_Child
:= False;
1433 Pack_Decl
: Node_Id
;
1438 Par_Spec
:= Parent_Spec
(Parent
(N
));
1440 -- If the package is formal package of an enclosing generic, it is
1441 -- transformed into a local generic declaration, and compiled to make
1442 -- its spec available. We need to retrieve the original generic to
1443 -- determine whether it is a child unit, and install its parents.
1447 Nkind
(Original_Node
(Parent
(N
))) = N_Formal_Package_Declaration
1449 Par
:= Entity
(Name
(Original_Node
(Parent
(N
))));
1450 Par_Spec
:= Parent_Spec
(Unit_Declaration_Node
(Par
));
1453 if Present
(Par_Spec
) then
1454 Generate_Parent_References
;
1456 while Scope
(Par
) /= Standard_Standard
1457 and then Is_Public_Child
(Id
, Par
)
1458 and then In_Open_Scopes
(Par
)
1460 Public_Child
:= True;
1462 Install_Private_Declarations
(Par
);
1463 Install_Private_With_Clauses
(Par
);
1464 Pack_Decl
:= Unit_Declaration_Node
(Par
);
1465 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
1470 if Is_Compilation_Unit
(Id
) then
1471 Install_Private_With_Clauses
(Id
);
1474 -- The current compilation unit may include private with_clauses,
1475 -- which are visible in the private part of the current nested
1476 -- package, and have to be installed now. This is not done for
1477 -- nested instantiations, where the private with_clauses of the
1478 -- enclosing unit have no effect once the instantiation info is
1479 -- established and we start analyzing the package declaration.
1482 Comp_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
1484 if Is_Package_Or_Generic_Package
(Comp_Unit
)
1485 and then not In_Private_Part
(Comp_Unit
)
1486 and then not In_Instance
1488 Install_Private_With_Clauses
(Comp_Unit
);
1489 Private_With_Clauses_Installed
:= True;
1494 -- If this is a package associated with a generic instance or formal
1495 -- package, then the private declarations of each of the generic's
1496 -- parents must be installed at this point.
1498 if Is_Generic_Instance
(Id
) then
1499 Install_Parent_Private_Declarations
(Id
);
1502 -- Analyze private part if present. The flag In_Private_Part is reset
1503 -- in End_Package_Scope.
1505 L
:= Last_Entity
(Id
);
1507 if Present
(Priv_Decls
) then
1508 Set_In_Private_Part
(Id
);
1510 -- Upon entering a public child's private part, it may be necessary
1511 -- to declare subprograms that were derived in the package's visible
1512 -- part but not yet made visible.
1514 if Public_Child
then
1515 Declare_Inherited_Private_Subprograms
(Id
);
1518 Analyze_Declarations
(Priv_Decls
);
1520 -- Check the private declarations for incomplete deferred constants
1522 Inspect_Deferred_Constant_Completion
(Priv_Decls
);
1524 -- The first private entity is the immediate follower of the last
1525 -- visible entity, if there was one.
1528 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1530 Set_First_Private_Entity
(Id
, First_Entity
(Id
));
1533 -- There may be inherited private subprograms that need to be declared,
1534 -- even in the absence of an explicit private part. If there are any
1535 -- public declarations in the package and the package is a public child
1536 -- unit, then an implicit private part is assumed.
1538 elsif Present
(L
) and then Public_Child
then
1539 Set_In_Private_Part
(Id
);
1540 Declare_Inherited_Private_Subprograms
(Id
);
1541 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1544 E
:= First_Entity
(Id
);
1545 while Present
(E
) loop
1547 -- Check rule of 3.6(11), which in general requires waiting till all
1548 -- full types have been seen.
1550 if Ekind
(E
) = E_Record_Type
or else Ekind
(E
) = E_Array_Type
then
1551 Check_Aliased_Component_Types
(E
);
1554 -- Check preelaborable initialization for full type completing a
1555 -- private type for which pragma Preelaborable_Initialization given.
1558 and then Must_Have_Preelab_Init
(E
)
1559 and then not Has_Preelaborable_Initialization
(E
)
1562 ("full view of & does not have preelaborable initialization", E
);
1565 -- An invariant may appear on a full view of a type
1568 and then Has_Private_Declaration
(E
)
1569 and then Nkind
(Parent
(E
)) = N_Full_Type_Declaration
1570 and then Has_Aspects
(Parent
(E
))
1576 ASN
:= First
(Aspect_Specifications
(Parent
(E
)));
1577 while Present
(ASN
) loop
1578 if Nam_In
(Chars
(Identifier
(ASN
)), Name_Invariant
,
1579 Name_Type_Invariant
)
1581 Build_Invariant_Procedure
(E
, N
);
1593 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1594 -- declaration having a known_discriminant_part shall not be an
1595 -- unchecked union type.
1597 if Present
(Vis_Decls
) then
1598 Inspect_Unchecked_Union_Completion
(Vis_Decls
);
1601 if Present
(Priv_Decls
) then
1602 Inspect_Unchecked_Union_Completion
(Priv_Decls
);
1605 if Ekind
(Id
) = E_Generic_Package
1606 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1607 and then Present
(Priv_Decls
)
1609 -- Save global references in private declarations, ignoring the
1610 -- visible declarations that were processed earlier.
1613 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1614 Save_Vis
: constant List_Id
:= Visible_Declarations
(Orig_Spec
);
1615 Save_Form
: constant List_Id
:=
1616 Generic_Formal_Declarations
(Orig_Decl
);
1619 Set_Visible_Declarations
(Orig_Spec
, Empty_List
);
1620 Set_Generic_Formal_Declarations
(Orig_Decl
, Empty_List
);
1621 Save_Global_References
(Orig_Decl
);
1622 Set_Generic_Formal_Declarations
(Orig_Decl
, Save_Form
);
1623 Set_Visible_Declarations
(Orig_Spec
, Save_Vis
);
1627 Process_End_Label
(N
, 'e', Id
);
1629 -- Remove private_with_clauses of enclosing compilation unit, if they
1632 if Private_With_Clauses_Installed
then
1633 Remove_Private_With_Clauses
(Cunit
(Current_Sem_Unit
));
1636 -- For the case of a library level package, we must go through all the
1637 -- entities clearing the indications that the value may be constant and
1638 -- not modified. Why? Because any client of this package may modify
1639 -- these values freely from anywhere. This also applies to any nested
1640 -- packages or generic packages.
1642 -- For now we unconditionally clear constants for packages that are
1643 -- instances of generic packages. The reason is that we do not have the
1644 -- body yet, and we otherwise think things are unreferenced when they
1645 -- are not. This should be fixed sometime (the effect is not terrible,
1646 -- we just lose some warnings, and also some cases of value propagation)
1649 if Is_Library_Level_Entity
(Id
)
1650 or else Is_Generic_Instance
(Id
)
1652 Clear_Constants
(Id
, First_Entity
(Id
));
1653 Clear_Constants
(Id
, First_Private_Entity
(Id
));
1656 -- Issue an error in SPARK mode if a package specification contains
1657 -- more than one tagged type or type extension.
1659 Check_One_Tagged_Type_Or_Extension_At_Most
;
1661 -- If switch set, output information on why body required
1663 if List_Body_Required_Info
1664 and then In_Extended_Main_Source_Unit
(Id
)
1665 and then Unit_Requires_Body
(Id
)
1667 Unit_Requires_Body_Info
(Id
);
1669 end Analyze_Package_Specification
;
1671 --------------------------------------
1672 -- Analyze_Private_Type_Declaration --
1673 --------------------------------------
1675 procedure Analyze_Private_Type_Declaration
(N
: Node_Id
) is
1676 PF
: constant Boolean := Is_Pure
(Enclosing_Lib_Unit_Entity
);
1677 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1680 Generate_Definition
(Id
);
1681 Set_Is_Pure
(Id
, PF
);
1682 Init_Size_Align
(Id
);
1684 if not Is_Package_Or_Generic_Package
(Current_Scope
)
1685 or else In_Private_Part
(Current_Scope
)
1687 Error_Msg_N
("invalid context for private declaration", N
);
1690 New_Private_Type
(N
, Id
, N
);
1691 Set_Depends_On_Private
(Id
);
1693 if Has_Aspects
(N
) then
1694 Analyze_Aspect_Specifications
(N
, Id
);
1696 end Analyze_Private_Type_Declaration
;
1698 ----------------------------------
1699 -- Check_Anonymous_Access_Types --
1700 ----------------------------------
1702 procedure Check_Anonymous_Access_Types
1703 (Spec_Id
: Entity_Id
;
1710 -- Itype references are only needed by gigi, to force elaboration of
1711 -- itypes. In the absence of code generation, they are not needed.
1713 if not Expander_Active
then
1717 E
:= First_Entity
(Spec_Id
);
1718 while Present
(E
) loop
1719 if Ekind
(E
) = E_Anonymous_Access_Type
1720 and then From_Limited_With
(E
)
1722 IR
:= Make_Itype_Reference
(Sloc
(P_Body
));
1725 if No
(Declarations
(P_Body
)) then
1726 Set_Declarations
(P_Body
, New_List
(IR
));
1728 Prepend
(IR
, Declarations
(P_Body
));
1734 end Check_Anonymous_Access_Types
;
1736 -------------------------------------------
1737 -- Declare_Inherited_Private_Subprograms --
1738 -------------------------------------------
1740 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
) is
1742 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean;
1743 -- Check whether an inherited subprogram S is an operation of an
1744 -- untagged derived type T.
1746 ---------------------
1747 -- Is_Primitive_Of --
1748 ---------------------
1750 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean is
1754 -- If the full view is a scalar type, the type is the anonymous base
1755 -- type, but the operation mentions the first subtype, so check the
1756 -- signature against the base type.
1758 if Base_Type
(Etype
(S
)) = Base_Type
(T
) then
1762 Formal
:= First_Formal
(S
);
1763 while Present
(Formal
) loop
1764 if Base_Type
(Etype
(Formal
)) = Base_Type
(T
) then
1768 Next_Formal
(Formal
);
1773 end Is_Primitive_Of
;
1780 Op_Elmt_2
: Elmt_Id
;
1781 Prim_Op
: Entity_Id
;
1782 New_Op
: Entity_Id
:= Empty
;
1783 Parent_Subp
: Entity_Id
;
1786 -- Start of processing for Declare_Inherited_Private_Subprograms
1789 E
:= First_Entity
(Id
);
1790 while Present
(E
) loop
1792 -- If the entity is a nonprivate type extension whose parent type
1793 -- is declared in an open scope, then the type may have inherited
1794 -- operations that now need to be made visible. Ditto if the entity
1795 -- is a formal derived type in a child unit.
1797 if ((Is_Derived_Type
(E
) and then not Is_Private_Type
(E
))
1799 (Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
1800 and then Is_Generic_Type
(E
)))
1801 and then In_Open_Scopes
(Scope
(Etype
(E
)))
1802 and then Is_Base_Type
(E
)
1804 if Is_Tagged_Type
(E
) then
1805 Op_List
:= Primitive_Operations
(E
);
1807 Tag
:= First_Tag_Component
(E
);
1809 Op_Elmt
:= First_Elmt
(Op_List
);
1810 while Present
(Op_Elmt
) loop
1811 Prim_Op
:= Node
(Op_Elmt
);
1813 -- Search primitives that are implicit operations with an
1814 -- internal name whose parent operation has a normal name.
1816 if Present
(Alias
(Prim_Op
))
1817 and then Find_Dispatching_Type
(Alias
(Prim_Op
)) /= E
1818 and then not Comes_From_Source
(Prim_Op
)
1819 and then Is_Internal_Name
(Chars
(Prim_Op
))
1820 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
1822 Parent_Subp
:= Alias
(Prim_Op
);
1824 -- Case 1: Check if the type has also an explicit
1825 -- overriding for this primitive.
1827 Op_Elmt_2
:= Next_Elmt
(Op_Elmt
);
1828 while Present
(Op_Elmt_2
) loop
1830 -- Skip entities with attribute Interface_Alias since
1831 -- they are not overriding primitives (these entities
1832 -- link an interface primitive with their covering
1835 if Chars
(Node
(Op_Elmt_2
)) = Chars
(Parent_Subp
)
1836 and then Type_Conformant
(Prim_Op
, Node
(Op_Elmt_2
))
1837 and then No
(Interface_Alias
(Node
(Op_Elmt_2
)))
1839 -- The private inherited operation has been
1840 -- overridden by an explicit subprogram:
1841 -- replace the former by the latter.
1843 New_Op
:= Node
(Op_Elmt_2
);
1844 Replace_Elmt
(Op_Elmt
, New_Op
);
1845 Remove_Elmt
(Op_List
, Op_Elmt_2
);
1846 Set_Overridden_Operation
(New_Op
, Parent_Subp
);
1848 -- We don't need to inherit its dispatching slot.
1849 -- Set_All_DT_Position has previously ensured that
1850 -- the same slot was assigned to the two primitives
1853 and then Present
(DTC_Entity
(New_Op
))
1854 and then Present
(DTC_Entity
(Prim_Op
))
1857 (DT_Position
(New_Op
) = DT_Position
(Prim_Op
));
1861 goto Next_Primitive
;
1864 Next_Elmt
(Op_Elmt_2
);
1867 -- Case 2: We have not found any explicit overriding and
1868 -- hence we need to declare the operation (i.e., make it
1871 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
1873 -- Inherit the dispatching slot if E is already frozen
1876 and then Present
(DTC_Entity
(Alias
(Prim_Op
)))
1878 Set_DTC_Entity_Value
(E
, New_Op
);
1879 Set_DT_Position
(New_Op
,
1880 DT_Position
(Alias
(Prim_Op
)));
1884 (Is_Dispatching_Operation
(New_Op
)
1885 and then Node
(Last_Elmt
(Op_List
)) = New_Op
);
1887 -- Substitute the new operation for the old one in the
1888 -- type's primitive operations list. Since the new
1889 -- operation was also just added to the end of list,
1890 -- the last element must be removed.
1892 -- (Question: is there a simpler way of declaring the
1893 -- operation, say by just replacing the name of the
1894 -- earlier operation, reentering it in the in the symbol
1895 -- table (how?), and marking it as private???)
1897 Replace_Elmt
(Op_Elmt
, New_Op
);
1898 Remove_Last_Elmt
(Op_List
);
1902 Next_Elmt
(Op_Elmt
);
1905 -- Generate listing showing the contents of the dispatch table
1907 if Debug_Flag_ZZ
then
1912 -- For untagged type, scan forward to locate inherited hidden
1915 Prim_Op
:= Next_Entity
(E
);
1916 while Present
(Prim_Op
) loop
1917 if Is_Subprogram
(Prim_Op
)
1918 and then Present
(Alias
(Prim_Op
))
1919 and then not Comes_From_Source
(Prim_Op
)
1920 and then Is_Internal_Name
(Chars
(Prim_Op
))
1921 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
1922 and then Is_Primitive_Of
(E
, Prim_Op
)
1924 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
1927 Next_Entity
(Prim_Op
);
1929 -- Derived operations appear immediately after the type
1930 -- declaration (or the following subtype indication for
1931 -- a derived scalar type). Further declarations cannot
1932 -- include inherited operations of the type.
1934 if Present
(Prim_Op
) then
1935 exit when Ekind
(Prim_Op
) not in Overloadable_Kind
;
1943 end Declare_Inherited_Private_Subprograms
;
1945 -----------------------
1946 -- End_Package_Scope --
1947 -----------------------
1949 procedure End_Package_Scope
(P
: Entity_Id
) is
1951 Uninstall_Declarations
(P
);
1953 end End_Package_Scope
;
1955 ---------------------------
1956 -- Exchange_Declarations --
1957 ---------------------------
1959 procedure Exchange_Declarations
(Id
: Entity_Id
) is
1960 Full_Id
: constant Entity_Id
:= Full_View
(Id
);
1961 H1
: constant Entity_Id
:= Homonym
(Id
);
1962 Next1
: constant Entity_Id
:= Next_Entity
(Id
);
1967 -- If missing full declaration for type, nothing to exchange
1969 if No
(Full_Id
) then
1973 -- Otherwise complete the exchange, and preserve semantic links
1975 Next2
:= Next_Entity
(Full_Id
);
1976 H2
:= Homonym
(Full_Id
);
1978 -- Reset full declaration pointer to reflect the switched entities and
1979 -- readjust the next entity chains.
1981 Exchange_Entities
(Id
, Full_Id
);
1983 Set_Next_Entity
(Id
, Next1
);
1984 Set_Homonym
(Id
, H1
);
1986 Set_Full_View
(Full_Id
, Id
);
1987 Set_Next_Entity
(Full_Id
, Next2
);
1988 Set_Homonym
(Full_Id
, H2
);
1989 end Exchange_Declarations
;
1991 ----------------------------
1992 -- Install_Package_Entity --
1993 ----------------------------
1995 procedure Install_Package_Entity
(Id
: Entity_Id
) is
1997 if not Is_Internal
(Id
) then
1998 if Debug_Flag_E
then
1999 Write_Str
("Install: ");
2000 Write_Name
(Chars
(Id
));
2004 if Is_Child_Unit
(Id
) then
2007 -- Do not enter implicitly inherited non-overridden subprograms of
2008 -- a tagged type back into visibility if they have non-conformant
2009 -- homographs (Ada RM 8.3 12.3/2).
2011 elsif Is_Hidden_Non_Overridden_Subpgm
(Id
) then
2015 Set_Is_Immediately_Visible
(Id
);
2018 end Install_Package_Entity
;
2020 ----------------------------------
2021 -- Install_Private_Declarations --
2022 ----------------------------------
2024 procedure Install_Private_Declarations
(P
: Entity_Id
) is
2027 Priv_Deps
: Elist_Id
;
2029 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2030 -- When the full view of a private type is made available, we do the
2031 -- same for its private dependents under proper visibility conditions.
2032 -- When compiling a grand-chid unit this needs to be done recursively.
2034 -----------------------------
2035 -- Swap_Private_Dependents --
2036 -----------------------------
2038 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
2041 Priv_Elmt
: Elmt_Id
;
2045 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
2046 while Present
(Priv_Elmt
) loop
2047 Priv
:= Node
(Priv_Elmt
);
2049 -- Before the exchange, verify that the presence of the Full_View
2050 -- field. This field will be empty if the entity has already been
2051 -- installed due to a previous call.
2053 if Present
(Full_View
(Priv
)) and then Is_Visible_Dependent
(Priv
)
2055 if Is_Private_Type
(Priv
) then
2056 Deps
:= Private_Dependents
(Priv
);
2062 -- For each subtype that is swapped, we also swap the reference
2063 -- to it in Private_Dependents, to allow access to it when we
2064 -- swap them out in End_Package_Scope.
2066 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
2067 Exchange_Declarations
(Priv
);
2068 Set_Is_Immediately_Visible
2069 (Priv
, In_Open_Scopes
(Scope
(Priv
)));
2070 Set_Is_Potentially_Use_Visible
2071 (Priv
, Is_Potentially_Use_Visible
(Node
(Priv_Elmt
)));
2073 -- Within a child unit, recurse, except in generic child unit,
2074 -- which (unfortunately) handle private_dependents separately.
2077 and then Is_Child_Unit
(Cunit_Entity
(Current_Sem_Unit
))
2078 and then not Is_Empty_Elmt_List
(Deps
)
2079 and then not Inside_A_Generic
2081 Swap_Private_Dependents
(Deps
);
2085 Next_Elmt
(Priv_Elmt
);
2087 end Swap_Private_Dependents
;
2089 -- Start of processing for Install_Private_Declarations
2092 -- First exchange declarations for private types, so that the full
2093 -- declaration is visible. For each private type, we check its
2094 -- Private_Dependents list and also exchange any subtypes of or derived
2095 -- types from it. Finally, if this is a Taft amendment type, the
2096 -- incomplete declaration is irrelevant, and we want to link the
2097 -- eventual full declaration with the original private one so we
2098 -- also skip the exchange.
2100 Id
:= First_Entity
(P
);
2101 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2102 if Is_Private_Base_Type
(Id
)
2103 and then Present
(Full_View
(Id
))
2104 and then Comes_From_Source
(Full_View
(Id
))
2105 and then Scope
(Full_View
(Id
)) = Scope
(Id
)
2106 and then Ekind
(Full_View
(Id
)) /= E_Incomplete_Type
2108 -- If there is a use-type clause on the private type, set the full
2109 -- view accordingly.
2111 Set_In_Use
(Full_View
(Id
), In_Use
(Id
));
2112 Full
:= Full_View
(Id
);
2114 if Is_Private_Base_Type
(Full
)
2115 and then Has_Private_Declaration
(Full
)
2116 and then Nkind
(Parent
(Full
)) = N_Full_Type_Declaration
2117 and then In_Open_Scopes
(Scope
(Etype
(Full
)))
2118 and then In_Package_Body
(Current_Scope
)
2119 and then not Is_Private_Type
(Etype
(Full
))
2121 -- This is the completion of a private type by a derivation
2122 -- from another private type which is not private anymore. This
2123 -- can only happen in a package nested within a child package,
2124 -- when the parent type is defined in the parent unit. At this
2125 -- point the current type is not private either, and we have
2126 -- to install the underlying full view, which is now visible.
2127 -- Save the current full view as well, so that all views can be
2128 -- restored on exit. It may seem that after compiling the child
2129 -- body there are not environments to restore, but the back-end
2130 -- expects those links to be valid, and freeze nodes depend on
2133 if No
(Full_View
(Full
))
2134 and then Present
(Underlying_Full_View
(Full
))
2136 Set_Full_View
(Id
, Underlying_Full_View
(Full
));
2137 Set_Underlying_Full_View
(Id
, Full
);
2139 Set_Underlying_Full_View
(Full
, Empty
);
2140 Set_Is_Frozen
(Full_View
(Id
));
2144 Priv_Deps
:= Private_Dependents
(Id
);
2145 Exchange_Declarations
(Id
);
2146 Set_Is_Immediately_Visible
(Id
);
2147 Swap_Private_Dependents
(Priv_Deps
);
2153 -- Next make other declarations in the private part visible as well
2155 Id
:= First_Private_Entity
(P
);
2156 while Present
(Id
) loop
2157 Install_Package_Entity
(Id
);
2158 Set_Is_Hidden
(Id
, False);
2162 -- Indicate that the private part is currently visible, so it can be
2163 -- properly reset on exit.
2165 Set_In_Private_Part
(P
);
2166 end Install_Private_Declarations
;
2168 ----------------------------------
2169 -- Install_Visible_Declarations --
2170 ----------------------------------
2172 procedure Install_Visible_Declarations
(P
: Entity_Id
) is
2174 Last_Entity
: Entity_Id
;
2178 (Is_Package_Or_Generic_Package
(P
) or else Is_Record_Type
(P
));
2180 if Is_Package_Or_Generic_Package
(P
) then
2181 Last_Entity
:= First_Private_Entity
(P
);
2183 Last_Entity
:= Empty
;
2186 Id
:= First_Entity
(P
);
2187 while Present
(Id
) and then Id
/= Last_Entity
loop
2188 Install_Package_Entity
(Id
);
2191 end Install_Visible_Declarations
;
2193 --------------------------
2194 -- Is_Private_Base_Type --
2195 --------------------------
2197 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean is
2199 return Ekind
(E
) = E_Private_Type
2200 or else Ekind
(E
) = E_Limited_Private_Type
2201 or else Ekind
(E
) = E_Record_Type_With_Private
;
2202 end Is_Private_Base_Type
;
2204 --------------------------
2205 -- Is_Visible_Dependent --
2206 --------------------------
2208 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean
2210 S
: constant Entity_Id
:= Scope
(Dep
);
2213 -- Renamings created for actual types have the visibility of the actual
2215 if Ekind
(S
) = E_Package
2216 and then Is_Generic_Instance
(S
)
2217 and then (Is_Generic_Actual_Type
(Dep
)
2218 or else Is_Generic_Actual_Type
(Full_View
(Dep
)))
2222 elsif not (Is_Derived_Type
(Dep
))
2223 and then Is_Derived_Type
(Full_View
(Dep
))
2225 -- When instantiating a package body, the scope stack is empty, so
2226 -- check instead whether the dependent type is defined in the same
2227 -- scope as the instance itself.
2229 return In_Open_Scopes
(S
)
2230 or else (Is_Generic_Instance
(Current_Scope
)
2231 and then Scope
(Dep
) = Scope
(Current_Scope
));
2235 end Is_Visible_Dependent
;
2237 ----------------------------
2238 -- May_Need_Implicit_Body --
2239 ----------------------------
2241 procedure May_Need_Implicit_Body
(E
: Entity_Id
) is
2242 P
: constant Node_Id
:= Unit_Declaration_Node
(E
);
2243 S
: constant Node_Id
:= Parent
(P
);
2248 if not Has_Completion
(E
)
2249 and then Nkind
(P
) = N_Package_Declaration
2250 and then (Present
(Activation_Chain_Entity
(P
)) or else Has_RACW
(E
))
2253 Make_Package_Body
(Sloc
(E
),
2254 Defining_Unit_Name
=> Make_Defining_Identifier
(Sloc
(E
),
2255 Chars
=> Chars
(E
)),
2256 Declarations
=> New_List
);
2258 if Nkind
(S
) = N_Package_Specification
then
2259 if Present
(Private_Declarations
(S
)) then
2260 Decls
:= Private_Declarations
(S
);
2262 Decls
:= Visible_Declarations
(S
);
2265 Decls
:= Declarations
(S
);
2271 end May_Need_Implicit_Body
;
2273 ----------------------
2274 -- New_Private_Type --
2275 ----------------------
2277 procedure New_Private_Type
(N
: Node_Id
; Id
: Entity_Id
; Def
: Node_Id
) is
2279 -- For other than Ada 2012, enter the name in the current scope
2281 if Ada_Version
< Ada_2012
then
2284 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2285 -- there may be an incomplete previous view.
2291 Prev
:= Find_Type_Name
(N
);
2292 pragma Assert
(Prev
= Id
2293 or else (Ekind
(Prev
) = E_Incomplete_Type
2294 and then Present
(Full_View
(Prev
))
2295 and then Full_View
(Prev
) = Id
));
2299 if Limited_Present
(Def
) then
2300 Set_Ekind
(Id
, E_Limited_Private_Type
);
2302 Set_Ekind
(Id
, E_Private_Type
);
2306 Set_Has_Delayed_Freeze
(Id
);
2307 Set_Is_First_Subtype
(Id
);
2308 Init_Size_Align
(Id
);
2310 Set_Is_Constrained
(Id
,
2311 No
(Discriminant_Specifications
(N
))
2312 and then not Unknown_Discriminants_Present
(N
));
2314 -- Set tagged flag before processing discriminants, to catch illegal
2317 Set_Is_Tagged_Type
(Id
, Tagged_Present
(Def
));
2319 Set_Discriminant_Constraint
(Id
, No_Elist
);
2320 Set_Stored_Constraint
(Id
, No_Elist
);
2322 if Present
(Discriminant_Specifications
(N
)) then
2324 Process_Discriminants
(N
);
2327 elsif Unknown_Discriminants_Present
(N
) then
2328 Set_Has_Unknown_Discriminants
(Id
);
2331 Set_Private_Dependents
(Id
, New_Elmt_List
);
2333 if Tagged_Present
(Def
) then
2334 Set_Ekind
(Id
, E_Record_Type_With_Private
);
2335 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2336 Set_Is_Abstract_Type
(Id
, Abstract_Present
(Def
));
2337 Set_Is_Limited_Record
(Id
, Limited_Present
(Def
));
2338 Set_Has_Delayed_Freeze
(Id
, True);
2340 -- Create a class-wide type with the same attributes
2342 Make_Class_Wide_Type
(Id
);
2344 elsif Abstract_Present
(Def
) then
2345 Error_Msg_N
("only a tagged type can be abstract", N
);
2347 end New_Private_Type
;
2349 ----------------------------
2350 -- Uninstall_Declarations --
2351 ----------------------------
2353 procedure Uninstall_Declarations
(P
: Entity_Id
) is
2354 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
2357 Priv_Elmt
: Elmt_Id
;
2358 Priv_Sub
: Entity_Id
;
2360 procedure Preserve_Full_Attributes
(Priv
, Full
: Entity_Id
);
2361 -- Copy to the private declaration the attributes of the full view that
2362 -- need to be available for the partial view also.
2364 function Type_In_Use
(T
: Entity_Id
) return Boolean;
2365 -- Check whether type or base type appear in an active use_type clause
2367 ------------------------------
2368 -- Preserve_Full_Attributes --
2369 ------------------------------
2371 procedure Preserve_Full_Attributes
(Priv
, Full
: Entity_Id
) is
2372 Priv_Is_Base_Type
: constant Boolean := Is_Base_Type
(Priv
);
2375 Set_Size_Info
(Priv
, (Full
));
2376 Set_RM_Size
(Priv
, RM_Size
(Full
));
2377 Set_Size_Known_At_Compile_Time
2378 (Priv
, Size_Known_At_Compile_Time
(Full
));
2379 Set_Is_Volatile
(Priv
, Is_Volatile
(Full
));
2380 Set_Treat_As_Volatile
(Priv
, Treat_As_Volatile
(Full
));
2381 Set_Is_Ada_2005_Only
(Priv
, Is_Ada_2005_Only
(Full
));
2382 Set_Is_Ada_2012_Only
(Priv
, Is_Ada_2012_Only
(Full
));
2383 Set_Has_Pragma_Unmodified
(Priv
, Has_Pragma_Unmodified
(Full
));
2384 Set_Has_Pragma_Unreferenced
(Priv
, Has_Pragma_Unreferenced
(Full
));
2385 Set_Has_Pragma_Unreferenced_Objects
2386 (Priv
, Has_Pragma_Unreferenced_Objects
2388 if Is_Unchecked_Union
(Full
) then
2389 Set_Is_Unchecked_Union
(Base_Type
(Priv
));
2391 -- Why is atomic not copied here ???
2393 if Referenced
(Full
) then
2394 Set_Referenced
(Priv
);
2397 if Priv_Is_Base_Type
then
2398 Set_Is_Controlled
(Priv
, Is_Controlled
(Base_Type
(Full
)));
2399 Set_Finalize_Storage_Only
2400 (Priv
, Finalize_Storage_Only
2401 (Base_Type
(Full
)));
2402 Set_Has_Task
(Priv
, Has_Task
(Base_Type
(Full
)));
2403 Set_Has_Protected
(Priv
, Has_Protected
(Base_Type
(Full
)));
2404 Set_Has_Controlled_Component
2405 (Priv
, Has_Controlled_Component
2406 (Base_Type
(Full
)));
2409 Set_Freeze_Node
(Priv
, Freeze_Node
(Full
));
2411 -- Propagate information of type invariants, which may be specified
2412 -- for the full view.
2414 if Has_Invariants
(Full
) and not Has_Invariants
(Priv
) then
2415 Set_Has_Invariants
(Priv
);
2416 Set_Subprograms_For_Type
(Priv
, Subprograms_For_Type
(Full
));
2419 if Is_Tagged_Type
(Priv
)
2420 and then Is_Tagged_Type
(Full
)
2421 and then not Error_Posted
(Full
)
2423 if Is_Tagged_Type
(Priv
) then
2425 -- If the type is tagged, the tag itself must be available on
2426 -- the partial view, for expansion purposes.
2428 Set_First_Entity
(Priv
, First_Entity
(Full
));
2430 -- If there are discriminants in the partial view, these remain
2431 -- visible. Otherwise only the tag itself is visible, and there
2432 -- are no nameable components in the partial view.
2434 if No
(Last_Entity
(Priv
)) then
2435 Set_Last_Entity
(Priv
, First_Entity
(Priv
));
2439 Set_Has_Discriminants
(Priv
, Has_Discriminants
(Full
));
2441 if Has_Discriminants
(Full
) then
2442 Set_Discriminant_Constraint
(Priv
,
2443 Discriminant_Constraint
(Full
));
2446 end Preserve_Full_Attributes
;
2452 function Type_In_Use
(T
: Entity_Id
) return Boolean is
2454 return Scope
(Base_Type
(T
)) = P
2455 and then (In_Use
(T
) or else In_Use
(Base_Type
(T
)));
2458 -- Start of processing for Uninstall_Declarations
2461 Id
:= First_Entity
(P
);
2462 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2463 if Debug_Flag_E
then
2464 Write_Str
("unlinking visible entity ");
2465 Write_Int
(Int
(Id
));
2469 -- On exit from the package scope, we must preserve the visibility
2470 -- established by use clauses in the current scope. Two cases:
2472 -- a) If the entity is an operator, it may be a primitive operator of
2473 -- a type for which there is a visible use-type clause.
2475 -- b) for other entities, their use-visibility is determined by a
2476 -- visible use clause for the package itself. For a generic instance,
2477 -- the instantiation of the formals appears in the visible part,
2478 -- but the formals are private and remain so.
2480 if Ekind
(Id
) = E_Function
2481 and then Is_Operator_Symbol_Name
(Chars
(Id
))
2482 and then not Is_Hidden
(Id
)
2483 and then not Error_Posted
(Id
)
2485 Set_Is_Potentially_Use_Visible
(Id
,
2487 or else Type_In_Use
(Etype
(Id
))
2488 or else Type_In_Use
(Etype
(First_Formal
(Id
)))
2489 or else (Present
(Next_Formal
(First_Formal
(Id
)))
2492 (Etype
(Next_Formal
(First_Formal
(Id
))))));
2494 if In_Use
(P
) and then not Is_Hidden
(Id
) then
2496 -- A child unit of a use-visible package remains use-visible
2497 -- only if it is itself a visible child unit. Otherwise it
2498 -- would remain visible in other contexts where P is use-
2499 -- visible, because once compiled it stays in the entity list
2500 -- of its parent unit.
2502 if Is_Child_Unit
(Id
) then
2503 Set_Is_Potentially_Use_Visible
2504 (Id
, Is_Visible_Lib_Unit
(Id
));
2506 Set_Is_Potentially_Use_Visible
(Id
);
2510 Set_Is_Potentially_Use_Visible
(Id
, False);
2514 -- Local entities are not immediately visible outside of the package
2516 Set_Is_Immediately_Visible
(Id
, False);
2518 -- If this is a private type with a full view (for example a local
2519 -- subtype of a private type declared elsewhere), ensure that the
2520 -- full view is also removed from visibility: it may be exposed when
2521 -- swapping views in an instantiation.
2523 if Is_Type
(Id
) and then Present
(Full_View
(Id
)) then
2524 Set_Is_Immediately_Visible
(Full_View
(Id
), False);
2527 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
2528 Check_Abstract_Overriding
(Id
);
2529 Check_Conventions
(Id
);
2532 if Ekind_In
(Id
, E_Private_Type
, E_Limited_Private_Type
)
2533 and then No
(Full_View
(Id
))
2534 and then not Is_Generic_Type
(Id
)
2535 and then not Is_Derived_Type
(Id
)
2537 Error_Msg_N
("missing full declaration for private type&", Id
);
2539 elsif Ekind
(Id
) = E_Record_Type_With_Private
2540 and then not Is_Generic_Type
(Id
)
2541 and then No
(Full_View
(Id
))
2543 if Nkind
(Parent
(Id
)) = N_Private_Type_Declaration
then
2544 Error_Msg_N
("missing full declaration for private type&", Id
);
2547 ("missing full declaration for private extension", Id
);
2550 -- Case of constant, check for deferred constant declaration with
2551 -- no full view. Likely just a matter of a missing expression, or
2552 -- accidental use of the keyword constant.
2554 elsif Ekind
(Id
) = E_Constant
2556 -- OK if constant value present
2558 and then No
(Constant_Value
(Id
))
2560 -- OK if full view present
2562 and then No
(Full_View
(Id
))
2564 -- OK if imported, since that provides the completion
2566 and then not Is_Imported
(Id
)
2568 -- OK if object declaration replaced by renaming declaration as
2569 -- a result of OK_To_Rename processing (e.g. for concatenation)
2571 and then Nkind
(Parent
(Id
)) /= N_Object_Renaming_Declaration
2573 -- OK if object declaration with the No_Initialization flag set
2575 and then not (Nkind
(Parent
(Id
)) = N_Object_Declaration
2576 and then No_Initialization
(Parent
(Id
)))
2578 -- If no private declaration is present, we assume the user did
2579 -- not intend a deferred constant declaration and the problem
2580 -- is simply that the initializing expression is missing.
2582 if not Has_Private_Declaration
(Etype
(Id
)) then
2584 -- We assume that the user did not intend a deferred constant
2585 -- declaration, and the expression is just missing.
2588 ("constant declaration requires initialization expression",
2591 if Is_Limited_Type
(Etype
(Id
)) then
2593 ("\if variable intended, remove CONSTANT from declaration",
2597 -- Otherwise if a private declaration is present, then we are
2598 -- missing the full declaration for the deferred constant.
2602 ("missing full declaration for deferred constant (RM 7.4)",
2605 if Is_Limited_Type
(Etype
(Id
)) then
2607 ("\if variable intended, remove CONSTANT from declaration",
2616 -- If the specification was installed as the parent of a public child
2617 -- unit, the private declarations were not installed, and there is
2620 if not In_Private_Part
(P
) then
2623 Set_In_Private_Part
(P
, False);
2626 -- Make private entities invisible and exchange full and private
2627 -- declarations for private types. Id is now the first private entity
2630 while Present
(Id
) loop
2631 if Debug_Flag_E
then
2632 Write_Str
("unlinking private entity ");
2633 Write_Int
(Int
(Id
));
2637 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
2638 Check_Abstract_Overriding
(Id
);
2639 Check_Conventions
(Id
);
2642 Set_Is_Immediately_Visible
(Id
, False);
2644 if Is_Private_Base_Type
(Id
) and then Present
(Full_View
(Id
)) then
2645 Full
:= Full_View
(Id
);
2647 -- If the partial view is not declared in the visible part of the
2648 -- package (as is the case when it is a type derived from some
2649 -- other private type in the private part of the current package),
2650 -- no exchange takes place.
2653 or else List_Containing
(Parent
(Id
)) /=
2654 Visible_Declarations
(Specification
(Decl
))
2659 -- The entry in the private part points to the full declaration,
2660 -- which is currently visible. Exchange them so only the private
2661 -- type declaration remains accessible, and link private and full
2662 -- declaration in the opposite direction. Before the actual
2663 -- exchange, we copy back attributes of the full view that must
2664 -- be available to the partial view too.
2666 Preserve_Full_Attributes
(Id
, Full
);
2668 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(P
));
2670 -- The following test may be redundant, as this is already
2671 -- diagnosed in sem_ch3. ???
2673 if Is_Indefinite_Subtype
(Full
)
2674 and then not Is_Indefinite_Subtype
(Id
)
2676 Error_Msg_Sloc
:= Sloc
(Parent
(Id
));
2678 ("full view of& not compatible with declaration#", Full
, Id
);
2681 -- Swap out the subtypes and derived types of Id that
2682 -- were compiled in this scope, or installed previously
2683 -- by Install_Private_Declarations.
2685 -- Before we do the swap, we verify the presence of the Full_View
2686 -- field which may be empty due to a swap by a previous call to
2687 -- End_Package_Scope (e.g. from the freezing mechanism).
2689 Priv_Elmt
:= First_Elmt
(Private_Dependents
(Id
));
2690 while Present
(Priv_Elmt
) loop
2691 Priv_Sub
:= Node
(Priv_Elmt
);
2693 if Present
(Full_View
(Priv_Sub
)) then
2694 if Scope
(Priv_Sub
) = P
2695 or else not In_Open_Scopes
(Scope
(Priv_Sub
))
2697 Set_Is_Immediately_Visible
(Priv_Sub
, False);
2700 if Is_Visible_Dependent
(Priv_Sub
) then
2701 Preserve_Full_Attributes
2702 (Priv_Sub
, Full_View
(Priv_Sub
));
2703 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv_Sub
));
2704 Exchange_Declarations
(Priv_Sub
);
2708 Next_Elmt
(Priv_Elmt
);
2711 -- Now restore the type itself to its private view
2713 Exchange_Declarations
(Id
);
2715 -- If we have installed an underlying full view for a type derived
2716 -- from a private type in a child unit, restore the proper views
2717 -- of private and full view. See corresponding code in
2718 -- Install_Private_Declarations.
2720 -- After the exchange, Full denotes the private type in the
2721 -- visible part of the package.
2723 if Is_Private_Base_Type
(Full
)
2724 and then Present
(Full_View
(Full
))
2725 and then Present
(Underlying_Full_View
(Full
))
2726 and then In_Package_Body
(Current_Scope
)
2728 Set_Full_View
(Full
, Underlying_Full_View
(Full
));
2729 Set_Underlying_Full_View
(Full
, Empty
);
2732 elsif Ekind
(Id
) = E_Incomplete_Type
2733 and then Comes_From_Source
(Id
)
2734 and then No
(Full_View
(Id
))
2736 -- Mark Taft amendment types. Verify that there are no primitive
2737 -- operations declared for the type (3.10.1(9)).
2739 Set_Has_Completion_In_Body
(Id
);
2746 Elmt
:= First_Elmt
(Private_Dependents
(Id
));
2747 while Present
(Elmt
) loop
2748 Subp
:= Node
(Elmt
);
2750 -- Is_Primitive is tested because there can be cases where
2751 -- nonprimitive subprograms (in nested packages) are added
2752 -- to the Private_Dependents list.
2754 if Is_Overloadable
(Subp
) and then Is_Primitive
(Subp
) then
2756 ("type& must be completed in the private part",
2759 -- The result type of an access-to-function type cannot be a
2760 -- Taft-amendment type, unless the version is Ada 2012 or
2761 -- later (see AI05-151).
2763 elsif Ada_Version
< Ada_2012
2764 and then Ekind
(Subp
) = E_Subprogram_Type
2766 if Etype
(Subp
) = Id
2768 (Is_Class_Wide_Type
(Etype
(Subp
))
2769 and then Etype
(Etype
(Subp
)) = Id
)
2772 ("type& must be completed in the private part",
2773 Associated_Node_For_Itype
(Subp
), Id
);
2781 elsif not Is_Child_Unit
(Id
)
2782 and then (not Is_Private_Type
(Id
) or else No
(Full_View
(Id
)))
2785 Set_Is_Potentially_Use_Visible
(Id
, False);
2791 end Uninstall_Declarations
;
2793 ------------------------
2794 -- Unit_Requires_Body --
2795 ------------------------
2797 function Unit_Requires_Body
2799 Ignore_Abstract_State
: Boolean := False) return Boolean
2804 -- Imported entity never requires body. Right now, only subprograms can
2805 -- be imported, but perhaps in the future we will allow import of
2808 if Is_Imported
(P
) then
2811 -- Body required if library package with pragma Elaborate_Body
2813 elsif Has_Pragma_Elaborate_Body
(P
) then
2816 -- Body required if subprogram
2818 elsif Is_Subprogram_Or_Generic_Subprogram
(P
) then
2821 -- Treat a block as requiring a body
2823 elsif Ekind
(P
) = E_Block
then
2826 elsif Ekind
(P
) = E_Package
2827 and then Nkind
(Parent
(P
)) = N_Package_Specification
2828 and then Present
(Generic_Parent
(Parent
(P
)))
2831 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(P
));
2833 if Has_Pragma_Elaborate_Body
(G_P
) then
2838 -- A [generic] package that introduces at least one non-null abstract
2839 -- state requires completion. However, there is a separate rule that
2840 -- requires that such a package have a reason other than this for a
2841 -- body being required (if necessary a pragma Elaborate_Body must be
2842 -- provided). If Ignore_Abstract_State is True, we don't do this check
2843 -- (so we can use Unit_Requires_Body to check for some other reason).
2845 elsif Ekind_In
(P
, E_Generic_Package
, E_Package
)
2846 and then not Ignore_Abstract_State
2847 and then Present
(Abstract_States
(P
))
2848 and then not Is_Null_State
(Node
(First_Elmt
(Abstract_States
(P
))))
2853 -- Otherwise search entity chain for entity requiring completion
2855 E
:= First_Entity
(P
);
2856 while Present
(E
) loop
2858 -- Always ignore child units. Child units get added to the entity
2859 -- list of a parent unit, but are not original entities of the
2860 -- parent, and so do not affect whether the parent needs a body.
2862 if Is_Child_Unit
(E
) then
2865 -- Ignore formal packages and their renamings
2867 elsif Ekind
(E
) = E_Package
2868 and then Nkind
(Original_Node
(Unit_Declaration_Node
(E
))) =
2869 N_Formal_Package_Declaration
2873 -- Otherwise test to see if entity requires a completion.
2874 -- Note that subprogram entities whose declaration does not come
2875 -- from source are ignored here on the basis that we assume the
2876 -- expander will provide an implicit completion at some point.
2878 elsif (Is_Overloadable
(E
)
2879 and then Ekind
(E
) /= E_Enumeration_Literal
2880 and then Ekind
(E
) /= E_Operator
2881 and then not Is_Abstract_Subprogram
(E
)
2882 and then not Has_Completion
(E
)
2883 and then Comes_From_Source
(Parent
(E
)))
2886 (Ekind
(E
) = E_Package
2888 and then not Has_Completion
(E
)
2889 and then Unit_Requires_Body
(E
))
2892 (Ekind
(E
) = E_Incomplete_Type
2893 and then No
(Full_View
(E
))
2894 and then not Is_Generic_Type
(E
))
2897 (Ekind_In
(E
, E_Task_Type
, E_Protected_Type
)
2898 and then not Has_Completion
(E
))
2901 (Ekind
(E
) = E_Generic_Package
2903 and then not Has_Completion
(E
)
2904 and then Unit_Requires_Body
(E
))
2907 (Is_Generic_Subprogram
(E
)
2908 and then not Has_Completion
(E
))
2913 -- Entity that does not require completion
2923 end Unit_Requires_Body
;
2925 -----------------------------
2926 -- Unit_Requires_Body_Info --
2927 -----------------------------
2929 procedure Unit_Requires_Body_Info
(P
: Entity_Id
) is
2933 -- Imported entity never requires body. Right now, only subprograms can
2934 -- be imported, but perhaps in the future we will allow import of
2937 if Is_Imported
(P
) then
2940 -- Body required if library package with pragma Elaborate_Body
2942 elsif Has_Pragma_Elaborate_Body
(P
) then
2943 Error_Msg_N
("info: & requires body (Elaborate_Body)?Y?", P
);
2945 -- Body required if subprogram
2947 elsif Is_Subprogram_Or_Generic_Subprogram
(P
) then
2948 Error_Msg_N
("info: & requires body (subprogram case)?Y?", P
);
2950 -- Body required if generic parent has Elaborate_Body
2952 elsif Ekind
(P
) = E_Package
2953 and then Nkind
(Parent
(P
)) = N_Package_Specification
2954 and then Present
(Generic_Parent
(Parent
(P
)))
2957 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(P
));
2959 if Has_Pragma_Elaborate_Body
(G_P
) then
2961 ("info: & requires body (generic parent Elaborate_Body)?Y?",
2966 -- A [generic] package that introduces at least one non-null abstract
2967 -- state requires completion. However, there is a separate rule that
2968 -- requires that such a package have a reason other than this for a
2969 -- body being required (if necessary a pragma Elaborate_Body must be
2970 -- provided). If Ignore_Abstract_State is True, we don't do this check
2971 -- (so we can use Unit_Requires_Body to check for some other reason).
2973 elsif Ekind_In
(P
, E_Generic_Package
, E_Package
)
2974 and then Present
(Abstract_States
(P
))
2975 and then not Is_Null_State
(Node
(First_Elmt
(Abstract_States
(P
))))
2978 ("info: & requires body (non-null abstract state aspect)?Y?", P
);
2981 -- Otherwise search entity chain for entity requiring completion
2983 E
:= First_Entity
(P
);
2984 while Present
(E
) loop
2986 -- Always ignore child units. Child units get added to the entity
2987 -- list of a parent unit, but are not original entities of the
2988 -- parent, and so do not affect whether the parent needs a body.
2990 if Is_Child_Unit
(E
) then
2993 -- Ignore formal packages and their renamings
2995 elsif Ekind
(E
) = E_Package
2996 and then Nkind
(Original_Node
(Unit_Declaration_Node
(E
))) =
2997 N_Formal_Package_Declaration
3001 -- Otherwise test to see if entity requires a completion.
3002 -- Note that subprogram entities whose declaration does not come
3003 -- from source are ignored here on the basis that we assume the
3004 -- expander will provide an implicit completion at some point.
3006 elsif (Is_Overloadable
(E
)
3007 and then Ekind
(E
) /= E_Enumeration_Literal
3008 and then Ekind
(E
) /= E_Operator
3009 and then not Is_Abstract_Subprogram
(E
)
3010 and then not Has_Completion
(E
)
3011 and then Comes_From_Source
(Parent
(E
)))
3014 (Ekind
(E
) = E_Package
3016 and then not Has_Completion
(E
)
3017 and then Unit_Requires_Body
(E
))
3020 (Ekind
(E
) = E_Incomplete_Type
3021 and then No
(Full_View
(E
))
3022 and then not Is_Generic_Type
(E
))
3025 (Ekind_In
(E
, E_Task_Type
, E_Protected_Type
)
3026 and then not Has_Completion
(E
))
3029 (Ekind
(E
) = E_Generic_Package
3031 and then not Has_Completion
(E
)
3032 and then Unit_Requires_Body
(E
))
3035 (Is_Generic_Subprogram
(E
)
3036 and then not Has_Completion
(E
))
3038 Error_Msg_Node_2
:= E
;
3040 ("info: & requires body (& requires completion)?Y?", E
, P
);
3042 -- Entity that does not require completion
3050 end Unit_Requires_Body_Info
;