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 without -gnatn where we want
323 -- to favor generating static inline functions as much as
326 -- Note that we test Has_Pragma_Inline here in addition
327 -- to Is_Inlined. We are doing this for a client, since
328 -- we are computing which entities should be public, and
329 -- it is the client who will decide if actual inlining
330 -- should occur, so we need to catch all cases where the
331 -- subprogram may be inlined by the client.
334 or else Has_Pragma_Inline_Always
(Decl_Id
)
335 or else Inline_Active
)
336 and then (Is_Inlined
(Decl_Id
)
337 or else Has_Pragma_Inline
(Decl_Id
))
339 Has_Referencer_Of_Non_Subprograms
:= True;
341 -- Inspect the statements of the subprogram body
342 -- to determine whether the body references other
345 Scan_Subprogram_Refs
(Decl
);
350 end Set_Referencer_Of_Non_Subprograms
;
357 -- Examine all declarations in reverse order, hiding all entities
358 -- from external visibility until a referencer has been found. The
359 -- algorithm recurses into nested packages.
361 Decl
:= Last
(Decls
);
362 while Present
(Decl
) loop
364 -- A stub is always considered a referencer
366 if Nkind
(Decl
) in N_Body_Stub
then
369 -- Package declaration
371 elsif Nkind
(Decl
) = N_Package_Declaration
then
372 Spec
:= Specification
(Decl
);
373 Decl_Id
:= Defining_Entity
(Spec
);
375 -- Inspect the declarations of a non-generic package to try
376 -- and hide more entities from external visibility.
378 if not Is_Generic_Unit
(Decl_Id
) then
379 if In_Nested_Instance
then
381 elsif Is_Generic_Instance
(Decl_Id
) then
383 Has_Completion
(Decl_Id
)
384 or else Unit_Requires_Body
(Generic_Parent
(Spec
));
386 In_Instance
:= False;
389 if Has_Referencer
(Private_Declarations
(Spec
),
391 Has_Referencer_Of_Non_Subprograms
)
393 Has_Referencer
(Visible_Declarations
(Spec
),
395 Has_Referencer_Of_Non_Subprograms
)
403 elsif Nkind
(Decl
) = N_Package_Body
404 and then Present
(Corresponding_Spec
(Decl
))
406 Decl_Id
:= Corresponding_Spec
(Decl
);
408 -- A generic package body is a referencer. It would seem
409 -- that we only have to consider generics that can be
410 -- exported, i.e. where the corresponding spec is the
411 -- spec of the current package, but because of nested
412 -- instantiations, a fully private generic body may export
413 -- other private body entities. Furthermore, regardless of
414 -- whether there was a previous inlined subprogram, (an
415 -- instantiation of) the generic package may reference any
416 -- entity declared before it.
418 if Is_Generic_Unit
(Decl_Id
) then
421 -- Inspect the declarations of a non-generic package body to
422 -- try and hide more entities from external visibility.
424 elsif Has_Referencer
(Declarations
(Decl
),
427 Is_Generic_Instance
(Decl_Id
),
428 Has_Referencer_Of_Non_Subprograms
)
435 elsif Nkind
(Decl
) = N_Subprogram_Body
then
436 if Present
(Corresponding_Spec
(Decl
)) then
437 Decl_Id
:= Corresponding_Spec
(Decl
);
439 -- A generic subprogram body acts as a referencer
441 if Is_Generic_Unit
(Decl_Id
) then
445 Ignore
:= Set_Referencer_Of_Non_Subprograms
;
447 -- Otherwise this is a stand alone subprogram body
450 Decl_Id
:= Defining_Entity
(Decl
);
452 -- See the N_Subprogram_Declaration case below
454 if not Set_Referencer_Of_Non_Subprograms
455 and then (not In_Nested_Instance
456 or else not Subprogram_Table
.Get_First
)
457 and then not Subprogram_Table
.Get
(Decl_Id
)
459 -- We can reset Is_Public right away
460 Set_Is_Public
(Decl_Id
, False);
466 elsif Nkind
(Decl
) = N_Freeze_Entity
then
469 pragma Unreferenced
(Discard
);
471 -- Inspect the actions to find references to subprograms.
472 -- We assume that the actions do not contain other kinds
473 -- of references and, therefore, we do not stop the scan
474 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
475 -- it would pessimize common cases for which the actions
476 -- contain the declaration of an init procedure, since
477 -- such a procedure is automatically marked inline.
480 Has_Referencer
(Actions
(Decl
),
482 Has_Referencer_Of_Non_Subprograms
);
485 -- Exceptions, objects and renamings do not need to be public
486 -- if they are not followed by a construct which can reference
489 elsif Nkind
(Decl
) in N_Exception_Declaration
490 | N_Object_Declaration
491 | N_Object_Renaming_Declaration
493 Decl_Id
:= Defining_Entity
(Decl
);
495 -- We cannot say anything for objects declared in nested
496 -- instances because instantiations are not done yet so the
497 -- bodies are not visible and could contain references to
500 if not In_Nested_Instance
501 and then not Is_Imported
(Decl_Id
)
502 and then not Is_Exported
(Decl_Id
)
503 and then No
(Interface_Name
(Decl_Id
))
504 and then not Has_Referencer_Of_Non_Subprograms
506 Set_Is_Public
(Decl_Id
, False);
509 -- Likewise for subprograms and renamings, but we work harder
510 -- for them to see whether they are referenced on an individual
511 -- basis by looking into the table of referenced subprograms.
513 elsif Nkind
(Decl
) in N_Subprogram_Declaration
514 | N_Subprogram_Renaming_Declaration
516 Decl_Id
:= Defining_Entity
(Decl
);
518 -- We cannot say anything for subprograms declared in nested
519 -- instances because instantiations are not done yet so the
520 -- bodies are not visible and could contain references to
521 -- them, except if we still have no subprograms at all which
522 -- are referenced by an inlined body.
524 if (not In_Nested_Instance
525 or else not Subprogram_Table
.Get_First
)
526 and then not Is_Imported
(Decl_Id
)
527 and then not Is_Exported
(Decl_Id
)
528 and then No
(Interface_Name
(Decl_Id
))
529 and then not Subprogram_Table
.Get
(Decl_Id
)
531 Set_Is_Public
(Decl_Id
, False);
534 -- For a subprogram renaming, if the entity is referenced,
535 -- then so is the renamed subprogram. But there is an issue
536 -- with generic bodies because instantiations are not done
537 -- yet and, therefore, cannot be scanned for referencers.
538 -- That's why we use an approximation and test that we have
539 -- at least one subprogram referenced by an inlined body
540 -- instead of precisely the entity of this renaming.
542 if Nkind
(Decl
) = N_Subprogram_Renaming_Declaration
543 and then Subprogram_Table
.Get_First
544 and then Is_Entity_Name
(Name
(Decl
))
545 and then Present
(Entity
(Name
(Decl
)))
546 and then Is_Subprogram
(Entity
(Name
(Decl
)))
548 Subprogram_Table
.Set
(Entity
(Name
(Decl
)), True);
555 return Has_Referencer_Of_Non_Subprograms
;
558 -------------------------
559 -- Scan_Subprogram_Ref --
560 -------------------------
562 function Scan_Subprogram_Ref
(N
: Node_Id
) return Traverse_Result
is
564 -- Detect a reference of the form
567 if Nkind
(N
) in N_Subprogram_Call
568 and then Is_Entity_Name
(Name
(N
))
569 and then Present
(Entity
(Name
(N
)))
570 and then Is_Subprogram
(Entity
(Name
(N
)))
572 Subprogram_Table
.Set
(Entity
(Name
(N
)), True);
574 -- Detect a reference of the form
575 -- Subp'Some_Attribute
577 elsif Nkind
(N
) = N_Attribute_Reference
578 and then Is_Entity_Name
(Prefix
(N
))
579 and then Present
(Entity
(Prefix
(N
)))
580 and then Is_Subprogram
(Entity
(Prefix
(N
)))
582 Subprogram_Table
.Set
(Entity
(Prefix
(N
)), True);
584 -- Constants can be substituted by their value in gigi, which may
585 -- contain a reference, so scan the value recursively.
587 elsif Is_Entity_Name
(N
)
588 and then Present
(Entity
(N
))
589 and then Ekind
(Entity
(N
)) = E_Constant
592 Val
: constant Node_Id
:= Constant_Value
(Entity
(N
));
595 and then not Compile_Time_Known_Value
(Val
)
597 Scan_Subprogram_Refs
(Val
);
603 end Scan_Subprogram_Ref
;
605 --------------------------
606 -- Scan_Subprogram_Refs --
607 --------------------------
609 procedure Scan_Subprogram_Refs
(Node
: Node_Id
) is
611 if not Traversed_Table
.Get
(Node
) then
612 Traversed_Table
.Set
(Node
, True);
613 Traverse_And_Scan_Subprogram_Refs
(Node
);
615 end Scan_Subprogram_Refs
;
620 pragma Unreferenced
(Discard
);
622 -- Start of processing for Hide_Public_Entities
625 -- The algorithm examines the top level declarations of a package
626 -- body in reverse looking for a construct that may export entities
627 -- declared prior to it. If such a scenario is encountered, then all
628 -- entities in the range Last (Decls) .. construct are hidden from
629 -- external visibility. Consider:
637 -- package body Pack is
638 -- External_Obj : ...; -- (1)
640 -- package body Gen is -- (2)
641 -- ... External_Obj ... -- (3)
644 -- Local_Obj : ...; -- (4)
647 -- In this example Local_Obj (4) must not be externally visible as
648 -- it cannot be exported by anything in Pack. The body of generic
649 -- package Gen (2) on the other hand acts as a "referencer" and may
650 -- export anything declared before it. Since the compiler does not
651 -- perform flow analysis, it is not possible to determine precisely
652 -- which entities will be exported when Gen is instantiated. In the
653 -- example above External_Obj (1) is exported at (3), but this may
654 -- not always be the case. The algorithm takes a conservative stance
655 -- and leaves entity External_Obj public.
657 -- This very conservative algorithm is supplemented by a more precise
658 -- processing for inlined bodies. For them, we traverse the syntactic
659 -- tree and record which subprograms are actually referenced from it.
660 -- This makes it possible to compute a much smaller set of externally
661 -- visible subprograms in the absence of generic bodies, which can
662 -- have a significant impact on the inlining decisions made in the
663 -- back end and the removal of out-of-line bodies from the object
664 -- code. We do it only for inlined bodies because they are supposed
665 -- to be reasonably small and tree traversal is very expensive.
667 -- Note that even this special processing is not optimal for inlined
668 -- bodies, because we treat all inlined subprograms alike. An optimal
669 -- algorithm would require computing the transitive closure of the
670 -- inlined subprograms that can really be referenced from other units
671 -- in the source code.
673 -- We could extend this processing for inlined bodies and record all
674 -- entities, not just subprograms, referenced from them, which would
675 -- make it possible to compute a much smaller set of all externally
676 -- visible entities in the absence of generic bodies. But this would
677 -- mean implementing a more thorough tree traversal of the bodies,
678 -- i.e. not just syntactic, and the gain would very likely be worth
679 -- neither the hassle nor the slowdown of the compiler.
681 -- Finally, an important thing to be aware of is that, at this point,
682 -- instantiations are not done yet so we cannot directly see inlined
683 -- bodies coming from them. That's not catastrophic because only the
684 -- actual parameters of the instantiations matter here, and they are
685 -- present in the declarations list of the instantiated packages.
687 Traversed_Table
.Reset
;
688 Subprogram_Table
.Reset
;
689 Discard
:= Has_Referencer
(Decls
, False, False);
690 end Hide_Public_Entities
;
692 ----------------------------------
693 -- Install_Composite_Operations --
694 ----------------------------------
696 procedure Install_Composite_Operations
(P
: Entity_Id
) is
700 Id
:= First_Entity
(P
);
701 while Present
(Id
) loop
703 and then (Is_Limited_Composite
(Id
)
704 or else Is_Private_Composite
(Id
))
705 and then No
(Private_Component
(Id
))
707 Set_Is_Limited_Composite
(Id
, False);
708 Set_Is_Private_Composite
(Id
, False);
713 end Install_Composite_Operations
;
717 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
718 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
719 Saved_EA
: constant Boolean := Expander_Active
;
720 Saved_ISMP
: constant Boolean :=
721 Ignore_SPARK_Mode_Pragmas_In_Instance
;
722 -- Save the Ghost and SPARK mode-related data to restore on exit
726 Last_Spec_Entity
: Entity_Id
;
731 -- Start of processing for Analyze_Package_Body_Helper
734 -- Find corresponding package specification, and establish the current
735 -- scope. The visible defining entity for the package is the defining
736 -- occurrence in the spec. On exit from the package body, all body
737 -- declarations are attached to the defining entity for the body, but
738 -- the later is never used for name resolution. In this fashion there
739 -- is only one visible entity that denotes the package.
741 -- Set Body_Id. Note that this will be reset to point to the generic
742 -- copy later on in the generic case.
744 Body_Id
:= Defining_Entity
(N
);
746 -- Body is body of package instantiation. Corresponding spec has already
749 if Present
(Corresponding_Spec
(N
)) then
750 Spec_Id
:= Corresponding_Spec
(N
);
751 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
754 Spec_Id
:= Current_Entity_In_Scope
(Defining_Entity
(N
));
757 and then Is_Package_Or_Generic_Package
(Spec_Id
)
759 Pack_Decl
:= Unit_Declaration_Node
(Spec_Id
);
761 if Nkind
(Pack_Decl
) = N_Package_Renaming_Declaration
then
762 Error_Msg_N
("cannot supply body for package renaming", N
);
765 elsif Present
(Corresponding_Body
(Pack_Decl
)) then
766 Error_Msg_N
("redefinition of package body", N
);
771 Error_Msg_N
("missing specification for package body", N
);
775 if Is_Package_Or_Generic_Package
(Spec_Id
)
776 and then (Scope
(Spec_Id
) = Standard_Standard
777 or else Is_Child_Unit
(Spec_Id
))
778 and then not Unit_Requires_Body
(Spec_Id
)
780 if Ada_Version
= Ada_83
then
782 ("optional package body (not allowed in Ada 95)??", N
);
784 Error_Msg_N
("spec of this package does not allow a body", N
);
785 Error_Msg_N
("\either remove the body or add pragma "
786 & "Elaborate_Body in the spec", N
);
791 -- A [generic] package body freezes the contract of the nearest
792 -- enclosing package body and all other contracts encountered in
793 -- the same declarative part up to and excluding the package body:
795 -- package body Nearest_Enclosing_Package
796 -- with Refined_State => (State => Constit)
800 -- package body Freezes_Enclosing_Package_Body
801 -- with Refined_State => (State_2 => Constit_2)
806 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
808 -- This ensures that any annotations referenced by the contract of a
809 -- [generic] subprogram body declared within the current package body
810 -- are available. This form of freezing is decoupled from the usual
811 -- Freeze_xxx mechanism because it must also work in the context of
812 -- generics where normal freezing is disabled.
814 -- Only bodies coming from source should cause this type of freezing.
815 -- Instantiated generic bodies are excluded because their processing is
816 -- performed in a separate compilation pass which lacks enough semantic
817 -- information with respect to contract analysis. It is safe to suppress
818 -- the freezing of contracts in this case because this action already
819 -- took place at the end of the enclosing declarative part.
821 if Comes_From_Source
(N
)
822 and then not Is_Generic_Instance
(Spec_Id
)
824 Freeze_Previous_Contracts
(N
);
827 -- A package body is Ghost when the corresponding spec is Ghost. Set
828 -- the mode now to ensure that any nodes generated during analysis and
829 -- expansion are properly flagged as ignored Ghost.
831 Mark_And_Set_Ghost_Body
(N
, Spec_Id
);
833 -- Deactivate expansion inside the body of ignored Ghost entities,
834 -- as this code will ultimately be ignored. This avoids requiring the
835 -- presence of run-time units which are not needed. Only do this for
836 -- user entities, as internally generated entities might still need
837 -- to be expanded (e.g. those generated for types).
839 if Present
(Ignored_Ghost_Region
)
840 and then Comes_From_Source
(Body_Id
)
842 Expander_Active
:= False;
845 -- If the body completes the initial declaration of a compilation unit
846 -- which is subject to pragma Elaboration_Checks, set the model of the
847 -- pragma because it applies to all parts of the unit.
849 Install_Elaboration_Model
(Spec_Id
);
851 Set_Is_Compilation_Unit
(Body_Id
, Is_Compilation_Unit
(Spec_Id
));
852 Style
.Check_Identifier
(Body_Id
, Spec_Id
);
854 if Is_Child_Unit
(Spec_Id
) then
855 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
857 ("body of child unit& cannot be an inner package", N
, Spec_Id
);
860 Set_Is_Child_Unit
(Body_Id
);
863 -- Generic package case
865 if Ekind
(Spec_Id
) = E_Generic_Package
then
867 -- Disable expansion and perform semantic analysis on copy. The
868 -- unannotated body will be used in all instantiations.
870 Body_Id
:= Defining_Entity
(N
);
871 Mutate_Ekind
(Body_Id
, E_Package_Body
);
872 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
873 Set_Is_Obsolescent
(Body_Id
, Is_Obsolescent
(Spec_Id
));
874 Set_Body_Entity
(Spec_Id
, Body_Id
);
875 Set_Spec_Entity
(Body_Id
, Spec_Id
);
877 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
880 -- Collect all contract-related source pragmas found within the
881 -- template and attach them to the contract of the package body.
882 -- This contract is used in the capture of global references within
885 Create_Generic_Contract
(N
);
887 -- Update Body_Id to point to the copied node for the remainder of
890 Body_Id
:= Defining_Entity
(N
);
894 -- The Body_Id is that of the copied node in the generic case, the
895 -- current node otherwise. Note that N was rewritten above, so we must
896 -- be sure to get the latest Body_Id value.
898 if Ekind
(Body_Id
) = E_Package
then
899 Reinit_Field_To_Zero
(Body_Id
, F_Body_Needed_For_Inlining
);
901 Mutate_Ekind
(Body_Id
, E_Package_Body
);
902 Set_Body_Entity
(Spec_Id
, Body_Id
);
903 Set_Spec_Entity
(Body_Id
, Spec_Id
);
905 -- Defining name for the package body is not a visible entity: Only the
906 -- defining name for the declaration is visible.
908 Set_Etype
(Body_Id
, Standard_Void_Type
);
909 Set_Scope
(Body_Id
, Scope
(Spec_Id
));
910 Set_Corresponding_Spec
(N
, Spec_Id
);
911 Set_Corresponding_Body
(Pack_Decl
, Body_Id
);
913 -- The body entity is not used for semantics or code generation, but
914 -- it is attached to the entity list of the enclosing scope to simplify
915 -- the listing of back-annotations for the types it main contain.
917 if Scope
(Spec_Id
) /= Standard_Standard
then
918 Append_Entity
(Body_Id
, Scope
(Spec_Id
));
921 -- Indicate that we are currently compiling the body of the package
923 Set_In_Package_Body
(Spec_Id
);
924 Set_Has_Completion
(Spec_Id
);
925 Last_Spec_Entity
:= Last_Entity
(Spec_Id
);
927 Analyze_Aspect_Specifications
(N
, Body_Id
);
929 Push_Scope
(Spec_Id
);
931 -- Set SPARK_Mode only for non-generic package
933 if Ekind
(Spec_Id
) = E_Package
then
934 Set_SPARK_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
935 Set_SPARK_Aux_Pragma
(Body_Id
, SPARK_Mode_Pragma
);
936 Set_SPARK_Pragma_Inherited
(Body_Id
);
937 Set_SPARK_Aux_Pragma_Inherited
(Body_Id
);
939 -- A package body may be instantiated or inlined at a later pass.
940 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
941 -- it applied to the package spec.
943 if Ignore_SPARK_Mode_Pragmas
(Spec_Id
) then
944 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
948 Set_Categorization_From_Pragmas
(N
);
950 Install_Visible_Declarations
(Spec_Id
);
951 Install_Private_Declarations
(Spec_Id
);
952 Install_Private_With_Clauses
(Spec_Id
);
953 Install_Composite_Operations
(Spec_Id
);
955 Check_Anonymous_Access_Types
(Spec_Id
, N
);
957 if Ekind
(Spec_Id
) = E_Generic_Package
then
958 Set_Use
(Generic_Formal_Declarations
(Pack_Decl
));
961 Set_Use
(Visible_Declarations
(Specification
(Pack_Decl
)));
962 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
964 -- This is a nested package, so it may be necessary to declare certain
965 -- inherited subprograms that are not yet visible because the parent
966 -- type's subprograms are now visible.
967 -- Note that for child units these operations were generated when
968 -- analyzing the package specification.
970 if Ekind
(Scope
(Spec_Id
)) = E_Package
971 and then Scope
(Spec_Id
) /= Standard_Standard
972 and then not Is_Child_Unit
(Spec_Id
)
974 Declare_Inherited_Private_Subprograms
(Spec_Id
);
977 if Present
(Declarations
(N
)) then
978 Analyze_Declarations
(Declarations
(N
));
979 Inspect_Deferred_Constant_Completion
(Declarations
(N
));
982 -- Verify that the SPARK_Mode of the body agrees with that of its spec
984 if Present
(SPARK_Pragma
(Body_Id
)) then
985 if Present
(SPARK_Aux_Pragma
(Spec_Id
)) then
986 if Get_SPARK_Mode_From_Annotation
(SPARK_Aux_Pragma
(Spec_Id
)) =
989 Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = On
991 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
992 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
993 Error_Msg_Sloc
:= Sloc
(SPARK_Aux_Pragma
(Spec_Id
));
995 ("\value Off was set for SPARK_Mode on & #", N
, Spec_Id
);
998 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
999 -- as specified in source code, or because SPARK_Mode On is ignored
1000 -- in an instance where the context is SPARK_Mode Off/Auto.
1002 elsif Get_SPARK_Mode_From_Annotation
(SPARK_Pragma
(Body_Id
)) = Off
1003 and then (Is_Generic_Unit
(Spec_Id
) or else In_Instance
)
1008 Error_Msg_Sloc
:= Sloc
(SPARK_Pragma
(Body_Id
));
1009 Error_Msg_N
("incorrect application of SPARK_Mode#", N
);
1010 Error_Msg_Sloc
:= Sloc
(Spec_Id
);
1012 ("\no value was set for SPARK_Mode on & #", N
, Spec_Id
);
1016 -- Analyze_Declarations has caused freezing of all types. Now generate
1017 -- bodies for RACW primitives and stream attributes, if any.
1019 if Ekind
(Spec_Id
) = E_Package
and then Has_RACW
(Spec_Id
) then
1021 -- Attach subprogram bodies to support RACWs declared in spec
1023 Append_RACW_Bodies
(Declarations
(N
), Spec_Id
);
1024 Analyze_List
(Declarations
(N
));
1027 HSS
:= Handled_Statement_Sequence
(N
);
1029 if Present
(HSS
) then
1030 Process_End_Label
(HSS
, 't', Spec_Id
);
1033 -- Check that elaboration code in a preelaborable package body is
1034 -- empty other than null statements and labels (RM 10.2.1(6)).
1036 Validate_Null_Statement_Sequence
(N
);
1039 Validate_Categorization_Dependency
(N
, Spec_Id
);
1040 Check_Completion
(Body_Id
);
1042 -- Generate start of body reference. Note that we do this fairly late,
1043 -- because the call will use In_Extended_Main_Source_Unit as a check,
1044 -- and we want to make sure that Corresponding_Stub links are set
1046 Generate_Reference
(Spec_Id
, Body_Id
, 'b', Set_Ref
=> False);
1048 -- For a generic package, collect global references and mark them on
1049 -- the original body so that they are not resolved again at the point
1050 -- of instantiation.
1052 if Ekind
(Spec_Id
) /= E_Package
then
1053 Save_Global_References
(Original_Node
(N
));
1057 -- The entities of the package body have so far been chained onto the
1058 -- declaration chain for the spec. That's been fine while we were in the
1059 -- body, since we wanted them to be visible, but now that we are leaving
1060 -- the package body, they are no longer visible, so we remove them from
1061 -- the entity chain of the package spec entity, and copy them to the
1062 -- entity chain of the package body entity, where they will never again
1065 if Present
(Last_Spec_Entity
) then
1066 Set_First_Entity
(Body_Id
, Next_Entity
(Last_Spec_Entity
));
1067 Set_Next_Entity
(Last_Spec_Entity
, Empty
);
1068 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
1069 Set_Last_Entity
(Spec_Id
, Last_Spec_Entity
);
1072 Set_First_Entity
(Body_Id
, First_Entity
(Spec_Id
));
1073 Set_Last_Entity
(Body_Id
, Last_Entity
(Spec_Id
));
1074 Set_First_Entity
(Spec_Id
, Empty
);
1075 Set_Last_Entity
(Spec_Id
, Empty
);
1078 Update_Use_Clause_Chain
;
1079 End_Package_Scope
(Spec_Id
);
1081 -- All entities declared in body are not visible
1087 E
:= First_Entity
(Body_Id
);
1088 while Present
(E
) loop
1089 Set_Is_Immediately_Visible
(E
, False);
1090 Set_Is_Potentially_Use_Visible
(E
, False);
1093 -- Child units may appear on the entity list (e.g. if they appear
1094 -- in the context of a subunit) but they are not body entities.
1096 if not Is_Child_Unit
(E
) then
1097 Set_Is_Package_Body_Entity
(E
);
1104 Check_References
(Body_Id
);
1106 -- For a generic unit, check that the formal parameters are referenced,
1107 -- and that local variables are used, as for regular packages.
1109 if Ekind
(Spec_Id
) = E_Generic_Package
then
1110 Check_References
(Spec_Id
);
1113 -- At this point all entities of the package body are externally visible
1114 -- to the linker as their Is_Public flag is set to True. This proactive
1115 -- approach is necessary because an inlined or a generic body for which
1116 -- code is generated in other units may need to see these entities. Cut
1117 -- down the number of global symbols that do not need public visibility
1118 -- as this has two beneficial effects:
1119 -- (1) It makes the compilation process more efficient.
1120 -- (2) It gives the code generator more leeway to optimize within each
1121 -- unit, especially subprograms.
1123 -- This is done only for top-level library packages or child units as
1124 -- the algorithm does a top-down traversal of the package body. This is
1125 -- also done for instances because instantiations are still pending by
1126 -- the time the enclosing package body is analyzed.
1128 if (Scope
(Spec_Id
) = Standard_Standard
1129 or else Is_Child_Unit
(Spec_Id
)
1130 or else Is_Generic_Instance
(Spec_Id
))
1131 and then not Is_Generic_Unit
(Spec_Id
)
1133 Hide_Public_Entities
(Declarations
(N
));
1136 -- If expander is not active, then here is where we turn off the
1137 -- In_Package_Body flag, otherwise it is turned off at the end of the
1138 -- corresponding expansion routine. If this is an instance body, we need
1139 -- to qualify names of local entities, because the body may have been
1140 -- compiled as a preliminary to another instantiation.
1142 if not Expander_Active
then
1143 Set_In_Package_Body
(Spec_Id
, False);
1145 if Is_Generic_Instance
(Spec_Id
)
1146 and then Operating_Mode
= Generate_Code
1148 Qualify_Entity_Names
(N
);
1152 if Present
(Ignored_Ghost_Region
) then
1153 Expander_Active
:= Saved_EA
;
1156 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
1157 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
1158 end Analyze_Package_Body_Helper
;
1160 ---------------------------------
1161 -- Analyze_Package_Declaration --
1162 ---------------------------------
1164 procedure Analyze_Package_Declaration
(N
: Node_Id
) is
1165 Id
: constant Node_Id
:= Defining_Entity
(N
);
1167 Is_Comp_Unit
: constant Boolean :=
1168 Nkind
(Parent
(N
)) = N_Compilation_Unit
;
1170 Body_Required
: Boolean;
1171 -- True when this package declaration requires a corresponding body
1174 if Debug_Flag_C
then
1175 Write_Str
("==> package spec ");
1176 Write_Name
(Chars
(Id
));
1177 Write_Str
(" from ");
1178 Write_Location
(Sloc
(N
));
1183 Generate_Definition
(Id
);
1185 Mutate_Ekind
(Id
, E_Package
);
1186 Set_Is_Not_Self_Hidden
(Id
);
1187 -- Needed early because of Set_Categorization_From_Pragmas below
1188 Set_Etype
(Id
, Standard_Void_Type
);
1190 -- Set SPARK_Mode from context
1192 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
1193 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
1194 Set_SPARK_Pragma_Inherited
(Id
);
1195 Set_SPARK_Aux_Pragma_Inherited
(Id
);
1197 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1198 -- the body of this package is instantiated or inlined later and out of
1199 -- context. The body uses this attribute to restore the value of the
1202 if Ignore_SPARK_Mode_Pragmas_In_Instance
then
1203 Set_Ignore_SPARK_Mode_Pragmas
(Id
);
1206 -- Analyze aspect specifications immediately, since we need to recognize
1207 -- things like Pure early enough to diagnose violations during analysis.
1209 Analyze_Aspect_Specifications
(N
, Id
);
1211 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1212 -- a limited-with clause of its own context. In this case the error has
1213 -- been previously notified by Analyze_Context.
1215 -- limited with Pkg; -- ERROR
1216 -- package Pkg is ...
1218 if From_Limited_With
(Id
) then
1224 Set_Is_Pure
(Id
, Is_Pure
(Enclosing_Lib_Unit_Entity
));
1225 Set_Categorization_From_Pragmas
(N
);
1227 Analyze
(Specification
(N
));
1228 Validate_Categorization_Dependency
(N
, Id
);
1230 -- Determine whether the package requires a body. Abstract states are
1231 -- intentionally ignored because they do require refinement which can
1232 -- only come in a body, but at the same time they do not force the need
1233 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1235 Body_Required
:= Unit_Requires_Body
(Id
);
1237 if not Body_Required
then
1239 -- If the package spec does not require an explicit body, then there
1240 -- are not entities requiring completion in the language sense. Call
1241 -- Check_Completion now to ensure that nested package declarations
1242 -- that require an implicit body get one. (In the case where a body
1243 -- is required, Check_Completion is called at the end of the body's
1244 -- declarative part.)
1248 -- If the package spec does not require an explicit body, then all
1249 -- abstract states declared in nested packages cannot possibly get a
1250 -- proper refinement (SPARK RM 7.1.4(4) and SPARK RM 7.2.2(3)). This
1251 -- check is performed only when the compilation unit is the main
1252 -- unit to allow for modular SPARK analysis where packages do not
1253 -- necessarily have bodies.
1255 if Is_Comp_Unit
then
1256 Check_State_Refinements
1258 Is_Main_Unit
=> Parent
(N
) = Cunit
(Main_Unit
));
1261 -- For package declarations at the library level, warn about
1262 -- references to unset objects, which is straightforward for packages
1263 -- with no bodies. For packages with bodies this is more complicated,
1264 -- because some of the objects might be set between spec and body
1265 -- elaboration, in nested or child packages, etc. Note that the
1266 -- recursive calls in Check_References will handle nested package
1269 if Is_Library_Level_Entity
(Id
) then
1270 Check_References
(Id
);
1274 -- Set Body_Required indication on the compilation unit node
1276 if Is_Comp_Unit
then
1277 Set_Body_Required
(Parent
(N
), Body_Required
);
1279 if Legacy_Elaboration_Checks
and not Body_Required
then
1280 Set_Suppress_Elaboration_Warnings
(Id
);
1284 End_Package_Scope
(Id
);
1286 -- For the declaration of a library unit that is a remote types package,
1287 -- check legality rules regarding availability of stream attributes for
1288 -- types that contain non-remote access values. This subprogram performs
1289 -- visibility tests that rely on the fact that we have exited the scope
1292 if Is_Comp_Unit
then
1293 Validate_RT_RAT_Component
(N
);
1296 if Debug_Flag_C
then
1298 Write_Str
("<== package spec ");
1299 Write_Name
(Chars
(Id
));
1300 Write_Str
(" from ");
1301 Write_Location
(Sloc
(N
));
1304 end Analyze_Package_Declaration
;
1306 -----------------------------------
1307 -- Analyze_Package_Specification --
1308 -----------------------------------
1310 -- Note that this code is shared for the analysis of generic package specs
1311 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1313 procedure Analyze_Package_Specification
(N
: Node_Id
) is
1314 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1315 Orig_Decl
: constant Node_Id
:= Original_Node
(Parent
(N
));
1316 Vis_Decls
: constant List_Id
:= Visible_Declarations
(N
);
1317 Priv_Decls
: constant List_Id
:= Private_Declarations
(N
);
1320 Public_Child
: Boolean;
1322 Private_With_Clauses_Installed
: Boolean := False;
1323 -- In Ada 2005, private with_clauses are visible in the private part
1324 -- of a nested package, even if it appears in the public part of the
1325 -- enclosing package. This requires a separate step to install these
1326 -- private_with_clauses, and remove them at the end of the nested
1329 procedure Clear_Constants
(Id
: Entity_Id
);
1330 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
1331 -- and Is_True_Constant) on all variables that are entities of Id.
1332 -- A recursive call is made for all packages and generic packages.
1334 procedure Generate_Parent_References
;
1335 -- For a child unit, generate references to parent units, for
1336 -- GNAT Studio navigation purposes.
1338 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean;
1339 -- Child and Unit are entities of compilation units. True if Child
1340 -- is a public child of Parent as defined in 10.1.1
1342 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
);
1343 -- Reject completion of an incomplete or private type declarations
1344 -- having a known discriminant part by an unchecked union.
1346 procedure Inspect_Untagged_Record_Completion
(Decls
: List_Id
);
1347 -- Find out whether a nonlimited untagged record completion has got a
1348 -- primitive equality operator and, if so, make it so that it will be
1349 -- used as the predefined operator of the private view of the record.
1351 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
);
1352 -- Given the package entity of a generic package instantiation or
1353 -- formal package whose corresponding generic is a child unit, installs
1354 -- the private declarations of each of the child unit's parents.
1355 -- This has to be done at the point of entering the instance package's
1356 -- private part rather than being done in Sem_Ch12.Install_Parent
1357 -- (which is where the parents' visible declarations are installed).
1359 ---------------------
1360 -- Clear_Constants --
1361 ---------------------
1363 procedure Clear_Constants
(Id
: Entity_Id
) is
1367 -- Ignore package renamings, not interesting and they can cause self
1368 -- referential loops in the code below.
1370 if Nkind
(Parent
(Id
)) = N_Package_Renaming_Declaration
then
1374 -- Note: in the loop below, the check for Next_Entity pointing back
1375 -- to the package entity may seem odd, but it is needed, because a
1376 -- package can contain a renaming declaration to itself, and such
1377 -- renamings are generated automatically within package instances.
1379 E
:= First_Entity
(Id
);
1380 while Present
(E
) and then E
/= Id
loop
1381 if Ekind
(E
) = E_Variable
then
1382 Set_Never_Set_In_Source
(E
, False);
1383 Set_Is_True_Constant
(E
, False);
1384 Set_Current_Value
(E
, Empty
);
1385 Set_Is_Known_Null
(E
, False);
1386 Set_Last_Assignment
(E
, Empty
);
1388 if not Can_Never_Be_Null
(E
) then
1389 Set_Is_Known_Non_Null
(E
, False);
1392 elsif Is_Package_Or_Generic_Package
(E
) then
1393 Clear_Constants
(E
);
1398 end Clear_Constants
;
1400 --------------------------------
1401 -- Generate_Parent_References --
1402 --------------------------------
1404 procedure Generate_Parent_References
is
1405 Decl
: constant Node_Id
:= Parent
(N
);
1408 if Id
= Cunit_Entity
(Main_Unit
)
1409 or else Parent
(Decl
) = Library_Unit
(Cunit
(Main_Unit
))
1411 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1413 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) not in
1414 N_Subprogram_Body | N_Subunit
1416 -- If current unit is an ancestor of main unit, generate a
1417 -- reference to its own parent.
1421 Main_Spec
: Node_Id
:= Unit
(Cunit
(Main_Unit
));
1424 if Nkind
(Main_Spec
) = N_Package_Body
then
1425 Main_Spec
:= Unit
(Library_Unit
(Cunit
(Main_Unit
)));
1428 U
:= Parent_Spec
(Main_Spec
);
1429 while Present
(U
) loop
1430 if U
= Parent
(Decl
) then
1431 Generate_Reference
(Id
, Scope
(Id
), 'k', False);
1434 elsif Nkind
(Unit
(U
)) = N_Package_Body
then
1438 U
:= Parent_Spec
(Unit
(U
));
1443 end Generate_Parent_References
;
1445 ---------------------
1446 -- Is_Public_Child --
1447 ---------------------
1449 function Is_Public_Child
(Child
, Unit
: Entity_Id
) return Boolean is
1451 if not Is_Private_Descendant
(Child
) then
1454 if Child
= Unit
then
1455 return not Private_Present
(
1456 Parent
(Unit_Declaration_Node
(Child
)));
1458 return Is_Public_Child
(Scope
(Child
), Unit
);
1461 end Is_Public_Child
;
1463 ----------------------------------------
1464 -- Inspect_Unchecked_Union_Completion --
1465 ----------------------------------------
1467 procedure Inspect_Unchecked_Union_Completion
(Decls
: List_Id
) is
1471 Decl
:= First
(Decls
);
1472 while Present
(Decl
) loop
1474 -- We are looking for an incomplete or private type declaration
1475 -- with a known_discriminant_part whose full view is an
1476 -- Unchecked_Union. The seemingly useless check with Is_Type
1477 -- prevents cascaded errors when routines defined only for type
1478 -- entities are called with non-type entities.
1480 if Nkind
(Decl
) in N_Incomplete_Type_Declaration
1481 | N_Private_Type_Declaration
1482 and then Is_Type
(Defining_Identifier
(Decl
))
1483 and then Has_Discriminants
(Defining_Identifier
(Decl
))
1484 and then Present
(Full_View
(Defining_Identifier
(Decl
)))
1486 Is_Unchecked_Union
(Full_View
(Defining_Identifier
(Decl
)))
1489 ("completion of discriminated partial view "
1490 & "cannot be an unchecked union",
1491 Full_View
(Defining_Identifier
(Decl
)));
1496 end Inspect_Unchecked_Union_Completion
;
1498 ----------------------------------------
1499 -- Inspect_Untagged_Record_Completion --
1500 ----------------------------------------
1502 procedure Inspect_Untagged_Record_Completion
(Decls
: List_Id
) is
1506 Decl
:= First
(Decls
);
1507 while Present
(Decl
) loop
1509 -- We are looking for a full type declaration of an untagged
1510 -- record with a private declaration and primitive operations.
1512 if Nkind
(Decl
) in N_Full_Type_Declaration
1513 and then Is_Record_Type
(Defining_Identifier
(Decl
))
1514 and then not Is_Limited_Type
(Defining_Identifier
(Decl
))
1515 and then not Is_Tagged_Type
(Defining_Identifier
(Decl
))
1516 and then Has_Private_Declaration
(Defining_Identifier
(Decl
))
1517 and then Has_Primitive_Operations
(Defining_Identifier
(Decl
))
1520 Prim_List
: constant Elist_Id
:=
1521 Collect_Primitive_Operations
(Defining_Identifier
(Decl
));
1530 Prim
:= First_Elmt
(Prim_List
);
1531 while Present
(Prim
) loop
1532 Op_Id
:= Node
(Prim
);
1533 Op_Decl
:= Declaration_Node
(Op_Id
);
1534 if Nkind
(Op_Decl
) in N_Subprogram_Specification
then
1535 Op_Decl
:= Parent
(Op_Decl
);
1538 -- We are looking for an equality operator immediately
1539 -- visible and declared in the private part followed by
1540 -- the synthesized inequality operator.
1542 if Is_User_Defined_Equality
(Op_Id
)
1543 and then Is_Immediately_Visible
(Op_Id
)
1544 and then List_Containing
(Op_Decl
) = Decls
1546 Ne_Id
:= Next_Entity
(Op_Id
);
1547 pragma Assert
(Ekind
(Ne_Id
) = E_Function
1548 and then Corresponding_Equality
(Ne_Id
) = Op_Id
);
1550 E
:= First_Private_Entity
(Id
);
1552 -- Move them from the private part of the entity list
1553 -- up to the end of the visible part of the same list.
1555 Remove_Entity
(Op_Id
);
1556 Remove_Entity
(Ne_Id
);
1558 Link_Entities
(Prev_Entity
(E
), Op_Id
);
1559 Link_Entities
(Op_Id
, Ne_Id
);
1560 Link_Entities
(Ne_Id
, E
);
1562 -- And if the private part contains another equality
1563 -- operator, move the equality operator to after it
1564 -- in the homonym chain, so that all its next homonyms
1565 -- in the same scope, if any, also are in the visible
1566 -- part. This is relied upon to resolve expanded names
1567 -- in Collect_Interps for example.
1569 while Present
(E
) loop
1570 exit when Ekind
(E
) = E_Function
1571 and then Chars
(E
) = Name_Op_Eq
;
1577 Remove_Homonym
(Op_Id
);
1579 Set_Homonym
(Op_Id
, Homonym
(E
));
1580 Set_Homonym
(E
, Op_Id
);
1593 end Inspect_Untagged_Record_Completion
;
1595 -----------------------------------------
1596 -- Install_Parent_Private_Declarations --
1597 -----------------------------------------
1599 procedure Install_Parent_Private_Declarations
(Inst_Id
: Entity_Id
) is
1600 Inst_Par
: Entity_Id
;
1601 Gen_Par
: Entity_Id
;
1602 Inst_Node
: Node_Id
;
1605 Inst_Par
:= Inst_Id
;
1608 Generic_Parent
(Specification
(Unit_Declaration_Node
(Inst_Par
)));
1609 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
1610 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
1612 if Nkind
(Inst_Node
) in
1613 N_Package_Instantiation | N_Formal_Package_Declaration
1614 and then Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
1616 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
1618 if Present
(Renamed_Entity
(Inst_Par
)) then
1619 Inst_Par
:= Renamed_Entity
(Inst_Par
);
1622 -- The instance may appear in a sibling generic unit, in
1623 -- which case the prefix must include the common (generic)
1624 -- ancestor, which is treated as a current instance.
1627 and then Ekind
(Inst_Par
) = E_Generic_Package
1629 Gen_Par
:= Inst_Par
;
1630 pragma Assert
(In_Open_Scopes
(Gen_Par
));
1635 (Specification
(Unit_Declaration_Node
(Inst_Par
)));
1638 -- Install the private declarations and private use clauses
1639 -- of a parent instance of the child instance, unless the
1640 -- parent instance private declarations have already been
1641 -- installed earlier in Analyze_Package_Specification, which
1642 -- happens when a generic child is instantiated, and the
1643 -- instance is a child of the parent instance.
1645 -- Installing the use clauses of the parent instance twice
1646 -- is both unnecessary and wrong, because it would cause the
1647 -- clauses to be chained to themselves in the use clauses
1648 -- list of the scope stack entry. That in turn would cause
1649 -- an endless loop from End_Use_Clauses upon scope exit.
1651 -- The parent is now fully visible. It may be a hidden open
1652 -- scope if we are currently compiling some child instance
1653 -- declared within it, but while the current instance is being
1654 -- compiled the parent is immediately visible. In particular
1655 -- its entities must remain visible if a stack save/restore
1656 -- takes place through a call to Rtsfind.
1658 if Present
(Gen_Par
) then
1659 if not In_Private_Part
(Inst_Par
) then
1660 Install_Private_Declarations
(Inst_Par
);
1661 Set_Use
(Private_Declarations
1663 (Unit_Declaration_Node
(Inst_Par
))));
1664 Set_Is_Hidden_Open_Scope
(Inst_Par
, False);
1667 -- If we've reached the end of the generic instance parents,
1668 -- then finish off by looping through the nongeneric parents
1669 -- and installing their private declarations.
1671 -- If one of the non-generic parents is itself on the scope
1672 -- stack, do not install its private declarations: they are
1673 -- installed in due time when the private part of that parent
1677 while Present
(Inst_Par
)
1678 and then Inst_Par
/= Standard_Standard
1679 and then (not In_Open_Scopes
(Inst_Par
)
1680 or else not In_Private_Part
(Inst_Par
))
1682 if Nkind
(Inst_Node
) = N_Formal_Package_Declaration
1684 not Is_Ancestor_Package
1685 (Inst_Par
, Cunit_Entity
(Current_Sem_Unit
))
1687 Install_Private_Declarations
(Inst_Par
);
1689 (Private_Declarations
1691 (Unit_Declaration_Node
(Inst_Par
))));
1692 Inst_Par
:= Scope
(Inst_Par
);
1705 end Install_Parent_Private_Declarations
;
1707 -- Start of processing for Analyze_Package_Specification
1710 if Present
(Vis_Decls
) then
1711 Analyze_Declarations
(Vis_Decls
);
1714 -- Inspect the entities defined in the package and ensure that all
1715 -- incomplete types have received full declarations. Build default
1716 -- initial condition and invariant procedures for all qualifying types.
1718 E
:= First_Entity
(Id
);
1719 while Present
(E
) loop
1721 -- Check on incomplete types
1723 -- AI05-0213: A formal incomplete type has no completion, and neither
1724 -- does the corresponding subtype in an instance.
1726 if Is_Incomplete_Type
(E
)
1727 and then No
(Full_View
(E
))
1728 and then not Is_Generic_Type
(E
)
1729 and then not From_Limited_With
(E
)
1730 and then not Is_Generic_Actual_Type
(E
)
1732 Error_Msg_N
("no declaration in visible part for incomplete}", E
);
1738 if Is_Remote_Call_Interface
(Id
)
1739 and then Nkind
(Parent
(Parent
(N
))) = N_Compilation_Unit
1741 Validate_RCI_Declarations
(Id
);
1744 -- Save global references in the visible declarations, before installing
1745 -- private declarations of parent unit if there is one, because the
1746 -- privacy status of types defined in the parent will change. This is
1747 -- only relevant for generic child units, but is done in all cases for
1750 if Ekind
(Id
) = E_Generic_Package
1751 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1754 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1755 Save_Priv
: constant List_Id
:= Private_Declarations
(Orig_Spec
);
1758 -- Insert the freezing nodes after the visible declarations to
1759 -- ensure that we analyze its aspects; needed to ensure that
1760 -- global entities referenced in the aspects are properly handled.
1762 if Ada_Version
>= Ada_2012
1763 and then Is_Non_Empty_List
(Vis_Decls
)
1764 and then Is_Empty_List
(Priv_Decls
)
1766 Insert_List_After_And_Analyze
1767 (Last
(Vis_Decls
), Freeze_Entity
(Id
, Last
(Vis_Decls
)));
1770 Set_Private_Declarations
(Orig_Spec
, Empty_List
);
1771 Save_Global_References
(Orig_Decl
);
1772 Set_Private_Declarations
(Orig_Spec
, Save_Priv
);
1776 -- If package is a public child unit, then make the private declarations
1777 -- of the parent visible.
1779 Public_Child
:= False;
1783 Pack_Decl
: Node_Id
;
1788 Par_Spec
:= Parent_Spec
(Parent
(N
));
1790 -- If the package is formal package of an enclosing generic, it is
1791 -- transformed into a local generic declaration, and compiled to make
1792 -- its spec available. We need to retrieve the original generic to
1793 -- determine whether it is a child unit, and install its parents.
1797 Nkind
(Original_Node
(Parent
(N
))) = N_Formal_Package_Declaration
1799 Par
:= Entity
(Name
(Original_Node
(Parent
(N
))));
1800 Par_Spec
:= Parent_Spec
(Unit_Declaration_Node
(Par
));
1803 if Present
(Par_Spec
) then
1804 Generate_Parent_References
;
1806 while Scope
(Par
) /= Standard_Standard
1807 and then Is_Public_Child
(Id
, Par
)
1808 and then In_Open_Scopes
(Par
)
1810 Public_Child
:= True;
1812 Install_Private_Declarations
(Par
);
1813 Install_Private_With_Clauses
(Par
);
1814 Pack_Decl
:= Unit_Declaration_Node
(Par
);
1815 Set_Use
(Private_Declarations
(Specification
(Pack_Decl
)));
1820 if Is_Compilation_Unit
(Id
) then
1821 Install_Private_With_Clauses
(Id
);
1823 -- The current compilation unit may include private with_clauses,
1824 -- which are visible in the private part of the current nested
1825 -- package, and have to be installed now. This is not done for
1826 -- nested instantiations, where the private with_clauses of the
1827 -- enclosing unit have no effect once the instantiation info is
1828 -- established and we start analyzing the package declaration.
1831 Comp_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
1833 if Is_Package_Or_Generic_Package
(Comp_Unit
)
1834 and then not In_Private_Part
(Comp_Unit
)
1835 and then not In_Instance
1837 Install_Private_With_Clauses
(Comp_Unit
);
1838 Private_With_Clauses_Installed
:= True;
1843 -- If this is a package associated with a generic instance or formal
1844 -- package, then the private declarations of each of the generic's
1845 -- parents must be installed at this point, but not if this is the
1846 -- abbreviated instance created to check a formal package, see the
1847 -- same condition in Analyze_Package_Instantiation.
1849 if Is_Generic_Instance
(Id
)
1850 and then not Is_Abbreviated_Instance
(Id
)
1852 Install_Parent_Private_Declarations
(Id
);
1855 -- Analyze private part if present. The flag In_Private_Part is reset
1856 -- in Uninstall_Declarations.
1858 L
:= Last_Entity
(Id
);
1860 if Present
(Priv_Decls
) then
1861 Set_In_Private_Part
(Id
);
1863 -- Upon entering a public child's private part, it may be necessary
1864 -- to declare subprograms that were derived in the package's visible
1865 -- part but not yet made visible.
1867 if Public_Child
then
1868 Declare_Inherited_Private_Subprograms
(Id
);
1871 Analyze_Declarations
(Priv_Decls
);
1873 -- Check the private declarations for incomplete deferred constants
1875 Inspect_Deferred_Constant_Completion
(Priv_Decls
);
1877 -- The first private entity is the immediate follower of the last
1878 -- visible entity, if there was one.
1881 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1883 Set_First_Private_Entity
(Id
, First_Entity
(Id
));
1886 -- There may be inherited private subprograms that need to be declared,
1887 -- even in the absence of an explicit private part. If there are any
1888 -- public declarations in the package and the package is a public child
1889 -- unit, then an implicit private part is assumed.
1891 elsif Present
(L
) and then Public_Child
then
1892 Set_In_Private_Part
(Id
);
1893 Declare_Inherited_Private_Subprograms
(Id
);
1894 Set_First_Private_Entity
(Id
, Next_Entity
(L
));
1897 E
:= First_Entity
(Id
);
1898 while Present
(E
) loop
1900 -- Check rule of 3.6(11), which in general requires waiting till all
1901 -- full types have been seen.
1903 if Ekind
(E
) = E_Record_Type
or else Ekind
(E
) = E_Array_Type
then
1904 Check_Aliased_Component_Types
(E
);
1907 -- Check preelaborable initialization for full type completing a
1908 -- private type when aspect Preelaborable_Initialization is True
1909 -- or is specified by Preelaborable_Initialization attributes
1910 -- (in the case of a private type in a generic unit). We pass
1911 -- the expression of the aspect (when present) to the parameter
1912 -- Preelab_Init_Expr to take into account the rule that presumes
1913 -- that subcomponents of generic formal types mentioned in the
1914 -- type's P_I aspect have preelaborable initialization (see
1915 -- AI12-0409 and RM 10.2.1(11.8/5)).
1917 if Is_Type
(E
) and then Must_Have_Preelab_Init
(E
) then
1919 PI_Aspect
: constant Node_Id
:=
1921 (E
, Aspect_Preelaborable_Initialization
);
1922 PI_Expr
: Node_Id
:= Empty
;
1924 if Present
(PI_Aspect
) then
1925 PI_Expr
:= Expression
(PI_Aspect
);
1928 if not Has_Preelaborable_Initialization
1929 (E
, Preelab_Init_Expr
=> PI_Expr
)
1932 ("full view of & does not have "
1933 & "preelaborable initialization", E
);
1938 -- Preanalyze class-wide conditions of dispatching primitives defined
1939 -- in nested packages. For library packages, class-wide pre- and
1940 -- postconditions are preanalyzed when the primitives are frozen
1941 -- (see Merge_Class_Conditions); for nested packages, the end of the
1942 -- package does not cause freezing (and hence they must be analyzed
1943 -- now to ensure the correct visibility of referenced entities).
1945 if not Is_Compilation_Unit
(Id
)
1946 and then Is_Dispatching_Operation
(E
)
1947 and then Present
(Contract
(E
))
1949 Preanalyze_Class_Conditions
(E
);
1955 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1956 -- declaration having a known_discriminant_part shall not be an
1957 -- unchecked union type.
1959 if Present
(Vis_Decls
) then
1960 Inspect_Unchecked_Union_Completion
(Vis_Decls
);
1963 if Present
(Priv_Decls
) then
1964 Inspect_Unchecked_Union_Completion
(Priv_Decls
);
1967 -- Implement AI12-0101 (which only removes a legality rule) and then
1968 -- AI05-0123 (which directly applies in the previously illegal case)
1969 -- in Ada 2012. Note that AI12-0101 is a binding interpretation.
1971 if Present
(Priv_Decls
) and then Ada_Version
>= Ada_2012
then
1972 Inspect_Untagged_Record_Completion
(Priv_Decls
);
1975 if Ekind
(Id
) = E_Generic_Package
1976 and then Nkind
(Orig_Decl
) = N_Generic_Package_Declaration
1977 and then Present
(Priv_Decls
)
1979 -- Save global references in private declarations, ignoring the
1980 -- visible declarations that were processed earlier.
1983 Orig_Spec
: constant Node_Id
:= Specification
(Orig_Decl
);
1984 Save_Vis
: constant List_Id
:= Visible_Declarations
(Orig_Spec
);
1985 Save_Form
: constant List_Id
:=
1986 Generic_Formal_Declarations
(Orig_Decl
);
1989 -- Insert the freezing nodes after the private declarations to
1990 -- ensure that we analyze its aspects; needed to ensure that
1991 -- global entities referenced in the aspects are properly handled.
1993 if Ada_Version
>= Ada_2012
1994 and then Is_Non_Empty_List
(Priv_Decls
)
1996 Insert_List_After_And_Analyze
1997 (Last
(Priv_Decls
), Freeze_Entity
(Id
, Last
(Priv_Decls
)));
2000 Set_Visible_Declarations
(Orig_Spec
, Empty_List
);
2001 Set_Generic_Formal_Declarations
(Orig_Decl
, Empty_List
);
2002 Save_Global_References
(Orig_Decl
);
2003 Set_Generic_Formal_Declarations
(Orig_Decl
, Save_Form
);
2004 Set_Visible_Declarations
(Orig_Spec
, Save_Vis
);
2008 Process_End_Label
(N
, 'e', Id
);
2010 -- Remove private_with_clauses of enclosing compilation unit, if they
2013 if Private_With_Clauses_Installed
then
2014 Remove_Private_With_Clauses
(Cunit
(Current_Sem_Unit
));
2017 -- For the case of a library level package, we must go through all the
2018 -- entities clearing the indications that the value may be constant and
2019 -- not modified. Why? Because any client of this package may modify
2020 -- these values freely from anywhere. This also applies to any nested
2021 -- packages or generic packages.
2023 -- For now we unconditionally clear constants for packages that are
2024 -- instances of generic packages. The reason is that we do not have the
2025 -- body yet, and we otherwise think things are unreferenced when they
2026 -- are not. This should be fixed sometime (the effect is not terrible,
2027 -- we just lose some warnings, and also some cases of value propagation)
2030 if Is_Library_Level_Entity
(Id
)
2031 or else Is_Generic_Instance
(Id
)
2033 Clear_Constants
(Id
);
2036 -- Output relevant information as to why the package requires a body.
2037 -- Do not consider generated packages as this exposes internal symbols
2038 -- and leads to confusing messages.
2040 if List_Body_Required_Info
2041 and then In_Extended_Main_Source_Unit
(Id
)
2042 and then Unit_Requires_Body
(Id
)
2043 and then Comes_From_Source
(Id
)
2045 Unit_Requires_Body_Info
(Id
);
2048 -- Nested package specs that do not require bodies are not checked for
2049 -- ineffective use clauses due to the possibility of subunits. This is
2050 -- because at this stage it is impossible to tell whether there will be
2053 if not Unit_Requires_Body
(Id
)
2054 and then Is_Compilation_Unit
(Id
)
2055 and then not Is_Private_Descendant
(Id
)
2057 Update_Use_Clause_Chain
;
2059 end Analyze_Package_Specification
;
2061 --------------------------------------
2062 -- Analyze_Private_Type_Declaration --
2063 --------------------------------------
2065 procedure Analyze_Private_Type_Declaration
(N
: Node_Id
) is
2066 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2067 PF
: constant Boolean := Is_Pure
(Enclosing_Lib_Unit_Entity
);
2070 Generate_Definition
(Id
);
2071 Set_Is_Pure
(Id
, PF
);
2072 Reinit_Size_Align
(Id
);
2074 if not Is_Package_Or_Generic_Package
(Current_Scope
)
2075 or else In_Private_Part
(Current_Scope
)
2077 Error_Msg_N
("invalid context for private declaration", N
);
2080 New_Private_Type
(N
, Id
, N
);
2081 Set_Depends_On_Private
(Id
);
2083 -- Set the SPARK mode from the current context
2085 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
2086 Set_SPARK_Pragma_Inherited
(Id
);
2088 Analyze_Aspect_Specifications
(N
, Id
);
2089 end Analyze_Private_Type_Declaration
;
2091 ----------------------------------
2092 -- Check_Anonymous_Access_Types --
2093 ----------------------------------
2095 procedure Check_Anonymous_Access_Types
2096 (Spec_Id
: Entity_Id
;
2103 -- Itype references are only needed by gigi, to force elaboration of
2104 -- itypes. In the absence of code generation, they are not needed.
2106 if not Expander_Active
then
2110 E
:= First_Entity
(Spec_Id
);
2111 while Present
(E
) loop
2112 if Ekind
(E
) = E_Anonymous_Access_Type
2113 and then From_Limited_With
(E
)
2115 IR
:= Make_Itype_Reference
(Sloc
(P_Body
));
2118 if No
(Declarations
(P_Body
)) then
2119 Set_Declarations
(P_Body
, New_List
(IR
));
2121 Prepend
(IR
, Declarations
(P_Body
));
2127 end Check_Anonymous_Access_Types
;
2129 -------------------------------------------
2130 -- Declare_Inherited_Private_Subprograms --
2131 -------------------------------------------
2133 procedure Declare_Inherited_Private_Subprograms
(Id
: Entity_Id
) is
2135 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean;
2136 -- Check whether an inherited subprogram S is an operation of an
2137 -- untagged derived type T.
2139 ---------------------
2140 -- Is_Primitive_Of --
2141 ---------------------
2143 function Is_Primitive_Of
(T
: Entity_Id
; S
: Entity_Id
) return Boolean is
2147 -- If the full view is a scalar type, the type is the anonymous base
2148 -- type, but the operation mentions the first subtype, so check the
2149 -- signature against the base type.
2151 if Base_Type
(Etype
(S
)) = Base_Type
(T
) then
2155 Formal
:= First_Formal
(S
);
2156 while Present
(Formal
) loop
2157 if Base_Type
(Etype
(Formal
)) = Base_Type
(T
) then
2161 Next_Formal
(Formal
);
2166 end Is_Primitive_Of
;
2173 Op_Elmt_2
: Elmt_Id
;
2174 Prim_Op
: Entity_Id
;
2175 New_Op
: Entity_Id
:= Empty
;
2176 Parent_Subp
: Entity_Id
;
2179 -- Start of processing for Declare_Inherited_Private_Subprograms
2182 E
:= First_Entity
(Id
);
2183 while Present
(E
) loop
2185 -- If the entity is a nonprivate type extension whose parent type
2186 -- is declared in an open scope, then the type may have inherited
2187 -- operations that now need to be made visible. Ditto if the entity
2188 -- is a formal derived type in a child unit.
2190 if ((Is_Derived_Type
(E
) and then not Is_Private_Type
(E
))
2192 (Nkind
(Parent
(E
)) = N_Private_Extension_Declaration
2193 and then Is_Generic_Type
(E
)))
2194 and then In_Open_Scopes
(Scope
(Etype
(E
)))
2195 and then Is_Base_Type
(E
)
2197 if Is_Tagged_Type
(E
) then
2198 Op_List
:= Primitive_Operations
(E
);
2200 Tag
:= First_Tag_Component
(E
);
2202 Op_Elmt
:= First_Elmt
(Op_List
);
2203 while Present
(Op_Elmt
) loop
2204 Prim_Op
:= Node
(Op_Elmt
);
2206 -- Search primitives that are implicit operations with an
2207 -- internal name whose parent operation has a normal name.
2209 if Present
(Alias
(Prim_Op
))
2210 and then Find_Dispatching_Type
(Alias
(Prim_Op
)) /= E
2211 and then not Comes_From_Source
(Prim_Op
)
2212 and then Is_Internal_Name
(Chars
(Prim_Op
))
2213 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
2215 Parent_Subp
:= Alias
(Prim_Op
);
2217 -- Case 1: Check if the type has also an explicit
2218 -- overriding for this primitive.
2220 Op_Elmt_2
:= Next_Elmt
(Op_Elmt
);
2221 while Present
(Op_Elmt_2
) loop
2223 -- Skip entities with attribute Interface_Alias since
2224 -- they are not overriding primitives (these entities
2225 -- link an interface primitive with their covering
2228 if Chars
(Node
(Op_Elmt_2
)) = Chars
(Parent_Subp
)
2229 and then Type_Conformant
(Prim_Op
, Node
(Op_Elmt_2
))
2230 and then No
(Interface_Alias
(Node
(Op_Elmt_2
)))
2232 -- The private inherited operation has been
2233 -- overridden by an explicit subprogram:
2234 -- replace the former by the latter.
2236 New_Op
:= Node
(Op_Elmt_2
);
2237 Replace_Elmt
(Op_Elmt
, New_Op
);
2238 Remove_Elmt
(Op_List
, Op_Elmt_2
);
2239 Set_Overridden_Operation
(New_Op
, Parent_Subp
);
2240 Set_Is_Ada_2022_Only
(New_Op
,
2241 Is_Ada_2022_Only
(Parent_Subp
));
2243 -- We don't need to inherit its dispatching slot.
2244 -- Set_All_DT_Position has previously ensured that
2245 -- the same slot was assigned to the two primitives
2248 and then Present
(DTC_Entity
(New_Op
))
2249 and then Present
(DTC_Entity
(Prim_Op
))
2252 (DT_Position
(New_Op
) = DT_Position
(Prim_Op
));
2256 goto Next_Primitive
;
2259 Next_Elmt
(Op_Elmt_2
);
2262 -- Case 2: We have not found any explicit overriding and
2263 -- hence we need to declare the operation (i.e., make it
2266 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
2268 -- Inherit the dispatching slot if E is already frozen
2271 and then Present
(DTC_Entity
(Alias
(Prim_Op
)))
2273 Set_DTC_Entity_Value
(E
, New_Op
);
2274 Set_DT_Position_Value
(New_Op
,
2275 DT_Position
(Alias
(Prim_Op
)));
2279 (Is_Dispatching_Operation
(New_Op
)
2280 and then Node
(Last_Elmt
(Op_List
)) = New_Op
);
2282 -- Substitute the new operation for the old one in the
2283 -- type's primitive operations list. Since the new
2284 -- operation was also just added to the end of list,
2285 -- the last element must be removed.
2287 -- (Question: is there a simpler way of declaring the
2288 -- operation, say by just replacing the name of the
2289 -- earlier operation, reentering it in the in the symbol
2290 -- table (how?), and marking it as private???)
2292 Replace_Elmt
(Op_Elmt
, New_Op
);
2293 Remove_Last_Elmt
(Op_List
);
2297 Next_Elmt
(Op_Elmt
);
2300 -- Generate listing showing the contents of the dispatch table
2302 if Debug_Flag_ZZ
then
2307 -- For untagged type, scan forward to locate inherited hidden
2310 Prim_Op
:= Next_Entity
(E
);
2311 while Present
(Prim_Op
) loop
2312 if Is_Subprogram
(Prim_Op
)
2313 and then Present
(Alias
(Prim_Op
))
2314 and then not Comes_From_Source
(Prim_Op
)
2315 and then Is_Internal_Name
(Chars
(Prim_Op
))
2316 and then not Is_Internal_Name
(Chars
(Alias
(Prim_Op
)))
2317 and then Is_Primitive_Of
(E
, Prim_Op
)
2319 Derive_Subprogram
(New_Op
, Alias
(Prim_Op
), E
, Etype
(E
));
2322 Next_Entity
(Prim_Op
);
2324 -- Derived operations appear immediately after the type
2325 -- declaration (or the following subtype indication for
2326 -- a derived scalar type). Further declarations cannot
2327 -- include inherited operations of the type.
2329 exit when Present
(Prim_Op
)
2330 and then not Is_Overloadable
(Prim_Op
);
2337 end Declare_Inherited_Private_Subprograms
;
2339 -----------------------
2340 -- End_Package_Scope --
2341 -----------------------
2343 procedure End_Package_Scope
(P
: Entity_Id
) is
2345 Uninstall_Declarations
(P
);
2347 end End_Package_Scope
;
2349 ---------------------------
2350 -- Exchange_Declarations --
2351 ---------------------------
2353 procedure Exchange_Declarations
(Id
: Entity_Id
) is
2354 Full_Id
: constant Entity_Id
:= Full_View
(Id
);
2355 H1
: constant Entity_Id
:= Homonym
(Id
);
2356 Next1
: constant Entity_Id
:= Next_Entity
(Id
);
2361 -- If missing full declaration for type, nothing to exchange
2363 if No
(Full_Id
) then
2367 -- Otherwise complete the exchange, and preserve semantic links
2369 Next2
:= Next_Entity
(Full_Id
);
2370 H2
:= Homonym
(Full_Id
);
2372 -- Reset full declaration pointer to reflect the switched entities and
2373 -- readjust the next entity chains.
2375 Exchange_Entities
(Id
, Full_Id
);
2377 Link_Entities
(Id
, Next1
);
2378 Set_Homonym
(Id
, H1
);
2380 Set_Full_View
(Full_Id
, Id
);
2381 Link_Entities
(Full_Id
, Next2
);
2382 Set_Homonym
(Full_Id
, H2
);
2383 end Exchange_Declarations
;
2385 ----------------------------
2386 -- Install_Package_Entity --
2387 ----------------------------
2389 procedure Install_Package_Entity
(Id
: Entity_Id
) is
2391 if not Is_Internal
(Id
) then
2392 if Debug_Flag_E
then
2393 Write_Str
("Install: ");
2394 Write_Name
(Chars
(Id
));
2398 if Is_Child_Unit
(Id
) then
2401 -- Do not enter implicitly inherited non-overridden subprograms of
2402 -- a tagged type back into visibility if they have non-conformant
2403 -- homographs (Ada RM 8.3 12.3/2).
2405 elsif Is_Hidden_Non_Overridden_Subpgm
(Id
) then
2409 Set_Is_Immediately_Visible
(Id
);
2412 end Install_Package_Entity
;
2414 ----------------------------------
2415 -- Install_Private_Declarations --
2416 ----------------------------------
2418 procedure Install_Private_Declarations
(P
: Entity_Id
) is
2421 Priv_Deps
: Elist_Id
;
2423 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2424 -- When the full view of a private type is made available, we do the
2425 -- same for its private dependents under proper visibility conditions.
2426 -- When compiling a child unit this needs to be done recursively.
2428 -----------------------------
2429 -- Swap_Private_Dependents --
2430 -----------------------------
2432 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
2436 Priv_Elmt
: Elmt_Id
;
2440 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
2441 while Present
(Priv_Elmt
) loop
2442 Priv
:= Node
(Priv_Elmt
);
2444 -- Before the exchange, verify that the presence of the Full_View
2445 -- field. This field will be empty if the entity has already been
2446 -- installed due to a previous call.
2448 if Present
(Full_View
(Priv
)) and then Is_Visible_Dependent
(Priv
)
2450 if Is_Private_Type
(Priv
) then
2451 Cunit
:= Cunit_Entity
(Current_Sem_Unit
);
2452 Deps
:= Private_Dependents
(Priv
);
2458 -- For each subtype that is swapped, we also swap the reference
2459 -- to it in Private_Dependents, to allow access to it when we
2460 -- swap them out in End_Package_Scope.
2462 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
2464 -- Ensure that both views of the dependent private subtype are
2465 -- immediately visible if within some open scope. Check full
2466 -- view before exchanging views.
2468 if In_Open_Scopes
(Scope
(Full_View
(Priv
))) then
2469 Set_Is_Immediately_Visible
(Priv
);
2472 Exchange_Declarations
(Priv
);
2473 Set_Is_Immediately_Visible
2474 (Priv
, In_Open_Scopes
(Scope
(Priv
)));
2476 Set_Is_Potentially_Use_Visible
2477 (Priv
, Is_Potentially_Use_Visible
(Node
(Priv_Elmt
)));
2479 -- Recurse for child units, except in generic child units,
2480 -- which unfortunately handle private_dependents separately.
2481 -- Note that the current unit may not have been analyzed,
2482 -- for example a package body, so we cannot rely solely on
2483 -- the Is_Child_Unit flag, but that's only an optimization.
2486 and then (No
(Etype
(Cunit
)) or else Is_Child_Unit
(Cunit
))
2487 and then not Is_Empty_Elmt_List
(Deps
)
2488 and then not Inside_A_Generic
2490 Swap_Private_Dependents
(Deps
);
2494 Next_Elmt
(Priv_Elmt
);
2496 end Swap_Private_Dependents
;
2498 -- Start of processing for Install_Private_Declarations
2501 -- First exchange declarations for private types, so that the full
2502 -- declaration is visible. For each private type, we check its
2503 -- Private_Dependents list and also exchange any subtypes of or derived
2504 -- types from it. Finally, if this is a Taft amendment type, the
2505 -- incomplete declaration is irrelevant, and we want to link the
2506 -- eventual full declaration with the original private one so we
2507 -- also skip the exchange.
2509 Id
:= First_Entity
(P
);
2510 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
2511 if Is_Private_Base_Type
(Id
)
2512 and then Present
(Full_View
(Id
))
2513 and then Comes_From_Source
(Full_View
(Id
))
2514 and then Scope
(Full_View
(Id
)) = Scope
(Id
)
2515 and then Ekind
(Full_View
(Id
)) /= E_Incomplete_Type
2517 -- If there is a use-type clause on the private type, set the full
2518 -- view accordingly.
2520 Set_In_Use
(Full_View
(Id
), In_Use
(Id
));
2521 Full
:= Full_View
(Id
);
2523 if Is_Private_Base_Type
(Full
)
2524 and then Has_Private_Declaration
(Full
)
2525 and then Nkind
(Parent
(Full
)) = N_Full_Type_Declaration
2526 and then In_Open_Scopes
(Scope
(Etype
(Full
)))
2527 and then In_Package_Body
(Current_Scope
)
2528 and then not Is_Private_Type
(Etype
(Full
))
2530 -- This is the completion of a private type by a derivation
2531 -- from another private type which is not private anymore. This
2532 -- can only happen in a package nested within a child package,
2533 -- when the parent type is defined in the parent unit. At this
2534 -- point the current type is not private either, and we have
2535 -- to install the underlying full view, which is now visible.
2536 -- Save the current full view as well, so that all views can be
2537 -- restored on exit. It may seem that after compiling the child
2538 -- body there are not environments to restore, but the back-end
2539 -- expects those links to be valid, and freeze nodes depend on
2542 if No
(Full_View
(Full
))
2543 and then Present
(Underlying_Full_View
(Full
))
2545 Set_Full_View
(Id
, Underlying_Full_View
(Full
));
2546 Set_Underlying_Full_View
(Id
, Full
);
2547 Set_Is_Underlying_Full_View
(Full
);
2549 Set_Underlying_Full_View
(Full
, Empty
);
2550 Set_Is_Frozen
(Full_View
(Id
));
2554 Priv_Deps
:= Private_Dependents
(Id
);
2555 Exchange_Declarations
(Id
);
2556 Set_Is_Immediately_Visible
(Id
);
2557 Swap_Private_Dependents
(Priv_Deps
);
2563 -- Next make other declarations in the private part visible as well
2565 Id
:= First_Private_Entity
(P
);
2566 while Present
(Id
) loop
2567 Install_Package_Entity
(Id
);
2568 Set_Is_Hidden
(Id
, False);
2572 -- An abstract state is partially refined when it has at least one
2573 -- Part_Of constituent. Since these constituents are being installed
2574 -- into visibility, update the partial refinement status of any state
2575 -- defined in the associated package, subject to at least one Part_Of
2578 if Is_Package_Or_Generic_Package
(P
) then
2580 States
: constant Elist_Id
:= Abstract_States
(P
);
2581 State_Elmt
: Elmt_Id
;
2582 State_Id
: Entity_Id
;
2585 if Present
(States
) then
2586 State_Elmt
:= First_Elmt
(States
);
2587 while Present
(State_Elmt
) loop
2588 State_Id
:= Node
(State_Elmt
);
2590 if Present
(Part_Of_Constituents
(State_Id
)) then
2591 Set_Has_Partial_Visible_Refinement
(State_Id
);
2594 Next_Elmt
(State_Elmt
);
2600 -- Indicate that the private part is currently visible, so it can be
2601 -- properly reset on exit.
2603 Set_In_Private_Part
(P
);
2604 end Install_Private_Declarations
;
2606 ----------------------------------
2607 -- Install_Visible_Declarations --
2608 ----------------------------------
2610 procedure Install_Visible_Declarations
(P
: Entity_Id
) is
2612 Last_Entity
: Entity_Id
;
2616 (Is_Package_Or_Generic_Package
(P
) or else Is_Record_Type
(P
));
2618 if Is_Package_Or_Generic_Package
(P
) then
2619 Last_Entity
:= First_Private_Entity
(P
);
2621 Last_Entity
:= Empty
;
2624 Id
:= First_Entity
(P
);
2625 while Present
(Id
) and then Id
/= Last_Entity
loop
2626 Install_Package_Entity
(Id
);
2629 end Install_Visible_Declarations
;
2631 --------------------------
2632 -- Is_Private_Base_Type --
2633 --------------------------
2635 function Is_Private_Base_Type
(E
: Entity_Id
) return Boolean is
2637 return Ekind
(E
) = E_Private_Type
2638 or else Ekind
(E
) = E_Limited_Private_Type
2639 or else Ekind
(E
) = E_Record_Type_With_Private
;
2640 end Is_Private_Base_Type
;
2642 --------------------------
2643 -- Is_Visible_Dependent --
2644 --------------------------
2646 function Is_Visible_Dependent
(Dep
: Entity_Id
) return Boolean
2648 S
: constant Entity_Id
:= Scope
(Dep
);
2651 -- Renamings created for actual types have the visibility of the actual
2653 if Ekind
(S
) = E_Package
2654 and then Is_Generic_Instance
(S
)
2655 and then (Is_Generic_Actual_Type
(Dep
)
2656 or else Is_Generic_Actual_Type
(Full_View
(Dep
)))
2660 elsif not (Is_Derived_Type
(Dep
))
2661 and then Is_Derived_Type
(Full_View
(Dep
))
2663 -- When instantiating a package body, the scope stack is empty, so
2664 -- check instead whether the dependent type is defined in the same
2665 -- scope as the instance itself.
2667 return In_Open_Scopes
(S
)
2668 or else (Is_Generic_Instance
(Current_Scope
)
2669 and then Scope
(Dep
) = Scope
(Current_Scope
));
2673 end Is_Visible_Dependent
;
2675 ----------------------------
2676 -- May_Need_Implicit_Body --
2677 ----------------------------
2679 procedure May_Need_Implicit_Body
(E
: Entity_Id
) is
2680 P
: constant Node_Id
:= Unit_Declaration_Node
(E
);
2681 S
: constant Node_Id
:= Parent
(P
);
2686 if not Has_Completion
(E
)
2687 and then Nkind
(P
) = N_Package_Declaration
2688 and then (Present
(Activation_Chain_Entity
(P
)) or else Has_RACW
(E
))
2691 Make_Package_Body
(Sloc
(E
),
2692 Defining_Unit_Name
=> Make_Defining_Identifier
(Sloc
(E
),
2693 Chars
=> Chars
(E
)),
2694 Declarations
=> New_List
);
2696 if Nkind
(S
) = N_Package_Specification
then
2697 if Present
(Private_Declarations
(S
)) then
2698 Decls
:= Private_Declarations
(S
);
2700 Decls
:= Visible_Declarations
(S
);
2703 Decls
:= Declarations
(S
);
2709 end May_Need_Implicit_Body
;
2711 ----------------------
2712 -- New_Private_Type --
2713 ----------------------
2715 procedure New_Private_Type
(N
: Node_Id
; Id
: Entity_Id
; Def
: Node_Id
) is
2717 -- For other than Ada 2012, enter the name in the current scope
2719 if Ada_Version
< Ada_2012
then
2722 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2723 -- there may be an incomplete previous view.
2729 Prev
:= Find_Type_Name
(N
);
2730 pragma Assert
(Prev
= Id
2731 or else (Ekind
(Prev
) = E_Incomplete_Type
2732 and then Present
(Full_View
(Prev
))
2733 and then Full_View
(Prev
) = Id
));
2737 if Limited_Present
(Def
) then
2738 Mutate_Ekind
(Id
, E_Limited_Private_Type
);
2740 Mutate_Ekind
(Id
, E_Private_Type
);
2743 Set_Is_Not_Self_Hidden
(Id
);
2745 Set_Has_Delayed_Freeze
(Id
);
2746 Set_Is_First_Subtype
(Id
);
2747 Reinit_Size_Align
(Id
);
2749 Set_Is_Constrained
(Id
,
2750 No
(Discriminant_Specifications
(N
))
2751 and then not Unknown_Discriminants_Present
(N
));
2753 -- Set tagged flag before processing discriminants, to catch illegal
2756 Set_Is_Tagged_Type
(Id
, Tagged_Present
(Def
));
2758 Set_Discriminant_Constraint
(Id
, No_Elist
);
2759 Set_Stored_Constraint
(Id
, No_Elist
);
2761 if Present
(Discriminant_Specifications
(N
)) then
2763 Process_Discriminants
(N
);
2766 elsif Unknown_Discriminants_Present
(N
) then
2767 Set_Has_Unknown_Discriminants
(Id
);
2770 Set_Private_Dependents
(Id
, New_Elmt_List
);
2772 if Tagged_Present
(Def
) then
2773 Mutate_Ekind
(Id
, E_Record_Type_With_Private
);
2774 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2775 Set_Is_Abstract_Type
(Id
, Abstract_Present
(Def
));
2776 Set_Is_Limited_Record
(Id
, Limited_Present
(Def
));
2777 Set_Has_Delayed_Freeze
(Id
, True);
2779 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2781 if Is_RTE
(Id
, RE_Timing_Event
) then
2782 Set_Has_Timing_Event
(Id
);
2785 -- Create a class-wide type with the same attributes
2787 Make_Class_Wide_Type
(Id
);
2789 elsif Abstract_Present
(Def
) then
2790 Error_Msg_N
("only a tagged type can be abstract", N
);
2792 -- We initialize the primitive operations list of an untagged private
2793 -- type to an empty element list. Do this even when Extensions_Allowed
2794 -- is False to issue better error messages. (Note: This could be done
2795 -- for all private types and shared with the tagged case above, but
2796 -- for now we do it separately.)
2799 Set_Direct_Primitive_Operations
(Id
, New_Elmt_List
);
2801 end New_Private_Type
;
2803 ---------------------------------
2804 -- Requires_Completion_In_Body --
2805 ---------------------------------
2807 function Requires_Completion_In_Body
2809 Pack_Id
: Entity_Id
;
2810 Do_Abstract_States
: Boolean := False) return Boolean
2813 -- Always ignore child units. Child units get added to the entity list
2814 -- of a parent unit, but are not original entities of the parent, and
2815 -- so do not affect whether the parent needs a body.
2817 if Is_Child_Unit
(Id
) then
2820 -- Ignore formal packages and their renamings
2822 elsif Ekind
(Id
) = E_Package
2823 and then Nkind
(Original_Node
(Unit_Declaration_Node
(Id
))) =
2824 N_Formal_Package_Declaration
2828 -- Otherwise test to see if entity requires a completion. Note that
2829 -- subprogram entities whose declaration does not come from source are
2830 -- ignored here on the basis that we assume the expander will provide an
2831 -- implicit completion at some point.
2833 elsif (Is_Overloadable
(Id
)
2834 and then Ekind
(Id
) not in E_Enumeration_Literal | E_Operator
2835 and then not Is_Abstract_Subprogram
(Id
)
2836 and then not Has_Completion
(Id
)
2837 and then Comes_From_Source
(Parent
(Id
)))
2840 (Ekind
(Id
) = E_Package
2841 and then Id
/= Pack_Id
2842 and then not Has_Completion
(Id
)
2843 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2846 (Ekind
(Id
) = E_Incomplete_Type
2847 and then No
(Full_View
(Id
))
2848 and then not Is_Generic_Type
(Id
))
2851 (Ekind
(Id
) in E_Task_Type | E_Protected_Type
2852 and then not Has_Completion
(Id
))
2855 (Ekind
(Id
) = E_Generic_Package
2856 and then Id
/= Pack_Id
2857 and then not Has_Completion
(Id
)
2858 and then Unit_Requires_Body
(Id
, Do_Abstract_States
))
2861 (Is_Generic_Subprogram
(Id
)
2862 and then not Has_Completion
(Id
))
2866 -- Otherwise the entity does not require completion in a package body
2871 end Requires_Completion_In_Body
;
2873 ----------------------------
2874 -- Uninstall_Declarations --
2875 ----------------------------
2877 procedure Uninstall_Declarations
(P
: Entity_Id
) is
2878 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
2882 procedure Preserve_Full_Attributes
(Priv
: Entity_Id
; Full
: Entity_Id
);
2883 -- Copy to the private declaration the attributes of the full view that
2884 -- need to be available for the partial view also.
2886 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
);
2887 -- When the full view of a private type is made unavailable, we do the
2888 -- same for its private dependents under proper visibility conditions.
2889 -- When compiling a child unit this needs to be done recursively.
2891 function Type_In_Use
(T
: Entity_Id
) return Boolean;
2892 -- Check whether type or base type appear in an active use_type clause
2894 ------------------------------
2895 -- Preserve_Full_Attributes --
2896 ------------------------------
2898 procedure Preserve_Full_Attributes
2902 Full_Base
: constant Entity_Id
:= Base_Type
(Full
);
2903 Priv_Is_Base_Type
: constant Boolean := Is_Base_Type
(Priv
);
2906 Set_Size_Info
(Priv
, Full
);
2907 Copy_RM_Size
(To
=> Priv
, From
=> Full
);
2908 Set_Size_Known_At_Compile_Time
2909 (Priv
, Size_Known_At_Compile_Time
(Full
));
2910 Set_Is_Volatile
(Priv
, Is_Volatile
(Full
));
2911 Set_Treat_As_Volatile
(Priv
, Treat_As_Volatile
(Full
));
2912 Set_Is_Atomic
(Priv
, Is_Atomic
(Full
));
2913 Set_Is_Ada_2005_Only
(Priv
, Is_Ada_2005_Only
(Full
));
2914 Set_Is_Ada_2012_Only
(Priv
, Is_Ada_2012_Only
(Full
));
2915 Set_Is_Ada_2022_Only
(Priv
, Is_Ada_2022_Only
(Full
));
2916 Set_Has_Pragma_Unmodified
(Priv
, Has_Pragma_Unmodified
(Full
));
2917 Set_Has_Pragma_Unreferenced
(Priv
, Has_Pragma_Unreferenced
(Full
));
2918 Set_Has_Pragma_Unreferenced_Objects
2919 (Priv
, Has_Pragma_Unreferenced_Objects
2921 Set_Predicates_Ignored
(Priv
, Predicates_Ignored
(Full
));
2922 if Is_Unchecked_Union
(Full
) then
2923 Set_Is_Unchecked_Union
(Base_Type
(Priv
));
2926 if Referenced
(Full
) then
2927 Set_Referenced
(Priv
);
2930 if Priv_Is_Base_Type
then
2931 Set_Is_Controlled_Active
2932 (Priv
, Is_Controlled_Active
(Full_Base
));
2933 Set_Finalize_Storage_Only
2934 (Priv
, Finalize_Storage_Only
(Full_Base
));
2935 Set_Has_Controlled_Component
2936 (Priv
, Has_Controlled_Component
(Full_Base
));
2938 Propagate_Concurrent_Flags
(Priv
, Base_Type
(Full
));
2941 -- As explained in Freeze_Entity, private types are required to point
2942 -- to the same freeze node as their corresponding full view, if any.
2943 -- But we ought not to overwrite a node already inserted in the tree.
2946 (Serious_Errors_Detected
/= 0
2947 or else No
(Freeze_Node
(Priv
))
2948 or else No
(Parent
(Freeze_Node
(Priv
)))
2949 or else Freeze_Node
(Priv
) = Freeze_Node
(Full
));
2951 Set_Freeze_Node
(Priv
, Freeze_Node
(Full
));
2953 -- Propagate Default_Initial_Condition-related attributes from the
2954 -- full view to the private view.
2956 Propagate_DIC_Attributes
(Priv
, From_Typ
=> Full
);
2958 -- Propagate invariant-related attributes from the full view to the
2961 Propagate_Invariant_Attributes
(Priv
, From_Typ
=> Full
);
2963 -- Propagate predicate-related attributes from the full view to the
2966 Propagate_Predicate_Attributes
(Priv
, From_Typ
=> Full
);
2968 if Is_Tagged_Type
(Priv
)
2969 and then Is_Tagged_Type
(Full
)
2970 and then not Error_Posted
(Full
)
2972 if Is_Tagged_Type
(Priv
) then
2974 -- If the type is tagged, the tag itself must be available on
2975 -- the partial view, for expansion purposes.
2977 Set_First_Entity
(Priv
, First_Entity
(Full
));
2979 -- If there are discriminants in the partial view, these remain
2980 -- visible. Otherwise only the tag itself is visible, and there
2981 -- are no nameable components in the partial view.
2983 if No
(Last_Entity
(Priv
)) then
2984 Set_Last_Entity
(Priv
, First_Entity
(Priv
));
2988 Set_Has_Discriminants
(Priv
, Has_Discriminants
(Full
));
2990 if Has_Discriminants
(Full
) then
2991 Set_Discriminant_Constraint
(Priv
,
2992 Discriminant_Constraint
(Full
));
2995 end Preserve_Full_Attributes
;
2997 -----------------------------
2998 -- Swap_Private_Dependents --
2999 -----------------------------
3001 procedure Swap_Private_Dependents
(Priv_Deps
: Elist_Id
) is
3005 Priv_Elmt
: Elmt_Id
;
3009 Priv_Elmt
:= First_Elmt
(Priv_Deps
);
3010 while Present
(Priv_Elmt
) loop
3011 Priv
:= Node
(Priv_Elmt
);
3013 -- Before we do the swap, we verify the presence of the Full_View
3014 -- field, which may be empty due to a swap by a previous call to
3015 -- End_Package_Scope (e.g. from the freezing mechanism).
3017 if Present
(Full_View
(Priv
)) then
3018 if Is_Private_Type
(Priv
) then
3019 Cunit
:= Cunit_Entity
(Current_Sem_Unit
);
3020 Deps
:= Private_Dependents
(Priv
);
3027 or else not In_Open_Scopes
(Scope
(Priv
))
3029 Set_Is_Immediately_Visible
(Priv
, False);
3032 if Is_Visible_Dependent
(Priv
) then
3033 Preserve_Full_Attributes
(Priv
, Full_View
(Priv
));
3034 Replace_Elmt
(Priv_Elmt
, Full_View
(Priv
));
3035 Exchange_Declarations
(Priv
);
3037 -- Recurse for child units, except in generic child units,
3038 -- which unfortunately handle private_dependents separately.
3039 -- Note that the current unit may not have been analyzed,
3040 -- for example a package body, so we cannot rely solely on
3041 -- the Is_Child_Unit flag, but that's only an optimization.
3044 and then (No
(Etype
(Cunit
)) or else Is_Child_Unit
(Cunit
))
3045 and then not Is_Empty_Elmt_List
(Deps
)
3046 and then not Inside_A_Generic
3048 Swap_Private_Dependents
(Deps
);
3053 Next_Elmt
(Priv_Elmt
);
3055 end Swap_Private_Dependents
;
3061 function Type_In_Use
(T
: Entity_Id
) return Boolean is
3063 return Scope
(Base_Type
(T
)) = P
3064 and then (In_Use
(T
) or else In_Use
(Base_Type
(T
)));
3067 -- Start of processing for Uninstall_Declarations
3070 Id
:= First_Entity
(P
);
3071 while Present
(Id
) and then Id
/= First_Private_Entity
(P
) loop
3072 if Debug_Flag_E
then
3073 Write_Str
("unlinking visible entity ");
3074 Write_Int
(Int
(Id
));
3078 -- On exit from the package scope, we must preserve the visibility
3079 -- established by use clauses in the current scope. Two cases:
3081 -- a) If the entity is an operator, it may be a primitive operator of
3082 -- a type for which there is a visible use-type clause.
3084 -- b) For other entities, their use-visibility is determined by a
3085 -- visible use clause for the package itself or a use-all-type clause
3086 -- applied directly to the entity's type. For a generic instance,
3087 -- the instantiation of the formals appears in the visible part,
3088 -- but the formals are private and remain so.
3090 if Ekind
(Id
) = E_Function
3091 and then Is_Operator_Symbol_Name
(Chars
(Id
))
3092 and then not Is_Hidden
(Id
)
3093 and then not Error_Posted
(Id
)
3095 Set_Is_Potentially_Use_Visible
(Id
,
3097 or else Type_In_Use
(Etype
(Id
))
3098 or else Type_In_Use
(Etype
(First_Formal
(Id
)))
3099 or else (Present
(Next_Formal
(First_Formal
(Id
)))
3102 (Etype
(Next_Formal
(First_Formal
(Id
))))));
3104 if In_Use
(P
) and then not Is_Hidden
(Id
) then
3106 -- A child unit of a use-visible package remains use-visible
3107 -- only if it is itself a visible child unit. Otherwise it
3108 -- would remain visible in other contexts where P is use-
3109 -- visible, because once compiled it stays in the entity list
3110 -- of its parent unit.
3112 if Is_Child_Unit
(Id
) then
3113 Set_Is_Potentially_Use_Visible
3114 (Id
, Is_Visible_Lib_Unit
(Id
));
3116 Set_Is_Potentially_Use_Visible
(Id
);
3119 -- Avoid crash caused by previous errors
3121 elsif No
(Etype
(Id
)) and then Serious_Errors_Detected
/= 0 then
3124 -- We need to avoid incorrectly marking enumeration literals as
3125 -- non-visible when a visible use-all-type clause is in effect.
3127 elsif Type_In_Use
(Etype
(Id
))
3128 and then Nkind
(Current_Use_Clause
(Etype
(Id
))) =
3130 and then All_Present
(Current_Use_Clause
(Etype
(Id
)))
3135 Set_Is_Potentially_Use_Visible
(Id
, False);
3139 -- Local entities are not immediately visible outside of the package
3141 Set_Is_Immediately_Visible
(Id
, False);
3143 -- If this is a private type with a full view (for example a local
3144 -- subtype of a private type declared elsewhere), ensure that the
3145 -- full view is also removed from visibility: it may be exposed when
3146 -- swapping views in an instantiation. Similarly, ensure that the
3147 -- use-visibility is properly set on both views.
3149 if Is_Type
(Id
) and then Present
(Full_View
(Id
)) then
3150 Set_Is_Immediately_Visible
(Full_View
(Id
), False);
3151 Set_Is_Potentially_Use_Visible
(Full_View
(Id
),
3152 Is_Potentially_Use_Visible
(Id
));
3155 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
3156 Check_Abstract_Overriding
(Id
);
3157 Check_Conventions
(Id
);
3160 if Ekind
(Id
) in E_Private_Type | E_Limited_Private_Type
3161 and then No
(Full_View
(Id
))
3162 and then not Is_Generic_Type
(Id
)
3163 and then not Is_Derived_Type
(Id
)
3165 Error_Msg_N
("missing full declaration for private type&", Id
);
3167 elsif Ekind
(Id
) = E_Record_Type_With_Private
3168 and then not Is_Generic_Type
(Id
)
3169 and then No
(Full_View
(Id
))
3171 if Nkind
(Parent
(Id
)) = N_Private_Type_Declaration
then
3172 Error_Msg_N
("missing full declaration for private type&", Id
);
3175 ("missing full declaration for private extension", Id
);
3178 -- Case of constant, check for deferred constant declaration with
3179 -- no full view. Likely just a matter of a missing expression, or
3180 -- accidental use of the keyword constant.
3182 elsif Ekind
(Id
) = E_Constant
3184 -- OK if constant value present
3186 and then No
(Constant_Value
(Id
))
3188 -- OK if full view present
3190 and then No
(Full_View
(Id
))
3192 -- OK if imported, since that provides the completion
3194 and then not Is_Imported
(Id
)
3196 -- OK if object declaration replaced by renaming declaration as
3197 -- a result of OK_To_Rename processing (e.g. for concatenation)
3199 and then Nkind
(Parent
(Id
)) /= N_Object_Renaming_Declaration
3201 -- OK if object declaration with the No_Initialization flag set
3203 and then not (Nkind
(Parent
(Id
)) = N_Object_Declaration
3204 and then No_Initialization
(Parent
(Id
)))
3206 -- If no private declaration is present, we assume the user did
3207 -- not intend a deferred constant declaration and the problem
3208 -- is simply that the initializing expression is missing.
3210 if not Has_Private_Declaration
(Etype
(Id
)) then
3212 ("constant declaration requires initialization expression",
3215 if Is_Limited_Type
(Etype
(Id
)) then
3217 ("\if variable intended, remove CONSTANT from declaration",
3221 -- Otherwise if a private declaration is present, then we are
3222 -- missing the full declaration for the deferred constant.
3226 ("missing full declaration for deferred constant (RM 7.4)",
3229 if Is_Limited_Type
(Etype
(Id
)) then
3231 ("\if variable intended, remove CONSTANT from declaration",
3240 -- If the specification was installed as the parent of a public child
3241 -- unit, the private declarations were not installed, and there is
3244 if not In_Private_Part
(P
) then
3248 -- Reset the flag now
3250 Set_In_Private_Part
(P
, False);
3252 -- Make private entities invisible and exchange full and private
3253 -- declarations for private types. Id is now the first private entity
3256 while Present
(Id
) loop
3257 if Debug_Flag_E
then
3258 Write_Str
("unlinking private entity ");
3259 Write_Int
(Int
(Id
));
3263 if Is_Tagged_Type
(Id
) and then Ekind
(Id
) = E_Record_Type
then
3264 Check_Abstract_Overriding
(Id
);
3265 Check_Conventions
(Id
);
3268 Set_Is_Immediately_Visible
(Id
, False);
3270 if Is_Private_Base_Type
(Id
) and then Present
(Full_View
(Id
)) then
3271 Full
:= Full_View
(Id
);
3273 -- If the partial view is not declared in the visible part of the
3274 -- package (as is the case when it is a type derived from some
3275 -- other private type in the private part of the current package),
3276 -- no exchange takes place.
3279 or else List_Containing
(Parent
(Id
)) /=
3280 Visible_Declarations
(Specification
(Decl
))
3285 -- The entry in the private part points to the full declaration,
3286 -- which is currently visible. Exchange them so only the private
3287 -- type declaration remains accessible, and link private and full
3288 -- declaration in the opposite direction. Before the actual
3289 -- exchange, we copy back attributes of the full view that must
3290 -- be available to the partial view too.
3292 Preserve_Full_Attributes
(Id
, Full
);
3294 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(P
));
3296 -- The following test may be redundant, as this is already
3297 -- diagnosed in sem_ch3. ???
3299 if not Is_Definite_Subtype
(Full
)
3300 and then Is_Definite_Subtype
(Id
)
3302 Error_Msg_Sloc
:= Sloc
(Parent
(Id
));
3304 ("full view of& not compatible with declaration#", Full
, Id
);
3307 -- Swap out the subtypes and derived types of Id that
3308 -- were compiled in this scope, or installed previously
3309 -- by Install_Private_Declarations.
3311 Swap_Private_Dependents
(Private_Dependents
(Id
));
3313 -- Now restore the type itself to its private view
3315 Exchange_Declarations
(Id
);
3317 -- If we have installed an underlying full view for a type derived
3318 -- from a private type in a child unit, restore the proper views
3319 -- of private and full view. See corresponding code in
3320 -- Install_Private_Declarations.
3322 -- After the exchange, Full denotes the private type in the
3323 -- visible part of the package.
3325 if Is_Private_Base_Type
(Full
)
3326 and then Present
(Full_View
(Full
))
3327 and then Present
(Underlying_Full_View
(Full
))
3328 and then In_Package_Body
(Current_Scope
)
3330 Set_Full_View
(Full
, Underlying_Full_View
(Full
));
3331 Set_Underlying_Full_View
(Full
, Empty
);
3334 elsif Ekind
(Id
) = E_Incomplete_Type
3335 and then Comes_From_Source
(Id
)
3336 and then No
(Full_View
(Id
))
3338 -- Mark Taft amendment types. Verify that there are no primitive
3339 -- operations declared for the type (3.10.1(9)).
3341 Set_Has_Completion_In_Body
(Id
);
3348 Elmt
:= First_Elmt
(Private_Dependents
(Id
));
3349 while Present
(Elmt
) loop
3350 Subp
:= Node
(Elmt
);
3352 -- Is_Primitive is tested because there can be cases where
3353 -- nonprimitive subprograms (in nested packages) are added
3354 -- to the Private_Dependents list.
3356 if Is_Overloadable
(Subp
) and then Is_Primitive
(Subp
) then
3358 ("type& must be completed in the private part",
3361 -- The result type of an access-to-function type cannot be a
3362 -- Taft-amendment type, unless the version is Ada 2012 or
3363 -- later (see AI05-151).
3365 elsif Ada_Version
< Ada_2012
3366 and then Ekind
(Subp
) = E_Subprogram_Type
3368 if Etype
(Subp
) = Id
3370 (Is_Class_Wide_Type
(Etype
(Subp
))
3371 and then Etype
(Etype
(Subp
)) = Id
)
3374 ("type& must be completed in the private part",
3375 Associated_Node_For_Itype
(Subp
), Id
);
3383 -- For subtypes of private types the frontend generates two entities:
3384 -- one associated with the partial view and the other associated with
3385 -- the full view. When the subtype declaration is public the frontend
3386 -- places the former entity in the list of public entities of the
3387 -- package and the latter entity in the private part of the package.
3388 -- When the subtype declaration is private it generates these two
3389 -- entities but both are placed in the private part of the package
3390 -- (and the full view has the same source location as the partial
3391 -- view and no parent; see Prepare_Private_Subtype_Completion).
3393 elsif Ekind
(Id
) in E_Private_Subtype
3394 | E_Limited_Private_Subtype
3395 and then Present
(Full_View
(Id
))
3396 and then Sloc
(Id
) = Sloc
(Full_View
(Id
))
3397 and then No
(Parent
(Full_View
(Id
)))
3400 Set_Is_Potentially_Use_Visible
(Id
, False);
3402 elsif not Is_Child_Unit
(Id
)
3403 and then (not Is_Private_Type
(Id
) or else No
(Full_View
(Id
)))
3406 Set_Is_Potentially_Use_Visible
(Id
, False);
3412 end Uninstall_Declarations
;
3414 ------------------------
3415 -- Unit_Requires_Body --
3416 ------------------------
3418 function Unit_Requires_Body
3419 (Pack_Id
: Entity_Id
;
3420 Do_Abstract_States
: Boolean := False) return Boolean
3424 Requires_Body
: Boolean := False;
3425 -- Flag set when the unit has at least one construct that requires
3426 -- completion in a body.
3429 -- Imported entity never requires body. Right now, only subprograms can
3430 -- be imported, but perhaps in the future we will allow import of
3433 if Is_Imported
(Pack_Id
) then
3436 -- Body required if library package with pragma Elaborate_Body
3438 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
3441 -- Body required if subprogram
3443 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
3446 -- Treat a block as requiring a body
3448 elsif Ekind
(Pack_Id
) = E_Block
then
3451 elsif Ekind
(Pack_Id
) = E_Package
3452 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3453 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3456 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3458 if Has_Pragma_Elaborate_Body
(G_P
) then
3464 -- Traverse the entity chain of the package and look for constructs that
3465 -- require a completion in a body.
3467 E
:= First_Entity
(Pack_Id
);
3468 while Present
(E
) loop
3470 -- Skip abstract states because their completion depends on several
3471 -- criteria (see below).
3473 if Ekind
(E
) = E_Abstract_State
then
3476 elsif Requires_Completion_In_Body
3477 (E
, Pack_Id
, Do_Abstract_States
)
3479 Requires_Body
:= True;
3486 -- A [generic] package that defines at least one non-null abstract state
3487 -- requires a completion only when at least one other construct requires
3488 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3489 -- performed if the caller requests this behavior.
3491 if Do_Abstract_States
3492 and then Is_Package_Or_Generic_Package
(Pack_Id
)
3493 and then Has_Non_Null_Abstract_State
(Pack_Id
)
3494 and then Requires_Body
3499 return Requires_Body
;
3500 end Unit_Requires_Body
;
3502 -----------------------------
3503 -- Unit_Requires_Body_Info --
3504 -----------------------------
3506 procedure Unit_Requires_Body_Info
(Pack_Id
: Entity_Id
) is
3510 -- An imported entity never requires body. Right now, only subprograms
3511 -- can be imported, but perhaps in the future we will allow import of
3514 if Is_Imported
(Pack_Id
) then
3517 -- Body required if library package with pragma Elaborate_Body
3519 elsif Has_Pragma_Elaborate_Body
(Pack_Id
) then
3520 Error_Msg_N
("info: & requires body (Elaborate_Body)?.y?", Pack_Id
);
3522 -- Body required if subprogram
3524 elsif Is_Subprogram_Or_Generic_Subprogram
(Pack_Id
) then
3525 Error_Msg_N
("info: & requires body (subprogram case)?.y?", Pack_Id
);
3527 -- Body required if generic parent has Elaborate_Body
3529 elsif Ekind
(Pack_Id
) = E_Package
3530 and then Nkind
(Parent
(Pack_Id
)) = N_Package_Specification
3531 and then Present
(Generic_Parent
(Parent
(Pack_Id
)))
3534 G_P
: constant Entity_Id
:= Generic_Parent
(Parent
(Pack_Id
));
3536 if Has_Pragma_Elaborate_Body
(G_P
) then
3538 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3543 -- A [generic] package that introduces at least one non-null abstract
3544 -- state requires completion. However, there is a separate rule that
3545 -- requires that such a package have a reason other than this for a
3546 -- body being required (if necessary a pragma Elaborate_Body must be
3547 -- provided). If Ignore_Abstract_State is True, we don't do this check
3548 -- (so we can use Unit_Requires_Body to check for some other reason).
3550 elsif Is_Package_Or_Generic_Package
(Pack_Id
)
3551 and then Present
(Abstract_States
(Pack_Id
))
3552 and then not Is_Null_State
3553 (Node
(First_Elmt
(Abstract_States
(Pack_Id
))))
3556 ("info: & requires body (non-null abstract state aspect)?.y?",
3560 -- Otherwise search entity chain for entity requiring completion
3562 E
:= First_Entity
(Pack_Id
);
3563 while Present
(E
) loop
3564 if Requires_Completion_In_Body
(E
, Pack_Id
) then
3565 Error_Msg_Node_2
:= E
;
3567 ("info: & requires body (& requires completion)?.y?", E
,
3573 end Unit_Requires_Body_Info
;