1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, 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 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Contracts
; use Contracts
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Einfo
.Entities
; use Einfo
.Entities
;
32 with Einfo
.Utils
; use Einfo
.Utils
;
33 with Errout
; use Errout
;
34 with Exp_Disp
; use Exp_Disp
;
36 with Exp_Util
; use Exp_Util
;
37 with Elists
; use Elists
;
38 with Fname
; use Fname
;
39 with Fname
.UF
; use Fname
.UF
;
40 with Freeze
; use Freeze
;
41 with Impunit
; use Impunit
;
42 with Inline
; use Inline
;
44 with Lib
.Load
; use Lib
.Load
;
45 with Lib
.Xref
; use Lib
.Xref
;
46 with Namet
; use Namet
;
47 with Nlists
; use Nlists
;
48 with Nmake
; use Nmake
;
50 with Output
; use Output
;
51 with Par_SCO
; use Par_SCO
;
52 with Restrict
; use Restrict
;
53 with Rident
; use Rident
;
54 with Rtsfind
; use Rtsfind
;
56 with Sem_Aux
; use Sem_Aux
;
57 with Sem_Ch3
; use Sem_Ch3
;
58 with Sem_Ch6
; use Sem_Ch6
;
59 with Sem_Ch7
; use Sem_Ch7
;
60 with Sem_Ch8
; use Sem_Ch8
;
61 with Sem_Ch13
; use Sem_Ch13
;
62 with Sem_Dist
; use Sem_Dist
;
63 with Sem_Prag
; use Sem_Prag
;
64 with Sem_Util
; use Sem_Util
;
65 with Sem_Warn
; use Sem_Warn
;
66 with Stand
; use Stand
;
67 with Sinfo
; use Sinfo
;
68 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
69 with Sinfo
.Utils
; use Sinfo
.Utils
;
70 with Sinfo
.CN
; use Sinfo
.CN
;
71 with Sinput
; use Sinput
;
72 with Snames
; use Snames
;
73 with Style
; use Style
;
74 with Stylesw
; use Stylesw
;
75 with Tbuild
; use Tbuild
;
76 with Uname
; use Uname
;
77 with Warnsw
; use Warnsw
;
79 package body Sem_Ch10
is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Analyze_Context
(N
: Node_Id
);
86 -- Analyzes items in the context clause of compilation unit
88 procedure Analyze_Required_Limited_With_Units
(N
: Node_Id
);
89 -- Subsidiary of Analyze_Compilation_Unit. Perform full analysis of the
90 -- limited-with units of N when it is a package declaration that does not
91 -- require a package body, and the profile of some subprogram defined in N
92 -- depends on shadow incomplete type entities visible through limited-with
93 -- context clauses. This analysis is required to provide the backend with
94 -- the non-limited view of these shadow entities.
96 procedure Build_Limited_Views
(N
: Node_Id
);
97 -- Build and decorate the list of shadow entities for a package mentioned
98 -- in a limited_with clause. If the package was not previously analyzed
99 -- then it also performs a basic decoration of the real entities. This is
100 -- required in order to avoid passing non-decorated entities to the
101 -- back-end. Implements Ada 2005 (AI-50217).
103 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
);
104 -- Common processing for all stubs (subprograms, tasks, packages, and
105 -- protected cases). N is the stub to be analyzed. Once the subunit name
106 -- is established, load and analyze. Nam is the non-overloadable entity
107 -- for which the proper body provides a completion. Subprogram stubs are
108 -- handled differently because they can be declarations.
110 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
);
111 -- Check whether the source for the body of a compilation unit must be
112 -- included in a standalone library.
114 procedure Check_No_Elab_Code_All
(N
: Node_Id
);
115 -- Carries out possible tests for violation of No_Elab_Code all for withed
116 -- units in the Context_Items of unit N.
118 procedure Check_Private_Child_Unit
(N
: Node_Id
);
119 -- If a with_clause mentions a private child unit, the compilation unit
120 -- must be a member of the same family, as described in 10.1.2.
122 procedure Check_Stub_Level
(N
: Node_Id
);
123 -- Verify that a stub is declared immediately within a compilation unit,
124 -- and not in an inner frame.
126 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
);
127 -- When a child unit appears in a context clause, the implicit withs on
128 -- parents are made explicit, and with clauses are inserted in the context
129 -- clause before the one for the child. If a parent in the with_clause
130 -- is a renaming, the implicit with_clause is on the renaming whose name
131 -- is mentioned in the with_clause, and not on the package it renames.
132 -- N is the compilation unit whose list of context items receives the
133 -- implicit with_clauses.
135 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
);
136 -- Generate cross-reference information for the parents of child units
137 -- and of subunits. N is a defining_program_unit_name, and P_Id is the
138 -- immediate parent scope.
140 function Has_With_Clause
143 Is_Limited
: Boolean := False) return Boolean;
144 -- Determine whether compilation unit C_Unit contains a [limited] with
145 -- clause for package Pack. Use the flag Is_Limited to designate desired
148 procedure Implicit_With_On_Parent
(Child_Unit
: Node_Id
; N
: Node_Id
);
149 -- If the main unit is a child unit, implicit withs are also added for
150 -- all its ancestors.
152 function In_Chain
(E
: Entity_Id
) return Boolean;
153 -- Check that the shadow entity is not already in the homonym chain, for
154 -- example through a limited_with clause in a parent unit.
156 procedure Install_Context_Clauses
(N
: Node_Id
; Chain
: Boolean := True);
157 -- Subsidiary to Install_Context and Install_Parents. Process all with
158 -- and use clauses for current unit and its library unit if any. The flag
159 -- Chain is used to control the "chaining" or linking together of use-type
160 -- and use-package clauses to avoid circularities with reinstalling
163 procedure Install_Limited_Context_Clauses
(N
: Node_Id
);
164 -- Subsidiary to Install_Context. Process only limited with_clauses for
165 -- current unit. Implements Ada 2005 (AI-50217).
167 procedure Install_Limited_With_Clause
(N
: Node_Id
);
168 -- Place shadow entities for a limited_with package in the visibility
169 -- structures for the current compilation. Implements Ada 2005 (AI-50217).
171 procedure Install_Parents
173 Is_Private
: Boolean;
174 Chain
: Boolean := True);
175 -- This procedure establishes the context for the compilation of a child
176 -- unit. If Lib_Unit is a child library spec then the context of the parent
177 -- is installed, and the parent itself made immediately visible, so that
178 -- the child unit is processed in the declarative region of the parent.
179 -- Install_Parents makes a recursive call to itself to ensure that all
180 -- parents are loaded in the nested case. If Lib_Unit is a library body,
181 -- the only effect of Install_Parents is to install the private decls of
182 -- the parents, because the visible parent declarations will have been
183 -- installed as part of the context of the corresponding spec. The flag
184 -- Chain is used to control the "chaining" or linking of use-type and
185 -- use-package clauses to avoid circularities when installing context.
187 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
);
188 -- In the compilation of a child unit, a child of any of the ancestor
189 -- units is directly visible if it is visible, because the parent is in
190 -- an enclosing scope. Iterate over context to find child units of U_Name
191 -- or of some ancestor of it.
193 procedure Install_With_Clause
194 (With_Clause
: Node_Id
;
195 Private_With_OK
: Boolean := False);
196 -- If the unit is not a child unit, make unit immediately visible. The
197 -- caller ensures that the unit is not already currently installed. The
198 -- flag Private_With_OK is set true in Install_Private_With_Clauses, which
199 -- is called when compiling the private part of a package, or installing
200 -- the private declarations of a parent unit.
202 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean;
203 -- When compiling a unit Q descended from some parent unit P, a limited
204 -- with_clause in the context of P that names some other ancestor of Q
205 -- must not be installed because the ancestor is immediately visible.
207 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean;
208 -- Lib_Unit is a library unit which may be a spec or a body. Is_Child_Spec
209 -- returns True if Lib_Unit is a library spec which is a child spec, i.e.
210 -- a library spec that has a parent. If the call to Is_Child_Spec returns
211 -- True, then Parent_Spec (Lib_Unit) is non-Empty and points to the
212 -- compilation unit for the parent spec.
214 -- Lib_Unit can also be a subprogram body that acts as its own spec. If the
215 -- Parent_Spec is non-empty, this is also a child unit.
217 procedure Remove_Context_Clauses
(N
: Node_Id
);
218 -- Subsidiary of previous one. Remove use_ and with_clauses
220 procedure Remove_Limited_With_Clause
(N
: Node_Id
);
221 -- Remove the shadow entities from visibility introduced for a package
222 -- mentioned in limited with clause N. Implements Ada 2005 (AI-50217).
224 procedure Remove_Limited_With_Unit
225 (Pack_Decl
: Node_Id
;
226 Lim_Clause
: Node_Id
:= Empty
);
227 -- Remove the shadow entities from visibility introduced for a package
228 -- denoted by declaration Pack_Decl. Lim_Clause is the related limited
229 -- with clause, if any. Implements Ada 2005 (AI-50217).
231 procedure Remove_Parents
(Lib_Unit
: Node_Id
);
232 -- Remove_Parents checks if Lib_Unit is a child spec. If so then the parent
233 -- contexts established by the corresponding call to Install_Parents are
234 -- removed. Remove_Parents contains a recursive call to itself to ensure
235 -- that all parents are removed in the nested case.
237 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
);
238 -- Reset all visibility flags on unit after compiling it, either as a main
239 -- unit or as a unit in the context.
241 procedure Replace
(Old_E
, New_E
: Entity_Id
);
242 -- Replace Old_E by New_E on visibility list
244 procedure Unchain
(E
: Entity_Id
);
245 -- Remove single entity from visibility list
248 -- A dummy procedure, for debugging use, called just before analyzing the
249 -- main unit (after dealing with any context clauses).
251 --------------------------
252 -- Limited_With_Clauses --
253 --------------------------
255 -- Limited_With clauses are the mechanism chosen for Ada 2005 to support
256 -- mutually recursive types declared in different units. A limited_with
257 -- clause that names package P in the context of unit U makes the types
258 -- declared in the visible part of P available within U, but with the
259 -- restriction that these types can only be used as incomplete types.
260 -- The limited_with clause does not impose a semantic dependence on P,
261 -- and it is possible for two packages to have limited_with_clauses on
262 -- each other without creating an elaboration circularity.
264 -- To support this feature, the analysis of a limited_with clause must
265 -- create an abbreviated view of the package, without performing any
266 -- semantic analysis on it. This "package abstract" contains shadow types
267 -- that are in one-one correspondence with the real types in the package,
268 -- and that have the properties of incomplete types.
270 -- The implementation creates two element lists: one to chain the shadow
271 -- entities, and one to chain the corresponding type entities in the tree
272 -- of the package. Links between corresponding entities in both chains
273 -- allow the compiler to select the proper view of a given type, depending
274 -- on the context. Note that in contrast with the handling of private
275 -- types, the limited view and the nonlimited view of a type are treated
276 -- as separate entities, and no entity exchange needs to take place, which
277 -- makes the implementation much simpler than could be feared.
279 ------------------------------
280 -- Analyze_Compilation_Unit --
281 ------------------------------
283 procedure Analyze_Compilation_Unit
(N
: Node_Id
) is
284 Unit_Node
: constant Node_Id
:= Unit
(N
);
286 procedure Check_Redundant_Withs
287 (Context_Items
: List_Id
;
288 Spec_Context_Items
: List_Id
:= No_List
);
289 -- Determine whether the context list of a compilation unit contains
290 -- redundant with clauses. When checking body clauses against spec
291 -- clauses, set Context_Items to the context list of the body and
292 -- Spec_Context_Items to that of the spec. Parent packages are not
293 -- examined for documentation purposes.
295 function Install_Inherited_Policy_Pragmas
296 (Comp_Unit
: Node_Id
) return Node_Id
;
297 -- Install assertion_policy pragmas placed at the start of the spec of
298 -- the given compilation unit (and the spec of its parent units). Return
299 -- the last pragma found in the check policy list before installing
300 -- these pragmas; used to remove the installed pragmas.
302 procedure Remove_Inherited_Policy_Pragmas
(Last_Pragma
: Node_Id
);
303 -- Remove assertion_policy pragmas installed after the given pragma. If
304 -- Last_Pragma is empty then remove all the pragmas installed in the
305 -- check policy list (if any).
307 ---------------------------
308 -- Check_Redundant_Withs --
309 ---------------------------
311 procedure Check_Redundant_Withs
312 (Context_Items
: List_Id
;
313 Spec_Context_Items
: List_Id
:= No_List
)
317 procedure Process_Body_Clauses
318 (Context_List
: List_Id
;
321 Used_Type_Or_Elab
: out Boolean);
322 -- Examine the context clauses of a package body, trying to match the
323 -- name entity of Clause with any list element. If the match occurs
324 -- on a use package clause set Used to True, for a use type clause or
325 -- pragma Elaborate[_All], set Used_Type_Or_Elab to True.
327 procedure Process_Spec_Clauses
328 (Context_List
: List_Id
;
331 Withed
: out Boolean;
332 Exit_On_Self
: Boolean := False);
333 -- Examine the context clauses of a package spec, trying to match
334 -- the name entity of Clause with any list element. If the match
335 -- occurs on a use package clause, set Used to True, for a with
336 -- package clause other than Clause, set Withed to True. Limited
337 -- with clauses, implicitly generated with clauses and withs
338 -- having pragmas Elaborate or Elaborate_All applied to them are
339 -- skipped. Exit_On_Self is used to control the search loop and
340 -- force an exit whenever Clause sees itself in the search.
342 --------------------------
343 -- Process_Body_Clauses --
344 --------------------------
346 procedure Process_Body_Clauses
347 (Context_List
: List_Id
;
350 Used_Type_Or_Elab
: out Boolean)
352 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
357 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean;
358 -- In an expanded name in a use clause, if the prefix is a renamed
359 -- package, the entity is set to the original package as a result,
360 -- when checking whether the package appears in a previous with
361 -- clause, the renaming has to be taken into account, to prevent
362 -- spurious/incorrect warnings. A common case is use of Text_IO.
368 function Same_Unit
(N
: Node_Id
; P
: Entity_Id
) return Boolean is
370 return Entity
(N
) = P
371 or else (Present
(Renamed_Entity
(P
))
372 and then Entity
(N
) = Renamed_Entity
(P
));
375 -- Start of processing for Process_Body_Clauses
379 Used_Type_Or_Elab
:= False;
381 Cont_Item
:= First
(Context_List
);
382 while Present
(Cont_Item
) loop
384 -- Package use clause
386 if Nkind
(Cont_Item
) = N_Use_Package_Clause
389 -- Search through use clauses
391 Use_Item
:= Name
(Cont_Item
);
393 -- Case of a direct use of the one we are looking for
395 if Entity
(Use_Item
) = Nam_Ent
then
398 -- Handle nested case, as in "with P; use P.Q.R"
405 -- Loop through prefixes looking for match
408 while Nkind
(UE
) = N_Expanded_Name
loop
409 if Same_Unit
(Prefix
(UE
), Nam_Ent
) then
421 elsif Nkind
(Cont_Item
) = N_Use_Type_Clause
422 and then not Used_Type_Or_Elab
428 -- Loop through prefixes looking for a match
430 UE
:= Prefix
(Subtype_Mark
(Cont_Item
));
432 if not Used_Type_Or_Elab
433 and then Same_Unit
(UE
, Nam_Ent
)
435 Used_Type_Or_Elab
:= True;
438 exit when Nkind
(UE
) /= N_Expanded_Name
;
443 -- Pragma Elaborate or Elaborate_All
445 elsif Nkind
(Cont_Item
) = N_Pragma
447 Pragma_Name_Unmapped
(Cont_Item
)
448 in Name_Elaborate | Name_Elaborate_All
449 and then not Used_Type_Or_Elab
452 First
(Pragma_Argument_Associations
(Cont_Item
));
453 while Present
(Prag_Unit
) and then not Used_Type_Or_Elab
loop
454 if Entity
(Expression
(Prag_Unit
)) = Nam_Ent
then
455 Used_Type_Or_Elab
:= True;
464 end Process_Body_Clauses
;
466 --------------------------
467 -- Process_Spec_Clauses --
468 --------------------------
470 procedure Process_Spec_Clauses
471 (Context_List
: List_Id
;
474 Withed
: out Boolean;
475 Exit_On_Self
: Boolean := False)
477 Nam_Ent
: constant Entity_Id
:= Entity
(Name
(Clause
));
484 Cont_Item
:= First
(Context_List
);
485 while Present
(Cont_Item
) loop
487 -- Stop the search since the context items after Cont_Item have
488 -- already been examined in a previous iteration of the reverse
489 -- loop in Check_Redundant_Withs.
492 and Cont_Item
= Clause
497 -- Package use clause
499 if Nkind
(Cont_Item
) = N_Use_Package_Clause
502 if Entity
(Name
(Cont_Item
)) = Nam_Ent
then
506 -- Package with clause. Avoid processing self, implicitly
507 -- generated with clauses or limited with clauses. Note that
508 -- we examine with clauses having pragmas Elaborate or
509 -- Elaborate_All applied to them due to cases such as:
513 -- pragma Elaborate (Pack);
515 -- In this case, the second with clause is redundant since
516 -- the pragma applies only to the first "with Pack;".
518 -- Note that we only consider with_clauses that comes from
519 -- source. In the case of renamings used as prefixes of names
520 -- in with_clauses, we generate a with_clause for the prefix,
521 -- which we do not treat as implicit because it is needed for
522 -- visibility analysis, but is also not redundant.
524 elsif Nkind
(Cont_Item
) = N_With_Clause
525 and then Comes_From_Source
(Cont_Item
)
526 and then not Implicit_With
(Cont_Item
)
527 and then not Limited_Present
(Cont_Item
)
528 and then Cont_Item
/= Clause
529 and then Entity
(Name
(Cont_Item
)) = Nam_Ent
536 end Process_Spec_Clauses
;
538 -- Start of processing for Check_Redundant_Withs
541 Clause
:= Last
(Context_Items
);
542 while Present
(Clause
) loop
544 -- Avoid checking implicitly generated with clauses, limited with
545 -- clauses or withs that have pragma Elaborate or Elaborate_All.
547 if Nkind
(Clause
) = N_With_Clause
548 and then not Implicit_With
(Clause
)
549 and then not Limited_Present
(Clause
)
550 and then not Elaborate_Present
(Clause
)
552 -- With_clauses introduced for renamings of parent clauses
553 -- are not marked implicit because they need to be properly
554 -- installed, but they do not come from source and do not
557 and then Comes_From_Source
(Clause
)
559 -- Package body-to-spec check
561 if Present
(Spec_Context_Items
) then
563 Used_In_Body
: Boolean;
564 Used_In_Spec
: Boolean;
565 Used_Type_Or_Elab
: Boolean;
566 Withed_In_Spec
: Boolean;
570 (Context_List
=> Spec_Context_Items
,
572 Used
=> Used_In_Spec
,
573 Withed
=> Withed_In_Spec
);
576 (Context_List
=> Context_Items
,
578 Used
=> Used_In_Body
,
579 Used_Type_Or_Elab
=> Used_Type_Or_Elab
);
581 -- "Type Elab" refers to the presence of either a use
582 -- type clause, pragmas Elaborate or Elaborate_All.
584 -- +---------------+---------------------------+------+
585 -- | Spec | Body | Warn |
586 -- +--------+------+--------+------+-----------+------+
587 -- | Withed | Used | Withed | Used | Type Elab | |
588 -- | X | | X | | | X |
589 -- | X | | X | X | | |
590 -- | X | | X | | X | |
591 -- | X | | X | X | X | |
592 -- | X | X | X | | | X |
593 -- | X | X | X | | X | |
594 -- | X | X | X | X | | X |
595 -- | X | X | X | X | X | |
596 -- +--------+------+--------+------+-----------+------+
599 and then not Used_Type_Or_Elab
)
601 ((not Used_In_Spec
and then not Used_In_Body
)
602 or else Used_In_Spec
)
604 Error_Msg_N
-- CODEFIX
605 ("redundant with clause in body?r?", Clause
);
609 -- Standalone package spec or body check
612 if Is_Ancestor_Package
(Entity
(Name
(Clause
)),
613 Defining_Entity
(Unit_Node
))
616 ("unnecessary with of ancestor?r?", Clause
);
620 Dummy
: Boolean := False;
621 Withed
: Boolean := False;
624 -- The mechanism for examining the context clauses of a
625 -- package spec can be applied to package body clauses.
628 (Context_List
=> Context_Items
,
632 Exit_On_Self
=> True);
635 Error_Msg_N
-- CODEFIX
636 ("redundant with clause?r?", Clause
);
644 end Check_Redundant_Withs
;
646 --------------------------------------
647 -- Install_Inherited_Policy_Pragmas --
648 --------------------------------------
650 -- Opt.Check_Policy_List is handled as a stack; assertion policy
651 -- pragmas defined at inner scopes are placed at the beginning of
652 -- the list. Therefore, policy pragmas defined at the start of
653 -- parent units must be appended to the end of this list.
655 -- When the compilation unit is a package body (or a subprogram body
656 -- that does not act as its spec) we recursively traverse to its spec
657 -- (and from there to its ultimate parent); when the compilation unit
658 -- is a child package (or subprogram) spec we recursively climb until
659 -- its ultimate parent. In both cases policy pragmas defined at the
660 -- beginning of all these traversed units are appended to the check
661 -- policy list in the way back to the current compilation unit (and
662 -- they are left installed in reverse order). For example:
664 -- pragma Assertion_Policy (...) -- [policy-1]
665 -- package Pkg is ...
667 -- pragma Assertion_Policy (...) -- [policy-2]
668 -- package Pkg.Child is ...
670 -- pragma Assertion_Policy (...) -- [policy-3]
671 -- package body Pkg.Child is ...
673 -- When the compilation unit Pkg.Child is analyzed, and its context
674 -- clauses are analyzed, these are the contents of Check_Policy_List:
676 -- Opt.Check_Policy_List -> [policy-3]
678 -- last_policy_pragma
680 -- After climbing to the ultimate parent spec, these are the contents
681 -- of Check_Policy_List:
683 -- Opt.Check_Policy_List -> [policy-3] -> [policy-2] -> [policy-1]
685 -- last_policy_pragma
687 -- The reference to the last policy pragma in the initial contents of
688 -- the list is used later to remove installed inherited pragmas.
690 function Install_Inherited_Policy_Pragmas
691 (Comp_Unit
: Node_Id
) return Node_Id
693 Last_Policy_Pragma
: Node_Id
;
695 procedure Install_Parent_Policy_Pragmas
(N
: Node_Id
);
696 -- Recursively climb to the ultimate parent and install their policy
697 -- pragmas after Last_Policy_Pragma.
699 -----------------------------------
700 -- Install_Parent_Policy_Pragmas --
701 -----------------------------------
703 procedure Install_Parent_Policy_Pragmas
(N
: Node_Id
) is
704 Lib_Unit
: constant Node_Id
:= Unit
(N
);
708 if Is_Child_Spec
(Lib_Unit
) then
709 Install_Parent_Policy_Pragmas
(Parent_Spec
(Lib_Unit
));
711 elsif Nkind
(Lib_Unit
) = N_Package_Body
then
712 Install_Parent_Policy_Pragmas
(Library_Unit
(N
));
714 elsif Nkind
(Lib_Unit
) = N_Subprogram_Body
715 and then not Acts_As_Spec
(N
)
717 Install_Parent_Policy_Pragmas
(Library_Unit
(N
));
720 -- Search for check policy pragmas defined at the start of the
721 -- context items. They are not part of the context clause, but
722 -- that is where the parser places them.
724 Item
:= First
(Context_Items
(N
));
726 and then Nkind
(Item
) = N_Pragma
727 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
729 if Pragma_Name
(Item
) = Name_Check_Policy
then
730 if No
(Last_Policy_Pragma
) then
731 Set_Next_Pragma
(Item
, Opt
.Check_Policy_List
);
732 Opt
.Check_Policy_List
:= Item
;
735 Set_Next_Pragma
(Item
, Next_Pragma
(Last_Policy_Pragma
));
736 Set_Next_Pragma
(Last_Policy_Pragma
, Item
);
742 end Install_Parent_Policy_Pragmas
;
746 Lib_Unit
: constant Node_Id
:= Unit
(Comp_Unit
);
748 -- Start of processing for Install_Inherited_Policy_Pragmas
751 -- Search for the last configuration pragma of the current
752 -- compilation unit in the check policy list. These pragmas were
753 -- added to the ckeck policy list as part of the analysis of the
754 -- context of the current compilation unit (because, although
755 -- configuration pragmas are not part of the context clauses,
756 -- they are placed there by the parser).
758 Last_Policy_Pragma
:= Opt
.Check_Policy_List
;
760 if Present
(Last_Policy_Pragma
) then
761 while Present
(Next_Pragma
(Last_Policy_Pragma
)) loop
762 Last_Policy_Pragma
:= Next_Pragma
(Last_Policy_Pragma
);
766 -- We must not install configuration pragmas of the current unit
767 -- because they have been installed by Analyze_Context (see previous
770 if Is_Child_Spec
(Lib_Unit
) then
771 Install_Parent_Policy_Pragmas
(Parent_Spec
(Lib_Unit
));
773 elsif Nkind
(Lib_Unit
) = N_Package_Body
then
774 Install_Parent_Policy_Pragmas
(Library_Unit
(Comp_Unit
));
776 elsif Nkind
(Lib_Unit
) = N_Subprogram_Body
777 and then not Acts_As_Spec
(Comp_Unit
)
779 Install_Parent_Policy_Pragmas
(Library_Unit
(Comp_Unit
));
782 return Last_Policy_Pragma
;
783 end Install_Inherited_Policy_Pragmas
;
785 -------------------------------------
786 -- Remove_Inherited_Policy_Pragmas --
787 -------------------------------------
789 procedure Remove_Inherited_Policy_Pragmas
(Last_Pragma
: Node_Id
) is
794 if No
(Opt
.Check_Policy_List
) then
798 -- If this unit does not have assertion_policy pragmas, then all the
799 -- pragmas installed in the check policy list were inherited and must
800 -- be removed from the list.
802 if No
(Last_Pragma
) then
803 Curr_Prag
:= Opt
.Check_Policy_List
;
805 -- Otherwise, pragmas installed after Last_Pragma must be removed.
808 Curr_Prag
:= Last_Pragma
;
811 -- Remove pragmas from the list
813 Next_Prag
:= Next_Pragma
(Curr_Prag
);
814 while Present
(Next_Prag
) loop
815 Set_Next_Pragma
(Curr_Prag
, Empty
);
817 Curr_Prag
:= Next_Prag
;
818 Next_Prag
:= Next_Pragma
(Curr_Prag
);
821 if No
(Last_Pragma
) then
822 Opt
.Check_Policy_List
:= Empty
;
824 end Remove_Inherited_Policy_Pragmas
;
828 Main_Cunit
: constant Node_Id
:= Cunit
(Main_Unit
);
829 Lib_Unit
: Node_Id
:= Library_Unit
(N
);
830 Par_Spec_Name
: Unit_Name_Type
;
832 Unum
: Unit_Number_Type
;
833 Options
: Style_Check_Options
;
835 Last_Policy_Pragma
: Node_Id
;
836 -- Last policy pragma of this compilation unit installed in the check
837 -- policy list when its context is analyzed (see Analyze_Context); this
838 -- node is used as a reference to remove from this list policy pragmas
839 -- inherited from parent units.
841 -- Start of processing for Analyze_Compilation_Unit
844 Exp_Put_Image
.Preload_Root_Buffer_Type
(N
);
846 Process_Compilation_Unit_Pragmas
(N
);
848 -- If the unit is a subunit whose parent has not been analyzed (which
849 -- indicates that the main unit is a subunit, either the current one or
850 -- one of its descendants) then the subunit is compiled as part of the
851 -- analysis of the parent, which we proceed to do. Basically this gets
852 -- handled from the top down and we don't want to do anything at this
853 -- level (i.e. this subunit will be handled on the way down from the
854 -- parent), so at this level we immediately return. If the subunit ends
855 -- up not analyzed, it means that the parent did not contain a stub for
856 -- it, or that there errors were detected in some ancestor.
858 if Nkind
(Unit_Node
) = N_Subunit
and then not Analyzed
(Lib_Unit
) then
859 Semantics
(Lib_Unit
);
861 if not Analyzed
(Proper_Body
(Unit_Node
)) then
862 if Serious_Errors_Detected
> 0 then
863 Error_Msg_N
("subunit not analyzed (errors in parent unit)", N
);
865 Error_Msg_N
("missing stub for subunit", N
);
872 -- Analyze context (this will call Sem recursively for with'ed units) To
873 -- detect circularities among with-clauses that are not caught during
874 -- loading, we set the Context_Pending flag on the current unit. If the
875 -- flag is already set there is a potential circularity. We exclude
876 -- predefined units from this check because they are known to be safe.
877 -- We also exclude package bodies that are present because circularities
878 -- between bodies are harmless (and necessary).
880 if Context_Pending
(N
) then
882 Circularity
: Boolean := True;
885 if In_Predefined_Unit
(N
) then
886 Circularity
:= False;
889 for U
in Main_Unit
+ 1 .. Last_Unit
loop
890 if Nkind
(Unit
(Cunit
(U
))) = N_Package_Body
891 and then not Analyzed
(Cunit
(U
))
893 Circularity
:= False;
900 Error_Msg_N
("circular dependency caused by with_clauses", N
);
902 ("\possibly missing limited_with clause"
903 & " in one of the following", N
);
905 for U
in Main_Unit
.. Last_Unit
loop
906 if Context_Pending
(Cunit
(U
)) then
907 Error_Msg_Unit_1
:= Get_Unit_Name
(Unit
(Cunit
(U
)));
908 Error_Msg_N
("\unit$", N
);
912 raise Unrecoverable_Error
;
916 Set_Context_Pending
(N
);
919 -- Store the style check options before analyzing context pragmas that
920 -- might change them for this compilation unit.
922 Save_Style_Check_Options
(Options
);
926 Set_Context_Pending
(N
, False);
928 -- If the unit is a package body, the spec is already loaded and must be
929 -- analyzed first, before we analyze the body.
931 if Nkind
(Unit_Node
) = N_Package_Body
then
933 -- If no Lib_Unit, then there was a serious previous error, so just
934 -- ignore the entire analysis effort.
936 if No
(Lib_Unit
) then
937 Check_Error_Detected
;
941 -- Analyze the package spec
943 Semantics
(Lib_Unit
);
945 -- Check for unused with's
947 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
949 -- Verify that the library unit is a package declaration
951 if Nkind
(Unit
(Lib_Unit
)) not in
952 N_Package_Declaration | N_Generic_Package_Declaration
955 ("no legal package declaration for package body", N
);
958 -- Otherwise, the entity in the declaration is visible. Update the
959 -- version to reflect dependence of this body on the spec.
962 Spec_Id
:= Defining_Entity
(Unit
(Lib_Unit
));
963 Set_Is_Immediately_Visible
(Spec_Id
, True);
964 Version_Update
(N
, Lib_Unit
);
966 if Nkind
(Defining_Unit_Name
(Unit_Node
)) =
967 N_Defining_Program_Unit_Name
969 Generate_Parent_References
(Unit_Node
, Scope
(Spec_Id
));
974 -- If the unit is a subprogram body, then we similarly need to analyze
975 -- its spec. However, things are a little simpler in this case, because
976 -- here, this analysis is done mostly for error checking and consistency
977 -- purposes (but not only, e.g. there could be a contract on the spec),
978 -- so there's nothing else to be done.
980 elsif Nkind
(Unit_Node
) = N_Subprogram_Body
then
981 if Acts_As_Spec
(N
) then
983 -- If the subprogram body is a child unit, we must create a
984 -- declaration for it, in order to properly load the parent(s).
985 -- After this, the original unit does not acts as a spec, because
986 -- there is an explicit one. If this unit appears in a context
987 -- clause, then an implicit with on the parent will be added when
988 -- installing the context. If this is the main unit, there is no
989 -- Unit_Table entry for the declaration (it has the unit number
990 -- of the main unit) and code generation is unaffected.
992 Unum
:= Get_Cunit_Unit_Number
(N
);
993 Par_Spec_Name
:= Get_Parent_Spec_Name
(Unit_Name
(Unum
));
995 if Present
(Par_Spec_Name
) then
998 (Load_Name
=> Par_Spec_Name
,
1003 if Unum
/= No_Unit
then
1005 -- Build subprogram declaration and attach parent unit to it
1006 -- This subprogram declaration does not come from source,
1007 -- Nevertheless the backend must generate debugging info for
1008 -- it, and this must be indicated explicitly. We also mark
1009 -- the body entity as a child unit now, to prevent a
1010 -- cascaded error if the spec entity cannot be entered
1011 -- in its scope. Finally we create a Units table entry for
1012 -- the subprogram declaration, to maintain a one-to-one
1013 -- correspondence with compilation unit nodes. This is
1014 -- critical for the tree traversals performed by CodePeer.
1017 Loc
: constant Source_Ptr
:= Sloc
(N
);
1018 SCS
: constant Boolean :=
1019 Get_Comes_From_Source_Default
;
1022 Set_Comes_From_Source_Default
(False);
1024 -- Note: We copy the Context_Items from the explicit body
1025 -- to the implicit spec, setting the former to Empty_List
1026 -- to preserve the treeish nature of the tree, during
1027 -- analysis of the spec. Then we put it back the way it
1028 -- was -- copy the Context_Items from the spec to the
1029 -- body, and set the spec Context_Items to Empty_List.
1030 -- It is necessary to preserve the treeish nature,
1031 -- because otherwise we will call End_Use_* twice on the
1035 Make_Compilation_Unit
(Loc
,
1036 Context_Items
=> Context_Items
(N
),
1038 Make_Subprogram_Declaration
(Sloc
(N
),
1041 (Specification
(Unit_Node
))),
1043 Make_Compilation_Unit_Aux
(Loc
));
1045 Set_Context_Items
(N
, Empty_List
);
1046 Set_Library_Unit
(N
, Lib_Unit
);
1047 Set_Parent_Spec
(Unit
(Lib_Unit
), Cunit
(Unum
));
1048 Make_Child_Decl_Unit
(N
);
1049 Semantics
(Lib_Unit
);
1051 -- Now that a separate declaration exists, the body
1052 -- of the child unit does not act as spec any longer.
1054 Set_Acts_As_Spec
(N
, False);
1055 Move_Aspects
(From
=> Unit_Node
, To
=> Unit
(Lib_Unit
));
1056 Set_Is_Child_Unit
(Defining_Entity
(Unit_Node
));
1057 Set_Debug_Info_Needed
(Defining_Entity
(Unit
(Lib_Unit
)));
1058 Set_Comes_From_Source_Default
(SCS
);
1060 -- Restore Context_Items to the body
1062 Set_Context_Items
(N
, Context_Items
(Lib_Unit
));
1063 Set_Context_Items
(Lib_Unit
, Empty_List
);
1068 -- Here for subprogram with separate declaration
1071 Semantics
(Lib_Unit
);
1072 Check_Unused_Withs
(Get_Cunit_Unit_Number
(Lib_Unit
));
1073 Version_Update
(N
, Lib_Unit
);
1076 -- If this is a child unit, generate references to the parents
1078 if Nkind
(Defining_Unit_Name
(Specification
(Unit_Node
))) =
1079 N_Defining_Program_Unit_Name
1081 Generate_Parent_References
1082 (Specification
(Unit_Node
),
1083 Scope
(Defining_Entity
(Unit
(Lib_Unit
))));
1087 -- If it is a child unit, the parent must be elaborated first and we
1088 -- update version, since we are dependent on our parent.
1090 if Is_Child_Spec
(Unit_Node
) then
1092 -- The analysis of the parent is done with style checks off
1095 Save_Style_Check
: constant Boolean := Style_Check
;
1098 if not GNAT_Mode
then
1099 Style_Check
:= False;
1102 Semantics
(Parent_Spec
(Unit_Node
));
1103 Version_Update
(N
, Parent_Spec
(Unit_Node
));
1105 -- Restore style check settings
1107 Style_Check
:= Save_Style_Check
;
1111 -- With the analysis done, install assertion_policy pragmas defined at
1112 -- the start of the specification of this unit (and recursively the
1113 -- assertion policy pragmas defined at the start of the specification
1114 -- of its parent units); install also the context of this compilation
1115 -- unit. Note that we can't install the context from the with clauses
1116 -- as we analyze them, because each with clause must be analyzed in a
1117 -- clean visibility context, so we have to wait and install them all
1120 Last_Policy_Pragma
:= Install_Inherited_Policy_Pragmas
(N
);
1121 Install_Context
(N
);
1123 if Is_Child_Spec
(Unit_Node
) then
1125 -- Set the entities of all parents in the program_unit_name
1127 Generate_Parent_References
1128 (Unit_Node
, Get_Parent_Entity
(Unit
(Parent_Spec
(Unit_Node
))));
1131 -- All components of the context: with-clauses, library unit, ancestors
1132 -- if any, (and their context) are analyzed and installed.
1134 -- Call special debug routine sm if this is the main unit
1136 if Current_Sem_Unit
= Main_Unit
then
1140 -- Now analyze the unit (package, subprogram spec, body) itself
1142 Analyze
(Unit_Node
);
1144 if Warn_On_Redundant_Constructs
then
1145 Check_Redundant_Withs
(Context_Items
(N
));
1147 if Nkind
(Unit_Node
) = N_Package_Body
then
1148 Check_Redundant_Withs
1149 (Context_Items
=> Context_Items
(N
),
1150 Spec_Context_Items
=> Context_Items
(Lib_Unit
));
1154 -- The above call might have made Unit_Node an N_Subprogram_Body from
1155 -- something else, so propagate any Acts_As_Spec flag.
1157 if Nkind
(Unit_Node
) = N_Subprogram_Body
1158 and then Acts_As_Spec
(Unit_Node
)
1160 Set_Acts_As_Spec
(N
);
1163 -- Register predefined units in Rtsfind
1165 if In_Predefined_Unit
(N
) then
1166 Set_RTU_Loaded
(Unit_Node
);
1169 -- Treat compilation unit pragmas that appear after the library unit
1172 Prag_Node
: Node_Id
:= First
(Pragmas_After
(Aux_Decls_Node
(N
)));
1174 while Present
(Prag_Node
) loop
1175 Analyze
(Prag_Node
);
1180 -- Analyze the contract of a [generic] subprogram that acts as a
1181 -- compilation unit after all compilation pragmas have been analyzed.
1183 if Nkind
(Unit_Node
) in
1184 N_Generic_Subprogram_Declaration | N_Subprogram_Declaration
1186 Analyze_Entry_Or_Subprogram_Contract
(Defining_Entity
(Unit_Node
));
1189 -- Generate distribution stubs if requested and no error
1192 and then (Distribution_Stub_Mode
= Generate_Receiver_Stub_Body
1194 Distribution_Stub_Mode
= Generate_Caller_Stub_Body
)
1195 and then Fatal_Error
(Main_Unit
) /= Error_Detected
1197 if Is_RCI_Pkg_Spec_Or_Body
(N
) then
1199 -- Regular RCI package
1201 Add_Stub_Constructs
(N
);
1203 elsif (Nkind
(Unit_Node
) = N_Package_Declaration
1204 and then Is_Shared_Passive
(Defining_Entity
1205 (Specification
(Unit_Node
))))
1206 or else (Nkind
(Unit_Node
) = N_Package_Body
1208 Is_Shared_Passive
(Corresponding_Spec
(Unit_Node
)))
1210 -- Shared passive package
1212 Add_Stub_Constructs
(N
);
1214 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
1216 Is_Remote_Call_Interface
1217 (Defining_Entity
(Specification
(Instance_Spec
(Unit_Node
))))
1219 -- Instantiation of a RCI generic package
1221 Add_Stub_Constructs
(N
);
1225 -- Build dispatch tables of library-level tagged types only now because
1226 -- the generation of distribution stubs above may create some of them.
1228 if Expander_Active
and then Tagged_Type_Expansion
then
1229 case Nkind
(Unit_Node
) is
1230 when N_Package_Declaration | N_Package_Body
=>
1231 Build_Static_Dispatch_Tables
(Unit_Node
);
1233 when N_Package_Instantiation
=>
1234 Build_Static_Dispatch_Tables
(Instance_Spec
(Unit_Node
));
1241 -- Remove unit from visibility, so that environment is clean for the
1242 -- next compilation, which is either the main unit or some other unit
1245 if Nkind
(Unit_Node
) in N_Package_Declaration
1246 | N_Package_Renaming_Declaration
1247 | N_Subprogram_Declaration
1248 | N_Generic_Declaration
1249 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
1250 and then Acts_As_Spec
(Unit_Node
))
1252 Remove_Unit_From_Visibility
(Defining_Entity
(Unit_Node
));
1254 -- If the unit is an instantiation whose body will be elaborated for
1255 -- inlining purposes, use the proper entity of the instance. The entity
1256 -- may be missing if the instantiation was illegal.
1258 elsif Nkind
(Unit_Node
) = N_Package_Instantiation
1259 and then not Error_Posted
(Unit_Node
)
1260 and then Present
(Instance_Spec
(Unit_Node
))
1262 Remove_Unit_From_Visibility
1263 (Defining_Entity
(Instance_Spec
(Unit_Node
)));
1265 elsif Nkind
(Unit_Node
) = N_Package_Body
1266 or else (Nkind
(Unit_Node
) = N_Subprogram_Body
1267 and then not Acts_As_Spec
(Unit_Node
))
1269 -- Bodies that are not the main unit are compiled if they are generic
1270 -- or contain generic or inlined units. Their analysis brings in the
1271 -- context of the corresponding spec (unit declaration) which must be
1272 -- removed as well, to return the compilation environment to its
1275 Remove_Context
(Lib_Unit
);
1276 Set_Is_Immediately_Visible
(Defining_Entity
(Unit
(Lib_Unit
)), False);
1279 -- Last step is to deinstall the context we just installed as well as
1280 -- the unit just compiled.
1283 Remove_Inherited_Policy_Pragmas
(Last_Policy_Pragma
);
1285 -- When generating code for a non-generic main unit, check that withed
1286 -- generic units have a body if they need it, even if the units have not
1287 -- been instantiated. Force the load of the bodies to produce the proper
1288 -- error if the body is absent. The same applies to GNATprove mode, with
1289 -- the added benefit of capturing global references within the generic.
1290 -- This in turn allows for proper inlining of subprogram bodies without
1291 -- a previous declaration.
1293 if Get_Cunit_Unit_Number
(N
) = Main_Unit
1294 and then ((Operating_Mode
= Generate_Code
and then Expander_Active
)
1296 (Operating_Mode
= Check_Semantics
and then GNATprove_Mode
))
1298 -- Check whether the source for the body of the unit must be included
1299 -- in a standalone library.
1301 Check_Body_Needed_For_SAL
(Cunit_Entity
(Main_Unit
));
1303 -- Indicate that the main unit is now analyzed, to catch possible
1304 -- circularities between it and generic bodies. Remove main unit from
1305 -- visibility. This might seem superfluous, but the main unit must
1306 -- not be visible in the generic body expansions that follow.
1308 Set_Analyzed
(N
, True);
1309 Set_Is_Immediately_Visible
(Cunit_Entity
(Main_Unit
), False);
1314 Un
: Unit_Number_Type
;
1316 Save_Style_Check
: constant Boolean := Style_Check
;
1319 Item
:= First
(Context_Items
(N
));
1320 while Present
(Item
) loop
1322 -- Check for explicit with clause
1324 if Nkind
(Item
) = N_With_Clause
1325 and then not Implicit_With
(Item
)
1327 -- Ada 2005 (AI-50217): Ignore limited-withed units
1329 and then not Limited_Present
(Item
)
1331 Nam
:= Entity
(Name
(Item
));
1333 -- Compile the generic subprogram, unless it is intrinsic or
1334 -- imported so no body is required, or generic package body
1335 -- if the package spec requires a body.
1337 if (Is_Generic_Subprogram
(Nam
)
1338 and then not Is_Intrinsic_Subprogram
(Nam
)
1339 and then not Is_Imported
(Nam
))
1340 or else (Ekind
(Nam
) = E_Generic_Package
1341 and then Unit_Requires_Body
(Nam
))
1343 Style_Check
:= False;
1345 if Present
(Renamed_Entity
(Nam
)) then
1351 (Unit_Declaration_Node
1352 (Renamed_Entity
(Nam
)))),
1361 Get_Body_Name
(Get_Unit_Name
(Item
)),
1368 if Un
= No_Unit
then
1370 ("body of generic unit& not found", Item
, Nam
);
1373 elsif not Analyzed
(Cunit
(Un
))
1374 and then Un
/= Main_Unit
1375 and then Fatal_Error
(Un
) /= Error_Detected
1377 Style_Check
:= False;
1378 Semantics
(Cunit
(Un
));
1386 -- Restore style checks settings
1388 Style_Check
:= Save_Style_Check
;
1391 -- In GNATprove mode, force the loading of an Interrupt_Priority when
1392 -- processing compilation units with potentially "main" subprograms.
1393 -- This is required for the ceiling priority protocol checks, which
1394 -- are triggered by these subprograms.
1397 and then Nkind
(Unit_Node
) in N_Function_Instantiation
1398 | N_Procedure_Instantiation
1405 case Nkind
(Unit_Node
) is
1406 when N_Subprogram_Body
=>
1407 Spec
:= Specification
(Unit_Node
);
1409 when N_Subprogram_Instantiation
=>
1411 Subprogram_Specification
(Entity
(Name
(Unit_Node
)));
1414 raise Program_Error
;
1417 pragma Assert
(Nkind
(Spec
) in N_Subprogram_Specification
);
1419 -- Main subprogram must have no parameters, and if it is a
1420 -- function, it must return an integer.
1422 if No
(Parameter_Specifications
(Spec
))
1423 and then (Nkind
(Spec
) = N_Procedure_Specification
1425 Is_Integer_Type
(Etype
(Result_Definition
(Spec
))))
1427 SPARK_Implicit_Load
(RE_Interrupt_Priority
);
1433 -- Deal with creating elaboration counter if needed. We create an
1434 -- elaboration counter only for units that come from source since
1435 -- units manufactured by the compiler never need elab checks.
1437 if Comes_From_Source
(N
)
1438 and then Nkind
(Unit_Node
) in N_Package_Declaration
1439 | N_Generic_Package_Declaration
1440 | N_Subprogram_Declaration
1441 | N_Generic_Subprogram_Declaration
1444 Loc
: constant Source_Ptr
:= Sloc
(N
);
1445 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(Loc
);
1448 Spec_Id
:= Defining_Entity
(Unit_Node
);
1449 Generate_Definition
(Spec_Id
);
1451 -- See if an elaboration entity is required for possible access
1452 -- before elaboration checking. Note that we must allow for this
1453 -- even if -gnatE is not set, since a client may be compiled in
1454 -- -gnatE mode and reference the entity.
1456 -- These entities are also used by the binder to prevent multiple
1457 -- attempts to execute the elaboration code for the library case
1458 -- where the elaboration routine might otherwise be called more
1461 -- They are also needed to ensure explicit visibility from the
1462 -- binder generated code of all the units involved in a partition
1463 -- when control-flow preservation is requested.
1465 if not Opt
.Suppress_Control_Flow_Optimizations
1467 ( -- Pure units do not need checks
1471 -- Preelaborated units do not need checks
1473 or else Is_Preelaborated
(Spec_Id
)
1475 -- No checks needed if pragma Elaborate_Body present
1477 or else Has_Pragma_Elaborate_Body
(Spec_Id
)
1479 -- No checks needed if unit does not require a body
1481 or else not Unit_Requires_Body
(Spec_Id
)
1483 -- No checks needed for predefined files
1485 or else Is_Predefined_Unit
(Unum
)
1487 -- No checks required if no separate spec
1489 or else Acts_As_Spec
(N
)
1492 -- This is a case where we only need the entity for checking to
1493 -- prevent multiple elaboration checks.
1495 Set_Elaboration_Entity_Required
(Spec_Id
, False);
1497 -- Otherwise the unit requires an elaboration entity because it
1501 Set_Elaboration_Entity_Required
(Spec_Id
);
1504 Build_Elaboration_Entity
(N
, Spec_Id
);
1508 -- Freeze the compilation unit entity. This for sure is needed because
1509 -- of some warnings that can be output (see Freeze_Subprogram), but may
1510 -- in general be required. If freezing actions result, place them in the
1511 -- compilation unit actions list, and analyze them.
1514 L
: constant List_Id
:=
1515 Freeze_Entity
(Cunit_Entity
(Current_Sem_Unit
), N
);
1517 while Is_Non_Empty_List
(L
) loop
1518 Insert_Library_Level_Action
(Remove_Head
(L
));
1524 -- Call Check_Package_Body so that a body containing subprograms with
1525 -- Inline_Always can be made available for front end inlining.
1527 if Nkind
(Unit_Node
) = N_Package_Declaration
1528 and then Get_Cunit_Unit_Number
(N
) /= Main_Unit
1530 -- We don't need to do this if the Expander is not active, since there
1531 -- is no code to inline.
1533 and then Expander_Active
1536 Save_Style_Check
: constant Boolean := Style_Check
;
1537 Save_Warning
: constant Warning_Mode_Type
:= Warning_Mode
;
1538 Options
: Style_Check_Options
;
1541 Save_Style_Check_Options
(Options
);
1542 Reset_Style_Check_Options
;
1543 Opt
.Warning_Mode
:= Suppress
;
1545 Check_Package_Body_For_Inlining
(N
, Defining_Entity
(Unit_Node
));
1547 Reset_Style_Check_Options
;
1548 Set_Style_Check_Options
(Options
);
1549 Style_Check
:= Save_Style_Check
;
1550 Warning_Mode
:= Save_Warning
;
1554 -- If we are generating obsolescent warnings, then here is where we
1555 -- generate them for the with'ed items. The reason for this special
1556 -- processing is that the normal mechanism of generating the warnings
1557 -- for referenced entities does not work for context clause references.
1558 -- That's because when we first analyze the context, it is too early to
1559 -- know if the with'ing unit is itself obsolescent (which suppresses
1563 and then Warn_On_Obsolescent_Feature
1564 and then Nkind
(Unit_Node
) not in N_Generic_Instantiation
1566 -- Push current compilation unit as scope, so that the test for
1567 -- being within an obsolescent unit will work correctly. The check
1568 -- is not performed within an instantiation, because the warning
1569 -- will have been emitted in the corresponding generic unit.
1571 Push_Scope
(Defining_Entity
(Unit_Node
));
1573 -- Loop through context items to deal with with clauses
1581 Item
:= First
(Context_Items
(N
));
1582 while Present
(Item
) loop
1583 if Nkind
(Item
) = N_With_Clause
1585 -- Suppress this check in limited-withed units. Further work
1586 -- needed here if we decide to incorporate this check on
1587 -- limited-withed units.
1589 and then not Limited_Present
(Item
)
1592 Ent
:= Entity
(Nam
);
1594 if Is_Obsolescent
(Ent
) then
1595 Output_Obsolescent_Entity_Warnings
(Nam
, Ent
);
1603 -- Remove temporary install of current unit as scope
1608 -- Finally restore all the original style check options
1610 Set_Style_Check_Options
(Options
);
1612 -- If No_Elaboration_Code_All was encountered, this is where we do the
1613 -- transitive test of with'ed units to make sure they have the aspect.
1614 -- This is delayed till the end of analyzing the compilation unit to
1615 -- ensure that the pragma/aspect, if present, has been analyzed.
1617 Check_No_Elab_Code_All
(N
);
1619 -- If this is a main compilation containing a package declaration that
1620 -- requires no package body, and the profile of some subprogram depends
1621 -- on shadow incomplete entities then perform full analysis of its
1622 -- limited-with units.
1624 Analyze_Required_Limited_With_Units
(N
);
1625 end Analyze_Compilation_Unit
;
1627 ---------------------
1628 -- Analyze_Context --
1629 ---------------------
1631 procedure Analyze_Context
(N
: Node_Id
) is
1632 Ukind
: constant Node_Kind
:= Nkind
(Unit
(N
));
1636 -- First process all configuration pragmas at the start of the context
1637 -- items. Strictly these are not part of the context clause, but that
1638 -- is where the parser puts them. In any case for sure we must analyze
1639 -- these before analyzing the actual context items, since they can have
1640 -- an effect on that analysis (e.g. pragma Ada_2005 may allow a unit to
1641 -- be with'ed as a result of changing categorizations in Ada 2005).
1643 Item
:= First
(Context_Items
(N
));
1644 while Present
(Item
)
1645 and then Nkind
(Item
) = N_Pragma
1646 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
1652 -- This is the point at which we capture the configuration settings
1653 -- for the unit. At the moment only the Optimize_Alignment setting
1654 -- needs to be captured. Probably more later ???
1656 if Optimize_Alignment_Local
then
1657 Set_OA_Setting
(Current_Sem_Unit
, 'L');
1659 Set_OA_Setting
(Current_Sem_Unit
, Optimize_Alignment
);
1662 -- Loop through actual context items. This is done in two passes:
1664 -- a) The first pass analyzes nonlimited with clauses and also any
1665 -- configuration pragmas (we need to get the latter analyzed right
1666 -- away, since they can affect processing of subsequent items).
1668 -- b) The second pass analyzes limited_with clauses (Ada 2005: AI-50217)
1670 while Present
(Item
) loop
1672 -- For with clause, analyze the with clause, and then update the
1673 -- version, since we are dependent on a unit that we with.
1675 if Nkind
(Item
) = N_With_Clause
1676 and then not Limited_Present
(Item
)
1678 -- Skip analyzing with clause if no unit, nothing to do (this
1679 -- happens for a with that references a non-existent unit).
1681 if Present
(Library_Unit
(Item
)) then
1683 -- Skip analyzing with clause if this is a with_clause for
1684 -- the main unit, which happens if a subunit has a useless
1685 -- with_clause on its parent.
1687 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
) then
1690 -- Here for the case of a useless with for the main unit
1693 Set_Entity
(Name
(Item
), Cunit_Entity
(Current_Sem_Unit
));
1697 -- Do version update (skipped for implicit with)
1699 if not Implicit_With
(Item
) then
1700 Version_Update
(N
, Library_Unit
(Item
));
1703 -- Skip pragmas. Configuration pragmas at the start were handled in
1704 -- the loop above, and remaining pragmas are not processed until we
1705 -- actually install the context (see Install_Context). We delay the
1706 -- analysis of these pragmas to make sure that we have installed all
1707 -- the implicit with's on parent units.
1709 -- Skip use clauses at this stage, since we don't want to do any
1710 -- installing of potentially use-visible entities until we
1711 -- actually install the complete context (in Install_Context).
1712 -- Otherwise things can get installed in the wrong context.
1721 -- Second pass: examine all limited_with clauses. All other context
1722 -- items are ignored in this pass.
1724 Item
:= First
(Context_Items
(N
));
1725 while Present
(Item
) loop
1726 if Nkind
(Item
) = N_With_Clause
1727 and then Limited_Present
(Item
)
1729 -- No need to check errors on implicitly generated limited-with
1732 if not Implicit_With
(Item
) then
1734 -- Verify that the illegal contexts given in 10.1.2 (18/2) are
1735 -- properly rejected, including renaming declarations.
1737 if Ukind
not in N_Package_Declaration
1738 | N_Subprogram_Declaration
1739 | N_Generic_Declaration
1740 | N_Generic_Instantiation
1742 Error_Msg_N
("limited with_clause not allowed here", Item
);
1744 -- Check wrong use of a limited with clause applied to the
1745 -- compilation unit containing the limited-with clause.
1747 -- limited with P.Q;
1748 -- package P.Q is ...
1750 elsif Unit
(Library_Unit
(Item
)) = Unit
(N
) then
1751 Error_Msg_N
("wrong use of limited-with clause", Item
);
1753 -- Check wrong use of limited-with clause applied to some
1754 -- immediate ancestor.
1756 elsif Is_Child_Spec
(Unit
(N
)) then
1758 Lib_U
: constant Entity_Id
:= Unit
(Library_Unit
(Item
));
1762 P
:= Parent_Spec
(Unit
(N
));
1764 if Unit
(P
) = Lib_U
then
1766 ("limited with_clause cannot name ancestor",
1771 exit when not Is_Child_Spec
(Unit
(P
));
1772 P
:= Parent_Spec
(Unit
(P
));
1777 -- Check if the limited-withed unit is already visible through
1778 -- some context clause of the current compilation unit or some
1779 -- ancestor of the current compilation unit.
1782 Lim_Unit_Name
: constant Node_Id
:= Name
(Item
);
1783 Comp_Unit
: Node_Id
;
1785 Unit_Name
: Node_Id
;
1790 It
:= First
(Context_Items
(Comp_Unit
));
1791 while Present
(It
) loop
1793 and then Nkind
(It
) = N_With_Clause
1794 and then not Limited_Present
(It
)
1795 and then Nkind
(Unit
(Library_Unit
(It
))) in
1796 N_Package_Declaration |
1797 N_Package_Renaming_Declaration
1799 if Nkind
(Unit
(Library_Unit
(It
))) =
1800 N_Package_Declaration
1802 Unit_Name
:= Name
(It
);
1804 Unit_Name
:= Name
(Unit
(Library_Unit
(It
)));
1807 -- Check if the named package (or some ancestor)
1808 -- leaves visible the full-view of the unit given
1809 -- in the limited-with clause.
1812 if Designate_Same_Unit
(Lim_Unit_Name
,
1815 Error_Msg_Sloc
:= Sloc
(It
);
1817 ("simultaneous visibility of limited and "
1818 & "unlimited views not allowed", Item
);
1820 ("\unlimited view visible through context "
1821 & "clause #", Item
);
1824 elsif Nkind
(Unit_Name
) = N_Identifier
then
1828 Unit_Name
:= Prefix
(Unit_Name
);
1835 exit when not Is_Child_Spec
(Unit
(Comp_Unit
));
1837 Comp_Unit
:= Parent_Spec
(Unit
(Comp_Unit
));
1842 -- Skip analyzing with clause if no unit, see above
1844 if Present
(Library_Unit
(Item
)) then
1848 -- A limited_with does not impose an elaboration order, but there
1849 -- is a semantic dependency for recompilation purposes.
1851 if not Implicit_With
(Item
) then
1852 Version_Update
(N
, Library_Unit
(Item
));
1855 -- Pragmas and use clauses and with clauses other than limited with's
1856 -- are ignored in this pass through the context items.
1864 end Analyze_Context
;
1866 -------------------------------
1867 -- Analyze_Package_Body_Stub --
1868 -------------------------------
1870 procedure Analyze_Package_Body_Stub
(N
: Node_Id
) is
1871 Id
: constant Entity_Id
:= Defining_Entity
(N
);
1873 Opts
: Config_Switches_Type
;
1876 -- The package declaration must be in the current declarative part
1878 Check_Stub_Level
(N
);
1879 Nam
:= Current_Entity_In_Scope
(Id
);
1881 if No
(Nam
) or else not Is_Package_Or_Generic_Package
(Nam
) then
1882 Error_Msg_N
("missing specification for package stub", N
);
1884 elsif Has_Completion
(Nam
)
1885 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(Nam
)))
1887 Error_Msg_N
("duplicate or redundant stub for package", N
);
1890 -- Retain and restore the configuration options of the enclosing
1891 -- context as the proper body may introduce a set of its own.
1893 Opts
:= Save_Config_Switches
;
1895 -- Indicate that the body of the package exists. If we are doing
1896 -- only semantic analysis, the stub stands for the body. If we are
1897 -- generating code, the existence of the body will be confirmed
1898 -- when we load the proper body.
1900 Set_Scope
(Id
, Current_Scope
);
1901 Mutate_Ekind
(Id
, E_Package_Body
);
1902 Set_Etype
(Id
, Standard_Void_Type
);
1904 Analyze_Aspect_Specifications
(N
, Id
);
1906 Set_Has_Completion
(Nam
);
1907 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
1908 Generate_Reference
(Nam
, Id
, 'b');
1909 Analyze_Proper_Body
(N
, Nam
);
1911 Restore_Config_Switches
(Opts
);
1913 end Analyze_Package_Body_Stub
;
1915 -------------------------
1916 -- Analyze_Proper_Body --
1917 -------------------------
1919 procedure Analyze_Proper_Body
(N
: Node_Id
; Nam
: Entity_Id
) is
1920 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
1922 procedure Optional_Subunit
;
1923 -- This procedure is called when the main unit is a stub, or when we
1924 -- are not generating code. In such a case, we analyze the subunit if
1925 -- present, which is user-friendly, but we don't complain if the subunit
1926 -- is missing. In GNATprove_Mode, we issue an error to avoid formal
1927 -- verification of a partial unit.
1929 ----------------------
1930 -- Optional_Subunit --
1931 ----------------------
1933 procedure Optional_Subunit
is
1934 Comp_Unit
: Node_Id
;
1935 Unum
: Unit_Number_Type
;
1938 -- Try to load subunit, but ignore any errors that occur during the
1939 -- loading of the subunit, by using the special feature in Errout to
1940 -- ignore all errors. Note that Fatal_Error will still be set, so we
1941 -- will be able to check for this case below.
1943 if not GNATprove_Mode
then
1944 Ignore_Errors_Enable
:= Ignore_Errors_Enable
+ 1;
1949 (Load_Name
=> Subunit_Name
,
1950 Required
=> GNATprove_Mode
,
1954 if not GNATprove_Mode
then
1955 Ignore_Errors_Enable
:= Ignore_Errors_Enable
- 1;
1958 -- All done if we successfully loaded the subunit
1961 and then (Fatal_Error
(Unum
) /= Error_Detected
1962 or else Try_Semantics
)
1964 Comp_Unit
:= Cunit
(Unum
);
1966 -- If the file was empty or seriously mangled, the unit itself may
1969 if No
(Unit
(Comp_Unit
)) then
1971 ("subunit does not contain expected proper body", N
);
1973 elsif Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
1975 ("expected SEPARATE subunit, found child unit",
1976 Cunit_Entity
(Unum
));
1978 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
1979 Analyze_Subunit
(Comp_Unit
);
1980 Set_Library_Unit
(N
, Comp_Unit
);
1981 Set_Corresponding_Body
(N
, Defining_Entity
(Unit
(Comp_Unit
)));
1984 elsif Unum
= No_Unit
1985 and then Present
(Nam
)
1987 if Is_Protected_Type
(Nam
) then
1988 Set_Corresponding_Body
(Parent
(Nam
), Defining_Identifier
(N
));
1990 Set_Corresponding_Body
(
1991 Unit_Declaration_Node
(Nam
), Defining_Identifier
(N
));
1994 end Optional_Subunit
;
1998 Comp_Unit
: Node_Id
;
1999 Unum
: Unit_Number_Type
;
2001 -- Start of processing for Analyze_Proper_Body
2004 -- If the subunit is already loaded, it means that the main unit is a
2005 -- subunit, and that the current unit is one of its parents which was
2006 -- being analyzed to provide the needed context for the analysis of the
2007 -- subunit. In this case we analyze the subunit and continue with the
2008 -- parent, without looking at subsequent subunits.
2010 if Is_Loaded
(Subunit_Name
) then
2012 -- If the proper body is already linked to the stub node, the stub is
2013 -- in a generic unit and just needs analyzing.
2015 if Present
(Library_Unit
(N
)) then
2016 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
2018 -- If the subunit has severe errors, the spec of the enclosing
2019 -- body may not be available, in which case do not try analysis.
2021 if Serious_Errors_Detected
> 0
2022 and then No
(Library_Unit
(Library_Unit
(N
)))
2027 -- Collect SCO information for loaded subunit if we are in the
2028 -- extended main unit.
2031 and then In_Extended_Main_Source_Unit
2032 (Cunit_Entity
(Current_Sem_Unit
))
2034 SCO_Record_Raw
(Get_Cunit_Unit_Number
(Library_Unit
(N
)));
2037 Analyze_Subunit
(Library_Unit
(N
));
2039 -- Otherwise we must load the subunit and link to it
2042 -- Load the subunit, this must work, since we originally loaded
2043 -- the subunit earlier on. So this will not really load it, just
2044 -- give access to it.
2048 (Load_Name
=> Subunit_Name
,
2053 -- And analyze the subunit in the parent context (note that we
2054 -- do not call Semantics, since that would remove the parent
2055 -- context). Because of this, we have to manually reset the
2056 -- compiler state to Analyzing since it got destroyed by Load.
2058 if Unum
/= No_Unit
then
2059 Compiler_State
:= Analyzing
;
2061 -- Check that the proper body is a subunit and not a child
2062 -- unit. If the unit was previously loaded, the error will
2063 -- have been emitted when copying the generic node, so we
2064 -- just return to avoid cascaded errors.
2066 if Nkind
(Unit
(Cunit
(Unum
))) /= N_Subunit
then
2070 Set_Corresponding_Stub
(Unit
(Cunit
(Unum
)), N
);
2071 Analyze_Subunit
(Cunit
(Unum
));
2072 Set_Library_Unit
(N
, Cunit
(Unum
));
2076 -- If the main unit is a subunit, then we are just performing semantic
2077 -- analysis on that subunit, and any other subunits of any parent unit
2078 -- should be ignored. If the main unit is itself a subunit, another
2079 -- subunit is irrelevant unless it is a subunit of the current one, that
2080 -- is to say appears in the current source tree.
2082 elsif Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
2083 and then Subunit_Name
/= Unit_Name
(Main_Unit
)
2085 -- But before we return, set the flag for unloaded subunits. This
2086 -- will suppress junk warnings of variables in the same declarative
2087 -- part (or a higher level one) that are in danger of looking unused
2088 -- when in fact there might be a declaration in the subunit that we
2089 -- do not intend to load.
2091 Unloaded_Subunits
:= True;
2094 -- If the subunit is not already loaded, and we are generating code,
2095 -- then this is the case where compilation started from the parent, and
2096 -- we are generating code for an entire subunit tree. In that case we
2097 -- definitely need to load the subunit.
2099 -- In order to continue the analysis with the rest of the parent,
2100 -- and other subunits, we load the unit without requiring its
2101 -- presence, and emit a warning if not found, rather than terminating
2102 -- the compilation abruptly, as for other missing file problems.
2104 elsif Original_Operating_Mode
= Generate_Code
then
2106 -- If the proper body is already linked to the stub node, the stub is
2107 -- in a generic unit and just needs analyzing.
2109 -- We update the version. Although we are not strictly technically
2110 -- semantically dependent on the subunit, given our approach of macro
2111 -- substitution of subunits, it makes sense to include it in the
2112 -- version identification.
2114 if Present
(Library_Unit
(N
)) then
2115 Set_Corresponding_Stub
(Unit
(Library_Unit
(N
)), N
);
2116 Analyze_Subunit
(Library_Unit
(N
));
2117 Version_Update
(Cunit
(Main_Unit
), Library_Unit
(N
));
2119 -- Otherwise we must load the subunit and link to it
2122 -- Make sure that, if the subunit is preprocessed and -gnateG is
2123 -- specified, the preprocessed file will be written.
2125 Lib
.Analysing_Subunit_Of_Main
:= True;
2128 (Load_Name
=> Subunit_Name
,
2132 Lib
.Analysing_Subunit_Of_Main
:= False;
2134 -- Give message if we did not get the unit Emit warning even if
2135 -- missing subunit is not within main unit, to simplify debugging.
2137 pragma Assert
(Original_Operating_Mode
= Generate_Code
);
2138 if Unum
= No_Unit
then
2139 Error_Msg_Unit_1
:= Subunit_Name
;
2141 Get_File_Name
(Subunit_Name
, Subunit
=> True);
2143 ("subunit$$ in file{ not found??!!", N
);
2144 Subunits_Missing
:= True;
2147 -- Load_Unit may reset Compiler_State, since it may have been
2148 -- necessary to parse an additional units, so we make sure that
2149 -- we reset it to the Analyzing state.
2151 Compiler_State
:= Analyzing
;
2153 if Unum
/= No_Unit
then
2154 if Debug_Flag_L
then
2155 Write_Str
("*** Loaded subunit from stub. Analyze");
2159 Comp_Unit
:= Cunit
(Unum
);
2161 -- Check for child unit instead of subunit
2163 if Nkind
(Unit
(Comp_Unit
)) /= N_Subunit
then
2165 ("expected SEPARATE subunit, found child unit",
2166 Cunit_Entity
(Unum
));
2168 -- OK, we have a subunit
2171 Set_Corresponding_Stub
(Unit
(Comp_Unit
), N
);
2172 Set_Library_Unit
(N
, Comp_Unit
);
2174 -- We update the version. Although we are not technically
2175 -- semantically dependent on the subunit, given our approach
2176 -- of macro substitution of subunits, it makes sense to
2177 -- include it in the version identification.
2179 Version_Update
(Cunit
(Main_Unit
), Comp_Unit
);
2181 -- Collect SCO information for loaded subunit if we are in
2182 -- the extended main unit.
2185 and then In_Extended_Main_Source_Unit
2186 (Cunit_Entity
(Current_Sem_Unit
))
2188 SCO_Record_Raw
(Unum
);
2191 -- Analyze the unit if semantics active
2193 if Fatal_Error
(Unum
) /= Error_Detected
2194 or else Try_Semantics
2196 Analyze_Subunit
(Comp_Unit
);
2202 -- The remaining case is when the subunit is not already loaded and we
2203 -- are not generating code. In this case we are just performing semantic
2204 -- analysis on the parent, and we are not interested in the subunit. For
2205 -- subprograms, analyze the stub as a body. For other entities the stub
2206 -- has already been marked as completed.
2211 end Analyze_Proper_Body
;
2213 ----------------------------------
2214 -- Analyze_Protected_Body_Stub --
2215 ----------------------------------
2217 procedure Analyze_Protected_Body_Stub
(N
: Node_Id
) is
2218 Id
: constant Entity_Id
:= Defining_Entity
(N
);
2219 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Id
);
2220 Opts
: Config_Switches_Type
;
2223 Check_Stub_Level
(N
);
2225 -- First occurrence of name may have been as an incomplete type
2227 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2228 Nam
:= Full_View
(Nam
);
2231 if No
(Nam
) or else not Is_Protected_Type
(Etype
(Nam
)) then
2232 Error_Msg_N
("missing specification for Protected body", N
);
2235 -- Retain and restore the configuration options of the enclosing
2236 -- context as the proper body may introduce a set of its own.
2238 Opts
:= Save_Config_Switches
;
2240 Set_Scope
(Id
, Current_Scope
);
2241 Mutate_Ekind
(Id
, E_Protected_Body
);
2242 Set_Etype
(Id
, Standard_Void_Type
);
2244 Analyze_Aspect_Specifications
(N
, Id
);
2246 Set_Has_Completion
(Etype
(Nam
));
2247 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
2248 Generate_Reference
(Nam
, Id
, 'b');
2249 Analyze_Proper_Body
(N
, Etype
(Nam
));
2251 Restore_Config_Switches
(Opts
);
2253 end Analyze_Protected_Body_Stub
;
2255 -----------------------------------------
2256 -- Analyze_Required_Limited_With_Units --
2257 -----------------------------------------
2259 procedure Analyze_Required_Limited_With_Units
(N
: Node_Id
) is
2260 Unit_Node
: constant Node_Id
:= Unit
(N
);
2261 Spec_Id
: constant Entity_Id
:= Defining_Entity
(Unit_Node
);
2263 function Depends_On_Limited_Views
(Pkg_Id
: Entity_Id
) return Boolean;
2264 -- Determines whether the given package has some subprogram with a
2265 -- profile that depends on shadow incomplete type entities of a
2266 -- limited-with unit.
2268 function Has_Limited_With_Clauses
return Boolean;
2269 -- Determines whether the compilation unit N has limited-with context
2272 ------------------------------
2273 -- Has_Limited_With_Clauses --
2274 ------------------------------
2276 function Has_Limited_With_Clauses
return Boolean is
2277 Item
: Node_Id
:= First
(Context_Items
(N
));
2280 while Present
(Item
) loop
2281 if Nkind
(Item
) = N_With_Clause
2282 and then Limited_Present
(Item
)
2283 and then not Implicit_With
(Item
)
2292 end Has_Limited_With_Clauses
;
2294 ------------------------------
2295 -- Depends_On_Limited_Views --
2296 ------------------------------
2298 function Depends_On_Limited_Views
(Pkg_Id
: Entity_Id
) return Boolean is
2300 function Has_Limited_View_Types
(Subp
: Entity_Id
) return Boolean;
2301 -- Determines whether the type of some formal of Subp, or its return
2302 -- type, is a shadow incomplete entity of a limited-with unit.
2304 ----------------------------
2305 -- Has_Limited_View_Types --
2306 ----------------------------
2308 function Has_Limited_View_Types
(Subp
: Entity_Id
) return Boolean is
2309 Formal
: Entity_Id
:= First_Formal
(Subp
);
2312 while Present
(Formal
) loop
2313 if From_Limited_With
(Etype
(Formal
))
2314 and then Has_Non_Limited_View
(Etype
(Formal
))
2315 and then Ekind
(Non_Limited_View
(Etype
(Formal
)))
2321 Formal
:= Next_Formal
(Formal
);
2324 if Ekind
(Subp
) = E_Function
2325 and then From_Limited_With
(Etype
(Subp
))
2326 and then Has_Non_Limited_View
(Etype
(Subp
))
2327 and then Ekind
(Non_Limited_View
(Etype
(Subp
)))
2334 end Has_Limited_View_Types
;
2338 E
: Entity_Id
:= First_Entity
(Pkg_Id
);
2341 while Present
(E
) loop
2342 if Is_Subprogram
(E
)
2343 and then Has_Limited_View_Types
(E
)
2347 -- Recursion on nested packages skipping package renamings
2349 elsif Ekind
(E
) = E_Package
2350 and then No
(Renamed_Entity
(E
))
2351 and then Depends_On_Limited_Views
(E
)
2360 end Depends_On_Limited_Views
;
2366 -- Start of processing for Analyze_Required_Limited_With_Units
2369 -- Cases where no action is required
2371 if not Expander_Active
2372 or else Nkind
(Unit_Node
) /= N_Package_Declaration
2373 or else Main_Unit_Entity
/= Spec_Id
2374 or else Is_Generic_Unit
(Spec_Id
)
2375 or else Unit_Requires_Body
(Spec_Id
)
2376 or else not Has_Limited_With_Clauses
2377 or else not Depends_On_Limited_Views
(Spec_Id
)
2382 -- Perform full analyis of limited-with units to provide the backend
2383 -- with the full-view of shadow entities.
2385 Item
:= First
(Context_Items
(N
));
2386 while Present
(Item
) loop
2387 if Nkind
(Item
) = N_With_Clause
2388 and then Limited_Present
(Item
)
2389 and then not Implicit_With
(Item
)
2391 Semantics
(Library_Unit
(Item
));
2396 end Analyze_Required_Limited_With_Units
;
2398 ----------------------------------
2399 -- Analyze_Subprogram_Body_Stub --
2400 ----------------------------------
2402 -- A subprogram body stub can appear with or without a previous spec. If
2403 -- there is one, then the analysis of the body will find it and verify
2404 -- conformance. The formals appearing in the specification of the stub play
2405 -- no role, except for requiring an additional conformance check. If there
2406 -- is no previous subprogram declaration, the stub acts as a spec, and
2407 -- provides the defining entity for the subprogram.
2409 procedure Analyze_Subprogram_Body_Stub
(N
: Node_Id
) is
2411 Opts
: Config_Switches_Type
;
2414 Check_Stub_Level
(N
);
2416 -- Verify that the identifier for the stub is unique within this
2417 -- declarative part.
2419 if Nkind
(Parent
(N
)) in
2420 N_Block_Statement | N_Package_Body | N_Subprogram_Body
2422 Decl
:= First
(Declarations
(Parent
(N
)));
2423 while Present
(Decl
) and then Decl
/= N
loop
2424 if Nkind
(Decl
) = N_Subprogram_Body_Stub
2425 and then Chars
(Defining_Unit_Name
(Specification
(Decl
))) =
2426 Chars
(Defining_Unit_Name
(Specification
(N
)))
2428 Error_Msg_N
("identifier for stub is not unique", N
);
2435 -- Retain and restore the configuration options of the enclosing context
2436 -- as the proper body may introduce a set of its own.
2438 Opts
:= Save_Config_Switches
;
2440 -- Treat stub as a body, which checks conformance if there is a previous
2441 -- declaration, or else introduces entity and its signature.
2443 Analyze_Subprogram_Body
(N
);
2444 Analyze_Proper_Body
(N
, Empty
);
2446 Restore_Config_Switches
(Opts
);
2447 end Analyze_Subprogram_Body_Stub
;
2449 ---------------------
2450 -- Analyze_Subunit --
2451 ---------------------
2453 -- A subunit is compiled either by itself (for semantic checking) or as
2454 -- part of compiling the parent (for code generation). In either case, by
2455 -- the time we actually process the subunit, the parent has already been
2456 -- installed and analyzed. The node N is a compilation unit, whose context
2457 -- needs to be treated here, because we come directly here from the parent
2458 -- without calling Analyze_Compilation_Unit.
2460 -- The compilation context includes the explicit context of the subunit,
2461 -- and the context of the parent, together with the parent itself. In order
2462 -- to compile the current context, we remove the one inherited from the
2463 -- parent, in order to have a clean visibility table. We restore the parent
2464 -- context before analyzing the proper body itself. On exit, we remove only
2465 -- the explicit context of the subunit.
2467 -- WARNING: This routine manages SPARK regions. Return statements must be
2468 -- replaced by gotos which jump to the end of the routine and restore the
2471 procedure Analyze_Subunit
(N
: Node_Id
) is
2472 Lib_Unit
: constant Node_Id
:= Library_Unit
(N
);
2473 Par_Unit
: constant Entity_Id
:= Current_Scope
;
2475 Lib_Spec
: Node_Id
:= Library_Unit
(Lib_Unit
);
2476 Num_Scopes
: Nat
:= 0;
2477 Use_Clauses
: array (1 .. Scope_Stack
.Last
) of Node_Id
;
2478 Enclosing_Child
: Entity_Id
:= Empty
;
2479 Svg
: constant Suppress_Record
:= Scope_Suppress
;
2481 Save_Cunit_Restrictions
: constant Save_Cunit_Boolean_Restrictions
:=
2482 Cunit_Boolean_Restrictions_Save
;
2483 -- Save non-partition wide restrictions before processing the subunit.
2484 -- All subunits are analyzed with config restrictions reset and we need
2485 -- to restore these saved values at the end.
2487 procedure Analyze_Subunit_Context
;
2488 -- Capture names in use clauses of the subunit. This must be done before
2489 -- re-installing parent declarations, because items in the context must
2490 -- not be hidden by declarations local to the parent.
2492 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
);
2493 -- Recursive procedure to restore scope of all ancestors of subunit,
2494 -- from outermost in. If parent is not a subunit, the call to install
2495 -- context installs context of spec and (if parent is a child unit) the
2496 -- context of its parents as well. It is confusing that parents should
2497 -- be treated differently in both cases, but the semantics are just not
2500 procedure Re_Install_Use_Clauses
;
2501 -- As part of the removal of the parent scope, the use clauses are
2502 -- removed, to be reinstalled when the context of the subunit has been
2503 -- analyzed. Use clauses may also have been affected by the analysis of
2504 -- the context of the subunit, so they have to be applied again, to
2505 -- insure that the compilation environment of the rest of the parent
2506 -- unit is identical.
2508 procedure Remove_Scope
;
2509 -- Remove current scope from scope stack, and preserve the list of use
2510 -- clauses in it, to be reinstalled after context is analyzed.
2512 -----------------------------
2513 -- Analyze_Subunit_Context --
2514 -----------------------------
2516 procedure Analyze_Subunit_Context
is
2518 Unit_Name
: Entity_Id
;
2521 Analyze_Context
(N
);
2522 Check_No_Elab_Code_All
(N
);
2524 -- Make withed units immediately visible. If child unit, make the
2525 -- ultimate parent immediately visible.
2527 Item
:= First
(Context_Items
(N
));
2528 while Present
(Item
) loop
2529 if Nkind
(Item
) = N_With_Clause
then
2531 -- Protect frontend against previous errors in context clauses
2533 if Nkind
(Name
(Item
)) /= N_Selected_Component
then
2534 if Error_Posted
(Item
) then
2538 -- If a subunits has serious syntax errors, the context
2539 -- may not have been loaded. Add a harmless unit name to
2540 -- attempt processing.
2542 if Serious_Errors_Detected
> 0
2543 and then No
(Entity
(Name
(Item
)))
2545 Set_Entity
(Name
(Item
), Standard_Standard
);
2548 Unit_Name
:= Entity
(Name
(Item
));
2550 Set_Is_Visible_Lib_Unit
(Unit_Name
);
2551 exit when Scope
(Unit_Name
) = Standard_Standard
;
2552 Unit_Name
:= Scope
(Unit_Name
);
2554 if No
(Unit_Name
) then
2555 Check_Error_Detected
;
2560 if not Is_Immediately_Visible
(Unit_Name
) then
2561 Set_Is_Immediately_Visible
(Unit_Name
);
2562 Set_Context_Installed
(Item
);
2567 elsif Nkind
(Item
) = N_Use_Package_Clause
then
2568 Analyze
(Name
(Item
));
2570 elsif Nkind
(Item
) = N_Use_Type_Clause
then
2571 Analyze
(Subtype_Mark
(Item
));
2577 -- Reset visibility of withed units. They will be made visible again
2578 -- when we install the subunit context.
2580 Item
:= First
(Context_Items
(N
));
2581 while Present
(Item
) loop
2582 if Nkind
(Item
) = N_With_Clause
2584 -- Protect frontend against previous errors in context clauses
2586 and then Nkind
(Name
(Item
)) /= N_Selected_Component
2587 and then not Error_Posted
(Item
)
2589 Unit_Name
:= Entity
(Name
(Item
));
2591 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
2592 exit when Scope
(Unit_Name
) = Standard_Standard
;
2593 Unit_Name
:= Scope
(Unit_Name
);
2596 if Context_Installed
(Item
) then
2597 Set_Is_Immediately_Visible
(Unit_Name
, False);
2598 Set_Context_Installed
(Item
, False);
2604 end Analyze_Subunit_Context
;
2606 ------------------------
2607 -- Re_Install_Parents --
2608 ------------------------
2610 procedure Re_Install_Parents
(L
: Node_Id
; Scop
: Entity_Id
) is
2614 if Nkind
(Unit
(L
)) = N_Subunit
then
2615 Re_Install_Parents
(Library_Unit
(L
), Scope
(Scop
));
2618 Install_Context
(L
, False);
2620 -- If the subunit occurs within a child unit, we must restore the
2621 -- immediate visibility of any siblings that may occur in context.
2622 -- In addition, we must reset the previous visibility of the
2623 -- parent unit which is now on the scope stack. This is because
2624 -- the Previous_Visibility was previously set when removing the
2625 -- context. This is necessary to prevent the parent entity from
2626 -- remaining visible after the subunit is compiled. This only
2627 -- has an effect if a homonym exists in a body to be processed
2628 -- later if inlining is enabled.
2630 if Present
(Enclosing_Child
) then
2631 Install_Siblings
(Enclosing_Child
, L
);
2632 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
2638 if Scop
/= Par_Unit
then
2639 Set_Is_Immediately_Visible
(Scop
);
2642 -- Make entities in scope visible again. For child units, restore
2643 -- visibility only if they are actually in context.
2645 E
:= First_Entity
(Current_Scope
);
2646 while Present
(E
) loop
2647 if not Is_Child_Unit
(E
) or else Is_Visible_Lib_Unit
(E
) then
2648 Set_Is_Immediately_Visible
(E
);
2654 -- A subunit appears within a body, and for a nested subunits all the
2655 -- parents are bodies. Restore full visibility of their private
2658 if Is_Package_Or_Generic_Package
(Scop
) then
2659 Set_In_Package_Body
(Scop
);
2660 Install_Private_Declarations
(Scop
);
2662 end Re_Install_Parents
;
2664 ----------------------------
2665 -- Re_Install_Use_Clauses --
2666 ----------------------------
2668 procedure Re_Install_Use_Clauses
is
2671 for J
in reverse 1 .. Num_Scopes
loop
2672 U
:= Use_Clauses
(J
);
2673 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:= U
;
2674 Install_Use_Clauses
(U
);
2676 end Re_Install_Use_Clauses
;
2682 procedure Remove_Scope
is
2686 Num_Scopes
:= Num_Scopes
+ 1;
2687 Use_Clauses
(Num_Scopes
) :=
2688 Scope_Stack
.Table
(Scope_Stack
.Last
).First_Use_Clause
;
2690 E
:= First_Entity
(Current_Scope
);
2691 while Present
(E
) loop
2692 Set_Is_Immediately_Visible
(E
, False);
2696 if Is_Child_Unit
(Current_Scope
) then
2697 Enclosing_Child
:= Current_Scope
;
2703 Saved_SM
: SPARK_Mode_Type
:= SPARK_Mode
;
2704 Saved_SMP
: Node_Id
:= SPARK_Mode_Pragma
;
2705 -- Save the SPARK mode-related data to restore on exit. Removing
2706 -- enclosing scopes and contexts to provide a clean environment for the
2707 -- context of the subunit will eliminate any previously set SPARK_Mode.
2709 -- Start of processing for Analyze_Subunit
2712 -- For subunit in main extended unit, we reset the configuration values
2713 -- for the non-partition-wide restrictions. For other units reset them.
2715 if In_Extended_Main_Source_Unit
(N
) then
2716 Restore_Config_Cunit_Boolean_Restrictions
;
2718 Reset_Cunit_Boolean_Restrictions
;
2723 Nam
: Node_Id
:= Name
(Unit
(N
));
2726 if Nkind
(Nam
) = N_Selected_Component
then
2727 Nam
:= Selector_Name
(Nam
);
2730 Check_Identifier
(Nam
, Par_Unit
);
2734 if not Is_Empty_List
(Context_Items
(N
)) then
2736 -- Save current use clauses
2739 Remove_Context
(Lib_Unit
);
2741 -- Now remove parents and their context, including enclosing subunits
2742 -- and the outer parent body which is not a subunit.
2744 if Present
(Lib_Spec
) then
2745 Remove_Context
(Lib_Spec
);
2747 while Nkind
(Unit
(Lib_Spec
)) = N_Subunit
loop
2748 Lib_Spec
:= Library_Unit
(Lib_Spec
);
2750 Remove_Context
(Lib_Spec
);
2753 if Nkind
(Unit
(Lib_Unit
)) = N_Subunit
then
2757 if Nkind
(Unit
(Lib_Spec
)) in N_Package_Body | N_Subprogram_Body
2759 Remove_Context
(Library_Unit
(Lib_Spec
));
2763 Set_Is_Immediately_Visible
(Par_Unit
, False);
2765 Analyze_Subunit_Context
;
2767 -- Take into account the effect of any SPARK_Mode configuration
2768 -- pragma, which takes precedence over a different value of
2769 -- SPARK_Mode inherited from the context of the stub.
2771 if SPARK_Mode
/= None
then
2772 Saved_SM
:= SPARK_Mode
;
2773 Saved_SMP
:= SPARK_Mode_Pragma
;
2776 Re_Install_Parents
(Lib_Unit
, Par_Unit
);
2777 Set_Is_Immediately_Visible
(Par_Unit
);
2779 -- If the context includes a child unit of the parent of the subunit,
2780 -- the parent will have been removed from visibility, after compiling
2781 -- that cousin in the context. The visibility of the parent must be
2782 -- restored now. This also applies if the context includes another
2783 -- subunit of the same parent which in turn includes a child unit in
2786 if Is_Package_Or_Generic_Package
(Par_Unit
) then
2787 if not Is_Immediately_Visible
(Par_Unit
)
2788 or else (Present
(First_Entity
(Par_Unit
))
2790 Is_Immediately_Visible
(First_Entity
(Par_Unit
)))
2792 Set_Is_Immediately_Visible
(Par_Unit
);
2793 Install_Visible_Declarations
(Par_Unit
);
2794 Install_Private_Declarations
(Par_Unit
);
2798 Re_Install_Use_Clauses
;
2799 Install_Context
(N
, Chain
=> False);
2801 -- Restore state of suppress flags for current body
2803 Scope_Suppress
:= Svg
;
2805 -- If the subunit is within a child unit, then siblings of any parent
2806 -- unit that appear in the context clause of the subunit must also be
2807 -- made immediately visible.
2809 if Present
(Enclosing_Child
) then
2810 Install_Siblings
(Enclosing_Child
, N
);
2814 Generate_Parent_References
(Unit
(N
), Par_Unit
);
2816 -- Reinstall the SPARK_Mode which was in effect prior to any scope and
2817 -- context manipulations, taking into account a possible SPARK_Mode
2818 -- configuration pragma if present.
2820 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
2822 -- If the subunit is part of a compilation unit which is subject to
2823 -- pragma Elaboration_Checks, set the model specified by the pragma
2824 -- because it applies to all parts of the unit.
2826 Install_Elaboration_Model
(Par_Unit
);
2828 -- The syntax rules require a proper body for a subprogram subunit
2830 if Nkind
(Proper_Body
(Sinfo
.Nodes
.Unit
(N
))) = N_Subprogram_Declaration
2832 if Null_Present
(Specification
(Proper_Body
(Sinfo
.Nodes
.Unit
(N
))))
2835 ("null procedure not allowed as subunit",
2836 Proper_Body
(Unit
(N
)));
2839 ("subprogram declaration not allowed as subunit",
2840 Defining_Unit_Name
(Specification
(Proper_Body
(Unit
(N
)))));
2844 Analyze
(Proper_Body
(Unit
(N
)));
2847 -- The subunit may contain a with_clause on a sibling of some ancestor.
2848 -- Removing the context will remove from visibility those ancestor child
2849 -- units, which must be restored to the visibility they have in the
2852 if Present
(Enclosing_Child
) then
2857 while Present
(C
) and then C
/= Standard_Standard
loop
2858 Set_Is_Immediately_Visible
(C
);
2859 Set_Is_Visible_Lib_Unit
(C
);
2865 -- Deal with restore of restrictions
2867 Cunit_Boolean_Restrictions_Restore
(Save_Cunit_Restrictions
);
2868 end Analyze_Subunit
;
2870 ----------------------------
2871 -- Analyze_Task_Body_Stub --
2872 ----------------------------
2874 procedure Analyze_Task_Body_Stub
(N
: Node_Id
) is
2875 Id
: constant Entity_Id
:= Defining_Entity
(N
);
2876 Loc
: constant Source_Ptr
:= Sloc
(N
);
2877 Nam
: Entity_Id
:= Current_Entity_In_Scope
(Id
);
2880 Check_Stub_Level
(N
);
2882 -- First occurrence of name may have been as an incomplete type
2884 if Present
(Nam
) and then Ekind
(Nam
) = E_Incomplete_Type
then
2885 Nam
:= Full_View
(Nam
);
2888 if No
(Nam
) or else not Is_Task_Type
(Etype
(Nam
)) then
2889 Error_Msg_N
("missing specification for task body", N
);
2892 Set_Scope
(Id
, Current_Scope
);
2893 Mutate_Ekind
(Id
, E_Task_Body
);
2894 Set_Etype
(Id
, Standard_Void_Type
);
2896 Analyze_Aspect_Specifications
(N
, Id
);
2898 Generate_Reference
(Nam
, Id
, 'b');
2899 Set_Corresponding_Spec_Of_Stub
(N
, Nam
);
2901 -- Check for duplicate stub, if so give message and terminate
2903 if Has_Completion
(Etype
(Nam
)) then
2904 Error_Msg_N
("duplicate stub for task", N
);
2907 Set_Has_Completion
(Etype
(Nam
));
2910 Analyze_Proper_Body
(N
, Etype
(Nam
));
2912 -- Set elaboration flag to indicate that entity is callable. This
2913 -- cannot be done in the expansion of the body itself, because the
2914 -- proper body is not in a declarative part. This is only done if
2915 -- expansion is active, because the context may be generic and the
2916 -- flag not defined yet.
2918 if Expander_Active
then
2920 Make_Assignment_Statement
(Loc
,
2922 Make_Identifier
(Loc
,
2923 Chars
=> New_External_Name
(Chars
(Etype
(Nam
)), 'E')),
2924 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)));
2927 end Analyze_Task_Body_Stub
;
2929 -------------------------
2930 -- Analyze_With_Clause --
2931 -------------------------
2933 -- Analyze the declaration of a unit in a with clause. At end, label the
2934 -- with clause with the defining entity for the unit.
2936 procedure Analyze_With_Clause
(N
: Node_Id
) is
2938 -- Retrieve the original kind of the unit node, before analysis. If it
2939 -- is a subprogram instantiation, its analysis below will rewrite the
2940 -- node as the declaration of the wrapper package. If the same
2941 -- instantiation appears indirectly elsewhere in the context, it will
2942 -- have been analyzed already.
2944 Unit_Kind
: constant Node_Kind
:=
2945 Nkind
(Original_Node
(Unit
(Library_Unit
(N
))));
2946 Nam
: constant Node_Id
:= Name
(N
);
2948 Par_Name
: Entity_Id
;
2953 -- Set True if the unit currently being compiled is an internal unit
2955 Restriction_Violation
: Boolean := False;
2956 -- Set True if a with violates a restriction, no point in giving any
2957 -- warnings if we have this definite error.
2959 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
2962 U
:= Unit
(Library_Unit
(N
));
2964 -- If this is an internal unit which is a renaming, then this is a
2965 -- violation of No_Obsolescent_Features.
2967 -- Note: this is not quite right if the user defines one of these units
2968 -- himself, but that's a marginal case, and fixing it is hard ???
2970 if Ada_Version
>= Ada_95
2971 and then In_Predefined_Renaming
(U
)
2973 if Restriction_Check_Required
(No_Obsolescent_Features
) then
2974 Check_Restriction
(No_Obsolescent_Features
, N
);
2975 Restriction_Violation
:= True;
2978 if Warn_On_Obsolescent_Feature
then
2980 ("renamed predefined unit is an obsolescent feature "
2981 & "(RM J.1)?j?", N
);
2985 -- Check No_Implementation_Units violation
2987 if Restriction_Check_Required
(No_Implementation_Units
) then
2988 if Not_Impl_Defined_Unit
(Get_Source_Unit
(U
)) then
2991 Check_Restriction
(No_Implementation_Units
, Nam
);
2992 Restriction_Violation
:= True;
2996 -- Several actions are skipped for dummy packages (those supplied for
2997 -- with's where no matching file could be found). Such packages are
2998 -- identified by the Sloc value being set to No_Location.
3000 if Limited_Present
(N
) then
3002 -- Ada 2005 (AI-50217): Build visibility structures but do not
3003 -- analyze the unit.
3005 -- If the designated unit is a predefined unit, which might be used
3006 -- implicitly through the rtsfind machinery, a limited with clause
3007 -- on such a unit is usually pointless, because run-time units are
3008 -- unlikely to appear in mutually dependent units, and because this
3009 -- disables the rtsfind mechanism. We transform such limited with
3010 -- clauses into regular with clauses.
3012 if Sloc
(U
) /= No_Location
then
3013 if In_Predefined_Unit
(U
) then
3014 Set_Limited_Present
(N
, False);
3015 Analyze_With_Clause
(N
);
3017 Build_Limited_Views
(N
);
3024 -- If we are compiling under "don't quit" mode (-gnatq) and we have
3025 -- already detected serious errors then we mark the with-clause nodes as
3026 -- analyzed before the corresponding compilation unit is analyzed. This
3027 -- is done here to protect the frontend against never ending recursion
3028 -- caused by circularities in the sources (because the previous errors
3029 -- may break the regular machine of the compiler implemented in
3030 -- Load_Unit to detect circularities).
3032 if Serious_Errors_Detected
> 0 and then Try_Semantics
then
3036 Semantics
(Library_Unit
(N
));
3038 Intunit
:= Is_Internal_Unit
(Current_Sem_Unit
);
3040 if Sloc
(U
) /= No_Location
then
3042 -- Check restrictions, except that we skip the check if this is an
3043 -- internal unit unless we are compiling the internal unit as the
3044 -- main unit. We also skip this for dummy packages.
3046 Check_Restriction_No_Dependence
(Nam
, N
);
3048 if not Intunit
or else Current_Sem_Unit
= Main_Unit
then
3049 Check_Restricted_Unit
(Unit_Name
(Get_Source_Unit
(U
)), N
);
3052 -- Deal with special case of GNAT.Current_Exceptions which interacts
3053 -- with the optimization of local raise statements into gotos.
3055 if Nkind
(Nam
) = N_Selected_Component
3056 and then Nkind
(Prefix
(Nam
)) = N_Identifier
3057 and then Chars
(Prefix
(Nam
)) = Name_Gnat
3058 and then Chars
(Selector_Name
(Nam
))
3059 in Name_Most_Recent_Exception | Name_Exception_Traces
3061 Check_Restriction
(No_Exception_Propagation
, N
);
3062 Special_Exception_Package_Used
:= True;
3065 -- Check for inappropriate with of internal implementation unit if we
3066 -- are not compiling an internal unit and also check for withing unit
3067 -- in wrong version of Ada. Do not issue these messages for implicit
3068 -- with's generated by the compiler itself.
3070 if Implementation_Unit_Warnings
3071 and then not Intunit
3072 and then not Implicit_With
(N
)
3073 and then not Restriction_Violation
3075 case Get_Kind_Of_Unit
(Get_Source_Unit
(U
)) is
3076 when Implementation_Unit
=>
3077 Error_Msg_F
("& is an internal 'G'N'A'T unit?i?", Name
(N
));
3079 -- Add alternative name if available, otherwise issue a
3080 -- general warning message.
3082 if Error_Msg_Strlen
/= 0 then
3083 Error_Msg_F
("\use ""~"" instead?i?", Name
(N
));
3086 ("\use of this unit is non-portable and "
3087 & "version-dependent?i?", Name
(N
));
3090 when Not_Predefined_Unit | Ada_95_Unit
=>
3091 null; -- no checks needed
3093 when Ada_2005_Unit
=>
3094 if Ada_Version
< Ada_2005
3095 and then Warn_On_Ada_2005_Compatibility
3097 Error_Msg_N
("& is an Ada 2005 unit?i?", Name
(N
));
3100 when Ada_2012_Unit
=>
3101 if Ada_Version
< Ada_2012
3102 and then Warn_On_Ada_2012_Compatibility
3104 Error_Msg_N
("& is an Ada 2012 unit?i?", Name
(N
));
3107 when Ada_2022_Unit
=>
3108 if Ada_Version
< Ada_2022
3109 and then Warn_On_Ada_2022_Compatibility
3111 Error_Msg_N
("& is an Ada 2022 unit?i?", Name
(N
));
3117 -- Semantic analysis of a generic unit is performed on a copy of
3118 -- the original tree. Retrieve the entity on which semantic info
3119 -- actually appears.
3121 if Unit_Kind
in N_Generic_Declaration
then
3122 E_Name
:= Defining_Entity
(U
);
3124 -- Note: in the following test, Unit_Kind is the original Nkind, but in
3125 -- the case of an instantiation, semantic analysis above will have
3126 -- replaced the unit by its instantiated version. If the instance body
3127 -- has been generated, the instance now denotes the body entity. For
3128 -- visibility purposes we need the entity of its spec.
3130 elsif (Unit_Kind
= N_Package_Instantiation
3131 or else Nkind
(Original_Node
(Unit
(Library_Unit
(N
)))) =
3132 N_Package_Instantiation
)
3133 and then Nkind
(U
) = N_Package_Body
3135 E_Name
:= Corresponding_Spec
(U
);
3137 elsif Unit_Kind
= N_Package_Instantiation
3138 and then Nkind
(U
) = N_Package_Instantiation
3139 and then Present
(Instance_Spec
(U
))
3141 -- If the instance has not been rewritten as a package declaration,
3142 -- then it appeared already in a previous with clause. Retrieve
3143 -- the entity from the previous instance.
3145 E_Name
:= Defining_Entity
(Specification
(Instance_Spec
(U
)));
3147 elsif Unit_Kind
in N_Subprogram_Instantiation
then
3149 -- The visible subprogram is created during instantiation, and is
3150 -- an attribute of the wrapper package. We retrieve the wrapper
3151 -- package directly from the instantiation node. If the instance
3152 -- is inlined the unit is still an instantiation. Otherwise it has
3153 -- been rewritten as the declaration of the wrapper itself.
3155 if Nkind
(U
) in N_Subprogram_Instantiation
then
3158 (Defining_Entity
(Specification
(Instance_Spec
(U
))));
3160 E_Name
:= Related_Instance
(Defining_Entity
(U
));
3163 elsif Unit_Kind
= N_Package_Renaming_Declaration
3164 or else Unit_Kind
in N_Generic_Renaming_Declaration
3166 E_Name
:= Defining_Entity
(U
);
3168 elsif Unit_Kind
= N_Subprogram_Body
3169 and then Nkind
(Name
(N
)) = N_Selected_Component
3170 and then not Acts_As_Spec
(Library_Unit
(N
))
3172 -- For a child unit that has no spec, one has been created and
3173 -- analyzed. The entity required is that of the spec.
3175 E_Name
:= Corresponding_Spec
(U
);
3178 E_Name
:= Defining_Entity
(U
);
3181 if Nkind
(Name
(N
)) = N_Selected_Component
then
3183 -- Child unit in a with clause
3185 Change_Selected_Component_To_Expanded_Name
(Name
(N
));
3187 -- If this is a child unit without a spec, and it has been analyzed
3188 -- already, a declaration has been created for it. The with_clause
3189 -- must reflect the actual body, and not the generated declaration,
3190 -- to prevent spurious binding errors involving an out-of-date spec.
3191 -- Note that this can only happen if the unit includes more than one
3192 -- with_clause for the child unit (e.g. in separate subunits).
3194 if Unit_Kind
= N_Subprogram_Declaration
3195 and then Analyzed
(Library_Unit
(N
))
3196 and then not Comes_From_Source
(Library_Unit
(N
))
3198 Set_Library_Unit
(N
,
3199 Cunit
(Get_Source_Unit
(Corresponding_Body
(U
))));
3203 -- Restore style checks
3205 Style_Check
:= Save_Style_Check
;
3207 -- Record the reference, but do NOT set the unit as referenced, we want
3208 -- to consider the unit as unreferenced if this is the only reference
3211 Set_Entity_With_Checks
(Name
(N
), E_Name
);
3212 Generate_Reference
(E_Name
, Name
(N
), 'w', Set_Ref
=> False);
3214 -- Generate references and check No_Dependence restriction for parents
3216 if Is_Child_Unit
(E_Name
) then
3217 Pref
:= Prefix
(Name
(N
));
3218 Par_Name
:= Scope
(E_Name
);
3219 while Nkind
(Pref
) = N_Selected_Component
loop
3220 Change_Selected_Component_To_Expanded_Name
(Pref
);
3222 if Present
(Entity
(Selector_Name
(Pref
)))
3224 Present
(Renamed_Entity
(Entity
(Selector_Name
(Pref
))))
3225 and then Entity
(Selector_Name
(Pref
)) /= Par_Name
3227 -- The prefix is a child unit that denotes a renaming declaration.
3228 -- Replace the prefix directly with the renamed unit, because the
3229 -- rest of the prefix is irrelevant to the visibility of the real
3232 Rewrite
(Pref
, New_Occurrence_Of
(Par_Name
, Sloc
(Pref
)));
3236 Set_Entity_With_Checks
(Pref
, Par_Name
);
3238 Generate_Reference
(Par_Name
, Pref
);
3239 Check_Restriction_No_Dependence
(Pref
, N
);
3240 Pref
:= Prefix
(Pref
);
3242 -- If E_Name is the dummy entity for a nonexistent unit, its scope
3243 -- is set to Standard_Standard, and no attempt should be made to
3244 -- further unwind scopes.
3246 if Par_Name
/= Standard_Standard
then
3247 Par_Name
:= Scope
(Par_Name
);
3250 -- Abandon processing in case of previous errors
3252 if No
(Par_Name
) then
3253 Check_Error_Detected
;
3258 if Present
(Entity
(Pref
))
3259 and then not Analyzed
(Parent
(Parent
(Entity
(Pref
))))
3261 -- If the entity is set without its unit being compiled, the
3262 -- original parent is a renaming, and Par_Name is the renamed
3263 -- entity. For visibility purposes, we need the original entity,
3264 -- which must be analyzed now because Load_Unit directly retrieves
3265 -- the renamed unit, and the renaming declaration itself has not
3268 Analyze
(Parent
(Parent
(Entity
(Pref
))));
3269 pragma Assert
(Renamed_Entity
(Entity
(Pref
)) = Par_Name
);
3270 Par_Name
:= Entity
(Pref
);
3273 -- Guard against missing or misspelled child units
3275 if Present
(Par_Name
) then
3276 Set_Entity_With_Checks
(Pref
, Par_Name
);
3277 Generate_Reference
(Par_Name
, Pref
);
3280 pragma Assert
(Serious_Errors_Detected
/= 0);
3282 -- Mark the node to indicate that a related error has been posted.
3283 -- This defends further compilation passes against improper use of
3284 -- the invalid WITH clause node.
3286 Set_Error_Posted
(N
);
3287 Set_Name
(N
, Error
);
3292 -- If the withed unit is System, and a system extension pragma is
3293 -- present, compile the extension now, rather than waiting for a
3294 -- visibility check on a specific entity.
3296 if Chars
(E_Name
) = Name_System
3297 and then Scope
(E_Name
) = Standard_Standard
3298 and then Present
(System_Extend_Unit
)
3299 and then Present_System_Aux
(N
)
3301 -- If the extension is not present, an error will have been emitted
3306 -- Ada 2005 (AI-262): Remove from visibility the entity corresponding
3307 -- to private_with units; they will be made visible later (just before
3308 -- the private part is analyzed)
3310 if Private_Present
(N
) then
3311 Set_Is_Immediately_Visible
(E_Name
, False);
3314 -- Propagate Fatal_Error setting from with'ed unit to current unit
3316 case Fatal_Error
(Get_Source_Unit
(Library_Unit
(N
))) is
3318 -- Nothing to do if with'ed unit had no error
3323 -- If with'ed unit had a detected fatal error, propagate it
3325 when Error_Detected
=>
3326 Set_Fatal_Error
(Current_Sem_Unit
, Error_Detected
);
3328 -- If with'ed unit had an ignored error, then propagate it but do not
3329 -- overide an existring setting.
3331 when Error_Ignored
=>
3332 if Fatal_Error
(Current_Sem_Unit
) = None
then
3333 Set_Fatal_Error
(Current_Sem_Unit
, Error_Ignored
);
3336 end Analyze_With_Clause
;
3338 ------------------------------
3339 -- Check_Private_Child_Unit --
3340 ------------------------------
3342 procedure Check_Private_Child_Unit
(N
: Node_Id
) is
3343 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3345 Curr_Unit
: Entity_Id
;
3346 Sub_Parent
: Node_Id
;
3347 Priv_Child
: Entity_Id
;
3348 Par_Lib
: Entity_Id
;
3352 if Nkind
(Lib_Unit
) in N_Package_Body | N_Subprogram_Body
then
3353 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(N
)));
3354 Par_Lib
:= Curr_Unit
;
3356 elsif Nkind
(Lib_Unit
) = N_Subunit
then
3358 -- The parent is itself a body. The parent entity is to be found in
3359 -- the corresponding spec.
3361 Sub_Parent
:= Library_Unit
(N
);
3362 Curr_Unit
:= Defining_Entity
(Unit
(Library_Unit
(Sub_Parent
)));
3364 -- If the parent itself is a subunit, Curr_Unit is the entity of the
3365 -- enclosing body, retrieve the spec entity which is the proper
3366 -- ancestor we need for the following tests.
3368 if Ekind
(Curr_Unit
) = E_Package_Body
then
3369 Curr_Unit
:= Spec_Entity
(Curr_Unit
);
3372 Par_Lib
:= Curr_Unit
;
3375 Curr_Unit
:= Defining_Entity
(Lib_Unit
);
3377 Par_Lib
:= Curr_Unit
;
3378 Par_Spec
:= Parent_Spec
(Lib_Unit
);
3380 if No
(Par_Spec
) then
3383 Par_Lib
:= Defining_Entity
(Unit
(Par_Spec
));
3387 -- Loop through context items
3389 Item
:= First
(Context_Items
(N
));
3390 while Present
(Item
) loop
3392 -- Ada 2005 (AI-262): Allow private_with of a private child package
3393 -- in public siblings
3395 if Nkind
(Item
) = N_With_Clause
3396 and then not Implicit_With
(Item
)
3397 and then not Limited_Present
(Item
)
3398 and then Is_Private_Descendant
(Entity
(Name
(Item
)))
3400 Priv_Child
:= Entity
(Name
(Item
));
3403 Curr_Parent
: Entity_Id
:= Par_Lib
;
3404 Child_Parent
: Entity_Id
:= Scope
(Priv_Child
);
3405 Prv_Ancestor
: Entity_Id
:= Child_Parent
;
3406 Curr_Private
: Boolean := Is_Private_Library_Unit
(Curr_Unit
);
3409 -- If the child unit is a public child then locate the nearest
3410 -- private ancestor. Child_Parent will then be set to the
3411 -- parent of that ancestor.
3413 if not Is_Private_Library_Unit
(Priv_Child
) then
3414 while Present
(Prv_Ancestor
)
3415 and then not Is_Private_Library_Unit
(Prv_Ancestor
)
3417 Prv_Ancestor
:= Scope
(Prv_Ancestor
);
3420 if Present
(Prv_Ancestor
) then
3421 Child_Parent
:= Scope
(Prv_Ancestor
);
3425 while Present
(Curr_Parent
)
3426 and then Curr_Parent
/= Standard_Standard
3427 and then Curr_Parent
/= Child_Parent
3430 Curr_Private
or else Is_Private_Library_Unit
(Curr_Parent
);
3431 Curr_Parent
:= Scope
(Curr_Parent
);
3434 if No
(Curr_Parent
) then
3435 Curr_Parent
:= Standard_Standard
;
3438 if Curr_Parent
/= Child_Parent
then
3439 if Ekind
(Priv_Child
) = E_Generic_Package
3440 and then Chars
(Priv_Child
) in Text_IO_Package_Name
3441 and then Chars
(Scope
(Scope
(Priv_Child
))) = Name_Ada
3442 and then Scope
(Scope
(Scope
(Priv_Child
))) =
3446 ("& is a nested package, not a compilation unit",
3447 Name
(Item
), Priv_Child
);
3451 ("unit in with clause is private child unit!", Item
);
3453 ("\current unit must also have parent&!",
3454 Item
, Child_Parent
);
3458 or else Private_Present
(Item
)
3459 or else Nkind
(Lib_Unit
) in N_Package_Body | N_Subunit
3460 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
3461 and then not Acts_As_Spec
(Parent
(Lib_Unit
)))
3467 ("current unit must also be private descendant of&",
3468 Item
, Child_Parent
);
3475 end Check_Private_Child_Unit
;
3477 ----------------------
3478 -- Check_Stub_Level --
3479 ----------------------
3481 procedure Check_Stub_Level
(N
: Node_Id
) is
3482 Par
: constant Node_Id
:= Parent
(N
);
3483 Kind
: constant Node_Kind
:= Nkind
(Par
);
3487 N_Package_Body | N_Subprogram_Body | N_Task_Body | N_Protected_Body
3488 and then Nkind
(Parent
(Par
)) in N_Compilation_Unit | N_Subunit
3492 -- In an instance, a missing stub appears at any level. A warning
3493 -- message will have been emitted already for the missing file.
3495 elsif not In_Instance
then
3496 Error_Msg_N
("stub cannot appear in an inner scope", N
);
3498 elsif Expander_Active
then
3499 Error_Msg_N
("missing proper body", N
);
3501 end Check_Stub_Level
;
3507 procedure Decorate_Type
3510 Is_Tagged
: Boolean := False;
3511 Materialize
: Boolean := False)
3516 -- An unanalyzed type or a shadow entity of a type is treated as an
3517 -- incomplete type, and carries the corresponding attributes.
3519 Mutate_Ekind
(Ent
, E_Incomplete_Type
);
3520 Set_Is_Not_Self_Hidden
(Ent
);
3521 Set_Etype
(Ent
, Ent
);
3522 Set_Full_View
(Ent
, Empty
);
3523 Set_Is_First_Subtype
(Ent
);
3524 Set_Scope
(Ent
, Scop
);
3525 Set_Stored_Constraint
(Ent
, No_Elist
);
3526 Reinit_Size_Align
(Ent
);
3528 if From_Limited_With
(Ent
) then
3529 Set_Private_Dependents
(Ent
, New_Elmt_List
);
3532 -- A tagged type and its corresponding shadow entity share one common
3533 -- class-wide type. The list of primitive operations for the shadow
3537 Set_Is_Tagged_Type
(Ent
);
3538 Set_Direct_Primitive_Operations
(Ent
, New_Elmt_List
);
3542 (E_Void
, Scope
(Ent
), Sloc
(Ent
), Ent
, 'C', 0, 'T');
3544 Set_Class_Wide_Type
(Ent
, CW_Typ
);
3546 -- Set parent to be the same as the parent of the tagged type.
3547 -- We need a parent field set, and it is supposed to point to
3548 -- the declaration of the type. The tagged type declaration
3549 -- essentially declares two separate types, the tagged type
3550 -- itself and the corresponding class-wide type, so it is
3551 -- reasonable for the parent fields to point to the declaration
3554 Set_Parent
(CW_Typ
, Parent
(Ent
));
3556 Mutate_Ekind
(CW_Typ
, E_Class_Wide_Type
);
3557 Set_Class_Wide_Type
(CW_Typ
, CW_Typ
);
3558 Set_Etype
(CW_Typ
, Ent
);
3559 Set_Equivalent_Type
(CW_Typ
, Empty
);
3560 Set_From_Limited_With
(CW_Typ
, From_Limited_With
(Ent
));
3561 Set_Has_Unknown_Discriminants
(CW_Typ
);
3562 Set_Is_First_Subtype
(CW_Typ
);
3563 Set_Is_Tagged_Type
(CW_Typ
);
3564 Set_Materialize_Entity
(CW_Typ
, Materialize
);
3565 Set_Scope
(CW_Typ
, Scop
);
3566 Reinit_Size_Align
(CW_Typ
);
3570 ------------------------
3571 -- Expand_With_Clause --
3572 ------------------------
3574 procedure Expand_With_Clause
(Item
: Node_Id
; Nam
: Node_Id
; N
: Node_Id
) is
3575 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
3577 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
;
3578 -- Build name to be used in implicit with_clause. In most cases this
3579 -- is the source name, but if renamings are present we must make the
3580 -- original unit visible, not the one it renames. The entity in the
3581 -- with clause is the renamed unit, but the identifier is the one from
3582 -- the source, which allows us to recover the unit renaming.
3584 ---------------------
3585 -- Build_Unit_Name --
3586 ---------------------
3588 function Build_Unit_Name
(Nam
: Node_Id
) return Node_Id
is
3593 if Nkind
(Nam
) = N_Identifier
then
3594 return New_Occurrence_Of
(Entity
(Nam
), Loc
);
3597 Ent
:= Entity
(Nam
);
3599 if Present
(Entity
(Selector_Name
(Nam
)))
3600 and then Chars
(Entity
(Selector_Name
(Nam
))) /= Chars
(Ent
)
3602 Nkind
(Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))) =
3603 N_Package_Renaming_Declaration
3605 -- The name in the with_clause is of the form A.B.C, and B is
3606 -- given by a renaming declaration. In that case we may not
3607 -- have analyzed the unit for B, but replaced it directly in
3608 -- lib-load with the unit it renames. We have to make A.B
3609 -- visible, so analyze the declaration for B now, in case it
3610 -- has not been done yet.
3612 Ent
:= Entity
(Selector_Name
(Nam
));
3615 (Unit_Declaration_Node
(Entity
(Selector_Name
(Nam
)))));
3619 Make_Expanded_Name
(Loc
,
3620 Chars
=> Chars
(Entity
(Nam
)),
3621 Prefix
=> Build_Unit_Name
(Prefix
(Nam
)),
3622 Selector_Name
=> New_Occurrence_Of
(Ent
, Loc
));
3623 Set_Entity
(Result
, Ent
);
3627 end Build_Unit_Name
;
3631 Ent
: constant Entity_Id
:= Entity
(Nam
);
3632 Withn
: constant Node_Id
:=
3634 (Loc
, Name
=> Build_Unit_Name
(Nam
),
3635 First_Name
=> True, Last_Name
=> True);
3637 -- Start of processing for Expand_With_Clause
3640 Set_Corresponding_Spec
(Withn
, Ent
);
3641 Set_Implicit_With
(Withn
);
3642 Set_Library_Unit
(Withn
, Parent
(Unit_Declaration_Node
(Ent
)));
3643 Set_Parent_With
(Withn
);
3645 -- If the unit is a [generic] package or subprogram declaration
3646 -- (including a subprogram body acting as spec), a private_with_clause
3647 -- on a child unit implies that the implicit with on the parent is also
3650 if Nkind
(Unit
(N
)) in N_Generic_Package_Declaration
3651 | N_Package_Declaration
3652 | N_Generic_Subprogram_Declaration
3653 | N_Subprogram_Declaration
3656 Set_Private_Present
(Withn
, Private_Present
(Item
));
3659 Prepend
(Withn
, Context_Items
(N
));
3660 Mark_Rewrite_Insertion
(Withn
);
3662 Install_With_Clause
(Withn
);
3664 -- If we have "with X.Y;", we want to recurse on "X", except in the
3665 -- unusual case where X.Y is a renaming of X. In that case, the scope
3666 -- of X will be null.
3668 if Nkind
(Nam
) = N_Expanded_Name
3669 and then Present
(Scope
(Entity
(Prefix
(Nam
))))
3671 Expand_With_Clause
(Item
, Prefix
(Nam
), N
);
3673 end Expand_With_Clause
;
3675 --------------------------------
3676 -- Generate_Parent_References --
3677 --------------------------------
3679 procedure Generate_Parent_References
(N
: Node_Id
; P_Id
: Entity_Id
) is
3681 P_Name
: Entity_Id
:= P_Id
;
3684 if Nkind
(N
) = N_Subunit
then
3687 Pref
:= Name
(Parent
(Defining_Entity
(N
)));
3690 if Nkind
(Pref
) = N_Expanded_Name
then
3692 -- Done already, if the unit has been compiled indirectly as
3693 -- part of the closure of its context because of inlining.
3698 while Nkind
(Pref
) = N_Selected_Component
loop
3699 Change_Selected_Component_To_Expanded_Name
(Pref
);
3700 Set_Entity
(Pref
, P_Name
);
3701 Set_Etype
(Pref
, Etype
(P_Name
));
3702 Generate_Reference
(P_Name
, Pref
, 'r');
3703 Pref
:= Prefix
(Pref
);
3704 P_Name
:= Scope
(P_Name
);
3707 -- The guard here on P_Name is to handle the error condition where
3708 -- the parent unit is missing because the file was not found.
3710 if Present
(P_Name
) then
3711 Set_Entity
(Pref
, P_Name
);
3712 Set_Etype
(Pref
, Etype
(P_Name
));
3713 Generate_Reference
(P_Name
, Pref
, 'r');
3714 Style
.Check_Identifier
(Pref
, P_Name
);
3716 end Generate_Parent_References
;
3718 ---------------------
3719 -- Has_With_Clause --
3720 ---------------------
3722 function Has_With_Clause
3725 Is_Limited
: Boolean := False) return Boolean
3729 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
;
3730 -- Return the entity for the unit named in a [limited] with clause
3736 function Named_Unit
(Clause
: Node_Id
) return Entity_Id
is
3738 if Nkind
(Name
(Clause
)) = N_Selected_Component
then
3739 return Entity
(Selector_Name
(Name
(Clause
)));
3741 return Entity
(Name
(Clause
));
3745 -- Start of processing for Has_With_Clause
3748 Item
:= First
(Context_Items
(C_Unit
));
3749 while Present
(Item
) loop
3750 if Nkind
(Item
) = N_With_Clause
3751 and then Limited_Present
(Item
) = Is_Limited
3752 and then Named_Unit
(Item
) = Pack
3761 end Has_With_Clause
;
3763 -----------------------------
3764 -- Implicit_With_On_Parent --
3765 -----------------------------
3767 procedure Implicit_With_On_Parent
3768 (Child_Unit
: Node_Id
;
3771 Loc
: constant Source_Ptr
:= Sloc
(N
);
3772 P
: constant Node_Id
:= Parent_Spec
(Child_Unit
);
3773 P_Unit
: Node_Id
:= Unit
(P
);
3774 P_Name
: constant Entity_Id
:= Get_Parent_Entity
(P_Unit
);
3776 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
;
3777 -- Build prefix of child unit name. Recurse if needed
3779 function Build_Unit_Name
return Node_Id
;
3780 -- If the unit is a child unit, build qualified name with all ancestors
3782 -------------------------
3783 -- Build_Ancestor_Name --
3784 -------------------------
3786 function Build_Ancestor_Name
(P
: Node_Id
) return Node_Id
is
3787 P_Ref
: constant Node_Id
:=
3788 New_Occurrence_Of
(Defining_Entity
(P
), Loc
);
3789 P_Spec
: Node_Id
:= P
;
3792 -- Ancestor may have been rewritten as a package body. Retrieve the
3793 -- original spec to trace earlier ancestors.
3795 if Nkind
(P
) = N_Package_Body
3796 and then Nkind
(Original_Node
(P
)) = N_Package_Instantiation
3798 P_Spec
:= Original_Node
(P
);
3801 if No
(Parent_Spec
(P_Spec
)) then
3805 Make_Selected_Component
(Loc
,
3807 Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Spec
))),
3808 Selector_Name
=> P_Ref
);
3810 end Build_Ancestor_Name
;
3812 ---------------------
3813 -- Build_Unit_Name --
3814 ---------------------
3816 function Build_Unit_Name
return Node_Id
is
3820 if No
(Parent_Spec
(P_Unit
)) then
3821 return New_Occurrence_Of
(P_Name
, Loc
);
3825 Make_Expanded_Name
(Loc
,
3826 Chars
=> Chars
(P_Name
),
3828 Build_Ancestor_Name
(Unit
(Parent_Spec
(P_Unit
))),
3829 Selector_Name
=> New_Occurrence_Of
(P_Name
, Loc
));
3830 Set_Entity
(Result
, P_Name
);
3834 end Build_Unit_Name
;
3836 -- Start of processing for Implicit_With_On_Parent
3839 -- The unit of the current compilation may be a package body that
3840 -- replaces an instance node. In this case we need the original instance
3841 -- node to construct the proper parent name.
3843 if Nkind
(P_Unit
) = N_Package_Body
3844 and then Nkind
(Original_Node
(P_Unit
)) = N_Package_Instantiation
3846 P_Unit
:= Original_Node
(P_Unit
);
3849 -- We add the implicit with if the child unit is the current unit being
3850 -- compiled. If the current unit is a body, we do not want to add an
3851 -- implicit_with a second time to the corresponding spec.
3853 if Nkind
(Child_Unit
) = N_Package_Declaration
3854 and then Child_Unit
/= Unit
(Cunit
(Current_Sem_Unit
))
3860 Withn
: constant Node_Id
:=
3862 (Loc
, Name
=> Build_Unit_Name
,
3863 First_Name
=> True, Last_Name
=> True);
3865 Set_Corresponding_Spec
(Withn
, P_Name
);
3866 Set_Implicit_With
(Withn
);
3867 Set_Library_Unit
(Withn
, P
);
3868 Set_Parent_With
(Withn
);
3870 -- Node is placed at the beginning of the context items, so that
3871 -- subsequent use clauses on the parent can be validated.
3873 Prepend
(Withn
, Context_Items
(N
));
3874 Mark_Rewrite_Insertion
(Withn
);
3876 Install_With_Clause
(Withn
);
3879 if Is_Child_Spec
(P_Unit
) then
3880 Implicit_With_On_Parent
(P_Unit
, N
);
3882 end Implicit_With_On_Parent
;
3888 function In_Chain
(E
: Entity_Id
) return Boolean is
3892 H
:= Current_Entity
(E
);
3893 while Present
(H
) loop
3904 ---------------------
3905 -- Install_Context --
3906 ---------------------
3908 procedure Install_Context
(N
: Node_Id
; Chain
: Boolean := True) is
3909 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3912 Install_Context_Clauses
(N
, Chain
);
3914 if Is_Child_Spec
(Lib_Unit
) then
3916 (Lib_Unit
=> Lib_Unit
,
3917 Is_Private
=> Private_Present
(Parent
(Lib_Unit
)),
3921 Install_Limited_Context_Clauses
(N
);
3922 end Install_Context
;
3924 -----------------------------
3925 -- Install_Context_Clauses --
3926 -----------------------------
3928 procedure Install_Context_Clauses
(N
: Node_Id
; Chain
: Boolean := True) is
3929 Lib_Unit
: constant Node_Id
:= Unit
(N
);
3931 Uname_Node
: Entity_Id
;
3932 Check_Private
: Boolean := False;
3933 Decl_Node
: Node_Id
;
3934 Lib_Parent
: Entity_Id
;
3937 -- First skip configuration pragmas at the start of the context. They
3938 -- are not technically part of the context clause, but that's where the
3939 -- parser puts them. Note they were analyzed in Analyze_Context.
3941 Item
:= First
(Context_Items
(N
));
3942 while Present
(Item
)
3943 and then Nkind
(Item
) = N_Pragma
3944 and then Pragma_Name
(Item
) in Configuration_Pragma_Names
3949 -- Loop through the actual context clause items. We process everything
3950 -- except Limited_With clauses in this routine. Limited_With clauses
3951 -- are separately installed (see Install_Limited_Context_Clauses).
3953 while Present
(Item
) loop
3955 -- Case of explicit WITH clause
3957 if Nkind
(Item
) = N_With_Clause
3958 and then not Implicit_With
(Item
)
3960 if Limited_Present
(Item
) then
3962 -- Limited withed units will be installed later
3966 -- If Name (Item) is not an entity name, something is wrong, and
3967 -- this will be detected in due course, for now ignore the item
3969 elsif not Is_Entity_Name
(Name
(Item
)) then
3972 elsif No
(Entity
(Name
(Item
))) then
3973 Set_Entity
(Name
(Item
), Any_Id
);
3977 Uname_Node
:= Entity
(Name
(Item
));
3979 if Is_Private_Descendant
(Uname_Node
) then
3980 Check_Private
:= True;
3983 Install_With_Clause
(Item
);
3985 Decl_Node
:= Unit_Declaration_Node
(Uname_Node
);
3987 -- If the unit is a subprogram instance, it appears nested within
3988 -- a package that carries the parent information.
3990 if Is_Generic_Instance
(Uname_Node
)
3991 and then Ekind
(Uname_Node
) /= E_Package
3993 Decl_Node
:= Parent
(Parent
(Decl_Node
));
3996 if Is_Child_Spec
(Decl_Node
) then
3997 if Nkind
(Name
(Item
)) = N_Expanded_Name
then
3998 Expand_With_Clause
(Item
, Prefix
(Name
(Item
)), N
);
4000 -- If not an expanded name, the child unit must be a
4001 -- renaming, nothing to do.
4006 elsif Nkind
(Decl_Node
) = N_Subprogram_Body
4007 and then not Acts_As_Spec
(Parent
(Decl_Node
))
4008 and then Is_Child_Spec
(Unit
(Library_Unit
(Parent
(Decl_Node
))))
4010 Implicit_With_On_Parent
4011 (Unit
(Library_Unit
(Parent
(Decl_Node
))), N
);
4014 -- Check license conditions unless this is a dummy unit
4016 if Sloc
(Library_Unit
(Item
)) /= No_Location
then
4017 License_Check
: declare
4018 Withu
: constant Unit_Number_Type
:=
4019 Get_Source_Unit
(Library_Unit
(Item
));
4020 Withl
: constant License_Type
:=
4021 License
(Source_Index
(Withu
));
4022 Unitl
: constant License_Type
:=
4023 License
(Source_Index
(Current_Sem_Unit
));
4025 procedure License_Error
;
4026 -- Signal error of bad license
4032 procedure License_Error
is
4035 ("license of withed unit & may be inconsistent??",
4039 -- Start of processing for License_Check
4042 -- Exclude license check if withed unit is an internal unit.
4043 -- This situation arises e.g. with the GPL version of GNAT.
4045 if Is_Internal_Unit
(Withu
) then
4048 -- Otherwise check various cases
4060 if Withl
= Restricted
then
4064 when Modified_GPL
=>
4065 if Withl
= Restricted
or else Withl
= GPL
then
4069 when Unrestricted
=>
4076 -- Case of USE PACKAGE clause
4078 elsif Nkind
(Item
) = N_Use_Package_Clause
then
4079 Analyze_Use_Package
(Item
, Chain
);
4081 -- Case of USE TYPE clause
4083 elsif Nkind
(Item
) = N_Use_Type_Clause
then
4084 Analyze_Use_Type
(Item
, Chain
);
4088 elsif Nkind
(Item
) = N_Pragma
then
4096 if Is_Child_Spec
(Lib_Unit
) then
4098 -- The unit also has implicit with_clauses on its own parents
4100 if No
(Context_Items
(N
)) then
4101 Set_Context_Items
(N
, New_List
);
4104 Implicit_With_On_Parent
(Lib_Unit
, N
);
4107 -- If the unit is a body, the context of the specification must also
4108 -- be installed. That includes private with_clauses in that context.
4110 if Nkind
(Lib_Unit
) = N_Package_Body
4111 or else (Nkind
(Lib_Unit
) = N_Subprogram_Body
4112 and then not Acts_As_Spec
(N
))
4114 Install_Context
(Library_Unit
(N
), Chain
);
4116 -- Only install private with-clauses of a spec that comes from
4117 -- source, excluding specs created for a subprogram body that is
4120 if Comes_From_Source
(Library_Unit
(N
)) then
4121 Install_Private_With_Clauses
4122 (Defining_Entity
(Unit
(Library_Unit
(N
))));
4125 if Is_Child_Spec
(Unit
(Library_Unit
(N
))) then
4127 -- If the unit is the body of a public child unit, the private
4128 -- declarations of the parent must be made visible. If the child
4129 -- unit is private, the private declarations have been installed
4130 -- already in the call to Install_Parents for the spec. Installing
4131 -- private declarations must be done for all ancestors of public
4132 -- child units. In addition, sibling units mentioned in the
4133 -- context clause of the body are directly visible.
4141 Lib_Spec
:= Unit
(Library_Unit
(N
));
4142 while Is_Child_Spec
(Lib_Spec
) loop
4143 P
:= Unit
(Parent_Spec
(Lib_Spec
));
4144 P_Name
:= Defining_Entity
(P
);
4146 if not (Private_Present
(Parent
(Lib_Spec
)))
4147 and then not In_Private_Part
(P_Name
)
4149 Install_Private_Declarations
(P_Name
);
4150 Install_Private_With_Clauses
(P_Name
);
4151 Set_Use
(Private_Declarations
(Specification
(P
)));
4159 -- For a package body, children in context are immediately visible
4161 Install_Siblings
(Defining_Entity
(Unit
(Library_Unit
(N
))), N
);
4164 if Nkind
(Lib_Unit
) in N_Generic_Package_Declaration
4165 | N_Generic_Subprogram_Declaration
4166 | N_Package_Declaration
4167 | N_Subprogram_Declaration
4169 if Is_Child_Spec
(Lib_Unit
) then
4170 Lib_Parent
:= Defining_Entity
(Unit
(Parent_Spec
(Lib_Unit
)));
4171 Set_Is_Private_Descendant
4172 (Defining_Entity
(Lib_Unit
),
4173 Is_Private_Descendant
(Lib_Parent
)
4174 or else Private_Present
(Parent
(Lib_Unit
)));
4177 Set_Is_Private_Descendant
4178 (Defining_Entity
(Lib_Unit
),
4179 Private_Present
(Parent
(Lib_Unit
)));
4183 if Check_Private
then
4184 Check_Private_Child_Unit
(N
);
4186 end Install_Context_Clauses
;
4188 -------------------------------------
4189 -- Install_Limited_Context_Clauses --
4190 -------------------------------------
4192 procedure Install_Limited_Context_Clauses
(N
: Node_Id
) is
4195 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
);
4196 -- Check that the unlimited view of a given compilation_unit is not
4197 -- already visible through "use + renamings".
4199 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
);
4200 -- Check that if a limited_with clause of a given compilation_unit
4201 -- mentions a descendant of a private child of some library unit, then
4202 -- the given compilation_unit must be the declaration of a private
4203 -- descendant of that library unit, or a public descendant of such. The
4204 -- code is analogous to that of Check_Private_Child_Unit but we cannot
4205 -- use entities on the limited with_clauses because their units have not
4206 -- been analyzed, so we have to climb the tree of ancestors looking for
4207 -- private keywords.
4209 procedure Expand_Limited_With_Clause
4210 (Comp_Unit
: Node_Id
;
4213 -- If a child unit appears in a limited_with clause, there are implicit
4214 -- limited_with clauses on all parents that are not already visible
4215 -- through a regular with clause. This procedure creates the implicit
4216 -- limited with_clauses for the parents and loads the corresponding
4217 -- units. The shadow entities are created when the inserted clause is
4218 -- analyzed. Implements Ada 2005 (AI-50217).
4220 ---------------------
4221 -- Check_Renamings --
4222 ---------------------
4224 procedure Check_Renamings
(P
: Node_Id
; W
: Node_Id
) is
4232 pragma Assert
(Nkind
(W
) = N_With_Clause
);
4234 -- Protect the frontend against previous critical errors
4236 case Nkind
(Unit
(Library_Unit
(W
))) is
4237 when N_Generic_Package_Declaration
4238 | N_Generic_Subprogram_Declaration
4239 | N_Package_Declaration
4240 | N_Subprogram_Declaration
4248 -- Check "use + renamings"
4250 WEnt
:= Defining_Unit_Name
(Specification
(Unit
(Library_Unit
(W
))));
4251 Spec
:= Specification
(Unit
(P
));
4253 Item
:= First
(Visible_Declarations
(Spec
));
4254 while Present
(Item
) loop
4256 -- Look only at use package clauses
4258 if Nkind
(Item
) = N_Use_Package_Clause
then
4260 E
:= Entity
(Name
(Item
));
4262 pragma Assert
(Present
(Parent
(E
)));
4264 if Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
4265 and then Renamed_Entity
(E
) = WEnt
4267 -- The unlimited view is visible through use clause and
4268 -- renamings. There is no need to generate the error
4269 -- message here because Is_Visible_Through_Renamings
4270 -- takes care of generating the precise error message.
4274 elsif Nkind
(Parent
(E
)) = N_Package_Specification
then
4276 -- The use clause may refer to a local package.
4277 -- Check all the enclosing scopes.
4280 while E2
/= Standard_Standard
and then E2
/= WEnt
loop
4286 ("unlimited view visible through use clause", W
);
4295 -- Recursive call to check all the ancestors
4297 if Is_Child_Spec
(Unit
(P
)) then
4298 Check_Renamings
(P
=> Parent_Spec
(Unit
(P
)), W
=> W
);
4300 end Check_Renamings
;
4302 ---------------------------------------
4303 -- Check_Private_Limited_Withed_Unit --
4304 ---------------------------------------
4306 procedure Check_Private_Limited_Withed_Unit
(Item
: Node_Id
) is
4307 Curr_Parent
: Node_Id
;
4308 Child_Parent
: Node_Id
;
4309 Curr_Private
: Boolean;
4312 -- Compilation unit of the parent of the withed library unit
4314 Child_Parent
:= Library_Unit
(Item
);
4316 -- If the child unit is a public child, then locate its nearest
4317 -- private ancestor, if any, then Child_Parent will then be set to
4318 -- the parent of that ancestor.
4320 if not Private_Present
(Library_Unit
(Item
)) then
4321 while Present
(Child_Parent
)
4322 and then not Private_Present
(Child_Parent
)
4324 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
4327 if No
(Child_Parent
) then
4332 Child_Parent
:= Parent_Spec
(Unit
(Child_Parent
));
4334 -- Traverse all the ancestors of the current compilation unit to
4335 -- check if it is a descendant of named library unit.
4337 Curr_Parent
:= Parent
(Item
);
4338 Curr_Private
:= Private_Present
(Curr_Parent
);
4340 while Present
(Parent_Spec
(Unit
(Curr_Parent
)))
4341 and then Curr_Parent
/= Child_Parent
4343 Curr_Parent
:= Parent_Spec
(Unit
(Curr_Parent
));
4344 Curr_Private
:= Curr_Private
or else Private_Present
(Curr_Parent
);
4347 if Curr_Parent
/= Child_Parent
then
4349 ("unit in with clause is private child unit!", Item
);
4351 ("\current unit must also have parent&!",
4352 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
4354 elsif Private_Present
(Parent
(Item
))
4355 or else Curr_Private
4356 or else Private_Present
(Item
)
4357 or else Nkind
(Unit
(Parent
(Item
))) in
4358 N_Package_Body | N_Subprogram_Body | N_Subunit
4360 -- Current unit is private, of descendant of a private unit
4366 ("current unit must also be private descendant of&",
4367 Item
, Defining_Unit_Name
(Specification
(Unit
(Child_Parent
))));
4369 end Check_Private_Limited_Withed_Unit
;
4371 --------------------------------
4372 -- Expand_Limited_With_Clause --
4373 --------------------------------
4375 procedure Expand_Limited_With_Clause
4376 (Comp_Unit
: Node_Id
;
4380 Loc
: constant Source_Ptr
:= Sloc
(Nam
);
4381 Unum
: Unit_Number_Type
;
4384 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean;
4385 -- Returns true if the context already includes a with_clause for
4386 -- this unit. If the with_clause is nonlimited, the unit is fully
4387 -- visible and an implicit limited_with should not be created. If
4388 -- there is already a limited_with clause for W, a second one is
4389 -- simply redundant.
4391 --------------------------
4392 -- Previous_Withed_Unit --
4393 --------------------------
4395 function Previous_Withed_Unit
(W
: Node_Id
) return Boolean is
4399 -- A limited with_clause cannot appear in the same context_clause
4400 -- as a nonlimited with_clause which mentions the same library.
4402 Item
:= First
(Context_Items
(Comp_Unit
));
4403 while Present
(Item
) loop
4404 if Nkind
(Item
) = N_With_Clause
4405 and then Library_Unit
(Item
) = Library_Unit
(W
)
4414 end Previous_Withed_Unit
;
4416 -- Start of processing for Expand_Limited_With_Clause
4419 if Nkind
(Nam
) = N_Identifier
then
4421 -- Create node for name of withed unit
4424 Make_With_Clause
(Loc
,
4425 Name
=> New_Copy
(Nam
));
4427 else pragma Assert
(Nkind
(Nam
) = N_Selected_Component
);
4429 Make_With_Clause
(Loc
,
4430 Name
=> Make_Selected_Component
(Loc
,
4431 Prefix
=> New_Copy_Tree
(Prefix
(Nam
)),
4432 Selector_Name
=> New_Copy
(Selector_Name
(Nam
))));
4433 Set_Parent
(Withn
, Parent
(N
));
4436 Set_First_Name
(Withn
);
4437 Set_Implicit_With
(Withn
);
4438 Set_Limited_Present
(Withn
);
4442 (Load_Name
=> Get_Spec_Name
(Get_Unit_Name
(Nam
)),
4447 -- Do not generate a limited_with_clause on the current unit. This
4448 -- path is taken when a unit has a limited_with clause on one of its
4451 if Unum
= Current_Sem_Unit
then
4455 Set_Library_Unit
(Withn
, Cunit
(Unum
));
4456 Set_Corresponding_Spec
4457 (Withn
, Specification
(Unit
(Cunit
(Unum
))));
4459 if not Previous_Withed_Unit
(Withn
) then
4460 Prepend
(Withn
, Context_Items
(Parent
(N
)));
4461 Mark_Rewrite_Insertion
(Withn
);
4463 -- Add implicit limited_with_clauses for parents of child units
4464 -- mentioned in limited_with clauses.
4466 if Nkind
(Nam
) = N_Selected_Component
then
4467 Expand_Limited_With_Clause
(Comp_Unit
, Prefix
(Nam
), N
);
4472 if not Limited_View_Installed
(Withn
) then
4473 Install_Limited_With_Clause
(Withn
);
4476 end Expand_Limited_With_Clause
;
4478 -- Start of processing for Install_Limited_Context_Clauses
4481 Item
:= First
(Context_Items
(N
));
4482 while Present
(Item
) loop
4483 if Nkind
(Item
) = N_With_Clause
4484 and then Limited_Present
(Item
)
4485 and then not Error_Posted
(Item
)
4487 if Nkind
(Name
(Item
)) = N_Selected_Component
then
4488 Expand_Limited_With_Clause
4489 (Comp_Unit
=> N
, Nam
=> Prefix
(Name
(Item
)), N
=> Item
);
4492 Check_Private_Limited_Withed_Unit
(Item
);
4494 if not Implicit_With
(Item
) and then Is_Child_Spec
(Unit
(N
)) then
4495 Check_Renamings
(Parent_Spec
(Unit
(N
)), Item
);
4498 -- A unit may have a limited with on itself if it has a limited
4499 -- with_clause on one of its child units. In that case it is
4500 -- already being compiled and it makes no sense to install its
4503 -- If the item is a limited_private_with_clause, install it if the
4504 -- current unit is a body or if it is a private child. Otherwise
4505 -- the private clause is installed before analyzing the private
4506 -- part of the current unit.
4508 if Library_Unit
(Item
) /= Cunit
(Current_Sem_Unit
)
4509 and then not Limited_View_Installed
(Item
)
4511 not Is_Ancestor_Unit
4512 (Library_Unit
(Item
), Cunit
(Current_Sem_Unit
))
4514 if not Private_Present
(Item
)
4515 or else Private_Present
(N
)
4516 or else Nkind
(Unit
(N
)) in
4517 N_Package_Body | N_Subprogram_Body | N_Subunit
4519 Install_Limited_With_Clause
(Item
);
4527 -- Ada 2005 (AI-412): Examine visible declarations of a package spec,
4528 -- looking for incomplete subtype declarations of incomplete types
4529 -- visible through a limited with clause.
4531 if Ada_Version
>= Ada_2005
4532 and then Analyzed
(N
)
4533 and then Nkind
(Unit
(N
)) = N_Package_Declaration
4538 Non_Lim_View
: Entity_Id
;
4541 Decl
:= First
(Visible_Declarations
(Specification
(Unit
(N
))));
4542 while Present
(Decl
) loop
4543 if Nkind
(Decl
) = N_Subtype_Declaration
4545 Ekind
(Defining_Identifier
(Decl
)) = E_Incomplete_Subtype
4547 From_Limited_With
(Defining_Identifier
(Decl
))
4549 Def_Id
:= Defining_Identifier
(Decl
);
4550 Non_Lim_View
:= Non_Limited_View
(Def_Id
);
4552 if not Is_Incomplete_Type
(Non_Lim_View
) then
4554 -- Convert an incomplete subtype declaration into a
4555 -- corresponding nonlimited view subtype declaration.
4556 -- This is usually the case when analyzing a body that
4557 -- has regular with clauses, when the spec has limited
4560 -- If the nonlimited view is still incomplete, it is
4561 -- the dummy entry already created, and the declaration
4562 -- cannot be reanalyzed. This is the case when installing
4563 -- a parent unit that has limited with-clauses.
4565 Set_Subtype_Indication
(Decl
,
4566 New_Occurrence_Of
(Non_Lim_View
, Sloc
(Def_Id
)));
4567 Set_Etype
(Def_Id
, Non_Lim_View
);
4568 Reinit_Field_To_Zero
(Def_Id
, F_Non_Limited_View
,
4569 Old_Ekind
=> (E_Incomplete_Subtype
=> True,
4571 Reinit_Field_To_Zero
(Def_Id
, F_Private_Dependents
);
4573 (Def_Id
, Subtype_Kind
(Ekind
(Non_Lim_View
)));
4574 Set_Analyzed
(Decl
, False);
4576 -- Reanalyze the declaration, suppressing the call to
4577 -- Enter_Name to avoid duplicate names.
4579 Analyze_Subtype_Declaration
4589 end Install_Limited_Context_Clauses
;
4591 ---------------------
4592 -- Install_Parents --
4593 ---------------------
4595 procedure Install_Parents
4596 (Lib_Unit
: Node_Id
;
4597 Is_Private
: Boolean;
4598 Chain
: Boolean := True)
4606 P
:= Unit
(Parent_Spec
(Lib_Unit
));
4607 P_Name
:= Get_Parent_Entity
(P
);
4609 if Etype
(P_Name
) = Any_Type
then
4613 if Ekind
(P_Name
) = E_Generic_Package
4614 and then Nkind
(Lib_Unit
) not in N_Generic_Declaration
4615 | N_Generic_Renaming_Declaration
4618 ("child of a generic package must be a generic unit", Lib_Unit
);
4620 elsif not Is_Package_Or_Generic_Package
(P_Name
) then
4622 ("parent unit must be package or generic package", Lib_Unit
);
4623 raise Unrecoverable_Error
;
4625 elsif Present
(Renamed_Entity
(P_Name
)) then
4626 Error_Msg_N
("parent unit cannot be a renaming", Lib_Unit
);
4627 raise Unrecoverable_Error
;
4629 -- Verify that a child of an instance is itself an instance, or the
4630 -- renaming of one. Given that an instance that is a unit is replaced
4631 -- with a package declaration, check against the original node. The
4632 -- parent may be currently being instantiated, in which case it appears
4633 -- as a declaration, but the generic_parent is already established
4634 -- indicating that we deal with an instance.
4636 elsif Nkind
(Original_Node
(P
)) = N_Package_Instantiation
then
4637 if Nkind
(Lib_Unit
) in N_Renaming_Declaration
4638 or else Nkind
(Original_Node
(Lib_Unit
)) in N_Generic_Instantiation
4640 (Nkind
(Lib_Unit
) = N_Package_Declaration
4641 and then Present
(Generic_Parent
(Specification
(Lib_Unit
))))
4646 ("child of an instance must be an instance or renaming",
4651 -- This is the recursive call that ensures all parents are loaded
4653 if Is_Child_Spec
(P
) then
4657 Is_Private
or else Private_Present
(Parent
(Lib_Unit
)),
4661 -- Now we can install the context for this parent
4663 Install_Context_Clauses
(Parent_Spec
(Lib_Unit
), Chain
);
4664 Install_Limited_Context_Clauses
(Parent_Spec
(Lib_Unit
));
4665 Install_Siblings
(P_Name
, Parent
(Lib_Unit
));
4667 -- The child unit is in the declarative region of the parent. The parent
4668 -- must therefore appear in the scope stack and be visible, as when
4669 -- compiling the corresponding body. If the child unit is private or it
4670 -- is a package body, private declarations must be accessible as well.
4671 -- Use declarations in the parent must also be installed. Finally, other
4672 -- child units of the same parent that are in the context are
4673 -- immediately visible.
4675 -- Find entity for compilation unit, and set its private descendant
4676 -- status as needed. Indicate that it is a compilation unit, which is
4677 -- redundant in general, but needed if this is a generated child spec
4678 -- for a child body without previous spec.
4680 E_Name
:= Defining_Entity
(Lib_Unit
);
4682 Set_Is_Child_Unit
(E_Name
);
4683 Set_Is_Compilation_Unit
(E_Name
);
4685 Set_Is_Private_Descendant
(E_Name
,
4686 Is_Private_Descendant
(P_Name
)
4687 or else Private_Present
(Parent
(Lib_Unit
)));
4689 P_Spec
:= Package_Specification
(P_Name
);
4690 Push_Scope
(P_Name
);
4692 -- Save current visibility of unit
4694 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
:=
4695 Is_Immediately_Visible
(P_Name
);
4696 Set_Is_Immediately_Visible
(P_Name
);
4697 Install_Visible_Declarations
(P_Name
);
4698 Set_Use
(Visible_Declarations
(P_Spec
));
4700 -- If the parent is a generic unit, its formal part may contain formal
4701 -- packages and use clauses for them.
4703 if Ekind
(P_Name
) = E_Generic_Package
then
4704 Set_Use
(Generic_Formal_Declarations
(Parent
(P_Spec
)));
4707 if Is_Private
or else Private_Present
(Parent
(Lib_Unit
)) then
4708 Install_Private_Declarations
(P_Name
);
4709 Install_Private_With_Clauses
(P_Name
);
4710 Set_Use
(Private_Declarations
(P_Spec
));
4712 end Install_Parents
;
4714 ----------------------------------
4715 -- Install_Private_With_Clauses --
4716 ----------------------------------
4718 procedure Install_Private_With_Clauses
(P
: Entity_Id
) is
4719 Decl
: constant Node_Id
:= Unit_Declaration_Node
(P
);
4723 if Debug_Flag_I
then
4724 Write_Str
("install private with clauses of ");
4725 Write_Name
(Chars
(P
));
4729 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
4730 Item
:= First
(Context_Items
(Parent
(Decl
)));
4731 while Present
(Item
) loop
4732 -- If Item is a private with clause, install it, but do not
4733 -- install implicit private with's that come from (for example)
4734 -- with's on instantiated generics. DO install implicit private
4735 -- with's that come from parents, which is necessary in general,
4736 -- but ???not quite right if the former (generic) case also
4739 if Nkind
(Item
) = N_With_Clause
4740 and then Private_Present
(Item
)
4741 and then (not Implicit_With
(Item
) or else Parent_With
(Item
))
4743 -- If the unit is an ancestor of the current one, it is the
4744 -- case of a private limited with clause on a child unit, and
4745 -- the compilation of one of its descendants, in that case the
4746 -- limited view is irrelevant.
4748 if Limited_Present
(Item
) then
4749 if not Limited_View_Installed
(Item
)
4751 not Is_Ancestor_Unit
(Library_Unit
(Item
),
4752 Cunit
(Current_Sem_Unit
))
4754 Install_Limited_With_Clause
(Item
);
4757 Install_With_Clause
(Item
, Private_With_OK
=> True);
4764 end Install_Private_With_Clauses
;
4766 ----------------------
4767 -- Install_Siblings --
4768 ----------------------
4770 procedure Install_Siblings
(U_Name
: Entity_Id
; N
: Node_Id
) is
4776 -- Iterate over explicit with clauses, and check whether the scope of
4777 -- each entity is an ancestor of the current unit, in which case it is
4778 -- immediately visible.
4780 Item
:= First
(Context_Items
(N
));
4781 while Present
(Item
) loop
4783 -- Do not install private_with_clauses declaration, unless unit
4784 -- is itself a private child unit, or is a body. Note that for a
4785 -- subprogram body the private_with_clause does not take effect
4786 -- until after the specification.
4788 if Nkind
(Item
) /= N_With_Clause
4789 or else Implicit_With
(Item
)
4790 or else Limited_Present
(Item
)
4791 or else Error_Posted
(Item
)
4793 -- Skip processing malformed trees
4795 or else (Try_Semantics
4796 and then Nkind
(Name
(Item
)) not in N_Has_Entity
)
4800 elsif not Private_Present
(Item
)
4801 or else Private_Present
(N
)
4802 or else Nkind
(Unit
(N
)) = N_Package_Body
4804 Id
:= Entity
(Name
(Item
));
4806 if Is_Child_Unit
(Id
)
4807 and then Is_Ancestor_Package
(Scope
(Id
), U_Name
)
4809 Set_Is_Immediately_Visible
(Id
);
4811 -- Check for the presence of another unit in the context that
4812 -- may be inadvertently hidden by the child.
4814 Prev
:= Current_Entity
(Id
);
4817 and then Is_Immediately_Visible
(Prev
)
4818 and then not Is_Child_Unit
(Prev
)
4824 Clause
:= First
(Context_Items
(N
));
4825 while Present
(Clause
) loop
4826 if Nkind
(Clause
) = N_With_Clause
4827 and then Entity
(Name
(Clause
)) = Prev
4830 ("child unit& hides compilation unit " &
4831 "with the same name??",
4841 -- The With_Clause may be on a grandchild or one of its further
4842 -- descendants, which makes a child immediately visible. Examine
4843 -- ancestry to determine whether such a child exists. For example,
4844 -- if current unit is A.C, and with_clause is on A.X.Y.Z, then X
4845 -- is immediately visible.
4847 elsif Is_Child_Unit
(Id
) then
4853 while Is_Child_Unit
(Par
) loop
4854 if Is_Ancestor_Package
(Scope
(Par
), U_Name
) then
4855 Set_Is_Immediately_Visible
(Par
);
4864 -- If the item is a private with-clause on a child unit, the parent
4865 -- may have been installed already, but the child unit must remain
4866 -- invisible until installed in a private part or body, unless there
4867 -- is already a regular with_clause for it in the current unit.
4869 elsif Private_Present
(Item
) then
4870 Id
:= Entity
(Name
(Item
));
4872 if Is_Child_Unit
(Id
) then
4876 function In_Context
return Boolean;
4877 -- Scan context of current unit, to check whether there is
4878 -- a with_clause on the same unit as a private with-clause
4879 -- on a parent, in which case child unit is visible. If the
4880 -- unit is a grandchild, the same applies to its parent.
4886 function In_Context
return Boolean is
4889 First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
4890 while Present
(Clause
) loop
4891 if Nkind
(Clause
) = N_With_Clause
4892 and then Comes_From_Source
(Clause
)
4893 and then Is_Entity_Name
(Name
(Clause
))
4894 and then not Private_Present
(Clause
)
4896 if Entity
(Name
(Clause
)) = Id
4898 (Nkind
(Name
(Clause
)) = N_Expanded_Name
4899 and then Entity
(Prefix
(Name
(Clause
))) = Id
)
4912 Set_Is_Visible_Lib_Unit
(Id
, In_Context
);
4919 end Install_Siblings
;
4921 ---------------------------------
4922 -- Install_Limited_With_Clause --
4923 ---------------------------------
4925 procedure Install_Limited_With_Clause
(N
: Node_Id
) is
4926 P_Unit
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
4929 Is_Child_Package
: Boolean := False;
4930 Lim_Header
: Entity_Id
;
4931 Lim_Typ
: Entity_Id
;
4933 procedure Check_Body_Required
;
4934 -- A unit mentioned in a limited with_clause may not be mentioned in
4935 -- a regular with_clause, but must still be included in the current
4936 -- partition. We need to determine whether the unit needs a body, so
4937 -- that the binder can determine the name of the file to be compiled.
4938 -- Checking whether a unit needs a body can be done without semantic
4939 -- analysis, by examining the nature of the declarations in the package.
4941 function Has_Limited_With_Clause
4942 (C_Unit
: Entity_Id
;
4943 Pack
: Entity_Id
) return Boolean;
4944 -- Determine whether any package in the ancestor chain starting with
4945 -- C_Unit has a limited with clause for package Pack.
4947 -------------------------
4948 -- Check_Body_Required --
4949 -------------------------
4951 procedure Check_Body_Required
is
4952 PA
: constant List_Id
:=
4953 Pragmas_After
(Aux_Decls_Node
(Parent
(P_Unit
)));
4955 procedure Check_Declarations
(Spec
: Node_Id
);
4956 -- Recursive procedure that does the work and checks nested packages
4958 ------------------------
4959 -- Check_Declarations --
4960 ------------------------
4962 procedure Check_Declarations
(Spec
: Node_Id
) is
4964 Incomplete_Decls
: constant Elist_Id
:= New_Elmt_List
;
4966 Subp_List
: constant Elist_Id
:= New_Elmt_List
;
4968 procedure Check_Pragma_Import
(P
: Node_Id
);
4969 -- If a pragma import applies to a previous subprogram, the
4970 -- enclosing unit may not need a body. The processing is syntactic
4971 -- and does not require a declaration to be analyzed. The code
4972 -- below also handles pragma Import when applied to a subprogram
4973 -- that renames another. In this case the pragma applies to the
4976 -- Chains of multiple renames are not handled by the code below.
4977 -- It is probably impossible to handle all cases without proper
4978 -- name resolution. In such cases the algorithm is conservative
4979 -- and will indicate that a body is needed???
4981 -------------------------
4982 -- Check_Pragma_Import --
4983 -------------------------
4985 procedure Check_Pragma_Import
(P
: Node_Id
) is
4991 procedure Remove_Homonyms
(E
: Node_Id
);
4992 -- Make one pass over list of subprograms. Called again if
4993 -- subprogram is a renaming. E is known to be an identifier.
4995 ---------------------
4996 -- Remove_Homonyms --
4997 ---------------------
4999 procedure Remove_Homonyms
(E
: Node_Id
) is
5000 R
: Entity_Id
:= Empty
;
5001 -- Name of renamed entity, if any
5004 Subp_Id
:= First_Elmt
(Subp_List
);
5005 while Present
(Subp_Id
) loop
5006 if Chars
(Node
(Subp_Id
)) = Chars
(E
) then
5007 if Nkind
(Parent
(Parent
(Node
(Subp_Id
))))
5008 /= N_Subprogram_Renaming_Declaration
5011 Next_Elmt
(Subp_Id
);
5012 Remove_Elmt
(Subp_List
, Prev_Id
);
5014 R
:= Name
(Parent
(Parent
(Node
(Subp_Id
))));
5018 Next_Elmt
(Subp_Id
);
5023 if Nkind
(R
) = N_Identifier
then
5024 Remove_Homonyms
(R
);
5026 elsif Nkind
(R
) = N_Selected_Component
then
5027 Remove_Homonyms
(Selector_Name
(R
));
5029 -- Renaming of attribute
5035 end Remove_Homonyms
;
5037 -- Start of processing for Check_Pragma_Import
5040 -- Find name of entity in Import pragma. We have not analyzed
5041 -- the construct, so we must guard against syntax errors.
5043 Arg
:= Next
(First
(Pragma_Argument_Associations
(P
)));
5046 or else Nkind
(Expression
(Arg
)) /= N_Identifier
5050 Imported
:= Expression
(Arg
);
5053 Remove_Homonyms
(Imported
);
5054 end Check_Pragma_Import
;
5056 -- Start of processing for Check_Declarations
5059 -- Search for Elaborate Body pragma
5061 Decl
:= First
(Visible_Declarations
(Spec
));
5062 while Present
(Decl
)
5063 and then Nkind
(Decl
) = N_Pragma
5065 if Get_Pragma_Id
(Decl
) = Pragma_Elaborate_Body
then
5066 Set_Body_Required
(Library_Unit
(N
));
5073 -- Look for declarations that require the presence of a body. We
5074 -- have already skipped pragmas at the start of the list.
5076 while Present
(Decl
) loop
5078 -- Subprogram that comes from source means body may be needed.
5079 -- Save for subsequent examination of import pragmas.
5081 if Comes_From_Source
(Decl
)
5082 and then Nkind
(Decl
) in N_Subprogram_Declaration
5083 | N_Subprogram_Renaming_Declaration
5084 | N_Generic_Subprogram_Declaration
5086 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
5088 -- Package declaration of generic package declaration. We need
5089 -- to recursively examine nested declarations.
5091 elsif Nkind
(Decl
) in N_Package_Declaration
5092 | N_Generic_Package_Declaration
5094 Check_Declarations
(Specification
(Decl
));
5096 elsif Nkind
(Decl
) = N_Pragma
5097 and then Pragma_Name
(Decl
) = Name_Import
5099 Check_Pragma_Import
(Decl
);
5105 -- Same set of tests for private part. In addition to subprograms
5106 -- detect the presence of Taft Amendment types (incomplete types
5107 -- completed in the body).
5109 Decl
:= First
(Private_Declarations
(Spec
));
5110 while Present
(Decl
) loop
5111 if Comes_From_Source
(Decl
)
5112 and then Nkind
(Decl
) in N_Subprogram_Declaration
5113 | N_Subprogram_Renaming_Declaration
5114 | N_Generic_Subprogram_Declaration
5116 Append_Elmt
(Defining_Entity
(Decl
), Subp_List
);
5118 elsif Nkind
(Decl
) in N_Package_Declaration
5119 | N_Generic_Package_Declaration
5121 Check_Declarations
(Specification
(Decl
));
5123 -- Collect incomplete type declarations for separate pass
5125 elsif Nkind
(Decl
) = N_Incomplete_Type_Declaration
then
5126 Append_Elmt
(Decl
, Incomplete_Decls
);
5128 elsif Nkind
(Decl
) = N_Pragma
5129 and then Pragma_Name
(Decl
) = Name_Import
5131 Check_Pragma_Import
(Decl
);
5137 -- Now check incomplete declarations to locate Taft amendment
5138 -- types. This can be done by examining the defining identifiers
5139 -- of type declarations without real semantic analysis.
5145 Inc
:= First_Elmt
(Incomplete_Decls
);
5146 while Present
(Inc
) loop
5147 Decl
:= Next
(Node
(Inc
));
5148 while Present
(Decl
) loop
5149 if Nkind
(Decl
) = N_Full_Type_Declaration
5150 and then Chars
(Defining_Identifier
(Decl
)) =
5151 Chars
(Defining_Identifier
(Node
(Inc
)))
5159 -- If no completion, this is a TAT, and a body is needed
5162 Set_Body_Required
(Library_Unit
(N
));
5170 -- Finally, check whether there are subprograms that still require
5171 -- a body, i.e. are not renamings or null.
5173 if not Is_Empty_Elmt_List
(Subp_List
) then
5179 Subp_Id
:= First_Elmt
(Subp_List
);
5180 Spec
:= Parent
(Node
(Subp_Id
));
5182 while Present
(Subp_Id
) loop
5183 if Nkind
(Parent
(Spec
))
5184 = N_Subprogram_Renaming_Declaration
5188 elsif Nkind
(Spec
) = N_Procedure_Specification
5189 and then Null_Present
(Spec
)
5194 Set_Body_Required
(Library_Unit
(N
));
5198 Next_Elmt
(Subp_Id
);
5202 end Check_Declarations
;
5204 -- Start of processing for Check_Body_Required
5207 -- If this is an imported package (Java and CIL usage) no body is
5208 -- needed. Scan list of pragmas that may follow a compilation unit
5209 -- to look for a relevant pragma Import.
5211 if Present
(PA
) then
5217 while Present
(Prag
) loop
5218 if Nkind
(Prag
) = N_Pragma
5219 and then Get_Pragma_Id
(Prag
) = Pragma_Import
5229 Check_Declarations
(Specification
(P_Unit
));
5230 end Check_Body_Required
;
5232 -----------------------------
5233 -- Has_Limited_With_Clause --
5234 -----------------------------
5236 function Has_Limited_With_Clause
5237 (C_Unit
: Entity_Id
;
5238 Pack
: Entity_Id
) return Boolean
5245 while Present
(Par
) loop
5246 if Ekind
(Par
) /= E_Package
then
5250 -- Retrieve the Compilation_Unit node for Par and determine if
5251 -- its context clauses contain a limited with for Pack.
5253 Par_Unit
:= Parent
(Parent
(Parent
(Par
)));
5255 if Nkind
(Par_Unit
) = N_Package_Declaration
then
5256 Par_Unit
:= Parent
(Par_Unit
);
5259 if Has_With_Clause
(Par_Unit
, Pack
, True) then
5263 -- If there are more ancestors, climb up the tree, otherwise we
5266 if Is_Child_Unit
(Par
) then
5274 end Has_Limited_With_Clause
;
5276 -- Start of processing for Install_Limited_With_Clause
5279 pragma Assert
(not Limited_View_Installed
(N
));
5281 -- In case of limited with_clause on subprograms, generics, instances,
5282 -- or renamings, the corresponding error was previously posted and we
5283 -- have nothing to do here. If the file is missing altogether, it has
5284 -- no source location.
5286 if Nkind
(P_Unit
) /= N_Package_Declaration
5287 or else Sloc
(P_Unit
) = No_Location
5292 P
:= Defining_Unit_Name
(Specification
(P_Unit
));
5294 -- Handle child packages
5296 if Nkind
(P
) = N_Defining_Program_Unit_Name
then
5297 Is_Child_Package
:= True;
5298 P
:= Defining_Identifier
(P
);
5301 -- Do not install the limited-view if the context of the unit is already
5302 -- available through a regular with clause.
5304 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
5305 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
5310 -- Do not install the limited-view if the full-view is already visible
5311 -- through renaming declarations.
5313 if Is_Visible_Through_Renamings
(P
, N
) then
5317 -- Do not install the limited view if this is the unit being analyzed.
5318 -- This unusual case will happen when a unit has a limited_with clause
5319 -- on one of its children. The compilation of the child forces the load
5320 -- of the parent which tries to install the limited view of the child
5321 -- again. Installing the limited view must also be disabled when
5322 -- compiling the body of the child unit.
5324 if P
= Cunit_Entity
(Current_Sem_Unit
)
5325 or else (Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
5326 and then P
= Main_Unit_Entity
5327 and then Is_Ancestor_Unit
5328 (Cunit
(Main_Unit
), Cunit
(Current_Sem_Unit
)))
5333 -- This scenario is similar to the one above, the difference is that the
5334 -- compilation of sibling Par.Sib forces the load of parent Par which
5335 -- tries to install the limited view of Lim_Pack [1]. However Par.Sib
5336 -- has a with clause for Lim_Pack [2] in its body, and thus needs the
5337 -- nonlimited views of all entities from Lim_Pack.
5339 -- limited with Lim_Pack; -- [1]
5340 -- package Par is ... package Lim_Pack is ...
5342 -- with Lim_Pack; -- [2]
5343 -- package Par.Sib is ... package body Par.Sib is ...
5345 -- In this case Main_Unit_Entity is the spec of Par.Sib and Current_
5346 -- Sem_Unit is the body of Par.Sib.
5348 if Ekind
(P
) = E_Package
5349 and then Ekind
(Main_Unit_Entity
) = E_Package
5350 and then Is_Child_Unit
(Main_Unit_Entity
)
5352 -- The body has a regular with clause
5354 and then Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
5355 and then Has_With_Clause
(Cunit
(Current_Sem_Unit
), P
)
5357 -- One of the ancestors has a limited with clause
5359 and then Nkind
(Parent
(Parent
(Main_Unit_Entity
))) =
5360 N_Package_Specification
5361 and then Has_Limited_With_Clause
(Scope
(Main_Unit_Entity
), P
)
5366 -- A common use of the limited-with is to have a limited-with in the
5367 -- package spec, and a normal with in its package body. For example:
5369 -- limited with X; -- [1]
5373 -- package body A is ...
5375 -- The compilation of A's body installs the context clauses found at [2]
5376 -- and then the context clauses of its specification (found at [1]). As
5377 -- a consequence, at [1] the specification of X has been analyzed and it
5378 -- is immediately visible. According to the semantics of limited-with
5379 -- context clauses we don't install the limited view because the full
5380 -- view of X supersedes its limited view.
5382 if Analyzed
(P_Unit
)
5384 (Is_Immediately_Visible
(P
)
5385 or else (Is_Child_Package
and then Is_Visible_Lib_Unit
(P
)))
5388 -- The presence of both the limited and the analyzed nonlimited view
5389 -- may also be an error, such as an illegal context for a limited
5390 -- with_clause. In that case, do not process the context item at all.
5392 if Error_Posted
(N
) then
5396 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Body
then
5400 Item
:= First
(Context_Items
(Cunit
(Current_Sem_Unit
)));
5401 while Present
(Item
) loop
5402 if Nkind
(Item
) = N_With_Clause
5403 and then Comes_From_Source
(Item
)
5404 and then Entity
(Name
(Item
)) = P
5413 -- If this is a child body, assume that the nonlimited with_clause
5414 -- appears in an ancestor. Could be refined ???
5418 (Unit
(Library_Unit
(Cunit
(Current_Sem_Unit
)))))
5425 -- If in package declaration, nonlimited view brought in from
5426 -- parent unit or some error condition.
5432 if Debug_Flag_I
then
5433 Write_Str
("install limited view of ");
5434 Write_Name
(Chars
(P
));
5438 -- If the unit has not been analyzed and the limited view has not been
5439 -- already installed then we install it.
5441 if not Analyzed
(P_Unit
) then
5442 if not In_Chain
(P
) then
5444 -- Minimum decoration
5446 Mutate_Ekind
(P
, E_Package
);
5447 Set_Etype
(P
, Standard_Void_Type
);
5448 Set_Scope
(P
, Standard_Standard
);
5449 Set_Is_Visible_Lib_Unit
(P
);
5451 if Is_Child_Package
then
5452 Set_Is_Child_Unit
(P
);
5453 Set_Scope
(P
, Defining_Entity
(Unit
(Parent_Spec
(P_Unit
))));
5456 -- Place entity on visibility structure
5458 Set_Homonym
(P
, Current_Entity
(P
));
5459 Set_Current_Entity
(P
);
5461 if Debug_Flag_I
then
5462 Write_Str
(" (homonym) chain ");
5463 Write_Name
(Chars
(P
));
5467 -- Install the incomplete view. The first element of the limited
5468 -- view is a header (an E_Package entity) used to reference the
5469 -- first shadow entity in the private part of the package.
5471 Lim_Header
:= Limited_View
(P
);
5472 Lim_Typ
:= First_Entity
(Lim_Header
);
5474 while Present
(Lim_Typ
)
5475 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5477 Set_Homonym
(Lim_Typ
, Current_Entity
(Lim_Typ
));
5478 Set_Current_Entity
(Lim_Typ
);
5480 if Debug_Flag_I
then
5481 Write_Str
(" (homonym) chain ");
5482 Write_Name
(Chars
(Lim_Typ
));
5486 Next_Entity
(Lim_Typ
);
5490 -- If the unit appears in a previous regular with_clause, the regular
5491 -- entities of the public part of the withed package must be replaced
5492 -- by the shadow ones.
5494 -- This code must be kept synchronized with the code that replaces the
5495 -- shadow entities by the real entities in Remove_Limited_With_Unit,
5496 -- otherwise the contents of the homonym chains are not consistent.
5499 -- Hide all the type entities of the public part of the package to
5500 -- avoid its usage. This is needed to cover all the subtype decla-
5501 -- rations because we do not remove them from the homonym chain.
5503 E
:= First_Entity
(P
);
5504 while Present
(E
) and then E
/= First_Private_Entity
(P
) loop
5506 Set_Was_Hidden
(E
, Is_Hidden
(E
));
5513 -- Replace the real entities by the shadow entities of the limited
5514 -- view. The first element of the limited view is a header that is
5515 -- used to reference the first shadow entity in the private part
5516 -- of the package. Successive elements are the limited views of the
5517 -- type (including regular incomplete types) declared in the package.
5519 Lim_Header
:= Limited_View
(P
);
5521 Lim_Typ
:= First_Entity
(Lim_Header
);
5522 while Present
(Lim_Typ
)
5523 and then Lim_Typ
/= First_Private_Entity
(Lim_Header
)
5525 pragma Assert
(not In_Chain
(Lim_Typ
));
5527 -- Do not unchain nested packages and child units
5529 if Ekind
(Lim_Typ
) /= E_Package
5530 and then not Is_Child_Unit
(Lim_Typ
)
5533 Typ
: constant Entity_Id
:= Non_Limited_View
(Lim_Typ
);
5538 -- Replace Typ by Lim_Typ in the homonyms list, so that the
5539 -- limited view becomes available.
5541 -- If the nonlimited view is a record with an anonymous
5542 -- self-referential component, the analysis of the record
5543 -- declaration creates an incomplete type with the same name
5544 -- in order to define an internal access type. The visible
5545 -- entity is now the incomplete type, and that is the one to
5546 -- replace in the visibility structure.
5548 -- Similarly, if the source already contains the incomplete
5549 -- type declaration, the limited view of the incomplete type
5550 -- is in fact never visible (AI05-129) but we have created a
5551 -- shadow entity E1 for it that points to E2, the incomplete
5552 -- type at stake. This in turn has full view E3 that is the
5553 -- full declaration, with a corresponding shadow entity E4.
5554 -- When reinstalling the limited view, the visible entity E2
5555 -- is first replaced with E1, but E4 must eventually become
5556 -- the visible entity as per the AI and thus displace E1, as
5557 -- it is replacing E3 in the homonyms list.
5559 -- regular views limited views
5561 -- * E2 (incomplete) <-- E1 (shadow)
5566 -- E3 (full) <-- E4 (shadow) *
5568 -- [*] denotes the visible entity (Current_Entity)
5570 Prev
:= Current_Entity
(Lim_Typ
);
5572 while Present
(Prev
) loop
5573 -- This is a regular replacement
5576 or else (Ekind
(Prev
) = E_Incomplete_Type
5577 and then Full_View
(Prev
) = Typ
)
5579 Replace
(Prev
, Lim_Typ
);
5581 if Debug_Flag_I
then
5582 Write_Str
(" (homonym) replace ");
5583 Write_Name
(Chars
(Typ
));
5589 -- This is where E1 is replaced with E4
5591 elsif Ekind
(Prev
) = E_Incomplete_Type
5592 and then From_Limited_With
(Prev
)
5594 Ekind
(Non_Limited_View
(Prev
)) = E_Incomplete_Type
5595 and then Full_View
(Non_Limited_View
(Prev
)) = Typ
5597 Replace
(Prev
, Lim_Typ
);
5599 if Debug_Flag_I
then
5600 Write_Str
(" (homonym) E1 -> E4 ");
5601 Write_Name
(Chars
(Typ
));
5608 Prev
:= Homonym
(Prev
);
5613 Next_Entity
(Lim_Typ
);
5617 -- The package must be visible while the limited-with clause is active
5618 -- because references to the type P.T must resolve in the usual way.
5619 -- In addition, we remember that the limited-view has been installed to
5620 -- uninstall it at the point of context removal.
5622 Set_Is_Immediately_Visible
(P
);
5623 Set_Limited_View_Installed
(N
);
5625 -- If unit has not been analyzed in some previous context, check
5626 -- (imperfectly ???) whether it might need a body.
5628 if not Analyzed
(P_Unit
) then
5629 Check_Body_Required
;
5632 -- If the package in the limited_with clause is a child unit, the clause
5633 -- is unanalyzed and appears as a selected component. Recast it as an
5634 -- expanded name so that the entity can be properly set. Use entity of
5635 -- parent, if available, for higher ancestors in the name.
5637 if Nkind
(Name
(N
)) = N_Selected_Component
then
5645 while Nkind
(Nam
) = N_Selected_Component
5646 and then Present
(Ent
)
5648 Change_Selected_Component_To_Expanded_Name
(Nam
);
5650 -- Set entity of parent identifiers if the unit is a child
5651 -- unit. This ensures that the tree is properly formed from
5652 -- semantic point of view. The unit entities are not fully
5653 -- analyzed, so we need to follow unit links in the tree.
5655 Set_Entity
(Nam
, Ent
);
5657 Nam
:= Prefix
(Nam
);
5660 (Unit
(Parent_Spec
(Unit_Declaration_Node
(Ent
))));
5662 -- Set entity of last ancestor
5664 if Nkind
(Nam
) = N_Identifier
then
5665 Set_Entity
(Nam
, Ent
);
5671 Set_Entity
(Name
(N
), P
);
5672 Set_From_Limited_With
(P
);
5673 end Install_Limited_With_Clause
;
5675 -------------------------
5676 -- Install_With_Clause --
5677 -------------------------
5679 procedure Install_With_Clause
5680 (With_Clause
: Node_Id
;
5681 Private_With_OK
: Boolean := False)
5683 Uname
: constant Entity_Id
:= Entity
(Name
(With_Clause
));
5684 P
: constant Entity_Id
:= Scope
(Uname
);
5687 -- Ada 2005 (AI-262): Do not install the private withed unit if we are
5688 -- compiling a package declaration and the Private_With_OK flag was not
5689 -- set by the caller. These declarations will be installed later (before
5690 -- analyzing the private part of the package).
5692 if Private_Present
(With_Clause
)
5693 and then Nkind
(Unit
(Parent
(With_Clause
)))
5694 in N_Package_Declaration | N_Generic_Package_Declaration
5695 and then not Private_With_OK
5700 if Debug_Flag_I
then
5701 if Private_Present
(With_Clause
) then
5702 Write_Str
("install private withed unit ");
5703 elsif Parent_With
(With_Clause
) then
5704 Write_Str
("install parent withed unit ");
5705 elsif Implicit_With
(With_Clause
) then
5706 Write_Str
("install implicit withed unit ");
5708 Write_Str
("install withed unit ");
5711 Write_Name
(Chars
(Uname
));
5715 -- We do not apply the restrictions to an internal unit unless we are
5716 -- compiling the internal unit as a main unit. This check is also
5717 -- skipped for dummy units (for missing packages).
5719 if Sloc
(Uname
) /= No_Location
5720 and then (not Is_Internal_Unit
(Current_Sem_Unit
)
5721 or else Current_Sem_Unit
= Main_Unit
)
5723 Check_Restricted_Unit
5724 (Unit_Name
(Get_Source_Unit
(Uname
)), With_Clause
);
5727 if P
/= Standard_Standard
then
5729 -- If the unit is not analyzed after analysis of the with clause and
5730 -- it is an instantiation then it awaits a body and is the main unit.
5731 -- Its appearance in the context of some other unit indicates a
5732 -- circular dependency (DEC suite perversity).
5734 if not Analyzed
(Uname
)
5735 and then Nkind
(Parent
(Uname
)) = N_Package_Instantiation
5738 ("instantiation depends on itself", Name
(With_Clause
));
5740 elsif not Analyzed
(Uname
)
5741 and then Is_Internal_Unit
(Current_Sem_Unit
)
5742 and then not Is_Visible_Lib_Unit
(Uname
)
5743 and then No
(Scope
(Uname
))
5745 if Is_Predefined_Unit
(Current_Sem_Unit
) then
5747 ("predefined unit depends on itself", Name
(With_Clause
));
5750 ("GNAT-defined unit depends on itself", Name
(With_Clause
));
5754 elsif not Is_Visible_Lib_Unit
(Uname
) then
5756 -- Abandon processing in case of previous errors
5758 if No
(Scope
(Uname
)) then
5759 Check_Error_Detected
;
5763 Set_Is_Visible_Lib_Unit
(Uname
);
5765 -- If the unit is a wrapper package for a compilation unit that is
5766 -- a subprogram instance, indicate that the instance itself is a
5767 -- visible unit. This is necessary if the instance is inlined.
5769 if Is_Wrapper_Package
(Uname
) then
5770 Set_Is_Visible_Lib_Unit
(Related_Instance
(Uname
));
5773 -- If the child unit appears in the context of its parent, it is
5774 -- immediately visible.
5776 if In_Open_Scopes
(Scope
(Uname
)) then
5777 Set_Is_Immediately_Visible
(Uname
);
5780 if Is_Generic_Instance
(Uname
)
5781 and then Is_Subprogram
(Uname
)
5783 -- Set flag as well on the visible entity that denotes the
5784 -- instance, which renames the current one.
5786 Set_Is_Visible_Lib_Unit
5788 (Defining_Entity
(Unit
(Library_Unit
(With_Clause
)))));
5791 -- The parent unit may have been installed already, and may have
5792 -- appeared in a use clause.
5794 if In_Use
(Scope
(Uname
)) then
5795 Set_Is_Potentially_Use_Visible
(Uname
);
5798 Set_Context_Installed
(With_Clause
);
5801 elsif not Is_Immediately_Visible
(Uname
) then
5802 Set_Is_Visible_Lib_Unit
(Uname
);
5804 if not Private_Present
(With_Clause
) or else Private_With_OK
then
5805 Set_Is_Immediately_Visible
(Uname
);
5808 Set_Context_Installed
(With_Clause
);
5811 -- A [private] with clause overrides a limited with clause. Restore the
5812 -- proper view of the package by performing the following actions:
5814 -- * Remove all shadow entities which hide their corresponding
5815 -- entities from direct visibility by updating the entity and
5818 -- * Enter the corresponding entities back in direct visibility
5820 -- Note that the original limited with clause which installed its view
5821 -- is still marked as "active". This effect is undone when the clause
5822 -- itself is removed, see Remove_Limited_With_Clause.
5824 if Ekind
(Uname
) = E_Package
and then From_Limited_With
(Uname
) then
5825 Remove_Limited_With_Unit
(Unit_Declaration_Node
(Uname
));
5828 -- Ada 2005 (AI-377): it is illegal for a with_clause to name a child
5829 -- unit if there is a visible homograph for it declared in the same
5830 -- declarative region. This pathological case can only arise when an
5831 -- instance I1 of a generic unit G1 has an explicit child unit I1.G2,
5832 -- G1 has a generic child also named G2, and the context includes with_
5833 -- clauses for both I1.G2 and for G1.G2, making an implicit declaration
5834 -- of I1.G2 visible as well. If the child unit is named Standard, do
5835 -- not apply the check to the Standard package itself.
5837 if Is_Child_Unit
(Uname
)
5838 and then Is_Visible_Lib_Unit
(Uname
)
5839 and then Ada_Version
>= Ada_2005
5842 Decl1
: constant Node_Id
:= Unit_Declaration_Node
(P
);
5848 U2
:= Homonym
(Uname
);
5849 while Present
(U2
) and then U2
/= Standard_Standard
loop
5851 Decl2
:= Unit_Declaration_Node
(P2
);
5853 if Is_Child_Unit
(U2
) and then Is_Visible_Lib_Unit
(U2
) then
5854 if Is_Generic_Instance
(P
)
5855 and then Nkind
(Decl1
) = N_Package_Declaration
5856 and then Generic_Parent
(Specification
(Decl1
)) = P2
5858 Error_Msg_N
("illegal with_clause", With_Clause
);
5860 ("\child unit has visible homograph" &
5861 " (RM 8.3(26), 10.1.1(19))",
5865 elsif Is_Generic_Instance
(P2
)
5866 and then Nkind
(Decl2
) = N_Package_Declaration
5867 and then Generic_Parent
(Specification
(Decl2
)) = P
5869 -- With_clause for child unit of instance appears before
5870 -- in the context. We want to place the error message on
5871 -- it, not on the generic child unit itself.
5874 Prev_Clause
: Node_Id
;
5877 Prev_Clause
:= First
(List_Containing
(With_Clause
));
5878 while Entity
(Name
(Prev_Clause
)) /= U2
loop
5882 pragma Assert
(Present
(Prev_Clause
));
5883 Error_Msg_N
("illegal with_clause", Prev_Clause
);
5885 ("\child unit has visible homograph" &
5886 " (RM 8.3(26), 10.1.1(19))",
5897 end Install_With_Clause
;
5903 function Is_Child_Spec
(Lib_Unit
: Node_Id
) return Boolean is
5904 K
: constant Node_Kind
:= Nkind
(Lib_Unit
);
5907 return (K
in N_Generic_Declaration
or else
5908 K
in N_Generic_Instantiation
or else
5909 K
in N_Generic_Renaming_Declaration
or else
5910 K
= N_Package_Declaration
or else
5911 K
= N_Package_Renaming_Declaration
or else
5912 K
= N_Subprogram_Declaration
or else
5913 K
= N_Subprogram_Renaming_Declaration
)
5914 and then Present
(Parent_Spec
(Lib_Unit
));
5917 ------------------------------------
5918 -- Is_Legal_Shadow_Entity_In_Body --
5919 ------------------------------------
5921 function Is_Legal_Shadow_Entity_In_Body
(T
: Entity_Id
) return Boolean is
5922 C_Unit
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
5924 return Nkind
(Unit
(C_Unit
)) = N_Package_Body
5927 (C_Unit
, Cunit_Entity
(Get_Source_Unit
(Non_Limited_View
(T
))));
5928 end Is_Legal_Shadow_Entity_In_Body
;
5930 ----------------------
5931 -- Is_Ancestor_Unit --
5932 ----------------------
5934 function Is_Ancestor_Unit
(U1
: Node_Id
; U2
: Node_Id
) return Boolean is
5935 E1
: constant Entity_Id
:= Defining_Entity
(Unit
(U1
));
5938 if Nkind
(Unit
(U2
)) in N_Package_Body | N_Subprogram_Body
then
5939 E2
:= Defining_Entity
(Unit
(Library_Unit
(U2
)));
5940 return Is_Ancestor_Package
(E1
, E2
);
5944 end Is_Ancestor_Unit
;
5946 ----------------------------------
5947 -- Is_Visible_Through_Renamings --
5948 ----------------------------------
5950 function Is_Visible_Through_Renamings
5952 Error_Node
: Node_Id
:= Empty
) return Boolean
5954 function Is_Limited_Withed_Unit
5955 (Lib_Unit
: Node_Id
;
5956 Pkg_Ent
: Entity_Id
) return Boolean;
5957 -- Return True if Pkg_Ent is a limited-withed package of the given
5960 ----------------------------
5961 -- Is_Limited_Withed_Unit --
5962 ----------------------------
5964 function Is_Limited_Withed_Unit
5965 (Lib_Unit
: Node_Id
;
5966 Pkg_Ent
: Entity_Id
) return Boolean
5968 Item
: Node_Id
:= First
(Context_Items
(Lib_Unit
));
5971 while Present
(Item
) loop
5972 if Nkind
(Item
) = N_With_Clause
5973 and then Limited_Present
(Item
)
5974 and then Entity
(Name
(Item
)) = Pkg_Ent
5983 end Is_Limited_Withed_Unit
;
5987 Kind
: constant Node_Kind
:= Nkind
(Unit
(Cunit
(Current_Sem_Unit
)));
5993 -- Example of the error detected by this subprogram:
6001 -- package Ren_P renames P;
6007 -- limited with P; -- ERROR
6008 -- package R.C is ...
6010 Aux_Unit
:= Cunit
(Current_Sem_Unit
);
6013 Item
:= First
(Context_Items
(Aux_Unit
));
6014 while Present
(Item
) loop
6015 if Nkind
(Item
) = N_With_Clause
6016 and then not Limited_Present
(Item
)
6017 and then Nkind
(Unit
(Library_Unit
(Item
))) =
6018 N_Package_Declaration
6021 First
(Visible_Declarations
6022 (Specification
(Unit
(Library_Unit
(Item
)))));
6023 while Present
(Decl
) loop
6024 if Nkind
(Decl
) = N_Package_Renaming_Declaration
6025 and then Entity
(Name
(Decl
)) = P
6026 and then not Is_Limited_Withed_Unit
6027 (Lib_Unit
=> Library_Unit
(Item
),
6028 Pkg_Ent
=> Entity
(Name
(Decl
)))
6030 -- Generate the error message only if the current unit
6031 -- is a package declaration; in case of subprogram
6032 -- bodies and package bodies we just return True to
6033 -- indicate that the limited view must not be
6036 if Kind
= N_Package_Declaration
6037 and then Present
(Error_Node
)
6040 ("simultaneous visibility of the limited and " &
6041 "unlimited views not allowed", Error_Node
);
6042 Error_Msg_Sloc
:= Sloc
(Item
);
6044 ("\\ unlimited view of & visible through the " &
6045 "context clause #", Error_Node
, P
);
6046 Error_Msg_Sloc
:= Sloc
(Decl
);
6047 Error_Msg_NE
("\\ and the renaming #", Error_Node
, P
);
6060 -- If it is a body not acting as spec, follow pointer to the
6061 -- corresponding spec, otherwise follow pointer to parent spec.
6063 if Present
(Library_Unit
(Aux_Unit
))
6064 and then Nkind
(Unit
(Aux_Unit
)) in
6065 N_Package_Body | N_Subprogram_Body
6067 if Aux_Unit
= Library_Unit
(Aux_Unit
) then
6069 -- Aux_Unit is a body that acts as a spec. Clause has
6070 -- already been flagged as illegal.
6075 Aux_Unit
:= Library_Unit
(Aux_Unit
);
6079 Aux_Unit
:= Parent_Spec
(Unit
(Aux_Unit
));
6082 exit when No
(Aux_Unit
);
6086 end Is_Visible_Through_Renamings
;
6088 -----------------------
6089 -- Load_Needed_Body --
6090 -----------------------
6092 -- N is a generic unit named in a with clause, or else it is a unit that
6093 -- contains a generic unit or an inlined function. In order to perform an
6094 -- instantiation, the body of the unit must be present. If the unit itself
6095 -- is generic, we assume that an instantiation follows, and load & analyze
6096 -- the body unconditionally. This forces analysis of the spec as well.
6098 -- If the unit is not generic, but contains a generic unit, it is loaded on
6099 -- demand, at the point of instantiation (see ch12).
6101 procedure Load_Needed_Body
6105 Body_Name
: Unit_Name_Type
;
6106 Unum
: Unit_Number_Type
;
6108 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
6109 -- The loading and analysis is done with style checks off
6112 if not GNAT_Mode
then
6113 Style_Check
:= False;
6116 Body_Name
:= Get_Body_Name
(Get_Unit_Name
(Unit
(N
)));
6119 (Load_Name
=> Body_Name
,
6125 if Unum
= No_Unit
then
6129 Compiler_State
:= Analyzing
; -- reset after load
6131 if Fatal_Error
(Unum
) /= Error_Detected
or else Try_Semantics
then
6132 if Debug_Flag_L
then
6133 Write_Str
("*** Loaded generic body");
6137 -- We always perform analyses
6138 Semantics
(Cunit
(Unum
));
6144 Style_Check
:= Save_Style_Check
;
6145 end Load_Needed_Body
;
6147 -------------------------
6148 -- Build_Limited_Views --
6149 -------------------------
6151 procedure Build_Limited_Views
(N
: Node_Id
) is
6152 Unum
: constant Unit_Number_Type
:=
6153 Get_Source_Unit
(Library_Unit
(N
));
6154 Is_Analyzed
: constant Boolean := Analyzed
(Cunit
(Unum
));
6156 Shadow_Pack
: Entity_Id
;
6157 -- The corresponding shadow entity of the withed package. This entity
6158 -- offers incomplete views of packages and types as well as abstract
6159 -- views of states and variables declared within.
6161 Last_Shadow
: Entity_Id
:= Empty
;
6162 -- The last shadow entity created by routine Build_Shadow_Entity
6164 procedure Build_Shadow_Entity
6167 Shadow
: out Entity_Id
;
6168 Is_Tagged
: Boolean := False);
6169 -- Create a shadow entity that hides Ent and offers an abstract or
6170 -- incomplete view of Ent. Scop is the proper scope. Flag Is_Tagged
6171 -- should be set when Ent is a tagged type. The generated entity is
6172 -- added to Shadow_Pack. The routine updates the value of Last_Shadow.
6174 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
);
6175 -- Perform minimal decoration of a package or its corresponding shadow
6176 -- entity denoted by Ent. Scop is the proper scope.
6178 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
);
6179 -- Perform full decoration of an abstract state or its corresponding
6180 -- shadow entity denoted by Ent. Scop is the proper scope.
6182 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
);
6183 -- Perform minimal decoration of a variable denoted by Ent. Scop is the
6186 procedure Process_Declarations_And_States
6190 Create_Abstract_Views
: Boolean);
6191 -- Inspect the states of package Pack and declarative list Decls. Create
6192 -- shadow entities for all nested packages, states, types and variables
6193 -- encountered. Scop is the proper scope. Create_Abstract_Views should
6194 -- be set when the abstract states and variables need to be processed.
6196 -------------------------
6197 -- Build_Shadow_Entity --
6198 -------------------------
6200 procedure Build_Shadow_Entity
6203 Shadow
: out Entity_Id
;
6204 Is_Tagged
: Boolean := False)
6207 Shadow
:= Make_Temporary
(Sloc
(Ent
), 'Z');
6209 -- The shadow entity must share the same name and parent as the
6212 Set_Chars
(Shadow
, Chars
(Ent
));
6213 Set_Parent
(Shadow
, Parent
(Ent
));
6215 -- The abstract view of a variable is a state, not another variable
6217 if Ekind
(Ent
) = E_Variable
then
6218 Mutate_Ekind
(Shadow
, E_Abstract_State
);
6220 Mutate_Ekind
(Shadow
, Ekind
(Ent
));
6223 Set_Is_Not_Self_Hidden
(Shadow
);
6224 Set_Is_Internal
(Shadow
);
6225 Set_From_Limited_With
(Shadow
);
6227 -- Add the new shadow entity to the limited view of the package
6229 Last_Shadow
:= Shadow
;
6230 Append_Entity
(Shadow
, Shadow_Pack
);
6232 -- Perform context-specific decoration of the shadow entity
6234 if Ekind
(Ent
) = E_Abstract_State
then
6235 Decorate_State
(Shadow
, Scop
);
6236 Set_Non_Limited_View
(Shadow
, Ent
);
6238 elsif Ekind
(Ent
) = E_Package
then
6239 Decorate_Package
(Shadow
, Scop
);
6241 elsif Is_Type
(Ent
) then
6242 Decorate_Type
(Shadow
, Scop
, Is_Tagged
);
6244 -- If Ent is a private type and we are analyzing the body of its
6245 -- scope, its private and full views are swapped and, therefore,
6246 -- we need to undo this swapping in order to build the same shadow
6247 -- entity as we would have in other contexts.
6249 if Is_Private_Type
(Ent
)
6250 and then Present
(Full_View
(Ent
))
6251 and then In_Package_Body
(Scop
)
6253 Set_Non_Limited_View
(Shadow
, Full_View
(Ent
));
6255 Set_Non_Limited_View
(Shadow
, Ent
);
6259 Set_Non_Limited_View
6260 (Class_Wide_Type
(Shadow
), Class_Wide_Type
(Ent
));
6263 if Is_Incomplete_Or_Private_Type
(Ent
) then
6264 Set_Private_Dependents
(Shadow
, New_Elmt_List
);
6267 elsif Ekind
(Ent
) = E_Variable
then
6268 Decorate_State
(Shadow
, Scop
);
6269 Set_Non_Limited_View
(Shadow
, Ent
);
6271 end Build_Shadow_Entity
;
6273 ----------------------
6274 -- Decorate_Package --
6275 ----------------------
6277 procedure Decorate_Package
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
6279 Mutate_Ekind
(Ent
, E_Package
);
6280 Set_Etype
(Ent
, Standard_Void_Type
);
6281 Set_Scope
(Ent
, Scop
);
6282 end Decorate_Package
;
6284 --------------------
6285 -- Decorate_State --
6286 --------------------
6288 procedure Decorate_State
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
6290 Mutate_Ekind
(Ent
, E_Abstract_State
);
6291 Set_Is_Not_Self_Hidden
(Ent
);
6292 Set_Etype
(Ent
, Standard_Void_Type
);
6293 Set_Scope
(Ent
, Scop
);
6294 Set_Encapsulating_State
(Ent
, Empty
);
6297 -----------------------
6298 -- Decorate_Variable --
6299 -----------------------
6301 procedure Decorate_Variable
(Ent
: Entity_Id
; Scop
: Entity_Id
) is
6303 Mutate_Ekind
(Ent
, E_Variable
);
6304 Set_Etype
(Ent
, Standard_Void_Type
);
6305 Set_Scope
(Ent
, Scop
);
6306 end Decorate_Variable
;
6308 -------------------------------------
6309 -- Process_Declarations_And_States --
6310 -------------------------------------
6312 procedure Process_Declarations_And_States
6316 Create_Abstract_Views
: Boolean)
6318 procedure Find_And_Process_States
;
6319 -- Determine whether package Pack defines abstract state either by
6320 -- using an aspect or a pragma. If this is the case, build shadow
6321 -- entities for all abstract states of Pack.
6323 procedure Process_States
(States
: Elist_Id
);
6324 -- Generate shadow entities for all abstract states in list States
6326 -----------------------------
6327 -- Find_And_Process_States --
6328 -----------------------------
6330 procedure Find_And_Process_States
is
6331 procedure Process_State
(State
: Node_Id
);
6332 -- Generate shadow entities for a single abstract state or
6333 -- multiple states expressed as an aggregate.
6339 procedure Process_State
(State
: Node_Id
) is
6340 Loc
: constant Source_Ptr
:= Sloc
(State
);
6347 -- Multiple abstract states appear as an aggregate
6349 if Nkind
(State
) = N_Aggregate
then
6350 Elmt
:= First
(Expressions
(State
));
6351 while Present
(Elmt
) loop
6352 Process_State
(Elmt
);
6358 -- A null state has no abstract view
6360 elsif Nkind
(State
) = N_Null
then
6363 -- State declaration with various options appears as an
6364 -- extension aggregate.
6366 elsif Nkind
(State
) = N_Extension_Aggregate
then
6367 Decl
:= Ancestor_Part
(State
);
6369 -- Simple state declaration
6371 elsif Nkind
(State
) = N_Identifier
then
6374 -- Possibly an illegal state declaration
6380 -- Abstract states are elaborated when the related pragma is
6381 -- elaborated. Since the withed package is not analyzed yet,
6382 -- the entities of the abstract states are not available. To
6383 -- overcome this complication, create the entities now and
6384 -- store them in their respective declarations. The entities
6385 -- are later used by routine Create_Abstract_State to declare
6386 -- and enter the states into visibility.
6388 if No
(Entity
(Decl
)) then
6389 Id
:= Make_Defining_Identifier
(Loc
, Chars
(Decl
));
6391 Set_Entity
(Decl
, Id
);
6392 Set_Parent
(Id
, State
);
6393 Decorate_State
(Id
, Scop
);
6395 -- Otherwise the package was previously withed
6398 Id
:= Entity
(Decl
);
6401 Build_Shadow_Entity
(Id
, Scop
, Dummy
);
6406 Pack_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Pack
);
6410 -- Start of processing for Find_And_Process_States
6413 -- Find aspect Abstract_State
6415 Asp
:= First
(Aspect_Specifications
(Pack_Decl
));
6416 while Present
(Asp
) loop
6417 if Chars
(Identifier
(Asp
)) = Name_Abstract_State
then
6418 Process_State
(Expression
(Asp
));
6426 -- Find pragma Abstract_State by inspecting the declarations
6428 Decl
:= First
(Decls
);
6429 while Present
(Decl
) and then Nkind
(Decl
) = N_Pragma
loop
6430 if Pragma_Name
(Decl
) = Name_Abstract_State
then
6433 (First
(Pragma_Argument_Associations
(Decl
))));
6440 end Find_And_Process_States
;
6442 --------------------
6443 -- Process_States --
6444 --------------------
6446 procedure Process_States
(States
: Elist_Id
) is
6451 Elmt
:= First_Elmt
(States
);
6452 while Present
(Elmt
) loop
6453 Build_Shadow_Entity
(Node
(Elmt
), Scop
, Dummy
);
6461 Is_Tagged
: Boolean;
6467 -- Start of processing for Process_Declarations_And_States
6470 -- Build abstract views for all states defined in the package
6472 if Create_Abstract_Views
then
6474 -- When a package has been analyzed, all states are stored in list
6475 -- Abstract_States. Generate the shadow entities directly.
6478 if Present
(Abstract_States
(Pack
)) then
6479 Process_States
(Abstract_States
(Pack
));
6482 -- The package may declare abstract states by using an aspect or a
6483 -- pragma. Attempt to locate one of these construct and if found,
6484 -- build the shadow entities.
6487 Find_And_Process_States
;
6491 -- Inspect the declarative list, looking for nested packages, types
6492 -- and variable declarations.
6494 Decl
:= First
(Decls
);
6495 while Present
(Decl
) loop
6499 if Nkind
(Decl
) = N_Package_Declaration
then
6500 Def_Id
:= Defining_Entity
(Decl
);
6502 -- Perform minor decoration when the withed package has not
6505 if not Is_Analyzed
then
6506 Decorate_Package
(Def_Id
, Scop
);
6509 -- Create a shadow entity that offers a limited view of all
6510 -- visible types declared within.
6512 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
6514 Process_Declarations_And_States
6517 Visible_Declarations
(Specification
(Decl
)),
6519 Create_Abstract_Views
=> Create_Abstract_Views
);
6523 elsif Nkind
(Decl
) in N_Full_Type_Declaration
6524 | N_Incomplete_Type_Declaration
6525 | N_Private_Extension_Declaration
6526 | N_Private_Type_Declaration
6527 | N_Protected_Type_Declaration
6528 | N_Task_Type_Declaration
6530 Def_Id
:= Defining_Entity
(Decl
);
6532 -- Determine whether the type is tagged. Note that packages
6533 -- included via a limited with clause are not always analyzed,
6534 -- hence the tree lookup rather than the use of attribute
6537 if Nkind
(Decl
) = N_Full_Type_Declaration
then
6538 Def
:= Type_Definition
(Decl
);
6541 (Nkind
(Def
) = N_Record_Definition
6542 and then Tagged_Present
(Def
))
6544 (Nkind
(Def
) = N_Derived_Type_Definition
6545 and then Present
(Record_Extension_Part
(Def
)));
6547 elsif Nkind
(Decl
) in N_Incomplete_Type_Declaration
6548 | N_Private_Type_Declaration
6550 Is_Tagged
:= Tagged_Present
(Decl
);
6552 elsif Nkind
(Decl
) = N_Private_Extension_Declaration
then
6559 -- Perform minor decoration when the withed package has not
6562 if not Is_Analyzed
then
6563 Decorate_Type
(Def_Id
, Scop
, Is_Tagged
, True);
6566 -- Create a shadow entity that hides the type and offers an
6567 -- incomplete view of the said type.
6569 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
, Is_Tagged
);
6573 elsif Create_Abstract_Views
6574 and then Nkind
(Decl
) = N_Object_Declaration
6575 and then not Constant_Present
(Decl
)
6577 Def_Id
:= Defining_Entity
(Decl
);
6579 -- Perform minor decoration when the withed package has not
6582 if not Is_Analyzed
then
6583 Decorate_Variable
(Def_Id
, Scop
);
6586 -- Create a shadow entity that hides the variable and offers an
6587 -- abstract view of the said variable.
6589 Build_Shadow_Entity
(Def_Id
, Scop
, Shadow
);
6594 end Process_Declarations_And_States
;
6598 Nam
: constant Node_Id
:= Name
(N
);
6599 Pack
: constant Entity_Id
:= Cunit_Entity
(Unum
);
6601 Last_Public_Shadow
: Entity_Id
:= Empty
;
6602 Private_Shadow
: Entity_Id
;
6605 -- Start of processing for Build_Limited_Views
6608 pragma Assert
(Limited_Present
(N
));
6610 -- A library_item mentioned in a limited_with_clause is a package
6611 -- declaration, not a subprogram declaration, generic declaration,
6612 -- generic instantiation, or package renaming declaration.
6614 case Nkind
(Unit
(Library_Unit
(N
))) is
6615 when N_Package_Declaration
=>
6618 when N_Subprogram_Declaration
=>
6620 ("subprogram not allowed in `LIMITED WITH` clause", N
);
6623 when N_Generic_Declaration
=>
6624 Error_Msg_N
("generic not allowed in `LIMITED WITH` clause", N
);
6627 when N_Generic_Instantiation
=>
6629 ("generic instantiation not allowed in `LIMITED WITH` clause",
6633 when N_Generic_Renaming_Declaration
=>
6635 ("generic renaming not allowed in `LIMITED WITH` clause", N
);
6638 when N_Subprogram_Renaming_Declaration
=>
6640 ("renamed subprogram not allowed in `LIMITED WITH` clause", N
);
6643 when N_Package_Renaming_Declaration
=>
6645 ("renamed package not allowed in `LIMITED WITH` clause", N
);
6649 raise Program_Error
;
6652 -- The withed unit may not be analyzed, but the with clause itself
6653 -- must be minimally decorated. This ensures that the checks on unused
6654 -- with clauses also process limieted withs.
6656 Mutate_Ekind
(Pack
, E_Package
);
6657 Set_Is_Not_Self_Hidden
(Pack
);
6658 Set_Etype
(Pack
, Standard_Void_Type
);
6660 if Is_Entity_Name
(Nam
) then
6661 Set_Entity
(Nam
, Pack
);
6663 elsif Nkind
(Nam
) = N_Selected_Component
then
6664 Set_Entity
(Selector_Name
(Nam
), Pack
);
6667 -- Check if the chain is already built
6669 Spec
:= Specification
(Unit
(Library_Unit
(N
)));
6671 if Limited_View_Installed
(Spec
) then
6675 -- Create the shadow package wich hides the withed unit and provides
6676 -- incomplete view of all types and packages declared within.
6678 Shadow_Pack
:= Make_Temporary
(Sloc
(N
), 'Z');
6679 Mutate_Ekind
(Shadow_Pack
, E_Package
);
6680 Set_Is_Internal
(Shadow_Pack
);
6681 Set_Limited_View
(Pack
, Shadow_Pack
);
6683 -- Inspect the abstract states and visible declarations of the withed
6684 -- unit and create shadow entities that hide existing packages, states,
6685 -- variables and types.
6687 Process_Declarations_And_States
6689 Decls
=> Visible_Declarations
(Spec
),
6691 Create_Abstract_Views
=> True);
6693 Last_Public_Shadow
:= Last_Shadow
;
6695 -- Ada 2005 (AI-262): Build the limited view of the private declarations
6696 -- to accommodate limited private with clauses.
6698 Process_Declarations_And_States
6700 Decls
=> Private_Declarations
(Spec
),
6702 Create_Abstract_Views
=> False);
6704 if Present
(Last_Public_Shadow
) then
6705 Private_Shadow
:= Next_Entity
(Last_Public_Shadow
);
6707 Private_Shadow
:= First_Entity
(Shadow_Pack
);
6710 Set_First_Private_Entity
(Shadow_Pack
, Private_Shadow
);
6711 Set_Limited_View_Installed
(Spec
);
6712 end Build_Limited_Views
;
6714 ----------------------------
6715 -- Check_No_Elab_Code_All --
6716 ----------------------------
6718 procedure Check_No_Elab_Code_All
(N
: Node_Id
) is
6720 if Present
(No_Elab_Code_All_Pragma
)
6721 and then In_Extended_Main_Source_Unit
(N
)
6722 and then Present
(Context_Items
(N
))
6725 CL
: constant List_Id
:= Context_Items
(N
);
6730 while Present
(CI
) loop
6731 if Nkind
(CI
) = N_With_Clause
6733 No_Elab_Code_All
(Get_Source_Unit
(Library_Unit
(CI
)))
6735 -- In GNATprove mode, some runtime units are implicitly
6736 -- loaded to make their entities available for analysis. In
6737 -- this case, ignore violations of No_Elaboration_Code_All
6738 -- for this special analysis mode.
6741 (GNATprove_Mode
and then Implicit_With
(CI
))
6743 Error_Msg_Sloc
:= Sloc
(No_Elab_Code_All_Pragma
);
6745 ("violation of No_Elaboration_Code_All#", CI
);
6747 ("\unit& does not have No_Elaboration_Code_All",
6748 CI
, Entity
(Name
(CI
)));
6755 end Check_No_Elab_Code_All
;
6757 -------------------------------
6758 -- Check_Body_Needed_For_SAL --
6759 -------------------------------
6761 procedure Check_Body_Needed_For_SAL
(Unit_Name
: Entity_Id
) is
6762 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean;
6763 -- Determine whether use of entity E might require the presence of its
6764 -- body. For a package this requires a recursive traversal of all nested
6767 -----------------------
6768 -- Entity_Needs_Body --
6769 -----------------------
6771 function Entity_Needs_Body
(E
: Entity_Id
) return Boolean is
6775 if Is_Subprogram
(E
) and then Has_Pragma_Inline
(E
) then
6778 elsif Is_Generic_Subprogram
(E
) then
6780 -- A generic subprogram always requires the presence of its
6781 -- body because an instantiation needs both templates. The only
6782 -- exceptions is a generic subprogram renaming. In this case the
6783 -- body is needed only when the template is declared outside the
6784 -- compilation unit being checked.
6786 if Present
(Renamed_Entity
(E
)) then
6787 return not Within_Scope
(E
, Unit_Name
);
6792 elsif Ekind
(E
) = E_Generic_Package
6794 Nkind
(Unit_Declaration_Node
(E
)) = N_Generic_Package_Declaration
6795 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6799 elsif Ekind
(E
) = E_Package
6800 and then Nkind
(Unit_Declaration_Node
(E
)) = N_Package_Declaration
6801 and then Present
(Corresponding_Body
(Unit_Declaration_Node
(E
)))
6803 Ent
:= First_Entity
(E
);
6804 while Present
(Ent
) loop
6805 if Entity_Needs_Body
(Ent
) then
6817 end Entity_Needs_Body
;
6819 -- Start of processing for Check_Body_Needed_For_SAL
6822 if Ekind
(Unit_Name
) = E_Generic_Package
6823 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6824 N_Generic_Package_Declaration
6826 Present
(Corresponding_Body
(Unit_Declaration_Node
(Unit_Name
)))
6828 Set_Body_Needed_For_SAL
(Unit_Name
);
6830 elsif Ekind
(Unit_Name
) in E_Generic_Procedure | E_Generic_Function
then
6831 Set_Body_Needed_For_SAL
(Unit_Name
);
6833 elsif Is_Subprogram
(Unit_Name
)
6834 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) =
6835 N_Subprogram_Declaration
6836 and then Has_Pragma_Inline
(Unit_Name
)
6838 Set_Body_Needed_For_SAL
(Unit_Name
);
6840 elsif Ekind
(Unit_Name
) = E_Subprogram_Body
then
6841 Check_Body_Needed_For_SAL
6842 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6844 elsif Ekind
(Unit_Name
) = E_Package
6845 and then Entity_Needs_Body
(Unit_Name
)
6847 Set_Body_Needed_For_SAL
(Unit_Name
);
6849 elsif Ekind
(Unit_Name
) = E_Package_Body
6850 and then Nkind
(Unit_Declaration_Node
(Unit_Name
)) = N_Package_Body
6852 Check_Body_Needed_For_SAL
6853 (Corresponding_Spec
(Unit_Declaration_Node
(Unit_Name
)));
6855 end Check_Body_Needed_For_SAL
;
6857 --------------------
6858 -- Remove_Context --
6859 --------------------
6861 procedure Remove_Context
(N
: Node_Id
) is
6862 Lib_Unit
: constant Node_Id
:= Unit
(N
);
6865 -- If this is a child unit, first remove the parent units
6867 if Is_Child_Spec
(Lib_Unit
) then
6868 Remove_Parents
(Lib_Unit
);
6871 Remove_Context_Clauses
(N
);
6874 ----------------------------
6875 -- Remove_Context_Clauses --
6876 ----------------------------
6878 procedure Remove_Context_Clauses
(N
: Node_Id
) is
6880 Unit_Name
: Entity_Id
;
6883 -- Ada 2005 (AI-50217): We remove the context clauses in two phases:
6884 -- limited-views first and regular-views later (to maintain the stack
6887 -- First Phase: Remove limited_with context clauses
6889 Item
:= First
(Context_Items
(N
));
6890 while Present
(Item
) loop
6892 -- We are interested only in with clauses that got installed on entry
6894 if Nkind
(Item
) = N_With_Clause
6895 and then Limited_Present
(Item
)
6897 if Limited_View_Installed
(Item
) then
6898 Remove_Limited_With_Clause
(Item
);
6900 -- An unusual case: If the library unit of the Main_Unit has a
6901 -- limited with_clause on some unit P and the context somewhere
6902 -- includes a with_clause on P, P has been analyzed. The entity
6903 -- for P is still visible, which in general is harmless because
6904 -- this is the end of the compilation, but it can affect pending
6905 -- instantiations that may have been generated elsewhere, so it
6906 -- it is necessary to remove U from visibility so that inlining
6907 -- and the analysis of instance bodies can proceed cleanly.
6909 elsif Current_Sem_Unit
= Main_Unit
6910 and then Serious_Errors_Detected
= 0
6911 and then not Implicit_With
(Item
)
6913 Set_Is_Immediately_Visible
6914 (Defining_Entity
(Unit
(Library_Unit
(Item
))), False);
6921 -- Second Phase: Loop through context items and undo regular
6922 -- with_clauses and use_clauses.
6924 Item
:= First
(Context_Items
(N
));
6925 while Present
(Item
) loop
6927 -- We are interested only in with clauses which got installed on
6928 -- entry, as indicated by their Context_Installed flag set
6930 if Nkind
(Item
) = N_With_Clause
6931 and then Limited_Present
(Item
)
6932 and then Limited_View_Installed
(Item
)
6936 elsif Nkind
(Item
) = N_With_Clause
6937 and then Context_Installed
(Item
)
6939 -- Remove items from one with'ed unit
6941 Unit_Name
:= Entity
(Name
(Item
));
6942 Remove_Unit_From_Visibility
(Unit_Name
);
6943 Set_Context_Installed
(Item
, False);
6945 elsif Nkind
(Item
) = N_Use_Package_Clause
then
6946 End_Use_Package
(Item
);
6948 elsif Nkind
(Item
) = N_Use_Type_Clause
then
6949 End_Use_Type
(Item
);
6954 end Remove_Context_Clauses
;
6956 --------------------------------
6957 -- Remove_Limited_With_Clause --
6958 --------------------------------
6960 procedure Remove_Limited_With_Clause
(N
: Node_Id
) is
6961 Pack_Decl
: constant Entity_Id
:= Unit
(Library_Unit
(N
));
6964 pragma Assert
(Limited_View_Installed
(N
));
6966 -- Limited with clauses that designate units other than packages are
6967 -- illegal and are never installed.
6969 if Nkind
(Pack_Decl
) = N_Package_Declaration
then
6970 Remove_Limited_With_Unit
(Pack_Decl
, N
);
6973 -- Indicate that the limited views of the clause have been removed
6975 Set_Limited_View_Installed
(N
, False);
6976 end Remove_Limited_With_Clause
;
6978 ------------------------------
6979 -- Remove_Limited_With_Unit --
6980 ------------------------------
6982 procedure Remove_Limited_With_Unit
6983 (Pack_Decl
: Node_Id
;
6984 Lim_Clause
: Node_Id
:= Empty
)
6986 procedure Remove_Shadow_Entities_From_Visibility
(Pack_Id
: Entity_Id
);
6987 -- Remove the shadow entities of package Pack_Id from direct visibility
6989 procedure Remove_Shadow_Entities_With_Restore
(Pack_Id
: Entity_Id
);
6990 -- Remove the shadow entities of package Pack_Id from direct visibility,
6991 -- restore the corresponding entities they hide into direct visibility,
6992 -- and update the entity and homonym chains.
6994 --------------------------------------------
6995 -- Remove_Shadow_Entities_From_Visibility --
6996 --------------------------------------------
6998 procedure Remove_Shadow_Entities_From_Visibility
(Pack_Id
: Entity_Id
) is
6999 Lim_Header
: constant Entity_Id
:= Limited_View
(Pack_Id
);
7000 Upto
: constant Entity_Id
:= First_Private_Entity
(Lim_Header
);
7005 -- Remove the package from direct visibility
7008 Set_Is_Immediately_Visible
(Pack_Id
, False);
7010 -- Remove all shadow entities from direct visibility
7012 Shadow
:= First_Entity
(Lim_Header
);
7013 while Present
(Shadow
) and then Shadow
/= Upto
loop
7015 Next_Entity
(Shadow
);
7017 end Remove_Shadow_Entities_From_Visibility
;
7019 -----------------------------------------
7020 -- Remove_Shadow_Entities_With_Restore --
7021 -----------------------------------------
7023 -- This code must be kept synchronized with the code that replaces the
7024 -- real entities by the shadow entities in Install_Limited_With_Clause,
7025 -- otherwise the contents of the homonym chains are not consistent.
7027 procedure Remove_Shadow_Entities_With_Restore
(Pack_Id
: Entity_Id
) is
7028 procedure Restore_Chain_For_Shadow
(Shadow
: Entity_Id
);
7029 -- Remove shadow entity Shadow by updating the entity and homonym
7032 procedure Restore_Chains
7035 -- Remove a sequence of shadow entities starting from From and ending
7036 -- prior to Upto by updating the entity and homonym chains.
7038 procedure Restore_Type_Visibility
7041 -- Restore a sequence of types starting from From and ending prior to
7042 -- Upto back in direct visibility.
7044 ------------------------------
7045 -- Restore_Chain_For_Shadow --
7046 ------------------------------
7048 procedure Restore_Chain_For_Shadow
(Shadow
: Entity_Id
) is
7049 Typ
: constant Entity_Id
:= Non_Limited_View
(Shadow
);
7050 pragma Assert
(not In_Chain
(Typ
));
7055 -- If the package has incomplete types, the limited view of the
7056 -- incomplete type is in fact never visible (AI05-129) but we
7057 -- have created a shadow entity E1 for it, that points to E2,
7058 -- the incomplete type at stake. This in turn has a full view
7059 -- E3 that is the full declaration, with a corresponding
7060 -- shadow entity E4. When reinstalling the nonlimited view,
7061 -- the visible entity E4 is replaced directly with E2 in the
7062 -- the homonyms list and E3 is simply ignored.
7064 -- regular views limited views
7066 -- * E2 (incomplete) <-- E1 (shadow)
7071 -- E3 (full) <-- E4 (shadow) *
7073 -- [*] denotes the visible entity (Current_Entity)
7075 Prev
:= Current_Entity
(Shadow
);
7077 while Present
(Prev
) loop
7078 -- This is a regular replacement
7080 if Prev
= Shadow
then
7081 Replace
(Prev
, Typ
);
7083 if Debug_Flag_I
then
7084 Write_Str
(" (homonym) replace ");
7085 Write_Name
(Chars
(Typ
));
7091 -- This is where E4 is replaced with E2
7093 elsif Ekind
(Prev
) = E_Incomplete_Type
7094 and then From_Limited_With
(Prev
)
7095 and then Ekind
(Typ
) = E_Incomplete_Type
7096 and then Full_View
(Typ
) = Non_Limited_View
(Prev
)
7098 Replace
(Prev
, Typ
);
7100 if Debug_Flag_I
then
7101 Write_Str
(" (homonym) E4 -> E2 ");
7102 Write_Name
(Chars
(Typ
));
7109 Prev
:= Homonym
(Prev
);
7111 end Restore_Chain_For_Shadow
;
7113 --------------------
7114 -- Restore_Chains --
7115 --------------------
7117 procedure Restore_Chains
7125 while Present
(Shadow
) and then Shadow
/= Upto
loop
7127 -- Do not unchain nested packages and child units
7129 if Ekind
(Shadow
) = E_Package
then
7132 elsif Is_Child_Unit
(Non_Limited_View
(Shadow
)) then
7136 Restore_Chain_For_Shadow
(Shadow
);
7139 Next_Entity
(Shadow
);
7143 -----------------------------
7144 -- Restore_Type_Visibility --
7145 -----------------------------
7147 procedure Restore_Type_Visibility
7155 while Present
(Typ
) and then Typ
/= Upto
loop
7156 if Is_Type
(Typ
) then
7157 Set_Is_Hidden
(Typ
, Was_Hidden
(Typ
));
7162 end Restore_Type_Visibility
;
7166 Lim_Header
: constant Entity_Id
:= Limited_View
(Pack_Id
);
7168 -- Start of processing Remove_Shadow_Entities_With_Restore
7171 -- The limited view of a package is being uninstalled by removing
7172 -- the effects of a limited with clause. If the clause appears in a
7173 -- unit which is not part of the main unit closure, then the related
7174 -- package must not be visible.
7176 if Present
(Lim_Clause
)
7177 and then not In_Extended_Main_Source_Unit
(Lim_Clause
)
7179 Set_Is_Immediately_Visible
(Pack_Id
, False);
7181 -- Otherwise a limited view is being overridden by a nonlimited view.
7182 -- Leave the visibility of the package as is because the unit must be
7183 -- visible when the nonlimited view is installed.
7189 -- Remove the shadow entities from visibility by updating the entity
7190 -- and homonym chains.
7193 (From
=> First_Entity
(Lim_Header
),
7194 Upto
=> First_Private_Entity
(Lim_Header
));
7196 -- Reinstate the types that were hidden by the shadow entities back
7197 -- into direct visibility.
7199 Restore_Type_Visibility
7200 (From
=> First_Entity
(Pack_Id
),
7201 Upto
=> First_Private_Entity
(Pack_Id
));
7202 end Remove_Shadow_Entities_With_Restore
;
7206 Pack_Id
: constant Entity_Id
:= Defining_Entity
(Pack_Decl
);
7208 -- Start of processing for Remove_Limited_With_Unit
7211 -- Nothing to do when the limited view of the package is not installed
7213 if not From_Limited_With
(Pack_Id
) then
7217 if Debug_Flag_I
then
7218 Write_Str
("remove limited view of ");
7219 Write_Name
(Chars
(Pack_Id
));
7220 Write_Str
(" from visibility");
7224 -- The package already appears in the compilation closure. As a result,
7225 -- its shadow entities must be replaced by the real entities they hide
7226 -- and the previously hidden entities must be entered back into direct
7229 if Analyzed
(Pack_Decl
) then
7230 Remove_Shadow_Entities_With_Restore
(Pack_Id
);
7232 -- Otherwise the package is not analyzed and its shadow entities must be
7233 -- removed from direct visibility.
7236 Remove_Shadow_Entities_From_Visibility
(Pack_Id
);
7239 -- Indicate that the limited view of the package is not installed
7241 Set_From_Limited_With
(Pack_Id
, False);
7242 end Remove_Limited_With_Unit
;
7244 --------------------
7245 -- Remove_Parents --
7246 --------------------
7248 procedure Remove_Parents
(Lib_Unit
: Node_Id
) is
7251 P_Spec
: Node_Id
:= Empty
;
7253 Vis
: constant Boolean :=
7254 Scope_Stack
.Table
(Scope_Stack
.Last
).Previous_Visibility
;
7257 if Is_Child_Spec
(Lib_Unit
) then
7258 P_Spec
:= Parent_Spec
(Lib_Unit
);
7260 elsif Nkind
(Lib_Unit
) = N_Package_Body
7261 and then Nkind
(Original_Node
(Lib_Unit
)) = N_Package_Instantiation
7263 P_Spec
:= Parent_Spec
(Original_Node
(Lib_Unit
));
7266 if Present
(P_Spec
) then
7268 P_Name
:= Get_Parent_Entity
(P
);
7269 Remove_Context_Clauses
(P_Spec
);
7270 End_Package_Scope
(P_Name
);
7271 Set_Is_Immediately_Visible
(P_Name
, Vis
);
7273 -- Remove from visibility the siblings as well, which are directly
7274 -- visible while the parent is in scope.
7276 E
:= First_Entity
(P_Name
);
7277 while Present
(E
) loop
7278 if Is_Child_Unit
(E
) then
7279 Set_Is_Immediately_Visible
(E
, False);
7285 Set_In_Package_Body
(P_Name
, False);
7287 -- This is the recursive call to remove the context of any higher
7288 -- level parent. This recursion ensures that all parents are removed
7289 -- in the reverse order of their installation.
7295 ---------------------------------
7296 -- Remove_Private_With_Clauses --
7297 ---------------------------------
7299 procedure Remove_Private_With_Clauses
(Comp_Unit
: Node_Id
) is
7302 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean;
7303 -- Check whether a given unit appears in a regular with_clause. Used to
7304 -- determine whether a private_with_clause, implicit or explicit, should
7307 ----------------------------
7308 -- In_Regular_With_Clause --
7309 ----------------------------
7311 function In_Regular_With_Clause
(E
: Entity_Id
) return Boolean is
7315 Item
:= First
(Context_Items
(Comp_Unit
));
7317 while Present
(Item
) loop
7318 if Nkind
(Item
) = N_With_Clause
7320 -- The following guard is needed to ensure that the name has
7321 -- been properly analyzed before we go fetching its entity.
7323 and then Is_Entity_Name
(Name
(Item
))
7324 and then Entity
(Name
(Item
)) = E
7325 and then not Private_Present
(Item
)
7334 end In_Regular_With_Clause
;
7336 -- Start of processing for Remove_Private_With_Clauses
7339 Item
:= First
(Context_Items
(Comp_Unit
));
7340 while Present
(Item
) loop
7341 if Nkind
(Item
) = N_With_Clause
and then Private_Present
(Item
) then
7343 -- If private_with_clause is redundant, remove it from context,
7344 -- as a small optimization to subsequent handling of private_with
7345 -- clauses in other nested packages. We replace the clause with
7346 -- a null statement, which is otherwise ignored by the rest of
7349 if In_Regular_With_Clause
(Entity
(Name
(Item
))) then
7351 Nxt
: constant Node_Id
:= Next
(Item
);
7353 Rewrite
(Item
, Make_Null_Statement
(Sloc
(Item
)));
7358 elsif Limited_Present
(Item
) then
7359 if not Limited_View_Installed
(Item
) then
7360 Remove_Limited_With_Clause
(Item
);
7366 Remove_Unit_From_Visibility
(Entity
(Name
(Item
)));
7367 Set_Context_Installed
(Item
, False);
7375 end Remove_Private_With_Clauses
;
7377 ---------------------------------
7378 -- Remove_Unit_From_Visibility --
7379 ---------------------------------
7381 procedure Remove_Unit_From_Visibility
(Unit_Name
: Entity_Id
) is
7383 if Debug_Flag_I
then
7384 Write_Str
("remove unit ");
7385 Write_Name
(Chars
(Unit_Name
));
7386 Write_Str
(" from visibility");
7390 Set_Is_Visible_Lib_Unit
(Unit_Name
, False);
7391 Set_Is_Potentially_Use_Visible
(Unit_Name
, False);
7392 Set_Is_Immediately_Visible
(Unit_Name
, False);
7394 -- If the unit is a wrapper package, the subprogram instance is
7395 -- what must be removed from visibility.
7396 -- Should we use Related_Instance instead???
7398 if Is_Wrapper_Package
(Unit_Name
) then
7399 Set_Is_Immediately_Visible
(Current_Entity
(Unit_Name
), False);
7401 end Remove_Unit_From_Visibility
;
7416 procedure Replace
(Old_E
, New_E
: Entity_Id
) is
7420 Prev
:= Current_Entity
(Old_E
);
7425 elsif Prev
= Old_E
then
7426 Set_Current_Entity
(New_E
);
7427 Set_Homonym
(New_E
, Homonym
(Old_E
));
7430 while Present
(Prev
) and then Homonym
(Prev
) /= Old_E
loop
7431 Prev
:= Homonym
(Prev
);
7434 if Present
(Prev
) then
7435 Set_Homonym
(Prev
, New_E
);
7436 Set_Homonym
(New_E
, Homonym
(Old_E
));
7445 procedure Unchain
(E
: Entity_Id
) is
7449 Prev
:= Current_Entity
(E
);
7455 Set_Name_Entity_Id
(Chars
(E
), Homonym
(E
));
7458 while Present
(Prev
) and then Homonym
(Prev
) /= E
loop
7459 Prev
:= Homonym
(Prev
);
7462 if Present
(Prev
) then
7463 Set_Homonym
(Prev
, Homonym
(E
));
7467 if Debug_Flag_I
then
7468 Write_Str
(" (homonym) unchain ");
7469 Write_Name
(Chars
(E
));