1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Einfo
.Entities
; use Einfo
.Entities
;
37 with Einfo
.Utils
; use Einfo
.Utils
;
38 with Elists
; use Elists
;
39 with Errout
; use Errout
;
40 with Exp_Disp
; use Exp_Disp
;
41 with Exp_Dist
; use Exp_Dist
;
42 with Exp_Dbug
; use Exp_Dbug
;
43 with Freeze
; use Freeze
;
44 with Ghost
; use Ghost
;
46 with Lib
.Xref
; use Lib
.Xref
;
47 with Namet
; use Namet
;
48 with Nmake
; use Nmake
;
49 with Nlists
; use Nlists
;
51 with Output
; use Output
;
52 with Rtsfind
; use Rtsfind
;
54 with Sem_Aux
; use Sem_Aux
;
55 with Sem_Cat
; use Sem_Cat
;
56 with Sem_Ch3
; use Sem_Ch3
;
57 with Sem_Ch6
; use Sem_Ch6
;
58 with Sem_Ch8
; use Sem_Ch8
;
59 with Sem_Ch10
; use Sem_Ch10
;
60 with Sem_Ch12
; use Sem_Ch12
;
61 with Sem_Ch13
; use Sem_Ch13
;
62 with Sem_Disp
; use Sem_Disp
;
63 with Sem_Eval
; use Sem_Eval
;
64 with Sem_Prag
; use Sem_Prag
;
65 with Sem_Util
; use Sem_Util
;
66 with Sem_Warn
; use Sem_Warn
;
67 with Snames
; use Snames
;
68 with Stand
; use Stand
;
69 with Sinfo
; use Sinfo
;
70 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
71 with Sinfo
.Utils
; use Sinfo
.Utils
;
72 with Sinput
; use Sinput
;
74 with Uintp
; use Uintp
;
75 with Warnsw
; use Warnsw
;
79 package body Sem_Ch7
is
81 -----------------------------------
82 -- Handling private declarations --
83 -----------------------------------
85 -- The principle that each entity has a single defining occurrence clashes
86 -- with the presence of two separate definitions for private types: the
87 -- first is the private type declaration, and the second is the full type
88 -- declaration. It is important that all references to the type point to
89 -- the same defining occurrence, namely the first one. To enforce the two
90 -- separate views of the entity, the corresponding information is swapped
91 -- between the two declarations. Outside of the package, the defining
92 -- occurrence only contains the private declaration information, while in
93 -- the private part and the body of the package the defining occurrence
94 -- contains the full declaration. To simplify the swap, the defining
95 -- occurrence that currently holds the private declaration points to the
96 -- full declaration. During semantic processing the defining occurrence
97 -- also points to a list of private dependents, that is to say access types
98 -- or composite types whose designated types or component types are
99 -- subtypes or derived types of the private type in question. After the
100 -- full declaration has been seen, the private dependents are updated to
101 -- indicate that they have full definitions.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Package_Body_Helper
(N
: Node_Id
);
108 -- Does all the real work of Analyze_Package_Body
110 procedure Check_Anonymous_Access_Types
111 (Spec_Id
: Entity_Id
;
113 -- If the spec of a package has a limited_with_clause, it may declare
114 -- anonymous access types whose designated type is a limited view, such an
115 -- anonymous access return type for a function. This access type cannot be
116 -- elaborated in the spec itself, but it may need an itype reference if it
117 -- is used within a nested scope. In that case the itype reference is
118 -- created at the beginning of the corresponding package body and inserted
119 -- before other body declarations.
121 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
);
122 -- Called upon entering the private part of a public child package and the
123 -- body of a nested package, to potentially declare certain inherited
124 -- subprograms that were inherited by types in the visible part, but whose
125 -- declaration was deferred because the parent operation was private and
126 -- not visible at that point. These subprograms are located by traversing
127 -- the visible part declarations looking for non-private type extensions
128 -- and then examining each of the primitive operations of such types to
129 -- find those that were inherited but declared with a special internal
130 -- name. Each such operation is now declared as an operation with a normal
131 -- name (using the name of the parent operation) and replaces the previous
132 -- implicit operation in the primitive operations list of the type. If the
133 -- inherited private operation has been overridden, then it's replaced by
134 -- the overriding operation.
136 procedure Install_Package_Entity
(Id
: Entity_Id
);
137 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
138 -- one entity on its visibility chain, and recurses on the visible part if
139 -- the entity is an inner package.
141 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean;
142 -- True for a private type that is not a subtype
144 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean;
145 -- If the private dependent is a private type whose full view is derived
146 -- from the parent type, its full properties are revealed only if we are in
147 -- the immediate scope of the private dependent. Should this predicate be
148 -- tightened further???
150 function Requires_Completion_In_Body
153 Do_Abstract_States
: Boolean := False) return Boolean;
154 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
155 -- Determine whether entity Id declared in package spec Pack_Id requires
156 -- completion in a package body. Flag Do_Abstract_Stats should be set when
157 -- abstract states are to be considered in the completion test.
159 procedure Unit_Requires_Body_Info
(Pack_Id
: Entity_Id
);
160 -- Outputs info messages showing why package Pack_Id requires a body. The
161 -- caller has checked that the switch requesting this information is set,
162 -- and that the package does indeed require a body.
164 --------------------------
165 -- Analyze_Package_Body --
166 --------------------------
168 procedure Analyze_Package_Body
(N
: Node_Id
) is
169 Loc
: constant Source_Ptr
:= Sloc
(N
);
173 Write_Str
("==> package body ");
174 Write_Name
(Chars
(Defining_Entity
(N
)));
175 Write_Str
(" from ");
176 Write_Location
(Loc
);
181 -- The real work is split out into the helper, so it can do "return;"
182 -- without skipping the debug output.
184 Analyze_Package_Body_Helper
(N
);
188 Write_Str
("<== package body ");
189 Write_Name
(Chars
(Defining_Entity
(N
)));
190 Write_Str
(" from ");
191 Write_Location
(Loc
);
194 end Analyze_Package_Body
;
196 ------------------------------------------------------
197 -- Analyze_Package_Body_Helper Data and Subprograms --
198 ------------------------------------------------------
200 Entity_Table_Size
: constant := 4093;
201 -- Number of headers in hash table
203 subtype Entity_Header_Num
is Integer range 0 .. Entity_Table_Size
- 1;
204 -- Range of headers in hash table
206 function Node_Hash
(Id
: Entity_Id
) return Entity_Header_Num
;
207 -- Simple hash function for Entity_Ids
209 package Subprogram_Table
is new GNAT
.Htable
.Simple_HTable
210 (Header_Num
=> Entity_Header_Num
,
216 -- Hash table to record which subprograms are referenced. It is declared
217 -- at library level to avoid elaborating it for every call to Analyze.
219 package Traversed_Table
is new GNAT
.Htable
.Simple_HTable
220 (Header_Num
=> Entity_Header_Num
,
226 -- Hash table to record which nodes we have traversed, so we can avoid
227 -- traversing the same nodes repeatedly.
233 function Node_Hash
(Id
: Entity_Id
) return Entity_Header_Num
is
235 return Entity_Header_Num
(Id
mod Entity_Table_Size
);
238 ---------------------------------
239 -- Analyze_Package_Body_Helper --
240 ---------------------------------
242 -- WARNING: This routine manages Ghost regions. Return statements must be
243 -- replaced by gotos which jump to the end of the routine and restore the
246 procedure Analyze_Package_Body_Helper
(N
: Node_Id
) is
247 procedure Hide_Public_Entities
(Decls
: List_Id
);
248 -- Attempt to hide all public entities found in declarative list Decls
249 -- by resetting their Is_Public flag to False depending on whether the
250 -- entities are not referenced by inlined or generic bodies. This kind
251 -- of processing is a conservative approximation and will still leave
252 -- entities externally visible if the package is not simple enough.
254 procedure Install_Composite_Operations
(P
: Entity_Id
);
255 -- Composite types declared in the current scope may depend on types
256 -- that were private at the point of declaration, and whose full view
257 -- is now in scope. Indicate that the corresponding operations on the
258 -- composite type are available.
260 --------------------------
261 -- Hide_Public_Entities --
262 --------------------------
264 procedure Hide_Public_Entities
(Decls
: List_Id
) is
265 function Has_Referencer
267 In_Nested_Instance
: Boolean;
268 Has_Outer_Referencer_Of_Non_Subprograms
: Boolean) return Boolean;
269 -- A "referencer" is a construct which may reference a previous
270 -- declaration. Examine all declarations in list Decls in reverse
271 -- and determine whether one such referencer exists. All entities
272 -- in the range Last (Decls) .. Referencer are hidden from external
273 -- visibility. In_Nested_Instance is true if we are inside a package
274 -- instance that has a body.
276 function Scan_Subprogram_Ref
(N
: Node_Id
) return Traverse_Result
;
277 -- Determine whether a node denotes a reference to a subprogram
279 procedure Traverse_And_Scan_Subprogram_Refs
is
280 new Traverse_Proc
(Scan_Subprogram_Ref
);
281 -- Subsidiary to routine Has_Referencer. Determine whether a node
282 -- contains references to a subprogram and record them.
283 -- WARNING: this is a very expensive routine as it performs a full
286 procedure Scan_Subprogram_Refs
(Node
: Node_Id
);
287 -- If we haven't already traversed Node, then mark and traverse it.
293 function Has_Referencer
295 In_Nested_Instance
: Boolean;
296 Has_Outer_Referencer_Of_Non_Subprograms
: Boolean) return Boolean
298 Has_Referencer_Of_Non_Subprograms
: Boolean :=
299 Has_Outer_Referencer_Of_Non_Subprograms
;
300 -- Set if an inlined subprogram body was detected as a referencer.
301 -- In this case, we do not return True immediately but keep hiding
302 -- subprograms from external visibility.
306 In_Instance
: Boolean;
310 function Set_Referencer_Of_Non_Subprograms
return Boolean;
311 -- Set Has_Referencer_Of_Non_Subprograms and call
312 -- Scan_Subprogram_Refs if relevant.
313 -- Return whether Scan_Subprogram_Refs was called.
315 ---------------------------------------
316 -- Set_Referencer_Of_Non_Subprograms --
317 ---------------------------------------
319 function Set_Referencer_Of_Non_Subprograms
return Boolean is
321 -- An inlined subprogram body acts as a referencer
322 -- unless we generate C code since inlining is then
323 -- handled by the C compiler.
325 -- Note that we test Has_Pragma_Inline here in addition
326 -- to Is_Inlined. We are doing this for a client, since
327 -- we are computing which entities should be public, and
328 -- it is the client who will decide if actual inlining
329 -- should occur, so we need to catch all cases where the
330 -- subprogram may be inlined by the client.
332 if (not CCG_Mode
or else Has_Pragma_Inline_Always
(Decl_Id
))
333 and then (Is_Inlined
(Decl_Id
)
334 or else Has_Pragma_Inline
(Decl_Id
))
336 Has_Referencer_Of_Non_Subprograms
:= True;
338 -- Inspect the statements of the subprogram body
339 -- to determine whether the body references other
342 Scan_Subprogram_Refs
(Decl
);
347 end Set_Referencer_Of_Non_Subprograms
;
354 -- Examine all declarations in reverse order, hiding all entities
355 -- from external visibility until a referencer has been found. The
356 -- algorithm recurses into nested packages.
358 Decl
:= Last
(Decls
);
359 while Present
(Decl
) loop
361 -- A stub is always considered a referencer
363 if Nkind
(Decl
) in N_Body_Stub
then
366 -- Package declaration
368 elsif Nkind
(Decl
) = N_Package_Declaration
then
369 Spec
:= Specification
(Decl
);
370 Decl_Id
:= Defining_Entity
(Spec
);
372 -- Inspect the declarations of a non-generic package to try
373 -- and hide more entities from external visibility.
375 if not Is_Generic_Unit
(Decl_Id
) then
376 if In_Nested_Instance
then
378 elsif Is_Generic_Instance
(Decl_Id
) then
380 Has_Completion
(Decl_Id
)
381 or else Unit_Requires_Body
(Generic_Parent
(Spec
));
383 In_Instance
:= False;
386 if Has_Referencer
(Private_Declarations
(Spec
),
388 Has_Referencer_Of_Non_Subprograms
)
390 Has_Referencer
(Visible_Declarations
(Spec
),
392 Has_Referencer_Of_Non_Subprograms
)
400 elsif Nkind
(Decl
) = N_Package_Body
401 and then Present
(Corresponding_Spec
(Decl
))
403 Decl_Id
:= Corresponding_Spec
(Decl
);
405 -- A generic package body is a referencer. It would seem
406 -- that we only have to consider generics that can be
407 -- exported, i.e. where the corresponding spec is the
408 -- spec of the current package, but because of nested
409 -- instantiations, a fully private generic body may export
410 -- other private body entities. Furthermore, regardless of
411 -- whether there was a previous inlined subprogram, (an
412 -- instantiation of) the generic package may reference any
413 -- entity declared before it.
415 if Is_Generic_Unit
(Decl_Id
) then
418 -- Inspect the declarations of a non-generic package body to
419 -- try and hide more entities from external visibility.
421 elsif Has_Referencer
(Declarations
(Decl
),
424 Is_Generic_Instance
(Decl_Id
),
425 Has_Referencer_Of_Non_Subprograms
)
432 elsif Nkind
(Decl
) = N_Subprogram_Body
then
433 if Present
(Corresponding_Spec
(Decl
)) then
434 Decl_Id
:= Corresponding_Spec
(Decl
);
436 -- A generic subprogram body acts as a referencer
438 if Is_Generic_Unit
(Decl_Id
) then
442 Ignore
:= Set_Referencer_Of_Non_Subprograms
;
444 -- Otherwise this is a stand alone subprogram body
447 Decl_Id
:= Defining_Entity
(Decl
);
449 if not Set_Referencer_Of_Non_Subprograms
450 and then not Subprogram_Table
.Get
(Decl_Id
)
452 -- We can reset Is_Public right away
453 Set_Is_Public
(Decl_Id
, False);
459 elsif Nkind
(Decl
) = N_Freeze_Entity
then
462 pragma Unreferenced
(Discard
);
464 -- Inspect the actions to find references to subprograms.
465 -- We assume that the actions do not contain other kinds
466 -- of references and, therefore, we do not stop the scan
467 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
468 -- it would pessimize common cases for which the actions
469 -- contain the declaration of an init procedure, since
470 -- such a procedure is automatically marked inline.
473 Has_Referencer
(Actions
(Decl
),
475 Has_Referencer_Of_Non_Subprograms
);
478 -- Exceptions, objects and renamings do not need to be public
479 -- if they are not followed by a construct which can reference
482 elsif Nkind
(Decl
) in N_Exception_Declaration
483 | N_Object_Declaration
484 | N_Object_Renaming_Declaration
486 Decl_Id
:= Defining_Entity
(Decl
);
488 -- We cannot say anything for objects declared in nested
489 -- instances because instantiations are not done yet so the
490 -- bodies are not visible and could contain references to
493 if not In_Nested_Instance
494 and then not Is_Imported
(Decl_Id
)
495 and then not Is_Exported
(Decl_Id
)
496 and then No
(Interface_Name
(Decl_Id
))
497 and then not Has_Referencer_Of_Non_Subprograms
499 Set_Is_Public
(Decl_Id
, False);
502 -- Likewise for subprograms and renamings, but we work harder
503 -- for them to see whether they are referenced on an individual
504 -- basis by looking into the table of referenced subprograms.
506 elsif Nkind
(Decl
) in N_Subprogram_Declaration
507 | N_Subprogram_Renaming_Declaration
509 Decl_Id
:= Defining_Entity
(Decl
);
511 -- We cannot say anything for subprograms declared in nested
512 -- instances because instantiations are not done yet so the
513 -- bodies are not visible and could contain references to
514 -- them, except if we still have no subprograms at all which
515 -- are referenced by an inlined body.
517 if (not In_Nested_Instance
518 or else not Subprogram_Table
.Get_First
)
519 and then not Is_Imported
(Decl_Id
)
520 and then not Is_Exported
(Decl_Id
)
521 and then No
(Interface_Name
(Decl_Id
))
522 and then not Subprogram_Table
.Get
(Decl_Id
)
524 Set_Is_Public
(Decl_Id
, False);
527 -- For a subprogram renaming, if the entity is referenced,
528 -- then so is the renamed subprogram. But there is an issue
529 -- with generic bodies because instantiations are not done
530 -- yet and, therefore, cannot be scanned for referencers.
531 -- That's why we use an approximation and test that we have
532 -- at least one subprogram referenced by an inlined body
533 -- instead of precisely the entity of this renaming.
535 if Nkind
(Decl
) = N_Subprogram_Renaming_Declaration
536 and then Subprogram_Table
.Get_First
537 and then Is_Entity_Name
(Name
(Decl
))
538 and then Present
(Entity
(Name
(Decl
)))
539 and then Is_Subprogram
(Entity
(Name
(Decl
)))
541 Subprogram_Table
.Set
(Entity
(Name
(Decl
)), True);
548 return Has_Referencer_Of_Non_Subprograms
;
551 -------------------------
552 -- Scan_Subprogram_Ref --
553 -------------------------
555 function Scan_Subprogram_Ref
(N
: Node_Id
) return Traverse_Result
is
557 -- Detect a reference of the form
560 if Nkind
(N
) in N_Subprogram_Call
561 and then Is_Entity_Name
(Name
(N
))
562 and then Present
(Entity
(Name
(N
)))
563 and then Is_Subprogram
(Entity
(Name
(N
)))
565 Subprogram_Table
.Set
(Entity
(Name
(N
)), True);
567 -- Detect a reference of the form
568 -- Subp'Some_Attribute
570 elsif Nkind
(N
) = N_Attribute_Reference
571 and then Is_Entity_Name
(Prefix
(N
))
572 and then Present
(Entity
(Prefix
(N
)))
573 and then Is_Subprogram
(Entity
(Prefix
(N
)))
575 Subprogram_Table
.Set
(Entity
(Prefix
(N
)), True);
577 -- Constants can be substituted by their value in gigi, which may
578 -- contain a reference, so scan the value recursively.
580 elsif Is_Entity_Name
(N
)
581 and then Present
(Entity
(N
))
582 and then Ekind
(Entity
(N
)) = E_Constant
585 Val
: constant Node_Id
:= Constant_Value
(Entity
(N
));
588 and then not Compile_Time_Known_Value
(Val
)
590 Scan_Subprogram_Refs
(Val
);
596 end Scan_Subprogram_Ref
;
598 --------------------------
599 -- Scan_Subprogram_Refs --
600 --------------------------
602 procedure Scan_Subprogram_Refs
(Node
: Node_Id
) is
604 if not Traversed_Table
.Get
(Node
) then
605 Traversed_Table
.Set
(Node
, True);
606 Traverse_And_Scan_Subprogram_Refs
(Node
);
608 end Scan_Subprogram_Refs
;
613 pragma Unreferenced
(Discard
);
615 -- Start of processing for Hide_Public_Entities
618 -- The algorithm examines the top level declarations of a package
619 -- body in reverse looking for a construct that may export entities
620 -- declared prior to it. If such a scenario is encountered, then all
621 -- entities in the range Last (Decls) .. construct are hidden from
622 -- external visibility. Consider:
630 -- package body Pack is
631 -- External_Obj : ...; -- (1)
633 -- package body Gen is -- (2)
634 -- ... External_Obj ... -- (3)
637 -- Local_Obj : ...; -- (4)
640 -- In this example Local_Obj (4) must not be externally visible as
641 -- it cannot be exported by anything in Pack. The body of generic
642 -- package Gen (2) on the other hand acts as a "referencer" and may
643 -- export anything declared before it. Since the compiler does not
644 -- perform flow analysis, it is not possible to determine precisely
645 -- which entities will be exported when Gen is instantiated. In the
646 -- example above External_Obj (1) is exported at (3), but this may
647 -- not always be the case. The algorithm takes a conservative stance
648 -- and leaves entity External_Obj public.
650 -- This very conservative algorithm is supplemented by a more precise
651 -- processing for inlined bodies. For them, we traverse the syntactic
652 -- tree and record which subprograms are actually referenced from it.
653 -- This makes it possible to compute a much smaller set of externally
654 -- visible subprograms in the absence of generic bodies, which can
655 -- have a significant impact on the inlining decisions made in the
656 -- back end and the removal of out-of-line bodies from the object
657 -- code. We do it only for inlined bodies because they are supposed
658 -- to be reasonably small and tree traversal is very expensive.
660 -- Note that even this special processing is not optimal for inlined
661 -- bodies, because we treat all inlined subprograms alike. An optimal
662 -- algorithm would require computing the transitive closure of the
663 -- inlined subprograms that can really be referenced from other units
664 -- in the source code.
666 -- We could extend this processing for inlined bodies and record all
667 -- entities, not just subprograms, referenced from them, which would
668 -- make it possible to compute a much smaller set of all externally
669 -- visible entities in the absence of generic bodies. But this would
670 -- mean implementing a more thorough tree traversal of the bodies,
671 -- i.e. not just syntactic, and the gain would very likely be worth
672 -- neither the hassle nor the slowdown of the compiler.
674 -- Finally, an important thing to be aware of is that, at this point,
675 -- instantiations are not done yet so we cannot directly see inlined
676 -- bodies coming from them. That's not catastrophic because only the
677 -- actual parameters of the instantiations matter here, and they are
678 -- present in the declarations list of the instantiated packages.
680 Traversed_Table
.Reset
;
681 Subprogram_Table
.Reset
;
682 Discard
:= Has_Referencer
(Decls
, False, False);
683 end Hide_Public_Entities
;
685 ----------------------------------
686 -- Install_Composite_Operations --
687 ----------------------------------
689 procedure Install_Composite_Operations
(P
: Entity_Id
) is
693 Id
:= First_Entity
(P
);
694 while Present
(Id
) loop
696 and then (Is_Limited_Composite
(Id
)
697 or else Is_Private_Composite
(Id
))
698 and then No
(Private_Component
(Id
))
700 Set_Is_Limited_Composite
(Id
, False);
701 Set_Is_Private_Composite
(Id
, False);
706 end Install_Composite_Operations
;
710 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
711 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
712 Saved_EA
: constant Boolean := Expander_Active
;
713 Saved_ISMP
: constant Boolean :=
714 Ignore_SPARK_Mode_Pragmas_In_Instance
;
715 -- Save the Ghost and SPARK mode-related data to restore on exit
719 Last_Spec_Entity
: Entity_Id
;
724 -- Start of processing for Analyze_Package_Body_Helper
727 -- Find corresponding package specification, and establish the current
728 -- scope. The visible defining entity for the package is the defining
729 -- occurrence in the spec. On exit from the package body, all body
730 -- declarations are attached to the defining entity for the body, but
731 -- the later is never used for name resolution. In this fashion there
732 -- is only one visible entity that denotes the package.
734 -- Set Body_Id. Note that this will be reset to point to the generic
735 -- copy later on in the generic case.
737 Body_Id
:= Defining_Entity
(N
);
739 -- Body is body of package instantiation. Corresponding spec has already
742 if Present
(Corresponding_Spec
(N
)) then
743 Spec_Id
:= Corresponding_Spec
(N
);
744 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
747 Spec_Id
:= Current_Entity_In_Scope
(Defining_Entity
(N
));
750 and then Is_Package_Or_Generic_Package
(Spec_Id
)
752 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
754 if Nkind
(Pack_Decl
) = N_Package_Renaming_Declaration
then
755 Error_Msg_N
("cannot supply body for package renaming", N
);
758 elsif Present
(Corresponding_Body
(Pack_Decl
)) then
759 Error_Msg_N
("redefinition of package body", N
);
764 Error_Msg_N
("missing specification for package body", N
);
768 if Is_Package_Or_Generic_Package
(Spec_Id
)
769 and then (Scope
(Spec_Id
) = Standard_Standard
770 or else Is_Child_Unit
(Spec_Id
))
771 and then not Unit_Requires_Body
(Spec_Id
)
773 if Ada_Version
= Ada_83
then
775 ("optional package body (not allowed in Ada 95)??", N
);
777 Error_Msg_N
("spec of this package does not allow a body", N
);
778 Error_Msg_N
("\either remove the body or add pragma "
779 & "Elaborate_Body in the spec", N
);
784 -- A [generic] package body freezes the contract of the nearest
785 -- enclosing package body and all other contracts encountered in
786 -- the same declarative part up to and excluding the package body:
788 -- package body Nearest_Enclosing_Package
789 -- with Refined_State => (State => Constit)
793 -- package body Freezes_Enclosing_Package_Body
794 -- with Refined_State => (State_2 => Constit_2)
799 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
801 -- This ensures that any annotations referenced by the contract of a
802 -- [generic] subprogram body declared within the current package body
803 -- are available. This form of freezing is decoupled from the usual
804 -- Freeze_xxx mechanism because it must also work in the context of
805 -- generics where normal freezing is disabled.
807 -- Only bodies coming from source should cause this type of freezing.
808 -- Instantiated generic bodies are excluded because their processing is
809 -- performed in a separate compilation pass which lacks enough semantic
810 -- information with respect to contract analysis. It is safe to suppress
811 -- the freezing of contracts in this case because this action already
812 -- took place at the end of the enclosing declarative part.
814 if Comes_From_Source
(N
)
815 and then not Is_Generic_Instance
(Spec_Id
)
817 Freeze_Previous_Contracts
(N
);
820 -- A package body is Ghost when the corresponding spec is Ghost. Set
821 -- the mode now to ensure that any nodes generated during analysis and
822 -- expansion are properly flagged as ignored Ghost.
824 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
826 -- Deactivate expansion inside the body of ignored Ghost entities,
827 -- as this code will ultimately be ignored. This avoids requiring the
828 -- presence of run-time units which are not needed. Only do this for
829 -- user entities, as internally generated entities might still need
830 -- to be expanded (e.g. those generated for types).
832 if Present
(Ignored_Ghost_Region
)
833 and then Comes_From_Source
(Body_Id
)
835 Expander_Active
:= False;
838 -- If the body completes the initial declaration of a compilation unit
839 -- which is subject to pragma Elaboration_Checks, set the model of the
840 -- pragma because it applies to all parts of the unit.
842 Install_Elaboration_Model
(Spec_Id
);
844 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
845 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
847 if Is_Child_Unit
(Spec_Id
) then
848 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
850 ("body of child unit& cannot be an inner package", N
, Spec_Id
);
853 Set_Is_Child_Unit
(Body_Id
);
856 -- Generic package case
858 if Ekind
(Spec_Id
) = E_Generic_Package
then
860 -- Disable expansion and perform semantic analysis on copy. The
861 -- unannotated body will be used in all instantiations.
863 Body_Id
:= Defining_Entity
(N
);
864 Mutate_Ekind
(Body_Id
, E_Package_Body
);
865 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
866 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
867 Set_Body_Entity
(Spec_Id
, Body_Id
);
868 Set_Spec_Entity
(Body_Id
, Spec_Id
);
870 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
873 -- Once the contents of the generic copy and the template are
874 -- swapped, do the same for their respective aspect specifications.
876 Exchange_Aspects
(N
, New_N
);
878 -- Collect all contract-related source pragmas found within the
879 -- template and attach them to the contract of the package body.
880 -- This contract is used in the capture of global references within
883 Create_Generic_Contract
(N
);
885 -- Update Body_Id to point to the copied node for the remainder of
888 Body_Id
:= Defining_Entity
(N
);
892 -- The Body_Id is that of the copied node in the generic case, the
893 -- current node otherwise. Note that N was rewritten above, so we must
894 -- be sure to get the latest Body_Id value.
896 Mutate_Ekind
(Body_Id
, E_Package_Body
);
897 Set_Body_Entity
(Spec_Id
, Body_Id
);
898 Set_Spec_Entity
(Body_Id
, Spec_Id
);
900 -- Defining name for the package body is not a visible entity: Only the
901 -- defining name for the declaration is visible.
903 Set_Etype
(Body_Id
, Standard_Void_Type
);
904 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
905 Set_Corresponding_Spec
(N
, Spec_Id
);
906 Set_Corresponding_Body
(Pack_Decl
, Body_Id
);
908 -- The body entity is not used for semantics or code generation, but
909 -- it is attached to the entity list of the enclosing scope to simplify
910 -- the listing of back-annotations for the types it main contain.
912 if Scope
(Spec_Id
) /= Standard_Standard
then
913 Append_Entity
(Body_Id
, Scope
(Spec_Id
));
916 -- Indicate that we are currently compiling the body of the package
918 Set_In_Package_Body
(Spec_Id
);
919 Set_Has_Completion
(Spec_Id
);
920 Last_Spec_Entity
:= Last_Entity
(Spec_Id
);
922 if Has_Aspects
(N
) then
923 Analyze_Aspect_Specifications
(N
, Body_Id
);
926 Push_Scope
(Spec_Id
);
928 -- Set SPARK_Mode only for non-generic package
930 if Ekind
(Spec_Id
) = E_Package
then
931 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
932 Set_SPARK_Aux_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
933 Set_SPARK_Pragma_Inherited
(Body_Id
);
934 Set_SPARK_Aux_Pragma_Inherited
(Body_Id
);
936 -- A package body may be instantiated or inlined at a later pass.
937 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
938 -- it applied to the package spec.
940 if Ignore_SPARK_Mode_Pragmas
(Spec_Id
) then
941 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
945 Set_Categorization_From_Pragmas
(N
);
947 Install_Visible_Declarations
(Spec_Id
);
948 Install_Private_Declarations
(Spec_Id
);
949 Install_Private_With_Clauses
(Spec_Id
);
950 Install_Composite_Operations
(Spec_Id
);
952 Check_Anonymous_Access_Types
(Spec_Id
, N
);
954 if Ekind
(Spec_Id
) = E_Generic_Package
then
955 Set_Use
(Generic_Formal_Declarations
(Pack_Decl
));
958 Set_Use
(Visible_Declarations
(Specification
(Pack_Decl
)));
959 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
961 -- This is a nested package, so it may be necessary to declare certain
962 -- inherited subprograms that are not yet visible because the parent
963 -- type's subprograms are now visible.
964 -- Note that for child units these operations were generated when
965 -- analyzing the package specification.
967 if Ekind
(Scope
(Spec_Id
)) = E_Package
968 and then Scope
(Spec_Id
) /= Standard_Standard
969 and then not Is_Child_Unit
(Spec_Id
)
971 Declare_Inherited_Private_Subprograms
(Spec_Id
);
974 if Present
(Declarations
(N
)) then
975 Analyze_Declarations
(Declarations
(N
));
976 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
979 -- Verify that the SPARK_Mode of the body agrees with that of its spec
981 if Present
(SPARK_Pragma
(Body_Id
)) then
982 if Present
(SPARK_Aux_Pragma
(Spec_Id
)) then
983 if Get_SPARK_Mode_From_Annotation
(SPARK_Aux_Pragma
(Spec_Id
)) =
986 Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = On
988 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
989 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
990 Error_Msg_Sloc
:= Sloc
(SPARK_Aux_Pragma
(Spec_Id
));
992 ("\value Off was set for SPARK_Mode on & #", N
, Spec_Id
);
995 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
996 -- as specified in source code, or because SPARK_Mode On is ignored
997 -- in an instance where the context is SPARK_Mode Off/Auto.
999 elsif Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = Off
1000 and then (Is_Generic_Unit
(Spec_Id
) or else In_Instance
)
1005 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
1006 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
1007 Error_Msg_Sloc
:= Sloc
(Spec_Id
);
1009 ("\no value was set for SPARK_Mode on & #", N
, Spec_Id
);
1013 -- Analyze_Declarations has caused freezing of all types. Now generate
1014 -- bodies for RACW primitives and stream attributes, if any.
1016 if Ekind
(Spec_Id
) = E_Package
and then Has_RACW
(Spec_Id
) then
1018 -- Attach subprogram bodies to support RACWs declared in spec
1020 Append_RACW_Bodies
(Declarations
(N
), Spec_Id
);
1021 Analyze_List
(Declarations
(N
));
1024 HSS
:= Handled_Statement_Sequence
(N
);
1026 if Present
(HSS
) then
1027 Process_End_Label
(HSS
, 't', Spec_Id
);
1030 -- Check that elaboration code in a preelaborable package body is
1031 -- empty other than null statements and labels (RM 10.2.1(6)).
1033 Validate_Null_Statement_Sequence
(N
);
1036 Validate_Categorization_Dependency
(N
, Spec_Id
);
1037 Check_Completion
(Body_Id
);
1039 -- Generate start of body reference. Note that we do this fairly late,
1040 -- because the call will use In_Extended_Main_Source_Unit as a check,
1041 -- and we want to make sure that Corresponding_Stub links are set
1043 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
1045 -- For a generic package, collect global references and mark them on
1046 -- the original body so that they are not resolved again at the point
1047 -- of instantiation.
1049 if Ekind
(Spec_Id
) /= E_Package
then
1050 Save_Global_References
(Original_Node
(N
));
1054 -- The entities of the package body have so far been chained onto the
1055 -- declaration chain for the spec. That's been fine while we were in the
1056 -- body, since we wanted them to be visible, but now that we are leaving
1057 -- the package body, they are no longer visible, so we remove them from
1058 -- the entity chain of the package spec entity, and copy them to the
1059 -- entity chain of the package body entity, where they will never again
1062 if Present
(Last_Spec_Entity
) then
1063 Set_First_Entity
(Body_Id
, Next_Entity
(Last_Spec_Entity
));
1064 Set_Next_Entity
(Last_Spec_Entity
, Empty
);
1065 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
1066 Set_Last_Entity
(Spec_Id
, Last_Spec_Entity
);
1069 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
1070 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
1071 Set_First_Entity
(Spec_Id
, Empty
);
1072 Set_Last_Entity
(Spec_Id
, Empty
);
1075 Update_Use_Clause_Chain
;
1076 End_Package_Scope
(Spec_Id
);
1078 -- All entities declared in body are not visible
1084 E
:= First_Entity
(Body_Id
);
1085 while Present
(E
) loop
1086 Set_Is_Immediately_Visible
(E
, False);
1087 Set_Is_Potentially_Use_Visible
(E
, False);
1090 -- Child units may appear on the entity list (e.g. if they appear
1091 -- in the context of a subunit) but they are not body entities.
1093 if not Is_Child_Unit
(E
) then
1094 Set_Is_Package_Body_Entity
(E
);
1101 Check_References
(Body_Id
);
1103 -- For a generic unit, check that the formal parameters are referenced,
1104 -- and that local variables are used, as for regular packages.
1106 if Ekind
(Spec_Id
) = E_Generic_Package
then
1107 Check_References
(Spec_Id
);
1110 -- At this point all entities of the package body are externally visible
1111 -- to the linker as their Is_Public flag is set to True. This proactive
1112 -- approach is necessary because an inlined or a generic body for which
1113 -- code is generated in other units may need to see these entities. Cut
1114 -- down the number of global symbols that do not need public visibility
1115 -- as this has two beneficial effects:
1116 -- (1) It makes the compilation process more efficient.
1117 -- (2) It gives the code generator more leeway to optimize within each
1118 -- unit, especially subprograms.
1120 -- This is done only for top-level library packages or child units as
1121 -- the algorithm does a top-down traversal of the package body. This is
1122 -- also done for instances because instantiations are still pending by
1123 -- the time the enclosing package body is analyzed.
1125 if (Scope
(Spec_Id
) = Standard_Standard
1126 or else Is_Child_Unit
(Spec_Id
)
1127 or else Is_Generic_Instance
(Spec_Id
))
1128 and then not Is_Generic_Unit
(Spec_Id
)
1130 Hide_Public_Entities
(Declarations
(N
));
1133 -- If expander is not active, then here is where we turn off the
1134 -- In_Package_Body flag, otherwise it is turned off at the end of the
1135 -- corresponding expansion routine. If this is an instance body, we need
1136 -- to qualify names of local entities, because the body may have been
1137 -- compiled as a preliminary to another instantiation.
1139 if not Expander_Active
then
1140 Set_In_Package_Body
(Spec_Id
, False);
1142 if Is_Generic_Instance
(Spec_Id
)
1143 and then Operating_Mode
= Generate_Code
1145 Qualify_Entity_Names
(N
);
1149 if Present
(Ignored_Ghost_Region
) then
1150 Expander_Active
:= Saved_EA
;
1153 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
1154 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
1155 end Analyze_Package_Body_Helper
;
1157 ---------------------------------
1158 -- Analyze_Package_Declaration --
1159 ---------------------------------
1161 procedure Analyze_Package_Declaration
(N
: Node_Id
) is
1162 Id
: constant Node_Id
:= Defining_Entity
(N
);
1164 Is_Comp_Unit
: constant Boolean :=
1165 Nkind
(Parent
(N
)) = N_Compilation_Unit
;
1167 Body_Required
: Boolean;
1168 -- True when this package declaration requires a corresponding body
1171 if Debug_Flag_C
then
1172 Write_Str
("==> package spec ");
1173 Write_Name
(Chars
(Id
));
1174 Write_Str
(" from ");
1175 Write_Location
(Sloc
(N
));
1180 Generate_Definition
(Id
);
1182 Mutate_Ekind
(Id
, E_Package
);
1183 Set_Etype
(Id
, Standard_Void_Type
);
1185 -- Set SPARK_Mode from context
1187 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
1188 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
1189 Set_SPARK_Pragma_Inherited
(Id
);
1190 Set_SPARK_Aux_Pragma_Inherited
(Id
);
1192 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1193 -- the body of this package is instantiated or inlined later and out of
1194 -- context. The body uses this attribute to restore the value of the
1197 if Ignore_SPARK_Mode_Pragmas_In_Instance
then
1198 Set_Ignore_SPARK_Mode_Pragmas
(Id
);
1201 -- Analyze aspect specifications immediately, since we need to recognize
1202 -- things like Pure early enough to diagnose violations during analysis.
1204 if Has_Aspects
(N
) then
1205 Analyze_Aspect_Specifications
(N
, Id
);
1208 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1209 -- a limited-with clause of its own context. In this case the error has
1210 -- been previously notified by Analyze_Context.
1212 -- limited with Pkg; -- ERROR
1213 -- package Pkg is ...
1215 if From_Limited_With
(Id
) then
1221 Set_Is_Pure
(Id
, Is_Pure
(Enclosing_Lib_Unit_Entity
));
1222 Set_Categorization_From_Pragmas
(N
);
1224 Analyze
(Specification
(N
));
1225 Validate_Categorization_Dependency
(N
, Id
);
1227 -- Determine whether the package requires a body. Abstract states are
1228 -- intentionally ignored because they do require refinement which can
1229 -- only come in a body, but at the same time they do not force the need
1230 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1232 Body_Required
:= Unit_Requires_Body
(Id
);
1234 if not Body_Required
then
1236 -- If the package spec does not require an explicit body, then there
1237 -- are not entities requiring completion in the language sense. Call
1238 -- Check_Completion now to ensure that nested package declarations
1239 -- that require an implicit body get one. (In the case where a body
1240 -- is required, Check_Completion is called at the end of the body's
1241 -- declarative part.)
1245 -- If the package spec does not require an explicit body, then all
1246 -- abstract states declared in nested packages cannot possibly get a
1247 -- proper refinement (SPARK RM 7.1.4(4) and SPARK RM 7.2.2(3)). This
1248 -- check is performed only when the compilation unit is the main
1249 -- unit to allow for modular SPARK analysis where packages do not
1250 -- necessarily have bodies.
1252 if Is_Comp_Unit
then
1253 Check_State_Refinements
1255 Is_Main_Unit
=> Parent
(N
) = Cunit
(Main_Unit
));
1258 -- Warn about references to unset objects, which is straightforward
1259 -- for packages with no bodies. For packages with bodies this is more
1260 -- complicated, because some of the objects might be set between spec
1261 -- and body elaboration, in nested or child packages, etc.
1263 Check_References
(Id
);
1266 -- Set Body_Required indication on the compilation unit node
1268 if Is_Comp_Unit
then
1269 Set_Body_Required
(Parent
(N
), Body_Required
);
1271 if Legacy_Elaboration_Checks
and not Body_Required
then
1272 Set_Suppress_Elaboration_Warnings
(Id
);
1276 End_Package_Scope
(Id
);
1278 -- For the declaration of a library unit that is a remote types package,
1279 -- check legality rules regarding availability of stream attributes for
1280 -- types that contain non-remote access values. This subprogram performs
1281 -- visibility tests that rely on the fact that we have exited the scope
1284 if Is_Comp_Unit
then
1285 Validate_RT_RAT_Component
(N
);
1288 if Debug_Flag_C
then
1290 Write_Str
("<== package spec ");
1291 Write_Name
(Chars
(Id
));
1292 Write_Str
(" from ");
1293 Write_Location
(Sloc
(N
));
1296 end Analyze_Package_Declaration
;
1298 -----------------------------------
1299 -- Analyze_Package_Specification --
1300 -----------------------------------
1302 -- Note that this code is shared for the analysis of generic package specs
1303 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1305 procedure Analyze_Package_Specification
(N
: Node_Id
) is
1306 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1307 Orig_Decl
: constant Node_Id
:= Original_Node
(Parent
(N
));
1308 Vis_Decls
: constant List_Id
:= Visible_Declarations
(N
);
1309 Priv_Decls
: constant List_Id
:= Private_Declarations
(N
);
1312 Public_Child
: Boolean;
1314 Private_With_Clauses_Installed
: Boolean := False;
1315 -- In Ada 2005, private with_clauses are visible in the private part
1316 -- of a nested package, even if it appears in the public part of the
1317 -- enclosing package. This requires a separate step to install these
1318 -- private_with_clauses, and remove them at the end of the nested
1321 procedure Clear_Constants
(Id
: Entity_Id
);
1322 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
1323 -- and Is_True_Constant) on all variables that are entities of Id.
1324 -- A recursive call is made for all packages and generic packages.
1326 procedure Generate_Parent_References
;
1327 -- For a child unit, generate references to parent units, for
1328 -- GNAT Studio navigation purposes.
1330 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean;
1331 -- Child and Unit are entities of compilation units. True if Child
1332 -- is a public child of Parent as defined in 10.1.1
1334 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
);
1335 -- Reject completion of an incomplete or private type declarations
1336 -- having a known discriminant part by an unchecked union.
1338 procedure Inspect_Untagged_Record_Completion
(Decls
: List_Id
);
1339 -- Find out whether a nonlimited untagged record completion has got a
1340 -- primitive equality operator and, if so, make it so that it will be
1341 -- used as the predefined operator of the private view of the record.
1343 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
);
1344 -- Given the package entity of a generic package instantiation or
1345 -- formal package whose corresponding generic is a child unit, installs
1346 -- the private declarations of each of the child unit's parents.
1347 -- This has to be done at the point of entering the instance package's
1348 -- private part rather than being done in Sem_Ch12.Install_Parent
1349 -- (which is where the parents' visible declarations are installed).
1351 ---------------------
1352 -- Clear_Constants --
1353 ---------------------
1355 procedure Clear_Constants
(Id
: Entity_Id
) is
1359 -- Ignore package renamings, not interesting and they can cause self
1360 -- referential loops in the code below.
1362 if Nkind
(Parent
(Id
)) = N_Package_Renaming_Declaration
then
1366 -- Note: in the loop below, the check for Next_Entity pointing back
1367 -- to the package entity may seem odd, but it is needed, because a
1368 -- package can contain a renaming declaration to itself, and such
1369 -- renamings are generated automatically within package instances.
1371 E
:= First_Entity
(Id
);
1372 while Present
(E
) and then E
/= Id
loop
1373 if Ekind
(E
) = E_Variable
then
1374 Set_Never_Set_In_Source
(E
, False);
1375 Set_Is_True_Constant
(E
, False);
1376 Set_Current_Value
(E
, Empty
);
1377 Set_Is_Known_Null
(E
, False);
1378 Set_Last_Assignment
(E
, Empty
);
1380 if not Can_Never_Be_Null
(E
) then
1381 Set_Is_Known_Non_Null
(E
, False);
1384 elsif Is_Package_Or_Generic_Package
(E
) then
1385 Clear_Constants
(E
);
1390 end Clear_Constants
;
1392 --------------------------------
1393 -- Generate_Parent_References --
1394 --------------------------------
1396 procedure Generate_Parent_References
is
1397 Decl
: constant Node_Id
:= Parent
(N
);
1400 if Id
= Cunit_Entity
(Main_Unit
)
1401 or else Parent
(Decl
) = Library_Unit
(Cunit
(Main_Unit
))
1403 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1405 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) not in
1406 N_Subprogram_Body | N_Subunit
1408 -- If current unit is an ancestor of main unit, generate a
1409 -- reference to its own parent.
1413 Main_Spec
: Node_Id
:= Unit
(Cunit
(Main_Unit
));
1416 if Nkind
(Main_Spec
) = N_Package_Body
then
1417 Main_Spec
:= Unit
(Library_Unit
(Cunit
(Main_Unit
)));
1420 U
:= Parent_Spec
(Main_Spec
);
1421 while Present
(U
) loop
1422 if U
= Parent
(Decl
) then
1423 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1426 elsif Nkind
(Unit
(U
)) = N_Package_Body
then
1430 U
:= Parent_Spec
(Unit
(U
));
1435 end Generate_Parent_References
;
1437 ---------------------
1438 -- Is_Public_Child --
1439 ---------------------
1441 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean is
1443 if not Is_Private_Descendant
(Child
) then
1446 if Child
= Unit
then
1447 return not Private_Present
(
1448 Parent
(Unit_Declaration_Node
(Child
)));
1450 return Is_Public_Child
(Scope
(Child
), Unit
);
1453 end Is_Public_Child
;
1455 ----------------------------------------
1456 -- Inspect_Unchecked_Union_Completion --
1457 ----------------------------------------
1459 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
) is
1463 Decl
:= First
(Decls
);
1464 while Present
(Decl
) loop
1466 -- We are looking for an incomplete or private type declaration
1467 -- with a known_discriminant_part whose full view is an
1468 -- Unchecked_Union. The seemingly useless check with Is_Type
1469 -- prevents cascaded errors when routines defined only for type
1470 -- entities are called with non-type entities.
1472 if Nkind
(Decl
) in N_Incomplete_Type_Declaration
1473 | N_Private_Type_Declaration
1474 and then Is_Type
(Defining_Identifier
(Decl
))
1475 and then Has_Discriminants
(Defining_Identifier
(Decl
))
1476 and then Present
(Full_View
(Defining_Identifier
(Decl
)))
1478 Is_Unchecked_Union
(Full_View
(Defining_Identifier
(Decl
)))
1481 ("completion of discriminated partial view "
1482 & "cannot be an unchecked union",
1483 Full_View
(Defining_Identifier
(Decl
)));
1488 end Inspect_Unchecked_Union_Completion
;
1490 ----------------------------------------
1491 -- Inspect_Untagged_Record_Completion --
1492 ----------------------------------------
1494 procedure Inspect_Untagged_Record_Completion
(Decls
: List_Id
) is
1498 Decl
:= First
(Decls
);
1499 while Present
(Decl
) loop
1501 -- We are looking for a full type declaration of an untagged
1502 -- record with a private declaration and primitive operations.
1504 if Nkind
(Decl
) in N_Full_Type_Declaration
1505 and then Is_Record_Type
(Defining_Identifier
(Decl
))
1506 and then not Is_Limited_Type
(Defining_Identifier
(Decl
))
1507 and then not Is_Tagged_Type
(Defining_Identifier
(Decl
))
1508 and then Has_Private_Declaration
(Defining_Identifier
(Decl
))
1509 and then Has_Primitive_Operations
(Defining_Identifier
(Decl
))
1512 Prim_List
: constant Elist_Id
:=
1513 Collect_Primitive_Operations
(Defining_Identifier
(Decl
));
1522 Prim
:= First_Elmt
(Prim_List
);
1523 while Present
(Prim
) loop
1524 Op_Id
:= Node
(Prim
);
1525 Op_Decl
:= Declaration_Node
(Op_Id
);
1526 if Nkind
(Op_Decl
) in N_Subprogram_Specification
then
1527 Op_Decl
:= Parent
(Op_Decl
);
1530 -- We are looking for an equality operator immediately
1531 -- visible and declared in the private part followed by
1532 -- the synthesized inequality operator.
1534 if Is_User_Defined_Equality
(Op_Id
)
1535 and then Is_Immediately_Visible
(Op_Id
)
1536 and then List_Containing
(Op_Decl
) = Decls
1538 Ne_Id
:= Next_Entity
(Op_Id
);
1539 pragma Assert
(Ekind
(Ne_Id
) = E_Function
1540 and then Corresponding_Equality
(Ne_Id
) = Op_Id
);
1542 E
:= First_Private_Entity
(Id
);
1544 -- Move them from the private part of the entity list
1545 -- up to the end of the visible part of the same list.
1547 Remove_Entity
(Op_Id
);
1548 Remove_Entity
(Ne_Id
);
1550 Link_Entities
(Prev_Entity
(E
), Op_Id
);
1551 Link_Entities
(Op_Id
, Ne_Id
);
1552 Link_Entities
(Ne_Id
, E
);
1554 -- And if the private part contains another equality
1555 -- operator, move the equality operator to after it
1556 -- in the homonym chain, so that all its next homonyms
1557 -- in the same scope, if any, also are in the visible
1558 -- part. This is relied upon to resolve expanded names
1559 -- in Collect_Interps for example.
1561 while Present
(E
) loop
1562 exit when Ekind
(E
) = E_Function
1563 and then Chars
(E
) = Name_Op_Eq
;
1569 Remove_Homonym
(Op_Id
);
1571 Set_Homonym
(Op_Id
, Homonym
(E
));
1572 Set_Homonym
(E
, Op_Id
);
1585 end Inspect_Untagged_Record_Completion
;
1587 -----------------------------------------
1588 -- Install_Parent_Private_Declarations --
1589 -----------------------------------------
1591 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
) is
1592 Inst_Par
: Entity_Id
;
1593 Gen_Par
: Entity_Id
;
1594 Inst_Node
: Node_Id
;
1597 Inst_Par
:= Inst_Id
;
1600 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
1601 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
1602 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
1604 if Nkind
(Inst_Node
) in
1605 N_Package_Instantiation | N_Formal_Package_Declaration
1606 and then Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
1608 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
1610 if Present
(Renamed_Entity
(Inst_Par
)) then
1611 Inst_Par
:= Renamed_Entity
(Inst_Par
);
1614 -- The instance may appear in a sibling generic unit, in
1615 -- which case the prefix must include the common (generic)
1616 -- ancestor, which is treated as a current instance.
1619 and then Ekind
(Inst_Par
) = E_Generic_Package
1621 Gen_Par
:= Inst_Par
;
1622 pragma Assert
(In_Open_Scopes
(Gen_Par
));
1627 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
1630 -- Install the private declarations and private use clauses
1631 -- of a parent instance of the child instance, unless the
1632 -- parent instance private declarations have already been
1633 -- installed earlier in Analyze_Package_Specification, which
1634 -- happens when a generic child is instantiated, and the
1635 -- instance is a child of the parent instance.
1637 -- Installing the use clauses of the parent instance twice
1638 -- is both unnecessary and wrong, because it would cause the
1639 -- clauses to be chained to themselves in the use clauses
1640 -- list of the scope stack entry. That in turn would cause
1641 -- an endless loop from End_Use_Clauses upon scope exit.
1643 -- The parent is now fully visible. It may be a hidden open
1644 -- scope if we are currently compiling some child instance
1645 -- declared within it, but while the current instance is being
1646 -- compiled the parent is immediately visible. In particular
1647 -- its entities must remain visible if a stack save/restore
1648 -- takes place through a call to Rtsfind.
1650 if Present
(Gen_Par
) then
1651 if not In_Private_Part
(Inst_Par
) then
1652 Install_Private_Declarations
(Inst_Par
);
1653 Set_Use
(Private_Declarations
1655 (Unit_Declaration_Node
(Inst_Par
))));
1656 Set_Is_Hidden_Open_Scope
(Inst_Par
, False);
1659 -- If we've reached the end of the generic instance parents,
1660 -- then finish off by looping through the nongeneric parents
1661 -- and installing their private declarations.
1663 -- If one of the non-generic parents is itself on the scope
1664 -- stack, do not install its private declarations: they are
1665 -- installed in due time when the private part of that parent
1669 while Present
(Inst_Par
)
1670 and then Inst_Par
/= Standard_Standard
1671 and then (not In_Open_Scopes
(Inst_Par
)
1672 or else not In_Private_Part
(Inst_Par
))
1674 if Nkind
(Inst_Node
) = N_Formal_Package_Declaration
1676 not Is_Ancestor_Package
1677 (Inst_Par
, Cunit_Entity
(Current_Sem_Unit
))
1679 Install_Private_Declarations
(Inst_Par
);
1681 (Private_Declarations
1683 (Unit_Declaration_Node
(Inst_Par
))));
1684 Inst_Par
:= Scope
(Inst_Par
);
1697 end Install_Parent_Private_Declarations
;
1699 -- Start of processing for Analyze_Package_Specification
1702 if Present
(Vis_Decls
) then
1703 Analyze_Declarations
(Vis_Decls
);
1706 -- Inspect the entities defined in the package and ensure that all
1707 -- incomplete types have received full declarations. Build default
1708 -- initial condition and invariant procedures for all qualifying types.
1710 E
:= First_Entity
(Id
);
1711 while Present
(E
) loop
1713 -- Check on incomplete types
1715 -- AI05-0213: A formal incomplete type has no completion, and neither
1716 -- does the corresponding subtype in an instance.
1718 if Is_Incomplete_Type
(E
)
1719 and then No
(Full_View
(E
))
1720 and then not Is_Generic_Type
(E
)
1721 and then not From_Limited_With
(E
)
1722 and then not Is_Generic_Actual_Type
(E
)
1724 Error_Msg_N
("no declaration in visible part for incomplete}", E
);
1730 if Is_Remote_Call_Interface
(Id
)
1731 and then Nkind
(Parent
(Parent
(N
))) = N_Compilation_Unit
1733 Validate_RCI_Declarations
(Id
);
1736 -- Save global references in the visible declarations, before installing
1737 -- private declarations of parent unit if there is one, because the
1738 -- privacy status of types defined in the parent will change. This is
1739 -- only relevant for generic child units, but is done in all cases for
1742 if Ekind
(Id
) = E_Generic_Package
1743 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1746 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1747 Save_Priv
: constant List_Id
:= Private_Declarations
(Orig_Spec
);
1750 -- Insert the freezing nodes after the visible declarations to
1751 -- ensure that we analyze its aspects; needed to ensure that
1752 -- global entities referenced in the aspects are properly handled.
1754 if Ada_Version
>= Ada_2012
1755 and then Is_Non_Empty_List
(Vis_Decls
)
1756 and then Is_Empty_List
(Priv_Decls
)
1758 Insert_List_After_And_Analyze
1759 (Last
(Vis_Decls
), Freeze_Entity
(Id
, Last
(Vis_Decls
)));
1762 Set_Private_Declarations
(Orig_Spec
, Empty_List
);
1763 Save_Global_References
(Orig_Decl
);
1764 Set_Private_Declarations
(Orig_Spec
, Save_Priv
);
1768 -- If package is a public child unit, then make the private declarations
1769 -- of the parent visible.
1771 Public_Child
:= False;
1775 Pack_Decl
: Node_Id
;
1780 Par_Spec
:= Parent_Spec
(Parent
(N
));
1782 -- If the package is formal package of an enclosing generic, it is
1783 -- transformed into a local generic declaration, and compiled to make
1784 -- its spec available. We need to retrieve the original generic to
1785 -- determine whether it is a child unit, and install its parents.
1789 Nkind
(Original_Node
(Parent
(N
))) = N_Formal_Package_Declaration
1791 Par
:= Entity
(Name
(Original_Node
(Parent
(N
))));
1792 Par_Spec
:= Parent_Spec
(Unit_Declaration_Node
(Par
));
1795 if Present
(Par_Spec
) then
1796 Generate_Parent_References
;
1798 while Scope
(Par
) /= Standard_Standard
1799 and then Is_Public_Child
(Id
, Par
)
1800 and then In_Open_Scopes
(Par
)
1802 Public_Child
:= True;
1804 Install_Private_Declarations
(Par
);
1805 Install_Private_With_Clauses
(Par
);
1806 Pack_Decl
:= Unit_Declaration_Node
(Par
);
1807 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
1812 if Is_Compilation_Unit
(Id
) then
1813 Install_Private_With_Clauses
(Id
);
1815 -- The current compilation unit may include private with_clauses,
1816 -- which are visible in the private part of the current nested
1817 -- package, and have to be installed now. This is not done for
1818 -- nested instantiations, where the private with_clauses of the
1819 -- enclosing unit have no effect once the instantiation info is
1820 -- established and we start analyzing the package declaration.
1823 Comp_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
1825 if Is_Package_Or_Generic_Package
(Comp_Unit
)
1826 and then not In_Private_Part
(Comp_Unit
)
1827 and then not In_Instance
1829 Install_Private_With_Clauses
(Comp_Unit
);
1830 Private_With_Clauses_Installed
:= True;
1835 -- If this is a package associated with a generic instance or formal
1836 -- package, then the private declarations of each of the generic's
1837 -- parents must be installed at this point, but not if this is the
1838 -- abbreviated instance created to check a formal package, see the
1839 -- same condition in Analyze_Package_Instantiation.
1841 if Is_Generic_Instance
(Id
)
1842 and then not Is_Abbreviated_Instance
(Id
)
1844 Install_Parent_Private_Declarations
(Id
);
1847 -- Analyze private part if present. The flag In_Private_Part is reset
1848 -- in Uninstall_Declarations.
1850 L
:= Last_Entity
(Id
);
1852 if Present
(Priv_Decls
) then
1853 Set_In_Private_Part
(Id
);
1855 -- Upon entering a public child's private part, it may be necessary
1856 -- to declare subprograms that were derived in the package's visible
1857 -- part but not yet made visible.
1859 if Public_Child
then
1860 Declare_Inherited_Private_Subprograms
(Id
);
1863 Analyze_Declarations
(Priv_Decls
);
1865 -- Check the private declarations for incomplete deferred constants
1867 Inspect_Deferred_Constant_Completion
(Priv_Decls
);
1869 -- The first private entity is the immediate follower of the last
1870 -- visible entity, if there was one.
1873 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1875 Set_First_Private_Entity
(Id
, First_Entity
(Id
));
1878 -- There may be inherited private subprograms that need to be declared,
1879 -- even in the absence of an explicit private part. If there are any
1880 -- public declarations in the package and the package is a public child
1881 -- unit, then an implicit private part is assumed.
1883 elsif Present
(L
) and then Public_Child
then
1884 Set_In_Private_Part
(Id
);
1885 Declare_Inherited_Private_Subprograms
(Id
);
1886 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1889 E
:= First_Entity
(Id
);
1890 while Present
(E
) loop
1892 -- Check rule of 3.6(11), which in general requires waiting till all
1893 -- full types have been seen.
1895 if Ekind
(E
) = E_Record_Type
or else Ekind
(E
) = E_Array_Type
then
1896 Check_Aliased_Component_Types
(E
);
1899 -- Check preelaborable initialization for full type completing a
1900 -- private type when aspect Preelaborable_Initialization is True
1901 -- or is specified by Preelaborable_Initialization attributes
1902 -- (in the case of a private type in a generic unit). We pass
1903 -- the expression of the aspect (when present) to the parameter
1904 -- Preelab_Init_Expr to take into account the rule that presumes
1905 -- that subcomponents of generic formal types mentioned in the
1906 -- type's P_I aspect have preelaborable initialization (see
1907 -- AI12-0409 and RM 10.2.1(11.8/5)).
1909 if Is_Type
(E
) and then Must_Have_Preelab_Init
(E
) then
1911 PI_Aspect
: constant Node_Id
:=
1913 (E
, Aspect_Preelaborable_Initialization
);
1914 PI_Expr
: Node_Id
:= Empty
;
1916 if Present
(PI_Aspect
) then
1917 PI_Expr
:= Expression
(PI_Aspect
);
1920 if not Has_Preelaborable_Initialization
1921 (E
, Preelab_Init_Expr
=> PI_Expr
)
1924 ("full view of & does not have "
1925 & "preelaborable initialization", E
);
1933 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1934 -- declaration having a known_discriminant_part shall not be an
1935 -- unchecked union type.
1937 if Present
(Vis_Decls
) then
1938 Inspect_Unchecked_Union_Completion
(Vis_Decls
);
1941 if Present
(Priv_Decls
) then
1942 Inspect_Unchecked_Union_Completion
(Priv_Decls
);
1945 -- Implement AI12-0101 (which only removes a legality rule) and then
1946 -- AI05-0123 (which directly applies in the previously illegal case)
1947 -- in Ada 2012. Note that AI12-0101 is a binding interpretation.
1949 if Present
(Priv_Decls
) and then Ada_Version
>= Ada_2012
then
1950 Inspect_Untagged_Record_Completion
(Priv_Decls
);
1953 if Ekind
(Id
) = E_Generic_Package
1954 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1955 and then Present
(Priv_Decls
)
1957 -- Save global references in private declarations, ignoring the
1958 -- visible declarations that were processed earlier.
1961 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1962 Save_Vis
: constant List_Id
:= Visible_Declarations
(Orig_Spec
);
1963 Save_Form
: constant List_Id
:=
1964 Generic_Formal_Declarations
(Orig_Decl
);
1967 -- Insert the freezing nodes after the private declarations to
1968 -- ensure that we analyze its aspects; needed to ensure that
1969 -- global entities referenced in the aspects are properly handled.
1971 if Ada_Version
>= Ada_2012
1972 and then Is_Non_Empty_List
(Priv_Decls
)
1974 Insert_List_After_And_Analyze
1975 (Last
(Priv_Decls
), Freeze_Entity
(Id
, Last
(Priv_Decls
)));
1978 Set_Visible_Declarations
(Orig_Spec
, Empty_List
);
1979 Set_Generic_Formal_Declarations
(Orig_Decl
, Empty_List
);
1980 Save_Global_References
(Orig_Decl
);
1981 Set_Generic_Formal_Declarations
(Orig_Decl
, Save_Form
);
1982 Set_Visible_Declarations
(Orig_Spec
, Save_Vis
);
1986 Process_End_Label
(N
, 'e', Id
);
1988 -- Remove private_with_clauses of enclosing compilation unit, if they
1991 if Private_With_Clauses_Installed
then
1992 Remove_Private_With_Clauses
(Cunit
(Current_Sem_Unit
));
1995 -- For the case of a library level package, we must go through all the
1996 -- entities clearing the indications that the value may be constant and
1997 -- not modified. Why? Because any client of this package may modify
1998 -- these values freely from anywhere. This also applies to any nested
1999 -- packages or generic packages.
2001 -- For now we unconditionally clear constants for packages that are
2002 -- instances of generic packages. The reason is that we do not have the
2003 -- body yet, and we otherwise think things are unreferenced when they
2004 -- are not. This should be fixed sometime (the effect is not terrible,
2005 -- we just lose some warnings, and also some cases of value propagation)
2008 if Is_Library_Level_Entity
(Id
)
2009 or else Is_Generic_Instance
(Id
)
2011 Clear_Constants
(Id
);
2014 -- Output relevant information as to why the package requires a body.
2015 -- Do not consider generated packages as this exposes internal symbols
2016 -- and leads to confusing messages.
2018 if List_Body_Required_Info
2019 and then In_Extended_Main_Source_Unit
(Id
)
2020 and then Unit_Requires_Body
(Id
)
2021 and then Comes_From_Source
(Id
)
2023 Unit_Requires_Body_Info
(Id
);
2026 -- Nested package specs that do not require bodies are not checked for
2027 -- ineffective use clauses due to the possibility of subunits. This is
2028 -- because at this stage it is impossible to tell whether there will be
2031 if not Unit_Requires_Body
(Id
)
2032 and then Is_Compilation_Unit
(Id
)
2033 and then not Is_Private_Descendant
(Id
)
2035 Update_Use_Clause_Chain
;
2037 end Analyze_Package_Specification
;
2039 --------------------------------------
2040 -- Analyze_Private_Type_Declaration --
2041 --------------------------------------
2043 procedure Analyze_Private_Type_Declaration
(N
: Node_Id
) is
2044 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2045 PF
: constant Boolean := Is_Pure
(Enclosing_Lib_Unit_Entity
);
2048 Generate_Definition
(Id
);
2049 Set_Is_Pure
(Id
, PF
);
2050 Reinit_Size_Align
(Id
);
2052 if not Is_Package_Or_Generic_Package
(Current_Scope
)
2053 or else In_Private_Part
(Current_Scope
)
2055 Error_Msg_N
("invalid context for private declaration", N
);
2058 New_Private_Type
(N
, Id
, N
);
2059 Set_Depends_On_Private
(Id
);
2061 -- Set the SPARK mode from the current context
2063 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
2064 Set_SPARK_Pragma_Inherited
(Id
);
2066 if Has_Aspects
(N
) then
2067 Analyze_Aspect_Specifications
(N
, Id
);
2069 end Analyze_Private_Type_Declaration
;
2071 ----------------------------------
2072 -- Check_Anonymous_Access_Types --
2073 ----------------------------------
2075 procedure Check_Anonymous_Access_Types
2076 (Spec_Id
: Entity_Id
;
2083 -- Itype references are only needed by gigi, to force elaboration of
2084 -- itypes. In the absence of code generation, they are not needed.
2086 if not Expander_Active
then
2090 E
:= First_Entity
(Spec_Id
);
2091 while Present
(E
) loop
2092 if Ekind
(E
) = E_Anonymous_Access_Type
2093 and then From_Limited_With
(E
)
2095 IR
:= Make_Itype_Reference
(Sloc
(P_Body
));
2098 if No
(Declarations
(P_Body
)) then
2099 Set_Declarations
(P_Body
, New_List
(IR
));
2101 Prepend
(IR
, Declarations
(P_Body
));
2107 end Check_Anonymous_Access_Types
;
2109 -------------------------------------------
2110 -- Declare_Inherited_Private_Subprograms --
2111 -------------------------------------------
2113 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
) is
2115 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean;
2116 -- Check whether an inherited subprogram S is an operation of an
2117 -- untagged derived type T.
2119 ---------------------
2120 -- Is_Primitive_Of --
2121 ---------------------
2123 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean is
2127 -- If the full view is a scalar type, the type is the anonymous base
2128 -- type, but the operation mentions the first subtype, so check the
2129 -- signature against the base type.
2131 if Base_Type
(Etype
(S
)) = Base_Type
(T
) then
2135 Formal
:= First_Formal
(S
);
2136 while Present
(Formal
) loop
2137 if Base_Type
(Etype
(Formal
)) = Base_Type
(T
) then
2141 Next_Formal
(Formal
);
2146 end Is_Primitive_Of
;
2153 Op_Elmt_2
: Elmt_Id
;
2154 Prim_Op
: Entity_Id
;
2155 New_Op
: Entity_Id
:= Empty
;
2156 Parent_Subp
: Entity_Id
;
2159 -- Start of processing for Declare_Inherited_Private_Subprograms
2162 E
:= First_Entity
(Id
);
2163 while Present
(E
) loop
2165 -- If the entity is a nonprivate type extension whose parent type
2166 -- is declared in an open scope, then the type may have inherited
2167 -- operations that now need to be made visible. Ditto if the entity
2168 -- is a formal derived type in a child unit.
2170 if ((Is_Derived_Type
(E
) and then not Is_Private_Type
(E
))
2172 (Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
2173 and then Is_Generic_Type
(E
)))
2174 and then In_Open_Scopes
(Scope
(Etype
(E
)))
2175 and then Is_Base_Type
(E
)
2177 if Is_Tagged_Type
(E
) then
2178 Op_List
:= Primitive_Operations
(E
);
2180 Tag
:= First_Tag_Component
(E
);
2182 Op_Elmt
:= First_Elmt
(Op_List
);
2183 while Present
(Op_Elmt
) loop
2184 Prim_Op
:= Node
(Op_Elmt
);
2186 -- Search primitives that are implicit operations with an
2187 -- internal name whose parent operation has a normal name.
2189 if Present
(Alias
(Prim_Op
))
2190 and then Find_Dispatching_Type
(Alias
(Prim_Op
)) /= E
2191 and then not Comes_From_Source
(Prim_Op
)
2192 and then Is_Internal_Name
(Chars
(Prim_Op
))
2193 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
2195 Parent_Subp
:= Alias
(Prim_Op
);
2197 -- Case 1: Check if the type has also an explicit
2198 -- overriding for this primitive.
2200 Op_Elmt_2
:= Next_Elmt
(Op_Elmt
);
2201 while Present
(Op_Elmt_2
) loop
2203 -- Skip entities with attribute Interface_Alias since
2204 -- they are not overriding primitives (these entities
2205 -- link an interface primitive with their covering
2208 if Chars
(Node
(Op_Elmt_2
)) = Chars
(Parent_Subp
)
2209 and then Type_Conformant
(Prim_Op
, Node
(Op_Elmt_2
))
2210 and then No
(Interface_Alias
(Node
(Op_Elmt_2
)))
2212 -- The private inherited operation has been
2213 -- overridden by an explicit subprogram:
2214 -- replace the former by the latter.
2216 New_Op
:= Node
(Op_Elmt_2
);
2217 Replace_Elmt
(Op_Elmt
, New_Op
);
2218 Remove_Elmt
(Op_List
, Op_Elmt_2
);
2219 Set_Overridden_Operation
(New_Op
, Parent_Subp
);
2220 Set_Is_Ada_2022_Only
(New_Op
,
2221 Is_Ada_2022_Only
(Parent_Subp
));
2223 -- We don't need to inherit its dispatching slot.
2224 -- Set_All_DT_Position has previously ensured that
2225 -- the same slot was assigned to the two primitives
2228 and then Present
(DTC_Entity
(New_Op
))
2229 and then Present
(DTC_Entity
(Prim_Op
))
2232 (DT_Position
(New_Op
) = DT_Position
(Prim_Op
));
2236 goto Next_Primitive
;
2239 Next_Elmt
(Op_Elmt_2
);
2242 -- Case 2: We have not found any explicit overriding and
2243 -- hence we need to declare the operation (i.e., make it
2246 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
2248 -- Inherit the dispatching slot if E is already frozen
2251 and then Present
(DTC_Entity
(Alias
(Prim_Op
)))
2253 Set_DTC_Entity_Value
(E
, New_Op
);
2254 Set_DT_Position_Value
(New_Op
,
2255 DT_Position
(Alias
(Prim_Op
)));
2259 (Is_Dispatching_Operation
(New_Op
)
2260 and then Node
(Last_Elmt
(Op_List
)) = New_Op
);
2262 -- Substitute the new operation for the old one in the
2263 -- type's primitive operations list. Since the new
2264 -- operation was also just added to the end of list,
2265 -- the last element must be removed.
2267 -- (Question: is there a simpler way of declaring the
2268 -- operation, say by just replacing the name of the
2269 -- earlier operation, reentering it in the in the symbol
2270 -- table (how?), and marking it as private???)
2272 Replace_Elmt
(Op_Elmt
, New_Op
);
2273 Remove_Last_Elmt
(Op_List
);
2277 Next_Elmt
(Op_Elmt
);
2280 -- Generate listing showing the contents of the dispatch table
2282 if Debug_Flag_ZZ
then
2287 -- For untagged type, scan forward to locate inherited hidden
2290 Prim_Op
:= Next_Entity
(E
);
2291 while Present
(Prim_Op
) loop
2292 if Is_Subprogram
(Prim_Op
)
2293 and then Present
(Alias
(Prim_Op
))
2294 and then not Comes_From_Source
(Prim_Op
)
2295 and then Is_Internal_Name
(Chars
(Prim_Op
))
2296 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
2297 and then Is_Primitive_Of
(E
, Prim_Op
)
2299 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
2302 Next_Entity
(Prim_Op
);
2304 -- Derived operations appear immediately after the type
2305 -- declaration (or the following subtype indication for
2306 -- a derived scalar type). Further declarations cannot
2307 -- include inherited operations of the type.
2309 exit when Present
(Prim_Op
)
2310 and then not Is_Overloadable
(Prim_Op
);
2317 end Declare_Inherited_Private_Subprograms
;
2319 -----------------------
2320 -- End_Package_Scope --
2321 -----------------------
2323 procedure End_Package_Scope
(P
: Entity_Id
) is
2325 Uninstall_Declarations
(P
);
2327 end End_Package_Scope
;
2329 ---------------------------
2330 -- Exchange_Declarations --
2331 ---------------------------
2333 procedure Exchange_Declarations
(Id
: Entity_Id
) is
2334 Full_Id
: constant Entity_Id
:= Full_View
(Id
);
2335 H1
: constant Entity_Id
:= Homonym
(Id
);
2336 Next1
: constant Entity_Id
:= Next_Entity
(Id
);
2341 -- If missing full declaration for type, nothing to exchange
2343 if No
(Full_Id
) then
2347 -- Otherwise complete the exchange, and preserve semantic links
2349 Next2
:= Next_Entity
(Full_Id
);
2350 H2
:= Homonym
(Full_Id
);
2352 -- Reset full declaration pointer to reflect the switched entities and
2353 -- readjust the next entity chains.
2355 Exchange_Entities
(Id
, Full_Id
);
2357 Link_Entities
(Id
, Next1
);
2358 Set_Homonym
(Id
, H1
);
2360 Set_Full_View
(Full_Id
, Id
);
2361 Link_Entities
(Full_Id
, Next2
);
2362 Set_Homonym
(Full_Id
, H2
);
2363 end Exchange_Declarations
;
2365 ----------------------------
2366 -- Install_Package_Entity --
2367 ----------------------------
2369 procedure Install_Package_Entity
(Id
: Entity_Id
) is
2371 if not Is_Internal
(Id
) then
2372 if Debug_Flag_E
then
2373 Write_Str
("Install: ");
2374 Write_Name
(Chars
(Id
));
2378 if Is_Child_Unit
(Id
) then
2381 -- Do not enter implicitly inherited non-overridden subprograms of
2382 -- a tagged type back into visibility if they have non-conformant
2383 -- homographs (Ada RM 8.3 12.3/2).
2385 elsif Is_Hidden_Non_Overridden_Subpgm
(Id
) then
2389 Set_Is_Immediately_Visible
(Id
);
2392 end Install_Package_Entity
;
2394 ----------------------------------
2395 -- Install_Private_Declarations --
2396 ----------------------------------
2398 procedure Install_Private_Declarations
(P
: Entity_Id
) is
2401 Priv_Deps
: Elist_Id
;
2403 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2404 -- When the full view of a private type is made available, we do the
2405 -- same for its private dependents under proper visibility conditions.
2406 -- When compiling a child unit this needs to be done recursively.
2408 -----------------------------
2409 -- Swap_Private_Dependents --
2410 -----------------------------
2412 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
2416 Priv_Elmt
: Elmt_Id
;
2420 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
2421 while Present
(Priv_Elmt
) loop
2422 Priv
:= Node
(Priv_Elmt
);
2424 -- Before the exchange, verify that the presence of the Full_View
2425 -- field. This field will be empty if the entity has already been
2426 -- installed due to a previous call.
2428 if Present
(Full_View
(Priv
)) and then Is_Visible_Dependent
(Priv
)
2430 if Is_Private_Type
(Priv
) then
2431 Cunit
:= Cunit_Entity
(Current_Sem_Unit
);
2432 Deps
:= Private_Dependents
(Priv
);
2438 -- For each subtype that is swapped, we also swap the reference
2439 -- to it in Private_Dependents, to allow access to it when we
2440 -- swap them out in End_Package_Scope.
2442 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
2444 -- Ensure that both views of the dependent private subtype are
2445 -- immediately visible if within some open scope. Check full
2446 -- view before exchanging views.
2448 if In_Open_Scopes
(Scope
(Full_View
(Priv
))) then
2449 Set_Is_Immediately_Visible
(Priv
);
2452 Exchange_Declarations
(Priv
);
2453 Set_Is_Immediately_Visible
2454 (Priv
, In_Open_Scopes
(Scope
(Priv
)));
2456 Set_Is_Potentially_Use_Visible
2457 (Priv
, Is_Potentially_Use_Visible
(Node
(Priv_Elmt
)));
2459 -- Recurse for child units, except in generic child units,
2460 -- which unfortunately handle private_dependents separately.
2461 -- Note that the current unit may not have been analyzed,
2462 -- for example a package body, so we cannot rely solely on
2463 -- the Is_Child_Unit flag, but that's only an optimization.
2466 and then (No
(Etype
(Cunit
)) or else Is_Child_Unit
(Cunit
))
2467 and then not Is_Empty_Elmt_List
(Deps
)
2468 and then not Inside_A_Generic
2470 Swap_Private_Dependents
(Deps
);
2474 Next_Elmt
(Priv_Elmt
);
2476 end Swap_Private_Dependents
;
2478 -- Start of processing for Install_Private_Declarations
2481 -- First exchange declarations for private types, so that the full
2482 -- declaration is visible. For each private type, we check its
2483 -- Private_Dependents list and also exchange any subtypes of or derived
2484 -- types from it. Finally, if this is a Taft amendment type, the
2485 -- incomplete declaration is irrelevant, and we want to link the
2486 -- eventual full declaration with the original private one so we
2487 -- also skip the exchange.
2489 Id
:= First_Entity
(P
);
2490 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2491 if Is_Private_Base_Type
(Id
)
2492 and then Present
(Full_View
(Id
))
2493 and then Comes_From_Source
(Full_View
(Id
))
2494 and then Scope
(Full_View
(Id
)) = Scope
(Id
)
2495 and then Ekind
(Full_View
(Id
)) /= E_Incomplete_Type
2497 -- If there is a use-type clause on the private type, set the full
2498 -- view accordingly.
2500 Set_In_Use
(Full_View
(Id
), In_Use
(Id
));
2501 Full
:= Full_View
(Id
);
2503 if Is_Private_Base_Type
(Full
)
2504 and then Has_Private_Declaration
(Full
)
2505 and then Nkind
(Parent
(Full
)) = N_Full_Type_Declaration
2506 and then In_Open_Scopes
(Scope
(Etype
(Full
)))
2507 and then In_Package_Body
(Current_Scope
)
2508 and then not Is_Private_Type
(Etype
(Full
))
2510 -- This is the completion of a private type by a derivation
2511 -- from another private type which is not private anymore. This
2512 -- can only happen in a package nested within a child package,
2513 -- when the parent type is defined in the parent unit. At this
2514 -- point the current type is not private either, and we have
2515 -- to install the underlying full view, which is now visible.
2516 -- Save the current full view as well, so that all views can be
2517 -- restored on exit. It may seem that after compiling the child
2518 -- body there are not environments to restore, but the back-end
2519 -- expects those links to be valid, and freeze nodes depend on
2522 if No
(Full_View
(Full
))
2523 and then Present
(Underlying_Full_View
(Full
))
2525 Set_Full_View
(Id
, Underlying_Full_View
(Full
));
2526 Set_Underlying_Full_View
(Id
, Full
);
2527 Set_Is_Underlying_Full_View
(Full
);
2529 Set_Underlying_Full_View
(Full
, Empty
);
2530 Set_Is_Frozen
(Full_View
(Id
));
2534 Priv_Deps
:= Private_Dependents
(Id
);
2535 Exchange_Declarations
(Id
);
2536 Set_Is_Immediately_Visible
(Id
);
2537 Swap_Private_Dependents
(Priv_Deps
);
2543 -- Next make other declarations in the private part visible as well
2545 Id
:= First_Private_Entity
(P
);
2546 while Present
(Id
) loop
2547 Install_Package_Entity
(Id
);
2548 Set_Is_Hidden
(Id
, False);
2552 -- An abstract state is partially refined when it has at least one
2553 -- Part_Of constituent. Since these constituents are being installed
2554 -- into visibility, update the partial refinement status of any state
2555 -- defined in the associated package, subject to at least one Part_Of
2558 if Is_Package_Or_Generic_Package
(P
) then
2560 States
: constant Elist_Id
:= Abstract_States
(P
);
2561 State_Elmt
: Elmt_Id
;
2562 State_Id
: Entity_Id
;
2565 if Present
(States
) then
2566 State_Elmt
:= First_Elmt
(States
);
2567 while Present
(State_Elmt
) loop
2568 State_Id
:= Node
(State_Elmt
);
2570 if Present
(Part_Of_Constituents
(State_Id
)) then
2571 Set_Has_Partial_Visible_Refinement
(State_Id
);
2574 Next_Elmt
(State_Elmt
);
2580 -- Indicate that the private part is currently visible, so it can be
2581 -- properly reset on exit.
2583 Set_In_Private_Part
(P
);
2584 end Install_Private_Declarations
;
2586 ----------------------------------
2587 -- Install_Visible_Declarations --
2588 ----------------------------------
2590 procedure Install_Visible_Declarations
(P
: Entity_Id
) is
2592 Last_Entity
: Entity_Id
;
2596 (Is_Package_Or_Generic_Package
(P
) or else Is_Record_Type
(P
));
2598 if Is_Package_Or_Generic_Package
(P
) then
2599 Last_Entity
:= First_Private_Entity
(P
);
2601 Last_Entity
:= Empty
;
2604 Id
:= First_Entity
(P
);
2605 while Present
(Id
) and then Id
/= Last_Entity
loop
2606 Install_Package_Entity
(Id
);
2609 end Install_Visible_Declarations
;
2611 --------------------------
2612 -- Is_Private_Base_Type --
2613 --------------------------
2615 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean is
2617 return Ekind
(E
) = E_Private_Type
2618 or else Ekind
(E
) = E_Limited_Private_Type
2619 or else Ekind
(E
) = E_Record_Type_With_Private
;
2620 end Is_Private_Base_Type
;
2622 --------------------------
2623 -- Is_Visible_Dependent --
2624 --------------------------
2626 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean
2628 S
: constant Entity_Id
:= Scope
(Dep
);
2631 -- Renamings created for actual types have the visibility of the actual
2633 if Ekind
(S
) = E_Package
2634 and then Is_Generic_Instance
(S
)
2635 and then (Is_Generic_Actual_Type
(Dep
)
2636 or else Is_Generic_Actual_Type
(Full_View
(Dep
)))
2640 elsif not (Is_Derived_Type
(Dep
))
2641 and then Is_Derived_Type
(Full_View
(Dep
))
2643 -- When instantiating a package body, the scope stack is empty, so
2644 -- check instead whether the dependent type is defined in the same
2645 -- scope as the instance itself.
2647 return In_Open_Scopes
(S
)
2648 or else (Is_Generic_Instance
(Current_Scope
)
2649 and then Scope
(Dep
) = Scope
(Current_Scope
));
2653 end Is_Visible_Dependent
;
2655 ----------------------------
2656 -- May_Need_Implicit_Body --
2657 ----------------------------
2659 procedure May_Need_Implicit_Body
(E
: Entity_Id
) is
2660 P
: constant Node_Id
:= Unit_Declaration_Node
(E
);
2661 S
: constant Node_Id
:= Parent
(P
);
2666 if not Has_Completion
(E
)
2667 and then Nkind
(P
) = N_Package_Declaration
2668 and then (Present
(Activation_Chain_Entity
(P
)) or else Has_RACW
(E
))
2671 Make_Package_Body
(Sloc
(E
),
2672 Defining_Unit_Name
=> Make_Defining_Identifier
(Sloc
(E
),
2673 Chars
=> Chars
(E
)),
2674 Declarations
=> New_List
);
2676 if Nkind
(S
) = N_Package_Specification
then
2677 if Present
(Private_Declarations
(S
)) then
2678 Decls
:= Private_Declarations
(S
);
2680 Decls
:= Visible_Declarations
(S
);
2683 Decls
:= Declarations
(S
);
2689 end May_Need_Implicit_Body
;
2691 ----------------------
2692 -- New_Private_Type --
2693 ----------------------
2695 procedure New_Private_Type
(N
: Node_Id
; Id
: Entity_Id
; Def
: Node_Id
) is
2697 -- For other than Ada 2012, enter the name in the current scope
2699 if Ada_Version
< Ada_2012
then
2702 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2703 -- there may be an incomplete previous view.
2709 Prev
:= Find_Type_Name
(N
);
2710 pragma Assert
(Prev
= Id
2711 or else (Ekind
(Prev
) = E_Incomplete_Type
2712 and then Present
(Full_View
(Prev
))
2713 and then Full_View
(Prev
) = Id
));
2717 if Limited_Present
(Def
) then
2718 Mutate_Ekind
(Id
, E_Limited_Private_Type
);
2720 Mutate_Ekind
(Id
, E_Private_Type
);
2724 Set_Has_Delayed_Freeze
(Id
);
2725 Set_Is_First_Subtype
(Id
);
2726 Reinit_Size_Align
(Id
);
2728 Set_Is_Constrained
(Id
,
2729 No
(Discriminant_Specifications
(N
))
2730 and then not Unknown_Discriminants_Present
(N
));
2732 -- Set tagged flag before processing discriminants, to catch illegal
2735 Set_Is_Tagged_Type
(Id
, Tagged_Present
(Def
));
2737 Set_Discriminant_Constraint
(Id
, No_Elist
);
2738 Set_Stored_Constraint
(Id
, No_Elist
);
2740 if Present
(Discriminant_Specifications
(N
)) then
2742 Process_Discriminants
(N
);
2745 elsif Unknown_Discriminants_Present
(N
) then
2746 Set_Has_Unknown_Discriminants
(Id
);
2749 Set_Private_Dependents
(Id
, New_Elmt_List
);
2751 if Tagged_Present
(Def
) then
2752 Mutate_Ekind
(Id
, E_Record_Type_With_Private
);
2753 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2754 Set_Is_Abstract_Type
(Id
, Abstract_Present
(Def
));
2755 Set_Is_Limited_Record
(Id
, Limited_Present
(Def
));
2756 Set_Has_Delayed_Freeze
(Id
, True);
2758 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2760 if Is_RTE
(Id
, RE_Timing_Event
) then
2761 Set_Has_Timing_Event
(Id
);
2764 -- Create a class-wide type with the same attributes
2766 Make_Class_Wide_Type
(Id
);
2768 elsif Abstract_Present
(Def
) then
2769 Error_Msg_N
("only a tagged type can be abstract", N
);
2771 -- We initialize the primitive operations list of an untagged private
2772 -- type to an empty element list. Do this even when Extensions_Allowed
2773 -- is False to issue better error messages. (Note: This could be done
2774 -- for all private types and shared with the tagged case above, but
2775 -- for now we do it separately.)
2778 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2780 end New_Private_Type
;
2782 ---------------------------------
2783 -- Requires_Completion_In_Body --
2784 ---------------------------------
2786 function Requires_Completion_In_Body
2788 Pack_Id
: Entity_Id
;
2789 Do_Abstract_States
: Boolean := False) return Boolean
2792 -- Always ignore child units. Child units get added to the entity list
2793 -- of a parent unit, but are not original entities of the parent, and
2794 -- so do not affect whether the parent needs a body.
2796 if Is_Child_Unit
(Id
) then
2799 -- Ignore formal packages and their renamings
2801 elsif Ekind
(Id
) = E_Package
2802 and then Nkind
(Original_Node
(Unit_Declaration_Node
(Id
))) =
2803 N_Formal_Package_Declaration
2807 -- Otherwise test to see if entity requires a completion. Note that
2808 -- subprogram entities whose declaration does not come from source are
2809 -- ignored here on the basis that we assume the expander will provide an
2810 -- implicit completion at some point.
2812 elsif (Is_Overloadable
(Id
)
2813 and then Ekind
(Id
) not in E_Enumeration_Literal | E_Operator
2814 and then not Is_Abstract_Subprogram
(Id
)
2815 and then not Has_Completion
(Id
)
2816 and then Comes_From_Source
(Parent
(Id
)))
2819 (Ekind
(Id
) = E_Package
2820 and then Id
/= Pack_Id
2821 and then not Has_Completion
(Id
)
2822 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2825 (Ekind
(Id
) = E_Incomplete_Type
2826 and then No
(Full_View
(Id
))
2827 and then not Is_Generic_Type
(Id
))
2830 (Ekind
(Id
) in E_Task_Type | E_Protected_Type
2831 and then not Has_Completion
(Id
))
2834 (Ekind
(Id
) = E_Generic_Package
2835 and then Id
/= Pack_Id
2836 and then not Has_Completion
(Id
)
2837 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2840 (Is_Generic_Subprogram
(Id
)
2841 and then not Has_Completion
(Id
))
2845 -- Otherwise the entity does not require completion in a package body
2850 end Requires_Completion_In_Body
;
2852 ----------------------------
2853 -- Uninstall_Declarations --
2854 ----------------------------
2856 procedure Uninstall_Declarations
(P
: Entity_Id
) is
2857 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
2861 procedure Preserve_Full_Attributes
(Priv
: Entity_Id
; Full
: Entity_Id
);
2862 -- Copy to the private declaration the attributes of the full view that
2863 -- need to be available for the partial view also.
2865 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2866 -- When the full view of a private type is made unavailable, we do the
2867 -- same for its private dependents under proper visibility conditions.
2868 -- When compiling a child unit this needs to be done recursively.
2870 function Type_In_Use
(T
: Entity_Id
) return Boolean;
2871 -- Check whether type or base type appear in an active use_type clause
2873 ------------------------------
2874 -- Preserve_Full_Attributes --
2875 ------------------------------
2877 procedure Preserve_Full_Attributes
2881 Full_Base
: constant Entity_Id
:= Base_Type
(Full
);
2882 Priv_Is_Base_Type
: constant Boolean := Is_Base_Type
(Priv
);
2885 Set_Size_Info
(Priv
, Full
);
2886 Copy_RM_Size
(To
=> Priv
, From
=> Full
);
2887 Set_Size_Known_At_Compile_Time
2888 (Priv
, Size_Known_At_Compile_Time
(Full
));
2889 Set_Is_Volatile
(Priv
, Is_Volatile
(Full
));
2890 Set_Treat_As_Volatile
(Priv
, Treat_As_Volatile
(Full
));
2891 Set_Is_Atomic
(Priv
, Is_Atomic
(Full
));
2892 Set_Is_Ada_2005_Only
(Priv
, Is_Ada_2005_Only
(Full
));
2893 Set_Is_Ada_2012_Only
(Priv
, Is_Ada_2012_Only
(Full
));
2894 Set_Is_Ada_2022_Only
(Priv
, Is_Ada_2022_Only
(Full
));
2895 Set_Has_Pragma_Unmodified
(Priv
, Has_Pragma_Unmodified
(Full
));
2896 Set_Has_Pragma_Unreferenced
(Priv
, Has_Pragma_Unreferenced
(Full
));
2897 Set_Has_Pragma_Unreferenced_Objects
2898 (Priv
, Has_Pragma_Unreferenced_Objects
2900 Set_Predicates_Ignored
(Priv
, Predicates_Ignored
(Full
));
2901 if Is_Unchecked_Union
(Full
) then
2902 Set_Is_Unchecked_Union
(Base_Type
(Priv
));
2905 if Referenced
(Full
) then
2906 Set_Referenced
(Priv
);
2909 if Priv_Is_Base_Type
then
2910 Set_Is_Controlled_Active
2911 (Priv
, Is_Controlled_Active
(Full_Base
));
2912 Set_Finalize_Storage_Only
2913 (Priv
, Finalize_Storage_Only
(Full_Base
));
2914 Set_Has_Controlled_Component
2915 (Priv
, Has_Controlled_Component
(Full_Base
));
2917 Propagate_Concurrent_Flags
(Priv
, Base_Type
(Full
));
2920 -- As explained in Freeze_Entity, private types are required to point
2921 -- to the same freeze node as their corresponding full view, if any.
2922 -- But we ought not to overwrite a node already inserted in the tree.
2925 (Serious_Errors_Detected
/= 0
2926 or else No
(Freeze_Node
(Priv
))
2927 or else No
(Parent
(Freeze_Node
(Priv
)))
2928 or else Freeze_Node
(Priv
) = Freeze_Node
(Full
));
2930 Set_Freeze_Node
(Priv
, Freeze_Node
(Full
));
2932 -- Propagate Default_Initial_Condition-related attributes from the
2933 -- full view to the private view.
2935 Propagate_DIC_Attributes
(Priv
, From_Typ
=> Full
);
2937 -- Propagate invariant-related attributes from the full view to the
2940 Propagate_Invariant_Attributes
(Priv
, From_Typ
=> Full
);
2942 -- Propagate predicate-related attributes from the full view to the
2945 Propagate_Predicate_Attributes
(Priv
, From_Typ
=> Full
);
2947 if Is_Tagged_Type
(Priv
)
2948 and then Is_Tagged_Type
(Full
)
2949 and then not Error_Posted
(Full
)
2951 if Is_Tagged_Type
(Priv
) then
2953 -- If the type is tagged, the tag itself must be available on
2954 -- the partial view, for expansion purposes.
2956 Set_First_Entity
(Priv
, First_Entity
(Full
));
2958 -- If there are discriminants in the partial view, these remain
2959 -- visible. Otherwise only the tag itself is visible, and there
2960 -- are no nameable components in the partial view.
2962 if No
(Last_Entity
(Priv
)) then
2963 Set_Last_Entity
(Priv
, First_Entity
(Priv
));
2967 Set_Has_Discriminants
(Priv
, Has_Discriminants
(Full
));
2969 if Has_Discriminants
(Full
) then
2970 Set_Discriminant_Constraint
(Priv
,
2971 Discriminant_Constraint
(Full
));
2974 end Preserve_Full_Attributes
;
2976 -----------------------------
2977 -- Swap_Private_Dependents --
2978 -----------------------------
2980 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
2984 Priv_Elmt
: Elmt_Id
;
2988 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
2989 while Present
(Priv_Elmt
) loop
2990 Priv
:= Node
(Priv_Elmt
);
2992 -- Before we do the swap, we verify the presence of the Full_View
2993 -- field, which may be empty due to a swap by a previous call to
2994 -- End_Package_Scope (e.g. from the freezing mechanism).
2996 if Present
(Full_View
(Priv
)) then
2997 if Is_Private_Type
(Priv
) then
2998 Cunit
:= Cunit_Entity
(Current_Sem_Unit
);
2999 Deps
:= Private_Dependents
(Priv
);
3006 or else not In_Open_Scopes
(Scope
(Priv
))
3008 Set_Is_Immediately_Visible
(Priv
, False);
3011 if Is_Visible_Dependent
(Priv
) then
3012 Preserve_Full_Attributes
(Priv
, Full_View
(Priv
));
3013 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
3014 Exchange_Declarations
(Priv
);
3016 -- Recurse for child units, except in generic child units,
3017 -- which unfortunately handle private_dependents separately.
3018 -- Note that the current unit may not have been analyzed,
3019 -- for example a package body, so we cannot rely solely on
3020 -- the Is_Child_Unit flag, but that's only an optimization.
3023 and then (No
(Etype
(Cunit
)) or else Is_Child_Unit
(Cunit
))
3024 and then not Is_Empty_Elmt_List
(Deps
)
3025 and then not Inside_A_Generic
3027 Swap_Private_Dependents
(Deps
);
3032 Next_Elmt
(Priv_Elmt
);
3034 end Swap_Private_Dependents
;
3040 function Type_In_Use
(T
: Entity_Id
) return Boolean is
3042 return Scope
(Base_Type
(T
)) = P
3043 and then (In_Use
(T
) or else In_Use
(Base_Type
(T
)));
3046 -- Start of processing for Uninstall_Declarations
3049 Id
:= First_Entity
(P
);
3050 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
3051 if Debug_Flag_E
then
3052 Write_Str
("unlinking visible entity ");
3053 Write_Int
(Int
(Id
));
3057 -- On exit from the package scope, we must preserve the visibility
3058 -- established by use clauses in the current scope. Two cases:
3060 -- a) If the entity is an operator, it may be a primitive operator of
3061 -- a type for which there is a visible use-type clause.
3063 -- b) For other entities, their use-visibility is determined by a
3064 -- visible use clause for the package itself or a use-all-type clause
3065 -- applied directly to the entity's type. For a generic instance,
3066 -- the instantiation of the formals appears in the visible part,
3067 -- but the formals are private and remain so.
3069 if Ekind
(Id
) = E_Function
3070 and then Is_Operator_Symbol_Name
(Chars
(Id
))
3071 and then not Is_Hidden
(Id
)
3072 and then not Error_Posted
(Id
)
3074 Set_Is_Potentially_Use_Visible
(Id
,
3076 or else Type_In_Use
(Etype
(Id
))
3077 or else Type_In_Use
(Etype
(First_Formal
(Id
)))
3078 or else (Present
(Next_Formal
(First_Formal
(Id
)))
3081 (Etype
(Next_Formal
(First_Formal
(Id
))))));
3083 if In_Use
(P
) and then not Is_Hidden
(Id
) then
3085 -- A child unit of a use-visible package remains use-visible
3086 -- only if it is itself a visible child unit. Otherwise it
3087 -- would remain visible in other contexts where P is use-
3088 -- visible, because once compiled it stays in the entity list
3089 -- of its parent unit.
3091 if Is_Child_Unit
(Id
) then
3092 Set_Is_Potentially_Use_Visible
3093 (Id
, Is_Visible_Lib_Unit
(Id
));
3095 Set_Is_Potentially_Use_Visible
(Id
);
3098 -- Avoid crash caused by previous errors
3100 elsif No
(Etype
(Id
)) and then Serious_Errors_Detected
/= 0 then
3103 -- We need to avoid incorrectly marking enumeration literals as
3104 -- non-visible when a visible use-all-type clause is in effect.
3106 elsif Type_In_Use
(Etype
(Id
))
3107 and then Nkind
(Current_Use_Clause
(Etype
(Id
))) =
3109 and then All_Present
(Current_Use_Clause
(Etype
(Id
)))
3114 Set_Is_Potentially_Use_Visible
(Id
, False);
3118 -- Local entities are not immediately visible outside of the package
3120 Set_Is_Immediately_Visible
(Id
, False);
3122 -- If this is a private type with a full view (for example a local
3123 -- subtype of a private type declared elsewhere), ensure that the
3124 -- full view is also removed from visibility: it may be exposed when
3125 -- swapping views in an instantiation. Similarly, ensure that the
3126 -- use-visibility is properly set on both views.
3128 if Is_Type
(Id
) and then Present
(Full_View
(Id
)) then
3129 Set_Is_Immediately_Visible
(Full_View
(Id
), False);
3130 Set_Is_Potentially_Use_Visible
(Full_View
(Id
),
3131 Is_Potentially_Use_Visible
(Id
));
3134 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
3135 Check_Abstract_Overriding
(Id
);
3136 Check_Conventions
(Id
);
3139 if Ekind
(Id
) in E_Private_Type | E_Limited_Private_Type
3140 and then No
(Full_View
(Id
))
3141 and then not Is_Generic_Type
(Id
)
3142 and then not Is_Derived_Type
(Id
)
3144 Error_Msg_N
("missing full declaration for private type&", Id
);
3146 elsif Ekind
(Id
) = E_Record_Type_With_Private
3147 and then not Is_Generic_Type
(Id
)
3148 and then No
(Full_View
(Id
))
3150 if Nkind
(Parent
(Id
)) = N_Private_Type_Declaration
then
3151 Error_Msg_N
("missing full declaration for private type&", Id
);
3154 ("missing full declaration for private extension", Id
);
3157 -- Case of constant, check for deferred constant declaration with
3158 -- no full view. Likely just a matter of a missing expression, or
3159 -- accidental use of the keyword constant.
3161 elsif Ekind
(Id
) = E_Constant
3163 -- OK if constant value present
3165 and then No
(Constant_Value
(Id
))
3167 -- OK if full view present
3169 and then No
(Full_View
(Id
))
3171 -- OK if imported, since that provides the completion
3173 and then not Is_Imported
(Id
)
3175 -- OK if object declaration replaced by renaming declaration as
3176 -- a result of OK_To_Rename processing (e.g. for concatenation)
3178 and then Nkind
(Parent
(Id
)) /= N_Object_Renaming_Declaration
3180 -- OK if object declaration with the No_Initialization flag set
3182 and then not (Nkind
(Parent
(Id
)) = N_Object_Declaration
3183 and then No_Initialization
(Parent
(Id
)))
3185 -- If no private declaration is present, we assume the user did
3186 -- not intend a deferred constant declaration and the problem
3187 -- is simply that the initializing expression is missing.
3189 if not Has_Private_Declaration
(Etype
(Id
)) then
3191 -- We assume that the user did not intend a deferred constant
3192 -- declaration, and the expression is just missing.
3195 ("constant declaration requires initialization expression",
3198 if Is_Limited_Type
(Etype
(Id
)) then
3200 ("\if variable intended, remove CONSTANT from declaration",
3204 -- Otherwise if a private declaration is present, then we are
3205 -- missing the full declaration for the deferred constant.
3209 ("missing full declaration for deferred constant (RM 7.4)",
3212 if Is_Limited_Type
(Etype
(Id
)) then
3214 ("\if variable intended, remove CONSTANT from declaration",
3223 -- If the specification was installed as the parent of a public child
3224 -- unit, the private declarations were not installed, and there is
3227 if not In_Private_Part
(P
) then
3231 -- Reset the flag now
3233 Set_In_Private_Part
(P
, False);
3235 -- Make private entities invisible and exchange full and private
3236 -- declarations for private types. Id is now the first private entity
3239 while Present
(Id
) loop
3240 if Debug_Flag_E
then
3241 Write_Str
("unlinking private entity ");
3242 Write_Int
(Int
(Id
));
3246 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
3247 Check_Abstract_Overriding
(Id
);
3248 Check_Conventions
(Id
);
3251 Set_Is_Immediately_Visible
(Id
, False);
3253 if Is_Private_Base_Type
(Id
) and then Present
(Full_View
(Id
)) then
3254 Full
:= Full_View
(Id
);
3256 -- If the partial view is not declared in the visible part of the
3257 -- package (as is the case when it is a type derived from some
3258 -- other private type in the private part of the current package),
3259 -- no exchange takes place.
3262 or else List_Containing
(Parent
(Id
)) /=
3263 Visible_Declarations
(Specification
(Decl
))
3268 -- The entry in the private part points to the full declaration,
3269 -- which is currently visible. Exchange them so only the private
3270 -- type declaration remains accessible, and link private and full
3271 -- declaration in the opposite direction. Before the actual
3272 -- exchange, we copy back attributes of the full view that must
3273 -- be available to the partial view too.
3275 Preserve_Full_Attributes
(Id
, Full
);
3277 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(P
));
3279 -- The following test may be redundant, as this is already
3280 -- diagnosed in sem_ch3. ???
3282 if not Is_Definite_Subtype
(Full
)
3283 and then Is_Definite_Subtype
(Id
)
3285 Error_Msg_Sloc
:= Sloc
(Parent
(Id
));
3287 ("full view of& not compatible with declaration#", Full
, Id
);
3290 -- Swap out the subtypes and derived types of Id that
3291 -- were compiled in this scope, or installed previously
3292 -- by Install_Private_Declarations.
3294 Swap_Private_Dependents
(Private_Dependents
(Id
));
3296 -- Now restore the type itself to its private view
3298 Exchange_Declarations
(Id
);
3300 -- If we have installed an underlying full view for a type derived
3301 -- from a private type in a child unit, restore the proper views
3302 -- of private and full view. See corresponding code in
3303 -- Install_Private_Declarations.
3305 -- After the exchange, Full denotes the private type in the
3306 -- visible part of the package.
3308 if Is_Private_Base_Type
(Full
)
3309 and then Present
(Full_View
(Full
))
3310 and then Present
(Underlying_Full_View
(Full
))
3311 and then In_Package_Body
(Current_Scope
)
3313 Set_Full_View
(Full
, Underlying_Full_View
(Full
));
3314 Set_Underlying_Full_View
(Full
, Empty
);
3317 elsif Ekind
(Id
) = E_Incomplete_Type
3318 and then Comes_From_Source
(Id
)
3319 and then No
(Full_View
(Id
))
3321 -- Mark Taft amendment types. Verify that there are no primitive
3322 -- operations declared for the type (3.10.1(9)).
3324 Set_Has_Completion_In_Body
(Id
);
3331 Elmt
:= First_Elmt
(Private_Dependents
(Id
));
3332 while Present
(Elmt
) loop
3333 Subp
:= Node
(Elmt
);
3335 -- Is_Primitive is tested because there can be cases where
3336 -- nonprimitive subprograms (in nested packages) are added
3337 -- to the Private_Dependents list.
3339 if Is_Overloadable
(Subp
) and then Is_Primitive
(Subp
) then
3341 ("type& must be completed in the private part",
3344 -- The result type of an access-to-function type cannot be a
3345 -- Taft-amendment type, unless the version is Ada 2012 or
3346 -- later (see AI05-151).
3348 elsif Ada_Version
< Ada_2012
3349 and then Ekind
(Subp
) = E_Subprogram_Type
3351 if Etype
(Subp
) = Id
3353 (Is_Class_Wide_Type
(Etype
(Subp
))
3354 and then Etype
(Etype
(Subp
)) = Id
)
3357 ("type& must be completed in the private part",
3358 Associated_Node_For_Itype
(Subp
), Id
);
3366 -- For subtypes of private types the frontend generates two entities:
3367 -- one associated with the partial view and the other associated with
3368 -- the full view. When the subtype declaration is public the frontend
3369 -- places the former entity in the list of public entities of the
3370 -- package and the latter entity in the private part of the package.
3371 -- When the subtype declaration is private it generates these two
3372 -- entities but both are placed in the private part of the package
3373 -- (and the full view has the same source location as the partial
3374 -- view and no parent; see Prepare_Private_Subtype_Completion).
3376 elsif Ekind
(Id
) in E_Private_Subtype
3377 | E_Limited_Private_Subtype
3378 and then Present
(Full_View
(Id
))
3379 and then Sloc
(Id
) = Sloc
(Full_View
(Id
))
3380 and then No
(Parent
(Full_View
(Id
)))
3383 Set_Is_Potentially_Use_Visible
(Id
, False);
3385 elsif not Is_Child_Unit
(Id
)
3386 and then (not Is_Private_Type
(Id
) or else No
(Full_View
(Id
)))
3389 Set_Is_Potentially_Use_Visible
(Id
, False);
3395 end Uninstall_Declarations
;
3397 ------------------------
3398 -- Unit_Requires_Body --
3399 ------------------------
3401 function Unit_Requires_Body
3402 (Pack_Id
: Entity_Id
;
3403 Do_Abstract_States
: Boolean := False) return Boolean
3407 Requires_Body
: Boolean := False;
3408 -- Flag set when the unit has at least one construct that requires
3409 -- completion in a body.
3412 -- Imported entity never requires body. Right now, only subprograms can
3413 -- be imported, but perhaps in the future we will allow import of
3416 if Is_Imported
(Pack_Id
) then
3419 -- Body required if library package with pragma Elaborate_Body
3421 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
3424 -- Body required if subprogram
3426 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
3429 -- Treat a block as requiring a body
3431 elsif Ekind
(Pack_Id
) = E_Block
then
3434 elsif Ekind
(Pack_Id
) = E_Package
3435 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3436 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3439 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3441 if Has_Pragma_Elaborate_Body
(G_P
) then
3447 -- Traverse the entity chain of the package and look for constructs that
3448 -- require a completion in a body.
3450 E
:= First_Entity
(Pack_Id
);
3451 while Present
(E
) loop
3453 -- Skip abstract states because their completion depends on several
3454 -- criteria (see below).
3456 if Ekind
(E
) = E_Abstract_State
then
3459 elsif Requires_Completion_In_Body
3460 (E
, Pack_Id
, Do_Abstract_States
)
3462 Requires_Body
:= True;
3469 -- A [generic] package that defines at least one non-null abstract state
3470 -- requires a completion only when at least one other construct requires
3471 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3472 -- performed if the caller requests this behavior.
3474 if Do_Abstract_States
3475 and then Is_Package_Or_Generic_Package
(Pack_Id
)
3476 and then Has_Non_Null_Abstract_State
(Pack_Id
)
3477 and then Requires_Body
3482 return Requires_Body
;
3483 end Unit_Requires_Body
;
3485 -----------------------------
3486 -- Unit_Requires_Body_Info --
3487 -----------------------------
3489 procedure Unit_Requires_Body_Info
(Pack_Id
: Entity_Id
) is
3493 -- An imported entity never requires body. Right now, only subprograms
3494 -- can be imported, but perhaps in the future we will allow import of
3497 if Is_Imported
(Pack_Id
) then
3500 -- Body required if library package with pragma Elaborate_Body
3502 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
3503 Error_Msg_N
("info: & requires body (Elaborate_Body)?.y?", Pack_Id
);
3505 -- Body required if subprogram
3507 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
3508 Error_Msg_N
("info: & requires body (subprogram case)?.y?", Pack_Id
);
3510 -- Body required if generic parent has Elaborate_Body
3512 elsif Ekind
(Pack_Id
) = E_Package
3513 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3514 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3517 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3519 if Has_Pragma_Elaborate_Body
(G_P
) then
3521 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3526 -- A [generic] package that introduces at least one non-null abstract
3527 -- state requires completion. However, there is a separate rule that
3528 -- requires that such a package have a reason other than this for a
3529 -- body being required (if necessary a pragma Elaborate_Body must be
3530 -- provided). If Ignore_Abstract_State is True, we don't do this check
3531 -- (so we can use Unit_Requires_Body to check for some other reason).
3533 elsif Is_Package_Or_Generic_Package
(Pack_Id
)
3534 and then Present
(Abstract_States
(Pack_Id
))
3535 and then not Is_Null_State
3536 (Node
(First_Elmt
(Abstract_States
(Pack_Id
))))
3539 ("info: & requires body (non-null abstract state aspect)?.y?",
3543 -- Otherwise search entity chain for entity requiring completion
3545 E
:= First_Entity
(Pack_Id
);
3546 while Present
(E
) loop
3547 if Requires_Completion_In_Body
(E
, Pack_Id
) then
3548 Error_Msg_Node_2
:= E
;
3550 ("info: & requires body (& requires completion)?.y?", E
,
3556 end Unit_Requires_Body_Info
;